Skip to content

Commit

Permalink
feat: Strip debug info from opt builds
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
matte1 committed Feb 22, 2024
1 parent 53daac7 commit df91f38
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 13 additions & 3 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit df91f38

Please sign in to comment.