Skip to content

Commit edf2b45

Browse files
allevatobrentleyjones
authored andcommitted
Allow the Swift toolchain to be associated with an execution group and pass an optional execution group name down to actions that use it
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 (cherry picked from commit aeee2bb) Signed-off-by: Brentley Jones <[email protected]>
1 parent 24f42ed commit edf2b45

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

swift/internal/actions.bzl

+5
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ def is_action_enabled(action_name, swift_toolchain):
120120
return bool(tool_config)
121121

122122
def run_toolchain_action(
123+
*,
123124
actions,
124125
action_name,
126+
exec_group = None,
125127
feature_configuration,
126128
prerequisites,
127129
swift_toolchain,
@@ -133,6 +135,8 @@ def run_toolchain_action(
133135
actions: The rule context's `Actions` object, which will be used to
134136
create `Args` objects.
135137
action_name: The name of the action that should be run.
138+
exec_group: Runs the Swift compilation action under the given execution
139+
group's context. If `None`, the default execution group is used.
136140
feature_configuration: A feature configuration obtained from
137141
`swift_common.configure_features`.
138142
mnemonic: The mnemonic to associate with the action. If not provided,
@@ -206,6 +210,7 @@ def run_toolchain_action(
206210
actions.run(
207211
arguments = [tool_executable_args, args],
208212
env = tool_config.env,
213+
exec_group = exec_group,
209214
executable = executable,
210215
execution_requirements = execution_requirements,
211216
inputs = depset(

swift/internal/compiling.bzl

+17
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ def compile_module_interface(
204204
*,
205205
actions,
206206
compilation_contexts,
207+
exec_group = None,
207208
feature_configuration,
208209
module_name,
209210
swiftinterface_file,
@@ -219,6 +220,8 @@ def compile_module_interface(
219220
Swift-compatible preprocessor defines, header search paths, and so
220221
forth. These are typically retrieved from the `CcInfo` providers of
221222
a target's dependencies.
223+
exec_group: Runs the Swift compilation action under the given execution
224+
group's context. If `None`, the default execution group is used.
222225
feature_configuration: A feature configuration obtained from
223226
`swift_common.configure_features`.
224227
module_name: The name of the Swift module being compiled. This must be
@@ -327,6 +330,7 @@ def compile_module_interface(
327330
run_toolchain_action(
328331
actions = actions,
329332
action_name = SWIFT_ACTION_COMPILE_MODULE_INTERFACE,
333+
exec_group = exec_group,
330334
feature_configuration = feature_configuration,
331335
outputs = [swiftmodule_file],
332336
prerequisites = prerequisites,
@@ -360,6 +364,7 @@ def compile(
360364
cc_infos,
361365
copts = [],
362366
defines = [],
367+
exec_group = None,
363368
extra_swift_infos = [],
364369
feature_configuration,
365370
generated_header_name = None,
@@ -392,6 +397,8 @@ def compile(
392397
determine whether whole module optimization is being requested,
393398
which affects the nature of the output files.
394399
defines: Symbols that should be defined by passing `-D` to the compiler.
400+
exec_group: Runs the Swift compilation action under the given execution
401+
group's context. If `None`, the default execution group is used.
395402
extra_swift_infos: Extra `SwiftInfo` providers that aren't contained
396403
by the `deps` of the target being compiled but are required for
397404
compilation.
@@ -708,6 +715,7 @@ to use swift_common.compile(include_dev_srch_paths = ...) instead.\
708715
run_toolchain_action(
709716
actions = actions,
710717
action_name = SWIFT_ACTION_COMPILE,
718+
exec_group = exec_group,
711719
feature_configuration = feature_configuration,
712720
outputs = all_compile_outputs,
713721
prerequisites = prerequisites,
@@ -753,6 +761,7 @@ to use swift_common.compile(include_dev_srch_paths = ...) instead.\
753761
precompiled_module = _precompile_clang_module(
754762
actions = actions,
755763
cc_compilation_context = compilation_context_to_compile,
764+
exec_group = exec_group,
756765
feature_configuration = feature_configuration,
757766
is_swift_generated_header = True,
758767
module_map_file = compile_outputs.generated_module_map_file,
@@ -836,6 +845,7 @@ def precompile_clang_module(
836845
*,
837846
actions,
838847
cc_compilation_context,
848+
exec_group = None,
839849
feature_configuration,
840850
module_map_file,
841851
module_name,
@@ -855,6 +865,8 @@ def precompile_clang_module(
855865
of headers of the direct dependencies of the module being compiled,
856866
which Clang needs to be physically present before it detects that
857867
they belong to one of the precompiled module dependencies.
868+
exec_group: Runs the Swift compilation action under the given execution
869+
group's context. If `None`, the default execution group is used.
858870
feature_configuration: A feature configuration obtained from
859871
`swift_common.configure_features`.
860872
module_map_file: A textual module map file that defines the Clang module
@@ -875,6 +887,7 @@ def precompile_clang_module(
875887
return _precompile_clang_module(
876888
actions = actions,
877889
cc_compilation_context = cc_compilation_context,
890+
exec_group = exec_group,
878891
feature_configuration = feature_configuration,
879892
is_swift_generated_header = False,
880893
module_map_file = module_map_file,
@@ -888,6 +901,7 @@ def _precompile_clang_module(
888901
*,
889902
actions,
890903
cc_compilation_context,
904+
exec_group = None,
891905
feature_configuration,
892906
is_swift_generated_header,
893907
module_map_file,
@@ -908,6 +922,8 @@ def _precompile_clang_module(
908922
of headers of the direct dependencies of the module being compiled,
909923
which Clang needs to be physically present before it detects that
910924
they belong to one of the precompiled module dependencies.
925+
exec_group: Runs the Swift compilation action under the given execution
926+
group's context. If `None`, the default execution group is used.
911927
feature_configuration: A feature configuration obtained from
912928
`swift_common.configure_features`.
913929
is_swift_generated_header: If True, the action is compiling the
@@ -989,6 +1005,7 @@ def _precompile_clang_module(
9891005
run_toolchain_action(
9901006
actions = actions,
9911007
action_name = SWIFT_ACTION_PRECOMPILE_C_MODULE,
1008+
exec_group = exec_group,
9921009
feature_configuration = feature_configuration,
9931010
outputs = [precompiled_module],
9941011
prerequisites = prerequisites,

swift/internal/toolchain_utils.bzl

+13-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616

1717
SWIFT_TOOLCHAIN_TYPE = "@build_bazel_rules_swift//toolchains:toolchain_type"
1818

19-
def get_swift_toolchain(ctx, attr = "_toolchain"):
19+
def get_swift_toolchain(ctx, *, exec_group = None, attr = "_toolchain"):
2020
"""Gets the Swift toolchain associated with the rule or aspect.
2121
2222
Args:
2323
ctx: The rule or aspect context.
24+
exec_group: The name of the execution group that should contain the
25+
toolchain. If this is provided and the toolchain is not declared in
26+
that execution group, it will be looked up from `ctx` as a fallback
27+
instead. If this argument is `None` (the default), then the
28+
toolchain will only be looked up from `ctx.`
2429
attr: The name of the attribute on the calling rule or aspect that
2530
should be used to retrieve the toolchain if it is not provided by
2631
the `toolchains` argument of the rule/aspect. Note that this is only
@@ -30,6 +35,11 @@ def get_swift_toolchain(ctx, attr = "_toolchain"):
3035
Returns:
3136
A `SwiftToolchainInfo` provider.
3237
"""
38+
if exec_group:
39+
group = ctx.exec_groups[exec_group]
40+
if group and SWIFT_TOOLCHAIN_TYPE in group.toolchains:
41+
return group.toolchains[SWIFT_TOOLCHAIN_TYPE].swift_toolchain
42+
3343
if SWIFT_TOOLCHAIN_TYPE in ctx.toolchains:
3444
return ctx.toolchains[SWIFT_TOOLCHAIN_TYPE].swift_toolchain
3545

@@ -55,8 +65,8 @@ def use_swift_toolchain():
5565
```
5666
5767
Returns:
58-
A list of toolchain types that should be passed to `rule()` or
59-
`aspect()`.
68+
A list of toolchain types that should be passed to `rule()`, `aspect()`,
69+
or `exec_group`.
6070
"""
6171

6272
# TODO(b/205018581): Intentionally empty for now so that rule definitions

0 commit comments

Comments
 (0)