Files
d2r-mobile/dm/android/tests/run_checks.py

108 lines
5.1 KiB
Python

#!/usr/bin/env python3
"""Game-free host checks, NDK build, and optional real-device Vulkan regression.
Default: host + ASan/UBSan + Android test binaries, without contacting a device.
--run-device requires an explicit serial; it only pushes these test binaries to
a unique /data/local/tmp directory. It does not install/start the game app.
"""
import argparse
import hashlib
import json
import os
from pathlib import Path
import shlex
import shutil
import subprocess
import time
import uuid
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--output', type=Path, required=True)
parser.add_argument('--ndk', type=Path)
parser.add_argument('--host-only', action='store_true')
parser.add_argument('--run-device', action='store_true')
parser.add_argument('--serial')
args = parser.parse_args()
if args.run_device and (args.host_only or not args.serial):
parser.error('--run-device requires --serial and an Android build')
repo = Path(__file__).resolve().parents[3]
out = args.output.resolve()
out.mkdir(parents=True, exist_ok=False)
report = {'schema': 1, 'passed': False, 'device_contacted': False,
'gpu_executed': False, 'game_tested': False, 'steps': []}
def save():
(out / 'result.json').write_text(json.dumps(report, indent=2) + '\n')
def run(name, command, timeout=120):
print(name, flush=True)
start = time.monotonic()
process = subprocess.run(command, cwd=repo, capture_output=True, text=True, timeout=timeout)
(out / (name + '.log')).write_text(process.stdout + process.stderr)
report['steps'].append({'name': name, 'command': command, 'exit_code': process.returncode,
'elapsed_seconds': round(time.monotonic() - start, 3)})
save()
process.check_returncode()
return process.stdout
save()
try:
candidates = sorted((Path.home() / 'Library/Android/sdk/ndk').glob('*'),
key=lambda p: tuple(int(v) for v in p.name.split('.') if v.isdigit()))
ndk = args.ndk or (Path(os.environ['ANDROID_NDK_HOME']) if os.environ.get('ANDROID_NDK_HOME') else None)
if ndk is None and candidates:
ndk = candidates[-1]
if ndk is None:
raise RuntimeError('Specify --ndk (Vulkan headers are needed even for host-only checks)')
ndk = ndk.resolve()
headers = list(ndk.glob('toolchains/llvm/prebuilt/*/sysroot/usr/include'))
if len(headers) != 1:
raise RuntimeError('Expected one NDK host toolchain under ' + str(ndk))
cc = shlex.split(os.environ.get('HOST_CC', 'clang'))
common = cc + ['-std=c11', '-O1', '-g', '-Wall', '-Wextra', '-Werror', '-idirafter', str(headers[0]),
'dm/android/tests/t_vk_mip_layout.c']
for mode, flags in [('normal', []), ('sanitized', ['-fsanitize=address,undefined', '-fno-omit-frame-pointer'])]:
binary = out / ('mip-' + mode)
run('build-' + mode, common + flags + ['-o', str(binary)])
run('run-' + mode, [str(binary)])
if not args.host_only:
binaries = out / 'android'
run('ndk-build', ['make', '-C', 'dm/android/tests', 'all', 'NDK=' + str(ndk), 'OUT=' + str(binaries)], 180)
report['artifacts_sha256'] = {p.name: hashlib.sha256(p.read_bytes()).hexdigest() for p in sorted(binaries.iterdir())}
sources = ['dm/gfx/vulkan/nvn_vk.c', 'dm/gfx/vulkan/nvn_vk_mip_layout.h',
'dm/android/tests/t_vk_perf.c', 'dm/android/tests/t_vk_caps.c',
'dm/android/tests/t_vk_mip_layout.c', 'dm/android/tests/Makefile', 'dm/android/tests/run_checks.py']
report['source_sha256'] = {p: hashlib.sha256((repo / p).read_bytes()).hexdigest() for p in sources}
if args.run_device:
adb = shutil.which('adb')
if not adb:
raise RuntimeError('adb is not available')
device = [adb, '-s', args.serial]
report['device_contacted'] = True
run('device-state', device + ['get-state'])
remote = '/data/local/tmp/d2r-checks-' + uuid.uuid4().hex
run('device-mkdir', device + ['shell', 'mkdir', remote])
try:
for name in ('t_vk_caps', 't_vk_perf'):
run('push-' + name, device + ['push', str(binaries / name), remote + '/' + name])
run('chmod-' + name, device + ['shell', 'chmod', '700', remote + '/' + name])
caps = run('device-caps', device + ['shell', remote + '/t_vk_caps'])
report['capabilities'] = json.loads(caps)
report['gpu_executed'] = True
run('device-regression', device + ['shell', remote + '/t_vk_perf'], 180)
finally:
run('device-cleanup', device + ['shell', 'rm', '-rf', remote])
report['passed'] = True
except Exception as exc:
report['error'] = str(exc)
raise
finally:
save()
print('ANDROID_CHECKS_PASS ' + str(out / 'result.json'))
if __name__ == '__main__':
main()