src/bootstrap/src/core/config/config.rs RUST 2,706 lines View on github.com → Search inside
File is large — showing lines 1–2,000 of 2,706.
1//! This module defines the central `Config` struct, which aggregates all components2//! 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 as7//! utility methods for querying and manipulating the complete configuration state.8//!9//! Additionally, this module contains the core logic for parsing, validating, and inferring10//! 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 performing14//! cross-component validation. The main `parse_inner` function and its supporting15//! 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};2324use 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};3031use crate::core::build_steps::llvm;32use crate::core::build_steps::llvm::LLVM_INVALIDATION_PATHS;33use crate::core::build_steps::test::failed_tests::collect_previously_failed_tests;34pub use crate::core::config::flags::Subcommand;35use crate::core::config::flags::{Color, Flags, Warnings};36use crate::core::config::target_selection::TargetSelectionList;37use crate::core::config::toml::TomlConfig;38use crate::core::config::toml::build::{Build, Tool};39use crate::core::config::toml::change_id::ChangeId;40use crate::core::config::toml::dist::Dist;41use crate::core::config::toml::gcc::Gcc;42use crate::core::config::toml::install::Install;43use crate::core::config::toml::llvm::Llvm;44use crate::core::config::toml::rust::{45    BootstrapOverrideLld, Rust, RustOptimize, check_incompatible_options_for_ci_rustc,46    parse_codegen_backends,47};48use crate::core::config::toml::target::{49    DefaultLinuxLinkerOverride, Target, TomlTarget, default_linux_linker_overrides,50};51use crate::core::config::{52    CompilerBuiltins, DebuginfoLevel, DryRun, GccCiMode, LlvmLibunwind, Merge, ReplaceOpt,53    RustcLto, SplitDebuginfo, StringOrBool, threads_from_config,54};55use crate::core::download::{56    DownloadContext, download_beta_toolchain, is_download_ci_available, maybe_download_rustfmt,57};58use crate::utils::channel;59use crate::utils::exec::{ExecutionContext, command};60use crate::utils::helpers::{exe, get_host_target};61use crate::{CodegenBackendKind, GitInfo, OnceLock, TargetSelection, check_ci_llvm, helpers, t};6263/// Each path in this list is considered "allowed" in the `download-rustc="if-unchanged"` logic.64/// This means they can be modified and changes to these paths should never trigger a compiler build65/// when "if-unchanged" is set.66///67/// NOTE: Paths must have the ":!" prefix to tell git to ignore changes in those paths during68/// the diff check.69///70/// WARNING: Be cautious when adding paths to this list. If a path that influences the compiler build71/// is added here, it will cause bootstrap to skip necessary rebuilds, which may lead to risky results.72/// For example, "src/bootstrap" should never be included in this list as it plays a crucial role in the73/// final output/compiler, which can be significantly affected by changes made to the bootstrap sources.74#[rustfmt::skip] // We don't want rustfmt to oneline this list75pub const RUSTC_IF_UNCHANGED_ALLOWED_PATHS: &[&str] = &[76    ":!library",77    ":!src/tools",78    ":!src/librustdoc",79    ":!src/rustdoc-json-types",80    ":!tests",81    ":!triagebot.toml",82    ":!src/bootstrap/defaults",83];8485/// Global configuration for the entire build and/or bootstrap.86///87/// This structure is parsed from `bootstrap.toml`, and some of the fields are inferred from `git` or build-time parameters.88///89/// Note that this structure is not decoded directly into, but rather it is90/// filled out from the decoded forms of the structs below. For documentation91/// on each field, see the corresponding fields in92/// `bootstrap.example.toml`.93#[derive(Clone)]94pub struct Config {95    pub change_id: Option<ChangeId>,96    pub bypass_bootstrap_lock: bool,97    pub ccache: Option<String>,98    /// Call Build::ninja() instead of this.99    pub ninja_in_file: bool,100    pub submodules: Option<bool>,101    pub compiler_docs: bool,102    pub library_docs_private_items: bool,103    pub docs_minification: bool,104    pub docs: bool,105    pub locked_deps: bool,106    pub vendor: bool,107    pub target_config: HashMap<TargetSelection, Target>,108    pub full_bootstrap: bool,109    pub bootstrap_cache_path: Option<PathBuf>,110    pub extended: bool,111    pub tools: Option<HashSet<String>>,112    /// Specify build configuration specific for some tool, such as enabled features, see [Tool].113    /// The key in the map is the name of the tool, and the value is tool-specific configuration.114    pub tool: HashMap<String, Tool>,115    pub sanitizers: bool,116    pub profiler: bool,117    pub omit_git_hash: bool,118    pub skip: Vec<PathBuf>,119    pub include_default_paths: bool,120    pub rustc_error_format: Option<String>,121    pub json_output: bool,122    pub compile_time_deps: bool,123    pub test_compare_mode: bool,124    pub color: Color,125    pub patch_binaries_for_nix: Option<bool>,126    pub stage0_metadata: build_helper::stage0_parser::Stage0,127    pub android_ndk: Option<PathBuf>,128    pub optimized_compiler_builtins: CompilerBuiltins,129    pub record_failed_tests_path: PathBuf,130131    pub stdout_is_tty: bool,132    pub stderr_is_tty: bool,133134    pub on_fail: Option<String>,135    pub explicit_stage_from_cli: bool,136    pub explicit_stage_from_config: bool,137    pub stage: u32,138    pub keep_stage: Vec<u32>,139    pub keep_stage_std: Vec<u32>,140    pub src: PathBuf,141    /// defaults to `bootstrap.toml`142    pub config: Option<PathBuf>,143    pub jobs: Option<u32>,144    pub cmd: Subcommand,145    pub quiet: bool,146    pub incremental: bool,147    pub dump_bootstrap_shims: bool,148    /// Arguments appearing after `--` to be forwarded to tools,149    /// e.g. `--fix-broken` or test arguments.150    pub free_args: Vec<String>,151152    /// `None` if we shouldn't download CI compiler artifacts, or the commit to download if we should.153    pub download_rustc_commit: Option<String>,154155    pub deny_warnings: bool,156    pub backtrace_on_ice: bool,157158    // llvm codegen options159    pub llvm_assertions: bool,160    pub llvm_tests: bool,161    pub llvm_enzyme: bool,162    pub llvm_offload: bool,163    pub llvm_plugins: bool,164    pub llvm_optimize: bool,165    pub llvm_thin_lto: bool,166    pub llvm_release_debuginfo: bool,167    pub llvm_static_stdcpp: bool,168    pub llvm_libzstd: bool,169    pub llvm_link_shared: Cell<Option<bool>>,170    pub llvm_clang_cl: Option<String>,171    pub llvm_targets: Option<String>,172    pub llvm_experimental_targets: Option<String>,173    pub llvm_link_jobs: Option<u32>,174    pub llvm_version_suffix: Option<String>,175    pub llvm_use_linker: Option<String>,176    pub llvm_clang_dir: Option<PathBuf>,177    pub llvm_allow_old_toolchain: bool,178    pub llvm_polly: bool,179    pub llvm_clang: bool,180    pub llvm_enable_warnings: bool,181    pub llvm_from_ci: bool,182    pub llvm_build_config: HashMap<String, String>,183184    pub bootstrap_override_lld: BootstrapOverrideLld,185    pub lld_enabled: bool,186    pub llvm_tools_enabled: bool,187    pub llvm_bitcode_linker_enabled: bool,188189    pub llvm_cflags: Option<String>,190    pub llvm_cxxflags: Option<String>,191    pub llvm_ldflags: Option<String>,192    pub llvm_use_libcxx: bool,193194    // gcc codegen options195    pub gcc_ci_mode: GccCiMode,196    pub libgccjit_libs_dir: Option<PathBuf>,197198    // rust codegen options199    pub rust_optimize: RustOptimize,200    pub rust_codegen_units: Option<u32>,201    pub rust_codegen_units_std: Option<u32>,202    pub rustc_debug_assertions: bool,203    pub std_debug_assertions: bool,204    pub tools_debug_assertions: bool,205206    pub rust_overflow_checks: bool,207    pub rust_overflow_checks_std: bool,208    pub rust_debug_logging: bool,209    pub rust_debuginfo_level_rustc: DebuginfoLevel,210    pub rust_debuginfo_level_std: DebuginfoLevel,211    pub rust_debuginfo_level_tools: DebuginfoLevel,212    pub rust_debuginfo_level_tests: DebuginfoLevel,213    pub rust_rpath: bool,214    pub rust_strip: bool,215    pub rust_frame_pointers: bool,216    pub rust_stack_protector: Option<String>,217    pub rustc_default_linker: Option<String>,218    pub rust_optimize_tests: bool,219    pub rust_dist_src: bool,220    pub rust_codegen_backends: Vec<CodegenBackendKind>,221    pub rust_verify_llvm_ir: bool,222    pub rust_thin_lto_import_instr_limit: Option<u32>,223    pub rust_randomize_layout: bool,224    pub rust_remap_debuginfo: bool,225    pub rust_new_symbol_mangling: Option<bool>,226    pub rust_annotate_moves_size_limit: Option<u64>,227    pub rust_profile_use: Option<String>,228    pub rust_profile_generate: Option<String>,229    pub rust_lto: RustcLto,230    pub rust_validate_mir_opts: Option<u32>,231    pub rust_std_features: BTreeSet<String>,232    pub rust_break_on_ice: bool,233    pub rust_parallel_frontend_threads: Option<u32>,234    pub rust_rustflags: Vec<String>,235236    pub llvm_profile_use: Option<String>,237    pub llvm_profile_generate: bool,238    pub llvm_libunwind_default: Option<LlvmLibunwind>,239    pub enable_bolt_settings: bool,240241    pub reproducible_artifacts: Vec<String>,242243    pub host_target: TargetSelection,244    pub hosts: Vec<TargetSelection>,245    pub targets: Vec<TargetSelection>,246    pub local_rebuild: bool,247    pub jemalloc: bool,248    pub control_flow_guard: bool,249    pub ehcont_guard: bool,250251    // dist misc252    pub dist_sign_folder: Option<PathBuf>,253    pub dist_upload_addr: Option<String>,254    pub dist_compression_formats: Option<Vec<String>>,255    pub dist_compression_profile: String,256    pub dist_include_mingw_linker: bool,257    pub dist_vendor: bool,258259    // libstd features260    pub backtrace: bool, // support for RUST_BACKTRACE261262    // misc263    pub low_priority: bool,264    pub channel: String,265    pub description: Option<String>,266    pub verbose_tests: bool,267    pub save_toolstates: Option<PathBuf>,268    pub print_step_timings: bool,269    pub print_step_rusage: bool,270271    // Fallback musl-root for all targets272    pub musl_root: Option<PathBuf>,273    pub prefix: Option<PathBuf>,274    pub sysconfdir: Option<PathBuf>,275    pub datadir: Option<PathBuf>,276    pub docdir: Option<PathBuf>,277    pub bindir: PathBuf,278    pub libdir: Option<PathBuf>,279    pub mandir: Option<PathBuf>,280    pub codegen_tests: bool,281    pub nodejs: Option<PathBuf>,282    pub yarn: Option<PathBuf>,283    pub gdb: Option<PathBuf>,284    pub lldb: Option<PathBuf>,285    pub python: Option<PathBuf>,286    pub windows_rc: Option<PathBuf>,287    pub reuse: Option<PathBuf>,288    pub cargo_native_static: bool,289    pub configure_args: Vec<String>,290    pub out: PathBuf,291    pub rust_info: channel::GitInfo,292293    pub cargo_info: channel::GitInfo,294    pub rust_analyzer_info: channel::GitInfo,295    pub clippy_info: channel::GitInfo,296    pub miri_info: channel::GitInfo,297    pub rustfmt_info: channel::GitInfo,298    pub enzyme_info: channel::GitInfo,299    pub in_tree_llvm_info: channel::GitInfo,300    pub in_tree_gcc_info: channel::GitInfo,301302    // These are either the stage0 downloaded binaries or the locally installed ones.303    pub initial_cargo: PathBuf,304    pub initial_rustc: PathBuf,305    pub initial_rustdoc: PathBuf,306    pub initial_cargo_clippy: Option<PathBuf>,307    pub initial_sysroot: PathBuf,308    pub initial_rustfmt: Option<PathBuf>,309310    /// The paths to work with. For example: with `./x check foo bar` we get311    /// `paths=["foo", "bar"]`.312    pub paths: Vec<PathBuf>,313314    /// Command for visual diff display, e.g. `diff-tool --color=always`.315    pub compiletest_diff_tool: Option<String>,316317    /// Whether to allow running both `compiletest` self-tests and `compiletest`-managed test suites318    /// against the stage 0 (rustc, std).319    ///320    /// This is only intended to be used when the stage 0 compiler is actually built from in-tree321    /// sources.322    pub compiletest_allow_stage0: bool,323324    /// Default value for `--extra-checks`325    pub tidy_extra_checks: Option<String>,326    pub ci_env: CiEnv,327328    /// Cache for determining path modifications329    pub path_modification_cache: Arc<Mutex<HashMap<Vec<&'static str>, PathFreshness>>>,330331    /// Skip checking the standard library if `rust.download-rustc` isn't available.332    /// This is mostly for RA as building the stage1 compiler to check the library tree333    /// on each code change might be too much for some computers.334    pub skip_std_check_if_no_download_rustc: bool,335336    pub exec_ctx: ExecutionContext,337}338339impl Config {340    pub fn set_dry_run(&mut self, dry_run: DryRun) {341        self.exec_ctx.set_dry_run(dry_run);342    }343344    pub fn get_dry_run(&self) -> &DryRun {345        self.exec_ctx.get_dry_run()346    }347348    #[cfg_attr(349        feature = "tracing",350        instrument(target = "CONFIG_HANDLING", level = "trace", name = "Config::parse", skip_all)351    )]352    pub fn parse(flags: Flags) -> Config {353        Self::parse_inner(flags, Self::get_toml)354    }355356    #[cfg_attr(357        feature = "tracing",358        instrument(359            target = "CONFIG_HANDLING",360            level = "trace",361            name = "Config::parse_inner",362            skip_all363        )364    )]365    pub(crate) fn parse_inner(366        flags: Flags,367        get_toml: impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>,368    ) -> Config {369        // Destructure flags to ensure that we use all its fields370        // The field variables are prefixed with `flags_` to avoid clashes371        // with values from TOML config files with same names.372        let Flags {373            cmd: flags_cmd,374            verbose: flags_verbose,375            quiet: flags_quiet,376            incremental: flags_incremental,377            config: flags_config,378            build_dir: flags_build_dir,379            build: flags_build,380            host: flags_host,381            target: flags_target,382            exclude: flags_exclude,383            skip: flags_skip,384            include_default_paths: flags_include_default_paths,385            rustc_error_format: flags_rustc_error_format,386            on_fail: flags_on_fail,387            dry_run: flags_dry_run,388            dump_bootstrap_shims: flags_dump_bootstrap_shims,389            stage: flags_stage,390            keep_stage: flags_keep_stage,391            keep_stage_std: flags_keep_stage_std,392            src: flags_src,393            jobs: flags_jobs,394            warnings: flags_warnings,395            json_output: flags_json_output,396            compile_time_deps: flags_compile_time_deps,397            color: flags_color,398            bypass_bootstrap_lock: flags_bypass_bootstrap_lock,399            rust_profile_generate: flags_rust_profile_generate,400            rust_profile_use: flags_rust_profile_use,401            llvm_profile_use: flags_llvm_profile_use,402            llvm_profile_generate: flags_llvm_profile_generate,403            enable_bolt_settings: flags_enable_bolt_settings,404            skip_stage0_validation: flags_skip_stage0_validation,405            reproducible_artifact: flags_reproducible_artifact,406            paths: flags_paths,407            set: flags_set,408            free_args: flags_free_args,409            ci: flags_ci,410            skip_std_check_if_no_download_rustc: flags_skip_std_check_if_no_download_rustc,411        } = flags;412413        #[cfg(feature = "tracing")]414        span!(415            target: "CONFIG_HANDLING",416            tracing::Level::TRACE,417            "collecting paths and path exclusions",418            "flags.paths" = ?flags_paths,419            "flags.skip" = ?flags_skip,420            "flags.exclude" = ?flags_exclude421        );422423        if flags_cmd.no_doc() {424            eprintln!(425                "WARN: `x.py test --no-doc` is renamed to `--all-targets`. `--no-doc` will be removed in the near future. Additionally `--tests` is added which only executes unit and integration tests."426            )427        }428429        // Set config values based on flags.430        let mut exec_ctx = ExecutionContext::new(flags_verbose, flags_cmd.fail_fast());431        exec_ctx.set_dry_run(if flags_dry_run { DryRun::UserSelected } else { DryRun::Disabled });432433        let default_src_dir = {434            let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));435            // Undo `src/bootstrap`436            manifest_dir.parent().unwrap().parent().unwrap().to_owned()437        };438        let src = if let Some(s) = compute_src_directory(flags_src, &exec_ctx) {439            s440        } else {441            default_src_dir.clone()442        };443444        #[cfg(test)]445        {446            if let Some(config_path) = flags_config.as_ref() {447                assert!(448                    !config_path.starts_with(&src),449                    "Path {config_path:?} should not be inside or equal to src dir {src:?}"450                );451            } else {452                panic!("During test the config should be explicitly added");453            }454        }455456        // Now load the TOML config, as soon as possible457        let (mut toml, toml_path) = load_toml_config(&src, flags_config, &get_toml);458459        postprocess_toml(&mut toml, &src, toml_path.clone(), &exec_ctx, &flags_set, &get_toml);460461        // Now override TOML values with flags, to make sure that we won't later override flags with462        // TOML values by accident instead, because flags have higher priority.463        let Build {464            description: build_description,465            build: build_build,466            host: build_host,467            target: build_target,468            build_dir: build_build_dir,469            cargo: mut build_cargo,470            rustc: mut build_rustc,471            rustdoc: build_rustdoc,472            rustfmt: build_rustfmt,473            cargo_clippy: build_cargo_clippy,474            docs: build_docs,475            compiler_docs: build_compiler_docs,476            library_docs_private_items: build_library_docs_private_items,477            docs_minification: build_docs_minification,478            submodules: build_submodules,479            gdb: build_gdb,480            lldb: build_lldb,481            nodejs: build_nodejs,482483            yarn: build_yarn,484            npm: build_npm,485            python: build_python,486            windows_rc: build_windows_rc,487            reuse: build_reuse,488            locked_deps: build_locked_deps,489            vendor: build_vendor,490            full_bootstrap: build_full_bootstrap,491            bootstrap_cache_path: build_bootstrap_cache_path,492            extended: build_extended,493            tools: build_tools,494            tool: build_tool,495            verbose: build_verbose,496            sanitizers: build_sanitizers,497            profiler: build_profiler,498            cargo_native_static: build_cargo_native_static,499            low_priority: build_low_priority,500            configure_args: build_configure_args,501            local_rebuild: build_local_rebuild,502            print_step_timings: build_print_step_timings,503            print_step_rusage: build_print_step_rusage,504            check_stage: build_check_stage,505            doc_stage: build_doc_stage,506            build_stage: build_build_stage,507            test_stage: build_test_stage,508            install_stage: build_install_stage,509            dist_stage: build_dist_stage,510            bench_stage: build_bench_stage,511            patch_binaries_for_nix: build_patch_binaries_for_nix,512            record_failed_tests_path: build_record_failed_tests_path,513            // This field is only used by bootstrap.py514            metrics: _,515            android_ndk: build_android_ndk,516            optimized_compiler_builtins: build_optimized_compiler_builtins,517            jobs: build_jobs,518            compiletest_diff_tool: build_compiletest_diff_tool,519            // No longer has any effect; kept (for now) to avoid breaking people's configs.520            compiletest_use_stage0_libtest: _,521            tidy_extra_checks: build_tidy_extra_checks,522            ccache: build_ccache,523            exclude: build_exclude,524            compiletest_allow_stage0: build_compiletest_allow_stage0,525        } = toml.build.unwrap_or_default();526527        let Install {528            prefix: install_prefix,529            sysconfdir: install_sysconfdir,530            docdir: install_docdir,531            bindir: install_bindir,532            libdir: install_libdir,533            mandir: install_mandir,534            datadir: install_datadir,535        } = toml.install.unwrap_or_default();536537        let Rust {538            optimize: rust_optimize,539            debug: rust_debug,540            codegen_units: rust_codegen_units,541            codegen_units_std: rust_codegen_units_std,542            rustc_debug_assertions: rust_rustc_debug_assertions,543            std_debug_assertions: rust_std_debug_assertions,544            tools_debug_assertions: rust_tools_debug_assertions,545            overflow_checks: rust_overflow_checks,546            overflow_checks_std: rust_overflow_checks_std,547            debug_logging: rust_debug_logging,548            debuginfo_level: rust_debuginfo_level,549            debuginfo_level_rustc: rust_debuginfo_level_rustc,550            debuginfo_level_std: rust_debuginfo_level_std,551            debuginfo_level_tools: rust_debuginfo_level_tools,552            debuginfo_level_tests: rust_debuginfo_level_tests,553            backtrace: rust_backtrace,554            incremental: rust_incremental,555            randomize_layout: rust_randomize_layout,556            default_linker: rust_default_linker,557            channel: rust_channel,558            musl_root: rust_musl_root,559            rpath: rust_rpath,560            verbose_tests: rust_verbose_tests,561            optimize_tests: rust_optimize_tests,562            codegen_tests: rust_codegen_tests,563            omit_git_hash: rust_omit_git_hash,564            dist_src: rust_dist_src,565            save_toolstates: rust_save_toolstates,566            codegen_backends: rust_codegen_backends,567            lld: rust_lld_enabled,568            llvm_tools: rust_llvm_tools,569            llvm_bitcode_linker: rust_llvm_bitcode_linker,570            deny_warnings: rust_deny_warnings,571            backtrace_on_ice: rust_backtrace_on_ice,572            verify_llvm_ir: rust_verify_llvm_ir,573            thin_lto_import_instr_limit: rust_thin_lto_import_instr_limit,574            parallel_frontend_threads: rust_parallel_frontend_threads,575            remap_debuginfo: rust_remap_debuginfo,576            jemalloc: rust_jemalloc,577            test_compare_mode: rust_test_compare_mode,578            llvm_libunwind: rust_llvm_libunwind,579            control_flow_guard: rust_control_flow_guard,580            ehcont_guard: rust_ehcont_guard,581            new_symbol_mangling: rust_new_symbol_mangling,582            annotate_moves_size_limit: rust_annotate_moves_size_limit,583            profile_generate: rust_profile_generate,584            profile_use: rust_profile_use,585            download_rustc: rust_download_rustc,586            lto: rust_lto,587            validate_mir_opts: rust_validate_mir_opts,588            frame_pointers: rust_frame_pointers,589            stack_protector: rust_stack_protector,590            strip: rust_strip,591            bootstrap_override_lld: rust_bootstrap_override_lld,592            bootstrap_override_lld_legacy: rust_bootstrap_override_lld_legacy,593            std_features: rust_std_features,594            break_on_ice: rust_break_on_ice,595            rustflags: rust_rustflags,596        } = toml.rust.unwrap_or_default();597598        let Llvm {599            optimize: llvm_optimize,600            thin_lto: llvm_thin_lto,601            release_debuginfo: llvm_release_debuginfo,602            assertions: llvm_assertions,603            tests: llvm_tests,604            enzyme: llvm_enzyme,605            plugins: llvm_plugin,606            static_libstdcpp: llvm_static_libstdcpp,607            libzstd: llvm_libzstd,608            ninja: llvm_ninja,609            targets: llvm_targets,610            experimental_targets: llvm_experimental_targets,611            link_jobs: llvm_link_jobs,612            link_shared: llvm_link_shared,613            version_suffix: llvm_version_suffix,614            clang_cl: llvm_clang_cl,615            cflags: llvm_cflags,616            cxxflags: llvm_cxxflags,617            ldflags: llvm_ldflags,618            use_libcxx: llvm_use_libcxx,619            use_linker: llvm_use_linker,620            allow_old_toolchain: llvm_allow_old_toolchain,621            offload: llvm_offload,622            offload_clang_dir: llvm_clang_dir,623            polly: llvm_polly,624            clang: llvm_clang,625            enable_warnings: llvm_enable_warnings,626            download_ci_llvm: llvm_download_ci_llvm,627            build_config: llvm_build_config,628        } = toml.llvm.unwrap_or_default();629630        let Dist {631            sign_folder: dist_sign_folder,632            upload_addr: dist_upload_addr,633            src_tarball: dist_src_tarball,634            compression_formats: dist_compression_formats,635            compression_profile: dist_compression_profile,636            include_mingw_linker: dist_include_mingw_linker,637            vendor: dist_vendor,638        } = toml.dist.unwrap_or_default();639640        let Gcc {641            download_ci_gcc: gcc_download_ci_gcc,642            libgccjit_libs_dir: gcc_libgccjit_libs_dir,643        } = toml.gcc.unwrap_or_default();644645        if rust_bootstrap_override_lld.is_some() && rust_bootstrap_override_lld_legacy.is_some() {646            panic!(647                "Cannot use both `rust.use-lld` and `rust.bootstrap-override-lld`. Please use only `rust.bootstrap-override-lld`"648            );649        }650651        let bootstrap_override_lld =652            rust_bootstrap_override_lld.or(rust_bootstrap_override_lld_legacy).unwrap_or_default();653654        if rust_optimize.as_ref().is_some_and(|v| matches!(v, RustOptimize::Bool(false))) {655            eprintln!(656                "WARNING: setting `optimize` to `false` is known to cause errors and \657                should be considered unsupported. Refer to `bootstrap.example.toml` \658                for more details."659            );660        }661662        // Prefer CLI verbosity flags if set (`flags_verbose` > 0), otherwise take the value from663        // TOML.664        exec_ctx.set_verbosity(cmp::max(build_verbose.unwrap_or_default() as u8, flags_verbose));665666        let stage0_metadata = build_helper::stage0_parser::parse_stage0_file();667        let path_modification_cache = Arc::new(Mutex::new(HashMap::new()));668669        let host_target = flags_build670            .or(build_build)671            .map(|build| TargetSelection::from_user(&build))672            .unwrap_or_else(get_host_target);673        let hosts = flags_host674            .map(|TargetSelectionList(hosts)| hosts)675            .or_else(|| {676                build_host.map(|h| h.iter().map(|t| TargetSelection::from_user(t)).collect())677            })678            .unwrap_or_else(|| vec![host_target]);679680        let llvm_assertions = llvm_assertions.unwrap_or(false);681        let mut target_config = HashMap::new();682        let mut channel = "dev".to_string();683684        let out = flags_build_dir.or_else(|| build_build_dir.map(PathBuf::from));685        let out = if cfg!(test) {686            out.expect("--build-dir has to be specified in tests")687        } else {688            out.unwrap_or_else(|| PathBuf::from("build"))689        };690691        // NOTE: Bootstrap spawns various commands with different working directories.692        // To avoid writing to random places on the file system, `config.out` needs to be an absolute path.693        let mut out = if !out.is_absolute() {694            // `canonicalize` requires the path to already exist. Use our vendored copy of `absolute` instead.695            absolute(&out).expect("can't make empty path absolute")696        } else {697            out698        };699700        let default_stage0_rustc_path = |dir: &Path| {701            dir.join(host_target).join("stage0").join("bin").join(exe("rustc", host_target))702        };703704        if cfg!(test) {705            // When configuring bootstrap for tests, make sure to set the rustc and Cargo to the706            // same ones used to call the tests (if custom ones are not defined in the toml). If we707            // don't do that, bootstrap will use its own detection logic to find a suitable rustc708            // and Cargo, which doesn't work when the caller is specìfying a custom local rustc or709            // Cargo in their bootstrap.toml.710            build_rustc = build_rustc.take().or(std::env::var_os("RUSTC").map(|p| p.into()));711            build_cargo = build_cargo.take().or(std::env::var_os("CARGO").map(|p| p.into()));712713            // If we are running only `cargo test` (and not `x test bootstrap`), which is useful714            // e.g. for debugging bootstrap itself, then we won't have RUSTC and CARGO set to the715            // proper paths.716            // We thus "guess" that the build directory is located at <src>/build, and try to load717            // rustc and cargo from there718            let is_test_outside_x = std::env::var("CARGO_TARGET_DIR").is_err();719            if is_test_outside_x && build_rustc.is_none() {720                let stage0_rustc = default_stage0_rustc_path(&default_src_dir.join("build"));721                assert!(722                    stage0_rustc.exists(),723                    "Trying to run cargo test without having a stage0 rustc available in {}",724                    stage0_rustc.display()725                );726                build_rustc = Some(stage0_rustc);727            }728        }729730        if !flags_skip_stage0_validation {731            if let Some(rustc) = &build_rustc {732                check_stage0_version(rustc, "rustc", &src, &exec_ctx);733            }734            if let Some(cargo) = &build_cargo {735                check_stage0_version(cargo, "cargo", &src, &exec_ctx);736            }737        }738739        if build_cargo_clippy.is_some() && build_rustc.is_none() {740            println!(741                "WARNING: Using `build.cargo-clippy` without `build.rustc` usually fails due to toolchain conflict."742            );743        }744745        let ci_env = match flags_ci {746            Some(true) => CiEnv::GitHubActions,747            Some(false) => CiEnv::None,748            None => CiEnv::current(),749        };750        let dwn_ctx = DownloadContext {751            path_modification_cache: path_modification_cache.clone(),752            src: &src,753            submodules: &build_submodules,754            host_target,755            patch_binaries_for_nix: build_patch_binaries_for_nix,756            exec_ctx: &exec_ctx,757            stage0_metadata: &stage0_metadata,758            llvm_assertions,759            bootstrap_cache_path: &build_bootstrap_cache_path,760            ci_env,761        };762763        let initial_rustc = build_rustc.unwrap_or_else(|| {764            download_beta_toolchain(&dwn_ctx, &out);765            default_stage0_rustc_path(&out)766        });767768        let initial_rustdoc = build_rustdoc769            .unwrap_or_else(|| initial_rustc.with_file_name(exe("rustdoc", host_target)));770771        let initial_sysroot = t!(PathBuf::from_str(772            command(&initial_rustc)773                .args(["--print", "sysroot"])774                .run_in_dry_run()775                .run_capture_stdout(&exec_ctx)776                .stdout()777                .trim()778        ));779780        let initial_cargo = build_cargo.unwrap_or_else(|| {781            download_beta_toolchain(&dwn_ctx, &out);782            initial_sysroot.join("bin").join(exe("cargo", host_target))783        });784785        // NOTE: it's important this comes *after* we set `initial_rustc` just above.786        if exec_ctx.dry_run() {787            out = out.join("tmp-dry-run");788            fs::create_dir_all(&out).expect("Failed to create dry-run directory");789        }790791        let file_content = t!(fs::read_to_string(src.join("src/ci/channel")));792        let ci_channel = file_content.trim_end();793794        let is_user_configured_rust_channel = match rust_channel {795            Some(channel_) if channel_ == "auto-detect" => {796                channel = ci_channel.into();797                true798            }799            Some(channel_) => {800                channel = channel_;801                true802            }803            None => false,804        };805806        let omit_git_hash = rust_omit_git_hash.unwrap_or(channel == "dev");807808        let rust_info = git_info(&exec_ctx, omit_git_hash, &src);809810        if !is_user_configured_rust_channel && rust_info.is_from_tarball() {811            channel = ci_channel.into();812        }813814        // FIXME(#133381): alt rustc builds currently do *not* have rustc debug assertions815        // enabled. We should not download a CI alt rustc if we need rustc to have debug816        // assertions (e.g. for crashes test suite). This can be changed once something like817        // [Enable debug assertions on alt818        // builds](https://github.com/rust-lang/rust/pull/131077) lands.819        //820        // Note that `rust.debug = true` currently implies `rust.debug-assertions = true`!821        //822        // This relies also on the fact that the global default for `download-rustc` will be823        // `false` if it's not explicitly set.824        let debug_assertions_requested = matches!(rust_rustc_debug_assertions, Some(true))825            || (matches!(rust_debug, Some(true))826                && !matches!(rust_rustc_debug_assertions, Some(false)));827828        if debug_assertions_requested829            && let Some(ref opt) = rust_download_rustc830            && opt.is_string_or_true()831        {832            eprintln!(833                "WARN: currently no CI rustc builds have rustc debug assertions \834                        enabled. Please either set `rust.debug-assertions` to `false` if you \835                        want to use download CI rustc or set `rust.download-rustc` to `false`."836            );837        }838839        let mut download_rustc_commit =840            download_ci_rustc_commit(&dwn_ctx, &rust_info, rust_download_rustc, llvm_assertions);841842        if debug_assertions_requested && download_rustc_commit.is_some() {843            eprintln!(844                "WARN: `rust.debug-assertions = true` will prevent downloading CI rustc as alt CI \845                rustc is not currently built with debug assertions."846            );847            // We need to put this later down_ci_rustc_commit.848            download_rustc_commit = None;849        }850851        // We need to override `rust.channel` if it's manually specified when using the CI rustc.852        // This is because if the compiler uses a different channel than the one specified in bootstrap.toml,853        // tests may fail due to using a different channel than the one used by the compiler during tests.854        if let Some(commit) = &download_rustc_commit855            && is_user_configured_rust_channel856        {857            println!(858                "WARNING: `rust.download-rustc` is enabled. The `rust.channel` option will be overridden by the CI rustc's channel."859            );860861            channel =862                read_file_by_commit(&dwn_ctx, &rust_info, Path::new("src/ci/channel"), commit)863                    .trim()864                    .to_owned();865        }866867        if build_npm.is_some() {868            println!(869                "WARNING: `build.npm` set in bootstrap.toml, this option no longer has any effect. . Use `build.yarn` instead to provide a path to a `yarn` binary."870            );871        }872873        let mut lld_enabled = rust_lld_enabled.unwrap_or(false);874875        // Linux targets for which the user explicitly overrode the used linker876        let mut targets_with_user_linker_override = HashSet::new();877878        if let Some(t) = toml.target {879            for (triple, cfg) in t {880                let TomlTarget {881                    cc: target_cc,882                    cxx: target_cxx,883                    ar: target_ar,884                    ranlib: target_ranlib,885                    default_linker: target_default_linker,886                    default_linker_linux_override: target_default_linker_linux_override,887                    linker: target_linker,888                    split_debuginfo: target_split_debuginfo,889                    llvm_config: target_llvm_config,890                    llvm_has_rust_patches: target_llvm_has_rust_patches,891                    llvm_filecheck: target_llvm_filecheck,892                    llvm_libunwind: target_llvm_libunwind,893                    sanitizers: target_sanitizers,894                    profiler: target_profiler,895                    rpath: target_rpath,896                    rustflags: target_rustflags,897                    crt_static: target_crt_static,898                    musl_root: target_musl_root,899                    musl_libdir: target_musl_libdir,900                    wasi_root: target_wasi_root,901                    qemu_rootfs: target_qemu_rootfs,902                    no_std: target_no_std,903                    codegen_backends: target_codegen_backends,904                    runner: target_runner,905                    optimized_compiler_builtins: target_optimized_compiler_builtins,906                    jemalloc: target_jemalloc,907                } = cfg;908909                let mut target = Target::from_triple(&triple);910911                if target_default_linker_linux_override.is_some() {912                    targets_with_user_linker_override.insert(triple.clone());913                }914915                let default_linker_linux_override = match target_default_linker_linux_override {916                    Some(DefaultLinuxLinkerOverride::SelfContainedLldCc) => {917                        if rust_default_linker.is_some() {918                            panic!(919                                "cannot set both `default-linker` and `default-linker-linux` for target `{triple}`"920                            );921                        }922                        if !triple.contains("linux-gnu") {923                            panic!(924                                "`default-linker-linux` can only be set for Linux GNU targets, not for `{triple}`"925                            );926                        }927                        if !lld_enabled {928                            panic!(929                                "Trying to override the default Linux linker for `{triple}` to be self-contained LLD, but LLD is not being built. Enable it with rust.lld = true."930                            );931                        }932                        DefaultLinuxLinkerOverride::SelfContainedLldCc933                    }934                    Some(DefaultLinuxLinkerOverride::Off) => DefaultLinuxLinkerOverride::Off,935                    None => DefaultLinuxLinkerOverride::default(),936                };937938                if let Some(ref s) = target_llvm_config {939                    if download_rustc_commit.is_some() && triple == *host_target.triple {940                        panic!(941                            "setting llvm_config for the host is incompatible with download-rustc"942                        );943                    }944                    target.llvm_config = Some(src.join(s));945                }946                if let Some(patches) = target_llvm_has_rust_patches {947                    assert!(948                        build_submodules == Some(false) || target_llvm_config.is_some(),949                        "use of `llvm-has-rust-patches` is restricted to cases where either submodules are disabled or llvm-config been provided"950                    );951                    target.llvm_has_rust_patches = Some(patches);952                }953                if let Some(ref s) = target_llvm_filecheck {954                    target.llvm_filecheck = Some(src.join(s));955                }956                target.llvm_libunwind = target_llvm_libunwind.as_ref().map(|v| {957                    v.parse().unwrap_or_else(|_| {958                        panic!("failed to parse target.{triple}.llvm-libunwind")959                    })960                });961                if let Some(s) = target_no_std {962                    target.no_std = s;963                }964                target.cc = target_cc.map(PathBuf::from);965                target.cxx = target_cxx.map(PathBuf::from);966                target.ar = target_ar.map(PathBuf::from);967                target.ranlib = target_ranlib.map(PathBuf::from);968                target.linker = target_linker.map(PathBuf::from);969                target.crt_static = target_crt_static;970                target.default_linker = target_default_linker;971                target.default_linker_linux_override = default_linker_linux_override;972                target.musl_root = target_musl_root.map(PathBuf::from);973                target.musl_libdir = target_musl_libdir.map(PathBuf::from);974                target.wasi_root = target_wasi_root.map(PathBuf::from);975                target.qemu_rootfs = target_qemu_rootfs.map(PathBuf::from);976                target.runner = target_runner;977                target.sanitizers = target_sanitizers;978                target.profiler = target_profiler;979                target.rpath = target_rpath;980                target.rustflags = target_rustflags.unwrap_or_default();981                target.optimized_compiler_builtins = target_optimized_compiler_builtins;982                target.jemalloc = target_jemalloc;983                if let Some(backends) = target_codegen_backends {984                    target.codegen_backends =985                        Some(parse_codegen_backends(backends, &format!("target.{triple}")))986                }987988                target.split_debuginfo = target_split_debuginfo.as_ref().map(|v| {989                    v.parse().unwrap_or_else(|_| {990                        panic!("invalid value for target.{triple}.split-debuginfo")991                    })992                });993994                target_config.insert(TargetSelection::from_user(&triple), target);995            }996        }997998        let llvm_from_ci = parse_download_ci_llvm(999            &dwn_ctx,1000            &rust_info,1001            &download_rustc_commit,1002            llvm_download_ci_llvm,1003            llvm_assertions,1004        );1005        let is_host_system_llvm =1006            is_system_llvm(&target_config, llvm_from_ci, host_target, host_target);10071008        if llvm_from_ci {1009            let warn = |option: &str| {1010                println!(1011                    "WARNING: `{option}` will only be used on `compiler/rustc_llvm` build, not for the LLVM build."1012                );1013                println!(1014                    "HELP: To use `{option}` for LLVM builds, set `download-ci-llvm` option to false."1015                );1016            };10171018            if llvm_static_libstdcpp.is_some() {1019                warn("static-libstdcpp");1020            }10211022            if llvm_link_shared.is_some() {1023                warn("link-shared");1024            }10251026            // FIXME(#129153): instead of all the ad-hoc `download-ci-llvm` checks that follow,1027            // use the `builder-config` present in tarballs since #128822 to compare the local1028            // config to the ones used to build the LLVM artifacts on CI, and only notify users1029            // if they've chosen a different value.10301031            if llvm_libzstd.is_some() {1032                println!(1033                    "WARNING: when using `download-ci-llvm`, the local `llvm.libzstd` option, \1034                    like almost all `llvm.*` options, will be ignored and set by the LLVM CI \1035                    artifacts builder config."1036                );1037                println!(1038                    "HELP: To use `llvm.libzstd` for LLVM/LLD builds, set `download-ci-llvm` option to false."1039                );1040            }1041        }10421043        if llvm_from_ci {1044            let triple = &host_target.triple;1045            let ci_llvm_bin = ci_llvm_root(&dwn_ctx, llvm_from_ci, &out).join("bin");1046            let build_target =1047                target_config.entry(host_target).or_insert_with(|| Target::from_triple(triple));1048            check_ci_llvm!(build_target.llvm_config);1049            check_ci_llvm!(build_target.llvm_filecheck);1050            build_target.llvm_config = Some(ci_llvm_bin.join(exe("llvm-config", host_target)));1051            build_target.llvm_filecheck = Some(ci_llvm_bin.join(exe("FileCheck", host_target)));1052        }10531054        for (target, linker_override) in default_linux_linker_overrides() {1055            // If the user overrode the default Linux linker, do not apply bootstrap defaults1056            if targets_with_user_linker_override.contains(&target) {1057                continue;1058            }10591060            // The rust.lld option is global, and not target specific, so if we enable it, it will1061            // be applied to all targets being built.1062            // So we only apply an override if we're building a compiler/host code for the given1063            // override target.1064            // Note: we could also make the LLD config per-target, but that would complicate things1065            if !hosts.contains(&TargetSelection::from_user(&target)) {1066                continue;1067            }10681069            let default_linux_linker_override = match linker_override {1070                DefaultLinuxLinkerOverride::Off => continue,1071                DefaultLinuxLinkerOverride::SelfContainedLldCc => {1072                    // If we automatically default to the self-contained LLD linker,1073                    // we also need to handle the rust.lld option.1074                    match rust_lld_enabled {1075                        // If LLD was not enabled explicitly, we enable it, unless LLVM config has1076                        // been set1077                        None if !is_host_system_llvm => {1078                            lld_enabled = true;1079                            Some(DefaultLinuxLinkerOverride::SelfContainedLldCc)1080                        }1081                        None => None,1082                        // If it was enabled already, we don't need to do anything1083                        Some(true) => Some(DefaultLinuxLinkerOverride::SelfContainedLldCc),1084                        // If it was explicitly disabled, we do not apply the1085                        // linker override1086                        Some(false) => None,1087                    }1088                }1089            };1090            if let Some(linker_override) = default_linux_linker_override {1091                target_config1092                    .entry(TargetSelection::from_user(&target))1093                    .or_default()1094                    .default_linker_linux_override = linker_override;1095            }1096        }10971098        let initial_rustfmt = build_rustfmt.or_else(|| maybe_download_rustfmt(&dwn_ctx, &out));10991100        if matches!(bootstrap_override_lld, BootstrapOverrideLld::SelfContained)1101            && !lld_enabled1102            && flags_stage.unwrap_or(0) > 01103        {1104            panic!(1105                "Trying to use self-contained lld as a linker, but LLD is not being added to the sysroot. Enable it with rust.lld = true."1106            );1107        }11081109        if lld_enabled && is_host_system_llvm {1110            panic!("Cannot enable LLD with `rust.lld = true` when using external llvm-config.");1111        }11121113        let download_rustc = download_rustc_commit.is_some();11141115        let stage = match flags_cmd {1116            Subcommand::Check { .. } => flags_stage.or(build_check_stage).unwrap_or(1),1117            Subcommand::Clippy { .. } | Subcommand::Fix => {1118                flags_stage.or(build_check_stage).unwrap_or(1)1119            }1120            // `download-rustc` only has a speed-up for stage2 builds. Default to stage2 unless explicitly overridden.1121            Subcommand::Doc { .. } => {1122                flags_stage.or(build_doc_stage).unwrap_or(if download_rustc { 2 } else { 1 })1123            }1124            Subcommand::Build { .. } => {1125                flags_stage.or(build_build_stage).unwrap_or(if download_rustc { 2 } else { 1 })1126            }1127            Subcommand::Test { .. } | Subcommand::Miri { .. } => {1128                flags_stage.or(build_test_stage).unwrap_or(if download_rustc { 2 } else { 1 })1129            }1130            Subcommand::Bench { .. } => flags_stage.or(build_bench_stage).unwrap_or(2),1131            Subcommand::Dist => flags_stage.or(build_dist_stage).unwrap_or(2),1132            Subcommand::Install => flags_stage.or(build_install_stage).unwrap_or(2),1133            Subcommand::Perf { .. } => flags_stage.unwrap_or(1),1134            // Most of the run commands execute bootstrap tools, which don't depend on the compiler.1135            // Other commands listed here should always use bootstrap tools.1136            Subcommand::Clean { .. }1137            | Subcommand::Run { .. }1138            | Subcommand::Setup { .. }1139            | Subcommand::Format { .. }1140            | Subcommand::Vendor { .. } => flags_stage.unwrap_or(0),1141        };11421143        let local_rebuild = build_local_rebuild.unwrap_or(false);11441145        let check_stage0 = |kind: &str| {1146            if local_rebuild {1147                eprintln!("WARNING: running {kind} in stage 0. This might not work as expected.");1148            } else {1149                eprintln!(1150                    "ERROR: cannot {kind} anything on stage 0. Use at least stage 1 or set build.local-rebuild=true and use a stage0 compiler built from in-tree sources."1151                );1152                exit!(1);1153            }1154        };11551156        // Now check that the selected stage makes sense, and if not, print an error and end1157        match (stage, &flags_cmd) {1158            (0, Subcommand::Build { .. }) => {1159                check_stage0("build");1160            }1161            (0, Subcommand::Check { .. }) => {1162                check_stage0("check");1163            }1164            (0, Subcommand::Doc { .. }) => {1165                check_stage0("doc");1166            }1167            (0, Subcommand::Clippy { .. }) => {1168                check_stage0("clippy");1169            }1170            (0, Subcommand::Dist) => {1171                check_stage0("dist");1172            }1173            (0, Subcommand::Install) => {1174                check_stage0("install");1175            }1176            (0, Subcommand::Test { .. }) if build_compiletest_allow_stage0 != Some(true) => {1177                eprintln!(1178                    "ERROR: cannot test anything on stage 0. Use at least stage 1. If you want to run compiletest with an external stage0 toolchain, enable `build.compiletest-allow-stage0`."1179                );1180                exit!(1);1181            }1182            _ => {}1183        }11841185        if flags_compile_time_deps && !matches!(flags_cmd, Subcommand::Check { .. }) {1186            eprintln!("ERROR: Can't use --compile-time-deps with any subcommand other than check.");1187            exit!(1);1188        }11891190        if matches!(flags_cmd, Subcommand::Fix) {1191            eprintln!(1192                "WARNING: `x fix` is provided on a best-effort basis and does not support all `cargo fix` options correctly."1193            );1194        }11951196        // CI should always run stage 2 builds, unless it specifically states otherwise1197        #[cfg(not(test))]1198        if flags_stage.is_none() && ci_env.is_running_in_ci() {1199            match flags_cmd {1200                Subcommand::Test { .. }1201                | Subcommand::Miri { .. }1202                | Subcommand::Doc { .. }1203                | Subcommand::Build { .. }1204                | Subcommand::Bench { .. }1205                | Subcommand::Dist1206                | Subcommand::Install => {1207                    assert_eq!(1208                        stage, 2,1209                        "x.py should be run with `--stage 2` on CI, but was run with `--stage {stage}`",1210                    );1211                }1212                Subcommand::Clean { .. }1213                | Subcommand::Check { .. }1214                | Subcommand::Clippy { .. }1215                | Subcommand::Fix1216                | Subcommand::Run { .. }1217                | Subcommand::Setup { .. }1218                | Subcommand::Format { .. }1219                | Subcommand::Vendor { .. }1220                | Subcommand::Perf { .. } => {}1221            }1222        }12231224        let with_defaults = |debuginfo_level_specific: Option<_>| {1225            debuginfo_level_specific.or(rust_debuginfo_level).unwrap_or(1226                if rust_debug == Some(true) {1227                    DebuginfoLevel::Limited1228                } else {1229                    DebuginfoLevel::None1230                },1231            )1232        };12331234        let ccache = match build_ccache {1235            Some(StringOrBool::String(s)) => Some(s),1236            Some(StringOrBool::Bool(true)) => Some("ccache".to_string()),1237            _ => None,1238        };12391240        let explicit_stage_from_config = build_test_stage.is_some()1241            || build_build_stage.is_some()1242            || build_doc_stage.is_some()1243            || build_dist_stage.is_some()1244            || build_install_stage.is_some()1245            || build_check_stage.is_some()1246            || build_bench_stage.is_some();12471248        let deny_warnings = match flags_warnings {1249            Warnings::Deny => true,1250            Warnings::Warn => false,1251            Warnings::Default => rust_deny_warnings.unwrap_or(true),1252        };12531254        let gcc_ci_mode = match gcc_download_ci_gcc {1255            Some(value) => match value {1256                true => GccCiMode::DownloadFromCi,1257                false => GccCiMode::BuildLocally,1258            },1259            None => GccCiMode::default(),1260        };12611262        let targets = flags_target1263            .map(|TargetSelectionList(targets)| targets)1264            .or_else(|| {1265                build_target.map(|t| t.iter().map(|t| TargetSelection::from_user(t)).collect())1266            })1267            .unwrap_or_else(|| hosts.clone());12681269        #[allow(clippy::map_identity)]1270        let skip = flags_skip1271            .into_iter()1272            .chain(flags_exclude)1273            .chain(build_exclude.unwrap_or_default())1274            .map(|p| {1275                // Never return top-level path here as it would break `--skip`1276                // logic on rustc's internal test framework which is utilized by compiletest.1277                #[cfg(windows)]1278                {1279                    PathBuf::from(p.to_string_lossy().replace('/', "\\"))1280                }1281                #[cfg(not(windows))]1282                {1283                    p1284                }1285            })1286            .collect();12871288        let cargo_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/cargo"));1289        let clippy_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/clippy"));1290        let in_tree_gcc_info = git_info(&exec_ctx, false, &src.join("src/gcc"));1291        let in_tree_llvm_info = git_info(&exec_ctx, false, &src.join("src/llvm-project"));1292        let enzyme_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/enzyme"));1293        let miri_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/miri"));1294        let rust_analyzer_info =1295            git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/rust-analyzer"));1296        let rustfmt_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/rustfmt"));12971298        let optimized_compiler_builtins =1299            build_optimized_compiler_builtins.unwrap_or(if channel == "dev" {1300                CompilerBuiltins::BuildRustOnly1301            } else {1302                CompilerBuiltins::BuildLLVMFuncs1303            });1304        let vendor = build_vendor.unwrap_or(1305            rust_info.is_from_tarball()1306                && src.join("vendor").exists()1307                && src.join(".cargo/config.toml").exists(),1308        );1309        let verbose_tests = rust_verbose_tests.unwrap_or(exec_ctx.is_verbose());13101311        let record_failed_tests_path =1312            out.join(build_record_failed_tests_path.unwrap_or_else(|| "failed-tests".to_string()));13131314        let paths = {1315            let mut paths = Vec::new();1316            if flags_cmd.rerun() {1317                paths = collect_previously_failed_tests(&record_failed_tests_path);1318            } else {1319                paths.extend(flags_paths);1320            }1321            paths1322        };13231324        Config {1325            // tidy-alphabetical-start1326            android_ndk: build_android_ndk,1327            backtrace: rust_backtrace.unwrap_or(true),1328            backtrace_on_ice: rust_backtrace_on_ice.unwrap_or(false),1329            bindir: install_bindir.map(PathBuf::from).unwrap_or("bin".into()),1330            bootstrap_cache_path: build_bootstrap_cache_path,1331            bootstrap_override_lld,1332            bypass_bootstrap_lock: flags_bypass_bootstrap_lock,1333            cargo_info,1334            cargo_native_static: build_cargo_native_static.unwrap_or(false),1335            ccache,1336            change_id: toml.change_id.inner,1337            channel,1338            ci_env,1339            clippy_info,1340            cmd: flags_cmd,1341            codegen_tests: rust_codegen_tests.unwrap_or(true),1342            color: flags_color,1343            compile_time_deps: flags_compile_time_deps,1344            compiler_docs: build_compiler_docs.unwrap_or(false),1345            compiletest_allow_stage0: build_compiletest_allow_stage0.unwrap_or(false),1346            compiletest_diff_tool: build_compiletest_diff_tool,1347            config: toml_path,1348            configure_args: build_configure_args.unwrap_or_default(),1349            control_flow_guard: rust_control_flow_guard.unwrap_or(false),1350            datadir: install_datadir.map(PathBuf::from),1351            deny_warnings,1352            description: build_description,1353            dist_compression_formats,1354            dist_compression_profile: dist_compression_profile.unwrap_or("fast".into()),1355            dist_include_mingw_linker: dist_include_mingw_linker.unwrap_or(true),1356            dist_sign_folder: dist_sign_folder.map(PathBuf::from),1357            dist_upload_addr,1358            dist_vendor: dist_vendor.unwrap_or_else(|| {1359                // If we're building from git or tarball sources, enable it by default.1360                rust_info.is_managed_git_subrepository() || rust_info.is_from_tarball()1361            }),1362            docdir: install_docdir.map(PathBuf::from),1363            docs: build_docs.unwrap_or(true),1364            docs_minification: build_docs_minification.unwrap_or(true),1365            download_rustc_commit,1366            dump_bootstrap_shims: flags_dump_bootstrap_shims,1367            ehcont_guard: rust_ehcont_guard.unwrap_or(false),1368            enable_bolt_settings: flags_enable_bolt_settings,1369            enzyme_info,1370            exec_ctx,1371            explicit_stage_from_cli: flags_stage.is_some(),1372            explicit_stage_from_config,1373            extended: build_extended.unwrap_or(false),1374            free_args: flags_free_args,1375            full_bootstrap: build_full_bootstrap.unwrap_or(false),1376            gcc_ci_mode,1377            gdb: build_gdb.map(PathBuf::from),1378            host_target,1379            hosts,1380            in_tree_gcc_info,1381            in_tree_llvm_info,1382            include_default_paths: flags_include_default_paths,1383            incremental: flags_incremental || rust_incremental == Some(true),1384            initial_cargo,1385            initial_cargo_clippy: build_cargo_clippy,1386            initial_rustc,1387            initial_rustdoc,1388            initial_rustfmt,1389            initial_sysroot,1390            jemalloc: rust_jemalloc.unwrap_or(false),1391            jobs: Some(threads_from_config(flags_jobs.or(build_jobs).unwrap_or(0))),1392            json_output: flags_json_output,1393            keep_stage: flags_keep_stage,1394            keep_stage_std: flags_keep_stage_std,1395            libdir: install_libdir.map(PathBuf::from),1396            libgccjit_libs_dir: gcc_libgccjit_libs_dir,1397            library_docs_private_items: build_library_docs_private_items.unwrap_or(false),1398            lld_enabled,1399            lldb: build_lldb.map(PathBuf::from),1400            llvm_allow_old_toolchain: llvm_allow_old_toolchain.unwrap_or(false),1401            llvm_assertions,1402            llvm_bitcode_linker_enabled: rust_llvm_bitcode_linker.unwrap_or(false),1403            llvm_build_config: llvm_build_config.clone().unwrap_or(Default::default()),1404            llvm_cflags,1405            llvm_clang: llvm_clang.unwrap_or(false),1406            llvm_clang_cl,1407            llvm_clang_dir: llvm_clang_dir.map(PathBuf::from),1408            llvm_cxxflags,1409            llvm_enable_warnings: llvm_enable_warnings.unwrap_or(false),1410            llvm_enzyme: llvm_enzyme.unwrap_or(false),1411            llvm_experimental_targets,1412            llvm_from_ci,1413            llvm_ldflags,1414            llvm_libunwind_default: rust_llvm_libunwind1415                .map(|v| v.parse().expect("failed to parse rust.llvm-libunwind")),1416            llvm_libzstd: llvm_libzstd.unwrap_or(false),1417            llvm_link_jobs,1418            // If we're building with ThinLTO on, by default we want to link1419            // to LLVM shared, to avoid re-doing ThinLTO (which happens in1420            // the link step) with each stage.1421            llvm_link_shared: Cell::new(1422                llvm_link_shared1423                    .or((!llvm_from_ci && llvm_thin_lto.unwrap_or(false)).then_some(true)),1424            ),1425            llvm_offload: llvm_offload.unwrap_or(false),1426            llvm_optimize: llvm_optimize.unwrap_or(true),1427            llvm_plugins: llvm_plugin.unwrap_or(false),1428            llvm_polly: llvm_polly.unwrap_or(false),1429            llvm_profile_generate: flags_llvm_profile_generate,1430            llvm_profile_use: flags_llvm_profile_use,1431            llvm_release_debuginfo: llvm_release_debuginfo.unwrap_or(false),1432            llvm_static_stdcpp: llvm_static_libstdcpp.unwrap_or(false),1433            llvm_targets,1434            llvm_tests: llvm_tests.unwrap_or(false),1435            llvm_thin_lto: llvm_thin_lto.unwrap_or(false),1436            llvm_tools_enabled: rust_llvm_tools.unwrap_or(true),1437            llvm_use_libcxx: llvm_use_libcxx.unwrap_or(false),1438            llvm_use_linker,1439            llvm_version_suffix,1440            local_rebuild,1441            locked_deps: build_locked_deps.unwrap_or(false),1442            low_priority: build_low_priority.unwrap_or(false),1443            mandir: install_mandir.map(PathBuf::from),1444            miri_info,1445            musl_root: rust_musl_root.map(PathBuf::from),1446            ninja_in_file: llvm_ninja.unwrap_or(true),1447            nodejs: build_nodejs.map(PathBuf::from),1448            omit_git_hash,1449            on_fail: flags_on_fail,1450            optimized_compiler_builtins,1451            out,1452            patch_binaries_for_nix: build_patch_binaries_for_nix,1453            path_modification_cache,1454            paths,1455            prefix: install_prefix.map(PathBuf::from),1456            print_step_rusage: build_print_step_rusage.unwrap_or(false),1457            print_step_timings: build_print_step_timings.unwrap_or(false),1458            profiler: build_profiler.unwrap_or(false),1459            python: build_python.map(PathBuf::from),1460            quiet: flags_quiet,1461            record_failed_tests_path,1462            reproducible_artifacts: flags_reproducible_artifact,1463            reuse: build_reuse.map(PathBuf::from),1464            rust_analyzer_info,1465            rust_annotate_moves_size_limit,1466            rust_break_on_ice: rust_break_on_ice.unwrap_or(true),1467            rust_codegen_backends: rust_codegen_backends1468                .map(|backends| parse_codegen_backends(backends, "rust"))1469                .unwrap_or(vec![CodegenBackendKind::Llvm]),1470            rust_codegen_units: rust_codegen_units.map(threads_from_config),1471            rust_codegen_units_std: rust_codegen_units_std.map(threads_from_config),1472            rust_debug_logging: rust_debug_logging1473                .or(rust_rustc_debug_assertions)1474                .unwrap_or(rust_debug == Some(true)),1475            rust_debuginfo_level_rustc: with_defaults(rust_debuginfo_level_rustc),1476            rust_debuginfo_level_std: with_defaults(rust_debuginfo_level_std),1477            rust_debuginfo_level_tests: rust_debuginfo_level_tests.unwrap_or(DebuginfoLevel::None),1478            rust_debuginfo_level_tools: with_defaults(rust_debuginfo_level_tools),1479            rust_dist_src: dist_src_tarball.unwrap_or_else(|| rust_dist_src.unwrap_or(true)),1480            rust_frame_pointers: rust_frame_pointers.unwrap_or(false),1481            rust_info,1482            rust_lto: rust_lto1483                .as_deref()1484                .map(|value| RustcLto::from_str(value).unwrap())1485                .unwrap_or_default(),1486            rust_new_symbol_mangling,1487            rust_optimize: rust_optimize.unwrap_or(RustOptimize::Bool(true)),1488            rust_optimize_tests: rust_optimize_tests.unwrap_or(true),1489            rust_overflow_checks: rust_overflow_checks.unwrap_or(rust_debug == Some(true)),1490            rust_overflow_checks_std: rust_overflow_checks_std1491                .or(rust_overflow_checks)1492                .unwrap_or(rust_debug == Some(true)),1493            rust_parallel_frontend_threads: rust_parallel_frontend_threads.map(threads_from_config),1494            rust_profile_generate: flags_rust_profile_generate.or(rust_profile_generate),1495            rust_profile_use: flags_rust_profile_use.or(rust_profile_use),1496            rust_randomize_layout: rust_randomize_layout.unwrap_or(false),1497            rust_remap_debuginfo: rust_remap_debuginfo.unwrap_or(false),1498            rust_rpath: rust_rpath.unwrap_or(true),1499            rust_rustflags: rust_rustflags.unwrap_or_default(),1500            rust_stack_protector,1501            rust_std_features: rust_std_features1502                .unwrap_or(BTreeSet::from([String::from("panic-unwind")])),1503            rust_strip: rust_strip.unwrap_or(false),1504            rust_thin_lto_import_instr_limit,1505            rust_validate_mir_opts,1506            rust_verify_llvm_ir: rust_verify_llvm_ir.unwrap_or(false),1507            rustc_debug_assertions: rust_rustc_debug_assertions.unwrap_or(rust_debug == Some(true)),1508            rustc_default_linker: rust_default_linker,1509            rustc_error_format: flags_rustc_error_format,1510            rustfmt_info,1511            sanitizers: build_sanitizers.unwrap_or(false),1512            save_toolstates: rust_save_toolstates.map(PathBuf::from),1513            skip,1514            skip_std_check_if_no_download_rustc: flags_skip_std_check_if_no_download_rustc,1515            src,1516            stage,1517            stage0_metadata,1518            std_debug_assertions: rust_std_debug_assertions1519                .or(rust_rustc_debug_assertions)1520                .unwrap_or(rust_debug == Some(true)),1521            stderr_is_tty: std::io::stderr().is_terminal(),1522            stdout_is_tty: std::io::stdout().is_terminal(),1523            submodules: build_submodules,1524            sysconfdir: install_sysconfdir.map(PathBuf::from),1525            target_config,1526            targets,1527            test_compare_mode: rust_test_compare_mode.unwrap_or(false),1528            tidy_extra_checks: build_tidy_extra_checks,1529            tool: build_tool.unwrap_or_default(),1530            tools: build_tools,1531            tools_debug_assertions: rust_tools_debug_assertions1532                .or(rust_rustc_debug_assertions)1533                .unwrap_or(rust_debug == Some(true)),1534            vendor,1535            verbose_tests,1536            windows_rc: build_windows_rc.map(PathBuf::from),1537            yarn: build_yarn.map(PathBuf::from),1538            // tidy-alphabetical-end1539        }1540    }15411542    pub fn dry_run(&self) -> bool {1543        self.exec_ctx.dry_run()1544    }15451546    pub fn is_running_on_ci(&self) -> bool {1547        self.ci_env.is_running_in_ci()1548    }15491550    pub fn is_explicit_stage(&self) -> bool {1551        self.explicit_stage_from_cli || self.explicit_stage_from_config1552    }15531554    pub(crate) fn test_args(&self) -> Vec<&str> {1555        let mut test_args = match self.cmd {1556            Subcommand::Test { ref test_args, .. }1557            | Subcommand::Bench { ref test_args, .. }1558            | Subcommand::Miri { ref test_args, .. } => {1559                test_args.iter().flat_map(|s| s.split_whitespace()).collect()1560            }1561            _ => vec![],1562        };1563        test_args.extend(self.free_args.iter().map(|s| s.as_str()));1564        test_args1565    }15661567    pub(crate) fn args(&self) -> Vec<&str> {1568        let mut args = match self.cmd {1569            Subcommand::Run { ref args, .. } => {1570                args.iter().flat_map(|s| s.split_whitespace()).collect()1571            }1572            _ => vec![],1573        };1574        args.extend(self.free_args.iter().map(|s| s.as_str()));1575        args1576    }15771578    /// Returns the content of the given file at a specific commit.1579    pub(crate) fn read_file_by_commit(&self, file: &Path, commit: &str) -> String {1580        let dwn_ctx = DownloadContext::from(self);1581        read_file_by_commit(dwn_ctx, &self.rust_info, file, commit)1582    }15831584    /// Bootstrap embeds a version number into the name of shared libraries it uploads in CI.1585    /// Return the version it would have used for the given commit.1586    pub(crate) fn artifact_version_part(&self, commit: &str) -> String {1587        let (channel, version) = if self.rust_info.is_managed_git_subrepository() {1588            let channel =1589                self.read_file_by_commit(Path::new("src/ci/channel"), commit).trim().to_owned();1590            let version =1591                self.read_file_by_commit(Path::new("src/version"), commit).trim().to_owned();1592            (channel, version)1593        } else {1594            let channel = fs::read_to_string(self.src.join("src/ci/channel"));1595            let version = fs::read_to_string(self.src.join("src/version"));1596            match (channel, version) {1597                (Ok(channel), Ok(version)) => {1598                    (channel.trim().to_owned(), version.trim().to_owned())1599                }1600                (channel, version) => {1601                    let src = self.src.display();1602                    eprintln!("ERROR: failed to determine artifact channel and/or version");1603                    eprintln!(1604                        "HELP: consider using a git checkout or ensure these files are readable"1605                    );1606                    if let Err(channel) = channel {1607                        eprintln!("reading {src}/src/ci/channel failed: {channel:?}");1608                    }1609                    if let Err(version) = version {1610                        eprintln!("reading {src}/src/version failed: {version:?}");1611                    }1612                    panic!();1613                }1614            }1615        };16161617        match channel.as_str() {1618            "stable" => version,1619            "beta" => channel,1620            "nightly" => channel,1621            other => unreachable!("{:?} is not recognized as a valid channel", other),1622        }1623    }16241625    /// Try to find the relative path of `bindir`, otherwise return it in full.1626    pub fn bindir_relative(&self) -> &Path {1627        let bindir = &self.bindir;1628        if bindir.is_absolute() {1629            // Try to make it relative to the prefix.1630            if let Some(prefix) = &self.prefix1631                && let Ok(stripped) = bindir.strip_prefix(prefix)1632            {1633                return stripped;1634            }1635        }1636        bindir1637    }16381639    /// Try to find the relative path of `libdir`.1640    pub fn libdir_relative(&self) -> Option<&Path> {1641        let libdir = self.libdir.as_ref()?;1642        if libdir.is_relative() {1643            Some(libdir)1644        } else {1645            // Try to make it relative to the prefix.1646            libdir.strip_prefix(self.prefix.as_ref()?).ok()1647        }1648    }16491650    /// The absolute path to the downloaded LLVM artifacts.1651    pub(crate) fn ci_llvm_root(&self) -> PathBuf {1652        let dwn_ctx = DownloadContext::from(self);1653        ci_llvm_root(dwn_ctx, self.llvm_from_ci, &self.out)1654    }16551656    /// Directory where the extracted `rustc-dev` component is stored.1657    pub(crate) fn ci_rustc_dir(&self) -> PathBuf {1658        assert!(self.download_rustc());1659        self.out.join(self.host_target).join("ci-rustc")1660    }16611662    /// Determine whether llvm should be linked dynamically.1663    ///1664    /// If `false`, llvm should be linked statically.1665    /// This is computed on demand since LLVM might have to first be downloaded from CI.1666    pub(crate) fn llvm_link_shared(&self) -> bool {1667        let mut opt = self.llvm_link_shared.get();1668        if opt.is_none() && self.dry_run() {1669            // just assume static for now - dynamic linking isn't supported on all platforms1670            return false;1671        }16721673        let llvm_link_shared = *opt.get_or_insert_with(|| {1674            if self.llvm_from_ci {1675                self.maybe_download_ci_llvm();1676                let ci_llvm = self.ci_llvm_root();1677                let link_type = t!(1678                    std::fs::read_to_string(ci_llvm.join("link-type.txt")),1679                    format!("CI llvm missing: {}", ci_llvm.display())1680                );1681                link_type == "dynamic"1682            } else {1683                // unclear how thought-through this default is, but it maintains compatibility with1684                // previous behavior1685                false1686            }1687        });1688        self.llvm_link_shared.set(opt);1689        llvm_link_shared1690    }16911692    /// Return whether we will use a downloaded, pre-compiled version of rustc, or just build from source.1693    pub(crate) fn download_rustc(&self) -> bool {1694        self.download_rustc_commit().is_some()1695    }16961697    pub(crate) fn download_rustc_commit(&self) -> Option<&str> {1698        static DOWNLOAD_RUSTC: OnceLock<Option<String>> = OnceLock::new();1699        if self.dry_run() && DOWNLOAD_RUSTC.get().is_none() {1700            // avoid trying to actually download the commit1701            return self.download_rustc_commit.as_deref();1702        }17031704        DOWNLOAD_RUSTC1705            .get_or_init(|| match &self.download_rustc_commit {1706                None => None,1707                Some(commit) => {1708                    self.download_ci_rustc(commit);17091710                    // CI-rustc can't be used without CI-LLVM. If `self.llvm_from_ci` is false, it means the "if-unchanged"1711                    // logic has detected some changes in the LLVM submodule (download-ci-llvm=false can't happen here as1712                    // we don't allow it while parsing the configuration).1713                    if !self.llvm_from_ci {1714                        // This happens when LLVM submodule is updated in CI, we should disable ci-rustc without an error1715                        // to not break CI. For non-CI environments, we should return an error.1716                        if self.is_running_on_ci() {1717                            println!("WARNING: LLVM submodule has changes, `download-rustc` will be disabled.");1718                            return None;1719                        } else {1720                            panic!("ERROR: LLVM submodule has changes, `download-rustc` can't be used.");1721                        }1722                    }17231724                    if let Some(config_path) = &self.config {1725                        let ci_config_toml = match self.get_builder_toml("ci-rustc") {1726                            Ok(ci_config_toml) => ci_config_toml,1727                            Err(e) if e.to_string().contains("unknown field") => {1728                                println!("WARNING: CI rustc has some fields that are no longer supported in bootstrap; download-rustc will be disabled.");1729                                println!("HELP: Consider rebasing to a newer commit if available.");1730                                return None;1731                            }1732                            Err(e) => {1733                                eprintln!("ERROR: Failed to parse CI rustc bootstrap.toml: {e}");1734                                exit!(2);1735                            }1736                        };17371738                        let current_config_toml = Self::get_toml(config_path).unwrap();17391740                        // Check the config compatibility1741                        // FIXME: this doesn't cover `--set` flags yet.1742                        let res = check_incompatible_options_for_ci_rustc(1743                            self.host_target,1744                            current_config_toml,1745                            ci_config_toml,1746                        );17471748                        // Primarily used by CI runners to avoid handling download-rustc incompatible1749                        // options one by one on shell scripts.1750                        let disable_ci_rustc_if_incompatible = env::var_os("DISABLE_CI_RUSTC_IF_INCOMPATIBLE")1751                            .is_some_and(|s| s == "1" || s == "true");17521753                        if disable_ci_rustc_if_incompatible && res.is_err() {1754                            println!("WARNING: download-rustc is disabled with `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` env.");1755                            return None;1756                        }17571758                        res.unwrap();1759                    }17601761                    Some(commit.clone())1762                }1763            })1764            .as_deref()1765    }17661767    /// Runs a function if verbosity is greater than 01768    pub fn do_if_verbose(&self, f: impl Fn()) {1769        self.exec_ctx.do_if_verbose(f);1770    }17711772    pub fn any_sanitizers_to_build(&self) -> bool {1773        self.target_config1774            .iter()1775            .any(|(ts, t)| !ts.is_msvc() && t.sanitizers.unwrap_or(self.sanitizers))1776    }17771778    pub fn any_profiler_enabled(&self) -> bool {1779        self.target_config.values().any(|t| matches!(&t.profiler, Some(p) if p.is_string_or_true()))1780            || self.profiler1781    }17821783    /// Returns whether or not submodules should be managed by bootstrap.1784    pub fn submodules(&self) -> bool {1785        // If not specified in config, the default is to only manage1786        // submodules if we're currently inside a git repository.1787        self.submodules.unwrap_or(self.rust_info.is_managed_git_subrepository())1788    }17891790    pub fn git_config(&self) -> GitConfig<'_> {1791        GitConfig {1792            nightly_branch: &self.stage0_metadata.config.nightly_branch,1793            git_merge_commit_email: &self.stage0_metadata.config.git_merge_commit_email,1794        }1795    }17961797    /// Given a path to the directory of a submodule, update it.1798    ///1799    /// `relative_path` should be relative to the root of the git repository, not an absolute path.1800    ///1801    /// This *does not* update the submodule if `bootstrap.toml` explicitly says1802    /// not to, or if we're not in a git repository (like a plain source1803    /// tarball). Typically [`crate::Build::require_submodule`] should be1804    /// used instead to provide a nice error to the user if the submodule is1805    /// missing.1806    #[cfg_attr(1807        feature = "tracing",1808        instrument(1809            level = "trace",1810            name = "Config::update_submodule",1811            skip_all,1812            fields(relative_path = ?relative_path),1813        ),1814    )]1815    pub(crate) fn update_submodule(&self, relative_path: &str) {1816        let dwn_ctx = DownloadContext::from(self);1817        update_submodule(dwn_ctx, &self.rust_info, relative_path);1818    }18191820    /// Returns true if any of the `paths` have been modified locally.1821    pub fn has_changes_from_upstream(&self, paths: &[&'static str]) -> bool {1822        let dwn_ctx = DownloadContext::from(self);1823        has_changes_from_upstream(dwn_ctx, paths)1824    }18251826    /// Checks whether any of the given paths have been modified w.r.t. upstream.1827    pub fn check_path_modifications(&self, paths: &[&'static str]) -> PathFreshness {1828        // Checking path modifications through git can be relatively expensive (>100ms).1829        // We do not assume that the sources would change during bootstrap's execution,1830        // so we can cache the results here.1831        // Note that we do not use a static variable for the cache, because it would cause problems1832        // in tests that create separate `Config` instances.1833        self.path_modification_cache1834            .lock()1835            .unwrap()1836            .entry(paths.to_vec())1837            .or_insert_with(|| {1838                check_path_modifications(&self.src, &self.git_config(), paths, self.ci_env).unwrap()1839            })1840            .clone()1841    }18421843    pub fn sanitizers_enabled(&self, target: TargetSelection) -> bool {1844        self.target_config.get(&target).and_then(|t| t.sanitizers).unwrap_or(self.sanitizers)1845    }18461847    pub fn needs_sanitizer_runtime_built(&self, target: TargetSelection) -> bool {1848        // MSVC uses the Microsoft-provided sanitizer runtime, but all other runtimes we build.1849        !target.is_msvc() && self.sanitizers_enabled(target)1850    }18511852    pub fn profiler_path(&self, target: TargetSelection) -> Option<&str> {1853        match self.target_config.get(&target)?.profiler.as_ref()? {1854            StringOrBool::String(s) => Some(s),1855            StringOrBool::Bool(_) => None,1856        }1857    }18581859    pub fn profiler_enabled(&self, target: TargetSelection) -> bool {1860        self.target_config1861            .get(&target)1862            .and_then(|t| t.profiler.as_ref())1863            .map(StringOrBool::is_string_or_true)1864            .unwrap_or(self.profiler)1865    }18661867    /// Returns codegen backends that should be:1868    /// - Built and added to the sysroot when we build the compiler.1869    /// - Distributed when `x dist` is executed (if the codegen backend has a dist step).1870    pub fn enabled_codegen_backends(&self, target: TargetSelection) -> &[CodegenBackendKind] {1871        self.target_config1872            .get(&target)1873            .and_then(|cfg| cfg.codegen_backends.as_deref())1874            .unwrap_or(&self.rust_codegen_backends)1875    }18761877    /// Returns the codegen backend that should be configured as the *default* codegen backend1878    /// for a rustc compiled by bootstrap.1879    pub fn default_codegen_backend(&self, target: TargetSelection) -> &CodegenBackendKind {1880        // We're guaranteed to have always at least one codegen backend listed.1881        self.enabled_codegen_backends(target).first().unwrap()1882    }18831884    pub fn jemalloc(&self, target: TargetSelection) -> bool {1885        self.target_config.get(&target).and_then(|cfg| cfg.jemalloc).unwrap_or(self.jemalloc)1886    }18871888    pub fn rpath_enabled(&self, target: TargetSelection) -> bool {1889        self.target_config.get(&target).and_then(|t| t.rpath).unwrap_or(self.rust_rpath)1890    }18911892    pub fn optimized_compiler_builtins(&self, target: TargetSelection) -> &CompilerBuiltins {1893        self.target_config1894            .get(&target)1895            .and_then(|t| t.optimized_compiler_builtins.as_ref())1896            .unwrap_or(&self.optimized_compiler_builtins)1897    }18981899    pub fn llvm_enabled(&self, target: TargetSelection) -> bool {1900        self.enabled_codegen_backends(target).contains(&CodegenBackendKind::Llvm)1901    }19021903    pub fn llvm_libunwind(&self, target: TargetSelection) -> LlvmLibunwind {1904        self.target_config1905            .get(&target)1906            .and_then(|t| t.llvm_libunwind)1907            .or(self.llvm_libunwind_default)1908            .unwrap_or(1909                if target.contains("fuchsia")1910                    || (target.contains("hexagon") && !target.contains("qurt"))1911                {1912                    // Fuchsia and Hexagon Linux use in-tree llvm-libunwind.1913                    // Hexagon QuRT uses libc_eh from the Hexagon SDK instead.1914                    LlvmLibunwind::InTree1915                } else {1916                    LlvmLibunwind::No1917                },1918            )1919    }19201921    pub fn split_debuginfo(&self, target: TargetSelection) -> SplitDebuginfo {1922        self.target_config1923            .get(&target)1924            .and_then(|t| t.split_debuginfo)1925            .unwrap_or_else(|| SplitDebuginfo::default_for_platform(target))1926    }19271928    /// Checks if the given target is the same as the host target.1929    pub fn is_host_target(&self, target: TargetSelection) -> bool {1930        self.host_target == target1931    }19321933    /// Returns `true` if this is an external version of LLVM not managed by bootstrap.1934    /// In particular, we expect llvm sources to be available when this is false.1935    ///1936    /// NOTE: this is not the same as `!is_rust_llvm` when `llvm_has_patches` is set.1937    pub fn is_system_llvm(&self, target: TargetSelection) -> bool {1938        is_system_llvm(&self.target_config, self.llvm_from_ci, self.host_target, target)1939    }19401941    /// Returns `true` if this is our custom, patched, version of LLVM.1942    ///1943    /// This does not necessarily imply that we're managing the `llvm-project` submodule.1944    pub fn is_rust_llvm(&self, target: TargetSelection) -> bool {1945        match self.target_config.get(&target) {1946            // We're using a user-controlled version of LLVM. The user has explicitly told us whether the version has our patches.1947            // (They might be wrong, but that's not a supported use-case.)1948            // 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`.1949            Some(Target { llvm_has_rust_patches: Some(patched), .. }) => *patched,1950            // The user hasn't promised the patches match.1951            // This only has our patches if it's downloaded from CI or built from source.1952            _ => !self.is_system_llvm(target),1953        }1954    }19551956    pub fn exec_ctx(&self) -> &ExecutionContext {1957        &self.exec_ctx1958    }19591960    pub fn git_info(&self, omit_git_hash: bool, dir: &Path) -> GitInfo {1961        GitInfo::new(omit_git_hash, dir, self)1962    }1963}19641965impl AsRef<ExecutionContext> for Config {1966    fn as_ref(&self) -> &ExecutionContext {1967        &self.exec_ctx1968    }1969}19701971fn compute_src_directory(src_dir: Option<PathBuf>, exec_ctx: &ExecutionContext) -> Option<PathBuf> {1972    if let Some(src) = src_dir {1973        return Some(src);1974    } else {1975        // Infer the source directory. This is non-trivial because we want to support a downloaded bootstrap binary,1976        // running on a completely different machine from where it was compiled.1977        let mut cmd = helpers::git(None);1978        // NOTE: we cannot support running from outside the repository because the only other path we have available1979        // is set at compile time, which can be wrong if bootstrap was downloaded rather than compiled locally.1980        // We still support running outside the repository if we find we aren't in a git directory.19811982        // NOTE: We get a relative path from git to work around an issue on MSYS/mingw. If we used an absolute path,1983        // and end up using MSYS's git rather than git-for-windows, we would get a unix-y MSYS path. But as bootstrap1984        // has already been (kinda-cross-)compiled to Windows land, we require a normal Windows path.1985        cmd.arg("rev-parse").arg("--show-cdup");1986        // Discard stderr because we expect this to fail when building from a tarball.1987        let output = cmd.allow_failure().run_capture_stdout(exec_ctx);1988        if output.is_success() {1989            let git_root_relative = output.stdout();1990            // We need to canonicalize this path to make sure it uses backslashes instead of forward slashes,1991            // and to resolve any relative components.1992            let git_root = env::current_dir()1993                .unwrap()1994                .join(PathBuf::from(git_root_relative.trim()))1995                .canonicalize()1996                .unwrap();1997            let s = git_root.to_str().unwrap();19981999            // Bootstrap is quite bad at handling /? in front of paths2000            let git_root = match s.strip_prefix("\\\\?\\") {

Findings

✓ No findings reported for this file.

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.