-
Notifications
You must be signed in to change notification settings - Fork 41
/
build.rs
165 lines (142 loc) · 4.96 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
extern crate cc;
#[cfg(windows)]
mod windows {
use std::error::Error;
use std::{env, fmt, fs, io, process};
pub fn opencv_include() -> String {
if let Ok(dir) = env::var("OPENCV_DIR") {
format!("{}\\include", dir)
} else {
eprint!("%OPENCV_DIR% is not set.");
process::exit(0x0100);
}
}
pub fn opencv_link() {
if let Err(e) = try_opencv_link() {
eprint!("Error while building cv-rs: {:?}.", e);
process::exit(0x0100);
}
}
fn try_opencv_link() -> Result<(), Box<Error>> {
let opencv_dir = env::var("OPENCV_LIB")?;
let files = fs::read_dir(&opencv_dir)?.collect::<Vec<_>>();
let opencv_world = get_opencv_lib_path(files.iter(), "world")?;
let img_hash = get_opencv_lib_path(files.iter(), "img_hash")?;
println!("cargo:rustc-link-search=native={}", opencv_dir);
println!("cargo:rustc-link-lib={}", opencv_world);
println!("cargo:rustc-link-lib={}", img_hash);
Ok(())
}
fn get_opencv_lib_path<'a, T: Iterator<Item = &'a io::Result<fs::DirEntry>>>(
files: T,
name: &str,
) -> Result<String, Box<Error>> {
let opencv_world_entry = files.filter_map(|entry| entry.as_ref().ok()).find(|entry| {
let file_name = entry.file_name().to_string_lossy().into_owned();
(file_name.starts_with(&format!("opencv_{}", name))
|| file_name.starts_with(&format!("libopencv_{}", name)))
&& !file_name.ends_with("d.lib")
});
let lib = opencv_world_entry.ok_or_else(|| {
BuildError::new(format!(
"Cannot find opencv_{} file in provided %OPENCV_LIB% directory",
name
))
})?;
let lib = lib.file_name();
let lib = lib
.into_string()
.map_err(|e| BuildError::new(format!("Cannot convert path '{:?}' to string", e)))?;
// we expect filename to be something like 'open_world340.lib' or
// 'open_world.340.dll.a', so we just consider everything after the
// version number is an extension
let lib_without_extension = lib.trim_end_matches(|c: char| !c.is_numeric());
Ok(lib_without_extension.into())
}
#[derive(Debug)]
struct BuildError {
details: String,
}
impl BuildError {
fn new<T: Into<String>>(details: T) -> Self {
Self {
details: details.into(),
}
}
}
impl Error for BuildError {
fn description(&self) -> &str {
&self.details
}
}
impl fmt::Display for BuildError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.details)
}
}
}
#[cfg(unix)]
mod unix {
use std::env;
pub fn opencv_include() -> String {
if let Ok(dir) = env::var("OPENCV_DIR") {
format!("{}/include", dir)
} else {
"/usr/local/include".into()
}
}
pub fn opencv_link() {
let cargo_rustc_link_search = env::var("OPENCV_LIB").unwrap_or("/usr/local/lib".into());
println!("cargo:rustc-link-search=native={}", cargo_rustc_link_search);
println!("cargo:rustc-link-lib=opencv_core");
println!("cargo:rustc-link-lib=opencv_features2d");
println!("cargo:rustc-link-lib=opencv_xfeatures2d");
println!("cargo:rustc-link-lib=opencv_highgui");
println!("cargo:rustc-link-lib=opencv_img_hash");
println!("cargo:rustc-link-lib=opencv_imgcodecs");
println!("cargo:rustc-link-lib=opencv_imgproc");
println!("cargo:rustc-link-lib=opencv_objdetect");
if cfg!(feature = "text") {
println!("cargo:rustc-link-lib=opencv_text");
}
println!("cargo:rustc-link-lib=opencv_videoio");
println!("cargo:rustc-link-lib=opencv_video");
if cfg!(feature = "cuda") {
println!("cargo:rustc-link-lib=opencv_cudaobjdetect");
}
}
}
#[cfg(windows)]
use windows::*;
#[cfg(unix)]
use unix::*;
fn main() {
let files = get_files("native");
let mut opencv_config = cc::Build::new();
opencv_config
.cpp(true)
.files(files)
.include("native")
.include(opencv_include());
if cfg!(not(target_env = "msvc")) {
opencv_config.flag("--std=c++11");
}
if cfg!(feature = "text") {
let text_files = get_files("native/text");
opencv_config.files(text_files);
}
if cfg!(feature = "cuda") {
let cuda_files = get_files("native/cuda");
opencv_config.files(cuda_files);
}
opencv_config.compile("libopencv-wrapper.a");
opencv_link();
}
fn get_files(path: &str) -> Vec<std::path::PathBuf> {
std::fs::read_dir(path)
.unwrap()
.into_iter()
.filter_map(|x| x.ok().map(|x| x.path()))
.filter(|x| x.extension().map(|e| e == "cc").unwrap_or(false))
.collect::<Vec<_>>()
}