Skip to content

Commit 6196c7f

Browse files
nathaniel-bennetttgross35
authored andcommitted
Fix rustc version when clippy-driver is used
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
1 parent fdd3a26 commit 6196c7f

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

build.rs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::env;
2-
use std::process::Command;
2+
use std::process::{Command, Output};
33
use std::str;
44

55
// List of cfgs this build script is allowed to set. The list is needed to support check-cfg, as we
@@ -194,39 +194,59 @@ fn main() {
194194
}
195195
}
196196

197-
fn rustc_minor_nightly() -> (u32, bool) {
198-
macro_rules! otry {
199-
($e:expr) => {
200-
match $e {
201-
Some(e) => e,
202-
None => panic!("Failed to get rustc version"),
203-
}
204-
};
205-
}
206-
197+
/// Run `rustc --version` and capture the output, adjusting arguments as needed if `clippy-driver`
198+
/// is used instead.
199+
fn rustc_version_cmd(is_clippy_driver: bool) -> Output {
200+
let rustc_wrapper = env::var_os("RUSTC_WRAPPER").filter(|w| !w.is_empty());
207201
let rustc = env::var_os("RUSTC").expect("Failed to get rustc version: missing RUSTC env");
208-
let mut cmd = match env::var_os("RUSTC_WRAPPER").as_ref() {
209-
Some(wrapper) if !wrapper.is_empty() => {
202+
203+
let mut cmd = match rustc_wrapper {
204+
Some(wrapper) => {
210205
let mut cmd = Command::new(wrapper);
211206
cmd.arg(rustc);
207+
if is_clippy_driver {
208+
cmd.arg("--rustc");
209+
}
210+
212211
cmd
213212
}
214-
_ => Command::new(rustc),
213+
None => Command::new(rustc),
215214
};
216215

217-
let output = cmd
218-
.arg("--version")
219-
.output()
220-
.ok()
221-
.expect("Failed to get rustc version");
216+
cmd.arg("--version");
217+
218+
let output = cmd.output().ok().expect("Failed to get rustc version");
219+
222220
if !output.status.success() {
223221
panic!(
224222
"failed to run rustc: {}",
225223
String::from_utf8_lossy(output.stderr.as_slice())
226224
);
227225
}
228226

227+
output
228+
}
229+
230+
/// Return the minor version of `rustc`, as well as a bool indicating whether or not the version
231+
/// is a nightly.
232+
fn rustc_minor_nightly() -> (u32, bool) {
233+
macro_rules! otry {
234+
($e:expr) => {
235+
match $e {
236+
Some(e) => e,
237+
None => panic!("Failed to get rustc version"),
238+
}
239+
};
240+
}
241+
242+
let mut output = rustc_version_cmd(false);
243+
244+
if otry!(str::from_utf8(&output.stdout).ok()).starts_with("clippy") {
245+
output = rustc_version_cmd(true);
246+
}
247+
229248
let version = otry!(str::from_utf8(&output.stdout).ok());
249+
230250
let mut pieces = version.split('.');
231251

232252
if pieces.next() != Some("rustc 1") {

0 commit comments

Comments
 (0)