build_cross_tools_gcc4 revision f868be68635cd3b6ed42cae7047b820ecea8f6a6
1#!/bin/sh
2#
3# parameters <machine> <haiku sourcedir> <buildtools dir> [ <haiku output dir> ]
4
5# get and check the parameters
6if [ $# -lt 3 ]; then
7	echo Usage: $0 '<machine> <haiku sourcedir> <buildtools dir> [ <haiku output dir> ]' >&2
8	exit 1
9fi
10
11haikuMachine=$1
12haikuSourceDir=$2
13buildToolsDir=$3
14
15case $(uname) in
16FreeBSD|OpenBSD)
17	MAKE=gmake
18	;;
19*)
20	MAKE=make
21	;;
22esac
23export MAKE
24
25if [ $# -lt 4 ]; then
26	haikuOutputDir=$haikuSourceDir/generated
27else
28	haikuOutputDir=$4
29fi
30
31if [ ! -d $haikuSourceDir ]; then
32	echo "No such directory: \"$haikuSourceDir\"" >&2
33	exit 1
34fi
35
36if [ ! -d $buildToolsDir ]; then
37	echo "No such directory: \"$buildToolsDir\"" >&2
38	exit 1
39fi
40
41
42# create the output dir
43mkdir -p $haikuOutputDir || exit 1
44
45
46# get absolute paths
47currentDir=$(pwd)
48
49cd $haikuSourceDir
50haikuSourceDir=$(pwd)
51cd $currentDir
52
53cd $buildToolsDir
54buildToolsDir=$(pwd)
55cd $currentDir
56
57cd $haikuOutputDir
58haikuOutputDir=$(pwd)
59
60binutilsSourceDir=$buildToolsDir/binutils
61gccSourceDir=$buildToolsDir/gcc
62
63
64# get gcc version
65gccVersion=$(cat $gccSourceDir/gcc/BASE-VER)
66
67if [ -z "$gccVersion" ]; then
68	echo "Failed to find out gcc version." >&2
69	exit 1
70fi
71
72
73# create the object and installation directories for the cross compilation tools
74installDir=$haikuOutputDir/cross-tools
75objDir=$haikuOutputDir/cross-tools-build
76binutilsObjDir=$objDir/binutils
77gccObjDir=$objDir/gcc
78tmpIncludeDir=$objDir/sysincludes
79tmpLibDir=$objDir/syslibs
80
81rm -rf $installDir $objDir
82
83mkdir -p $installDir $objDir $binutilsObjDir $gccObjDir $tmpIncludeDir \
84	$tmpLibDir || exit 1
85mkdir -p $installDir/lib/gcc/$haikuMachine/$gccVersion
86
87
88# build binutils
89cd $binutilsObjDir
90CFLAGS="-O2" CXXFLAGS="-O2" $binutilsSourceDir/configure \
91	--prefix=$installDir --target=$haikuMachine --disable-nls \
92	--disable-shared || exit 1
93$MAKE || exit 1
94$MAKE install || exit 1
95
96export PATH=$PATH:$installDir/bin
97
98
99# build gcc
100
101# prepare the include files
102copy_headers()
103{
104	sourceDir=$1
105	targetDir=$2
106
107	headers="$(find $sourceDir -name \*\.h | grep -v /.svn)"
108	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
109	for f in $headers; do
110		headerTargetDir=$targetDir/$(dirname $f)
111		mkdir -p $headerTargetDir
112		cp $sourceDir/$f $headerTargetDir
113	done
114}
115
116copy_headers $haikuSourceDir/headers/os $tmpIncludeDir/be
117copy_headers $haikuSourceDir/headers/posix $tmpIncludeDir/posix
118
119# configure gcc
120cd $gccObjDir
121CFLAGS="-O2" CXXFLAGS="-O2" $gccSourceDir/configure --prefix=$installDir \
122	--target=$haikuMachine --disable-nls --disable-shared \
123	--enable-languages=c,c++ --with-headers=$tmpIncludeDir \
124	--with-libs=$tmpLibDir || exit 1
125
126# make gcc
127$MAKE || {
128	echo "ERROR: Building gcc failed." >&2
129	exit 1
130}
131
132# install gcc
133$MAKE install || {
134	echo "ERROR: Installing the cross compiler failed." >&2
135	exit 1
136}
137
138# cleanup
139
140# remove the system headers from the installation dir
141# Only the ones from the source tree should be used.
142sysIncludeDir=$installDir/$haikuMachine/sys-include
143rm -rf $sysIncludeDir/be $sysIncludeDir/posix
144
145# remove the objects dir
146rm -rf $objDir
147
148
149echo "binutils and gcc for cross compilation have been built successfully!"
150