|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "The wasm32-wasip2 Target Has Reached Tier 2 Support" |
| 4 | +author: Yosh Wuyts |
| 5 | +--- |
| 6 | + |
| 7 | +In April of this year we posted an update about [Rust's WASI |
| 8 | +targets](https://blog.rust-lang.org/2024/04/09/updates-to-rusts-wasi-targets.html) |
| 9 | +to the main Rust blog. In it we covered the rename of the `wasm32-wasi` target |
| 10 | +to `wasm32-wasip1`, and the introduction of the new `wasm32-wasip2` target as a |
| 11 | +"tier 3" target. This meant that while the target was available as part of |
| 12 | +`rustc`, it was not guaranteed to build. We're pleased to announce |
| 13 | +that this is changing. |
| 14 | + |
| 15 | +For those unfamiliar with WebAssembly (Wasm) components and WASI 0.2, here is a |
| 16 | +quick, simplified primer: |
| 17 | + |
| 18 | +- **Core Wasm** is a (virtual) instruction format for programs to be compiled into (think: x86-64). |
| 19 | +- **Wasm components** are a container format and type system that wrap Core Wasm instructions into typed, hermetic binaries and libraries (think: ELF). |
| 20 | +- **WASI** is a reserved namespace for a collection of standardized Wasm component interfaces (think: POSIX header files). |
| 21 | + |
| 22 | +For a more detailed explanation see the [WASI 0.2 announcement |
| 23 | +post](https://bytecodealliance.org/articles/WASI-0.2) on the Bytecode Alliance |
| 24 | +blog. |
| 25 | + |
| 26 | +## What's new? |
| 27 | + |
| 28 | +Starting Rust 1.81 (2024-09-05) the `wasm32-wasip2` (WASI 0.2) target will be |
| 29 | +made available as a tier-2 target. Among other things this now means it is |
| 30 | +guaranteed to build, and will become available to install as a prebuilt target |
| 31 | +via Rustup using the following command: |
| 32 | + |
| 33 | +```bash |
| 34 | +rustup target add wasm32-wasip2 |
| 35 | +``` |
| 36 | + |
| 37 | +Up until now Rust users writing [Wasm |
| 38 | +Components](https://component-model.bytecodealliance.org) would use tools (like |
| 39 | +[cargo-component]) which target the WASI 0.1 target (`wasm32-wasip1`) which |
| 40 | +would be packaged into a WASI 0.2 Component by a post-processing step invoked |
| 41 | +separately by the tooling. With the introduction of `wasm32-wasip2` as a tier-2 |
| 42 | +target, that tooling can instead directly target WASI 0.2 without the need for |
| 43 | +additional tooling or compat shims. |
| 44 | + |
| 45 | +What this also means is that ecosystem crates can begin targeting WASI 0.2 |
| 46 | +directly for platform-specific code. WASI 0.1 did not have support for sockets. |
| 47 | +Now that we have a stable tier 2 platform available, crate authors should be |
| 48 | +able to finally start writing WASI-compatible network code. To target WASI 0.2 |
| 49 | +from Rust, authors can use the following `cfg` attribute: |
| 50 | + |
| 51 | +[cargo-component]: https://github.com/bytecodealliance/cargo-component |
| 52 | + |
| 53 | +```rust |
| 54 | +#[cfg(all(target_os = "wasi", target_env = "p2"))] |
| 55 | +mod wasip2 { |
| 56 | + // items go here |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## What's next? |
| 61 | + |
| 62 | +The WASI 0.2 Rust target being on tier 2 means it's supported and stable. But |
| 63 | +unfortunately it is also still incomplete. Most notably stdlib support is still |
| 64 | +limited. To target platform-native functionality we recommend to either use the |
| 65 | +[wasi](https://docs.rs/wasi/latest/wasi/) crate directly, which is the WASI |
| 66 | +equivalent to the `libc` crate. Or else generate their own bindings to the [WASI |
| 67 | +specifications](https://github.com/WebAssembly/WASI/tree/main/wasip2) using the |
| 68 | +[wit-bindgen](https://github.com/bytecodealliance/wit-bindgen/) generator. |
| 69 | + |
| 70 | +We expect to gradually extend the Rust stdlib with support for WASI 0.2 APIs |
| 71 | +throughout the remainder of this year into the next. That work has already |
| 72 | +started, with for example |
| 73 | +[rust-lang/rust#129638](https://github.com/rust-lang/rust/pull/129638) adding |
| 74 | +native support for `std::net` to WASI 0.2. We expect more of these PRs to land |
| 75 | +through the remainder of the year. |
| 76 | + |
| 77 | +## Conclusion |
| 78 | + |
| 79 | +The `wasm32-wasip2` target will become installable via rustup starting Rust |
| 80 | +1.81.0 (2024-09-05). This will make it possible for the Rust compiler to |
| 81 | +directly compile to the Wasm Components format, targeting the WASI 0.2 |
| 82 | +interfaces. We're excited for Wasm Components and WASI 0.2 to have reached this |
| 83 | +milestone, and are excited to see what folks in the ecosystem will be building |
| 84 | +with it! |
0 commit comments