Skip to content

Commit 8b1f9c3

Browse files
nglevinluispadron
authored andcommitted
Add dossier codesigning support to all of the XCFramework rules.
Cherry-pick: 6af59ea
1 parent 5262722 commit 8b1f9c3

7 files changed

Lines changed: 231 additions & 36 deletions

File tree

apple/internal/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ bzl_library(
832832
":swift_support",
833833
":transition_support",
834834
"//apple:providers",
835+
"//apple/internal:codesigning_support",
835836
"//apple/internal/aspects:resource_aspect",
836837
"//apple/internal/aspects:resource_aspect_hint",
837838
"//apple/internal/aspects:swift_usage_aspect",

apple/internal/codesigning_support.bzl

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,28 @@ def _codesignopts_from_rule_ctx(ctx):
7171
for opt in ctx.attr.codesignopts
7272
]
7373

74-
def _preferred_codesigning_identity(platform_prerequisites):
75-
"""Returns the preferred codesigning identity from platform prerequisites"""
76-
if not platform_prerequisites.platform.is_device:
74+
def _preferred_codesigning_identity(
75+
*,
76+
build_settings,
77+
objc_fragment,
78+
requires_adhoc_signing):
79+
"""Returns the preferred codesigning identity from platform prerequisites.
80+
81+
Args:
82+
build_settings: The build settings from apple_xplat_toolchain_info or platform_prerequisites.
83+
objc_fragment: The objc fragment interface from ctx.fragments.objc.
84+
requires_adhoc_signing: Whether this signing operation requires adhoc signing with the adhoc
85+
pseudo identity. i.e. if this is a simulator build.
86+
"""
87+
if requires_adhoc_signing:
7788
return _ADHOC_PSEUDO_IDENTITY
78-
build_settings = platform_prerequisites.build_settings
7989
if build_settings:
80-
objc_fragment = platform_prerequisites.objc_fragment
8190
if objc_fragment:
8291
# TODO(b/252873771): Remove this fallback when the native Bazel flag
8392
# ios_signing_cert_name is removed.
8493
return (build_settings.signing_certificate_name or
8594
objc_fragment.signing_certificate_name)
86-
else:
87-
return build_settings.signing_certificate_name
95+
return build_settings.signing_certificate_name
8896
return None
8997

