1#!/bin/sh 2# 3# parameters <machine> <haiku sourcedir> <buildtools dir> <isntall dir> 4 5# get and check the parameters 6if [ $# -lt 4 ]; then 7 echo Usage: $0 '<machine> <haiku sourcedir> <buildtools dir>' \ 8 '<install dir>' >&2 9 exit 1 10fi 11 12haikuMachine=$1 13haikuSourceDir=$2 14buildToolsDir=$3 15installDir=$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 [ `uname -o` = 'Haiku' ]; then 54 # force cross-build if building on Haiku: 55 buildhostMachine=${haikuMachine}_buildhost 56 buildHostSpec="--build=$buildhostMachine --host=$buildhostMachine" 57fi 58 59if [ ! -d $haikuSourceDir ]; then 60 echo "No such directory: \"$haikuSourceDir\"" >&2 61 exit 1 62fi 63 64if [ ! -d $buildToolsDir ]; then 65 echo "No such directory: \"$buildToolsDir\"" >&2 66 exit 1 67fi 68 69 70# create the output dir 71mkdir -p $installDir || exit 1 72 73 74# get absolute paths 75currentDir=$(pwd) 76 77cd $haikuSourceDir 78haikuSourceDir=$(pwd) 79cd $currentDir 80 81cd $buildToolsDir 82buildToolsDir=$(pwd) 83cd $currentDir 84 85cd $installDir 86installDir=$(pwd) 87cd $currentDir 88 89binutilsSourceDir=$buildToolsDir/binutils 90gccSourceDir=$buildToolsDir/gcc 91 92 93# get gcc version 94gccVersion=$(cat $gccSourceDir/gcc/BASE-VER) 95 96if [ -z "$gccVersion" ]; then 97 echo "Failed to find out gcc version." >&2 98 exit 1 99fi 100 101# touch all info files in order to avoid the dependency on makeinfo 102# (which apparently doesn't work reliably on all the different host 103# configurations and changes files which in turn appear as local changes 104# to the VCS). 105find $binutilsSourceDir $gccSourceDir -name \*.info -print0 | xargs -0 touch 106 107# create the object and installation directories for the cross compilation tools 108objDir=${installDir}-build 109binutilsObjDir=$objDir/binutils 110gccObjDir=$objDir/gcc 111stdcxxObjDir=$objDir/stdcxx 112sysrootDir=$installDir/sysroot 113tmpIncludeDir=$sysrootDir/boot/system/develop/headers 114tmpLibDir=$sysrootDir/boot/system/develop/lib 115 116rm -rf $installDir $objDir 117 118mkdir -p $installDir $objDir $binutilsObjDir $gccObjDir $stdcxxObjDir \ 119 $tmpIncludeDir $tmpLibDir || exit 1 120mkdir -p $installDir/lib/gcc/$haikuMachine/$gccVersion 121 122if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then 123 cloogSourceDir=$buildToolsDir/cloog 124 gmpSourceDir=$buildToolsDir/gcc/gmp 125 pplSourceDir=$buildToolsDir/ppl 126 127 pplObjDir=$objDir/ppl 128 gmpObjDir=$objDir/gmp 129 cloogObjDir=$objDir/cloog 130 mkdir -p $pplObjDir $gmpObjDir $cloogObjDir || exit 1 131 132 gccConfigureArgs="$gccConfigureArgs --with-cloog=$installDir \ 133 --enable-cloog-backend=isl --with-ppl=$installDir \ 134 --disable-cloog-version-check --with-gmp=$installDir \ 135 --with-host-libstdcxx=\"-lstdc++\"" 136fi 137 138if [ -n "$SECONDARY_ARCH" ]; then 139 gccConfigureArgs="$gccConfigureArgs --enable-hybrid-secondary" 140fi 141 142# force the POSIX locale, as the build (makeinfo) might choke otherwise 143export LC_ALL=POSIX 144 145# build binutils 146cd $binutilsObjDir 147CFLAGS="-O2" CXXFLAGS="-O2" $binutilsSourceDir/configure \ 148 --prefix=$installDir $buildHostSpec --target=$haikuMachine \ 149 --disable-nls --disable-shared --disable-werror \ 150 --with-sysroot=$sysrootDir \ 151 $binutilsConfigureArgs \ 152 || exit 1 153$MAKE $additionalMakeArgs || exit 1 154$MAKE $additionalMakeArgs install || exit 1 155 156export PATH=$PATH:$installDir/bin 157 158if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then 159 # build gmp 160 cd $gmpObjDir 161 $gmpSourceDir/configure --prefix=$installDir \ 162 --disable-shared --enable-cxx || exit 1 163 $MAKE $additionalMakeArgs || exit 1 164 $MAKE $additionalMakeArgs install || exit 1 165 166 # build ppl 167 cd $pplObjDir 168 CFLAGS="-O2" CXXFLAGS="-O2" $pplSourceDir/configure --prefix=$installDir \ 169 --disable-nls --disable-shared --disable-watchdog \ 170 --disable-maintainer-mode || exit 1 171 $MAKE $additionalMakeArgs AUTOCONF:=true AUTOHEADER:=true ACLOCAL:=true \ 172 AUTOMAKE:=true || exit 1 173 $MAKE $additionalMakeArgs install AUTOCONF:=true AUTOHEADER:=true \ 174 ACLOCAL:=true AUTOMAKE:=true || exit 1 175 176 # build cloog 177 cd $cloogObjDir 178 CFLAGS="-O2" CXXFLAGS="-O2" $cloogSourceDir/configure \ 179 --prefix=$installDir --disable-nls --disable-shared \ 180 --with-gmp-prefix=$installDir || exit 1 181 $MAKE $additionalMakeArgs || exit 1 182 $MAKE $additionalMakeArgs install || exit 1 183fi 184 185# build gcc 186 187# prepare the include files 188copy_headers() 189{ 190 sourceDir=$1 191 targetDir=$2 192 193 headers="$(find $sourceDir -name \*\.h)" 194 headers="$(echo $headers | sed -e s@$sourceDir/@@g)" 195 for f in $headers; do 196 headerTargetDir=$targetDir/$(dirname $f) 197 mkdir -p $headerTargetDir 198 cp $sourceDir/$f $headerTargetDir 199 done 200} 201 202copy_headers $haikuSourceDir/headers/config $tmpIncludeDir/config 203copy_headers $haikuSourceDir/headers/os $tmpIncludeDir/os 204copy_headers $haikuSourceDir/headers/posix $tmpIncludeDir/posix 205 206# configure gcc 207cd $gccObjDir 208CFLAGS="-O2" CXXFLAGS="-O2" $gccSourceDir/configure \ 209 --prefix=$installDir $buildHostSpec --target=$haikuMachine \ 210 --disable-nls --disable-shared --with-system-zlib \ 211 --enable-languages=c,c++ --enable-lto --enable-frame-pointer \ 212 --with-sysroot=$sysrootDir \ 213 $gccConfigureArgs \ 214 || exit 1 215 216# make gcc 217$MAKE $additionalMakeArgs || { 218 echo "ERROR: Building gcc failed." >&2 219 exit 1 220} 221 222# install gcc 223$MAKE $additionalMakeArgs install || { 224 echo "ERROR: Installing the cross compiler failed." >&2 225 exit 1 226} 227 228# build libraries for the kernel if the target arch requires it 229if [ -n "$kernelCcFlags" ]; then 230 $MAKE -C $haikuMachine/libgcc clean 231 $MAKE -C $haikuMachine/libgcc CFLAGS="-g -O2 $kernelCcFlags" || { 232 echo "Error: Building kernel libgcc failed." >&2 233 exit 1 234 } 235 236 cp $haikuMachine/libgcc/libgcc.a \ 237 $installDir/$haikuMachine/lib/libgcc-kernel.a || exit 1 238 239 $MAKE -C $haikuMachine/libstdc++-v3/libsupc++ clean 240 $MAKE -C $haikuMachine/libstdc++-v3/libsupc++ CFLAGS="-g -O2 $kernelCcFlags" \ 241 CXXFLAGS="-g -O2 $kernelCcFlags" || { 242 echo "Error: Building kernel libsupc++ failed." >&2 243 exit 1 244 } 245 246 cp $haikuMachine/libstdc++-v3/libsupc++/.libs/libsupc++.a \ 247 $installDir/$haikuMachine/lib/libsupc++-kernel.a || exit 1 248fi 249 250# cleanup 251 252# remove the system headers from the installation dir 253# Only the ones from the source tree should be used. 254rm -rf $installDir/$haikuMachine/sys-include 255 256# remove the sysroot dir 257rm -rf $sysrootDir 258 259# remove the objects dir 260rm -rf $objDir 261 262 263echo "binutils and gcc for cross compilation have been built successfully!" 264