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