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