From b9f51f5970772a4001a2564871ed41cb613e87bb Mon Sep 17 00:00:00 2001 From: David Havas Date: Fri, 3 Jan 2025 20:37:18 +0100 Subject: [PATCH] Update hello_comp_opt example with new LTO settings (#3155) Fixes comment mentioned in [3143#issuecomment-2568947858](https://github.com/bazelbuild/rules_rust/issues/3143#issuecomment-2568947858) --------- Co-authored-by: UebelAndre --- examples/compile_opt/.bazelrc | 4 ++++ examples/compile_opt/MODULE.bazel | 4 ++-- examples/compile_opt/README.md | 22 ++++++++++++------- .../compile_opt/hello_comp_opt/BUILD.bazel | 1 - 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/examples/compile_opt/.bazelrc b/examples/compile_opt/.bazelrc index d45bbb98b1..28163ca7b9 100644 --- a/examples/compile_opt/.bazelrc +++ b/examples/compile_opt/.bazelrc @@ -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 diff --git a/examples/compile_opt/MODULE.bazel b/examples/compile_opt/MODULE.bazel index 282c4e655b..c97a130771 100644 --- a/examples/compile_opt/MODULE.bazel +++ b/examples/compile_opt/MODULE.bazel @@ -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 = "../..", @@ -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( diff --git a/examples/compile_opt/README.md b/examples/compile_opt/README.md index b229f7607a..9eda7d9c9f 100644 --- a/examples/compile_opt/README.md +++ b/examples/compile_opt/README.md @@ -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( @@ -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( @@ -27,7 +34,6 @@ rust_binary( deps = [], rustc_flags = select({ "//:release": [ - "-Clto=true", "-Ccodegen-units=1", "-Cpanic=abort", "-Copt-level=3", @@ -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 //...` diff --git a/examples/compile_opt/hello_comp_opt/BUILD.bazel b/examples/compile_opt/hello_comp_opt/BUILD.bazel index a12ba0761d..f1436f99fd 100644 --- a/examples/compile_opt/hello_comp_opt/BUILD.bazel +++ b/examples/compile_opt/hello_comp_opt/BUILD.bazel @@ -5,7 +5,6 @@ rust_binary( srcs = ["src/main.rs"], rustc_flags = select({ "//:release": [ - "-Clto", "-Ccodegen-units=1", "-Cpanic=abort", "-Copt-level=3",