bootstrap/core/config/
config.rs

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