build_cross_tools_gcc4 revision 1e93288e656fdcaf4ec315b6f1ae5568bfd4acee
1#!/bin/sh
2#
3# parameters <machine> <haiku sourcedir> <buildtools dir> <isntall dir>
4
5# get and check the parameters
6if [ $# -lt 4 ]; then
7	echo Usage: $0 '<machine> <haiku sourcedir> <buildtools dir>' \
8		'<install dir>' >&2
9	exit 1
10fi
11
12haikuMachine=$1
13haikuSourceDir=$2
14buildToolsDir=$3
15installDir=$4
16shift 4
17additionalMakeArgs=$*
18
19case $haikuMachine in
20x86_64-*)
21	# GCC's default is to enable multilib, but there is a bug when
22	# explicitly using --enable-multilib that causes a build
23	# failure
24	binutilsConfigureArgs=""
25	gccConfigureArgs=""
26	kernelCcFlags="-mno-red-zone"
27	;;
28m68k-*)
29	binutilsConfigureArgs="--enable-multilib"
30	gccConfigureArgs="--enable-multilib"
31	;;
32arm-*)
33	binutilsConfigureArgs="--enable-multilib"
34	gccConfigureArgs="--enable-multilib"
35	;;
36*)
37	binutilsConfigureArgs="--disable-multilib"
38	gccConfigureArgs="--disable-multilib"
39	kernelCcFlags=
40	;;
41esac
42
43if [ `uname -s` = 'Haiku' ]; then
44	# force cross-build if building on Haiku:
45	buildhostMachine=${haikuMachine}_buildhost
46	buildHostSpec="--build=$buildhostMachine --host=$buildhostMachine"
47fi
48
49if [ ! -d "$haikuSourceDir" ]; then
50	echo "No such directory: \"$haikuSourceDir\"" >&2
51	exit 1
52fi
53
54if [ ! -d "$buildToolsDir" ]; then
55	echo "No such directory: \"$buildToolsDir\"" >&2
56	exit 1
57fi
58
59
60# create the output dir
61mkdir -p "$installDir" || exit 1
62
63
64# get absolute paths
65currentDir=$(pwd)
66
67cd "$haikuSourceDir"
68haikuSourceDir=$(pwd)
69cd "$currentDir"
70
71cd "$buildToolsDir"
72buildToolsDir=$(pwd)
73cd "$currentDir"
74
75cd "$installDir"
76installDir=$(pwd)
77cd "$currentDir"
78
79binutilsSourceDir="$buildToolsDir/binutils"
80gccSourceDir="$buildToolsDir/gcc"
81
82
83# get gcc version
84gccVersion=$(cat "$gccSourceDir/gcc/BASE-VER")
85
86if [ -z "$gccVersion" ]; then
87	echo "Failed to find out gcc version." >&2
88	exit 1
89fi
90
91# touch all info files in order to avoid the dependency on makeinfo
92# (which apparently doesn't work reliably on all the different host 
93# configurations and changes files which in turn appear as local changes
94# to the VCS).
95find "$binutilsSourceDir" "$gccSourceDir" -name \*.info -print0 | xargs -0 touch
96
97# create the object and installation directories for the cross compilation tools
98objDir="${installDir}-build"
99binutilsObjDir="$objDir/binutils"
100gccObjDir="$objDir/gcc"
101stdcxxObjDir="$objDir/stdcxx"
102sysrootDir="$installDir/sysroot"
103tmpIncludeDir="$sysrootDir/boot/system/develop/headers"
104tmpLibDir="$sysrootDir/boot/system/develop/lib"
105
106rm -rf "$installDir" "$objDir"
107
108mkdir -p "$installDir" "$objDir" "$binutilsObjDir" "$gccObjDir" "$stdcxxObjDir" \
109	"$tmpIncludeDir" "$tmpLibDir" || exit 1
110mkdir -p "$installDir/lib/gcc/$haikuMachine/$gccVersion"
111
112if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
113	cloogSourceDir="$buildToolsDir/cloog"
114	gmpSourceDir="$buildToolsDir/gcc/gmp"
115	islSourceDir="$buildToolsDir/isl"
116
117	islObjDir="$objDir/isl"
118	gmpObjDir="$objDir/gmp"
119	cloogObjDir="$objDir/cloog"
120	mkdir -p "$islObjDir" "$gmpObjDir" "$cloogObjDir" || exit 1
121
122	gccConfigureArgs="$gccConfigureArgs --with-cloog=$installDir \
123		--enable-cloog-backend=isl --with-isl=$installDir \
124		--with-gmp=$installDir \
125		--with-host-libstdcxx=\"-lstdc++\""
126fi
127
128if [ -n "$SECONDARY_ARCH" ]; then
129	gccConfigureArgs="$gccConfigureArgs --with-hybrid-secondary=$SECONDARY_ARCH"
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 -pipe" CXXFLAGS="-O2" "$binutilsSourceDir/configure" \
138	--prefix="$installDir" $buildHostSpec --target=$haikuMachine \
139	--enable-targets=$haikuMachine,i386-efi-pe,x86_64-efi-pe \
140	--disable-nls --disable-shared --disable-werror \
141	--with-sysroot="$sysrootDir" \
142	$binutilsConfigureArgs \
143	|| exit 1
144$MAKE $additionalMakeArgs || exit 1
145$MAKE $additionalMakeArgs install || exit 1
146
147export PATH=$PATH:"$installDir/bin"
148
149if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
150	# build gmp
151	cd "$gmpObjDir"
152	"$gmpSourceDir/configure" --prefix="$installDir" \
153		--disable-shared --enable-cxx || exit 1
154	$MAKE $additionalMakeArgs || exit 1
155	$MAKE $additionalMakeArgs install || exit 1
156
157	# build isl
158	cd "$islObjDir"
159	CFLAGS="-O2 -pipe" CXXFLAGS="-O2 -pipe" "$islSourceDir/configure" \
160		--prefix="$installDir" --disable-nls --disable-shared \
161		 --with-gmp-prefix="$installDir" || exit 1
162	$MAKE $additionalMakeArgs || exit 1
163	$MAKE $additionalMakeArgs install || exit 1
164
165	# build cloog
166	cd "$cloogObjDir"
167	CFLAGS="-O2 -pipe" CXXFLAGS="-O2" "$cloogSourceDir/configure" \
168		--prefix="$installDir" --disable-nls --disable-shared \
169		--with-gmp-prefix="$installDir" --with-isl-prefix=="$installDir" \
170		|| exit 1
171	$MAKE $additionalMakeArgs || exit 1
172	$MAKE $additionalMakeArgs install || exit 1
173fi
174
175# build gcc
176
177# prepare the include files
178copy_headers()
179{
180	sourceDir=$1
181	targetDir=$2
182
183	headers="$(find $sourceDir -name \*\.h)"
184	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
185	for f in $headers; do
186		headerTargetDir="$targetDir/$(dirname $f)"
187		mkdir -p "$headerTargetDir"
188		cp "$sourceDir/$f" "$headerTargetDir"
189	done
190}
191
192copy_headers "$haikuSourceDir/headers/config" "$tmpIncludeDir/config"
193copy_headers "$haikuSourceDir/headers/os" "$tmpIncludeDir/os"
194copy_headers "$haikuSourceDir/headers/posix" "$tmpIncludeDir/posix"
195
196# configure gcc
197cd "$gccObjDir"
198CFLAGS="-O2 -pipe" CXXFLAGS="-O2" "$gccSourceDir/configure" \
199	--prefix="$installDir" $buildHostSpec --target=$haikuMachine \
200	--disable-nls --disable-shared --with-system-zlib \
201	--enable-languages=c,c++ --enable-lto --enable-frame-pointer \
202	--with-sysroot="$sysrootDir" \
203	$gccConfigureArgs \
204	|| exit 1
205
206# make gcc
207$MAKE $additionalMakeArgs || {
208	echo "ERROR: Building gcc failed." >&2
209	exit 1
210}
211
212# install gcc
213$MAKE $additionalMakeArgs install || {
214	echo "ERROR: Installing the cross compiler failed." >&2
215	exit 1
216}
217
218# build libraries for the kernel if the target arch requires it
219if [ -n "$kernelCcFlags" ]; then
220	$MAKE -C "$haikuMachine/libgcc" clean
221	$MAKE -C "$haikuMachine/libgcc" CFLAGS="-g -O2 $kernelCcFlags" || {
222		echo "Error: Building kernel libgcc failed." >&2
223		exit 1
224	}
225
226	cp "$haikuMachine/libgcc/libgcc.a" \
227		"$installDir/$haikuMachine/lib/libgcc-kernel.a" || exit 1
228
229	$MAKE -C "$haikuMachine/libstdc++-v3/libsupc++" clean
230	$MAKE -C "$haikuMachine/libstdc++-v3/libsupc++" CFLAGS="-g -O2 $kernelCcFlags" \
231		CXXFLAGS="-g -O2 $kernelCcFlags" || {
232		echo "Error: Building kernel libsupc++ failed." >&2
233		exit 1
234	}
235
236	cp "$haikuMachine/libstdc++-v3/libsupc++/.libs/libsupc++.a" \
237		"$installDir/$haikuMachine/lib/libsupc++-kernel.a" || exit 1
238fi
239
240# cleanup
241
242# remove the system headers from the installation dir
243# Only the ones from the source tree should be used.
244rm -rf "$installDir/$haikuMachine/sys-include"
245
246# remove the sysroot dir
247rm -rf "$sysrootDir"
248
249# remove the objects dir
250rm -rf "$objDir"
251
252
253echo "binutils and gcc for cross compilation have been built successfully!"
254