1#![cfg_attr(test, allow(unused))]
19
20use std::cell::Cell;
21use std::collections::{BTreeSet, HashMap, HashSet};
22use std::fmt::Display;
23use std::path::{Path, PathBuf};
24use std::sync::OnceLock;
25use std::time::{Instant, SystemTime};
26use std::{env, fs, io, str};
27
28use build_helper::ci::gha;
29use build_helper::exit;
30use cc::Tool;
31use termcolor::{ColorChoice, StandardStream, WriteColor};
32use utils::build_stamp::BuildStamp;
33use utils::channel::GitInfo;
34use utils::exec::ExecutionContext;
35
36use crate::core::builder;
37use crate::core::builder::Kind;
38use crate::core::config::{BootstrapOverrideLld, DryRun, LlvmLibunwind, TargetSelection, flags};
39use crate::utils::exec::{BootstrapCommand, command};
40use crate::utils::helpers::{self, dir_is_empty, exe, libdir, set_file_times, split_debuginfo};
41
42mod core;
43mod utils;
44
45pub use core::builder::PathSet;
46#[cfg(feature = "tracing")]
47pub use core::builder::STEP_SPAN_TARGET;
48pub use core::config::flags::{Flags, Subcommand};
49pub use core::config::{ChangeId, Config};
50
51#[cfg(feature = "tracing")]
52use tracing::{instrument, span};
53pub use utils::change_tracker::{
54 CONFIG_CHANGE_HISTORY, find_recent_config_change_ids, human_readable_changes,
55};
56pub use utils::helpers::{PanicTracker, symlink_dir};
57#[cfg(feature = "tracing")]
58pub use utils::tracing::setup_tracing;
59
60use crate::core::build_steps::vendor::VENDOR_DIR;
61
62const LLVM_TOOLS: &[&str] = &[
63 "llvm-cov", "llvm-nm", "llvm-objcopy", "llvm-objdump", "llvm-profdata", "llvm-readobj", "llvm-size", "llvm-strip", "llvm-ar", "llvm-as", "llvm-dis", "llvm-link", "llc", "opt", ];
78
79const LLD_FILE_NAMES: &[&str] = &["ld.lld", "ld64.lld", "lld-link", "wasm-ld"];
81
82#[expect(clippy::type_complexity)] const EXTRA_CHECK_CFGS: &[(Option<Mode>, &str, Option<&[&'static str]>)] = &[
86 (Some(Mode::Rustc), "bootstrap", None),
87 (Some(Mode::Codegen), "bootstrap", None),
88 (Some(Mode::ToolRustcPrivate), "bootstrap", None),
89 (Some(Mode::ToolStd), "bootstrap", None),
90 (Some(Mode::ToolRustcPrivate), "rust_analyzer", None),
91 (Some(Mode::ToolStd), "rust_analyzer", None),
92 ];
96
97#[derive(Eq, PartialOrd, Ord, Clone, Copy, Debug)]
103pub struct Compiler {
104 stage: u32,
105 host: TargetSelection,
106 forced_compiler: bool,
110}
111
112impl std::hash::Hash for Compiler {
113 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
114 self.stage.hash(state);
115 self.host.hash(state);
116 }
117}
118
119impl PartialEq for Compiler {
120 fn eq(&self, other: &Self) -> bool {
121 self.stage == other.stage && self.host == other.host
122 }
123}
124
125#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
127pub enum CodegenBackendKind {
128 #[default]
129 Llvm,
130 Cranelift,
131 Gcc,
132 Custom(String),
133}
134
135impl CodegenBackendKind {
136 pub fn name(&self) -> &str {
139 match self {
140 CodegenBackendKind::Llvm => "llvm",
141 CodegenBackendKind::Cranelift => "cranelift",
142 CodegenBackendKind::Gcc => "gcc",
143 CodegenBackendKind::Custom(name) => name,
144 }
145 }
146
147 pub fn crate_name(&self) -> String {
149 format!("rustc_codegen_{}", self.name())
150 }
151
152 pub fn is_llvm(&self) -> bool {
153 matches!(self, Self::Llvm)
154 }
155
156 pub fn is_cranelift(&self) -> bool {
157 matches!(self, Self::Cranelift)
158 }
159
160 pub fn is_gcc(&self) -> bool {
161 matches!(self, Self::Gcc)
162 }
163}
164
165impl std::str::FromStr for CodegenBackendKind {
166 type Err = &'static str;
167
168 fn from_str(s: &str) -> Result<Self, Self::Err> {
169 match s.to_lowercase().as_str() {
170 "" => Err("Invalid empty backend name"),
171 "gcc" => Ok(Self::Gcc),
172 "llvm" => Ok(Self::Llvm),
173 "cranelift" => Ok(Self::Cranelift),
174 _ => Ok(Self::Custom(s.to_string())),
175 }
176 }
177}
178
179#[derive(PartialEq, Eq, Copy, Clone, Debug)]
180pub enum TestTarget {
181 Default,
183 AllTargets,
185 DocOnly,
187 Tests,
189}
190
191impl TestTarget {
192 fn runs_doctests(&self) -> bool {
193 matches!(self, TestTarget::DocOnly | TestTarget::Default)
194 }
195}
196
197pub enum GitRepo {
198 Rustc,
199 Llvm,
200}
201
202pub struct Build {
213 config: Config,
215
216 version: String,
218
219 src: PathBuf,
221 out: PathBuf,
222 bootstrap_out: PathBuf,
223 cargo_info: GitInfo,
224 rust_analyzer_info: GitInfo,
225 clippy_info: GitInfo,
226 miri_info: GitInfo,
227 rustfmt_info: GitInfo,
228 enzyme_info: GitInfo,
229 in_tree_llvm_info: GitInfo,
230 in_tree_gcc_info: GitInfo,
231 local_rebuild: bool,
232 fail_fast: bool,
233 test_target: TestTarget,
234 verbosity: usize,
235
236 host_target: TargetSelection,
238 hosts: Vec<TargetSelection>,
240 targets: Vec<TargetSelection>,
242
243 initial_rustc: PathBuf,
244 initial_rustdoc: PathBuf,
245 initial_cargo: PathBuf,
246 initial_lld: PathBuf,
247 initial_relative_libdir: PathBuf,
248 initial_sysroot: PathBuf,
249
250 cc: HashMap<TargetSelection, cc::Tool>,
253 cxx: HashMap<TargetSelection, cc::Tool>,
254 ar: HashMap<TargetSelection, PathBuf>,
255 ranlib: HashMap<TargetSelection, PathBuf>,
256 wasi_sdk_path: Option<PathBuf>,
257
258 crates: HashMap<String, Crate>,
261 crate_paths: HashMap<PathBuf, String>,
262 is_sudo: bool,
263 prerelease_version: Cell<Option<u32>>,
264
265 #[cfg(feature = "build-metrics")]
266 metrics: crate::utils::metrics::BuildMetrics,
267
268 #[cfg(feature = "tracing")]
269 step_graph: std::cell::RefCell<crate::utils::step_graph::StepGraph>,
270}
271
272#[derive(Debug, Clone)]
273struct Crate {
274 name: String,
275 deps: HashSet<String>,
276 path: PathBuf,
277 features: Vec<String>,
278}
279
280impl Crate {
281 fn local_path(&self, build: &Build) -> PathBuf {
282 self.path.strip_prefix(&build.config.src).unwrap().into()
283 }
284}
285
286#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
288pub enum DependencyType {
289 Host,
291 Target,
293 TargetSelfContained,
295}
296
297#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
302pub enum Mode {
303 Std,
305
306 Rustc,
308
309 Codegen,
311
312 ToolBootstrap,
324
325 ToolTarget,
336
337 ToolStd,
341
342 ToolRustcPrivate,
348}
349
350impl Mode {
351 pub fn must_support_dlopen(&self) -> bool {
352 match self {
353 Mode::Std | Mode::Codegen => true,
354 Mode::ToolBootstrap
355 | Mode::ToolRustcPrivate
356 | Mode::ToolStd
357 | Mode::ToolTarget
358 | Mode::Rustc => false,
359 }
360 }
361}
362
363pub enum RemapScheme {
367 Compiler,
369 NonCompiler,
371}
372
373#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
374pub enum CLang {
375 C,
376 Cxx,
377}
378
379#[derive(Debug, Clone, Copy, PartialEq, Eq)]
380pub enum FileType {
381 Executable,
383 NativeLibrary,
385 Script,
387 Regular,
389}
390
391impl FileType {
392 pub fn perms(self) -> u32 {
394 match self {
395 FileType::Executable | FileType::Script => 0o755,
396 FileType::Regular | FileType::NativeLibrary => 0o644,
397 }
398 }
399
400 pub fn could_have_split_debuginfo(self) -> bool {
401 match self {
402 FileType::Executable | FileType::NativeLibrary => true,
403 FileType::Script | FileType::Regular => false,
404 }
405 }
406}
407
408macro_rules! forward {
409 ( $( $fn:ident( $($param:ident: $ty:ty),* ) $( -> $ret:ty)? ),+ $(,)? ) => {
410 impl Build {
411 $( fn $fn(&self, $($param: $ty),* ) $( -> $ret)? {
412 self.config.$fn( $($param),* )
413 } )+
414 }
415 }
416}
417
418forward! {
419 do_if_verbose(f: impl Fn()),
420 is_verbose() -> bool,
421 create(path: &Path, s: &str),
422 remove(f: &Path),
423 tempdir() -> PathBuf,
424 llvm_link_shared() -> bool,
425 download_rustc() -> bool,
426}
427
428struct TargetAndStage {
431 target: TargetSelection,
432 stage: u32,
433}
434
435impl From<(TargetSelection, u32)> for TargetAndStage {
436 fn from((target, stage): (TargetSelection, u32)) -> Self {
437 Self { target, stage }
438 }
439}
440
441impl From<Compiler> for TargetAndStage {
442 fn from(compiler: Compiler) -> Self {
443 Self { target: compiler.host, stage: compiler.stage }
444 }
445}
446
447impl Build {
448 pub fn new(mut config: Config) -> Build {
453 let src = config.src.clone();
454 let out = config.out.clone();
455
456 #[cfg(unix)]
457 let is_sudo = match env::var_os("SUDO_USER") {
460 Some(_sudo_user) => {
461 let uid = unsafe { libc::getuid() };
466 uid == 0
467 }
468 None => false,
469 };
470 #[cfg(not(unix))]
471 let is_sudo = false;
472
473 let rust_info = config.rust_info.clone();
474 let cargo_info = config.cargo_info.clone();
475 let rust_analyzer_info = config.rust_analyzer_info.clone();
476 let clippy_info = config.clippy_info.clone();
477 let miri_info = config.miri_info.clone();
478 let rustfmt_info = config.rustfmt_info.clone();
479 let enzyme_info = config.enzyme_info.clone();
480 let in_tree_llvm_info = config.in_tree_llvm_info.clone();
481 let in_tree_gcc_info = config.in_tree_gcc_info.clone();
482
483 let initial_target_libdir = command(&config.initial_rustc)
484 .run_in_dry_run()
485 .args(["--print", "target-libdir"])
486 .run_capture_stdout(&config)
487 .stdout()
488 .trim()
489 .to_owned();
490
491 let initial_target_dir = Path::new(&initial_target_libdir)
492 .parent()
493 .unwrap_or_else(|| panic!("{initial_target_libdir} has no parent"));
494
495 let initial_lld = initial_target_dir.join("bin").join("rust-lld");
496
497 let initial_relative_libdir = if cfg!(test) {
498 PathBuf::default()
500 } else {
501 let ancestor = initial_target_dir.ancestors().nth(2).unwrap_or_else(|| {
502 panic!("Not enough ancestors for {}", initial_target_dir.display())
503 });
504
505 ancestor
506 .strip_prefix(&config.initial_sysroot)
507 .unwrap_or_else(|_| {
508 panic!(
509 "Couldn’t resolve the initial relative libdir from {}",
510 initial_target_dir.display()
511 )
512 })
513 .to_path_buf()
514 };
515
516 let version = std::fs::read_to_string(src.join("src").join("version"))
517 .expect("failed to read src/version");
518 let version = version.trim();
519
520 let mut bootstrap_out = std::env::current_exe()
521 .expect("could not determine path to running process")
522 .parent()
523 .unwrap()
524 .to_path_buf();
525 if bootstrap_out.ends_with("deps") {
528 bootstrap_out.pop();
529 }
530 if !bootstrap_out.join(exe("rustc", config.host_target)).exists() && !cfg!(test) {
531 panic!(
533 "`rustc` not found in {}, run `cargo build --bins` before `cargo run`",
534 bootstrap_out.display()
535 )
536 }
537
538 if rust_info.is_from_tarball() && config.description.is_none() {
539 config.description = Some("built from a source tarball".to_owned());
540 }
541
542 let mut build = Build {
543 initial_lld,
544 initial_relative_libdir,
545 initial_rustc: config.initial_rustc.clone(),
546 initial_rustdoc: config.initial_rustdoc.clone(),
547 initial_cargo: config.initial_cargo.clone(),
548 initial_sysroot: config.initial_sysroot.clone(),
549 local_rebuild: config.local_rebuild,
550 fail_fast: config.cmd.fail_fast(),
551 test_target: config.cmd.test_target(),
552 verbosity: config.exec_ctx.verbosity as usize,
553
554 host_target: config.host_target,
555 hosts: config.hosts.clone(),
556 targets: config.targets.clone(),
557
558 config,
559 version: version.to_string(),
560 src,
561 out,
562 bootstrap_out,
563
564 cargo_info,
565 rust_analyzer_info,
566 clippy_info,
567 miri_info,
568 rustfmt_info,
569 enzyme_info,
570 in_tree_llvm_info,
571 in_tree_gcc_info,
572 cc: HashMap::new(),
573 cxx: HashMap::new(),
574 ar: HashMap::new(),
575 ranlib: HashMap::new(),
576 wasi_sdk_path: env::var_os("WASI_SDK_PATH").map(PathBuf::from),
577 crates: HashMap::new(),
578 crate_paths: HashMap::new(),
579 is_sudo,
580 prerelease_version: Cell::new(None),
581
582 #[cfg(feature = "build-metrics")]
583 metrics: crate::utils::metrics::BuildMetrics::init(),
584
585 #[cfg(feature = "tracing")]
586 step_graph: std::cell::RefCell::new(crate::utils::step_graph::StepGraph::default()),
587 };
588
589 let local_version_verbose = command(&build.initial_rustc)
592 .run_in_dry_run()
593 .args(["--version", "--verbose"])
594 .run_capture_stdout(&build)
595 .stdout();
596 let local_release = local_version_verbose
597 .lines()
598 .filter_map(|x| x.strip_prefix("release:"))
599 .next()
600 .unwrap()
601 .trim();
602 if local_release.split('.').take(2).eq(version.split('.').take(2)) {
603 build.do_if_verbose(|| println!("auto-detected local-rebuild {local_release}"));
604 build.local_rebuild = true;
605 }
606
607 build.do_if_verbose(|| println!("finding compilers"));
608 utils::cc_detect::fill_compilers(&mut build);
609 if !matches!(build.config.cmd, Subcommand::Setup { .. }) {
615 build.do_if_verbose(|| println!("running sanity check"));
616 crate::core::sanity::check(&mut build);
617
618 let rust_submodules = ["library/backtrace"];
621 for s in rust_submodules {
622 build.require_submodule(
623 s,
624 Some(
625 "The submodule is required for the standard library \
626 and the main Cargo workspace.",
627 ),
628 );
629 }
630 build.update_existing_submodules();
632
633 build.do_if_verbose(|| println!("learning about cargo"));
634 crate::core::metadata::build(&mut build);
635 }
636
637 let build_triple = build.out.join(build.host_target);
639 t!(fs::create_dir_all(&build_triple));
640 let host = build.out.join("host");
641 if host.is_symlink() {
642 #[cfg(windows)]
645 t!(fs::remove_dir(&host));
646 #[cfg(not(windows))]
647 t!(fs::remove_file(&host));
648 }
649 t!(
650 symlink_dir(&build.config, &build_triple, &host),
651 format!("symlink_dir({} => {}) failed", host.display(), build_triple.display())
652 );
653
654 build
655 }
656
657 #[cfg_attr(
666 feature = "tracing",
667 instrument(
668 level = "trace",
669 name = "Build::require_submodule",
670 skip_all,
671 fields(submodule = submodule),
672 ),
673 )]
674 pub fn require_submodule(&self, submodule: &str, err_hint: Option<&str>) {
675 if self.rust_info().is_from_tarball() {
676 return;
677 }
678
679 if self.config.dry_run() {
680 return;
681 }
682
683 if cfg!(test) && !self.config.submodules() {
686 return;
687 }
688 self.config.update_submodule(submodule);
689 let absolute_path = self.config.src.join(submodule);
690 if !absolute_path.exists() || dir_is_empty(&absolute_path) {
691 let maybe_enable = if !self.config.submodules()
692 && self.config.rust_info.is_managed_git_subrepository()
693 {
694 "\nConsider setting `build.submodules = true` or manually initializing the submodules."
695 } else {
696 ""
697 };
698 let err_hint = err_hint.map_or_else(String::new, |e| format!("\n{e}"));
699 eprintln!(
700 "submodule {submodule} does not appear to be checked out, \
701 but it is required for this step{maybe_enable}{err_hint}"
702 );
703 exit!(1);
704 }
705 }
706
707 fn update_existing_submodules(&self) {
710 if !self.config.submodules() {
713 return;
714 }
715 let output = helpers::git(Some(&self.src))
716 .args(["config", "--file"])
717 .arg(".gitmodules")
718 .args(["--get-regexp", "path"])
719 .run_capture(self)
720 .stdout();
721 std::thread::scope(|s| {
722 for line in output.lines() {
725 let submodule = line.split_once(' ').unwrap().1;
726 let config = self.config.clone();
727 s.spawn(move || {
728 Self::update_existing_submodule(&config, submodule);
729 });
730 }
731 });
732 }
733
734 pub fn update_existing_submodule(config: &Config, submodule: &str) {
736 if !config.submodules() {
738 return;
739 }
740
741 if config.git_info(false, Path::new(submodule)).is_managed_git_subrepository() {
742 config.update_submodule(submodule);
743 }
744 }
745
746 #[cfg_attr(feature = "tracing", instrument(level = "debug", name = "Build::build", skip_all))]
748 pub fn build(&mut self) {
749 trace!("setting up job management");
750 unsafe {
751 crate::utils::job::setup(self);
752 }
753
754 {
756 #[cfg(feature = "tracing")]
757 let _hardcoded_span =
758 span!(tracing::Level::DEBUG, "handling hardcoded subcommands (Format, Perf)")
759 .entered();
760
761 match &self.config.cmd {
762 Subcommand::Format { check, all } => {
763 return core::build_steps::format::format(
764 &builder::Builder::new(self),
765 *check,
766 *all,
767 &self.config.paths,
768 );
769 }
770 Subcommand::Perf(args) => {
771 return core::build_steps::perf::perf(&builder::Builder::new(self), args);
772 }
773 _cmd => {
774 debug!(cmd = ?_cmd, "not a hardcoded subcommand; returning to normal handling");
775 }
776 }
777
778 debug!("handling subcommand normally");
779 }
780
781 if !self.config.dry_run() {
782 #[cfg(feature = "tracing")]
783 let _real_run_span = span!(tracing::Level::DEBUG, "executing real run").entered();
784
785 {
788 #[cfg(feature = "tracing")]
789 let _sanity_check_span =
790 span!(tracing::Level::DEBUG, "(1) executing dry-run sanity-check").entered();
791 self.config.set_dry_run(DryRun::SelfCheck);
792 let builder = builder::Builder::new(self);
793 builder.execute_cli();
794 }
795
796 {
798 #[cfg(feature = "tracing")]
799 let _actual_run_span =
800 span!(tracing::Level::DEBUG, "(2) executing actual run").entered();
801 self.config.set_dry_run(DryRun::Disabled);
802 let builder = builder::Builder::new(self);
803 builder.execute_cli();
804 }
805 } else {
806 #[cfg(feature = "tracing")]
807 let _dry_run_span = span!(tracing::Level::DEBUG, "executing dry run").entered();
808
809 let builder = builder::Builder::new(self);
810 builder.execute_cli();
811 }
812
813 #[cfg(feature = "tracing")]
814 debug!("checking for postponed test failures from `test --no-fail-fast`");
815
816 self.config.exec_ctx().report_failures_and_exit();
818
819 #[cfg(feature = "build-metrics")]
820 self.metrics.persist(self);
821 }
822
823 fn rust_info(&self) -> &GitInfo {
824 &self.config.rust_info
825 }
826
827 fn std_features(&self, target: TargetSelection) -> String {
830 let mut features: BTreeSet<&str> =
831 self.config.rust_std_features.iter().map(|s| s.as_str()).collect();
832
833 match self.config.llvm_libunwind(target) {
834 LlvmLibunwind::InTree => features.insert("llvm-libunwind"),
835 LlvmLibunwind::System => features.insert("system-llvm-libunwind"),
836 LlvmLibunwind::No => false,
837 };
838
839 if self.config.backtrace {
840 features.insert("backtrace");
841 }
842
843 if self.config.profiler_enabled(target) {
844 features.insert("profiler");
845 }
846
847 if target.contains("zkvm") {
849 features.insert("compiler-builtins-mem");
850 }
851
852 if self.config.llvm_enzyme {
853 features.insert("llvm_enzyme");
854 }
855
856 features.into_iter().collect::<Vec<_>>().join(" ")
857 }
858
859 fn rustc_features(&self, kind: Kind, target: TargetSelection, crates: &[String]) -> String {
861 let possible_features_by_crates: HashSet<_> = crates
862 .iter()
863 .flat_map(|krate| &self.crates[krate].features)
864 .map(std::ops::Deref::deref)
865 .collect();
866 let check = |feature: &str| -> bool {
867 crates.is_empty() || possible_features_by_crates.contains(feature)
868 };
869 let mut features = vec![];
870 if self.config.jemalloc(target) && check("jemalloc") {
871 features.push("jemalloc");
872 }
873 if (self.config.llvm_enabled(target) || kind == Kind::Check) && check("llvm") {
874 features.push("llvm");
875 }
876 if self.config.llvm_enzyme {
877 features.push("llvm_enzyme");
878 }
879 if self.config.llvm_offload {
880 features.push("llvm_offload");
881 }
882 if self.config.rust_randomize_layout && check("rustc_randomized_layouts") {
884 features.push("rustc_randomized_layouts");
885 }
886 if self.config.compile_time_deps && kind == Kind::Check {
887 features.push("check_only");
888 }
889
890 if crates.iter().any(|c| c == "rustc_transmute") {
891 features.push("rustc");
894 }
895
896 if !self.config.rust_debug_logging && check("max_level_info") {
902 features.push("max_level_info");
903 }
904
905 features.join(" ")
906 }
907
908 fn cargo_dir(&self, mode: Mode) -> &'static str {
911 match (mode, self.config.rust_optimize.is_release()) {
912 (Mode::Std, _) => "dist",
913 (_, true) => "release",
914 (_, false) => "debug",
915 }
916 }
917
918 fn tools_dir(&self, build_compiler: Compiler) -> PathBuf {
919 let out = self
920 .out
921 .join(build_compiler.host)
922 .join(format!("stage{}-tools-bin", build_compiler.stage + 1));
923 t!(fs::create_dir_all(&out));
924 out
925 }
926
927 fn stage_out(&self, build_compiler: Compiler, mode: Mode) -> PathBuf {
932 use std::fmt::Write;
933
934 fn bootstrap_tool() -> (Option<u32>, &'static str) {
935 (None, "bootstrap-tools")
936 }
937 fn staged_tool(build_compiler: Compiler) -> (Option<u32>, &'static str) {
938 (Some(build_compiler.stage + 1), "tools")
939 }
940
941 let (stage, suffix) = match mode {
942 Mode::Std => (Some(build_compiler.stage), "std"),
944 Mode::Rustc => (Some(build_compiler.stage + 1), "rustc"),
946 Mode::Codegen => (Some(build_compiler.stage + 1), "codegen"),
947 Mode::ToolBootstrap => bootstrap_tool(),
948 Mode::ToolStd | Mode::ToolRustcPrivate => (Some(build_compiler.stage + 1), "tools"),
949 Mode::ToolTarget => {
950 if build_compiler.stage == 0 {
953 bootstrap_tool()
954 } else {
955 staged_tool(build_compiler)
956 }
957 }
958 };
959 let path = self.out.join(build_compiler.host);
960 let mut dir_name = String::new();
961 if let Some(stage) = stage {
962 write!(dir_name, "stage{stage}-").unwrap();
963 }
964 dir_name.push_str(suffix);
965 path.join(dir_name)
966 }
967
968 fn cargo_out(&self, build_compiler: Compiler, mode: Mode, target: TargetSelection) -> PathBuf {
972 self.stage_out(build_compiler, mode).join(target).join(self.cargo_dir(mode))
973 }
974
975 fn llvm_out(&self, target: TargetSelection) -> PathBuf {
980 if self.config.llvm_from_ci && self.config.is_host_target(target) {
981 self.config.ci_llvm_root()
982 } else {
983 self.out.join(target).join("llvm")
984 }
985 }
986
987 fn enzyme_out(&self, target: TargetSelection) -> PathBuf {
988 self.out.join(&*target.triple).join("enzyme")
989 }
990
991 fn offload_out(&self, target: TargetSelection) -> PathBuf {
992 self.out.join(&*target.triple).join("offload")
993 }
994
995 fn lld_out(&self, target: TargetSelection) -> PathBuf {
996 self.out.join(target).join("lld")
997 }
998
999 fn doc_out(&self, target: TargetSelection) -> PathBuf {
1001 self.out.join(target).join("doc")
1002 }
1003
1004 fn json_doc_out(&self, target: TargetSelection) -> PathBuf {
1006 self.out.join(target).join("json-doc")
1007 }
1008
1009 fn test_out(&self, target: TargetSelection) -> PathBuf {
1010 self.out.join(target).join("test")
1011 }
1012
1013 fn compiler_doc_out(&self, target: TargetSelection) -> PathBuf {
1015 self.out.join(target).join("compiler-doc")
1016 }
1017
1018 fn md_doc_out(&self, target: TargetSelection) -> PathBuf {
1020 self.out.join(target).join("md-doc")
1021 }
1022
1023 fn vendored_crates_path(&self) -> Option<PathBuf> {
1025 if self.config.vendor { Some(self.src.join(VENDOR_DIR)) } else { None }
1026 }
1027
1028 fn llvm_filecheck(&self, target: TargetSelection) -> PathBuf {
1030 let target_config = self.config.target_config.get(&target);
1031 if let Some(s) = target_config.and_then(|c| c.llvm_filecheck.as_ref()) {
1032 s.to_path_buf()
1033 } else if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
1034 let llvm_bindir = command(s).arg("--bindir").run_capture_stdout(self).stdout();
1035 let filecheck = Path::new(llvm_bindir.trim()).join(exe("FileCheck", target));
1036 if filecheck.exists() {
1037 filecheck
1038 } else {
1039 let llvm_libdir = command(s).arg("--libdir").run_capture_stdout(self).stdout();
1042 let lib_filecheck =
1043 Path::new(llvm_libdir.trim()).join("llvm").join(exe("FileCheck", target));
1044 if lib_filecheck.exists() {
1045 lib_filecheck
1046 } else {
1047 filecheck
1051 }
1052 }
1053 } else {
1054 let base = self.llvm_out(target).join("build");
1055 let base = if !self.ninja() && target.is_msvc() {
1056 if self.config.llvm_optimize {
1057 if self.config.llvm_release_debuginfo {
1058 base.join("RelWithDebInfo")
1059 } else {
1060 base.join("Release")
1061 }
1062 } else {
1063 base.join("Debug")
1064 }
1065 } else {
1066 base
1067 };
1068 base.join("bin").join(exe("FileCheck", target))
1069 }
1070 }
1071
1072 fn native_dir(&self, target: TargetSelection) -> PathBuf {
1074 self.out.join(target).join("native")
1075 }
1076
1077 fn test_helpers_out(&self, target: TargetSelection) -> PathBuf {
1080 self.native_dir(target).join("rust-test-helpers")
1081 }
1082
1083 fn add_rust_test_threads(&self, cmd: &mut BootstrapCommand) {
1085 if env::var_os("RUST_TEST_THREADS").is_none() {
1086 cmd.env("RUST_TEST_THREADS", self.jobs().to_string());
1087 }
1088 }
1089
1090 fn rustc_snapshot_libdir(&self) -> PathBuf {
1092 self.rustc_snapshot_sysroot().join(libdir(self.config.host_target))
1093 }
1094
1095 fn rustc_snapshot_sysroot(&self) -> &Path {
1097 static SYSROOT_CACHE: OnceLock<PathBuf> = OnceLock::new();
1098 SYSROOT_CACHE.get_or_init(|| {
1099 command(&self.initial_rustc)
1100 .run_in_dry_run()
1101 .args(["--print", "sysroot"])
1102 .run_capture_stdout(self)
1103 .stdout()
1104 .trim()
1105 .to_owned()
1106 .into()
1107 })
1108 }
1109
1110 fn info(&self, msg: &str) {
1111 match self.config.get_dry_run() {
1112 DryRun::SelfCheck => (),
1113 DryRun::Disabled | DryRun::UserSelected => {
1114 println!("{msg}");
1115 }
1116 }
1117 }
1118
1119 #[must_use = "Groups should not be dropped until the Step finishes running"]
1131 #[track_caller]
1132 fn msg(
1133 &self,
1134 action: impl Into<Kind>,
1135 what: impl Display,
1136 mode: impl Into<Option<Mode>>,
1137 target_and_stage: impl Into<TargetAndStage>,
1138 target: impl Into<Option<TargetSelection>>,
1139 ) -> Option<gha::Group> {
1140 let target_and_stage = target_and_stage.into();
1141 let action = action.into();
1142 assert!(
1143 action != Kind::Test,
1144 "Please use `Build::msg_test` instead of `Build::msg(Kind::Test)`"
1145 );
1146
1147 let actual_stage = match mode.into() {
1148 Some(Mode::Std) => target_and_stage.stage,
1150 Some(
1152 Mode::Rustc
1153 | Mode::Codegen
1154 | Mode::ToolBootstrap
1155 | Mode::ToolTarget
1156 | Mode::ToolStd
1157 | Mode::ToolRustcPrivate,
1158 )
1159 | None => target_and_stage.stage + 1,
1160 };
1161
1162 let action = action.description();
1163 let what = what.to_string();
1164 let msg = |fmt| {
1165 let space = if !what.is_empty() { " " } else { "" };
1166 format!("{action} stage{actual_stage} {what}{space}{fmt}")
1167 };
1168 let msg = if let Some(target) = target.into() {
1169 let build_stage = target_and_stage.stage;
1170 let host = target_and_stage.target;
1171 if host == target {
1172 msg(format_args!("(stage{build_stage} -> stage{actual_stage}, {target})"))
1173 } else {
1174 msg(format_args!("(stage{build_stage}:{host} -> stage{actual_stage}:{target})"))
1175 }
1176 } else {
1177 msg(format_args!(""))
1178 };
1179 self.group(&msg)
1180 }
1181
1182 #[must_use = "Groups should not be dropped until the Step finishes running"]
1188 #[track_caller]
1189 fn msg_test(
1190 &self,
1191 what: impl Display,
1192 target: TargetSelection,
1193 stage: u32,
1194 ) -> Option<gha::Group> {
1195 let action = Kind::Test.description();
1196 let msg = format!("{action} stage{stage} {what} ({target})");
1197 self.group(&msg)
1198 }
1199
1200 #[must_use = "Groups should not be dropped until the Step finishes running"]
1204 #[track_caller]
1205 fn msg_unstaged(
1206 &self,
1207 action: impl Into<Kind>,
1208 what: impl Display,
1209 target: TargetSelection,
1210 ) -> Option<gha::Group> {
1211 let action = action.into().description();
1212 let msg = format!("{action} {what} for {target}");
1213 self.group(&msg)
1214 }
1215
1216 #[track_caller]
1217 fn group(&self, msg: &str) -> Option<gha::Group> {
1218 match self.config.get_dry_run() {
1219 DryRun::SelfCheck => None,
1220 DryRun::Disabled | DryRun::UserSelected => Some(gha::group(msg)),
1221 }
1222 }
1223
1224 fn jobs(&self) -> u32 {
1227 self.config.jobs.unwrap_or_else(|| {
1228 std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get) as u32
1229 })
1230 }
1231
1232 fn debuginfo_map_to(&self, which: GitRepo, remap_scheme: RemapScheme) -> Option<String> {
1233 if !self.config.rust_remap_debuginfo {
1234 return None;
1235 }
1236
1237 match which {
1238 GitRepo::Rustc => {
1239 let sha = self.rust_sha().unwrap_or(&self.version);
1240
1241 match remap_scheme {
1242 RemapScheme::Compiler => {
1243 Some(format!("/rustc-dev/{sha}"))
1252 }
1253 RemapScheme::NonCompiler => {
1254 Some(format!("/rustc/{sha}"))
1256 }
1257 }
1258 }
1259 GitRepo::Llvm => Some(String::from("/rustc/llvm")),
1260 }
1261 }
1262
1263 fn cc(&self, target: TargetSelection) -> PathBuf {
1265 if self.config.dry_run() {
1266 return PathBuf::new();
1267 }
1268 self.cc[&target].path().into()
1269 }
1270
1271 fn cc_tool(&self, target: TargetSelection) -> Tool {
1273 self.cc[&target].clone()
1274 }
1275
1276 fn cxx_tool(&self, target: TargetSelection) -> Tool {
1278 self.cxx[&target].clone()
1279 }
1280
1281 fn cc_handled_clags(&self, target: TargetSelection, c: CLang) -> Vec<String> {
1284 if self.config.dry_run() {
1285 return Vec::new();
1286 }
1287 let base = match c {
1288 CLang::C => self.cc[&target].clone(),
1289 CLang::Cxx => self.cxx[&target].clone(),
1290 };
1291
1292 base.args()
1295 .iter()
1296 .map(|s| s.to_string_lossy().into_owned())
1297 .filter(|s| !s.starts_with("-O") && !s.starts_with("/O"))
1298 .collect::<Vec<String>>()
1299 }
1300
1301 fn cc_unhandled_cflags(
1303 &self,
1304 target: TargetSelection,
1305 which: GitRepo,
1306 c: CLang,
1307 ) -> Vec<String> {
1308 let mut base = Vec::new();
1309
1310 if matches!(c, CLang::Cxx) && target.contains("apple-darwin") {
1314 base.push("-stdlib=libc++".into());
1315 }
1316
1317 if &*target.triple == "i686-pc-windows-gnu" {
1321 base.push("-fno-omit-frame-pointer".into());
1322 }
1323
1324 if let Some(map_to) = self.debuginfo_map_to(which, RemapScheme::NonCompiler) {
1325 let map = format!("{}={}", self.src.display(), map_to);
1326 let cc = self.cc(target);
1327 if cc.ends_with("clang") || cc.ends_with("gcc") {
1328 base.push(format!("-fdebug-prefix-map={map}"));
1329 } else if cc.ends_with("clang-cl.exe") {
1330 base.push("-Xclang".into());
1331 base.push(format!("-fdebug-prefix-map={map}"));
1332 }
1333 }
1334 base
1335 }
1336
1337 fn ar(&self, target: TargetSelection) -> Option<PathBuf> {
1339 if self.config.dry_run() {
1340 return None;
1341 }
1342 self.ar.get(&target).cloned()
1343 }
1344
1345 fn ranlib(&self, target: TargetSelection) -> Option<PathBuf> {
1347 if self.config.dry_run() {
1348 return None;
1349 }
1350 self.ranlib.get(&target).cloned()
1351 }
1352
1353 fn cxx(&self, target: TargetSelection) -> Result<PathBuf, String> {
1355 if self.config.dry_run() {
1356 return Ok(PathBuf::new());
1357 }
1358 match self.cxx.get(&target) {
1359 Some(p) => Ok(p.path().into()),
1360 None => Err(format!("target `{target}` is not configured as a host, only as a target")),
1361 }
1362 }
1363
1364 fn linker(&self, target: TargetSelection) -> Option<PathBuf> {
1366 if self.config.dry_run() {
1367 return Some(PathBuf::new());
1368 }
1369 if let Some(linker) = self.config.target_config.get(&target).and_then(|c| c.linker.clone())
1370 {
1371 Some(linker)
1372 } else if target.contains("vxworks") {
1373 Some(self.cxx[&target].path().into())
1376 } else if !self.config.is_host_target(target)
1377 && helpers::use_host_linker(target)
1378 && !target.is_msvc()
1379 {
1380 Some(self.cc(target))
1381 } else if self.config.bootstrap_override_lld.is_used()
1382 && self.is_lld_direct_linker(target)
1383 && self.host_target == target
1384 {
1385 match self.config.bootstrap_override_lld {
1386 BootstrapOverrideLld::SelfContained => Some(self.initial_lld.clone()),
1387 BootstrapOverrideLld::External => Some("lld".into()),
1388 BootstrapOverrideLld::None => None,
1389 }
1390 } else {
1391 None
1392 }
1393 }
1394
1395 fn is_lld_direct_linker(&self, target: TargetSelection) -> bool {
1398 target.is_msvc()
1399 }
1400
1401 fn crt_static(&self, target: TargetSelection) -> Option<bool> {
1403 if target.contains("pc-windows-msvc") {
1404 Some(true)
1405 } else {
1406 self.config.target_config.get(&target).and_then(|t| t.crt_static)
1407 }
1408 }
1409
1410 fn musl_root(&self, target: TargetSelection) -> Option<&Path> {
1415 let configured_root = self
1416 .config
1417 .target_config
1418 .get(&target)
1419 .and_then(|t| t.musl_root.as_ref())
1420 .or(self.config.musl_root.as_ref())
1421 .map(|p| &**p);
1422
1423 if self.config.is_host_target(target) && configured_root.is_none() {
1424 Some(Path::new("/usr"))
1425 } else {
1426 configured_root
1427 }
1428 }
1429
1430 fn musl_libdir(&self, target: TargetSelection) -> Option<PathBuf> {
1432 self.config
1433 .target_config
1434 .get(&target)
1435 .and_then(|t| t.musl_libdir.clone())
1436 .or_else(|| self.musl_root(target).map(|root| root.join("lib")))
1437 }
1438
1439 fn wasi_libdir(&self, target: TargetSelection) -> Option<PathBuf> {
1446 let configured =
1447 self.config.target_config.get(&target).and_then(|t| t.wasi_root.as_ref()).map(|p| &**p);
1448 if let Some(path) = configured {
1449 return Some(path.join("lib").join(target.to_string()));
1450 }
1451 let mut env_root = self.wasi_sdk_path.clone()?;
1452 env_root.push("share");
1453 env_root.push("wasi-sysroot");
1454 env_root.push("lib");
1455 env_root.push(target.to_string());
1456 Some(env_root)
1457 }
1458
1459 fn no_std(&self, target: TargetSelection) -> Option<bool> {
1461 self.config.target_config.get(&target).map(|t| t.no_std)
1462 }
1463
1464 fn remote_tested(&self, target: TargetSelection) -> bool {
1467 self.qemu_rootfs(target).is_some()
1468 || target.contains("android")
1469 || env::var_os("TEST_DEVICE_ADDR").is_some()
1470 }
1471
1472 fn runner(&self, target: TargetSelection) -> Option<String> {
1478 let configured_runner =
1479 self.config.target_config.get(&target).and_then(|t| t.runner.as_ref()).map(|p| &**p);
1480 if let Some(runner) = configured_runner {
1481 return Some(runner.to_owned());
1482 }
1483
1484 if target.starts_with("wasm") && target.contains("wasi") {
1485 self.default_wasi_runner(target)
1486 } else {
1487 None
1488 }
1489 }
1490
1491 fn default_wasi_runner(&self, target: TargetSelection) -> Option<String> {
1495 let mut finder = crate::core::sanity::Finder::new();
1496
1497 if let Some(path) = finder.maybe_have("wasmtime")
1501 && let Ok(mut path) = path.into_os_string().into_string()
1502 {
1503 path.push_str(" run -C cache=n --dir .");
1504 path.push_str(" --env RUSTC_BOOTSTRAP");
1511
1512 if target.contains("wasip2") {
1513 path.push_str(" --wasi inherit-network --wasi allow-ip-name-lookup");
1514 }
1515
1516 return Some(path);
1517 }
1518
1519 None
1520 }
1521
1522 fn tool_enabled(&self, tool: &str) -> bool {
1527 if !self.config.extended {
1528 return false;
1529 }
1530 match &self.config.tools {
1531 Some(set) => set.contains(tool),
1532 None => true,
1533 }
1534 }
1535
1536 fn qemu_rootfs(&self, target: TargetSelection) -> Option<&Path> {
1542 self.config.target_config.get(&target).and_then(|t| t.qemu_rootfs.as_ref()).map(|p| &**p)
1543 }
1544
1545 fn extended_error_dir(&self) -> PathBuf {
1547 self.out.join("tmp/extended-error-metadata")
1548 }
1549
1550 fn force_use_stage1(&self, stage: u32, target: TargetSelection) -> bool {
1569 !self.config.full_bootstrap
1570 && !self.config.download_rustc()
1571 && stage >= 2
1572 && (self.hosts.contains(&target) || target == self.host_target)
1573 }
1574
1575 fn force_use_stage2(&self, stage: u32) -> bool {
1581 self.config.download_rustc() && stage >= 2
1582 }
1583
1584 fn release(&self, num: &str) -> String {
1590 match &self.config.channel[..] {
1591 "stable" => num.to_string(),
1592 "beta" => {
1593 if !self.config.omit_git_hash {
1594 format!("{}-beta.{}", num, self.beta_prerelease_version())
1595 } else {
1596 format!("{num}-beta")
1597 }
1598 }
1599 "nightly" => format!("{num}-nightly"),
1600 _ => format!("{num}-dev"),
1601 }
1602 }
1603
1604 fn beta_prerelease_version(&self) -> u32 {
1605 fn extract_beta_rev_from_file<P: AsRef<Path>>(version_file: P) -> Option<String> {
1606 let version = fs::read_to_string(version_file).ok()?;
1607
1608 helpers::extract_beta_rev(&version)
1609 }
1610
1611 if let Some(s) = self.prerelease_version.get() {
1612 return s;
1613 }
1614
1615 let count = extract_beta_rev_from_file(self.src.join("version")).unwrap_or_else(|| {
1619 helpers::git(Some(&self.src))
1623 .arg("rev-list")
1624 .arg("--count")
1625 .arg("--merges")
1626 .arg(format!(
1627 "refs/remotes/origin/{}..HEAD",
1628 self.config.stage0_metadata.config.nightly_branch
1629 ))
1630 .run_in_dry_run()
1631 .run_capture(self)
1632 .stdout()
1633 });
1634 let n = count.trim().parse().unwrap();
1635 self.prerelease_version.set(Some(n));
1636 n
1637 }
1638
1639 fn rust_release(&self) -> String {
1641 self.release(&self.version)
1642 }
1643
1644 fn rust_package_vers(&self) -> String {
1650 match &self.config.channel[..] {
1651 "stable" => self.version.to_string(),
1652 "beta" => "beta".to_string(),
1653 "nightly" => "nightly".to_string(),
1654 _ => format!("{}-dev", self.version),
1655 }
1656 }
1657
1658 fn rust_version(&self) -> String {
1664 let mut version = self.rust_info().version(self, &self.version);
1665 if let Some(ref s) = self.config.description
1666 && !s.is_empty()
1667 {
1668 version.push_str(" (");
1669 version.push_str(s);
1670 version.push(')');
1671 }
1672 version
1673 }
1674
1675 fn rust_sha(&self) -> Option<&str> {
1677 self.rust_info().sha()
1678 }
1679
1680 fn release_num(&self, package: &str) -> String {
1682 let toml_file_name = self.src.join(format!("src/tools/{package}/Cargo.toml"));
1683 let toml = t!(fs::read_to_string(toml_file_name));
1684 for line in toml.lines() {
1685 if let Some(stripped) =
1686 line.strip_prefix("version = \"").and_then(|s| s.strip_suffix('"'))
1687 {
1688 return stripped.to_owned();
1689 }
1690 }
1691
1692 panic!("failed to find version in {package}'s Cargo.toml")
1693 }
1694
1695 fn unstable_features(&self) -> bool {
1698 !matches!(&self.config.channel[..], "stable" | "beta")
1699 }
1700
1701 fn in_tree_crates(&self, root: &str, target: Option<TargetSelection>) -> Vec<&Crate> {
1705 let mut ret = Vec::new();
1706 let mut list = vec![root.to_owned()];
1707 let mut visited = HashSet::new();
1708 while let Some(krate) = list.pop() {
1709 let krate = self
1710 .crates
1711 .get(&krate)
1712 .unwrap_or_else(|| panic!("metadata missing for {krate}: {:?}", self.crates));
1713 ret.push(krate);
1714 for dep in &krate.deps {
1715 if !self.crates.contains_key(dep) {
1716 continue;
1718 }
1719 if visited.insert(dep)
1725 && (dep != "profiler_builtins"
1726 || target
1727 .map(|t| self.config.profiler_enabled(t))
1728 .unwrap_or_else(|| self.config.any_profiler_enabled()))
1729 && (dep != "rustc_codegen_llvm"
1730 || self.config.hosts.iter().any(|host| self.config.llvm_enabled(*host)))
1731 {
1732 list.push(dep.clone());
1733 }
1734 }
1735 }
1736 ret.sort_unstable_by_key(|krate| krate.name.clone()); ret
1738 }
1739
1740 fn read_stamp_file(&self, stamp: &BuildStamp) -> Vec<(PathBuf, DependencyType)> {
1741 if self.config.dry_run() {
1742 return Vec::new();
1743 }
1744
1745 if !stamp.path().exists() {
1746 eprintln!(
1747 "ERROR: Unable to find the stamp file {}, did you try to keep a nonexistent build stage?",
1748 stamp.path().display()
1749 );
1750 crate::exit!(1);
1751 }
1752
1753 let mut paths = Vec::new();
1754 let contents = t!(fs::read(stamp.path()), stamp.path());
1755 for part in contents.split(|b| *b == 0) {
1758 if part.is_empty() {
1759 continue;
1760 }
1761 let dependency_type = match part[0] as char {
1762 'h' => DependencyType::Host,
1763 's' => DependencyType::TargetSelfContained,
1764 't' => DependencyType::Target,
1765 _ => unreachable!(),
1766 };
1767 let path = PathBuf::from(t!(str::from_utf8(&part[1..])));
1768 paths.push((path, dependency_type));
1769 }
1770 paths
1771 }
1772
1773 #[track_caller]
1778 pub fn resolve_symlink_and_copy(&self, src: &Path, dst: &Path) {
1779 self.copy_link_internal(src, dst, true);
1780 }
1781
1782 #[track_caller]
1787 pub fn copy_link(&self, src: &Path, dst: &Path, file_type: FileType) {
1788 self.copy_link_internal(src, dst, false);
1789
1790 if file_type.could_have_split_debuginfo()
1791 && let Some(dbg_file) = split_debuginfo(src)
1792 {
1793 self.copy_link_internal(
1794 &dbg_file,
1795 &dst.with_extension(dbg_file.extension().unwrap()),
1796 false,
1797 );
1798 }
1799 }
1800
1801 #[track_caller]
1802 fn copy_link_internal(&self, src: &Path, dst: &Path, dereference_symlinks: bool) {
1803 if self.config.dry_run() {
1804 return;
1805 }
1806 if src == dst {
1807 return;
1808 }
1809
1810 #[cfg(feature = "tracing")]
1811 let _span = trace_io!("file-copy-link", ?src, ?dst);
1812
1813 if let Err(e) = fs::remove_file(dst)
1814 && cfg!(windows)
1815 && e.kind() != io::ErrorKind::NotFound
1816 {
1817 let now = t!(SystemTime::now().duration_since(SystemTime::UNIX_EPOCH));
1820 let _ = fs::rename(dst, format!("{}-{}", dst.display(), now.as_nanos()));
1821 }
1822 let mut metadata = t!(src.symlink_metadata(), format!("src = {}", src.display()));
1823 let mut src = src.to_path_buf();
1824 if metadata.file_type().is_symlink() {
1825 if dereference_symlinks {
1826 src = t!(fs::canonicalize(src));
1827 metadata = t!(fs::metadata(&src), format!("target = {}", src.display()));
1828 } else {
1829 let link = t!(fs::read_link(src));
1830 t!(self.symlink_file(link, dst));
1831 return;
1832 }
1833 }
1834 if let Ok(()) = fs::hard_link(&src, dst) {
1835 } else {
1838 if let Err(e) = fs::copy(&src, dst) {
1839 panic!("failed to copy `{}` to `{}`: {}", src.display(), dst.display(), e)
1840 }
1841 t!(fs::set_permissions(dst, metadata.permissions()));
1842
1843 let file_times = fs::FileTimes::new()
1846 .set_accessed(t!(metadata.accessed()))
1847 .set_modified(t!(metadata.modified()));
1848 t!(set_file_times(dst, file_times));
1849 }
1850 }
1851
1852 #[track_caller]
1856 pub fn cp_link_r(&self, src: &Path, dst: &Path) {
1857 if self.config.dry_run() {
1858 return;
1859 }
1860 for f in self.read_dir(src) {
1861 let path = f.path();
1862 let name = path.file_name().unwrap();
1863 let dst = dst.join(name);
1864 if t!(f.file_type()).is_dir() {
1865 t!(fs::create_dir_all(&dst));
1866 self.cp_link_r(&path, &dst);
1867 } else {
1868 self.copy_link(&path, &dst, FileType::Regular);
1869 }
1870 }
1871 }
1872
1873 #[track_caller]
1879 pub fn cp_link_filtered(&self, src: &Path, dst: &Path, filter: &dyn Fn(&Path) -> bool) {
1880 self.cp_link_filtered_recurse(src, dst, Path::new(""), filter)
1882 }
1883
1884 #[track_caller]
1886 fn cp_link_filtered_recurse(
1887 &self,
1888 src: &Path,
1889 dst: &Path,
1890 relative: &Path,
1891 filter: &dyn Fn(&Path) -> bool,
1892 ) {
1893 for f in self.read_dir(src) {
1894 let path = f.path();
1895 let name = path.file_name().unwrap();
1896 let dst = dst.join(name);
1897 let relative = relative.join(name);
1898 if filter(&relative) {
1900 if t!(f.file_type()).is_dir() {
1901 let _ = fs::remove_dir_all(&dst);
1902 self.create_dir(&dst);
1903 self.cp_link_filtered_recurse(&path, &dst, &relative, filter);
1904 } else {
1905 self.copy_link(&path, &dst, FileType::Regular);
1906 }
1907 }
1908 }
1909 }
1910
1911 fn copy_link_to_folder(&self, src: &Path, dest_folder: &Path) {
1912 let file_name = src.file_name().unwrap();
1913 let dest = dest_folder.join(file_name);
1914 self.copy_link(src, &dest, FileType::Regular);
1915 }
1916
1917 fn install(&self, src: &Path, dstdir: &Path, file_type: FileType) {
1918 if self.config.dry_run() {
1919 return;
1920 }
1921 let dst = dstdir.join(src.file_name().unwrap());
1922
1923 #[cfg(feature = "tracing")]
1924 let _span = trace_io!("install", ?src, ?dst);
1925
1926 t!(fs::create_dir_all(dstdir));
1927 if !src.exists() {
1928 panic!("ERROR: File \"{}\" not found!", src.display());
1929 }
1930
1931 self.copy_link_internal(src, &dst, true);
1932 chmod(&dst, file_type.perms());
1933
1934 if file_type.could_have_split_debuginfo()
1936 && let Some(dbg_file) = split_debuginfo(src)
1937 {
1938 self.install(&dbg_file, dstdir, FileType::Regular);
1939 }
1940 }
1941
1942 fn read(&self, path: &Path) -> String {
1943 if self.config.dry_run() {
1944 return String::new();
1945 }
1946 t!(fs::read_to_string(path))
1947 }
1948
1949 #[track_caller]
1950 fn create_dir(&self, dir: &Path) {
1951 if self.config.dry_run() {
1952 return;
1953 }
1954
1955 #[cfg(feature = "tracing")]
1956 let _span = trace_io!("dir-create", ?dir);
1957
1958 t!(fs::create_dir_all(dir))
1959 }
1960
1961 fn remove_dir(&self, dir: &Path) {
1962 if self.config.dry_run() {
1963 return;
1964 }
1965
1966 #[cfg(feature = "tracing")]
1967 let _span = trace_io!("dir-remove", ?dir);
1968
1969 t!(fs::remove_dir_all(dir))
1970 }
1971
1972 fn clear_dir(&self, dir: &Path) {
1975 if self.config.dry_run() {
1976 return;
1977 }
1978
1979 #[cfg(feature = "tracing")]
1980 let _span = trace_io!("dir-clear", ?dir);
1981
1982 let _ = std::fs::remove_dir_all(dir);
1983 self.create_dir(dir);
1984 }
1985
1986 fn read_dir(&self, dir: &Path) -> impl Iterator<Item = fs::DirEntry> {
1987 let iter = match fs::read_dir(dir) {
1988 Ok(v) => v,
1989 Err(_) if self.config.dry_run() => return vec![].into_iter(),
1990 Err(err) => panic!("could not read dir {dir:?}: {err:?}"),
1991 };
1992 iter.map(|e| t!(e)).collect::<Vec<_>>().into_iter()
1993 }
1994
1995 fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(&self, src: P, link: Q) -> io::Result<()> {
1996 #[cfg(unix)]
1997 use std::os::unix::fs::symlink as symlink_file;
1998 #[cfg(windows)]
1999 use std::os::windows::fs::symlink_file;
2000 if !self.config.dry_run() { symlink_file(src.as_ref(), link.as_ref()) } else { Ok(()) }
2001 }
2002
2003 fn ninja(&self) -> bool {
2006 let mut cmd_finder = crate::core::sanity::Finder::new();
2007
2008 if self.config.ninja_in_file {
2009 if cmd_finder.maybe_have("ninja-build").is_none()
2012 && cmd_finder.maybe_have("ninja").is_none()
2013 {
2014 eprintln!(
2015 "
2016Couldn't find required command: ninja (or ninja-build)
2017
2018You should install ninja as described at
2019<https://github.com/ninja-build/ninja/wiki/Pre-built-Ninja-packages>,
2020or set `ninja = false` in the `[llvm]` section of `bootstrap.toml`.
2021Alternatively, set `download-ci-llvm = true` in that `[llvm]` section
2022to download LLVM rather than building it.
2023"
2024 );
2025 exit!(1);
2026 }
2027 }
2028
2029 if !self.config.ninja_in_file
2037 && self.config.host_target.is_msvc()
2038 && cmd_finder.maybe_have("ninja").is_some()
2039 {
2040 return true;
2041 }
2042
2043 self.config.ninja_in_file
2044 }
2045
2046 pub fn colored_stdout<R, F: FnOnce(&mut dyn WriteColor) -> R>(&self, f: F) -> R {
2047 self.colored_stream_inner(StandardStream::stdout, self.config.stdout_is_tty, f)
2048 }
2049
2050 pub fn colored_stderr<R, F: FnOnce(&mut dyn WriteColor) -> R>(&self, f: F) -> R {
2051 self.colored_stream_inner(StandardStream::stderr, self.config.stderr_is_tty, f)
2052 }
2053
2054 fn colored_stream_inner<R, F, C>(&self, constructor: C, is_tty: bool, f: F) -> R
2055 where
2056 C: Fn(ColorChoice) -> StandardStream,
2057 F: FnOnce(&mut dyn WriteColor) -> R,
2058 {
2059 let choice = match self.config.color {
2060 flags::Color::Always => ColorChoice::Always,
2061 flags::Color::Never => ColorChoice::Never,
2062 flags::Color::Auto if !is_tty => ColorChoice::Never,
2063 flags::Color::Auto => ColorChoice::Auto,
2064 };
2065 let mut stream = constructor(choice);
2066 let result = f(&mut stream);
2067 stream.reset().unwrap();
2068 result
2069 }
2070
2071 pub fn exec_ctx(&self) -> &ExecutionContext {
2072 &self.config.exec_ctx
2073 }
2074
2075 pub fn report_summary(&self, path: &Path, start_time: Instant) {
2076 self.config.exec_ctx.profiler().report_summary(path, start_time);
2077 }
2078
2079 #[cfg(feature = "tracing")]
2080 pub fn report_step_graph(self, directory: &Path) {
2081 self.step_graph.into_inner().store_to_dot_files(directory);
2082 }
2083}
2084
2085impl AsRef<ExecutionContext> for Build {
2086 fn as_ref(&self) -> &ExecutionContext {
2087 &self.config.exec_ctx
2088 }
2089}
2090
2091#[cfg(unix)]
2092fn chmod(path: &Path, perms: u32) {
2093 use std::os::unix::fs::*;
2094 t!(fs::set_permissions(path, fs::Permissions::from_mode(perms)));
2095}
2096#[cfg(windows)]
2097fn chmod(_path: &Path, _perms: u32) {}
2098
2099impl Compiler {
2100 pub fn new(stage: u32, host: TargetSelection) -> Self {
2101 Self { stage, host, forced_compiler: false }
2102 }
2103
2104 pub fn forced_compiler(&mut self, forced_compiler: bool) {
2105 self.forced_compiler = forced_compiler;
2106 }
2107
2108 pub fn is_snapshot(&self, build: &Build) -> bool {
2110 self.stage == 0 && self.host == build.host_target
2111 }
2112
2113 pub fn is_forced_compiler(&self) -> bool {
2115 self.forced_compiler
2116 }
2117}
2118
2119fn envify(s: &str) -> String {
2120 s.chars()
2121 .map(|c| match c {
2122 '-' => '_',
2123 c => c,
2124 })
2125 .flat_map(|c| c.to_uppercase())
2126 .collect()
2127}
2128
2129pub fn prepare_behaviour_dump_dir(build: &Build) {
2131 static INITIALIZED: OnceLock<bool> = OnceLock::new();
2132
2133 let dump_path = build.out.join("bootstrap-shims-dump");
2134
2135 let initialized = INITIALIZED.get().unwrap_or(&false);
2136 if !initialized {
2137 if dump_path.exists() {
2139 t!(fs::remove_dir_all(&dump_path));
2140 }
2141
2142 t!(fs::create_dir_all(&dump_path));
2143
2144 t!(INITIALIZED.set(true));
2145 }
2146}