forked from redox-os/ion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
83 lines (76 loc) · 2.5 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
76
77
78
79
80
81
82
83
extern crate ansi_term;
extern crate version_check;
use ansi_term::Color::{Blue, Red, White};
use version_check::is_nightly;
// 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;
use std::fs::File;
use std::io::{self, Read, Write};
use std::path::Path;
use std::process::Command;
fn main() {
match is_nightly() {
Some(true) => (), // Success!
Some(false) => {
eprintln!(
"{} {}",
Red.bold().paint("Error:"),
White.paint("Ion requires nightly to build.")
);
// print_version_err(&*version_string);
eprintln!(
"{}{}{}",
Blue.paint("Use `"),
White.paint("rustup update"),
Blue.paint("` to update to the latest stable compiler.")
);
panic!("Aborting compilation due to incompatible compiler.")
}
_ => {
eprintln!("cargo:warning={}", "Ion was unable to check rustc compatibility.");
eprintln!("cargo:warning={}", "Build may fail due to incompatible rustc version.");
}
}
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 rev = match Command::new("git").arg("rev-parse").arg("master").output() {
Ok(out) => match String::from_utf8(out.stdout) {
Ok(s) => s,
Err(_) => git_rev_from_file()?,
},
Err(_) => git_rev_from_file()?,
};
Ok(rev)
}
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)
}