diff --git a/rustc_tools_util/README.md b/rustc_tools_util/README.md index a88f47e4dbc2..79c90b86b6cf 100644 --- a/rustc_tools_util/README.md +++ b/rustc_tools_util/README.md @@ -30,6 +30,10 @@ fn main() { "cargo:rustc-env=COMMIT_DATE={}", rustc_tools_util::get_commit_date().unwrap_or_default() ); + println!( + "cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}", + rustc_tools_util::get_channel_from_compiler_output().unwrap_or_default() + ); } ```` diff --git a/rustc_tools_util/src/lib.rs b/rustc_tools_util/src/lib.rs index 19c27754839e..8f89d50c64a4 100644 --- a/rustc_tools_util/src/lib.rs +++ b/rustc_tools_util/src/lib.rs @@ -8,7 +8,7 @@ macro_rules! get_version_info { let patch = env!("CARGO_PKG_VERSION_PATCH").parse::().unwrap(); let crate_name = String::from(env!("CARGO_PKG_NAME")); - let host_compiler = $crate::get_channel(); + let host_compiler = option_env!("RUSTC_RELEASE_CHANNEL").map(str::to_string); let commit_hash = option_env!("GIT_HASH").map(str::to_string); let commit_date = option_env!("COMMIT_DATE").map(str::to_string); @@ -79,15 +79,6 @@ impl std::fmt::Debug for VersionInfo { } } -pub fn get_channel() -> Option { - if let Ok(channel) = env::var("CFG_RELEASE_CHANNEL") { - Some(channel) - } else { - // we could ask ${RUSTC} -Vv and do some parsing and find out - Some(String::from("nightly")) - } -} - pub fn get_commit_hash() -> Option { std::process::Command::new("git") .args(&["rev-parse", "--short", "HEAD"]) @@ -104,6 +95,34 @@ pub fn get_commit_date() -> Option { .and_then(|r| String::from_utf8(r.stdout).ok()) } +pub fn get_channel() -> Option { + match env::var("CFG_RELEASE_CHANNEL") { + Ok(channel) => Some(channel), + Err(_) => { + // if that failed, try to ask rustc -V, do some parsing and find out + match std::process::Command::new("rustc") + .arg("-V") + .output() + .ok() + .and_then(|r| String::from_utf8(r.stdout).ok()) + { + Some(rustc_output) => { + if rustc_output.contains("beta") { + Some(String::from("beta")) + } else if rustc_output.contains("stable") { + Some(String::from("stable")) + } else { + // default to nightly if we fail to parse + Some(String::from("nightly")) + } + }, + // default to nightly + None => Some(String::from("nightly")), + } + }, + } +} + #[cfg(test)] mod test { use super::*;