Skip to content

Commit 4e4fa7e

Browse files
authored
Rollup merge of #126967 - alexcrichton:wasm32-wasip2-tier-2, r=Mark-Simulacrum
Promote the `wasm32-wasip2` target to Tier 2 This commit promotes the `wasm32-wasip2` Rust target to tier 2 as proposed in rust-lang/compiler-team#760. There are two major changes in this PR: 1. The `dist-various-2` container, which already produces the other WASI targets, now has an extra target added for `wasm32-wasip2`. 2. A new `wasm-component-ld` binary is added to all host toolchains when LLD is enabled. This is the linker used for the `wasm32-wasip2` target. This new linker is added for all host toolchains to ensure that all host toolchains can produce the `wasm32-wasip2` target. This is similar to how `rust-lld` was originally included for all host toolchains to be able to produce WebAssembly output when the targets were first added. The new linker is developed [here][wasm-component-ld] and is pulled in via a crates.io-based dependency to the tree here. [wasm-component-ld]: https://github.com./bytecodealliance/wasm-component-ld
2 parents 9833e21 + c370bf4 commit 4e4fa7e

File tree

9 files changed

+264
-3
lines changed

9 files changed

+264
-3
lines changed

Diff for: Cargo.lock

+154-3
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,7 @@ dependencies = [
16821682
"compiler_builtins",
16831683
"rustc-std-workspace-alloc",
16841684
"rustc-std-workspace-core",
1685+
"serde",
16851686
]
16861687

16871688
[[package]]
@@ -1891,6 +1892,12 @@ dependencies = [
18911892
"syn 2.0.67",
18921893
]
18931894

1895+
[[package]]
1896+
name = "id-arena"
1897+
version = "2.2.1"
1898+
source = "registry+https://github.com./rust-lang/crates.io-index"
1899+
checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005"
1900+
18941901
[[package]]
18951902
name = "ident_case"
18961903
version = "1.0.1"
@@ -2120,6 +2127,12 @@ version = "1.0.5"
21202127
source = "registry+https://github.com./rust-lang/crates.io-index"
21212128
checksum = "db13adb97ab515a3691f56e4dbab09283d0b86cb45abd991d8634a9d6f501760"
21222129

2130+
[[package]]
2131+
name = "lexopt"
2132+
version = "0.3.0"
2133+
source = "registry+https://github.com./rust-lang/crates.io-index"
2134+
checksum = "baff4b617f7df3d896f97fe922b64817f6cd9a756bb81d40f8883f2f66dcb401"
2135+
21232136
[[package]]
21242137
name = "libc"
21252138
version = "0.2.155"
@@ -2634,7 +2647,7 @@ dependencies = [
26342647
"indexmap",
26352648
"memchr",
26362649
"ruzstd 0.5.0",
2637-
"wasmparser",
2650+
"wasmparser 0.118.2",
26382651
]
26392652

26402653
[[package]]
@@ -3445,7 +3458,7 @@ dependencies = [
34453458
"object 0.34.0",
34463459
"regex",
34473460
"similar",
3448-
"wasmparser",
3461+
"wasmparser 0.118.2",
34493462
]
34503463

34513464
[[package]]
@@ -3832,7 +3845,7 @@ dependencies = [
38323845
"thin-vec",
38333846
"thorin-dwp",
38343847
"tracing",
3835-
"wasm-encoder",
3848+
"wasm-encoder 0.200.0",
38363849
"windows",
38373850
]
38383851

@@ -5276,6 +5289,15 @@ dependencies = [
52765289
"color-eyre",
52775290
]
52785291

5292+
[[package]]
5293+
name = "spdx"
5294+
version = "0.10.6"
5295+
source = "registry+https://github.com./rust-lang/crates.io-index"
5296+
checksum = "47317bbaf63785b53861e1ae2d11b80d6b624211d42cb20efcd210ee6f8a14bc"
5297+
dependencies = [
5298+
"smallvec",
5299+
]
5300+
52795301
[[package]]
52805302
name = "spdx-expression"
52815303
version = "0.5.2"
@@ -6324,6 +6346,28 @@ version = "0.2.92"
63246346
source = "registry+https://github.com./rust-lang/crates.io-index"
63256347
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
63266348

