Skip to content

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

37.6 KB
Raw Blame
"""The process-boundary shim: everything `driver.py` cannot reach from inside Python.

`driver.py` can replace functions in the upstream module, but it cannot change what a *child*
Python process or a shell does. The upstream pipeline starts a lot of those:

* `dm/gfx/tools/dmsh_chain.py` calls `which spirv-cross` and `xcrun -sdk ... metal --version`
  while it fingerprints the shader chain — and it runs inside `dmsh_build.py`, `dmsh_facts.py`
  and the cache-key code of the NSP script itself.
* `dm/gfx/tools/ios_metal.py` and `dmsh_build.py` build the command lists
  `['xcrun', '-sdk', 'iphoneos', 'metal', ...]` and run them.
* `dm/gfx/tools/dmsh_chain.py` runs the committed `dmsh_xlate` apphost, which is a **Mach-O**
  executable and cannot start on Windows; the DLLs beside it are the same build, so
  `dotnet dmsh_xlate.dll` is equivalent and produces the same `--source-sha` output the lock
  file records.
* the pipeline spells the interpreter `python3` in places, which Windows does not have.

The clean way to cover all of that without editing upstream files is `sitecustomize.py`:
put this folder on `PYTHONPATH` and CPython imports it in **every** interpreter before the
program's own imports. It then rewrites `argv` at the `subprocess.Popen` boundary only.

`chrome/` holds the `xcrun.exe`-style stand-ins for the rare case where a tool is spawned
directly by name from a non-Python parent (the NSP script's own `shutil.which('xcodebuild')`
check, for instance). They are plain `.cmd` files so no compiler is needed to build them.
"""
from __future__ import annotations

import os
import shutil
import subprocess
import sys
from pathlib import Path

from . import toolchain as tc

SITECUSTOMIZE = 'sitecustomize.py'


# ---------------------------------------------------------------- the sitecustomize body

