okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-pkg-nommq f1d2467b5 into the batch 19 candidate (packaging: strata_kq only where the tree has it, the Windows NVIDIA recipe; prefill sizes expert matrices from Strata's own type table)bc09a17e5

main
5.2 KiB120 linesraw
1"""The CUDA Linux package scripts build strata_kq only when the build tree has it (packaging/engine_targets.sh).
2
3A build configured with -DSTRATA_MMQ=OFF has no strata_kq target; `--target strata strata_kq` stopped such a package build with
4"ninja: error: unknown target 'strata_kq'". The scripts now ask the build tree; a build with MMQ builds the same two targets as before.
5
6    python -m unittest tools.test_engine_targets -v
7"""
8from __future__ import annotations
9
10import os
11import shutil
12import stat
13import subprocess
14import sys
15import tempfile
16import unittest
17from pathlib import Path
18
19ROOT = Path(__file__).resolve().parents[1]
20HELPER = ROOT / "packaging" / "engine_targets.sh"
21SCRIPTS = {"packaging/build_cuda_linux.sh": "cmake", "spark/build_engine.sh": '"$cmake"'}
22
23FAKE_CMAKE = """#!/bin/sh
24echo "$*" >> "$FAKE_ARGS"
25case "$FAKE_MODE" in
26  kq) echo "[1/3] Building CUDA object libstrata_kq.so"; exit 0 ;;
27  nokq) echo "ninja: error: unknown target 'strata_kq'"; exit 1 ;;
28  nokq_hint) echo "ninja: error: unknown target 'strata_kq', did you mean 'strata'?"; exit 1 ;;
29  make_nokq) echo "make: *** No rule to make target 'strata_kq'.  Stop."; exit 2 ;;
30  *) echo "ninja: error: loading 'build.ninja': No such file or directory"; exit 1 ;;
31esac
32"""
33
34
35def run_helper(cmake: str, build: str, env: dict | None = None) -> subprocess.CompletedProcess:
36    return subprocess.run(["sh", "-c", '. "$0"; engine_targets "$1" "$2"', str(HELPER), cmake, build],
37                          capture_output=True, text=True, env=env, timeout=600)
38
39
40@unittest.skipIf(os.name == "nt" or shutil.which("sh") is None, "POSIX sh")
41class EngineTargetsFake(unittest.TestCase):
42    def setUp(self):
43        self.tmp = Path(tempfile.mkdtemp())
44        self.cmake = self.tmp / "cmake"
45        self.cmake.write_text(FAKE_CMAKE, encoding="utf-8", newline="\n")
46        self.cmake.chmod(self.cmake.stat().st_mode | stat.S_IXUSR)
47        self.args = self.tmp / "args.txt"
48
49    def tearDown(self):
50        shutil.rmtree(self.tmp, ignore_errors=True)
51
52    def call(self, mode: str) -> subprocess.CompletedProcess:
53        env = dict(os.environ, FAKE_MODE=mode, FAKE_ARGS=str(self.args))
54        return run_helper(str(self.cmake), "/b/build-x", env)
55
56    def test_a_tree_with_strata_kq_builds_both_targets_as_before(self):
57        r = self.call("kq")
58        self.assertEqual(r.returncode, 0, r.stderr)
59        self.assertEqual(r.stdout, "strata strata_kq\n")
60        self.assertEqual(self.args.read_text().split(), ["--build", "/b/build-x", "--target", "strata_kq", "--", "-n"])
61
62    def test_a_tree_without_strata_kq_builds_strata_alone(self):
63        for mode in ("nokq", "nokq_hint", "make_nokq"):
64            r = self.call(mode)
65            self.assertEqual((r.returncode, r.stdout), (0, "strata\n"), mode)
66
67    def test_any_other_failure_stops_the_build_and_shows_why(self):
68        r = self.call("broken")
69        self.assertNotEqual(r.returncode, 0)
70        self.assertEqual(r.stdout, "")
71        self.assertIn("loading 'build.ninja'", r.stderr)
72        self.assertIn("cannot tell", r.stderr)
73
74
75@unittest.skipIf(os.name == "nt" or not (shutil.which("sh") and shutil.which("cmake") and shutil.which("ninja")), "sh, cmake and ninja")
76class EngineTargetsRealNinja(unittest.TestCase):
77    """A real CMake + Ninja tree with and without a strata_kq target: the dry run builds nothing."""
78
79    def tree(self, with_kq: bool) -> Path:
80        src = self.tmp / ("src-kq" if with_kq else "src")
81        src.mkdir()
82        made = self.tmp / ("made-kq" if with_kq else "made")
83        lines = ["cmake_minimum_required(VERSION 3.16)", "project(t NONE)",
84                 "add_custom_target(strata COMMAND ${CMAKE_COMMAND} -E touch strata.out)"]
85        if with_kq:
86            lines.append(f'add_custom_target(strata_kq COMMAND ${{CMAKE_COMMAND}} -E touch "{made.as_posix()}")')
87        (src / "CMakeLists.txt").write_text("\n".join(lines) + "\n", encoding="utf-8")
88        build = self.tmp / ("build-kq" if with_kq else "build")
89        subprocess.run(["cmake", "-G", "Ninja", "-S", str(src), "-B", str(build)], check=True, capture_output=True, timeout=600)
90        return build
91
92    def setUp(self):
93        self.tmp = Path(tempfile.mkdtemp())
94
95    def tearDown(self):
96        shutil.rmtree(self.tmp, ignore_errors=True)
97
98    def test_real_trees(self):
99        r = run_helper("cmake", str(self.tree(True)))
100        self.assertEqual((r.returncode, r.stdout), (0, "strata strata_kq\n"), r.stderr)
101        self.assertFalse((self.tmp / "made-kq").exists(), "the dry run ran a build command")
102        r = run_helper("cmake", str(self.tree(False)))
103        self.assertEqual((r.returncode, r.stdout), (0, "strata\n"), r.stderr)
104
105
106class ScriptsUseTheHelper(unittest.TestCase):
107    def test_the_cuda_linux_scripts_ask_the_build_tree(self):
108        for rel, cmake in SCRIPTS.items():
109            text = (ROOT / rel).read_text(encoding="utf-8")
110            self.assertNotIn("--target strata strata_kq", text, rel)
111            self.assertIn('. "$repo/packaging/engine_targets.sh"', text, rel)
112            self.assertIn(f'targets=$(engine_targets {cmake} "$build")', text, rel)
113            self.assertIn('--target $targets', text, rel)
114
115    def test_the_helper_is_lf(self):
116        self.assertNotIn(b"\r", HELPER.read_bytes())
117
118
119if __name__ == "__main__":
120    sys.exit(unittest.main())