|
9 | 9 | #![allow(indexing_slicing, shadow_reuse, unknown_lints)]
|
10 | 10 | #![allow(float_arithmetic, integer_arithmetic)]
|
11 | 11 |
|
12 |
| -// this only exists to allow the "dogfood" integration test to work |
13 |
| -#[allow(dead_code)] |
14 |
| -#[allow(print_stdout)] |
15 |
| -fn main() { |
16 |
| - println!("What are you doing? Don't run clippy as an executable"); |
| 12 | +extern crate rustc_driver; |
| 13 | +extern crate getopts; |
| 14 | + |
| 15 | +use rustc_driver::{driver, CompilerCalls, RustcDefaultCalls, Compilation}; |
| 16 | +use rustc::session::{config, Session}; |
| 17 | +use rustc::session::config::{Input, ErrorOutputType}; |
| 18 | +use syntax::diagnostics; |
| 19 | +use std::path::PathBuf; |
| 20 | + |
| 21 | +struct ClippyCompilerCalls(RustcDefaultCalls); |
| 22 | + |
| 23 | +impl std::default::Default for ClippyCompilerCalls { |
| 24 | + fn default() -> Self { |
| 25 | + Self::new() |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl ClippyCompilerCalls { |
| 30 | + fn new() -> Self { |
| 31 | + ClippyCompilerCalls(RustcDefaultCalls) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl<'a> CompilerCalls<'a> for ClippyCompilerCalls { |
| 36 | + fn early_callback(&mut self, |
| 37 | + matches: &getopts::Matches, |
| 38 | + sopts: &config::Options, |
| 39 | + descriptions: &diagnostics::registry::Registry, |
| 40 | + output: ErrorOutputType) |
| 41 | + -> Compilation { |
| 42 | + self.0.early_callback(matches, sopts, descriptions, output) |
| 43 | + } |
| 44 | + fn no_input(&mut self, |
| 45 | + matches: &getopts::Matches, |
| 46 | + sopts: &config::Options, |
| 47 | + odir: &Option<PathBuf>, |
| 48 | + ofile: &Option<PathBuf>, |
| 49 | + descriptions: &diagnostics::registry::Registry) |
| 50 | + -> Option<(Input, Option<PathBuf>)> { |
| 51 | + self.0.no_input(matches, sopts, odir, ofile, descriptions) |
| 52 | + } |
| 53 | + fn late_callback(&mut self, |
| 54 | + matches: &getopts::Matches, |
| 55 | + sess: &Session, |
| 56 | + input: &Input, |
| 57 | + odir: &Option<PathBuf>, |
| 58 | + ofile: &Option<PathBuf>) |
| 59 | + -> Compilation { |
| 60 | + self.0.late_callback(matches, sess, input, odir, ofile) |
| 61 | + } |
| 62 | + fn build_controller(&mut self, sess: &Session, matches: &getopts::Matches) -> driver::CompileController<'a> { |
| 63 | + let mut control = self.0.build_controller(sess, matches); |
| 64 | + |
| 65 | + let old = std::mem::replace(&mut control.after_parse.callback, box |_| {}); |
| 66 | + control.after_parse.callback = Box::new(move |state| { |
| 67 | + { |
| 68 | + let mut registry = rustc_plugin::registry::Registry::new(state.session, state.krate.as_ref().expect("at this compilation stage the krate must be parsed")); |
| 69 | + registry.args_hidden = Some(Vec::new()); |
| 70 | + plugin_registrar(&mut registry); |
| 71 | + |
| 72 | + let rustc_plugin::registry::Registry { early_lint_passes, late_lint_passes, lint_groups, llvm_passes, attributes, mir_passes, .. } = registry; |
| 73 | + let sess = &state.session; |
| 74 | + let mut ls = sess.lint_store.borrow_mut(); |
| 75 | + for pass in early_lint_passes { |
| 76 | + ls.register_early_pass(Some(sess), true, pass); |
| 77 | + } |
| 78 | + for pass in late_lint_passes { |
| 79 | + ls.register_late_pass(Some(sess), true, pass); |
| 80 | + } |
| 81 | + |
| 82 | + for (name, to) in lint_groups { |
| 83 | + ls.register_group(Some(sess), true, name, to); |
| 84 | + } |
| 85 | + |
| 86 | + sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes); |
| 87 | + sess.mir_passes.borrow_mut().extend(mir_passes); |
| 88 | + sess.plugin_attributes.borrow_mut().extend(attributes); |
| 89 | + } |
| 90 | + old(state); |
| 91 | + }); |
| 92 | + |
| 93 | + control |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +use std::path::Path; |
| 98 | + |
| 99 | +pub fn main() { |
| 100 | + use std::env; |
| 101 | + |
| 102 | + if env::var("CLIPPY_DOGFOOD").map(|_| true).unwrap_or(false) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + let dep_path = env::current_dir().expect("current dir is not readable").join("target").join("debug").join("deps"); |
| 107 | + let sys_root = match (option_env!("MULTIRUST_HOME"), option_env!("MULTIRUST_TOOLCHAIN")) { |
| 108 | + (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain), |
| 109 | + _ => option_env!("SYSROOT").expect("need to specify SYSROOT env var during clippy compilation or use multirust").to_owned(), |
| 110 | + }; |
| 111 | + |
| 112 | + if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) { |
| 113 | + let args = wrap_args(std::env::args().skip(2), dep_path, sys_root); |
| 114 | + let path = std::env::current_exe().expect("current executable path invalid"); |
| 115 | + let run = std::process::Command::new("cargo") |
| 116 | + .args(&args) |
| 117 | + .env("RUSTC", path) |
| 118 | + .spawn().expect("could not run cargo") |
| 119 | + .wait().expect("failed to wait for cargo?") |
| 120 | + .success(); |
| 121 | + assert!(run, "cargo rustc failed"); |
| 122 | + } else { |
| 123 | + let args: Vec<String> = if env::args().any(|s| s == "--sysroot") { |
| 124 | + env::args().collect() |
| 125 | + } else { |
| 126 | + env::args().chain(Some("--sysroot".to_owned())).chain(Some(sys_root)).collect() |
| 127 | + }; |
| 128 | + rustc_driver::run_compiler(&args, &mut ClippyCompilerCalls::new()); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +fn wrap_args<P, I>(old_args: I, dep_path: P, sysroot: String) -> Vec<String> |
| 133 | + where P: AsRef<Path>, I: Iterator<Item=String> { |
| 134 | + |
| 135 | + let mut args = vec!["rustc".to_owned()]; |
| 136 | + |
| 137 | + let mut found_dashes = false; |
| 138 | + for arg in old_args { |
| 139 | + found_dashes |= arg == "--"; |
| 140 | + args.push(arg); |
| 141 | + } |
| 142 | + if !found_dashes { |
| 143 | + args.push("--".to_owned()); |
| 144 | + } |
| 145 | + args.push("-L".to_owned()); |
| 146 | + args.push(dep_path.as_ref().to_string_lossy().into_owned()); |
| 147 | + args.push(String::from("--sysroot")); |
| 148 | + args.push(sysroot); |
| 149 | + args.push("-Zno-trans".to_owned()); |
| 150 | + args |
17 | 151 | }
|
18 | 152 |
|
19 | 153 | #[macro_use]
|
|
0 commit comments