diff --git a/make_game.py b/make_game.py index 41c273f748..b9a03e03ee 100644 --- a/make_game.py +++ b/make_game.py @@ -1731,19 +1731,6 @@ def drop_index(index: str) -> str: return "" -def translator_digest() -> str: - """SHA-256 over every file directly in tools/: rdna2spirv.py, the modules it imports and the data it reads - (gfx10_formats.json). Any change there counts, so parts made by other code are never merged with new ones.""" - import hashlib - h = hashlib.sha256() - for name in sorted(os.listdir(TOOLS)): - path = os.path.join(TOOLS, name) - if os.path.isfile(path): - with open(path, "rb") as f: - h.update(name.encode("utf-8") + b"\0" + f.read() + b"\0") - return h.hexdigest() - - def _stop_procs(procs: list) -> None: """Terminate and reap any child processes still running after an interrupted or failed launch.""" for p in procs: @@ -1774,12 +1761,12 @@ def translate(disasm: str, spirv: str, jobs: int, emulate: bool = False, A shard publishes shard.json and then index.part.json. Both are needed to reuse it. A run stopped part-way, or during the merge, keeps those parts when it resumes with the same --jobs, wave64 mode, fallback - flags and translator source, and starts only the shards that have none. shaders() drops the plan whenever an - earlier step reruns.""" + flags and translator inputs, and starts only the shards that have none. The plan fingerprints the same translator + inputs as the merged index. shaders() drops the plan whenever an earlier step reruns.""" os.makedirs(spirv, exist_ok=True) plan_path = os.path.join(spirv, SHARD_PLAN) plan = {"jobs": jobs, "wave64_emulate": bool(emulate), "fallbacks": list(fallbacks), - "translator": translator_digest()} + "translator": translation_sources_digest()} try: with open(plan_path, encoding="utf-8") as f: same = json.load(f) == plan diff --git a/tests/test_make_game_resume.py b/tests/test_make_game_resume.py index 475155f810..c8bf96eeb3 100644 --- a/tests/test_make_game_resume.py +++ b/tests/test_make_game_resume.py @@ -3,6 +3,7 @@ Standard library only; no GPU, no Vulkan SDK, no game data: the shard processes and the merge are mocked. A fake shard writes its marker and then index.part.json the way rdna2spirv.py --shard does. """ +import hashlib import json import os import sys @@ -50,7 +51,7 @@ def wait(self): return 0 with mock.patch.object(make_game.subprocess, "Popen", side_effect=FakeProc), \ mock.patch.object(make_game, "py") as py, \ - mock.patch.object(make_game, "translator_digest", return_value=digest), \ + mock.patch.object(make_game, "translation_sources_digest", return_value=digest), \ mock.patch.object(make_game, "log", lambda *a: None): make_game.translate("disasm", self.spirv, jobs, emulate=emulate) self.assertEqual(py.call_count, 1) @@ -157,21 +158,76 @@ def fake_disassemble(*args): class TranslatorDigestTest(unittest.TestCase): - def test_any_file_in_tools_changes_it_and_folders_do_not(self): - with tempfile.TemporaryDirectory() as tools: - for name, text in (("rdna2spirv.py", "a"), ("gfx10_formats.json", "{}")): - with open(os.path.join(tools, name), "w", encoding="utf-8") as f: - f.write(text) - with mock.patch.object(make_game, "TOOLS", tools): - first = make_game.translator_digest() - os.makedirs(os.path.join(tools, "__pycache__")) - self.assertEqual(make_game.translator_digest(), first) - with open(os.path.join(tools, "gfx10_formats.json"), "w", encoding="utf-8") as f: - f.write("{ }") - self.assertNotEqual(make_game.translator_digest(), first) + def setUp(self): + tmp = self.enterContext(tempfile.TemporaryDirectory()) + self.spirv = os.path.join(tmp, "spirv") + self.tools = os.path.join(tmp, "tools") + os.makedirs(self.tools) + for name in make_game.TRANSLATION_SOURCES: + self.write_tool(name, "{}\n" if name == "gfx10_formats.json" else "# synthetic source\n") + + def write_tool(self, name, content): + with open(os.path.join(self.tools, name), "w", encoding="utf-8", newline="\n") as f: + f.write(content) + + def translate(self): + started = [] + + def start_shard(args): + shard, jobs = map(int, args[args.index("--shard") + 1].split("/")) + started.append(shard) + for name in (f"shard{shard}.json", f"index.part{shard}.json"): + # A shard should only start after both of its stale files have been removed. + with open(os.path.join(self.spirv, name), "x", encoding="utf-8", newline="\n") as f: + json.dump({"index": shard, "count": jobs}, f) + proc = mock.Mock() + proc.poll.return_value = 0 + return proc + + with (mock.patch.object(make_game, "TOOLS", self.tools), + mock.patch.object(make_game.subprocess, "Popen", side_effect=start_shard), + mock.patch.object(make_game, "py") as merge, + mock.patch.object(make_game, "log")): + make_game.translate("disasm", self.spirv, 2) + # Leave the index unwritten, as if interrupted before the merge finished. + merge.assert_called_once_with("rdna2spirv.py", "disasm", "--out", self.spirv, "--merge") + return started + + def test_unrelated_tool_addition_and_edit_preserve_finished_shards(self): + self.assertEqual(self.translate(), [0, 1]) + for name in ("shard1.json", "index.part1.json"): + os.remove(os.path.join(self.spirv, name)) + self.write_tool("unrelated.py", "# added\n") + self.assertEqual(self.translate(), [1]) + self.write_tool("unrelated.py", "# edited\n") + self.assertEqual(self.translate(), []) + + def test_translator_source_and_format_table_edits_rebuild_finished_shards(self): + self.assertEqual(self.translate(), [0, 1]) + for name, content in (("rdna2spirv.py", "# edited translator\n"), ("gfx10_formats.json", "{ }\n")): + with self.subTest(name=name): + self.write_tool(name, content) + self.assertEqual(self.translate(), [0, 1]) + + def test_old_shard_plan_rebuilds_once(self): + self.assertEqual(self.translate(), [0, 1]) + # The old plan hashed every top-level tools file with NUL separators. + legacy = hashlib.sha256() + for name in sorted(os.listdir(self.tools)): + with open(os.path.join(self.tools, name), "rb") as f: + legacy.update(name.encode("utf-8") + b"\0" + f.read() + b"\0") + plan = os.path.join(self.spirv, make_game.SHARD_PLAN) + with open(plan, encoding="utf-8") as f: + saved = json.load(f) + with open(plan, "w", encoding="utf-8", newline="\n") as f: + json.dump({**saved, "translator": legacy.hexdigest()}, f) + self.assertEqual(self.translate(), [0, 1]) + with open(plan, encoding="utf-8") as f: + self.assertEqual(json.load(f), saved) + self.assertEqual(self.translate(), []) def test_the_real_tools_folder_gives_a_sha256(self): - digest = make_game.translator_digest() + digest = make_game.translation_sources_digest() self.assertEqual(len(digest), 64) int(digest, 16)