diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2025-06-20 18:52:13 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2025-06-24 16:00:04 +0300 |
| commit | 0be37cab97cf330d0470cf6e7db383d944a90975 (patch) | |
| tree | 12f53b8cd0369d4612795ebc81d76eabdf934e7a /compiler/rustc_session/src | |
| parent | e4b9d0141fdd210fcceebd2b67f7be113401c461 (diff) | |
| download | rust-0be37cab97cf330d0470cf6e7db383d944a90975.tar.gz rust-0be37cab97cf330d0470cf6e7db383d944a90975.zip | |
rustc_session: Add a structure for keeping both explicit and default sysroots
Also avoid creating and cloning sysroot unnecessarily.
Diffstat (limited to 'compiler/rustc_session/src')
| -rw-r--r-- | compiler/rustc_session/src/config.rs | 31 | ||||
| -rw-r--r-- | compiler/rustc_session/src/filesearch.rs | 18 | ||||
| -rw-r--r-- | compiler/rustc_session/src/options.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_session/src/session.rs | 16 |
4 files changed, 36 insertions, 31 deletions
diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 87e4b0a17aa..c62e4ac30ea 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -1296,6 +1296,28 @@ bitflags::bitflags! { } } +#[derive(Clone, Debug)] +pub struct Sysroot { + pub explicit: Option<PathBuf>, + pub default: PathBuf, +} + +impl Sysroot { + pub fn new(explicit: Option<PathBuf>) -> Sysroot { + Sysroot { explicit, default: filesearch::default_sysroot() } + } + + /// Return explicit sysroot if it was passed with `--sysroot`, or default sysroot otherwise. + pub fn path(&self) -> &Path { + self.explicit.as_deref().unwrap_or(&self.default) + } + + /// Returns both explicit sysroot if it was passed with `--sysroot` and the default sysroot. + pub fn all_paths(&self) -> impl Iterator<Item = &Path> { + self.explicit.as_deref().into_iter().chain(iter::once(&*self.default)) + } +} + pub fn host_tuple() -> &'static str { // Get the host triple out of the build environment. This ensures that our // idea of the host triple is the same as for the set of libraries we've @@ -1342,7 +1364,7 @@ impl Default for Options { describe_lints: false, output_types: OutputTypes(BTreeMap::new()), search_paths: vec![], - sysroot: filesearch::materialize_sysroot(None), + sysroot: Sysroot::new(None), target_triple: TargetTuple::from_tuple(host_tuple()), test: false, incremental: None, @@ -2673,7 +2695,6 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M let cg = cg; - let sysroot_opt = matches.opt_str("sysroot").map(|m| PathBuf::from(&m)); let target_triple = parse_target_triple(early_dcx, matches); let opt_level = parse_opt_level(early_dcx, matches, &cg); // The `-g` and `-C debuginfo` flags specify the same setting, so we want to be able @@ -2712,10 +2733,10 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M let logical_env = parse_logical_env(early_dcx, matches); - let sysroot = filesearch::materialize_sysroot(sysroot_opt); + let sysroot = Sysroot::new(matches.opt_str("sysroot").map(PathBuf::from)); let real_source_base_dir = |suffix: &str, confirm: &str| { - let mut candidate = sysroot.join(suffix); + let mut candidate = sysroot.path().join(suffix); if let Ok(metadata) = candidate.symlink_metadata() { // Replace the symlink bootstrap creates, with its destination. // We could try to use `fs::canonicalize` instead, but that might @@ -2742,7 +2763,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M let mut search_paths = vec![]; for s in &matches.opt_strs("L") { search_paths.push(SearchPath::from_cli_opt( - &sysroot, + sysroot.path(), &target_triple, early_dcx, s, diff --git a/compiler/rustc_session/src/filesearch.rs b/compiler/rustc_session/src/filesearch.rs index 4f8c3926207..f64fa86948c 100644 --- a/compiler/rustc_session/src/filesearch.rs +++ b/compiler/rustc_session/src/filesearch.rs @@ -5,7 +5,6 @@ use std::{env, fs}; use rustc_fs_util::try_canonicalize; use rustc_target::spec::Target; -use smallvec::{SmallVec, smallvec}; use crate::search_paths::{PathKind, SearchPath}; @@ -182,24 +181,9 @@ fn current_dll_path() -> Result<PathBuf, String> { Err("current_dll_path is not supported on WASI".to_string()) } -pub fn sysroot_with_fallback(sysroot: &Path) -> SmallVec<[PathBuf; 2]> { - let mut candidates = smallvec![sysroot.to_owned()]; - let default_sysroot = get_or_default_sysroot(); - if default_sysroot != sysroot { - candidates.push(default_sysroot); - } - candidates -} - -/// Returns the provided sysroot or calls [`get_or_default_sysroot`] if it's none. -/// Panics if [`get_or_default_sysroot`] returns an error. -pub fn materialize_sysroot(maybe_sysroot: Option<PathBuf>) -> PathBuf { - maybe_sysroot.unwrap_or_else(|| get_or_default_sysroot()) -} - /// This function checks if sysroot is found using env::args().next(), and if it /// is not found, finds sysroot from current rustc_driver dll. -pub fn get_or_default_sysroot() -> PathBuf { +pub(crate) fn default_sysroot() -> PathBuf { fn default_from_rustc_driver_dll() -> Result<PathBuf, String> { let dll = current_dll_path()?; diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 7fef942525b..232531dc673 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -337,7 +337,7 @@ top_level_options!( output_types: OutputTypes [TRACKED], search_paths: Vec<SearchPath> [UNTRACKED], libs: Vec<NativeLib> [TRACKED], - sysroot: PathBuf [UNTRACKED], + sysroot: Sysroot [UNTRACKED], target_triple: TargetTuple [TRACKED], diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index ad58c3c8f7d..bad2581ae31 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -149,7 +149,6 @@ pub struct Session { pub opts: config::Options, pub target_tlib_path: Arc<SearchPath>, pub psess: ParseSess, - pub sysroot: PathBuf, /// Input, input file path and output file path to this compilation process. pub io: CompilerIO, @@ -456,8 +455,10 @@ impl Session { /// directories are also returned, for example if `--sysroot` is used but tools are missing /// (#125246): we also add the bin directories to the sysroot where rustc is located. pub fn get_tools_search_paths(&self, self_contained: bool) -> Vec<PathBuf> { - let search_paths = filesearch::sysroot_with_fallback(&self.sysroot) - .into_iter() + let search_paths = self + .opts + .sysroot + .all_paths() .map(|sysroot| filesearch::make_target_bin_path(&sysroot, config::host_tuple())); if self_contained { @@ -1028,7 +1029,6 @@ pub fn build_session( fluent_resources: Vec<&'static str>, driver_lint_caps: FxHashMap<lint::LintId, lint::Level>, target: Target, - sysroot: PathBuf, cfg_version: &'static str, ice_file: Option<PathBuf>, using_internal_features: &'static AtomicBool, @@ -1063,7 +1063,7 @@ pub fn build_session( } let host_triple = TargetTuple::from_tuple(config::host_tuple()); - let (host, target_warnings) = Target::search(&host_triple, &sysroot) + let (host, target_warnings) = Target::search(&host_triple, sopts.sysroot.path()) .unwrap_or_else(|e| dcx.handle().fatal(format!("Error loading host specification: {e}"))); for warning in target_warnings.warning_messages() { dcx.handle().warn(warning) @@ -1096,13 +1096,14 @@ pub fn build_session( let host_triple = config::host_tuple(); let target_triple = sopts.target_triple.tuple(); // FIXME use host sysroot? - let host_tlib_path = Arc::new(SearchPath::from_sysroot_and_triple(&sysroot, host_triple)); + let host_tlib_path = + Arc::new(SearchPath::from_sysroot_and_triple(sopts.sysroot.path(), host_triple)); let target_tlib_path = if host_triple == target_triple { // Use the same `SearchPath` if host and target triple are identical to avoid unnecessary // rescanning of the target lib path and an unnecessary allocation. Arc::clone(&host_tlib_path) } else { - Arc::new(SearchPath::from_sysroot_and_triple(&sysroot, target_triple)) + Arc::new(SearchPath::from_sysroot_and_triple(sopts.sysroot.path(), target_triple)) }; let prof = SelfProfilerRef::new( @@ -1134,7 +1135,6 @@ pub fn build_session( opts: sopts, target_tlib_path, psess, - sysroot, io, incr_comp_session: RwLock::new(IncrCompSession::NotInitialized), prof, |
