Skip to content

Commit

Permalink
[Rust] Improve the error reporting in build.rs files by using anyhow. (
Browse files Browse the repository at this point in the history
…apache#6401)

* 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 <[email protected]>

* Format

* Fix build.rs

Co-authored-by: Greg Hale <[email protected]>
  • Loading branch information
jroesch and imalsogreg authored Sep 10, 2020
1 parent 3a4e61a commit b05aa96
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 55 deletions.
3 changes: 3 additions & 0 deletions apps/sgx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ edition = "2018"

[dependencies]
tvm-runtime = { path = "../../rust/runtime" }

[build-dependencies]
anyhow = "^1.0"
1 change: 0 additions & 1 deletion apps/wasm-standalone/wasm-graph/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@

fn main() {
let out_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/lib");

println!("cargo:rustc-link-search=native={}", out_dir);
}
1 change: 1 addition & 0 deletions rust/tvm-graph-rt/tests/test_nn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ tvm-graph-rt = { path = "../../" }

[build-dependencies]
ar = "0.6"
anyhow = "^1.0"
22 changes: 11 additions & 11 deletions rust/tvm-graph-rt/tests/test_nn/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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(),
Expand All @@ -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(())
}
1 change: 1 addition & 0 deletions rust/tvm-graph-rt/tests/test_tvm_basic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ tvm-rt = { path = "../../../tvm-rt" }

[build-dependencies]
ar = "0.6"
anyhow = "^1.0"
27 changes: 14 additions & 13 deletions rust/tvm-graph-rt/tests/test_tvm_basic/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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(())
}
4 changes: 4 additions & 0 deletions rust/tvm-graph-rt/tests/test_tvm_dso/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ edition = "2018"
[dependencies]
ndarray="0.12"
tvm-graph-rt = { path = "../../" }

[build-dependencies]
ar = "0.6"
anyhow = "^1.0"
20 changes: 12 additions & 8 deletions rust/tvm-graph-rt/tests/test_tvm_dso/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}",
Expand All @@ -39,4 +41,6 @@ fn main() {
.last()
.unwrap_or("")
);

Ok(())
}
1 change: 1 addition & 0 deletions rust/tvm-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ enumn = "^0.1"

[build-dependencies]
bindgen = { version="0.51", default-features=false }
anyhow = "^1.0"
55 changes: 38 additions & 17 deletions rust/tvm-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<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");
Expand All @@ -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(())
}
3 changes: 3 additions & 0 deletions rust/tvm/examples/resnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ ndarray = "0.12"
tvm = { path = "../../" }
image = "0.20"
csv = "1.1"

[build-dependencies]
anyhow = "^1.0"
9 changes: 7 additions & 2 deletions rust/tvm/examples/resnet/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}",
Expand All @@ -35,8 +37,11 @@ fn main() {
.last()
.unwrap_or("")
);

println!(
"cargo:rustc-link-search=native={}",
env!("CARGO_MANIFEST_DIR")
);

Ok(())
}
3 changes: 3 additions & 0 deletions rust/tvm/tests/basics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ edition = "2018"
ndarray = "0.12"
tvm = { path = "../../" }

[build-dependencies]
anyhow = "^1.0"

[features]
default = ["cpu"]
cpu = []
Expand Down
10 changes: 7 additions & 3 deletions rust/tvm/tests/basics/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -31,18 +33,20 @@ 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()
.unwrap_or("")
);

println!("cargo:rustc-link-search=native={}", out_dir);

Ok(())
}

0 comments on commit b05aa96

Please sign in to comment.