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