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 target.starts_with("aarch64") && env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none() {
236 cargo.env("JEMALLOC_SYS_WITH_LG_PAGE", "16");
237 }
238
239 cargo.env("CFG_RELEASE", builder.rust_release());
243 cargo.env("CFG_RELEASE_CHANNEL", &builder.config.channel);
244 cargo.env("CFG_VERSION", builder.rust_version());
245 cargo.env("CFG_RELEASE_NUM", &builder.version);
246 cargo.env("DOC_RUST_LANG_ORG_CHANNEL", builder.doc_rust_lang_org_channel());
247
248 if let Some(ref ver_date) = builder.rust_info().commit_date() {
249 cargo.env("CFG_VER_DATE", ver_date);
250 }
251
252 if let Some(ref ver_hash) = builder.rust_info().sha() {
253 cargo.env("CFG_VER_HASH", ver_hash);
254 }
255
256 if let Some(description) = &builder.config.description {
257 cargo.env("CFG_VER_DESCRIPTION", description);
258 }
259
260 let info = builder.config.git_info(builder.config.omit_git_hash, &dir);
261 if let Some(sha) = info.sha() {
262 cargo.env("CFG_COMMIT_HASH", sha);
263 }
264
265 if let Some(sha_short) = info.sha_short() {
266 cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
267 }
268
269 if let Some(date) = info.commit_date() {
270 cargo.env("CFG_COMMIT_DATE", date);
271 }
272
273 if !features.is_empty() {
274 cargo.arg("--features").arg(features.join(", "));
275 }
276
277 cargo.rustflag("-Zunstable-options");
285
286 if !path.ends_with("cargo") {
303 cargo.env("FORCE_ON_BROKEN_PIPE_KILL", "-Zon-broken-pipe=kill");
308 }
309
310 cargo
311}
312
313pub enum ToolTargetBuildMode {
316 Build(TargetSelection),
319 Dist(Compiler),
323}
324
325pub(crate) fn get_tool_target_compiler(
327 builder: &Builder<'_>,
328 mode: ToolTargetBuildMode,
329) -> Compiler {
330 let (target, build_compiler_stage) = match mode {
331 ToolTargetBuildMode::Build(target) => {
332 assert!(builder.top_stage > 0);
333 (target, builder.top_stage - 1)
335 }
336 ToolTargetBuildMode::Dist(target_compiler) => {
337 assert!(target_compiler.stage > 0);
338 (target_compiler.host, target_compiler.stage - 1)
341 }
342 };
343
344 let compiler = if builder.host_target == target {
345 builder.compiler(build_compiler_stage, builder.host_target)
346 } else {
347 let build_compiler = builder.compiler(build_compiler_stage.max(1), builder.host_target);
350 builder.std(build_compiler, builder.host_target);
352 build_compiler
353 };
354 builder.std(compiler, target);
355 compiler
356}
357
358fn copy_link_tool_bin(
361 builder: &Builder<'_>,
362 build_compiler: Compiler,
363 target: TargetSelection,
364 mode: Mode,
365 name: &str,
366) -> PathBuf {
367 let cargo_out = builder.cargo_out(build_compiler, mode, target).join(exe(name, target));
368 let bin = builder.tools_dir(build_compiler).join(exe(name, target));
369 builder.copy_link(&cargo_out, &bin, FileType::Executable);
370 bin
371}
372
373macro_rules! bootstrap_tool {
374 ($(
375 $name:ident, $path:expr, $tool_name:expr
376 $(,is_external_tool = $external:expr)*
377 $(,is_unstable_tool = $unstable:expr)*
378 $(,allow_features = $allow_features:expr)?
379 $(,submodules = $submodules:expr)?
380 $(,artifact_kind = $artifact_kind:expr)?
381 ;
382 )+) => {
383 #[derive(PartialEq, Eq, Clone)]
384 pub enum Tool {
385 $(
386 $name,
387 )+
388 }
389
390 impl<'a> Builder<'a> {
391 pub fn tool_exe(&self, tool: Tool) -> PathBuf {
392 match tool {
393 $(Tool::$name =>
394 self.ensure($name {
395 compiler: self.compiler(0, self.config.host_target),
396 target: self.config.host_target,
397 }).tool_path,
398 )+
399 }
400 }
401 }
402
403 $(
404 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
405 pub struct $name {
406 pub compiler: Compiler,
407 pub target: TargetSelection,
408 }
409
410 impl Step for $name {
411 type Output = ToolBuildResult;
412
413 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
414 run.path($path)
415 }
416
417 fn make_run(run: RunConfig<'_>) {
418 run.builder.ensure($name {
419 compiler: run.builder.compiler(0, run.builder.config.host_target),
421 target: run.target,
422 });
423 }
424
425 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
426 $(
427 for submodule in $submodules {
428 builder.require_submodule(submodule, None);
429 }
430 )*
431
432 let is_unstable = false $(|| $unstable)*;
433 let compiletest_wants_stage0 = $tool_name == "compiletest" && builder.config.compiletest_use_stage0_libtest;
434
435 builder.ensure(ToolBuild {
436 build_compiler: self.compiler,
437 target: self.target,
438 tool: $tool_name,
439 mode: if is_unstable && !compiletest_wants_stage0 {
440 Mode::ToolStd
442 } else {
443 Mode::ToolBootstrap
444 },
445 path: $path,
446 source_type: if false $(|| $external)* {
447 SourceType::Submodule
448 } else {
449 SourceType::InTree
450 },
451 extra_features: vec![],
452 allow_features: {
453 let mut _value = "";
454 $( _value = $allow_features; )?
455 _value
456 },
457 cargo_args: vec![],
458 artifact_kind: if false $(|| $artifact_kind == ToolArtifactKind::Library)* {
459 ToolArtifactKind::Library
460 } else {
461 ToolArtifactKind::Binary
462 }
463 })
464 }
465
466 fn metadata(&self) -> Option<StepMetadata> {
467 Some(
468 StepMetadata::build(stringify!($name), self.target)
469 .built_by(self.compiler)
470 )
471 }
472 }
473 )+
474 }
475}
476
477pub(crate) const COMPILETEST_ALLOW_FEATURES: &str = "internal_output_capture";
478
479bootstrap_tool!(
480 Rustbook, "src/tools/rustbook", "rustbook", is_external_tool = true, submodules = SUBMODULES_FOR_RUSTBOOK;
485 UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen";
486 Tidy, "src/tools/tidy", "tidy";
487 Linkchecker, "src/tools/linkchecker", "linkchecker";
488 CargoTest, "src/tools/cargotest", "cargotest";
489 Compiletest, "src/tools/compiletest", "compiletest", is_unstable_tool = true, allow_features = COMPILETEST_ALLOW_FEATURES;
490 BuildManifest, "src/tools/build-manifest", "build-manifest";
491 RemoteTestClient, "src/tools/remote-test-client", "remote-test-client";
492 RustInstaller, "src/tools/rust-installer", "rust-installer";
493 RustdocTheme, "src/tools/rustdoc-themes", "rustdoc-themes";
494 LintDocs, "src/tools/lint-docs", "lint-docs";
495 JsonDocCk, "src/tools/jsondocck", "jsondocck";
496 JsonDocLint, "src/tools/jsondoclint", "jsondoclint";
497 HtmlChecker, "src/tools/html-checker", "html-checker";
498 BumpStage0, "src/tools/bump-stage0", "bump-stage0";
499 ReplaceVersionPlaceholder, "src/tools/replace-version-placeholder", "replace-version-placeholder";
500 CollectLicenseMetadata, "src/tools/collect-license-metadata", "collect-license-metadata";
501 GenerateCopyright, "src/tools/generate-copyright", "generate-copyright";
502 GenerateWindowsSys, "src/tools/generate-windows-sys", "generate-windows-sys";
503 RustdocGUITest, "src/tools/rustdoc-gui-test", "rustdoc-gui-test", is_unstable_tool = true, allow_features = COMPILETEST_ALLOW_FEATURES;
505 CoverageDump, "src/tools/coverage-dump", "coverage-dump";
506 UnicodeTableGenerator, "src/tools/unicode-table-generator", "unicode-table-generator";
507 FeaturesStatusDump, "src/tools/features-status-dump", "features-status-dump";
508 OptimizedDist, "src/tools/opt-dist", "opt-dist", submodules = &["src/tools/rustc-perf"];
509 RunMakeSupport, "src/tools/run-make-support", "run_make_support", artifact_kind = ToolArtifactKind::Library;
510);
511
512pub static SUBMODULES_FOR_RUSTBOOK: &[&str] = &["src/doc/book", "src/doc/reference"];
515
516#[derive(Debug, Clone, Hash, PartialEq, Eq)]
519pub struct RustcPerf {
520 pub compiler: Compiler,
521 pub target: TargetSelection,
522}
523
524impl Step for RustcPerf {
525 type Output = ToolBuildResult;
527
528 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
529 run.path("src/tools/rustc-perf")
530 }
531
532 fn make_run(run: RunConfig<'_>) {
533 run.builder.ensure(RustcPerf {
534 compiler: run.builder.compiler(0, run.builder.config.host_target),
535 target: run.target,
536 });
537 }
538
539 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
540 builder.require_submodule("src/tools/rustc-perf", None);
542
543 let tool = ToolBuild {
544 build_compiler: self.compiler,
545 target: self.target,
546 tool: "collector",
547 mode: Mode::ToolBootstrap,
548 path: "src/tools/rustc-perf",
549 source_type: SourceType::Submodule,
550 extra_features: Vec::new(),
551 allow_features: "",
552 cargo_args: vec!["-p".to_string(), "collector".to_string()],
555 artifact_kind: ToolArtifactKind::Binary,
556 };
557 let res = builder.ensure(tool.clone());
558 copy_link_tool_bin(builder, tool.build_compiler, tool.target, tool.mode, "rustc-fake");
561
562 res
563 }
564}
565
566#[derive(Debug, Clone, Hash, PartialEq, Eq)]
567pub struct ErrorIndex {
568 compilers: RustcPrivateCompilers,
569}
570
571impl ErrorIndex {
572 pub fn command(builder: &Builder<'_>, compilers: RustcPrivateCompilers) -> BootstrapCommand {
573 let mut cmd = command(builder.ensure(ErrorIndex { compilers }).tool_path);
576
577 let target_compiler = compilers.target_compiler();
578 let mut dylib_paths = builder.rustc_lib_paths(target_compiler);
579 dylib_paths.push(builder.sysroot_target_libdir(target_compiler, target_compiler.host));
580 add_dylib_path(dylib_paths, &mut cmd);
581 cmd
582 }
583}
584
585impl Step for ErrorIndex {
586 type Output = ToolBuildResult;
587
588 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
589 run.path("src/tools/error_index_generator")
590 }
591
592 fn make_run(run: RunConfig<'_>) {
593 run.builder.ensure(ErrorIndex {
599 compilers: RustcPrivateCompilers::new(
600 run.builder,
601 run.builder.top_stage,
602 run.builder.host_target,
603 ),
604 });
605 }
606
607 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
608 builder.ensure(ToolBuild {
609 build_compiler: self.compilers.build_compiler,
610 target: self.compilers.target(),
611 tool: "error_index_generator",
612 mode: Mode::ToolRustcPrivate,
613 path: "src/tools/error_index_generator",
614 source_type: SourceType::InTree,
615 extra_features: Vec::new(),
616 allow_features: "",
617 cargo_args: Vec::new(),
618 artifact_kind: ToolArtifactKind::Binary,
619 })
620 }
621
622 fn metadata(&self) -> Option<StepMetadata> {
623 Some(
624 StepMetadata::build("error-index", self.compilers.target())
625 .built_by(self.compilers.build_compiler),
626 )
627 }
628}
629
630#[derive(Debug, Clone, Hash, PartialEq, Eq)]
631pub struct RemoteTestServer {
632 pub build_compiler: Compiler,
633 pub target: TargetSelection,
634}
635
636impl Step for RemoteTestServer {
637 type Output = ToolBuildResult;
638
639 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
640 run.path("src/tools/remote-test-server")
641 }
642
643 fn make_run(run: RunConfig<'_>) {
644 run.builder.ensure(RemoteTestServer {
645 build_compiler: get_tool_target_compiler(
646 run.builder,
647 ToolTargetBuildMode::Build(run.target),
648 ),
649 target: run.target,
650 });
651 }
652
653 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
654 builder.ensure(ToolBuild {
655 build_compiler: self.build_compiler,
656 target: self.target,
657 tool: "remote-test-server",
658 mode: Mode::ToolTarget,
659 path: "src/tools/remote-test-server",
660 source_type: SourceType::InTree,
661 extra_features: Vec::new(),
662 allow_features: "",
663 cargo_args: Vec::new(),
664 artifact_kind: ToolArtifactKind::Binary,
665 })
666 }
667
668 fn metadata(&self) -> Option<StepMetadata> {
669 Some(StepMetadata::build("remote-test-server", self.target).built_by(self.build_compiler))
670 }
671}
672
673#[derive(Debug, Clone, Hash, PartialEq, Eq)]
678pub struct Rustdoc {
679 pub target_compiler: Compiler,
682}
683
684impl Step for Rustdoc {
685 type Output = PathBuf;
687
688 const DEFAULT: bool = true;
689 const IS_HOST: bool = true;
690
691 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
692 run.path("src/tools/rustdoc").path("src/librustdoc")
693 }
694
695 fn make_run(run: RunConfig<'_>) {
696 run.builder.ensure(Rustdoc {
697 target_compiler: run.builder.compiler(run.builder.top_stage, run.target),
698 });
699 }
700
701 fn run(self, builder: &Builder<'_>) -> Self::Output {
702 let target_compiler = self.target_compiler;
703 let target = target_compiler.host;
704
705 if target_compiler.stage == 0 {
707 if !target_compiler.is_snapshot(builder) {
708 panic!("rustdoc in stage 0 must be snapshot rustdoc");
709 }
710
711 return builder.initial_rustdoc.clone();
712 }
713
714 let bin_rustdoc = || {
716 let sysroot = builder.sysroot(target_compiler);
717 let bindir = sysroot.join("bin");
718 t!(fs::create_dir_all(&bindir));
719 let bin_rustdoc = bindir.join(exe("rustdoc", target_compiler.host));
720 let _ = fs::remove_file(&bin_rustdoc);
721 bin_rustdoc
722 };
723
724 if builder.download_rustc() && builder.rust_info().is_managed_git_subrepository() {
727 let files_to_track = &["src/librustdoc", "src/tools/rustdoc", "src/rustdoc-json-types"];
728
729 if !builder.config.has_changes_from_upstream(files_to_track) {
731 let precompiled_rustdoc = builder
732 .config
733 .ci_rustc_dir()
734 .join("bin")
735 .join(exe("rustdoc", target_compiler.host));
736
737 let bin_rustdoc = bin_rustdoc();
738 builder.copy_link(&precompiled_rustdoc, &bin_rustdoc, FileType::Executable);
739 return bin_rustdoc;
740 }
741 }
742
743 let mut extra_features = Vec::new();
751 if builder.config.jemalloc(target) {
752 extra_features.push("jemalloc".to_string());
753 }
754
755 let compilers = RustcPrivateCompilers::from_target_compiler(builder, target_compiler);
756 let tool_path = builder
757 .ensure(ToolBuild {
758 build_compiler: compilers.build_compiler,
759 target,
760 tool: "rustdoc_tool_binary",
764 mode: Mode::ToolRustcPrivate,
765 path: "src/tools/rustdoc",
766 source_type: SourceType::InTree,
767 extra_features,
768 allow_features: "",
769 cargo_args: Vec::new(),
770 artifact_kind: ToolArtifactKind::Binary,
771 })
772 .tool_path;
773
774 if builder.config.rust_debuginfo_level_tools == DebuginfoLevel::None {
775 compile::strip_debug(builder, target, &tool_path);
778 }
779 let bin_rustdoc = bin_rustdoc();
780 builder.copy_link(&tool_path, &bin_rustdoc, FileType::Executable);
781 bin_rustdoc
782 }
783
784 fn metadata(&self) -> Option<StepMetadata> {
785 Some(
786 StepMetadata::build("rustdoc", self.target_compiler.host)
787 .stage(self.target_compiler.stage),
788 )
789 }
790}
791
792#[derive(Debug, Clone, Hash, PartialEq, Eq)]
795pub struct Cargo {
796 build_compiler: Compiler,
797 target: TargetSelection,
798}
799
800impl Cargo {
801 pub fn from_build_compiler(build_compiler: Compiler, target: TargetSelection) -> Self {
804 Self { build_compiler, target }
805 }
806}
807
808impl Step for Cargo {
809 type Output = ToolBuildResult;
810 const DEFAULT: bool = true;
811 const IS_HOST: bool = true;
812
813 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
814 let builder = run.builder;
815 run.path("src/tools/cargo").default_condition(builder.tool_enabled("cargo"))
816 }
817
818 fn make_run(run: RunConfig<'_>) {
819 run.builder.ensure(Cargo {
820 build_compiler: get_tool_target_compiler(
821 run.builder,
822 ToolTargetBuildMode::Build(run.target),
823 ),
824 target: run.target,
825 });
826 }
827
828 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
829 builder.build.require_submodule("src/tools/cargo", None);
830
831 builder.std(self.build_compiler, builder.host_target);
832 builder.std(self.build_compiler, self.target);
833
834 builder.ensure(ToolBuild {
835 build_compiler: self.build_compiler,
836 target: self.target,
837 tool: "cargo",
838 mode: Mode::ToolTarget,
839 path: "src/tools/cargo",
840 source_type: SourceType::Submodule,
841 extra_features: Vec::new(),
842 allow_features: "min_specialization,specialization",
847 cargo_args: Vec::new(),
848 artifact_kind: ToolArtifactKind::Binary,
849 })
850 }
851
852 fn metadata(&self) -> Option<StepMetadata> {
853 Some(StepMetadata::build("cargo", self.target).built_by(self.build_compiler))
854 }
855}
856
857#[derive(Clone)]
860pub struct BuiltLldWrapper {
861 tool: ToolBuildResult,
862 lld_dir: PathBuf,
863}
864
865#[derive(Debug, Clone, Hash, PartialEq, Eq)]
866pub struct LldWrapper {
867 pub build_compiler: Compiler,
868 pub target: TargetSelection,
869}
870
871impl LldWrapper {
872 pub fn for_use_by_compiler(builder: &Builder<'_>, target_compiler: Compiler) -> Self {
874 Self {
875 build_compiler: get_tool_target_compiler(
876 builder,
877 ToolTargetBuildMode::Dist(target_compiler),
878 ),
879 target: target_compiler.host,
880 }
881 }
882}
883
884impl Step for LldWrapper {
885 type Output = BuiltLldWrapper;
886
887 const IS_HOST: bool = true;
888
889 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
890 run.path("src/tools/lld-wrapper")
891 }
892
893 fn make_run(run: RunConfig<'_>) {
894 run.builder.ensure(LldWrapper {
895 build_compiler: get_tool_target_compiler(
896 run.builder,
897 ToolTargetBuildMode::Build(run.target),
898 ),
899 target: run.target,
900 });
901 }
902
903 fn run(self, builder: &Builder<'_>) -> Self::Output {
904 let lld_dir = builder.ensure(llvm::Lld { target: self.target });
905 let tool = builder.ensure(ToolBuild {
906 build_compiler: self.build_compiler,
907 target: self.target,
908 tool: "lld-wrapper",
909 mode: Mode::ToolTarget,
910 path: "src/tools/lld-wrapper",
911 source_type: SourceType::InTree,
912 extra_features: Vec::new(),
913 allow_features: "",
914 cargo_args: Vec::new(),
915 artifact_kind: ToolArtifactKind::Binary,
916 });
917 BuiltLldWrapper { tool, lld_dir }
918 }
919
920 fn metadata(&self) -> Option<StepMetadata> {
921 Some(StepMetadata::build("LldWrapper", self.target).built_by(self.build_compiler))
922 }
923}
924
925pub(crate) fn copy_lld_artifacts(
926 builder: &Builder<'_>,
927 lld_wrapper: BuiltLldWrapper,
928 target_compiler: Compiler,
929) {
930 let target = target_compiler.host;
931
932 let libdir_bin = builder.sysroot_target_bindir(target_compiler, target);
933 t!(fs::create_dir_all(&libdir_bin));
934
935 let src_exe = exe("lld", target);
936 let dst_exe = exe("rust-lld", target);
937
938 builder.copy_link(
939 &lld_wrapper.lld_dir.join("bin").join(src_exe),
940 &libdir_bin.join(dst_exe),
941 FileType::Executable,
942 );
943 let self_contained_lld_dir = libdir_bin.join("gcc-ld");
944 t!(fs::create_dir_all(&self_contained_lld_dir));
945
946 for name in crate::LLD_FILE_NAMES {
947 builder.copy_link(
948 &lld_wrapper.tool.tool_path,
949 &self_contained_lld_dir.join(exe(name, target)),
950 FileType::Executable,
951 );
952 }
953}
954
955#[derive(Debug, Clone, Hash, PartialEq, Eq)]
958pub struct WasmComponentLd {
959 build_compiler: Compiler,
960 target: TargetSelection,
961}
962
963impl WasmComponentLd {
964 pub fn for_use_by_compiler(builder: &Builder<'_>, target_compiler: Compiler) -> Self {
966 Self {
967 build_compiler: get_tool_target_compiler(
968 builder,
969 ToolTargetBuildMode::Dist(target_compiler),
970 ),
971 target: target_compiler.host,
972 }
973 }
974}
975
976impl Step for WasmComponentLd {
977 type Output = ToolBuildResult;
978
979 const IS_HOST: bool = true;
980
981 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
982 run.path("src/tools/wasm-component-ld")
983 }
984
985 fn make_run(run: RunConfig<'_>) {
986 run.builder.ensure(WasmComponentLd {
987 build_compiler: get_tool_target_compiler(
988 run.builder,
989 ToolTargetBuildMode::Build(run.target),
990 ),
991 target: run.target,
992 });
993 }
994
995 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
996 builder.ensure(ToolBuild {
997 build_compiler: self.build_compiler,
998 target: self.target,
999 tool: "wasm-component-ld",
1000 mode: Mode::ToolTarget,
1001 path: "src/tools/wasm-component-ld",
1002 source_type: SourceType::InTree,
1003 extra_features: vec![],
1004 allow_features: "",
1005 cargo_args: vec![],
1006 artifact_kind: ToolArtifactKind::Binary,
1007 })
1008 }
1009
1010 fn metadata(&self) -> Option<StepMetadata> {
1011 Some(StepMetadata::build("WasmComponentLd", self.target).built_by(self.build_compiler))
1012 }
1013}
1014
1015#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1016pub struct RustAnalyzer {
1017 compilers: RustcPrivateCompilers,
1018}
1019
1020impl RustAnalyzer {
1021 pub fn from_compilers(compilers: RustcPrivateCompilers) -> Self {
1022 Self { compilers }
1023 }
1024}
1025
1026impl RustAnalyzer {
1027 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";
1028}
1029
1030impl Step for RustAnalyzer {
1031 type Output = ToolBuildResult;
1032 const DEFAULT: bool = true;
1033 const IS_HOST: bool = true;
1034
1035 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1036 let builder = run.builder;
1037 run.path("src/tools/rust-analyzer").default_condition(builder.tool_enabled("rust-analyzer"))
1038 }
1039
1040 fn make_run(run: RunConfig<'_>) {
1041 run.builder.ensure(RustAnalyzer {
1042 compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1043 });
1044 }
1045
1046 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1047 let build_compiler = self.compilers.build_compiler;
1048 let target = self.compilers.target();
1049 builder.ensure(ToolBuild {
1050 build_compiler,
1051 target,
1052 tool: "rust-analyzer",
1053 mode: Mode::ToolRustcPrivate,
1054 path: "src/tools/rust-analyzer",
1055 extra_features: vec!["in-rust-tree".to_owned()],
1056 source_type: SourceType::InTree,
1057 allow_features: RustAnalyzer::ALLOW_FEATURES,
1058 cargo_args: Vec::new(),
1059 artifact_kind: ToolArtifactKind::Binary,
1060 })
1061 }
1062
1063 fn metadata(&self) -> Option<StepMetadata> {
1064 Some(
1065 StepMetadata::build("rust-analyzer", self.compilers.target())
1066 .built_by(self.compilers.build_compiler),
1067 )
1068 }
1069}
1070
1071#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1072pub struct RustAnalyzerProcMacroSrv {
1073 compilers: RustcPrivateCompilers,
1074}
1075
1076impl RustAnalyzerProcMacroSrv {
1077 pub fn from_compilers(compilers: RustcPrivateCompilers) -> Self {
1078 Self { compilers }
1079 }
1080}
1081
1082impl Step for RustAnalyzerProcMacroSrv {
1083 type Output = ToolBuildResult;
1084
1085 const DEFAULT: bool = true;
1086 const IS_HOST: bool = true;
1087
1088 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1089 let builder = run.builder;
1090 run.path("src/tools/rust-analyzer")
1092 .path("src/tools/rust-analyzer/crates/proc-macro-srv-cli")
1093 .default_condition(
1094 builder.tool_enabled("rust-analyzer")
1095 || builder.tool_enabled("rust-analyzer-proc-macro-srv"),
1096 )
1097 }
1098
1099 fn make_run(run: RunConfig<'_>) {
1100 run.builder.ensure(RustAnalyzerProcMacroSrv {
1101 compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1102 });
1103 }
1104
1105 fn run(self, builder: &Builder<'_>) -> Self::Output {
1106 let tool_result = builder.ensure(ToolBuild {
1107 build_compiler: self.compilers.build_compiler,
1108 target: self.compilers.target(),
1109 tool: "rust-analyzer-proc-macro-srv",
1110 mode: Mode::ToolRustcPrivate,
1111 path: "src/tools/rust-analyzer/crates/proc-macro-srv-cli",
1112 extra_features: vec!["in-rust-tree".to_owned()],
1113 source_type: SourceType::InTree,
1114 allow_features: RustAnalyzer::ALLOW_FEATURES,
1115 cargo_args: Vec::new(),
1116 artifact_kind: ToolArtifactKind::Binary,
1117 });
1118
1119 let libexec_path = builder.sysroot(self.compilers.target_compiler).join("libexec");
1122 t!(fs::create_dir_all(&libexec_path));
1123 builder.copy_link(
1124 &tool_result.tool_path,
1125 &libexec_path.join("rust-analyzer-proc-macro-srv"),
1126 FileType::Executable,
1127 );
1128
1129 tool_result
1130 }
1131
1132 fn metadata(&self) -> Option<StepMetadata> {
1133 Some(
1134 StepMetadata::build("rust-analyzer-proc-macro-srv", self.compilers.target())
1135 .built_by(self.compilers.build_compiler),
1136 )
1137 }
1138}
1139
1140#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1141pub struct LlvmBitcodeLinker {
1142 build_compiler: Compiler,
1143 target: TargetSelection,
1144}
1145
1146impl LlvmBitcodeLinker {
1147 pub fn from_build_compiler(build_compiler: Compiler, target: TargetSelection) -> Self {
1150 Self { build_compiler, target }
1151 }
1152
1153 pub fn from_target_compiler(builder: &Builder<'_>, target_compiler: Compiler) -> Self {
1155 Self {
1156 build_compiler: get_tool_target_compiler(
1157 builder,
1158 ToolTargetBuildMode::Dist(target_compiler),
1159 ),
1160 target: target_compiler.host,
1161 }
1162 }
1163
1164 pub fn get_build_compiler_for_target(
1166 builder: &Builder<'_>,
1167 target: TargetSelection,
1168 ) -> Compiler {
1169 get_tool_target_compiler(builder, ToolTargetBuildMode::Build(target))
1170 }
1171}
1172
1173impl Step for LlvmBitcodeLinker {
1174 type Output = ToolBuildResult;
1175 const DEFAULT: bool = true;
1176 const IS_HOST: bool = true;
1177
1178 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1179 let builder = run.builder;
1180 run.path("src/tools/llvm-bitcode-linker")
1181 .default_condition(builder.tool_enabled("llvm-bitcode-linker"))
1182 }
1183
1184 fn make_run(run: RunConfig<'_>) {
1185 run.builder.ensure(LlvmBitcodeLinker {
1186 build_compiler: Self::get_build_compiler_for_target(run.builder, run.target),
1187 target: run.target,
1188 });
1189 }
1190
1191 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1192 builder.ensure(ToolBuild {
1193 build_compiler: self.build_compiler,
1194 target: self.target,
1195 tool: "llvm-bitcode-linker",
1196 mode: Mode::ToolTarget,
1197 path: "src/tools/llvm-bitcode-linker",
1198 source_type: SourceType::InTree,
1199 extra_features: vec![],
1200 allow_features: "",
1201 cargo_args: Vec::new(),
1202 artifact_kind: ToolArtifactKind::Binary,
1203 })
1204 }
1205
1206 fn metadata(&self) -> Option<StepMetadata> {
1207 Some(StepMetadata::build("LlvmBitcodeLinker", self.target).built_by(self.build_compiler))
1208 }
1209}
1210
1211#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1212pub struct LibcxxVersionTool {
1213 pub target: TargetSelection,
1214}
1215
1216#[expect(dead_code)]
1217#[derive(Debug, Clone)]
1218pub enum LibcxxVersion {
1219 Gnu(usize),
1220 Llvm(usize),
1221}
1222
1223impl Step for LibcxxVersionTool {
1224 type Output = LibcxxVersion;
1225 const DEFAULT: bool = false;
1226 const IS_HOST: bool = true;
1227
1228 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1229 run.never()
1230 }
1231
1232 fn run(self, builder: &Builder<'_>) -> LibcxxVersion {
1233 let out_dir = builder.out.join(self.target.to_string()).join("libcxx-version");
1234 let executable = out_dir.join(exe("libcxx-version", self.target));
1235
1236 if !executable.exists() {
1241 if !out_dir.exists() {
1242 t!(fs::create_dir_all(&out_dir));
1243 }
1244
1245 let compiler = builder.cxx(self.target).unwrap();
1246 let mut cmd = command(compiler);
1247
1248 cmd.arg("-o")
1249 .arg(&executable)
1250 .arg(builder.src.join("src/tools/libcxx-version/main.cpp"));
1251
1252 cmd.run(builder);
1253
1254 if !executable.exists() {
1255 panic!("Something went wrong. {} is not present", executable.display());
1256 }
1257 }
1258
1259 let version_output = command(executable).run_capture_stdout(builder).stdout();
1260
1261 let version_str = version_output.split_once("version:").unwrap().1;
1262 let version = version_str.trim().parse::<usize>().unwrap();
1263
1264 if version_output.starts_with("libstdc++") {
1265 LibcxxVersion::Gnu(version)
1266 } else if version_output.starts_with("libc++") {
1267 LibcxxVersion::Llvm(version)
1268 } else {
1269 panic!("Coudln't recognize the standard library version.");
1270 }
1271 }
1272}
1273
1274#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
1286pub struct RustcPrivateCompilers {
1287 build_compiler: Compiler,
1289 target_compiler: Compiler,
1292}
1293
1294impl RustcPrivateCompilers {
1295 pub fn new(builder: &Builder<'_>, stage: u32, target: TargetSelection) -> Self {
1298 let build_compiler = Self::build_compiler_from_stage(builder, stage);
1299
1300 let target_compiler = builder.compiler(build_compiler.stage + 1, target);
1303
1304 Self { build_compiler, target_compiler }
1305 }
1306
1307 pub fn from_build_and_target_compiler(
1308 build_compiler: Compiler,
1309 target_compiler: Compiler,
1310 ) -> Self {
1311 Self { build_compiler, target_compiler }
1312 }
1313
1314 pub fn from_build_compiler(
1316 builder: &Builder<'_>,
1317 build_compiler: Compiler,
1318 target: TargetSelection,
1319 ) -> Self {
1320 let target_compiler = builder.compiler(build_compiler.stage + 1, target);
1321 Self { build_compiler, target_compiler }
1322 }
1323
1324 pub fn from_target_compiler(builder: &Builder<'_>, target_compiler: Compiler) -> Self {
1326 Self {
1327 build_compiler: Self::build_compiler_from_stage(builder, target_compiler.stage),
1328 target_compiler,
1329 }
1330 }
1331
1332 fn build_compiler_from_stage(builder: &Builder<'_>, stage: u32) -> Compiler {
1333 assert!(stage > 0);
1334
1335 if builder.download_rustc() && stage == 1 {
1336 builder.compiler(1, builder.config.host_target)
1338 } else {
1339 builder.compiler(stage - 1, builder.config.host_target)
1340 }
1341 }
1342
1343 pub fn build_compiler(&self) -> Compiler {
1344 self.build_compiler
1345 }
1346
1347 pub fn target_compiler(&self) -> Compiler {
1348 self.target_compiler
1349 }
1350
1351 pub fn target(&self) -> TargetSelection {
1353 self.target_compiler.host
1354 }
1355}
1356
1357macro_rules! tool_rustc_extended {
1360 (
1361 $name:ident {
1362 path: $path:expr,
1363 tool_name: $tool_name:expr,
1364 stable: $stable:expr
1365 $( , add_bins_to_sysroot: $add_bins_to_sysroot:expr )?
1366 $( , add_features: $add_features:expr )?
1367 $( , cargo_args: $cargo_args:expr )?
1368 $( , )?
1369 }
1370 ) => {
1371 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
1372 pub struct $name {
1373 compilers: RustcPrivateCompilers,
1374 }
1375
1376 impl $name {
1377 pub fn from_compilers(compilers: RustcPrivateCompilers) -> Self {
1378 Self {
1379 compilers,
1380 }
1381 }
1382 }
1383
1384 impl Step for $name {
1385 type Output = ToolBuildResult;
1386 const DEFAULT: bool = true; const IS_HOST: bool = true;
1388
1389 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1390 should_run_extended_rustc_tool(
1391 run,
1392 $tool_name,
1393 $path,
1394 $stable,
1395 )
1396 }
1397
1398 fn make_run(run: RunConfig<'_>) {
1399 run.builder.ensure($name {
1400 compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1401 });
1402 }
1403
1404 fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1405 let Self { compilers } = self;
1406 build_extended_rustc_tool(
1407 builder,
1408 compilers,
1409 $tool_name,
1410 $path,
1411 None $( .or(Some(&$add_bins_to_sysroot)) )?,
1412 None $( .or(Some($add_features)) )?,
1413 None $( .or(Some($cargo_args)) )?,
1414 )
1415 }
1416
1417 fn metadata(&self) -> Option<StepMetadata> {
1418 Some(
1419 StepMetadata::build($tool_name, self.compilers.target())
1420 .built_by(self.compilers.build_compiler)
1421 )
1422 }
1423 }
1424 }
1425}
1426
1427fn should_run_extended_rustc_tool<'a>(
1428 run: ShouldRun<'a>,
1429 tool_name: &'static str,
1430 path: &'static str,
1431 stable: bool,
1432) -> ShouldRun<'a> {
1433 let builder = run.builder;
1434 run.path(path).default_condition(
1435 builder.config.extended
1436 && builder.config.tools.as_ref().map_or(
1437 stable || builder.build.unstable_features(),
1440 |tools| {
1442 tools.iter().any(|tool| match tool.as_ref() {
1443 "clippy" => tool_name == "clippy-driver",
1444 x => tool_name == x,
1445 })
1446 },
1447 ),
1448 )
1449}
1450
1451fn build_extended_rustc_tool(
1452 builder: &Builder<'_>,
1453 compilers: RustcPrivateCompilers,
1454 tool_name: &'static str,
1455 path: &'static str,
1456 add_bins_to_sysroot: Option<&[&str]>,
1457 add_features: Option<fn(&Builder<'_>, TargetSelection, &mut Vec<String>)>,
1458 cargo_args: Option<&[&'static str]>,
1459) -> ToolBuildResult {
1460 let target = compilers.target();
1461 let mut extra_features = Vec::new();
1462 if let Some(func) = add_features {
1463 func(builder, target, &mut extra_features);
1464 }
1465
1466 let build_compiler = compilers.build_compiler;
1467 let ToolBuildResult { tool_path, .. } = builder.ensure(ToolBuild {
1468 build_compiler,
1469 target,
1470 tool: tool_name,
1471 mode: Mode::ToolRustcPrivate,
1472 path,
1473 extra_features,
1474 source_type: SourceType::InTree,
1475 allow_features: "",
1476 cargo_args: cargo_args.unwrap_or_default().iter().map(|s| String::from(*s)).collect(),
1477 artifact_kind: ToolArtifactKind::Binary,
1478 });
1479
1480 let target_compiler = compilers.target_compiler;
1481 if let Some(add_bins_to_sysroot) = add_bins_to_sysroot
1482 && !add_bins_to_sysroot.is_empty()
1483 {
1484 let bindir = builder.sysroot(target_compiler).join("bin");
1485 t!(fs::create_dir_all(&bindir));
1486
1487 for add_bin in add_bins_to_sysroot {
1488 let bin_destination = bindir.join(exe(add_bin, target_compiler.host));
1489 builder.copy_link(&tool_path, &bin_destination, FileType::Executable);
1490 }
1491
1492 let path = bindir.join(exe(tool_name, target_compiler.host));
1494 ToolBuildResult { tool_path: path, build_compiler }
1495 } else {
1496 ToolBuildResult { tool_path, build_compiler }
1497 }
1498}
1499
1500tool_rustc_extended!(Cargofmt {
1501 path: "src/tools/rustfmt",
1502 tool_name: "cargo-fmt",
1503 stable: true,
1504 add_bins_to_sysroot: ["cargo-fmt"]
1505});
1506tool_rustc_extended!(CargoClippy {
1507 path: "src/tools/clippy",
1508 tool_name: "cargo-clippy",
1509 stable: true,
1510 add_bins_to_sysroot: ["cargo-clippy"]
1511});
1512tool_rustc_extended!(Clippy {
1513 path: "src/tools/clippy",
1514 tool_name: "clippy-driver",
1515 stable: true,
1516 add_bins_to_sysroot: ["clippy-driver"],
1517 add_features: |builder, target, features| {
1518 if builder.config.jemalloc(target) {
1519 features.push("jemalloc".to_string());
1520 }
1521 }
1522});
1523tool_rustc_extended!(Miri {
1524 path: "src/tools/miri",
1525 tool_name: "miri",
1526 stable: false,
1527 add_bins_to_sysroot: ["miri"],
1528 cargo_args: &["--all-targets"],
1530});
1531tool_rustc_extended!(CargoMiri {
1532 path: "src/tools/miri/cargo-miri",
1533 tool_name: "cargo-miri",
1534 stable: false,
1535 add_bins_to_sysroot: ["cargo-miri"]
1536});
1537tool_rustc_extended!(Rustfmt {
1538 path: "src/tools/rustfmt",
1539 tool_name: "rustfmt",
1540 stable: true,
1541 add_bins_to_sysroot: ["rustfmt"]
1542});
1543
1544pub const TEST_FLOAT_PARSE_ALLOW_FEATURES: &str = "f16,cfg_target_has_reliable_f16_f128";
1545
1546impl Builder<'_> {
1547 pub fn tool_cmd(&self, tool: Tool) -> BootstrapCommand {
1550 let mut cmd = command(self.tool_exe(tool));
1551 let compiler = self.compiler(0, self.config.host_target);
1552 let host = &compiler.host;
1553 let mut lib_paths: Vec<PathBuf> =
1558 vec![self.cargo_out(compiler, Mode::ToolBootstrap, *host).join("deps")];
1559
1560 if compiler.host.is_msvc() {
1564 let curpaths = env::var_os("PATH").unwrap_or_default();
1565 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
1566 for (k, v) in self.cc[&compiler.host].env() {
1567 if k != "PATH" {
1568 continue;
1569 }
1570 for path in env::split_paths(v) {
1571 if !curpaths.contains(&path) {
1572 lib_paths.push(path);
1573 }
1574 }
1575 }
1576 }
1577
1578 add_dylib_path(lib_paths, &mut cmd);
1579
1580 cmd.env("RUSTC", &self.initial_rustc);
1582
1583 cmd
1584 }
1585}