Skip to main content

bootstrap/core/config/
config.rs

1//! This module defines the central `Config` struct, which aggregates all components
2//! of the bootstrap configuration into a single unit.
3//!
4//! It serves as the primary public interface for accessing the bootstrap configuration.
5//! The module coordinates the overall configuration parsing process using logic from `parsing.rs`
6//! and provides top-level methods such as `Config::parse()` for initialization, as well as
7//! utility methods for querying and manipulating the complete configuration state.
8//!
9//! Additionally, this module contains the core logic for parsing, validating, and inferring
10//! the final `Config` from various raw inputs.
11//!
12//! It manages the process of reading command-line arguments, environment variables,
13//! and the `bootstrap.toml` file—merging them, applying defaults, and performing
14//! cross-component validation. The main `parse_inner` function and its supporting
15//! helpers reside here, transforming raw `Toml` data into the structured `Config` type.
16use 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;
33use crate::core::build_steps::test::failed_tests::collect_previously_failed_tests;
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    BootstrapOverrideLld, Rust, RustOptimize, check_incompatible_options_for_ci_rustc,
46    parse_codegen_backends,
47};
48use crate::core::config::toml::target::{
49    DefaultLinuxLinkerOverride, Target, TomlTarget, default_linux_linker_overrides,
50};
51use crate::core::config::{
52    CompilerBuiltins, DebuginfoLevel, DryRun, GccCiMode, LlvmLibunwind, Merge, ReplaceOpt,
53    RustcLto, SplitDebuginfo, StringOrBool, threads_from_config,
54};
55use crate::core::download::{
56    DownloadContext, download_beta_toolchain, is_download_ci_available, maybe_download_rustfmt,
57};
58use crate::utils::channel;
59use crate::utils::exec::{ExecutionContext, command};
60use crate::utils::helpers::{exe, get_host_target};
61use crate::{CodegenBackendKind, GitInfo, OnceLock, TargetSelection, check_ci_llvm, helpers, t};
62
63/// Each path in this list is considered "allowed" in the `download-rustc="if-unchanged"` logic.
64/// This means they can be modified and changes to these paths should never trigger a compiler build
65/// when "if-unchanged" is set.
66///
67/// NOTE: Paths must have the ":!" prefix to tell git to ignore changes in those paths during
68/// the diff check.
69///
70/// WARNING: Be cautious when adding paths to this list. If a path that influences the compiler build
71/// is added here, it will cause bootstrap to skip necessary rebuilds, which may lead to risky results.
72/// For example, "src/bootstrap" should never be included in this list as it plays a crucial role in the
73/// final output/compiler, which can be significantly affected by changes made to the bootstrap sources.
74#[rustfmt::skip] // We don't want rustfmt to oneline this list
75pub const RUSTC_IF_UNCHANGED_ALLOWED_PATHS: &[&str] = &[
76    ":!library",
77    ":!src/tools",
78    ":!src/librustdoc",
79    ":!src/rustdoc-json-types",
80    ":!tests",
81    ":!triagebot.toml",
82    ":!src/bootstrap/defaults",
83];
84
85/// Global configuration for the entire build and/or bootstrap.
86///
87/// This structure is parsed from `bootstrap.toml`, and some of the fields are inferred from `git` or build-time parameters.
88///
89/// Note that this structure is not decoded directly into, but rather it is
90/// filled out from the decoded forms of the structs below. For documentation
91/// on each field, see the corresponding fields in
92/// `bootstrap.example.toml`.
93#[derive(Clone)]
94pub struct Config {
95    pub change_id: Option<ChangeId>,
96    pub bypass_bootstrap_lock: bool,
97    pub ccache: Option<String>,
98    /// Call Build::ninja() instead of this.
99    pub ninja_in_file: bool,
100    pub submodules: Option<bool>,
101    pub compiler_docs: bool,
102    pub library_docs_private_items: bool,
103    pub docs_minification: bool,
104    pub docs: bool,
105    pub locked_deps: bool,
106    pub vendor: bool,
107    pub target_config: HashMap<TargetSelection, Target>,
108    pub full_bootstrap: bool,
109    pub bootstrap_cache_path: Option<PathBuf>,
110    pub extended: bool,
111    pub tools: Option<HashSet<String>>,
112    /// Specify build configuration specific for some tool, such as enabled features, see [Tool].
113    /// The key in the map is the name of the tool, and the value is tool-specific configuration.
114    pub tool: HashMap<String, Tool>,
115    pub sanitizers: bool,
116    pub profiler: bool,
117    pub omit_git_hash: bool,
118    pub skip: Vec<PathBuf>,
119    pub include_default_paths: bool,
120    pub rustc_error_format: Option<String>,
121    pub json_output: bool,
122    pub compile_time_deps: bool,
123    pub test_compare_mode: bool,
124    pub color: Color,
125    pub patch_binaries_for_nix: Option<bool>,
126    pub stage0_metadata: build_helper::stage0_parser::Stage0,
127    pub android_ndk: Option<PathBuf>,
128    pub optimized_compiler_builtins: CompilerBuiltins,
129    pub record_failed_tests_path: PathBuf,
130
131    pub stdout_is_tty: bool,
132    pub stderr_is_tty: bool,
133
134    pub on_fail: Option<String>,
135    pub explicit_stage_from_cli: bool,
136    pub explicit_stage_from_config: bool,
137    pub stage: u32,
138    pub keep_stage: Vec<u32>,
139    pub keep_stage_std: Vec<u32>,
140    pub src: PathBuf,
141    /// defaults to `bootstrap.toml`
142    pub config: Option<PathBuf>,
143    pub jobs: Option<u32>,
144    pub cmd: Subcommand,
145    pub quiet: bool,
146    pub incremental: bool,
147    pub dump_bootstrap_shims: bool,
148    /// Arguments appearing after `--` to be forwarded to tools,
149    /// e.g. `--fix-broken` or test arguments.
150    pub free_args: Vec<String>,
151
152    /// `None` if we shouldn't download CI compiler artifacts, or the commit to download if we should.
153    pub download_rustc_commit: Option<String>,
154
155    pub deny_warnings: bool,
156    pub backtrace_on_ice: bool,
157
158    // llvm codegen options
159    pub llvm_assertions: bool,
160    pub llvm_tests: bool,
161    pub llvm_enzyme: bool,
162    pub llvm_offload: bool,
163    pub llvm_plugins: bool,
164    pub llvm_optimize: bool,
165    pub llvm_thin_lto: bool,
166    pub llvm_release_debuginfo: bool,
167    pub llvm_static_stdcpp: bool,
168    pub llvm_libzstd: bool,
169    pub llvm_link_shared: Cell<Option<bool>>,
170    pub llvm_clang_cl: Option<String>,
171    pub llvm_targets: Option<String>,
172    pub llvm_experimental_targets: Option<String>,
173    pub llvm_link_jobs: Option<u32>,
174    pub llvm_version_suffix: Option<String>,
175    pub llvm_use_linker: Option<String>,
176    pub llvm_clang_dir: Option<PathBuf>,
177    pub llvm_allow_old_toolchain: bool,
178    pub llvm_polly: bool,
179    pub llvm_clang: bool,
180    pub llvm_enable_warnings: bool,
181    pub llvm_from_ci: bool,
182    pub llvm_build_config: HashMap<String, String>,
183
184    pub bootstrap_override_lld: BootstrapOverrideLld,
185    pub lld_enabled: bool,
186    pub llvm_tools_enabled: bool,
187    pub llvm_bitcode_linker_enabled: bool,
188
189    pub llvm_cflags: Option<String>,
190    pub llvm_cxxflags: Option<String>,
191    pub llvm_ldflags: Option<String>,
192    pub llvm_use_libcxx: bool,
193
194    // gcc codegen options
195    pub gcc_ci_mode: GccCiMode,
196    pub libgccjit_libs_dir: Option<PathBuf>,
197
198    // rust codegen options
199    pub rust_optimize: RustOptimize,
200    pub rust_codegen_units: Option<u32>,
201    pub rust_codegen_units_std: Option<u32>,
202    pub rustc_debug_assertions: bool,
203    pub std_debug_assertions: bool,
204    pub tools_debug_assertions: bool,
205
206    pub rust_overflow_checks: bool,
207    pub rust_overflow_checks_std: bool,
208    pub rust_debug_logging: bool,
209    pub rust_debuginfo_level_rustc: DebuginfoLevel,
210    pub rust_debuginfo_level_std: DebuginfoLevel,
211    pub rust_debuginfo_level_tools: DebuginfoLevel,
212    pub rust_debuginfo_level_tests: DebuginfoLevel,
213    pub rust_rpath: bool,
214    pub rust_strip: bool,
215    pub rust_frame_pointers: bool,
216    pub rust_stack_protector: Option<String>,
217    pub rustc_default_linker: Option<String>,
218    pub rust_optimize_tests: bool,
219    pub rust_dist_src: bool,
220    pub rust_codegen_backends: Vec<CodegenBackendKind>,
221    pub rust_verify_llvm_ir: bool,
222    pub rust_thin_lto_import_instr_limit: Option<u32>,
223    pub rust_randomize_layout: bool,
224    pub rust_remap_debuginfo: bool,
225    pub rust_new_symbol_mangling: Option<bool>,
226    pub rust_annotate_moves_size_limit: Option<u64>,
227    pub rust_profile_use: Option<String>,
228    pub rust_profile_generate: Option<String>,
229    pub rust_lto: RustcLto,
230    pub rust_validate_mir_opts: Option<u32>,
231    pub rust_std_features: BTreeSet<String>,
232    pub rust_break_on_ice: bool,
233    pub rust_parallel_frontend_threads: Option<u32>,
234    pub rust_rustflags: Vec<String>,
235
236    pub llvm_profile_use: Option<String>,
237    pub llvm_profile_generate: bool,
238    pub llvm_libunwind_default: Option<LlvmLibunwind>,
239    pub enable_bolt_settings: bool,
240
241    pub reproducible_artifacts: Vec<String>,
242
243    pub host_target: TargetSelection,
244    pub hosts: Vec<TargetSelection>,
245    pub targets: Vec<TargetSelection>,
246    pub local_rebuild: bool,
247    pub jemalloc: bool,
248    pub control_flow_guard: bool,
249    pub ehcont_guard: bool,
250
251    // dist misc
252    pub dist_sign_folder: Option<PathBuf>,
253    pub dist_upload_addr: Option<String>,
254    pub dist_compression_formats: Option<Vec<String>>,
255    pub dist_compression_profile: String,
256    pub dist_include_mingw_linker: bool,
257    pub dist_vendor: bool,
258
259    // libstd features
260    pub backtrace: bool, // support for RUST_BACKTRACE
261
262    // misc
263    pub low_priority: bool,
264    pub channel: String,
265    pub description: Option<String>,
266    pub verbose_tests: bool,
267    pub save_toolstates: Option<PathBuf>,
268    pub print_step_timings: bool,
269    pub print_step_rusage: bool,
270
271    // Fallback musl-root for all targets
272    pub musl_root: Option<PathBuf>,
273    pub prefix: Option<PathBuf>,
274    pub sysconfdir: Option<PathBuf>,
275    pub datadir: Option<PathBuf>,
276    pub docdir: Option<PathBuf>,
277    pub bindir: PathBuf,
278    pub libdir: Option<PathBuf>,
279    pub mandir: Option<PathBuf>,
280    pub codegen_tests: bool,
281    pub nodejs: Option<PathBuf>,
282    pub yarn: Option<PathBuf>,
283    pub gdb: Option<PathBuf>,
284    pub lldb: Option<PathBuf>,
285    pub python: Option<PathBuf>,
286    pub windows_rc: Option<PathBuf>,
287    pub reuse: Option<PathBuf>,
288    pub cargo_native_static: bool,
289    pub configure_args: Vec<String>,
290    pub out: PathBuf,
291    pub rust_info: channel::GitInfo,
292
293    pub cargo_info: channel::GitInfo,
294    pub rust_analyzer_info: channel::GitInfo,
295    pub clippy_info: channel::GitInfo,
296    pub miri_info: channel::GitInfo,
297    pub rustfmt_info: channel::GitInfo,
298    pub enzyme_info: channel::GitInfo,
299    pub in_tree_llvm_info: channel::GitInfo,
300    pub in_tree_gcc_info: channel::GitInfo,
301
302    // These are either the stage0 downloaded binaries or the locally installed ones.
303    pub initial_cargo: PathBuf,
304    pub initial_rustc: PathBuf,
305    pub initial_rustdoc: PathBuf,
306    pub initial_cargo_clippy: Option<PathBuf>,
307    pub initial_sysroot: PathBuf,
308    pub initial_rustfmt: Option<PathBuf>,
309
310    /// The paths to work with. For example: with `./x check foo bar` we get
311    /// `paths=["foo", "bar"]`.
312    pub paths: Vec<PathBuf>,
313
314    /// Command for visual diff display, e.g. `diff-tool --color=always`.
315    pub compiletest_diff_tool: Option<String>,
316
317    /// Whether to allow running both `compiletest` self-tests and `compiletest`-managed test suites
318    /// against the stage 0 (rustc, std).
319    ///
320    /// This is only intended to be used when the stage 0 compiler is actually built from in-tree
321    /// sources.
322    pub compiletest_allow_stage0: bool,
323
324    /// Default value for `--extra-checks`
325    pub tidy_extra_checks: Option<String>,
326    pub ci_env: CiEnv,
327
328    /// Cache for determining path modifications
329    pub path_modification_cache: Arc<Mutex<HashMap<Vec<&'static str>, PathFreshness>>>,
330
331    /// Skip checking the standard library if `rust.download-rustc` isn't available.
332    /// This is mostly for RA as building the stage1 compiler to check the library tree
333    /// on each code change might be too much for some computers.
334    pub skip_std_check_if_no_download_rustc: bool,
335
336    pub exec_ctx: ExecutionContext,
337}
338
339impl Config {
340    pub fn set_dry_run(&mut self, dry_run: DryRun) {
341        self.exec_ctx.set_dry_run(dry_run);
342    }
343
344    pub fn get_dry_run(&self) -> &DryRun {
345        self.exec_ctx.get_dry_run()
346    }
347
348    #[cfg_attr(
349        feature = "tracing",
350        instrument(target = "CONFIG_HANDLING", level = "trace", name = "Config::parse", skip_all)
351    )]
352    pub fn parse(flags: Flags) -> Config {
353        Self::parse_inner(flags, Self::get_toml)
354    }
355
356    #[cfg_attr(
357        feature = "tracing",
358        instrument(
359            target = "CONFIG_HANDLING",
360            level = "trace",
361            name = "Config::parse_inner",
362            skip_all
363        )
364    )]
365    pub(crate) fn parse_inner(
366        flags: Flags,
367        get_toml: impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>,
368    ) -> Config {
369        // Destructure flags to ensure that we use all its fields
370        // The field variables are prefixed with `flags_` to avoid clashes
371        // with values from TOML config files with same names.
372        let Flags {
373            cmd: flags_cmd,
374            verbose: flags_verbose,
375            quiet: flags_quiet,
376            incremental: flags_incremental,
377            config: flags_config,
378            build_dir: flags_build_dir,
379            build: flags_build,
380            host: flags_host,
381            target: flags_target,
382            exclude: flags_exclude,
383            skip: flags_skip,
384            include_default_paths: flags_include_default_paths,
385            rustc_error_format: flags_rustc_error_format,
386            on_fail: flags_on_fail,
387            dry_run: flags_dry_run,
388            dump_bootstrap_shims: flags_dump_bootstrap_shims,
389            stage: flags_stage,
390            keep_stage: flags_keep_stage,
391            keep_stage_std: flags_keep_stage_std,
392            src: flags_src,
393            jobs: flags_jobs,
394            warnings: flags_warnings,
395            json_output: flags_json_output,
396            compile_time_deps: flags_compile_time_deps,
397            color: flags_color,
398            bypass_bootstrap_lock: flags_bypass_bootstrap_lock,
399            rust_profile_generate: flags_rust_profile_generate,
400            rust_profile_use: flags_rust_profile_use,
401            llvm_profile_use: flags_llvm_profile_use,
402            llvm_profile_generate: flags_llvm_profile_generate,
403            enable_bolt_settings: flags_enable_bolt_settings,
404            skip_stage0_validation: flags_skip_stage0_validation,
405            reproducible_artifact: flags_reproducible_artifact,
406            paths: flags_paths,
407            set: flags_set,
408            free_args: flags_free_args,
409            ci: flags_ci,
410            skip_std_check_if_no_download_rustc: flags_skip_std_check_if_no_download_rustc,
411        } = flags;
412
413        #[cfg(feature = "tracing")]
414        span!(
415            target: "CONFIG_HANDLING",
416            tracing::Level::TRACE,
417            "collecting paths and path exclusions",
418            "flags.paths" = ?flags_paths,
419            "flags.skip" = ?flags_skip,
420            "flags.exclude" = ?flags_exclude
421        );
422
423        if flags_cmd.no_doc() {
424            eprintln!(
425                "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."
426            )
427        }
428
429        // Set config values based on flags.
430        let mut exec_ctx = ExecutionContext::new(flags_verbose, flags_cmd.fail_fast());
431        exec_ctx.set_dry_run(if flags_dry_run { DryRun::UserSelected } else { DryRun::Disabled });
432
433        let default_src_dir = {
434            let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
435            // Undo `src/bootstrap`
436            manifest_dir.parent().unwrap().parent().unwrap().to_owned()
437        };
438        let src = if let Some(s) = compute_src_directory(flags_src, &exec_ctx) {
439            s
440        } else {
441            default_src_dir.clone()
442        };
443
444        #[cfg(test)]
445        {
446            if let Some(config_path) = flags_config.as_ref() {
447                assert!(
448                    !config_path.starts_with(&src),
449                    "Path {config_path:?} should not be inside or equal to src dir {src:?}"
450                );
451            } else {
452                panic!("During test the config should be explicitly added");
453            }
454        }
455
456        // Now load the TOML config, as soon as possible
457        let (mut toml, toml_path) = load_toml_config(&src, flags_config, &get_toml);
458
459        postprocess_toml(&mut toml, &src, toml_path.clone(), &exec_ctx, &flags_set, &get_toml);
460
461        // Now override TOML values with flags, to make sure that we won't later override flags with
462        // TOML values by accident instead, because flags have higher priority.
463        let Build {
464            description: build_description,
465            build: build_build,
466            host: build_host,
467            target: build_target,
468            build_dir: build_build_dir,
469            cargo: mut build_cargo,
470            rustc: mut build_rustc,
471            rustdoc: build_rustdoc,
472            rustfmt: build_rustfmt,
473            cargo_clippy: build_cargo_clippy,
474            docs: build_docs,
475            compiler_docs: build_compiler_docs,
476            library_docs_private_items: build_library_docs_private_items,
477            docs_minification: build_docs_minification,
478            submodules: build_submodules,
479            gdb: build_gdb,
480            lldb: build_lldb,
481            nodejs: build_nodejs,
482
483            yarn: build_yarn,
484            npm: build_npm,
485            python: build_python,
486            windows_rc: build_windows_rc,
487            reuse: build_reuse,
488            locked_deps: build_locked_deps,
489            vendor: build_vendor,
490            full_bootstrap: build_full_bootstrap,
491            bootstrap_cache_path: build_bootstrap_cache_path,
492            extended: build_extended,
493            tools: build_tools,
494            tool: build_tool,
495            verbose: build_verbose,
496            sanitizers: build_sanitizers,
497            profiler: build_profiler,
498            cargo_native_static: build_cargo_native_static,
499            low_priority: build_low_priority,
500            configure_args: build_configure_args,
501            local_rebuild: build_local_rebuild,
502            print_step_timings: build_print_step_timings,
503            print_step_rusage: build_print_step_rusage,
504            check_stage: build_check_stage,
505            doc_stage: build_doc_stage,
506            build_stage: build_build_stage,
507            test_stage: build_test_stage,
508            install_stage: build_install_stage,
509            dist_stage: build_dist_stage,
510            bench_stage: build_bench_stage,
511            patch_binaries_for_nix: build_patch_binaries_for_nix,
512            record_failed_tests_path: build_record_failed_tests_path,
513            // This field is only used by bootstrap.py
514            metrics: _,
515            android_ndk: build_android_ndk,
516            optimized_compiler_builtins: build_optimized_compiler_builtins,
517            jobs: build_jobs,
518            compiletest_diff_tool: build_compiletest_diff_tool,
519            // No longer has any effect; kept (for now) to avoid breaking people's configs.
520            compiletest_use_stage0_libtest: _,
521            tidy_extra_checks: build_tidy_extra_checks,
522            ccache: build_ccache,
523            exclude: build_exclude,
524            compiletest_allow_stage0: build_compiletest_allow_stage0,
525        } = toml.build.unwrap_or_default();
526
527        let Install {
528            prefix: install_prefix,
529            sysconfdir: install_sysconfdir,
530            docdir: install_docdir,
531            bindir: install_bindir,
532            libdir: install_libdir,
533            mandir: install_mandir,
534            datadir: install_datadir,
535        } = toml.install.unwrap_or_default();
536
537        let Rust {
538            optimize: rust_optimize,
539            debug: rust_debug,
540            codegen_units: rust_codegen_units,
541            codegen_units_std: rust_codegen_units_std,
542            rustc_debug_assertions: rust_rustc_debug_assertions,
543            std_debug_assertions: rust_std_debug_assertions,
544            tools_debug_assertions: rust_tools_debug_assertions,
545            overflow_checks: rust_overflow_checks,
546            overflow_checks_std: rust_overflow_checks_std,
547            debug_logging: rust_debug_logging,
548            debuginfo_level: rust_debuginfo_level,
549            debuginfo_level_rustc: rust_debuginfo_level_rustc,
550            debuginfo_level_std: rust_debuginfo_level_std,
551            debuginfo_level_tools: rust_debuginfo_level_tools,
552            debuginfo_level_tests: rust_debuginfo_level_tests,
553            backtrace: rust_backtrace,
554            incremental: rust_incremental,
555            randomize_layout: rust_randomize_layout,
556            default_linker: rust_default_linker,
557            channel: rust_channel,
558            musl_root: rust_musl_root,
559            rpath: rust_rpath,
560            verbose_tests: rust_verbose_tests,
561            optimize_tests: rust_optimize_tests,
562            codegen_tests: rust_codegen_tests,
563            omit_git_hash: rust_omit_git_hash,
564            dist_src: rust_dist_src,
565            save_toolstates: rust_save_toolstates,
566            codegen_backends: rust_codegen_backends,
567            lld: rust_lld_enabled,
568            llvm_tools: rust_llvm_tools,
569            llvm_bitcode_linker: rust_llvm_bitcode_linker,
570            deny_warnings: rust_deny_warnings,
571            backtrace_on_ice: rust_backtrace_on_ice,
572            verify_llvm_ir: rust_verify_llvm_ir,
573            thin_lto_import_instr_limit: rust_thin_lto_import_instr_limit,
574            parallel_frontend_threads: rust_parallel_frontend_threads,
575            remap_debuginfo: rust_remap_debuginfo,
576            jemalloc: rust_jemalloc,
577            test_compare_mode: rust_test_compare_mode,
578            llvm_libunwind: rust_llvm_libunwind,
579            control_flow_guard: rust_control_flow_guard,
580            ehcont_guard: rust_ehcont_guard,
581            new_symbol_mangling: rust_new_symbol_mangling,
582            annotate_moves_size_limit: rust_annotate_moves_size_limit,
583            profile_generate: rust_profile_generate,
584            profile_use: rust_profile_use,
585            download_rustc: rust_download_rustc,
586            lto: rust_lto,
587            validate_mir_opts: rust_validate_mir_opts,
588            frame_pointers: rust_frame_pointers,
589            stack_protector: rust_stack_protector,
590            strip: rust_strip,
591            bootstrap_override_lld: rust_bootstrap_override_lld,
592            bootstrap_override_lld_legacy: rust_bootstrap_override_lld_legacy,
593            std_features: rust_std_features,
594            break_on_ice: rust_break_on_ice,
595            rustflags: rust_rustflags,
596        } = toml.rust.unwrap_or_default();
597
598        let Llvm {
599            optimize: llvm_optimize,
600            thin_lto: llvm_thin_lto,
601            release_debuginfo: llvm_release_debuginfo,
602            assertions: llvm_assertions,
603            tests: llvm_tests,
604            enzyme: llvm_enzyme,
605            plugins: llvm_plugin,
606            static_libstdcpp: llvm_static_libstdcpp,
607            libzstd: llvm_libzstd,
608            ninja: llvm_ninja,
609            targets: llvm_targets,
610            experimental_targets: llvm_experimental_targets,
611            link_jobs: llvm_link_jobs,
612            link_shared: llvm_link_shared,
613            version_suffix: llvm_version_suffix,
614            clang_cl: llvm_clang_cl,
615            cflags: llvm_cflags,
616            cxxflags: llvm_cxxflags,
617            ldflags: llvm_ldflags,
618            use_libcxx: llvm_use_libcxx,
619            use_linker: llvm_use_linker,
620            allow_old_toolchain: llvm_allow_old_toolchain,
621            offload: llvm_offload,
622            offload_clang_dir: llvm_clang_dir,
623            polly: llvm_polly,
624            clang: llvm_clang,
625            enable_warnings: llvm_enable_warnings,
626            download_ci_llvm: llvm_download_ci_llvm,
627            build_config: llvm_build_config,
628        } = toml.llvm.unwrap_or_default();
629
630        let Dist {
631            sign_folder: dist_sign_folder,
632            upload_addr: dist_upload_addr,
633            src_tarball: dist_src_tarball,
634            compression_formats: dist_compression_formats,
635            compression_profile: dist_compression_profile,
636            include_mingw_linker: dist_include_mingw_linker,
637            vendor: dist_vendor,
638        } = toml.dist.unwrap_or_default();
639
640        let Gcc {
641            download_ci_gcc: gcc_download_ci_gcc,
642            libgccjit_libs_dir: gcc_libgccjit_libs_dir,
643        } = toml.gcc.unwrap_or_default();
644
645        if rust_bootstrap_override_lld.is_some() && rust_bootstrap_override_lld_legacy.is_some() {
646            panic!(
647                "Cannot use both `rust.use-lld` and `rust.bootstrap-override-lld`. Please use only `rust.bootstrap-override-lld`"
648            );
649        }
650
651        let bootstrap_override_lld =
652            rust_bootstrap_override_lld.or(rust_bootstrap_override_lld_legacy).unwrap_or_default();
653
654        if rust_optimize.as_ref().is_some_and(|v| matches!(v, RustOptimize::Bool(false))) {
655            eprintln!(
656                "WARNING: setting `optimize` to `false` is known to cause errors and \
657                should be considered unsupported. Refer to `bootstrap.example.toml` \
658                for more details."
659            );
660        }
661
662        // Prefer CLI verbosity flags if set (`flags_verbose` > 0), otherwise take the value from
663        // TOML.
664        exec_ctx.set_verbosity(cmp::max(build_verbose.unwrap_or_default() as u8, flags_verbose));
665
666        let stage0_metadata = build_helper::stage0_parser::parse_stage0_file();
667        let path_modification_cache = Arc::new(Mutex::new(HashMap::new()));
668
669        let host_target = flags_build
670            .or(build_build)
671            .map(|build| TargetSelection::from_user(&build))
672            .unwrap_or_else(get_host_target);
673        let hosts = flags_host
674            .map(|TargetSelectionList(hosts)| hosts)
675            .or_else(|| {
676                build_host.map(|h| h.iter().map(|t| TargetSelection::from_user(t)).collect())
677            })
678            .unwrap_or_else(|| vec![host_target]);
679
680        let llvm_assertions = llvm_assertions.unwrap_or(false);
681        let mut target_config = HashMap::new();
682        let mut channel = "dev".to_string();
683
684        let out = flags_build_dir.or_else(|| build_build_dir.map(PathBuf::from));
685        let out = if cfg!(test) {
686            out.expect("--build-dir has to be specified in tests")
687        } else {
688            out.unwrap_or_else(|| PathBuf::from("build"))
689        };
690
691        // NOTE: Bootstrap spawns various commands with different working directories.
692        // To avoid writing to random places on the file system, `config.out` needs to be an absolute path.
693        let mut out = if !out.is_absolute() {
694            // `canonicalize` requires the path to already exist. Use our vendored copy of `absolute` instead.
695            absolute(&out).expect("can't make empty path absolute")
696        } else {
697            out
698        };
699
700        let default_stage0_rustc_path = |dir: &Path| {
701            dir.join(host_target).join("stage0").join("bin").join(exe("rustc", host_target))
702        };
703
704        if cfg!(test) {
705            // When configuring bootstrap for tests, make sure to set the rustc and Cargo to the
706            // same ones used to call the tests (if custom ones are not defined in the toml). If we
707            // don't do that, bootstrap will use its own detection logic to find a suitable rustc
708            // and Cargo, which doesn't work when the caller is specìfying a custom local rustc or
709            // Cargo in their bootstrap.toml.
710            build_rustc = build_rustc.take().or(std::env::var_os("RUSTC").map(|p| p.into()));
711            build_cargo = build_cargo.take().or(std::env::var_os("CARGO").map(|p| p.into()));
712
713            // If we are running only `cargo test` (and not `x test bootstrap`), which is useful
714            // e.g. for debugging bootstrap itself, then we won't have RUSTC and CARGO set to the
715            // proper paths.
716            // We thus "guess" that the build directory is located at <src>/build, and try to load
717            // rustc and cargo from there
718            let is_test_outside_x = std::env::var("CARGO_TARGET_DIR").is_err();
719            if is_test_outside_x && build_rustc.is_none() {
720                let stage0_rustc = default_stage0_rustc_path(&default_src_dir.join("build"));
721                assert!(
722                    stage0_rustc.exists(),
723                    "Trying to run cargo test without having a stage0 rustc available in {}",
724                    stage0_rustc.display()
725                );
726                build_rustc = Some(stage0_rustc);
727            }
728        }
729
730        if !flags_skip_stage0_validation {
731            if let Some(rustc) = &build_rustc {
732                check_stage0_version(rustc, "rustc", &src, &exec_ctx);
733            }
734            if let Some(cargo) = &build_cargo {
735                check_stage0_version(cargo, "cargo", &src, &exec_ctx);
736            }
737        }
738
739        if build_cargo_clippy.is_some() && build_rustc.is_none() {
740            println!(
741                "WARNING: Using `build.cargo-clippy` without `build.rustc` usually fails due to toolchain conflict."
742            );
743        }
744
745        let ci_env = match flags_ci {
746            Some(true) => CiEnv::GitHubActions,
747            Some(false) => CiEnv::None,
748            None => CiEnv::current(),
749        };
750        let dwn_ctx = DownloadContext {
751            path_modification_cache: path_modification_cache.clone(),
752            src: &src,
753            submodules: &build_submodules,
754            host_target,
755            patch_binaries_for_nix: build_patch_binaries_for_nix,
756            exec_ctx: &exec_ctx,
757            stage0_metadata: &stage0_metadata,
758            llvm_assertions,
759            bootstrap_cache_path: &build_bootstrap_cache_path,
760            ci_env,
761        };
762
763        let initial_rustc = build_rustc.unwrap_or_else(|| {
764            download_beta_toolchain(&dwn_ctx, &out);
765            default_stage0_rustc_path(&out)
766        });
767
768        let initial_rustdoc = build_rustdoc
769            .unwrap_or_else(|| initial_rustc.with_file_name(exe("rustdoc", host_target)));
770
771        let initial_sysroot = t!(PathBuf::from_str(
772            command(&initial_rustc)
773                .args(["--print", "sysroot"])
774                .run_in_dry_run()
775                .run_capture_stdout(&exec_ctx)
776                .stdout()
777                .trim()
778        ));
779
780        let initial_cargo = build_cargo.unwrap_or_else(|| {
781            download_beta_toolchain(&dwn_ctx, &out);
782            initial_sysroot.join("bin").join(exe("cargo", host_target))
783        });
784
785        // NOTE: it's important this comes *after* we set `initial_rustc` just above.
786        if exec_ctx.dry_run() {
787            out = out.join("tmp-dry-run");
788            fs::create_dir_all(&out).expect("Failed to create dry-run directory");
789        }
790
791        let file_content = t!(fs::read_to_string(src.join("src/ci/channel")));
792        let ci_channel = file_content.trim_end();
793
794        let is_user_configured_rust_channel = match rust_channel {
795            Some(channel_) if channel_ == "auto-detect" => {
796                channel = ci_channel.into();
797                true
798            }
799            Some(channel_) => {
800                channel = channel_;
801                true
802            }
803            None => false,
804        };
805
806        let omit_git_hash = rust_omit_git_hash.unwrap_or(channel == "dev");
807
808        let rust_info = git_info(&exec_ctx, omit_git_hash, &src);
809
810        if !is_user_configured_rust_channel && rust_info.is_from_tarball() {
811            channel = ci_channel.into();
812        }
813
814        // FIXME(#133381): alt rustc builds currently do *not* have rustc debug assertions
815        // enabled. We should not download a CI alt rustc if we need rustc to have debug
816        // assertions (e.g. for crashes test suite). This can be changed once something like
817        // [Enable debug assertions on alt
818        // builds](https://github.com/rust-lang/rust/pull/131077) lands.
819        //
820        // Note that `rust.debug = true` currently implies `rust.debug-assertions = true`!
821        //
822        // This relies also on the fact that the global default for `download-rustc` will be
823        // `false` if it's not explicitly set.
824        let debug_assertions_requested = matches!(rust_rustc_debug_assertions, Some(true))
825            || (matches!(rust_debug, Some(true))
826                && !matches!(rust_rustc_debug_assertions, Some(false)));
827
828        if debug_assertions_requested
829            && let Some(ref opt) = rust_download_rustc
830            && opt.is_string_or_true()
831        {
832            eprintln!(
833                "WARN: currently no CI rustc builds have rustc debug assertions \
834                        enabled. Please either set `rust.debug-assertions` to `false` if you \
835                        want to use download CI rustc or set `rust.download-rustc` to `false`."
836            );
837        }
838
839        let mut download_rustc_commit =
840            download_ci_rustc_commit(&dwn_ctx, &rust_info, rust_download_rustc, llvm_assertions);
841
842        if debug_assertions_requested && download_rustc_commit.is_some() {
843            eprintln!(
844                "WARN: `rust.debug-assertions = true` will prevent downloading CI rustc as alt CI \
845                rustc is not currently built with debug assertions."
846            );
847            // We need to put this later down_ci_rustc_commit.
848            download_rustc_commit = None;
849        }
850
851        // We need to override `rust.channel` if it's manually specified when using the CI rustc.
852        // This is because if the compiler uses a different channel than the one specified in bootstrap.toml,
853        // tests may fail due to using a different channel than the one used by the compiler during tests.
854        if let Some(commit) = &download_rustc_commit
855            && is_user_configured_rust_channel
856        {
857            println!(
858                "WARNING: `rust.download-rustc` is enabled. The `rust.channel` option will be overridden by the CI rustc's channel."
859            );
860
861            channel =
862                read_file_by_commit(&dwn_ctx, &rust_info, Path::new("src/ci/channel"), commit)
863                    .trim()
864                    .to_owned();
865        }
866
867        if build_npm.is_some() {
868            println!(
869                "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."
870            );
871        }
872
873        let mut lld_enabled = rust_lld_enabled.unwrap_or(false);
874
875        // Linux targets for which the user explicitly overrode the used linker
876        let mut targets_with_user_linker_override = HashSet::new();
877
878        if let Some(t) = toml.target {
879            for (triple, cfg) in t {
880                let TomlTarget {
881                    cc: target_cc,
882                    cxx: target_cxx,
883                    ar: target_ar,
884                    ranlib: target_ranlib,
885                    default_linker: target_default_linker,
886                    default_linker_linux_override: target_default_linker_linux_override,
887                    linker: target_linker,
888                    split_debuginfo: target_split_debuginfo,
889                    llvm_config: target_llvm_config,
890                    llvm_has_rust_patches: target_llvm_has_rust_patches,
891                    llvm_filecheck: target_llvm_filecheck,
892                    llvm_libunwind: target_llvm_libunwind,
893                    sanitizers: target_sanitizers,
894                    profiler: target_profiler,
895                    rpath: target_rpath,
896                    rustflags: target_rustflags,
897                    crt_static: target_crt_static,
898                    musl_root: target_musl_root,
899                    musl_libdir: target_musl_libdir,
900                    wasi_root: target_wasi_root,
901                    qemu_rootfs: target_qemu_rootfs,
902                    no_std: target_no_std,
903                    codegen_backends: target_codegen_backends,
904                    runner: target_runner,
905                    optimized_compiler_builtins: target_optimized_compiler_builtins,
906                    jemalloc: target_jemalloc,
907                } = cfg;
908
909                let mut target = Target::from_triple(&triple);
910
911                if target_default_linker_linux_override.is_some() {
912                    targets_with_user_linker_override.insert(triple.clone());
913                }
914
915                let default_linker_linux_override = match target_default_linker_linux_override {
916                    Some(DefaultLinuxLinkerOverride::SelfContainedLldCc) => {
917                        if rust_default_linker.is_some() {
918                            panic!(
919                                "cannot set both `default-linker` and `default-linker-linux` for target `{triple}`"
920                            );
921                        }
922                        if !triple.contains("linux-gnu") {
923                            panic!(
924                                "`default-linker-linux` can only be set for Linux GNU targets, not for `{triple}`"
925                            );
926                        }
927                        if !lld_enabled {
928                            panic!(
929                                "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."
930                            );
931                        }
932                        DefaultLinuxLinkerOverride::SelfContainedLldCc
933                    }
934                    Some(DefaultLinuxLinkerOverride::Off) => DefaultLinuxLinkerOverride::Off,
935                    None => DefaultLinuxLinkerOverride::default(),
936                };
937
938                if let Some(ref s) = target_llvm_config {
939                    if download_rustc_commit.is_some() && triple == *host_target.triple {
940                        panic!(
941                            "setting llvm_config for the host is incompatible with download-rustc"
942                        );
943                    }
944                    target.llvm_config = Some(src.join(s));
945                }
946                if let Some(patches) = target_llvm_has_rust_patches {
947                    assert!(
948                        build_submodules == Some(false) || target_llvm_config.is_some(),
949                        "use of `llvm-has-rust-patches` is restricted to cases where either submodules are disabled or llvm-config been provided"
950                    );
951                    target.llvm_has_rust_patches = Some(patches);
952                }
953                if let Some(ref s) = target_llvm_filecheck {
954                    target.llvm_filecheck = Some(src.join(s));
955                }
956                target.llvm_libunwind = target_llvm_libunwind.as_ref().map(|v| {
957                    v.parse().unwrap_or_else(|_| {
958                        panic!("failed to parse target.{triple}.llvm-libunwind")
959                    })
960                });
961                if let Some(s) = target_no_std {
962                    target.no_std = s;
963                }
964                target.cc = target_cc.map(PathBuf::from);
965                target.cxx = target_cxx.map(PathBuf::from);
966                target.ar = target_ar.map(PathBuf::from);
967                target.ranlib = target_ranlib.map(PathBuf::from);
968                target.linker = target_linker.map(PathBuf::from);
969                target.crt_static = target_crt_static;
970                target.default_linker = target_default_linker;
971                target.default_linker_linux_override = default_linker_linux_override;
972                target.musl_root = target_musl_root.map(PathBuf::from);
973                target.musl_libdir = target_musl_libdir.map(PathBuf::from);
974                target.wasi_root = target_wasi_root.map(PathBuf::from);
975                target.qemu_rootfs = target_qemu_rootfs.map(PathBuf::from);
976                target.runner = target_runner;
977                target.sanitizers = target_sanitizers;
978                target.profiler = target_profiler;
979                target.rpath = target_rpath;
980                target.rustflags = target_rustflags.unwrap_or_default();
981                target.optimized_compiler_builtins = target_optimized_compiler_builtins;
982                target.jemalloc = target_jemalloc;
983                if let Some(backends) = target_codegen_backends {
984                    target.codegen_backends =
985                        Some(parse_codegen_backends(backends, &format!("target.{triple}")))
986                }
987
988                target.split_debuginfo = target_split_debuginfo.as_ref().map(|v| {
989                    v.parse().unwrap_or_else(|_| {
990                        panic!("invalid value for target.{triple}.split-debuginfo")
991                    })
992                });
993
994                target_config.insert(TargetSelection::from_user(&triple), target);
995            }
996        }
997
998        let llvm_from_ci = parse_download_ci_llvm(
999            &dwn_ctx,
1000            &rust_info,
1001            &download_rustc_commit,
1002            llvm_download_ci_llvm,
1003            llvm_assertions,
1004        );
1005        let is_host_system_llvm =
1006            is_system_llvm(&target_config, llvm_from_ci, host_target, host_target);
1007
1008        if llvm_from_ci {
1009            let warn = |option: &str| {
1010                println!(
1011                    "WARNING: `{option}` will only be used on `compiler/rustc_llvm` build, not for the LLVM build."
1012                );
1013                println!(
1014                    "HELP: To use `{option}` for LLVM builds, set `download-ci-llvm` option to false."
1015                );
1016            };
1017
1018            if llvm_static_libstdcpp.is_some() {
1019                warn("static-libstdcpp");
1020            }
1021
1022            if llvm_link_shared.is_some() {
1023                warn("link-shared");
1024            }
1025
1026            // FIXME(#129153): instead of all the ad-hoc `download-ci-llvm` checks that follow,
1027            // use the `builder-config` present in tarballs since #128822 to compare the local
1028            // config to the ones used to build the LLVM artifacts on CI, and only notify users
1029            // if they've chosen a different value.
1030
1031            if llvm_libzstd.is_some() {
1032                println!(
1033                    "WARNING: when using `download-ci-llvm`, the local `llvm.libzstd` option, \
1034                    like almost all `llvm.*` options, will be ignored and set by the LLVM CI \
1035                    artifacts builder config."
1036                );
1037                println!(
1038                    "HELP: To use `llvm.libzstd` for LLVM/LLD builds, set `download-ci-llvm` option to false."
1039                );
1040            }
1041        }
1042
1043        if llvm_from_ci {
1044            let triple = &host_target.triple;
1045            let ci_llvm_bin = ci_llvm_root(&dwn_ctx, llvm_from_ci, &out).join("bin");
1046            let build_target =
1047                target_config.entry(host_target).or_insert_with(|| Target::from_triple(triple));
1048            check_ci_llvm!(build_target.llvm_config);
1049            check_ci_llvm!(build_target.llvm_filecheck);
1050            build_target.llvm_config = Some(ci_llvm_bin.join(exe("llvm-config", host_target)));
1051            build_target.llvm_filecheck = Some(ci_llvm_bin.join(exe("FileCheck", host_target)));
1052        }
1053
1054        for (target, linker_override) in default_linux_linker_overrides() {
1055            // If the user overrode the default Linux linker, do not apply bootstrap defaults
1056            if targets_with_user_linker_override.contains(&target) {
1057                continue;
1058            }
1059
1060            // The rust.lld option is global, and not target specific, so if we enable it, it will
1061            // be applied to all targets being built.
1062            // So we only apply an override if we're building a compiler/host code for the given
1063            // override target.
1064            // Note: we could also make the LLD config per-target, but that would complicate things
1065            if !hosts.contains(&TargetSelection::from_user(&target)) {
1066                continue;
1067            }
1068
1069            let default_linux_linker_override = match linker_override {
1070                DefaultLinuxLinkerOverride::Off => continue,
1071                DefaultLinuxLinkerOverride::SelfContainedLldCc => {
1072                    // If we automatically default to the self-contained LLD linker,
1073                    // we also need to handle the rust.lld option.
1074                    match rust_lld_enabled {
1075                        // If LLD was not enabled explicitly, we enable it, unless LLVM config has
1076                        // been set
1077                        None if !is_host_system_llvm => {
1078                            lld_enabled = true;
1079                            Some(DefaultLinuxLinkerOverride::SelfContainedLldCc)
1080                        }
1081                        None => None,
1082                        // If it was enabled already, we don't need to do anything
1083                        Some(true) => Some(DefaultLinuxLinkerOverride::SelfContainedLldCc),
1084                        // If it was explicitly disabled, we do not apply the
1085                        // linker override
1086                        Some(false) => None,
1087                    }
1088                }
1089            };
1090            if let Some(linker_override) = default_linux_linker_override {
1091                target_config
1092                    .entry(TargetSelection::from_user(&target))
1093                    .or_default()
1094                    .default_linker_linux_override = linker_override;
1095            }
1096        }
1097
1098        let initial_rustfmt = build_rustfmt.or_else(|| maybe_download_rustfmt(&dwn_ctx, &out));
1099
1100        if matches!(bootstrap_override_lld, BootstrapOverrideLld::SelfContained)
1101            && !lld_enabled
1102            && flags_stage.unwrap_or(0) > 0
1103        {
1104            panic!(
1105                "Trying to use self-contained lld as a linker, but LLD is not being added to the sysroot. Enable it with rust.lld = true."
1106            );
1107        }
1108
1109        if lld_enabled && is_host_system_llvm {
1110            panic!("Cannot enable LLD with `rust.lld = true` when using external llvm-config.");
1111        }
1112
1113        let download_rustc = download_rustc_commit.is_some();
1114
1115        let stage = match flags_cmd {
1116            Subcommand::Check { .. } => flags_stage.or(build_check_stage).unwrap_or(1),
1117            Subcommand::Clippy { .. } | Subcommand::Fix => {
1118                flags_stage.or(build_check_stage).unwrap_or(1)
1119            }
1120            // `download-rustc` only has a speed-up for stage2 builds. Default to stage2 unless explicitly overridden.
1121            Subcommand::Doc { .. } => {
1122                flags_stage.or(build_doc_stage).unwrap_or(if download_rustc { 2 } else { 1 })
1123            }
1124            Subcommand::Build { .. } => {
1125                flags_stage.or(build_build_stage).unwrap_or(if download_rustc { 2 } else { 1 })
1126            }
1127            Subcommand::Test { .. } | Subcommand::Miri { .. } => {
1128                flags_stage.or(build_test_stage).unwrap_or(if download_rustc { 2 } else { 1 })
1129            }
1130            Subcommand::Bench { .. } => flags_stage.or(build_bench_stage).unwrap_or(2),
1131            Subcommand::Dist => flags_stage.or(build_dist_stage).unwrap_or(2),
1132            Subcommand::Install => flags_stage.or(build_install_stage).unwrap_or(2),
1133            Subcommand::Perf { .. } => flags_stage.unwrap_or(1),
1134            // Most of the run commands execute bootstrap tools, which don't depend on the compiler.
1135            // Other commands listed here should always use bootstrap tools.
1136            Subcommand::Clean { .. }
1137            | Subcommand::Run { .. }
1138            | Subcommand::Setup { .. }
1139            | Subcommand::Format { .. }
1140            | Subcommand::Vendor { .. } => flags_stage.unwrap_or(0),
1141        };
1142
1143        let local_rebuild = build_local_rebuild.unwrap_or(false);
1144
1145        let check_stage0 = |kind: &str| {
1146            if local_rebuild {
1147                eprintln!("WARNING: running {kind} in stage 0. This might not work as expected.");
1148            } else {
1149                eprintln!(
1150                    "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."
1151                );
1152                exit!(1);
1153            }
1154        };
1155
1156        // Now check that the selected stage makes sense, and if not, print an error and end
1157        match (stage, &flags_cmd) {
1158            (0, Subcommand::Build { .. }) => {
1159                check_stage0("build");
1160            }
1161            (0, Subcommand::Check { .. }) => {
1162                check_stage0("check");
1163            }
1164            (0, Subcommand::Doc { .. }) => {
1165                check_stage0("doc");
1166            }
1167            (0, Subcommand::Clippy { .. }) => {
1168                check_stage0("clippy");
1169            }
1170            (0, Subcommand::Dist) => {
1171                check_stage0("dist");
1172            }
1173            (0, Subcommand::Install) => {
1174                check_stage0("install");
1175            }
1176            (0, Subcommand::Test { .. }) if build_compiletest_allow_stage0 != Some(true) => {
1177                eprintln!(
1178                    "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`."
1179                );
1180                exit!(1);
1181            }
1182            _ => {}
1183        }
1184
1185        if flags_compile_time_deps && !matches!(flags_cmd, Subcommand::Check { .. }) {
1186            eprintln!("ERROR: Can't use --compile-time-deps with any subcommand other than check.");
1187            exit!(1);
1188        }
1189
1190        if matches!(flags_cmd, Subcommand::Fix) {
1191            eprintln!(
1192                "WARNING: `x fix` is provided on a best-effort basis and does not support all `cargo fix` options correctly."
1193            );
1194        }
1195
1196        // CI should always run stage 2 builds, unless it specifically states otherwise
1197        #[cfg(not(test))]
1198        if flags_stage.is_none() && ci_env.is_running_in_ci() {
1199            match flags_cmd {
1200                Subcommand::Test { .. }
1201                | Subcommand::Miri { .. }
1202                | Subcommand::Doc { .. }
1203                | Subcommand::Build { .. }
1204                | Subcommand::Bench { .. }
1205                | Subcommand::Dist
1206                | Subcommand::Install => {
1207                    assert_eq!(
1208                        stage, 2,
1209                        "\
1210x.py was run under CI with an implicit `--stage {stage}`. This is probably wrong and you want stage 2.
1211NOTE: Please add `--stage 2` to your command line, or if you're sure you want to run stage {stage} then add `--stage {stage}` explicitly"
1212                    );
1213                }
1214                Subcommand::Clean { .. }
1215                | Subcommand::Check { .. }
1216                | Subcommand::Clippy { .. }
1217                | Subcommand::Fix
1218                | Subcommand::Run { .. }
1219                | Subcommand::Setup { .. }
1220                | Subcommand::Format { .. }
1221                | Subcommand::Vendor { .. }
1222                | Subcommand::Perf { .. } => {}
1223            }
1224        }
1225
1226        let with_defaults = |debuginfo_level_specific: Option<_>| {
1227            debuginfo_level_specific.or(rust_debuginfo_level).unwrap_or(
1228                if rust_debug == Some(true) {
1229                    DebuginfoLevel::Limited
1230                } else {
1231                    DebuginfoLevel::None
1232                },
1233            )
1234        };
1235
1236        let ccache = match build_ccache {
1237            Some(StringOrBool::String(s)) => Some(s),
1238            Some(StringOrBool::Bool(true)) => Some("ccache".to_string()),
1239            _ => None,
1240        };
1241
1242        let explicit_stage_from_config = build_test_stage.is_some()
1243            || build_build_stage.is_some()
1244            || build_doc_stage.is_some()
1245            || build_dist_stage.is_some()
1246            || build_install_stage.is_some()
1247            || build_check_stage.is_some()
1248            || build_bench_stage.is_some();
1249
1250        let deny_warnings = match flags_warnings {
1251            Warnings::Deny => true,
1252            Warnings::Warn => false,
1253            Warnings::Default => rust_deny_warnings.unwrap_or(true),
1254        };
1255
1256        let gcc_ci_mode = match gcc_download_ci_gcc {
1257            Some(value) => match value {
1258                true => GccCiMode::DownloadFromCi,
1259                false => GccCiMode::BuildLocally,
1260            },
1261            None => GccCiMode::default(),
1262        };
1263
1264        let targets = flags_target
1265            .map(|TargetSelectionList(targets)| targets)
1266            .or_else(|| {
1267                build_target.map(|t| t.iter().map(|t| TargetSelection::from_user(t)).collect())
1268            })
1269            .unwrap_or_else(|| hosts.clone());
1270
1271        #[allow(clippy::map_identity)]
1272        let skip = flags_skip
1273            .into_iter()
1274            .chain(flags_exclude)
1275            .chain(build_exclude.unwrap_or_default())
1276            .map(|p| {
1277                // Never return top-level path here as it would break `--skip`
1278                // logic on rustc's internal test framework which is utilized by compiletest.
1279                #[cfg(windows)]
1280                {
1281                    PathBuf::from(p.to_string_lossy().replace('/', "\\"))
1282                }
1283                #[cfg(not(windows))]
1284                {
1285                    p
1286                }
1287            })
1288            .collect();
1289
1290        let cargo_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/cargo"));
1291        let clippy_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/clippy"));
1292        let in_tree_gcc_info = git_info(&exec_ctx, false, &src.join("src/gcc"));
1293        let in_tree_llvm_info = git_info(&exec_ctx, false, &src.join("src/llvm-project"));
1294        let enzyme_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/enzyme"));
1295        let miri_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/miri"));
1296        let rust_analyzer_info =
1297            git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/rust-analyzer"));
1298        let rustfmt_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/rustfmt"));
1299
1300        let optimized_compiler_builtins =
1301            build_optimized_compiler_builtins.unwrap_or(if channel == "dev" {
1302                CompilerBuiltins::BuildRustOnly
1303            } else {
1304                CompilerBuiltins::BuildLLVMFuncs
1305            });
1306        let vendor = build_vendor.unwrap_or(
1307            rust_info.is_from_tarball()
1308                && src.join("vendor").exists()
1309                && src.join(".cargo/config.toml").exists(),
1310        );
1311        let verbose_tests = rust_verbose_tests.unwrap_or(exec_ctx.is_verbose());
1312
1313        let record_failed_tests_path =
1314            out.join(build_record_failed_tests_path.unwrap_or_else(|| "failed-tests".to_string()));
1315
1316        let paths = {
1317            let mut paths = Vec::new();
1318            if flags_cmd.rerun() {
1319                paths = collect_previously_failed_tests(&record_failed_tests_path);
1320            } else {
1321                paths.extend(flags_paths);
1322            }
1323            paths
1324        };
1325
1326        Config {
1327            // tidy-alphabetical-start
1328            android_ndk: build_android_ndk,
1329            backtrace: rust_backtrace.unwrap_or(true),
1330            backtrace_on_ice: rust_backtrace_on_ice.unwrap_or(false),
1331            bindir: install_bindir.map(PathBuf::from).unwrap_or("bin".into()),
1332            bootstrap_cache_path: build_bootstrap_cache_path,
1333            bootstrap_override_lld,
1334            bypass_bootstrap_lock: flags_bypass_bootstrap_lock,
1335            cargo_info,
1336            cargo_native_static: build_cargo_native_static.unwrap_or(false),
1337            ccache,
1338            change_id: toml.change_id.inner,
1339            channel,
1340            ci_env,
1341            clippy_info,
1342            cmd: flags_cmd,
1343            codegen_tests: rust_codegen_tests.unwrap_or(true),
1344            color: flags_color,
1345            compile_time_deps: flags_compile_time_deps,
1346            compiler_docs: build_compiler_docs.unwrap_or(false),
1347            compiletest_allow_stage0: build_compiletest_allow_stage0.unwrap_or(false),
1348            compiletest_diff_tool: build_compiletest_diff_tool,
1349            config: toml_path,
1350            configure_args: build_configure_args.unwrap_or_default(),
1351            control_flow_guard: rust_control_flow_guard.unwrap_or(false),
1352            datadir: install_datadir.map(PathBuf::from),
1353            deny_warnings,
1354            description: build_description,
1355            dist_compression_formats,
1356            dist_compression_profile: dist_compression_profile.unwrap_or("fast".into()),
1357            dist_include_mingw_linker: dist_include_mingw_linker.unwrap_or(true),
1358            dist_sign_folder: dist_sign_folder.map(PathBuf::from),
1359            dist_upload_addr,
1360            dist_vendor: dist_vendor.unwrap_or_else(|| {
1361                // If we're building from git or tarball sources, enable it by default.
1362                rust_info.is_managed_git_subrepository() || rust_info.is_from_tarball()
1363            }),
1364            docdir: install_docdir.map(PathBuf::from),
1365            docs: build_docs.unwrap_or(true),
1366            docs_minification: build_docs_minification.unwrap_or(true),
1367            download_rustc_commit,
1368            dump_bootstrap_shims: flags_dump_bootstrap_shims,
1369            ehcont_guard: rust_ehcont_guard.unwrap_or(false),
1370            enable_bolt_settings: flags_enable_bolt_settings,
1371            enzyme_info,
1372            exec_ctx,
1373            explicit_stage_from_cli: flags_stage.is_some(),
1374            explicit_stage_from_config,
1375            extended: build_extended.unwrap_or(false),
1376            free_args: flags_free_args,
1377            full_bootstrap: build_full_bootstrap.unwrap_or(false),
1378            gcc_ci_mode,
1379            gdb: build_gdb.map(PathBuf::from),
1380            host_target,
1381            hosts,
1382            in_tree_gcc_info,
1383            in_tree_llvm_info,
1384            include_default_paths: flags_include_default_paths,
1385            incremental: flags_incremental || rust_incremental == Some(true),
1386            initial_cargo,
1387            initial_cargo_clippy: build_cargo_clippy,
1388            initial_rustc,
1389            initial_rustdoc,
1390            initial_rustfmt,
1391            initial_sysroot,
1392            jemalloc: rust_jemalloc.unwrap_or(false),
1393            jobs: Some(threads_from_config(flags_jobs.or(build_jobs).unwrap_or(0))),
1394            json_output: flags_json_output,
1395            keep_stage: flags_keep_stage,
1396            keep_stage_std: flags_keep_stage_std,
1397            libdir: install_libdir.map(PathBuf::from),
1398            libgccjit_libs_dir: gcc_libgccjit_libs_dir,
1399            library_docs_private_items: build_library_docs_private_items.unwrap_or(false),
1400            lld_enabled,
1401            lldb: build_lldb.map(PathBuf::from),
1402            llvm_allow_old_toolchain: llvm_allow_old_toolchain.unwrap_or(false),
1403            llvm_assertions,
1404            llvm_bitcode_linker_enabled: rust_llvm_bitcode_linker.unwrap_or(false),
1405            llvm_build_config: llvm_build_config.clone().unwrap_or(Default::default()),
1406            llvm_cflags,
1407            llvm_clang: llvm_clang.unwrap_or(false),
1408            llvm_clang_cl,
1409            llvm_clang_dir: llvm_clang_dir.map(PathBuf::from),
1410            llvm_cxxflags,
1411            llvm_enable_warnings: llvm_enable_warnings.unwrap_or(false),
1412            llvm_enzyme: llvm_enzyme.unwrap_or(false),
1413            llvm_experimental_targets,
1414            llvm_from_ci,
1415            llvm_ldflags,
1416            llvm_libunwind_default: rust_llvm_libunwind
1417                .map(|v| v.parse().expect("failed to parse rust.llvm-libunwind")),
1418            llvm_libzstd: llvm_libzstd.unwrap_or(false),
1419            llvm_link_jobs,
1420            // If we're building with ThinLTO on, by default we want to link
1421            // to LLVM shared, to avoid re-doing ThinLTO (which happens in
1422            // the link step) with each stage.
1423            llvm_link_shared: Cell::new(
1424                llvm_link_shared
1425                    .or((!llvm_from_ci && llvm_thin_lto.unwrap_or(false)).then_some(true)),
1426            ),
1427            llvm_offload: llvm_offload.unwrap_or(false),
1428            llvm_optimize: llvm_optimize.unwrap_or(true),
1429            llvm_plugins: llvm_plugin.unwrap_or(false),
1430            llvm_polly: llvm_polly.unwrap_or(false),
1431            llvm_profile_generate: flags_llvm_profile_generate,
1432            llvm_profile_use: flags_llvm_profile_use,
1433            llvm_release_debuginfo: llvm_release_debuginfo.unwrap_or(false),
1434            llvm_static_stdcpp: llvm_static_libstdcpp.unwrap_or(false),
1435            llvm_targets,
1436            llvm_tests: llvm_tests.unwrap_or(false),
1437            llvm_thin_lto: llvm_thin_lto.unwrap_or(false),
1438            llvm_tools_enabled: rust_llvm_tools.unwrap_or(true),
1439            llvm_use_libcxx: llvm_use_libcxx.unwrap_or(false),
1440            llvm_use_linker,
1441            llvm_version_suffix,
1442            local_rebuild,
1443            locked_deps: build_locked_deps.unwrap_or(false),
1444            low_priority: build_low_priority.unwrap_or(false),
1445            mandir: install_mandir.map(PathBuf::from),
1446            miri_info,
1447            musl_root: rust_musl_root.map(PathBuf::from),
1448            ninja_in_file: llvm_ninja.unwrap_or(true),
1449            nodejs: build_nodejs.map(PathBuf::from),
1450            omit_git_hash,
1451            on_fail: flags_on_fail,
1452            optimized_compiler_builtins,
1453            out,
1454            patch_binaries_for_nix: build_patch_binaries_for_nix,
1455            path_modification_cache,
1456            paths,
1457            prefix: install_prefix.map(PathBuf::from),
1458            print_step_rusage: build_print_step_rusage.unwrap_or(false),
1459            print_step_timings: build_print_step_timings.unwrap_or(false),
1460            profiler: build_profiler.unwrap_or(false),
1461            python: build_python.map(PathBuf::from),
1462            quiet: flags_quiet,
1463            record_failed_tests_path,
1464            reproducible_artifacts: flags_reproducible_artifact,
1465            reuse: build_reuse.map(PathBuf::from),
1466            rust_analyzer_info,
1467            rust_annotate_moves_size_limit,
1468            rust_break_on_ice: rust_break_on_ice.unwrap_or(true),
1469            rust_codegen_backends: rust_codegen_backends
1470                .map(|backends| parse_codegen_backends(backends, "rust"))
1471                .unwrap_or(vec![CodegenBackendKind::Llvm]),
1472            rust_codegen_units: rust_codegen_units.map(threads_from_config),
1473            rust_codegen_units_std: rust_codegen_units_std.map(threads_from_config),
1474            rust_debug_logging: rust_debug_logging
1475                .or(rust_rustc_debug_assertions)
1476                .unwrap_or(rust_debug == Some(true)),
1477            rust_debuginfo_level_rustc: with_defaults(rust_debuginfo_level_rustc),
1478            rust_debuginfo_level_std: with_defaults(rust_debuginfo_level_std),
1479            rust_debuginfo_level_tests: rust_debuginfo_level_tests.unwrap_or(DebuginfoLevel::None),
1480            rust_debuginfo_level_tools: with_defaults(rust_debuginfo_level_tools),
1481            rust_dist_src: dist_src_tarball.unwrap_or_else(|| rust_dist_src.unwrap_or(true)),
1482            rust_frame_pointers: rust_frame_pointers.unwrap_or(false),
1483            rust_info,
1484            rust_lto: rust_lto
1485                .as_deref()
1486                .map(|value| RustcLto::from_str(value).unwrap())
1487                .unwrap_or_default(),
1488            rust_new_symbol_mangling,
1489            rust_optimize: rust_optimize.unwrap_or(RustOptimize::Bool(true)),
1490            rust_optimize_tests: rust_optimize_tests.unwrap_or(true),
1491            rust_overflow_checks: rust_overflow_checks.unwrap_or(rust_debug == Some(true)),
1492            rust_overflow_checks_std: rust_overflow_checks_std
1493                .or(rust_overflow_checks)
1494                .unwrap_or(rust_debug == Some(true)),
1495            rust_parallel_frontend_threads: rust_parallel_frontend_threads.map(threads_from_config),
1496            rust_profile_generate: flags_rust_profile_generate.or(rust_profile_generate),
1497            rust_profile_use: flags_rust_profile_use.or(rust_profile_use),
1498            rust_randomize_layout: rust_randomize_layout.unwrap_or(false),
1499            rust_remap_debuginfo: rust_remap_debuginfo.unwrap_or(false),
1500            rust_rpath: rust_rpath.unwrap_or(true),
1501            rust_rustflags: rust_rustflags.unwrap_or_default(),
1502            rust_stack_protector,
1503            rust_std_features: rust_std_features
1504                .unwrap_or(BTreeSet::from([String::from("panic-unwind")])),
1505            rust_strip: rust_strip.unwrap_or(false),
1506            rust_thin_lto_import_instr_limit,
1507            rust_validate_mir_opts,
1508            rust_verify_llvm_ir: rust_verify_llvm_ir.unwrap_or(false),
1509            rustc_debug_assertions: rust_rustc_debug_assertions.unwrap_or(rust_debug == Some(true)),
1510            rustc_default_linker: rust_default_linker,
1511            rustc_error_format: flags_rustc_error_format,
1512            rustfmt_info,
1513            sanitizers: build_sanitizers.unwrap_or(false),
1514            save_toolstates: rust_save_toolstates.map(PathBuf::from),
1515            skip,
1516            skip_std_check_if_no_download_rustc: flags_skip_std_check_if_no_download_rustc,
1517            src,
1518            stage,
1519            stage0_metadata,
1520            std_debug_assertions: rust_std_debug_assertions
1521                .or(rust_rustc_debug_assertions)
1522                .unwrap_or(rust_debug == Some(true)),
1523            stderr_is_tty: std::io::stderr().is_terminal(),
1524            stdout_is_tty: std::io::stdout().is_terminal(),
1525            submodules: build_submodules,
1526            sysconfdir: install_sysconfdir.map(PathBuf::from),
1527            target_config,
1528            targets,
1529            test_compare_mode: rust_test_compare_mode.unwrap_or(false),
1530            tidy_extra_checks: build_tidy_extra_checks,
1531            tool: build_tool.unwrap_or_default(),
1532            tools: build_tools,
1533            tools_debug_assertions: rust_tools_debug_assertions
1534                .or(rust_rustc_debug_assertions)
1535                .unwrap_or(rust_debug == Some(true)),
1536            vendor,
1537            verbose_tests,
1538            windows_rc: build_windows_rc.map(PathBuf::from),
1539            yarn: build_yarn.map(PathBuf::from),
1540            // tidy-alphabetical-end
1541        }
1542    }
1543
1544    pub fn dry_run(&self) -> bool {
1545        self.exec_ctx.dry_run()
1546    }
1547
1548    pub fn is_running_on_ci(&self) -> bool {
1549        self.ci_env.is_running_in_ci()
1550    }
1551
1552    pub fn is_explicit_stage(&self) -> bool {
1553        self.explicit_stage_from_cli || self.explicit_stage_from_config
1554    }
1555
1556    pub(crate) fn test_args(&self) -> Vec<&str> {
1557        let mut test_args = match self.cmd {
1558            Subcommand::Test { ref test_args, .. }
1559            | Subcommand::Bench { ref test_args, .. }
1560            | Subcommand::Miri { ref test_args, .. } => {
1561                test_args.iter().flat_map(|s| s.split_whitespace()).collect()
1562            }
1563            _ => vec![],
1564        };
1565        test_args.extend(self.free_args.iter().map(|s| s.as_str()));
1566        test_args
1567    }
1568
1569    pub(crate) fn args(&self) -> Vec<&str> {
1570        let mut args = match self.cmd {
1571            Subcommand::Run { ref args, .. } => {
1572                args.iter().flat_map(|s| s.split_whitespace()).collect()
1573            }
1574            _ => vec![],
1575        };
1576        args.extend(self.free_args.iter().map(|s| s.as_str()));
1577        args
1578    }
1579
1580    /// Returns the content of the given file at a specific commit.
1581    pub(crate) fn read_file_by_commit(&self, file: &Path, commit: &str) -> String {
1582        let dwn_ctx = DownloadContext::from(self);
1583        read_file_by_commit(dwn_ctx, &self.rust_info, file, commit)
1584    }
1585
1586    /// Bootstrap embeds a version number into the name of shared libraries it uploads in CI.
1587    /// Return the version it would have used for the given commit.
1588    pub(crate) fn artifact_version_part(&self, commit: &str) -> String {
1589        let (channel, version) = if self.rust_info.is_managed_git_subrepository() {
1590            let channel =
1591                self.read_file_by_commit(Path::new("src/ci/channel"), commit).trim().to_owned();
1592            let version =
1593                self.read_file_by_commit(Path::new("src/version"), commit).trim().to_owned();
1594            (channel, version)
1595        } else {
1596            let channel = fs::read_to_string(self.src.join("src/ci/channel"));
1597            let version = fs::read_to_string(self.src.join("src/version"));
1598            match (channel, version) {
1599                (Ok(channel), Ok(version)) => {
1600                    (channel.trim().to_owned(), version.trim().to_owned())
1601                }
1602                (channel, version) => {
1603                    let src = self.src.display();
1604                    eprintln!("ERROR: failed to determine artifact channel and/or version");
1605                    eprintln!(
1606                        "HELP: consider using a git checkout or ensure these files are readable"
1607                    );
1608                    if let Err(channel) = channel {
1609                        eprintln!("reading {src}/src/ci/channel failed: {channel:?}");
1610                    }
1611                    if let Err(version) = version {
1612                        eprintln!("reading {src}/src/version failed: {version:?}");
1613                    }
1614                    panic!();
1615                }
1616            }
1617        };
1618
1619        match channel.as_str() {
1620            "stable" => version,
1621            "beta" => channel,
1622            "nightly" => channel,
1623            other => unreachable!("{:?} is not recognized as a valid channel", other),
1624        }
1625    }
1626
1627    /// Try to find the relative path of `bindir`, otherwise return it in full.
1628    pub fn bindir_relative(&self) -> &Path {
1629        let bindir = &self.bindir;
1630        if bindir.is_absolute() {
1631            // Try to make it relative to the prefix.
1632            if let Some(prefix) = &self.prefix
1633                && let Ok(stripped) = bindir.strip_prefix(prefix)
1634            {
1635                return stripped;
1636            }
1637        }
1638        bindir
1639    }
1640
1641    /// Try to find the relative path of `libdir`.
1642    pub fn libdir_relative(&self) -> Option<&Path> {
1643        let libdir = self.libdir.as_ref()?;
1644        if libdir.is_relative() {
1645            Some(libdir)
1646        } else {
1647            // Try to make it relative to the prefix.
1648            libdir.strip_prefix(self.prefix.as_ref()?).ok()
1649        }
1650    }
1651
1652    /// The absolute path to the downloaded LLVM artifacts.
1653    pub(crate) fn ci_llvm_root(&self) -> PathBuf {
1654        let dwn_ctx = DownloadContext::from(self);
1655        ci_llvm_root(dwn_ctx, self.llvm_from_ci, &self.out)
1656    }
1657
1658    /// Directory where the extracted `rustc-dev` component is stored.
1659    pub(crate) fn ci_rustc_dir(&self) -> PathBuf {
1660        assert!(self.download_rustc());
1661        self.out.join(self.host_target).join("ci-rustc")
1662    }
1663
1664    /// Determine whether llvm should be linked dynamically.
1665    ///
1666    /// If `false`, llvm should be linked statically.
1667    /// This is computed on demand since LLVM might have to first be downloaded from CI.
1668    pub(crate) fn llvm_link_shared(&self) -> bool {
1669        let mut opt = self.llvm_link_shared.get();
1670        if opt.is_none() && self.dry_run() {
1671            // just assume static for now - dynamic linking isn't supported on all platforms
1672            return false;
1673        }
1674
1675        let llvm_link_shared = *opt.get_or_insert_with(|| {
1676            if self.llvm_from_ci {
1677                self.maybe_download_ci_llvm();
1678                let ci_llvm = self.ci_llvm_root();
1679                let link_type = t!(
1680                    std::fs::read_to_string(ci_llvm.join("link-type.txt")),
1681                    format!("CI llvm missing: {}", ci_llvm.display())
1682                );
1683                link_type == "dynamic"
1684            } else {
1685                // unclear how thought-through this default is, but it maintains compatibility with
1686                // previous behavior
1687                false
1688            }
1689        });
1690        self.llvm_link_shared.set(opt);
1691        llvm_link_shared
1692    }
1693
1694    /// Return whether we will use a downloaded, pre-compiled version of rustc, or just build from source.
1695    pub(crate) fn download_rustc(&self) -> bool {
1696        self.download_rustc_commit().is_some()
1697    }
1698
1699    pub(crate) fn download_rustc_commit(&self) -> Option<&str> {
1700        static DOWNLOAD_RUSTC: OnceLock<Option<String>> = OnceLock::new();
1701        if self.dry_run() && DOWNLOAD_RUSTC.get().is_none() {
1702            // avoid trying to actually download the commit
1703            return self.download_rustc_commit.as_deref();
1704        }
1705
1706        DOWNLOAD_RUSTC
1707            .get_or_init(|| match &self.download_rustc_commit {
1708                None => None,
1709                Some(commit) => {
1710                    self.download_ci_rustc(commit);
1711
1712                    // CI-rustc can't be used without CI-LLVM. If `self.llvm_from_ci` is false, it means the "if-unchanged"
1713                    // logic has detected some changes in the LLVM submodule (download-ci-llvm=false can't happen here as
1714                    // we don't allow it while parsing the configuration).
1715                    if !self.llvm_from_ci {
1716                        // This happens when LLVM submodule is updated in CI, we should disable ci-rustc without an error
1717                        // to not break CI. For non-CI environments, we should return an error.
1718                        if self.is_running_on_ci() {
1719                            println!("WARNING: LLVM submodule has changes, `download-rustc` will be disabled.");
1720                            return None;
1721                        } else {
1722                            panic!("ERROR: LLVM submodule has changes, `download-rustc` can't be used.");
1723                        }
1724                    }
1725
1726                    if let Some(config_path) = &self.config {
1727                        let ci_config_toml = match self.get_builder_toml("ci-rustc") {
1728                            Ok(ci_config_toml) => ci_config_toml,
1729                            Err(e) if e.to_string().contains("unknown field") => {
1730                                println!("WARNING: CI rustc has some fields that are no longer supported in bootstrap; download-rustc will be disabled.");
1731                                println!("HELP: Consider rebasing to a newer commit if available.");
1732                                return None;
1733                            }
1734                            Err(e) => {
1735                                eprintln!("ERROR: Failed to parse CI rustc bootstrap.toml: {e}");
1736                                exit!(2);
1737                            }
1738                        };
1739
1740                        let current_config_toml = Self::get_toml(config_path).unwrap();
1741
1742                        // Check the config compatibility
1743                        // FIXME: this doesn't cover `--set` flags yet.
1744                        let res = check_incompatible_options_for_ci_rustc(
1745                            self.host_target,
1746                            current_config_toml,
1747                            ci_config_toml,
1748                        );
1749
1750                        // Primarily used by CI runners to avoid handling download-rustc incompatible
1751                        // options one by one on shell scripts.
1752                        let disable_ci_rustc_if_incompatible = env::var_os("DISABLE_CI_RUSTC_IF_INCOMPATIBLE")
1753                            .is_some_and(|s| s == "1" || s == "true");
1754
1755                        if disable_ci_rustc_if_incompatible && res.is_err() {
1756                            println!("WARNING: download-rustc is disabled with `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` env.");
1757                            return None;
1758                        }
1759
1760                        res.unwrap();
1761                    }
1762
1763                    Some(commit.clone())
1764                }
1765            })
1766            .as_deref()
1767    }
1768
1769    /// Runs a function if verbosity is greater than 0
1770    pub fn do_if_verbose(&self, f: impl Fn()) {
1771        self.exec_ctx.do_if_verbose(f);
1772    }
1773
1774    pub fn any_sanitizers_to_build(&self) -> bool {
1775        self.target_config
1776            .iter()
1777            .any(|(ts, t)| !ts.is_msvc() && t.sanitizers.unwrap_or(self.sanitizers))
1778    }
1779
1780    pub fn any_profiler_enabled(&self) -> bool {
1781        self.target_config.values().any(|t| matches!(&t.profiler, Some(p) if p.is_string_or_true()))
1782            || self.profiler
1783    }
1784
1785    /// Returns whether or not submodules should be managed by bootstrap.
1786    pub fn submodules(&self) -> bool {
1787        // If not specified in config, the default is to only manage
1788        // submodules if we're currently inside a git repository.
1789        self.submodules.unwrap_or(self.rust_info.is_managed_git_subrepository())
1790    }
1791
1792    pub fn git_config(&self) -> GitConfig<'_> {
1793        GitConfig {
1794            nightly_branch: &self.stage0_metadata.config.nightly_branch,
1795            git_merge_commit_email: &self.stage0_metadata.config.git_merge_commit_email,
1796        }
1797    }
1798
1799    /// Given a path to the directory of a submodule, update it.
1800    ///
1801    /// `relative_path` should be relative to the root of the git repository, not an absolute path.
1802    ///
1803    /// This *does not* update the submodule if `bootstrap.toml` explicitly says
1804    /// not to, or if we're not in a git repository (like a plain source
1805    /// tarball). Typically [`crate::Build::require_submodule`] should be
1806    /// used instead to provide a nice error to the user if the submodule is
1807    /// missing.
1808    #[cfg_attr(
1809        feature = "tracing",
1810        instrument(
1811            level = "trace",
1812            name = "Config::update_submodule",
1813            skip_all,
1814            fields(relative_path = ?relative_path),
1815        ),
1816    )]
1817    pub(crate) fn update_submodule(&self, relative_path: &str) {
1818        let dwn_ctx = DownloadContext::from(self);
1819        update_submodule(dwn_ctx, &self.rust_info, relative_path);
1820    }
1821
1822    /// Returns true if any of the `paths` have been modified locally.
1823    pub fn has_changes_from_upstream(&self, paths: &[&'static str]) -> bool {
1824        let dwn_ctx = DownloadContext::from(self);
1825        has_changes_from_upstream(dwn_ctx, paths)
1826    }
1827
1828    /// Checks whether any of the given paths have been modified w.r.t. upstream.
1829    pub fn check_path_modifications(&self, paths: &[&'static str]) -> PathFreshness {
1830        // Checking path modifications through git can be relatively expensive (>100ms).
1831        // We do not assume that the sources would change during bootstrap's execution,
1832        // so we can cache the results here.
1833        // Note that we do not use a static variable for the cache, because it would cause problems
1834        // in tests that create separate `Config` instances.
1835        self.path_modification_cache
1836            .lock()
1837            .unwrap()
1838            .entry(paths.to_vec())
1839            .or_insert_with(|| {
1840                check_path_modifications(&self.src, &self.git_config(), paths, self.ci_env).unwrap()
1841            })
1842            .clone()
1843    }
1844
1845    pub fn sanitizers_enabled(&self, target: TargetSelection) -> bool {
1846        self.target_config.get(&target).and_then(|t| t.sanitizers).unwrap_or(self.sanitizers)
1847    }
1848
1849    pub fn needs_sanitizer_runtime_built(&self, target: TargetSelection) -> bool {
1850        // MSVC uses the Microsoft-provided sanitizer runtime, but all other runtimes we build.
1851        !target.is_msvc() && self.sanitizers_enabled(target)
1852    }
1853
1854    pub fn profiler_path(&self, target: TargetSelection) -> Option<&str> {
1855        match self.target_config.get(&target)?.profiler.as_ref()? {
1856            StringOrBool::String(s) => Some(s),
1857            StringOrBool::Bool(_) => None,
1858        }
1859    }
1860
1861    pub fn profiler_enabled(&self, target: TargetSelection) -> bool {
1862        self.target_config
1863            .get(&target)
1864            .and_then(|t| t.profiler.as_ref())
1865            .map(StringOrBool::is_string_or_true)
1866            .unwrap_or(self.profiler)
1867    }
1868
1869    /// Returns codegen backends that should be:
1870    /// - Built and added to the sysroot when we build the compiler.
1871    /// - Distributed when `x dist` is executed (if the codegen backend has a dist step).
1872    pub fn enabled_codegen_backends(&self, target: TargetSelection) -> &[CodegenBackendKind] {
1873        self.target_config
1874            .get(&target)
1875            .and_then(|cfg| cfg.codegen_backends.as_deref())
1876            .unwrap_or(&self.rust_codegen_backends)
1877    }
1878
1879    /// Returns the codegen backend that should be configured as the *default* codegen backend
1880    /// for a rustc compiled by bootstrap.
1881    pub fn default_codegen_backend(&self, target: TargetSelection) -> &CodegenBackendKind {
1882        // We're guaranteed to have always at least one codegen backend listed.
1883        self.enabled_codegen_backends(target).first().unwrap()
1884    }
1885
1886    pub fn jemalloc(&self, target: TargetSelection) -> bool {
1887        self.target_config.get(&target).and_then(|cfg| cfg.jemalloc).unwrap_or(self.jemalloc)
1888    }
1889
1890    pub fn rpath_enabled(&self, target: TargetSelection) -> bool {
1891        self.target_config.get(&target).and_then(|t| t.rpath).unwrap_or(self.rust_rpath)
1892    }
1893
1894    pub fn optimized_compiler_builtins(&self, target: TargetSelection) -> &CompilerBuiltins {
1895        self.target_config
1896            .get(&target)
1897            .and_then(|t| t.optimized_compiler_builtins.as_ref())
1898            .unwrap_or(&self.optimized_compiler_builtins)
1899    }
1900
1901    pub fn llvm_enabled(&self, target: TargetSelection) -> bool {
1902        self.enabled_codegen_backends(target).contains(&CodegenBackendKind::Llvm)
1903    }
1904
1905    pub fn llvm_libunwind(&self, target: TargetSelection) -> LlvmLibunwind {
1906        self.target_config
1907            .get(&target)
1908            .and_then(|t| t.llvm_libunwind)
1909            .or(self.llvm_libunwind_default)
1910            .unwrap_or(
1911                if target.contains("fuchsia")
1912                    || (target.contains("hexagon") && !target.contains("qurt"))
1913                {
1914                    // Fuchsia and Hexagon Linux use in-tree llvm-libunwind.
1915                    // Hexagon QuRT uses libc_eh from the Hexagon SDK instead.
1916                    LlvmLibunwind::InTree
1917                } else {
1918                    LlvmLibunwind::No
1919                },
1920            )
1921    }
1922
1923    pub fn split_debuginfo(&self, target: TargetSelection) -> SplitDebuginfo {
1924        self.target_config
1925            .get(&target)
1926            .and_then(|t| t.split_debuginfo)
1927            .unwrap_or_else(|| SplitDebuginfo::default_for_platform(target))
1928    }
1929
1930    /// Checks if the given target is the same as the host target.
1931    pub fn is_host_target(&self, target: TargetSelection) -> bool {
1932        self.host_target == target
1933    }
1934
1935    /// Returns `true` if this is an external version of LLVM not managed by bootstrap.
1936    /// In particular, we expect llvm sources to be available when this is false.
1937    ///
1938    /// NOTE: this is not the same as `!is_rust_llvm` when `llvm_has_patches` is set.
1939    pub fn is_system_llvm(&self, target: TargetSelection) -> bool {
1940        is_system_llvm(&self.target_config, self.llvm_from_ci, self.host_target, target)
1941    }
1942
1943    /// Returns `true` if this is our custom, patched, version of LLVM.
1944    ///
1945    /// This does not necessarily imply that we're managing the `llvm-project` submodule.
1946    pub fn is_rust_llvm(&self, target: TargetSelection) -> bool {
1947        match self.target_config.get(&target) {
1948            // We're using a user-controlled version of LLVM. The user has explicitly told us whether the version has our patches.
1949            // (They might be wrong, but that's not a supported use-case.)
1950            // In particular, this tries to support `submodules = false` and `patches = false`, for using a newer version of LLVM that's not through `rust-lang/llvm-project`.
1951            Some(Target { llvm_has_rust_patches: Some(patched), .. }) => *patched,
1952            // The user hasn't promised the patches match.
1953            // This only has our patches if it's downloaded from CI or built from source.
1954            _ => !self.is_system_llvm(target),
1955        }
1956    }
1957
1958    pub fn exec_ctx(&self) -> &ExecutionContext {
1959        &self.exec_ctx
1960    }
1961
1962    pub fn git_info(&self, omit_git_hash: bool, dir: &Path) -> GitInfo {
1963        GitInfo::new(omit_git_hash, dir, self)
1964    }
1965}
1966
1967impl AsRef<ExecutionContext> for Config {
1968    fn as_ref(&self) -> &ExecutionContext {
1969        &self.exec_ctx
1970    }
1971}
1972
1973fn compute_src_directory(src_dir: Option<PathBuf>, exec_ctx: &ExecutionContext) -> Option<PathBuf> {
1974    if let Some(src) = src_dir {
1975        return Some(src);
1976    } else {
1977        // Infer the source directory. This is non-trivial because we want to support a downloaded bootstrap binary,
1978        // running on a completely different machine from where it was compiled.
1979        let mut cmd = helpers::git(None);
1980        // NOTE: we cannot support running from outside the repository because the only other path we have available
1981        // is set at compile time, which can be wrong if bootstrap was downloaded rather than compiled locally.
1982        // We still support running outside the repository if we find we aren't in a git directory.
1983
1984        // NOTE: We get a relative path from git to work around an issue on MSYS/mingw. If we used an absolute path,
1985        // and end up using MSYS's git rather than git-for-windows, we would get a unix-y MSYS path. But as bootstrap
1986        // has already been (kinda-cross-)compiled to Windows land, we require a normal Windows path.
1987        cmd.arg("rev-parse").arg("--show-cdup");
1988        // Discard stderr because we expect this to fail when building from a tarball.
1989        let output = cmd.allow_failure().run_capture_stdout(exec_ctx);
1990        if output.is_success() {
1991            let git_root_relative = output.stdout();
1992            // We need to canonicalize this path to make sure it uses backslashes instead of forward slashes,
1993            // and to resolve any relative components.
1994            let git_root = env::current_dir()
1995                .unwrap()
1996                .join(PathBuf::from(git_root_relative.trim()))
1997                .canonicalize()
1998                .unwrap();
1999            let s = git_root.to_str().unwrap();
2000
2001            // Bootstrap is quite bad at handling /? in front of paths
2002            let git_root = match s.strip_prefix("\\\\?\\") {
2003                Some(p) => PathBuf::from(p),
2004                None => git_root,
2005            };
2006            // If this doesn't have at least `stage0`, we guessed wrong. This can happen when,
2007            // for example, the build directory is inside of another unrelated git directory.
2008            // In that case keep the original `CARGO_MANIFEST_DIR` handling.
2009            //
2010            // NOTE: this implies that downloadable bootstrap isn't supported when the build directory is outside
2011            // the source directory. We could fix that by setting a variable from all three of python, ./x, and x.ps1.
2012            if git_root.join("src").join("stage0").exists() {
2013                return Some(git_root);
2014            }
2015        } else {
2016            // We're building from a tarball, not git sources.
2017            // We don't support pre-downloaded bootstrap in this case.
2018        }
2019    };
2020    None
2021}
2022
2023/// Loads bootstrap TOML config and returns the config together with a path from where
2024/// it was loaded.
2025/// `src` is the source root directory, and `config_path` is an optionally provided path to the
2026/// config.
2027fn load_toml_config(
2028    src: &Path,
2029    config_path: Option<PathBuf>,
2030    get_toml: &impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>,
2031) -> (TomlConfig, Option<PathBuf>) {
2032    // Locate the configuration file using the following priority (first match wins):
2033    // 1. `--config <path>` (explicit flag)
2034    // 2. `RUST_BOOTSTRAP_CONFIG` environment variable
2035    // 3. `./bootstrap.toml` (local file)
2036    // 4. `<root>/bootstrap.toml`
2037    // 5. `./config.toml` (fallback for backward compatibility)
2038    // 6. `<root>/config.toml`
2039    let toml_path = config_path.or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from));
2040    let using_default_path = toml_path.is_none();
2041    let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("bootstrap.toml"));
2042
2043    if using_default_path && !toml_path.exists() {
2044        toml_path = src.join(PathBuf::from("bootstrap.toml"));
2045        if !toml_path.exists() {
2046            toml_path = PathBuf::from("config.toml");
2047            if !toml_path.exists() {
2048                toml_path = src.join(PathBuf::from("config.toml"));
2049            }
2050        }
2051    }
2052
2053    // Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path,
2054    // but not if `bootstrap.toml` hasn't been created.
2055    if !using_default_path || toml_path.exists() {
2056        let path = Some(if cfg!(not(test)) {
2057            toml_path = toml_path.canonicalize().unwrap();
2058            toml_path.clone()
2059        } else {
2060            toml_path.clone()
2061        });
2062        (get_toml(&toml_path).unwrap_or_else(|e| bad_config(&toml_path, e)), path)
2063    } else {
2064        (TomlConfig::default(), None)
2065    }
2066}
2067
2068fn postprocess_toml(
2069    toml: &mut TomlConfig,
2070    src_dir: &Path,
2071    toml_path: Option<PathBuf>,
2072    exec_ctx: &ExecutionContext,
2073    override_set: &[String],
2074    get_toml: &impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>,
2075) {
2076    let git_info = GitInfo::new(false, src_dir, exec_ctx);
2077
2078    if git_info.is_from_tarball() && toml.profile.is_none() {
2079        toml.profile = Some("dist".into());
2080    }
2081
2082    // Reverse the list to ensure the last added config extension remains the most dominant.
2083    // For example, given ["a.toml", "b.toml"], "b.toml" should take precedence over "a.toml".
2084    //
2085    // This must be handled before applying the `profile` since `include`s should always take
2086    // precedence over `profile`s.
2087    for include_path in toml.include.clone().unwrap_or_default().iter().rev() {
2088        let include_path = toml_path
2089            .as_ref()
2090            .expect("include found in default TOML config")
2091            .parent()
2092            .unwrap()
2093            .join(include_path);
2094
2095        let included_toml =
2096            get_toml(&include_path).unwrap_or_else(|e| bad_config(&include_path, e));
2097        toml.merge(
2098            Some(include_path),
2099            &mut Default::default(),
2100            included_toml,
2101            ReplaceOpt::IgnoreDuplicate,
2102        );
2103    }
2104
2105    if let Some(include) = &toml.profile {
2106        // Allows creating alias for profile names, allowing
2107        // profiles to be renamed while maintaining back compatibility
2108        // Keep in sync with `profile_aliases` in bootstrap.py
2109        let profile_aliases = HashMap::from([("user", "dist")]);
2110        let include = match profile_aliases.get(include.as_str()) {
2111            Some(alias) => alias,
2112            None => include.as_str(),
2113        };
2114        let mut include_path = PathBuf::from(src_dir);
2115        include_path.push("src");
2116        include_path.push("bootstrap");
2117        include_path.push("defaults");
2118        include_path.push(format!("bootstrap.{include}.toml"));
2119        let included_toml = get_toml(&include_path).unwrap_or_else(|e| {
2120            eprintln!(
2121                "ERROR: Failed to parse default config profile at '{}': {e}",
2122                include_path.display()
2123            );
2124            exit!(2);
2125        });
2126        toml.merge(
2127            Some(include_path),
2128            &mut Default::default(),
2129            included_toml,
2130            ReplaceOpt::IgnoreDuplicate,
2131        );
2132    }
2133
2134    let mut override_toml = TomlConfig::default();
2135    for option in override_set.iter() {
2136        fn get_table(option: &str) -> Result<TomlConfig, toml::de::Error> {
2137            toml::from_str(option).and_then(|table: toml::Value| TomlConfig::deserialize(table))
2138        }
2139
2140        let mut err = match get_table(option) {
2141            Ok(v) => {
2142                override_toml.merge(None, &mut Default::default(), v, ReplaceOpt::ErrorOnDuplicate);
2143                continue;
2144            }
2145            Err(e) => e,
2146        };
2147        // We want to be able to set string values without quotes,
2148        // like in `configure.py`. Try adding quotes around the right hand side
2149        if let Some((key, value)) = option.split_once('=')
2150            && !value.contains('"')
2151        {
2152            match get_table(&format!(r#"{key}="{value}""#)) {
2153                Ok(v) => {
2154                    override_toml.merge(
2155                        None,
2156                        &mut Default::default(),
2157                        v,
2158                        ReplaceOpt::ErrorOnDuplicate,
2159                    );
2160                    continue;
2161                }
2162                Err(e) => err = e,
2163            }
2164        }
2165        eprintln!("failed to parse override `{option}`: `{err}");
2166        exit!(2)
2167    }
2168    toml.merge(None, &mut Default::default(), override_toml, ReplaceOpt::Override);
2169}
2170
2171#[cfg(test)]
2172pub fn check_stage0_version(
2173    _program_path: &Path,
2174    _component_name: &'static str,
2175    _src_dir: &Path,
2176    _exec_ctx: &ExecutionContext,
2177) {
2178}
2179
2180/// check rustc/cargo version is same or lower with 1 apart from the building one
2181#[cfg(not(test))]
2182pub fn check_stage0_version(
2183    program_path: &Path,
2184    component_name: &'static str,
2185    src_dir: &Path,
2186    exec_ctx: &ExecutionContext,
2187) {
2188    use build_helper::util::fail;
2189
2190    if exec_ctx.dry_run() {
2191        return;
2192    }
2193
2194    let stage0_output =
2195        command(program_path).arg("--version").run_capture_stdout(exec_ctx).stdout();
2196    let mut stage0_output = stage0_output.lines().next().unwrap().split(' ');
2197
2198    let stage0_name = stage0_output.next().unwrap();
2199    if stage0_name != component_name {
2200        fail(&format!(
2201            "Expected to find {component_name} at {} but it claims to be {stage0_name}",
2202            program_path.display()
2203        ));
2204    }
2205
2206    let stage0_version =
2207        semver::Version::parse(stage0_output.next().unwrap().split('-').next().unwrap().trim())
2208            .unwrap();
2209    let source_version =
2210        semver::Version::parse(fs::read_to_string(src_dir.join("src/version")).unwrap().trim())
2211            .unwrap();
2212    if !(source_version == stage0_version
2213        || (source_version.major == stage0_version.major
2214            && (source_version.minor == stage0_version.minor
2215                || source_version.minor == stage0_version.minor + 1)))
2216    {
2217        let prev_version = format!("{}.{}.x", source_version.major, source_version.minor - 1);
2218        fail(&format!(
2219            "Unexpected {component_name} version: {stage0_version}, we should use {prev_version}/{source_version} to build source with {source_version}"
2220        ));
2221    }
2222}
2223
2224fn print_rustc_modifications(
2225    dwn_ctx: &DownloadContext<'_>,
2226    if_unchanged: bool,
2227    mut modifications: Vec<PathBuf>,
2228) -> Option<()> {
2229    if !dwn_ctx.exec_ctx.is_verbose() {
2230        modifications.retain(|path| !path.starts_with("compiler"));
2231    }
2232    if modifications.is_empty() {
2233        // only compiler changes; still force a rebuild but don't say why.
2234        eprintln!(
2235            "skipping rustc download with `download-rustc = 'if-unchanged'` due to local changes"
2236        );
2237        return None;
2238    }
2239
2240    eprintln!(
2241        "NOTE: detected {} modifications that could affect a build of rustc",
2242        modifications.len()
2243    );
2244    for file in modifications.iter().take(10) {
2245        eprintln!("- {}", file.display());
2246    }
2247    if modifications.len() > 10 {
2248        eprintln!("- ... and {} more", modifications.len() - 10);
2249    }
2250
2251    if if_unchanged {
2252        eprintln!("skipping rustc download due to `download-rustc = 'if-unchanged'`");
2253        None
2254    } else {
2255        eprintln!("downloading unconditionally due to `download-rustc = true`");
2256        Some(())
2257    }
2258}
2259
2260pub fn download_ci_rustc_commit<'a>(
2261    dwn_ctx: impl AsRef<DownloadContext<'a>>,
2262    rust_info: &channel::GitInfo,
2263    download_rustc: Option<StringOrBool>,
2264    llvm_assertions: bool,
2265) -> Option<String> {
2266    let dwn_ctx = dwn_ctx.as_ref();
2267
2268    if !is_download_ci_available(&dwn_ctx.host_target.triple, llvm_assertions) {
2269        return None;
2270    }
2271
2272    // If `download-rustc` is not set, default to rebuilding.
2273    let if_unchanged = match download_rustc {
2274        // Globally default `download-rustc` to `false`, because some contributors don't use
2275        // profiles for reasons such as:
2276        // - They need to seamlessly switch between compiler/library work.
2277        // - They don't want to use compiler profile because they need to override too many
2278        //   things and it's easier to not use a profile.
2279        None | Some(StringOrBool::Bool(false)) => return None,
2280        Some(StringOrBool::Bool(true)) => false,
2281        Some(StringOrBool::String(s)) if s == "if-unchanged" => {
2282            if !rust_info.is_managed_git_subrepository() {
2283                println!(
2284                    "ERROR: `download-rustc=if-unchanged` is only compatible with Git managed sources."
2285                );
2286                crate::exit!(1);
2287            }
2288
2289            true
2290        }
2291        Some(StringOrBool::String(other)) => {
2292            panic!("unrecognized option for download-rustc: {other}")
2293        }
2294    };
2295
2296    let commit = if rust_info.is_managed_git_subrepository() {
2297        // Look for a version to compare to based on the current commit.
2298        // Only commits merged by bors will have CI artifacts.
2299        let freshness = check_path_modifications_(dwn_ctx, RUSTC_IF_UNCHANGED_ALLOWED_PATHS);
2300        dwn_ctx.exec_ctx.do_if_verbose(|| {
2301            eprintln!("rustc freshness: {freshness:?}");
2302        });
2303        match freshness {
2304            PathFreshness::LastModifiedUpstream { upstream } => upstream,
2305            PathFreshness::HasLocalModifications { upstream, modifications } => {
2306                if dwn_ctx.is_running_on_ci() {
2307                    eprintln!("CI rustc commit matches with HEAD and we are in CI.");
2308                    eprintln!(
2309                        "`rustc.download-ci` functionality will be skipped as artifacts are not available."
2310                    );
2311                    return None;
2312                }
2313
2314                print_rustc_modifications(dwn_ctx, if_unchanged, modifications)?;
2315                upstream
2316            }
2317            PathFreshness::MissingUpstream => {
2318                eprintln!("No upstream commit found");
2319                return None;
2320            }
2321        }
2322    } else {
2323        channel::read_commit_info_file(dwn_ctx.src)
2324            .map(|info| info.sha.trim().to_owned())
2325            .expect("git-commit-info is missing in the project root")
2326    };
2327
2328    Some(commit)
2329}
2330
2331pub fn check_path_modifications_<'a>(
2332    dwn_ctx: impl AsRef<DownloadContext<'a>>,
2333    paths: &[&'static str],
2334) -> PathFreshness {
2335    let dwn_ctx = dwn_ctx.as_ref();
2336    // Checking path modifications through git can be relatively expensive (>100ms).
2337    // We do not assume that the sources would change during bootstrap's execution,
2338    // so we can cache the results here.
2339    // Note that we do not use a static variable for the cache, because it would cause problems
2340    // in tests that create separate `Config` instances.
2341    dwn_ctx
2342        .path_modification_cache
2343        .lock()
2344        .unwrap()
2345        .entry(paths.to_vec())
2346        .or_insert_with(|| {
2347            check_path_modifications(
2348                dwn_ctx.src,
2349                &git_config(dwn_ctx.stage0_metadata),
2350                paths,
2351                dwn_ctx.ci_env,
2352            )
2353            .unwrap()
2354        })
2355        .clone()
2356}
2357
2358pub fn git_config(stage0_metadata: &build_helper::stage0_parser::Stage0) -> GitConfig<'_> {
2359    GitConfig {
2360        nightly_branch: &stage0_metadata.config.nightly_branch,
2361        git_merge_commit_email: &stage0_metadata.config.git_merge_commit_email,
2362    }
2363}
2364
2365pub fn parse_download_ci_llvm<'a>(
2366    dwn_ctx: impl AsRef<DownloadContext<'a>>,
2367    rust_info: &channel::GitInfo,
2368    download_rustc_commit: &Option<String>,
2369    download_ci_llvm: Option<StringOrBool>,
2370    asserts: bool,
2371) -> bool {
2372    let dwn_ctx = dwn_ctx.as_ref();
2373    let download_ci_llvm = download_ci_llvm.unwrap_or(StringOrBool::Bool(true));
2374
2375    let if_unchanged = || {
2376        if rust_info.is_from_tarball() {
2377            // Git is needed for running "if-unchanged" logic.
2378            println!("ERROR: 'if-unchanged' is only compatible with Git managed sources.");
2379            crate::exit!(1);
2380        }
2381
2382        // Fetching the LLVM submodule is unnecessary for self-tests.
2383        #[cfg(not(test))]
2384        update_submodule(dwn_ctx, rust_info, "src/llvm-project");
2385
2386        // Check for untracked changes in `src/llvm-project` and other important places.
2387        let has_changes = has_changes_from_upstream(dwn_ctx, LLVM_INVALIDATION_PATHS);
2388
2389        // Return false if there are untracked changes, otherwise check if CI LLVM is available.
2390        if has_changes {
2391            false
2392        } else {
2393            llvm::is_ci_llvm_available_for_target(&dwn_ctx.host_target, asserts)
2394        }
2395    };
2396
2397    match download_ci_llvm {
2398        StringOrBool::Bool(b) => {
2399            if !b && download_rustc_commit.is_some() {
2400                panic!(
2401                    "`llvm.download-ci-llvm` cannot be set to `false` if `rust.download-rustc` is set to `true` or `if-unchanged`."
2402                );
2403            }
2404
2405            #[cfg(not(test))]
2406            if b && dwn_ctx.is_running_on_ci() && CiEnv::is_rust_lang_managed_ci_job() {
2407                // On rust-lang CI, we must always rebuild LLVM if there were any modifications to it
2408                panic!(
2409                    "`llvm.download-ci-llvm` cannot be set to `true` on CI. Use `if-unchanged` instead."
2410                );
2411            }
2412
2413            // If download-ci-llvm=true we also want to check that CI llvm is available
2414            b && llvm::is_ci_llvm_available_for_target(&dwn_ctx.host_target, asserts)
2415        }
2416        StringOrBool::String(s) if s == "if-unchanged" => if_unchanged(),
2417        StringOrBool::String(other) => {
2418            panic!("unrecognized option for download-ci-llvm: {other:?}")
2419        }
2420    }
2421}
2422
2423pub fn has_changes_from_upstream<'a>(
2424    dwn_ctx: impl AsRef<DownloadContext<'a>>,
2425    paths: &[&'static str],
2426) -> bool {
2427    let dwn_ctx = dwn_ctx.as_ref();
2428    match check_path_modifications_(dwn_ctx, paths) {
2429        PathFreshness::LastModifiedUpstream { .. } => false,
2430        PathFreshness::HasLocalModifications { .. } | PathFreshness::MissingUpstream => true,
2431    }
2432}
2433
2434#[cfg_attr(
2435    feature = "tracing",
2436    instrument(
2437        level = "trace",
2438        name = "Config::update_submodule",
2439        skip_all,
2440        fields(relative_path = ?relative_path),
2441    ),
2442)]
2443pub(crate) fn update_submodule<'a>(
2444    dwn_ctx: impl AsRef<DownloadContext<'a>>,
2445    rust_info: &channel::GitInfo,
2446    relative_path: &str,
2447) {
2448    let dwn_ctx = dwn_ctx.as_ref();
2449    if rust_info.is_from_tarball() || !submodules_(dwn_ctx.submodules, rust_info) {
2450        return;
2451    }
2452
2453    let absolute_path = dwn_ctx.src.join(relative_path);
2454
2455    // NOTE: This check is required because `jj git clone` doesn't create directories for
2456    // submodules, they are completely ignored. The code below assumes this directory exists,
2457    // so create it here.
2458    if !absolute_path.exists() {
2459        t!(fs::create_dir_all(&absolute_path));
2460    }
2461
2462    // NOTE: The check for the empty directory is here because when running x.py the first time,
2463    // the submodule won't be checked out. Check it out now so we can build it.
2464    if !git_info(dwn_ctx.exec_ctx, false, &absolute_path).is_managed_git_subrepository()
2465        && !helpers::dir_is_empty(&absolute_path)
2466    {
2467        return;
2468    }
2469
2470    let submodule_git = || helpers::git(Some(&absolute_path));
2471
2472    // Determine commit checked out in submodule.
2473    let checked_out_hash =
2474        submodule_git().args(["rev-parse", "HEAD"]).run_capture_stdout(dwn_ctx.exec_ctx).stdout();
2475    let checked_out_hash = checked_out_hash.trim_end();
2476    // Determine commit that the submodule *should* have.
2477    let recorded = helpers::git(Some(dwn_ctx.src))
2478        .run_in_dry_run() // otherwise parsing `actual_hash` fails
2479        .args(["ls-tree", "HEAD"])
2480        .arg(relative_path)
2481        .run_capture_stdout(dwn_ctx.exec_ctx)
2482        .stdout();
2483
2484    let actual_hash = recorded
2485        .split_whitespace()
2486        .nth(2)
2487        .unwrap_or_else(|| panic!("unexpected output `{recorded}` when updating {relative_path}"));
2488
2489    if actual_hash == checked_out_hash {
2490        // already checked out
2491        return;
2492    }
2493
2494    if !dwn_ctx.exec_ctx.dry_run() {
2495        println!("Updating submodule {relative_path}");
2496    };
2497
2498    helpers::git(Some(dwn_ctx.src))
2499        .allow_failure()
2500        .args(["submodule", "-q", "sync"])
2501        .arg(relative_path)
2502        .run(dwn_ctx.exec_ctx);
2503
2504    // Try passing `--progress` to start, then run git again without if that fails.
2505    let update = |progress: bool| {
2506        // Git is buggy and will try to fetch submodules from the tracking branch for *this* repository,
2507        // even though that has no relation to the upstream for the submodule.
2508        let current_branch = helpers::git(Some(dwn_ctx.src))
2509            .allow_failure()
2510            .args(["symbolic-ref", "--short", "HEAD"])
2511            .run_capture(dwn_ctx.exec_ctx);
2512
2513        let mut git = helpers::git(Some(dwn_ctx.src)).allow_failure();
2514        if current_branch.is_success() {
2515            // If there is a tag named after the current branch, git will try to disambiguate by prepending `heads/` to the branch name.
2516            // This syntax isn't accepted by `branch.{branch}`. Strip it.
2517            let branch = current_branch.stdout();
2518            let branch = branch.trim();
2519            let branch = branch.strip_prefix("heads/").unwrap_or(branch);
2520            git.arg("-c").arg(format!("branch.{branch}.remote=origin"));
2521        }
2522        git.args(["submodule", "update", "--init", "--recursive", "--depth=1"]);
2523        if progress {
2524            git.arg("--progress");
2525        }
2526        git.arg(relative_path);
2527        git
2528    };
2529    if !update(true).allow_failure().run(dwn_ctx.exec_ctx) {
2530        update(false).allow_failure().run(dwn_ctx.exec_ctx);
2531    }
2532
2533    // Save any local changes, but avoid running `git stash pop` if there are none (since it will exit with an error).
2534    // diff-index reports the modifications through the exit status
2535    let has_local_modifications = !submodule_git()
2536        .allow_failure()
2537        .args(["diff-index", "--quiet", "HEAD"])
2538        .run(dwn_ctx.exec_ctx);
2539    if has_local_modifications {
2540        submodule_git().allow_failure().args(["stash", "push"]).run(dwn_ctx.exec_ctx);
2541    }
2542
2543    submodule_git().allow_failure().args(["reset", "-q", "--hard"]).run(dwn_ctx.exec_ctx);
2544    submodule_git().allow_failure().args(["clean", "-qdfx"]).run(dwn_ctx.exec_ctx);
2545
2546    if has_local_modifications {
2547        submodule_git().allow_failure().args(["stash", "pop"]).run(dwn_ctx.exec_ctx);
2548    }
2549}
2550
2551pub fn git_info(exec_ctx: &ExecutionContext, omit_git_hash: bool, dir: &Path) -> GitInfo {
2552    GitInfo::new(omit_git_hash, dir, exec_ctx)
2553}
2554
2555pub fn submodules_(submodules: &Option<bool>, rust_info: &channel::GitInfo) -> bool {
2556    // If not specified in config, the default is to only manage
2557    // submodules if we're currently inside a git repository.
2558    submodules.unwrap_or(rust_info.is_managed_git_subrepository())
2559}
2560
2561/// Returns `true` if this is an external version of LLVM not managed by bootstrap.
2562/// In particular, we expect llvm sources to be available when this is false.
2563///
2564/// NOTE: this is not the same as `!is_rust_llvm` when `llvm_has_patches` is set.
2565pub fn is_system_llvm(
2566    target_config: &HashMap<TargetSelection, Target>,
2567    llvm_from_ci: bool,
2568    host_target: TargetSelection,
2569    target: TargetSelection,
2570) -> bool {
2571    match target_config.get(&target) {
2572        Some(Target { llvm_config: Some(_), .. }) => {
2573            let ci_llvm = llvm_from_ci && is_host_target(&host_target, &target);
2574            !ci_llvm
2575        }
2576        // We're building from the in-tree src/llvm-project sources.
2577        Some(Target { llvm_config: None, .. }) => false,
2578        None => false,
2579    }
2580}
2581
2582pub fn is_host_target(host_target: &TargetSelection, target: &TargetSelection) -> bool {
2583    host_target == target
2584}
2585
2586pub(crate) fn ci_llvm_root<'a>(
2587    dwn_ctx: impl AsRef<DownloadContext<'a>>,
2588    llvm_from_ci: bool,
2589    out: &Path,
2590) -> PathBuf {
2591    let dwn_ctx = dwn_ctx.as_ref();
2592    assert!(llvm_from_ci);
2593    out.join(dwn_ctx.host_target).join("ci-llvm")
2594}
2595
2596/// Returns the content of the given file at a specific commit.
2597pub(crate) fn read_file_by_commit<'a>(
2598    dwn_ctx: impl AsRef<DownloadContext<'a>>,
2599    rust_info: &channel::GitInfo,
2600    file: &Path,
2601    commit: &str,
2602) -> String {
2603    let dwn_ctx = dwn_ctx.as_ref();
2604    assert!(
2605        rust_info.is_managed_git_subrepository(),
2606        "`Config::read_file_by_commit` is not supported in non-git sources."
2607    );
2608
2609    let mut git = helpers::git(Some(dwn_ctx.src));
2610    git.arg("show").arg(format!("{commit}:{}", file.to_str().unwrap()));
2611    git.run_capture_stdout(dwn_ctx.exec_ctx).stdout()
2612}
2613
2614fn bad_config(toml_path: &Path, e: toml::de::Error) -> ! {
2615    eprintln!("ERROR: Failed to parse '{}': {e}", toml_path.display());
2616    let e_s = e.to_string();
2617    if e_s.contains("unknown field")
2618        && let Some(field_name) = e_s.split("`").nth(1)
2619        && let sections = find_correct_section_for_field(field_name)
2620        && !sections.is_empty()
2621    {
2622        if sections.len() == 1 {
2623            match sections[0] {
2624                WouldBeValidFor::TopLevel { is_section } => {
2625                    if is_section {
2626                        eprintln!(
2627                            "hint: section name `{field_name}` used as a key within a section"
2628                        );
2629                    } else {
2630                        eprintln!("hint: try using `{field_name}` as a top level key");
2631                    }
2632                }
2633                WouldBeValidFor::Section(section) => {
2634                    eprintln!("hint: try moving `{field_name}` to the `{section}` section")
2635                }
2636            }
2637        } else {
2638            eprintln!(
2639                "hint: `{field_name}` would be valid {}",
2640                join_oxford_comma(sections.iter(), "or"),
2641            );
2642        }
2643    }
2644
2645    exit!(2);
2646}
2647
2648#[derive(Copy, Clone, Debug)]
2649enum WouldBeValidFor {
2650    TopLevel { is_section: bool },
2651    Section(&'static str),
2652}
2653
2654fn join_oxford_comma(
2655    mut parts: impl ExactSizeIterator<Item = impl std::fmt::Display>,
2656    conj: &str,
2657) -> String {
2658    use std::fmt::Write;
2659    let mut out = String::new();
2660
2661    assert!(parts.len() > 1);
2662    while let Some(part) = parts.next() {
2663        if parts.len() == 0 {
2664            write!(&mut out, "{conj} {part}")
2665        } else {
2666            write!(&mut out, "{part}, ")
2667        }
2668        .unwrap();
2669    }
2670    out
2671}
2672
2673impl std::fmt::Display for WouldBeValidFor {
2674    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2675        match self {
2676            Self::TopLevel { .. } => write!(f, "at top level"),
2677            Self::Section(section_name) => write!(f, "in section `{section_name}`"),
2678        }
2679    }
2680}
2681
2682fn find_correct_section_for_field(field_name: &str) -> Vec<WouldBeValidFor> {
2683    let sections = ["build", "install", "llvm", "gcc", "rust", "dist"];
2684    sections
2685        .iter()
2686        .map(Some)
2687        .chain([None])
2688        .filter_map(|section_name| {
2689            let dummy_config_str = if let Some(section_name) = section_name {
2690                format!("{section_name}.{field_name} = 0\n")
2691            } else {
2692                format!("{field_name} = 0\n")
2693            };
2694            let is_unknown_field = toml::from_str::<toml::Value>(&dummy_config_str)
2695                .and_then(TomlConfig::deserialize)
2696                .err()
2697                .is_some_and(|e| e.to_string().contains("unknown field"));
2698            if is_unknown_field {
2699                None
2700            } else {
2701                Some(section_name.copied().map(WouldBeValidFor::Section).unwrap_or_else(|| {
2702                    WouldBeValidFor::TopLevel { is_section: sections.contains(&field_name) }
2703                }))
2704            }
2705        })
2706        .collect()
2707}