Skip to content

Commit a0f0ff2

Browse files
allevatoswiple-rules-gardener
authored andcommitted
Deprecate the SwiftClangModuleInfo provider.
To lessen the number of providers that rules doing Swift compilation have to deal with, required C module maps are now being propagated in `SwiftInfo` rather than their own provider (the other three fields are obsolete thanks to `CcInfo`'s compilation context). This change retains the provider but moves it into an aspect that can be removed easily once the uses of it have been removed from Tulsi. RELNOTES: The `SwiftClangModuleInfo` provider has been deprecated. Headers and include search paths can be obtained from `CcInfo`'s compilation context. Module maps are propagated in `SwiftInfo.transitive_modulemaps`. PiperOrigin-RevId: 247418171
1 parent e9fc09a commit a0f0ff2

15 files changed

+228
-332
lines changed

swift/internal/api.bzl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ _SANITIZER_FEATURE_FLAG_MAP = {
7575

7676
def _create_swift_info(
7777
defines = [],
78+
modulemaps = [],
7879
module_name = None,
7980
swiftdocs = [],
8081
swiftmodules = [],
@@ -93,6 +94,8 @@ def _create_swift_info(
9394
9495
Args:
9596
defines: A list of defines that will be provided as `copts` of the target being built.
97+
modulemaps: A list of module maps that should be passed to ClangImporter by any target
98+
that depends on the one propagating this provider.
9699
module_name: A string containing the name of the Swift module. If this is `None`, the
97100
provider does not represent a compiled module but rather a collection of modules (this
98101
happens, for example, with `proto_library` targets that have no sources of their own
@@ -110,10 +113,12 @@ def _create_swift_info(
110113
A new `SwiftInfo` provider with the given values.
111114
"""
112115
transitive_defines = []
116+
transitive_modulemaps = []
113117
transitive_swiftdocs = []
114118
transitive_swiftmodules = []
115119
for swift_info in swift_infos:
116120
transitive_defines.append(swift_info.transitive_defines)
121+
transitive_modulemaps.append(swift_info.transitive_modulemaps)
117122
transitive_swiftdocs.append(swift_info.transitive_swiftdocs)
118123
transitive_swiftmodules.append(swift_info.transitive_swiftmodules)
119124

@@ -124,6 +129,7 @@ def _create_swift_info(
124129
module_name = module_name,
125130
swift_version = swift_version,
126131
transitive_defines = depset(defines, transitive = transitive_defines),
132+
transitive_modulemaps = depset(modulemaps, transitive = transitive_modulemaps),
127133
transitive_swiftdocs = depset(swiftdocs, transitive = transitive_swiftdocs),
128134
transitive_swiftmodules = depset(swiftmodules, transitive = transitive_swiftmodules),
129135
)
@@ -443,8 +449,7 @@ def _compile(
443449
which affects the nature of the output files.
444450
defines: Symbols that should be defined by passing `-D` to the compiler.
445451
deps: Dependencies of the target being compiled. These targets must propagate one of the
446-
following providers: `CcInfo`, `SwiftClangModuleInfo`, `SwiftInfo`, or
447-
`apple_common.Objc`.
452+
following providers: `CcInfo`, `SwiftInfo`, or `apple_common.Objc`.
448453
genfiles_dir: The Bazel `*-genfiles` directory root. If provided, its path is added to
449454
ClangImporter's header search paths for compatibility with Bazel's C++ and Objective-C
450455
rules which support inclusions of generated headers from that location.
@@ -903,8 +908,8 @@ def _swiftc_command_line_and_inputs(
903908
requested, which affects the nature of the output files.
904909
defines: Symbols that should be defined by passing `-D` to the compiler.
905910
deps: Dependencies of the target being compiled. These targets must
906-
propagate one of the following providers: `CcInfo`,
907-
`SwiftClangModuleInfo`, `SwiftInfo`, or `apple_common.Objc`.
911+
propagate one of the following providers: `CcInfo`, `SwiftInfo`, or
912+
`apple_common.Objc`.
908913
genfiles_dir: The Bazel `*-genfiles` directory root. If provided, its path
909914
is added to ClangImporter's header search paths for compatibility with
910915
Bazel's C++ and Objective-C rules which support inclusions of generated

swift/internal/attrs.bzl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
"""Common attributes used by multiple Swift build rules."""
1616

17-
load(":providers.bzl", "SwiftClangModuleInfo", "SwiftInfo")
17+
load(":legacy_swift_clang_module_info_aspect.bzl", "legacy_swift_clang_module_info_aspect")
18+
load(":providers.bzl", "SwiftInfo")
1819

1920
def swift_common_rule_attrs(additional_deps_aspects = []):
2021
return {
@@ -29,14 +30,13 @@ binary or library, or other programs needed by it.
2930
""",
3031
),
3132
"deps": attr.label_list(
32-
aspects = additional_deps_aspects,
33+
aspects = [legacy_swift_clang_module_info_aspect] + additional_deps_aspects,
3334
doc = """
3435
A list of targets that are dependencies of the target being built, which will be
3536
linked into that target. Allowed kinds of dependencies are:
3637
37-
* `swift_c_module` (or anything propagating `SwiftClangModuleInfo`)
38-
* `swift_import` and `swift_library` (or anything propagating `SwiftInfo`)
39-
* `cc_library` (or anything propagating `CcInfo`)
38+
* `swift_c_module`, `swift_import` and `swift_library` (or anything propagating `SwiftInfo`)
39+
* `cc_library` (or anything propagating `CcInfo`)
4040
4141
Additionally, on platforms that support Objective-C interop, `objc_library`
4242
targets (or anything propagating the `apple_common.Objc` provider) are allowed
@@ -45,7 +45,6 @@ Linux), those dependencies will be **ignored.**
4545
""",
4646
providers = [
4747
[CcInfo],
48-
[SwiftClangModuleInfo],
4948
[SwiftInfo],
5049
[apple_common.Objc],
5150
],

swift/internal/compiling.bzl

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ load(
2222
)
2323
load(":actions.bzl", "run_toolchain_swift_action")
2424
load(":derived_files.bzl", "derived_files")
25-
load(":providers.bzl", "SwiftClangModuleInfo", "SwiftInfo")
25+
load(":providers.bzl", "SwiftInfo")
2626
load(
2727
":utils.bzl",
2828
"collect_cc_libraries",
29-
"collect_transitive",
3029
"get_providers",
3130
"objc_provider_framework_name",
3231
)
@@ -50,58 +49,67 @@ def collect_transitive_compile_inputs(args, deps, direct_defines = []):
5049
input_depsets = []
5150

5251
# Collect all the search paths, module maps, flags, and so forth from transitive dependencies.
53-
transitive_swiftmodules = collect_transitive(
54-
deps,
55-
SwiftInfo,
56-
"transitive_swiftmodules",
57-
)
58-
args.add_all(transitive_swiftmodules, format_each = "-I%s", map_each = _dirname_map_fn)
59-
input_depsets.append(transitive_swiftmodules)
60-
61-
transitive_defines = collect_transitive(
62-
deps,
63-
SwiftInfo,
64-
"transitive_defines",
65-
direct = direct_defines,
66-
)
67-
args.add_all(transitive_defines, format_each = "-D%s")
68-
69-
transitive_modulemaps = collect_transitive(
70-
deps,
71-
SwiftClangModuleInfo,
72-
"transitive_modulemaps",
73-
)
74-
input_depsets.append(transitive_modulemaps)
52+
transitive_cc_defines = []
53+
transitive_cc_headers = []
54+
transitive_cc_includes = []
55+
transitive_cc_quote_includes = []
56+
transitive_cc_system_includes = []
57+
transitive_defines = []
58+
transitive_modulemaps = []
59+
transitive_swiftmodules = []
60+
for dep in deps:
61+
if SwiftInfo in dep:
62+
swift_info = dep[SwiftInfo]
63+
transitive_defines.append(swift_info.transitive_defines)
64+
transitive_modulemaps.append(swift_info.transitive_modulemaps)
65+
transitive_swiftmodules.append(swift_info.transitive_swiftmodules)
66+
if CcInfo in dep:
67+
compilation_context = dep[CcInfo].compilation_context
68+
transitive_cc_defines.append(compilation_context.defines)
69+
transitive_cc_headers.append(compilation_context.headers)
70+
transitive_cc_includes.append(compilation_context.includes)
71+
transitive_cc_quote_includes.append(compilation_context.quote_includes)
72+
transitive_cc_system_includes.append(compilation_context.system_includes)
73+
74+
# Add import paths for the directories containing dependencies' swiftmodules.
75+
all_swiftmodules = depset(transitive = transitive_swiftmodules)
76+
args.add_all(all_swiftmodules, format_each = "-I%s", map_each = _dirname_map_fn)
77+
input_depsets.append(all_swiftmodules)
78+
79+
# Pass Swift defines propagated by dependencies.
80+
all_defines = depset(direct_defines, transitive = transitive_defines)
81+
args.add_all(all_defines, format_each = "-D%s")
82+
83+
# Pass module maps from C/C++ dependencies to ClangImporter.
84+
# TODO(allevato): Will `CcInfo` eventually keep these in its compilation context?
85+
all_modulemaps = depset(transitive = transitive_modulemaps)
86+
input_depsets.append(all_modulemaps)
87+
args.add_all(all_modulemaps, before_each = "-Xcc", format_each = "-fmodule-map-file=%s")
88+
89+
# Add C++ headers from dependencies to the action inputs so the compiler can read them.
90+
input_depsets.append(depset(transitive = transitive_cc_headers))
91+
92+
# Pass any C++ defines and include search paths to ClangImporter.
7593
args.add_all(
76-
transitive_modulemaps,
94+
depset(transitive = transitive_cc_defines),
7795
before_each = "-Xcc",
78-
format_each = "-fmodule-map-file=%s",
96+
format_each = "-D%s",
7997
)
80-
81-
transitive_cc_headers = collect_transitive(
82-
deps,
83-
SwiftClangModuleInfo,
84-
"transitive_headers",
98+
args.add_all(
99+
depset(transitive = transitive_cc_includes),
100+
before_each = "-Xcc",
101+
format_each = "-I%s",
85102
)
86-
input_depsets.append(transitive_cc_headers)
87-
88-
transitive_cc_compile_flags = collect_transitive(
89-
deps,
90-
SwiftClangModuleInfo,
91-
"transitive_compile_flags",
103+
args.add_all(
104+
depset(transitive = transitive_cc_quote_includes),
105+
before_each = "-Xcc",
106+
format_each = "-iquote%s",
92107
)
93-
94-
# Handle possible spaces in these arguments correctly (for example,
95-
# `-isystem foo`) by prepending `-Xcc` to each one.
96-
for arg in transitive_cc_compile_flags.to_list():
97-
args.add_all(arg.split(" "), before_each = "-Xcc")
98-
99-
transitive_cc_defines = collect_transitive(
100-
deps,
101-
SwiftClangModuleInfo,
102-
"transitive_defines",
108+
args.add_all(
109+
depset(transitive = transitive_cc_system_includes),
110+
before_each = "-Xcc",
111+
format_each = "-isystem%s",
103112
)
104-
args.add_all(transitive_cc_defines, before_each = "-Xcc", format_each = "-D%s")
105113

106114
return input_depsets
107115

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Attaches `SwiftClangModuleInfo` providers as needed to Swift targets.
16+
17+
Tulsi depends on this provider, so we can't remove it completely until a new release has been cut.
18+
Moving it entirely to this aspect (which simulates the behavior that would have happened on the
19+
rules themselves) allows us to remove it more easily in the future.
20+
"""
21+
22+
load(":providers.bzl", "SwiftClangModuleInfo", "SwiftInfo")
23+
24+
def _legacy_swift_clang_module_info_aspect_impl(target, aspect_ctx):
25+
has_interesting_provider = False
26+
27+
if SwiftInfo in target:
28+
has_interesting_provider = True
29+
swift_info = target[SwiftInfo]
30+
transitive_modulemaps = swift_info.transitive_modulemaps
31+
else:
32+
transitive_modulemaps = depset()
33+
34+
if CcInfo in target:
35+
has_interesting_provider = True
36+
compilation_context = target[CcInfo].compilation_context
37+
transitive_compile_flags = depset(
38+
["-I{}".format(path) for path in compilation_context.includes.to_list()] +
39+
["-isystem{}".format(path) for path in compilation_context.system_includes.to_list()] +
40+
["-iquote{}".format(path) for path in compilation_context.quote_includes.to_list()],
41+
)
42+
transitive_defines = compilation_context.defines
43+
transitive_headers = compilation_context.headers
44+
else:
45+
transitive_compile_flags = None
46+
transitive_defines = None
47+
transitive_headers = None
48+
49+
if has_interesting_provider:
50+
return [SwiftClangModuleInfo(
51+
transitive_compile_flags = transitive_compile_flags,
52+
transitive_defines = transitive_defines,
53+
transitive_headers = transitive_headers,
54+
transitive_modulemaps = transitive_modulemaps,
55+
)]
56+
57+
return []
58+
59+
legacy_swift_clang_module_info_aspect = aspect(
60+
implementation = _legacy_swift_clang_module_info_aspect_impl,
61+
)

0 commit comments

Comments
 (0)