1use std::ffi::OsStr;
13use std::path::PathBuf;
14use std::{env, fs};
15
16use crate::core::build_steps::compile::is_lto_stage;
17use crate::core::build_steps::toolstate::ToolState;
18use crate::core::build_steps::{compile, llvm};
19use crate::core::builder;
20use crate::core::builder::{
21 Builder, Cargo as CargoCommand, RunConfig, ShouldRun, Step, StepMetadata, cargo_profile_var,
22};
23use crate::core::config::{DebuginfoLevel, RustcLto, TargetSelection};
24use crate::utils::exec::{BootstrapCommand, command};
25use crate::utils::helpers::{add_dylib_path, exe, t};
26use crate::{Compiler, FileType, Kind, Mode};
27
28#[derive(Debug, Clone, Hash, PartialEq, Eq)]
29pub enum SourceType {
30 InTree,
31 Submodule,
32}
33
34#[derive(Debug, Clone, Hash, PartialEq, Eq)]
35pub enum ToolArtifactKind {
36 Binary,
37 Library,
38}
39
40#[derive(Debug, Clone, Hash, PartialEq, Eq)]
41struct ToolBuild {
42 build_compiler: Compiler,
44 target: TargetSelection,
45 tool: &'static str,
46 path: &'static str,
47 mode: Mode,
48 source_type: SourceType,
49 extra_features: Vec<String>,
50 allow_features: &'static str,
52 cargo_args: Vec<String>,
54 artifact_kind: ToolArtifactKind,
56}
57
58#[derive(Clone)]
61pub struct ToolBuildResult {
62 pub tool_path: PathBuf,
64 pub build_compiler: Compiler,
66}
67
68impl Step for ToolBuild {
69 type Output = ToolBuildResult;
70
71 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
72 run.never()
73 }
74
75 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
80 let target = self.target;
81 let mut tool = self.tool;
82 let path = self.path;
83
84 match self.mode {
85 Mode::ToolRustcPrivate => {
86 if !self.build_compiler.is_forced_compiler() && builder.download_rustc() {
88 builder.std(self.build_compiler, self.build_compiler.host);
89 builder.ensure(compile::Rustc::new(self.build_compiler, target));
90 }
91 }
92 Mode::ToolStd => {
93 if !self.build_compiler.is_forced_compiler() {
95 builder.std(self.build_compiler, target);
96 }
97 }
98 Mode::ToolBootstrap | Mode::ToolTarget => {} _ => panic!("unexpected Mode for tool build"),
100 }
101
102 let mut cargo = prepare_tool_cargo(
103 builder,
104 self.build_compiler,
105 self.mode,
106 target,
107 Kind::Build,
108 path,
109 self.source_type,
110 &self.extra_features,
111 );
112
113 if let Some(ref ccache) = builder.config.ccache
118 && matches!(self.mode, Mode::ToolBootstrap)
119 && !builder.config.incremental
120 {
121 cargo.env("RUSTC_WRAPPER", ccache);
122 }
123
124 if is_lto_stage(&self.build_compiler)
127 && (self.mode == Mode::ToolRustcPrivate || self.path == "src/tools/cargo")
128 {
129 let lto = match builder.config.rust_lto {
130 RustcLto::Off => Some("off"),
131 RustcLto::Thin => Some("thin"),
132 RustcLto::Fat => Some("fat"),
133 RustcLto::ThinLocal => None,
134 };
135 if let Some(lto) = lto {
136 cargo.env(cargo_profile_var("LTO", &builder.config), lto);
137 }
138 }
139
140 if !self.allow_features.is_empty() {
141 cargo.allow_features(self.allow_features);
142 }
143
144 cargo.args(self.cargo_args);
145
146 let _guard =
147 builder.msg(Kind::Build, self.tool, self.mode, self.build_compiler, self.target);
148
149 let build_success = compile::stream_cargo(builder, cargo, vec![], &mut |_| {});
151
152 builder.save_toolstate(
153 tool,
154 if build_success { ToolState::TestFail } else { ToolState::BuildFail },
155 );
156
157 if !build_success {
158 crate::exit!(1);
159 } else {
160 if tool == "tidy" {
164 tool = "rust-tidy";
165 }
166 let tool_path = match self.artifact_kind {
167 ToolArtifactKind::Binary => {
168 copy_link_tool_bin(builder, self.build_compiler, self.target, self.mode, tool)
169 }
170 ToolArtifactKind::Library => builder
171 .cargo_out(self.build_compiler, self.mode, self.target)
172 .join(format!("lib{tool}.rlib")),
173 };
174
175 ToolBuildResult { tool_path, build_compiler: self.build_compiler }
176 }
177 }
178}
179
180#[expect(clippy::too_many_arguments)] pub fn prepare_tool_cargo(
182 builder: &Builder<'_>,
183 compiler: Compiler,
184 mode: Mode,
185 target: TargetSelection,
186 cmd_kind: Kind,
187 path: &str,
188 source_type: SourceType,
189 extra_features: &[String],
190) -> CargoCommand {
191 let mut cargo = builder::Cargo::new(builder, compiler, mode, source_type, target, cmd_kind);
192
193 let path = PathBuf::from(path);
194 let dir = builder.src.join(&path);
195 cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
196
197 let mut features = extra_features.to_vec();
198 if builder.build.config.cargo_native_static {
199 if path.ends_with("cargo")
200 || path.ends_with("clippy")
201 || path.ends_with("miri")
202 || path.ends_with("rustfmt")
203 {
204 cargo.env("LIBZ_SYS_STATIC", "1");
205 }
206 if path.ends_with("cargo") {
207 features.push("all-static".to_string());
208 }
209 }
210
211 builder
217 .config
218 .tool
219 .iter()
220 .filter(|(tool_name, _)| path.file_name().and_then(OsStr::to_str) == Some(tool_name))
221 .for_each(|(_, tool)| features.extend(tool.features.clone().unwrap_or_default()));
222
223 cargo.env("SYSROOT", builder.sysroot(compiler));
226
227 cargo.env("LZMA_API_STATIC", "1");
230
231 if env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none() {
234 if target.starts_with("aarch64") {
237 cargo.env("JEMALLOC_SYS_WITH_LG_PAGE", "16");
238 }
239 else if target.starts_with("loongarch") {
241 cargo.env("JEMALLOC_SYS_WITH_LG_PAGE", "14");
242 }
243 }
244
245 cargo.env("CFG_RELEASE", builder.rust_release());
249 cargo.env("CFG_RELEASE_CHANNEL", &builder.config.channel);
250 cargo.env("CFG_VERSION", builder.rust_version());
251 cargo.env("CFG_RELEASE_NUM", &builder.version);
252 cargo.env("DOC_RUST_LANG_ORG_CHANNEL", builder.doc_rust_lang_org_channel());
253
254 if let Some(ref ver_date) = builder.rust_info().commit_date() {
255 cargo.env("CFG_VER_DATE", ver_date);
256 }
257
258 if let Some(ref ver_hash) = builder.rust_info().sha() {
259 cargo.env("CFG_VER_HASH", ver_hash);
260 }
261
262 if let Some(description) = &builder.config.description {
263 cargo.env("CFG_VER_DESCRIPTION", description);
264 }
265
266 let info = builder.config.git_info(builder.config.omit_git_hash, &dir);
267 if let Some(sha) = info.sha() {
268 cargo.env("CFG_COMMIT_HASH", sha);
269 }
270
271 if let Some(sha_short) = info.sha_short() {
272 cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
273 }
274
275 if let Some(date) = info.commit_date() {
276 cargo.env("CFG_COMMIT_DATE", date);
277 }
278
279 if !features.is_empty() {
280 cargo.arg("--features").arg(features.join(", "));
281 }
282
283 cargo.rustflag("-Zunstable-options");
291
292 if !path.ends_with("cargo") {
309 cargo.env("FORCE_ON_BROKEN_PIPE_KILL", "-Zon-broken-pipe=kill");
314 }
315
316 cargo
317}
318
319pub enum ToolTargetBuildMode {
322 Build(TargetSelection),
325 Dist(Compiler),
329}
330
331pub(crate) fn get_tool_target_compiler(
333 builder: &Builder<'_>,
334 mode: ToolTargetBuildMode,
335) -> Compiler {
336 let (target, build_compiler_stage) = match mode {
337 ToolTargetBuildMode::Build(target) => {
338 assert!(builder.top_stage > 0);
339 (target, builder.top_stage - 1)
341 }
342 ToolTargetBuildMode::Dist(target_compiler) => {
343 assert!(target_compiler.stage > 0);
344 (target_compiler.host, target_compiler.stage - 1)
347 }
348 };
349
350 let compiler = if builder.host_target == target {
351 builder.compiler(build_compiler_stage, builder.host_target)
352 } else {
353 let build_compiler = builder.compiler(build_compiler_stage.max(1), builder.host_target);
356 builder.std(build_compiler, builder.host_target);
358 build_compiler
359 };
360 builder.std(compiler, target);
361 compiler
362}
363
364fn copy_link_tool_bin(
367 builder: &Builder<'_>,
368 build_compiler: Compiler,
369 target: TargetSelection,
370 mode: Mode,
371 name: &str,
372) -> PathBuf {
373 let cargo_out = builder.cargo_out(build_compiler, mode, target).join(exe(name, target));
374 let bin = builder.tools_dir(build_compiler).join(exe(name, target));
375 builder.copy_link(&cargo_out, &bin, FileType::Executable);
376 bin
377}
378
379macro_rules! bootstrap_tool {
380 ($(
381 $name:ident, $path:expr, $tool_name:expr
382 $(,is_external_tool = $external:expr)*
383 $(,is_unstable_tool = $unstable:expr)*
384 $(,allow_features = $allow_features:expr)?
385 $(,submodules = $submodules:expr)?
386 $(,artifact_kind = $artifact_kind:expr)?
387 ;
388 )+) => {
389 #[derive(PartialEq, Eq, Clone)]
390 pub enum Tool {
391 $(
392 $name,
393 )+
394 }
395
396 impl<'a> Builder<'a> {
397 pub fn tool_exe(&self, tool: Tool) -> PathBuf {
398 match tool {
399 $(Tool::$name =>
400 self.ensure($name {
401 compiler: self.compiler(0, self.config.host_target),
402 target: self.config.host_target,
403 }).tool_path,
404 )+
405 }
406 }
407 }
408
409 $(
410 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
411 pub struct $name {
412 pub compiler: Compiler,
413 pub target: TargetSelection,
414 }
415
416 impl Step for $name {
417 type Output = ToolBuildResult;
418
419 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
420 run.path($path)
421 }
422
423 fn make_run(run: RunConfig<'_>) {
424 run.builder.ensure($name {
425 compiler: run.builder.compiler(0, run.builder.config.host_target),
427 target: run.target,
428 });
429 }
430
431 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
432 $(
433 for submodule in $submodules {
434 builder.require_submodule(submodule, None);
435 }
436 )*
437
438 let is_unstable = false $(|| $unstable)*;
439 let compiletest_wants_stage0 = $tool_name == "compiletest" && builder.config.compiletest_use_stage0_libtest;
440
441 builder.ensure(ToolBuild {
442 build_compiler: self.compiler,
443 target: self.target,
444 tool: $tool_name,
445 mode: if is_unstable && !compiletest_wants_stage0 {
446 Mode::ToolStd
448 } else {
449 Mode::ToolBootstrap
450 },
451 path: $path,
452 source_type: if false $(|| $external)* {
453 SourceType::Submodule
454 } else {
455 SourceType::InTree
456 },
457 extra_features: vec![],
458 allow_features: {
459 let mut _value = "";
460 $( _value = $allow_features; )?
461 _value
462 },
463 cargo_args: vec![],
464 artifact_kind: if false $(|| $artifact_kind == ToolArtifactKind::Library)* {
465 ToolArtifactKind::Library
466 } else {
467 ToolArtifactKind::Binary
468 }
469 })
470 }
471
472 fn metadata(&self) -> Option<StepMetadata> {
473 Some(
474 StepMetadata::build(stringify!($name), self.target)
475 .built_by(self.compiler)
476 )
477 }
478 }
479 )+
480 }
481}
482
483pub(crate) const COMPILETEST_ALLOW_FEATURES: &str = "internal_output_capture";
484
485bootstrap_tool!(
486 Rustbook, "src/tools/rustbook", "rustbook", is_external_tool = true, submodules = SUBMODULES_FOR_RUSTBOOK;
491 UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen";
492 Tidy, "src/tools/tidy", "tidy";
493 Linkchecker, "src/tools/linkchecker", "linkchecker";
494 CargoTest, "src/tools/cargotest", "cargotest";
495 Compiletest, "src/tools/compiletest", "compiletest", is_unstable_tool = true, allow_features = COMPILETEST_ALLOW_FEATURES;
496 BuildManifest, "src/tools/build-manifest", "build-manifest";
497 RemoteTestClient, "src/tools/remote-test-client", "remote-test-client";
498 RustInstaller, "src/tools/rust-installer", "rust-installer";
499 RustdocTheme, "src/tools/rustdoc-themes", "rustdoc-themes";
500 LintDocs, "src/tools/lint-docs", "lint-docs";
501 JsonDocCk, "src/tools/jsondocck", "jsondocck";
502 JsonDocLint, "src/tools/jsondoclint", "jsondoclint";
503 HtmlChecker, "src/tools/html-checker", "html-checker";
504 BumpStage0, "src/tools/bump-stage0", "bump-stage0";
505 ReplaceVersionPlaceholder, "src/tools/replace-version-placeholder", "replace-version-placeholder";
506 CollectLicenseMetadata, "src/tools/collect-license-metadata", "collect-license-metadata";
507 GenerateCopyright, "src/tools/generate-copyright", "generate-copyright";
508 GenerateWindowsSys, "src/tools/generate-windows-sys", "generate-windows-sys";
509 RustdocGUITest, "src/tools/rustdoc-gui-test", "rustdoc-gui-test", is_unstable_tool = true, allow_features = COMPILETEST_ALLOW_FEATURES;
511 CoverageDump, "src/tools/coverage-dump", "coverage-dump";
512 UnicodeTableGenerator, "src/tools/unicode-table-generator", "unicode-table-generator";
513 FeaturesStatusDump, "src/tools/features-status-dump", "features-status-dump";
514 OptimizedDist, "src/tools/opt-dist", "opt-dist", submodules = &["src/tools/rustc-perf"];
515 RunMakeSupport, "src/tools/run-make-support", "run_make_support", artifact_kind = ToolArtifactKind::Library;
516);
517
518pub static SUBMODULES_FOR_RUSTBOOK: &[&str] = &["src/doc/book", "src/doc/reference"];
521
522#[derive(Debug, Clone, Hash, PartialEq, Eq)]
525pub struct RustcPerf {
526 pub compiler: Compiler,
527 pub target: TargetSelection,
528}
529
530impl Step for RustcPerf {
531 type Output = ToolBuildResult;
533
534 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
535 run.path("src/tools/rustc-perf")
536 }
537
538 fn make_run(run: RunConfig<'_>) {
539 run.builder.ensure(RustcPerf {
540 compiler: run.builder.compiler(0, run.builder.config.host_target),
541 target: run.target,
542 });
543 }
544
545 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
546 builder.require_submodule("src/tools/rustc-perf", None);
548
549 let tool = ToolBuild {
550 build_compiler: self.compiler,
551 target: self.target,
552 tool: "collector",
553 mode: Mode::ToolBootstrap,
554 path: "src/tools/rustc-perf",
555 source_type: SourceType::Submodule,
556 extra_features: Vec::new(),
557 allow_features: "",
558 cargo_args: vec!["-p".to_string(), "collector".to_string()],
561 artifact_kind: ToolArtifactKind::Binary,
562 };
563 let res = builder.ensure(tool.clone());
564 copy_link_tool_bin(builder, tool.build_compiler, tool.target, tool.mode, "rustc-fake");
567
568 res
569 }
570}
571
572#[derive(Debug, Clone, Hash, PartialEq, Eq)]
573pub struct ErrorIndex {
574 compilers: RustcPrivateCompilers,
575}
576
577impl ErrorIndex {
578 pub fn command(builder: &Builder<'_>, compilers: RustcPrivateCompilers) -> BootstrapCommand {
579 let mut cmd = command(builder.ensure(ErrorIndex { compilers }).tool_path);
582
583 let target_compiler = compilers.target_compiler();
584 let mut dylib_paths = builder.rustc_lib_paths(target_compiler);
585 dylib_paths.push(builder.sysroot_target_libdir(target_compiler, target_compiler.host));
586 add_dylib_path(dylib_paths, &mut cmd);
587 cmd
588 }
589}
590
591impl Step for ErrorIndex {
592 type Output = ToolBuildResult;
593
594 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
595 run.path("src/tools/error_index_generator")
596 }
597
598 fn make_run(run: RunConfig<'_>) {
599 run.builder.ensure(ErrorIndex {
605 compilers: RustcPrivateCompilers::new(
606 run.builder,
607 run.builder.top_stage,
608 run.builder.host_target,
609 ),
610 });
611 }
612
613 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
614 builder.ensure(ToolBuild {
615 build_compiler: self.compilers.build_compiler,
616 target: self.compilers.target(),
617 tool: "error_index_generator",
618 mode: Mode::ToolRustcPrivate,
619 path: "src/tools/error_index_generator",
620 source_type: SourceType::InTree,
621 extra_features: Vec::new(),
622 allow_features: "",
623 cargo_args: Vec::new(),
624 artifact_kind: ToolArtifactKind::Binary,
625 })
626 }
627
628 fn metadata(&self) -> Option<StepMetadata> {
629 Some(
630 StepMetadata::build("error-index", self.compilers.target())
631 .built_by(self.compilers.build_compiler),
632 )
633 }
634}
635
636#[derive(Debug, Clone, Hash, PartialEq, Eq)]
637pub struct RemoteTestServer {
638 pub build_compiler: Compiler,
639 pub target: TargetSelection,
640}
641
642impl Step for RemoteTestServer {
643 type Output = ToolBuildResult;
644
645 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
646 run.path("src/tools/remote-test-server")
647 }
648
649 fn make_run(run: RunConfig<'_>) {
650 run.builder.ensure(RemoteTestServer {
651 build_compiler: get_tool_target_compiler(
652 run.builder,
653 ToolTargetBuildMode::Build(run.target),
654 ),
655 target: run.target,
656 });
657 }
658
659 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
660 builder.ensure(ToolBuild {
661 build_compiler: self.build_compiler,
662 target: self.target,
663 tool: "remote-test-server",
664 mode: Mode::ToolTarget,
665 path: "src/tools/remote-test-server",
666 source_type: SourceType::InTree,
667 extra_features: Vec::new(),
668 allow_features: "",
669 cargo_args: Vec::new(),
670 artifact_kind: ToolArtifactKind::Binary,
671 })
672 }
673
674 fn metadata(&self) -> Option<StepMetadata> {
675 Some(StepMetadata::build("remote-test-server", self.target).built_by(self.build_compiler))
676 }
677}
678
679#[derive(Debug, Clone, Hash, PartialEq, Eq)]
684pub struct Rustdoc {
685 pub target_compiler: Compiler,
688}
689
690impl Step for Rustdoc {
691 type Output = PathBuf;
693
694 const DEFAULT: bool = true;
695 const IS_HOST: bool = true;
696
697 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
698 run.path("src/tools/rustdoc").path("src/librustdoc")
699 }
700
701 fn make_run(run: RunConfig<'_>) {
702 run.builder.ensure(Rustdoc {
703 target_compiler: run.builder.compiler(run.builder.top_stage, run.target),
704 });
705 }
706
707 fn run(self, builder: &Builder<'_>) -> Self::Output {
708 let target_compiler = self.target_compiler;
709 let target = target_compiler.host;
710
711 if target_compiler.stage == 0 {
713 if !target_compiler.is_snapshot(builder) {
714 panic!("rustdoc in stage 0 must be snapshot rustdoc");
715 }
716
717 return builder.initial_rustdoc.clone();
718 }
719
720 let bin_rustdoc = || {
722 let sysroot = builder.sysroot(target_compiler);
723 let bindir = sysroot.join("bin");
724 t!(fs::create_dir_all(&bindir));
725 let bin_rustdoc = bindir.join(exe("rustdoc", target_compiler.host));
726 let _ = fs::remove_file(&bin_rustdoc);
727 bin_rustdoc
728 };
729
730 if builder.download_rustc() && builder.rust_info().is_managed_git_subrepository() {
733 let files_to_track = &["src/librustdoc", "src/tools/rustdoc", "src/rustdoc-json-types"];
734
735 if !builder.config.has_changes_from_upstream(files_to_track) {
737 let precompiled_rustdoc = builder
738 .config
739 .ci_rustc_dir()
740 .join("bin")
741 .join(exe("rustdoc", target_compiler.host));
742
743 let bin_rustdoc = bin_rustdoc();
744 builder.copy_link(&precompiled_rustdoc, &bin_rustdoc, FileType::Executable);
745 return bin_rustdoc;
746 }
747 }
748
749 let mut extra_features = Vec::new();
757 if builder.config.jemalloc(target) {
758 extra_features.push("jemalloc".to_string());
759 }
760
761 let compilers = RustcPrivateCompilers::from_target_compiler(builder, target_compiler);
762 let tool_path = builder
763 .ensure(ToolBuild {
764 build_compiler: compilers.build_compiler,
765 target,
766 tool: "rustdoc_tool_binary",
770 mode: Mode::ToolRustcPrivate,
771 path: "src/tools/rustdoc",
772 source_type: SourceType::InTree,
773 extra_features,
774 allow_features: "",
775 cargo_args: Vec::new(),
776 artifact_kind: ToolArtifactKind::Binary,
777 })
778 .tool_path;
779
780 if builder.config.rust_debuginfo_level_tools == DebuginfoLevel::None {
781 compile::strip_debug(builder, target, &tool_path);
784 }
785 let bin_rustdoc = bin_rustdoc();
786 builder.copy_link(&tool_path, &bin_rustdoc, FileType::Executable);
787 bin_rustdoc
788 }
789
790 fn metadata(&self) -> Option<StepMetadata> {
791 Some(
792 StepMetadata::build("rustdoc", self.target_compiler.host)
793 .stage(self.target_compiler.stage),
794 )
795 }
796}
797
798#[derive(Debug, Clone, Hash, PartialEq, Eq)]
801pub struct Cargo {
802 build_compiler: Compiler,
803 target: TargetSelection,
804}
805
806impl Cargo {
807 pub fn from_build_compiler(build_compiler: Compiler, target: TargetSelection) -> Self {
810 Self { build_compiler, target }
811 }
812}
813
814impl Step for Cargo {
815 type Output = ToolBuildResult;
816 const DEFAULT: bool = true;
817 const IS_HOST: bool = true;
818
819 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
820 let builder = run.builder;
821 run.path("src/tools/cargo").default_condition(builder.tool_enabled("cargo"))
822 }
823
824 fn make_run(run: RunConfig<'_>) {
825 run.builder.ensure(Cargo {
826 build_compiler: get_tool_target_compiler(
827 run.builder,
828 ToolTargetBuildMode::Build(run.target),
829 ),
830 target: run.target,
831 });
832 }
833
834 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
835 builder.build.require_submodule("src/tools/cargo", None);
836
837 builder.std(self.build_compiler, builder.host_target);
838 builder.std(self.build_compiler, self.target);
839
840 builder.ensure(ToolBuild {
841 build_compiler: self.build_compiler,
842 target: self.target,
843 tool: "cargo",
844 mode: Mode::ToolTarget,
845 path: "src/tools/cargo",
846 source_type: SourceType::Submodule,
847 extra_features: Vec::new(),
848 allow_features: "min_specialization,specialization",
853 cargo_args: Vec::new(),
854 artifact_kind: ToolArtifactKind::Binary,
855 })
856 }
857
858 fn metadata(&self) -> Option<StepMetadata> {
859 Some(StepMetadata::build("cargo", self.target).built_by(self.build_compiler))
860 }
861}
862
863#[derive(Clone)]
866pub struct BuiltLldWrapper {
867 tool: ToolBuildResult,
868 lld_dir: PathBuf,
869}
870
871#[derive(Debug, Clone, Hash, PartialEq, Eq)]
872pub struct LldWrapper {
873 pub build_compiler: Compiler,
874 pub target: TargetSelection,
875}
876
877impl LldWrapper {
878 pub fn for_use_by_compiler(builder: &Builder<'_>, target_compiler: Compiler) -> Self {
880 Self {
881 build_compiler: get_tool_target_compiler(
882 builder,
883 ToolTargetBuildMode::Dist(target_compiler),
884 ),
885 target: target_compiler.host,
886 }
887 }
888}
889
890impl Step for LldWrapper {
891 type Output = BuiltLldWrapper;
892
893 const IS_HOST: bool = true;
894
895 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
896 run.path("src/tools/lld-wrapper")
897 }
898
899 fn make_run(run: RunConfig<'_>) {
900 run.builder.ensure(LldWrapper {
901 build_compiler: get_tool_target_compiler(
902 run.builder,
903 ToolTargetBuildMode::Build(run.target),
904 ),
905 target: run.target,
906 });
907 }
908
909 fn run(self, builder: &Builder<'_>) -> Self::Output {
910 let lld_dir = builder.ensure(llvm::Lld { target: self.target });
911 let tool = builder.ensure(ToolBuild {
912 build_compiler: self.build_compiler,
913 target: self.target,
914 tool: "lld-wrapper",
915 mode: Mode::ToolTarget,
916 path: "src/tools/lld-wrapper",
917 source_type: SourceType::InTree,
918 extra_features: Vec::new(),
919 allow_features: "",
920 cargo_args: Vec::new(),
921 artifact_kind: ToolArtifactKind::Binary,
922 });
923 BuiltLldWrapper { tool, lld_dir }
924 }
925
926 fn metadata(&self) -> Option<StepMetadata> {
927 Some(StepMetadata::build("LldWrapper", self.target).built_by(self.build_compiler))
928 }
929}
930
931pub(crate) fn copy_lld_artifacts(
932 builder: &Builder<'_>,
933 lld_wrapper: BuiltLldWrapper,
934 target_compiler: Compiler,
935) {
936 let target = target_compiler.host;
937
938 let libdir_bin = builder.sysroot_target_bindir(target_compiler, target);
939 t!(fs::create_dir_all(&libdir_bin));
940
941 let src_exe = exe("lld", target);
942 let dst_exe = exe("rust-lld", target);
943
944 builder.copy_link(
945 &lld_wrapper.lld_dir.join("bin").join(src_exe),
946 &libdir_bin.join(dst_exe),
947 FileType::Executable,
948 );
949 let self_contained_lld_dir = libdir_bin.join("gcc-ld");
950 t!(fs::create_dir_all(&self_contained_lld_dir));
951
952 for name in crate::LLD_FILE_NAMES {
953 builder.copy_link(
954 &lld_wrapper.tool.tool_path,
955 &self_contained_lld_dir.join(exe(name, target)),
956 FileType::Executable,
957 );
958 }
959}
960
961#[derive(Debug, Clone, Hash, PartialEq, Eq)]
964pub struct WasmComponentLd {
965 build_compiler: Compiler,
966 target: TargetSelection,
967}
968
969impl WasmComponentLd {
970 pub fn for_use_by_compiler(builder: &Builder<'_>, target_compiler: Compiler) -> Self {
972 Self {
973 build_compiler: get_tool_target_compiler(
974 builder,
975 ToolTargetBuildMode::Dist(target_compiler),
976 ),
977 target: target_compiler.host,
978 }
979 }
980}
981
982impl Step for WasmComponentLd {
983 type Output = ToolBuildResult;
984
985 const IS_HOST: bool = true;
986
987 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
988 run.path("src/tools/wasm-component-ld")
989 }
990
991 fn make_run(run: RunConfig<'_>) {
992 run.builder.ensure(WasmComponentLd {
993 build_compiler: get_tool_target_compiler(
994 run.builder,
995 ToolTargetBuildMode::Build(run.target),
996 ),
997 target: run.target,
998 });
999 }
1000
1001 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1002 builder.ensure(ToolBuild {
1003 build_compiler: self.build_compiler,
1004 target: self.target,
1005 tool: "wasm-component-ld",
1006 mode: Mode::ToolTarget,
1007 path: "src/tools/wasm-component-ld",
1008 source_type: SourceType::InTree,
1009 extra_features: vec![],
1010 allow_features: "",
1011 cargo_args: vec![],
1012 artifact_kind: ToolArtifactKind::Binary,
1013 })
1014 }
1015
1016 fn metadata(&self) -> Option<StepMetadata> {
1017 Some(StepMetadata::build("WasmComponentLd", self.target).built_by(self.build_compiler))
1018 }
1019}
1020
1021#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1022pub struct RustAnalyzer {
1023 compilers: RustcPrivateCompilers,
1024}
1025
1026impl RustAnalyzer {
1027 pub fn from_compilers(compilers: RustcPrivateCompilers) -> Self {
1028 Self { compilers }
1029 }
1030}
1031
1032impl RustAnalyzer {
1033 pub const ALLOW_FEATURES: &'static str = "rustc_private,proc_macro_internals,proc_macro_diagnostic,proc_macro_span,proc_macro_span_shrink,proc_macro_def_site";
1034}
1035
1036impl Step for RustAnalyzer {
1037 type Output = ToolBuildResult;
1038 const DEFAULT: bool = true;
1039 const IS_HOST: bool = true;
1040
1041 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1042 let builder = run.builder;
1043 run.path("src/tools/rust-analyzer").default_condition(builder.tool_enabled("rust-analyzer"))
1044 }
1045
1046 fn make_run(run: RunConfig<'_>) {
1047 run.builder.ensure(RustAnalyzer {
1048 compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1049 });
1050 }
1051
1052 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1053 let build_compiler = self.compilers.build_compiler;
1054 let target = self.compilers.target();
1055 builder.ensure(ToolBuild {
1056 build_compiler,
1057 target,
1058 tool: "rust-analyzer",
1059 mode: Mode::ToolRustcPrivate,
1060 path: "src/tools/rust-analyzer",
1061 extra_features: vec!["in-rust-tree".to_owned()],
1062 source_type: SourceType::InTree,
1063 allow_features: RustAnalyzer::ALLOW_FEATURES,
1064 cargo_args: Vec::new(),
1065 artifact_kind: ToolArtifactKind::Binary,
1066 })
1067 }
1068
1069 fn metadata(&self) -> Option<StepMetadata> {
1070 Some(
1071 StepMetadata::build("rust-analyzer", self.compilers.target())
1072 .built_by(self.compilers.build_compiler),
1073 )
1074 }
1075}
1076
1077#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1078pub struct RustAnalyzerProcMacroSrv {
1079 compilers: RustcPrivateCompilers,
1080}
1081
1082impl RustAnalyzerProcMacroSrv {
1083 pub fn from_compilers(compilers: RustcPrivateCompilers) -> Self {
1084 Self { compilers }
1085 }
1086}
1087
1088impl Step for RustAnalyzerProcMacroSrv {
1089 type Output = ToolBuildResult;
1090
1091 const DEFAULT: bool = true;
1092 const IS_HOST: bool = true;
1093
1094 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1095 let builder = run.builder;
1096 run.path("src/tools/rust-analyzer")
1098 .path("src/tools/rust-analyzer/crates/proc-macro-srv-cli")
1099 .default_condition(
1100 builder.tool_enabled("rust-analyzer")
1101 || builder.tool_enabled("rust-analyzer-proc-macro-srv"),
1102 )
1103 }
1104
1105 fn make_run(run: RunConfig<'_>) {
1106 run.builder.ensure(RustAnalyzerProcMacroSrv {
1107 compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1108 });
1109 }
1110
1111 fn run(self, builder: &Builder<'_>) -> Self::Output {
1112 let tool_result = builder.ensure(ToolBuild {
1113 build_compiler: self.compilers.build_compiler,
1114 target: self.compilers.target(),
1115 tool: "rust-analyzer-proc-macro-srv",
1116 mode: Mode::ToolRustcPrivate,
1117 path: "src/tools/rust-analyzer/crates/proc-macro-srv-cli",
1118 extra_features: vec!["in-rust-tree".to_owned()],
1119 source_type: SourceType::InTree,
1120 allow_features: RustAnalyzer::ALLOW_FEATURES,
1121 cargo_args: Vec::new(),
1122 artifact_kind: ToolArtifactKind::Binary,
1123 });
1124
1125 let libexec_path = builder.sysroot(self.compilers.target_compiler).join("libexec");
1128 t!(fs::create_dir_all(&libexec_path));
1129 builder.copy_link(
1130 &tool_result.tool_path,
1131 &libexec_path.join("rust-analyzer-proc-macro-srv"),
1132 FileType::Executable,
1133 );
1134
1135 tool_result
1136 }
1137
1138 fn metadata(&self) -> Option<StepMetadata> {
1139 Some(
1140 StepMetadata::build("rust-analyzer-proc-macro-srv", self.compilers.target())
1141 .built_by(self.compilers.build_compiler),
1142 )
1143 }
1144}
1145
1146#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1147pub struct LlvmBitcodeLinker {
1148 build_compiler: Compiler,
1149 target: TargetSelection,
1150}
1151
1152impl LlvmBitcodeLinker {
1153 pub fn from_build_compiler(build_compiler: Compiler, target: TargetSelection) -> Self {
1156 Self { build_compiler, target }
1157 }
1158
1159 pub fn from_target_compiler(builder: &Builder<'_>, target_compiler: Compiler) -> Self {
1161 Self {
1162 build_compiler: get_tool_target_compiler(
1163 builder,
1164 ToolTargetBuildMode::Dist(target_compiler),
1165 ),
1166 target: target_compiler.host,
1167 }
1168 }
1169
1170 pub fn get_build_compiler_for_target(
1172 builder: &Builder<'_>,
1173 target: TargetSelection,
1174 ) -> Compiler {
1175 get_tool_target_compiler(builder, ToolTargetBuildMode::Build(target))
1176 }
1177}
1178
1179impl Step for LlvmBitcodeLinker {
1180 type Output = ToolBuildResult;
1181 const DEFAULT: bool = true;
1182 const IS_HOST: bool = true;
1183
1184 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1185 let builder = run.builder;
1186 run.path("src/tools/llvm-bitcode-linker")
1187 .default_condition(builder.tool_enabled("llvm-bitcode-linker"))
1188 }
1189
1190 fn make_run(run: RunConfig<'_>) {
1191 run.builder.ensure(LlvmBitcodeLinker {
1192 build_compiler: Self::get_build_compiler_for_target(run.builder, run.target),
1193 target: run.target,
1194 });
1195 }
1196
1197 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1198 builder.ensure(ToolBuild {
1199 build_compiler: self.build_compiler,
1200 target: self.target,
1201 tool: "llvm-bitcode-linker",
1202 mode: Mode::ToolTarget,
1203 path: "src/tools/llvm-bitcode-linker",
1204 source_type: SourceType::InTree,
1205 extra_features: vec![],
1206 allow_features: "",
1207 cargo_args: Vec::new(),
1208 artifact_kind: ToolArtifactKind::Binary,
1209 })
1210 }
1211
1212 fn metadata(&self) -> Option<StepMetadata> {
1213 Some(StepMetadata::build("LlvmBitcodeLinker", self.target).built_by(self.build_compiler))
1214 }
1215}
1216
1217#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1218pub struct LibcxxVersionTool {
1219 pub target: TargetSelection,
1220}
1221
1222#[expect(dead_code)]
1223#[derive(Debug, Clone)]
1224pub enum LibcxxVersion {
1225 Gnu(usize),
1226 Llvm(usize),
1227}
1228
1229impl Step for LibcxxVersionTool {
1230 type Output = LibcxxVersion;
1231 const DEFAULT: bool = false;
1232 const IS_HOST: bool = true;
1233
1234 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1235 run.never()
1236 }
1237
1238 fn run(self, builder: &Builder<'_>) -> LibcxxVersion {
1239 let out_dir = builder.out.join(self.target.to_string()).join("libcxx-version");
1240 let executable = out_dir.join(exe("libcxx-version", self.target));
1241
1242 if !executable.exists() {
1247 if !out_dir.exists() {
1248 t!(fs::create_dir_all(&out_dir));
1249 }
1250
1251 let compiler = builder.cxx(self.target).unwrap();
1252 let mut cmd = command(compiler);
1253
1254 cmd.arg("-o")
1255 .arg(&executable)
1256 .arg(builder.src.join("src/tools/libcxx-version/main.cpp"));
1257
1258 cmd.run(builder);
1259
1260 if !executable.exists() {
1261 panic!("Something went wrong. {} is not present", executable.display());
1262 }
1263 }
1264
1265 let version_output = command(executable).run_capture_stdout(builder).stdout();
1266
1267 let version_str = version_output.split_once("version:").unwrap().1;
1268 let version = version_str.trim().parse::<usize>().unwrap();
1269
1270 if version_output.starts_with("libstdc++") {
1271 LibcxxVersion::Gnu(version)
1272 } else if version_output.starts_with("libc++") {
1273 LibcxxVersion::Llvm(version)
1274 } else {
1275 panic!("Coudln't recognize the standard library version.");
1276 }
1277 }
1278}
1279
1280#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
1292pub struct RustcPrivateCompilers {
1293 build_compiler: Compiler,
1295 target_compiler: Compiler,
1298}
1299
1300impl RustcPrivateCompilers {
1301 pub fn new(builder: &Builder<'_>, stage: u32, target: TargetSelection) -> Self {
1304 let build_compiler = Self::build_compiler_from_stage(builder, stage);
1305
1306 let target_compiler = builder.compiler(build_compiler.stage + 1, target);
1309
1310 Self { build_compiler, target_compiler }
1311 }
1312
1313 pub fn from_build_and_target_compiler(
1314 build_compiler: Compiler,
1315 target_compiler: Compiler,
1316 ) -> Self {
1317 Self { build_compiler, target_compiler }
1318 }
1319
1320 pub fn from_build_compiler(
1322 builder: &Builder<'_>,
1323 build_compiler: Compiler,
1324 target: TargetSelection,
1325 ) -> Self {
1326 let target_compiler = builder.compiler(build_compiler.stage + 1, target);
1327 Self { build_compiler, target_compiler }
1328 }
1329
1330 pub fn from_target_compiler(builder: &Builder<'_>, target_compiler: Compiler) -> Self {
1332 Self {
1333 build_compiler: Self::build_compiler_from_stage(builder, target_compiler.stage),
1334 target_compiler,
1335 }
1336 }
1337
1338 fn build_compiler_from_stage(builder: &Builder<'_>, stage: u32) -> Compiler {
1339 assert!(stage > 0);
1340
1341 if builder.download_rustc() && stage == 1 {
1342 builder.compiler(1, builder.config.host_target)
1344 } else {
1345 builder.compiler(stage - 1, builder.config.host_target)
1346 }
1347 }
1348
1349 pub fn build_compiler(&self) -> Compiler {
1350 self.build_compiler
1351 }
1352
1353 pub fn target_compiler(&self) -> Compiler {
1354 self.target_compiler
1355 }
1356
1357 pub fn target(&self) -> TargetSelection {
1359 self.target_compiler.host
1360 }
1361}
1362
1363macro_rules! tool_rustc_extended {
1366 (
1367 $name:ident {
1368 path: $path:expr,
1369 tool_name: $tool_name:expr,
1370 stable: $stable:expr
1371 $( , add_bins_to_sysroot: $add_bins_to_sysroot:expr )?
1372 $( , add_features: $add_features:expr )?
1373 $( , cargo_args: $cargo_args:expr )?
1374 $( , )?
1375 }
1376 ) => {
1377 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
1378 pub struct $name {
1379 compilers: RustcPrivateCompilers,
1380 }
1381
1382 impl $name {
1383 pub fn from_compilers(compilers: RustcPrivateCompilers) -> Self {
1384 Self {
1385 compilers,
1386 }
1387 }
1388 }
1389
1390 impl Step for $name {
1391 type Output = ToolBuildResult;
1392 const DEFAULT: bool = true; const IS_HOST: bool = true;
1394
1395 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1396 should_run_extended_rustc_tool(
1397 run,
1398 $tool_name,
1399 $path,
1400 $stable,
1401 )
1402 }
1403
1404 fn make_run(run: RunConfig<'_>) {
1405 run.builder.ensure($name {
1406 compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1407 });
1408 }
1409
1410 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1411 let Self { compilers } = self;
1412 build_extended_rustc_tool(
1413 builder,
1414 compilers,
1415 $tool_name,
1416 $path,
1417 None $( .or(Some(&$add_bins_to_sysroot)) )?,
1418 None $( .or(Some($add_features)) )?,
1419 None $( .or(Some($cargo_args)) )?,
1420 )
1421 }
1422
1423 fn metadata(&self) -> Option<StepMetadata> {
1424 Some(
1425 StepMetadata::build($tool_name, self.compilers.target())
1426 .built_by(self.compilers.build_compiler)
1427 )
1428 }
1429 }
1430 }
1431}
1432
1433fn should_run_extended_rustc_tool<'a>(
1434 run: ShouldRun<'a>,
1435 tool_name: &'static str,
1436 path: &'static str,
1437 stable: bool,
1438) -> ShouldRun<'a> {
1439 let builder = run.builder;
1440 run.path(path).default_condition(
1441 builder.config.extended
1442 && builder.config.tools.as_ref().map_or(
1443 stable || builder.build.unstable_features(),
1446 |tools| {
1448 tools.iter().any(|tool| match tool.as_ref() {
1449 "clippy" => tool_name == "clippy-driver",
1450 x => tool_name == x,
1451 })
1452 },
1453 ),
1454 )
1455}
1456
1457fn build_extended_rustc_tool(
1458 builder: &Builder<'_>,
1459 compilers: RustcPrivateCompilers,
1460 tool_name: &'static str,
1461 path: &'static str,
1462 add_bins_to_sysroot: Option<&[&str]>,
1463 add_features: Option<fn(&Builder<'_>, TargetSelection, &mut Vec<String>)>,
1464 cargo_args: Option<&[&'static str]>,
1465) -> ToolBuildResult {
1466 let target = compilers.target();
1467 let mut extra_features = Vec::new();
1468 if let Some(func) = add_features {
1469 func(builder, target, &mut extra_features);
1470 }
1471
1472 let build_compiler = compilers.build_compiler;
1473 let ToolBuildResult { tool_path, .. } = builder.ensure(ToolBuild {
1474 build_compiler,
1475 target,
1476 tool: tool_name,
1477 mode: Mode::ToolRustcPrivate,
1478 path,
1479 extra_features,
1480 source_type: SourceType::InTree,
1481 allow_features: "",
1482 cargo_args: cargo_args.unwrap_or_default().iter().map(|s| String::from(*s)).collect(),
1483 artifact_kind: ToolArtifactKind::Binary,
1484 });
1485
1486 let target_compiler = compilers.target_compiler;
1487 if let Some(add_bins_to_sysroot) = add_bins_to_sysroot
1488 && !add_bins_to_sysroot.is_empty()
1489 {
1490 let bindir = builder.sysroot(target_compiler).join("bin");
1491 t!(fs::create_dir_all(&bindir));
1492
1493 for add_bin in add_bins_to_sysroot {
1494 let bin_destination = bindir.join(exe(add_bin, target_compiler.host));
1495 builder.copy_link(&tool_path, &bin_destination, FileType::Executable);
1496 }
1497
1498 let path = bindir.join(exe(tool_name, target_compiler.host));
1500 ToolBuildResult { tool_path: path, build_compiler }
1501 } else {
1502 ToolBuildResult { tool_path, build_compiler }
1503 }
1504}
1505
1506tool_rustc_extended!(Cargofmt {
1507 path: "src/tools/rustfmt",
1508 tool_name: "cargo-fmt",
1509 stable: true,
1510 add_bins_to_sysroot: ["cargo-fmt"]
1511});
1512tool_rustc_extended!(CargoClippy {
1513 path: "src/tools/clippy",
1514 tool_name: "cargo-clippy",
1515 stable: true,
1516 add_bins_to_sysroot: ["cargo-clippy"]
1517});
1518tool_rustc_extended!(Clippy {
1519 path: "src/tools/clippy",
1520 tool_name: "clippy-driver",
1521 stable: true,
1522 add_bins_to_sysroot: ["clippy-driver"],
1523 add_features: |builder, target, features| {
1524 if builder.config.jemalloc(target) {
1525 features.push("jemalloc".to_string());
1526 }
1527 }
1528});
1529tool_rustc_extended!(Miri {
1530 path: "src/tools/miri",
1531 tool_name: "miri",
1532 stable: false,
1533 add_bins_to_sysroot: ["miri"],
1534 cargo_args: &["--all-targets"],
1536});
1537tool_rustc_extended!(CargoMiri {
1538 path: "src/tools/miri/cargo-miri",
1539 tool_name: "cargo-miri",
1540 stable: false,
1541 add_bins_to_sysroot: ["cargo-miri"]
1542});
1543tool_rustc_extended!(Rustfmt {
1544 path: "src/tools/rustfmt",
1545 tool_name: "rustfmt",
1546 stable: true,
1547 add_bins_to_sysroot: ["rustfmt"]
1548});
1549
1550pub const TEST_FLOAT_PARSE_ALLOW_FEATURES: &str = "f16,cfg_target_has_reliable_f16_f128";
1551
1552impl Builder<'_> {
1553 pub fn tool_cmd(&self, tool: Tool) -> BootstrapCommand {
1556 let mut cmd = command(self.tool_exe(tool));
1557 let compiler = self.compiler(0, self.config.host_target);
1558 let host = &compiler.host;
1559 let mut lib_paths: Vec<PathBuf> =
1564 vec![self.cargo_out(compiler, Mode::ToolBootstrap, *host).join("deps")];
1565
1566 if compiler.host.is_msvc() {
1570 let curpaths = env::var_os("PATH").unwrap_or_default();
1571 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
1572 for (k, v) in self.cc[&compiler.host].env() {
1573 if k != "PATH" {
1574 continue;
1575 }
1576 for path in env::split_paths(v) {
1577 if !curpaths.contains(&path) {
1578 lib_paths.push(path);
1579 }
1580 }
1581 }
1582 }
1583
1584 add_dylib_path(lib_paths, &mut cmd);
1585
1586 cmd.env("RUSTC", &self.initial_rustc);
1588
1589 cmd
1590 }
1591}