Skip to content

Commit 081797d

Browse files
committed
Tentative: filter out entries
1 parent ab258f8 commit 081797d

File tree

8 files changed

+51
-32
lines changed

8 files changed

+51
-32
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,7 @@ fn get_object_file_path(sess: &Session, name: &str, self_contained: bool) -> Pat
16471647
return file_path;
16481648
}
16491649
}
1650-
for search_path in sess.target_filesearch(PathKind::Native).search_paths() {
1650+
for search_path in sess.target_filesearch().search_paths(PathKind::Native) {
16511651
let file_path = search_path.dir.join(name);
16521652
if file_path.exists() {
16531653
return file_path;

compiler/rustc_interface/src/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ fn configure_and_expand(
175175
if cfg!(windows) {
176176
old_path = env::var_os("PATH").unwrap_or(old_path);
177177
let mut new_path = Vec::from_iter(
178-
sess.host_filesearch(PathKind::All).search_paths().map(|p| p.dir.clone()),
178+
sess.host_filesearch().search_paths(PathKind::All).map(|p| p.dir.clone()),
179179
);
180180
for path in env::split_paths(&old_path) {
181181
if !new_path.contains(&path) {

compiler/rustc_metadata/src/creader.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -502,17 +502,16 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
502502
};
503503

504504
// Load the proc macro crate for the host
505-
506505
locator.reset();
507506
locator.is_proc_macro = true;
508507
locator.target = &self.sess.host;
509508
locator.tuple = TargetTuple::from_tuple(config::host_tuple());
510-
locator.filesearch = self.sess.host_filesearch(path_kind);
509+
locator.filesearch = self.sess.host_filesearch();
510+
locator.path_kind = path_kind;
511511

512512
let Some(host_result) = self.load(locator)? else {
513513
return Ok(None);
514514
};
515-
516515
Ok(Some(if self.sess.opts.unstable_opts.dual_proc_macros {
517516
let host_result = match host_result {
518517
LoadResult::Previous(..) => {

compiler/rustc_metadata/src/locator.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,10 @@ pub(crate) struct CrateLocator<'a> {
253253
extra_filename: Option<&'a str>,
254254
pub target: &'a Target,
255255
pub tuple: TargetTuple,
256-
pub filesearch: FileSearch<'a>,
256+
pub filesearch: &'a FileSearch,
257257
pub is_proc_macro: bool,
258258

259+
pub path_kind: PathKind,
259260
// Mutable in-progress state or output.
260261
crate_rejections: CrateRejections,
261262
}
@@ -339,7 +340,8 @@ impl<'a> CrateLocator<'a> {
339340
extra_filename,
340341
target: &sess.target,
341342
tuple: sess.opts.target_triple.clone(),
342-
filesearch: sess.target_filesearch(path_kind),
343+
filesearch: sess.target_filesearch(),
344+
path_kind,
343345
is_proc_macro: false,
344346
crate_rejections: CrateRejections::default(),
345347
}
@@ -407,7 +409,7 @@ impl<'a> CrateLocator<'a> {
407409
// given that `extra_filename` comes from the `-C extra-filename`
408410
// option and thus can be anything, and the incorrect match will be
409411
// handled safely in `extract_one`.
410-
for search_path in self.filesearch.search_paths() {
412+
for search_path in self.filesearch.search_paths(self.path_kind) {
411413
debug!("searching {}", search_path.dir.display());
412414
let spf = &search_path.files;
413415

compiler/rustc_metadata/src/native_libs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ pub fn walk_native_lib_search_dirs<R>(
2828
mut f: impl FnMut(&Path, bool /*is_framework*/) -> ControlFlow<R>,
2929
) -> ControlFlow<R> {
3030
// Library search paths explicitly supplied by user (`-L` on the command line).
31-
for search_path in sess.target_filesearch(PathKind::Native).cli_search_paths() {
31+
for search_path in sess.target_filesearch().cli_search_paths(PathKind::Native) {
3232
f(&search_path.dir, false)?;
3333
}
34-
for search_path in sess.target_filesearch(PathKind::Framework).cli_search_paths() {
34+
for search_path in sess.target_filesearch().cli_search_paths(PathKind::Framework) {
3535
// Frameworks are looked up strictly in framework-specific paths.
3636
if search_path.kind != PathKind::All {
3737
f(&search_path.dir, true)?;

compiler/rustc_session/src/filesearch.rs

+23-16
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,44 @@ use std::path::{Path, PathBuf};
44
use std::{env, fs};
55

66
use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
7+
use rustc_target::spec::Target;
78
use smallvec::{SmallVec, smallvec};
89

910
use crate::search_paths::{PathKind, SearchPath};
1011

1112
#[derive(Clone)]
12-
pub struct FileSearch<'a> {
13-
cli_search_paths: &'a [SearchPath],
14-
tlib_path: &'a SearchPath,
15-
kind: PathKind,
13+
pub struct FileSearch {
14+
cli_search_paths: Vec<SearchPath>,
15+
tlib_path: SearchPath,
1616
}
1717

18-
impl<'a> FileSearch<'a> {
19-
pub fn cli_search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
20-
let kind = self.kind;
18+
impl FileSearch {
19+
pub fn cli_search_paths<'b>(&'b self, kind: PathKind) -> impl Iterator<Item = &'b SearchPath> {
2120
self.cli_search_paths.iter().filter(move |sp| sp.kind.matches(kind))
2221
}
2322

24-
pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
25-
let kind = self.kind;
23+
pub fn search_paths<'b>(&'b self, kind: PathKind) -> impl Iterator<Item = &'b SearchPath> {
2624
self.cli_search_paths
2725
.iter()
2826
.filter(move |sp| sp.kind.matches(kind))
29-
.chain(std::iter::once(self.tlib_path))
27+
.chain(std::iter::once(&self.tlib_path))
3028
}
3129

32-
pub fn new(
33-
cli_search_paths: &'a [SearchPath],
34-
tlib_path: &'a SearchPath,
35-
kind: PathKind,
36-
) -> FileSearch<'a> {
37-
FileSearch { cli_search_paths, tlib_path, kind }
30+
pub fn new(cli_search_paths: &[SearchPath], tlib_path: &SearchPath, target: &Target) -> Self {
31+
let this = FileSearch {
32+
cli_search_paths: cli_search_paths.to_owned(),
33+
tlib_path: tlib_path.clone(),
34+
};
35+
this.refine(&["lib", &target.staticlib_prefix, &target.dll_prefix])
36+
}
37+
// Produce a new file search from this search that has a smaller set of candidates.
38+
fn refine(mut self, allowed_prefixes: &[&str]) -> FileSearch {
39+
self.cli_search_paths
40+
.iter_mut()
41+
.for_each(|search_paths| search_paths.files.retain(allowed_prefixes));
42+
self.tlib_path.files.retain(allowed_prefixes);
43+
44+
self
3845
}
3946
}
4047

compiler/rustc_session/src/search_paths.rs

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ impl FilesIndex {
4646
.peekable();
4747
ret.peek().is_some().then(|| ret)
4848
}
49+
pub fn retain(&mut self, prefixes: &[&str]) {
50+
self.0.retain(|k, _| prefixes.iter().any(|prefix| k.starts_with(prefix)));
51+
}
4952
}
5053
/// The obvious implementation of `SearchPath::files` is a `Vec<PathBuf>`. But
5154
/// it is searched repeatedly by `find_library_crate`, and the searches involve

compiler/rustc_session/src/session.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ use crate::config::{
4444
InstrumentCoverage, OptLevel, OutFileName, OutputType, RemapPathScopeComponents,
4545
SwitchWithOptPath,
4646
};
47+
use crate::filesearch::FileSearch;
4748
use crate::parse::{ParseSess, add_feature_diagnostics};
48-
use crate::search_paths::{PathKind, SearchPath};
49+
use crate::search_paths::SearchPath;
4950
use crate::{errors, filesearch, lint};
5051

5152
struct OptimizationFuel {
@@ -218,6 +219,9 @@ pub struct Session {
218219
/// This is mainly useful for other tools that reads that debuginfo to figure out
219220
/// how to call the compiler with the same arguments.
220221
pub expanded_args: Vec<String>,
222+
223+
target_filesearch: FileSearch,
224+
host_filesearch: FileSearch,
221225
}
222226

223227
#[derive(PartialEq, Eq, PartialOrd, Ord)]
@@ -443,11 +447,11 @@ impl Session {
443447
format!("__rustc_proc_macro_decls_{:08x}__", stable_crate_id.as_u64())
444448
}
445449

446-
pub fn target_filesearch(&self, kind: PathKind) -> filesearch::FileSearch<'_> {
447-
filesearch::FileSearch::new(&self.opts.search_paths, &self.target_tlib_path, kind)
450+
pub fn target_filesearch(&self) -> &filesearch::FileSearch {
451+
&self.target_filesearch
448452
}
449-
pub fn host_filesearch(&self, kind: PathKind) -> filesearch::FileSearch<'_> {
450-
filesearch::FileSearch::new(&self.opts.search_paths, &self.host_tlib_path, kind)
453+
pub fn host_filesearch(&self) -> &filesearch::FileSearch {
454+
&self.host_filesearch
451455
}
452456

453457
/// Returns a list of directories where target-specific tool binaries are located. Some fallback
@@ -1111,7 +1115,9 @@ pub fn build_session(
11111115
});
11121116

11131117
let asm_arch = if target.allow_asm { InlineAsmArch::from_str(&target.arch).ok() } else { None };
1114-
1118+
let target_filesearch =
1119+
filesearch::FileSearch::new(&sopts.search_paths, &target_tlib_path, &target);
1120+
let host_filesearch = filesearch::FileSearch::new(&sopts.search_paths, &host_tlib_path, &host);
11151121
let sess = Session {
11161122
target,
11171123
host,
@@ -1138,6 +1144,8 @@ pub fn build_session(
11381144
cfg_version,
11391145
using_internal_features,
11401146
expanded_args,
1147+
target_filesearch,
1148+
host_filesearch,
11411149
};
11421150

11431151
validate_commandline_args_with_session_available(&sess);

0 commit comments

Comments
 (0)