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