aboutsummaryrefslogtreecommitdiff
path: root/build.rs
blob: a47c4ab45c96353652bf73c1bfa5a8c9bbec60f0 (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
31
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: MIT

use std::env::var;

/// <https://github.com/denoland/deno/blob/main/cli/build.rs#L265:L285>
fn git_commit_hash() -> String {
  if let Ok(output) = std::process::Command::new("git")
    .arg("rev-list")
    .arg("-1")
    .arg("HEAD")
    .output()
  {
    if output.status.success() {
      std::str::from_utf8(&output.stdout[..40])
        .unwrap()
        .to_string()
    } else {
      "UNKNOWN".to_string()
    }
  } else {
    "UNKNOWN".to_string()
  }
}

fn main() {
  println!("cargo:rustc-env=GIT_COMMIT_HASH={}", git_commit_hash());
  println!("cargo:rerun-if-env-changed=GIT_COMMIT_HASH");
  println!("cargo:rustc-env=TARGET={}", var("TARGET").unwrap());
  println!("cargo:rustc-env=PROFILE={}", var("PROFILE").unwrap());
}