| 1 | #!/usr/bin/env bash |
| 2 | # Build the core crate for wazero. No wasm-bindgen, no wasm-pack: the Go host |
| 3 | # provides no JS glue, so the module must be plain wasm32 with a C ABI. |
| 4 | set -euo pipefail |
| 5 | cd "$(dirname "$0")/.." |
| 6 | # Lint the PDF renderer before building its standalone C ABI module. |
| 7 | if [[ "${1:-}" != "--build-only" && "${1:-}" != "--canonical-build" && "${1:-}" != "--native-build" ]]; then |
| 8 | cargo clippy --manifest-path core/pdf/Cargo.toml --all-targets -- -D warnings |
| 9 | fi |
| 10 | # Rust/Cargo crate identities affect code ordering even after path remapping. |
| 11 | # Build on Linux amd64 at fixed source/toolchain paths on every host that can. |
| 12 | if [[ "${1:-}" == "--canonical-build" ]]; then |
| 13 | [[ "$(uname -s)/$(uname -m)" == "Linux/x86_64" && "$(pwd)" == /work \ |
| 14 | && "$CARGO_HOME" == /usr/local/cargo && "$RUSTUP_HOME" == /usr/local/rustup ]] \ |
| 15 | || { echo "build-pdf-wasm: invalid canonical build environment" >&2; exit 1; } |
| 16 | export CARGO_TARGET_DIR=/work/core/pdf/target RUSTUP_TOOLCHAIN=1.98.0 |
| 17 | elif [[ "${1:-}" == "--native-build" ]]; then |
| 18 | : # this checkout, this host's toolchain: built below, not reproducible |
| 19 | else |
| 20 | # Cargo's crate identities (-C metadata) also depend on the host |
| 21 | # architecture, so no build on a Linux arm64 host matches the committed |
| 22 | # amd64 bytes, and the amd64 image crashes under qemu there. Build |
| 23 | # natively instead, for the tests that read the artifact. That build |
| 24 | # writes no stamp; the gate checks freshness there by the input stamp |
| 25 | # alone, and on Linux amd64 by the stamp and the bytes. |
| 26 | if [[ "$(uname -s)" == Linux && "$(uname -m)" != x86_64 ]]; then |
| 27 | echo "build-pdf-wasm: Linux/$(uname -m) builds natively; the result is not the canonical amd64 build" >&2 |
| 28 | exec ./scripts/build-pdf-wasm.sh --native-build |
| 29 | fi |
| 30 | # Hash the inputs before building, so an edit made while cargo runs |
| 31 | # cannot be stamped as built (scripts/wasm-stamp.sh says what is hashed). |
| 32 | inputs=$(./scripts/wasm-stamp.sh hash slides_pdf) |
| 33 | out="core/pdf/target/wasm32-unknown-unknown/release/slides_pdf.wasm" |
| 34 | mkdir -p "$(dirname "$out")" |
| 35 | temporary=$(mktemp "${out}.XXXXXX") |
| 36 | trap 'rm -f "$temporary"' EXIT |
| 37 | if [[ "$(uname -s)/$(uname -m)" == "Linux/x86_64" ]] && command -v bwrap >/dev/null; then |
| 38 | cargo_home=$(cd "${CARGO_HOME:-$HOME/.cargo}" && pwd) |
| 39 | rustup_home=$(cd "${RUSTUP_HOME:-$HOME/.rustup}" && pwd) |
| 40 | mkdir -p core/pdf/target/canonical-linux-amd64 |
| 41 | bwrap --tmpfs / \ |
| 42 | --ro-bind /usr /usr --tmpfs /usr/local \ |
| 43 | --ro-bind /bin /bin --ro-bind /lib /lib --ro-bind /lib64 /lib64 \ |
| 44 | --ro-bind /etc /etc --bind /tmp /tmp --proc /proc --dev /dev \ |
| 45 | --ro-bind "$(pwd)" /work \ |
| 46 | --bind "$(pwd)/core/pdf/target/canonical-linux-amd64" /work/core/pdf/target \ |
| 47 | --bind "$cargo_home" /usr/local/cargo --ro-bind "$rustup_home" /usr/local/rustup \ |
| 48 | --chdir /work --setenv CARGO_HOME /usr/local/cargo --setenv RUSTUP_HOME /usr/local/rustup \ |
| 49 | --setenv PATH /usr/local/cargo/bin:/usr/local/bin:/usr/bin:/bin \ |
| 50 | bash -ec './scripts/build-pdf-wasm.sh --canonical-build >&2; cat core/pdf/target/wasm32-unknown-unknown/release/slides_pdf.wasm' > "$temporary" |
| 51 | else |
| 52 | command -v docker >/dev/null || { echo "build-pdf-wasm: install Docker, or bubblewrap on Linux amd64" >&2; exit 1; } |
| 53 | target_key=$(printf %s "$(pwd)" | shasum -a 256 | cut -c1-16) |
| 54 | docker run --rm --platform linux/amd64 \ |
| 55 | --mount "type=bind,source=$(pwd),target=/work,readonly" \ |
| 56 | --mount "type=volume,source=oficina-slides-pdf-wasm-target-${target_key},target=/work/core/pdf/target" \ |
| 57 | --mount type=volume,source=oficina-slides-pdf-wasm-cargo,target=/usr/local/cargo \ |
| 58 | --mount type=volume,source=oficina-slides-pdf-wasm-rustup,target=/usr/local/rustup \ |
| 59 | --workdir /work \ |
| 60 | rust@sha256:8aea3a845c48caac4091a8d0e6494d213061f8f014dc2f5a5f1a88a1a8c9f301 \ |
| 61 | bash -ec 'rustup target add wasm32-unknown-unknown >&2; ./scripts/build-pdf-wasm.sh --canonical-build >&2; cat core/pdf/target/wasm32-unknown-unknown/release/slides_pdf.wasm' > "$temporary" |
| 62 | fi |
| 63 | mv "$temporary" "$out" |
| 64 | # The stamp goes beside the committed copy. Any host's gate checks it: |
| 65 | # the inputs must still hash to what was built, and the committed wasm |
| 66 | # must be this build, so a forgotten cp fails too. |
| 67 | ./scripts/wasm-stamp.sh write slides_pdf "$inputs" |
| 68 | echo "build-pdf-wasm: built $out ($(wc -c < "$out") bytes) and wrote internal/slides/pdfcore/testdata/slides_pdf.wasm.stamp" >&2 |
| 69 | echo "build-pdf-wasm: to ship it, cp $out internal/slides/pdfcore/testdata/slides_pdf.wasm and commit both" >&2 |
| 70 | exit 0 |
| 71 | fi |
| 72 | # Ship only the PDF library. Fixed path remapping and stripped symbols keep |
| 73 | # diagnostics and local crate identities independent of the checkout location. |
| 74 | # The custom getrandom cfg is inert for the PDF dependency graph, but retains |
| 75 | # the workspace wasm configuration when RUSTFLAGS replaces config-file flags. |
| 76 | registry_root="${CARGO_HOME:-$HOME/.cargo}/registry/src" |
| 77 | workspace_root="$(pwd)" |
| 78 | # Cargo also puts local path dependency identities into mangled debug names. |
| 79 | # Remove that optional symbol section so identical code reproduces across worktrees. |
| 80 | export RUSTFLAGS="-C strip=symbols --cfg getrandom_backend=\"custom\" --remap-path-prefix=${registry_root}=/cargo-registry-src --remap-path-prefix=${workspace_root}=/slides-pdf" |
| 81 | cargo build --release --manifest-path core/pdf/Cargo.toml --target wasm32-unknown-unknown --locked --lib |
| 82 | out="core/pdf/target/wasm32-unknown-unknown/release/slides_pdf.wasm" |
| 83 | [ -f "$out" ] || { echo "build-pdf-wasm: no artifact at $out" >&2; exit 1; } |
| 84 | echo "$out ($(wc -c < "$out") bytes)" |
| 85 | |