Skip to main content

bootstrap/core/config/
config.rs

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