cuprate-hinto-janai/helper/build.rs
hinto-janai a58d33b95e
helper: add constants & COMMIT (#64)
* helper: add `build.rs`

* helper: add `constants` feature

* helper: add `constants.rs`

* helper: use `.as_bytes()` for commit hash length check

* helper: `to_lowercase()` and `trim()` to `COMMIT`
2024-02-15 21:44:43 +00:00

32 lines
919 B
Rust

fn main() {
#[cfg(feature = "constants")]
set_commit_env();
}
#[cfg(feature = "constants")]
/// This sets the git `COMMIT` environment variable.
fn set_commit_env() {
const PATH: &str = "../.git/refs/heads/";
println!("cargo:rerun-if-changed={PATH}");
// FIXME: This could also be `std::fs::read({PATH}/{branch})`
// so the machine building doesn't need `git`, although:
// 1. Having `git` as a build dependency is probably ok
// 2. It causes issues on PRs that aren't the `main` branch
let output = std::process::Command::new("git")
.arg("rev-parse")
.arg("HEAD")
.output()
.unwrap();
let commit = std::str::from_utf8(&output.stdout)
.unwrap()
.trim()
.to_lowercase();
// Commit hash should always be 40 bytes long.
assert_eq!(commit.as_bytes().len(), 40);
println!("cargo:rustc-env=COMMIT={commit}");
}