feat: Add `//rust/settings:lto` (#3104)

Fixes https://github.com/bazelbuild/rules_rust/issues/3045

This PR adds a new build setting `//rust/settings:lto=(off|thin|fat)`
which changes how we specify the following flags:

*
[`lto`](https://doc.rust-lang.org/rustc/codegen-options/index.html#lto)
*
[`embed-bitcode`](https://doc.rust-lang.org/rustc/codegen-options/index.html#embed-bitcode)
*
[`linker-plugin-lto`](https://doc.rust-lang.org/rustc/codegen-options/index.html#linker-plugin-lto)

The way we invoke the flags was based on how Cargo does it today
([code](https://github.com/rust-lang/cargo/blob/769f622e12db0001431d8ae36d1093fb8727c5d9/src/cargo/core/compiler/lto.rs#L4))
and based on suggestions from the [Rust
docs](https://doc.rust-lang.org/rustc/codegen-options/index.html#embed-bitcode).

When LTO is not enabled, we will specify `-Cembed-bitcode=no` which
tells `rustc` to skip embedding LLVM bitcode and should speed up builds.
Similarly when LTO is enabled we specify `-Clinker-plugin-lto` which
will cause `rustc` to skip generating objects files entirely, and
instead replace them with LLVM bitcode*.

*only when building an `rlib`, when building other crate types we
continue generating object files.

I added unit tests to make sure we pass the flags correctly, as well as
some docs describing the new setting. Please let me know if I should add
more!
6 files changed
tree: a72e786fb493e02c26c18003fe4f19455a5b2920
  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. .bazelversion
  18. .clang-format
  19. .clippy.toml
  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
  37. WORKSPACE.bzlmod
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.