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