diff --git a/apps/sgx/Cargo.toml b/apps/sgx/Cargo.toml index 93fae202abee..2f64f2718b9c 100644 --- a/apps/sgx/Cargo.toml +++ b/apps/sgx/Cargo.toml @@ -23,3 +23,6 @@ edition = "2018" [dependencies] tvm-runtime = { path = "../../rust/runtime" } + +[build-dependencies] +anyhow = "^1.0" diff --git a/apps/wasm-standalone/wasm-graph/build.rs b/apps/wasm-standalone/wasm-graph/build.rs index 8fd4c3c411fc..8f9b36df3379 100644 --- a/apps/wasm-standalone/wasm-graph/build.rs +++ b/apps/wasm-standalone/wasm-graph/build.rs @@ -19,6 +19,5 @@ fn main() { let out_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/lib"); - println!("cargo:rustc-link-search=native={}", out_dir); } diff --git a/rust/tvm-graph-rt/tests/test_nn/Cargo.toml b/rust/tvm-graph-rt/tests/test_nn/Cargo.toml index 1b18fbbe41ec..a3ed3624a3b9 100644 --- a/rust/tvm-graph-rt/tests/test_nn/Cargo.toml +++ b/rust/tvm-graph-rt/tests/test_nn/Cargo.toml @@ -30,3 +30,4 @@ tvm-graph-rt = { path = "../../" } [build-dependencies] ar = "0.6" +anyhow = "^1.0" diff --git a/rust/tvm-graph-rt/tests/test_nn/build.rs b/rust/tvm-graph-rt/tests/test_nn/build.rs index 8ae1131a5572..5cf4cc848afe 100644 --- a/rust/tvm-graph-rt/tests/test_nn/build.rs +++ b/rust/tvm-graph-rt/tests/test_nn/build.rs @@ -21,15 +21,16 @@ extern crate ar; use std::{env, fs::File, path::Path, process::Command}; +use anyhow::{Context, Result}; use ar::Builder; -fn main() { - let out_dir = env::var("OUT_DIR").unwrap(); +fn main() -> Result<()> { + let out_dir = env::var("OUT_DIR")?; let out_dir = Path::new(&out_dir).join("test_nn"); - std::fs::create_dir_all(&out_dir).unwrap(); + std::fs::create_dir_all(&out_dir)?; - let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let manifest_dir = env::var("CARGO_MANIFEST_DIR")?; let manifest_dir = Path::new(&manifest_dir); let generator = manifest_dir.join("src").join("build_test_graph.py"); @@ -39,7 +40,7 @@ fn main() { let output = Command::new(&generator) .arg(&out_dir) .output() - .expect("Failed to execute command"); + .with_context(|| format!("Failed to execute: {:?}", generator))?; assert!( graph_path.exists(), @@ -53,18 +54,17 @@ fn main() { ); let lib_file = out_dir.join("libtestnn.a"); - let file = File::create(&lib_file).unwrap(); + let file = File::create(&lib_file).context("failed to create library file")?; let mut builder = Builder::new(file); - builder.append_path(graph_path).unwrap(); + builder.append_path(graph_path)?; - let status = Command::new("ranlib") - .arg(&lib_file) - .status() - .expect("fdjlksafjdsa"); + let status = Command::new("ranlib").arg(&lib_file).status()?; assert!(status.success()); println!("cargo:rustc-link-lib=static=testnn"); println!("cargo:rustc-link-search=native={}", out_dir.display()); println!("cargo:rerun-if-changed={}", generator.display()); + + Ok(()) } diff --git a/rust/tvm-graph-rt/tests/test_tvm_basic/Cargo.toml b/rust/tvm-graph-rt/tests/test_tvm_basic/Cargo.toml index c5a9064b7d86..7e86ef046356 100644 --- a/rust/tvm-graph-rt/tests/test_tvm_basic/Cargo.toml +++ b/rust/tvm-graph-rt/tests/test_tvm_basic/Cargo.toml @@ -29,3 +29,4 @@ tvm-rt = { path = "../../../tvm-rt" } [build-dependencies] ar = "0.6" +anyhow = "^1.0" diff --git a/rust/tvm-graph-rt/tests/test_tvm_basic/build.rs b/rust/tvm-graph-rt/tests/test_tvm_basic/build.rs index ade9e0297c9e..e1b4cfea74d5 100644 --- a/rust/tvm-graph-rt/tests/test_tvm_basic/build.rs +++ b/rust/tvm-graph-rt/tests/test_tvm_basic/build.rs @@ -21,15 +21,17 @@ extern crate ar; use std::{path::PathBuf, process::Command}; -use ar::Builder; use std::fs::File; -fn main() { +use anyhow::Result; +use ar::Builder; + +fn main() -> Result<()> { let mut out_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); out_dir.push("lib"); if !out_dir.is_dir() { - std::fs::create_dir(&out_dir).unwrap(); + std::fs::create_dir(&out_dir)?; } let obj_file = out_dir.join("test.o"); @@ -40,30 +42,29 @@ fn main() { "/src/build_test_lib.py" )) .arg(&out_dir) - .output() - .expect("Failed to execute command"); + .output()?; + assert!( obj_file.exists(), "Could not build tvm lib: {}", - String::from_utf8(output.stderr) - .unwrap() + String::from_utf8(output.stderr)? .trim() .split("\n") .last() .unwrap_or("") ); - let mut builder = Builder::new(File::create(&lib_file).unwrap()); - builder.append_path(&obj_file).unwrap(); + let mut builder = Builder::new(File::create(&lib_file)?); + builder.append_path(&obj_file)?; + drop(builder); - let status = Command::new("ranlib") - .arg(&lib_file) - .status() - .expect("fdjlksafjdsa"); + let status = Command::new("ranlib").arg(&lib_file).status()?; assert!(status.success()); println!("cargo:rustc-link-lib=static=test_basic"); println!("cargo:rustc-link-search=native={}", out_dir.display()); + + Ok(()) } diff --git a/rust/tvm-graph-rt/tests/test_tvm_dso/Cargo.toml b/rust/tvm-graph-rt/tests/test_tvm_dso/Cargo.toml index dc7d9f63f234..1ff645b9c400 100644 --- a/rust/tvm-graph-rt/tests/test_tvm_dso/Cargo.toml +++ b/rust/tvm-graph-rt/tests/test_tvm_dso/Cargo.toml @@ -25,3 +25,7 @@ edition = "2018" [dependencies] ndarray="0.12" tvm-graph-rt = { path = "../../" } + +[build-dependencies] +ar = "0.6" +anyhow = "^1.0" diff --git a/rust/tvm-graph-rt/tests/test_tvm_dso/build.rs b/rust/tvm-graph-rt/tests/test_tvm_dso/build.rs index f1d9822b01a5..1e3a9ab0770b 100644 --- a/rust/tvm-graph-rt/tests/test_tvm_dso/build.rs +++ b/rust/tvm-graph-rt/tests/test_tvm_dso/build.rs @@ -19,16 +19,18 @@ use std::{env, path::Path, process::Command}; -fn main() { +use anyhow::{Context, Result}; + +fn main() -> Result<()> { let out_dir = env::var("OUT_DIR").unwrap(); - let output = Command::new(concat!( - env!("CARGO_MANIFEST_DIR"), - "/src/build_test_lib.py" - )) - .arg(&out_dir) - .output() - .expect("Failed to execute command"); + let exe = concat!(env!("CARGO_MANIFEST_DIR"), "/src/build_test_lib.py"); + + let output = Command::new(exe) + .arg(&out_dir) + .output() + .with_context(|| anyhow::anyhow!("Failed to execute: {} {}", exe, &out_dir))?; + assert!( Path::new(&format!("{}/test.so", out_dir)).exists(), "Could not build tvm lib: {}", @@ -39,4 +41,6 @@ fn main() { .last() .unwrap_or("") ); + + Ok(()) } diff --git a/rust/tvm-sys/Cargo.toml b/rust/tvm-sys/Cargo.toml index faddce48d15d..4e3fc98b4e75 100644 --- a/rust/tvm-sys/Cargo.toml +++ b/rust/tvm-sys/Cargo.toml @@ -33,3 +33,4 @@ enumn = "^0.1" [build-dependencies] bindgen = { version="0.51", default-features=false } +anyhow = "^1.0" diff --git a/rust/tvm-sys/build.rs b/rust/tvm-sys/build.rs index 01d2934855c9..05806c0d5ce0 100644 --- a/rust/tvm-sys/build.rs +++ b/rust/tvm-sys/build.rs @@ -19,24 +19,43 @@ extern crate bindgen; +use std::env; use std::path::PathBuf; -use std::env; +use anyhow::{Context, Result}; -fn main() { - let tvm_home = option_env!("TVM_HOME").map(str::to_string).unwrap_or({ - let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .canonicalize() - .unwrap(); - crate_dir - .parent() - .unwrap() - .parent() - .unwrap() - .to_str() - .unwrap() - .to_string() - }); +fn main() -> Result<()> { + let tvm_home = option_env!("TVM_HOME") + .map::, _>(|s: &str| Ok(str::to_string(s))) + .unwrap_or_else(|| { + let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .canonicalize() + .with_context(|| { + format!( + "failed to cannonicalize() CARGO_MANIFEST_DIR={}", + env!("CARGO_MANIFEST_DIR") + ) + })?; + + Ok(crate_dir + .parent() + .with_context(|| { + format!( + "failed to find parent of CARGO_MANIFEST_DIR={}", + env!("CARGO_MANIFEST_DIR") + ) + })? + .parent() + .with_context(|| { + format!( + "failed to find the parent of the parent of CARGO MANIFEST_DIR={}", + env!("CARGO_MANIFEST_DIR") + ) + })? + .to_str() + .context("failed to convert to strings")? + .to_string()) + })?; if cfg!(feature = "bindings") { println!("cargo:rerun-if-env-changed=TVM_HOME"); @@ -56,7 +75,9 @@ fn main() { .derive_eq(true) .derive_default(true) .generate() - .expect("unable to generate bindings") + .map_err(|()| anyhow::anyhow!("failed to generate bindings"))? .write_to_file(PathBuf::from("src/c_runtime_api.rs")) - .expect("can not write the bindings!"); + .context("failed to write bindings")?; + + Ok(()) } diff --git a/rust/tvm/examples/resnet/Cargo.toml b/rust/tvm/examples/resnet/Cargo.toml index e1f63a9d3e58..fd10569869d5 100644 --- a/rust/tvm/examples/resnet/Cargo.toml +++ b/rust/tvm/examples/resnet/Cargo.toml @@ -28,3 +28,6 @@ ndarray = "0.12" tvm = { path = "../../" } image = "0.20" csv = "1.1" + +[build-dependencies] +anyhow = "^1.0" diff --git a/rust/tvm/examples/resnet/build.rs b/rust/tvm/examples/resnet/build.rs index b9a3c4ccdf12..b259a626eb5e 100644 --- a/rust/tvm/examples/resnet/build.rs +++ b/rust/tvm/examples/resnet/build.rs @@ -17,14 +17,16 @@ * under the License. */ +use anyhow::{Context, Result}; use std::{path::Path, process::Command}; -fn main() { +fn main() -> Result<()> { let output = Command::new("python3") .arg(concat!(env!("CARGO_MANIFEST_DIR"), "/src/build_resnet.py")) .arg(&format!("--build-dir={}", env!("CARGO_MANIFEST_DIR"))) .output() - .expect("Failed to execute command"); + .with_context(|| anyhow::anyhow!("failed to run python3"))?; + assert!( Path::new(&format!("{}/deploy_lib.o", env!("CARGO_MANIFEST_DIR"))).exists(), "Could not prepare demo: {}", @@ -35,8 +37,11 @@ fn main() { .last() .unwrap_or("") ); + println!( "cargo:rustc-link-search=native={}", env!("CARGO_MANIFEST_DIR") ); + + Ok(()) } diff --git a/rust/tvm/tests/basics/Cargo.toml b/rust/tvm/tests/basics/Cargo.toml index dac9e4698a9c..421a7ae3cb7d 100644 --- a/rust/tvm/tests/basics/Cargo.toml +++ b/rust/tvm/tests/basics/Cargo.toml @@ -27,6 +27,9 @@ edition = "2018" ndarray = "0.12" tvm = { path = "../../" } +[build-dependencies] +anyhow = "^1.0" + [features] default = ["cpu"] cpu = [] diff --git a/rust/tvm/tests/basics/build.rs b/rust/tvm/tests/basics/build.rs index 99e412c5c46a..6d5807c3d419 100644 --- a/rust/tvm/tests/basics/build.rs +++ b/rust/tvm/tests/basics/build.rs @@ -17,7 +17,9 @@ * under the License. */ -fn main() { +use anyhow::{Context, Result}; + +fn main() -> Result<()> { let out_dir = std::env::var("OUT_DIR").unwrap(); let tvm_mk_add = concat!(env!("CARGO_MANIFEST_DIR"), "/src/tvm_add.py"); @@ -31,13 +33,13 @@ fn main() { &std::env::var("OUT_DIR").unwrap(), ]) .output() - .expect("Failed to execute command"); + .with_context(|| anyhow::anyhow!(tvm_mk_add))?; assert!( std::path::Path::new(&format!("{}/test_add.so", out_dir)).exists(), "Could not build tvm lib: {}", String::from_utf8(output.stderr) - .unwrap() + .context("utf-8 conversion failed")? .trim() .split("\n") .last() @@ -45,4 +47,6 @@ fn main() { ); println!("cargo:rustc-link-search=native={}", out_dir); + + Ok(()) }