From 6196c7fb0ea37667022828860b1206ae15671d96 Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sat, 31 Aug 2024 11:01:53 -0400 Subject: [PATCH] Fix rustc version when `clippy-driver` is used This is a combined cherry-pick of the following two commits: - 18b8da96 ("Handle rustc version output correctly...") - cdf12d2a ("Update `rustc_version_cmd`") These two commits are squashed for the backport to slightly reduce the amount of conflict resolution needed going forward (some small tweaks were still needed). This backports the following: - - Commit 1 original message: Handle rustc version output correctly when `clippy-driver` used Commit 2 original message: Update `rustc_version_cmd` Change `if let` to a `match` because it is about the same complexity but also works with our MSRV for 0.2. This should allow backporting [1] easier, as well as future backports that touch this code. Additionally, add some new documentation comments. [1]: https://github.com/rust-lang/libc/pull/3893 --- build.rs | 58 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/build.rs b/build.rs index 098ee0fd6d7bc..bfc1f63e6ff9e 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,5 @@ use std::env; -use std::process::Command; +use std::process::{Command, Output}; use std::str; // List of cfgs this build script is allowed to set. The list is needed to support check-cfg, as we @@ -194,31 +194,29 @@ fn main() { } } -fn rustc_minor_nightly() -> (u32, bool) { - macro_rules! otry { - ($e:expr) => { - match $e { - Some(e) => e, - None => panic!("Failed to get rustc version"), - } - }; - } - +/// Run `rustc --version` and capture the output, adjusting arguments as needed if `clippy-driver` +/// is used instead. +fn rustc_version_cmd(is_clippy_driver: bool) -> Output { + let rustc_wrapper = env::var_os("RUSTC_WRAPPER").filter(|w| !w.is_empty()); let rustc = env::var_os("RUSTC").expect("Failed to get rustc version: missing RUSTC env"); - let mut cmd = match env::var_os("RUSTC_WRAPPER").as_ref() { - Some(wrapper) if !wrapper.is_empty() => { + + let mut cmd = match rustc_wrapper { + Some(wrapper) => { let mut cmd = Command::new(wrapper); cmd.arg(rustc); + if is_clippy_driver { + cmd.arg("--rustc"); + } + cmd } - _ => Command::new(rustc), + None => Command::new(rustc), }; - let output = cmd - .arg("--version") - .output() - .ok() - .expect("Failed to get rustc version"); + cmd.arg("--version"); + + let output = cmd.output().ok().expect("Failed to get rustc version"); + if !output.status.success() { panic!( "failed to run rustc: {}", @@ -226,7 +224,29 @@ fn rustc_minor_nightly() -> (u32, bool) { ); } + output +} + +/// Return the minor version of `rustc`, as well as a bool indicating whether or not the version +/// is a nightly. +fn rustc_minor_nightly() -> (u32, bool) { + macro_rules! otry { + ($e:expr) => { + match $e { + Some(e) => e, + None => panic!("Failed to get rustc version"), + } + }; + } + + let mut output = rustc_version_cmd(false); + + if otry!(str::from_utf8(&output.stdout).ok()).starts_with("clippy") { + output = rustc_version_cmd(true); + } + let version = otry!(str::from_utf8(&output.stdout).ok()); + let mut pieces = version.split('.'); if pieces.next() != Some("rustc 1") {