]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/tool.rs
Add clippy as a submodule
[rust.git] / src / bootstrap / tool.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::fs;
12 use std::env;
13 use std::path::PathBuf;
14 use std::process::Command;
15
16 use Mode;
17 use Compiler;
18 use builder::{Step, RunConfig, ShouldRun, Builder};
19 use util::{copy, exe, add_lib_path};
20 use compile::{self, libtest_stamp, libstd_stamp, librustc_stamp};
21 use native;
22 use channel::GitInfo;
23 use cache::Interned;
24
25 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
26 pub struct CleanTools {
27     pub compiler: Compiler,
28     pub target: Interned<String>,
29     pub mode: Mode,
30 }
31
32 impl Step for CleanTools {
33     type Output = ();
34
35     fn should_run(run: ShouldRun) -> ShouldRun {
36         run.never()
37     }
38
39     /// Build a tool in `src/tools`
40     ///
41     /// This will build the specified tool with the specified `host` compiler in
42     /// `stage` into the normal cargo output directory.
43     fn run(self, builder: &Builder) {
44         let build = builder.build;
45         let compiler = self.compiler;
46         let target = self.target;
47         let mode = self.mode;
48
49         let stamp = match mode {
50             Mode::Libstd => libstd_stamp(build, compiler, target),
51             Mode::Libtest => libtest_stamp(build, compiler, target),
52             Mode::Librustc => librustc_stamp(build, compiler, target),
53             _ => panic!(),
54         };
55         let out_dir = build.cargo_out(compiler, Mode::Tool, target);
56         build.clear_if_dirty(&out_dir, &stamp);
57     }
58 }
59
60 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
61 struct ToolBuild {
62     compiler: Compiler,
63     target: Interned<String>,
64     tool: &'static str,
65     mode: Mode,
66 }
67
68 impl Step for ToolBuild {
69     type Output = PathBuf;
70
71     fn should_run(run: ShouldRun) -> ShouldRun {
72         run.never()
73     }
74
75     /// Build a tool in `src/tools`
76     ///
77     /// This will build the specified tool with the specified `host` compiler in
78     /// `stage` into the normal cargo output directory.
79     fn run(self, builder: &Builder) -> PathBuf {
80         let build = builder.build;
81         let compiler = self.compiler;
82         let target = self.target;
83         let tool = self.tool;
84
85         match self.mode {
86             Mode::Libstd => builder.ensure(compile::Std { compiler, target }),
87             Mode::Libtest => builder.ensure(compile::Test { compiler, target }),
88             Mode::Librustc => builder.ensure(compile::Rustc { compiler, target }),
89             Mode::Tool => panic!("unexpected Mode::Tool for tool build")
90         }
91
92         let _folder = build.fold_output(|| format!("stage{}-{}", compiler.stage, tool));
93         println!("Building stage{} tool {} ({})", compiler.stage, tool, target);
94
95         let mut cargo = prepare_tool_cargo(builder, compiler, target, tool);
96         build.run(&mut cargo);
97         build.cargo_out(compiler, Mode::Tool, target).join(exe(tool, &compiler.host))
98     }
99 }
100
101 fn prepare_tool_cargo(
102     builder: &Builder,
103     compiler: Compiler,
104     target: Interned<String>,
105     tool: &'static str,
106 ) -> Command {
107     let build = builder.build;
108     let mut cargo = builder.cargo(compiler, Mode::Tool, target, "build");
109     let dir = build.src.join("src/tools").join(tool);
110     cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
111
112     // We don't want to build tools dynamically as they'll be running across
113     // stages and such and it's just easier if they're not dynamically linked.
114     cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
115
116     if let Some(dir) = build.openssl_install_dir(target) {
117         cargo.env("OPENSSL_STATIC", "1");
118         cargo.env("OPENSSL_DIR", dir);
119         cargo.env("LIBZ_SYS_STATIC", "1");
120     }
121
122     cargo.env("CFG_RELEASE_CHANNEL", &build.config.channel);
123
124     let info = GitInfo::new(&build.config, &dir);
125     if let Some(sha) = info.sha() {
126         cargo.env("CFG_COMMIT_HASH", sha);
127     }
128     if let Some(sha_short) = info.sha_short() {
129         cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
130     }
131     if let Some(date) = info.commit_date() {
132         cargo.env("CFG_COMMIT_DATE", date);
133     }
134     cargo
135 }
136
137 macro_rules! tool {
138     ($($name:ident, $path:expr, $tool_name:expr, $mode:expr;)+) => {
139         #[derive(Copy, Clone)]
140         pub enum Tool {
141             $(
142                 $name,
143             )+
144         }
145
146         impl<'a> Builder<'a> {
147             pub fn tool_exe(&self, tool: Tool) -> PathBuf {
148                 match tool {
149                     $(Tool::$name =>
150                         self.ensure($name {
151                             compiler: self.compiler(0, self.build.build),
152                             target: self.build.build,
153                         }),
154                     )+
155                 }
156             }
157         }
158
159         $(
160             #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
161         pub struct $name {
162             pub compiler: Compiler,
163             pub target: Interned<String>,
164         }
165
166         impl Step for $name {
167             type Output = PathBuf;
168
169             fn should_run(run: ShouldRun) -> ShouldRun {
170                 run.path($path)
171             }
172
173             fn make_run(run: RunConfig) {
174                 run.builder.ensure($name {
175                     compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
176                     target: run.target,
177                 });
178             }
179
180             fn run(self, builder: &Builder) -> PathBuf {
181                 builder.ensure(ToolBuild {
182                     compiler: self.compiler,
183                     target: self.target,
184                     tool: $tool_name,
185                     mode: $mode,
186                 })
187             }
188         }
189         )+
190     }
191 }
192
193 tool!(
194     Rustbook, "src/tools/rustbook", "rustbook", Mode::Librustc;
195     ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::Librustc;
196     UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::Libstd;
197     Tidy, "src/tools/tidy", "tidy", Mode::Libstd;
198     Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::Libstd;
199     CargoTest, "src/tools/cargotest", "cargotest", Mode::Libstd;
200     Compiletest, "src/tools/compiletest", "compiletest", Mode::Libtest;
201     BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Librustc;
202     RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::Libstd;
203     RustInstaller, "src/tools/rust-installer", "rust-installer", Mode::Libstd;
204 );
205
206 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
207 pub struct RemoteTestServer {
208     pub compiler: Compiler,
209     pub target: Interned<String>,
210 }
211
212 impl Step for RemoteTestServer {
213     type Output = PathBuf;
214
215     fn should_run(run: ShouldRun) -> ShouldRun {
216         run.path("src/tools/remote-test-server")
217     }
218
219     fn make_run(run: RunConfig) {
220         run.builder.ensure(RemoteTestServer {
221             compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
222             target: run.target,
223         });
224     }
225
226     fn run(self, builder: &Builder) -> PathBuf {
227         builder.ensure(ToolBuild {
228             compiler: self.compiler,
229             target: self.target,
230             tool: "remote-test-server",
231             mode: Mode::Libstd,
232         })
233     }
234 }
235
236 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
237 pub struct Rustdoc {
238     pub host: Interned<String>,
239 }
240
241 impl Step for Rustdoc {
242     type Output = PathBuf;
243     const DEFAULT: bool = true;
244     const ONLY_HOSTS: bool = true;
245
246     fn should_run(run: ShouldRun) -> ShouldRun {
247         run.path("src/tools/rustdoc")
248     }
249
250     fn make_run(run: RunConfig) {
251         run.builder.ensure(Rustdoc {
252             host: run.host,
253         });
254     }
255
256     fn run(self, builder: &Builder) -> PathBuf {
257         let build = builder.build;
258         let target_compiler = builder.compiler(builder.top_stage, self.host);
259         let target = target_compiler.host;
260         let build_compiler = if target_compiler.stage == 0 {
261             builder.compiler(0, builder.build.build)
262         } else if target_compiler.stage >= 2 {
263             // Past stage 2, we consider the compiler to be ABI-compatible and hence capable of
264             // building rustdoc itself.
265             builder.compiler(target_compiler.stage, builder.build.build)
266         } else {
267             // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
268             // we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
269             // compilers, which isn't what we want.
270             builder.compiler(target_compiler.stage - 1, builder.build.build)
271         };
272
273         builder.ensure(compile::Rustc { compiler: build_compiler, target });
274
275         let _folder = build.fold_output(|| format!("stage{}-rustdoc", target_compiler.stage));
276         println!("Building rustdoc for stage{} ({})", target_compiler.stage, target_compiler.host);
277
278         let mut cargo = prepare_tool_cargo(builder, build_compiler, target, "rustdoc");
279         build.run(&mut cargo);
280         // Cargo adds a number of paths to the dylib search path on windows, which results in
281         // the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
282         // rustdoc a different name.
283         let tool_rustdoc = build.cargo_out(build_compiler, Mode::Tool, target)
284             .join(exe("rustdoc-tool-binary", &target_compiler.host));
285
286         // don't create a stage0-sysroot/bin directory.
287         if target_compiler.stage > 0 {
288             let sysroot = builder.sysroot(target_compiler);
289             let bindir = sysroot.join("bin");
290             t!(fs::create_dir_all(&bindir));
291             let bin_rustdoc = bindir.join(exe("rustdoc", &*target_compiler.host));
292             let _ = fs::remove_file(&bin_rustdoc);
293             copy(&tool_rustdoc, &bin_rustdoc);
294             bin_rustdoc
295         } else {
296             tool_rustdoc
297         }
298     }
299 }
300
301 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
302 pub struct Cargo {
303     pub compiler: Compiler,
304     pub target: Interned<String>,
305 }
306
307 impl Step for Cargo {
308     type Output = PathBuf;
309     const DEFAULT: bool = true;
310     const ONLY_HOSTS: bool = true;
311
312     fn should_run(run: ShouldRun) -> ShouldRun {
313         let builder = run.builder;
314         run.path("src/tools/cargo").default_condition(builder.build.config.extended)
315     }
316
317     fn make_run(run: RunConfig) {
318         run.builder.ensure(Cargo {
319             compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
320             target: run.target,
321         });
322     }
323
324     fn run(self, builder: &Builder) -> PathBuf {
325         builder.ensure(native::Openssl {
326             target: self.target,
327         });
328         // Cargo depends on procedural macros, which requires a full host
329         // compiler to be available, so we need to depend on that.
330         builder.ensure(compile::Rustc {
331             compiler: self.compiler,
332             target: builder.build.build,
333         });
334         builder.ensure(ToolBuild {
335             compiler: self.compiler,
336             target: self.target,
337             tool: "cargo",
338             mode: Mode::Librustc,
339         })
340     }
341 }
342
343 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
344 pub struct Clippy {
345     pub compiler: Compiler,
346     pub target: Interned<String>,
347 }
348
349 impl Step for Clippy {
350     type Output = PathBuf;
351     const DEFAULT: bool = true;
352     const ONLY_HOSTS: bool = true;
353
354     fn should_run(run: ShouldRun) -> ShouldRun {
355         run.path("src/tools/clippy")
356     }
357
358     fn make_run(run: RunConfig) {
359         run.builder.ensure(Clippy {
360             compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
361             target: run.target,
362         });
363     }
364
365     fn run(self, builder: &Builder) -> PathBuf {
366         // Clippy depends on procedural macros (serde), which requires a full host
367         // compiler to be available, so we need to depend on that.
368         builder.ensure(compile::Rustc {
369             compiler: self.compiler,
370             target: builder.build.build,
371         });
372         builder.ensure(ToolBuild {
373             compiler: self.compiler,
374             target: self.target,
375             tool: "clippy",
376             mode: Mode::Librustc,
377         })
378     }
379 }
380
381 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
382 pub struct Rls {
383     pub compiler: Compiler,
384     pub target: Interned<String>,
385 }
386
387 impl Step for Rls {
388     type Output = PathBuf;
389     const DEFAULT: bool = true;
390     const ONLY_HOSTS: bool = true;
391
392     fn should_run(run: ShouldRun) -> ShouldRun {
393         let builder = run.builder;
394         run.path("src/tools/rls").default_condition(builder.build.config.extended)
395     }
396
397     fn make_run(run: RunConfig) {
398         run.builder.ensure(Rls {
399             compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
400             target: run.target,
401         });
402     }
403
404     fn run(self, builder: &Builder) -> PathBuf {
405         builder.ensure(native::Openssl {
406             target: self.target,
407         });
408         // RLS depends on procedural macros, which requires a full host
409         // compiler to be available, so we need to depend on that.
410         builder.ensure(compile::Rustc {
411             compiler: self.compiler,
412             target: builder.build.build,
413         });
414         builder.ensure(ToolBuild {
415             compiler: self.compiler,
416             target: self.target,
417             tool: "rls",
418             mode: Mode::Librustc,
419         })
420     }
421 }
422
423 impl<'a> Builder<'a> {
424     /// Get a `Command` which is ready to run `tool` in `stage` built for
425     /// `host`.
426     pub fn tool_cmd(&self, tool: Tool) -> Command {
427         let mut cmd = Command::new(self.tool_exe(tool));
428         let compiler = self.compiler(0, self.build.build);
429         self.prepare_tool_cmd(compiler, &mut cmd);
430         cmd
431     }
432
433     /// Prepares the `cmd` provided to be able to run the `compiler` provided.
434     ///
435     /// Notably this munges the dynamic library lookup path to point to the
436     /// right location to run `compiler`.
437     fn prepare_tool_cmd(&self, compiler: Compiler, cmd: &mut Command) {
438         let host = &compiler.host;
439         let mut paths: Vec<PathBuf> = vec![
440             PathBuf::from(&self.sysroot_libdir(compiler, compiler.host)),
441             self.cargo_out(compiler, Mode::Tool, *host).join("deps"),
442         ];
443
444         // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
445         // mode) and that C compiler may need some extra PATH modification. Do
446         // so here.
447         if compiler.host.contains("msvc") {
448             let curpaths = env::var_os("PATH").unwrap_or_default();
449             let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
450             for &(ref k, ref v) in self.cc[&compiler.host].0.env() {
451                 if k != "PATH" {
452                     continue
453                 }
454                 for path in env::split_paths(v) {
455                     if !curpaths.contains(&path) {
456                         paths.push(path);
457                     }
458                 }
459             }
460         }
461         add_lib_path(paths, cmd);
462     }
463 }