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