SUMMARY="C/C++ cross-compiler for ARM"
DESCRIPTION="
Compiler for ARM processors.
"
HOMEPAGE="http://gcc.gnu.org"

SOURCE_URI="http://ftpmirror.gnu.org/gcc/gcc-6.4.0/gcc-6.4.0.tar.xz"
CHECKSUM_SHA256="850bf21eafdfe5cd5f6827148184c08c4a0852a37ccf36ce69855334d2c914d4"
SOURCE_DIR="gcc-$portVersion"
REVISION="1"
LICENSE="
	GNU GPL v2
	GNU LGPL v2
	"
COPYRIGHT="1988-2017 Free Software Foundation, Inc."


ARCHITECTURES="?all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

targetArch="arm-none-eabi"
targetU="arm_none_eabi"

PROVIDES="
	${targetU}_gcc_nolibc$secondaryArchSuffix = $portVersion compat >= 4
	cmd:${targetU}_c++$secondaryArchSuffix = $portVersion compat >= 4
	cmd:${targetU}_cc$secondaryArchSuffix = $portVersion compat >= 4
	cmd:${targetU}_cpp$secondaryArchSuffix = $portVersion compat >= 4
	cmd:${targetU}_g++$secondaryArchSuffix = $portVersion compat >= 4
	cmd:${targetU}_gcc$secondaryArchSuffix = $portVersion compat >= 4
	cmd:${targetU}_gcc_4.8.2$secondaryArchSuffix = $portVersion compat >= 4
	cmd:${targetU}_gcov$secondaryArchSuffix = $portVersion compat >= 4
	lib:${targetU}_libstdc++$secondaryArchSuffix = $portVersion compat >= 4
	"

REQUIRES="
	haiku$secondaryArchSuffix
	cmd:${targetU}_as$secondaryArchSuffix
	lib:libgmp$secondaryArchSuffix
	lib:libmpfr$secondaryArchSuffix
	lib:libmpc$secondaryArchSuffix
	"
BUILD_REQUIRES="
	cmd:${targetU}_as$secondaryArchSuffix
	devel:libgmp$secondaryArchSuffix
	devel:libmpfr$secondaryArchSuffix
	devel:libmpc$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	haiku${secondaryArchSuffix}_devel
	cmd:autoconf
	cmd:awk
	cmd:bison
	cmd:find
	cmd:flex
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:makeinfo
	cmd:sed
	cmd:strip
	cmd:tar
	cmd:xargs
	"

sourceDir=$(pwd)
relativeInstallDir="develop/tools/${targetArch}"
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

	# Setting a quoted string in an environment variable is ugly (the quotes
	# are removed at various stages and need an absurd amount of escaping), so
	# let's use a file and --include instead.
	echo '#define LIBRARY_PATH_ENV "ARM_LIBRARY_PATH"' > haiku_host.h

	CXXFLAGS="-O2 -include $objectsDir/haiku_host.h" \
	CFLAGS="-O2 -U_FORTIFY_SOURCE" "$sourceDir/configure" \
		--build=$effectiveTargetMachineTriple \
		--prefix=$installDir --libexecdir=$installDir/lib --mandir=$manDir \
		--docdir=$docDir --disable-nls --disable-libssp --with-gnu-ld \
		--enable-languages=c,c++ --enable-lto --enable-frame-pointer \
		--enable-multilib --disable-shared \
		--without-headers --with-newlib \
		--with-sysroot=$installDir \
		--with-native-system-header-dir=/include \
		--with-pkgversion=$(echo $portVersion | cut -c 7-) --target=$targetArch

	make $jobArgs all-gcc all-target-libgcc
}

INSTALL()
{
	cd $objectsDir

	make install-gcc install-target-libgcc

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

	echo "Creating required symlinks"

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

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

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