#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Build drhelius' Lynx Hardware Tests (MIT) from source with cc65. The board's main judge is these ROMs; under the restart rules no binary shipped by a reference is run, so they are built here from the pinned sources in ``reference/src/lynx-tests`` (``scripts/fetch_references.py``). Each suite's sources and the shared directory are copied to ``tests/test_roms/build/`` and compiled there, following the suite's Makefile and ``shared/lynxcc65.mak`` (compile flags ``-g -t lynx -O -Or -Cl -Os``; link with ``-Ln`` for the label file the harness reads ``_g_results`` from). cc65 is found through ``$CC65_HOME`` (its ``bin/`` holds cc65, ca65, cl65), else the pinned tree ``reference/src/cc65`` once built with ``make``, else ``PATH``. Release 2.19 is too old (its ``lynx.inc`` lacks symbols the tests use). python scripts/build_lynx_tests.py # every suite python scripts/build_lynx_tests.py cpu timers # some """ from __future__ import annotations import os import shutil import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SOURCE = ROOT / "reference" / "src" / "lynx-tests" BUILD = ROOT / "tests" / "test_roms" / "build" SUITES = [ "audio", "audio2", "cpu", "math", "memio", "page-mode", "sdoneack", "sprites1", "sprites2", "sprites3", "sprites4", "sprites5", "timers", "timers2", "uart", "uart2", "uart3", "refresh-rate", ] CFLAGS = ["-g", "-t", "lynx", "-O", "-Or", "-Cl", "-Os"] SEGMENTS = [ "--code-name", "CODE", "--rodata-name", "RODATA", "--bss-name", "BSS", "--data-name", "DATA", ] # fmt: skip PINNED_CC65 = ROOT / "reference" / "src" / "cc65" # pinned_repos.json, built with make if not os.environ.get("CC65_HOME") and (PINNED_CC65 / "bin" / "cl65").is_file(): os.environ["CC65_HOME"] = str(PINNED_CC65) def tool(name: str) -> str: home = os.environ.get("CC65_HOME") if home and (Path(home) / "bin" / name).is_file(): return str(Path(home) / "bin" / name) found = shutil.which(name) if not found: sys.exit(f"{name} not found: set CC65_HOME to a cc65 installation") return found def objects(suite_dir: Path) -> list[str]: for line in (suite_dir / "Makefile").read_text().splitlines(): if line.startswith("objects"): return line.split("=", 1)[1].split() raise SystemExit(f"{suite_dir}/Makefile names no objects") def build(suite: str) -> Path: work = BUILD / suite if work.exists(): shutil.copytree(SOURCE / suite, work, dirs_exist_ok=True) else: shutil.copytree(SOURCE / suite, work) shutil.copytree(SOURCE / "shared", BUILD / "shared", dirs_exist_ok=True) env = dict(os.environ) home = os.environ.get("CC65_HOME") asminc = str(Path(home) / "asminc") if home else "" built = [] for obj in objects(work): stem = obj[:-2] if (work / f"{stem}.c").exists(): subprocess.run( [tool("cc65"), *SEGMENTS, *CFLAGS, "-I", ".", "-I", "../shared", f"{stem}.c"], cwd=work, check=True, env=env, ) # fmt: skip subprocess.run([tool("ca65"), "-o", obj, "-g", f"{stem}.s"], cwd=work, check=True) (work / f"{stem}.s").replace(work / f"{stem}.c.s") else: command = [tool("ca65"), "-t", "lynx", "-o", obj, "-g", f"{stem}.s"] if asminc: command[3:3] = ["-I", asminc] subprocess.run(command, cwd=work, check=True) built.append(obj) target = f"{suite}.lnx" subprocess.run( [tool("cl65"), "-Ln", f"{suite}.sym", "-t", "lynx", "-o", target, *built, "lynx.lib"], cwd=work, check=True, ) # fmt: skip return work / target def main(argv: list[str]) -> int: suites = argv or list(SUITES) for suite in suites: path = build(suite) print(f"built {path.relative_to(ROOT)} ({path.stat().st_size} bytes)") return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))