ObjFW  Artifact [505c826ae7]

Artifact 505c826ae7f3510a41d989b56887c75a29f1aec9a46fc531759b9eee6a65a55e:


#!/bin/sh
if test x"$(basename $0)" != x"objfw-compile"; then
	OBJFW_CONFIG="$(basename $0 | sed 's/-objfw-compile$//')-objfw-config"
else
	OBJFW_CONFIG="objfw-config"
fi

if ! which $OBJFW_CONFIG 2>&1 >/dev/null; then
	echo "You need to have ObjFW and $OBJFW_CONFIG installed!"
	exit 1
fi

CPPFLAGS="$CPPFLAGS $($OBJFW_CONFIG --cppflags)"
OBJC="$($OBJFW_CONFIG --objc)"
OBJCFLAGS="$OBJCFLAGS $($OBJFW_CONFIG --objcflags)"
LIBS="$LIBS $($OBJFW_CONFIG --libs)"
LDFLAGS="$LDFLAGS $($OBJFW_CONFIG --ldflags)"

if test x"$1" = "x"; then
	echo "Syntax: objfw-compile -o outname source1.m source2.m ..."
	exit 1
fi

status_compiling() {
	printf "\033[K\033[0;33mCompiling \033[1;33m$1\033[0;33m...\033[0m\r"
}
status_compiled() {
	printf "\033[K\033[0;32mSuccessfully compiled \033[1;32m$1\033[0;32m."
	printf "\033[0m\n"
}
status_compile_failed() {
	printf "\033[K\033[0;31mFailed to compile \033[1;31m$1\033[0;31m!"
	printf "\033[0m\n"
	exit $2
}
status_linking() {
	printf "\033[K\033[0;33mLinking \033[1;33m$1\033[0;33m...\033[0m\r"
}
status_linked() {
	printf "\033[K\033[0;32mSuccessfully linked \033[1;32m$1\033[0;32m."
	printf "\033[0m\n"
}
status_link_failed() {
	printf "\033[K\033[0;31mFailed to link \033[1;31m$1\033[0;31m!"
	printf "\033[0m\n"
	exit $2
}

flags_done="no"
out=""
objs=""
link="no"
lib="no"
out_prefix=""
out_suffix=""

while test x"$1" != "x"; do
	case "$1" in
		-o|--out)
			shift
			out="$1"
			;;
		-l|--lib)
			if test x"flags_done" = x"yes"; then
				printf "The --lib flag needs to be specified "
				printf "before any source file!\n"
				exit 1
			fi

			shift

			if ! echo "$1" | grep "^[0-9]\+\.[0-9]\+" >/dev/null
			then
				echo "$1 is not a valid library version!"
				exit 1
			fi

			export LIB_MAJOR="${1%.*}"
			export LIB_MINOR="${1#*.}"

			lib="yes"
			OBJCFLAGS="$OBJCFLAGS $($OBJFW_CONFIG --lib-cflags)"
			LDFLAGS="$LDFLAGS $($OBJFW_CONFIG --lib-ldflags)"
			out_prefix="$($OBJFW_CONFIG --lib-prefix)"
			out_suffix="$($OBJFW_CONFIG --lib-suffix)"
			;;
		-*)
			echo "Unknown option: $1"
			exit 1
			;;
		*.m)
			flags_done="yes"
			obj="${1%.m}.o"
			objs="$objs $obj"
			build="no"
			deps=$($OBJC -E -M $CPPFLAGS $ENV_CPPFLAGS $1 | \
			       sed 's/.*: //' | sed 's/\\//g')

			if test -f "$obj"; then
				for dep in $deps; do
					test "$dep" -nt $obj && build="yes"
				done
			else
				build="yes"
			fi

			if test x"$build" = x"yes"; then
				link="yes"
				status_compiling $1
				$OBJC $CPPFLAGS $OBJCFLAGS -c -o $obj $1 || \
					status_compile_failed $1 $?
				status_compiled $1
			fi
			;;
		*)
			echo "Only .m files can be compiled!" 1>&2
			exit 1
			;;
	esac

	shift
done

if test x"$out" = x""; then
	echo "No output name specified! Use -o or --out!"
	exit 1
fi

test x"$lib" = x"no" && out_suffix="$($OBJFW_CONFIG --prog-suffix)"

if test ! -f "$out_prefix$out$out_suffix" -o x"$link" = x"yes"; then
	status_linking $out_prefix$out$out_suffix
	$OBJC -o $out_prefix$out$out_suffix $objs $LIBS $ENV_LIBS $LDFLAGS \
		$ENV_LDFLAGS || status_link_failed $out $?
	status_linked $out_prefix$out$out_suffix
fi