#!/bin/sh

test -f $PLAN9/config && . $PLAN9/config
usegcc()
{
	cc=${CC9:-gcc}
	ngflags=" \
		-O2 \
		-c \
		-Wall \
		-Wno-parentheses \
		-Wno-missing-braces \
		-Wno-switch \
		-Wno-comment \
		-Wno-sign-compare \
		-Wno-unknown-pragmas \
		-fno-omit-frame-pointer \
		-fsigned-char \
	"
	# want to put -fno-optimize-sibling-calls here but
	# that option only works with gcc3+ it seems
	cflags="$ngflags -ggdb"
}

quiet()
{
	# The uniq at the end is for gcc's strcmp/etc. built-in nonsense,
	# which multiplies single errors as a result of its expansion.
	# The "Cursor. is deprecated" kills off warnings from Apple
	# about using SetCursor/InitCursor.  (Okay, they're deprecated,
	# but you could at least tell us what to use instead, Apple!)

	ignore=': error: .Each undeclared identifier'
	ignore=$ignore'|: error: for each function it appears'
	ignore=$ignore'|is dangerous, better use'
	ignore=$ignore'|is almost always misused'
	ignore=$ignore'|: In function '
	ignore=$ignore'|: At top level:'
	ignore=$ignore'|support .long long.'
	ignore=$ignore'|In file included from'
	ignore=$ignore'|        from'
	ignore=$ignore'|use of C99 long long'
	ignore=$ignore'|ISO C forbids conversion'
	ignore=$ignore'|is deprecated'
	ignore=$ignore'|warn_unused_result'

	grep -v '__p9l_autolib_' $1 |
	egrep -v "$ignore" |
	sed 's/ .first use in this function.$//; s/\"\([^\"][^\"]*\)\", line \([0-9][0-9]*\)/\1:\2/g' |
	$(which uniq) 1>&2  # avoid built-in uniq on SunOS
}

useclang()
{
	cc=${CC9:-clang}
	ngflags=" \
		-O2 \
		-c \
		-Wall \
		-Wno-parentheses \
		-Wno-missing-braces \
		-Wno-switch \
		-Wno-comment \
		-Wno-sign-compare \
		-Wno-unknown-pragmas \
		-Wno-empty-body \
		-Wno-unused-value \
		-Wno-array-bounds \
		-Wno-gnu-designator \
		-Wno-array-bounds \
		-Wno-unneeded-internal-declaration \
		-fsigned-char \
		-fno-caret-diagnostics \
	"
	cflags="$ngflags -g"
}

tag="${SYSNAME:-`uname`}-${OBJTYPE:-`uname -m`}-${CC9:-cc}"
case "$tag" in
*FreeBSD*gcc*)	usegcc ;;
*FreeBSD*clang*)	useclang ;;
*DragonFly*|*BSD*)	usegcc ;;
*Darwin-x86_64*clang*)
		useclang
		cflags="$ngflags -g3 -m64"
		;;
*Darwin-x86_64*) usegcc
		cflags="$ngflags -g3 -no-cpp-precomp -m64" ;;
*Darwin*clang*)
		useclang
		cflags="$ngflags -g3 -m32"
		;;
*Darwin*)	usegcc 
		cflags="$ngflags -g3 -no-cpp-precomp -m32" ;;
*HP-UX*)	cc=${CC9:-cc}; cflags="-g -O -c -Ae" ;;
*Linux*)	usegcc 
		case "${CC9:-gcc}" in
		tcc)
			cc=tcc
			cflags="-c -g"
			;;
		esac
		case "${SYSVERSION:-`uname -r`}" in
		2.4.*)
			cflags="$cflags -D__Linux24__"
			;;
		2.6.*)
			cflags="$cflags -D__Linux26__"
			;;
		esac
		;;
*OSF1*)		cc=${CC9:-cc}; cflags="-g -O -c" ;;
*SunOS*-cc)	cc=cc;
		cflags="-mt -g -O -c -xCC -D__sun__" 
		u=`uname`
		v=`uname -r`
		s=`echo $u$v | tr '. ' '__'`
		cflags="$cflags -D__${s}__"
		;;
*SunOS*-gcc)	usegcc
		u=`uname`
		v=`uname -r`
		s=`echo $u$v | tr '. ' '__'`
		cflags="$ngflags -g"
		cflags="$cflags -D__sun__ -D__${s}__"
		;;
*AIX*)		usegcc
		cflags="$ngflags -g -D__AIX__"
		;;
*)
	echo 9c does not know how to compile on "$tag" 1>&2
	exit 1
esac

# N.B. Must use temp file to avoid pipe; pipe loses status.
xtmp=/tmp/9c.$$.$USER.out
$cc -DPLAN9PORT -I$PLAN9/include $cflags "$@" 2>$xtmp
status=$?
quiet $xtmp
rm -f $xtmp
exit $status
