Skip to content

Commit c193b6b

Browse files
allevatoswiple-rules-gardener
authored andcommitted
Move link-related info out of SwiftInfo and into CcInfo.
As of this change, all Swift targets propagate a `CcInfo` provider that references the libraries that dependents should link in order to use that library. The `SwiftInfo` provided by the library now only contains Swift-specific information, such as the `.swiftmodule` files that dependents need. RELNOTES: The `direct_libraries`, `direct_linkopts`, `transitive_additional_inputs`, `transitive_libraries`, and `transitive_linkopts` fields of `SwiftInfo` have been removed. This information is now propagated using linking contexts in `CcInfo` providers. RELNOTES: `swift_grpc_library` and `swift_proto_library` now return the generated source files as default outputs for easier inspection. PiperOrigin-RevId: 246581943
1 parent 0dfbdc8 commit c193b6b

16 files changed

+341
-446
lines changed

swift/internal/api.bzl

Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -74,54 +74,58 @@ _SANITIZER_FEATURE_FLAG_MAP = {
7474
}
7575

7676
def _create_swift_info(
77-
additional_inputs = [],
7877
defines = [],
79-
libraries = [],
80-
linkopts = [],
8178
module_name = None,
8279
swiftdocs = [],
8380
swiftmodules = [],
81+
swift_infos = [],
8482
swift_version = None):
8583
"""Creates a new `SwiftInfo` provider with the given values.
8684
8785
This function is recommended instead of directly creating a `SwiftInfo` provider because it
8886
encodes reasonable defaults for fields that some rules may not be interested in and ensures
8987
that the direct and transitive fields are set consistently.
9088
89+
This function can also be used to do a simple merge of `SwiftInfo` providers, by leaving all
90+
of the arguments except for `swift_infos` as their empty defaults. In that case, the returned
91+
provider will not represent a true Swift module; it is merely a "collector" for other
92+
dependencies.
93+
9194
Args:
92-
additional_inputs: A list of additional input files passed into a library or binary target
93-
via the `swiftc_inputs` attribute.
9495
defines: A list of defines that will be provided as `copts` of the target being built.
95-
libraries: A list of `.a` files that are the direct outputs of the target being built.
96-
linkopts: A list of linker flags that will be passed to the linker when the target being
97-
built is linked into a binary.
98-
module_name: A string containing the name of the Swift module, or `None` if the provider
99-
does not represent a compiled module (this happens, for example, with `proto_library`
100-
targets that act as "collectors" of other modules but have no sources of their own).
96+
module_name: A string containing the name of the Swift module. If this is `None`, the
97+
provider does not represent a compiled module but rather a collection of modules (this
98+
happens, for example, with `proto_library` targets that have no sources of their own
99+
but depend on others that do).
101100
swiftdocs: A list of `.swiftdoc` files that are the direct outputs of the target being
102-
built.
101+
built. If omitted, an empty list is used.
103102
swiftmodules: A list of `.swiftmodule` files that are the direct outputs of the target
104-
being built.
103+
being built. If omitted, an empty list is used.
104+
swift_infos: A list of `SwiftInfo` providers from dependencies, whose transitive fields
105+
should be merged into the new one. If omitted, no transitive data is collected.
105106
swift_version: A string containing the value of the `-swift-version` flag used when
106-
compiling this target, or `None` if it was not set or is not relevant.
107+
compiling this target, or `None` (the default) if it was not set or is not relevant.
107108
108109
Returns:
109110
A new `SwiftInfo` provider with the given values.
110111
"""
112+
transitive_defines = []
113+
transitive_swiftdocs = []
114+
transitive_swiftmodules = []
115+
for swift_info in swift_infos:
116+
transitive_defines.append(swift_info.transitive_defines)
117+
transitive_swiftdocs.append(swift_info.transitive_swiftdocs)
118+
transitive_swiftmodules.append(swift_info.transitive_swiftmodules)
119+
111120
return SwiftInfo(
112121
direct_defines = defines,
113-
direct_libraries = libraries,
114-
direct_linkopts = linkopts,
115122
direct_swiftdocs = swiftdocs,
116123
direct_swiftmodules = swiftmodules,
117124
module_name = module_name,
118125
swift_version = swift_version,
119-
transitive_additional_inputs = depset(direct = additional_inputs),
120-
transitive_defines = depset(direct = defines),
121-
transitive_libraries = depset(direct = libraries, order = "topological"),
122-
transitive_linkopts = depset(direct = linkopts),
123-
transitive_swiftdocs = depset(direct = swiftdocs),
124-
transitive_swiftmodules = depset(direct = swiftmodules),
126+
transitive_defines = depset(defines, transitive = transitive_defines),
127+
transitive_swiftdocs = depset(swiftdocs, transitive = transitive_swiftdocs),
128+
transitive_swiftmodules = depset(swiftmodules, transitive = transitive_swiftmodules),
125129
)
126130

