Skip to content

Commit dc13b70

Browse files
committed
linker: Add search_paths parameter to link_dylib_by_name
This allows for implementing looking up Meson and MinGW import libraries. See rust-lang#122455
1 parent 266326f commit dc13b70

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1280,8 +1280,9 @@ fn link_sanitizer_runtime(
12801280
let filename = format!("rustc{channel}_rt.{name}");
12811281
let path = find_sanitizer_runtime(sess, &filename);
12821282
let rpath = path.to_str().expect("non-utf8 component in path");
1283+
let search_paths = SearchPaths::default();
12831284
linker.args(&["-Wl,-rpath", "-Xlinker", rpath]);
1284-
linker.link_dylib_by_name(&filename, false, true);
1285+
linker.link_dylib_by_name(&filename, false, &search_paths, true);
12851286
} else if sess.target.is_like_msvc && flavor == LinkerFlavor::Msvc(Lld::No) && name == "asan" {
12861287
// MSVC provides the `/INFERASANLIBS` argument to automatically find the
12871288
// compatible ASAN library.
@@ -2568,7 +2569,7 @@ fn add_native_libs_from_crate(
25682569
}
25692570
NativeLibKind::Dylib { as_needed } => {
25702571
if link_dynamic {
2571-
cmd.link_dylib_by_name(name, verbatim, as_needed.unwrap_or(true))
2572+
cmd.link_dylib_by_name(name, verbatim, search_paths, as_needed.unwrap_or(true))
25722573
}
25732574
}
25742575
NativeLibKind::Unspecified => {
@@ -2580,7 +2581,7 @@ fn add_native_libs_from_crate(
25802581
}
25812582
} else {
25822583
if link_dynamic {
2583-
cmd.link_dylib_by_name(name, verbatim, true);
2584+
cmd.link_dylib_by_name(name, verbatim, search_paths, true);
25842585
}
25852586
}
25862587
}
@@ -2912,7 +2913,8 @@ fn add_dynamic_crate(cmd: &mut dyn Linker, sess: &Session, cratepath: &Path) {
29122913
let stem = stem.unwrap().to_str().unwrap();
29132914
// Convert library file-stem into a cc -l argument.
29142915
let prefix = if stem.starts_with("lib") && !sess.target.is_like_windows { 3 } else { 0 };
2915-
cmd.link_dylib_by_name(&stem[prefix..], false, true);
2916+
let search_paths = SearchPaths::default();
2917+
cmd.link_dylib_by_name(&stem[prefix..], false, &search_paths, true);
29162918
}
29172919

29182920
fn relevant_lib(sess: &Session, lib: &NativeLib) -> bool {

compiler/rustc_codegen_ssa/src/back/linker.rs

+64-10
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,13 @@ pub fn get_linker<'a>(
167167
pub trait Linker {
168168
fn cmd(&mut self) -> &mut Command;
169169
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path);
170-
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, as_needed: bool);
170+
fn link_dylib_by_name(
171+
&mut self,
172+
name: &str,
173+
verbatim: bool,
174+
search_paths: &SearchPaths,
175+
as_needed: bool,
176+
);
171177
fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
172178
bug!("framework linked with unsupported linker")
173179
}
@@ -432,7 +438,13 @@ impl<'a> Linker for GccLinker<'a> {
432438
}
433439
}
434440

435-
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, as_needed: bool) {
441+
fn link_dylib_by_name(
442+
&mut self,
443+
name: &str,
444+
verbatim: bool,
445+
_search_paths: &SearchPaths,
446+
as_needed: bool,
447+
) {
436448
if self.sess.target.os == "illumos" && name == "c" {
437449
// libc will be added via late_link_args on illumos so that it will
438450
// appear last in the library search order.
@@ -807,7 +819,7 @@ impl<'a> Linker for MsvcLinker<'a> {
807819
}
808820
}
809821

810-
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
822+
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _search_paths: &SearchPaths, _as_needed: bool) {
811823
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
812824
}
813825

@@ -1051,7 +1063,13 @@ impl<'a> Linker for EmLinker<'a> {
10511063

10521064
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
10531065

1054-
fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
1066+
fn link_dylib_by_name(
1067+
&mut self,
1068+
name: &str,
1069+
_verbatim: bool,
1070+
_search_paths: &SearchPaths,
1071+
_as_needed: bool,
1072+
) {
10551073
// Emscripten always links statically
10561074
self.cmd.arg("-l").arg(name);
10571075
}
@@ -1225,7 +1243,13 @@ impl<'a> Linker for WasmLd<'a> {
12251243
}
12261244
}
12271245

1228-
fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
1246+
fn link_dylib_by_name(
1247+
&mut self,
1248+
name: &str,
1249+
_verbatim: bool,
1250+
_search_paths: &SearchPaths,
1251+
_as_needed: bool,
1252+
) {
12291253
self.cmd.arg("-l").arg(name);
12301254
}
12311255

@@ -1372,7 +1396,13 @@ impl<'a> Linker for L4Bender<'a> {
13721396

13731397
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
13741398

1375-
fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
1399+
fn link_dylib_by_name(
1400+
&mut self,
1401+
_name: &str,
1402+
_verbatim: bool,
1403+
_search_paths: &SearchPaths,
1404+
_as_needed: bool,
1405+
) {
13761406
bug!("dylibs are not supported on L4Re");
13771407
}
13781408

@@ -1549,7 +1579,13 @@ impl<'a> Linker for AixLinker<'a> {
15491579
}
15501580
}
15511581

1552-
fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
1582+
fn link_dylib_by_name(
1583+
&mut self,
1584+
name: &str,
1585+
_verbatim: bool,
1586+
_search_paths: &SearchPaths,
1587+
_as_needed: bool,
1588+
) {
15531589
self.hint_dynamic();
15541590
self.cmd.arg(format!("-l{name}"));
15551591
}
@@ -1755,7 +1791,13 @@ impl<'a> Linker for PtxLinker<'a> {
17551791

17561792
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
17571793

1758-
fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
1794+
fn link_dylib_by_name(
1795+
&mut self,
1796+
_name: &str,
1797+
_verbatim: bool,
1798+
_search_paths: &SearchPaths,
1799+
_as_needed: bool,
1800+
) {
17591801
panic!("external dylibs not supported")
17601802
}
17611803

@@ -1837,7 +1879,13 @@ impl<'a> Linker for LlbcLinker<'a> {
18371879

18381880
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
18391881

1840-
fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
1882+
fn link_dylib_by_name(
1883+
&mut self,
1884+
_name: &str,
1885+
_verbatim: bool,
1886+
_search_paths: &SearchPaths,
1887+
_as_needed: bool,
1888+
) {
18411889
panic!("external dylibs not supported")
18421890
}
18431891

@@ -1928,7 +1976,13 @@ impl<'a> Linker for BpfLinker<'a> {
19281976

19291977
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
19301978

1931-
fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
1979+
fn link_dylib_by_name(
1980+
&mut self,
1981+
_name: &str,
1982+
_verbatim: bool,
1983+
_search_paths: &SearchPaths,
1984+
_as_needed: bool,
1985+
) {
19321986
panic!("external dylibs not supported")
19331987
}
19341988

0 commit comments

Comments
 (0)