#!/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 hashlib import json import os from pathlib import Path import plistlib import re import shutil 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 make_prime_list(request, work, os_root): paths, counts = set(), {} def collect(mount, prefix): command = [sys.executable, str(ROOT / 'scripts/list-metal-libraries.py'), str(mount), prefix] return set(subprocess.check_output(command, text=True).splitlines()) for image in request['prime_images']: if Path(image['image']).resolve() == Path(request['os_image']).resolve(): found = collect(os_root, image['prefix']) else: # Use a system mount point when the build output resides on an attached image. with tempfile.TemporaryDirectory(prefix=f'unixnu-prime-{image["name"]}-') as scratch: with os_mount(image['image'], Path(scratch)) as mount: found = collect(mount, image['prefix']) counts[image['name']] = len(found) paths.update(found) if not paths: raise SystemExit('No Metal libraries in the selected runtime images') output = ROOT / 'build/prime-list.txt' output.parent.mkdir(parents=True, exist_ok=True) output.write_text('\n'.join(sorted(paths)) + '\n') output.with_suffix('.json').write_text(json.dumps(dict(images=request['prime_images'], counts=counts, libraries=len(paths)), indent=2) + '\n') print(f'Metal priming inventory: {len(paths)} paths, {counts}', flush=True) return output 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', '--reuse-bridge') return ROOT / 'build' / {'ios27': 'adreno-metal-plugin27', 'macos27': 'adreno-metal-plugin-macos27'}.get(target, 'adreno-metal-plugin') def build_boot_shims(work, base=None): """macOS on a read-only root container: the owner apfs_boot_util (scripts/macos-boot-shims) below bootshim/ of the addon (BASE, or one of its own), which the kernel shows over apfs.fs/Contents/Resources with apfs-boot-shim=1.""" tree = work / 'boot-shims' (tree / 'bootshim').mkdir(parents=True) exe = tree / 'bootshim/apfs_boot_util' sdk = subprocess.check_output(['xcrun', '--sdk', 'macosx', '--show-sdk-path'], text=True).strip() run('xcrun', '--sdk', 'macosx', 'clang', '-target', 'arm64e-apple-macos27.0', '-isysroot', sdk, '-O2', '-Wall', '-Werror', ROOT / 'scripts/macos-boot-shims/apfs_boot_util.c', '-o', exe) run('codesign', '--force', '--sign', '-', '--pagesize', '4096', '--identifier', 'org.unixnu.apfs_boot_util', exe) if base is None: base = work / 'boot-shims-empty.bin' base.write_bytes(b'NGPUADD2' + struct.pack('> /Users/Shared/unixnu-test/{name}.log 2>&1') (jobs / f'org.unixnu.test.{name}.plist').write_bytes(plistlib.dumps(dict( Label=f'org.unixnu.test.{name}', RunAtLoad=True, ProgramArguments=['/bin/sh', '-c', command], **extra), fmt=plistlib.FMT_XML)) candidate = work / 'test-addon.bin' script('addon-add-dir.py', addon, candidate, 'test', staged, 'Library/LaunchDaemons', jobs_d, 'Library/LaunchAgents', jobs_a) addon = out / 'test-addon.bin' candidate.replace(addon) if trust is not None and (request.get('wifi_tool') or bluetooth_report is not None or appsync_report is not None or absent_report is not None): # Keep every original module intact. Append only absent hashes; # rebuilding from a previously integrated addon stays idempotent. hashes = addon_hashes(addon, work / 'signed-addon', request.get('audio_system_plugins', ()), appsync_report['system_executables'] if appsync_report else None) data = trust.read_bytes() count = struct.unpack_from('