Skip to content

Commit

Permalink
Simplify swift_library_group (#1292)
Browse files Browse the repository at this point in the history
While working on something else I realized that the code for
`swift_library_group` was more complex than it needed to be.

Signed-off-by: Brentley Jones <github@brentleyjones.com>
  • Loading branch information
brentleyjones authored Jul 18, 2024
1 parent 66e5842 commit 293fb3c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 54 deletions.
2 changes: 1 addition & 1 deletion doc/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ need to import the grouped libraries directly.
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="swift_library_group-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="swift_library_group-deps"></a>deps | A list of targets that should be included in the group.<br><br>Allowed kinds of dependencies are:<br><br>* `swift_library` (or anything propagating `SwiftInfo`)<br><br>* `cc_library` (or anything propagating `CcInfo`)<br><br>Additionally, on platforms that support Objective-C interop, `objc_library` targets (or anything propagating the `apple_common.Objc` provider) are allowed as dependencies. On platforms that do not support Objective-C interop (such as Linux), those dependencies will be **ignored.** | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="swift_library_group-deps"></a>deps | A list of targets that should be included in the group. Allowed kinds of dependencies are:<br><br>* `swift_library` (or anything propagating `SwiftInfo`)<br><br>* `cc_library` (or anything propagating `CcInfo`)<br><br>Additionally, on platforms that support Objective-C interop, `objc_library` targets (or anything propagating the `apple_common.Objc` provider) are allowed as dependencies. On platforms that do not support Objective-C interop (such as Linux), those dependencies will be **ignored.** | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |


<a id="swift_module_alias"></a>
Expand Down
4 changes: 1 addition & 3 deletions swift/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,9 @@ bzl_library(
deps = [
":providers",
":swift_clang_module_aspect",
":swift_common",
"//swift/internal:attrs",
"//swift/internal:toolchain_utils",
"//swift/internal:providers",
"//swift/internal:utils",
"@bazel_skylib//lib:dicts",
],
)

Expand Down
62 changes: 12 additions & 50 deletions swift/swift_library_group.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,25 @@

"""Implementation of the `swift_library_group` rule."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("//swift/internal:attrs.bzl", "swift_deps_attr", "swift_toolchain_attrs")
load("//swift/internal:toolchain_utils.bzl", "use_swift_toolchain")
load("//swift/internal:attrs.bzl", "swift_deps_attr")
load("//swift/internal:providers.bzl", "create_swift_info")
load("//swift/internal:utils.bzl", "get_providers")
load(":providers.bzl", "SwiftInfo")
load(":swift_clang_module_aspect.bzl", "swift_clang_module_aspect")
load(":swift_common.bzl", "swift_common")

def _swift_library_group_impl(ctx):
deps = ctx.attr.deps
deps_cc_infos = [
dep[CcInfo]
for dep in deps
if CcInfo in dep
]
swift_toolchain = swift_common.get_toolchain(ctx)

return [
DefaultInfo(),
CcInfo(
compilation_context = cc_common.merge_cc_infos(
cc_infos = deps_cc_infos,
).compilation_context,
linking_context = cc_common.merge_linking_contexts(
linking_contexts = [
cc_info.linking_context
for cc_info in (
deps_cc_infos +
swift_toolchain.implicit_deps_providers.cc_infos
)
] + [
cc_common.create_linking_context(
linker_inputs = depset(
direct = [
cc_common.create_linker_input(
owner = ctx.label,
),
],
),
),
],
),
cc_common.merge_cc_infos(
cc_infos = [dep[CcInfo] for dep in deps if CcInfo in dep],
),
coverage_common.instrumented_files_info(
ctx,
dependency_attributes = ["deps"],
),
swift_common.create_swift_info(
create_swift_info(
swift_infos = get_providers(deps, SwiftInfo),
),
# Propagate an `apple_common.Objc` provider with linking info about the
Expand All @@ -70,25 +41,17 @@ def _swift_library_group_impl(ctx):
# TODO(b/171413861): This can be removed when the Obj-C rules are
# migrated to use `CcLinkingContext`.
apple_common.new_objc_provider(
providers = get_providers(
deps,
apple_common.Objc,
) + swift_toolchain.implicit_deps_providers.objc_infos,
providers = get_providers(deps, apple_common.Objc),
),
]

swift_library_group = rule(
attrs = dicts.add(
swift_toolchain_attrs(),
{
"deps": swift_deps_attr(
aspects = [swift_clang_module_aspect],
doc = """\
A list of targets that should be included in the group.
""",
),
},
),
attrs = {
"deps": swift_deps_attr(
aspects = [swift_clang_module_aspect],
doc = "A list of targets that should be included in the group.",
),
},
doc = """\
Groups Swift compatible libraries (e.g. `swift_library` and `objc_library`).
The target can be used anywhere a `swift_library` can be used. It behaves
Expand All @@ -98,5 +61,4 @@ Unlike `swift_module_alias`, a new module isn't created for this target, you
need to import the grouped libraries directly.
""",
implementation = _swift_library_group_impl,
toolchains = use_swift_toolchain(),
)

0 comments on commit 293fb3c

Please sign in to comment.