build.rs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //! Fingerprint of the translator's own source, for the shader cache key
  2. //! (`victory-gpu`'s `shader_cache`): a rebuilt executable whose translator did
  3. //! not change keeps its cached translations. Also lists every `VICTORY_*`
  4. //! switch the translator reads, whose values join the key at run time.
  5. //! Reads only this crate's `src` folder; writes nothing but cargo directives.
  6. use std::path::{Path, PathBuf};
  7. fn files(dir: &Path, out: &mut Vec<PathBuf>) {
  8. let Ok(entries) = std::fs::read_dir(dir) else {
  9. return;
  10. };
  11. for e in entries.flatten() {
  12. let p = e.path();
  13. if p.is_dir() {
  14. files(&p, out);
  15. } else if p.extension().is_some_and(|x| x == "rs") {
  16. out.push(p);
  17. }
  18. }
  19. }
  20. /// FNV-1a, 64 bits, two lanes with different offsets: a mismatch only costs
  21. /// a cache miss, so this need not be cryptographic.
  22. fn fnv(state: &mut [u64; 2], bytes: &[u8]) {
  23. for &b in bytes {
  24. for (i, s) in state.iter_mut().enumerate() {
  25. *s ^= u64::from(b) ^ (i as u64 * 0x9e);
  26. *s = s.wrapping_mul(0x0000_0100_0000_01b3);
  27. }
  28. }
  29. }
  30. fn main() {
  31. let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
  32. println!("cargo:rerun-if-changed=src");
  33. println!("cargo:rerun-if-changed=Cargo.toml");
  34. let mut list = Vec::new();
  35. files(&src, &mut list);
  36. list.sort();
  37. let mut state = [0xcbf2_9ce4_8422_2325u64, 0x8422_2325_cbf2_9ce4u64];
  38. let mut switches = std::collections::BTreeSet::new();
  39. for p in &list {
  40. let rel = p.strip_prefix(&src).unwrap_or(p).to_string_lossy().replace('\\', "/");
  41. let text = std::fs::read(p).unwrap_or_default();
  42. fnv(&mut state, rel.as_bytes());
  43. fnv(&mut state, &(text.len() as u64).to_le_bytes());
  44. fnv(&mut state, &text);
  45. let s = String::from_utf8_lossy(&text);
  46. let mut rest = &s[..];
  47. while let Some(at) = rest.find("\"VICTORY_") {
  48. let tail = &rest[at + 1..];
  49. let end = tail
  50. .find(|c: char| !(c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_'))
  51. .unwrap_or(tail.len());
  52. if tail[end..].starts_with('"') {
  53. switches.insert(tail[..end].to_string());
  54. }
  55. rest = &tail[end..];
  56. }
  57. }
  58. println!("cargo:rustc-env=VICTORY_SHADER_SOURCE_HASH={:016x}{:016x}", state[0], state[1]);
  59. let joined: Vec<_> = switches.into_iter().collect();
  60. println!("cargo:rustc-env=VICTORY_SHADER_SWITCHES={}", joined.join(","));
  61. }