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