|
| 1 | +# `wasm32-unknown-emscipten` |
| 2 | + |
| 3 | +**Tier: 2** |
| 4 | + |
| 5 | +The `wasm32-unknown-emscripten` target is a WebAssembly compilation target which |
| 6 | +uses the Emscripten compiler toolchain. Unlike the |
| 7 | +[`wasm32-unknown-unknown`](./wasm32-unknown-unknown.md) target, which only has |
| 8 | +partial support for the Rust standard library, `wasm32-unknown-emscripten` |
| 9 | +integrates with JavaScipt on the web to provide a more featureful standard library. |
| 10 | +This target is often used when compiling for the web, accessing web APIs, or |
| 11 | +interoperating with C and C++ code. |
| 12 | + |
| 13 | +One existing user of this target (please feel free to edit and expand this list |
| 14 | +too) is the [`pyodide` project](https://github.com/pyodide/pyodide) which provides |
| 15 | +a Python runtime in WebAssembly using Emscripten and compiles Python extension |
| 16 | +modules written in Rust to the `wasm32-unknown-emscripten` target. |
| 17 | + |
| 18 | +## Target maintainers |
| 19 | + |
| 20 | +When this target was added to the compiler, platform-specific documentation here |
| 21 | +was not maintained at that time. This means that the list below is not |
| 22 | +exhaustive, and there are more interested parties in this target. That being |
| 23 | +said, those interested in maintaining this target are: |
| 24 | + |
| 25 | +- TODO |
| 26 | + |
| 27 | +## Requirements |
| 28 | + |
| 29 | +This target is cross-compiled. The Emscripten compiler toolchain must be |
| 30 | +installed to link Rust-compiled code with Emscripten's libc and other C/C++ |
| 31 | +code. |
| 32 | + |
| 33 | +## Building the target |
| 34 | + |
| 35 | +Building this target can be done by: |
| 36 | + |
| 37 | +* Configure the `wasm32-unknown-emscripten` target to get built. |
| 38 | +* Configure LLD to be built. |
| 39 | +* Ensure the `WebAssembly` target backend is not disabled in LLVM. |
| 40 | + |
| 41 | +These are all controlled through `config.toml` options. It should be possible |
| 42 | +to build this target on any platform. |
| 43 | + |
| 44 | +## Building Rust programs |
| 45 | + |
| 46 | +Rust programs can be compiled by adding this target via rustup: |
| 47 | + |
| 48 | +```sh |
| 49 | +$ rustup target add wasm32-unknown-emscripten |
| 50 | +``` |
| 51 | + |
| 52 | +and then compiling with the target: |
| 53 | + |
| 54 | +```sh |
| 55 | +$ rustc foo.rs --target wasm32-unknown-emscripten |
| 56 | +$ file foo.wasm |
| 57 | +``` |
| 58 | + |
| 59 | +## Cross-compilation |
| 60 | + |
| 61 | +This target can be cross-compiled from any host. |
| 62 | + |
| 63 | +## Emscripten ABI Compatibility |
| 64 | + |
| 65 | +The Emscripten compiler toolchain does not follow a semantic versioning scheme |
| 66 | +that clearly indicates when breaking changes to the ABI can be made. If you are |
| 67 | +using the pre-compiled `std` from rustup, it may be compiled with a different |
| 68 | +Emscripten version than the local Emscripten you have installed. If a breaking |
| 69 | +change to the ABI was made in between the two versions, your code will exhibit |
| 70 | +undefined behaviour. If you come across issues, you can rebuild the Rust standard |
| 71 | +library with your local Emscripten version using |
| 72 | + |
| 73 | +```sh |
| 74 | +$ cargo +nightly -Zbuild-std build |
| 75 | +``` |
| 76 | + |
| 77 | +## Testing |
| 78 | + |
| 79 | +This target is not extensively tested in CI for the rust-lang/rust repository. |
| 80 | +It's recommended to test the `wasm32-wasip1` target instead for WebAssembly |
| 81 | +compatibility. |
| 82 | + |
| 83 | +## Conditionally compiling code |
| 84 | + |
| 85 | +It's recommended to conditionally compile code for this target with: |
| 86 | + |
| 87 | +```text |
| 88 | +#[cfg(all(target_family = "wasm", target_os = "emscripten"))] |
| 89 | +``` |
| 90 | + |
| 91 | +Note that there is no way to tell via `#[cfg]` whether code will be running on |
| 92 | +the web or not. |
| 93 | + |
| 94 | +## Enabled WebAssembly features |
| 95 | + |
| 96 | +WebAssembly is an evolving standard which adds new features such as new |
| 97 | +instructions over time. The `wasm32-unknown-emscripten` target inherits the |
| 98 | +default settings of Emscripten. |
0 commit comments