]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/tool.rs
Remove core_intrinsics feature gate
[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::env;
12 use std::path::{Path, PathBuf};
13 use std::process::Command;
14
15 use Mode;
16 use Compiler;
17 use builder::{Step, Builder};
18 use util::{exe, add_lib_path};
19 use compile::{self, libtest_stamp, libstd_stamp, librustc_stamp, Rustc};
20 use native;
21 use channel::GitInfo;
22
23 //// ========================================================================
24 //// Build tools
25 ////
26 //// Tools used during the build system but not shipped
27 //// "pseudo rule" which represents completely cleaning out the tools dir in
28 //// one stage. This needs to happen whenever a dependency changes (e.g.
29 //// libstd, libtest, librustc) and all of the tool compilations above will
30 //// be sequenced after this rule.
31 //rules.build("maybe-clean-tools", "path/to/nowhere")
32 //     .after("librustc-tool")
33 //     .after("libtest-tool")
34 //     .after("libstd-tool");
35 //
36 //rules.build("librustc-tool", "path/to/nowhere")
37 //     .dep(|s| s.name("librustc"))
38 //     .run(move |s| compile::maybe_clean_tools(build, s.stage, s.target, Mode::Librustc));
39 //rules.build("libtest-tool", "path/to/nowhere")
40 //     .dep(|s| s.name("libtest"))
41 //     .run(move |s| compile::maybe_clean_tools(build, s.stage, s.target, Mode::Libtest));
42 //rules.build("libstd-tool", "path/to/nowhere")
43 //     .dep(|s| s.name("libstd"))
44 //     .run(move |s| compile::maybe_clean_tools(build, s.stage, s.target, Mode::Libstd));
45 //
46
47 #[derive(Serialize)]
48 pub struct CleanTools<'a> {
49     pub stage: u32,
50     pub target: &'a str,
51     pub mode: Mode,
52 }
53
54 impl<'a> Step<'a> for CleanTools<'a> {
55     type Id = CleanTools<'static>;
56     type Output = ();
57
58     /// Build a tool in `src/tools`
59     ///
60     /// This will build the specified tool with the specified `host` compiler in
61     /// `stage` into the normal cargo output directory.
62     fn run(self, builder: &Builder) {
63         let build = builder.build;
64         let stage = self.stage;
65         let target = self.target;
66         let mode = self.mode;
67
68         let compiler = builder.compiler(stage, &build.build);
69
70         let stamp = match mode {
71             Mode::Libstd => libstd_stamp(build, compiler, target),
72             Mode::Libtest => libtest_stamp(build, compiler, target),
73             Mode::Librustc => librustc_stamp(build, compiler, target),
74             _ => panic!(),
75         };
76         let out_dir = build.cargo_out(compiler, Mode::Tool, target);
77         build.clear_if_dirty(&out_dir, &stamp);
78     }
79 }
80
81 #[derive(Serialize)]
82 pub struct ToolBuild<'a> {
83     pub stage: u32,
84     pub target: &'a str,
85     pub tool: &'a str,
86     pub mode: Mode,
87 }
88
89 impl<'a> Step<'a> for ToolBuild<'a> {
90     type Id = ToolBuild<'static>;
91     type Output = PathBuf;
92
93     /// Build a tool in `src/tools`
94     ///
95     /// This will build the specified tool with the specified `host` compiler in
96     /// `stage` into the normal cargo output directory.
97     fn run(self, builder: &Builder) -> PathBuf {
98         let build = builder.build;
99         let stage = self.stage;
100         let target = self.target;
101         let tool = self.tool;
102
103         let compiler = builder.compiler(stage, &build.build);
104         builder.ensure(CleanTools { stage, target, mode: self.mode });
105         match self.mode {
106             Mode::Libstd => builder.ensure(compile::Std { compiler, target }),
107             Mode::Libtest => builder.ensure(compile::Test { compiler, target }),
108             Mode::Librustc => builder.ensure(compile::Rustc { compiler, target }),
109             Mode::Tool => panic!("unexpected Mode::Tool for tool build")
110         }
111
112         let _folder = build.fold_output(|| format!("stage{}-{}", stage, tool));
113         println!("Building stage{} tool {} ({})", stage, tool, target);
114
115         let mut cargo = builder.cargo(compiler, Mode::Tool, target, "build");
116         let dir = build.src.join("src/tools").join(tool);
117         cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
118
119         // We don't want to build tools dynamically as they'll be running across
120         // stages and such and it's just easier if they're not dynamically linked.
121         cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
122
123         if let Some(dir) = build.openssl_install_dir(target) {
124             cargo.env("OPENSSL_STATIC", "1");
125             cargo.env("OPENSSL_DIR", dir);
126             cargo.env("LIBZ_SYS_STATIC", "1");
127         }
128
129         cargo.env("CFG_RELEASE_CHANNEL", &build.config.channel);
130
131         let info = GitInfo::new(&dir);
132         if let Some(sha) = info.sha() {
133             cargo.env("CFG_COMMIT_HASH", sha);
134         }
135         if let Some(sha_short) = info.sha_short() {
136             cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
137         }
138         if let Some(date) = info.commit_date() {
139             cargo.env("CFG_COMMIT_DATE", date);
140         }
141
142         build.run(&mut cargo);
143         build.cargo_out(compiler, Mode::Tool, target).join(exe(tool, compiler.host))
144     }
145 }
146
147 macro_rules! tool {
148     ($($name:ident, $path:expr, $tool_name:expr, $mode:expr;)+) => {
149         #[derive(Copy, Clone)]
150         pub enum Tool {
151             $(
152                 $name,
153             )+
154         }
155
156         impl<'a> Builder<'a> {
157             pub fn tool_exe(&self, tool: Tool) -> PathBuf {
158                 match tool {
159                     $(Tool::$name =>
160                         self.ensure($name {
161                             stage: 0,
162                             target: &self.build.build,
163                         }),
164                     )+
165                 }
166             }
167         }
168
169         $(
170         #[derive(Serialize)]
171         pub struct $name<'a> {
172             pub stage: u32,
173             pub target: &'a str,
174         }
175
176         impl<'a> Step<'a> for $name<'a> {
177             type Id = $name<'static>;
178             type Output = PathBuf;
179
180             fn should_run(_builder: &Builder, path: &Path) -> bool {
181                 path.ends_with($path)
182             }
183
184             fn make_run(builder: &Builder, _path: Option<&Path>, _host: &str, target: &str) {
185                 builder.ensure($name {
186                     stage: builder.top_stage,
187                     target,
188                 });
189             }
190
191             fn run(self, builder: &Builder) -> PathBuf {
192                 builder.ensure(ToolBuild {
193                     stage: self.stage,
194                     target: self.target,
195                     tool: $tool_name,
196                     mode: $mode,
197                 })
198             }
199         }
200         )+
201     }
202 }
203
204 tool!(
205     // rules.build("tool-rustbook", "src/tools/rustbook")
206     //      .dep(|s| s.name("maybe-clean-tools"))
207     //      .dep(|s| s.name("librustc-tool"))
208     //      .run(move |s| compile::tool(build, s.stage, s.target, "rustbook"));
209     Rustbook, "src/tools/rustbook", "rustbook", Mode::Librustc;
210     // rules.build("tool-error-index", "src/tools/error_index_generator")
211     //      .dep(|s| s.name("maybe-clean-tools"))
212     //      .dep(|s| s.name("librustc-tool"))
213     //      .run(move |s| compile::tool(build, s.stage, s.target, "error_index_generator"));
214     ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::Librustc;
215     // rules.build("tool-unstable-book-gen", "src/tools/unstable-book-gen")
216     //      .dep(|s| s.name("maybe-clean-tools"))
217     //      .dep(|s| s.name("libstd-tool"))
218     //      .run(move |s| compile::tool(build, s.stage, s.target, "unstable-book-gen"));
219     UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::Libstd;
220     // rules.build("tool-tidy", "src/tools/tidy")
221     //      .dep(|s| s.name("maybe-clean-tools"))
222     //      .dep(|s| s.name("libstd-tool"))
223     //      .run(move |s| compile::tool(build, s.stage, s.target, "tidy"));
224     Tidy, "src/tools/tidy", "tidy", Mode::Libstd;
225     // rules.build("tool-linkchecker", "src/tools/linkchecker")
226     //      .dep(|s| s.name("maybe-clean-tools"))
227     //      .dep(|s| s.name("libstd-tool"))
228     //      .run(move |s| compile::tool(build, s.stage, s.target, "linkchecker"));
229     Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::Libstd;
230     // rules.build("tool-cargotest", "src/tools/cargotest")
231     //      .dep(|s| s.name("maybe-clean-tools"))
232     //      .dep(|s| s.name("libstd-tool"))
233     //      .run(move |s| compile::tool(build, s.stage, s.target, "cargotest"));
234     CargoTest, "src/tools/cargotest", "cargotest", Mode::Libstd;
235     // rules.build("tool-compiletest", "src/tools/compiletest")
236     //      .dep(|s| s.name("maybe-clean-tools"))
237     //      .dep(|s| s.name("libtest-tool"))
238     //      .run(move |s| compile::tool(build, s.stage, s.target, "compiletest"));
239     Compiletest, "src/tools/compiletest", "compiletest", Mode::Libtest;
240     // rules.build("tool-build-manifest", "src/tools/build-manifest")
241     //      .dep(|s| s.name("maybe-clean-tools"))
242     //      .dep(|s| s.name("libstd-tool"))
243     //      .run(move |s| compile::tool(build, s.stage, s.target, "build-manifest"));
244     BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Librustc;
245     // rules.build("tool-remote-test-client", "src/tools/remote-test-client")
246     //      .dep(|s| s.name("maybe-clean-tools"))
247     //      .dep(|s| s.name("libstd-tool"))
248     //      .run(move |s| compile::tool(build, s.stage, s.target, "remote-test-client"));
249     RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::Libstd;
250     // rules.build("tool-rust-installer", "src/tools/rust-installer")
251     //      .dep(|s| s.name("maybe-clean-tools"))
252     //      .dep(|s| s.name("libstd-tool"))
253     //      .run(move |s| compile::tool(build, s.stage, s.target, "rust-installer"));
254     RustInstaller, "src/tools/rust-installer", "rust-installer", Mode::Libstd;
255 );
256
257 #[derive(Serialize)]
258 pub struct RemoteTestServer<'a> {
259     pub stage: u32,
260     pub target: &'a str,
261 }
262
263 impl<'a> Step<'a> for RemoteTestServer<'a> {
264     type Id = RemoteTestServer<'static>;
265     type Output = PathBuf;
266
267     fn should_run(_builder: &Builder, path: &Path) -> bool {
268         path.ends_with("src/tools/remote-test-server")
269     }
270
271     fn make_run(builder: &Builder, _path: Option<&Path>, _host: &str, target: &str) {
272         builder.ensure(RemoteTestServer {
273             stage: builder.top_stage,
274             target,
275         });
276     }
277
278     fn run(self, builder: &Builder) -> PathBuf {
279         builder.ensure(ToolBuild {
280             stage: self.stage,
281             target: self.target,
282             tool: "remote-test-server",
283             mode: Mode::Libstd,
284         })
285     }
286 }
287
288 // rules.build("tool-cargo", "src/tools/cargo")
289 //      .host(true)
290 //      .default(build.config.extended)
291 //      .dep(|s| s.name("maybe-clean-tools"))
292 //      .dep(|s| s.name("libstd-tool"))
293 //      .dep(|s| s.stage(0).host(s.target).name("openssl"))
294 //      .dep(move |s| {
295 //          // Cargo depends on procedural macros, which requires a full host
296 //          // compiler to be available, so we need to depend on that.
297 //          s.name("librustc-link")
298 //           .target(&build.build)
299 //           .host(&build.build)
300 //      })
301 //      .run(move |s| compile::tool(build, s.stage, s.target, "cargo"));
302 #[derive(Serialize)]
303 pub struct Cargo<'a> {
304     pub stage: u32,
305     pub target: &'a str,
306 }
307
308 impl<'a> Step<'a> for Cargo<'a> {
309     type Id = Cargo<'static>;
310     type Output = PathBuf;
311     const DEFAULT: bool = true;
312     const ONLY_HOSTS: bool = true;
313
314     fn should_run(_builder: &Builder, path: &Path) -> bool {
315         path.ends_with("src/tools/cargo")
316     }
317
318     fn make_run(builder: &Builder, path: Option<&Path>, _host: &str, target: &str) {
319         if path.is_none() && !builder.build.config.extended {
320             return;
321         }
322         builder.ensure(Cargo {
323             stage: builder.top_stage,
324             target,
325         });
326     }
327
328     fn run(self, builder: &Builder) -> PathBuf {
329         builder.ensure(native::Openssl {
330             target: self.target,
331         });
332         // Cargo depends on procedural macros, which requires a full host
333         // compiler to be available, so we need to depend on that.
334         builder.ensure(Rustc {
335             compiler: builder.compiler(builder.top_stage, &builder.build.build),
336             target: &builder.build.build,
337         });
338         builder.ensure(ToolBuild {
339             stage: self.stage,
340             target: self.target,
341             tool: "cargo",
342             mode: Mode::Libstd,
343         })
344     }
345 }
346
347 // rules.build("tool-rls", "src/tools/rls")
348 //      .host(true)
349 //      .default(build.config.extended)
350 //      .dep(|s| s.name("librustc-tool"))
351 //      .dep(|s| s.stage(0).host(s.target).name("openssl"))
352 //      .dep(move |s| {
353 //          // rls, like cargo, uses procedural macros
354 //          s.name("librustc-link")
355 //           .target(&build.build)
356 //           .host(&build.build)
357 //      })
358 //      .run(move |s| compile::tool(build, s.stage, s.target, "rls"));
359 //
360 #[derive(Serialize)]
361 pub struct Rls<'a> {
362     pub stage: u32,
363     pub target: &'a str,
364 }
365
366 impl<'a> Step<'a> for Rls<'a> {
367     type Id = Rls<'static>;
368     type Output = PathBuf;
369     const DEFAULT: bool = true;
370     const ONLY_HOSTS: bool = true;
371
372     fn should_run(_builder: &Builder, path: &Path) -> bool {
373         path.ends_with("src/tools/rls")
374     }
375
376     fn make_run(builder: &Builder, path: Option<&Path>, _host: &str, target: &str) {
377         if path.is_none() && !builder.build.config.extended {
378             return;
379         }
380         builder.ensure(Cargo {
381             stage: builder.top_stage,
382             target,
383         });
384     }
385
386     fn run(self, builder: &Builder) -> PathBuf {
387         builder.ensure(native::Openssl {
388             target: self.target,
389         });
390         // RLS depends on procedural macros, which requires a full host
391         // compiler to be available, so we need to depend on that.
392         builder.ensure(Rustc {
393             compiler: builder.compiler(builder.top_stage, &builder.build.build),
394             target: &builder.build.build,
395         });
396         builder.ensure(ToolBuild {
397             stage: self.stage,
398             target: self.target,
399             tool: "rls",
400             mode: Mode::Librustc,
401         })
402     }
403 }
404
405 impl<'a> Builder<'a> {
406     /// Get a `Command` which is ready to run `tool` in `stage` built for
407     /// `host`.
408     pub fn tool_cmd(&self, tool: Tool) -> Command {
409         let mut cmd = Command::new(self.tool_exe(tool));
410         let compiler = self.compiler(0, &self.build.build);
411         self.prepare_tool_cmd(compiler, &mut cmd);
412         cmd
413     }
414
415     /// Prepares the `cmd` provided to be able to run the `compiler` provided.
416     ///
417     /// Notably this munges the dynamic library lookup path to point to the
418     /// right location to run `compiler`.
419     fn prepare_tool_cmd(&self, compiler: Compiler, cmd: &mut Command) {
420         let host = compiler.host;
421         let mut paths = vec![
422             self.sysroot_libdir(compiler, compiler.host),
423             self.cargo_out(compiler, Mode::Tool, host).join("deps"),
424         ];
425
426         // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
427         // mode) and that C compiler may need some extra PATH modification. Do
428         // so here.
429         if compiler.host.contains("msvc") {
430             let curpaths = env::var_os("PATH").unwrap_or_default();
431             let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
432             for &(ref k, ref v) in self.cc[compiler.host].0.env() {
433                 if k != "PATH" {
434                     continue
435                 }
436                 for path in env::split_paths(v) {
437                     if !curpaths.contains(&path) {
438                         paths.push(path);
439                     }
440                 }
441             }
442         }
443         add_lib_path(paths, cmd);
444     }
445 }