Skip to content

Commit fd50d65

Browse files
committed
Add the wasm32-wasi-preview2 target
Signed-off-by: Ryan Levick <[email protected]>
1 parent 8d39ec1 commit fd50d65

File tree

12 files changed

+113
-17
lines changed

12 files changed

+113
-17
lines changed

Cargo.lock

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,9 +2151,8 @@ checksum = "db13adb97ab515a3691f56e4dbab09283d0b86cb45abd991d8634a9d6f501760"
21512151

21522152
[[package]]
21532153
name = "libc"
2154-
version = "0.2.150"
2155-
source = "registry+https://github.com/rust-lang/crates.io-index"
2156-
checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c"
2154+
version = "0.2.151"
2155+
source = "git+https://github.com/rylev/rust-libc?rev=fc437f39b197e7713ca91f1385e4b309b3e9a820#fc437f39b197e7713ca91f1385e4b309b3e9a820"
21572156
dependencies = [
21582157
"rustc-std-workspace-core",
21592158
]

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ miniz_oxide.debug = 0
105105
object.debug = 0
106106

107107
[patch.crates-io]
108+
libc = { git = "https://github.com/rylev/rust-libc", rev = "fc437f39b197e7713ca91f1385e4b309b3e9a820" }
109+
108110
# See comments in `library/rustc-std-workspace-core/README.md` for what's going on
109111
# here
110112
rustc-std-workspace-core = { path = 'library/rustc-std-workspace-core' }

