Skip to content

Start supporting the wasm32-wasip2 target #892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/guest-rust/rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
extern crate alloc;

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

/// This function is called from generated bindings and will be deleted by
Expand All @@ -13,7 +14,7 @@ mod cabi_realloc;
///
/// For more information about this see `./ci/rebuild-libcabi-realloc.sh`.
pub fn maybe_link_cabi_realloc() {
#[cfg(target_family = "wasm")]
#[cfg(all(target_family = "wasm", not(target_env = "p2")))]
{
extern "C" {
fn cabi_realloc(
Expand Down Expand Up @@ -44,6 +45,7 @@ pub fn maybe_link_cabi_realloc() {
/// `cabi_realloc` module above. It's otherwise never explicitly called.
///
/// For more information about this see `./ci/rebuild-libcabi-realloc.sh`.
#[cfg(not(target_env = "p2"))]
pub unsafe fn cabi_realloc(
old_ptr: *mut u8,
old_len: usize,
Expand Down
2 changes: 1 addition & 1 deletion crates/guest-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ pub mod rt {
wit_bindgen_rt::maybe_link_cabi_realloc();
}

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

pub use crate::pre_wit_bindgen_0_20_0::*;
Expand Down
15 changes: 13 additions & 2 deletions crates/test-rust-wasm/artifacts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,28 @@ fn main() {

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

let target_to_test = match std::env::var("WIT_BINDGEN_WASI_TEST_TARGET") {
Ok(s) => s,
Err(_) => "wasm32-wasi".to_string(),
};

let mut cmd = Command::new("cargo");
cmd.arg("build")
.current_dir("../../test-rust-wasm")
.arg("--target=wasm32-wasi")
.arg("--target")
.arg(&target_to_test)
.env("CARGO_TARGET_DIR", &out_dir)
.env("CARGO_PROFILE_DEV_DEBUG", "1");
let status = cmd.status().unwrap();
assert!(status.success());

let mut wasms = Vec::new();
for file in out_dir.join("wasm32-wasi/debug").read_dir().unwrap() {
for file in out_dir
.join(&target_to_test)
.join("debug")
.read_dir()
.unwrap()
{
let file = file.unwrap().path();
if file.extension().and_then(|s| s.to_str()) != Some("wasm") {
continue;
Expand Down
25 changes: 15 additions & 10 deletions tests/runtime/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,23 @@ fn tests(name: &str, dir_name: &str) -> Result<Vec<PathBuf>> {
None => false,
})
.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"));
println!("rust core module = {core:?}");
let module = std::fs::read(&core)?;
let wasm = ComponentEncoder::default()
.module(&module)?
.validate(true)
.adapter("wasi_snapshot_preview1", &wasi_adapter)?
.realloc_via_memory_grow(true)
.encode()?;
let bytes = std::fs::read(&core)?;
let dst = if wasmparser::Parser::is_component(&bytes) {
PathBuf::from(core)
} else {
println!("rust core module = {core:?}");
let wasm = ComponentEncoder::default()
.module(&bytes)?
.validate(true)
.adapter("wasi_snapshot_preview1", &wasi_adapter)?
.realloc_via_memory_grow(true)
.encode()?;

let dst = out_dir.join("rust.wasm");
let dst = out_dir.join("rust.wasm");
std::fs::write(&dst, &wasm)?;
dst
};
println!("rust component {dst:?}");
std::fs::write(&dst, &wasm)?;
result.push(dst);
}

Expand Down