⛏️ index : haiku.git

#!/bin/sh
#
# parameters <machine> <haiku sourcedir> <buildtools dir> <install dir>

# get and check the parameters
if [ $# -lt 4 ]; then
	echo Usage: $0 '<machine> <haiku sourcedir> <buildtools dir>' \
		'<install dir> [extra make flags]' >&2
	exit 1
fi

set -e

if [ -z "$MAKE" ]; then
	echo "MAKE undefined. Assuming make."
	export MAKE=make
fi

haikuMachine=$1
haikuSourceDir=$2
buildToolsDir=$3
installDir=$4
shift 4
additionalMakeArgs=$*
gdbSourceDir="$HAIKU_USE_GDB"

ccFlags="-O2"
#CLang needs c++14 to build ISL: https://dev.haiku-os.org/ticket/16434
cxxFlags="-O2 -std=c++14"
binutilsTargets="$haikuMachine"
gdbTarget="$haikuMachine"
case $haikuMachine in
i586-*)
	binutilsConfigureArgs="--disable-multilib"
	gccConfigureArgs="--disable-multilib"
	binutilsTargets="$binutilsTargets,i386-efi-pe,x86_64-efi-pe"
	gdbConfigureArgs="--disable-multilib"
	;;
x86_64-*)
	binutilsConfigureArgs="--enable-multilib"
	gccConfigureArgs="--enable-multilib"
	binutilsTargets="$binutilsTargets,i386-efi-pe,x86_64-efi-pe"
	gdbConfigureArgs="--disable-multilib"
	;;
m68k-*)
	binutilsConfigureArgs="--enable-multilib"
	gccConfigureArgs="--enable-multilib"
	gdbConfigureArgs="--disable-multilib"
	gdbTarget="m68k-unknown-elf"
	;;
arm-*)
	# Multilib creates a lot of confusion as the wrong libs end up being linked.
	# For now, target Cortex-A8 devices.
	binutilsConfigureArgs="--disable-multilib --with-float=hard
		--with-cpu=cortex-a8 --with-fpu=vfpv3"
	gccConfigureArgs="--disable-multilib --with-float=hard
		--with-cpu=cortex-a8 --with-fpu=vfpv3"

	# TODO: Disable building with TLS support for ARM until implemented.
	binutilsConfigureArgs="$binutilsConfigureArgs --disable-tls"
	gccConfigureArgs="$gccConfigureArgs --disable-tls"
	gdbConfigureArgs="--disable-multilib --disable-werror -enable-interwork"
	gdbTarget="arm-unknown-elf"
	;;
riscv*-*)
	binutilsConfigureArgs=" --with-arch=rv64gc"
	gccConfigureArgs=" --with-arch=rv64gc"
	gdbConfigureArgs=""

	binutilsConfigureArgs="$binutilsConfigureArgs --disable-multilib"
	gccConfigureArgs="$gccConfigureArgs --disable-multilib"
	gdbConfigureArgs="$gdbConfigureArgs --disable-multilib"
	;;
powerpc-*)
	binutilsConfigureArgs="--disable-multilib"
	gccConfigureArgs="--disable-multilib"
	binutilsTargets="$binutilsTargets,powerpc-apple-linux,powerpc-apple-freebsd,powerpc-apple-vxworks"
	gdbConfigureArgs="--disable-multilib --disable-werror"
	gdbTarget="powerpc-unknown-elf"

	# TODO: Disable building with TLS support for PPC until implemented.
	binutilsConfigureArgs="$binutilsConfigureArgs --disable-tls"
	gccConfigureArgs="$gccConfigureArgs --disable-tls"
	;;
*)
	binutilsConfigureArgs="--disable-multilib"
	gccConfigureArgs="--disable-multilib"
	gdbConfigureArgs="--disable-multilib"
	;;
esac

if [ `uname -s` = 'Haiku' ]; then
	# force cross-build if building on Haiku:
	buildhostMachine=${haikuMachine}_buildhost
	buildHostSpec="--build=$buildhostMachine --host=$buildhostMachine"
fi

if [ ! -d "$haikuSourceDir" ]; then
	echo "No such directory: \"$haikuSourceDir\"" >&2
	exit 1
fi

if [ ! -d "$buildToolsDir" ]; then
	echo "No such directory: \"$buildToolsDir\"" >&2
	exit 1
fi

if [ -n "$gdbSourceDir" -a ! -d "$gdbSourceDir" ]; then
	echo "No such directory: \"$gdbSourceDir\"" >&2
	exit 1
fi


# create the output dir
mkdir -p "$installDir" || exit 1


# get absolute paths
currentDir=$(pwd)

cd "$haikuSourceDir"
haikuSourceDir=$(pwd)
cd "$currentDir"

cd "$buildToolsDir"
buildToolsDir=$(pwd)
cd "$currentDir"

cd "$installDir"
installDir=$(pwd)
cd "$currentDir"

if [ -n "$gdbSourceDir" ]; then
cd "$gdbSourceDir"
gdbSourceDir=$(pwd)
cd "$currentDir"
fi

binutilsSourceDir="$buildToolsDir/binutils"
gccSourceDir="$buildToolsDir/gcc"


# get gcc version
gccVersion=$(cat "$gccSourceDir/gcc/BASE-VER")

if [ -z "$gccVersion" ]; then
	echo "Failed to find out gcc version." >&2
	exit 1
fi

