Bzlmod has no support for strip_level or opt_level
chrisstaite-menlo opened this issue · 3 comments
chrisstaite-menlo commented
There's currently no way to pass the strip_level or opt_level to rust_toolchain using Bzlmod.
marvin-hansen commented
You can do this with manual compiler option pass through in 3 steps
- 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.
- 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"],
)
- Run bazel build with optimization
bazel build -c opt //...
Example code:
https://github.com/marvin-hansen/bazel_rust_example
Hope that helps.
chrisstaite-menlo commented
Oh, that's great, thank you!
marvin-hansen commented
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?