Skip to content

Commit

Permalink
Generate .pdb files when building cc_shared_library targets for Windows
Browse files Browse the repository at this point in the history
This enables the functionality to generate .pdb files when building a DLL from a cc_shared_library target with the Lexan toolchain, which is already supported when using a cc_binary target.

The change has been tested manually with a local cc_shared_library target, with and without the shared_lib_name attribute.

I'm not exactly sure how the automated/unit tests for cc_shared_library work, but it seems like when the test targets in //third_party/bazel/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/BUILD.builtin_test are built for Windows, the pdb logic should also be executed, so if there are failures they should be caught. A test that would make sense is to check the filenames for the generated pdb files, but I don't see tests that do this for DLLs so I assume we're not doing that for Windows.

PiperOrigin-RevId: 588441321
Change-Id: I20aa6d855514d3864c7f2f4b658f279367128030
  • Loading branch information
Googler authored and copybara-github committed Dec 6, 2023
1 parent c96fbc5 commit d160b12
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ load(":common/cc/cc_helper.bzl", "cc_helper")
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/cc/cc_shared_library_hint_info.bzl", "CcSharedLibraryHintInfo")
load(":common/cc/semantics.bzl", "semantics")
load(":common/paths.bzl", "paths")
load(":common/proto/proto_info.bzl", "ProtoInfo")

# TODO(#5200): Add export_define to library_to_link and cc_library
Expand Down Expand Up @@ -682,6 +683,13 @@ def _cc_shared_library_impl(ctx):
if ctx.attr.shared_lib_name:
main_output = ctx.actions.declare_file(ctx.attr.shared_lib_name)

pdb_file = None
if cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "generate_pdb_file"):
if ctx.attr.shared_lib_name:
pdb_file = ctx.actions.declare_file(paths.replace_extension(ctx.attr.shared_lib_name, ".pdb"))
else:
pdb_file = ctx.actions.declare_file(ctx.label.name + ".pdb")

win_def_file = None
if cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "targets_windows"):
object_files = []
Expand Down Expand Up @@ -714,6 +722,7 @@ def _cc_shared_library_impl(ctx):
name = ctx.label.name,
output_type = "dynamic_library",
main_output = main_output,
pdb_file = pdb_file,
win_def_file = win_def_file,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ load(
"nocode_cc_lib",
"wrapped_cc_lib",
"exports_test",
"pdb_test",
)
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
load("@rules_python//python:py_test.bzl", "py_test")
Expand Down Expand Up @@ -119,7 +120,10 @@ cc_shared_library(
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3:diff_pkg_so",
"private_lib_so",
],
features = ["windows_export_all_symbols"],
features = [
"windows_export_all_symbols",
"generate_pdb_file",
],
exports_filter = [
":indirect_dep2",
],
Expand Down Expand Up @@ -519,3 +523,8 @@ exports_test(
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:a_suffix",
],
)

pdb_test(
name = "pdb_test",
target = ":foo_so",
)
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,31 @@ def _exports_test_macro(name, target, targets_that_should_be_claimed_to_be_expor
)

exports_test = _exports_test_macro

def _pdb_test_impl(env, target):
if not env.ctx.target_platform_has_constraint(env.ctx.attr._is_windows[platform_common.ConstraintValueInfo]):
return

target_action = None
for action in target.actions:
if action.mnemonic == "CppLink":
target_action = action
break

outputs = [f.basename for f in target_action.outputs.to_list()]

env.expect.that_collection(outputs).contains_at_least_predicates([
matching.contains("foo_so.pdb"),
])

def _pdb_test_macro(name, target):
analysis_test(
name = name,
impl = _pdb_test_impl,
target = target,
attrs = {
"_is_windows": attr.label(default = "@platforms//os:windows"),
},
)

pdb_test = _pdb_test_macro

0 comments on commit d160b12

Please sign in to comment.