1#!/bin/sh 2# 3# parameters <machine> <haiku sourcedir> <buildtools dir> <install dir> 4 5# get and check the parameters 6if [ $# -lt 4 ]; then 7 echo Usage: $0 '<machine> <haiku sourcedir> <buildtools dir>' \ 8 '<install dir> [extra make flags]' >&2 9 exit 1 10fi 11 12set -e 13 14if [ -z "$MAKE" ]; then 15 echo "MAKE undefined. Assuming make." 16 export MAKE=make 17fi 18 19if [ -z "$HAIKU_USE_GCC_GRAPHITE" ]; then 20 echo "HAIKU_USE_GCC_GRAPHITE undefined. Assuming false" 21 export HAIKU_USE_GCC_GRAPHITE=0 22fi 23 24haikuMachine=$1 25haikuSourceDir=$2 26buildToolsDir=$3 27installDir=$4 28shift 4 29additionalMakeArgs=$* 30gdbSourceDir="$HAIKU_USE_GDB" 31 32ccFlags="-O2" 33cxxFlags="-O2" 34binutilsTargets="$haikuMachine" 35gdbTarget="$haikuMachine" 36case $haikuMachine in 37i586-*) 38 binutilsConfigureArgs="--disable-multilib" 39 gccConfigureArgs="--disable-multilib" 40 binutilsTargets="$binutilsTargets,i386-efi-pe,x86_64-efi-pe" 41 gdbConfigureArgs="--disable-multilib" 42 ;; 43x86_64-*) 44 # GCC's default is to enable multilib, but there is a bug when 45 # explicitly using --enable-multilib that causes a build 46 # failure 47 binutilsConfigureArgs="" 48 binutilsTargets="$binutilsTargets,i386-efi-pe,x86_64-efi-pe" 49 gdbConfigureArgs="--disable-multilib" 50 ;; 51m68k-*) 52 binutilsConfigureArgs="--enable-multilib" 53 gccConfigureArgs="--enable-multilib" 54 gdbConfigureArgs="--disable-multilib" 55 gdbTarget="m68k-unknown-elf" 56 ;; 57arm-*) 58 # Multilib creates a lot of confusion as the wrong libs end up being linked. 59 # For now, target Cortex-A8 devices. 60 binutilsConfigureArgs="--disable-multilib --with-float=hard 61 --with-cpu=cortex-a8 --with-fpu=vfpv3" 62 gccConfigureArgs="--disable-multilib --with-float=hard 63 --with-cpu=cortex-a8 --with-fpu=vfpv3" 64 65 # TODO: Disable building with TLS support for ARM until implemented. 66 binutilsConfigureArgs="$binutilsConfigureArgs --disable-tls" 67 gccConfigureArgs="$gccConfigureArgs --disable-tls" 68 gdbConfigureArgs="--disable-multilib --disable-werror -enable-interwork" 69 gdbTarget="arm-unknown-elf" 70 ;; 71riscv*-*) 72 binutilsConfigureArgs="--disable-multilib" 73 gccConfigureArgs="--disable-multilib" 74 gdbConfigureArgs="--disable-multilib" 75 76 # TODO: Disable building with TLS support for riscv until implemented. 77 binutilsConfigureArgs="$binutilsConfigureArgs --disable-tls" 78 gccConfigureArgs="$gccConfigureArgs --disable-tls" 79 ;; 80powerpc-*) 81 binutilsConfigureArgs="--disable-multilib" 82 gccConfigureArgs="--disable-multilib" 83 binutilsTargets="$binutilsTargets,powerpc-apple-linux,powerpc-apple-freebsd,powerpc-apple-vxworks" 84 gdbConfigureArgs="--disable-multilib --disable-werror" 85 gdbTarget="powerpc-unknown-elf" 86 87 # TODO: Disable building with TLS support for PPC until implemented. 88 binutilsConfigureArgs="$binutilsConfigureArgs --disable-tls" 89 gccConfigureArgs="$gccConfigureArgs --disable-tls" 90 ;; 91*) 92 binutilsConfigureArgs="--disable-multilib" 93 gccConfigureArgs="--disable-multilib" 94 gdbConfigureArgs="--disable-multilib" 95 ;; 96esac 97 98if [ `uname -s` = 'Haiku' ]; then 99 # force cross-build if building on Haiku: 100 buildhostMachine=${haikuMachine}_buildhost 101 buildHostSpec="--build=$buildhostMachine --host=$buildhostMachine" 102fi 103 104if [ ! -d "$haikuSourceDir" ]; then 105 echo "No such directory: \"$haikuSourceDir\"" >&2 106 exit 1 107fi 108 109if [ ! -d "$buildToolsDir" ]; then 110 echo "No such directory: \"$buildToolsDir\"" >&2 111 exit 1 112fi 113 114if [ -n "$gdbSourceDir" -a ! -d "$gdbSourceDir" ]; then 115 echo "No such directory: \"$gdbSourceDir\"" >&2 116 exit 1 117fi 118 119 120# create the output dir 121mkdir -p "$installDir" || exit 1 122 123 124# get absolute paths 125currentDir=$(pwd) 126 127cd "$haikuSourceDir" 128haikuSourceDir=$(pwd) 129cd "$currentDir" 130 131cd "$buildToolsDir" 132buildToolsDir=$(pwd) 133cd "$currentDir" 134 135cd "$installDir" 136installDir=$(pwd) 137cd "$currentDir" 138 139if [ -n "$gdbSourceDir" ]; then 140cd "$gdbSourceDir" 141gdbSourceDir=$(pwd) 142cd "$currentDir" 143fi 144 145binutilsSourceDir="$buildToolsDir/binutils" 146gccSourceDir="$buildToolsDir/gcc" 147 148 149# get gcc version 150gccVersion=$(cat "$gccSourceDir/gcc/BASE-VER") 151 152if [ -z "$gccVersion" ]; then 153 echo "Failed to find out gcc version." >&2 154 exit 1 155fi 156 157# clear out the "missing" scripts so that they don't cause errors on tools 158# that are not installed, as we will have committed any files generated by 159# them to the buildtools repo already. 160echo "#!/bin/sh" >"$binutilsSourceDir"/missing 161echo "#!/bin/sh" >"$gccSourceDir"/missing 162echo "#!/bin/sh" >"$gccSourceDir"/isl/missing 163 164# create the object and installation directories for the cross compilation tools 165objDir="${installDir}-build" 166binutilsObjDir="$objDir/binutils" 167gccObjDir="$objDir/gcc" 168gdbObjDir="$objDir/gdb" 169stdcxxObjDir="$objDir/stdcxx" 170sysrootDir="$installDir/sysroot" 171tmpIncludeDir="$sysrootDir/boot/system/develop/headers" 172tmpLibDir="$sysrootDir/boot/system/develop/lib" 173 174rm -rf "$installDir" "$objDir" 175 176mkdir -p "$installDir" "$objDir" "$binutilsObjDir" "$gccObjDir" "$gdbObjDir" \ 177 "$stdcxxObjDir" "$tmpIncludeDir" "$tmpLibDir" || exit 1 178mkdir -p "$installDir/lib/gcc/$haikuMachine/$gccVersion" 179 180if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then 181 cloogSourceDir="$buildToolsDir/cloog" 182 gmpSourceDir="$buildToolsDir/gcc/gmp" 183 islSourceDir="$buildToolsDir/isl" 184 185 islObjDir="$objDir/isl" 186 gmpObjDir="$objDir/gmp" 187 cloogObjDir="$objDir/cloog" 188 mkdir -p "$islObjDir" "$gmpObjDir" "$cloogObjDir" || exit 1 189 190 gccConfigureArgs="$gccConfigureArgs --with-cloog=$installDir \ 191 --enable-cloog-backend=isl --with-isl=$installDir \ 192 --with-gmp=$installDir \ 193 --with-host-libstdcxx=\"-lstdc++\"" 194fi 195if [ "$HAIKU_USE_GCC_PIPE" = 1 ]; then 196 ccFlags="$ccFlags -pipe" 197 cxxFlags="$cxxFlags -pipe" 198fi 199 200if [ -n "$SECONDARY_ARCH" ]; then 201 gccConfigureArgs="$gccConfigureArgs --with-hybrid-secondary=$SECONDARY_ARCH" 202fi 203 204# force the POSIX locale, as the build (makeinfo) might choke otherwise 205export LC_ALL=POSIX 206 207# build gdb 208if [ -n "$HAIKU_USE_GDB" ]; then 209cd "$gdbObjDir" 210 "$gdbSourceDir/configure" \ 211 --prefix="$installDir" --target=$gdbTarget \ 212 $gdbConfigureArgs \ 213 || exit 1 214 $MAKE $additionalMakeArgs || exit 1 215 $MAKE $additionalMakeArgs install || exit 1 216fi 217 218# build binutils 219cd "$binutilsObjDir" 220CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$binutilsSourceDir/configure" \ 221 --prefix="$installDir" $buildHostSpec --target=$haikuMachine \ 222 --enable-targets=$binutilsTargets \ 223 --disable-nls --disable-shared --disable-werror \ 224 --with-sysroot="$sysrootDir" \ 225 --disable-maintainer-mode \ 226 $binutilsConfigureArgs \ 227 || exit 1 228$MAKE $additionalMakeArgs || exit 1 229$MAKE $additionalMakeArgs install || exit 1 230 231export PATH="$PATH:$installDir/bin" 232 233if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then 234 # build gmp 235 cd "$gmpObjDir" 236 CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gmpSourceDir/configure" \ 237 --prefix="$installDir" --disable-shared --enable-cxx || exit 1 238 $MAKE $additionalMakeArgs || exit 1 239 $MAKE $additionalMakeArgs install || exit 1 240 241 # build isl 242 cd "$islObjDir" 243 CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$islSourceDir/configure" \ 244 --prefix="$installDir" --disable-nls --disable-shared \ 245 --with-gmp-prefix="$installDir" || exit 1 246 $MAKE $additionalMakeArgs || exit 1 247 $MAKE $additionalMakeArgs install || exit 1 248 249 # build cloog 250 cd "$cloogObjDir" 251 CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$cloogSourceDir/configure" \ 252 --prefix="$installDir" --disable-nls --disable-shared \ 253 --with-gmp-prefix="$installDir" --with-isl-prefix=="$installDir" \ 254 || exit 1 255 $MAKE $additionalMakeArgs || exit 1 256 $MAKE $additionalMakeArgs install || exit 1 257fi 258 259# build gcc 260 261# prepare the include files 262copy_headers() 263{ 264 sourceDir=$1 265 targetDir=$2 266 267 headers="$(find $sourceDir -name \*\.h)" 268 headers="$(echo $headers | sed -e s@$sourceDir/@@g)" 269 for f in $headers; do 270 headerTargetDir="$targetDir/$(dirname $f)" 271 mkdir -p "$headerTargetDir" 272 cp "$sourceDir/$f" "$headerTargetDir" 273 done 274} 275 276copy_headers "$haikuSourceDir/headers/config" "$tmpIncludeDir/config" 277copy_headers "$haikuSourceDir/headers/os" "$tmpIncludeDir/os" 278copy_headers "$haikuSourceDir/headers/posix" "$tmpIncludeDir/posix" 279 280# configure gcc 281cd "$gccObjDir" 282CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gccSourceDir/configure" \ 283 --prefix="$installDir" $buildHostSpec --target=$haikuMachine \ 284 --disable-nls --disable-shared --with-system-zlib \ 285 --enable-languages=c,c++ --enable-lto --enable-frame-pointer \ 286 --enable-__cxa-atexit --enable-threads=posix \ 287 --with-default-libstdcxx-abi=gcc4-compatible \ 288 --with-sysroot="$sysrootDir" \ 289 --disable-maintainer-mode \ 290 $gccConfigureArgs \ 291 || exit 1 292 293# make gcc 294$MAKE $additionalMakeArgs || { 295 echo "ERROR: Building gcc failed." >&2 296 exit 1 297} 298 299# install gcc 300$MAKE $additionalMakeArgs install || { 301 echo "ERROR: Installing the cross compiler failed." >&2 302 exit 1 303} 304 305case $haikuMachine in 306x86_64-*) 307 # pick up the 32-bit libraries for the bootloader 308 bootLibgcc=`$installDir/bin/$haikuMachine-gcc -m32 -print-file-name=libgcc.a` 309 $installDir/bin/$haikuMachine-strip --strip-debug $bootLibgcc 310 bootLibsupcxx=`$installDir/bin/$haikuMachine-gcc -m32 -print-file-name=libsupc++.a` 311 $installDir/bin/$haikuMachine-strip --strip-debug $bootLibsupcxx 312 ;; 313esac 314 315# cleanup 316 317# remove the system headers from the installation dir 318# Only the ones from the source tree should be used. 319rm -rf "$installDir/$haikuMachine/sys-include" 320 321# rename the static libstdc++ to prevent accidental usage 322# (it should be used by the bootstrap process only) 323mv "$installDir/$haikuMachine/lib/libstdc++.a" \ 324 "$installDir/$haikuMachine/lib/libstdc++-static.a" 325 326# remove the sysroot dir 327rm -rf "$sysrootDir" 328 329# remove the objects dir 330rm -rf "$objDir" 331 332 333echo "binutils and gcc for cross compilation have been built successfully!" 334