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