xref: /haiku/build/scripts/build_cross_tools (revision 6ec69f4426d9d171c683124d62cfbd7762a37f27)
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
154case `uname` in
155	Darwin)
156		# GCC 2 compiled for x86_64 OS X is broken, compile for i386.
157		export CC="gcc -arch i386"
158	;;
159esac
160CFLAGS="-O2 -U_FORTIFY_SOURCE" CXXFLAGS="-O2" $buildToolsDir/gcc/configure \
161	--prefix=$installDir $buildHostSpec --target=i586-pc-haiku \
162	--disable-nls --enable-shared=yes --enable-languages=c,c++ \
163	--with-headers=$tmpIncludeDir --with-libs=$tmpLibDir $gccConfigureArgs \
164	|| exit 1
165unset CC
166
167# hack the Makefile to avoid trouble with stuff we don't need anyway
168sedExpr=
169for toRemove in libiberty libio libjava libobjc libstdc++; do
170	sedExpr="$sedExpr -e 's@^\(TARGET_CONFIGDIRS =.*\)$toRemove\(.*\)@\1\2@'"
171done
172echo sedExpr: $sedExpr
173mv Makefile Makefile.bak || exit 1
174eval "sed $sedExpr Makefile.bak > Makefile" || exit 1
175rm Makefile.bak
176
177# make gcc
178$MAKE cross || {
179	echo "ERROR: Building gcc failed." >&2
180	exit 1
181}
182
183# install gcc
184$MAKE install-gcc-cross || {
185	echo "ERROR: Installing the cross compiler failed." >&2
186	exit 1
187}
188
189# Remove the math.h gcc header. It has been generated by fixincludes
190# (unconditional hack: math_huge_val_ifndef) from ours and it is semantically
191# equivalent.
192rm -f $installDir/lib/gcc-lib/i586-pc-haiku/$haikuRequiredLegacyGCCVersion/include/math.h
193
194# Symlink the built-in C++ headers path to the sys-include directory. This is
195# not actually needed for cross compiling Haiku itself, but simplifies using the
196# cross compiler for building the bootstrap packages.
197(cd $installDir/include; ln -s ../i586-pc-haiku/sys-include/c++/2.95.3 g++)
198
199
200# cleanup
201
202# remove the system headers from the installation dir
203# Only the ones from the source tree should be used.
204rm -rf $installDir/i586-pc-haiku/sys-include
205
206# remove the objects dir
207rm -rf $objDir
208
209
210echo "binutils and gcc for cross compilation have been built successfully!"
211