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	kernelCcFlags="-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	kernelCcFlags=
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# force the POSIX locale, as the build (makeinfo) might choke otherwise
117export LC_ALL=POSIX
118
119# build binutils
120cd $binutilsObjDir
121CFLAGS="-O2" CXXFLAGS="-O2" $binutilsSourceDir/configure \
122	--prefix=$installDir --target=$haikuMachine --disable-nls \
123	--disable-shared --disable-werror $binutilsConfigureArgs || exit 1
124$MAKE $additionalMakeArgs || exit 1
125$MAKE $additionalMakeArgs install || exit 1
126
127export PATH=$PATH:$installDir/bin
128
129
130# build gcc
131
132# prepare the include files
133copy_headers()
134{
135	sourceDir=$1
136	targetDir=$2
137
138	headers="$(find $sourceDir -name \*\.h)"
139	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
140	for f in $headers; do
141		headerTargetDir=$targetDir/$(dirname $f)
142		mkdir -p $headerTargetDir
143		cp $sourceDir/$f $headerTargetDir
144	done
145}
146
147copy_headers $haikuSourceDir/headers/config $tmpIncludeDir/config
148copy_headers $haikuSourceDir/headers/os $tmpIncludeDir/os
149copy_headers $haikuSourceDir/headers/posix $tmpIncludeDir/posix
150
151# configure gcc
152cd $gccObjDir
153CFLAGS="-O2" CXXFLAGS="-O2" $gccSourceDir/configure --prefix=$installDir \
154	--target=$haikuMachine --disable-nls --disable-shared --with-system-zlib \
155	--enable-languages=c,c++ --enable-lto --enable-frame-pointer \
156	--with-headers=$tmpIncludeDir --with-libs=$tmpLibDir \
157	$gccConfigureArgs || exit 1
158
159# make gcc
160$MAKE $additionalMakeArgs || {
161	echo "ERROR: Building gcc failed." >&2
162	exit 1
163}
164
165# install gcc
166$MAKE $additionalMakeArgs install || {
167	echo "ERROR: Installing the cross compiler failed." >&2
168	exit 1
169}
170
171# build libraries for the kernel if the target arch requires it
172if [ -n "$kernelCcFlags" ]; then
173	$MAKE -C $haikuMachine/libgcc clean
174	$MAKE -C $haikuMachine/libgcc CFLAGS="-g -O2 $kernelCcFlags" || {
175		echo "Error: Building kernel libgcc failed." >&2
176		exit 1
177	}
178
179	cp $haikuMachine/libgcc/libgcc.a \
180		$installDir/$haikuMachine/lib/libgcc-kernel.a || exit 1
181
182	$MAKE -C $haikuMachine/libstdc++-v3/libsupc++ clean
183	$MAKE -C $haikuMachine/libstdc++-v3/libsupc++ CFLAGS="-g -O2 $kernelCcFlags" \
184		CXXFLAGS="-g -O2 $kernelCcFlags" || {
185		echo "Error: Building kernel libsupc++ failed." >&2
186		exit 1
187	}
188
189	cp $haikuMachine/libstdc++-v3/libsupc++/.libs/libsupc++.a \
190		$installDir/$haikuMachine/lib/libsupc++-kernel.a || exit 1
191fi
192
193# cleanup
194
195# remove the system headers from the installation dir
196# Only the ones from the source tree should be used.
197sysIncludeDir=$installDir/$haikuMachine/sys-include
198rm -rf $sysIncludeDir/os $sysIncludeDir/posix
199
200# remove the objects dir
201rm -rf $objDir
202
203
204echo "binutils and gcc for cross compilation have been built successfully!"
205