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