This repository has been archived by the owner on Nov 8, 2018. It is now read-only.
forked from bharrisau/rust-libcore
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.rs
68 lines (60 loc) · 1.98 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
use std::process::Command;
use std::env;
use std::fs;
use std::io;
use std::path::Path;
const RUST_DIR: &'static str = "./rust";
fn ensure_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
let path = path.as_ref();
match fs::read_dir(path) {
Ok(..) => Ok(()),
Err(..) => fs::create_dir(path),
}
}
fn main() {
// Run rustc to get the version
let rustc_output = Command::new("rustc").arg("--version")
.output().unwrap_or_else(|e| {
panic!("failed to execute rustc: {}", e);
});
let output_bytes: &[u8] = rustc_output.stdout.as_ref();
let version = match std::str::from_utf8(output_bytes) {
Ok(s) => s.split(" ").nth(2).expect("rustc gave invalid version format"),
Err(e) => panic!(e),
}.trim_left_matches("(");
match fs::metadata(Path::new(RUST_DIR).join(version)) {
Ok(meta) => {
if !meta.is_file() {
match fs::remove_dir_all(RUST_DIR) {
Err(e) => panic!(e),
_ => {},
}
}
},
Err(..) => {}, // Ok to ignore since this just means that the version file doesn't exist
}
// Ensure the rust directory exists
if let Err(e) = ensure_dir(RUST_DIR) {
panic!(e);
}
// cd to the rust directory
if let Err(e) = env::set_current_dir(RUST_DIR) {
panic!(e);
}
// Shell out to perform the build. In the future, the logic
// to grab libcore could be done in rust in order to support
// platforms without a posix shell
Command::new("sh")
.arg("../build.sh")
.env("DOWNLOAD_LINK",
format!("https://github.com/rust-lang/rust/tarball/{}", version))
.status().unwrap_or_else(|e| {
panic!("failed to execute process: {}", e);
});
// Now that we've successfully unzipped, write the version
// file as a success tag.
match fs::File::create(version) {
Err(e) => panic!(e),
_ => {},
}
}