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