aboutsummaryrefslogtreecommitdiffstats
path: root/build.rs
blob: 63677f71dbd79a15fd30ca0b22c21913b1698e45 (plain) (blame)
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
31
32
33
34
35
36
37
38
39
40
41
use std::process::Command;

fn git(args: &[&str]) -> String {
    let output = Command::new("git")
        .args(args)
        .output()
        .expect("failed to execute git");

    if !output.status.success() {
        panic!("git command failed: git {}", args.join(" "));
    }

    String::from_utf8(output.stdout)
        .expect("git output not utf8")
        .trim()
        .to_string()
}

fn main() {
    // Re-run if HEAD changes
    println!("cargo:rerun-if-changed=.git/HEAD");
    println!("cargo:rerun-if-changed=.git/refs");

    let git_hash = git(&["rev-parse", "HEAD"]);
    let git_hash_short = git(&["rev-parse", "--short", "HEAD"]);

    let git_dirty = {
        let status = Command::new("git")
            .args(["diff", "--quiet"])
            .status()
            .expect("failed to check git dirty state");

        !status.success()
    };

    println!("cargo:rustc-env=GIT_HASH={git_hash}");
    println!("cargo:rustc-env=GIT_HASH_SHORT={git_hash_short}");
    if git_dirty {
        println!("cargo:rustc-env=GIT_DIRTY=1");
    }
}