# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../gcc/gcc.conf
# Copyright (C) 2006 - 2014 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE 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.
# --- SDE-COPYRIGHT-NOTE-END ---

#exclude internal libffi, we have it packaged for g-wrap
hook_add preconf 1 'sed -i -e "s/@toolexeclibdir@/@toolexeclibdir@\/gcc/" libffi/Makefile.in'

# comparable version
gcc_ver=0
for x in $(echo "$ver" | cut -d- -f1 | cut -d. -f1-3 | tr '.' ' '); do
	gcc_ver=$( expr $gcc_ver \* 1000 + $x )
done

# provide same settings we use for -m{arch,cpu,tune} via configure options
# so they are default compiler settings on the final target machine
if [ "${gcc_ver}" -ge 4004000 ]; then
  for x in $GCC_WRAPPER_INSERT $GCC_WRAPPER_APPEND; do
    case "$x" in
      -mabi=*|-march=*|-mcpu=*|-mtune=*|-mfpu=*) var_append confopt " " "${x/-m/--with-}" ;;
      -mfloat-abi=*) var_append confopt " " "${x/-mfloat-abi/--with-float}" ;;
    esac
  done
fi

# create and set up cmd_wrapper for xgcc
function setup_xgcc_wrapper() {
	mkdir gcc ; pushd gcc

	cat <<-EOF > xgcc-wrapper
#!/bin/bash
# place the xgcc in the path
export PATH="$PWD:\$PATH"
[ "\$GCC_WRAPPER_DEBUG" = 1 ] && export CMD_WRAPPER_DEBUG=1
mycmd=\$1
if [ "\${mycmd%gcj}" != "\$mycmd" ]; then
  export CMD_WRAPPER_OTHERS="\$GCJ_WRAPPER_OTHERS"
  export CMD_WRAPPER_INSERT="\$GCJ_WRAPPER_INSERT"
  export CMD_WRAPPER_REMOVE="\$GCJ_WRAPPER_REMOVE"
  export CMD_WRAPPER_APPEND="\$GCJ_WRAPPER_APPEND"
  export CMD_WRAPPER_FILTER="\$GCJ_WRAPPER_FILTER"
elif [ "\${mycmd%gfortran}" != "\$mycmd" ]; then
  export CMD_WRAPPER_OTHERS="\$F95_WRAPPER_OTHERS"
  export CMD_WRAPPER_INSERT="\$F95_WRAPPER_INSERT"
  export CMD_WRAPPER_REMOVE="\$F95_WRAPPER_REMOVE"
  export CMD_WRAPPER_APPEND="\$F95_WRAPPER_APPEND"
  export CMD_WRAPPER_FILTER="\$F95_WRAPPER_FILTER"
else
  export CMD_WRAPPER_OTHERS="\$CC_WRAPPER_OTHERS:\$GCC_WRAPPER_OTHERS:\$GCC3_WRAPPER_OTHERS"
  export CMD_WRAPPER_INSERT="\$CC_WRAPPER_INSERT \$GCC_WRAPPER_INSERT \$GCC3_WRAPPER_INSERT"
  export CMD_WRAPPER_REMOVE="\$CC_WRAPPER_REMOVE \$GCC_WRAPPER_REMOVE \$GCC3_WRAPPER_REMOVE"
  export CMD_WRAPPER_APPEND="\$CC_WRAPPER_APPEND \$GCC_WRAPPER_APPEND \$GCC3_WRAPPER_APPEND"
  export CMD_WRAPPER_FILTER="\$CC_WRAPPER_FILTER|\$GCC_WRAPPER_FILTER|\$GCC3_WRAPPER_FILTER"
fi
exec cmd_wrapper "\$@"
EOF

	chmod 0755 xgcc-wrapper
	export STAGE_CC_WRAPPER=$PWD/xgcc-wrapper

	popd
}

# We must use an 'objdir' directory for building gcc.

hook_add preconf 9 "mkdir -p objdir; cd objdir"
configscript="../configure"

# See http://gcc.gnu.org/gcc-3.2/c++-abi.html
# and http://www.codesourcery.com/cxx-abi/.
if [ "$SDECFG_CROSSBUILD" = 1 ]; then
	# FIXME: stage 1 c++ packages fail if this is enabled
	var_append confopt ' ' "--disable-__cxa_atexit"
elif [ "$arch_machine" != "i386" ]; then
	var_append confopt ' ' "--enable-__cxa_atexit"
else
	var_append confopt ' ' "--disable-__cxa_atexit"
fi

# we might build a SVN or prereleases, disable checking
var_append confopt ' ' '--disable-checking'

# we build a cross compiler in stage0 and later use known good GCCs, no bstrap
var_append confopt ' ' '--disable-bootstrap'

# enable plugin support
var_append extraconfopt ' ' '--enable-plugin'

