#! /bin/bash

use_sysroot=true # The linaro GCC supports --sysroot, so use it

configdir=${0%/*/*}/etc
usrbindir=${0%/*/*}/usr/bin
platform=${0%/sysroots/native/*}
cmdname=${0##*/}
tool=${cmdname##*-}
toolname=${tool//+/P}

: ${arch_compiler_prefix:=${cmdname%-*}}
: ${TARGET_SYSROOT_LOC:=$platform/sysroots/${arch_compiler_prefix}}

#
# Get architecture options from the platform manifest
#
# This should be provided in the environment, but getting these manually
# allows to use the compiler without being started by the platform,
# thus allowing to use this compiler in CDT projects for example,
# the only thing needed being to have the native sysroot in the PATH
#
arch_env=$configdir/$arch_compiler_prefix.env
manifest=$platform/ac6_platform_manifest.xml
if [ $manifest -nt $arch_env ]; then
	rm -f $arch_env
	mkdir -p $configdir
	tmpbase=/tmp/cross-gcc.$$
	cat << EOF > $tmpbase.sed
s/^.*<target_triplet>\(.*\)<\/target_triplet>.*$/compiler_prefix="\1"/p
/<property name="arch_compiler_prefix".*>$arch_compiler_prefix<\/property>/,/<\/properties>/{
s/^.*<property name="\([a-zA-Z0-9_]\+\)"[^>]*>\(.*\)<\/property>.*$/\1="\2"/p
}
EOF
	sed -n -f $tmpbase.sed $manifest > $arch_env
fi
if [ ! -r $arch_env ]; then
	echo "Not in a native sysroot... Bail out!" >&2
	exit 1
fi
. $arch_env

# Don't use arch_xxx_options, they are not valid for all tools...
tool_options="${toolname}_options"
tool_options="${!tool_options}"

sysroot=$platform/sysroots/$arch_compiler_prefix

if [[ "$tool" =~ "++" ]]; then
	cPP_includes="/usr/include/c++/${compiler_prefix} /usr/include/c++"
	cPP_libdirs=""
fi
c_includes="/usr/include/gcc/fixed /usr/include/gcc /usr/include"
c_libdirs="/usr/lib /lib /usr/lib/gcc"

default_includes="-nostdinc"
default_libdirs=
if $use_sysroot; then
	# This is useful mainly for libraries.
	sysroot_option="--sysroot=$sysroot"
fi
for include in $cPP_includes $c_includes; do
	default_includes="$default_includes -I$sysroot$include"
done
for lib in $cPP_libdirs $c_libdirs; do
	default_libdirs="$default_libdirs -L$sysroot$lib"
done

# This is crazy, but some badly-designed cross-compiler-aware systems may add /usr/include or /lib&/usr/lib to the compiler call...
declare -a options
nb_options=0
case $tool in
	gcc | g++ | c++ | as | cpp | ld | ld-*)
		# For these tools some options must be tailored to fit the target and sysroot, and conflicts
		# with values set in the global architecture options must be resolved in favor of local options
		pass_next=false
		check_next=false
		relocate_next=false
		next_is_path=false
		for option in "$@"; do
			case $option in
				-L/usr/*/lib | -L/lib/* | -L/usr/lib/* | -L/usr/*/lib/*)
					# Here we prefix these by our sysroot
					options[nb_options]="-L$sysroot${option#-L}"
					nb_options=$((nb_options + 1))
					continue
					;;
				-L/lib | -L/usr/lib)
					# Ignore standard directories, as they will be added as default directories later on
					continue
					;;
				-I/usr/include/* | -I/usr/*/include | -I/usr/*/include/*)
					# Here we prefix these by our sysroot
					options[nb_options]="-I$sysroot${option#-I}"
					nb_options=$((nb_options + 1))
					continue
					;;
				-I/usr/include)
					# Ignore standard directories, as they will be added as default directories later on
					continue
					;;
				
				# Cleanup conflicting ARCH options, first FPU-related options
				-mfloat-abi=* | -msoft-float | -mhard-float)
					# Allow the user to override the float ABI selection done by default in tool_options so that it is only a default
					opts=
					for opt in $arch_options; do
						case $opt in
							-mfloat-abi=*)
								;;
							*)
								opts="$opts $opt"
								;;
						esac
					done
					arch_options="${opts# }"
					options[nb_options]="$option"
					nb_options=$((nb_options + 1))
					continue
					;;
				-mfpu=*)
					# Allow the user to override the FPU selection done by default in tool_options so that it is only a default
					opts=
					for opt in $arch_options; do
						case $opt in
							-mfpu=*)
								;;
							*)
								opts="$opts $opt"
								;;
						esac
					done
					arch_options="${opts# }"
					options[nb_options]="$option"
					nb_options=$((nb_options + 1))
					continue
					;;
		
				# Then CPU-related options
				-mabi=*)
					# Allow the user to override the CPU selection done by default in tool_options so that it is only a default
					opts=
					for opt in $arch_options; do
						case $opt in
							-mabi=*)
								;;
							*)
								opts="$opts $opt"
								;;
						esac
					done
					arch_options="${opts# }"
					options[nb_options]="$option"
					nb_options=$((nb_options + 1))
					continue
					;;
				-mcpu=*)
					# Allow the user to override the CPU selection done by default in tool_options so that it is only a default
					opts=
					for opt in $arch_options; do
						case $opt in
							-mcpu=*)
								;;
							*)
								opts="$opts $opt"
								;;
						esac
					done
					arch_options="${opts# }"
					options[nb_options]="$option"
					nb_options=$((nb_options + 1))
					continue
					;;
				-march=*)
					# Allow the user to override the ARCH selection done by default in tool_options so that it is only a default
					opts=
					for opt in $arch_options; do
						case $opt in
							-march=*)
								;;
							*)
								opts="$opts $opt"
								;;
						esac
					done
					arch_options="${opts# }"
					options[nb_options]="$option"
					nb_options=$((nb_options + 1))
					continue
					;;
				-mtune=*)
					# Allow the user to override the ARCH selection done by default in tool_options so that it is only a default
					opts=
					for opt in $arch_options; do
						case $opt in
							-mtune=*)
								;;
							*)
								opts="$opts $opt"
								;;
						esac
					done
					arch_options="${opts# }"
					options[nb_options]="$option"
					nb_options=$((nb_options + 1))
					continue
					;;
				-m*-endian)
					# Allow the user to override the ARCH selection done by default in tool_options so that it is only a default
					opts=
					for opt in $arch_options; do
						case $opt in
							-m*-endian)
								;;
							*)
								opts="$opts $opt"
								;;
						esac
					done
					arch_options="${opts# }"
					options[nb_options]="$option"
					nb_options=$((nb_options + 1))
					continue
					;;
				-mcmodel=)
					# Allow the user to override the ARCH selection done by default in tool_options so that it is only a default
					opts=
					for opt in $arch_options; do
						case $opt in
							-m*-endian)
								;;
							*)
								opts="$opts $opt"
								;;
						esac
					done
					arch_options="${opts# }"
					options[nb_options]="$option"
					nb_options=$((nb_options + 1))
					continue
					;;
						
				# Also cleanup conflicting tool options
				-std=*)
					# Allow the user to override the language dialect selection done by default in tool_options so that it is only a default
					opts=
					for opt in $tool_options; do
						case $opt in
							-std=*)
								;;
							*)
								opts="$opts $opt"
								;;
						esac
					done
					tool_options="${opts# }"
					options[nb_options]="$option"
					nb_options=$((nb_options + 1))
					continue
					;;
			esac
		
			# Other options must be checked to detect -Wl, and -Xlinker linker-directed sequences and
			# rpath options must be relocated back to target the final rootfs, not the sysroot...
			# However as they are also used at link-time, they must be preceded by a -L in the sysroot...
			if $pass_next; then
				pass_next=false
			elif $check_next; then
				# Here I just follow a -Xlinker option
				if $next_is_path; then
					add_option="-L$option"
					option="${option#$sysroot}"
					next_is_path=false
				else
					case $option in
						-rpath)
							next_is_path=true
							prepend_point=$nb_options
							;;
						-rpath=*)
							option="${option#-rpath=}"
							add_option="-L$option"
							option="-rpath=${option#$sysroot}"
							;;
					esac
				fi
				check_next=false
			elif $relocate_next; then
				if [ X"$option" = X"-Xlinker" ]; then
					check_next=true
					next_is_path=true;
				else
					if [[ " $tool" =~ " ld" ]]; then
						add_option="-L$option"
						option="${option#$sysroot}"
					else
						option="${option#-Wl,}"
						add_option="-L$option"
						option="-Wl,${option#$sysroot}"
					fi
				fi
				relocate_next=false
			elif [[ " $tool" =~ " ld" ]]; then
				case $option in
					-rpath)
						relocate_next=true
						prepend_point=$nb_options
						;;
					-rpath=*)
						option="${option#-rpath=}"
						add_option="-L$option"
						option="-rpath=${option#$sysroot}"
						;;
				esac
			elif [ "$tool" != "cpp" -o "$tool" != "as" ]; then
				case $option in
					-Wl,-rpath,*)
						option="${option#-Wl,-rpath,}"
						add_option="-L$option"
						option="-Wl,-rpath,${option#$sysroot}"
						;;
					-Wl,-rpath=*)
						option="${option#-Wl,-rpath=}"
						add_option="-L$option"
						option="-Wl,-rpath,${option#$sysroot}"
						;;
					-Wl,-rpath)
						relocate_next=true
						prepend_point=$nb_options
						;;
					-Xlinker)
						check_next=true
						prepend_point=$nb_options
						;;
					-Wl,*)
						if $next_is_path; then
							option="${option#-Wl,}"
							add_option="-L$option"
							option="-Wl,${option#$sysroot}"
							next_is_path=false
						fi
						;;
				esac
			fi 
			if [ -n "$add_option" ]; then
				if [ -n "$prepend_point" ] && [ $prepend_point -lt $nb_options ]; then
					insert_point=$nb_options
					nb_options=$((nb_options+1))
					while [ $insert_point -gt $prepend_point ]; do
						insert_point=$(($insert_point - 1))
						options[insert_point+1]=${options[insert_point]}
					done
					options[prepend_point]="$add_option"
					prepend_point=
				else
					options[nb_options]="$add_option"
					nb_options=$((nb_options + 1))
				fi
				add_option=
			fi
			options[nb_options]="$option"
			nb_options=$((nb_options + 1))
		done
		;;
	*)
		for option in "$@"; do
			options[nb_options]="$option"
			nb_options=$((nb_options + 1))
		done
		;;
esac

case $tool in
	gcc | g++ | c++)
		exec ${usrbindir}/${compiler_prefix}-${tool} ${sysroot_option} ${arch_options} ${tool_options} "${options[@]}" ${default_includes} ${default_libdirs}
		;;
	as | cpp)
		exec ${usrbindir}/${compiler_prefix}-${tool} ${arch_options} ${tool_options} "${options[@]}" ${default_includes}
		;;
	ld | ld-*)
		exec ${usrbindir}/${compiler_prefix}-${tool} ${tool_options} "${options[@]}" ${default_libdirs}
		;;
	*)
		exec ${usrbindir}/${compiler_prefix}-${tool} ${tool_options} "${options[@]}"
		;;
esac