Skip to content

Commit 07a7e25

Browse files
nglevinluispadron
authored andcommitted
Share the features configurations from top level rule implementations with the shared Apple linking logic.
Cherry-pick: f608a7b
1 parent 8cdb863 commit 07a7e25

12 files changed

+140
-62
lines changed

apple/apple_binary.bzl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,22 @@ Resolved Xcode is version {xcode_version}.
8484
apple_xplat_toolchain_info = ctx.attr._xplat_toolchain[AppleXPlatToolsToolchainInfo]
8585
binary_type = ctx.attr.binary_type
8686
bundle_loader = ctx.attr.bundle_loader
87+
88+
extra_linkopts = []
89+
extra_requested_features = []
90+
91+
if binary_type == "dylib":
92+
extra_linkopts.append("-dynamiclib")
93+
elif binary_type == "loadable_bundle":
94+
extra_linkopts.extend([
95+
"-bundle",
96+
"-Wl,-rpath,@loader_path/Frameworks",
97+
])
98+
extra_requested_features.append("link_bundle")
99+
87100
cc_configured_features = features_support.cc_configured_features(
88101
ctx = ctx,
102+
extra_requested_features = extra_requested_features,
89103
)
90104
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
91105

@@ -100,16 +114,6 @@ Resolved Xcode is version {xcode_version}.
100114
secure_features = secure_features,
101115
)
102116

