native_libs: More consistent native library search between rustc and linker #138170
+392
−79
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
-force_load
withld64
, which is required to implement+whole-archive
, and only accepts full paths instead of nameslibfoo.a
on MSVC (linker: Allow MSVC to use import libraries following the Meson/MinGW convention #123436)It is important for the rustc search and for the linker search to give the same result (modulo cases
1.
and3.
, 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
-L
s 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 torustc_codegen_ssa
) still skipsself-contained
sysroot and the Apple SDK directories.Determining
self_contained_components
andapple_sdk_root
requires a large chunk of linker-probing logic to be available inrustc_metadata
, which generally doesn't do any linking at all.On the other hand, turning
get_self_contained_components()
andget_apple_sdk_root()
into queries to avoid reevaluating them from different places is also pretty annoying, because the logic inrustc_codegen_ssa
cannot use queries, so they would need to be evaluated earlier and the results would need to be stored inCodegenResults
.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