-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
53 lines (43 loc) · 1.43 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
use clap::CommandFactory;
use clap_complete::{generate_to, Shell};
use std::env;
use std::io::Error;
use std::process::Command;
include!("src/cli.rs");
fn main() -> Result<(), Error> {
let outdir = match env::var_os("OUT_DIR") {
None => return Err(Error::new(std::io::ErrorKind::Other, "no $OUT_DIR!")),
Some(outdir) => outdir,
};
let mut app = ProgramOptions::command();
generate_to(Shell::Bash, &mut app, "dcamctl", &outdir)?;
generate_to(Shell::Zsh, &mut app, "dcamctl", &outdir)?;
generate_to(Shell::Fish, &mut app, "dcamctl", outdir)?;
if let Some(v) = version_check::Version::read() {
println!("cargo:rustc-env=BUILD_RUSTC={}", v)
}
if let Some(hash) = get_commit_hash().or_else(|| env::var("BUILD_ID").ok()) {
println!("cargo:rustc-env=BUILD_ID={}", hash);
}
println!(
"cargo:rustc-env=BUILD_INFO={}-{}-{}-{}",
env::var("CARGO_CFG_TARGET_ARCH").unwrap(),
env::var("CARGO_CFG_TARGET_VENDOR").unwrap(),
env::var("CARGO_CFG_TARGET_OS").unwrap(),
env::var("CARGO_CFG_TARGET_ENV").unwrap(),
);
Ok(())
}
fn get_commit_hash() -> Option<String> {
Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.output()
.ok()
.and_then(|r| {
if r.status.success() {
String::from_utf8(r.stdout).ok()
} else {
None
}
})
}