Skip to content

Commit

Permalink
Auto merge of #132394 - madsmtm:fix-apple-verbatim-link, r=<try>
Browse files Browse the repository at this point in the history
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 #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
  • Loading branch information
bors committed Oct 31, 2024
2 parents c8b8378 + 71a8af9 commit b856a70
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
18 changes: 17 additions & 1 deletion compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,23 @@ impl<'a> Linker for GccLinker<'a> {
self.hint_static();
let colon = if verbatim && self.is_gnu { ":" } else { "" };
if !whole_archive {
self.link_or_cc_arg(format!("-l{colon}{name}"));
if self.sess.target.is_like_osx && verbatim {
// The man page for ld64's `-l` option says:
// > This option tells the linker to search for libx.dylib or libx.a in the library
// > search path. If string x is of the form y.o, then that file is searched for in
// > the same places, but without prepending `lib` or appending `.a` or `.dylib` to
// > the filename.
//
// So if we're linking a verbatim file, then we need to do the lookup and pass it
// directly as a normal file parameter on the command line.
//
// NOTE: We _could_ add `&& name.ends_with(".o")`, but having more guaranteed
// consistent behaviour in `rustc` regardless of the file extension seems like the
// better solution.
self.link_or_cc_arg(find_native_static_library(name, verbatim, self.sess));
} else {
self.link_or_cc_arg(format!("-l{colon}{name}"));
}
} else if self.sess.target.is_like_osx {
// -force_load is the macOS equivalent of --whole-archive, but it
// involves passing the full path to the library to link.
Expand Down
3 changes: 0 additions & 3 deletions tests/run-make/native-link-modifier-verbatim-linker/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
// This test is the same as native-link-modifier-rustc, but without rlibs.
// See https://github.com/rust-lang/rust/issues/99425

//@ ignore-apple
// Reason: linking fails due to the unusual ".ext" staticlib name.

use run_make_support::rustc;

fn main() {
Expand Down

0 comments on commit b856a70

Please sign in to comment.