-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.rs
62 lines (52 loc) · 1.67 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
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{env, io};
use clap::ValueEnum;
use clap_complete::{generate_to, Shell};
include!("src/cli.rs");
//use clap::Shell;
fn main() -> Result<(), io::Error> {
// Export build target, build time, and git commit
println!(
"cargo:rustc-env=TARGET={}",
env::var("TARGET").unwrap_or_else(|_| "Unknown Target".to_string())
);
let mut git_branch = "Unknown Branch".to_string();
let mut git_commit = "Unknown Commit".to_string();
if let Ok(out) = Command::new("git")
.args(vec!["rev-parse", "--abbrev-ref", "HEAD"])
.output()
{
if !out.stdout.is_empty() {
git_branch =
String::from_utf8(out.stdout).unwrap_or_else(|_| "Unknown Branch".to_string());
}
}
if let Ok(out) = Command::new("git")
.args(vec!["rev-parse", "--short", "HEAD"])
.output()
{
if !out.stdout.is_empty() {
git_commit =
String::from_utf8(out.stdout).unwrap_or_else(|_| "Unknown Commit".to_string());
}
}
println!("cargo:rustc-env=GIT_BRANCH={git_branch}");
println!("cargo:rustc-env=GIT_COMMIT={git_commit}");
println!(
"cargo:rustc-env=TIME={}",
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
);
let outdir = match env::var_os("OUT_DIR") {
None => return Ok(()),
Some(outdir) => outdir,
};
let mut cmd = clap::command!();
for &shell in Shell::value_variants() {
generate_to(shell, &mut cmd, env!("CARGO_PKG_NAME"), outdir.as_os_str())?;
}
Ok(())
}