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::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::{DryRun, LldMode, LlvmLibunwind, TargetSelection, flags};
39use crate::utils::exec::{BootstrapCommand, command};
40use crate::utils::helpers::{
41 self, dir_is_empty, exe, libdir, set_file_times, split_debuginfo, symlink_dir,
42};
43
44mod core;
45mod utils;
46
47pub use core::builder::PathSet;
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;
57
58use crate::core::build_steps::vendor::VENDOR_DIR;
59
60const LLVM_TOOLS: &[&str] = &[
61 "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", ];
76
77const LLD_FILE_NAMES: &[&str] = &["ld.lld", "ld64.lld", "lld-link", "wasm-ld"];
79
80#[expect(clippy::type_complexity)] const EXTRA_CHECK_CFGS: &[(Option<Mode>, &str, Option<&[&'static str]>)] = &[
84 (Some(Mode::Rustc), "bootstrap", None),
85 (Some(Mode::Codegen), "bootstrap", None),
86 (Some(Mode::ToolRustc), "bootstrap", None),
87 (Some(Mode::ToolStd), "bootstrap", None),
88 (Some(Mode::Rustc), "llvm_enzyme", None),
89 (Some(Mode::Codegen), "llvm_enzyme", None),
90 (Some(Mode::ToolRustc), "llvm_enzyme", None),
91 (Some(Mode::ToolRustc), "rust_analyzer", None),
92 (Some(Mode::ToolStd), "rust_analyzer", None),
93 ];
97
98#[derive(Eq, PartialOrd, Ord, Clone, Copy, Debug)]
104pub struct Compiler {
105 stage: u32,
106 host: TargetSelection,
107 forced_compiler: bool,
111}
112
113impl std::hash::Hash for Compiler {
114 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
115 self.stage.hash(state);
116 self.host.hash(state);
117 }
118}
119
120impl PartialEq for Compiler {
121 fn eq(&self, other: &Self) -> bool {
122 self.stage == other.stage && self.host == other.host
123 }
124}
125
126#[derive(PartialEq, Eq, Copy, Clone, Debug)]
127pub enum DocTests {
128 Yes,
130 No,
132 Only,
134}
135
136pub enum GitRepo {
137 Rustc,
138 Llvm,
139}
140
141#[derive(Clone)]
152pub struct Build {
153 config: Config,
155
156 version: String,
158
159 src: PathBuf,
161 out: PathBuf,
162 bootstrap_out: PathBuf,
163 cargo_info: GitInfo,
164 rust_analyzer_info: GitInfo,
165 clippy_info: GitInfo,
166 miri_info: GitInfo,
167 rustfmt_info: GitInfo,
168 enzyme_info: GitInfo,
169 in_tree_llvm_info: GitInfo,
170 in_tree_gcc_info: GitInfo,
171 local_rebuild: bool,
172 fail_fast: bool,
173 doc_tests: DocTests,
174 verbosity: usize,
175
176 host_target: TargetSelection,
178 hosts: Vec<TargetSelection>,
180 targets: Vec<TargetSelection>,
182
183 initial_rustc: PathBuf,
184 initial_rustdoc: PathBuf,
185 initial_cargo: PathBuf,
186 initial_lld: PathBuf,
187 initial_relative_libdir: PathBuf,
188 initial_sysroot: PathBuf,
189
190 cc: HashMap<TargetSelection, cc::Tool>,
193 cxx: HashMap<TargetSelection, cc::Tool>,
194 ar: HashMap<TargetSelection, PathBuf>,
195 ranlib: HashMap<TargetSelection, PathBuf>,
196 wasi_sdk_path: Option<PathBuf>,
197
198 crates: HashMap<String, Crate>,
201 crate_paths: HashMap<PathBuf, String>,
202 is_sudo: bool,
203 prerelease_version: Cell<Option<u32>>,
204
205 #[cfg(feature = "build-metrics")]
206 metrics: crate::utils::metrics::BuildMetrics,
207}
208
209#[derive(Debug, Clone)]
210struct Crate {
211 name: String,
212 deps: HashSet<String>,
213 path: PathBuf,
214 features: Vec<String>,
215}
216
217impl Crate {
218 fn local_path(&self, build: &Build) -> PathBuf {
219 self.path.strip_prefix(&build.config.src).unwrap().into()
220 }
221}
222
223#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
225pub enum DependencyType {
226 Host,
228 Target,
230 TargetSelfContained,
232}
233
234#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
239pub enum Mode {
240 Std,
242
243 Rustc,
245
246 Codegen,
248
249 ToolBootstrap,
261
262 ToolStd,
266
267 ToolRustc,
272}
273
274impl Mode {
275 pub fn is_tool(&self) -> bool {
276 matches!(self, Mode::ToolBootstrap | Mode::ToolRustc | Mode::ToolStd)
277 }
278
279 pub fn must_support_dlopen(&self) -> bool {
280 matches!(self, Mode::Std | Mode::Codegen)
281 }
282}
283
284pub enum RemapScheme {
288 Compiler,
290 NonCompiler,
292}
293
294#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
295pub enum CLang {
296 C,
297 Cxx,
298}
299
300#[derive(Debug, Clone, Copy, PartialEq, Eq)]
301pub enum FileType {
302 Executable,
304 NativeLibrary,
306 Script,
308 Regular,
310}
311
312impl FileType {
313 pub fn perms(self) -> u32 {
315 match self {
316 FileType::Executable | FileType::Script => 0o755,
317 FileType::Regular | FileType::NativeLibrary => 0o644,
318 }
319 }
320
321 pub fn could_have_split_debuginfo(self) -> bool {
322 match self {
323 FileType::Executable | FileType::NativeLibrary => true,
324 FileType::Script | FileType::Regular => false,
325 }
326 }
327}
328
329macro_rules! forward {
330 ( $( $fn:ident( $($param:ident: $ty:ty),* ) $( -> $ret:ty)? ),+ $(,)? ) => {
331 impl Build {
332 $( fn $fn(&self, $($param: $ty),* ) $( -> $ret)? {
333 self.config.$fn( $($param),* )
334 } )+
335 }
336 }
337}
338
339forward! {
340 verbose(f: impl Fn()),
341 is_verbose() -> bool,
342 create(path: &Path, s: &str),
343 remove(f: &Path),
344 tempdir() -> PathBuf,
345 llvm_link_shared() -> bool,
346 download_rustc() -> bool,
347}
348
349impl Build {
350 pub fn new(mut config: Config) -> Build {
355 let src = config.src.clone();
356 let out = config.out.clone();
357
358 #[cfg(unix)]
359 let is_sudo = match env::var_os("SUDO_USER") {
362 Some(_sudo_user) => {
363 let uid = unsafe { libc::getuid() };
368 uid == 0
369 }
370 None => false,
371 };
372 #[cfg(not(unix))]
373 let is_sudo = false;
374
375 let rust_info = config.rust_info.clone();
376 let cargo_info = config.cargo_info.clone();
377 let rust_analyzer_info = config.rust_analyzer_info.clone();
378 let clippy_info = config.clippy_info.clone();
379 let miri_info = config.miri_info.clone();
380 let rustfmt_info = config.rustfmt_info.clone();
381 let enzyme_info = config.enzyme_info.clone();
382 let in_tree_llvm_info = config.in_tree_llvm_info.clone();
383 let in_tree_gcc_info = config.in_tree_gcc_info.clone();
384
385 let initial_target_libdir = command(&config.initial_rustc)
386 .run_in_dry_run()
387 .args(["--print", "target-libdir"])
388 .run_capture_stdout(&config)
389 .stdout()
390 .trim()
391 .to_owned();
392
393 let initial_target_dir = Path::new(&initial_target_libdir)
394 .parent()
395 .unwrap_or_else(|| panic!("{initial_target_libdir} has no parent"));
396
397 let initial_lld = initial_target_dir.join("bin").join("rust-lld");
398
399 let initial_relative_libdir = if cfg!(test) {
400 PathBuf::default()
402 } else {
403 let ancestor = initial_target_dir.ancestors().nth(2).unwrap_or_else(|| {
404 panic!("Not enough ancestors for {}", initial_target_dir.display())
405 });
406
407 ancestor
408 .strip_prefix(&config.initial_sysroot)
409 .unwrap_or_else(|_| {
410 panic!(
411 "Couldn’t resolve the initial relative libdir from {}",
412 initial_target_dir.display()
413 )
414 })
415 .to_path_buf()
416 };
417
418 let version = std::fs::read_to_string(src.join("src").join("version"))
419 .expect("failed to read src/version");
420 let version = version.trim();
421
422 let mut bootstrap_out = std::env::current_exe()
423 .expect("could not determine path to running process")
424 .parent()
425 .unwrap()
426 .to_path_buf();
427 if bootstrap_out.ends_with("deps") {
430 bootstrap_out.pop();
431 }
432 if !bootstrap_out.join(exe("rustc", config.host_target)).exists() && !cfg!(test) {
433 panic!(
435 "`rustc` not found in {}, run `cargo build --bins` before `cargo run`",
436 bootstrap_out.display()
437 )
438 }
439
440 if rust_info.is_from_tarball() && config.description.is_none() {
441 config.description = Some("built from a source tarball".to_owned());
442 }
443
444 let mut build = Build {
445 initial_lld,
446 initial_relative_libdir,
447 initial_rustc: config.initial_rustc.clone(),
448 initial_rustdoc: config
449 .initial_rustc
450 .with_file_name(exe("rustdoc", config.host_target)),
451 initial_cargo: config.initial_cargo.clone(),
452 initial_sysroot: config.initial_sysroot.clone(),
453 local_rebuild: config.local_rebuild,
454 fail_fast: config.cmd.fail_fast(),
455 doc_tests: config.cmd.doc_tests(),
456 verbosity: config.verbose,
457
458 host_target: config.host_target,
459 hosts: config.hosts.clone(),
460 targets: config.targets.clone(),
461
462 config,
463 version: version.to_string(),
464 src,
465 out,
466 bootstrap_out,
467
468 cargo_info,
469 rust_analyzer_info,
470 clippy_info,
471 miri_info,
472 rustfmt_info,
473 enzyme_info,
474 in_tree_llvm_info,
475 in_tree_gcc_info,
476 cc: HashMap::new(),
477 cxx: HashMap::new(),
478 ar: HashMap::new(),
479 ranlib: HashMap::new(),
480 wasi_sdk_path: env::var_os("WASI_SDK_PATH").map(PathBuf::from),
481 crates: HashMap::new(),
482 crate_paths: HashMap::new(),
483 is_sudo,
484 prerelease_version: Cell::new(None),
485
486 #[cfg(feature = "build-metrics")]
487 metrics: crate::utils::metrics::BuildMetrics::init(),
488 };
489
490 let local_version_verbose = command(&build.initial_rustc)
493 .run_in_dry_run()
494 .args(["--version", "--verbose"])
495 .run_capture_stdout(&build)
496 .stdout();
497 let local_release = local_version_verbose
498 .lines()
499 .filter_map(|x| x.strip_prefix("release:"))
500 .next()
501 .unwrap()
502 .trim();
503 if local_release.split('.').take(2).eq(version.split('.').take(2)) {
504 build.verbose(|| println!("auto-detected local-rebuild {local_release}"));
505 build.local_rebuild = true;
506 }
507
508 build.verbose(|| println!("finding compilers"));
509 utils::cc_detect::fill_compilers(&mut build);
510 if !matches!(build.config.cmd, Subcommand::Setup { .. }) {
516 build.verbose(|| println!("running sanity check"));
517 crate::core::sanity::check(&mut build);
518
519 let rust_submodules = ["library/backtrace"];
522 for s in rust_submodules {
523 build.require_submodule(
524 s,
525 Some(
526 "The submodule is required for the standard library \
527 and the main Cargo workspace.",
528 ),
529 );
530 }
531 build.update_existing_submodules();
533
534 build.verbose(|| println!("learning about cargo"));
535 crate::core::metadata::build(&mut build);
536 }
537
538 let build_triple = build.out.join(build.host_target);
540 t!(fs::create_dir_all(&build_triple));
541 let host = build.out.join("host");
542 if host.is_symlink() {
543 #[cfg(windows)]
546 t!(fs::remove_dir(&host));
547 #[cfg(not(windows))]
548 t!(fs::remove_file(&host));
549 }
550 t!(
551 symlink_dir(&build.config, &build_triple, &host),
552 format!("symlink_dir({} => {}) failed", host.display(), build_triple.display())
553 );
554
555 build
556 }
557
558 #[cfg_attr(
567 feature = "tracing",
568 instrument(
569 level = "trace",
570 name = "Build::require_submodule",
571 skip_all,
572 fields(submodule = submodule),
573 ),
574 )]
575 pub fn require_submodule(&self, submodule: &str, err_hint: Option<&str>) {
576 if self.rust_info().is_from_tarball() {
577 return;
578 }
579
580 if cfg!(test) && !self.config.submodules() {
583 return;
584 }
585 self.config.update_submodule(submodule);
586 let absolute_path = self.config.src.join(submodule);
587 if !absolute_path.exists() || dir_is_empty(&absolute_path) {
588 let maybe_enable = if !self.config.submodules()
589 && self.config.rust_info.is_managed_git_subrepository()
590 {
591 "\nConsider setting `build.submodules = true` or manually initializing the submodules."
592 } else {
593 ""
594 };
595 let err_hint = err_hint.map_or_else(String::new, |e| format!("\n{e}"));
596 eprintln!(
597 "submodule {submodule} does not appear to be checked out, \
598 but it is required for this step{maybe_enable}{err_hint}"
599 );
600 exit!(1);
601 }
602 }
603
604 fn update_existing_submodules(&self) {
607 if !self.config.submodules() {
610 return;
611 }
612 let output = helpers::git(Some(&self.src))
613 .args(["config", "--file"])
614 .arg(".gitmodules")
615 .args(["--get-regexp", "path"])
616 .run_capture(self)
617 .stdout();
618 std::thread::scope(|s| {
619 for line in output.lines() {
622 let submodule = line.split_once(' ').unwrap().1;
623 let config = self.config.clone();
624 s.spawn(move || {
625 Self::update_existing_submodule(&config, submodule);
626 });
627 }
628 });
629 }
630
631 pub fn update_existing_submodule(config: &Config, submodule: &str) {
633 if !config.submodules() {
635 return;
636 }
637
638 if config.git_info(false, Path::new(submodule)).is_managed_git_subrepository() {
639 config.update_submodule(submodule);
640 }
641 }
642
643 #[cfg_attr(feature = "tracing", instrument(level = "debug", name = "Build::build", skip_all))]
645 pub fn build(&mut self) {
646 trace!("setting up job management");
647 unsafe {
648 crate::utils::job::setup(self);
649 }
650
651 {
653 #[cfg(feature = "tracing")]
654 let _hardcoded_span = span!(
655 tracing::Level::DEBUG,
656 "handling hardcoded subcommands (Format, Suggest, Perf)"
657 )
658 .entered();
659
660 match &self.config.cmd {
661 Subcommand::Format { check, all } => {
662 return core::build_steps::format::format(
663 &builder::Builder::new(self),
664 *check,
665 *all,
666 &self.config.paths,
667 );
668 }
669 Subcommand::Suggest { run } => {
670 return core::build_steps::suggest::suggest(&builder::Builder::new(self), *run);
671 }
672 Subcommand::Perf(args) => {
673 return core::build_steps::perf::perf(&builder::Builder::new(self), args);
674 }
675 _cmd => {
676 debug!(cmd = ?_cmd, "not a hardcoded subcommand; returning to normal handling");
677 }
678 }
679
680 debug!("handling subcommand normally");
681 }
682
683 if !self.config.dry_run() {
684 #[cfg(feature = "tracing")]
685 let _real_run_span = span!(tracing::Level::DEBUG, "executing real run").entered();
686
687 {
690 #[cfg(feature = "tracing")]
691 let _sanity_check_span =
692 span!(tracing::Level::DEBUG, "(1) executing dry-run sanity-check").entered();
693 self.config.set_dry_run(DryRun::SelfCheck);
694 let builder = builder::Builder::new(self);
695 builder.execute_cli();
696 }
697
698 {
700 #[cfg(feature = "tracing")]
701 let _actual_run_span =
702 span!(tracing::Level::DEBUG, "(2) executing actual run").entered();
703 self.config.set_dry_run(DryRun::Disabled);
704 let builder = builder::Builder::new(self);
705 builder.execute_cli();
706 }
707 } else {
708 #[cfg(feature = "tracing")]
709 let _dry_run_span = span!(tracing::Level::DEBUG, "executing dry run").entered();
710
711 let builder = builder::Builder::new(self);
712 builder.execute_cli();
713 }
714
715 #[cfg(feature = "tracing")]
716 debug!("checking for postponed test failures from `test --no-fail-fast`");
717
718 self.config.exec_ctx().report_failures_and_exit();
720
721 #[cfg(feature = "build-metrics")]
722 self.metrics.persist(self);
723 }
724
725 fn rust_info(&self) -> &GitInfo {
726 &self.config.rust_info
727 }
728
729 fn std_features(&self, target: TargetSelection) -> String {
732 let mut features: BTreeSet<&str> =
733 self.config.rust_std_features.iter().map(|s| s.as_str()).collect();
734
735 match self.config.llvm_libunwind(target) {
736 LlvmLibunwind::InTree => features.insert("llvm-libunwind"),
737 LlvmLibunwind::System => features.insert("system-llvm-libunwind"),
738 LlvmLibunwind::No => false,
739 };
740
741 if self.config.backtrace {
742 features.insert("backtrace");
743 }
744
745 if self.config.profiler_enabled(target) {
746 features.insert("profiler");
747 }
748
749 if target.contains("zkvm") {
751 features.insert("compiler-builtins-mem");
752 }
753
754 features.into_iter().collect::<Vec<_>>().join(" ")
755 }
756
757 fn rustc_features(&self, kind: Kind, target: TargetSelection, crates: &[String]) -> String {
759 let possible_features_by_crates: HashSet<_> = crates
760 .iter()
761 .flat_map(|krate| &self.crates[krate].features)
762 .map(std::ops::Deref::deref)
763 .collect();
764 let check = |feature: &str| -> bool {
765 crates.is_empty() || possible_features_by_crates.contains(feature)
766 };
767 let mut features = vec![];
768 if self.config.jemalloc(target) && check("jemalloc") {
769 features.push("jemalloc");
770 }
771 if (self.config.llvm_enabled(target) || kind == Kind::Check) && check("llvm") {
772 features.push("llvm");
773 }
774 if self.config.rust_randomize_layout && check("rustc_randomized_layouts") {
776 features.push("rustc_randomized_layouts");
777 }
778
779 if !self.config.rust_debug_logging && check("max_level_info") {
785 features.push("max_level_info");
786 }
787
788 features.join(" ")
789 }
790
791 fn cargo_dir(&self) -> &'static str {
794 if self.config.rust_optimize.is_release() { "release" } else { "debug" }
795 }
796
797 fn tools_dir(&self, compiler: Compiler) -> PathBuf {
798 let out = self.out.join(compiler.host).join(format!("stage{}-tools-bin", compiler.stage));
799 t!(fs::create_dir_all(&out));
800 out
801 }
802
803 fn stage_out(&self, compiler: Compiler, mode: Mode) -> PathBuf {
808 let suffix = match mode {
809 Mode::Std => "-std",
810 Mode::Rustc => "-rustc",
811 Mode::Codegen => "-codegen",
812 Mode::ToolBootstrap => {
813 return self.out.join(compiler.host).join("bootstrap-tools");
814 }
815 Mode::ToolStd | Mode::ToolRustc => "-tools",
816 };
817 self.out.join(compiler.host).join(format!("stage{}{}", compiler.stage, suffix))
818 }
819
820 fn cargo_out(&self, compiler: Compiler, mode: Mode, target: TargetSelection) -> PathBuf {
824 self.stage_out(compiler, mode).join(target).join(self.cargo_dir())
825 }
826
827 fn llvm_out(&self, target: TargetSelection) -> PathBuf {
832 if self.config.llvm_from_ci && self.config.is_host_target(target) {
833 self.config.ci_llvm_root()
834 } else {
835 self.out.join(target).join("llvm")
836 }
837 }
838
839 fn enzyme_out(&self, target: TargetSelection) -> PathBuf {
840 self.out.join(&*target.triple).join("enzyme")
841 }
842
843 fn gcc_out(&self, target: TargetSelection) -> PathBuf {
844 self.out.join(&*target.triple).join("gcc")
845 }
846
847 fn lld_out(&self, target: TargetSelection) -> PathBuf {
848 self.out.join(target).join("lld")
849 }
850
851 fn doc_out(&self, target: TargetSelection) -> PathBuf {
853 self.out.join(target).join("doc")
854 }
855
856 fn json_doc_out(&self, target: TargetSelection) -> PathBuf {
858 self.out.join(target).join("json-doc")
859 }
860
861 fn test_out(&self, target: TargetSelection) -> PathBuf {
862 self.out.join(target).join("test")
863 }
864
865 fn compiler_doc_out(&self, target: TargetSelection) -> PathBuf {
867 self.out.join(target).join("compiler-doc")
868 }
869
870 fn md_doc_out(&self, target: TargetSelection) -> PathBuf {
872 self.out.join(target).join("md-doc")
873 }
874
875 fn vendored_crates_path(&self) -> Option<PathBuf> {
877 if self.config.vendor { Some(self.src.join(VENDOR_DIR)) } else { None }
878 }
879
880 fn llvm_filecheck(&self, target: TargetSelection) -> PathBuf {
882 let target_config = self.config.target_config.get(&target);
883 if let Some(s) = target_config.and_then(|c| c.llvm_filecheck.as_ref()) {
884 s.to_path_buf()
885 } else if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
886 let llvm_bindir = command(s).arg("--bindir").run_capture_stdout(self).stdout();
887 let filecheck = Path::new(llvm_bindir.trim()).join(exe("FileCheck", target));
888 if filecheck.exists() {
889 filecheck
890 } else {
891 let llvm_libdir = command(s).arg("--libdir").run_capture_stdout(self).stdout();
894 let lib_filecheck =
895 Path::new(llvm_libdir.trim()).join("llvm").join(exe("FileCheck", target));
896 if lib_filecheck.exists() {
897 lib_filecheck
898 } else {
899 filecheck
903 }
904 }
905 } else {
906 let base = self.llvm_out(target).join("build");
907 let base = if !self.ninja() && target.is_msvc() {
908 if self.config.llvm_optimize {
909 if self.config.llvm_release_debuginfo {
910 base.join("RelWithDebInfo")
911 } else {
912 base.join("Release")
913 }
914 } else {
915 base.join("Debug")
916 }
917 } else {
918 base
919 };
920 base.join("bin").join(exe("FileCheck", target))
921 }
922 }
923
924 fn native_dir(&self, target: TargetSelection) -> PathBuf {
926 self.out.join(target).join("native")
927 }
928
929 fn test_helpers_out(&self, target: TargetSelection) -> PathBuf {
932 self.native_dir(target).join("rust-test-helpers")
933 }
934
935 fn add_rust_test_threads(&self, cmd: &mut BootstrapCommand) {
937 if env::var_os("RUST_TEST_THREADS").is_none() {
938 cmd.env("RUST_TEST_THREADS", self.jobs().to_string());
939 }
940 }
941
942 fn rustc_snapshot_libdir(&self) -> PathBuf {
944 self.rustc_snapshot_sysroot().join(libdir(self.config.host_target))
945 }
946
947 fn rustc_snapshot_sysroot(&self) -> &Path {
949 static SYSROOT_CACHE: OnceLock<PathBuf> = OnceLock::new();
950 SYSROOT_CACHE.get_or_init(|| {
951 command(&self.initial_rustc)
952 .run_in_dry_run()
953 .args(["--print", "sysroot"])
954 .run_capture_stdout(self)
955 .stdout()
956 .trim()
957 .to_owned()
958 .into()
959 })
960 }
961
962 pub fn is_verbose_than(&self, level: usize) -> bool {
964 self.verbosity > level
965 }
966
967 fn verbose_than(&self, level: usize, f: impl Fn()) {
969 if self.is_verbose_than(level) {
970 f()
971 }
972 }
973
974 fn info(&self, msg: &str) {
975 match self.config.get_dry_run() {
976 DryRun::SelfCheck => (),
977 DryRun::Disabled | DryRun::UserSelected => {
978 println!("{msg}");
979 }
980 }
981 }
982
983 #[must_use = "Groups should not be dropped until the Step finishes running"]
984 #[track_caller]
985 fn msg_clippy(
986 &self,
987 what: impl Display,
988 target: impl Into<Option<TargetSelection>>,
989 ) -> Option<gha::Group> {
990 self.msg(Kind::Clippy, self.config.stage, what, self.config.host_target, target)
991 }
992
993 #[must_use = "Groups should not be dropped until the Step finishes running"]
994 #[track_caller]
995 fn msg_check(
996 &self,
997 what: impl Display,
998 target: impl Into<Option<TargetSelection>>,
999 custom_stage: Option<u32>,
1000 ) -> Option<gha::Group> {
1001 self.msg(
1002 Kind::Check,
1003 custom_stage.unwrap_or(self.config.stage),
1004 what,
1005 self.config.host_target,
1006 target,
1007 )
1008 }
1009
1010 #[must_use = "Groups should not be dropped until the Step finishes running"]
1011 #[track_caller]
1012 fn msg_doc(
1013 &self,
1014 compiler: Compiler,
1015 what: impl Display,
1016 target: impl Into<Option<TargetSelection>> + Copy,
1017 ) -> Option<gha::Group> {
1018 self.msg(Kind::Doc, compiler.stage, what, compiler.host, target.into())
1019 }
1020
1021 #[must_use = "Groups should not be dropped until the Step finishes running"]
1022 #[track_caller]
1023 fn msg_build(
1024 &self,
1025 compiler: Compiler,
1026 what: impl Display,
1027 target: impl Into<Option<TargetSelection>>,
1028 ) -> Option<gha::Group> {
1029 self.msg(Kind::Build, compiler.stage, what, compiler.host, target)
1030 }
1031
1032 #[must_use = "Groups should not be dropped until the Step finishes running"]
1036 #[track_caller]
1037 fn msg(
1038 &self,
1039 action: impl Into<Kind>,
1040 stage: u32,
1041 what: impl Display,
1042 host: impl Into<Option<TargetSelection>>,
1043 target: impl Into<Option<TargetSelection>>,
1044 ) -> Option<gha::Group> {
1045 let action = action.into().description();
1046 let msg = |fmt| format!("{action} stage{stage} {what}{fmt}");
1047 let msg = if let Some(target) = target.into() {
1048 let host = host.into().unwrap();
1049 if host == target {
1050 msg(format_args!(" ({target})"))
1051 } else {
1052 msg(format_args!(" ({host} -> {target})"))
1053 }
1054 } else {
1055 msg(format_args!(""))
1056 };
1057 self.group(&msg)
1058 }
1059
1060 #[must_use = "Groups should not be dropped until the Step finishes running"]
1064 #[track_caller]
1065 fn msg_unstaged(
1066 &self,
1067 action: impl Into<Kind>,
1068 what: impl Display,
1069 target: TargetSelection,
1070 ) -> Option<gha::Group> {
1071 let action = action.into().description();
1072 let msg = format!("{action} {what} for {target}");
1073 self.group(&msg)
1074 }
1075
1076 #[must_use = "Groups should not be dropped until the Step finishes running"]
1077 #[track_caller]
1078 fn msg_sysroot_tool(
1079 &self,
1080 action: impl Into<Kind>,
1081 stage: u32,
1082 what: impl Display,
1083 host: TargetSelection,
1084 target: TargetSelection,
1085 ) -> Option<gha::Group> {
1086 let action = action.into().description();
1087 let msg = |fmt| format!("{action} {what} {fmt}");
1088 let msg = if host == target {
1089 msg(format_args!("(stage{stage} -> stage{}, {target})", stage + 1))
1090 } else {
1091 msg(format_args!("(stage{stage}:{host} -> stage{}:{target})", stage + 1))
1092 };
1093 self.group(&msg)
1094 }
1095
1096 #[track_caller]
1097 fn group(&self, msg: &str) -> Option<gha::Group> {
1098 match self.config.get_dry_run() {
1099 DryRun::SelfCheck => None,
1100 DryRun::Disabled | DryRun::UserSelected => Some(gha::group(msg)),
1101 }
1102 }
1103
1104 fn jobs(&self) -> u32 {
1107 self.config.jobs.unwrap_or_else(|| {
1108 std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get) as u32
1109 })
1110 }
1111
1112 fn debuginfo_map_to(&self, which: GitRepo, remap_scheme: RemapScheme) -> Option<String> {
1113 if !self.config.rust_remap_debuginfo {
1114 return None;
1115 }
1116
1117 match which {
1118 GitRepo::Rustc => {
1119 let sha = self.rust_sha().unwrap_or(&self.version);
1120
1121 match remap_scheme {
1122 RemapScheme::Compiler => {
1123 Some(format!("/rustc-dev/{sha}"))
1132 }
1133 RemapScheme::NonCompiler => {
1134 Some(format!("/rustc/{sha}"))
1136 }
1137 }
1138 }
1139 GitRepo::Llvm => Some(String::from("/rustc/llvm")),
1140 }
1141 }
1142
1143 fn cc(&self, target: TargetSelection) -> PathBuf {
1145 if self.config.dry_run() {
1146 return PathBuf::new();
1147 }
1148 self.cc[&target].path().into()
1149 }
1150
1151 fn cc_tool(&self, target: TargetSelection) -> Tool {
1153 self.cc[&target].clone()
1154 }
1155
1156 fn cxx_tool(&self, target: TargetSelection) -> Tool {
1158 self.cxx[&target].clone()
1159 }
1160
1161 fn cc_handled_clags(&self, target: TargetSelection, c: CLang) -> Vec<String> {
1164 if self.config.dry_run() {
1165 return Vec::new();
1166 }
1167 let base = match c {
1168 CLang::C => self.cc[&target].clone(),
1169 CLang::Cxx => self.cxx[&target].clone(),
1170 };
1171
1172 base.args()
1175 .iter()
1176 .map(|s| s.to_string_lossy().into_owned())
1177 .filter(|s| !s.starts_with("-O") && !s.starts_with("/O"))
1178 .collect::<Vec<String>>()
1179 }
1180
1181 fn cc_unhandled_cflags(
1183 &self,
1184 target: TargetSelection,
1185 which: GitRepo,
1186 c: CLang,
1187 ) -> Vec<String> {
1188 let mut base = Vec::new();
1189
1190 if matches!(c, CLang::Cxx) && target.contains("apple-darwin") {
1194 base.push("-stdlib=libc++".into());
1195 }
1196
1197 if &*target.triple == "i686-pc-windows-gnu" {
1201 base.push("-fno-omit-frame-pointer".into());
1202 }
1203
1204 if let Some(map_to) = self.debuginfo_map_to(which, RemapScheme::NonCompiler) {
1205 let map = format!("{}={}", self.src.display(), map_to);
1206 let cc = self.cc(target);
1207 if cc.ends_with("clang") || cc.ends_with("gcc") {
1208 base.push(format!("-fdebug-prefix-map={map}"));
1209 } else if cc.ends_with("clang-cl.exe") {
1210 base.push("-Xclang".into());
1211 base.push(format!("-fdebug-prefix-map={map}"));
1212 }
1213 }
1214 base
1215 }
1216
1217 fn ar(&self, target: TargetSelection) -> Option<PathBuf> {
1219 if self.config.dry_run() {
1220 return None;
1221 }
1222 self.ar.get(&target).cloned()
1223 }
1224
1225 fn ranlib(&self, target: TargetSelection) -> Option<PathBuf> {
1227 if self.config.dry_run() {
1228 return None;
1229 }
1230 self.ranlib.get(&target).cloned()
1231 }
1232
1233 fn cxx(&self, target: TargetSelection) -> Result<PathBuf, String> {
1235 if self.config.dry_run() {
1236 return Ok(PathBuf::new());
1237 }
1238 match self.cxx.get(&target) {
1239 Some(p) => Ok(p.path().into()),
1240 None => Err(format!("target `{target}` is not configured as a host, only as a target")),
1241 }
1242 }
1243
1244 fn linker(&self, target: TargetSelection) -> Option<PathBuf> {
1246 if self.config.dry_run() {
1247 return Some(PathBuf::new());
1248 }
1249 if let Some(linker) = self.config.target_config.get(&target).and_then(|c| c.linker.clone())
1250 {
1251 Some(linker)
1252 } else if target.contains("vxworks") {
1253 Some(self.cxx[&target].path().into())
1256 } else if !self.config.is_host_target(target)
1257 && helpers::use_host_linker(target)
1258 && !target.is_msvc()
1259 {
1260 Some(self.cc(target))
1261 } else if self.config.lld_mode.is_used()
1262 && self.is_lld_direct_linker(target)
1263 && self.host_target == target
1264 {
1265 match self.config.lld_mode {
1266 LldMode::SelfContained => Some(self.initial_lld.clone()),
1267 LldMode::External => Some("lld".into()),
1268 LldMode::Unused => None,
1269 }
1270 } else {
1271 None
1272 }
1273 }
1274
1275 fn is_lld_direct_linker(&self, target: TargetSelection) -> bool {
1278 target.is_msvc()
1279 }
1280
1281 fn crt_static(&self, target: TargetSelection) -> Option<bool> {
1283 if target.contains("pc-windows-msvc") {
1284 Some(true)
1285 } else {
1286 self.config.target_config.get(&target).and_then(|t| t.crt_static)
1287 }
1288 }
1289
1290 fn musl_root(&self, target: TargetSelection) -> Option<&Path> {
1292 self.config
1293 .target_config
1294 .get(&target)
1295 .and_then(|t| t.musl_root.as_ref())
1296 .or(self.config.musl_root.as_ref())
1297 .map(|p| &**p)
1298 }
1299
1300 fn musl_libdir(&self, target: TargetSelection) -> Option<PathBuf> {
1302 let t = self.config.target_config.get(&target)?;
1303 if let libdir @ Some(_) = &t.musl_libdir {
1304 return libdir.clone();
1305 }
1306 self.musl_root(target).map(|root| root.join("lib"))
1307 }
1308
1309 fn wasi_libdir(&self, target: TargetSelection) -> Option<PathBuf> {
1316 let configured =
1317 self.config.target_config.get(&target).and_then(|t| t.wasi_root.as_ref()).map(|p| &**p);
1318 if let Some(path) = configured {
1319 return Some(path.join("lib").join(target.to_string()));
1320 }
1321 let mut env_root = PathBuf::from(std::env::var_os("WASI_SDK_PATH")?);
1322 env_root.push("share");
1323 env_root.push("wasi-sysroot");
1324 env_root.push("lib");
1325 env_root.push(target.to_string());
1326 Some(env_root)
1327 }
1328
1329 fn no_std(&self, target: TargetSelection) -> Option<bool> {
1331 self.config.target_config.get(&target).map(|t| t.no_std)
1332 }
1333
1334 fn remote_tested(&self, target: TargetSelection) -> bool {
1337 self.qemu_rootfs(target).is_some()
1338 || target.contains("android")
1339 || env::var_os("TEST_DEVICE_ADDR").is_some()
1340 }
1341
1342 fn runner(&self, target: TargetSelection) -> Option<String> {
1348 let configured_runner =
1349 self.config.target_config.get(&target).and_then(|t| t.runner.as_ref()).map(|p| &**p);
1350 if let Some(runner) = configured_runner {
1351 return Some(runner.to_owned());
1352 }
1353
1354 if target.starts_with("wasm") && target.contains("wasi") {
1355 self.default_wasi_runner(target)
1356 } else {
1357 None
1358 }
1359 }
1360
1361 fn default_wasi_runner(&self, target: TargetSelection) -> Option<String> {
1365 let mut finder = crate::core::sanity::Finder::new();
1366
1367 if let Some(path) = finder.maybe_have("wasmtime")
1371 && let Ok(mut path) = path.into_os_string().into_string()
1372 {
1373 path.push_str(" run -C cache=n --dir .");
1374 path.push_str(" --env RUSTC_BOOTSTRAP");
1381
1382 if target.contains("wasip2") {
1383 path.push_str(" --wasi inherit-network --wasi allow-ip-name-lookup");
1384 }
1385
1386 return Some(path);
1387 }
1388
1389 None
1390 }
1391
1392 fn tool_enabled(&self, tool: &str) -> bool {
1397 if !self.config.extended {
1398 return false;
1399 }
1400 match &self.config.tools {
1401 Some(set) => set.contains(tool),
1402 None => true,
1403 }
1404 }
1405
1406 fn qemu_rootfs(&self, target: TargetSelection) -> Option<&Path> {
1412 self.config.target_config.get(&target).and_then(|t| t.qemu_rootfs.as_ref()).map(|p| &**p)
1413 }
1414
1415 fn python(&self) -> &Path {
1417 if self.config.host_target.ends_with("apple-darwin") {
1418 Path::new("/usr/bin/python3")
1422 } else {
1423 self.config
1424 .python
1425 .as_ref()
1426 .expect("python is required for running LLDB or rustdoc tests")
1427 }
1428 }
1429
1430 fn extended_error_dir(&self) -> PathBuf {
1432 self.out.join("tmp/extended-error-metadata")
1433 }
1434
1435 fn force_use_stage1(&self, stage: u32, target: TargetSelection) -> bool {
1454 !self.config.full_bootstrap
1455 && !self.config.download_rustc()
1456 && stage >= 2
1457 && (self.hosts.contains(&target) || target == self.host_target)
1458 }
1459
1460 fn force_use_stage2(&self, stage: u32) -> bool {
1466 self.config.download_rustc() && stage >= 2
1467 }
1468
1469 fn release(&self, num: &str) -> String {
1475 match &self.config.channel[..] {
1476 "stable" => num.to_string(),
1477 "beta" => {
1478 if !self.config.omit_git_hash {
1479 format!("{}-beta.{}", num, self.beta_prerelease_version())
1480 } else {
1481 format!("{num}-beta")
1482 }
1483 }
1484 "nightly" => format!("{num}-nightly"),
1485 _ => format!("{num}-dev"),
1486 }
1487 }
1488
1489 fn beta_prerelease_version(&self) -> u32 {
1490 fn extract_beta_rev_from_file<P: AsRef<Path>>(version_file: P) -> Option<String> {
1491 let version = fs::read_to_string(version_file).ok()?;
1492
1493 helpers::extract_beta_rev(&version)
1494 }
1495
1496 if let Some(s) = self.prerelease_version.get() {
1497 return s;
1498 }
1499
1500 let count = extract_beta_rev_from_file(self.src.join("version")).unwrap_or_else(|| {
1504 helpers::git(Some(&self.src))
1508 .arg("rev-list")
1509 .arg("--count")
1510 .arg("--merges")
1511 .arg(format!(
1512 "refs/remotes/origin/{}..HEAD",
1513 self.config.stage0_metadata.config.nightly_branch
1514 ))
1515 .run_in_dry_run()
1516 .run_capture(self)
1517 .stdout()
1518 });
1519 let n = count.trim().parse().unwrap();
1520 self.prerelease_version.set(Some(n));
1521 n
1522 }
1523
1524 fn rust_release(&self) -> String {
1526 self.release(&self.version)
1527 }
1528
1529 fn package_vers(&self, num: &str) -> String {
1536 match &self.config.channel[..] {
1537 "stable" => num.to_string(),
1538 "beta" => "beta".to_string(),
1539 "nightly" => "nightly".to_string(),
1540 _ => format!("{num}-dev"),
1541 }
1542 }
1543
1544 fn rust_package_vers(&self) -> String {
1546 self.package_vers(&self.version)
1547 }
1548
1549 fn rust_version(&self) -> String {
1555 let mut version = self.rust_info().version(self, &self.version);
1556 if let Some(ref s) = self.config.description
1557 && !s.is_empty()
1558 {
1559 version.push_str(" (");
1560 version.push_str(s);
1561 version.push(')');
1562 }
1563 version
1564 }
1565
1566 fn rust_sha(&self) -> Option<&str> {
1568 self.rust_info().sha()
1569 }
1570
1571 fn release_num(&self, package: &str) -> String {
1573 let toml_file_name = self.src.join(format!("src/tools/{package}/Cargo.toml"));
1574 let toml = t!(fs::read_to_string(toml_file_name));
1575 for line in toml.lines() {
1576 if let Some(stripped) =
1577 line.strip_prefix("version = \"").and_then(|s| s.strip_suffix('"'))
1578 {
1579 return stripped.to_owned();
1580 }
1581 }
1582
1583 panic!("failed to find version in {package}'s Cargo.toml")
1584 }
1585
1586 fn unstable_features(&self) -> bool {
1589 !matches!(&self.config.channel[..], "stable" | "beta")
1590 }
1591
1592 fn in_tree_crates(&self, root: &str, target: Option<TargetSelection>) -> Vec<&Crate> {
1596 let mut ret = Vec::new();
1597 let mut list = vec![root.to_owned()];
1598 let mut visited = HashSet::new();
1599 while let Some(krate) = list.pop() {
1600 let krate = self
1601 .crates
1602 .get(&krate)
1603 .unwrap_or_else(|| panic!("metadata missing for {krate}: {:?}", self.crates));
1604 ret.push(krate);
1605 for dep in &krate.deps {
1606 if !self.crates.contains_key(dep) {
1607 continue;
1609 }
1610 if visited.insert(dep)
1616 && (dep != "profiler_builtins"
1617 || target
1618 .map(|t| self.config.profiler_enabled(t))
1619 .unwrap_or_else(|| self.config.any_profiler_enabled()))
1620 && (dep != "rustc_codegen_llvm"
1621 || self.config.hosts.iter().any(|host| self.config.llvm_enabled(*host)))
1622 {
1623 list.push(dep.clone());
1624 }
1625 }
1626 }
1627 ret.sort_unstable_by_key(|krate| krate.name.clone()); ret
1629 }
1630
1631 fn read_stamp_file(&self, stamp: &BuildStamp) -> Vec<(PathBuf, DependencyType)> {
1632 if self.config.dry_run() {
1633 return Vec::new();
1634 }
1635
1636 if !stamp.path().exists() {
1637 eprintln!(
1638 "ERROR: Unable to find the stamp file {}, did you try to keep a nonexistent build stage?",
1639 stamp.path().display()
1640 );
1641 crate::exit!(1);
1642 }
1643
1644 let mut paths = Vec::new();
1645 let contents = t!(fs::read(stamp.path()), stamp.path());
1646 for part in contents.split(|b| *b == 0) {
1649 if part.is_empty() {
1650 continue;
1651 }
1652 let dependency_type = match part[0] as char {
1653 'h' => DependencyType::Host,
1654 's' => DependencyType::TargetSelfContained,
1655 't' => DependencyType::Target,
1656 _ => unreachable!(),
1657 };
1658 let path = PathBuf::from(t!(str::from_utf8(&part[1..])));
1659 paths.push((path, dependency_type));
1660 }
1661 paths
1662 }
1663
1664 pub fn resolve_symlink_and_copy(&self, src: &Path, dst: &Path) {
1669 self.copy_link_internal(src, dst, true);
1670 }
1671
1672 pub fn copy_link(&self, src: &Path, dst: &Path, file_type: FileType) {
1677 self.copy_link_internal(src, dst, false);
1678
1679 if file_type.could_have_split_debuginfo()
1680 && let Some(dbg_file) = split_debuginfo(src)
1681 {
1682 self.copy_link_internal(
1683 &dbg_file,
1684 &dst.with_extension(dbg_file.extension().unwrap()),
1685 false,
1686 );
1687 }
1688 }
1689
1690 fn copy_link_internal(&self, src: &Path, dst: &Path, dereference_symlinks: bool) {
1691 if self.config.dry_run() {
1692 return;
1693 }
1694 self.verbose_than(1, || println!("Copy/Link {src:?} to {dst:?}"));
1695 if src == dst {
1696 return;
1697 }
1698 if let Err(e) = fs::remove_file(dst)
1699 && cfg!(windows)
1700 && e.kind() != io::ErrorKind::NotFound
1701 {
1702 let now = t!(SystemTime::now().duration_since(SystemTime::UNIX_EPOCH));
1705 let _ = fs::rename(dst, format!("{}-{}", dst.display(), now.as_nanos()));
1706 }
1707 let mut metadata = t!(src.symlink_metadata(), format!("src = {}", src.display()));
1708 let mut src = src.to_path_buf();
1709 if metadata.file_type().is_symlink() {
1710 if dereference_symlinks {
1711 src = t!(fs::canonicalize(src));
1712 metadata = t!(fs::metadata(&src), format!("target = {}", src.display()));
1713 } else {
1714 let link = t!(fs::read_link(src));
1715 t!(self.symlink_file(link, dst));
1716 return;
1717 }
1718 }
1719 if let Ok(()) = fs::hard_link(&src, dst) {
1720 } else {
1723 if let Err(e) = fs::copy(&src, dst) {
1724 panic!("failed to copy `{}` to `{}`: {}", src.display(), dst.display(), e)
1725 }
1726 t!(fs::set_permissions(dst, metadata.permissions()));
1727
1728 let file_times = fs::FileTimes::new()
1731 .set_accessed(t!(metadata.accessed()))
1732 .set_modified(t!(metadata.modified()));
1733 t!(set_file_times(dst, file_times));
1734 }
1735 }
1736
1737 pub fn cp_link_r(&self, src: &Path, dst: &Path) {
1741 if self.config.dry_run() {
1742 return;
1743 }
1744 for f in self.read_dir(src) {
1745 let path = f.path();
1746 let name = path.file_name().unwrap();
1747 let dst = dst.join(name);
1748 if t!(f.file_type()).is_dir() {
1749 t!(fs::create_dir_all(&dst));
1750 self.cp_link_r(&path, &dst);
1751 } else {
1752 self.copy_link(&path, &dst, FileType::Regular);
1753 }
1754 }
1755 }
1756
1757 pub fn cp_link_filtered(&self, src: &Path, dst: &Path, filter: &dyn Fn(&Path) -> bool) {
1763 self.cp_link_filtered_recurse(src, dst, Path::new(""), filter)
1765 }
1766
1767 fn cp_link_filtered_recurse(
1769 &self,
1770 src: &Path,
1771 dst: &Path,
1772 relative: &Path,
1773 filter: &dyn Fn(&Path) -> bool,
1774 ) {
1775 for f in self.read_dir(src) {
1776 let path = f.path();
1777 let name = path.file_name().unwrap();
1778 let dst = dst.join(name);
1779 let relative = relative.join(name);
1780 if filter(&relative) {
1782 if t!(f.file_type()).is_dir() {
1783 let _ = fs::remove_dir_all(&dst);
1784 self.create_dir(&dst);
1785 self.cp_link_filtered_recurse(&path, &dst, &relative, filter);
1786 } else {
1787 let _ = fs::remove_file(&dst);
1788 self.copy_link(&path, &dst, FileType::Regular);
1789 }
1790 }
1791 }
1792 }
1793
1794 fn copy_link_to_folder(&self, src: &Path, dest_folder: &Path) {
1795 let file_name = src.file_name().unwrap();
1796 let dest = dest_folder.join(file_name);
1797 self.copy_link(src, &dest, FileType::Regular);
1798 }
1799
1800 fn install(&self, src: &Path, dstdir: &Path, file_type: FileType) {
1801 if self.config.dry_run() {
1802 return;
1803 }
1804 let dst = dstdir.join(src.file_name().unwrap());
1805 self.verbose_than(1, || println!("Install {src:?} to {dst:?}"));
1806 t!(fs::create_dir_all(dstdir));
1807 if !src.exists() {
1808 panic!("ERROR: File \"{}\" not found!", src.display());
1809 }
1810
1811 self.copy_link_internal(src, &dst, true);
1812 chmod(&dst, file_type.perms());
1813
1814 if file_type.could_have_split_debuginfo()
1816 && let Some(dbg_file) = split_debuginfo(src)
1817 {
1818 self.install(&dbg_file, dstdir, FileType::Regular);
1819 }
1820 }
1821
1822 fn read(&self, path: &Path) -> String {
1823 if self.config.dry_run() {
1824 return String::new();
1825 }
1826 t!(fs::read_to_string(path))
1827 }
1828
1829 fn create_dir(&self, dir: &Path) {
1830 if self.config.dry_run() {
1831 return;
1832 }
1833 t!(fs::create_dir_all(dir))
1834 }
1835
1836 fn remove_dir(&self, dir: &Path) {
1837 if self.config.dry_run() {
1838 return;
1839 }
1840 t!(fs::remove_dir_all(dir))
1841 }
1842
1843 fn read_dir(&self, dir: &Path) -> impl Iterator<Item = fs::DirEntry> {
1844 let iter = match fs::read_dir(dir) {
1845 Ok(v) => v,
1846 Err(_) if self.config.dry_run() => return vec![].into_iter(),
1847 Err(err) => panic!("could not read dir {dir:?}: {err:?}"),
1848 };
1849 iter.map(|e| t!(e)).collect::<Vec<_>>().into_iter()
1850 }
1851
1852 fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(&self, src: P, link: Q) -> io::Result<()> {
1853 #[cfg(unix)]
1854 use std::os::unix::fs::symlink as symlink_file;
1855 #[cfg(windows)]
1856 use std::os::windows::fs::symlink_file;
1857 if !self.config.dry_run() { symlink_file(src.as_ref(), link.as_ref()) } else { Ok(()) }
1858 }
1859
1860 fn ninja(&self) -> bool {
1863 let mut cmd_finder = crate::core::sanity::Finder::new();
1864
1865 if self.config.ninja_in_file {
1866 if cmd_finder.maybe_have("ninja-build").is_none()
1869 && cmd_finder.maybe_have("ninja").is_none()
1870 {
1871 eprintln!(
1872 "
1873Couldn't find required command: ninja (or ninja-build)
1874
1875You should install ninja as described at
1876<https://github.com/ninja-build/ninja/wiki/Pre-built-Ninja-packages>,
1877or set `ninja = false` in the `[llvm]` section of `bootstrap.toml`.
1878Alternatively, set `download-ci-llvm = true` in that `[llvm]` section
1879to download LLVM rather than building it.
1880"
1881 );
1882 exit!(1);
1883 }
1884 }
1885
1886 if !self.config.ninja_in_file
1894 && self.config.host_target.is_msvc()
1895 && cmd_finder.maybe_have("ninja").is_some()
1896 {
1897 return true;
1898 }
1899
1900 self.config.ninja_in_file
1901 }
1902
1903 pub fn colored_stdout<R, F: FnOnce(&mut dyn WriteColor) -> R>(&self, f: F) -> R {
1904 self.colored_stream_inner(StandardStream::stdout, self.config.stdout_is_tty, f)
1905 }
1906
1907 pub fn colored_stderr<R, F: FnOnce(&mut dyn WriteColor) -> R>(&self, f: F) -> R {
1908 self.colored_stream_inner(StandardStream::stderr, self.config.stderr_is_tty, f)
1909 }
1910
1911 fn colored_stream_inner<R, F, C>(&self, constructor: C, is_tty: bool, f: F) -> R
1912 where
1913 C: Fn(ColorChoice) -> StandardStream,
1914 F: FnOnce(&mut dyn WriteColor) -> R,
1915 {
1916 let choice = match self.config.color {
1917 flags::Color::Always => ColorChoice::Always,
1918 flags::Color::Never => ColorChoice::Never,
1919 flags::Color::Auto if !is_tty => ColorChoice::Never,
1920 flags::Color::Auto => ColorChoice::Auto,
1921 };
1922 let mut stream = constructor(choice);
1923 let result = f(&mut stream);
1924 stream.reset().unwrap();
1925 result
1926 }
1927
1928 pub fn exec_ctx(&self) -> &ExecutionContext {
1929 &self.config.exec_ctx
1930 }
1931}
1932
1933impl AsRef<ExecutionContext> for Build {
1934 fn as_ref(&self) -> &ExecutionContext {
1935 &self.config.exec_ctx
1936 }
1937}
1938
1939#[cfg(unix)]
1940fn chmod(path: &Path, perms: u32) {
1941 use std::os::unix::fs::*;
1942 t!(fs::set_permissions(path, fs::Permissions::from_mode(perms)));
1943}
1944#[cfg(windows)]
1945fn chmod(_path: &Path, _perms: u32) {}
1946
1947impl Compiler {
1948 pub fn new(stage: u32, host: TargetSelection) -> Self {
1949 Self { stage, host, forced_compiler: false }
1950 }
1951
1952 pub fn forced_compiler(&mut self, forced_compiler: bool) {
1953 self.forced_compiler = forced_compiler;
1954 }
1955
1956 pub fn with_stage(mut self, stage: u32) -> Compiler {
1957 self.stage = stage;
1958 self
1959 }
1960
1961 pub fn is_snapshot(&self, build: &Build) -> bool {
1963 self.stage == 0 && self.host == build.host_target
1964 }
1965
1966 pub fn is_forced_compiler(&self) -> bool {
1968 self.forced_compiler
1969 }
1970}
1971
1972fn envify(s: &str) -> String {
1973 s.chars()
1974 .map(|c| match c {
1975 '-' => '_',
1976 c => c,
1977 })
1978 .flat_map(|c| c.to_uppercase())
1979 .collect()
1980}
1981
1982pub fn prepare_behaviour_dump_dir(build: &Build) {
1984 static INITIALIZED: OnceLock<bool> = OnceLock::new();
1985
1986 let dump_path = build.out.join("bootstrap-shims-dump");
1987
1988 let initialized = INITIALIZED.get().unwrap_or(&false);
1989 if !initialized {
1990 if dump_path.exists() {
1992 t!(fs::remove_dir_all(&dump_path));
1993 }
1994
1995 t!(fs::create_dir_all(&dump_path));
1996
1997 t!(INITIALIZED.set(true));
1998 }
1999}