forked from liu-mengyang/rust-mmdeploy-sys
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
106 lines (82 loc) · 2.93 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
extern crate bindgen;
use std::io;
use std::env;
use std::path::PathBuf;
use std::fs;
struct Library {
name: &'static str,
}
static LIBRARIES: &[Library] = &[
Library {
name: "mmdeploy"
}
];
fn output() -> PathBuf {
PathBuf::from(env::var("OUT_DIR").unwrap())
}
fn build() -> io::Result<()> {
Ok(())
}
fn link_to_libraries() {
// let mmdeploy_ty = if statik { "static" } else { "dylib" };
let mmdeploy_ty = "dylib";
for lib in LIBRARIES {
println!("cargo:rustc-link-lib={}={}", mmdeploy_ty, lib.name);
}
}
fn search_include(include_paths: &[PathBuf], header: &str) -> String {
for dir in include_paths {
let include = dir.join(header);
if fs::metadata(&include).is_ok() {
return include.as_path().to_str().unwrap().to_string();
}
}
format!("/usr/include/{}", header)
}
fn main() {
// let statik = env::var("CARGO_FEATURE_STATIC").is_ok();
let include_path: Vec<PathBuf> = if env::var("CARGO_FEATURE_BUILD").is_ok() {
// TODO: Support static build mode.
println!("Static build mode");
vec![]
} else if let Ok(mmdeploy_dir) = env::var("MMDEPLOY_DIR") {
let mmdeploy_dir = PathBuf::from(mmdeploy_dir);
println!(
"cargo:rustc-link-search=native={}",
mmdeploy_dir.join("lib").to_string_lossy()
);
link_to_libraries();
vec![mmdeploy_dir.join("include")]
} else {
// TODO: Support pkg-config
println!("Fallback to pkg-config");
vec![]
};
let clang_includes = include_path
.iter()
.map(|include| format!("-I{}", include.to_string_lossy()));
// create builder
let mut builder = bindgen::Builder::default()
.clang_args(clang_includes);
builder = builder
.header(search_include(&include_path, "mmdeploy/common.h"))
.header(search_include(&include_path, "mmdeploy/model.h"))
.header(search_include(&include_path, "mmdeploy/executor.h"))
.header(search_include(&include_path, "mmdeploy/pipeline.h"))
.header(search_include(&include_path, "mmdeploy/classifier.h"))
.header(search_include(&include_path, "mmdeploy/detector.h"))
.header(search_include(&include_path, "mmdeploy/segmentor.h"))
.header(search_include(&include_path, "mmdeploy/pose_detector.h"))
.header(search_include(&include_path, "mmdeploy/rotated_detector.h"))
.header(search_include(&include_path, "mmdeploy/text_recognizer.h"))
.header(search_include(&include_path, "mmdeploy/text_detector.h"))
.header(search_include(&include_path, "mmdeploy/restorer.h"));
// generate builder
let bindings = builder
.generate()
.expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/bindings.rs file.
bindings
.write_to_file(output().join("bindings.rs"))
.expect("Couldn't write bindings!");
}