build_cross_tools_gcc4 revision c864ba1a2d8525aeabd6199c01675f70f5abd7bd
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	kernelSupcxxFlags="-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	kernelSupcxxFlags=
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 -name \*.info -print0 | xargs -0 touch
99find $gccSourceDir -name \*.info -print0 | xargs -0 touch
100
101# create the object and installation directories for the cross compilation tools
102installDir=$haikuOutputDir/cross-tools
103objDir=$haikuOutputDir/cross-tools-build
104binutilsObjDir=$objDir/binutils
105gccObjDir=$objDir/gcc
106stdcxxObjDir=$objDir/stdcxx
107tmpIncludeDir=$objDir/sysincludes
108tmpLibDir=$objDir/syslibs
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
116
117# build binutils
118cd $binutilsObjDir
119CFLAGS="-O2" CXXFLAGS="-O2" $binutilsSourceDir/configure \
120	--prefix=$installDir --target=$haikuMachine --disable-nls \
121	--disable-shared --disable-werror $binutilsConfigureArgs || exit 1
122$MAKE $additionalMakeArgs || exit 1
123$MAKE $additionalMakeArgs install || exit 1
124
125export PATH=$PATH:$installDir/bin
126
127
128# build gcc
129
130# prepare the include files
131copy_headers()
132{
133	sourceDir=$1
134	targetDir=$2
135
136	headers="$(find $sourceDir -name \*\.h | grep -v /.svn)"
137	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
138	for f in $headers; do
139		headerTargetDir=$targetDir/$(dirname $f)
140		mkdir -p $headerTargetDir
141		cp $sourceDir/$f $headerTargetDir
142	done
143}
144
145copy_headers $haikuSourceDir/headers/config $tmpIncludeDir/config
146copy_headers $haikuSourceDir/headers/os $tmpIncludeDir/os
147copy_headers $haikuSourceDir/headers/posix $tmpIncludeDir/posix
148
149# configure gcc
150cd $gccObjDir
151CFLAGS="-O2" CXXFLAGS="-O2" $gccSourceDir/configure --prefix=$installDir \
152	--target=$haikuMachine --disable-nls --disable-shared --with-system-zlib \
153	--enable-languages=c,c++ --enable-lto --enable-frame-pointer \
154	--with-headers=$tmpIncludeDir --with-libs=$tmpLibDir \
155	$gccConfigureArgs || exit 1
156
157# make gcc
158$MAKE $additionalMakeArgs || {
159	echo "ERROR: Building gcc failed." >&2
160	exit 1
161}
162
163# install gcc
164$MAKE $additionalMakeArgs install || {
165	echo "ERROR: Installing the cross compiler failed." >&2
166	exit 1
167}
168
169# build libsupc++ for the kernel if the target arch requires it
170if [ -n "$kernelSupcxxFlags" ]; then
171	cd $stdcxxObjDir
172	CFLAGS="-O2 $kernelSupcxxFlags" CXXFLAGS="-O2 $kernelSupcxxFlags" \
173		$gccSourceDir/libstdc++-v3/configure --prefix=$installDir \
174		--target=$haikuMachine --host=$haikuMachine --disable-shared \
175		--disable-multilib || exit 1
176	$MAKE $additionalMakeArgs -C libsupc++ || {
177		echo "Error: Building kernel libsupc++ failed." >&2
178		exit 1
179	}
180	cp libsupc++/.libs/libsupc++.a $installDir/$haikuMachine/lib/libsupc++-kernel.a
181fi
182
183# cleanup
184
185# remove the system headers from the installation dir
186# Only the ones from the source tree should be used.
187sysIncludeDir=$installDir/$haikuMachine/sys-include
188rm -rf $sysIncludeDir/os $sysIncludeDir/posix
189
190# remove the objects dir
191rm -rf $objDir
192
193
194echo "binutils and gcc for cross compilation have been built successfully!"
195