| 1 | #!/usr/bin/env bash |
| 2 | # Stage the anvild binary that the image COPYs in. NOT run on the VPS — it |
| 3 | # never compiles anything. |
| 4 | # |
| 5 | # Uses cargo-zigbuild to cross-compile a fully static x86_64-musl executable at |
| 6 | # native speed (no QEMU) and leaves it in docker/, beside the Dockerfile that |
| 7 | # COPYs it. Building and pushing the image is compose's job from there (see |
| 8 | # compose.yaml); ./deploy/deploy.sh runs both halves. |
| 9 | # |
| 10 | # ./deploy/build.sh release — what gets deployed |
| 11 | # ./deploy/build.sh --debug unoptimized, for the local compose.override.yaml |
| 12 | # ./deploy/build.sh --worker also stage anvil-worker, for the local runners |
| 13 | # |
| 14 | # --worker is a local-development thing only: production runners are native |
| 15 | # processes on the build host (docs/remote-runners.md), and nothing on hagrid |
| 16 | # builds or runs the worker image. |
| 17 | # |
| 18 | # Prereqs (one-time): |
| 19 | # zig (dnf install zig, or brew install zig) |
| 20 | # cargo install cargo-zigbuild |
| 21 | # The toolchain and the musl target come from rust-toolchain.toml — rustup |
| 22 | # installs both on the first cargo invocation in the repo. |
| 23 | set -euo pipefail |
| 24 | |
| 25 | TARGET="x86_64-unknown-linux-musl" |
| 26 | PROFILE=release |
| 27 | CARGO_FLAGS=(--release) |
| 28 | # Binary name → the image dir whose Dockerfile COPYs it. Both are gitignored |
| 29 | # and overwritten by the next build. |
| 30 | BINS=(anvild) |
| 31 | stage_dir() { |
| 32 | case "$1" in |
| 33 | anvild) echo docker/anvil ;; |
| 34 | anvil-worker) echo docker/worker ;; |
| 35 | *) echo "no image dir for binary $1" >&2; exit 1 ;; |
| 36 | esac |
| 37 | } |
| 38 | |
| 39 | for arg in "$@"; do |
| 40 | case "$arg" in |
| 41 | --debug) PROFILE=debug; CARGO_FLAGS=() ;; |
| 42 | --worker) BINS+=(anvil-worker) ;; |
| 43 | *) echo "usage: $0 [--debug] [--worker]" >&2; exit 64 ;; |
| 44 | esac |
| 45 | done |
| 46 | |
| 47 | cd "$(dirname "$0")/.." |
| 48 | |
| 49 | # macOS terminals default to a 256 open-file soft limit; zig's linker opens |
| 50 | # every rlib at once and dies with ProcessFdQuotaExceeded. Raise it for this |
| 51 | # process (children inherit it). |
| 52 | ulimit -n 4096 2>/dev/null || true |
| 53 | |
| 54 | # Static musl even for --debug: the runtime image is Ubuntu, and a binary |
| 55 | # linked against Fedora's glibc would not run there. |
| 56 | for bin in "${BINS[@]}"; do |
| 57 | echo "==> cross-compiling $bin for $TARGET ($PROFILE, native, via zig)" |
| 58 | cargo zigbuild --target "$TARGET" --bin "$bin" "${CARGO_FLAGS[@]}" |
| 59 | |
| 60 | dir="$(stage_dir "$bin")" |
| 61 | echo "==> staging binary at $dir/$bin" |
| 62 | cp "target/$TARGET/$PROFILE/$bin" "$dir/$bin" |
| 63 | done |
| 64 | |
| 65 | # Deliberately left in place: `docker compose build` runs after this and needs |
| 66 | # it in the context. It is gitignored, and the next build overwrites it. |
| 67 | echo "==> staged ($PROFILE). Build the image with: docker compose build" |