Auto merge of #123854 - petrochenkov:searchdirs2, r=lqd

linker: Remove laziness and caching from native search directory walks

It shouldn't be necessary for performance now.

Follow up to https://github.com/rust-lang/rust/pull/123827.
This commit is contained in:
bors 2024-04-13 13:12:56 +00:00
commit 3a0db6c152
5 changed files with 22 additions and 121 deletions

View File

@ -43,7 +43,6 @@
use tempfile::Builder as TempFileBuilder; use tempfile::Builder as TempFileBuilder;
use itertools::Itertools; use itertools::Itertools;
use std::cell::OnceCell;
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::ffi::{OsStr, OsString}; use std::ffi::{OsStr, OsString};
use std::fs::{read, File, OpenOptions}; use std::fs::{read, File, OpenOptions};
@ -53,20 +52,6 @@
use std::process::{ExitStatus, Output, Stdio}; use std::process::{ExitStatus, Output, Stdio};
use std::{env, fmt, fs, io, mem, str}; use std::{env, fmt, fs, io, mem, str};
#[derive(Default)]
pub struct SearchPaths(OnceCell<Vec<PathBuf>>);
impl SearchPaths {
pub(super) fn get(&self, sess: &Session) -> impl Iterator<Item = &Path> {
let native_search_paths = || {
Vec::from_iter(
sess.target_filesearch(PathKind::Native).search_path_dirs().map(|p| p.to_owned()),
)
};
self.0.get_or_init(native_search_paths).iter().map(|p| &**p)
}
}
pub fn ensure_removed(dcx: &DiagCtxt, path: &Path) { pub fn ensure_removed(dcx: &DiagCtxt, path: &Path) {
if let Err(e) = fs::remove_file(path) { if let Err(e) = fs::remove_file(path) {
if e.kind() != io::ErrorKind::NotFound { if e.kind() != io::ErrorKind::NotFound {
@ -382,24 +367,21 @@ fn link_rlib<'a>(
// feature then we'll need to figure out how to record what objects were // feature then we'll need to figure out how to record what objects were
// loaded from the libraries found here and then encode that into the // loaded from the libraries found here and then encode that into the
// metadata of the rlib we're generating somehow. // metadata of the rlib we're generating somehow.
let search_paths = SearchPaths::default();
for lib in codegen_results.crate_info.used_libraries.iter() { for lib in codegen_results.crate_info.used_libraries.iter() {
let NativeLibKind::Static { bundle: None | Some(true), .. } = lib.kind else { let NativeLibKind::Static { bundle: None | Some(true), .. } = lib.kind else {
continue; continue;
}; };
let search_paths = search_paths.get(sess);
if flavor == RlibFlavor::Normal if flavor == RlibFlavor::Normal
&& let Some(filename) = lib.filename && let Some(filename) = lib.filename
{ {
let path = find_native_static_library(filename.as_str(), true, search_paths, sess); let path = find_native_static_library(filename.as_str(), true, sess);
let src = read(path) let src = read(path)
.map_err(|e| sess.dcx().emit_fatal(errors::ReadFileError { message: e }))?; .map_err(|e| sess.dcx().emit_fatal(errors::ReadFileError { message: e }))?;
let (data, _) = create_wrapper_file(sess, ".bundled_lib".to_string(), &src); let (data, _) = create_wrapper_file(sess, ".bundled_lib".to_string(), &src);
let wrapper_file = emit_wrapper_file(sess, &data, tmpdir, filename.as_str()); let wrapper_file = emit_wrapper_file(sess, &data, tmpdir, filename.as_str());
packed_bundled_libs.push(wrapper_file); packed_bundled_libs.push(wrapper_file);
} else { } else {
let path = let path = find_native_static_library(lib.name.as_str(), lib.verbatim, sess);
find_native_static_library(lib.name.as_str(), lib.verbatim, search_paths, sess);
ab.add_archive(&path, Box::new(|_| false)).unwrap_or_else(|error| { ab.add_archive(&path, Box::new(|_| false)).unwrap_or_else(|error| {
sess.dcx().emit_fatal(errors::AddNativeLibrary { library_path: path, error }) sess.dcx().emit_fatal(errors::AddNativeLibrary { library_path: path, error })
}); });
@ -2518,7 +2500,6 @@ fn add_native_libs_from_crate(
archive_builder_builder: &dyn ArchiveBuilderBuilder, archive_builder_builder: &dyn ArchiveBuilderBuilder,
codegen_results: &CodegenResults, codegen_results: &CodegenResults,
tmpdir: &Path, tmpdir: &Path,
search_paths: &SearchPaths,
bundled_libs: &FxIndexSet<Symbol>, bundled_libs: &FxIndexSet<Symbol>,
cnum: CrateNum, cnum: CrateNum,
link_static: bool, link_static: bool,
@ -2581,7 +2562,7 @@ fn add_native_libs_from_crate(
cmd.link_staticlib_by_path(&path, whole_archive); cmd.link_staticlib_by_path(&path, whole_archive);
} }
} else { } else {
cmd.link_staticlib_by_name(name, verbatim, whole_archive, search_paths); cmd.link_staticlib_by_name(name, verbatim, whole_archive);
} }
} }
} }
@ -2595,7 +2576,7 @@ fn add_native_libs_from_crate(
// link kind is unspecified. // link kind is unspecified.
if !link_output_kind.can_link_dylib() && !sess.target.crt_static_allows_dylibs { if !link_output_kind.can_link_dylib() && !sess.target.crt_static_allows_dylibs {
if link_static { if link_static {
cmd.link_staticlib_by_name(name, verbatim, false, search_paths); cmd.link_staticlib_by_name(name, verbatim, false);
} }
} else { } else {
if link_dynamic { if link_dynamic {
@ -2642,7 +2623,6 @@ fn add_local_native_libraries(
} }
} }
let search_paths = SearchPaths::default();
// All static and dynamic native library dependencies are linked to the local crate. // All static and dynamic native library dependencies are linked to the local crate.
let link_static = true; let link_static = true;
let link_dynamic = true; let link_dynamic = true;
@ -2652,7 +2632,6 @@ fn add_local_native_libraries(
archive_builder_builder, archive_builder_builder,
codegen_results, codegen_results,
tmpdir, tmpdir,
&search_paths,
&Default::default(), &Default::default(),
LOCAL_CRATE, LOCAL_CRATE,
link_static, link_static,
@ -2684,7 +2663,6 @@ fn add_upstream_rust_crates(
.find(|(ty, _)| *ty == crate_type) .find(|(ty, _)| *ty == crate_type)
.expect("failed to find crate type in dependency format list"); .expect("failed to find crate type in dependency format list");
let search_paths = SearchPaths::default();
for &cnum in &codegen_results.crate_info.used_crates { for &cnum in &codegen_results.crate_info.used_crates {
// We may not pass all crates through to the linker. Some crates may appear statically in // We may not pass all crates through to the linker. Some crates may appear statically in
// an existing dylib, meaning we'll pick up all the symbols from the dylib. // an existing dylib, meaning we'll pick up all the symbols from the dylib.
@ -2741,7 +2719,6 @@ fn add_upstream_rust_crates(
archive_builder_builder, archive_builder_builder,
codegen_results, codegen_results,
tmpdir, tmpdir,
&search_paths,
&bundled_libs, &bundled_libs,
cnum, cnum,
link_static, link_static,
@ -2759,7 +2736,6 @@ fn add_upstream_native_libraries(
tmpdir: &Path, tmpdir: &Path,
link_output_kind: LinkOutputKind, link_output_kind: LinkOutputKind,
) { ) {
let search_paths = SearchPaths::default();
for &cnum in &codegen_results.crate_info.used_crates { for &cnum in &codegen_results.crate_info.used_crates {
// Static libraries are not linked here, they are linked in `add_upstream_rust_crates`. // Static libraries are not linked here, they are linked in `add_upstream_rust_crates`.
// FIXME: Merge this function to `add_upstream_rust_crates` so that all native libraries // FIXME: Merge this function to `add_upstream_rust_crates` so that all native libraries
@ -2781,7 +2757,6 @@ fn add_upstream_native_libraries(
archive_builder_builder, archive_builder_builder,
codegen_results, codegen_results,
tmpdir, tmpdir,
&search_paths,
&Default::default(), &Default::default(),
cnum, cnum,
link_static, link_static,

View File

@ -1,6 +1,5 @@
use super::command::Command; use super::command::Command;
use super::symbol_export; use super::symbol_export;
use crate::back::link::SearchPaths;
use crate::errors; use crate::errors;
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
@ -172,13 +171,7 @@ pub trait Linker {
fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) { fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
bug!("framework linked with unsupported linker") bug!("framework linked with unsupported linker")
} }
fn link_staticlib_by_name( fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool);
&mut self,
name: &str,
verbatim: bool,
whole_archive: bool,
search_paths: &SearchPaths,
);
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool); fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool);
fn include_path(&mut self, path: &Path); fn include_path(&mut self, path: &Path);
fn framework_path(&mut self, path: &Path); fn framework_path(&mut self, path: &Path);
@ -482,13 +475,7 @@ fn link_framework_by_name(&mut self, name: &str, _verbatim: bool, as_needed: boo
self.cmd.arg("-framework").arg(name); self.cmd.arg("-framework").arg(name);
} }
fn link_staticlib_by_name( fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
&mut self,
name: &str,
verbatim: bool,
whole_archive: bool,
search_paths: &SearchPaths,
) {
self.hint_static(); self.hint_static();
let colon = if verbatim && self.is_gnu { ":" } else { "" }; let colon = if verbatim && self.is_gnu { ":" } else { "" };
if !whole_archive { if !whole_archive {
@ -497,8 +484,7 @@ fn link_staticlib_by_name(
// -force_load is the macOS equivalent of --whole-archive, but it // -force_load is the macOS equivalent of --whole-archive, but it
// involves passing the full path to the library to link. // involves passing the full path to the library to link.
self.linker_arg("-force_load"); self.linker_arg("-force_load");
let search_paths = search_paths.get(self.sess); self.linker_arg(find_native_static_library(name, verbatim, self.sess));
self.linker_arg(find_native_static_library(name, verbatim, search_paths, self.sess));
} else { } else {
self.linker_arg("--whole-archive"); self.linker_arg("--whole-archive");
self.cmd.arg(format!("-l{colon}{name}")); self.cmd.arg(format!("-l{colon}{name}"));
@ -825,13 +811,7 @@ fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" })); self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
} }
fn link_staticlib_by_name( fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
&mut self,
name: &str,
verbatim: bool,
whole_archive: bool,
_search_paths: &SearchPaths,
) {
let prefix = if whole_archive { "/WHOLEARCHIVE:" } else { "" }; let prefix = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
let suffix = if verbatim { "" } else { ".lib" }; let suffix = if verbatim { "" } else { ".lib" };
self.cmd.arg(format!("{prefix}{name}{suffix}")); self.cmd.arg(format!("{prefix}{name}{suffix}"));
@ -1064,13 +1044,7 @@ fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool)
self.cmd.arg("-l").arg(name); self.cmd.arg("-l").arg(name);
} }
fn link_staticlib_by_name( fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, _whole_archive: bool) {
&mut self,
name: &str,
_verbatim: bool,
_whole_archive: bool,
_search_paths: &SearchPaths,
) {
self.cmd.arg("-l").arg(name); self.cmd.arg("-l").arg(name);
} }
@ -1243,13 +1217,7 @@ fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool)
self.cmd.arg("-l").arg(name); self.cmd.arg("-l").arg(name);
} }
fn link_staticlib_by_name( fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
&mut self,
name: &str,
_verbatim: bool,
whole_archive: bool,
_search_paths: &SearchPaths,
) {
if !whole_archive { if !whole_archive {
self.cmd.arg("-l").arg(name); self.cmd.arg("-l").arg(name);
} else { } else {
@ -1396,13 +1364,7 @@ fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool)
bug!("dylibs are not supported on L4Re"); bug!("dylibs are not supported on L4Re");
} }
fn link_staticlib_by_name( fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
&mut self,
name: &str,
_verbatim: bool,
whole_archive: bool,
_search_paths: &SearchPaths,
) {
self.hint_static(); self.hint_static();
if !whole_archive { if !whole_archive {
self.cmd.arg(format!("-PC{name}")); self.cmd.arg(format!("-PC{name}"));
@ -1580,20 +1542,13 @@ fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool)
self.cmd.arg(format!("-l{name}")); self.cmd.arg(format!("-l{name}"));
} }
fn link_staticlib_by_name( fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
&mut self,
name: &str,
verbatim: bool,
whole_archive: bool,
search_paths: &SearchPaths,
) {
self.hint_static(); self.hint_static();
if !whole_archive { if !whole_archive {
self.cmd.arg(format!("-l{name}")); self.cmd.arg(format!("-l{name}"));
} else { } else {
let mut arg = OsString::from("-bkeepfile:"); let mut arg = OsString::from("-bkeepfile:");
let search_path = search_paths.get(self.sess); arg.push(find_native_static_library(name, verbatim, self.sess));
arg.push(find_native_static_library(name, verbatim, search_path, self.sess));
self.cmd.arg(arg); self.cmd.arg(arg);
} }
} }
@ -1792,13 +1747,7 @@ fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool)
panic!("external dylibs not supported") panic!("external dylibs not supported")
} }
fn link_staticlib_by_name( fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
&mut self,
_name: &str,
_verbatim: bool,
_whole_archive: bool,
_search_paths: &SearchPaths,
) {
panic!("staticlibs not supported") panic!("staticlibs not supported")
} }
@ -1880,13 +1829,7 @@ fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool)
panic!("external dylibs not supported") panic!("external dylibs not supported")
} }
fn link_staticlib_by_name( fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
&mut self,
_name: &str,
_verbatim: bool,
_whole_archive: bool,
_search_paths: &SearchPaths,
) {
panic!("staticlibs not supported") panic!("staticlibs not supported")
} }
@ -1977,13 +1920,7 @@ fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool)
panic!("external dylibs not supported") panic!("external dylibs not supported")
} }
fn link_staticlib_by_name( fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
&mut self,
_name: &str,
_verbatim: bool,
_whole_archive: bool,
_search_paths: &SearchPaths,
) {
panic!("staticlibs not supported") panic!("staticlibs not supported")
} }