6349+
[[package]]
6350+
name = "wasm-component-ld"
6351+
version = "0.5.4"
6352+
source = "registry+https://github.com./rust-lang/crates.io-index"
6353+
checksum = "314d932d5e84c9678751b85498b1482b2f32f185744e449d3ce0b1d400376dad"
6354+
dependencies = [
6355+
"anyhow",
6356+
"clap",
6357+
"lexopt",
6358+
"tempfile",
6359+
"wasmparser 0.210.0",
6360+
"wat",
6361+
"wit-component",
6362+
]
6363+
6364+
[[package]]
6365+
name = "wasm-component-ld-wrapper"
6366+
version = "0.1.0"
6367+
dependencies = [
6368+
"wasm-component-ld",
6369+
]
6370+
63276371
[[package]]
63286372
name = "wasm-encoder"
63296373
version = "0.200.0"
@@ -6333,6 +6377,40 @@ dependencies = [
63336377
"leb128",
63346378
]
63356379

6380+
[[package]]
6381+
name = "wasm-encoder"
6382+
version = "0.210.0"
6383+
source = "registry+https://github.com./rust-lang/crates.io-index"
6384+
checksum = "e7e3764d9d6edabd8c9e16195e177be0d20f6ab942ad18af52860f12f82bc59a"
6385+
dependencies = [
6386+
"leb128",
6387+
]
6388+
6389+
[[package]]
6390+
name = "wasm-encoder"
6391+
version = "0.211.1"
6392+
source = "registry+https://github.com./rust-lang/crates.io-index"
6393+
checksum = "5e7d931a1120ef357f32b74547646b6fa68ea25e377772b72874b131a9ed70d4"
6394+
dependencies = [
6395+
"leb128",
6396+
]
6397+
6398+
[[package]]
6399+
name = "wasm-metadata"
6400+
version = "0.210.0"
6401+
source = "registry+https://github.com./rust-lang/crates.io-index"
6402+
checksum = "012729d1294907fcb0866f08460ab95426a6d0b176a599619b84cac7653452b4"
6403+
dependencies = [
6404+
"anyhow",
6405+
"indexmap",
6406+
"serde",
6407+
"serde_derive",
6408+
"serde_json",
6409+
"spdx",
6410+
"wasm-encoder 0.210.0",
6411+
"wasmparser 0.210.0",
6412+
]
6413+
63366414
[[package]]
63376415
name = "wasmparser"
63386416
version = "0.118.2"
@@ -6343,6 +6421,42 @@ dependencies = [
63436421
"semver",
63446422
]
63456423

6424+
[[package]]
6425+
name = "wasmparser"
6426+
version = "0.210.0"
6427+
source = "registry+https://github.com./rust-lang/crates.io-index"
6428+
checksum = "a7bbcd21e7581619d9f6ca00f8c4f08f1cacfe58bf63f83af57cd0476f1026f5"
6429+
dependencies = [
6430+
"ahash",
6431+
"bitflags 2.5.0",
6432+
"hashbrown",
6433+
"indexmap",
6434+
"semver",
6435+
"serde",
6436+
]
6437+
6438+
[[package]]
6439+
name = "wast"
6440+
version = "211.0.1"
6441+
source = "registry+https://github.com./rust-lang/crates.io-index"
6442+
checksum = "b25506dd82d00da6b14a87436b3d52b1d264083fa79cdb72a0d1b04a8595ccaa"
6443+
dependencies = [
6444+
"bumpalo",
6445+
"leb128",
6446+
"memchr",
6447+
"unicode-width",
6448+
"wasm-encoder 0.211.1",
6449+
]
6450+
6451+
[[package]]
6452+
name = "wat"
6453+
version = "1.211.1"
6454+
source = "registry+https://github.com./rust-lang/crates.io-index"
6455+
checksum = "eb716ca6c86eecac2d82541ffc39860118fc0af9309c4f2670637bea2e1bdd7d"
6456+
dependencies = [
6457+
"wast",
6458+
]
6459+
63466460
[[package]]
63476461
name = "winapi"
63486462
version = "0.3.9"
@@ -6570,6 +6684,43 @@ dependencies = [
65706684
"memchr",
65716685
]
65726686

6687+
[[package]]
6688+
name = "wit-component"
6689+
version = "0.210.0"
6690+
source = "registry+https://github.com./rust-lang/crates.io-index"
6691+
checksum = "a450bdb5d032acf1fa0865451fa0c6f50e62f2d31eaa8dba967c2e2d068694a4"
6692+
dependencies = [
6693+
"anyhow",
6694+
"bitflags 2.5.0",
6695+
"indexmap",
6696+
"log",
6697+
"serde",
6698+
"serde_derive",
6699+
"serde_json",
6700+
"wasm-encoder 0.210.0",
6701+
"wasm-metadata",
6702+
"wasmparser 0.210.0",
6703+
"wit-parser",
6704+
]
6705+
6706+
[[package]]
6707+
name = "wit-parser"
6708+
version = "0.210.0"
6709+
source = "registry+https://github.com./rust-lang/crates.io-index"
6710+
checksum = "60a965cbd439af19a4b44a54a97ab8957d86f02d01320efc9e31c1d3605c6710"
6711+
dependencies = [
6712+
"anyhow",
6713+
"id-arena",
6714+
"indexmap",
6715+
"log",
6716+
"semver",
6717+
"serde",
6718+
"serde_derive",
6719+
"serde_json",
6720+
"unicode-xid",
6721+
"wasmparser 0.210.0",
6722+
]
6723+
65736724
[[package]]
65746725
name = "writeable"
65756726
version = "0.5.5"

