build_cross_tools_gcc4 revision 3d4c153e
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	;;
61riscv*-*)
62	binutilsConfigureArgs="--disable-multilib"
63	gccConfigureArgs="--disable-multilib"
64	gdbConfigureArgs="--disable-multilib"
65
66	# TODO: Disable building with TLS support for riscv until implemented.
67	binutilsConfigureArgs="$binutilsConfigureArgs --disable-tls"
68	gccConfigureArgs="$gccConfigureArgs --disable-tls"
69	;;
70powerpc-*)
71	binutilsConfigureArgs="--disable-multilib"
72	gccConfigureArgs="--disable-multilib"
73	binutilsTargets="$binutilsTargets,powerpc-apple-linux,powerpc-apple-freebsd,powerpc-apple-vxworks"
74	gdbConfigureArgs="--disable-multilib --disable-werror"
75	gdbTarget="powerpc-unknown-elf"
76
77	# TODO: Disable building with TLS support for PPC until implemented.
78	binutilsConfigureArgs="$binutilsConfigureArgs --disable-tls"
79	gccConfigureArgs="$gccConfigureArgs --disable-tls"
80	;;
81*)
82	binutilsConfigureArgs="--disable-multilib"
83	gccConfigureArgs="--disable-multilib"
84	gdbConfigureArgs="--disable-multilib"
85	;;
86esac
87
88if [ `uname -s` = 'Haiku' ]; then
89	# force cross-build if building on Haiku:
90	buildhostMachine=${haikuMachine}_buildhost
91	buildHostSpec="--build=$buildhostMachine --host=$buildhostMachine"
92fi
93
94if [ ! -d "$haikuSourceDir" ]; then
95	echo "No such directory: \"$haikuSourceDir\"" >&2
96	exit 1
97fi
98
99if [ ! -d "$buildToolsDir" ]; then
100	echo "No such directory: \"$buildToolsDir\"" >&2
101	exit 1
102fi
103
104if [ -n "$gdbSourceDir" -a ! -d "$gdbSourceDir" ]; then
105	echo "No such directory: \"$gdbSourceDir\"" >&2
106	exit 1
107fi
108
109
110# create the output dir
111mkdir -p "$installDir" || exit 1
112
113
114# get absolute paths
115currentDir=$(pwd)
116
117cd "$haikuSourceDir"
118haikuSourceDir=$(pwd)
119cd "$currentDir"
120
121cd "$buildToolsDir"
122buildToolsDir=$(pwd)
123cd "$currentDir"
124
125cd "$installDir"
126installDir=$(pwd)
127cd "$currentDir"
128
129if [ -n "$gdbSourceDir" ]; then
130cd "$gdbSourceDir"
131gdbSourceDir=$(pwd)
132cd "$currentDir"
133fi
134
135binutilsSourceDir="$buildToolsDir/binutils"
136gccSourceDir="$buildToolsDir/gcc"
137
138
139# get gcc version
140gccVersion=$(cat "$gccSourceDir/gcc/BASE-VER")
141
142if [ -z "$gccVersion" ]; then
143	echo "Failed to find out gcc version." >&2
144	exit 1
145fi
146
147# clear out the "missing" scripts so that they don't cause errors on tools
148# that are not installed, as we will have committed any files generated by
149# them to the buildtools repo already.
150echo "#!/bin/sh" >"$binutilsSourceDir"/missing
151echo "#!/bin/sh" >"$gccSourceDir"/missing
152echo "#!/bin/sh" >"$gccSourceDir"/isl/missing
153
154# create the object and installation directories for the cross compilation tools
155objDir="${installDir}-build"
156binutilsObjDir="$objDir/binutils"
157gccObjDir="$objDir/gcc"
158gdbObjDir="$objDir/gdb"
159stdcxxObjDir="$objDir/stdcxx"
160sysrootDir="$installDir/sysroot"
161tmpIncludeDir="$sysrootDir/boot/system/develop/headers"
162tmpLibDir="$sysrootDir/boot/system/develop/lib"
163
164rm -rf "$installDir" "$objDir"
165
166mkdir -p "$installDir" "$objDir" "$binutilsObjDir" "$gccObjDir" "$gdbObjDir" \
167	"$stdcxxObjDir" "$tmpIncludeDir" "$tmpLibDir" || exit 1
168mkdir -p "$installDir/lib/gcc/$haikuMachine/$gccVersion"
169
170if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
171	cloogSourceDir="$buildToolsDir/cloog"
172	gmpSourceDir="$buildToolsDir/gcc/gmp"
173	islSourceDir="$buildToolsDir/isl"
174
175	islObjDir="$objDir/isl"
176	gmpObjDir="$objDir/gmp"
177	cloogObjDir="$objDir/cloog"
178	mkdir -p "$islObjDir" "$gmpObjDir" "$cloogObjDir" || exit 1
179
180	gccConfigureArgs="$gccConfigureArgs --with-cloog=$installDir \
181		--enable-cloog-backend=isl --with-isl=$installDir \
182		--with-gmp=$installDir \
183		--with-host-libstdcxx=\"-lstdc++\""
184fi
185if [ "$HAIKU_USE_GCC_PIPE" = 1 ]; then
186	ccFlags="$ccFlags -pipe"
187	cxxFlags="$cxxFlags -pipe"
188fi
189
190if [ -n "$SECONDARY_ARCH" ]; then
191	gccConfigureArgs="$gccConfigureArgs --with-hybrid-secondary=$SECONDARY_ARCH"
192fi
193
194# force the POSIX locale, as the build (makeinfo) might choke otherwise
195export LC_ALL=POSIX
196
197# build gdb
198if [ -n "$HAIKU_USE_GDB" ]; then
199cd "$gdbObjDir"
200	"$gdbSourceDir/configure" \
201		--prefix="$installDir" --target=$gdbTarget \
202		$gdbConfigureArgs \
203		|| exit 1
204	$MAKE $additionalMakeArgs || exit 1
205	$MAKE $additionalMakeArgs install || exit 1
206fi
207
208# build binutils
209cd "$binutilsObjDir"
210CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$binutilsSourceDir/configure" \
211	--prefix="$installDir" $buildHostSpec --target=$haikuMachine \
212	--enable-targets=$binutilsTargets \
213	--disable-nls --disable-shared --disable-werror \
214	--with-sysroot="$sysrootDir" \
215	--disable-maintainer-mode \
216	$binutilsConfigureArgs \
217	|| exit 1
218$MAKE $additionalMakeArgs || exit 1
219$MAKE $additionalMakeArgs install || exit 1
220
221export PATH="$PATH:$installDir/bin"
222
223if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
224	# build gmp
225	cd "$gmpObjDir"
226	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gmpSourceDir/configure" \
227		--prefix="$installDir" --disable-shared --enable-cxx || exit 1
228	$MAKE $additionalMakeArgs || exit 1
229	$MAKE $additionalMakeArgs install || exit 1
230
231	# build isl
232	cd "$islObjDir"
233	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$islSourceDir/configure" \
234		--prefix="$installDir" --disable-nls --disable-shared \
235		 --with-gmp-prefix="$installDir" || exit 1
236	$MAKE $additionalMakeArgs || exit 1
237	$MAKE $additionalMakeArgs install || exit 1
238
239	# build cloog
240	cd "$cloogObjDir"
241	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$cloogSourceDir/configure" \
242		--prefix="$installDir" --disable-nls --disable-shared \
243		--with-gmp-prefix="$installDir" --with-isl-prefix=="$installDir" \
244		|| exit 1
245	$MAKE $additionalMakeArgs || exit 1
246	$MAKE $additionalMakeArgs install || exit 1
247fi
248
249# build gcc
250
251# prepare the include files
252copy_headers()
253{
254	sourceDir=$1
255	targetDir=$2
256
257	headers="$(find $sourceDir -name \*\.h)"
258	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
259	for f in $headers; do
260		headerTargetDir="$targetDir/$(dirname $f)"
261		mkdir -p "$headerTargetDir"
262		cp "$sourceDir/$f" "$headerTargetDir"
263	done
264}
265
266copy_headers "$haikuSourceDir/headers/config" "$tmpIncludeDir/config"
267copy_headers "$haikuSourceDir/headers/os" "$tmpIncludeDir/os"
268copy_headers "$haikuSourceDir/headers/posix" "$tmpIncludeDir/posix"
269
270# configure gcc
271cd "$gccObjDir"
272CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gccSourceDir/configure" \
273	--prefix="$installDir" $buildHostSpec --target=$haikuMachine \
274	--disable-nls --disable-shared --with-system-zlib \
275	--enable-languages=c,c++ --enable-lto --enable-frame-pointer \
276	--enable-__cxa-atexit --enable-threads=posix \
277	--with-default-libstdcxx-abi=gcc4-compatible \
278	--with-sysroot="$sysrootDir" \
279	--disable-maintainer-mode \
280	$gccConfigureArgs \
281	|| exit 1
282
283# make gcc
284$MAKE $additionalMakeArgs || {
285	echo "ERROR: Building gcc failed." >&2
286	exit 1
287}
288
289# install gcc
290$MAKE $additionalMakeArgs install || {
291	echo "ERROR: Installing the cross compiler failed." >&2
292	exit 1
293}
294
295case $haikuMachine in
296x86_64-*)
297	# pick up the 32-bit libraries for the bootloader
298	bootLibgcc=`$installDir/bin/$haikuMachine-gcc -m32 -print-file-name=libgcc.a`
299	$installDir/bin/$haikuMachine-strip --strip-debug $bootLibgcc
300	bootLibsupcxx=`$installDir/bin/$haikuMachine-gcc -m32 -print-file-name=libsupc++.a`
301	$installDir/bin/$haikuMachine-strip --strip-debug $bootLibsupcxx
302	;;
303esac
304
305# cleanup
306
307# remove the system headers from the installation dir
308# Only the ones from the source tree should be used.
309rm -rf "$installDir/$haikuMachine/sys-include"
310
311# rename the static libstdc++ to prevent accidental usage
312# (it should be used by the bootstrap process only)
313mv "$installDir/$haikuMachine/lib/libstdc++.a" \
314	"$installDir/$haikuMachine/lib/libstdc++-static.a"
315
316# remove the sysroot dir
317rm -rf "$sysrootDir"
318
319# remove the objects dir
320rm -rf "$objDir"
321
322
323echo "binutils and gcc for cross compilation have been built successfully!"
324