Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation to WebAssembly with wasi-sdk #1248

Open
rhornig opened this issue Oct 25, 2024 Discussed in #1241 · 0 comments
Open

Compilation to WebAssembly with wasi-sdk #1248

rhornig opened this issue Oct 25, 2024 Discussed in #1241 · 0 comments
Assignees
Labels
feature A feature request

Comments

@rhornig
Copy link
Contributor

rhornig commented Oct 25, 2024

Discussed in #1241

Originally posted by ansemjo October 22, 2024
Hey there,

I recently attempted to compile OMNeT++ simulations to WebAssembly (specifically the WASI "preview1" / wasip1 target) by using the wasi-sdk as a toolchain. In a first step, I tested how to compile OMNeT++ to static libraries, that don't depend on any system libraries at runtime; and that worked fine. However, since each simulation project is basically an executable that links against the OMNeT++ library, the library itself needs to be compiled to a WebAssembly format, that the wasi-sdk toolchain can then link against. And now that's where I am stuck.

Is there any interest to support WebAssembly as a compilation target in the future?

There is already zigen/omnetpp-wasm but that focuses on using Emscripten to package the entire Qt GUI on a webpage. It's amazing that this works but I'd be more interested in the standalone mode to produce WASI binaries of only the Cmdenv. Maybe I'll just need to look into this again to see if it can be repurposed.


It's been a while since I've tested this so here are some (probably incomplete) notes:

  1. To build OMNeT++ with static libraries, set in configure.user:
SHARED_LIBS=no
WITH_QTENV=no
WITH_OSG=no
WITH_OSGEARTH=no

Then run make clean && make -j$(nproc) as usual.

  1. Build your simulation as a static executable:
opp_makemake -f --deep -u Cmdenv -L $__omnetpp_root_dir/lib \
    -K CFLAGS=-static -K LDFLAGS=-static
  1. Attempt to build the simulation with the wasi-sdk:
opp_makemake -f --deep -u Cmdenv -L $__omnetpp_root_dir/lib \
    -K CFLAGS=-static -K LDFLAGS=-static \
  # use emulated signals in wasi-sdk:
    -lwasi-emulated-signal -D_WASI_EMULATED_SIGNAL \
  # do not use POSIX coroutines in omnetpp:
    -DUSE_PORTABLE_COROUTINES

But that fails because the previously compiled static library cannot be linked with WebAssembly modules.

  1. So rebuild OMNeT++ itself with the wasi-sdk. Setting CXX and CC beforehand didn't work, so I added the SDK to PATH and masked those explicitly for configure:
export PATH="$WASI_SDK_PATH/bin:$PATH"
CC= CXX= ./configure --host=wasm32 \
    CFLAGS="-D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_MMAN -D_WASI_EMULATED_GETPID -D_WASI_EMULATED_PROCESS_CLOCKS" \
    LDFLAGS="-lwasi-emulated-signal -lwasi-emulated-mman -lwasi-emulated-getpid -lwasi-emulated-process-clocks"

A small dummy enum is needed in src/common/filelock.h (reference):

  #if !defined(F_WRLCK)
  enum {
  F_WRLCK,
  F_RDLCK,
  F_GETLK,
  F_SETLK,
  F_SETLKW,
  F_UNLCK
  };
  #endif

But now running make fails due to various longjmp errors that appear to arise due to the use of exceptions. My current understanding is that you could enable experimental support for exceptions in WebAssembly but neither wasmtime nor a browser envionment currently supports that anyway. I've also seen that SQLite is linked a few times and while SQLite now has a dedicated Wasm build, doing the same for an older version that is bundled with the project is probably quite hard.

@rhornig rhornig added the feature A feature request label Oct 25, 2024
@rhornig rhornig self-assigned this Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request
Projects
None yet
Development

No branches or pull requests

1 participant