cross-toolchain/altcross-2stage.sh

182 lines
4.9 KiB
Bash
Executable File

#!/bin/bash
set -e
set -x
TARGET=aarch64-linux-gnu
unset NIL
KERNEL_SOURCE="`find /usr/src/kernel/sources -type f -name 'kernel-source-*.tar'`"
BINUTILS_SOURCE="`find /usr/src/binutils-source -type f -name 'binutils-*.tar'`"
GCC_SOURCE="`find /usr/src/gcc-source -type f -name 'gcc-*.tar'`"
GLIBC_SOURCE="`find /usr/src/glibc-source -type f -name 'glibc-*.tar'`"
getversion() {
local fname="${1##*/}"
local bname="${fname%.tar*}"
local ver="${bname#*-}"
echo "$ver"
}
GCC_VERSION=`getversion $GCC_SOURCE`
GCC_MAJOR_VERSION="${GCC_VERSION%%.*}"
BUILDDIR="`pwd`/build"
SRCDIR="`pwd`/src"
INSTALLDIR="`pwd`/inst/altcross-gcc-${GCC_VERSION}"
BOOTSTRAP_INSTALLDIR="`pwd`/stage1/altcross-gcc-${GCC_VERSION}"
SYSROOT=/$TARGET/libc
SYSROOTDIR="$INSTALLDIR/$SYSROOT"
mkdir -p -m755 "$SRCDIR"
cd "$SRCDIR"
mkdir -p -m755 linux binutils gcc glibc
tar -x --strip-components=1 -f "$KERNEL_SOURCE" -C linux
tar -x --strip-components=1 -f "$BINUTILS_SOURCE" -C binutils
tar -x --strip-components=1 -f "$GCC_SOURCE" -C gcc
tar -x --strip-components=1 -f "$GLIBC_SOURCE" -C glibc
cd "gcc"
sed -i contrib/download_prerequisites -e '/base_url=/s/ftp/http/'
./contrib/download_prerequisites
mkdir -p -m755 "$INSTALLDIR/bin" "$BOOTSTRAP_INSTALLDIR/bin"
save_PATH="$PATH"
export PATH="$BOOTSTRAP_INSTALLDIR/bin:/usr/lib/ccache:$PATH"
unset LD_LIBRARY_PATH
mkdir -p -m755 \
"$BUILDDIR/obj_binutils" \
"$BUILDDIR/obj_gcc_bootstrap" \
"$BUILDDIR/obj_gcc" \
"$BUILDDIR/obj_glibc" \
"$BUILDDIR/obj_linux" \
${NIL}
# binutils
cd "$BUILDDIR/obj_binutils"
../../src/binutils/configure \
--target="$TARGET" \
--prefix= \
--with-sysroot="$SYSROOT" \
--with-build-sysroot="$SYSROOTDIR" \
--disable-multilib \
${NIL}
make -j$(nproc)
make -j$(nproc) install DESTDIR="$INSTALLDIR"
make -j$(nproc) install DESTDIR="$BOOTSTRAP_INSTALLDIR"
# bootstrap gcc
cd "$BUILDDIR/obj_gcc_bootstrap"
../../src/gcc/configure \
--prefix= \
--target=$TARGET \
--enable-languages=c \
--disable-bootstrap \
--disable-multilib \
--disable-shared \
--disable-threads \
--disable-libgomp \
--disable-libitm \
--disable-libquadmath \
--disable-libsanitizer \
--disable-libssp \
--disable-libvtv \
--disable-libatomic \
--disable-libcilkrts \
--with-newlib \
--without-headers \
--with-sysroot="$SYSROOT" \
--with-build-sysroot="$SYSROOTDIR" \
--enable-version-specific-runtime-libs \
--with-gcc-major-version-only \
${NIL}
make -j$(nproc) all-gcc all-target-libgcc
make -j$(nproc) install-gcc install-target-libgcc DESTDIR="$BOOTSTRAP_INSTALLDIR"
# kernel headers
make -j$(nproc) \
-C "$SRCDIR/linux" \
O="$BUILDDIR/obj_linux" \
ARCH=arm64 \
INSTALL_HDR_PATH="$SYSROOTDIR/usr" \
headers_install
# glibc
cd "$BUILDDIR/obj_glibc"
../../src/glibc/configure \
--prefix=/usr \
--target=$TARGET \
--host=$TARGET \
--build="$MACHTYPE" \
--with-sysroot="$SYSROOT" \
--with-build-sysroot="$SYSROOTDIR" \
--with-headers="$SYSROOTDIR"/usr/include \
--with-lib="$SYSROOTDIR"/usr/lib \
--disable-multilib \
--disable-crypt \
libc_cv_forced_unwind=yes \
${NIL}
make -j$(nproc)
make install DESTDIR="$SYSROOTDIR"
# XXX: gcc needs this to locate crt{1,i}.o
mkdir -p -m755 $SYSROOTDIR/usr/lib
export PATH="$INSTALLDIR/bin:/usr/lib/ccache:${save_PATH}"
mv "${BOOTSTRAP_INSTALLDIR}" "${BOOTSTRAP_INSTALLDIR}.save"
# gcc
# XXX: libsanitizer needs "crypt.h" and ALT Linux' glibc does NOT provide one.
# therefore disable libsanitizer for now.
cd "$BUILDDIR/obj_gcc"
../../src/gcc/configure \
--prefix= \
--target=$TARGET \
--enable-languages=c,c++ \
--enable-version-specific-runtime-libs \
--with-sysroot="$SYSROOT" \
--with-build-sysroot="$SYSROOTDIR" \
--with-gcc-major-version-only \
--disable-libsanitizer \
--disable-multilib \
${NIL}
make -j$(nproc)
make -j$(nproc) install DESTDIR="$INSTALLDIR"
# set /lib64/ld-linux-aarch64.so.1 as ELF interpreter
$INSTALLDIR/bin/${TARGET}-gcc -dumpspecs > $BUILDDIR/specs
sed -e 's;/lib/ld-linux-aarch64;/lib64/ld-linux-aarch64;g' -i $BUILDDIR/specs
# Use aarch64-linux-gnu-as instead of generic `as`
sed -e "/^[*]invoke_as:/,/^[*]cpp:/ s; as ; ../../../../bin/${TARGET}-as ;" -i $BUILDDIR/specs
# Same for objcopy
sed -e "s; objcopy ; ../../../../bin/${TARGET}-objcopy ;" -i $BUILDDIR/specs
mv $BUILDDIR/specs "$INSTALLDIR/lib/gcc/$TARGET/${GCC_MAJOR_VERSION}/"
# relocate libgcc_s
mv $INSTALLDIR/lib/gcc/$TARGET/lib64/libgcc_s.so* "$INSTALLDIR/lib/gcc/$TARGET/${GCC_MAJOR_VERSION}/"
rmdir "$INSTALLDIR/lib/gcc/$TARGET/lib64"
$INSTALLDIR/bin/${target}-gcc -o ${BUILDDIR}/hello_c hello.c || exit 2
$INSTALLDIR/bin/${target}-g++ -o ${BUILDDIR}/hello_cpp hello.cpp || exit 3
gcc_runtime_libdir=`$INSTALLDIR/bin/${target}-gcc --print-libgcc-file-name`
gcc_runtime_libdir="${gcc_runtime_libdir%/*}"
env LD_LIBRARY_PATH=$INSTALLDIR${SYSROOT}/lib64:${gcc_runtime_libdir} \
qemu-${arch}-static -L ${INSTALLDIR}${SYSROOT} ${BUILDDIR}/hello_c || exit 5
env LD_LIBRARY_PATH=$INSTALLDIR${SYSROOT}/lib64:${gcc_runtime_libdir} \
qemu-${arch}-static -L ${INSTALLDIR}${SYSROOT} ${BUILDDIR}/hello_cpp || exit 7