-
Notifications
You must be signed in to change notification settings - Fork 140
/
xcode_swift_toolchain.bzl
848 lines (775 loc) · 32.5 KB
/
xcode_swift_toolchain.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
# 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.
"""BUILD rules used to provide a Swift toolchain provided by Xcode on macOS.
The rules defined in this file are not intended to be used outside of the Swift
toolchain package. If you are looking for rules to build Swift code using this
toolchain, see `swift.bzl`.
"""
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_skylib//lib:partial.bzl", "partial")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load(":actions.bzl", "swift_action_names")
load(":attrs.bzl", "swift_toolchain_driver_attrs")
load(":compiling.bzl", "compile_action_configs", "features_from_swiftcopts")
load(
":feature_names.bzl",
"SWIFT_FEATURE_BUNDLED_XCTESTS",
"SWIFT_FEATURE_CACHEABLE_SWIFTMODULES",
"SWIFT_FEATURE_COVERAGE",
"SWIFT_FEATURE_COVERAGE_PREFIX_MAP",
"SWIFT_FEATURE_DEBUG_PREFIX_MAP",
"SWIFT_FEATURE_ENABLE_BATCH_MODE",
"SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES",
"SWIFT_FEATURE_FILE_PREFIX_MAP",
"SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD",
"SWIFT_FEATURE_OBJC_LINK_FLAGS",
"SWIFT_FEATURE_OPT_USES_WMO",
"SWIFT_FEATURE_REMAP_XCODE_PATH",
"SWIFT_FEATURE_SUPPORTS_BARE_SLASH_REGEX",
"SWIFT_FEATURE_SUPPORTS_LIBRARY_EVOLUTION",
"SWIFT_FEATURE_SUPPORTS_PRIVATE_DEPS",
"SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG",
"SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE",
"SWIFT_FEATURE_USE_RESPONSE_FILES",
"SWIFT_FEATURE__FORCE_ALWAYSLINK_TRUE",
"SWIFT_FEATURE__SUPPORTS_MACROS",
)
load(":features.bzl", "features_for_build_modes")
load(":toolchain_config.bzl", "swift_toolchain_config")
load(
":providers.bzl",
"SwiftFeatureAllowlistInfo",
"SwiftInfo",
"SwiftPackageConfigurationInfo",
"SwiftToolchainInfo",
)
load(":target_triples.bzl", "target_triples")
load(
":utils.bzl",
"collect_implicit_deps_providers",
"get_swift_executable_for_toolchain",
"resolve_optional_tool",
)
# TODO: Remove once we drop bazel 7.x
_OBJC_PROVIDER_LINKING = hasattr(apple_common.new_objc_provider(), "linkopt")
# Maps (operating system, environment) pairs from target triples to the legacy
# Bazel core `apple_common.platform` values, since we still use some APIs that
# require these.
_TRIPLE_OS_TO_PLATFORM = {
("ios", None): apple_common.platform.ios_device,
("ios", "simulator"): apple_common.platform.ios_simulator,
("macos", None): apple_common.platform.macos,
("tvos", None): apple_common.platform.tvos_device,
("tvos", "simulator"): apple_common.platform.tvos_simulator,
# TODO: Remove getattr use once we no longer support 6.x
("xros", None): getattr(apple_common.platform, "visionos_device", None),
("xros", "simulator"): getattr(apple_common.platform, "visionos_simulator", None),
("watchos", None): apple_common.platform.watchos_device,
("watchos", "simulator"): apple_common.platform.watchos_simulator,
}
def _bazel_apple_platform(target_triple):
"""Returns the `apple_common.platform` value for the given target triple."""
# TODO: Remove once we no longer support 6.x
if target_triples.unversioned_os(target_triple) == "xros" and not hasattr(
apple_common.platform,
"visionos_device",
):
fail("visionOS requested but your version of bazel doesn't support it")
return _TRIPLE_OS_TO_PLATFORM[(
target_triples.unversioned_os(target_triple),
target_triple.environment,
)]
def _command_line_objc_copts(compilation_mode, cpp_fragment, objc_fragment):
"""Returns copts that should be passed to `clang` from the `objc` fragment.
Args:
compilation_mode: The current compilation mode.
cpp_fragment: The `cpp` configuration fragment.
objc_fragment: The `objc` configuration fragment.
Returns:
A list of `clang` copts, each of which is preceded by `-Xcc` so that
they can be passed through `swiftc` to its underlying ClangImporter
instance.
"""
# In general, every compilation mode flag from native `objc_*` rules should
# be passed, but `-g` seems to break Clang module compilation. Since this
# flag does not make much sense for module compilation and only touches
# headers, it's ok to omit.
# TODO(b/153867054): These flags were originally being set by Bazel's legacy
# hardcoded Objective-C behavior, which has been migrated to crosstool. In
# the long term, we should query crosstool for the flags we're interested in
# and pass those to ClangImporter, and do this across all platforms. As an
# immediate short-term workaround, we preserve the old behavior by passing
# the exact set of flags that Bazel was originally passing if the list we
# get back from the configuration fragment is empty.
legacy_copts = objc_fragment.copts_for_current_compilation_mode
if not legacy_copts:
if compilation_mode == "dbg":
legacy_copts = [
"-O0",
"-DDEBUG=1",
"-fstack-protector",
"-fstack-protector-all",
]
elif compilation_mode == "opt":
legacy_copts = [
"-Os",
"-DNDEBUG=1",
"-Wno-unused-variable",
"-Winit-self",
"-Wno-extra",
]
if getattr(cpp_fragment, "objccopts", None) != None:
clang_copts = getattr(cpp_fragment, "objccopts") + legacy_copts
else:
clang_copts = getattr(objc_fragment, "copts", []) + legacy_copts
return [copt for copt in clang_copts if copt != "-g"]
def _platform_developer_framework_dir(
apple_toolchain,
target_triple):
"""Returns the Developer framework directory for the platform.
Args:
apple_toolchain: The `apple_common.apple_toolchain()` object.
target_triple: The triple of the platform being targeted.
Returns:
The path to the Developer framework directory for the platform if one
exists, otherwise `None`.
"""
return paths.join(
apple_toolchain.developer_dir(),
"Platforms",
"{}.platform".format(
_bazel_apple_platform(target_triple).name_in_plist,
),
"Developer/Library/Frameworks",
)
def _sdk_developer_framework_dir(apple_toolchain, target_triple):
"""Returns the Developer framework directory for the SDK.
Args:
apple_toolchain: The `apple_common.apple_toolchain()` object.
target_triple: The triple of the platform being targeted.
Returns:
The path to the Developer framework directory for the SDK if one
exists, otherwise `None`.
"""
# All platforms have a `Developer/Library/Frameworks` directory in their SDK
# root except for macOS (all versions of Xcode so far)
os = target_triples.unversioned_os(target_triple)
if os == "macos":
return None
return paths.join(apple_toolchain.sdk_dir(), "Developer/Library/Frameworks")
def _swift_linkopts_providers(
apple_toolchain,
target_triple,
toolchain_label,
toolchain_root):
"""Returns providers containing flags that should be passed to the linker.
The providers returned by this function will be used as implicit
dependencies of the toolchain to ensure that any binary containing Swift code
will link to the standard libraries correctly.
Args:
apple_toolchain: The `apple_common.apple_toolchain()` object.
target_triple: The target triple `struct`.
toolchain_label: The label of the Swift toolchain that will act as the
owner of the linker input propagating the flags.
toolchain_root: The path to a custom Swift toolchain that could contain
libraries required to link the binary
Returns:
A `struct` containing the following fields:
* `cc_info`: A `CcInfo` provider that will provide linker flags to
binaries that depend on Swift targets.
* `objc_info`: An `apple_common.Objc` provider that will provide
linker flags to binaries that depend on Swift targets.
"""
linkopts = []
if toolchain_root:
# This -L has to come before Xcode's to make sure libraries are
# overridden when applicable
linkopts.append("-L{}/usr/lib/swift/{}".format(
toolchain_root,
target_triples.platform_name_for_swift(target_triple),
))
swift_lib_dir = paths.join(
apple_toolchain.developer_dir(),
"Toolchains/XcodeDefault.xctoolchain/usr/lib/swift",
target_triples.platform_name_for_swift(target_triple),
)
linkopts.extend([
"-L{}".format(swift_lib_dir),
"-L/usr/lib/swift",
# TODO(b/112000244): These should get added by the C++ Starlark API,
# but we're using the "c++-link-executable" action right now instead
# of "objc-executable" because the latter requires additional
# variables not provided by cc_common. Figure out how to handle this
# correctly.
"-Wl,-objc_abi_version,2",
"-Wl,-rpath,/usr/lib/swift",
])
if _OBJC_PROVIDER_LINKING:
objc_info = apple_common.new_objc_provider(linkopt = depset(linkopts))
else:
objc_info = apple_common.new_objc_provider()
return struct(
cc_info = CcInfo(
linking_context = cc_common.create_linking_context(
linker_inputs = depset([
cc_common.create_linker_input(
owner = toolchain_label,
user_link_flags = depset(linkopts),
),
]),
),
),
objc_info = objc_info,
)
def _resource_directory_configurator(developer_dir, _prerequisites, args):
"""Configures compiler flags about the toolchain's resource directory.
We must pass a resource directory explicitly if the build rules are invoked
using a custom driver executable or a partial toolchain root, so that the
compiler doesn't try to find its resources relative to that binary.
Args:
developer_dir: The path to Xcode's Developer directory. This argument is
pre-bound in the partial.
_prerequisites: The value returned by
`swift_common.action_prerequisites`.
args: The `Args` object to which flags will be added.
"""
args.add(
"-resource-dir",
(
"{developer_dir}/Toolchains/{toolchain}.xctoolchain/" +
"usr/lib/swift"
).format(
developer_dir = developer_dir,
toolchain = "XcodeDefault",
),
)
def _all_action_configs(
additional_objc_copts,
additional_swiftc_copts,
apple_toolchain,
generated_header_rewriter,
needs_resource_directory,
target_triple):
"""Returns the action configurations for the Swift toolchain.
Args:
additional_objc_copts: Additional Objective-C compiler flags obtained
from the `objc` configuration fragment (and legacy flags that were
previously passed directly by Bazel).
additional_swiftc_copts: Additional Swift compiler flags obtained from
the `swift` configuration fragment.
apple_toolchain: The `apple_common.apple_toolchain()` object.
generated_header_rewriter: An executable that will be invoked after
compilation to rewrite the generated header, or None if this is not
desired.
needs_resource_directory: If True, the toolchain needs the resource
directory passed explicitly to the compiler.
target_triple: The triple of the platform being targeted.
Returns:
The action configurations for the Swift toolchain.
"""
# Basic compilation flags (target triple and toolchain search paths).
action_configs = [
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.COMPILE_MODULE_INTERFACE,
swift_action_names.DERIVE_FILES,
swift_action_names.PRECOMPILE_C_MODULE,
swift_action_names.DUMP_AST,
],
configurators = [
swift_toolchain_config.add_arg(
"-target",
target_triples.str(target_triple),
),
swift_toolchain_config.add_arg(
"-sdk",
apple_toolchain.sdk_dir(),
),
],
),
]
action_configs.extend([
# Xcode path remapping
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg(
"-debug-prefix-map",
"__BAZEL_XCODE_DEVELOPER_DIR__=/PLACEHOLDER_DEVELOPER_DIR",
),
],
features = [
[SWIFT_FEATURE_REMAP_XCODE_PATH, SWIFT_FEATURE_DEBUG_PREFIX_MAP],
],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.COMPILE_MODULE_INTERFACE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg(
"-coverage-prefix-map",
"__BAZEL_XCODE_DEVELOPER_DIR__=/PLACEHOLDER_DEVELOPER_DIR",
),
],
features = [
[
SWIFT_FEATURE_REMAP_XCODE_PATH,
SWIFT_FEATURE_COVERAGE_PREFIX_MAP,
SWIFT_FEATURE_COVERAGE,
],
],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg(
"-file-prefix-map",
"__BAZEL_XCODE_DEVELOPER_DIR__=/PLACEHOLDER_DEVELOPER_DIR",
),
],
features = [
[
SWIFT_FEATURE_REMAP_XCODE_PATH,
SWIFT_FEATURE_FILE_PREFIX_MAP,
],
],
),
])
if needs_resource_directory:
# If the user is using a custom driver but not a complete custom
# toolchain, provide the original toolchain's resources as the resource
# directory so that modules are found correctly.
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 = [
partial.make(
_resource_directory_configurator,
apple_toolchain.developer_dir(),
),
],
),
)
action_configs.extend(compile_action_configs(
additional_objc_copts = additional_objc_copts,
additional_swiftc_copts = additional_swiftc_copts,
generated_header_rewriter = generated_header_rewriter.executable,
))
return action_configs
def _all_tool_configs(
custom_toolchain,
env,
execution_requirements,
generated_header_rewriter,
swift_executable,
toolchain_root):
"""Returns the tool configurations for the Swift toolchain.
Args:
custom_toolchain: The bundle identifier of a custom Swift toolchain, if
one was requested.
env: The environment variables to set when launching tools.
execution_requirements: The execution requirements for tools.
generated_header_rewriter: A `struct` returned by
`resolve_optional_tool` that represents an executable that will be
invoked after compilation to rewrite the generated header.
swift_executable: A custom Swift driver executable to be used during the
build, if provided.
toolchain_root: The root directory of the toolchain, if provided.
Returns:
A dictionary mapping action name to tool configuration.
"""
# Configure the environment variables that the worker needs to fill in the
# Bazel placeholders for SDK root and developer directory, along with the
# custom toolchain if requested.
if custom_toolchain:
env = dict(env)
env["TOOLCHAINS"] = custom_toolchain
env["SWIFT_AVOID_WARNING_USING_OLD_DRIVER"] = "1"
tool_config = swift_toolchain_config.driver_tool_config(
driver_mode = "swiftc",
env = env,
execution_requirements = execution_requirements,
swift_executable = swift_executable,
tool_input_manifests = generated_header_rewriter.input_manifests,
tool_inputs = generated_header_rewriter.inputs,
toolchain_root = toolchain_root,
use_param_file = True,
worker_mode = "persistent",
)
tool_configs = {
swift_action_names.COMPILE: tool_config,
swift_action_names.DERIVE_FILES: tool_config,
swift_action_names.DUMP_AST: tool_config,
swift_action_names.PRECOMPILE_C_MODULE: (
swift_toolchain_config.driver_tool_config(
driver_mode = "swiftc",
env = env,
execution_requirements = execution_requirements,
swift_executable = swift_executable,
toolchain_root = toolchain_root,
use_param_file = True,
worker_mode = "wrap",
)
),
swift_action_names.COMPILE_MODULE_INTERFACE: (
swift_toolchain_config.driver_tool_config(
driver_mode = "swiftc",
args = ["-frontend"],
env = env,
execution_requirements = execution_requirements,
swift_executable = swift_executable,
toolchain_root = toolchain_root,
use_param_file = True,
worker_mode = "wrap",
)
),
}
return tool_configs
def _is_xcode_at_least_version(xcode_config, desired_version):
"""Returns True if we are building with at least the given Xcode version.
Args:
xcode_config: The `apple_common.XcodeVersionConfig` provider.
desired_version: The minimum desired Xcode version, as a dotted version
string.
Returns:
True if the current target is being built with a version of Xcode at
least as high as the given version.
"""
current_version = xcode_config.xcode_version()
if not current_version:
fail("Could not determine Xcode version at all. This likely means " +
"Xcode isn't available; if you think this is a mistake, please " +
"file an issue.")
desired_version_value = apple_common.dotted_version(desired_version)
return current_version >= desired_version_value
def _xcode_env(target_triple, xcode_config):
"""Returns a dictionary containing Xcode-related environment variables.
Args:
target_triple: The triple of the platform being targeted.
xcode_config: The `XcodeVersionConfig` provider that contains
information about the current Xcode configuration.
Returns:
A `dict` containing Xcode-related environment variables that should be
passed to Swift compile and link actions.
"""
return dicts.add(
apple_common.apple_host_system_env(xcode_config),
apple_common.target_apple_env(
xcode_config,
_bazel_apple_platform(target_triple),
),
)
def _entry_point_linkopts_provider(*, entry_point_name):
"""Returns linkopts to customize the entry point of a binary."""
return struct(
linkopts = ["-Wl,-alias,_{},_main".format(entry_point_name)],
)
def _xcode_swift_toolchain_impl(ctx):
cpp_fragment = ctx.fragments.cpp
apple_toolchain = apple_common.apple_toolchain()
cc_toolchain = find_cpp_toolchain(ctx)
target_triple = target_triples.normalize_for_swift(
target_triples.parse(cc_toolchain.target_gnu_system_name),
)
xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
# TODO: b/312204041 - Remove the use of the `swift` fragment once we've
# migrated the `--swiftcopt` flag via `--flag_alias`.
swiftcopts = list(ctx.fragments.swift.copts())
if "-exec-" in ctx.bin_dir.path:
swiftcopts.extend(ctx.attr._exec_copts[BuildSettingInfo].value)
else:
swiftcopts.extend(ctx.attr._copts[BuildSettingInfo].value)
# `--define=SWIFT_USE_TOOLCHAIN_ROOT=<path>` is a rapid development feature
# that lets you build *just* a custom `swift` driver (and `swiftc`
# symlink), rather than a full toolchain, and point compilation actions at
# those. Note that the files must still be in a "toolchain-like" directory
# structure, meaning that the path passed here must contain a `bin`
# directory and that directory contains the `swift` and `swiftc` files.
#
# TODO(allevato): Retire this feature in favor of the `swift_executable`
# attribute, which supports remote builds.
#
# To use a "standard" custom toolchain built using the full Swift build
# script, use `--define=SWIFT_CUSTOM_TOOLCHAIN=<id>` as shown below.
swift_executable = get_swift_executable_for_toolchain(ctx)
toolchain_root = ctx.var.get("SWIFT_USE_TOOLCHAIN_ROOT")
# TODO: Remove SWIFT_CUSTOM_TOOLCHAIN for the next major release
custom_toolchain = ctx.var.get("SWIFT_CUSTOM_TOOLCHAIN") or ctx.configuration.default_shell_env.get("TOOLCHAINS")
custom_xcode_toolchain_root = None
if ctx.var.get("SWIFT_CUSTOM_TOOLCHAIN"):
print("WARNING: SWIFT_CUSTOM_TOOLCHAIN is deprecated. Use --action_env=TOOLCHAINS=<id> instead.") # buildifier: disable=print
if toolchain_root and custom_toolchain:
fail("Do not use SWIFT_USE_TOOLCHAIN_ROOT and TOOLCHAINS" +
"in the same build.")
elif custom_toolchain:
custom_xcode_toolchain_root = "__BAZEL_CUSTOM_XCODE_TOOLCHAIN_PATH__"
swift_linkopts_providers = _swift_linkopts_providers(
apple_toolchain = apple_toolchain,
target_triple = target_triple,
toolchain_label = ctx.label,
toolchain_root = toolchain_root or custom_xcode_toolchain_root,
)
# Compute the default requested features and conditional ones based on Xcode
# version.
requested_features = features_for_build_modes(
ctx,
cpp_fragment = cpp_fragment,
) + features_from_swiftcopts(swiftcopts = swiftcopts)
requested_features.extend(ctx.features)
requested_features.extend([
SWIFT_FEATURE_BUNDLED_XCTESTS,
SWIFT_FEATURE_CACHEABLE_SWIFTMODULES,
SWIFT_FEATURE_COVERAGE_PREFIX_MAP,
SWIFT_FEATURE_DEBUG_PREFIX_MAP,
SWIFT_FEATURE_ENABLE_BATCH_MODE,
SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES,
SWIFT_FEATURE_OBJC_LINK_FLAGS,
SWIFT_FEATURE_OPT_USES_WMO,
SWIFT_FEATURE_REMAP_XCODE_PATH,
SWIFT_FEATURE_SUPPORTS_LIBRARY_EVOLUTION,
SWIFT_FEATURE_SUPPORTS_PRIVATE_DEPS,
SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG,
SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE,
SWIFT_FEATURE_USE_RESPONSE_FILES,
])
# Xcode 14 implies Swift 5.7.
if _is_xcode_at_least_version(xcode_config, "14.0"):
requested_features.append(SWIFT_FEATURE_FILE_PREFIX_MAP)
requested_features.append(SWIFT_FEATURE_SUPPORTS_BARE_SLASH_REGEX)
if getattr(ctx.fragments.objc, "alwayslink_by_default", False):
requested_features.append(SWIFT_FEATURE__FORCE_ALWAYSLINK_TRUE)
if _is_xcode_at_least_version(xcode_config, "15.0"):
requested_features.append(SWIFT_FEATURE__SUPPORTS_MACROS)
env = _xcode_env(target_triple = target_triple, xcode_config = xcode_config)
execution_requirements = xcode_config.execution_info()
generated_header_rewriter = resolve_optional_tool(
ctx,
target = ctx.attr.generated_header_rewriter,
)
all_tool_configs = _all_tool_configs(
custom_toolchain = custom_toolchain,
env = env,
execution_requirements = execution_requirements,
generated_header_rewriter = generated_header_rewriter,
swift_executable = swift_executable,
toolchain_root = toolchain_root,
)
all_action_configs = _all_action_configs(
additional_objc_copts = _command_line_objc_copts(
ctx.var["COMPILATION_MODE"],
ctx.fragments.cpp,
ctx.fragments.objc,
),
additional_swiftc_copts = swiftcopts,
apple_toolchain = apple_toolchain,
generated_header_rewriter = generated_header_rewriter,
needs_resource_directory = swift_executable or toolchain_root,
target_triple = target_triple,
)
swift_toolchain_developer_paths = []
platform_developer_framework_dir = _platform_developer_framework_dir(
apple_toolchain,
target_triple,
)
if platform_developer_framework_dir:
swift_toolchain_developer_paths.append(
struct(
developer_path_label = "platform",
path = platform_developer_framework_dir,
),
)
sdk_developer_framework_dir = _sdk_developer_framework_dir(
apple_toolchain,
target_triple,
)
if sdk_developer_framework_dir:
swift_toolchain_developer_paths.append(
struct(
developer_path_label = "sdk",
path = sdk_developer_framework_dir,
),
)
return [
SwiftToolchainInfo(
action_configs = all_action_configs,
cc_toolchain_info = cc_toolchain,
clang_implicit_deps_providers = collect_implicit_deps_providers(
ctx.attr.clang_implicit_deps,
),
developer_dirs = swift_toolchain_developer_paths,
entry_point_linkopts_provider = _entry_point_linkopts_provider,
feature_allowlists = [
target[SwiftFeatureAllowlistInfo]
for target in ctx.attr.feature_allowlists
],
generated_header_module_implicit_deps_providers = (
collect_implicit_deps_providers(
ctx.attr.generated_header_module_implicit_deps,
)
),
implicit_deps_providers = collect_implicit_deps_providers(
ctx.attr.implicit_deps + ctx.attr.clang_implicit_deps,
additional_cc_infos = [swift_linkopts_providers.cc_info],
additional_objc_infos = [swift_linkopts_providers.objc_info],
),
package_configurations = [
target[SwiftPackageConfigurationInfo]
for target in ctx.attr.package_configurations
],
requested_features = requested_features,
swift_worker = ctx.attr._worker[DefaultInfo].files_to_run,
test_configuration = struct(
env = env,
execution_requirements = execution_requirements,
),
tool_configs = all_tool_configs,
unsupported_features = ctx.disabled_features + [
SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD,
],
),
]
xcode_swift_toolchain = rule(
attrs = dicts.add(
swift_toolchain_driver_attrs(),
{
"clang_implicit_deps": attr.label_list(
doc = """\
A list of labels to library targets that should be unconditionally added as
implicit dependencies of any explicit C/Objective-C module compiled by the Swift
toolchain and also as implicit dependencies of any Swift modules compiled by
the Swift toolchain.
Despite being C/Objective-C modules, the targets specified by this attribute
must propagate the `SwiftInfo` provider because the Swift build rules use that
provider to look up Clang module requirements. In particular, the targets must
propagate the provider in their rule implementation themselves and not rely on
the implicit traversal performed by `swift_clang_module_aspect`; the latter is
not possible as it would create a dependency cycle between the toolchain and the
implicit dependencies.
""",
providers = [[SwiftInfo]],
),
"feature_allowlists": attr.label_list(
doc = """\
A list of `swift_feature_allowlist` targets that allow or prohibit packages from
requesting or disabling features.
""",
providers = [[SwiftFeatureAllowlistInfo]],
),
"generated_header_module_implicit_deps": attr.label_list(
doc = """\
Targets whose `SwiftInfo` providers should be treated as compile-time inputs to
actions that precompile the explicit module for the generated Objective-C header
of a Swift module.
""",
providers = [[SwiftInfo]],
),
"generated_header_rewriter": attr.label(
allow_files = True,
cfg = "exec",
doc = """\
If present, an executable that will be invoked after compilation to rewrite the
generated header.
This tool is expected to have a command line interface such that the Swift
compiler invocation is passed to it following a `"--"` argument, and any
arguments preceding the `"--"` can be defined by the tool itself (however, at
this time the worker does not support passing additional flags to the tool).
""",
executable = True,
),
"implicit_deps": attr.label_list(
allow_files = True,
doc = """\
A list of labels to library targets that should be unconditionally added as
implicit dependencies of any Swift compilation or linking target.
""",
providers = [
[CcInfo],
[SwiftInfo],
],
),
"package_configurations": attr.label_list(
doc = """\
A list of `swift_package_configuration` targets that specify additional compiler
configuration options that are applied to targets on a per-package basis.
""",
providers = [[SwiftPackageConfigurationInfo]],
),
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
doc = """\
The C++ toolchain from which linking flags and other tools needed by the Swift
toolchain (such as `clang`) will be retrieved.
""",
),
"_copts": attr.label(
default = Label("@build_bazel_rules_swift//swift:copt"),
doc = """\
The label of the `string_list` containing additional flags that should be passed
to the compiler.
""",
),
"_exec_copts": attr.label(
default = Label("@build_bazel_rules_swift//swift:exec_copt"),
doc = """\
The label of the `string_list` containing additional flags that should be passed
to the compiler for exec transition builds.
""",
),
"_worker": attr.label(
cfg = "exec",
allow_files = True,
default = Label(
"@build_bazel_rules_swift//tools/worker:worker_wrapper",
),
doc = """\
An executable that wraps Swift compiler invocations and also provides support
for incremental compilation using a persistent mode.
""",
executable = True,
),
"_xcode_config": attr.label(
default = configuration_field(
name = "xcode_config_label",
fragment = "apple",
),
),
},
),
doc = "Represents a Swift compiler toolchain provided by Xcode.",
fragments = [
"cpp",
"objc",
"swift",
],
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
incompatible_use_toolchain_transition = True,
implementation = _xcode_swift_toolchain_impl,
)