#!/usr/bin/env python3 # 在没有苹果 Metal 编译器的机器上跑着色器出库链(Windows / Linux / 没装 Xcode 的 Mac;给 tools/d2rm_pack.py 用)。 # 用法:nometal_run.py <脚本.py> [参数…] 例:nometal_run.py dmsh_build.py <包目录> <缓存目录> # 做法:不改出库链的任何一个文件(它们的内容参与翻译链版本,一改内测的着色器库就得整体换代),只在本进程里把它们调外部命令的 # 那一个入口(subprocess.run)接过来,认出下面几条命令自己答,其余原样放行: # xcrun -sdk metal --version → 答「none」(工具指纹里编译器记成不用 → 链版本自成一支,缓存与正常模式互不相混) # xcrun -sdk metal -c <源码> -o <中间件> → 把源码拷成中间件(换行统一成 \n) # xcrun -sdk metallib <中间件> -o <库> → 把中间件原样拷成库 # which <名字> → 用 Python 自己找 # cp -c <甲> <乙> → 普通拷贝 # 结果:「库文件」(*_b.metallib / *_bf.metallib)里放的是着色器源码。壳(DM_SHELL 构建)的图形层发现库不是编好的就在设备上现编 # (gfx/nvn/nvn_mtl.m),系统会把编好的结果缓存,第二次启动起不再编。 # 少掉的一道:正常模式下苹果编译器顺带验了每份源码能编过;这里不验,编不过的要到设备上用到它时才报红。 # Runs the shader build chain on a machine without Apple's Metal compiler (Windows / Linux / a Mac without Xcode; for use by tools/d2rm_pack.py). # Usage: nometal_run.py [args...] Example: nometal_run.py dmsh_build.py # How: not a single file of the build chain is modified (their contents feed into the translation chain version, and any change would force a whole new generation of the beta shader libraries); only, within this process, the one entry through which they call external commands # (subprocess.run) is taken over: the commands below are recognized and answered here, and everything else is passed through unchanged: # xcrun -sdk metal --version → answers "none" (the compiler is recorded as unused in the tool fingerprint → the chain version forms its own branch, and its cache never mixes with normal mode's) # xcrun -sdk metal -c -o → copies the source as the intermediate file (line endings normalized to LF) # xcrun -sdk metallib -o → copies the intermediate file verbatim as the library # which → looked up by Python itself # cp -c → ordinary copy # Result: the "library files" (*_b.metallib / *_bf.metallib) contain shader source. When the graphics layer of the shell (DM_SHELL build) finds that a library is not a compiled one, it compiles it on the device # (gfx/nvn/nvn_mtl.m); the system caches the compiled result, and from the second launch on nothing is compiled again. # The one check that is lost: in normal mode Apple's compiler also verifies along the way that every source compiles; here that is not verified, and one that fails to compile only reports a failure when it is used on the device. import os, runpy, shutil, subprocess, sys _real = subprocess.run def _done(args, out=''): return subprocess.CompletedProcess(args, 0, stdout=out, stderr='') def run(args, *a, **k): text = k.get('text') or k.get('universal_newlines') enc = (lambda s: s) if text else (lambda s: s.encode()) if isinstance(args, (list, tuple)) and args: c = [str(x) for x in args] if c[0] == 'xcrun' and 'metal' in c and '--version' in c: return _done(args, enc('none\n')) if c[0] == 'xcrun' and 'metal' in c and '-c' in c and '-o' in c: # 源码 = metal 之后第一个不是选项、也不是 -o 的值的参数(iOS 那条命令在 -c 后面还带一个版本选项) | source = the first argument after metal that is neither an option nor the value of -o (the iOS command also carries a version option after -c) o = c.index('-o'); src = [x for i, x in enumerate(c[c.index('metal') + 1:], c.index('metal') + 1) if not x.startswith('-') and i != o + 1] if len(src) != 1: raise RuntimeError('nometal_run:认不出源码参数 %r' % c) with open(src[0], 'rb') as f: b = f.read() with open(c[o + 1], 'wb') as f: f.write(b.replace(b'\r\n', b'\n')) # Windows 上 spirv-cross 写出的源码是 \r\n 换行:进安装包前归一,同样的输入各系统出的包才相同 | On Windows spirv-cross writes sources with CRLF line endings: normalize before they go into the install package, so the same input gives the same package on every system return _done(args, enc('')) if c[0] == 'xcrun' and 'metallib' in c and '-o' in c: shutil.copyfile(c[c.index('metallib') + 1], c[c.index('-o') + 1]); return _done(args, enc('')) if c[0] == 'xcrun': raise RuntimeError('nometal_run:没认出的 xcrun 命令 %r' % c) if c[0] == 'which' and len(c) == 2: return _done(args, enc((shutil.which(c[1]) or '') + '\n')) if c[0] == 'cp' and len(c) == 4 and c[1] == '-c': shutil.copyfile(c[2], c[3]); return _done(args, enc('')) return _real(args, *a, **k) if __name__ == '__main__': if len(sys.argv) < 2: sys.exit(__doc__ or '用法:nometal_run.py <脚本.py> [参数…]') subprocess.run = run script = os.path.abspath(sys.argv[1]); sys.argv = sys.argv[1:]; sys.path.insert(0, os.path.dirname(script)) runpy.run_path(script, run_name='__main__')