1#!/bin/sh
2
3# Parameters <haiku sourcedir> <buildtools dir> <install dir>
4# Influential environmental variable:
5# * haikuRequiredLegacyGCCVersion: The required version of the gcc. Will be
6#   checked against the version in the buildtools directory.
7
8# get and check the parameters
9if [ $# -lt 3 ]; then
10	echo Usage: $0 '<haiku sourcedir> <buildtools dir> <install dir>' >&2
11	exit 1
12fi
13
14set -e
15
16haikuSourceDir=$1
17buildToolsDir=$2/legacy
18installDir=$3
19shift 3
20additionalMakeArgs=$*
21	# Note: The gcc 2 build has trouble with -jN N > 1, hence we only use the
22	# additional flags for the binutils build. Should there ever be any other
23	# flags than -jN, we need to handle this differently.
24
25if [ `uname -s` = 'Haiku' ]; then
26	# force cross-build if building on Haiku:
27	buildhostMachine=i586-pc-haiku_buildhost
28	buildHostSpec="--build=$buildhostMachine --host=$buildhostMachine"
29fi
30
31if [ ! -d $haikuSourceDir ]; then
32	echo "ERROR: No such directory: \"$haikuSourceDir\"" >&2
33	exit 1
34fi
35
36if [ ! -d $buildToolsDir ]; then
37	echo "ERROR: No such directory: \"$buildToolsDir\"" >&2
38	exit 1
39fi
40
41# verify or extract the gcc version
42gccVersionDotC="$buildToolsDir/gcc/gcc/version.c"
43if [ -n "$haikuRequiredLegacyGCCVersion" ]; then
44	# haikuRequiredLegacyGCCVersion has been specified. Check whether the
45	# version of the gcc in the given build tools directory matches.
46	if ! grep "$haikuRequiredLegacyGCCVersion" \
47			"$gccVersionDotC" >/dev/null 2>&1 ; then
48		echo "*** Mismatching compiler versions between configure script and" \
49			>&2
50		echo "*** $gccVersionDotC" >&2
51		echo "*** Did you perhaps update only one of haiku & buildtools?" >&2
52		exit 1
53	fi
54else
55	# haikuRequiredLegacyGCCVersion wasn't specified. Extract the version string
56	# from the gcc's version.c.
57	haikuRequiredLegacyGCCVersion=`grep "2.95.3-haiku-" "$gccVersionDotC" \
58		| sed 's@.*\"\(.*\)\".*@\1@'`
59	if [ -z "$haikuRequiredLegacyGCCVersion" ]; then
60		echo "ERROR: Failed to extract version string from $gccVersionDotC" >&2
61		exit 1
62	fi
63fi
64
65
66# create the output dir
67mkdir -p $installDir || exit 1
68
69
70# get absolute paths
71currentDir=`pwd`
72
73cd $haikuSourceDir
74haikuSourceDir=`pwd`
75cd $currentDir
76
77cd $buildToolsDir
78buildToolsDir=`pwd`
79cd $currentDir
80
81cd $installDir
82installDir=`pwd`
83cd $currentDir
84
85
86# create the object and installation directories for the cross compilation tools
87objDir=${installDir}-build
88binutilsObjDir=$objDir/binutils
89gccObjDir=$objDir/gcc
90tmpIncludeDir=$objDir/sysincludes
91tmpLibDir=$objDir/syslibs
92
93rm -rf $installDir $objDir
94
95mkdir -p $installDir $objDir $binutilsObjDir $gccObjDir $tmpIncludeDir \
96	$tmpLibDir || exit 1
97mkdir -p $installDir/lib/gcc-lib/i586-pc-haiku/$haikuRequiredLegacyGCCVersion
98
99gccConfigureArgs=
100if [ -n "$SECONDARY_ARCH" ]; then
101	gccConfigureArgs="$gccConfigureArgs --with-hybrid-secondary=$SECONDARY_ARCH"
102fi
103
104# force the POSIX locale, as the build (makeinfo) might choke otherwise
105export LC_ALL=POSIX
106
107# drop an multiple job arguments from MAKEFLAGS
108if [ ! -z "$MAKEFLAGS" ]; then
109	export MAKEFLAGS=$(echo $MAKEFLAGS | sed -e 's/-j\s*[0-9][0-9]*//g')
110fi
111
112
113# build binutils
114cd $binutilsObjDir
115CFLAGS="-O2" CXXFLAGS="-O2" $buildToolsDir/binutils/configure \
116	--prefix=$installDir $buildHostSpec --target=i586-pc-haiku \
117	--disable-nls --enable-shared=yes --disable-werror || exit 1
118$MAKE $additionalMakeArgs || exit 1
119$MAKE $additionalMakeArgs install || exit 1
120
121PATH=$PATH:$installDir/bin
122export PATH
123
124
125# build gcc
126
127# prepare the include files
128copy_headers()
129{
130	sourceDir=$1
131	targetDir=$2
132
133	headers="`find $sourceDir -name \*\.h`"
134	headers="`echo $headers | sed -e s@$sourceDir/@@g`"
135	for f in $headers; do
136		headerTargetDir=$targetDir/`dirname $f`
137		mkdir -p $headerTargetDir
138		cp $sourceDir/$f $headerTargetDir
139	done
140}
141
142copy_headers $haikuSourceDir/headers/config $tmpIncludeDir/config
143copy_headers $haikuSourceDir/headers/os $tmpIncludeDir/os
144copy_headers $haikuSourceDir/headers/posix $tmpIncludeDir/posix
145
146# Touch some files generated by bison, so that bison won't run to update them.
147# Fixes issues with newer bison versions.
148# And while at it, touch gperf target, too (as gperf may not be installed)
149(cd $buildToolsDir/gcc/gcc; touch c-parse.c c-parse.h cexp.c cp/parse.c \
150	cp/parse.h c-gperf.h)
151
152# configure gcc
153cd $gccObjDir
154# GCC 2 compiled for x86_64 on most systems is broken, compile for 32-bit.
155export CC="gcc -m32"
156CFLAGS="-O2 -U_FORTIFY_SOURCE" CXXFLAGS="-O2" $buildToolsDir/gcc/configure \
157	--prefix=$installDir $buildHostSpec --target=i586-pc-haiku \
158	--disable-nls --enable-shared=yes --enable-languages=c,c++ \
159	--with-headers=$tmpIncludeDir --with-libs=$tmpLibDir $gccConfigureArgs \
160	|| exit 1
161unset CC
162
163# hack the Makefile to avoid trouble with stuff we don't need anyway
164sedExpr=
165for toRemove in libiberty libio libjava libobjc libstdc++; do
166	sedExpr="$sedExpr -e 's@^\(TARGET_CONFIGDIRS =.*\)$toRemove\(.*\)@\1\2@'"
167done
168echo sedExpr: $sedExpr
169mv Makefile Makefile.bak || exit 1
170eval "sed $sedExpr Makefile.bak > Makefile" || exit 1
171rm Makefile.bak
172
173# make gcc
174$MAKE cross || {
175	echo "ERROR: Building gcc failed." >&2
176	exit 1
177}
178
179# install gcc
180$MAKE install-gcc-cross || {
181	echo "ERROR: Installing the cross compiler failed." >&2
182	exit 1
183}
184
185# Remove the math.h gcc header. It has been generated by fixincludes
186# (unconditional hack: math_huge_val_ifndef) from ours and it is semantically
187# equivalent.
188rm -f $installDir/lib/gcc-lib/i586-pc-haiku/$haikuRequiredLegacyGCCVersion/include/math.h
189
190# Symlink the built-in C++ headers path to the sys-include directory. This is
191# not actually needed for cross compiling Haiku itself, but simplifies using the
192# cross compiler for building the bootstrap packages.
193(cd $installDir/include; ln -s ../i586-pc-haiku/sys-include/c++/2.95.3 g++)
194
195
196# cleanup
197
198# remove the system headers from the installation dir
199# Only the ones from the source tree should be used.
200rm -rf $installDir/i586-pc-haiku/sys-include
201
202# remove the objects dir
203rm -rf $objDir
204
205
206echo "binutils and gcc for cross compilation have been built successfully!"
207