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