104 lines
4.9 KiB
Bash
Executable File
104 lines
4.9 KiB
Bash
Executable File
#!/bin/sh
|
|
# Build auditable option/olsr payloads. No third-party prepatched binaries.
|
|
set -eu
|
|
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
|
CACHE="$ROOT/build/runtime-sources"
|
|
ARCH=${1:-${ROUTEROS_ARCH:-arm}}
|
|
BUILD_DIR="$ROOT/build/runtime-$ARCH"
|
|
mkdir -p "$CACHE"
|
|
# Always build fresh payload trees so removed files cannot leak into releases.
|
|
rm -rf "$BUILD_DIR/option-root" "$BUILD_DIR/olsr-root" "$BUILD_DIR/bird-root"
|
|
mkdir -p "$BUILD_DIR"
|
|
fetch() {
|
|
url=$1 file=$2 checksum=$3
|
|
if [ -f "$file" ] && ! printf '%s %s\n' "$checksum" "$file" | sha256sum -c - >/dev/null 2>&1; then
|
|
rm -f "$file"
|
|
fi
|
|
# Reuse the original ARM toolchain cache name used by previous builds.
|
|
# This avoids downloading the same 100 MB archive again after a cache-key change.
|
|
if [ ! -f "$file" ] && [ -f "$CACHE/toolchain.tgz" ] &&
|
|
printf '%s %s\n' "$checksum" "$CACHE/toolchain.tgz" | sha256sum -c - >/dev/null 2>&1; then
|
|
cp "$CACHE/toolchain.tgz" "$file"
|
|
fi
|
|
if [ ! -f "$file" ]; then
|
|
tmp="$file.part"
|
|
rm -f "$tmp"
|
|
curl --fail --location --retry 3 --retry-delay 2 "$url" -o "$tmp"
|
|
printf '%s %s\n' "$checksum" "$tmp" | sha256sum -c -
|
|
mv "$tmp" "$file"
|
|
fi
|
|
printf '%s %s\n' "$checksum" "$file" | sha256sum -c -
|
|
}
|
|
case "$ARCH" in
|
|
arm)
|
|
TOOLCHAIN=arm-linux-musleabi-cross
|
|
TOOLCHAIN_URL=https://musl.cc/arm-linux-musleabi-cross.tgz
|
|
TOOLCHAIN_SHA=d70c607101fee5330083463feacf5992892a85b826d1626094500e0c37ec7d25
|
|
TARGET=arm-linux-musleabi
|
|
;;
|
|
arm64)
|
|
TOOLCHAIN=aarch64-linux-musl-cross
|
|
TOOLCHAIN_URL=https://musl.cc/aarch64-linux-musl-cross.tgz
|
|
TOOLCHAIN_SHA=c909817856d6ceda86aa510894fa3527eac7989f0ef6e87b5721c58737a06c38
|
|
TARGET=aarch64-linux-musl
|
|
;;
|
|
*) echo "Unsupported runtime architecture: $ARCH" >&2; exit 2 ;;
|
|
esac
|
|
fetch "$TOOLCHAIN_URL" "$CACHE/$TOOLCHAIN.tgz" "$TOOLCHAIN_SHA"
|
|
if [ ! -d "$CACHE/$TOOLCHAIN" ]; then tar -xzf "$CACHE/$TOOLCHAIN.tgz" -C "$CACHE"; fi
|
|
export CROSS_COMPILE="$CACHE/$TOOLCHAIN/bin/$TARGET-"
|
|
export ROUTEROS_ARCH="$ARCH"
|
|
export TARGET
|
|
export BUILD_DIR
|
|
fetch https://busybox.net/downloads/busybox-1.37.0.tar.bz2 "$CACHE/busybox.tar.bz2" \
|
|
3311dff32e746499f4df0d5df04d7eb396382d7e108bb9250e7b519b837043a4
|
|
if [ ! -d "$CACHE/busybox-1.37.0" ]; then tar -xjf "$CACHE/busybox.tar.bz2" -C "$CACHE"; fi
|
|
SOURCE="$CACHE/busybox-1.37.0"
|
|
make -C "$SOURCE" CROSS_COMPILE="$CROSS_COMPILE" allnoconfig
|
|
python3 - "$SOURCE/.config" <<'PY'
|
|
from pathlib import Path
|
|
import sys
|
|
path = Path(sys.argv[1])
|
|
settings = ['STATIC', 'LFS', 'ASH', 'SH_IS_ASH', 'BASH_IS_ASH', 'ASH_BASH_COMPAT', 'ASH_PRINTF', 'ASH_ECHO', 'ASH_TEST',
|
|
'FEATURE_FANCY_HEAD', 'CAT', 'CHMOD', 'CP', 'DATE', 'ECHO', 'GREP', 'HEAD', 'ID', 'IP',
|
|
'FEATURE_IP_ADDRESS', 'FEATURE_IP_LINK', 'FEATURE_IP_ROUTE',
|
|
'FEATURE_IP_RULE', 'KILL', 'LS', 'MKDIR', 'MKFIFO', 'MOUNT', 'MV', 'PRINTF',
|
|
'READLINK', 'RM', 'SED', 'SHA256SUM', 'SLEEP', 'STAT', 'TAIL',
|
|
'TEST', 'TIMEOUT', 'UMOUNT', 'UNAME']
|
|
text = path.read_text()
|
|
for setting in settings:
|
|
text = text.replace(f'# CONFIG_{setting} is not set', f'CONFIG_{setting}=y')
|
|
text = text.replace('CONFIG_SH_IS_NONE=y', '# CONFIG_SH_IS_NONE is not set')
|
|
text = text.replace('CONFIG_BASH_IS_NONE=y', '# CONFIG_BASH_IS_NONE is not set')
|
|
path.write_text(text)
|
|
PY
|
|
# Defaults for any Kconfig dependencies introduced by enabling applets.
|
|
python3 - "$SOURCE" "$CROSS_COMPILE" <<'PY'
|
|
import subprocess, sys
|
|
subprocess.run(['make', '-C', sys.argv[1], 'CROSS_COMPILE=' + sys.argv[2], 'oldconfig'],
|
|
input=b'\n' * 4096, check=True, stdout=subprocess.DEVNULL)
|
|
PY
|
|
make -C "$SOURCE" -j"${JOBS:-2}" CROSS_COMPILE="$CROSS_COMPILE"
|
|
mkdir -p "$BUILD_DIR/option-root/bin" "$BUILD_DIR/option-root/share/licenses"
|
|
cp "$SOURCE/busybox" "$BUILD_DIR/option-root/bin/busybox"
|
|
ln -sfn busybox "$BUILD_DIR/option-root/bin/bash"
|
|
ln -sfn busybox "$BUILD_DIR/option-root/bin/sh"
|
|
# Make enabled applets usable in the interactive shell too.
|
|
for applet in cat chmod cp date echo grep head id ip kill ls mkdir mkfifo mount mv printf readlink rm sed sha256sum sleep stat tail test timeout umount uname; do
|
|
ln -sfn busybox "$BUILD_DIR/option-root/bin/$applet"
|
|
done
|
|
cp "$SOURCE/LICENSE" "$BUILD_DIR/option-root/share/licenses/busybox.txt"
|
|
cp "$SOURCE/.config" "$BUILD_DIR/option-root/share/busybox.config"
|
|
printf '%s\n' 'BusyBox utilities only. No license generator, shell unlock, or loader patch.' \
|
|
> "$BUILD_DIR/option-root/share/STATUS.txt"
|
|
"$ROOT/tools/build-olsrd.sh"
|
|
|
|
"$ROOT/tools/build-bird.sh"
|
|
|
|
# Attribution for statically linked toolchain runtime libraries.
|
|
for payload in option olsr; do
|
|
cp "$ROOT/vendor/licenses/musl-COPYRIGHT" "$BUILD_DIR/$payload-root/share/licenses/"
|
|
cp "$ROOT/vendor/licenses/GCC-RUNTIME-EXCEPTION" "$BUILD_DIR/$payload-root/share/licenses/"
|
|
cp "$ROOT/LICENSE" "$BUILD_DIR/$payload-root/share/licenses/GPL-3.txt"
|
|
done
|