ObjFW  objfw-compile at [ddb15601a2]

File utils/objfw-compile artifact f4f00bf2af part of check-in ddb15601a2


#!/bin/sh
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" -o x"$2" = "x"; then
	echo "Syntax: objfw-compile 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
}

out="$1"
objs=""
link="no"
shift

case "$out" in
	*.c | *.C | *.cpp | *.cxx | *.C | *.h | *.m | *.mm | *.M)
		echo "The first paramter must be the output name!" 1>&2
		exit 1
		;;
esac

while test x"$1" != "x"; do
	case "$1" in
		*.m)
			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 ! -f "$out" -o x"$link" = x"yes"; then
	status_linking $out
	$OBJC -o $out $objs $LIBS $ENV_LIBS $LDFLAGS $ENV_LDFLAGS || \
		status_link_failed $out $?
	status_linked $out
fi