_SITECUSTOMIZE_SRC = r'''# Generated by nspb_qt.win.shim; imported by every Python the build starts.
# It rewrites macOS tool names at the subprocess boundary, leaving upstream sources untouched.
import os, sys

if os.name == 'nt' and not os.environ.get('NSPB_SHIM_OFF'):
    import shutil, subprocess

    # The shim lives in the app's data folder, but the modules it calls live in the package.
    # NSPB_WIN_DIR is written in when this file is generated; the fallback keeps a hand-copied
    # sitecustomize working when the variable is absent.
    _HERE = os.path.dirname(os.path.abspath(__file__))
    _PKG = os.environ.get('NSPB_WIN_DIR') or _HERE
    _XCRUN = os.path.join(_PKG, 'xcrun_shim.py')
    _BUILD_APP = os.path.join(_PKG, 'build_app.py')

    # the package root goes on sys.path so `import toolchain` / `import xcrun_shim` resolve, and
    # so `import nspb_qt.win` works from a child that wants the whole package
    for _p in (_PKG, os.path.dirname(_PKG), os.path.dirname(os.path.dirname(_PKG))):
        if _p and _p not in sys.path:
            sys.path.insert(0, _p)

    # ---- POSIX signal, os.execve & multi-core fork Pool adapter for patch_nso.py / nro_collect.py
    # patch_nso.py's in_parallel() uses multiprocessing.get_context('fork').Pool(WORKERS) +
    # signal.pthread_sigmask; nro_collect.py calls multiprocessing.Pool() at top level without
    # an `if __name__ == '__main__':` guard. On Windows there is no fork(), and Capstone's C
    # disassembler (`cs_disasm`) releases the GIL, so a thread pool with per-thread Capstone
    # instances gives full multi-core speedup without fork() or spawn recursion.
    import signal as _signal
    for _sig_attr, _sig_val in (('SIG_BLOCK', 1), ('SIG_UNBLOCK', 2), ('SIG_SETMASK', 3)):
        if not hasattr(_signal, _sig_attr):
            setattr(_signal, _sig_attr, _sig_val)
    if not hasattr(_signal, 'pthread_sigmask'):
        _signal.pthread_sigmask = lambda how, mask: set()

    def _win_execve(path, argv, env):
        r = subprocess.run([os.fspath(path)] + [os.fspath(a) for a in list(argv)[1:]], env=env)
        os._exit(r.returncode)

    os.execve = _win_execve

    import concurrent.futures as _cf, marshal as _marshal, multiprocessing as _mp, multiprocessing.spawn as _mp_spawn, pickle as _pickle, tempfile as _tempfile, threading as _mp_threading, types as _mp_types
    _MAX_WIN_WORKERS = 61
    _OrigProcessPoolExecutor = _cf.ProcessPoolExecutor

    class _WinProcessPoolExecutor(_OrigProcessPoolExecutor):
        def __init__(self, max_workers=None, *a, **k):
            if isinstance(max_workers, int) and max_workers > _MAX_WIN_WORKERS:
                max_workers = _MAX_WIN_WORKERS
            super().__init__(max_workers, *a, **k)

    _cf.ProcessPoolExecutor = _WinProcessPoolExecutor
    _mp_tls = _mp_threading.local()
    _orig_get_context = _mp.get_context
    _orig_get_prep_data = _mp_spawn.get_preparation_data
    _in_fork_pool_spawn = False

    def _safe_get_prep_data(name):
        d = _orig_get_prep_data(name)
        if _in_fork_pool_spawn:
            d.pop('init_main_from_path', None)
            d.pop('init_main_from_name', None)
        return d

    _mp_spawn.get_preparation_data = _safe_get_prep_data

    class _WinForkPool:
        def __init__(self, processes=None, initializer=None, initargs=(), *a, **k):
            self._workers = max(1, int(processes or os.cpu_count() or 4))
            self._ex = _cf.ThreadPoolExecutor(max_workers=self._workers)

        def __enter__(self):
            return self

        def __exit__(self, *exc):
            self._ex.shutdown(wait=True)
            return False

        def close(self):
            pass

        def join(self):
            self._ex.shutdown(wait=True)

        def terminate(self):
            self._ex.shutdown(wait=False, cancel_futures=True)

        def map(self, fn, iterable, chunksize=1):
            return list(self._ex.map(fn, iterable))

        def imap_unordered(self, fn, iterable, chunksize=1):
            return self._ex.map(fn, iterable)

        def starmap(self, fn, jobs, chunksize=1):
            global _in_fork_pool_spawn
            jobs = list(jobs)
            g = getattr(fn, '__globals__', None)
            fname = getattr(fn, '__name__', '')
            if fname == 'x18_words' and isinstance(g, dict) and 'Cs' in g:
                IMG = g['IMG']
                if len(jobs) >= 2 and len(IMG) >= 0x20000:
                    img_path = None
                    try:
                        import mp_worker as _mpw
                        with _tempfile.NamedTemporaryFile(prefix='nspb_x18_', delete=False) as tf:
                            tf.write(IMG)
                            img_path = tf.name
                        n_procs = min(_MAX_WIN_WORKERS, len(jobs), max(self._workers, os.cpu_count() or 4))
                        _in_fork_pool_spawn = True
                        try:
                            pool = _orig_get_context('spawn').Pool(
                                processes=n_procs,
                                initializer=_mpw.init_x18,
                                initargs=(img_path,),
                            )
                        finally:
                            _in_fork_pool_spawn = False
                        try:
                            res = pool.map(_mpw.run_x18_job, jobs)
                            pool.close()
                            pool.join()
                            return res
                        finally:
                            try:
                                pool.terminate()
                            except Exception:
                                pass
                    except Exception:
                        pass
                    finally:
                        if img_path:
                            try:
                                os.unlink(img_path)
                            except OSError:
                                pass
                Cs, ARCH, MODE = g['Cs'], g['CS_ARCH_ARM64'], g['CS_MODE_ARM']
                R18 = g['R18']

                def _x18_job(job):
                    lo, hi = job
                    md = getattr(_mp_tls, 'md_lite', None)
                    if md is None:
                        md = _mp_tls.md_lite = Cs(ARCH, MODE)
                    out, a = set(), lo
                    while a < hi:
                        end = min(hi, a + 0x4000); last = a - 4
                        for addr, _sz, _mn, op in md.disasm_lite(bytes(IMG[a:end]), a):
                            last = addr
                            if R18.search(op):
                                out.add(addr)
                        a = last + 4 if last + 4 >= end else last + 8
                    return out

                return list(self._ex.map(_x18_job, jobs))
            if fname == 'spin_heads_with_region' and isinstance(g, dict) and 'Cs' in g:
                IMG = g['IMG']
                if len(jobs) >= 2 and len(g.get('spin_heads', ())) >= 8:
                    img_path = st_path = None
                    try:
                        import mp_worker as _mpw
                        fnames = (
                            'spin_norm', 'spin_rw', 'spin_kind', 'spin_rmw', 'spin_lock',
                            'spin_store', 'spin_quick', 'spin_region1', 'spin_region',
                            'spin_heads_with_region',
                        )
                        codes_blob = _marshal.dumps(
                            {k: (g[k].__code__, g[k].__defaults__) for k in fnames if k in g and isinstance(g[k], _mp_types.FunctionType)}
                        )
                        state_keys = (
                            'SPIN_PATHS', 'SPIN_STEPS', 'SPIN_STORES', 'SPIN_LSE', 'SPIN_BAD',
                            'SPIN_STOP', 'DATA_BLOCKS', 'spin_touched', 'spin_cand', 'spin_heads',
                        )
                        state = {k: g[k] for k in state_keys if k in g}
                        with _tempfile.NamedTemporaryFile(prefix='nspb_spin_img_', delete=False) as tf_i, \
                                _tempfile.NamedTemporaryFile(prefix='nspb_spin_st_', delete=False) as tf_s:
                            tf_i.write(IMG)
                            img_path = tf_i.name
                            _pickle.dump((codes_blob, state), tf_s, protocol=_pickle.HIGHEST_PROTOCOL)
                            st_path = tf_s.name
                        n_procs = min(_MAX_WIN_WORKERS, len(jobs), max(self._workers, os.cpu_count() or 4))
                        _in_fork_pool_spawn = True
                        try:
                            pool = _orig_get_context('spawn').Pool(
                                processes=n_procs,
                                initializer=_mpw.init_spin,
                                initargs=(img_path, st_path),
                            )
                        finally:
                            _in_fork_pool_spawn = False
                        try:
                            res = pool.map(_mpw.run_spin_job, jobs)
                            pool.close()
                            pool.join()
                            return res
                        finally:
                            try:
                                pool.terminate()
                            except Exception:
                                pass
                    except Exception:
                        pass
                    finally:
                        for _p in (img_path, st_path):
                            if _p:
                                try:
                                    os.unlink(_p)
                                except OSError:
                                    pass
                Cs, ARCH, MODE = g['Cs'], g['CS_ARCH_ARM64'], g['CS_MODE_ARM']

                def _spin_job(job):
                    t_fn = getattr(_mp_tls, 'spin_fn', None)
                    if t_fn is None or getattr(_mp_tls, 'spin_src', None) is not fn:
                        md_d = Cs(ARCH, MODE)
                        md_d.detail = True
                        dec = {}

                        def _tls_spin_ins(a, _dec=dec, _md=md_d, _img=IMG):
                            if a not in _dec:
                                _dec[a] = next(_md.disasm(bytes(_img[a:a + 4]), a), None)
                            return _dec[a]

                        tg = dict(g)
                        tg['md'] = md_d
                        tg['spin_dec'] = dec
                        tg['spin_ins'] = _tls_spin_ins
                        for _k in ('spin_rw', 'spin_kind', 'spin_rmw', 'spin_lock', 'spin_store',
                                   'spin_region1', 'spin_region', 'spin_heads_with_region'):
                            if _k in g and isinstance(g[_k], _mp_types.FunctionType):
                                tg[_k] = _mp_types.FunctionType(g[_k].__code__, tg, _k,
                                                                g[_k].__defaults__, g[_k].__closure__)
                        t_fn = _mp_tls.spin_fn = tg['spin_heads_with_region']
                        _mp_tls.spin_src = fn
                    return t_fn(*job)

                return list(self._ex.map(_spin_job, jobs))
            return list(self._ex.map(lambda j: fn(*j), jobs))

    class _WinForkContext:
        Pool = staticmethod(_WinForkPool)

    def _win_get_context(method=None):
        if method == 'fork':
            return _WinForkContext()
        return _orig_get_context(method)

    _mp.get_context = _win_get_context
    _mp.Pool = _WinForkPool

    # ---- text files written the way the pipeline expects them
    # The host compares lines of the pipeline's tables byte for byte; an inputs.tsv line ending
    # in "\r" did not match on the device (found by the reference implementation the hard way).
    import builtins, io
    _open = io.open

    def _lf_open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None,
                 closefd=True, opener=None):
        if newline is None and 'b' not in mode and any(c in mode for c in 'wax+'):
            newline = '\n'
        return _open(file, mode, buffering, encoding, errors, newline, closefd, opener)

    io.open = builtins.open = _lf_open

    # ---- os.open binary mode & os.pread (POSIX-only on CPython for Windows)
    _os_open = os.open

    def _win_os_open(path, flags, mode=0o777, *, dir_fd=None):
        if not (flags & getattr(os, 'O_TEXT', 0)):
            flags |= getattr(os, 'O_BINARY', 0)
        if dir_fd is not None:
            return _os_open(path, flags, mode, dir_fd=dir_fd)
        return _os_open(path, flags, mode)

    os.open = _win_os_open

    if not hasattr(os, 'pread'):
        import threading as _threading
        _pread_lock = _threading.Lock()

        def _win_pread(fd, n, offset):
            with _pread_lock:
                cur = os.lseek(fd, 0, os.SEEK_CUR)
                try:
                    os.lseek(fd, offset, os.SEEK_SET)
                    out = bytearray()
                    while len(out) < n:
                        chunk = os.read(fd, n - len(out))
                        if not chunk:
                            break
                        out.extend(chunk)
                    return bytes(out)
                finally:
                    try:
                        os.lseek(fd, cur, os.SEEK_SET)
                    except OSError:
                        pass

        os.pread = _win_pread

    # ---- os.symlink fallback when Developer Mode is off (WinError 1314)
    _os_symlink = os.symlink

    def _win_symlink(src, dst, target_is_directory=False, *, dir_fd=None):
        try:
            if dir_fd is not None:
                return _os_symlink(src, dst, target_is_directory=target_is_directory, dir_fd=dir_fd)
            return _os_symlink(src, dst, target_is_directory=target_is_directory)
        except OSError:
            real_src = src
            if not os.path.isabs(src):
                real_src = os.path.normpath(os.path.join(os.path.dirname(os.fspath(dst)), src))
            if os.path.isdir(real_src):
                shutil.copytree(real_src, dst, dirs_exist_ok=True)
            else:
                shutil.copy2(real_src, dst)

    os.symlink = _win_symlink

    # ---- POSIX slashes in os.path.relpath and romfs.py
    # Upstream's shader_split.py, gl_shaders.py, vk_shaders.py, uepak.py and nro_collect.py
    # sort and record RomFS-relative paths with '/' (and compare them against shader_map.tsv).
    _relpath = os.path.relpath

    def _posix_relpath(path, start=None):
        r = _relpath(path, start) if start is not None else _relpath(path)
        return r.replace('\\', '/') if isinstance(r, str) else r.replace(b'\\', b'/')

    os.path.relpath = _posix_relpath

    def _patch_romfs_module(mod):
        if getattr(mod, '_nspb_patched', False):
            return
        mod._nspb_patched = True
        _orig_find = mod.Image.find
        mod.Image.find = lambda self, rel: _orig_find(self, rel.replace('\\', '/'))

        def _resolve(path):
            p = os.path.abspath(os.fsdecode(path)).replace('\\', '/')
            parts = p.split('/')
            for i, part in enumerate(parts):
                if not part.endswith('.img'):
                    continue
                image = '/'.join(parts[:i + 1])
                try:
                    st = os.stat(image)
                except OSError:
                    continue
                if not mod._stat.S_ISREG(st.st_mode):
                    continue
                loaded = mod._image(image, (st.st_ino, st.st_size, st.st_mtime_ns, st.st_ctime_ns))
                if loaded is not None:
                    return loaded, '/'.join(parts[i + 1:])
            return None

        mod._resolve = _resolve
        _orig_walk = mod.walk

        def _walk(top, *a, **k):
            for root, dirs, files in _orig_walk(top, *a, **k):
                yield (root.replace('\\', '/') if isinstance(root, str) else root), dirs, files

        mod.walk = _walk
        mod.os.walk = _walk
        mod._OS.sep = '/'
        mod._PathOps.sep = '/'
        _orig_join = os.path.join
        _orig_normpath = os.path.normpath

        def _posix_join(*a):
            res = _orig_join(*a)
            return res.replace('\\', '/') if isinstance(res, str) else res

        def _posix_normpath(path):
            res = _orig_normpath(path)
            return res.replace('\\', '/') if isinstance(res, str) else res.replace(b'\\', b'/')

        mod._PathOps.join = staticmethod(_posix_join)
        mod._PathOps.relpath = staticmethod(_posix_relpath)
        mod._PathOps.normpath = staticmethod(_posix_normpath)

    def _patch_sdk_patches_module(mod):
        jf = os.environ.get('NSPB_AUTO_SDK_CUTS')
        if not jf or not hasattr(mod, 'CUTS') or not os.path.isfile(jf):
            return
        try:
            import json
            with open(jf, 'r', encoding='utf-8') as f:
                data = json.load(f)
            for bid, cuts in data.items():
                mod.CUTS.setdefault(bid, [tuple(c) for c in cuts])
        except Exception:
            pass

    def _patch_tool_lock_module(mod):
        if getattr(mod, '_nspb_patched', False) or not hasattr(mod, 'check_tools'):
            return
        mod._nspb_patched = True
        _orig_check = mod.check_tools

        def _win_check_tools():
            bad = _orig_check()
            if os.environ.get('DM_ALLOW_SYSTEM_SPIRV_CROSS') == '1':
                bad = [b for b in bad
                       if not (b.startswith('spirv-cross: ') and not b.endswith(' / actual missing'))]
            if os.environ.get('DM_ALLOW_OTHER_METAL') == '1':
                bad = [b for b in bad if not b.startswith('metal_macosx: ')]
            return bad

        mod.check_tools = _win_check_tools

    def _patch_tree_tar_module(mod):
        if getattr(mod, '_nspb_patched', False):
            return
        mod._nspb_patched = True
        # Windows cannot os.open(directory, O_RDONLY) for flock(); a single build runs per work dir.
        mod._lock = lambda folder, say=None: True
        mod._unlock = lambda folder: None

    def _ascii_temp_base():
        if _PKG not in sys.path:
            sys.path.insert(0, _PKG)
        import toolchain as _tc
        with _tc.ascii_temp_dir('nspb-probe-') as _p:
            return os.path.dirname(str(_p))

    def _patch_dmsh_pch_module(mod):
        if getattr(mod, '_nspb_patched', False) or not hasattr(mod, 'tempfile'):
            return
        mod._nspb_patched = True
        _orig_tf = mod.tempfile

        class _PchTempfile:
            def __getattr__(self, k):
                return getattr(_orig_tf, k)

            def mkdtemp(self, suffix=None, prefix=None, dir=None):
                if dir and (' ' in str(dir) or not str(dir).isascii()):
                    dir = _ascii_temp_base()
                return _orig_tf.mkdtemp(suffix=suffix, prefix=prefix, dir=dir)

        mod.tempfile = _PchTempfile()

    def _patch_dmsh_mtlcache_module(mod):
        if getattr(mod, '_nspb_patched', False) or not hasattr(mod, 'Cache'):
            return
        mod._nspb_patched = True
        _orig_init = mod.Cache.__init__

        def _win_cache_init(self, store, targets, work):
            if work and (' ' in str(work) or not str(work).isascii()):
                import tempfile as _tf, atexit as _atexit
                work = _tf.mkdtemp(prefix='mtlwork_', dir=_ascii_temp_base())
                _atexit.register(shutil.rmtree, work, ignore_errors=True)
            _orig_init(self, store, targets, work)

        mod.Cache.__init__ = _win_cache_init

    import importlib.machinery as _machinery
    _exec_module = _machinery.SourceFileLoader.exec_module

    def _win_exec_module(self, module):
        _exec_module(self, module)
        name = getattr(module, '__name__', '')
        if name == 'romfs' or name.endswith('.romfs'):
            _patch_romfs_module(module)
        elif name == 'sdk_patches' or name.endswith('.sdk_patches'):
            _patch_sdk_patches_module(module)
        elif name == 'tool_lock' or name.endswith('.tool_lock'):
            _patch_tool_lock_module(module)
        elif name == 'tree_tar' or name.endswith('.tree_tar'):
            _patch_tree_tar_module(module)
        elif name == 'pack_dump' or name.endswith('.pack_dump'):
            module.clone = shutil.copy2
        elif name == 'dmsh_pch' or name.endswith('.dmsh_pch'):
            _patch_dmsh_pch_module(module)
        elif name == 'dmsh_mtlcache' or name.endswith('.dmsh_mtlcache'):
            _patch_dmsh_mtlcache_module(module)

    _machinery.SourceFileLoader.exec_module = _win_exec_module

    # ---- os.replace as on a Mac
    # Windows refuses to replace a file another thread has open. The shader caches are
    # content-addressed, so two workers finishing the same entry is a legitimate race that
    # POSIX rename allows and Windows does not; retry, then accept an identical file.
    import time as _time
    _replace = os.replace

    def _win_replace(src, dst, *a, **k):
        for i in range(20):
            try:
                return _replace(src, dst, *a, **k)
            except PermissionError:
                if i == 19:
                    try:
                        if (os.path.isfile(dst) and os.path.getsize(dst) == os.path.getsize(src)):
                            os.remove(src)
                            return None
                    except OSError:
                        pass
                    raise
                _time.sleep(0.05 * (i + 1))

    os.replace = _win_replace

    # ---- ctypes.CDLL(None) / libSystem
    # can_clone() asks the process itself for clonefile; uepak.py decrypts through libSystem's
    # CommonCrypto. Neither exists here. An empty library makes the clone probe fall back to
    # plain copies, and CommonCrypto's CCCrypt is backed by Windows' own AES (CNG) so Unreal
    # pak indexes still decode.
    class _NoLib:
        def __getattr__(self, name):
            raise AttributeError(f'{name} (no process-wide C library on Windows)')

    class _CCCrypt:
        restype = argtypes = None

        def __call__(self, op, alg, options, key, keylen, iv, data, datalen, out, outlen, moved):
            if alg != 0 or options not in (0, 2):
                return -4301        # kCCUnimplemented: only AES (0) in ECB mode (2), unpadded
            from ctypes import wintypes, byref, c_void_p, c_ulong, create_unicode_buffer
            bc = ctypes.WinDLL('bcrypt')
            h = c_void_p(); hk = c_void_p(); res = c_ulong()
            if bc.BCryptOpenAlgorithmProvider(byref(h), ctypes.c_wchar_p('AES'), None, 0):
                return -4300
            try:
                mode = create_unicode_buffer('ChainingModeECB')
                bc.BCryptSetProperty(h, ctypes.c_wchar_p('ChainingMode'), mode,
                                     ctypes.sizeof(mode), 0)
                k = bytes(key)[:keylen]
                if bc.BCryptGenerateSymmetricKey(h, byref(hk), None, 0, k, len(k), 0):
                    return -4300
                src = bytes(data)[:datalen]
                fn = bc.BCryptDecrypt if op == 1 else bc.BCryptEncrypt
                if fn(hk, src, len(src), None, None, 0, out, outlen, byref(res), 0):
                    return -4300
                moved._obj.value = res.value if hasattr(moved, '_obj') else res.value
                return 0
            finally:
                if hk:
                    bc.BCryptDestroyKey(hk)
                bc.BCryptCloseAlgorithmProvider(h, 0)

    class _LibSystem:
        CCCrypt = _CCCrypt()

    import ctypes
    _CDLL = ctypes.CDLL

    def _cdll(name, *a, **k):
        if name is None:
            return _NoLib()
        if isinstance(name, str) and name.endswith('libSystem.B.dylib'):
            return _LibSystem()
        return _CDLL(name, *a, **k)

    ctypes.CDLL = _cdll

    # ---- the command map
    _CACHE = {}

    def _resolve(rest):
        """xcrun -sdk <sdk> metal / metallib straight to the Windows tool, without a Python
        process per call: a shader build makes thousands of them."""
        if len(rest) < 3 or rest[0] not in ('-sdk', '--sdk') or rest[2] not in ('metal', 'metallib'):
            return None
        if rest[2] == 'metal' and '-c' not in rest[3:] and '-o' in rest[3:]:
            return None  # compile+link in one call (host_msl.py): handled by xcrun_shim.py
        if any(' ' in str(a) or not str(a).isascii() for a in rest[3:]):
            return None  # paths with spaces or non-ASCII chars break metal.exe; xcrun_shim.py stages them via cwd
        if 'modules' not in _CACHE:
            if _PKG not in sys.path:
                sys.path.insert(0, _PKG)
            import xcrun_shim, toolchain
            _CACHE['modules'] = (xcrun_shim, toolchain)
        x, t = _CACHE['modules']
        sdk, tool = rest[1], rest[2]
        key = (sdk, tool)
        if key not in _CACHE:
            _CACHE[key] = t.metal_tool(tool, sdk)
        exe = _CACHE[key]
        if not exe:
            return None
        return [str(exe)] + (x.metal_args(sdk, rest[3:]) if tool == 'metal' else rest[3:])

    def _map(args):
        if isinstance(args, (str, bytes)) or not args:
            return args
        args = [os.fspath(a) if isinstance(a, os.PathLike) else a for a in args]
        if not isinstance(args[0], str):
            return args
        head = args[0].replace('\\', '/')
        base = head.rsplit('/', 1)[-1].lower()
        if base in ('xcrun', 'xcrun.exe'):
            return _resolve(args[1:]) or [sys.executable, _XCRUN] + args[1:]
        if base == 'dmsh_xlate' and '/dmsh_xlate_bin/' in head:
            # an absolute path, not `which`: a .NET SDK the app installed is not on PATH
            import toolchain as tc
            dn = tc.dotnet_dir()
            dotnet = str(dn / 'dotnet.exe') if dn else (shutil.which('dotnet') or 'dotnet')
            return [dotnet, args[0] + '.dll'] + args[1:]
        if base in ('python3', 'python3.exe') or base in ('python', 'python.exe'):
            py_exe = os.environ.get('NSPB_PYTHON') or sys.executable
            return [py_exe] + [a for a in args[1:] if a != '-I']
        if base in ('spirv-cross', 'spirv-cross.exe'):
            sc = os.environ.get('NSPB_SPIRV_CROSS') or os.environ.get('SPIRV_CROSS')
            if sc and os.path.isfile(sc):
                return [sc] + args[1:]
        if base in ('bash', 'bash.exe') and len(args) > 1 and \
                args[1].replace('\\', '/').endswith('dm/ios/tools/build_ipa.sh'):
            return [sys.executable, _BUILD_APP] + args[2:]
        if base in ('which', 'which.exe') and len(args) == 2:
            return [sys.executable, '-c',
                    'import shutil,sys; p=shutil.which(sys.argv[1]); print(p or ""); '
                    'sys.exit(0 if p else 1)', args[1]]
        return args

    # A macOS-only Metal compile (the Mac shader libraries) is never packed into the iOS app;
    # what the iOS build reads from those outputs is the MSL text and the JSON beside it. The
    # reference implementation writes an empty file instead of starting a process, which halves
    # the shader stage. NSPB_MAC_METAL=1 compiles them for real.
    _run = subprocess.run

    def _run_fast(args, *a, **k):
        if isinstance(args, (list, tuple)) and args and isinstance(args[0], str):
            _head = args[0].replace('\\', '/').rsplit('/', 1)[-1].lower()
            if (_head in ('cp', 'cp.exe') and len(args) == 4 and args[1] == '-c') or _head in ('xattr', 'xattr.exe'):
                return subprocess.CompletedProcess(args, 1,
                                                   b'' if not k.get('text') else '',
                                                   b'' if not k.get('text') else '')
        if (not os.environ.get('NSPB_MAC_METAL') and isinstance(args, (list, tuple))
                and len(args) > 4 and isinstance(args[0], str)
                and args[0].replace('\\', '/').rsplit('/', 1)[-1].lower() in ('xcrun', 'xcrun.exe')
                and args[1] in ('-sdk', '--sdk') and str(args[2]).startswith('macosx')
                and args[3] in ('metal', 'metallib') and '-o' in args):
            out = os.fspath(args[list(args).index('-o') + 1])
            with open(out, 'wb'):
                pass
            return subprocess.CompletedProcess(args, 0,
                                               b'' if not k.get('text') else '',
                                               b'' if not k.get('text') else '')
        return _run(args, *a, **k)

    subprocess.run = _run_fast

    _init = subprocess.Popen.__init__

    def _popen_init(self, args, *a, **k):
        if os.environ.get('NSPB_SHIM_TRACE'):
            print('nspb shim:', args, '->', _map(args), file=sys.stderr)
        _init(self, _map(args), *a, **k)

    subprocess.Popen.__init__ = _popen_init

    # ---- pwd
    # scripts/build_ipa_from_nsp.py imports pwd at module level (a POSIX-only module), so the
    # upstream file cannot even be loaded on Windows without this stand-in. It is only used to
    # recover HOME from the password database when the environment has none.
    if 'pwd' not in sys.modules:
        import types
        _pwd = types.ModuleType('pwd')

        class struct_passwd(tuple):
            __slots__ = ()

            @property
            def pw_dir(self):
                return self[5]

        def getpwuid(uid):
            home = os.environ.get('USERPROFILE') or os.path.expanduser('~')
            name = os.environ.get('USERNAME') or 'user'
            return struct_passwd((name, 'x', uid if uid is not None else 0, 0, '', home, ''))

        _pwd.struct_passwd = struct_passwd
        _pwd.getpwuid = getpwuid
        _pwd.getpwnam = lambda name: getpwuid(0)
        sys.modules['pwd'] = _pwd

    if not hasattr(os, 'getuid'):
        os.getuid = lambda: 0

    # ---- fcntl
    # dm/tools/tree_tar.py (imported at top level by scripts/build_ipa_from_nsp.py) imports fcntl
    # for advisory directory locking. Register a Windows stand-in so the import succeeds.
    if 'fcntl' not in sys.modules:
        import types as _types
        _fcntl = _types.ModuleType('fcntl')
        _fcntl.LOCK_SH = 1
        _fcntl.LOCK_EX = 2
        _fcntl.LOCK_NB = 4
        _fcntl.LOCK_UN = 8
        _fcntl.F_GETFD = 1
        _fcntl.F_SETFD = 2
        _fcntl.F_GETFL = 3
        _fcntl.F_SETFL = 4
        _fcntl.FD_CLOEXEC = 1
        _fcntl.flock = lambda fd, op: None
        _fcntl.lockf = lambda fd, cmd, len=0, start=0, whence=0: None
        _fcntl.fcntl = lambda fd, cmd, arg=0: 0
        _fcntl.ioctl = lambda fd, req, arg=0, mutate_flag=True: 0
        sys.modules['fcntl'] = _fcntl
'''


