#!/usr/bin/env bash # Guarantee that `apps/cotton/.ffi/CottonFFI.xcframework` EXISTS, so SwiftPM can # always resolve `apps/cotton/Package.swift`. # # bash scripts/ffi-stub.sh # idempotent; run it as often as you like # # WHAT IT WRITES. When the real archive is absent it writes a STUB XCFramework: # one `Info.plist` (`XCFrameworkFormatVersion 1.0`, `CFBundlePackageType XFWK`) # listing one library per destination the package builds for — macOS, iOS, the # iOS simulator — each an EMPTY static archive (the eight bytes `!`, no # member, no symbol, no architecture) with NO `HeadersPath`. SwiftPM resolves it # and builds and links against it; because no slice carries a header or a # module map there is no `CottonFFI` module, so `canImport(CottonFFI)` is false, # `CottonRuntime.linksFFI` answers false and `FFISmokeTests` FAILS rather than # skipping. A stub is a PLACEHOLDER FOR A PATH, never a fallback that masks a # missing core. # # WHY THE LIST IS NOT EMPTY ANY MORE (12 Oct 2026). It was: an empty # `AvailableLibraries` array. Swift Build — the default build system of # Xcode 27.0 / Swift 6.4 — refuses that while PLANNING ("Failed to load # XCFramework …: There are no libraries provided for the XCFramework"), so in a # worktree that had never built the real core `swift test` built nothing and # `scripts/ui-tests-stub.sh` could not run; the older llbuild # (`--build-system native`) still took it. What keeps the core out of a stub # build was never the empty list — it is the missing module, and that is kept. # The archive is bytes this script prints, not something a compiler makes, so # the stub is written the same on a Mac with no Xcode and on the Linux checks. # # WHY IT EXISTS. `Package.swift` used to ask the filesystem for the archive and # declare its `binaryTarget` only when the answer was yes. SwiftPM compiles a # manifest once and caches it MACHINE-WIDE, keyed by package identity, manifest # bytes, tools version, environment and swiftpm version — NOT by the package's # path. So the first worktree on a Mac to evaluate that file before its `.ffi` # existed answered "no FFI" for every other worktree of the same commit, which # then built with the archive sitting right there and ran an app saying "The # Rust core is not in this build". With three or four worktrees per Mac that is # a daily trap. The manifest is unconditional now; this script is the other # half of the bargain. Cotton note "Repo landmines — why the build's rules exist". # # IT NEVER TOUCHES A REAL ARCHIVE. The stub carries a `CottonFFIStub` marker; # anything at that path without the marker is a real build product, and this # script says so and exits 0. `scripts/build-xcframework.sh` deletes whatever is # there before it writes the real slices, so a stub never survives beside one # either. set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" XCFRAMEWORK="$ROOT/apps/cotton/.ffi/CottonFFI.xcframework" PLIST="$XCFRAMEWORK/Info.plist" MARKER="CottonFFIStub" if [ -e "$PLIST" ] && ! grep -q "$MARKER" "$PLIST" 2>/dev/null; then echo "[ffi-stub] the real XCFramework is already at ${XCFRAMEWORK#"$ROOT/"} — left alone" exit 0 fi # One listed library: an identifier (its directory), a platform, a variant or # none, and the architectures it claims. No `HeadersPath` — see the header. LIBRARY="libcotton_ffi_stub.a" slice() { local id="$1" platform="$2" variant="$3"; shift 3 mkdir -p "$FRESH/$id" # An archive with no members: valid for every architecture, linked as nothing. printf '!\n' > "$FRESH/$id/$LIBRARY" printf '\t\t\n' printf '\t\t\tBinaryPath\n\t\t\t%s\n' "$LIBRARY" printf '\t\t\tLibraryIdentifier\n\t\t\t%s\n' "$id" printf '\t\t\tLibraryPath\n\t\t\t%s\n' "$LIBRARY" printf '\t\t\tSupportedArchitectures\n\t\t\t\n' local arch for arch in "$@"; do printf '\t\t\t\t%s\n' "$arch"; done printf '\t\t\t\n' printf '\t\t\tSupportedPlatform\n\t\t\t%s\n' "$platform" if [ -n "$variant" ]; then printf '\t\t\tSupportedPlatformVariant\n\t\t\t%s\n' "$variant" fi printf '\t\t\n' } # Written beside the path and moved in only when it differs from what is # there: a run over a stub already in this shape touches no file, so no build # sees a newer library and links again; a stub in the older shape (the empty # list) is replaced whole. FRESH="$XCFRAMEWORK.stub-new" rm -rf "$FRESH" mkdir -p "$FRESH" { cat <<'PLIST_HEAD' CottonFFIStub AvailableLibraries PLIST_HEAD slice macos-arm64_x86_64 macos "" arm64 x86_64 slice ios-arm64 ios "" arm64 slice ios-arm64_x86_64-simulator ios simulator arm64 x86_64 cat <<'PLIST_FOOT' CFBundlePackageType XFWK XCFrameworkFormatVersion 1.0 PLIST_FOOT } > "$FRESH/Info.plist" if [ -d "$XCFRAMEWORK" ] && diff -rq "$FRESH" "$XCFRAMEWORK" >/dev/null 2>&1; then rm -rf "$FRESH" else rm -rf "$XCFRAMEWORK" mv "$FRESH" "$XCFRAMEWORK" fi echo "[ffi-stub] placeholder at ${XCFRAMEWORK#"$ROOT/"} — there is no Rust core in this build;" echo "[ffi-stub] run \`bash scripts/build-xcframework.sh\` for the real one."