This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
forked from bazel-ios/rules_ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxcodeproj.bzl
1128 lines (988 loc) · 51.7 KB
/
xcodeproj.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
""" Xcode Project Generation Logic """
load("@build_bazel_rules_apple//apple:providers.bzl", "AppleBundleInfo")
load("@build_bazel_rules_apple//apple/internal:platform_support.bzl", "platform_support")
load("@build_bazel_rules_swift//swift:swift.bzl", "SwiftInfo")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("//rules:hmap.bzl", "HeaderMapInfo")
load("//rules/framework:vfs_overlay.bzl", "VFSOverlayInfo", VFS_OVERLAY_FRAMEWORK_SEARCH_PATH = "FRAMEWORK_SEARCH_PATH")
load("//rules:additional_scheme_info.bzl", "AdditionalSchemeInfo")
load("//rules:features.bzl", "feature_names")
def _get_attr_values_for_name(deps, provider, field):
return [
getattr(dep[provider], field)
for dep in deps
if dep and provider in dep
]
_TargetInfo = provider()
_SrcsInfo = provider()
_PLATFORM_MAPPING = {
"ios": "iOS",
"macos": "macOS",
}
_PRODUCT_SPECIFIER_LENGTH = len("com.apple.product-type.")
_IGNORE_AS_TARGET_TAG = "xcodeproj-ignore-as-target"
def _dir(o):
return [
x
for x in dir(o)
if x not in ("to_json", "to_proto")
]
def _is_current_project_file(f):
return f.is_source and _is_current_project_path(f.path)
def _is_current_project_path(path):
return not path.startswith("external/")
def _srcs_info_build_files(ctx):
path = ctx.build_file_path
if not _is_current_project_path(path):
return []
return [path]
def _xcodeproj_aspect_collect_hmap_paths(deps, target, ctx):
"""Helper method collecting hmap paths from HeaderMapInfo
Args:
deps: array of deps collected from target
target: same as what is passed into aspect impl
ctx: same as what is passed into aspect impl
Returns:
Array of hmap paths (relative to bazel root)
"""
hmap_paths = []
for dep in deps:
if HeaderMapInfo in dep:
files = getattr(dep[HeaderMapInfo], "files").to_list()
for file in files:
# Relative to workspace root
relative_path = getattr(file, "path")
hmap_paths.append(relative_path)
return hmap_paths
def _make_swift_vfs_args(vfs_path):
return [
"-Xfrontend",
"-vfsoverlay$BAZEL_WORKSPACE_ROOT/{}".format(vfs_path),
"-Xfrontend",
"-F{}".format(VFS_OVERLAY_FRAMEWORK_SEARCH_PATH),
"-I{}".format(VFS_OVERLAY_FRAMEWORK_SEARCH_PATH),
"-Xcc",
"-ivfsoverlay$BAZEL_WORKSPACE_ROOT/{}".format(vfs_path),
"-Xcc",
"-F{}".format(VFS_OVERLAY_FRAMEWORK_SEARCH_PATH),
"-vfsoverlay$BAZEL_WORKSPACE_ROOT/{}".format(vfs_path),
"-F{}".format(VFS_OVERLAY_FRAMEWORK_SEARCH_PATH),
]
def _make_swift_copts(target, deps, ctx):
hmap_files = []
vfs_files = []
collected_vfs = False
for dep in deps:
if VFSOverlayInfo in dep and not collected_vfs:
vfs_files.extend(dep[VFSOverlayInfo].files.to_list())
collected_vfs = True
if HeaderMapInfo in dep:
hmap_files.extend(dep[HeaderMapInfo].files.to_list())
copts = []
for hmap in hmap_files:
copts.append("-Xcc")
hmap_arg = "-I$BAZEL_WORKSPACE_ROOT/%s" % hmap.path
copts.append("\"%s\"" % hmap_arg)
# Include the root
copts.append("-Xcc")
copts.append("-I\"$BAZEL_WORKSPACE_ROOT\"")
for vfs in vfs_files:
copts.extend(_make_swift_vfs_args(vfs.path))
return copts
# Put copts inside of a depset as a string
def _join_copts(copts):
if not copts:
return []
return [" ".join(copts)]
# Get the swift_libaries' copts propagate the deps copts. This is needed to
# push the copts upwards from the libraries to top level bundles.
def _xcodeproj_aspect_collect_swift_copts(deps, target, ctx):
copts = None
if ctx.rule.kind == "swift_library":
copts = _make_swift_copts(target, deps, ctx)
else:
for dep in deps:
if _SrcsInfo in dep:
dep_swift_opts_list = dep[_SrcsInfo].swift_copts.to_list()
if len(dep_swift_opts_list):
return dep_swift_opts_list
return _join_copts(copts)
def _make_objc_vfs_args(vfs_path):
return [
"-ivfsoverlay$BAZEL_WORKSPACE_ROOT/{}".format(vfs_path),
"-F{}".format(VFS_OVERLAY_FRAMEWORK_SEARCH_PATH),
]
# Derives hmap copts and vfs copts from the inputs as rules_ios handles them.
# This is similar to what it does for HEADER_SEARCH_PATHS. Longer term, perhaps
# code can be removed in-favor for XCHammer.
def _make_objc_copts(target, deps, ctx):
hmap_files = []
vfs_files = []
collected_vfs = False
for dep in deps:
if VFSOverlayInfo in dep and not collected_vfs:
vfs_files.extend(dep[VFSOverlayInfo].files.to_list())
collected_vfs = True
if HeaderMapInfo in dep:
hmap_files.extend(dep[HeaderMapInfo].files.to_list())
copts = []
for hmap in hmap_files:
hmap_arg = "-I$BAZEL_WORKSPACE_ROOT/%s" % hmap.path
copts.append("\"%s\"" % hmap_arg)
# Include the root
copts.append("-I\"$BAZEL_WORKSPACE_ROOT\"")
for vfs in vfs_files:
copts.extend(_make_objc_vfs_args(vfs.path))
return copts
# Similar to above but for objc
def _xcodeproj_aspect_collect_objc_copts(deps, target, ctx):
copts = None
if ctx.rule.kind == "objc_library":
copts = _make_objc_copts(target, deps, ctx)
else:
for dep in deps:
if _SrcsInfo in dep:
dep_objc_copts = dep[_SrcsInfo].objc_copts.to_list()
if len(dep_objc_copts):
return dep_objc_copts
return _join_copts(copts)
def _targeted_device_family(ctx):
""" The targeted device family of the rule
Args:
ctx: same as what is passed into aspect impl
Returns:
the targeted device family in the format recognized by xcode
"""
families = platform_support.ui_device_family_plist_value(platform_prerequisites = struct(
device_families = getattr(ctx.rule.attr, "families", []),
))
return ",".join(["%d" % family for family in families]) if families else None
def _xcodeproj_aspect_impl(target, ctx):
providers = []
deps = []
deps += getattr(ctx.rule.attr, "deps", [])
deps += getattr(ctx.rule.attr, "infoplists", [])
tags = getattr(ctx.rule.attr, "tags", [])
entitlements = getattr(ctx.rule.attr, "entitlements", None)
if entitlements:
deps.append(entitlements)
virtualize_frameworks = feature_names.virtualize_frameworks in ctx.features
if virtualize_frameworks:
# Effectivly for virtual frameworks we don't need to copy the files
# because they are read directly from the VFS
objc_copts = _xcodeproj_aspect_collect_objc_copts(deps, target, ctx)
swift_copts = _xcodeproj_aspect_collect_swift_copts(deps, target, ctx)
hmap_paths = []
else:
objc_copts = []
swift_copts = []
hmap_paths = _xcodeproj_aspect_collect_hmap_paths(deps, target, ctx)
# TODO: handle apple_resource_bundle targets
env_vars = ()
commandline_args = ()
# bazel_build_target_name is the argument to bazel build. Example values:
# Frameworks/FWName:FWName_library
# @some_external_repo//Frameworks/FWName:FWName_library
# bazel_bin_subdir is the subdirectory within bazel-bin for artifacts built by this target. Example values:
# Frameworks/FWName
# external/some_external_repo/Frameworks/FWName
bazel_build_target_name = ""
if target.label.workspace_name != "":
bazel_build_target_name = "@%s//" % target.label.workspace_name
bazel_build_target_name += "%s:%s" % (target.label.package, target.label.name)
bazel_bin_subdir = "%s/%s" % (target.label.workspace_root, target.label.package)
if AppleBundleInfo in target:
swift_objc_header_path = None
if SwiftInfo in target:
for h in target[apple_common.Objc].direct_headers:
if h.path.endswith("-Swift.h"):
swift_objc_header_path = h.path
bundle_info = target[AppleBundleInfo]
test_host_appname = None
test_host_target = None
if ctx.rule.kind == "ios_unit_test":
# The following converts {"env_k1": "env_v1", "env_k2": "env_v2"}
# to (("env_k1", "env_v1"), ("env_k2", "env_v2"))
# for both "env vars" and "command line args"
env_key_value_pairs = getattr(ctx.rule.attr, "env", {})
env_vars = tuple(env_key_value_pairs.items())
commandlines_args = getattr(ctx.rule.attr, "args", [])
commandline_args = tuple(commandlines_args)
test_host_target = getattr(ctx.rule.attr, "test_host", None)
if test_host_target:
test_host_appname = test_host_target[_TargetInfo].direct_targets[0].name
framework_includes = depset([], transitive = _get_attr_values_for_name(deps, _SrcsInfo, "framework_includes"))
info = struct(
name = bundle_info.bundle_name,
bundle_id = bundle_info.bundle_id,
bundle_extension = bundle_info.bundle_extension,
bazel_build_target_workspace = target.label.workspace_name or ctx.workspace_name,
bazel_build_target_name = bazel_build_target_name,
bazel_bin_subdir = bazel_bin_subdir,
srcs = depset([], transitive = _get_attr_values_for_name(deps, _SrcsInfo, "srcs")),
non_arc_srcs = depset([], transitive = _get_attr_values_for_name(deps, _SrcsInfo, "non_arc_srcs")),
asset_srcs = depset([], transitive = _get_attr_values_for_name(deps, _SrcsInfo, "asset_srcs")),
framework_includes = framework_includes,
cc_defines = depset([], transitive = _get_attr_values_for_name(deps, _SrcsInfo, "cc_defines")),
swift_defines = depset([], transitive = _get_attr_values_for_name(deps, _SrcsInfo, "swift_defines")),
build_files = depset(_srcs_info_build_files(ctx), transitive = _get_attr_values_for_name(deps, _SrcsInfo, "build_files")),
product_type = bundle_info.product_type[_PRODUCT_SPECIFIER_LENGTH:],
platform_type = bundle_info.platform_type,
minimum_os_version = bundle_info.minimum_os_version,
test_host_appname = test_host_appname,
env_vars = env_vars,
swift_objc_header_path = swift_objc_header_path,
swift_module_paths = depset([], transitive = _get_attr_values_for_name(deps, _SrcsInfo, "swift_module_paths")),
hmap_paths = depset([], transitive = _get_attr_values_for_name(deps, _SrcsInfo, "hmap_paths")),
swift_copts = depset([], transitive = _get_attr_values_for_name(deps, _SrcsInfo, "swift_copts")),
objc_copts = depset([], transitive = _get_attr_values_for_name(deps, _SrcsInfo, "objc_copts")),
commandline_args = commandline_args,
targeted_device_family = _targeted_device_family(ctx),
)
if ctx.rule.kind != "apple_framework_packaging":
providers.append(
_SrcsInfo(
srcs = info.srcs,
non_arc_srcs = info.non_arc_srcs,
asset_srcs = info.asset_srcs,
framework_includes = info.framework_includes,
cc_defines = info.cc_defines,
swift_defines = info.swift_defines,
build_files = depset(_srcs_info_build_files(ctx)),
direct_srcs = [],
hmap_paths = info.hmap_paths,
swift_copts = info.swift_copts,
objc_copts = info.objc_copts,
swift_module_paths = info.swift_module_paths,
),
)
direct_targets = []
transitive_targets = []
if not _IGNORE_AS_TARGET_TAG in tags:
direct_targets.append(info)
transitive_targets.append(info)
if test_host_target:
# Add the test_host_target to the targets to be added to the xcode project
test_host_direct_targets = test_host_target[_TargetInfo].direct_targets
direct_targets.extend(test_host_direct_targets)
transitive_targets.extend(test_host_direct_targets)
target_info = _TargetInfo(direct_targets = direct_targets, targets = depset(transitive_targets, transitive = _get_attr_values_for_name(deps, _TargetInfo, "targets")))
providers.append(target_info)
else:
srcs = []
non_arc_srcs = []
asset_srcs = []
for attr in _dir(ctx.rule.files):
if attr == "srcs":
srcs += getattr(ctx.rule.files, attr, [])
elif attr == "non_arc_srcs":
non_arc_srcs += getattr(ctx.rule.files, attr, [])
else:
asset_srcs += getattr(ctx.rule.files, attr, [])
srcs = [f for f in srcs if _is_current_project_file(f)]
non_arc_srcs = [f for f in non_arc_srcs if _is_current_project_file(f)]
asset_srcs = [f for f in asset_srcs if _is_current_project_file(f)]
framework_includes = _get_attr_values_for_name(deps, _SrcsInfo, "framework_includes")
cc_defines = _get_attr_values_for_name(deps, _SrcsInfo, "cc_defines")
swift_defines = _get_attr_values_for_name(deps, _SrcsInfo, "swift_defines")
if CcInfo in target:
framework_includes.append(target[CcInfo].compilation_context.framework_includes)
cc_defines.append(target[CcInfo].compilation_context.defines)
swift_module_paths = []
if SwiftInfo in target:
swift_info = target[SwiftInfo]
swift_defines.append(depset(_collect_swift_defines(swift_info.direct_modules)))
swift_defines.append(depset(_collect_swift_defines(swift_info.transitive_modules.to_list())))
swift_module_paths = [m.swift.swiftmodule.path for m in target[SwiftInfo].direct_modules]
providers.append(
_SrcsInfo(
srcs = depset(srcs, transitive = _get_attr_values_for_name(deps, _SrcsInfo, "srcs")),
non_arc_srcs = depset(non_arc_srcs, transitive = _get_attr_values_for_name(deps, _SrcsInfo, "non_arc_srcs")),
asset_srcs = depset(asset_srcs, transitive = _get_attr_values_for_name(deps, _SrcsInfo, "asset_srcs")),
framework_includes = depset([], transitive = framework_includes),
cc_defines = depset([], transitive = cc_defines),
build_files = depset(_srcs_info_build_files(ctx), transitive = _get_attr_values_for_name(deps, _SrcsInfo, "build_files")),
swift_defines = depset([], transitive = swift_defines),
direct_srcs = srcs,
hmap_paths = depset(hmap_paths),
swift_copts = depset(direct = swift_copts),
objc_copts = depset(direct = objc_copts),
swift_module_paths = depset(swift_module_paths, transitive = _get_attr_values_for_name(deps, _SrcsInfo, "swift_module_paths")),
),
)
infos = None
actual = None
if ctx.rule.kind in ("test_suite"):
actual = getattr(ctx.rule.attr, "tests")[0]
elif ctx.rule.kind in ("alias"):
actual = getattr(ctx.rule.attr, "actual")
if actual and _TargetInfo in actual:
infos = actual[_TargetInfo].direct_targets
targets = None
if infos:
targets = depset(infos, transitive = _get_attr_values_for_name(deps, _TargetInfo, "targets"))
else:
targets = depset(transitive = _get_attr_values_for_name(deps, _TargetInfo, "targets"))
providers.append(
_TargetInfo(direct_targets = infos, targets = targets),
)
return providers
_xcodeproj_aspect = aspect(
implementation = _xcodeproj_aspect_impl,
attr_aspects = ["deps", "actual", "tests", "infoplists", "entitlements", "resources", "test_host"],
)
def _collect_swift_defines(modules):
defines = {}
for module in modules:
swift_module = module.swift
if swift_module and swift_module.defines:
for x in swift_module.defines:
defines[x] = None
return defines.keys()
# Borrowed from rules_swift/compiling.bzl
def _exclude_swift_incompatible_define(define):
"""A `map_each` helper that excludes a define if it is not Swift-compatible.
This function rejects any defines that are not of the form `FOO=1` or `FOO`.
Note that in C-family languages, the option `-DFOO` is equivalent to
`-DFOO=1` so we must preserve both.
Args:
define: A string of the form `FOO` or `FOO=BAR` that represents an
Objective-C define.
Returns:
The token portion of the define it is Swift-compatible, or `None`
otherwise.
"""
token, equal, value = define.partition("=")
if (not equal and not value) or (equal == "=" and value == "1"):
return token
return None
def _framework_search_paths_for_target(target_name, all_transitive_targets):
# Ensure Xcode will resolve references to the XCTest framework.
framework_search_paths = ["$(PLATFORM_DIR)/Developer/Library/Frameworks"]
# all_transitive_targets includes all the targets built with their different configurations.
# Some configurations are only applied when the target is reached transitively
# (e.g. via an app or test that applies and propagates new build settings).
for at in all_transitive_targets:
if at.name == target_name:
for fi in at.framework_includes.to_list():
if fi[0] != "/":
fi = "$BAZEL_WORKSPACE_ROOT/%s" % fi
framework_search_paths.append("\"%s\"" % fi)
return framework_search_paths
def _swiftmodulepaths_for_target(target_name, all_transitive_targets):
"""Helper method to aggregate all swiftmodules paths that could be generated by this target
Args:
target_name: The name of the target whose header search paths we want to aggregate
all_transitive_targets: all targets that have been traversed by the aspect
Returns:
One string joined by space with all swiftmodule paths, each path is quoted and separated by a space
"""
swiftmodulefiles = []
# all_transitive_targets includes all the targets built with their different configurations.
# Some configurations are only applied when the target is reached transitively
# (e.g. via an app or test that applies and propagates new build settings).
for at in all_transitive_targets:
if at.name == target_name:
for modulefilename in at.swift_module_paths.to_list():
if modulefilename not in swiftmodulefiles:
swiftmodulefiles.append(modulefilename)
swiftmodulefiles.append(modulefilename.replace(".swiftmodule", ".swiftdoc"))
swiftmodulefiles.append(modulefilename.replace(".swiftmodule", ".swiftsourceinfo"))
return " ".join(swiftmodulefiles)
def _header_search_paths_for_target(target_name, all_transitive_targets):
"""Helper method transforming valid hmap paths into full absolute paths and concat together
Args:
target_name: The name of the target whose header search paths we want to aggregate
all_transitive_targets: all targets that have been traversed by the aspect
Returns:
One string joined by absolute hmap paths, each path is quoted and separated by a space
"""
header_search_paths = []
all_hmaps = []
# all_transitive_targets includes all the targets built with their different configurations.
# Some configurations are only applied when the target is reached transitively
# (e.g. via an app or test that applies and propagates new build settings).
for at in all_transitive_targets:
if at.name == target_name:
all_hmaps.extend(at.hmap_paths.to_list())
for hmap in all_hmaps:
if len(hmap) == 0:
continue
if hmap != "." and hmap[0] != "/":
hmap = "$BAZEL_WORKSPACE_ROOT/%s" % hmap
header_search_paths.append("\"%s\"" % hmap)
# We always need to include a search path at workspace root
header_search_paths.append("\"$BAZEL_WORKSPACE_ROOT\"")
return " ".join(header_search_paths)
def _swift_copts_for_target(target_name, all_transitive_targets):
copts = []
for at in all_transitive_targets:
# Returns the first matching targets - consider finding this in another
# routine
if at.name == target_name:
copts.extend(at.swift_copts.to_list())
break
return copts
# Similar to above but for objc
def _objc_copts_for_target(target_name, all_transitive_targets):
copts = []
for at in all_transitive_targets:
if at.name == target_name:
copts.extend(at.objc_copts.to_list())
break
return copts
_XCASSETS = "xcassets"
_XCDATAMODELD = "xcdatamodeld"
_XCDATAMODEL = "xcdatamodel"
_XCMAPPINGMODEL = "xcmappingmodel"
_XCSTICKERS = "xcstickers"
def _classify_asset(path):
"""Helper method to identify known extesion via the passed in path
Args:
path: single file/directory path (relative or absolute)
Returns:
A tuple where first argument is the first known extension found, or None.
Second argument is the path that we constructed so far that leads to this known extension.
For example: foo/bar/a.xcassets/someicon.imageset gives back
("xcassets", "foo/bar/a.xcassets")
"""
# Order is important here, since we stop as soon as a match is found
# This is especially important for xcdatamodeld vesus xcdatamodel,
# since a xcdatamodeld can have many xcdatamodel as children
known_xc_extensions = [_XCDATAMODELD, _XCDATAMODEL, _XCMAPPINGMODEL, _XCASSETS, _XCSTICKERS]
path_components = path.split("/")
path_so_far = ""
for component in path_components:
path_so_far += component
for extension in known_xc_extensions:
if component.endswith("." + extension):
return (extension, path_so_far)
# Match no exntesion, keep appending the component
path_so_far += "/"
# No match, return None and complete path
return (None, path_so_far)
def _gather_asset_sources(target_info, path_prefix):
"""Helper method gather asset sources (non-code resources) based on its type or special names
Args:
target_info: containing asset_srcs and build_files that we compose the list from
path_prefix: any prefix needed to correctly resolve a relative path in xcode proj
Returns:
array of dictionry, each dict represents a Target Resource described in XcodeGen:
https://github.com/yonaskolb/XcodeGen/blob/master/Docs/ProjectSpec.md#target-source
"""
asset_sources = []
datamodel_groups = {}
catalog_groups = {}
for s in target_info.asset_srcs.to_list():
short_path = s.short_path
(extension, path_so_far) = _classify_asset(short_path)
# Reference for logics below:
# https://github.com/bazelbuild/rules_apple/blob/master/apple/internal/partials/support/resources_support.bzl#L162
if extension == None:
payload = {
"path": paths.join(path_prefix, short_path),
"optional": True,
"buildPhase": "none",
}
asset_sources.append(payload)
elif extension in [_XCASSETS, _XCSTICKERS]:
basename = paths.basename(path_so_far)
if basename not in catalog_groups:
catalog_groups[path_so_far] = basename
elif extension in [_XCDATAMODELD, _XCDATAMODEL, _XCMAPPINGMODEL]:
# Any file under .xcdatamodeld or .xcdatamodel is ignored but itself will be added.
# However there two possibilities for .xcdatamodel to exist:
# 1. foo/bar.xcdatamodel (standalone)
# 2. foo/foo2.xcdatamodeld/bar(v2).xcdatamodel (versioned datadmodel)
# Iteration of path components above stops at xcdatamodeld first
# so the second case is taken care of
datamodel_name = paths.basename(path_so_far)
if datamodel_name not in datamodel_groups:
datamodel_groups[path_so_far] = datamodel_name
else:
fail("Known extension {} returned from _classify_asset method is not handled".format(extension))
for datamodel_key in datamodel_groups.keys():
payload = {
"path": paths.join(path_prefix, datamodel_key),
"optional": True,
"buildPhase": "none",
}
asset_sources.append(payload)
for asset_key in catalog_groups.keys():
payload = {
"path": paths.join(path_prefix, asset_key),
"optional": True,
"buildPhase": "none",
}
asset_sources.append(payload)
# Append BUILD.bazel files to project
asset_sources += [{
"path": paths.join(path_prefix, p),
"optional": True,
"buildPhase": "none",
# TODO: add source language type once https://github.com/yonaskolb/XcodeGen/issues/850 is resolved
} for p in target_info.build_files.to_list()]
return asset_sources
_CONFLICTING_TARGET_MSG = """\
Failed to generate xcodeproj for "{}" due to conflicting targets:
Target "{}" is already defined with type "{}".
A same-name target with label "{}" of type "{}" wants to override.
Double check your rule declaration for naming or add `xcodeproj-ignore-as-target` as a tag to choose which target to ignore.
"""
_BUILD_WITH_BAZEL_SCRIPT = """
set -euxo pipefail
cd $BAZEL_WORKSPACE_ROOT
export BAZEL_DIAGNOSTICS_DIR="$BUILD_DIR/../../bazel-xcode-diagnostics/"
mkdir -p $BAZEL_DIAGNOSTICS_DIR
export DATE_SUFFIX="$(date +%Y%m%d.%H%M%S%L)"
export BAZEL_BUILD_EVENT_TEXT_FILENAME="$BAZEL_DIAGNOSTICS_DIR/build-event-$DATE_SUFFIX.txt"
export BAZEL_BUILD_EXECUTION_LOG_FILENAME="$BAZEL_DIAGNOSTICS_DIR/build-execution-log-$DATE_SUFFIX.log"
export BAZEL_PROFILE_FILENAME="$BAZEL_DIAGNOSTICS_DIR/build-profile-$DATE_SUFFIX.log"
env -u RUBYOPT -u RUBY_HOME -u GEM_HOME $BAZEL_BUILD_EXEC $BAZEL_BUILD_TARGET_LABEL
$BAZEL_INSTALLER
"""
def _populate_xcodeproj_targets_and_schemes(ctx, targets, src_dot_dots, all_transitive_targets):
"""Helper method to generate dicts for targets and schemes inside Xcode context
Args:
ctx: context provided to rule impl
targets: each dict contains info of a bazel target (not xcode target)
src_dot_dots: caller needs to figure out how many `../` needed to correctly points to an actual file
all_transitive_targets: includes all the targets built with their different configurations.
Some configurations are only applied when the target is reached transitively
(e.g. via an app or test that applies and propagates new build settings).
Returns:
A tuple where first argument a dict representing xcode targets.
Second argument represents xcode schemes
"""
xcodeproj_targets_by_name = {}
xcodeproj_schemes_by_name = {}
for target_info in targets:
target_name = target_info.name
product_type = target_info.product_type
lldbinit_file = "$CONFIGURATION_TEMP_DIR/%s.lldbinit" % target_name
if target_name in xcodeproj_targets_by_name:
existing_type = xcodeproj_targets_by_name[target_name]["type"]
if product_type != existing_type:
fail(_CONFLICTING_TARGET_MSG.format(ctx.label, target_name, existing_type, target_info.bazel_build_target_name, target_info.product_type))
target_macho_type = "staticlib" if product_type == "framework" else "$(inherited)"
compiled_sources = [{
"path": paths.join(src_dot_dots, s.short_path),
"optional": True,
} for s in target_info.srcs.to_list()]
compiled_non_arc_sources = [{
"path": paths.join(src_dot_dots, s.short_path),
"optional": True,
"compilerFlags": "-fno-objc-arc",
} for s in target_info.non_arc_srcs.to_list()]
asset_sources = _gather_asset_sources(target_info, src_dot_dots)
target_settings = {
"PRODUCT_NAME": target_name,
"ONLY_ACTIVE_ARCH": "YES",
"BAZEL_BIN_SUBDIR": target_info.bazel_bin_subdir,
"MACH_O_TYPE": target_macho_type,
"CLANG_ENABLE_MODULES": "YES",
"CLANG_ENABLE_OBJC_ARC": "YES",
"BAZEL_BUILD_TARGET_LABEL": target_info.bazel_build_target_name,
"BAZEL_BUILD_TARGET_WORKSPACE": target_info.bazel_build_target_workspace,
}
virtualize_frameworks = feature_names.virtualize_frameworks in ctx.features
if virtualize_frameworks:
target_settings["OTHER_SWIFT_FLAGS"] = _swift_copts_for_target(target_name, all_transitive_targets)
target_settings["OTHER_CFLAGS"] = _objc_copts_for_target(target_name, all_transitive_targets)
else:
target_settings["BAZEL_SWIFTMODULEFILES_TO_COPY"] = _swiftmodulepaths_for_target(target_name, all_transitive_targets)
target_settings["HEADER_SEARCH_PATHS"] = _header_search_paths_for_target(target_name, all_transitive_targets)
framework_search_paths = _framework_search_paths_for_target(target_name, all_transitive_targets)
target_settings["FRAMEWORK_SEARCH_PATHS"] = " ".join(framework_search_paths)
macros = ["\"%s\"" % d for d in target_info.cc_defines.to_list()]
macros.append("$(inherited)")
target_settings["GCC_PREPROCESSOR_DEFINITIONS"] = " ".join(macros)
if target_info.swift_objc_header_path:
target_settings["SWIFT_OBJC_INTERFACE_HEADER_NAME"] = paths.basename(target_info.swift_objc_header_path)
defines_without_equal_sign = ["$(inherited)"]
for d in target_info.swift_defines.to_list():
d = _exclude_swift_incompatible_define(d)
if d != None:
defines_without_equal_sign.append(d)
target_settings["SWIFT_ACTIVE_COMPILATION_CONDITIONS"] = " ".join(
["\"%s\"" % d for d in defines_without_equal_sign],
)
extra_clang_flags = ["-D%s" % d for d in target_info.cc_defines.to_list()]
if virtualize_frameworks:
extra_clang_flags += ["-Xcc %s" % d for d in _objc_copts_for_target(target_name, all_transitive_targets)]
target_settings["BAZEL_LLDB_SWIFT_EXTRA_CLANG_FLAGS"] = " ".join(extra_clang_flags)
target_settings["BAZEL_LLDB_INIT_FILE"] = lldbinit_file
if product_type == "application":
target_settings["INFOPLIST_FILE"] = "$BAZEL_STUBS_DIR/Info-stub.plist"
target_settings["PRODUCT_BUNDLE_IDENTIFIER"] = target_info.bundle_id
if product_type == "bundle.unit-test":
target_settings["SUPPORTS_MACCATALYST"] = False
target_dependencies = []
test_host_appname = getattr(target_info, "test_host_appname", None)
if test_host_appname:
target_dependencies.append({"target": test_host_appname})
target_settings["TEST_HOST"] = "$(BUILT_PRODUCTS_DIR)/{test_host_appname}.app/{test_host_appname}".format(test_host_appname = test_host_appname)
if target_info.targeted_device_family:
target_settings["TARGETED_DEVICE_FAMILY"] = target_info.targeted_device_family
pre_build_scripts = []
if len(ctx.attr.additional_prebuild_script) > 0:
pre_build_scripts.append({
"name": "Additional prebuild script",
"script": ctx.attr.additional_prebuild_script,
})
pre_build_scripts.append({
"name": "Build with bazel",
"script": _BUILD_WITH_BAZEL_SCRIPT,
})
xcodeproj_targets_by_name[target_name] = {
"sources": compiled_sources + compiled_non_arc_sources + asset_sources,
"type": product_type,
"platform": _PLATFORM_MAPPING[target_info.platform_type],
"deploymentTarget": target_info.minimum_os_version,
"settings": target_settings,
"dependencies": target_dependencies,
"preBuildScripts": pre_build_scripts,
}
# Skip a scheme generation if allowlist is not empty
# and current product type not in the list
allow_scheme_list = ctx.attr.generate_schemes_for_product_types
if len(allow_scheme_list) > 0 and product_type not in allow_scheme_list:
continue
scheme_action_details = {"targets": [target_name]}
env_vars_dict = {}
for (k, v) in getattr(target_info, "env_vars", ()):
# Specific scheme can override the ones defined under env_vars here:
if ctx.attr.scheme_existing_envvar_overrides.get(k, None):
env_vars_dict[k] = ctx.attr.scheme_existing_envvar_overrides[k]
else:
env_vars_dict[k] = v
scheme_action_details["environmentVariables"] = env_vars_dict
commandline_args_tuple = getattr(target_info, "commandline_args", ())
scheme_action_details["commandLineArguments"] = {}
for arg in commandline_args_tuple:
scheme_action_details["commandLineArguments"][arg] = True
# From https://developer.apple.com/documentation/xcode-release-notes/xcode-12-release-notes
#
# You can now specify a path for the LLDB init file to use in a Run and Test action.
# Configure this path in the Info tab of a scheme’s Run or Test action.
# The path can contain a build settings macro such as ${SRCROOT}, so the file can be part of a project. (38677796) (FB5425738)
scheme_action_details["customLLDBInit"] = lldbinit_file
# See https://github.com/yonaskolb/XcodeGen/blob/master/Docs/ProjectSpec.md#scheme
# on structure of xcodeproj_schemes_by_name[target_info.name]
xcodeproj_schemes_by_name[target_name] = {
"build": {
"parallelizeBuild": False,
"buildImplicitDependencies": False,
"targets": {
target_name: ["run", "test", "profile"],
},
},
# By putting under run action, test action will just use them automatically
"run": scheme_action_details,
}
scheme_infos = [target[AdditionalSchemeInfo] for target in ctx.attr.additional_scheme_infos]
build_target_to_scheme_info = {scheme_info.build_target: scheme_info for scheme_info in scheme_infos}
# They will show as `TestableReference` under the scheme
if target_info.product_type == "bundle.unit-test":
# All test targets will by default have a test action for themselves.
xcodeproj_schemes_by_name[target_name]["test"] = {
"targets": [target_name],
"customLLDBInit": lldbinit_file,
}
elif target_name in build_target_to_scheme_info:
# Add additional scheme information provided by any provided scheme infos.
scheme_info_for_target = build_target_to_scheme_info[target_name]
xcodeproj_schemes_by_name[target_name]["test"] = {
"targets": scheme_info_for_target.test_action_targets,
"customLLDBInit": lldbinit_file,
"environmentVariables": [
{
"variable": env_var_name,
"value": env_var_value,
"isEnabled": True,
}
for target in scheme_info_for_target.test_action_targets
for env_var_name, env_var_value in target.environment_variables.items()
],
}
pre_actions_attr = ctx.attr.additional_pre_actions
_add_pre_post_actions(target_name, xcodeproj_schemes_by_name[target_name], "preActions", pre_actions_attr)
post_actions_attr = ctx.attr.additional_post_actions
_add_pre_post_actions(target_name, xcodeproj_schemes_by_name[target_name], "postActions", post_actions_attr)
return (xcodeproj_targets_by_name, xcodeproj_schemes_by_name)
def _add_pre_post_actions(target_name, scheme, key, actions):
"""Helper method to populate passed in scheme with pre/post actions. Note their output won't show up in build log.
Args:
target_name: Target the scheme is aiming at
scheme: target scheme to update for
key: one of preActions or postActions
actions: original attribute passed in from ctx.attr.additional_pre_actions or ctx.attr.additional_post_actions
"""
supported_keys = ["preActions", "postActions"]
if key not in supported_keys:
fail("Key must be one of %s" % supported_keys)
for action_type in actions:
if action_type not in scheme:
break
actions_to_add = actions[action_type]
payload = scheme[action_type]
list = []
# Note `settingsTarget` is explicitly omitted because
# for a target with lots of env vars we might exceed Xcode arg_max limit
for action in actions_to_add:
list.append({"script": action})
payload[key] = list
def _validate_output_path(output_path):
output_path_components = output_path.split("/")
prohibited_components = [".", ".."]
contains_dot_paths = any([elem in output_path_components for elem in prohibited_components])
if contains_dot_paths:
fail("The output_path provided cant contain the following path components: <%s>" % prohibited_components)
def _xcodeproj_impl(ctx):
_validate_output_path(ctx.attr.output_path)
xcodegen_jsonfile = ctx.actions.declare_file(
paths.join(ctx.attr.output_path, "%s-xcodegen.json" % ctx.attr.name),
)
project_name = (ctx.attr.project_name or ctx.attr.name) + ".xcodeproj"
if "/" in project_name:
fail("No / allowed in project_name")
project = ctx.actions.declare_directory(paths.join(ctx.attr.output_path, project_name))
destination_path = paths.normalize(paths.join(ctx.label.package, ctx.attr.output_path))
nesting = destination_path.count("/") + 1 if destination_path != "." else 0
src_dot_dots = "/".join([".."] * (nesting + 3))
script_dot_dots = "/".join([".."] * nesting)
proj_options = {
"createIntermediateGroups": True,
"defaultConfig": "Debug",
"groupSortPosition": "none",
"settingPresets": "none",
}
proj_settings_base = {}
# User defined macro for Bazel only
proj_settings_base.update({
"BAZEL_BUILD_EXEC": "$BAZEL_STUBS_DIR/build-wrapper",
"BAZEL_OUTPUT_PROCESSOR": "$BAZEL_STUBS_DIR/output-processor.rb",
"BAZEL_PATH": ctx.attr.bazel_path,
"BAZEL_WORKSPACE_ROOT": "$SRCROOT/%s" % script_dot_dots,
"BAZEL_STUBS_DIR": "$PROJECT_FILE_PATH/bazelstubs",
"BAZEL_INSTALLERS_DIR": "$PROJECT_FILE_PATH/bazelinstallers",
"BAZEL_INSTALLER": "$BAZEL_INSTALLERS_DIR/%s" % ctx.executable.installer.basename,
"BAZEL_EXECUTION_LOG_ENABLED": ctx.attr.bazel_execution_log_enabled,
"BAZEL_PROFILE_ENABLED": ctx.attr.bazel_profile_enabled,
"BAZEL_CONFIGS": ctx.attr.configs,
"BAZEL_ADDITIONAL_BAZEL_BUILD_OPTIONS": " ".join(["{} ".format(opt) for opt in ctx.attr.additional_bazel_build_options]),
"BAZEL_ADDITIONAL_LLDB_SETTINGS": "\n".join(ctx.attr.additional_lldb_settings),
})
# Stubbing compiler, linker executables used by xcode so no actual building happening on Xcode side
proj_settings_base.update({
"CC": "$BAZEL_STUBS_DIR/clang-stub",
"CXX": "$CC",
"CLANG_ANALYZER_EXEC": "$CC",
"LD": "$BAZEL_STUBS_DIR/ld-stub",
"LIBTOOL": "/usr/bin/true",
"SWIFT_EXEC": "$BAZEL_STUBS_DIR/swiftc-stub",
# LD isn't used for all use cases - direct it to use this LD
"OTHER_LDFLAGS": "-fuse-ld=$BAZEL_STUBS_DIR/ld-stub",
})
# Change of settings to help params used for compiling individual files to match closer to Bazel
proj_settings_base.update({
"USE_HEADERMAP": False,
})
# Other misc. settings changes
proj_settings_base.update({
"CODE_SIGNING_ALLOWED": False,
"DEBUG_INFORMATION_FORMAT": "dwarf",
"DONT_RUN_SWIFT_STDLIB_TOOL": True,
"SWIFT_OBJC_INTERFACE_HEADER_NAME": "",
"SWIFT_VERSION": 5,
})
# For debugging config only:
proj_settings_debug = {
"GCC_PREPROCESSOR_DEFINITIONS": "DEBUG",
"SWIFT_ACTIVE_COMPILATION_CONDITIONS": "DEBUG",
}
proj_settings = {
"base": proj_settings_base,
"configs": {
"Debug": proj_settings_debug,
},
}
targets = []
all_transitive_targets = depset(transitive = _get_attr_values_for_name(ctx.attr.deps, _TargetInfo, "targets")).to_list()
if ctx.attr.include_transitive_targets:
targets = all_transitive_targets
else:
targets = []
for t in _get_attr_values_for_name(ctx.attr.deps, _TargetInfo, "direct_targets"):
targets.extend(t)
(xcodeproj_targets_by_name, xcodeproj_schemes_by_name) = _populate_xcodeproj_targets_and_schemes(ctx, targets, src_dot_dots, all_transitive_targets)
project_file_groups = [
{"path": paths.join(src_dot_dots, f.short_path), "optional": True}
for f in ctx.files.additional_files
if _is_current_project_file(f)
]
# The 'xcodegen' tool requires at least one build configuration
# of each type 'debug' and 'release'. Add those and set all the others to 'none'
#
# Note that the consumer can still set 'Debug' and 'Release' in 'ctx.attr.configs'
# and take advantage of the configs in the .bazelrc file.
xcodeproj_info_configs = {k: "none" for k in ctx.attr.configs}
xcodeproj_info_configs["Debug"] = "debug"
xcodeproj_info_configs["Release"] = "release"
xcodeproj_info = struct(
name = paths.split_extension(project_name)[0],
attributes = ctx.attr.project_attributes_overrides,
options = proj_options,
settings = proj_settings,
configs = xcodeproj_info_configs,
targets = xcodeproj_targets_by_name,
schemes = xcodeproj_schemes_by_name,
fileGroups = project_file_groups,
)
ctx.actions.write(xcodegen_jsonfile, xcodeproj_info.to_json())
ctx.actions.run(
executable = ctx.executable._xcodegen,
arguments = ["--quiet", "--no-env", "--spec", xcodegen_jsonfile.path, "--project", project.dirname],
inputs = [xcodegen_jsonfile],
outputs = [project],
)
install_script = ctx.actions.declare_file(
"%s-install-xcodeproj.sh" % ctx.attr.name,
)
installer_runfile_paths = [i.short_path for i in ctx.attr.installer[DefaultInfo].default_runfiles.files.to_list()]
build_wrapper_runfile_paths = [i.short_path for i in ctx.attr.build_wrapper[DefaultInfo].default_runfiles.files.to_list()]
# In order to be runnable, the print_json_leaf_nodes script needs to live
# next to a print_json_leaf_nodes.runfiles directory that contains its runfiles.
# The print_json_leaf_nodes_runfiles array will be populated with the subdirectories
# and paths that the py_binary expects the runfiles directory to contain.
print_json_leaf_nodes_runfiles = []
for pi in ctx.attr.print_json_leaf_nodes[DefaultInfo].default_runfiles.files.to_list():
if pi.short_path.startswith("../"):
runfiles_subdirectory_path = pi.short_path[3:]
print_json_leaf_nodes_runfiles.append(runfiles_subdirectory_path)
else:
runfiles_subdirectory_path = "%s/%s" % (ctx.workspace_name, pi.short_path)
print_json_leaf_nodes_runfiles.append(runfiles_subdirectory_path)