Skip to content

Commit

Permalink
Add DUMPBIN make variable to MSVC C/C++ toolchain
Browse files Browse the repository at this point in the history
`DUMPBIN` is a WIndows/MSVC tool that provides similar function to `NM` in the Linux world. `NM` already has a make variable, so I think it's fair to add one for `DUMPBIN`.

Tests and docs are updated as well.

RELNOTES[NEW]: New $(DUMPBIN) make variable is now available for Visual Studio toolchains.

Closes #21232.

PiperOrigin-RevId: 611398596
Change-Id: Ibe55d530c0dcace07372206741cc928f7c190dde
  • Loading branch information
alexsharoff authored and copybara-github committed Feb 29, 2024
1 parent 2b3e686 commit 42611f9
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 10 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,6 @@
architectures.
</li>

<li> <code>NM</code>: The "nm" command from crosstool. </li>
<li> <code>OBJCOPY</code>: The objcopy command from the same suite as the C/C++
compiler. </li>

<li> <code>STRIP</code>: The strip command from the same suite as the C/C++
compiler.</li>
</ul>
Expand Down
45 changes: 45 additions & 0 deletions src/test/py/bazel/bazel_windows_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,51 @@ def testTestShardStatusFile(self):
['test', '--incompatible_check_sharding_support', '//:foo_test']
)

def testMakeVariableForDumpbinExecutable(self):
if not self.IsWindows():
return

self.CreateWorkspaceWithDefaultRepos('WORKSPACE')
self.ScratchFile(
'BUILD',
[
'cc_binary(',
' name = "test_dll",',
' linkshared = 1,',
' srcs = ["dllexport.c"],',
')',
'genrule(',
' name = "dumpbin",',
' srcs = [":test_dll"],',
' outs = ["dumpbin_out.txt"],',
# We have to use double quotes due to /S argument in cmd.exe call
(
' cmd_bat = \'""$(DUMPBIN)"" /EXPORTS $(location :test_dll)'
" > $@',"
),
(
' toolchains ='
' ["@bazel_tools//tools/cpp:current_cc_toolchain"],'
),
')',
],
)
self.ScratchFile(
'dllexport.c',
[
'__declspec(dllexport) int windows_dllexport_test() { return 1; }',
],
)

_, stdout, _ = self.RunBazel(['info', 'bazel-bin'])
bazel_bin = stdout[0]

self.RunBazel(['build', ':dumpbin'])

dumpbin_out = os.path.join(bazel_bin, 'dumpbin_out.txt')
self.assertTrue(os.path.exists(dumpbin_out))
self.AssertFileContentContains(dumpbin_out, 'windows_dllexport_test')


