-
Notifications
You must be signed in to change notification settings - Fork 138
/
compiling.bzl
2999 lines (2724 loc) · 117 KB
/
compiling.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Implementation of compilation logic for Swift."""
load("@bazel_skylib//lib:partial.bzl", "partial")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//lib:sets.bzl", "sets")
load("@bazel_skylib//lib:types.bzl", "types")
load(
":actions.bzl",
"is_action_enabled",
"run_toolchain_action",
"swift_action_names",
)
load(":debugging.bzl", "should_embed_swiftmodule_for_debugging")
load(":derived_files.bzl", "derived_files")
load(
":feature_names.bzl",
"SWIFT_FEATURE_BITCODE_EMBEDDED",
"SWIFT_FEATURE_CACHEABLE_SWIFTMODULES",
"SWIFT_FEATURE_CODEVIEW_DEBUG_INFO",
"SWIFT_FEATURE_COVERAGE",
"SWIFT_FEATURE_COVERAGE_PREFIX_MAP",
"SWIFT_FEATURE_DBG",
"SWIFT_FEATURE_DEBUG_PREFIX_MAP",
"SWIFT_FEATURE_DISABLE_SYSTEM_INDEX",
"SWIFT_FEATURE_EMIT_BC",
"SWIFT_FEATURE_EMIT_C_MODULE",
"SWIFT_FEATURE_EMIT_SWIFTINTERFACE",
"SWIFT_FEATURE_ENABLE_BATCH_MODE",
"SWIFT_FEATURE_ENABLE_LIBRARY_EVOLUTION",
"SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES",
"SWIFT_FEATURE_ENABLE_TESTING",
"SWIFT_FEATURE_FASTBUILD",
"SWIFT_FEATURE_FULL_DEBUG_INFO",
"SWIFT_FEATURE_GLOBAL_MODULE_CACHE_USES_TMPDIR",
"SWIFT_FEATURE_INDEX_WHILE_BUILDING",
"SWIFT_FEATURE_LAYERING_CHECK",
"SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD",
"SWIFT_FEATURE_NO_ASAN_VERSION_CHECK",
"SWIFT_FEATURE_NO_GENERATED_MODULE_MAP",
"SWIFT_FEATURE_OPT",
"SWIFT_FEATURE_OPT_USES_OSIZE",
"SWIFT_FEATURE_OPT_USES_WMO",
"SWIFT_FEATURE_REWRITE_GENERATED_HEADER",
"SWIFT_FEATURE_SPLIT_DERIVED_FILES_GENERATION",
"SWIFT_FEATURE_SUPPORTS_LIBRARY_EVOLUTION",
"SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG",
"SWIFT_FEATURE_SYSTEM_MODULE",
"SWIFT_FEATURE_USE_C_MODULES",
"SWIFT_FEATURE_USE_GLOBAL_INDEX_STORE",
"SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE",
"SWIFT_FEATURE_USE_OLD_DRIVER",
"SWIFT_FEATURE_USE_PCH_OUTPUT_DIR",
"SWIFT_FEATURE_VFSOVERLAY",
"SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS",
"SWIFT_FEATURE__WMO_IN_SWIFTCOPTS",
)
load(
":features.bzl",
"are_all_features_enabled",
"get_cc_feature_configuration",
"is_feature_enabled",
)
load(":module_maps.bzl", "write_module_map")
load(
":providers.bzl",
"SwiftInfo",
"create_clang_module",
"create_module",
"create_swift_info",
"create_swift_module",
)
load(":toolchain_config.bzl", "swift_toolchain_config")
load(
":utils.bzl",
"compact",
"compilation_context_for_explicit_module_compilation",
"get_providers",
"struct_fields",
)
load(":vfsoverlay.bzl", "write_vfsoverlay")
# VFS root where all .swiftmodule files will be placed when
# SWIFT_FEATURE_VFSOVERLAY is enabled.
_SWIFTMODULES_VFS_ROOT = "/__build_bazel_rules_swift/swiftmodules"
# The number of threads to use for WMO builds, using the same number of cores
# that is on a Mac Pro for historical reasons.
# TODO(b/32571265): Generalize this based on platform and core count
# when an API to obtain this is available.
_DEFAULT_WMO_THREAD_COUNT = 12
# Swift command line flags that enable whole module optimization. (This
# dictionary is used as a set for quick lookup; the values are irrelevant.)
_WMO_FLAGS = {
"-wmo": True,
"-whole-module-optimization": True,
"-force-single-frontend-invocation": True,
}
def compile_action_configs(
*,
os = None,
arch = None,
sdkroot = None,
xctest_version = None,
additional_objc_copts = [],
additional_swiftc_copts = [],
generated_header_rewriter = None):
"""Returns the list of action configs needed to perform Swift compilation.
Toolchains must add these to their own list of action configs so that
compilation actions will be correctly configured.
Args:
os: The OS that we are bulding for.
arch: The architecture that we are building for.
sdkroot: An optional path to the SDK to build against.
xctest_version: The version of XCTest to build against.
additional_objc_copts: An optional list of additional Objective-C
compiler flags that should be passed (preceded by `-Xcc`) to Swift
compile actions *and* Swift explicit module precompile actions after
any other toolchain- or user-provided flags.
additional_swiftc_copts: An optional list of additional Swift compiler
flags that should be passed to Swift compile actions only after any
other toolchain- or user-provided flags.
generated_header_rewriter: An executable that will be invoked after
compilation to rewrite the generated header, or None if this is not
desired.
Returns:
The list of action configs needed to perform compilation.
"""
#### Flags that control the driver
action_configs = [
# Use the legacy driver if requested.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
swift_action_names.DUMP_AST,
],
configurators = [
swift_toolchain_config.add_arg("-disallow-use-new-driver"),
],
features = [SWIFT_FEATURE_USE_OLD_DRIVER],
),
]
if sdkroot:
action_configs.append(
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
swift_action_names.DUMP_AST,
],
configurators = [
swift_toolchain_config.add_arg(
"-sdk",
sdkroot,
),
],
),
)
if os and xctest_version:
action_configs.append(
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
swift_action_names.DUMP_AST,
],
configurators = [
swift_toolchain_config.add_arg(
paths.join(sdkroot, "..", "..", "Library", "XCTest-{}".format(xctest_version), "usr", "lib", "swift", os),
format = "-I%s",
),
],
),
)
# Compatibility with older builds of the Swift SDKs
if arch:
action_configs.append(
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
swift_action_names.DUMP_AST,
],
configurators = [
swift_toolchain_config.add_arg(
paths.join(sdkroot, "..", "..", "Library", "XCTest-{}".format(xctest_version), "usr", "lib", "swift", os, arch),
format = "-I%s",
),
],
),
)
#### Flags that control compilation outputs
action_configs += [
# Emit object file(s).
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [
swift_toolchain_config.add_arg("-emit-object"),
],
not_features = [SWIFT_FEATURE_EMIT_BC],
),
# Emit llvm bc file(s).
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [
swift_toolchain_config.add_arg("-emit-bc"),
],
features = [SWIFT_FEATURE_EMIT_BC],
),
# Add the single object file or object file map, whichever is needed.
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [_output_object_or_file_map_configurator],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.DERIVE_FILES],
configurators = [_output_swiftmodule_or_file_map_configurator],
),
# Dump ast files
swift_toolchain_config.action_config(
actions = [swift_action_names.DUMP_AST],
configurators = [
swift_toolchain_config.add_arg("-dump-ast"),
swift_toolchain_config.add_arg("-suppress-warnings"),
],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.DUMP_AST],
configurators = [_output_ast_path_or_file_map_configurator],
),
# Emit precompiled Clang modules, and embed all files that were read
# during compilation into the PCM.
swift_toolchain_config.action_config(
actions = [swift_action_names.PRECOMPILE_C_MODULE],
configurators = [
swift_toolchain_config.add_arg("-emit-pcm"),
swift_toolchain_config.add_arg("-Xcc", "-Xclang"),
swift_toolchain_config.add_arg(
"-Xcc",
"-fmodules-embed-all-files",
),
],
),
# Don't embed Clang module breadcrumbs in debug info.
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [
swift_toolchain_config.add_arg(
"-Xfrontend",
"-no-clang-module-breadcrumbs",
),
],
),
# Add the output precompiled module file path to the command line.
swift_toolchain_config.action_config(
actions = [swift_action_names.PRECOMPILE_C_MODULE],
configurators = [_output_pcm_file_configurator],
),
# Configure the path to the emitted .swiftmodule file.
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [_emit_module_path_configurator],
not_features = [SWIFT_FEATURE_SPLIT_DERIVED_FILES_GENERATION],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.DERIVE_FILES],
configurators = [_emit_module_path_configurator],
),
# Configure library evolution and the path to the .swiftinterface file.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg("-enable-library-evolution"),
],
features = [
SWIFT_FEATURE_SUPPORTS_LIBRARY_EVOLUTION,
SWIFT_FEATURE_ENABLE_LIBRARY_EVOLUTION,
],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [_emit_module_interface_path_configurator],
features = [
SWIFT_FEATURE_SUPPORTS_LIBRARY_EVOLUTION,
SWIFT_FEATURE_EMIT_SWIFTINTERFACE,
],
),
# Configure the path to the emitted *-Swift.h file.
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [_emit_objc_header_path_configurator],
not_features = [
SWIFT_FEATURE_SPLIT_DERIVED_FILES_GENERATION,
],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.DERIVE_FILES],
configurators = [_emit_objc_header_path_configurator],
),
]
if generated_header_rewriter:
# Only add the generated header rewriter to the command line only if the
# toolchain provides one, the relevant feature is requested, and the
# particular compilation action is generating a header.
def generated_header_rewriter_configurator(prerequisites, args):
if prerequisites.generated_header_file:
args.add(
generated_header_rewriter,
format = "-Xwrapped-swift=-generated-header-rewriter=%s",
)
action_configs.append(
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [generated_header_rewriter_configurator],
features = [SWIFT_FEATURE_REWRITE_GENERATED_HEADER],
),
)
#### Compilation-mode-related flags
#
# These configs set flags based on the current compilation mode. They mirror
# the descriptions of these compilation modes given in the Bazel
# documentation:
# https://docs.bazel.build/versions/master/user-manual.html#flag--compilation_mode
action_configs += [
# Define appropriate conditional compilation symbols depending on the
# build mode.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.DUMP_AST,
],
configurators = [
swift_toolchain_config.add_arg("-DDEBUG"),
],
features = [[SWIFT_FEATURE_DBG], [SWIFT_FEATURE_FASTBUILD]],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.DUMP_AST,
],
configurators = [
swift_toolchain_config.add_arg("-DNDEBUG"),
],
features = [SWIFT_FEATURE_OPT],
),
# Set the optimization mode. For dbg/fastbuild, use `-O0`. For opt, use
# `-O` unless the `swift.opt_uses_osize` feature is enabled, then use
# `-Osize`.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg("-Onone"),
],
features = [[SWIFT_FEATURE_DBG], [SWIFT_FEATURE_FASTBUILD]],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg("-O"),
],
features = [SWIFT_FEATURE_OPT],
not_features = [SWIFT_FEATURE_OPT_USES_OSIZE],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg("-Osize"),
],
features = [SWIFT_FEATURE_OPT, SWIFT_FEATURE_OPT_USES_OSIZE],
),
# If the `swift.opt_uses_wmo` feature is enabled, opt builds should also
# automatically imply whole-module optimization.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg("-whole-module-optimization"),
],
features = [
[SWIFT_FEATURE_OPT, SWIFT_FEATURE_OPT_USES_WMO],
[SWIFT_FEATURE__WMO_IN_SWIFTCOPTS],
],
),
# Enable or disable serialization of debugging options into
# swiftmodules.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg(
"-Xfrontend",
"-no-serialize-debugging-options",
),
],
features = [SWIFT_FEATURE_CACHEABLE_SWIFTMODULES],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg(
"-Xfrontend",
"-serialize-debugging-options",
),
],
not_features = [
[SWIFT_FEATURE_OPT],
[SWIFT_FEATURE_CACHEABLE_SWIFTMODULES],
],
),
# Enable testability if requested.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.DUMP_AST,
],
configurators = [
swift_toolchain_config.add_arg("-enable-testing"),
],
features = [SWIFT_FEATURE_ENABLE_TESTING],
),
# Emit appropriate levels of debug info. On Apple platforms, requesting
# dSYMs (regardless of compilation mode) forces full debug info because
# `dsymutil` produces spurious warnings about symbols in the debug map
# when run on DI emitted by `-gline-tables-only`.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [swift_toolchain_config.add_arg("-g")],
features = [[SWIFT_FEATURE_DBG], [SWIFT_FEATURE_FULL_DEBUG_INFO]],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg("-g"),
swift_toolchain_config.add_arg("-debug-info-format=codeview"),
],
features = [
[SWIFT_FEATURE_DBG, SWIFT_FEATURE_CODEVIEW_DEBUG_INFO],
[SWIFT_FEATURE_FASTBUILD, SWIFT_FEATURE_CODEVIEW_DEBUG_INFO],
[SWIFT_FEATURE_FULL_DEBUG_INFO, SWIFT_FEATURE_CODEVIEW_DEBUG_INFO],
],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg("-gline-tables-only"),
],
features = [SWIFT_FEATURE_FASTBUILD],
not_features = [
[SWIFT_FEATURE_FULL_DEBUG_INFO],
[SWIFT_FEATURE_CODEVIEW_DEBUG_INFO],
],
),
# Make paths written into debug info workspace-relative.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
],
configurators = [
swift_toolchain_config.add_arg(
"-Xwrapped-swift=-debug-prefix-pwd-is-dot",
),
],
features = [
[SWIFT_FEATURE_DEBUG_PREFIX_MAP, SWIFT_FEATURE_DBG],
[SWIFT_FEATURE_DEBUG_PREFIX_MAP, SWIFT_FEATURE_FASTBUILD],
[SWIFT_FEATURE_DEBUG_PREFIX_MAP, SWIFT_FEATURE_FULL_DEBUG_INFO],
],
),
# Make paths written into coverage info workspace-relative.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg(
"-Xwrapped-swift=-coverage-prefix-pwd-is-dot",
),
],
features = [
[SWIFT_FEATURE_COVERAGE_PREFIX_MAP, SWIFT_FEATURE_COVERAGE],
],
),
]
#### Coverage and sanitizer instrumentation flags
#
# Note that for the sanitizer flags, we don't define Swift-specific ones;
# if the underlying C++ toolchain doesn't define them, we don't bother
# supporting them either.
action_configs += [
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg("-profile-generate"),
swift_toolchain_config.add_arg("-profile-coverage-mapping"),
],
features = [SWIFT_FEATURE_COVERAGE],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg("-sanitize=address"),
],
features = ["asan"],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg(
"-Xllvm",
"-asan-guard-against-version-mismatch=0",
),
],
features = [
"asan",
SWIFT_FEATURE_NO_ASAN_VERSION_CHECK,
],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [
swift_toolchain_config.add_arg("-sanitize=thread"),
],
features = ["tsan"],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [
swift_toolchain_config.add_arg("-sanitize=undefined"),
],
features = ["ubsan"],
),
]
#### Flags controlling how Swift/Clang modular inputs are processed
action_configs += [
# Treat paths in .modulemap files as workspace-relative, not modulemap-
# relative.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
swift_action_names.DUMP_AST,
],
configurators = [
swift_toolchain_config.add_arg("-Xcc", "-Xclang"),
swift_toolchain_config.add_arg(
"-Xcc",
"-fmodule-map-file-home-is-cwd",
),
],
features = [SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD],
),
# Configure how implicit modules are handled--either using the module
# cache, or disabled completely when using explicit modules.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.DUMP_AST,
],
configurators = [_global_module_cache_configurator],
features = [SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE],
not_features = [
[SWIFT_FEATURE_USE_C_MODULES],
[SWIFT_FEATURE_GLOBAL_MODULE_CACHE_USES_TMPDIR],
],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.DUMP_AST,
],
configurators = [_tmpdir_module_cache_configurator],
features = [
SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE,
SWIFT_FEATURE_GLOBAL_MODULE_CACHE_USES_TMPDIR,
],
not_features = [SWIFT_FEATURE_USE_C_MODULES],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.DUMP_AST,
],
configurators = [
swift_toolchain_config.add_arg(
"-Xwrapped-swift=-ephemeral-module-cache",
),
],
not_features = [
[SWIFT_FEATURE_USE_C_MODULES],
[SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE],
],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.DUMP_AST,
],
configurators = [_pch_output_dir_configurator],
features = [
SWIFT_FEATURE_USE_PCH_OUTPUT_DIR,
],
),
# When using C modules, disable the implicit search for module map files
# because all of them, including system dependencies, will be provided
# explicitly.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
swift_action_names.DUMP_AST,
],
configurators = [
swift_toolchain_config.add_arg(
"-Xcc",
"-fno-implicit-module-maps",
),
],
features = [SWIFT_FEATURE_USE_C_MODULES],
),
# Do not allow implicit modules to be used at all when emitting an
# explicit C/Objective-C module. Consider the case of two modules A and
# B, where A depends on B. If B does not emit an explicit module, then
# when A is compiled it would contain a hardcoded reference to B via its
# path in the implicit module cache. Thus, A would not be movable; some
# library importing A would try to resolve B at that path, which may no
# longer exist when the upstream library is built.
#
# This implies that for a C/Objective-C library to build as an explicit
# module, all of its dependencies must as well. On the other hand, a
# Swift library can be compiled with some of its Objective-C
# dependencies still using implicit modules, as long as no Objective-C
# library wants to import that Swift library's generated header and
# build itself as an explicit module.
swift_toolchain_config.action_config(
actions = [swift_action_names.PRECOMPILE_C_MODULE],
configurators = [
swift_toolchain_config.add_arg(
"-Xcc",
"-fno-implicit-modules",
),
],
features = [SWIFT_FEATURE_USE_C_MODULES],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.PRECOMPILE_C_MODULE],
configurators = [_c_layering_check_configurator],
features = [SWIFT_FEATURE_LAYERING_CHECK],
not_features = [SWIFT_FEATURE_SYSTEM_MODULE],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.PRECOMPILE_C_MODULE],
configurators = [
# Before Swift 5.4, ClangImporter doesn't currently handle the
# IsSystem bit correctly for the input file and ignores the
# `-fsystem-module` flag, which causes the module map to be
# treated as a user input. We can work around this by disabling
# diagnostics for system modules. However, this also disables
# behavior in ClangImporter that causes system APIs that use
# `UInt` to be imported to use `Int` instead. The only solution
# here is to use Xcode 12.5 or higher.
swift_toolchain_config.add_arg("-Xcc", "-w"),
swift_toolchain_config.add_arg(
"-Xcc",
"-Wno-nullability-declspec",
),
],
features = [SWIFT_FEATURE_SYSTEM_MODULE],
not_features = [SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.PRECOMPILE_C_MODULE],
configurators = [
# `-Xclang -emit-module` ought to be unnecessary if `-emit-pcm`
# is present because ClangImporter configures the invocation to
# use the `GenerateModule` action. However, it does so *after*
# creating the invocation by parsing the command line via a
# helper shared by `-emit-pcm` and other operations, so the
# changing of the action to `GenerateModule` occurs too late;
# the argument parser doesn't know that this will be the
# intended action and it emits a spurious diagnostic:
# "'-fsystem-module' only allowed with '-emit-module'". So, for
# system modules we'll pass `-emit-module` as well; it gets rid
# of the diagnostic and doesn't appear to cause other issues.
swift_toolchain_config.add_arg("-Xcc", "-Xclang"),
swift_toolchain_config.add_arg("-Xcc", "-emit-module"),
swift_toolchain_config.add_arg("-Xcc", "-Xclang"),
swift_toolchain_config.add_arg("-Xcc", "-fsystem-module"),
],
features = [
SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG,
SWIFT_FEATURE_SYSTEM_MODULE,
],
),
]
#### Search paths for Swift module dependencies
action_configs.extend([
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.DUMP_AST,
],
configurators = [_dependencies_swiftmodules_configurator],
not_features = [SWIFT_FEATURE_VFSOVERLAY],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.DUMP_AST,
],
configurators = [
_dependencies_swiftmodules_vfsoverlay_configurator,
],
features = [SWIFT_FEATURE_VFSOVERLAY],
),
])
#### Search paths for framework dependencies
action_configs.extend([
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.DUMP_AST,
],
configurators = [
lambda prereqs, args: _framework_search_paths_configurator(
prereqs,
args,
is_swift = True,
),
],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.PRECOMPILE_C_MODULE],
configurators = [
lambda prereqs, args: _framework_search_paths_configurator(
prereqs,
args,
is_swift = False,
),
],
),
])
#### Other ClangImporter flags
action_configs.extend([
# Pass flags to Clang for search paths and propagated defines.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
swift_action_names.DUMP_AST,
],
configurators = [
_clang_search_paths_configurator,
_dependencies_clang_defines_configurator,
],
),
# Pass flags to Clang for dependencies' module maps or explicit modules,
# whichever are being used for this build.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
swift_action_names.DUMP_AST,
],
configurators = [_dependencies_clang_modules_configurator],
features = [SWIFT_FEATURE_USE_C_MODULES],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
swift_action_names.DUMP_AST,
],
configurators = [_dependencies_clang_modulemaps_configurator],
not_features = [SWIFT_FEATURE_USE_C_MODULES],
),
])
#### Various other Swift compilation flags
action_configs += [
# Request color diagnostics, since Bazel pipes the output and causes the
# driver's TTY check to fail.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
],
configurators = [
swift_toolchain_config.add_arg(
"-Xfrontend",
"-color-diagnostics",
),
],
),
# Request batch mode if the compiler supports it. We only do this if the
# user hasn't requested WMO in some fashion, because otherwise an
# annoying warning message is emitted. At this level, we can disable the
# configurator if the `swift.opt` and `swift.opt_uses_wmo` features are
# both present. Inside the configurator, we also check the user compile
# flags themselves, since some Swift users enable it there as a build
# performance hack.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [_batch_mode_configurator],
features = [SWIFT_FEATURE_ENABLE_BATCH_MODE],
not_features = [
[SWIFT_FEATURE_OPT, SWIFT_FEATURE_OPT_USES_WMO],
[SWIFT_FEATURE__WMO_IN_SWIFTCOPTS],
],
),
# Set the number of threads to use for WMO. (We can skip this if we know
# we'll already be applying `-num-threads` via `--swiftcopt` flags.)
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
partial.make(
_wmo_thread_count_configurator,
# WMO is implied by features, so don't check the user
# compile flags.
False,
),
],
features = [
[SWIFT_FEATURE_OPT, SWIFT_FEATURE_OPT_USES_WMO],
[SWIFT_FEATURE__WMO_IN_SWIFTCOPTS],
],
not_features = [SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
partial.make(
_wmo_thread_count_configurator,
# WMO is not implied by features, so check the user compile
# flags in case they enabled it there.
True,
),
],
not_features = [
[SWIFT_FEATURE_OPT, SWIFT_FEATURE_OPT_USES_WMO],
[SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS],
],
),
# Set the module name.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
swift_action_names.DUMP_AST,
],
configurators = [_module_name_configurator],
),
# Pass extra flags for swiftmodule only compilations
swift_toolchain_config.action_config(
actions = [swift_action_names.DERIVE_FILES],
configurators = [
swift_toolchain_config.add_arg(
"-experimental-skip-non-inlinable-function-bodies",
),
],
features = [SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [_global_index_store_configurator],
features = [
SWIFT_FEATURE_INDEX_WHILE_BUILDING,
SWIFT_FEATURE_USE_GLOBAL_INDEX_STORE,
],
),