#!/usr/bin/env python3 """Build payload runtime inputs from a pinned IPSW and the selected sources. Invoked by unixnu.runtime with a JSON request; no device ID or private binary is stored in source control. Existing Data seeds are copied into no writable mount. """ import argparse import contextlib import json import os from pathlib import Path import plistlib import re import struct import subprocess import sys import tempfile import zlib ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'scripts')) from mesa_patch_identity import validate_recorded_patches def run(*command, **kwargs): subprocess.run([str(part) for part in command], check=True, **kwargs) def script(name, *args, **kwargs): run(sys.executable, ROOT / 'scripts' / name, *args, **kwargs) @contextlib.contextmanager def os_mount(image, work): # A developer may already have this exact OS mounted for inspection. Reuse # only a read-only attachment, and leave its lifetime with its owner. info = plistlib.loads(subprocess.check_output(['hdiutil', 'info', '-plist'])) for entry in info.get('images', []): if Path(entry.get('image-path', '')).resolve() != Path(image).resolve(): continue if entry.get('writeable', True): raise SystemExit('The IPSW OS image is already attached writable; require an unchanged read-only input') mounts = [Path(item['mount-point']) for item in entry.get('system-entities', []) if item.get('mount-point') and (Path(item['mount-point']) / 'sbin/launchd').is_file()] if len(mounts) == 1: yield mounts[0] return mount = work / 'os-mount' mount.mkdir() attached = plistlib.loads(subprocess.check_output(['hdiutil', 'attach', '-readonly', '-nobrowse', '-mountpoint', str(mount), '-plist', str(image)])) device = attached['system-entities'][0]['dev-entry'] try: yield mount finally: run('hdiutil', 'detach', device) def checkout(path, pin): if not path.exists(): path.mkdir(parents=True) run('git', 'init', '-q', path) run('git', '-C', path, 'fetch', '--depth=1', pin.get('mirror', pin['repository']), pin['commit']) run('git', '-C', path, 'checkout', '--detach', 'FETCH_HEAD') return True revision = subprocess.check_output(['git', '-C', str(path), 'rev-parse', 'HEAD'], text=True).strip() if revision != pin['commit']: raise SystemExit(f'{path}: source revision differs from its pin; preserve and inspect the checkout') return False def build_plugin(target): pins = json.loads((ROOT / 'scripts/data/adreno-compiler-inputs.json').read_text()) mesa = ROOT / '.cache/mesa-work' fresh = checkout(mesa, pins['mesa']) if fresh: for patch in pins['patches']: run('git', '-C', mesa, 'apply', ROOT / patch) validate_recorded_patches(ROOT, mesa, pins) translator = json.loads((ROOT / 'scripts/data/gpu-metal-vulkan-inputs.json').read_text())['translator'] checkout(ROOT / '.cache/gpu-reference/metal2vulkan', translator) venv = ROOT / '.cache/mesa-venv' if not (venv / 'bin/python').is_file(): run(sys.executable, '-m', 'venv', venv) run(venv / 'bin/python', '-m', 'pip', 'install', '-r', ROOT / 'scripts/requirements-adreno-compiler.txt') script('build-turnip-darwin.py', '--target', target) # Replay is a diagnostic capture from a host GPU simulation, not a runtime # dependency. Do not require unrelated test fixtures in a release build. script('build-adreno-metal-plugin.py', '--target', target, '--no-replay') return ROOT / 'build' / ('adreno-metal-plugin27' if target == 'ios27' else 'adreno-metal-plugin') def addon_hashes(path, work): data = path.read_bytes() if data[:8] != b'NGPUADD2' or len(data) < 24 or struct.unpack_from('