Skip to content

Commit 9f32c44

Browse files
committed
Auto merge of rust-lang#132394 - madsmtm:fix-apple-verbatim-link, r=<try>
Apple: Fix direct linking with +verbatim Linking with `+verbatim` somewhat worked before, but only when the library was included as part of an rlib. Fixes rust-lang#132264. CC `@petrochenkov,` since you've worked on `+verbatim` before. `@rustbot` label O-apple A-linkage try-job: aarch64-apple try-job: x86_64-apple-2
2 parents c8b8378 + 3cb4340 commit 9f32c44

File tree

2 files changed

+17
-4
lines changed
  • compiler/rustc_codegen_ssa/src/back

2 files changed

+17
-4
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,23 @@ impl<'a> Linker for GccLinker<'a> {
585585
self.hint_static();
586586
let colon = if verbatim && self.is_gnu { ":" } else { "" };
587587
if !whole_archive {
588-
self.link_or_cc_arg(format!("-l{colon}{name}"));
588+
if self.sess.target.is_like_osx && verbatim {
589+
// The man page for ld64's `-l` option says:
590+
// > This option tells the linker to search for libx.dylib or libx.a in the library
591+
// > search path. If string x is of the form y.o, then that file is searched for in
592+
// > the same places, but without prepending `lib' or appending `.a' or `.dylib' to
593+
// > the filename.
594+
//
595+
// So if we're linking a verbatim file, then we need to do the lookup and pass it
596+
// directly as a normal file parameter on the command line.
597+
//
598+
// NOTE: We _could_ add `&& name.ends_with(".o")`, but having more guaranteed
599+
// consistent behaviour in `rustc` regardless of the file extension seems like the
600+
// better solution.
601+
self.link_or_cc_arg(find_native_static_library(name, verbatim, self.sess));
602+
} else {
603+
self.link_or_cc_arg(format!("-l{colon}{name}"));
604+
}
589605
} else if self.sess.target.is_like_osx {
590606
// -force_load is the macOS equivalent of --whole-archive, but it
591607
// involves passing the full path to the library to link.

tests/run-make/native-link-modifier-verbatim-linker/rmake.rs

-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
// This test is the same as native-link-modifier-rustc, but without rlibs.
44
// See https://github.com/rust-lang/rust/issues/99425
55

6-
//@ ignore-apple
7-
// Reason: linking fails due to the unusual ".ext" staticlib name.
8-
96
use run_make_support::rustc;
107

118
fn main() {

0 commit comments

Comments
 (0)