File size: 8,882 Bytes
fa19039 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | #!/usr/bin/env python3
"""Build the pinned macOS pilot cells; preserve commands, .ii, dSYM and inputs."""
import argparse, hashlib, json, shutil, subprocess, tarfile, time
from pathlib import Path
def sha(p):
h = hashlib.sha256()
with p.open("rb") as f:
for block in iter(lambda: f.read(1024 * 1024), b""):
h.update(block)
return h.hexdigest()
def run(cmd, cwd, log):
log.write(json.dumps({"command": cmd, "cwd": str(cwd)}) + "\n")
log.flush()
subprocess.run(cmd, cwd=cwd, stdout=log, stderr=log, check=True)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--root", type=Path, required=True)
ap.add_argument("--plan", type=Path, required=True)
a = ap.parse_args()
root = a.root.resolve()
plan = json.loads(a.plan.read_text())
base = root / "staged/qualification-v2"
for c in plan["cells"]:
if c["platform"] != "macos":
continue
src = root / "sources" / c["project"]
assert (
subprocess.check_output(
["git", "-C", str(src), "rev-parse", "HEAD"], text=True
).strip()
== c["source_revision"]
)
owned = base / "sources" / c["project"]
owned.mkdir(parents=True, exist_ok=True)
subprocess.run(
[
"git",
"-C",
str(src),
"archive",
"--format=tar.gz",
"--output",
str(owned / "original.tar.gz"),
"HEAD",
],
check=True,
)
for mode, opt in [
("O0", "-O0"),
("O2", "-O2"),
("O2-noinline", "-O2 -fno-inline"),
]:
build = root / "builds/macos" / c["project"] / mode
out = base / "macos/arm64" / mode / c["project"]
out.mkdir(parents=True, exist_ok=True)
build.mkdir(parents=True, exist_ok=True)
status = {
"project": c["project"],
"platform": "macos",
"optimization": mode,
"source_revision": c["source_revision"],
"state": "building",
"scoring_ready": False,
}
started = time.monotonic()
try:
flags = opt + " -g -fno-builtin -save-temps=obj"
configure = [
"cmake",
"-S",
str(src),
"-B",
str(build),
"-G",
"Unix Makefiles",
"--fresh",
"-DCMAKE_BUILD_TYPE=Qualification",
"-DCMAKE_C_FLAGS=" + flags,
"-DCMAKE_CXX_FLAGS=" + flags,
"-DCMAKE_C_FLAGS_QUALIFICATION=-DNDEBUG",
"-DCMAKE_CXX_FLAGS_QUALIFICATION=-DNDEBUG",
"-DCMAKE_OSX_ARCHITECTURES=arm64",
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
"-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=OFF",
"-DBUILD_TESTING=OFF",
]
if c["project"] == "snappy":
configure += [
"-DBUILD_SHARED_LIBS=ON",
"-DSNAPPY_BUILD_TESTS=OFF",
"-DSNAPPY_BUILD_BENCHMARKS=OFF",
]
target = "snappy"
image = build / "libsnappy.dylib"
elif c["project"] == "ninja":
target = "ninja"
image = build / "ninja"
else:
configure += [
"-DBUILD_SHARED_LIBS=OFF",
"-DENABLE_GTEST=OFF",
"-DENABLE_BENCHMARK=OFF",
"-DBUILD_PYTHON=OFF",
"-DBUILD_OPENCC_JIEBA_PLUGIN=OFF",
]
target = "opencc"
image = build / "src/tools/opencc"
with (out / "build.log").open("w") as log:
run(configure, src, log)
run(
[
"cmake",
"--build",
str(build),
"--target",
target,
"--parallel",
"3",
],
src,
log,
)
if not image.exists():
options = [
p
for p in build.rglob("opencc")
if p.is_file() and not p.is_symlink()
]
assert len(options) == 1, options
image = options[0]
binary = out / image.name
shutil.copy2(image, binary)
run(
[
"xcrun",
"dsymutil",
str(image),
"-o",
str(out / (image.name + ".dSYM")),
],
src,
log,
)
run(
[
"xcrun",
"dwarfdump",
"--verify",
str(out / (image.name + ".dSYM")),
],
src,
log,
)
with (out / "debug-uuid.txt").open("w") as uuid:
subprocess.run(
[
"xcrun",
"dwarfdump",
"--uuid",
str(binary),
str(out / (image.name + ".dSYM")),
],
stdout=uuid,
check=True,
)
stripped = out / (image.name + ".stripped")
shutil.copy2(binary, stripped)
run(["llvm-strip", "--strip-all", str(stripped)], src, log)
for path, label in [(binary, "original"), (stripped, "stripped")]:
with (out / (label + "-sections.txt")).open("w") as dest:
subprocess.run(
[
"llvm-readobj",
"--file-headers",
"--sections",
str(path),
],
stdout=dest,
check=True,
)
shutil.copy2(
build / "compile_commands.json", out / "compile_commands.json"
)
preprocessed = out / "preprocessed.tar.gz"
iis = [
p
for p in build.rglob("*.ii")
if "CompilerId" not in str(p) and "CMakeScratch" not in str(p)
]
assert iis, "No preprocessed TUs"
with tarfile.open(preprocessed, "w:gz") as tar:
for p in sorted(iis):
tar.add(p, arcname=str(p.relative_to(build)))
status.update(
state="artifact-built-debug-verified",
binary=str(binary.relative_to(base)),
binary_sha256=sha(binary),
stripped=str(stripped.relative_to(base)),
stripped_sha256=sha(stripped),
preprocessed_tus=len(iis),
compiler=subprocess.check_output(
["xcrun", "clang++", "--version"], text=True
).splitlines()[0],
effective_flags=flags + " -DNDEBUG",
remaining_gates=[
"source-owned function identity extraction",
"source/ABI type ground truth adapter",
"code-section parity and no-debug input verification",
],
)
except Exception as e:
status.update(state="failed", error=str(e))
status["elapsed_seconds"] = time.monotonic() - started
(out / "status.json").write_text(json.dumps(status, indent=2))
print(json.dumps(status), flush=True)
if __name__ == "__main__":
main()
|