Support `cargo:rustc-link-arg=FLAG` in build script runner (#1061)

Cargo 1.50 added support for `cargo:rustc-link-arg` and `cargo:rust-link-arg-bins` instructions in build script output (`rustc-cdylib-link-arg` has been supported since 1.35 and `cargo:rustc-link-arg-bin` was later added in 1.54).

The build script runner currently prints a warning when `rustc-cdylib-link-arg` is used but entirely ignores the other ones (likely setting them as `BuildScriptOutput::DepEnv` since this is the default branch).

This patch adds support for the `cargo:rustc-link-arg` instruction and prints an warning when `cargo:rust-link-arg-bins` and `cargo:rust-link-arg-bin` are used, just like `rustc-cdylib-link-arg`.

Note that `cargo:rustc-link-arg` is important because `cargo` errors when `cargo:rustc-flags` is used for anything that isn't a `-l` or `-L` flag (nb: this is enforced by cargo so it'd work with Bazel). There are cases (such as linking a `-weak_framework` or `-weak_library` on macos) that cannot use `cargo:rustc-link-lib` and passing a custom `-Clink-arg` is required)

I think that we should also support `cargo:rust-link-arg-bins`, `rustc-cdylib-link-arg` and `cargo:rustc-link-arg-bin` but that's a bigger task since it requires knowing the crate type the build output will apply to. I've made issue https://github.com/bazelbuild/rules_rust/issues/1062 for it.

Ref: https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script
1 file changed
tree: 53828d2ab5b559bbf614d990f3b8bfac3a62119f
  1. .bazelci/
  2. .github/
  3. bindgen/
  4. cargo/
  5. crate_universe/
  6. docs/
  7. examples/
  8. proto/
  9. rust/
  10. test/
  11. tools/
  12. util/
  13. wasm_bindgen/
  14. .bazelignore
  15. .bazelrc
  16. .gitignore
  17. ARCHITECTURE.md
  18. AUTHORS
  19. BUILD.bazel
  20. CODEOWNERS
  21. COMPATIBILITY.md
  22. CONTRIBUTING.md
  23. CONTRIBUTORS
  24. LICENSE.txt
  25. README.md
  26. WORKSPACE.bazel
  27. workspace.bzl
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.