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