|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "The wasm32-wasip2 Target Has Reached Tier 2 Support" |
| 4 | +author: Yosh Wuyts |
| 5 | +--- |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +In April of this year we posted an update about [Rust's WASI |
| 10 | +targets](https://blog.rust-lang.org/2024/04/09/updates-to-rusts-wasi-targets.html) |
| 11 | +to the main Rust blog. In it we covered the rename of the `wasm32-wasi` target |
| 12 | +to `wasm32-wasip1`, and the introduction of the new `wasm32-wasip2` target as a |
| 13 | +"tier 3" target. This meant that while the target was available as part of |
| 14 | +`rust-lang/rustc`, it was not guaranteed to build. We're pleased to announce |
| 15 | +that this has changed in Rust 1.82. |
| 16 | + |
| 17 | +For those unfamiliar with WebAssembly (Wasm) components and WASI 0.2, here is a quick, simplified primer: |
| 18 | + |
| 19 | +- **Wasm** is a (virtual) instruction format for programs to be compiled into (think: x86). |
| 20 | +- **Wasm Components** are a container format and type system that wrap Core Wasm instructions into typed, hermetic binaries and libraries (think: ELF). |
| 21 | +- **WASI** is a reserved namespace for a collection of standardized Wasm component interfaces (think: POSIX header files). |
| 22 | + |
| 23 | +For a more detailed explanation see the [WASI 0.2 announcement post](https://bytecodealliance.org/articles/WASI-0.2) on the Bytecode Alliance blog. |
| 24 | + |
| 25 | +## What's new? |
| 26 | + |
| 27 | +Starting Rust 1.82 (2024-10-17) the `wasm32-wasip2` (WASI 0.2) target has |
| 28 | +reached tier-2 platform support in the Rust compiler. Among other things this |
| 29 | +now means it is guaranteed to build, and is now available to install via Rustup |
| 30 | +using the following command: |
| 31 | + |
| 32 | +```bash |
| 33 | +rustup target add wasm32-wasip2 |
| 34 | +``` |
| 35 | + |
| 36 | +Up until now Rust users writing [Wasm |
| 37 | +Components](https://component-model.bytecodealliance.org) would always have to rely on tools (such as |
| 38 | +[cargo-component]) which target the WASI 0.1 target (`wasm32-wasip1`) and |
| 39 | +package it into a WASI 0.2 Component via a post-processing step invoked. Now |
| 40 | +that `wasm32-wasip2` is available to everyone via Rustup, tooling can |
| 41 | +begin to directly target WASI 0.2 without the need for additional post-processing. |
| 42 | + |
| 43 | +What this also means is that ecosystem crates can begin targeting WASI 0.2 |
| 44 | +directly for platform-specific code. WASI 0.1 did not have support for sockets. |
| 45 | +Now that we have a stable tier 2 platform available, crate authors should be |
| 46 | +able to finally start writing WASI-compatible network code. To target WASI 0.2 |
| 47 | +from Rust, authors can use the following `cfg` attribute: |
| 48 | + |
| 49 | +[cargo-component]: https://github.com/bytecodealliance/cargo-component |
| 50 | + |
| 51 | +```rust |
| 52 | +#[cfg(all(target_os = "wasi", target_env = "p2"))] |
| 53 | +mod wasip2 { |
| 54 | + // items go here |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +To target the older WASI 0.1 target, Rust also accepts `target_env = "p1"`. |
| 59 | + |
| 60 | +## Standard Library Support |
| 61 | + |
| 62 | +The WASI 0.2 Rust target reaching tier 2 platform support is in a way just the |
| 63 | +beginning. means it's supported and stable. While the platform itself is now |
| 64 | +stable, support in the stdlib for WASI 0.2 APIs is still limited. While the WASI |
| 65 | +0.2 specification specifies APIs for example for timers, files, and sockets - if |
| 66 | +you try and use the stdlib APIs for these today, you'll find they don't yet |
| 67 | +work. |
| 68 | + |
| 69 | +We expect to gradually extend the Rust stdlib with support for WASI 0.2 APIs |
| 70 | +throughout the remainder of this year into the next. That work has already |
| 71 | +started, with |
| 72 | +[rust-lang/rust#129638](https://github.com/rust-lang/rust/pull/129638) adding |
| 73 | +native support for `std::net` in Rust 1.83. We expect more of these PRs to land |
| 74 | +through the remainder of the year. |
| 75 | + |
| 76 | +Though this doesn't need to stop users from using WASI 0.2 today. The stdlib is |
| 77 | +great because it provides *portable* abstractions, usually built on top of an |
| 78 | +operating system's `libc` or equivalent. If you want to use WASI 0.2 APIs |
| 79 | +directly today, you can either use the |
| 80 | +[wasi](https://docs.rs/wasi/latest/wasi/) crate directly. Or generate your own |
| 81 | +WASI bindings from the [WASI |
| 82 | +specification's](https://github.com/WebAssembly/WASI/tree/main/wasip2) interface |
| 83 | +types using [wit-bindgen](https://github.com/bytecodealliance/wit-bindgen/). |
| 84 | + |
| 85 | +## Conclusion |
| 86 | + |
| 87 | +The `wasm32-wasip2` target is now installable via Rustup. This makes it possible |
| 88 | +for the Rust compiler to directly compile to the Wasm Components format |
| 89 | +targeting the WASI 0.2 interfaces. There is now also a way for crates to compile |
| 90 | +add WASI 0.2 platform support by writing: |
| 91 | + |
| 92 | +```rust |
| 93 | +#[cfg(all(target_os = "wasi", target_env = "p2"))] |
| 94 | +mod wasip2 {} |
| 95 | +``` |
| 96 | + |
| 97 | +We're excited for Wasm Components and WASI 0.2 to have reached this milestone |
| 98 | +within the Rust project, and are excited to see what folks in the community will |
| 99 | +be building with it! |
0 commit comments