Skip to content

Commit f24522a

Browse files
committed
Auto merge of rust-lang#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 00167ab + 4cd6eee commit f24522a

File tree

9 files changed

+264
-3
lines changed

9 files changed

+264
-3
lines changed

Cargo.lock

Lines changed: 154 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,7 @@ dependencies = [
16711671
"compiler_builtins",
16721672
"rustc-std-workspace-alloc",
16731673
"rustc-std-workspace-core",
1674+
"serde",
16741675
]
16751676

16761677
[[package]]
@@ -1880,6 +1881,12 @@ dependencies = [
18801881
"syn 2.0.67",
18811882
]
18821883

1884+
[[package]]
1885+
name = "id-arena"
1886+
version = "2.2.1"
1887+
source = "registry+https://github.com/rust-lang/crates.io-index"
1888+
checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005"
1889+
18831890
[[package]]
18841891
name = "ident_case"
18851892
version = "1.0.1"
@@ -2109,6 +2116,12 @@ version = "1.0.5"
21092116
source = "registry+https://github.com/rust-lang/crates.io-index"
21102117
checksum = "db13adb97ab515a3691f56e4dbab09283d0b86cb45abd991d8634a9d6f501760"
21112118

2119+
[[package]]
2120+
name = "lexopt"
2121+
version = "0.3.0"
2122+
source = "registry+https://github.com/rust-lang/crates.io-index"
2123+
checksum = "baff4b617f7df3d896f97fe922b64817f6cd9a756bb81d40f8883f2f66dcb401"
2124+
21122125
[[package]]
21132126
name = "libc"
21142127
version = "0.2.155"
@@ -2623,7 +2636,7 @@ dependencies = [
26232636
"indexmap",
26242637
"memchr",
26252638
"ruzstd 0.5.0",
2626-
"wasmparser",
2639+
"wasmparser 0.118.2",
26272640
]
26282641

26292642
[[package]]
@@ -3425,7 +3438,7 @@ dependencies = [
34253438
"object 0.34.0",
34263439
"regex",
34273440
"similar",
3428-
"wasmparser",
3441+
"wasmparser 0.118.2",
34293442
]
34303443

34313444
[[package]]
@@ -3812,7 +3825,7 @@ dependencies = [
38123825
"thin-vec",
38133826
"thorin-dwp",
38143827
"tracing",
3815-
"wasm-encoder",
3828+
"wasm-encoder 0.200.0",
38163829
"windows",
38173830
]
38183831

@@ -5256,6 +5269,15 @@ dependencies = [
52565269
"color-eyre",
52575270
]
52585271

5272+
[[package]]
5273+
name = "spdx"
5274+
version = "0.10.6"
5275+
source = "registry+https://github.com/rust-lang/crates.io-index"
5276+
checksum = "47317bbaf63785b53861e1ae2d11b80d6b624211d42cb20efcd210ee6f8a14bc"
5277+
dependencies = [
5278+
"smallvec",
5279+
]
5280+
52595281
[[package]]
52605282
name = "spdx-expression"
52615283
version = "0.5.2"
@@ -6304,6 +6326,28 @@ version = "0.2.92"
63046326
source = "registry+https://github.com/rust-lang/crates.io-index"
63056327
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
63066328

6329+
[[package]]
6330+
name = "wasm-component-ld"
6331+
version = "0.5.4"
6332+
source = "registry+https://github.com/rust-lang/crates.io-index"
6333+
checksum = "314d932d5e84c9678751b85498b1482b2f32f185744e449d3ce0b1d400376dad"
6334+
dependencies = [
6335+
"anyhow",
6336+
"clap",
6337+
"lexopt",
6338+
"tempfile",
6339+
"wasmparser 0.210.0",
6340+
"wat",
6341+
"wit-component",
6342+
]
6343+
6344+
[[package]]
6345+
name = "wasm-component-ld-wrapper"
6346+
version = "0.1.0"
6347+
dependencies = [
6348+
"wasm-component-ld",
6349+
]
6350+
63076351
[[package]]
63086352
name = "wasm-encoder"
63096353
version = "0.200.0"
@@ -6313,6 +6357,40 @@ dependencies = [
63136357
"leb128",
63146358
]
63156359

6360+
[[package]]
6361+
name = "wasm-encoder"
6362+
version = "0.210.0"
6363+
source = "registry+https://github.com/rust-lang/crates.io-index"
6364+
checksum = "e7e3764d9d6edabd8c9e16195e177be0d20f6ab942ad18af52860f12f82bc59a"
6365+
dependencies = [
6366+
"leb128",
6367+
]
6368+
6369+
[[package]]
6370+
name = "wasm-encoder"
6371+
version = "0.211.1"
6372+
source = "registry+https://github.com/rust-lang/crates.io-index"
6373+
checksum = "5e7d931a1120ef357f32b74547646b6fa68ea25e377772b72874b131a9ed70d4"
6374+
dependencies = [
6375+
"leb128",
6376+
]
6377+
6378+
[[package]]
6379+
name = "wasm-metadata"
6380+
version = "0.210.0"
6381+
source = "registry+https://github.com/rust-lang/crates.io-index"
6382+
checksum = "012729d1294907fcb0866f08460ab95426a6d0b176a599619b84cac7653452b4"
6383+
dependencies = [
6384+
"anyhow",
6385+
"indexmap",
6386+
"serde",
6387+
"serde_derive",
6388+
"serde_json",
6389+
"spdx",
6390+
"wasm-encoder 0.210.0",
6391+
"wasmparser 0.210.0",
6392+
]
6393+
63166394
[[package]]
63176395
name = "wasmparser"
63186396
version = "0.118.2"
@@ -6323,6 +6401,42 @@ dependencies = [
63236401
"semver",
63246402
]
63256403

