Move all examples to a top-level directory (#1286)
* Move all examples to a top-level directory This commit moves all API examples (Rust and C) to a top-level `examples` directory. This is intended to make it more discoverable and conventional as to where examples are located. Additionally all examples are now available in both Rust and C to see how to execute the example in the language you're familiar with. The intention is that as more languages are supported we'd add more languages as examples here too. Each example is also accompanied by either a `*.wat` file which is parsed as input, or a Rust project in a `wasm` folder which is compiled as input. A simple driver crate was also added to `crates/misc` which executes all the examples on CI, ensuring the C and Rust examples all execute successfully.
This commit is contained in:
83
crates/misc/run-examples/src/main.rs
Normal file
83
crates/misc/run-examples/src/main.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
use std::collections::BTreeSet;
|
||||
use std::process::Command;
|
||||
|
||||
fn main() {
|
||||
let examples = std::fs::read_dir("examples").unwrap();
|
||||
let examples = examples
|
||||
.map(|e| {
|
||||
let e = e.unwrap();
|
||||
let path = e.path();
|
||||
let dir = e.metadata().unwrap().is_dir();
|
||||
(path.file_stem().unwrap().to_str().unwrap().to_owned(), dir)
|
||||
})
|
||||
.collect::<BTreeSet<_>>();
|
||||
|
||||
println!("======== Building libwasmtime.a ===========");
|
||||
run(Command::new("cargo").args(&["build", "-p", "wasmtime-c-api"]));
|
||||
|
||||
for (example, is_dir) in examples {
|
||||
if example == "README" {
|
||||
continue;
|
||||
}
|
||||
if is_dir {
|
||||
println!("======== Rust wasm file `{}` ============", example);
|
||||
run(Command::new("cargo")
|
||||
.arg("build")
|
||||
.arg("-p")
|
||||
.arg(format!("example-{}-wasm", example))
|
||||
.arg("--target")
|
||||
.arg("wasm32-unknown-unknown"));
|
||||
}
|
||||
println!("======== Rust example `{}` ============", example);
|
||||
run(Command::new("cargo")
|
||||
.arg("run")
|
||||
.arg("--example")
|
||||
.arg(&example));
|
||||
|
||||
println!("======== C example `{}` ============", example);
|
||||
let mut cmd = cc::Build::new()
|
||||
.opt_level(0)
|
||||
.cargo_metadata(false)
|
||||
.target(env!("TARGET"))
|
||||
.host(env!("TARGET"))
|
||||
.include("crates/c-api/include")
|
||||
.include("crates/c-api/wasm-c-api/include")
|
||||
.define("WASM_API_EXTERN", Some("")) // static linkage, not dynamic
|
||||
.warnings(false)
|
||||
.get_compiler()
|
||||
.to_command();
|
||||
if is_dir {
|
||||
cmd.arg(format!("examples/{}/main.c", example));
|
||||
} else {
|
||||
cmd.arg(format!("examples/{}.c", example));
|
||||
}
|
||||
let exe = if cfg!(windows) {
|
||||
cmd.arg("target/debug/wasmtime.lib")
|
||||
.arg("ws2_32.lib")
|
||||
.arg("advapi32.lib")
|
||||
.arg("userenv.lib")
|
||||
.arg("ntdll.lib")
|
||||
.arg("shell32.lib")
|
||||
.arg("ole32.lib");
|
||||
"./main.exe"
|
||||
} else {
|
||||
cmd.arg("target/debug/libwasmtime.a").arg("-o").arg("foo");
|
||||
"./foo"
|
||||
};
|
||||
if cfg!(target_os = "linux") {
|
||||
cmd.arg("-lpthread").arg("-ldl").arg("-lm");
|
||||
}
|
||||
run(&mut cmd);
|
||||
|
||||
run(&mut Command::new(exe));
|
||||
}
|
||||
}
|
||||
|
||||
fn run(cmd: &mut Command) {
|
||||
let s = cmd.status().unwrap();
|
||||
if !s.success() {
|
||||
eprintln!("failed to run {:?}", cmd);
|
||||
eprintln!("status: {}", s);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user