#!/bin/sh

##### process arguments of the script #####

while [ 1 -le $# ]
do
	case $1 in
	-l)
		OPTION=-l	# and link
		SOURCE=$2
		ROOT=$(basename $SOURCE .c)
		ROOT=$(basename $ROOT .cc)
		ROOT=$(basename $ROOT .cpp)
		TARGET=$OBJDIR/$ROOT
		ARGCOUNT=2
		;;
	-p*|-E*)
		OPTION=-E	# preprocess
		SOURCE=$2
		ROOT=$(basename $SOURCE .c)
		ROOT=$(basename $ROOT .cc)
		ROOT=$(basename $ROOT .cpp)
		TARGET=$OBJDIR/$ROOT.i
		ARGCOUNT=2
		;;
	-c*|-C*)
		OPTION=-c	# compile
		SOURCE=$2
		ROOT=$(basename $SOURCE .c)
		ROOT=$(basename $ROOT .cc)
		ROOT=$(basename $ROOT .cpp)
		TARGET=$OBJDIR/$ROOT$MK_OBJEXT
		DEPEND=$OBJDIR/$ROOT$MK_DEPEXT
		ARGCOUNT=2
		;;
	*)
		OPTION=-c	# default
		SOURCE=$1
		ROOT=$(basename $SOURCE .c)
		ROOT=$(basename $ROOT .cc)
		ROOT=$(basename $ROOT .cpp)
		TARGET=$OBJDIR/$ROOT$MK_OBJEXT
		DEPEND=$OBJDIR/$ROOT$MK_DEPEXT
		ARGCOUNT=1
		;;
	esac
	shift $ARGCOUNT
done

mkdir -p $(dirname $TARGET)

CMD=""
postpend() { CMD="$CMD $*" ; }
prepend() { CMD="$* $CMD" ; }
error() { echo "mk_cc : error : $*" ; exit 1 ; }

##### select possible compilers and flags #####

case $MK_OPT in
fuzz)	export AFL_QUIET=1
	: ${CC:=afl-clang-fast} ;;
*)	: ${CC:=gcc} ;;
esac

OFLAGS="-g3 -fno-strict-aliasing"

case $MK_OPT in
opt)	OFLAGS="$OFLAGS -O3 -ffunction-sections -fdata-sections";;
*)	OFLAGS="$OFLAGS -D_DEBUG=1";;
esac

case $MK_OPT in
cov)
	case $CC in
	*clang*) OFLAGS="$OFLAGS -O0 -fprofile-instr-generate -fcoverage-mapping" ;;
	*) OFLAGS="$OFLAGS --coverage" ;;
	esac ;;
fuzz)
	# N.B. we want to be able to test "#if FUZZING" in the source code.
	# "#if __AFL_COMPILER" is not good enough because even in a fuzzing
	# build, many files are compiled with a normal (non-afl) compiler,
	# because the resulting interpreter runs faster when only a few key
	# source files are actually instrumented.
	OFLAGS="$OFLAGS -DFUZZING" ;;
esac

postpend -fPIC

##### interpreter-specific flags #####

postpend -DxxBIT=$MK_BITS
postpend -DHAS_UNICODE=$MK_UNICODE
postpend -DUNIX=1
postpend -DRUNTIME=$MK_RUNTIME
postpend -D__STDC_WANT_DEC_FP__=0
postpend -fvisibility=hidden
postpend -DDYALOGRIDELIB=$MK_CONGADLLNAME

if [ $MK_HAS_HTMLRENDERER = false ]; then
	postpend -DHAS_HTML_RENDERER=0
fi

# This has the potential to generate smaller and/or faster code, and also
# helps detect multiple definitions of global variables.
postpend -fno-common

# Ubuntu defaults to -D_FORTIFY_SOURCE=2 which gives spurious build errors
postpend -U_FORTIFY_SOURCE 

# Ubuntu 14.10 defaults to -fstack-protector, which slows things down.
postpend -fno-stack-protector

##### target-specific flags #####

case $MK_AP_BUILD in
1)
	postpend -DAP_BUILD=1

	postpend -fpic		# for AP shared libraries

	postpend -I$MK_XFSRC
	postpend -I$MK_PORTDIR/src
	postpend -I$MK_SRCDIR
	;;
