Files
wasmtime/crates/misc/run-examples/src/main.rs
Alex Crichton d452e5097f Build wasmtime-c-api differenty in run-examples (#1346)
* Build wasmtime-c-api differenty in run-examples

This tweaks how the wasmtime-c-api crate is built slightly, changing how
we invoke Cargo. Due to historical Cargo bugs this should help minimize
the number of rebuilds due to features since the feature selection will
be different.

* rustfmt
2020-03-17 15:08:35 -05:00

86 lines
2.6 KiB
Rust

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"])
.current_dir("crates/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);
}
}