103-
extra_linkopts = []
104-
105-
if binary_type == "dylib":
106-
extra_linkopts.append("-dynamiclib")
107-
elif binary_type == "loadable_bundle":
108-
extra_linkopts.extend([
109-
"-bundle",
110-
"-Wl,-rpath,@loader_path/Frameworks",
111-
])
112-
113117
extra_linkopts.extend([
114118
_linker_flag_for_sdk_dylib(dylib)
115119
for dylib in ctx.attr.sdk_dylibs
@@ -129,6 +133,7 @@ Resolved Xcode is version {xcode_version}.
129133
build_settings = apple_xplat_toolchain_info.build_settings,
130134
bundle_loader = bundle_loader,
131135
bundle_name = rule_label.name,
136+
cc_configured_features = cc_configured_features,
132137
exported_symbols_lists = ctx.files.exported_symbols_lists,
133138
extra_linkopts = extra_linkopts,
134139
platform_prerequisites = None,

apple/apple_static_library.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Expected Apple platform type of "{platform_type}", but that was not found in {to
9191

9292
archive_result = linking_support.register_static_library_archive_action(
9393
ctx = ctx,
94+
cc_configured_features = cc_configured_features,
9495
cc_toolchains = cc_toolchain_forwarder,
9596
)
9697

apple/internal/compilation_support.bzl

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,10 @@ visibility([
4040
"//apple/...",
4141
])
4242

43-
def _build_feature_configuration(common_variables):
44-
ctx = common_variables.ctx
45-
46-
enabled_features = []
47-
enabled_features.extend(ctx.features)
48-
enabled_features.extend(common_variables.extra_enabled_features)
49-
50-
disabled_features = []
51-
disabled_features.extend(ctx.disabled_features)
52-
disabled_features.extend(common_variables.extra_disabled_features)
53-
disabled_features.append("parse_headers")
54-
55-
return cc_common.configure_features(
56-
ctx = common_variables.ctx,
57-
cc_toolchain = common_variables.toolchain,
43+
def _build_feature_configuration(*, cc_configured_features, cc_toolchain):
44+
return cc_configured_features.configure_features(
45+
cc_toolchain = cc_toolchain,
5846
language = "objc",
59-
requested_features = enabled_features,
60-
unsupported_features = disabled_features,
6147
)
6248

6349
def _build_fully_linked_variable_extensions(*, archive, libs):
@@ -144,13 +130,21 @@ def _get_libraries_for_linking(libraries_to_link):
144130
libraries.append(_get_library_for_linking(library_to_link))
145131
return libraries
146132

147-
def _register_fully_link_action(*, cc_linking_context, common_variables, name):
148-
ctx = common_variables.ctx
149-
feature_configuration = _build_feature_configuration(common_variables)
133+
def _register_fully_link_action(
134+
*,
135+
cc_configured_features,
136+
cc_linking_context,
137+
common_variables,
138+
name):
139+
feature_configuration = _build_feature_configuration(
140+
cc_configured_features = cc_configured_features,
141+
cc_toolchain = common_variables.toolchain,
142+
)
150143

151144
libraries_to_link = _libraries_from_linking_context(cc_linking_context).to_list()
152145
libraries = _get_libraries_for_linking(libraries_to_link)
153146

147+
ctx = common_variables.ctx
154148
output_archive = ctx.actions.declare_file(name + ".a")
155149
extensions = _build_fully_linked_variable_extensions(
156150
archive = output_archive,
@@ -307,6 +301,7 @@ def _register_configuration_specific_link_actions(
307301
attr_linkopts,
308302
bundle_name,
309303
common_variables,
304+
cc_configured_features,
310305
cc_linking_context,
311306
extra_link_args,
312307
extra_link_inputs,
@@ -323,8 +318,12 @@ def _register_configuration_specific_link_actions(
323318
Returns:
324319
(File) the linked binary
325320
"""
321+
feature_configuration = _build_feature_configuration(
322+
cc_configured_features = cc_configured_features,
323+
cc_toolchain = common_variables.toolchain,
324+
)
325+
326326
ctx = common_variables.ctx
327-
feature_configuration = _build_feature_configuration(common_variables)
328327

329328
# TODO: Remove when we drop Bazel 8
330329
if bazel_features.cc.objc_fragment_has_builtin_objc_strip_action:
@@ -606,7 +605,6 @@ def _register_configuration_specific_link_actions_with_objc_variables(
606605
return binary
607606

608607
compilation_support = struct(
609-
# TODO(b/331163513): Move apple_common.compliation_support.build_common_variables here, too.
610608
register_fully_link_action = _register_fully_link_action,
611609
register_configuration_specific_link_actions = _register_configuration_specific_link_actions,
612610
subtract_linking_contexts = _subtract_linking_contexts,

apple/internal/features_support.bzl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,19 @@ def _cc_configured_features(
4848
if extra_requested_features:
4949
features += extra_requested_features
5050

51-
disabled_features = ctx.disabled_features
51+
disabled_features = ctx.disabled_features + [
52+
# Disabled include scanning (b/321109350) to work around issues with GrepIncludes actions
53+
# being routed to the wrong exec platform.
54+
"cc_include_scanning",
55+
# Disabled parse_headers (b/174937981) to avoid validating headers from top-level Apple
56+
# rules (i.e. legacy uses of ios_framework). Those headers are expected to be as public API
57+
# i.e. a "swiftinterface" equivalent for (Objective-)C(++) frameworks and are not expected
58+
# to build in any given Bazel WORKSPACE.
59+
#
60+
# TODO: b/251214758 - Re-enable parse_headers once 3P framework users establish interfaces
61+
# via a more stable means, as was done for generated interfaces in Swift.
62+
"parse_headers",
63+
]
5264
if extra_disabled_features:
5365
disabled_features += extra_disabled_features
5466

apple/internal/ios_rules.bzl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ def _ios_application_impl(ctx):
171171
apple_mac_toolchain_info = ctx.attr._mac_toolchain[AppleMacToolsToolchainInfo]
172172
apple_xplat_toolchain_info = ctx.attr._xplat_toolchain[AppleXPlatToolsToolchainInfo]
173173

174+
extra_requested_features = []
175+
if ctx.attr.testonly:
176+
extra_requested_features.append("exported_symbols")
177+
174178
bundle_name, bundle_extension = bundling_support.bundle_full_name(
175179
custom_bundle_name = ctx.attr.bundle_name,
176180
label_name = ctx.label.name,
@@ -186,6 +190,7 @@ def _ios_application_impl(ctx):
186190
bundle_verification_targets = [struct(target = ext) for ext in ctx.attr.extensions]
187191
cc_configured_features = features_support.cc_configured_features(
188192
ctx = ctx,
193+
extra_requested_features = extra_requested_features,
189194
)
190195
features = cc_configured_features.enabled_features
191196
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
@@ -260,6 +265,7 @@ def _ios_application_impl(ctx):
260265
avoid_deps = ctx.attr.frameworks,
261266
build_settings = apple_xplat_toolchain_info.build_settings,
262267
bundle_name = bundle_name,
268+
cc_configured_features = cc_configured_features,
263269
entitlements = entitlements.linking,
264270
exported_symbols_lists = ctx.files.exported_symbols_lists,
265271
extra_linkopts = extra_linkopts,
@@ -649,6 +655,7 @@ def _ios_app_clip_impl(ctx):
649655
avoid_deps = ctx.attr.frameworks,
650656
build_settings = apple_xplat_toolchain_info.build_settings,
651657
bundle_name = bundle_name,
658+
cc_configured_features = cc_configured_features,
652659
entitlements = entitlements.linking,
653660
exported_symbols_lists = ctx.files.exported_symbols_lists,
654661
platform_prerequisites = platform_prerequisites,
@@ -909,7 +916,10 @@ def _ios_framework_impl(ctx):
909916
bundle_name = bundle_name,
910917
suffix_default = ctx.attr._bundle_id_suffix_default,
911918
)
912-
cc_configured_features = features_support.cc_configured_features(ctx = ctx)
919+
cc_configured_features = features_support.cc_configured_features(
920+
ctx = ctx,
921+
extra_requested_features = ["link_dylib"],
922+
)
913923
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
914924
executable_name = ctx.attr.executable_name
915925
features = cc_configured_features.enabled_features
@@ -965,6 +975,7 @@ def _ios_framework_impl(ctx):
965975
avoid_deps = ctx.attr.frameworks,
966976
build_settings = apple_xplat_toolchain_info.build_settings,
967977
bundle_name = bundle_name,
978+
cc_configured_features = cc_configured_features,
968979
# Frameworks do not have entitlements.
969980
entitlements = None,
970981
exported_symbols_lists = ctx.files.exported_symbols_lists,
@@ -1230,6 +1241,7 @@ def _ios_extension_impl(ctx):
12301241
avoid_deps = ctx.attr.frameworks,
12311242
build_settings = apple_xplat_toolchain_info.build_settings,
12321243
bundle_name = bundle_name,
1244+
cc_configured_features = cc_configured_features,
12331245
entitlements = entitlements.linking,
12341246
exported_symbols_lists = ctx.files.exported_symbols_lists,
12351247
extra_linkopts = extra_linkopts,
@@ -1541,6 +1553,7 @@ def _ios_dynamic_framework_impl(ctx):
15411553
avoid_deps = ctx.attr.frameworks,
15421554
build_settings = apple_xplat_toolchain_info.build_settings,
15431555
bundle_name = bundle_name,
1556+
cc_configured_features = cc_configured_features,
15441557
# Frameworks do not have entitlements.
15451558
entitlements = None,
15461559
exported_symbols_lists = ctx.files.exported_symbols_lists,
@@ -1783,6 +1796,7 @@ def _ios_static_framework_impl(ctx):
17831796

17841797
archive_result = linking_support.register_static_library_archive_action(
17851798
ctx = ctx,
1799+
cc_configured_features = cc_configured_features,
17861800
cc_toolchains = cc_toolchain_forwarder,
17871801
)
17881802
binary_artifact = archive_result.library
@@ -2198,6 +2212,7 @@ def _ios_imessage_extension_impl(ctx):
21982212
avoid_deps = ctx.attr.frameworks,
21992213
build_settings = apple_xplat_toolchain_info.build_settings,
22002214
bundle_name = bundle_name,
2215+
cc_configured_features = cc_configured_features,
22012216
entitlements = entitlements.linking,
22022217
exported_symbols_lists = ctx.files.exported_symbols_lists,
22032218
extra_linkopts = extra_linkopts,

0 commit comments

Comments
 (0)