Skip to content

Commit

Permalink
Fix rustc version when clippy-driver is used
Browse files Browse the repository at this point in the history
This is a combined cherry-pick of the following two commits:

- 18b8da9 ("Handle rustc version output correctly...")
- cdf12d2 ("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:

- <#3893>
- <#3903>

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]: #3893

Co-authored-by: Nathaniel Bennett <[email protected]>
Co-authored-by: Trevor Gross <[email protected]>
  • Loading branch information
tgross35 and nathaniel-bennett committed Sep 10, 2024
1 parent 57a7d46 commit 5078335
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions build.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -194,39 +194,59 @@ 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: {}",
String::from_utf8_lossy(output.stderr.as_slice())
);
}

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

0 comments on commit 5078335

Please sign in to comment.