98 lines
3.4 KiB
Python
98 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Embed the app's files in a deterministic, borrowed asset table."""
|
|
|
|
import argparse
|
|
import os
|
|
from pathlib import Path
|
|
import json
|
|
|
|
|
|
MIME_TYPES = {
|
|
".png": "image/png",
|
|
".webp": "image/webp",
|
|
".jpg": "image/jpeg",
|
|
".jpeg": "image/jpeg",
|
|
".ogg": "audio/ogg",
|
|
".wav": "audio/wav",
|
|
".mp3": "audio/mpeg",
|
|
".ttf": "font/ttf",
|
|
".otf": "font/otf",
|
|
".kss": "text/plain",
|
|
".txt": "text/plain",
|
|
".dat": "application/octet-stream",
|
|
}
|
|
|
|
|
|
def emit(output: Path, files: list[Path], root: Path) -> None:
|
|
entries = {}
|
|
for file in files:
|
|
resolved = file.resolve(strict=True)
|
|
# Keep build/packages/<name> links unresolved so package assets get
|
|
# stable embedded names regardless of the package cache location.
|
|
if file.name == "apps.zx":
|
|
path = "apps.zx"
|
|
else:
|
|
path = Path(os.path.abspath(file)).relative_to(root).as_posix()
|
|
if path.startswith("build/apps/"):
|
|
path = "cells/" + path.removeprefix("build/apps/")
|
|
if path == "build/inbe.zx":
|
|
path = "inbe.zx"
|
|
if path.startswith("build/packages/kss/styles/"):
|
|
path = path.removeprefix("build/packages/kss/")
|
|
if path.startswith("build/packages/kryon/icons/"):
|
|
path = path.removeprefix("build/packages/kryon/")
|
|
if path in entries:
|
|
raise ValueError(f"duplicate asset path: {path}")
|
|
mime = MIME_TYPES.get(resolved.suffix.lower(), "application/octet-stream")
|
|
entries[path] = (resolved.read_bytes(), mime)
|
|
|
|
lines = [
|
|
"/* Generated by scripts/embed-app-assets.py. */",
|
|
'#include "assets_host.h"',
|
|
"",
|
|
]
|
|
for index, (_, (data, _)) in enumerate(sorted(entries.items())):
|
|
# String literals preserve the bytes while avoiding millions of
|
|
# initializer-expression nodes in the native compiler's memory.
|
|
lines.append(f"static const unsigned char asset_{index}[] =")
|
|
for offset in range(0, len(data), 2048):
|
|
literal = "".join(f"\\x{byte:02x}" for byte in data[offset:offset + 2048])
|
|
lines.append(' "' + literal + '"')
|
|
if not data:
|
|
lines.append(' ""')
|
|
lines.append(";")
|
|
lines.extend(["", "const AssetEntry asset_entries[] = {"])
|
|
for index, (path, (data, mime)) in enumerate(sorted(entries.items())):
|
|
lines.append(
|
|
f" {{(uint8_t *){json.dumps(path)}, (uint8_t *){json.dumps(mime)}, "
|
|
f"(uint8_t *)asset_{index}, {len(data)}u}},"
|
|
)
|
|
if not entries:
|
|
lines.append(' {(uint8_t *)"", (uint8_t *)"", NULL, 0u},')
|
|
lines.extend([
|
|
"};",
|
|
f"const size_t asset_entry_count = {len(entries)}u;",
|
|
"const AssetEntry *asset_entry_at(size_t index) {",
|
|
" return index < asset_entry_count ? &asset_entries[index] : NULL;",
|
|
"}",
|
|
"size_t asset_entry_total(void) { return asset_entry_count; }",
|
|
"",
|
|
])
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
new_content = "\n".join(lines)
|
|
if not output.exists() or output.read_text() != new_content:
|
|
output.write_text(new_content)
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("output", type=Path)
|
|
parser.add_argument("files", nargs="*", type=Path)
|
|
args = parser.parse_args()
|
|
root = Path(__file__).resolve().parent.parent
|
|
emit(args.output, args.files, root)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|