]> git.lizzy.rs Git - rust.git/blob - build.rs
more fallout
[rust.git] / build.rs
1 extern crate walkdir;
2
3 use std::env;
4 use std::fs::File;
5 use std::io::Write;
6 use std::path::Path;
7 use std::process::Command;
8
9 use walkdir::WalkDir;
10
11 fn main() {
12     let out_dir = env::var("OUT_DIR").unwrap();
13     let dest_path = Path::new(&out_dir).join("git_info.rs");
14     let mut f = File::create(&dest_path).unwrap();
15
16     writeln!(f,
17              "const COMMIT_HASH: Option<&'static str> = {:?};",
18              git_head_sha1())
19             .unwrap();
20     writeln!(f,
21              "const WORKTREE_CLEAN: Option<bool> = {:?};",
22              git_tree_is_clean())
23             .unwrap();
24
25     // cargo:rerun-if-changed requires one entry per individual file.
26     for entry in WalkDir::new("src") {
27         let entry = entry.unwrap();
28         println!("cargo:rerun-if-changed={}", entry.path().display());
29     }
30 }
31
32 // Returns `None` if git is not available.
33 fn git_head_sha1() -> Option<String> {
34     Command::new("git")
35         .arg("rev-parse")
36         .arg("--short")
37         .arg("HEAD")
38         .output()
39         .ok()
40         .and_then(|o| String::from_utf8(o.stdout).ok())
41         .map(|mut s| {
42                  let len = s.trim_right().len();
43                  s.truncate(len);
44                  s
45              })
46 }
47
48 // Returns `None` if git is not available.
49 fn git_tree_is_clean() -> Option<bool> {
50     Command::new("git")
51         .arg("status")
52         .arg("--porcelain")
53         .arg("--untracked-files=no")
54         .output()
55         .ok()
56         .map(|o| o.stdout.is_empty())
57 }