Skip to content

Commit

Permalink
linker: Merge link_staticlib_* and link_whole_staticlib_*
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jan 22, 2024
1 parent 0ae891e commit e77af0f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 130 deletions.
18 changes: 5 additions & 13 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ fn link_sanitizer_runtime(
} else {
let filename = format!("librustc{channel}_rt.{name}.a");
let path = find_sanitizer_runtime(sess, &filename).join(&filename);
linker.link_whole_staticlib_by_path(&path);
linker.link_staticlib_by_path(&path, true);
}
}

Expand Down Expand Up @@ -2514,18 +2514,10 @@ fn add_native_libs_from_crate(
if let Some(filename) = lib.filename {
// If rlib contains native libs as archives, they are unpacked to tmpdir.
let path = tmpdir.join(filename.as_str());
if whole_archive {
cmd.link_whole_staticlib_by_path(&path);
} else {
cmd.link_staticlib_by_path(&path);
}
cmd.link_staticlib_by_path(&path, whole_archive);
}
} else {
if whole_archive {
cmd.link_whole_staticlib_by_name(name, verbatim, search_paths);
} else {
cmd.link_staticlib_by_name(name, verbatim)
}
cmd.link_staticlib_by_name(name, verbatim, whole_archive, search_paths);
}
}
}
Expand All @@ -2539,7 +2531,7 @@ fn add_native_libs_from_crate(
// link kind is unspecified.
if !link_output_kind.can_link_dylib() && !sess.target.crt_static_allows_dylibs {
if link_static {
cmd.link_staticlib_by_name(name, verbatim)
cmd.link_staticlib_by_name(name, verbatim, false, search_paths);
}
} else {
if link_dynamic {
Expand Down Expand Up @@ -2796,7 +2788,7 @@ fn add_static_crate<'a>(
} else {
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)
Expand Down
198 changes: 81 additions & 117 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,14 @@ pub trait Linker {
fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
bug!("framework linked with unsupported linker")
}
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool);
fn link_whole_staticlib_by_name(
fn link_staticlib_by_name(
&mut self,
name: &str,
verbatim: bool,
whole_archive: bool,
search_paths: &SearchPaths,
);
fn link_staticlib_by_path(&mut self, path: &Path);
fn link_whole_staticlib_by_path(&mut self, path: &Path);
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool);
fn include_path(&mut self, path: &Path);
fn framework_path(&mut self, path: &Path);
fn output_filename(&mut self, path: &Path);
Expand Down Expand Up @@ -482,26 +481,17 @@ impl<'a> Linker for GccLinker<'a> {
self.cmd.arg("-framework").arg(name);
}

fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool) {
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(
fn link_staticlib_by_name(
&mut self,
name: &str,
verbatim: bool,
whole_archive: bool,
search_paths: &SearchPaths,
) {
self.hint_static();
let target = &self.sess.target;
if !target.is_like_osx {
if !whole_archive {
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.cmd.arg(format!("-l{}{name}", if verbatim && self.is_gnu { ":" } else { "" }));
self.linker_arg("--no-whole-archive");
Expand All @@ -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.cmd.arg(path);
}

fn link_whole_staticlib_by_path(&mut self, path: &Path) {
self.hint_static();
if self.sess.target.is_like_osx {
if !whole_archive {
self.cmd.arg(path);
} else if self.sess.target.is_like_osx {
self.linker_arg("-force_load");
self.linker_arg(&path);
} else {
Expand Down Expand Up @@ -836,27 +823,28 @@ impl<'a> Linker for MsvcLinker<'a> {
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
}

fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool) {
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
}

fn link_whole_staticlib_by_name(
fn link_staticlib_by_name(
&mut self,
name: &str,
verbatim: bool,
whole_archive: bool,
_search_paths: &SearchPaths,
) {
self.cmd.arg(format!("/WHOLEARCHIVE:{}{}", name, if verbatim { "" } else { ".lib" }));
}

fn link_staticlib_by_path(&mut self, path: &Path) {
self.cmd.arg(path);
if !whole_archive {
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
} else {
self.cmd.arg(format!("/WHOLEARCHIVE:{}{}", name, if verbatim { "" } else { ".lib" }));
}
}

fn link_whole_staticlib_by_path(&mut self, path: &Path) {
let mut arg = OsString::from("/WHOLEARCHIVE:");
arg.push(path);
self.cmd.arg(arg);
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:");
arg.push(path);
self.cmd.arg(arg);
}
}

fn add_object(&mut self, path: &Path) {
Expand Down Expand Up @@ -1062,34 +1050,25 @@ impl<'a> Linker for EmLinker<'a> {

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
self.link_staticlib_by_name(name, verbatim);
}

fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) {
self.cmd.arg("-l").arg(name);
}

fn link_whole_staticlib_by_name(
fn link_staticlib_by_name(
&mut self,
name: &str,
verbatim: bool,
_verbatim: bool,
_whole_archive: bool,
_search_paths: &SearchPaths,
) {
// not supported?
self.link_staticlib_by_name(name, verbatim);
self.cmd.arg("-l").arg(name);
}

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);
}

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) {
self.cmd.arg("-L").arg(path);
}
Expand Down Expand Up @@ -1255,25 +1234,26 @@ impl<'a> Linker for WasmLd<'a> {
self.cmd.arg("-l").arg(name);
}

fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) {
self.cmd.arg("-l").arg(name);
}

fn link_whole_staticlib_by_name(
fn link_staticlib_by_name(
&mut self,
name: &str,
_verbatim: bool,
whole_archive: bool,
_search_paths: &SearchPaths,
) {
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);
if !whole_archive {
self.cmd.arg("-l").arg(name);
} else {
self.cmd.arg("--whole-archive").arg("-l").arg(name).arg("--no-whole-archive");
}
}

fn link_whole_staticlib_by_path(&mut self, path: &Path) {
self.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive");
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");
}
}

fn include_path(&mut self, path: &Path) {
Expand Down Expand Up @@ -1407,30 +1387,29 @@ impl<'a> Linker for L4Bender<'a> {
bug!("dylibs are not supported on L4Re");
}

fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) {
self.hint_static();
self.cmd.arg(format!("-PC{name}"));
}

fn link_whole_staticlib_by_name(
fn link_staticlib_by_name(
&mut self,
name: &str,
_verbatim: bool,
whole_archive: bool,
_search_paths: &SearchPaths,
) {
self.hint_static();
self.cmd.arg("--whole-archive").arg(format!("-l{name}"));
self.cmd.arg("--no-whole-archive");
}

fn link_staticlib_by_path(&mut self, path: &Path) {
self.hint_static();
self.cmd.arg(path);
if !whole_archive {
self.cmd.arg(format!("-PC{name}"));
} else {
self.cmd.arg("--whole-archive").arg(format!("-l{name}"));
self.cmd.arg("--no-whole-archive");
}
}

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.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive");
if !whole_archive {
self.cmd.arg(path);
} else {
self.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive");
}
}

fn include_path(&mut self, path: &Path) {
Expand Down Expand Up @@ -1593,31 +1572,30 @@ impl<'a> Linker for AixLinker<'a> {
self.cmd.arg(format!("-l{name}"));
}

fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) {
self.hint_static();
self.cmd.arg(format!("-l{name}"));
}

fn link_whole_staticlib_by_name(
fn link_staticlib_by_name(
&mut self,
name: &str,
verbatim: bool,
whole_archive: bool,
search_paths: &SearchPaths,
) {
self.hint_static();
let lib =
find_native_static_library(name, verbatim, search_paths.get(self.sess), self.sess);
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);
if !whole_archive {
self.cmd.arg(format!("-l{name}"));
} else {
let lib =
find_native_static_library(name, verbatim, search_paths.get(self.sess), self.sess);
self.cmd.arg(format!("-bkeepfile:{}", lib.to_str().unwrap()));
}
}

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.cmd.arg(format!("-bkeepfile:{}", path.to_str().unwrap()));
if !whole_archive {
self.cmd.arg(path);
} else {
self.cmd.arg(format!("-bkeepfile:{}", path.to_str().unwrap()));
}
}

fn include_path(&mut self, path: &Path) {
Expand Down Expand Up @@ -1810,24 +1788,17 @@ impl<'a> Linker for PtxLinker<'a> {
panic!("external dylibs not supported")
}

fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool) {
panic!("staticlibs not supported")
}

fn link_whole_staticlib_by_name(
fn link_staticlib_by_name(
&mut self,
_name: &str,
_verbatim: bool,
_whole_archive: bool,
_search_paths: &SearchPaths,
) {
panic!("staticlibs not supported")
}

fn link_staticlib_by_path(&mut self, path: &Path) {
self.cmd.arg("--rlib").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.cmd.arg("--rlib").arg(path);
}

Expand Down Expand Up @@ -1904,24 +1875,17 @@ impl<'a> Linker for BpfLinker<'a> {
panic!("external dylibs not supported")
}

fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool) {
panic!("staticlibs not supported")
}

fn link_whole_staticlib_by_name(
fn link_staticlib_by_name(
&mut self,
_name: &str,
_verbatim: bool,
_whole_archive: bool,
_search_paths: &SearchPaths,
) {
panic!("staticlibs not supported")
}

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) {
self.cmd.arg(path);
}

Expand Down

0 comments on commit e77af0f

Please sign in to comment.