-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathframework.bzl
1340 lines (1203 loc) · 53.9 KB
/
framework.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
"""Framework rules"""
load("//rules/framework:vfs_overlay.bzl", "VFSOverlayInfo", "make_vfsoverlay")
load("//rules:features.bzl", "feature_names")
load("//rules:library.bzl", "PrivateHeadersInfo", "apple_library")
load("//rules:plists.bzl", "process_infoplists")
load("//rules:providers.bzl", "AvoidDepsInfo", "FrameworkInfo")
load("//rules:transition_support.bzl", "transition_support")
load("//rules:utils.bzl", "is_bazel_7")
load("//rules/internal:objc_provider_utils.bzl", "objc_provider_utils")
load("@bazel_skylib//lib:partial.bzl", "partial")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
load("@build_bazel_rules_apple//apple/internal:apple_product_type.bzl", "apple_product_type")
load("@build_bazel_rules_apple//apple/internal:features_support.bzl", "features_support")
load("@build_bazel_rules_apple//apple/internal:linking_support.bzl", "linking_support")
load("@build_bazel_rules_apple//apple/internal:outputs.bzl", "outputs")
load("@build_bazel_rules_apple//apple/internal:partials.bzl", "partials")
load("@build_bazel_rules_apple//apple/internal:platform_support.bzl", "platform_support")
load("@build_bazel_rules_apple//apple/internal:processor.bzl", "processor")
load("@build_bazel_rules_apple//apple/internal:resource_actions.bzl", "resource_actions")
load("@build_bazel_rules_apple//apple/internal:resources.bzl", "resources")
load("@build_bazel_rules_apple//apple/internal:rule_support.bzl", "rule_support")
load("@build_bazel_rules_apple//apple/internal:apple_toolchains.bzl", "AppleMacToolsToolchainInfo", "AppleXPlatToolsToolchainInfo")
load("@build_bazel_rules_apple//apple/internal:swift_support.bzl", "swift_support")
load("@build_bazel_rules_apple//apple/internal/utils:clang_rt_dylibs.bzl", "clang_rt_dylibs")
load("@build_bazel_rules_apple//apple/internal:providers.bzl", "AppleBundleInfo", "ApplePlatformInfo", "IosFrameworkBundleInfo", "new_applebundleinfo", "new_iosframeworkbundleinfo")
load("@build_bazel_rules_swift//swift:swift.bzl", "SwiftInfo", "swift_clang_module_aspect", "swift_common")
load(
"@build_bazel_rules_apple//apple/internal/aspects:resource_aspect.bzl",
"apple_resource_aspect",
)
_APPLE_FRAMEWORK_PACKAGING_KWARGS = [
"visibility",
"features",
"frameworks",
"tags",
"bundle_id",
"skip_packaging",
"link_dynamic",
"exported_symbols_lists",
]
def apple_framework(
name,
apple_library = apple_library,
infoplists = [],
infoplists_by_build_setting = {},
xcconfig = {},
xcconfig_by_build_setting = {},
**kwargs):
"""Builds and packages an Apple framework.
Args:
name: The name of the framework.
apple_library: The macro used to package sources into a library.
infoplists: A list of Info.plist files to be merged into the framework.
infoplists_by_build_setting: A dictionary of infoplists grouped by bazel build setting.
Each value is applied if the respective bazel build setting
is resolved during the analysis phase.
If '//conditions:default' is not set the value in 'infoplists'
is set as default.
xcconfig: A dictionary of xcconfigs to be applied to the framework by default.
xcconfig_by_build_setting: A dictionary of xcconfigs grouped by bazel build setting.
Each value is applied if the respective bazel build setting
is resolved during the analysis phase.
If '//conditions:default' is not set the value in 'xcconfig'
is set as default.
**kwargs: Arguments passed to the apple_library and apple_framework_packaging rules as appropriate.
"""
framework_packaging_kwargs = {arg: kwargs.pop(arg) for arg in _APPLE_FRAMEWORK_PACKAGING_KWARGS if arg in kwargs}
kwargs["enable_framework_vfs"] = kwargs.pop("enable_framework_vfs", True)
merged_infoplists = None
if len(infoplists_by_build_setting.values()) > 0 or len(infoplists) > 0:
merged_infoplists = select(process_infoplists(
name = name,
infoplists = infoplists,
infoplists_by_build_setting = infoplists_by_build_setting,
xcconfig = xcconfig,
xcconfig_by_build_setting = xcconfig_by_build_setting,
))
environment_plist = kwargs.pop("environment_plist", select({
"@build_bazel_rules_ios//rules/apple_platform:ios": "@build_bazel_rules_apple//apple/internal:environment_plist_ios",
"@build_bazel_rules_ios//rules/apple_platform:macos": "@build_bazel_rules_apple//apple/internal:environment_plist_macos",
"@build_bazel_rules_ios//rules/apple_platform:tvos": "@build_bazel_rules_apple//apple/internal:environment_plist_tvos",
"@build_bazel_rules_ios//rules/apple_platform:watchos": "@build_bazel_rules_apple//apple/internal:environment_plist_watchos",
"//conditions:default": None,
}))
testonly = kwargs.pop("testonly", False)
library = apple_library(
name = name,
testonly = testonly,
xcconfig = xcconfig,
xcconfig_by_build_setting = xcconfig_by_build_setting,
**kwargs
)
framework_deps = []
platforms = library.platforms if library.platforms else {}
# At the time of writing this is still used in the output path
# computation
minimum_os_version = select({
"@build_bazel_rules_ios//rules/apple_platform:ios": platforms.get("ios", ""),
"@build_bazel_rules_ios//rules/apple_platform:macos": platforms.get("macos", ""),
"@build_bazel_rules_ios//rules/apple_platform:tvos": platforms.get("tvos", ""),
"@build_bazel_rules_ios//rules/apple_platform:watchos": platforms.get("watchos", ""),
"//conditions:default": "",
})
platform_type = select({
"@build_bazel_rules_ios//rules/apple_platform:ios": "ios",
"@build_bazel_rules_ios//rules/apple_platform:macos": "macos",
"@build_bazel_rules_ios//rules/apple_platform:tvos": "tvos",
"@build_bazel_rules_ios//rules/apple_platform:watchos": "watchos",
"//conditions:default": "",
})
framework_deps += library.lib_names
apple_framework_packaging(
name = name,
framework_name = library.namespace,
infoplists = merged_infoplists,
environment_plist = environment_plist,
transitive_deps = library.transitive_deps,
vfs = library.import_vfsoverlays,
deps = framework_deps,
platforms = platforms,
private_deps = kwargs.get("private_deps", []),
library_linkopts = library.linkopts,
testonly = testonly,
minimum_os_version = minimum_os_version,
platform_type = platform_type,
**framework_packaging_kwargs
)
def _validate_deps_minimum_os_version(framework_name, minimum_os_version, deps):
"""Validates that deps' minimum_os_versions are not higher than that of this target.
Note that Swift has already enforced that at compile time, while objective-c doesn't.
framework_name: the framework name
minimum_os_version: the minimum os version of this framework
deps: the deps of this framework
"""
# TODO @cshi we need a number of fixes to enable this
if True:
return
for dep in deps:
if AppleBundleInfo in dep:
dep_bundle_info = dep[AppleBundleInfo]
dep_min_os_version = apple_common.dotted_version(dep_bundle_info.minimum_os_version)
if minimum_os_version.compare_to(dep_min_os_version) < 0:
fail(
"""A framework target's minimum_os_version must NOT be lower than those of its deps,
but framework {} with minimum_os_version {} depends on framework {} with minimum_os_version {}.
""".format(framework_name, minimum_os_version, dep_bundle_info.bundle_name, dep_min_os_version),
)
def _find_framework_dir(outputs):
for output in outputs:
prefix = output.path.rsplit(".framework/", 1)[0]
return prefix + ".framework"
return None
def _framework_packaging_symlink_headers(ctx, inputs, outputs):
inputs_by_basename = {input.basename: input for input in inputs}
# If this check is true it means that multiple inputs have the same 'basename',
# an additional check is done to see if that was caused by 'action_inputs' containing
# two different paths to the same file
#
# In that case fails with a msg listing the differences found
if len(inputs_by_basename) != len(inputs):
inputs_by_basename_paths = [x.path for x in inputs_by_basename.values()]
inputs_with_duplicated_basename = [x for x in inputs if not x.path in inputs_by_basename_paths]
if len(inputs_with_duplicated_basename) > 0:
fail("""
[Error] Multiple files with the same name exists.\n
See below for the list of paths found for each basename:\n
{}
""".format({x.basename: (x.path, inputs_by_basename[x.basename].path) for x in inputs_with_duplicated_basename}))
# If no error occurs create symlinks for each output with
# each input as 'target_file'
output_input_dict = {output: inputs_by_basename[output.basename] for output in outputs}
for (output, input) in output_input_dict.items():
ctx.actions.symlink(output = output, target_file = input)
def _framework_packaging_single(ctx, action, inputs, output, manifest = None):
outputs = _framework_packaging_multi(ctx, action, inputs, [output], manifest = manifest)
return outputs[0] if outputs else None
def _framework_packaging_multi(ctx, action, inputs, outputs, manifest = None):
if not inputs:
return []
if inputs == [None]:
return []
virtualize_frameworks = feature_names.virtualize_frameworks in ctx.features
if virtualize_frameworks:
return inputs
if action in ctx.attr.skip_packaging:
return []
action_inputs = [manifest] + inputs if manifest else inputs
outputs = [ctx.actions.declare_file(f) for f in outputs]
framework_name = ctx.attr.framework_name
framework_dir = _find_framework_dir(outputs)
args = ctx.actions.args().use_param_file("@%s").set_param_file_format("multiline")
args.add("--framework_name", framework_name)
args.add("--framework_root", framework_dir)
args.add("--action", action)
args.add_all("--inputs", inputs)
args.add_all("--outputs", outputs)
if action in ["header", "private_header"]:
_framework_packaging_symlink_headers(ctx, inputs, outputs)
else:
ctx.actions.run(
executable = ctx.executable._framework_packaging,
arguments = [args],
inputs = action_inputs,
outputs = outputs,
mnemonic = "PackagingFramework%s" % action.title().replace("_", ""),
)
return outputs
def _compact(args):
return [item for item in args if item]
def _get_virtual_framework_info(ctx, framework_files, compilation_context_fields, deps, transitive_deps, vfs):
import_vfsoverlays = [
dep[VFSOverlayInfo].vfs_info
for dep in vfs
if VFSOverlayInfo in dep
]
# Propagated interface headers - this must encompass all of them
propagated_interface_headers = []
# We need to map all the deps here - for both swift headers and others
fw_dep_vfsoverlays = []
# Whether or not to collect SwiftInfo providers from rules_swift_package_manager targets
enable_rules_swift_package_manager = feature_names.experimental_rules_swift_package_manager in ctx.features
for dep in transitive_deps + deps:
if enable_rules_swift_package_manager and SwiftInfo in dep:
if dep.label.workspace_name.startswith("swiftpkg"):
for spm_dep in dep[SwiftInfo].transitive_modules.to_list():
if spm_dep.swift:
spm_dep_vfs = make_vfsoverlay(
ctx,
hdrs = [],
module_map = [],
swiftmodules = [spm_dep.swift.swiftmodule, spm_dep.swift.swiftdoc],
private_hdrs = [],
has_swift = True,
merge_vfsoverlays = [],
framework_name = spm_dep.name,
)
fw_dep_vfsoverlays.append(spm_dep_vfs.vfs_info)
# Collect transitive headers. For now, this needs to include all of the
# transitive headers
if CcInfo in dep:
compilation_context = dep[CcInfo].compilation_context
propagated_interface_headers.append(compilation_context.headers)
if FrameworkInfo in dep:
framework_info = dep[FrameworkInfo]
fw_dep_vfsoverlays.extend(framework_info.vfsoverlay_infos)
framework_headers = depset(framework_info.headers + framework_info.modulemap + framework_info.private_headers)
propagated_interface_headers.append(framework_headers)
outputs = framework_files.outputs
compile_with_xcode = feature_names.compile_with_xcode in ctx.features
vfs = make_vfsoverlay(
ctx,
hdrs = outputs.headers,
module_map = outputs.modulemaps,
# We might need to pass in .swiftinterface files here as well
# esp. if the error is `swift declaration not found` for some module
swiftmodules = _compact([outputs.swiftmodule, outputs.swiftdoc]),
private_hdrs = outputs.private_headers,
has_swift = True if outputs.swiftmodule else False,
merge_vfsoverlays = [] if compile_with_xcode else (fw_dep_vfsoverlays + import_vfsoverlays),
)
# Includes interface headers here ( handled in cc_info merge for no virtual )
compilation_context_fields["headers"] = depset(
direct = outputs.headers + outputs.private_headers + outputs.modulemaps,
transitive = propagated_interface_headers,
)
return FrameworkInfo(
vfsoverlay_infos = [vfs.vfs_info],
headers = outputs.headers,
private_headers = outputs.private_headers,
modulemap = outputs.modulemaps,
swiftmodule = outputs.swiftmodule,
swiftdoc = outputs.swiftdoc,
)
def _get_framework_files(ctx, deps):
framework_name = ctx.attr.framework_name
bundle_extension = ctx.attr.bundle_extension
# declare framework directory
framework_dir = "%s/%s.%s" % (ctx.attr.name, framework_name, bundle_extension)
# binaries
binaries_in = []
# headers
headers_in = []
headers_out = []
private_headers_in = []
private_headers_out = []
# modulemap
modulemap_in = None
# current build architecture
arch = ctx.fragments.apple.single_arch_cpu
# swift specific artifacts
swiftmodule_in = None
swiftmodule_out = None
swiftinterface_out = None
swiftinterface_in = None
swiftdoc_in = None
swiftdoc_out = None
infoplist_in = None
infoplist_out = None
symbol_graph_in = None
symbol_graph_out = None
# collect files
for dep in deps:
files = dep.files.to_list()
for file in files:
if file.is_source:
continue
# collect binary files
if file.path.endswith(".a"):
binaries_in.append(file)
# collect swift specific files
if file.path.endswith(".swiftmodule"):
swiftmodule_in = file
swiftmodule_out = paths.join(
framework_dir,
"Modules",
framework_name + ".swiftmodule",
arch + ".swiftmodule",
)
if file.path.endswith(".swiftinterface"):
swiftinterface_in = file
swiftinterface_out = paths.join(
framework_dir,
"Modules",
framework_name + ".swiftinterface",
arch + ".swiftinterface",
)
if file.path.endswith(".swiftdoc"):
swiftdoc_in = file
swiftdoc_out = paths.join(
framework_dir,
"Modules",
framework_name + ".swiftmodule",
arch + ".swiftdoc",
)
if PrivateHeadersInfo in dep:
for hdr in dep[PrivateHeadersInfo].headers.to_list():
private_headers_in.append(hdr)
destination = paths.join(framework_dir, "PrivateHeaders", hdr.basename)
private_headers_out.append(destination)
has_header = False
for provider in [CcInfo, apple_common.Objc]:
if provider in dep:
for hdr in _get_direct_public_headers(provider, dep):
if not hdr.is_directory and hdr.path.endswith((".h", ".hh", ".hpp")):
has_header = True
headers_in.append(hdr)
destination = paths.join(framework_dir, "Headers", hdr.basename)
headers_out.append(destination)
elif hdr.path.endswith(".modulemap"):
modulemap_in = hdr
# If theres a symbol graph, we need to copy it over
if dep.output_groups and hasattr(dep.output_groups, "swift_symbol_graph"):
symbol_graph_in = dep.output_groups.swift_symbol_graph.to_list()[0]
symbol_graph_out = paths.join(framework_dir, framework_name + ".symbolgraph")
if not has_header:
# only thing is the generated module map -- we don't want it
continue
if SwiftInfo in dep and dep[SwiftInfo].direct_modules:
# apple_common.Objc.direct_module_maps is broken coming from swift_library
# (it contains one level of transitive module maps), so ignore SwiftInfo from swift_library,
# since it doesn't have a module_map field anyway
continue
# collect modulemaps
for modulemap in dep[apple_common.Objc].direct_module_maps:
if modulemap.owner == dep.label:
# module map is generated by the objc_library, and does not come
# from the attr
continue
modulemap_in = modulemap
binary_out = paths.join(framework_dir, framework_name) if binaries_in else None
modulemap_out = paths.join(framework_dir, "Modules", "module.modulemap") if modulemap_in else None
virtualize_frameworks = feature_names.virtualize_frameworks in ctx.features
if not virtualize_frameworks:
framework_manifest = ctx.actions.declare_file(framework_dir + ".manifest")
if not ctx.attr.link_dynamic:
infoplist_in = _merge_root_infoplists(ctx)
infoplist_out = paths.join(
framework_dir,
"Info.plist",
)
else:
framework_manifest = None
# Package each part of the framework separately,
# so inputs that do not depend on compilation
# are available before those that do,
# improving parallelism
binary_out = _framework_packaging_single(ctx, "binary", binaries_in, binary_out, framework_manifest)
headers_out = _framework_packaging_multi(ctx, "header", headers_in, headers_out, framework_manifest)
private_headers_out = _framework_packaging_multi(ctx, "private_header", private_headers_in, private_headers_out, framework_manifest)
# Instead of creating a symlink of the modulemap, we need to copy it to modulemap_out.
# It's a hacky fix to guarantee running the clean action before compiling objc files depending on this framework in non-sandboxed mode.
# Otherwise, stale header files under framework_root will cause compilation failure in non-sandboxed mode.
modulemap_out = _framework_packaging_single(ctx, "modulemap", [modulemap_in], modulemap_out, framework_manifest)
swiftmodule_out = _framework_packaging_single(ctx, "swiftmodule", [swiftmodule_in], swiftmodule_out, framework_manifest)
swiftinterface_out = _framework_packaging_single(ctx, "swiftinterface", [swiftinterface_in], swiftinterface_out, framework_manifest)
swiftdoc_out = _framework_packaging_single(ctx, "swiftdoc", [swiftdoc_in], swiftdoc_out, framework_manifest)
infoplist_out = _framework_packaging_single(ctx, "infoplist", [infoplist_in], infoplist_out, framework_manifest)
symbol_graph_out = _framework_packaging_single(ctx, "symbol_graph", [symbol_graph_in], symbol_graph_out, framework_manifest)
outputs = struct(
binary = binary_out,
headers = headers_out,
infoplist = infoplist_out,
private_headers = private_headers_out,
modulemaps = [modulemap_out] if modulemap_out else [],
swiftmodule = swiftmodule_out,
swiftdoc = swiftdoc_out,
swiftinterface = swiftinterface_out,
manifest = framework_manifest,
symbol_graph = symbol_graph_out,
)
inputs = struct(
binaries = binaries_in,
headers = headers_in,
private_headers = private_headers_in,
modulemaps = [modulemap_in] if modulemap_in else [],
swiftmodule = swiftmodule_in,
swiftdoc = swiftdoc_in,
swiftinterface = swiftinterface_in,
symbol_graph = symbol_graph_in,
)
return struct(inputs = inputs, outputs = outputs)
def _get_direct_public_headers(provider, dep):
if provider == CcInfo:
if PrivateHeadersInfo in dep:
return []
return dep[provider].compilation_context.direct_public_headers
elif provider == apple_common.Objc:
return getattr(dep[provider], "direct_headers", [])
else:
fail("Unknown provider " + provider + " only CcInfo and Objc supported")
def _get_symlinked_framework_clean_action(ctx, framework_files, compilation_context_fields):
framework_name = ctx.attr.framework_name
outputs = framework_files.outputs
framework_manifest = outputs.manifest
framework_contents = _compact(
[
outputs.binary,
outputs.swiftmodule,
outputs.swiftdoc,
] +
outputs.modulemaps +
outputs.headers +
outputs.private_headers,
)
framework_root = _find_framework_dir(framework_contents)
if framework_root:
ctx.actions.run(
executable = ctx.executable._framework_packaging,
arguments = [
"--action",
"clean",
"--framework_name",
framework_name,
"--framework_root",
framework_root,
"--inputs",
ctx.actions.args().use_param_file("%s", use_always = True).set_param_file_format("multiline")
.add_all(framework_contents),
"--outputs",
framework_manifest.path,
],
outputs = [framework_manifest],
mnemonic = "CleaningFramework",
execution_requirements = {
"local": "True",
},
)
compilation_context_fields["framework_includes"] = depset(
direct = [paths.dirname(framework_root)],
)
else:
ctx.actions.write(framework_manifest, "# Empty framework\n")
def _get_cc_info_linker_inputs(*, deps):
linker_inputs = []
for dep in deps:
if not CcInfo in dep:
continue
for linker_input in dep[CcInfo].linking_context.linker_inputs.to_list():
linker_inputs.append(linker_input)
return depset(linker_inputs)
def _create_swiftmodule(attrs):
kwargs = {}
# The upstream code will try collect it - it will be None when non existent
if attrs.symbol_graph:
kwargs["symbol_graph"] = attrs.symbol_graph
return swift_common.create_swift_module(
swiftdoc = attrs.swiftdoc,
swiftmodule = attrs.swiftmodule,
swiftinterface = attrs.swiftinterface,
**kwargs
)
def _copy_swiftmodule(ctx, framework_files, clang_module):
inputs = framework_files.inputs
outputs = framework_files.outputs
# only add a swift module to the SwiftInfo if we've actually got a swiftmodule
swiftmodule_name = paths.split_extension(inputs.swiftmodule.basename)[0]
# need to include the swiftmodule here, even though it will be found through the framework search path,
# since swift_library needs to know that the swiftdoc is an input to the compile action
swift_module = _create_swiftmodule(outputs)
if swiftmodule_name != ctx.attr.framework_name:
# Swift won't find swiftmodule files inside of frameworks whose name doesn't match the
# module name. It's annoying (since clang finds them just fine), but we have no choice but to point to the
# original swift module/doc, so that swift can find it.
swift_module = _create_swiftmodule(inputs)
return [
# only add the swift module, the objc modulemap is already listed as a header,
# and it will be discovered via the framework search path
swift_common.create_module(
name = swiftmodule_name,
clang = clang_module,
swift = swift_module,
),
]
def _get_merged_swift_info(ctx, framework_files, transitive_deps, clang_module):
swift_info_fields = {
"swift_infos": [dep[SwiftInfo] for dep in transitive_deps if SwiftInfo in dep],
}
if framework_files.outputs.swiftmodule:
swift_info_fields["modules"] = _copy_swiftmodule(ctx, framework_files, clang_module)
else:
swift_info_fields["modules"] = [
swift_common.create_module(
name = ctx.attr.framework_name,
clang = clang_module,
),
]
return swift_common.create_swift_info(**swift_info_fields)
def _merge_root_infoplists(ctx):
if ctx.attr.infoplists == None or len(ctx.attr.infoplists) == 0:
return None
output_plist = ctx.actions.declare_file(
paths.join("%s-intermediates" % ctx.label.name, "Info.plist"),
)
bundle_name = ctx.attr.framework_name
current_apple_platform = transition_support.current_apple_platform(apple_fragment = ctx.fragments.apple, xcode_config = ctx.attr._xcode_config)
platform_type = str(current_apple_platform.platform.platform_type)
apple_mac_toolchain_info = ctx.attr._mac_toolchain[AppleMacToolsToolchainInfo]
if hasattr(rule_support, "rule_descriptor_no_ctx"):
descriptor_fn = rule_support.rule_descriptor_no_ctx
else:
descriptor_fn = rule_support.rule_descriptor
rule_descriptor = descriptor_fn(
platform_type = platform_type,
product_type = apple_product_type.static_framework,
)
features = features_support.compute_enabled_features(
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
resource_actions.merge_root_infoplists(
actions = ctx.actions,
bundle_name = bundle_name,
bundle_id = ctx.attr.bundle_id,
bundle_extension = ctx.attr.bundle_extension,
environment_plist = ctx.file.environment_plist,
input_plists = ctx.files.infoplists,
launch_storyboard = None,
output_plist = output_plist,
output_pkginfo = None,
output_discriminator = "framework",
platform_prerequisites = _platform_prerequisites(ctx, rule_descriptor, platform_type, features),
plisttool = apple_mac_toolchain_info.plisttool,
rule_descriptor = rule_descriptor,
rule_label = ctx.label,
version = None,
)
return output_plist
def _attrs_for_split_slice(attrs_by_split_slices, split_slice_key):
if len(attrs_by_split_slices.keys()) == 0:
return []
elif len(attrs_by_split_slices.keys()) == 1:
return attrs_by_split_slices.values()[0]
else:
return attrs_by_split_slices[split_slice_key]
def _platform_prerequisites(ctx, rule_descriptor, platform_type, features):
# Consider plumbing this in
deps = getattr(ctx.attr, "deps", None)
uses_swift = swift_support.uses_swift(deps) if deps else False
apple_xplat_toolchain_info = ctx.attr._xplat_toolchain[AppleXPlatToolsToolchainInfo]
version_args = {
"build_settings": apple_xplat_toolchain_info.build_settings,
}
return platform_support.platform_prerequisites(
apple_fragment = ctx.fragments.apple,
config_vars = ctx.var,
cpp_fragment = ctx.fragments.cpp,
device_families = rule_descriptor.allowed_device_families,
explicit_minimum_deployment_os = ctx.attr.minimum_deployment_os_version,
explicit_minimum_os = ctx.attr.minimum_os_version,
features = features,
objc_fragment = ctx.fragments.objc,
platform_type_string = platform_type,
uses_swift = uses_swift,
xcode_version_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig],
**version_args
)
def _bundle_dynamic_framework(ctx, is_extension_safe, avoid_deps):
"""Packages this as dynamic framework
Currently, this doesn't include headers or other interface files.
"""
actions = ctx.actions
apple_mac_toolchain_info = ctx.attr._mac_toolchain[AppleMacToolsToolchainInfo]
apple_xplat_toolchain_info = ctx.attr._xplat_toolchain[AppleXPlatToolsToolchainInfo]
bin_root_path = ctx.bin_dir.path
bundle_id = ctx.attr.bundle_id
if not bundle_id:
# This is generally not expected behavior - if they don't want a
# processed infoplit its possible, but validate for the common case
fail("Missing bundle_id: Info.plist actions require one")
bundle_name = ctx.attr.framework_name
bundle_extension = ".framework"
features = features_support.compute_enabled_features(
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
label = ctx.label
# This file is used as part of the rules_apple bundling logic
archive = actions.declare_file(ctx.attr.name + ".framework.zip")
predeclared_outputs = struct(archive = archive)
provisioning_profile = None
resource_deps = ctx.attr.deps + ctx.attr.transitive_deps + ctx.attr.data
current_apple_platform = transition_support.current_apple_platform(apple_fragment = ctx.fragments.apple, xcode_config = ctx.attr._xcode_config)
platform_type = str(current_apple_platform.platform.platform_type)
if hasattr(rule_support, "rule_descriptor_no_ctx"):
descriptor_fn = rule_support.rule_descriptor_no_ctx
else:
descriptor_fn = rule_support.rule_descriptor
rule_descriptor = descriptor_fn(
platform_type = platform_type,
product_type = apple_product_type.framework,
)
platform_prerequisites = _platform_prerequisites(ctx, rule_descriptor, platform_type, features)
signed_frameworks = []
if provisioning_profile:
signed_frameworks = [
bundle_name + bundle_extension,
]
top_level_resources = resources.collect(
attr = ctx.attr,
res_attrs = ["data"],
)
extra_linkopts = [
"-dynamiclib",
"-Wl,-install_name,@rpath/{name}{extension}/{name}".format(
extension = bundle_extension,
name = bundle_name,
),
]
if is_extension_safe:
extra_linkopts.append("-fapplication-extension")
top_level_infoplists = resources.collect(
attr = ctx.attr,
res_attrs = ["infoplists"],
)
link_result = linking_support.register_binary_linking_action(
ctx,
avoid_deps = avoid_deps,
entitlements = None,
exported_symbols_lists = ctx.files.exported_symbols_lists,
extra_linkopts = extra_linkopts,
platform_prerequisites = platform_prerequisites,
stamp = ctx.attr.stamp,
)
binary_artifact = link_result.binary
debug_outputs = linking_support.debug_outputs_by_architecture(link_result.outputs)
archive_for_embedding = outputs.archive_for_embedding(
actions = actions,
bundle_name = bundle_name,
bundle_extension = bundle_extension,
label_name = label.name,
rule_descriptor = rule_descriptor,
platform_prerequisites = platform_prerequisites,
predeclared_outputs = predeclared_outputs,
)
dep_frameworks = ctx.attr.frameworks
# TODO(jmarino) - consider how to better handle frameworks of frameworks
processor_partials = []
processor_partials.append(
partials.apple_bundle_info_partial(
actions = actions,
bundle_extension = bundle_extension,
bundle_id = bundle_id,
bundle_name = bundle_name,
executable_name = bundle_name,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
predeclared_outputs = predeclared_outputs,
product_type = apple_product_type.framework,
rule_descriptor = rule_descriptor,
),
)
processor_partials.append(
partials.binary_partial(
executable_name = bundle_name,
actions = actions,
binary_artifact = binary_artifact,
bundle_name = bundle_name,
label_name = label.name,
),
)
processor_partials.append(
partials.codesigning_dossier_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
bundle_extension = bundle_extension,
bundle_location = processor.location.framework,
bundle_name = bundle_name,
embed_target_dossiers = False,
embedded_targets = dep_frameworks,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
predeclared_outputs = predeclared_outputs,
provisioning_profile = provisioning_profile,
rule_descriptor = rule_descriptor,
),
)
processor_partials.append(
partials.clang_rt_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
)
processor_partials.append(
partials.debug_symbols_partial(
actions = actions,
bundle_extension = bundle_extension,
bundle_name = bundle_name,
debug_dependencies = dep_frameworks,
dsym_binaries = debug_outputs.dsym_binaries,
label_name = label.name,
linkmaps = debug_outputs.linkmaps,
dsym_info_plist_template = apple_mac_toolchain_info.dsym_info_plist_template,
platform_prerequisites = platform_prerequisites,
plisttool = apple_mac_toolchain_info.plisttool,
rule_label = label,
version = None,
),
)
processor_partials.append(
partials.embedded_bundles_partial(
frameworks = [archive_for_embedding],
embeddable_targets = dep_frameworks,
platform_prerequisites = platform_prerequisites,
signed_frameworks = depset(signed_frameworks),
),
)
processor_partials.append(
partials.extension_safe_validation_partial(
is_extension_safe = is_extension_safe,
rule_label = label,
targets_to_validate = dep_frameworks,
),
)
cc_toolchain = find_cpp_toolchain(ctx)
cc_features = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
language = "objc",
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
processor_partials.append(
partials.framework_provider_partial(
actions = actions,
bin_root_path = bin_root_path,
binary_artifact = binary_artifact,
bundle_name = bundle_name,
bundle_only = False,
cc_features = cc_features,
cc_info = link_result.cc_info,
cc_toolchain = cc_toolchain,
rule_label = label,
),
)
processor_partials.append(
partials.resources_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
bundle_extension = bundle_extension,
bundle_id = bundle_id,
bundle_name = bundle_name,
environment_plist = ctx.file.environment_plist,
executable_name = bundle_name,
launch_storyboard = None,
platform_prerequisites = platform_prerequisites,
resource_deps = resource_deps,
rule_descriptor = rule_descriptor,
rule_label = label,
targets_to_avoid = avoid_deps,
top_level_infoplists = top_level_infoplists,
top_level_resources = top_level_resources,
version = None,
version_keys_required = False,
),
)
processor_partials.append(
partials.swift_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
dependency_targets = dep_frameworks,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
),
)
processor_partials.append(
partials.apple_symbols_file_partial(
actions = actions,
binary_artifact = binary_artifact,
dsym_binaries = debug_outputs.dsym_binaries,
dependency_targets = dep_frameworks,
label_name = label.name,
include_symbols_in_bundle = False,
platform_prerequisites = platform_prerequisites,
),
)
processor_result = processor.process(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
bundle_extension = bundle_extension,
bundle_name = bundle_name,
codesign_inputs = [],
codesignopts = [],
features = features,
ipa_post_processor = None,
partials = processor_partials,
platform_prerequisites = platform_prerequisites,
predeclared_outputs = predeclared_outputs,
process_and_sign_template = apple_mac_toolchain_info.process_and_sign_template,
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
provisioning_profile = provisioning_profile,
rule_descriptor = rule_descriptor,
rule_label = label,
)
return struct(
files = processor_result.output_files,
providers = [
new_iosframeworkbundleinfo(),
OutputGroupInfo(
**outputs.merge_output_groups(
link_result.output_groups,
processor_result.output_groups,
)
),
] + processor_result.providers,
)
def _bundle_static_framework(ctx, is_extension_safe, current_apple_platform, outputs):
"""Returns bundle info for a static framework commonly used intra-build"""
partial_output = partial.call(
partials.extension_safe_validation_partial(
is_extension_safe = is_extension_safe,
rule_label = ctx.label,
targets_to_validate = ctx.attr.frameworks,
),
)
# Static packaging - archives are passed from library deps
return struct(files = depset([]), providers = [
new_applebundleinfo(
archive = None,
archive_root = None,
binary = outputs.binary,
bundle_id = ctx.attr.bundle_id,
bundle_name = ctx.attr.framework_name,
bundle_extension = ctx.attr.bundle_extension,
entitlements = None,
infoplist = outputs.infoplist,
minimum_os_version = str(current_apple_platform.target_os_version),
minimum_deployment_os_version = ctx.attr.minimum_deployment_os_version,
platform_type = str(current_apple_platform.platform.platform_type),
product_type = apple_product_type.static_framework,
uses_swift = outputs.swiftmodule != None,
),
] + partial_output.providers)
def _apple_framework_packaging_impl(ctx):
# The current build architecture
arch = ctx.fragments.apple.single_arch_cpu
# The current Apple platform type, such as iOS, macOS, tvOS, or watchOS