72 lines
3.6 KiB
Python
72 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
# SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
"""Ask the pinned Mojo compiler (on Compiler Explorer) about every Mojo reference implementation of the library,
|
|
and store each answer beside the routine, in golden/mojo-<level>.json. Network; standard library only.
|
|
|
|
node scripts/library.mjs mojo-sources | python3 scripts/library_godbolt.py [--force] [--dry-run]
|
|
|
|
Each program is the reference's source with a main that reads every case's arguments from one line of stdin and
|
|
prints each result (scripts/library.mjs builds both, and sends the same program and line to core's interpreter and
|
|
the local compiler): the program holds no case, so the answer is the compiled routine's on run-time inputs, not
|
|
the compiler's working out a call on literals while it builds. Each is built with the pin's build.args (library/mojo/
|
|
pin.json), as every Mojo program is. An answer is keyed by the SHA-256 of that exact program, the stdin it was given,
|
|
the build arguments and the compiler id, so editing a reference or a case, or the build arguments, makes it
|
|
unverified until it is asked again; an answer already stored for the same program, stdin and arguments is not asked
|
|
again unless --force.
|
|
pixi run library-check then compares the stored output with the pseudo's results.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import sys
|
|
from datetime import date
|
|
from pathlib import Path
|
|
|
|
CORE = Path(__file__).resolve().parent.parent
|
|
sys.path.insert(0, str(CORE / "scripts" / "mojo"))
|
|
from mojocorpus import godbolt # noqa: E402
|
|
from mojocorpus.common import PipelineError, build_args, load_pin # noqa: E402
|
|
|
|
|
|
def main(argv: list[str]) -> int:
|
|
force = "--force" in argv
|
|
dry = "--dry-run" in argv
|
|
pin = load_pin()
|
|
base, compiler, timeout = pin.base, pin.compiler, pin.timeout
|
|
args = build_args(pin.build) # a reference program has no arguments of its own
|
|
items = json.load(sys.stdin)
|
|
asked = failed = 0
|
|
for it in items:
|
|
src, stdin = it["source"], it["stdin"]
|
|
sha = hashlib.sha256(src.encode("utf-8")).hexdigest()
|
|
out = Path(it["golden"])
|
|
if out.exists() and not force:
|
|
old = json.loads(out.read_text())
|
|
if old.get("source_sha256") == sha and old.get("stdin") == stdin and old.get("args") == args and old.get("compiler") == compiler:
|
|
print(f"[library] {it['id']}: stored answer is current")
|
|
continue
|
|
if dry:
|
|
print(f"[library] {it['id']}: would ask {base} ({compiler}), {len(src)} bytes and {len(stdin)} of stdin")
|
|
continue
|
|
try:
|
|
data = godbolt.post(base, compiler, godbolt.request_body(src, "run", stdin=stdin, args=args), timeout=timeout)
|
|
except PipelineError as e: # godbolt.post has already retried; leave this one unverified and go on
|
|
failed += 1
|
|
print(f"[library] {it['id']}: {e}")
|
|
continue
|
|
result = godbolt.normalize("run", data)
|
|
out.parent.mkdir(parents=True, exist_ok=True)
|
|
out.write_text(json.dumps({"tool": "godbolt", "compiler": compiler, "mojoVersion": pin.version,
|
|
"source_sha256": sha, "stdin": stdin, "args": args, "asked": date.today().isoformat(), "result": result},
|
|
indent=2, ensure_ascii=False) + "\n")
|
|
asked += 1
|
|
print(f"[library] {it['id']}: compiled={result['compiled']} exit={result['exitCode']}")
|
|
print(f"[library] asked {asked} program(s){f', {failed} failed' if failed else ''}")
|
|
return 1 if failed else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main(sys.argv[1:]))
|