-
Notifications
You must be signed in to change notification settings - Fork 522
Update rust_bindgen_library to only link into the direct consumer
#2361
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rust_bindgen_library to only link cc_lib into the direct c…rust_bindgen_library to only link into the direct consumer
Collaborator
Author
|
|
e13a082 to
e88d6aa
Compare
Collaborator
illicitonion
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally looks reasonable, added a few thoughts
ff988e9 to
865e063
Compare
57162c0 to
60d0382
Compare
Collaborator
Author
|
@illicitonion this good to go? |
Co-authored-by: Daniel Wagner-Hall <[email protected]>
illicitonion
approved these changes
Dec 28, 2023
tidefield
pushed a commit
that referenced
this pull request
Feb 16, 2024
### Problem
As of now (before this PR), we seem to mix `link_flags` file into using
for two purposes.
For
```
cc_library(
name = "simple",
srcs = ["simple.cc"],
hdrs = ["simple.h"],
linkopts = ["-framework"],
)
rust_bindgen_library(
name = "simple_bindgen",
cc_lib = ":simple",
header = "simple.h",
)
rust_binary(
name = "simple_example",
srcs = ["main.rs"],
deps = [":simple_bindgen"],
)
```
`rust_bindgen_library` produces a `link_flags` file with
```
-lstatic=simple
-C
link-args=-framework
```
`-lstatic=simple` is consumed by `rustc` whereas `-framework` is
expected to be consumed by an actual linker (either invoked by `rustc`
or `cc_common.link`)
The flags are then passed to `rustc` command to compile
`libsimple_bindgen.rlib`. It does not yield any error because
`-lstatic=simple` is correctly used whereas `-framework` is no-op (there
is no linker being invoked for producing `rlib`). However, the rustc
***doesn't*** pass `-framework` to the linker that link the
`rust_binary` (which is where the cc linkopts matters).
Another problem is that this approach is not compatible with
`experimental_use_cc_common_link` option which let `cc_common.link`
instead of `rustc` invoke the linker. See #2413
### Solution
We're referring "link" as at least two things (which I think what causes
problem here):
1.
https://doc.rust-lang.org/rustc/command-line-arguments.html#-l-link-the-generated-crate-to-a-native-library
2. https://doc.rust-lang.org/rustc/codegen-options/index.html#linker
https://doc.rust-lang.org/rustc/codegen-options/index.html#link-args
As proposed in
#2415 (comment),
this PR splits `<rust_library_bindgen>__bindgen.link_flags` produced by
`rust_bindgen` rule into two files:
1. `rustc_flags`
```
-lstatic=simple
```
2. `linker_flags`
```
-framework
```
We "sort-of" (but not perfectly) had it correct before
#2361 when we link the
binary directly with the cc_library (aka both kinds of flags).
But since we want to support the Cargo style of linking
```
cc_lib -> bindgen -> lib_a -> bin
```
instead of
```
cc_lib -> bindgen -> lib_a -> bin
\___________________________^
```
we can pass `-lstatic=simple` to the `rustc` command that builds
`simple_bindgen` (rust_library) and propagate `linkopts` to
`simple_example` (rust_binary) so that the linker can use it.
```
cc_library -> rust_bindgen -> rust_library -> rust_binary
-lstatic=simple
-Clink-args="-framework"
```
This is long and sounds like a proposal. I'm open for suggestion
@UebelAndre @illicitonion @krasimirgg
Fixes #2413
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Most uses of bindgen are via build scripts which typically use either
-Lnative=to specify linker paths for the current target being built. The way the rules are currently defined, has the bindgen library linked directly into the final library which leads to some annoying ordering errors compared to the same project building with Cargo. This PR aims to support the Cargo style of linking via aleak_symbols = Falseflag, thus changing the dependency graph when linking from:to:
Note that
leak_symbolsis intended to be used as an escape hatch to restore the original behavior and will be deleted in the future.