diff --git a/doc/api.md b/doc/api.md index fa78c9e0f..feca0f33a 100644 --- a/doc/api.md +++ b/doc/api.md @@ -463,6 +463,36 @@ A `SwiftToolchainInfo` provider, or `None` if the toolchain was not found and not required. + + +## swift_common.is_action_enabled + +
+swift_common.is_action_enabled(action_name, swift_toolchain)
+
+ +Returns True if the given action is enabled in the Swift toolchain. + +This function should be used before invoking APIs that invoke actions that +might not be available depending on the version of the Swift toolchain. For +example, `SwiftSynthesizeInterface` actions (created by calling +`swift_common.synthesize_interface`) are only available starting from Swift +6.1. + + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| action_name | The name of the action, which corresponds to the action's mnemonic (for example, `SwiftSynthesizeInterface`). | none | +| swift_toolchain | The Swift toolchain being used to build. | none | + +**RETURNS** + +True if the action is enabled, or False if it is not. + + ## swift_common.is_enabled diff --git a/swift/BUILD b/swift/BUILD index b0a262d00..17c8149bb 100644 --- a/swift/BUILD +++ b/swift/BUILD @@ -94,6 +94,7 @@ bzl_library( name = "swift_common", srcs = ["swift_common.bzl"], deps = [ + "//swift/internal:actions", "//swift/internal:compiling", "//swift/internal:features", "//swift/internal:interface_synthesizing", diff --git a/swift/internal/actions.bzl b/swift/internal/actions.bzl index 0843c543a..f01d6d18d 100644 --- a/swift/internal/actions.bzl +++ b/swift/internal/actions.bzl @@ -116,8 +116,15 @@ def _apply_action_configs( def is_action_enabled(action_name, swift_toolchain): """Returns True if the given action is enabled in the Swift toolchain. + This function should be used before invoking APIs that invoke actions that + might not be available depending on the version of the Swift toolchain. For + example, `SwiftSynthesizeInterface` actions (created by calling + `swift_common.synthesize_interface`) are only available starting from Swift + 6.1. + Args: - action_name: The name of the action. + action_name: The name of the action, which corresponds to the action's + mnemonic (for example, `SwiftSynthesizeInterface`). swift_toolchain: The Swift toolchain being used to build. Returns: diff --git a/swift/internal/toolchain_utils.bzl b/swift/internal/toolchain_utils.bzl index 2b0fbe375..ba4a97b85 100644 --- a/swift/internal/toolchain_utils.bzl +++ b/swift/internal/toolchain_utils.bzl @@ -51,7 +51,7 @@ def get_swift_toolchain( if group and toolchain_type in group.toolchains: return group.toolchains[toolchain_type].swift_toolchain - if toolchain_type in ctx.toolchains: + if toolchain_type in ctx.toolchains and ctx.toolchains[toolchain_type]: return ctx.toolchains[toolchain_type].swift_toolchain # TODO(b/205018581): Delete this code path when migration to the new diff --git a/swift/providers.bzl b/swift/providers.bzl index e0e069ba1..f7ab88f9e 100644 --- a/swift/providers.bzl +++ b/swift/providers.bzl @@ -83,6 +83,18 @@ def _swift_info_init( direct_swift_infos = [], modules = [], swift_infos = []): + """Creates a `SwiftInfo` provider from the given arguments. + + Args: + direct_swift_infos: A list of `SwiftInfo` providers from dependencies + whose direct modules should be treated as direct modules in the resulting + provider, in addition to their transitive modules being merged. + modules: A list of values (as returned by `create_swift_module_context()`) + that represent Clang and/or Swift module artifacts that are direct outputs + of the target being built. + swift_infos: A list of `SwiftInfo` providers from dependencies whose + transitive modules should be merged into the resulting provider. + """ direct_modules = modules + [ module for provider in direct_swift_infos diff --git a/swift/swift_common.bzl b/swift/swift_common.bzl index 107754deb..132ad3994 100644 --- a/swift/swift_common.bzl +++ b/swift/swift_common.bzl @@ -20,6 +20,10 @@ example, `swift_proto_library` generates Swift source code from `.proto` files and then needs to compile them. This module provides that lower-level interface. """ +load( + "//swift/internal:actions.bzl", + "is_action_enabled", +) load( "//swift/internal:compiling.bzl", "compile", @@ -62,6 +66,7 @@ swift_common = struct( create_linking_context_from_compilation_outputs = create_linking_context_from_compilation_outputs, extract_symbol_graph = extract_symbol_graph, get_toolchain = get_swift_toolchain, + is_action_enabled = is_action_enabled, is_enabled = is_feature_enabled, precompile_clang_module = precompile_clang_module, synthesize_interface = synthesize_interface,