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

Bzlmod has no support for strip_level or opt_level #2701

Closed
chrisstaite-menlo opened this issue Jun 17, 2024 · 3 comments
Closed

Bzlmod has no support for strip_level or opt_level #2701

chrisstaite-menlo opened this issue Jun 17, 2024 · 3 comments
Labels

Comments

@chrisstaite-menlo
Copy link

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

@marvin-hansen
Copy link
Contributor

marvin-hansen commented Jun 19, 2024

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.

@chrisstaite-menlo
Copy link
Author

Oh, that's great, thank you!

@marvin-hansen
Copy link
Contributor

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?

github-merge-queue bot pushed a commit that referenced this issue Jul 12, 2024
This PR provides documentation of Bazelmod and several code examples
that addresses a number of issues related to Bazelmod.

Preview of the documentation:
https://github.com/marvin-hansen/rules_rust/blob/main/docs/crate_universe_bzlmod.md

First and foremost it paves the way for a meaningful update the Bazelmod
documentation that references these and existing code examples. This
touches at least the following issues:
* #2670
* #2181


The compile_opt example addresses or resolves:
*  #515
* #2701

The musl_cross_compilling example addresses or resolves
* #390 
* #276

The oci_container does not relate to any open issue, 
although the tokio example in it gives a nice end to end example so 
this definitely helps those looking for something non-trivial.

The proto example addresses or resolves:
*  #2668
*  #302
* #2534
* Possibly a few more if I were to search longer

Formalities
* I've signed the CLA
* I've signed all commits

---------

Signed-off-by: Marvin Hansen <marvin.hansen@gmail.com>
Co-authored-by: Daniel Wagner-Hall <dawagner@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants