build_cross_tools_gcc4 revision d1feb7cb
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# create the object and installation directories for the cross compilation tools
145objDir="${installDir}-build"
146binutilsObjDir="$objDir/binutils"
147gccObjDir="$objDir/gcc"
148gdbObjDir="$objDir/gdb"
149stdcxxObjDir="$objDir/stdcxx"
150sysrootDir="$installDir/sysroot"
151tmpIncludeDir="$sysrootDir/boot/system/develop/headers"
152tmpLibDir="$sysrootDir/boot/system/develop/lib"
153
154rm -rf "$installDir" "$objDir"
155
156mkdir -p "$installDir" "$objDir" "$binutilsObjDir" "$gccObjDir" "$gdbObjDir" \
157	"$stdcxxObjDir" "$tmpIncludeDir" "$tmpLibDir" || exit 1
158mkdir -p "$installDir/lib/gcc/$haikuMachine/$gccVersion"
159
160if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
161	cloogSourceDir="$buildToolsDir/cloog"
162	gmpSourceDir="$buildToolsDir/gcc/gmp"
163	islSourceDir="$buildToolsDir/isl"
164
165	islObjDir="$objDir/isl"
166	gmpObjDir="$objDir/gmp"
167	cloogObjDir="$objDir/cloog"
168	mkdir -p "$islObjDir" "$gmpObjDir" "$cloogObjDir" || exit 1
169
170	gccConfigureArgs="$gccConfigureArgs --with-cloog=$installDir \
171		--enable-cloog-backend=isl --with-isl=$installDir \
172		--with-gmp=$installDir \
173		--with-host-libstdcxx=\"-lstdc++\""
174fi
175if [ "$HAIKU_USE_GCC_PIPE" = 1 ]; then
176	ccFlags="$ccFlags -pipe"
177	cxxFlags="$cxxFlags -pipe"
178fi
179
180if [ -n "$SECONDARY_ARCH" ]; then
181	gccConfigureArgs="$gccConfigureArgs --with-hybrid-secondary=$SECONDARY_ARCH"
182fi
183
184# force the POSIX locale, as the build (makeinfo) might choke otherwise
185export LC_ALL=POSIX
186
187# build gdb
188if [ -n "$HAIKU_USE_GDB" ]; then
189cd "$gdbObjDir"
190	"$gdbSourceDir/configure" \
191		--prefix="$installDir" --target=$gdbTarget \
192		$gdbConfigureArgs \
193		|| exit 1
194	$MAKE $additionalMakeArgs || exit 1
195	$MAKE $additionalMakeArgs install || exit 1
196fi
197
198# build binutils
199cd "$binutilsObjDir"
200CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$binutilsSourceDir/configure" \
201	--prefix="$installDir" $buildHostSpec --target=$haikuMachine \
202	--enable-targets=$binutilsTargets \
203	--disable-nls --disable-shared --disable-werror \
204	--with-sysroot="$sysrootDir" --disable-initfini-array \
205	$binutilsConfigureArgs \
206	|| exit 1
207$MAKE $additionalMakeArgs || exit 1
208$MAKE $additionalMakeArgs install || exit 1
209
210export PATH=$PATH:"$installDir/bin"
211
212if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
213	# build gmp
214	cd "$gmpObjDir"
215	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gmpSourceDir/configure" \
216		--prefix="$installDir" --disable-shared --enable-cxx || exit 1
217	$MAKE $additionalMakeArgs || exit 1
218	$MAKE $additionalMakeArgs install || exit 1
219
220	# build isl
221	cd "$islObjDir"
222	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$islSourceDir/configure" \
223		--prefix="$installDir" --disable-nls --disable-shared \
224		 --with-gmp-prefix="$installDir" || exit 1
225	$MAKE $additionalMakeArgs || exit 1
226	$MAKE $additionalMakeArgs install || exit 1
227
228	# build cloog
229	cd "$cloogObjDir"
230	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$cloogSourceDir/configure" \
231		--prefix="$installDir" --disable-nls --disable-shared \
232		--with-gmp-prefix="$installDir" --with-isl-prefix=="$installDir" \
233		|| exit 1
234	$MAKE $additionalMakeArgs || exit 1
235	$MAKE $additionalMakeArgs install || exit 1
236fi
237
238# build gcc
239
240# prepare the include files
241copy_headers()
242{
243	sourceDir=$1
244	targetDir=$2
245
246	headers="$(find $sourceDir -name \*\.h)"
247	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
248	for f in $headers; do
249		headerTargetDir="$targetDir/$(dirname $f)"
250		mkdir -p "$headerTargetDir"
251		cp "$sourceDir/$f" "$headerTargetDir"
252	done
253}
254
255copy_headers "$haikuSourceDir/headers/config" "$tmpIncludeDir/config"
256copy_headers "$haikuSourceDir/headers/os" "$tmpIncludeDir/os"
257copy_headers "$haikuSourceDir/headers/posix" "$tmpIncludeDir/posix"
258
259# configure gcc
260cd "$gccObjDir"
261CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gccSourceDir/configure" \
262	--prefix="$installDir" $buildHostSpec --target=$haikuMachine \
263	--disable-nls --disable-shared --with-system-zlib \
264	--enable-languages=c,c++ --enable-lto --enable-frame-pointer \
265	--enable-__cxa-atexit --enable-threads=posix \
266	--with-default-libstdcxx-abi=gcc4-compatible \
267	--with-sysroot="$sysrootDir" \
268	$gccConfigureArgs \
269	|| exit 1
270
271# make gcc
272$MAKE $additionalMakeArgs || {
273	echo "ERROR: Building gcc failed." >&2
274	exit 1
275}
276
277# install gcc
278$MAKE $additionalMakeArgs install || {
279	echo "ERROR: Installing the cross compiler failed." >&2
280	exit 1
281}
282
283case $haikuMachine in
284x86_64-*)
285	# pick up the 32-bit libraries for the bootloader
286	bootLibgcc=`$installDir/bin/$haikuMachine-gcc -m32 -print-file-name=libgcc.a`
287	$installDir/bin/$haikuMachine-strip --strip-debug $bootLibgcc
288	bootLibsupcxx=`$installDir/bin/$haikuMachine-gcc -m32 -print-file-name=libsupc++.a`
289	$installDir/bin/$haikuMachine-strip --strip-debug $bootLibsupcxx
290	;;
291esac
292
293# cleanup
294
295# remove the system headers from the installation dir
296# Only the ones from the source tree should be used.
297rm -rf "$installDir/$haikuMachine/sys-include"
298
299# remove the sysroot dir
300rm -rf "$sysrootDir"
301
302# remove the objects dir
303rm -rf "$objDir"
304
305
306echo "binutils and gcc for cross compilation have been built successfully!"
307