def _write_if_changed(path: Path, text: str) -> None:
    data = text.encode('utf-8')
    if not path.is_file() or path.read_bytes() != data:
        path.write_bytes(data)


def install(shim_dir: Path, python_exe: str | None = None) -> None:
    """Write `sitecustomize.py` and the `.cmd` stand-ins into `shim_dir`.

    Idempotent: the files are rewritten only when their bytes differ, so an incremental build
    does not invalidate anything keyed on their mtime.

    The generated `sitecustomize.py` needs to know where this package lives, because it is
    written into a separate folder (the app's data dir) while the modules it calls stay in the
    package. That path is baked in as a literal, so the file does not depend on the environment
    of whichever process imports it.
    """
    shim_dir.mkdir(parents=True, exist_ok=True)
    pkg = str(Path(__file__).resolve().parent)
    body = _SITECUSTOMIZE_SRC.replace(
        "    _PKG = os.environ.get('NSPB_WIN_DIR') or _HERE\n",
        '    _PKG = os.environ.get(\'NSPB_WIN_DIR\') or ' + repr(pkg) + '\n')
    _write_if_changed(shim_dir / SITECUSTOMIZE, body)
    py = python_exe or sys.executable
    here = Path(pkg)
    for name, target in (('xcrun', here / 'xcrun_shim.py'),
                         ('xcodebuild', here / 'xcodebuild_shim.py'),
                         ('xcodegen', here / 'xcodegen_shim.py')):
        body_cmd = (f'@echo off\r\n'
                    f'"{py}" "{target}" %*\r\n')
        _write_if_changed(shim_dir / f'{name}.cmd', body_cmd)
    # a `python3.cmd` so the pipeline's `python3` spelling works in a shell too
    body_py = f'@echo off\r\n"{py}" %*\r\n'
    _write_if_changed(shim_dir / 'python3.cmd', body_py)


