bootstrap/core/config/
config.rs

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