9098
def _codesign_args_for_path(
@@ -131,7 +139,11 @@ def _codesign_args_for_path(
131139

132140
# First, try to use the identity passed on the command line, if any. If it's a simulator build,
133141
# use an ad hoc identity.
134-
identity = _preferred_codesigning_identity(platform_prerequisites)
142+
identity = _preferred_codesigning_identity(
143+
build_settings = platform_prerequisites.build_settings,
144+
objc_fragment = platform_prerequisites.objc_fragment,
145+
requires_adhoc_signing = not platform_prerequisites.platform.is_device,
146+
)
135147
if not identity:
136148
if provisioning_profile:
137149
cmd_codesigning.extend([
@@ -294,10 +306,12 @@ def _should_sign_simulator_bundles(
294306
"""Check if a main bundle should be codesigned.
295307
296308
Args:
309+
config_vars: The config_vars from `ctx.var`.
310+
features: List of features enabled by the user. Typically from `ctx.features`.
311+
rule_descriptor: A rule descriptor for platform and product types from the rule context.
297312
298313
Returns:
299314
True/False for if the bundle should be signed.
300-
301315
"""
302316
if "apple.codesign_simulator_bundles" in config_vars:
303317
# buildifier: disable=print
@@ -348,16 +362,16 @@ def _codesigning_args(
348362
"""Returns a set of codesigning arguments to be passed to the codesigning tool.
349363
350364
Args:
351-
entitlements: The entitlements file to sign with. Can be None.
352-
features: List of features enabled by the user. Typically from `ctx.features`.
353-
full_archive_path: The full path to the codesigning target.
354-
is_framework: If the target is a framework. False by default.
355-
platform_prerequisites: Struct containing information on the platform being targeted.
356-
provisioning_profile: File for the provisioning profile.
357-
rule_descriptor: A rule descriptor for platform and product types from the rule context.
365+
entitlements: The entitlements file to sign with. Can be None.
366+
features: List of features enabled by the user. Typically from `ctx.features`.
367+
full_archive_path: The full path to the codesigning target.
368+
is_framework: If the target is a framework. False by default.
369+
platform_prerequisites: Struct containing information on the platform being targeted.
370+
provisioning_profile: File for the provisioning profile.
371+
rule_descriptor: A rule descriptor for platform and product types from the rule context.
358372
359373
Returns:
360-
A list containing the arguments to pass to the codesigning tool.
374+
A list containing the arguments to pass to the codesigning tool.
361375
"""
362376
should_sign_bundles = _should_sign_bundles(
363377
provisioning_profile = provisioning_profile,
@@ -479,30 +493,39 @@ def _codesigning_command(
479493
)
480494

481495
def _generate_codesigning_dossier_action(
496+
*,
482497
actions,
483-
label_name,
498+
apple_fragment,
499+
codesign_identity,
484500
dossier_codesigningtool,
485501
embedded_dossiers,
486502
entitlements,
487503
output_discriminator,
488504
output_dossier,
489-
platform_prerequisites,
490-
provisioning_profile):
505+
label_name,
506+
provisioning_profile,
507+
target_signs_with_entitlements,
508+
xcode_config):
491509
"""Generates a codesigning dossier based on parameters.
492510
493511
Args:
494512
actions: The actions provider from `ctx.actions`.
495-
label_name: Name of the target being built.
513+
apple_fragment: The apple fragment from `ctx.fragments.apple` to use for the action.
514+
codesign_identity: The identity for the dossier to sign with.
496515
dossier_codesigningtool: The files_to_run for the code signing tool.
497516
embedded_dossiers: An optional List of Structs generated from
498517
`embedded_codesigning_dossier` that should also be included in this
499518
dossier.
500519
entitlements: Optional file representing the entitlements to sign with.
501520
output_discriminator: A string to differentiate between different target intermediate files
502521
or `None`.
503-
output_dossier: The `File` representing the output dossier file - the zipped dossier will be placed here.
504-
platform_prerequisites: Struct containing information on the platform being targeted.
522+
output_dossier: The `File` representing the output dossier file - the zipped dossier will be
523+
placed here.
524+
label_name: Name of the target being built.
505525
provisioning_profile: The provisioning profile file. May be `None`.
526+
target_signs_with_entitlements: Whether the target platform needs signing with entitlements,
527+
which is true for non-simulator builds.
528+
xcode_config: The `apple_common.XcodeVersionConfig` provider from the context.
506529
"""
507530
input_files = [x.dossier_file for x in embedded_dossiers]
508531

@@ -511,16 +534,15 @@ def _generate_codesigning_dossier_action(
511534

512535
dossier_arguments = ["--output", output_dossier.path, "--zip"]
513536

514-
# Try to use the identity passed on the command line, if any. If it's a simulator build, use an
515-
# ad hoc identity.
516-
codesign_identity = _preferred_codesigning_identity(platform_prerequisites)
537+
# Try to use the identity passed through, if any. Use the ad-hoc pseudo-identity if no identity
538+
# or provisioning profile is passed through.
517539
if not codesign_identity and not provisioning_profile:
518540
codesign_identity = _ADHOC_PSEUDO_IDENTITY
519541
if codesign_identity:
520542
dossier_arguments.extend(["--codesign_identity", codesign_identity])
521543
else:
522544
dossier_arguments.append("--infer_identity")
523-
if entitlements and platform_prerequisites.platform.is_device:
545+
if entitlements and target_signs_with_entitlements:
524546
# Entitlements are embedded as segments of the linked simulator binary. They should not be
525547
# used for signing simulator binaries.
526548
input_files.append(entitlements)
@@ -559,14 +581,14 @@ def _generate_codesigning_dossier_action(
559581

560582
apple_support.run(
561583
actions = actions,
562-
apple_fragment = platform_prerequisites.apple_fragment,
584+
apple_fragment = apple_fragment,
563585
arguments = args,
564586
executable = dossier_codesigningtool,
565587
inputs = input_files,
566588
mnemonic = mnemonic,
567589
outputs = [output_dossier],
568590
progress_message = progress_message,
569-
xcode_config = platform_prerequisites.xcode_version_config,
591+
xcode_config = xcode_config,
570592
)
571593

572594
def _post_process_and_sign_archive_action(
@@ -826,6 +848,7 @@ codesigning_support = struct(
826848
embedded_codesigning_dossier = _embedded_codesigning_dossier,
827849
generate_codesigning_dossier_action = _generate_codesigning_dossier_action,
828850
post_process_and_sign_archive_action = _post_process_and_sign_archive_action,
851+
preferred_codesigning_identity = _preferred_codesigning_identity,
829852
should_sign_bundles = _should_sign_bundles,
830853
sign_binary_action = _sign_binary_action,
831854
)

apple/internal/partials/codesigning_dossier.bzl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,25 @@ def _codesigning_dossier_partial_impl(
274274
bundle_name = bundle_name,
275275
) if bundle_location else None
276276

277+
codesign_identity = codesigning_support.preferred_codesigning_identity(
278+
build_settings = platform_prerequisites.build_settings,
279+
objc_fragment = platform_prerequisites.objc_fragment,
280+
requires_adhoc_signing = not platform_prerequisites.platform.is_device,
281+
)
282+
277283
codesigning_support.generate_codesigning_dossier_action(
278284
actions = actions,
279-
label_name = label_name,
285+
apple_fragment = platform_prerequisites.apple_fragment,
286+
codesign_identity = codesign_identity,
280287
dossier_codesigningtool = apple_mac_toolchain_info.dossier_codesigningtool,
281-
output_discriminator = output_discriminator,
282-
output_dossier = output_dossier,
283-
platform_prerequisites = platform_prerequisites,
284288
embedded_dossiers = embedded_codesign_dossiers,
285289
entitlements = entitlements,
290+
label_name = label_name,
291+
output_discriminator = output_discriminator,
292+
output_dossier = output_dossier,
286293
provisioning_profile = provisioning_profile,
294+
target_signs_with_entitlements = platform_prerequisites.platform.is_device,
295+
xcode_config = platform_prerequisites.xcode_version_config,
287296
)
288297

289298
embedded_dossier_depset = None

apple/internal/xcframework_rules.bzl

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ load(
3838
"//apple/internal:cc_info_support.bzl",
3939
"cc_info_support",
4040
)
41+
load(
42+
"//apple/internal:codesigning_support.bzl",
43+
"codesigning_support",
44+
)
4145
load(
4246
"//apple/internal:experimental.bzl",
4347
"is_experimental_tree_artifact_enabled",
@@ -72,6 +76,7 @@ load(
7276
"AppleBundleVersionInfo",
7377
"ApplePlatformInfo",
7478
"new_applebundleinfo",
79+
"new_applecodesigningdossierinfo",
7580
"new_applestaticxcframeworkbundleinfo",
7681
"new_applexcframeworkbundleinfo",
7782
)
@@ -1173,6 +1178,50 @@ def _create_xcframework_bundle(
11731178
progress_message = "Bundling %s" % label_name,
11741179
)
11751180

1181+
def _create_xcframework_codesigning_dossier(
1182+
*,
1183+
actions,
1184+
apple_fragment,
1185+
apple_mac_toolchain_info,
1186+
build_settings,
1187+
objc_fragment,
1188+
rule_label,
1189+
xcode_config):
1190+
"""Generates the codesigning dossier for an XCFramework."""
1191+
output_dossier = actions.declare_file("%s_dossier.zip" % rule_label.name)
1192+
1193+
codesign_identity = codesigning_support.preferred_codesigning_identity(
1194+
build_settings = build_settings,
1195+
objc_fragment = objc_fragment,
1196+
# Never adhoc sign XCFrameworks; the SDK signing requires a valid code signing identity
1197+
# corresponding to a certificate.
1198+
requires_adhoc_signing = False,
1199+
)
1200+
1201+
codesigning_support.generate_codesigning_dossier_action(
1202+
actions = actions,
1203+
apple_fragment = apple_fragment,
1204+
codesign_identity = codesign_identity,
1205+
dossier_codesigningtool = apple_mac_toolchain_info.dossier_codesigningtool,
1206+
embedded_dossiers = [],
1207+
entitlements = None,
1208+
label_name = rule_label.name,
1209+
output_discriminator = None,
1210+
output_dossier = output_dossier,
1211+
provisioning_profile = None,
1212+
target_signs_with_entitlements = False, # Frameworks are never signed with entitlements.
1213+
xcode_config = xcode_config,
1214+
)
1215+
1216+
return struct(
1217+
output_groups = {
1218+
"dossier": depset([output_dossier]),
1219+
},
1220+
providers = [new_applecodesigningdossierinfo(
1221+
dossier = output_dossier,
1222+
)],
1223+
)
1224+
11761225
def _apple_xcframework_impl(ctx):
11771226
"""Implementation of apple_xcframework."""
11781227
actions = ctx.actions
@@ -1208,6 +1257,8 @@ def _apple_xcframework_impl(ctx):
12081257
tree_artifact_is_enabled = True
12091258
outputs_archive = actions.declare_directory(bundle_name + ".xcframework")
12101259

1260+
build_settings = apple_xplat_toolchain_info.build_settings
1261+
12111262
# Add the disable_legacy_signing feature to the list of features
12121263
# TODO(b/72148898): Remove this when dossier based signing becomes the default.
12131264
features = ctx.features
@@ -1262,7 +1313,7 @@ def _apple_xcframework_impl(ctx):
12621313

12631314
link_result = linking_support.register_binary_linking_action(
12641315
ctx,
1265-
build_settings = apple_xplat_toolchain_info.build_settings,
1316+
build_settings = build_settings,
12661317
bundle_name = bundle_name,
12671318
cc_toolchains = cc_toolchain_forwarder,
12681319
# Frameworks do not have entitlements.
@@ -1343,6 +1394,16 @@ def _apple_xcframework_impl(ctx):
13431394
xcode_config = xcode_version_config,
13441395
)
13451396

1397+
dossier_outputs = _create_xcframework_codesigning_dossier(
1398+
actions = actions,
1399+
apple_fragment = apple_fragment,
1400+
apple_mac_toolchain_info = apple_mac_toolchain_info,
1401+
build_settings = build_settings,
1402+
objc_fragment = objc_fragment,
1403+
rule_label = rule_label,
1404+
xcode_config = xcode_version_config,
1405+
)
1406+
13461407
processor_output = [
13471408
# Limiting the contents of AppleBundleInfo to what is necessary for testing and validation.
13481409
new_applebundleinfo(
@@ -1367,10 +1428,11 @@ def _apple_xcframework_impl(ctx):
13671428
),
13681429
OutputGroupInfo(
13691430
**outputs.merge_output_groups(
1431+
dossier_outputs.output_groups,
13701432
*bundled_artifacts.framework_output_groups
13711433
)
13721434
),
1373-
]
1435+
] + dossier_outputs.providers
13741436
return processor_output
13751437

13761438
apple_xcframework = rule_factory.create_apple_rule(
@@ -1613,6 +1675,8 @@ def _apple_static_xcframework_impl(ctx):
16131675
tree_artifact_is_enabled = True
16141676
outputs_archive = actions.declare_directory(bundle_name + ".xcframework")
16151677

1678+
build_settings = apple_xplat_toolchain_info.build_settings
1679+
16161680
_validate_resource_attrs(
16171681
all_attrs = ctx.attr,
16181682
bundle_format = bundle_format,
@@ -1712,6 +1776,16 @@ def _apple_static_xcframework_impl(ctx):
17121776
xcode_config = xcode_version_config,
17131777
)
17141778

1779+
dossier_outputs = _create_xcframework_codesigning_dossier(
1780+
actions = actions,
1781+
apple_fragment = apple_fragment,
1782+
apple_mac_toolchain_info = apple_mac_toolchain_info,
1783+
build_settings = build_settings,
1784+
objc_fragment = objc_fragment,
1785+
rule_label = rule_label,
1786+
xcode_config = xcode_version_config,
1787+
)
1788+
17151789
return [
17161790
# Limiting the contents of AppleBundleInfo to what is necessary for testing and validation.
17171791
new_applebundleinfo(
@@ -1730,10 +1804,11 @@ def _apple_static_xcframework_impl(ctx):
17301804
),
17311805
OutputGroupInfo(
17321806
**outputs.merge_output_groups(
1807+
dossier_outputs.output_groups,
17331808
*bundled_artifacts.framework_output_groups
17341809
)
17351810
),
1736-
]
1811+
] + dossier_outputs.providers
17371812

17381813
apple_static_xcframework = rule_factory.create_apple_rule(
17391814
cfg = transition_support.xcframework_base_transition,

0 commit comments

Comments
 (0)