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