Skip to content

Commit d26d64b

Browse files
committed
linker: Link dylib crates by path
1 parent e1ac0fa commit d26d64b

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+13-20
Original file line numberDiff line numberDiff line change
@@ -2816,6 +2816,15 @@ fn rehome_sysroot_lib_dir(sess: &Session, lib_dir: &Path) -> PathBuf {
28162816
}
28172817
}
28182818

2819+
fn rehome_lib_path(sess: &Session, path: &Path) -> PathBuf {
2820+
if let Some(dir) = path.parent() {
2821+
let file_name = path.file_name().expect("library path has no file name component");
2822+
rehome_sysroot_lib_dir(sess, dir).join(file_name)
2823+
} else {
2824+
fix_windows_verbatim_for_gcc(path)
2825+
}
2826+
}
2827+
28192828
// Adds the static "rlib" versions of all crates to the command line.
28202829
// There's a bit of magic which happens here specifically related to LTO,
28212830
// namely that we remove upstream object files.
@@ -2847,12 +2856,7 @@ fn add_static_crate(
28472856
let cratepath = &src.rlib.as_ref().unwrap().0;
28482857

28492858
let mut link_upstream = |path: &Path| {
2850-
let rlib_path = if let Some(dir) = path.parent() {
2851-
let file_name = path.file_name().expect("rlib path has no file name path component");
2852-
rehome_sysroot_lib_dir(sess, dir).join(file_name)
2853-
} else {
2854-
fix_windows_verbatim_for_gcc(path)
2855-
};
2859+
let rlib_path = rehome_lib_path(sess, path);
28562860
cmd.link_staticlib_by_path(&rlib_path, false);
28572861
};
28582862

@@ -2918,27 +2922,16 @@ fn add_static_crate(
29182922

29192923
// Same thing as above, but for dynamic crates instead of static crates.
29202924
fn add_dynamic_crate(cmd: &mut dyn Linker, sess: &Session, cratepath: &Path) {
2921-
// Just need to tell the linker about where the library lives and
2922-
// what its name is
2923-
let parent = cratepath.parent();
29242925
// When producing a dll, the MSVC linker may not actually emit a
29252926
// `foo.lib` file if the dll doesn't actually export any symbols, so we
29262927
// check to see if the file is there and just omit linking to it if it's
29272928
// not present.
29282929
if sess.target.is_like_msvc && !cratepath.with_extension("dll.lib").exists() {
29292930
return;
29302931
}
2931-
if let Some(dir) = parent {
2932-
cmd.include_path(&rehome_sysroot_lib_dir(sess, dir));
2933-
}
2934-
// "<dir>/name.dll -> name.dll" on windows-msvc
2935-
// "<dir>/name.dll -> name" on windows-gnu
2936-
// "<dir>/libname.<ext> -> name" elsewhere
2937-
let stem = if sess.target.is_like_msvc { cratepath.file_name() } else { cratepath.file_stem() };
2938-
let stem = stem.unwrap().to_str().unwrap();
2939-
// Convert library file-stem into a cc -l argument.
2940-
let prefix = if stem.starts_with("lib") && !sess.target.is_like_windows { 3 } else { 0 };
2941-
cmd.link_dylib_by_name(&stem[prefix..], false, true);
2932+
2933+
let dylib_path = rehome_lib_path(sess, cratepath);
2934+
cmd.link_dylib_by_path(&dylib_path, true);
29422935
}
29432936

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

compiler/rustc_codegen_ssa/src/back/linker.rs

+26
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ pub trait Linker {
170170
fn cmd(&mut self) -> &mut Command;
171171
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path);
172172
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, as_needed: bool);
173+
fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
174+
self.cmd().arg(path);
175+
}
173176
fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
174177
bug!("framework linked with unsupported linker")
175178
}
@@ -466,6 +469,29 @@ impl<'a> Linker for GccLinker<'a> {
466469
}
467470
}
468471

472+
fn link_dylib_by_path(&mut self, path: &Path, as_needed: bool) {
473+
if !as_needed {
474+
if self.sess.target.is_like_osx {
475+
// FIXME(81490): ld64 doesn't support these flags but macOS 11
476+
// has -needed-l{} / -needed_library {}
477+
// but we have no way to detect that here.
478+
self.sess.dcx().emit_warn(errors::Ld64UnimplementedModifier);
479+
} else if self.is_gnu && !self.sess.target.is_like_windows {
480+
self.linker_arg("--no-as-needed");
481+
} else {
482+
self.sess.dcx().emit_warn(errors::LinkerUnsupportedModifier);
483+
}
484+
}
485+
self.cmd.arg(path);
486+
if !as_needed {
487+
if self.sess.target.is_like_osx {
488+
// See above FIXME comment
489+
} else if self.is_gnu && !self.sess.target.is_like_windows {
490+
self.linker_arg("--as-needed");
491+
}
492+
}
493+
}
494+
469495
fn link_framework_by_name(&mut self, name: &str, _verbatim: bool, as_needed: bool) {
470496
self.hint_dynamic();
471497
if !as_needed {

0 commit comments

Comments
 (0)