-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
141 lines (130 loc) · 4.78 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
fn main() {
println!("cargo:rerun-if-changed=vendor");
#[cfg(all(feature = "fuse-overlayfs-vendored", target_os = "linux"))]
{
let d = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("vendor/fuse-overlayfs/");
if !d.exists() {
panic!("fuse-overlayfs submodule is not present fuse-overlayfs-vendored cannot be used")
}
const EXEC: &str = "fuse-overlayfs";
let basedir = std::path::Path::new("./target/vendored");
let outdir = basedir.join(EXEC);
if !outdir.exists() {
std::fs::create_dir_all(&outdir).unwrap()
}
let exec_path = outdir.join("bin").join(EXEC);
let build = || {
let srcdir = outdir.join("src");
if !srcdir.exists() {
std::fs::create_dir(&srcdir).unwrap();
}
fs_extra::dir::copy(
&d,
&srcdir,
&fs_extra::dir::CopyOptions::new()
.overwrite(true)
.content_only(true),
)
.unwrap();
autotools::Config::new(srcdir)
.out_dir(std::fs::canonicalize(&outdir).expect("cannot canonicalize"))
.reconf("-fis")
.build()
};
#[cfg(feature = "build-cache")]
if vendored::need_rebuild(&d, &outdir, vec![]) {
build();
}
#[cfg(not(feature = "build-cache"))]
build();
println!(
"cargo::rustc-env=FUSE-OVERLAYFS-BIN={}",
&exec_path.to_string_lossy()
);
}
#[cfg(all(feature = "unionfs-fuse-vendored", target_os = "linux"))]
{
let d = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("vendor/unionfs-fuse/");
if !d.exists() {
panic!("unionfs-fuse submodule is not present unionfs-fuse-vendored cannot be used")
}
const EXEC: &str = "unionfs";
let outdir = std::path::Path::new("./target/vendored").join(EXEC);
if !outdir.exists() {
std::fs::create_dir_all(&outdir).unwrap()
}
let exec_path = outdir.join("build/src").join(EXEC);
let build = || {
cmake::Config::new(&d)
.out_dir(std::fs::canonicalize(&outdir).expect("cannot canonicalize"))
.very_verbose(false)
.build();
};
#[cfg(feature = "build-cache")]
if vendored::need_rebuild(&d, &outdir, vec![]) {
build();
}
#[cfg(not(feature = "build-cache"))]
build();
println!(
"cargo::rustc-env=UNIONFS-FUSE-BIN={}",
&exec_path.to_string_lossy()
);
}
}
#[cfg(any(feature = "fuse-overlayfs-vendored", feature = "unionfs-fuse-vendored"))]
mod vendored {
#[inline]
#[allow(dead_code)]
#[cfg(feature = "build-cache")]
pub fn need_rebuild<P: AsRef<std::path::Path>, Q: AsRef<std::path::Path>>(
src: P,
out: Q,
exception: Vec<String>,
) -> bool {
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Cache {
new_or_edited: Vec<(String, [u8; 16])>,
}
let cache_file = out.as_ref().join("cache.tag");
let cache: Option<Cache> = std::fs::File::open(&cache_file)
.ok()
.map(std::io::BufReader::new)
.and_then(|x| serde_json::from_reader(x).ok());
let mut hashed = vec![];
fn check_dir(
hashed: &mut Vec<(String, [u8; 16])>,
src: &std::path::Path,
exception: &[String],
) {
for entry in std::fs::read_dir(src).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if entry.file_type().unwrap().is_dir() {
check_dir(hashed, &path, exception);
continue;
}
let path_str = path.to_string_lossy().to_string();
if !exception.contains(&path_str) {
let buf = std::fs::read(src.join(&path_str)).unwrap();
let hash = md5::compute(&buf).0;
hashed.push((path_str, hash));
}
}
}
check_dir(&mut hashed, src.as_ref(), &exception);
if !cache.is_some_and(|x| x.new_or_edited == hashed) {
println!("Detected changes in vendored dependency. Updating cache.");
let new_cache = Cache {
new_or_edited: hashed,
};
std::fs::write(cache_file, serde_json::to_string(&new_cache).unwrap())
.expect("Unable to write cache file");
true
} else {
println!("No changes detected in vendored dependency.");
false
}
}
}