#!/usr/bin/env bash # Canonical OnionWire release build. RUNS INSIDE THE PINNED BUILDER IMAGE # (BUILDER_IMAGE in .forgejo/workflows/release.yml — pinned by digest, not by a # floating tag, and recorded in the build-info asset). # # This is the only supported way to produce a release binary. The bytes it # emits are exactly what `scripts/verify-reproducible.sh` rebuilds and compares, # so anything that can move those bytes is pinned here rather than left to the # environment: # # * the builder image, by digest — `rust:1.98-bookworm` floats, and rustc # patches change codegen; # * every absolute build path (source, cargo target dir, cargo registry). # rustc's `--remap-path-prefix` covers paths that reach rustc's own # strings and debug info, but NOT absolute paths compiled in by a C # toolchain — and this crate always has one: rusqlite's # `bundled-sqlcipher-vendored-openssl` feature compiles OpenSSL from # source, and OpenSSL bakes its build and config paths into the artifact. # The build therefore happens at fixed paths inside the container; # * SOURCE_DATE_EPOCH, from the released commit's committer date, so no # build clock reaches the output (OpenSSL and any date-dependent crate # read it); # * the dependency graph, via `--locked`; # * the strip step and its exact flag (binutils versions differ between # hosts; inside the pinned image they do not). # # RUSTFLAGS is *replaced*, never extended. An ambient RUSTFLAGS from a shell # profile or a CI default would silently change the output bytes, which is # precisely the thing this script exists to prevent. # # Inputs (env): # SOURCE_DATE_EPOCH required. `git log -1 --format=%ct ` # SRC source checkout (default /src) # CARGO_TARGET_DIR build directory (default /target) # OUT where dist assets are written (default /out) # TARGET_TRIPLE asset name triple, defaults to the image's rustc host # BUILDER_IMAGE recorded in build-info.txt only (no effect on bytes) # CARGO_BUILD_JOBS build parallelism (no effect on bytes; default 2) # # Outputs in $OUT: # onionwire- the stripped binary # onionwire-.sha256 sha256sum-format checksum of it # build-info-.txt the recipe record: toolchain, epoch, commit, # image, and the hash this run produced set -euo pipefail SRC="${SRC:-/src}" OUT="${OUT:-/out}" export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-/target}" : "${SOURCE_DATE_EPOCH:?set SOURCE_DATE_EPOCH to the committer time of the released commit: git log -1 --format=%ct }" export SOURCE_DATE_EPOCH # No colour, no progress bars: not a determinism input, but it keeps CI logs # diffable between two builds of the same commit. export CARGO_TERM_COLOR=never export CARGO_BUILD_JOBS="${CARGO_BUILD_JOBS:-2}" # Fixed paths. The remaps make the output independent of where $SRC actually # lives on the verifying host; the container keeps the paths themselves fixed # for the C toolchains that ignore remapping entirely. # # CARGO_HOME is remapped as a whole, not just `registry`: the default feature # set pulls `cuprated` from a pinned git revision, and its checkout lands under # `$CARGO_HOME/git/...` — a path that would otherwise reach the artifact through # cuprate's own `file!()`/panic locations. CARGO_HOME_DIR="${CARGO_HOME:-/usr/local/cargo}" export RUSTFLAGS="--remap-path-prefix=${SRC}=/onionwire --remap-path-prefix=${CARGO_TARGET_DIR}=/onionwire/target --remap-path-prefix=${CARGO_HOME_DIR}=/cargo" cd "$SRC" # Build dependencies are installed HERE rather than by the caller, so CI and a # verifier get the same toolchain. `cmake` and a C toolchain are required because # the default feature set includes `cuprate-daemon` (RandomX + fjall), which the # release binary ships. # # Honest limit: these apt packages are not digest-pinned the way the image is, so # a Debian security update to gcc or cmake can move the output bytes. build-info # records their versions, so that mismatch is diagnosable instead of mysterious. apt-get -o Acquire::Retries=5 -o Acquire::ForceIPv4=true update apt-get -o Acquire::Retries=5 -o Acquire::ForceIPv4=true install -y --no-install-recommends \ pkg-config libssl-dev cmake build-essential TARGET_TRIPLE="${TARGET_TRIPLE:-$(rustc -vV | sed -n 's/^host: //p')}" [ -n "$TARGET_TRIPLE" ] || { echo "could not determine the target triple from rustc -vV" >&2; exit 1; } # Derived once into variables: the values are wanted twice (the log line and # build-info.txt), and nesting a command substitution with its own quotes # inside a double-quoted string is where this file previously broke. EPOCH_UTC="$(date -u -d "@${SOURCE_DATE_EPOCH}" +%Y-%m-%dT%H:%M:%SZ)" RUSTC_VV="$(rustc -Vv | tr '\n' ' ' | sed 's/ */ /g')" CARGO_V="$(cargo -V)" STRIP_V="$(strip --version | head -1)" CC_V="$(cc --version | head -1)" CMAKE_V="$(cmake --version | head -1)" echo "==> builder image: ${BUILDER_IMAGE:-unknown}" echo "==> rustc: ${RUSTC_VV}" echo "==> cargo: ${CARGO_V}" echo "==> SOURCE_DATE_EPOCH: ${SOURCE_DATE_EPOCH} (${EPOCH_UTC})" echo "==> RUSTFLAGS: ${RUSTFLAGS}" cargo build --release --locked --bin onionwire BIN="${CARGO_TARGET_DIR}/release/onionwire" [ -f "$BIN" ] || { echo "no binary at $BIN" >&2; exit 1; } # CI passes the released sha in (the builder image has no git checkout to read); # a verifier running this from a clone lets it derive the sha itself. GIT_SHA="${GIT_SHA:-$(git -C "$SRC" rev-parse HEAD 2>/dev/null || echo unknown)}" strip --strip-all "$BIN" mkdir -p "$OUT" install -m 755 "$BIN" "${OUT}/onionwire-${TARGET_TRIPLE}" ( cd "$OUT" sha256sum "onionwire-${TARGET_TRIPLE}" > "onionwire-${TARGET_TRIPLE}.sha256" sha256sum -c "onionwire-${TARGET_TRIPLE}.sha256" ) # The recipe record. Published next to the binary so a verifier can see which # toolchain, epoch and commit produced the hash they are comparing against # without having to reverse it out of a CI log. BINARY_SHA="$(cut -d' ' -f1 < "${OUT}/onionwire-${TARGET_TRIPLE}.sha256")" { echo "onionwire ${TARGET_TRIPLE}" echo "commit: ${GIT_SHA}" echo "source_date_epoch: ${SOURCE_DATE_EPOCH}" echo "commit_time_utc: ${EPOCH_UTC}" echo "builder_image: ${BUILDER_IMAGE:-unknown}" echo "rustc: ${RUSTC_VV}" echo "cargo: ${CARGO_V}" echo "strip: ${STRIP_V}" echo "cc: ${CC_V}" echo "cmake: ${CMAKE_V}" echo "rustflags: ${RUSTFLAGS}" echo "build_command: cargo build --release --locked --bin onionwire" echo "sha256: ${BINARY_SHA}" } > "${OUT}/build-info-${TARGET_TRIPLE}.txt" echo "==> build-info-${TARGET_TRIPLE}.txt" cat "${OUT}/build-info-${TARGET_TRIPLE}.txt"