From 246de8a64cc8b1c477e1011a81db343b38fcae97 Mon Sep 17 00:00:00 2001 From: Matt Epperson Date: Tue, 2 Apr 2024 08:52:43 -0600 Subject: [PATCH] Selectively generate pdb Only generate pdb files for windows if the strip level is set to none. --- rust/private/rustc.bzl | 5 +- .../debug_info/debug_info_analysis_test.bzl | 96 ++++++++++++++----- 2 files changed, 78 insertions(+), 23 deletions(-) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 67fd9f0d3a..6fdc7b920c 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1289,12 +1289,15 @@ def rustc_compile_action( if rustc_output: action_outputs.append(rustc_output) + # Get the compilation mode for the current target. + compilation_mode = get_compilation_mode_opts(ctx, toolchain) + # Rustc generates a pdb file (on Windows) or a dsym folder (on macos) so provide it in an output group for crate # types that benefit from having debug information in a separate file. pdb_file = None dsym_folder = None if crate_info.type in ("cdylib", "bin"): - if toolchain.target_os == "windows": + if toolchain.target_os == "windows" and compilation_mode.strip_level == "none": pdb_file = ctx.actions.declare_file(crate_info.output.basename[:-len(crate_info.output.extension)] + "pdb", sibling = crate_info.output) action_outputs.append(pdb_file) elif toolchain.target_os == "darwin": diff --git a/test/unit/debug_info/debug_info_analysis_test.bzl b/test/unit/debug_info/debug_info_analysis_test.bzl index 361640db59..06a05b4f93 100644 --- a/test/unit/debug_info/debug_info_analysis_test.bzl +++ b/test/unit/debug_info/debug_info_analysis_test.bzl @@ -3,18 +3,61 @@ load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") load("//rust:defs.bzl", "rust_binary", "rust_shared_library", "rust_test") -def _pdb_file_test_impl(ctx): +def _pdb_file_test_impl(ctx, expect_pdb_file): env = analysistest.begin(ctx) target = analysistest.target_under_test(env) - files = target[DefaultInfo].files.to_list() + + if not expect_pdb_file: + asserts.equals(env, len(files), 0) + return analysistest.end(env) + asserts.equals(env, len(files), 1) file = files[0] asserts.equals(env, file.extension, "pdb") - return analysistest.end(env) -pdb_file_test = analysistest.make(_pdb_file_test_impl) + + +def _pdb_file_for_dbg_test_impl(ctx): + """Test for dbg compilation mode.""" + return _pdb_file_test_impl(ctx, True) + +pdb_file_dbg_test = analysistest.make( + _pdb_file_for_dbg_test_impl, + config_settings = { + "//command_line_option:compilation_mode": "dbg", + } +) + +def _pdb_file_for_fastbuild_test_impl(ctx): + """Test for fastbuild compilation mode.""" + return _pdb_file_test_impl(ctx, True) + +pdb_file_fastbuild_test = analysistest.make( + _pdb_file_for_fastbuild_test_impl, + config_settings = { + "//command_line_option:compilation_mode": "fastbuild", + } +) + +def _pdb_file_for_opt_test_impl(ctx): + """Test for opt compilation mode.""" + return _pdb_file_test_impl(ctx, False) + +pdb_file_opt_test = analysistest.make( + _pdb_file_for_opt_test_impl, + config_settings = { + "//command_line_option:compilation_mode": "opt", + } +) + +# Mapping from compilation mode to pdb file test. +pdb_file_tests = { + "dbg": pdb_file_dbg_test, + "fastbuild": pdb_file_fastbuild_test, + "opt": pdb_file_opt_test +} def _dsym_folder_test_impl(ctx): env = analysistest.begin(ctx) @@ -47,11 +90,12 @@ def debug_info_analysis_test_suite(name): output_group = "pdb_file", ) - pdb_file_test( - name = "lib_pdb_test", - target_under_test = ":mylib.pdb", - target_compatible_with = ["@platforms//os:windows"], - ) + for compilation_mode, pdb_test in pdb_file_tests.items(): + pdb_test( + name = "lib_pdb_test_{}".format(compilation_mode), + target_under_test = ":mylib.pdb", + target_compatible_with = ["@platforms//os:windows"], + ) native.filegroup( name = "mylib.dSYM", @@ -77,11 +121,12 @@ def debug_info_analysis_test_suite(name): output_group = "pdb_file", ) - pdb_file_test( - name = "bin_pdb_test", - target_under_test = ":mybin.pdb", - target_compatible_with = ["@platforms//os:windows"], - ) + for compilation_mode, pdb_test in pdb_file_tests.items(): + pdb_test( + name = "bin_pdb_test_{}".format(compilation_mode), + target_under_test = ":mybin.pdb", + target_compatible_with = ["@platforms//os:windows"], + ) native.filegroup( name = "mybin.dSYM", @@ -108,11 +153,12 @@ def debug_info_analysis_test_suite(name): testonly = True, ) - pdb_file_test( - name = "test_pdb_test", - target_under_test = ":mytest.pdb", - target_compatible_with = ["@platforms//os:windows"], - ) + for compilation_mode, pdb_test in pdb_file_tests.items(): + pdb_test( + name = "test_pdb_test_{}".format(compilation_mode), + target_under_test = ":mytest.pdb", + target_compatible_with = ["@platforms//os:windows"], + ) native.filegroup( name = "mytest.dSYM", @@ -130,11 +176,17 @@ def debug_info_analysis_test_suite(name): native.test_suite( name = name, tests = [ - ":lib_pdb_test", ":lib_dsym_test", - ":bin_pdb_test", ":bin_dsym_test", - ":test_pdb_test", ":test_dsym_test", + ] + [ + ":lib_pdb_test_{}".format(compilation_mode) + for compilation_mode in pdb_file_tests + ] + [ + ":bin_pdb_test_{}".format(compilation_mode) + for compilation_mode in pdb_file_tests + ] + [ + ":test_pdb_test_{}".format(compilation_mode) + for compilation_mode in pdb_file_tests ], )