about summary refs log tree commit diff
path: root/build.rs
blob: 087f52e968247310c82fed969b2cf8e797ff9c0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use std::process::Command;

// Embeds the short git hash and the HEAD commit's date into the binary via
// env!() in src/lib.rs. Using the commit date (not the wall-clock build
// time) keeps this a pure function of the git commit being built, so the
// build stays reproducible.

fn main() {
    let git_hash = Command::new("git")
        .args(["rev-parse", "--short", "HEAD"])
        .output()
        .ok()
        .filter(|output| output.status.success())
        .and_then(|output| String::from_utf8(output.stdout).ok())
        .map(|s| s.trim().to_string())
        .unwrap_or_else(|| "unknown".to_string());
    println!("cargo:rustc-env=GN4_GIT_HASH={git_hash}");

    let commit_date = Command::new("git")
        .args(["show", "-s", "--format=%cd", "--date=format:%Y%m%d", "HEAD"])
        .output()
        .ok()
        .filter(|output| output.status.success())
        .and_then(|output| String::from_utf8(output.stdout).ok())
        .map(|s| s.trim().to_string())
        .unwrap_or_else(|| "unknown".to_string());
    println!("cargo:rustc-env=GN4_COMMIT_DATE={commit_date}");

    println!("cargo:rerun-if-changed=.git/HEAD");
}