Skip to content

Commit cf5d057

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 fc601ba commit cf5d057

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

rust/private/rustc.bzl

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

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

rust/toolchain.bzl

+13-3
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,13 @@ def _rust_toolchain_impl(ctx):
490490
list: A list containing the target's toolchain Provider info
491491
"""
492492
compilation_mode_opts = {}
493-
for k, v in ctx.attr.opt_level.items():
493+
for k, opt_level in ctx.attr.opt_level.items():
494494
if not k in ctx.attr.debug_info:
495495
fail("Compilation mode {} is not defined in debug_info but is defined opt_level".format(k))
496-
compilation_mode_opts[k] = struct(debug_info = ctx.attr.debug_info[k], opt_level = v)
497-
for k, v in ctx.attr.debug_info.items():
496+
if not k in ctx.attr.strip_level:
497+
fail("Compilation mode {} is not defined in strip_level but is defined opt_level".format(k))
498+
compilation_mode_opts[k] = struct(debug_info = ctx.attr.debug_info[k], opt_level = opt_level, strip_level = ctx.attr.strip_level[k])
499+
for k in ctx.attr.debug_info.keys():
498500
if not k in ctx.attr.opt_level:
499501
fail("Compilation mode {} is not defined in opt_level but is defined debug_info".format(k))
500502

@@ -813,6 +815,14 @@ rust_toolchain = rule(
813815
),
814816
mandatory = True,
815817
),
818+
"strip_level": attr.string_dict(
819+
doc = "Rustc strip levels.",
820+
default = {
821+
"dbg": "none",
822+
"fastbuild": "none",
823+
"opt": "debuginfo",
824+
},
825+
),
816826
"target_json": attr.string(
817827
doc = ("Override the target_triple with a custom target specification. " +
818828
"For more details see: https://doc.rust-lang.org/rustc/targets/custom.html"),

0 commit comments

Comments
 (0)