1use std::cell::Cell;
17use std::collections::{BTreeSet, HashMap, HashSet};
18use std::io::IsTerminal;
19use std::path::{Path, PathBuf, absolute};
20use std::str::FromStr;
21use std::sync::{Arc, Mutex};
22use std::{cmp, env, fs};
23
24use build_helper::ci::CiEnv;
25use build_helper::exit;
26use build_helper::git::{GitConfig, PathFreshness, check_path_modifications};
27use serde::Deserialize;
28#[cfg(feature = "tracing")]
29use tracing::{instrument, span};
30
31use crate::core::build_steps::llvm;
32use crate::core::build_steps::llvm::LLVM_INVALIDATION_PATHS;
33pub use crate::core::config::flags::Subcommand;
34use crate::core::config::flags::{Color, Flags, Warnings};
35use crate::core::config::target_selection::TargetSelectionList;
36use crate::core::config::toml::TomlConfig;
37use crate::core::config::toml::build::{Build, Tool};
38use crate::core::config::toml::change_id::ChangeId;
39use crate::core::config::toml::dist::Dist;
40use crate::core::config::toml::gcc::Gcc;
41use crate::core::config::toml::install::Install;
42use crate::core::config::toml::llvm::Llvm;
43use crate::core::config::toml::rust::{
44 LldMode, Rust, RustOptimize, check_incompatible_options_for_ci_rustc,
45 default_lld_opt_in_targets, parse_codegen_backends,
46};
47use crate::core::config::toml::target::Target;
48use crate::core::config::{
49 CompilerBuiltins, DebuginfoLevel, DryRun, GccCiMode, LlvmLibunwind, Merge, ReplaceOpt,
50 RustcLto, SplitDebuginfo, StringOrBool, threads_from_config,
51};
52use crate::core::download::{
53 DownloadContext, download_beta_toolchain, is_download_ci_available, maybe_download_rustfmt,
54};
55use crate::utils::channel;
56use crate::utils::exec::{ExecutionContext, command};
57use crate::utils::helpers::{exe, get_host_target};
58use crate::{CodegenBackendKind, GitInfo, OnceLock, TargetSelection, check_ci_llvm, helpers, t};
59
60#[rustfmt::skip] pub const RUSTC_IF_UNCHANGED_ALLOWED_PATHS: &[&str] = &[
73 ":!library",
74 ":!src/tools",
75 ":!src/librustdoc",
76 ":!src/rustdoc-json-types",
77 ":!tests",
78 ":!triagebot.toml",
79];
80
81#[derive(Default, Clone)]
90pub struct Config {
91 pub change_id: Option<ChangeId>,
92 pub bypass_bootstrap_lock: bool,
93 pub ccache: Option<String>,
94 pub ninja_in_file: bool,
96 pub submodules: Option<bool>,
97 pub compiler_docs: bool,
98 pub library_docs_private_items: bool,
99 pub docs_minification: bool,
100 pub docs: bool,
101 pub locked_deps: bool,
102 pub vendor: bool,
103 pub target_config: HashMap<TargetSelection, Target>,
104 pub full_bootstrap: bool,
105 pub bootstrap_cache_path: Option<PathBuf>,
106 pub extended: bool,
107 pub tools: Option<HashSet<String>>,
108 pub tool: HashMap<String, Tool>,
111 pub sanitizers: bool,
112 pub profiler: bool,
113 pub omit_git_hash: bool,
114 pub skip: Vec<PathBuf>,
115 pub include_default_paths: bool,
116 pub rustc_error_format: Option<String>,
117 pub json_output: bool,
118 pub compile_time_deps: bool,
119 pub test_compare_mode: bool,
120 pub color: Color,
121 pub patch_binaries_for_nix: Option<bool>,
122 pub stage0_metadata: build_helper::stage0_parser::Stage0,
123 pub android_ndk: Option<PathBuf>,
124 pub optimized_compiler_builtins: CompilerBuiltins,
125
126 pub stdout_is_tty: bool,
127 pub stderr_is_tty: bool,
128
129 pub on_fail: Option<String>,
130 pub explicit_stage_from_cli: bool,
131 pub explicit_stage_from_config: bool,
132 pub stage: u32,
133 pub keep_stage: Vec<u32>,
134 pub keep_stage_std: Vec<u32>,
135 pub src: PathBuf,
136 pub config: Option<PathBuf>,
138 pub jobs: Option<u32>,
139 pub cmd: Subcommand,
140 pub incremental: bool,
141 pub dump_bootstrap_shims: bool,
142 pub free_args: Vec<String>,
145
146 pub download_rustc_commit: Option<String>,
148
149 pub deny_warnings: bool,
150 pub backtrace_on_ice: bool,
151
152 pub llvm_assertions: bool,
154 pub llvm_tests: bool,
155 pub llvm_enzyme: bool,
156 pub llvm_offload: bool,
157 pub llvm_plugins: bool,
158 pub llvm_optimize: bool,
159 pub llvm_thin_lto: bool,
160 pub llvm_release_debuginfo: bool,
161 pub llvm_static_stdcpp: bool,
162 pub llvm_libzstd: bool,
163 pub llvm_link_shared: Cell<Option<bool>>,
164 pub llvm_clang_cl: Option<String>,
165 pub llvm_targets: Option<String>,
166 pub llvm_experimental_targets: Option<String>,
167 pub llvm_link_jobs: Option<u32>,
168 pub llvm_version_suffix: Option<String>,
169 pub llvm_use_linker: Option<String>,
170 pub llvm_allow_old_toolchain: bool,
171 pub llvm_polly: bool,
172 pub llvm_clang: bool,
173 pub llvm_enable_warnings: bool,
174 pub llvm_from_ci: bool,
175 pub llvm_build_config: HashMap<String, String>,
176
177 pub lld_mode: LldMode,
178 pub lld_enabled: bool,
179 pub llvm_tools_enabled: bool,
180 pub llvm_bitcode_linker_enabled: bool,
181
182 pub llvm_cflags: Option<String>,
183 pub llvm_cxxflags: Option<String>,
184 pub llvm_ldflags: Option<String>,
185 pub llvm_use_libcxx: bool,
186
187 pub gcc_ci_mode: GccCiMode,
189
190 pub rust_optimize: RustOptimize,
192 pub rust_codegen_units: Option<u32>,
193 pub rust_codegen_units_std: Option<u32>,
194 pub rustc_debug_assertions: bool,
195 pub std_debug_assertions: bool,
196 pub tools_debug_assertions: bool,
197
198 pub rust_overflow_checks: bool,
199 pub rust_overflow_checks_std: bool,
200 pub rust_debug_logging: bool,
201 pub rust_debuginfo_level_rustc: DebuginfoLevel,
202 pub rust_debuginfo_level_std: DebuginfoLevel,
203 pub rust_debuginfo_level_tools: DebuginfoLevel,
204 pub rust_debuginfo_level_tests: DebuginfoLevel,
205 pub rust_rpath: bool,
206 pub rust_strip: bool,
207 pub rust_frame_pointers: bool,
208 pub rust_stack_protector: Option<String>,
209 pub rustc_default_linker: Option<String>,
210 pub rust_optimize_tests: bool,
211 pub rust_dist_src: bool,
212 pub rust_codegen_backends: Vec<CodegenBackendKind>,
213 pub rust_verify_llvm_ir: bool,
214 pub rust_thin_lto_import_instr_limit: Option<u32>,
215 pub rust_randomize_layout: bool,
216 pub rust_remap_debuginfo: bool,
217 pub rust_new_symbol_mangling: Option<bool>,
218 pub rust_profile_use: Option<String>,
219 pub rust_profile_generate: Option<String>,
220 pub rust_lto: RustcLto,
221 pub rust_validate_mir_opts: Option<u32>,
222 pub rust_std_features: BTreeSet<String>,
223 pub rust_break_on_ice: bool,
224 pub rust_parallel_frontend_threads: Option<u32>,
225
226 pub llvm_profile_use: Option<String>,
227 pub llvm_profile_generate: bool,
228 pub llvm_libunwind_default: Option<LlvmLibunwind>,
229 pub enable_bolt_settings: bool,
230
231 pub reproducible_artifacts: Vec<String>,
232
233 pub host_target: TargetSelection,
234 pub hosts: Vec<TargetSelection>,
235 pub targets: Vec<TargetSelection>,
236 pub local_rebuild: bool,
237 pub jemalloc: bool,
238 pub control_flow_guard: bool,
239 pub ehcont_guard: bool,
240
241 pub dist_sign_folder: Option<PathBuf>,
243 pub dist_upload_addr: Option<String>,
244 pub dist_compression_formats: Option<Vec<String>>,
245 pub dist_compression_profile: String,
246 pub dist_include_mingw_linker: bool,
247 pub dist_vendor: bool,
248
249 pub backtrace: bool, pub low_priority: bool,
254 pub channel: String,
255 pub description: Option<String>,
256 pub verbose_tests: bool,
257 pub save_toolstates: Option<PathBuf>,
258 pub print_step_timings: bool,
259 pub print_step_rusage: bool,
260
261 pub musl_root: Option<PathBuf>,
263 pub prefix: Option<PathBuf>,
264 pub sysconfdir: Option<PathBuf>,
265 pub datadir: Option<PathBuf>,
266 pub docdir: Option<PathBuf>,
267 pub bindir: PathBuf,
268 pub libdir: Option<PathBuf>,
269 pub mandir: Option<PathBuf>,
270 pub codegen_tests: bool,
271 pub nodejs: Option<PathBuf>,
272 pub npm: Option<PathBuf>,
273 pub gdb: Option<PathBuf>,
274 pub lldb: Option<PathBuf>,
275 pub python: Option<PathBuf>,
276 pub windows_rc: Option<PathBuf>,
277 pub reuse: Option<PathBuf>,
278 pub cargo_native_static: bool,
279 pub configure_args: Vec<String>,
280 pub out: PathBuf,
281 pub rust_info: channel::GitInfo,
282
283 pub cargo_info: channel::GitInfo,
284 pub rust_analyzer_info: channel::GitInfo,
285 pub clippy_info: channel::GitInfo,
286 pub miri_info: channel::GitInfo,
287 pub rustfmt_info: channel::GitInfo,
288 pub enzyme_info: channel::GitInfo,
289 pub in_tree_llvm_info: channel::GitInfo,
290 pub in_tree_gcc_info: channel::GitInfo,
291
292 pub initial_cargo: PathBuf,
294 pub initial_rustc: PathBuf,
295 pub initial_cargo_clippy: Option<PathBuf>,
296 pub initial_sysroot: PathBuf,
297 pub initial_rustfmt: Option<PathBuf>,
298
299 pub paths: Vec<PathBuf>,
302
303 pub compiletest_diff_tool: Option<String>,
305
306 pub compiletest_allow_stage0: bool,
312
313 pub compiletest_use_stage0_libtest: bool,
315
316 pub tidy_extra_checks: Option<String>,
318 pub is_running_on_ci: bool,
319
320 pub path_modification_cache: Arc<Mutex<HashMap<Vec<&'static str>, PathFreshness>>>,
322
323 pub skip_std_check_if_no_download_rustc: bool,
327
328 pub exec_ctx: ExecutionContext,
329}
330
331impl Config {
332 pub fn set_dry_run(&mut self, dry_run: DryRun) {
333 self.exec_ctx.set_dry_run(dry_run);
334 }
335
336 pub fn get_dry_run(&self) -> &DryRun {
337 self.exec_ctx.get_dry_run()
338 }
339
340 #[cfg_attr(
341 feature = "tracing",
342 instrument(target = "CONFIG_HANDLING", level = "trace", name = "Config::parse", skip_all)
343 )]
344 pub fn parse(flags: Flags) -> Config {
345 Self::parse_inner(flags, Self::get_toml)
346 }
347
348 #[cfg_attr(
349 feature = "tracing",
350 instrument(
351 target = "CONFIG_HANDLING",
352 level = "trace",
353 name = "Config::parse_inner",
354 skip_all
355 )
356 )]
357 pub(crate) fn parse_inner(
358 flags: Flags,
359 get_toml: impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>,
360 ) -> Config {
361 let Flags {
365 cmd: flags_cmd,
366 verbose: flags_verbose,
367 incremental: flags_incremental,
368 config: flags_config,
369 build_dir: flags_build_dir,
370 build: flags_build,
371 host: flags_host,
372 target: flags_target,
373 exclude: flags_exclude,
374 skip: flags_skip,
375 include_default_paths: flags_include_default_paths,
376 rustc_error_format: flags_rustc_error_format,
377 on_fail: flags_on_fail,
378 dry_run: flags_dry_run,
379 dump_bootstrap_shims: flags_dump_bootstrap_shims,
380 stage: flags_stage,
381 keep_stage: flags_keep_stage,
382 keep_stage_std: flags_keep_stage_std,
383 src: flags_src,
384 jobs: flags_jobs,
385 warnings: flags_warnings,
386 json_output: flags_json_output,
387 compile_time_deps: flags_compile_time_deps,
388 color: flags_color,
389 bypass_bootstrap_lock: flags_bypass_bootstrap_lock,
390 rust_profile_generate: flags_rust_profile_generate,
391 rust_profile_use: flags_rust_profile_use,
392 llvm_profile_use: flags_llvm_profile_use,
393 llvm_profile_generate: flags_llvm_profile_generate,
394 enable_bolt_settings: flags_enable_bolt_settings,
395 skip_stage0_validation: flags_skip_stage0_validation,
396 reproducible_artifact: flags_reproducible_artifact,
397 paths: flags_paths,
398 set: flags_set,
399 free_args: flags_free_args,
400 ci: flags_ci,
401 skip_std_check_if_no_download_rustc: flags_skip_std_check_if_no_download_rustc,
402 } = flags;
403
404 #[cfg(feature = "tracing")]
405 span!(
406 target: "CONFIG_HANDLING",
407 tracing::Level::TRACE,
408 "collecting paths and path exclusions",
409 "flags.paths" = ?flags_paths,
410 "flags.skip" = ?flags_skip,
411 "flags.exclude" = ?flags_exclude
412 );
413
414 let mut exec_ctx = ExecutionContext::new(flags_verbose, flags_cmd.fail_fast());
416 exec_ctx.set_dry_run(if flags_dry_run { DryRun::UserSelected } else { DryRun::Disabled });
417 let mut src = {
418 let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
419 manifest_dir.parent().unwrap().parent().unwrap().to_owned()
421 };
422
423 if let Some(src_) = compute_src_directory(flags_src, &exec_ctx) {
424 src = src_;
425 }
426
427 let (mut toml, toml_path) = load_toml_config(&src, flags_config, &get_toml);
429
430 postprocess_toml(&mut toml, &src, toml_path.clone(), &exec_ctx, &flags_set, &get_toml);
431
432 let Build {
435 description: build_description,
436 build: build_build,
437 host: build_host,
438 target: build_target,
439 build_dir: build_build_dir,
440 cargo: mut build_cargo,
441 rustc: mut build_rustc,
442 rustfmt: build_rustfmt,
443 cargo_clippy: build_cargo_clippy,
444 docs: build_docs,
445 compiler_docs: build_compiler_docs,
446 library_docs_private_items: build_library_docs_private_items,
447 docs_minification: build_docs_minification,
448 submodules: build_submodules,
449 gdb: build_gdb,
450 lldb: build_lldb,
451 nodejs: build_nodejs,
452 npm: build_npm,
453 python: build_python,
454 windows_rc: build_windows_rc,
455 reuse: build_reuse,
456 locked_deps: build_locked_deps,
457 vendor: build_vendor,
458 full_bootstrap: build_full_bootstrap,
459 bootstrap_cache_path: build_bootstrap_cache_path,
460 extended: build_extended,
461 tools: build_tools,
462 tool: build_tool,
463 verbose: build_verbose,
464 sanitizers: build_sanitizers,
465 profiler: build_profiler,
466 cargo_native_static: build_cargo_native_static,
467 low_priority: build_low_priority,
468 configure_args: build_configure_args,
469 local_rebuild: build_local_rebuild,
470 print_step_timings: build_print_step_timings,
471 print_step_rusage: build_print_step_rusage,
472 check_stage: build_check_stage,
473 doc_stage: build_doc_stage,
474 build_stage: build_build_stage,
475 test_stage: build_test_stage,
476 install_stage: build_install_stage,
477 dist_stage: build_dist_stage,
478 bench_stage: build_bench_stage,
479 patch_binaries_for_nix: build_patch_binaries_for_nix,
480 metrics: _,
482 android_ndk: build_android_ndk,
483 optimized_compiler_builtins: build_optimized_compiler_builtins,
484 jobs: build_jobs,
485 compiletest_diff_tool: build_compiletest_diff_tool,
486 compiletest_use_stage0_libtest: build_compiletest_use_stage0_libtest,
487 tidy_extra_checks: build_tidy_extra_checks,
488 ccache: build_ccache,
489 exclude: build_exclude,
490 compiletest_allow_stage0: build_compiletest_allow_stage0,
491 } = toml.build.unwrap_or_default();
492
493 let Install {
494 prefix: install_prefix,
495 sysconfdir: install_sysconfdir,
496 docdir: install_docdir,
497 bindir: install_bindir,
498 libdir: install_libdir,
499 mandir: install_mandir,
500 datadir: install_datadir,
501 } = toml.install.unwrap_or_default();
502
503 let Rust {
504 optimize: rust_optimize,
505 debug: rust_debug,
506 codegen_units: rust_codegen_units,
507 codegen_units_std: rust_codegen_units_std,
508 rustc_debug_assertions: rust_rustc_debug_assertions,
509 std_debug_assertions: rust_std_debug_assertions,
510 tools_debug_assertions: rust_tools_debug_assertions,
511 overflow_checks: rust_overflow_checks,
512 overflow_checks_std: rust_overflow_checks_std,
513 debug_logging: rust_debug_logging,
514 debuginfo_level: rust_debuginfo_level,
515 debuginfo_level_rustc: rust_debuginfo_level_rustc,
516 debuginfo_level_std: rust_debuginfo_level_std,
517 debuginfo_level_tools: rust_debuginfo_level_tools,
518 debuginfo_level_tests: rust_debuginfo_level_tests,
519 backtrace: rust_backtrace,
520 incremental: rust_incremental,
521 randomize_layout: rust_randomize_layout,
522 default_linker: rust_default_linker,
523 channel: rust_channel,
524 musl_root: rust_musl_root,
525 rpath: rust_rpath,
526 verbose_tests: rust_verbose_tests,
527 optimize_tests: rust_optimize_tests,
528 codegen_tests: rust_codegen_tests,
529 omit_git_hash: rust_omit_git_hash,
530 dist_src: rust_dist_src,
531 save_toolstates: rust_save_toolstates,
532 codegen_backends: rust_codegen_backends,
533 lld: rust_lld_enabled,
534 llvm_tools: rust_llvm_tools,
535 llvm_bitcode_linker: rust_llvm_bitcode_linker,
536 deny_warnings: rust_deny_warnings,
537 backtrace_on_ice: rust_backtrace_on_ice,
538 verify_llvm_ir: rust_verify_llvm_ir,
539 thin_lto_import_instr_limit: rust_thin_lto_import_instr_limit,
540 parallel_frontend_threads: rust_parallel_frontend_threads,
541 remap_debuginfo: rust_remap_debuginfo,
542 jemalloc: rust_jemalloc,
543 test_compare_mode: rust_test_compare_mode,
544 llvm_libunwind: rust_llvm_libunwind,
545 control_flow_guard: rust_control_flow_guard,
546 ehcont_guard: rust_ehcont_guard,
547 new_symbol_mangling: rust_new_symbol_mangling,
548 profile_generate: rust_profile_generate,
549 profile_use: rust_profile_use,
550 download_rustc: rust_download_rustc,
551 lto: rust_lto,
552 validate_mir_opts: rust_validate_mir_opts,
553 frame_pointers: rust_frame_pointers,
554 stack_protector: rust_stack_protector,
555 strip: rust_strip,
556 lld_mode: rust_lld_mode,
557 std_features: rust_std_features,
558 break_on_ice: rust_break_on_ice,
559 } = toml.rust.unwrap_or_default();
560
561 let Llvm {
562 optimize: llvm_optimize,
563 thin_lto: llvm_thin_lto,
564 release_debuginfo: llvm_release_debuginfo,
565 assertions: llvm_assertions,
566 tests: llvm_tests,
567 enzyme: llvm_enzyme,
568 plugins: llvm_plugin,
569 static_libstdcpp: llvm_static_libstdcpp,
570 libzstd: llvm_libzstd,
571 ninja: llvm_ninja,
572 targets: llvm_targets,
573 experimental_targets: llvm_experimental_targets,
574 link_jobs: llvm_link_jobs,
575 link_shared: llvm_link_shared,
576 version_suffix: llvm_version_suffix,
577 clang_cl: llvm_clang_cl,
578 cflags: llvm_cflags,
579 cxxflags: llvm_cxxflags,
580 ldflags: llvm_ldflags,
581 use_libcxx: llvm_use_libcxx,
582 use_linker: llvm_use_linker,
583 allow_old_toolchain: llvm_allow_old_toolchain,
584 offload: llvm_offload,
585 polly: llvm_polly,
586 clang: llvm_clang,
587 enable_warnings: llvm_enable_warnings,
588 download_ci_llvm: llvm_download_ci_llvm,
589 build_config: llvm_build_config,
590 } = toml.llvm.unwrap_or_default();
591
592 let Dist {
593 sign_folder: dist_sign_folder,
594 upload_addr: dist_upload_addr,
595 src_tarball: dist_src_tarball,
596 compression_formats: dist_compression_formats,
597 compression_profile: dist_compression_profile,
598 include_mingw_linker: dist_include_mingw_linker,
599 vendor: dist_vendor,
600 } = toml.dist.unwrap_or_default();
601
602 let Gcc { download_ci_gcc: gcc_download_ci_gcc } = toml.gcc.unwrap_or_default();
603
604 if rust_optimize.as_ref().is_some_and(|v| matches!(v, RustOptimize::Bool(false))) {
605 eprintln!(
606 "WARNING: setting `optimize` to `false` is known to cause errors and \
607 should be considered unsupported. Refer to `bootstrap.example.toml` \
608 for more details."
609 );
610 }
611
612 exec_ctx.set_verbosity(cmp::max(build_verbose.unwrap_or_default() as u8, flags_verbose));
615
616 let stage0_metadata = build_helper::stage0_parser::parse_stage0_file();
617 let path_modification_cache = Arc::new(Mutex::new(HashMap::new()));
618
619 let host_target = flags_build
620 .or(build_build)
621 .map(|build| TargetSelection::from_user(&build))
622 .unwrap_or_else(get_host_target);
623 let hosts = flags_host
624 .map(|TargetSelectionList(hosts)| hosts)
625 .or_else(|| {
626 build_host.map(|h| h.iter().map(|t| TargetSelection::from_user(t)).collect())
627 })
628 .unwrap_or_else(|| vec![host_target]);
629
630 let llvm_assertions = llvm_assertions.unwrap_or(false);
631 let mut target_config = HashMap::new();
632 let mut channel = "dev".to_string();
633 let out = flags_build_dir.or(build_build_dir.map(PathBuf::from)).unwrap_or_else(|| {
634 if cfg!(test) {
635 Path::new(
637 &env::var_os("CARGO_TARGET_DIR").expect("cargo test directly is not supported"),
638 )
639 .parent()
640 .unwrap()
641 .to_path_buf()
642 } else {
643 PathBuf::from("build")
644 }
645 });
646
647 let mut out = if !out.is_absolute() {
650 absolute(&out).expect("can't make empty path absolute")
652 } else {
653 out
654 };
655
656 if cfg!(test) {
657 build_rustc = build_rustc.take().or(std::env::var_os("RUSTC").map(|p| p.into()));
663 build_cargo = build_cargo.take().or(std::env::var_os("CARGO").map(|p| p.into()));
664 }
665
666 if !flags_skip_stage0_validation {
667 if let Some(rustc) = &build_rustc {
668 check_stage0_version(rustc, "rustc", &src, &exec_ctx);
669 }
670 if let Some(cargo) = &build_cargo {
671 check_stage0_version(cargo, "cargo", &src, &exec_ctx);
672 }
673 }
674
675 if build_cargo_clippy.is_some() && build_rustc.is_none() {
676 println!(
677 "WARNING: Using `build.cargo-clippy` without `build.rustc` usually fails due to toolchain conflict."
678 );
679 }
680
681 let is_running_on_ci = flags_ci.unwrap_or(CiEnv::is_ci());
682 let dwn_ctx = DownloadContext {
683 path_modification_cache: path_modification_cache.clone(),
684 src: &src,
685 submodules: &build_submodules,
686 host_target,
687 patch_binaries_for_nix: build_patch_binaries_for_nix,
688 exec_ctx: &exec_ctx,
689 stage0_metadata: &stage0_metadata,
690 llvm_assertions,
691 bootstrap_cache_path: &build_bootstrap_cache_path,
692 is_running_on_ci,
693 };
694
695 let initial_rustc = build_rustc.unwrap_or_else(|| {
696 download_beta_toolchain(&dwn_ctx, &out);
697 out.join(host_target).join("stage0").join("bin").join(exe("rustc", host_target))
698 });
699
700 let initial_sysroot = t!(PathBuf::from_str(
701 command(&initial_rustc)
702 .args(["--print", "sysroot"])
703 .run_in_dry_run()
704 .run_capture_stdout(&exec_ctx)
705 .stdout()
706 .trim()
707 ));
708
709 let initial_cargo = build_cargo.unwrap_or_else(|| {
710 download_beta_toolchain(&dwn_ctx, &out);
711 initial_sysroot.join("bin").join(exe("cargo", host_target))
712 });
713
714 if exec_ctx.dry_run() {
716 out = out.join("tmp-dry-run");
717 fs::create_dir_all(&out).expect("Failed to create dry-run directory");
718 }
719
720 let file_content = t!(fs::read_to_string(src.join("src/ci/channel")));
721 let ci_channel = file_content.trim_end();
722
723 let is_user_configured_rust_channel = match rust_channel {
724 Some(channel_) if channel_ == "auto-detect" => {
725 channel = ci_channel.into();
726 true
727 }
728 Some(channel_) => {
729 channel = channel_;
730 true
731 }
732 None => false,
733 };
734
735 let omit_git_hash = rust_omit_git_hash.unwrap_or(channel == "dev");
736
737 let rust_info = git_info(&exec_ctx, omit_git_hash, &src);
738
739 if !is_user_configured_rust_channel && rust_info.is_from_tarball() {
740 channel = ci_channel.into();
741 }
742
743 let debug_assertions_requested = matches!(rust_rustc_debug_assertions, Some(true))
754 || (matches!(rust_debug, Some(true))
755 && !matches!(rust_rustc_debug_assertions, Some(false)));
756
757 if debug_assertions_requested
758 && let Some(ref opt) = rust_download_rustc
759 && opt.is_string_or_true()
760 {
761 eprintln!(
762 "WARN: currently no CI rustc builds have rustc debug assertions \
763 enabled. Please either set `rust.debug-assertions` to `false` if you \
764 want to use download CI rustc or set `rust.download-rustc` to `false`."
765 );
766 }
767
768 let mut download_rustc_commit =
769 download_ci_rustc_commit(&dwn_ctx, &rust_info, rust_download_rustc, llvm_assertions);
770
771 if debug_assertions_requested && download_rustc_commit.is_some() {
772 eprintln!(
773 "WARN: `rust.debug-assertions = true` will prevent downloading CI rustc as alt CI \
774 rustc is not currently built with debug assertions."
775 );
776 download_rustc_commit = None;
778 }
779
780 if let Some(commit) = &download_rustc_commit
784 && is_user_configured_rust_channel
785 {
786 println!(
787 "WARNING: `rust.download-rustc` is enabled. The `rust.channel` option will be overridden by the CI rustc's channel."
788 );
789
790 channel =
791 read_file_by_commit(&dwn_ctx, &rust_info, Path::new("src/ci/channel"), commit)
792 .trim()
793 .to_owned();
794 }
795
796 if let Some(t) = toml.target {
797 for (triple, cfg) in t {
798 let mut target = Target::from_triple(&triple);
799
800 if let Some(ref s) = cfg.llvm_config {
801 if download_rustc_commit.is_some() && triple == *host_target.triple {
802 panic!(
803 "setting llvm_config for the host is incompatible with download-rustc"
804 );
805 }
806 target.llvm_config = Some(src.join(s));
807 }
808 if let Some(patches) = cfg.llvm_has_rust_patches {
809 assert!(
810 build_submodules == Some(false) || cfg.llvm_config.is_some(),
811 "use of `llvm-has-rust-patches` is restricted to cases where either submodules are disabled or llvm-config been provided"
812 );
813 target.llvm_has_rust_patches = Some(patches);
814 }
815 if let Some(ref s) = cfg.llvm_filecheck {
816 target.llvm_filecheck = Some(src.join(s));
817 }
818 target.llvm_libunwind = cfg.llvm_libunwind.as_ref().map(|v| {
819 v.parse().unwrap_or_else(|_| {
820 panic!("failed to parse target.{triple}.llvm-libunwind")
821 })
822 });
823 if let Some(s) = cfg.no_std {
824 target.no_std = s;
825 }
826 target.cc = cfg.cc.map(PathBuf::from);
827 target.cxx = cfg.cxx.map(PathBuf::from);
828 target.ar = cfg.ar.map(PathBuf::from);
829 target.ranlib = cfg.ranlib.map(PathBuf::from);
830 target.linker = cfg.linker.map(PathBuf::from);
831 target.crt_static = cfg.crt_static;
832 target.musl_root = cfg.musl_root.map(PathBuf::from);
833 target.musl_libdir = cfg.musl_libdir.map(PathBuf::from);
834 target.wasi_root = cfg.wasi_root.map(PathBuf::from);
835 target.qemu_rootfs = cfg.qemu_rootfs.map(PathBuf::from);
836 target.runner = cfg.runner;
837 target.sanitizers = cfg.sanitizers;
838 target.profiler = cfg.profiler;
839 target.rpath = cfg.rpath;
840 target.optimized_compiler_builtins = cfg.optimized_compiler_builtins;
841 target.jemalloc = cfg.jemalloc;
842 if let Some(backends) = cfg.codegen_backends {
843 target.codegen_backends =
844 Some(parse_codegen_backends(backends, &format!("target.{triple}")))
845 }
846
847 target.split_debuginfo = cfg.split_debuginfo.as_ref().map(|v| {
848 v.parse().unwrap_or_else(|_| {
849 panic!("invalid value for target.{triple}.split-debuginfo")
850 })
851 });
852
853 target_config.insert(TargetSelection::from_user(&triple), target);
854 }
855 }
856
857 let llvm_from_ci = parse_download_ci_llvm(
858 &dwn_ctx,
859 &rust_info,
860 &download_rustc_commit,
861 llvm_download_ci_llvm,
862 llvm_assertions,
863 );
864
865 let lld_enabled = if default_lld_opt_in_targets().contains(&host_target.triple.to_string())
878 && hosts == [host_target]
879 {
880 let no_llvm_config =
881 target_config.get(&host_target).is_none_or(|config| config.llvm_config.is_none());
882 rust_lld_enabled.unwrap_or(llvm_from_ci || no_llvm_config)
883 } else {
884 rust_lld_enabled.unwrap_or(false)
885 };
886
887 if llvm_from_ci {
888 let warn = |option: &str| {
889 println!(
890 "WARNING: `{option}` will only be used on `compiler/rustc_llvm` build, not for the LLVM build."
891 );
892 println!(
893 "HELP: To use `{option}` for LLVM builds, set `download-ci-llvm` option to false."
894 );
895 };
896
897 if llvm_static_libstdcpp.is_some() {
898 warn("static-libstdcpp");
899 }
900
901 if llvm_link_shared.is_some() {
902 warn("link-shared");
903 }
904
905 if llvm_libzstd.is_some() {
911 println!(
912 "WARNING: when using `download-ci-llvm`, the local `llvm.libzstd` option, \
913 like almost all `llvm.*` options, will be ignored and set by the LLVM CI \
914 artifacts builder config."
915 );
916 println!(
917 "HELP: To use `llvm.libzstd` for LLVM/LLD builds, set `download-ci-llvm` option to false."
918 );
919 }
920 }
921
922 if llvm_from_ci {
923 let triple = &host_target.triple;
924 let ci_llvm_bin = ci_llvm_root(&dwn_ctx, llvm_from_ci, &out).join("bin");
925 let build_target =
926 target_config.entry(host_target).or_insert_with(|| Target::from_triple(triple));
927 check_ci_llvm!(build_target.llvm_config);
928 check_ci_llvm!(build_target.llvm_filecheck);
929 build_target.llvm_config = Some(ci_llvm_bin.join(exe("llvm-config", host_target)));
930 build_target.llvm_filecheck = Some(ci_llvm_bin.join(exe("FileCheck", host_target)));
931 }
932
933 let initial_rustfmt = build_rustfmt.or_else(|| maybe_download_rustfmt(&dwn_ctx, &out));
934
935 if matches!(rust_lld_mode.unwrap_or_default(), LldMode::SelfContained)
936 && !lld_enabled
937 && flags_stage.unwrap_or(0) > 0
938 {
939 panic!(
940 "Trying to use self-contained lld as a linker, but LLD is not being added to the sysroot. Enable it with rust.lld = true."
941 );
942 }
943
944 if lld_enabled && is_system_llvm(&dwn_ctx, &target_config, llvm_from_ci, host_target) {
945 panic!("Cannot enable LLD with `rust.lld = true` when using external llvm-config.");
946 }
947
948 let download_rustc = download_rustc_commit.is_some();
949
950 let stage = match flags_cmd {
951 Subcommand::Check { .. } => flags_stage.or(build_check_stage).unwrap_or(1),
952 Subcommand::Clippy { .. } | Subcommand::Fix => {
953 flags_stage.or(build_check_stage).unwrap_or(1)
954 }
955 Subcommand::Doc { .. } => {
957 flags_stage.or(build_doc_stage).unwrap_or(if download_rustc { 2 } else { 1 })
958 }
959 Subcommand::Build { .. } => {
960 flags_stage.or(build_build_stage).unwrap_or(if download_rustc { 2 } else { 1 })
961 }
962 Subcommand::Test { .. } | Subcommand::Miri { .. } => {
963 flags_stage.or(build_test_stage).unwrap_or(if download_rustc { 2 } else { 1 })
964 }
965 Subcommand::Bench { .. } => flags_stage.or(build_bench_stage).unwrap_or(2),
966 Subcommand::Dist => flags_stage.or(build_dist_stage).unwrap_or(2),
967 Subcommand::Install => flags_stage.or(build_install_stage).unwrap_or(2),
968 Subcommand::Perf { .. } => flags_stage.unwrap_or(1),
969 Subcommand::Clean { .. }
972 | Subcommand::Run { .. }
973 | Subcommand::Setup { .. }
974 | Subcommand::Format { .. }
975 | Subcommand::Vendor { .. } => flags_stage.unwrap_or(0),
976 };
977
978 let local_rebuild = build_local_rebuild.unwrap_or(false);
979
980 let check_stage0 = |kind: &str| {
981 if local_rebuild {
982 eprintln!("WARNING: running {kind} in stage 0. This might not work as expected.");
983 } else {
984 eprintln!(
985 "ERROR: cannot {kind} anything on stage 0. Use at least stage 1 or set build.local-rebuild=true and use a stage0 compiler built from in-tree sources."
986 );
987 exit!(1);
988 }
989 };
990
991 match (stage, &flags_cmd) {
993 (0, Subcommand::Build { .. }) => {
994 check_stage0("build");
995 }
996 (0, Subcommand::Check { .. }) => {
997 check_stage0("check");
998 }
999 (0, Subcommand::Doc { .. }) => {
1000 check_stage0("doc");
1001 }
1002 (0, Subcommand::Clippy { .. }) => {
1003 check_stage0("clippy");
1004 }
1005 (0, Subcommand::Dist) => {
1006 check_stage0("dist");
1007 }
1008 (0, Subcommand::Install) => {
1009 check_stage0("install");
1010 }
1011 (0, Subcommand::Test { .. }) if build_compiletest_allow_stage0 != Some(true) => {
1012 eprintln!(
1013 "ERROR: cannot test anything on stage 0. Use at least stage 1. If you want to run compiletest with an external stage0 toolchain, enable `build.compiletest-allow-stage0`."
1014 );
1015 exit!(1);
1016 }
1017 _ => {}
1018 }
1019
1020 if flags_compile_time_deps && !matches!(flags_cmd, Subcommand::Check { .. }) {
1021 eprintln!("ERROR: Can't use --compile-time-deps with any subcommand other than check.");
1022 exit!(1);
1023 }
1024
1025 #[cfg(not(test))]
1027 if flags_stage.is_none() && is_running_on_ci {
1028 match flags_cmd {
1029 Subcommand::Test { .. }
1030 | Subcommand::Miri { .. }
1031 | Subcommand::Doc { .. }
1032 | Subcommand::Build { .. }
1033 | Subcommand::Bench { .. }
1034 | Subcommand::Dist
1035 | Subcommand::Install => {
1036 assert_eq!(
1037 stage, 2,
1038 "x.py should be run with `--stage 2` on CI, but was run with `--stage {stage}`",
1039 );
1040 }
1041 Subcommand::Clean { .. }
1042 | Subcommand::Check { .. }
1043 | Subcommand::Clippy { .. }
1044 | Subcommand::Fix
1045 | Subcommand::Run { .. }
1046 | Subcommand::Setup { .. }
1047 | Subcommand::Format { .. }
1048 | Subcommand::Vendor { .. }
1049 | Subcommand::Perf { .. } => {}
1050 }
1051 }
1052
1053 let with_defaults = |debuginfo_level_specific: Option<_>| {
1054 debuginfo_level_specific.or(rust_debuginfo_level).unwrap_or(
1055 if rust_debug == Some(true) {
1056 DebuginfoLevel::Limited
1057 } else {
1058 DebuginfoLevel::None
1059 },
1060 )
1061 };
1062
1063 let ccache = match build_ccache {
1064 Some(StringOrBool::String(s)) => Some(s),
1065 Some(StringOrBool::Bool(true)) => Some("ccache".to_string()),
1066 _ => None,
1067 };
1068
1069 let explicit_stage_from_config = build_test_stage.is_some()
1070 || build_build_stage.is_some()
1071 || build_doc_stage.is_some()
1072 || build_dist_stage.is_some()
1073 || build_install_stage.is_some()
1074 || build_check_stage.is_some()
1075 || build_bench_stage.is_some();
1076
1077 let deny_warnings = match flags_warnings {
1078 Warnings::Deny => true,
1079 Warnings::Warn => false,
1080 Warnings::Default => rust_deny_warnings.unwrap_or(true),
1081 };
1082
1083 let gcc_ci_mode = match gcc_download_ci_gcc {
1084 Some(value) => match value {
1085 true => GccCiMode::DownloadFromCi,
1086 false => GccCiMode::BuildLocally,
1087 },
1088 None => GccCiMode::default(),
1089 };
1090
1091 let targets = flags_target
1092 .map(|TargetSelectionList(targets)| targets)
1093 .or_else(|| {
1094 build_target.map(|t| t.iter().map(|t| TargetSelection::from_user(t)).collect())
1095 })
1096 .unwrap_or_else(|| hosts.clone());
1097
1098 #[allow(clippy::map_identity)]
1099 let skip = flags_skip
1100 .into_iter()
1101 .chain(flags_exclude)
1102 .chain(build_exclude.unwrap_or_default())
1103 .map(|p| {
1104 #[cfg(windows)]
1107 {
1108 PathBuf::from(p.to_string_lossy().replace('/', "\\"))
1109 }
1110 #[cfg(not(windows))]
1111 {
1112 p
1113 }
1114 })
1115 .collect();
1116
1117 let cargo_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/cargo"));
1118 let clippy_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/clippy"));
1119 let in_tree_gcc_info = git_info(&exec_ctx, false, &src.join("src/gcc"));
1120 let in_tree_llvm_info = git_info(&exec_ctx, false, &src.join("src/llvm-project"));
1121 let enzyme_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/enzyme"));
1122 let miri_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/miri"));
1123 let rust_analyzer_info =
1124 git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/rust-analyzer"));
1125 let rustfmt_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/rustfmt"));
1126
1127 let optimized_compiler_builtins =
1128 build_optimized_compiler_builtins.unwrap_or(if channel == "dev" {
1129 CompilerBuiltins::BuildRustOnly
1130 } else {
1131 CompilerBuiltins::BuildLLVMFuncs
1132 });
1133 let vendor = build_vendor.unwrap_or(
1134 rust_info.is_from_tarball()
1135 && src.join("vendor").exists()
1136 && src.join(".cargo/config.toml").exists(),
1137 );
1138 let verbose_tests = rust_verbose_tests.unwrap_or(exec_ctx.is_verbose());
1139
1140 Config {
1141 android_ndk: build_android_ndk,
1143 backtrace: rust_backtrace.unwrap_or(true),
1144 backtrace_on_ice: rust_backtrace_on_ice.unwrap_or(false),
1145 bindir: install_bindir.map(PathBuf::from).unwrap_or("bin".into()),
1146 bootstrap_cache_path: build_bootstrap_cache_path,
1147 bypass_bootstrap_lock: flags_bypass_bootstrap_lock,
1148 cargo_info,
1149 cargo_native_static: build_cargo_native_static.unwrap_or(false),
1150 ccache,
1151 change_id: toml.change_id.inner,
1152 channel,
1153 clippy_info,
1154 cmd: flags_cmd,
1155 codegen_tests: rust_codegen_tests.unwrap_or(true),
1156 color: flags_color,
1157 compile_time_deps: flags_compile_time_deps,
1158 compiler_docs: build_compiler_docs.unwrap_or(false),
1159 compiletest_allow_stage0: build_compiletest_allow_stage0.unwrap_or(false),
1160 compiletest_diff_tool: build_compiletest_diff_tool,
1161 compiletest_use_stage0_libtest: build_compiletest_use_stage0_libtest.unwrap_or(true),
1162 config: toml_path,
1163 configure_args: build_configure_args.unwrap_or_default(),
1164 control_flow_guard: rust_control_flow_guard.unwrap_or(false),
1165 datadir: install_datadir.map(PathBuf::from),
1166 deny_warnings,
1167 description: build_description,
1168 dist_compression_formats,
1169 dist_compression_profile: dist_compression_profile.unwrap_or("fast".into()),
1170 dist_include_mingw_linker: dist_include_mingw_linker.unwrap_or(true),
1171 dist_sign_folder: dist_sign_folder.map(PathBuf::from),
1172 dist_upload_addr,
1173 dist_vendor: dist_vendor.unwrap_or_else(|| {
1174 rust_info.is_managed_git_subrepository() || rust_info.is_from_tarball()
1176 }),
1177 docdir: install_docdir.map(PathBuf::from),
1178 docs: build_docs.unwrap_or(true),
1179 docs_minification: build_docs_minification.unwrap_or(true),
1180 download_rustc_commit,
1181 dump_bootstrap_shims: flags_dump_bootstrap_shims,
1182 ehcont_guard: rust_ehcont_guard.unwrap_or(false),
1183 enable_bolt_settings: flags_enable_bolt_settings,
1184 enzyme_info,
1185 exec_ctx,
1186 explicit_stage_from_cli: flags_stage.is_some(),
1187 explicit_stage_from_config,
1188 extended: build_extended.unwrap_or(false),
1189 free_args: flags_free_args,
1190 full_bootstrap: build_full_bootstrap.unwrap_or(false),
1191 gcc_ci_mode,
1192 gdb: build_gdb.map(PathBuf::from),
1193 host_target,
1194 hosts,
1195 in_tree_gcc_info,
1196 in_tree_llvm_info,
1197 include_default_paths: flags_include_default_paths,
1198 incremental: flags_incremental || rust_incremental == Some(true),
1199 initial_cargo,
1200 initial_cargo_clippy: build_cargo_clippy,
1201 initial_rustc,
1202 initial_rustfmt,
1203 initial_sysroot,
1204 is_running_on_ci,
1205 jemalloc: rust_jemalloc.unwrap_or(false),
1206 jobs: Some(threads_from_config(flags_jobs.or(build_jobs).unwrap_or(0))),
1207 json_output: flags_json_output,
1208 keep_stage: flags_keep_stage,
1209 keep_stage_std: flags_keep_stage_std,
1210 libdir: install_libdir.map(PathBuf::from),
1211 library_docs_private_items: build_library_docs_private_items.unwrap_or(false),
1212 lld_enabled,
1213 lld_mode: rust_lld_mode.unwrap_or_default(),
1214 lldb: build_lldb.map(PathBuf::from),
1215 llvm_allow_old_toolchain: llvm_allow_old_toolchain.unwrap_or(false),
1216 llvm_assertions,
1217 llvm_bitcode_linker_enabled: rust_llvm_bitcode_linker.unwrap_or(false),
1218 llvm_build_config: llvm_build_config.clone().unwrap_or(Default::default()),
1219 llvm_cflags,
1220 llvm_clang: llvm_clang.unwrap_or(false),
1221 llvm_clang_cl,
1222 llvm_cxxflags,
1223 llvm_enable_warnings: llvm_enable_warnings.unwrap_or(false),
1224 llvm_enzyme: llvm_enzyme.unwrap_or(false),
1225 llvm_experimental_targets,
1226 llvm_from_ci,
1227 llvm_ldflags,
1228 llvm_libunwind_default: rust_llvm_libunwind
1229 .map(|v| v.parse().expect("failed to parse rust.llvm-libunwind")),
1230 llvm_libzstd: llvm_libzstd.unwrap_or(false),
1231 llvm_link_jobs,
1232 llvm_link_shared: Cell::new(
1236 llvm_link_shared
1237 .or((!llvm_from_ci && llvm_thin_lto.unwrap_or(false)).then_some(true)),
1238 ),
1239 llvm_offload: llvm_offload.unwrap_or(false),
1240 llvm_optimize: llvm_optimize.unwrap_or(true),
1241 llvm_plugins: llvm_plugin.unwrap_or(false),
1242 llvm_polly: llvm_polly.unwrap_or(false),
1243 llvm_profile_generate: flags_llvm_profile_generate,
1244 llvm_profile_use: flags_llvm_profile_use,
1245 llvm_release_debuginfo: llvm_release_debuginfo.unwrap_or(false),
1246 llvm_static_stdcpp: llvm_static_libstdcpp.unwrap_or(false),
1247 llvm_targets,
1248 llvm_tests: llvm_tests.unwrap_or(false),
1249 llvm_thin_lto: llvm_thin_lto.unwrap_or(false),
1250 llvm_tools_enabled: rust_llvm_tools.unwrap_or(true),
1251 llvm_use_libcxx: llvm_use_libcxx.unwrap_or(false),
1252 llvm_use_linker,
1253 llvm_version_suffix,
1254 local_rebuild,
1255 locked_deps: build_locked_deps.unwrap_or(false),
1256 low_priority: build_low_priority.unwrap_or(false),
1257 mandir: install_mandir.map(PathBuf::from),
1258 miri_info,
1259 musl_root: rust_musl_root.map(PathBuf::from),
1260 ninja_in_file: llvm_ninja.unwrap_or(true),
1261 nodejs: build_nodejs.map(PathBuf::from),
1262 npm: build_npm.map(PathBuf::from),
1263 omit_git_hash,
1264 on_fail: flags_on_fail,
1265 optimized_compiler_builtins,
1266 out,
1267 patch_binaries_for_nix: build_patch_binaries_for_nix,
1268 path_modification_cache,
1269 paths: flags_paths,
1270 prefix: install_prefix.map(PathBuf::from),
1271 print_step_rusage: build_print_step_rusage.unwrap_or(false),
1272 print_step_timings: build_print_step_timings.unwrap_or(false),
1273 profiler: build_profiler.unwrap_or(false),
1274 python: build_python.map(PathBuf::from),
1275 reproducible_artifacts: flags_reproducible_artifact,
1276 reuse: build_reuse.map(PathBuf::from),
1277 rust_analyzer_info,
1278 rust_break_on_ice: rust_break_on_ice.unwrap_or(true),
1279 rust_codegen_backends: rust_codegen_backends
1280 .map(|backends| parse_codegen_backends(backends, "rust"))
1281 .unwrap_or(vec![CodegenBackendKind::Llvm]),
1282 rust_codegen_units: rust_codegen_units.map(threads_from_config),
1283 rust_codegen_units_std: rust_codegen_units_std.map(threads_from_config),
1284 rust_debug_logging: rust_debug_logging
1285 .or(rust_rustc_debug_assertions)
1286 .unwrap_or(rust_debug == Some(true)),
1287 rust_debuginfo_level_rustc: with_defaults(rust_debuginfo_level_rustc),
1288 rust_debuginfo_level_std: with_defaults(rust_debuginfo_level_std),
1289 rust_debuginfo_level_tests: rust_debuginfo_level_tests.unwrap_or(DebuginfoLevel::None),
1290 rust_debuginfo_level_tools: with_defaults(rust_debuginfo_level_tools),
1291 rust_dist_src: dist_src_tarball.unwrap_or_else(|| rust_dist_src.unwrap_or(true)),
1292 rust_frame_pointers: rust_frame_pointers.unwrap_or(false),
1293 rust_info,
1294 rust_lto: rust_lto
1295 .as_deref()
1296 .map(|value| RustcLto::from_str(value).unwrap())
1297 .unwrap_or_default(),
1298 rust_new_symbol_mangling,
1299 rust_optimize: rust_optimize.unwrap_or(RustOptimize::Bool(true)),
1300 rust_optimize_tests: rust_optimize_tests.unwrap_or(true),
1301 rust_overflow_checks: rust_overflow_checks.unwrap_or(rust_debug == Some(true)),
1302 rust_overflow_checks_std: rust_overflow_checks_std
1303 .or(rust_overflow_checks)
1304 .unwrap_or(rust_debug == Some(true)),
1305 rust_parallel_frontend_threads: rust_parallel_frontend_threads.map(threads_from_config),
1306 rust_profile_generate: flags_rust_profile_generate.or(rust_profile_generate),
1307 rust_profile_use: flags_rust_profile_use.or(rust_profile_use),
1308 rust_randomize_layout: rust_randomize_layout.unwrap_or(false),
1309 rust_remap_debuginfo: rust_remap_debuginfo.unwrap_or(false),
1310 rust_rpath: rust_rpath.unwrap_or(true),
1311 rust_stack_protector,
1312 rust_std_features: rust_std_features
1313 .unwrap_or(BTreeSet::from([String::from("panic-unwind")])),
1314 rust_strip: rust_strip.unwrap_or(false),
1315 rust_thin_lto_import_instr_limit,
1316 rust_validate_mir_opts,
1317 rust_verify_llvm_ir: rust_verify_llvm_ir.unwrap_or(false),
1318 rustc_debug_assertions: rust_rustc_debug_assertions.unwrap_or(rust_debug == Some(true)),
1319 rustc_default_linker: rust_default_linker,
1320 rustc_error_format: flags_rustc_error_format,
1321 rustfmt_info,
1322 sanitizers: build_sanitizers.unwrap_or(false),
1323 save_toolstates: rust_save_toolstates.map(PathBuf::from),
1324 skip,
1325 skip_std_check_if_no_download_rustc: flags_skip_std_check_if_no_download_rustc,
1326 src,
1327 stage,
1328 stage0_metadata,
1329 std_debug_assertions: rust_std_debug_assertions
1330 .or(rust_rustc_debug_assertions)
1331 .unwrap_or(rust_debug == Some(true)),
1332 stderr_is_tty: std::io::stderr().is_terminal(),
1333 stdout_is_tty: std::io::stdout().is_terminal(),
1334 submodules: build_submodules,
1335 sysconfdir: install_sysconfdir.map(PathBuf::from),
1336 target_config,
1337 targets,
1338 test_compare_mode: rust_test_compare_mode.unwrap_or(false),
1339 tidy_extra_checks: build_tidy_extra_checks,
1340 tool: build_tool.unwrap_or_default(),
1341 tools: build_tools,
1342 tools_debug_assertions: rust_tools_debug_assertions
1343 .or(rust_rustc_debug_assertions)
1344 .unwrap_or(rust_debug == Some(true)),
1345 vendor,
1346 verbose_tests,
1347 windows_rc: build_windows_rc.map(PathBuf::from),
1348 }
1350 }
1351
1352 pub fn dry_run(&self) -> bool {
1353 self.exec_ctx.dry_run()
1354 }
1355
1356 pub fn is_explicit_stage(&self) -> bool {
1357 self.explicit_stage_from_cli || self.explicit_stage_from_config
1358 }
1359
1360 pub(crate) fn test_args(&self) -> Vec<&str> {
1361 let mut test_args = match self.cmd {
1362 Subcommand::Test { ref test_args, .. }
1363 | Subcommand::Bench { ref test_args, .. }
1364 | Subcommand::Miri { ref test_args, .. } => {
1365 test_args.iter().flat_map(|s| s.split_whitespace()).collect()
1366 }
1367 _ => vec![],
1368 };
1369 test_args.extend(self.free_args.iter().map(|s| s.as_str()));
1370 test_args
1371 }
1372
1373 pub(crate) fn args(&self) -> Vec<&str> {
1374 let mut args = match self.cmd {
1375 Subcommand::Run { ref args, .. } => {
1376 args.iter().flat_map(|s| s.split_whitespace()).collect()
1377 }
1378 _ => vec![],
1379 };
1380 args.extend(self.free_args.iter().map(|s| s.as_str()));
1381 args
1382 }
1383
1384 pub(crate) fn read_file_by_commit(&self, file: &Path, commit: &str) -> String {
1386 let dwn_ctx = DownloadContext::from(self);
1387 read_file_by_commit(dwn_ctx, &self.rust_info, file, commit)
1388 }
1389
1390 pub(crate) fn artifact_version_part(&self, commit: &str) -> String {
1393 let (channel, version) = if self.rust_info.is_managed_git_subrepository() {
1394 let channel =
1395 self.read_file_by_commit(Path::new("src/ci/channel"), commit).trim().to_owned();
1396 let version =
1397 self.read_file_by_commit(Path::new("src/version"), commit).trim().to_owned();
1398 (channel, version)
1399 } else {
1400 let channel = fs::read_to_string(self.src.join("src/ci/channel"));
1401 let version = fs::read_to_string(self.src.join("src/version"));
1402 match (channel, version) {
1403 (Ok(channel), Ok(version)) => {
1404 (channel.trim().to_owned(), version.trim().to_owned())
1405 }
1406 (channel, version) => {
1407 let src = self.src.display();
1408 eprintln!("ERROR: failed to determine artifact channel and/or version");
1409 eprintln!(
1410 "HELP: consider using a git checkout or ensure these files are readable"
1411 );
1412 if let Err(channel) = channel {
1413 eprintln!("reading {src}/src/ci/channel failed: {channel:?}");
1414 }
1415 if let Err(version) = version {
1416 eprintln!("reading {src}/src/version failed: {version:?}");
1417 }
1418 panic!();
1419 }
1420 }
1421 };
1422
1423 match channel.as_str() {
1424 "stable" => version,
1425 "beta" => channel,
1426 "nightly" => channel,
1427 other => unreachable!("{:?} is not recognized as a valid channel", other),
1428 }
1429 }
1430
1431 pub fn bindir_relative(&self) -> &Path {
1433 let bindir = &self.bindir;
1434 if bindir.is_absolute() {
1435 if let Some(prefix) = &self.prefix
1437 && let Ok(stripped) = bindir.strip_prefix(prefix)
1438 {
1439 return stripped;
1440 }
1441 }
1442 bindir
1443 }
1444
1445 pub fn libdir_relative(&self) -> Option<&Path> {
1447 let libdir = self.libdir.as_ref()?;
1448 if libdir.is_relative() {
1449 Some(libdir)
1450 } else {
1451 libdir.strip_prefix(self.prefix.as_ref()?).ok()
1453 }
1454 }
1455
1456 pub(crate) fn ci_llvm_root(&self) -> PathBuf {
1458 let dwn_ctx = DownloadContext::from(self);
1459 ci_llvm_root(dwn_ctx, self.llvm_from_ci, &self.out)
1460 }
1461
1462 pub(crate) fn ci_rustc_dir(&self) -> PathBuf {
1464 assert!(self.download_rustc());
1465 self.out.join(self.host_target).join("ci-rustc")
1466 }
1467
1468 pub(crate) fn llvm_link_shared(&self) -> bool {
1473 let mut opt = self.llvm_link_shared.get();
1474 if opt.is_none() && self.dry_run() {
1475 return false;
1477 }
1478
1479 let llvm_link_shared = *opt.get_or_insert_with(|| {
1480 if self.llvm_from_ci {
1481 self.maybe_download_ci_llvm();
1482 let ci_llvm = self.ci_llvm_root();
1483 let link_type = t!(
1484 std::fs::read_to_string(ci_llvm.join("link-type.txt")),
1485 format!("CI llvm missing: {}", ci_llvm.display())
1486 );
1487 link_type == "dynamic"
1488 } else {
1489 false
1492 }
1493 });
1494 self.llvm_link_shared.set(opt);
1495 llvm_link_shared
1496 }
1497
1498 pub(crate) fn download_rustc(&self) -> bool {
1500 self.download_rustc_commit().is_some()
1501 }
1502
1503 pub(crate) fn download_rustc_commit(&self) -> Option<&str> {
1504 static DOWNLOAD_RUSTC: OnceLock<Option<String>> = OnceLock::new();
1505 if self.dry_run() && DOWNLOAD_RUSTC.get().is_none() {
1506 return self.download_rustc_commit.as_deref();
1508 }
1509
1510 DOWNLOAD_RUSTC
1511 .get_or_init(|| match &self.download_rustc_commit {
1512 None => None,
1513 Some(commit) => {
1514 self.download_ci_rustc(commit);
1515
1516 if !self.llvm_from_ci {
1520 if self.is_running_on_ci {
1523 println!("WARNING: LLVM submodule has changes, `download-rustc` will be disabled.");
1524 return None;
1525 } else {
1526 panic!("ERROR: LLVM submodule has changes, `download-rustc` can't be used.");
1527 }
1528 }
1529
1530 if let Some(config_path) = &self.config {
1531 let ci_config_toml = match self.get_builder_toml("ci-rustc") {
1532 Ok(ci_config_toml) => ci_config_toml,
1533 Err(e) if e.to_string().contains("unknown field") => {
1534 println!("WARNING: CI rustc has some fields that are no longer supported in bootstrap; download-rustc will be disabled.");
1535 println!("HELP: Consider rebasing to a newer commit if available.");
1536 return None;
1537 },
1538 Err(e) => {
1539 eprintln!("ERROR: Failed to parse CI rustc bootstrap.toml: {e}");
1540 exit!(2);
1541 },
1542 };
1543
1544 let current_config_toml = Self::get_toml(config_path).unwrap();
1545
1546 let res = check_incompatible_options_for_ci_rustc(
1549 self.host_target,
1550 current_config_toml,
1551 ci_config_toml,
1552 );
1553
1554 let disable_ci_rustc_if_incompatible = env::var_os("DISABLE_CI_RUSTC_IF_INCOMPATIBLE")
1557 .is_some_and(|s| s == "1" || s == "true");
1558
1559 if disable_ci_rustc_if_incompatible && res.is_err() {
1560 println!("WARNING: download-rustc is disabled with `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` env.");
1561 return None;
1562 }
1563
1564 res.unwrap();
1565 }
1566
1567 Some(commit.clone())
1568 }
1569 })
1570 .as_deref()
1571 }
1572
1573 pub fn verbose(&self, f: impl Fn()) {
1575 self.exec_ctx.verbose(f);
1576 }
1577
1578 pub fn any_sanitizers_to_build(&self) -> bool {
1579 self.target_config
1580 .iter()
1581 .any(|(ts, t)| !ts.is_msvc() && t.sanitizers.unwrap_or(self.sanitizers))
1582 }
1583
1584 pub fn any_profiler_enabled(&self) -> bool {
1585 self.target_config.values().any(|t| matches!(&t.profiler, Some(p) if p.is_string_or_true()))
1586 || self.profiler
1587 }
1588
1589 pub fn submodules(&self) -> bool {
1591 self.submodules.unwrap_or(self.rust_info.is_managed_git_subrepository())
1594 }
1595
1596 pub fn git_config(&self) -> GitConfig<'_> {
1597 GitConfig {
1598 nightly_branch: &self.stage0_metadata.config.nightly_branch,
1599 git_merge_commit_email: &self.stage0_metadata.config.git_merge_commit_email,
1600 }
1601 }
1602
1603 #[cfg_attr(
1613 feature = "tracing",
1614 instrument(
1615 level = "trace",
1616 name = "Config::update_submodule",
1617 skip_all,
1618 fields(relative_path = ?relative_path),
1619 ),
1620 )]
1621 pub(crate) fn update_submodule(&self, relative_path: &str) {
1622 let dwn_ctx = DownloadContext::from(self);
1623 update_submodule(dwn_ctx, &self.rust_info, relative_path);
1624 }
1625
1626 pub fn has_changes_from_upstream(&self, paths: &[&'static str]) -> bool {
1628 let dwn_ctx = DownloadContext::from(self);
1629 has_changes_from_upstream(dwn_ctx, paths)
1630 }
1631
1632 pub fn check_path_modifications(&self, paths: &[&'static str]) -> PathFreshness {
1634 self.path_modification_cache
1640 .lock()
1641 .unwrap()
1642 .entry(paths.to_vec())
1643 .or_insert_with(|| {
1644 check_path_modifications(&self.src, &self.git_config(), paths, CiEnv::current())
1645 .unwrap()
1646 })
1647 .clone()
1648 }
1649
1650 pub fn sanitizers_enabled(&self, target: TargetSelection) -> bool {
1651 self.target_config.get(&target).and_then(|t| t.sanitizers).unwrap_or(self.sanitizers)
1652 }
1653
1654 pub fn needs_sanitizer_runtime_built(&self, target: TargetSelection) -> bool {
1655 !target.is_msvc() && self.sanitizers_enabled(target)
1657 }
1658
1659 pub fn profiler_path(&self, target: TargetSelection) -> Option<&str> {
1660 match self.target_config.get(&target)?.profiler.as_ref()? {
1661 StringOrBool::String(s) => Some(s),
1662 StringOrBool::Bool(_) => None,
1663 }
1664 }
1665
1666 pub fn profiler_enabled(&self, target: TargetSelection) -> bool {
1667 self.target_config
1668 .get(&target)
1669 .and_then(|t| t.profiler.as_ref())
1670 .map(StringOrBool::is_string_or_true)
1671 .unwrap_or(self.profiler)
1672 }
1673
1674 pub fn enabled_codegen_backends(&self, target: TargetSelection) -> &[CodegenBackendKind] {
1678 self.target_config
1679 .get(&target)
1680 .and_then(|cfg| cfg.codegen_backends.as_deref())
1681 .unwrap_or(&self.rust_codegen_backends)
1682 }
1683
1684 pub fn default_codegen_backend(&self, target: TargetSelection) -> &CodegenBackendKind {
1687 self.enabled_codegen_backends(target).first().unwrap()
1689 }
1690
1691 pub fn jemalloc(&self, target: TargetSelection) -> bool {
1692 self.target_config.get(&target).and_then(|cfg| cfg.jemalloc).unwrap_or(self.jemalloc)
1693 }
1694
1695 pub fn rpath_enabled(&self, target: TargetSelection) -> bool {
1696 self.target_config.get(&target).and_then(|t| t.rpath).unwrap_or(self.rust_rpath)
1697 }
1698
1699 pub fn optimized_compiler_builtins(&self, target: TargetSelection) -> &CompilerBuiltins {
1700 self.target_config
1701 .get(&target)
1702 .and_then(|t| t.optimized_compiler_builtins.as_ref())
1703 .unwrap_or(&self.optimized_compiler_builtins)
1704 }
1705
1706 pub fn llvm_enabled(&self, target: TargetSelection) -> bool {
1707 self.enabled_codegen_backends(target).contains(&CodegenBackendKind::Llvm)
1708 }
1709
1710 pub fn llvm_libunwind(&self, target: TargetSelection) -> LlvmLibunwind {
1711 self.target_config
1712 .get(&target)
1713 .and_then(|t| t.llvm_libunwind)
1714 .or(self.llvm_libunwind_default)
1715 .unwrap_or(if target.contains("fuchsia") {
1716 LlvmLibunwind::InTree
1717 } else {
1718 LlvmLibunwind::No
1719 })
1720 }
1721
1722 pub fn split_debuginfo(&self, target: TargetSelection) -> SplitDebuginfo {
1723 self.target_config
1724 .get(&target)
1725 .and_then(|t| t.split_debuginfo)
1726 .unwrap_or_else(|| SplitDebuginfo::default_for_platform(target))
1727 }
1728
1729 pub fn is_host_target(&self, target: TargetSelection) -> bool {
1731 self.host_target == target
1732 }
1733
1734 pub fn is_system_llvm(&self, target: TargetSelection) -> bool {
1739 let dwn_ctx = DownloadContext::from(self);
1740 is_system_llvm(dwn_ctx, &self.target_config, self.llvm_from_ci, target)
1741 }
1742
1743 pub fn is_rust_llvm(&self, target: TargetSelection) -> bool {
1747 match self.target_config.get(&target) {
1748 Some(Target { llvm_has_rust_patches: Some(patched), .. }) => *patched,
1752 _ => !self.is_system_llvm(target),
1755 }
1756 }
1757
1758 pub fn exec_ctx(&self) -> &ExecutionContext {
1759 &self.exec_ctx
1760 }
1761
1762 pub fn git_info(&self, omit_git_hash: bool, dir: &Path) -> GitInfo {
1763 GitInfo::new(omit_git_hash, dir, self)
1764 }
1765}
1766
1767impl AsRef<ExecutionContext> for Config {
1768 fn as_ref(&self) -> &ExecutionContext {
1769 &self.exec_ctx
1770 }
1771}
1772
1773fn compute_src_directory(src_dir: Option<PathBuf>, exec_ctx: &ExecutionContext) -> Option<PathBuf> {
1774 if let Some(src) = src_dir {
1775 return Some(src);
1776 } else {
1777 let mut cmd = helpers::git(None);
1780 cmd.arg("rev-parse").arg("--show-cdup");
1788 let output = cmd.allow_failure().run_capture_stdout(exec_ctx);
1790 if output.is_success() {
1791 let git_root_relative = output.stdout();
1792 let git_root = env::current_dir()
1795 .unwrap()
1796 .join(PathBuf::from(git_root_relative.trim()))
1797 .canonicalize()
1798 .unwrap();
1799 let s = git_root.to_str().unwrap();
1800
1801 let git_root = match s.strip_prefix("\\\\?\\") {
1803 Some(p) => PathBuf::from(p),
1804 None => git_root,
1805 };
1806 if git_root.join("src").join("stage0").exists() {
1813 return Some(git_root);
1814 }
1815 } else {
1816 }
1819 };
1820 None
1821}
1822
1823fn load_toml_config(
1828 src: &Path,
1829 config_path: Option<PathBuf>,
1830 get_toml: &impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>,
1831) -> (TomlConfig, Option<PathBuf>) {
1832 let toml_path = config_path.or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from));
1840 let using_default_path = toml_path.is_none();
1841 let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("bootstrap.toml"));
1842
1843 if using_default_path && !toml_path.exists() {
1844 toml_path = src.join(PathBuf::from("bootstrap.toml"));
1845 if !toml_path.exists() {
1846 toml_path = PathBuf::from("config.toml");
1847 if !toml_path.exists() {
1848 toml_path = src.join(PathBuf::from("config.toml"));
1849 }
1850 }
1851 }
1852
1853 if !using_default_path || toml_path.exists() {
1856 let path = Some(if cfg!(not(test)) {
1857 toml_path = toml_path.canonicalize().unwrap();
1858 toml_path.clone()
1859 } else {
1860 toml_path.clone()
1861 });
1862 (get_toml(&toml_path).unwrap_or_else(|e| bad_config(&toml_path, e)), path)
1863 } else {
1864 (TomlConfig::default(), None)
1865 }
1866}
1867
1868fn postprocess_toml(
1869 toml: &mut TomlConfig,
1870 src_dir: &Path,
1871 toml_path: Option<PathBuf>,
1872 exec_ctx: &ExecutionContext,
1873 override_set: &[String],
1874 get_toml: &impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>,
1875) {
1876 let git_info = GitInfo::new(false, src_dir, exec_ctx);
1877
1878 if git_info.is_from_tarball() && toml.profile.is_none() {
1879 toml.profile = Some("dist".into());
1880 }
1881
1882 for include_path in toml.include.clone().unwrap_or_default().iter().rev() {
1888 let include_path = toml_path
1889 .as_ref()
1890 .expect("include found in default TOML config")
1891 .parent()
1892 .unwrap()
1893 .join(include_path);
1894
1895 let included_toml =
1896 get_toml(&include_path).unwrap_or_else(|e| bad_config(&include_path, e));
1897 toml.merge(
1898 Some(include_path),
1899 &mut Default::default(),
1900 included_toml,
1901 ReplaceOpt::IgnoreDuplicate,
1902 );
1903 }
1904
1905 if let Some(include) = &toml.profile {
1906 let profile_aliases = HashMap::from([("user", "dist")]);
1910 let include = match profile_aliases.get(include.as_str()) {
1911 Some(alias) => alias,
1912 None => include.as_str(),
1913 };
1914 let mut include_path = PathBuf::from(src_dir);
1915 include_path.push("src");
1916 include_path.push("bootstrap");
1917 include_path.push("defaults");
1918 include_path.push(format!("bootstrap.{include}.toml"));
1919 let included_toml = get_toml(&include_path).unwrap_or_else(|e| {
1920 eprintln!(
1921 "ERROR: Failed to parse default config profile at '{}': {e}",
1922 include_path.display()
1923 );
1924 exit!(2);
1925 });
1926 toml.merge(
1927 Some(include_path),
1928 &mut Default::default(),
1929 included_toml,
1930 ReplaceOpt::IgnoreDuplicate,
1931 );
1932 }
1933
1934 let mut override_toml = TomlConfig::default();
1935 for option in override_set.iter() {
1936 fn get_table(option: &str) -> Result<TomlConfig, toml::de::Error> {
1937 toml::from_str(option).and_then(|table: toml::Value| TomlConfig::deserialize(table))
1938 }
1939
1940 let mut err = match get_table(option) {
1941 Ok(v) => {
1942 override_toml.merge(None, &mut Default::default(), v, ReplaceOpt::ErrorOnDuplicate);
1943 continue;
1944 }
1945 Err(e) => e,
1946 };
1947 if let Some((key, value)) = option.split_once('=')
1950 && !value.contains('"')
1951 {
1952 match get_table(&format!(r#"{key}="{value}""#)) {
1953 Ok(v) => {
1954 override_toml.merge(
1955 None,
1956 &mut Default::default(),
1957 v,
1958 ReplaceOpt::ErrorOnDuplicate,
1959 );
1960 continue;
1961 }
1962 Err(e) => err = e,
1963 }
1964 }
1965 eprintln!("failed to parse override `{option}`: `{err}");
1966 exit!(2)
1967 }
1968 toml.merge(None, &mut Default::default(), override_toml, ReplaceOpt::Override);
1969}
1970
1971#[cfg(test)]
1972pub fn check_stage0_version(
1973 _program_path: &Path,
1974 _component_name: &'static str,
1975 _src_dir: &Path,
1976 _exec_ctx: &ExecutionContext,
1977) {
1978}
1979
1980#[cfg(not(test))]
1982pub fn check_stage0_version(
1983 program_path: &Path,
1984 component_name: &'static str,
1985 src_dir: &Path,
1986 exec_ctx: &ExecutionContext,
1987) {
1988 use build_helper::util::fail;
1989
1990 if exec_ctx.dry_run() {
1991 return;
1992 }
1993
1994 let stage0_output =
1995 command(program_path).arg("--version").run_capture_stdout(exec_ctx).stdout();
1996 let mut stage0_output = stage0_output.lines().next().unwrap().split(' ');
1997
1998 let stage0_name = stage0_output.next().unwrap();
1999 if stage0_name != component_name {
2000 fail(&format!(
2001 "Expected to find {component_name} at {} but it claims to be {stage0_name}",
2002 program_path.display()
2003 ));
2004 }
2005
2006 let stage0_version =
2007 semver::Version::parse(stage0_output.next().unwrap().split('-').next().unwrap().trim())
2008 .unwrap();
2009 let source_version =
2010 semver::Version::parse(fs::read_to_string(src_dir.join("src/version")).unwrap().trim())
2011 .unwrap();
2012 if !(source_version == stage0_version
2013 || (source_version.major == stage0_version.major
2014 && (source_version.minor == stage0_version.minor
2015 || source_version.minor == stage0_version.minor + 1)))
2016 {
2017 let prev_version = format!("{}.{}.x", source_version.major, source_version.minor - 1);
2018 fail(&format!(
2019 "Unexpected {component_name} version: {stage0_version}, we should use {prev_version}/{source_version} to build source with {source_version}"
2020 ));
2021 }
2022}
2023
2024pub fn download_ci_rustc_commit<'a>(
2025 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2026 rust_info: &channel::GitInfo,
2027 download_rustc: Option<StringOrBool>,
2028 llvm_assertions: bool,
2029) -> Option<String> {
2030 let dwn_ctx = dwn_ctx.as_ref();
2031
2032 if !is_download_ci_available(&dwn_ctx.host_target.triple, llvm_assertions) {
2033 return None;
2034 }
2035
2036 let if_unchanged = match download_rustc {
2038 None | Some(StringOrBool::Bool(false)) => return None,
2044 Some(StringOrBool::Bool(true)) => false,
2045 Some(StringOrBool::String(s)) if s == "if-unchanged" => {
2046 if !rust_info.is_managed_git_subrepository() {
2047 println!(
2048 "ERROR: `download-rustc=if-unchanged` is only compatible with Git managed sources."
2049 );
2050 crate::exit!(1);
2051 }
2052
2053 true
2054 }
2055 Some(StringOrBool::String(other)) => {
2056 panic!("unrecognized option for download-rustc: {other}")
2057 }
2058 };
2059
2060 let commit = if rust_info.is_managed_git_subrepository() {
2061 let freshness = check_path_modifications_(dwn_ctx, RUSTC_IF_UNCHANGED_ALLOWED_PATHS);
2064 dwn_ctx.exec_ctx.verbose(|| {
2065 eprintln!("rustc freshness: {freshness:?}");
2066 });
2067 match freshness {
2068 PathFreshness::LastModifiedUpstream { upstream } => upstream,
2069 PathFreshness::HasLocalModifications { upstream } => {
2070 if if_unchanged {
2071 return None;
2072 }
2073
2074 if dwn_ctx.is_running_on_ci {
2075 eprintln!("CI rustc commit matches with HEAD and we are in CI.");
2076 eprintln!(
2077 "`rustc.download-ci` functionality will be skipped as artifacts are not available."
2078 );
2079 return None;
2080 }
2081
2082 upstream
2083 }
2084 PathFreshness::MissingUpstream => {
2085 eprintln!("No upstream commit found");
2086 return None;
2087 }
2088 }
2089 } else {
2090 channel::read_commit_info_file(dwn_ctx.src)
2091 .map(|info| info.sha.trim().to_owned())
2092 .expect("git-commit-info is missing in the project root")
2093 };
2094
2095 Some(commit)
2096}
2097
2098pub fn check_path_modifications_<'a>(
2099 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2100 paths: &[&'static str],
2101) -> PathFreshness {
2102 let dwn_ctx = dwn_ctx.as_ref();
2103 dwn_ctx
2109 .path_modification_cache
2110 .lock()
2111 .unwrap()
2112 .entry(paths.to_vec())
2113 .or_insert_with(|| {
2114 check_path_modifications(
2115 dwn_ctx.src,
2116 &git_config(dwn_ctx.stage0_metadata),
2117 paths,
2118 CiEnv::current(),
2119 )
2120 .unwrap()
2121 })
2122 .clone()
2123}
2124
2125pub fn git_config(stage0_metadata: &build_helper::stage0_parser::Stage0) -> GitConfig<'_> {
2126 GitConfig {
2127 nightly_branch: &stage0_metadata.config.nightly_branch,
2128 git_merge_commit_email: &stage0_metadata.config.git_merge_commit_email,
2129 }
2130}
2131
2132pub fn parse_download_ci_llvm<'a>(
2133 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2134 rust_info: &channel::GitInfo,
2135 download_rustc_commit: &Option<String>,
2136 download_ci_llvm: Option<StringOrBool>,
2137 asserts: bool,
2138) -> bool {
2139 let dwn_ctx = dwn_ctx.as_ref();
2140 let download_ci_llvm = download_ci_llvm.unwrap_or(StringOrBool::Bool(true));
2141
2142 let if_unchanged = || {
2143 if rust_info.is_from_tarball() {
2144 println!("ERROR: 'if-unchanged' is only compatible with Git managed sources.");
2146 crate::exit!(1);
2147 }
2148
2149 #[cfg(not(test))]
2151 update_submodule(dwn_ctx, rust_info, "src/llvm-project");
2152
2153 let has_changes = has_changes_from_upstream(dwn_ctx, LLVM_INVALIDATION_PATHS);
2155
2156 if has_changes {
2158 false
2159 } else {
2160 llvm::is_ci_llvm_available_for_target(&dwn_ctx.host_target, asserts)
2161 }
2162 };
2163
2164 match download_ci_llvm {
2165 StringOrBool::Bool(b) => {
2166 if !b && download_rustc_commit.is_some() {
2167 panic!(
2168 "`llvm.download-ci-llvm` cannot be set to `false` if `rust.download-rustc` is set to `true` or `if-unchanged`."
2169 );
2170 }
2171
2172 #[cfg(not(test))]
2173 if b && dwn_ctx.is_running_on_ci && CiEnv::is_rust_lang_managed_ci_job() {
2174 panic!(
2176 "`llvm.download-ci-llvm` cannot be set to `true` on CI. Use `if-unchanged` instead."
2177 );
2178 }
2179
2180 b && llvm::is_ci_llvm_available_for_target(&dwn_ctx.host_target, asserts)
2182 }
2183 StringOrBool::String(s) if s == "if-unchanged" => if_unchanged(),
2184 StringOrBool::String(other) => {
2185 panic!("unrecognized option for download-ci-llvm: {other:?}")
2186 }
2187 }
2188}
2189
2190pub fn has_changes_from_upstream<'a>(
2191 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2192 paths: &[&'static str],
2193) -> bool {
2194 let dwn_ctx = dwn_ctx.as_ref();
2195 match check_path_modifications_(dwn_ctx, paths) {
2196 PathFreshness::LastModifiedUpstream { .. } => false,
2197 PathFreshness::HasLocalModifications { .. } | PathFreshness::MissingUpstream => true,
2198 }
2199}
2200
2201#[cfg_attr(
2202 feature = "tracing",
2203 instrument(
2204 level = "trace",
2205 name = "Config::update_submodule",
2206 skip_all,
2207 fields(relative_path = ?relative_path),
2208 ),
2209)]
2210pub(crate) fn update_submodule<'a>(
2211 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2212 rust_info: &channel::GitInfo,
2213 relative_path: &str,
2214) {
2215 let dwn_ctx = dwn_ctx.as_ref();
2216 if rust_info.is_from_tarball() || !submodules_(dwn_ctx.submodules, rust_info) {
2217 return;
2218 }
2219
2220 let absolute_path = dwn_ctx.src.join(relative_path);
2221
2222 if !absolute_path.exists() {
2226 t!(fs::create_dir_all(&absolute_path));
2227 }
2228
2229 if !git_info(dwn_ctx.exec_ctx, false, &absolute_path).is_managed_git_subrepository()
2232 && !helpers::dir_is_empty(&absolute_path)
2233 {
2234 return;
2235 }
2236
2237 let submodule_git = || {
2244 let mut cmd = helpers::git(Some(&absolute_path));
2245 cmd.run_in_dry_run();
2246 cmd
2247 };
2248
2249 let checked_out_hash =
2251 submodule_git().args(["rev-parse", "HEAD"]).run_capture_stdout(dwn_ctx.exec_ctx).stdout();
2252 let checked_out_hash = checked_out_hash.trim_end();
2253 let recorded = helpers::git(Some(dwn_ctx.src))
2255 .run_in_dry_run()
2256 .args(["ls-tree", "HEAD"])
2257 .arg(relative_path)
2258 .run_capture_stdout(dwn_ctx.exec_ctx)
2259 .stdout();
2260
2261 let actual_hash = recorded
2262 .split_whitespace()
2263 .nth(2)
2264 .unwrap_or_else(|| panic!("unexpected output `{recorded}`"));
2265
2266 if actual_hash == checked_out_hash {
2267 return;
2269 }
2270
2271 println!("Updating submodule {relative_path}");
2272
2273 helpers::git(Some(dwn_ctx.src))
2274 .allow_failure()
2275 .run_in_dry_run()
2276 .args(["submodule", "-q", "sync"])
2277 .arg(relative_path)
2278 .run(dwn_ctx.exec_ctx);
2279
2280 let update = |progress: bool| {
2282 let current_branch = helpers::git(Some(dwn_ctx.src))
2285 .allow_failure()
2286 .run_in_dry_run()
2287 .args(["symbolic-ref", "--short", "HEAD"])
2288 .run_capture(dwn_ctx.exec_ctx);
2289
2290 let mut git = helpers::git(Some(dwn_ctx.src)).allow_failure();
2291 git.run_in_dry_run();
2292 if current_branch.is_success() {
2293 let branch = current_branch.stdout();
2296 let branch = branch.trim();
2297 let branch = branch.strip_prefix("heads/").unwrap_or(branch);
2298 git.arg("-c").arg(format!("branch.{branch}.remote=origin"));
2299 }
2300 git.args(["submodule", "update", "--init", "--recursive", "--depth=1"]);
2301 if progress {
2302 git.arg("--progress");
2303 }
2304 git.arg(relative_path);
2305 git
2306 };
2307 if !update(true).allow_failure().run(dwn_ctx.exec_ctx) {
2308 update(false).allow_failure().run(dwn_ctx.exec_ctx);
2309 }
2310
2311 let has_local_modifications = !submodule_git()
2314 .allow_failure()
2315 .args(["diff-index", "--quiet", "HEAD"])
2316 .run(dwn_ctx.exec_ctx);
2317 if has_local_modifications {
2318 submodule_git().allow_failure().args(["stash", "push"]).run(dwn_ctx.exec_ctx);
2319 }
2320
2321 submodule_git().allow_failure().args(["reset", "-q", "--hard"]).run(dwn_ctx.exec_ctx);
2322 submodule_git().allow_failure().args(["clean", "-qdfx"]).run(dwn_ctx.exec_ctx);
2323
2324 if has_local_modifications {
2325 submodule_git().allow_failure().args(["stash", "pop"]).run(dwn_ctx.exec_ctx);
2326 }
2327}
2328
2329pub fn git_info(exec_ctx: &ExecutionContext, omit_git_hash: bool, dir: &Path) -> GitInfo {
2330 GitInfo::new(omit_git_hash, dir, exec_ctx)
2331}
2332
2333pub fn submodules_(submodules: &Option<bool>, rust_info: &channel::GitInfo) -> bool {
2334 submodules.unwrap_or(rust_info.is_managed_git_subrepository())
2337}
2338
2339pub fn is_system_llvm<'a>(
2344 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2345 target_config: &HashMap<TargetSelection, Target>,
2346 llvm_from_ci: bool,
2347 target: TargetSelection,
2348) -> bool {
2349 let dwn_ctx = dwn_ctx.as_ref();
2350 match target_config.get(&target) {
2351 Some(Target { llvm_config: Some(_), .. }) => {
2352 let ci_llvm = llvm_from_ci && is_host_target(&dwn_ctx.host_target, &target);
2353 !ci_llvm
2354 }
2355 Some(Target { llvm_config: None, .. }) => false,
2357 None => false,
2358 }
2359}
2360
2361pub fn is_host_target(host_target: &TargetSelection, target: &TargetSelection) -> bool {
2362 host_target == target
2363}
2364
2365pub(crate) fn ci_llvm_root<'a>(
2366 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2367 llvm_from_ci: bool,
2368 out: &Path,
2369) -> PathBuf {
2370 let dwn_ctx = dwn_ctx.as_ref();
2371 assert!(llvm_from_ci);
2372 out.join(dwn_ctx.host_target).join("ci-llvm")
2373}
2374
2375pub(crate) fn read_file_by_commit<'a>(
2377 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2378 rust_info: &channel::GitInfo,
2379 file: &Path,
2380 commit: &str,
2381) -> String {
2382 let dwn_ctx = dwn_ctx.as_ref();
2383 assert!(
2384 rust_info.is_managed_git_subrepository(),
2385 "`Config::read_file_by_commit` is not supported in non-git sources."
2386 );
2387
2388 let mut git = helpers::git(Some(dwn_ctx.src));
2389 git.arg("show").arg(format!("{commit}:{}", file.to_str().unwrap()));
2390 git.run_capture_stdout(dwn_ctx.exec_ctx).stdout()
2391}
2392
2393fn bad_config(toml_path: &Path, e: toml::de::Error) -> ! {
2394 eprintln!("ERROR: Failed to parse '{}': {e}", toml_path.display());
2395 let e_s = e.to_string();
2396 if e_s.contains("unknown field")
2397 && let Some(field_name) = e_s.split("`").nth(1)
2398 && let sections = find_correct_section_for_field(field_name)
2399 && !sections.is_empty()
2400 {
2401 if sections.len() == 1 {
2402 match sections[0] {
2403 WouldBeValidFor::TopLevel { is_section } => {
2404 if is_section {
2405 eprintln!(
2406 "hint: section name `{field_name}` used as a key within a section"
2407 );
2408 } else {
2409 eprintln!("hint: try using `{field_name}` as a top level key");
2410 }
2411 }
2412 WouldBeValidFor::Section(section) => {
2413 eprintln!("hint: try moving `{field_name}` to the `{section}` section")
2414 }
2415 }
2416 } else {
2417 eprintln!(
2418 "hint: `{field_name}` would be valid {}",
2419 join_oxford_comma(sections.iter(), "or"),
2420 );
2421 }
2422 }
2423
2424 exit!(2);
2425}
2426
2427#[derive(Copy, Clone, Debug)]
2428enum WouldBeValidFor {
2429 TopLevel { is_section: bool },
2430 Section(&'static str),
2431}
2432
2433fn join_oxford_comma(
2434 mut parts: impl ExactSizeIterator<Item = impl std::fmt::Display>,
2435 conj: &str,
2436) -> String {
2437 use std::fmt::Write;
2438 let mut out = String::new();
2439
2440 assert!(parts.len() > 1);
2441 while let Some(part) = parts.next() {
2442 if parts.len() == 0 {
2443 write!(&mut out, "{conj} {part}")
2444 } else {
2445 write!(&mut out, "{part}, ")
2446 }
2447 .unwrap();
2448 }
2449 out
2450}
2451
2452impl std::fmt::Display for WouldBeValidFor {
2453 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2454 match self {
2455 Self::TopLevel { .. } => write!(f, "at top level"),
2456 Self::Section(section_name) => write!(f, "in section `{section_name}`"),
2457 }
2458 }
2459}
2460
2461fn find_correct_section_for_field(field_name: &str) -> Vec<WouldBeValidFor> {
2462 let sections = ["build", "install", "llvm", "gcc", "rust", "dist"];
2463 sections
2464 .iter()
2465 .map(Some)
2466 .chain([None])
2467 .filter_map(|section_name| {
2468 let dummy_config_str = if let Some(section_name) = section_name {
2469 format!("{section_name}.{field_name} = 0\n")
2470 } else {
2471 format!("{field_name} = 0\n")
2472 };
2473 let is_unknown_field = toml::from_str::<toml::Value>(&dummy_config_str)
2474 .and_then(TomlConfig::deserialize)
2475 .err()
2476 .is_some_and(|e| e.to_string().contains("unknown field"));
2477 if is_unknown_field {
2478 None
2479 } else {
2480 Some(section_name.copied().map(WouldBeValidFor::Section).unwrap_or_else(|| {
2481 WouldBeValidFor::TopLevel { is_section: sections.contains(&field_name) }
2482 }))
2483 }
2484 })
2485 .collect()
2486}