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