use std::collections::{BTreeMap, BTreeSet}; use std::path::{Path, PathBuf}; fn files(dir: &Path, out: &mut Vec) { for entry in std::fs::read_dir(dir).into_iter().flatten().flatten() { let path = entry.path(); if path.is_dir() { files(&path, out); } else { out.push(path); } } } fn code(text: &str) -> String { let b = text.as_bytes(); let mut out = Vec::with_capacity(b.len()); let mut i = 0; while i < b.len() { let c = b[i]; if c == b'/' && b.get(i + 1) == Some(&b'/') { while i < b.len() && b[i] != b'\n' { i += 1; } } else if c == b'/' && b.get(i + 1) == Some(&b'*') { let mut depth = 0; while i < b.len() { if b[i] == b'/' && b.get(i + 1) == Some(&b'*') { depth += 1; i += 2; } else if b[i] == b'*' && b.get(i + 1) == Some(&b'/') { depth -= 1; i += 2; if depth == 0 { break; } } else { i += 1; } } out.push(b' '); } else if c == b'r' && matches!(b.get(i + 1), Some(b'"' | b'#')) && !ident(b, i) { let start = i; i += 1; let mut hashes = 0; while b.get(i) == Some(&b'#') { hashes += 1; i += 1; } if b.get(i) != Some(&b'"') { out.extend_from_slice(&b[start..i]); continue; } i += 1; while i < b.len() { if b[i] == b'"' && b[i + 1..] .iter() .take(hashes) .filter(|&&h| h == b'#') .count() == hashes { i += 1 + hashes; break; } i += 1; } out.extend_from_slice(&b[start..i.min(b.len())]); } else if c == b'"' { let start = i; i += 1; while i < b.len() && b[i] != b'"' { i += if b[i] == b'\\' { 2 } else { 1 }; } i += 1; out.extend_from_slice(&b[start..i.min(b.len())]); } else if c == b'\'' { let start = i; let end = if b.get(i + 1) == Some(&b'\\') { b.get(i + 3..) .and_then(|r| r.iter().position(|&x| x == b'\'')) .map(|p| i + 4 + p) } else { let len = text[i + 1..].chars().next().map_or(1, char::len_utf8); (b.get(i + 1 + len) == Some(&b'\'')).then_some(i + 2 + len) }; i = end.unwrap_or(i + 1); out.extend_from_slice(&b[start..i]); } else { out.push(c); i += 1; } } let text = String::from_utf8_lossy(&out); let mut kept = String::with_capacity(text.len()); for line in text.lines().map(str::trim_end).filter(|l| !l.is_empty()) { kept.push_str(line); kept.push('\n'); } kept } fn ident(b: &[u8], i: usize) -> bool { let word = |j: usize| b[j].is_ascii_alphanumeric() || b[j] == b'_'; match i { 0 => false, 1 => b[0] != b'b' && word(0), _ if b[i - 1] == b'b' => word(i - 2), _ => word(i - 1), } } fn locked(lock: &str, root: &str) -> String { let mut packages: BTreeMap = BTreeMap::new(); let mut deps: BTreeMap> = BTreeMap::new(); for block in lock.split("[[package]]").skip(1) { let block = block.split("\n[").next().unwrap_or(block).trim(); let Some(name) = block.lines().find_map(|l| l.strip_prefix("name = ")) else { continue; }; let name = name.trim_matches('"').to_string(); let mut list = Vec::new(); if let Some(rest) = block.split("dependencies = [").nth(1) { for dep in rest.split(']').next().unwrap_or("").split(',') { let dep = dep.trim().trim_matches('"'); if let Some(dep) = dep.split(' ').next().filter(|d| !d.is_empty()) { list.push(dep.to_string()); } } } packages.entry(name.clone()).or_default().push_str(block); deps.entry(name).or_default().extend(list); } let mut seen = BTreeSet::new(); let mut todo = vec![root.to_string()]; while let Some(name) = todo.pop() { if seen.insert(name.clone()) { todo.extend(deps.get(&name).into_iter().flatten().cloned()); } } seen.iter() .filter_map(|n| packages.get(n)) .map(|b| format!("{b}\n")) .collect() } fn main() { let here = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap_or_default()); let crates = here.join(".."); let root = crates.join(".."); let mut all = vec![ here.join("Cargo.toml"), root.join("Cargo.lock"), root.join("rust-toolchain.toml"), ]; let source = here.join("src/shader"); println!("cargo:rerun-if-changed={}", source.display()); files(&source, &mut all); all.sort(); let mut hash = blake3::Hasher::new(); for path in &all { println!("cargo:rerun-if-changed={}", path.display()); let name = path .file_name() .unwrap_or_default() .to_string_lossy() .into_owned(); let text = String::from_utf8_lossy(&std::fs::read(path).unwrap_or_default()).replace('\r', ""); let bytes = if name == "Cargo.lock" { locked(&text, "eonacat-xsbox-gpu") } else if path.extension().is_some_and(|e| e == "rs") { code(&text) } else { text }; hash.update(name.as_bytes()); hash.update(&(bytes.len() as u64).to_le_bytes()); hash.update(bytes.as_bytes()); } println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-env-changed=EONACAT_XS_BUILD_NAME"); let git = |args: &[&str]| { std::process::Command::new("git") .args(args) .current_dir(&here) .output() .ok() .filter(|o| o.status.success()) .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_owned()) .filter(|s| !s.is_empty()) }; for path in ["HEAD", "logs/HEAD", "packed-refs", "refs/tags"] { if let Some(p) = git(&["rev-parse", "--path-format=absolute", "--git-path", path]) { println!("cargo:rerun-if-changed={p}"); } } let build = std::env::var("EONACAT_XS_BUILD_NAME").ok().filter(|v| !v.trim().is_empty()).unwrap_or_else(|| { match git(&["describe", "--tags", "--match", "v20*", "--long", "HEAD"]) { Some(d) => { let mut parts = d.rsplitn(3, '-'); let (_, since, tag) = (parts.next(), parts.next().unwrap_or("0"), parts.next().unwrap_or(&d)); if since == "0" { tag.to_owned() } else { format!("{tag}-{since}") } } None => git(&["rev-parse", "--short=8", "HEAD"]).unwrap_or_else(|| "unknown".to_owned()), } }); println!("cargo:rustc-env=EONACAT_XS_BUILD={build}"); println!("cargo:rustc-env=EONACAT_XS_TRANSLATOR={}", hash.finalize().to_hex()); let mut shader_hash = blake3::Hasher::new(); let mut switches = BTreeSet::new(); let mut shader_files = Vec::new(); files(&source, &mut shader_files); shader_files.sort(); for path in shader_files { let relative = path.strip_prefix(&source).unwrap_or(&path); let name = relative.to_string_lossy().replace('\\', "/"); let contents = std::fs::read_to_string(&path).unwrap_or_default().replace('\r', ""); shader_hash.update(name.as_bytes()); shader_hash.update(&(contents.len() as u64).to_le_bytes()); shader_hash.update(contents.as_bytes()); if path.extension().is_some_and(|ext| ext == "rs") { let bytes = contents.as_bytes(); let mut i = 0; while i + 12 <= bytes.len() { if bytes[i..].starts_with(b"EONACAT_XS_") { let start = i; i += 11; while i < bytes.len() && (bytes[i].is_ascii_uppercase() || bytes[i].is_ascii_digit() || bytes[i] == b'_') { i += 1; } if i > start + 11 { switches.insert(contents[start..i].to_owned()); } } else { i += 1; } } } } println!("cargo:rustc-env=EONACAT_XS_SHADER_SWITCHES={}", switches.into_iter().collect::>().join(",")); println!("cargo:rustc-env=EONACAT_XS_SHADER_SOURCE_HASH={}", shader_hash.finalize().to_hex()); }