Skip to content

Commit

Permalink
Update hello_comp_opt example with new LTO settings (#3155)
Browse files Browse the repository at this point in the history
Fixes comment mentioned in
[3143#issuecomment-2568947858](#3143 (comment))

---------

Co-authored-by: UebelAndre <[email protected]>
  • Loading branch information
havasd and UebelAndre authored Jan 3, 2025
1 parent 568bb7b commit b9f51f5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
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 --compilation_mode=opt
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

0 comments on commit b9f51f5

Please sign in to comment.