View File

@ -171,7 +171,7 @@ fn configure_and_expand(
if cfg!(windows) { if cfg!(windows) {
old_path = env::var_os("PATH").unwrap_or(old_path); old_path = env::var_os("PATH").unwrap_or(old_path);
let mut new_path = Vec::from_iter( let mut new_path = Vec::from_iter(
sess.host_filesearch(PathKind::All).search_path_dirs().map(|p| p.to_owned()), sess.host_filesearch(PathKind::All).search_paths().map(|p| p.dir.clone()),
); );
for path in env::split_paths(&old_path) { for path in env::split_paths(&old_path) {
if !new_path.contains(&path) { if !new_path.contains(&path) {

View File

@ -17,14 +17,9 @@
use crate::errors; use crate::errors;
use std::path::{Path, PathBuf}; use std::path::PathBuf;
pub fn find_native_static_library<'a>( pub fn find_native_static_library(name: &str, verbatim: bool, sess: &Session) -> PathBuf {
name: &str,
verbatim: bool,
search_paths: impl Iterator<Item = &'a Path>,
sess: &Session,
) -> PathBuf {
let formats = if verbatim { let formats = if verbatim {
vec![("".into(), "".into())] vec![("".into(), "".into())]
} else { } else {
@ -35,9 +30,9 @@ pub fn find_native_static_library<'a>(
if os == unix { vec![os] } else { vec![os, unix] } if os == unix { vec![os] } else { vec![os, unix] }
}; };
for path in search_paths { for path in sess.target_filesearch(PathKind::Native).search_paths() {
for (prefix, suffix) in &formats { for (prefix, suffix) in &formats {
let test = path.join(format!("{prefix}{name}{suffix}")); let test = path.dir.join(format!("{prefix}{name}{suffix}"));
if test.exists() { if test.exists() {
return test; return test;
} }
@ -60,8 +55,7 @@ fn find_bundled_library(
&& (sess.opts.unstable_opts.packed_bundled_libs || has_cfg || whole_archive == Some(true)) && (sess.opts.unstable_opts.packed_bundled_libs || has_cfg || whole_archive == Some(true))
{ {
let verbatim = verbatim.unwrap_or(false); let verbatim = verbatim.unwrap_or(false);
let search_paths = sess.target_filesearch(PathKind::Native).search_path_dirs(); return find_native_static_library(name.as_str(), verbatim, sess)
return find_native_static_library(name.as_str(), verbatim, search_paths, sess)
.file_name() .file_name()
.and_then(|s| s.to_str()) .and_then(|s| s.to_str())
.map(Symbol::intern); .map(Symbol::intern);

View File

@ -45,11 +45,6 @@ pub fn new(
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple); debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
FileSearch { sysroot, triple, search_paths, tlib_path, kind } FileSearch { sysroot, triple, search_paths, tlib_path, kind }
} }
/// Returns just the directories within the search paths.
pub fn search_path_dirs(&self) -> impl Iterator<Item = &'a Path> {
self.search_paths().map(|sp| &*sp.dir)
}
} }
pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf { pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {