comodore-iec-emu/kernel/build-in-docker.sh
2026-06-18 22:25:42 +02:00

49 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Build the IEC listener kernel module in an emulated arm64 container, so it can
# be compiled on a non-Pi (x86) host. The resulting iec_listener.ko is written
# back into this kernel/ directory on the host.
#
# Usage:
# ./build-in-docker.sh # build the module
# ./build-in-docker.sh clean # clean build artifacts
#
# Configurable via environment variables:
# HEADERS_PKG headers package (default: linux-headers-rpi-v8, 64-bit Zero 2 W)
# e.g. linux-headers-rpi-v7 / -v6 for 32-bit, raspberrypi-kernel-headers
# KERNEL_VERSION exact version to pin, e.g. 1:6.6.51-1+rpt3 (default: latest in repo)
# IMAGE builder image tag (default: iec-kbuild)
#
# NOTE: the raspberrypi apt archive generally serves only the *latest* kernel in
# its pool, so pinning KERNEL_VERSION to an old release may fail to download. The
# reliable match strategy is to keep the Pi current (`sudo apt full-upgrade`) and
# build with the default (latest). See docs/kernel-notes.md.
set -euo pipefail
cd "$(dirname "$0")"
IMAGE="${IMAGE:-iec-kbuild}"
HEADERS_PKG="${HEADERS_PKG:-linux-headers-rpi-v8}"
KERNEL_VERSION="${KERNEL_VERSION:-}"
# 1. Make sure the host can run arm64 binaries under qemu (one-time, privileged).
if [ ! -e /proc/sys/fs/binfmt_misc/qemu-aarch64 ]; then
echo "==> registering qemu arm64 binfmt (one-time, needs a privileged container)"
docker run --privileged --rm tonistiigi/binfmt --install arm64
fi
# 2. Build the builder image (installs toolchain + matching kernel headers).
echo "==> building image '$IMAGE' (HEADERS_PKG=$HEADERS_PKG KERNEL_VERSION=${KERNEL_VERSION:-latest})"
docker build --platform linux/arm64 \
--build-arg HEADERS_PKG="$HEADERS_PKG" \
--build-arg KERNEL_VERSION="$KERNEL_VERSION" \
-t "$IMAGE" .
# 3. Compile; mount this dir so the .ko lands on the host, owned by the caller.
echo "==> compiling module"
docker run --rm --platform linux/arm64 \
-u "$(id -u):$(id -g)" -e HOME=/tmp \
-v "$PWD:/build" "$IMAGE" "$@"
echo "==> done"
ls -l iec_listener.ko 2>/dev/null || echo "(no iec_listener.ko produced — see output above)"