diff options
| author | Yash Dubey <yash.dubey803at@gmail.com> | 2026-11-24 07:35:50 +0530 |
|---|---|---|
| committer | Yash Dubey <yash.dubey803at@gmail.com> | 2026-11-24 07:35:50 +0530 |
| commit | dfd239266fd62e1fbbfcf264e9bb4e5c8e8dc801 (patch) | |
| tree | 0fb3fbb22dda6a8b3af3b30b0fc3bcc1a23480f6 | |
| parent | 9ec34cdff41b19d7671bc260e5f40dc69790d905 (diff) | |
make_game, first_light: a vk_pipeline_check or vk_harness older than its sources is rebuilt or reported, not reused silently
| -rw-r--r-- | make_game.py | 36 | ||||
| -rw-r--r-- | tests/test_first_light_harness_paths.py | 46 | ||||
| -rw-r--r-- | tests/test_make_game_pipeline_preflight.py | 59 | ||||
| -rw-r--r-- | tools/first_light.py | 24 | ||||
| -rw-r--r-- | tools/stale_build.py | 50 |
5 files changed, 207 insertions, 8 deletions
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 | |||
| 73 | import leonida_tui # noqa: E402 | 73 | import leonida_tui # noqa: E402 |
| 74 | from shader_stage_manifest import shader_stems # noqa: E402 | 74 | from shader_stage_manifest import shader_stems # noqa: E402 |
| 75 | from replace_retry import replace as replace_retry # noqa: E402 | 75 | from replace_retry import replace as replace_retry # noqa: E402 |
| 76 | from stale_build import newer_sources # noqa: E402 | ||
| 76 | 77 | ||
| 77 | VERSIONS = os.path.join(ROOT, "game") | 78 | VERSIONS = os.path.join(ROOT, "game") |
| 78 | STAGES = ["check", "shaders", "assets", "runtime", "pipelines"] | 79 | STAGES = ["check", "shaders", "assets", "runtime", "pipelines"] |
| @@ -900,12 +901,16 @@ def preflight(stages: list, work: str, wav: bool = False, runtime_build: str | N | |||
| 900 | "Free some space or choose another drive with --work." + hint) | 901 | "Free some space or choose another drive with --work." + hint) |
| 901 | if os.name != "nt" and not runtime_build and any(s in WINDOWS_ONLY for s in stages): | 902 | if os.name != "nt" and not runtime_build and any(s in WINDOWS_ONLY for s in stages): |
| 902 | cross_configure() | 903 | cross_configure() |
| 903 | if "pipelines" in stages and os.name != "nt" and not pipeline_check_exe(): | 904 | if "pipelines" in stages and os.name != "nt": |
| 904 | build_pipeline_check(jobs) | 905 | checker = pipeline_check_exe() |
| 905 | if not pipeline_check_exe(): | 906 | # A checker built before a pull is rebuilt too; cmake --build only recompiles what changed. Without CMake |
| 906 | fail("vk_pipeline_check built, but not as runtime/build/linux/vk_pipeline_check (a multi-config " | 907 | # the pipelines stage says it is out of date and uses it. |
| 907 | "generator puts it in a subfolder): unset CMAKE_GENERATOR if it names one, delete " | 908 | if not checker or (stale_checker(checker) and shutil.which("cmake")): |
| 908 | "runtime/build/linux and run the build again.") | 909 | build_pipeline_check(jobs) |
| 910 | if not pipeline_check_exe(): | ||
| 911 | fail("vk_pipeline_check built, but not as runtime/build/linux/vk_pipeline_check (a multi-config " | ||
| 912 | "generator puts it in a subfolder): unset CMAKE_GENERATOR if it names one, delete " | ||
| 913 | "runtime/build/linux and run the build again.") | ||
| 909 | if "assets" in stages: | 914 | if "assets" in stages: |
| 910 | ensure_submodule() | 915 | ensure_submodule() |
| 911 | 916 | ||
| @@ -2369,6 +2374,18 @@ def pipeline_check_exe(runtime_build: str | None = None) -> str | None: | |||
| 2369 | return next((p for p in found if os.path.isfile(p) and (windows or os.access(p, os.X_OK))), None) | 2374 | return next((p for p in found if os.path.isfile(p) and (windows or os.access(p, os.X_OK))), None) |
| 2370 | 2375 | ||
| 2371 | 2376 | ||
| 2377 | def stale_checker(exe: str) -> list: | ||
| 2378 | """The sources of vk_pipeline_check changed after `exe` was built, when `exe` is in this checkout's runtime/build. | ||
| 2379 | A checker from elsewhere (--runtime-build) isn't compared: its time stamp says nothing about these sources.""" | ||
| 2380 | build = os.path.abspath(os.path.join(ROOT, "runtime", "build")) | ||
| 2381 | try: | ||
| 2382 | if os.path.commonpath([build, os.path.abspath(exe)]) != build: | ||
| 2383 | return [] | ||
| 2384 | except ValueError: # another drive | ||
| 2385 | return [] | ||
| 2386 | return newer_sources(exe, ROOT, "vk_pipeline_check") | ||
| 2387 | |||
| 2388 | |||
| 2372 | def require_translated_shaders(work: str) -> str: | 2389 | def require_translated_shaders(work: str) -> str: |
| 2373 | spirv = os.path.join(work, "shaders", "spirv") | 2390 | spirv = os.path.join(work, "shaders", "spirv") |
| 2374 | index = os.path.join(spirv, "index.json") | 2391 | index = os.path.join(spirv, "index.json") |
| @@ -2394,6 +2411,13 @@ def pipelines(work: str, out: str, jobs: "int | None" = None, *, runtime_build: | |||
| 2394 | fail("vk_pipeline_check wasn't built. With CMake 3.24+, a C++20 compiler and the Vulkan headers and " | 2411 | fail("vk_pipeline_check wasn't built. With CMake 3.24+, a C++20 compiler and the Vulkan headers and " |
| 2395 | "loader installed, run: cmake -S runtime -B runtime/build/linux && " | 2412 | "loader installed, run: cmake -S runtime -B runtime/build/linux && " |
| 2396 | "cmake --build runtime/build/linux --parallel") | 2413 | "cmake --build runtime/build/linux --parallel") |
| 2414 | newer = stale_checker(exe) | ||
| 2415 | if newer: | ||
| 2416 | rebuild = ("run the runtime stage with VULKAN_SDK set" if os.name == "nt" else | ||
| 2417 | "cmake --build runtime/build/linux --target vk_pipeline_check") | ||
| 2418 | more = f" and {len(newer) - 3} more" if len(newer) > 3 else "" | ||
| 2419 | log(f"pipelines: WARNING: {exe} is older than {', '.join(newer[:3])}{more}, so it is not this checkout's " | ||
| 2420 | f"checker; to rebuild it, {rebuild}") | ||
| 2397 | spirv = require_translated_shaders(work) | 2421 | spirv = require_translated_shaders(work) |
| 2398 | log("pipelines: creating every shader module and pipeline (compute and graphics) on this GPU") | 2422 | log("pipelines: creating every shader module and pipeline (compute and graphics) on this GPU") |
| 2399 | os.makedirs(out, exist_ok=True) | 2423 | 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 @@ | |||
| 1 | """CPU-only checks for the first-light harness lookup.""" | 1 | """CPU-only checks for the first-light harness lookup.""" |
| 2 | 2 | ||
| 3 | import contextlib | ||
| 4 | import io | ||
| 3 | import os | 5 | import os |
| 4 | from pathlib import Path | 6 | from pathlib import Path |
| 5 | import sys | 7 | import sys |
| @@ -49,6 +51,50 @@ class FirstLightHarnessPathsTests(unittest.TestCase): | |||
| 49 | environment.unlink() | 51 | environment.unlink() |
| 50 | self.assertEqual(first_light.find_harness(None), str(configured)) | 52 | self.assertEqual(first_light.find_harness(None), str(configured)) |
| 51 | 53 | ||
| 54 | def put_sources(self) -> Path: | ||
| 55 | test = self.root / "runtime" / "test" | ||
| 56 | test.mkdir(parents=True, exist_ok=True) | ||
| 57 | (test / "vk_harness.cpp").write_text('#include "../src/table.h"\n', encoding="utf-8") | ||
| 58 | (self.root / "runtime" / "src").mkdir(exist_ok=True) | ||
| 59 | (self.root / "runtime" / "src" / "table.h").write_text("", encoding="utf-8") | ||
| 60 | return test | ||
| 61 | |||
| 62 | def find_with_stderr(self) -> tuple[str, str]: | ||
| 63 | err = io.StringIO() | ||
| 64 | with contextlib.redirect_stderr(err): | ||
| 65 | found = first_light.find_harness(None) | ||
| 66 | return found, err.getvalue() | ||
| 67 | |||
| 68 | def test_a_harness_older_than_its_sources_is_reported(self) -> None: | ||
| 69 | # Old #7148: a harness built before a pull drew with the old program and said nothing. | ||
| 70 | harness = self.put_harness("runtime", "build", "linux") | ||
| 71 | self.put_sources() | ||
| 72 | os.utime(harness, (1_000_000, 1_000_000)) | ||
| 73 | found, err = self.find_with_stderr() | ||
| 74 | self.assertEqual(found, str(harness)) | ||
| 75 | self.assertIn("WARNING", err) | ||
| 76 | self.assertIn("runtime/test/vk_harness.cpp", err) | ||
| 77 | self.assertIn("runtime/src/table.h", err) | ||
| 78 | self.assertIn("--target vk_harness", err) | ||
| 79 | |||
| 80 | def test_a_current_harness_is_used_quietly(self) -> None: | ||
| 81 | self.put_sources() | ||
| 82 | harness = self.put_harness("runtime", "build", "linux") | ||
| 83 | future = os.path.getmtime(self.root / "runtime" / "src" / "table.h") + 60 | ||
| 84 | os.utime(harness, (future, future)) | ||
| 85 | found, err = self.find_with_stderr() | ||
| 86 | self.assertEqual(found, str(harness)) | ||
| 87 | self.assertEqual(err, "") | ||
| 88 | |||
| 89 | def test_a_harness_outside_the_build_folder_is_not_compared(self) -> None: | ||
| 90 | self.put_sources() | ||
| 91 | explicit = self.put_harness("explicit") | ||
| 92 | os.utime(explicit, (1_000_000, 1_000_000)) | ||
| 93 | err = io.StringIO() | ||
| 94 | with contextlib.redirect_stderr(err): | ||
| 95 | self.assertEqual(first_light.find_harness(str(explicit)), str(explicit)) | ||
| 96 | self.assertEqual(err.getvalue(), "") | ||
| 97 | |||
| 52 | 98 | ||
| 53 | if __name__ == "__main__": | 99 | if __name__ == "__main__": |
| 54 | unittest.main() | 100 | 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): | |||
| 192 | make_game.preflight(["pipelines"], self.work) | 192 | make_game.preflight(["pipelines"], self.work) |
| 193 | build.assert_not_called() | 193 | build.assert_not_called() |
| 194 | 194 | ||
| 195 | def put_checker_older_than_its_source(self): | ||
| 196 | """runtime/build/linux/vk_pipeline_check built before its source last changed (a pull since the build).""" | ||
| 197 | build = os.path.join(self.root, "runtime", "build", "linux") | ||
| 198 | test = os.path.join(self.root, "runtime", "test") | ||
| 199 | os.makedirs(build) | ||
| 200 | os.makedirs(test) | ||
| 201 | checker = os.path.join(build, "vk_pipeline_check") | ||
| 202 | for path, text in ((checker, ""), (os.path.join(test, "vk_pipeline_check.cpp"), '#include "gate.h"\n'), | ||
| 203 | (os.path.join(test, "gate.h"), "")): | ||
| 204 | with open(path, "w", encoding="utf-8") as f: | ||
| 205 | f.write(text) | ||
| 206 | os.utime(checker, (1_000_000, 1_000_000)) | ||
| 207 | os.utime(os.path.join(test, "vk_pipeline_check.cpp"), (500_000, 500_000)) | ||
| 208 | return checker | ||
| 209 | |||
| 210 | def test_a_checker_older_than_its_sources_is_rebuilt(self): | ||
| 211 | # Old #7148: a checker from before a pull was taken as it was, and the stage checked with the old program. | ||
| 212 | self.write_shader_index() | ||
| 213 | checker = self.put_checker_older_than_its_source() | ||
| 214 | 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: | ||
| 215 | self.assertEqual(make_game.stale_checker(checker), ["runtime/test/gate.h"]) | ||
| 216 | make_game.preflight(["pipelines"], self.work, jobs=4) | ||
| 217 | build.assert_called_once_with(4) | ||
| 218 | |||
| 219 | def test_a_current_checker_in_the_build_folder_is_not_rebuilt(self): | ||
| 220 | self.write_shader_index() | ||
| 221 | checker = self.put_checker_older_than_its_source() | ||
| 222 | future = os.path.getmtime(os.path.join(self.root, "runtime", "test", "gate.h")) + 60 | ||
| 223 | os.utime(checker, (future, future)) | ||
| 224 | 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: | ||
| 225 | self.assertEqual(make_game.stale_checker(checker), []) | ||
| 226 | make_game.preflight(["pipelines"], self.work) | ||
| 227 | build.assert_not_called() | ||
| 228 | |||
| 229 | def test_a_stale_checker_without_cmake_is_used_with_a_warning(self): | ||
| 230 | self.write_shader_index() | ||
| 231 | checker = self.put_checker_older_than_its_source() | ||
| 232 | 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: | ||
| 233 | make_game.preflight(["pipelines"], self.work) | ||
| 234 | build.assert_not_called() | ||
| 235 | with mock.patch.object(make_game, "log") as log, mock.patch.object(make_game, "checker_supports", side_effect=SystemExit(0)): | ||
| 236 | with self.assertRaises(SystemExit): | ||
| 237 | make_game.pipelines(self.work, os.path.join(self.root, "out")) | ||
| 238 | warning = log.call_args_list[0].args[0] | ||
| 239 | self.assertIn("WARNING", warning) | ||
| 240 | self.assertIn("runtime/test/gate.h", warning) | ||
| 241 | self.assertIn("cmake --build runtime/build/linux --target vk_pipeline_check", warning) | ||
| 242 | |||
| 243 | def test_a_checker_outside_the_build_folder_is_not_compared(self): | ||
| 244 | # --runtime-build and the like: their time stamps say nothing about this checkout's sources. | ||
| 245 | self.put_checker_older_than_its_source() | ||
| 246 | elsewhere = os.path.join(self.root, "elsewhere", "vk_pipeline_check") | ||
| 247 | os.makedirs(os.path.dirname(elsewhere)) | ||
| 248 | with open(elsewhere, "w", encoding="utf-8"): | ||
| 249 | pass | ||
| 250 | os.utime(elsewhere, (1_000_000, 1_000_000)) | ||
| 251 | with mock.patch.object(make_game, "ROOT", self.root): | ||
| 252 | self.assertEqual(make_game.stale_checker(elsewhere), []) | ||
| 253 | |||
| 195 | def test_the_build_uses_the_readme_folder(self): | 254 | def test_the_build_uses_the_readme_folder(self): |
| 196 | with mock.patch.object(make_game, "ROOT", self.root), \ | 255 | with mock.patch.object(make_game, "ROOT", self.root), \ |
| 197 | mock.patch.object(make_game, "log"), \ | 256 | 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__)) | |||
| 50 | ROOT = os.path.dirname(TOOLS) | 50 | ROOT = os.path.dirname(TOOLS) |
| 51 | sys.path.insert(0, os.path.join(ROOT, "runtime", "test", "shaders")) | 51 | sys.path.insert(0, os.path.join(ROOT, "runtime", "test", "shaders")) |
| 52 | from compute_fixture import find_test_program # noqa: E402 | 52 | from compute_fixture import find_test_program # noqa: E402 |
| 53 | sys.path.insert(0, TOOLS) # python -I leaves the script's own folder off sys.path | ||
| 54 | from stale_build import newer_sources # noqa: E402 | ||
| 53 | 55 | ||
| 54 | DEFAULT_VS = "VS_main_170e32e6_Wrapped" | 56 | DEFAULT_VS = "VS_main_170e32e6_Wrapped" |
| 55 | DEFAULT_PS = "$PS_Rage_Untextured_14f8529b_Wrapped" | 57 | DEFAULT_PS = "$PS_Rage_Untextured_14f8529b_Wrapped" |
| @@ -584,6 +586,24 @@ def uv_probes(vertices, indices, width: int, height: int, limit: int = 4) -> lis | |||
| 584 | return found | 586 | return found |
| 585 | 587 | ||
| 586 | 588 | ||
| 589 | def warn_if_stale(harness: str) -> str: | ||
| 590 | """`harness`, after a warning when it is a build in this checkout's runtime/build older than vk_harness's sources | ||
| 591 | (a pull since it was built): it would draw with the old harness and say nothing. A harness outside runtime/build | ||
| 592 | (--harness or LEONIDA_VK_HARNESS naming another folder) isn't compared.""" | ||
| 593 | build = os.path.abspath(os.path.join(ROOT, "runtime", "build")) | ||
| 594 | try: | ||
| 595 | ours = os.path.commonpath([build, os.path.abspath(harness)]) == build | ||
| 596 | except ValueError: # another drive | ||
| 597 | ours = False | ||
| 598 | newer = newer_sources(harness, ROOT, "vk_harness") if ours else [] | ||
| 599 | if newer: | ||
| 600 | more = f" and {len(newer) - 3} more" if len(newer) > 3 else "" | ||
| 601 | print(f"WARNING: {harness} is older than {', '.join(newer[:3])}{more}; rebuild it " | ||
| 602 | f"(cmake --build <build folder> --target vk_harness) to draw with this checkout's harness", | ||
| 603 | file=sys.stderr) | ||
| 604 | return harness | ||
| 605 | |||
| 606 | |||
| 587 | def find_harness(explicit: str | None) -> str: | 607 | def find_harness(explicit: str | None) -> str: |
| 588 | executable = "vk_harness.exe" if os.name == "nt" else "vk_harness" | 608 | executable = "vk_harness.exe" if os.name == "nt" else "vk_harness" |
| 589 | build = os.path.join(ROOT, "runtime", "build") | 609 | build = os.path.join(ROOT, "runtime", "build") |
| @@ -594,11 +614,11 @@ def find_harness(explicit: str | None) -> str: | |||
| 594 | *(os.path.join(folder, executable) for folder in build_dirs), | 614 | *(os.path.join(folder, executable) for folder in build_dirs), |
| 595 | ): | 615 | ): |
| 596 | if candidate and os.path.isfile(candidate): | 616 | if candidate and os.path.isfile(candidate): |
| 597 | return candidate | 617 | return warn_if_stale(candidate) |
| 598 | for folder in build_dirs: | 618 | for folder in build_dirs: |
| 599 | configured = find_test_program(folder, "vk_harness") | 619 | configured = find_test_program(folder, "vk_harness") |
| 600 | if configured: | 620 | if configured: |
| 601 | return os.path.join(configured, executable) | 621 | return warn_if_stale(os.path.join(configured, executable)) |
| 602 | raise SystemExit( | 622 | raise SystemExit( |
| 603 | "vk_harness not found: build runtime/build/linux or runtime/build/windows (target vk_harness), " | 623 | "vk_harness not found: build runtime/build/linux or runtime/build/windows (target vk_harness), " |
| 604 | "or set --harness/LEONIDA_VK_HARNESS" | 624 | "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 @@ | |||
| 1 | """Which sources of a runtime/test program are newer than a built copy of it. | ||
| 2 | |||
| 3 | make_game.py (vk_pipeline_check) and first_light.py (vk_harness) take a program already in runtime/build when there | ||
| 4 | is one. After a pull that changed the program, that copy is from the old sources and would run silently. The | ||
| 5 | sources are runtime/test/<name>.cpp and the headers it pulls in with #include "..." (followed through other | ||
| 6 | headers): a change to any of them recompiles and relinks the program, so a current build is never older than them. | ||
| 7 | runtime/CMakeLists.txt isn't one: a change there that doesn't concern the program leaves it as it was. | ||
| 8 | """ | ||
| 9 | import os | ||
| 10 | import re | ||
| 11 | |||
| 12 | _INCLUDE = re.compile(r'^\s*#\s*include\s*"([^"]+)"', re.MULTILINE) | ||
| 13 | |||
| 14 | |||
| 15 | def program_sources(root, name): | ||
| 16 | """The files program `name` of runtime/test is built from, as far as they exist in this checkout.""" | ||
| 17 | runtime = os.path.join(root, "runtime") | ||
| 18 | pending = [os.path.join(runtime, "test", name + ".cpp")] | ||
| 19 | found = [] | ||
| 20 | seen = set() | ||
| 21 | while pending: | ||
| 22 | path = os.path.normpath(pending.pop()) | ||
| 23 | if path in seen or not os.path.isfile(path): | ||
| 24 | continue | ||
| 25 | seen.add(path) | ||
| 26 | found.append(path) | ||
| 27 | try: | ||
| 28 | with open(path, encoding="utf-8", errors="replace") as f: | ||
| 29 | text = f.read() | ||
| 30 | except OSError: | ||
| 31 | continue | ||
| 32 | pending.extend(os.path.join(os.path.dirname(path), inc) for inc in _INCLUDE.findall(text)) | ||
| 33 | return found | ||
| 34 | |||
| 35 | |||
| 36 | def newer_sources(exe, root, name): | ||
| 37 | """The sources of program `name` changed after `exe` was built, relative to `root`; empty when `exe` is current | ||
| 38 | or isn't there to compare.""" | ||
| 39 | try: | ||
| 40 | built = os.path.getmtime(exe) | ||
| 41 | except OSError: | ||
| 42 | return [] | ||
| 43 | newer = [] | ||
| 44 | for path in program_sources(root, name): | ||
| 45 | try: | ||
| 46 | if os.path.getmtime(path) > built: | ||
| 47 | newer.append(os.path.relpath(path, root).replace(os.sep, "/")) | ||
| 48 | except OSError: | ||
| 49 | pass | ||
| 50 | return newer | ||