File: build.rs

package info (click to toggle)
rust-cotp 1.9.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,392 kB
  • sloc: python: 168; xml: 14; sh: 9; makefile: 4
file content (27 lines) | stat: -rw-r--r-- 834 bytes parent folder | download | duplicates (2)
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
use std::{env, process::Command};

fn main() {
    let version = env!("CARGO_PKG_VERSION");

    if Ok("release".to_owned()) == env::var("PROFILE") {
        println!("cargo:rustc-env=COTP_VERSION={version}");
    } else {
        // Suffix with -DEBUG
        // If we can get the last commit hash, let's append that also
        if let Some(last_commit) = get_last_commit() {
            println!("cargo:rustc-env=COTP_VERSION={version}-DEBUG-{last_commit}");
        } else {
            println!("cargo:rustc-env=COTP_VERSION={version}-DEBUG");
        }
    }
}

fn get_last_commit() -> Option<String> {
    Command::new("git")
        .args(["rev-parse", "--short=12", "HEAD"])
        .output()
        .ok()
        .filter(|e| e.status.success())
        .map(|e| String::from_utf8(e.stdout))
        .and_then(Result::ok)
}