Diff for: Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ members = [
4545
"src/tools/opt-dist",
4646
"src/tools/coverage-dump",
4747
"src/tools/rustc-perf-wrapper",
48+
"src/tools/wasm-component-ld",
4849
]
4950

5051
exclude = [
@@ -104,6 +105,9 @@ rustc-demangle.debug = 0
104105
[profile.release.package.lld-wrapper]
105106
debug = 0
106107
strip = true
108+
[profile.release.package.wasm-component-ld-wrapper]
109+
debug = 0
110+
strip = true
107111

108112
[patch.crates-io]
109113
# See comments in `library/rustc-std-workspace-core/README.md` for what's going on

Diff for: src/bootstrap/src/core/build_steps/compile.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,21 @@ impl Step for Assemble {
18201820
&self_contained_lld_dir.join(exe(name, target_compiler.host)),
18211821
);
18221822
}
1823+
1824+
// In addition to `rust-lld` also install `wasm-component-ld` when
1825+
// LLD is enabled. This is a relatively small binary that primarily
1826+
// delegates to the `rust-lld` binary for linking and then runs
1827+
// logic to create the final binary. This is used by the
1828+
// `wasm32-wasip2` target of Rust.
1829+
let wasm_component_ld_exe =
1830+
builder.ensure(crate::core::build_steps::tool::WasmComponentLd {
1831+
compiler: build_compiler,
1832+
target: target_compiler.host,
1833+
});
1834+
builder.copy_link(
1835+
&wasm_component_ld_exe,
1836+
&libdir_bin.join(wasm_component_ld_exe.file_name().unwrap()),
1837+
);
18231838
}
18241839

