Skip to content

Commit 2683049

Browse files
committed
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. rust-lang/cargo#4122 (comment)
1 parent 53daac7 commit 2683049

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

rust/private/rustc.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,7 @@ def construct_arguments(
958958
compilation_mode = get_compilation_mode_opts(ctx, toolchain)
959959
rustc_flags.add(compilation_mode.opt_level, format = "--codegen=opt-level=%s")
960960
rustc_flags.add(compilation_mode.debug_info, format = "--codegen=debuginfo=%s")
961+
rustc_flags.add(compilation_mode.strip_level, format = "--codegen=strip=%s")
961962

962963
# For determinism to help with build distribution and such
963964
if remap_path_prefix != None:

rust/toolchain.bzl

+12-2
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,12 @@ def _rust_toolchain_impl(ctx):
464464
list: A list containing the target's toolchain Provider info
465465
"""
466466
compilation_mode_opts = {}
467-
for k, v in ctx.attr.opt_level.items():
467+
for k, opt_level in ctx.attr.opt_level.items():
468468
if not k in ctx.attr.debug_info:
469469
fail("Compilation mode {} is not defined in debug_info but is defined opt_level".format(k))
470-
compilation_mode_opts[k] = struct(debug_info = ctx.attr.debug_info[k], opt_level = v)
470+
if not k in ctx.attr.strip_level:
471+
fail("Compilation mode {} is not defined in strip_level but is defined opt_level".format(k))
472+
compilation_mode_opts[k] = struct(debug_info = ctx.attr.debug_info[k], opt_level = opt_level, strip_level = ctx.attr.strip_level[k])
471473
for k, v in ctx.attr.debug_info.items():
472474
if not k in ctx.attr.opt_level:
473475
fail("Compilation mode {} is not defined in opt_level but is defined debug_info".format(k))
@@ -738,6 +740,14 @@ rust_toolchain = rule(
738740
"opt": "3",
739741
},
740742
),
743+
"strip_level": attr.string_dict(
744+
doc = "Rustc strip levels.",
745+
default = {
746+
"dbg": "none",
747+
"fastbuild": "none",
748+
"opt": "debuginfo",
749+
},
750+
),
741751
"per_crate_rustc_flags": attr.string_list(
742752
doc = "Extra flags to pass to rustc in non-exec configuration",
743753
),

0 commit comments

Comments
 (0)