use std::{
env, fs,
path::{Path, PathBuf},
};
const FILES: &[&str] = &[
"sokf-spec/sokf.tramlines.yaml",
"knowledge/formats/sokf.formats.yaml",
"harness/skills/sokf/SKILL.md",
"harness/instructions.md",
];
fn main() {
let manifest =
PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("cargo sets CARGO_MANIFEST_DIR"));
let out = PathBuf::from(env::var("OUT_DIR").expect("cargo sets OUT_DIR"));
let workspace = manifest.join("../../..");
let mut hash = Fnv(0xCBF2_9CE4_8422_2325);
for rel in FILES {
let source = [workspace.join(rel), manifest.join(rel)]
.into_iter()
.find(|p| p.is_file())
.unwrap_or_else(|| {
panic!(
"{rel} is under neither {} nor {}",
workspace.display(),
manifest.display()
)
});
println!("cargo:rerun-if-changed={}", source.display());
let text = fs::read_to_string(&source)
.expect("read embedded file")
.replace("\r\n", "\n");
let target = out.join(rel);
fs::create_dir_all(
target
.parent()
.expect("embedded file path has a parent directory"),
)
.expect("create OUT_DIR subdirectory");
fs::write(target, &text).expect("write embedded file");
hash.update(rel.as_bytes());
hash.update(text.as_bytes());
}
let lock = workspace.join("Cargo.lock");
if lock.is_file() {
println!("cargo:rerun-if-changed={}", lock.display());
hash.update(&fs::read(&lock).expect("read Cargo.lock"));
}
for dir in patched_crates(&workspace) {
println!("cargo:rerun-if-changed={}", dir.display());
let mut files = Vec::new();
crate_files(&dir, &mut files);
files.sort();
for file in files {
println!("cargo:rerun-if-changed={}", file.display());
hash.update(
file.strip_prefix(&dir)
.unwrap_or(&file)
.to_string_lossy()
.as_bytes(),
);
hash.update(&fs::read(&file).expect("read patched crate file"));
}
}
let src = manifest.join("src");
println!("cargo:rerun-if-changed={}", src.display());
let mut files = Vec::new();
sources(&src, &mut files);
files.sort();
for file in files {
hash.update(
file.strip_prefix(&src)
.unwrap_or(&file)
.to_string_lossy()
.as_bytes(),
);
hash.update(&fs::read(&file).expect("read source file"));
}
println!("cargo:rustc-env=SOKF_BUILD_FINGERPRINT={:016x}", hash.0);
}
fn patched_crates(workspace: &Path) -> Vec<PathBuf> {
let mut dirs = Vec::new();
for file in [
workspace.join("Cargo.toml"),
workspace.join(".cargo/config.toml"),
] {
let Ok(text) = fs::read_to_string(&file) else {
continue;
};
println!("cargo:rerun-if-changed={}", file.display());
let mut in_patch = false;
for line in text.lines() {
let line = line.trim();
if line.starts_with('[') {
in_patch = line.starts_with("[patch.") || line.starts_with("[patch]");
continue;
}
if !in_patch || line.starts_with('#') {
continue;
}
if let Some(path) = path_value(line) {
let dir = workspace.join(path);
if dir.is_dir() && !dirs.contains(&dir) {
dirs.push(dir);
}
}
}
}
dirs
}
fn path_value(line: &str) -> Option<&str> {
let mut rest = line;
while let Some(at) = rest.find("path") {
let before = rest[..at].chars().next_back();
let after = rest[at + 4..].trim_start();
rest = &rest[at + 4..];
if before.is_some_and(|c| c.is_alphanumeric() || c == '-' || c == '_') {
continue;
}
let Some(value) = after.strip_prefix('=') else {
continue;
};
let value = value.trim_start().strip_prefix('"')?;
return value.find('"').map(|end| &value[..end]);
}
None
}
fn crate_files(dir: &Path, out: &mut Vec<PathBuf>) {
let Ok(entries) = fs::read_dir(dir) else {
return;
};
for entry in entries {
let path = entry.expect("read patched crate entry").path();
let name = path.file_name().map(|n| n.to_string_lossy().into_owned());
if name
.as_deref()
.is_some_and(|n| n.starts_with('.') || n == "target" || n == "tests")
{
continue;
}
if path.is_dir() {
crate_files(&path, out);
} else if path.is_file() {
out.push(path);
}
}
}
fn sources(dir: &Path, out: &mut Vec<PathBuf>) {
for entry in fs::read_dir(dir).expect("read source directory") {
let path = entry.expect("read source directory entry").path();
if path.is_dir() {
sources(&path, out);
} else {
out.push(path);
}
}
}
struct Fnv(u64);
impl Fnv {
fn update(&mut self, bytes: &[u8]) {
for b in bytes {
self.0 ^= u64::from(*b);
self.0 = self.0.wrapping_mul(0x0100_0000_01B3);
}
self.0 ^= 0xFF;
self.0 = self.0.wrapping_mul(0x0100_0000_01B3);
}
}