18251840
if builder.config.llvm_enabled(target_compiler.host) {

Diff for: src/bootstrap/src/core/build_steps/tool.rs

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ bootstrap_tool!(
337337
RustdocGUITest, "src/tools/rustdoc-gui-test", "rustdoc-gui-test", is_unstable_tool = true, allow_features = "test";
338338
CoverageDump, "src/tools/coverage-dump", "coverage-dump";
339339
RustcPerfWrapper, "src/tools/rustc-perf-wrapper", "rustc-perf-wrapper";
340+
WasmComponentLd, "src/tools/wasm-component-ld", "wasm-component-ld", is_unstable_tool = true, allow_features = "min_specialization";
340341
);
341342

342343
#[derive(Debug, Clone, Hash, PartialEq, Eq)]

Diff for: src/ci/docker/host-x86_64/dist-various-2/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ ENV TARGETS=$TARGETS,wasm32-unknown-unknown
112112
ENV TARGETS=$TARGETS,wasm32-wasi
113113
ENV TARGETS=$TARGETS,wasm32-wasip1
114114
ENV TARGETS=$TARGETS,wasm32-wasip1-threads
115+
ENV TARGETS=$TARGETS,wasm32-wasip2
115116
ENV TARGETS=$TARGETS,sparcv9-sun-solaris
116117
ENV TARGETS=$TARGETS,x86_64-pc-solaris
117118
ENV TARGETS=$TARGETS,x86_64-unknown-linux-gnux32

Diff for: src/tools/tidy/src/deps.rs

+5
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ const EXCEPTIONS: ExceptionList = &[
9595
("self_cell", "Apache-2.0"), // rustc (fluent translations)
9696
("snap", "BSD-3-Clause"), // rustc
9797
("wasm-encoder", "Apache-2.0 WITH LLVM-exception"), // rustc
98+
("wasm-metadata", "Apache-2.0 WITH LLVM-exception"), // rustc
9899
("wasmparser", "Apache-2.0 WITH LLVM-exception"), // rustc
100+
("wast", "Apache-2.0 WITH LLVM-exception"), // rustc
101+
("wat", "Apache-2.0 WITH LLVM-exception"), // rustc
102+
("wit-component", "Apache-2.0 WITH LLVM-exception"), // rustc
103+
("wit-parser", "Apache-2.0 WITH LLVM-exception"), // rustc
99104
// tidy-alphabetical-end
100105
];
101106

Diff for: src/tools/wasm-component-ld/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# See the `README.md` in this directory for what this tool is.
2+
3+
[package]
4+
name = "wasm-component-ld-wrapper"
5+
version = "0.1.0"
6+
edition = "2021"
7+
8+
[[bin]]
9+
name = "wasm-component-ld"
10+
path = "src/main.rs"
11+
12+
[dependencies]
13+
wasm-component-ld = "0.5.4"

Diff for: src/tools/wasm-component-ld/README.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# `wasm-component-ld`
2+
3+
This wrapper is a wrapper around the [`wasm-component-ld`] crates.io crate. That
4+
crate. That crate is itself a thin wrapper around two pieces:
5+
6+
* `wasm-ld` - the LLVM-based linker distributed as part of LLD and packaged in
7+
Rust as `rust-lld`.
8+
* [`wit-component`] - a Rust crate for creating a [WebAssembly Component] from a
9+
core wasm module.
10+
11+
This linker is used for Rust's `wasm32-wasip2` target to natively output a
12+
component instead of a core WebAssembly module, unlike other WebAssembly
13+
targets. If you're confused about any of this here's an FAQ-style explanation of
14+
what's going on here:
15+
16+
* **What's a component?** - It's a proposal to the WebAssembly standard
17+
primarily developed at this time by out-of-browser use cases of WebAssembly.
18+
You can find high-level documentation [here][component docs].
19+
20+
* **What's WASIp2?** - Not to be confused with WASIp1, WASIp0,
21+
`wasi_snapshot_preview1`, or `wasi_unstable`, it's a version of WASI. Released
22+
in January 2024 it's the first version of WASI defined in terms of the
23+
component model.
24+
25+
* **Why does this need its own linker?** - like any target that Rust has the
26+
`wasm32-wasip2` target needs a linker. What makes this different from other
27+
WebAssembly targets is that WASIp2 is defined at the component level, not core
28+
WebAssembly level. This means that filesystem functions take a `string`
29+
instead of `i32 i32`, for example. This means that the raw output of LLVM and
30+
`wasm-ld`, a core WebAssembly module, is not suitable.
31+
32+
* **Isn't writing a linker really hard?** - Generally, yes, but this linker
33+
works by first asking `wasm-ld` to do all the hard work. It invokes `wasm-ld`
34+
and then uses the output core WebAssembly module to create a component.
35+
36+
* **How do you create a component from a core module?** - this is the purpose of
37+
the [`wit-component`] crate, notably the `ComponentEncoder` type. This uses
38+
component type information embedded in the core module and a general set of
39+
conventions/guidelines with what the core module imports/exports. A component
40+
is then hooked up to codify all of these conventions in a component itself.
41+
42+
* **Why not require users to run `wit-component` themselves?** - while possible
43+
it adds friction to the usage `wasm32-wasip2` target. More importantly though
44+
the "module only" output of the `wasm32-wasip2` target is not ready right now.
45+
The standard library still imports from `wasi_snapshot_preview1` and it will
46+
take time to migrate all usage to WASIp2.
47+
48+
* **What exactly does this linker do?** - the `wasm-component-ld` has the same
49+
CLI interface and flags as `wasm-ld`, plus some more that are
50+
component-specific. These flags are used to forward most flags to `wasm-ld` to
51+
produce a core wasm module. After the core wasm module is produced the
52+
`wit-component` crate will read custom sections in the final binary which
53+
contain component type information. After merging all this type information
54+
together a component is produced which wraps the core module.
55+
56+
If you've got any other questions about this linker or its operation don't
57+
hesitate to reach out to the maintainers of the `wasm32-wasip2` target.
58+
59+
[`wasm-component-ld`]: https://crates.io/crates/wasm-component-ld
60+
[`wit-component`]: https://crates.io/crates/wit-component
61+
[WebAssembly Component]: https://github.com./webassembly/component-model
62+
[component docs]: https://component-model.bytecodealliance.org/

Diff for: src/tools/wasm-component-ld/src/main.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// See the `README.md` in this directory for what this tool is.
2+
3+
// The source for this crate lives at
4+
// https://github.com./bytecodealliance/wasm-component-ld and the binary is
5+
// independently used in other projects such as `wasi-sdk` so the `main`
6+
// function is just reexported here to delegate. A Cargo dependency is used to
7+
// facilitate version management in the Rust repository and work well with
8+
// vendored/offline builds.
9+
use wasm_component_ld::main;

0 commit comments

Comments
 (0)