Skip to content

Commit

Permalink
Add support for feature generate_linkmap to cc_binary
Browse files Browse the repository at this point in the history
See #20966.

~I'm not entirely sure how to test the unix-only functionality.~

Closes #20967.

PiperOrigin-RevId: 616728419
Change-Id: I0b8eaa8245becebe2047bfc2bd7725f3fd45b8f1
  • Loading branch information
t-8ch authored and copybara-github committed Mar 18, 2024
1 parent 7a3f563 commit e6b8adb
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public static ToolchainTypeRequirement ccToolchainTypeRequirement(RuleDefinition
*/
public static final String NO_GENERATE_DEBUG_SYMBOLS_FEATURE_NAME = "no_generate_debug_symbols";

/** A feature to indicate whether to generate linkmap. For Apple platform only. */
/** A feature to indicate whether to generate linkmap. */
public static final String GENERATE_LINKMAP_FEATURE_NAME = "generate_linkmap";

/** A feature to indicate whether to do linker deadstrip. For Apple platform only. */
Expand Down
14 changes: 13 additions & 1 deletion src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ def _create_transitive_linking_actions(
pdb_file,
win_def_file,
additional_linkopts,
additional_make_variable_substitutions):
additional_make_variable_substitutions,
additional_outputs):
cc_compilation_outputs_with_only_objects = cc_common.create_compilation_outputs(objects = None, pic_objects = None)
deps_cc_info = CcInfo(linking_context = deps_cc_linking_context)
libraries_for_current_cc_linking_context = []
Expand Down Expand Up @@ -515,6 +516,7 @@ def _create_transitive_linking_actions(
never_link = True,
pdb_file = pdb_file,
win_def_file = win_def_file,
additional_outputs = additional_outputs,
)
cc_launcher_info = cc_internal.create_cc_launcher_info(cc_info = cc_info_without_extra_link_time_libraries, compilation_outputs = cc_compilation_outputs_with_only_objects)
return (cc_linking_outputs, cc_launcher_info, cc_linking_context)
Expand Down Expand Up @@ -716,6 +718,13 @@ def cc_binary_impl(ctx, additional_linkopts, force_linkstatic = False):
if cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "generate_pdb_file"):
pdb_file = ctx.actions.declare_file(_strip_extension(binary) + ".pdb", sibling = binary)

additional_linker_outputs = []

linkmap = None
if cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "generate_linkmap"):
linkmap = ctx.actions.declare_file(binary.basename + ".map", sibling = binary)
additional_linker_outputs.append(linkmap)

extra_link_time_libraries = deps_cc_linking_context.extra_link_time_libraries()
linker_inputs_extra = depset()
runtime_libraries_extra = depset()
Expand All @@ -740,6 +749,7 @@ def cc_binary_impl(ctx, additional_linkopts, force_linkstatic = False):
win_def_file,
additional_linkopts,
additional_make_variable_substitutions,
additional_linker_outputs,
)

cc_linking_outputs_binary_library = cc_linking_outputs_binary.library_to_link
Expand Down Expand Up @@ -832,6 +842,8 @@ def cc_binary_impl(ctx, additional_linkopts, force_linkstatic = False):
output_groups["pdb_file"] = depset([pdb_file])
if generated_def_file != None:
output_groups["def_file"] = depset([generated_def_file])
if linkmap:
output_groups["linkmap"] = depset([linkmap])

if cc_linking_outputs_binary_library != None:
# For consistency and readability.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

""" A rule that mocks cc_toolchain configuration."""

