Skip to content

Commit 89b1888

Browse files
nglevinluispadron
authored andcommitted
Add secure_features support to SDK rules, backed by tests to validate arm64e support and disabled features.
Cherry-pick: 4012850
1 parent d53defa commit 89b1888

25 files changed

Lines changed: 701 additions & 140 deletions

apple/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ bzl_library(
7474
srcs = ["apple_binary.bzl"],
7575
deps = [
7676
"//apple/internal:apple_toolchains",
77+
"//apple/internal:features_support",
7778
"//apple/internal:linking_support",
7879
"//apple/internal:providers",
7980
"//apple/internal:rule_attrs",
8081
"//apple/internal:rule_factory",
82+
"//apple/internal:secure_features_support",
8183
"//apple/internal:transition_support",
8284
"@build_bazel_apple_support//lib:apple_support",
8385
"@rules_cc//cc/common",
@@ -88,11 +90,12 @@ bzl_library(
8890
name = "apple_static_library",
8991
srcs = ["apple_static_library.bzl"],
9092
deps = [
91-
":providers",
93+
"//apple/internal:features_support",
9294
"//apple/internal:linking_support",
9395
"//apple/internal:providers",
9496
"//apple/internal:rule_attrs",
9597
"//apple/internal:rule_factory",
98+
"//apple/internal:secure_features_support",
9699
"//apple/internal:transition_support",
97100
"@rules_cc//cc/common",
98101
],

apple/apple_binary.bzl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ load(
2424
"AppleXPlatToolsToolchainInfo",
2525
"apple_toolchain_utils",
2626
)
27+
load(
28+
"//apple/internal:features_support.bzl",
29+
"features_support",
30+
)
2731
load(
2832
"//apple/internal:linking_support.bzl",
2933
"linking_support",
@@ -41,6 +45,10 @@ load(
4145
"//apple/internal:rule_factory.bzl",
4246
"rule_factory",
4347
)
48+
load(
49+
"//apple/internal:secure_features_support.bzl",
50+
"secure_features_support",
51+
)
4452
load(
4553
"//apple/internal:transition_support.bzl",
4654
"transition_support",
@@ -76,12 +84,19 @@ Resolved Xcode is version {xcode_version}.
7684
apple_xplat_toolchain_info = ctx.attr._xplat_toolchain[AppleXPlatToolsToolchainInfo]
7785
binary_type = ctx.attr.binary_type
7886
bundle_loader = ctx.attr.bundle_loader
87+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx)
7988
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
8089

90+
rule_label = ctx.label
8191
secure_features = ctx.attr.secure_features
82-
if secure_features:
83-
if not apple_xplat_toolchain_info.build_settings.enable_wip_features:
84-
fail("secure_features are still a work in progress and not yet supported in the rules.")
92+
93+
# Check that the requested secure features are supported and enabled for the toolchain.
94+
secure_features_support.validate_secure_features_support(
95+
cc_configured_features_init = cc_configured_features_init,
96+
cc_toolchain_forwarder = cc_toolchain_forwarder,
97+
rule_label = rule_label,
98+
secure_features = secure_features,
99+
)
85100

86101
extra_linkopts = []
87102

@@ -111,7 +126,7 @@ Resolved Xcode is version {xcode_version}.
111126
cc_toolchains = cc_toolchain_forwarder,
112127
build_settings = apple_xplat_toolchain_info.build_settings,
113128
bundle_loader = bundle_loader,
114-
bundle_name = ctx.label.name,
129+
bundle_name = rule_label.name,
115130
exported_symbols_lists = ctx.files.exported_symbols_lists,
116131
extra_linkopts = extra_linkopts,
117132
platform_prerequisites = None,

apple/apple_static_library.bzl

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ load(
2020
"ApplePlatformInfo",
2121
)
2222
load(
23-
"//apple/internal:apple_toolchains.bzl",
24-
"AppleXPlatToolsToolchainInfo",
23+
"//apple/internal:features_support.bzl",
24+
"features_support",
2525
)
2626
load(
2727
"//apple/internal:linking_support.bzl",
@@ -39,13 +39,19 @@ load(
3939
"//apple/internal:rule_factory.bzl",
4040
"rule_factory",
4141
)
42+
load(
43+
"//apple/internal:secure_features_support.bzl",
44+
"secure_features_support",
45+
)
4246
load(
4347
"//apple/internal:transition_support.bzl",
4448
"transition_support",
4549
)
4650

4751
def _apple_static_library_impl(ctx):
48-
apple_xplat_toolchain_info = ctx.attr._xplat_toolchain[AppleXPlatToolsToolchainInfo]
52+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx)
53+
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
54+
rule_label = ctx.label
4955

5056
if ctx.attr.platform_type == "visionos":
5157
xcode_version_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
@@ -61,13 +67,10 @@ Resolved Xcode is version {xcode_version}.
6167
# `dotted_version` or explicitly through `fail` on an unrecognized platform type value.
6268

6369
secure_features = ctx.attr.secure_features
64-
if secure_features:
65-
if not apple_xplat_toolchain_info.build_settings.enable_wip_features:
66-
fail("secure_features are still a work in progress and not yet supported in the rules.")
6770

68-
# Validate that the resolved platform matches the platform_type attr.
69-
for toolchain_key, resolved_toolchain in ctx.split_attr._cc_toolchain_forwarder.items():
70-
if resolved_toolchain[ApplePlatformInfo].target_os != ctx.attr.platform_type:
71+
for toolchain_key, cc_toolchain in cc_toolchain_forwarder.items():
72+
# Validate that the resolved platform matches the platform_type attr.
73+
if cc_toolchain[ApplePlatformInfo].target_os != ctx.attr.platform_type:
7174
fail("""
7275
ERROR: Unexpected resolved platform:
7376
Expected Apple platform type of "{platform_type}", but that was not found in {toolchain_key}.
@@ -76,7 +79,14 @@ Expected Apple platform type of "{platform_type}", but that was not found in {to
7679
toolchain_key = toolchain_key,
7780
))
7881

79-
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
82+
# Check that the requested secure features are supported and enabled for the toolchain.
83+
secure_features_support.validate_secure_features_support(
84+
cc_configured_features_init = cc_configured_features_init,
85+
cc_toolchain_forwarder = cc_toolchain_forwarder,
86+
rule_label = rule_label,
87+
secure_features = secure_features,
88+
)
89+
8090
archive_result = linking_support.register_static_library_archive_action(
8191
ctx = ctx,
8292
cc_toolchains = cc_toolchain_forwarder,

apple/internal/BUILD

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ bzl_library(
224224
"//apple:common",
225225
"//apple/internal/utils:defines",
226226
"@build_bazel_apple_support//lib:apple_support",
227-
"@rules_cc//cc/common",
228227
],
229228
)
230229

@@ -326,6 +325,7 @@ bzl_library(
326325
":rule_factory",
327326
":rule_support",
328327
":run_support",
328+
":secure_features_support",
329329
":stub_support",
330330
":swift_support",
331331
":transition_support",
@@ -413,6 +413,7 @@ bzl_library(
413413
":rule_factory",
414414
":rule_support",
415415
":run_support",
416+
":secure_features_support",
416417
":swift_support",
417418
":transition_support",
418419
"//apple:providers",
@@ -640,9 +641,10 @@ bzl_library(
640641
name = "secure_features_support",
641642
srcs = ["secure_features_support.bzl"],
642643
visibility = [
643-
"//apple/internal:__subpackages__",
644+
"//apple:__subpackages__",
644645
],
645646
deps = [
647+
":providers",
646648
"@rules_cc//cc/common",
647649
],
648650
)
@@ -729,6 +731,7 @@ bzl_library(
729731
":rule_factory",
730732
":rule_support",
731733
":run_support",
734+
":secure_features_support",
732735
":swift_support",
733736
":transition_support",
734737
"//apple:providers",
@@ -843,6 +846,7 @@ bzl_library(
843846
":rule_attrs",
844847
":rule_factory",
845848
":rule_support",
849+
":secure_features_support",
846850
":swift_support",
847851
":transition_support",
848852
"//apple:providers",

apple/internal/entitlements_support.bzl

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ load(
1818
"@build_bazel_apple_support//lib:apple_support.bzl",
1919
"apple_support",
2020
)
21-
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
2221
load(
2322
"//apple:common.bzl",
2423
"entitlements_validation_mode",
@@ -31,10 +30,6 @@ load(
3130
"//apple/internal:bundling_support.bzl",
3231
"bundling_support",
3332
)
34-
load(
35-
"//apple/internal:providers.bzl",
36-
"ApplePlatformInfo",
37-
)
3833
load(
3934
"//apple/internal:resource_actions.bzl",
4035
"resource_actions",
@@ -203,7 +198,6 @@ def _extract_signing_info(
203198
def _process_entitlements(
204199
actions,
205200
apple_mac_toolchain_info,
206-
apple_xplat_toolchain_info,
207201
bundle_id,
208202
cc_configured_features_init,
209203
cc_toolchains,
@@ -236,8 +230,6 @@ def _process_entitlements(
236230
actions: The object used to register actions.
237231
apple_mac_toolchain_info: The `struct` of tools from the shared Apple
238232
toolchain.
239-
apple_xplat_toolchain_info: The `struct` of tools from the shared Apple
240-
cross platform toolchain.
241233
bundle_id: The bundle identifier.
242234
cc_configured_features_init: The function to initialize the feature configuration for a
243235
given cc_toolchain.
@@ -284,36 +276,23 @@ def _process_entitlements(
284276
app_clip = {"com.apple.developer.on-demand-install-capable": True}
285277
forced_plists.append(struct(**app_clip))
286278
if secure_features:
287-
all_secure_features_entitlements = dict()
288-
for cc_toolchain in cc_toolchains.values():
289-
cc_toolchain_info = cc_toolchain[cc_common.CcToolchainInfo]
290-
291-
# Calculate the effective set of Crosstool features for this toolchain, as we do want to
292-
# double check that the secure features are supported and enabled.
293-
feature_configuration = cc_configured_features_init(
294-
cc_toolchain = cc_toolchain_info,
295-
language = "objc",
296-
)
279+
# Check that the requested secure features are supported and enabled for the toolchain.
280+
secure_features_support.validate_secure_features_support(
281+
cc_configured_features_init = cc_configured_features_init,
282+
cc_toolchain_forwarder = cc_toolchains,
283+
rule_label = rule_label,
284+
secure_features = secure_features,
285+
)
297286

298-
# Check that the requested secure features are supported and enabled for the toolchain.
299-
secure_features_support.validate_secure_features_support(
300-
cc_toolchain_info = cc_toolchain_info,
301-
feature_configuration = feature_configuration,
302-
platform_info = cc_toolchain[ApplePlatformInfo],
303-
rule_label = rule_label,
287+
# Retrieve the entitlements required by the requested secure features, if there are any.
288+
secure_features_entitlements = (
289+
secure_features_support.entitlements_from_secure_features(
304290
secure_features = secure_features,
291+
xcode_version = platform_prerequisites.xcode_version_config.xcode_version(),
305292
)
306-
307-
# Retrieve the entitlements required by the requested secure features, if there are any.
308-
secure_features_entitlements = (
309-
secure_features_support.entitlements_from_secure_features(
310-
secure_features = secure_features,
311-
xcode_version = platform_prerequisites.xcode_version_config.xcode_version(),
312-
)
313-
)
314-
all_secure_features_entitlements.update(secure_features_entitlements)
315-
if all_secure_features_entitlements:
316-
forced_plists.append(struct(**all_secure_features_entitlements))
293+
)
294+
if secure_features_entitlements:
295+
forced_plists.append(struct(**secure_features_entitlements))
317296

318297
inputs = list(plists)
319298

apple/internal/ios_rules.bzl

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ load(
122122
"//apple/internal:run_support.bzl",
123123
"run_support",
124124
)
125+
load(
126+
"//apple/internal:secure_features_support.bzl",
127+
"secure_features_support",
128+
)
125129
load(
126130
"//apple/internal:stub_support.bzl",
127131
"stub_support",
@@ -232,7 +236,6 @@ def _ios_application_impl(ctx):
232236
entitlements = entitlements_support.process_entitlements(
233237
actions = actions,
234238
apple_mac_toolchain_info = apple_mac_toolchain_info,
235-
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
236239
bundle_id = bundle_id,
237240
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
238241
cc_toolchains = cc_toolchain_forwarder,
@@ -625,7 +628,6 @@ def _ios_app_clip_impl(ctx):
625628
entitlements = entitlements_support.process_entitlements(
626629
actions = actions,
627630
apple_mac_toolchain_info = apple_mac_toolchain_info,
628-
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
629631
bundle_id = bundle_id,
630632
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
631633
cc_toolchains = cc_toolchain_forwarder,
@@ -1208,7 +1210,6 @@ def _ios_extension_impl(ctx):
12081210
entitlements = entitlements_support.process_entitlements(
12091211
actions = actions,
12101212
apple_mac_toolchain_info = apple_mac_toolchain_info,
1211-
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
12121213
bundle_id = bundle_id,
12131214
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
12141215
cc_toolchains = cc_toolchain_forwarder,
@@ -1752,6 +1753,7 @@ def _ios_static_framework_impl(ctx):
17521753
apple_mac_toolchain_info = ctx.attr._mac_toolchain[AppleMacToolsToolchainInfo]
17531754
apple_xplat_toolchain_info = ctx.attr._xplat_toolchain[AppleXPlatToolsToolchainInfo]
17541755
avoid_deps = ctx.attr.avoid_deps
1756+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx)
17551757
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
17561758
deps = ctx.attr.deps
17571759
label = ctx.label
@@ -1782,6 +1784,15 @@ def _ios_static_framework_impl(ctx):
17821784
xcode_version_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig],
17831785
)
17841786
resource_deps = ctx.attr.deps + ctx.attr.resources
1787+
secure_features = ctx.attr.secure_features
1788+
1789+
# Check that the requested secure features are supported and enabled for the toolchain.
1790+
secure_features_support.validate_secure_features_support(
1791+
cc_configured_features_init = cc_configured_features_init,
1792+
cc_toolchain_forwarder = cc_toolchain_forwarder,
1793+
rule_label = label,
1794+
secure_features = secure_features,
1795+
)
17851796

17861797
archive_result = linking_support.register_static_library_archive_action(
17871798
ctx = ctx,
@@ -1965,7 +1976,6 @@ app an implementation.
19651976
entitlements = entitlements_support.process_entitlements(
19661977
actions = actions,
19671978
apple_mac_toolchain_info = apple_mac_toolchain_info,
1968-
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
19691979
bundle_id = bundle_id,
19701980
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
19711981
cc_toolchains = cc_toolchain_forwarder,
@@ -2177,7 +2187,6 @@ def _ios_imessage_extension_impl(ctx):
21772187
entitlements = entitlements_support.process_entitlements(
21782188
actions = actions,
21792189
apple_mac_toolchain_info = apple_mac_toolchain_info,
2180-
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
21812190
bundle_id = bundle_id,
21822191
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
21832192
cc_toolchains = cc_toolchain_forwarder,
@@ -2454,7 +2463,6 @@ def _ios_sticker_pack_extension_impl(ctx):
24542463
entitlements = entitlements_support.process_entitlements(
24552464
actions = actions,
24562465
apple_mac_toolchain_info = apple_mac_toolchain_info,
2457-
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
24582466
bundle_id = bundle_id,
24592467
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
24602468
cc_toolchains = cc_toolchain_forwarder,
@@ -3075,6 +3083,12 @@ fashion, such as a Cocoapod.
30753083
A list of `.h` files that will be publicly exposed by this framework. These headers should have
30763084
framework-relative imports, and if non-empty, an umbrella header named `%{bundle_name}.h` will also
30773085
be generated that imports all of the headers listed here.
3086+
""",
3087+
),
3088+
"secure_features": attr.string_list(
3089+
doc = """
3090+
A list of strings representing Apple Enhanced Security crosstool features that should be enabled for
3091+
this target.
30783092
""",
30793093
),
30803094
"umbrella_header": attr.label(

0 commit comments

Comments
 (0)