Skip to content

Instantly share code, notes, and snippets.

@cjxgm
Last active February 23, 2021 05:21
Show Gist options
  • Save cjxgm/15ae90b982ba86c147330894404f8f7d to your computer and use it in GitHub Desktop.
Save cjxgm/15ae90b982ba86c147330894404f8f7d to your computer and use it in GitHub Desktop.
Rust + WebAssembly Guide, on Arch Linux

Rust + WebAssembly Guide, on Arch Linux

Motivation

It's just too complex to make Rust+WASM work on macOS, but all the other guides online target macOS. This guide targets only Arch Linux. If your favorate distro is great, you should be able to do the same thing.

Setup

Install rustup and emscripten with the distro's native package manager. Then install stable Rust and wasm target with Rustup.

# pacman -S rustup emscripten
$ rustup update stable
$ rustup target add wasm32-unknown-emscripten

Now, logout and re-login to make /etc/profile.d/emscripten.sh in effect.

Compile & Run

$ cargo init --bin hello
$ cd hello
$ rustc --target=wasm32-unknown-emscripten src/main.rs -o main.html
$ emrun main.html   # Spin up a server and open your default browser

The rustc step may fail, if you are using Emscripten Port version 37. This will be fixed in future releases of Emscripten Port, which means, you may need to follow the error messages and patch the code yourself according to WebAssembly/binaryen#1196.

@jeanlescure
Copy link

After trying out these steps (and failing), I found that there is an official rustwasm team that maintains a tool called wasm-pack which works quite well out of the box on Arch Linux.

Moreover, as of 2020 Emscripten is on version 2 and latest Rust only plays well with version 1.39.0 of Emscripten. Due to this it is not a viable option on Arch Linux, as pacman only lists latest Emscripten versions and getting the older version is non-trivial to the point of being virtually impossible to implement in any sane dev environment.

@cjxgm
Copy link
Author

cjxgm commented Feb 23, 2021

This was written years ago and things have changed. You can use many tools out there (like wasm-pack or cargo-wasi) now.
Sadly, Gist does not support archiving like a real GitHub repo does, so this doc will stay here as is.

@jeanlescure
Copy link

No worries, cheers 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment