#!/bin/sh

SYS=`uname -s`

echo "Configuring nwcc for $SYS ... "

GCCPATHS="gcc /usr/local/bin/gcc"

if test "$SYS" = "SunOS"; then
	AWK=nawk

	# I've had problems with gcc on Solaris/SPARC; it does not like
	# many things relating to static variables. The most easily
	# reproducible problem is tried below

	# First check whether we have gcc at all
	echo 'int main() { return 0; }' >_testprog.c

	echo "Checking for gcc/SPARC bug ..."

	for i in $GCCPATHS; do
		/usr/xpg4/bin/sh -c "$i _testprog.c -o _testprog 2>/dev/null >/dev/null"
		if test -x _testprog; then
			# Yes, we have gcc! Now check whether it has the
			# static variable bugs
			cat >_testprog.c <<EOF
#include <stdio.h>
int
main() {
	static int	foo;
	printf("%d\n", foo);
	return 0;
}
EOF
			/usr/xpg4/bin/sh -c "$i _testprog.c -o _testprog 2>/dev/null >/dev/null"
			if test -x _testprog; then
				if test `./_testprog` != "0"; then
			echo "    gcc looks buggy on this system!"
			echo "    Will attempt to use Sun's compiler instead."
			echo "    If you only have gcc, you should upgrade to"
			echo "    a newer version, or get Sun's compiler"
			echo "    (which is free now.)"
					GCCPATHS=""
					ABI=-xarch=v9
				else
			echo "    gcc looks usable on this system!"
			echo "    If, however, it generates bad nwcc binaries,"
			echo "    you should upgrade to a newer version, or"
			echo "    get Sun's compiler (which is free now.)"
					ABI=-m64
				fi
			fi
			break
		fi
	done
else
	AWK=awk
fi

rm -f config.h
echo "/* Generated configuration header - do not edit! */" >config.h
echo "#ifndef _CONFIG_H" >>config.h
echo "#define _CONFIG_H" >>config.h

for i in "$@"; do
	FIRST=`echo $i | $AWK -F= '{ print $1 }'`
	SECOND=`echo $i | $AWK -F= '{ print $2 }'`
	if test "$FIRST" = "--installprefix"; then
		if test "$SECOND" = ""; then
			echo "Need argument for --installprefix!"
			exit 1
		fi
		echo "#define INSTALLDIR \"$SECOND\"" >>config.h
		echo "'make install' will install to \"$SECOND\""
	else
		if test "$FIRST" != "--help"; then
			echo "Unknown option \"$FIRST\""
		fi
		echo "Supported options are:"
		echo
		echo "--installprefix=/path/to/install/dir"
		echo
#		echo "You can set the CC environment variable if you wish"
#		echo "to use a non-default compiler for compilation."
#		echo
#		echo "E.g. in bash do \`CC=/usr/local/bin/gcc ./configure'"
#		echo "Or in tcsh \`setenv CC /usr/local/bin/gcc'"
		exit 1
	fi	
done

# Now we get the compiler if CC is not set. It's important to use a prototype
# for main and also the ``const'' keyword to rule out K&R compilers
if test "$CC" = ""; then
	echo 'int main(void) { const char *str = "hello world"; puts(str); }' \
		>_testprog.c
	for i in `echo $GCCPATHS cc /opt/SUNWspro/bin/cc xlC nwcc /usr/local/bin/nwcc`; do 
		if test "$SYS" = SunOS; then 
			/usr/xpg4/bin/sh -c \
			"$i _testprog.c -o _testprog 2>/dev/null >/dev/null"
		else
			$i _testprog.c -o _testprog 2>/dev/null >/dev/null
		fi
		if test -x _testprog; then
			CC=$i
			break;
		fi
	done
fi
if test "$CC" = ""; then
	echo
	echo "ERROR: Cannot find suitable C compiler! If you do have"
	echo "one, you may have to adjust your PATH environment"
	echo "variable. Otherwise you can set the CC variable to the"
	echo "full compiler path."
	exit 1
fi
echo "Using $CC as compiler ..."
if echo $CC | grep gcc >/dev/null; then
	CFLAGS='$(ABI) -Wall -W -ggdb -g -DHAVE_LLONG'
