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
use std::path::Path;
use std::{fs, io};
fn main() {
// prevent running for docs.rs
#[cfg(not(feature = "docsrs"))]
{
/*
Shared libraries need to be within the target dir, see
https://doc.rust-lang.org/cargo/reference/environment-variables.html#dynamic-library-paths
Therefore, libraries are first extracted from `vzense-lib.tar.xz` to `targst/vzense_lib/` and then symlinked from there to target/<buildType>/deps/.
*/
let lib_src = std::env::current_dir().unwrap().join("target/vzense-lib");
// check if libraries have been extracted already
if !lib_src.exists() {
let base_path = std::env::current_dir().unwrap();
let target_path = base_path.join("target");
if !target_path.exists() {
fs::create_dir(target_path.clone()).unwrap();
}
std::fs::copy(
base_path.join("vzense-lib.tar.xz"),
target_path.join("vzense-lib.tar.xz"),
)
.expect(&format!("could not copy vzense-lib {:?}", base_path.join("vzense-lib.tar.xz")));
// decompress the vzense-lib directory
std::process::Command::new("unxz")
.current_dir(target_path.clone())
.arg("vzense-lib.tar.xz")
.output()
.expect("could not unxz vzense-lib");
// untar
std::process::Command::new("tar")
.current_dir(target_path.clone())
.arg("-xf")
.arg("vzense-lib.tar")
.output()
.expect("could not untar vzense-lib");
// rm tar
// std::process::Command::new("rm")
// .current_dir(target_path)
// .arg("vzense-lib.tar")
// .output()
// .unwrap();
}
let mut deps_path = std::env::current_exe().unwrap();
for _ in 0..3 {
deps_path.pop(); // 3 x cd ..
}
deps_path.push("deps"); // cd deps
// create symlinks
symlink_dir_all(lib_src, deps_path.clone())
.expect("failed to create symlinks to libraries");
// tell cargo to look for shared libraries in the specified directory
println!("cargo:rustc-link-search={}", deps_path.to_str().unwrap());
// necessary for runtime to find shared libraries
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{}",
deps_path.to_str().unwrap()
);
// tell rustc to link the shared libraries
println!("cargo:rustc-link-lib=vzense_api");
println!("cargo:rustc-link-lib=Scepter_api");
}
/// create symlinks recursively to whole directory
fn symlink_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let file = dst.as_ref().join(entry.file_name());
let ty = entry.file_type()?;
if ty.is_dir() {
symlink_dir_all(entry.path(), file)?;
} else {
// fs::copy(entry.path(), file)?;
if !file.exists() {
std::os::unix::fs::symlink(entry.path(), file)?;
}
}
}
Ok(())
}
}