How can I use rust-sdl2 with emscripten?


#1

Having seen compiling-to-the-web-with-rust-and-emscripten I decided to hack a little game in Rust. I’ve installed rustc 1.15.0-nightly (1c448574b 2016-11-28) and have done another preparation steps from the link above. Having that done I was able to run my Rust code in the browser. Then I tried to add rust-sdl2 crate and write something more related to computer games. But it doesn’t look like a working joint for now. cargo build --target asmjs-unknown-emscripten gives me the next error:

error: linking with `emcc` failed: exit code: 1
  |
  = note: "emcc" "-L" "/Users/hoster/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/asmjs-unknown-emscripten/lib" "/Users/hoster/projects/gwar/target/asmjs-unknown-emscripten/debug/deps/gwar-4bbeb8b6e0396b50.0.o" "-o" "/Users/hoster/projects/gwar/target/asmjs-unknown-emscripten/debug/deps/gwar-4bbeb8b6e0396b50.js" "-Wl,--gc-sections" "-nodefaultlibs" "-L" "/Users/hoster/projects/gwar/target/asmjs-unknown-emscripten/debug/deps" "-L" "/Users/hoster/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/asmjs-unknown-emscripten/lib" "-Wl,-Bstatic" "-Wl,-Bdynamic" "/Users/hoster/projects/gwar/target/asmjs-unknown-emscripten/debug/deps/libsdl2-a3439967d6cd896a.rlib" "/Users/hoster/projects/gwar/target/asmjs-unknown-emscripten/debug/deps/libsdl2_sys-6a6cdeed38de6269.rlib" "/Users/hoster/projects/gwar/target/asmjs-unknown-emscripten/debug/deps/liblazy_static-5e13a0eed191ed30.rlib" "/Users/hoster/projects/gwar/target/asmjs-unknown-emscripten/debug/deps/librand-35167bebb3b301e7.rlib" "/Users/hoster/projects/gwar/target/asmjs-unknown-emscripten/debug/deps/liblibc-09f884e06faabf22.rlib" "/Users/hoster/projects/gwar/target/asmjs-unknown-emscripten/debug/deps/libnum-351b1cddc85503c1.rlib" "/Users/hoster/projects/gwar/target/asmjs-unknown-emscripten/debug/deps/libnum_iter-618824a5957aaa8f.rlib" "/Users/hoster/projects/gwar/target/asmjs-unknown-emscripten/debug/deps/libnum_integer-9eb11829964f3c6e.rlib" "/Users/hoster/projects/gwar/target/asmjs-unknown-emscripten/debug/deps/libnum_traits-b3505e86c388a8aa.rlib" "/Users/hoster/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/asmjs-unknown-emscripten/lib/libstd-17342542cc541012.rlib" "/Users/hoster/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/asmjs-unknown-emscripten/lib/libpanic_unwind-f78756b576499725.rlib" "/Users/hoster/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/asmjs-unknown-emscripten/lib/libunwind-11f7709e0c71505b.rlib" "/Users/hoster/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/asmjs-unknown-emscripten/lib/librand-46ed9b788a6928f6.rlib" "/Users/hoster/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/asmjs-unknown-emscripten/lib/libcollections-e32369d7fef31fbf.rlib" "/Users/hoster/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/asmjs-unknown-emscripten/lib/liballoc-24699c1ddb055eb0.rlib" "/Users/hoster/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/asmjs-unknown-emscripten/lib/librustc_unicode-844a33a197b559a5.rlib" "/Users/hoster/projects/gwar/target/asmjs-unknown-emscripten/debug/deps/libbitflags-da52b57722b7b0f5.rlib" "/Users/hoster/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/asmjs-unknown-emscripten/lib/liballoc_system-3e467e865c8fa572.rlib" "/Users/hoster/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/asmjs-unknown-emscripten/lib/liblibc-60365c932e50e382.rlib" "/Users/hoster/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/asmjs-unknown-emscripten/lib/libcore-d9873b515905cac5.rlib" "/Users/hoster/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/asmjs-unknown-emscripten/lib/libcompiler_builtins-e428224f6caf212a.rlib" "-l" "SDL2" "-l" "c" "-s" "ERROR_ON_UNDEFINED_SYMBOLS=1"
  = note: WARNING:root:emcc: cannot find library "SDL2"

And then a lot of lines like this:

/Users/hoster/emsdk_portable/clang/fastcomp/build_incoming_64/bin/llvm-nm: /Users/hoster/projects/gwar/target/asmjs-unknown-emscripten/debug/deps/libsdl2-a3439967d6cd896a.rlib(rust.metadata.bin) The end of the file was unexpectedly encountered

I removed -l SDL2 part, added -s USE_SDL=2 and ran emcc after cargo build manually. Eventually it gave me a JavaScript output which I was able to run on my HTML page. However, almost all examples from here just hang a web page.

Is it possible at this moment to run SDL2 + Rust + emscripten bundle?


#2

SDL2 is a native library, so you’ll need to figure out how to compile SDL2 with emscripten (see this and this, for example) and then figure out how to combine it with rustc output.