bazelbuild/rules_rust

Bzlmod has no support for strip_level or opt_level

chrisstaite-menlo opened this issue · 3 comments

There's currently no way to pass the strip_level or opt_level to rust_toolchain using Bzlmod.

You can do this with manual compiler option pass through in 3 steps

  1. In your root folder BUILD.bazel, add the following entry:
config_setting(
    name = "release",
    values = {
        "compilation_mode": "opt",
    },
)

This maps Rusts release mode to Bazel's -c opt.

  1. In your binary, add the opt flags & strip settings:
# Build binary
rust_binary(
    name = "bin",
    crate_root = "src/main.rs",
    srcs = glob([
        "src/*/*.rs",
        "src/*.rs",
    ]),
    # Compiler optimization
    rustc_flags = select({
       "//:release": [
            "-Clto",
            "-Ccodegen-units=1",
            "-Cpanic=abort",
            "-Copt-level=3",
            "-Cstrip=symbols",
            ],
        "//conditions:default":
        [
           "-Copt-level=0",
        ],
    }),

    deps = [   ],
    visibility = ["//visibility:public"],
)
  1. Run bazel build with optimization

bazel build -c opt //...

Example code:
https://github.com/marvin-hansen/bazel_rust_example

Hope that helps.

Oh, that's great, thank you!

I'm drafting currently the new Bazelmod documentation that should get upstreamed hopefully soon. Do you think this issue can be closed given this will be documented?