From df91f387bf10a9d762f4bc325da0936ba490630e Mon Sep 17 00:00:00 2001 From: matte1 Date: Thu, 22 Feb 2024 16:37:38 -0700 Subject: [PATCH] feat: Strip debug info from opt builds Attempts to follow the cargo proposal linked below to remove debug info for release builds. Furthermore this should uphold the expected behavior of bazel for cc builds which automatically strips debug symbols for optimized builds. https://github.com/rust-lang/cargo/issues/4122#issuecomment-1868318860 --- rust/private/rustc.bzl | 1 + rust/toolchain.bzl | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 4abfdba15f..095724135f 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -958,6 +958,7 @@ def construct_arguments( compilation_mode = get_compilation_mode_opts(ctx, toolchain) rustc_flags.add(compilation_mode.opt_level, format = "--codegen=opt-level=%s") rustc_flags.add(compilation_mode.debug_info, format = "--codegen=debuginfo=%s") + rustc_flags.add(compilation_mode.strip_level, format = "--codegen=strip=%s") # For determinism to help with build distribution and such if remap_path_prefix != None: diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index 9580d7cfc4..b321ae1048 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -464,11 +464,13 @@ def _rust_toolchain_impl(ctx): list: A list containing the target's toolchain Provider info """ compilation_mode_opts = {} - for k, v in ctx.attr.opt_level.items(): + for k, opt_level in ctx.attr.opt_level.items(): if not k in ctx.attr.debug_info: fail("Compilation mode {} is not defined in debug_info but is defined opt_level".format(k)) - compilation_mode_opts[k] = struct(debug_info = ctx.attr.debug_info[k], opt_level = v) - for k, v in ctx.attr.debug_info.items(): + if not k in ctx.attr.strip_level: + fail("Compilation mode {} is not defined in strip_level but is defined opt_level".format(k)) + compilation_mode_opts[k] = struct(debug_info = ctx.attr.debug_info[k], opt_level = opt_level, strip_level = ctx.attr.strip_level[k]) + for k in ctx.attr.debug_info.keys(): if not k in ctx.attr.opt_level: fail("Compilation mode {} is not defined in opt_level but is defined debug_info".format(k)) @@ -778,6 +780,14 @@ rust_toolchain = rule( ), mandatory = True, ), + "strip_level": attr.string_dict( + doc = "Rustc strip levels.", + default = { + "dbg": "none", + "fastbuild": "none", + "opt": "debuginfo", + }, + ), "target_json": attr.string( doc = ("Override the target_triple with a custom target specification. " + "For more details see: https://doc.rust-lang.org/rustc/targets/custom.html"),