Skip to content

Commit

Permalink
Allow rules/aspects to optionally depend on the Swift toolchain
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 634762729
(cherry picked from commit 0300b69)
Signed-off-by: Brentley Jones <github@brentleyjones.com>
  • Loading branch information
allevato authored and brentleyjones committed Oct 14, 2024
1 parent fbf0830 commit b681e92
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions swift/internal/toolchain_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

SWIFT_TOOLCHAIN_TYPE = "@build_bazel_rules_swift//toolchains:toolchain_type"

def get_swift_toolchain(ctx, *, exec_group = None, attr = "_toolchain"):
def get_swift_toolchain(
ctx,
*,
exec_group = None,
mandatory = True,
attr = "_toolchain"):
"""Gets the Swift toolchain associated with the rule or aspect.
Args:
Expand All @@ -26,14 +31,17 @@ def get_swift_toolchain(ctx, *, exec_group = None, attr = "_toolchain"):
that execution group, it will be looked up from `ctx` as a fallback
instead. If this argument is `None` (the default), then the
toolchain will only be looked up from `ctx.`
mandatory: If `False`, this function will return `None` instead of
failing if no toolchain is found. Defaults to `True`.
attr: The name of the attribute on the calling rule or aspect that
should be used to retrieve the toolchain if it is not provided by
the `toolchains` argument of the rule/aspect. Note that this is only
supported for legacy/migration purposes and will be removed once
migration to toolchains is complete.
Returns:
A `SwiftToolchainInfo` provider.
A `SwiftToolchainInfo` provider, or `None` if the toolchain was not
found and not required.
"""
if exec_group:
group = ctx.exec_groups[exec_group]
Expand All @@ -49,10 +57,14 @@ def get_swift_toolchain(ctx, *, exec_group = None, attr = "_toolchain"):
if toolchain_target and platform_common.ToolchainInfo in toolchain_target:
return toolchain_target[platform_common.ToolchainInfo].swift_toolchain

fail("To use `get_swift_toolchain`, you must declare the toolchain in " +
"your rule using `toolchains = use_swift_toolchain()`.")
if mandatory:
fail("To use `swift_common.get_toolchain`, you must declare the " +
"toolchain in your rule using " +
"`toolchains = swift_common.use_toolchain()`.")

def use_swift_toolchain():
return None

def use_swift_toolchain(*, mandatory = True):
"""Returns a list of toolchain types needed to use the Swift toolchain.
This function returns a list so that it can be easily composed with other
Expand All @@ -63,8 +75,18 @@ def use_swift_toolchain():
toolchains = use_swift_toolchain() + [other toolchains...]
```
Args:
mandatory: Whether or not it should be an error if the toolchain cannot
be resolved. Defaults to True.
Returns:
A list of toolchain types that should be passed to `rule()`, `aspect()`,
or `exec_group`.
A list of
[toolchain types](https://bazel.build/rules/lib/builtins/toolchain_type.html)
that should be passed to `rule()`, `aspect()`, or `exec_group()`.
"""
return [SWIFT_TOOLCHAIN_TYPE]
return [
config_common.toolchain_type(
SWIFT_TOOLCHAIN_TYPE,
mandatory = mandatory,
),
]

0 comments on commit b681e92

Please sign in to comment.