build.rs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //! Fingerprints the shader translator, so that cached translations
  2. //! (`src/shader_cache.rs`) last until the translator itself changes and not
  3. //! only until the next build of anything.
  4. use std::path::{Path, PathBuf};
  5. fn files(dir: &Path, out: &mut Vec<PathBuf>) {
  6. for entry in std::fs::read_dir(dir).into_iter().flatten().flatten() {
  7. let path = entry.path();
  8. if path.is_dir() {
  9. files(&path, out);
  10. } else {
  11. out.push(path);
  12. }
  13. }
  14. }
  15. fn main() {
  16. let here = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap_or_default());
  17. let crates = here.join("..");
  18. let root = crates.join("..");
  19. // The translator, what it is built from, and the form entries are kept in.
  20. let mut all = vec![
  21. crates.join("victory-shader/Cargo.toml"),
  22. here.join("src/shader_cache.rs"),
  23. root.join("Cargo.lock"),
  24. root.join("rust-toolchain.toml"),
  25. ];
  26. let source = crates.join("victory-shader/src");
  27. println!("cargo:rerun-if-changed={}", source.display());
  28. files(&source, &mut all);
  29. all.sort();
  30. let mut hash = blake3::Hasher::new();
  31. for path in &all {
  32. println!("cargo:rerun-if-changed={}", path.display());
  33. // Names and line endings as they are in every checkout.
  34. let name = path.file_name().unwrap_or_default().to_string_lossy().into_owned();
  35. let bytes: Vec<u8> = std::fs::read(path).unwrap_or_default().into_iter().filter(|&b| b != 13).collect();
  36. hash.update(name.as_bytes());
  37. hash.update(&(bytes.len() as u64).to_le_bytes());
  38. hash.update(&bytes);
  39. }
  40. println!("cargo:rerun-if-changed=build.rs");
  41. println!("cargo:rustc-env=VICTORY_TRANSLATOR={}", hash.finalize().to_hex());
  42. }