build_cross_tools_gcc4 revision 6d345e6c
1#!/bin/sh
2#
3# parameters <machine> <haiku sourcedir> <buildtools dir> <isntall dir>
4
5# get and check the parameters
6if [ $# -lt 4 ]; then
7	echo Usage: $0 '<machine> <haiku sourcedir> <buildtools dir>' \
8		'<install dir>' >&2
9	exit 1
10fi
11
12haikuMachine=$1
13haikuSourceDir=$2
14buildToolsDir=$3
15installDir=$4
16shift 4
17additionalMakeArgs=$*
18
19kernelCcFlags="-D_KERNEL_MODE"
20ccFlags="-O2"
21cxxFlags="-O2"
22case $haikuMachine in
23x86_64-*)
24	# GCC's default is to enable multilib, but there is a bug when
25	# explicitly using --enable-multilib that causes a build
26	# failure
27	binutilsConfigureArgs=""
28	# TODO: trying to build gcc-4.8.3 on x86_64 with --enable-tls fails with an
29	# internal compiler error, so we disable it (overriding --enable-tls used
30	# in the configure invocation below) until that problem is solved.
31	gccConfigureArgs="--disable-tls"
32	kernelCcFlags="$kernelCcFlags -mno-red-zone"
33	;;
34m68k-*)
35	binutilsConfigureArgs="--enable-multilib"
36	gccConfigureArgs="--enable-multilib"
37	;;
38arm-*)
39	# Multilib creates a lot of confusion as the wrong libs end up being linked.
40	# For now, target Cortex-A8 devices.
41	binutilsConfigureArgs="--disable-multilib --with-float=hard
42		--with-cpu=cortex-a8 --with-arch=armv7-a --with-fpu=vfpv3"
43	gccConfigureArgs="--disable-multilib --with-float=hard
44		--with-cpu=cortex-a8 --with-arch=armv7-a --with-fpu=vfpv3"
45
46	# TODO: Disable building with TLS support for ARM until implemented.
47	binutilsConfigureArgs="$binutilsConfigureArgs --disable-tls"
48	gccConfigureArgs="$gccConfigureArgs --disable-tls"
49	;;
50*)
51	binutilsConfigureArgs="--disable-multilib"
52	gccConfigureArgs="--disable-multilib"
53	;;
54esac
55
56if [ `uname -s` = 'Haiku' ]; then
57	# force cross-build if building on Haiku:
58	buildhostMachine=${haikuMachine}_buildhost
59	buildHostSpec="--build=$buildhostMachine --host=$buildhostMachine"
60fi
61
62if [ ! -d "$haikuSourceDir" ]; then
63	echo "No such directory: \"$haikuSourceDir\"" >&2
64	exit 1
65fi
66
67if [ ! -d "$buildToolsDir" ]; then
68	echo "No such directory: \"$buildToolsDir\"" >&2
69	exit 1
70fi
71
72
73# create the output dir
74mkdir -p "$installDir" || exit 1
75
76
77# get absolute paths
78currentDir=$(pwd)
79
80cd "$haikuSourceDir"
81haikuSourceDir=$(pwd)
82cd "$currentDir"
83
84cd "$buildToolsDir"
85buildToolsDir=$(pwd)
86cd "$currentDir"
87
88cd "$installDir"
89installDir=$(pwd)
90cd "$currentDir"
91
92binutilsSourceDir="$buildToolsDir/binutils"
93gccSourceDir="$buildToolsDir/gcc"
94
95
96# get gcc version
97gccVersion=$(cat "$gccSourceDir/gcc/BASE-VER")
98
99if [ -z "$gccVersion" ]; then
100	echo "Failed to find out gcc version." >&2
101	exit 1
102fi
103
104# touch all info files in order to avoid the dependency on makeinfo
105# (which apparently doesn't work reliably on all the different host
106# configurations and changes files which in turn appear as local changes
107# to the VCS).
108find "$binutilsSourceDir" "$gccSourceDir" -name \*.info -print0 | xargs -0 touch
109
110# create the object and installation directories for the cross compilation tools
111objDir="${installDir}-build"
112binutilsObjDir="$objDir/binutils"
113gccObjDir="$objDir/gcc"
114stdcxxObjDir="$objDir/stdcxx"
115sysrootDir="$installDir/sysroot"
116tmpIncludeDir="$sysrootDir/boot/system/develop/headers"
117tmpLibDir="$sysrootDir/boot/system/develop/lib"
118
119rm -rf "$installDir" "$objDir"
120
121mkdir -p "$installDir" "$objDir" "$binutilsObjDir" "$gccObjDir" "$stdcxxObjDir" \
122	"$tmpIncludeDir" "$tmpLibDir" || exit 1
123mkdir -p "$installDir/lib/gcc/$haikuMachine/$gccVersion"
124
125if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
126	cloogSourceDir="$buildToolsDir/cloog"
127	gmpSourceDir="$buildToolsDir/gcc/gmp"
128	islSourceDir="$buildToolsDir/isl"
129
130	islObjDir="$objDir/isl"
131	gmpObjDir="$objDir/gmp"
132	cloogObjDir="$objDir/cloog"
133	mkdir -p "$islObjDir" "$gmpObjDir" "$cloogObjDir" || exit 1
134
135	gccConfigureArgs="$gccConfigureArgs --with-cloog=$installDir \
136		--enable-cloog-backend=isl --with-isl=$installDir \
137		--with-gmp=$installDir \
138		--with-host-libstdcxx=\"-lstdc++\""
139fi
140if [ "$HAIKU_USE_GCC_PIPE" = 1 ]; then
141	ccFlags="$ccFlags -pipe"
142	cxxFlags="$cxxFlags -pipe"
143fi
144
145if [ -n "$SECONDARY_ARCH" ]; then
146	gccConfigureArgs="$gccConfigureArgs --with-hybrid-secondary=$SECONDARY_ARCH"
147fi
148
149# force the POSIX locale, as the build (makeinfo) might choke otherwise
150export LC_ALL=POSIX
151
152# build binutils
153cd "$binutilsObjDir"
154CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$binutilsSourceDir/configure" \
155	--prefix="$installDir" $buildHostSpec --target=$haikuMachine \
156	--enable-targets=$haikuMachine,i386-efi-pe,x86_64-efi-pe \
157	--disable-nls --disable-shared --disable-werror \
158	--with-sysroot="$sysrootDir" \
159	$binutilsConfigureArgs \
160	|| exit 1
161$MAKE $additionalMakeArgs || exit 1
162$MAKE $additionalMakeArgs install || exit 1
163
164export PATH=$PATH:"$installDir/bin"
165
166if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
167	# build gmp
168	cd "$gmpObjDir"
169	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gmpSourceDir/configure" \
170		--prefix="$installDir" --disable-shared --enable-cxx || exit 1
171	$MAKE $additionalMakeArgs || exit 1
172	$MAKE $additionalMakeArgs install || exit 1
173
174	# build isl
175	cd "$islObjDir"
176	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$islSourceDir/configure" \
177		--prefix="$installDir" --disable-nls --disable-shared \
178		 --with-gmp-prefix="$installDir" || exit 1
179	$MAKE $additionalMakeArgs || exit 1
180	$MAKE $additionalMakeArgs install || exit 1
181
182	# build cloog
183	cd "$cloogObjDir"
184	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$cloogSourceDir/configure" \
185		--prefix="$installDir" --disable-nls --disable-shared \
186		--with-gmp-prefix="$installDir" --with-isl-prefix=="$installDir" \
187		|| exit 1
188	$MAKE $additionalMakeArgs || exit 1
189	$MAKE $additionalMakeArgs install || exit 1
190fi
191
192# build gcc
193
194# prepare the include files
195copy_headers()
196{
197	sourceDir=$1
198	targetDir=$2
199
200	headers="$(find $sourceDir -name \*\.h)"
201	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
202	for f in $headers; do
203		headerTargetDir="$targetDir/$(dirname $f)"
204		mkdir -p "$headerTargetDir"
205		cp "$sourceDir/$f" "$headerTargetDir"
206	done
207}
208
209copy_headers "$haikuSourceDir/headers/config" "$tmpIncludeDir/config"
210copy_headers "$haikuSourceDir/headers/os" "$tmpIncludeDir/os"
211copy_headers "$haikuSourceDir/headers/posix" "$tmpIncludeDir/posix"
212
213# configure gcc
214cd "$gccObjDir"
215CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gccSourceDir/configure" \
216	--prefix="$installDir" $buildHostSpec --target=$haikuMachine \
217	--disable-nls --disable-shared --with-system-zlib \
218	--enable-languages=c,c++ --enable-lto --enable-frame-pointer \
219	--with-sysroot="$sysrootDir" --enable-threads=posix --enable-tls \
220	$gccConfigureArgs \
221	|| exit 1
222
223# make gcc
224$MAKE $additionalMakeArgs || {
225	echo "ERROR: Building gcc failed." >&2
226	exit 1
227}
228
229# install gcc
230$MAKE $additionalMakeArgs install || {
231	echo "ERROR: Installing the cross compiler failed." >&2
232	exit 1
233}
234
235# build libraries for the kernel if the target arch requires it
236if [ -n "$kernelCcFlags" ]; then
237	# switch threads config to single for libgcc
238	ln -sf "$gccSourceDir/libgcc/gthr-single.h" "$haikuMachine/libgcc/gthr-default.h"
239	$MAKE -C "$haikuMachine/libgcc" clean
240	$MAKE -C "$haikuMachine/libgcc" CFLAGS="-g -O2 $kernelCcFlags" || {
241		echo "Error: Building kernel libgcc failed." >&2
242		exit 1
243	}
244
245	cp "$haikuMachine/libgcc/libgcc.a" \
246		"$installDir/$haikuMachine/lib/libgcc-kernel.a" || exit 1
247
248	$MAKE -C "$haikuMachine/libstdc++-v3/libsupc++" clean
249
250	# switch threads config to single for libsupc++
251	[ -f "$haikuMachine/32/libstdc++-v3/include/$haikuMachine/bits/gthr-default.h" ] \
252		&& cp -f "$haikuMachine/32/libstdc++-v3/include/$haikuMachine/bits/gthr-single.h" \
253		"$haikuMachine/32/libstdc++-v3/include/$haikuMachine/bits/gthr-default.h"
254	cp -f "$haikuMachine/libstdc++-v3/include/$haikuMachine/bits/gthr-single.h" \
255		"$haikuMachine/libstdc++-v3/include/$haikuMachine/bits/gthr-default.h"
256	[ -f "$haikuMachine/32/libstdc++-v3/config.h" ] \
257		&& sed -i '/.*_GLIBCXX_HAS_GTHREADS.*/c\#undef _GLIBCXX_HAS_GTHREADS' \
258		"$haikuMachine/32/libstdc++-v3/config.h"
259	[ -f "$haikuMachine/32/libstdc++-v3/include/$haikuMachine/bits/c++config.h" ] \
260		&& sed -i '/.*_GLIBCXX_HAS_TLS.*/c\#undef _GLIBCXX_HAS_TLS' \
261		"$haikuMachine/32/libstdc++-v3/include/$haikuMachine/bits/c++config.h"
262	sed -i '/.*_GLIBCXX_HAS_GTHREADS.*/c\#undef _GLIBCXX_HAS_GTHREADS' \
263		"$haikuMachine/libstdc++-v3/config.h"
264	sed -i '/.*_GLIBCXX_HAVE_TLS.*/c\#undef _GLIBCXX_HAVE_TLS' \
265		"$haikuMachine/libstdc++-v3/include/$haikuMachine/bits/c++config.h"
266	$MAKE -C "$haikuMachine/libstdc++-v3/libsupc++" CFLAGS="-g -O2 $kernelCcFlags" \
267		CXXFLAGS="-g -O2 $kernelCcFlags" || {
268		echo "Error: Building kernel libsupc++ failed." >&2
269		exit 1
270	}
271
272	cp "$haikuMachine/libstdc++-v3/libsupc++/.libs/libsupc++.a" \
273		"$installDir/$haikuMachine/lib/libsupc++-kernel.a" || exit 1
274fi
275
276# cleanup
277
278# remove the system headers from the installation dir
279# Only the ones from the source tree should be used.
280rm -rf "$installDir/$haikuMachine/sys-include"
281
282# remove the sysroot dir
283rm -rf "$sysrootDir"
284
285# remove the objects dir
286rm -rf "$objDir"
287
288
289echo "binutils and gcc for cross compilation have been built successfully!"
290