forked from dragonflyoss/nydus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
64 lines (58 loc) · 2.03 KB
/
build.rs
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2020 Ant Group. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0
use std::ffi::OsString;
use std::process::Command;
use std::str::FromStr;
use std::{ffi, io};
fn get_version_from_cmd(executable: &ffi::OsStr) -> io::Result<String> {
let output = Command::new(executable).arg("-V").output()?;
let mut v = String::from_utf8(output.stdout).unwrap();
v.pop(); // remove newline
Ok(v)
}
fn get_git_commit_hash() -> String {
let commit = Command::new("git")
.arg("rev-parse")
.arg("--verify")
.arg("HEAD")
.output();
if let Ok(commit_output) = commit {
if let Some(commit) = String::from_utf8_lossy(&commit_output.stdout)
.lines()
.next()
{
return commit.to_string();
}
}
"unknown".to_string()
}
fn get_git_commit_version() -> String {
let tag = Command::new("git").args(["describe", "--tags"]).output();
if let Ok(tag) = tag {
if let Some(tag) = String::from_utf8_lossy(&tag.stdout).lines().next() {
return tag.to_string();
}
}
"unknown".to_string()
}
fn main() {
let rustc_ver = if let Ok(p) = std::env::var("RUSTC") {
let rustc = OsString::from_str(&p).unwrap();
get_version_from_cmd(&rustc).unwrap()
} else {
"<Unknown>".to_string()
};
let profile = std::env::var("PROFILE").unwrap_or_else(|_| "<Unknown>".to_string());
let build_time = time::OffsetDateTime::now_utc()
.format(&time::format_description::well_known::Iso8601::DEFAULT)
.unwrap();
let git_commit_hash = get_git_commit_hash();
let git_commit_version = get_git_commit_version();
println!("cargo:rerun-if-changed=../git/HEAD");
println!("cargo:rustc-env=RUSTC_VERSION={}", rustc_ver);
println!("cargo:rustc-env=PROFILE={}", profile);
println!("cargo:rustc-env=BUILT_TIME_UTC={}", build_time);
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", git_commit_hash);
println!("cargo:rustc-env=GIT_COMMIT_VERSION={}", git_commit_version);
}