Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

frontend-qt: forward run() kwargs in driver.py and bundle glslangValidator.exe

LNPANDYededcdab0a10
diff --git a/.github/workflows/build-qt.yml b/.github/workflows/build-qt.yml
index 8c3439ea..7c9e7d44 100644
--- a/.github/workflows/build-qt.yml
+++ b/.github/workflows/build-qt.yml
@@ -54,12 +54,13 @@ jobs:
python tools/engine.py tree --out engine-tree
du -sh engine-tree
- - name: Build Windows spirv-cross.exe for the bundled engine
+ - name: Build Windows shader tools for the bundled engine
shell: bash
run: |
# The engine tree ships a macOS Mach-O spirv-cross binary in dm/gfx/tools/spirv_cross_bin/.
- # Build the Windows executable from the exact commit build.sh pins so every NVN, Vulkan and
- # OpenGL shader build works out of the box without a manual Vulkan SDK install.
+ # Build the Windows executable from the exact commit build.sh pins, and bundle Khronos's
+ # standalone glslangValidator.exe alongside it so NVN, Vulkan and OpenGL shader builds
+ # all work out of the box without a manual Vulkan SDK install.
TAG=$(sed -n 's/^TAG=\([^ ]*\).*/\1/p' engine-tree/dm/gfx/tools/spirv_cross_bin/build.sh)
COMMIT=$(sed -n 's/^COMMIT=\([^ ]*\).*/\1/p' engine-tree/dm/gfx/tools/spirv_cross_bin/build.sh)
git clone --quiet --depth 1 --branch "$TAG" https://github.com/KhronosGroup/SPIRV-Cross.git spirv-cross-src
@@ -73,6 +74,16 @@ jobs:
engine-tree/dm/gfx/tools/spirv_cross_bin/spirv-cross.exe --revision
rm -rf spirv-cross-src spirv-cross-build
+ GLSLANG_URL="https://github.com/KhronosGroup/glslang/releases/download/16.6.0/glslang-16.6.0-windows-x86_64-release.zip"
+ GLSLANG_SHA="82bf434e69b9bb4829de7e2b4bc2c5e7a7861e53d66cf75e5cc70f5f694a8d9b"
+ curl -fsSL "$GLSLANG_URL" -o glslang.zip
+ got_sha=$(sha256sum glslang.zip | awk '{print $1}')
+ [ "$got_sha" = "$GLSLANG_SHA" ] || { echo "glslang.zip sha256 is $got_sha, expected $GLSLANG_SHA" >&2; exit 1; }
+ unzip -q glslang.zip bin/glslang.exe
+ cp bin/glslang.exe engine-tree/dm/gfx/tools/spirv_cross_bin/glslangValidator.exe
+ engine-tree/dm/gfx/tools/spirv_cross_bin/glslangValidator.exe --version
+ rm -rf glslang.zip bin
+
- name: Build
working-directory: frontend-qt
shell: bash
diff --git a/frontend-qt/nspb_qt/win/driver.py b/frontend-qt/nspb_qt/win/driver.py
index 56887934..510b5d01 100644
--- a/frontend-qt/nspb_qt/win/driver.py
+++ b/frontend-qt/nspb_qt/win/driver.py
@@ -153,7 +153,7 @@ def patch(mod: ModuleType, repo: Path, *, j: int | None = None) -> None:
_UPSTREAM_RUN = mod.run
# ---------------------------------------------------------------- prereqs
- def check_prereqs(env, xlate, mac=False):
+ def check_prereqs(env, xlate, mac=False, *a, **kw):
if mac:
raise BuildError('the Mac platforms need a Mac: only iOS builds run on Windows')
bad = [p for p in tc.check_all(deep=True) if p.required and not p.ok]
@@ -169,7 +169,7 @@ def patch(mod: ModuleType, repo: Path, *, j: int | None = None) -> None:
mod.check_prereqs = check_prereqs
# ---------------------------------------------------------------- nx_extract
- def build_nx_extract(repo_: Path, work: Path, logs: Path, env):
+ def build_nx_extract(repo_: Path, work: Path, logs: Path, env, *a, **kw):
"""The .NET build, but on Windows the output is `nx_extract.exe`."""
import tempfile
nx = work / 'tools/nx_extract/nx_extract'
@@ -191,12 +191,12 @@ def patch(mod: ModuleType, repo: Path, *, j: int | None = None) -> None:
mod.build_nx_extract = build_nx_extract
# ---------------------------------------------------------------- APFS clones
- mod.can_clone = lambda d: False
- mod.apple_silicon = lambda: True # only the heap estimate below asks
- mod.ALLOW_OTHER_METAL = True # Windows Metal Tools version differs from Xcode's
+ mod.can_clone = lambda *a, **kw: False
+ mod.apple_silicon = lambda *a, **kw: True # only the heap estimate below asks
+ mod.ALLOW_OTHER_METAL = True # Windows Metal Tools version differs from Xcode's
# ---------------------------------------------------------------- the Mac host
- def build_mac_host(repo_, out, env, logs, g):
+ def build_mac_host(repo_, out, env, logs, g, *a, **kw):
raise BuildError(
'this step boots the game\'s own arm64 code in a Mac host, which no Windows PC can '
'run. It is needed only for a game whose shaders appear at run time (GLSLC / Vulkan / '
@@ -205,7 +205,7 @@ def patch(mod: ModuleType, repo: Path, *, j: int | None = None) -> None:
'do not need it.')
mod.build_mac_host = build_mac_host
- def heap_check(repo_, d, patched, work, env, logs, g):
+ def heap_check(repo_, d, patched, work, env, logs, g, *a, **kw):
"""The heap check without the Mac host.
Upstream boots the game for a few seconds to read its heap request. Without that, a game
@@ -240,7 +240,7 @@ def patch(mod: ModuleType, repo: Path, *, j: int | None = None) -> None:
return type(g)(g.dir)
mod.heap_check = heap_check
- def region_skip(repo_, d, patched, work, env, logs, g):
+ def region_skip(repo_, d, patched, work, env, logs, g, *a, **kw):
"""The address-region check without the Mac host.
Upstream boots the game in the Mac host to report where its address regions reach against
@@ -297,7 +297,7 @@ def patch(mod: ModuleType, repo: Path, *, j: int | None = None) -> None:
_orig_rewrite = mod._nspb_orig_rewrite
_orig_patch_files = mod._nspb_orig_patch_files
- def rewrite(py_, repo_, exe_dir, out, env, logs, g, provisional=False):
+ def rewrite(py_, repo_, exe_dir, out, env, logs, g, provisional=False, *a, **kw):
import json
from . import sdk_auto_cuts
sdk_nso = Path(exe_dir) / 'sdk'
@@ -331,10 +331,10 @@ def patch(mod: ModuleType, repo: Path, *, j: int | None = None) -> None:
else:
os.environ.pop('NSPB_AUTO_SDK_CUTS', None)
env.pop('NSPB_AUTO_SDK_CUTS', None)
- return _orig_rewrite(py_, repo_, exe_dir, out, env, logs, g, provisional)
+ return _orig_rewrite(py_, repo_, exe_dir, out, env, logs, g, provisional, *a, **kw)
- def patch_files(repo_, exe_dir, mods, game_id=None):
- f = _orig_patch_files(repo_, exe_dir, mods, game_id)
+ def patch_files(repo_, exe_dir, mods, game_id=None, *a, **kw):
+ f = _orig_patch_files(repo_, exe_dir, mods, game_id, *a, **kw)
j = os.environ.get('NSPB_AUTO_SDK_CUTS')
return f + ([Path(j)] if j and Path(j).exists() else [])
@@ -342,7 +342,7 @@ def patch(mod: ModuleType, repo: Path, *, j: int | None = None) -> None:
mod.patch_files = patch_files
# ---------------------------------------------------------------- the shader cache key
- def toolchain_probe(repo_=None, env=None):
+ def toolchain_probe(repo_=None, env=None, *a, **kw):
"""What the shader-stage cache keys record as "the compilers".
Upstream records `xcodebuild -version` and fingerprints the SPIRV-Cross
@@ -375,7 +375,7 @@ def patch(mod: ModuleType, repo: Path, *, j: int | None = None) -> None:
mod.capture_glslc = _refuse_capture('this game')
mod.capture_shaders = _refuse_capture('this game')
- def capture_jit(py_, repo_, d, patched, work, env, logs, g):
+ def capture_jit(py_, repo_, d, patched, work, env, logs, g, *a, **kw):
if (Path(d) / 'jit' / 'jit.img').is_file():
say(f'reusing the captured JIT image: {Path(d) / "jit/jit.img"}')
return
@@ -410,10 +410,11 @@ def patch(mod: ModuleType, repo: Path, *, j: int | None = None) -> None:
mod.TOOL_PATHS = extra
# ---------------------------------------------------------------- the app build
- def run_ipa(cmd, log, env=None, cwd=None, on_line=None):
+ def run_ipa(cmd, log, *a, **kw):
"""Intercept the `bash dm/ios/tools/build_ipa.sh` call and run `build_app.py` instead.
- The signature matches upstream's `run()`, so the caller's logging, progress and error
+ The signature forwards all positional and keyword arguments (`env`, `cwd`, `on_line`,
+ `ok`, `fatal`, etc.) to upstream's `run()`, so the caller's logging, progress and error
reporting are reused unchanged; only the command is swapped.
"""
cmd = [str(c) for c in cmd]
@@ -423,7 +424,7 @@ def patch(mod: ModuleType, repo: Path, *, j: int | None = None) -> None:
raise BuildError('--prepare-xcode needs Xcode and xcodegen: drop it on Windows')
cmd = [sys.executable, str(Path(__file__).resolve().parent / 'build_app.py'),
*cmd[2:]]
- return _UPSTREAM_RUN(cmd, log, env=env, cwd=cwd, on_line=on_line)
+ return _UPSTREAM_RUN(cmd, log, *a, **kw)
mod.run = run_ipa
@@ -441,6 +442,14 @@ def main(argv: list[str] | None = None) -> int:
env = shim.build_env(python_exe=sys.executable)
os.environ.clear()
os.environ.update(env)
+ sdir = str(shim.shim_dir())
+ if sdir not in sys.path:
+ sys.path.insert(0, sdir)
+ if 'sitecustomize' not in sys.modules:
+ try:
+ import sitecustomize # noqa: F401
+ except Exception:
+ pass
mod = load_upstream(repo)
patch(mod, repo)
diff --git a/frontend-qt/nspb_qt/win/toolchain.py b/frontend-qt/nspb_qt/win/toolchain.py
index 188311ad..51bc1ec1 100644
--- a/frontend-qt/nspb_qt/win/toolchain.py
+++ b/frontend-qt/nspb_qt/win/toolchain.py
@@ -815,9 +815,10 @@ def find_exe(name: str) -> Path | None:
own = tools_dir() / f'zstd-{ZSTD_VERSION}' / f'{name}.exe'
if name == 'zstd' and own.is_file():
return own
- if name == 'spirv-cross':
- if env_sc := os.environ.get('NSPB_SPIRV_CROSS'):
- p = Path(env_sc)
+ if name in ('spirv-cross', 'glslangValidator'):
+ env_key = 'NSPB_SPIRV_CROSS' if name == 'spirv-cross' else 'NSPB_GLSLANG_VALIDATOR'
+ if env_bin := os.environ.get(env_key):
+ p = Path(env_bin)
if p.is_file():
return p
here = Path(__file__).resolve()
@@ -831,7 +832,7 @@ def find_exe(name: str) -> Path | None:
pass
for r in roots:
if r:
- p = Path(r) / 'dm' / 'gfx' / 'tools' / 'spirv_cross_bin' / 'spirv-cross.exe'
+ p = Path(r) / 'dm' / 'gfx' / 'tools' / 'spirv_cross_bin' / f'{name}.exe'
if p.is_file():
return p
for cand in (f'{name}.exe', name):
diff --git a/frontend-qt/tests/test_win.py b/frontend-qt/tests/test_win.py
index dd6b1215..95169104 100644
--- a/frontend-qt/tests/test_win.py
+++ b/frontend-qt/tests/test_win.py
@@ -2254,6 +2254,42 @@ class WindowsBuildFixesTest(unittest.TestCase):
self.assertEqual((out / 'romfs' / 'romfs.img').read_bytes(), b'DMROMFS1_payload')
self.assertEqual((out / 'romfs' / 'aoc' / '0.img').read_bytes(), b'dlc0')
+ def test_run_ipa_forwards_ok_and_fatal_kwargs(self) -> None:
+ from nspb_qt.win import driver
+ repo = QT.parent / 'engine'
+ if not (repo / 'scripts/build_ipa_from_nsp.py').is_file():
+ self.skipTest('engine/ is missing')
+ mod = driver.load_upstream(repo)
+ recorded = []
+
+ def fake_run(cmd, log, env=None, cwd=None, on_line=None, ok=(), fatal=True):
+ recorded.append((list(cmd), ok, fatal))
+ return 0
+
+ orig = driver._UPSTREAM_RUN
+ try:
+ driver._UPSTREAM_RUN = fake_run
+ driver.patch(mod, repo)
+ mod.run(['nx_extract', 'keys.txt', '--keys', 'a.nsp'], Path('k.log'),
+ env={}, ok=(6,), fatal=False)
+ finally:
+ driver._UPSTREAM_RUN = orig
+ driver.patch(mod, repo)
+ self.assertEqual(len(recorded), 1)
+ self.assertEqual(recorded[0][1], (6,))
+ self.assertIs(recorded[0][2], False)
+
+ def test_find_exe_locates_bundled_glslang_validator(self) -> None:
+ with tempfile.TemporaryDirectory() as td:
+ fake_engine = Path(td) / 'engine'
+ bin_dir = fake_engine / 'dm' / 'gfx' / 'tools' / 'spirv_cross_bin'
+ bin_dir.mkdir(parents=True)
+ spv = bin_dir / 'glslangValidator.exe'
+ spv.write_bytes(b'MZ')
+ with mock.patch.dict(os.environ, {'NSPB_REPO': str(fake_engine)}, clear=False), \
+ mock.patch.object(tc, 'setting', return_value=None):
+ self.assertEqual(tc.find_exe('glslangValidator'), spv)
+
if __name__ == '__main__':
unittest.main(verbosity=2)