Skip to content

Commit

Permalink
Provide pdb file for cydlib/bin on Windows in a pdb_file output group
Browse files Browse the repository at this point in the history
I've chose the `pdb_file` name for the output group since it matches
what `cc_binary` uses
(https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java;l=707)

There is one edge case that is not covered here: if the user passes
`/DEBUG:NONE` in the `rustc_flags` no pdb will be generated and I
imagine the build will fail since it's now missing an output. I guess we
could check the `rustc_flags` for it but I don't know if it's the right
place to do so?
  • Loading branch information
ddeville committed Dec 14, 2021
1 parent c1274b6 commit 32518eb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
22 changes: 18 additions & 4 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,23 @@ def _create_test_launcher(ctx, toolchain, output, env, providers):
if not default_info:
fail("No DefaultInfo provider returned from `rustc_compile_action`")

output_group_info = OutputGroupInfo(
launcher_files = depset(launcher_files),
output = depset([output]),
)

# Binaries on Windows might provide a pdb file via an `OutputGroupInfo` that we need to merge
if toolchain.os == "windows":
for i in range(len(providers)):
if type(providers[i]) == "OutputGroupInfo":
output_group_info = OutputGroupInfo(
launcher_files = output_group_info.launcher_files,
output = output_group_info.output,
pdb_file = providers[i].pdb_file,
)
providers.pop(i)
break

providers.extend([
DefaultInfo(
files = default_info.files,
Expand All @@ -384,10 +401,7 @@ def _create_test_launcher(ctx, toolchain, output, env, providers):
),
executable = launcher,
),
OutputGroupInfo(
launcher_files = depset(launcher_files),
output = depset([output]),
),
output_group_info,
])

return providers
Expand Down
25 changes: 17 additions & 8 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -794,19 +794,26 @@ def rustc_compile_action(

outputs = [crate_info.output]

# For a cdylib that might be added as a dependency to a cc_* target on Windows, it is important to include the
# interface library that rustc generates in the output files.
interface_library = None
if toolchain.os == "windows" and crate_info.type == "cdylib":
# Rustc generates the import library with a `.dll.lib` extension rather than the usual `.lib` one that msvc
# expects (see https://github.com/rust-lang/rust/pull/29520 for more context).
interface_library = ctx.actions.declare_file(crate_info.output.basename + ".lib")
outputs.append(interface_library)
pdb_file = None
if toolchain.os == "windows":
# For a cdylib that might be added as a dependency to a cc_* target on Windows, it is important to include the
# interface library that rustc generates in the output files.
if crate_info.type == "cdylib":
# Rustc generates the import library with a `.dll.lib` extension rather than the usual `.lib` one that msvc
# expects (see https://github.com/rust-lang/rust/pull/29520 for more context).
interface_library = ctx.actions.declare_file(crate_info.output.basename + ".lib")
outputs.append(interface_library)

# Rustc always generates a pdb file so provide it in an output group for crate types that benefit from having
# debug information in a separate file. Note that test targets do really need a pdb we skip them.
if crate_info.type in ("cdylib", "bin") and not crate_info.is_test:
pdb_file = ctx.actions.declare_file(crate_info.output.basename[:-len(crate_info.output.basename.extension)] + "pdb")

ctx.actions.run(
executable = ctx.executable._process_wrapper,
inputs = compile_inputs,
outputs = outputs,
outputs = outputs + [pdb_file] if pdb_file else outputs,
env = env,
arguments = args.all,
mnemonic = "Rustc",
Expand Down Expand Up @@ -839,6 +846,8 @@ def rustc_compile_action(
]
if toolchain.target_arch != "wasm32":
providers += establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_configuration, interface_library)
if pdb_file:
providers.append(OutputGroupInfo(pdb_file = depset([pdb_file])))

return providers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def win_interface_library_analysis_test_suite(name):
target_compatible_with = ["@platforms//os:windows"],
)

native.filegroup(
name = "mylib.pdb",
srcs = [":mylib"],
output_group = "pdb_file",
)

cc_binary(
name = "mybin",
srcs = ["bin.cc"],
Expand Down

0 comments on commit 32518eb

Please sign in to comment.