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

Update rust_bindgen_library to only link into the direct consumer #2361

Merged
merged 8 commits into from
Dec 28, 2023

Conversation

UebelAndre
Copy link
Collaborator

@UebelAndre UebelAndre commented Dec 27, 2023

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 a leak_symbols = False flag, thus changing the dependency graph when linking from:

cc_lib -> bindgen -> lib_a -> lib_b -> bin
    \__________________________________^

to:

cc_lib -> bindgen -> lib_a -> lib_b -> bin

Note that leak_symbols is intended to be used as an escape hatch to restore the original behavior and will be deleted in the future.

@UebelAndre UebelAndre changed the title Update rust_bindgen_library to only link cc_lib into the direct c… Update rust_bindgen_library to only link into the direct consumer Dec 27, 2023
@UebelAndre
Copy link
Collaborator Author

linkstatic is probably a bad name. Open to suggestions but I'll think harder about it.

Copy link
Collaborator

@illicitonion illicitonion left a 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

bindgen/private/bindgen.bzl Outdated Show resolved Hide resolved
bindgen/private/bindgen.bzl Outdated Show resolved Hide resolved
@UebelAndre UebelAndre enabled auto-merge (squash) December 28, 2023 13:40
@UebelAndre
Copy link
Collaborator Author

@illicitonion this good to go?

UebelAndre and others added 2 commits December 28, 2023 08:19
Co-authored-by: Daniel Wagner-Hall <dawagner@gmail.com>
@UebelAndre UebelAndre merged commit 23fcf67 into bazelbuild:main Dec 28, 2023
3 checks passed
@UebelAndre UebelAndre deleted the bindgen branch December 28, 2023 16:29
daivinhtran 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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants