]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/tool.rs
Auto merge of #43558 - GuillaumeGomez:union-const-colors, r=QuietMisdreavus
[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 struct CleanTools {
27     compiler: Compiler,
28     target: Interned<String>,
29     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         builder.ensure(CleanTools { compiler, target, mode: self.mode });
86         match self.mode {
87             Mode::Libstd => builder.ensure(compile::Std { compiler, target }),
88             Mode::Libtest => builder.ensure(compile::Test { compiler, target }),
89             Mode::Librustc => builder.ensure(compile::Rustc { compiler, target }),
90             Mode::Tool => panic!("unexpected Mode::Tool for tool build")
91         }
92
93         let _folder = build.fold_output(|| format!("stage{}-{}", compiler.stage, tool));
94         println!("Building stage{} tool {} ({})", compiler.stage, tool, target);
95
96         let mut cargo = builder.cargo(compiler, Mode::Tool, target, "build");
97         let dir = build.src.join("src/tools").join(tool);
98         cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
99
100         // We don't want to build tools dynamically as they'll be running across
101         // stages and such and it's just easier if they're not dynamically linked.
102         cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
103
104         if let Some(dir) = build.openssl_install_dir(target) {
105             cargo.env("OPENSSL_STATIC", "1");
106             cargo.env("OPENSSL_DIR", dir);
107             cargo.env("LIBZ_SYS_STATIC", "1");
108         }
109
110         cargo.env("CFG_RELEASE_CHANNEL", &build.config.channel);
111
112         let info = GitInfo::new(&dir);
113         if let Some(sha) = info.sha() {
114             cargo.env("CFG_COMMIT_HASH", sha);
115         }
116         if let Some(sha_short) = info.sha_short() {
117             cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
118         }
119         if let Some(date) = info.commit_date() {
120             cargo.env("CFG_COMMIT_DATE", date);
121         }
122
123         build.run(&mut cargo);
124         build.cargo_out(compiler, Mode::Tool, target).join(exe(tool, &compiler.host))
125     }
126 }
127
128 macro_rules! tool {
129     ($($name:ident, $path:expr, $tool_name:expr, $mode:expr;)+) => {
130         #[derive(Copy, Clone)]
131         pub enum Tool {
132             $(
133                 $name,
134             )+
135         }
136
137         impl<'a> Builder<'a> {
138             pub fn tool_exe(&self, tool: Tool) -> PathBuf {
139                 match tool {
140                     $(Tool::$name =>
141                         self.ensure($name {
142                             compiler: self.compiler(0, self.build.build),
143                             target: self.build.build,
144                         }),
145                     )+
146                 }
147             }
148         }
149
150         $(
151             #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
152         pub struct $name {
153             pub compiler: Compiler,
154             pub target: Interned<String>,
155         }
156
157         impl Step for $name {
158             type Output = PathBuf;
159
160             fn should_run(run: ShouldRun) -> ShouldRun {
161                 run.path($path)
162             }
163
164             fn make_run(run: RunConfig) {
165                 run.builder.ensure($name {
166                     compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
167                     target: run.target,
168                 });
169             }
170
171             fn run(self, builder: &Builder) -> PathBuf {
172                 builder.ensure(ToolBuild {
173                     compiler: self.compiler,
174                     target: self.target,
175                     tool: $tool_name,
176                     mode: $mode,
177                 })
178             }
179         }
180         )+
181     }
182 }
183
184 tool!(
185     Rustbook, "src/tools/rustbook", "rustbook", Mode::Librustc;
186     ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::Librustc;
187     UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::Libstd;
188     Tidy, "src/tools/tidy", "tidy", Mode::Libstd;
189     Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::Libstd;
190     CargoTest, "src/tools/cargotest", "cargotest", Mode::Libstd;
191     Compiletest, "src/tools/compiletest", "compiletest", Mode::Libtest;
192     BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Librustc;
193     RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::Libstd;
194     RustInstaller, "src/tools/rust-installer", "rust-installer", Mode::Libstd;
195 );
196
197 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
198 pub struct RemoteTestServer {
199     pub compiler: Compiler,
200     pub target: Interned<String>,
201 }
202
203 impl Step for RemoteTestServer {
204     type Output = PathBuf;
205
206     fn should_run(run: ShouldRun) -> ShouldRun {
207         run.path("src/tools/remote-test-server")
208     }
209
210     fn make_run(run: RunConfig) {
211         run.builder.ensure(RemoteTestServer {
212             compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
213             target: run.target,
214         });
215     }
216
217     fn run(self, builder: &Builder) -> PathBuf {
218         builder.ensure(ToolBuild {
219             compiler: self.compiler,
220             target: self.target,
221             tool: "remote-test-server",
222             mode: Mode::Libstd,
223         })
224     }
225 }
226
227 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
228 pub struct Rustdoc {
229     pub target_compiler: Compiler,
230 }
231
232 impl Step for Rustdoc {
233     type Output = PathBuf;
234     const DEFAULT: bool = true;
235     const ONLY_HOSTS: bool = true;
236
237     fn should_run(run: ShouldRun) -> ShouldRun {
238         run.path("src/tools/rustdoc")
239     }
240
241     fn make_run(run: RunConfig) {
242         run.builder.ensure(Rustdoc {
243             target_compiler: run.builder.compiler(run.builder.top_stage, run.host),
244         });
245     }
246
247     fn run(self, builder: &Builder) -> PathBuf {
248         let target_compiler = self.target_compiler;
249         let build_compiler = if target_compiler.stage == 0 {
250             builder.compiler(0, builder.build.build)
251         } else {
252             // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
253             // we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
254             // compilers, which isn't what we want.
255             builder.compiler(target_compiler.stage - 1, builder.build.build)
256         };
257
258         let tool_rustdoc = builder.ensure(ToolBuild {
259             compiler: build_compiler,
260             target: target_compiler.host,
261             tool: "rustdoc",
262             mode: Mode::Librustc,
263         });
264
265         // don't create a stage0-sysroot/bin directory.
266         if target_compiler.stage > 0 {
267             let sysroot = builder.sysroot(target_compiler);
268             let bindir = sysroot.join("bin");
269             t!(fs::create_dir_all(&bindir));
270             let bin_rustdoc = bindir.join(exe("rustdoc", &*target_compiler.host));
271             let _ = fs::remove_file(&bin_rustdoc);
272             copy(&tool_rustdoc, &bin_rustdoc);
273             bin_rustdoc
274         } else {
275             tool_rustdoc
276         }
277     }
278 }
279
280 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
281 pub struct Cargo {
282     pub compiler: Compiler,
283     pub target: Interned<String>,
284 }
285
286 impl Step for Cargo {
287     type Output = PathBuf;
288     const DEFAULT: bool = true;
289     const ONLY_HOSTS: bool = true;
290
291     fn should_run(run: ShouldRun) -> ShouldRun {
292         let builder = run.builder;
293         run.path("src/tools/cargo").default_condition(builder.build.config.extended)
294     }
295
296     fn make_run(run: RunConfig) {
297         run.builder.ensure(Cargo {
298             compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
299             target: run.target,
300         });
301     }
302
303     fn run(self, builder: &Builder) -> PathBuf {
304         builder.ensure(native::Openssl {
305             target: self.target,
306         });
307         // Cargo depends on procedural macros, which requires a full host
308         // compiler to be available, so we need to depend on that.
309         builder.ensure(compile::Rustc {
310             compiler: self.compiler,
311             target: builder.build.build,
312         });
313         builder.ensure(ToolBuild {
314             compiler: self.compiler,
315             target: self.target,
316             tool: "cargo",
317             mode: Mode::Librustc,
318         })
319     }
320 }
321
322 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
323 pub struct Rls {
324     pub compiler: Compiler,
325     pub target: Interned<String>,
326 }
327
328 impl Step for Rls {
329     type Output = PathBuf;
330     const DEFAULT: bool = true;
331     const ONLY_HOSTS: bool = true;
332
333     fn should_run(run: ShouldRun) -> ShouldRun {
334         let builder = run.builder;
335         run.path("src/tools/rls").default_condition(builder.build.config.extended)
336     }
337
338     fn make_run(run: RunConfig) {
339         run.builder.ensure(Rls {
340             compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
341             target: run.target,
342         });
343     }
344
345     fn run(self, builder: &Builder) -> PathBuf {
346         builder.ensure(native::Openssl {
347             target: self.target,
348         });
349         // RLS depends on procedural macros, which requires a full host
350         // compiler to be available, so we need to depend on that.
351         builder.ensure(compile::Rustc {
352             compiler: self.compiler,
353             target: builder.build.build,
354         });
355         builder.ensure(ToolBuild {
356             compiler: self.compiler,
357             target: self.target,
358             tool: "rls",
359             mode: Mode::Librustc,
360         })
361     }
362 }
363
364 impl<'a> Builder<'a> {
365     /// Get a `Command` which is ready to run `tool` in `stage` built for
366     /// `host`.
367     pub fn tool_cmd(&self, tool: Tool) -> Command {
368         let mut cmd = Command::new(self.tool_exe(tool));
369         let compiler = self.compiler(0, self.build.build);
370         self.prepare_tool_cmd(compiler, &mut cmd);
371         cmd
372     }
373
374     /// Prepares the `cmd` provided to be able to run the `compiler` provided.
375     ///
376     /// Notably this munges the dynamic library lookup path to point to the
377     /// right location to run `compiler`.
378     fn prepare_tool_cmd(&self, compiler: Compiler, cmd: &mut Command) {
379         let host = &compiler.host;
380         let mut paths: Vec<PathBuf> = vec![
381             PathBuf::from(&self.sysroot_libdir(compiler, compiler.host)),
382             self.cargo_out(compiler, Mode::Tool, *host).join("deps"),
383         ];
384
385         // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
386         // mode) and that C compiler may need some extra PATH modification. Do
387         // so here.
388         if compiler.host.contains("msvc") {
389             let curpaths = env::var_os("PATH").unwrap_or_default();
390             let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
391             for &(ref k, ref v) in self.cc[&compiler.host].0.env() {
392                 if k != "PATH" {
393                     continue
394                 }
395                 for path in env::split_paths(v) {
396                     if !curpaths.contains(&path) {
397                         paths.push(path);
398                     }
399                 }
400             }
401         }
402         add_lib_path(paths, cmd);
403     }
404 }