1
Fork 0

linker: Do not collect search paths unless necessary

This commit is contained in:
Vadim Petrochenkov 2024-01-18 16:13:13 +03:00
parent 14cd3fd6f9
commit 859f37ae86
2 changed files with 29 additions and 21 deletions

View file

@ -52,6 +52,15 @@ use std::path::{Path, PathBuf};
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) -> &[PathBuf] {
self.0.get_or_init(|| archive_search_paths(sess))
}
}
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 {
@ -2445,7 +2454,7 @@ 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: &OnceCell<Vec<PathBuf>>, search_paths: &SearchPaths,
bundled_libs: &FxHashSet<Symbol>, bundled_libs: &FxHashSet<Symbol>,
cnum: CrateNum, cnum: CrateNum,
link_static: bool, link_static: bool,
@ -2513,11 +2522,7 @@ fn add_native_libs_from_crate(
} }
} else { } else {
if whole_archive { if whole_archive {
cmd.link_whole_staticlib_by_name( cmd.link_whole_staticlib_by_name(name, verbatim, search_paths);
name,
verbatim,
search_paths.get_or_init(|| archive_search_paths(sess)),
);
} else { } else {
cmd.link_staticlib_by_name(name, verbatim) cmd.link_staticlib_by_name(name, verbatim)
} }
@ -2581,7 +2586,7 @@ fn add_local_native_libraries(
} }
} }
let search_paths = OnceCell::new(); 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;
@ -2623,7 +2628,7 @@ fn add_upstream_rust_crates<'a>(
.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 = OnceCell::new(); 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.
@ -2698,7 +2703,7 @@ fn add_upstream_native_libraries(
tmpdir: &Path, tmpdir: &Path,
link_output_kind: LinkOutputKind, link_output_kind: LinkOutputKind,
) { ) {
let search_path = OnceCell::new(); 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
@ -2720,7 +2725,7 @@ fn add_upstream_native_libraries(
archive_builder_builder, archive_builder_builder,
codegen_results, codegen_results,
tmpdir, tmpdir,
&search_path, &search_paths,
&Default::default(), &Default::default(),
cnum, cnum,
link_static, link_static,

View file

@ -1,5 +1,6 @@
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;
@ -175,7 +176,7 @@ pub trait Linker {
&mut self, &mut self,
name: &str, name: &str,
verbatim: bool, verbatim: bool,
search_paths: &[PathBuf], search_paths: &SearchPaths,
); );
fn link_staticlib_by_path(&mut self, path: &Path); fn link_staticlib_by_path(&mut self, path: &Path);
fn link_whole_staticlib_by_path(&mut self, path: &Path); fn link_whole_staticlib_by_path(&mut self, path: &Path);
@ -496,7 +497,7 @@ impl<'a> Linker for GccLinker<'a> {
&mut self, &mut self,
name: &str, name: &str,
verbatim: bool, verbatim: bool,
search_paths: &[PathBuf], search_paths: &SearchPaths,
) { ) {
self.hint_static(); self.hint_static();
let target = &self.sess.target; let target = &self.sess.target;
@ -508,7 +509,8 @@ impl<'a> Linker for GccLinker<'a> {
// -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 lib = find_native_static_library(name, verbatim, search_paths, self.sess); let lib =
find_native_static_library(name, verbatim, search_paths.get(self.sess), self.sess);
self.linker_arg(&lib); self.linker_arg(&lib);
} }
} }
@ -842,7 +844,7 @@ impl<'a> Linker for MsvcLinker<'a> {
&mut self, &mut self,
name: &str, name: &str,
verbatim: bool, verbatim: bool,
_search_paths: &[PathBuf], _search_paths: &SearchPaths,
) { ) {
self.cmd.arg(format!("/WHOLEARCHIVE:{}{}", name, if verbatim { "" } else { ".lib" })); self.cmd.arg(format!("/WHOLEARCHIVE:{}{}", name, if verbatim { "" } else { ".lib" }));
} }
@ -1073,7 +1075,7 @@ impl<'a> Linker for EmLinker<'a> {
&mut self, &mut self,
name: &str, name: &str,
verbatim: bool, verbatim: bool,
_search_paths: &[PathBuf], _search_paths: &SearchPaths,
) { ) {
// not supported? // not supported?
self.link_staticlib_by_name(name, verbatim); self.link_staticlib_by_name(name, verbatim);
@ -1261,7 +1263,7 @@ impl<'a> Linker for WasmLd<'a> {
&mut self, &mut self,
name: &str, name: &str,
_verbatim: bool, _verbatim: bool,
_search_paths: &[PathBuf], _search_paths: &SearchPaths,
) { ) {
self.cmd.arg("--whole-archive").arg("-l").arg(name).arg("--no-whole-archive"); self.cmd.arg("--whole-archive").arg("-l").arg(name).arg("--no-whole-archive");
} }
@ -1414,7 +1416,7 @@ impl<'a> Linker for L4Bender<'a> {
&mut self, &mut self,
name: &str, name: &str,
_verbatim: bool, _verbatim: bool,
_search_paths: &[PathBuf], _search_paths: &SearchPaths,
) { ) {
self.hint_static(); self.hint_static();
self.cmd.arg("--whole-archive").arg(format!("-l{name}")); self.cmd.arg("--whole-archive").arg(format!("-l{name}"));
@ -1600,10 +1602,11 @@ impl<'a> Linker for AixLinker<'a> {
&mut self, &mut self,
name: &str, name: &str,
verbatim: bool, verbatim: bool,
search_paths: &[PathBuf], search_paths: &SearchPaths,
) { ) {
self.hint_static(); self.hint_static();
let lib = find_native_static_library(name, verbatim, search_paths, self.sess); let lib =
find_native_static_library(name, verbatim, search_paths.get(self.sess), self.sess);
self.cmd.arg(format!("-bkeepfile:{}", lib.to_str().unwrap())); self.cmd.arg(format!("-bkeepfile:{}", lib.to_str().unwrap()));
} }
@ -1815,7 +1818,7 @@ impl<'a> Linker for PtxLinker<'a> {
&mut self, &mut self,
_name: &str, _name: &str,
_verbatim: bool, _verbatim: bool,
_search_paths: &[PathBuf], _search_paths: &SearchPaths,
) { ) {
panic!("staticlibs not supported") panic!("staticlibs not supported")
} }
@ -1909,7 +1912,7 @@ impl<'a> Linker for BpfLinker<'a> {
&mut self, &mut self,
_name: &str, _name: &str,
_verbatim: bool, _verbatim: bool,
_search_paths: &[PathBuf], _search_paths: &SearchPaths,
) { ) {
panic!("staticlibs not supported") panic!("staticlibs not supported")
} }