Browse Source

run-examples: Provide more error context for debugging

Use `anyhow` for nice errors and provide error context on commands that we run.
pull/1996/head
Nick Fitzgerald 4 years ago
parent
commit
ec331a088c
  1. 1
      Cargo.lock
  2. 1
      crates/misc/run-examples/Cargo.toml
  3. 53
      crates/misc/run-examples/src/main.rs

1
Cargo.lock

@ -1742,6 +1742,7 @@ dependencies = [
name = "run-examples"
version = "0.18.0"
dependencies = [
"anyhow",
"cc",
]

1
crates/misc/run-examples/Cargo.toml

@ -6,4 +6,5 @@ edition = "2018"
publish = false
[dependencies]
anyhow = "1.0.31"
cc = "1.0"

53
crates/misc/run-examples/src/main.rs

@ -1,26 +1,25 @@
use anyhow::Context;
use std::collections::BTreeSet;
use std::process::Command;
fn main() {
fn main() -> anyhow::Result<()> {
let example_to_run = std::env::args().nth(1);
let examples = std::fs::read_dir("examples").unwrap();
let examples = examples
.filter_map(|e| {
let e = e.unwrap();
let path = e.path();
let dir = e.metadata().unwrap().is_dir();
if let Some("wat") = path.extension().and_then(|s| s.to_str()) {
return None;
}
let mut examples = BTreeSet::new();
for e in std::fs::read_dir("examples")? {
let e = e?;
let path = e.path();
let dir = e.metadata()?.is_dir();
if let Some("wat") = path.extension().and_then(|s| s.to_str()) {
continue;
}
Some((path.file_stem().unwrap().to_str().unwrap().to_owned(), dir))
})
.collect::<BTreeSet<_>>();
examples.insert((path.file_stem().unwrap().to_str().unwrap().to_owned(), dir));
}
println!("======== Building libwasmtime.a ===========");
run(Command::new("cargo")
.args(&["build"])
.current_dir("crates/c-api"));
.current_dir("crates/c-api"))?;
for (example, is_dir) in examples {
if example == "README" {
@ -43,13 +42,13 @@ fn main() {
.arg("-p")
.arg(format!("example-{}-wasm", example))
.arg("--target")
.arg(target));
.arg(target))?;
}
println!("======== Rust example `{}` ============", example);
run(Command::new("cargo")
.arg("run")
.arg("--example")
.arg(&example));
.arg(&example))?;
println!("======== C/C++ example `{}` ============", example);
for extension in ["c", "cc"].iter() {
@ -93,18 +92,22 @@ fn main() {
if cfg!(target_os = "linux") {
cmd.arg("-lpthread").arg("-ldl").arg("-lm");
}
run(&mut cmd);
run(&mut cmd)?;
run(&mut Command::new(exe));
run(&mut Command::new(exe))?;
}
}
Ok(())
}
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);
}
fn run(cmd: &mut Command) -> anyhow::Result<()> {
(|| -> anyhow::Result<()> {
let s = cmd.status()?;
if !s.success() {
anyhow::bail!("Exited with failure status: {}", s);
}
Ok(())
})()
.with_context(|| format!("failed to run `{:?}`", cmd))
}

Loading…
Cancel
Save