Skip to content

libcvc for wasi (wasm32-wasip1), and a wasi leg in publish-cvcgl-wasm - #1584

Open
transfix wants to merge 3 commits into
masterfrom
feat/libcvc-wasi
Open

transfix wants to merge 3 commits into
masterfrom
feat/libcvc-wasi

Conversation

@transfix

Copy link
Copy Markdown
Owner

libcvc builds for platform wasi: wasm32-wasip1, a WASI command for an engine such as Wasmtime. publish-cvcgl-wasm.yml gets a libcvc-wasi job that builds it on each v* tag, runs two smokes under wasmtime and publishes libcvc for wasi/wasm32. It's part of Joe's fleet ask of 2026-11-26 (wasm, wasm-mt and wasi builds on every release).

Depends on cy-pca/cvcpkg#646, which fixes the wasi lanes of libcvc's closure (boost, pegtl, ImageMagick and its codecs). Until those bundles are published, the job's install-deps step fails.

The target

WASI preview 1 has no threads (std::thread throws "Not supported", though mutexes and condition variables work), no sockets of its own, no processes, no signals and no terminal control. That matches Emscripten without -pthread, so the port mostly reuses the switches that build already has:

  • CMake.
    • elseif(WASI) next to if(EMSCRIPTEN): wasm exceptions in the standard encoding (-fwasm-exceptions -mllvm -wasm-enable-sjlj -mllvm -wasm-use-legacy-eh=false, the flavour of the whole cvcpkg wasi column) and an 8 MiB stack.
    • The cvc target passes the exception flags and wasi-sdk's eh runtime (unwind, setjmp) to every consumer, plus wasi-libc's signal, getpid and process-clock emulations, which the static ImageMagick links.
    • Boost is static, as on Emscripten.
    • state_transport_ipc.cpp (Unix domain sockets) is left out, as on Windows.
  • CVC_NO_THREADS (new inc/cvc/core/thread_support.h): defined for Emscripten without pthreads and for wasip1. The no-threads guards that only meant "no thread can start" now use it: async_lane, lock_manager, sliced_task, media, data, world_worker, scene_swarm and field_step. Guards that also pick a browser API stay __EMSCRIPTEN__.
  • producer_thread: the browser build ticks from an Emscripten timer. On wasi, ticks happen only when the host calls run_now().
  • Extended to __wasi__:
    • stubs: Boost.Asio users (utility, loopback redirect, net_engine, local address), cvc::process;
    • simpler paths: browser.cpp (no process to spawn), host_signals (no signals: a stop comes only from request_stop()), host_info (isatty without a size), term_graphics (no probe), uri (no dladdr; a random-name temp file instead of mkstemps), coef_mlp (no dladdr);
    • no owner or mode checks: private_file;
    • other: oidc's randomness (getentropy), print's stdout lock, native_stack's stack bound (__stack_low, as Emscripten's emscripten_stack_get_end).

Checked

Tested locally in WSL Ubuntu 22.04 with cvcpkg's wasi-sdk 33 and wasmtime 45, against wasi bundles built from cvcpkg#646.

  • The cvc target and both smokes build with no cvcpkg env flags, so the CMake WASI branch supplies everything.
  • wasmtime run -W exceptions=y state_exec_wasm_smoke prints STATE_EXEC_WASM_OK. That covers the state_exec locks with no threads, app::lock_manager() single, and the pools at threads 0.
  • image_magick_roundtrip_test, with a preopened /tmp, round-trips TIFF (byte-identical) and WebP. Emscripten's static coder-registration workaround isn't needed on wasi.
  • cmake --install plus cvcpkg pack --from-prefix --platform wasi packs the install (59.6 MB). Its CMake files name no build path, and cvcTargets.cmake carries the EH flags and unwind/setjmp to consumers.
  • clang-format 18.1.8 shows no diff on the changed lines.

Before the job can publish

  1. wasi: wasm exception handling for the column; fix the lanes libcvc needs cy-pca/cvcpkg#646 is merged and its wasi bundles are published (a populate run).
  2. A cvcpkg release includes fix(http-cache): a late single-flight leader re-reads the entry before fetching #646's pack fix. Until then, pack --from-prefix --platform wasi tags the bundle x86_64, and the job stops with an error saying so.
  3. A v* tag, or a publish-cvcgl-wasm dispatch with dry_run=false.

Not covered

  • No wasi ari or hosts. Whether there should be one, and in what form, is a question for Joe.
  • The single-threaded wasm (Emscripten) build isn't rebuilt here. CVC_NO_THREADS is the same condition as before for it; ci.yml's ari-wasm job covers it.

libcvc builds as a static library for platform wasi, a WASI command for an
engine such as Wasmtime, against cvcpkg's wasi column (cy-pca/cvcpkg#646).
publish-cvcgl-wasm.yml's new libcvc-wasi job builds it on each v* tag with
cvcpkg's wasi-sdk, runs state_exec_wasm_smoke and image_magick_roundtrip_test
under wasmtime, and packs + publishes libcvc for wasi/wasm32.

wasip1 has no threads (the thread API is there, std::thread throws), no
sockets of its own, no processes, no signals and no terminal control: the
model of Emscripten without -pthread, so the port reuses that build's
switches.

- CMake: an `elseif(WASI)` beside `if(EMSCRIPTEN)` builds with wasm
  exceptions in the standard encoding (-fwasm-exceptions, -wasm-enable-sjlj,
  -wasm-use-legacy-eh=false: the flavour of the whole wasi column) and an
  8 MiB stack. The cvc target hands those flags, wasi-sdk's eh runtime
  (unwind, setjmp) and the static ImageMagick's wasi-libc emulations
  (signal, getpid, process clocks) to its consumers. Boost static, as on
  Emscripten. state_transport_ipc.cpp is left out, as on Windows.
- inc/cvc/core/thread_support.h: CVC_NO_THREADS, for Emscripten without
  pthreads and for wasip1. The guards that only meant "no thread can start"
  use it (async_lane, lock_manager, sliced_task, media, data, world_worker,
  scene_swarm, field_step); those that also pick a browser API stay
  __EMSCRIPTEN__. producer_thread gets a wasi branch: the host's run_now()
  ticks it, since there is no Emscripten timer.
- __wasi__ beside __EMSCRIPTEN__: stub out the Boost.Asio users (utility,
  loopback redirect, net_engine, local address) and cvc::process; browser.cpp
  has no process to spawn; host_signals has no signals (a stop comes from
  request_stop()); host_info gets isatty and no size; term_graphics probes
  nothing; uri drops dladdr and writes temp files under a random name
  instead of mkstemps; coef_mlp drops dladdr; private_file skips owner and
  mode checks; oidc takes randomness from getentropy; print takes no stdout
  lock; native_stack bounds the stack at wasm-ld's __stack_low.

Checked in WSL with cvcpkg's wasi-sdk 33 and wasmtime 45, against wasi
bundles built from #646: the cvc target and both smokes build without the
cvcpkg env flags (the CMake branch alone), state_exec_wasm_smoke prints
STATE_EXEC_WASM_OK, image_magick_roundtrip_test round-trips TIFF
(byte-identical) and WebP through a preopened /tmp, and `cvcpkg pack
--from-prefix --platform wasi` packs the install (relocatable CMake files).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant