#!/bin/sh

# Parameters <haiku sourcedir> <buildtools dir> <haiku output dir>
# Influential environmental variable:
# * haikuRequiredLegacyGCCVersion: The required version of the gcc. Will be
#   checked against the version in the buildtools directory.

# get and check the parameters
if [ $# -lt 3 ]; then
	echo Usage: $0 '<haiku sourcedir> <buildtools dir> <haiku output dir>' >&2
	exit 1
fi

haikuSourceDir=$1
buildToolsDir=$2/legacy
haikuOutputDir=$3
shift 3
additionalMakeArgs=$*
	# Note: The gcc 2 build has trouble with -jN N > 1, hence we only use the
	# additional flags for the binutils build. Should there ever be any other
	# flags than -jN, we need to handle this differently.


if [ ! -d $haikuSourceDir ]; then
	echo "ERROR: No such directory: \"$haikuSourceDir\"" >&2
	exit 1
fi

if [ ! -d $buildToolsDir ]; then
	echo "ERROR: No such directory: \"$buildToolsDir\"" >&2
	exit 1
fi

# verify or extract the gcc version
gccVersionDotC="$buildToolsDir/gcc/gcc/version.c"
if [ -n "$haikuRequiredLegacyGCCVersion" ]; then
	# haikuRequiredLegacyGCCVersion has been specified. Check whether the
	# version of the gcc in the given build tools directory matches.
	if ! grep "$haikuRequiredLegacyGCCVersion" \
			"$gccVersionDotC" >/dev/null 2>&1 ; then
		echo "*** Mismatching compiler versions between configure script and" \
			>&2
		echo "*** $gccVersionDotC" >&2
		echo "*** Did you perhaps update only one of haiku & buildtools?" >&2
		exit 1
	fi
else
	# haikuRequiredLegacyGCCVersion wasn't specified. Extract the version string
	# from the gcc's version.c.
	haikuRequiredLegacyGCCVersion=`grep "2.95.3-haiku-" "$gccVersionDotC" \
		| sed 's@.*\"\(.*\)\".*@\1@'`
	if [ -z "$haikuRequiredLegacyGCCVersion" ]; then
		echo "ERROR: Failed to extract version string from $gccVersionDotC" >&2
		exit 1
	fi
fi


# create the output dir
mkdir -p $haikuOutputDir || exit 1


# get absolute paths
currentDir=`pwd`

cd $haikuSourceDir
haikuSourceDir=`pwd`
cd $currentDir

cd $buildToolsDir
buildToolsDir=`pwd`
cd $currentDir

cd $haikuOutputDir
haikuOutputDir=`pwd`


# create the object and installation directories for the cross compilation tools
installDir=$haikuOutputDir/cross-tools
objDir=$haikuOutputDir/cross-tools-build
binutilsObjDir=$objDir/binutils
gccObjDir=$objDir/gcc
tmpIncludeDir=$objDir/sysincludes
tmpLibDir=$objDir/syslibs

rm -rf $installDir $objDir

mkdir -p $installDir $objDir $binutilsObjDir $gccObjDir $tmpIncludeDir \
	$tmpLibDir || exit 1
mkdir -p $installDir/lib/gcc-lib/i586-pc-haiku/$haikuRequiredLegacyGCCVersion

# build binutils
cd $binutilsObjDir
CFLAGS="-O2" CXXFLAGS="-O2" $buildToolsDir/binutils/configure \
	--prefix=$installDir --target=i586-pc-haiku --disable-nls \
	--enable-shared=yes --disable-werror || exit 1
make $additionalMakeArgs || exit 1
make $additionalMakeArgs install || exit 1

PATH=$PATH:$installDir/bin
export PATH


# build gcc

# prepare the include files
copy_headers()
{
	sourceDir=$1
	targetDir=$2

	headers="`find $sourceDir -name \*\.h | grep -v /.svn`"
	headers="`echo $headers | sed -e s@$sourceDir/@@g`"
	for f in $headers; do
		headerTargetDir=$targetDir/`dirname $f`
		mkdir -p $headerTargetDir
		cp $sourceDir/$f $headerTargetDir
	done
}

copy_headers $haikuSourceDir/headers/config $tmpIncludeDir/config
copy_headers $haikuSourceDir/headers/os $tmpIncludeDir/os
copy_headers $haikuSourceDir/headers/posix $tmpIncludeDir/posix

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

# configure gcc
cd $gccObjDir
case `uname` in
	Darwin)
		# GCC 2 compiled for x86_64 OS X is broken, compile for i386.
		export CC="gcc -arch i386"
	;;
esac
CFLAGS="-O2 -U_FORTIFY_SOURCE" CXXFLAGS="-O2" $buildToolsDir/gcc/configure \
	--prefix=$installDir \
	--target=i586-pc-haiku --disable-nls --enable-shared=yes \
	--enable-languages=c,c++ --with-headers=$tmpIncludeDir \
	--with-libs=$tmpLibDir || exit 1
unset CC

# hack the Makefile to avoid trouble with stuff we don't need anyway
sedExpr=
for toRemove in libiberty libio libjava libobjc libstdc++; do
	sedExpr="$sedExpr -e 's@^\(TARGET_CONFIGDIRS =.*\)$toRemove\(.*\)@\1\2@'"
done
echo sedExpr: $sedExpr
mv Makefile Makefile.bak || exit 1
eval "sed $sedExpr Makefile.bak > Makefile" || exit 1
rm Makefile.bak

# make gcc
make cross || {
	echo "ERROR: Building gcc failed." >&2
	exit 1
}

# install gcc
make install-gcc-cross || {
	echo "ERROR: Installing the cross compiler failed." >&2
	exit 1
}

# Remove the math.h gcc header. It has been generated by fixincludes
# (unconditional hack: math_huge_val_ifndef) from ours and it is semantically
# equivalent.
rm -f $installDir/lib/gcc-lib/i586-pc-haiku/$haikuRequiredLegacyGCCVersion/include/math.h


# cleanup

# remove the system headers from the installation dir
# Only the ones from the source tree should be used.
sysIncludeDir=$installDir/i586-pc-haiku/sys-include
rm -rf $sysIncludeDir/be $sysIncludeDir/posix

# remove the objects dir
rm -rf $objDir


echo "binutils and gcc for cross compilation have been built successfully!"

