[ANN] ocaml-emcc: compile OCaml programs with their C stubs to WebAssembly

Hi everyone,

I’ve been working on a tool called ocaml-emcc, and it’s now in a state where other people could try it: GitHub - rboudrouss/ocaml-emcc: Ocaml-emcc compiles ocaml projects and their C dependecies to javascript/wasm using Emscripten · GitHub

The idea is pretty simple. Instead of compiling OCaml to JS or wasm, I compile ocamlrun with emscripten, compile all the C stubs of the program and its dependencies with emcc, and link the whole thing into one module. Nothing has to be rewritten in JavaScript. zarith, sqlite3, camlzip, cryptokit, mirage-crypto, ctypes bindings and so on “just” work, and so does your own C.

The obvious downside is that it’s bytecode running in an interpreter, so it’s a lot slower than js_of_ocaml or wasm_of_ocaml. If your code is pure OCaml or its C stubs already have JavaScript implementations, use those instead. This is for the cases where the FFI part is the problem.

Give it a shot!

opam pin add ocaml-emcc git+https://github.com/rboudrouss/ocaml-emcc.git
source /path/to/emsdk/emsdk_env.sh  # emcc in PATH (you need to install emscripten)
ocaml-emcc build main.js     # in your dune project
node main.js                 # or load main.js in a web page

The extension of the output decides what you get (see the README and emscripten’s documentation for more flags):

  • main.js: node and browsers
  • main.html: the same, plus a minimal page
  • main.wasm: a standalone module, which still imports a few emscripten functions
  • main.wasm --wasi: imports only WASI functions, so it runs on wasmtime or node’s WASI

It’s still a prototype, so there are rough edges, if you try it on something and it breaks, I’d really like to hear about it. Packages that don’t build are exactly what I’m looking for right now. Feel free to propose/ask for new recipes too.

This started as an attempt to compile MOPSA, a static analyzer written in OCaml, to wasm. It turned out to be really hard because of how much FFI it relies on (apron, gmp, clang’s frontend…). I eventually got it working by patching both OCaml and MOPSA’s sources, and gave a talk about it at the OCaml Workshop 2026. Since then I’ve been turning that work into a general tool, and it turns out to generalize quite well, so here it is.

I’m curious to hear what people would use it for, especially if you have an OCaml project that never made it to the browser because of its C dependencies.

13 Likes

I’m excited to try it out. I wanted to use this approach on a raylib-ocamlgame ever since I’ve seen the sildes in the OCaml workshop.

It’s a pity we don’t have a better way to compile ctypes project to wasm but I’ll take it.

1 Like

Thanks! I’ve added a recipe to ocaml-emcc for the raylib package. It builds raylib for its PLATFORM_WEB, so a raylib-ocaml program builds as is with ocaml-emcc build game.html. It seems to work, but I’ve only tried a small demo in Chromium, so feedback is very welcome.

It links with -sASYNCIFY, so the usual while not (window_should_close ()) do ... done loop still works but emcc warns that ASYNCIFY doesn’t fully support wasm exceptions, which OCaml’s runtime uses for longjmp. In my tests exceptions raised from OCaml and from C worked fine across frames, but if you run into odd crashes, try JSPI instead: ocaml-emcc build game.html -- -sJSPI. It needs a recent browser, but it’s smaller and doesn’t have that limitation.

raylib-ocaml’s build rules also needed a patch for cross-compilation because in the target context of dune build -x wasm32, %{system} is still the host’s, so the desktop raylib was built there with the host’s compiler. If you’d be open to something like that upstream, I’d be happy to send a PR.

1 Like

I’m right now working on supporting cross-compilation to wasm for the vendored raylib in raylib-ocaml’s build files. My current version seems to work with yesterday’s release if I link glfw manually with -USE_GLFW=3.
The recipe doesn’t apply anymore, of course. But adding another one for a version 2.4.0 which just supplies this one link flag should be easy enough.

Pretty awesome. And from what I can tell performance is not as bad as I’d feared.

1 Like