# build version specific runtime libraries (default 0)
# needed if multiple versions of gcc are installed
[ "$SDECFG_PKG_GCC_VERSION_LIBS" = 1 ] && \
	var_append confopt " " "--enable-version-specific-runtime-libs"

# build pre-compiled header (PCH) for libstdc++ (default 0)
[ "$SDECFG_PKG_GCC_CXX_PCH" = 1 ] || \
		var_append confopt ' ' "--disable-libstdcxx-pch"

# add the multi-lib config
if [ "$SDECFG_MULTILIB" = 1 ]; then
	var_append confopt ' ' "--enable-multilib"
	if [ -n "$SDECFG_MULTILIBLIST" ]; then
		var_append confopt ' ' "--with-multilib-list=$SDECFG_MULTILIBLIST"
	fi
else
	var_append confopt ' ' "--disable-multilib"
fi

if [ "${ver:0:3}" = "3.4" -o "${ver:0:1}" = "4" ]; then
	gcc_lib_dir="gcc/${arch_target}/${ver%%-*}"
else
	gcc_lib_dir="gcc-lib/${arch_target}/${ver%%-*}"
fi

# available languages:
# grep ^language= gcc/*/config-lang.in | cut -d\" -f2 | sort | tr '\n' ' '
# ada c++ fortran java obj-c++ objc treelang

# only C and C++ by default, and on every stage
languages="c,c++"

# explicitely enable C99 and 'long long' support
var_append confopt ' ' "--enable-c99"
var_append confopt ' ' "--enable-long-long"

# pass --build-id option to the linker for all final links
var_append confopt ' ' "--enable-linker-build-id"

# special features when glibc is the primary libc for the target
if [ "$SDECFG_LIBC" = 'glibc' ]; then
	# use the gnu_unique_object relocation for C++ template static data
	# members and inline function local statics
	var_append confopt ' ' "--enable-gnu-unique-object"

	# pass --hash-style=gnu option to the linker for all final links
	var_append confopt ' ' "--with-linker-hash-style=gnu"
fi


if atstage toolchain; then
	bindir="/$prefix/crosscc"

	var_append confopt " " "--with-sysroot=$sysroot"

	var_append confopt " " "--program-prefix=${arch_target}-"
	var_append confopt " " "--disable-cpp"
	var_append confopt " " "--disable-shared"
	var_append confopt " " "--disable-libssp"
	var_append confopt " " "--disable-libgomp"
	var_append confopt " " "--disable-libquadmath"
	var_append confopt " " "--disable-libitm"

	# use the native libraries we build in the toolchain directory
	var_append extraconfopt ' ' "--with-gmp=$( pkgprefix -r gmp )"
	var_append extraconfopt ' ' "--with-gmp-lib=$( pkgprefix -r libdir gmp )"
	var_append extraconfopt ' ' "--with-gmp-include=$( pkgprefix -r includedir gmp )"
	var_append extraconfopt ' ' "--with-mpfr=$( pkgprefix -r mpfr )"
	var_append extraconfopt ' ' "--with-mpfr-lib=$( pkgprefix -r libdir mpfr )"
	var_append extraconfopt ' ' "--with-mpfr-include=$( pkgprefix -r includedir mpfr )"
	var_append extraconfopt ' ' "--with-mpc=$( pkgprefix -r libmpc )"
	var_append extraconfopt ' ' "--with-mpc-lib=$( pkgprefix -r libdir libmpc )"
	var_append extraconfopt ' ' "--with-mpc-include=$( pkgprefix -r includedir libmpc )"

	export LDFLAGS="-Wl,-rpath,${root}${libdir}"

fi

# Graphite loop optimization
if pkginstalled -f cloog && pkginstalled -f ppl; then
	# use the cloog-isl backend
	var_append extraconfopt ' ' "--enable-cloog-backend=isl"
	var_append extraconfopt ' ' "--with-cloog=$( pkgprefix -r cloog )"
	var_append extraconfopt ' ' "--with-cloog-lib=$( pkgprefix -r libdir cloog )"
	var_append extraconfopt ' ' "--with-cloog-include=$( pkgprefix -r includedir cloog )"

	# even when using the cloog-isl backend ppl is still needed to build gcc
	var_append extraconfopt ' ' "--with-ppl=$( pkgprefix -r ppl )"
	var_append extraconfopt ' ' "--with-ppl-lib=$( pkgprefix -r libdir ppl )"
	var_append extraconfopt ' ' "--with-ppl-include=$( pkgprefix -r includedir ppl )"
fi

if atstage cross; then
	# for gcc versions 4.3 and upwards we have to explicitely specifiy the
	# target architecture at cross-compiling stage
	var_append confopt ' ' '--target=$arch_target'
fi

