Skip to content

rustc_tools_util: try to handle case of not having CFG_RELEASE_CHANNEL better when getting compiler channel. #4045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rustc_tools_util/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

````
Expand Down
39 changes: 29 additions & 10 deletions rustc_tools_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ macro_rules! get_version_info {
let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().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);

Expand Down Expand Up @@ -79,15 +79,6 @@ impl std::fmt::Debug for VersionInfo {
}
}

pub fn get_channel() -> Option<String> {
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<String> {
std::process::Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
Expand All @@ -104,6 +95,34 @@ pub fn get_commit_date() -> Option<String> {
.and_then(|r| String::from_utf8(r.stdout).ok())
}

pub fn get_channel() -> Option<String> {
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::*;
Expand Down