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