]> git.lizzy.rs Git - rust.git/blob - src/driver.rs
Auto merge of #3552 - phansch:make_integration_tests_fail_again, r=oli-obk
[rust.git] / src / driver.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 // error-pattern:yummy
11 #![feature(box_syntax)]
12 #![feature(rustc_private)]
13 #![feature(try_from)]
14 #![allow(clippy::missing_docs_in_private_items)]
15
16 // FIXME: switch to something more ergonomic here, once available.
17 // (currently there is no way to opt into sysroot crates w/o `extern crate`)
18 #[allow(unused_extern_crates)]
19 extern crate rustc_driver;
20 #[allow(unused_extern_crates)]
21 extern crate rustc_plugin;
22 use self::rustc_driver::{driver::CompileController, Compilation};
23
24 use std::convert::TryInto;
25 use std::path::Path;
26 use std::process::{exit, Command};
27
28 fn show_version() {
29     println!(env!("CARGO_PKG_VERSION"));
30 }
31
32 pub fn main() {
33     rustc_driver::init_rustc_env_logger();
34     exit(
35         rustc_driver::run(move || {
36             use std::env;
37
38             if std::env::args().any(|a| a == "--version" || a == "-V") {
39                 show_version();
40                 exit(0);
41             }
42
43             let sys_root = option_env!("SYSROOT")
44                 .map(String::from)
45                 .or_else(|| std::env::var("SYSROOT").ok())
46                 .or_else(|| {
47                     let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
48                     let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
49                     home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
50                 })
51                 .or_else(|| {
52                     Command::new("rustc")
53                         .arg("--print")
54                         .arg("sysroot")
55                         .output()
56                         .ok()
57                         .and_then(|out| String::from_utf8(out.stdout).ok())
58                         .map(|s| s.trim().to_owned())
59                 })
60                 .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
61
62             // Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
63             // We're invoking the compiler programmatically, so we ignore this/
64             let mut orig_args: Vec<String> = env::args().collect();
65             if orig_args.len() <= 1 {
66                 std::process::exit(1);
67             }
68             if Path::new(&orig_args[1]).file_stem() == Some("rustc".as_ref()) {
69                 // we still want to be able to invoke it normally though
70                 orig_args.remove(1);
71             }
72             // this conditional check for the --sysroot flag is there so users can call
73             // `clippy_driver` directly
74             // without having to pass --sysroot or anything
75             let mut args: Vec<String> = if orig_args.iter().any(|s| s == "--sysroot") {
76                 orig_args.clone()
77             } else {
78                 orig_args
79                     .clone()
80                     .into_iter()
81                     .chain(Some("--sysroot".to_owned()))
82                     .chain(Some(sys_root))
83                     .collect()
84             };
85
86             // this check ensures that dependencies are built but not linted and the final
87             // crate is
88             // linted but not built
89             let clippy_enabled = env::var("CLIPPY_TESTS").ok().map_or(false, |val| val == "true")
90                 || orig_args.iter().any(|s| s == "--emit=dep-info,metadata");
91
92             if clippy_enabled {
93                 args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
94                 if let Ok(extra_args) = env::var("CLIPPY_ARGS") {
95                     args.extend(extra_args.split("__CLIPPY_HACKERY__").filter_map(|s| {
96                         if s.is_empty() {
97                             None
98                         } else {
99                             Some(s.to_string())
100                         }
101                     }));
102                 }
103             }
104
105             let mut controller = CompileController::basic();
106             if clippy_enabled {
107                 controller.after_parse.callback = Box::new(move |state| {
108                     let mut registry = rustc_plugin::registry::Registry::new(
109                         state.session,
110                         state
111                             .krate
112                             .as_ref()
113                             .expect(
114                                 "at this compilation stage \
115                                  the crate must be parsed",
116                             )
117                             .span,
118                     );
119                     registry.args_hidden = Some(Vec::new());
120
121                     let conf = clippy_lints::read_conf(&registry);
122                     clippy_lints::register_plugins(&mut registry, &conf);
123
124                     let rustc_plugin::registry::Registry {
125                         early_lint_passes,
126                         late_lint_passes,
127                         lint_groups,
128                         llvm_passes,
129                         attributes,
130                         ..
131                     } = registry;
132                     let sess = &state.session;
133                     let mut ls = sess.lint_store.borrow_mut();
134                     for pass in early_lint_passes {
135                         ls.register_early_pass(Some(sess), true, pass);
136                     }
137                     for pass in late_lint_passes {
138                         ls.register_late_pass(Some(sess), true, pass);
139                     }
140
141                     for (name, (to, deprecated_name)) in lint_groups {
142                         ls.register_group(Some(sess), true, name, deprecated_name, to);
143                     }
144                     clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
145                     clippy_lints::register_renamed(&mut ls);
146
147                     sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
148                     sess.plugin_attributes.borrow_mut().extend(attributes);
149                 });
150             }
151             controller.compilation_done.stop = Compilation::Stop;
152
153             let args = args;
154             rustc_driver::run_compiler(&args, Box::new(controller), None, None)
155         })
156         .try_into()
157         .expect("exit code too large"),
158     )
159 }