Skip to content

Commit dbca6b8

Browse files
committed
Give rustc_tools_util's host_compiler some love: serialize full rustc version, debug print, and test.
1 parent 3b98f39 commit dbca6b8

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ path = "src/driver.rs"
2525
[dependencies]
2626
clippy_config = { path = "clippy_config" }
2727
clippy_lints = { path = "clippy_lints" }
28-
rustc_tools_util = "0.4.0"
28+
rustc_tools_util = { path = "rustc_tools_util", version = "0.4.0" }
2929
tempfile = { version = "3.3", optional = true }
3030
termize = "0.1"
3131
color-print = "0.3.4"
@@ -54,7 +54,7 @@ parking_lot = "0.12"
5454
tokio = { version = "1", features = ["io-util"] }
5555

5656
[build-dependencies]
57-
rustc_tools_util = "0.4.0"
57+
rustc_tools_util = { path = "rustc_tools_util", version = "0.4.0" }
5858

5959
[features]
6060
integration = ["tempfile"]

rustc_tools_util/src/lib.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ macro_rules! get_version_info {
1212
let patch = std::env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap();
1313
let crate_name = String::from(std::env!("CARGO_PKG_NAME"));
1414

15+
let host_compiler_version = std::option_env!("RUSTC_VERSION").map(str::to_string);
1516
let host_compiler = std::option_env!("RUSTC_RELEASE_CHANNEL").map(str::to_string);
1617
let commit_hash = std::option_env!("GIT_HASH").map(str::to_string);
1718
let commit_date = std::option_env!("COMMIT_DATE").map(str::to_string);
@@ -20,6 +21,7 @@ macro_rules! get_version_info {
2021
major,
2122
minor,
2223
patch,
24+
host_compiler_version,
2325
host_compiler,
2426
commit_hash,
2527
commit_date,
@@ -29,7 +31,7 @@ macro_rules! get_version_info {
2931
}
3032

3133
/// This macro can be used in `build.rs` to automatically set the needed
32-
/// environment values, namely `GIT_HASH`, `COMMIT_DATE` and
34+
/// environment values, namely `GIT_HASH`, `COMMIT_DATE`, `RUSTC_VERSION` and
3335
/// `RUSTC_RELEASE_CHANNEL`
3436
#[macro_export]
3537
macro_rules! setup_version_info {
@@ -43,7 +45,15 @@ macro_rules! setup_version_info {
4345
"cargo:rustc-env=COMMIT_DATE={}",
4446
$crate::get_commit_date().unwrap_or_default()
4547
);
46-
println!("cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}", $crate::get_channel());
48+
let compiler_version = $crate::get_compiler_version();
49+
println!(
50+
"cargo:rustc-env=RUSTC_VERSION={}",
51+
compiler_version.as_deref().unwrap_or_default()
52+
);
53+
println!(
54+
"cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}",
55+
$crate::get_channel(compiler_version)
56+
);
4757
}};
4858
}
4959

@@ -52,6 +62,7 @@ pub struct VersionInfo {
5262
pub major: u8,
5363
pub minor: u8,
5464
pub patch: u16,
65+
pub host_compiler_version: Option<String>,
5566
pub host_compiler: Option<String>,
5667
pub commit_hash: Option<String>,
5768
pub commit_date: Option<String>,
@@ -87,16 +98,20 @@ impl std::fmt::Debug for VersionInfo {
8798
"VersionInfo {{ crate_name: \"{}\", major: {}, minor: {}, patch: {}",
8899
self.crate_name, self.major, self.minor, self.patch,
89100
)?;
90-
if self.commit_hash.is_some() {
91-
write!(
92-
f,
93-
", commit_hash: \"{}\", commit_date: \"{}\" }}",
94-
self.commit_hash.clone().unwrap_or_default().trim(),
95-
self.commit_date.clone().unwrap_or_default().trim()
96-
)?;
97-
} else {
98-
write!(f, " }}")?;
101+
if let Some(ref commit_hash) = self.commit_hash {
102+
write!(f, ", commit_hash: \"{}\"", commit_hash.trim(),)?;
99103
}
104+
if let Some(ref commit_date) = self.commit_date {
105+
write!(f, ", commit_date: \"{}\"", commit_date.trim())?;
106+
}
107+
if let Some(ref version) = self.host_compiler_version {
108+
write!(f, ", host_compiler_version: \"{}\"", version.trim())?;
109+
}
110+
if let Some(ref host_compiler) = self.host_compiler {
111+
write!(f, ", host_compiler: \"{}\"", host_compiler.trim())?;
112+
}
113+
114+
write!(f, " }}")?;
100115

101116
Ok(())
102117
}
@@ -152,13 +167,18 @@ pub fn get_commit_date() -> Option<String> {
152167
}
153168

154169
#[must_use]
155-
pub fn get_channel() -> String {
170+
pub fn get_compiler_version() -> Option<String> {
171+
get_output("rustc", &["-V"])
172+
}
173+
174+
#[must_use]
175+
pub fn get_channel(compiler_version: Option<String>) -> String {
156176
if let Ok(channel) = std::env::var("CFG_RELEASE_CHANNEL") {
157177
return channel;
158178
}
159179

160180
// if that failed, try to ask rustc -V, do some parsing and find out
161-
if let Some(rustc_output) = get_output("rustc", &["-V"]) {
181+
if let Some(rustc_output) = compiler_version {
162182
if rustc_output.contains("beta") {
163183
return String::from("beta");
164184
} else if rustc_output.contains("nightly") {
@@ -184,6 +204,8 @@ mod test {
184204
// hard to make positive tests for these since they will always change
185205
assert!(vi.commit_hash.is_none());
186206
assert!(vi.commit_date.is_none());
207+
208+
assert!(vi.host_compiler.is_none());
187209
}
188210

189211
#[test]

tests/versioncheck.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,12 @@ fn check_that_clippy_has_the_same_major_version_as_rustc() {
9090
},
9191
}
9292
}
93+
94+
#[test]
95+
fn check_host_compiler() {
96+
let version = rustc_tools_util::get_version_info!();
97+
assert_eq!(version.host_compiler, Some("nightly".to_string()));
98+
99+
let rustc_version = version.host_compiler_version.unwrap();
100+
assert!(rustc_version.contains("nightly"));
101+
}

0 commit comments

Comments
 (0)