Factor out source collection in genrule.
package com.google.devtools.build.lib.rules.genrule;
+import static com.google.common.collect.ImmutableMap.toImmutableMap;
+
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
@@ -68,6 +70,23 @@ public abstract class GenRuleBase implements RuleConfiguredTargetFactory {
*/
protected abstract boolean isStampingEnabled(RuleContext ruleContext);
+ private ImmutableMap<Label, NestedSet<Artifact>> collectSources(
+ List<? extends TransitiveInfoCollection> srcs) {
+ ImmutableMap.Builder<Label, NestedSet<Artifact>> labelMap = ImmutableMap.builder();
+
+ for (TransitiveInfoCollection dep : srcs) {
+ // This target provides specific types of files for genrules.
+ GenRuleSourcesProvider provider = dep.getProvider(GenRuleSourcesProvider.class);
+ NestedSet<Artifact> files =
+ (provider != null)
+ ? provider.getGenruleFiles()
+ : dep.getProvider(FileProvider.class).getFilesToBuild();
+ labelMap.put(AliasProvider.getDependencyLabel(dep), files);
+ }
+
+ return labelMap.build();
+ }
+
enum CommandType {
BASH,
WINDOWS_BATCH,
@@ -104,7 +123,6 @@ public abstract class GenRuleBase implements RuleConfiguredTargetFactory {
throws InterruptedException, RuleErrorException, ActionConflictException {
NestedSet<Artifact> filesToBuild =
NestedSetBuilder.wrap(Order.STABLE_ORDER, ruleContext.getOutputArtifacts());
- NestedSetBuilder<Artifact> resolvedSrcsBuilder = NestedSetBuilder.stableOrder();
if (filesToBuild.isEmpty()) {
ruleContext.attributeError("outs", "Genrules without outputs don't make sense");
@@ -121,22 +139,18 @@ public abstract class GenRuleBase implements RuleConfiguredTargetFactory {
Pair<CommandType, String> cmdTypeAndAttr = determineCommandTypeAndAttribute(ruleContext);
- ImmutableMap.Builder<Label, Iterable<Artifact>> labelMap = ImmutableMap.builder();
- for (TransitiveInfoCollection dep : ruleContext.getPrerequisites("srcs")) {
- // This target provides specific types of files for genrules.
- GenRuleSourcesProvider provider = dep.getProvider(GenRuleSourcesProvider.class);
- NestedSet<Artifact> files = (provider != null)
- ? provider.getGenruleFiles()
- : dep.getProvider(FileProvider.class).getFilesToBuild();
- resolvedSrcsBuilder.addTransitive(files);
+ ImmutableMap<Label, NestedSet<Artifact>> labelMap =
+ collectSources(ruleContext.getPrerequisites("srcs"));
+ NestedSet<Artifact> resolvedSrcs = NestedSetBuilder.fromNestedSets(labelMap.values()).build();
+
// The CommandHelper class makes an explicit copy of this in the constructor, so flattening
// here should be benign.
- labelMap.put(AliasProvider.getDependencyLabel(dep), files.toList());
- }
- NestedSet<Artifact> resolvedSrcs = resolvedSrcsBuilder.build();
-
CommandHelper commandHelper =
- commandHelperBuilder(ruleContext).addLabelMap(labelMap.build()).build();
+ commandHelperBuilder(ruleContext)
+ .addLabelMap(
+ labelMap.entrySet().stream()
+ .collect(toImmutableMap(Map.Entry::getKey, e -> e.getValue().toList())))
+ .build();
if (ruleContext.hasErrors()) {
return null;