compiler/rustc_target/src/spec/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,7 @@ supported_targets! {
15741574
("wasm32-unknown-emscripten", wasm32_unknown_emscripten),
15751575
("wasm32-unknown-unknown", wasm32_unknown_unknown),
15761576
("wasm32-wasi", wasm32_wasi),
1577+
("wasm32-wasi-preview2", wasm32_wasi_preview2),
15771578
("wasm32-wasi-preview1-threads", wasm32_wasi_preview1_threads),
15781579
("wasm64-unknown-unknown", wasm64_unknown_unknown),
15791580

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//! The `wasm32-wasi-preview2` target is the next evolution of the
2+
//! wasm32-wasi target. While the wasi specification is still under
3+
//! active development, the {review 2 iteration is considered an "island
4+
//! of stability" that should allow users to rely on it indefinitely.
5+
//!
6+
//! The `wasi` target is a proposal to define a standardized set of WebAssembly
7+
//! component imports that allow it to interoperate with the host system in a
8+
//! standardized way. This set of imports is intended to empower WebAssembly
9+
//! binaries with host capabilities such as filesystem access, network access, etc.
10+
//!
11+
//! Wasi Preview 2 relies on the WebAssembly component model which is an extension of
12+
//! the core WebAssembly specification which allows interoperability between WebAssembly
13+
//! modules (known as "components") through high-level, shared-nothing APIs instead of the
14+
//! low-level, shared-everything linear memory model of the core WebAssembly specification.
15+
//!
16+
//! You can see more about wasi at <https://wasi.dev> and the component model at
17+
//! <https://github.com/WebAssembly/component-model>.
18+
19+
use crate::spec::crt_objects;
20+
use crate::spec::LinkSelfContainedDefault;
21+
use crate::spec::{base, Target};
22+
23+
pub fn target() -> Target {
24+
let mut options = base::wasm::options();
25+
26+
options.os = "wasi-preview2".into();
27+
options.linker = Some("wasm-component-ld".into());
28+
29+
options.pre_link_objects_self_contained = crt_objects::pre_wasi_self_contained();
30+
options.post_link_objects_self_contained = crt_objects::post_wasi_self_contained();
31+
32+
// FIXME: Figure out cases in which WASM needs to link with a native toolchain.
33+
options.link_self_contained = LinkSelfContainedDefault::True;
34+
35+
// Right now this is a bit of a workaround but we're currently saying that
36+
// the target by default has a static crt which we're taking as a signal
37+
// for "use the bundled crt". If that's turned off then the system's crt
38+
// will be used, but this means that default usage of this target doesn't
39+
// need an external compiler but it's still interoperable with an external
40+
// compiler if configured correctly.
41+
options.crt_static_default = true;
42+
options.crt_static_respected = true;
43+
44+
// Allow `+crt-static` to create a "cdylib" output which is just a wasm file
45+
// without a main function.
46+
options.crt_static_allows_dylibs = true;
47+
48+
// WASI's `sys::args::init` function ignores its arguments; instead,
49+
// `args::args()` makes the WASI API calls itself.
50+
options.main_needs_argc_argv = false;
51+
52+
// And, WASI mangles the name of "main" to distinguish between different
53+
// signatures.
54+
options.entry_name = "__main_void".into();
55+
56+
Target {
57+
llvm_target: "wasm32-unknown-unknown".into(),
58+
pointer_width: 32,
59+
data_layout: "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20".into(),
60+
arch: "wasm32".into(),
61+
options,
62+
}
63+
}

library/std/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fortanix-sgx-abi = { version = "0.5.0", features = ['rustc-dep-of-std'], public
5050
[target.'cfg(target_os = "hermit")'.dependencies]
5151
hermit-abi = { version = "0.3.2", features = ['rustc-dep-of-std'], public = true }
5252

53-
[target.'cfg(target_os = "wasi")'.dependencies]
53+
[target.'cfg(any(target_os = "wasi", target_os = "wasi-preview2"))'.dependencies]
5454
wasi = { version = "0.11.0", features = ['rustc-dep-of-std'], default-features = false }
5555

5656
[target.'cfg(target_os = "uefi")'.dependencies]

library/std/src/os/fd/raw.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::os::raw;
1212
use crate::os::unix::io::AsFd;
1313
#[cfg(unix)]
1414
use crate::os::unix::io::OwnedFd;
15-
#[cfg(target_os = "wasi")]
15+
#[cfg(any(target_os = "wasi", target_os = "wasi-preview2"))]
1616
use crate::os::wasi::io::OwnedFd;
1717
use crate::sys_common::{AsInner, IntoInner};
1818
#[cfg(target_os = "hermit")]

library/std/src/os/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub mod raw;
1818
#[cfg(all(
1919
doc,
2020
any(
21-
all(target_arch = "wasm32", not(target_os = "wasi")),
21+
all(target_arch = "wasm32", not(target_os = "wasi"), not(target_os = "wasi-preview2")),
2222
all(target_vendor = "fortanix", target_env = "sgx")
2323
)
2424
))]
@@ -27,7 +27,7 @@ pub mod unix {}
2727
#[cfg(all(
2828
doc,
2929
any(
30-
all(target_arch = "wasm32", not(target_os = "wasi")),
30+
all(target_arch = "wasm32", not(target_os = "wasi"), not(target_os = "wasi-preview2")),
3131
all(target_vendor = "fortanix", target_env = "sgx")
3232
)
3333
))]
@@ -36,7 +36,7 @@ pub mod linux {}
3636
#[cfg(all(
3737
doc,
3838
any(
39-
all(target_arch = "wasm32", not(target_os = "wasi")),
39+
all(target_arch = "wasm32", not(target_os = "wasi"), not(target_os = "wasi-preview2")),
4040
all(target_vendor = "fortanix", target_env = "sgx")
4141
)
4242
))]
@@ -45,7 +45,7 @@ pub mod wasi {}
4545
#[cfg(all(
4646
doc,
4747
any(
48-
all(target_arch = "wasm32", not(target_os = "wasi")),
48+
all(target_arch = "wasm32", not(target_os = "wasi"), not(target_os = "wasi-preview2")),
4949
all(target_vendor = "fortanix", target_env = "sgx")
5050
)
5151
))]
@@ -56,7 +56,7 @@ pub mod windows {}
5656
#[cfg(not(all(
5757
doc,
5858
any(
59-
all(target_arch = "wasm32", not(target_os = "wasi")),
59+
all(target_arch = "wasm32", not(target_os = "wasi"), not(target_os = "wasi-preview2")),
6060
all(target_vendor = "fortanix", target_env = "sgx")
6161
)
6262
)))]
@@ -78,18 +78,18 @@ pub mod linux;
7878
#[cfg(not(all(
7979
doc,
8080
any(
81-
all(target_arch = "wasm32", not(target_os = "wasi")),
81+
all(target_arch = "wasm32", not(target_os = "wasi"), not(target_os = "wasi-preview2")),
8282
all(target_vendor = "fortanix", target_env = "sgx")
8383
)
8484
)))]
85-
#[cfg(any(target_os = "wasi", doc))]
85+
#[cfg(any(target_os = "wasi", target_os = "wasi-preview2", doc))]
8686
pub mod wasi;
8787

