1use std::any::{Any, type_name};
2use std::cell::{Cell, RefCell};
3use std::collections::BTreeSet;
4use std::fmt::{self, Debug, Write};
5use std::hash::Hash;
6use std::ops::Deref;
7use std::path::{Path, PathBuf};
8use std::sync::LazyLock;
9use std::time::{Duration, Instant};
10use std::{env, fs};
11
12use clap::ValueEnum;
13#[cfg(feature = "tracing")]
14use tracing::instrument;
15
16pub use self::cargo::{Cargo, cargo_profile_var};
17pub use crate::Compiler;
18use crate::core::build_steps::{
19 check, clean, clippy, compile, dist, doc, gcc, install, llvm, run, setup, test, tool, vendor,
20};
21use crate::core::config::flags::Subcommand;
22use crate::core::config::{DryRun, TargetSelection};
23use crate::utils::cache::Cache;
24use crate::utils::exec::{BootstrapCommand, command};
25use crate::utils::helpers::{self, LldThreads, add_dylib_path, exe, libdir, linker_args, t};
26use crate::{Build, Crate, trace};
27
28mod cargo;
29
30#[cfg(test)]
31mod tests;
32
33pub struct Builder<'a> {
36 pub build: &'a Build,
38
39 pub top_stage: u32,
43
44 pub kind: Kind,
46
47 cache: Cache,
50
51 stack: RefCell<Vec<Box<dyn AnyDebug>>>,
54
55 time_spent_on_dependencies: Cell<Duration>,
57
58 pub paths: Vec<PathBuf>,
62}
63
64impl Deref for Builder<'_> {
65 type Target = Build;
66
67 fn deref(&self) -> &Self::Target {
68 self.build
69 }
70}
71
72trait AnyDebug: Any + Debug {}
77impl<T: Any + Debug> AnyDebug for T {}
78impl dyn AnyDebug {
79 fn downcast_ref<T: Any>(&self) -> Option<&T> {
81 (self as &dyn Any).downcast_ref()
82 }
83
84 }
86
87pub trait Step: 'static + Clone + Debug + PartialEq + Eq + Hash {
88 type Output: Clone;
90
91 const DEFAULT: bool = false;
97
98 const ONLY_HOSTS: bool = false;
100
101 fn run(self, builder: &Builder<'_>) -> Self::Output;
115
116 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_>;
118
119 fn make_run(_run: RunConfig<'_>) {
123 unimplemented!()
128 }
129}
130
131pub struct RunConfig<'a> {
132 pub builder: &'a Builder<'a>,
133 pub target: TargetSelection,
134 pub paths: Vec<PathSet>,
135}
136
137impl RunConfig<'_> {
138 pub fn build_triple(&self) -> TargetSelection {
139 self.builder.build.build
140 }
141
142 #[track_caller]
144 pub fn cargo_crates_in_set(&self) -> Vec<String> {
145 let mut crates = Vec::new();
146 for krate in &self.paths {
147 let path = &krate.assert_single_path().path;
148
149 let crate_name = self
150 .builder
151 .crate_paths
152 .get(path)
153 .unwrap_or_else(|| panic!("missing crate for path {}", path.display()));
154
155 crates.push(crate_name.to_string());
156 }
157 crates
158 }
159
160 pub fn make_run_crates(&self, alias: Alias) -> Vec<String> {
167 let has_alias =
168 self.paths.iter().any(|set| set.assert_single_path().path.ends_with(alias.as_str()));
169 if !has_alias {
170 return self.cargo_crates_in_set();
171 }
172
173 let crates = match alias {
174 Alias::Library => self.builder.in_tree_crates("sysroot", Some(self.target)),
175 Alias::Compiler => self.builder.in_tree_crates("rustc-main", Some(self.target)),
176 };
177
178 crates.into_iter().map(|krate| krate.name.to_string()).collect()
179 }
180}
181
182#[derive(Debug, Copy, Clone)]
183pub enum Alias {
184 Library,
185 Compiler,
186}
187
188impl Alias {
189 fn as_str(self) -> &'static str {
190 match self {
191 Alias::Library => "library",
192 Alias::Compiler => "compiler",
193 }
194 }
195}
196
197pub fn crate_description(crates: &[impl AsRef<str>]) -> String {
201 if crates.is_empty() {
202 return "".into();
203 }
204
205 let mut descr = String::from(" {");
206 descr.push_str(crates[0].as_ref());
207 for krate in &crates[1..] {
208 descr.push_str(", ");
209 descr.push_str(krate.as_ref());
210 }
211 descr.push('}');
212 descr
213}
214
215struct StepDescription {
216 default: bool,
217 only_hosts: bool,
218 should_run: fn(ShouldRun<'_>) -> ShouldRun<'_>,
219 make_run: fn(RunConfig<'_>),
220 name: &'static str,
221 kind: Kind,
222}
223
224#[derive(Clone, PartialOrd, Ord, PartialEq, Eq)]
225pub struct TaskPath {
226 pub path: PathBuf,
227 pub kind: Option<Kind>,
228}
229
230impl Debug for TaskPath {
231 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
232 if let Some(kind) = &self.kind {
233 write!(f, "{}::", kind.as_str())?;
234 }
235 write!(f, "{}", self.path.display())
236 }
237}
238
239#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq)]
241pub enum PathSet {
242 Set(BTreeSet<TaskPath>),
253 Suite(TaskPath),
260}
261
262impl PathSet {
263 fn empty() -> PathSet {
264 PathSet::Set(BTreeSet::new())
265 }
266
267 fn one<P: Into<PathBuf>>(path: P, kind: Kind) -> PathSet {
268 let mut set = BTreeSet::new();
269 set.insert(TaskPath { path: path.into(), kind: Some(kind) });
270 PathSet::Set(set)
271 }
272
273 fn has(&self, needle: &Path, module: Kind) -> bool {
274 match self {
275 PathSet::Set(set) => set.iter().any(|p| Self::check(p, needle, module)),
276 PathSet::Suite(suite) => Self::check(suite, needle, module),
277 }
278 }
279
280 fn check(p: &TaskPath, needle: &Path, module: Kind) -> bool {
282 let check_path = || {
283 p.path.ends_with(needle) || p.path.starts_with(needle)
285 };
286 if let Some(p_kind) = &p.kind { check_path() && *p_kind == module } else { check_path() }
287 }
288
289 fn intersection_removing_matches(&self, needles: &mut [CLIStepPath], module: Kind) -> PathSet {
296 let mut check = |p| {
297 let mut result = false;
298 for n in needles.iter_mut() {
299 let matched = Self::check(p, &n.path, module);
300 if matched {
301 n.will_be_executed = true;
302 result = true;
303 }
304 }
305 result
306 };
307 match self {
308 PathSet::Set(set) => PathSet::Set(set.iter().filter(|&p| check(p)).cloned().collect()),
309 PathSet::Suite(suite) => {
310 if check(suite) {
311 self.clone()
312 } else {
313 PathSet::empty()
314 }
315 }
316 }
317 }
318
319 #[track_caller]
323 pub fn assert_single_path(&self) -> &TaskPath {
324 match self {
325 PathSet::Set(set) => {
326 assert_eq!(set.len(), 1, "called assert_single_path on multiple paths");
327 set.iter().next().unwrap()
328 }
329 PathSet::Suite(_) => unreachable!("called assert_single_path on a Suite path"),
330 }
331 }
332}
333
334const PATH_REMAP: &[(&str, &[&str])] = &[
335 ("rust-analyzer-proc-macro-srv", &["src/tools/rust-analyzer/crates/proc-macro-srv-cli"]),
338 (
340 "tests",
341 &[
342 "tests/assembly",
344 "tests/codegen",
345 "tests/codegen-units",
346 "tests/coverage",
347 "tests/coverage-run-rustdoc",
348 "tests/crashes",
349 "tests/debuginfo",
350 "tests/incremental",
351 "tests/mir-opt",
352 "tests/pretty",
353 "tests/run-make",
354 "tests/rustdoc",
355 "tests/rustdoc-gui",
356 "tests/rustdoc-js",
357 "tests/rustdoc-js-std",
358 "tests/rustdoc-json",
359 "tests/rustdoc-ui",
360 "tests/ui",
361 "tests/ui-fulldeps",
362 ],
364 ),
365];
366
367fn remap_paths(paths: &mut Vec<PathBuf>) {
368 let mut remove = vec![];
369 let mut add = vec![];
370 for (i, path) in paths.iter().enumerate().filter_map(|(i, path)| path.to_str().map(|s| (i, s)))
371 {
372 for &(search, replace) in PATH_REMAP {
373 if path.trim_matches(std::path::is_separator) == search {
375 remove.push(i);
376 add.extend(replace.iter().map(PathBuf::from));
377 break;
378 }
379 }
380 }
381 remove.sort();
382 remove.dedup();
383 for idx in remove.into_iter().rev() {
384 paths.remove(idx);
385 }
386 paths.append(&mut add);
387}
388
389#[derive(Clone, PartialEq)]
390struct CLIStepPath {
391 path: PathBuf,
392 will_be_executed: bool,
393}
394
395#[cfg(test)]
396impl CLIStepPath {
397 fn will_be_executed(mut self, will_be_executed: bool) -> Self {
398 self.will_be_executed = will_be_executed;
399 self
400 }
401}
402
403impl Debug for CLIStepPath {
404 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
405 write!(f, "{}", self.path.display())
406 }
407}
408
409impl From<PathBuf> for CLIStepPath {
410 fn from(path: PathBuf) -> Self {
411 Self { path, will_be_executed: false }
412 }
413}
414
415impl StepDescription {
416 fn from<S: Step>(kind: Kind) -> StepDescription {
417 StepDescription {
418 default: S::DEFAULT,
419 only_hosts: S::ONLY_HOSTS,
420 should_run: S::should_run,
421 make_run: S::make_run,
422 name: std::any::type_name::<S>(),
423 kind,
424 }
425 }
426
427 fn maybe_run(&self, builder: &Builder<'_>, mut pathsets: Vec<PathSet>) {
428 pathsets.retain(|set| !self.is_excluded(builder, set));
429
430 if pathsets.is_empty() {
431 return;
432 }
433
434 let targets = if self.only_hosts { &builder.hosts } else { &builder.targets };
436
437 for target in targets {
438 let run = RunConfig { builder, paths: pathsets.clone(), target: *target };
439 (self.make_run)(run);
440 }
441 }
442
443 fn is_excluded(&self, builder: &Builder<'_>, pathset: &PathSet) -> bool {
444 if builder.config.skip.iter().any(|e| pathset.has(e, builder.kind)) {
445 if !matches!(builder.config.dry_run, DryRun::SelfCheck) {
446 println!("Skipping {pathset:?} because it is excluded");
447 }
448 return true;
449 }
450
451 if !builder.config.skip.is_empty() && !matches!(builder.config.dry_run, DryRun::SelfCheck) {
452 builder.verbose(|| {
453 println!(
454 "{:?} not skipped for {:?} -- not in {:?}",
455 pathset, self.name, builder.config.skip
456 )
457 });
458 }
459 false
460 }
461
462 fn run(v: &[StepDescription], builder: &Builder<'_>, paths: &[PathBuf]) {
463 let should_runs = v
464 .iter()
465 .map(|desc| (desc.should_run)(ShouldRun::new(builder, desc.kind)))
466 .collect::<Vec<_>>();
467
468 if builder.download_rustc() && (builder.kind == Kind::Dist || builder.kind == Kind::Install)
469 {
470 eprintln!(
471 "ERROR: '{}' subcommand is incompatible with `rust.download-rustc`.",
472 builder.kind.as_str()
473 );
474 crate::exit!(1);
475 }
476
477 for (desc, should_run) in v.iter().zip(&should_runs) {
479 assert!(
480 !should_run.paths.is_empty(),
481 "{:?} should have at least one pathset",
482 desc.name
483 );
484 }
485
486 if paths.is_empty() || builder.config.include_default_paths {
487 for (desc, should_run) in v.iter().zip(&should_runs) {
488 if desc.default && should_run.is_really_default() {
489 desc.maybe_run(builder, should_run.paths.iter().cloned().collect());
490 }
491 }
492 }
493
494 let mut paths: Vec<PathBuf> = paths
496 .iter()
497 .map(|p| {
498 if !p.exists() {
500 return p.clone();
501 }
502
503 match std::path::absolute(p) {
505 Ok(p) => p.strip_prefix(&builder.src).unwrap_or(&p).to_path_buf(),
506 Err(e) => {
507 eprintln!("ERROR: {e:?}");
508 panic!("Due to the above error, failed to resolve path: {p:?}");
509 }
510 }
511 })
512 .collect();
513
514 remap_paths(&mut paths);
515
516 paths.retain(|path| {
519 for (desc, should_run) in v.iter().zip(&should_runs) {
520 if let Some(suite) = should_run.is_suite_path(path) {
521 desc.maybe_run(builder, vec![suite.clone()]);
522 return false;
523 }
524 }
525 true
526 });
527
528 if paths.is_empty() {
529 return;
530 }
531
532 let mut paths: Vec<CLIStepPath> = paths.into_iter().map(|p| p.into()).collect();
533 let mut path_lookup: Vec<(CLIStepPath, bool)> =
534 paths.clone().into_iter().map(|p| (p, false)).collect();
535
536 let mut steps_to_run = vec![];
540
541 for (desc, should_run) in v.iter().zip(&should_runs) {
542 let pathsets = should_run.pathset_for_paths_removing_matches(&mut paths, desc.kind);
543
544 let mut closest_index = usize::MAX;
550
551 for (index, (path, is_used)) in path_lookup.iter_mut().enumerate() {
553 if !*is_used && !paths.contains(path) {
554 closest_index = index;
555 *is_used = true;
556 break;
557 }
558 }
559
560 steps_to_run.push((closest_index, desc, pathsets));
561 }
562
563 steps_to_run.sort_by_key(|(index, _, _)| *index);
565
566 for (_index, desc, pathsets) in steps_to_run {
568 if !pathsets.is_empty() {
569 desc.maybe_run(builder, pathsets);
570 }
571 }
572
573 paths.retain(|p| !p.will_be_executed);
574
575 if !paths.is_empty() {
576 eprintln!("ERROR: no `{}` rules matched {:?}", builder.kind.as_str(), paths);
577 eprintln!(
578 "HELP: run `x.py {} --help --verbose` to show a list of available paths",
579 builder.kind.as_str()
580 );
581 eprintln!(
582 "NOTE: if you are adding a new Step to bootstrap itself, make sure you register it with `describe!`"
583 );
584 crate::exit!(1);
585 }
586 }
587}
588
589enum ReallyDefault<'a> {
590 Bool(bool),
591 Lazy(LazyLock<bool, Box<dyn Fn() -> bool + 'a>>),
592}
593
594pub struct ShouldRun<'a> {
595 pub builder: &'a Builder<'a>,
596 kind: Kind,
597
598 paths: BTreeSet<PathSet>,
600
601 is_really_default: ReallyDefault<'a>,
604}
605
606impl<'a> ShouldRun<'a> {
607 fn new(builder: &'a Builder<'_>, kind: Kind) -> ShouldRun<'a> {
608 ShouldRun {
609 builder,
610 kind,
611 paths: BTreeSet::new(),
612 is_really_default: ReallyDefault::Bool(true), }
614 }
615
616 pub fn default_condition(mut self, cond: bool) -> Self {
617 self.is_really_default = ReallyDefault::Bool(cond);
618 self
619 }
620
621 pub fn lazy_default_condition(mut self, lazy_cond: Box<dyn Fn() -> bool + 'a>) -> Self {
622 self.is_really_default = ReallyDefault::Lazy(LazyLock::new(lazy_cond));
623 self
624 }
625
626 pub fn is_really_default(&self) -> bool {
627 match &self.is_really_default {
628 ReallyDefault::Bool(val) => *val,
629 ReallyDefault::Lazy(lazy) => *lazy.deref(),
630 }
631 }
632
633 pub fn crate_or_deps(self, name: &str) -> Self {
638 let crates = self.builder.in_tree_crates(name, None);
639 self.crates(crates)
640 }
641
642 pub(crate) fn crates(mut self, crates: Vec<&Crate>) -> Self {
648 for krate in crates {
649 let path = krate.local_path(self.builder);
650 self.paths.insert(PathSet::one(path, self.kind));
651 }
652 self
653 }
654
655 pub fn alias(mut self, alias: &str) -> Self {
657 assert!(
661 self.kind == Kind::Setup || !self.builder.src.join(alias).exists(),
662 "use `builder.path()` for real paths: {alias}"
663 );
664 self.paths.insert(PathSet::Set(
665 std::iter::once(TaskPath { path: alias.into(), kind: Some(self.kind) }).collect(),
666 ));
667 self
668 }
669
670 pub fn path(self, path: &str) -> Self {
674 self.paths(&[path])
675 }
676
677 pub fn paths(mut self, paths: &[&str]) -> Self {
687 let submodules_paths = build_helper::util::parse_gitmodules(&self.builder.src);
688
689 self.paths.insert(PathSet::Set(
690 paths
691 .iter()
692 .map(|p| {
693 if !submodules_paths.iter().any(|sm_p| p.contains(sm_p)) {
695 assert!(
696 self.builder.src.join(p).exists(),
697 "`should_run.paths` should correspond to real on-disk paths - use `alias` if there is no relevant path: {p}"
698 );
699 }
700
701 TaskPath { path: p.into(), kind: Some(self.kind) }
702 })
703 .collect(),
704 ));
705 self
706 }
707
708 fn is_suite_path(&self, requested_path: &Path) -> Option<&PathSet> {
710 self.paths.iter().find(|pathset| match pathset {
711 PathSet::Suite(suite) => requested_path.starts_with(&suite.path),
712 PathSet::Set(_) => false,
713 })
714 }
715
716 pub fn suite_path(mut self, suite: &str) -> Self {
717 self.paths.insert(PathSet::Suite(TaskPath { path: suite.into(), kind: Some(self.kind) }));
718 self
719 }
720
721 pub fn never(mut self) -> ShouldRun<'a> {
723 self.paths.insert(PathSet::empty());
724 self
725 }
726
727 fn pathset_for_paths_removing_matches(
737 &self,
738 paths: &mut [CLIStepPath],
739 kind: Kind,
740 ) -> Vec<PathSet> {
741 let mut sets = vec![];
742 for pathset in &self.paths {
743 let subset = pathset.intersection_removing_matches(paths, kind);
744 if subset != PathSet::empty() {
745 sets.push(subset);
746 }
747 }
748 sets
749 }
750}
751
752#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord, ValueEnum)]
753pub enum Kind {
754 #[value(alias = "b")]
755 Build,
756 #[value(alias = "c")]
757 Check,
758 Clippy,
759 Fix,
760 Format,
761 #[value(alias = "t")]
762 Test,
763 Miri,
764 MiriSetup,
765 MiriTest,
766 Bench,
767 #[value(alias = "d")]
768 Doc,
769 Clean,
770 Dist,
771 Install,
772 #[value(alias = "r")]
773 Run,
774 Setup,
775 Suggest,
776 Vendor,
777 Perf,
778}
779
780impl Kind {
781 pub fn as_str(&self) -> &'static str {
782 match self {
783 Kind::Build => "build",
784 Kind::Check => "check",
785 Kind::Clippy => "clippy",
786 Kind::Fix => "fix",
787 Kind::Format => "fmt",
788 Kind::Test => "test",
789 Kind::Miri => "miri",
790 Kind::MiriSetup => panic!("`as_str` is not supported for `Kind::MiriSetup`."),
791 Kind::MiriTest => panic!("`as_str` is not supported for `Kind::MiriTest`."),
792 Kind::Bench => "bench",
793 Kind::Doc => "doc",
794 Kind::Clean => "clean",
795 Kind::Dist => "dist",
796 Kind::Install => "install",
797 Kind::Run => "run",
798 Kind::Setup => "setup",
799 Kind::Suggest => "suggest",
800 Kind::Vendor => "vendor",
801 Kind::Perf => "perf",
802 }
803 }
804
805 pub fn description(&self) -> String {
806 match self {
807 Kind::Test => "Testing",
808 Kind::Bench => "Benchmarking",
809 Kind::Doc => "Documenting",
810 Kind::Run => "Running",
811 Kind::Suggest => "Suggesting",
812 Kind::Clippy => "Linting",
813 Kind::Perf => "Profiling & benchmarking",
814 _ => {
815 let title_letter = self.as_str()[0..1].to_ascii_uppercase();
816 return format!("{title_letter}{}ing", &self.as_str()[1..]);
817 }
818 }
819 .to_owned()
820 }
821}
822
823#[derive(Debug, Clone, Hash, PartialEq, Eq)]
824struct Libdir {
825 compiler: Compiler,
826 target: TargetSelection,
827}
828
829impl Step for Libdir {
830 type Output = PathBuf;
831
832 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
833 run.never()
834 }
835
836 fn run(self, builder: &Builder<'_>) -> PathBuf {
837 let relative_sysroot_libdir = builder.sysroot_libdir_relative(self.compiler);
838 let sysroot = builder.sysroot(self.compiler).join(relative_sysroot_libdir).join("rustlib");
839
840 if !builder.config.dry_run() {
841 if !builder.download_rustc() {
844 let sysroot_target_libdir = sysroot.join(self.target).join("lib");
845 builder.verbose(|| {
846 eprintln!(
847 "Removing sysroot {} to avoid caching bugs",
848 sysroot_target_libdir.display()
849 )
850 });
851 let _ = fs::remove_dir_all(&sysroot_target_libdir);
852 t!(fs::create_dir_all(&sysroot_target_libdir));
853 }
854
855 if self.compiler.stage == 0 {
856 dist::maybe_install_llvm_target(
860 builder,
861 self.compiler.host,
862 &builder.sysroot(self.compiler),
863 );
864 }
865 }
866
867 sysroot
868 }
869}
870
871impl<'a> Builder<'a> {
872 fn get_step_descriptions(kind: Kind) -> Vec<StepDescription> {
873 macro_rules! describe {
874 ($($rule:ty),+ $(,)?) => {{
875 vec![$(StepDescription::from::<$rule>(kind)),+]
876 }};
877 }
878 match kind {
879 Kind::Build => describe!(
880 compile::Std,
881 compile::Rustc,
882 compile::Assemble,
883 compile::CodegenBackend,
884 compile::StartupObjects,
885 tool::BuildManifest,
886 tool::Rustbook,
887 tool::ErrorIndex,
888 tool::UnstableBookGen,
889 tool::Tidy,
890 tool::Linkchecker,
891 tool::CargoTest,
892 tool::Compiletest,
893 tool::RemoteTestServer,
894 tool::RemoteTestClient,
895 tool::RustInstaller,
896 tool::Cargo,
897 tool::RustAnalyzer,
898 tool::RustAnalyzerProcMacroSrv,
899 tool::Rustdoc,
900 tool::Clippy,
901 tool::CargoClippy,
902 llvm::Llvm,
903 gcc::Gcc,
904 llvm::Sanitizers,
905 tool::Rustfmt,
906 tool::Cargofmt,
907 tool::Miri,
908 tool::CargoMiri,
909 llvm::Lld,
910 llvm::Enzyme,
911 llvm::CrtBeginEnd,
912 tool::RustdocGUITest,
913 tool::OptimizedDist,
914 tool::CoverageDump,
915 tool::LlvmBitcodeLinker,
916 tool::RustcPerf,
917 ),
918 Kind::Clippy => describe!(
919 clippy::Std,
920 clippy::Rustc,
921 clippy::Bootstrap,
922 clippy::BuildHelper,
923 clippy::BuildManifest,
924 clippy::CargoMiri,
925 clippy::Clippy,
926 clippy::CodegenGcc,
927 clippy::CollectLicenseMetadata,
928 clippy::Compiletest,
929 clippy::CoverageDump,
930 clippy::Jsondocck,
931 clippy::Jsondoclint,
932 clippy::LintDocs,
933 clippy::LlvmBitcodeLinker,
934 clippy::Miri,
935 clippy::MiroptTestTools,
936 clippy::OptDist,
937 clippy::RemoteTestClient,
938 clippy::RemoteTestServer,
939 clippy::RustAnalyzer,
940 clippy::Rustdoc,
941 clippy::Rustfmt,
942 clippy::RustInstaller,
943 clippy::TestFloatParse,
944 clippy::Tidy,
945 clippy::CI,
946 ),
947 Kind::Check | Kind::Fix => describe!(
948 check::Std,
949 check::Rustc,
950 check::Rustdoc,
951 check::CodegenBackend,
952 check::Clippy,
953 check::Miri,
954 check::CargoMiri,
955 check::MiroptTestTools,
956 check::Rustfmt,
957 check::RustAnalyzer,
958 check::TestFloatParse,
959 check::Bootstrap,
960 check::RunMakeSupport,
961 check::Compiletest,
962 check::FeaturesStatusDump,
963 check::CoverageDump,
964 ),
965 Kind::Test => describe!(
966 crate::core::build_steps::toolstate::ToolStateCheck,
967 test::Tidy,
968 test::Ui,
969 test::Crashes,
970 test::Coverage,
971 test::MirOpt,
972 test::Codegen,
973 test::CodegenUnits,
974 test::Assembly,
975 test::Incremental,
976 test::Debuginfo,
977 test::UiFullDeps,
978 test::Rustdoc,
979 test::CoverageRunRustdoc,
980 test::Pretty,
981 test::CodegenCranelift,
982 test::CodegenGCC,
983 test::Crate,
984 test::CrateLibrustc,
985 test::CrateRustdoc,
986 test::CrateRustdocJsonTypes,
987 test::CrateBootstrap,
988 test::Linkcheck,
989 test::TierCheck,
990 test::Cargotest,
991 test::Cargo,
992 test::RustAnalyzer,
993 test::ErrorIndex,
994 test::Distcheck,
995 test::Nomicon,
996 test::Reference,
997 test::RustdocBook,
998 test::RustByExample,
999 test::TheBook,
1000 test::UnstableBook,
1001 test::RustcBook,
1002 test::LintDocs,
1003 test::EmbeddedBook,
1004 test::EditionGuide,
1005 test::Rustfmt,
1006 test::Miri,
1007 test::CargoMiri,
1008 test::Clippy,
1009 test::CompiletestTest,
1010 test::CrateRunMakeSupport,
1011 test::CrateBuildHelper,
1012 test::RustdocJSStd,
1013 test::RustdocJSNotStd,
1014 test::RustdocGUI,
1015 test::RustdocTheme,
1016 test::RustdocUi,
1017 test::RustdocJson,
1018 test::HtmlCheck,
1019 test::RustInstaller,
1020 test::TestFloatParse,
1021 test::CollectLicenseMetadata,
1022 test::Bootstrap,
1024 test::RunMake,
1026 ),
1027 Kind::Miri => describe!(test::Crate),
1028 Kind::Bench => describe!(test::Crate, test::CrateLibrustc),
1029 Kind::Doc => describe!(
1030 doc::UnstableBook,
1031 doc::UnstableBookGen,
1032 doc::TheBook,
1033 doc::Standalone,
1034 doc::Std,
1035 doc::Rustc,
1036 doc::Rustdoc,
1037 doc::Rustfmt,
1038 doc::ErrorIndex,
1039 doc::Nomicon,
1040 doc::Reference,
1041 doc::RustdocBook,
1042 doc::RustByExample,
1043 doc::RustcBook,
1044 doc::Cargo,
1045 doc::CargoBook,
1046 doc::Clippy,
1047 doc::ClippyBook,
1048 doc::Miri,
1049 doc::EmbeddedBook,
1050 doc::EditionGuide,
1051 doc::StyleGuide,
1052 doc::Tidy,
1053 doc::Bootstrap,
1054 doc::Releases,
1055 doc::RunMakeSupport,
1056 doc::BuildHelper,
1057 doc::Compiletest,
1058 ),
1059 Kind::Dist => describe!(
1060 dist::Docs,
1061 dist::RustcDocs,
1062 dist::JsonDocs,
1063 dist::Mingw,
1064 dist::Rustc,
1065 dist::CodegenBackend,
1066 dist::Std,
1067 dist::RustcDev,
1068 dist::Analysis,
1069 dist::Src,
1070 dist::Cargo,
1071 dist::RustAnalyzer,
1072 dist::Rustfmt,
1073 dist::Clippy,
1074 dist::Miri,
1075 dist::LlvmTools,
1076 dist::LlvmBitcodeLinker,
1077 dist::RustDev,
1078 dist::Bootstrap,
1079 dist::Extended,
1080 dist::PlainSourceTarball,
1085 dist::BuildManifest,
1086 dist::ReproducibleArtifacts,
1087 dist::Gcc
1088 ),
1089 Kind::Install => describe!(
1090 install::Docs,
1091 install::Std,
1092 install::Rustc,
1097 install::Cargo,
1098 install::RustAnalyzer,
1099 install::Rustfmt,
1100 install::Clippy,
1101 install::Miri,
1102 install::LlvmTools,
1103 install::Src,
1104 ),
1105 Kind::Run => describe!(
1106 run::BuildManifest,
1107 run::BumpStage0,
1108 run::ReplaceVersionPlaceholder,
1109 run::Miri,
1110 run::CollectLicenseMetadata,
1111 run::GenerateCopyright,
1112 run::GenerateWindowsSys,
1113 run::GenerateCompletions,
1114 run::UnicodeTableGenerator,
1115 run::FeaturesStatusDump,
1116 run::CyclicStep,
1117 run::CoverageDump,
1118 run::Rustfmt,
1119 ),
1120 Kind::Setup => {
1121 describe!(setup::Profile, setup::Hook, setup::Link, setup::Editor)
1122 }
1123 Kind::Clean => describe!(clean::CleanAll, clean::Rustc, clean::Std),
1124 Kind::Vendor => describe!(vendor::Vendor),
1125 Kind::Format | Kind::Suggest | Kind::Perf => vec![],
1127 Kind::MiriTest | Kind::MiriSetup => unreachable!(),
1128 }
1129 }
1130
1131 pub fn get_help(build: &Build, kind: Kind) -> Option<String> {
1132 let step_descriptions = Builder::get_step_descriptions(kind);
1133 if step_descriptions.is_empty() {
1134 return None;
1135 }
1136
1137 let builder = Self::new_internal(build, kind, vec![]);
1138 let builder = &builder;
1139 let mut should_run = ShouldRun::new(builder, Kind::Build);
1142 for desc in step_descriptions {
1143 should_run.kind = desc.kind;
1144 should_run = (desc.should_run)(should_run);
1145 }
1146 let mut help = String::from("Available paths:\n");
1147 let mut add_path = |path: &Path| {
1148 t!(write!(help, " ./x.py {} {}\n", kind.as_str(), path.display()));
1149 };
1150 for pathset in should_run.paths {
1151 match pathset {
1152 PathSet::Set(set) => {
1153 for path in set {
1154 add_path(&path.path);
1155 }
1156 }
1157 PathSet::Suite(path) => {
1158 add_path(&path.path.join("..."));
1159 }
1160 }
1161 }
1162 Some(help)
1163 }
1164
1165 fn new_internal(build: &Build, kind: Kind, paths: Vec<PathBuf>) -> Builder<'_> {
1166 Builder {
1167 build,
1168 top_stage: build.config.stage,
1169 kind,
1170 cache: Cache::new(),
1171 stack: RefCell::new(Vec::new()),
1172 time_spent_on_dependencies: Cell::new(Duration::new(0, 0)),
1173 paths,
1174 }
1175 }
1176
1177 pub fn new(build: &Build) -> Builder<'_> {
1178 let paths = &build.config.paths;
1179 let (kind, paths) = match build.config.cmd {
1180 Subcommand::Build => (Kind::Build, &paths[..]),
1181 Subcommand::Check { .. } => (Kind::Check, &paths[..]),
1182 Subcommand::Clippy { .. } => (Kind::Clippy, &paths[..]),
1183 Subcommand::Fix => (Kind::Fix, &paths[..]),
1184 Subcommand::Doc { .. } => (Kind::Doc, &paths[..]),
1185 Subcommand::Test { .. } => (Kind::Test, &paths[..]),
1186 Subcommand::Miri { .. } => (Kind::Miri, &paths[..]),
1187 Subcommand::Bench { .. } => (Kind::Bench, &paths[..]),
1188 Subcommand::Dist => (Kind::Dist, &paths[..]),
1189 Subcommand::Install => (Kind::Install, &paths[..]),
1190 Subcommand::Run { .. } => (Kind::Run, &paths[..]),
1191 Subcommand::Clean { .. } => (Kind::Clean, &paths[..]),
1192 Subcommand::Format { .. } => (Kind::Format, &[][..]),
1193 Subcommand::Suggest { .. } => (Kind::Suggest, &[][..]),
1194 Subcommand::Setup { profile: ref path } => (
1195 Kind::Setup,
1196 path.as_ref().map_or([].as_slice(), |path| std::slice::from_ref(path)),
1197 ),
1198 Subcommand::Vendor { .. } => (Kind::Vendor, &paths[..]),
1199 Subcommand::Perf { .. } => (Kind::Perf, &paths[..]),
1200 };
1201
1202 Self::new_internal(build, kind, paths.to_owned())
1203 }
1204
1205 pub fn execute_cli(&self) {
1206 self.run_step_descriptions(&Builder::get_step_descriptions(self.kind), &self.paths);
1207 }
1208
1209 pub fn default_doc(&self, paths: &[PathBuf]) {
1210 self.run_step_descriptions(&Builder::get_step_descriptions(Kind::Doc), paths);
1211 }
1212
1213 pub fn doc_rust_lang_org_channel(&self) -> String {
1214 let channel = match &*self.config.channel {
1215 "stable" => &self.version,
1216 "beta" => "beta",
1217 "nightly" | "dev" => "nightly",
1218 _ => "stable",
1220 };
1221
1222 format!("https://doc.rust-lang.org/{channel}")
1223 }
1224
1225 fn run_step_descriptions(&self, v: &[StepDescription], paths: &[PathBuf]) {
1226 StepDescription::run(v, self, paths);
1227 }
1228
1229 pub fn link_std_into_rustc_driver(&self, target: TargetSelection) -> bool {
1232 !target.triple.ends_with("-windows-gnu")
1233 }
1234
1235 #[cfg_attr(
1240 feature = "tracing",
1241 instrument(
1242 level = "trace",
1243 name = "Builder::compiler",
1244 target = "COMPILER",
1245 skip_all,
1246 fields(
1247 stage = stage,
1248 host = ?host,
1249 ),
1250 ),
1251 )]
1252 pub fn compiler(&self, stage: u32, host: TargetSelection) -> Compiler {
1253 self.ensure(compile::Assemble { target_compiler: Compiler::new(stage, host) })
1254 }
1255
1256 #[cfg_attr(
1268 feature = "tracing",
1269 instrument(
1270 level = "trace",
1271 name = "Builder::compiler_for",
1272 target = "COMPILER_FOR",
1273 skip_all,
1274 fields(
1275 stage = stage,
1276 host = ?host,
1277 target = ?target,
1278 ),
1279 ),
1280 )]
1281 pub fn compiler_for(
1284 &self,
1285 stage: u32,
1286 host: TargetSelection,
1287 target: TargetSelection,
1288 ) -> Compiler {
1289 let mut resolved_compiler = if self.build.force_use_stage2(stage) {
1290 trace!(target: "COMPILER_FOR", ?stage, "force_use_stage2");
1291 self.compiler(2, self.config.build)
1292 } else if self.build.force_use_stage1(stage, target) {
1293 trace!(target: "COMPILER_FOR", ?stage, "force_use_stage1");
1294 self.compiler(1, self.config.build)
1295 } else {
1296 trace!(target: "COMPILER_FOR", ?stage, ?host, "no force, fallback to `compiler()`");
1297 self.compiler(stage, host)
1298 };
1299
1300 if stage != resolved_compiler.stage {
1301 resolved_compiler.forced_compiler(true);
1302 }
1303
1304 trace!(target: "COMPILER_FOR", ?resolved_compiler);
1305 resolved_compiler
1306 }
1307
1308 pub fn sysroot(&self, compiler: Compiler) -> PathBuf {
1309 self.ensure(compile::Sysroot::new(compiler))
1310 }
1311
1312 pub fn sysroot_target_bindir(&self, compiler: Compiler, target: TargetSelection) -> PathBuf {
1314 self.ensure(Libdir { compiler, target }).join(target).join("bin")
1315 }
1316
1317 pub fn sysroot_target_libdir(&self, compiler: Compiler, target: TargetSelection) -> PathBuf {
1320 self.ensure(Libdir { compiler, target }).join(target).join("lib")
1321 }
1322
1323 pub fn sysroot_codegen_backends(&self, compiler: Compiler) -> PathBuf {
1324 self.sysroot_target_libdir(compiler, compiler.host).with_file_name("codegen-backends")
1325 }
1326
1327 pub fn rustc_libdir(&self, compiler: Compiler) -> PathBuf {
1333 if compiler.is_snapshot(self) {
1334 self.rustc_snapshot_libdir()
1335 } else {
1336 match self.config.libdir_relative() {
1337 Some(relative_libdir) if compiler.stage >= 1 => {
1338 self.sysroot(compiler).join(relative_libdir)
1339 }
1340 _ => self.sysroot(compiler).join(libdir(compiler.host)),
1341 }
1342 }
1343 }
1344
1345 pub fn libdir_relative(&self, compiler: Compiler) -> &Path {
1351 if compiler.is_snapshot(self) {
1352 libdir(self.config.build).as_ref()
1353 } else {
1354 match self.config.libdir_relative() {
1355 Some(relative_libdir) if compiler.stage >= 1 => relative_libdir,
1356 _ => libdir(compiler.host).as_ref(),
1357 }
1358 }
1359 }
1360
1361 pub fn sysroot_libdir_relative(&self, compiler: Compiler) -> &Path {
1366 match self.config.libdir_relative() {
1367 Some(relative_libdir) if compiler.stage >= 1 => relative_libdir,
1368 _ if compiler.stage == 0 => &self.build.initial_relative_libdir,
1369 _ => Path::new("lib"),
1370 }
1371 }
1372
1373 pub fn rustc_lib_paths(&self, compiler: Compiler) -> Vec<PathBuf> {
1374 let mut dylib_dirs = vec![self.rustc_libdir(compiler)];
1375
1376 if self.config.llvm_from_ci {
1378 let ci_llvm_lib = self.out.join(compiler.host).join("ci-llvm").join("lib");
1379 dylib_dirs.push(ci_llvm_lib);
1380 }
1381
1382 dylib_dirs
1383 }
1384
1385 pub fn add_rustc_lib_path(&self, compiler: Compiler, cmd: &mut BootstrapCommand) {
1388 if cfg!(any(windows, target_os = "cygwin")) {
1392 return;
1393 }
1394
1395 add_dylib_path(self.rustc_lib_paths(compiler), cmd);
1396 }
1397
1398 pub fn rustc(&self, compiler: Compiler) -> PathBuf {
1400 if compiler.is_snapshot(self) {
1401 self.initial_rustc.clone()
1402 } else {
1403 self.sysroot(compiler).join("bin").join(exe("rustc", compiler.host))
1404 }
1405 }
1406
1407 fn codegen_backends(&self, compiler: Compiler) -> impl Iterator<Item = PathBuf> {
1409 fs::read_dir(self.sysroot_codegen_backends(compiler))
1410 .into_iter()
1411 .flatten()
1412 .filter_map(Result::ok)
1413 .map(|entry| entry.path())
1414 }
1415
1416 pub fn rustdoc(&self, compiler: Compiler) -> PathBuf {
1417 self.ensure(tool::Rustdoc { compiler }).tool_path
1418 }
1419
1420 pub fn cargo_clippy_cmd(&self, run_compiler: Compiler) -> BootstrapCommand {
1421 if run_compiler.stage == 0 {
1422 let cargo_clippy = self
1423 .config
1424 .initial_cargo_clippy
1425 .clone()
1426 .unwrap_or_else(|| self.build.config.download_clippy());
1427
1428 let mut cmd = command(cargo_clippy);
1429 cmd.env("CARGO", &self.initial_cargo);
1430 return cmd;
1431 }
1432
1433 let _ = self.ensure(tool::Clippy { compiler: run_compiler, target: self.build.build });
1434 let cargo_clippy =
1435 self.ensure(tool::CargoClippy { compiler: run_compiler, target: self.build.build });
1436 let mut dylib_path = helpers::dylib_path();
1437 dylib_path.insert(0, self.sysroot(run_compiler).join("lib"));
1438
1439 let mut cmd = command(cargo_clippy.tool_path);
1440 cmd.env(helpers::dylib_path_var(), env::join_paths(&dylib_path).unwrap());
1441 cmd.env("CARGO", &self.initial_cargo);
1442 cmd
1443 }
1444
1445 pub fn cargo_miri_cmd(&self, run_compiler: Compiler) -> BootstrapCommand {
1446 assert!(run_compiler.stage > 0, "miri can not be invoked at stage 0");
1447 let miri = self.ensure(tool::Miri { compiler: run_compiler, target: self.build.build });
1449 let cargo_miri =
1450 self.ensure(tool::CargoMiri { compiler: run_compiler, target: self.build.build });
1451 let mut cmd = command(cargo_miri.tool_path);
1453 cmd.env("MIRI", &miri.tool_path);
1454 cmd.env("CARGO", &self.initial_cargo);
1455 add_dylib_path(self.rustc_lib_paths(run_compiler), &mut cmd);
1464 cmd
1465 }
1466
1467 pub fn rustdoc_cmd(&self, compiler: Compiler) -> BootstrapCommand {
1468 let mut cmd = command(self.bootstrap_out.join("rustdoc"));
1469 cmd.env("RUSTC_STAGE", compiler.stage.to_string())
1470 .env("RUSTC_SYSROOT", self.sysroot(compiler))
1471 .env("RUSTDOC_LIBDIR", self.rustc_libdir(compiler))
1474 .env("CFG_RELEASE_CHANNEL", &self.config.channel)
1475 .env("RUSTDOC_REAL", self.rustdoc(compiler))
1476 .env("RUSTC_BOOTSTRAP", "1");
1477
1478 cmd.arg("-Wrustdoc::invalid_codeblock_attributes");
1479
1480 if self.config.deny_warnings {
1481 cmd.arg("-Dwarnings");
1482 }
1483 cmd.arg("-Znormalize-docs");
1484 cmd.args(linker_args(self, compiler.host, LldThreads::Yes));
1485 cmd
1486 }
1487
1488 pub fn llvm_config(&self, target: TargetSelection) -> Option<PathBuf> {
1493 if self.config.llvm_enabled(target) && self.kind != Kind::Check && !self.config.dry_run() {
1494 let llvm::LlvmResult { llvm_config, .. } = self.ensure(llvm::Llvm { target });
1495 if llvm_config.is_file() {
1496 return Some(llvm_config);
1497 }
1498 }
1499 None
1500 }
1501
1502 pub fn ensure<S: Step>(&'a self, step: S) -> S::Output {
1506 {
1507 let mut stack = self.stack.borrow_mut();
1508 for stack_step in stack.iter() {
1509 if stack_step.downcast_ref::<S>().is_none_or(|stack_step| *stack_step != step) {
1511 continue;
1512 }
1513 let mut out = String::new();
1514 out += &format!("\n\nCycle in build detected when adding {step:?}\n");
1515 for el in stack.iter().rev() {
1516 out += &format!("\t{el:?}\n");
1517 }
1518 panic!("{}", out);
1519 }
1520 if let Some(out) = self.cache.get(&step) {
1521 self.verbose_than(1, || println!("{}c {:?}", " ".repeat(stack.len()), step));
1522
1523 return out;
1524 }
1525 self.verbose_than(1, || println!("{}> {:?}", " ".repeat(stack.len()), step));
1526 stack.push(Box::new(step.clone()));
1527 }
1528
1529 #[cfg(feature = "build-metrics")]
1530 self.metrics.enter_step(&step, self);
1531
1532 let (out, dur) = {
1533 let start = Instant::now();
1534 let zero = Duration::new(0, 0);
1535 let parent = self.time_spent_on_dependencies.replace(zero);
1536 let out = step.clone().run(self);
1537 let dur = start.elapsed();
1538 let deps = self.time_spent_on_dependencies.replace(parent + dur);
1539 (out, dur.saturating_sub(deps))
1540 };
1541
1542 if self.config.print_step_timings && !self.config.dry_run() {
1543 let step_string = format!("{step:?}");
1544 let brace_index = step_string.find('{').unwrap_or(0);
1545 let type_string = type_name::<S>();
1546 println!(
1547 "[TIMING] {} {} -- {}.{:03}",
1548 &type_string.strip_prefix("bootstrap::").unwrap_or(type_string),
1549 &step_string[brace_index..],
1550 dur.as_secs(),
1551 dur.subsec_millis()
1552 );
1553 }
1554
1555 #[cfg(feature = "build-metrics")]
1556 self.metrics.exit_step(self);
1557
1558 {
1559 let mut stack = self.stack.borrow_mut();
1560 let cur_step = stack.pop().expect("step stack empty");
1561 assert_eq!(cur_step.downcast_ref(), Some(&step));
1562 }
1563 self.verbose_than(1, || println!("{}< {:?}", " ".repeat(self.stack.borrow().len()), step));
1564 self.cache.put(step, out.clone());
1565 out
1566 }
1567
1568 pub(crate) fn ensure_if_default<T, S: Step<Output = Option<T>>>(
1572 &'a self,
1573 step: S,
1574 kind: Kind,
1575 ) -> S::Output {
1576 let desc = StepDescription::from::<S>(kind);
1577 let should_run = (desc.should_run)(ShouldRun::new(self, desc.kind));
1578
1579 for pathset in &should_run.paths {
1581 if desc.is_excluded(self, pathset) {
1582 return None;
1583 }
1584 }
1585
1586 if desc.default && should_run.is_really_default() { self.ensure(step) } else { None }
1588 }
1589
1590 pub(crate) fn was_invoked_explicitly<S: Step>(&'a self, kind: Kind) -> bool {
1592 let desc = StepDescription::from::<S>(kind);
1593 let should_run = (desc.should_run)(ShouldRun::new(self, desc.kind));
1594
1595 for path in &self.paths {
1596 if should_run.paths.iter().any(|s| s.has(path, desc.kind))
1597 && !desc.is_excluded(
1598 self,
1599 &PathSet::Suite(TaskPath { path: path.clone(), kind: Some(desc.kind) }),
1600 )
1601 {
1602 return true;
1603 }
1604 }
1605
1606 false
1607 }
1608
1609 pub(crate) fn maybe_open_in_browser<S: Step>(&self, path: impl AsRef<Path>) {
1610 if self.was_invoked_explicitly::<S>(Kind::Doc) {
1611 self.open_in_browser(path);
1612 } else {
1613 self.info(&format!("Doc path: {}", path.as_ref().display()));
1614 }
1615 }
1616
1617 pub(crate) fn open_in_browser(&self, path: impl AsRef<Path>) {
1618 let path = path.as_ref();
1619
1620 if self.config.dry_run() || !self.config.cmd.open() {
1621 self.info(&format!("Doc path: {}", path.display()));
1622 return;
1623 }
1624
1625 self.info(&format!("Opening doc {}", path.display()));
1626 if let Err(err) = opener::open(path) {
1627 self.info(&format!("{err}\n"));
1628 }
1629 }
1630}