linker: Merge link_staticlib_* and link_whole_staticlib_*

This commit is contained in:
Vadim Petrochenkov 2024-01-18 17:41:18 +03:00
parent 859f37ae86
commit d15db6b260
2 changed files with 86 additions and 130 deletions

View File

@ -1282,7 +1282,7 @@ fn link_sanitizer_runtime(
} else { } else {
let filename = format!("librustc{channel}_rt.{name}.a"); let filename = format!("librustc{channel}_rt.{name}.a");
let path = find_sanitizer_runtime(sess, &filename).join(&filename); let path = find_sanitizer_runtime(sess, &filename).join(&filename);
linker.link_whole_staticlib_by_path(&path); linker.link_staticlib_by_path(&path, true);
} }
} }
@ -2514,18 +2514,10 @@ fn add_native_libs_from_crate(
if let Some(filename) = lib.filename { if let Some(filename) = lib.filename {
// If rlib contains native libs as archives, they are unpacked to tmpdir. // If rlib contains native libs as archives, they are unpacked to tmpdir.
let path = tmpdir.join(filename.as_str()); let path = tmpdir.join(filename.as_str());
if whole_archive { cmd.link_staticlib_by_path(&path, whole_archive);
cmd.link_whole_staticlib_by_path(&path);
} else {
cmd.link_staticlib_by_path(&path);
}
} }
} else { } else {
if whole_archive { cmd.link_staticlib_by_name(name, verbatim, whole_archive, search_paths);
cmd.link_whole_staticlib_by_name(name, verbatim, search_paths);
} else {
cmd.link_staticlib_by_name(name, verbatim)
}
} }
} }
} }
@ -2539,7 +2531,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) cmd.link_staticlib_by_name(name, verbatim, false, search_paths);
} }
} else { } else {
if link_dynamic { if link_dynamic {
@ -2796,7 +2788,7 @@ fn add_static_crate<'a>(
} else { } else {
fix_windows_verbatim_for_gcc(path) fix_windows_verbatim_for_gcc(path)
}; };
cmd.link_staticlib_by_path(&rlib_path); cmd.link_staticlib_by_path(&rlib_path, false);
}; };
if !are_upstream_rust_objects_already_included(sess) if !are_upstream_rust_objects_already_included(sess)

View File

