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