Skip to content

Commit dff8ee5

Browse files
committed
Replace all uses of sysroot_candidates with get_or_default_sysroot
Before this change we had two different ways to attempt to locate the sysroot which are inconsistently used: * get_or_default_sysroot which tries to locate based on the 0th cli argument and if that doesn't work falls back to locating it using the librustc_driver.so location and returns a single path., * sysroot_candidates which takes the former and additionally does another attempt at locating using librustc_driver.so except without linux multiarch handling and then returns both paths., The latter was originally introduced to be able to locate the codegen backend back when cg_llvm was dynamically linked even for a custom driver when the --sysroot passed in does not contain a copy of cg_llvm. Back then get_or_default_sysroot did not attempt to locate the sysroot based on the location of librustc_driver.so yet. Because that is now done, the only case where removing sysroot_candidates can break things is if you have a custom driver inside what looks like a sysroot including the lib/rustlib directory, but which is missing some parts of the full sysroot like eg rust-lld.
1 parent 387dae9 commit dff8ee5

File tree

5 files changed

+17
-44
lines changed

5 files changed

+17
-44
lines changed

compiler/rustc_error_messages/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl From<Vec<FluentError>> for TranslationBundleError {
107107
#[instrument(level = "trace")]
108108
pub fn fluent_bundle(
109109
sysroot: PathBuf,
110-
sysroot_candidates: Vec<PathBuf>,
110+
default_sysroot: PathBuf,
111111
requested_locale: Option<LanguageIdentifier>,
112112
additional_ftl_path: Option<&Path>,
113113
with_directionality_markers: bool,
@@ -141,7 +141,7 @@ pub fn fluent_bundle(
141141
// If the user requests the default locale then don't try to load anything.
142142
if let Some(requested_locale) = requested_locale {
143143
let mut found_resources = false;
144-
for mut sysroot in Some(sysroot).into_iter().chain(sysroot_candidates.into_iter()) {
144+
for mut sysroot in [sysroot, default_sysroot] {
145145
sysroot.push("share");
146146
sysroot.push("locale");
147147
sysroot.push(requested_locale.to_string());

compiler/rustc_interface/src/interface.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_parse::parser::attr::AllowLeadingUnsafe;
1818
use rustc_query_impl::QueryCtxt;
1919
use rustc_query_system::query::print_query_stack;
2020
use rustc_session::config::{self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName};
21-
use rustc_session::filesearch::sysroot_candidates;
21+
use rustc_session::filesearch::get_or_default_sysroot;
2222
use rustc_session::parse::ParseSess;
2323
use rustc_session::{CompilerIO, EarlyDiagCtxt, Session, lint};
2424
use rustc_span::source_map::{FileLoader, RealFileLoader, SourceMapInputs};
@@ -443,7 +443,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
443443

444444
let bundle = match rustc_errors::fluent_bundle(
445445
config.opts.sysroot.clone(),
446-
sysroot_candidates().to_vec(),
446+
get_or_default_sysroot(),
447447
config.opts.unstable_opts.translate_lang.clone(),
448448
config.opts.unstable_opts.translate_additional_ftl.as_deref(),
449449
config.opts.unstable_opts.translate_directionality_markers,

compiler/rustc_interface/src/util.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
22
use std::path::{Path, PathBuf};
33
use std::sync::atomic::{AtomicBool, Ordering};
44
use std::sync::{Arc, OnceLock};
5-
use std::{env, iter, thread};
5+
use std::{env, thread};
66

77
use rustc_ast as ast;
88
use rustc_codegen_ssa::traits::CodegenBackend;
@@ -12,7 +12,6 @@ use rustc_metadata::{DylibError, load_symbol_from_dylib};
1212
use rustc_middle::ty::CurrentGcx;
1313
use rustc_parse::validate_attr;
1414
use rustc_session::config::{Cfg, OutFileName, OutputFilenames, OutputTypes, host_tuple};
15-
use rustc_session::filesearch::sysroot_candidates;
1615
use rustc_session::lint::{self, BuiltinLintDiag, LintBuffer};
1716
use rustc_session::output::{CRATE_TYPES, categorize_crate_type};
1817
use rustc_session::{EarlyDiagCtxt, Session, filesearch};
@@ -346,7 +345,7 @@ pub fn rustc_path<'a>() -> Option<&'a Path> {
346345
}
347346

348347
fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
349-
sysroot_candidates().iter().find_map(|sysroot| {
348+
Some(filesearch::get_or_default_sysroot()).iter().find_map(|sysroot| {
350349
let candidate = sysroot.join(bin_path).join(if cfg!(target_os = "windows") {
351350
"rustc.exe"
352351
} else {
@@ -374,10 +373,10 @@ fn get_codegen_sysroot(
374373
);
375374

376375
let target = host_tuple();
377-
let sysroot_candidates = sysroot_candidates();
376+
let sysroot_candidates = filesearch::sysroot_with_fallback(&sysroot);
378377

379-
let sysroot = iter::once(sysroot)
380-
.chain(sysroot_candidates.iter().map(<_>::as_ref))
378+
let sysroot = sysroot_candidates
379+
.iter()
381380
.map(|sysroot| {
382381
filesearch::make_target_lib_path(sysroot, target).with_file_name("codegen-backends")
383382
})

compiler/rustc_session/src/filesearch.rs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -182,35 +182,13 @@ fn current_dll_path() -> Result<PathBuf, String> {
182182
Err("current_dll_path is not supported on WASI".to_string())
183183
}
184184

185-
pub fn sysroot_candidates() -> SmallVec<[PathBuf; 2]> {
186-
let mut sysroot_candidates: SmallVec<[PathBuf; 2]> = smallvec![get_or_default_sysroot()];
187-
if let Ok(dll) = current_dll_path() {
188-
// use `parent` twice to chop off the file name and then also the
189-
// directory containing the dll which should be either `lib` or `bin`.
190-
if let Some(path) = dll.parent().and_then(|p| p.parent()) {
191-
// The original `path` pointed at the `rustc_driver` crate's dll.
192-
// Now that dll should only be in one of two locations. The first is
193-
// in the compiler's libdir, for example `$sysroot/lib/*.dll`. The
194-
// other is the target's libdir, for example
195-
// `$sysroot/lib/rustlib/$target/lib/*.dll`.
196-
//
197-
// We don't know which, so let's assume that if our `path` above
198-
// ends in `$target` we *could* be in the target libdir, and always
199-
// assume that we may be in the main libdir.
200-
sysroot_candidates.push(path.to_owned());
201-
202-
if path.ends_with(crate::config::host_tuple()) {
203-
sysroot_candidates.extend(
204-
path.parent() // chop off `$target`
205-
.and_then(|p| p.parent()) // chop off `rustlib`
206-
.and_then(|p| p.parent()) // chop off `lib`
207-
.map(|s| s.to_owned()),
208-
);
209-
}
210-
}
185+
pub fn sysroot_with_fallback(sysroot: &Path) -> SmallVec<[PathBuf; 2]> {
186+
let mut candidates = smallvec![sysroot.to_owned()];
187+
let default_sysroot = get_or_default_sysroot();
188+
if default_sysroot != sysroot {
189+
candidates.push(default_sysroot);
211190
}
212-
213-
sysroot_candidates
191+
candidates
214192
}
215193

216194
/// Returns the provided sysroot or calls [`get_or_default_sysroot`] if it's none.
@@ -236,7 +214,7 @@ pub fn get_or_default_sysroot() -> PathBuf {
236214
dll.display()
237215
))?;
238216

239-
// if `dir` points target's dir, move up to the sysroot
217+
// if `dir` points to target's dir, move up to the sysroot
240218
let mut sysroot_dir = if dir.ends_with(crate::config::host_tuple()) {
241219
dir.parent() // chop off `$target`
242220
.and_then(|p| p.parent()) // chop off `rustlib`

compiler/rustc_session/src/session.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,9 @@ impl Session {
458458
/// directories are also returned, for example if `--sysroot` is used but tools are missing
459459
/// (#125246): we also add the bin directories to the sysroot where rustc is located.
460460
pub fn get_tools_search_paths(&self, self_contained: bool) -> Vec<PathBuf> {
461-
let bin_path = filesearch::make_target_bin_path(&self.sysroot, config::host_tuple());
462-
let fallback_sysroot_paths = filesearch::sysroot_candidates()
461+
let search_paths = filesearch::sysroot_with_fallback(&self.sysroot)
463462
.into_iter()
464-
// Ignore sysroot candidate if it was the same as the sysroot path we just used.
465-
.filter(|sysroot| *sysroot != self.sysroot)
466463
.map(|sysroot| filesearch::make_target_bin_path(&sysroot, config::host_tuple()));
467-
let search_paths = std::iter::once(bin_path).chain(fallback_sysroot_paths);
468464

469465
if self_contained {
470466
// The self-contained tools are expected to be e.g. in `bin/self-contained` in the

0 commit comments

Comments
 (0)