Skip to content

Commit

Permalink
linker: Add search_paths parameter to link_dylib_by_name
Browse files Browse the repository at this point in the history
This allows for implementing looking up Meson and MinGW import
libraries.

See rust-lang#122455
  • Loading branch information
amyspark committed Apr 18, 2024
1 parent 266326f commit dc13b70
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 14 deletions.
10 changes: 6 additions & 4 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,8 +1280,9 @@ fn link_sanitizer_runtime(
let filename = format!("rustc{channel}_rt.{name}");
let path = find_sanitizer_runtime(sess, &filename);
let rpath = path.to_str().expect("non-utf8 component in path");
let search_paths = SearchPaths::default();
linker.args(&["-Wl,-rpath", "-Xlinker", rpath]);
linker.link_dylib_by_name(&filename, false, true);
linker.link_dylib_by_name(&filename, false, &search_paths, true);
} else if sess.target.is_like_msvc && flavor == LinkerFlavor::Msvc(Lld::No) && name == "asan" {
// MSVC provides the `/INFERASANLIBS` argument to automatically find the
// compatible ASAN library.
Expand Down Expand Up @@ -2568,7 +2569,7 @@ fn add_native_libs_from_crate(
}
NativeLibKind::Dylib { as_needed } => {
if link_dynamic {
cmd.link_dylib_by_name(name, verbatim, as_needed.unwrap_or(true))
cmd.link_dylib_by_name(name, verbatim, search_paths, as_needed.unwrap_or(true))
}
}
NativeLibKind::Unspecified => {
Expand All @@ -2580,7 +2581,7 @@ fn add_native_libs_from_crate(
}
} else {
if link_dynamic {
cmd.link_dylib_by_name(name, verbatim, true);
cmd.link_dylib_by_name(name, verbatim, search_paths, true);
}
}
}
Expand Down Expand Up @@ -2912,7 +2913,8 @@ fn add_dynamic_crate(cmd: &mut dyn Linker, sess: &Session, cratepath: &Path) {
let stem = stem.unwrap().to_str().unwrap();
// Convert library file-stem into a cc -l argument.
let prefix = if stem.starts_with("lib") && !sess.target.is_like_windows { 3 } else { 0 };
cmd.link_dylib_by_name(&stem[prefix..], false, true);
let search_paths = SearchPaths::default();
cmd.link_dylib_by_name(&stem[prefix..], false, &search_paths, true);
}

fn relevant_lib(sess: &Session, lib: &NativeLib) -> bool {
Expand Down
74 changes: 64 additions & 10 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ pub fn get_linker<'a>(
pub trait Linker {
fn cmd(&mut self) -> &mut Command;
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,
search_paths: &SearchPaths,
as_needed: bool,
);
fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
bug!("framework linked with unsupported linker")
}
Expand Down Expand Up @@ -432,7 +438,13 @@ impl<'a> Linker for GccLinker<'a> {
}
}

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,
_search_paths: &SearchPaths,
as_needed: bool,
) {
if self.sess.target.os == "illumos" && name == "c" {
// libc will be added via late_link_args on illumos so that it will
// appear last in the library search order.
Expand Down Expand Up @@ -807,7 +819,7 @@ impl<'a> Linker for MsvcLinker<'a> {
}
}

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, _search_paths: &SearchPaths, _as_needed: bool) {
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
}

Expand Down Expand Up @@ -1051,7 +1063,13 @@ 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,
_search_paths: &SearchPaths,
_as_needed: bool,
) {
// Emscripten always links statically
self.cmd.arg("-l").arg(name);
}
Expand Down Expand Up @@ -1225,7 +1243,13 @@ impl<'a> Linker for WasmLd<'a> {
}
}

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,
_search_paths: &SearchPaths,
_as_needed: bool,
) {
self.cmd.arg("-l").arg(name);
}

Expand Down Expand Up @@ -1372,7 +1396,13 @@ impl<'a> Linker for L4Bender<'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,
_search_paths: &SearchPaths,
_as_needed: bool,
) {
bug!("dylibs are not supported on L4Re");
}

Expand Down Expand Up @@ -1549,7 +1579,13 @@ impl<'a> Linker for AixLinker<'a> {
}
}

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,
_search_paths: &SearchPaths,
_as_needed: bool,
) {
self.hint_dynamic();
self.cmd.arg(format!("-l{name}"));
}
Expand Down Expand Up @@ -1755,7 +1791,13 @@ impl<'a> Linker for PtxLinker<'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,
_search_paths: &SearchPaths,
_as_needed: bool,
) {
panic!("external dylibs not supported")
}

Expand Down Expand Up @@ -1837,7 +1879,13 @@ impl<'a> Linker for LlbcLinker<'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,
_search_paths: &SearchPaths,
_as_needed: bool,
) {
panic!("external dylibs not supported")
}

Expand Down Expand Up @@ -1928,7 +1976,13 @@ impl<'a> Linker for BpfLinker<'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,
_search_paths: &SearchPaths,
_as_needed: bool,
) {
panic!("external dylibs not supported")
}

Expand Down

0 comments on commit dc13b70

Please sign in to comment.