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