You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Compiled mode is the default: build and link compile the bytecode
to C with c_of_ocaml (in PATH, or $OCAML_EMCC_C_OF_OCAML). The
interpreter stays available with --interpreted (or interpreted: yes
in ocaml-emcc.conf), less tested, and is used with OCaml before 4.14; --compiled is still accepted. tests/run.sh --interpreted runs the
tests interpreted.
Compiled, c_of_ocaml checks its options (c_of_ocaml --check) before
anything is built: an option it lacks (a newer c_of_ocaml has it) or a
combination it refuses (--cps-stack with --effects disabled) fails
at once, with its message.
The dune patched for the (ctypes) stanza is built again once dune is
upgraded (the copy of the previous one, read-only, made it fail:
bls12-381, lilv).
--jspi (jspi: yes in ocaml-emcc.conf) links with JSPI (emcc's -sJSPI): main is asynchronous, and the compiled code recurses
deeper than the stack of a browser (6 000 to 15 000 nested calls) on new
stacks of the engine (c_of_ocaml's segmented stack), in Chrome and Edge
137 or later, or node 24 and later (node 24's --experimental-wasm-jspi
is set at startup). Without JSPI, the program says so instead of failing
to instantiate. Under --wasi, the compiled code has no new stacks (it
imported emscripten's JavaScript for them, which a WASI host lacks). It also replaces -sASYNCIFY for C
that suspends (emscripten_sleep, raylib's loop), keeping the tail
calls, for a smaller program which may suspend under the code of
plugins.
--cps-stack (cps-stack: yes), compiled: the recursive functions get
a version in continuation-passing style (c_of_ocaml's --cps-stack),
in which a recursion deeper than the stack of a browser goes on, on the
heap, in every browser and with a synchronous main, for a program
about 1.5 times bigger. With --dynlink, the plugins have it too
(plugin --main follows the program, or plugin --cps-stack), and all
have double translation. Not with --effects other than double-translation. It works with -sASYNCIFY (no wasm tail
calls), C suspending under a deep recursion, with a c_of_ocaml whose
returns to the continuations bounce on the trampoline.
wasm32: the minor heap is 8M words (32 MB) by default instead of 1M,
used as the program allocates: programs allocating much spend less
time in minor collections (compiled, binary_trees 1.40 s, now 0.86 s;
hamming 0.27 s, now 0.18 s; boyer_no_exc 0.51 s, now 0.38 s), for up to
28 MB more memory. OCAMLRUNPARAM=s=... still sets it. Existing
runtimes: ocaml-emcc setup --force.
OCaml 5.1 and later: the minor heaps and the pools of the major heap
take their memory from malloc instead of emscripten's mmap, which
cleared all of it and kept a list of the mappings: a program with --threads starts some 5 ms faster with 28 MB less memory (the minor
heaps of its 8 possible domains), and a larger minor heap
(OCAMLRUNPARAM=s=) costs no memory until it is used. Existing
runtimes: ocaml-emcc setup --force.
Compiled for wasm32, under node 24 and later: main runs on a large stack of its
own (JSPI's WebAssembly.promising, enabled at startup under node 24)
instead of in a worker thread, 15 to 35 ms less at startup. Programs
with js_of_ocaml's primitives or asyncify, whose callbacks enter the
program again from node's event loop, and wasm64 ones keep the worker.
--node-fs: reading a directory (Sys.readdir, Unix.readdir,
readdir(3)) takes the types of its entries from node's readdir, without
emscripten's lstat of each entry: ocamlc compiling 160 files spends 6 %
less time (3.9 s, now 3.7 s). FIFOs and sockets have their own type
instead of a regular file's, and d_ino is not the inode of the entry.
Compiled, under node: the main thread compiles the module while the
worker with the large stack starts (some 15 ms less at startup for a
6 MB program, ocamlc).
Compiled, under node: a program linked with --cps-stack or --jspi
runs on the main thread, without the worker and its large stack (20 to
40 ms less at startup), unless $OCAML_EMCC_STACK_MB is set. Out of
stack on the main thread is an uncaught Stack_overflow too.
Compiled with -sASYNCIFY: asyncify saves the stack in 1 MB
(-sASYNCIFY_STACK_SIZE=1048576, unless given) rather than 4 KB, which
suspending under some hundred frames exceeded. Only -sASYNCIFY itself
turns off the tail calls, not its other settings (-sASYNCIFY_IMPORTS).
--no-tail-calls (tail-calls: no), compiled: without wasm tail
calls (emcc's -mtail-call), for the browsers that lack them (Safari
before 18.2, Firefox before 121, Chrome before 112), usually with --cps-stack. Long loops of tail calls (through closures,
continuations) still run in constant stack, c_of_ocaml returning the
next tail call past a few hundred nested ones to the last call which is
not one. -sASYNCIFY implies it. Its plugins follow (plugin --main, or plugin --no-tail-calls).
Compiled mode compiles the parts of the C in parallel, the biggest
first, $OCAML_EMCC_JOBS at a time (default: the number of
processors).
The dune commands of several ocaml-emcc running at the same time take
turns (a lock in the temporary directory) instead of failing ("Another
Dune instance is currently running"): the links of a project can run in
parallel.
Compiled mode leaves to c_of_ocaml what is about the code, and keeps
here what is about emscripten; it needs a c_of_ocaml with a manifest
(--manifest) and camlc_main. The primitives the code calls and the
effects it was compiled with come from c_of_ocaml's manifest instead of
its C (the marker that plugin --main reads followed --effects, not
c_of_ocaml's choice). Backtraces follow the debug events of the
bytecode on c_of_ocaml's side, which warns of integer literals beyond 31
bits itself (link only does for the interpreter). The program starts
from main.c, and JSPI's new stacks for the segmented stack come from share/runtime/jspi.c, linked with --jspi only. The C functions the
code of plugins calls are linked in the program by c_of_ocaml
(asin and acos were missing from those link --dynlink exported).
Effect handlers with --dynlink: a program whose own code performs
effects is compiled with them (double translation) without --effects,
instead of raising Effect.Unhandled (c_of_ocaml 3456533a), and plugin refuses a plugin performing effects for a program without
them, or without --main, unless --effects disabled.
--dynlink is implied when a compiled program (not a standalone .wasm) links Emcc_dynlink.
The C of an executable's own (foreign_stubs ...) is linked (its
primitives raised Failure "... not implemented").
A program with plugins exports the usual C functions of libc
(snprintf, strchr, qsort...) even when it does not call them, as
the C of its plugins may (which C the program links depends on its
primitives).
Interpreted, --dynlink loads the C stubs of bytecode plugins (OCaml's Dynlink, of a .cma naming a dllX.so), which plugin --interpreted LIB.cma -o DIR builds as wasm side modules (--main naming an
interpreted program implies it). A host dllX.so that Dynlink finds
first gives way to the wasm one of $OCAML_EMCC_STUBLIBS or the current
directory. link notes that Dynlink's plugins with C need --dynlink.
Plugins of a program linked with --threads load: plugin --main
links them with -pthread, for the program's shared memory, as does plugin --threads (they failed with "could not load dynamic lib").
The callback of Emcc_dynlink.load_url runs after main in such a
program (main running in a pthread, the program exited first).
With --jspi, the threads other than the main one load the plugins
synchronously (a thread started after a plugin is loaded failed: SuspendError, emscripten's loads suspending outside of JSPI). A
plugin's entry is read as data, its address in the plugin's
part of the table: looked up as a function, it got a slot in the
table of one thread only, and on wasm64 the next loads aborted
("table index is out of bounds").
Domains, interpreted: the interpreter's state for its re-entries (see
the ocaml recipe) is per thread. Shared, two domains entering the
interpreter at the same time could take the other's, a rare crash
(memory access out of bounds, a few runs in a thousand of tests/domains). (Sysroots built before note the change: ocaml-emcc install --force ocaml.)
--threads: the memory of a thread that ends (its stack, its TLS) is
freed once the thread no longer uses it. With --wasi, a detached
thread (OCaml's threads and domains) freed it itself while still
running on it; with emscripten's glue, the main thread could free it
while the thread was still asking it to. A thread whose memory is above
2 GB (wasm32) is given back too: its address, read as negative, was
that of no thread ("Cannot read properties of undefined (reading
'pthread_ptr')", then memory corruption, in programs starting and ending
many domains).
js_of_ocaml with --threads: a thread other than the main one calling
JavaScript raises Failure (the program crashed: JavaScript values
belong to the main thread), the GC of another thread releases
JavaScript values on the main thread, and once main returned, the
other threads go on between the callbacks (they waited forever, main
kept the runtime).
--wasi: files can be mapped (Unix.map_file, mmap), as with
emscripten's glue: a copy, written back by msync and munmap when
shared and writable (it failed with ENOSYS).
--wasi, compiled: c_of_ocaml's small functions padding the module,
which only V8's inlining heuristic needs, are left out (--disable small-functions, when c_of_ocaml has it): a smaller module.
js_of_ocaml, compiled: the property and method names of the program
(its literals) are decoded once, which halves the cost of a property
access (120 to 65 ns) or a method call (280 to 140 ns).
The sqlite3 and openblas recipes are thread-safe (SQLITE_THREADSAFE=1,
sqlite's default, and openblas's USE_LOCKING): with --threads,
threads using them at the same time no longer share their state
unprotected. (Sysroots built before note the change: ocaml-emcc install --force sqlite3 openblas.)
--node-fs, compiled: Sys.chdir changes the current directory, which
relative paths then follow (node's worker thread running the program
cannot change the process's: chdir did nothing, Rocq found none of its
libraries).
x86's SIMD and floating-point flags (-msse2, -mfpmath=sse...), which
a configure run on the host may add (rocq-runtime on wasm64), are dropped
from C compilations unless they ask for wasm SIMD (-msimd128).
build: the libraries compiled against a package rebuilt for the
target are rebuilt however many rounds it takes (at most 5 before:
netsnmp's async libraries, over core, failed with "inconsistent
assumptions").
Packages building C with their own build (make, CMake, configure, run
by a dune rule) get emcc in the wasm context under the host's names: cc, gcc, g++, c++, clang, clang++, ar and ranlib. They
were the host's, which refused emcc's flags (qbf, re2, cudd,
hdr_histogram, cucumber, bls12-381). CMake looks for their libraries in
the sysroot first (hdr_histogram's zlib; the wrappers skip directories
named like the tool, emscripten's cmake/). In the host context, those
compilers drop emscripten's flags, which a build script may take from CFLAGS (bls12-381's build_blst.sh).
As with gcc, C++ stubs may use register (clang refuses it in C++17:
gbddml), and C functions declared without a prototype are no error with -Werror (cucumber's gherkin-c).
A dll<x>.so that a package's own build makes with emcc (stem's
snowball) is replaced by a placeholder when ocamlc links an executable
of the package for the target ("Unrecognized magic").
Stub libraries a package enables for some architectures only (curve448:
amd64, arm64...) are compiled from the C files of its native build: the
wasm context's architecture is none, so dune skipped them.
ocamlc -config of the wasm32 toolchain says word_size: 32 and int_size: 31, the program's, and packages choosing their OCaml code by
them in their dune files are built for wasm32 when they are rebuilt for
the target.
Recipes: HOST_TOOLS=1 (with PATCH_OCAML=1) builds the executables
that a package's build runs for the host, in a host context, when its
OCaml libraries are rebuilt for the target (rocq-runtime's
configure.exe, with C stubs).
The C files behind the members of a native stub archive built by CMake
(x.c.o, out of the source tree) are found.
Stubs without dune or a recipe: the C files reaching the OCaml headers
through several local headers are found (orsetto), and those including
them without their directory (<mlvalues.h>, as before OCaml 3.08:
labltk's).
ctypes-foreign: the functions emscripten only has as failing stubs
(getpwnam...) are found by dlsym (opasswd stopped at startup with
"undefined symbol"), and emscripten's libraries are looked for in $EM_CACHE when it is set. The rest of the users database, which it
lacks, finds no one and writes nothing (putpwent, the shadow
passwords' getspnam..., lckpwdf).
The stubs of packages whose dune package is named otherwise
(ocaml-systemd: -p systemd) are built.
The OCaml libraries rebuilt for the target are built again once dune is
downgraded: an older dune cannot read the newer one's dune-package
files ("Version 3.24 of the dune language is not supported": re2,
async, shell...).
The OCaml libraries rebuilt for the target whose C alone failed to build
(re2's vendored C++ library, janestreet_cpuid's x86 stubs) are kept,
their stubs being built apart (with an empty archive where their dune-package names one); a library that needed rebuilding and
failed is reported as such, not as one the target does not need.
Added recipes: bls12-381 and bls12-381-signature (a 32-bit size_t),
codex (its 32-bit branches), eigen (Eigen 3.4.1's fix for recent clang),
gdbm (dbm), hdr_histogram (musl's <endian.h>), janestreet_cpuid (its
CPUID primitives fail, as in JavaScript; its flags shifted in 63 bits),
libcurl (curl), libpcre (pcre), mariadb (mysql, mariadb), mm (mm.oss
without OSS), ocaml_intrinsics (CRC-32C in software), optiml-transport
(no GCC suffix on a floating constant), owl (no x86 cpuid; ndarray
dimensions as intnat), readline, rocq-runtime (no perf counter; its
OCaml rebuilt for 31-bit integers on wasm32), shared-memory-ring (memory
barriers for wasm), shell (no disk quotas: ENOSYS), tuntap (no devices:
ENOSYS), unionFind (identifiers for 31-bit integers: frama-c on wasm32),
unisim_archisec (floating-point exceptions defined, never raised),
unix-errno (Linux's errno codes), xapi-stdext-unix (no block devices:
ENOTSUP; TCP options at IPPROTO_TCP), zmq (libzmq).
Stub archives: one a package's rule makes with cc -c -o lib<x>.a (an
object, not an archive) is put in an archive, and of two copies of an
archive (the rule's own and dune's) one is kept: radamsa's functions
were defined twice.
Stubs compiled from the C files of a package: the headers of all the
opam packages it depends on, not only directly, are found
(jane-street-headers's ocaml_utils.h for core's), and those included
with their package's directory (<posix-time/posix-time.h>:
posix-mqueue).
When dune build -x fails for a package's stubs, the archives it built
are kept, and only the others are compiled from the C files of the
native build, those that do not compile left out: libbinaryen (whose dllbinaryen.so failed) no longer builds binaryen natively for an hour,
and core's other stubs build without linux_ext's (sys/prctl.h).
The C compiler of the sysroot drops the options of GNU ld that wasm-ld
does not know (-Wl,--no-undefined, -Wl,-z,defs, -Wl,--as-needed, -Wl,--eh-frame-hdr): libbinaryen's dllbinaryen.so links.
conf-clang (the clang binary, a tool of the build: libbpf compiles its
BPF programs with it) no longer builds the clang recipe (clang's
libraries, an hour): libbpf fails at once, on its missing headers.
The libc's libraries (-lm, -lpthread...) a package asks for build no
recipe: frei0r's (whose link flag is -lm) was built for blake3, efl...
ctypes-foreign: the OpenGL functions named in the program are found by dlsym, emscripten's WebGL ones, with its OpenGL 1.x emulation
(-sLEGACY_GL_EMULATION) for glBegin and the like; those emscripten
lacks (display lists, feedback mode) abort when called (gl-legacy
stopped at startup on glBegin).
cargo build in a dune rule of the wasm32 context builds the Rust
static library for wasm32-unknown-emscripten (its standard library
from rustup), with emcc, its build scripts with the host's cc, and
copies it where the rule expects it (blake3's, which then stops on its
vendored crates: ocaml-sys's c_char is unsigned on wasm32, Rust's
signed). Without that standard library (Debian's cargo), it says so.
The C compiler of the sysroot drops -g, which dune gives every stub:
the link strips the debugging information, and clang needed more than
3 GB for some generated C++ with it (unisim_archisec's decoders: 0.9 GB
without).
POSIX message queues (mq_open...), which emscripten's libc only
declares, fail with ENOSYS instead of being undefined at link
(posix-mqueue, interpreted).
A switch without ocamlfind: building the runtime failed (Unix_error ENOENT running ocamlfind printconf) instead of using the default
findlib configuration of the switch.
--owi (owi: yes in ocaml-emcc.conf, for --wasi) links a module
for owi's symbolic execution: WASI inside the module (no files, stdout
dropped, stderr to owi's print_char), atomic instructions made plain
ones (one thread: --threads refused), and ocaml-emcc.owi
(Emcc_owi) for symbolic values, assumptions and checks. owi has to
run try_table and throw, which its release does not yet. A runtime
built before its recipe changed is a warning: an older one can take
owi seconds to start.