diff options
author | Erik Johnston <erik@matrix.org> | 2022-09-12 11:03:42 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-12 10:03:42 +0000 |
commit | ebfeac7c5ded851a2639911ec6adf9d0fcdb029a (patch) | |
tree | 972049d45c5fed1241ee4820705033cbba784d5f /rust/build.rs | |
parent | Concurrently collect room unread counts for push badges (#13765) (diff) | |
download | synapse-ebfeac7c5ded851a2639911ec6adf9d0fcdb029a.tar.xz |
Check if Rust lib needs rebuilding. (#13759)
This protects against the common mistake of failing to remember to rebuild Rust code after making changes.
Diffstat (limited to 'rust/build.rs')
-rw-r--r-- | rust/build.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/rust/build.rs b/rust/build.rs new file mode 100644 index 0000000000..2117975e56 --- /dev/null +++ b/rust/build.rs @@ -0,0 +1,45 @@ +//! 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}; + +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()); + } + } + } + + 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}"); + + Ok(()) +} |