* 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>
[dependencies]
tvm-runtime = { path = "../../rust/runtime" }
+
+[build-dependencies]
+anyhow = "^1.0"
fn main() {
let out_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/lib");
-
println!("cargo:rustc-link-search=native={}", out_dir);
}
[build-dependencies]
ar = "0.6"
+anyhow = "^1.0"
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");
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(),
);
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(())
}
[build-dependencies]
ar = "0.6"
+anyhow = "^1.0"
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");
"/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(())
}
[dependencies]
ndarray="0.12"
tvm-graph-rt = { path = "../../" }
+
+[build-dependencies]
+ar = "0.6"
+anyhow = "^1.0"
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: {}",
.last()
.unwrap_or("")
);
+
+ Ok(())
}
[build-dependencies]
bindgen = { version="0.51", default-features=false }
+anyhow = "^1.0"
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");
.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(())
}
tvm = { path = "../../" }
image = "0.20"
csv = "1.1"
+
+[build-dependencies]
+anyhow = "^1.0"
* 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: {}",
.last()
.unwrap_or("")
);
+
println!(
"cargo:rustc-link-search=native={}",
env!("CARGO_MANIFEST_DIR")
);
+
+ Ok(())
}
ndarray = "0.12"
tvm = { path = "../../" }
+[build-dependencies]
+anyhow = "^1.0"
+
[features]
default = ["cpu"]
cpu = []
* 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");
&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()
);
println!("cargo:rustc-link-search=native={}", out_dir);
+
+ Ok(())
}