From 9632cb1f049bbb61fce24d32b1badca4fe4f62e4 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Wed, 17 Feb 2016 14:42:40 +0100 Subject: [PATCH] groundwork for allowing `cargo install` --- Cargo.toml | 4 +-- src/main.rs | 93 +++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 71 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b5a192b..82f4907 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,5 @@ authors = [ license = "MIT" readme = "README.md" -[dependencies.clippy] -git = "https://github.com/Manishearth/rust-clippy" +[dependencies] +clippy = {path = "clippy"} diff --git a/src/main.rs b/src/main.rs index 69340e0..4e43721 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,48 +1,93 @@ +#![feature(rustc_private)] + +#[allow(plugin_as_library)] +extern crate clippy; +extern crate rustc; +extern crate rustc_driver; +extern crate rustc_plugin; + +use rustc::session::Session; +use rustc_driver::{driver, CompilerCalls, Compilation}; + +struct ClippyCompilerCalls; + +impl<'a> CompilerCalls<'a> for ClippyCompilerCalls { + fn build_controller(&mut self, _: &Session) -> driver::CompileController<'a> { + let mut control = driver::CompileController::basic(); + + control.after_parse.callback = Box::new(|state| { + let mut registry = rustc_plugin::registry::Registry::new(state.session, state.krate.unwrap()); + clippy::plugin_registrar(&mut registry); + + let rustc_plugin::registry::Registry { early_lint_passes, late_lint_passes, lint_groups, llvm_passes, attributes, mir_passes, .. } = registry; + let sess = &state.session; + let mut ls = sess.lint_store.borrow_mut(); + for pass in early_lint_passes { + ls.register_early_pass(Some(sess), true, pass); + } + for pass in late_lint_passes { + ls.register_late_pass(Some(sess), true, pass); + } + + for (name, to) in lint_groups { + ls.register_group(Some(sess), true, name, to); + } + + *sess.plugin_llvm_passes.borrow_mut() = llvm_passes; + *sess.plugin_mir_passes.borrow_mut() = mir_passes; + *sess.plugin_attributes.borrow_mut() = attributes; + }); + control.after_analysis.stop = Compilation::Stop; + + control + } +} + use std::path::Path; -#[cfg(not(test))] fn main() { - use std::{env, fs}; - use std::process::{self, Command}; + use std::env; - let path = env::current_exe().unwrap(); - let path = fs::canonicalize(path).unwrap(); - let path = path.parent().unwrap(); - let path = path.join("deps"); + let path = env::current_dir().unwrap(); + let path = path.join("target").join("debug").join("deps"); let args = wrap_args(env::args(), path); - let mut command = Command::new("cargo"); - command.args(&args); - let mut child = command.spawn().unwrap_or_else(|e| panic!("{}", e)); - let exit_status = child.wait().unwrap_or_else(|e| panic!("{}", e)); - - if let Some(code) = exit_status.code() { - process::exit(code); + for file in std::fs::read_dir("src").expect("no `src` directory available") { + if let Ok(file) = file { + let name = file.file_name(); + if let Some(name) = name.to_str() { + if name == "lib.rs" || name == "main.rs" { + let mut args = args.clone(); + args.push(format!("src/{}", name)); + if name == "lib.rs" { + args.push("--crate-type".to_owned()); + args.push("lib".to_owned()); + } + println!("{:?}", args); + rustc_driver::run_compiler(&args, &mut ClippyCompilerCalls); + } + } + } } } -fn wrap_args(it: I, clippy_path: P) -> Vec +fn wrap_args(it: I, dep_path: P) -> Vec where T: AsRef, I: IntoIterator, P: AsRef { let it = it.into_iter(); let mut args = vec!["rustc".to_owned()]; - let mut has_double_hyphen = false; for arg in it.skip(2) { let arg = arg.as_ref().to_owned(); - has_double_hyphen |= &arg == "--"; args.push(arg); } - - if !has_double_hyphen { - args.push("--".to_owned()); - } args.push("-L".to_owned()); - args.push(clippy_path.as_ref().to_string_lossy().into_owned()); - args.push("-lclippy".to_owned()); - args.push("-Zextra-plugins=clippy".to_owned()); + args.push(dep_path.as_ref().to_string_lossy().into_owned()); + args.push(String::from("--sysroot")); + args.push(format!("{}/.multirust/toolchains/nightly", std::env::var("HOME").unwrap())); args.push("-Zno-trans".to_owned()); + args.push("-Dclippy"); args }