Implement TODO from: https://github.com/bazelbuild/bazel/blo[]fbbd6a32b95ba746f09dae1eaeaccf675cd5b3/src/main/java/com/google/devtools/build/lib/packages/Attribute.java#L1045
This allows the default value computation for latebound attributes to consider
the values of configurable attributes. This is most directly useful for user-definable Skylark defaults, which have full access to the values of all non-latebound attributes.
Without this change, this kind of scenario crashes Bazel. For example:
------------------
select_rules.bzl:
------------------
def _impl(ctx):
ctx.file_action(
output=ctx.outputs.out_file,
content=ctx.attr.string_value,
)
return struct()
# Bug does not manifest without using this as a default.
def _derived_value(attrs, _):
return Label("//some:dep")
selector_rule = rule(
implementation=_impl,
attrs={
"string_value": attr.string(default=""),
"out_file": attr.output(),
"_derived": attr.label(default=_derived_value),
},
output_to_genfiles=True,
)
def selector_macro(name, out_file="", string_value=""):
# This will fail with selectors.
selector_rule(
name="%s_skylark" % name,
string_value=string_value,
out_file=out_file + ".skylark",
)
# This does not.
native.genrule(
name="%s_genrule" % name,
cmd="echo '" + string_value + "' > $@",
outs=[out_file + ".genrule"],
)
native.filegroup(
name=name,
srcs=[":%s_genrule" % name, "%s_skylark" % name],
)
------------------
BUILD.bzl:
------------------
config_setting(
name = "selector",
values = {"compilation_mode": "opt"},
)
selector_macro(
name = "this_rule",
string_value = """soup? """ + select({
":selector": "no, thank you.",
"//conditions:default": "yes, please!!",
}),
out_file = "this_rule.txt",
)
--
MOS_MIGRATED_REVID=114326474
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/BaseRuleClasses.java b/src/main/java/com/google/devtools/build/lib/analysis/BaseRuleClasses.java
index 5b9347c..3f277d1 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/BaseRuleClasses.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/BaseRuleClasses.java
@@ -81,7 +81,8 @@
static final LateBoundLabelList<BuildConfiguration> ACTION_LISTENER =
new LateBoundLabelList<BuildConfiguration>() {
@Override
- public List<Label> getDefault(Rule rule, BuildConfiguration configuration) {
+ public List<Label> getDefault(Rule rule, AttributeMap attributes,
+ BuildConfiguration configuration) {
// action_listeners are special rules; they tell the build system to add extra_actions to
// existing rules. As such they need an edge to every ConfiguredTarget with the limitation
// that they only run on the target configuration and should not operate on action_listeners
@@ -93,7 +94,8 @@
private static final LateBoundLabelList<BuildConfiguration> COVERAGE_SUPPORT =
new LateBoundLabelList<BuildConfiguration>(ImmutableList.of(COVERAGE_SUPPORT_LABEL)) {
@Override
- public List<Label> getDefault(Rule rule, BuildConfiguration configuration) {
+ public List<Label> getDefault(Rule rule, AttributeMap attributes,
+ BuildConfiguration configuration) {
return configuration.isCodeCoverageEnabled()
? ImmutableList.copyOf(configuration.getCoverageLabels())
: ImmutableList.<Label>of();
@@ -103,7 +105,8 @@
private static final LateBoundLabelList<BuildConfiguration> GCOV =
new LateBoundLabelList<BuildConfiguration>(ImmutableList.of(COVERAGE_SUPPORT_LABEL)) {
@Override
- public List<Label> getDefault(Rule rule, BuildConfiguration configuration) {
+ public List<Label> getDefault(Rule rule, AttributeMap attributes,
+ BuildConfiguration configuration) {
return configuration.isCodeCoverageEnabled()
? ImmutableList.copyOf(configuration.getGcovLabels())
: ImmutableList.<Label>of();
@@ -113,7 +116,8 @@
private static final LateBoundLabelList<BuildConfiguration> COVERAGE_REPORT_GENERATOR =
new LateBoundLabelList<BuildConfiguration>(ImmutableList.of(COVERAGE_SUPPORT_LABEL)) {
@Override
- public List<Label> getDefault(Rule rule, BuildConfiguration configuration) {
+ public List<Label> getDefault(Rule rule, AttributeMap attributes,
+ BuildConfiguration configuration) {
return configuration.isCodeCoverageEnabled()
? ImmutableList.copyOf(configuration.getCoverageReportGeneratorLabels())
: ImmutableList.<Label>of();
@@ -126,7 +130,8 @@
private static final LateBoundLabel<BuildConfiguration> RUN_UNDER =
new LateBoundLabel<BuildConfiguration>() {
@Override
- public Label getDefault(Rule rule, BuildConfiguration configuration) {
+ public Label getDefault(Rule rule, AttributeMap attributes,
+ BuildConfiguration configuration) {
RunUnder runUnder = configuration.getRunUnder();
return runUnder == null ? null : runUnder.getLabel();
}