Wasm builder is a utility for building a project as a Wasm binary

The Wasm builder is a tool that integrates the process of building the WASM binary of your project into the main cargo build process.

Project setup

A project that should be compiled as a Wasm binary needs to:

  1. Add a build.rs file.
  2. Add wasm-builder as dependency into build-dependencies.

The build.rs file needs to contain the following code:

```rust use wasm_builder::WasmBuilder;

fn main() { WasmBuilder::new() // Tell the builder to build the project (crate) this build.rs is part of. .withcurrentproject() // Make sure to export the heap_base global, this is required by Tetcore .exportheapbase() // Build the Wasm file so that it imports the memory (need to be provided by at instantiation) .import_memory() // Build it. .build() } ```

As the final step, you need to add the following to your project:

rust include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

This will include the generated Wasm binary as two constants WASM_BINARY and WASM_BINARY_BLOATY. The former is a compact Wasm binary and the latter is the Wasm binary as being generated by the compiler. Both variables have Option<&'static [u8]> as type.

Features

Wasm builder supports to enable cargo features while building the Wasm binary. By default it will enable all features in the wasm build that are enabled for the native build except the default and std features. Besides that, wasm builder supports the special runtime-wasm feature. This runtime-wasm feature will be enabled by the wasm builder when it compiles the Wasm binary. If this feature is not present, it will not be enabled.

Environment variables

By using environment variables, you can configure which Wasm binaries are built and how:

Each project can be skipped individually by using the environment variable SKIP_PROJECT_NAME_WASM_BUILD. Where PROJECT_NAME needs to be replaced by the name of the cargo project, e.g. node-runtime will be NODE_RUNTIME.

Prerequisites:

Wasm builder requires the following prerequisites for building the Wasm binary:

If a specific rust nightly is installed with rustup, it is important that the wasm target is installed as well. For example if installing the rust nightly from 20.02.2020 using rustup install nightly-2020-02-20, the wasm target needs to be installed as well rustup target add wasm32-unknown-unknown --toolchain nightly-2020-02-20.

License: Apache-2.0