-
Notifications
You must be signed in to change notification settings - Fork 141
/
build.rs
67 lines (62 loc) · 1.88 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
// Specifies the minimum version needed to compile Ion.
// NOTE: 1.19 is required due to the usage of `break` with values for
// `loop` (RFC 1624, rust-lang/rust GitHub issue #37339).
// const MIN_VERSION: &'static str = "1.19.0";
use std::{
env,
fs::{self, File},
io::{self, Read, Write},
path::Path,
process::Command,
};
fn main() {
match write_version_file() {
Ok(_) => {}
Err(e) => panic!("Failed to create a version file: {:?}", e),
}
}
fn write_version_file() -> io::Result<()> {
let version = env::var("CARGO_PKG_VERSION").unwrap();
let target = env::var("TARGET").unwrap();
let version_fname = Path::new(&env::var("OUT_DIR").unwrap()).join("version_string");
let mut version_file = File::create(&version_fname)?;
write!(
&mut version_file,
"r#\"ion {} ({})\nrev {}\"#",
version,
target,
get_git_rev()?.trim()
)?;
Ok(())
}
fn get_git_rev() -> io::Result<String> {
let version_file = Path::new("git_revision.txt");
if version_file.exists() {
fs::read_to_string(&version_file)
} else {
Command::new("git")
.arg("rev-parse")
.arg("master")
.output()
.and_then(|out| {
String::from_utf8(out.stdout).map_err(|_| {
io::Error::new(
io::ErrorKind::InvalidData,
"git rev-parse master output was not UTF-8",
)
})
})
.or_else(|_| git_rev_from_file())
}
}
fn git_rev_from_file() -> io::Result<String> {
let git_file = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap())
.join(".git")
.join("refs")
.join("heads")
.join("master");
let mut file = File::open(git_file)?;
let mut rev = String::new();
file.read_to_string(&mut rev)?;
Ok(rev)
}