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.
14 use std::path::PathBuf;
15 use std::process::{Command, exit};
19 use builder::{Step, RunConfig, ShouldRun, Builder};
20 use util::{exe, add_lib_path};
21 use compile::{self, libtest_stamp, libstd_stamp, librustc_stamp};
25 use toolstate::ToolState;
27 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
28 pub struct CleanTools {
29 pub compiler: Compiler,
30 pub target: Interned<String>,
34 impl Step for CleanTools {
37 fn should_run(run: ShouldRun) -> ShouldRun {
41 fn run(self, builder: &Builder) {
42 let compiler = self.compiler;
43 let target = self.target;
44 let cause = self.cause;
46 // This is for the original compiler, but if we're forced to use stage 1, then
47 // std/test/rustc stamps won't exist in stage 2, so we need to get those from stage 1, since
48 // we copy the libs forward.
49 let tools_dir = builder.stage_out(compiler, Mode::ToolRustc);
50 let compiler = if builder.force_use_stage1(compiler, target) {
51 builder.compiler(1, compiler.host)
56 for &cur_mode in &[Mode::Std, Mode::Test, Mode::Rustc] {
57 let stamp = match cur_mode {
58 Mode::Std => libstd_stamp(builder, compiler, target),
59 Mode::Test => libtest_stamp(builder, compiler, target),
60 Mode::Rustc => librustc_stamp(builder, compiler, target),
64 if builder.clear_if_dirty(&tools_dir, &stamp) {
68 // If we are a rustc tool, and std changed, we also need to clear ourselves out -- our
69 // dependencies depend on std. Therefore, we iterate up until our own mode.
70 if cause == cur_mode {
77 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
80 target: Interned<String>,
85 extra_features: Vec<String>,
88 impl Step for ToolBuild {
89 type Output = Option<PathBuf>;
91 fn should_run(run: ShouldRun) -> ShouldRun {
95 /// Build a tool in `src/tools`
97 /// This will build the specified tool with the specified `host` compiler in
98 /// `stage` into the normal cargo output directory.
99 fn run(self, builder: &Builder) -> Option<PathBuf> {
100 let compiler = self.compiler;
101 let target = self.target;
102 let tool = self.tool;
103 let path = self.path;
104 let is_ext_tool = self.is_ext_tool;
107 Mode::ToolStd => builder.ensure(compile::Std { compiler, target }),
108 Mode::ToolTest => builder.ensure(compile::Test { compiler, target }),
109 Mode::ToolRustc => builder.ensure(compile::Rustc { compiler, target }),
110 _ => panic!("unexpected Mode for tool build")
113 let mut cargo = prepare_tool_cargo(builder, compiler, self.mode, target, "build", path);
114 cargo.arg("--features").arg(self.extra_features.join(" "));
116 let _folder = builder.fold_output(|| format!("stage{}-{}", compiler.stage, tool));
117 builder.info(&format!("Building stage{} tool {} ({})", compiler.stage, tool, target));
118 let mut duplicates = Vec::new();
119 let is_expected = compile::stream_cargo(builder, &mut cargo, &mut |msg| {
120 // Only care about big things like the RLS/Cargo for now
121 if tool != "rls" && tool != "cargo" {
124 let (id, features, filenames) = match msg {
125 compile::CargoMessage::CompilerArtifact {
130 (package_id, features, filenames)
134 let features = features.iter().map(|s| s.to_string()).collect::<Vec<_>>();
136 for path in filenames {
137 let val = (tool, PathBuf::from(&*path), features.clone());
138 // we're only interested in deduplicating rlibs for now
139 if val.1.extension().and_then(|s| s.to_str()) != Some("rlib") {
143 // Don't worry about libs that turn out to be host dependencies
144 // or build scripts, we only care about target dependencies that
146 if let Some(maybe_target) = val.1
147 .parent() // chop off file name
148 .and_then(|p| p.parent()) // chop off `deps`
149 .and_then(|p| p.parent()) // chop off `release`
150 .and_then(|p| p.file_name())
151 .and_then(|p| p.to_str())
153 if maybe_target != &*target {
158 let mut artifacts = builder.tool_artifacts.borrow_mut();
159 let prev_artifacts = artifacts
161 .or_insert_with(Default::default);
162 if let Some(prev) = prev_artifacts.get(&*id) {
172 prev_artifacts.insert(id.to_string(), val);
176 if is_expected && duplicates.len() != 0 {
177 println!("duplicate artfacts found when compiling a tool, this \
178 typically means that something was recompiled because \
179 a transitive dependency has different features activated \
180 than in a previous build:\n");
181 for (id, cur, prev) in duplicates {
183 println!(" `{}` enabled features {:?} at {:?}",
184 cur.0, cur.2, cur.1);
185 println!(" `{}` enabled features {:?} at {:?}",
186 prev.0, prev.2, prev.1);
189 panic!("tools should not compile multiple copies of the same crate");
192 builder.save_toolstate(tool, if is_expected {
205 let cargo_out = builder.cargo_out(compiler, self.mode, target)
206 .join(exe(tool, &compiler.host));
207 let bin = builder.tools_dir(compiler).join(exe(tool, &compiler.host));
208 builder.copy(&cargo_out, &bin);
214 pub fn prepare_tool_cargo(
218 target: Interned<String>,
219 command: &'static str,
222 let mut cargo = builder.cargo(compiler, mode, target, command);
223 let dir = builder.src.join(path);
224 cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
226 // We don't want to build tools dynamically as they'll be running across
227 // stages and such and it's just easier if they're not dynamically linked.
228 cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
230 if let Some(dir) = builder.openssl_install_dir(target) {
231 cargo.env("OPENSSL_STATIC", "1");
232 cargo.env("OPENSSL_DIR", dir);
233 cargo.env("LIBZ_SYS_STATIC", "1");
236 // if tools are using lzma we want to force the build script to build its
238 cargo.env("LZMA_API_STATIC", "1");
240 cargo.env("CFG_RELEASE_CHANNEL", &builder.config.channel);
241 cargo.env("CFG_VERSION", builder.rust_version());
243 let info = GitInfo::new(&builder.config, &dir);
244 if let Some(sha) = info.sha() {
245 cargo.env("CFG_COMMIT_HASH", sha);
247 if let Some(sha_short) = info.sha_short() {
248 cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
250 if let Some(date) = info.commit_date() {
251 cargo.env("CFG_COMMIT_DATE", date);
257 ($($name:ident, $path:expr, $tool_name:expr, $mode:expr $(,llvm_tools = $llvm:expr)*;)+) => {
258 #[derive(Copy, Clone)]
266 pub fn get_mode(&self) -> Mode {
267 let mode = match self {
268 $(Tool::$name => $mode,)+
273 /// Whether this tool requires LLVM to run
274 pub fn uses_llvm_tools(&self) -> bool {
276 $(Tool::$name => true $(&& $llvm)*,)+
281 impl<'a> Builder<'a> {
282 pub fn tool_exe(&self, tool: Tool) -> PathBuf {
283 let stage = self.tool_default_stage(tool);
287 compiler: self.compiler(stage, self.config.build),
288 target: self.config.build,
294 pub fn tool_default_stage(&self, tool: Tool) -> u32 {
295 // Compile the error-index in the same stage as rustdoc to avoid
296 // recompiling rustdoc twice if we can. Otherwise compile
297 // everything else in stage0 as there's no need to rebootstrap
300 Tool::ErrorIndex if self.top_stage >= 2 => self.top_stage,
307 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
309 pub compiler: Compiler,
310 pub target: Interned<String>,
313 impl Step for $name {
314 type Output = PathBuf;
316 fn should_run(run: ShouldRun) -> ShouldRun {
320 fn make_run(run: RunConfig) {
321 run.builder.ensure($name {
322 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
327 fn run(self, builder: &Builder) -> PathBuf {
328 builder.ensure(ToolBuild {
329 compiler: self.compiler,
335 extra_features: Vec::new(),
336 }).expect("expected to build -- essential tool")
343 // FIXME(#51459): We have only checked that RustInstaller does not require
344 // the LLVM binaries when running. We should go through all tools to determine
345 // if they really need LLVM binaries, and make `llvm_tools` a required argument.
347 Rustbook, "src/tools/rustbook", "rustbook", Mode::ToolRustc;
348 ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::ToolRustc;
349 UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::ToolStd;
350 Tidy, "src/tools/tidy", "tidy", Mode::ToolStd;
351 Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::ToolStd;
352 CargoTest, "src/tools/cargotest", "cargotest", Mode::ToolStd;
353 Compiletest, "src/tools/compiletest", "compiletest", Mode::ToolTest;
354 BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::ToolStd;
355 RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::ToolStd;
356 RustInstaller, "src/tools/rust-installer", "fabricate", Mode::ToolStd, llvm_tools = false;
357 RustdocTheme, "src/tools/rustdoc-themes", "rustdoc-themes", Mode::ToolStd;
360 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
361 pub struct RemoteTestServer {
362 pub compiler: Compiler,
363 pub target: Interned<String>,
366 impl Step for RemoteTestServer {
367 type Output = PathBuf;
369 fn should_run(run: ShouldRun) -> ShouldRun {
370 run.path("src/tools/remote-test-server")
373 fn make_run(run: RunConfig) {
374 run.builder.ensure(RemoteTestServer {
375 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
380 fn run(self, builder: &Builder) -> PathBuf {
381 builder.ensure(ToolBuild {
382 compiler: self.compiler,
384 tool: "remote-test-server",
386 path: "src/tools/remote-test-server",
388 extra_features: Vec::new(),
389 }).expect("expected to build -- essential tool")
393 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
395 pub host: Interned<String>,
398 impl Step for Rustdoc {
399 type Output = PathBuf;
400 const DEFAULT: bool = true;
401 const ONLY_HOSTS: bool = true;
403 fn should_run(run: ShouldRun) -> ShouldRun {
404 run.path("src/tools/rustdoc")
407 fn make_run(run: RunConfig) {
408 run.builder.ensure(Rustdoc {
413 fn run(self, builder: &Builder) -> PathBuf {
414 let target_compiler = builder.compiler(builder.top_stage, self.host);
415 let target = target_compiler.host;
416 let build_compiler = if target_compiler.stage == 0 {
417 builder.compiler(0, builder.config.build)
418 } else if target_compiler.stage >= 2 {
419 // Past stage 2, we consider the compiler to be ABI-compatible and hence capable of
420 // building rustdoc itself.
421 builder.compiler(target_compiler.stage, builder.config.build)
423 // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
424 // we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
425 // compilers, which isn't what we want.
426 builder.compiler(target_compiler.stage - 1, builder.config.build)
429 builder.ensure(compile::Rustc { compiler: build_compiler, target });
430 builder.ensure(compile::Rustc {
431 compiler: build_compiler,
432 target: builder.config.build,
435 let mut cargo = prepare_tool_cargo(builder,
440 "src/tools/rustdoc");
442 // Most tools don't get debuginfo, but rustdoc should.
443 cargo.env("RUSTC_DEBUGINFO", builder.config.rust_debuginfo.to_string())
444 .env("RUSTC_DEBUGINFO_LINES", builder.config.rust_debuginfo_lines.to_string());
446 let _folder = builder.fold_output(|| format!("stage{}-rustdoc", target_compiler.stage));
447 builder.info(&format!("Building rustdoc for stage{} ({})",
448 target_compiler.stage, target_compiler.host));
449 builder.run(&mut cargo);
451 // Cargo adds a number of paths to the dylib search path on windows, which results in
452 // the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
453 // rustdoc a different name.
454 let tool_rustdoc = builder.cargo_out(build_compiler, Mode::ToolRustc, target)
455 .join(exe("rustdoc_tool_binary", &target_compiler.host));
457 // don't create a stage0-sysroot/bin directory.
458 if target_compiler.stage > 0 {
459 let sysroot = builder.sysroot(target_compiler);
460 let bindir = sysroot.join("bin");
461 t!(fs::create_dir_all(&bindir));
462 let bin_rustdoc = bindir.join(exe("rustdoc", &*target_compiler.host));
463 let _ = fs::remove_file(&bin_rustdoc);
464 builder.copy(&tool_rustdoc, &bin_rustdoc);
472 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
474 pub compiler: Compiler,
475 pub target: Interned<String>,
478 impl Step for Cargo {
479 type Output = PathBuf;
480 const DEFAULT: bool = true;
481 const ONLY_HOSTS: bool = true;
483 fn should_run(run: ShouldRun) -> ShouldRun {
484 let builder = run.builder;
485 run.path("src/tools/cargo").default_condition(builder.config.extended)
488 fn make_run(run: RunConfig) {
489 run.builder.ensure(Cargo {
490 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
495 fn run(self, builder: &Builder) -> PathBuf {
496 builder.ensure(native::Openssl {
499 // Cargo depends on procedural macros, which requires a full host
500 // compiler to be available, so we need to depend on that.
501 builder.ensure(compile::Rustc {
502 compiler: self.compiler,
503 target: builder.config.build,
505 builder.ensure(ToolBuild {
506 compiler: self.compiler,
509 mode: Mode::ToolRustc,
510 path: "src/tools/cargo",
512 extra_features: Vec::new(),
513 }).expect("expected to build -- essential tool")
517 macro_rules! tool_extended {
518 (($sel:ident, $builder:ident),
523 $extra_deps:block;)+) => {
525 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
527 pub compiler: Compiler,
528 pub target: Interned<String>,
529 pub extra_features: Vec<String>,
532 impl Step for $name {
533 type Output = Option<PathBuf>;
534 const DEFAULT: bool = true;
535 const ONLY_HOSTS: bool = true;
537 fn should_run(run: ShouldRun) -> ShouldRun {
538 let builder = run.builder;
539 run.path($path).default_condition(builder.config.extended)
542 fn make_run(run: RunConfig) {
543 run.builder.ensure($name {
544 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
546 extra_features: Vec::new(),
551 fn run(mut $sel, $builder: &Builder) -> Option<PathBuf> {
553 $builder.ensure(ToolBuild {
554 compiler: $sel.compiler,
557 mode: Mode::ToolRustc,
559 extra_features: $sel.extra_features,
568 tool_extended!((self, builder),
569 Cargofmt, rustfmt, "src/tools/rustfmt", "cargo-fmt", {};
570 Clippy, clippy, "src/tools/clippy", "clippy-driver", {
571 // Clippy depends on procedural macros (serde), which requires a full host
572 // compiler to be available, so we need to depend on that.
573 builder.ensure(compile::Rustc {
574 compiler: self.compiler,
575 target: builder.config.build,
578 Miri, miri, "src/tools/miri", "miri", {};
579 Rls, rls, "src/tools/rls", "rls", {
580 builder.ensure(native::Openssl {
583 // RLS depends on procedural macros, which requires a full host
584 // compiler to be available, so we need to depend on that.
585 builder.ensure(compile::Rustc {
586 compiler: self.compiler,
587 target: builder.config.build,
590 Rustfmt, rustfmt, "src/tools/rustfmt", "rustfmt", {};
593 impl<'a> Builder<'a> {
594 /// Get a `Command` which is ready to run `tool` in `stage` built for
596 pub fn tool_cmd(&self, tool: Tool) -> Command {
597 let mut cmd = Command::new(self.tool_exe(tool));
598 let compiler = self.compiler(self.tool_default_stage(tool), self.config.build);
599 self.prepare_tool_cmd(compiler, tool, &mut cmd);
603 /// Prepares the `cmd` provided to be able to run the `compiler` provided.
605 /// Notably this munges the dynamic library lookup path to point to the
606 /// right location to run `compiler`.
607 fn prepare_tool_cmd(&self, compiler: Compiler, tool: Tool, cmd: &mut Command) {
608 let host = &compiler.host;
609 let mut lib_paths: Vec<PathBuf> = vec![
610 PathBuf::from(&self.sysroot_libdir(compiler, compiler.host)),
611 self.cargo_out(compiler, tool.get_mode(), *host).join("deps"),
614 // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
615 // mode) and that C compiler may need some extra PATH modification. Do
617 if compiler.host.contains("msvc") {
618 let curpaths = env::var_os("PATH").unwrap_or_default();
619 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
620 for &(ref k, ref v) in self.cc[&compiler.host].env() {
624 for path in env::split_paths(v) {
625 if !curpaths.contains(&path) {
626 lib_paths.push(path);
632 // Add the llvm/bin directory to PATH since it contains lots of
633 // useful, platform-independent tools
634 if tool.uses_llvm_tools() {
635 if let Some(llvm_bin_path) = self.llvm_bin_path() {
636 if host.contains("windows") {
637 // On Windows, PATH and the dynamic library path are the same,
638 // so we just add the LLVM bin path to lib_path
639 lib_paths.push(llvm_bin_path);
641 let old_path = env::var_os("PATH").unwrap_or_default();
642 let new_path = env::join_paths(iter::once(llvm_bin_path)
643 .chain(env::split_paths(&old_path)))
644 .expect("Could not add LLVM bin path to PATH");
645 cmd.env("PATH", new_path);
650 add_lib_path(lib_paths, cmd);
653 fn llvm_bin_path(&self) -> Option<PathBuf> {
654 if self.config.llvm_enabled && !self.config.dry_run {
655 let llvm_config = self.ensure(native::Llvm {
656 target: self.config.build,
660 // Add the llvm/bin directory to PATH since it contains lots of
661 // useful, platform-independent tools
662 let llvm_bin_path = llvm_config.parent()
663 .expect("Expected llvm-config to be contained in directory");
664 assert!(llvm_bin_path.is_dir());
665 Some(llvm_bin_path.to_path_buf())