]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/channel.rs
Auto merge of #43648 - RalfJung:jemalloc-debug, r=alexcrichton
[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 use config::Config;
25
26 // The version number
27 pub const CFG_RELEASE_NUM: &str = "1.21.0";
28
29 // An optional number to put after the label, e.g. '.2' -> '-beta.2'
30 // Be sure to make this starts with a dot to conform to semver pre-release
31 // versions (section 9)
32 pub const CFG_PRERELEASE_VERSION: &str = ".1";
33
34 pub struct GitInfo {
35     inner: Option<Info>,
36 }
37
38 struct Info {
39     commit_date: String,
40     sha: String,
41     short_sha: String,
42 }
43
44 impl GitInfo {
45     pub fn new(config: &Config, dir: &Path) -> GitInfo {
46         // See if this even begins to look like a git dir
47         if config.ignore_git || !dir.join(".git").exists() {
48             return GitInfo { inner: None }
49         }
50
51         // Make sure git commands work
52         let out = Command::new("git")
53                           .arg("rev-parse")
54                           .current_dir(dir)
55                           .output()
56                           .expect("failed to spawn git");
57         if !out.status.success() {
58             return GitInfo { inner: None }
59         }
60
61         // Ok, let's scrape some info
62         let ver_date = output(Command::new("git").current_dir(dir)
63                                       .arg("log").arg("-1")
64                                       .arg("--date=short")
65                                       .arg("--pretty=format:%cd"));
66         let ver_hash = output(Command::new("git").current_dir(dir)
67                                       .arg("rev-parse").arg("HEAD"));
68         let short_ver_hash = output(Command::new("git")
69                                             .current_dir(dir)
70                                             .arg("rev-parse")
71                                             .arg("--short=9")
72                                             .arg("HEAD"));
73         GitInfo {
74             inner: Some(Info {
75                 commit_date: ver_date.trim().to_string(),
76                 sha: ver_hash.trim().to_string(),
77                 short_sha: short_ver_hash.trim().to_string(),
78             }),
79         }
80     }
81
82     pub fn sha(&self) -> Option<&str> {
83         self.inner.as_ref().map(|s| &s.sha[..])
84     }
85
86     pub fn sha_short(&self) -> Option<&str> {
87         self.inner.as_ref().map(|s| &s.short_sha[..])
88     }
89
90     pub fn commit_date(&self) -> Option<&str> {
91         self.inner.as_ref().map(|s| &s.commit_date[..])
92     }
93
94     pub fn version(&self, build: &Build, num: &str) -> String {
95         let mut version = build.release(num);
96         if let Some(ref inner) = self.inner {
97             version.push_str(" (");
98             version.push_str(&inner.short_sha);
99             version.push_str(" ");
100             version.push_str(&inner.commit_date);
101             version.push_str(")");
102         }
103         version
104     }
105
106     pub fn is_git(&self) -> bool {
107         self.inner.is_some()
108     }
109 }