Skip to content

Commit 86f7347

Browse files
committed
Auto merge of #4045 - matthiaskrgr:RTU, r=phansch
rustc_tools_util: try to handle case of not having CFG_RELEASE_CHANNEL better when getting compiler channel. changelog: rustc_tools_util: try to handle case of not having CFG_RELEASE_CHANNEL better when getting compiler channel.
2 parents d0ff1a5 + 8012b91 commit 86f7347

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

rustc_tools_util/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ fn main() {
3030
"cargo:rustc-env=COMMIT_DATE={}",
3131
rustc_tools_util::get_commit_date().unwrap_or_default()
3232
);
33+
println!(
34+
"cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}",
35+
rustc_tools_util::get_channel_from_compiler_output().unwrap_or_default()
36+
);
3337
}
3438

3539
````

rustc_tools_util/src/lib.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ macro_rules! get_version_info {
88
let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap();
99
let crate_name = String::from(env!("CARGO_PKG_NAME"));
1010

11-
let host_compiler = $crate::get_channel();
11+
let host_compiler = option_env!("RUSTC_RELEASE_CHANNEL").map(str::to_string);
1212
let commit_hash = option_env!("GIT_HASH").map(str::to_string);
1313
let commit_date = option_env!("COMMIT_DATE").map(str::to_string);
1414

@@ -79,15 +79,6 @@ impl std::fmt::Debug for VersionInfo {
7979
}
8080
}
8181

82-
pub fn get_channel() -> Option<String> {
83-
if let Ok(channel) = env::var("CFG_RELEASE_CHANNEL") {
84-
Some(channel)
85-
} else {
86-
// we could ask ${RUSTC} -Vv and do some parsing and find out
87-
Some(String::from("nightly"))
88-
}
89-
}
90-
9182
pub fn get_commit_hash() -> Option<String> {
9283
std::process::Command::new("git")
9384
.args(&["rev-parse", "--short", "HEAD"])
@@ -104,6 +95,34 @@ pub fn get_commit_date() -> Option<String> {
10495
.and_then(|r| String::from_utf8(r.stdout).ok())
10596
}
10697

98+
pub fn get_channel() -> Option<String> {
99+
match env::var("CFG_RELEASE_CHANNEL") {
100+
Ok(channel) => Some(channel),
101+
Err(_) => {
102+
// if that failed, try to ask rustc -V, do some parsing and find out
103+
match std::process::Command::new("rustc")
104+
.arg("-V")
105+
.output()
106+
.ok()
107+
.and_then(|r| String::from_utf8(r.stdout).ok())
108+
{
109+
Some(rustc_output) => {
110+
if rustc_output.contains("beta") {
111+
Some(String::from("beta"))
112+
} else if rustc_output.contains("stable") {
113+
Some(String::from("stable"))
114+
} else {
115+
// default to nightly if we fail to parse
116+
Some(String::from("nightly"))
117+
}
118+
},
119+
// default to nightly
120+
None => Some(String::from("nightly")),
121+
}
122+
},
123+
}
124+
}
125+
107126
#[cfg(test)]
108127
mod test {
109128
use super::*;

0 commit comments

Comments
 (0)