1use std::collections::{BTreeSet, HashMap, HashSet};
17use std::io::IsTerminal;
18use std::path::{Path, PathBuf, absolute};
19use std::str::FromStr;
20use std::sync::{Arc, Mutex, OnceLock};
21use std::{cmp, env, fs};
22
23use build_helper::ci::CiEnv;
24use build_helper::git::{GitConfig, PathFreshness, check_path_modifications};
25use serde::Deserialize;
26#[cfg(feature = "tracing")]
27use tracing::{instrument, span};
28
29use crate::core::backend::CodegenBackendKind;
30use crate::core::build_steps::llvm;
31use crate::core::build_steps::llvm::{LLVM_INVALIDATION_PATHS, LlvmKind, LlvmOutput};
32use crate::core::build_steps::test::failed_tests::collect_previously_failed_tests;
33use crate::core::config::flags::{Color, Flags, Subcommand, Warnings};
34use crate::core::config::macros::check_ci_llvm;
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::pgo::{Pgo, PgoConfig};
44use crate::core::config::toml::rust::{
45 BootstrapOverrideLld, Rust, RustOptimize, check_incompatible_options_for_ci_rustc,
46 parse_codegen_backends,
47};
48use crate::core::config::toml::target::{
49 DefaultLinuxLinkerOverride, Target, TomlTarget, default_linux_linker_overrides,
50};
51use crate::core::config::{
52 Allocator, CompilerBuiltins, CompressDebuginfo, DebuggerPath, DebuginfoLevel, DryRun,
53 GccCiMode, LlvmCiMode, LlvmLibunwind, Merge, ReplaceOpt, RustcLto, SplitDebuginfo,
54 StringOrBool, TargetSelection, threads_from_config,
55};
56use crate::core::download::{DownloadContext, download_beta_toolchain, is_download_ci_available};
57use crate::utils::channel::{self, GitInfo};
58use crate::utils::exec::{ExecutionContext, command};
59use crate::utils::helpers::{self, exe, fail, get_host_target, t};
60
61#[rustfmt::skip] pub const RUSTC_IF_UNCHANGED_ALLOWED_PATHS: &[&str] = &[
74 ":!library",
75 ":!src/tools",
76 ":!src/librustdoc",
77 ":!src/rustdoc-json-types",
78 ":!tests",
79 ":!triagebot.toml",
80 ":!src/bootstrap/defaults",
81];
82
83#[derive(Clone)]
92pub(crate) struct Config {
93 pub change_id: Option<ChangeId>,
94 pub bypass_bootstrap_lock: bool,
95 pub ccache: Option<String>,
96 pub sde: Option<PathBuf>,
97 pub ninja_in_file: bool,
99 pub submodules: Option<bool>,
100 pub compiler_docs: bool,
101 pub library_docs_private_items: bool,
102 pub docs_minification: bool,
103 pub docs: bool,
104 pub locked_deps: bool,
105 pub vendor: bool,
106 pub target_config: HashMap<TargetSelection, Target>,
107 pub full_bootstrap: bool,
108 pub bootstrap_cache_path: Option<PathBuf>,
109 pub extended: bool,
110 pub tools: Option<HashSet<String>>,
111 pub tool: HashMap<String, Tool>,
114 pub sanitizers: bool,
115 pub profiler: bool,
116 pub omit_git_hash: bool,
117 pub skip: Vec<PathBuf>,
118 pub include_default_paths: bool,
119 pub rustc_error_format: Option<String>,
120 pub json_output: bool,
121 pub compile_time_deps: bool,
122 pub test_compare_mode: bool,
123 pub color: Color,
124 pub patch_binaries_for_nix: Option<bool>,
125 pub stage0_metadata: build_helper::stage0_parser::Stage0,
126 pub android_ndk: Option<PathBuf>,
127 pub optimized_compiler_builtins: CompilerBuiltins,
128 pub record_failed_tests_path: PathBuf,
129
130 pub stdout_is_tty: bool,
131 pub stderr_is_tty: bool,
132
133 pub on_fail: Option<String>,
134 pub explicit_stage_from_cli: bool,
135 pub explicit_stage_from_config: bool,
136 pub stage: u32,
137 pub keep_stage: Vec<u32>,
138 pub keep_stage_std: Vec<u32>,
139 pub src: PathBuf,
140 pub config: Option<PathBuf>,
142 pub jobs: Option<u32>,
143 pub cmd: Subcommand,
144 pub quiet: bool,
145 pub incremental: bool,
146 pub dump_bootstrap_shims: bool,
147 pub free_args: Vec<String>,
150
151 pub download_rustc_commit: Option<String>,
153
154 pub deny_warnings: bool,
155 pub backtrace_on_ice: bool,
156
157 pub llvm_assertions: bool,
159 pub llvm_tests: bool,
160 pub llvm_enzyme: bool,
161 pub llvm_offload: bool,
162 pub llvm_plugins: bool,
163 pub llvm_optimize: bool,
164 pub llvm_thin_lto: bool,
165 pub llvm_release_debuginfo: bool,
166 pub llvm_static_stdcpp: bool,
167 pub llvm_libzstd: bool,
168 pub llvm_link_shared: Option<bool>,
169 pub llvm_clang_cl: Option<String>,
170 pub llvm_targets: Option<String>,
171 pub llvm_experimental_targets: Option<String>,
172 pub llvm_link_jobs: Option<u32>,
173 pub llvm_version_suffix: Option<String>,
174 pub llvm_use_linker: Option<String>,
175 pub offload_clang_dir: Option<PathBuf>,
176 pub llvm_allow_old_toolchain: bool,
177 pub llvm_polly: bool,
178 pub llvm_clang: bool,
179 pub llvm_enable_warnings: bool,
180 pub llvm_ci_mode: LlvmCiMode,
181 pub llvm_build_config: HashMap<String, String>,
182
183 pub bootstrap_override_lld: BootstrapOverrideLld,
184 pub lld_enabled: bool,
185 pub llvm_tools_enabled: bool,
186 pub llvm_bitcode_linker_enabled: bool,
187
188 pub llvm_cflags: Option<String>,
189 pub llvm_cxxflags: Option<String>,
190 pub llvm_ldflags: Option<String>,
191 pub llvm_use_libcxx: bool,
192 pub llvm_pgo: LlvmPgoConfig,
193
194 pub gcc_ci_mode: GccCiMode,
196 pub libgccjit_libs_dir: Option<PathBuf>,
197
198 pub rust_optimize: RustOptimize,
200 pub rust_codegen_units: Option<u32>,
201 pub rust_codegen_units_std: Option<u32>,
202 pub rustc_debug_assertions: bool,
203 pub std_debug_assertions: bool,
204 pub tools_debug_assertions: bool,
205
206 pub rust_overflow_checks: bool,
207 pub rust_overflow_checks_std: bool,
208 pub rust_debug_logging: bool,
209 pub rust_debuginfo_level_rustc: DebuginfoLevel,
210 pub rust_debuginfo_level_std: DebuginfoLevel,
211 pub rust_debuginfo_level_tools: DebuginfoLevel,
212 pub rust_debuginfo_level_tests: DebuginfoLevel,
213 pub rust_compress_debuginfo: CompressDebuginfo,
214 pub rust_rpath: bool,
215 pub rust_strip: bool,
216 pub rust_frame_pointers: bool,
217 pub rust_stack_protector: Option<String>,
218 pub rustc_default_linker: Option<String>,
219 pub rust_optimize_tests: bool,
220 pub rust_dist_src: bool,
221 pub rust_codegen_backends: Vec<CodegenBackendKind>,
222 pub rust_verify_llvm_ir: bool,
223 pub rust_thin_lto_import_instr_limit: Option<u32>,
224 pub rust_randomize_layout: bool,
225 pub rust_remap_debuginfo: bool,
226 pub rust_new_symbol_mangling: Option<bool>,
227 pub rust_annotate_moves_size_limit: Option<u64>,
228 pub rust_lto: RustcLto,
229 pub rust_validate_mir_opts: Option<u32>,
230 pub rust_std_features: BTreeSet<String>,
231 pub rust_break_on_ice: bool,
232 pub rust_parallel_frontend_threads: Option<u32>,
233 pub rust_rustflags: Vec<String>,
234 pub rust_pgo: PgoConfig,
235 pub rustdoc_pgo: PgoConfig,
236 pub cargo_pgo: PgoConfig,
237 pub clippy_pgo: PgoConfig,
238
239 pub stdlib_semver_baseline: Option<String>,
240
241 pub llvm_libunwind_default: Option<LlvmLibunwind>,
242 pub enable_bolt_settings: bool,
243
244 pub reproducible_artifacts: Vec<String>,
245
246 pub host_target: TargetSelection,
247 pub hosts: Vec<TargetSelection>,
248 pub targets: Vec<TargetSelection>,
249 pub local_rebuild: bool,
250 pub allocator: Option<Allocator>,
251 pub control_flow_guard: bool,
252 pub ehcont_guard: bool,
253
254 pub dist_sign_folder: Option<PathBuf>,
256 pub dist_upload_addr: Option<String>,
257 pub dist_compression_formats: Option<Vec<String>>,
258 pub dist_compression_profile: String,
259 pub dist_include_mingw_linker: bool,
260 pub dist_vendor: bool,
261
262 pub backtrace: bool, pub low_priority: bool,
267 pub channel: String,
268 pub description: Option<String>,
269 pub verbose_tests: bool,
270 pub save_toolstates: Option<PathBuf>,
271 pub print_step_timings: bool,
272 pub print_step_rusage: bool,
273
274 pub musl_root: Option<PathBuf>,
276 pub prefix: Option<PathBuf>,
277 pub sysconfdir: Option<PathBuf>,
278 pub datadir: Option<PathBuf>,
279 pub docdir: Option<PathBuf>,
280 pub bindir: PathBuf,
281 pub libdir: Option<PathBuf>,
282 pub mandir: Option<PathBuf>,
283 pub codegen_tests: bool,
284 pub nodejs: Option<PathBuf>,
285 pub yarn: Option<PathBuf>,
286 pub gdb: Option<DebuggerPath>,
287 pub lldb: Option<DebuggerPath>,
288 pub python: Option<PathBuf>,
289 pub windows_rc: Option<PathBuf>,
290 pub reuse: Option<PathBuf>,
291 pub cargo_native_static: bool,
292 pub out: PathBuf,
293 pub rust_info: channel::GitInfo,
294
295 pub cargo_info: channel::GitInfo,
296 pub rust_analyzer_info: channel::GitInfo,
297 pub clippy_info: channel::GitInfo,
298 pub miri_info: channel::GitInfo,
299 pub rustfmt_info: channel::GitInfo,
300 pub enzyme_info: channel::GitInfo,
301 pub in_tree_llvm_info: channel::GitInfo,
302 pub in_tree_gcc_info: channel::GitInfo,
303
304 pub initial_cargo: PathBuf,
306 pub initial_rustc: PathBuf,
307 pub initial_rustdoc: PathBuf,
308 pub initial_cargo_clippy: Option<PathBuf>,
309 pub initial_sysroot: PathBuf,
310
311 pub external_rustfmt: Option<PathBuf>,
315
316 pub paths: Vec<PathBuf>,
319
320 pub compiletest_diff_tool: Option<String>,
322
323 pub compiletest_allow_stage0: bool,
329
330 pub tidy_extra_checks: Option<String>,
332 pub ci_env: CiEnv,
333
334 pub path_modification_cache: Arc<Mutex<HashMap<Vec<&'static str>, PathFreshness>>>,
336
337 pub skip_std_check_if_no_download_rustc: bool,
341
342 pub exec_ctx: ExecutionContext,
343
344 pub wasm_proc_macros: bool,
345}
346
347impl Config {
348 pub fn set_dry_run(&mut self, dry_run: DryRun) {
349 self.exec_ctx.set_dry_run(dry_run);
350 }
351
352 pub fn get_dry_run(&self) -> &DryRun {
353 self.exec_ctx.get_dry_run()
354 }
355
356 #[cfg_attr(
357 feature = "tracing",
358 instrument(target = "CONFIG_HANDLING", level = "trace", name = "Config::parse", skip_all)
359 )]
360 pub fn parse(flags: Flags) -> Config {
361 Self::parse_inner(flags, Self::get_toml)
362 }
363
364 #[cfg_attr(
365 feature = "tracing",
366 instrument(
367 target = "CONFIG_HANDLING",
368 level = "trace",
369 name = "Config::parse_inner",
370 skip_all
371 )
372 )]
373 pub(crate) fn parse_inner(
374 flags: Flags,
375 get_toml: impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>,
376 ) -> Config {
377 let Flags {
381 cmd: flags_cmd,
382 verbose: flags_verbose,
383 quiet: flags_quiet,
384 incremental: flags_incremental,
385 config: flags_config,
386 build_dir: flags_build_dir,
387 build: flags_build,
388 host: flags_host,
389 target: flags_target,
390 exclude: flags_exclude,
391 skip: flags_skip,
392 include_default_paths: flags_include_default_paths,
393 rustc_error_format: flags_rustc_error_format,
394 on_fail: flags_on_fail,
395 dry_run: flags_dry_run,
396 dump_bootstrap_shims: flags_dump_bootstrap_shims,
397 stage: flags_stage,
398 keep_stage: flags_keep_stage,
399 keep_stage_std: flags_keep_stage_std,
400 src: flags_src,
401 jobs: flags_jobs,
402 warnings: flags_warnings,
403 json_output: flags_json_output,
404 compile_time_deps: flags_compile_time_deps,
405 color: flags_color,
406 bypass_bootstrap_lock: flags_bypass_bootstrap_lock,
407 rust_profile_generate: flags_rust_profile_generate,
408 rust_profile_use: flags_rust_profile_use,
409 llvm_profile_use: flags_llvm_profile_use,
410 llvm_profile_generate: flags_llvm_profile_generate,
411 enable_bolt_settings: flags_enable_bolt_settings,
412 skip_stage0_validation: flags_skip_stage0_validation,
413 reproducible_artifact: flags_reproducible_artifact,
414 paths: flags_paths,
415 set: flags_set,
416 free_args: flags_free_args,
417 ci: flags_ci,
418 skip_std_check_if_no_download_rustc: flags_skip_std_check_if_no_download_rustc,
419 } = flags;
420
421 #[cfg(feature = "tracing")]
422 span!(
423 target: "CONFIG_HANDLING",
424 tracing::Level::TRACE,
425 "collecting paths and path exclusions",
426 "flags.paths" = ?flags_paths,
427 "flags.skip" = ?flags_skip,
428 "flags.exclude" = ?flags_exclude
429 );
430
431 if flags_cmd.no_doc() {
432 eprintln!(
433 "WARN: `x.py test --no-doc` is renamed to `--all-targets`. `--no-doc` will be removed in the near future. Additionally `--tests` is added which only executes unit and integration tests."
434 )
435 }
436
437 let mut exec_ctx = ExecutionContext::new(flags_verbose, flags_cmd.fail_fast());
439 exec_ctx.set_dry_run(if flags_dry_run { DryRun::UserSelected } else { DryRun::Disabled });
440
441 let default_src_dir = {
442 let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
443 manifest_dir.parent().unwrap().parent().unwrap().to_owned()
445 };
446 let src = if let Some(s) = compute_src_directory(flags_src, &exec_ctx) {
447 s
448 } else {
449 default_src_dir.clone()
450 };
451
452 #[cfg(test)]
453 {
454 if let Some(config_path) = flags_config.as_ref() {
455 assert!(
456 !config_path.starts_with(&src),
457 "Path {config_path:?} should not be inside or equal to src dir {src:?}"
458 );
459 } else {
460 panic!("During test the config should be explicitly added");
461 }
462 }
463
464 let (mut toml, toml_path) = load_toml_config(&src, flags_config, &get_toml);
466 postprocess_toml(&mut toml, &src, toml_path.clone(), &exec_ctx, &flags_set, &get_toml);
467 let TomlConfig {
468 change_id: toml_change_id,
469 build: toml_build,
470 install: toml_install,
471 llvm: toml_llvm,
472 gcc: toml_gcc,
473 rust: toml_rust,
474 target: toml_target,
475 dist: toml_dist,
476 pgo: toml_pgo,
477 profile: _,
478 include: _,
479 } = toml;
480
481 let Build {
484 description: build_description,
485 build: build_build,
486 host: build_host,
487 target: build_target,
488 build_dir: build_build_dir,
489 cargo: mut build_cargo,
490 rustc: mut build_rustc,
491 rustdoc: build_rustdoc,
492 rustfmt: build_rustfmt,
493 cargo_clippy: build_cargo_clippy,
494 docs: build_docs,
495 compiler_docs: build_compiler_docs,
496 library_docs_private_items: build_library_docs_private_items,
497 docs_minification: build_docs_minification,
498 submodules: build_submodules,
499 gdb: build_gdb,
500 lldb: build_lldb,
501 nodejs: build_nodejs,
502
503 yarn: build_yarn,
504 npm: build_npm,
505 python: build_python,
506 windows_rc: build_windows_rc,
507 reuse: build_reuse,
508 locked_deps: build_locked_deps,
509 vendor: build_vendor,
510 full_bootstrap: build_full_bootstrap,
511 bootstrap_cache_path: build_bootstrap_cache_path,
512 extended: build_extended,
513 tools: build_tools,
514 tool: build_tool,
515 verbose: build_verbose,
516 sanitizers: build_sanitizers,
517 profiler: build_profiler,
518 cargo_native_static: build_cargo_native_static,
519 low_priority: build_low_priority,
520 configure_args: _,
524 local_rebuild: build_local_rebuild,
525 print_step_timings: build_print_step_timings,
526 print_step_rusage: build_print_step_rusage,
527 check_stage: build_check_stage,
528 doc_stage: build_doc_stage,
529 build_stage: build_build_stage,
530 test_stage: build_test_stage,
531 install_stage: build_install_stage,
532 dist_stage: build_dist_stage,
533 bench_stage: build_bench_stage,
534 patch_binaries_for_nix: build_patch_binaries_for_nix,
535 record_failed_tests_path: build_record_failed_tests_path,
536 metrics: _,
538 android_ndk: build_android_ndk,
539 optimized_compiler_builtins: build_optimized_compiler_builtins,
540 jobs: build_jobs,
541 compiletest_diff_tool: build_compiletest_diff_tool,
542 tidy_extra_checks: build_tidy_extra_checks,
543 ccache: build_ccache,
544 exclude: build_exclude,
545 compiletest_allow_stage0: build_compiletest_allow_stage0,
546 sde: build_sde,
547 allocator: build_allocator,
548 } = toml_build.unwrap_or_default();
549
550 let Install {
551 prefix: install_prefix,
552 sysconfdir: install_sysconfdir,
553 docdir: install_docdir,
554 bindir: install_bindir,
555 libdir: install_libdir,
556 mandir: install_mandir,
557 datadir: install_datadir,
558 } = toml_install.unwrap_or_default();
559
560 let Rust {
561 optimize: rust_optimize,
562 debug: rust_debug,
563 codegen_units: rust_codegen_units,
564 codegen_units_std: rust_codegen_units_std,
565 rustc_debug_assertions: rust_rustc_debug_assertions,
566 std_debug_assertions: rust_std_debug_assertions,
567 tools_debug_assertions: rust_tools_debug_assertions,
568 overflow_checks: rust_overflow_checks,
569 overflow_checks_std: rust_overflow_checks_std,
570 debug_logging: rust_debug_logging,
571 debuginfo_level: rust_debuginfo_level,
572 debuginfo_level_rustc: rust_debuginfo_level_rustc,
573 debuginfo_level_std: rust_debuginfo_level_std,
574 debuginfo_level_tools: rust_debuginfo_level_tools,
575 debuginfo_level_tests: rust_debuginfo_level_tests,
576 compress_debuginfo: rust_compress_debuginfo,
577 backtrace: rust_backtrace,
578 incremental: rust_incremental,
579 randomize_layout: rust_randomize_layout,
580 default_linker: rust_default_linker,
581 channel: rust_channel,
582 musl_root: rust_musl_root,
583 rpath: rust_rpath,
584 verbose_tests: rust_verbose_tests,
585 optimize_tests: rust_optimize_tests,
586 codegen_tests: rust_codegen_tests,
587 omit_git_hash: rust_omit_git_hash,
588 dist_src: rust_dist_src,
589 save_toolstates: rust_save_toolstates,
590 codegen_backends: rust_codegen_backends,
591 lld: rust_lld_enabled,
592 llvm_tools: rust_llvm_tools,
593 llvm_bitcode_linker: rust_llvm_bitcode_linker,
594 deny_warnings: rust_deny_warnings,
595 backtrace_on_ice: rust_backtrace_on_ice,
596 verify_llvm_ir: rust_verify_llvm_ir,
597 thin_lto_import_instr_limit: rust_thin_lto_import_instr_limit,
598 parallel_frontend_threads: rust_parallel_frontend_threads,
599 remap_debuginfo: rust_remap_debuginfo,
600 jemalloc: rust_jemalloc,
601 test_compare_mode: rust_test_compare_mode,
602 llvm_libunwind: rust_llvm_libunwind,
603 control_flow_guard: rust_control_flow_guard,
604 ehcont_guard: rust_ehcont_guard,
605 new_symbol_mangling: rust_new_symbol_mangling,
606 annotate_moves_size_limit: rust_annotate_moves_size_limit,
607 profile_generate: rust_profile_generate,
608 profile_use: rust_profile_use,
609 download_rustc: rust_download_rustc,
610 lto: rust_lto,
611 validate_mir_opts: rust_validate_mir_opts,
612 frame_pointers: rust_frame_pointers,
613 stack_protector: rust_stack_protector,
614 strip: rust_strip,
615 bootstrap_override_lld: rust_bootstrap_override_lld,
616 std_features: rust_std_features,
617 break_on_ice: rust_break_on_ice,
618 rustflags: rust_rustflags,
619 stdlib_semver_baseline: rust_stdlib_semver_baseline,
620 wasm_proc_macros,
621 } = toml_rust.unwrap_or_default();
622
623 let Llvm {
624 optimize: llvm_optimize,
625 thin_lto: llvm_thin_lto,
626 release_debuginfo: llvm_release_debuginfo,
627 assertions: llvm_assertions,
628 tests: llvm_tests,
629 enzyme: llvm_enzyme,
630 plugins: llvm_plugin,
631 static_libstdcpp: llvm_static_libstdcpp,
632 libzstd: llvm_libzstd,
633 ninja: llvm_ninja,
634 targets: llvm_targets,
635 experimental_targets: llvm_experimental_targets,
636 link_jobs: llvm_link_jobs,
637 link_shared: llvm_link_shared,
638 version_suffix: llvm_version_suffix,
639 clang_cl: llvm_clang_cl,
640 cflags: llvm_cflags,
641 cxxflags: llvm_cxxflags,
642 ldflags: llvm_ldflags,
643 use_libcxx: llvm_use_libcxx,
644 use_linker: llvm_use_linker,
645 allow_old_toolchain: llvm_allow_old_toolchain,
646 offload: llvm_offload,
647 offload_clang_dir,
648 polly: llvm_polly,
649 clang: llvm_clang,
650 enable_warnings: llvm_enable_warnings,
651 download_ci_llvm: llvm_download_ci_llvm,
652 build_config: llvm_build_config,
653 } = toml_llvm.unwrap_or_default();
654
655 let Dist {
656 sign_folder: dist_sign_folder,
657 upload_addr: dist_upload_addr,
658 src_tarball: dist_src_tarball,
659 compression_formats: dist_compression_formats,
660 compression_profile: dist_compression_profile,
661 include_mingw_linker: dist_include_mingw_linker,
662 vendor: dist_vendor,
663 } = toml_dist.unwrap_or_default();
664
665 let Gcc {
666 download_ci_gcc: gcc_download_ci_gcc,
667 libgccjit_libs_dir: gcc_libgccjit_libs_dir,
668 } = toml_gcc.unwrap_or_default();
669
670 let Pgo {
671 rustc: pgo_rustc,
672 rustdoc: pgo_rustdoc,
673 cargo: pgo_cargo,
674 clippy: pgo_clippy,
675 llvm: pgo_llvm,
676 } = toml_pgo.unwrap_or_default();
677
678 if flags_rust_profile_use.is_some() || flags_rust_profile_generate.is_some() {
680 eprintln!(
681 "WARNING: the `--rust-profile-generate` and `--rust-profile-use` flags have been deprecated. Configure PGO through the config file instead, in the [pgo.rustc] section."
682 );
683 }
684 if rust_profile_use.is_some() || rust_profile_generate.is_some() {
685 eprintln!(
686 "WARNING: the `rust.profile-generate` and `rust.profile-use` config options have been deprecated. Configure PGO through the config file instead, in the [pgo.rustc] section."
687 );
688 }
689 if flags_llvm_profile_use.is_some() || flags_llvm_profile_generate {
690 eprintln!(
691 "WARNING: the `--llvm-profile-generate` and `--llvm-profile-use` flags have been deprecated. Configure PGO through the config file instead, in the [pgo.llvm] section."
692 );
693 }
694
695 let mut pgo_rustc = pgo_rustc.unwrap_or_default();
696 pgo_rustc.use_profile =
697 flags_rust_profile_use.or(pgo_rustc.use_profile).or(rust_profile_use);
698 pgo_rustc.generate_profile =
699 flags_rust_profile_generate.or(pgo_rustc.generate_profile).or(rust_profile_generate);
700 if pgo_rustc.use_profile.is_some() && pgo_rustc.generate_profile.is_some() {
701 panic!("Cannot use and generate rust PGO profiles at the same time");
702 }
703
704 let pgo_llvm = pgo_llvm.unwrap_or_default();
705 let pgo_llvm = LlvmPgoConfig {
706 use_profile: flags_llvm_profile_use.or(pgo_llvm.use_profile),
707 generate_profile: if flags_llvm_profile_generate {
708 Some(if let Ok(llvm_profile_dir) = std::env::var("LLVM_PROFILE_DIR") {
709 LlvmPgoGenerationMode::Directory(PathBuf::from(llvm_profile_dir))
710 } else {
711 LlvmPgoGenerationMode::Implicit
712 })
713 } else {
714 pgo_llvm.generate_profile.map(LlvmPgoGenerationMode::Directory)
715 },
716 };
717 if pgo_llvm.use_profile.is_some() && pgo_llvm.generate_profile.is_some() {
718 panic!("Cannot use and generate LLVM PGO profiles at the same time");
719 }
720
721 let init_pgo = |pgo: Option<PgoConfig>, name: &str| -> PgoConfig {
722 let pgo_config = pgo.unwrap_or_default();
723 if pgo_config.use_profile.is_some() && pgo_config.generate_profile.is_some() {
724 panic!("Cannot use and generate {name} PGO profiles at the same time");
725 }
726 pgo_config
727 };
728
729 let pgo_rustdoc = init_pgo(pgo_rustdoc, "rustdoc");
730 let pgo_cargo = init_pgo(pgo_cargo, "cargo");
731 let pgo_clippy = init_pgo(pgo_clippy, "clippy");
732
733 let bootstrap_override_lld = rust_bootstrap_override_lld.unwrap_or_default();
734
735 if rust_optimize.as_ref().is_some_and(|v| matches!(v, RustOptimize::Bool(false))) {
736 eprintln!(
737 "WARNING: setting `optimize` to `false` is known to cause errors and \
738 should be considered unsupported. Refer to `bootstrap.example.toml` \
739 for more details."
740 );
741 }
742
743 exec_ctx.set_verbosity(cmp::max(build_verbose.unwrap_or_default() as u8, flags_verbose));
746
747 let stage0_metadata = build_helper::stage0_parser::parse_stage0_file();
748 let path_modification_cache = Arc::new(Mutex::new(HashMap::new()));
749
750 let host_target = flags_build
751 .or(build_build)
752 .map(|build| TargetSelection::from_user(&build))
753 .unwrap_or_else(get_host_target);
754 let hosts = flags_host
755 .map(|TargetSelectionList(hosts)| hosts)
756 .or_else(|| {
757 build_host.map(|h| h.iter().map(|t| TargetSelection::from_user(t)).collect())
758 })
759 .unwrap_or_else(|| vec![host_target]);
760
761 let llvm_assertions = llvm_assertions.unwrap_or(false);
762 let mut target_config = HashMap::new();
763 let mut channel = "dev".to_string();
764
765 let out = flags_build_dir.or_else(|| build_build_dir.map(PathBuf::from));
766 let out = if cfg!(test) {
767 out.expect("--build-dir has to be specified in tests")
768 } else {
769 out.unwrap_or_else(|| PathBuf::from("build"))
770 };
771
772 let mut out = if !out.is_absolute() {
775 absolute(&out).expect("can't make empty path absolute")
777 } else {
778 out
779 };
780
781 let default_stage0_rustc_path = |dir: &Path| {
782 dir.join(host_target).join("stage0").join("bin").join(exe("rustc", host_target))
783 };
784
785 if cfg!(test) {
786 build_rustc = build_rustc.take().or(std::env::var_os("RUSTC").map(|p| p.into()));
792 build_cargo = build_cargo.take().or(std::env::var_os("CARGO").map(|p| p.into()));
793
794 let is_test_outside_x = std::env::var("CARGO_TARGET_DIR").is_err();
800 if is_test_outside_x && build_rustc.is_none() {
801 let stage0_rustc = default_stage0_rustc_path(&default_src_dir.join("build"));
802 assert!(
803 stage0_rustc.exists(),
804 "Trying to run cargo test without having a stage0 rustc available in {}",
805 stage0_rustc.display()
806 );
807 build_rustc = Some(stage0_rustc);
808 }
809 }
810
811 if !flags_skip_stage0_validation {
812 if let Some(rustc) = &build_rustc {
813 check_stage0_version(rustc, "rustc", &src, &exec_ctx);
814 }
815 if let Some(cargo) = &build_cargo {
816 check_stage0_version(cargo, "cargo", &src, &exec_ctx);
817 }
818 }
819
820 if build_cargo_clippy.is_some() && build_rustc.is_none() {
821 println!(
822 "WARNING: Using `build.cargo-clippy` without `build.rustc` usually fails due to toolchain conflict."
823 );
824 }
825
826 let ci_env = match flags_ci {
827 Some(true) => CiEnv::GitHubActions,
828 Some(false) => CiEnv::None,
829 None => CiEnv::current(),
830 };
831 let dwn_ctx = DownloadContext {
832 path_modification_cache: path_modification_cache.clone(),
833 src: &src,
834 submodules: &build_submodules,
835 host_target,
836 patch_binaries_for_nix: build_patch_binaries_for_nix,
837 exec_ctx: &exec_ctx,
838 stage0_metadata: &stage0_metadata,
839 llvm_assertions,
840 bootstrap_cache_path: &build_bootstrap_cache_path,
841 ci_env,
842 };
843
844 let initial_rustc = build_rustc.unwrap_or_else(|| {
845 download_beta_toolchain(&dwn_ctx, &out);
846 default_stage0_rustc_path(&out)
847 });
848
849 let initial_rustdoc = build_rustdoc
850 .unwrap_or_else(|| initial_rustc.with_file_name(exe("rustdoc", host_target)));
851
852 let initial_sysroot = t!(PathBuf::from_str(
853 command(&initial_rustc)
854 .args(["--print", "sysroot"])
855 .run_in_dry_run()
856 .run_capture_stdout(&exec_ctx)
857 .stdout()
858 .trim()
859 ));
860
861 let initial_cargo = build_cargo.unwrap_or_else(|| {
862 download_beta_toolchain(&dwn_ctx, &out);
863 initial_sysroot.join("bin").join(exe("cargo", host_target))
864 });
865
866 if exec_ctx.dry_run() {
868 out = out.join("tmp-dry-run");
869 fs::create_dir_all(&out).expect("Failed to create dry-run directory");
870 }
871
872 let file_content = t!(fs::read_to_string(src.join("src/ci/channel")));
873 let ci_channel = file_content.trim_end();
874
875 let is_user_configured_rust_channel = match rust_channel {
876 Some(channel_) if channel_ == "auto-detect" => {
877 channel = ci_channel.into();
878 true
879 }
880 Some(channel_) => {
881 channel = channel_;
882 true
883 }
884 None => false,
885 };
886
887 let omit_git_hash = rust_omit_git_hash.unwrap_or(channel == "dev");
888
889 let rust_info = git_info(&exec_ctx, omit_git_hash, &src);
890
891 if !is_user_configured_rust_channel && rust_info.is_from_tarball() {
892 channel = ci_channel.into();
893 }
894
895 let debug_assertions_requested = matches!(rust_rustc_debug_assertions, Some(true))
906 || (matches!(rust_debug, Some(true))
907 && !matches!(rust_rustc_debug_assertions, Some(false)));
908
909 if debug_assertions_requested
910 && let Some(ref opt) = rust_download_rustc
911 && opt.is_string_or_true()
912 {
913 eprintln!(
914 "WARN: currently no CI rustc builds have rustc debug assertions \
915 enabled. Please either set `rust.debug-assertions` to `false` if you \
916 want to use download CI rustc or set `rust.download-rustc` to `false`."
917 );
918 }
919
920 let mut download_rustc_commit =
921 download_ci_rustc_commit(&dwn_ctx, &rust_info, rust_download_rustc, llvm_assertions);
922
923 if debug_assertions_requested && download_rustc_commit.is_some() {
924 eprintln!(
925 "WARN: `rust.debug-assertions = true` will prevent downloading CI rustc as alt CI \
926 rustc is not currently built with debug assertions."
927 );
928 download_rustc_commit = None;
930 }
931
932 if let Some(commit) = &download_rustc_commit
936 && is_user_configured_rust_channel
937 {
938 println!(
939 "WARNING: `rust.download-rustc` is enabled. The `rust.channel` option will be overridden by the CI rustc's channel."
940 );
941
942 channel =
943 read_file_by_commit(&dwn_ctx, &rust_info, Path::new("src/ci/channel"), commit)
944 .trim()
945 .to_owned();
946 }
947
948 if build_npm.is_some() {
949 println!(
950 "WARNING: `build.npm` set in bootstrap.toml, this option no longer has any effect. . Use `build.yarn` instead to provide a path to a `yarn` binary."
951 );
952 }
953
954 let mut lld_enabled = rust_lld_enabled.unwrap_or(false);
955
956 let mut targets_with_user_linker_override = HashSet::new();
958
959 if let Some(t) = toml_target {
960 for (triple, cfg) in t {
961 let TomlTarget {
962 cc: target_cc,
963 cxx: target_cxx,
964 ar: target_ar,
965 ranlib: target_ranlib,
966 default_linker: target_default_linker,
967 default_linker_linux_override: target_default_linker_linux_override,
968 linker: target_linker,
969 split_debuginfo: target_split_debuginfo,
970 llvm_config: target_llvm_config,
971 llvm_has_rust_patches: target_llvm_has_rust_patches,
972 llvm_filecheck: target_llvm_filecheck,
973 llvm_libunwind: target_llvm_libunwind,
974 sanitizers: target_sanitizers,
975 profiler: target_profiler,
976 rpath: target_rpath,
977 rustflags: target_rustflags,
978 crt_static: target_crt_static,
979 musl_root: target_musl_root,
980 musl_libdir: target_musl_libdir,
981 wasi_root: target_wasi_root,
982 qemu_rootfs: target_qemu_rootfs,
983 no_std: target_no_std,
984 codegen_backends: target_codegen_backends,
985 runner: target_runner,
986 optimized_compiler_builtins: target_optimized_compiler_builtins,
987 allocator: target_allocator,
988 jemalloc: target_jemalloc,
989 } = cfg;
990
991 let mut target = Target::from_triple(&triple);
992
993 if target_default_linker_linux_override.is_some() {
994 targets_with_user_linker_override.insert(triple.clone());
995 }
996
997 let default_linker_linux_override = match target_default_linker_linux_override {
998 Some(DefaultLinuxLinkerOverride::SelfContainedLldCc) => {
999 if rust_default_linker.is_some() {
1000 panic!(
1001 "cannot set both `default-linker` and `default-linker-linux` for target `{triple}`"
1002 );
1003 }
1004 if !triple.contains("linux-gnu") {
1005 panic!(
1006 "`default-linker-linux` can only be set for Linux GNU targets, not for `{triple}`"
1007 );
1008 }
1009 if !lld_enabled {
1010 panic!(
1011 "Trying to override the default Linux linker for `{triple}` to be self-contained LLD, but LLD is not being built. Enable it with rust.lld = true."
1012 );
1013 }
1014 DefaultLinuxLinkerOverride::SelfContainedLldCc
1015 }
1016 Some(DefaultLinuxLinkerOverride::Off) => DefaultLinuxLinkerOverride::Off,
1017 None => DefaultLinuxLinkerOverride::default(),
1018 };
1019
1020 if let Some(ref s) = target_llvm_config {
1021 if download_rustc_commit.is_some() && triple == *host_target.triple {
1022 panic!(
1023 "setting llvm_config for the host is incompatible with download-rustc"
1024 );
1025 }
1026 target.llvm_config = Some(src.join(s));
1027 }
1028 if let Some(patches) = target_llvm_has_rust_patches {
1029 assert!(
1030 build_submodules == Some(false) || target_llvm_config.is_some(),
1031 "use of `llvm-has-rust-patches` is restricted to cases where either submodules are disabled or llvm-config been provided"
1032 );
1033 target.llvm_has_rust_patches = Some(patches);
1034 }
1035 if let Some(ref s) = target_llvm_filecheck {
1036 target.llvm_filecheck = Some(src.join(s));
1037 }
1038 target.llvm_libunwind = target_llvm_libunwind.as_ref().map(|v| {
1039 v.parse().unwrap_or_else(|_| {
1040 panic!("failed to parse target.{triple}.llvm-libunwind")
1041 })
1042 });
1043 if let Some(s) = target_no_std {
1044 target.no_std = s;
1045 }
1046 target.cc = target_cc.map(PathBuf::from);
1047 target.cxx = target_cxx.map(PathBuf::from);
1048 target.ar = target_ar.map(PathBuf::from);
1049 target.ranlib = target_ranlib.map(PathBuf::from);
1050 target.linker = target_linker.map(PathBuf::from);
1051 target.crt_static = target_crt_static;
1052 target.default_linker = target_default_linker;
1053 target.default_linker_linux_override = default_linker_linux_override;
1054 target.musl_root = target_musl_root.map(PathBuf::from);
1055 target.musl_libdir = target_musl_libdir.map(PathBuf::from);
1056 target.wasi_root = target_wasi_root.map(PathBuf::from);
1057 target.qemu_rootfs = target_qemu_rootfs.map(PathBuf::from);
1058 target.runner = target_runner;
1059 target.sanitizers = target_sanitizers;
1060 target.profiler = target_profiler;
1061 target.rpath = target_rpath;
1062 target.rustflags = target_rustflags.unwrap_or_default();
1063 target.optimized_compiler_builtins = target_optimized_compiler_builtins;
1064 target.allocator = reconcile_jemalloc(
1065 target_jemalloc,
1066 target_allocator,
1067 &format!("target.{triple}"),
1068 &format!("target.{triple}"),
1069 );
1070 if let Some(backends) = target_codegen_backends {
1071 target.codegen_backends =
1072 Some(parse_codegen_backends(backends, &format!("target.{triple}")))
1073 }
1074
1075 target.split_debuginfo = target_split_debuginfo.as_ref().map(|v| {
1076 v.parse().unwrap_or_else(|_| {
1077 panic!("invalid value for target.{triple}.split-debuginfo")
1078 })
1079 });
1080
1081 target_config.insert(TargetSelection::from_user(&triple), target);
1082 }
1083 }
1084
1085 let llvm_from_ci = parse_download_ci_llvm(
1086 &dwn_ctx,
1087 &rust_info,
1088 &download_rustc_commit,
1089 llvm_download_ci_llvm,
1090 llvm_assertions,
1091 );
1092
1093 let llvm_ci_mode =
1096 if llvm_from_ci { LlvmCiMode::DownloadFromCi } else { LlvmCiMode::BuildLocally };
1097
1098 let is_host_system_llvm =
1099 target_config.get(&host_target).and_then(|c| c.llvm_config.as_ref()).is_some();
1100
1101 if llvm_from_ci {
1102 let warn = |option: &str| {
1103 println!(
1104 "WARNING: `{option}` will only be used on `compiler/rustc_llvm` build, not for the LLVM build."
1105 );
1106 println!(
1107 "HELP: To use `{option}` for LLVM builds, set `download-ci-llvm` option to false."
1108 );
1109 };
1110
1111 if llvm_static_libstdcpp.is_some() {
1112 warn("static-libstdcpp");
1113 }
1114
1115 if llvm_link_shared.is_some() {
1116 warn("link-shared");
1117 }
1118
1119 if llvm_libzstd.is_some() {
1125 println!(
1126 "WARNING: when using `download-ci-llvm`, the local `llvm.libzstd` option, \
1127 like almost all `llvm.*` options, will be ignored and set by the LLVM CI \
1128 artifacts builder config."
1129 );
1130 println!(
1131 "HELP: To use `llvm.libzstd` for LLVM/LLD builds, set `download-ci-llvm` option to false."
1132 );
1133 }
1134
1135 if let Some(target) = target_config.get(&host_target) {
1136 check_ci_llvm!(target.llvm_config);
1137 check_ci_llvm!(target.llvm_filecheck);
1138 }
1139 }
1140
1141 for (target, linker_override) in default_linux_linker_overrides() {
1142 if targets_with_user_linker_override.contains(&target) {
1144 continue;
1145 }
1146
1147 if !hosts.contains(&TargetSelection::from_user(&target)) {
1153 continue;
1154 }
1155
1156 let default_linux_linker_override = match linker_override {
1157 DefaultLinuxLinkerOverride::Off => continue,
1158 DefaultLinuxLinkerOverride::SelfContainedLldCc => {
1159 match rust_lld_enabled {
1162 None if !is_host_system_llvm => {
1165 lld_enabled = true;
1166 Some(DefaultLinuxLinkerOverride::SelfContainedLldCc)
1167 }
1168 None => None,
1169 Some(true) => Some(DefaultLinuxLinkerOverride::SelfContainedLldCc),
1171 Some(false) => None,
1174 }
1175 }
1176 };
1177 if let Some(linker_override) = default_linux_linker_override {
1178 target_config
1179 .entry(TargetSelection::from_user(&target))
1180 .or_default()
1181 .default_linker_linux_override = linker_override;
1182 }
1183 }
1184
1185 if matches!(bootstrap_override_lld, BootstrapOverrideLld::SelfContained)
1186 && !lld_enabled
1187 && flags_stage.unwrap_or(0) > 0
1188 {
1189 panic!(
1190 "Trying to use self-contained lld as a linker, but LLD is not being added to the sysroot. Enable it with rust.lld = true."
1191 );
1192 }
1193
1194 if lld_enabled && is_host_system_llvm {
1195 panic!("Cannot enable LLD with `rust.lld = true` when using external llvm-config.");
1196 }
1197
1198 let download_rustc = download_rustc_commit.is_some();
1199
1200 let stage = match flags_cmd {
1201 Subcommand::Check { .. } | Subcommand::Clippy { .. } | Subcommand::Fix { .. } => {
1202 flags_stage.or(build_check_stage).unwrap_or(1)
1203 }
1204 Subcommand::Doc { .. } => {
1206 flags_stage.or(build_doc_stage).unwrap_or(if download_rustc { 2 } else { 1 })
1207 }
1208 Subcommand::Build { .. } => {
1209 flags_stage.or(build_build_stage).unwrap_or(if download_rustc { 2 } else { 1 })
1210 }
1211 Subcommand::Test { .. } | Subcommand::Miri { .. } => {
1212 flags_stage.or(build_test_stage).unwrap_or(if download_rustc { 2 } else { 1 })
1213 }
1214 Subcommand::Bench { .. } => flags_stage.or(build_bench_stage).unwrap_or(2),
1215 Subcommand::Dist => flags_stage.or(build_dist_stage).unwrap_or(2),
1216 Subcommand::Install => flags_stage.or(build_install_stage).unwrap_or(2),
1217 Subcommand::Perf { .. } => flags_stage.unwrap_or(1),
1218 Subcommand::Clean { .. }
1221 | Subcommand::Run { .. }
1222 | Subcommand::Setup { .. }
1223 | Subcommand::Format { .. }
1224 | Subcommand::Vendor { .. } => flags_stage.unwrap_or(0),
1225 };
1226
1227 let local_rebuild = build_local_rebuild.unwrap_or(false);
1228
1229 let check_stage0 = |kind: &str| {
1230 if local_rebuild {
1231 eprintln!("WARNING: running {kind} in stage 0. This might not work as expected.");
1232 } else {
1233 eprintln!(
1234 "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."
1235 );
1236 helpers::exit_process(1);
1237 }
1238 };
1239
1240 match (stage, &flags_cmd) {
1242 (0, Subcommand::Build { .. }) => {
1243 check_stage0("build");
1244 }
1245 (0, Subcommand::Check { .. }) => {
1246 check_stage0("check");
1247 }
1248 (0, Subcommand::Doc { .. }) => {
1249 check_stage0("doc");
1250 }
1251 (0, Subcommand::Clippy { .. }) => {
1252 check_stage0("clippy");
1253 }
1254 (0, Subcommand::Dist) => {
1255 check_stage0("dist");
1256 }
1257 (0, Subcommand::Install) => {
1258 check_stage0("install");
1259 }
1260 (0, Subcommand::Test { .. }) if build_compiletest_allow_stage0 != Some(true) => {
1261 eprintln!(
1262 "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`."
1263 );
1264 helpers::exit_process(1);
1265 }
1266 _ => {}
1267 }
1268
1269 if flags_compile_time_deps && !matches!(flags_cmd, Subcommand::Check { .. }) {
1270 eprintln!("ERROR: Can't use --compile-time-deps with any subcommand other than check.");
1271 helpers::exit_process(1);
1272 }
1273
1274 if matches!(flags_cmd, Subcommand::Fix { .. }) {
1275 eprintln!(
1276 "WARNING: `x fix` is provided on a best-effort basis and does not support all `cargo fix` options correctly."
1277 );
1278 }
1279
1280 if cfg!(not(test)) && flags_stage.is_none() && ci_env.is_running_in_ci() {
1282 match flags_cmd {
1283 Subcommand::Test { .. }
1284 | Subcommand::Miri { .. }
1285 | Subcommand::Doc { .. }
1286 | Subcommand::Build { .. }
1287 | Subcommand::Bench { .. }
1288 | Subcommand::Dist
1289 | Subcommand::Install => {
1290 assert_eq!(
1291 stage, 2,
1292 "\
1293x.py was run under CI with an implicit `--stage {stage}`. This is probably wrong and you want stage 2.
1294NOTE: Please add `--stage 2` to your command line, or if you're sure you want to run stage {stage} then add `--stage {stage}` explicitly"
1295 );
1296 }
1297 Subcommand::Clean { .. }
1298 | Subcommand::Check { .. }
1299 | Subcommand::Clippy { .. }
1300 | Subcommand::Fix { .. }
1301 | Subcommand::Run { .. }
1302 | Subcommand::Setup { .. }
1303 | Subcommand::Format { .. }
1304 | Subcommand::Vendor { .. }
1305 | Subcommand::Perf { .. } => {}
1306 }
1307 }
1308
1309 let with_defaults = |debuginfo_level_specific: Option<_>| {
1310 debuginfo_level_specific.or(rust_debuginfo_level).unwrap_or(
1311 if rust_debug == Some(true) {
1312 DebuginfoLevel::Limited
1313 } else {
1314 DebuginfoLevel::None
1315 },
1316 )
1317 };
1318
1319 let ccache = match build_ccache {
1320 Some(StringOrBool::String(s)) => Some(s),
1321 Some(StringOrBool::Bool(true)) => Some("ccache".to_string()),
1322 _ => None,
1323 };
1324
1325 let explicit_stage_from_config = build_test_stage.is_some()
1326 || build_build_stage.is_some()
1327 || build_doc_stage.is_some()
1328 || build_dist_stage.is_some()
1329 || build_install_stage.is_some()
1330 || build_check_stage.is_some()
1331 || build_bench_stage.is_some();
1332
1333 let deny_warnings = match flags_warnings {
1334 Warnings::Deny => true,
1335 Warnings::Warn => false,
1336 Warnings::Default => rust_deny_warnings.unwrap_or(true),
1337 };
1338
1339 let gcc_ci_mode = match gcc_download_ci_gcc {
1340 Some(value) => match value {
1341 true => GccCiMode::DownloadFromCi,
1342 false => GccCiMode::BuildLocally,
1343 },
1344 None => GccCiMode::default(),
1345 };
1346
1347 let targets = flags_target
1348 .map(|TargetSelectionList(targets)| targets)
1349 .or_else(|| {
1350 build_target.map(|t| t.iter().map(|t| TargetSelection::from_user(t)).collect())
1351 })
1352 .unwrap_or_else(|| hosts.clone());
1353
1354 #[allow(clippy::map_identity)]
1355 let skip = flags_skip
1356 .into_iter()
1357 .chain(flags_exclude)
1358 .chain(build_exclude.unwrap_or_default())
1359 .map(|p| {
1360 #[cfg(windows)]
1363 {
1364 PathBuf::from(p.to_string_lossy().replace('/', "\\"))
1365 }
1366 #[cfg(not(windows))]
1367 {
1368 p
1369 }
1370 })
1371 .collect();
1372
1373 let cargo_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/cargo"));
1374 let clippy_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/clippy"));
1375 let in_tree_gcc_info = git_info(&exec_ctx, false, &src.join("src/gcc"));
1376 let in_tree_llvm_info = git_info(&exec_ctx, false, &src.join("src/llvm-project"));
1377 let enzyme_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/enzyme"));
1378 let miri_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/miri"));
1379 let rust_analyzer_info =
1380 git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/rust-analyzer"));
1381 let rustfmt_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/rustfmt"));
1382
1383 let optimized_compiler_builtins =
1384 build_optimized_compiler_builtins.unwrap_or(if channel == "dev" {
1385 CompilerBuiltins::BuildRustOnly
1386 } else {
1387 CompilerBuiltins::BuildLLVMFuncs
1388 });
1389 let vendor = build_vendor.unwrap_or(
1390 rust_info.is_from_tarball()
1391 && src.join("vendor").exists()
1392 && src.join(".cargo/config.toml").exists(),
1393 );
1394 let verbose_tests = rust_verbose_tests.unwrap_or(exec_ctx.is_verbose());
1395
1396 let record_failed_tests_path =
1397 out.join(build_record_failed_tests_path.unwrap_or_else(|| "failed-tests".to_string()));
1398
1399 let paths = {
1400 let mut paths = Vec::new();
1401 if flags_cmd.rerun() {
1402 paths = collect_previously_failed_tests(&record_failed_tests_path);
1403 } else {
1404 paths.extend(flags_paths);
1405 }
1406 paths
1407 };
1408
1409 let llvm_link_shared =
1413 llvm_link_shared.or((!llvm_from_ci && llvm_thin_lto.unwrap_or(false)).then_some(true));
1414
1415 Config {
1416 allocator: reconcile_jemalloc(rust_jemalloc, build_allocator, "rust", "build"),
1418 android_ndk: build_android_ndk,
1419 backtrace: rust_backtrace.unwrap_or(true),
1420 backtrace_on_ice: rust_backtrace_on_ice.unwrap_or(false),
1421 bindir: install_bindir.map(PathBuf::from).unwrap_or("bin".into()),
1422 bootstrap_cache_path: build_bootstrap_cache_path,
1423 bootstrap_override_lld,
1424 bypass_bootstrap_lock: flags_bypass_bootstrap_lock,
1425 cargo_info,
1426 cargo_native_static: build_cargo_native_static.unwrap_or(false),
1427 cargo_pgo: pgo_cargo,
1428 ccache,
1429 change_id: toml_change_id.inner,
1430 channel,
1431 ci_env,
1432 clippy_info,
1433 clippy_pgo: pgo_clippy,
1434 cmd: flags_cmd,
1435 codegen_tests: rust_codegen_tests.unwrap_or(true),
1436 color: flags_color,
1437 compile_time_deps: flags_compile_time_deps,
1438 compiler_docs: build_compiler_docs.unwrap_or(false),
1439 compiletest_allow_stage0: build_compiletest_allow_stage0.unwrap_or(false),
1440 compiletest_diff_tool: build_compiletest_diff_tool,
1441 config: toml_path,
1442 control_flow_guard: rust_control_flow_guard.unwrap_or(false),
1443 datadir: install_datadir.map(PathBuf::from),
1444 deny_warnings,
1445 description: build_description,
1446 dist_compression_formats,
1447 dist_compression_profile: dist_compression_profile.unwrap_or("fast".into()),
1448 dist_include_mingw_linker: dist_include_mingw_linker.unwrap_or(true),
1449 dist_sign_folder: dist_sign_folder.map(PathBuf::from),
1450 dist_upload_addr,
1451 dist_vendor: dist_vendor.unwrap_or_else(|| {
1452 rust_info.is_managed_git_subrepository() || rust_info.is_from_tarball()
1454 }),
1455 docdir: install_docdir.map(PathBuf::from),
1456 docs: build_docs.unwrap_or(true),
1457 docs_minification: build_docs_minification.unwrap_or(true),
1458 download_rustc_commit,
1459 dump_bootstrap_shims: flags_dump_bootstrap_shims,
1460 ehcont_guard: rust_ehcont_guard.unwrap_or(false),
1461 enable_bolt_settings: flags_enable_bolt_settings,
1462 enzyme_info,
1463 exec_ctx,
1464 explicit_stage_from_cli: flags_stage.is_some(),
1465 explicit_stage_from_config,
1466 extended: build_extended.unwrap_or(false),
1467 external_rustfmt: build_rustfmt,
1468 free_args: flags_free_args,
1469 full_bootstrap: build_full_bootstrap.unwrap_or(false),
1470 gcc_ci_mode,
1471 gdb: build_gdb,
1472 host_target,
1473 hosts,
1474 in_tree_gcc_info,
1475 in_tree_llvm_info,
1476 include_default_paths: flags_include_default_paths,
1477 incremental: flags_incremental || rust_incremental == Some(true),
1478 initial_cargo,
1479 initial_cargo_clippy: build_cargo_clippy,
1480 initial_rustc,
1481 initial_rustdoc,
1482 initial_sysroot,
1483 jobs: Some(threads_from_config(flags_jobs.or(build_jobs).unwrap_or(0))),
1484 json_output: flags_json_output,
1485 keep_stage: flags_keep_stage,
1486 keep_stage_std: flags_keep_stage_std,
1487 libdir: install_libdir.map(PathBuf::from),
1488 libgccjit_libs_dir: gcc_libgccjit_libs_dir,
1489 library_docs_private_items: build_library_docs_private_items.unwrap_or(false),
1490 lld_enabled,
1491 lldb: build_lldb,
1492 llvm_allow_old_toolchain: llvm_allow_old_toolchain.unwrap_or(false),
1493 llvm_assertions,
1494 llvm_bitcode_linker_enabled: rust_llvm_bitcode_linker.unwrap_or(false),
1495 llvm_build_config: llvm_build_config.clone().unwrap_or(Default::default()),
1496 llvm_cflags,
1497 llvm_ci_mode,
1498 llvm_clang: llvm_clang.unwrap_or(false),
1499 llvm_clang_cl,
1500 llvm_cxxflags,
1501 llvm_enable_warnings: llvm_enable_warnings.unwrap_or(false),
1502 llvm_enzyme: llvm_enzyme.unwrap_or(false),
1503 llvm_experimental_targets,
1504 llvm_ldflags,
1505 llvm_libunwind_default: rust_llvm_libunwind
1506 .map(|v| v.parse().expect("failed to parse rust.llvm-libunwind")),
1507 llvm_libzstd: llvm_libzstd.unwrap_or(false),
1508 llvm_link_jobs,
1509 llvm_link_shared,
1510 llvm_offload: llvm_offload.unwrap_or(false),
1511 llvm_optimize: llvm_optimize.unwrap_or(true),
1512 llvm_pgo: pgo_llvm,
1513 llvm_plugins: llvm_plugin.unwrap_or(false),
1514 llvm_polly: llvm_polly.unwrap_or(false),
1515 llvm_release_debuginfo: llvm_release_debuginfo.unwrap_or(false),
1516 llvm_static_stdcpp: llvm_static_libstdcpp.unwrap_or(false),
1517 llvm_targets,
1518 llvm_tests: llvm_tests.unwrap_or(false),
1519 llvm_thin_lto: llvm_thin_lto.unwrap_or(false),
1520 llvm_tools_enabled: rust_llvm_tools.unwrap_or(true),
1521 llvm_use_libcxx: llvm_use_libcxx.unwrap_or(false),
1522 llvm_use_linker,
1523 llvm_version_suffix,
1524 local_rebuild,
1525 locked_deps: build_locked_deps.unwrap_or(false),
1526 low_priority: build_low_priority.unwrap_or(false),
1527 mandir: install_mandir.map(PathBuf::from),
1528 miri_info,
1529 musl_root: rust_musl_root.map(PathBuf::from),
1530 ninja_in_file: llvm_ninja.unwrap_or(true),
1531 nodejs: build_nodejs.map(PathBuf::from),
1532 offload_clang_dir: offload_clang_dir.map(PathBuf::from),
1533 omit_git_hash,
1534 on_fail: flags_on_fail,
1535 optimized_compiler_builtins,
1536 out,
1537 patch_binaries_for_nix: build_patch_binaries_for_nix,
1538 path_modification_cache,
1539 paths,
1540 prefix: install_prefix.map(PathBuf::from),
1541 print_step_rusage: build_print_step_rusage.unwrap_or(false),
1542 print_step_timings: build_print_step_timings.unwrap_or(false),
1543 profiler: build_profiler.unwrap_or(false),
1544 python: build_python.map(PathBuf::from),
1545 quiet: flags_quiet,
1546 record_failed_tests_path,
1547 reproducible_artifacts: flags_reproducible_artifact,
1548 reuse: build_reuse.map(PathBuf::from),
1549 rust_analyzer_info,
1550 rust_annotate_moves_size_limit,
1551 rust_break_on_ice: rust_break_on_ice.unwrap_or(true),
1552 rust_codegen_backends: rust_codegen_backends
1553 .map(|backends| parse_codegen_backends(backends, "rust"))
1554 .unwrap_or(vec![CodegenBackendKind::Llvm]),
1555 rust_codegen_units: rust_codegen_units.map(threads_from_config),
1556 rust_codegen_units_std: rust_codegen_units_std.map(threads_from_config),
1557 rust_compress_debuginfo: rust_compress_debuginfo.unwrap_or_default(),
1558 rust_debug_logging: rust_debug_logging
1559 .or(rust_rustc_debug_assertions)
1560 .unwrap_or(rust_debug == Some(true)),
1561 rust_debuginfo_level_rustc: with_defaults(rust_debuginfo_level_rustc),
1562 rust_debuginfo_level_std: with_defaults(rust_debuginfo_level_std),
1563 rust_debuginfo_level_tests: rust_debuginfo_level_tests.unwrap_or(DebuginfoLevel::None),
1564 rust_debuginfo_level_tools: with_defaults(rust_debuginfo_level_tools),
1565 rust_dist_src: dist_src_tarball.unwrap_or_else(|| rust_dist_src.unwrap_or(true)),
1566 rust_frame_pointers: rust_frame_pointers.unwrap_or(false),
1567 rust_info,
1568 rust_lto: rust_lto
1569 .as_deref()
1570 .map(|value| RustcLto::from_str(value).unwrap())
1571 .unwrap_or_default(),
1572 rust_new_symbol_mangling,
1573 rust_optimize: rust_optimize.unwrap_or(RustOptimize::Bool(true)),
1574 rust_optimize_tests: rust_optimize_tests.unwrap_or(true),
1575 rust_overflow_checks: rust_overflow_checks.unwrap_or(rust_debug == Some(true)),
1576 rust_overflow_checks_std: rust_overflow_checks_std
1577 .or(rust_overflow_checks)
1578 .unwrap_or(rust_debug == Some(true)),
1579 rust_parallel_frontend_threads: rust_parallel_frontend_threads.map(threads_from_config),
1580 rust_pgo: pgo_rustc,
1581 rust_randomize_layout: rust_randomize_layout.unwrap_or(false),
1582 rust_remap_debuginfo: rust_remap_debuginfo.unwrap_or(false),
1583 rust_rpath: rust_rpath.unwrap_or(true),
1584 rust_rustflags: rust_rustflags.unwrap_or_default(),
1585 rust_stack_protector,
1586 rust_std_features: rust_std_features
1587 .unwrap_or(BTreeSet::from([String::from("panic-unwind")])),
1588 rust_strip: rust_strip.unwrap_or(false),
1589 rust_thin_lto_import_instr_limit,
1590 rust_validate_mir_opts,
1591 rust_verify_llvm_ir: rust_verify_llvm_ir.unwrap_or(false),
1592 rustc_debug_assertions: rust_rustc_debug_assertions.unwrap_or(rust_debug == Some(true)),
1593 rustc_default_linker: rust_default_linker,
1594 rustc_error_format: flags_rustc_error_format,
1595 rustdoc_pgo: pgo_rustdoc,
1596 rustfmt_info,
1597 sanitizers: build_sanitizers.unwrap_or(false),
1598 save_toolstates: rust_save_toolstates.map(PathBuf::from),
1599 sde: build_sde.map(PathBuf::from),
1600 skip,
1601 skip_std_check_if_no_download_rustc: flags_skip_std_check_if_no_download_rustc,
1602 src,
1603 stage,
1604 stage0_metadata,
1605 std_debug_assertions: rust_std_debug_assertions
1606 .or(rust_rustc_debug_assertions)
1607 .unwrap_or(rust_debug == Some(true)),
1608 stderr_is_tty: std::io::stderr().is_terminal(),
1609 stdlib_semver_baseline: rust_stdlib_semver_baseline,
1610 stdout_is_tty: std::io::stdout().is_terminal(),
1611 submodules: build_submodules,
1612 sysconfdir: install_sysconfdir.map(PathBuf::from),
1613 target_config,
1614 targets,
1615 test_compare_mode: rust_test_compare_mode.unwrap_or(false),
1616 tidy_extra_checks: build_tidy_extra_checks,
1617 tool: build_tool.unwrap_or_default(),
1618 tools: build_tools,
1619 tools_debug_assertions: rust_tools_debug_assertions
1620 .or(rust_rustc_debug_assertions)
1621 .unwrap_or(rust_debug == Some(true)),
1622 vendor,
1623 verbose_tests,
1624 wasm_proc_macros: wasm_proc_macros.unwrap_or(false),
1625 windows_rc: build_windows_rc.map(PathBuf::from),
1626 yarn: build_yarn.map(PathBuf::from),
1627 }
1629 }
1630
1631 pub fn dry_run(&self) -> bool {
1632 self.exec_ctx.dry_run()
1633 }
1634
1635 pub fn is_running_on_ci(&self) -> bool {
1636 self.ci_env.is_running_in_ci()
1637 }
1638
1639 pub fn is_explicit_stage(&self) -> bool {
1640 self.explicit_stage_from_cli || self.explicit_stage_from_config
1641 }
1642
1643 pub(crate) fn test_args(&self) -> Vec<&str> {
1644 let mut test_args = match self.cmd {
1645 Subcommand::Test { ref test_args, .. }
1646 | Subcommand::Bench { ref test_args, .. }
1647 | Subcommand::Miri { ref test_args, .. } => {
1648 test_args.iter().flat_map(|s| s.split_whitespace()).collect()
1649 }
1650 _ => vec![],
1651 };
1652 test_args.extend(self.free_args.iter().map(|s| s.as_str()));
1653 test_args
1654 }
1655
1656 pub(crate) fn args(&self) -> Vec<&str> {
1657 let mut args = match self.cmd {
1658 Subcommand::Run { ref args, .. } => {
1659 args.iter().flat_map(|s| s.split_whitespace()).collect()
1660 }
1661 _ => vec![],
1662 };
1663 args.extend(self.free_args.iter().map(|s| s.as_str()));
1664 args
1665 }
1666
1667 pub(crate) fn read_file_by_commit(&self, file: &Path, commit: &str) -> String {
1669 let dwn_ctx = DownloadContext::from(self);
1670 read_file_by_commit(dwn_ctx, &self.rust_info, file, commit)
1671 }
1672
1673 pub(crate) fn artifact_version_part(&self, commit: &str) -> String {
1676 let (channel, version) = if self.rust_info.is_managed_git_subrepository() {
1677 let channel =
1678 self.read_file_by_commit(Path::new("src/ci/channel"), commit).trim().to_owned();
1679 let version =
1680 self.read_file_by_commit(Path::new("src/version"), commit).trim().to_owned();
1681 (channel, version)
1682 } else {
1683 let channel = fs::read_to_string(self.src.join("src/ci/channel"));
1684 let version = fs::read_to_string(self.src.join("src/version"));
1685 match (channel, version) {
1686 (Ok(channel), Ok(version)) => {
1687 (channel.trim().to_owned(), version.trim().to_owned())
1688 }
1689 (channel, version) => {
1690 let src = self.src.display();
1691 eprintln!("ERROR: failed to determine artifact channel and/or version");
1692 eprintln!(
1693 "HELP: consider using a git checkout or ensure these files are readable"
1694 );
1695 if let Err(channel) = channel {
1696 eprintln!("reading {src}/src/ci/channel failed: {channel:?}");
1697 }
1698 if let Err(version) = version {
1699 eprintln!("reading {src}/src/version failed: {version:?}");
1700 }
1701 panic!();
1702 }
1703 }
1704 };
1705
1706 match channel.as_str() {
1707 "stable" => version,
1708 "beta" => channel,
1709 "nightly" => channel,
1710 other => unreachable!("{:?} is not recognized as a valid channel", other),
1711 }
1712 }
1713
1714 pub fn bindir_relative(&self) -> &Path {
1716 let bindir = &self.bindir;
1717 if bindir.is_absolute() {
1718 if let Some(prefix) = &self.prefix
1720 && let Ok(stripped) = bindir.strip_prefix(prefix)
1721 {
1722 return stripped;
1723 }
1724 }
1725 bindir
1726 }
1727
1728 pub fn libdir_relative(&self) -> Option<&Path> {
1730 let libdir = self.libdir.as_ref()?;
1731 if libdir.is_relative() {
1732 Some(libdir)
1733 } else {
1734 libdir.strip_prefix(self.prefix.as_ref()?).ok()
1736 }
1737 }
1738
1739 pub(crate) fn ci_rustc_dir(&self) -> PathBuf {
1741 assert!(self.download_rustc());
1742 self.out.join(self.host_target).join("ci-rustc")
1743 }
1744
1745 pub(crate) fn download_rustc(&self) -> bool {
1747 self.download_rustc_commit().is_some()
1748 }
1749
1750 pub(crate) fn download_rustc_commit(&self) -> Option<&str> {
1751 static DOWNLOAD_RUSTC: OnceLock<Option<String>> = OnceLock::new();
1752 if self.dry_run() && DOWNLOAD_RUSTC.get().is_none() {
1753 return self.download_rustc_commit.as_deref();
1755 }
1756
1757 DOWNLOAD_RUSTC
1758 .get_or_init(|| match &self.download_rustc_commit {
1759 None => None,
1760 Some(commit) => {
1761 self.download_ci_rustc(commit);
1762
1763 if !self.llvm_ci_mode.download_from_ci() {
1767 if self.is_running_on_ci() {
1770 println!("WARNING: LLVM submodule has changes, `download-rustc` will be disabled.");
1771 return None;
1772 } else {
1773 panic!("ERROR: LLVM submodule has changes, `download-rustc` can't be used.");
1774 }
1775 }
1776
1777 if let Some(config_path) = &self.config {
1778 let ci_config_toml = match self.get_builder_toml("ci-rustc") {
1779 Ok(ci_config_toml) => ci_config_toml,
1780 Err(e) if e.to_string().contains("unknown field") => {
1781 println!("WARNING: CI rustc has some fields that are no longer supported in bootstrap; download-rustc will be disabled.");
1782 println!("HELP: Consider rebasing to a newer commit if available.");
1783 return None;
1784 }
1785 Err(e) => {
1786 eprintln!("ERROR: Failed to parse CI rustc bootstrap.toml: {e}");
1787 helpers::exit_process(2);
1788 }
1789 };
1790
1791 let current_config_toml = Self::get_toml(config_path).unwrap();
1792
1793 let res = check_incompatible_options_for_ci_rustc(
1796 self.host_target,
1797 current_config_toml,
1798 ci_config_toml,
1799 );
1800
1801 let disable_ci_rustc_if_incompatible = env::var_os("DISABLE_CI_RUSTC_IF_INCOMPATIBLE")
1804 .is_some_and(|s| s == "1" || s == "true");
1805
1806 if disable_ci_rustc_if_incompatible && res.is_err() {
1807 println!("WARNING: download-rustc is disabled with `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` env.");
1808 return None;
1809 }
1810
1811 res.unwrap();
1812 }
1813
1814 Some(commit.clone())
1815 }
1816 })
1817 .as_deref()
1818 }
1819
1820 pub fn do_if_verbose(&self, f: impl Fn()) {
1822 self.exec_ctx.do_if_verbose(f);
1823 }
1824
1825 pub fn any_sanitizers_to_build(&self) -> bool {
1826 self.target_config
1827 .iter()
1828 .any(|(ts, t)| !ts.is_msvc() && t.sanitizers.unwrap_or(self.sanitizers))
1829 }
1830
1831 pub fn any_profiler_enabled(&self) -> bool {
1832 self.target_config.values().any(|t| matches!(&t.profiler, Some(p) if p.is_string_or_true()))
1833 || self.profiler
1834 }
1835
1836 pub fn submodules(&self) -> bool {
1838 self.submodules.unwrap_or(self.rust_info.is_managed_git_subrepository())
1841 }
1842
1843 pub fn git_config(&self) -> GitConfig<'_> {
1844 GitConfig {
1845 nightly_branch: &self.stage0_metadata.config.nightly_branch,
1846 git_merge_commit_email: &self.stage0_metadata.config.git_merge_commit_email,
1847 }
1848 }
1849
1850 #[cfg_attr(
1860 feature = "tracing",
1861 instrument(
1862 level = "trace",
1863 name = "Config::update_submodule",
1864 skip_all,
1865 fields(relative_path = ?relative_path),
1866 ),
1867 )]
1868 pub(crate) fn update_submodule(&self, relative_path: &str) {
1869 let dwn_ctx = DownloadContext::from(self);
1870 update_submodule(dwn_ctx, &self.rust_info, relative_path);
1871 }
1872
1873 pub fn has_changes_from_upstream(&self, paths: &[&'static str]) -> bool {
1875 let dwn_ctx = DownloadContext::from(self);
1876 has_changes_from_upstream(dwn_ctx, paths)
1877 }
1878
1879 pub fn check_path_modifications(&self, paths: &[&'static str]) -> PathFreshness {
1881 self.path_modification_cache
1887 .lock()
1888 .unwrap()
1889 .entry(paths.to_vec())
1890 .or_insert_with(|| {
1891 check_path_modifications(&self.src, &self.git_config(), paths, self.ci_env).unwrap()
1892 })
1893 .clone()
1894 }
1895
1896 pub fn sanitizers_enabled(&self, target: TargetSelection) -> bool {
1897 self.target_config.get(&target).and_then(|t| t.sanitizers).unwrap_or(self.sanitizers)
1898 }
1899
1900 pub fn needs_sanitizer_runtime_built(&self, target: TargetSelection) -> bool {
1901 !target.is_msvc() && self.sanitizers_enabled(target)
1903 }
1904
1905 pub fn profiler_path(&self, target: TargetSelection) -> Option<&str> {
1906 match self.target_config.get(&target)?.profiler.as_ref()? {
1907 StringOrBool::String(s) => Some(s),
1908 StringOrBool::Bool(_) => None,
1909 }
1910 }
1911
1912 pub fn profiler_enabled(&self, target: TargetSelection) -> bool {
1913 self.target_config
1914 .get(&target)
1915 .and_then(|t| t.profiler.as_ref())
1916 .map(StringOrBool::is_string_or_true)
1917 .unwrap_or(self.profiler)
1918 }
1919
1920 pub fn enabled_codegen_backends(&self, target: TargetSelection) -> &[CodegenBackendKind] {
1924 self.target_config
1925 .get(&target)
1926 .and_then(|cfg| cfg.codegen_backends.as_deref())
1927 .unwrap_or(&self.rust_codegen_backends)
1928 }
1929
1930 pub fn default_codegen_backend(&self, target: TargetSelection) -> &CodegenBackendKind {
1933 self.enabled_codegen_backends(target).first().unwrap()
1935 }
1936
1937 pub fn allocator(&self, target: TargetSelection) -> Allocator {
1938 self.target_config
1939 .get(&target)
1940 .and_then(|cfg| cfg.allocator)
1941 .or(self.allocator)
1942 .unwrap_or(Allocator::System)
1943 }
1944
1945 pub fn rpath_enabled(&self, target: TargetSelection) -> bool {
1946 self.target_config.get(&target).and_then(|t| t.rpath).unwrap_or(self.rust_rpath)
1947 }
1948
1949 pub fn optimized_compiler_builtins(&self, target: TargetSelection) -> &CompilerBuiltins {
1950 self.target_config
1951 .get(&target)
1952 .and_then(|t| t.optimized_compiler_builtins.as_ref())
1953 .unwrap_or(&self.optimized_compiler_builtins)
1954 }
1955
1956 pub fn llvm_enabled(&self, target: TargetSelection) -> bool {
1957 self.enabled_codegen_backends(target).contains(&CodegenBackendKind::Llvm)
1958 }
1959
1960 pub fn llvm_libunwind(&self, target: TargetSelection) -> LlvmLibunwind {
1961 self.target_config
1962 .get(&target)
1963 .and_then(|t| t.llvm_libunwind)
1964 .or(self.llvm_libunwind_default)
1965 .unwrap_or(
1966 if target.contains("fuchsia")
1967 || (target.contains("hexagon") && !target.contains("qurt"))
1968 {
1969 LlvmLibunwind::InTree
1972 } else {
1973 LlvmLibunwind::No
1974 },
1975 )
1976 }
1977
1978 pub fn split_debuginfo(&self, target: TargetSelection) -> SplitDebuginfo {
1979 self.target_config
1980 .get(&target)
1981 .and_then(|t| t.split_debuginfo)
1982 .unwrap_or_else(|| SplitDebuginfo::default_for_platform(target))
1983 }
1984
1985 pub fn compress_debuginfo(&self, target: TargetSelection) -> CompressDebuginfo {
1986 self.target_config
1987 .get(&target)
1988 .and_then(|t| t.compress_debuginfo)
1989 .unwrap_or(self.rust_compress_debuginfo)
1990 }
1991
1992 pub fn is_host_target(&self, target: TargetSelection) -> bool {
1994 self.host_target == target
1995 }
1996
1997 pub fn is_rust_llvm(&self, llvm: &LlvmOutput, target: TargetSelection) -> bool {
2001 match self.target_config.get(&target) {
2002 Some(Target { llvm_has_rust_patches: Some(patched), .. }) => *patched,
2006 _ => match llvm.kind() {
2009 LlvmKind::BuiltLocally | LlvmKind::DownloadedFromCi => true,
2010 LlvmKind::External => false,
2011 },
2012 }
2013 }
2014
2015 pub fn exec_ctx(&self) -> &ExecutionContext {
2016 &self.exec_ctx
2017 }
2018
2019 pub fn git_info(&self, omit_git_hash: bool, dir: &Path) -> GitInfo {
2020 GitInfo::new(omit_git_hash, dir, self)
2021 }
2022}
2023
2024impl AsRef<ExecutionContext> for Config {
2025 fn as_ref(&self) -> &ExecutionContext {
2026 &self.exec_ctx
2027 }
2028}
2029
2030fn reconcile_jemalloc(
2035 jemalloc: Option<bool>,
2036 allocator: Option<Allocator>,
2037 jemalloc_section: &str,
2038 allocator_section: &str,
2039) -> Option<Allocator> {
2040 match (jemalloc, allocator) {
2041 (None, None) => None,
2042 (None, Some(allocator)) => Some(allocator),
2043 (Some(true), None) => {
2044 println!(
2045 "WARNING: The `jemalloc` option is deprecated. \
2046 Please use `{allocator_section}.allocator = \"jemalloc\"` instead of `{jemalloc_section}.jemalloc = true`",
2047 );
2048 Some(Allocator::Jemalloc)
2049 }
2050 (Some(false), None) => {
2051 println!(
2052 "WARNING: The `jemalloc` option is deprecated. \
2053 Please use `{allocator_section}.allocator = \"system\"` instead of `{jemalloc_section}.jemalloc = false`",
2054 );
2055 Some(Allocator::System)
2056 }
2057 _ => {
2058 panic!(
2059 "ERROR: `{jemalloc_section}.jemalloc` and `{allocator_section}.allocator` are both set. \
2060 Please remove the outdated `{jemalloc_section}.jemalloc` directive."
2061 )
2062 }
2063 }
2064}
2065
2066fn compute_src_directory(src_dir: Option<PathBuf>, exec_ctx: &ExecutionContext) -> Option<PathBuf> {
2067 if let Some(src) = src_dir {
2068 return Some(src);
2069 } else {
2070 let mut cmd = helpers::git(None);
2073 cmd.arg("rev-parse").arg("--show-cdup");
2081 let output = cmd.allow_failure().run_capture_stdout(exec_ctx);
2083 if output.is_success() {
2084 let git_root_relative = output.stdout();
2085 let git_root = env::current_dir()
2088 .unwrap()
2089 .join(PathBuf::from(git_root_relative.trim()))
2090 .canonicalize()
2091 .unwrap();
2092 let s = git_root.to_str().unwrap();
2093
2094 let git_root = match s.strip_prefix("\\\\?\\") {
2096 Some(p) => PathBuf::from(p),
2097 None => git_root,
2098 };
2099 if git_root.join("src").join("stage0").exists() {
2106 return Some(git_root);
2107 }
2108 } else {
2109 }
2112 };
2113 None
2114}
2115
2116#[derive(Clone)]
2117pub enum LlvmPgoGenerationMode {
2118 Implicit,
2120 Directory(PathBuf),
2122}
2123
2124#[derive(Clone)]
2125pub struct LlvmPgoConfig {
2126 pub use_profile: Option<PathBuf>,
2127 pub generate_profile: Option<LlvmPgoGenerationMode>,
2128}
2129
2130fn load_toml_config(
2135 src: &Path,
2136 config_path: Option<PathBuf>,
2137 get_toml: &impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>,
2138) -> (TomlConfig, Option<PathBuf>) {
2139 let toml_path = config_path.or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from));
2147 let using_default_path = toml_path.is_none();
2148 let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("bootstrap.toml"));
2149
2150 if using_default_path && !toml_path.exists() {
2151 toml_path = src.join(PathBuf::from("bootstrap.toml"));
2152 if !toml_path.exists() {
2153 toml_path = PathBuf::from("config.toml");
2154 if !toml_path.exists() {
2155 toml_path = src.join(PathBuf::from("config.toml"));
2156 }
2157 }
2158 }
2159
2160 if !using_default_path || toml_path.exists() {
2163 let path = Some(if cfg!(not(test)) {
2164 toml_path = toml_path.canonicalize().unwrap();
2165 toml_path.clone()
2166 } else {
2167 toml_path.clone()
2168 });
2169 (get_toml(&toml_path).unwrap_or_else(|e| bad_config(&toml_path, e)), path)
2170 } else {
2171 (TomlConfig::default(), None)
2172 }
2173}
2174
2175fn postprocess_toml(
2176 toml: &mut TomlConfig,
2177 src_dir: &Path,
2178 toml_path: Option<PathBuf>,
2179 exec_ctx: &ExecutionContext,
2180 override_set: &[String],
2181 get_toml: &impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>,
2182) {
2183 let git_info = GitInfo::new(false, src_dir, exec_ctx);
2184
2185 if git_info.is_from_tarball() && toml.profile.is_none() {
2186 toml.profile = Some("dist".into());
2187 }
2188
2189 for include_path in toml.include.clone().unwrap_or_default().iter().rev() {
2195 let include_path = toml_path
2196 .as_ref()
2197 .expect("include found in default TOML config")
2198 .parent()
2199 .unwrap()
2200 .join(include_path);
2201
2202 let included_toml =
2203 get_toml(&include_path).unwrap_or_else(|e| bad_config(&include_path, e));
2204 toml.merge(
2205 Some(include_path),
2206 &mut Default::default(),
2207 included_toml,
2208 ReplaceOpt::IgnoreDuplicate,
2209 );
2210 }
2211
2212 if let Some(include) = &toml.profile {
2213 let profile_aliases = HashMap::from([("user", "dist")]);
2217 let include = match profile_aliases.get(include.as_str()) {
2218 Some(alias) => alias,
2219 None => include.as_str(),
2220 };
2221 let mut include_path = PathBuf::from(src_dir);
2222 include_path.push("src");
2223 include_path.push("bootstrap");
2224 include_path.push("defaults");
2225 include_path.push(format!("bootstrap.{include}.toml"));
2226 let included_toml = get_toml(&include_path).unwrap_or_else(|e| {
2227 eprintln!(
2228 "ERROR: Failed to parse default config profile at '{}': {e}",
2229 include_path.display()
2230 );
2231 helpers::exit_process(2);
2232 });
2233 toml.merge(
2234 Some(include_path),
2235 &mut Default::default(),
2236 included_toml,
2237 ReplaceOpt::IgnoreDuplicate,
2238 );
2239 }
2240
2241 let mut override_toml = TomlConfig::default();
2242 for option in override_set.iter() {
2243 fn get_table(option: &str) -> Result<TomlConfig, toml::de::Error> {
2244 toml::from_str(option).and_then(|table: toml::Value| TomlConfig::deserialize(table))
2245 }
2246
2247 let mut err = match get_table(option) {
2248 Ok(v) => {
2249 override_toml.merge(None, &mut Default::default(), v, ReplaceOpt::ErrorOnDuplicate);
2250 continue;
2251 }
2252 Err(e) => e,
2253 };
2254 if let Some((key, value)) = option.split_once('=')
2257 && !value.contains('"')
2258 {
2259 match get_table(&format!(r#"{key}="{value}""#)) {
2260 Ok(v) => {
2261 override_toml.merge(
2262 None,
2263 &mut Default::default(),
2264 v,
2265 ReplaceOpt::ErrorOnDuplicate,
2266 );
2267 continue;
2268 }
2269 Err(e) => err = e,
2270 }
2271 }
2272 eprintln!("failed to parse override `{option}`: `{err}");
2273 helpers::exit_process(2);
2274 }
2275 toml.merge(None, &mut Default::default(), override_toml, ReplaceOpt::Override);
2276}
2277
2278pub fn check_stage0_version(
2280 program_path: &Path,
2281 component_name: &'static str,
2282 src_dir: &Path,
2283 exec_ctx: &ExecutionContext,
2284) {
2285 if cfg!(test) || exec_ctx.dry_run() {
2286 return;
2287 }
2288
2289 let stage0_output =
2290 command(program_path).arg("--version").run_capture_stdout(exec_ctx).stdout();
2291 let mut stage0_output = stage0_output.lines().next().unwrap().split(' ');
2292
2293 let stage0_name = stage0_output.next().unwrap();
2294 if stage0_name != component_name {
2295 fail(&format!(
2296 "Expected to find {component_name} at {} but it claims to be {stage0_name}",
2297 program_path.display()
2298 ));
2299 }
2300
2301 let stage0_version =
2302 semver::Version::parse(stage0_output.next().unwrap().split('-').next().unwrap().trim())
2303 .unwrap();
2304 let source_version =
2305 semver::Version::parse(fs::read_to_string(src_dir.join("src/version")).unwrap().trim())
2306 .unwrap();
2307 if !(source_version == stage0_version
2308 || (source_version.major == stage0_version.major
2309 && (source_version.minor == stage0_version.minor
2310 || source_version.minor == stage0_version.minor + 1)))
2311 {
2312 let prev_version = format!("{}.{}.x", source_version.major, source_version.minor - 1);
2313 fail(&format!(
2314 "Unexpected {component_name} version: {stage0_version}, we should use {prev_version}/{source_version} to build source with {source_version}"
2315 ));
2316 }
2317}
2318
2319fn print_rustc_modifications(
2320 dwn_ctx: &DownloadContext<'_>,
2321 if_unchanged: bool,
2322 mut modifications: Vec<PathBuf>,
2323) -> Option<()> {
2324 if !dwn_ctx.exec_ctx.is_verbose() {
2325 modifications.retain(|path| !path.starts_with("compiler"));
2326 }
2327 if modifications.is_empty() {
2328 eprintln!(
2330 "skipping rustc download with `download-rustc = 'if-unchanged'` due to local changes"
2331 );
2332 return None;
2333 }
2334
2335 eprintln!(
2336 "NOTE: detected {} modifications that could affect a build of rustc",
2337 modifications.len()
2338 );
2339 for file in modifications.iter().take(10) {
2340 eprintln!("- {}", file.display());
2341 }
2342 if modifications.len() > 10 {
2343 eprintln!("- ... and {} more", modifications.len() - 10);
2344 }
2345
2346 if if_unchanged {
2347 eprintln!("skipping rustc download due to `download-rustc = 'if-unchanged'`");
2348 None
2349 } else {
2350 eprintln!("downloading unconditionally due to `download-rustc = true`");
2351 Some(())
2352 }
2353}
2354
2355pub fn download_ci_rustc_commit<'a>(
2356 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2357 rust_info: &channel::GitInfo,
2358 download_rustc: Option<StringOrBool>,
2359 llvm_assertions: bool,
2360) -> Option<String> {
2361 let dwn_ctx = dwn_ctx.as_ref();
2362
2363 if !is_download_ci_available(&dwn_ctx.host_target.triple, llvm_assertions) {
2364 return None;
2365 }
2366
2367 let if_unchanged = match download_rustc {
2369 None | Some(StringOrBool::Bool(false)) => return None,
2375 Some(StringOrBool::Bool(true)) => false,
2376 Some(StringOrBool::String(s)) if s == "if-unchanged" => {
2377 if !rust_info.is_managed_git_subrepository() {
2378 println!(
2379 "ERROR: `download-rustc=if-unchanged` is only compatible with Git managed sources."
2380 );
2381 helpers::exit_process(1);
2382 }
2383
2384 true
2385 }
2386 Some(StringOrBool::String(other)) => {
2387 panic!("unrecognized option for download-rustc: {other}")
2388 }
2389 };
2390
2391 let commit = if rust_info.is_managed_git_subrepository() {
2392 let freshness = check_path_modifications_(dwn_ctx, RUSTC_IF_UNCHANGED_ALLOWED_PATHS);
2395 dwn_ctx.exec_ctx.do_if_verbose(|| {
2396 eprintln!("rustc freshness: {freshness:?}");
2397 });
2398 match freshness {
2399 PathFreshness::LastModifiedUpstream { upstream } => upstream,
2400 PathFreshness::HasLocalModifications { upstream, modifications } => {
2401 if dwn_ctx.is_running_on_ci() {
2402 eprintln!("CI rustc commit matches with HEAD and we are in CI.");
2403 eprintln!(
2404 "`rustc.download-ci` functionality will be skipped as artifacts are not available."
2405 );
2406 return None;
2407 }
2408
2409 print_rustc_modifications(dwn_ctx, if_unchanged, modifications)?;
2410 upstream
2411 }
2412 PathFreshness::MissingUpstream => {
2413 eprintln!("No upstream commit found");
2414 return None;
2415 }
2416 }
2417 } else {
2418 channel::read_commit_info_file(dwn_ctx.src)
2419 .map(|info| info.sha.trim().to_owned())
2420 .expect("git-commit-info is missing in the project root")
2421 };
2422
2423 Some(commit)
2424}
2425
2426pub fn check_path_modifications_<'a>(
2427 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2428 paths: &[&'static str],
2429) -> PathFreshness {
2430 let dwn_ctx = dwn_ctx.as_ref();
2431 dwn_ctx
2437 .path_modification_cache
2438 .lock()
2439 .unwrap()
2440 .entry(paths.to_vec())
2441 .or_insert_with(|| {
2442 check_path_modifications(
2443 dwn_ctx.src,
2444 &git_config(dwn_ctx.stage0_metadata),
2445 paths,
2446 dwn_ctx.ci_env,
2447 )
2448 .unwrap()
2449 })
2450 .clone()
2451}
2452
2453pub fn git_config(stage0_metadata: &build_helper::stage0_parser::Stage0) -> GitConfig<'_> {
2454 GitConfig {
2455 nightly_branch: &stage0_metadata.config.nightly_branch,
2456 git_merge_commit_email: &stage0_metadata.config.git_merge_commit_email,
2457 }
2458}
2459
2460pub fn parse_download_ci_llvm<'a>(
2461 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2462 rust_info: &channel::GitInfo,
2463 download_rustc_commit: &Option<String>,
2464 download_ci_llvm: Option<StringOrBool>,
2465 asserts: bool,
2466) -> bool {
2467 let dwn_ctx = dwn_ctx.as_ref();
2468 let download_ci_llvm = download_ci_llvm.unwrap_or(StringOrBool::Bool(true));
2469
2470 let if_unchanged = || {
2471 if rust_info.is_from_tarball() {
2472 println!("ERROR: 'if-unchanged' is only compatible with Git managed sources.");
2474 helpers::exit_process(1);
2475 }
2476
2477 if cfg!(not(test)) {
2479 update_submodule(dwn_ctx, rust_info, "src/llvm-project");
2480 }
2481
2482 let has_changes = has_changes_from_upstream(dwn_ctx, LLVM_INVALIDATION_PATHS);
2484
2485 if has_changes {
2487 false
2488 } else {
2489 llvm::is_ci_llvm_available_for_target(&dwn_ctx.host_target, asserts)
2490 }
2491 };
2492
2493 match download_ci_llvm {
2494 StringOrBool::Bool(b) => {
2495 if !b && download_rustc_commit.is_some() {
2496 panic!(
2497 "`llvm.download-ci-llvm` cannot be set to `false` if `rust.download-rustc` is set to `true` or `if-unchanged`."
2498 );
2499 }
2500
2501 if cfg!(not(test))
2502 && b
2503 && dwn_ctx.is_running_on_ci()
2504 && CiEnv::is_rust_lang_managed_ci_job()
2505 {
2506 panic!(
2508 "`llvm.download-ci-llvm` cannot be set to `true` on CI. Use `if-unchanged` instead."
2509 );
2510 }
2511
2512 b && llvm::is_ci_llvm_available_for_target(&dwn_ctx.host_target, asserts)
2514 }
2515 StringOrBool::String(s) if s == "if-unchanged" => if_unchanged(),
2516 StringOrBool::String(other) => {
2517 panic!("unrecognized option for download-ci-llvm: {other:?}")
2518 }
2519 }
2520}
2521
2522pub fn has_changes_from_upstream<'a>(
2523 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2524 paths: &[&'static str],
2525) -> bool {
2526 let dwn_ctx = dwn_ctx.as_ref();
2527 match check_path_modifications_(dwn_ctx, paths) {
2528 PathFreshness::LastModifiedUpstream { .. } => false,
2529 PathFreshness::HasLocalModifications { .. } | PathFreshness::MissingUpstream => true,
2530 }
2531}
2532
2533#[cfg_attr(
2534 feature = "tracing",
2535 instrument(
2536 level = "trace",
2537 name = "Config::update_submodule",
2538 skip_all,
2539 fields(relative_path = ?relative_path),
2540 ),
2541)]
2542pub(crate) fn update_submodule<'a>(
2543 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2544 rust_info: &channel::GitInfo,
2545 relative_path: &str,
2546) {
2547 let dwn_ctx = dwn_ctx.as_ref();
2548 if rust_info.is_from_tarball() || !submodules_(dwn_ctx.submodules, rust_info) {
2549 return;
2550 }
2551
2552 let absolute_path = dwn_ctx.src.join(relative_path);
2553
2554 if !absolute_path.exists() {
2558 t!(fs::create_dir_all(&absolute_path));
2559 }
2560
2561 if !git_info(dwn_ctx.exec_ctx, false, &absolute_path).is_managed_git_subrepository()
2564 && !helpers::dir_is_empty(&absolute_path)
2565 {
2566 return;
2567 }
2568
2569 let submodule_git = || helpers::git(Some(&absolute_path));
2570
2571 let checked_out_hash =
2573 submodule_git().args(["rev-parse", "HEAD"]).run_capture_stdout(dwn_ctx.exec_ctx).stdout();
2574 let checked_out_hash = checked_out_hash.trim_end();
2575 let recorded = helpers::git(Some(dwn_ctx.src))
2577 .run_in_dry_run() .args(["ls-tree", "HEAD"])
2579 .arg(relative_path)
2580 .run_capture_stdout(dwn_ctx.exec_ctx)
2581 .stdout();
2582
2583 let actual_hash = recorded
2584 .split_whitespace()
2585 .nth(2)
2586 .unwrap_or_else(|| panic!("unexpected output `{recorded}` when updating {relative_path}"));
2587
2588 if actual_hash == checked_out_hash {
2589 return;
2591 }
2592
2593 if !dwn_ctx.exec_ctx.dry_run() {
2594 println!("Updating submodule {relative_path}");
2595 };
2596
2597 helpers::git(Some(dwn_ctx.src))
2598 .allow_failure()
2599 .args(["submodule", "-q", "sync"])
2600 .arg(relative_path)
2601 .run(dwn_ctx.exec_ctx);
2602
2603 let update = |progress: bool| {
2605 let current_branch = helpers::git(Some(dwn_ctx.src))
2608 .allow_failure()
2609 .args(["symbolic-ref", "--short", "HEAD"])
2610 .run_capture(dwn_ctx.exec_ctx);
2611
2612 let mut git = helpers::git(Some(dwn_ctx.src)).allow_failure();
2613 if current_branch.is_success() {
2614 let branch = current_branch.stdout();
2617 let branch = branch.trim();
2618 let branch = branch.strip_prefix("heads/").unwrap_or(branch);
2619 git.arg("-c").arg(format!("branch.{branch}.remote=origin"));
2620 }
2621 git.args(["submodule", "update", "--init", "--recursive", "--depth=1"]);
2622 if progress {
2623 git.arg("--progress");
2624 }
2625 git.arg(relative_path);
2626 git
2627 };
2628 if !update(true).allow_failure().run(dwn_ctx.exec_ctx) {
2629 update(false).allow_failure().run(dwn_ctx.exec_ctx);
2630 }
2631
2632 let has_local_modifications = !submodule_git()
2635 .allow_failure()
2636 .args(["diff-index", "--quiet", "HEAD"])
2637 .run(dwn_ctx.exec_ctx);
2638 if has_local_modifications {
2639 submodule_git().allow_failure().args(["stash", "push"]).run(dwn_ctx.exec_ctx);
2640 }
2641
2642 submodule_git().allow_failure().args(["reset", "-q", "--hard"]).run(dwn_ctx.exec_ctx);
2643 submodule_git().allow_failure().args(["clean", "-qdfx"]).run(dwn_ctx.exec_ctx);
2644
2645 if has_local_modifications {
2646 submodule_git().allow_failure().args(["stash", "pop"]).run(dwn_ctx.exec_ctx);
2647 }
2648}
2649
2650pub fn git_info(exec_ctx: &ExecutionContext, omit_git_hash: bool, dir: &Path) -> GitInfo {
2651 GitInfo::new(omit_git_hash, dir, exec_ctx)
2652}
2653
2654pub fn submodules_(submodules: &Option<bool>, rust_info: &channel::GitInfo) -> bool {
2655 submodules.unwrap_or(rust_info.is_managed_git_subrepository())
2658}
2659
2660pub(crate) fn read_file_by_commit<'a>(
2662 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2663 rust_info: &channel::GitInfo,
2664 file: &Path,
2665 commit: &str,
2666) -> String {
2667 let dwn_ctx = dwn_ctx.as_ref();
2668 assert!(
2669 rust_info.is_managed_git_subrepository(),
2670 "`Config::read_file_by_commit` is not supported in non-git sources."
2671 );
2672
2673 let mut git = helpers::git(Some(dwn_ctx.src));
2674 git.arg("show").arg(format!("{commit}:{}", file.to_str().unwrap()));
2675 git.run_capture_stdout(dwn_ctx.exec_ctx).stdout()
2676}
2677
2678fn bad_config(toml_path: &Path, e: toml::de::Error) -> ! {
2679 eprintln!("ERROR: Failed to parse '{}': {e}", toml_path.display());
2680 let e_s = e.to_string();
2681 if e_s.contains("unknown field")
2682 && let Some(field_name) = e_s.split("`").nth(1)
2683 && let sections = find_correct_section_for_field(field_name)
2684 && !sections.is_empty()
2685 {
2686 if sections.len() == 1 {
2687 match sections[0] {
2688 WouldBeValidFor::TopLevel { is_section } => {
2689 if is_section {
2690 eprintln!(
2691 "hint: section name `{field_name}` used as a key within a section"
2692 );
2693 } else {
2694 eprintln!("hint: try using `{field_name}` as a top level key");
2695 }
2696 }
2697 WouldBeValidFor::Section(section) => {
2698 eprintln!("hint: try moving `{field_name}` to the `{section}` section")
2699 }
2700 }
2701 } else {
2702 eprintln!(
2703 "hint: `{field_name}` would be valid {}",
2704 join_oxford_comma(sections.iter(), "or"),
2705 );
2706 }
2707 }
2708
2709 helpers::exit_process(2);
2710}
2711
2712#[derive(Copy, Clone, Debug)]
2713enum WouldBeValidFor {
2714 TopLevel { is_section: bool },
2715 Section(&'static str),
2716}
2717
2718fn join_oxford_comma(
2719 mut parts: impl ExactSizeIterator<Item = impl std::fmt::Display>,
2720 conj: &str,
2721) -> String {
2722 use std::fmt::Write;
2723 let mut out = String::new();
2724
2725 assert!(parts.len() > 1);
2726 while let Some(part) = parts.next() {
2727 if parts.len() == 0 {
2728 write!(&mut out, "{conj} {part}")
2729 } else {
2730 write!(&mut out, "{part}, ")
2731 }
2732 .unwrap();
2733 }
2734 out
2735}
2736
2737impl std::fmt::Display for WouldBeValidFor {
2738 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2739 match self {
2740 Self::TopLevel { .. } => write!(f, "at top level"),
2741 Self::Section(section_name) => write!(f, "in section `{section_name}`"),
2742 }
2743 }
2744}
2745
2746fn find_correct_section_for_field(field_name: &str) -> Vec<WouldBeValidFor> {
2747 let sections = ["build", "install", "llvm", "gcc", "rust", "dist"];
2748 sections
2749 .iter()
2750 .map(Some)
2751 .chain([None])
2752 .filter_map(|section_name| {
2753 let dummy_config_str = if let Some(section_name) = section_name {
2754 format!("{section_name}.{field_name} = 0\n")
2755 } else {
2756 format!("{field_name} = 0\n")
2757 };
2758 let is_unknown_field = toml::from_str::<toml::Value>(&dummy_config_str)
2759 .and_then(TomlConfig::deserialize)
2760 .err()
2761 .is_some_and(|e| e.to_string().contains("unknown field"));
2762 if is_unknown_field {
2763 None
2764 } else {
2765 Some(section_name.copied().map(WouldBeValidFor::Section).unwrap_or_else(|| {
2766 WouldBeValidFor::TopLevel { is_section: sections.contains(&field_name) }
2767 }))
2768 }
2769 })
2770 .collect()
2771}