Skip to content

Commit 0ae891e

Browse files
committed
linker: Do not collect search paths unless necessary
1 parent d7712c6 commit 0ae891e

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ use std::path::{Path, PathBuf};
5252
use std::process::{ExitStatus, Output, Stdio};
5353
use std::{env, fmt, fs, io, mem, str};
5454

55+
#[derive(Default)]
56+
pub struct SearchPaths(OnceCell<Vec<PathBuf>>);
57+
58+
impl SearchPaths {
59+
pub(super) fn get(&self, sess: &Session) -> &[PathBuf] {
60+
self.0.get_or_init(|| archive_search_paths(sess))
61+
}
62+
}
63+
5564
pub fn ensure_removed(dcx: &DiagCtxt, path: &Path) {
5665
if let Err(e) = fs::remove_file(path) {
5766
if e.kind() != io::ErrorKind::NotFound {
@@ -2445,7 +2454,7 @@ fn add_native_libs_from_crate(
24452454
archive_builder_builder: &dyn ArchiveBuilderBuilder,
24462455
codegen_results: &CodegenResults,
24472456
tmpdir: &Path,
2448-
search_paths: &OnceCell<Vec<PathBuf>>,
2457+
search_paths: &SearchPaths,
24492458
bundled_libs: &FxHashSet<Symbol>,
24502459
cnum: CrateNum,
24512460
link_static: bool,
@@ -2513,11 +2522,7 @@ fn add_native_libs_from_crate(
25132522
}
25142523
} else {
25152524
if whole_archive {
2516-
cmd.link_whole_staticlib_by_name(
2517-
name,
2518-
verbatim,
2519-
search_paths.get_or_init(|| archive_search_paths(sess)),
2520-
);
2525+
cmd.link_whole_staticlib_by_name(name, verbatim, search_paths);
25212526
} else {
25222527
cmd.link_staticlib_by_name(name, verbatim)
25232528
}
@@ -2581,7 +2586,7 @@ fn add_local_native_libraries(
25812586
}
25822587
}
25832588

2584-
let search_paths = OnceCell::new();
2589+
let search_paths = SearchPaths::default();
25852590
// All static and dynamic native library dependencies are linked to the local crate.
25862591
let link_static = true;
25872592
let link_dynamic = true;
@@ -2623,7 +2628,7 @@ fn add_upstream_rust_crates<'a>(
26232628
.find(|(ty, _)| *ty == crate_type)
26242629
.expect("failed to find crate type in dependency format list");
26252630

2626-
let search_paths = OnceCell::new();
2631+
let search_paths = SearchPaths::default();
26272632
for &cnum in &codegen_results.crate_info.used_crates {
26282633
// We may not pass all crates through to the linker. Some crates may appear statically in
26292634
// an existing dylib, meaning we'll pick up all the symbols from the dylib.
@@ -2698,7 +2703,7 @@ fn add_upstream_native_libraries(
26982703
tmpdir: &Path,
26992704
link_output_kind: LinkOutputKind,
27002705
) {
2701-
let search_path = OnceCell::new();
2706+
let search_paths = SearchPaths::default();
27022707
for &cnum in &codegen_results.crate_info.used_crates {
27032708
// Static libraries are not linked here, they are linked in `add_upstream_rust_crates`.
27042709
// FIXME: Merge this function to `add_upstream_rust_crates` so that all native libraries
@@ -2720,7 +2725,7 @@ fn add_upstream_native_libraries(
27202725
archive_builder_builder,
27212726
codegen_results,
27222727
tmpdir,
2723-
&search_path,
2728+
&search_paths,
27242729
&Default::default(),
27252730
cnum,
27262731
link_static,

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::command::Command;
22
use super::symbol_export;
3+
use crate::back::link::SearchPaths;
34
use crate::errors;
45
use rustc_span::symbol::sym;
56

@@ -175,7 +176,7 @@ pub trait Linker {
175176
&mut self,
176177
name: &str,
177178
verbatim: bool,
178-
search_paths: &[PathBuf],
179+
search_paths: &SearchPaths,
179180
);
180181
fn link_staticlib_by_path(&mut self, path: &Path);
181182
fn link_whole_staticlib_by_path(&mut self, path: &Path);
@@ -496,7 +497,7 @@ impl<'a> Linker for GccLinker<'a> {
496497
&mut self,
497498
name: &str,
498499
verbatim: bool,
499-
search_paths: &[PathBuf],
500+
search_paths: &SearchPaths,
500501
) {
501502
self.hint_static();
502503
let target = &self.sess.target;
@@ -508,7 +509,8 @@ impl<'a> Linker for GccLinker<'a> {
508509
// -force_load is the macOS equivalent of --whole-archive, but it
509510
// involves passing the full path to the library to link.
510511
self.linker_arg("-force_load");
511-
let lib = find_native_static_library(name, verbatim, search_paths, self.sess);
512+
let lib =
513+
find_native_static_library(name, verbatim, search_paths.get(self.sess), self.sess);
512514
self.linker_arg(&lib);
513515
}
514516
}
@@ -842,7 +844,7 @@ impl<'a> Linker for MsvcLinker<'a> {
842844
&mut self,
843845
name: &str,
844846
verbatim: bool,
845-
_search_paths: &[PathBuf],
847+
_search_paths: &SearchPaths,
846848
) {
847849
self.cmd.arg(format!("/WHOLEARCHIVE:{}{}", name, if verbatim { "" } else { ".lib" }));
848850
}
@@ -1073,7 +1075,7 @@ impl<'a> Linker for EmLinker<'a> {
10731075
&mut self,
10741076
name: &str,
10751077
verbatim: bool,
1076-
_search_paths: &[PathBuf],
1078+
_search_paths: &SearchPaths,
10771079
) {
10781080
// not supported?
10791081
self.link_staticlib_by_name(name, verbatim);
@@ -1261,7 +1263,7 @@ impl<'a> Linker for WasmLd<'a> {
12611263
&mut self,
12621264
name: &str,
12631265
_verbatim: bool,
1264-
_search_paths: &[PathBuf],
1266+
_search_paths: &SearchPaths,
12651267
) {
12661268
self.cmd.arg("--whole-archive").arg("-l").arg(name).arg("--no-whole-archive");
12671269
}
@@ -1414,7 +1416,7 @@ impl<'a> Linker for L4Bender<'a> {
14141416
&mut self,
14151417
name: &str,
14161418
_verbatim: bool,
1417-
_search_paths: &[PathBuf],
1419+
_search_paths: &SearchPaths,
14181420
) {
14191421
self.hint_static();
14201422
self.cmd.arg("--whole-archive").arg(format!("-l{name}"));
@@ -1600,10 +1602,11 @@ impl<'a> Linker for AixLinker<'a> {
16001602
&mut self,
16011603
name: &str,
16021604
verbatim: bool,
1603-
search_paths: &[PathBuf],
1605+
search_paths: &SearchPaths,
16041606
) {
16051607
self.hint_static();
1606-
let lib = find_native_static_library(name, verbatim, search_paths, self.sess);
1608+
let lib =
1609+
find_native_static_library(name, verbatim, search_paths.get(self.sess), self.sess);
16071610
self.cmd.arg(format!("-bkeepfile:{}", lib.to_str().unwrap()));
16081611
}
16091612

@@ -1815,7 +1818,7 @@ impl<'a> Linker for PtxLinker<'a> {
18151818
&mut self,
18161819
_name: &str,
18171820
_verbatim: bool,
1818-
_search_paths: &[PathBuf],
1821+
_search_paths: &SearchPaths,
18191822
) {
18201823
panic!("staticlibs not supported")
18211824
}
@@ -1909,7 +1912,7 @@ impl<'a> Linker for BpfLinker<'a> {
19091912
&mut self,
19101913
_name: &str,
19111914
_verbatim: bool,
1912-
_search_paths: &[PathBuf],
1915+
_search_paths: &SearchPaths,
19131916
) {
19141917
panic!("staticlibs not supported")
19151918
}

0 commit comments

Comments
 (0)