Skip to content

Build prebuilt wasm modules for external C code - #84

Merged
sjoelund merged 1 commit into
masterfrom
wasm-externals
Oct 2, 2026
Merged

sjoelund merged 1 commit into
masterfrom
wasm-externals

Conversation

@sjoelund

@sjoelund sjoelund commented Oct 2, 2026

Copy link
Copy Markdown
Member

The wasm-jit target of omc links external "C" code as WebAssembly modules, and an omc without a C compiler (the one in a browser) has no other way to run it. The new build-wasm command prebuilds them for every version of every indexed library: omc's getExternalFunctions describes a library's external functions, and the package manager compiles their Include sources with call wrappers from their C signatures, plus the Library names a repos.json wasm recipe covers (sources, flags, forced headers, sources kept private, files configured as CMake's configure_file does). C++ sources compile against libc++ with WebAssembly exceptions.

Everything is built against the package manager's own libc, shipped with each bundle as one libc is loaded per simulation, and for a fixed set of wasm features that wasmtime, wasmer and V8 all run. With the toolchain these make a generation: omc keeps generations side by side and runs a model with the newest one all its libraries have, so a new libc or toolchain is a new generation and every bundle rebuilt, with no change to omc. The libc's sysroot is published as the index's wasmToolchain, which omc installs to compile a model's own Include sources with, and libc++'s headers and libc++.so as its optional cxx part, for C++ code.

System libraries come from scripts under wasm/, once each: as modules of their own (libc++, CPython, netCDF-C, ModelicaSDF, ExternalMediaLib) or as archives with hidden symbols linked into each library using them (HDF5, zlib), so two libraries' HDF5 versions never clash.

A version's modules are zipped as <Library>-wasm32-wasip1-<hash>.zip, the hash of what they were built from, so versions whose C code did not change share one zip-file; a library without external "C" code gets none. wasmdata.json records what was built, including the functions that did not compile, and a version is built again when its source, its recipe or omc's loader interface changes. genindex adds a wasm entry per version and the top-level systemLibraries and wasmToolchain.

Library What it gets
Modelica the MSL's C libraries of the latest release, for each
Buildings ModelicaBuildingsPython_3_8 over CPython 3.14
ExternalMedia ExternalMediaLib 4.1.1 with CoolProp 6.7.0
ExternData the ED_* libraries and their parsers, private HDF5
NcDataReader2 ncDataReader2 over netCDF-C 4.9.3
SDF ModelicaSDF 0.4.5, private HDF5 and matio
SystemDynamics prototypes its sources up to 2.1.2 do not include

libc++ is LLVM 21's, with libc++abi and libunwind in one libc++.so; libunwind needs llvm/llvm-project#168449 and no register assembly on wasm. Both CMake projects of ExternalMedia assume a native platform, so its sources and CoolProp's are compiled directly, CoolProp told it is on POSIX. Buildings' pythonWrapper.c calls
pythonExchangeValuesNoModelica without a prototype, which does not link in wasm, so its entry forces one in. WASI has no dup, mkdtemp or mktemp, which the MSL, ExternData and matio get from forced headers, and no clock without the emulated process clocks, which CoolProp gets privately. Buildings' FFD needs threads and is left out.

The Jenkins image moves to Ubuntu resolute with omc from OpenModelica's nightly packages, which has getExternalFunctions once the OpenModelica change adding it is in, also serving updateinfo through OMPython, clang/wasm-ld 21 with the wasm32 compiler-rt, CMake and Python 3.14 for the CPython cross-build; the cache stage copies the zip-files to libraries.openmodelica.org/precompiled/wasm32-wasip1.

Assisted-by: Claude Opus 5.5

@sjoelund
sjoelund enabled auto-merge (squash) October 2, 2026 20:29
The wasm-jit target of omc links external "C" code as WebAssembly
modules, and an omc without a C compiler (the one in a browser) has no
other way to run it. The new `build-wasm` command prebuilds them for
every version of every indexed library: omc's `getExternalFunctions`
describes a library's external functions, and the package manager
compiles their `Include` sources with call wrappers from their C
signatures, plus the `Library` names a repos.json `wasm` recipe covers
(sources, flags, forced headers, sources kept private, files configured
as CMake's `configure_file` does). C++ sources compile against libc++
with WebAssembly exceptions.

Everything is built against the package manager's own libc, shipped
with each bundle as one libc is loaded per simulation, and for a fixed
set of wasm features that wasmtime, wasmer and V8 all run. With the
toolchain these make a generation: omc keeps generations side by side
and runs a model with the newest one all its libraries have, so a new
libc or toolchain is a new generation and every bundle rebuilt, with no
change to omc. The libc's sysroot is published as the index's
`wasmToolchain`, which omc installs to compile a model's own `Include`
sources with, and libc++'s headers and `libc++.so` as its optional
`cxx` part, for C++ code.

System libraries come from scripts under `wasm/`, once each: as modules
of their own (libc++, CPython, netCDF-C, ModelicaSDF, ExternalMediaLib)
or as archives with hidden symbols linked into each library using them
(HDF5, zlib), so two libraries' HDF5 versions never clash.

A version's modules are zipped as `<Library>-wasm32-wasip1-<hash>.zip`,
the hash of what they were built from, so versions whose C code did not
change share one zip-file; a library without external "C" code gets
none. wasmdata.json records what was built, including the functions
that did not compile, and a version is built again when its source,
its recipe or omc's loader interface changes. `genindex` adds a `wasm`
entry per version and the top-level `systemLibraries` and
`wasmToolchain`.

| Library        | What it gets                                           |
|----------------|--------------------------------------------------------|
| Modelica       | the MSL's C libraries of the latest release, for each  |
| Buildings      | `ModelicaBuildingsPython_3_8` over CPython 3.14        |
| ExternalMedia  | ExternalMediaLib 4.1.1 with CoolProp 6.7.0             |
| ExternData     | the `ED_*` libraries and their parsers, private HDF5   |
| NcDataReader2  | `ncDataReader2` over netCDF-C 4.9.3                    |
| SDF            | ModelicaSDF 0.4.5, private HDF5 and matio              |
| SystemDynamics | prototypes its sources up to 2.1.2 do not include      |

libc++ is LLVM 21's, with libc++abi and libunwind in one `libc++.so`;
libunwind needs llvm/llvm-project#168449 and no register assembly on
wasm. Both CMake projects of ExternalMedia assume a native platform, so
its sources and CoolProp's are compiled directly, CoolProp told it is
on POSIX. Buildings' `pythonWrapper.c` calls
`pythonExchangeValuesNoModelica` without a prototype, which does not
link in wasm, so its entry forces one in. WASI has no `dup`, `mkdtemp`
or `mktemp`, which the MSL, ExternData and matio get from forced
headers, and no `clock` without the emulated process clocks, which
CoolProp gets privately. Buildings' FFD needs threads and is left out.

The Jenkins image moves to Ubuntu resolute with omc from OpenModelica's
nightly packages, which has `getExternalFunctions` once the OpenModelica
change adding it is in, also serving updateinfo through OMPython,
clang/wasm-ld 21 with the wasm32 compiler-rt, CMake and Python 3.14 for
the CPython cross-build; the `cache` stage copies the zip-files to
libraries.openmodelica.org/precompiled/wasm32-wasip1.

Assisted-by: Claude Opus 5.5
@sjoelund
sjoelund merged commit 3a27c09 into master Oct 2, 2026
4 checks passed
@sjoelund
sjoelund deleted the wasm-externals branch October 2, 2026 20:43
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