From 24e3b9b553d253102b5d2e6271dcec728098fed1 Mon Sep 17 00:00:00 2001 From: juliexxia Date: Mon, 2 Nov 2020 12:45:22 -0800 Subject: [PATCH] API change discussed in https://github.com/bazelbuild/bazel/pull/12006. Allow users to set exec_group(copy_from_rule=true) on starlark exec groups which signifies that this exec group inherits toolchains and constraints from the rule to which it's attached. Implementation detail: When processing parent rules, if parent rules have exec groups that are marked as inheriting from the rule, make them empty before adding them to the child rule. That way they can later be re-filled with the appropriate child rule's requirements instead of having the parent rule's requirements. PiperOrigin-RevId: 340295132 --- site/docs/exec-groups.md | 36 ++++++++++++++--- .../starlark/StarlarkRuleClassFunctions.java | 13 ++++++- .../build/lib/packages/ExecGroup.java | 22 ++++++++++- .../build/lib/packages/RuleClass.java | 39 +++++++++++++------ .../StarlarkRuleFunctionsApi.java | 14 ++++++- .../FakeStarlarkRuleFunctionsApi.java | 5 ++- .../lib/analysis/StarlarkExecGroupTest.java | 6 +-- .../lib/packages/RuleClassBuilderTest.java | 27 ++++++++----- 8 files changed, 128 insertions(+), 34 deletions(-) diff --git a/site/docs/exec-groups.md b/site/docs/exec-groups.md index 01f637dbbcc31e..1b2a812bbafb53 100644 --- a/site/docs/exec-groups.md +++ b/site/docs/exec-groups.md @@ -38,10 +38,7 @@ During rule definition, rule authors can a set of execution groups. On each execution group, the rule author can specify everything needed to select an execution platform for that execution group, namely any constraints via `exec_compatible_with` and toolchain types via -`toolchain`. If an execution group is created as empty (no specified toolchains -or constraints) it will automatically inherit these -[parameters](https://docs.bazel.build/versions/master/skylark/lib/globals.html#rule) -from the rule to which the group is attached. +`toolchain`. ```python # foo.bzl @@ -51,7 +48,6 @@ my_rule = rule( “link”: exec_group( exec_compatible_with = [ "@platforms//os:linux" ] toolchains = ["//foo:toolchain_type"], - ), “test”: exec_group( toolchains = ["//foo_tools:toolchain_type"], @@ -136,3 +132,33 @@ All actions with `exec_group = "link"` would see the exec properties dictionary as `{"mem": "16g"}`. As you see here, execution-group-level settings override target-level settings. +### Creating exec groups to set exec properties + +Sometimes you want to use an exec group to give specific actions different exec +properties but don't actually want different toolchains or constraints than the +rule. For these situations, you can create exec groups using the `copy_from_rule` +parameter: + +```python +# foo.bzl + +# Creating an exec group with `copy_from_rule=True` is the same as explicitly +# setting the exec group's toolchains and constraints to the same values as the +# rule's respective parameters. +my_rule = rule( + _impl, + exec_compatible_with = [ "@platforms//os:linux" ], + toolchains = ["//foo:toolchain_type"], + exec_groups = { + # The following two groups have the same toolchains and constraints: + “foo”: exec_group(copy_from_rule = True), + "bar": exec_group( + exec_compatible_with = [ "@platforms//os:linux" ], + toolchains = ["//foo:toolchain_type"], + ), + }, +) + +# +``` + diff --git a/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java b/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java index 8d26841001f72c..4e112a245f72b7 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java @@ -927,8 +927,19 @@ public Label label(String labelString, Boolean relativeToCallerRepository, Starl @Override public ExecGroup execGroup( - Sequence toolchains, Sequence execCompatibleWith, StarlarkThread thread) + Sequence toolchains, + Sequence execCompatibleWith, + Boolean copyFromRule, + StarlarkThread thread) throws EvalException { + if (copyFromRule) { + if (!toolchains.isEmpty() || !execCompatibleWith.isEmpty()) { + throw Starlark.errorf( + "An exec group cannot set copy_from_rule=True and declare toolchains or constraints."); + } + return ExecGroup.COPY_FROM_RULE_EXEC_GROUP; + } + ImmutableSet