if __name__ == '__main__':
absltest.main()
2 changes: 1 addition & 1 deletion src/test/tools/bzlmod/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tools/cpp/BUILD.windows.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ cc_toolchain_config(
"objcopy": "wrapper/bin/msvc_nop.bat",
"objdump": "wrapper/bin/msvc_nop.bat",
"strip": "wrapper/bin/msvc_nop.bat",
"dumpbin": "%{msvc_dumpbin_path_x64}",
},
archiver_flags = ["/MACHINE:X64"],
default_link_flags = ["/MACHINE:X64"],
Expand Down Expand Up @@ -381,6 +382,7 @@ cc_toolchain_config(
"objcopy": "wrapper/bin/msvc_nop.bat",
"objdump": "wrapper/bin/msvc_nop.bat",
"strip": "wrapper/bin/msvc_nop.bat",
"dumpbin": "%{msvc_dumpbin_path_x86}",
},
archiver_flags = ["/MACHINE:X86"],
default_link_flags = ["/MACHINE:X86"],
Expand Down Expand Up @@ -448,6 +450,7 @@ cc_toolchain_config(
"objcopy": "wrapper/bin/msvc_nop.bat",
"objdump": "wrapper/bin/msvc_nop.bat",
"strip": "wrapper/bin/msvc_nop.bat",
"dumpbin": "%{msvc_dumpbin_path_arm}",
},
archiver_flags = ["/MACHINE:ARM"],
default_link_flags = ["/MACHINE:ARM"],
Expand Down Expand Up @@ -515,6 +518,7 @@ cc_toolchain_config(
"objcopy": "wrapper/bin/msvc_nop.bat",
"objdump": "wrapper/bin/msvc_nop.bat",
"strip": "wrapper/bin/msvc_nop.bat",
"dumpbin": "%{msvc_dumpbin_path_arm64}",
},
archiver_flags = ["/MACHINE:ARM64"],
default_link_flags = ["/MACHINE:ARM64"],
Expand Down
9 changes: 5 additions & 4 deletions tools/cpp/windows_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,10 @@ def _find_missing_vc_tools(repository_ctx, vc_path, target_arch = "x64"):
def _get_target_tools(target):
"""Return a list of required tools names and their filenames for a certain target."""
tools = {
"x64": {"CL": "cl.exe", "LINK": "link.exe", "LIB": "lib.exe", "ML": "ml64.exe"},
"x86": {"CL": "cl.exe", "LINK": "link.exe", "LIB": "lib.exe", "ML": "ml.exe"},
"arm": {"CL": "cl.exe", "LINK": "link.exe", "LIB": "lib.exe"},
"arm64": {"CL": "cl.exe", "LINK": "link.exe", "LIB": "lib.exe"},
"x64": {"CL": "cl.exe", "LINK": "link.exe", "LIB": "lib.exe", "DUMPBIN": "dumpbin.exe", "ML": "ml64.exe"},
"x86": {"CL": "cl.exe", "LINK": "link.exe", "LIB": "lib.exe", "DUMPBIN": "dumpbin.exe", "ML": "ml.exe"},
"arm": {"CL": "cl.exe", "LINK": "link.exe", "LIB": "lib.exe", "DUMPBIN": "dumpbin.exe"},
"arm64": {"CL": "cl.exe", "LINK": "link.exe", "LIB": "lib.exe", "DUMPBIN": "dumpbin.exe"},
}
if tools.get(target) == None:
auto_configure_fail("Target architecture %s is not recognized" % target)
Expand Down Expand Up @@ -719,6 +719,7 @@ Fix this by installing the English language pack for the Visual Studio installat
"%{msvc_ml_path_" + target_arch + "}": build_tools.get("ML", "msvc_arm_toolchain_does_not_support_ml"),
"%{msvc_link_path_" + target_arch + "}": build_tools["LINK"],
"%{msvc_lib_path_" + target_arch + "}": build_tools["LIB"],
"%{msvc_dumpbin_path_" + target_arch + "}": build_tools["DUMPBIN"],
"%{msvc_parse_showincludes_" + target_arch + "}": repr(support_parse_showincludes),
"%{dbg_mode_debug_flag_" + target_arch + "}": "/DEBUG:FULL" if support_debug_fastlink else "/DEBUG",
"%{fastbuild_mode_debug_flag_" + target_arch + "}": "/DEBUG:FASTLINK" if support_debug_fastlink else "/DEBUG",
Expand Down
8 changes: 8 additions & 0 deletions tools/cpp/windows_cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ load(
"feature",
"flag_group",
"flag_set",
"make_variable",
"tool",
"tool_path",
"variable_with_value",
Expand Down Expand Up @@ -1394,6 +1395,12 @@ def _impl(ctx):
for name, path in ctx.attr.tool_paths.items()
]

make_variables = []

# dumpbin.exe is not available in MSYS toolchain
if "dumpbin" in ctx.attr.tool_paths:
make_variables.append(make_variable(name = "DUMPBIN", value = ctx.attr.tool_paths["dumpbin"]))

return cc_common.create_cc_toolchain_config_info(
ctx = ctx,
features = features,
Expand All @@ -1409,6 +1416,7 @@ def _impl(ctx):
abi_version = ctx.attr.abi_version,
abi_libc_version = ctx.attr.abi_libc_version,
tool_paths = tool_paths,
make_variables = make_variables,
)

cc_toolchain_config = rule(
Expand Down

0 comments on commit 42611f9

Please sign in to comment.