Files
entity/scripts/build-web.py
T

168 lines
9.8 KiB
Python

#!/usr/bin/env python3
"""Link a browser app with the same Entity account provider as native hosts.
The Emscripten runtime compiles verified ZX programs to WebAssembly when
they are opened, including embedded bundles. Bundles given with --linked
also have their Wasm lowering checked during the build, so unsupported
programs fail before the page is published.
"""
import argparse
import importlib.util
import os
from pathlib import Path
import shutil
import subprocess
def main():
parser = argparse.ArgumentParser(description=__doc__)
# A host that draws its own pages, such as dew.waozi.xyz, has no bundle.
parser.add_argument('--bundle', type=Path)
parser.add_argument('--profile', type=Path, required=True)
parser.add_argument('--output', type=Path, required=True)
parser.add_argument('--entry', default='runtime:main', help='project MODULE:FUNCTION entry')
parser.add_argument('--source', default='src/web/runtime.zi')
parser.add_argument('--linked', type=Path, action='append', default=[],
help='a bundle whose WebAssembly lowering the build must verify')
parser.add_argument('--canvas-app', help='compile --source as an AppContext canvas app (MODULE:Main)')
args = parser.parse_args()
project = Path.cwd()
ziran = os.environ.get('ZIRAN', 'ziran')
locked = [] if (project / 'ziran.local.toml').exists() else ['--locked']
def package(name):
return Path(subprocess.check_output([ziran, 'pkg', 'path', name, *locked], text=True).strip())
toolchain = package('ziran')
kryon = package('kryon')
work = project / 'build/profile-web'
generated = work / 'generated'
if generated.exists():
shutil.rmtree(generated)
output = args.output.resolve()
output.parent.mkdir(parents=True, exist_ok=True)
emcc = os.environ.get('EMCC') or str(Path.home() / 'emsdk/upstream/emscripten/emcc')
if not Path(emcc).exists():
emcc = 'emcc'
emar = str(Path(emcc).with_name('emar')) if '/' in emcc else 'emar'
emcmake = str(Path(emcc).with_name('emcmake')) if '/' in emcc else 'emcmake'
env = {**os.environ, 'EM_CACHE': str(work / 'emscripten-cache')}
canvas = []
bindings = []
entry = args.entry
if args.canvas_app:
if args.bundle or args.linked:
parser.error('--canvas-app uses compiled source without --bundle/--linked')
if (project / 'src/canvas_profile_host.zi').exists():
parser.error('--canvas-app needs an app project with an entity dependency; use make canvas-profile-host-test here')
bindings = ['--bind', 'kryon/canvas_app_run:CanvasAppMain=' + args.canvas_app]
if args.entry == 'runtime:main':
# Native --entry names a project module. Import the dependency's
# public entry instead of guessing its internal package identity.
entry_dir = work / 'entry'
entry_dir.mkdir(parents=True, exist_ok=True)
source = entry_dir / 'entity_canvas_entry.zi'
source.write_text('using Host :: #import "entity/canvas_profile_host";\n'
'#program_export\n'
'main :: () -> s32 { return Host.EntityCanvasMain() }\n')
canvas = [str(source)]
entry = 'entity_canvas_entry:main'
imports = ['--project', *locked]
if args.canvas_app and canvas:
imports += ['--module-path', str(entry_dir)]
# Keep every module's package identity, including explicit dependency entry
# files. Flattening private dependency roots here collides with app modules
# such as rewards and bypasses the lock's transitive namespace resolution.
# Kryon selects browser code by the target OS; PLATFORM_WEB remains for
# Entity's own web selectors.
# Kryon's test driver is test code. A page that a test drives is built with
# KRYON_TEST_DRIVER set; every other page leaves the driver out.
driver = ['--define', 'KRYON_TEST_DRIVER'] if os.environ.get('KRYON_TEST_DRIVER') else []
subprocess.run([ziran, 'build', *imports, '--target=c', '--target-os', 'web', '--target-cpu', 'wasm32',
'--define', 'PLATFORM_WEB', *driver,
'--define', 'BUNDLE_NATIVE_RENDERER', '--entry', entry, *bindings,
'-o', str(generated), args.source, *canvas], check=True, env=env)
if args.canvas_app:
# The complete provider is already generated above. Consume only its
# foreign support artifact, never a second copy through libentity.a.
def helper(name, path):
spec = importlib.util.spec_from_file_location(name, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
artifacts = helper('ziran_artifacts', toolchain / 'scripts/ziran_artifacts.py')
renderers = helper('renderer_artifact', kryon / 'scripts/renderer_artifact.py')
env.update(CC=str(Path(shutil.which(emcc) or emcc).absolute()), AR=emar)
target = 'emscripten-wasm32-emscripten'
crypto = artifacts.request('entity:native-crypto',
target, project=project, ziran=ziran, env=env)
renderer = renderers.renderer_metadata(artifacts.request('kryon:renderer-web', target,
project=project, ziran=ziran, env=env))
sources = sorted(generated.rglob('*.c'))
includes = sorted({generated, *(path.parent for path in sources)})
subprocess.run([emcc, '-O2', '-I' + str(toolchain / 'include'),
*['-iquote' + str(path) for path in includes], *renderer['compile_flags'],
*map(str, sources), *renderers.renderer_link(renderer, preloads=False),
*crypto.link_flags(env=env),
'--embed-file', renderer['resources']['font'] + '@/kryon-font.ttf',
'--embed-file', str(args.profile.resolve()) + '@/entity.zx',
'-sSTACK_SIZE=16777216', '-sASYNCIFY_STACK_SIZE=1048576',
'-sENVIRONMENT=web', '-sEXPORTED_RUNTIME_METHODS=FS', '-sEXPORTED_FUNCTIONS=_main',
'--shell-file', str(kryon / 'mk/zx-shell.html'), '-o', str(output)], check=True, env=env)
return
# The page runs the profile's ZX bundle, so its runtime needs Ziran's
# instruction engine; a BOOTSTRAP=1 runtime has none. Ziran builds its
# own host compiler with HOST_CC for this cross build.
runtime = work / 'runtime/libziran.a'
subprocess.run(['make', '-C', str(toolchain), 'BUILD_DIR=' + str(runtime.parent),
'CC=' + emcc, 'AR=' + emar, 'OBJCOPY=true', 'TARGET_MACHINE=wasm32-unknown-emscripten',
'CFLAGS=-O2', str(runtime)], check=True, env=env)
oqs_source = package('liboqs')
oqs = work / ('oqs-' + oqs_source.name)
subprocess.run([emcmake, 'cmake', '-S', str(oqs_source), '-B', str(oqs),
'-DCMAKE_BUILD_TYPE=MinSizeRel', '-DBUILD_SHARED_LIBS=OFF', '-DOQS_BUILD_ONLY_LIB=ON',
'-DOQS_USE_OPENSSL=OFF', '-DOQS_DIST_BUILD=OFF', '-DOQS_OPT_TARGET=generic',
'-DOQS_MINIMAL_BUILD=SIG_ml_dsa_44;KEM_ml_kem_768'], check=True, env=env)
subprocess.run(['cmake', '--build', str(oqs), '--target', 'oqs', '-j2'], check=True, env=env)
# Current Ziran installs its WebAssembly provider in Emscripten's
# libziran. It compiles each exact bundle at instantiation; the removed
# native-c/linked-zx route must not bypass that runtime and its host ABI.
if args.linked:
programs = work / 'linked'
if programs.exists():
shutil.rmtree(programs)
programs.mkdir(parents=True)
zx2wasm = os.environ.get('ZX2WASM') or str(toolchain / 'build/bin/zx2wasm')
for index, bundle in enumerate(args.linked):
subprocess.run([zx2wasm, '-o', str(programs / f'{index}.wasm'),
str(bundle.resolve())], check=True, env=env)
# Package the game separately so large bundles do not inflate the Wasm file.
# Emscripten does not allow --embed-file and --preload-file in one invocation.
packager = Path(shutil.which(emcc) or emcc).resolve().parent / 'tools/file_packager'
preload = []
if args.bundle:
script = work / 'game-preload.js'
subprocess.run([str(packager), str(output.with_suffix('.data')), '--preload',
str(args.bundle.resolve()) + '@/app.zx', '--js-output=' + str(script),
'--no-node'], check=True, env=env)
preload = ['--pre-js', str(script)]
subprocess.run([emcc, '-O2', '-ffunction-sections', '-fdata-sections',
'-I' + str(toolchain / 'include'), '-iquote', str(generated),
*[item for path in generated.rglob('*') if path.is_dir() for item in ('-iquote', str(path))],
*map(str, generated.rglob('*.c')), str(runtime),
'-I' + str(package('monocypher') / 'src'), str(package('monocypher') / 'src/monocypher.c'),
str(package('monocypher') / 'src/optional/monocypher-ed25519.c'), str(oqs / 'lib/liboqs.a'),
'--js-library', str(toolchain / 'web/ziran_web.js'), '-lidbfs.js',
'--embed-file', str(kryon / 'assets/fonts/LiberationSans-Regular.ttf') + '@/kryon-font.ttf',
*preload,
'--embed-file', str(args.profile.resolve()) + '@/entity.zx',
'-sUSE_ZLIB=1', '-sASYNCIFY', '-sALLOW_MEMORY_GROWTH=1', '-sFORCE_FILESYSTEM=1',
'-sSTACK_SIZE=16777216', '-sASYNCIFY_STACK_SIZE=1048576', '-sENVIRONMENT=web',
'-sEXPORTED_RUNTIME_METHODS=FS',
'-sEXPORTED_FUNCTIONS=_main',
'--shell-file', str(kryon / 'mk/zx-shell.html'), '-o', str(output)], check=True, env=env)
if __name__ == '__main__':
main()