xref: /haiku/build/scripts/build_cross_tools (revision 4466b89c65970de4c7236ac87faa2bee4589f413)
1#!/bin/sh
2
3# Parameters <haiku sourcedir> <buildtools dir> <haiku output dir>
4# Influential environmental variable:
5# * haikuRequiredLegacyGCCVersion: The requrired 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 | grep -v /.svn`"
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# configure gcc
126cd $gccObjDir
127CFLAGS="-O2 -U_FORTIFY_SOURCE" CXXFLAGS="-O2" $buildToolsDir/gcc/configure \
128	--prefix=$installDir \
129	--target=i586-pc-haiku --disable-nls --enable-shared=yes \
130	--enable-languages=c,c++ --with-headers=$tmpIncludeDir \
131	--with-libs=$tmpLibDir || exit 1
132
133# hack the Makefile to avoid trouble with stuff we don't need anyway
134sedExpr=
135for toRemove in libiberty libio libjava libobjc libstdc++; do
136	sedExpr="$sedExpr -e 's@^\(TARGET_CONFIGDIRS =.*\)$toRemove\(.*\)@\1\2@'"
137done
138echo sedExpr: $sedExpr
139mv Makefile Makefile.bak || exit 1
140eval "sed $sedExpr Makefile.bak > Makefile" || exit 1
141rm Makefile.bak
142
143# make gcc
144make cross || {
145	echo "ERROR: Building gcc failed." >&2
146	exit 1
147}
148
149# install gcc
150make install-gcc-cross || {
151	echo "ERROR: Installing the cross compiler failed." >&2
152	exit 1
153}
154
155# Remove the math.h gcc header. It has been generated by fixincludes
156# (unconditional hack: math_huge_val_ifndef) from ours and it is semantically
157# equivalent.
158rm $installDir/lib/gcc-lib/i586-pc-haiku/$haikuRequiredLegacyGCCVersion/include/math.h
159
160
161# cleanup
162
163# remove the system headers from the installation dir
164# Only the ones from the source tree should be used.
165sysIncludeDir=$installDir/i586-pc-haiku/sys-include
166rm -rf $sysIncludeDir/be $sysIncludeDir/posix
167
168# remove the objects dir
169rm -rf $objDir
170
171
172echo "binutils and gcc for cross compilation have been built successfully!"
173
174