[Rust] Improve the error reporting in build.rs files by using anyhow. (#6401)
authorJared Roesch <jroesch@octoml.ai>
Thu, 10 Sep 2020 15:25:28 +0000 (08:25 -0700)
committerGitHub <noreply@github.com>
Thu, 10 Sep 2020 15:25:28 +0000 (08:25 -0700)
* Improve build.rs error handling.

Instead of just unwrapping use Result on main function, and use anyhow to add error context.

* Remove NDArray and Python changes

* Format

* Fix build.rs

* Apply suggestions from code review

Co-authored-by: Greg Hale <ImAlsoGreg@gmail.com>
* Format

* Fix build.rs

Co-authored-by: Greg Hale <ImAlsoGreg@gmail.com>
14 files changed:
apps/sgx/Cargo.toml
apps/wasm-standalone/wasm-graph/build.rs
rust/tvm-graph-rt/tests/test_nn/Cargo.toml
rust/tvm-graph-rt/tests/test_nn/build.rs
rust/tvm-graph-rt/tests/test_tvm_basic/Cargo.toml
rust/tvm-graph-rt/tests/test_tvm_basic/build.rs
rust/tvm-graph-rt/tests/test_tvm_dso/Cargo.toml
rust/tvm-graph-rt/tests/test_tvm_dso/build.rs
rust/tvm-sys/Cargo.toml
rust/tvm-sys/build.rs
rust/tvm/examples/resnet/Cargo.toml
rust/tvm/examples/resnet/build.rs
rust/tvm/tests/basics/Cargo.toml
rust/tvm/tests/basics/build.rs

index 93fae20..2f64f27 100644 (file)
@@ -23,3 +23,6 @@ edition = "2018"
 
 [dependencies]
 tvm-runtime = { path = "../../rust/runtime" }
+
+[build-dependencies]
+anyhow = "^1.0"
index 8fd4c3c..8f9b36d 100644 (file)
@@ -19,6 +19,5 @@
 
 fn main() {
     let out_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/lib");
-
     println!("cargo:rustc-link-search=native={}", out_dir);
 }
index 1b18fbb..a3ed362 100644 (file)
@@ -30,3 +30,4 @@ tvm-graph-rt = { path = "../../" }
 
 [build-dependencies]
 ar = "0.6"
+anyhow = "^1.0"
index 8ae1131..5cf4cc8 100644 (file)
@@ -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(())
 }
index c5a9064..7e86ef0 100644 (file)
@@ -29,3 +29,4 @@ tvm-rt = { path = "../../../tvm-rt" }
 
 [build-dependencies]
 ar = "0.6"
+anyhow = "^1.0"
index ade9e02..e1b4cfe 100644 (file)
@@ -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(())
 }
index dc7d9f6..1ff645b 100644 (file)
@@ -25,3 +25,7 @@ edition = "2018"
 [dependencies]
 ndarray="0.12"
 tvm-graph-rt = { path = "../../" }
+
+[build-dependencies]
+ar = "0.6"
+anyhow = "^1.0"
index f1d9822..1e3a9ab 100644 (file)
 
 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(())
 }
index faddce4..4e3fc98 100644 (file)
@@ -33,3 +33,4 @@ enumn = "^0.1"
 
 [build-dependencies]
 bindgen = { version="0.51", default-features=false }
+anyhow = "^1.0"
index 01d2934..05806c0 100644 (file)
 
 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::<Result<String>, _>(|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(())
 }
index e1f63a9..fd10569 100644 (file)
@@ -28,3 +28,6 @@ ndarray = "0.12"
 tvm = { path = "../../" }
 image = "0.20"
 csv = "1.1"
+
+[build-dependencies]
+anyhow = "^1.0"
index b9a3c4c..b259a62 100644 (file)
  * 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(())
 }
index dac9e46..421a7ae 100644 (file)
@@ -27,6 +27,9 @@ edition = "2018"
 ndarray = "0.12"
 tvm = { path = "../../" }
 
+[build-dependencies]
+anyhow = "^1.0"
+
 [features]
 default = ["cpu"]
 cpu = []
index 99e412c..6d5807c 100644 (file)
@@ -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(())
 }