#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Build the referees from the pinned source trees into reference/build/. - ``reference/build/nba_sst``: NanoBoyAdvance's ARM7TDMI (the pinned Codeberg master) on stand-in Bus and Scheduler headers (``nba/shim``), run one SingleStepTests case at a time by ``nba/nba_sst.cc``. It records every bus cycle NBA makes, internal ones included, which the corpus does not. The driver and the shim headers are a GPL tool kept outside this repository (referees/README.md), at its documented path ``$EMU_TOOLS_GPL/tools/arm7tdmi-python/nba/`` (``EMU_TOOLS_GPL`` names a checkout of emu-tools-gpl); without it this referee is skipped and the others are built. - ``reference/build/mul_model.so``: zayd's Booth-multiplier model (``multiplication-algorithm``) behind ``referees/multiply/mul_model.c``, for ``scripts/multiply_model.py``. - ``reference/build/mgba_disasm``: mGBA's ARM and Thumb disassembler (the pinned tree's ``src/arm/decoder*.c``) behind ``referees/mgba/mgba_disasm.c``, for ``scripts/disasm_judge.py``. Needs ``g++`` with C++20 (GCC 11 or newer) and ``gcc``. Takes about 20 seconds. python scripts/fetch_references.py --no-repos # (once) then the trees: python scripts/fetch_references.py python scripts/build_referees.py python scripts/referee_sst.py # score NBA against the corpus python scripts/multiply_model.py # the corpus's multiplies against the model python scripts/disasm_judge.py # the disassembler against mGBA and MAME """ from __future__ import annotations import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] NBA = ROOT / "reference" / "src" / "NanoBoyAdvance" / "src" / "nba" BUILD = ROOT / "reference" / "build" # The NBA driver is a GPL tool kept outside the repository (referees/README.md), at a # fixed path under the emu-tools-gpl checkout that EMU_TOOLS_GPL names. NBA_DRIVER = Path("tools") / "arm7tdmi-python" / "nba" def build_nba() -> int: root = os.environ.get("EMU_TOOLS_GPL") driver = Path(root) / NBA_DRIVER if root else None if driver is None or not (driver / "nba_sst.cc").exists(): print( f"skipping nba_sst: set EMU_TOOLS_GPL (the driver is $EMU_TOOLS_GPL/{NBA_DRIVER}/)", file=sys.stderr, ) return 0 if not (NBA / "src" / "arm" / "arm7tdmi.hh").exists(): print("NanoBoyAdvance is not fetched; run scripts/fetch_references.py", file=sys.stderr) return 1 BUILD.mkdir(parents=True, exist_ok=True) command = [ "g++", "-std=c++20", "-O2", f"-I{driver / 'shim'}", f"-I{NBA / 'src'}", f"-I{NBA / 'include'}", str(driver / "nba_sst.cc"), "-o", str(BUILD / "nba_sst"), ] print(" ".join(command)) result = subprocess.run(command, check=False) if result.returncode == 0: print(f"built {(BUILD / 'nba_sst').relative_to(ROOT)}") return result.returncode def build_multiply() -> int: source = ROOT / "reference" / "src" / "multiplication-algorithm" if not (source / "impl.h").exists(): print("multiplication-algorithm is not fetched", file=sys.stderr) return 1 BUILD.mkdir(parents=True, exist_ok=True) command = [ "gcc", "-O2", "-shared", "-fPIC", f"-I{source}", str(ROOT / "referees" / "multiply" / "mul_model.c"), "-o", str(BUILD / "mul_model.so"), ] print(" ".join(command)) result = subprocess.run(command, check=False) if result.returncode == 0: print(f"built {(BUILD / 'mul_model.so').relative_to(ROOT)}") return result.returncode def build_mgba_disasm() -> int: mgba = ROOT / "reference" / "src" / "mgba" if not (mgba / "src" / "arm" / "decoder.c").exists(): print("mgba is not fetched", file=sys.stderr) return 1 BUILD.mkdir(parents=True, exist_ok=True) command = [ "gcc", "-O1", "-std=gnu11", "-DENABLE_DEBUGGERS", "-DHAVE_STRLCPY", # glibc 2.38+ has strlcpy; mgba-util's own drags in its text codecs f"-I{ROOT / 'referees' / 'mgba' / 'shim'}", f"-I{mgba / 'include'}", f"-I{mgba / 'src'}", str(ROOT / "referees" / "mgba" / "mgba_disasm.c"), *( str(mgba / "src" / "arm" / name) for name in ("decoder.c", "decoder-arm.c", "decoder-thumb.c") ), "-o", str(BUILD / "mgba_disasm"), ] print(" ".join(command)) result = subprocess.run(command, check=False) if result.returncode == 0: print(f"built {(BUILD / 'mgba_disasm').relative_to(ROOT)}") return result.returncode def main() -> int: return build_nba() or build_multiply() or build_mgba_disasm() if __name__ == "__main__": sys.exit(main())