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