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.
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.
13 use std::path::PathBuf;
14 use std::process::Command;
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};
25 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
26 pub struct CleanTools {
27 pub compiler: Compiler,
28 pub target: Interned<String>,
32 impl Step for CleanTools {
35 fn should_run(run: ShouldRun) -> ShouldRun {
39 /// Build a tool in `src/tools`
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;
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),
55 let out_dir = build.cargo_out(compiler, Mode::Tool, target);
56 build.clear_if_dirty(&out_dir, &stamp);
60 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
63 target: Interned<String>,
69 impl Step for ToolBuild {
70 type Output = PathBuf;
72 fn should_run(run: ShouldRun) -> ShouldRun {
76 /// Build a tool in `src/tools`
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;
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")
94 let _folder = build.fold_output(|| format!("stage{}-{}", compiler.stage, tool));
95 println!("Building stage{} tool {} ({})", compiler.stage, tool, target);
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))
103 pub fn prepare_tool_cargo(
106 target: Interned<String>,
107 command: &'static str,
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"));
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");
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");
125 cargo.env("CFG_RELEASE_CHANNEL", &build.config.channel);
126 cargo.env("CFG_VERSION", build.rust_version());
128 let info = GitInfo::new(&build.config, &dir);
129 if let Some(sha) = info.sha() {
130 cargo.env("CFG_COMMIT_HASH", sha);
132 if let Some(sha_short) = info.sha_short() {
133 cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
135 if let Some(date) = info.commit_date() {
136 cargo.env("CFG_COMMIT_DATE", date);
142 ($($name:ident, $path:expr, $tool_name:expr, $mode:expr;)+) => {
143 #[derive(Copy, Clone)]
150 impl<'a> Builder<'a> {
151 pub fn tool_exe(&self, tool: Tool) -> PathBuf {
152 let stage = self.tool_default_stage(tool);
156 compiler: self.compiler(stage, self.build.build),
157 target: self.build.build,
163 pub fn tool_default_stage(&self, tool: Tool) -> u32 {
164 // Compile the error-index in the top stage as it depends on
165 // rustdoc, so we want to avoid recompiling rustdoc twice if we
166 // can. Otherwise compile everything else in stage0 as there's
167 // no need to rebootstrap everything
169 Tool::ErrorIndex => self.top_stage,
176 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
178 pub compiler: Compiler,
179 pub target: Interned<String>,
182 impl Step for $name {
183 type Output = PathBuf;
185 fn should_run(run: ShouldRun) -> ShouldRun {
189 fn make_run(run: RunConfig) {
190 run.builder.ensure($name {
191 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
196 fn run(self, builder: &Builder) -> PathBuf {
197 builder.ensure(ToolBuild {
198 compiler: self.compiler,
211 Rustbook, "src/tools/rustbook", "rustbook", Mode::Librustc;
212 ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::Librustc;
213 UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::Libstd;
214 Tidy, "src/tools/tidy", "tidy", Mode::Libstd;
215 Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::Libstd;
216 CargoTest, "src/tools/cargotest", "cargotest", Mode::Libstd;
217 Compiletest, "src/tools/compiletest", "compiletest", Mode::Libtest;
218 BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Libstd;
219 RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::Libstd;
220 RustInstaller, "src/tools/rust-installer", "fabricate", Mode::Libstd;
223 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
224 pub struct RemoteTestServer {
225 pub compiler: Compiler,
226 pub target: Interned<String>,
229 impl Step for RemoteTestServer {
230 type Output = PathBuf;
232 fn should_run(run: ShouldRun) -> ShouldRun {
233 run.path("src/tools/remote-test-server")
236 fn make_run(run: RunConfig) {
237 run.builder.ensure(RemoteTestServer {
238 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
243 fn run(self, builder: &Builder) -> PathBuf {
244 builder.ensure(ToolBuild {
245 compiler: self.compiler,
247 tool: "remote-test-server",
249 path: "src/tools/remote-test-server",
254 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
256 pub host: Interned<String>,
259 impl Step for Rustdoc {
260 type Output = PathBuf;
261 const DEFAULT: bool = true;
262 const ONLY_HOSTS: bool = true;
264 fn should_run(run: ShouldRun) -> ShouldRun {
265 run.path("src/tools/rustdoc")
268 fn make_run(run: RunConfig) {
269 run.builder.ensure(Rustdoc {
274 fn run(self, builder: &Builder) -> PathBuf {
275 let build = builder.build;
276 let target_compiler = builder.compiler(builder.top_stage, self.host);
277 let target = target_compiler.host;
278 let build_compiler = if target_compiler.stage == 0 {
279 builder.compiler(0, builder.build.build)
280 } else if target_compiler.stage >= 2 {
281 // Past stage 2, we consider the compiler to be ABI-compatible and hence capable of
282 // building rustdoc itself.
283 builder.compiler(target_compiler.stage, builder.build.build)
285 // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
286 // we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
287 // compilers, which isn't what we want.
288 builder.compiler(target_compiler.stage - 1, builder.build.build)
291 builder.ensure(compile::Rustc { compiler: build_compiler, target });
293 let _folder = build.fold_output(|| format!("stage{}-rustdoc", target_compiler.stage));
294 println!("Building rustdoc for stage{} ({})", target_compiler.stage, target_compiler.host);
296 let mut cargo = prepare_tool_cargo(builder,
300 "src/tools/rustdoc");
301 build.run(&mut cargo);
302 // Cargo adds a number of paths to the dylib search path on windows, which results in
303 // the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
304 // rustdoc a different name.
305 let tool_rustdoc = build.cargo_out(build_compiler, Mode::Tool, target)
306 .join(exe("rustdoc-tool-binary", &target_compiler.host));
308 // don't create a stage0-sysroot/bin directory.
309 if target_compiler.stage > 0 {
310 let sysroot = builder.sysroot(target_compiler);
311 let bindir = sysroot.join("bin");
312 t!(fs::create_dir_all(&bindir));
313 let bin_rustdoc = bindir.join(exe("rustdoc", &*target_compiler.host));
314 let _ = fs::remove_file(&bin_rustdoc);
315 copy(&tool_rustdoc, &bin_rustdoc);
323 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
325 pub compiler: Compiler,
326 pub target: Interned<String>,
329 impl Step for Cargo {
330 type Output = PathBuf;
331 const DEFAULT: bool = true;
332 const ONLY_HOSTS: bool = true;
334 fn should_run(run: ShouldRun) -> ShouldRun {
335 let builder = run.builder;
336 run.path("src/tools/cargo").default_condition(builder.build.config.extended)
339 fn make_run(run: RunConfig) {
340 run.builder.ensure(Cargo {
341 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
346 fn run(self, builder: &Builder) -> PathBuf {
347 builder.ensure(native::Openssl {
350 // Cargo depends on procedural macros, which requires a full host
351 // compiler to be available, so we need to depend on that.
352 builder.ensure(compile::Rustc {
353 compiler: self.compiler,
354 target: builder.build.build,
356 builder.ensure(ToolBuild {
357 compiler: self.compiler,
360 mode: Mode::Librustc,
361 path: "src/tools/cargo",
366 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
368 pub compiler: Compiler,
369 pub target: Interned<String>,
372 impl Step for Clippy {
373 type Output = PathBuf;
374 const DEFAULT: bool = false;
375 const ONLY_HOSTS: bool = true;
377 fn should_run(run: ShouldRun) -> ShouldRun {
378 run.path("src/tools/clippy")
381 fn make_run(run: RunConfig) {
382 run.builder.ensure(Clippy {
383 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
388 fn run(self, builder: &Builder) -> PathBuf {
389 // Clippy depends on procedural macros (serde), which requires a full host
390 // compiler to be available, so we need to depend on that.
391 builder.ensure(compile::Rustc {
392 compiler: self.compiler,
393 target: builder.build.build,
395 builder.ensure(ToolBuild {
396 compiler: self.compiler,
399 mode: Mode::Librustc,
400 path: "src/tools/clippy",
405 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
407 pub compiler: Compiler,
408 pub target: Interned<String>,
412 type Output = PathBuf;
413 const DEFAULT: bool = true;
414 const ONLY_HOSTS: bool = true;
416 fn should_run(run: ShouldRun) -> ShouldRun {
417 let builder = run.builder;
418 run.path("src/tools/rls").default_condition(builder.build.config.extended)
421 fn make_run(run: RunConfig) {
422 run.builder.ensure(Rls {
423 compiler: run.builder.compiler(run.builder.top_stage, run.builder.build.build),
428 fn run(self, builder: &Builder) -> PathBuf {
429 builder.ensure(native::Openssl {
432 // RLS depends on procedural macros, which requires a full host
433 // compiler to be available, so we need to depend on that.
434 builder.ensure(compile::Rustc {
435 compiler: self.compiler,
436 target: builder.build.build,
438 builder.ensure(ToolBuild {
439 compiler: self.compiler,
442 mode: Mode::Librustc,
443 path: "src/tools/rls",
448 impl<'a> Builder<'a> {
449 /// Get a `Command` which is ready to run `tool` in `stage` built for
451 pub fn tool_cmd(&self, tool: Tool) -> Command {
452 let mut cmd = Command::new(self.tool_exe(tool));
453 let compiler = self.compiler(self.tool_default_stage(tool), self.build.build);
454 self.prepare_tool_cmd(compiler, &mut cmd);
458 /// Prepares the `cmd` provided to be able to run the `compiler` provided.
460 /// Notably this munges the dynamic library lookup path to point to the
461 /// right location to run `compiler`.
462 fn prepare_tool_cmd(&self, compiler: Compiler, cmd: &mut Command) {
463 let host = &compiler.host;
464 let mut paths: Vec<PathBuf> = vec![
465 PathBuf::from(&self.sysroot_libdir(compiler, compiler.host)),
466 self.cargo_out(compiler, Mode::Tool, *host).join("deps"),
469 // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
470 // mode) and that C compiler may need some extra PATH modification. Do
472 if compiler.host.contains("msvc") {
473 let curpaths = env::var_os("PATH").unwrap_or_default();
474 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
475 for &(ref k, ref v) in self.cc[&compiler.host].0.env() {
479 for path in env::split_paths(v) {
480 if !curpaths.contains(&path) {
486 add_lib_path(paths, cmd);