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