My First Wasmtime Experience in Windows 10

A month ago, Mozilla announced WASI (WebAssembly system interface) in their blog post. WASI is a new standardization that allows WebAssembly code to run anywhere. Although WASI is still a work in progress prototype, I can’t wait to try it out.

Building Wasmtime in Windows 10

Wasmtime is a standalone wasm-only runtime for WebAssembly which is an open source project released on GitHub.

To use Wasmtime, we have to build by ourselves. Download the source code:

git clone --recurse-submodules https://github.com/CraneStation/wasmtime.git

To build Wasmtime, you need cargo, clangand cmake.

Let’s try to build it via cmd.exe:

cargo build --release

Unfortunately, I got the error message:

Done Building Project "G:\wasi\wasmtime\target\release\build\wasmtime-wasi-628aff801f2e5951\out\build\SandboxedSystemPrimitives.vcxproj" (default targets) -- FAILED.
Done Building Project "G:\wasi\wasmtime\target\release\build\wasmtime-wasi-628aff801f2e5951\out\build\ALL_BUILD.vcxproj" (default targets) -- FAILED.
Done Building Project "G:\wasi\wasmtime\target\release\build\wasmtime-wasi-628aff801f2e5951\out\build\install.vcxproj" (default targets) -- FAILED.

Build FAILED.

"G:\wasi\wasmtime\target\release\build\wasmtime-wasi-628aff801f2e5951\out\build\install.vcxproj" (default target) (1) ->
"G:\wasi\wasmtime\target\release\build\wasmtime-wasi-628aff801f2e5951\out\build\ALL_BUILD.vcxproj" (default target) (3) ->
"G:\wasi\wasmtime\target\release\build\wasmtime-wasi-628aff801f2e5951\out\build\SandboxedSystemPrimitives.vcxproj" (default target) (4) ->
(ClCompile target) ->
  g:\wasi\wasmtime\wasmtime-wasi\sandboxed-system-primitives\src\str.c(22): warning C4047: 'initializing': 'char *' differs in levels of indirection from 'int' [G:\wasi\wasmtime\target\release\build\wasmtime-wasi-628aff801f2e5951\out\build\SandboxedSystemPrimitives.vcxproj]


"G:\wasi\wasmtime\target\release\build\wasmtime-wasi-628aff801f2e5951\out\build\install.vcxproj" (default target) (1) ->
"G:\wasi\wasmtime\target\release\build\wasmtime-wasi-628aff801f2e5951\out\build\ALL_BUILD.vcxproj" (default target) (3) ->
"G:\wasi\wasmtime\target\release\build\wasmtime-wasi-628aff801f2e5951\out\build\SandboxedSystemPrimitives.vcxproj" (default target) (4) ->
(ClCompile target) ->
  g:\wasi\wasmtime\wasmtime-wasi\sandboxed-system-primitives\src\posix.c(16): fatal error C1083: Cannot open include file: 'sys/ioctl.h': No such file or directory [G:\wasi\wasmtime\target\release\build\wasmtime-wasi-628aff801f2e5951\out\build\SandboxedSystemPrimitives.vcxproj]
  g:\wasi\wasmtime\wasmtime-wasi\sandboxed-system-primitives\src\random.c(15): fatal error C1083: Cannot open include file: 'pthread.h': No such file or directory [G:\wasi\wasmtime\target\release\build\wasmtime-wasi-628aff801f2e5951\out\build\SandboxedSystemPrimitives.vcxproj]

    1 Warning(s)
    2 Error(s)

The missed header files are only available in Linux. Wasmtime doesn’t yet support Windows according to the feedback from Mozilla’s developer:

fail to build wasmtime in windows

Alright, I opted Windows Subsystem for Linux (WSL) as my testing Linux OS.

If you don’t have Cargo installed and try running ‘cargo’ in the terminal, you will get the prompt:

Command 'cargo' not found, but can be installed with:

sudo apt install cargo 

Let’s run the command to see what will happen next.

Once Cargo installed, we can get started to build Wasmtime. However, it’s not as easy as I expected. There’s a new error:

error[E0603]: macro `regex` is private

I checked my cargo version:

cargo -V

It’s 1.31, whereas the latest Cargo version is 1.34. Maybe the error will be fixed by updating the Cargo version. The recommended installation is to use rustup:

curl https://sh.rustup.rs -sSf | sh

Note: it will fail if you have an old version installed:

info: downloading installer
error: it looks like you have an existing installation of Rust at:
error: /usr/bin
error: rustup cannot be installed alongside Rust. Please uninstall first
error: if this is what you want, restart the installation with `-y'
error: cannot install while Rust is installed

Remove Rust and run the installation command again:

sudo apt remove cargo rustc
sudo apt autoremove
curl https://sh.rustup.rs -sSf | sh

It didn’t work. The issue has been submitted to GitHub. The right way is to download the rustup-init.sh and then run:

RUSTUP_INIT_SKIP_PATH_CHECK=yes sh ./rustup-init.sh

After updating Cargo to the latest version, I could successfully build the Wasmtime.

Running Wasm File in Both Command Line Tool and Web Browser

Create a ‘hello world’ project:

cargo new --bin helloworld

Install Rust nightly and enable WASI support:

rustup toolchain install nightly
rustup target add wasm32-unknown-wasi --toolchain nightly

Build the project:

cargo +nightly build --target wasm32-unknown-wasi

Run the app:

<wasmtime repository root directory>/target/release/wasmtime  <hello world project root directory>/target/wasm32-unknown-wasi/debug/helloworld.wasm

run wasi wasmtime in command line tool

We can also run the app in browser polyfill:

wasi polyfill web browser

Reference