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 19ccFlags="-O2" 20cxxFlags="-O2" 21case $haikuMachine in 22x86_64-*) 23 # GCC's default is to enable multilib, but there is a bug when 24 # explicitly using --enable-multilib that causes a build 25 # failure 26 binutilsConfigureArgs="" 27 ;; 28m68k-*) 29 binutilsConfigureArgs="--enable-multilib" 30 gccConfigureArgs="--enable-multilib" 31 ;; 32arm-*) 33 # Multilib creates a lot of confusion as the wrong libs end up being linked. 34 # For now, target Cortex-A8 devices. 35 binutilsConfigureArgs="--disable-multilib --with-float=hard 36 --with-cpu=cortex-a8 --with-arch=armv7-a --with-fpu=vfpv3" 37 gccConfigureArgs="--disable-multilib --with-float=hard 38 --with-cpu=cortex-a8 --with-arch=armv7-a --with-fpu=vfpv3" 39 40 # TODO: Disable building with TLS support for ARM until implemented. 41 binutilsConfigureArgs="$binutilsConfigureArgs --disable-tls" 42 gccConfigureArgs="$gccConfigureArgs --disable-tls" 43 ;; 44*) 45 binutilsConfigureArgs="--disable-multilib" 46 gccConfigureArgs="--disable-multilib" 47 ;; 48esac 49 50if [ `uname -s` = 'Haiku' ]; then 51 # force cross-build if building on Haiku: 52 buildhostMachine=${haikuMachine}_buildhost 53 buildHostSpec="--build=$buildhostMachine --host=$buildhostMachine" 54fi 55 56if [ ! -d "$haikuSourceDir" ]; then 57 echo "No such directory: \"$haikuSourceDir\"" >&2 58 exit 1 59fi 60 61if [ ! -d "$buildToolsDir" ]; then 62 echo "No such directory: \"$buildToolsDir\"" >&2 63 exit 1 64fi 65 66 67# create the output dir 68mkdir -p "$installDir" || exit 1 69 70 71# get absolute paths 72currentDir=$(pwd) 73 74cd "$haikuSourceDir" 75haikuSourceDir=$(pwd) 76cd "$currentDir" 77 78cd "$buildToolsDir" 79buildToolsDir=$(pwd) 80cd "$currentDir" 81 82cd "$installDir" 83installDir=$(pwd) 84cd "$currentDir" 85 86binutilsSourceDir="$buildToolsDir/binutils" 87gccSourceDir="$buildToolsDir/gcc" 88 89 90# get gcc version 91gccVersion=$(cat "$gccSourceDir/gcc/BASE-VER") 92 93if [ -z "$gccVersion" ]; then 94 echo "Failed to find out gcc version." >&2 95 exit 1 96fi 97 98# touch all info files in order to avoid the dependency on makeinfo 99# (which apparently doesn't work reliably on all the different host 100# configurations and changes files which in turn appear as local changes 101# to the VCS). 102find "$binutilsSourceDir" "$gccSourceDir" -name \*.info -print0 | xargs -0 touch 103 104# create the object and installation directories for the cross compilation tools 105objDir="${installDir}-build" 106binutilsObjDir="$objDir/binutils" 107gccObjDir="$objDir/gcc" 108stdcxxObjDir="$objDir/stdcxx" 109sysrootDir="$installDir/sysroot" 110tmpIncludeDir="$sysrootDir/boot/system/develop/headers" 111tmpLibDir="$sysrootDir/boot/system/develop/lib" 112 113rm -rf "$installDir" "$objDir" 114 115mkdir -p "$installDir" "$objDir" "$binutilsObjDir" "$gccObjDir" "$stdcxxObjDir" \ 116 "$tmpIncludeDir" "$tmpLibDir" || exit 1 117mkdir -p "$installDir/lib/gcc/$haikuMachine/$gccVersion" 118 119if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then 120 cloogSourceDir="$buildToolsDir/cloog" 121 gmpSourceDir="$buildToolsDir/gcc/gmp" 122 islSourceDir="$buildToolsDir/isl" 123 124 islObjDir="$objDir/isl" 125 gmpObjDir="$objDir/gmp" 126 cloogObjDir="$objDir/cloog" 127 mkdir -p "$islObjDir" "$gmpObjDir" "$cloogObjDir" || exit 1 128 129 gccConfigureArgs="$gccConfigureArgs --with-cloog=$installDir \ 130 --enable-cloog-backend=isl --with-isl=$installDir \ 131 --with-gmp=$installDir \ 132 --with-host-libstdcxx=\"-lstdc++\"" 133fi 134if [ "$HAIKU_USE_GCC_PIPE" = 1 ]; then 135 ccFlags="$ccFlags -pipe" 136 cxxFlags="$cxxFlags -pipe" 137fi 138 139if [ -n "$SECONDARY_ARCH" ]; then 140 gccConfigureArgs="$gccConfigureArgs --with-hybrid-secondary=$SECONDARY_ARCH" 141fi 142 143# force the POSIX locale, as the build (makeinfo) might choke otherwise 144export LC_ALL=POSIX 145 146# build binutils 147cd "$binutilsObjDir" 148CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$binutilsSourceDir/configure" \ 149 --prefix="$installDir" $buildHostSpec --target=$haikuMachine \ 150 --enable-targets=$haikuMachine,i386-efi-pe,x86_64-efi-pe \ 151 --disable-nls --disable-shared --disable-werror \ 152 --with-sysroot="$sysrootDir" \ 153 $binutilsConfigureArgs \ 154 || exit 1 155$MAKE $additionalMakeArgs || exit 1 156$MAKE $additionalMakeArgs install || exit 1 157 158export PATH=$PATH:"$installDir/bin" 159 160if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then 161 # build gmp 162 cd "$gmpObjDir" 163 CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gmpSourceDir/configure" \ 164 --prefix="$installDir" --disable-shared --enable-cxx || exit 1 165 $MAKE $additionalMakeArgs || exit 1 166 $MAKE $additionalMakeArgs install || exit 1 167 168 # build isl 169 cd "$islObjDir" 170 CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$islSourceDir/configure" \ 171 --prefix="$installDir" --disable-nls --disable-shared \ 172 --with-gmp-prefix="$installDir" || exit 1 173 $MAKE $additionalMakeArgs || exit 1 174 $MAKE $additionalMakeArgs install || exit 1 175 176 # build cloog 177 cd "$cloogObjDir" 178 CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$cloogSourceDir/configure" \ 179 --prefix="$installDir" --disable-nls --disable-shared \ 180 --with-gmp-prefix="$installDir" --with-isl-prefix=="$installDir" \ 181 || exit 1 182 $MAKE $additionalMakeArgs || exit 1 183 $MAKE $additionalMakeArgs install || exit 1 184fi 185 186# build gcc 187 188# prepare the include files 189copy_headers() 190{ 191 sourceDir=$1 192 targetDir=$2 193 194 headers="$(find $sourceDir -name \*\.h)" 195 headers="$(echo $headers | sed -e s@$sourceDir/@@g)" 196 for f in $headers; do 197 headerTargetDir="$targetDir/$(dirname $f)" 198 mkdir -p "$headerTargetDir" 199 cp "$sourceDir/$f" "$headerTargetDir" 200 done 201} 202 203copy_headers "$haikuSourceDir/headers/config" "$tmpIncludeDir/config" 204copy_headers "$haikuSourceDir/headers/os" "$tmpIncludeDir/os" 205copy_headers "$haikuSourceDir/headers/posix" "$tmpIncludeDir/posix" 206 207# configure gcc 208cd "$gccObjDir" 209CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gccSourceDir/configure" \ 210 --prefix="$installDir" $buildHostSpec --target=$haikuMachine \ 211 --disable-nls --disable-shared --with-system-zlib \ 212 --enable-languages=c,c++ --enable-lto --enable-frame-pointer \ 213 --with-sysroot="$sysrootDir" --disable-threads --disable-tls \ 214 $gccConfigureArgs \ 215 || exit 1 216 217# make gcc 218$MAKE $additionalMakeArgs || { 219 echo "ERROR: Building gcc failed." >&2 220 exit 1 221} 222 223# install gcc 224$MAKE $additionalMakeArgs install || { 225 echo "ERROR: Installing the cross compiler failed." >&2 226 exit 1 227} 228 229case $haikuMachine in 230x86_64-*) 231 # pick up the 32-bit libraries for the bootloader 232 bootLibgcc=`$installDir/bin/$haikuMachine-gcc -m32 -print-file-name=libgcc.a` 233 $installDir/bin/$haikuMachine-strip --strip-debug $bootLibgcc 234 bootLibsupcxx=`$installDir/bin/$haikuMachine-gcc -m32 -print-file-name=libsupc++.a` 235 $installDir/bin/$haikuMachine-strip --strip-debug $bootLibsupcxx 236 ;; 237esac 238 239# cleanup 240 241# remove the system headers from the installation dir 242# Only the ones from the source tree should be used. 243rm -rf "$installDir/$haikuMachine/sys-include" 244 245# remove the sysroot dir 246rm -rf "$sysrootDir" 247 248# remove the objects dir 249rm -rf "$objDir" 250 251 252echo "binutils and gcc for cross compilation have been built successfully!" 253