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