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