#!/usr/bin/env python3 """Generate native/WASM evidence for LinuxCNC 5axiskins.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path INPUTS = { "source": "linuxcnc/src/emc/kinematics/5axiskins.c", "config": "linuxcnc/configs/sim/axis/vismach/5axis/bridgemill/5axis.ini", "hal": "linuxcnc/configs/sim/axis/vismach/5axis/bridgemill/5axisgui.hal", "header": "wasm-core/include/sim_kinematics.h", "source_impl": "wasm-core/src/sim_kinematics.c", "test": "wasm-core/tests/sim_kinematics_test.c", } def sha256(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def run_sample(command: list[str], root: Path) -> dict[str, object]: completed = subprocess.run(command, cwd=root, check=False, text=True, capture_output=True) if completed.returncode != 0: raise RuntimeError(f"{command[0]} exited {completed.returncode}: {completed.stderr.strip()}") for line in reversed(completed.stdout.splitlines()): if line.startswith("{"): return json.loads(line) raise RuntimeError(f"{command[0]} did not emit a kinematics sample") def generate(root: Path, output: Path) -> dict[str, object]: root = root.resolve() native_all = run_sample([str(root / "build-native/sim_kinematics_test")], root) wasm_all = run_sample(["node", str(root / "build-wasm/sim_kinematics_wasm_test.js")], root) native = native_all["fiveaxiskins"] wasm = wasm_all["fiveaxiskins"] config_text = (root / INPUTS["config"]).read_text(encoding="utf-8") source_text = (root / INPUTS["source"]).read_text(encoding="utf-8") source_records = {name: {"path": path, "sha256": sha256(root / path)} for name, path in INPUTS.items()} native_json = json.dumps(native, ensure_ascii=False, sort_keys=True, separators=(",", ":")) wasm_json = json.dumps(wasm, ensure_ascii=False, sort_keys=True, separators=(",", ":")) report = { "schema": "fiveaxis-kinematics-report.v1", "status": "partial", "implementation_status": "verified", "source_records": source_records, "configuration": { "path": INPUTS["config"], "kinematics": "5axiskins", "coordinates": "xyzbcwy", "joints": 7, "pivot_length_default": 250.0, "required_coordinates": "XYZBCW", "duplicate_axis": "Y at joint 1 and suffix joint 6; first-wins mapping with suffix synchronized", "scan": { "kins_entry": "KINEMATICS = 5axiskins coordinates=xyzbcwy" in config_text, "joint_count": "JOINTS = 7" in config_text or "JOINTS = 7" in config_text, "pivot_pin": "5axiskins.pivot-length" in (root / INPUTS["hal"]).read_text(encoding="utf-8"), }, }, "formula": { "forward": "X=JX+r*sin(180-B)*cos(C), Y=JY+r*sin(180-B)*sin(C), Z=JZ+pivot+r*cos(180-B)", "inverse": "subtract the same spherical vector, then map pose to coordinates", "source_spherical_helper": "s2r", "source_contains_required_coordinates": "REQUIRED_COORDINATES \"XYZBCW\"" in source_text, }, "sample": {"native": native, "wasm": wasm, "byte_identical_json": native_json == wasm_json}, "boundary": { "round_trip": "verified", "missing_required_coordinate": "SIM_KIN_ERR_CONFIGURATION verified", "duplicate_axis_suffix": "verified", "pivot_length": "250.0 verified", "state_hash": "native/WASM identical", }, "oracle": {"status": "blocked", "reason": "linuxcnc/unit_tests/kinematics is absent from the locked source"}, "native_wasm": {"status": "passed", "native_command": "build-native/sim_kinematics_test", "wasm_command": "node build-wasm/sim_kinematics_wasm_test.js"}, } report["report_sha256"] = hashlib.sha256(json.dumps(report, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode("utf-8")).hexdigest() output.parent.mkdir(parents=True, exist_ok=True) output.write_text(json.dumps(report, ensure_ascii=False, sort_keys=True, indent=2) + "\n", encoding="utf-8") return report def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--repo-root", type=Path, default=Path(__file__).resolve().parents[1]) parser.add_argument("--output", type=Path, default=Path("coverage/fiveaxis-kinematics.report.json")) args = parser.parse_args() report = generate(args.repo_root, args.repo_root / args.output) print(f"fiveaxis={report['status']} native_wasm={report['native_wasm']['status']} oracle={report['oracle']['status']}") return 0 if __name__ == "__main__": raise SystemExit(main())