2022-06-05 14:02:39 +03:00
#!/bin/bash
set -e
set -x
TARGET = aarch64-linux-gnu
2022-06-05 22:36:48 +03:00
ARCH = aarch64
LINUX_ARCH = arm64
QEMU_ARCH = aarch64
2022-06-05 14:02:39 +03:00
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 " \
2022-06-05 22:36:48 +03:00
ARCH = ${ LINUX_ARCH } \
2022-06-05 14:02:39 +03:00
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 }
2022-06-05 22:36:48 +03:00
make -j$( nproc)
2022-06-05 14:02:39 +03:00
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 "
2022-06-05 16:50:08 +03:00
2022-06-05 22:36:48 +03:00
$INSTALLDIR /bin/${ TARGET } -gcc -o ${ BUILDDIR } /hello_c hello.c || exit 2
$INSTALLDIR /bin/${ TARGET } -g++ -o ${ BUILDDIR } /hello_cpp hello.cpp || exit 3
$INSTALLDIR /bin/${ TARGET } -gcc -o ${ BUILDDIR } /bye_asm -static -nostdlib bye-${ LINUX_ARCH } .S || exit 11
2022-06-05 16:50:08 +03:00
2022-06-05 22:36:48 +03:00
gcc_runtime_libdir = ` $INSTALLDIR /bin/${ TARGET } -gcc --print-libgcc-file-name`
2022-06-05 16:50:08 +03:00
gcc_runtime_libdir = " ${ gcc_runtime_libdir %/* } "
2022-06-05 22:36:48 +03:00
if command -v qemu-${ QEMU_ARCH } -static; then
# run test binaries
env LD_LIBRARY_PATH = $INSTALLDIR ${ SYSROOT } /lib64:${ gcc_runtime_libdir } \
qemu-${ QEMU_ARCH } -static -L ${ INSTALLDIR } ${ SYSROOT } ${ BUILDDIR } /hello_c || exit 5
env LD_LIBRARY_PATH = $INSTALLDIR ${ SYSROOT } /lib64:${ gcc_runtime_libdir } \
qemu-${ QEMU_ARCH } -static -L ${ INSTALLDIR } ${ SYSROOT } ${ BUILDDIR } /hello_cpp || exit 7
qemu-${ QEMU_ARCH } -static ${ BUILDDIR } /bye_asm || exit 13
fi