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