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