2020-05-13 22:47:26 +04:00
|
|
|
use bindgen::callbacks::{EnumVariantValue, ParseCallbacks};
|
|
|
|
use heck::CamelCase;
|
2020-05-09 20:03:04 +04:00
|
|
|
use std::env;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
fn main() {
|
2020-05-10 22:38:23 +04:00
|
|
|
println!("cargo:rerun-if-env-changed=ONNXRUNTIME_LIB_DIR");
|
|
|
|
println!("cargo:rerun-if-env-changed=ONNXRUNTIME_INCLUDE_DIR");
|
|
|
|
|
|
|
|
match std::env::var("ONNXRUNTIME_LIB_DIR") {
|
2020-05-30 16:37:55 +04:00
|
|
|
Ok(dirs) => {
|
|
|
|
for dir in dirs.split(":") {
|
|
|
|
println!("cargo:rustc-link-search={}", dir);
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 22:38:23 +04:00
|
|
|
Err(_) => (),
|
|
|
|
};
|
|
|
|
|
2020-05-30 16:37:55 +04:00
|
|
|
let mut clang_args = Vec::new();
|
2020-05-10 22:38:23 +04:00
|
|
|
match std::env::var("ONNXRUNTIME_INCLUDE_DIR") {
|
2020-05-30 16:37:55 +04:00
|
|
|
Ok(dirs) => {
|
|
|
|
for dir in dirs.split(":") {
|
|
|
|
clang_args.push(format!("-I{}", dir));
|
|
|
|
}
|
2020-05-13 00:28:27 +04:00
|
|
|
}
|
2020-05-10 22:38:23 +04:00
|
|
|
Err(_) => (),
|
|
|
|
};
|
|
|
|
|
2020-05-10 15:08:30 +04:00
|
|
|
println!("cargo:rustc-link-lib=onnxruntime");
|
2020-05-09 20:03:04 +04:00
|
|
|
|
|
|
|
let bindings = bindgen::Builder::default()
|
2020-05-10 15:08:30 +04:00
|
|
|
.header("cbits/ort.h")
|
2020-05-30 16:37:55 +04:00
|
|
|
.clang_args(clang_args)
|
2020-05-09 20:03:04 +04:00
|
|
|
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
|
2020-05-10 15:20:23 +04:00
|
|
|
.whitelist_function("OrtGetApiBase")
|
|
|
|
.whitelist_var("ORT_.*")
|
|
|
|
.whitelist_recursively(true)
|
2020-05-13 22:47:26 +04:00
|
|
|
.blacklist_type("__int64_t")
|
|
|
|
.blacklist_type("__uint32_t")
|
|
|
|
.rustified_non_exhaustive_enum("*")
|
|
|
|
.parse_callbacks(Box::new(CustomEnums))
|
2020-05-16 13:42:41 +04:00
|
|
|
.no_copy(".*")
|
2020-05-10 15:08:30 +04:00
|
|
|
.layout_tests(false)
|
2020-05-09 20:03:04 +04:00
|
|
|
.generate()
|
|
|
|
.expect("Unable to generate bindings");
|
|
|
|
|
|
|
|
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
|
|
|
bindings
|
|
|
|
.write_to_file(out_path.join("bindings.rs"))
|
|
|
|
.expect("Couldn't write bindings!");
|
|
|
|
}
|
2020-05-13 22:47:26 +04:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct CustomEnums;
|
|
|
|
|
|
|
|
impl ParseCallbacks for CustomEnums {
|
2020-05-14 00:25:43 +04:00
|
|
|
fn item_name(&self, name: &str) -> Option<String> {
|
|
|
|
if name.starts_with("Ort") {
|
|
|
|
return Some(name.replace("Ort", ""));
|
|
|
|
}
|
|
|
|
|
|
|
|
if name.starts_with("ONNX") {
|
|
|
|
return Some(name.replace("ONNX", "Onnx"));
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-05-13 22:47:26 +04:00
|
|
|
fn enum_variant_name(
|
|
|
|
&self,
|
|
|
|
enum_name: Option<&str>,
|
|
|
|
variant_name: &str,
|
|
|
|
_variant_value: EnumVariantValue,
|
|
|
|
) -> Option<String> {
|
|
|
|
let mut variant_name = variant_name.to_camel_case();
|
|
|
|
|
|
|
|
if let Some(enum_name) = enum_name {
|
|
|
|
let enum_name = enum_name.replace("enum ", "").to_camel_case();
|
|
|
|
|
|
|
|
if variant_name.starts_with(&enum_name) {
|
|
|
|
variant_name = variant_name.replace(&enum_name, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if variant_name.starts_with("Ort") {
|
|
|
|
variant_name = variant_name.replace("Ort", "");
|
|
|
|
}
|
|
|
|
|
2020-05-14 00:25:43 +04:00
|
|
|
//Special case BFloat16
|
|
|
|
variant_name = variant_name.replace("Bfloat16", "BFloat16");
|
|
|
|
|
2020-05-13 22:47:26 +04:00
|
|
|
Some(variant_name)
|
|
|
|
}
|
|
|
|
}
|