Skip to content

Commit

Permalink
rustc_tools_util: try to handle case of not having CFG_RELEASE_CHANNE…
Browse files Browse the repository at this point in the history
…L better when getting compiler channel.
  • Loading branch information
matthiaskrgr committed Apr 28, 2019
1 parent 78d13e0 commit 8012b91
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
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

0 comments on commit 8012b91

Please sign in to comment.