# clear out the "missing" scripts so that they don't cause errors on tools
# that are not installed, as we will have committed any files generated by
# them to the buildtools repo already.
echo "#!/bin/sh" >"$binutilsSourceDir"/missing
echo "#!/bin/sh" >"$gccSourceDir"/missing
echo "#!/bin/sh" >"$gccSourceDir"/isl/missing

# create the object and installation directories for the cross compilation tools
objDir="${installDir}-build"
binutilsObjDir="$objDir/binutils"
gccObjDir="$objDir/gcc"
gdbObjDir="$objDir/gdb"
stdcxxObjDir="$objDir/stdcxx"
sysrootDir=${HAIKU_USE_SYSROOT:-"$installDir/sysroot"}
tmpIncludeDir="$sysrootDir/boot/system/develop/headers"
tmpLibDir="$sysrootDir/boot/system/develop/lib"

rm -rf "$installDir" "$objDir"

mkdir -p "$installDir" "$objDir" "$binutilsObjDir" "$gccObjDir" "$gdbObjDir" \
	"$stdcxxObjDir" "$tmpIncludeDir" "$tmpLibDir" || exit 1
mkdir -p "$installDir/lib/gcc/$haikuMachine/$gccVersion"

if [ "$HAIKU_USE_GCC_PIPE" = 1 ]; then
	ccFlags="$ccFlags -pipe"
	cxxFlags="$cxxFlags -pipe"
fi

if [ -n "$SECONDARY_ARCH" ]; then
	gccConfigureArgs="$gccConfigureArgs --with-hybrid-secondary=$SECONDARY_ARCH"
fi

# force the POSIX locale, as the build (makeinfo) might choke otherwise
export LC_ALL=POSIX

# build gdb
if [ -n "$HAIKU_USE_GDB" ]; then
cd "$gdbObjDir"
	"$gdbSourceDir/configure" \
		--prefix="$installDir" --target=$gdbTarget \
		$gdbConfigureArgs \
		|| exit 1
	$MAKE $additionalMakeArgs || exit 1
	$MAKE $additionalMakeArgs install || exit 1
fi

# build binutils
cd "$binutilsObjDir"
CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$binutilsSourceDir/configure" \
	--prefix="$installDir" $buildHostSpec --target=$haikuMachine \
	--enable-targets=$binutilsTargets \
	--disable-nls --disable-shared --disable-werror \
	--with-sysroot="$sysrootDir" \
	--disable-maintainer-mode \
	$binutilsConfigureArgs \
	|| exit 1
$MAKE $additionalMakeArgs || exit 1
$MAKE $additionalMakeArgs install || exit 1

export PATH="$PATH:$installDir/bin"

# build gcc

# prepare the include files
copy_headers()
{
	sourceDir=$1
	targetDir=$2

	headers="$(find $sourceDir -name \*\.h)"
	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
	for f in $headers; do
		headerTargetDir="$targetDir/$(dirname $f)"
		mkdir -p "$headerTargetDir"
		cp "$sourceDir/$f" "$headerTargetDir"
	done
}

copy_headers "$haikuSourceDir/headers/config" "$tmpIncludeDir/config"
copy_headers "$haikuSourceDir/headers/os" "$tmpIncludeDir/os"
copy_headers "$haikuSourceDir/headers/posix" "$tmpIncludeDir/posix"

# configure gcc
cd "$gccObjDir"
CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gccSourceDir/configure" \
	--prefix="$installDir" $buildHostSpec --target=$haikuMachine \
	--disable-nls --disable-shared --with-system-zlib \
	--enable-languages=c,c++ --enable-lto --enable-frame-pointer \
	--enable-__cxa-atexit --enable-threads=posix \
	--with-default-libstdcxx-abi=gcc4-compatible \
	--with-sysroot="$sysrootDir" \
	--disable-maintainer-mode \
	--disable-libgomp \
	--disable-libatomic \
	$gccConfigureArgs \
	|| exit 1

# make gcc
$MAKE $additionalMakeArgs || {
	echo "ERROR: Building gcc failed." >&2
	exit 1
}

# install gcc
$MAKE $additionalMakeArgs install || {
	echo "ERROR: Installing the cross compiler failed." >&2
	exit 1
}

case $haikuMachine in
x86_64-*)
	# pick up the 32-bit libraries for the bootloader
	bootLibgcc=`$installDir/bin/$haikuMachine-gcc -m32 -print-file-name=libgcc.a`
	$installDir/bin/$haikuMachine-strip --strip-debug $bootLibgcc
	bootLibsupcxx=`$installDir/bin/$haikuMachine-gcc -m32 -print-file-name=libsupc++.a`
	$installDir/bin/$haikuMachine-strip --strip-debug $bootLibsupcxx
	;;
esac

# cleanup

# remove the system headers from the installation dir
# Only the ones from the source tree should be used.
rm -rf "$installDir/$haikuMachine/sys-include"

# rename the static libstdc++ to prevent accidental usage
# (it should be used by the bootstrap process only)
mv "$installDir/$haikuMachine/lib/libstdc++.a" \
	"$installDir/$haikuMachine/lib/libstdc++-static.a"

# remove the sysroot dir
if [ -n "$HAIKU_USE_SYSROOT" ]; then
	rm -rf "$sysrootDir"/boot/system/*
else
	rm -rf "$sysrootDir"
fi

# remove the objects dir
rm -rf "$objDir"


echo "binutils and gcc for cross compilation have been built successfully!"