Wasienv: WASI Development Toolchain for C/C++
At Wasmer we have been porting a lot of C and C++ projects to WebAssembly and WASI, as we believe WebAssembly will emerge as the standard way to use third-party code from any programming language securely and easily.
WebAssembly System Interfaces (WASI) is an exciting new specification that allows running POSIX-like applications anywhere, safely and securely with WebAssembly.
We realized that compiling already existing C/C++ projects to WASI was much more challenging than we expected. This is because of two main reasons:
- It’s not trivial to install and use the WASI SDK
- It’s quite hard to port existing projects to WASI as it requires a tighter integration with all configuration and make tools
Inspired by these challenges we have been working non-stop on improving the tooling so it’s easier for anyone to use and adopt WebAssembly and WASI!
Today we are very happy to announce
wasienv
! 🎉
We aim for wasienv
to be the version manager and ergonomic driver that empowers developers and companies for targeting WebAssembly WASI easily. Similar to what rbenv
and pipenv
are for Ruby and Python.
🤩 Getting Started with Wasienv
You can install wasienv
onto your system with this command:
curl https://github.com/wasienv/wasienv/raw/master/install.sh -L| sh
Note:
wasienv
currently only works in macOS and Linux, support for Windows coming soon. It also requirespip
to be installed in your system.
You can also install wasienv
from source like this:
pip install wasienv --upgrade
wasienv install-sdk unstable
Once installed, the following commands will be available in your shell:
wasicc
/wasic++
: The Clang compilerwasiconfigure
: A tool to wrap./configure
commands with wasienvwasimake
: A tool to wrapcmake
andmake
commandswasild
: the WebAssembly linker
Now that we know the commands that we have now available, let’s get into the weeds. Let’s start creating an example.c
file!
Now, we should be ready to compile it to WebAssembly and WASI with wasicc
:
$ wasicc example.c -o example
We also made a Wasienv Docker Image to make it super easy to target WASI in any environment. Check more here!
This command, will automatically create two files:
example
: an executable file that runs the Wasm file using a standalone server-side WASI runtimeexample.wasm
: the WebAssembly file
Now, you can run the example file in your shell!
$ ./example Peter# The previous command is equivalent to:$ wasmer run example.wasm -- Peter
Now, you can use the generated example.wasm
as you please:
- 📦 Publish a new package to WAPM — Bonus: the command will be automatically available in the WebAssembly shell!
- 🛳 Ship a universal binary that will work on any platform
- 🚀 Execute your WASI Module in a browser (or inside your Node app!) with the NPM package
@wasmer/wasi
!
We have created some documentation to showcase how you can use
wasienv
in your CMake or Autoconf project!
How wasienv compares to Emscripten?
Emscripten is an incredibly useful tool. It was the precursor to asm.js and WebAssembly, and pioneered bringing already-existing C/C++ projects to the Browsers.
As such, they focused on fast iteration, small “runtime glue” code size, and a tight coupling between runtime and compiled .wasm
code.
However, for executing WebAssembly server-side there’s another set of tradeoffs to take into account:
- A stable ABI so the WebAssembly runtime can work with it on the long term
- A sandbox on top of the exposed ABI calls
We realized that Emscripten introduced the perfect ergonomics for porting C/C++ projects to the Browser. We wanted to bring the same ergonomics to server-side WebAssembly WASI modules for the WebAssembly Community.
Thanks!
We are excited to see what the WebAssembly Community will build with this.
💬 Feel free to reach out and share your projects!
Hope you find wasienv
useful for making your C and C++ projects compatible with WASI, so you can run them anywhere! 🎉