RepositoryRules: RepositoryConfig does not need/use URL now.
[haiku.git] / build / scripts / build_cross_tools
blob19e0ee303f1730deeac10f3a2669964ba3abeefa
1 #!/bin/sh
3 # Parameters <haiku sourcedir> <buildtools dir> <install dir>
4 # Influential environmental variable:
5 # * haikuRequiredLegacyGCCVersion: The required version of the gcc. Will be
6 # checked against the version in the buildtools directory.
8 # get and check the parameters
9 if [ $# -lt 3 ]; then
10 echo Usage: $0 '<haiku sourcedir> <buildtools dir> <install dir>' >&2
11 exit 1
14 haikuSourceDir=$1
15 buildToolsDir=$2/legacy
16 installDir=$3
17 shift 3
18 additionalMakeArgs=$*
19 # Note: The gcc 2 build has trouble with -jN N > 1, hence we only use the
20 # additional flags for the binutils build. Should there ever be any other
21 # flags than -jN, we need to handle this differently.
23 if [ `uname -s` = 'Haiku' ]; then
24 # force cross-build if building on Haiku:
25 buildhostMachine=i586-pc-haiku_buildhost
26 buildHostSpec="--build=$buildhostMachine --host=$buildhostMachine"
29 if [ ! -d $haikuSourceDir ]; then
30 echo "ERROR: No such directory: \"$haikuSourceDir\"" >&2
31 exit 1
34 if [ ! -d $buildToolsDir ]; then
35 echo "ERROR: No such directory: \"$buildToolsDir\"" >&2
36 exit 1
39 # verify or extract the gcc version
40 gccVersionDotC="$buildToolsDir/gcc/gcc/version.c"
41 if [ -n "$haikuRequiredLegacyGCCVersion" ]; then
42 # haikuRequiredLegacyGCCVersion has been specified. Check whether the
43 # version of the gcc in the given build tools directory matches.
44 if ! grep "$haikuRequiredLegacyGCCVersion" \
45 "$gccVersionDotC" >/dev/null 2>&1 ; then
46 echo "*** Mismatching compiler versions between configure script and" \
47 >&2
48 echo "*** $gccVersionDotC" >&2
49 echo "*** Did you perhaps update only one of haiku & buildtools?" >&2
50 exit 1
52 else
53 # haikuRequiredLegacyGCCVersion wasn't specified. Extract the version string
54 # from the gcc's version.c.
55 haikuRequiredLegacyGCCVersion=`grep "2.95.3-haiku-" "$gccVersionDotC" \
56 | sed 's@.*\"\(.*\)\".*@\1@'`
57 if [ -z "$haikuRequiredLegacyGCCVersion" ]; then
58 echo "ERROR: Failed to extract version string from $gccVersionDotC" >&2
59 exit 1
64 # create the output dir
65 mkdir -p $installDir || exit 1
68 # get absolute paths
69 currentDir=`pwd`
71 cd $haikuSourceDir
72 haikuSourceDir=`pwd`
73 cd $currentDir
75 cd $buildToolsDir
76 buildToolsDir=`pwd`
77 cd $currentDir
79 cd $installDir
80 installDir=`pwd`
81 cd $currentDir
84 # create the object and installation directories for the cross compilation tools
85 objDir=${installDir}-build
86 binutilsObjDir=$objDir/binutils
87 gccObjDir=$objDir/gcc
88 tmpIncludeDir=$objDir/sysincludes
89 tmpLibDir=$objDir/syslibs
91 rm -rf $installDir $objDir
93 mkdir -p $installDir $objDir $binutilsObjDir $gccObjDir $tmpIncludeDir \
94 $tmpLibDir || exit 1
95 mkdir -p $installDir/lib/gcc-lib/i586-pc-haiku/$haikuRequiredLegacyGCCVersion
97 gccConfigureArgs=
98 if [ -n "$SECONDARY_ARCH" ]; then
99 gccConfigureArgs="$gccConfigureArgs --with-hybrid-secondary=$SECONDARY_ARCH"
102 # force the POSIX locale, as the build (makeinfo) might choke otherwise
103 export LC_ALL=POSIX
105 # drop an multiple job arguments from MAKEFLAGS
106 if [ ! -z "$MAKEFLAGS" ]; then
107 export MAKEFLAGS=$(echo $MAKEFLAGS | sed -e 's/-j\s*[0-9][0-9]*//g')
111 # build binutils
112 cd $binutilsObjDir
113 CFLAGS="-O2" CXXFLAGS="-O2" $buildToolsDir/binutils/configure \
114 --prefix=$installDir $buildHostSpec --target=i586-pc-haiku \
115 --disable-nls --enable-shared=yes --disable-werror || exit 1
116 $MAKE $additionalMakeArgs || exit 1
117 $MAKE $additionalMakeArgs install || exit 1
119 PATH=$PATH:$installDir/bin
120 export PATH
123 # build gcc
125 # prepare the include files
126 copy_headers()
128 sourceDir=$1
129 targetDir=$2
131 headers="`find $sourceDir -name \*\.h`"
132 headers="`echo $headers | sed -e s@$sourceDir/@@g`"
133 for f in $headers; do
134 headerTargetDir=$targetDir/`dirname $f`
135 mkdir -p $headerTargetDir
136 cp $sourceDir/$f $headerTargetDir
137 done
140 copy_headers $haikuSourceDir/headers/config $tmpIncludeDir/config
141 copy_headers $haikuSourceDir/headers/os $tmpIncludeDir/os
142 copy_headers $haikuSourceDir/headers/posix $tmpIncludeDir/posix
144 # Touch some files generated by bison, so that bison won't run to update them.
145 # Fixes issues with newer bison versions.
146 # And while at it, touch gperf target, too (as gperf may not be installed)
147 (cd $buildToolsDir/gcc/gcc; touch c-parse.c c-parse.h cexp.c cp/parse.c \
148 cp/parse.h c-gperf.h)
150 # configure gcc
151 cd $gccObjDir
152 case `uname` in
153 Darwin)
154 # GCC 2 compiled for x86_64 OS X is broken, compile for i386.
155 export CC="gcc -arch i386"
157 esac
158 CFLAGS="-O2 -U_FORTIFY_SOURCE" CXXFLAGS="-O2" $buildToolsDir/gcc/configure \
159 --prefix=$installDir $buildHostSpec --target=i586-pc-haiku \
160 --disable-nls --enable-shared=yes --enable-languages=c,c++ \
161 --with-headers=$tmpIncludeDir --with-libs=$tmpLibDir $gccConfigureArgs \
162 || exit 1
163 unset CC
165 # hack the Makefile to avoid trouble with stuff we don't need anyway
166 sedExpr=
167 for toRemove in libiberty libio libjava libobjc libstdc++; do
168 sedExpr="$sedExpr -e 's@^\(TARGET_CONFIGDIRS =.*\)$toRemove\(.*\)@\1\2@'"
169 done
170 echo sedExpr: $sedExpr
171 mv Makefile Makefile.bak || exit 1
172 eval "sed $sedExpr Makefile.bak > Makefile" || exit 1
173 rm Makefile.bak
175 # make gcc
176 $MAKE cross || {
177 echo "ERROR: Building gcc failed." >&2
178 exit 1
181 # install gcc
182 $MAKE install-gcc-cross || {
183 echo "ERROR: Installing the cross compiler failed." >&2
184 exit 1
187 # Remove the math.h gcc header. It has been generated by fixincludes
188 # (unconditional hack: math_huge_val_ifndef) from ours and it is semantically
189 # equivalent.
190 rm -f $installDir/lib/gcc-lib/i586-pc-haiku/$haikuRequiredLegacyGCCVersion/include/math.h
192 # Symlink the built-in C++ headers path to the sys-include directory. This is
193 # not actually needed for cross compiling Haiku itself, but simplifies using the
194 # cross compiler for building the bootstrap packages.
195 (cd $installDir/include; ln -s ../i586-pc-haiku/sys-include/c++/2.95.3 g++)
198 # cleanup
200 # remove the system headers from the installation dir
201 # Only the ones from the source tree should be used.
202 rm -rf $installDir/i586-pc-haiku/sys-include
204 # remove the objects dir
205 rm -rf $objDir
208 echo "binutils and gcc for cross compilation have been built successfully!"