| 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 at deploy/anvild, which is where the |
| 7 | # Dockerfile expects it. Building and pushing the image is compose's job from |
| 8 | # there (see 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 | # |
| 13 | # Prereqs (one-time): |
| 14 | # zig (dnf install zig, or brew install zig) |
| 15 | # cargo install cargo-zigbuild |
| 16 | # rustup target add x86_64-unknown-linux-musl |
| 17 | set -euo pipefail |
| 18 | |
| 19 | TARGET="x86_64-unknown-linux-musl" |
| 20 | PROFILE=release |
| 21 | CARGO_FLAGS=(--release) |
| 22 | |
| 23 | case "${1:-}" in |
| 24 | --debug) PROFILE=debug; CARGO_FLAGS=() ;; |
| 25 | "") ;; |
| 26 | *) echo "usage: $0 [--debug]" >&2; exit 64 ;; |
| 27 | esac |
| 28 | |
| 29 | cd "$(dirname "$0")/.." |
| 30 | |
| 31 | # macOS terminals default to a 256 open-file soft limit; zig's linker opens |
| 32 | # every rlib at once and dies with ProcessFdQuotaExceeded. Raise it for this |
| 33 | # process (children inherit it). |
| 34 | ulimit -n 4096 2>/dev/null || true |
| 35 | |
| 36 | # Static musl even for --debug: the runtime image is Ubuntu, and a binary |
| 37 | # linked against Fedora's glibc would not run there. |
| 38 | echo "==> cross-compiling anvild for $TARGET ($PROFILE, native, via zig)" |
| 39 | cargo zigbuild --target "$TARGET" --bin anvild "${CARGO_FLAGS[@]}" |
| 40 | |
| 41 | echo "==> staging binary at deploy/anvild" |
| 42 | cp "target/$TARGET/$PROFILE/anvild" deploy/anvild |
| 43 | |
| 44 | # Deliberately left in place: `docker compose build` runs after this and needs |
| 45 | # it in the context. It is gitignored, and the next build overwrites it. |
| 46 | echo "==> staged ($PROFILE). Build the image with: docker compose build" |