def shim_dir() -> Path:
    """Where the stand-ins live: inside the app's data folder, so no repository file changes."""
    base = Path(os.environ.get('LOCALAPPDATA', Path.home() / 'AppData' / 'Local'))
    return base / 'NS Port Builder' / 'shim'


def build_env(base: dict[str, str] | None = None, python_exe: str | None = None) -> dict[str, str]:
    """The environment a Windows build runs with.

    `PYTHONPATH` puts the shim first so every child Python loads it; `PATH` puts the `.cmd`
    stand-ins first so a tool spawned by name resolves to them; the `NSPB_*` variables tell
    `toolchain.py` where the LLVM / SDK / Metal folders are, which is how the user's
    `toolchain.json` reaches a process that never reads the app's config.
    """
    env = dict(base if base is not None else os.environ)
    sd = shim_dir()
    install(sd, python_exe)

    parts = [str(sd)]
    llvm = tc.llvm_dir()
    if llvm:
        parts.append(str(llvm / 'bin'))
        env['NSPB_LLVM_DIR'] = str(llvm)
        lld = tc.ld64_lld() or (llvm / 'bin' / 'ld64.lld.exe')
        if Path(lld).is_file():
            parts.append(str(Path(lld).parent))
    for name in ('spirv-cross', 'zstd', 'glslangValidator'):
        p = tc.find_exe(name)
        if p:
            parts.append(str(p.parent))
            if name == 'spirv-cross':
                env.setdefault('SPIRV_CROSS', str(p))
                env['NSPB_SPIRV_CROSS'] = str(p)
    if python_exe:
        env['NSPB_PYTHON'] = str(python_exe)
    env.setdefault('DM_ALLOW_SYSTEM_SPIRV_CROSS', '1')
    env.setdefault('DM_ALLOW_OTHER_METAL', '1')
    # tc.dotnet_dir() first: it knows about the SDK the app installed for itself, which is not on
    # PATH and not in Program Files. `which` alone missed it, so `dotnet build` and the shader
    # translator failed on a machine that had just installed .NET through this app.
    if dn := tc.dotnet_dir():
        parts.append(str(dn))
        env['DOTNET_ROOT'] = env['DOTNET_ROOT_X64'] = str(dn)
    elif dn := shutil.which('dotnet'):
        parts.append(str(Path(dn).parent))

    pkg_root = str(Path(__file__).resolve().parents[2])
    env['PATH'] = os.pathsep.join(dict.fromkeys(parts + [env.get('PATH', '')]))
    env['PYTHONPATH'] = os.pathsep.join(
        dict.fromkeys([str(sd), pkg_root]
                      + [p for p in env.get('PYTHONPATH', '').split(os.pathsep) if p]))
    env.setdefault('PYTHONUTF8', '1')     # the pipeline reads and writes UTF-8 without naming it
    env.setdefault('PYTHONIOENCODING', 'utf-8')

    sdk = tc.ios_sdk()
    if sdk:
        env['NSPB_IOS_SDK'] = str(sdk)
        env['IPHONEOS_SDK_DIR'] = str(sdk)     # what platform_util.ios_sdk_ok already checks
    if m := tc.metal_dir():
        env['NSPB_METAL_DIR'] = str(m)
    exe = tc.metal_tool('metal')
    if exe:
        env['METAL_EXE'] = str(exe)
    with_lib = tc.metal_tool('metallib')
    if with_lib:
        env['METALLIB_EXE'] = str(with_lib)

    # the shader build spawns one metal.exe per shader and each start costs ~0.1 s on Windows,
    # so keep every core busy rather than the upstream default of 8 (capped at 61, CPython's
    # ProcessPoolExecutor _MAX_WINDOWS_WORKERS limit for WaitForMultipleObjects)
    env.setdefault('J', str(min(61, max(8, 2 * (os.cpu_count() or 4)))))
    return env


def verify_shim(env: dict[str, str]) -> tuple[bool, str]:
    """Prove the shim loads in a child interpreter before a build relies on it."""
    try:
        r = subprocess.run([env.get('NSPB_PYTHON') or sys.executable, '-c',
                            'import sys; sys.exit(0 if "sitecustomize" in sys.modules else 3)'],
                           capture_output=True, text=True, env=env, timeout=60, **tc.no_window())
    except (OSError, subprocess.SubprocessError) as e:
        return False, f'cannot run a probe interpreter: {e}'
    if r.returncode == 0:
        return True, 'shim loads in child interpreters'
    return False, ('the shim did not load in a child interpreter (sitecustomize is shadowed by '
                   'another one on PYTHONPATH?)')