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