Cotton code
121 lines · 5.4 KB · TypeScriptRaw
// `scripts/build-xcframework.sh --simulator-only --debug` is the walk's Rust// loop (27 Oct 2026): incremental, in a target directory of its own beside the// shared sccache-fed one, every core but two, no LTO, unpacked debuginfo. Every// other mode — and the cut's release slices above all — builds as before.//// The script is run for real with a stub `bash` first on PATH that records the// environment the lock re-exec is handed and runs every other `bash` call// through: the dev loop's settings are exported before that hop, so what the// stub sees is what cargo would. Nothing is compiled and no lock is taken.import { spawnSync } from "node:child_process";import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { cpus, tmpdir } from "node:os";import { dirname, join, resolve } from "node:path";import { fileURLToPath } from "node:url";import { beforeAll, describe, expect, it } from "vitest";const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../..");const SCRIPT = join(REPO_ROOT, "scripts", "build-xcframework.sh");const LOCK = join(REPO_ROOT, "scripts", "with-build-lock.sh");const NAMES = [  "CARGO_INCREMENTAL",  "CARGO_TARGET_DIR",  "CARGO_BUILD_JOBS",  "CARGO_PROFILE_DEV_LTO",  "CARGO_PROFILE_DEV_SPLIT_DEBUGINFO",  "COTTON_RUSTC_CACHE_WRAPPER",  "RUSTC_WRAPPER",];let work: string;beforeAll(() => {  work = mkdtempSync(join(tmpdir(), "xcframework-dev-loop-"));  const stub = join(work, "bash");  const record = NAMES.map((n) => `printf '%s=%s|%s\\n' ${n} "\${${n}-}" "\${${n}+set}"`).join("; ");  writeFileSync(    stub,    `#!/bin/sh\nif [ "$1" = "${LOCK}" ]; then { ${record}; } > "${join(work, "env")}"; exit 0; fi\nexec /bin/bash "$@"\n`,  );  chmodSync(stub, 0o755);});/** The environment the lock re-exec received, by name: [value, whether set]. */function handedToTheLock(args: string[], extra: Record<string, string> = {}): Map<string, [string, boolean]> {  const recorded = join(work, "env");  rmSync(recorded, { force: true });  const target = join(work, "target", "shared");  const out = spawnSync("/bin/bash", [SCRIPT, ...args], {    cwd: REPO_ROOT,    encoding: "utf8",    env: {      ...process.env,      PATH: `${work}:${process.env.PATH ?? ""}`,      COTTON_XCFRAMEWORK_LOCKED: "",      COTTON_XCFRAMEWORK_CACHE: join(work, "cache"),      COTTON_ARTIFACT_STORE: "",      COTTON_CLAIMS_DIR: join(work, "claims"),      COTTON_CUT_ARCHIVE_ONLY: "",      CARGO_INCREMENTAL: "0",      CARGO_BUILD_JOBS: "3",      CARGO_TARGET_DIR: target,      ...extra,    },  });  expect(out.status, out.stdout + out.stderr).toBe(0);  expect(existsSync(recorded), "the script reached its lock re-exec").toBe(true);  return new Map(    readFileSync(recorded, "utf8")      .trim()      .split("\n")      .map((line) => {        const [name, rest] = [line.slice(0, line.indexOf("=")), line.slice(line.indexOf("=") + 1)];        const bar = rest.lastIndexOf("|");        return [name, [rest.slice(0, bar), rest.slice(bar + 1) === "set"]] as [string, [string, boolean]];      }),  );}describe("build-xcframework.sh — the simulator dev loop", () => {  it("builds incrementally in its own target, beside the shared one, on all cores but two", () => {    const env = handedToTheLock(["--debug", "--simulator-only"], { RUSTC_WRAPPER: "/opt/bin/sccache", CARGO_BUILD_JOBS: "" });    expect(env.get("CARGO_INCREMENTAL")?.[0]).toBe("1");    expect(env.get("CARGO_TARGET_DIR")?.[0]).toBe(join(work, "target", "shared-sim-dev"));    expect(existsSync(join(work, "target", "shared-sim-dev", "CACHEDIR.TAG"))).toBe(true);    expect(env.get("CARGO_BUILD_JOBS")?.[0]).toBe(String(Math.max(1, cpus().length > 3 ? cpus().length - 2 : 1)));    expect(env.get("CARGO_PROFILE_DEV_LTO")?.[0]).toBe("false");    expect(env.get("CARGO_PROFILE_DEV_SPLIT_DEBUGINFO")?.[0]).toBe("unpacked");    // sccache refuses incremental compiles: dropped, present-and-empty so the    // worktree config's `[env]` cannot put it back.    expect(env.get("COTTON_RUSTC_CACHE_WRAPPER")).toEqual(["", true]);    expect(env.get("RUSTC_WRAPPER")).toEqual(["", true]);  });  it("honours an explicit job limit on a shared Mac", () => {    const env = handedToTheLock(["--debug", "--simulator-only"], { CARGO_BUILD_JOBS: "6" });    expect(env.get("CARGO_BUILD_JOBS")?.[0]).toBe("6");  });  it("names its target once, however many times the lock re-exec runs it", () => {    const env = handedToTheLock(["--debug", "--simulator-only"], { CARGO_TARGET_DIR: join(work, "target", "shared-sim-dev") });    expect(env.get("CARGO_TARGET_DIR")?.[0]).toBe(join(work, "target", "shared-sim-dev"));  });  it.each([[["--simulator-only"]], [["--debug"]], [["--debug", "--device-only"]], [[]]])(    "%j leaves the shared, non-incremental build as it was",    (args) => {      const env = handedToTheLock(args);      expect(env.get("CARGO_INCREMENTAL")?.[0]).toBe("0");      expect(env.get("CARGO_TARGET_DIR")?.[0]).toBe(join(work, "target", "shared"));      expect(env.get("CARGO_BUILD_JOBS")?.[0]).toBe("3");      expect(env.get("CARGO_PROFILE_DEV_LTO")?.[1]).toBe(false);      expect(env.get("COTTON_RUSTC_CACHE_WRAPPER")?.[1]).toBe(false);    },  );  it("the dry run names the mode", () => {    const out = spawnSync("bash", [SCRIPT, "--debug", "--simulator-only", "--dry-run"], { cwd: REPO_ROOT, encoding: "utf8" });    expect(out.stdout.trim()).toBe("[xcframework] would build: simulator (debug, incremental)");  });});