Skip to content

Commit 72009e5

Browse files
authored
Start supporting the wasm32-wasip2 target (#892)
This commit adds some initial support for `wasm32-wasip2` in `wit-bindgen`. Everything works as-is but one of the benefits of a new target is that we can move runtime bits such as `cabi_realloc` into the standard library rather than in this crate. This PR sets up infrastructure to test the `wasm32-wasip2` target and additionally assumes rust-lang/rust#122411 for providing the implementation of `cabi_realloc`.
1 parent 94dc94c commit 72009e5

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

crates/guest-rust/rt/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
extern crate alloc;
44

55
/// For more information about this see `./ci/rebuild-libcabi-realloc.sh`.
6+
#[cfg(not(target_env = "p2"))]
67
mod cabi_realloc;
78

89
/// This function is called from generated bindings and will be deleted by
@@ -13,7 +14,7 @@ mod cabi_realloc;
1314
///
1415
/// For more information about this see `./ci/rebuild-libcabi-realloc.sh`.
1516
pub fn maybe_link_cabi_realloc() {
16-
#[cfg(target_family = "wasm")]
17+
#[cfg(all(target_family = "wasm", not(target_env = "p2")))]
1718
{
1819
extern "C" {
1920
fn cabi_realloc(
@@ -44,6 +45,7 @@ pub fn maybe_link_cabi_realloc() {
4445
/// `cabi_realloc` module above. It's otherwise never explicitly called.
4546
///
4647
/// For more information about this see `./ci/rebuild-libcabi-realloc.sh`.
48+
#[cfg(not(target_env = "p2"))]
4749
pub unsafe fn cabi_realloc(
4850
old_ptr: *mut u8,
4951
old_len: usize,

crates/guest-rust/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ pub mod rt {
811811
wit_bindgen_rt::maybe_link_cabi_realloc();
812812
}
813813

814-
#[cfg(feature = "realloc")]
814+
#[cfg(all(feature = "realloc", not(target_env = "p2")))]
815815
pub use wit_bindgen_rt::cabi_realloc;
816816

817817
pub use crate::pre_wit_bindgen_0_20_0::*;

crates/test-rust-wasm/artifacts/build.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,28 @@ fn main() {
1010

1111
let wasi_adapter = manifest_dir.join("../../../tests/wasi_snapshot_preview1.reactor.wasm");
1212

13+
let target_to_test = match std::env::var("WIT_BINDGEN_WASI_TEST_TARGET") {
14+
Ok(s) => s,
15+
Err(_) => "wasm32-wasi".to_string(),
16+
};
17+
1318
let mut cmd = Command::new("cargo");
1419
cmd.arg("build")
1520
.current_dir("../../test-rust-wasm")
16-
.arg("--target=wasm32-wasi")
21+
.arg("--target")
22+
.arg(&target_to_test)
1723
.env("CARGO_TARGET_DIR", &out_dir)
1824
.env("CARGO_PROFILE_DEV_DEBUG", "1");
1925
let status = cmd.status().unwrap();
2026
assert!(status.success());
2127

2228
let mut wasms = Vec::new();
23-
for file in out_dir.join("wasm32-wasi/debug").read_dir().unwrap() {
29+
for file in out_dir
30+
.join(&target_to_test)
31+
.join("debug")
32+
.read_dir()
33+
.unwrap()
34+
{
2435
let file = file.unwrap().path();
2536
if file.extension().and_then(|s| s.to_str()) != Some("wasm") {
2637
continue;

tests/runtime/main.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,23 @@ fn tests(name: &str, dir_name: &str) -> Result<Vec<PathBuf>> {
166166
None => false,
167167
})
168168
.unwrap_or_else(|| panic!("failed to find wasm with name '{name}' - make sure to include '{name}.rs' module in crates/test-rust-wasm/src/bin directory"));
169-
println!("rust core module = {core:?}");
170-
let module = std::fs::read(&core)?;
171-
let wasm = ComponentEncoder::default()
172-
.module(&module)?
173-
.validate(true)
174-
.adapter("wasi_snapshot_preview1", &wasi_adapter)?
175-
.realloc_via_memory_grow(true)
176-
.encode()?;
169+
let bytes = std::fs::read(&core)?;
170+
let dst = if wasmparser::Parser::is_component(&bytes) {
171+
PathBuf::from(core)
172+
} else {
173+
println!("rust core module = {core:?}");
174+
let wasm = ComponentEncoder::default()
175+
.module(&bytes)?
176+
.validate(true)
177+
.adapter("wasi_snapshot_preview1", &wasi_adapter)?
178+
.realloc_via_memory_grow(true)
179+
.encode()?;
177180

178-
let dst = out_dir.join("rust.wasm");
181+
let dst = out_dir.join("rust.wasm");
182+
std::fs::write(&dst, &wasm)?;
183+
dst
184+
};
179185
println!("rust component {dst:?}");
180-
std::fs::write(&dst, &wasm)?;
181186
result.push(dst);
182187
}
183188

0 commit comments

Comments
 (0)