RustForge

foundry

Foundry OS: the operating system built on the Rustbelt kernel

foundry / ports / sbase / build.sh
2035 bytes ยท raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env bash
# Build sbase (suckless's small Unix tools: one short C file per tool) for
# Foundry OS: unpack the cached tarball, apply patches/*.patch in order, then
# sbase's plain Makefile (no configure step) with our compiler:
#   make CC=toolchain/foundry-cc
# Every tool comes out as its own static-PIE program. They are installed in
# /usr/lib/sbase on the ext2 root, OFF the default PATH, so the Foundry OS
# Rust tools (/bin/ls, /bin/cat, ...) stay the default: run an sbase tool by
# its full path, e.g. /usr/lib/sbase/sort, or put the directory on PATH
# yourself (PATH=/usr/lib/sbase:$PATH).
#
# Usage: ports/sbase/build.sh CACHE_DIR BUILD_DIR OUT_DIR
#   OUT_DIR/bin/ gets every tool; OUT_DIR/tools.txt lists their names.
set -euo pipefail
HERE=$(cd "$(dirname "$0")" && pwd)
ROOT=$(cd "$HERE/../.." && pwd)
CACHE=$(realpath "$1") BUILD=$(realpath -m "$2") OUTDIR=$(realpath -m "$3")
read -r _ file < "$HERE/sbase.sha256"
rm -rf "$BUILD" && mkdir -p "$BUILD"
tar -xzf "$CACHE/$file" -C "$BUILD"
src=$BUILD/${file%.tar.gz}
for p in "$HERE"/patches/*.patch; do
    [ -e "$p" ] || continue
    echo "[sbase] applying $(basename "$p")"; patch -s -d "$src" -p1 < "$p"
done
chmod +x "$src"/scripts/*.sh   # patch(1) can't set the executable bit
cd "$src"
make -j"$(nproc)" CC="$ROOT/toolchain/foundry-cc" CFLAGS="-O2" > "$BUILD/make.log" 2>&1 ||
    { tail -30 "$BUILD/make.log"; exit 1; }
# The tool list is the Makefile's BIN variable (one name per line).
tools=$(sed -n '/^BIN =/,/^$/p' Makefile | tr -d '\\' | tail -n +2 | tr -s ' \t\n' '\n' | sed '/^$/d')
rm -rf "$OUTDIR" && mkdir -p "$OUTDIR/bin"
for t in $tools; do
    cp "$t" "$OUTDIR/bin/$t"
    strip --strip-debug "$OUTDIR/bin/$t"
    if readelf -l "$OUTDIR/bin/$t" | grep -q INTERP; then
        echo "[sbase] $t has a PT_INTERP: not a static-PIE"; exit 1
    fi
done
echo "$tools" > "$OUTDIR/tools.txt"
cp LICENSE "$OUTDIR/LICENSE"
echo "[sbase] built $(echo "$tools" | wc -l) tools in $OUTDIR/bin ($(du -sb "$OUTDIR/bin" | cut -f1) bytes, all static-PIE)"