#!/usr/bin/env python3 """Build the referees from the trees scripts/fetch_references.py pinned. Everything lands under reference/build/ (ignored by git): - ``lua/``: Lua 5.4.7, a static library and headers, because the host has no Lua development package and mGBA's headless runner is scripted in Lua. - ``mgba/``: mGBA's core as ``libmgba.a`` (plus ``mgba-headless`` with Lua scripting; no Qt, SDL, OpenGL or FFmpeg) and ``mgba_driver``, built from scripts/mgba_driver.c, which scripts/referee_run.py drives. - ``nba/``: ``nba_driver`` and ``nba_trace``, built from scripts/nba_driver/ against NanoBoyAdvance's core library, when ``--nba`` is given. Nintendo's BIOS is not needed to build anything; the runs take the BIOS from the path the user gives (docs/start-here.md "BIOS"). python scripts/build_referees.py # Lua + mGBA python scripts/build_referees.py --nba # also NanoBoyAdvance """ from __future__ import annotations import argparse import os import shutil import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] REF = ROOT / "reference" BUILD = REF / "build" JOBS = str(os.cpu_count() or 4) def run(command: list[str], cwd: Path) -> None: print("+", " ".join(command)) subprocess.run(command, cwd=cwd, check=True) def build_lua() -> Path: source = REF / "tools" / "lua-5.4.7" prefix = BUILD / "lua" if (prefix / "lib" / "liblua.a").exists(): return prefix run(["make", "-j", JOBS, "linux", "MYCFLAGS=-fPIC"], source / "src") run(["make", "install", f"INSTALL_TOP={prefix}"], source) return prefix def build_mgba(lua: Path) -> Path: binary = BUILD / "mgba" / "mgba-headless" if binary.exists(): return binary off = [ "BUILD_QT", "BUILD_SDL", "BUILD_GL", "BUILD_GLES2", "BUILD_GLES3", "USE_FFMPEG", "USE_EDITLINE", "USE_MINIZIP", "USE_LIBZIP", "USE_SQLITE3", "USE_ELF", "USE_JSON_C", "USE_FREETYPE", "USE_LZMA", "USE_DISCORD_RPC", "USE_EPOXY", "ENABLE_GDB_STUB", "BUILD_SHARED", ] on = ["BUILD_HEADLESS", "BUILD_STATIC", "ENABLE_SCRIPTING", "USE_PNG", "USE_ZLIB"] flags = [f"-D{name}=OFF" for name in off] + [f"-D{name}=ON" for name in on] flags += [ "-DCMAKE_BUILD_TYPE=Release", f"-DLUA_INCLUDE_DIR={lua / 'include'}", f"-DLUA_LIBRARY={lua / 'lib' / 'liblua.a'}", "-DUSE_LUA=5.4", ] out = BUILD / "mgba" out.mkdir(parents=True, exist_ok=True) run(["cmake", "-G", "Ninja", str(REF / "src" / "mgba"), *flags], out) run(["ninja", "-j", JOBS, "mgba-headless"], out) return binary def build_mgba_driver() -> Path: """scripts/mgba_driver.c against libmgba.a, with the defines mGBA compiled with.""" out = BUILD / "mgba" binary = out / "mgba_driver" ninja = (out / "build.ninja").read_text(encoding="utf-8") # The struct layouts depend on these (ENABLE_SCRIPTING, ENABLE_DEBUGGERS, ...). block = ninja.split("src/core/core.c.o:", 1)[1] defines = block.split("DEFINES = ", 1)[1].split("\n", 1)[0].split() run( [ "cc", "-O2", "-flto=auto", *defines, "-o", str(binary), str(ROOT / "scripts" / "mgba_driver.c"), "-I", str(REF / "src" / "mgba" / "include"), "-I", str(out / "include"), str(out / "libmgba.a"), str(BUILD / "lua" / "lib" / "liblua.a"), "-lpng", "-lz", "-lm", "-lpthread", "-ldl", ], out, ) return binary def build_nba() -> Path: """scripts/nba_driver/ against NanoBoyAdvance's core library (no Qt, no Metal).""" out = BUILD / "nba" out.mkdir(parents=True, exist_ok=True) run( [ "cmake", "-G", "Ninja", str(ROOT / "scripts" / "nba_driver"), "-DCMAKE_BUILD_TYPE=Release", ], out, ) run(["ninja", "-j", JOBS, "nba_driver", "nba_trace"], out) return out / "nba_driver" def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--nba", action="store_true", help="also build NanoBoyAdvance's core") args = parser.parse_args() for tool in ("make", "cmake", "ninja", "cc"): if not shutil.which(tool): print(f"ERROR: {tool} not found") return 1 lua = build_lua() print("mGBA:", build_mgba(lua)) print("mGBA driver:", build_mgba_driver()) if args.nba: print("NanoBoyAdvance:", build_nba()) return 0 if __name__ == "__main__": sys.exit(main())