-
Notifications
You must be signed in to change notification settings - Fork 32
/
swiftpkg_build_files.bzl
1036 lines (915 loc) · 36.5 KB
/
swiftpkg_build_files.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
"""Module for creating Bazel declarations to build a Swift package."""
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@cgrindel_bazel_starlib//bzllib:defs.bzl", "bazel_labels", "lists")
load(":artifact_infos.bzl", "artifact_types", "link_types")
load(":bazel_apple_platforms.bzl", "bazel_apple_platforms")
load(":build_decls.bzl", "build_decls")
load(":build_files.bzl", "build_files")
load(":bzl_selects.bzl", "bzl_selects")
load(":load_statements.bzl", "load_statements")
load(":pkginfo_target_deps.bzl", "pkginfo_target_deps")
load(":pkginfo_targets.bzl", "pkginfo_targets")
load(":pkginfos.bzl", "build_setting_kinds", "module_types", "pkginfos", "target_types")
load(":starlark_codegen.bzl", scg = "starlark_codegen")
# MARK: - Target Entry Point
def _new_for_target(repository_ctx, pkg_ctx, target, artifact_infos = []):
if target.module_type == module_types.clang:
return _clang_target_build_file(repository_ctx, pkg_ctx, target)
elif target.module_type == module_types.swift:
return _swift_target_build_file(pkg_ctx, target)
elif target.module_type == module_types.system_library:
return _system_library_build_file(target)
elif target.module_type == module_types.binary:
# GH558: Support artifactBundle.
xcf_artifact_info = lists.find(
artifact_infos,
lambda ai: ai.artifiact_type == artifact_types.xcframework,
)
if xcf_artifact_info != None:
return _xcframework_import_build_file(target, xcf_artifact_info)
# GH046: Support plugins.
return None
# MARK: - Swift Target
def _swift_target_build_file(pkg_ctx, target):
if target.swift_src_info == None:
fail("Expected a `swift_src_info`. name: ", target.name)
all_build_files = []
attrs = {
"module_name": target.c99name,
"srcs": pkginfo_targets.srcs(target),
"visibility": ["//:__subpackages__"],
}
def _update_attr_list(name, value):
# We need to create a new list, because the retrieved list could be
# frozen.
attr_list = list(attrs.get(name, []))
attr_list.append(value)
attrs[name] = attr_list
# Naively parse the tools semver.
tools_version = pkg_ctx.pkg_info.tools_version or "0.0.0"
tools_version_components = tools_version.split(".") + ["0", "0"]
tools_version_major, tools_version_minor = [int(x if x.isdigit() else "0") for x in tools_version_components[0:2]]
# Gate package_name behind swift tools version 5.9
if tools_version_major >= 6 or (tools_version_major == 5 and tools_version_minor >= 9):
attrs["package_name"] = pkg_ctx.pkg_info.name
target_deps = []
macro_targets = []
for target_dep in target.dependencies:
dep_target_name = None
if target_dep.target:
dep_target_name = target_dep.target.target_name
elif target_dep.by_name:
dep_target_name = target_dep.by_name.name
if not dep_target_name:
target_deps.append(target_dep)
continue
dep_target = pkginfo_targets.get(
targets = pkg_ctx.pkg_info.targets,
name = dep_target_name,
fail_if_not_found = False,
)
if not dep_target or dep_target.type != target_types.macro:
target_deps.append(target_dep)
continue
macro_targets.append(dep_target)
if macro_targets:
attrs["plugins"] = [
# The targets will be local to this repo. We do not want the '@'
# prefix that can be added during normalization.
bazel_labels.normalize(t.label).removeprefix("@")
for t in macro_targets
]
deps = []
if target_deps:
deps = lists.flatten([
pkginfo_target_deps.bzl_select_list(pkg_ctx, td)
for td in target_deps
])
attrs["deps"] = bzl_selects.to_starlark(deps)
# NOTE: We specify defines using copts so that they stay local to the
# target. Specifying them using the defines attribute will propagate them.
copts = [
# SPM directive instructing the code to build as if a Swift package.
# https://github.com/apple/swift-package-manager/blob/main/Documentation/Usage.md#packaging-legacy-code
"-DSWIFT_PACKAGE",
]
# GH046: Support plugins.
is_library_target = lists.contains([target_types.library, target_types.regular], target.type)
# Check if any of the sources indicate that the module will be used by
# Objective-C code. If so, generate the bridge header file.
if target.swift_src_info.has_objc_directive and is_library_target:
attrs["generates_header"] = True
if target.swift_settings != None:
if len(target.swift_settings.defines) > 0:
copts.extend(lists.flatten([
bzl_selects.new_from_build_setting(
bs,
values_map_fn = lambda v: "-D" + v,
)
for bs in target.swift_settings.defines
]))
if len(target.swift_settings.unsafe_flags) > 0:
copts.extend(lists.flatten([
bzl_selects.new_from_build_setting(bs)
for bs in target.swift_settings.unsafe_flags
]))
if len(target.swift_settings.experimental_features) > 0:
for bs in target.swift_settings.experimental_features:
copts.append("-enable-experimental-feature")
copts.extend(lists.flatten(bzl_selects.new_from_build_setting(bs)))
if len(copts) > 0:
attrs["copts"] = bzl_selects.to_starlark(copts, mutually_inclusive = True)
if target.resources:
swift_apple_res_bundle_info = _apple_resource_bundle_for_swift(
pkg_ctx,
target,
)
all_build_files.append(swift_apple_res_bundle_info.build_file)
_update_attr_list("data", ":{}".format(
swift_apple_res_bundle_info.bundle_label_name,
))
_update_attr_list("srcs", ":{}".format(
swift_apple_res_bundle_info.accessor_label_name,
))
if is_library_target:
load_stmts = [swift_library_load_stmt]
decls = [_swift_library_from_target(target, attrs)]
elif target.type == target_types.executable:
load_stmts = [swift_binary_load_stmt]
decls = [_swift_binary_from_target(target, attrs)]
elif target.type == target_types.test:
load_stmts = [swift_test_load_stmt]
decls = [_swift_test_from_target(target, attrs)]
elif target.type == target_types.macro:
load_stmts = [swift_compiler_plugin_load_stmt]
decls = [_swift_compiler_plugin_from_target(target, attrs)]
else:
fail("Unrecognized target type for a Swift target. type:", target.type)
all_build_files.append(build_files.new(
load_stmts = load_stmts,
decls = decls,
))
# Generate a modulemap for the Swift module.
if attrs.get("generates_header", False):
all_build_files.append(_generate_modulemap_for_swift_target(target, deps))
return build_files.merge(*all_build_files)
def _swift_library_from_target(target, attrs):
# Mark swift_library targets as manual. We do this so that they are always
# built from a leaf node which can provide critical configuration
# information.
attrs["tags"] = ["manual"]
# SPM always includes the developer search paths when compiling Swift
# library targets. So, we do too.
attrs["always_include_developer_search_paths"] = True
return build_decls.new(
kind = swift_kinds.library,
name = pkginfo_targets.bazel_label_name(target),
attrs = attrs,
)
def _swift_binary_from_target(target, attrs):
return build_decls.new(
kind = swift_kinds.binary,
name = pkginfo_targets.bazel_label_name(target),
attrs = attrs,
)
def _swift_test_from_target(target, attrs):
return build_decls.new(
kind = swift_kinds.test,
name = pkginfo_targets.bazel_label_name(target),
attrs = attrs,
)
def _swift_compiler_plugin_from_target(target, attrs):
# Macros are set up as compiler plugins. We expose macro products as an
# alias to the swift_compiler_plugin target.
attrs["visibility"] = ["//visibility:public"]
return build_decls.new(
kind = swift_kinds.compiler_plugin,
name = pkginfo_targets.bazel_label_name(target),
attrs = attrs,
)
# MARK: - Clang Targets
def _clang_target_build_file(repository_ctx, pkg_ctx, target):
all_build_files = []
clang_src_info = target.clang_src_info
if clang_src_info == None:
fail("Expected `clang_src_info` to not be None.")
deps = lists.flatten([
pkginfo_target_deps.bzl_select_list(pkg_ctx, td)
for td in target.dependencies
])
attrs = {
# These flags are used by SPM when compiling clang modules.
"copts": [
# Enable 'blocks' language feature
"-fblocks",
# Synthesize retain and release calls for Objective-C pointers
"-fobjc-arc",
# Enable support for PIC macros
"-fPIC",
# Module name
"-fmodule-name={}".format(target.c99name),
],
"visibility": ["//:__subpackages__"],
}
def _set_if_not_empty(attr, list, transform_fn = None):
if len(list) > 0:
attrs[attr] = transform_fn(list) if transform_fn else list
def _update_attr_list(name, value):
# We need to create a new list, because the retrieved list could be
# frozen.
attr_list = list(attrs.get(name, []))
attr_list.append(value)
attrs[name] = attr_list
_set_if_not_empty("deps", deps, bzl_selects.to_starlark)
_set_if_not_empty("hdrs", clang_src_info.hdrs)
_set_if_not_empty("srcs", clang_src_info.srcs)
_set_if_not_empty("includes", clang_src_info.public_includes)
_set_if_not_empty("textual_hdrs", clang_src_info.textual_hdrs)
if target.resources:
clang_apple_res_bundle_info = _apple_resource_bundle_for_clang(
pkg_ctx,
target,
)
all_build_files.append(clang_apple_res_bundle_info.build_file)
_update_attr_list("data", ":{}".format(
clang_apple_res_bundle_info.bundle_label_name,
))
if clang_apple_res_bundle_info.objc_accessor_hdr_label_name:
# SPM provides a SWIFTPM_MODULE_BUNDLE macro to access the bundle for
# ObjC code. The header file contains the macro definition. It needs
# to be available in every Objc source file. So, we specify the
# -include flag specifying the header path.
# https://github.com/apple/swift-package-manager/blob/8387798811c6cc43761c5e1b48df2d3412dc5de4/Sources/Build/BuildDescription/ClangTargetBuildDescription.swift#L390
_update_attr_list("srcs", ":{}".format(
clang_apple_res_bundle_info.objc_accessor_hdr_label_name,
))
_update_attr_list("copts", "-include$(location :{})".format(
clang_apple_res_bundle_info.objc_accessor_hdr_label_name,
))
_update_attr_list("srcs", ":{}".format(
clang_apple_res_bundle_info.objc_accessor_impl_label_name,
))
# The copts may be updated by functions that were executed before this
# point. Use whatever has been set.
copts = attrs.get("copts", [])
# The SWIFT_PACKAGE define is a magical value that SPM uses when it
# builds clang libraries that will be used as Swift modules.
copts.append("-DSWIFT_PACKAGE=1")
local_includes = [
bzl_selects.new(value = p, kind = _condition_kinds.private_includes)
for p in clang_src_info.private_includes
]
def _normalize_and_create_copt_define(value):
normalized = scg.normalize_define_value(value)
return "-D" + normalized
all_settings = lists.compact([target.clang_settings, target.cxx_settings])
for settings in all_settings:
copts.extend(lists.flatten([
bzl_selects.new_from_build_setting(
bs,
# Define values can contain spaces. Bazel requires that they
# are already escaped.
values_map_fn = _normalize_and_create_copt_define,
)
for bs in settings.defines
]))
# Need to convert the headerSearchPaths to be relative to the
# target path. We also do not want to lose any conditions that may
# be attached.
hsp_bss = [
pkginfos.new_build_setting(
kind = bs.kind,
values = [
paths.join(target.path, p)
for p in bs.values
],
condition = bs.condition,
)
for bs in settings.hdr_srch_paths
]
local_includes.extend(lists.flatten([
bzl_selects.new_from_build_setting(bs)
for bs in hsp_bss
]))
copts.extend(lists.flatten([
bzl_selects.new_from_build_setting(bs)
for bs in settings.unsafe_flags
]))
linkopts = []
if target.linker_settings != None:
linkopts.extend(lists.flatten([
bzl_selects.new_from_build_setting(bs)
for bs in target.linker_settings.linked_libraries
]))
linkopts.extend(lists.flatten([
bzl_selects.new_from_build_setting(bs)
for bs in target.linker_settings.linked_frameworks
]))
if len(linkopts) > 0:
attrs["linkopts"] = bzl_selects.to_starlark(
linkopts,
kind_handlers = {
_condition_kinds.linked_library: bzl_selects.new_kind_handler(
transform = lambda ll: "-l{}".format(ll),
),
_condition_kinds.linked_framework: bzl_selects.new_kind_handler(
transform = lambda f: "-framework {}".format(f),
),
},
)
# Short path relative to Bazel output base. This is typically used when
# adding a path to a copt or linkeropt.
ext_repo_path = paths.join("external", repository_ctx.name)
copts.extend(local_includes)
# The `includes` attribute adds includes as -isystem which propagates
# to cc_XXX that depend upon the library. Providing includes as -I
# only provides the includes for this target.
# https://bazel.build/reference/be/c-cpp#cc_library.includes
def _local_includes_transform(p):
# Normalize the path and replace spaces with an escape sequence.
normalized = paths.normalize(paths.join(ext_repo_path, p))
normalized = normalized.replace(" ", "\\ ")
return "-I{}".format(normalized)
attrs["copts"] = bzl_selects.to_starlark(
copts,
kind_handlers = {
_condition_kinds.header_search_path: bzl_selects.new_kind_handler(
transform = _local_includes_transform,
),
_condition_kinds.private_includes: bzl_selects.new_kind_handler(
transform = _local_includes_transform,
),
},
)
bzl_target_name = target.label.name
if target.objc_src_info != None:
# Enable clang module support.
# https://bazel.build/reference/be/objective-c#objc_library.enable_modules
attrs["enable_modules"] = True
attrs["module_name"] = target.c99name
sdk_framework_bzl_selects = []
for sf in target.objc_src_info.builtin_frameworks:
platform_conditions = bazel_apple_platforms.for_framework(sf)
for pc in platform_conditions:
sdk_framework_bzl_selects.append(
bzl_selects.new(
value = sf,
kind = _condition_kinds.sdk_frameworks,
condition = pc,
),
)
attrs["sdk_frameworks"] = bzl_selects.to_starlark(
sdk_framework_bzl_selects,
)
# There is a known issue with Objective-C library targets not
# supporting the `@import` of modules defined in other Objective-C
# targets. As a workaround, we will define two targets. One is the
# `objc_library` target. The other is a `generate_modulemap`
# target. This second target generates a `module.modulemap` file and
# provides information about that generated file to `objc_library`
# targets, if `noop` is `False`. If `noop` is `True`, the target
# generates nothing and returns "empty" providers.
#
# Why not skip adding the `generate_modulemap` if `noop` is `True`?
# The logic that assigns dependencies for other targets has no way to
# know whether the modulemap target exists. Hence, we ensure that it
# always exists but does nothing.
#
# See `pkginfo_target_deps.bzl` for the logic that resolves the
# dependency labels.
# See `generate_modulemap.bzl` for details on the modulemap generation.
# See `//swiftpkg/tests/generate_modulemap_tests` package for a usage
# example.
modulemap_deps = _collect_modulemap_deps(deps)
load_stmts = [swiftpkg_generate_modulemap_load_stmt]
modulemap_target_name = pkginfo_targets.modulemap_label_name(bzl_target_name)
noop_modulemap = clang_src_info.modulemap_path != None
modulemap_attrs = {
"deps": bzl_selects.to_starlark(modulemap_deps),
"hdrs": clang_src_info.hdrs,
"module_name": target.c99name,
"noop": noop_modulemap,
"visibility": ["//:__subpackages__"],
}
decls = [
build_decls.new(objc_kinds.library, bzl_target_name, attrs = attrs),
build_decls.new(
kind = swiftpkg_kinds.generate_modulemap,
name = modulemap_target_name,
attrs = modulemap_attrs,
),
]
else:
load_stmts = [swift_interop_hint_load_stmt]
aspect_hint_target_name = pkginfo_targets.swift_hint_label_name(
bzl_target_name,
)
attrs["aspect_hints"] = [":{}".format(aspect_hint_target_name)]
aspect_hint_attrs = {"module_name": target.c99name}
decls = [
build_decls.new(clang_kinds.library, bzl_target_name, attrs = attrs),
build_decls.new(
kind = swift_kinds.interop_hint,
name = aspect_hint_target_name,
attrs = aspect_hint_attrs,
),
]
all_build_files.append(build_files.new(
load_stmts = load_stmts,
decls = decls,
))
return build_files.merge(*all_build_files)
# MARK: - System Library Targets
# GH009(chuck): Remove unused-variable directives
# buildifier: disable=unused-variable
def _system_library_build_file(target):
# GH009(chuck): Implement _system_library_build_file
return None
# MARK: - Apple xcframework Targets
def _xcframework_import_build_file(target, artifact_info):
if artifact_info.link_type == link_types.static:
load_stmts = [apple_static_xcframework_import_load_stmt]
kind = apple_kinds.static_xcframework_import
elif artifact_info.link_type == link_types.dynamic:
load_stmts = [apple_dynamic_xcframework_import_load_stmt]
kind = apple_kinds.dynamic_xcframework_import
else:
fail(
"""\
Unexpected link type for target. target: {target}, link_type: {link_type}, \
expected: {expected}\
""".format(
target = target.name,
link_type = artifact_info.link_type,
expected = ", ".join([link_types.static, link_types.dynamic]),
),
)
if target.path.endswith(".xcframework"):
glob = scg.new_fn_call(
"glob",
["{tpath}/**".format(tpath = target.path)],
)
else:
glob = scg.new_fn_call(
"glob",
["{tpath}/*.xcframework/**".format(tpath = target.path)],
)
decls = [
build_decls.new(
kind = kind,
name = pkginfo_targets.bazel_label_name(target),
attrs = {
"visibility": ["//:__subpackages__"],
"xcframework_imports": glob,
},
),
]
return build_files.new(
load_stmts = load_stmts,
decls = decls,
)
# MARK: - Apple Resource Group
def _apple_resource_bundle(target, package_name, default_localization):
bzl_target_name = pkginfo_targets.bazel_label_name(target)
bundle_label_name = pkginfo_targets.resource_bundle_label_name(bzl_target_name)
bundle_name = pkginfo_targets.resource_bundle_name(package_name, target.c99name)
infoplist_name = pkginfo_targets.resource_bundle_infoplist_label_name(
bzl_target_name,
)
resources = sorted([
r.path
for r in target.resources
])
load_stmts = [
apple_resource_bundle_load_stmt,
swiftpkg_resource_bundle_infoplist_load_stmt,
]
decls = [
build_decls.new(
kind = swiftpkg_kinds.resource_bundle_infoplist,
name = infoplist_name,
attrs = {
"region": default_localization,
},
),
build_decls.new(
kind = apple_kinds.resource_bundle,
name = bundle_label_name,
attrs = {
"bundle_name": bundle_name,
"infoplists": [":{}".format(infoplist_name)],
# Based upon the code in SPM, it looks like they only support unstructured resources.
# https://github.com/apple/swift-package-manager/blob/main/Sources/PackageModel/Resource.swift#L25-L33
"resources": resources,
"visibility": ["//:__subpackages__"],
},
),
]
return struct(
bundle_name = bundle_name,
bundle_label_name = bundle_label_name,
build_file = build_files.new(load_stmts = load_stmts, decls = decls),
)
def _apple_resource_bundle_for_swift(pkg_ctx, target):
apple_res_bundle_info = _apple_resource_bundle(
target,
pkg_ctx.pkg_info.name,
pkg_ctx.pkg_info.default_localization,
)
# Apparently, SPM provides a `Bundle.module` accessor. So, we do too.
# https://stackoverflow.com/questions/63237395/generating-resource-bundle-accessor-type-bundle-has-no-member-module
accessor_label_name = pkginfo_targets.resource_bundle_accessor_label_name(
pkginfo_targets.bazel_label_name(target),
)
return struct(
bundle_label_name = apple_res_bundle_info.bundle_label_name,
accessor_label_name = accessor_label_name,
build_file = build_files.merge(
apple_res_bundle_info.build_file,
build_files.new(
load_stmts = [swiftpkg_resource_bundle_accessor_load_stmt],
decls = [
build_decls.new(
kind = swiftpkg_kinds.resource_bundle_accessor,
name = accessor_label_name,
attrs = {
"bundle_name": apple_res_bundle_info.bundle_name,
},
),
],
),
),
)
def _apple_resource_bundle_for_clang(pkg_ctx, target):
apple_res_bundle_info = _apple_resource_bundle(
target,
pkg_ctx.pkg_info.name,
pkg_ctx.pkg_info.default_localization,
)
all_build_files = [apple_res_bundle_info.build_file]
objc_accessor_hdr_label_name = None
objc_accessor_impl_label_name = None
if target.objc_src_info:
bzl_target_name = pkginfo_targets.bazel_label_name(target)
objc_accessor_hdr_label_name = pkginfo_targets.objc_resource_bundle_accessor_hdr_label_name(
bzl_target_name,
)
objc_accessor_impl_label_name = pkginfo_targets.objc_resource_bundle_accessor_impl_label_name(
bzl_target_name,
)
all_build_files.append(
build_files.new(
load_stmts = [
swiftpkg_objc_resource_bundle_accessor_hdr_load_stmt,
swiftpkg_objc_resource_bundle_accessor_impl_load_stmt,
],
decls = [
build_decls.new(
kind = swiftpkg_kinds.objc_resource_bundle_accessor_hdr,
name = objc_accessor_hdr_label_name,
attrs = {
"module_name": target.c99name,
},
),
build_decls.new(
kind = swiftpkg_kinds.objc_resource_bundle_accessor_impl,
name = objc_accessor_impl_label_name,
attrs = {
"bundle_name": apple_res_bundle_info.bundle_name,
"module_name": target.c99name,
},
),
],
),
)
return struct(
bundle_label_name = apple_res_bundle_info.bundle_label_name,
objc_accessor_hdr_label_name = objc_accessor_hdr_label_name,
objc_accessor_impl_label_name = objc_accessor_impl_label_name,
build_file = build_files.merge(*all_build_files),
)
# MARK: - Modulemap Generation
def _collect_modulemap_deps(deps):
modulemap_deps = []
for dep in deps:
mm_values = [
v
for v in dep.value
if pkginfo_targets.is_modulemap_label(v)
]
if len(mm_values) == 0:
continue
mm_dep = bzl_selects.new(
value = mm_values,
kind = dep.kind,
condition = dep.condition,
)
modulemap_deps.append(mm_dep)
return modulemap_deps
def _generate_modulemap_for_swift_target(target, deps):
load_stmts = [swiftpkg_generate_modulemap_load_stmt]
bzl_target_name = pkginfo_targets.bazel_label_name(target)
modulemap_target_name = pkginfo_targets.modulemap_label_name(bzl_target_name)
modulemap_deps = _collect_modulemap_deps(deps)
attrs = {
"deps": bzl_selects.to_starlark(modulemap_deps),
"hdrs": [":{}".format(bzl_target_name)],
"module_name": target.c99name,
"visibility": ["//:__subpackages__"],
}
decls = [
build_decls.new(
kind = swiftpkg_kinds.generate_modulemap,
name = modulemap_target_name,
attrs = attrs,
),
]
return build_files.new(load_stmts = load_stmts, decls = decls)
# MARK: - Products Entry Point
def _new_for_products(pkg_ctx):
bld_files = lists.compact([
_new_for_product(pkg_ctx, prod)
for prod in pkg_ctx.pkg_info.products
])
# If we did not generate any build files, return an empty one.
if len(bld_files) == 0:
return build_files.new()
return build_files.merge(*bld_files)
def _new_for_product(pkg_ctx, product):
prod_type = product.type
if prod_type.is_executable:
return _executable_product_build_file(
pkg_ctx.pkg_info,
product,
pkg_ctx.repo_name,
)
elif prod_type.is_library:
return _library_product_build_file(pkg_ctx, product)
elif prod_type.is_macro:
return _alias_for_macro_build_file(pkg_ctx, product)
# GH046: Check for plugin product
return None
def _executable_product_build_file(pkg_info, product, repo_name):
# Retrieve the targets
targets = [
pkginfo_targets.get(pkg_info.targets, tname)
for tname in product.targets
]
targets_len = len(targets)
if targets_len == 1:
target = targets[0]
if target.type == target_types.executable:
# If the alias name will have the same name as the target, then do not create the alias.
label = pkginfo_targets.bazel_label(target, repo_name)
if label.name == product.name:
return None
# Create an alias to the binary target created in the target package.
return build_files.new(
decls = [
build_decls.new(
native_kinds.alias,
product.name,
attrs = {
"actual": bazel_labels.normalize(label),
"visibility": ["//visibility:public"],
},
),
],
)
else:
# This is an old-style (pre-5.4) configuration where an executable
# product references a regular target.
# Create the binary target here.
return build_files.new(
load_stmts = [load_statements.new(swift_location, swift_kinds.binary)],
decls = [_swift_binary_from_product(product, target, repo_name)],
)
elif targets_len > 1:
fail("Multiple targets specified for an executable product. name:", product.name)
else:
fail("Did not find any targets associated with product. name:", product.name)
def _library_product_build_file(pkg_ctx, product):
# A library product can reference one or more Swift targets. Hence a
# dependency on a library product is a shorthand for depend upon all of the
# Swift targets that is associated with the product. We use a
# `swift_library_group` to represent this.
target_labels = []
for tname in product.targets:
target = lists.find(pkg_ctx.pkg_info.targets, lambda t: t.name == tname)
if target == None:
fail("Did not find a target named {}.".format(tname))
target_labels.extend(
pkginfo_target_deps.labels_for_target(pkg_ctx.repo_name, target),
)
if len(target_labels) == 0:
fail("No targets specified for a library product. name:", product.name)
return build_files.new(
load_stmts = [swift_library_group_load_stmt],
decls = [
build_decls.new(
swift_kinds.library_group,
product.name,
attrs = {
"deps": [
bazel_labels.normalize(label)
for label in target_labels
],
"visibility": ["//visibility:public"],
},
),
],
)
def _swift_binary_from_product(product, dep_target, repo_name):
return build_decls.new(
kind = swift_kinds.binary,
name = product.name,
attrs = {
"deps": [bazel_labels.normalize(
pkginfo_targets.bazel_label(dep_target, repo_name = repo_name),
)],
"visibility": ["//visibility:public"],
},
)
def _alias_for_macro_build_file(pkg_ctx, product):
if len(product.targets) != 1:
fail("""\
Expected only one target for the macro product {name} but received {count}.\
""".format(
name = product.name,
count = len(product.targets),
))
target = pkginfo_targets.get(pkg_ctx.pkg_info.targets, product.targets[0])
label_name = pkginfo_targets.bazel_label_name(target)
return build_files.new(
decls = [
build_decls.new(
native_kinds.alias,
product.name,
attrs = {
"actual": ":{}".format(label_name),
"visibility": ["//visibility:public"],
},
),
],
)
# MARK: - License
def _new_for_license(pkg_info, license):
packageinfo_target_name = "package_info.rspm"
decls = [
build_decls.new(
rules_license_kinds.package_info,
packageinfo_target_name,
attrs = {
"package_name": pkg_info.name,
"package_url": pkg_info.url,
"package_version": pkg_info.version,
},
),
]
default_package_metadata = [":{}".format(packageinfo_target_name)]
load_stmts = [rules_license_package_info_load_stmt]
if license:
license_target_name = "license.rspm"
decls.append(
build_decls.new(
rules_license_kinds.license,
license_target_name,
attrs = {
"license_text": license,
},
),
)
load_stmts.append(rules_license_license_load_stmt)
default_package_metadata.insert(0, ":{}".format(license_target_name))
return build_files.new(
load_stmts = load_stmts,
package_attrs = {"default_package_metadata": default_package_metadata},
decls = decls,
)
# MARK: - Constants and API Definition
swift_location = "@build_bazel_rules_swift//swift:swift.bzl"
swift_kinds = struct(
library = "swift_library",
library_group = "swift_library_group",
binary = "swift_binary",
test = "swift_test",
c_module = "swift_c_module",
compiler_plugin = "swift_compiler_plugin",
interop_hint = "swift_interop_hint",
)
swift_library_load_stmt = load_statements.new(
swift_location,
swift_kinds.library,
)
swift_library_group_load_stmt = load_statements.new(
swift_location,
swift_kinds.library_group,
)
swift_binary_load_stmt = load_statements.new(
swift_location,
swift_kinds.binary,
)
swift_c_module_load_stmt = load_statements.new(
swift_location,
swift_kinds.c_module,
)
swift_compiler_plugin_load_stmt = load_statements.new(
swift_location,
swift_kinds.compiler_plugin,
)
swift_interop_hint_load_stmt = load_statements.new(
swift_location,
swift_kinds.interop_hint,
)
swift_test_load_stmt = load_statements.new(swift_location, swift_kinds.test)
clang_kinds = struct(
library = "cc_library",
)
objc_kinds = struct(
library = "objc_library",
)
native_kinds = struct(
alias = "alias",
)
rules_license_license_location = "@rules_license//rules:license.bzl"
rules_license_package_info_location = "@rules_license//rules:package_info.bzl"
rules_license_kinds = struct(
license = "license",
package_info = "package_info",
)
rules_license_license_load_stmt = load_statements.new(
rules_license_license_location,
rules_license_kinds.license,
)
rules_license_package_info_load_stmt = load_statements.new(
rules_license_package_info_location,
rules_license_kinds.package_info,
)
skylib_build_test_location = "@bazel_skylib//rules:build_test.bzl"
skylib_kinds = struct(
build_test = "build_test",
)
skylib_build_test_load_stmt = load_statements.new(
skylib_build_test_location,
skylib_kinds.build_test,
)
swiftpkg_build_files = struct(
new_for_license = _new_for_license,
new_for_target = _new_for_target,
new_for_products = _new_for_products,
new_for_product = _new_for_product,
)
apple_kinds = struct(
static_xcframework_import = "apple_static_xcframework_import",
dynamic_xcframework_import = "apple_dynamic_xcframework_import",
resource_bundle = "apple_resource_bundle",
)
apple_apple_location = "@build_bazel_rules_apple//apple:apple.bzl"
apple_resources_location = "@build_bazel_rules_apple//apple:resources.bzl"
apple_static_xcframework_import_load_stmt = load_statements.new(
apple_apple_location,
apple_kinds.static_xcframework_import,
)
apple_dynamic_xcframework_import_load_stmt = load_statements.new(
apple_apple_location,
apple_kinds.dynamic_xcframework_import,
)
apple_resource_bundle_load_stmt = load_statements.new(
apple_resources_location,
apple_kinds.resource_bundle,
)
swiftpkg_kinds = struct(
generate_modulemap = "generate_modulemap",
objc_resource_bundle_accessor_hdr = "objc_resource_bundle_accessor_hdr",
objc_resource_bundle_accessor_impl = "objc_resource_bundle_accessor_impl",
resource_bundle_accessor = "resource_bundle_accessor",
resource_bundle_infoplist = "resource_bundle_infoplist",