Cotton code
125 lines · 5.8 KB · ShellRaw
#!/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 `!<arch>`, 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 pipefailROOT="$(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 0fi# 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 '!<arch>\n' > "$FRESH/$id/$LIBRARY"  printf '\t\t<dict>\n'  printf '\t\t\t<key>BinaryPath</key>\n\t\t\t<string>%s</string>\n' "$LIBRARY"  printf '\t\t\t<key>LibraryIdentifier</key>\n\t\t\t<string>%s</string>\n' "$id"  printf '\t\t\t<key>LibraryPath</key>\n\t\t\t<string>%s</string>\n' "$LIBRARY"  printf '\t\t\t<key>SupportedArchitectures</key>\n\t\t\t<array>\n'  local arch  for arch in "$@"; do printf '\t\t\t\t<string>%s</string>\n' "$arch"; done  printf '\t\t\t</array>\n'  printf '\t\t\t<key>SupportedPlatform</key>\n\t\t\t<string>%s</string>\n' "$platform"  if [ -n "$variant" ]; then    printf '\t\t\t<key>SupportedPlatformVariant</key>\n\t\t\t<string>%s</string>\n' "$variant"  fi  printf '\t\t</dict>\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'<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict>	<!-- A STUB, written by scripts/ffi-stub.sh so SwiftPM can resolve	     apps/cotton/Package.swift and build against it. Each library is an	     empty archive with no headers, so there is no CottonFFI module and	     CottonRuntime.linksFFI answers false.	     `bash scripts/build-xcframework.sh` replaces this whole directory	     with the real archive. -->	<key>CottonFFIStub</key>	<true/>	<key>AvailableLibraries</key>	<array>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'	</array>	<key>CFBundlePackageType</key>	<string>XFWK</string>	<key>XCFrameworkFormatVersion</key>	<string>1.0</string></dict></plist>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"fiecho "[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."