*)
	postpend -I$MK_PORTDIR/src
	postpend -I$MK_SRCDIR
	postpend -I$MK_PCRE_OBJ
	postpend -I$MK_HTTPINTERCEPTOR_ROOT/HTTPIntreceptorLibrary/HTTPInterceptor
	postpend -I$MK_HTTPINTERCEPTOR_ROOT/AplWrapper/AplWrapper
	postpend -DNO_HTTP_GUI
	;;
esac

postpend $C_FLAGS
postpend -I$OBJDIR

##### pick the compiler and flags we need for this file #####

case $OPTION in
-E)	
	prepend -E
	;;
-c)
	prepend -MMD -MF $DEPEND -MP
	prepend -o $TARGET
	prepend -c
	;;
-l)
	prepend -o $TARGET
	;;
esac

case "/$SOURCE" in
*/sse2.*) prepend -msse2 ;;
*/ssse3.*) prepend -mssse3 ;;
*/sse41.*) prepend -msse4.1 ;;
*/bmi2.*)
	# Check that the compiler recognises this option
	if $CC -mbmi2 -fsyntax-only -x c /dev/null 2> /dev/null ; then
		prepend -mbmi2 -mpopcnt
	fi
	;;
# Some gcc versions generate bad code for unaligned accesses with generic -march
*/arith_avx2.*) prepend -mavx2 -march=core-avx2 ;;
esac

case $SOURCE in
*.c)
	prepend -std=gnu99
	case $MK_AP_BUILD in
	1)
		prepend -Wimplicit	# implicit declarations
		prepend -Wcomment	# check for trailing backslash on comments
		;;
	*)
		prepend -Wstrict-prototypes
		# Turn on the bits of -Wall that are recognised by all GCC 4.x
		# versions, except for some that are hard to fix in the source.
		prepend -Wimplicit
		prepend -Wchar-subscripts
		prepend -Wcomment
		prepend -Wformat
		prepend -Wnonnull
		prepend -Wreturn-type
		prepend -Wsequence-point
		prepend -Wstrict-aliasing
		prepend -Wtrigraphs
		prepend -Wunknown-pragmas
		prepend -Wunused-label
		#prepend -Wunused-variable	// this should be added once we've removed those that alreadt barf
		;;
	esac
	prepend -flax-vector-conversions
	prepend -funsigned-char
	prepend -fms-extensions		# unnamed struct and unions
	case $CC in
	*clang*)
		prepend -Wno-typedef-redefinition
		prepend -Wno-pointer-sign
		prepend -Wno-empty-body
		prepend -Wno-unused-value
		prepend -Wno-bitfield-constant-conversion
		prepend -fno-caret-diagnostics
		prepend -fmath-errno
	esac
	;;
*.cc|*.cpp)
	prepend -std=c++0x # alias for c++11
	prepend -fno-exceptions -fno-rtti
	prepend -Wno-invalid-offsetof
	prepend -Wreturn-type
	case $CC in
	*clang*)
		prepend -D_WCHAR_T # needed to build intel DECF librarys
	esac
	;;
*)
	error "Don't know what to do with this kind of file : $SOURCE"
	;;
esac

# cd to the directory containing the source file, to ensure that DMX does not
# unnecessarily store the source file path, and that DMX.InternalLocation is
# correct (Mantis 15678)
case $SOURCE in
*/*) cd ${SOURCE%/*}
esac
SOURCE=${SOURCE##*/} # remove path

prepend -Wno-address-of-packed-member
case $CC in
*clang*)
	prepend -Wno-extern-c-compat
	prepend -Wno-tautological-constant-out-of-range-compare
	prepend -Wno-microsoft-anon-tag
	prepend -Wno-parentheses
	prepend -Wno-switch
	;;
*) # assume GCC
	prepend -Wno-stringop-overflow
	prepend -Werror
	prepend -Wmain
	prepend -funsigned-bitfields
esac

case $MK_BITS in
32)
	# Mantis 12335: assume Pentium 4 for 32-bit builds and use SSE2 for
	# floating point arithmetic.
	prepend -march=pentium4 -mfpmath=sse
	;;
esac

prepend $CC -m$MK_BITS $OFLAGS

##### process command #####

case $OPTION in
-E)
	# -E writes to stdout
	;;
*)
	verbose $CMD $SOURCE
	;;
esac

$CMD $SOURCE
