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