#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """Fetch hoglet67's 6502Decoder at its pin and build ``decode6502``. python3 -I build.py # build/decode6502 python3 -I build.py --version # 6502decoder-build 0.1.0 (6502Decoder c32dd60) Part of the 6502decoder tool in emu-tools-gpl (README.md; GPL-3.0-or-later). The build step was moved here on 2026-11-25 (decisions page T3) from w65c02-python's ``scripts/build_tools.py``, where alewman first wrote it under MIT (``../../notices/MIT-emulator-family.txt``); the program is the one that step built: ``gcc -O2 -D_GNU_SOURCE`` of the decoder's thirteen C files, with libm. The source lands in ``upstream/6502Decoder-/`` (GitHub's archive of the commit), the program at ``build/decode6502``; both are git-ignored and the decoder is never committed. ``build/build-info.json`` records the pin and the options. Exit status as the family's tool contract says: 0 built (or already built), 2 a usage error, 3 the source could not be fetched, anything else a failed build. Nothing is ever deleted. """ from __future__ import annotations import json import shutil import subprocess import sys import tarfile import tempfile import time from pathlib import Path from urllib.request import Request, urlopen TOOL, VERSION = "6502decoder-build", "0.1.0" HERE = Path(__file__).resolve().parent UPSTREAM_DIR = HERE / "upstream" BUILD = HERE / "build" PROGRAM = BUILD / "decode6502" REPOSITORY = "https://github.com/hoglet67/6502Decoder" PIN = "c32dd60091493558e06e1c682dbea50b904a9fdc" # 2026-10-22 EXIT_OK, EXIT_USAGE, EXIT_MISSING = 0, 2, 3 #: The decoder's C files, in the order w65c02-python's build_tools.py compiled them. SOURCES = ( "main.c", "memory.c", "em_6502.c", "em_65816.c", "em_6800.c", "em_scmp.c", "profiler.c", "profiler_instr.c", "profiler_block.c", "profiler_call.c", "tube_decode.c", "musl_tsearch.c", "symbols.c", ) # fmt: skip OPTIONS = ["-O2", "-D_GNU_SOURCE"] def fetch() -> Path: target = UPSTREAM_DIR / f"6502Decoder-{PIN[:12]}" if (target / ".complete").exists(): return target if target.exists(): raise SystemExit(f"{target} is incomplete (an interrupted fetch?): move it aside first") UPSTREAM_DIR.mkdir(parents=True, exist_ok=True) url = f"{REPOSITORY}/archive/{PIN}.tar.gz" print(f"downloading {url}", flush=True) with tempfile.TemporaryDirectory(dir=UPSTREAM_DIR) as temporary: archive = Path(temporary) / "source.tar.gz" try: request = Request(url, headers={"User-Agent": "emu-tools-gpl"}) with urlopen(request, timeout=300) as response, archive.open("wb") as out: shutil.copyfileobj(response, out, 1 << 20) except OSError as error: print(f"could not fetch {url}: {error}", file=sys.stderr) raise SystemExit(EXIT_MISSING) from error with tarfile.open(archive, "r:gz") as bundle: bundle.extractall(temporary, filter="data") (root,) = [p for p in Path(temporary).iterdir() if p.is_dir()] root.rename(target) (target / ".complete").write_text(PIN + "\n") return target def build() -> Path: if PROGRAM.exists(): print(f"already built: {PROGRAM}") return PROGRAM source = fetch() / "src" BUILD.mkdir(parents=True, exist_ok=True) command = ["nice", "-n", "10", "gcc", *OPTIONS, "-o", str(PROGRAM)] command += [str(source / name) for name in SOURCES] + ["-lm"] print("+", " ".join(command[3:]), flush=True) if subprocess.run(command).returncode: raise SystemExit(1) info = { "tool": TOOL, "tool_version": VERSION, "upstream": {"name": "6502Decoder", "url": REPOSITORY, "commit": PIN}, "options": OPTIONS, "was": "w65c02-python scripts/build_tools.py --only decoder", "built": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), } (BUILD / "build-info.json").write_text(json.dumps(info, indent=1) + "\n") print(f"built {PROGRAM}") return PROGRAM def main(argv: list[str]) -> int: if argv == ["--version"]: print(f"{TOOL} {VERSION} (6502Decoder {PIN[:7]})") return EXIT_OK if argv: print("usage: python3 -I build.py [--version]", file=sys.stderr) return EXIT_USAGE build() return EXIT_OK if __name__ == "__main__": raise SystemExit(main(sys.argv[1:]))