151 lines
4.5 KiB
Bash
Executable File
151 lines
4.5 KiB
Bash
Executable File
#!/bin/bash
|
||
set -e
|
||
set -x
|
||
|
||
KERNEL_VERSION='5.10'
|
||
BINUTILS_VERSION='2.35.2'
|
||
GCC_VERSION='10.3.1-20210703'
|
||
GLIBC_VERSION='2.32-alt4'
|
||
|
||
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'`"
|
||
|
||
|
||
TARGET=aarch64-linux-gnu
|
||
|
||
GCC_MAJOR_VERSION="${GCC_VERSION%%.*}"
|
||
|
||
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"
|
||
|
||
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"
|
||
export PATH="$INSTALLDIR/bin:/usr/lib/ccache:$PATH"
|
||
unset LD_LIBRARY_PATH
|
||
|
||
mkdir -p -m755 \
|
||
"$BUILDDIR/obj_binutils" \
|
||
"$BUILDDIR/obj_gcc" \
|
||
"$BUILDDIR/obj_glibc" \
|
||
"$BUILDDIR/obj_linux" \
|
||
|
||
make -j$(nproc) \
|
||
-C "$SRCDIR/linux" \
|
||
O="$BUILDDIR/obj_linux" \
|
||
ARCH=arm64 \
|
||
INSTALL_HDR_PATH="$SYSROOTDIR/usr" \
|
||
headers_install
|
||
|
||
# binutils
|
||
|
||
cd "$BUILDDIR/obj_binutils"
|
||
../../src/binutils/configure \
|
||
--target="$TARGET" \
|
||
--prefix= \
|
||
--with-sysroot="$SYSROOT" \
|
||
--with-build-sysroot="$SYSROOTDIR" \
|
||
--disable-multilib
|
||
|
||
make -j$(nproc)
|
||
make -j$(nproc) install DESTDIR="$INSTALLDIR"
|
||
|
||
# gcc
|
||
# 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
|
||
|
||
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
|
||
|
||
# compiler only
|
||
make -j$(nproc) all-gcc
|
||
make -j$(nproc) install-gcc DESTDIR="$INSTALLDIR"
|
||
|
||
|
||
# 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
|
||
|
||
# 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"
|
||
|
||
# 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"
|