Skip to content

Commit 86db399

Browse files
author
Pat Hickey
committed
test-helpers: build rust guests with wasm32-unknown-unknown and assert they encode as components
except for "invalid" and "handles" which are not gonna work
1 parent db6de38 commit 86db399

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

crates/test-helpers/build.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fs;
33
use std::path::PathBuf;
44
use std::process::Command;
55
use wit_bindgen_core::{wit_parser::Interface, Direction, Generator};
6+
use wit_component::ComponentEncoder;
67

78
fn main() {
89
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
@@ -13,14 +14,18 @@ fn main() {
1314
let mut cmd = Command::new("cargo");
1415
cmd.arg("build")
1516
.current_dir("../test-rust-wasm")
16-
.arg("--target=wasm32-wasi")
17+
.arg("--target=wasm32-unknown-unknown")
1718
.env("CARGO_TARGET_DIR", &out_dir)
1819
.env("CARGO_PROFILE_DEV_DEBUG", "1")
1920
.env("RUSTFLAGS", "-Clink-args=--export-table")
2021
.env_remove("CARGO_ENCODED_RUSTFLAGS");
2122
let status = cmd.status().unwrap();
2223
assert!(status.success());
23-
for file in out_dir.join("wasm32-wasi/debug").read_dir().unwrap() {
24+
for file in out_dir
25+
.join("wasm32-unknown-unknown/debug")
26+
.read_dir()
27+
.unwrap()
28+
{
2429
let file = file.unwrap().path();
2530
if file.extension().and_then(|s| s.to_str()) != Some("wasm") {
2631
continue;
@@ -31,6 +36,26 @@ fn main() {
3136
file.to_str().unwrap().to_string(),
3237
));
3338

39+
// TODO: add an adapter for snapshot 1, and go back to compiling this target for
40+
// wasm32-wasi
41+
let module = fs::read(&file).expect("failed to read wasm file");
42+
match ComponentEncoder::default()
43+
.module(module.as_slice())
44+
.expect("pull custom sections from module")
45+
.validate(true)
46+
.encode()
47+
{
48+
Ok(_) => {}
49+
Err(e) => {
50+
if !["invalid", "handles"]
51+
.iter()
52+
.any(|s| *s == file.file_stem().unwrap().to_str().unwrap())
53+
{
54+
panic!("component encoder for {:?} failed: {:?}", file, e);
55+
}
56+
}
57+
}
58+
3459
let dep_file = file.with_extension("d");
3560
let deps = fs::read_to_string(&dep_file).expect("failed to read dep file");
3661
for dep in deps

0 commit comments

Comments
 (0)