]> git.lizzy.rs Git - rust.git/blob - build.rs
Merge pull request #2221 from topecongiro/rfc/blank-lines
[rust.git] / build.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::env;
12 use std::fs::File;
13 use std::io::Write;
14 use std::path::PathBuf;
15 use std::process::Command;
16
17 fn main() {
18     let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
19
20     File::create(out_dir.join("commit-info.txt"))
21         .unwrap()
22         .write_all(commit_info().as_bytes())
23         .unwrap();
24 }
25
26 // Try to get hash and date of the last commit on a best effort basis. If anything goes wrong
27 // (git not installed or if this is not a git repository) just return an empty string.
28 fn commit_info() -> String {
29     match (commit_hash(), commit_date()) {
30         (Some(hash), Some(date)) => format!(" ({} {})", hash.trim_right(), date),
31         _ => String::new(),
32     }
33 }
34
35 fn commit_hash() -> Option<String> {
36     Command::new("git")
37         .args(&["rev-parse", "--short", "HEAD"])
38         .output()
39         .ok()
40         .and_then(|r| String::from_utf8(r.stdout).ok())
41 }
42
43 fn commit_date() -> Option<String> {
44     Command::new("git")
45         .args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
46         .output()
47         .ok()
48         .and_then(|r| String::from_utf8(r.stdout).ok())
49 }