-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support auto linking on Apple targets #121293
Comments
CC people whom I know have worked with macOS linking before: @BlackHoleFox, @bjorn3, @simlay |
This would be great to have! For ELF I recently found the |
On Windows we could embed static dependencies in static libraries (using the |
Seems neat to me. Is auto-linking available in |
Just checked, |
Agreed.
I think I'd argue that:
In this specific case, it's a bit more difficult, as we'd have to choose one of these options ( |
Hmm, I don't know much about Windows, are there different linkers there, and what are their names? Seems like LLVM's |
Rustc doesn't know which linker will be used and the build system that invokes the linker likely allows picking a linker which doesn't support this feature. As such it has to respect |
Problem statement
Using a statically compiled Rust library from another language / with other toolchains can be somewhat troublesome, since the Rust library may have linking requirements that the user then has to manually pass to the linker.
Specific example: I have a static Rust library compiled for macOS that uses Winit and Wgpu and exposes a
rust_main
function, which I then pass to Xcode to link together with a small C main file that calls into Rust. Under the hood, Winit/Wgpu requires linking toAppKit
,Metal
and other such system frameworks. I'm required to also specify in Xcode the frameworks that my binary requires.Apart from being difficult to set up (as I, the user, has to know enough about linking to understand the inscrutable error messages), this workflow is also a possible SemVer hazard, as a library like Winit cannot add a dependency on a new the system library in a minor version without possibly breaking the user's build.
This can be fixed by using the
--print=native-static-libs
rustc
flag, but that probably requires more work for the user to integrate into the workflow than the quick fix of "just link to the broken library", and is also not very discoverable.Proposed solution
Swift and Clang have the concept of "auto linking", where the compiler will instruct the linker to link to the correct external libraries if the user imports code from one of these libraries. This is enabled in Clang with
-fmodules
, and can be further controlled with the-fno-autolink
flag.On the surface, this provides much the same functionality as Rust with its
#[link(...)]
attribute, but there's an important distinction: the dependency information is embedded in the object file, and understood by the linker itself, which allows it to work without Swift/Clang driving the linker invocation!This would fix #110143.
Implementation notes
Mach-O binaries uses the
LC_LINKER_OPTION
load command for this, which is understood by LLVM's lld, and Apple's ld64. A few resources on that:ELF binaries linking with LLVM's lld can use the
.linker-options
section or the.deplibs
section.As you can see, this is unfortunately quite platform-specific, and depends on the capabilities of the linker, so it probably isn't solvable in the general case; but I'd argue that this is still something that we could slowly improve support for, since this has a clear graceful fallback.
I'd be willing to (attempt to) implement this if you think this is desired?
Drawbacks / Unknowns
More complex linking integration, would this work for "Rust lib depending on Rust static lib" use-case, if that's even possible today?
Linker arguments are inherently ordered, and may be unexpectedly jumbled by this transformation? Would have to be properly researched and tested.
Sometimes, the user may want to opt-out of this behaviour. This can be done with the linker flag
-ignore_auto_link
, though we should probably document that somewhere once this has been implemented.The text was updated successfully, but these errors were encountered: