use std::path::PathBuf;
use std::process::Command;
fn main() {
println!("cargo:rerun-if-changed=schema/triage.fbs");
let flatc_path = find_flatc();
let Some(flatc) = flatc_path else {
panic!(
"Error: FlatBuffers schema compiler 'flatc' is required to compile triage-core.\n\
Please install flatc globally (e.g. via 'winget install Google.flatbuffers', \n\
'brew install flatbuffers', or download from github.com/google/flatbuffers)."
);
};
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
let status = Command::new(&flatc)
.arg("--rust")
.arg("-o")
.arg(&out_dir)
.arg("schema/triage.fbs")
.status();
match status {
Ok(s) if s.success() => {}
Ok(s) => {
panic!("flatc failed with exit code: {:?}", s.code());
}
Err(e) => {
panic!("failed to execute flatc at {:?}: {}", flatc, e);
}
}
}
fn find_flatc() -> Option<PathBuf> {
if let Some(paths) = std::env::var_os("PATH") {
for path in std::env::split_paths(&paths) {
let exe = if cfg!(target_os = "windows") {
path.join("flatc.exe")
} else {
path.join("flatc")
};
if exe.is_file() {
return Some(exe);
}
}
}
if cfg!(target_os = "windows") {
let local_appdata = std::env::var_os("LOCALAPPDATA")?;
let fallback = PathBuf::from(local_appdata)
.join("Microsoft")
.join("WinGet")
.join("Packages")
.join("Google.flatbuffers_Microsoft.Winget.Source_8wekyb3d8bbwe")
.join("flatc.exe");
if fallback.exists() {
return Some(fallback);
}
}
None
}