load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load(
"@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
"action_config",
Expand All @@ -29,7 +30,6 @@ load(
"tool_path",
"with_feature_set",
)
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")

_FEATURE_NAMES = struct(
generate_pdb_file = "generate_pdb_file",
Expand Down Expand Up @@ -117,6 +117,7 @@ _FEATURE_NAMES = struct(
optional_cc_flags_feature = "optional_cc_flags_feature",
cpp_compile_with_requirements = "cpp_compile_with_requirements",
no_copts_tokenization = "no_copts_tokenization",
generate_linkmap = "generate_linkmap",
)

_no_copts_tokenization_feature = feature(name = _FEATURE_NAMES.no_copts_tokenization)
Expand Down Expand Up @@ -1313,6 +1314,21 @@ _layering_check_module_maps_header_modules_simple_features = [
),
]

_generate_linkmap_feature = feature(
name = _FEATURE_NAMES.generate_linkmap,
flag_sets = [
flag_set(
actions = [ACTION_NAMES.cpp_link_executable],
flag_groups = [
flag_group(
flags = ["-linkmap=%{output_execpath}.map"],
expand_if_available = "output_execpath",
),
],
),
],
)

_feature_name_to_feature = {
_FEATURE_NAMES.no_legacy_features: _no_legacy_features_feature,
_FEATURE_NAMES.do_not_split_linking_cmdline: _do_not_split_linking_cmdline_feature,
Expand Down Expand Up @@ -1388,6 +1404,7 @@ _feature_name_to_feature = {
_FEATURE_NAMES.optional_cc_flags_feature: _optional_cc_flags_feature,
_FEATURE_NAMES.cpp_compile_with_requirements: _cpp_compile_with_requirements,
_FEATURE_NAMES.generate_pdb_file: _generate_pdb_file_feature,
_FEATURE_NAMES.generate_linkmap: _generate_linkmap_feature,
"header_modules_feature_configuration": _header_modules_feature_configuration,
"env_var_feature_configuration": _env_var_feature_configuration,
"host_and_nonhost_configuration": _host_and_nonhost_configuration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1328,4 +1328,25 @@ public void testNoLinkExtra() throws Exception {
List<String> artifactNames = baseArtifactNames(getLinkerInputs(target));
assertThat(artifactNames).doesNotContain("liblink_extra_lib.a");
}

@Test
public void testGenerateLinkMap() throws Exception {
AnalysisMock.get()
.ccSupport()
.setupCcToolchainConfig(
mockToolsConfig,
CcToolchainConfig.builder().withFeatures(CppRuleClasses.GENERATE_LINKMAP_FEATURE_NAME));
useConfiguration("--cpu=k8");
ConfiguredTarget generateLinkMapTest =
scratchConfiguredTarget(
"generate_linkmap",
"generate_linkmap_test",
"cc_binary(name = 'generate_linkmap_test',",
" features = ['generate_linkmap'],",
" srcs = ['generate_linkmap_test.cc'],",
" )");
Iterable<String> temps =
ActionsTestUtil.baseArtifactNames(getOutputGroup(generateLinkMapTest, "linkmap"));
assertThat(temps).containsExactly("generate_linkmap_test.map");
}
}
21 changes: 21 additions & 0 deletions tools/cpp/unix_cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,25 @@ def _impl(ctx):
],
)

generate_linkmap_feature = feature(
name = "generate_linkmap",
flag_sets = [
flag_set(
actions = [
ACTION_NAMES.cpp_link_executable,
],
flag_groups = [
flag_group(
flags = [
"-Wl,-Map=%{output_execpath}.map" if is_linux else "-Wl,-map,%{output_execpath}.map",
],
expand_if_available = "output_execpath",
),
],
),
],
)

output_execpath_flags_feature = feature(
name = "output_execpath_flags",
flag_sets = [
Expand Down Expand Up @@ -1384,6 +1403,7 @@ def _impl(ctx):
autofdo_feature,
build_interface_libraries_feature,
dynamic_library_linker_tool_feature,
generate_linkmap_feature,
shared_flag_feature,
linkstamps_feature,
output_execpath_flags_feature,
Expand Down Expand Up @@ -1458,6 +1478,7 @@ def _impl(ctx):
unfiltered_compile_flags_feature,
treat_warnings_as_errors_feature,
archive_param_file_feature,
generate_linkmap_feature,
]

return cc_common.create_cc_toolchain_config_info(
Expand Down
20 changes: 20 additions & 0 deletions tools/cpp/windows_cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,25 @@ def _impl(ctx):
name = "generate_pdb_file",
)

generate_linkmap_feature = feature(
name = "generate_linkmap",
flag_sets = [
flag_set(
actions = [
ACTION_NAMES.cpp_link_executable,
],
flag_groups = [
flag_group(
flags = [
"/MAP:%{output_execpath}.map",
],
expand_if_available = "output_execpath",
),
],
),
],
)

output_execpath_flags_feature = feature(
name = "output_execpath_flags",
flag_sets = [
Expand Down Expand Up @@ -1123,6 +1142,7 @@ def _impl(ctx):
parse_showincludes_feature,
no_dotd_file_feature,
generate_pdb_file_feature,
generate_linkmap_feature,
shared_flag_feature,
linkstamps_feature,
output_execpath_flags_feature,
Expand Down

0 comments on commit e6b8adb

Please sign in to comment.