SUMMARY="C/C++ cross-compiler for target ${effectiveTargetMachineTriple}"
DESCRIPTION="
Standard compiler for x86 platform.
"
HOMEPAGE="http://gcc.gnu.org"
LICENSE="
	GNU GPL v2
	GNU LGPL v2
	"
COPYRIGHT="1988-2013 Free Software Foundation, Inc."
SRC_URI="
	git+file://$portBaseDir/../binutils/download/buildtools.git#fd576c8d8a83e3fe9a6443f9c0431bd11125314e
	git+git://github.com/haiku/buildtools.git#fd576c8d8a83e3fe9a6443f9c0431bd11125314e
	"
REVISION="5"
ARCHITECTURES="x86 x86_64"
if [ $effectiveTargetArchitecture != x86_gcc2 ]; then
	# x86_gcc2 is fine as primary target architecture as long as we're building
	# for a different secondary architecture.
	ARCHITECTURES="$ARCHITECTURES x86_gcc2"
fi
SECONDARY_ARCHITECTURES="x86"

PROVIDES="
	gcc$secondaryArchSuffix = $portVersion compat >= 4
	cmd:c++$secondaryArchSuffix = $portVersion compat >= 4
	cmd:cc$secondaryArchSuffix = $portVersion compat >= 4
	cmd:cpp$secondaryArchSuffix = $portVersion compat >= 4
	cmd:g++$secondaryArchSuffix = $portVersion compat >= 4
	cmd:gcc$secondaryArchSuffix = $portVersion compat >= 4
	cmd:gcc_4.7.3$secondaryArchSuffix = $portVersion compat >= 4
	cmd:gcov$secondaryArchSuffix = $portVersion compat >= 4
	"

REQUIRES="
	haiku$secondaryArchSuffix >= $haikuVersion
	cmd:as$secondaryArchSuffix
	"
BUILD_REQUIRES="
	"
BUILD_PREREQUIRES="
	haiku${secondaryArchSuffix}_devel >= $haikuVersion
	cmd:autoconf
	cmd:flex
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:makeinfo
	cmd:sed
	cmd:strip
	cmd:tar
	"

SOURCE_DIR="gcc-$portVersion/gcc"

sourceDir=$(pwd)
relativeInstallDir="develop/tools$secondaryArchSubDir"
installDir="$prefix/$relativeInstallDir"
objectsDir=$(pwd)/../${portVersionedName}-obj

BUILD()
{
	rm -rf $objectsDir

	# Touch some files generated by bison, so that bison won't run to update
	# them. Fixes issues with newer bison versions.
	# And while at it, touch gperf target, too (as gperf may not be installed).
	(cd $sourceDir/gcc; touch c-parse.c c-parse.h cexp.c cp/parse.c \
		cp/parse.h c-gperf.h)

	mkdir -p $objectsDir
	cd $objectsDir

	local additionalConfigureFlags
	if [ -n "$secondaryArchSuffix" ]; then
		additionalConfigureFlags="\
			--with-hybrid-secondary=${effectiveTargetArchitecture}"
	fi
	if [ $effectiveTargetArchitecture == x86_64 ]; then
		# disable multilib support, as x86_64 by default tries to build the
		# 32-bit libraries, too, which fails as no 32-bit libroot is available
		additionalConfigureFlags+=" --disable-multilib"
	fi

	CFLAGS="-O2 -U_FORTIFY_SOURCE" CXXFLAGS="-O2" "$sourceDir/configure" \
		--build=$effectiveTargetMachineTriple \
		--prefix=$installDir --libexecdir=$installDir/lib --mandir=$manDir \
		--docdir=$docDir \
		--disable-nls --enable-shared --with-gnu-ld \
		--enable-languages=c,c++ --enable-lto --enable-frame-pointer \
		--with-pkgversion=$(echo $portVersion | cut -c 7-) \
		$additionalConfigureFlags

	make $jobArgs
}

INSTALL()
{
	cd $objectsDir

	make install-strip
	make install-html

	### HTML documentation ####################################

	echo "Organizing HTML documentation..."
	cd $docDir
	for dir in gmp libquadmath mpc mpfr; do
		mv ${dir}.html $dir
		ln -s $dir/index.html ${dir}.html
	done

	### Strip #################################################

	echo "Strip debug info"

	cd $installDir
	strip --strip-debug bin/*
	for f in cc1 cc1plus collect2 lto1; do
		strip --strip-debug lib/gcc/$effectiveTargetMachineTriple/*/$f
	done
	strip --strip-debug lib/*.a

	### Disable ASLR ##########################################

	echo "Add SYS:ENV attribute to disable ASLR"

	cd $installDir
	for f in bin/*; do
		if [ -r "$f" ]; then
			addattr SYS:ENV DISABLE_ASLR=1 $f
		fi
	done
	for f in cc1 cc1plus collect2 lto1; do
		addattr SYS:ENV DISABLE_ASLR=1 \
			lib/gcc/$effectiveTargetMachineTriple/*/$f
	done

	### Symlinks ##############################################

	echo "Creating required symlinks"

	# create missing cc symlink
	ln -sf gcc $installDir/bin/cc

	# make all tools available via default paths
	mkdir -p $binDir
	for f in c++ cc cpp g++ gcc gcov; do
		symlinkRelative -sfn $installDir/bin/$f $binDir
	done

	### Cleanup ###############################################

	echo "Cleanup"
	rm -rf $installDir/info
	rm -rf $installDir/share
}
