If you want to compile WebAssembly modules using LLVM, several tools need to be installed and configured. Getting these tools working together correctly can be an arduous and time-consuming process. Fortunately, someone went through the trouble of making this process much simpler. Daniel Wirtz created an npm package named webassembly (https://www.npmjs.com/package/webassembly) that can perform the following operations (with the corresponding CLI commands):
- Compile C/C++ code to a WebAssembly module (wa compile)
- Link multiple WebAssembly modules to one (wa link)
- Decompile a WebAssembly module to text format (wa disassemble)
- Assemble WebAssembly text format to a module (wa assemble)
The library is using Binaryen, Clang, LLVM, and additional LLVM tools behind the scenes. We'll install this package globally to ensure we have access to the wa command. To install, open a terminal instance and run the following command:
npm install -g webassembly
It may take a few minutes to install any required dependencies. Once complete, run the following command to validate the installation:
wa
You should see the following in terminal:
You should be ready to start compiling Wasm modules. Let's move on to the example code.