8888
// windows
8989
#[cfg(not(all(
9090
doc,
9191
any(
92-
all(target_arch = "wasm32", not(target_os = "wasi")),
92+
all(target_arch = "wasm32", not(target_os = "wasi"), not(target_os = "wasi-preview2")),
9393
all(target_vendor = "fortanix", target_env = "sgx")
9494
)
9595
)))]
@@ -155,7 +155,7 @@ pub(crate) mod watchos;
155155
#[cfg(target_os = "xous")]
156156
pub mod xous;
157157

158-
#[cfg(any(unix, target_os = "wasi", doc))]
158+
#[cfg(any(unix, target_os = "wasi", target_os = "wasi-preview2", doc))]
159159
pub mod fd;
160160

161161
#[cfg(any(target_os = "linux", target_os = "android", doc))]

library/std/src/sys/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ cfg_if::cfg_if! {
3838
} else if #[cfg(target_os = "hermit")] {
3939
mod hermit;
4040
pub use self::hermit::*;
41-
} else if #[cfg(target_os = "wasi")] {
41+
} else if #[cfg(any(target_os = "wasi", target_os = "wasi-preview2"))] {
4242
mod wasi;
4343
pub use self::wasi::*;
4444
} else if #[cfg(target_family = "wasm")] {

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ fn copy_self_contained_objects(
346346
panic!("Target {:?} does not have a \"wasi-root\" key", target.triple)
347347
})
348348
.join("lib")
349-
.join(target.to_string().replace("-preview1", ""));
349+
.join(target.to_string().replace("-preview1", "").replace("-preview2", ""));
350350
for &obj in &["libc.a", "crt1-command.o", "crt1-reactor.o"] {
351351
copy_and_stamp(
352352
builder,

src/bootstrap/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const EXTRA_CHECK_CFGS: &[(Option<Mode>, &str, Option<&[&'static str]>)] = &[
8989
(Some(Mode::Std), "backtrace_in_libstd", None),
9090
/* Extra values not defined in the built-in targets yet, but used in std */
9191
(Some(Mode::Std), "target_env", Some(&["libnx"])),
92-
// (Some(Mode::Std), "target_os", Some(&[])),
92+
(Some(Mode::Std), "target_os", Some(&["wasi-preview2"])),
9393
(Some(Mode::Std), "target_arch", Some(&["spirv", "nvptx", "xtensa"])),
9494
/* Extra names used by dependencies */
9595
// FIXME: Used by serde_json, but we should not be triggering on external dependencies.

src/doc/rustc/src/platform-support.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ target | std | host | notes
340340
`thumbv7a-pc-windows-msvc` | ? | |
341341
`thumbv7a-uwp-windows-msvc` | ✓ | |
342342
`thumbv7neon-unknown-linux-musleabihf` | ? | | Thumb2-mode ARMv7-A Linux with NEON, MUSL
343+
[`wasm32-wasi-preview2`](platform-support/wasm32-wasi-preview2.md) | ✓ | | WebAssembly
343344
[`wasm64-unknown-unknown`](platform-support/wasm64-unknown-unknown.md) | ? | | WebAssembly
344345
`x86_64-apple-ios-macabi` | ✓ | | Apple Catalyst on x86_64
345346
[`x86_64-apple-tvos`](platform-support/apple-tvos.md) | ? | | x86 64-bit tvOS
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# `wasm32-wasi-preview2`
2+
3+
**Tier: 3**
4+
5+
The `wasm32-wasi-preview2` target is a new and still (as of January 2024) an
6+
experimental target. This target is an extension to `wasm32-wasi-preview1` target,
7+
originally known as `wasm32-wasi`. It is the next evolution in the development of
8+
wasi (the [WebAssembly System Interface](https://wasi.dev)) that uses the WebAssembly
9+
[component model] to allow for a standardized set of syscalls that are intended to empower
10+
WebAssembly binaries with native host capabilities.
11+
12+
[component model]: https://github.com/WebAssembly/component-model
13+
14+
## Target maintainers
15+
16+
- Alex Crichton, https://github.com/alexcrichton
17+
- Ryan Levick, https://github.com/rylev
18+
19+
## Requirements
20+
21+
This target is cross-compiled. The target supports `std` fully.
22+
23+
## Platform requirements
24+
25+
The WebAssembly runtime should support the wasi preview 2 API set.
26+
27+
This target is not a stable target. This means that there are only a few engines
28+
which implement wasi preview 2, for example:
29+
30+
* Wasmtime - `--wasm-features=preview2`

0 commit comments

Comments
 (0)