#!/bin/bash
#
# See <https://wiki.bramley.dyalog.com/index.php/Mk_cc> for documentation.


# exit on all errors encountered
set -e

# configure the environment, if not already done. This needs to be done before
# we call any mk_build_fns functions, so might as well do it straight away.
if [[ -z $MK_HAVE_CONFIGURED ]]; then
	. "$DEVT"/svn/apl/allos/bin/configure
fi

# source common shell functions
. "$MK_ALLBINDIR"/mk_build_fns

if verbosity_at_least 3; then
	set -x
fi

# parse arguments and setup (see allos/bin/mk_build_fns for all the paramaters
# that will have been set).
parse_cc_cl "$@"



##### set the compiler #####
case $MK_OPT in
fuzz)
	export AFL_QUIET=1
	set_compiler afl-clang-fast afl-clang-fast++ ;;
*)
	set_compiler gcc g++ ;;
esac

set_c_std   -std=c11
set_cxx_std -std=c++17



##### generic C_FLAGS #####
add_flag -g3 -fno-strict-aliasing -ffp-contract=off
add_flag -DxxBIT="$MK_BITS"
add_flag -DHAS_UNICODE="$MK_UNICODE"
add_flag -DMK_PORT="$MK_PORT"
add_flag -DMK_OPT="$MK_OPT"
add_flag -DUNIX=1
add_flag -DRUNTIME="$MK_RUNTIME"
add_flag -D__STDC_WANT_DEC_FP__=0
add_flag -fvisibility=hidden
add_flag -DDYALOGRIDELIB="$MK_CONGADLLNAME"
add_flag -fPIC
add_flag -I"$OBJDIR"
add_flag ${HAVE_O_TARGET:+-I"$TARGET_DIR"}
add_flag -I"$MK_PORTDIR"/src
add_flag -I"$MK_SRCDIR"
add_flag -Wno-address-of-packed-member

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

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

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



##### compiler or C/C++ or port-specific C_FLAGS #####

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

case $MK_OPT in
cov)
	case $COMPILER in
	*clang*) add_flag -O0 -fprofile-instr-generate -fcoverage-mapping ;;
	*)       add_flag --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.
	add_flag -DFUZZING ;;
esac

case $SOURCE_TYPE in
c)
	add_flag -Wimplicit            # implicit declarations
	add_flag -Wcomment             # check for trailing backslash on comments
	add_flag -flax-vector-conversions
	add_flag -funsigned-char
	add_flag -fms-extensions       # unnamed struct and unions

	case $MK_AP_BUILD in
	1) ;;
	*)
		add_flag -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.
		add_flag -Wchar-subscripts
		add_flag -Wformat
		add_flag -Wnonnull
		add_flag -Wreturn-type
		add_flag -Wsequence-point
		add_flag -Wstrict-aliasing
		add_flag -Wtrigraphs
		add_flag -Wunknown-pragmas
		add_flag -Wunused-label
		#add_flag -Wunused-variable	// this should be added once we've removed those that alreadt barf
		;;
	esac

	case $COMPILER in
	*clang*)
		add_flag -Wno-typedef-redefinition
		add_flag -Wno-pointer-sign
		add_flag -Wno-empty-body
		add_flag -Wno-unused-value
		add_flag -Wno-bitfield-constant-conversion
		add_flag -fno-caret-diagnostics
		add_flag -fmath-errno
		;;
	esac
	;;
cpp)
	add_flag -fno-exceptions -fno-rtti
	add_flag -Wno-invalid-offsetof
	add_flag -Wreturn-type

	case $COMPILER in
	*clang*) add_flag -D_WCHAR_T ;; # needed to build intel DECF librarys
	esac
	;;
esac

add_flag $(mk_runtime_checks)

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

case $MK_AP_BUILD in
1)
	add_flag -DAP_BUILD=1
	add_flag -fpic		# for AP shared libraries
	add_flag -I"$MK_XFSRC"
	;;
*)
	add_flag -I"$MK_PCRE2_OBJ"
	add_flag -I"$MK_LZ4_SRC"
	add_flag -I"$MK_ZLIB_SRC"
	add_flag -I"$MK_JPH_SRC"
	add_flag -I"$MK_UTF8PROC_SRC"
	add_flag -I"$MK_HTTPINTERCEPTOR_ROOT"/HTTPIntreceptorLibrary/HTTPInterceptor
	add_flag -I"$MK_HTTPINTERCEPTOR_ROOT"/AplWrapper/AplWrapper
	add_flag -DNO_HTTP_GUI
	;;
esac

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

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



##### file-specific C_FLAGS #####

case $SRC_FILE in
	#sse2.cpp)       add_flag -msse2 ;;
	#ssse3.cpp)      add_flag -mssse3 ;;
	#sse41.cpp)      add_flag -msse4.1 ;;
	bmi2.cpp)        add_flag_if_recognized -mbmi2 -mpopcnt ;;
	# Some gcc versions generate bad code for unaligned accesses with
	# generic -march
	#arith_avx2.cpp) add_flag -mavx2 -march=core-avx2 ;;
esac



##### mode-specific flags #####

case $MODE in
	preproc_file)   add_persistent_flag -E -o "$TARGET" ;;
	preproc_stdout) add_persistent_flag -E ${HAVE_O_TARGET:+-o "$TARGET"} ;;
	assemble)       add_persistent_flag -S -o "$TARGET" ;;
	compile)        add_persistent_flag -MMD -MF "$DEPEND" -MP -c -o "$TARGET";;
	link)           add_persistent_flag -o "$TARGET" ;;
esac



##### execution #####

# build compiler_command and other preparation before execution
setup_exec_command

# execute command (no other MODE-specific operation needs to be done)
"${compiler_command[@]}"
