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