SUMMARY="The GNU C/C++ compiler"
DESCRIPTION="The standard compiler for x86_gcc2 platform, ABI-compatible with \
BeOS R5."
SUMMARY_syslibs_devel="C/C++-runtime static libraries, needed to build Haiku"
DESCRIPTION_syslibs_devel="This package is not of general interest - it \
contains the set of gcc's c/c++-runtime libraries that is required by Haiku's \
build system when building Haiku."
HOMEPAGE="http://gcc.gnu.org"
COPYRIGHT="1988-2000 Free Software Foundation, Inc."
LICENSE="GNU GPL v2
	GNU LGPL v2"
REVISION="2"
srcGitRev="aa5e6148654241454155fd352641bf4b97d53750"
SOURCE_URI="https://github.com/haiku/buildtools/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="16ee29af5c70177a04699656a9f11818eb20e17dee3e342eb7571f87aea92100"
SOURCE_DIR="buildtools-$srcGitRev/legacy/gcc"
SOURCE_FILENAME="gcc-$portVersion.tar.gz"

ARCHITECTURES="x86_gcc2 !x86"
SECONDARY_ARCHITECTURES="x86_gcc2"

PROVIDES="
	gcc$secondaryArchSuffix = $portVersion compat >= 2.95.3
	cmd:cc$secondaryArchSuffix
	cmd:c++$secondaryArchSuffix = $portVersion compat >= 2.95.3
	cmd:c++filt$secondaryArchSuffix = $portVersion compat >= 2.95.3
	cmd:cpp$secondaryArchSuffix = $portVersion compat >= 2.95.3
	cmd:g++$secondaryArchSuffix = $portVersion compat >= 2.95.3
	cmd:gcc$secondaryArchSuffix = $portVersion compat >= 2.95.3
	cmd:gcov$secondaryArchSuffix = 1.5 compat >= 1.5
	cmd:i586_pc_haiku_gcc$secondaryArchSuffix = $portVersion compat >= 2.95.3
	cmd:protoize$secondaryArchSuffix = $portVersion compat >= 2.95.3
	cmd:unprotoize$secondaryArchSuffix = $portVersion compat >= 2.95.3
	"
REQUIRES="
	haiku$secondaryArchSuffix
	binutils$secondaryArchSuffix
	"

PROVIDES_syslibs_devel="
	gcc${secondaryArchSuffix}_syslibs_devel = $portVersion compat >= 2.95.3
	devel:libgcc$secondaryArchSuffix
	"
REQUIRES_syslibs_devel=""

BUILD_REQUIRES="
	"
BUILD_PREREQUIRES="
	haiku${secondaryArchSuffix}_devel
	cmd:autoconf
	gcc$secondaryArchSuffix
	cmd:find
	cmd:flex
	cmd:make
	cmd:sed
	cmd:tar
	cmd:makeinfo_4.13
	"

BUILD_PACKAGE_ACTIVATION_PHASE=INSTALL

gccDir=$(pwd)
relativeGccInstallDir="develop/tools$secondaryArchSubDir"
gccInstallDir="$prefix/$relativeGccInstallDir"
gccObjectsDir=$gccDir/../gcc-obj

BUILD()
{
	rm -rf $gccObjectsDir

	# 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 gcc; touch c-parse.c c-parse.h cexp.c cp/parse.c \
		cp/parse.h c-gperf.h)

	mkdir -p $gccObjectsDir
	cd $gccObjectsDir

	local additionalConfigureFlags
	if [ -n "$secondaryArchSuffix" ]; then
		additionalConfigureFlags="\
			--with-hybrid-secondary=${effectiveTargetArchitecture}"
	fi

	CFLAGS="-O2" CXXFLAGS="-O2" "$gccDir/configure" \
		--prefix=$gccInstallDir \
		--disable-nls --enable-shared --enable-languages=c,c++ \
		$additionalConfigureFlags
	make bootstrap
}

INSTALL()
{
	cd $gccObjectsDir
	make install

	rm $gccInstallDir/lib/libiberty.a
		# only needed for building gcc

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

	local html_base=$docDir
	if [ ! -d "$html_base" ]; then
		echo "Building HTML documentation..."
		mkdir -p $html_base
		cd $html_base

		makeinfo-4.13 --html "$gccDir/gcc/cpp.texi"
		makeinfo-4.13 --html "$gccDir/gcc/gcc.texi"
		makeinfo-4.13 --force --html "$gccDir/libio/iostream.texi" \
			&& true
				# some errors

		ln -sf cpp/index.html $html_base/cpp.html
		ln -sf gcc/index.html $html_base/gcc.html
		ln -sf iostream/index.html $html_base/iostream.html
	fi

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

	echo "Creating required symlinks"

	# convert to absolute links to relative ones
	cd $gccInstallDir/bin
	ln -sfn g++ c++
	ln -sfn gcc cc
	ln -sfn gcc $effectiveTargetMachineTriple-gcc

	# make all tools available via default paths
	echo "Symlinking binaries into default path"
	mkdir -p $binDir
	symlinkRelative -s $gccInstallDir/bin/* $binDir

	### Libraries #############################################

	echo "Moving libraries around"

	# Move/copy libraries such that copies of static libraries (libgcc.a)
	# exist in $developLibDir. These are put into a separate package
	# (gcc_syslibs_devel), which is used by Haiku's build system.
	mkdir -p $libDir
	mkdir -p $developLibDir
	cd $gccInstallDir
	gccLibDir=lib/gcc-lib/$effectiveTargetMachineTriple/2.95.3-*

	# libgcc
	cp $gccLibDir/libgcc.a $developLibDir/

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

	echo "Strip debug info"

	cd $gccInstallDir
	strip --strip-debug bin/*
	strip --strip-debug $gccLibDir/* &>/dev/null || true

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

	echo "Cleanup"

	cd $gccInstallDir
	rm -rf info
	rm -rf share
	rm man/man1/cccp.1

	### C++ includes ##########################################

	echo "Install C++ includes & library"

	rm -rf $gccInstallDir/include/g++
	ln -snf /boot/system/develop/headers/c++/2.95.3 $gccInstallDir/include/g++

	ln -snf /boot/system/lib$secondaryArchSubDir/libstdc++.r4.so $gccInstallDir/lib/

	### Sub Packages ##########################################

	packageEntries "syslibs_devel" \
		$relativeDevelopLibDir/libgcc.a

	rm -rf $libDir
	rm -rf $developLibDir
}
