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