commit | 8fd0904a58d1256a531a89ecdb774ba970049185 | [log] [tgz] |
---|---|---|
author | Vinh Tran <vinhdaitran@google.com> | Thu Feb 15 23:04:34 2024 -0500 |
committer | GitHub <noreply@github.com> | Thu Feb 15 23:04:34 2024 -0500 |
tree | db985bd9ccc382cab5a7291f9f839ec2dc11950d | |
parent | 5c715ec50602e2ba6ca2ebfdd870662a6e6d1eda [diff] |
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
This repository provides rules for building Rust projects with Bazel.
General discussions and announcements take place in the GitHub Discussions, but there are additional places where community members gather to discuss rules_rust
.
Please refer to the full documentation.