Skip to content

Instantly share code, notes, and snippets.

@Darchiv
Created August 9, 2020 19:09
Show Gist options
  • Save Darchiv/aadb1f0252e295a3afcd26d3fb56fdd5 to your computer and use it in GitHub Desktop.
Save Darchiv/aadb1f0252e295a3afcd26d3fb56fdd5 to your computer and use it in GitHub Desktop.
Build script for a GNU toolchain
#!/bin/bash
# What this script does:
# - downloads sources of GCC, Binutils, and GDB;
# - unpacks them;
# - configures for local static build;
# - builds;
# - packs into (primitive) .deb files.
set -e
MIRROR=""
TARGET=arm-none-eabi
BUILD_DIR="build"
DIST_DIR="dist"
JOBS=$(nproc)
GCC_VERSION=10.2.0
BINUTILS_VERSION=2.35
GDB_VERSION=9.2
GMP_VERSION=6.2.0
MPFR_VERSION=4.1.0
MPC_VERSION=1.1.0
function create_package() {
NAME=$1
VERSION=$2
DESCRIPTION=$3
pushd "$DIST_DIR/$NAME"
INSTALLED_SIZE=$(du -s -BK --apparent-size --exclude='./DEBIAN/*' . | cut -f1)
INSTALLED_SIZE=${INSTALLED_SIZE%?}
cat > ./DEBIAN/control <<- EOM
Package: $NAME-$TARGET
Version: $VERSION
Architecture: $(dpkg-architecture -q DEB_HOST_ARCH)
Maintainer: none
Installed-Size: $INSTALLED_SIZE
Description: $DESCRIPTION
EOM
find . -path ./DEBIAN -prune -o -type f -exec md5sum '{}' \; > ./DEBIAN/md5sums
dpkg-deb --build -Zxz . ..
popd
}
mkdir -p src
mkdir -p "$BUILD_DIR"/{gmp,mpfr,mpc,binutils,gcc,gdb}
mkdir -p "$DIST_DIR"/{binutils,gcc,gdb}/DEBIAN
PREFIX=$(realpath "$BUILD_DIR"/usr)
DIST_BINUTILS=$(realpath "$DIST_DIR"/binutils/usr)
DIST_GCC=$(realpath "$DIST_DIR"/gcc/usr)
DIST_GDB=$(realpath "$DIST_DIR"/gdb/usr)
pushd src
wget -nc -B "$MIRROR" -i - << EOM
gcc/gcc-$GCC_VERSION/gcc-$GCC_VERSION.tar.xz
gmp/gmp-$GMP_VERSION.tar.xz
mpfr/mpfr-$MPFR_VERSION.tar.xz
mpc/mpc-$MPC_VERSION.tar.gz
binutils/binutils-$BINUTILS_VERSION.tar.xz
gdb/gdb-$GDB_VERSION.tar.xz
EOM
for file in *.tar.*; do
tar -xf "$file" --skip-old-files 2>/dev/null
done
popd
pushd build/gmp
../../src/gmp-$GMP_VERSION/configure \
--prefix="$PREFIX" \
--disable-shared
make -j"$JOBS"
make install
popd
pushd build/mpfr
../../src/mpfr-$MPFR_VERSION/configure \
--prefix="$PREFIX" \
--disable-shared \
--with-gmp="$PREFIX"
make -j"$JOBS"
make install
popd
pushd build/mpc
../../src/mpc-$MPC_VERSION/configure \
--prefix="$PREFIX" \
--disable-shared \
--with-gmp="$PREFIX" \
--with-mpfr="$PREFIX"
make -j"$JOBS"
make install
popd
pushd build/binutils
../../src/binutils-$BINUTILS_VERSION/configure \
--target=$TARGET \
--prefix="$DIST_BINUTILS" \
--disable-libquadmath \
--disable-libstdcxx \
--disable-nls \
--disable-shared \
--enable-lto \
--with-gmp="$PREFIX" \
--with-mpfr="$PREFIX" \
--with-mpc="$PREFIX" \
--with-sysroot="$DIST_BINUTILS"/$TARGET
make -j"$JOBS"
make install-strip
popd
rm -rf "$DIST_BINUTILS/share/info"
create_package binutils $BINUTILS_VERSION "Binutils for arm-none-eabi"
pushd build/gcc
# as, ld, ar, and other binutils programs will not be found without
# at least a proper path in --with-build-time-tools
../../src/gcc-$GCC_VERSION/configure \
--target=$TARGET \
--prefix="$DIST_GCC" \
--with-sysroot="$DIST_GCC"/$TARGET \
--with-as="$DIST_BINUTILS/$TARGET/bin/as" \
--with-ld="$DIST_BINUTILS/$TARGET/bin/ld" \
--with-build-time-tools="$DIST_BINUTILS/$TARGET/bin" \
--enable-languages=c \
--enable-plugins \
--enable-lto \
--disable-bootstrap \
--disable-decimal-float \
--disable-libffi \
--disable-libgomp \
--disable-libmudflap \
--disable-libquadmath \
--disable-libssp \
--disable-libstdcxx-pch \
--disable-nls \
--disable-shared \
--disable-threads \
--disable-tls \
--with-gnu-as \
--with-gnu-ld \
--without-headers \
--disable-libstdcxx \
--with-newlib \
--with-gmp="$PREFIX" \
--with-mpfr="$PREFIX" \
--with-mpc="$PREFIX"
make -j"$JOBS" all-gcc all-target-libgcc
make install-strip-gcc install-target-libgcc
popd
rm -rf "$DIST_GCC/share/info"
create_package gcc $GCC_VERSION "GNU CC for cross-compiling to arm-none-eabi"
pushd build/gdb
# python3.6-dev package is needed
# --with-gdb-datadir option moves datafiles to a target-specific
# directory so as not to conflict with other gdbs
../../src/gdb-$GDB_VERSION/configure \
--target=$TARGET \
--prefix="$DIST_GDB" \
--with-gdb-datadir="$DIST_GDB/arm-none-eabi/share/gdb" \
--disable-nls \
--disable-sim \
--disable-gas \
--disable-binutils \
--disable-ld \
--disable-gprof \
--with-curses \
--enable-tui \
--with-libmpfr-prefix="$PREFIX" \
--with-python=/usr/bin/python3.6 \
--with-gmp="$PREFIX" \
--with-mpfr="$PREFIX" \
--with-mpc="$PREFIX"
make -j"$JOBS"
make install-strip
popd
rm -rf "$DIST_GDB/share/info"
create_package gdb $GDB_VERSION "GNU Debugger for arm-none-eabi"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment