Skip to content

Commit cb760f6

Browse files
committed
Add generate_linkmap attribute support
1 parent 39dc56e commit cb760f6

9 files changed

Lines changed: 131 additions & 28 deletions

File tree

apple/internal/linking_support.bzl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def _link_multi_arch_binary(
298298
legacy_debug_outputs.setdefault(platform_info.target_arch, {})["dsym_binary"] = dsym_binary
299299

300300
linkmap = None
301-
if ctx.fragments.cpp.objc_generate_linkmap:
301+
if ctx.attr.generate_linkmap or ctx.fragments.cpp.objc_generate_linkmap:
302302
linkmap = intermediates.file(
303303
actions = ctx.actions,
304304
target_name = ctx.label.name,
@@ -371,7 +371,8 @@ def _debug_outputs_by_architecture(link_outputs):
371371

372372
for link_output in link_outputs:
373373
dsym_binaries[link_output.architecture] = link_output.dsym_binary
374-
linkmaps[link_output.architecture] = link_output.linkmap
374+
if link_output.linkmap:
375+
linkmaps[link_output.architecture] = link_output.linkmap
375376

376377
return struct(
377378
dsym_binaries = dsym_binaries,
@@ -548,13 +549,17 @@ def _register_binary_linking_action(
548549
linkopts.extend(["-bundle_loader", bundle_loader_file.path])
549550
link_inputs.append(bundle_loader_file)
550551

552+
requested_features = list(extra_requested_features)
553+
if ctx.attr.generate_linkmap or ctx.fragments.cpp.objc_generate_linkmap:
554+
requested_features.append("generate_linkmap")
555+
551556
linking_outputs = _link_multi_arch_binary(
552557
ctx = ctx,
553558
avoid_deps = all_avoid_deps,
554559
cc_toolchains = cc_toolchains,
555560
extra_linkopts = linkopts,
556561
extra_link_inputs = link_inputs,
557-
extra_requested_features = extra_requested_features,
562+
extra_requested_features = requested_features,
558563
# TODO(321109350): Disable include scanning to work around issue with GrepIncludes actions
559564
# being routed to the wrong exec platform.
560565
extra_disabled_features = extra_disabled_features + ["cc_include_scanning"],

apple/internal/partials/debug_symbols.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def _debug_symbols_partial_impl(
433433
direct_dsym_bundles.append(dsym_bundle_dir)
434434
direct_dsyms.extend(dsym_files)
435435

436-
if platform_prerequisites.cpp_fragment.objc_generate_linkmap:
436+
if linkmaps:
437437
linkmaps = _collect_linkmaps(
438438
actions = actions,
439439
debug_output_filename = debug_output_filename,

apple/internal/providers/apple_debug_info.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ partial and the resource aspect.
2626
Depset of `File` references to dSYM files if requested in the build with --apple_generate_dsym.
2727
""",
2828
"linkmaps": """
29-
Depset of `File` references to linkmap files if requested in the build with --objc_generate_linkmap.
29+
Depset of `File` references to linkmap files when linkmap generation is requested.
3030
""",
3131
},
3232
)

apple/internal/rule_attrs.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ were marked as `__private_extern__` (aka `visibility=hidden`) and will not be gl
213213
file.
214214
215215
See the man page documentation for `ld(1)` on macOS for more details.
216+
""",
217+
),
218+
"generate_linkmap": attr.bool(
219+
default = False,
220+
doc = """
221+
Whether to generate a linkmap file for this target's linked binary.
222+
223+
This attribute augments `--objc_generate_linkmap`; if the command-line flag is set, a linkmap is
224+
still generated even when this attribute is `False`.
216225
""",
217226
),
218227
"linkopts": attr.string_list(

doc/common_info.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ which files are requested:
3434
* `linkmaps`: This output group contains all linkmap files generated during
3535
the build, for the top level target **and** its embedded dependencies. To
3636
request this output group to be built, use the `--output_groups=+linkmaps`
37-
flag. In order to generate the linkmap files you still need to pass the
38-
`--objc_generate_linkmap` flag.
37+
flag. Linkmap generation can be requested either per-target with the
38+
`generate_linkmap` attribute or globally with the `--objc_generate_linkmap`
39+
flag.
3940

4041
### dSYMs Generation {#apple_generate_dsym}
4142

@@ -116,12 +117,23 @@ flags yourself.
116117

117118
Linkmaps can be useful for figuring out how the `deps` going into a target are
118119
contributing to the final size of the binary. Bazel will generate a link map
119-
when linking by adding `--objc_generate_linkmap` to a `bazel build`.
120+
when linking if either the rule's `generate_linkmap` attribute is `True` or
121+
`--objc_generate_linkmap` is added to a `bazel build`.
120122

121123
```shell
122124
bazel build --objc_generate_linkmap //your/target
123125
```
124126

127+
Or:
128+
129+
```bzl
130+
ios_application(
131+
name = "app",
132+
generate_linkmap = True,
133+
# ...
134+
)
135+
```
136+
125137
By default, only the top level linkmap file is built when this flag is
126138
specified. If you require the linkmap file of the top level target dependencies,
127139
you'll need to specify the `--output_groups=+linkmaps` flag.

test/starlark_tests/ios_application_tests.bzl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ load(
3232
)
3333
load(
3434
"//test/starlark_tests/rules:analysis_output_group_info_files_test.bzl",
35+
"analysis_output_group_info_files_attr_test",
3536
"analysis_output_group_info_files_test",
3637
)
3738
load(
@@ -71,6 +72,7 @@ load(
7172
)
7273
load(
7374
"//test/starlark_tests/rules:linkmap_test.bzl",
75+
"linkmap_attr_test",
7476
"linkmap_test",
7577
)
7678
load(
@@ -1009,6 +1011,32 @@ def ios_application_test_suite(name):
10091011
],
10101012
tags = [name],
10111013
)
1014+
linkmap_attr_test(
1015+
name = "{}_generate_linkmap_attr_test".format(name),
1016+
target_under_test = "//test/starlark_tests/targets_under_test/ios:app_with_generate_linkmap",
1017+
expected_linkmap_names = ["app_with_generate_linkmap"],
1018+
tags = [name],
1019+
)
1020+
analysis_output_group_info_files_attr_test(
1021+
name = "{}_generate_linkmap_attr_output_group_info_test".format(name),
1022+
target_under_test = "//test/starlark_tests/targets_under_test/ios:app_with_generate_linkmap",
1023+
output_group_name = "linkmaps",
1024+
expected_outputs = [
1025+
"app_with_generate_linkmap_x86_64.linkmap",
1026+
"app_with_generate_linkmap_arm64.linkmap",
1027+
],
1028+
tags = [name],
1029+
)
1030+
action_command_line_test(
1031+
name = "{}_generate_linkmap_attr_link_action_test".format(name),
1032+
target_under_test = "//test/starlark_tests/targets_under_test/ios:app_with_generate_linkmap",
1033+
mnemonic = "ObjcLink",
1034+
expected_argv = [
1035+
"-Xlinker -map",
1036+
"app_with_generate_linkmap.linkmap",
1037+
],
1038+
tags = [name],
1039+
)
10121040

10131041
# Test that Bitcode was removed from the imported framework when building
10141042
# with Bitcode disabled.

test/starlark_tests/rules/analysis_output_group_info_files_test.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,7 @@ def make_analysis_output_group_info_files_test(config_settings = {}):
7272
)
7373

7474
analysis_output_group_info_files_test = make_analysis_output_group_info_files_test()
75+
76+
analysis_output_group_info_files_attr_test = make_analysis_output_group_info_files_test(
77+
config_settings = {},
78+
)

test/starlark_tests/rules/linkmap_test.bzl

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,33 +64,41 @@ def _linkmap_test_impl(ctx):
6464

6565
return analysistest.end(env)
6666

67-
linkmap_test = analysistest.make(
68-
_linkmap_test_impl,
69-
attrs = {
70-
"architectures": attr.string_list(
71-
mandatory = False,
72-
default = [],
73-
doc = """
67+
def make_linkmap_test(config_settings = {}):
68+
return analysistest.make(
69+
_linkmap_test_impl,
70+
attrs = {
71+
"architectures": attr.string_list(
72+
mandatory = False,
73+
default = [],
74+
doc = """
7475
List of architectures to verify for the given dSYM bundles as provided. Defaults to x86_64 for all
7576
platforms.
7677
""",
77-
),
78-
"expected_linkmap_names": attr.string_list(
79-
mandatory = False,
80-
default = [],
81-
doc = """
78+
),
79+
"expected_linkmap_names": attr.string_list(
80+
mandatory = False,
81+
default = [],
82+
doc = """
8283
List of linkmap names to verify that linkmaps are created. Defaults to the target name if none is
8384
provided.
8485
""",
85-
),
86-
},
86+
),
87+
},
88+
config_settings = {
89+
"//command_line_option:macos_cpus": "arm64,x86_64",
90+
"//command_line_option:ios_multi_cpus": "sim_arm64,x86_64",
91+
"//command_line_option:tvos_cpus": "sim_arm64,x86_64",
92+
"//command_line_option:visionos_cpus": "sim_arm64",
93+
"//command_line_option:watchos_cpus": "arm64,x86_64",
94+
} | config_settings,
95+
fragments = ["apple"],
96+
)
97+
98+
linkmap_test = make_linkmap_test(
8799
config_settings = {
88100
"//command_line_option:objc_generate_linkmap": "true",
89-
"//command_line_option:macos_cpus": "arm64,x86_64",
90-
"//command_line_option:ios_multi_cpus": "sim_arm64,x86_64",
91-
"//command_line_option:tvos_cpus": "sim_arm64,x86_64",
92-
"//command_line_option:visionos_cpus": "sim_arm64",
93-
"//command_line_option:watchos_cpus": "arm64,x86_64",
94101
},
95-
fragments = ["apple"],
96102
)
103+
104+
linkmap_attr_test = make_linkmap_test()

test/starlark_tests/targets_under_test/ios/BUILD

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,43 @@ ios_application(
482482
],
483483
)
484484

485+
ios_application(
486+
name = "app_with_generate_linkmap",
487+
app_icons = ["//test/testdata/resources:app_icons_ios"],
488+
bundle_id = "com.google.example",
489+
entitlements = "//test/starlark_tests/resources:entitlements.plist",
490+
families = ["iphone"],
491+
generate_linkmap = True,
492+
infoplists = [
493+
"//test/starlark_tests/resources:Info.plist",
494+
],
495+
launch_storyboard = "//test/starlark_tests/resources:launch_screen_ios.storyboard",
496+
minimum_os_version = common.min_os_ios.baseline,
497+
provisioning_profile = "//test/testdata/provisioning:integration_testing_ios.mobileprovision",
498+
resources = [
499+
"//test/starlark_tests/resources:example_filegroup",
500+
"//test/starlark_tests/resources:localization",
501+
"//test/starlark_tests/resources:resource_bundle",
502+
],
503+
settings_bundle = "//test/starlark_tests/resources:settings_bundle_ios",
504+
tags = common.fixture_tags,
505+
visibility = [
506+
"//test/starlark_tests:__subpackages__",
507+
"//tools/dossier_codesigningtool:__pkg__",
508+
],
509+
deps = [
510+
"//test/starlark_tests/resources:basic_bundle_lib",
511+
"//test/starlark_tests/resources:bundle_library_ios_lib",
512+
"//test/starlark_tests/resources:empty_strings_file_lib",
513+
"//test/starlark_tests/resources:ios_localized_assets_lib",
514+
"//test/starlark_tests/resources:ios_non_localized_assets_lib",
515+
"//test/starlark_tests/resources:nested_bundle_lib",
516+
"//test/starlark_tests/resources:objc_linkopt_lib",
517+
"//test/starlark_tests/resources:objc_main_lib",
518+
"//test/starlark_tests/resources:sticker_pack_ios_lib",
519+
],
520+
)
521+
485522
ios_application(
486523
name = "app_with_bundle_library_dependency",
487524
bundle_id = "com.google.example",

0 commit comments

Comments
 (0)