Skip to content
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

native_libs: More consistent native library search between rustc and linker #138170

Closed
wants to merge 1 commit into from

Conversation

petrochenkov
Copy link
Contributor

@petrochenkov petrochenkov commented Mar 7, 2025

Continuation of #129366.

Typically rustc will delegate native library search to linker, by passing to it library search directory options, like -L or equivalents.

In some cases, however, rustc will search for native libraries "manually", by itself.

  1. When bundling libraries into rlibs and staticlibs
  2. When doing search instead of the linker, when linker simply doesn't support search by name
    • Example: -force_load with ld64, which is required to implement +whole-archive, and only accepts full paths instead of names
  3. When supporting libraries with alternative naming scheme not supported by linker

It is important for the rustc search and for the linker search to give the same result (modulo cases 1. and 3., where the difference is intentional).
It is not always possible, because linkers may also search for some default system directories that depend on a specific system and are hard to predict in general.
So we can make a weaker requirement (*) - the directory list searched by rustc must be a prefix of the directory list considered by linker.

In this PR I try to use the full list of directories passed as -Ls to linker for rustc's search as well.
Besides directories explicitly provided by user, it also includes sysroot, self-contained sysroot and some SDK directories on Apple targets.

This is not a full solution, because the library bundling logic in rustc_metadata (as opposed to rustc_codegen_ssa) still skips self-contained sysroot and the Apple SDK directories.
Determining self_contained_components and apple_sdk_root requires a large chunk of linker-probing logic to be available in rustc_metadata, which generally doesn't do any linking at all.
On the other hand, turning get_self_contained_components() and get_apple_sdk_root() into queries to avoid reevaluating them from different places is also pretty annoying, because the logic in rustc_codegen_ssa cannot use queries, so they would need to be evaluated earlier and the results would need to be stored in CodegenResults.

I'm not yet sure this is the right direction, maybe we should instead exclude all kinds of sysroots from the rustc's "manual" native library search. That would satisfy the requirement (*) as well.
r? @bjorn3

@rustbot
Copy link
Collaborator

rustbot commented Mar 7, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 7, 2025
@petrochenkov
Copy link
Contributor Author

maybe we should instead exclude all kinds of sysroots from the rustc's "manual" native library search

I'll post this alternative as well, and do a couple of other experiments, before considering this PR for merging.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 7, 2025
@petrochenkov petrochenkov changed the title native_libs: More consistent library search between rustc and linker native_libs: More consistent native library search between rustc and linker Mar 7, 2025
@bors
Copy link
Contributor

bors commented Mar 8, 2025

☔ The latest upstream changes (presumably #138202) made this pull request unmergeable. Please resolve the merge conflicts.

@petrochenkov
Copy link
Contributor Author

Determining self_contained_components and apple_sdk_root requires a large chunk of linker-probing logic to be available in rustc_metadata, which generally doesn't do any linking at all.

Moving the information about bundled libraries from regular metadata (where it's not necessary) to some kind of rlib digest (#138243) will help to avoid doing any kind of library lookup in rustc_metadata and do it all in rustc_codegen_ssa.

@petrochenkov
Copy link
Contributor Author

Closing this in favor of #138273 for now, but it can be reconsidered later if #138243 is implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants