From aeee2bb27d1a640705f155038cbdb8c50eeace9d Mon Sep 17 00:00:00 2001 From: Tony Allevato Date: Tue, 17 Jan 2023 11:16:43 -0800 Subject: [PATCH] Allow the Swift toolchain to be associated with an execution group and pass an optional execution group name down to actions that use it. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This updates the following APIs: - `swift_common.get_toolchain` now takes an `exec_group` argument, which is will look up the toolchain under `ctx.exec_groups[NAME]` instead of `ctx`. - `swift_common.{compile,compile_module_interface,precompile_clang_module}` now take an `exec_group` argument, which is passed down to the underlying `ctx.actions.run` call for actions using the toolchain. In all cases the default value of the argument is `None`, so omitting it retains the current behavior. Most importantly, this lets us declare the Swift toolchain somewhere other than the rule/aspect's `ctx`, which ensures that in a multiple toolchain scenario, the Swift toolchain doesn't fully decide the execution platform for all actions (even non-Swift actions). Since execution groups are somewhat in flux right now, this may not be the final form—it would be nice to have the coupling between the execution group and the toolchain automated. I considered just having each action be its own execution group (like C++ link actions use `cpp_link`), but that seems worse; Swift actions should almost never be split across different toolchains/platforms, and it would force the user to have to declare multiple exec groups if they wanted to use the feature with multiple actions. This lets them share something like a single "swift" execution group across multiple actions, with the only caveat being that they have to pass that name explicitly. PiperOrigin-RevId: 502638394 --- swift/internal/actions.bzl | 5 +++++ swift/internal/compiling.bzl | 17 +++++++++++++++++ swift/internal/toolchain_utils.bzl | 16 +++++++++++++--- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/swift/internal/actions.bzl b/swift/internal/actions.bzl index 2e074c8f4..d37ab656d 100644 --- a/swift/internal/actions.bzl +++ b/swift/internal/actions.bzl @@ -119,8 +119,10 @@ def is_action_enabled(action_name, swift_toolchain): return bool(tool_config) def run_toolchain_action( + *, actions, action_name, + exec_group = None, feature_configuration, prerequisites, swift_toolchain, @@ -132,6 +134,8 @@ def run_toolchain_action( actions: The rule context's `Actions` object, which will be used to create `Args` objects. action_name: The name of the action that should be run. + exec_group: Runs the Swift compilation action under the given execution + group's context. If `None`, the default execution group is used. feature_configuration: A feature configuration obtained from `swift_common.configure_features`. mnemonic: The mnemonic to associate with the action. If not provided, @@ -204,6 +208,7 @@ def run_toolchain_action( actions.run( arguments = [tool_executable_args, args], env = tool_config.env, + exec_group = exec_group, executable = executable, execution_requirements = execution_requirements, input_manifests = tool_config.tool_input_manifests, diff --git a/swift/internal/compiling.bzl b/swift/internal/compiling.bzl index 3fcec2fe4..2bd59fda9 100644 --- a/swift/internal/compiling.bzl +++ b/swift/internal/compiling.bzl @@ -70,6 +70,7 @@ def compile_module_interface( clang_module = None, compilation_contexts, copts = [], + exec_group = None, feature_configuration, module_name, swiftinterface_file, @@ -87,6 +88,8 @@ def compile_module_interface( forth. These are typically retrieved from the `CcInfo` providers of a target's dependencies. copts: A list of compiler flags that apply to the target being built. + exec_group: Runs the Swift compilation action under the given execution + group's context. If `None`, the default execution group is used. feature_configuration: A feature configuration obtained from `swift_common.configure_features`. module_name: The name of the Swift module being compiled. This must be @@ -171,6 +174,7 @@ def compile_module_interface( run_toolchain_action( actions = actions, action_name = SWIFT_ACTION_COMPILE_MODULE_INTERFACE, + exec_group = exec_group, feature_configuration = feature_configuration, outputs = [swiftmodule_file], prerequisites = prerequisites, @@ -204,6 +208,7 @@ def compile( compilation_contexts, copts = [], defines = [], + exec_group = None, feature_configuration, generated_header_name = None, module_name, @@ -230,6 +235,8 @@ def compile( determine whether whole module optimization is being requested, which affects the nature of the output files. defines: Symbols that should be defined by passing `-D` to the compiler. + exec_group: Runs the Swift compilation action under the given execution + group's context. If `None`, the default execution group is used. feature_configuration: A feature configuration obtained from `swift_common.configure_features`. generated_header_name: The name of the Objective-C generated header that @@ -444,6 +451,7 @@ def compile( run_toolchain_action( actions = actions, action_name = SWIFT_ACTION_COMPILE, + exec_group = exec_group, feature_configuration = feature_configuration, outputs = all_compile_outputs, prerequisites = prerequisites, @@ -474,6 +482,7 @@ def compile( pcm_outputs = _precompile_clang_module( actions = actions, cc_compilation_context = compilation_context_to_compile, + exec_group = exec_group, feature_configuration = feature_configuration, is_swift_generated_header = True, module_map_file = compile_outputs.generated_module_map_file, @@ -536,6 +545,7 @@ def precompile_clang_module( *, actions, cc_compilation_context, + exec_group = None, feature_configuration, module_map_file, module_name, @@ -555,6 +565,8 @@ def precompile_clang_module( of headers of the direct dependencies of the module being compiled, which Clang needs to be physically present before it detects that they belong to one of the precompiled module dependencies. + exec_group: Runs the Swift compilation action under the given execution + group's context. If `None`, the default execution group is used. feature_configuration: A feature configuration obtained from `swift_common.configure_features`. module_map_file: A textual module map file that defines the Clang module @@ -575,6 +587,7 @@ def precompile_clang_module( return _precompile_clang_module( actions = actions, cc_compilation_context = cc_compilation_context, + exec_group = exec_group, feature_configuration = feature_configuration, is_swift_generated_header = False, module_map_file = module_map_file, @@ -588,6 +601,7 @@ def _precompile_clang_module( *, actions, cc_compilation_context, + exec_group = None, feature_configuration, is_swift_generated_header, module_map_file, @@ -608,6 +622,8 @@ def _precompile_clang_module( of headers of the direct dependencies of the module being compiled, which Clang needs to be physically present before it detects that they belong to one of the precompiled module dependencies. + exec_group: Runs the Swift compilation action under the given execution + group's context. If `None`, the default execution group is used. feature_configuration: A feature configuration obtained from `swift_common.configure_features`. is_swift_generated_header: If True, the action is compiling the @@ -706,6 +722,7 @@ def _precompile_clang_module( run_toolchain_action( actions = actions, action_name = SWIFT_ACTION_PRECOMPILE_C_MODULE, + exec_group = exec_group, feature_configuration = feature_configuration, outputs = outputs, prerequisites = prerequisites, diff --git a/swift/internal/toolchain_utils.bzl b/swift/internal/toolchain_utils.bzl index cf24ba614..d2d830dbe 100644 --- a/swift/internal/toolchain_utils.bzl +++ b/swift/internal/toolchain_utils.bzl @@ -16,11 +16,16 @@ SWIFT_TOOLCHAIN_TYPE = "@build_bazel_rules_swift//toolchains:toolchain_type" -def get_swift_toolchain(ctx, attr = "_toolchain"): +def get_swift_toolchain(ctx, *, exec_group = None, attr = "_toolchain"): """Gets the Swift toolchain associated with the rule or aspect. Args: ctx: The rule or aspect context. + exec_group: The name of the execution group that should contain the + toolchain. If this is provided and the toolchain is not declared in + 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.` 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 @@ -30,6 +35,11 @@ def get_swift_toolchain(ctx, attr = "_toolchain"): Returns: A `SwiftToolchainInfo` provider. """ + if exec_group: + group = ctx.exec_groups[exec_group] + if group and SWIFT_TOOLCHAIN_TYPE in group.toolchains: + return group.toolchains[SWIFT_TOOLCHAIN_TYPE].swift_toolchain + if SWIFT_TOOLCHAIN_TYPE in ctx.toolchains: return ctx.toolchains[SWIFT_TOOLCHAIN_TYPE].swift_toolchain @@ -55,8 +65,8 @@ def use_swift_toolchain(): ``` Returns: - A list of toolchain types that should be passed to `rule()` or - `aspect()`. + A list of toolchain types that should be passed to `rule()`, `aspect()`, + or `exec_group`. """ # TODO(b/205018581): Intentionally empty for now so that rule definitions