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 cfg!(test) && !self.config.submodules() {
682 return;
683 }
684 self.config.update_submodule(submodule);
685 let absolute_path = self.config.src.join(submodule);
686 if !absolute_path.exists() || dir_is_empty(&absolute_path) {
687 let maybe_enable = if !self.config.submodules()
688 && self.config.rust_info.is_managed_git_subrepository()
689 {
690 "\nConsider setting `build.submodules = true` or manually initializing the submodules."
691 } else {
692 ""
693 };
694 let err_hint = err_hint.map_or_else(String::new, |e| format!("\n{e}"));
695 eprintln!(
696 "submodule {submodule} does not appear to be checked out, \
697 but it is required for this step{maybe_enable}{err_hint}"
698 );
699 exit!(1);
700 }
701 }
702
703 fn update_existing_submodules(&self) {
706 if !self.config.submodules() {
709 return;
710 }
711 let output = helpers::git(Some(&self.src))
712 .args(["config", "--file"])
713 .arg(".gitmodules")
714 .args(["--get-regexp", "path"])
715 .run_capture(self)
716 .stdout();
717 std::thread::scope(|s| {
718 for line in output.lines() {
721 let submodule = line.split_once(' ').unwrap().1;
722 let config = self.config.clone();
723 s.spawn(move || {
724 Self::update_existing_submodule(&config, submodule);
725 });
726 }
727 });
728 }
729
730 pub fn update_existing_submodule(config: &Config, submodule: &str) {
732 if !config.submodules() {
734 return;
735 }
736
737 if config.git_info(false, Path::new(submodule)).is_managed_git_subrepository() {
738 config.update_submodule(submodule);
739 }
740 }
741
742 #[cfg_attr(feature = "tracing", instrument(level = "debug", name = "Build::build", skip_all))]
744 pub fn build(&mut self) {
745 trace!("setting up job management");
746 unsafe {
747 crate::utils::job::setup(self);
748 }
749
750 {
752 #[cfg(feature = "tracing")]
753 let _hardcoded_span =
754 span!(tracing::Level::DEBUG, "handling hardcoded subcommands (Format, Perf)")
755 .entered();
756
757 match &self.config.cmd {
758 Subcommand::Format { check, all } => {
759 return core::build_steps::format::format(
760 &builder::Builder::new(self),
761 *check,
762 *all,
763 &self.config.paths,
764 );
765 }
766 Subcommand::Perf(args) => {
767 return core::build_steps::perf::perf(&builder::Builder::new(self), args);
768 }
769 _cmd => {
770 debug!(cmd = ?_cmd, "not a hardcoded subcommand; returning to normal handling");
771 }
772 }
773
774 debug!("handling subcommand normally");
775 }
776
777 if !self.config.dry_run() {
778 #[cfg(feature = "tracing")]
779 let _real_run_span = span!(tracing::Level::DEBUG, "executing real run").entered();
780
781 {
784 #[cfg(feature = "tracing")]
785 let _sanity_check_span =
786 span!(tracing::Level::DEBUG, "(1) executing dry-run sanity-check").entered();
787 self.config.set_dry_run(DryRun::SelfCheck);
788 let builder = builder::Builder::new(self);
789 builder.execute_cli();
790 }
791
792 {
794 #[cfg(feature = "tracing")]
795 let _actual_run_span =
796 span!(tracing::Level::DEBUG, "(2) executing actual run").entered();
797 self.config.set_dry_run(DryRun::Disabled);
798 let builder = builder::Builder::new(self);
799 builder.execute_cli();
800 }
801 } else {
802 #[cfg(feature = "tracing")]
803 let _dry_run_span = span!(tracing::Level::DEBUG, "executing dry run").entered();
804
805 let builder = builder::Builder::new(self);
806 builder.execute_cli();
807 }
808
809 #[cfg(feature = "tracing")]
810 debug!("checking for postponed test failures from `test --no-fail-fast`");
811
812 self.config.exec_ctx().report_failures_and_exit();
814
815 #[cfg(feature = "build-metrics")]
816 self.metrics.persist(self);
817 }
818
819 fn rust_info(&self) -> &GitInfo {
820 &self.config.rust_info
821 }
822
823 fn std_features(&self, target: TargetSelection) -> String {
826 let mut features: BTreeSet<&str> =
827 self.config.rust_std_features.iter().map(|s| s.as_str()).collect();
828
829 match self.config.llvm_libunwind(target) {
830 LlvmLibunwind::InTree => features.insert("llvm-libunwind"),
831 LlvmLibunwind::System => features.insert("system-llvm-libunwind"),
832 LlvmLibunwind::No => false,
833 };
834
835 if self.config.backtrace {
836 features.insert("backtrace");
837 }
838
839 if self.config.profiler_enabled(target) {
840 features.insert("profiler");
841 }
842
843 if target.contains("zkvm") {
845 features.insert("compiler-builtins-mem");
846 }
847
848 if self.config.llvm_enzyme {
849 features.insert("llvm_enzyme");
850 }
851
852 features.into_iter().collect::<Vec<_>>().join(" ")
853 }
854
855 fn rustc_features(&self, kind: Kind, target: TargetSelection, crates: &[String]) -> String {
857 let possible_features_by_crates: HashSet<_> = crates
858 .iter()
859 .flat_map(|krate| &self.crates[krate].features)
860 .map(std::ops::Deref::deref)
861 .collect();
862 let check = |feature: &str| -> bool {
863 crates.is_empty() || possible_features_by_crates.contains(feature)
864 };
865 let mut features = vec![];
866 if self.config.jemalloc(target) && check("jemalloc") {
867 features.push("jemalloc");
868 }
869 if (self.config.llvm_enabled(target) || kind == Kind::Check) && check("llvm") {
870 features.push("llvm");
871 }
872 if self.config.llvm_enzyme {
873 features.push("llvm_enzyme");
874 }
875 if self.config.llvm_offload {
876 features.push("llvm_offload");
877 }
878 if self.config.rust_randomize_layout && check("rustc_randomized_layouts") {
880 features.push("rustc_randomized_layouts");
881 }
882 if self.config.compile_time_deps && kind == Kind::Check {
883 features.push("check_only");
884 }
885
886 if crates.iter().any(|c| c == "rustc_transmute") {
887 features.push("rustc");
890 }
891
892 if !self.config.rust_debug_logging && check("max_level_info") {
898 features.push("max_level_info");
899 }
900
901 features.join(" ")
902 }
903
904 fn cargo_dir(&self, mode: Mode) -> &'static str {
907 match (mode, self.config.rust_optimize.is_release()) {
908 (Mode::Std, _) => "dist",
909 (_, true) => "release",
910 (_, false) => "debug",
911 }
912 }
913
914 fn tools_dir(&self, build_compiler: Compiler) -> PathBuf {
915 let out = self
916 .out
917 .join(build_compiler.host)
918 .join(format!("stage{}-tools-bin", build_compiler.stage + 1));
919 t!(fs::create_dir_all(&out));
920 out
921 }
922
923 fn stage_out(&self, build_compiler: Compiler, mode: Mode) -> PathBuf {
928 use std::fmt::Write;
929
930 fn bootstrap_tool() -> (Option<u32>, &'static str) {
931 (None, "bootstrap-tools")
932 }
933 fn staged_tool(build_compiler: Compiler) -> (Option<u32>, &'static str) {
934 (Some(build_compiler.stage + 1), "tools")
935 }
936
937 let (stage, suffix) = match mode {
938 Mode::Std => (Some(build_compiler.stage), "std"),
940 Mode::Rustc => (Some(build_compiler.stage + 1), "rustc"),
942 Mode::Codegen => (Some(build_compiler.stage + 1), "codegen"),
943 Mode::ToolBootstrap => bootstrap_tool(),
944 Mode::ToolStd | Mode::ToolRustcPrivate => (Some(build_compiler.stage + 1), "tools"),
945 Mode::ToolTarget => {
946 if build_compiler.stage == 0 {
949 bootstrap_tool()
950 } else {
951 staged_tool(build_compiler)
952 }
953 }
954 };
955 let path = self.out.join(build_compiler.host);
956 let mut dir_name = String::new();
957 if let Some(stage) = stage {
958 write!(dir_name, "stage{stage}-").unwrap();
959 }
960 dir_name.push_str(suffix);
961 path.join(dir_name)
962 }
963
964 fn cargo_out(&self, build_compiler: Compiler, mode: Mode, target: TargetSelection) -> PathBuf {
968 self.stage_out(build_compiler, mode).join(target).join(self.cargo_dir(mode))
969 }
970
971 fn llvm_out(&self, target: TargetSelection) -> PathBuf {
976 if self.config.llvm_from_ci && self.config.is_host_target(target) {
977 self.config.ci_llvm_root()
978 } else {
979 self.out.join(target).join("llvm")
980 }
981 }
982
983 fn enzyme_out(&self, target: TargetSelection) -> PathBuf {
984 self.out.join(&*target.triple).join("enzyme")
985 }
986
987 fn offload_out(&self, target: TargetSelection) -> PathBuf {
988 self.out.join(&*target.triple).join("offload")
989 }
990
991 fn lld_out(&self, target: TargetSelection) -> PathBuf {
992 self.out.join(target).join("lld")
993 }
994
995 fn doc_out(&self, target: TargetSelection) -> PathBuf {
997 self.out.join(target).join("doc")
998 }
999
1000 fn json_doc_out(&self, target: TargetSelection) -> PathBuf {
1002 self.out.join(target).join("json-doc")
1003 }
1004
1005 fn test_out(&self, target: TargetSelection) -> PathBuf {
1006 self.out.join(target).join("test")
1007 }
1008
1009 fn compiler_doc_out(&self, target: TargetSelection) -> PathBuf {
1011 self.out.join(target).join("compiler-doc")
1012 }
1013
1014 fn md_doc_out(&self, target: TargetSelection) -> PathBuf {
1016 self.out.join(target).join("md-doc")
1017 }
1018
1019 fn vendored_crates_path(&self) -> Option<PathBuf> {
1021 if self.config.vendor { Some(self.src.join(VENDOR_DIR)) } else { None }
1022 }
1023
1024 fn llvm_filecheck(&self, target: TargetSelection) -> PathBuf {
1026 let target_config = self.config.target_config.get(&target);
1027 if let Some(s) = target_config.and_then(|c| c.llvm_filecheck.as_ref()) {
1028 s.to_path_buf()
1029 } else if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
1030 let llvm_bindir = command(s).arg("--bindir").run_capture_stdout(self).stdout();
1031 let filecheck = Path::new(llvm_bindir.trim()).join(exe("FileCheck", target));
1032 if filecheck.exists() {
1033 filecheck
1034 } else {
1035 let llvm_libdir = command(s).arg("--libdir").run_capture_stdout(self).stdout();
1038 let lib_filecheck =
1039 Path::new(llvm_libdir.trim()).join("llvm").join(exe("FileCheck", target));
1040 if lib_filecheck.exists() {
1041 lib_filecheck
1042 } else {
1043 filecheck
1047 }
1048 }
1049 } else {
1050 let base = self.llvm_out(target).join("build");
1051 let base = if !self.ninja() && target.is_msvc() {
1052 if self.config.llvm_optimize {
1053 if self.config.llvm_release_debuginfo {
1054 base.join("RelWithDebInfo")
1055 } else {
1056 base.join("Release")
1057 }
1058 } else {
1059 base.join("Debug")
1060 }
1061 } else {
1062 base
1063 };
1064 base.join("bin").join(exe("FileCheck", target))
1065 }
1066 }
1067
1068 fn native_dir(&self, target: TargetSelection) -> PathBuf {
1070 self.out.join(target).join("native")
1071 }
1072
1073 fn test_helpers_out(&self, target: TargetSelection) -> PathBuf {
1076 self.native_dir(target).join("rust-test-helpers")
1077 }
1078
1079 fn add_rust_test_threads(&self, cmd: &mut BootstrapCommand) {
1081 if env::var_os("RUST_TEST_THREADS").is_none() {
1082 cmd.env("RUST_TEST_THREADS", self.jobs().to_string());
1083 }
1084 }
1085
1086 fn rustc_snapshot_libdir(&self) -> PathBuf {
1088 self.rustc_snapshot_sysroot().join(libdir(self.config.host_target))
1089 }
1090
1091 fn rustc_snapshot_sysroot(&self) -> &Path {
1093 static SYSROOT_CACHE: OnceLock<PathBuf> = OnceLock::new();
1094 SYSROOT_CACHE.get_or_init(|| {
1095 command(&self.initial_rustc)
1096 .run_in_dry_run()
1097 .args(["--print", "sysroot"])
1098 .run_capture_stdout(self)
1099 .stdout()
1100 .trim()
1101 .to_owned()
1102 .into()
1103 })
1104 }
1105
1106 fn info(&self, msg: &str) {
1107 match self.config.get_dry_run() {
1108 DryRun::SelfCheck => (),
1109 DryRun::Disabled | DryRun::UserSelected => {
1110 println!("{msg}");
1111 }
1112 }
1113 }
1114
1115 #[must_use = "Groups should not be dropped until the Step finishes running"]
1127 #[track_caller]
1128 fn msg(
1129 &self,
1130 action: impl Into<Kind>,
1131 what: impl Display,
1132 mode: impl Into<Option<Mode>>,
1133 target_and_stage: impl Into<TargetAndStage>,
1134 target: impl Into<Option<TargetSelection>>,
1135 ) -> Option<gha::Group> {
1136 let target_and_stage = target_and_stage.into();
1137 let action = action.into();
1138 assert!(
1139 action != Kind::Test,
1140 "Please use `Build::msg_test` instead of `Build::msg(Kind::Test)`"
1141 );
1142
1143 let actual_stage = match mode.into() {
1144 Some(Mode::Std) => target_and_stage.stage,
1146 Some(
1148 Mode::Rustc
1149 | Mode::Codegen
1150 | Mode::ToolBootstrap
1151 | Mode::ToolTarget
1152 | Mode::ToolStd
1153 | Mode::ToolRustcPrivate,
1154 )
1155 | None => target_and_stage.stage + 1,
1156 };
1157
1158 let action = action.description();
1159 let what = what.to_string();
1160 let msg = |fmt| {
1161 let space = if !what.is_empty() { " " } else { "" };
1162 format!("{action} stage{actual_stage} {what}{space}{fmt}")
1163 };
1164 let msg = if let Some(target) = target.into() {
1165 let build_stage = target_and_stage.stage;
1166 let host = target_and_stage.target;
1167 if host == target {
1168 msg(format_args!("(stage{build_stage} -> stage{actual_stage}, {target})"))
1169 } else {
1170 msg(format_args!("(stage{build_stage}:{host} -> stage{actual_stage}:{target})"))
1171 }
1172 } else {
1173 msg(format_args!(""))
1174 };
1175 self.group(&msg)
1176 }
1177
1178 #[must_use = "Groups should not be dropped until the Step finishes running"]
1184 #[track_caller]
1185 fn msg_test(
1186 &self,
1187 what: impl Display,
1188 target: TargetSelection,
1189 stage: u32,
1190 ) -> Option<gha::Group> {
1191 let action = Kind::Test.description();
1192 let msg = format!("{action} stage{stage} {what} ({target})");
1193 self.group(&msg)
1194 }
1195
1196 #[must_use = "Groups should not be dropped until the Step finishes running"]
1200 #[track_caller]
1201 fn msg_unstaged(
1202 &self,
1203 action: impl Into<Kind>,
1204 what: impl Display,
1205 target: TargetSelection,
1206 ) -> Option<gha::Group> {
1207 let action = action.into().description();
1208 let msg = format!("{action} {what} for {target}");
1209 self.group(&msg)
1210 }
1211
1212 #[track_caller]
1213 fn group(&self, msg: &str) -> Option<gha::Group> {
1214 match self.config.get_dry_run() {
1215 DryRun::SelfCheck => None,
1216 DryRun::Disabled | DryRun::UserSelected => Some(gha::group(msg)),
1217 }
1218 }
1219
1220 fn jobs(&self) -> u32 {
1223 self.config.jobs.unwrap_or_else(|| {
1224 std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get) as u32
1225 })
1226 }
1227
1228 fn debuginfo_map_to(&self, which: GitRepo, remap_scheme: RemapScheme) -> Option<String> {
1229 if !self.config.rust_remap_debuginfo {
1230 return None;
1231 }
1232
1233 match which {
1234 GitRepo::Rustc => {
1235 let sha = self.rust_sha().unwrap_or(&self.version);
1236
1237 match remap_scheme {
1238 RemapScheme::Compiler => {
1239 Some(format!("/rustc-dev/{sha}"))
1248 }
1249 RemapScheme::NonCompiler => {
1250 Some(format!("/rustc/{sha}"))
1252 }
1253 }
1254 }
1255 GitRepo::Llvm => Some(String::from("/rustc/llvm")),
1256 }
1257 }
1258
1259 fn cc(&self, target: TargetSelection) -> PathBuf {
1261 if self.config.dry_run() {
1262 return PathBuf::new();
1263 }
1264 self.cc[&target].path().into()
1265 }
1266
1267 fn cc_tool(&self, target: TargetSelection) -> Tool {
1269 self.cc[&target].clone()
1270 }
1271
1272 fn cxx_tool(&self, target: TargetSelection) -> Tool {
1274 self.cxx[&target].clone()
1275 }
1276
1277 fn cc_handled_clags(&self, target: TargetSelection, c: CLang) -> Vec<String> {
1280 if self.config.dry_run() {
1281 return Vec::new();
1282 }
1283 let base = match c {
1284 CLang::C => self.cc[&target].clone(),
1285 CLang::Cxx => self.cxx[&target].clone(),
1286 };
1287
1288 base.args()
1291 .iter()
1292 .map(|s| s.to_string_lossy().into_owned())
1293 .filter(|s| !s.starts_with("-O") && !s.starts_with("/O"))
1294 .collect::<Vec<String>>()
1295 }
1296
1297 fn cc_unhandled_cflags(
1299 &self,
1300 target: TargetSelection,
1301 which: GitRepo,
1302 c: CLang,
1303 ) -> Vec<String> {
1304 let mut base = Vec::new();
1305
1306 if matches!(c, CLang::Cxx) && target.contains("apple-darwin") {
1310 base.push("-stdlib=libc++".into());
1311 }
1312
1313 if &*target.triple == "i686-pc-windows-gnu" {
1317 base.push("-fno-omit-frame-pointer".into());
1318 }
1319
1320 if let Some(map_to) = self.debuginfo_map_to(which, RemapScheme::NonCompiler) {
1321 let map = format!("{}={}", self.src.display(), map_to);
1322 let cc = self.cc(target);
1323 if cc.ends_with("clang") || cc.ends_with("gcc") {
1324 base.push(format!("-fdebug-prefix-map={map}"));
1325 } else if cc.ends_with("clang-cl.exe") {
1326 base.push("-Xclang".into());
1327 base.push(format!("-fdebug-prefix-map={map}"));
1328 }
1329 }
1330 base
1331 }
1332
1333 fn ar(&self, target: TargetSelection) -> Option<PathBuf> {
1335 if self.config.dry_run() {
1336 return None;
1337 }
1338 self.ar.get(&target).cloned()
1339 }
1340
1341 fn ranlib(&self, target: TargetSelection) -> Option<PathBuf> {
1343 if self.config.dry_run() {
1344 return None;
1345 }
1346 self.ranlib.get(&target).cloned()
1347 }
1348
1349 fn cxx(&self, target: TargetSelection) -> Result<PathBuf, String> {
1351 if self.config.dry_run() {
1352 return Ok(PathBuf::new());
1353 }
1354 match self.cxx.get(&target) {
1355 Some(p) => Ok(p.path().into()),
1356 None => Err(format!("target `{target}` is not configured as a host, only as a target")),
1357 }
1358 }
1359
1360 fn linker(&self, target: TargetSelection) -> Option<PathBuf> {
1362 if self.config.dry_run() {
1363 return Some(PathBuf::new());
1364 }
1365 if let Some(linker) = self.config.target_config.get(&target).and_then(|c| c.linker.clone())
1366 {
1367 Some(linker)
1368 } else if target.contains("vxworks") {
1369 Some(self.cxx[&target].path().into())
1372 } else if !self.config.is_host_target(target)
1373 && helpers::use_host_linker(target)
1374 && !target.is_msvc()
1375 {
1376 Some(self.cc(target))
1377 } else if self.config.bootstrap_override_lld.is_used()
1378 && self.is_lld_direct_linker(target)
1379 && self.host_target == target
1380 {
1381 match self.config.bootstrap_override_lld {
1382 BootstrapOverrideLld::SelfContained => Some(self.initial_lld.clone()),
1383 BootstrapOverrideLld::External => Some("lld".into()),
1384 BootstrapOverrideLld::None => None,
1385 }
1386 } else {
1387 None
1388 }
1389 }
1390
1391 fn is_lld_direct_linker(&self, target: TargetSelection) -> bool {
1394 target.is_msvc()
1395 }
1396
1397 fn crt_static(&self, target: TargetSelection) -> Option<bool> {
1399 if target.contains("pc-windows-msvc") {
1400 Some(true)
1401 } else {
1402 self.config.target_config.get(&target).and_then(|t| t.crt_static)
1403 }
1404 }
1405
1406 fn musl_root(&self, target: TargetSelection) -> Option<&Path> {
1411 let configured_root = self
1412 .config
1413 .target_config
1414 .get(&target)
1415 .and_then(|t| t.musl_root.as_ref())
1416 .or(self.config.musl_root.as_ref())
1417 .map(|p| &**p);
1418
1419 if self.config.is_host_target(target) && configured_root.is_none() {
1420 Some(Path::new("/usr"))
1421 } else {
1422 configured_root
1423 }
1424 }
1425
1426 fn musl_libdir(&self, target: TargetSelection) -> Option<PathBuf> {
1428 self.config
1429 .target_config
1430 .get(&target)
1431 .and_then(|t| t.musl_libdir.clone())
1432 .or_else(|| self.musl_root(target).map(|root| root.join("lib")))
1433 }
1434
1435 fn wasi_libdir(&self, target: TargetSelection) -> Option<PathBuf> {
1442 let configured =
1443 self.config.target_config.get(&target).and_then(|t| t.wasi_root.as_ref()).map(|p| &**p);
1444 if let Some(path) = configured {
1445 return Some(path.join("lib").join(target.to_string()));
1446 }
1447 let mut env_root = self.wasi_sdk_path.clone()?;
1448 env_root.push("share");
1449 env_root.push("wasi-sysroot");
1450 env_root.push("lib");
1451 env_root.push(target.to_string());
1452 Some(env_root)
1453 }
1454
1455 fn no_std(&self, target: TargetSelection) -> Option<bool> {
1457 self.config.target_config.get(&target).map(|t| t.no_std)
1458 }
1459
1460 fn remote_tested(&self, target: TargetSelection) -> bool {
1463 self.qemu_rootfs(target).is_some()
1464 || target.contains("android")
1465 || env::var_os("TEST_DEVICE_ADDR").is_some()
1466 }
1467
1468 fn runner(&self, target: TargetSelection) -> Option<String> {
1474 let configured_runner =
1475 self.config.target_config.get(&target).and_then(|t| t.runner.as_ref()).map(|p| &**p);
1476 if let Some(runner) = configured_runner {
1477 return Some(runner.to_owned());
1478 }
1479
1480 if target.starts_with("wasm") && target.contains("wasi") {
1481 self.default_wasi_runner(target)
1482 } else {
1483 None
1484 }
1485 }
1486
1487 fn default_wasi_runner(&self, target: TargetSelection) -> Option<String> {
1491 let mut finder = crate::core::sanity::Finder::new();
1492
1493 if let Some(path) = finder.maybe_have("wasmtime")
1497 && let Ok(mut path) = path.into_os_string().into_string()
1498 {
1499 path.push_str(" run -C cache=n --dir .");
1500 path.push_str(" --env RUSTC_BOOTSTRAP");
1507
1508 if target.contains("wasip2") {
1509 path.push_str(" --wasi inherit-network --wasi allow-ip-name-lookup");
1510 }
1511
1512 return Some(path);
1513 }
1514
1515 None
1516 }
1517
1518 fn tool_enabled(&self, tool: &str) -> bool {
1523 if !self.config.extended {
1524 return false;
1525 }
1526 match &self.config.tools {
1527 Some(set) => set.contains(tool),
1528 None => true,
1529 }
1530 }
1531
1532 fn qemu_rootfs(&self, target: TargetSelection) -> Option<&Path> {
1538 self.config.target_config.get(&target).and_then(|t| t.qemu_rootfs.as_ref()).map(|p| &**p)
1539 }
1540
1541 fn extended_error_dir(&self) -> PathBuf {
1543 self.out.join("tmp/extended-error-metadata")
1544 }
1545
1546 fn force_use_stage1(&self, stage: u32, target: TargetSelection) -> bool {
1565 !self.config.full_bootstrap
1566 && !self.config.download_rustc()
1567 && stage >= 2
1568 && (self.hosts.contains(&target) || target == self.host_target)
1569 }
1570
1571 fn force_use_stage2(&self, stage: u32) -> bool {
1577 self.config.download_rustc() && stage >= 2
1578 }
1579
1580 fn release(&self, num: &str) -> String {
1586 match &self.config.channel[..] {
1587 "stable" => num.to_string(),
1588 "beta" => {
1589 if !self.config.omit_git_hash {
1590 format!("{}-beta.{}", num, self.beta_prerelease_version())
1591 } else {
1592 format!("{num}-beta")
1593 }
1594 }
1595 "nightly" => format!("{num}-nightly"),
1596 _ => format!("{num}-dev"),
1597 }
1598 }
1599
1600 fn beta_prerelease_version(&self) -> u32 {
1601 fn extract_beta_rev_from_file<P: AsRef<Path>>(version_file: P) -> Option<String> {
1602 let version = fs::read_to_string(version_file).ok()?;
1603
1604 helpers::extract_beta_rev(&version)
1605 }
1606
1607 if let Some(s) = self.prerelease_version.get() {
1608 return s;
1609 }
1610
1611 let count = extract_beta_rev_from_file(self.src.join("version")).unwrap_or_else(|| {
1615 helpers::git(Some(&self.src))
1619 .arg("rev-list")
1620 .arg("--count")
1621 .arg("--merges")
1622 .arg(format!(
1623 "refs/remotes/origin/{}..HEAD",
1624 self.config.stage0_metadata.config.nightly_branch
1625 ))
1626 .run_in_dry_run()
1627 .run_capture(self)
1628 .stdout()
1629 });
1630 let n = count.trim().parse().unwrap();
1631 self.prerelease_version.set(Some(n));
1632 n
1633 }
1634
1635 fn rust_release(&self) -> String {
1637 self.release(&self.version)
1638 }
1639
1640 fn rust_package_vers(&self) -> String {
1646 match &self.config.channel[..] {
1647 "stable" => self.version.to_string(),
1648 "beta" => "beta".to_string(),
1649 "nightly" => "nightly".to_string(),
1650 _ => format!("{}-dev", self.version),
1651 }
1652 }
1653
1654 fn rust_version(&self) -> String {
1660 let mut version = self.rust_info().version(self, &self.version);
1661 if let Some(ref s) = self.config.description
1662 && !s.is_empty()
1663 {
1664 version.push_str(" (");
1665 version.push_str(s);
1666 version.push(')');
1667 }
1668 version
1669 }
1670
1671 fn rust_sha(&self) -> Option<&str> {
1673 self.rust_info().sha()
1674 }
1675
1676 fn release_num(&self, package: &str) -> String {
1678 let toml_file_name = self.src.join(format!("src/tools/{package}/Cargo.toml"));
1679 let toml = t!(fs::read_to_string(toml_file_name));
1680 for line in toml.lines() {
1681 if let Some(stripped) =
1682 line.strip_prefix("version = \"").and_then(|s| s.strip_suffix('"'))
1683 {
1684 return stripped.to_owned();
1685 }
1686 }
1687
1688 panic!("failed to find version in {package}'s Cargo.toml")
1689 }
1690
1691 fn unstable_features(&self) -> bool {
1694 !matches!(&self.config.channel[..], "stable" | "beta")
1695 }
1696
1697 fn in_tree_crates(&self, root: &str, target: Option<TargetSelection>) -> Vec<&Crate> {
1701 let mut ret = Vec::new();
1702 let mut list = vec![root.to_owned()];
1703 let mut visited = HashSet::new();
1704 while let Some(krate) = list.pop() {
1705 let krate = self
1706 .crates
1707 .get(&krate)
1708 .unwrap_or_else(|| panic!("metadata missing for {krate}: {:?}", self.crates));
1709 ret.push(krate);
1710 for dep in &krate.deps {
1711 if !self.crates.contains_key(dep) {
1712 continue;
1714 }
1715 if visited.insert(dep)
1721 && (dep != "profiler_builtins"
1722 || target
1723 .map(|t| self.config.profiler_enabled(t))
1724 .unwrap_or_else(|| self.config.any_profiler_enabled()))
1725 && (dep != "rustc_codegen_llvm"
1726 || self.config.hosts.iter().any(|host| self.config.llvm_enabled(*host)))
1727 {
1728 list.push(dep.clone());
1729 }
1730 }
1731 }
1732 ret.sort_unstable_by_key(|krate| krate.name.clone()); ret
1734 }
1735
1736 fn read_stamp_file(&self, stamp: &BuildStamp) -> Vec<(PathBuf, DependencyType)> {
1737 if self.config.dry_run() {
1738 return Vec::new();
1739 }
1740
1741 if !stamp.path().exists() {
1742 eprintln!(
1743 "ERROR: Unable to find the stamp file {}, did you try to keep a nonexistent build stage?",
1744 stamp.path().display()
1745 );
1746 crate::exit!(1);
1747 }
1748
1749 let mut paths = Vec::new();
1750 let contents = t!(fs::read(stamp.path()), stamp.path());
1751 for part in contents.split(|b| *b == 0) {
1754 if part.is_empty() {
1755 continue;
1756 }
1757 let dependency_type = match part[0] as char {
1758 'h' => DependencyType::Host,
1759 's' => DependencyType::TargetSelfContained,
1760 't' => DependencyType::Target,
1761 _ => unreachable!(),
1762 };
1763 let path = PathBuf::from(t!(str::from_utf8(&part[1..])));
1764 paths.push((path, dependency_type));
1765 }
1766 paths
1767 }
1768
1769 #[track_caller]
1774 pub fn resolve_symlink_and_copy(&self, src: &Path, dst: &Path) {
1775 self.copy_link_internal(src, dst, true);
1776 }
1777
1778 #[track_caller]
1783 pub fn copy_link(&self, src: &Path, dst: &Path, file_type: FileType) {
1784 self.copy_link_internal(src, dst, false);
1785
1786 if file_type.could_have_split_debuginfo()
1787 && let Some(dbg_file) = split_debuginfo(src)
1788 {
1789 self.copy_link_internal(
1790 &dbg_file,
1791 &dst.with_extension(dbg_file.extension().unwrap()),
1792 false,
1793 );
1794 }
1795 }
1796
1797 #[track_caller]
1798 fn copy_link_internal(&self, src: &Path, dst: &Path, dereference_symlinks: bool) {
1799 if self.config.dry_run() {
1800 return;
1801 }
1802 if src == dst {
1803 return;
1804 }
1805
1806 #[cfg(feature = "tracing")]
1807 let _span = trace_io!("file-copy-link", ?src, ?dst);
1808
1809 if let Err(e) = fs::remove_file(dst)
1810 && cfg!(windows)
1811 && e.kind() != io::ErrorKind::NotFound
1812 {
1813 let now = t!(SystemTime::now().duration_since(SystemTime::UNIX_EPOCH));
1816 let _ = fs::rename(dst, format!("{}-{}", dst.display(), now.as_nanos()));
1817 }
1818 let mut metadata = t!(src.symlink_metadata(), format!("src = {}", src.display()));
1819 let mut src = src.to_path_buf();
1820 if metadata.file_type().is_symlink() {
1821 if dereference_symlinks {
1822 src = t!(fs::canonicalize(src));
1823 metadata = t!(fs::metadata(&src), format!("target = {}", src.display()));
1824 } else {
1825 let link = t!(fs::read_link(src));
1826 t!(self.symlink_file(link, dst));
1827 return;
1828 }
1829 }
1830 if let Ok(()) = fs::hard_link(&src, dst) {
1831 } else {
1834 if let Err(e) = fs::copy(&src, dst) {
1835 panic!("failed to copy `{}` to `{}`: {}", src.display(), dst.display(), e)
1836 }
1837 t!(fs::set_permissions(dst, metadata.permissions()));
1838
1839 let file_times = fs::FileTimes::new()
1842 .set_accessed(t!(metadata.accessed()))
1843 .set_modified(t!(metadata.modified()));
1844 t!(set_file_times(dst, file_times));
1845 }
1846 }
1847
1848 #[track_caller]
1852 pub fn cp_link_r(&self, src: &Path, dst: &Path) {
1853 if self.config.dry_run() {
1854 return;
1855 }
1856 for f in self.read_dir(src) {
1857 let path = f.path();
1858 let name = path.file_name().unwrap();
1859 let dst = dst.join(name);
1860 if t!(f.file_type()).is_dir() {
1861 t!(fs::create_dir_all(&dst));
1862 self.cp_link_r(&path, &dst);
1863 } else {
1864 self.copy_link(&path, &dst, FileType::Regular);
1865 }
1866 }
1867 }
1868
1869 #[track_caller]
1875 pub fn cp_link_filtered(&self, src: &Path, dst: &Path, filter: &dyn Fn(&Path) -> bool) {
1876 self.cp_link_filtered_recurse(src, dst, Path::new(""), filter)
1878 }
1879
1880 #[track_caller]
1882 fn cp_link_filtered_recurse(
1883 &self,
1884 src: &Path,
1885 dst: &Path,
1886 relative: &Path,
1887 filter: &dyn Fn(&Path) -> bool,
1888 ) {
1889 for f in self.read_dir(src) {
1890 let path = f.path();
1891 let name = path.file_name().unwrap();
1892 let dst = dst.join(name);
1893 let relative = relative.join(name);
1894 if filter(&relative) {
1896 if t!(f.file_type()).is_dir() {
1897 let _ = fs::remove_dir_all(&dst);
1898 self.create_dir(&dst);
1899 self.cp_link_filtered_recurse(&path, &dst, &relative, filter);
1900 } else {
1901 self.copy_link(&path, &dst, FileType::Regular);
1902 }
1903 }
1904 }
1905 }
1906
1907 fn copy_link_to_folder(&self, src: &Path, dest_folder: &Path) {
1908 let file_name = src.file_name().unwrap();
1909 let dest = dest_folder.join(file_name);
1910 self.copy_link(src, &dest, FileType::Regular);
1911 }
1912
1913 fn install(&self, src: &Path, dstdir: &Path, file_type: FileType) {
1914 if self.config.dry_run() {
1915 return;
1916 }
1917 let dst = dstdir.join(src.file_name().unwrap());
1918
1919 #[cfg(feature = "tracing")]
1920 let _span = trace_io!("install", ?src, ?dst);
1921
1922 t!(fs::create_dir_all(dstdir));
1923 if !src.exists() {
1924 panic!("ERROR: File \"{}\" not found!", src.display());
1925 }
1926
1927 self.copy_link_internal(src, &dst, true);
1928 chmod(&dst, file_type.perms());
1929
1930 if file_type.could_have_split_debuginfo()
1932 && let Some(dbg_file) = split_debuginfo(src)
1933 {
1934 self.install(&dbg_file, dstdir, FileType::Regular);
1935 }
1936 }
1937
1938 fn read(&self, path: &Path) -> String {
1939 if self.config.dry_run() {
1940 return String::new();
1941 }
1942 t!(fs::read_to_string(path))
1943 }
1944
1945 #[track_caller]
1946 fn create_dir(&self, dir: &Path) {
1947 if self.config.dry_run() {
1948 return;
1949 }
1950
1951 #[cfg(feature = "tracing")]
1952 let _span = trace_io!("dir-create", ?dir);
1953
1954 t!(fs::create_dir_all(dir))
1955 }
1956
1957 fn remove_dir(&self, dir: &Path) {
1958 if self.config.dry_run() {
1959 return;
1960 }
1961
1962 #[cfg(feature = "tracing")]
1963 let _span = trace_io!("dir-remove", ?dir);
1964
1965 t!(fs::remove_dir_all(dir))
1966 }
1967
1968 fn clear_dir(&self, dir: &Path) {
1971 if self.config.dry_run() {
1972 return;
1973 }
1974
1975 #[cfg(feature = "tracing")]
1976 let _span = trace_io!("dir-clear", ?dir);
1977
1978 let _ = std::fs::remove_dir_all(dir);
1979 self.create_dir(dir);
1980 }
1981
1982 fn read_dir(&self, dir: &Path) -> impl Iterator<Item = fs::DirEntry> {
1983 let iter = match fs::read_dir(dir) {
1984 Ok(v) => v,
1985 Err(_) if self.config.dry_run() => return vec![].into_iter(),
1986 Err(err) => panic!("could not read dir {dir:?}: {err:?}"),
1987 };
1988 iter.map(|e| t!(e)).collect::<Vec<_>>().into_iter()
1989 }
1990
1991 fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(&self, src: P, link: Q) -> io::Result<()> {
1992 #[cfg(unix)]
1993 use std::os::unix::fs::symlink as symlink_file;
1994 #[cfg(windows)]
1995 use std::os::windows::fs::symlink_file;
1996 if !self.config.dry_run() { symlink_file(src.as_ref(), link.as_ref()) } else { Ok(()) }
1997 }
1998
1999 fn ninja(&self) -> bool {
2002 let mut cmd_finder = crate::core::sanity::Finder::new();
2003
2004 if self.config.ninja_in_file {
2005 if cmd_finder.maybe_have("ninja-build").is_none()
2008 && cmd_finder.maybe_have("ninja").is_none()
2009 {
2010 eprintln!(
2011 "
2012Couldn't find required command: ninja (or ninja-build)
2013
2014You should install ninja as described at
2015<https://github.com/ninja-build/ninja/wiki/Pre-built-Ninja-packages>,
2016or set `ninja = false` in the `[llvm]` section of `bootstrap.toml`.
2017Alternatively, set `download-ci-llvm = true` in that `[llvm]` section
2018to download LLVM rather than building it.
2019"
2020 );
2021 exit!(1);
2022 }
2023 }
2024
2025 if !self.config.ninja_in_file
2033 && self.config.host_target.is_msvc()
2034 && cmd_finder.maybe_have("ninja").is_some()
2035 {
2036 return true;
2037 }
2038
2039 self.config.ninja_in_file
2040 }
2041
2042 pub fn colored_stdout<R, F: FnOnce(&mut dyn WriteColor) -> R>(&self, f: F) -> R {
2043 self.colored_stream_inner(StandardStream::stdout, self.config.stdout_is_tty, f)
2044 }
2045
2046 pub fn colored_stderr<R, F: FnOnce(&mut dyn WriteColor) -> R>(&self, f: F) -> R {
2047 self.colored_stream_inner(StandardStream::stderr, self.config.stderr_is_tty, f)
2048 }
2049
2050 fn colored_stream_inner<R, F, C>(&self, constructor: C, is_tty: bool, f: F) -> R
2051 where
2052 C: Fn(ColorChoice) -> StandardStream,
2053 F: FnOnce(&mut dyn WriteColor) -> R,
2054 {
2055 let choice = match self.config.color {
2056 flags::Color::Always => ColorChoice::Always,
2057 flags::Color::Never => ColorChoice::Never,
2058 flags::Color::Auto if !is_tty => ColorChoice::Never,
2059 flags::Color::Auto => ColorChoice::Auto,
2060 };
2061 let mut stream = constructor(choice);
2062 let result = f(&mut stream);
2063 stream.reset().unwrap();
2064 result
2065 }
2066
2067 pub fn exec_ctx(&self) -> &ExecutionContext {
2068 &self.config.exec_ctx
2069 }
2070
2071 pub fn report_summary(&self, path: &Path, start_time: Instant) {
2072 self.config.exec_ctx.profiler().report_summary(path, start_time);
2073 }
2074
2075 #[cfg(feature = "tracing")]
2076 pub fn report_step_graph(self, directory: &Path) {
2077 self.step_graph.into_inner().store_to_dot_files(directory);
2078 }
2079}
2080
2081impl AsRef<ExecutionContext> for Build {
2082 fn as_ref(&self) -> &ExecutionContext {
2083 &self.config.exec_ctx
2084 }
2085}
2086
2087#[cfg(unix)]
2088fn chmod(path: &Path, perms: u32) {
2089 use std::os::unix::fs::*;
2090 t!(fs::set_permissions(path, fs::Permissions::from_mode(perms)));
2091}
2092#[cfg(windows)]
2093fn chmod(_path: &Path, _perms: u32) {}
2094
2095impl Compiler {
2096 pub fn new(stage: u32, host: TargetSelection) -> Self {
2097 Self { stage, host, forced_compiler: false }
2098 }
2099
2100 pub fn forced_compiler(&mut self, forced_compiler: bool) {
2101 self.forced_compiler = forced_compiler;
2102 }
2103
2104 pub fn is_snapshot(&self, build: &Build) -> bool {
2106 self.stage == 0 && self.host == build.host_target
2107 }
2108
2109 pub fn is_forced_compiler(&self) -> bool {
2111 self.forced_compiler
2112 }
2113}
2114
2115fn envify(s: &str) -> String {
2116 s.chars()
2117 .map(|c| match c {
2118 '-' => '_',
2119 c => c,
2120 })
2121 .flat_map(|c| c.to_uppercase())
2122 .collect()
2123}
2124
2125pub fn prepare_behaviour_dump_dir(build: &Build) {
2127 static INITIALIZED: OnceLock<bool> = OnceLock::new();
2128
2129 let dump_path = build.out.join("bootstrap-shims-dump");
2130
2131 let initialized = INITIALIZED.get().unwrap_or(&false);
2132 if !initialized {
2133 if dump_path.exists() {
2135 t!(fs::remove_dir_all(&dump_path));
2136 }
2137
2138 t!(fs::create_dir_all(&dump_path));
2139
2140 t!(INITIALIZED.set(true));
2141 }
2142}