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