127131
def _compilation_attrs(additional_deps_aspects = []):
@@ -831,46 +835,6 @@ conformance.
831835
},
832836
)
833837

834-
def _merge_swift_infos(swift_infos):
835-
"""Merges a list of `SwiftInfo` providers into one.
836-
837-
Args:
838-
swift_infos: A sequence of `SwiftInfo`providers to merge.
839-
840-
Returns:
841-
A new `SwiftInfo` provider.
842-
"""
843-
transitive_additional_inputs = []
844-
transitive_defines = []
845-
transitive_libraries = []
846-
transitive_linkopts = []
847-
transitive_swiftdocs = []
848-
transitive_swiftmodules = []
849-
850-
for swift_info in swift_infos:
851-
transitive_additional_inputs.append(swift_info.transitive_additional_inputs)
852-
transitive_defines.append(swift_info.transitive_defines)
853-
transitive_libraries.append(swift_info.transitive_libraries)
854-
transitive_linkopts.append(swift_info.transitive_linkopts)
855-
transitive_swiftdocs.append(swift_info.transitive_swiftdocs)
856-
transitive_swiftmodules.append(swift_info.transitive_swiftmodules)
857-
858-
return SwiftInfo(
859-
direct_defines = [],
860-
direct_libraries = [],
861-
direct_linkopts = [],
862-
direct_swiftdocs = [],
863-
direct_swiftmodules = [],
864-
module_name = None,
865-
swift_version = None,
866-
transitive_additional_inputs = depset(transitive = transitive_additional_inputs),
867-
transitive_defines = depset(transitive = transitive_defines),
868-
transitive_libraries = depset(transitive = transitive_libraries),
869-
transitive_linkopts = depset(transitive = transitive_linkopts),
870-
transitive_swiftdocs = depset(transitive = transitive_swiftdocs),
871-
transitive_swiftmodules = depset(transitive = transitive_swiftmodules),
872-
)
873-
874838
def _swift_runtime_linkopts(is_static, toolchain, is_test = False):
875839
"""Returns the flags that should be passed to `clang` when linking a binary.
876840
@@ -1064,7 +1028,6 @@ swift_common = struct(
10641028
derive_module_name = _derive_module_name,
10651029
is_enabled = _is_enabled,
10661030
library_rule_attrs = _library_rule_attrs,
1067-
merge_swift_infos = _merge_swift_infos,
10681031
run_toolchain_action = run_toolchain_action,
10691032
run_toolchain_shell_action = run_toolchain_shell_action,
10701033
run_toolchain_swift_action = run_toolchain_swift_action,

swift/internal/attrs.bzl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"""Common attributes used by multiple Swift build rules."""
1616

1717
load(":providers.bzl", "SwiftClangModuleInfo", "SwiftInfo")
18-
load(":swift_cc_libs_aspect.bzl", "swift_cc_libs_aspect")
1918

2019
def swift_common_rule_attrs(additional_deps_aspects = []):
2120
return {
@@ -30,7 +29,7 @@ binary or library, or other programs needed by it.
3029
""",
3130
),
3231
"deps": attr.label_list(
33-
aspects = [swift_cc_libs_aspect] + additional_deps_aspects,
32+
aspects = additional_deps_aspects,
3433
doc = """
3534
A list of targets that are dependencies of the target being built, which will be
3635
linked into that target. Allowed kinds of dependencies are:

swift/internal/compiling.bzl

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ load(
2222
)
2323
load(":actions.bzl", "run_toolchain_swift_action")
2424
load(":derived_files.bzl", "derived_files")
25+
load(":providers.bzl", "SwiftClangModuleInfo", "SwiftInfo")
2526
load(
26-
":providers.bzl",
27-
"SwiftCcLibsInfo",
28-
"SwiftClangModuleInfo",
29-
"SwiftInfo",
27+
":utils.bzl",
28+
"collect_cc_libraries",
29+
"collect_transitive",
30+
"get_providers",
31+
"objc_provider_framework_name",
3032
)
31-
load(":utils.bzl", "collect_transitive", "objc_provider_framework_name")
3233

3334
# Swift compiler options that cause the code to be compiled using whole-module optimization.
3435
_WMO_COPTS = ("-force-single-frontend-invocation", "-whole-module-optimization", "-wmo")
@@ -276,25 +277,25 @@ def new_objc_provider(
276277
Returns:
277278
An `apple_common.Objc` provider that should be returned by the calling rule.
278279
"""
279-
objc_providers = [dep[apple_common.Objc] for dep in deps if apple_common.Objc in dep]
280+
objc_providers = get_providers(deps, apple_common.Objc)
280281
objc_provider_args = {
281-
"library": depset(direct = static_archives),
282282
"link_inputs": depset(direct = swiftmodules + link_inputs),
283283
"providers": objc_providers,
284284
"uses_swift": True,
285285
}
286286

287-
# Collect static libraries that came from cc_library targets, and include them in the
288-
# Objective-C provider. This ensures that they get linked into apple_binaries, which otherwise
289-
# wouldn't be able to see them.
287+
# The link action registered by `apple_binary` only looks at `Objc` providers, not `CcInfo`,
288+
# for libraries to link. Until that rule is migrated over, we need to collect libraries from
289+
# `CcInfo` (which will include Swift and C++) and put them into the new `Objc` provider.
290290
transitive_cc_libs = []
291-
for dep in deps:
292-
if SwiftCcLibsInfo in dep:
293-
cc_libraries = dep[SwiftCcLibsInfo].libraries
294-
if cc_libraries:
295-
transitive_cc_libs.append(cc_libraries)
296-
if transitive_cc_libs:
297-
objc_provider_args["imported_library"] = depset(transitive = transitive_cc_libs)
291+
for cc_info in get_providers(deps, CcInfo):
292+
static_libs = collect_cc_libraries(cc_info = cc_info, include_static = True)
293+
transitive_cc_libs.append(depset(static_libs, order = "topological"))
294+
objc_provider_args["library"] = depset(
295+
static_archives,
296+
transitive = transitive_cc_libs,
297+
order = "topological",
298+
)
298299

299300
if include_path:
300301
objc_provider_args["include"] = depset(direct = [include_path])
@@ -343,7 +344,7 @@ def objc_compile_requirements(args, deps):
343344
static_framework_names = []
344345
all_frameworks = []
345346

346-
objc_providers = [dep[apple_common.Objc] for dep in deps if apple_common.Objc in dep]
347+
objc_providers = get_providers(deps, apple_common.Objc)
347348

348349
post_framework_cleanup = framework_migration.is_post_framework_migration()
349350

swift/internal/deps.bzl

Lines changed: 0 additions & 143 deletions
This file was deleted.

0 commit comments

Comments
 (0)