Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update hello_comp_opt example with new LTO settings #3155

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/compile_opt/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ build --incompatible_merge_fixed_and_default_shell_env

# https://github.com/bazelbuild/bazel/issues/23043.
build --incompatible_autoload_externally=

# in optimized build we will use thin lto
common:opt -c opt
common:opt --@rules_rust//rust/settings:lto=thin
4 changes: 2 additions & 2 deletions examples/compile_opt/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module(
# B A Z E L C E N T R A L R E G I S T R Y # https://registry.bazel.build/
###############################################################################
# https://github.com/bazelbuild/rules_rust/releases
bazel_dep(name = "rules_rust", version = "0.46.0")
bazel_dep(name = "rules_rust", version = "0.56.0")
local_path_override(
module_name = "rules_rust",
path = "../..",
Expand All @@ -20,7 +20,7 @@ local_path_override(
# Rust toolchain
RUST_EDITION = "2021"

RUST_VERSION = "1.79.0"
RUST_VERSION = "1.83.0"

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
Expand Down
22 changes: 14 additions & 8 deletions examples/compile_opt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Each binary target can have its own compiler options, and these can be customised differently for different optimisation levels.
This takes three steps:

1) In your root folder BUILD.bazel, add the following entry:
1) In your root folder `BUILD.bazel`, add the following entry:

```Starlark
config_setting(
Expand All @@ -14,11 +14,18 @@ config_setting(
)
```

2) In your binary target, add the optimization flags & strip settings prefixed with -C.
For a complete list of Rust compiler optimization flag, please read the
[official cargo documentation](https://doc.rust-lang.org/cargo/reference/profiles.html).
2) Add config option in `.bazelrc` to enable LTO together with optimized build.

```Starlark
```Starlark
common:opt -c opt
UebelAndre marked this conversation as resolved.
Show resolved Hide resolved
common:opt --@rules_rust//rust/settings:lto=thin
```

3) In your binary target, add the optimization flags & strip settings prefixed with -C.
For a complete list of Rust compiler optimization flag, please read the
[official cargo documentation](https://doc.rust-lang.org/cargo/reference/profiles.html).

```Starlark
load("@rules_rust//rust:defs.bzl", "rust_binary")

rust_binary(
Expand All @@ -27,7 +34,6 @@ rust_binary(
deps = [],
rustc_flags = select({
"//:release": [
"-Clto=true",
"-Ccodegen-units=1",
"-Cpanic=abort",
"-Copt-level=3",
Expand All @@ -44,8 +50,8 @@ rust_binary(

Build with optimization:

`bazel build -c opt //...`
`bazel build --config opt //...`

And run the optimized binary:

`bazel run -c opt //...`
`bazel run --config opt //...`
1 change: 0 additions & 1 deletion examples/compile_opt/hello_comp_opt/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ rust_binary(
srcs = ["src/main.rs"],
rustc_flags = select({
"//:release": [
"-Clto",
"-Ccodegen-units=1",
"-Cpanic=abort",
"-Copt-level=3",
Expand Down
Loading