Skip to content

Commit 4cc3efd

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

16 files changed

Lines changed: 875 additions & 313 deletions

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(

apple/internal/xcframework_rules.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ def _group_link_outputs_by_library_identifier(
275275
# static library linking does not support dsym, and linkmaps yet.
276276
if linking_type == "binary":
277277
dsym_binaries[link_output.architecture] = link_output.dsym_binary
278-
linkmaps[link_output.architecture] = link_output.linkmap
278+
if link_output.linkmap:
279+
linkmaps[link_output.architecture] = link_output.linkmap
279280

280281
environment = link_outputs[0].environment
281282
platform = link_outputs[0].platform

doc/common_info.md

Lines changed: 34 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.
@@ -260,6 +272,25 @@ ios_application(
260272
)
261273
```
262274

275+
### Running applications
276+
277+
When using `bazel run` with Apple application targets, the generated simulator
278+
and device runners support additional control via the
279+
`BAZEL_APPLE_RUN_MODE` environment variable.
280+
281+
Supported values are:
282+
283+
* `install_and_run` (default): Terminates any existing instance, installs the
284+
app, and launches it.
285+
* `install_without_running`: Terminates any existing instance, installs the
286+
app, and exits without launching it.
287+
* `run_without_installing`: Terminates any existing instance and launches the
288+
existing installed app without reinstalling it.
289+
290+
If `BAZEL_APPLE_RUN_MODE=install_without_running` is used together with
291+
`BAZEL_APPLE_LAUNCH_INFO_PATH`, the runner removes any stale launch info file
292+
and does not write a new one, since no process is launched.
293+
263294
### Localization Handling
264295

265296
The Apple bundling rules have two flags for limiting which \*.lproj directories

doc/rules-apple.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,9 @@ load("@rules_apple//apple:apple.bzl", "apple_xcframework")
301301

302302
apple_xcframework(<a href="#apple_xcframework-name">name</a>, <a href="#apple_xcframework-deps">deps</a>, <a href="#apple_xcframework-data">data</a>, <a href="#apple_xcframework-additional_linker_inputs">additional_linker_inputs</a>, <a href="#apple_xcframework-bundle_id">bundle_id</a>, <a href="#apple_xcframework-bundle_name">bundle_name</a>,
303303
<a href="#apple_xcframework-codesign_inputs">codesign_inputs</a>, <a href="#apple_xcframework-codesignopts">codesignopts</a>, <a href="#apple_xcframework-exported_symbols_lists">exported_symbols_lists</a>, <a href="#apple_xcframework-extension_safe">extension_safe</a>,
304-
<a href="#apple_xcframework-families_required">families_required</a>, <a href="#apple_xcframework-infoplists">infoplists</a>, <a href="#apple_xcframework-ios">ios</a>, <a href="#apple_xcframework-linkopts">linkopts</a>, <a href="#apple_xcframework-macos">macos</a>, <a href="#apple_xcframework-minimum_deployment_os_versions">minimum_deployment_os_versions</a>,
305-
<a href="#apple_xcframework-minimum_os_versions">minimum_os_versions</a>, <a href="#apple_xcframework-public_hdrs">public_hdrs</a>, <a href="#apple_xcframework-stamp">stamp</a>, <a href="#apple_xcframework-tvos">tvos</a>, <a href="#apple_xcframework-umbrella_header">umbrella_header</a>, <a href="#apple_xcframework-version">version</a>, <a href="#apple_xcframework-visionos">visionos</a>)
304+
<a href="#apple_xcframework-families_required">families_required</a>, <a href="#apple_xcframework-generate_linkmap">generate_linkmap</a>, <a href="#apple_xcframework-infoplists">infoplists</a>, <a href="#apple_xcframework-ios">ios</a>, <a href="#apple_xcframework-linkopts">linkopts</a>, <a href="#apple_xcframework-macos">macos</a>,
305+
<a href="#apple_xcframework-minimum_deployment_os_versions">minimum_deployment_os_versions</a>, <a href="#apple_xcframework-minimum_os_versions">minimum_os_versions</a>, <a href="#apple_xcframework-public_hdrs">public_hdrs</a>, <a href="#apple_xcframework-stamp">stamp</a>, <a href="#apple_xcframework-tvos">tvos</a>,
306+
<a href="#apple_xcframework-umbrella_header">umbrella_header</a>, <a href="#apple_xcframework-version">version</a>, <a href="#apple_xcframework-visionos">visionos</a>)
306307
</pre>
307308

308309
Builds and bundles an XCFramework for third-party distribution.
@@ -322,7 +323,8 @@ Builds and bundles an XCFramework for third-party distribution.
322323
| <a id="apple_xcframework-codesignopts"></a>codesignopts | A list of strings representing extra flags that should be passed to `codesign`. | List of strings | optional | `[]` |
323324
| <a id="apple_xcframework-exported_symbols_lists"></a>exported_symbols_lists | A list of targets containing exported symbols lists files for the linker to control symbol resolution.<br><br>Each file is expected to have a list of global symbol names that will remain as global symbols in the compiled binary owned by this framework. All other global symbols will be treated as if they were marked as `__private_extern__` (aka `visibility=hidden`) and will not be global in the output file.<br><br>See the man page documentation for `ld(1)` on macOS for more details. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
324325
| <a id="apple_xcframework-extension_safe"></a>extension_safe | If true, compiles and links this framework with `-application-extension`, restricting the binary to use only extension-safe APIs. | Boolean | optional | `False` |
325-
| <a id="apple_xcframework-families_required"></a>families_required | A list of device families supported by this extension, with platforms such as `ios` as keys. Valid values are `iphone` and `ipad` for `ios`; at least one must be specified if a platform is defined. Currently, this only affects processing of `ios` resources. | <a href="https://bazel.build/rules/lib/core/dict">Dictionary: String -> List of strings</a> | optional | `{}` |
326+
| <a id="apple_xcframework-families_required"></a>families_required | A list of device families supported by this extension, with platforms such as `ios` as keys. Valid values are `iphone` and `ipad` for `ios`; at least one must be specified if a platform is defined. Currently, this only affects processing of `ios` resources. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> List of strings</a> | optional | `{}` |
327+
| <a id="apple_xcframework-generate_linkmap"></a>generate_linkmap | Whether to generate a linkmap file for this target's linked binary.<br><br>This attribute augments `--objc_generate_linkmap`; if the command-line flag is set, a linkmap is still generated even when this attribute is `False`. | Boolean | optional | `False` |
326328
| <a id="apple_xcframework-infoplists"></a>infoplists | A list of .plist files that will be merged to form the Info.plist for each of the embedded frameworks. At least one file must be specified. Please see [Info.plist Handling](https://github.com/bazelbuild/rules_apple/blob/main/doc/common_info.md#infoplist-handling) for what is supported. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
327329
| <a id="apple_xcframework-ios"></a>ios | A dictionary of strings indicating which platform variants should be built for the iOS platform ( `device` or `simulator`) as keys, and arrays of strings listing which architectures should be built for those platform variants (for example, `x86_64`, `arm64`) as their values. | <a href="https://bazel.build/rules/lib/core/dict">Dictionary: String -> List of strings</a> | optional | `{}` |
328330
| <a id="apple_xcframework-linkopts"></a>linkopts | A list of strings representing extra flags that should be passed to the linker. | List of strings | optional | `[]` |

0 commit comments

Comments
 (0)