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