From dfd239266fd62e1fbbfcf264e9bb4e5c8e8dc801 Mon Sep 17 00:00:00 2001 From: Yash Dubey Date: Tue, 24 Nov 2026 07:35:50 +0530 Subject: [PATCH] make_game, first_light: a vk_pipeline_check or vk_harness older than its sources is rebuilt or reported, not reused silently --- make_game.py | 36 ++++++++++--- tests/test_first_light_harness_paths.py | 46 +++++++++++++++++ tests/test_make_game_pipeline_preflight.py | 59 ++++++++++++++++++++++ tools/first_light.py | 24 ++++++++- tools/stale_build.py | 50 ++++++++++++++++++ 5 files changed, 207 insertions(+), 8 deletions(-) create mode 100644 tools/stale_build.py diff --git a/make_game.py b/make_game.py index ff5ff7676..6ec27e33d 100644 --- a/make_game.py +++ b/make_game.py @@ -73,6 +73,7 @@ from mc_command import mc_command # noqa: E402 import leonida_tui # noqa: E402 from shader_stage_manifest import shader_stems # noqa: E402 from replace_retry import replace as replace_retry # noqa: E402 +from stale_build import newer_sources # noqa: E402 VERSIONS = os.path.join(ROOT, "game") STAGES = ["check", "shaders", "assets", "runtime", "pipelines"] @@ -900,12 +901,16 @@ def preflight(stages: list, work: str, wav: bool = False, runtime_build: str | N "Free some space or choose another drive with --work." + hint) if os.name != "nt" and not runtime_build and any(s in WINDOWS_ONLY for s in stages): cross_configure() - if "pipelines" in stages and os.name != "nt" and not pipeline_check_exe(): - build_pipeline_check(jobs) - if not pipeline_check_exe(): - fail("vk_pipeline_check built, but not as runtime/build/linux/vk_pipeline_check (a multi-config " - "generator puts it in a subfolder): unset CMAKE_GENERATOR if it names one, delete " - "runtime/build/linux and run the build again.") + if "pipelines" in stages and os.name != "nt": + checker = pipeline_check_exe() + # A checker built before a pull is rebuilt too; cmake --build only recompiles what changed. Without CMake + # the pipelines stage says it is out of date and uses it. + if not checker or (stale_checker(checker) and shutil.which("cmake")): + build_pipeline_check(jobs) + if not pipeline_check_exe(): + fail("vk_pipeline_check built, but not as runtime/build/linux/vk_pipeline_check (a multi-config " + "generator puts it in a subfolder): unset CMAKE_GENERATOR if it names one, delete " + "runtime/build/linux and run the build again.") if "assets" in stages: ensure_submodule() @@ -2369,6 +2374,18 @@ def pipeline_check_exe(runtime_build: str | None = None) -> str | None: return next((p for p in found if os.path.isfile(p) and (windows or os.access(p, os.X_OK))), None) +def stale_checker(exe: str) -> list: + """The sources of vk_pipeline_check changed after `exe` was built, when `exe` is in this checkout's runtime/build. + A checker from elsewhere (--runtime-build) isn't compared: its time stamp says nothing about these sources.""" + build = os.path.abspath(os.path.join(ROOT, "runtime", "build")) + try: + if os.path.commonpath([build, os.path.abspath(exe)]) != build: + return [] + except ValueError: # another drive + return [] + return newer_sources(exe, ROOT, "vk_pipeline_check") + + def require_translated_shaders(work: str) -> str: spirv = os.path.join(work, "shaders", "spirv") index = os.path.join(spirv, "index.json") @@ -2394,6 +2411,13 @@ def pipelines(work: str, out: str, jobs: "int | None" = None, *, runtime_build: fail("vk_pipeline_check wasn't built. With CMake 3.24+, a C++20 compiler and the Vulkan headers and " "loader installed, run: cmake -S runtime -B runtime/build/linux && " "cmake --build runtime/build/linux --parallel") + newer = stale_checker(exe) + if newer: + rebuild = ("run the runtime stage with VULKAN_SDK set" if os.name == "nt" else + "cmake --build runtime/build/linux --target vk_pipeline_check") + more = f" and {len(newer) - 3} more" if len(newer) > 3 else "" + log(f"pipelines: WARNING: {exe} is older than {', '.join(newer[:3])}{more}, so it is not this checkout's " + f"checker; to rebuild it, {rebuild}") spirv = require_translated_shaders(work) log("pipelines: creating every shader module and pipeline (compute and graphics) on this GPU") os.makedirs(out, exist_ok=True) diff --git a/tests/test_first_light_harness_paths.py b/tests/test_first_light_harness_paths.py index 58ab34800..438dce472 100644 --- a/tests/test_first_light_harness_paths.py +++ b/tests/test_first_light_harness_paths.py @@ -1,5 +1,7 @@ """CPU-only checks for the first-light harness lookup.""" +import contextlib +import io import os from pathlib import Path import sys @@ -49,6 +51,50 @@ class FirstLightHarnessPathsTests(unittest.TestCase): environment.unlink() self.assertEqual(first_light.find_harness(None), str(configured)) + def put_sources(self) -> Path: + test = self.root / "runtime" / "test" + test.mkdir(parents=True, exist_ok=True) + (test / "vk_harness.cpp").write_text('#include "../src/table.h"\n', encoding="utf-8") + (self.root / "runtime" / "src").mkdir(exist_ok=True) + (self.root / "runtime" / "src" / "table.h").write_text("", encoding="utf-8") + return test + + def find_with_stderr(self) -> tuple[str, str]: + err = io.StringIO() + with contextlib.redirect_stderr(err): + found = first_light.find_harness(None) + return found, err.getvalue() + + def test_a_harness_older_than_its_sources_is_reported(self) -> None: + # Old #7148: a harness built before a pull drew with the old program and said nothing. + harness = self.put_harness("runtime", "build", "linux") + self.put_sources() + os.utime(harness, (1_000_000, 1_000_000)) + found, err = self.find_with_stderr() + self.assertEqual(found, str(harness)) + self.assertIn("WARNING", err) + self.assertIn("runtime/test/vk_harness.cpp", err) + self.assertIn("runtime/src/table.h", err) + self.assertIn("--target vk_harness", err) + + def test_a_current_harness_is_used_quietly(self) -> None: + self.put_sources() + harness = self.put_harness("runtime", "build", "linux") + future = os.path.getmtime(self.root / "runtime" / "src" / "table.h") + 60 + os.utime(harness, (future, future)) + found, err = self.find_with_stderr() + self.assertEqual(found, str(harness)) + self.assertEqual(err, "") + + def test_a_harness_outside_the_build_folder_is_not_compared(self) -> None: + self.put_sources() + explicit = self.put_harness("explicit") + os.utime(explicit, (1_000_000, 1_000_000)) + err = io.StringIO() + with contextlib.redirect_stderr(err): + self.assertEqual(first_light.find_harness(str(explicit)), str(explicit)) + self.assertEqual(err.getvalue(), "") + if __name__ == "__main__": unittest.main() diff --git a/tests/test_make_game_pipeline_preflight.py b/tests/test_make_game_pipeline_preflight.py index 6197a924e..01e61aec7 100644 --- a/tests/test_make_game_pipeline_preflight.py +++ b/tests/test_make_game_pipeline_preflight.py @@ -192,6 +192,65 @@ class LinuxPipelineCheckTest(PreflightEnv, unittest.TestCase): make_game.preflight(["pipelines"], self.work) build.assert_not_called() + def put_checker_older_than_its_source(self): + """runtime/build/linux/vk_pipeline_check built before its source last changed (a pull since the build).""" + build = os.path.join(self.root, "runtime", "build", "linux") + test = os.path.join(self.root, "runtime", "test") + os.makedirs(build) + os.makedirs(test) + checker = os.path.join(build, "vk_pipeline_check") + for path, text in ((checker, ""), (os.path.join(test, "vk_pipeline_check.cpp"), '#include "gate.h"\n'), + (os.path.join(test, "gate.h"), "")): + with open(path, "w", encoding="utf-8") as f: + f.write(text) + os.utime(checker, (1_000_000, 1_000_000)) + os.utime(os.path.join(test, "vk_pipeline_check.cpp"), (500_000, 500_000)) + return checker + + def test_a_checker_older_than_its_sources_is_rebuilt(self): + # Old #7148: a checker from before a pull was taken as it was, and the stage checked with the old program. + self.write_shader_index() + checker = self.put_checker_older_than_its_source() + with mock.patch.object(make_game, "ROOT", self.root), mock.patch.object(make_game, "pipeline_check_exe", return_value=checker), mock.patch.object(make_game, "build_pipeline_check") as build: + self.assertEqual(make_game.stale_checker(checker), ["runtime/test/gate.h"]) + make_game.preflight(["pipelines"], self.work, jobs=4) + build.assert_called_once_with(4) + + def test_a_current_checker_in_the_build_folder_is_not_rebuilt(self): + self.write_shader_index() + checker = self.put_checker_older_than_its_source() + future = os.path.getmtime(os.path.join(self.root, "runtime", "test", "gate.h")) + 60 + os.utime(checker, (future, future)) + with mock.patch.object(make_game, "ROOT", self.root), mock.patch.object(make_game, "pipeline_check_exe", return_value=checker), mock.patch.object(make_game, "build_pipeline_check") as build: + self.assertEqual(make_game.stale_checker(checker), []) + make_game.preflight(["pipelines"], self.work) + build.assert_not_called() + + def test_a_stale_checker_without_cmake_is_used_with_a_warning(self): + self.write_shader_index() + checker = self.put_checker_older_than_its_source() + with mock.patch.object(make_game, "ROOT", self.root), mock.patch("shutil.which", side_effect=lambda t: None if t == "cmake" else "/bin/tool"), mock.patch.object(make_game, "pipeline_check_exe", return_value=checker), mock.patch.object(make_game, "build_pipeline_check") as build: + make_game.preflight(["pipelines"], self.work) + build.assert_not_called() + with mock.patch.object(make_game, "log") as log, mock.patch.object(make_game, "checker_supports", side_effect=SystemExit(0)): + with self.assertRaises(SystemExit): + make_game.pipelines(self.work, os.path.join(self.root, "out")) + warning = log.call_args_list[0].args[0] + self.assertIn("WARNING", warning) + self.assertIn("runtime/test/gate.h", warning) + self.assertIn("cmake --build runtime/build/linux --target vk_pipeline_check", warning) + + def test_a_checker_outside_the_build_folder_is_not_compared(self): + # --runtime-build and the like: their time stamps say nothing about this checkout's sources. + self.put_checker_older_than_its_source() + elsewhere = os.path.join(self.root, "elsewhere", "vk_pipeline_check") + os.makedirs(os.path.dirname(elsewhere)) + with open(elsewhere, "w", encoding="utf-8"): + pass + os.utime(elsewhere, (1_000_000, 1_000_000)) + with mock.patch.object(make_game, "ROOT", self.root): + self.assertEqual(make_game.stale_checker(elsewhere), []) + def test_the_build_uses_the_readme_folder(self): with mock.patch.object(make_game, "ROOT", self.root), \ mock.patch.object(make_game, "log"), \ diff --git a/tools/first_light.py b/tools/first_light.py index 7ce4c8a7d..75d14d7af 100644 --- a/tools/first_light.py +++ b/tools/first_light.py @@ -50,6 +50,8 @@ TOOLS = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.dirname(TOOLS) sys.path.insert(0, os.path.join(ROOT, "runtime", "test", "shaders")) from compute_fixture import find_test_program # noqa: E402 +sys.path.insert(0, TOOLS) # python -I leaves the script's own folder off sys.path +from stale_build import newer_sources # noqa: E402 DEFAULT_VS = "VS_main_170e32e6_Wrapped" DEFAULT_PS = "$PS_Rage_Untextured_14f8529b_Wrapped" @@ -584,6 +586,24 @@ def uv_probes(vertices, indices, width: int, height: int, limit: int = 4) -> lis return found +def warn_if_stale(harness: str) -> str: + """`harness`, after a warning when it is a build in this checkout's runtime/build older than vk_harness's sources + (a pull since it was built): it would draw with the old harness and say nothing. A harness outside runtime/build + (--harness or LEONIDA_VK_HARNESS naming another folder) isn't compared.""" + build = os.path.abspath(os.path.join(ROOT, "runtime", "build")) + try: + ours = os.path.commonpath([build, os.path.abspath(harness)]) == build + except ValueError: # another drive + ours = False + newer = newer_sources(harness, ROOT, "vk_harness") if ours else [] + if newer: + more = f" and {len(newer) - 3} more" if len(newer) > 3 else "" + print(f"WARNING: {harness} is older than {', '.join(newer[:3])}{more}; rebuild it " + f"(cmake --build --target vk_harness) to draw with this checkout's harness", + file=sys.stderr) + return harness + + def find_harness(explicit: str | None) -> str: executable = "vk_harness.exe" if os.name == "nt" else "vk_harness" build = os.path.join(ROOT, "runtime", "build") @@ -594,11 +614,11 @@ def find_harness(explicit: str | None) -> str: *(os.path.join(folder, executable) for folder in build_dirs), ): if candidate and os.path.isfile(candidate): - return candidate + return warn_if_stale(candidate) for folder in build_dirs: configured = find_test_program(folder, "vk_harness") if configured: - return os.path.join(configured, executable) + return warn_if_stale(os.path.join(configured, executable)) raise SystemExit( "vk_harness not found: build runtime/build/linux or runtime/build/windows (target vk_harness), " "or set --harness/LEONIDA_VK_HARNESS" diff --git a/tools/stale_build.py b/tools/stale_build.py new file mode 100644 index 000000000..0945e5fed --- /dev/null +++ b/tools/stale_build.py @@ -0,0 +1,50 @@ +"""Which sources of a runtime/test program are newer than a built copy of it. + +make_game.py (vk_pipeline_check) and first_light.py (vk_harness) take a program already in runtime/build when there +is one. After a pull that changed the program, that copy is from the old sources and would run silently. The +sources are runtime/test/.cpp and the headers it pulls in with #include "..." (followed through other +headers): a change to any of them recompiles and relinks the program, so a current build is never older than them. +runtime/CMakeLists.txt isn't one: a change there that doesn't concern the program leaves it as it was. +""" +import os +import re + +_INCLUDE = re.compile(r'^\s*#\s*include\s*"([^"]+)"', re.MULTILINE) + + +def program_sources(root, name): + """The files program `name` of runtime/test is built from, as far as they exist in this checkout.""" + runtime = os.path.join(root, "runtime") + pending = [os.path.join(runtime, "test", name + ".cpp")] + found = [] + seen = set() + while pending: + path = os.path.normpath(pending.pop()) + if path in seen or not os.path.isfile(path): + continue + seen.add(path) + found.append(path) + try: + with open(path, encoding="utf-8", errors="replace") as f: + text = f.read() + except OSError: + continue + pending.extend(os.path.join(os.path.dirname(path), inc) for inc in _INCLUDE.findall(text)) + return found + + +def newer_sources(exe, root, name): + """The sources of program `name` changed after `exe` was built, relative to `root`; empty when `exe` is current + or isn't there to compare.""" + try: + built = os.path.getmtime(exe) + except OSError: + return [] + newer = [] + for path in program_sources(root, name): + try: + if os.path.getmtime(path) > built: + newer.append(os.path.relpath(path, root).replace(os.sep, "/")) + except OSError: + pass + return newer