-
Notifications
You must be signed in to change notification settings - Fork 20
/
build.rs
51 lines (43 loc) · 1.4 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
//! Get the version string using `git describe --dirty` or, if it
//! fails, using the `CARGO_PKG_VERSION`.
//!
//! The `GIT` environment variable can be used to set an alternative
//! path to the git executable.
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::process::Command;
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("version.rs");
let mut f = File::create(&dest_path).unwrap();
let git =
env::var("GIT").unwrap_or("git".into());
let description =
Command::new(git)
.arg("describe")
.arg("--dirty")
.output();
let cargo_version = env!("CARGO_PKG_VERSION").to_owned();
let mut version =
match description {
Ok(output) => {
if output.status.success() {
format!("git-{}",
String::from_utf8(output.stdout).unwrap())
} else {
cargo_version
}
}
_ => cargo_version,
};
// Make sure version is on a single line
if let Some(l) = version.find('\n') {
version.truncate(l);
}
writeln!(f, "pub const VERSION: &'static str = \
\"{}\";", version).unwrap();
writeln!(f, "pub const VERSION_CSTR: &'static str = \
\"{}\\0\";", version).unwrap();
}