Files
Mateusz Gruszczyński c30064b531
build / test (push) Canceled after 0s
build / deb (amd64) (push) Canceled after 0s
build / deb (arm64) (push) Canceled after 0s
build / deb (armhf) (push) Canceled after 0s
first commit
2026-11-16 16:19:21 +01:00

131 lines
5.5 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# Builds the simply-dvb .deb package for the selected architecture.
#
# scripts/build-deb.sh amd64 # PC / x86_64 server
# scripts/build-deb.sh arm64 # Raspberry Pi 3/4/5 with 64-bit Raspberry Pi OS / Debian / Ubuntu
# scripts/build-deb.sh armhf # Raspberry Pi 2/3/4 with 32-bit Raspberry Pi OS
#
# By default the build itself runs in a Docker container (image scripts/docker/builder.Dockerfile),
# so the host only needs Docker – no Rust, zig or cargo-deb. The result lands in dist/ on the host.
# BUILD_IN_DOCKER=0 build directly on the host (e.g. in CI that already runs in a container)
# BUILDER_IMAGE=... builder image name (default simply-dvb-builder:1)
# DOCKER_ARGS="..." extra arguments for `docker run` (e.g. --network host)
# DOCKER_BUILD_ARGS="..." extra arguments for `docker build` of the builder image
#
# Build method inside (BUILD_METHOD variable):
# auto (default) – zigbuild (fixed glibc 2.31 baseline for all architectures), otherwise
# native (when host = target), cross or cross-gcc
# native – cargo build on the same architecture (e.g. directly on an RPi)
# zigbuild – cargo-zigbuild (zig as the linker, selectable glibc version: GLIBC=2.31)
# cross – cross-rs (Docker containers with ready-made toolchains)
# gcc – system cross-gcc (aarch64-linux-gnu-gcc / arm-linux-gnueabihf-gcc)
#
# Other variables:
# GLIBC=2.31 minimum glibc version for zigbuild (2.31 = Debian 11 / RPi OS bullseye and newer)
# OUT_DIR=dist output directory
set -euo pipefail
cd "$(dirname "$0")/.."
ARCH="${1:-}"
case "$ARCH" in
amd64|x86_64) DEB_ARCH=amd64; TARGET=x86_64-unknown-linux-gnu; GCC_PREFIX=x86_64-linux-gnu ;;
arm64|aarch64) DEB_ARCH=arm64; TARGET=aarch64-unknown-linux-gnu; GCC_PREFIX=aarch64-linux-gnu ;;
armhf|armv7) DEB_ARCH=armhf; TARGET=armv7-unknown-linux-gnueabihf; GCC_PREFIX=arm-linux-gnueabihf ;;
*) echo "Usage: $0 {amd64|arm64|armhf}" >&2; exit 2 ;;
esac
have() { command -v "$1" >/dev/null 2>&1; }
# ---------------------------------------------------------------- build in a container
if [ -z "${SIMPLY_DVB_IN_DOCKER:-}" ] && [ "${BUILD_IN_DOCKER:-1}" != 0 ]; then
have docker || { echo "Docker not found. Install it or run with BUILD_IN_DOCKER=0." >&2; exit 1; }
IMAGE="${BUILDER_IMAGE:-simply-dvb-builder:1}"
echo "==> builder image $IMAGE"
# shellcheck disable=SC2086
docker build ${DOCKER_BUILD_ARGS:-} -t "$IMAGE" -f scripts/docker/builder.Dockerfile scripts/docker
OUT="${OUT_DIR:-dist}"
mkdir -p .cache/cargo .cache/home "$OUT"
echo "==> building $ARCH in a container"
# Output files belong to the current user; crate and build caches stay in .cache/ and target/docker/.
# shellcheck disable=SC2086
exec docker run --rm \
--user "$(id -u):$(id -g)" \
-v "$PWD:/src" -w /src \
-e HOME=/src/.cache/home \
-e CARGO_HOME=/src/.cache/cargo \
-e CARGO_TARGET_DIR=/src/target/docker \
-e ZIG_GLOBAL_CACHE_DIR=/src/.cache/home/zig \
-e BUILD_METHOD="${BUILD_METHOD:-zigbuild}" \
-e GLIBC="${GLIBC:-2.31}" \
-e OUT_DIR="$OUT" \
-e HTTP_PROXY -e HTTPS_PROXY -e NO_PROXY -e http_proxy -e https_proxy -e no_proxy \
${DOCKER_ARGS:-} \
"$IMAGE" scripts/build-deb.sh "$ARCH"
fi
METHOD="${BUILD_METHOD:-auto}"
GLIBC="${GLIBC:-2.31}"
OUT_DIR="${OUT_DIR:-dist}"
VERSION=$(grep -m1 '^version' Cargo.toml | cut -d'"' -f2)
host_arch() {
case "$(uname -m)" in
x86_64) echo amd64 ;;
aarch64|arm64) echo arm64 ;;
armv7l|armv6l) echo armhf ;;
*) uname -m ;;
esac
}
if [ "$METHOD" = auto ]; then
if have cargo-zigbuild && (have zig || python3 -c 'import ziglang' 2>/dev/null); then METHOD=zigbuild
elif [ "$(host_arch)" = "$DEB_ARCH" ]; then METHOD=native
elif have cross; then METHOD=cross
elif have "${GCC_PREFIX}-gcc"; then METHOD=gcc
else
echo "No cross-compilation tools for $DEB_ARCH. Install one of:" >&2
echo " cargo install cargo-zigbuild && pip install ziglang (recommended)" >&2
echo " cargo install cross (requires Docker)" >&2
echo " apt install gcc-${GCC_PREFIX} (cross-gcc)" >&2
exit 1
fi
fi
echo "==> simply-dvb $VERSION · $DEB_ARCH ($TARGET) · method: $METHOD"
have cargo-deb || cargo install cargo-deb --locked
if [ -z "${SIMPLY_DVB_IN_DOCKER:-}" ] && [ "$METHOD" != native ] && [ "$METHOD" != cross ] && have rustup; then
rustup target add "$TARGET" >/dev/null
fi
case "$METHOD" in
native)
cargo build --release --locked --target "$TARGET"
;;
zigbuild)
cargo zigbuild --release --locked --target "${TARGET}.${GLIBC}"
;;
cross)
cross build --release --locked --target "$TARGET"
;;
gcc)
upper=$(echo "$TARGET" | tr 'a-z-' 'A-Z_')
export "CARGO_TARGET_${upper}_LINKER=${GCC_PREFIX}-gcc"
export "CC_${TARGET//-/_}=${GCC_PREFIX}-gcc"
export "AR_${TARGET//-/_}=${GCC_PREFIX}-ar"
cargo build --release --locked --target "$TARGET"
;;
*) echo "Unknown method: $METHOD" >&2; exit 2 ;;
esac
TARGET_DIR="${CARGO_TARGET_DIR:-target}"
BIN="$TARGET_DIR/$TARGET/release/simply-dvb"
[ -x "$BIN" ] || { echo "Missing file $BIN" >&2; exit 1; }
# cargo-deb: the binary is already built; target/release/* paths are mapped to target/<triple>/release/*
DEB=$(cargo deb --no-build --no-strip --target "$TARGET" 2>&1 | tail -n1)
mkdir -p "$OUT_DIR"
cp "$DEB" "$OUT_DIR/"
echo "==> $(ls -1 "$OUT_DIR"/simply-dvb_*_"$DEB_ARCH".deb)"
dpkg-deb -f "$OUT_DIR/$(basename "$DEB")" Package Version Architecture Depends 2>/dev/null || true