Files
mandy/tests/test_make_game_platform.py
Kevin Morales a65a039cfd make_game.py: pass the versioned LLVM tools to CMake as full paths
CMake keeps the archiver and resource compiler given on the command line as
bare names (CMAKE_AR=llvm-lib-23), so a build folder configured from a
versioned set still needed those names on PATH: once a static library had to
be archived again, the runtime stage failed. The overrides are now the
resolved paths, so every entry in the cache is absolute.
2026-11-22 02:36:02 -07:00

425 lines
21 KiB
Python

"""Platform-specific build stages, runtime packaging and vk_pipeline_check discovery.
python -I tests/test_make_game_platform.py
No game data: stage selection uses mocks, and the packaging and discovery tests create synthetic files.
"""
import io
import os
import sys
import tempfile
import unittest
from unittest import mock
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if ROOT not in sys.path:
sys.path.insert(0, ROOT)
import make_game # noqa: E402
STAGE_FUNCS = ("shaders", "assets", "runtime", "pipelines")
class DefaultStagesTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.game = os.path.join(self.tmp.name, "game")
os.makedirs(self.game)
def tearDown(self):
self.tmp.cleanup()
def run_main(self, os_name, *args, lacking=("clang-cl",)):
argv = ["make_game.py", "--game", self.game, "--out", os.path.join(self.tmp.name, "out"), *args]
mocks = {n: mock.MagicMock() for n in STAGE_FUNCS}
with mock.patch.object(sys, "argv", argv), \
mock.patch.object(make_game.os, "name", os_name), \
mock.patch.object(make_game, "cross_build_missing", return_value=list(lacking)), \
mock.patch.object(make_game, "below_normal"), \
mock.patch.object(make_game, "check", return_value={"expected_exe": "game.exe"}), \
mock.patch.object(make_game, "preflight") as preflight, \
mock.patch.object(make_game, "log"), \
mock.patch.multiple(make_game, **mocks):
self.assertEqual(make_game.main(), 0)
return preflight.call_args.args[0], {n: m.called for n, m in mocks.items()}
def test_default_stages_leave_runtime_out_off_windows_without_the_cross_build(self):
stages, ran = self.run_main("posix")
self.assertEqual(stages, ["check", "shaders", "assets", "pipelines"])
self.assertEqual(ran, {"shaders": True, "assets": True, "runtime": False, "pipelines": True})
def test_default_stages_keep_runtime_off_windows_with_the_cross_build(self):
stages, ran = self.run_main("posix", lacking=())
self.assertEqual(stages, make_game.STAGES)
self.assertTrue(ran["runtime"])
def test_default_stages_keep_runtime_on_windows(self):
stages, ran = self.run_main("nt")
self.assertEqual(stages, make_game.STAGES)
self.assertTrue(ran["runtime"])
def test_explicit_full_stage_list_reaches_preflight_off_windows(self):
stages, _ = self.run_main("posix", "--stages", ",".join(make_game.STAGES), "--wave64-emulate", "off")
self.assertEqual(stages, make_game.STAGES)
def test_explicit_stages_are_kept_as_given(self):
stages, _ = self.run_main("posix", "--stages", "shaders,runtime")
self.assertEqual(stages, ["shaders", "runtime"])
def test_default_stages_keep_runtime_off_windows_with_a_runtime_build(self):
stages, ran = self.run_main("posix", "--runtime-build", self.tmp.name)
self.assertEqual(stages, make_game.STAGES)
self.assertTrue(ran["runtime"])
class PreflightTest(unittest.TestCase):
def preflight(self, lacking, work):
with mock.patch.object(make_game.os, "name", "posix"), \
mock.patch.object(make_game.sys, "version_info", (3, 12, 0)), \
mock.patch.object(make_game, "cross_build_missing", return_value=lacking), \
mock.patch.object(make_game, "cross_configure") as configure, \
mock.patch.object(make_game, "visual_cpp_tools_ok") as vs:
make_game.preflight(["runtime"], work)
vs.assert_not_called()
return configure
def test_runtime_off_windows_without_the_cross_build_fails_before_any_work(self):
err = io.StringIO()
with tempfile.TemporaryDirectory() as work, \
mock.patch("sys.stderr", err), \
mock.patch.object(make_game.shutil, "disk_usage") as usage:
with self.assertRaises(SystemExit) as ctx:
self.preflight(["clang-cl", "the xwin SDK"], os.path.join(work, "w"))
self.assertEqual(ctx.exception.code, 1)
self.assertIn("clang-cl (for the runtime stage, or pass --runtime-build)", err.getvalue())
self.assertIn("the xwin SDK (for the runtime stage", err.getvalue())
usage.assert_not_called()
self.assertFalse(os.path.exists(os.path.join(work, "w")))
def test_runtime_off_windows_configures_the_cross_build_before_any_work(self):
with tempfile.TemporaryDirectory() as work:
self.preflight([], os.path.join(work, "w")).assert_called_once_with()
class CrossBuildMissingTest(unittest.TestCase):
def setUp(self):
self.sdk = self.enterContext(tempfile.TemporaryDirectory())
os.makedirs(os.path.join(self.sdk, "VC", "Tools", "MSVC"))
os.makedirs(os.path.join(self.sdk, "Windows Kits", "10", "Lib", "10.0.26100", "um", "x64"))
def missing(self, have=("cmake", *make_game.CROSS_TOOLS), major=23, sdk=None, configured=None):
def which(t):
return t if t.startswith("/") and t in have else f"/usr/bin/{t}" if t in have else None
majors = major if callable(major) else (lambda exe="clang-cl": major)
with mock.patch.object(make_game.shutil, "which", side_effect=which), \
mock.patch.object(make_game, "clang_cl_major", side_effect=majors) as self.major, \
mock.patch.object(make_game, "configured_cross_tools", return_value=configured or {}), \
mock.patch.dict(os.environ, {"XWIN_DIR": sdk if sdk is not None else self.sdk}):
return make_game.cross_build_missing()
def test_a_full_versioned_set_counts(self):
self.assertEqual(self.missing(have=("cmake", *(f"{t}-21" for t in make_game.CROSS_TOOLS))), [])
self.major.assert_called_once_with("clang-cl-21")
def test_an_old_plain_set_does_not_hide_a_newer_versioned_one(self):
have = ("cmake", *make_game.CROSS_TOOLS, *(f"{t}-21" for t in make_game.CROSS_TOOLS))
self.assertEqual(self.missing(have=have, major=lambda exe="clang-cl": 18 if exe == "clang-cl" else 21), [])
def test_versions_are_not_mixed_and_the_missing_one_is_named(self):
have = ("cmake", "clang-cl-21", "lld-link-21", "llvm-lib-21", "llvm-rc-21", "llvm-mt-20")
(item,) = self.missing(have=have)
self.assertTrue(item.startswith("llvm-mt-21: LLVM 19 or newer"), item)
def test_a_configured_build_folder_needs_only_its_own_tools(self):
tools = {"CMAKE_CXX_COMPILER": "/opt/llvm-21/bin/clang-cl", "CMAKE_LINKER": "/opt/llvm-21/bin/lld-link",
"CMAKE_AR": "llvm-lib", "CMAKE_MT": "/opt/llvm-21/bin/llvm-mt", "CMAKE_RC_COMPILER": "llvm-rc"}
have = ("cmake", "llvm-lib", "llvm-rc", "/opt/llvm-21/bin/clang-cl", "/opt/llvm-21/bin/lld-link",
"/opt/llvm-21/bin/llvm-mt")
self.assertEqual(self.missing(have=have, configured=tools), [])
self.major.assert_not_called()
(item,) = self.missing(have=have[:-1], configured=tools)
self.assertTrue(item.startswith("/opt/llvm-21/bin/llvm-mt, which runtime/build/windows was configured with"))
self.assertIn("delete runtime/build/windows", item)
def test_everything_there(self):
self.assertEqual(self.missing(), [])
def test_missing_llvm_tools_are_named_together(self):
(item,) = self.missing(have=("cmake", "clang-cl", "llvm-lib", "llvm-rc", "llvm-mt"))
self.assertTrue(item.startswith("lld-link: LLVM 19 or newer"))
(item,) = self.missing(have=("cmake",))
self.assertTrue(item.startswith(", ".join(make_game.CROSS_TOOLS) + ":"))
def test_clang_older_than_19_is_refused(self):
(item,) = self.missing(major=18)
self.assertIn("this one is 18", item)
self.assertEqual(self.missing(major=None), [])
def test_cmake_and_the_sdk(self):
missing = self.missing(have=make_game.CROSS_TOOLS, sdk=os.path.join(self.sdk, "nothing"))
self.assertEqual(len(missing), 2)
self.assertEqual(missing[0], "CMake 3.24 or newer")
self.assertIn("xwin", missing[1])
def test_xwin_dir_defaults_to_the_documented_folder(self):
home = self.enterContext(tempfile.TemporaryDirectory())
env = {k: v for k, v in os.environ.items() if k != "XWIN_DIR"}
with mock.patch.dict(os.environ, env, clear=True), \
mock.patch.object(make_game.os.path, "expanduser", side_effect=lambda p: p.replace("~", home, 1)):
self.assertIsNone(make_game.xwin_dir())
sdk = os.path.join(home, ".local", "share", "xwin-sdk")
os.makedirs(os.path.join(sdk, "VC", "Tools", "MSVC"))
self.assertIsNone(make_game.xwin_dir())
os.makedirs(os.path.join(sdk, "Windows Kits", "10", "Lib", "10.0.26100", "um", "x64"))
self.assertEqual(make_game.xwin_dir(), sdk)
class RuntimeBuildPreflightTest(unittest.TestCase):
def preflight(self, build, files, err=None):
for name in files:
open(os.path.join(build, name), "wb").close()
err = io.StringIO() if err is None else err
with mock.patch.object(make_game.os, "name", "posix"), \
mock.patch("sys.stderr", err), \
mock.patch.object(make_game.sys, "version_info", (3, 12, 0)), \
mock.patch.object(make_game, "visual_cpp_tools_ok", return_value=False) as vs:
make_game.preflight(["runtime"], os.path.join(build, "w"), runtime_build=build)
vs.assert_not_called()
return err.getvalue()
def test_runtime_off_windows_passes_with_a_complete_build(self):
with tempfile.TemporaryDirectory() as build:
self.preflight(build, make_game.RUNTIME_FILES)
def test_runtime_build_without_the_tls_host_fails_before_any_work(self):
err = io.StringIO()
with tempfile.TemporaryDirectory() as build:
with self.assertRaises(SystemExit) as ctx:
self.preflight(build, ["gta6rt.exe"], err=err)
self.assertEqual(ctx.exception.code, 1)
self.assertIn("gta6rt_tls_host.dll", err.getvalue())
self.assertFalse(os.path.exists(os.path.join(build, "w")))
class RuntimePackagingTest(unittest.TestCase):
def setUp(self):
self.root = self.enterContext(tempfile.TemporaryDirectory())
self.build = os.path.join(self.root, "runtime", "build")
self.game = os.path.join(self.root, "game")
self.out = os.path.join(self.root, "out")
os.makedirs(self.build)
os.makedirs(self.game)
self.write_artifact("gta6rt.exe", b"runtime")
def write_artifact(self, name, contents):
with open(os.path.join(self.build, name), "wb") as f:
f.write(contents)
def package(self):
with mock.patch.object(make_game.os, "name", "nt"), \
mock.patch.object(make_game, "ROOT", self.root), \
mock.patch.object(make_game, "run"), \
mock.patch.object(make_game, "log"):
make_game.runtime(self.game, self.out, {"expected_exe": "game.exe"})
def test_runtime_copies_executable_and_tls_host_together(self):
self.write_artifact("gta6rt_tls_host.dll", b"TLS host")
self.package()
with open(os.path.join(self.out, "gta6rt.exe"), "rb") as f:
self.assertEqual(f.read(), b"runtime")
with open(os.path.join(self.out, "gta6rt_tls_host.dll"), "rb") as f:
self.assertEqual(f.read(), b"TLS host")
with open(os.path.join(self.out, "game_path.txt"), encoding="utf-8") as f:
self.assertEqual(f.read(), self.game)
def test_runtime_rejects_missing_tls_host_before_copying(self):
err = io.StringIO()
with mock.patch("sys.stderr", err), self.assertRaises(SystemExit) as ctx:
self.package()
self.assertEqual(ctx.exception.code, 1)
self.assertIn("gta6rt_tls_host.dll", err.getvalue())
self.assertFalse(os.path.exists(self.out))
class ConfiguredCrossToolsTest(unittest.TestCase):
def test_reads_cmakes_compiler_files_newest_last(self):
root = self.enterContext(tempfile.TemporaryDirectory())
files = os.path.join(root, "runtime", "build", "windows", "CMakeFiles")
with mock.patch.object(make_game, "ROOT", root):
self.assertEqual(make_game.configured_cross_tools(), {})
for version, compiler in (("3.31.0", "/old/clang-cl"), ("4.5.1", "/usr/bin/clang-cl")):
os.makedirs(os.path.join(files, version))
with open(os.path.join(files, version, "CMakeCXXCompiler.cmake"), "w", encoding="utf-8") as f:
f.write(f'set(CMAKE_CXX_COMPILER "{compiler}")\nset(CMAKE_CXX_COMPILER_ARG1 "")\n'
'set(CMAKE_AR "llvm-lib")\nset(CMAKE_LINKER "/usr/bin/lld-link")\n'
'set(CMAKE_MT "/usr/bin/llvm-mt")\n')
os.utime(os.path.join(files, version, "CMakeCXXCompiler.cmake"), (1, 1 if version == "3.31.0" else 2))
with open(os.path.join(files, "4.5.1", "CMakeRCCompiler.cmake"), "w", encoding="utf-8") as f:
f.write('set(CMAKE_RC_COMPILER "llvm-rc")\n')
with mock.patch.object(make_game, "ROOT", root):
self.assertEqual(make_game.configured_cross_tools(), {
"CMAKE_CXX_COMPILER": "/usr/bin/clang-cl", "CMAKE_AR": "llvm-lib", "CMAKE_LINKER": "/usr/bin/lld-link",
"CMAKE_MT": "/usr/bin/llvm-mt", "CMAKE_RC_COMPILER": "llvm-rc"})
class CrossBuildTest(unittest.TestCase):
def setUp(self):
self.root = self.enterContext(tempfile.TemporaryDirectory())
self.build = os.path.join(self.root, "runtime", "build", "windows")
self.game = os.path.join(self.root, "game")
self.out = os.path.join(self.root, "out")
os.makedirs(self.game)
def runtime(self, xwin="/sdk", ninja=True, suffix="", configured=None):
def which(t):
if t == "ninja":
return "/usr/bin/ninja" if ninja else None
return f"/usr/bin/{t}" if suffix else None
def fake_run(args, hint="", **kw):
if args[1] == "--build":
os.makedirs(self.build, exist_ok=True)
for name in make_game.RUNTIME_FILES:
with open(os.path.join(self.build, name), "wb") as f:
f.write(name.encode())
with mock.patch.object(make_game.os, "name", "posix"), \
mock.patch.object(make_game, "ROOT", self.root), \
mock.patch.object(make_game, "xwin_dir", return_value=xwin), \
mock.patch.object(make_game.shutil, "which", side_effect=which), \
mock.patch.object(make_game, "cross_tool_suffix", return_value=suffix), \
mock.patch.object(make_game, "configured_cross_tools", return_value=configured or {}), \
mock.patch.object(make_game, "run", side_effect=fake_run) as self.run_mock, \
mock.patch.object(make_game, "log"):
make_game.runtime(self.game, self.out, {"expected_exe": "game.exe"}, None, 4)
return [c.args[0] for c in self.run_mock.call_args_list]
def test_cross_builds_the_loader_and_packages_it_for_wine(self):
configure, build = self.runtime()
toolchain = os.path.join(self.root, "runtime", "cmake", "clang-cl-xwin.cmake")
self.assertEqual(configure, ["cmake", "-S", os.path.join(self.root, "runtime"), "-B", self.build,
"-G", "Ninja", "-DCMAKE_BUILD_TYPE=RelWithDebInfo",
"--toolchain", toolchain, "-DXWIN_DIR=/sdk"])
self.assertEqual(build, ["cmake", "--build", self.build, "--parallel", "4",
"--target", "gta6rt", "gta6rt_tls_host"])
for name in make_game.RUNTIME_FILES:
with open(os.path.join(self.out, name), "rb") as f:
self.assertEqual(f.read(), name.encode())
self.assertTrue(os.path.isfile(os.path.join(self.out, "gta6rt.sh")))
def test_versioned_tools_are_passed_until_the_folder_records_its_tools(self):
for cache in (False, True):
if cache:
open(os.path.join(self.build, "CMakeCache.txt"), "w").close()
configure, _ = self.runtime(suffix="-21")
for var, name in make_game.CROSS_VARS.items():
self.assertIn(f"-DCMAKE_{var}=/usr/bin/{name}-21", configure)
configure, _ = self.runtime(suffix="-21", configured={"CMAKE_CXX_COMPILER": "/usr/bin/clang-cl-21"})
self.assertFalse(any(a.startswith("-DCMAKE_CXX_COMPILER") for a in configure))
def test_without_ninja_cmake_picks_the_generator(self):
configure, _ = self.runtime(ninja=False)
self.assertNotIn("-G", configure)
self.assertIn("-DCMAKE_BUILD_TYPE=RelWithDebInfo", configure)
def test_an_existing_build_folder_keeps_its_generator_and_build_type(self):
os.makedirs(self.build)
open(os.path.join(self.build, "CMakeCache.txt"), "w").close()
configure, _ = self.runtime()
self.assertNotIn("-G", configure)
self.assertFalse(any(a.startswith("-DCMAKE_BUILD_TYPE") for a in configure))
self.assertIn("-DXWIN_DIR=/sdk", configure)
def test_without_an_sdk_nothing_runs(self):
err = io.StringIO()
with mock.patch("sys.stderr", err), self.assertRaises(SystemExit):
self.runtime(xwin=None)
self.run_mock.assert_not_called()
self.assertIn("xwin", err.getvalue())
self.assertFalse(os.path.exists(self.out))
class RuntimeBuildPackagingTest(unittest.TestCase):
def test_off_windows_copies_the_build_and_writes_a_wine_launcher(self):
with tempfile.TemporaryDirectory() as root:
build, game, out = (os.path.join(root, n) for n in ("build", "game", "out"))
os.makedirs(build)
os.makedirs(game)
for name in make_game.RUNTIME_FILES:
with open(os.path.join(build, name), "wb") as f:
f.write(name.encode())
with mock.patch.object(make_game.os, "name", "posix"), \
mock.patch.object(make_game, "run") as run, \
mock.patch.object(make_game, "log"):
make_game.runtime(game, out, {"expected_exe": "game.exe"}, build)
run.assert_not_called()
for name in make_game.RUNTIME_FILES:
with open(os.path.join(out, name), "rb") as f:
self.assertEqual(f.read(), name.encode())
launcher = os.path.join(out, "gta6rt.sh")
with open(launcher, encoding="utf-8") as f:
text = f.read()
self.assertTrue(text.startswith("#!/bin/sh\n"))
self.assertIn('"${WINE:-wine}" "$here/gta6rt.exe" "$@"', text)
if os.name != "nt":
self.assertTrue(os.access(launcher, os.X_OK))
class PipelineCheckExeTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.build = os.path.join(self.tmp.name, "runtime", "build")
os.makedirs(os.path.join(self.build, "linux"))
self.root = mock.patch.object(make_game, "ROOT", self.tmp.name)
self.root.start()
def tearDown(self):
self.root.stop()
self.tmp.cleanup()
def touch(self, *parts):
p = os.path.join(self.build, *parts)
open(p, "w").close()
return p
def exe(self, os_name):
with mock.patch.object(make_game.os, "name", os_name):
return make_game.pipeline_check_exe()
def test_linux_prefers_the_readme_build_folder(self):
self.assertIsNone(self.exe("posix"))
plain = self.touch("vk_pipeline_check")
self.assertEqual(self.exe("posix"), plain)
readme = self.touch("linux", "vk_pipeline_check")
self.assertEqual(self.exe("posix"), readme)
def test_windows_finds_only_the_exe(self):
self.touch("linux", "vk_pipeline_check")
self.assertIsNone(self.exe("nt"))
exe = self.touch("vk_pipeline_check.exe")
self.assertEqual(self.exe("nt"), exe)
def test_pipelines_off_windows_runs_the_linux_build(self):
exe = self.touch("linux", "vk_pipeline_check")
work = os.path.join(self.tmp.name, "work")
os.makedirs(os.path.join(work, "shaders", "spirv"))
open(os.path.join(work, "shaders", "spirv", "index.json"), "w").close()
out = os.path.join(self.tmp.name, "out")
with mock.patch.object(make_game.os, "name", "posix"), \
mock.patch.object(make_game, "log"), \
mock.patch.object(make_game, "run") as run:
make_game.pipelines(work, out)
self.assertEqual(run.call_args.args[0][0], exe)
def test_pipelines_off_windows_without_the_build_says_how_to_build_it(self):
err = io.StringIO()
with mock.patch.object(make_game.os, "name", "posix"), mock.patch("sys.stderr", err):
with self.assertRaises(SystemExit):
make_game.pipelines(os.path.join(self.tmp.name, "work"), os.path.join(self.tmp.name, "out"))
self.assertIn("cmake -S runtime -B runtime/build/linux", err.getvalue())
if __name__ == "__main__":
unittest.main()