else
	CFLAGS='$(ABI) -g -DHAVE_LLONG'
fi
echo "    Setting CFLAGS to $CFLAGS"


echo 'int main() { puts(" all gnoooh you can eat"); }' >_test.c
gcc -E -D__GNUC__ _test.c >_junk 2>&1
OUTPUT=`cat _junk`
if echo $OUTPUT | grep __GNUC__ | grep warning >/dev/null; then
	echo '#define GNUBYDEFAULT' >>config.h
else
	touch config.h
fi
rm _test.c _junk >/dev/null 2>/dev/null

if test "$SYS" = "AIX"; then
	# XXX this assumes gcc and the compilation of extlibnwcc.o
	# really belongs into the Makefile :-(
	echo '#include <stdio.h>' >_test.c
	printf 'int main() { printf("%%d\\n", (int)sizeof(char *)); }\n' >>_test.c
	if gcc -maix64 _test.c -o a.out >/dev/null 2>/dev/null; then
		SIZE=`./a.out`
		if test "$SIZE" = "8"; then
			echo '#define HAVE_64BIT' >>config.h
			echo "Building 64bit libnwcc ..."
			gcc -maix64 -DEXTERNAL libnwcc.c -c -o extlibnwcc64.o 
			echo "Building 32bit libnwcc ..."
			gcc -maix32 -DEXTERNAL libnwcc.c -c -o extlibnwcc32.o
			echo
			echo "nwcc will be built as a 32bit program! Use"
			echo
			echo '      make ABI=-maix64'
			echo
			echo "to build 64bit binaries instead. Note that"
			echo "either way you can still use the -abi=aix32"
			echo "and -abi=aix64 nwcc command line flags to"
			echo "specifically request generating code of"
			echo "different bitness."
		fi
	fi
	rm -f _test.c a.out
elif test "$SYS" = SunOS; then
	if test `uname -p` = sparc; then
		# We have to use our own crt1.s
		# First build 32bit crt
		AS_ARGS_32="-DSPARC_PTR_SIZE=4 -DSPARC_PTR_SCALING=2"
		AS_ARGS_64="-DSPARC_PTR_SIZE=8 -DSPARC_PTR_SCALING=3"

		echo "Building 32bit crt1 ..."
		/usr/ccs/bin/as -P $AS_ARGS_32 -o crt1-32.o crt1-sparc.s

		echo 'int main() { return 0; }' >_test.c
		if gcc -m64 _test.c >/dev/null 2>/dev/null; then
			if ./a.out 2>/dev/null; then  
				# We're on a 64bit system - build 64bit crt1
				echo "Building 64bit crt1 ..."
				/usr/ccs/bin/as -xarch=v9 -P $AS_ARGS_64 -o crt1-64.o crt1-sparc.s
				echo "Building 64bit libnwcc ..."
				gcc -m64 -DEXTERNAL_USE libnwcc.c -c -o extlibnwcc64.o
			fi
		fi
	fi	
elif test "$SYS" = IRIX64; then
	echo "Building n64 libnwcc ..."
	gcc -mabi=64 -DEXTERNAL libnwcc.c -c -o extlibnwcc64.o 
	echo "Building n32 libnwcc ..."
	gcc -DEXTERNAL libnwcc.c -c -o extlibnwcc32.o
	echo
	echo "nwcc will be built as an n32 program! Use"
	echo
	echo '      make ABI=-mabi=64'
	echo
	echo "to build n64 binaries instead. Note that"
	echo "either way you can still use the -abi=n32"
	echo "and -abi=n64 nwcc command line flags to"
	echo "specifically request generating code for"
	echo "a different ABI."
fi

echo "#endif /* #ifndef _CONFIG_H */" >>config.h

# if test -f Make.old; then
# 	:
# else
# 	mv Makefile Make.old
# fi

echo "CC=$CC" >Makefile
echo "CFLAGS=$CFLAGS" >>Makefile
echo "ABI=$ABI" >>Makefile
cat Makefile.skel >> Makefile

echo
echo You may type \`make\' now



syntax highlighted by Code2HTML, v. 0.9.1