Skip to content

Instantly share code, notes, and snippets.

@badboy
Last active February 28, 2017 04:05
Show Gist options
  • Save badboy/2086757d09b7019e9f4ec8e98ee17054 to your computer and use it in GitHub Desktop.
Save badboy/2086757d09b7019e9f4ec8e98ee17054 to your computer and use it in GitHub Desktop.

Build Rust with Emscripten support

For more up2date information: http://www.hellorust.com/emscripten/

Emscripten is the LLVM-based project to compile stuff into JavaScript. Luckily, Rust already uses LLVM and just recently got an upgrade to the used version of LLVM. Now emscripten uses a fork of LLVM, which is on the way to use more or less the same base LLVM as Rust. See the next-merge branch.

Simple 13 step recipe to build Rust with Emscripten support

To get it working is still a bit of work, so here are instructions to build it from scratch:

  1. curl -O https://s3.amazonaws.com/mozilla-games/emscripten/releases/emsdk-portable.tar.gz
  2. tar -xzf emsdk-portable.tar.gz
  3. source emsdk_portable/emsdk_env.sh
  4. emsdk update
  5. emsdk install sdk-incoming-64bit
  6. emsdk activate sdk-incoming-64bit
  7. cd ~/src
  8. git clone https://github.com/rust-lang/rust && cd rust
  9. ./configure --enable-rustbuild --target=asmjs-unknown-emscripten,wasm32-unknown-emscripten
  10. python2 src/rust/src/bootstrap/bootstrap.py
  11. echo 'fn main() { println!("Hello, Emscripten!"); }' > hello.rs
  12. build/x86_64-unknown-linux-gnu/stage2/bin/rustc --target=asmjs-unknown-emscripten hello.rs
  13. node hello.js

Rustup usage

If you are using rustup, you can link the newly compiled rustc for easy usage:

rustup toolchain link emscripten ~/src/rust/build/x86_64-unknown-linux-gnu/stage2
rustup default emscripten
@saghm
Copy link

saghm commented Aug 9, 2016

Hey, I found this while googling something, and I wanted to try it out! Unfortunately, I'm running into issues during step 17 (running the bootstrap script); it seems to be having trouble finding the commit of the llvm submodule to check out. I'm wondering if the error has something to do with missing arguments to ./configure? Nothing from the emsdk section of the instructions (1-13) seems to be used afterwards. I tried doing ./configure --enable-rustbuild --target=asmjs-unknown-emscripten --llvm-root=$DIR_CONTAINING_EMSDK/emsdk_portable/clang/fastcomp/build_incoming_64 --disable-llvm-version-check, but that doesn't work either, as the code in src/rustllvm fails immediately when looking for header files in the fastcomp directory.

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