Skip to content

Commit a20c9e5

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 a20c9e5

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

rust/private/rustc.bzl

+2
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,8 @@ 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+
if toolchain.target_os.startswith("linux") or toolchain.target_os.startswith("none"):
962+
rustc_flags.add(compilation_mode.strip_level, format = "--codegen=strip=%s")
961963

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

rust/toolchain.bzl

+13-3
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,13 @@ 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)
471-
for k, v in ctx.attr.debug_info.items():
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])
473+
for k in ctx.attr.debug_info.keys():
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))
474476

@@ -778,6 +780,14 @@ rust_toolchain = rule(
778780
),
779781
mandatory = True,
780782
),
783+
"strip_level": attr.string_dict(
784+
doc = "Rustc strip levels.",
785+
default = {
786+
"dbg": "none",
787+
"fastbuild": "none",
788+
"opt": "debuginfo",
789+
},
790+
),
781791
"target_json": attr.string(
782792
doc = ("Override the target_triple with a custom target specification. " +
783793
"For more details see: https://doc.rust-lang.org/rustc/targets/custom.html"),

0 commit comments

Comments
 (0)