Skip to content

Commit

Permalink
Tentative: filter out entries
Browse files Browse the repository at this point in the history
  • Loading branch information
osiewicz committed Nov 12, 2024
1 parent ab258f8 commit 081797d
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 32 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,7 @@ fn get_object_file_path(sess: &Session, name: &str, self_contained: bool) -> Pat
return file_path;
}
}
for search_path in sess.target_filesearch(PathKind::Native).search_paths() {
for search_path in sess.target_filesearch().search_paths(PathKind::Native) {
let file_path = search_path.dir.join(name);
if file_path.exists() {
return file_path;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ fn configure_and_expand(
if cfg!(windows) {
old_path = env::var_os("PATH").unwrap_or(old_path);
let mut new_path = Vec::from_iter(
sess.host_filesearch(PathKind::All).search_paths().map(|p| p.dir.clone()),
sess.host_filesearch().search_paths(PathKind::All).map(|p| p.dir.clone()),
);
for path in env::split_paths(&old_path) {
if !new_path.contains(&path) {
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,17 +502,16 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
};

// Load the proc macro crate for the host

locator.reset();
locator.is_proc_macro = true;
locator.target = &self.sess.host;
locator.tuple = TargetTuple::from_tuple(config::host_tuple());
locator.filesearch = self.sess.host_filesearch(path_kind);
locator.filesearch = self.sess.host_filesearch();
locator.path_kind = path_kind;

let Some(host_result) = self.load(locator)? else {
return Ok(None);
};

Ok(Some(if self.sess.opts.unstable_opts.dual_proc_macros {
let host_result = match host_result {
LoadResult::Previous(..) => {
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,10 @@ pub(crate) struct CrateLocator<'a> {
extra_filename: Option<&'a str>,
pub target: &'a Target,
pub tuple: TargetTuple,
pub filesearch: FileSearch<'a>,
pub filesearch: &'a FileSearch,
pub is_proc_macro: bool,

pub path_kind: PathKind,
// Mutable in-progress state or output.
crate_rejections: CrateRejections,
}
Expand Down Expand Up @@ -339,7 +340,8 @@ impl<'a> CrateLocator<'a> {
extra_filename,
target: &sess.target,
tuple: sess.opts.target_triple.clone(),
filesearch: sess.target_filesearch(path_kind),
filesearch: sess.target_filesearch(),
path_kind,
is_proc_macro: false,
crate_rejections: CrateRejections::default(),
}
Expand Down Expand Up @@ -407,7 +409,7 @@ impl<'a> CrateLocator<'a> {
// given that `extra_filename` comes from the `-C extra-filename`
// option and thus can be anything, and the incorrect match will be
// handled safely in `extract_one`.
for search_path in self.filesearch.search_paths() {
for search_path in self.filesearch.search_paths(self.path_kind) {
debug!("searching {}", search_path.dir.display());
let spf = &search_path.files;

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/native_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ pub fn walk_native_lib_search_dirs<R>(
mut f: impl FnMut(&Path, bool /*is_framework*/) -> ControlFlow<R>,
) -> ControlFlow<R> {
// Library search paths explicitly supplied by user (`-L` on the command line).
for search_path in sess.target_filesearch(PathKind::Native).cli_search_paths() {
for search_path in sess.target_filesearch().cli_search_paths(PathKind::Native) {
f(&search_path.dir, false)?;
}
for search_path in sess.target_filesearch(PathKind::Framework).cli_search_paths() {
for search_path in sess.target_filesearch().cli_search_paths(PathKind::Framework) {
// Frameworks are looked up strictly in framework-specific paths.
if search_path.kind != PathKind::All {
f(&search_path.dir, true)?;
Expand Down
39 changes: 23 additions & 16 deletions compiler/rustc_session/src/filesearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,44 @@ use std::path::{Path, PathBuf};
use std::{env, fs};

use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
use rustc_target::spec::Target;
use smallvec::{SmallVec, smallvec};

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

#[derive(Clone)]
pub struct FileSearch<'a> {
cli_search_paths: &'a [SearchPath],
tlib_path: &'a SearchPath,
kind: PathKind,
pub struct FileSearch {
cli_search_paths: Vec<SearchPath>,
tlib_path: SearchPath,
}

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

pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
let kind = self.kind;
pub fn search_paths<'b>(&'b self, kind: PathKind) -> impl Iterator<Item = &'b SearchPath> {
self.cli_search_paths
.iter()
.filter(move |sp| sp.kind.matches(kind))
.chain(std::iter::once(self.tlib_path))
.chain(std::iter::once(&self.tlib_path))
}

pub fn new(
cli_search_paths: &'a [SearchPath],
tlib_path: &'a SearchPath,
kind: PathKind,
) -> FileSearch<'a> {
FileSearch { cli_search_paths, tlib_path, kind }
pub fn new(cli_search_paths: &[SearchPath], tlib_path: &SearchPath, target: &Target) -> Self {
let this = FileSearch {
cli_search_paths: cli_search_paths.to_owned(),
tlib_path: tlib_path.clone(),
};
this.refine(&["lib", &target.staticlib_prefix, &target.dll_prefix])
}
// Produce a new file search from this search that has a smaller set of candidates.
fn refine(mut self, allowed_prefixes: &[&str]) -> FileSearch {
self.cli_search_paths
.iter_mut()
.for_each(|search_paths| search_paths.files.retain(allowed_prefixes));
self.tlib_path.files.retain(allowed_prefixes);

self
}
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_session/src/search_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ impl FilesIndex {
.peekable();
ret.peek().is_some().then(|| ret)
}
pub fn retain(&mut self, prefixes: &[&str]) {
self.0.retain(|k, _| prefixes.iter().any(|prefix| k.starts_with(prefix)));
}
}
/// The obvious implementation of `SearchPath::files` is a `Vec<PathBuf>`. But
/// it is searched repeatedly by `find_library_crate`, and the searches involve
Expand Down
20 changes: 14 additions & 6 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ use crate::config::{
InstrumentCoverage, OptLevel, OutFileName, OutputType, RemapPathScopeComponents,
SwitchWithOptPath,
};
use crate::filesearch::FileSearch;
use crate::parse::{ParseSess, add_feature_diagnostics};
use crate::search_paths::{PathKind, SearchPath};
use crate::search_paths::SearchPath;
use crate::{errors, filesearch, lint};

struct OptimizationFuel {
Expand Down Expand Up @@ -218,6 +219,9 @@ pub struct Session {
/// This is mainly useful for other tools that reads that debuginfo to figure out
/// how to call the compiler with the same arguments.
pub expanded_args: Vec<String>,

target_filesearch: FileSearch,
host_filesearch: FileSearch,
}

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

pub fn target_filesearch(&self, kind: PathKind) -> filesearch::FileSearch<'_> {
filesearch::FileSearch::new(&self.opts.search_paths, &self.target_tlib_path, kind)
pub fn target_filesearch(&self) -> &filesearch::FileSearch {
&self.target_filesearch
}
pub fn host_filesearch(&self, kind: PathKind) -> filesearch::FileSearch<'_> {
filesearch::FileSearch::new(&self.opts.search_paths, &self.host_tlib_path, kind)
pub fn host_filesearch(&self) -> &filesearch::FileSearch {
&self.host_filesearch
}

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

let asm_arch = if target.allow_asm { InlineAsmArch::from_str(&target.arch).ok() } else { None };

let target_filesearch =
filesearch::FileSearch::new(&sopts.search_paths, &target_tlib_path, &target);
let host_filesearch = filesearch::FileSearch::new(&sopts.search_paths, &host_tlib_path, &host);
let sess = Session {
target,
host,
Expand All @@ -1138,6 +1144,8 @@ pub fn build_session(
cfg_version,
using_internal_features,
expanded_args,
target_filesearch,
host_filesearch,
};

validate_commandline_args_with_session_available(&sess);
Expand Down

0 comments on commit 081797d

Please sign in to comment.