Skip to content

Commit 60ed2aa

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 29c038b commit 60ed2aa

12 files changed

Lines changed: 140 additions & 62 deletions

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
@@ -39,24 +39,10 @@ visibility([
3939
"//apple/...",
4040
])
4141

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

6248
def _build_fully_linked_variable_extensions(*, archive, libs):
@@ -143,13 +129,21 @@ def _get_libraries_for_linking(libraries_to_link):
143129
libraries.append(_get_library_for_linking(library_to_link))
144130
return libraries
145131

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

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

146+
ctx = common_variables.ctx
153147
output_archive = ctx.actions.declare_file(name + ".a")
154148
extensions = _build_fully_linked_variable_extensions(
155149
archive = output_archive,
@@ -306,6 +300,7 @@ def _register_configuration_specific_link_actions(
306300
attr_linkopts,
307301
bundle_name,
308302
common_variables,
303+
cc_configured_features,
309304
cc_linking_context,
310305
extra_link_args,
311306
extra_link_inputs,
@@ -322,8 +317,12 @@ def _register_configuration_specific_link_actions(
322317
Returns:
323318
(File) the linked binary
324319
"""
320+
feature_configuration = _build_feature_configuration(
321+
cc_configured_features = cc_configured_features,
322+
cc_toolchain = common_variables.toolchain,
323+
)
324+
325325
ctx = common_variables.ctx
326-
feature_configuration = _build_feature_configuration(common_variables)
327326

328327
# TODO: Remove when we drop Bazel 8
329328
if bazel_features.cc.objc_fragment_has_builtin_objc_strip_action:
@@ -615,7 +614,6 @@ def _register_configuration_specific_link_actions_with_objc_variables(
615614
return binary
616615

617616
compilation_support = struct(
618-
# TODO(b/331163513): Move apple_common.compliation_support.build_common_variables here, too.
619617
get_library_for_linking = _get_library_for_linking,
620618
get_static_library_for_linking = _get_static_library_for_linking,
621619
register_fully_link_action = _register_fully_link_action,

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,
@@ -646,6 +652,7 @@ def _ios_app_clip_impl(ctx):
646652
avoid_deps = ctx.attr.frameworks,
647653
build_settings = apple_xplat_toolchain_info.build_settings,
648654
bundle_name = bundle_name,
655+
cc_configured_features = cc_configured_features,
649656
entitlements = entitlements.linking,
650657
exported_symbols_lists = ctx.files.exported_symbols_lists,
651658
platform_prerequisites = platform_prerequisites,
@@ -915,7 +922,10 @@ def _ios_framework_impl(ctx):
915922
bundle_name = bundle_name,
916923
suffix_default = ctx.attr._bundle_id_suffix_default,
917924
)
918-
cc_configured_features = features_support.cc_configured_features(ctx = ctx)
925+
cc_configured_features = features_support.cc_configured_features(
926+
ctx = ctx,
927+
extra_requested_features = ["link_dylib"],
928+
)
919929
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
920930
executable_name = ctx.attr.executable_name
921931
features = cc_configured_features.enabled_features
@@ -971,6 +981,7 @@ def _ios_framework_impl(ctx):
971981
avoid_deps = ctx.attr.frameworks,
972982
build_settings = apple_xplat_toolchain_info.build_settings,
973983
bundle_name = bundle_name,
984+
cc_configured_features = cc_configured_features,
974985
# Frameworks do not have entitlements.
975986
entitlements = None,
976987
exported_symbols_lists = ctx.files.exported_symbols_lists,
@@ -1236,6 +1247,7 @@ def _ios_extension_impl(ctx):
12361247
avoid_deps = ctx.attr.frameworks,
12371248
build_settings = apple_xplat_toolchain_info.build_settings,
12381249
bundle_name = bundle_name,
1250+
cc_configured_features = cc_configured_features,
12391251
entitlements = entitlements.linking,
12401252
exported_symbols_lists = ctx.files.exported_symbols_lists,
12411253
extra_linkopts = extra_linkopts,
@@ -1544,6 +1556,7 @@ def _ios_dynamic_framework_impl(ctx):
15441556
avoid_deps = ctx.attr.frameworks,
15451557
build_settings = apple_xplat_toolchain_info.build_settings,
15461558
bundle_name = bundle_name,
1559+
cc_configured_features = cc_configured_features,
15471560
# Frameworks do not have entitlements.
15481561
entitlements = None,
15491562
exported_symbols_lists = ctx.files.exported_symbols_lists,
@@ -1786,6 +1799,7 @@ def _ios_static_framework_impl(ctx):
17861799

17871800
archive_result = linking_support.register_static_library_archive_action(
17881801
ctx = ctx,
1802+
cc_configured_features = cc_configured_features,
17891803
cc_toolchains = cc_toolchain_forwarder,
17901804
)
17911805
binary_artifact = archive_result.library
@@ -2201,6 +2215,7 @@ def _ios_imessage_extension_impl(ctx):
22012215
avoid_deps = ctx.attr.frameworks,
22022216
build_settings = apple_xplat_toolchain_info.build_settings,
22032217
bundle_name = bundle_name,
2218+
cc_configured_features = cc_configured_features,
22042219
entitlements = entitlements.linking,
22052220
exported_symbols_lists = ctx.files.exported_symbols_lists,
22062221
extra_linkopts = extra_linkopts,

0 commit comments

Comments
 (0)