package ocaml-emcc
Build not checked
Compile OCaml programs with C stubs to WebAssembly with emscripten
Install
dune-project
Dependency
Authors
Maintainers
Sources
0.4.0.tar.gz
sha256=fa9b85d77d66d76846aaae01910ae0cd5fa714991d977b5ab9e681a40faf4534
sha512=9b8b97c4db79ea13ed32cbb3946d77bd6c9368ac54bee9de9da868b86d4403e184c63be55c53c879c0e043e86a0972bfbe6041a84902b004c1204f87ed006299
doc/CHANGES.html
0.4.0
- Recipe patches are placed by their text, not their line numbers (
share/tools/patch.awk): the old text of each hunk must occur once in the file,@@lines need no numbers, and the context only has to tell the place apart. A hunk matching no place, or several, stops the build (GNU patch applied some of ours with fuzz, at the place it guessed). Theocamlpatches lost their spare context, andpatches/5.1/andpatches/5.2+/becamepatches/5.1+/. - New recipes for packages that the default paths cannot build:
mlcuddidl(CUDD configured for the target, stubs generated by camlidl from its.idlfiles),hacl-star-raw(HACL*'s portable C with cmake, ctypes stubs printed by generators run on the host, the vectorized and Vale variants trapping) andmccs(GLPK silenced withglp_term_out: musl writes what was written to a closedstdoutonce fd 1 is back). Anocaml-luarecipe unpacks and patches the Lua it bundles, as its opam file does before dune runs (it only built from a kept build directory). Tests:cudd,hacl,mccs, andposix(posix-time2, -socket, -stat). - ctypes bindings are rebuilt for wasm64 too: emscripten's musl does not lay the libc's structs out as glibc (
struct stat'sst_sizewas glibc'sst_mtime). When a package rebuilt for the target is rebuilt again (another version), the packages depending on it are too; the builds of versions no longer installed, or made against other versions of their dependencies, are dropped first (ctypes 0.24's next to ctypes-foreign 0.22: "inconsistent assumptions"); a package depending on one that cannot be rebuilt (ctypes 0.20, built by make) keeps the host's build, andlinkwarns. A package that dune builds no library of (hacl-star-raw, built by make next to hacl-star) keeps the host's build. - A recipe built from an opam package (
SOURCE=opam:) is built again for another version of it, as dune-built stub libraries already were (recipes built before:install --force). - opam commands of the driver act on the switch being built for, not on opam's current one; the sources of a package come from its build directory or
opam source, which applies thepatches:of its opam file (not from the archive in.opam-switch/sources, which has none). - wasm64:
getaddrinfois resolved in C (compat.c): emscripten's JavaScript one throws on memory64 (BigInt(null)), so didUnix.getaddrinfo. - Stubs compiled blindly get the include directories of the package's opam dependencies (ctypes before 0.21:
ocaml_integers.h). The native build behind the archives thatdune -xbuilds with host objects is made when_build/defaultlacks them (dune -xleaves only what the target needs there). A recipe asked for a library it did not build is built again. mlgmpidl: its stubs regenerated with camlidl getMPFR_VERSION_MAJOR(apron's test failed without a kept build directory).zarith: the defines of its configure, and an emptyz_features.hbefore 1.12.- Tests:
pcre2andlz4dropped,clibskeeps checkseum and bigstringaf (the others needed nothing other tests do not cover).tests/versions.shruns tests against several versions of their package.
0.3.0
- OCaml 5.0 to 5.3: the
ocamlrecipe builds their runtime. They commit the minor heap withmmap(MAP_FIXED), which emscripten lacks ("Failed to create main domain"):patches/5.0/andpatches/5.1/commit it withmprotectas 5.2 and later do (patches/5.4+/becomespatches/5.2+/). 5.0 callspthread_getaffinity_np, which its configure only compiles (patches/5.0/), and names the otherlibs stub archives without thebytsuffix. - OCaml 5:
-pthreadis kept out of the runtime by givingPTHREAD_CFLAGS=-D_REENTRANTto configure, instead of editingMakefile.config(5.0 put it inOC_CFLAGS, which the edit missed: "no domain lock held"). - The C compiler of the wasm context drops
CPATH,C_INCLUDE_PATH,CPLUS_INCLUDE_PATHandLIBRARY_PATH: their host headers came before those of the sysroot (ctypes-foreign's stubs were compiled with the host'sffi.h: "Unexpected type 0" at the first call). - ctypes on wasm32, OCaml 5.2 and later: a library compiled against the host's ctypes (ctypes-foreign, once zstd had rebuilt ctypes) is rebuilt for wasm32 again. ocamlc quotes the files of "make inconsistent assumptions", which hid them.
- A stub library of the sysroot built from an opam package (with dune, or blindly) for another version than the installed one, after an
opam upgrade, is built again: the old one was linked (digestif 1.4's blake3 primitives undefined). - A library of the compiler missing from the runtime of the sysroot (
libthreads.a, built since) has the runtime built again, instead of failing ("libthreads.a (wasm) not found"). - ctypes on wasm32: ctypes and the packages of the program depending on it are rebuilt for wasm32 (
driver.sh shadow) into the sysroot's findlib directory, which the toolchain searches first, andbuildbuilds the bytecode against them: struct layouts,sizeofand arrays now have the sizes of wasm32, for the structs laid out at run time and for the type descriptions generated when the package is built (yaml now works on wasm32).linkanddoctorwarn when a bytecode was compiled against the host's builds, or uses a package that could not be rebuilt (packages without dune). - dune's
(ctypes)stanza (a project's, or a package's): dune links its generators natively only (ocaml/dune#13650), which the wasm context cannot. A project or package using it is built by a dune patched to link them as custom bytecode withoutocamlopt(share/tools/dune/ctypes-linkage.patch), built once per version from the sources of the switch's dune: its structs have the layouts of the target. Thebuildof a project with the stanza used to fail, and the stubs of such packages came from their native build. - The C compiler of the wasm context makes node scripts of the executables it links (
-sEXECUTABLE), for build rules to run, with the node of emscripten's configuration (memory64 for wasm64). - The
ocamlcwrapper of the toolchains no longer writes placeholderdllX.sofiles in the host's switch, next to its installed libraries. On OCaml 4.x, those ofunixandstrinlib/ocaml(inld.conf) hid the real ones: the host'socaml unix.cmaandocamlc unix.cmafailed (The external function unix_waitpid is not available). A switch wherebuildran before keeps them: delete the emptydll*.sooflib/outside thestublibsdirectories. --wasi(link,build):OUT.wasmis a WASI module, importing only WASI functions, for wasmtime, node's WASI and other WASI hosts (wasm32). emscripten's standalone libc fails to open orstata file and imports the rest of the file system from JavaScript:share/runtime/wasi.cimplements these system calls with WASI (preopened directories, a current directory), sleeping,selectandgetrusagewithpoll_oneoffand the WASI clocks,dup/dup2of an opened file by opening it again, file locks and owners as no-ops. Linked with-sPURE_WASI, the module is translated to the standard exception handling instructions (exnref) that WASI hosts implement.tests/run.sh --wasiruns the suite this way (wasmtime or node's WASI), and CI does on 4.14 and 5.5.- A standalone
OUT.wasm(with or without--wasi) has its bytecode as C arrays started bycaml_startup_code, instead of a file of emscripten's JavaScript file system, which it does not have.linklists the imports that are not WASI. - The functions of
share/runtime/*.c(compat.c,dlsym.c,wasi.c) that the program's libraries define as well are left out: the program's are linked. ocamlrecipe: on wasm32, the integer literals beyond 31 bits are wrapped bycaml_startup_codeas well; the debug events of a program started bycaml_startup_codecan come from memory (backtraces of standalone modules have their locations). A runtime built before needsocaml-emcc setup --force(linksays so).- Recipes change the sources they build with patches (
patches/next torecipe.sh) instead ofsedandperl: ocaml, libffi, liblmdb, lmdb, lwt, mirage-crypto, cryptokit, secp256k1-internal. Theocamlones apply to the sources of every version from 4.08 to 5.5 (on 5.0 to 5.3, the wasm32 build used to stop on the DATA literals). A recipe's checksum covers its patches: the "recipe changed" note shows when they change.
0.2.1
- Installed packages configured against a host library: in the wasm context of
dune -x,llvm-configis theclangrecipe's (aspkg-configalready was the sysroot's), and-lclang-cpp,-lclangX,-lLLVM-<major>build theclangrecipe, which links the LLVM libraries with-lclang-cpp. A package binding clang's C++ API, installed with opam against the host's LLVM, builds without configuration; its stubs used to be compiled against the host's LLVM headers. Recipes declare such names withLIB_PATTERNS=. Aclangrecipe built before needsocaml-emcc install --force clang. - The
mpfrrecipe installsmpfr.pc:pkg-config mpfrfailed in the wasm context, and packages asking it fell back to compiling their C files without their own flags. tests/depgraph: an installed package (mpfr throughpkg-config, zarith) whose whole dependency graph is built from the program.
0.2.0
Tried on FFI-heavy packages (mirage-crypto, lwt, cryptokit, minisat, ocaml-lua, stb_image, extunix, lz4, snappy, argon2, ocaml-expat, lmdb, glpk, fftw3, secp256k1-internal, tsdl...); fixes for what broke.
- wasm32: integer literals beyond 31 bits in the bytecode's DATA section are wrapped to 31 bits instead of aborting the program at startup (optint, hence checkseum and decompress, holds such literals in its 32-bit code).
- Node: the standard streams are the process's, as bytes (binary output and input were decoded as UTF-8, NUL bytes dropped).
- C++ stubs and libraries with exceptions (
-fwasm-exceptions). -pthreaddropped from the flags of stubs (lwt):caml_statebecame a thread-local variable there.- wasm64:
caml_ba_alloc_dimsgets its dimensions asintnat(stubs passingints corrupted the heap); libffi writes widened return values at their address (ctypes-foreign calls returningintread 0). linkshows the C functions whose calls wasm-ld turned into traps (signature mismatches between C files), it used to hide them.dune -x: the tools of the host next to the toolchain'socamlc(%{ocaml}...);pkg-configonly shows the sysroot's libraries to the wasm context; C tests see the sysroot's headers and libraries; archives holding host objects (a vendored library built bymake) are rebuilt from the native build's C files; a kept_buildof another tool is left alone; a package that produced no archive is not marked as built.- Packages without dune: the C files behind the native build's archive.
linkbuilds the libraries named by-lXin.cmafiles (custom-only libraries such as stb_image, C libraries bound with ctypes-foreign).- System recipes are also found from the
-lXof a package's.cmafiles. - New recipes: liblz4, snappy, expat, fftw3, glpk, libargon2, liblmdb, sdl2; mirage-crypto (-ec, -rng), lwt, cryptokit, secp256k1-internal, lmdb.
0.1.0
First release.
ocaml-emcc build OUT.js: a dune executable and the C stubs of its dependencies, compiled to WebAssembly with emscripten (wasm32 or wasm64).- wasm OCaml runtime rebuilt from the sources of the switch's compiler, OCaml 4.08 to 5.5.
- Step-by-step commands (
setup,dune,deps,link,doctor...) for projects without dune. - Recipes for C libraries: gmp, mpfr, zlib, sqlite3, libffi, libbz2, libpcre2-8, zstd, gsl, camlidl, mlgmpidl, apron, clang; project recipes in
emcc-recipes/. - ctypes and ctypes-foreign support.