2021-06-07 12:54:02 +03:00
#!/bin/bash
set -e
set -x
2022-06-05 18:48:06 +03:00
TARGET = aarch64-linux-gnu
2021-06-07 12:54:02 +03:00
2021-07-29 18:55:36 +03:00
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'`"
2021-06-07 12:54:02 +03:00
2022-06-05 18:48:06 +03:00
getversion( ) {
local fname = " ${ 1 ##*/ } "
local bname = " ${ fname %.tar* } "
local ver = " ${ bname #*- } "
echo " $ver "
}
2021-06-07 12:54:02 +03:00
2022-06-05 18:48:06 +03:00
GCC_VERSION = ` getversion $GCC_SOURCE `
2021-06-09 16:36:23 +03:00
GCC_MAJOR_VERSION = " ${ GCC_VERSION %%.* } "
2021-06-07 12:54:02 +03:00
BUILDDIR = "`pwd`/build"
SRCDIR = "`pwd`/src"
INSTALLDIR = " `pwd`/inst/altcross-gcc- ${ GCC_VERSION } "
SYSROOT = /$TARGET /libc
SYSROOTDIR = " $INSTALLDIR / $SYSROOT "
mkdir -p -m755 " $SRCDIR "
cd " $SRCDIR "
2021-07-29 18:55:36 +03:00
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
2021-06-07 12:54:02 +03:00
2021-07-29 18:55:36 +03:00
cd "gcc"
2021-06-07 12:54:02 +03:00
sed -i contrib/download_prerequisites -e '/base_url=/s/ftp/http/'
./contrib/download_prerequisites
mkdir -p -m755 " $INSTALLDIR /bin "
2021-06-09 16:36:36 +03:00
export PATH = " $INSTALLDIR /bin:/usr/lib/ccache: $PATH "
2021-06-07 12:54:02 +03:00
unset LD_LIBRARY_PATH
mkdir -p -m755 \
" $BUILDDIR /obj_binutils " \
" $BUILDDIR /obj_gcc " \
" $BUILDDIR /obj_glibc " \
2021-07-29 18:55:36 +03:00
" $BUILDDIR /obj_linux " \
2021-06-07 12:54:02 +03:00
make -j$( nproc) \
2021-07-29 18:55:36 +03:00
-C " $SRCDIR /linux " \
O = " $BUILDDIR /obj_linux " \
2021-06-07 12:54:02 +03:00
ARCH = arm64 \
INSTALL_HDR_PATH = " $SYSROOTDIR /usr " \
headers_install
# binutils
cd " $BUILDDIR /obj_binutils "
2021-07-29 18:55:36 +03:00
../../src/binutils/configure \
2021-06-07 12:54:02 +03:00
--target= " $TARGET " \
--prefix= \
--with-sysroot= " $SYSROOT " \
--with-build-sysroot= " $SYSROOTDIR " \
--disable-multilib
make -j$( nproc)
make -j$( nproc) install DESTDIR = " $INSTALLDIR "
# gcc
2021-07-29 20:11:56 +03:00
# Disable libsanitizer to avoid the build failure:
#
# ../../../../../src/gcc/libsanitizer/asan/asan_linux.cpp: In function ‘ void __asan::AsanCheckIncompatibleRT()’ :
# ../../../../../src/gcc/libsanitizer/asan/asan_linux.cpp:217:21: error: ‘ PATH_MAX’ was not declared in this scope
# 217 | char filename[PATH_MAX];
# | ^~~~~~~~
# ../../../../../src/gcc/libsanitizer/asan/asan_linux.cpp:218:35: error: ‘ filename’ was not declared in this scope; did you mean ‘ fileno’ ?
# 218 | MemoryMappedSegment segment(filename, sizeof(filename));
# | ^~~~~~~~
# | fileno
2021-06-07 12:54:02 +03:00
cd " $BUILDDIR /obj_gcc "
2021-07-29 18:55:36 +03:00
../../src/gcc/configure \
2021-06-07 12:54:02 +03:00
--prefix= \
--target= $TARGET \
--enable-languages= c,c++ \
2021-06-09 16:36:23 +03:00
--enable-version-specific-runtime-libs \
2021-06-07 12:54:02 +03:00
--with-sysroot= " $SYSROOT " \
--with-build-sysroot= " $SYSROOTDIR " \
2021-06-07 17:26:25 +03:00
--with-gcc-major-version-only \
2021-07-29 20:11:56 +03:00
--disable-libsanitizer \
2021-06-07 12:54:02 +03:00
--disable-multilib
# compiler only
make -j$( nproc) all-gcc
make -j$( nproc) install-gcc DESTDIR = " $INSTALLDIR "
# glibc
cd " $BUILDDIR /obj_glibc "
2021-07-29 18:55:36 +03:00
../../src/glibc/configure \
2021-06-07 12:54:02 +03:00
--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
# glibc: headers + C runtime bits
make -j$( nproc) install-bootstrap-headers= yes install-headers DESTDIR = " $SYSROOTDIR "
make -j$( nproc) csu/subdir_lib
install -d -m755 " $SYSROOTDIR " /usr/lib
install csu/crt1.o csu/crti.o csu/crtn.o " $SYSROOTDIR " /usr/lib
$TARGET -gcc -nostdlib -nostartfiles -shared -x c /dev/null -o " $SYSROOTDIR " /usr/lib/libc.so
touch " $SYSROOTDIR " /usr/include/gnu/stubs.h " $SYSROOTDIR " /usr/include/bits/stdio_lim.h
# GCC: runtime (libgcc)
cd " $BUILDDIR /obj_gcc "
make -j$( nproc) all-target-libgcc
make -j$( nproc) install-target-libgcc DESTDIR = " $INSTALLDIR "
# finish off glibc
cd " $BUILDDIR /obj_glibc "
make -j$( nproc)
make install DESTDIR = " $SYSROOTDIR "
# finish off GCC
cd " $BUILDDIR /obj_gcc "
make -j$( nproc)
make -j$( nproc) install DESTDIR = " $INSTALLDIR "
2021-06-09 16:36:23 +03:00
2021-06-10 14:33:12 +03:00
# 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
2021-06-11 19:08:28 +03:00
# 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
2021-07-29 20:12:21 +03:00
sed -e " s; objcopy ; ../../../../bin/ ${ TARGET } -objcopy ; " -i $BUILDDIR /specs
2021-06-11 19:08:28 +03:00
2021-06-10 14:33:12 +03:00
mv $BUILDDIR /specs " $INSTALLDIR /lib/gcc/ $TARGET / ${ GCC_MAJOR_VERSION } / "
2021-06-09 16:36:23 +03:00
# 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 "