# --- T2-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by scripts/Create-CopyPatch.
# 
# T2 SDE: package/.../gcc/parse-config
# Copyright (C) 2004 - 2020 The T2 SDE Project
# Copyright (C) 1998 - 2003 ROCK Linux Project
# 
# More information can be found in the files COPYING and README.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- T2-COPYRIGHT-NOTE-END ---

# Use the stack protector patch, if not disabled for the pkg
#
if [ "${SDECFG_DEFAULT_CC:0:3}" = "gcc" -a "$SDECFG_PKG_GCC_STACKPRO" = 1 ]; then
	if ! hasflag NO-SSP; then
		var_append GCC_WRAPPER_INSERT ' ' '-fstack-protector'
	else
		echo_warning 'Package incompatible with GCC stack-protector, disabled.'
	fi
fi

# It's usually better to not change the kernel optimization too drastically.
#
if [[ $SDECFG_DEFAULT_KCC = gcc* ]] ; then
	export KCC="${archprefix}kcc"
	var_insert KCC_WRAPPER_REMOVE " " "-O -O[0-9sgz]"
fi

# set CC, CXX and F77 environment variables
#
for x in CC CXX F77 ; do
	eval "y=\$SDECFG_DEFAULT_$x"
	if [[ $y = gcc* ]] ; then
		case "$x-$stagelevel" in
		    CC-*)
			z="${archprefix}gcc ${archprefix}gcc" ;;
		    CXX*)
			z="${archprefix}g++ ${archprefix}g++" ;;
		    F77*)
			z="${archprefix}g77 ${archprefix}gfortran" ;;
		    *)
			export $x=${x}_not_available_in_stage_${stagelevel}
			continue ;;
		esac
		for y in $z ; do
			[ "`type -p $y`" ] && export $x=$y
		done
	fi
done

# Add the usual gcc optimazation options
# Strip or add debug information
#
if [ "$SDECFG_DEBUG" = 0 ] ; then
	var_append GCC_WRAPPER_APPEND " " "-s"
	var_insert GCC_WRAPPER_REMOVE " " "-g*"
	var_append SYSGCC_WRAPPER_APPEND " " "-s"
	var_insert SYSGCC_WRAPPER_REMOVE " " "-g*"
else
	var_append GCC_WRAPPER_APPEND " " "-ggdb"
	var_insert GCC_WRAPPER_REMOVE " " "-s -g*"
	var_append SYSGCC_WRAPPER_APPEND " " "-ggdb"
	var_insert SYSGCC_WRAPPER_REMOVE " " "-s -g*"
fi

# avoid undefined references to __sync_fetch_and_add_4
[[ "$HOSTTYPE" = i?86 ]] && var_append SYSGCC_WRAPPER_APPEND ' ' "-march=$HOSTTYPE"

# Remove the optimization options, since we pass generic ones ... In ROCK
# history this has been -m*, but this caused -msse and -maltivec style options
# to get lost. So we better explicitly only remove some -m options ... -ReneR
var_insert GCC_WRAPPER_REMOVE " " "-O -O[0-9sgz] -mtune* -march* -mcpu* -Werror -Wextra"
var_insert KCC_WRAPPER_REMOVE " " "-Werror"
var_insert SYSGCC_WRAPPER_REMOVE " " "-O -O[0-9sgz] -mtune* -march* -mcpu* -Werror"

case "$SDECFG_OPT" in
	smart*)
		f="$confdir/$pkg.prof"
		# hack mostly for glibc32, could me made more generic
		[ -f "$f" ] || f="$confdir/../${pkg%%[0-9]*}/${pkg%%[0-9]*}.prof"
		# TODO: or fix awk script for non existing file?
		[ -f "$f" ] || f="/dev/null"
		[ "$SDECFG_OPT" = "smarter" ] && opt="O3" || opt="O2"
		var_insert GCC_WRAPPER_INSERT " " "-SPEED-$opt -SIZE-Os"
		var_insert GCC_WRAPPER_FILTER "|" "gawk -f $base/misc/tools-source/smart_wrapper.awk $f -"
		var_insert KCC_WRAPPER_INSERT " " "-SPEED-$opt -SIZE-Os"
		var_insert KCC_WRAPPER_FILTER "|" "gawk -f $base/misc/tools-source/smart_wrapper.awk $f -"
		var_insert SYSGCC_WRAPPER_INSERT " " "-SPEED-$opt -SIZE-Os"
		var_insert SYSGCC_WRAPPER_FILTER "|" "gawk -f $base/misc/tools-source/smart_wrapper.awk $f -"
		;;
	speed)
		var_insert GCC_WRAPPER_INSERT " " "-O2"
		var_insert KCC_WRAPPER_INSERT " " "-O2"
		var_insert SYSGCC_WRAPPER_INSERT " " "-O2"
		;;
	speed-vect)
		var_insert GCC_WRAPPER_INSERT " " "-O2 -ftree-vectorize"
		var_insert KCC_WRAPPER_INSERT " " "-O2"
		var_insert SYSGCC_WRAPPER_INSERT " " "-O2 -ftree-vectorize"
		;;
	fast)
		var_insert GCC_WRAPPER_INSERT " " "-O3"
		var_insert KCC_WRAPPER_INSERT " " "-O3"
		var_insert SYSGCC_WRAPPER_INSERT " " "-O3"
		;;
	size)
		var_insert GCC_WRAPPER_INSERT " " "-Os"
		var_insert KCC_WRAPPER_INSERT " " "-Os"
		var_insert SYSGCC_WRAPPER_INSERT " " "-Os"
		;;
	size-vect)
		var_insert GCC_WRAPPER_INSERT " " "-Os -ftree-vectorize"
		var_insert KCC_WRAPPER_INSERT " " "-Os"
		var_insert SYSGCC_WRAPPER_INSERT " " "-Os -ftree-vectorize"
		;;
	lazy)
		var_insert GCC_WRAPPER_INSERT " " "-O1"
		var_insert KCC_WRAPPER_INSERT " " "-O1"
		var_insert SYSGCC_WRAPPER_INSERT " " "-O1"
		;;
	debug)
		var_insert GCC_WRAPPER_INSERT " " "-Og"
		var_insert KCC_WRAPPER_INSERT " " "-Og"
		var_insert SYSGCC_WRAPPER_INSERT " " "-Og"
		;;
	none)
		var_insert GCC_WRAPPER_INSERT " " "-O0"
		var_insert KCC_WRAPPER_INSERT " " "-O0"
		var_insert SYSGCC_WRAPPER_INSERT " " "-O0"
		;;
