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