Skip to content
This repository was archived by the owner on Jan 25, 2024. It is now read-only.

Commit 9afbc30

Browse files
allevatoswiple-rules-gardener
authored andcommitted
Support implicit C/Objective-C dependencies that are passed when compiling any explicit Clang module.
The toolchain's `clang_implicit_deps` are not passed to Swift compilations; only to actions that compile an explicit C/Objective-C module using `swift_common.precompile_clang_module`. PiperOrigin-RevId: 375816204
1 parent 407b1c8 commit 9afbc30

File tree

5 files changed

+66
-20
lines changed

5 files changed

+66
-20
lines changed

swift/internal/compiling.bzl

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,9 +1526,7 @@ def compile(
15261526
is_swift_generated_header = True,
15271527
module_map_file = compile_outputs.generated_module_map_file,
15281528
module_name = module_name,
1529-
swift_info = create_swift_info(
1530-
swift_infos = generated_module_deps_swift_infos,
1531-
),
1529+
swift_infos = generated_module_deps_swift_infos,
15321530
swift_toolchain = swift_toolchain,
15331531
target_name = target_name,
15341532
)
@@ -1574,7 +1572,7 @@ def precompile_clang_module(
15741572
target_name,
15751573
bin_dir = None,
15761574
genfiles_dir = None,
1577-
swift_info = None):
1575+
swift_infos = []):
15781576
"""Precompiles an explicit Clang module that is compatible with Swift.
15791577
15801578
Args:
@@ -1607,8 +1605,8 @@ def precompile_clang_module(
16071605
path is added to ClangImporter's header search paths for
16081606
compatibility with Bazel's C++ and Objective-C rules which support
16091607
inclusions of generated headers from that location.
1610-
swift_info: A `SwiftInfo` provider that contains dependencies required
1611-
to compile this module.
1608+
swift_infos: A list of `SwiftInfo` providers representing dependencies
1609+
required to compile this module.
16121610
16131611
Returns:
16141612
A `File` representing the precompiled module (`.pcm`) file, or `None` if
@@ -1623,7 +1621,7 @@ def precompile_clang_module(
16231621
is_swift_generated_header = False,
16241622
module_map_file = module_map_file,
16251623
module_name = module_name,
1626-
swift_info = swift_info,
1624+
swift_infos = swift_infos,
16271625
swift_toolchain = swift_toolchain,
16281626
target_name = target_name,
16291627
)
@@ -1640,7 +1638,7 @@ def _precompile_clang_module(
16401638
target_name,
16411639
bin_dir = None,
16421640
genfiles_dir = None,
1643-
swift_info = None):
1641+
swift_infos = []):
16441642
"""Precompiles an explicit Clang module that is compatible with Swift.
16451643
16461644
Args:
@@ -1675,8 +1673,8 @@ def _precompile_clang_module(
16751673
path is added to ClangImporter's header search paths for
16761674
compatibility with Bazel's C++ and Objective-C rules which support
16771675
inclusions of generated headers from that location.
1678-
swift_info: A `SwiftInfo` provider that contains dependencies required
1679-
to compile this module.
1676+
swift_infos: A list of `SwiftInfo` providers representing dependencies
1677+
required to compile this module.
16801678
16811679
Returns:
16821680
A `File` representing the precompiled module (`.pcm`) file, or `None` if
@@ -1702,8 +1700,20 @@ def _precompile_clang_module(
17021700
target_name = target_name,
17031701
)
17041702

1705-
if swift_info:
1706-
transitive_modules = swift_info.transitive_modules.to_list()
1703+
if not is_swift_generated_header:
1704+
implicit_swift_infos = (
1705+
swift_toolchain.clang_implicit_deps_providers.swift_infos
1706+
)
1707+
else:
1708+
implicit_swift_infos = []
1709+
1710+
if not is_swift_generated_header and implicit_swift_infos:
1711+
swift_infos = list(swift_infos)
1712+
swift_infos.extend(implicit_swift_infos)
1713+
1714+
if swift_infos:
1715+
merged_swift_info = create_swift_info(swift_infos = swift_infos)
1716+
transitive_modules = merged_swift_info.transitive_modules.to_list()
17071717
else:
17081718
transitive_modules = []
17091719

swift/internal/providers.bzl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ using this toolchain.
110110
"cc_toolchain_info": """\
111111
The `cc_common.CcToolchainInfo` provider from the Bazel C++ toolchain that this
112112
Swift toolchain depends on.
113+
""",
114+
"clang_implicit_deps_providers": """\
115+
A `struct` with the following fields, which represent providers from targets
116+
that should be added as implicit dependencies of any precompiled explicit
117+
C/Objective-C modules:
118+
119+
* `cc_infos`: A list of `CcInfo` providers from targets specified as the
120+
toolchain's implicit dependencies.
121+
* `objc_infos`: A list of `apple_common.Objc` providers from targets specified
122+
as the toolchain's implicit dependencies.
123+
* `swift_infos`: A list of `SwiftInfo` providers from targets specified as the
124+
toolchain's implicit dependencies.
125+
126+
For ease of use, this field is never `None`; it will always be a valid `struct`
127+
containing the fields described above, even if those lists are empty.
113128
""",
114129
"cpu": """\
115130
`String`. The CPU architecture that the toolchain is targeting.

swift/internal/swift_clang_module_aspect.bzl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,15 +423,14 @@ def _handle_module(
423423
"""
424424
attr = aspect_ctx.rule.attr
425425

426-
if swift_infos:
427-
merged_swift_info = create_swift_info(swift_infos = swift_infos)
428-
else:
429-
merged_swift_info = None
426+
all_swift_infos = (
427+
swift_infos + swift_toolchain.clang_implicit_deps_providers.swift_infos
428+
)
430429

431430
# Collect the names of Clang modules that the module being built directly
432431
# depends on.
433432
dependent_module_names = []
434-
for swift_info in swift_infos:
433+
for swift_info in all_swift_infos:
435434
for module in swift_info.direct_modules:
436435
if module.clang:
437436
dependent_module_names.append(module.name)
@@ -450,8 +449,8 @@ def _handle_module(
450449
)
451450

452451
if not module_map_file:
453-
if merged_swift_info:
454-
return [merged_swift_info]
452+
if all_swift_infos:
453+
return [create_swift_info(swift_infos = swift_infos)]
455454
else:
456455
return []
457456

@@ -474,7 +473,7 @@ def _handle_module(
474473
genfiles_dir = aspect_ctx.genfiles_dir,
475474
module_map_file = module_map_file,
476475
module_name = module_name,
477-
swift_info = merged_swift_info,
476+
swift_infos = swift_infos,
478477
swift_toolchain = swift_toolchain,
479478
target_name = target.label.name,
480479
)

swift/internal/swift_toolchain.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ def _swift_toolchain_impl(ctx):
212212
action_configs = all_action_configs,
213213
all_files = depset(all_files),
214214
cc_toolchain_info = cc_toolchain,
215+
clang_implicit_deps_providers = (
216+
collect_implicit_deps_providers([])
217+
),
215218
cpu = ctx.attr.arch,
216219
feature_allowlists = [
217220
target[SwiftFeatureAllowlistInfo]

swift/internal/xcode_swift_toolchain.bzl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,9 @@ def _xcode_swift_toolchain_impl(ctx):
730730
action_configs = all_action_configs,
731731
all_files = depset(all_files),
732732
cc_toolchain_info = cc_toolchain,
733+
clang_implicit_deps_providers = collect_implicit_deps_providers(
734+
ctx.attr.clang_implicit_deps,
735+
),
733736
cpu = cpu,
734737
feature_allowlists = [
735738
target[SwiftFeatureAllowlistInfo]
@@ -764,6 +767,22 @@ xcode_swift_toolchain = rule(
764767
attrs = dicts.add(
765768
swift_toolchain_driver_attrs(),
766769
{
770+
"clang_implicit_deps": attr.label_list(
771+
doc = """\
772+
A list of labels to library targets that should be unconditionally added as
773+
implicit dependencies of any explicit C/Objective-C module compiled by the Swift
774+
toolchain.
775+
776+
Despite being C/Objective-C modules, the targets specified by this attribute
777+
must propagate the `SwiftInfo` provider because the Swift build rules use that
778+
provider to look up Clang module requirements. In particular, the targets must
779+
propagate the provider in their rule implementation themselves and not rely on
780+
the implicit traversal performed by `swift_clang_module_aspect`; the latter is
781+
not possible as it would create a dependency cycle between the toolchain and the
782+
implicit dependencies.
783+
""",
784+
providers = [[SwiftInfo]],
785+
),
767786
"feature_allowlists": attr.label_list(
768787
doc = """\
769788
A list of `swift_feature_allowlist` targets that allow or prohibit packages from

0 commit comments

Comments
 (0)