Skip to content

Commit 2f4006c

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 #122455
1 parent 4003e74 commit 2f4006c

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,8 +1300,9 @@ fn link_sanitizer_runtime(
13001300
let filename = format!("rustc{channel}_rt.{name}");
13011301
let path = find_sanitizer_runtime(sess, &filename);
13021302
let rpath = path.to_str().expect("non-utf8 component in path");
1303+
let search_paths = SearchPaths::default();
13031304
linker.args(&["-Wl,-rpath", "-Xlinker", rpath]);
1304-
linker.link_dylib_by_name(&filename, false, true);
1305+
linker.link_dylib_by_name(&filename, false, &search_paths, true);
13051306
} else if sess.target.is_like_msvc && flavor == LinkerFlavor::Msvc(Lld::No) && name == "asan" {
13061307
// MSVC provides the `/INFERASANLIBS` argument to automatically find the
13071308
// compatible ASAN library.
@@ -2569,7 +2570,7 @@ fn add_native_libs_from_crate(
25692570
}
25702571
NativeLibKind::Dylib { as_needed } => {
25712572
if link_dynamic {
2572-
cmd.link_dylib_by_name(name, verbatim, as_needed.unwrap_or(true))
2573+
cmd.link_dylib_by_name(name, verbatim, search_paths, as_needed.unwrap_or(true))
25732574
}
25742575
}
25752576
NativeLibKind::Unspecified => {
@@ -2581,7 +2582,7 @@ fn add_native_libs_from_crate(
25812582
}
25822583
} else {
25832584
if link_dynamic {
2584-
cmd.link_dylib_by_name(name, verbatim, true);
2585+
cmd.link_dylib_by_name(name, verbatim, search_paths, true);
25852586
}
25862587
}
25872588
}
@@ -2919,7 +2920,8 @@ fn add_dynamic_crate(cmd: &mut dyn Linker, sess: &Session, cratepath: &Path) {
29192920
let stem = stem.unwrap().to_str().unwrap();
29202921
// Convert library file-stem into a cc -l argument.
29212922
let prefix = if stem.starts_with("lib") && !sess.target.is_like_windows { 3 } else { 0 };
2922-
cmd.link_dylib_by_name(&stem[prefix..], false, true);
2923+
let search_paths = SearchPaths::default();
2924+
cmd.link_dylib_by_name(&stem[prefix..], false, &search_paths, true);
29232925
}
29242926

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

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,13 @@ pub fn get_linker<'a>(
168168
pub trait Linker {
169169
fn cmd(&mut self) -> &mut Command;
170170
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path);
171-
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, as_needed: bool);
171+
fn link_dylib_by_name(
172+
&mut self,
173+
name: &str,
174+
verbatim: bool,
175+
search_paths: &SearchPaths,
176+
as_needed: bool,
177+
);
172178
fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
173179
bug!("framework linked with unsupported linker")
174180
}
@@ -439,7 +445,13 @@ impl<'a> Linker for GccLinker<'a> {
439445
}
440446
}
441447

442-
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, as_needed: bool) {
448+
fn link_dylib_by_name(
449+
&mut self,
450+
name: &str,
451+
verbatim: bool,
452+
_search_paths: &SearchPaths,
453+
as_needed: bool,
454+
) {
443455
if self.sess.target.os == "illumos" && name == "c" {
444456
// libc will be added via late_link_args on illumos so that it will
445457
// appear last in the library search order.
@@ -821,7 +833,7 @@ impl<'a> Linker for MsvcLinker<'a> {
821833
}
822834
}
823835

824-
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
836+
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _search_paths: &SearchPaths, _as_needed: bool) {
825837
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
826838
}
827839

@@ -1065,7 +1077,13 @@ impl<'a> Linker for EmLinker<'a> {
10651077

10661078
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
10671079

1068-
fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
1080+
fn link_dylib_by_name(
1081+
&mut self,
1082+
name: &str,
1083+
_verbatim: bool,
1084+
_search_paths: &SearchPaths,
1085+
_as_needed: bool,
1086+
) {
10691087
// Emscripten always links statically
10701088
self.cmd.arg("-l").arg(name);
10711089
}
@@ -1245,7 +1263,13 @@ impl<'a> Linker for WasmLd<'a> {
12451263
}
12461264
}
12471265

1248-
fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
1266+
fn link_dylib_by_name(
1267+
&mut self,
1268+
name: &str,
1269+
_verbatim: bool,
1270+
_search_paths: &SearchPaths,
1271+
_as_needed: bool,
1272+
) {
12491273
self.cmd.arg("-l").arg(name);
12501274
}
12511275

@@ -1398,7 +1422,13 @@ impl<'a> Linker for L4Bender<'a> {
13981422

13991423
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
14001424

1401-
fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
1425+
fn link_dylib_by_name(
1426+
&mut self,
1427+
_name: &str,
1428+
_verbatim: bool,
1429+
_search_paths: &SearchPaths,
1430+
_as_needed: bool,
1431+
) {
14021432
bug!("dylibs are not supported on L4Re");
14031433
}
14041434

@@ -1581,7 +1611,13 @@ impl<'a> Linker for AixLinker<'a> {
15811611
}
15821612
}
15831613

1584-
fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
1614+
fn link_dylib_by_name(
1615+
&mut self,
1616+
name: &str,
1617+
_verbatim: bool,
1618+
_search_paths: &SearchPaths,
1619+
_as_needed: bool,
1620+
) {
15851621
self.hint_dynamic();
15861622
self.cmd.arg(format!("-l{name}"));
15871623
}
@@ -1794,7 +1830,13 @@ impl<'a> Linker for PtxLinker<'a> {
17941830

17951831
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
17961832

1797-
fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
1833+
fn link_dylib_by_name(
1834+
&mut self,
1835+
_name: &str,
1836+
_verbatim: bool,
1837+
_search_paths: &SearchPaths,
1838+
_as_needed: bool,
1839+
) {
17981840
panic!("external dylibs not supported")
17991841
}
18001842

@@ -1882,7 +1924,13 @@ impl<'a> Linker for LlbcLinker<'a> {
18821924

18831925
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
18841926

1885-
fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
1927+
fn link_dylib_by_name(
1928+
&mut self,
1929+
_name: &str,
1930+
_verbatim: bool,
1931+
_search_paths: &SearchPaths,
1932+
_as_needed: bool,
1933+
) {
18861934
panic!("external dylibs not supported")
18871935
}
18881936

@@ -1979,7 +2027,13 @@ impl<'a> Linker for BpfLinker<'a> {
19792027

19802028
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
19812029

1982-
fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
2030+
fn link_dylib_by_name(
2031+
&mut self,
2032+
_name: &str,
2033+
_verbatim: bool,
2034+
_search_paths: &SearchPaths,
2035+
_as_needed: bool,
2036+
) {
19832037
panic!("external dylibs not supported")
19842038
}
19852039

0 commit comments

Comments
 (0)