-
Notifications
You must be signed in to change notification settings - Fork 273
/
xcframework_rules.bzl
1243 lines (1161 loc) · 50.7 KB
/
xcframework_rules.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2021 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Implementation of the xcframework rules."""
load("@bazel_skylib//lib:partial.bzl", "partial")
load("@bazel_skylib//lib:paths.bzl", "paths")
load(
"@build_bazel_apple_support//lib:apple_support.bzl",
"apple_support",
)
load("@build_bazel_rules_swift//swift:swift.bzl", "SwiftInfo")
load(
"//apple:providers.bzl",
"AppleBundleVersionInfo",
)
load(
"//apple/internal:apple_product_type.bzl",
"apple_product_type",
)
load(
"//apple/internal:apple_toolchains.bzl",
"AppleMacToolsToolchainInfo",
"AppleXPlatToolsToolchainInfo",
)
load(
"//apple/internal:cc_info_support.bzl",
"cc_info_support",
)
load(
"//apple/internal:experimental.bzl",
"is_experimental_tree_artifact_enabled",
)
load(
"//apple/internal:features_support.bzl",
"features_support",
)
load(
"//apple/internal:intermediates.bzl",
"intermediates",
)
load(
"//apple/internal:linking_support.bzl",
"linking_support",
)
load(
"//apple/internal:outputs.bzl",
"outputs",
)
load(
"//apple/internal:partials.bzl",
"partials",
)
load(
"//apple/internal:platform_support.bzl",
"platform_support",
)
load(
"//apple/internal:processor.bzl",
"processor",
)
load(
"//apple/internal:providers.bzl",
"new_applebundleinfo",
"new_applestaticxcframeworkbundleinfo",
"new_applexcframeworkbundleinfo",
)
load(
"//apple/internal:resources.bzl",
"resources",
)
load(
"//apple/internal:rule_attrs.bzl",
"rule_attrs",
)
load(
"//apple/internal:rule_factory.bzl",
"rule_factory",
)
load(
"//apple/internal:rule_support.bzl",
"rule_support",
)
load(
"//apple/internal:swift_support.bzl",
"swift_support",
)
load(
"//apple/internal:transition_support.bzl",
"transition_support",
)
load(
"//apple/internal/aspects:resource_aspect.bzl",
"apple_resource_aspect",
)
load(
"//apple/internal/aspects:swift_usage_aspect.bzl",
"swift_usage_aspect",
)
load(
"//apple/internal/utils:files.bzl",
"files",
)
def _group_link_outputs_by_library_identifier(
*,
actions,
apple_fragment,
deps,
label_name,
link_result,
xcode_config):
"""Groups linking outputs by library identifier with additional platform information.
Linking outputs artifacts are combined using the lipo tool if necessary due to grouping.
Args:
actions: The actions provider from `ctx.actions`.
apple_fragment: An Apple fragment (ctx.fragments.apple).
deps: Label list of dependencies from rule context (ctx.split_attr.deps).
label_name: Name of the target being built.
link_result: The struct returned by `linking_support.register_binary_linking_action`.
xcode_config: The `apple_common.XcodeVersionConfig` provider from the context.
Returns:
A list of structs with the following fields; `architectures` containing a list of the
architectures that the binary was built with, `binary` referencing the output binary linked
with the `lipo` tool if necessary, or referencing a symlink to the original binary if not,
`dsym_binaries` which is a mapping of architectures to dsym binaries if any were created,
`environment` to reference the target environment the binary was built for, `linkmaps` which
is a mapping of architectures to linkmaps if any were created, and `platform` to reference
the target platform the binary was built for.
"""
linking_type = None
for attr_name in ["binary", "library"]:
if hasattr(link_result, attr_name):
linking_type = attr_name
break
if not linking_type:
fail("Apple linking APIs output struct must define either 'binary' or 'library'.\n" +
"This is most likely a rules_apple bug, please file a bug with reproduction steps.")
# Organize each output as a platform_environment, where each can accept one or more archs.
link_outputs_by_framework = {}
# Iterate through the outputs of the registered linking action, match archs to platform and
# environment combinations.
for link_output in link_result.outputs:
framework_key = link_output.platform + "_" + link_output.environment
if link_outputs_by_framework.get(framework_key):
link_outputs_by_framework[framework_key].append(link_output)
else:
link_outputs_by_framework[framework_key] = [link_output]
link_outputs_by_library_identifier = {}
# Iterate through the structure again, this time creating a structure equivalent to link_result
# .outputs but with .architecture replaced with .architectures, .dsym_binary replaced with
# .dsym_binaries, and .linkmap replaced with .linkmaps
for framework_key, link_outputs in link_outputs_by_framework.items():
inputs = [getattr(output, linking_type) for output in link_outputs]
filename = "{}_{}".format(label_name, framework_key)
extension = inputs[0].extension
if extension != "":
filename = "{}.{}".format(filename, extension)
fat_binary = actions.declare_file(filename)
linking_support.lipo_or_symlink_inputs(
actions = actions,
inputs = inputs,
output = fat_binary,
apple_fragment = apple_fragment,
xcode_config = xcode_config,
)
architectures = []
dsym_binaries = {}
linkmaps = {}
split_attr_keys = []
swift_infos = {}
uses_swift = False
for link_output in link_outputs:
split_attr_key = transition_support.xcframework_split_attr_key(
arch = link_output.architecture,
environment = link_output.environment,
platform_type = link_output.platform,
)
architectures.append(link_output.architecture)
split_attr_keys.append(split_attr_key)
# If there's any Swift dependencies on this framework rule,
# look for providers to see if we need to generate Swift interfaces.
if swift_support.uses_swift(deps[split_attr_key]):
uses_swift = True
for dep in deps[split_attr_key]:
if SwiftInfo in dep:
swift_infos[link_output.architecture] = dep[SwiftInfo]
# static library linking does not support dsym, and linkmaps yet.
if linking_type == "binary":
dsym_binaries[link_output.architecture] = link_output.dsym_binary
linkmaps[link_output.architecture] = link_output.linkmap
environment = link_outputs[0].environment
platform = link_outputs[0].platform
library_identifier = _library_identifier(
architectures = architectures,
environment = environment,
platform = platform,
)
link_outputs_by_library_identifier[library_identifier] = struct(
architectures = architectures,
binary = fat_binary,
dsym_binaries = dsym_binaries,
environment = environment,
linkmaps = linkmaps,
platform = platform,
split_attr_keys = split_attr_keys,
swift_infos = swift_infos,
uses_swift = uses_swift,
)
return link_outputs_by_library_identifier
def _library_identifier(*, architectures, environment, platform):
"""Return a unique identifier for an embedded framework to disambiguate it from others.
Args:
architectures: The architectures of the target that was built. For example, `x86_64` or
`arm64`.
environment: The environment of the target that was built, which corresponds to the
toolchain's target triple values as reported by `apple_common` linking APIs.
Typically `device` or `simulator`.
platform: The platform of the target that was built, which corresponds to the toolchain's
target triple values as reported by `apple_common` linking APIs.
For example, `ios`, `macos`, `tvos`, `visionos` or `watchos`.
Returns:
A string that can be used to determine the subfolder this embedded framework will be found
in the final XCFramework bundle. This mirrors the formatting for subfolders as given by the
xcodebuild -create-xcframework tool.
"""
library_identifier = "{}-{}".format(platform, "_".join(architectures))
if environment != "device":
library_identifier += "-{}".format(environment)
return library_identifier
def _unioned_attrs(*, attr_names, split_attr, split_attr_keys):
"""Return a list of attribute values unioned for the given attributes, by split attribute key.
Args:
attr_names: The rule attributes to union. Assumed to contain lists of values.
split_attr: The Starlark interface for 1:2+ transitions, typically from `ctx.split_attr`.
split_attr_keys: A list of strings representing each 1:2+ transition key to check.
Returns:
A new list of attributes based on the union of all rule attributes given, by split
attribute key.
"""
unioned_attrs = []
for attr_name in attr_names:
attr = getattr(split_attr, attr_name)
if not attr:
continue
for split_attr_key in split_attr_keys:
found_attr = attr.get(split_attr_key)
if found_attr:
unioned_attrs += found_attr
return unioned_attrs
def _available_library_dictionary(
*,
architectures,
environment,
headers_path,
library_identifier,
library_path,
platform):
"""Generates a dictionary containing keys referencing a framework in the XCFramework bundle.
Args:
architectures: The architectures of the target that was built. For example, `x86_64` or
`arm64`.
environment: The environment of the target that was built, which corresponds to the
toolchain's target triple values as reported by `apple_common` linking APIs.
Typically `device` or `simulator`.
headers_path: A string representing the path inside the library identifier to reference
bundled headers and modulemap files.
library_identifier: A string representing the path to the framework to reference in the
xcframework bundle.
library_path: A string representing the path inside the library identifier to reference in
the xcframework bundle.
platform: The platform of the target that was built, which corresponds to the toolchain's
target triple values as reported by `apple_common` linking APIs.
For example, `ios`, `macos`, `tvos`, `visionos`, or `watchos`.
Returns:
A dictionary containing keys representing how a given framework should be referenced in the
root Info.plist of a given XCFramework bundle.
"""
available_library = {
"LibraryIdentifier": library_identifier,
"LibraryPath": library_path,
"SupportedArchitectures": architectures,
"SupportedPlatform": platform,
}
if headers_path:
available_library["HeadersPath"] = headers_path
if environment != "device":
available_library["SupportedPlatformVariant"] = environment
return available_library
def _create_xcframework_root_infoplist(
*,
actions,
apple_fragment,
available_libraries,
plisttool,
rule_label,
xcode_config):
"""Generates a root Info.plist for a given XCFramework.
Args:
actions: The actions provider from `ctx.actions`.
apple_fragment: An Apple fragment (ctx.fragments.apple).
available_libraries: A dictionary containing keys representing how a given framework should
be referenced in the root Info.plist of a given XCFramework bundle.
plisttool: A files_to_run for the plist tool.
rule_label: The label of the target being analyzed.
xcode_config: The `apple_common.XcodeVersionConfig` provider from the context.
Returns:
A `File` representing a root Info.plist to be embedded within an XCFramework bundle.
"""
root_info_plist = intermediates.file(
actions = actions,
target_name = rule_label.name,
output_discriminator = None,
file_name = "Info.plist",
)
default_xcframework_plist = {
"CFBundlePackageType": "XFWK",
"XCFrameworkFormatVersion": "1.0",
}
plisttool_control = struct(
binary = False,
output = root_info_plist.path,
plists = [{"AvailableLibraries": available_libraries}, default_xcframework_plist],
target = str(rule_label),
)
plisttool_control_file = intermediates.file(
actions = actions,
target_name = rule_label.name,
output_discriminator = None,
file_name = "xcframework_plisttool_control.json",
)
actions.write(
output = plisttool_control_file,
content = json.encode(plisttool_control),
)
apple_support.run(
actions = actions,
apple_fragment = apple_fragment,
arguments = [plisttool_control_file.path],
executable = plisttool,
inputs = [plisttool_control_file],
mnemonic = "CreateXCFrameworkRootInfoPlist",
outputs = [root_info_plist],
xcode_config = xcode_config,
)
return root_info_plist
def _create_xcframework_bundle(
*,
actions,
bundle_name,
bundletool,
framework_archive_files,
framework_archive_merge_files,
framework_archive_merge_zips = [],
label_name,
output_archive,
root_info_plist):
"""Generates the bundle archive for an XCFramework.
Args:
actions: The actions providerx from `ctx.actions`.
bundle_name: The name of the XCFramework bundle.
bundletool: A files to run for the bundle tool.
framework_archive_files: A list of depsets referencing files to be used as inputs to the
bundling action. This should include every archive referenced as a "src" of
framework_archive_merge_zips.
framework_archive_merge_files: A list of structs representing files that should be merged
into the bundle. Each struct contains two fields: "src", the path of the file that
should be merged into the bundle; and "dest", the path inside the bundle where the file
should be placed. The destination path is relative to `bundle_path`.
framework_archive_merge_zips: A list of structs representing ZIP archives whose contents
should be merged into the bundle. Each struct contains two fields: "src", the path of
the archive whose contents should be merged into the bundle; and "dest", the path inside
the bundle where the ZIPs contents should be placed. The destination path is relative to
`bundle_path`.
label_name: Name of the target being built.
output_archive: The file representing the final bundled archive.
root_info_plist: A `File` representing a fully formed root Info.plist for this XCFramework.
"""
bundletool_control_file = intermediates.file(
actions = actions,
target_name = label_name,
output_discriminator = None,
file_name = "xcframework_bundletool_control.json",
)
root_info_plist_merge_file = struct(src = root_info_plist.path, dest = "Info.plist")
bundletool_control = struct(
bundle_merge_files = [root_info_plist_merge_file] + framework_archive_merge_files,
bundle_merge_zips = framework_archive_merge_zips,
bundle_path = bundle_name + ".xcframework",
output = output_archive.path,
compress = True,
)
actions.write(
output = bundletool_control_file,
content = json.encode(bundletool_control),
)
actions.run(
arguments = [bundletool_control_file.path],
executable = bundletool,
inputs = depset(
direct = [bundletool_control_file, root_info_plist],
transitive = framework_archive_files,
),
mnemonic = "CreateXCFrameworkBundle",
outputs = [output_archive],
progress_message = "Bundling %s" % label_name,
)
def _apple_xcframework_impl(ctx):
"""Implementation of apple_xcframework."""
actions = ctx.actions
apple_mac_toolchain_info = ctx.attr._mac_toolchain[AppleMacToolsToolchainInfo]
apple_xplat_toolchain_info = ctx.attr._xplat_toolchain[AppleXPlatToolsToolchainInfo]
bundle_name = ctx.attr.bundle_name or ctx.attr.name
executable_name = getattr(ctx.attr, "executable_name", bundle_name)
deps = ctx.split_attr.deps
if (apple_xplat_toolchain_info.build_settings.use_tree_artifacts_outputs or
is_experimental_tree_artifact_enabled(config_vars = ctx.var)):
fail("The apple_xcframework rule does not yet support the experimental tree artifact. " +
"Please ensure that the `apple.experimental.tree_artifact_outputs` variable is not " +
"set to 1 on the command line or in your active build configuration.")
# Add the disable_legacy_signing feature to the list of features
# TODO(b/72148898): Remove this when dossier based signing becomes the default.
features = ctx.features
features.append("disable_legacy_signing")
label = ctx.label
# Bundle extension needs to be ".xcframework" for root bundle, but macos/ios/tvos will always
# be ".framework"
nested_bundle_extension = ".framework"
# Similarly, bundle_id is expected to be in terms of the bundle ID for each embedded framework,
# as this value is not used in the XCFramework's root Info.plist.
nested_bundle_id = ctx.attr.bundle_id
link_result = linking_support.register_binary_linking_action(
ctx,
# Frameworks do not have entitlements.
entitlements = None,
exported_symbols_lists = ctx.files.exported_symbols_lists,
extra_linkopts = [
# iOS, tvOS and watchOS single target app framework binaries live in
# Application.app/Frameworks/Framework.framework/Framework
# watchOS 2 extension-dependent app framework binaries live in
# Application.app/PlugIns/Extension.appex/Frameworks/Framework.framework/Framework
#
# iOS, tvOS and watchOS single target app frameworks are packaged in executable as
# Application.app/Frameworks
# watchOS 2 extension-dependent app frameworks are packaged in executable as
# Application.app/PlugIns/Extension.appex/Frameworks
#
# While different, these resolve to the same paths relative to their respective
# executables. Only macOS (which is not yet supported) is an outlier; this will require
# changes to native Bazel linking logic for Apple binary targets.
"-Wl,-rpath,@executable_path/Frameworks",
"-dynamiclib",
"-Wl,-install_name,@rpath/{name}{extension}/{name}".format(
extension = nested_bundle_extension,
name = bundle_name,
),
] + (["-fapplication-extension"] if ctx.attr.extension_safe else []),
platform_prerequisites = None,
# All required knowledge for 3P facing frameworks is passed directly through the given
# `extra_linkopts`; no rule_descriptor is needed to share with this linking action.
rule_descriptor = None,
stamp = ctx.attr.stamp,
)
link_outputs_by_library_identifier = _group_link_outputs_by_library_identifier(
actions = actions,
apple_fragment = ctx.fragments.apple,
deps = deps,
label_name = label.name,
link_result = link_result,
xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig],
)
available_libraries = []
framework_archive_files = []
framework_archive_merge_files = []
framework_archive_merge_zips = []
framework_output_files = []
framework_output_groups = []
features = features_support.compute_enabled_features(
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
for library_identifier, link_output in link_outputs_by_library_identifier.items():
binary_artifact = link_output.binary
rule_descriptor = rule_support.rule_descriptor(
platform_type = link_output.platform,
product_type = apple_product_type.framework,
)
platform_prerequisites = platform_support.platform_prerequisites(
apple_fragment = ctx.fragments.apple,
build_settings = apple_xplat_toolchain_info.build_settings,
config_vars = ctx.var,
cpp_fragment = ctx.fragments.cpp,
device_families = ctx.attr.families_required.get(
link_output.platform,
default = rule_descriptor.allowed_device_families,
),
explicit_minimum_deployment_os = ctx.attr.minimum_deployment_os_versions.get(
link_output.platform,
),
explicit_minimum_os = ctx.attr.minimum_os_versions.get(link_output.platform),
features = features,
objc_fragment = ctx.fragments.objc,
platform_type_string = link_output.platform,
uses_swift = link_output.uses_swift,
xcode_version_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig],
)
overridden_predeclared_outputs = struct(
archive = intermediates.file(
actions = actions,
target_name = label.name,
output_discriminator = library_identifier,
file_name = label.name + ".zip",
),
)
resource_deps = _unioned_attrs(
attr_names = ["data", "deps"],
split_attr = ctx.split_attr,
split_attr_keys = link_output.split_attr_keys,
)
top_level_infoplists = resources.collect(
attr = ctx.split_attr,
res_attrs = ["infoplists"],
split_attr_keys = link_output.split_attr_keys,
)
top_level_resources = resources.collect(
attr = ctx.split_attr,
res_attrs = ["data"],
split_attr_keys = link_output.split_attr_keys,
)
environment_plist = files.get_file_with_name(
name = "environment_plist_{platform}".format(
platform = link_output.platform,
),
files = ctx.files._environment_plist_files,
)
processor_partials = [
partials.apple_bundle_info_partial(
actions = actions,
bundle_extension = nested_bundle_extension,
bundle_id = nested_bundle_id,
bundle_name = bundle_name,
entitlements = None,
executable_name = executable_name,
extension_safe = ctx.attr.extension_safe,
label_name = label.name,
output_discriminator = library_identifier,
platform_prerequisites = platform_prerequisites,
predeclared_outputs = overridden_predeclared_outputs,
product_type = rule_descriptor.product_type,
rule_descriptor = rule_descriptor,
),
partials.binary_partial(
actions = actions,
binary_artifact = binary_artifact,
bundle_name = bundle_name,
executable_name = executable_name,
label_name = label.name,
output_discriminator = library_identifier,
),
partials.debug_symbols_partial(
actions = actions,
bundle_extension = nested_bundle_extension,
bundle_name = bundle_name,
debug_discriminator = link_output.platform + "_" + link_output.environment,
dsym_binaries = link_output.dsym_binaries,
dsym_info_plist_template = apple_mac_toolchain_info.dsym_info_plist_template,
executable_name = executable_name,
label_name = label.name,
linkmaps = link_output.linkmaps,
output_discriminator = library_identifier,
platform_prerequisites = platform_prerequisites,
plisttool = apple_mac_toolchain_info.plisttool,
rule_label = label,
version = ctx.attr.version,
),
partials.resources_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
bundle_extension = nested_bundle_extension,
bundle_id = nested_bundle_id,
bundle_name = bundle_name,
environment_plist = environment_plist,
executable_name = executable_name,
launch_storyboard = None,
output_discriminator = library_identifier,
platform_prerequisites = platform_prerequisites,
resource_deps = resource_deps,
rule_descriptor = rule_descriptor,
rule_label = label,
top_level_infoplists = top_level_infoplists,
top_level_resources = top_level_resources,
version = ctx.attr.version,
version_keys_required = False,
),
partials.swift_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
),
]
if link_output.uses_swift and link_output.swift_infos:
processor_partials.append(
partials.swift_framework_partial(
actions = actions,
bundle_name = bundle_name,
label_name = label.name,
output_discriminator = library_identifier,
swift_infos = link_output.swift_infos,
),
)
else:
processor_partials.append(
partials.framework_header_modulemap_partial(
actions = actions,
bundle_name = bundle_name,
hdrs = ctx.files.public_hdrs,
label_name = label.name,
output_discriminator = library_identifier,
umbrella_header = ctx.file.umbrella_header,
),
)
processor_result = processor.process(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
bundle_extension = nested_bundle_extension,
bundle_name = bundle_name,
entitlements = None,
features = features,
ipa_post_processor = None,
output_discriminator = library_identifier,
partials = processor_partials,
platform_prerequisites = platform_prerequisites,
predeclared_outputs = overridden_predeclared_outputs,
process_and_sign_template = apple_mac_toolchain_info.process_and_sign_template,
provisioning_profile = None,
rule_descriptor = rule_descriptor,
rule_label = label,
)
for provider in processor_result.providers:
# Save the framework archive.
if getattr(provider, "archive", None):
# Repackage every archive found for bundle_merge_zips in the final bundler action.
framework_archive_merge_zips.append(
struct(src = provider.archive.path, dest = library_identifier),
)
# Save a reference to those archives as file-friendly inputs to the bundler action.
framework_archive_files.append(depset([provider.archive]))
# Save the dSYMs.
if getattr(provider, "dsyms", None):
framework_output_files.append(depset(transitive = [provider.dsyms]))
framework_output_groups.append({"dsyms": provider.dsyms})
# Save the linkmaps.
if getattr(provider, "linkmaps", None):
framework_output_files.append(depset(transitive = [provider.linkmaps]))
framework_output_groups.append({"linkmaps": provider.linkmaps})
# Save additional library details for the XCFramework's root info plist.
available_libraries.append(_available_library_dictionary(
architectures = link_output.architectures,
environment = link_output.environment,
headers_path = None,
library_identifier = library_identifier,
library_path = bundle_name + nested_bundle_extension,
platform = link_output.platform,
))
root_info_plist = _create_xcframework_root_infoplist(
actions = actions,
apple_fragment = ctx.fragments.apple,
available_libraries = available_libraries,
plisttool = apple_mac_toolchain_info.plisttool,
rule_label = label,
xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig],
)
_create_xcframework_bundle(
actions = actions,
bundle_name = bundle_name,
bundletool = apple_xplat_toolchain_info.bundletool,
framework_archive_files = framework_archive_files,
framework_archive_merge_files = framework_archive_merge_files,
framework_archive_merge_zips = framework_archive_merge_zips,
label_name = label.name,
output_archive = ctx.outputs.archive,
root_info_plist = root_info_plist,
)
processor_output = [
# Limiting the contents of AppleBundleInfo to what is necessary for testing and validation.
new_applebundleinfo(
archive = ctx.outputs.archive,
bundle_extension = ".xcframework",
bundle_id = nested_bundle_id,
bundle_name = bundle_name,
executable_name = executable_name,
infoplist = root_info_plist,
platform_type = None,
),
new_applexcframeworkbundleinfo(),
DefaultInfo(
files = depset([ctx.outputs.archive], transitive = framework_output_files),
),
OutputGroupInfo(
**outputs.merge_output_groups(
*framework_output_groups
)
),
]
return processor_output
apple_xcframework = rule_factory.create_apple_rule(
cfg = None,
doc = "Builds and bundles an XCFramework for third-party distribution.",
implementation = _apple_xcframework_impl,
predeclared_outputs = {"archive": "%{name}.xcframework.zip"},
toolchains = [],
attrs = [
rule_attrs.common_tool_attrs(),
rule_attrs.binary_linking_attrs(
deps_cfg = transition_support.xcframework_transition,
extra_deps_aspects = [
apple_resource_aspect,
],
is_test_supporting_rule = False,
requires_legacy_cc_toolchain = False,
),
{
"_environment_plist_files": attr.label_list(
default = [
"//apple/internal:environment_plist_ios",
"//apple/internal:environment_plist_tvos",
],
),
"bundle_id": attr.string(
mandatory = True,
doc = """
The bundle ID (reverse-DNS path followed by app name) for each of the embedded frameworks. If
present, this value will be embedded in an Info.plist within each framework bundle.
""",
),
"bundle_name": attr.string(
mandatory = False,
doc = """
The desired name of the xcframework bundle (without the extension) and the bundles for all embedded
frameworks. If this attribute is not set, then the name of the target will be used instead.
""",
),
"data": attr.label_list(
allow_files = True,
aspects = [apple_resource_aspect],
cfg = transition_support.xcframework_transition,
doc = """
A list of resources or files bundled with the bundle. The resources will be stored in the
appropriate resources location within each of the embedded framework bundles.
""",
),
"extension_safe": attr.bool(
default = False,
doc = """
If true, compiles and links this framework with `-application-extension`, restricting the binary to
use only extension-safe APIs.
""",
),
"families_required": attr.string_list_dict(
doc = """
A list of device families supported by this extension, with platforms such as `ios` as keys. Valid
values are `iphone` and `ipad` for `ios`; at least one must be specified if a platform is defined.
Currently, this only affects processing of `ios` resources.
""",
),
"infoplists": attr.label_list(
allow_empty = False,
allow_files = [".plist"],
cfg = transition_support.xcframework_transition,
doc = """
A list of .plist files that will be merged to form the Info.plist for each of the embedded
frameworks. At least one file must be specified. Please see
[Info.plist Handling](https://github.com/bazelbuild/rules_apple/blob/master/doc/common_info.md#infoplist-handling)
for what is supported.
""",
mandatory = True,
),
"ios": attr.string_list_dict(
doc = """
A dictionary of strings indicating which platform variants should be built for the iOS platform (
`device` or `simulator`) as keys, and arrays of strings listing which architectures should be
built for those platform variants (for example, `x86_64`, `arm64`) as their values.
""",
),
"macos": attr.string_list(
doc = """
A list of strings indicating which architecture should be built for the macOS platform (for example, `x86_64`, `arm64`).
""",
),
"tvos": attr.string_list_dict(
doc = """
A dictionary of strings indicating which platform variants should be built for the tvOS platform (
`device` or `simulator`) as keys, and arrays of strings listing which architectures should be
built for those platform variants (for example, `x86_64`, `arm64`) as their values.
""",
),
"minimum_deployment_os_versions": attr.string_dict(
doc = """
A dictionary of strings indicating the minimum deployment OS version supported by the target,
represented as a dotted version number (for example, "9.0") as values, with their respective
platforms such as `ios` as keys. This is different from `minimum_os_versions`, which is effective
at compile time. Ensure version specific APIs are guarded with `available` clauses.
""",
mandatory = False,
),
"minimum_os_versions": attr.string_dict(
doc = """
A dictionary of strings indicating the minimum OS version supported by the target, represented as a
dotted version number (for example, "8.0") as values, with their respective platforms such as `ios`,
or `tvos` as keys:
minimum_os_versions = {
"ios": "13.0",
"tvos": "15.0",
}
""",
mandatory = True,
),
"public_hdrs": attr.label_list(
allow_files = [".h"],
doc = """
A list of files directly referencing header files to be used as the publicly visible interface for
each of these embedded frameworks. These header files will be embedded within each bundle,
typically in a subdirectory such as `Headers`.
""",
),
"version": attr.label(
providers = [[AppleBundleVersionInfo]],
doc = """
An `apple_bundle_version` target that represents the version for this target. See
[`apple_bundle_version`](https://github.com/bazelbuild/rules_apple/blob/master/doc/rules-versioning.md#apple_bundle_version).
""",
),
"umbrella_header": attr.label(
allow_single_file = [".h"],
doc = """
An optional single .h file to use as the umbrella header for this framework. Usually, this header
will have the same name as this target, so that clients can load the header using the #import
<MyFramework/MyFramework.h> format. If this attribute is not specified (the common use case), an
umbrella header will be generated under the same name as this target.
""",
),
},
],
)
def _apple_static_xcframework_impl(ctx):
"""Implementation of apple_static_xcframework."""
actions = ctx.actions
apple_fragment = ctx.fragments.apple
apple_mac_toolchain_info = ctx.attr._mac_toolchain[AppleMacToolsToolchainInfo]
apple_xplat_toolchain_info = ctx.attr._xplat_toolchain[AppleXPlatToolsToolchainInfo]
bundle_name = ctx.attr.bundle_name or ctx.label.name
deps = ctx.split_attr.deps
label = ctx.label
executable_name = getattr(ctx.attr, "executable_name", bundle_name)
features = features_support.compute_enabled_features(
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
outputs_archive = ctx.outputs.archive
xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
link_result = linking_support.register_static_library_linking_action(ctx = ctx)
link_outputs_by_library_identifier = _group_link_outputs_by_library_identifier(
actions = actions,
apple_fragment = apple_fragment,
deps = deps,
label_name = bundle_name,
link_result = link_result,
xcode_config = xcode_config,
)
available_libraries = []
framework_archive_files = []
framework_archive_merge_files = []
for library_identifier, link_output in link_outputs_by_library_identifier.items():
# Bundle binary artifact for specific library identifier
binary_artifact = link_output.binary
framework_dir = paths.join(library_identifier, bundle_name + ".framework")
framework_archive_merge_files.append(struct(
src = binary_artifact.path,
dest = paths.join(framework_dir, bundle_name),
))
framework_archive_files.append(depset([binary_artifact]))
if link_output.uses_swift and link_output.swift_infos:
# Generate headers, modulemaps, and swiftmodules
interface_artifacts = partial.call(
partials.swift_framework_partial(
actions = actions,
avoid_deps = ctx.attr.avoid_deps,
bundle_name = bundle_name,
framework_modulemap = True,
label_name = label.name,
output_discriminator = library_identifier,
swift_infos = link_output.swift_infos,
),
)
else:
# Generate headers, and modulemaps
sdk_frameworks = cc_info_support.get_sdk_frameworks(
deps = ctx.split_attr.deps,
split_deps_keys = link_output.split_attr_keys,
)
sdk_dylibs = cc_info_support.get_sdk_dylibs(
deps = ctx.split_attr.deps,
split_deps_keys = link_output.split_attr_keys,
)
interface_artifacts = partial.call(partials.framework_header_modulemap_partial(
actions = actions,
bundle_name = bundle_name,
framework_modulemap = True,
hdrs = ctx.files.public_hdrs,
label_name = label.name,
output_discriminator = library_identifier,
umbrella_header = ctx.file.umbrella_header,
sdk_frameworks = sdk_frameworks,
sdk_dylibs = sdk_dylibs,
))
# Bundle headers & modulemaps (and swiftmodules if available)