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