feat(wasi): add wasm32-wasi as a compile target #13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Supersedes #9
This PR adds support for cross-compiling to wasm32-wasi as a CMake target. It builds on top of the
wasm32-wasitoolchain here, and more accurately, wasi-sdk.The example contained in this PR exposes a WebAssembly interface following the reactor model. In other words, it is an example module that exposes functions that can be called in reaction to events on the host.
Usage information from README:
If you wish to expose parts of your library as a WebAssembly module, you can add the
extern "C" __attribute__((export_name("<my_function_name>")))annotation to any function you wish to expose insrc/wasm/interface.cpp, and compile using thewasm32-wasi-clangpreset. This will generate a minimal WebAssembly binary exposing your exported functions atbuild/<PRESET>/src/wasm/<Debug|Release|RelWithDebInfo>/lib<PROJECT_NAME>.wasm. This binary conforms to the WASI WebAssembly standard, so it can be utilized in any WASI-supporting runtime likewasmer.ioor wasmtime.A WebAssembly version of your CLI will also be available:
Note
Exposed WebAssembly functions currently only accept parameters and return values of numerical type. This is how core WebAssembly files work, and without additional glue-code or binding generators like wit-bindgen, complex data-types cannot be sent over the runtime's ABI boundary. Complex data must be transferred via pointer and serialized bytes in exported/shared memory.
Acknowledgements:
stack_tand signal support, which has not yet been implemented in wasi-sdk. A custom port for this library is loaded from https://github.com/rioam2/vcpkg-wasm32-wasidup, which is not implemented in libc, and therefore wasi-sdk. As such, a custom port for this library is loaded from https://github.com/rioam2/vcpkg-wasm32-wasifmtandcatch2with wasm32-wasi that has not been upstreamed.