#!/bin/sh# Parameters <haiku sourcedir> <buildtools dir> <install 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 parametersif [ $# -lt 3 ]; thenecho Usage: $0 '<haiku sourcedir> <buildtools dir> <install dir>' \'[extra make flags]' >&2exit 1fiset -eif [ -z "$MAKE" ]; thenecho "MAKE undefined. Assuming make."export MAKE=makefihaikuSourceDir=$1buildToolsDir=$2/legacyinstallDir=$3shift 3additionalMakeArgs=$*# 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 [ `uname -s` = 'Haiku' ]; then# force cross-build if building on Haiku:buildhostMachine=i586-pc-haiku_buildhostbuildHostSpec="--build=$buildhostMachine --host=$buildhostMachine"fiif [ ! -d $haikuSourceDir ]; thenecho "ERROR: No such directory: \"$haikuSourceDir\"" >&2exit 1fiif [ ! -d $buildToolsDir ]; thenecho "ERROR: No such directory: \"$buildToolsDir\"" >&2exit 1fi# verify or extract the gcc versiongccVersionDotC="$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 ; thenecho "*** Mismatching compiler versions between configure script and" \>&2echo "*** $gccVersionDotC" >&2echo "*** Did you perhaps update only one of haiku & buildtools?" >&2exit 1fielse# 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" ]; thenecho "ERROR: Failed to extract version string from $gccVersionDotC" >&2exit 1fifi# create the output dirmkdir -p $installDir || exit 1# get absolute pathscurrentDir=`pwd`cd $haikuSourceDirhaikuSourceDir=`pwd`cd $currentDircd $buildToolsDirbuildToolsDir=`pwd`cd $currentDircd $installDirinstallDir=`pwd`cd $currentDir# create the object and installation directories for the cross compilation toolsobjDir=${installDir}-buildbinutilsObjDir=$objDir/binutilsgccObjDir=$objDir/gcctmpIncludeDir=$objDir/sysincludestmpLibDir=$objDir/syslibsrm -rf $installDir $objDirmkdir -p $installDir $objDir $binutilsObjDir $gccObjDir $tmpIncludeDir \$tmpLibDir || exit 1mkdir -p $installDir/lib/gcc-lib/i586-pc-haiku/$haikuRequiredLegacyGCCVersiongccConfigureArgs=if [ -n "$SECONDARY_ARCH" ]; thengccConfigureArgs="$gccConfigureArgs --with-hybrid-secondary=$SECONDARY_ARCH"fi# force the POSIX locale, as the build (makeinfo) might choke otherwiseexport LC_ALL=POSIX# drop an multiple job arguments from MAKEFLAGSif [ ! -z "$MAKEFLAGS" ]; thenexport MAKEFLAGS=$(echo $MAKEFLAGS | sed -e 's/-j\s*[0-9][0-9]*//g')fi# build binutilscd $binutilsObjDirCFLAGS="-O2 -fcommon" CXXFLAGS="-O2 -fcommon" $buildToolsDir/binutils/configure \--prefix=$installDir $buildHostSpec --target=i586-pc-haiku \--disable-nls --enable-shared=yes --disable-werror || exit 1$MAKE $additionalMakeArgs || exit 1$MAKE $additionalMakeArgs install || exit 1PATH=$PATH:$installDir/binexport PATH# build gcc# prepare the include filescopy_headers(){sourceDir=$1targetDir=$2headers="`find $sourceDir -name \*\.h`"headers="`echo $headers | sed -e s@$sourceDir/@@g`"for f in $headers; doheaderTargetDir=$targetDir/`dirname $f`mkdir -p $headerTargetDircp $sourceDir/$f $headerTargetDirdone}copy_headers $haikuSourceDir/headers/config $tmpIncludeDir/configcopy_headers $haikuSourceDir/headers/os $tmpIncludeDir/oscopy_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 gcccd $gccObjDir# GCC 2 compiled for x86_64 on most systems is broken, compile for 32-bit.export CC="gcc -m32"CFLAGS="-O2 -U_FORTIFY_SOURCE -fcommon" CXXFLAGS="-O2 -fcommon" $buildToolsDir/gcc/configure \--prefix=$installDir $buildHostSpec --target=i586-pc-haiku \--disable-nls --enable-shared=yes --enable-languages=c,c++ \--with-headers=$tmpIncludeDir --with-libs=$tmpLibDir $gccConfigureArgs \|| exit 1unset CC# hack the Makefile to avoid trouble with stuff we don't need anywaysedExpr=for toRemove in libiberty libio libjava libobjc libstdc++; dosedExpr="$sedExpr -e 's@^\(TARGET_CONFIGDIRS =.*\)$toRemove\(.*\)@\1\2@'"doneecho sedExpr: $sedExprmv Makefile Makefile.bak || exit 1eval "sed $sedExpr Makefile.bak > Makefile" || exit 1rm Makefile.bak# make gcc$MAKE cross || {echo "ERROR: Building gcc failed." >&2exit 1}# install gcc$MAKE install-gcc-cross || {echo "ERROR: Installing the cross compiler failed." >&2exit 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# Symlink the built-in C++ headers path to the sys-include directory. This is# not actually needed for cross compiling Haiku itself, but simplifies using the# cross compiler for building the bootstrap packages.(cd $installDir/include; ln -s ../i586-pc-haiku/sys-include/c++/2.95.3 g++)# cleanup# remove the system headers from the installation dir# Only the ones from the source tree should be used.rm -rf $installDir/i586-pc-haiku/sys-include# remove the objects dirrm -rf $objDirecho "binutils and gcc for cross compilation have been built successfully!"