forked from tikv/tikv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
75 lines (66 loc) · 2.02 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
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
extern crate time;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
fn main() {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out_dir.join("build-info.txt"))
.unwrap()
.write_all(build_info().as_bytes())
.unwrap();
}
// build_info returns a string of commit hash and utc time.
// TODO: use serde.
fn build_info() -> String {
// explicit separates outputs by '\n'.
format!(
"{}\n{}\n{}\n{}",
commit_hash().trim_right(),
branch_info().trim_right(),
utc_time(),
rustc_version()
)
}
fn utc_time() -> String {
let utc = time::now_utc();
format!("{}", utc.strftime("%Y-%m-%d %I:%M:%S").unwrap())
}
fn commit_hash() -> String {
let mut cmd = Command::new("git");
cmd.args(&["rev-parse", "HEAD"]);
cmd.output()
.ok()
.and_then(|o| String::from_utf8(o.stdout).ok())
.unwrap_or("None".to_owned())
}
fn branch_info() -> String {
let mut cmd = Command::new("git");
cmd.args(&["rev-parse", "--abbrev-ref", "HEAD"]);
cmd.output()
.ok()
.and_then(|o| String::from_utf8(o.stdout).ok())
.unwrap_or("None".to_owned())
}
fn rustc_version() -> String {
let mut cmd = Command::new(env::var("RUSTC").unwrap());
cmd.args(&["--version"]);
cmd.output()
.ok()
.and_then(|o| String::from_utf8(o.stdout).ok())
.map(|v| v.trim_left_matches("rustc").trim().to_owned())
.unwrap()
}