Files
Oliver Hamlet c94629303d Add a Cargo build script that sets LIBLOOT_REVISION
It only does so if git is on the PATH and `git rev-parse --short HEAD` produces a UTF-8 string, but the script should never fail.
2025-07-24 20:06:40 +01:00

39 lines
1.3 KiB
Rust

use std::process::Command;
fn main() {
if std::env::var_os("LIBLOOT_REVISION").is_some() {
return;
}
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=build.rs");
let Ok(git_rev_parse_output) = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
else {
println!("cargo:warning=Calling git rev-parse failed, LIBLOOT_REVISION will be unset!");
return;
};
if let Ok(git_hash) = String::from_utf8(git_rev_parse_output.stdout) {
println!("cargo:rustc-env=LIBLOOT_REVISION={git_hash}");
} else {
println!(
"cargo:warning=Could not convert git rev-parse output to a UTF-8 string, LIBLOOT_REVISION will be unset!"
);
}
// Don't warn if this errors, as it will error if HEAD points directly to a commit.
if let Ok(git_symbolic_ref_output) = Command::new("git").args(["symbolic-ref", "HEAD"]).output()
{
if let Ok(symbolic_ref) = String::from_utf8(git_symbolic_ref_output.stdout) {
println!("cargo:rerun-if-changed=.git/{symbolic_ref}");
} else {
println!(
"cargo:warning=Could not convert git symbolic-ref output to a UTF-8 string, LIBLOOT_REVISION may become stale!"
);
}
}
}