xref: /haiku/build/scripts/build_cross_tools_gcc4 (revision 7a74a5df454197933bc6e80a542102362ee98703)
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
114# build binutils
115cd $binutilsObjDir
116CFLAGS="-O2" CXXFLAGS="-O2" $binutilsSourceDir/configure \
117	--prefix=$installDir --target=$haikuMachine --disable-nls \
118	--disable-shared --disable-werror $binutilsConfigureArgs || exit 1
119$MAKE $additionalMakeArgs || exit 1
120$MAKE $additionalMakeArgs install || exit 1
121
122export PATH=$PATH:$installDir/bin
123
124
125# build gcc
126
127# prepare the include files
128copy_headers()
129{
130	sourceDir=$1
131	targetDir=$2
132
133	headers="$(find $sourceDir -name \*\.h | grep -v /.svn)"
134	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
135	for f in $headers; do
136		headerTargetDir=$targetDir/$(dirname $f)
137		mkdir -p $headerTargetDir
138		cp $sourceDir/$f $headerTargetDir
139	done
140}
141
142copy_headers $haikuSourceDir/headers/config $tmpIncludeDir/config
143copy_headers $haikuSourceDir/headers/os $tmpIncludeDir/os
144copy_headers $haikuSourceDir/headers/posix $tmpIncludeDir/posix
145
146# configure gcc
147cd $gccObjDir
148CFLAGS="-O2" CXXFLAGS="-O2" $gccSourceDir/configure --prefix=$installDir \
149	--target=$haikuMachine --disable-nls --disable-shared --with-system-zlib \
150	--enable-languages=c,c++ --enable-lto --enable-frame-pointer \
151	--with-headers=$tmpIncludeDir --with-libs=$tmpLibDir \
152	$gccConfigureArgs || exit 1
153
154# make gcc
155$MAKE $additionalMakeArgs || {
156	echo "ERROR: Building gcc failed." >&2
157	exit 1
158}
159
160# install gcc
161$MAKE $additionalMakeArgs install || {
162	echo "ERROR: Installing the cross compiler failed." >&2
163	exit 1
164}
165
166# cleanup
167
168# remove the system headers from the installation dir
169# Only the ones from the source tree should be used.
170sysIncludeDir=$installDir/$haikuMachine/sys-include
171rm -rf $sysIncludeDir/os $sysIncludeDir/posix
172
173# remove the objects dir
174rm -rf $objDir
175
176
177echo "binutils and gcc for cross compilation have been built successfully!"
178