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