forked from bazelbuild/rules_swift
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompiling.bzl
More file actions
2266 lines (2020 loc) · 87.1 KB
/
Copy pathcompiling.bzl
File metadata and controls
2266 lines (2020 loc) · 87.1 KB
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:collections.bzl", "collections")
load("@bazel_skylib//lib:partial.bzl", "partial")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//lib:types.bzl", "types")
load(
":actions.bzl",
"is_action_enabled",
"run_toolchain_action",
"swift_action_names",
)
load(":autolinking.bzl", "register_autolink_extract_action")
load(":debugging.bzl", "ensure_swiftmodule_is_embedded")
load(":derived_files.bzl", "derived_files")
load(
":feature_names.bzl",
"SWIFT_FEATURE_CACHEABLE_SWIFTMODULES",
"SWIFT_FEATURE_COMPILE_STATS",
"SWIFT_FEATURE_COVERAGE",
"SWIFT_FEATURE_DBG",
"SWIFT_FEATURE_DEBUG_PREFIX_MAP",
"SWIFT_FEATURE_EMIT_C_MODULE",
"SWIFT_FEATURE_EMIT_SWIFTINTERFACE",
"SWIFT_FEATURE_ENABLE_BATCH_MODE",
"SWIFT_FEATURE_ENABLE_LIBRARY_EVOLUTION",
"SWIFT_FEATURE_ENABLE_TESTING",
"SWIFT_FEATURE_FASTBUILD",
"SWIFT_FEATURE_FULL_DEBUG_INFO",
"SWIFT_FEATURE_IMPLICIT_MODULES",
"SWIFT_FEATURE_INDEX_WHILE_BUILDING",
"SWIFT_FEATURE_MINIMAL_DEPS",
"SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD",
"SWIFT_FEATURE_NO_GENERATED_HEADER",
"SWIFT_FEATURE_NO_GENERATED_MODULE_MAP",
"SWIFT_FEATURE_OPT",
"SWIFT_FEATURE_OPT_USES_OSIZE",
"SWIFT_FEATURE_OPT_USES_WMO",
"SWIFT_FEATURE_SUPPORTS_LIBRARY_EVOLUTION",
"SWIFT_FEATURE_USE_C_MODULES",
"SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE",
"SWIFT_FEATURE_VFSOVERLAY",
"SWIFT_FEATURE_SPLIT_DERIVED_FILES_GENERATION",
"SWIFT_FEATURE_EMIT_BC",
)
load(":features.bzl", "are_all_features_enabled", "is_feature_enabled")
load(":providers.bzl", "SwiftInfo", "create_swift_info")
load(":toolchain_config.bzl", "swift_toolchain_config")
load(
":utils.bzl",
"collect_cc_libraries",
"compact",
"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
def compile_action_configs():
"""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.
Returns:
The list of action configs needed to perform compilation.
"""
#### Flags that control compilation outputs
action_configs = [
# Emit object file(s).
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
not_features = [SWIFT_FEATURE_EMIT_BC],
configurators = [
swift_toolchain_config.add_arg("-emit-object"),
],
),
# Emit bitcode file
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
features = [SWIFT_FEATURE_EMIT_BC],
configurators = [
swift_toolchain_config.add_arg("-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.DERIVED_FILES],
configurators = [_output_swiftmodule_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",
),
],
),
# 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.DERIVED_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.DERIVED_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],
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_NO_GENERATED_HEADER],
[SWIFT_FEATURE_SPLIT_DERIVED_FILES_GENERATION],
],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.DERIVED_FILES],
configurators = [_emit_objc_header_path_configurator],
not_features = [SWIFT_FEATURE_NO_GENERATED_HEADER],
),
# Configure the location where compiler performance statistics are
# dumped.
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [_stats_output_dir_configurator],
features = [SWIFT_FEATURE_COMPILE_STATS],
),
]
#### 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.DERIVED_FILES
],
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.DERIVED_FILES
],
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],
configurators = [
swift_toolchain_config.add_arg("-Onone"),
],
features = [[SWIFT_FEATURE_DBG], [SWIFT_FEATURE_FASTBUILD]],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
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],
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.DERIVED_FILES
],
configurators = [
swift_toolchain_config.add_arg("-whole-module-optimization"),
],
features = [SWIFT_FEATURE_OPT, SWIFT_FEATURE_OPT_USES_WMO],
),
# Enable or disable serialization of debugging options into
# swiftmodules.
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [
swift_toolchain_config.add_arg(
"-Xfrontend",
"-no-serialize-debugging-options",
),
],
features = [SWIFT_FEATURE_CACHEABLE_SWIFTMODULES],
not_features = [SWIFT_FEATURE_OPT],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
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.DERIVED_FILES
],
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],
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],
configurators = [
swift_toolchain_config.add_arg("-gline-tables-only"),
],
features = [SWIFT_FEATURE_FASTBUILD],
not_features = [SWIFT_FEATURE_FULL_DEBUG_INFO],
),
# Make paths written into debug info workspace-relative.
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
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],
],
),
]
#### 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],
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],
configurators = [
swift_toolchain_config.add_arg("-sanitize=address"),
],
features = ["asan"],
),
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [
swift_toolchain_config.add_arg("-sanitize=thread"),
],
features = ["tsan"],
),
]
#### 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.DERIVED_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
],
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.DERIVED_FILES
],
configurators = [_global_module_cache_configurator],
features = [
SWIFT_FEATURE_IMPLICIT_MODULES,
SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE,
],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVED_FILES
],
configurators = [
swift_toolchain_config.add_arg(
"-Xwrapped-swift=-ephemeral-module-cache",
),
],
features = [SWIFT_FEATURE_IMPLICIT_MODULES],
not_features = [SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE],
),
]
#### Search paths for Swift module dependencies
action_configs.extend([
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVED_FILES,
],
configurators = [_dependencies_swiftmodules_configurator],
not_features = [SWIFT_FEATURE_VFSOVERLAY],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVED_FILES,
],
configurators = [
_dependencies_swiftmodules_vfsoverlay_configurator,
],
features = [SWIFT_FEATURE_VFSOVERLAY],
),
])
#### Search paths for framework dependencies
action_configs.append(
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVED_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
],
configurators = [_framework_search_paths_configurator],
),
)
#### 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.DERIVED_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
],
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.DERIVED_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
],
configurators = [_dependencies_clang_modules_configurator],
features = [SWIFT_FEATURE_USE_C_MODULES],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVED_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
],
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.DERIVED_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.DERIVED_FILES,
],
configurators = [_batch_mode_configurator],
features = [SWIFT_FEATURE_ENABLE_BATCH_MODE],
not_features = [SWIFT_FEATURE_OPT, SWIFT_FEATURE_OPT_USES_WMO],
),
# Set the number of threads to use for WMO.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVED_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_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVED_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],
),
# Set the module name.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVED_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
],
configurators = [_module_name_configurator],
),
# Configure index-while-building.
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [_index_while_building_configurator],
features = [SWIFT_FEATURE_INDEX_WHILE_BUILDING],
),
# User-defined conditional compilation flags (defined for Swift; those
# passed directly to ClangImporter are handled above).
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVED_FILES,
],
configurators = [_conditional_compilation_flag_configurator],
),
# Disable auto-linking for prebuilt static frameworks.
swift_toolchain_config.action_config(
actions = [swift_action_names.COMPILE],
configurators = [_static_frameworks_disable_autolink_configurator],
),
]
# NOTE: The position of this action config in the list is important, because
# it places user compile flags after flags added by the rules, allowing
# `copts` attributes and `--swiftcopt` flag values to override flags set by
# the rule implementations as a last resort.
action_configs.append(
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVED_FILES,
],
configurators = [_user_compile_flags_configurator],
),
)
action_configs.append(
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVED_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
],
configurators = [_source_files_configurator],
),
)
# Add additional input files to the sandbox (does not modify flags).
action_configs.append(
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVED_FILES,
],
configurators = [_additional_inputs_configurator],
),
)
return action_configs
def _output_or_file_map(output_file_map, outputs, args):
"""Adds the output file map or single file to the command line."""
if output_file_map:
args.add("-output-file-map", output_file_map)
return swift_toolchain_config.config_result(
inputs = [output_file_map],
)
if len(outputs) != 1:
fail(
"Internal error: If not using an output file map, there should " +
"only be a single object file expected as the output, but we " +
"found: {}".format(outputs),
)
args.add("-o", outputs[0])
return None
def _output_object_or_file_map_configurator(prerequisites, args):
"""Adds the output file map or single object file to the command line."""
return _output_or_file_map(
output_file_map = prerequisites.output_file_map,
outputs = prerequisites.object_files,
args = args,
)
def _output_swiftmodule_or_file_map_configurator(prerequisites, args):
"""Adds the output file map or single object file to the command line."""
return _output_or_file_map(
output_file_map = prerequisites.output_file_map,
outputs = [prerequisites.swiftmodule_file],
args = args,
)
def _output_pcm_file_configurator(prerequisites, args):
"""Adds the `.pcm` output path to the command line."""
args.add("-o", prerequisites.pcm_file)
def _emit_module_path_configurator(prerequisites, args):
"""Adds the `.swiftmodule` output path to the command line."""
args.add("-emit-module-path", prerequisites.swiftmodule_file)
def _emit_module_interface_path_configurator(prerequisites, args):
"""Adds the `.swiftinterface` output path to the command line."""
args.add("-emit-module-interface-path", prerequisites.swiftinterface_file)
def _emit_objc_header_path_configurator(prerequisites, args):
"""Adds the generated header output path to the command line."""
args.add("-emit-objc-header-path", prerequisites.generated_header_file)
def _global_module_cache_configurator(prerequisites, args):
"""Adds flags to enable the global module cache."""
# If bin_dir is not provided, then we don't pass any special flags to
# the compiler, letting it decide where the cache should live. This is
# usually somewhere in the system temporary directory.
if prerequisites.bin_dir:
args.add(
"-module-cache-path",
paths.join(prerequisites.bin_dir.path, "_swift_module_cache"),
)
def _batch_mode_configurator(prerequisites, args):
"""Adds flags to enable batch compilation mode."""
if not _is_wmo_manually_requested(prerequisites.user_compile_flags):
args.add("-enable-batch-mode")
def _clang_search_paths_configurator(prerequisites, args):
"""Adds Clang search paths to the command line."""
args.add_all(
depset(transitive = [
prerequisites.cc_info.compilation_context.includes,
# TODO(b/146575101): Replace with `objc_info.include` once this bug
# is fixed. See `_merge_target_providers` below for more details.
prerequisites.objc_include_paths_workaround,
]),
before_each = "-Xcc",
format_each = "-I%s",
)
# Add Clang search paths for the workspace root and Bazel output roots. The
# first allows ClangImporter to find headers included using
# workspace-relative paths when they are referenced from within other
# headers. The latter allows ClangImporter to find generated headers in
# `bazel-{bin,genfiles}` even when included using their workspace-relative
# path, matching the behavior used when compiling C/C++/Objective-C.
#
# Note that when `--incompatible_merge_genfiles_directory` is specified,
# `bin_dir` and `genfiles_dir` will have the same path; the depset will
# ensure that the `-iquote` flags are deduped.
direct_quote_includes = ["."]
if prerequisites.bin_dir:
direct_quote_includes.append(prerequisites.bin_dir.path)
if prerequisites.genfiles_dir:
direct_quote_includes.append(prerequisites.genfiles_dir.path)
args.add_all(
depset(
direct_quote_includes,
transitive = [
prerequisites.cc_info.compilation_context.quote_includes,
],
),
before_each = "-Xcc",
format_each = "-iquote%s",
)
args.add_all(
prerequisites.cc_info.compilation_context.system_includes,
map_each = _filter_out_unsupported_include_paths,
before_each = "-Xcc",
format_each = "-isystem%s",
)
def _dependencies_clang_defines_configurator(prerequisites, args):
"""Adds C/C++ dependencies' preprocessor defines to the command line."""
all_clang_defines = depset(transitive = [
prerequisites.cc_info.compilation_context.defines,
])
args.add_all(all_clang_defines, before_each = "-Xcc", format_each = "-D%s")
def _collect_clang_module_inputs(
cc_info,
is_swift,
modules,
objc_info,
prefer_precompiled_modules):
"""Collects Clang module-related inputs to pass to an action.
Args:
cc_info: The `CcInfo` provider of the target being compiled. The direct
headers of this provider will be collected as inputs.
is_swift: If True, this is a Swift compilation; otherwise, it is a
Clang module compilation.
modules: A list of module structures (as returned by
`swift_common.create_module`). The precompiled Clang modules or the
textual module maps and headers of these modules (depending on the
value of `prefer_precompiled_modules`) will be collected as inputs.
objc_info: The `apple_common.Objc` provider of the target being
compiled.
prefer_precompiled_modules: If True, precompiled module artifacts should
be preferred over textual module map files and headers for modules
that have them. If False, textual module map files and headers
should always be used.
Returns:
A toolchain configuration result (i.e.,
`swift_toolchain_config.config_result`) that contains the input
artifacts for the action.
"""
module_inputs = []
header_depsets = []
# Swift compiles (not Clang module compiles) that prefer precompiled modules
# do not need the full set of transitive headers.
if cc_info and not (is_swift and prefer_precompiled_modules):
header_depsets.append(cc_info.compilation_context.headers)
for module in modules:
clang_module = module.clang
module_map = clang_module.module_map
if prefer_precompiled_modules:
# If the build prefers precompiled modules, use the .pcm if it
# exists; otherwise, use the textual module map and the headers for
# that module (because we only want to propagate the headers that
# are required, not the full transitive set).
precompiled_module = clang_module.precompiled_module
if precompiled_module:
module_inputs.append(precompiled_module)
else:
module_inputs.append(clang_module.module_map)
module_inputs.extend(
clang_module.compilation_context.direct_headers,
)
module_inputs.extend(
clang_module.compilation_context.direct_textual_headers,
)
else:
# If the build prefers textual module maps and headers, just get the
# module map for each module; we've already collected the full
# transitive header set below.
module_inputs.append(module_map)
# If we prefer textual module maps and headers for the build, fall back to
# using the full set of transitive headers.
if not prefer_precompiled_modules:
if objc_info:
header_depsets.append(objc_info.umbrella_header)
return swift_toolchain_config.config_result(
inputs = module_inputs,
transitive_inputs = header_depsets,
)
def _clang_modulemap_dependency_args(module):
"""Returns `swiftc` arguments for the module map of a Clang module.
Args:
module: A struct containing information about the module, as defined by
`swift_common.create_module`.
Returns:
A list of arguments to pass to `swiftc`.
"""
return [
"-Xcc",
"-fmodule-map-file={}".format(module.clang.module_map.path),
]
def _clang_module_dependency_args(module):
"""Returns `swiftc` arguments for a precompiled Clang module, if possible.
If no precompiled module was emitted for this module, then this function
falls back to the textual module map.
Args:
module: A struct containing information about the module, as defined by
`swift_common.create_module`.
Returns:
A list of arguments to pass to `swiftc`.
"""
if not module.clang.precompiled_module:
return _clang_modulemap_dependency_args(module)
return [
"-Xcc",
"-fmodule-file={}".format(module.clang.precompiled_module.path),
]
def _dependencies_clang_modulemaps_configurator(prerequisites, args):
"""Configures Clang module maps from dependencies."""
modules = [
module
for module in prerequisites.transitive_modules
if module.clang
]
args.add_all(modules, map_each = _clang_modulemap_dependency_args)
return _collect_clang_module_inputs(
cc_info = prerequisites.cc_info,
is_swift = prerequisites.is_swift,
modules = modules,
objc_info = prerequisites.objc_info,
prefer_precompiled_modules = False,
)
def _dependencies_clang_modules_configurator(prerequisites, args):
"""Configures precompiled Clang modules from dependencies."""
modules = [
module
for module in prerequisites.transitive_modules
if module.clang
]
args.add_all(modules, map_each = _clang_module_dependency_args)
return _collect_clang_module_inputs(
cc_info = prerequisites.cc_info,
is_swift = prerequisites.is_swift,
modules = modules,
objc_info = prerequisites.objc_info,
prefer_precompiled_modules = True,
)
def _framework_search_paths_configurator(prerequisites, args):
"""Add search paths for prebuilt frameworks to the command line."""
args.add_all(
prerequisites.cc_info.compilation_context.framework_includes,
format_each = "-F%s",
)
def _static_frameworks_disable_autolink_configurator(prerequisites, args):
"""Add flags to disable auto-linking for static prebuilt frameworks.
This disables the `LC_LINKER_OPTION` load commands for auto-linking when
importing a static framework. This is needed to correctly deduplicate static
frameworks from being linked into test binaries when it is also linked into
the application binary.
"""
# TODO(b/143301479): This can be removed if we can disable auto-linking
# universally in the linker invocation. For Clang, we already pass
# `-fno-autolink`, but Swift doesn't have a similar option (to stop emitting
# `LC_LINKER_OPTION` load commands unconditionally). However, ld64 has an
# undocumented `-ignore_auto_link` flag that we could use. In either case,
# though, this would likely also disable auto-linking for system frameworks,
# so we would need to model those as explicit dependencies first.
args.add_all(
prerequisites.objc_info.static_framework_names,
map_each = _disable_autolink_framework_copts,
)
def _dependencies_swiftmodules_configurator(prerequisites, args):
"""Adds `.swiftmodule` files from deps to search paths and action inputs."""
args.add_all(
prerequisites.transitive_modules,
format_each = "-I%s",
map_each = _swift_module_search_path_map_fn,
uniquify = True,
)
return swift_toolchain_config.config_result(
inputs = prerequisites.transitive_swiftmodules,
)
def _dependencies_swiftmodules_vfsoverlay_configurator(prerequisites, args):
"""Provides a single `.swiftmodule` search path using a VFS overlay."""
swiftmodules = prerequisites.transitive_swiftmodules
# Bug: `swiftc` doesn't pass its `-vfsoverlay` arg to the frontend.
# Workaround: Pass `-vfsoverlay` directly via `-Xfrontend`.
args.add(
"-Xfrontend",
"-vfsoverlay{}".format(prerequisites.vfsoverlay_file.path),
)
args.add("-I{}".format(prerequisites.vfsoverlay_search_path))
return swift_toolchain_config.config_result(
inputs = swiftmodules + [prerequisites.vfsoverlay_file],
)
def _module_name_configurator(prerequisites, args):
"""Adds the module name flag to the command line."""
args.add("-module-name", prerequisites.module_name)
def _stats_output_dir_configurator(prerequisites, args):
"""Adds the compile stats output directory path to the command line."""
args.add("-stats-output-dir", prerequisites.stats_directory.path)
def _source_files_configurator(prerequisites, args):
"""Adds source files to the command line and required inputs."""
args.add_all(prerequisites.source_files)
return swift_toolchain_config.config_result(
inputs = prerequisites.source_files,
)
def _user_compile_flags_configurator(prerequisites, args):
"""Adds user compile flags to the command line."""
args.add_all(prerequisites.user_compile_flags)
def _wmo_thread_count_configurator(should_check_flags, prerequisites, args):
"""Adds thread count flags for WMO compiles to the command line.
Args:
should_check_flags: If `True`, WMO wasn't enabled by a feature so the
user compile flags should be checked for an explicit WMO option.
This argument is pre-bound when the partial is created for the
action config. If `False`, unconditionally apply the flags, because
it is assumed that the configurator was triggered by feature
satisfaction.
prerequisites: The action prerequisites.
args: The `Args` object to which flags will be added.
"""
if not should_check_flags or (
should_check_flags and
_is_wmo_manually_requested(prerequisites.user_compile_flags)
):
# Force threaded mode for WMO builds.
args.add("-num-threads", str(_DEFAULT_WMO_THREAD_COUNT))
def _is_wmo_manually_requested(user_compile_flags):
"""Returns `True` if a WMO flag is in the given list of compiler flags.
Args:
user_compile_flags: A list of compiler flags to scan for WMO usage.
Returns:
True if WMO is enabled in the given list of flags.
"""