build_cross_tools_gcc4 revision 92bb2fb3
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
112tmpIncludeDir=$objDir/sysincludes
113tmpLibDir=$objDir/syslibs
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	pplSourceDir=$buildToolsDir/ppl
125
126	pplObjDir=$objDir/ppl
127	gmpObjDir=$objDir/gmp
128	cloogObjDir=$objDir/cloog
129	mkdir -p $pplObjDir $gmpObjDir $cloogObjDir || exit 1
130	
131	gccConfigureArgs="$gccConfigureArgs --with-cloog=$installDir \
132		--enable-cloog-backend=isl --with-ppl=$installDir \
133		--disable-cloog-version-check --with-gmp=$installDir \
134		--with-host-libstdcxx=\"-lstdc++\""
135fi
136
137# force the POSIX locale, as the build (makeinfo) might choke otherwise
138export LC_ALL=POSIX
139
140# build binutils
141cd $binutilsObjDir
142CFLAGS="-O2" CXXFLAGS="-O2" $binutilsSourceDir/configure \
143	--prefix=$installDir $buildHostSpec --target=$haikuMachine \
144	--disable-nls --disable-shared --disable-werror $binutilsConfigureArgs \
145	|| exit 1
146$MAKE $additionalMakeArgs || exit 1
147$MAKE $additionalMakeArgs install || exit 1
148
149export PATH=$PATH:$installDir/bin
150
151if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
152	# build gmp
153	cd $gmpObjDir
154	$gmpSourceDir/configure --prefix=$installDir \
155		--disable-shared --enable-cxx || exit 1
156	$MAKE $additionalMakeArgs || exit 1
157	$MAKE $additionalMakeArgs install || exit 1
158	
159	# build ppl
160	cd $pplObjDir
161	CFLAGS="-O2" CXXFLAGS="-O2" $pplSourceDir/configure --prefix=$installDir \
162		--disable-nls --disable-shared --disable-watchdog \
163		--disable-maintainer-mode || exit 1
164	$MAKE $additionalMakeArgs AUTOCONF:=true AUTOHEADER:=true ACLOCAL:=true \
165		AUTOMAKE:=true || exit 1
166	$MAKE $additionalMakeArgs install AUTOCONF:=true AUTOHEADER:=true \
167		ACLOCAL:=true AUTOMAKE:=true || exit 1
168
169	# build cloog
170	cd $cloogObjDir
171	CFLAGS="-O2" CXXFLAGS="-O2" $cloogSourceDir/configure \
172		--prefix=$installDir --disable-nls --disable-shared \
173		--with-gmp-prefix=$installDir || exit 1
174	$MAKE $additionalMakeArgs || exit 1
175	$MAKE $additionalMakeArgs install || exit 1
176fi
177
178# build gcc
179
180# prepare the include files
181copy_headers()
182{
183	sourceDir=$1
184	targetDir=$2
185
186	headers="$(find $sourceDir -name \*\.h)"
187	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
188	for f in $headers; do
189		headerTargetDir=$targetDir/$(dirname $f)
190		mkdir -p $headerTargetDir
191		cp $sourceDir/$f $headerTargetDir
192	done
193}
194
195copy_headers $haikuSourceDir/headers/config $tmpIncludeDir/config
196copy_headers $haikuSourceDir/headers/os $tmpIncludeDir/os
197copy_headers $haikuSourceDir/headers/posix $tmpIncludeDir/posix
198
199# configure gcc
200cd $gccObjDir
201CFLAGS="-O2" CXXFLAGS="-O2" $gccSourceDir/configure \
202	--prefix=$installDir $buildHostSpec --target=$haikuMachine \
203	--disable-nls --disable-shared --with-system-zlib \
204	--enable-languages=c,c++ --enable-lto --enable-frame-pointer \
205	--with-headers=$tmpIncludeDir --with-libs=$tmpLibDir \
206	$gccConfigureArgs \
207	|| exit 1
208
209# make gcc
210$MAKE $additionalMakeArgs || {
211	echo "ERROR: Building gcc failed." >&2
212	exit 1
213}
214
215# install gcc
216$MAKE $additionalMakeArgs install || {
217	echo "ERROR: Installing the cross compiler failed." >&2
218	exit 1
219}
220
221# build libraries for the kernel if the target arch requires it
222if [ -n "$kernelCcFlags" ]; then
223	$MAKE -C $haikuMachine/libgcc clean
224	$MAKE -C $haikuMachine/libgcc CFLAGS="-g -O2 $kernelCcFlags" || {
225		echo "Error: Building kernel libgcc failed." >&2
226		exit 1
227	}
228
229	cp $haikuMachine/libgcc/libgcc.a \
230		$installDir/$haikuMachine/lib/libgcc-kernel.a || exit 1
231
232	$MAKE -C $haikuMachine/libstdc++-v3/libsupc++ clean
233	$MAKE -C $haikuMachine/libstdc++-v3/libsupc++ CFLAGS="-g -O2 $kernelCcFlags" \
234		CXXFLAGS="-g -O2 $kernelCcFlags" || {
235		echo "Error: Building kernel libsupc++ failed." >&2
236		exit 1
237	}
238
239	cp $haikuMachine/libstdc++-v3/libsupc++/.libs/libsupc++.a \
240		$installDir/$haikuMachine/lib/libsupc++-kernel.a || exit 1
241fi
242
243# cleanup
244
245# remove the system headers from the installation dir
246# Only the ones from the source tree should be used.
247rm -rf $installDir/$haikuMachine/sys-include
248
249# remove the objects dir
250rm -rf $objDir
251
252
253echo "binutils and gcc for cross compilation have been built successfully!"
254