Files
mandy/tests/test_build_runtime_cmake.py
NotRexed 00131bb85e main keeps its own runtime, tools and tests; the branch's variants stay on port/d3d12-nvidia
After the merge the tree held this branch's older states of files main maintains. Run against main's code,
tests/test_acphal_abi.py and 11 of the 15 translator tests that differed failed, and the C++ runtime would have
been built from two generations of its sources. So that the merge cannot break what main builds and tests:

- runtime/ is main's, file for file (68 files only this branch had are not carried over);
- every file under tools/, tests/ and docs/ that both sides have is main's (80 files);
- ten translator tests only this branch has, which test rules main's translator does not have, and their
  fixtures are left out.

What main gains: d3d12/ (the Rust D3D12 runtime), its launchers and helpers (Launcher.bat, scripts/play*.ps1,
play-linux.sh, port_ctl.py, smooth-motion.ps1, fetch_vkd3d_proton.py, sync_victory.py), mods/example_lua, eleven
notes under docs/, seven tools and the tests that pass with main's code, the README's sections and credits, and
the "Runtime traps" section of AGENTS.md. Everything left out here is on the branch port/d3d12-nvidia.

Checked: compileall; make_game.py --help; check_no_game_data.py over every file this adds to main; the
line-ending, docs-encoding, CI-registration and docs tests; each test file that differs from main, on its own.
Not run: the whole translator suite, the C++ build, cargo over d3d12/ after the merge (d3d12/ is byte for byte the
branch's), CI.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-27 17:58:38 -05:00

119 lines
6.0 KiB
Python

"""Run the runtime wrapper's real CMake batch branch with a native argument recorder.
Windows only; needs the .NET Framework C# compiler, not MSVC, CMake, a GPU or game files.
The recorder validates configure paths and lets either stage fail independently.
"""
import base64
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "build_runtime.ps1"
CSC = Path(os.environ.get("WINDIR", r"C:\Windows")) / "Microsoft.NET/Framework64/v4.0.30319/csc.exe"
RECORDER = r'''
using System;
using System.IO;
class Recorder {
static int Main(string[] args) {
bool build = args.Length > 0 && args[0] == "--build";
string stage = build ? "build" : "configure";
File.WriteAllLines(Path.Combine(Environment.GetEnvironmentVariable("LEONIDA_CMAKE_LOG"), stage + ".txt"), args);
if (!build) {
for (int i = 0; i + 1 < args.Length; ++i)
if (args[i] == "-S" && !Directory.Exists(args[i + 1])) return 9;
}
return Environment.GetEnvironmentVariable("LEONIDA_CMAKE_FAIL") == stage ? 7 : 0;
}
}
'''
@unittest.skipUnless(os.name == "nt" and CSC.is_file(), "requires Windows PowerShell and .NET Framework csc")
class CMakeBatchTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temp = tempfile.TemporaryDirectory(prefix="leonida_cmake_")
cls.addClassCleanup(cls.temp.cleanup)
cls.base = Path(cls.temp.name)
source = cls.base / "recorder.cs"
source.write_text(RECORDER, encoding="utf-8", newline="\n")
cls.recorder = cls.base / "recorder.exe"
subprocess.run([str(CSC), "/nologo", "/target:exe", "/out:" + str(cls.recorder), str(source)],
check=True, capture_output=True, timeout=30)
def run_build(self, folder, extension, fail=""):
text = SCRIPT.read_text(encoding="utf-8-sig")
start = text.index('$cfg = if ($Debug)')
end = text.index('} else {', text.index('if ($cmake -and $ninja)', start))
branch = text[start:end] + "}\n"
writer = text[text.index('$bat = Join-Path $out "_build.cmd"'):]
root = folder / "checkout"
src, out = root / "runtime", root / "runtime/build"
out.mkdir(parents=True)
tools = folder / "tools ^ & 東京"
tools.mkdir()
backend = tools / "recorder.exe"
shutil.copyfile(self.recorder, backend)
cmake = tools / ("cmake" + extension)
if extension == ".exe":
shutil.copyfile(self.recorder, cmake)
else:
cmake.write_text('@echo off\nchcp 65001 >nul\n"' + str(backend) + '" %*\nexit /b %errorlevel%\n',
encoding="utf-8", newline="\r\n")
# The real vcvars lives in the VS installation, outside the checkout.
vcvars = self.base / "vcvars64.bat"
vcvars.write_text('@exit /b 0\n', encoding="ascii", newline="\r\n")
ninja = tools / "ninja.exe"
command = ("$ErrorActionPreference = 'Stop'\n"
"$root = $env:LEONIDA_CMAKE_ROOT\n"
"$src = Join-Path $root 'runtime'\n"
"$out = Join-Path $src 'build'\n"
"$installer = $env:LEONIDA_CMAKE_INSTALLER\n"
"$vcvars = Join-Path $installer 'vcvars64.bat'\n"
"$cmake = $env:LEONIDA_CMAKE_EXE\n"
"$ninja = $env:LEONIDA_CMAKE_NINJA\n"
"$Debug = $false\n$FetchSdl3 = $true\n$jobs = 3\n"
+ branch + writer)
encoded = base64.b64encode(command.encode("utf-16-le")).decode("ascii")
result = subprocess.run(
["powershell.exe", "-NoProfile", "-NonInteractive", "-EncodedCommand", encoded],
env=dict(os.environ, LEONIDA_CMAKE_ROOT=str(root), LEONIDA_CMAKE_LOG=str(folder),
LEONIDA_CMAKE_INSTALLER=str(self.base),
LEONIDA_CMAKE_EXE=str(cmake), LEONIDA_CMAKE_NINJA=str(ninja),
LEONIDA_CMAKE_FAIL=fail, VULKAN_SDK=str(folder / "vulkan")),
capture_output=True, timeout=45,
)
return result, src, out, ninja
def test_native_and_batch_cmake_paths_and_exit_codes(self):
for name in ("plain", "caret^dir", "пробел ^ & 東京"):
for extension in (".exe", ".cmd", ".bat"):
for fail in ("", "configure", "build"):
with self.subTest(folder=name, extension=extension, fail=fail):
with tempfile.TemporaryDirectory(dir=self.base) as tmp:
folder = Path(tmp) / name
folder.mkdir()
result, src, out, ninja = self.run_build(folder, extension, fail)
batch = (out / "_build.cmd").read_bytes()
self.assertNotIn(b"\n", batch.replace(b"\r\n", b""))
self.assertEqual(result.returncode, 1 if fail else 0,
(result.stdout + result.stderr).decode(errors="replace"))
configure = (folder / "configure.txt").read_text(encoding="utf-8-sig").splitlines()
self.assertEqual(configure, ["-S", str(src), "-B", str(out), "-G", "Ninja",
"-DCMAKE_MAKE_PROGRAM=" + str(ninja),
"-DCMAKE_BUILD_TYPE=RelWithDebInfo",
"-DLEONIDA_SDL3_FETCH=ON", "-DLEONIDA_REQUIRE_VULKAN=ON"])
built = folder / "build.txt"
if fail == "configure":
self.assertFalse(built.exists(), "configure failure must stop before build")
else:
self.assertEqual(built.read_text(encoding="utf-8-sig").splitlines(),
["--build", str(out), "-j", "3"])
if __name__ == "__main__":
unittest.main()