|
| 1 | +# HTML5 |
| 2 | + |
| 3 | +Exporting to HTML5 works just like exporting to other platforms, however there are some things that can make the process a bit tricky and require extra attention. |
| 4 | + |
| 5 | +## General considerations |
| 6 | + |
| 7 | +The Godot HTML5 export templates are built with [Emscripten](https://emscripten.org/), so you need to build your code for the `wasm32-unknown-emscripten` target, `wasm32-unknown-unknown` will not work. Furthermore, you need to use a version of emscripten that is compatible with both your version of Rust and the version of emscripten used to build the export template. |
| 8 | +In practice, this means you might have to [build the HTML5 export template yourself](https://docs.godotengine.org/en/stable/development/compiling/compiling_for_web.html) and/or use a specific version of Rust. You can check which versions you are using like this: |
| 9 | + |
| 10 | +```bash |
| 11 | +emcc -v |
| 12 | +rustc --version --verbose |
| 13 | +``` |
| 14 | + |
| 15 | +Also note that `wasm32-unknown-emscripten` is a 32-bit target, which can cause problems with crates incorrectly assuming that `usize` is 64 bits wide. |
| 16 | + |
| 17 | +## Godot 3.4.4 |
| 18 | + |
| 19 | +**Disclaimer**: _Currently, the following steps are only tested and confirmed to work on Linux._ |
| 20 | + |
| 21 | +Godot 3.4.4's export template is built with a very old Emscripten version that doesn't play nice with Rust. We will have to build it ourselves with a more recent version. |
| 22 | +We also need to build the `std` crate ourselves, so we will have to use a nightly Rust build. |
| 23 | + |
| 24 | +Confirmed working versions are: |
| 25 | +* rustc 1.63.0-nightly (dc80ca78b 2022-06-21) |
| 26 | +* Emscripten 3.1.15-git (4100960b42619b28f19baf4254d5db2097234b32) |
| 27 | + |
| 28 | + |
| 29 | +### Getting Emscripten |
| 30 | + |
| 31 | +We will be using Emscripten tip-of-tree: |
| 32 | + |
| 33 | +```bash |
| 34 | +# Get the emsdk repo |
| 35 | +git clone https://github.com/emscripten-core/emsdk.git |
| 36 | + |
| 37 | +# Enter that directory |
| 38 | +cd emsdk |
| 39 | + |
| 40 | +# Download and install the tip-of-tree SDK tools. |
| 41 | +./emsdk install tot |
| 42 | + |
| 43 | +# Make the "tot" SDK "active" for the current user. (writes .emscripten file) |
| 44 | +./emsdk activate tot |
| 45 | + |
| 46 | +# Activate PATH and other environment variables in the current terminal |
| 47 | +source ./emsdk_env.sh |
| 48 | +``` |
| 49 | + |
| 50 | +### Building Godot |
| 51 | + |
| 52 | +Build the Godot Export template according to the [instructions](https://docs.godotengine.org/en/stable/development/compiling/compiling_for_web.html): |
| 53 | + |
| 54 | +```bash |
| 55 | +source "/path/to/emsdk-portable/emsdk_env.sh" |
| 56 | +scons platform=javascript tools=no gdnative_enabled=yes target=release |
| 57 | +mv bin/godot.javascript.opt.gdnative.zip bin/webassembly_gdnative_release.zip |
| 58 | +``` |
| 59 | + |
| 60 | +Set the newly built export template as a custom template in Godot and be sure to set the export type as GDNative. When exporting, uncheck "Export With Debug". |
| 61 | + |
| 62 | +### Building your Rust code |
| 63 | + |
| 64 | +In your project's `config.toml`, add the following: |
| 65 | + |
| 66 | +```toml |
| 67 | +[target.wasm32-unknown-emscripten] |
| 68 | +rustflags = [ |
| 69 | + "-Clink-arg=-sSIDE_MODULE=2", # build a side module that Godot can load |
| 70 | + "-Crelocation-model=pic", # needed to prevent linker errors |
| 71 | + "-Cpanic=abort", # panic unwinding is currently broken without -sWASM_BIGINT, see below |
| 72 | + #"-Clink-arg=-sWASM_BIGINT", # alternative to panic=abort, requires building godot with -sWASM_BIGINT also |
| 73 | +] |
| 74 | +``` |
| 75 | + |
| 76 | +Build like this: |
| 77 | + |
| 78 | +```bash |
| 79 | +source "/path/to/emsdk-portable/emsdk_env.sh" |
| 80 | +export C_INCLUDE_PATH=$EMSDK/upstream/emscripten/cache/sysroot/include |
| 81 | + |
| 82 | +cargo +nightly build --target=wasm32-unknown-emscripten --release -Zbuild-std=core,std,alloc,panic_abort |
| 83 | +``` |
| 84 | + |
| 85 | +### Further reading |
| 86 | + |
| 87 | +In the future, some/all of will probably not be needed, for more info see: |
| 88 | +* [Tracking issue for godot-rust wasm support](https://github.com/godot-rust/godot-rust/issues/647) |
| 89 | +* [Rust PR that obsoletes -Clink-arg=-sSIDE_MODULE=2](https://github.com/rust-lang/rust/pull/98358) |
| 90 | +* [Rust PR that obsoletes -Crelocation-model=pic and -Zbuild-std](https://github.com/rust-lang/rust/pull/98149) |
| 91 | +* [Godot PR that updates Emscripten version](https://github.com/godotengine/godot/pull/61989) |
| 92 | +* [Godot PR that would obsolete -Cpanic=abort in favor of -sWASM_BIGINT](https://github.com/godotengine/godot/pull/62397) |
0 commit comments