Skip to content

Commit eabba0d

Browse files
nglevinluispadron
authored andcommitted
Unify all the disassociated references to the "unstripped" binary outputs and the anticipated base name via use of "fragment_support" and "outputs", shared through compilation_support and linking_support. This will be used to reference the unstripped binary that is found in the generated dSYM bundles.
Cherry-pick: c19c91b
1 parent fc6a0d1 commit eabba0d

File tree

5 files changed

+134
-47
lines changed

5 files changed

+134
-47
lines changed

apple/internal/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ bzl_library(
202202
"//apple:__subpackages__",
203203
],
204204
deps = [
205+
":fragment_support",
206+
":outputs",
205207
":platform_support",
206208
"@bazel_skylib//lib:paths",
207209
],
@@ -260,6 +262,14 @@ bzl_library(
260262
],
261263
)
262264

265+
bzl_library(
266+
name = "fragment_support",
267+
srcs = ["fragment_support.bzl"],
268+
visibility = [
269+
"//apple:__subpackages__",
270+
],
271+
)
272+
263273
bzl_library(
264274
name = "framework_import_support",
265275
srcs = ["framework_import_support.bzl"],
@@ -345,6 +355,7 @@ bzl_library(
345355
":compilation_support",
346356
":entitlements_support",
347357
":intermediates",
358+
":outputs",
348359
":providers",
349360
":rule_support",
350361
"@bazel_skylib//lib:collections",
@@ -431,6 +442,7 @@ bzl_library(
431442
],
432443
deps = [
433444
":experimental",
445+
":fragment_support",
434446
":intermediates",
435447
"@bazel_skylib//lib:paths",
436448
],

apple/internal/compilation_support.bzl

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414

1515
"""Utility methods used for creating objc_* rules actions"""
1616

17-
load(
18-
"@bazel_skylib//lib:paths.bzl",
19-
"paths",
20-
)
2117
load(
2218
"@rules_cc//cc/common:cc_common.bzl",
2319
"cc_common",
2420
)
21+
load(
22+
"//apple/internal:fragment_support.bzl",
23+
"fragment_support",
24+
)
25+
load(
26+
"//apple/internal:outputs.bzl",
27+
"outputs",
28+
)
2529
load(
2630
"//apple/internal:platform_support.bzl",
2731
"platform_support",
@@ -132,10 +136,12 @@ def _register_binary_strip_action(
132136
"-bundle" in extra_link_args or link_bundle or "-kext" in extra_link_args):
133137
strip_safe = True
134138

135-
# TODO(b/331163513): Use intermediates.file() instead of declare_shareable_artifact().
136-
stripped_binary = ctx.actions.declare_shareable_artifact(
137-
paths.join(ctx.label.package, name),
138-
apple_platform_info.target_build_config.bin_dir,
139+
stripped_binary = outputs.main_binary(
140+
actions = ctx.actions,
141+
apple_platform_info = apple_platform_info,
142+
cpp_fragment = ctx.fragments.cpp,
143+
label = ctx.label,
144+
unstripped = False,
139145
)
140146
args = ctx.actions.args()
141147
args.add("strip")
@@ -160,13 +166,6 @@ def _register_binary_strip_action(
160166
)
161167
return stripped_binary
162168

163-
def _emit_builtin_objc_strip_action(ctx):
164-
return (
165-
ctx.fragments.objc.builtin_objc_strip_action and
166-
ctx.fragments.cpp.objc_enable_binary_stripping() and
167-
ctx.fragments.cpp.compilation_mode() == "opt"
168-
)
169-
170169
def _register_configuration_specific_link_actions(
171170
*,
172171
additional_outputs,
@@ -191,23 +190,13 @@ def _register_configuration_specific_link_actions(
191190
ctx = common_variables.ctx
192191
feature_configuration = _build_feature_configuration(common_variables)
193192

194-
# When compilation_mode=opt and objc_enable_binary_stripping are specified, the unstripped
195-
# binary containing debug symbols is generated by the linker, which also needs the debug
196-
# symbols for dead-code removal. The binary is also used to generate dSYM bundle if
197-
# --apple_generate_dsym is specified. A symbol strip action is later registered to strip
198-
# the symbol table from the unstripped binary.
199-
if _emit_builtin_objc_strip_action(ctx):
200-
# TODO(b/331163513): Use intermediates.file() instead of declare_shareable_artifact().
201-
binary = ctx.actions.declare_shareable_artifact(
202-
paths.join(ctx.label.package, name + "_unstripped"),
203-
apple_platform_info.target_build_config.bin_dir,
204-
)
205-
else:
206-
# TODO(b/331163513): Use intermediates.file() instead of declare_shareable_artifact().
207-
binary = ctx.actions.declare_shareable_artifact(
208-
paths.join(ctx.label.package, name),
209-
apple_platform_info.target_build_config.bin_dir,
210-
)
193+
binary = outputs.main_binary(
194+
actions = ctx.actions,
195+
apple_platform_info = apple_platform_info,
196+
cpp_fragment = ctx.fragments.cpp,
197+
label = ctx.label,
198+
unstripped = ctx.fragments.objc.builtin_objc_strip_action,
199+
)
211200

212201
prefixed_attr_linkopts = [
213202
"-Wl,%s" % linkopt
@@ -245,7 +234,9 @@ def _register_configuration_specific_link_actions(
245234
variables_extension = user_variable_extensions,
246235
)
247236

248-
if _emit_builtin_objc_strip_action(ctx):
237+
if fragment_support.is_objc_strip_action_enabled(
238+
cpp_fragment = ctx.fragments.cpp,
239+
) and ctx.fragments.objc.builtin_objc_strip_action:
249240
return _register_binary_strip_action(
250241
ctx = ctx,
251242
apple_platform_info = apple_platform_info,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2025 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Wrapper to centralize configuration of fragment-based logic."""
16+
17+
visibility([
18+
"//apple/...",
19+
"//test/...",
20+
])
21+
22+
def _is_objc_strip_action_enabled(*, cpp_fragment):
23+
"""Returns wheither the Objective-C strip action is active."""
24+
return (
25+
cpp_fragment.objc_enable_binary_stripping() and
26+
cpp_fragment.compilation_mode() == "opt"
27+
)
28+
29+
fragment_support = struct(
30+
is_objc_strip_action_enabled = _is_objc_strip_action_enabled,
31+
)

apple/internal/linking_support.bzl

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414

1515
"""Support for linking related actions."""
1616

17-
load(
18-
"@bazel_skylib//lib:paths.bzl",
19-
"paths",
20-
)
2117
load("@build_bazel_apple_support//lib:lipo.bzl", "lipo")
2218
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
2319
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
@@ -41,6 +37,10 @@ load(
4137
"//apple/internal:multi_arch_binary_support.bzl",
4238
"subtract_linking_contexts",
4339
)
40+
load(
41+
"//apple/internal:outputs.bzl",
42+
"outputs",
43+
)
4444
load(
4545
"//apple/internal:providers.bzl",
4646
"AppleDynamicFrameworkInfo",
@@ -234,7 +234,7 @@ def _link_multi_arch_binary(
234234
avoid_cc_infos.extend([dep[CcInfo] for dep in avoid_deps if CcInfo in dep])
235235
avoid_cc_linking_contexts = [dep.linking_context for dep in avoid_cc_infos]
236236

237-
outputs = []
237+
linker_outputs = []
238238
cc_infos = []
239239
legacy_debug_outputs = {}
240240

@@ -300,15 +300,16 @@ Please report this as a bug to the Apple BUILD Rules team.
300300
dsym_variants = dsym_variants,
301301
))
302302
else:
303-
if ctx.fragments.cpp.objc_should_strip_binary:
304-
suffix = "_bin_unstripped"
305-
else:
306-
suffix = "_bin"
303+
main_binary_unstripped_basename = outputs.main_binary_basename(
304+
cpp_fragment = ctx.fragments.cpp,
305+
label_name = ctx.label.name,
306+
unstripped = True,
307+
)
307308
dsym_output = intermediates.file(
308309
actions = ctx.actions,
309310
target_name = ctx.label.name,
310311
output_discriminator = split_transition_key,
311-
file_name = "{}.dwarf".format(ctx.label.name + suffix),
312+
file_name = "{}.dwarf".format(main_binary_unstripped_basename),
312313
)
313314

314315
# TODO(b/391401130): Remove this once all users are migrated to the downstream
@@ -333,7 +334,12 @@ Please report this as a bug to the Apple BUILD Rules team.
333334
additional_outputs.append(linkmap)
334335
legacy_debug_outputs.setdefault(platform_info.target_arch, {})["linkmap"] = linkmap
335336

336-
name = ctx.label.name + "_bin"
337+
main_binary_basename = outputs.main_binary_basename(
338+
cpp_fragment = ctx.fragments.cpp,
339+
label_name = ctx.label.name,
340+
unstripped = False,
341+
)
342+
337343
executable = compilation_support.register_configuration_specific_link_actions(
338344
additional_outputs = additional_outputs,
339345
apple_platform_info = platform_info,
@@ -342,7 +348,7 @@ Please report this as a bug to the Apple BUILD Rules team.
342348
cc_linking_context = cc_linking_context,
343349
extra_link_args = extra_linkopts,
344350
extra_link_inputs = extra_link_inputs,
345-
name = name,
351+
name = main_binary_basename,
346352
stamp = stamp,
347353
user_variable_extensions = variables_extension | extensions,
348354
)
@@ -356,7 +362,7 @@ Please report this as a bug to the Apple BUILD Rules team.
356362
"linkmap": linkmap,
357363
}
358364

359-
outputs.append(struct(**output))
365+
linker_outputs.append(struct(**output))
360366

361367
header_tokens = []
362368
for _, deps in split_deps.items():
@@ -369,7 +375,7 @@ Please report this as a bug to the Apple BUILD Rules team.
369375
return struct(
370376
cc_info = cc_common.merge_cc_infos(direct_cc_infos = cc_infos),
371377
output_groups = output_groups,
372-
outputs = outputs,
378+
outputs = linker_outputs,
373379
debug_outputs_provider = new_appledebugoutputsinfo(outputs_map = legacy_debug_outputs),
374380
)
375381

apple/internal/outputs.bzl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ load(
2626
"//apple/internal:experimental.bzl",
2727
"is_experimental_tree_artifact_enabled",
2828
)
29+
load(
30+
"//apple/internal:fragment_support.bzl",
31+
"fragment_support",
32+
)
2933
load(
3034
"//apple/internal:intermediates.bzl",
3135
"intermediates",
@@ -122,6 +126,47 @@ def _infoplist(*, actions, label_name, output_discriminator):
122126
file_name = "Info.plist",
123127
)
124128

129+
def _main_binary(
130+
*,
131+
actions,
132+
apple_platform_info,
133+
cpp_fragment,
134+
label,
135+
unstripped):
136+
"""Returns a file reference for the main binary that gets linked."""
137+
138+
# TODO(b/331163513): Use intermediates.file() instead of declare_shareable_artifact() as soon as
139+
# it is safe to do so.
140+
return actions.declare_shareable_artifact(
141+
paths.join(label.package, _main_binary_basename(
142+
cpp_fragment = cpp_fragment,
143+
label_name = label.name,
144+
unstripped = unstripped,
145+
)),
146+
apple_platform_info.target_build_config.bin_dir,
147+
)
148+
149+
def _main_binary_basename(
150+
*,
151+
cpp_fragment,
152+
label_name,
153+
unstripped):
154+
"""Returns the basename of the main binary that gets linked."""
155+
156+
# When compilation_mode=opt and objc_enable_binary_stripping are specified, the unstripped
157+
# binary containing debug symbols is generated by the linker, which also needs the debug
158+
# symbols for dead-code removal. The binary is also used to generate dSYM bundle if
159+
# --apple_generate_dsym is specified. A symbol strip action is later registered to strip
160+
# the symbol table from the unstripped binary.
161+
if fragment_support.is_objc_strip_action_enabled(
162+
cpp_fragment = cpp_fragment,
163+
) and unstripped:
164+
suffix = "_bin_unstripped"
165+
else:
166+
suffix = "_bin"
167+
168+
return label_name + suffix
169+
125170
def _has_different_embedding_archive(*, platform_prerequisites, rule_descriptor):
126171
"""Returns True if this target exposes a different archive when embedded in another target."""
127172
tree_artifact_enabled = is_experimental_tree_artifact_enabled(
@@ -188,6 +233,8 @@ outputs = struct(
188233
dsyms = _dsyms,
189234
executable = _executable,
190235
infoplist = _infoplist,
236+
main_binary = _main_binary,
237+
main_binary_basename = _main_binary_basename,
191238
merge_output_groups = _merge_output_groups,
192239
root_path_from_archive = _root_path_from_archive,
193240
has_different_embedding_archive = _has_different_embedding_archive,

0 commit comments

Comments
 (0)