esac

# gcc-4.1.0 hangs with -v --help -pipe, thus we do not add it in that case
var_insert GCC_WRAPPER_INSERT ' ' '--help?:-pipe'
var_insert SYSGCC_WRAPPER_INSERT ' ' '--help?:-pipe'

[ "$SDECFG_LD_AS_NEEDED" != 0 ] &&
	var_append GCC_WRAPPER_INSERT " " "-c?:-Wl,--as-needed"

[ "$SDECFG_PIE" != 0 ] &&
	var_append GCC_WRAPPER_INSERT " " "-c?-fPIE:-pie"

var_append GCC_WRAPPER_APPEND " " "$SDECFG_C_FLAGS"

# Compile (i.e. link) all bins statically
#
if [ "$SDECFG_STATIC" = 1 ]; then
	var_insert GCC_WRAPPER_INSERT " " "-static"
fi

# Compile with no exceptions or rtti, gcc itself must support both
#
if [ "$SDECFG_LIMITCXX" = 1 ] && [[ $pkg != gcc* ]] ; then
	var_append CXX_WRAPPER_APPEND " " "-fno-exceptions -fno-rtti"
fi

# Passing -I/usr{/local/}include creates a warning in gcc3 which is confusing
# some of the configure scripts and Makefiles.
#
var_insert GCC_WRAPPER_REMOVE " " "-I/usr/include -I/usr/local/include"

# pkg-config and other output or hardcoded configure assumption
# do not include the "DESTDIR" we need to adapt the pathes accordingly
function gcc_sysroot_filter() {
	# merge multiple path separators
	var_append GCC_WRAPPER_FILTER '|' "sed -e 's,//*,/,g'"

	# translate all non-$sysroot with trailing /lib and correct to $libdir
	var_append GCC_WRAPPER_FILTER '|' \
	"sed -e 's@^\(-[L]\|-Wl.*-rpath-link,\)*\(/\|/usr/\|/usr/[^/]*/\|/opt/[^/]*/\)\(lib\)\$@\1$root\2${libdir##*/}@'"

	# keep any other non-$sysdir /lib* as it
	var_append GCC_WRAPPER_FILTER '|' \
	"sed -e 's@^\(-[L]\|-Wl.*-rpath-link,\)*\(/\|/usr/\|/usr/[^/]*/\|/opt/[^/]*/\)\(lib\)@\1$root\2\3@'"

	# and tranlate includes, too, and yes, we need "lib" for for lib/glib/include/glibconfig.h
	var_append GCC_WRAPPER_FILTER '|' \
	"sed -e 's@^\(-[I]\)*\(/\|/usr/\|/usr/[^/]*/\|/opt/[^/]*/\)\(include\|lib\)@\1$root\2\3@'"
	var_append CPP_WRAPPER_FILTER '|' \
	"sed -e 's@^\(-[I]\)*\(/\|/usr/\|/usr/[^/]*/\|/opt/[^/]*/\)\(include\|lib\)@\1$root\2\3@'"
}
hook_add prepare 5 gcc_sysroot_filter

# Add the architecture gcc optimisation options
#
if [ -f architecture/$arch/gcc-options.in ] ; then
	. architecture/$arch/gcc-options.in
fi

# Inject -fPIC if the package is marked to need a quirk ...
#
hasflag FPIC-QUIRK && var_append GCC_WRAPPER_APPEND ' ' '-fPIC'

# Build with LTO, if not disabled for this package
#
if [ "$SDECFG_LTO" == 1 ]; then
	! hasflag NO-LTO &&
		var_append GCC_WRAPPER_INSERT " " "conftest.*?:-flto=${SDECFG_PARALLEL_MAX:-auto}" ||
		echo_warning 'Not using LTO, disabled for this package.'
fi

# Remove -W,l,--as-needed, some packages throw up with it...
#
if hasflag NO-AS-NEEDED; then
	var_remove_regex GCC_WRAPPER_INSERT ' ' ".*-Wl,--as-needed"
	var_append GCC_WRAPPER_REMOVE ' ' "-Wl,--as-needed"
fi
