build.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //! This build script calculates the hash of all files in the `src/`
  2. //! directory and adds it as an environment variable during build time.
  3. //!
  4. //! This is used so that the python code can detect when the built native module
  5. //! does not match the source in-tree, helping to detect the case where the
  6. //! source has been updated but the library hasn't been rebuilt.
  7. use std::path::PathBuf;
  8. use blake2::{Blake2b512, Digest};
  9. fn main() -> Result<(), std::io::Error> {
  10. let mut dirs = vec![PathBuf::from("src")];
  11. let mut paths = Vec::new();
  12. while let Some(path) = dirs.pop() {
  13. let mut entries = std::fs::read_dir(path)?
  14. .map(|res| res.map(|e| e.path()))
  15. .collect::<Result<Vec<_>, std::io::Error>>()?;
  16. entries.sort();
  17. for entry in entries {
  18. if entry.is_dir() {
  19. dirs.push(entry);
  20. } else {
  21. paths.push(entry.to_str().expect("valid rust paths").to_string());
  22. }
  23. }
  24. }
  25. paths.sort();
  26. let mut hasher = Blake2b512::new();
  27. for path in paths {
  28. let bytes = std::fs::read(path)?;
  29. hasher.update(bytes);
  30. }
  31. let hex_digest = hex::encode(hasher.finalize());
  32. println!("cargo:rustc-env=SYNAPSE_RUST_DIGEST={hex_digest}");
  33. Ok(())
  34. }