Files
Rayhaan JaufeerallyandClaude Opus 5.5 e1d819d629 Add a script that builds a static Linux executable
scripts/build-static.sh builds the daemon for x86_64 Linux against
musl, as one executable without dependencies to copy to a router. It
finds a musl C compiler for jemalloc (musl-cross on macOS, musl-gcc on
Linux), and uses its ar and ranlib too: with the host's, a build from
macOS gets an empty libjemalloc.a and fails to link. It prints the
revision and the SHA-256 of the result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 01:03:16 +02:00

50 lines
1.8 KiB
Bash
Executable File

#!/bin/sh
# Builds the daemon as one static executable for x86_64 Linux, which runs
# on any x86_64 Linux distribution with nothing else installed.
#
# scripts/build-static.sh
#
# The executable is linked against musl, with jemalloc as the memory
# allocator (see bin/bgp/src/main.rs). jemalloc is C, so a C compiler for
# the target is needed as well as the Rust target:
#
# rustup target add x86_64-unknown-linux-musl
# macOS: brew install FiloSottile/musl-cross/musl-cross
# Debian, Ubuntu (on x86_64): apt-get install musl-tools
# Arch (on x86_64): pacman -S musl
set -eu
target=x86_64-unknown-linux-musl
repository="$(cd "$(dirname "$0")/.." && pwd)"
# The cross compiler from musl-cross, or musl's own wrapper on Linux.
if command -v x86_64-linux-musl-gcc >/dev/null; then
cc=x86_64-linux-musl-gcc
ar=x86_64-linux-musl-ar
ranlib=x86_64-linux-musl-ranlib
elif command -v musl-gcc >/dev/null; then
cc=musl-gcc
ar=ar
ranlib=ranlib
else
echo "No C compiler for $target: see the comment at the top of $0" >&2
exit 1
fi
cd "$repository"
# jemalloc is built by its own configure script, which looks for an
# archiver named after the target (x86_64-unknown-linux-musl-ar) and, not
# finding one, would take the host's: on macOS that one cannot index Linux
# objects, and the library comes out empty. So AR and RANLIB are named.
CC_x86_64_unknown_linux_musl=$cc \
AR_x86_64_unknown_linux_musl=$ar \
AR=$ar \
RANLIB=$ranlib \
CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=$cc \
cargo build --release --locked -p bgp --target "$target"
binary="target/$target/release/bgp"
echo
file "$binary"
echo "revision $(git rev-parse --short HEAD)$(git diff --quiet HEAD || echo ', with uncommitted changes')"
if command -v sha256sum >/dev/null; then sha256sum "$binary"; else shasum -a 256 "$binary"; fi