Skip to content

Conversation

@ParkMyCar
Copy link
Contributor

Fixes #3045

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

The way we invoke the flags was based on how Cargo does it today (code) and based on suggestions from the Rust docs.

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!

Copy link
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thank you!

@UebelAndre UebelAndre added this pull request to the merge queue Dec 16, 2024
Merged via the queue into bazelbuild:main with commit fae114c Dec 16, 2024
4 checks passed
github-merge-queue bot pushed a commit that referenced this pull request Dec 29, 2024
Related to #3119

Adds an option named "manual" to `//rust/settings/lto` which was
introduced in #3104. When
set to "manual" we will not automatically set any flags so users with
customized setups can configure the relevant flags on their own.

FWIW this was an oversight and IMO should have been included in the
original PR. Apologies to anyone who stumbled over this or encountered
build issues!
ParkMyCar added a commit to MaterializeInc/rules_rust that referenced this pull request Feb 11, 2025
Fixes bazelbuild#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!
gibfahn added a commit to gibfahn/rules_py that referenced this pull request Oct 21, 2025
The -Clto flag can conflict with parent module's Rust toolchain
configuration, particularly when embed-bitcode=no is set. This change
removes the hardcoded LTO flag to allow proper inheritance of toolchain
settings.

Related: bazelbuild/rules_rust#3104
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rustc: set --embed-bitcode=no if LTO is not enabled

2 participants