1 use std::collections::HashSet;
4 use std::path::PathBuf;
5 use std::process::Command;
7 use crate::builder::{Builder, Cargo as CargoCommand, RunConfig, ShouldRun, Step};
8 use crate::channel::GitInfo;
10 use crate::config::TargetSelection;
11 use crate::toolstate::ToolState;
12 use crate::util::{add_dylib_path, exe, t};
16 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
22 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
25 target: TargetSelection,
29 is_optional_tool: bool,
30 source_type: SourceType,
31 extra_features: Vec<String>,
34 impl Step for ToolBuild {
35 type Output = Option<PathBuf>;
37 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
41 /// Builds a tool in `src/tools`
43 /// This will build the specified tool with the specified `host` compiler in
44 /// `stage` into the normal cargo output directory.
45 fn run(self, builder: &Builder<'_>) -> Option<PathBuf> {
46 let compiler = self.compiler;
47 let target = self.target;
48 let mut tool = self.tool;
50 let is_optional_tool = self.is_optional_tool;
54 builder.ensure(compile::Std::new(compiler, compiler.host));
55 builder.ensure(compile::Rustc::new(compiler, target));
57 Mode::ToolStd => builder.ensure(compile::Std::new(compiler, target)),
58 Mode::ToolBootstrap => {} // uses downloaded stage0 compiler libs
59 _ => panic!("unexpected Mode for tool build"),
62 let cargo = prepare_tool_cargo(
73 builder.info(&format!("Building stage{} tool {} ({})", compiler.stage, tool, target));
74 let mut duplicates = Vec::new();
75 let is_expected = compile::stream_cargo(builder, cargo, &mut |msg| {
76 // Only care about big things like the RLS/Cargo for now
78 "rls" | "cargo" | "clippy-driver" | "miri" | "rustfmt" => {}
82 let (id, features, filenames) = match msg {
83 compile::CargoMessage::CompilerArtifact {
88 } => (package_id, features, filenames),
91 let features = features.iter().map(|s| s.to_string()).collect::<Vec<_>>();
93 for path in filenames {
94 let val = (tool, PathBuf::from(&*path), features.clone());
95 // we're only interested in deduplicating rlibs for now
96 if val.1.extension().and_then(|s| s.to_str()) != Some("rlib") {
100 // Don't worry about compiles that turn out to be host
101 // dependencies or build scripts. To skip these we look for
102 // anything that goes in `.../release/deps` but *doesn't* go in
103 // `$target/release/deps`. This ensure that outputs in
104 // `$target/release` are still considered candidates for
106 if let Some(parent) = val.1.parent() {
107 if parent.ends_with("release/deps") {
108 let maybe_target = parent
110 .and_then(|p| p.parent())
111 .and_then(|p| p.file_name())
112 .and_then(|p| p.to_str())
114 if maybe_target != &*target.triple {
120 // Record that we've built an artifact for `id`, and if one was
121 // already listed then we need to see if we reused the same
122 // artifact or produced a duplicate.
123 let mut artifacts = builder.tool_artifacts.borrow_mut();
124 let prev_artifacts = artifacts.entry(target).or_default();
125 let prev = match prev_artifacts.get(&*id) {
128 prev_artifacts.insert(id.to_string(), val);
133 return; // same path, same artifact
136 // If the paths are different and one of them *isn't* inside of
137 // `release/deps`, then it means it's probably in
138 // `$target/release`, or it's some final artifact like
139 // `libcargo.rlib`. In these situations Cargo probably just
140 // copied it up from `$target/release/deps/libcargo-xxxx.rlib`,
141 // so if the features are equal we can just skip it.
142 let prev_no_hash = prev.1.parent().unwrap().ends_with("release/deps");
143 let val_no_hash = val.1.parent().unwrap().ends_with("release/deps");
144 if prev.2 == val.2 || !prev_no_hash || !val_no_hash {
148 // ... and otherwise this looks like we duplicated some sort of
149 // compilation, so record it to generate an error later.
150 duplicates.push((id.to_string(), val, prev.clone()));
154 if is_expected && !duplicates.is_empty() {
156 "duplicate artifacts found when compiling a tool, this \
157 typically means that something was recompiled because \
158 a transitive dependency has different features activated \
159 than in a previous build:\n"
161 let (same, different): (Vec<_>, Vec<_>) =
162 duplicates.into_iter().partition(|(_, cur, prev)| cur.2 == prev.2);
163 if !same.is_empty() {
165 "the following dependencies are duplicated although they \
166 have the same features enabled:"
168 for (id, cur, prev) in same {
169 eprintln!(" {}", id);
171 eprintln!(" `{}` ({:?})\n `{}` ({:?})", cur.0, cur.1, prev.0, prev.1);
174 if !different.is_empty() {
175 eprintln!("the following dependencies have different features:");
176 for (id, cur, prev) in different {
177 eprintln!(" {}", id);
178 let cur_features: HashSet<_> = cur.2.into_iter().collect();
179 let prev_features: HashSet<_> = prev.2.into_iter().collect();
181 " `{}` additionally enabled features {:?} at {:?}",
183 &cur_features - &prev_features,
187 " `{}` additionally enabled features {:?} at {:?}",
189 &prev_features - &cur_features,
196 "to fix this you will probably want to edit the local \
197 src/tools/rustc-workspace-hack/Cargo.toml crate, as \
198 that will update the dependency graph to ensure that \
199 these crates all share the same feature set"
201 panic!("tools should not compile multiple copies of the same crate");
204 builder.save_toolstate(
206 if is_expected { ToolState::TestFail } else { ToolState::BuildFail },
210 if !is_optional_tool {
211 crate::detail_exit(1);
216 // HACK(#82501): on Windows, the tools directory gets added to PATH when running tests, and
217 // compiletest confuses HTML tidy with the in-tree tidy. Name the in-tree tidy something
218 // different so the problem doesn't come up.
222 let cargo_out = builder.cargo_out(compiler, self.mode, target).join(exe(tool, target));
223 let bin = builder.tools_dir(compiler).join(exe(tool, target));
224 builder.copy(&cargo_out, &bin);
230 pub fn prepare_tool_cargo(
231 builder: &Builder<'_>,
234 target: TargetSelection,
235 command: &'static str,
237 source_type: SourceType,
238 extra_features: &[String],
240 let mut cargo = builder.cargo(compiler, mode, source_type, target, command);
241 let dir = builder.src.join(path);
242 cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
244 let mut features = extra_features.to_vec();
245 if builder.build.config.cargo_native_static {
246 if path.ends_with("cargo")
247 || path.ends_with("rls")
248 || path.ends_with("clippy")
249 || path.ends_with("miri")
250 || path.ends_with("rustfmt")
252 cargo.env("LIBZ_SYS_STATIC", "1");
253 features.push("rustc-workspace-hack/all-static".to_string());
257 // clippy tests need to know about the stage sysroot. Set them consistently while building to
258 // avoid rebuilding when running tests.
259 cargo.env("SYSROOT", builder.sysroot(compiler));
261 // if tools are using lzma we want to force the build script to build its
263 cargo.env("LZMA_API_STATIC", "1");
265 // CFG_RELEASE is needed by rustfmt (and possibly other tools) which
266 // import rustc-ap-rustc_attr which requires this to be set for the
267 // `#[cfg(version(...))]` attribute.
268 cargo.env("CFG_RELEASE", builder.rust_release());
269 cargo.env("CFG_RELEASE_CHANNEL", &builder.config.channel);
270 cargo.env("CFG_VERSION", builder.rust_version());
271 cargo.env("CFG_RELEASE_NUM", &builder.version);
272 cargo.env("DOC_RUST_LANG_ORG_CHANNEL", builder.doc_rust_lang_org_channel());
274 let info = GitInfo::new(builder.config.ignore_git, &dir);
275 if let Some(sha) = info.sha() {
276 cargo.env("CFG_COMMIT_HASH", sha);
278 if let Some(sha_short) = info.sha_short() {
279 cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
281 if let Some(date) = info.commit_date() {
282 cargo.env("CFG_COMMIT_DATE", date);
284 if !features.is_empty() {
285 cargo.arg("--features").arg(&features.join(", "));
290 macro_rules! bootstrap_tool {
292 $name:ident, $path:expr, $tool_name:expr
293 $(,is_external_tool = $external:expr)*
294 $(,is_unstable_tool = $unstable:expr)*
297 #[derive(Copy, PartialEq, Eq, Clone)]
304 impl<'a> Builder<'a> {
305 pub fn tool_exe(&self, tool: Tool) -> PathBuf {
309 compiler: self.compiler(0, self.config.build),
310 target: self.config.build,
318 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
320 pub compiler: Compiler,
321 pub target: TargetSelection,
324 impl Step for $name {
325 type Output = PathBuf;
327 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
331 fn make_run(run: RunConfig<'_>) {
332 run.builder.ensure($name {
334 compiler: run.builder.compiler(0, run.builder.config.build),
339 fn run(self, builder: &Builder<'_>) -> PathBuf {
340 builder.ensure(ToolBuild {
341 compiler: self.compiler,
344 mode: if false $(|| $unstable)* {
345 // use in-tree libraries for unstable features
351 is_optional_tool: false,
352 source_type: if false $(|| $external)* {
353 SourceType::Submodule
357 extra_features: vec![],
358 }).expect("expected to build -- essential tool")
366 Rustbook, "src/tools/rustbook", "rustbook";
367 UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen";
368 Tidy, "src/tools/tidy", "tidy";
369 Linkchecker, "src/tools/linkchecker", "linkchecker";
370 CargoTest, "src/tools/cargotest", "cargotest";
371 Compiletest, "src/tools/compiletest", "compiletest", is_unstable_tool = true;
372 BuildManifest, "src/tools/build-manifest", "build-manifest";
373 RemoteTestClient, "src/tools/remote-test-client", "remote-test-client";
374 RustInstaller, "src/tools/rust-installer", "rust-installer", is_external_tool = true;
375 RustdocTheme, "src/tools/rustdoc-themes", "rustdoc-themes";
376 ExpandYamlAnchors, "src/tools/expand-yaml-anchors", "expand-yaml-anchors";
377 LintDocs, "src/tools/lint-docs", "lint-docs";
378 JsonDocCk, "src/tools/jsondocck", "jsondocck";
379 JsonDocLint, "src/tools/jsondoclint", "jsondoclint";
380 HtmlChecker, "src/tools/html-checker", "html-checker";
381 BumpStage0, "src/tools/bump-stage0", "bump-stage0";
382 ReplaceVersionPlaceholder, "src/tools/replace-version-placeholder", "replace-version-placeholder";
383 CollectLicenseMetadata, "src/tools/collect-license-metadata", "collect-license-metadata";
384 GenerateCopyright, "src/tools/generate-copyright", "generate-copyright";
387 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
388 pub struct ErrorIndex {
389 pub compiler: Compiler,
393 pub fn command(builder: &Builder<'_>) -> Command {
394 // Error-index-generator links with the rustdoc library, so we need to add `rustc_lib_paths`
395 // for rustc_private and libLLVM.so, and `sysroot_lib` for libstd, etc.
396 let host = builder.config.build;
397 let compiler = builder.compiler_for(builder.top_stage, host, host);
398 let mut cmd = Command::new(builder.ensure(ErrorIndex { compiler }));
399 let mut dylib_paths = builder.rustc_lib_paths(compiler);
400 dylib_paths.push(PathBuf::from(&builder.sysroot_libdir(compiler, compiler.host)));
401 add_dylib_path(dylib_paths, &mut cmd);
406 impl Step for ErrorIndex {
407 type Output = PathBuf;
409 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
410 run.path("src/tools/error_index_generator")
413 fn make_run(run: RunConfig<'_>) {
414 // Compile the error-index in the same stage as rustdoc to avoid
415 // recompiling rustdoc twice if we can.
417 // NOTE: This `make_run` isn't used in normal situations, only if you
418 // manually build the tool with `x.py build
419 // src/tools/error-index-generator` which almost nobody does.
420 // Normally, `x.py test` or `x.py doc` will use the
421 // `ErrorIndex::command` function instead.
423 run.builder.compiler(run.builder.top_stage.saturating_sub(1), run.builder.config.build);
424 run.builder.ensure(ErrorIndex { compiler });
427 fn run(self, builder: &Builder<'_>) -> PathBuf {
430 compiler: self.compiler,
431 target: self.compiler.host,
432 tool: "error_index_generator",
433 mode: Mode::ToolRustc,
434 path: "src/tools/error_index_generator",
435 is_optional_tool: false,
436 source_type: SourceType::InTree,
437 extra_features: Vec::new(),
439 .expect("expected to build -- essential tool")
443 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
444 pub struct RemoteTestServer {
445 pub compiler: Compiler,
446 pub target: TargetSelection,
449 impl Step for RemoteTestServer {
450 type Output = PathBuf;
452 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
453 run.path("src/tools/remote-test-server")
456 fn make_run(run: RunConfig<'_>) {
457 run.builder.ensure(RemoteTestServer {
458 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
463 fn run(self, builder: &Builder<'_>) -> PathBuf {
466 compiler: self.compiler,
468 tool: "remote-test-server",
470 path: "src/tools/remote-test-server",
471 is_optional_tool: false,
472 source_type: SourceType::InTree,
473 extra_features: Vec::new(),
475 .expect("expected to build -- essential tool")
479 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
481 /// This should only ever be 0 or 2.
482 /// We sometimes want to reference the "bootstrap" rustdoc, which is why this option is here.
483 pub compiler: Compiler,
486 impl Step for Rustdoc {
487 type Output = PathBuf;
488 const DEFAULT: bool = true;
489 const ONLY_HOSTS: bool = true;
491 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
492 run.path("src/tools/rustdoc").path("src/librustdoc")
495 fn make_run(run: RunConfig<'_>) {
496 run.builder.ensure(Rustdoc {
497 // Note: this is somewhat unique in that we actually want a *target*
498 // compiler here, because rustdoc *is* a compiler. We won't be using
499 // this as the compiler to build with, but rather this is "what
500 // compiler are we producing"?
501 compiler: run.builder.compiler(run.builder.top_stage, run.target),
505 fn run(self, builder: &Builder<'_>) -> PathBuf {
506 let target_compiler = self.compiler;
507 if target_compiler.stage == 0 {
508 if !target_compiler.is_snapshot(builder) {
509 panic!("rustdoc in stage 0 must be snapshot rustdoc");
511 return builder.initial_rustc.with_file_name(exe("rustdoc", target_compiler.host));
513 let target = target_compiler.host;
514 // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
515 // we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
516 // compilers, which isn't what we want. Rustdoc should be linked in the same way as the
517 // rustc compiler it's paired with, so it must be built with the previous stage compiler.
518 let build_compiler = builder.compiler(target_compiler.stage - 1, builder.config.build);
520 // When using `download-rustc` and a stage0 build_compiler, copying rustc doesn't actually
521 // build stage0 libstd (because the libstd in sysroot has the wrong ABI). Explicitly build
523 builder.ensure(compile::Std::new(build_compiler, target_compiler.host));
524 builder.ensure(compile::Rustc::new(build_compiler, target_compiler.host));
525 // NOTE: this implies that `download-rustc` is pretty useless when compiling with the stage0
526 // compiler, since you do just as much work.
527 if !builder.config.dry_run() && builder.download_rustc() && build_compiler.stage == 0 {
529 "warning: `download-rustc` does nothing when building stage1 tools; consider using `--stage 2` instead"
533 // The presence of `target_compiler` ensures that the necessary libraries (codegen backends,
534 // compiler libraries, ...) are built. Rustdoc does not require the presence of any
535 // libraries within sysroot_libdir (i.e., rustlib), though doctests may want it (since
536 // they'll be linked to those libraries). As such, don't explicitly `ensure` any additional
537 // libraries here. The intuition here is that If we've built a compiler, we should be able
540 let mut features = Vec::new();
541 if builder.config.jemalloc {
542 features.push("jemalloc".to_string());
545 let cargo = prepare_tool_cargo(
556 builder.info(&format!(
557 "Building rustdoc for stage{} ({})",
558 target_compiler.stage, target_compiler.host
560 builder.run(&mut cargo.into());
562 // Cargo adds a number of paths to the dylib search path on windows, which results in
563 // the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
564 // rustdoc a different name.
565 let tool_rustdoc = builder
566 .cargo_out(build_compiler, Mode::ToolRustc, target)
567 .join(exe("rustdoc_tool_binary", target_compiler.host));
569 // don't create a stage0-sysroot/bin directory.
570 if target_compiler.stage > 0 {
571 let sysroot = builder.sysroot(target_compiler);
572 let bindir = sysroot.join("bin");
573 t!(fs::create_dir_all(&bindir));
574 let bin_rustdoc = bindir.join(exe("rustdoc", target_compiler.host));
575 let _ = fs::remove_file(&bin_rustdoc);
576 builder.copy(&tool_rustdoc, &bin_rustdoc);
584 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
586 pub compiler: Compiler,
587 pub target: TargetSelection,
590 impl Step for Cargo {
591 type Output = PathBuf;
592 const DEFAULT: bool = true;
593 const ONLY_HOSTS: bool = true;
595 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
596 let builder = run.builder;
597 run.path("src/tools/cargo").default_condition(
598 builder.config.extended
599 && builder.config.tools.as_ref().map_or(
601 // If `tools` is set, search list for this tool.
602 |tools| tools.iter().any(|tool| tool == "cargo"),
607 fn make_run(run: RunConfig<'_>) {
608 run.builder.ensure(Cargo {
609 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
614 fn run(self, builder: &Builder<'_>) -> PathBuf {
615 let cargo_bin_path = builder
617 compiler: self.compiler,
620 mode: Mode::ToolRustc,
621 path: "src/tools/cargo",
622 is_optional_tool: false,
623 source_type: SourceType::Submodule,
624 extra_features: Vec::new(),
626 .expect("expected to build -- essential tool");
628 let build_cred = |name, path| {
629 // These credential helpers are currently experimental.
630 // Any build failures will be ignored.
631 let _ = builder.ensure(ToolBuild {
632 compiler: self.compiler,
635 mode: Mode::ToolRustc,
637 is_optional_tool: true,
638 source_type: SourceType::Submodule,
639 extra_features: Vec::new(),
643 if self.target.contains("windows") {
645 "cargo-credential-wincred",
646 "src/tools/cargo/crates/credential/cargo-credential-wincred",
649 if self.target.contains("apple-darwin") {
651 "cargo-credential-macos-keychain",
652 "src/tools/cargo/crates/credential/cargo-credential-macos-keychain",
656 "cargo-credential-1password",
657 "src/tools/cargo/crates/credential/cargo-credential-1password",
663 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
664 pub struct LldWrapper {
665 pub compiler: Compiler,
666 pub target: TargetSelection,
669 impl Step for LldWrapper {
670 type Output = PathBuf;
672 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
676 fn run(self, builder: &Builder<'_>) -> PathBuf {
677 let src_exe = builder
679 compiler: self.compiler,
683 path: "src/tools/lld-wrapper",
684 is_optional_tool: false,
685 source_type: SourceType::InTree,
686 extra_features: Vec::new(),
688 .expect("expected to build -- essential tool");
694 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
695 pub struct RustAnalyzer {
696 pub compiler: Compiler,
697 pub target: TargetSelection,
700 impl Step for RustAnalyzer {
701 type Output = Option<PathBuf>;
702 const DEFAULT: bool = true;
703 const ONLY_HOSTS: bool = true;
705 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
706 let builder = run.builder;
707 run.path("src/tools/rust-analyzer").default_condition(
708 builder.config.extended
713 .map_or(true, |tools| tools.iter().any(|tool| tool == "rust-analyzer")),
717 fn make_run(run: RunConfig<'_>) {
718 run.builder.ensure(RustAnalyzer {
719 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
724 fn run(self, builder: &Builder<'_>) -> Option<PathBuf> {
725 builder.ensure(ToolBuild {
726 compiler: self.compiler,
728 tool: "rust-analyzer",
730 path: "src/tools/rust-analyzer",
731 extra_features: vec!["rust-analyzer/in-rust-tree".to_owned()],
732 is_optional_tool: false,
733 source_type: SourceType::InTree,
738 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
739 pub struct RustAnalyzerProcMacroSrv {
740 pub compiler: Compiler,
741 pub target: TargetSelection,
744 impl Step for RustAnalyzerProcMacroSrv {
745 type Output = Option<PathBuf>;
746 const DEFAULT: bool = true;
747 const ONLY_HOSTS: bool = true;
749 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
750 // Allow building `rust-analyzer-proc-macro-srv` both as part of the `rust-analyzer` and as a stand-alone tool.
751 run.path("src/tools/rust-analyzer")
752 .path("src/tools/rust-analyzer/crates/proc-macro-srv-cli")
755 fn make_run(run: RunConfig<'_>) {
756 run.builder.ensure(RustAnalyzerProcMacroSrv {
757 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
762 fn run(self, builder: &Builder<'_>) -> Option<PathBuf> {
763 let path = builder.ensure(ToolBuild {
764 compiler: self.compiler,
766 tool: "rust-analyzer-proc-macro-srv",
768 path: "src/tools/rust-analyzer/crates/proc-macro-srv-cli",
769 extra_features: vec!["proc-macro-srv/sysroot-abi".to_owned()],
770 is_optional_tool: false,
771 source_type: SourceType::InTree,
774 // Copy `rust-analyzer-proc-macro-srv` to `<sysroot>/libexec/`
775 // so that r-a can use it.
776 let libexec_path = builder.sysroot(self.compiler).join("libexec");
777 t!(fs::create_dir_all(&libexec_path));
778 builder.copy(&path, &libexec_path.join("rust-analyzer-proc-macro-srv"));
784 macro_rules! tool_extended {
785 (($sel:ident, $builder:ident),
789 stable = $stable:expr
790 $(,tool_std = $tool_std:literal)?
793 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
795 pub compiler: Compiler,
796 pub target: TargetSelection,
797 pub extra_features: Vec<String>,
800 impl Step for $name {
801 type Output = Option<PathBuf>;
802 const DEFAULT: bool = true; // Overwritten below
803 const ONLY_HOSTS: bool = true;
805 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
806 let builder = run.builder;
807 run.path($path).default_condition(
808 builder.config.extended
809 && builder.config.tools.as_ref().map_or(
810 // By default, on nightly/dev enable all tools, else only
811 // build stable tools.
812 $stable || builder.build.unstable_features(),
813 // If `tools` is set, search list for this tool.
815 tools.iter().any(|tool| match tool.as_ref() {
816 "clippy" => $tool_name == "clippy-driver",
817 x => $tool_name == x,
823 fn make_run(run: RunConfig<'_>) {
824 run.builder.ensure($name {
825 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
827 extra_features: Vec::new(),
832 fn run(mut $sel, $builder: &Builder<'_>) -> Option<PathBuf> {
833 $builder.ensure(ToolBuild {
834 compiler: $sel.compiler,
837 mode: if false $(|| $tool_std)? { Mode::ToolStd } else { Mode::ToolRustc },
839 extra_features: $sel.extra_features,
840 is_optional_tool: true,
841 source_type: SourceType::InTree,
849 // Note: tools need to be also added to `Builder::get_step_descriptions` in `builder.rs`
850 // to make `./x.py build <tool>` work.
851 // Note: Most submodule updates for tools are handled by bootstrap.py, since they're needed just to
852 // invoke Cargo to build bootstrap. See the comment there for more details.
853 tool_extended!((self, builder),
854 Cargofmt, "src/tools/rustfmt", "cargo-fmt", stable=true;
855 CargoClippy, "src/tools/clippy", "cargo-clippy", stable=true;
856 Clippy, "src/tools/clippy", "clippy-driver", stable=true;
857 Miri, "src/tools/miri", "miri", stable=false;
858 CargoMiri, "src/tools/miri/cargo-miri", "cargo-miri", stable=true;
859 // FIXME: tool_std is not quite right, we shouldn't allow nightly features.
860 // But `builder.cargo` doesn't know how to handle ToolBootstrap in stages other than 0,
861 // and this is close enough for now.
862 Rls, "src/tools/rls", "rls", stable=true, tool_std=true;
863 RustDemangler, "src/tools/rust-demangler", "rust-demangler", stable=false, tool_std=true;
864 Rustfmt, "src/tools/rustfmt", "rustfmt", stable=true;
867 impl<'a> Builder<'a> {
868 /// Gets a `Command` which is ready to run `tool` in `stage` built for
870 pub fn tool_cmd(&self, tool: Tool) -> Command {
871 let mut cmd = Command::new(self.tool_exe(tool));
872 let compiler = self.compiler(0, self.config.build);
873 let host = &compiler.host;
874 // Prepares the `cmd` provided to be able to run the `compiler` provided.
876 // Notably this munges the dynamic library lookup path to point to the
877 // right location to run `compiler`.
878 let mut lib_paths: Vec<PathBuf> = vec![
879 self.build.rustc_snapshot_libdir(),
880 self.cargo_out(compiler, Mode::ToolBootstrap, *host).join("deps"),
883 // On MSVC a tool may invoke a C compiler (e.g., compiletest in run-make
884 // mode) and that C compiler may need some extra PATH modification. Do
886 if compiler.host.contains("msvc") {
887 let curpaths = env::var_os("PATH").unwrap_or_default();
888 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
889 for &(ref k, ref v) in self.cc[&compiler.host].env() {
893 for path in env::split_paths(v) {
894 if !curpaths.contains(&path) {
895 lib_paths.push(path);
901 add_dylib_path(lib_paths, &mut cmd);
903 // Provide a RUSTC for this command to use.
904 cmd.env("RUSTC", &self.initial_rustc);