Fix linkopts set in cc deps of bindgen (#2422)

### 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
https://github.com/bazelbuild/rules_rust/pull/2415#issuecomment-1890109092,
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
https://github.com/bazelbuild/rules_rust/pull/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
4 files changed
tree: db985bd9ccc382cab5a7291f9f839ec2dc11950d
  1. .bazelci/
  2. .bcr/
  3. .github/
  4. bindgen/
  5. cargo/
  6. crate_universe/
  7. docs/
  8. examples/
  9. ffi/
  10. nix/
  11. proto/
  12. rust/
  13. test/
  14. tools/
  15. util/
  16. wasm_bindgen/
  17. .bazelignore
  18. .bazelrc
  19. .clang-format
  20. .envrc
  21. .gitattributes
  22. .gitignore
  23. .prettierrc.toml
  24. .rustfmt.toml
  25. ARCHITECTURE.md
  26. AUTHORS
  27. BUILD.bazel
  28. CODEOWNERS
  29. COMPATIBILITY.md
  30. CONTRIBUTING.md
  31. CONTRIBUTORS
  32. LICENSE.txt
  33. MODULE.bazel
  34. README.md
  35. version.bzl
  36. WORKSPACE.bazel
README.md

Rust Rules

  • Postsubmit Build status

Overview

This repository provides rules for building Rust projects with Bazel.

Community

General discussions and announcements take place in the GitHub Discussions, but there are additional places where community members gather to discuss rules_rust.

Documentation

Please refer to the full documentation.