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>,
32 /// Nightly-only features that are allowed (comma-separated list).
33 allow_features: &'static str,
36 impl Step for ToolBuild {
37 type Output = Option<PathBuf>;
39 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
43 /// Builds a tool in `src/tools`
45 /// This will build the specified tool with the specified `host` compiler in
46 /// `stage` into the normal cargo output directory.
47 fn run(self, builder: &Builder<'_>) -> Option<PathBuf> {
48 let compiler = self.compiler;
49 let target = self.target;
50 let mut tool = self.tool;
52 let is_optional_tool = self.is_optional_tool;
56 builder.ensure(compile::Std::new(compiler, compiler.host));
57 builder.ensure(compile::Rustc::new(compiler, target));
59 Mode::ToolStd => builder.ensure(compile::Std::new(compiler, target)),
60 Mode::ToolBootstrap => {} // uses downloaded stage0 compiler libs
61 _ => panic!("unexpected Mode for tool build"),
64 let mut cargo = prepare_tool_cargo(
74 if !self.allow_features.is_empty() {
75 cargo.allow_features(self.allow_features);
78 builder.info(&format!("Building stage{} tool {} ({})", compiler.stage, tool, target));
79 let mut duplicates = Vec::new();
80 let is_expected = compile::stream_cargo(builder, cargo, vec![], &mut |msg| {
81 // Only care about big things like the RLS/Cargo for now
83 "rls" | "cargo" | "clippy-driver" | "miri" | "rustfmt" => {}
87 let (id, features, filenames) = match msg {
88 compile::CargoMessage::CompilerArtifact {
93 } => (package_id, features, filenames),
96 let features = features.iter().map(|s| s.to_string()).collect::<Vec<_>>();
98 for path in filenames {
99 let val = (tool, PathBuf::from(&*path), features.clone());
100 // we're only interested in deduplicating rlibs for now
101 if val.1.extension().and_then(|s| s.to_str()) != Some("rlib") {
105 // Don't worry about compiles that turn out to be host
106 // dependencies or build scripts. To skip these we look for
107 // anything that goes in `.../release/deps` but *doesn't* go in
108 // `$target/release/deps`. This ensure that outputs in
109 // `$target/release` are still considered candidates for
111 if let Some(parent) = val.1.parent() {
112 if parent.ends_with("release/deps") {
113 let maybe_target = parent
115 .and_then(|p| p.parent())
116 .and_then(|p| p.file_name())
117 .and_then(|p| p.to_str())
119 if maybe_target != &*target.triple {
125 // Record that we've built an artifact for `id`, and if one was
126 // already listed then we need to see if we reused the same
127 // artifact or produced a duplicate.
128 let mut artifacts = builder.tool_artifacts.borrow_mut();
129 let prev_artifacts = artifacts.entry(target).or_default();
130 let prev = match prev_artifacts.get(&*id) {
133 prev_artifacts.insert(id.to_string(), val);
138 return; // same path, same artifact
141 // If the paths are different and one of them *isn't* inside of
142 // `release/deps`, then it means it's probably in
143 // `$target/release`, or it's some final artifact like
144 // `libcargo.rlib`. In these situations Cargo probably just
145 // copied it up from `$target/release/deps/libcargo-xxxx.rlib`,
146 // so if the features are equal we can just skip it.
147 let prev_no_hash = prev.1.parent().unwrap().ends_with("release/deps");
148 let val_no_hash = val.1.parent().unwrap().ends_with("release/deps");
149 if prev.2 == val.2 || !prev_no_hash || !val_no_hash {
153 // ... and otherwise this looks like we duplicated some sort of
154 // compilation, so record it to generate an error later.
155 duplicates.push((id.to_string(), val, prev.clone()));
159 if is_expected && !duplicates.is_empty() {
161 "duplicate artifacts found when compiling a tool, this \
162 typically means that something was recompiled because \
163 a transitive dependency has different features activated \
164 than in a previous build:\n"
166 let (same, different): (Vec<_>, Vec<_>) =
167 duplicates.into_iter().partition(|(_, cur, prev)| cur.2 == prev.2);
168 if !same.is_empty() {
170 "the following dependencies are duplicated although they \
171 have the same features enabled:"
173 for (id, cur, prev) in same {
174 eprintln!(" {}", id);
176 eprintln!(" `{}` ({:?})\n `{}` ({:?})", cur.0, cur.1, prev.0, prev.1);
179 if !different.is_empty() {
180 eprintln!("the following dependencies have different features:");
181 for (id, cur, prev) in different {
182 eprintln!(" {}", id);
183 let cur_features: HashSet<_> = cur.2.into_iter().collect();
184 let prev_features: HashSet<_> = prev.2.into_iter().collect();
186 " `{}` additionally enabled features {:?} at {:?}",
188 &cur_features - &prev_features,
192 " `{}` additionally enabled features {:?} at {:?}",
194 &prev_features - &cur_features,
201 "to fix this you will probably want to edit the local \
202 src/tools/rustc-workspace-hack/Cargo.toml crate, as \
203 that will update the dependency graph to ensure that \
204 these crates all share the same feature set"
206 panic!("tools should not compile multiple copies of the same crate");
209 builder.save_toolstate(
211 if is_expected { ToolState::TestFail } else { ToolState::BuildFail },
215 if !is_optional_tool {
216 crate::detail_exit(1);
221 // HACK(#82501): on Windows, the tools directory gets added to PATH when running tests, and
222 // compiletest confuses HTML tidy with the in-tree tidy. Name the in-tree tidy something
223 // different so the problem doesn't come up.
227 let cargo_out = builder.cargo_out(compiler, self.mode, target).join(exe(tool, target));
228 let bin = builder.tools_dir(compiler).join(exe(tool, target));
229 builder.copy(&cargo_out, &bin);
235 pub fn prepare_tool_cargo(
236 builder: &Builder<'_>,
239 target: TargetSelection,
240 command: &'static str,
242 source_type: SourceType,
243 extra_features: &[String],
245 let mut cargo = builder.cargo(compiler, mode, source_type, target, command);
246 let dir = builder.src.join(path);
247 cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
249 let mut features = extra_features.to_vec();
250 if builder.build.config.cargo_native_static {
251 if path.ends_with("cargo")
252 || path.ends_with("rls")
253 || path.ends_with("clippy")
254 || path.ends_with("miri")
255 || path.ends_with("rustfmt")
257 cargo.env("LIBZ_SYS_STATIC", "1");
258 features.push("rustc-workspace-hack/all-static".to_string());
262 // clippy tests need to know about the stage sysroot. Set them consistently while building to
263 // avoid rebuilding when running tests.
264 cargo.env("SYSROOT", builder.sysroot(compiler));
266 // if tools are using lzma we want to force the build script to build its
268 cargo.env("LZMA_API_STATIC", "1");
270 // CFG_RELEASE is needed by rustfmt (and possibly other tools) which
271 // import rustc-ap-rustc_attr which requires this to be set for the
272 // `#[cfg(version(...))]` attribute.
273 cargo.env("CFG_RELEASE", builder.rust_release());
274 cargo.env("CFG_RELEASE_CHANNEL", &builder.config.channel);
275 cargo.env("CFG_VERSION", builder.rust_version());
276 cargo.env("CFG_RELEASE_NUM", &builder.version);
277 cargo.env("DOC_RUST_LANG_ORG_CHANNEL", builder.doc_rust_lang_org_channel());
279 let info = GitInfo::new(builder.config.ignore_git, &dir);
280 if let Some(sha) = info.sha() {
281 cargo.env("CFG_COMMIT_HASH", sha);
283 if let Some(sha_short) = info.sha_short() {
284 cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
286 if let Some(date) = info.commit_date() {
287 cargo.env("CFG_COMMIT_DATE", date);
289 if !features.is_empty() {
290 cargo.arg("--features").arg(&features.join(", "));
295 macro_rules! bootstrap_tool {
297 $name:ident, $path:expr, $tool_name:expr
298 $(,is_external_tool = $external:expr)*
299 $(,is_unstable_tool = $unstable:expr)*
300 $(,allow_features = $allow_features:expr)?
303 #[derive(Copy, PartialEq, Eq, Clone)]
310 impl<'a> Builder<'a> {
311 pub fn tool_exe(&self, tool: Tool) -> PathBuf {
315 compiler: self.compiler(0, self.config.build),
316 target: self.config.build,
324 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
326 pub compiler: Compiler,
327 pub target: TargetSelection,
330 impl Step for $name {
331 type Output = PathBuf;
333 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
337 fn make_run(run: RunConfig<'_>) {
338 run.builder.ensure($name {
340 compiler: run.builder.compiler(0, run.builder.config.build),
345 fn run(self, builder: &Builder<'_>) -> PathBuf {
346 builder.ensure(ToolBuild {
347 compiler: self.compiler,
350 mode: if false $(|| $unstable)* {
351 // use in-tree libraries for unstable features
357 is_optional_tool: false,
358 source_type: if false $(|| $external)* {
359 SourceType::Submodule
363 extra_features: vec![],
364 allow_features: concat!($($allow_features)*),
365 }).expect("expected to build -- essential tool")
373 Rustbook, "src/tools/rustbook", "rustbook";
374 UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen";
375 Tidy, "src/tools/tidy", "tidy";
376 Linkchecker, "src/tools/linkchecker", "linkchecker";
377 CargoTest, "src/tools/cargotest", "cargotest";
378 Compiletest, "src/tools/compiletest", "compiletest", is_unstable_tool = true, allow_features = "test";
379 BuildManifest, "src/tools/build-manifest", "build-manifest";
380 RemoteTestClient, "src/tools/remote-test-client", "remote-test-client";
381 RustInstaller, "src/tools/rust-installer", "rust-installer", is_external_tool = true;
382 RustdocTheme, "src/tools/rustdoc-themes", "rustdoc-themes";
383 ExpandYamlAnchors, "src/tools/expand-yaml-anchors", "expand-yaml-anchors";
384 LintDocs, "src/tools/lint-docs", "lint-docs";
385 JsonDocCk, "src/tools/jsondocck", "jsondocck";
386 JsonDocLint, "src/tools/jsondoclint", "jsondoclint";
387 HtmlChecker, "src/tools/html-checker", "html-checker";
388 BumpStage0, "src/tools/bump-stage0", "bump-stage0";
389 ReplaceVersionPlaceholder, "src/tools/replace-version-placeholder", "replace-version-placeholder";
390 CollectLicenseMetadata, "src/tools/collect-license-metadata", "collect-license-metadata";
391 GenerateCopyright, "src/tools/generate-copyright", "generate-copyright";
394 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
395 pub struct ErrorIndex {
396 pub compiler: Compiler,
400 pub fn command(builder: &Builder<'_>) -> Command {
401 // Error-index-generator links with the rustdoc library, so we need to add `rustc_lib_paths`
402 // for rustc_private and libLLVM.so, and `sysroot_lib` for libstd, etc.
403 let host = builder.config.build;
404 let compiler = builder.compiler_for(builder.top_stage, host, host);
405 let mut cmd = Command::new(builder.ensure(ErrorIndex { compiler }));
406 let mut dylib_paths = builder.rustc_lib_paths(compiler);
407 dylib_paths.push(PathBuf::from(&builder.sysroot_libdir(compiler, compiler.host)));
408 add_dylib_path(dylib_paths, &mut cmd);
413 impl Step for ErrorIndex {
414 type Output = PathBuf;
416 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
417 run.path("src/tools/error_index_generator")
420 fn make_run(run: RunConfig<'_>) {
421 // Compile the error-index in the same stage as rustdoc to avoid
422 // recompiling rustdoc twice if we can.
424 // NOTE: This `make_run` isn't used in normal situations, only if you
425 // manually build the tool with `x.py build
426 // src/tools/error-index-generator` which almost nobody does.
427 // Normally, `x.py test` or `x.py doc` will use the
428 // `ErrorIndex::command` function instead.
430 run.builder.compiler(run.builder.top_stage.saturating_sub(1), run.builder.config.build);
431 run.builder.ensure(ErrorIndex { compiler });
434 fn run(self, builder: &Builder<'_>) -> PathBuf {
437 compiler: self.compiler,
438 target: self.compiler.host,
439 tool: "error_index_generator",
440 mode: Mode::ToolRustc,
441 path: "src/tools/error_index_generator",
442 is_optional_tool: false,
443 source_type: SourceType::InTree,
444 extra_features: Vec::new(),
447 .expect("expected to build -- essential tool")
451 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
452 pub struct RemoteTestServer {
453 pub compiler: Compiler,
454 pub target: TargetSelection,
457 impl Step for RemoteTestServer {
458 type Output = PathBuf;
460 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
461 run.path("src/tools/remote-test-server")
464 fn make_run(run: RunConfig<'_>) {
465 run.builder.ensure(RemoteTestServer {
466 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
471 fn run(self, builder: &Builder<'_>) -> PathBuf {
474 compiler: self.compiler,
476 tool: "remote-test-server",
478 path: "src/tools/remote-test-server",
479 is_optional_tool: false,
480 source_type: SourceType::InTree,
481 extra_features: Vec::new(),
484 .expect("expected to build -- essential tool")
488 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
490 /// This should only ever be 0 or 2.
491 /// We sometimes want to reference the "bootstrap" rustdoc, which is why this option is here.
492 pub compiler: Compiler,
495 impl Step for Rustdoc {
496 type Output = PathBuf;
497 const DEFAULT: bool = true;
498 const ONLY_HOSTS: bool = true;
500 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
501 run.path("src/tools/rustdoc").path("src/librustdoc")
504 fn make_run(run: RunConfig<'_>) {
505 run.builder.ensure(Rustdoc {
506 // Note: this is somewhat unique in that we actually want a *target*
507 // compiler here, because rustdoc *is* a compiler. We won't be using
508 // this as the compiler to build with, but rather this is "what
509 // compiler are we producing"?
510 compiler: run.builder.compiler(run.builder.top_stage, run.target),
514 fn run(self, builder: &Builder<'_>) -> PathBuf {
515 let target_compiler = self.compiler;
516 if target_compiler.stage == 0 {
517 if !target_compiler.is_snapshot(builder) {
518 panic!("rustdoc in stage 0 must be snapshot rustdoc");
520 return builder.initial_rustc.with_file_name(exe("rustdoc", target_compiler.host));
522 let target = target_compiler.host;
523 // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
524 // we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
525 // compilers, which isn't what we want. Rustdoc should be linked in the same way as the
526 // rustc compiler it's paired with, so it must be built with the previous stage compiler.
527 let build_compiler = builder.compiler(target_compiler.stage - 1, builder.config.build);
529 // When using `download-rustc` and a stage0 build_compiler, copying rustc doesn't actually
530 // build stage0 libstd (because the libstd in sysroot has the wrong ABI). Explicitly build
532 builder.ensure(compile::Std::new(build_compiler, target_compiler.host));
533 builder.ensure(compile::Rustc::new(build_compiler, target_compiler.host));
534 // NOTE: this implies that `download-rustc` is pretty useless when compiling with the stage0
535 // compiler, since you do just as much work.
536 if !builder.config.dry_run() && builder.download_rustc() && build_compiler.stage == 0 {
538 "warning: `download-rustc` does nothing when building stage1 tools; consider using `--stage 2` instead"
542 // The presence of `target_compiler` ensures that the necessary libraries (codegen backends,
543 // compiler libraries, ...) are built. Rustdoc does not require the presence of any
544 // libraries within sysroot_libdir (i.e., rustlib), though doctests may want it (since
545 // they'll be linked to those libraries). As such, don't explicitly `ensure` any additional
546 // libraries here. The intuition here is that If we've built a compiler, we should be able
549 let mut features = Vec::new();
550 if builder.config.jemalloc {
551 features.push("jemalloc".to_string());
554 let cargo = prepare_tool_cargo(
565 builder.info(&format!(
566 "Building rustdoc for stage{} ({})",
567 target_compiler.stage, target_compiler.host
569 builder.run(&mut cargo.into());
571 // Cargo adds a number of paths to the dylib search path on windows, which results in
572 // the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
573 // rustdoc a different name.
574 let tool_rustdoc = builder
575 .cargo_out(build_compiler, Mode::ToolRustc, target)
576 .join(exe("rustdoc_tool_binary", target_compiler.host));
578 // don't create a stage0-sysroot/bin directory.
579 if target_compiler.stage > 0 {
580 let sysroot = builder.sysroot(target_compiler);
581 let bindir = sysroot.join("bin");
582 t!(fs::create_dir_all(&bindir));
583 let bin_rustdoc = bindir.join(exe("rustdoc", target_compiler.host));
584 let _ = fs::remove_file(&bin_rustdoc);
585 builder.copy(&tool_rustdoc, &bin_rustdoc);
593 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
595 pub compiler: Compiler,
596 pub target: TargetSelection,
599 impl Step for Cargo {
600 type Output = PathBuf;
601 const DEFAULT: bool = true;
602 const ONLY_HOSTS: bool = true;
604 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
605 let builder = run.builder;
606 run.path("src/tools/cargo").default_condition(
607 builder.config.extended
608 && builder.config.tools.as_ref().map_or(
610 // If `tools` is set, search list for this tool.
611 |tools| tools.iter().any(|tool| tool == "cargo"),
616 fn make_run(run: RunConfig<'_>) {
617 run.builder.ensure(Cargo {
618 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
623 fn run(self, builder: &Builder<'_>) -> PathBuf {
624 let cargo_bin_path = builder
626 compiler: self.compiler,
629 mode: Mode::ToolRustc,
630 path: "src/tools/cargo",
631 is_optional_tool: false,
632 source_type: SourceType::Submodule,
633 extra_features: Vec::new(),
636 .expect("expected to build -- essential tool");
638 let build_cred = |name, path| {
639 // These credential helpers are currently experimental.
640 // Any build failures will be ignored.
641 let _ = builder.ensure(ToolBuild {
642 compiler: self.compiler,
645 mode: Mode::ToolRustc,
647 is_optional_tool: true,
648 source_type: SourceType::Submodule,
649 extra_features: Vec::new(),
654 if self.target.contains("windows") {
656 "cargo-credential-wincred",
657 "src/tools/cargo/crates/credential/cargo-credential-wincred",
660 if self.target.contains("apple-darwin") {
662 "cargo-credential-macos-keychain",
663 "src/tools/cargo/crates/credential/cargo-credential-macos-keychain",
667 "cargo-credential-1password",
668 "src/tools/cargo/crates/credential/cargo-credential-1password",
674 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
675 pub struct LldWrapper {
676 pub compiler: Compiler,
677 pub target: TargetSelection,
680 impl Step for LldWrapper {
681 type Output = PathBuf;
683 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
687 fn run(self, builder: &Builder<'_>) -> PathBuf {
688 let src_exe = builder
690 compiler: self.compiler,
694 path: "src/tools/lld-wrapper",
695 is_optional_tool: false,
696 source_type: SourceType::InTree,
697 extra_features: Vec::new(),
700 .expect("expected to build -- essential tool");
706 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
707 pub struct RustAnalyzer {
708 pub compiler: Compiler,
709 pub target: TargetSelection,
713 pub const ALLOW_FEATURES: &str =
714 "proc_macro_internals,proc_macro_diagnostic,proc_macro_span,proc_macro_span_shrink";
717 impl Step for RustAnalyzer {
718 type Output = Option<PathBuf>;
719 const DEFAULT: bool = true;
720 const ONLY_HOSTS: bool = true;
722 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
723 let builder = run.builder;
724 run.path("src/tools/rust-analyzer").default_condition(
725 builder.config.extended
730 .map_or(true, |tools| tools.iter().any(|tool| tool == "rust-analyzer")),
734 fn make_run(run: RunConfig<'_>) {
735 run.builder.ensure(RustAnalyzer {
736 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
741 fn run(self, builder: &Builder<'_>) -> Option<PathBuf> {
742 builder.ensure(ToolBuild {
743 compiler: self.compiler,
745 tool: "rust-analyzer",
747 path: "src/tools/rust-analyzer",
748 extra_features: vec!["rust-analyzer/in-rust-tree".to_owned()],
749 is_optional_tool: false,
750 source_type: SourceType::InTree,
751 allow_features: RustAnalyzer::ALLOW_FEATURES,
756 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
757 pub struct RustAnalyzerProcMacroSrv {
758 pub compiler: Compiler,
759 pub target: TargetSelection,
762 impl Step for RustAnalyzerProcMacroSrv {
763 type Output = Option<PathBuf>;
764 const DEFAULT: bool = true;
765 const ONLY_HOSTS: bool = true;
767 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
768 let builder = run.builder;
769 // Allow building `rust-analyzer-proc-macro-srv` both as part of the `rust-analyzer` and as a stand-alone tool.
770 run.path("src/tools/rust-analyzer")
771 .path("src/tools/rust-analyzer/crates/proc-macro-srv-cli")
772 .default_condition(builder.config.tools.as_ref().map_or(true, |tools| {
775 .any(|tool| tool == "rust-analyzer" || tool == "rust-analyzer-proc-macro-srv")
779 fn make_run(run: RunConfig<'_>) {
780 run.builder.ensure(RustAnalyzerProcMacroSrv {
781 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
786 fn run(self, builder: &Builder<'_>) -> Option<PathBuf> {
787 let path = builder.ensure(ToolBuild {
788 compiler: self.compiler,
790 tool: "rust-analyzer-proc-macro-srv",
792 path: "src/tools/rust-analyzer/crates/proc-macro-srv-cli",
793 extra_features: vec!["proc-macro-srv/sysroot-abi".to_owned()],
794 is_optional_tool: false,
795 source_type: SourceType::InTree,
796 allow_features: RustAnalyzer::ALLOW_FEATURES,
799 // Copy `rust-analyzer-proc-macro-srv` to `<sysroot>/libexec/`
800 // so that r-a can use it.
801 let libexec_path = builder.sysroot(self.compiler).join("libexec");
802 t!(fs::create_dir_all(&libexec_path));
803 builder.copy(&path, &libexec_path.join("rust-analyzer-proc-macro-srv"));
809 macro_rules! tool_extended {
810 (($sel:ident, $builder:ident),
814 stable = $stable:expr
815 $(,tool_std = $tool_std:literal)?
816 $(,allow_features = $allow_features:expr)?
819 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
821 pub compiler: Compiler,
822 pub target: TargetSelection,
823 pub extra_features: Vec<String>,
826 impl Step for $name {
827 type Output = Option<PathBuf>;
828 const DEFAULT: bool = true; // Overwritten below
829 const ONLY_HOSTS: bool = true;
831 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
832 let builder = run.builder;
833 run.path($path).default_condition(
834 builder.config.extended
835 && builder.config.tools.as_ref().map_or(
836 // By default, on nightly/dev enable all tools, else only
837 // build stable tools.
838 $stable || builder.build.unstable_features(),
839 // If `tools` is set, search list for this tool.
841 tools.iter().any(|tool| match tool.as_ref() {
842 "clippy" => $tool_name == "clippy-driver",
843 x => $tool_name == x,
849 fn make_run(run: RunConfig<'_>) {
850 run.builder.ensure($name {
851 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
853 extra_features: Vec::new(),
858 fn run(mut $sel, $builder: &Builder<'_>) -> Option<PathBuf> {
859 $builder.ensure(ToolBuild {
860 compiler: $sel.compiler,
863 mode: if false $(|| $tool_std)? { Mode::ToolStd } else { Mode::ToolRustc },
865 extra_features: $sel.extra_features,
866 is_optional_tool: true,
867 source_type: SourceType::InTree,
868 allow_features: concat!($($allow_features)*),
876 // Note: tools need to be also added to `Builder::get_step_descriptions` in `builder.rs`
877 // to make `./x.py build <tool>` work.
878 // Note: Most submodule updates for tools are handled by bootstrap.py, since they're needed just to
879 // invoke Cargo to build bootstrap. See the comment there for more details.
880 tool_extended!((self, builder),
881 Cargofmt, "src/tools/rustfmt", "cargo-fmt", stable=true;
882 CargoClippy, "src/tools/clippy", "cargo-clippy", stable=true;
883 Clippy, "src/tools/clippy", "clippy-driver", stable=true;
884 Miri, "src/tools/miri", "miri", stable=false;
885 CargoMiri, "src/tools/miri/cargo-miri", "cargo-miri", stable=true;
886 // FIXME: tool_std is not quite right, we shouldn't allow nightly features.
887 // But `builder.cargo` doesn't know how to handle ToolBootstrap in stages other than 0,
888 // and this is close enough for now.
889 Rls, "src/tools/rls", "rls", stable=true, tool_std=true;
890 RustDemangler, "src/tools/rust-demangler", "rust-demangler", stable=false, tool_std=true;
891 Rustfmt, "src/tools/rustfmt", "rustfmt", stable=true;
894 impl<'a> Builder<'a> {
895 /// Gets a `Command` which is ready to run `tool` in `stage` built for
897 pub fn tool_cmd(&self, tool: Tool) -> Command {
898 let mut cmd = Command::new(self.tool_exe(tool));
899 let compiler = self.compiler(0, self.config.build);
900 let host = &compiler.host;
901 // Prepares the `cmd` provided to be able to run the `compiler` provided.
903 // Notably this munges the dynamic library lookup path to point to the
904 // right location to run `compiler`.
905 let mut lib_paths: Vec<PathBuf> = vec![
906 self.build.rustc_snapshot_libdir(),
907 self.cargo_out(compiler, Mode::ToolBootstrap, *host).join("deps"),
910 // On MSVC a tool may invoke a C compiler (e.g., compiletest in run-make
911 // mode) and that C compiler may need some extra PATH modification. Do
913 if compiler.host.contains("msvc") {
914 let curpaths = env::var_os("PATH").unwrap_or_default();
915 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
916 for &(ref k, ref v) in self.cc[&compiler.host].env() {
920 for path in env::split_paths(v) {
921 if !curpaths.contains(&path) {
922 lib_paths.push(path);
928 add_dylib_path(lib_paths, &mut cmd);
930 // Provide a RUSTC for this command to use.
931 cmd.env("RUSTC", &self.initial_rustc);