run-examples: Provide more error context for debugging

Use `anyhow` for nice errors and provide error context on commands that we run.
This commit is contained in:
Nick Fitzgerald
2020-07-10 13:51:58 -07:00
parent 2040a654d6
commit ec331a088c
3 changed files with 30 additions and 25 deletions

1
Cargo.lock generated
View File

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

View File

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

View File

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