xref: /haiku/build/scripts/build_cross_tools (revision 180e55a48b0ab615bb710530840c4f21f5e918f1)
1#!/bin/sh
2
3# parameters <haiku sourcedir> <buildtools dir> [ <haiku output dir> ]
4
5# get and check the parameters
6if [ $# -lt 2 ]; then
7	echo Usage: $0 '<haiku sourcedir> <buildtools dir> [ <haiku output dir> ]' >&2
8	exit 1
9fi
10
11haikuSourceDir=$1
12buildToolsDir=$2/legacy
13
14if [ $# -lt 3 ]; then
15	haikuOutputDir=$haikuSourceDir/generated
16else
17	haikuOutputDir=$3
18fi
19
20if [ ! -d $haikuSourceDir ]; then
21	echo "No such directory: \"$haikuSourceDir\"" >&2
22	exit 1
23fi
24
25if [ ! -d $buildToolsDir ]; then
26	echo "No such directory: \"$buildToolsDir\"" >&2
27	exit 1
28fi
29
30
31# create the output dir
32mkdir -p $haikuOutputDir || exit 1
33
34
35# get absolute paths
36currentDir=`pwd`
37
38cd $haikuSourceDir
39haikuSourceDir=`pwd`
40cd $currentDir
41
42cd $buildToolsDir
43buildToolsDir=`pwd`
44cd $currentDir
45
46cd $haikuOutputDir
47haikuOutputDir=`pwd`
48
49
50# create the object and installation directories for the cross compilation tools
51installDir=$haikuOutputDir/cross-tools
52objDir=$haikuOutputDir/cross-tools-build
53binutilsObjDir=$objDir/binutils
54gccObjDir=$objDir/gcc
55tmpIncludeDir=$objDir/sysincludes
56tmpLibDir=$objDir/syslibs
57
58rm -rf $installDir $objDir
59
60mkdir -p $installDir $objDir $binutilsObjDir $gccObjDir $tmpIncludeDir \
61	$tmpLibDir || exit 1
62mkdir -p $installDir/lib/gcc-lib/i586-pc-haiku/$haikuRequiredLegacyGCCVersion
63
64# build binutils
65cd $binutilsObjDir
66CFLAGS="-O2" CXXFLAGS="-O2" $buildToolsDir/binutils/configure \
67	--prefix=$installDir --target=i586-pc-haiku --disable-nls \
68	--enable-shared=yes --disable-werror || exit 1
69make || exit 1
70make install || exit 1
71
72PATH=$PATH:$installDir/bin
73export PATH
74
75
76# build gcc
77
78# prepare the include files
79copy_headers()
80{
81	sourceDir=$1
82	targetDir=$2
83
84	headers="`find $sourceDir -name \*\.h | grep -v /.svn`"
85	headers="`echo $headers | sed -e s@$sourceDir/@@g`"
86	for f in $headers; do
87		headerTargetDir=$targetDir/`dirname $f`
88		mkdir -p $headerTargetDir
89		cp $sourceDir/$f $headerTargetDir
90	done
91}
92
93copy_headers $haikuSourceDir/headers/os $tmpIncludeDir/be
94copy_headers $haikuSourceDir/headers/posix $tmpIncludeDir/posix
95
96# configure gcc
97cd $gccObjDir
98CFLAGS="-O2 -U_FORTIFY_SOURCE" CXXFLAGS="-O2" $buildToolsDir/gcc/configure \
99	--prefix=$installDir \
100	--target=i586-pc-haiku --disable-nls --enable-shared=yes \
101	--enable-languages=c,c++ --with-headers=$tmpIncludeDir \
102	--with-libs=$tmpLibDir || exit 1
103
104# hack the Makefile to avoid trouble with stuff we don't need anyway
105sedExpr=
106for toRemove in libiberty libio libjava libobjc libstdc++; do
107	sedExpr="$sedExpr -e 's@^\(TARGET_CONFIGDIRS =.*\)$toRemove\(.*\)@\1\2@'"
108done
109echo sedExpr: $sedExpr
110mv Makefile Makefile.bak || exit 1
111eval "sed $sedExpr Makefile.bak > Makefile" || exit 1
112rm Makefile.bak
113
114# make gcc
115make cross || {
116	echo "ERROR: Building gcc failed." >&2
117	exit 1
118}
119
120# install gcc
121make install-gcc-cross || {
122	echo "ERROR: Installing the cross compiler failed." >&2
123	exit 1
124}
125
126# Remove the math.h gcc header. It has been generated by fixincludes
127# (unconditional hack: math_huge_val_ifndef) from ours and it is semantically
128# equivalent.
129rm $installDir/lib/gcc-lib/i586-pc-haiku/$haikuRequiredLegacyGCCVersion/include/math.h
130
131
132# cleanup
133
134# remove the system headers from the installation dir
135# Only the ones from the source tree should be used.
136sysIncludeDir=$installDir/i586-pc-haiku/sys-include
137rm -rf $sysIncludeDir/be $sysIncludeDir/posix
138
139# remove the objects dir
140rm -rf $objDir
141
142
143echo "binutils and gcc for cross compilation have been built successfully!"
144
145