1use std::cell::Cell;
18use std::collections::{BTreeSet, HashMap, HashSet};
19use std::io::IsTerminal;
20use std::path::{Path, PathBuf, absolute};
21use std::str::FromStr;
22use std::sync::{Arc, Mutex};
23use std::{cmp, env, fs};
24
25use build_helper::ci::CiEnv;
26use build_helper::exit;
27use build_helper::git::{GitConfig, PathFreshness, check_path_modifications};
28use serde::Deserialize;
29#[cfg(feature = "tracing")]
30use tracing::{instrument, span};
31
32use crate::core::build_steps::llvm;
33use crate::core::build_steps::llvm::LLVM_INVALIDATION_PATHS;
34pub use crate::core::config::flags::Subcommand;
35use crate::core::config::flags::{Color, Flags, Warnings};
36use crate::core::config::target_selection::TargetSelectionList;
37use crate::core::config::toml::TomlConfig;
38use crate::core::config::toml::build::{Build, Tool};
39use crate::core::config::toml::change_id::ChangeId;
40use crate::core::config::toml::dist::Dist;
41use crate::core::config::toml::gcc::Gcc;
42use crate::core::config::toml::install::Install;
43use crate::core::config::toml::llvm::Llvm;
44use crate::core::config::toml::rust::{
45 LldMode, Rust, RustOptimize, check_incompatible_options_for_ci_rustc,
46 default_lld_opt_in_targets, parse_codegen_backends,
47};
48use crate::core::config::toml::target::Target;
49use crate::core::config::{
50 DebuginfoLevel, DryRun, GccCiMode, LlvmLibunwind, Merge, ReplaceOpt, RustcLto, SplitDebuginfo,
51 StringOrBool, set, threads_from_config,
52};
53use crate::core::download::{
54 DownloadContext, download_beta_toolchain, is_download_ci_available, maybe_download_rustfmt,
55};
56use crate::utils::channel;
57use crate::utils::exec::{ExecutionContext, command};
58use crate::utils::helpers::{exe, get_host_target};
59use crate::{CodegenBackendKind, GitInfo, OnceLock, TargetSelection, check_ci_llvm, helpers, t};
60
61#[rustfmt::skip] pub const RUSTC_IF_UNCHANGED_ALLOWED_PATHS: &[&str] = &[
74 ":!library",
75 ":!src/tools",
76 ":!src/librustdoc",
77 ":!src/rustdoc-json-types",
78 ":!tests",
79 ":!triagebot.toml",
80];
81
82#[derive(Default, Clone)]
91pub struct Config {
92 pub change_id: Option<ChangeId>,
93 pub bypass_bootstrap_lock: bool,
94 pub ccache: Option<String>,
95 pub ninja_in_file: bool,
97 pub submodules: Option<bool>,
98 pub compiler_docs: bool,
99 pub library_docs_private_items: bool,
100 pub docs_minification: bool,
101 pub docs: bool,
102 pub locked_deps: bool,
103 pub vendor: bool,
104 pub target_config: HashMap<TargetSelection, Target>,
105 pub full_bootstrap: bool,
106 pub bootstrap_cache_path: Option<PathBuf>,
107 pub extended: bool,
108 pub tools: Option<HashSet<String>>,
109 pub tool: HashMap<String, Tool>,
112 pub sanitizers: bool,
113 pub profiler: bool,
114 pub omit_git_hash: bool,
115 pub skip: Vec<PathBuf>,
116 pub include_default_paths: bool,
117 pub rustc_error_format: Option<String>,
118 pub json_output: bool,
119 pub compile_time_deps: bool,
120 pub test_compare_mode: bool,
121 pub color: Color,
122 pub patch_binaries_for_nix: Option<bool>,
123 pub stage0_metadata: build_helper::stage0_parser::Stage0,
124 pub android_ndk: Option<PathBuf>,
125 pub optimized_compiler_builtins: bool,
127
128 pub stdout_is_tty: bool,
129 pub stderr_is_tty: bool,
130
131 pub on_fail: Option<String>,
132 pub explicit_stage_from_cli: bool,
133 pub explicit_stage_from_config: bool,
134 pub stage: u32,
135 pub keep_stage: Vec<u32>,
136 pub keep_stage_std: Vec<u32>,
137 pub src: PathBuf,
138 pub config: Option<PathBuf>,
140 pub jobs: Option<u32>,
141 pub cmd: Subcommand,
142 pub incremental: bool,
143 pub dump_bootstrap_shims: bool,
144 pub free_args: Vec<String>,
147
148 pub download_rustc_commit: Option<String>,
150
151 pub deny_warnings: bool,
152 pub backtrace_on_ice: bool,
153
154 pub llvm_assertions: bool,
156 pub llvm_tests: bool,
157 pub llvm_enzyme: bool,
158 pub llvm_offload: bool,
159 pub llvm_plugins: bool,
160 pub llvm_optimize: bool,
161 pub llvm_thin_lto: bool,
162 pub llvm_release_debuginfo: bool,
163 pub llvm_static_stdcpp: bool,
164 pub llvm_libzstd: bool,
165 pub llvm_link_shared: Cell<Option<bool>>,
166 pub llvm_clang_cl: Option<String>,
167 pub llvm_targets: Option<String>,
168 pub llvm_experimental_targets: Option<String>,
169 pub llvm_link_jobs: Option<u32>,
170 pub llvm_version_suffix: Option<String>,
171 pub llvm_use_linker: Option<String>,
172 pub llvm_allow_old_toolchain: bool,
173 pub llvm_polly: bool,
174 pub llvm_clang: bool,
175 pub llvm_enable_warnings: bool,
176 pub llvm_from_ci: bool,
177 pub llvm_build_config: HashMap<String, String>,
178
179 pub lld_mode: LldMode,
180 pub lld_enabled: bool,
181 pub llvm_tools_enabled: bool,
182 pub llvm_bitcode_linker_enabled: bool,
183
184 pub llvm_cflags: Option<String>,
185 pub llvm_cxxflags: Option<String>,
186 pub llvm_ldflags: Option<String>,
187 pub llvm_use_libcxx: bool,
188
189 pub gcc_ci_mode: GccCiMode,
191
192 pub rust_optimize: RustOptimize,
194 pub rust_codegen_units: Option<u32>,
195 pub rust_codegen_units_std: Option<u32>,
196
197 pub rustc_debug_assertions: bool,
198 pub std_debug_assertions: bool,
199 pub tools_debug_assertions: bool,
200
201 pub rust_overflow_checks: bool,
202 pub rust_overflow_checks_std: bool,
203 pub rust_debug_logging: bool,
204 pub rust_debuginfo_level_rustc: DebuginfoLevel,
205 pub rust_debuginfo_level_std: DebuginfoLevel,
206 pub rust_debuginfo_level_tools: DebuginfoLevel,
207 pub rust_debuginfo_level_tests: DebuginfoLevel,
208 pub rust_rpath: bool,
209 pub rust_strip: bool,
210 pub rust_frame_pointers: bool,
211 pub rust_stack_protector: Option<String>,
212 pub rustc_default_linker: Option<String>,
213 pub rust_optimize_tests: bool,
214 pub rust_dist_src: bool,
215 pub rust_codegen_backends: Vec<CodegenBackendKind>,
216 pub rust_verify_llvm_ir: bool,
217 pub rust_thin_lto_import_instr_limit: Option<u32>,
218 pub rust_randomize_layout: bool,
219 pub rust_remap_debuginfo: bool,
220 pub rust_new_symbol_mangling: Option<bool>,
221 pub rust_profile_use: Option<String>,
222 pub rust_profile_generate: Option<String>,
223 pub rust_lto: RustcLto,
224 pub rust_validate_mir_opts: Option<u32>,
225 pub rust_std_features: BTreeSet<String>,
226 pub llvm_profile_use: Option<String>,
227 pub llvm_profile_generate: bool,
228 pub llvm_libunwind_default: Option<LlvmLibunwind>,
229 pub enable_bolt_settings: bool,
230
231 pub reproducible_artifacts: Vec<String>,
232
233 pub host_target: TargetSelection,
234 pub hosts: Vec<TargetSelection>,
235 pub targets: Vec<TargetSelection>,
236 pub local_rebuild: bool,
237 pub jemalloc: bool,
238 pub control_flow_guard: bool,
239 pub ehcont_guard: bool,
240
241 pub dist_sign_folder: Option<PathBuf>,
243 pub dist_upload_addr: Option<String>,
244 pub dist_compression_formats: Option<Vec<String>>,
245 pub dist_compression_profile: String,
246 pub dist_include_mingw_linker: bool,
247 pub dist_vendor: bool,
248
249 pub backtrace: bool, pub low_priority: bool,
254 pub channel: String,
255 pub description: Option<String>,
256 pub verbose_tests: bool,
257 pub save_toolstates: Option<PathBuf>,
258 pub print_step_timings: bool,
259 pub print_step_rusage: bool,
260
261 pub musl_root: Option<PathBuf>,
263 pub prefix: Option<PathBuf>,
264 pub sysconfdir: Option<PathBuf>,
265 pub datadir: Option<PathBuf>,
266 pub docdir: Option<PathBuf>,
267 pub bindir: PathBuf,
268 pub libdir: Option<PathBuf>,
269 pub mandir: Option<PathBuf>,
270 pub codegen_tests: bool,
271 pub nodejs: Option<PathBuf>,
272 pub npm: Option<PathBuf>,
273 pub gdb: Option<PathBuf>,
274 pub lldb: Option<PathBuf>,
275 pub python: Option<PathBuf>,
276 pub reuse: Option<PathBuf>,
277 pub cargo_native_static: bool,
278 pub configure_args: Vec<String>,
279 pub out: PathBuf,
280 pub rust_info: channel::GitInfo,
281
282 pub cargo_info: channel::GitInfo,
283 pub rust_analyzer_info: channel::GitInfo,
284 pub clippy_info: channel::GitInfo,
285 pub miri_info: channel::GitInfo,
286 pub rustfmt_info: channel::GitInfo,
287 pub enzyme_info: channel::GitInfo,
288 pub in_tree_llvm_info: channel::GitInfo,
289 pub in_tree_gcc_info: channel::GitInfo,
290
291 pub initial_cargo: PathBuf,
293 pub initial_rustc: PathBuf,
294 pub initial_cargo_clippy: Option<PathBuf>,
295 pub initial_sysroot: PathBuf,
296 pub initial_rustfmt: Option<PathBuf>,
297
298 pub paths: Vec<PathBuf>,
301
302 pub compiletest_diff_tool: Option<String>,
304
305 pub compiletest_allow_stage0: bool,
311
312 pub compiletest_use_stage0_libtest: bool,
314
315 pub tidy_extra_checks: Option<String>,
317 pub is_running_on_ci: bool,
318
319 pub path_modification_cache: Arc<Mutex<HashMap<Vec<&'static str>, PathFreshness>>>,
321
322 pub skip_std_check_if_no_download_rustc: bool,
326
327 pub exec_ctx: ExecutionContext,
328}
329
330impl Config {
331 #[cfg_attr(
332 feature = "tracing",
333 instrument(target = "CONFIG_HANDLING", level = "trace", name = "Config::default_opts")
334 )]
335 pub fn default_opts() -> Config {
336 #[cfg(feature = "tracing")]
337 span!(target: "CONFIG_HANDLING", tracing::Level::TRACE, "constructing default config");
338
339 Config {
340 bypass_bootstrap_lock: false,
341 llvm_optimize: true,
342 ninja_in_file: true,
343 llvm_static_stdcpp: false,
344 llvm_libzstd: false,
345 backtrace: true,
346 rust_optimize: RustOptimize::Bool(true),
347 rust_optimize_tests: true,
348 rust_randomize_layout: false,
349 submodules: None,
350 docs: true,
351 docs_minification: true,
352 rust_rpath: true,
353 rust_strip: false,
354 channel: "dev".to_string(),
355 codegen_tests: true,
356 rust_dist_src: true,
357 rust_codegen_backends: vec![CodegenBackendKind::Llvm],
358 deny_warnings: true,
359 bindir: "bin".into(),
360 dist_include_mingw_linker: true,
361 dist_compression_profile: "fast".into(),
362
363 stdout_is_tty: std::io::stdout().is_terminal(),
364 stderr_is_tty: std::io::stderr().is_terminal(),
365
366 host_target: get_host_target(),
368
369 src: {
370 let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
371 manifest_dir.parent().unwrap().parent().unwrap().to_owned()
373 },
374 out: PathBuf::from("build"),
375
376 llvm_tools_enabled: true,
379
380 ..Default::default()
381 }
382 }
383
384 pub fn set_dry_run(&mut self, dry_run: DryRun) {
385 self.exec_ctx.set_dry_run(dry_run);
386 }
387
388 pub fn get_dry_run(&self) -> &DryRun {
389 self.exec_ctx.get_dry_run()
390 }
391
392 #[cfg_attr(
393 feature = "tracing",
394 instrument(target = "CONFIG_HANDLING", level = "trace", name = "Config::parse", skip_all)
395 )]
396 pub fn parse(flags: Flags) -> Config {
397 Self::parse_inner(flags, Self::get_toml)
398 }
399
400 #[cfg_attr(
401 feature = "tracing",
402 instrument(
403 target = "CONFIG_HANDLING",
404 level = "trace",
405 name = "Config::parse_inner",
406 skip_all
407 )
408 )]
409 pub(crate) fn parse_inner(
410 flags: Flags,
411 get_toml: impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>,
412 ) -> Config {
413 let Flags {
417 cmd: flags_cmd,
418 verbose: flags_verbose,
419 incremental: flags_incremental,
420 config: flags_config,
421 build_dir: flags_build_dir,
422 build: flags_build,
423 host: flags_host,
424 target: flags_target,
425 exclude: flags_exclude,
426 skip: flags_skip,
427 include_default_paths: flags_include_default_paths,
428 rustc_error_format: flags_rustc_error_format,
429 on_fail: flags_on_fail,
430 dry_run: flags_dry_run,
431 dump_bootstrap_shims: flags_dump_bootstrap_shims,
432 stage: flags_stage,
433 keep_stage: flags_keep_stage,
434 keep_stage_std: flags_keep_stage_std,
435 src: flags_src,
436 jobs: flags_jobs,
437 warnings: flags_warnings,
438 json_output: flags_json_output,
439 compile_time_deps: flags_compile_time_deps,
440 color: flags_color,
441 bypass_bootstrap_lock: flags_bypass_bootstrap_lock,
442 rust_profile_generate: flags_rust_profile_generate,
443 rust_profile_use: flags_rust_profile_use,
444 llvm_profile_use: flags_llvm_profile_use,
445 llvm_profile_generate: flags_llvm_profile_generate,
446 enable_bolt_settings: flags_enable_bolt_settings,
447 skip_stage0_validation: flags_skip_stage0_validation,
448 reproducible_artifact: flags_reproducible_artifact,
449 paths: flags_paths,
450 set: flags_set,
451 free_args: flags_free_args,
452 ci: flags_ci,
453 skip_std_check_if_no_download_rustc: flags_skip_std_check_if_no_download_rustc,
454 } = flags;
455
456 #[cfg(feature = "tracing")]
457 span!(
458 target: "CONFIG_HANDLING",
459 tracing::Level::TRACE,
460 "collecting paths and path exclusions",
461 "flags.paths" = ?flags_paths,
462 "flags.skip" = ?flags_skip,
463 "flags.exclude" = ?flags_exclude
464 );
465
466 let mut config = Config::default_opts();
469 let exec_ctx = ExecutionContext::new(flags_verbose, flags_cmd.fail_fast());
470
471 config.exec_ctx = exec_ctx;
472
473 if let Some(src) = compute_src_directory(flags_src, &config.exec_ctx) {
474 config.src = src;
475 }
476
477 let (mut toml, toml_path) = load_toml_config(&config.src, flags_config, &get_toml);
479 config.config = toml_path.clone();
480
481 postprocess_toml(
482 &mut toml,
483 &config.src,
484 toml_path,
485 config.exec_ctx(),
486 &flags_set,
487 &get_toml,
488 );
489
490 let Build {
493 description: build_description,
494 build: mut build_build,
495 host: build_host,
496 target: build_target,
497 build_dir: build_build_dir,
498 cargo: mut build_cargo,
499 rustc: mut build_rustc,
500 rustfmt: build_rustfmt,
501 cargo_clippy: build_cargo_clippy,
502 docs: build_docs,
503 compiler_docs: build_compiler_docs,
504 library_docs_private_items: build_library_docs_private_items,
505 docs_minification: build_docs_minification,
506 submodules: build_submodules,
507 gdb: build_gdb,
508 lldb: build_lldb,
509 nodejs: build_nodejs,
510 npm: build_npm,
511 python: build_python,
512 reuse: build_reuse,
513 locked_deps: build_locked_deps,
514 vendor: build_vendor,
515 full_bootstrap: build_full_bootstrap,
516 bootstrap_cache_path: build_bootstrap_cache_path,
517 extended: build_extended,
518 tools: build_tools,
519 tool: build_tool,
520 verbose: build_verbose,
521 sanitizers: build_sanitizers,
522 profiler: build_profiler,
523 cargo_native_static: build_cargo_native_static,
524 low_priority: build_low_priority,
525 configure_args: build_configure_args,
526 local_rebuild: build_local_rebuild,
527 print_step_timings: build_print_step_timings,
528 print_step_rusage: build_print_step_rusage,
529 check_stage: build_check_stage,
530 doc_stage: build_doc_stage,
531 build_stage: build_build_stage,
532 test_stage: build_test_stage,
533 install_stage: build_install_stage,
534 dist_stage: build_dist_stage,
535 bench_stage: build_bench_stage,
536 patch_binaries_for_nix: build_patch_binaries_for_nix,
537 metrics: _,
539 android_ndk: build_android_ndk,
540 optimized_compiler_builtins: build_optimized_compiler_builtins,
541 jobs: mut build_jobs,
542 compiletest_diff_tool: build_compiletest_diff_tool,
543 compiletest_use_stage0_libtest: build_compiletest_use_stage0_libtest,
544 tidy_extra_checks: build_tidy_extra_checks,
545 ccache: build_ccache,
546 exclude: build_exclude,
547 compiletest_allow_stage0: build_compiletest_allow_stage0,
548 } = toml.build.unwrap_or_default();
549
550 let Install {
551 prefix: install_prefix,
552 sysconfdir: install_sysconfdir,
553 docdir: install_docdir,
554 bindir: install_bindir,
555 libdir: install_libdir,
556 mandir: install_mandir,
557 datadir: install_datadir,
558 } = toml.install.unwrap_or_default();
559
560 let Rust {
561 optimize: rust_optimize,
562 debug: rust_debug,
563 codegen_units: rust_codegen_units,
564 codegen_units_std: rust_codegen_units_std,
565 rustc_debug_assertions: rust_rustc_debug_assertions,
566 std_debug_assertions: rust_std_debug_assertions,
567 tools_debug_assertions: rust_tools_debug_assertions,
568 overflow_checks: rust_overflow_checks,
569 overflow_checks_std: rust_overflow_checks_std,
570 debug_logging: rust_debug_logging,
571 debuginfo_level: rust_debuginfo_level,
572 debuginfo_level_rustc: rust_debuginfo_level_rustc,
573 debuginfo_level_std: rust_debuginfo_level_std,
574 debuginfo_level_tools: rust_debuginfo_level_tools,
575 debuginfo_level_tests: rust_debuginfo_level_tests,
576 backtrace: rust_backtrace,
577 incremental: rust_incremental,
578 randomize_layout: rust_randomize_layout,
579 default_linker: rust_default_linker,
580 channel: rust_channel,
581 musl_root: rust_musl_root,
582 rpath: rust_rpath,
583 verbose_tests: rust_verbose_tests,
584 optimize_tests: rust_optimize_tests,
585 codegen_tests: rust_codegen_tests,
586 omit_git_hash: rust_omit_git_hash,
587 dist_src: rust_dist_src,
588 save_toolstates: rust_save_toolstates,
589 codegen_backends: rust_codegen_backends,
590 lld: rust_lld_enabled,
591 llvm_tools: rust_llvm_tools,
592 llvm_bitcode_linker: rust_llvm_bitcode_linker,
593 deny_warnings: rust_deny_warnings,
594 backtrace_on_ice: rust_backtrace_on_ice,
595 verify_llvm_ir: rust_verify_llvm_ir,
596 thin_lto_import_instr_limit: rust_thin_lto_import_instr_limit,
597 remap_debuginfo: rust_remap_debuginfo,
598 jemalloc: rust_jemalloc,
599 test_compare_mode: rust_test_compare_mode,
600 llvm_libunwind: rust_llvm_libunwind,
601 control_flow_guard: rust_control_flow_guard,
602 ehcont_guard: rust_ehcont_guard,
603 new_symbol_mangling: rust_new_symbol_mangling,
604 profile_generate: rust_profile_generate,
605 profile_use: rust_profile_use,
606 download_rustc: rust_download_rustc,
607 lto: rust_lto,
608 validate_mir_opts: rust_validate_mir_opts,
609 frame_pointers: rust_frame_pointers,
610 stack_protector: rust_stack_protector,
611 strip: rust_strip,
612 lld_mode: rust_lld_mode,
613 std_features: rust_std_features,
614 } = toml.rust.unwrap_or_default();
615
616 let Llvm {
617 optimize: llvm_optimize,
618 thin_lto: llvm_thin_lto,
619 release_debuginfo: llvm_release_debuginfo,
620 assertions: llvm_assertions,
621 tests: llvm_tests,
622 enzyme: llvm_enzyme,
623 plugins: llvm_plugin,
624 static_libstdcpp: llvm_static_libstdcpp,
625 libzstd: llvm_libzstd,
626 ninja: llvm_ninja,
627 targets: llvm_targets,
628 experimental_targets: llvm_experimental_targets,
629 link_jobs: llvm_link_jobs,
630 link_shared: llvm_link_shared,
631 version_suffix: llvm_version_suffix,
632 clang_cl: llvm_clang_cl,
633 cflags: llvm_cflags,
634 cxxflags: llvm_cxxflags,
635 ldflags: llvm_ldflags,
636 use_libcxx: llvm_use_libcxx,
637 use_linker: llvm_use_linker,
638 allow_old_toolchain: llvm_allow_old_toolchain,
639 offload: llvm_offload,
640 polly: llvm_polly,
641 clang: llvm_clang,
642 enable_warnings: llvm_enable_warnings,
643 download_ci_llvm: llvm_download_ci_llvm,
644 build_config: llvm_build_config,
645 } = toml.llvm.unwrap_or_default();
646
647 let Dist {
648 sign_folder: dist_sign_folder,
649 upload_addr: dist_upload_addr,
650 src_tarball: dist_src_tarball,
651 compression_formats: dist_compression_formats,
652 compression_profile: dist_compression_profile,
653 include_mingw_linker: dist_include_mingw_linker,
654 vendor: dist_vendor,
655 } = toml.dist.unwrap_or_default();
656
657 let Gcc { download_ci_gcc: gcc_download_ci_gcc } = toml.gcc.unwrap_or_default();
658
659 if cfg!(test) {
660 build_rustc = build_rustc.take().or(std::env::var_os("RUSTC").map(|p| p.into()));
666 build_cargo = build_cargo.take().or(std::env::var_os("CARGO").map(|p| p.into()));
667 }
668
669 build_jobs = flags_jobs.or(build_jobs);
670 build_build = flags_build.or(build_build);
671
672 let build_dir = flags_build_dir.or(build_build_dir.map(PathBuf::from));
673 let host = if let Some(TargetSelectionList(hosts)) = flags_host {
674 Some(hosts)
675 } else {
676 build_host
677 .map(|file_host| file_host.iter().map(|h| TargetSelection::from_user(h)).collect())
678 };
679 let target = if let Some(TargetSelectionList(targets)) = flags_target {
680 Some(targets)
681 } else {
682 build_target.map(|file_target| {
683 file_target.iter().map(|h| TargetSelection::from_user(h)).collect()
684 })
685 };
686
687 if let Some(rustc) = &build_rustc
688 && !flags_skip_stage0_validation
689 {
690 check_stage0_version(rustc, "rustc", &config.src, config.exec_ctx());
691 }
692 if let Some(cargo) = &build_cargo
693 && !flags_skip_stage0_validation
694 {
695 check_stage0_version(cargo, "cargo", &config.src, config.exec_ctx());
696 }
697
698 config
701 .exec_ctx
702 .set_verbosity(cmp::max(build_verbose.unwrap_or_default() as u8, flags_verbose));
703
704 let mut paths: Vec<PathBuf> = flags_skip.into_iter().chain(flags_exclude).collect();
705 if let Some(exclude) = build_exclude {
706 paths.extend(exclude);
707 }
708
709 config.paths = flags_paths;
711 config.include_default_paths = flags_include_default_paths;
712 config.rustc_error_format = flags_rustc_error_format;
713 config.json_output = flags_json_output;
714 config.compile_time_deps = flags_compile_time_deps;
715 config.on_fail = flags_on_fail;
716 config.cmd = flags_cmd;
717 config.incremental = flags_incremental;
718 config.set_dry_run(if flags_dry_run { DryRun::UserSelected } else { DryRun::Disabled });
719 config.dump_bootstrap_shims = flags_dump_bootstrap_shims;
720 config.keep_stage = flags_keep_stage;
721 config.keep_stage_std = flags_keep_stage_std;
722 config.color = flags_color;
723 config.free_args = flags_free_args;
724 config.llvm_profile_use = flags_llvm_profile_use;
725 config.llvm_profile_generate = flags_llvm_profile_generate;
726 config.enable_bolt_settings = flags_enable_bolt_settings;
727 config.bypass_bootstrap_lock = flags_bypass_bootstrap_lock;
728 config.is_running_on_ci = flags_ci.unwrap_or(CiEnv::is_ci());
729 config.skip_std_check_if_no_download_rustc = flags_skip_std_check_if_no_download_rustc;
730
731 if cfg!(test) {
734 config.out = Path::new(
736 &env::var_os("CARGO_TARGET_DIR").expect("cargo test directly is not supported"),
737 )
738 .parent()
739 .unwrap()
740 .to_path_buf();
741 }
742
743 config.compiletest_allow_stage0 = build_compiletest_allow_stage0.unwrap_or(false);
744 config.stage0_metadata = build_helper::stage0_parser::parse_stage0_file();
745
746 config.change_id = toml.change_id.inner;
747
748 config.skip = paths
749 .into_iter()
750 .map(|p| {
751 if cfg!(windows) {
755 PathBuf::from(p.to_str().unwrap().replace('/', "\\"))
756 } else {
757 p
758 }
759 })
760 .collect();
761
762 #[cfg(feature = "tracing")]
763 span!(
764 target: "CONFIG_HANDLING",
765 tracing::Level::TRACE,
766 "normalizing and combining `flag.skip`/`flag.exclude` paths",
767 "config.skip" = ?config.skip,
768 );
769
770 config.jobs = Some(threads_from_config(build_jobs.unwrap_or(0)));
771 if let Some(build) = build_build {
772 config.host_target = TargetSelection::from_user(&build);
773 }
774
775 set(&mut config.out, build_dir);
776 if !config.out.is_absolute() {
779 config.out = absolute(&config.out).expect("can't make empty path absolute");
781 }
782
783 if build_cargo_clippy.is_some() && build_rustc.is_none() {
784 println!(
785 "WARNING: Using `build.cargo-clippy` without `build.rustc` usually fails due to toolchain conflict."
786 );
787 }
788
789 config.initial_rustc = if let Some(rustc) = build_rustc {
790 rustc
791 } else {
792 let dwn_ctx = DownloadContext::from(&config);
793 download_beta_toolchain(dwn_ctx);
794 config
795 .out
796 .join(config.host_target)
797 .join("stage0")
798 .join("bin")
799 .join(exe("rustc", config.host_target))
800 };
801
802 config.initial_sysroot = t!(PathBuf::from_str(
803 command(&config.initial_rustc)
804 .args(["--print", "sysroot"])
805 .run_in_dry_run()
806 .run_capture_stdout(&config)
807 .stdout()
808 .trim()
809 ));
810
811 config.initial_cargo_clippy = build_cargo_clippy;
812
813 config.initial_cargo = if let Some(cargo) = build_cargo {
814 cargo
815 } else {
816 let dwn_ctx = DownloadContext::from(&config);
817 download_beta_toolchain(dwn_ctx);
818 config.initial_sysroot.join("bin").join(exe("cargo", config.host_target))
819 };
820
821 if config.dry_run() {
823 let dir = config.out.join("tmp-dry-run");
824 t!(fs::create_dir_all(&dir));
825 config.out = dir;
826 }
827
828 config.hosts = if let Some(hosts) = host { hosts } else { vec![config.host_target] };
829 config.targets = if let Some(targets) = target {
830 targets
831 } else {
832 config.hosts.clone()
835 };
836
837 config.nodejs = build_nodejs.map(PathBuf::from);
838 config.npm = build_npm.map(PathBuf::from);
839 config.gdb = build_gdb.map(PathBuf::from);
840 config.lldb = build_lldb.map(PathBuf::from);
841 config.python = build_python.map(PathBuf::from);
842 config.reuse = build_reuse.map(PathBuf::from);
843 config.submodules = build_submodules;
844 config.android_ndk = build_android_ndk;
845 config.bootstrap_cache_path = build_bootstrap_cache_path;
846 set(&mut config.low_priority, build_low_priority);
847 set(&mut config.compiler_docs, build_compiler_docs);
848 set(&mut config.library_docs_private_items, build_library_docs_private_items);
849 set(&mut config.docs_minification, build_docs_minification);
850 set(&mut config.docs, build_docs);
851 set(&mut config.locked_deps, build_locked_deps);
852 set(&mut config.full_bootstrap, build_full_bootstrap);
853 set(&mut config.extended, build_extended);
854 config.tools = build_tools;
855 set(&mut config.tool, build_tool);
856 set(&mut config.sanitizers, build_sanitizers);
857 set(&mut config.profiler, build_profiler);
858 set(&mut config.cargo_native_static, build_cargo_native_static);
859 set(&mut config.configure_args, build_configure_args);
860 set(&mut config.local_rebuild, build_local_rebuild);
861 set(&mut config.print_step_timings, build_print_step_timings);
862 set(&mut config.print_step_rusage, build_print_step_rusage);
863 config.patch_binaries_for_nix = build_patch_binaries_for_nix;
864
865 config.verbose_tests = config.is_verbose();
867
868 config.prefix = install_prefix.map(PathBuf::from);
869 config.sysconfdir = install_sysconfdir.map(PathBuf::from);
870 config.datadir = install_datadir.map(PathBuf::from);
871 config.docdir = install_docdir.map(PathBuf::from);
872 set(&mut config.bindir, install_bindir.map(PathBuf::from));
873 config.libdir = install_libdir.map(PathBuf::from);
874 config.mandir = install_mandir.map(PathBuf::from);
875
876 config.llvm_assertions = llvm_assertions.unwrap_or(false);
877
878 let file_content = t!(fs::read_to_string(config.src.join("src/ci/channel")));
879 let ci_channel = file_content.trim_end();
880
881 let is_user_configured_rust_channel = match rust_channel {
882 Some(channel) if channel == "auto-detect" => {
883 config.channel = ci_channel.into();
884 true
885 }
886 Some(channel) => {
887 config.channel = channel;
888 true
889 }
890 None => false,
891 };
892
893 let default = config.channel == "dev";
894 config.omit_git_hash = rust_omit_git_hash.unwrap_or(default);
895
896 config.rust_info = git_info(&config.exec_ctx, config.omit_git_hash, &config.src);
897 config.cargo_info =
898 git_info(&config.exec_ctx, config.omit_git_hash, &config.src.join("src/tools/cargo"));
899 config.rust_analyzer_info = git_info(
900 &config.exec_ctx,
901 config.omit_git_hash,
902 &config.src.join("src/tools/rust-analyzer"),
903 );
904 config.clippy_info =
905 git_info(&config.exec_ctx, config.omit_git_hash, &config.src.join("src/tools/clippy"));
906 config.miri_info =
907 git_info(&config.exec_ctx, config.omit_git_hash, &config.src.join("src/tools/miri"));
908 config.rustfmt_info =
909 git_info(&config.exec_ctx, config.omit_git_hash, &config.src.join("src/tools/rustfmt"));
910 config.enzyme_info =
911 git_info(&config.exec_ctx, config.omit_git_hash, &config.src.join("src/tools/enzyme"));
912 config.in_tree_llvm_info =
913 git_info(&config.exec_ctx, false, &config.src.join("src/llvm-project"));
914 config.in_tree_gcc_info = git_info(&config.exec_ctx, false, &config.src.join("src/gcc"));
915
916 config.vendor = build_vendor.unwrap_or(
917 config.rust_info.is_from_tarball()
918 && config.src.join("vendor").exists()
919 && config.src.join(".cargo/config.toml").exists(),
920 );
921
922 if !is_user_configured_rust_channel && config.rust_info.is_from_tarball() {
923 config.channel = ci_channel.into();
924 }
925
926 config.rust_profile_use = flags_rust_profile_use;
927 config.rust_profile_generate = flags_rust_profile_generate;
928
929 let debug_assertions_requested = matches!(rust_rustc_debug_assertions, Some(true))
940 || (matches!(rust_debug, Some(true))
941 && !matches!(rust_rustc_debug_assertions, Some(false)));
942
943 if debug_assertions_requested
944 && let Some(ref opt) = rust_download_rustc
945 && opt.is_string_or_true()
946 {
947 eprintln!(
948 "WARN: currently no CI rustc builds have rustc debug assertions \
949 enabled. Please either set `rust.debug-assertions` to `false` if you \
950 want to use download CI rustc or set `rust.download-rustc` to `false`."
951 );
952 }
953
954 let dwn_ctx = DownloadContext::from(&config);
955 config.download_rustc_commit =
956 download_ci_rustc_commit(dwn_ctx, rust_download_rustc, config.llvm_assertions);
957
958 if debug_assertions_requested && config.download_rustc_commit.is_some() {
959 eprintln!(
960 "WARN: `rust.debug-assertions = true` will prevent downloading CI rustc as alt CI \
961 rustc is not currently built with debug assertions."
962 );
963 config.download_rustc_commit = None;
965 }
966
967 if let Some(t) = toml.target {
968 for (triple, cfg) in t {
969 let mut target = Target::from_triple(&triple);
970
971 if let Some(ref s) = cfg.llvm_config {
972 if config.download_rustc_commit.is_some()
973 && triple == *config.host_target.triple
974 {
975 panic!(
976 "setting llvm_config for the host is incompatible with download-rustc"
977 );
978 }
979 target.llvm_config = Some(config.src.join(s));
980 }
981 if let Some(patches) = cfg.llvm_has_rust_patches {
982 assert!(
983 config.submodules == Some(false) || cfg.llvm_config.is_some(),
984 "use of `llvm-has-rust-patches` is restricted to cases where either submodules are disabled or llvm-config been provided"
985 );
986 target.llvm_has_rust_patches = Some(patches);
987 }
988 if let Some(ref s) = cfg.llvm_filecheck {
989 target.llvm_filecheck = Some(config.src.join(s));
990 }
991 target.llvm_libunwind = cfg.llvm_libunwind.as_ref().map(|v| {
992 v.parse().unwrap_or_else(|_| {
993 panic!("failed to parse target.{triple}.llvm-libunwind")
994 })
995 });
996 if let Some(s) = cfg.no_std {
997 target.no_std = s;
998 }
999 target.cc = cfg.cc.map(PathBuf::from);
1000 target.cxx = cfg.cxx.map(PathBuf::from);
1001 target.ar = cfg.ar.map(PathBuf::from);
1002 target.ranlib = cfg.ranlib.map(PathBuf::from);
1003 target.linker = cfg.linker.map(PathBuf::from);
1004 target.crt_static = cfg.crt_static;
1005 target.musl_root = cfg.musl_root.map(PathBuf::from);
1006 target.musl_libdir = cfg.musl_libdir.map(PathBuf::from);
1007 target.wasi_root = cfg.wasi_root.map(PathBuf::from);
1008 target.qemu_rootfs = cfg.qemu_rootfs.map(PathBuf::from);
1009 target.runner = cfg.runner;
1010 target.sanitizers = cfg.sanitizers;
1011 target.profiler = cfg.profiler;
1012 target.rpath = cfg.rpath;
1013 target.optimized_compiler_builtins = cfg.optimized_compiler_builtins;
1014 target.jemalloc = cfg.jemalloc;
1015 if let Some(backends) = cfg.codegen_backends {
1016 target.codegen_backends =
1017 Some(parse_codegen_backends(backends, &format!("target.{triple}")))
1018 }
1019
1020 target.split_debuginfo = cfg.split_debuginfo.as_ref().map(|v| {
1021 v.parse().unwrap_or_else(|_| {
1022 panic!("invalid value for target.{triple}.split-debuginfo")
1023 })
1024 });
1025
1026 config.target_config.insert(TargetSelection::from_user(&triple), target);
1027 }
1028 }
1029
1030 if rust_optimize.as_ref().is_some_and(|v| matches!(v, RustOptimize::Bool(false))) {
1031 eprintln!(
1032 "WARNING: setting `optimize` to `false` is known to cause errors and \
1033 should be considered unsupported. Refer to `bootstrap.example.toml` \
1034 for more details."
1035 );
1036 }
1037
1038 config.rust_new_symbol_mangling = rust_new_symbol_mangling;
1039 set(&mut config.rust_optimize_tests, rust_optimize_tests);
1040 set(&mut config.codegen_tests, rust_codegen_tests);
1041 set(&mut config.rust_rpath, rust_rpath);
1042 set(&mut config.rust_strip, rust_strip);
1043 set(&mut config.rust_frame_pointers, rust_frame_pointers);
1044 config.rust_stack_protector = rust_stack_protector;
1045 set(&mut config.jemalloc, rust_jemalloc);
1046 set(&mut config.test_compare_mode, rust_test_compare_mode);
1047 set(&mut config.backtrace, rust_backtrace);
1048 set(&mut config.rust_dist_src, rust_dist_src);
1049 set(&mut config.verbose_tests, rust_verbose_tests);
1050 if let Some(true) = rust_incremental {
1052 config.incremental = true;
1053 }
1054 set(&mut config.lld_mode, rust_lld_mode);
1055 set(&mut config.llvm_bitcode_linker_enabled, rust_llvm_bitcode_linker);
1056
1057 config.rust_randomize_layout = rust_randomize_layout.unwrap_or_default();
1058 config.llvm_tools_enabled = rust_llvm_tools.unwrap_or(true);
1059
1060 config.llvm_enzyme = config.channel == "dev" || config.channel == "nightly";
1061 config.rustc_default_linker = rust_default_linker;
1062 config.musl_root = rust_musl_root.map(PathBuf::from);
1063 config.save_toolstates = rust_save_toolstates.map(PathBuf::from);
1064 set(
1065 &mut config.deny_warnings,
1066 match flags_warnings {
1067 Warnings::Deny => Some(true),
1068 Warnings::Warn => Some(false),
1069 Warnings::Default => rust_deny_warnings,
1070 },
1071 );
1072 set(&mut config.backtrace_on_ice, rust_backtrace_on_ice);
1073 set(&mut config.rust_verify_llvm_ir, rust_verify_llvm_ir);
1074 config.rust_thin_lto_import_instr_limit = rust_thin_lto_import_instr_limit;
1075 set(&mut config.rust_remap_debuginfo, rust_remap_debuginfo);
1076 set(&mut config.control_flow_guard, rust_control_flow_guard);
1077 set(&mut config.ehcont_guard, rust_ehcont_guard);
1078 config.llvm_libunwind_default =
1079 rust_llvm_libunwind.map(|v| v.parse().expect("failed to parse rust.llvm-libunwind"));
1080 set(
1081 &mut config.rust_codegen_backends,
1082 rust_codegen_backends.map(|backends| parse_codegen_backends(backends, "rust")),
1083 );
1084
1085 config.rust_codegen_units = rust_codegen_units.map(threads_from_config);
1086 config.rust_codegen_units_std = rust_codegen_units_std.map(threads_from_config);
1087
1088 if config.rust_profile_use.is_none() {
1089 config.rust_profile_use = rust_profile_use;
1090 }
1091
1092 if config.rust_profile_generate.is_none() {
1093 config.rust_profile_generate = rust_profile_generate;
1094 }
1095
1096 config.rust_lto =
1097 rust_lto.as_deref().map(|value| RustcLto::from_str(value).unwrap()).unwrap_or_default();
1098 config.rust_validate_mir_opts = rust_validate_mir_opts;
1099
1100 config.rust_optimize = rust_optimize.unwrap_or(RustOptimize::Bool(true));
1101
1102 if default_lld_opt_in_targets().contains(&config.host_target.triple.to_string())
1115 && config.hosts == [config.host_target]
1116 {
1117 let no_llvm_config = config
1118 .target_config
1119 .get(&config.host_target)
1120 .is_none_or(|target_config| target_config.llvm_config.is_none());
1121 let enable_lld = config.llvm_from_ci || no_llvm_config;
1122 config.lld_enabled = rust_lld_enabled.unwrap_or(enable_lld);
1124 } else {
1125 set(&mut config.lld_enabled, rust_lld_enabled);
1126 }
1127
1128 let default_std_features = BTreeSet::from([String::from("panic-unwind")]);
1129 config.rust_std_features = rust_std_features.unwrap_or(default_std_features);
1130
1131 let default = rust_debug == Some(true);
1132 config.rustc_debug_assertions = rust_rustc_debug_assertions.unwrap_or(default);
1133 config.std_debug_assertions =
1134 rust_std_debug_assertions.unwrap_or(config.rustc_debug_assertions);
1135 config.tools_debug_assertions =
1136 rust_tools_debug_assertions.unwrap_or(config.rustc_debug_assertions);
1137 config.rust_overflow_checks = rust_overflow_checks.unwrap_or(default);
1138 config.rust_overflow_checks_std =
1139 rust_overflow_checks_std.unwrap_or(config.rust_overflow_checks);
1140
1141 config.rust_debug_logging = rust_debug_logging.unwrap_or(config.rustc_debug_assertions);
1142
1143 let with_defaults = |debuginfo_level_specific: Option<_>| {
1144 debuginfo_level_specific.or(rust_debuginfo_level).unwrap_or(
1145 if rust_debug == Some(true) {
1146 DebuginfoLevel::Limited
1147 } else {
1148 DebuginfoLevel::None
1149 },
1150 )
1151 };
1152 config.rust_debuginfo_level_rustc = with_defaults(rust_debuginfo_level_rustc);
1153 config.rust_debuginfo_level_std = with_defaults(rust_debuginfo_level_std);
1154 config.rust_debuginfo_level_tools = with_defaults(rust_debuginfo_level_tools);
1155 config.rust_debuginfo_level_tests =
1156 rust_debuginfo_level_tests.unwrap_or(DebuginfoLevel::None);
1157
1158 config.reproducible_artifacts = flags_reproducible_artifact;
1159 config.description = build_description;
1160
1161 if let Some(commit) = &config.download_rustc_commit
1165 && is_user_configured_rust_channel
1166 {
1167 println!(
1168 "WARNING: `rust.download-rustc` is enabled. The `rust.channel` option will be overridden by the CI rustc's channel."
1169 );
1170
1171 let dwn_ctx = DownloadContext::from(&config);
1172 let channel =
1173 read_file_by_commit(dwn_ctx, Path::new("src/ci/channel"), commit).trim().to_owned();
1174
1175 config.channel = channel;
1176 }
1177
1178 set(&mut config.ninja_in_file, llvm_ninja);
1179 set(&mut config.llvm_optimize, llvm_optimize);
1180 set(&mut config.llvm_thin_lto, llvm_thin_lto);
1181 set(&mut config.llvm_release_debuginfo, llvm_release_debuginfo);
1182 set(&mut config.llvm_static_stdcpp, llvm_static_libstdcpp);
1183 set(&mut config.llvm_libzstd, llvm_libzstd);
1184 if let Some(v) = llvm_link_shared {
1185 config.llvm_link_shared.set(Some(v));
1186 }
1187 config.llvm_targets.clone_from(&llvm_targets);
1188 config.llvm_experimental_targets.clone_from(&llvm_experimental_targets);
1189 config.llvm_link_jobs = llvm_link_jobs;
1190 config.llvm_version_suffix.clone_from(&llvm_version_suffix);
1191 config.llvm_clang_cl.clone_from(&llvm_clang_cl);
1192 config.llvm_tests = llvm_tests.unwrap_or_default();
1193 config.llvm_enzyme = llvm_enzyme.unwrap_or_default();
1194 config.llvm_plugins = llvm_plugin.unwrap_or_default();
1195
1196 config.llvm_cflags.clone_from(&llvm_cflags);
1197 config.llvm_cxxflags.clone_from(&llvm_cxxflags);
1198 config.llvm_ldflags.clone_from(&llvm_ldflags);
1199 set(&mut config.llvm_use_libcxx, llvm_use_libcxx);
1200 config.llvm_use_linker.clone_from(&llvm_use_linker);
1201 config.llvm_allow_old_toolchain = llvm_allow_old_toolchain.unwrap_or(false);
1202 config.llvm_offload = llvm_offload.unwrap_or(false);
1203 config.llvm_polly = llvm_polly.unwrap_or(false);
1204 config.llvm_clang = llvm_clang.unwrap_or(false);
1205 config.llvm_enable_warnings = llvm_enable_warnings.unwrap_or(false);
1206 config.llvm_build_config = llvm_build_config.clone().unwrap_or(Default::default());
1207
1208 let dwn_ctx = DownloadContext::from(&config);
1209 config.llvm_from_ci =
1210 parse_download_ci_llvm(dwn_ctx, llvm_download_ci_llvm, config.llvm_assertions);
1211
1212 if config.llvm_from_ci {
1213 let warn = |option: &str| {
1214 println!(
1215 "WARNING: `{option}` will only be used on `compiler/rustc_llvm` build, not for the LLVM build."
1216 );
1217 println!(
1218 "HELP: To use `{option}` for LLVM builds, set `download-ci-llvm` option to false."
1219 );
1220 };
1221
1222 if llvm_static_libstdcpp.is_some() {
1223 warn("static-libstdcpp");
1224 }
1225
1226 if llvm_link_shared.is_some() {
1227 warn("link-shared");
1228 }
1229
1230 if llvm_libzstd.is_some() {
1236 println!(
1237 "WARNING: when using `download-ci-llvm`, the local `llvm.libzstd` option, \
1238 like almost all `llvm.*` options, will be ignored and set by the LLVM CI \
1239 artifacts builder config."
1240 );
1241 println!(
1242 "HELP: To use `llvm.libzstd` for LLVM/LLD builds, set `download-ci-llvm` option to false."
1243 );
1244 }
1245 }
1246
1247 if !config.llvm_from_ci && config.llvm_thin_lto && llvm_link_shared.is_none() {
1248 config.llvm_link_shared.set(Some(true));
1252 }
1253
1254 config.gcc_ci_mode = match gcc_download_ci_gcc {
1255 Some(value) => match value {
1256 true => GccCiMode::DownloadFromCi,
1257 false => GccCiMode::BuildLocally,
1258 },
1259 None => GccCiMode::default(),
1260 };
1261
1262 match build_ccache {
1263 Some(StringOrBool::String(ref s)) => config.ccache = Some(s.to_string()),
1264 Some(StringOrBool::Bool(true)) => {
1265 config.ccache = Some("ccache".to_string());
1266 }
1267 Some(StringOrBool::Bool(false)) | None => {}
1268 }
1269
1270 if config.llvm_from_ci {
1271 let triple = &config.host_target.triple;
1272 let dwn_ctx = DownloadContext::from(&config);
1273 let ci_llvm_bin = ci_llvm_root(dwn_ctx).join("bin");
1274 let build_target = config
1275 .target_config
1276 .entry(config.host_target)
1277 .or_insert_with(|| Target::from_triple(triple));
1278
1279 check_ci_llvm!(build_target.llvm_config);
1280 check_ci_llvm!(build_target.llvm_filecheck);
1281 build_target.llvm_config =
1282 Some(ci_llvm_bin.join(exe("llvm-config", config.host_target)));
1283 build_target.llvm_filecheck =
1284 Some(ci_llvm_bin.join(exe("FileCheck", config.host_target)));
1285 }
1286
1287 config.dist_sign_folder = dist_sign_folder.map(PathBuf::from);
1288 config.dist_upload_addr = dist_upload_addr;
1289 config.dist_compression_formats = dist_compression_formats;
1290 set(&mut config.dist_compression_profile, dist_compression_profile);
1291 set(&mut config.rust_dist_src, dist_src_tarball);
1292 set(&mut config.dist_include_mingw_linker, dist_include_mingw_linker);
1293 config.dist_vendor = dist_vendor.unwrap_or_else(|| {
1294 config.rust_info.is_managed_git_subrepository() || config.rust_info.is_from_tarball()
1296 });
1297
1298 config.initial_rustfmt = if let Some(r) = build_rustfmt {
1299 Some(r)
1300 } else {
1301 let dwn_ctx = DownloadContext::from(&config);
1302 maybe_download_rustfmt(dwn_ctx)
1303 };
1304
1305 if matches!(config.lld_mode, LldMode::SelfContained)
1306 && !config.lld_enabled
1307 && flags_stage.unwrap_or(0) > 0
1308 {
1309 panic!(
1310 "Trying to use self-contained lld as a linker, but LLD is not being added to the sysroot. Enable it with rust.lld = true."
1311 );
1312 }
1313
1314 let dwn_ctx = DownloadContext::from(&config);
1315 if config.lld_enabled && is_system_llvm(dwn_ctx, config.host_target) {
1316 panic!("Cannot enable LLD with `rust.lld = true` when using external llvm-config.");
1317 }
1318
1319 config.optimized_compiler_builtins =
1320 build_optimized_compiler_builtins.unwrap_or(config.channel != "dev");
1321 config.compiletest_diff_tool = build_compiletest_diff_tool;
1322 config.compiletest_use_stage0_libtest =
1323 build_compiletest_use_stage0_libtest.unwrap_or(true);
1324 config.tidy_extra_checks = build_tidy_extra_checks;
1325
1326 let download_rustc = config.download_rustc_commit.is_some();
1327 config.explicit_stage_from_cli = flags_stage.is_some();
1328 config.explicit_stage_from_config = build_test_stage.is_some()
1329 || build_build_stage.is_some()
1330 || build_doc_stage.is_some()
1331 || build_dist_stage.is_some()
1332 || build_install_stage.is_some()
1333 || build_check_stage.is_some()
1334 || build_bench_stage.is_some();
1335
1336 config.stage = match config.cmd {
1337 Subcommand::Check { .. } => flags_stage.or(build_check_stage).unwrap_or(1),
1338 Subcommand::Clippy { .. } | Subcommand::Fix => {
1339 flags_stage.or(build_check_stage).unwrap_or(1)
1340 }
1341 Subcommand::Doc { .. } => {
1343 flags_stage.or(build_doc_stage).unwrap_or(if download_rustc { 2 } else { 1 })
1344 }
1345 Subcommand::Build => {
1346 flags_stage.or(build_build_stage).unwrap_or(if download_rustc { 2 } else { 1 })
1347 }
1348 Subcommand::Test { .. } | Subcommand::Miri { .. } => {
1349 flags_stage.or(build_test_stage).unwrap_or(if download_rustc { 2 } else { 1 })
1350 }
1351 Subcommand::Bench { .. } => flags_stage.or(build_bench_stage).unwrap_or(2),
1352 Subcommand::Dist => flags_stage.or(build_dist_stage).unwrap_or(2),
1353 Subcommand::Install => flags_stage.or(build_install_stage).unwrap_or(2),
1354 Subcommand::Perf { .. } => flags_stage.unwrap_or(1),
1355 Subcommand::Clean { .. }
1358 | Subcommand::Run { .. }
1359 | Subcommand::Setup { .. }
1360 | Subcommand::Format { .. }
1361 | Subcommand::Vendor { .. } => flags_stage.unwrap_or(0),
1362 };
1363
1364 match (config.stage, &config.cmd) {
1366 (0, Subcommand::Build) => {
1367 eprintln!("ERROR: cannot build anything on stage 0. Use at least stage 1.");
1368 exit!(1);
1369 }
1370 (0, Subcommand::Check { .. }) => {
1371 eprintln!("ERROR: cannot check anything on stage 0. Use at least stage 1.");
1372 exit!(1);
1373 }
1374 (0, Subcommand::Doc { .. }) => {
1375 eprintln!("ERROR: cannot document anything on stage 0. Use at least stage 1.");
1376 exit!(1);
1377 }
1378 (0, Subcommand::Clippy { .. }) => {
1379 eprintln!("ERROR: cannot run clippy on stage 0. Use at least stage 1.");
1380 exit!(1);
1381 }
1382 _ => {}
1383 }
1384
1385 if config.compile_time_deps && !matches!(config.cmd, Subcommand::Check { .. }) {
1386 eprintln!(
1387 "WARNING: Can't use --compile-time-deps with any subcommand other than check."
1388 );
1389 exit!(1);
1390 }
1391
1392 #[cfg(not(test))]
1394 if flags_stage.is_none() && config.is_running_on_ci {
1395 match config.cmd {
1396 Subcommand::Test { .. }
1397 | Subcommand::Miri { .. }
1398 | Subcommand::Doc { .. }
1399 | Subcommand::Build
1400 | Subcommand::Bench { .. }
1401 | Subcommand::Dist
1402 | Subcommand::Install => {
1403 assert_eq!(
1404 config.stage, 2,
1405 "x.py should be run with `--stage 2` on CI, but was run with `--stage {}`",
1406 config.stage,
1407 );
1408 }
1409 Subcommand::Clean { .. }
1410 | Subcommand::Check { .. }
1411 | Subcommand::Clippy { .. }
1412 | Subcommand::Fix
1413 | Subcommand::Run { .. }
1414 | Subcommand::Setup { .. }
1415 | Subcommand::Format { .. }
1416 | Subcommand::Vendor { .. }
1417 | Subcommand::Perf { .. } => {}
1418 }
1419 }
1420
1421 config
1422 }
1423
1424 pub fn dry_run(&self) -> bool {
1425 self.exec_ctx.dry_run()
1426 }
1427
1428 pub fn is_explicit_stage(&self) -> bool {
1429 self.explicit_stage_from_cli || self.explicit_stage_from_config
1430 }
1431
1432 pub(crate) fn test_args(&self) -> Vec<&str> {
1433 let mut test_args = match self.cmd {
1434 Subcommand::Test { ref test_args, .. }
1435 | Subcommand::Bench { ref test_args, .. }
1436 | Subcommand::Miri { ref test_args, .. } => {
1437 test_args.iter().flat_map(|s| s.split_whitespace()).collect()
1438 }
1439 _ => vec![],
1440 };
1441 test_args.extend(self.free_args.iter().map(|s| s.as_str()));
1442 test_args
1443 }
1444
1445 pub(crate) fn args(&self) -> Vec<&str> {
1446 let mut args = match self.cmd {
1447 Subcommand::Run { ref args, .. } => {
1448 args.iter().flat_map(|s| s.split_whitespace()).collect()
1449 }
1450 _ => vec![],
1451 };
1452 args.extend(self.free_args.iter().map(|s| s.as_str()));
1453 args
1454 }
1455
1456 pub(crate) fn read_file_by_commit(&self, file: &Path, commit: &str) -> String {
1458 let dwn_ctx = DownloadContext::from(self);
1459 read_file_by_commit(dwn_ctx, file, commit)
1460 }
1461
1462 pub(crate) fn artifact_version_part(&self, commit: &str) -> String {
1465 let (channel, version) = if self.rust_info.is_managed_git_subrepository() {
1466 let channel =
1467 self.read_file_by_commit(Path::new("src/ci/channel"), commit).trim().to_owned();
1468 let version =
1469 self.read_file_by_commit(Path::new("src/version"), commit).trim().to_owned();
1470 (channel, version)
1471 } else {
1472 let channel = fs::read_to_string(self.src.join("src/ci/channel"));
1473 let version = fs::read_to_string(self.src.join("src/version"));
1474 match (channel, version) {
1475 (Ok(channel), Ok(version)) => {
1476 (channel.trim().to_owned(), version.trim().to_owned())
1477 }
1478 (channel, version) => {
1479 let src = self.src.display();
1480 eprintln!("ERROR: failed to determine artifact channel and/or version");
1481 eprintln!(
1482 "HELP: consider using a git checkout or ensure these files are readable"
1483 );
1484 if let Err(channel) = channel {
1485 eprintln!("reading {src}/src/ci/channel failed: {channel:?}");
1486 }
1487 if let Err(version) = version {
1488 eprintln!("reading {src}/src/version failed: {version:?}");
1489 }
1490 panic!();
1491 }
1492 }
1493 };
1494
1495 match channel.as_str() {
1496 "stable" => version,
1497 "beta" => channel,
1498 "nightly" => channel,
1499 other => unreachable!("{:?} is not recognized as a valid channel", other),
1500 }
1501 }
1502
1503 pub fn bindir_relative(&self) -> &Path {
1505 let bindir = &self.bindir;
1506 if bindir.is_absolute() {
1507 if let Some(prefix) = &self.prefix
1509 && let Ok(stripped) = bindir.strip_prefix(prefix)
1510 {
1511 return stripped;
1512 }
1513 }
1514 bindir
1515 }
1516
1517 pub fn libdir_relative(&self) -> Option<&Path> {
1519 let libdir = self.libdir.as_ref()?;
1520 if libdir.is_relative() {
1521 Some(libdir)
1522 } else {
1523 libdir.strip_prefix(self.prefix.as_ref()?).ok()
1525 }
1526 }
1527
1528 pub(crate) fn ci_llvm_root(&self) -> PathBuf {
1530 let dwn_ctx = DownloadContext::from(self);
1531 ci_llvm_root(dwn_ctx)
1532 }
1533
1534 pub(crate) fn ci_rustc_dir(&self) -> PathBuf {
1536 assert!(self.download_rustc());
1537 self.out.join(self.host_target).join("ci-rustc")
1538 }
1539
1540 pub(crate) fn llvm_link_shared(&self) -> bool {
1545 let mut opt = self.llvm_link_shared.get();
1546 if opt.is_none() && self.dry_run() {
1547 return false;
1549 }
1550
1551 let llvm_link_shared = *opt.get_or_insert_with(|| {
1552 if self.llvm_from_ci {
1553 self.maybe_download_ci_llvm();
1554 let ci_llvm = self.ci_llvm_root();
1555 let link_type = t!(
1556 std::fs::read_to_string(ci_llvm.join("link-type.txt")),
1557 format!("CI llvm missing: {}", ci_llvm.display())
1558 );
1559 link_type == "dynamic"
1560 } else {
1561 false
1564 }
1565 });
1566 self.llvm_link_shared.set(opt);
1567 llvm_link_shared
1568 }
1569
1570 pub(crate) fn download_rustc(&self) -> bool {
1572 self.download_rustc_commit().is_some()
1573 }
1574
1575 pub(crate) fn download_rustc_commit(&self) -> Option<&str> {
1576 static DOWNLOAD_RUSTC: OnceLock<Option<String>> = OnceLock::new();
1577 if self.dry_run() && DOWNLOAD_RUSTC.get().is_none() {
1578 return self.download_rustc_commit.as_deref();
1580 }
1581
1582 DOWNLOAD_RUSTC
1583 .get_or_init(|| match &self.download_rustc_commit {
1584 None => None,
1585 Some(commit) => {
1586 self.download_ci_rustc(commit);
1587
1588 if !self.llvm_from_ci {
1592 if self.is_running_on_ci {
1595 println!("WARNING: LLVM submodule has changes, `download-rustc` will be disabled.");
1596 return None;
1597 } else {
1598 panic!("ERROR: LLVM submodule has changes, `download-rustc` can't be used.");
1599 }
1600 }
1601
1602 if let Some(config_path) = &self.config {
1603 let ci_config_toml = match self.get_builder_toml("ci-rustc") {
1604 Ok(ci_config_toml) => ci_config_toml,
1605 Err(e) if e.to_string().contains("unknown field") => {
1606 println!("WARNING: CI rustc has some fields that are no longer supported in bootstrap; download-rustc will be disabled.");
1607 println!("HELP: Consider rebasing to a newer commit if available.");
1608 return None;
1609 },
1610 Err(e) => {
1611 eprintln!("ERROR: Failed to parse CI rustc bootstrap.toml: {e}");
1612 exit!(2);
1613 },
1614 };
1615
1616 let current_config_toml = Self::get_toml(config_path).unwrap();
1617
1618 let res = check_incompatible_options_for_ci_rustc(
1621 self.host_target,
1622 current_config_toml,
1623 ci_config_toml,
1624 );
1625
1626 let disable_ci_rustc_if_incompatible = env::var_os("DISABLE_CI_RUSTC_IF_INCOMPATIBLE")
1629 .is_some_and(|s| s == "1" || s == "true");
1630
1631 if disable_ci_rustc_if_incompatible && res.is_err() {
1632 println!("WARNING: download-rustc is disabled with `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` env.");
1633 return None;
1634 }
1635
1636 res.unwrap();
1637 }
1638
1639 Some(commit.clone())
1640 }
1641 })
1642 .as_deref()
1643 }
1644
1645 pub fn verbose(&self, f: impl Fn()) {
1647 self.exec_ctx.verbose(f);
1648 }
1649
1650 pub fn any_sanitizers_to_build(&self) -> bool {
1651 self.target_config
1652 .iter()
1653 .any(|(ts, t)| !ts.is_msvc() && t.sanitizers.unwrap_or(self.sanitizers))
1654 }
1655
1656 pub fn any_profiler_enabled(&self) -> bool {
1657 self.target_config.values().any(|t| matches!(&t.profiler, Some(p) if p.is_string_or_true()))
1658 || self.profiler
1659 }
1660
1661 pub fn submodules(&self) -> bool {
1663 self.submodules.unwrap_or(self.rust_info.is_managed_git_subrepository())
1666 }
1667
1668 pub fn git_config(&self) -> GitConfig<'_> {
1669 GitConfig {
1670 nightly_branch: &self.stage0_metadata.config.nightly_branch,
1671 git_merge_commit_email: &self.stage0_metadata.config.git_merge_commit_email,
1672 }
1673 }
1674
1675 #[cfg_attr(
1685 feature = "tracing",
1686 instrument(
1687 level = "trace",
1688 name = "Config::update_submodule",
1689 skip_all,
1690 fields(relative_path = ?relative_path),
1691 ),
1692 )]
1693 pub(crate) fn update_submodule(&self, relative_path: &str) {
1694 let dwn_ctx = DownloadContext::from(self);
1695 update_submodule(dwn_ctx, relative_path);
1696 }
1697
1698 pub fn has_changes_from_upstream(&self, paths: &[&'static str]) -> bool {
1700 let dwn_ctx = DownloadContext::from(self);
1701 has_changes_from_upstream(dwn_ctx, paths)
1702 }
1703
1704 pub fn check_path_modifications(&self, paths: &[&'static str]) -> PathFreshness {
1706 self.path_modification_cache
1712 .lock()
1713 .unwrap()
1714 .entry(paths.to_vec())
1715 .or_insert_with(|| {
1716 check_path_modifications(&self.src, &self.git_config(), paths, CiEnv::current())
1717 .unwrap()
1718 })
1719 .clone()
1720 }
1721
1722 pub fn sanitizers_enabled(&self, target: TargetSelection) -> bool {
1723 self.target_config.get(&target).and_then(|t| t.sanitizers).unwrap_or(self.sanitizers)
1724 }
1725
1726 pub fn needs_sanitizer_runtime_built(&self, target: TargetSelection) -> bool {
1727 !target.is_msvc() && self.sanitizers_enabled(target)
1729 }
1730
1731 pub fn profiler_path(&self, target: TargetSelection) -> Option<&str> {
1732 match self.target_config.get(&target)?.profiler.as_ref()? {
1733 StringOrBool::String(s) => Some(s),
1734 StringOrBool::Bool(_) => None,
1735 }
1736 }
1737
1738 pub fn profiler_enabled(&self, target: TargetSelection) -> bool {
1739 self.target_config
1740 .get(&target)
1741 .and_then(|t| t.profiler.as_ref())
1742 .map(StringOrBool::is_string_or_true)
1743 .unwrap_or(self.profiler)
1744 }
1745
1746 pub fn enabled_codegen_backends(&self, target: TargetSelection) -> &[CodegenBackendKind] {
1750 self.target_config
1751 .get(&target)
1752 .and_then(|cfg| cfg.codegen_backends.as_deref())
1753 .unwrap_or(&self.rust_codegen_backends)
1754 }
1755
1756 pub fn default_codegen_backend(&self, target: TargetSelection) -> Option<CodegenBackendKind> {
1759 self.enabled_codegen_backends(target).first().cloned()
1760 }
1761
1762 pub fn jemalloc(&self, target: TargetSelection) -> bool {
1763 self.target_config.get(&target).and_then(|cfg| cfg.jemalloc).unwrap_or(self.jemalloc)
1764 }
1765
1766 pub fn rpath_enabled(&self, target: TargetSelection) -> bool {
1767 self.target_config.get(&target).and_then(|t| t.rpath).unwrap_or(self.rust_rpath)
1768 }
1769
1770 pub fn optimized_compiler_builtins(&self, target: TargetSelection) -> bool {
1771 self.target_config
1772 .get(&target)
1773 .and_then(|t| t.optimized_compiler_builtins)
1774 .unwrap_or(self.optimized_compiler_builtins)
1775 }
1776
1777 pub fn llvm_enabled(&self, target: TargetSelection) -> bool {
1778 self.enabled_codegen_backends(target).contains(&CodegenBackendKind::Llvm)
1779 }
1780
1781 pub fn llvm_libunwind(&self, target: TargetSelection) -> LlvmLibunwind {
1782 self.target_config
1783 .get(&target)
1784 .and_then(|t| t.llvm_libunwind)
1785 .or(self.llvm_libunwind_default)
1786 .unwrap_or(if target.contains("fuchsia") {
1787 LlvmLibunwind::InTree
1788 } else {
1789 LlvmLibunwind::No
1790 })
1791 }
1792
1793 pub fn split_debuginfo(&self, target: TargetSelection) -> SplitDebuginfo {
1794 self.target_config
1795 .get(&target)
1796 .and_then(|t| t.split_debuginfo)
1797 .unwrap_or_else(|| SplitDebuginfo::default_for_platform(target))
1798 }
1799
1800 pub fn is_host_target(&self, target: TargetSelection) -> bool {
1802 self.host_target == target
1803 }
1804
1805 pub fn is_system_llvm(&self, target: TargetSelection) -> bool {
1810 let dwn_ctx = DownloadContext::from(self);
1811 is_system_llvm(dwn_ctx, target)
1812 }
1813
1814 pub fn is_rust_llvm(&self, target: TargetSelection) -> bool {
1818 match self.target_config.get(&target) {
1819 Some(Target { llvm_has_rust_patches: Some(patched), .. }) => *patched,
1823 _ => !self.is_system_llvm(target),
1826 }
1827 }
1828
1829 pub fn exec_ctx(&self) -> &ExecutionContext {
1830 &self.exec_ctx
1831 }
1832
1833 pub fn git_info(&self, omit_git_hash: bool, dir: &Path) -> GitInfo {
1834 GitInfo::new(omit_git_hash, dir, self)
1835 }
1836}
1837
1838impl AsRef<ExecutionContext> for Config {
1839 fn as_ref(&self) -> &ExecutionContext {
1840 &self.exec_ctx
1841 }
1842}
1843
1844fn compute_src_directory(src_dir: Option<PathBuf>, exec_ctx: &ExecutionContext) -> Option<PathBuf> {
1845 if let Some(src) = src_dir {
1846 return Some(src);
1847 } else {
1848 let mut cmd = helpers::git(None);
1851 cmd.arg("rev-parse").arg("--show-cdup");
1859 let output = cmd.allow_failure().run_capture_stdout(exec_ctx);
1861 if output.is_success() {
1862 let git_root_relative = output.stdout();
1863 let git_root = env::current_dir()
1866 .unwrap()
1867 .join(PathBuf::from(git_root_relative.trim()))
1868 .canonicalize()
1869 .unwrap();
1870 let s = git_root.to_str().unwrap();
1871
1872 let git_root = match s.strip_prefix("\\\\?\\") {
1874 Some(p) => PathBuf::from(p),
1875 None => git_root,
1876 };
1877 if git_root.join("src").join("stage0").exists() {
1884 return Some(git_root);
1885 }
1886 } else {
1887 }
1890 };
1891 None
1892}
1893
1894fn load_toml_config(
1899 src: &Path,
1900 config_path: Option<PathBuf>,
1901 get_toml: &impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>,
1902) -> (TomlConfig, Option<PathBuf>) {
1903 let toml_path = config_path.or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from));
1911 let using_default_path = toml_path.is_none();
1912 let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("bootstrap.toml"));
1913
1914 if using_default_path && !toml_path.exists() {
1915 toml_path = src.join(PathBuf::from("bootstrap.toml"));
1916 if !toml_path.exists() {
1917 toml_path = PathBuf::from("config.toml");
1918 if !toml_path.exists() {
1919 toml_path = src.join(PathBuf::from("config.toml"));
1920 }
1921 }
1922 }
1923
1924 if !using_default_path || toml_path.exists() {
1927 let path = Some(if cfg!(not(test)) {
1928 toml_path = toml_path.canonicalize().unwrap();
1929 toml_path.clone()
1930 } else {
1931 toml_path.clone()
1932 });
1933 (
1934 get_toml(&toml_path).unwrap_or_else(|e| {
1935 eprintln!("ERROR: Failed to parse '{}': {e}", toml_path.display());
1936 exit!(2);
1937 }),
1938 path,
1939 )
1940 } else {
1941 (TomlConfig::default(), None)
1942 }
1943}
1944
1945fn postprocess_toml(
1946 toml: &mut TomlConfig,
1947 src_dir: &Path,
1948 toml_path: Option<PathBuf>,
1949 exec_ctx: &ExecutionContext,
1950 override_set: &[String],
1951 get_toml: &impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>,
1952) {
1953 let git_info = GitInfo::new(false, src_dir, exec_ctx);
1954
1955 if git_info.is_from_tarball() && toml.profile.is_none() {
1956 toml.profile = Some("dist".into());
1957 }
1958
1959 for include_path in toml.include.clone().unwrap_or_default().iter().rev() {
1965 let include_path = toml_path
1966 .as_ref()
1967 .expect("include found in default TOML config")
1968 .parent()
1969 .unwrap()
1970 .join(include_path);
1971
1972 let included_toml = get_toml(&include_path).unwrap_or_else(|e| {
1973 eprintln!("ERROR: Failed to parse '{}': {e}", include_path.display());
1974 exit!(2);
1975 });
1976 toml.merge(
1977 Some(include_path),
1978 &mut Default::default(),
1979 included_toml,
1980 ReplaceOpt::IgnoreDuplicate,
1981 );
1982 }
1983
1984 if let Some(include) = &toml.profile {
1985 let profile_aliases = HashMap::from([("user", "dist")]);
1989 let include = match profile_aliases.get(include.as_str()) {
1990 Some(alias) => alias,
1991 None => include.as_str(),
1992 };
1993 let mut include_path = PathBuf::from(src_dir);
1994 include_path.push("src");
1995 include_path.push("bootstrap");
1996 include_path.push("defaults");
1997 include_path.push(format!("bootstrap.{include}.toml"));
1998 let included_toml = get_toml(&include_path).unwrap_or_else(|e| {
1999 eprintln!(
2000 "ERROR: Failed to parse default config profile at '{}': {e}",
2001 include_path.display()
2002 );
2003 exit!(2);
2004 });
2005 toml.merge(
2006 Some(include_path),
2007 &mut Default::default(),
2008 included_toml,
2009 ReplaceOpt::IgnoreDuplicate,
2010 );
2011 }
2012
2013 let mut override_toml = TomlConfig::default();
2014 for option in override_set.iter() {
2015 fn get_table(option: &str) -> Result<TomlConfig, toml::de::Error> {
2016 toml::from_str(option).and_then(|table: toml::Value| TomlConfig::deserialize(table))
2017 }
2018
2019 let mut err = match get_table(option) {
2020 Ok(v) => {
2021 override_toml.merge(None, &mut Default::default(), v, ReplaceOpt::ErrorOnDuplicate);
2022 continue;
2023 }
2024 Err(e) => e,
2025 };
2026 if let Some((key, value)) = option.split_once('=')
2029 && !value.contains('"')
2030 {
2031 match get_table(&format!(r#"{key}="{value}""#)) {
2032 Ok(v) => {
2033 override_toml.merge(
2034 None,
2035 &mut Default::default(),
2036 v,
2037 ReplaceOpt::ErrorOnDuplicate,
2038 );
2039 continue;
2040 }
2041 Err(e) => err = e,
2042 }
2043 }
2044 eprintln!("failed to parse override `{option}`: `{err}");
2045 exit!(2)
2046 }
2047 toml.merge(None, &mut Default::default(), override_toml, ReplaceOpt::Override);
2048}
2049
2050#[cfg(test)]
2051pub fn check_stage0_version(
2052 _program_path: &Path,
2053 _component_name: &'static str,
2054 _src_dir: &Path,
2055 _exec_ctx: &ExecutionContext,
2056) {
2057}
2058
2059#[cfg(not(test))]
2061pub fn check_stage0_version(
2062 program_path: &Path,
2063 component_name: &'static str,
2064 src_dir: &Path,
2065 exec_ctx: &ExecutionContext,
2066) {
2067 use build_helper::util::fail;
2068
2069 if exec_ctx.dry_run() {
2070 return;
2071 }
2072
2073 let stage0_output =
2074 command(program_path).arg("--version").run_capture_stdout(exec_ctx).stdout();
2075 let mut stage0_output = stage0_output.lines().next().unwrap().split(' ');
2076
2077 let stage0_name = stage0_output.next().unwrap();
2078 if stage0_name != component_name {
2079 fail(&format!(
2080 "Expected to find {component_name} at {} but it claims to be {stage0_name}",
2081 program_path.display()
2082 ));
2083 }
2084
2085 let stage0_version =
2086 semver::Version::parse(stage0_output.next().unwrap().split('-').next().unwrap().trim())
2087 .unwrap();
2088 let source_version =
2089 semver::Version::parse(fs::read_to_string(src_dir.join("src/version")).unwrap().trim())
2090 .unwrap();
2091 if !(source_version == stage0_version
2092 || (source_version.major == stage0_version.major
2093 && (source_version.minor == stage0_version.minor
2094 || source_version.minor == stage0_version.minor + 1)))
2095 {
2096 let prev_version = format!("{}.{}.x", source_version.major, source_version.minor - 1);
2097 fail(&format!(
2098 "Unexpected {component_name} version: {stage0_version}, we should use {prev_version}/{source_version} to build source with {source_version}"
2099 ));
2100 }
2101}
2102
2103pub fn download_ci_rustc_commit<'a>(
2104 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2105 download_rustc: Option<StringOrBool>,
2106 llvm_assertions: bool,
2107) -> Option<String> {
2108 let dwn_ctx = dwn_ctx.as_ref();
2109
2110 if !is_download_ci_available(&dwn_ctx.host_target.triple, llvm_assertions) {
2111 return None;
2112 }
2113
2114 let if_unchanged = match download_rustc {
2116 None | Some(StringOrBool::Bool(false)) => return None,
2122 Some(StringOrBool::Bool(true)) => false,
2123 Some(StringOrBool::String(s)) if s == "if-unchanged" => {
2124 if !dwn_ctx.rust_info.is_managed_git_subrepository() {
2125 println!(
2126 "ERROR: `download-rustc=if-unchanged` is only compatible with Git managed sources."
2127 );
2128 crate::exit!(1);
2129 }
2130
2131 true
2132 }
2133 Some(StringOrBool::String(other)) => {
2134 panic!("unrecognized option for download-rustc: {other}")
2135 }
2136 };
2137
2138 let commit = if dwn_ctx.rust_info.is_managed_git_subrepository() {
2139 let freshness = check_path_modifications_(dwn_ctx, RUSTC_IF_UNCHANGED_ALLOWED_PATHS);
2142 dwn_ctx.exec_ctx.verbose(|| {
2143 eprintln!("rustc freshness: {freshness:?}");
2144 });
2145 match freshness {
2146 PathFreshness::LastModifiedUpstream { upstream } => upstream,
2147 PathFreshness::HasLocalModifications { upstream } => {
2148 if if_unchanged {
2149 return None;
2150 }
2151
2152 if dwn_ctx.is_running_on_ci {
2153 eprintln!("CI rustc commit matches with HEAD and we are in CI.");
2154 eprintln!(
2155 "`rustc.download-ci` functionality will be skipped as artifacts are not available."
2156 );
2157 return None;
2158 }
2159
2160 upstream
2161 }
2162 PathFreshness::MissingUpstream => {
2163 eprintln!("No upstream commit found");
2164 return None;
2165 }
2166 }
2167 } else {
2168 channel::read_commit_info_file(dwn_ctx.src)
2169 .map(|info| info.sha.trim().to_owned())
2170 .expect("git-commit-info is missing in the project root")
2171 };
2172
2173 Some(commit)
2174}
2175
2176pub fn check_path_modifications_<'a>(
2177 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2178 paths: &[&'static str],
2179) -> PathFreshness {
2180 let dwn_ctx = dwn_ctx.as_ref();
2181 dwn_ctx
2187 .path_modification_cache
2188 .lock()
2189 .unwrap()
2190 .entry(paths.to_vec())
2191 .or_insert_with(|| {
2192 check_path_modifications(
2193 dwn_ctx.src,
2194 &git_config(dwn_ctx.stage0_metadata),
2195 paths,
2196 CiEnv::current(),
2197 )
2198 .unwrap()
2199 })
2200 .clone()
2201}
2202
2203pub fn git_config(stage0_metadata: &build_helper::stage0_parser::Stage0) -> GitConfig<'_> {
2204 GitConfig {
2205 nightly_branch: &stage0_metadata.config.nightly_branch,
2206 git_merge_commit_email: &stage0_metadata.config.git_merge_commit_email,
2207 }
2208}
2209
2210pub fn parse_download_ci_llvm<'a>(
2211 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2212 download_ci_llvm: Option<StringOrBool>,
2213 asserts: bool,
2214) -> bool {
2215 let dwn_ctx = dwn_ctx.as_ref();
2216
2217 let default = if dwn_ctx.is_running_on_ci {
2220 StringOrBool::String("if-unchanged".to_string())
2221 } else {
2222 StringOrBool::Bool(true)
2223 };
2224 let download_ci_llvm = download_ci_llvm.unwrap_or(default);
2225
2226 let if_unchanged = || {
2227 if dwn_ctx.rust_info.is_from_tarball() {
2228 println!("ERROR: 'if-unchanged' is only compatible with Git managed sources.");
2230 crate::exit!(1);
2231 }
2232
2233 #[cfg(not(test))]
2235 update_submodule(dwn_ctx, "src/llvm-project");
2236
2237 let has_changes = has_changes_from_upstream(dwn_ctx, LLVM_INVALIDATION_PATHS);
2239
2240 if has_changes {
2242 false
2243 } else {
2244 llvm::is_ci_llvm_available_for_target(&dwn_ctx.host_target, asserts)
2245 }
2246 };
2247
2248 match download_ci_llvm {
2249 StringOrBool::Bool(b) => {
2250 if !b && dwn_ctx.download_rustc_commit.is_some() {
2251 panic!(
2252 "`llvm.download-ci-llvm` cannot be set to `false` if `rust.download-rustc` is set to `true` or `if-unchanged`."
2253 );
2254 }
2255
2256 if b && dwn_ctx.is_running_on_ci {
2257 panic!(
2259 "`llvm.download-ci-llvm` cannot be set to `true` on CI. Use `if-unchanged` instead."
2260 );
2261 }
2262
2263 b && llvm::is_ci_llvm_available_for_target(&dwn_ctx.host_target, asserts)
2265 }
2266 StringOrBool::String(s) if s == "if-unchanged" => if_unchanged(),
2267 StringOrBool::String(other) => {
2268 panic!("unrecognized option for download-ci-llvm: {other:?}")
2269 }
2270 }
2271}
2272
2273pub fn has_changes_from_upstream<'a>(
2274 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2275 paths: &[&'static str],
2276) -> bool {
2277 let dwn_ctx = dwn_ctx.as_ref();
2278 match check_path_modifications_(dwn_ctx, paths) {
2279 PathFreshness::LastModifiedUpstream { .. } => false,
2280 PathFreshness::HasLocalModifications { .. } | PathFreshness::MissingUpstream => true,
2281 }
2282}
2283
2284#[cfg_attr(
2285 feature = "tracing",
2286 instrument(
2287 level = "trace",
2288 name = "Config::update_submodule",
2289 skip_all,
2290 fields(relative_path = ?relative_path),
2291 ),
2292)]
2293pub(crate) fn update_submodule<'a>(dwn_ctx: impl AsRef<DownloadContext<'a>>, relative_path: &str) {
2294 let dwn_ctx = dwn_ctx.as_ref();
2295 if dwn_ctx.rust_info.is_from_tarball() || !submodules_(dwn_ctx.submodules, dwn_ctx.rust_info) {
2296 return;
2297 }
2298
2299 let absolute_path = dwn_ctx.src.join(relative_path);
2300
2301 if !absolute_path.exists() {
2305 t!(fs::create_dir_all(&absolute_path));
2306 }
2307
2308 if !git_info(dwn_ctx.exec_ctx, false, &absolute_path).is_managed_git_subrepository()
2311 && !helpers::dir_is_empty(&absolute_path)
2312 {
2313 return;
2314 }
2315
2316 let submodule_git = || {
2323 let mut cmd = helpers::git(Some(&absolute_path));
2324 cmd.run_in_dry_run();
2325 cmd
2326 };
2327
2328 let checked_out_hash =
2330 submodule_git().args(["rev-parse", "HEAD"]).run_capture_stdout(dwn_ctx.exec_ctx).stdout();
2331 let checked_out_hash = checked_out_hash.trim_end();
2332 let recorded = helpers::git(Some(dwn_ctx.src))
2334 .run_in_dry_run()
2335 .args(["ls-tree", "HEAD"])
2336 .arg(relative_path)
2337 .run_capture_stdout(dwn_ctx.exec_ctx)
2338 .stdout();
2339
2340 let actual_hash = recorded
2341 .split_whitespace()
2342 .nth(2)
2343 .unwrap_or_else(|| panic!("unexpected output `{recorded}`"));
2344
2345 if actual_hash == checked_out_hash {
2346 return;
2348 }
2349
2350 println!("Updating submodule {relative_path}");
2351
2352 helpers::git(Some(dwn_ctx.src))
2353 .allow_failure()
2354 .run_in_dry_run()
2355 .args(["submodule", "-q", "sync"])
2356 .arg(relative_path)
2357 .run(dwn_ctx.exec_ctx);
2358
2359 let update = |progress: bool| {
2361 let current_branch = helpers::git(Some(dwn_ctx.src))
2364 .allow_failure()
2365 .run_in_dry_run()
2366 .args(["symbolic-ref", "--short", "HEAD"])
2367 .run_capture(dwn_ctx.exec_ctx);
2368
2369 let mut git = helpers::git(Some(dwn_ctx.src)).allow_failure();
2370 git.run_in_dry_run();
2371 if current_branch.is_success() {
2372 let branch = current_branch.stdout();
2375 let branch = branch.trim();
2376 let branch = branch.strip_prefix("heads/").unwrap_or(branch);
2377 git.arg("-c").arg(format!("branch.{branch}.remote=origin"));
2378 }
2379 git.args(["submodule", "update", "--init", "--recursive", "--depth=1"]);
2380 if progress {
2381 git.arg("--progress");
2382 }
2383 git.arg(relative_path);
2384 git
2385 };
2386 if !update(true).allow_failure().run(dwn_ctx.exec_ctx) {
2387 update(false).allow_failure().run(dwn_ctx.exec_ctx);
2388 }
2389
2390 let has_local_modifications = !submodule_git()
2393 .allow_failure()
2394 .args(["diff-index", "--quiet", "HEAD"])
2395 .run(dwn_ctx.exec_ctx);
2396 if has_local_modifications {
2397 submodule_git().allow_failure().args(["stash", "push"]).run(dwn_ctx.exec_ctx);
2398 }
2399
2400 submodule_git().allow_failure().args(["reset", "-q", "--hard"]).run(dwn_ctx.exec_ctx);
2401 submodule_git().allow_failure().args(["clean", "-qdfx"]).run(dwn_ctx.exec_ctx);
2402
2403 if has_local_modifications {
2404 submodule_git().allow_failure().args(["stash", "pop"]).run(dwn_ctx.exec_ctx);
2405 }
2406}
2407
2408pub fn git_info(exec_ctx: &ExecutionContext, omit_git_hash: bool, dir: &Path) -> GitInfo {
2409 GitInfo::new(omit_git_hash, dir, exec_ctx)
2410}
2411
2412pub fn submodules_(submodules: &Option<bool>, rust_info: &channel::GitInfo) -> bool {
2413 submodules.unwrap_or(rust_info.is_managed_git_subrepository())
2416}
2417
2418pub fn is_system_llvm<'a>(
2423 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2424 target: TargetSelection,
2425) -> bool {
2426 let dwn_ctx = dwn_ctx.as_ref();
2427 match dwn_ctx.target_config.get(&target) {
2428 Some(Target { llvm_config: Some(_), .. }) => {
2429 let ci_llvm = dwn_ctx.llvm_from_ci && is_host_target(&dwn_ctx.host_target, &target);
2430 !ci_llvm
2431 }
2432 Some(Target { llvm_config: None, .. }) => false,
2434 None => false,
2435 }
2436}
2437
2438pub fn is_host_target(host_target: &TargetSelection, target: &TargetSelection) -> bool {
2439 host_target == target
2440}
2441
2442pub(crate) fn ci_llvm_root<'a>(dwn_ctx: impl AsRef<DownloadContext<'a>>) -> PathBuf {
2443 let dwn_ctx = dwn_ctx.as_ref();
2444 assert!(dwn_ctx.llvm_from_ci);
2445 dwn_ctx.out.join(dwn_ctx.host_target).join("ci-llvm")
2446}
2447
2448pub(crate) fn read_file_by_commit<'a>(
2450 dwn_ctx: impl AsRef<DownloadContext<'a>>,
2451 file: &Path,
2452 commit: &str,
2453) -> String {
2454 let dwn_ctx = dwn_ctx.as_ref();
2455 assert!(
2456 dwn_ctx.rust_info.is_managed_git_subrepository(),
2457 "`Config::read_file_by_commit` is not supported in non-git sources."
2458 );
2459
2460 let mut git = helpers::git(Some(dwn_ctx.src));
2461 git.arg("show").arg(format!("{commit}:{}", file.to_str().unwrap()));
2462 git.run_capture_stdout(dwn_ctx.exec_ctx).stdout()
2463}