mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 01:04:03 +00:00
linker: Do not collect search paths unless necessary
This commit is contained in:
parent
14cd3fd6f9
commit
859f37ae86
@ -52,6 +52,15 @@ use std::path::{Path, PathBuf};
|
||||
use std::process::{ExitStatus, Output, Stdio};
|
||||
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) {
|
||||
if let Err(e) = fs::remove_file(path) {
|
||||
if e.kind() != io::ErrorKind::NotFound {
|
||||
@ -2445,7 +2454,7 @@ fn add_native_libs_from_crate(
|
||||
archive_builder_builder: &dyn ArchiveBuilderBuilder,
|
||||
codegen_results: &CodegenResults,
|
||||
tmpdir: &Path,
|
||||
search_paths: &OnceCell<Vec<PathBuf>>,
|
||||
search_paths: &SearchPaths,
|
||||
bundled_libs: &FxHashSet<Symbol>,
|
||||
cnum: CrateNum,
|
||||
link_static: bool,
|
||||
@ -2513,11 +2522,7 @@ fn add_native_libs_from_crate(
|
||||
}
|
||||
} else {
|
||||
if whole_archive {
|
||||
cmd.link_whole_staticlib_by_name(
|
||||
name,
|
||||
verbatim,
|
||||
search_paths.get_or_init(|| archive_search_paths(sess)),
|
||||
);
|
||||
cmd.link_whole_staticlib_by_name(name, verbatim, search_paths);
|
||||
} else {
|
||||
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.
|
||||
let link_static = true;
|
||||
let link_dynamic = true;
|
||||
@ -2623,7 +2628,7 @@ fn add_upstream_rust_crates<'a>(
|
||||
.find(|(ty, _)| *ty == crate_type)
|
||||
.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 {
|
||||
// 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.
|
||||
@ -2698,7 +2703,7 @@ fn add_upstream_native_libraries(
|
||||
tmpdir: &Path,
|
||||
link_output_kind: LinkOutputKind,
|
||||
) {
|
||||
let search_path = OnceCell::new();
|
||||
let search_paths = SearchPaths::default();
|
||||
for &cnum in &codegen_results.crate_info.used_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
|
||||
@ -2720,7 +2725,7 @@ fn add_upstream_native_libraries(
|
||||
archive_builder_builder,
|
||||
codegen_results,
|
||||
tmpdir,
|
||||
&search_path,
|
||||
&search_paths,
|
||||
&Default::default(),
|
||||
cnum,
|
||||
link_static,
|
||||
|
@ -1,5 +1,6 @@
|
||||
use super::command::Command;
|
||||
use super::symbol_export;
|
||||
use crate::back::link::SearchPaths;
|
||||
use crate::errors;
|
||||
use rustc_span::symbol::sym;
|
||||
|
||||
@ -175,7 +176,7 @@ pub trait Linker {
|
||||
&mut self,
|
||||
name: &str,
|
||||
verbatim: bool,
|
||||
search_paths: &[PathBuf],
|
||||
search_paths: &SearchPaths,
|
||||
);
|
||||
fn link_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,
|
||||
name: &str,
|
||||
verbatim: bool,
|
||||
search_paths: &[PathBuf],
|
||||
search_paths: &SearchPaths,
|
||||
) {
|
||||
self.hint_static();
|
||||
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
|
||||
// involves passing the full path to the library to link.
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -842,7 +844,7 @@ impl<'a> Linker for MsvcLinker<'a> {
|
||||
&mut self,
|
||||
name: &str,
|
||||
verbatim: bool,
|
||||
_search_paths: &[PathBuf],
|
||||
_search_paths: &SearchPaths,
|
||||
) {
|
||||
self.cmd.arg(format!("/WHOLEARCHIVE:{}{}", name, if verbatim { "" } else { ".lib" }));
|
||||
}
|
||||
@ -1073,7 +1075,7 @@ impl<'a> Linker for EmLinker<'a> {
|
||||
&mut self,
|
||||
name: &str,
|
||||
verbatim: bool,
|
||||
_search_paths: &[PathBuf],
|
||||
_search_paths: &SearchPaths,
|
||||
) {
|
||||
// not supported?
|
||||
self.link_staticlib_by_name(name, verbatim);
|
||||
@ -1261,7 +1263,7 @@ impl<'a> Linker for WasmLd<'a> {
|
||||
&mut self,
|
||||
name: &str,
|
||||
_verbatim: bool,
|
||||
_search_paths: &[PathBuf],
|
||||
_search_paths: &SearchPaths,
|
||||
) {
|
||||
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,
|
||||
name: &str,
|
||||
_verbatim: bool,
|
||||
_search_paths: &[PathBuf],
|
||||
_search_paths: &SearchPaths,
|
||||
) {
|
||||
self.hint_static();
|
||||
self.cmd.arg("--whole-archive").arg(format!("-l{name}"));
|
||||
@ -1600,10 +1602,11 @@ impl<'a> Linker for AixLinker<'a> {
|
||||
&mut self,
|
||||
name: &str,
|
||||
verbatim: bool,
|
||||
search_paths: &[PathBuf],
|
||||
search_paths: &SearchPaths,
|
||||
) {
|
||||
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()));
|
||||
}
|
||||
|
||||
@ -1815,7 +1818,7 @@ impl<'a> Linker for PtxLinker<'a> {
|
||||
&mut self,
|
||||
_name: &str,
|
||||
_verbatim: bool,
|
||||
_search_paths: &[PathBuf],
|
||||
_search_paths: &SearchPaths,
|
||||
) {
|
||||
panic!("staticlibs not supported")
|
||||
}
|
||||
@ -1909,7 +1912,7 @@ impl<'a> Linker for BpfLinker<'a> {
|
||||
&mut self,
|
||||
_name: &str,
|
||||
_verbatim: bool,
|
||||
_search_paths: &[PathBuf],
|
||||
_search_paths: &SearchPaths,
|
||||
) {
|
||||
panic!("staticlibs not supported")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user