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