"""Prepare the audited Windows runtime from a pinned APK and rebuilt replacements. The donor was built from a CI merge, not just the PR head. Only unchanged components are reused. Session helpers come from this checkout, libblsession must be rebuilt for all three architectures, Gamescope comes from the current checksum-pinned release, and the VR components are rebuilt in Docker (or reused from a verified build manifest). Store engines are omitted (build with skipRust). """ import argparse import hashlib import io import json import os from pathlib import Path, PurePosixPath import shutil import struct import subprocess import sys import tarfile import tempfile from zipfile import ZipFile ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tools/release')) import runtime_inputs as runtime from check_session_assets import source_assets sys.path.insert(0, str(ROOT / 'tools/vr')) import build_windows as vr_build DONOR_SHA256 = 'b7efef6442f384446bdd1750cec21f0eda61b370bc21d9f55187448e5c256760' DONOR_PR_SOURCE = 'bdf876d47cc9bb6fa810d014370da5bbb3c28039' DONOR_MAIN_SOURCE = '46fac69438ed428f2ca43ea935cd47790264f380' DONOR_TREE = 'ddbca4d572b809fb969be0cf80ef83d6ef6580ce' DONOR_REVISION = 'cf24c5f2c771633ea426046006c2b977c7bd7d32' DONOR_CERT_SHA256 = 'b241ea7d77822d39b89d3227e06f3ecf62adec6cbfd38ad06196787991b5d3f0' DONOR_URL = 'https://github.com/Droid-Deck/DroidDeck-CI/releases/download/pr-732/DroidDeck-PR.732-bdf876d.apk' AUDITED_SOURCE = '1dc66bf9be64a404c80400049c0803614814461b' LOCAL_BUILD_PLUMBING = {'app/build.gradle', 'tools/release/runtime_inputs.py'} LOCAL_ANDROID_SOURCES = {'app/src/main/cpp/CMakeLists.txt', 'app/src/main/cpp/viture/sdk_bridge.cpp', 'app/src/main/cpp/viture/display_modes.h', 'app/src/main/cpp/viture/stereo_frame_gate.h', 'app/src/main/cpp/viture/uvc_bridge.cpp', 'app/src/main/cpp/waylandcomp/src/cardboard.c', 'app/src/main/cpp/waylandcomp/src/cardboard.h', 'app/src/main/cpp/waylandcomp/src/cardboard.frag', 'app/src/main/cpp/waylandcomp/src/cardboard_frag.h', 'app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c', 'app/src/main/cpp/waylandcomp/src/spatialpose.c', 'app/src/main/cpp/waylandcomp/CMakeLists.txt'} LOCAL_SESSION_HELPERS = {'tools/linuxfs/overlay/usr/local/bin/droiddeck-session', 'tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-compat', 'tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-ime', 'tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr', 'tools/linuxfs/overlay/usr/local/bin/droiddeck-text-input'} IME_SOURCES = {'tools/ime/packages.json', 'tools/ime/prepare.py', 'tools/ime/smoke.py'} IME_MANIFEST = 'usr/local/share/droiddeck/ime-files.json' IME_PACKAGES = 'build/windows-setup/ime-packages' VR_PREFIX = 'assets/linuxfs/usr/local/lib/droiddeck-vr/' CHECK_PATHS = list(dict.fromkeys(runtime.SOURCE_PATHS + [ 'app/src/main/cpp', 'app/src/main/jniLibs', 'app/src/main/rust/stores', 'tools/vr'])) REBUILT = {'libblsession.so': 183, 'x86_64/libblsession.so': 62, 'i386/libblsession.so': 3} REBUILT_SHA256 = { 'libblsession.so': '1093ce33ca6d0a17a2eec7b3a3cc53969b01ffc459aef16b320cd3857825b738', 'x86_64/libblsession.so': 'd0a558fee1ac4d5b898170a96b03e15610d9835e7097425027b8c31728da870a', 'i386/libblsession.so': '785d8d9bf1fa20d083a7fed65d064c7580684d7065530d95a648e2d32901d2a2', } JNI = ('libproot.so', 'libproot-loader.so', 'libdirectaudiorelay.so') PREPARED = ('pulseaudio.tzst', 'graphics_driver/adrenotools-turnip25.1.0-quest1.tzst', 'graphics_driver/adrenotools-turnip26.3.0-a6xx.tzst', 'graphics_driver/adrenotools-turnip26.3.0-quest2.tzst') def git(*args): return subprocess.check_output(['git', '-C', str(ROOT), *args]) def changed(base, target=None): args = ['diff', '--no-ext-diff', '--ignore-cr-at-eol', '--name-only', '-z', base] if target: args.append(target) return set(filter(None, git(*args, '--', *CHECK_PATHS).decode().split('\0'))) def check_sources(): if git('merge-tree', '--write-tree', DONOR_PR_SOURCE, DONOR_MAIN_SOURCE).decode().splitlines()[0] != DONOR_TREE: raise ValueError('The donor CI merge does not match the audited source tree') helper_sources = {path.relative_to(ROOT).as_posix() for path in source_assets()} reviewed = (LOCAL_BUILD_PLUMBING | LOCAL_ANDROID_SOURCES | IME_SOURCES | (LOCAL_SESSION_HELPERS & helper_sources)) unexpected = {p for p in changed(AUDITED_SOURCE, 'HEAD') - reviewed if not vr_build.is_source(p)} if unexpected: raise ValueError('Sources changed since the component audit:\n' + '\n'.join(sorted(unexpected))) local = changed('HEAD') untracked = set(filter(None, git('ls-files', '--others', '--exclude-standard', '-z', '--', *CHECK_PATHS).decode().split('\0'))) unexpected = {p for p in (local - reviewed) | (untracked - LOCAL_ANDROID_SOURCES - IME_SOURCES) if not vr_build.is_source(p)} if unexpected: raise ValueError('Unreviewed runtime/native/Rust changes:\n' + '\n'.join(sorted(unexpected))) classifications = {} for path in sorted(changed(DONOR_TREE, 'HEAD') | local | untracked): if path in IME_SOURCES: action = 'regenerated from checksum-pinned RPMs; never reused from donor' elif vr_build.is_source(path): action = 'VR component rebuilt from current source and verified; never imported from donor' elif path in LOCAL_ANDROID_SOURCES: action = 'Android native source compiled by Gradle; not imported from donor' elif path.startswith('app/src/main/rust/stores/'): action = 'store engines omitted; Gradle must use skipRust=true' elif path in helper_sources: action = 'restaged from current source with LF line endings' elif path in {'tools/linuxfs/preload/net.c', 'tools/linuxfs/preload/webhelper.c'}: action = 'all three libblsession libraries rebuilt from current source' elif path == 'tools/gamescope/release.env': action = 'current checksum-pinned Gamescope archive replaces donor' elif path in {'tools/build_local.sh', 'tools/directaudio/fetch.sh'}: action = 'audited download/build cache plumbing; component inputs unchanged' elif path == 'app/src/main/cpp/waylandcomp/src/compositor.c': action = 'Android native library rebuilt by Gradle; not imported' elif path in LOCAL_BUILD_PLUMBING: action = 'local Windows build plumbing; changes recorded below' else: raise ValueError('Unclassified donor source difference: ' + path) classifications[path] = action return classifications def validate_relative(name): path = PurePosixPath(name) if path.is_absolute() or not path.parts or any(part in {'.', '..'} or ':' in part for part in path.parts): raise ValueError('Unsafe archive member: ' + name) if '\\' in name: raise ValueError('Backslash in archive member: ' + name) return path def checked_output(relative): validate_relative(relative) target = (ROOT / relative).resolve() if not target.is_relative_to(ROOT.resolve()): raise ValueError('Output escapes the checkout: ' + relative) if git('ls-files', '--', relative).strip(): raise ValueError('Refusing to overwrite tracked file: ' + relative) return target def read_gamescope(path): pins = dict(line.split('=', 1) for line in (ROOT / 'tools/gamescope/release.env').read_text().splitlines() if line and not line.startswith('#')) if runtime.digest(path) != pins['GAMESCOPE_SHA256']: raise ValueError('Gamescope archive checksum does not match tools/gamescope/release.env') data = subprocess.run(['zstd', '-dc', str(path)], capture_output=True, check=True).stdout files = {} with tarfile.open(fileobj=io.BytesIO(data)) as archive: for member in archive: name = member.name.removeprefix('./') validate_relative(name) if member.isdir(): continue if not member.isfile(): raise ValueError('Gamescope archive contains a non-regular member: ' + name) if name in files: raise ValueError('Duplicate Gamescope archive member: ' + name) files[name] = archive.extractfile(member).read() if set(files) != {'usr/local/bin/gamescope'}: raise ValueError('Unexpected Gamescope archive payload: ' + ', '.join(files)) return files, pins def find_docker(): docker = shutil.which('docker') if docker: return docker desktop = Path(os.environ.get('ProgramFiles', 'C:/Program Files')) / 'Docker/Docker/resources/bin/docker.exe' if desktop.is_file(): return str(desktop) raise ValueError('Docker is required to prepare IME files on Windows; install Docker Desktop or add docker to PATH') def prepare_ime(image): workspace = ROOT / 'build/windows-setup' workspace.mkdir(parents=True, exist_ok=True) packages = ROOT / IME_PACKAGES packages.mkdir(parents=True, exist_ok=True) with tempfile.TemporaryDirectory(prefix='droiddeck-ime-', dir=workspace) as folder: temporary = Path(folder) linuxfs = temporary / 'linuxfs' linuxfs.mkdir() script = ROOT / 'tools/ime/prepare.py' if sys.platform == 'win32': docker = find_docker() inspect_cmd = [docker, 'image', 'inspect', '--format', '{{.Id}} {{.Os}}/{{.Architecture}}', image] inspected = subprocess.run(inspect_cmd, capture_output=True, text=True, check=False) fields = inspected.stdout.strip().split() if inspected.returncode == 0 else [] image_id = fields[0] if len(fields) == 2 and fields[0] and fields[1] == 'linux/amd64' else '' if not image_id and image == vr_build.IMAGE: dockerfile = (ROOT / 'tools/local-cross.Dockerfile').read_bytes().replace(b'\r\n', b'\n') try: subprocess.run([docker, 'build', '--platform', 'linux/amd64', '--tag', image, '--file', '-', str(temporary)], input=dockerfile, check=True) except subprocess.CalledProcessError as error: raise ValueError('IME preparation failed to build default compiler Docker image ' + image + '; inspect the Docker build output above, resolve the build error, and rerun preparation') from error inspected = subprocess.run(inspect_cmd, capture_output=True, text=True, check=False) fields = inspected.stdout.strip().split() if inspected.returncode == 0 else [] image_id = fields[0] if len(fields) == 2 and fields[0] and fields[1] == 'linux/amd64' else '' if not image_id: detail = inspected.stdout.strip() if inspected.returncode == 0 else inspected.stderr.strip() detail = detail or 'no image id or platform returned' raise ValueError('IME preparation requires the existing Docker image ' + image + ' for linux/amd64; build or load it first (inspect result: ' + detail + ')') snapshot = temporary / 'src' (snapshot / 'tools/ime').mkdir(parents=True) shutil.copy2(script, snapshot / 'tools/ime/prepare.py') shutil.copy2(ROOT / 'tools/ime/packages.json', snapshot / 'tools/ime/packages.json') command = [docker, 'run', '--rm', '--platform', 'linux/amd64', '-v', str(snapshot) + ':/src:ro', '-v', str(packages) + ':/cache', '-v', str(linuxfs) + ':/out', image_id, 'sh', '-c', 'for tool in rpm2cpio cpio; do command -v "$tool" >/dev/null 2>&1 || { ' 'echo "missing $tool; rebuild compiler image using tools/local-cross.Dockerfile" >&2; exit 1; }; done; ' 'exec python3 /src/tools/ime/prepare.py --packages /cache --download --output /out'] else: command = [sys.executable, str(script), '--packages', str(packages), '--download', '--output', str(linuxfs)] try: subprocess.run(command, check=True) except subprocess.CalledProcessError as error: if sys.platform == 'win32': raise ValueError('IME preparation failed; ensure Docker is running and image ' + image + ' is available') from error raise manifest_path = linuxfs / IME_MANIFEST if not manifest_path.is_file(): raise ValueError('IME preparation did not produce ' + IME_MANIFEST) manifest = json.loads(manifest_path.read_text(encoding='utf-8')) if manifest.get('version') != 1 or not isinstance(manifest.get('files'), dict): raise ValueError('Unexpected IME manifest format') files = {} for relative, entry in sorted(manifest['files'].items()): if not isinstance(entry, dict): raise ValueError('Unexpected IME manifest entry: ' + str(relative)) validate_relative(relative) path = linuxfs / relative if not path.is_file(): raise ValueError('IME manifest references a missing file: ' + relative) data = path.read_bytes() if hashlib.sha256(data).hexdigest() != entry.get('sha256'): raise ValueError('IME file checksum mismatch: ' + relative) files[relative] = data files[IME_MANIFEST] = manifest_path.read_bytes() return files, manifest def validate_rebuilt(path, machine): data = path.read_bytes() if len(data) < 20 or data[:4] != b'\x7fELF' or data[5] != 1 or struct.unpack('