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