6404+
[[package]]
6405+
name = "wasmparser"
6406+
version = "0.210.0"
6407+
source = "registry+https://github.com/rust-lang/crates.io-index"
6408+
checksum = "a7bbcd21e7581619d9f6ca00f8c4f08f1cacfe58bf63f83af57cd0476f1026f5"
6409+
dependencies = [
6410+
"ahash",
6411+
"bitflags 2.5.0",
6412+
"hashbrown",
6413+
"indexmap",
6414+
"semver",
6415+
"serde",
6416+
]
6417+
6418+
[[package]]
6419+
name = "wast"
6420+
version = "211.0.1"
6421+
source = "registry+https://github.com/rust-lang/crates.io-index"
6422+
checksum = "b25506dd82d00da6b14a87436b3d52b1d264083fa79cdb72a0d1b04a8595ccaa"
6423+
dependencies = [
6424+
"bumpalo",
6425+
"leb128",
6426+
"memchr",
6427+
"unicode-width",
6428+
"wasm-encoder 0.211.1",
6429+
]
6430+
6431+
[[package]]
6432+
name = "wat"
6433+
version = "1.211.1"
6434+
source = "registry+https://github.com/rust-lang/crates.io-index"
6435+
checksum = "eb716ca6c86eecac2d82541ffc39860118fc0af9309c4f2670637bea2e1bdd7d"
6436+
dependencies = [
6437+
"wast",
6438+
]
6439+
63266440
[[package]]
63276441
name = "winapi"
63286442
version = "0.3.9"
@@ -6550,6 +6664,43 @@ dependencies = [
65506664
"memchr",
65516665
]
65526666

6667+
[[package]]
6668+
name = "wit-component"
6669+
version = "0.210.0"
6670+
source = "registry+https://github.com/rust-lang/crates.io-index"
6671+
checksum = "a450bdb5d032acf1fa0865451fa0c6f50e62f2d31eaa8dba967c2e2d068694a4"
6672+
dependencies = [
6673+
"anyhow",
6674+
"bitflags 2.5.0",
6675+
"indexmap",
6676+
"log",
6677+
"serde",
6678+
"serde_derive",
6679+
"serde_json",
6680+
"wasm-encoder 0.210.0",
6681+
"wasm-metadata",
6682+
"wasmparser 0.210.0",
6683+
"wit-parser",
6684+
]
6685+
6686+
[[package]]
6687+
name = "wit-parser"
6688+
version = "0.210.0"
6689+
source = "registry+https://github.com/rust-lang/crates.io-index"
6690+
checksum = "60a965cbd439af19a4b44a54a97ab8957d86f02d01320efc9e31c1d3605c6710"
6691+
dependencies = [
6692+
"anyhow",
6693+
"id-arena",
6694+
"indexmap",
6695+
"log",
6696+
"semver",
6697+
"serde",
6698+
"serde_derive",
6699+
"serde_json",
6700+
"unicode-xid",
6701+
"wasmparser 0.210.0",
6702+
]
6703+
65536704
[[package]]
65546705
name = "writeable"
65556706
version = "0.5.5"

Cargo.toml

Lines changed: 4 additions & 0 deletions
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

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,21 @@ impl Step for Assemble {
18291829
&self_contained_lld_dir.join(exe(name, target_compiler.host)),
18301830
);
18311831
}
1832+
1833+
// In addition to `rust-lld` also install `wasm-component-ld` when
1834+
// LLD is enabled. This is a relatively small binary that primarily
1835+
// delegates to the `rust-lld` binary for linking and then runs
1836+
// logic to create the final binary. This is used by the
1837+
// `wasm32-wasip2` target of Rust.
1838+
let wasm_component_ld_exe =
1839+
builder.ensure(crate::core::build_steps::tool::WasmComponentLd {
1840+
compiler: build_compiler.with_stage(0),
1841+
target: target_compiler.host,
1842+
});
1843+
builder.copy_link(
1844+
&wasm_component_ld_exe,
1845+
&libdir_bin.join(wasm_component_ld_exe.file_name().unwrap()),
1846+
);
18321847
}
18331848

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

src/bootstrap/src/core/build_steps/tool.rs

Lines changed: 1 addition & 0 deletions
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";
340341
);
341342

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

src/ci/docker/host-x86_64/dist-various-2/Dockerfile

Lines changed: 1 addition & 0 deletions
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

src/tools/tidy/src/deps.rs

Lines changed: 5 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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"

src/tools/wasm-component-ld/README.md

Lines changed: 62 additions & 0 deletions
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/
Lines changed: 9 additions & 0 deletions
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)