| 1 | """Build a QA-only Go runner with the Apple VM setup view bound to one temporary instance.""" |
| 2 | |
| 3 | import argparse |
| 4 | import json |
| 5 | import shutil |
| 6 | import subprocess |
| 7 | import tempfile |
| 8 | from pathlib import Path |
| 9 | |
| 10 | |
| 11 | SCRIPTS = Path(__file__).resolve().parent |
| 12 | REPO = SCRIPTS.parents[1] |
| 13 | VZ = "github.com/Code-Hex/vz/v3" |
| 14 | |
| 15 | |
| 16 | def run(*command): |
| 17 | subprocess.run(command, check=True, cwd=REPO) |
| 18 | |
| 19 | |
| 20 | def replace_once(text, old, new): |
| 21 | if text.count(old) != 1: |
| 22 | raise SystemExit(f"Upstream hook changed. Inspect the source containing {old!r}.") |
| 23 | return text.replace(old, new) |
| 24 | |
| 25 | |
| 26 | def main(): |
| 27 | parser = argparse.ArgumentParser(description=__doc__) |
| 28 | parser.add_argument("instance", type=Path, help="Directory of the disposable Lima macOS instance") |
| 29 | parser.add_argument("output", type=Path, help="New directory for the signed QA runner and its library") |
| 30 | parser.add_argument("--runner", default="./cmd/cage", help="Go main package which owns the fixture runtime") |
| 31 | args = parser.parse_args() |
| 32 | instance = args.instance.resolve(strict=True) |
| 33 | temp_roots = {Path(tempfile.gettempdir()).resolve(), Path("/private/tmp").resolve()} |
| 34 | if not instance.is_dir() or not any(root in instance.parents for root in temp_roots): |
| 35 | parser.error("Select an instance inside a disposable temporary directory.") |
| 36 | out = args.output.resolve() |
| 37 | out.mkdir(mode=0o700) |
| 38 | dylib = out / "libcagesetup.dylib" |
| 39 | run("xcrun", "clang", "-target", "arm64-apple-macos14.0", "-fobjc-arc", "-Wall", "-Wextra", "-Werror", |
| 40 | "-dynamiclib", SCRIPTS / "macos-setup-view.m", SCRIPTS / "macos-setup-bridge.m", |
| 41 | "-framework", "AppKit", "-framework", "Virtualization", "-o", dylib) |
| 42 | with tempfile.TemporaryDirectory(prefix="cage-setup-build-") as scratch: |
| 43 | work = Path(scratch) |
| 44 | module = json.loads(subprocess.check_output(["go", "list", "-m", "-json", VZ], cwd=REPO)) |
| 45 | if module.get("Version") != "v3.7.1": |
| 46 | raise SystemExit("Review the native bridge for the current Code-Hex/vz version.") |
| 47 | source = Path(module["Dir"]) |
| 48 | copy = work / "vz" |
| 49 | shutil.copytree(source, copy) |
| 50 | copy.chmod(0o700) |
| 51 | shutil.copyfile(SCRIPTS / "macos-setup-bridge.h", copy / "cage_setup_bridge.h") |
| 52 | (copy / dylib.name).symlink_to(dylib) |
| 53 | binding = copy / "virtualization.go" |
| 54 | text = replace_once(binding.read_text(), '# include "virtualization_15.h"', |
| 55 | '# include "virtualization_15.h"\n# include "cage_setup_bridge.h"') |
| 56 | text = replace_once(text, "#cgo darwin LDFLAGS: -lobjc", |
| 57 | "#cgo darwin LDFLAGS: -L${SRCDIR} -lcagesetup -lobjc") |
| 58 | binding.chmod(0o600) |
| 59 | binding.write_text(replace_once(text, "C.startVirtualMachineWindow(", "C.CageRunSetupBridge(")) |
| 60 | |
| 61 | driver = REPO / "third_party/lima/pkg/driver/vz/vz_driver_darwin.go" |
| 62 | contents = driver.read_text() |
| 63 | start = contents.index("func (l *LimaVzDriver) canRunGUI() bool {") |
| 64 | end = contents.index("\n}", start) |
| 65 | selected = json.dumps(str(instance), ensure_ascii=False) |
| 66 | hook = ("func (l *LimaVzDriver) canRunGUI() bool {\n" |
| 67 | f"\treturn l.Instance.Dir == {selected} && *l.Instance.Config.OS == limatype.DARWIN " |
| 68 | '&& *l.Instance.Config.Video.Display == "offscreen"') |
| 69 | modified = work / "driver.go" |
| 70 | modified.write_text(contents[:start] + hook + contents[end:]) |
| 71 | runner = json.loads(subprocess.check_output(["go", "list", "-json", args.runner], cwd=REPO)) |
| 72 | runner_dir = Path(runner["Dir"]).resolve() |
| 73 | if runner["Name"] != "main" or REPO not in runner_dir.parents: |
| 74 | parser.error("Choose a main package in this checkout.") |
| 75 | startup = work / "setup_thread.go" |
| 76 | startup.write_text('package main\nimport "runtime"\nfunc init() { runtime.LockOSThread() }\n') |
| 77 | overlay = work / "overlay.json" |
| 78 | overlay.write_text(json.dumps({"Replace": { |
| 79 | str(driver): str(modified), str(runner_dir / "cage_setup_qa_thread_darwin.go"): str(startup), |
| 80 | }})) |
| 81 | mod = work / "qa.mod" |
| 82 | shutil.copyfile(REPO / "go.mod", mod) |
| 83 | shutil.copyfile(REPO / "go.sum", work / "qa.sum") |
| 84 | run("go", "mod", "edit", "-modfile", mod, "-replace", f"{VZ}={copy}") |
| 85 | run("go", "build", "-modfile", mod, "-overlay", overlay, "-o", out / "qa-runner", args.runner) |
| 86 | run("codesign", "-f", "-s", "-", "--entitlements", REPO / "third_party/lima/vz.entitlements", out / "qa-runner") |
| 87 | |
| 88 | |
| 89 | if __name__ == "__main__": |
| 90 | main() |