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

# 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 [ "${ver:0:3}" = "4.4" ]; then
  for x in $GCC_WRAPPER_INSERT $GCC_WRAPPER_APPEND; do
    case "$x" in
      -march=*|-mcpu=*|-mtune=*) var_append confopt " " "${x/-m/--with-}" ;;
    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'

# 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++"

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"

	# use the native libraries we build in the toolchain directory
	var_append extraconfopt ' ' '--with-gmp=$root'
	var_append extraconfopt ' ' '--with-mpfr=$root --with-mpfr-lib=$root/$libdir'
	var_append extraconfopt ' ' '--with-ppl=$root'
	var_append extraconfopt ' ' '--with-cloog=$root'
	export LDFLAGS="-Wl,-rpath,${root}${libdir}"
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'
	# some configure tests of gcc > 4.3 need some help for linking
	var_append GCC_WRAPPER_APPEND ' ' "-Wl,-rpath,${root}/lib"
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 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

	# 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

	# Create symlinks for runtime libraries so they can be found by
	# the dynamic loader and force rebuilding of the wrapper links
	#
	if atstage native; then
		( cd $root/$libdir ; ln -sfv ${gcc_lib_dir}/*.so.* . )

		# multilib w/ 32bit - we do just check for the output dir ...
		[ -e $root/$libdir/${gcc_lib_dir}/32 ] && (
			cd $root/$libdir/../lib/
			ln -sfv ../lib64/${gcc_lib_dir}/32/*.so.* .
		)
	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"