Skip to content

Commit

Permalink
Merge pull request #3893 from nathaniel-bennett/clippy-rustc-wrapper
Browse files Browse the repository at this point in the history
Handle version output correctly when `clippy-driver` used
  • Loading branch information
tgross35 authored Sep 1, 2024
2 parents 0e6afd5 + 18b8da9 commit 237f3e7
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions build.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -111,37 +112,54 @@ 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: {}",
String::from_utf8_lossy(output.stderr.as_slice())
);
}

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") {
Expand Down

0 comments on commit 237f3e7

Please sign in to comment.