]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/channel.rs
4664b1f765e630a9595a3f985f709b5ae3ff2796
[rust.git] / src / bootstrap / channel.rs
1 // Copyright 2015 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 //! Build configuration for Rust's release channels.
12 //!
13 //! Implements the stable/beta/nightly channel distinctions by setting various
14 //! flags like the `unstable_features`, calculating variables like `release` and
15 //! `package_vers`, and otherwise indicating to the compiler what it should
16 //! print out as part of its version information.
17
18 use std::path::Path;
19 use std::process::Command;
20
21 use build_helper::output;
22
23 use Build;
24
25 // The version number
26 pub const CFG_RELEASE_NUM: &'static str = "1.20.0";
27
28 // An optional number to put after the label, e.g. '.2' -> '-beta.2'
29 // Be sure to make this starts with a dot to conform to semver pre-release
30 // versions (section 9)
31 pub const CFG_PRERELEASE_VERSION: &'static str = ".1";
32
33 pub struct GitInfo {
34     inner: Option<Info>,
35 }
36
37 struct Info {
38     commit_date: String,
39     sha: String,
40     short_sha: String,
41 }
42
43 impl GitInfo {
44     pub fn new(dir: &Path) -> GitInfo {
45         // See if this even begins to look like a git dir
46         if !dir.join(".git").exists() {
47             return GitInfo { inner: None }
48         }
49
50         // Make sure git commands work
51         let out = Command::new("git")
52                           .arg("rev-parse")
53                           .current_dir(dir)
54                           .output()
55                           .expect("failed to spawn git");
56         if !out.status.success() {
57             return GitInfo { inner: None }
58         }
59
60         // Ok, let's scrape some info
61         let ver_date = output(Command::new("git").current_dir(dir)
62                                       .arg("log").arg("-1")
63                                       .arg("--date=short")
64                                       .arg("--pretty=format:%cd"));
65         let ver_hash = output(Command::new("git").current_dir(dir)
66                                       .arg("rev-parse").arg("HEAD"));
67         let short_ver_hash = output(Command::new("git")
68                                             .current_dir(dir)
69                                             .arg("rev-parse")
70                                             .arg("--short=9")
71                                             .arg("HEAD"));
72         GitInfo {
73             inner: Some(Info {
74                 commit_date: ver_date.trim().to_string(),
75                 sha: ver_hash.trim().to_string(),
76                 short_sha: short_ver_hash.trim().to_string(),
77             }),
78         }
79     }
80
81     pub fn sha(&self) -> Option<&str> {
82         self.inner.as_ref().map(|s| &s.sha[..])
83     }
84
85     pub fn sha_short(&self) -> Option<&str> {
86         self.inner.as_ref().map(|s| &s.short_sha[..])
87     }
88
89     pub fn commit_date(&self) -> Option<&str> {
90         self.inner.as_ref().map(|s| &s.commit_date[..])
91     }
92
93     pub fn version(&self, build: &Build, num: &str) -> String {
94         let mut version = build.release(num);
95         if let Some(ref inner) = self.inner {
96             version.push_str(" (");
97             version.push_str(&inner.short_sha);
98             version.push_str(" ");
99             version.push_str(&inner.commit_date);
100             version.push_str(")");
101         }
102         return version
103     }
104 }