@ -171,15 +171,14 @@ 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(&mut self, name: &str, verbatim: bool); fn link_staticlib_by_name(
fn link_whole_staticlib_by_name(
&mut self, &mut self,
name: &str, name: &str,
verbatim: bool, verbatim: bool,
whole_archive: bool,
search_paths: &SearchPaths, search_paths: &SearchPaths,
); );
fn link_staticlib_by_path(&mut self, path: &Path); fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool);
fn link_whole_staticlib_by_path(&mut self, path: &Path);
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);
fn output_filename(&mut self, path: &Path); fn output_filename(&mut self, path: &Path);
@ -482,26 +481,17 @@ impl<'a> Linker for GccLinker<'a> {
self.cmd.arg("-framework").arg(name); self.cmd.arg("-framework").arg(name);
} }
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool) { fn link_staticlib_by_name(
self.hint_static();
self.cmd.arg(format!("-l{}{name}", if verbatim && self.is_gnu { ":" } else { "" },));
}
// Here we explicitly ask that the entire archive is included into the
// result artifact. For more details see #15460, but the gist is that
// the linker will strip away any unused objects in the archive if we
// don't otherwise explicitly reference them. This can occur for
// libraries which are just providing bindings, libraries with generic
// functions, etc.
fn link_whole_staticlib_by_name(
&mut self, &mut self,
name: &str, name: &str,
verbatim: bool, verbatim: bool,
whole_archive: bool,
search_paths: &SearchPaths, search_paths: &SearchPaths,
) { ) {
self.hint_static(); self.hint_static();
let target = &self.sess.target; if !whole_archive {
if !target.is_like_osx { self.cmd.arg(format!("-l{}{name}", if verbatim && self.is_gnu { ":" } else { "" }));
} else if !self.sess.target.is_like_osx {
self.linker_arg("--whole-archive"); self.linker_arg("--whole-archive");
self.cmd.arg(format!("-l{}{name}", if verbatim && self.is_gnu { ":" } else { "" })); self.cmd.arg(format!("-l{}{name}", if verbatim && self.is_gnu { ":" } else { "" }));
self.linker_arg("--no-whole-archive"); self.linker_arg("--no-whole-archive");
@ -515,14 +505,11 @@ impl<'a> Linker for GccLinker<'a> {
} }
} }
fn link_staticlib_by_path(&mut self, path: &Path) { fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
self.hint_static(); self.hint_static();
if !whole_archive {
self.cmd.arg(path); self.cmd.arg(path);
} } else if self.sess.target.is_like_osx {
fn link_whole_staticlib_by_path(&mut self, path: &Path) {
self.hint_static();
if self.sess.target.is_like_osx {
self.linker_arg("-force_load"); self.linker_arg("-force_load");
self.linker_arg(&path); self.linker_arg(&path);
} else { } else {
@ -836,28 +823,29 @@ impl<'a> Linker for MsvcLinker<'a> {
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" })); self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
} }
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool) { fn link_staticlib_by_name(
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
}
fn link_whole_staticlib_by_name(
&mut self, &mut self,
name: &str, name: &str,
verbatim: bool, verbatim: bool,
whole_archive: bool,
_search_paths: &SearchPaths, _search_paths: &SearchPaths,
) { ) {
if !whole_archive {
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
} else {
self.cmd.arg(format!("/WHOLEARCHIVE:{}{}", name, if verbatim { "" } else { ".lib" })); self.cmd.arg(format!("/WHOLEARCHIVE:{}{}", name, if verbatim { "" } else { ".lib" }));
} }
fn link_staticlib_by_path(&mut self, path: &Path) {
self.cmd.arg(path);
} }
fn link_whole_staticlib_by_path(&mut self, path: &Path) { fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
if !whole_archive {
self.cmd.arg(path);
} else {
let mut arg = OsString::from("/WHOLEARCHIVE:"); let mut arg = OsString::from("/WHOLEARCHIVE:");
arg.push(path); arg.push(path);
self.cmd.arg(arg); self.cmd.arg(arg);
} }
}
fn add_object(&mut self, path: &Path) { fn add_object(&mut self, path: &Path) {
self.cmd.arg(path); self.cmd.arg(path);
@ -1062,34 +1050,25 @@ impl<'a> Linker for EmLinker<'a> {
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {} fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) { fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
// Emscripten always links statically // Emscripten always links statically
self.link_staticlib_by_name(name, verbatim);
}
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) {
self.cmd.arg("-l").arg(name); self.cmd.arg("-l").arg(name);
} }
fn link_whole_staticlib_by_name( fn link_staticlib_by_name(
&mut self, &mut self,
name: &str, name: &str,
verbatim: bool, _verbatim: bool,
_whole_archive: bool,
_search_paths: &SearchPaths, _search_paths: &SearchPaths,
) { ) {
// not supported? self.cmd.arg("-l").arg(name);
self.link_staticlib_by_name(name, verbatim);
} }
fn link_staticlib_by_path(&mut self, path: &Path) { fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
self.add_object(path); self.add_object(path);
} }
fn link_whole_staticlib_by_path(&mut self, path: &Path) {
// not supported?
self.link_staticlib_by_path(path);
}
fn include_path(&mut self, path: &Path) { fn include_path(&mut self, path: &Path) {
self.cmd.arg("-L").arg(path); self.cmd.arg("-L").arg(path);
} }
@ -1255,26 +1234,27 @@ impl<'a> Linker for WasmLd<'a> {
self.cmd.arg("-l").arg(name); self.cmd.arg("-l").arg(name);
} }
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) { fn link_staticlib_by_name(
self.cmd.arg("-l").arg(name);
}
fn link_whole_staticlib_by_name(
&mut self, &mut self,
name: &str, name: &str,
_verbatim: bool, _verbatim: bool,
whole_archive: bool,
_search_paths: &SearchPaths, _search_paths: &SearchPaths,
) { ) {
if !whole_archive {
self.cmd.arg("-l").arg(name);
} else {
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");
} }
fn link_staticlib_by_path(&mut self, path: &Path) {
self.cmd.arg(path);
} }
fn link_whole_staticlib_by_path(&mut self, path: &Path) { fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
if !whole_archive {
self.cmd.arg(path);
} else {
self.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive"); self.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive");
} }
}
fn include_path(&mut self, path: &Path) { fn include_path(&mut self, path: &Path) {
self.cmd.arg("-L").arg(path); self.cmd.arg("-L").arg(path);
@ -1407,31 +1387,30 @@ impl<'a> Linker for L4Bender<'a> {
bug!("dylibs are not supported on L4Re"); bug!("dylibs are not supported on L4Re");
} }
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) { fn link_staticlib_by_name(
self.hint_static();
self.cmd.arg(format!("-PC{name}"));
}
fn link_whole_staticlib_by_name(
&mut self, &mut self,
name: &str, name: &str,
_verbatim: bool, _verbatim: bool,
whole_archive: bool,
_search_paths: &SearchPaths, _search_paths: &SearchPaths,
) { ) {
self.hint_static(); self.hint_static();
if !whole_archive {
self.cmd.arg(format!("-PC{name}"));
} else {
self.cmd.arg("--whole-archive").arg(format!("-l{name}")); self.cmd.arg("--whole-archive").arg(format!("-l{name}"));
self.cmd.arg("--no-whole-archive"); self.cmd.arg("--no-whole-archive");
} }
fn link_staticlib_by_path(&mut self, path: &Path) {
self.hint_static();
self.cmd.arg(path);
} }
fn link_whole_staticlib_by_path(&mut self, path: &Path) { fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
self.hint_static(); self.hint_static();
if !whole_archive {
self.cmd.arg(path);
} else {
self.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive"); self.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive");
} }
}
fn include_path(&mut self, path: &Path) { fn include_path(&mut self, path: &Path) {
self.cmd.arg("-L").arg(path); self.cmd.arg("-L").arg(path);
@ -1593,32 +1572,31 @@ impl<'a> Linker for AixLinker<'a> {
self.cmd.arg(format!("-l{name}")); self.cmd.arg(format!("-l{name}"));
} }
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) { fn link_staticlib_by_name(
self.hint_static();
self.cmd.arg(format!("-l{name}"));
}
fn link_whole_staticlib_by_name(
&mut self, &mut self,
name: &str, name: &str,
verbatim: bool, verbatim: bool,
whole_archive: bool,
search_paths: &SearchPaths, search_paths: &SearchPaths,
) { ) {
self.hint_static(); self.hint_static();
if !whole_archive {
self.cmd.arg(format!("-l{name}"));
} else {
let lib = let lib =
find_native_static_library(name, verbatim, search_paths.get(self.sess), self.sess); 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()));
} }
fn link_staticlib_by_path(&mut self, path: &Path) {
self.hint_static();
self.cmd.arg(path);
} }
fn link_whole_staticlib_by_path(&mut self, path: &Path) { fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
self.hint_static(); self.hint_static();
if !whole_archive {
self.cmd.arg(path);
} else {
self.cmd.arg(format!("-bkeepfile:{}", path.to_str().unwrap())); self.cmd.arg(format!("-bkeepfile:{}", path.to_str().unwrap()));
} }
}
fn include_path(&mut self, path: &Path) { fn include_path(&mut self, path: &Path) {
self.cmd.arg("-L").arg(path); self.cmd.arg("-L").arg(path);
@ -1810,24 +1788,17 @@ impl<'a> Linker for PtxLinker<'a> {
panic!("external dylibs not supported") panic!("external dylibs not supported")
} }
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool) { fn link_staticlib_by_name(
panic!("staticlibs not supported")
}
fn link_whole_staticlib_by_name(
&mut self, &mut self,
_name: &str, _name: &str,
_verbatim: bool, _verbatim: bool,
_whole_archive: bool,
_search_paths: &SearchPaths, _search_paths: &SearchPaths,
) { ) {
panic!("staticlibs not supported") panic!("staticlibs not supported")
} }
fn link_staticlib_by_path(&mut self, path: &Path) { fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
self.cmd.arg("--rlib").arg(path);
}
fn link_whole_staticlib_by_path(&mut self, path: &Path) {
self.cmd.arg("--rlib").arg(path); self.cmd.arg("--rlib").arg(path);
} }
@ -1904,24 +1875,17 @@ impl<'a> Linker for BpfLinker<'a> {
panic!("external dylibs not supported") panic!("external dylibs not supported")
} }
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool) { fn link_staticlib_by_name(
panic!("staticlibs not supported")
}
fn link_whole_staticlib_by_name(
&mut self, &mut self,
_name: &str, _name: &str,
_verbatim: bool, _verbatim: bool,
_whole_archive: bool,
_search_paths: &SearchPaths, _search_paths: &SearchPaths,
) { ) {
panic!("staticlibs not supported") panic!("staticlibs not supported")
} }
fn link_staticlib_by_path(&mut self, path: &Path) { fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
self.cmd.arg(path);
}
fn link_whole_staticlib_by_path(&mut self, path: &Path) {
self.cmd.arg(path); self.cmd.arg(path);
} }