Skip to content

Commit 2708932

Browse files
committed
rustc_tools_util: try to handle case of not having CFG_RELEASE_CHANNEL better when getting compiler channel.
1 parent 09a0422 commit 2708932

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
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 & 9 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,14 +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-
}
9082

9183
pub fn get_commit_hash() -> Option<String> {
9284
std::process::Command::new("git")
@@ -104,6 +96,34 @@ pub fn get_commit_date() -> Option<String> {
10496
.and_then(|r| String::from_utf8(r.stdout).ok())
10597
}
10698

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

0 commit comments

Comments
 (0)