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
166
#![warn(missing_docs, rust_2018_idioms, rust_2018_compatibility)]
#![warn(clippy::all)]
use std::{
env,
fs,
io,
path::{Path, PathBuf},
};
#[cfg(feature = "download")]
mod download;
#[cfg(feature = "download")]
pub use download::*;
fn get_target_dir() -> io::Result<PathBuf> {
let cur_exe = env::current_exe()?;
Ok(cur_exe.parent().unwrap().parent().unwrap().parent().unwrap().to_path_buf())
}
fn get_dylib_extension() -> Result<&'static str, env::VarError> {
let target = env::var("TARGET")?;
Ok(if target.contains("pc-windows") {
"dll"
} else {
"so"
})
}
#[derive(Debug)]
pub enum DyLibNameFilter<'a> {
FileName(&'a str),
Extension(&'a str),
LibName(&'a str),
}
pub fn install_dylibs<T: AsRef<Path> + ?Sized>(
from: &T,
filter: Option<DyLibNameFilter<'_>>,
target_dir: Option<&Path>,
) -> io::Result<()> {
use DyLibNameFilter::*;
let extension = if let Some(Extension(extension)) = filter {
".".to_string() + extension
} else if let Some(FileName(_)) = filter {
String::new()
} else {
get_dylib_extension().expect("Couldn't detect dylib extension").to_string()
};
let target_dir = if let Some(target_dir) = target_dir {
PathBuf::from(target_dir)
} else {
get_target_dir()?
};
for entry in fs::read_dir(from)? {
let entry_path = entry?.path();
if let Some(file_name) = entry_path.file_name() {
if let Some(file_name) = file_name.to_str() {
let matches = if let Some(FileName(target_fname)) = filter {
target_fname == file_name
} else if let Some(Extension(_)) = filter {
file_name.ends_with(extension.as_str())
} else if let Some(LibName(lib_name)) = filter {
file_name == lib_name.to_string() + &extension || file_name == "lib".to_string() + lib_name + &extension
} else {
file_name.ends_with(extension.as_str())
};
if matches {
fs::copy(entry_path.as_path(), target_dir.join(file_name).as_path())?;
}
}
}
}
Ok(())
}
pub fn add_link_search_path<T: AsRef<Path> + ?Sized>(path: &T) {
println!("cargo:rustc-link-search=all={}", path.as_ref().display());
}
#[cfg(test)]
mod tests {
use std::{
collections::HashMap,
fs,
path::Path,
vec::Vec,
};
pub(crate) fn dir_list_equals(path: &Path, list: Vec<&'static str>) -> bool {
let mut results = HashMap::new();
for entry in fs::read_dir(path).unwrap() {
let entry_path = entry.unwrap().path();
let file_name = entry_path.file_name().unwrap().to_str().unwrap();
*results.entry(file_name.to_string()).or_insert(0) += 1;
}
list.len() == results.len() && list.iter().all(|&x| *results.entry(x.to_string()).or_insert(0) == 1)
}
#[test]
fn install_dylibs_test() {
let cur_file = Path::new(file!());
let root = cur_file.parent().unwrap().parent().unwrap();
let out = root.join("target").join("test").join("install_dylibs");
let _ = fs::remove_dir_all(out.as_path());
let data_dir = root.join("test_input");
let dll_out = out.join("dll");
let so_out = out.join("so");
let fn_out = out.join("fn");
fs::create_dir_all(dll_out.as_path()).unwrap();
fs::create_dir_all(so_out.as_path()).unwrap();
fs::create_dir_all(fn_out.as_path()).unwrap();
use crate::*;
use crate::DyLibNameFilter::*;
install_dylibs(data_dir.as_path(), Some(Extension("dll")), Some(dll_out.as_path())).unwrap();
install_dylibs(data_dir.as_path(), Some(Extension("so")), Some(so_out.as_path())).unwrap();
install_dylibs(data_dir.as_path(), Some(FileName("dummy")), Some(fn_out.as_path())).unwrap();
assert_eq!(true, dir_list_equals(dll_out.as_path(), vec![ "dummy0.dll", "dummy1.dll" ]));
assert_eq!(true, dir_list_equals(so_out.as_path(), vec![ "dummy.so", "libdummy.so" ]));
assert_eq!(true, dir_list_equals(fn_out.as_path(), vec![ "dummy" ]));
}
}