Skip to content

Commit ee57d99

Browse files
AlessandroPatticopybara-github
authored andcommitted
Fix genrule autostamping in bazel
Genrule does not support conditional stamping based on the value of the `--stamp` flag. This is because genrule treats the `stamp` attribute as `boolean`, while all other rules use the `tristate` type. After this change, the stamp attribute in genrule can be set to `-1` to request conditional stamping. RELNOTES: `genrule` now supports setting `stamp = -1` to request conditional stamping (based on the value of the build's `--stamp` flag). Closes #21407. PiperOrigin-RevId: 609085052 Change-Id: I873941e9aaae3760a545c1900cd13cb60a9ada39
1 parent f8337c7 commit ee57d99

4 files changed

Lines changed: 22 additions & 26 deletions

File tree

src/main/java/com/google/devtools/build/lib/bazel/rules/genrule/BazelGenRule.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,15 @@
1818
import com.google.devtools.build.lib.actions.Artifact;
1919
import com.google.devtools.build.lib.analysis.AliasProvider;
2020
import com.google.devtools.build.lib.analysis.FileProvider;
21-
import com.google.devtools.build.lib.analysis.RuleContext;
2221
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
2322
import com.google.devtools.build.lib.cmdline.Label;
2423
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
25-
import com.google.devtools.build.lib.packages.Type;
2624
import com.google.devtools.build.lib.rules.genrule.GenRuleBase;
2725
import java.util.List;
2826

2927
/** An implementation of genrule for Bazel. */
3028
public final class BazelGenRule extends GenRuleBase {
3129

32-
@Override
33-
protected boolean isStampingEnabled(RuleContext ruleContext) {
34-
if (!ruleContext.attributes().has("stamp", Type.BOOLEAN)) {
35-
return false;
36-
}
37-
return ruleContext.attributes().get("stamp", Type.BOOLEAN);
38-
}
39-
4030
@Override
4131
protected ImmutableMap<Label, NestedSet<Artifact>> collectSources(
4232
List<? extends TransitiveInfoCollection> srcs) {

src/main/java/com/google/devtools/build/lib/bazel/rules/genrule/BazelGenRuleRule.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,20 @@
1515

1616
import static com.google.devtools.build.lib.packages.Attribute.attr;
1717
import static com.google.devtools.build.lib.packages.BuildType.LABEL;
18-
import static com.google.devtools.build.lib.packages.Type.BOOLEAN;
18+
import static com.google.devtools.build.lib.packages.BuildType.TRISTATE;
1919

2020
import com.google.devtools.build.lib.analysis.RuleDefinition;
2121
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
2222
import com.google.devtools.build.lib.analysis.config.ExecutionTransitionFactory;
2323
import com.google.devtools.build.lib.packages.RuleClass;
24+
import com.google.devtools.build.lib.packages.TriState;
2425
import com.google.devtools.build.lib.rules.genrule.GenRuleBaseRule;
2526

2627
/**
2728
* Rule definition for genrule for Bazel.
2829
*/
2930
public final class BazelGenRuleRule implements RuleDefinition {
30-
public static final String GENRULE_SETUP_LABEL = "//tools/genrule:genrule-setup.sh";
31+
private static final String GENRULE_SETUP_LABEL = "//tools/genrule:genrule-setup.sh";
3132

3233
@Override
3334
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) {
@@ -43,9 +44,7 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
4344
attr("$genrule_setup", LABEL)
4445
.cfg(ExecutionTransitionFactory.createFactory())
4546
.value(env.getToolsLabel(GENRULE_SETUP_LABEL)))
46-
47-
// TODO(bazel-team): stamping doesn't seem to work. Fix it or remove attribute.
48-
.add(attr("stamp", BOOLEAN).value(false))
47+
.add(attr("stamp", TRISTATE).value(TriState.NO))
4948
.build();
5049
}
5150

src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
4444
import com.google.devtools.build.lib.collect.nestedset.Order;
4545
import com.google.devtools.build.lib.packages.AttributeMap;
46+
import com.google.devtools.build.lib.packages.BuildType;
4647
import com.google.devtools.build.lib.packages.TargetUtils;
48+
import com.google.devtools.build.lib.packages.TriState;
4749
import com.google.devtools.build.lib.packages.Type;
4850
import com.google.devtools.build.lib.util.FileTypeSet;
4951
import com.google.devtools.build.lib.util.OnDemandString;
@@ -56,7 +58,7 @@
5658

5759
/**
5860
* A base implementation of genrule, to be used by specific implementing rules which can change the
59-
* semantics of {@link #isStampingEnabled} and {@link #collectSources}.
61+
* semantics of {@link #collectSources}.
6062
*/
6163
public abstract class GenRuleBase implements RuleConfiguredTargetFactory {
6264

@@ -227,20 +229,20 @@ public String toString() {
227229
.build();
228230
}
229231

230-
/**
231-
* Returns {@code true} if the rule should be stamped.
232-
*
233-
* <p>Genrule implementations can set this based on the rule context, including by defining their
234-
* own attributes over and above what is present in {@link GenRuleBaseRule}.
235-
*/
236-
@ForOverride
237-
protected abstract boolean isStampingEnabled(RuleContext ruleContext);
238-
239232
/** Collects sources from src attribute. */
240233
@ForOverride
241234
protected abstract ImmutableMap<Label, NestedSet<Artifact>> collectSources(
242235
List<? extends TransitiveInfoCollection> srcs) throws RuleErrorException;
243236

237+
private static boolean isStampingEnabled(RuleContext ruleContext) {
238+
// This intentionally does not call AnalysisUtils.isStampingEnabled(). That method returns false
239+
// in the exec configuration (regardless of the attribute value), which is the behavior for
240+
// binaries, but not genrules.
241+
TriState stamp = ruleContext.attributes().get("stamp", BuildType.TRISTATE);
242+
return stamp == TriState.YES
243+
|| (stamp == TriState.AUTO && ruleContext.getConfiguration().stampBinaries());
244+
}
245+
244246
private enum CommandType {
245247
BASH,
246248
WINDOWS_BATCH,

src/test/java/com/google/devtools/build/lib/bazel/rules/genrule/GenRuleConfiguredTargetTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ private void createStampingTargets() throws Exception {
530530
"u/BUILD",
531531
"genrule(name='foo_stamp', srcs=[], outs=['uu'], stamp=1, cmd='')",
532532
"genrule(name='foo_nostamp', srcs=[], outs=['vv'], stamp=0, cmd='')",
533+
"genrule(name='foo_autostamp', srcs=[], outs=['aa'], stamp=-1, cmd='')",
533534
"genrule(name='foo_default', srcs=[], outs=['xx'], cmd='')");
534535
}
535536

@@ -562,6 +563,8 @@ public void testStampingWithNoStamp() throws Exception {
562563
assertStamped(getExecConfiguredTarget("//u:foo_stamp"));
563564
assertNotStamped("//u:foo_nostamp");
564565
assertNotStamped(getExecConfiguredTarget("//u:foo_nostamp"));
566+
assertNotStamped("//u:foo_autostamp");
567+
assertNotStamped(getExecConfiguredTarget("//u:foo_autostamp"));
565568
assertNotStamped("//u:foo_default");
566569
}
567570

@@ -571,8 +574,10 @@ public void testStampingWithStamp() throws Exception {
571574
createStampingTargets();
572575
assertStamped("//u:foo_stamp");
573576
assertStamped(getExecConfiguredTarget("//u:foo_stamp"));
574-
// assertStamped("//u:foo_nostamp");
577+
assertNotStamped("//u:foo_nostamp");
575578
assertNotStamped(getExecConfiguredTarget("//u:foo_nostamp"));
579+
assertStamped("//u:foo_autostamp");
580+
assertNotStamped(getExecConfiguredTarget("//u:foo_autostamp"));
576581
assertNotStamped("//u:foo_default");
577582
}
578583

0 commit comments

Comments
 (0)