From: Oliver Schneider Date: Fri, 27 May 2016 13:31:19 +0000 (+0200) Subject: don't require `cargo clippy` to pass a `--lib` or `--bin x` argument X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=0818e70497f9f18075dbc5c5d56f62fa5025f1d1;p=rust.git don't require `cargo clippy` to pass a `--lib` or `--bin x` argument --- diff --git a/Cargo.toml b/Cargo.toml index 672b9d7ef94..828857701d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,12 +32,12 @@ quine-mc_cluskey = "0.2.2" # begin automatic update clippy_lints = { version = "0.0.70", path = "clippy_lints" } # end automatic update +rustc-serialize = "0.3" [dev-dependencies] compiletest_rs = "0.1.0" lazy_static = "0.1.15" regex = "0.1.56" -rustc-serialize = "0.3" [features] debugging = [] diff --git a/src/cargo.rs b/src/cargo.rs new file mode 100644 index 00000000000..188d4d38e8d --- /dev/null +++ b/src/cargo.rs @@ -0,0 +1,48 @@ +use std::collections::HashMap; + +#[derive(RustcDecodable, Debug)] +pub struct Metadata { + pub packages: Vec, + resolve: Option<()>, + pub version: usize, +} + +#[derive(RustcDecodable, Debug)] +pub struct Package { + name: String, + version: String, + id: String, + source: Option<()>, + dependencies: Vec, + pub targets: Vec, + features: HashMap>, + manifest_path: String, +} + +#[derive(RustcDecodable, Debug)] +pub struct Dependency { + name: String, + source: Option, + req: String, + kind: Option, + optional: bool, + uses_default_features: bool, + features: Vec>, + target: Option<()>, +} + +#[allow(non_camel_case_types)] +#[derive(RustcDecodable, Debug)] +pub enum Kind { + dylib, + test, + bin, + lib, +} + +#[derive(RustcDecodable, Debug)] +pub struct Target { + pub name: String, + pub kind: Vec, + src_path: String, +} diff --git a/src/main.rs b/src/main.rs index 970222e1076..51683060be2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ extern crate syntax; extern crate rustc_plugin; extern crate clippy_lints; +extern crate rustc_serialize; use rustc_driver::{driver, CompilerCalls, RustcDefaultCalls, Compilation}; use rustc::session::{config, Session}; @@ -16,6 +17,8 @@ use std::path::PathBuf; use std::process::Command; +mod cargo; + struct ClippyCompilerCalls(RustcDefaultCalls); impl std::default::Default for ClippyCompilerCalls { @@ -118,16 +121,19 @@ pub fn main() { }; if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) { - let args = wrap_args(std::env::args().skip(2), dep_path, sys_root); - let path = std::env::current_exe().expect("current executable path invalid"); - let exit_status = std::process::Command::new("cargo") - .args(&args) - .env("RUSTC", path) - .spawn().expect("could not run cargo") - .wait().expect("failed to wait for cargo?"); - - if let Some(code) = exit_status.code() { - std::process::exit(code); + let output = std::process::Command::new("cargo").args(&["metadata", "--no-deps"]).output().expect("could not run `cargo metadata`"); + let stdout = std::str::from_utf8(&output.stdout).expect("`cargo metadata` output is not utf8"); + let mut metadata: cargo::Metadata = rustc_serialize::json::decode(stdout).expect("`cargo metadata` output is not valid json"); + assert_eq!(metadata.version, 1); + for target in metadata.packages.remove(0).targets { + let args = std::env::args().skip(2); + assert_eq!(target.kind.len(), 1); + match target.kind[0] { + cargo::Kind::dylib => process(std::iter::once("--lib".to_owned()).chain(args), &dep_path, &sys_root), + cargo::Kind::bin => process(vec!["--bin".to_owned(), target.name].into_iter().chain(args), &dep_path, &sys_root), + // don't process tests + _ => {}, + } } } else { let args: Vec = if env::args().any(|s| s == "--sysroot") { @@ -145,7 +151,7 @@ pub fn main() { } } -fn wrap_args(old_args: I, dep_path: P, sysroot: String) -> Vec +fn process(old_args: I, dep_path: P, sysroot: &str) where P: AsRef, I: Iterator { let mut args = vec!["rustc".to_owned()]; @@ -161,7 +167,17 @@ fn wrap_args(old_args: I, dep_path: P, sysroot: String) -> Vec args.push("-L".to_owned()); args.push(dep_path.as_ref().to_string_lossy().into_owned()); args.push(String::from("--sysroot")); - args.push(sysroot); + args.push(sysroot.to_owned()); args.push("-Zno-trans".to_owned()); - args + + let path = std::env::current_exe().expect("current executable path invalid"); + let exit_status = std::process::Command::new("cargo") + .args(&args) + .env("RUSTC", path) + .spawn().expect("could not run cargo") + .wait().expect("failed to wait for cargo?"); + + if let Some(code) = exit_status.code() { + std::process::exit(code); + } }