| 1 | // Builds the litehtml renderer prototype for the web dev app: renderer-litehtml/ |
| 2 | // (the core + vendored litehtml + the JS-backed C ABI in src/web/wasm.cpp) → |
| 3 | // Emscripten → renderer-litehtml/pkg-web/twidget_litehtml.{js,wasm} |
| 4 | // (gitignored; the Vite plugin serves it at /litehtml/, outside the module |
| 5 |
| 6 | // rebuilt only when their source is newer. Dev tooling, see |
| 7 | // docs/litehtml-renderer.md. Needs `emcc`/`em++` (brew install emscripten). |
| 8 | // |
| 9 | // corepack pnpm build:litehtml-web |
| 10 |
| 11 | import { spawn, spawnSync } from 'node:child_process'; |
| 12 | import fs from 'node:fs'; |
| 13 | import os from 'node:os'; |
| 14 | import path from 'node:path'; |
| 15 | import { fileURLToPath } from 'node:url'; |
| 16 |
| 17 | const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 18 | const core = path.join(root, 'apps', 'renderer-litehtml'); |
| 19 | const lite = path.join(core, 'third_party', 'litehtml'); |
| 20 | const outDir = path.join(core, 'pkg-web'); |
| 21 | const objDir = path.join(core, 'target', 'web-obj'); |
| 22 | /** tw_ua_stylesheet.h: the profile's UA layer (packages/core/profile.json tables.uaStylesheet), as CMakeLists.txt makes it for the other builds. */ |
| 23 | const genDir = path.join(core, 'target', 'web-gen'); |
| 24 | const env = { ...process.env, PATH: ['/opt/homebrew/bin', '/usr/local/bin', process.env.PATH ?? ''].join(path.delimiter) }; |
| 25 |
| 26 | const includes = [ |
| 27 | path.join(core, 'src'), genDir, path.join(root, 'packages', 'desktop', 'sandbox', 'include'), |
| 28 | path.join(lite, 'include'), path.join(lite, 'include', 'litehtml'), path.join(lite, 'src'), |
| 29 | path.join(lite, 'src', 'gumbo', 'include'), path.join(lite, 'src', 'gumbo', 'include', 'gumbo'), |
| 30 | ].map((p) => `-I${p}`); |
| 31 | const cxxFlags = ['-O2', '-std=c++20', '-fwasm-exceptions', '-Wno-deprecated-declarations', ...includes]; |
| 32 | const cFlags = ['-O2', '-std=gnu99', '-Wno-void-pointer-to-enum-cast', ...includes]; |
| 33 |
| 34 | const sources = [ |
| 35 | ...['renderer.cpp', 'css.cpp', 'animations.cpp', 'canvas.cpp', 'compositor.cpp', 'theme.cpp'].map((f) => path.join(core, 'src', f)), |
| 36 | path.join(core, 'src', 'web', 'wasm.cpp'), |
| 37 | ...fs.readdirSync(path.join(lite, 'src')).filter((f) => f.endsWith('.cpp')).map((f) => path.join(lite, 'src', f)), |
| 38 | ...fs.readdirSync(path.join(lite, 'src', 'gumbo')).filter((f) => f.endsWith('.c')).map((f) => path.join(lite, 'src', 'gumbo', f)), |
| 39 | ]; |
| 40 | const exported = ['twl_create', 'twl_backend_id', 'twl_destroy', 'twl_receive', 'twl_set_width', 'twl_set_theme', 'twl_touch', 'twl_image_ready', 'twl_size', 'twl_dirty', 'twl_display_list', 'twl_stats', 'twl_html', 'twl_string_free', 'twl_alloc', 'twl_free']; |
| 41 |
| 42 | function objectFor(src: string) { |
| 43 | return path.join(objDir, path.relative(root, src).replace(/[\\/]/g, '__') + '.o'); |
| 44 | } |
| 45 |
| 46 | function stale(src: string, obj: string) { |
| 47 | if (!fs.existsSync(obj)) return true; |
| 48 | return fs.statSync(obj).mtimeMs < fs.statSync(src).mtimeMs; |
| 49 | } |
| 50 |
| 51 | function compile(src: string): Promise<void> { |
| 52 | const obj = objectFor(src); |
| 53 | const isC = src.endsWith('.c'); |
| 54 | const args = [...(isC ? cFlags : cxxFlags), '-c', src, '-o', obj]; |
| 55 | return new Promise((resolve, reject) => { |
| 56 | const p = spawn(isC ? 'emcc' : 'em++', args, { cwd: core, env, stdio: 'inherit' }); |
| 57 | p.on('error', (e) => reject(new Error(`${isC ? 'emcc' : 'em++'}: ${e.message}${e.message.includes('ENOENT') ? ' (install it: brew install emscripten)' : ''}`))); |
| 58 | p.on('exit', (code) => (code === 0 ? resolve() : reject(new Error(`${path.basename(src)}: exit ${code}`)))); |
| 59 | }); |
| 60 | } |
| 61 |
| 62 | /** Writes tw_ua_stylesheet.h when its text changed; true when it did (renderer.cpp includes it). */ |
| 63 | function writeUaHeader(): boolean { |
| 64 | const ua: string = JSON.parse(fs.readFileSync(path.join(root, 'packages', 'core', 'profile.json'), 'utf8')).tables.uaStylesheet; |
| 65 | const text = '// Generated by scripts/build-litehtml-web.ts from packages/core/profile.json (tables.uaStylesheet).\n#pragma once\n' + |
| 66 | `inline constexpr const char* TW_UA_STYLESHEET = R"tw-ua(${ua})tw-ua";\n`; |
| 67 | const file = path.join(genDir, 'tw_ua_stylesheet.h'); |
| 68 | if (fs.existsSync(file) && fs.readFileSync(file, 'utf8') === text) return false; |
| 69 | fs.mkdirSync(genDir, { recursive: true }); |
| 70 | fs.writeFileSync(file, text); |
| 71 | return true; |
| 72 | } |
| 73 |
| 74 | async function main() { |
| 75 | fs.mkdirSync(objDir, { recursive: true }); |
| 76 | if (writeUaHeader()) fs.rmSync(objectFor(path.join(core, 'src', 'renderer.cpp')), { force: true }); |
| 77 | fs.mkdirSync(outDir, { recursive: true }); |
| 78 | const todo = sources.filter((s) => stale(s, objectFor(s))); |
| 79 | console.log(`compiling ${todo.length} of ${sources.length} files`); |
| 80 | const workers = Math.max(1, Math.min(os.cpus().length, 8)); |
| 81 | let next = 0; |
| 82 | await Promise.all(Array.from({ length: workers }, async () => { |
| 83 | while (next < todo.length) await compile(todo[next++]); |
| 84 | })); |
| 85 | const out = path.join(outDir, 'twidget_litehtml.js'); |
| 86 | const args = [ |
| 87 | '-O2', '-fwasm-exceptions', |
| 88 | '-sMODULARIZE=1', '-sEXPORT_ES6=1', '-sENVIRONMENT=web,node', '-sFILESYSTEM=0', '-sALLOW_MEMORY_GROWTH=1', '-sINITIAL_MEMORY=32MB', |
| 89 | `-sEXPORTED_FUNCTIONS=${exported.map((f) => '_' + f).join(',')}`, |
| 90 | '-sEXPORTED_RUNTIME_METHODS=UTF8ToString,stringToUTF8,lengthBytesUTF8,HEAPU8,HEAPU32,HEAPF32', |
| 91 | '-o', out, ...sources.map(objectFor), |
| 92 | ]; |
| 93 | console.log(`$ em++ ${args.slice(0, 9).join(' ')} … (${sources.length} objects)`); |
| 94 | const r = spawnSync('em++', args, { cwd: core, env, stdio: 'inherit' }); |
| 95 | if (r.status !== 0) process.exit(r.status ?? 1); |
| 96 | for (const f of fs.readdirSync(outDir).sort()) console.log(`${f}: ${(fs.statSync(path.join(outDir, f)).size / 1024 / 1024).toFixed(2)} MB`); |
| 97 | } |
| 98 |
| 99 | main().catch((e) => { console.error(e.message); process.exit(1); }); |
| 100 |