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.
This commit is contained in:
Oliver Hamlet
2025-07-24 20:06:40 +01:00
parent b492e34ab9
commit c94629303d
2 changed files with 41 additions and 1 deletions
+3 -1
View File
@@ -31,7 +31,9 @@ libloot-<last tag>-<revisions since tag>-g<short revision ID>_<branch>-<platform
Make sure you have [Rust](https://www.rust-lang.org/) installed.
The `LIBLOOT_REVISION` environment variable is used to embed the commit hash into the build. If it's not defined then `unknown` will be used instead. To define it in PowerShell, run:
The `LIBLOOT_REVISION` environment variable is used to embed the commit hash into the build. If it's not defined then `unknown` will be used instead. The Cargo build script will automatically define it if libloot is built from a Git repository and `git` is accessible from your `PATH`.
To define it in PowerShell, run:
```powershell
$env:LIBLOOT_REVISION = git rev-parse --short HEAD
+38
View File
@@ -0,0 +1,38 @@
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!"
);
}
}
}