aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/build_browser_sweep.py (2194B)


      1 """Link the current WASM objects with a test-only presentation/capture adapter."""
      2 import os
      3 import hashlib
      4 import json
      5 import pathlib
      6 import shlex
      7 import subprocess
      8 
      9 root = pathlib.Path(__file__).resolve().parents[2]
     10 output = root / 'local/playfield-sweep/browser-build'
     11 output.mkdir(parents=True, exist_ok=True)
     12 compiler = root / 'toolchains/emsdk/upstream/emscripten/emcc'
     13 subprocess.run(['make', '-f', 'Makefile.wasm', '-j4', 'objects'], cwd=root / 'client', check=True)
     14 subprocess.run([str(compiler), '-O2', '-pthread', '-Iclient/runtime', '-c',
     15     'tools/client/sweep_browser_capture.c', '-o', str(output / 'capture.o')], cwd=root, check=True)
     16 
     17 # Reuse the production link flags; the adapter adds no production runtime switches.
     18 plan = subprocess.check_output(['make', '-f', 'Makefile.wasm', '-n', '-W',
     19     'Makefile.wasm', 'build/wasm/web/game.js'], cwd=root / 'client', text=True)
     20 commands = [shlex.split(line) for line in plan.splitlines()
     21     if line.startswith('../toolchains/emsdk/upstream/emscripten/emcc ')
     22     and line.endswith('-o build/wasm/web/game.js')]
     23 assert len(commands) == 1
     24 command = commands[0]
     25 command[-1] = str(output / 'sweep-game.js')
     26 command += [str(output / 'capture.o'), '-Wl,--wrap=gl_target_present']
     27 subprocess.run(command, cwd=root / 'client', env={**os.environ, 'BINARYEN_CORES': '8'}, check=True)
     28 
     29 # Tie each sweep to the linked module and the source inputs, not a later checkout.
     30 inputs = subprocess.check_output(['git', 'ls-files', 'client'], cwd=root, text=True).splitlines()
     31 inputs.append('tools/client/sweep_browser_capture.c')
     32 digest = hashlib.sha256()
     33 for name in inputs:
     34     digest.update(name.encode() + b'\0')
     35     digest.update((root / name).read_bytes())
     36 with (output / 'sweep-game.wasm').open('rb') as module:
     37     module_hash = hashlib.file_digest(module, 'sha256').hexdigest()
     38 metadata = {
     39     'source_commit': subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=root, text=True).strip(),
     40     'source_sha256': digest.hexdigest(), 'module_sha256': module_hash,
     41     'compiler': subprocess.check_output([str(compiler), '--version'], text=True).splitlines()[0],
     42 }
     43 (output / 'build.json').write_text(json.dumps(metadata, indent=2) + '\n')