Skip to content

Commit 3a0db6c

Browse files
committed
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 #123827.
2 parents 7106800 + ed62b57 commit 3a0db6c

File tree

5 files changed

+23
-122
lines changed

5 files changed

+23
-122
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+4-29
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use regex::Regex;
4343
use tempfile::Builder as TempFileBuilder;
4444

4545
use itertools::Itertools;
46-
use std::cell::OnceCell;
4746
use std::collections::BTreeSet;
4847
use std::ffi::{OsStr, OsString};
4948
use std::fs::{read, File, OpenOptions};
@@ -53,20 +52,6 @@ use std::path::{Path, PathBuf};
5352
use std::process::{ExitStatus, Output, Stdio};
5453
use std::{env, fmt, fs, io, mem, str};
5554

56-
#[derive(Default)]
57-
pub struct SearchPaths(OnceCell<Vec<PathBuf>>);
58-
59-
impl SearchPaths {
60-
pub(super) fn get(&self, sess: &Session) -> impl Iterator<Item = &Path> {
61-
let native_search_paths = || {
62-
Vec::from_iter(
63-
sess.target_filesearch(PathKind::Native).search_path_dirs().map(|p| p.to_owned()),
64-
)
65-
};
66-
self.0.get_or_init(native_search_paths).iter().map(|p| &**p)
67-
}
68-
}
69-
7055
pub fn ensure_removed(dcx: &DiagCtxt, path: &Path) {
7156
if let Err(e) = fs::remove_file(path) {
7257
if e.kind() != io::ErrorKind::NotFound {
@@ -382,24 +367,21 @@ fn link_rlib<'a>(
382367
// feature then we'll need to figure out how to record what objects were
383368
// loaded from the libraries found here and then encode that into the
384369
// metadata of the rlib we're generating somehow.
385-
let search_paths = SearchPaths::default();
386370
for lib in codegen_results.crate_info.used_libraries.iter() {
387371
let NativeLibKind::Static { bundle: None | Some(true), .. } = lib.kind else {
388372
continue;
389373
};
390-
let search_paths = search_paths.get(sess);
391374
if flavor == RlibFlavor::Normal
392375
&& let Some(filename) = lib.filename
393376
{
394-
let path = find_native_static_library(filename.as_str(), true, search_paths, sess);
377+
let path = find_native_static_library(filename.as_str(), true, sess);
395378
let src = read(path)
396379
.map_err(|e| sess.dcx().emit_fatal(errors::ReadFileError { message: e }))?;
397380
let (data, _) = create_wrapper_file(sess, ".bundled_lib".to_string(), &src);
398381
let wrapper_file = emit_wrapper_file(sess, &data, tmpdir, filename.as_str());
399382
packed_bundled_libs.push(wrapper_file);
400383
} else {
401-
let path =
402-
find_native_static_library(lib.name.as_str(), lib.verbatim, search_paths, sess);
384+
let path = find_native_static_library(lib.name.as_str(), lib.verbatim, sess);
403385
ab.add_archive(&path, Box::new(|_| false)).unwrap_or_else(|error| {
404386
sess.dcx().emit_fatal(errors::AddNativeLibrary { library_path: path, error })
405387
});
@@ -2518,7 +2500,6 @@ fn add_native_libs_from_crate(
25182500
archive_builder_builder: &dyn ArchiveBuilderBuilder,
25192501
codegen_results: &CodegenResults,
25202502
tmpdir: &Path,
2521-
search_paths: &SearchPaths,
25222503
bundled_libs: &FxIndexSet<Symbol>,
25232504
cnum: CrateNum,
25242505
link_static: bool,
@@ -2581,7 +2562,7 @@ fn add_native_libs_from_crate(
25812562
cmd.link_staticlib_by_path(&path, whole_archive);
25822563
}
25832564
} else {
2584-
cmd.link_staticlib_by_name(name, verbatim, whole_archive, search_paths);
2565+
cmd.link_staticlib_by_name(name, verbatim, whole_archive);
25852566
}
25862567
}
25872568
}
@@ -2595,7 +2576,7 @@ fn add_native_libs_from_crate(
25952576
// link kind is unspecified.
25962577
if !link_output_kind.can_link_dylib() && !sess.target.crt_static_allows_dylibs {
25972578
if link_static {
2598-
cmd.link_staticlib_by_name(name, verbatim, false, search_paths);
2579+
cmd.link_staticlib_by_name(name, verbatim, false);
25992580
}
26002581
} else {
26012582
if link_dynamic {
@@ -2642,7 +2623,6 @@ fn add_local_native_libraries(
26422623
}
26432624
}
26442625

2645-
let search_paths = SearchPaths::default();
26462626
// All static and dynamic native library dependencies are linked to the local crate.
26472627
let link_static = true;
26482628
let link_dynamic = true;
@@ -2652,7 +2632,6 @@ fn add_local_native_libraries(
26522632
archive_builder_builder,
26532633
codegen_results,
26542634
tmpdir,
2655-
&search_paths,
26562635
&Default::default(),
26572636
LOCAL_CRATE,
26582637
link_static,
@@ -2684,7 +2663,6 @@ fn add_upstream_rust_crates(
26842663
.find(|(ty, _)| *ty == crate_type)
26852664
.expect("failed to find crate type in dependency format list");
26862665

2687-
let search_paths = SearchPaths::default();
26882666
for &cnum in &codegen_results.crate_info.used_crates {
26892667
// We may not pass all crates through to the linker. Some crates may appear statically in
26902668
// an existing dylib, meaning we'll pick up all the symbols from the dylib.
@@ -2741,7 +2719,6 @@ fn add_upstream_rust_crates(
27412719
archive_builder_builder,
27422720
codegen_results,
27432721
tmpdir,
2744-
&search_paths,
27452722
&bundled_libs,
27462723
cnum,
27472724
link_static,
@@ -2759,7 +2736,6 @@ fn add_upstream_native_libraries(
27592736
tmpdir: &Path,
27602737
link_output_kind: LinkOutputKind,
27612738
) {
2762-
let search_paths = SearchPaths::default();
27632739
for &cnum in &codegen_results.crate_info.used_crates {
27642740
// Static libraries are not linked here, they are linked in `add_upstream_rust_crates`.
27652741
// FIXME: Merge this function to `add_upstream_rust_crates` so that all native libraries
@@ -2781,7 +2757,6 @@ fn add_upstream_native_libraries(
27812757
archive_builder_builder,
27822758
codegen_results,
27832759
tmpdir,
2784-
&search_paths,
27852760
&Default::default(),
27862761
cnum,
27872762
link_static,

compiler/rustc_codegen_ssa/src/back/linker.rs

+12-75
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::command::Command;
22
use super::symbol_export;
3-
use crate::back::link::SearchPaths;
43
use crate::errors;
54
use rustc_span::symbol::sym;
65

@@ -172,13 +171,7 @@ pub trait Linker {
172171
fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
173172
bug!("framework linked with unsupported linker")
174173
}
175-
fn link_staticlib_by_name(
176-
&mut self,
177-
name: &str,
178-
verbatim: bool,
179-
whole_archive: bool,
180-
search_paths: &SearchPaths,
181-
);
174+
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool);
182175
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool);
183176
fn include_path(&mut self, path: &Path);
184177
fn framework_path(&mut self, path: &Path);
@@ -482,13 +475,7 @@ impl<'a> Linker for GccLinker<'a> {
482475
self.cmd.arg("-framework").arg(name);
483476
}
484477

485-
fn link_staticlib_by_name(
486-
&mut self,
487-
name: &str,
488-
verbatim: bool,
489-
whole_archive: bool,
490-
search_paths: &SearchPaths,
491-
) {
478+
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
492479
self.hint_static();
493480
let colon = if verbatim && self.is_gnu { ":" } else { "" };
494481
if !whole_archive {
@@ -497,8 +484,7 @@ impl<'a> Linker for GccLinker<'a> {
497484
// -force_load is the macOS equivalent of --whole-archive, but it
498485
// involves passing the full path to the library to link.
499486
self.linker_arg("-force_load");
500-
let search_paths = search_paths.get(self.sess);
501-
self.linker_arg(find_native_static_library(name, verbatim, search_paths, self.sess));
487+
self.linker_arg(find_native_static_library(name, verbatim, self.sess));
502488
} else {
503489
self.linker_arg("--whole-archive");
504490
self.cmd.arg(format!("-l{colon}{name}"));
@@ -825,13 +811,7 @@ impl<'a> Linker for MsvcLinker<'a> {
825811
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
826812
}
827813

828-
fn link_staticlib_by_name(
829-
&mut self,
830-
name: &str,
831-
verbatim: bool,
832-
whole_archive: bool,
833-
_search_paths: &SearchPaths,
834-
) {
814+
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
835815
let prefix = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
836816
let suffix = if verbatim { "" } else { ".lib" };
837817
self.cmd.arg(format!("{prefix}{name}{suffix}"));
@@ -1064,13 +1044,7 @@ impl<'a> Linker for EmLinker<'a> {
10641044
self.cmd.arg("-l").arg(name);
10651045
}
10661046

1067-
fn link_staticlib_by_name(
1068-
&mut self,
1069-
name: &str,
1070-
_verbatim: bool,
1071-
_whole_archive: bool,
1072-
_search_paths: &SearchPaths,
1073-
) {
1047+
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, _whole_archive: bool) {
10741048
self.cmd.arg("-l").arg(name);
10751049
}
10761050

@@ -1243,13 +1217,7 @@ impl<'a> Linker for WasmLd<'a> {
12431217
self.cmd.arg("-l").arg(name);
12441218
}
12451219

1246-
fn link_staticlib_by_name(
1247-
&mut self,
1248-
name: &str,
1249-
_verbatim: bool,
1250-
whole_archive: bool,
1251-
_search_paths: &SearchPaths,
1252-
) {
1220+
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
12531221
if !whole_archive {
12541222
self.cmd.arg("-l").arg(name);
12551223
} else {
@@ -1396,13 +1364,7 @@ impl<'a> Linker for L4Bender<'a> {
13961364
bug!("dylibs are not supported on L4Re");
13971365
}
13981366

1399-
fn link_staticlib_by_name(
1400-
&mut self,
1401-
name: &str,
1402-
_verbatim: bool,
1403-
whole_archive: bool,
1404-
_search_paths: &SearchPaths,
1405-
) {
1367+
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
14061368
self.hint_static();
14071369
if !whole_archive {
14081370
self.cmd.arg(format!("-PC{name}"));
@@ -1580,20 +1542,13 @@ impl<'a> Linker for AixLinker<'a> {
15801542
self.cmd.arg(format!("-l{name}"));
15811543
}
15821544

1583-
fn link_staticlib_by_name(
1584-
&mut self,
1585-
name: &str,
1586-
verbatim: bool,
1587-
whole_archive: bool,
1588-
search_paths: &SearchPaths,
1589-
) {
1545+
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
15901546
self.hint_static();
15911547
if !whole_archive {
15921548
self.cmd.arg(format!("-l{name}"));
15931549
} else {
15941550
let mut arg = OsString::from("-bkeepfile:");
1595-
let search_path = search_paths.get(self.sess);
1596-
arg.push(find_native_static_library(name, verbatim, search_path, self.sess));
1551+
arg.push(find_native_static_library(name, verbatim, self.sess));
15971552
self.cmd.arg(arg);
15981553
}
15991554
}
@@ -1792,13 +1747,7 @@ impl<'a> Linker for PtxLinker<'a> {
17921747
panic!("external dylibs not supported")
17931748
}
17941749

1795-
fn link_staticlib_by_name(
1796-
&mut self,
1797-
_name: &str,
1798-
_verbatim: bool,
1799-
_whole_archive: bool,
1800-
_search_paths: &SearchPaths,
1801-
) {
1750+
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
18021751
panic!("staticlibs not supported")
18031752
}
18041753

@@ -1880,13 +1829,7 @@ impl<'a> Linker for LlbcLinker<'a> {
18801829
panic!("external dylibs not supported")
18811830
}
18821831

1883-
fn link_staticlib_by_name(
1884-
&mut self,
1885-
_name: &str,
1886-
_verbatim: bool,
1887-
_whole_archive: bool,
1888-
_search_paths: &SearchPaths,
1889-
) {
1832+
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
18901833
panic!("staticlibs not supported")
18911834
}
18921835

@@ -1977,13 +1920,7 @@ impl<'a> Linker for BpfLinker<'a> {
19771920
panic!("external dylibs not supported")
19781921
}
19791922

1980-
fn link_staticlib_by_name(
1981-
&mut self,
1982-
_name: &str,
1983-
_verbatim: bool,
1984-
_whole_archive: bool,
1985-
_search_paths: &SearchPaths,
1986-
) {
1923+
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
19871924
panic!("staticlibs not supported")
19881925
}
19891926

compiler/rustc_interface/src/passes.rs

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

compiler/rustc_metadata/src/native_libs.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,9 @@ use rustc_target::spec::abi::Abi;
1717

1818
use crate::errors;
1919

20-
use std::path::{Path, PathBuf};
21-
22-
pub fn find_native_static_library<'a>(
23-
name: &str,
24-
verbatim: bool,
25-
search_paths: impl Iterator<Item = &'a Path>,
26-
sess: &Session,
27-
) -> PathBuf {
20+
use std::path::PathBuf;
21+
22+
pub fn find_native_static_library(name: &str, verbatim: bool, sess: &Session) -> PathBuf {
2823
let formats = if verbatim {
2924
vec![("".into(), "".into())]
3025
} else {
@@ -35,9 +30,9 @@ pub fn find_native_static_library<'a>(
3530
if os == unix { vec![os] } else { vec![os, unix] }
3631
};
3732

38-
for path in search_paths {
33+
for path in sess.target_filesearch(PathKind::Native).search_paths() {
3934
for (prefix, suffix) in &formats {
40-
let test = path.join(format!("{prefix}{name}{suffix}"));
35+
let test = path.dir.join(format!("{prefix}{name}{suffix}"));
4136
if test.exists() {
4237
return test;
4338
}
@@ -60,8 +55,7 @@ fn find_bundled_library(
6055
&& (sess.opts.unstable_opts.packed_bundled_libs || has_cfg || whole_archive == Some(true))
6156
{
6257
let verbatim = verbatim.unwrap_or(false);
63-
let search_paths = sess.target_filesearch(PathKind::Native).search_path_dirs();
64-
return find_native_static_library(name.as_str(), verbatim, search_paths, sess)
58+
return find_native_static_library(name.as_str(), verbatim, sess)
6559
.file_name()
6660
.and_then(|s| s.to_str())
6761
.map(Symbol::intern);

compiler/rustc_session/src/filesearch.rs

-5
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ impl<'a> FileSearch<'a> {
4545
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
4646
FileSearch { sysroot, triple, search_paths, tlib_path, kind }
4747
}
48-
49-
/// Returns just the directories within the search paths.
50-
pub fn search_path_dirs(&self) -> impl Iterator<Item = &'a Path> {
51-
self.search_paths().map(|sp| &*sp.dir)
52-
}
5348
}
5449

5550
pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {

0 commit comments

Comments
 (0)