#!/usr/bin/env bash # Build ONE slice of the Rust core's staticlib and print the archive's path. # # scripts/build-rust-ffi.sh [--debug | --profile ] [--target ] [--features ] [--no-default-features] [--print-archive] # # The cargo half of `scripts/build-xcframework.sh`, which calls this once per # slice — the two iOS triples as the client build (`--no-default-features`) — # and hands the archives to `xcodebuild -create-xcframework`, so this script # STAGES NOTHING: no staging directory under the package, no linker search # path, no linked executable to drop. It runs cargo, reads the archive # path out of `cargo metadata` (CI sets CARGO_TARGET_DIR under the lane's HOME, # so `crates/target/` is not assumed) and prints it. `--print-archive` is # accepted for the callers that pass it; printing is what it does anyway. # # The header is never copied here: `build-xcframework.sh` copies the committed # `crates/cotton-ffi/include/cotton_ffi.h` and the one module map into the # archive on every run, from their one source each. set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" CRATE_DIR="$ROOT/crates" export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$(bash "$ROOT/scripts/ci/cargo-target-dir.sh" --ensure ffi)}" PROFILE=release PROFILE_DIR=release TARGET="" FEATURES="" NO_DEFAULT_FEATURES=0 PRINT_ARCHIVE=0 while [ $# -gt 0 ]; do case "$1" in --debug) PROFILE=dev; PROFILE_DIR=debug; shift ;; # A named profile of crates/Cargo.toml (the iPhone slice's `ios`): cargo # writes it under a directory of the same name. --profile) PROFILE="$2"; PROFILE_DIR="$2"; shift 2 ;; --target) TARGET="$2"; shift 2 ;; --features) FEATURES="$2"; shift 2 ;; --no-default-features) NO_DEFAULT_FEATURES=1; shift ;; --print-archive) PRINT_ARCHIVE=1; shift ;; *) echo "usage: build-rust-ffi.sh [--debug | --profile ] [--target ] [--features ] [--no-default-features] [--print-archive]" >&2; exit 2 ;; esac done cd "$CRATE_DIR" # An archive built without IPHONEOS_DEPLOYMENT_TARGET is tagged with whatever # SDK the build Mac runs, and iOS 26 will not load it. `build-xcframework.sh` # exports it before calling this. args=(build --profile "$PROFILE" -p cotton-ffi) [ -n "$TARGET" ] && args+=(--target "$TARGET") [ "$NO_DEFAULT_FEATURES" = 1 ] && args+=(--no-default-features) [ -n "$FEATURES" ] && args+=(--features "$FEATURES") # Preparation and compilation share one lock, including direct FFI builds. # stderr, never stdout: `--print-archive` puts ONE path on stdout and a caller # reads it with `$(...)`. echo "[ffi] cargo ${args[*]}" >&2 bash "$ROOT/scripts/cargo-target-guard.sh" --run cargo +1.98.1 "${args[@]}" target_dir="$(cargo metadata --format-version 1 --no-deps | jq -r .target_directory)" [ -n "$target_dir" ] && [ "$target_dir" != "null" ] || { echo "[ffi] cargo metadata named no target directory" >&2 exit 1 } archive="$target_dir${TARGET:+/$TARGET}/$PROFILE_DIR/libcotton_ffi.a" [ -f "$archive" ] || { echo "[ffi] no archive at $archive" >&2; exit 1; } # The archive's path is the whole answer. Nothing is STAGED here: the staticlib # this script builds is only ever an input to `build-xcframework.sh`, which reads # the path off stdout (`--print-archive`, kept for the callers that pass it). printf '%s\n' "$archive"