/** * `setup-libclang.sh` ON A MAC NAMES XCODE'S COPY AND STOPS. * * mupdf-sys runs bindgen at BUILD time, so every Rust lane needs a libclang * that carries its own builtin headers. Until the `rust` job became a * `cotton-rust` job it only ever ran on the Linux host, and the whole script is * shaped for that one machine: an `/usr/lib/llvm-*` probe, and a download of * LLVM's `x86_64-linux-gnu` tarball. Run unchanged on a Mac it would probe a * directory that is not there and then fetch a library that cannot load. * * The Macs have in fact been building mupdf-sys for as long as the XCFramework * has existed — `scripts/build-xcframework.sh` runs the same bindgen over the * same headers — because the toolchain under `xcode-select -p` carries libclang * and its builtin headers together. So the Darwin branch names that copy. * * WHAT THIS PROVES, and why it is a stubbed run rather than a read: the branch * has to answer correctly on a machine WITHOUT Xcode too, and the only honest * answer there is a refusal by name. A script that fell through to the Linux * path would fail three steps later on a tarball, which is the failure this * whole file exists to keep out of a log. `xcode-select` is stubbed on PATH so * both answers can be made to happen on demand. */ import { spawnSync } from "node:child_process"; import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterAll, describe, expect, it } from "vitest"; const ROOT = join(__dirname, "..", ".."); const SCRIPT = join(ROOT, "scripts", "ci", "setup-libclang.sh"); const temps: string[] = []; afterAll(() => { for (const dir of temps.splice(0)) rmSync(dir, { recursive: true, force: true }); }); function scratch(): string { const dir = mkdtempSync(join(tmpdir(), "setup-libclang-")); temps.push(dir); return dir; } /** * Run the script with `uname` forced to Darwin and `xcode-select` stubbed. * * `uname` is stubbed as well as `xcode-select` so the Darwin branch is exercised * on the Linux CI host too — a guard that only runs on a Mac is a guard the lane * does not have. */ function run(options: { developer: string | null; libclang: boolean }) { const dir = scratch(); const bin = join(dir, "bin"); mkdirSync(bin, { recursive: true }); writeFileSync(join(bin, "uname"), '#!/bin/sh\nif [ "$1" = "-s" ]; then echo Darwin; else echo Darwin; fi\n'); chmodSync(join(bin, "uname"), 0o755); writeFileSync( join(bin, "xcode-select"), options.developer === null ? "#!/bin/sh\nexit 1\n" : `#!/bin/sh\nprintf '%s\\n' ${JSON.stringify(options.developer)}\n` ); chmodSync(join(bin, "xcode-select"), 0o755); if (options.developer && options.libclang) { const lib = join(options.developer, "Toolchains", "XcodeDefault.xctoolchain", "usr", "lib"); mkdirSync(lib, { recursive: true }); writeFileSync(join(lib, "libclang.dylib"), ""); } const env = join(dir, "github-env"); writeFileSync(env, ""); const result = spawnSync("bash", [SCRIPT], { encoding: "utf8", env: { ...process.env, PATH: `${bin}:${process.env.PATH ?? ""}`, GITHUB_ENV: env, HOME: dir }, }); return { ...result, wrote: readFileSync(env, "utf8") }; } describe("setup-libclang.sh on a Mac", () => { it("is present and readable — this guard runs it by path", () => { expect(existsSync(SCRIPT), `${SCRIPT} is missing`).toBe(true); }); it("names the selected toolchain's libclang and fetches nothing", () => { const developer = join(scratch(), "Developer"); const result = run({ developer, libclang: true }); expect(result.status, result.stderr).toBe(0); // The LINUX halves must not have run: no tarball, no /usr/lib probe result. expect(result.stdout).not.toMatch(/linux-gnu|curl|tar /); expect(result.wrote.trim()).toBe( `LIBCLANG_PATH=${join(developer, "Toolchains", "XcodeDefault.xctoolchain", "usr", "lib")}` ); }); it("REFUSES BY NAME when no toolchain is selected, rather than falling through to the Linux path", () => { const result = run({ developer: null, libclang: false }); expect(result.status, result.stdout).not.toBe(0); expect(result.stderr).toMatch(/no Xcode toolchain is selected/); // A refusal writes nothing: a half-written LIBCLANG_PATH would be read by // every cargo step after it in the same job. expect(result.wrote.trim()).toBe(""); }); it("REFUSES BY NAME when the selected toolchain carries no libclang", () => { const developer = join(scratch(), "Developer"); const result = run({ developer, libclang: false }); expect(result.status, result.stdout).not.toBe(0); expect(result.stderr).toMatch(/carries no libclang/); expect(result.stderr).toContain(developer); expect(result.wrote.trim()).toBe(""); }); });