From 18b8da96730c0ee9219732b9d3b7d4046106179d Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sat, 31 Aug 2024 11:01:53 -0400 Subject: [PATCH] Handle rustc version output correctly when `clippy-driver` used --- build.rs | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/build.rs b/build.rs index 46f5dc8efff7c..cde1b35294deb 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,6 @@ use std::env; -use std::process::Command; +use std::ffi::{OsStr, OsString}; +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 @@ -111,29 +112,26 @@ 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"), - } - }; - } - +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 = if let Some(wrapper) = env::var_os("RUSTC_WRAPPER").filter(|w| !w.is_empty()) { + + let mut cmd = if let Some(wrapper) = rustc_wrapper { let mut cmd = Command::new(wrapper); cmd.arg(rustc); + if is_clippy_driver { + cmd.arg("--rustc"); + } + cmd } else { Command::new(rustc) }; - let output = cmd - .arg("--version") - .output() - .expect("Failed to get rustc version"); + cmd.arg("--version"); + + let output = cmd.output().expect("Failed to get rustc version"); + if !output.status.success() { panic!( "failed to run rustc: {}", @@ -141,7 +139,27 @@ fn rustc_minor_nightly() -> (u32, bool) { ); } + output +} + +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") {