So it’s a bit rough (especially the
std parts, I mutilated the library a little bit), but it works!
A hosted example: http://bl.ocks.org/AerialX/1041460cb9dd5876658c
Basically, a light Cargo wrapper is used to do all the heavy lifting. To get this all working, compile the
cargo-build project with cargo (make sure to provide LLVM_PREFIX), then follow the instructions to build the std variant for the i386-unknown-emscripten target, place it in a rustc sysroot hierarchy, then use cargo-build to compile projects using it.
For more details… A flexible target spec is used to tell rustc to build i386 with emscripten’s data layout, so it actually generates rlibs with x86 + LLVM bitcode for LTO purposes. The final binary linking step then uses LTO to spit out LLVM IR, so all dependencies get pulled in as one bitcode file. Then I run a hacky string replace over the IR to get LLVM 3.5 to accept it, and some optimization passes over it to get rid of llvm.assume. The cargo wrapper just exists to handle all this for us, since it’s usually not flexible enough to control what arguments are passed on to rustc. Then
std is changed slightly to pull out most of the native parts, mainly threading and unwind functionality - sync primitives are stubbed out.
Looking toward the future…
- Proper support for native dependencies. These will probably work well enough right now, but need to be manually linked in with
emccafter Cargo has generated the LLVM IR.
- Piston support! The existing SDL+OpenGL backends hopefully won’t be too hard to get working.
- Ideally rustc would be able to just store/create/link bitcode in the rlib. Assembling doesn’t always make sense for platforms like Emscripten.
- Bring Emscripten (and its PNaCl passes) over to LLVM 3.6. This is all a hack that just happens to work, and it’d be much nicer if we didn’t have to worry about the incompatibilities between the two versions.
- Make emscripten an actual supported platform in
std. Decide on a decent strategy for it and consider what to do with the system components that don’t apply to it.