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