198 lines
8.8 KiB
Python
198 lines
8.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Build Choano from its locked shared packages; Arcade only consumes it."""
|
|
import argparse
|
|
import importlib.util
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import shlex
|
|
import subprocess
|
|
import sys
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
os.chdir(ROOT)
|
|
ZIRAN = os.environ.get('ZIRAN', 'ziran')
|
|
LOCKED = [] if (ROOT / 'ziran.local.toml').exists() else ['--locked']
|
|
|
|
|
|
def run(*args, **kwargs):
|
|
subprocess.run(list(map(str, args)), check=True, **kwargs)
|
|
|
|
|
|
def prepare_theme():
|
|
run(sys.executable, ROOT / "scripts/theme.py")
|
|
|
|
|
|
def package(name):
|
|
return Path(subprocess.check_output([ZIRAN, 'pkg', 'path', name, *LOCKED], text=True).strip())
|
|
|
|
|
|
def shared_script(owner, name):
|
|
spec = importlib.util.spec_from_file_location(name, package(owner) / 'scripts' / (name + '.py'))
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
def renderer_metadata(selection, platform, env=None):
|
|
renderers = shared_script('kryon', 'renderer_artifact')
|
|
return renderers.renderer_metadata(artifact(selection, platform, env))
|
|
|
|
|
|
def renderer_links():
|
|
meta = renderer_metadata('kryon:renderer-desktop', 'linux-x86_64-gnu')
|
|
links = [*meta['archives'], *meta['link_flags']]
|
|
# Keep the lazy codecs in DT_NEEDED for AppImage packaging. Ziran splits
|
|
# LDLIBS without expanding a literal $ORIGIN; the AppRun supplies its path.
|
|
for library in meta['shared_libraries']:
|
|
links += ['-Wl,--no-as-needed', library, '-Wl,--as-needed', '-Wl,-rpath,' + str(Path(library).parent)]
|
|
return links
|
|
|
|
|
|
def artifact(selection, platform, env=None):
|
|
return shared_script('ziran', 'ziran_artifacts').request(selection, platform, project=ROOT, ziran=ZIRAN, env=env)
|
|
|
|
|
|
# The system libraries Entity's native-profile recipe adds for its Profile UI
|
|
# Choano compiles the account modules into each native host, so it only
|
|
# requests Entity's platform and cryptography support.
|
|
PROFILE_SYSTEM = {
|
|
'linux-x86_64-gnu': (['sdl2', 'cairo', 'freetype2', 'fontconfig', 'pangocairo', 'gtk+-3.0'], ['-lX11']),
|
|
'windows-x86_64-mingw': ([], ['-flto=1', '-luser32', '-lshell32', '-lole32', '-lcomdlg32']),
|
|
}
|
|
|
|
|
|
def account_links(platform, env=None):
|
|
"""The Entity account provider's foreign libraries for PLATFORM, in its own
|
|
order: its Ziran modules are ordinary imports, so no Entity archive."""
|
|
flags = artifact('entity:native-crypto', platform, env).link_flags(env=env)
|
|
packages, libraries = PROFILE_SYSTEM[platform]
|
|
if packages:
|
|
flags.extend(shlex.split(subprocess.check_output(['pkg-config', '--libs', *packages], text=True, env=env)))
|
|
return [*flags, *libraries]
|
|
|
|
|
|
# The Profile bundle native editions read from assets/; never tracked, and
|
|
# kept out of the portable bundle and the web site, which do not open it.
|
|
PROFILE_ASSET = ROOT / 'assets/entity.zx'
|
|
|
|
|
|
def profile_asset():
|
|
profile()
|
|
partial = PROFILE_ASSET.with_name('entity.zx.new')
|
|
shutil.copyfile(ROOT / 'build/entity.zx', partial)
|
|
partial.replace(PROFILE_ASSET)
|
|
return PROFILE_ASSET
|
|
|
|
|
|
def without_profile(folder, names):
|
|
return ['entity.zx', 'entity.zx.new'] if Path(folder).resolve() == PROFILE_ASSET.parent else []
|
|
|
|
|
|
def link_or_copy(source, target):
|
|
try:
|
|
os.link(source, target)
|
|
except OSError:
|
|
shutil.copy2(source, target)
|
|
|
|
|
|
NATIVE = ROOT / 'build/Choano-native'
|
|
NATIVE_WORK = ROOT / 'build/native-accounts'
|
|
|
|
|
|
def native():
|
|
"""build/Choano-native: src/desktop.zi with the Entity account provider.
|
|
Kryon's native profile takes no defines, so this is its desktop route
|
|
(POSIX threads, the desktop backend's libraries) with CHOANO_ACCOUNTS."""
|
|
profile_asset()
|
|
# Preserve dependency-owned archive order before its platform libraries.
|
|
links = renderer_links()
|
|
links.extend(account_links('linux-x86_64-gnu'))
|
|
curl = subprocess.run(['pkg-config', '--libs', 'libcurl'], text=True, capture_output=True)
|
|
links.extend(shlex.split(curl.stdout) if curl.returncode == 0 else ['-l:libcurl.so.4'])
|
|
links.extend(['-pthread', *shlex.split(os.environ.get('LDLIBS', ''))])
|
|
env = {**os.environ, 'LDLIBS': shlex.join(links),
|
|
'CFLAGS': os.environ.get('CFLAGS', '-O2') + ' -ffunction-sections -fdata-sections -Wno-main -pthread',
|
|
'LDFLAGS': (os.environ.get('LDFLAGS', '') + ' -Wl,--gc-sections').strip()}
|
|
run(ZIRAN, 'build', '--project', *LOCKED, '--target=c', '--exe', '--define', 'POSIX_THREADS',
|
|
'--define', 'CHOANO_ACCOUNTS', '--entry', 'desktop:main', '-o', NATIVE_WORK, 'src/desktop.zi', env=env)
|
|
partial = NATIVE.with_name(NATIVE.name + '.new')
|
|
shutil.copyfile(NATIVE_WORK / 'desktop', partial)
|
|
partial.chmod(0o755)
|
|
partial.replace(NATIVE)
|
|
# The window and menu name Kryon's desktop host reads beside the program.
|
|
NATIVE.with_name(NATIVE.name + '.title').write_text('Choano\n')
|
|
|
|
|
|
def bundle():
|
|
# The art without the native Profile bundle: hosts own the portable Profile.
|
|
staged = ROOT / 'build/portable-assets'
|
|
if staged.exists():
|
|
shutil.rmtree(staged)
|
|
# Every portable asset belongs to the game's verified namespace. Native
|
|
# editions also use root-level packaging manifests, which portable hosts
|
|
# must not expose outside assets/choano/.
|
|
shutil.copytree(ROOT / 'assets/choano', staged / 'choano', copy_function=link_or_copy)
|
|
for name in ('ARTWORK.json', 'manifest.json'):
|
|
source = ROOT / 'assets' / name
|
|
if source.is_file():
|
|
shutil.copyfile(source, staged / 'choano' / name)
|
|
# The game publishes its identity with the unchanged portable payload.
|
|
# Hosts validate this descriptor before exposing the game to a player.
|
|
descriptor = json.loads((ROOT / 'catalog.json').read_text())
|
|
public = {key: value for key, value in descriptor.items()
|
|
if key not in ('build_defines', 'prepare_assets')}
|
|
(staged / 'choano/catalog.json').write_text(json.dumps(public, indent=2) + '\n')
|
|
run(ZIRAN, 'bundle', '--project', *LOCKED, '--define', 'CHOANO_PORTABLE',
|
|
'--define', 'KRYON_HOSTED_UI', '--entry', 'portable:Main',
|
|
'--asset-dir', 'assets=' + str(staged), '-o', 'build/choano.zx', 'src/portable.zi')
|
|
|
|
|
|
def profile():
|
|
entity = package('entity')
|
|
# Profile owns its imports; resolve its package exports with the same compiler.
|
|
entity_locked = [] if (entity / 'ziran.local.toml').exists() else ['--locked']
|
|
run(ZIRAN, 'bundle', '--project', *entity_locked,
|
|
'--define', 'KRYON_HOSTED_UI', '--asset-dir', 'assets=' + str(entity / 'assets'),
|
|
'--entry', 'entity:Main', '-o', ROOT / 'build/entity.zx', entity / 'src/entity.zi', cwd=entity)
|
|
|
|
|
|
def web():
|
|
profile()
|
|
output = ROOT / 'build/web'
|
|
output.mkdir(parents=True, exist_ok=True)
|
|
# Run the bundled profile through the web runtime. Ziran 0.7.0 does not
|
|
# provide the embedded C exporter that Entity's optional --linked uses.
|
|
run(sys.executable, package('entity') / 'scripts/build-web.py',
|
|
'--profile', ROOT / 'build/entity.zx', '--output', output / 'game.html',
|
|
'--source', 'src/web/runtime.zi', '--entry', 'runtime:main')
|
|
# The browser decodes the same AVIF art; Web Audio plays the same WAV files.
|
|
if (output / 'assets').exists():
|
|
shutil.rmtree(output / 'assets')
|
|
shutil.copytree(ROOT / 'assets', output / 'assets', ignore=without_profile)
|
|
for item in (ROOT / 'src/web').iterdir():
|
|
if item.suffix in {'.html', '.js', '.css'}:
|
|
shutil.copyfile(item, output / item.name)
|
|
revision = subprocess.run(['git', 'rev-parse', 'HEAD'], text=True, capture_output=True).stdout.strip() or 'uncommitted'
|
|
page = output / 'index.html'
|
|
asset_revision = json.loads((ROOT / 'assets/manifest.json').read_text())['sha256']
|
|
page.write_text(page.read_text().replace('__CHOANO_ASSET_REVISION__', asset_revision)
|
|
.replace('__CHOANO_BUILD_REVISION__', revision))
|
|
(output / 'build.json').write_text(json.dumps({'source_revision': revision,
|
|
'game': 'choano', 'assets_sha256': json.loads((ROOT / 'assets/manifest.json').read_text())['sha256']}, indent=2) + '\n')
|
|
(output / 'game.html').unlink()
|
|
(output / '_headers').write_text('/game.*\n Cache-Control: no-cache\n/assets/manifest.json\n Cache-Control: no-cache\n/build.json\n Cache-Control: no-cache\n/index.html\n Cache-Control: no-cache\n')
|
|
if any(p.stat().st_size > 25 * 1024 * 1024 for p in output.rglob('*') if p.is_file()):
|
|
raise SystemExit('A site file exceeds the Cloudflare Pages limit')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('target', choices=['native', 'bundle', 'profile', 'profile-asset', 'web'])
|
|
args = parser.parse_args()
|
|
prepare_theme()
|
|
(ROOT / 'build').mkdir(exist_ok=True)
|
|
{'native': native, 'bundle': bundle, 'profile': profile, 'profile-asset': profile_asset,
|
|
'web': web}[args.target]()
|