xref: /haiku/build/scripts/build_cross_tools (revision 225b6382637a7346d5378ee45a6581b4e2616055)
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 -o` = '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
97# force the POSIX locale, as the build (makeinfo) might choke otherwise
98export LC_ALL=POSIX
99
100# drop an multiple job arguments from MAKEFLAGS
101if [ ! -z "$MAKEFLAGS" ]; then
102	export MAKEFLAGS=$(echo $MAKEFLAGS | sed -e 's/-j\s*[0-9][0-9]*//g')
103fi
104
105
106# build binutils
107cd $binutilsObjDir
108CFLAGS="-O2" CXXFLAGS="-O2" $buildToolsDir/binutils/configure \
109	--prefix=$installDir $buildHostSpec --target=i586-pc-haiku \
110	--disable-nls --enable-shared=yes --disable-werror || exit 1
111make $additionalMakeArgs || exit 1
112make $additionalMakeArgs install || exit 1
113
114PATH=$PATH:$installDir/bin
115export PATH
116
117
118# build gcc
119
120# prepare the include files
121copy_headers()
122{
123	sourceDir=$1
124	targetDir=$2
125
126	headers="`find $sourceDir -name \*\.h`"
127	headers="`echo $headers | sed -e s@$sourceDir/@@g`"
128	for f in $headers; do
129		headerTargetDir=$targetDir/`dirname $f`
130		mkdir -p $headerTargetDir
131		cp $sourceDir/$f $headerTargetDir
132	done
133}
134
135copy_headers $haikuSourceDir/headers/config $tmpIncludeDir/config
136copy_headers $haikuSourceDir/headers/os $tmpIncludeDir/os
137copy_headers $haikuSourceDir/headers/posix $tmpIncludeDir/posix
138
139# Touch some files generated by bison, so that bison won't run to update them.
140# Fixes issues with newer bison versions.
141# And while at it, touch gperf target, too (as gperf may not be installed)
142(cd $buildToolsDir/gcc/gcc; touch c-parse.c c-parse.h cexp.c cp/parse.c \
143	cp/parse.h c-gperf.h)
144
145# configure gcc
146cd $gccObjDir
147case `uname` in
148	Darwin)
149		# GCC 2 compiled for x86_64 OS X is broken, compile for i386.
150		export CC="gcc -arch i386"
151	;;
152esac
153CFLAGS="-O2 -U_FORTIFY_SOURCE" CXXFLAGS="-O2" $buildToolsDir/gcc/configure \
154	--prefix=$installDir $buildHostSpec --target=i586-pc-haiku \
155	--disable-nls --enable-shared=yes --enable-languages=c,c++ \
156	--with-headers=$tmpIncludeDir --with-libs=$tmpLibDir \
157	|| exit 1
158unset CC
159
160# hack the Makefile to avoid trouble with stuff we don't need anyway
161sedExpr=
162for toRemove in libiberty libio libjava libobjc libstdc++; do
163	sedExpr="$sedExpr -e 's@^\(TARGET_CONFIGDIRS =.*\)$toRemove\(.*\)@\1\2@'"
164done
165echo sedExpr: $sedExpr
166mv Makefile Makefile.bak || exit 1
167eval "sed $sedExpr Makefile.bak > Makefile" || exit 1
168rm Makefile.bak
169
170# make gcc
171make cross || {
172	echo "ERROR: Building gcc failed." >&2
173	exit 1
174}
175
176# install gcc
177make install-gcc-cross || {
178	echo "ERROR: Installing the cross compiler failed." >&2
179	exit 1
180}
181
182# Remove the math.h gcc header. It has been generated by fixincludes
183# (unconditional hack: math_huge_val_ifndef) from ours and it is semantically
184# equivalent.
185rm -f $installDir/lib/gcc-lib/i586-pc-haiku/$haikuRequiredLegacyGCCVersion/include/math.h
186
187# Symlink the built-in C++ headers path to the sys-include directory. This is
188# not actually needed for cross compiling Haiku itself, but simplifies using the
189# cross compiler for building the bootstrap packages.
190(cd $installDir/include; ln -s ../i586-pc-haiku/sys-include/c++/2.95.3 g++)
191
192
193# cleanup
194
195# remove the system headers from the installation dir
196# Only the ones from the source tree should be used.
197rm -rf $installDir/i586-pc-haiku/sys-include
198
199# remove the objects dir
200rm -rf $objDir
201
202
203echo "binutils and gcc for cross compilation have been built successfully!"
204