aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/query2
diff options
context:
space:
mode:
authorGravatar juliexxia <juliexxia@google.com>2018-05-01 11:32:53 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-05-01 11:34:33 -0700
commitcc126af0a41922c270c582c987cc99810dae4885 (patch)
treed1d75f011c80a1603a60a456cd0c8ee66c0d193a /src/main/java/com/google/devtools/build/lib/query2
parentb80eb18e66adcdcbfbc1ec62cd1a67db1924504d (diff)
Resolve selects in the attributes of targets for cquery's --output=proto format
PiperOrigin-RevId: 194967939
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/query2')
-rw-r--r--src/main/java/com/google/devtools/build/lib/query2/ProtoOutputFormatterCallback.java41
-rw-r--r--src/main/java/com/google/devtools/build/lib/query2/output/ProtoOutputFormatter.java63
2 files changed, 75 insertions, 29 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/query2/ProtoOutputFormatterCallback.java b/src/main/java/com/google/devtools/build/lib/query2/ProtoOutputFormatterCallback.java
index ff405e4bb6..337ba34c53 100644
--- a/src/main/java/com/google/devtools/build/lib/query2/ProtoOutputFormatterCallback.java
+++ b/src/main/java/com/google/devtools/build/lib/query2/ProtoOutputFormatterCallback.java
@@ -14,9 +14,18 @@
package com.google.devtools.build.lib.query2;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
import com.google.devtools.build.lib.analysis.AnalysisProtos;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
+import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider;
+import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget;
+import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.events.Reporter;
+import com.google.devtools.build.lib.packages.Attribute;
+import com.google.devtools.build.lib.packages.AttributeFormatter;
+import com.google.devtools.build.lib.packages.ConfiguredAttributeMapper;
+import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.query2.engine.QueryEnvironment.TargetAccessor;
import com.google.devtools.build.lib.query2.output.AspectResolver;
import com.google.devtools.build.lib.query2.output.CqueryOptions;
@@ -26,6 +35,7 @@ import com.google.devtools.build.lib.query2.proto.proto2api.Build.QueryResult.Bu
import com.google.devtools.build.lib.skyframe.SkyframeExecutor;
import java.io.IOException;
import java.io.OutputStream;
+import java.util.Map;
/** Proto output formatter for cquery results. */
public class ProtoOutputFormatterCallback extends CqueryThreadsafeCallback {
@@ -34,6 +44,8 @@ public class ProtoOutputFormatterCallback extends CqueryThreadsafeCallback {
private AnalysisProtos.CqueryResult.Builder protoResult;
+ private ConfiguredTarget currentTarget;
+
ProtoOutputFormatterCallback(
Reporter reporter,
CqueryOptions options,
@@ -78,7 +90,7 @@ public class ProtoOutputFormatterCallback extends CqueryThreadsafeCallback {
@Override
public void processOutput(Iterable<ConfiguredTarget> partialResult) throws InterruptedException {
- ProtoOutputFormatter formatter = new ProtoOutputFormatter();
+ ConfiguredProtoOutputFormatter formatter = new ConfiguredProtoOutputFormatter();
formatter.setOptions(options, resolver);
for (ConfiguredTarget configuredTarget : partialResult) {
AnalysisProtos.ConfiguredTarget.Builder builder =
@@ -88,6 +100,7 @@ public class ProtoOutputFormatterCallback extends CqueryThreadsafeCallback {
// for all its work with targets, ProtoOuputFormatterCallbackTest doesn't test any of the
// logic in this next line. If this were to change (i.e. we manipulate targets any further),
// we will want to add relevant tests.
+ currentTarget = configuredTarget;
builder.setTarget(
formatter.toTargetProtoBuffer(accessor.getTargetFromConfiguredTarget(configuredTarget)));
@@ -100,4 +113,30 @@ public class ProtoOutputFormatterCallback extends CqueryThreadsafeCallback {
protoResult.addResults(builder.build());
}
}
+
+ private class ConfiguredProtoOutputFormatter extends ProtoOutputFormatter {
+ @Override
+ protected void addAttributes(Build.Rule.Builder rulePb, Rule rule) throws InterruptedException {
+ ImmutableMap<Label, ConfigMatchingProvider> configConditions =
+ ((RuleConfiguredTarget) currentTarget).getConfigConditions();
+ ConfiguredAttributeMapper attributeMapper =
+ ConfiguredAttributeMapper.of(rule, configConditions);
+ Map<Attribute, Build.Attribute> serializedAttributes = Maps.newHashMap();
+ for (Attribute attr : rule.getAttributes()) {
+ if (!shouldIncludeAttribute(rule, attr)) {
+ continue;
+ }
+ Object attributeValue = attributeMapper.get(attr.getName(), attr.getType());
+ Build.Attribute serializedAttribute =
+ AttributeFormatter.getAttributeProto(
+ attr,
+ attributeValue,
+ rule.isAttributeValueExplicitlySpecified(attr),
+ /*encodeBooleanAndTriStateAsIntegerAndString=*/ true);
+ serializedAttributes.put(attr, serializedAttribute);
+ }
+ rulePb.addAllAttribute(serializedAttributes.values());
+ postProcess(rule, rulePb, serializedAttributes);
+ }
+ }
}
diff --git a/src/main/java/com/google/devtools/build/lib/query2/output/ProtoOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/output/ProtoOutputFormatter.java
index f0eab1cbb0..d0d984f6b5 100644
--- a/src/main/java/com/google/devtools/build/lib/query2/output/ProtoOutputFormatter.java
+++ b/src/main/java/com/google/devtools/build/lib/query2/output/ProtoOutputFormatter.java
@@ -184,33 +184,7 @@ public class ProtoOutputFormatter extends AbstractUnorderedFormatter {
if (includeLocation()) {
rulePb.setLocation(location);
}
- Map<Attribute, Build.Attribute> serializedAttributes = Maps.newHashMap();
- AggregatingAttributeMapper attributeMapper = AggregatingAttributeMapper.of(rule);
- for (Attribute attr : rule.getAttributes()) {
- if ((!includeDefaultValues && !rule.isAttributeValueExplicitlySpecified(attr))
- || !includeAttribute(rule, attr)) {
- continue;
- }
- Object attributeValue;
- if (flattenSelects || !attributeMapper.isConfigurable(attr.getName())) {
- attributeValue =
- flattenAttributeValues(attr.getType(), getPossibleAttributeValues(rule, attr));
- } else {
- attributeValue = attributeMapper.getSelectorList(attr.getName(), attr.getType());
- }
- Build.Attribute serializedAttribute =
- AttributeFormatter.getAttributeProto(
- attr,
- attributeValue,
- rule.isAttributeValueExplicitlySpecified(attr),
- /*encodeBooleanAndTriStateAsIntegerAndString=*/ true);
- serializedAttributes.put(attr, serializedAttribute);
- }
- rulePb.addAllAttribute(
- serializedAttributes.values().stream().distinct().collect(Collectors.toList()));
-
- postProcess(rule, rulePb, serializedAttributes);
-
+ addAttributes(rulePb, rule);
String transitiveHashCode = rule.getRuleClassObject().getRuleDefinitionEnvironmentHashCode();
if (transitiveHashCode != null && includeRuleDefinitionEnvironment()) {
// The RuleDefinitionEnvironment is always defined for Skylark rules and
@@ -265,7 +239,6 @@ public class ProtoOutputFormatter extends AbstractUnorderedFormatter {
for (String feature : rule.getFeatures()) {
rulePb.addDefaultSetting(feature);
}
-
targetPb.setType(RULE);
targetPb.setRule(rulePb);
} else if (target instanceof OutputFile) {
@@ -364,6 +337,40 @@ public class ProtoOutputFormatter extends AbstractUnorderedFormatter {
return targetPb.build();
}
+ protected void addAttributes(Build.Rule.Builder rulePb, Rule rule)
+ throws InterruptedException {
+ Map<Attribute, Build.Attribute> serializedAttributes = Maps.newHashMap();
+ AggregatingAttributeMapper attributeMapper = AggregatingAttributeMapper.of(rule);
+ for (Attribute attr : rule.getAttributes()) {
+ if (!shouldIncludeAttribute(rule, attr)) {
+ continue;
+ }
+ Object attributeValue;
+ if (flattenSelects || !attributeMapper.isConfigurable(attr.getName())) {
+ attributeValue =
+ flattenAttributeValues(attr.getType(), getPossibleAttributeValues(rule, attr));
+ } else {
+ attributeValue = attributeMapper.getSelectorList(attr.getName(), attr.getType());
+ }
+ Build.Attribute serializedAttribute =
+ AttributeFormatter.getAttributeProto(
+ attr,
+ attributeValue,
+ rule.isAttributeValueExplicitlySpecified(attr),
+ /*encodeBooleanAndTriStateAsIntegerAndString=*/ true);
+ serializedAttributes.put(attr, serializedAttribute);
+ }
+ rulePb.addAllAttribute(
+ serializedAttributes.values().stream().distinct().collect(Collectors.toList()));
+
+ postProcess(rule, rulePb, serializedAttributes);
+ }
+
+ protected boolean shouldIncludeAttribute(Rule rule, Attribute attr) {
+ return (includeDefaultValues || rule.isAttributeValueExplicitlySpecified(attr))
+ && includeAttribute(rule, attr);
+ }
+
private static Object getAspectAttributeValue(Attribute attribute, Collection<Label> labels) {
Type<?> attributeType = attribute.getType();
if (attributeType.equals(BuildType.LABEL)) {