1#!/bin/sh 2# 3# parameters <machine> <haiku sourcedir> <buildtools dir> <isntall 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 12haikuMachine=$1 13haikuSourceDir=$2 14buildToolsDir=$3 15installDir=$4 16shift 4 17additionalMakeArgs=$* 18 19case $haikuMachine in 20x86_64-*) 21 # GCC's default is to enable multilib, but there is a bug when 22 # explicitly using --enable-multilib that causes a build 23 # failure 24 binutilsConfigureArgs="" 25 gccConfigureArgs="" 26 kernelCcFlags="-mno-red-zone" 27 ;; 28m68k-*) 29 binutilsConfigureArgs="--enable-multilib" 30 gccConfigureArgs="--enable-multilib" 31 ;; 32arm-*) 33 binutilsConfigureArgs="--enable-multilib" 34 gccConfigureArgs="--enable-multilib" 35 ;; 36*) 37 binutilsConfigureArgs="--disable-multilib" 38 gccConfigureArgs="--disable-multilib" 39 kernelCcFlags= 40 ;; 41esac 42 43if [ `uname -s` = 'Haiku' ]; then 44 # force cross-build if building on Haiku: 45 buildhostMachine=${haikuMachine}_buildhost 46 buildHostSpec="--build=$buildhostMachine --host=$buildhostMachine" 47fi 48 49if [ ! -d "$haikuSourceDir" ]; then 50 echo "No such directory: \"$haikuSourceDir\"" >&2 51 exit 1 52fi 53 54if [ ! -d "$buildToolsDir" ]; then 55 echo "No such directory: \"$buildToolsDir\"" >&2 56 exit 1 57fi 58 59 60# create the output dir 61mkdir -p "$installDir" || exit 1 62 63 64# get absolute paths 65currentDir=$(pwd) 66 67cd "$haikuSourceDir" 68haikuSourceDir=$(pwd) 69cd "$currentDir" 70 71cd "$buildToolsDir" 72buildToolsDir=$(pwd) 73cd "$currentDir" 74 75cd "$installDir" 76installDir=$(pwd) 77cd "$currentDir" 78 79binutilsSourceDir="$buildToolsDir/binutils" 80gccSourceDir="$buildToolsDir/gcc" 81 82 83# get gcc version 84gccVersion=$(cat "$gccSourceDir/gcc/BASE-VER") 85 86if [ -z "$gccVersion" ]; then 87 echo "Failed to find out gcc version." >&2 88 exit 1 89fi 90 91# touch all info files in order to avoid the dependency on makeinfo 92# (which apparently doesn't work reliably on all the different host 93# configurations and changes files which in turn appear as local changes 94# to the VCS). 95find "$binutilsSourceDir" "$gccSourceDir" -name \*.info -print0 | xargs -0 touch 96 97# create the object and installation directories for the cross compilation tools 98objDir="${installDir}-build" 99binutilsObjDir="$objDir/binutils" 100gccObjDir="$objDir/gcc" 101stdcxxObjDir="$objDir/stdcxx" 102sysrootDir="$installDir/sysroot" 103tmpIncludeDir="$sysrootDir/boot/system/develop/headers" 104tmpLibDir="$sysrootDir/boot/system/develop/lib" 105 106rm -rf "$installDir" "$objDir" 107 108mkdir -p "$installDir" "$objDir" "$binutilsObjDir" "$gccObjDir" "$stdcxxObjDir" \ 109 "$tmpIncludeDir" "$tmpLibDir" || exit 1 110mkdir -p "$installDir/lib/gcc/$haikuMachine/$gccVersion" 111 112if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then 113 cloogSourceDir="$buildToolsDir/cloog" 114 gmpSourceDir="$buildToolsDir/gcc/gmp" 115 pplSourceDir="$buildToolsDir/ppl" 116 117 pplObjDir="$objDir/ppl" 118 gmpObjDir="$objDir/gmp" 119 cloogObjDir="$objDir/cloog" 120 mkdir -p "$pplObjDir" "$gmpObjDir" "$cloogObjDir" || exit 1 121 122 gccConfigureArgs="$gccConfigureArgs --with-cloog=\"$installDir\" \ 123 --enable-cloog-backend=isl --with-ppl=\"$installDir\" \ 124 --disable-cloog-version-check --with-gmp=\"$installDir\" \ 125 --with-host-libstdcxx=\"-lstdc++\"" 126fi 127 128if [ -n "$SECONDARY_ARCH" ]; then 129 gccConfigureArgs="$gccConfigureArgs --with-hybrid-secondary=$SECONDARY_ARCH" 130fi 131 132# force the POSIX locale, as the build (makeinfo) might choke otherwise 133export LC_ALL=POSIX 134 135# build binutils 136cd "$binutilsObjDir" 137CFLAGS="-O2" CXXFLAGS="-O2" "$binutilsSourceDir/configure" \ 138 --prefix="$installDir" $buildHostSpec --target=$haikuMachine \ 139 --enable-targets=$haikuMachine,i386-efi-pe,x86_64-efi-pe \ 140 --disable-nls --disable-shared --disable-werror \ 141 --with-sysroot="$sysrootDir" \ 142 $binutilsConfigureArgs \ 143 || exit 1 144$MAKE $additionalMakeArgs || exit 1 145$MAKE $additionalMakeArgs install || exit 1 146 147export PATH=$PATH:"$installDir/bin" 148 149if [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then 150 # build gmp 151 cd "$gmpObjDir" 152 "$gmpSourceDir/configure" --prefix="$installDir" \ 153 --disable-shared --enable-cxx || exit 1 154 $MAKE $additionalMakeArgs || exit 1 155 $MAKE $additionalMakeArgs install || exit 1 156 157 # build ppl 158 cd "$pplObjDir" 159 CFLAGS="-O2" CXXFLAGS="-O2" "$pplSourceDir/configure" --prefix="$installDir" \ 160 --disable-nls --disable-shared --disable-watchdog \ 161 --disable-maintainer-mode || exit 1 162 $MAKE $additionalMakeArgs AUTOCONF:=true AUTOHEADER:=true ACLOCAL:=true \ 163 AUTOMAKE:=true || exit 1 164 $MAKE $additionalMakeArgs install AUTOCONF:=true AUTOHEADER:=true \ 165 ACLOCAL:=true AUTOMAKE:=true || exit 1 166 167 # build cloog 168 cd "$cloogObjDir" 169 CFLAGS="-O2" CXXFLAGS="-O2" "$cloogSourceDir/configure" \ 170 --prefix="$installDir" --disable-nls --disable-shared \ 171 --with-gmp-prefix="$installDir" || exit 1 172 $MAKE $additionalMakeArgs || exit 1 173 $MAKE $additionalMakeArgs install || exit 1 174fi 175 176# build gcc 177 178# prepare the include files 179copy_headers() 180{ 181 sourceDir=$1 182 targetDir=$2 183 184 headers="$(find $sourceDir -name \*\.h)" 185 headers="$(echo $headers | sed -e s@$sourceDir/@@g)" 186 for f in $headers; do 187 headerTargetDir="$targetDir/$(dirname $f)" 188 mkdir -p "$headerTargetDir" 189 cp "$sourceDir/$f" "$headerTargetDir" 190 done 191} 192 193copy_headers "$haikuSourceDir/headers/config" "$tmpIncludeDir/config" 194copy_headers "$haikuSourceDir/headers/os" "$tmpIncludeDir/os" 195copy_headers "$haikuSourceDir/headers/posix" "$tmpIncludeDir/posix" 196 197# configure gcc 198cd "$gccObjDir" 199CFLAGS="-O2" CXXFLAGS="-O2" "$gccSourceDir/configure" \ 200 --prefix="$installDir" $buildHostSpec --target=$haikuMachine \ 201 --disable-nls --disable-shared --with-system-zlib \ 202 --enable-languages=c,c++ --enable-lto --enable-frame-pointer \ 203 --with-sysroot="$sysrootDir" \ 204 $gccConfigureArgs \ 205 || exit 1 206 207# make gcc 208$MAKE $additionalMakeArgs || { 209 echo "ERROR: Building gcc failed." >&2 210 exit 1 211} 212 213# install gcc 214$MAKE $additionalMakeArgs install || { 215 echo "ERROR: Installing the cross compiler failed." >&2 216 exit 1 217} 218 219# build libraries for the kernel if the target arch requires it 220if [ -n "$kernelCcFlags" ]; then 221 $MAKE -C "$haikuMachine/libgcc" clean 222 $MAKE -C "$haikuMachine/libgcc" CFLAGS="-g -O2 $kernelCcFlags" || { 223 echo "Error: Building kernel libgcc failed." >&2 224 exit 1 225 } 226 227 cp "$haikuMachine/libgcc/libgcc.a" \ 228 "$installDir/$haikuMachine/lib/libgcc-kernel.a" || exit 1 229 230 $MAKE -C "$haikuMachine/libstdc++-v3/libsupc++" clean 231 $MAKE -C "$haikuMachine/libstdc++-v3/libsupc++" CFLAGS="-g -O2 $kernelCcFlags" \ 232 CXXFLAGS="-g -O2 $kernelCcFlags" || { 233 echo "Error: Building kernel libsupc++ failed." >&2 234 exit 1 235 } 236 237 cp "$haikuMachine/libstdc++-v3/libsupc++/.libs/libsupc++.a" \ 238 "$installDir/$haikuMachine/lib/libsupc++-kernel.a" || exit 1 239fi 240 241# cleanup 242 243# remove the system headers from the installation dir 244# Only the ones from the source tree should be used. 245rm -rf "$installDir/$haikuMachine/sys-include" 246 247# remove the sysroot dir 248rm -rf "$sysrootDir" 249 250# remove the objects dir 251rm -rf "$objDir" 252 253 254echo "binutils and gcc for cross compilation have been built successfully!" 255