build_cross_tools_gcc4 revision 49f8422a
1#!/bin/sh
2#
3# parameters <machine> <haiku sourcedir> <buildtools dir> <haiku output dir>
4
5# get and check the parameters
6if [ $# -lt 4 ]; then
7	echo Usage: $0 '<machine> <haiku sourcedir> <buildtools dir>' \
8		'<haiku output dir>' >&2
9	exit 1
10fi
11
12haikuMachine=$1
13haikuSourceDir=$2
14buildToolsDir=$3
15haikuOutputDir=$4
16shift 4
17additionalMakeArgs=$*
18
19case `uname` in
20FreeBSD|OpenBSD)
21	MAKE=gmake
22	;;
23*)
24	MAKE=make
25	;;
26esac
27export MAKE
28
29case $haikuMachine in
30x86_64-*)
31	# GCC's default is to enable multilib, but there is a bug when
32	# explicitly using --enable-multilib that causes a build
33	# failure
34	binutilsConfigureArgs=""
35	gccConfigureArgs=""
36	kernelCcFlags="-mno-red-zone"
37	;;
38m68k-*)
39	binutilsConfigureArgs="--enable-multilib"
40	gccConfigureArgs="--enable-multilib"
41	;;
42arm-*)
43	binutilsConfigureArgs="--enable-multilib"
44	gccConfigureArgs="--enable-multilib"
45	;;
46*)
47	binutilsConfigureArgs="--disable-multilib"
48	gccConfigureArgs="--disable-multilib"
49	kernelCcFlags=
50	;;
51esac
52
53if [ `uname -o` = 'Haiku' ]; then
54	# force cross-build if building on Haiku:
55	buildhostMachine=${haikuMachine}_buildhost
56	buildHostSpec="--build=$buildhostMachine --host=$buildhostMachine"
57fi
58
59if [ ! -d $haikuSourceDir ]; then
60	echo "No such directory: \"$haikuSourceDir\"" >&2
61	exit 1
62fi
63
64if [ ! -d $buildToolsDir ]; then
65	echo "No such directory: \"$buildToolsDir\"" >&2
66	exit 1
67fi
68
69
70# create the output dir
71mkdir -p $haikuOutputDir || exit 1
72
73
74# get absolute paths
75currentDir=$(pwd)
76
77cd $haikuSourceDir
78haikuSourceDir=$(pwd)
79cd $currentDir
80
81cd $buildToolsDir
82buildToolsDir=$(pwd)
83cd $currentDir
84
85cd $haikuOutputDir
86haikuOutputDir=$(pwd)
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
107installDir=$haikuOutputDir/cross-tools
108objDir=$haikuOutputDir/cross-tools-build
109binutilsObjDir=$objDir/binutils
110gccObjDir=$objDir/gcc
111stdcxxObjDir=$objDir/stdcxx
112sysrootDir=$objDir/sysroot
113tmpIncludeDir=$sysrootDir/boot/system/develop/headers
114tmpLibDir=$sysrootDir/boot/system/develop/lib
115
116rm -rf $installDir $objDir
117
118mkdir -p $installDir $objDir $binutilsObjDir $gccObjDir $stdcxxObjDir \
119	$tmpIncludeDir $tmpLibDir || exit 1
120mkdir -p $installDir/lib/gcc/$haikuMachine/$gccVersion
121
122if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
123	cloogSourceDir=$buildToolsDir/cloog
124	gmpSourceDir=$buildToolsDir/gcc/gmp
125	pplSourceDir=$buildToolsDir/ppl
126
127	pplObjDir=$objDir/ppl
128	gmpObjDir=$objDir/gmp
129	cloogObjDir=$objDir/cloog
130	mkdir -p $pplObjDir $gmpObjDir $cloogObjDir || exit 1
131	
132	gccConfigureArgs="$gccConfigureArgs --with-cloog=$installDir \
133		--enable-cloog-backend=isl --with-ppl=$installDir \
134		--disable-cloog-version-check --with-gmp=$installDir \
135		--with-host-libstdcxx=\"-lstdc++\""
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="-O2" CXXFLAGS="-O2" $binutilsSourceDir/configure \
144	--prefix=$installDir $buildHostSpec --target=$haikuMachine \
145	--disable-nls --disable-shared --disable-werror \
146	--with-sysroot=$sysrootDir \
147	$binutilsConfigureArgs \
148	|| exit 1
149$MAKE $additionalMakeArgs || exit 1
150$MAKE $additionalMakeArgs install || exit 1
151
152export PATH=$PATH:$installDir/bin
153
154if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
155	# build gmp
156	cd $gmpObjDir
157	$gmpSourceDir/configure --prefix=$installDir \
158		--disable-shared --enable-cxx || exit 1
159	$MAKE $additionalMakeArgs || exit 1
160	$MAKE $additionalMakeArgs install || exit 1
161	
162	# build ppl
163	cd $pplObjDir
164	CFLAGS="-O2" CXXFLAGS="-O2" $pplSourceDir/configure --prefix=$installDir \
165		--disable-nls --disable-shared --disable-watchdog \
166		--disable-maintainer-mode || exit 1
167	$MAKE $additionalMakeArgs AUTOCONF:=true AUTOHEADER:=true ACLOCAL:=true \
168		AUTOMAKE:=true || exit 1
169	$MAKE $additionalMakeArgs install AUTOCONF:=true AUTOHEADER:=true \
170		ACLOCAL:=true AUTOMAKE:=true || exit 1
171
172	# build cloog
173	cd $cloogObjDir
174	CFLAGS="-O2" CXXFLAGS="-O2" $cloogSourceDir/configure \
175		--prefix=$installDir --disable-nls --disable-shared \
176		--with-gmp-prefix=$installDir || 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="-O2" CXXFLAGS="-O2" $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 \
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	$MAKE -C $haikuMachine/libgcc clean
227	$MAKE -C $haikuMachine/libgcc CFLAGS="-g -O2 $kernelCcFlags" || {
228		echo "Error: Building kernel libgcc failed." >&2
229		exit 1
230	}
231
232	cp $haikuMachine/libgcc/libgcc.a \
233		$installDir/$haikuMachine/lib/libgcc-kernel.a || exit 1
234
235	$MAKE -C $haikuMachine/libstdc++-v3/libsupc++ clean
236	$MAKE -C $haikuMachine/libstdc++-v3/libsupc++ CFLAGS="-g -O2 $kernelCcFlags" \
237		CXXFLAGS="-g -O2 $kernelCcFlags" || {
238		echo "Error: Building kernel libsupc++ failed." >&2
239		exit 1
240	}
241
242	cp $haikuMachine/libstdc++-v3/libsupc++/.libs/libsupc++.a \
243		$installDir/$haikuMachine/lib/libsupc++-kernel.a || exit 1
244fi
245
246# cleanup
247
248# remove the system headers from the installation dir
249# Only the ones from the source tree should be used.
250rm -rf $installDir/$haikuMachine/sys-include
251
252# remove the objects dir
253rm -rf $objDir
254
255
256echo "binutils and gcc for cross compilation have been built successfully!"
257