Skip to content

Commit

Permalink
Fix rustc_tools_util's version.host_compiler release channel, expos…
Browse files Browse the repository at this point in the history
…e the rustc version, and add tests (#14123)

changelog: Fix rustc_tools_util's `version.host_compiler` release
channel, expose the rustc version, and add tests.

Previously the host_compiler would be set to "nighly" on the stable
channel. Generally, the field felt a bit neglected neither being printed
not tested.
  • Loading branch information
flip1995 authored Feb 13, 2025
2 parents 8ae4750 + 8cac5b0 commit 943d604
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ path = "src/driver.rs"
[dependencies]
clippy_config = { path = "clippy_config" }
clippy_lints = { path = "clippy_lints" }
rustc_tools_util = "0.4.0"
rustc_tools_util = { path = "rustc_tools_util", version = "0.4.1" }
tempfile = { version = "3.3", optional = true }
termize = "0.1"
color-print = "0.3.4"
Expand Down Expand Up @@ -54,7 +54,7 @@ parking_lot = "0.12"
tokio = { version = "1", features = ["io-util"] }

[build-dependencies]
rustc_tools_util = "0.4.0"
rustc_tools_util = { path = "rustc_tools_util", version = "0.4.1" }

[features]
integration = ["tempfile"]
Expand Down
2 changes: 1 addition & 1 deletion rustc_tools_util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustc_tools_util"
version = "0.4.0"
version = "0.4.1"
description = "small helper to generate version information for git packages"
repository = "https://github.com/rust-lang/rust-clippy"
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions rustc_tools_util/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ build = "build.rs"
List rustc_tools_util as regular AND build dependency.
````toml
[dependencies]
rustc_tools_util = "0.4.0"
rustc_tools_util = "0.4.1"

[build-dependencies]
rustc_tools_util = "0.4.0"
rustc_tools_util = "0.4.1"
````

In `build.rs`, generate the data in your `main()`
Expand Down
55 changes: 33 additions & 22 deletions rustc_tools_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ macro_rules! get_version_info {
}};
}

/// This macro can be used in `build.rs` to automatically set the needed
/// environment values, namely `GIT_HASH`, `COMMIT_DATE` and
/// `RUSTC_RELEASE_CHANNEL`
/// This macro can be used in `build.rs` to automatically set the needed environment values, namely
/// `GIT_HASH`, `COMMIT_DATE` and `RUSTC_RELEASE_CHANNEL`
#[macro_export]
macro_rules! setup_version_info {
() => {{
Expand All @@ -43,7 +42,11 @@ macro_rules! setup_version_info {
"cargo:rustc-env=COMMIT_DATE={}",
$crate::get_commit_date().unwrap_or_default()
);
println!("cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}", $crate::get_channel());
let compiler_version = $crate::get_compiler_version();
println!(
"cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}",
$crate::get_channel(compiler_version)
);
}};
}

Expand Down Expand Up @@ -87,16 +90,17 @@ impl std::fmt::Debug for VersionInfo {
"VersionInfo {{ crate_name: \"{}\", major: {}, minor: {}, patch: {}",
self.crate_name, self.major, self.minor, self.patch,
)?;
if self.commit_hash.is_some() {
write!(
f,
", commit_hash: \"{}\", commit_date: \"{}\" }}",
self.commit_hash.clone().unwrap_or_default().trim(),
self.commit_date.clone().unwrap_or_default().trim()
)?;
} else {
write!(f, " }}")?;
if let Some(ref commit_hash) = self.commit_hash {
write!(f, ", commit_hash: \"{}\"", commit_hash.trim(),)?;
}
if let Some(ref commit_date) = self.commit_date {
write!(f, ", commit_date: \"{}\"", commit_date.trim())?;
}
if let Some(ref host_compiler) = self.host_compiler {
write!(f, ", host_compiler: \"{}\"", host_compiler.trim())?;
}

write!(f, " }}")?;

Ok(())
}
Expand Down Expand Up @@ -152,22 +156,27 @@ pub fn get_commit_date() -> Option<String> {
}

#[must_use]
pub fn get_channel() -> String {
pub fn get_compiler_version() -> Option<String> {
get_output("rustc", &["-V"])
}

#[must_use]
pub fn get_channel(compiler_version: Option<String>) -> String {
if let Ok(channel) = std::env::var("CFG_RELEASE_CHANNEL") {
return channel;
}

// if that failed, try to ask rustc -V, do some parsing and find out
if let Some(rustc_output) = get_output("rustc", &["-V"]) {
if let Some(rustc_output) = compiler_version {
if rustc_output.contains("beta") {
return String::from("beta");
} else if rustc_output.contains("stable") {
return String::from("stable");
} else if rustc_output.contains("nightly") {
return String::from("nightly");
}
}

// default to nightly
String::from("nightly")
// default to stable
String::from("stable")
}

#[cfg(test)]
Expand All @@ -179,17 +188,19 @@ mod test {
let vi = get_version_info!();
assert_eq!(vi.major, 0);
assert_eq!(vi.minor, 4);
assert_eq!(vi.patch, 0);
assert_eq!(vi.patch, 1);
assert_eq!(vi.crate_name, "rustc_tools_util");
// hard to make positive tests for these since they will always change
assert!(vi.commit_hash.is_none());
assert!(vi.commit_date.is_none());

assert!(vi.host_compiler.is_none());
}

#[test]
fn test_display_local() {
let vi = get_version_info!();
assert_eq!(vi.to_string(), "rustc_tools_util 0.4.0");
assert_eq!(vi.to_string(), "rustc_tools_util 0.4.1");
}

#[test]
Expand All @@ -198,7 +209,7 @@ mod test {
let s = format!("{vi:?}");
assert_eq!(
s,
"VersionInfo { crate_name: \"rustc_tools_util\", major: 0, minor: 4, patch: 0 }"
"VersionInfo { crate_name: \"rustc_tools_util\", major: 0, minor: 4, patch: 1 }"
);
}
}
6 changes: 6 additions & 0 deletions tests/versioncheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,9 @@ fn check_that_clippy_has_the_same_major_version_as_rustc() {
},
}
}

#[test]
fn check_host_compiler() {
let version = rustc_tools_util::get_version_info!();
assert_eq!(version.host_compiler, Some("nightly".to_string()));
}

0 comments on commit 943d604

Please sign in to comment.