build_cross_tools_gcc4 revision 901c33fc
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	binutilsConfigureArgs="--enable-multilib"
32	gccConfigureArgs="--enable-multilib"
33	;;
34m68k-*)
35	binutilsConfigureArgs="--enable-multilib"
36	gccConfigureArgs="--enable-multilib"
37	;;
38arm-*)
39	binutilsConfigureArgs="--enable-multilib"
40	gccConfigureArgs="--enable-multilib"
41	;;
42*)
43	binutilsConfigureArgs="--disable-multilib"
44	gccConfigureArgs="--disable-multilib"
45	;;
46esac
47
48if [ ! -d $haikuSourceDir ]; then
49	echo "No such directory: \"$haikuSourceDir\"" >&2
50	exit 1
51fi
52
53if [ ! -d $buildToolsDir ]; then
54	echo "No such directory: \"$buildToolsDir\"" >&2
55	exit 1
56fi
57
58
59# create the output dir
60mkdir -p $haikuOutputDir || exit 1
61
62
63# get absolute paths
64currentDir=$(pwd)
65
66cd $haikuSourceDir
67haikuSourceDir=$(pwd)
68cd $currentDir
69
70cd $buildToolsDir
71buildToolsDir=$(pwd)
72cd $currentDir
73
74cd $haikuOutputDir
75haikuOutputDir=$(pwd)
76
77binutilsSourceDir=$buildToolsDir/binutils
78gccSourceDir=$buildToolsDir/gcc
79
80
81# get gcc version
82gccVersion=$(cat $gccSourceDir/gcc/BASE-VER)
83
84if [ -z "$gccVersion" ]; then
85	echo "Failed to find out gcc version." >&2
86	exit 1
87fi
88
89
90# create the object and installation directories for the cross compilation tools
91installDir=$haikuOutputDir/cross-tools
92objDir=$haikuOutputDir/cross-tools-build
93binutilsObjDir=$objDir/binutils
94gccObjDir=$objDir/gcc
95tmpIncludeDir=$objDir/sysincludes
96tmpLibDir=$objDir/syslibs
97
98rm -rf $installDir $objDir
99
100mkdir -p $installDir $objDir $binutilsObjDir $gccObjDir $tmpIncludeDir \
101	$tmpLibDir || exit 1
102mkdir -p $installDir/lib/gcc/$haikuMachine/$gccVersion
103
104
105# build binutils
106cd $binutilsObjDir
107CFLAGS="-O2" CXXFLAGS="-O2" $binutilsSourceDir/configure \
108	--prefix=$installDir --target=$haikuMachine --disable-nls \
109	--disable-shared --disable-werror $binutilsConfigureArgs || exit 1
110$MAKE $additionalMakeArgs || exit 1
111$MAKE $additionalMakeArgs install || exit 1
112
113export PATH=$PATH:$installDir/bin
114
115
116# build gcc
117
118# prepare the include files
119copy_headers()
120{
121	sourceDir=$1
122	targetDir=$2
123
124	headers="$(find $sourceDir -name \*\.h | grep -v /.svn)"
125	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
126	for f in $headers; do
127		headerTargetDir=$targetDir/$(dirname $f)
128		mkdir -p $headerTargetDir
129		cp $sourceDir/$f $headerTargetDir
130	done
131}
132
133copy_headers $haikuSourceDir/headers/config $tmpIncludeDir/config
134copy_headers $haikuSourceDir/headers/os $tmpIncludeDir/os
135copy_headers $haikuSourceDir/headers/posix $tmpIncludeDir/posix
136
137# configure gcc
138cd $gccObjDir
139CFLAGS="-O2" CXXFLAGS="-O2" $gccSourceDir/configure --prefix=$installDir \
140	--target=$haikuMachine --disable-nls --disable-shared \
141	--enable-languages=c,c++ \
142	--with-headers=$tmpIncludeDir --with-libs=$tmpLibDir \
143	$gccConfigureArgs || exit 1
144
145# make gcc
146$MAKE $additionalMakeArgs || {
147	echo "ERROR: Building gcc failed." >&2
148	exit 1
149}
150
151# install gcc
152$MAKE $additionalMakeArgs install || {
153	echo "ERROR: Installing the cross compiler failed." >&2
154	exit 1
155}
156
157# cleanup
158
159# remove the system headers from the installation dir
160# Only the ones from the source tree should be used.
161sysIncludeDir=$installDir/$haikuMachine/sys-include
162rm -rf $sysIncludeDir/os $sysIncludeDir/posix
163
164# remove the objects dir
165rm -rf $objDir
166
167
168echo "binutils and gcc for cross compilation have been built successfully!"
169