Add support for Rust ABI dynamic libraries (#4179)

Currently, there is not explicit stable Rust ABI for dynamic libraries,
but Rust library may still be compiled as one. This can be useful if a
large project wants to compile some core portion of its code as a shared
object that can be dynamically linked against several different
binaries. Bevy, for example, offers this capability as a way to allow
customers of Bevy to tweak their own code without having to statically
re-link the central Bevy engine code.

This PR exposes the Rust ABI dylib capabilities with a new rule,
`rust_dylib_library`. For clarity, it also creates a
`rust_cylib_library` rule, and updates methods to this name, with
`rust_shared_library` just aliasing to the `rust_cdylib_library`
implementation.

Building a Rust ABI dylib may cause the standard library to be linked
dynamically against the generated dylib. When depended on, the dynamic
standard library needs to be included to get the binary to execute.
Currently, `rules_rust` offers this as a toolchain-level flag, but
applying a toolchain transition at the dylib's interface imposes a new
toolchain on its deps, which then forces any upstream `rlibs`, for
example, to recompile.

To fix this, this PR exposes a `link_std_dylib` attr to `rust_binary`,
`rust_dylib_library` and `rust_test` to allow these targets to
optionally bundle themselves with the dynamically-linked stdlib.

---------

Co-authored-by: UebelAndre <github@uebelandre.com>
17 files changed
tree: bb7dab65803ae17899ab4f4133b39af11c0fbe6c
  1. .bazelci/
  2. .bcr/
  3. .github/
  4. cargo/
  5. crate_universe/
  6. docs/
  7. examples/
  8. extensions/
  9. ffi/
  10. nix/
  11. rust/
  12. test/
  13. tools/
  14. util/
  15. .bazelignore
  16. .bazelrc
  17. .clang-format
  18. .clippy.toml
  19. .envrc
  20. .gitattributes
  21. .gitignore
  22. .pre-commit-config.yaml
  23. .prettierrc.toml
  24. .rustfmt.toml
  25. .typos.toml
  26. ARCHITECTURE.md
  27. AUTHORS
  28. BUILD.bazel
  29. CODEOWNERS
  30. COMPATIBILITY.md
  31. CONTRIBUTING.md
  32. CONTRIBUTORS
  33. LICENSE.txt
  34. MODULE.bazel
  35. README.md
  36. version.bzl
  37. WORKSPACE.bazel
README.md

Rust Rules

  • Postsubmit Build status

Overview

This repository provides rules for building Rust projects with Bazel.

Starter repo

The fastest way to try this in an empty project is to click the green “Use this template” button on https://github.com/bazel-starters/rust.

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.