-
Notifications
You must be signed in to change notification settings - Fork 552
Expand file tree
/
Copy pathbuild.rs
More file actions
65 lines (51 loc) · 2.13 KB
/
Copy pathbuild.rs
File metadata and controls
65 lines (51 loc) · 2.13 KB
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
//! This build script calculates the hash of all files in the `src/`
//! directory and adds it as an environment variable during build time.
//!
//! This is used so that the python code can detect when the built native module
//! does not match the source in-tree, helping to detect the case where the
//! source has been updated but the library hasn't been rebuilt.
use std::path::PathBuf;
use blake2::{Blake2b512, Digest};
use rustc_version::version_meta;
fn main() -> Result<(), std::io::Error> {
let mut dirs = vec![PathBuf::from("src")];
let mut paths = Vec::new();
while let Some(path) = dirs.pop() {
let mut entries = std::fs::read_dir(path)?
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, std::io::Error>>()?;
entries.sort();
for entry in entries {
if entry.is_dir() {
dirs.push(entry);
} else {
paths.push(entry.to_str().expect("valid rust paths").to_string());
}
}
}
// Manually add Cargo.toml's, Cargo.lock and build.rs to the hash, since changes to
// these files should also invalidate the built module.
paths.push("Cargo.toml".to_string());
paths.push("../Cargo.lock".to_string());
paths.push("../Cargo.toml".to_string());
paths.push("build.rs".to_string());
paths.sort();
let mut hasher = Blake2b512::new();
for path in paths {
let bytes = std::fs::read(path)?;
hasher.update(bytes);
}
let hex_digest = hex::encode(hasher.finalize());
println!("cargo:rustc-env=SYNAPSE_RUST_DIGEST={hex_digest}");
let rustc_version = version_meta()
.map(|v| v.short_version_string)
.unwrap_or_else(|_| "unknown".to_string());
println!("cargo:rustc-env=SYNAPSE_RUSTC_VERSION={}", rustc_version,);
// The default rules don't pick up trivial changes to the workspace config
// files, but we need to rebuild if those change to pick up the changed
// hashes.
println!("cargo::rerun-if-changed=.");
println!("cargo::rerun-if-changed=../Cargo.lock");
println!("cargo::rerun-if-changed=../Cargo.toml");
Ok(())
}