if ! atstage native; then
	var_append confopt " " "--disable-libmudflap"
else
	var_append confopt " " "--with-gnu-as"
	var_append confopt " " "--with-gnu-ld"
	var_append confopt " " "--enable-threads=posix"

	if [ "$SDECFG_PKG_GCC_JAVA" != 1 ] ; then
		var_append confopt " " "--disable-libgcj"
	else
		var_append confopt ' ' "--enable-libgcj"
		var_append confopt ' ' "--with-ecj-jar=$(match_source_file -p ecj)"
		var_append languages ',' 'java'
		if pkginstalled fastjar; then
			var_append confopt ' ' "--disable-fastjar"
		fi
	fi

	if [ "$SDECFG_PKG_GCC_OBJC" != 0 ] ; then
		var_append languages ',' 'objc'
	fi

	if [ "$SDECFG_PKG_GCC_F77" != 1 ] ; then
		var_append confopt " " "--disable-libgfortran"
	elif pkginstalled -f mpfr; then
		var_append languages ',' 'fortran'
	else
		echo_warning 'Fortran Compiler disabled, mpfr not found.'
		var_append confopt " " "--disable-libgfortran"
	fi

	if [ "$SDECFG_PKG_GCC_GO" != 1 ] ; then
		var_append confopt " " "--disable-libgo"
	else
		var_append languages ',' 'go'
	fi

	# Set enable-shared
	if [ "$diet_dynamic_static" == "static" ] ; then
		var_append confopt " " "--disable-shared"
	else
		var_append confopt " " "--enable-shared"
	fi
fi

var_append confopt " " "--enable-languages=$languages"

gcc_premake() {
	# create and set up the xgcc cmd_wrapper
	setup_xgcc_wrapper

	if [ $arch = powerpc ] ; then
		ln -s ppc-nocross-linux-gnu powerpc-nocross-linux-gnu
	fi

	if atstage toolchain; then
		mkdir -p $root/$prefix/$arch_target/{bin,lib}
		rm -f $root/$prefix/$arch_target/include
		if [ -f $root/$prefix/include/stdio.h ]; then
			ln -s ../include $root/$prefix/$arch_target/include
		elif [ -f $root/$prefix/diet-include/stdio.h ]; then
			ln -s ../diet-include $root/$prefix/$arch_target/include
		fi
	fi
}

gcc_postmake()
{
	# force a tools rebuild
	rm -fv $base/build/$SDECFG_ID/TOOLCHAIN/$toolsdir/.lastupdate

	# only minimal adaptions for stage0
	if atstage toolchain; then
		${arch_target}-ranlib \
			$root/$libdir/${gcc_lib_dir}/libgcc.a
		for x in gcc-${ver:0:1} kcc{,-${ver:0:1}} cc; do
			ln -svf ${arch_target}-gcc \
				$root/$prefix/crosscc/${arch_target}-$x
		done

		ln -svf ${arch_target}-gcc $root/$prefix/crosscc/${arch_target}-cc

		rm -vf "$root/$prefix/crosscc/{gcc,gcj}"

		return
	fi

	if atstage cross; then
		# This stuff needs manual installation
		#
		echo "Creating /$prefix/${arch_target} ..."
		mkdir -p $root/$prefix/${arch_target}/lib

		# FIXME! temporal hack to install libgcc_s (Ticket #100)
		if [ "$SDECFG_MULTILIB" = 1 -a "$SDECFG_SPARC64_32BIT" != 1 ]; then
			rm -rf $root$libdir/libgcc_s*
			pushd $builddir/gcc-*/objdir/gcc/
			for x in libgcc_s*.so ; do
			  cp -v $x $root$libdir/$x.1
			  ln -sfv $x.1 $root$libdir/$x
			done
			popd
		fi
	else
		# Fix the location of any improperly installed (F77) libraries
		mv -vf $root/$libdir/$gcc_lib_dir/../lib* \
		       $root/$libdir/$gcc_lib_dir || true
	fi

	# Create various symlinks for cc, c++, cpp and f77.
	#
	rm -f $root/$prefix/bin/cc
	ln -sfv gcc $root/$prefix/bin/cc
	ln -sfv ../$prefix/bin/cpp $root/lib/cpp
	ln -sfv gcc $root/$prefix/bin/kcc-${ver:0:1}
	ln -sfv gcc $root/$prefix/bin/kcc
	if [ -f $root/$prefix/bin/gfortran ] ; then
		rm -fv $root/$prefix/bin/f77
		ln -sfv gfortran $root/$prefix/bin/f77
	fi

	true
}

# Apply the respective gcc-2 or gcc-3 stack protector patch.
hook_add prepatch 3 ". $base/package/*/gcc/apply-protector.sh"
hook_add premake  5 "gcc_premake"
hook_add postmake 5 "gcc_postmake"