7ab9b8e
#!/bin/sh
7ab9b8e
7ab9b8e
case "$1" in -h|-H|-help|--help)
7ab9b8e
	cat <
7ab9b8e
Usage: ${0##*/} [-shared]
7ab9b8e
       -shared  also attempt to build a shared library
7ab9b8e
Env:   CC       explicit "cc" compiler to use
7ab9b8e
       AR       explicit "ar" to use
7ab9b8e
       RANLIB   explicit "ranlib" to use
7ab9b8e
       CFLAGS   explicit compiler options to use
7ab9b8e
       FPIC     explicit -fPIC/-fpic (or none) option to use
7ab9b8e
EOT
7ab9b8e
	exit 0
7ab9b8e
esac
7ab9b8e
7ab9b8e
set -e
7ab9b8e
7ab9b8e
LIB=libBlocksRuntime.a
7ab9b8e
SRC=BlocksRuntime
7ab9b8e
7ab9b8e
shared=
7ab9b8e
[ z"$1" != z"-shared" -a z"$1" != z"--shared" ] || shared=1
7ab9b8e
7ab9b8e
if [ -n "$shared" ]; then
7ab9b8e
	UNAME_S="$(uname -s 2>/dev/null)" || :
7ab9b8e
	case "$UNAME_S" in
7ab9b8e
		Darwin)
7ab9b8e
			SHLIB="${LIB%.a}.dylib"
7ab9b8e
			SHOPT="-dynamiclib -Wl,-all_load"
7ab9b8e
			SHOPT2=
7ab9b8e
			;;
7ab9b8e
		*)
7ab9b8e
			SHLIB="${LIB%.a}.so.0.$2"
7ab9b8e
			SHOPT="-s -shared -Wl,-soname,${LIB%.a}.so.0 -Wl,-whole-archive"
7ab9b8e
			SHOPT2="-Wl,-no-whole-archive"
7ab9b8e
			;;
7ab9b8e
	esac
7ab9b8e
fi
7ab9b8e
7ab9b8e
if [ -z "$CC" ]; then
7ab9b8e
	if command -v gcc > /dev/null; then
7ab9b8e
		CC=gcc
7ab9b8e
	elif command -v clang > /dev/null; then
7ab9b8e
		CC=clang
7ab9b8e
	elif command -v cc > /dev/null; then
7ab9b8e
		CC=cc
7ab9b8e
	else
7ab9b8e
		echo "Could not guess name of compiler, please set CC" >&2
7ab9b8e
		exit 2
7ab9b8e
	fi
7ab9b8e
fi
7ab9b8e
7ab9b8e
echo "CC=$CC"
7ab9b8e
: ${AR:=ar}
7ab9b8e
echo "AR=$AR"
7ab9b8e
: ${RANLIB:=ranlib}
7ab9b8e
echo "RANLIB=$RANLIB"
7ab9b8e
7ab9b8e
if [ "${CFLAGS+set}" != "set" ]; then
7ab9b8e
	case "$CC" in
7ab9b8e
		*gcc*|*clang*)
7ab9b8e
			CFLAGS=-O2
7ab9b8e
			;;
7ab9b8e
		*)
7ab9b8e
			CFLAGS=-O
7ab9b8e
			;;
7ab9b8e
	esac
7ab9b8e
fi
7ab9b8e
7ab9b8e
has_cc_opt()
7ab9b8e
{
7ab9b8e
	"$CC" "$1" -o /tmp/cc.$$ -c -x c /dev/null >/dev/null 2>&1 &&
7ab9b8e
	rm -f /tmp/cc.$$
7ab9b8e
}
7ab9b8e
if [ "${FPIC+set}" != "set" ]; then
7ab9b8e
	if has_cc_opt "-fPIC"; then
7ab9b8e
		FPIC="-fPIC"
7ab9b8e
	elif has_cc_opt "-fpic"; then
7ab9b8e
		FPIC="-fpic"
7ab9b8e
	fi
7ab9b8e
fi
7ab9b8e
[ -z "$FPIC" ] || echo "FPIC=$FPIC"
7ab9b8e
7ab9b8e
echo "CFLAGS=$CFLAGS"
7ab9b8e
7ab9b8e
echo "LIB=$LIB"
7ab9b8e
[ -z "$shared" ] || echo "SHLIB=$SHLIB"
7ab9b8e
[ -z "$shared" ] || echo "SHOPT=$SHOPT"
7ab9b8e
[ -z "$shared" ] || [ -z "$SHOPT2" ] || echo "SHOPT2=$SHOPT2"
7ab9b8e
echo "SRC=$SRC"
7ab9b8e
7ab9b8e
(
7ab9b8e
	PS4= && set -ex
7ab9b8e
	! test -e $LIB || rm $LIB
7ab9b8e
) || exit
7ab9b8e
[ -z "$shared" ] ||
7ab9b8e
(
7ab9b8e
	PS4= && set -ex
7ab9b8e
	! test -e $SHLIB || rm $SHLIB
7ab9b8e
) || exit
7ab9b8e
(
7ab9b8e
	PS4= && set -ex
7ab9b8e
	"$CC" -c $FPIC $CFLAGS -o $SRC/data.o $SRC/data.c &&
7ab9b8e
	"$CC" -c $FPIC $CFLAGS -o $SRC/runtime.o -I . $SRC/runtime.c &&
7ab9b8e
	"$AR" cr $LIB $SRC/data.o $SRC/runtime.o &&
7ab9b8e
	"$RANLIB" $LIB
7ab9b8e
) || exit
7ab9b8e
[ -z "$shared" ] ||
7ab9b8e
(
7ab9b8e
	PS4= && set -ex
7ab9b8e
	"$CC" $FPIC $CFLAGS -o "$SHLIB" $SHOPT $LIB $SHOPT2
7ab9b8e
) || exit