diff options
author | 2018-04-03 10:27:08 -0700 | |
---|---|---|
committer | 2018-04-03 10:28:53 -0700 | |
commit | c9bdc1fab5b218d5049edbbb09facff2aeac96b3 (patch) | |
tree | 1a9729a06e15aba2af1be876d22688999cfed54d /src/main/java/com/google/devtools/build/lib/query2/output | |
parent | 91167ddef4a08e4a2081481375677d356d7d88a0 (diff) |
Move proto-related options up a level to common query options. This is part of several CLs which implement proto output with cquery.
PiperOrigin-RevId: 191460698
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/query2/output')
4 files changed, 24 insertions, 84 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/query2/output/OutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/output/OutputFormatter.java index 49c99604f8..d7ab9d6598 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/output/OutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/output/OutputFormatter.java @@ -38,6 +38,7 @@ import com.google.devtools.build.lib.packages.Rule; import com.google.devtools.build.lib.packages.Target; import com.google.devtools.build.lib.packages.TriState; import com.google.devtools.build.lib.query2.AbstractBlazeQueryEnvironment; +import com.google.devtools.build.lib.query2.CommonQueryOptions; import com.google.devtools.build.lib.query2.engine.AggregatingQueryExpressionVisitor.ContainsFunctionQueryExpressionVisitor; import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback; import com.google.devtools.build.lib.query2.engine.QueryEnvironment; @@ -142,7 +143,7 @@ public abstract class OutputFormatter implements Serializable { * passing to {@link Rule#getLabels()}, {@link XmlOutputFormatter}, etc. */ public static DependencyFilter getDependencyFilter( - QueryOptions queryOptions) { + CommonQueryOptions queryOptions) { // TODO(bazel-team): Optimize: and(ALL_DEPS, x) -> x, etc. return DependencyFilter.and( queryOptions.includeHostDeps ? DependencyFilter.ALL_DEPS : DependencyFilter.NO_HOST_DEPS, @@ -186,7 +187,7 @@ public abstract class OutputFormatter implements Serializable { */ public interface StreamedFormatter { /** Specifies options to be used by subsequent calls to {@link #createStreamCallback}. */ - void setOptions(QueryOptions options, AspectResolver aspectResolver); + void setOptions(CommonQueryOptions options, AspectResolver aspectResolver); /** * Returns a {@link ThreadSafeOutputFormatterCallback} whose @@ -216,7 +217,7 @@ public abstract class OutputFormatter implements Serializable { abstract static class AbstractUnorderedFormatter extends OutputFormatter implements StreamedFormatter { - protected QueryOptions options; + protected CommonQueryOptions options; protected AspectResolver aspectResolver; protected DependencyFilter dependencyFilter; @@ -230,7 +231,7 @@ public abstract class OutputFormatter implements Serializable { } @Override - public void setOptions(QueryOptions options, AspectResolver aspectResolver) { + public void setOptions(CommonQueryOptions options, AspectResolver aspectResolver) { this.options = options; this.aspectResolver = aspectResolver; this.dependencyFilter = OutputFormatter.getDependencyFilter(options); 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 ef5c31a073..f0eab1cbb0 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 @@ -41,6 +41,7 @@ import com.google.devtools.build.lib.packages.PackageGroup; import com.google.devtools.build.lib.packages.ProtoUtils; import com.google.devtools.build.lib.packages.Rule; import com.google.devtools.build.lib.packages.Target; +import com.google.devtools.build.lib.query2.CommonQueryOptions; import com.google.devtools.build.lib.query2.FakeLoadTarget; import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback; import com.google.devtools.build.lib.query2.engine.QueryEnvironment; @@ -98,7 +99,7 @@ public class ProtoOutputFormatter extends AbstractUnorderedFormatter { } @Override - public void setOptions(QueryOptions options, AspectResolver aspectResolver) { + public void setOptions(CommonQueryOptions options, AspectResolver aspectResolver) { super.setOptions(options, aspectResolver); this.relativeLocations = options.relativeLocations; this.includeDefaultValues = options.protoIncludeDefaultValues; diff --git a/src/main/java/com/google/devtools/build/lib/query2/output/QueryOptions.java b/src/main/java/com/google/devtools/build/lib/query2/output/QueryOptions.java index c3e3e52f5c..fbd2644d51 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/output/QueryOptions.java +++ b/src/main/java/com/google/devtools/build/lib/query2/output/QueryOptions.java @@ -13,25 +13,16 @@ // limitations under the License. package com.google.devtools.build.lib.query2.output; -import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.query2.CommonQueryOptions; import com.google.devtools.build.lib.query2.engine.QueryEnvironment.Setting; -import com.google.devtools.common.options.Converters.CommaSeparatedOptionListConverter; import com.google.devtools.common.options.EnumConverter; import com.google.devtools.common.options.Option; import com.google.devtools.common.options.OptionDocumentationCategory; import com.google.devtools.common.options.OptionEffectTag; -import java.util.List; import java.util.Set; /** Command-line options for the Blaze query language, revision 2. */ public class QueryOptions extends CommonQueryOptions { - /** An enum converter for {@code AspectResolver.Mode} . Should be used internally only. */ - public static class AspectResolutionModeConverter extends EnumConverter<AspectResolver.Mode> { - public AspectResolutionModeConverter() { - super(AspectResolver.Mode.class, "Aspect resolution mode"); - } - } /** An enum converter for {@code OrderOutput} . Should be used internally only. */ public static class OrderOutputConverter extends EnumConverter<OrderOutput> { @@ -136,30 +127,6 @@ public class QueryOptions extends CommonQueryOptions { public boolean graphFactored; @Option( - name = "proto:default_values", - defaultValue = "true", - documentationCategory = OptionDocumentationCategory.QUERY, - effectTags = {OptionEffectTag.TERMINAL_OUTPUT}, - help = - "If true, attributes whose value is not explicitly specified in the BUILD file are " - + "included; otherwise they are omitted. This option is applicable to --output=proto" - ) - public boolean protoIncludeDefaultValues; - - @Option( - name = "proto:output_rule_attrs", - converter = CommaSeparatedOptionListConverter.class, - defaultValue = "all", - documentationCategory = OptionDocumentationCategory.QUERY, - effectTags = {OptionEffectTag.TERMINAL_OUTPUT}, - help = - "Comma separated list of attributes to include in output. Defaults to all attributes. " - + "Set to empty string to not output any attribute. " - + "This option is applicable to --output=proto." - ) - public List<String> protoOutputRuleAttributes = ImmutableList.of("all"); - - @Option( name = "xml:line_numbers", defaultValue = "true", documentationCategory = OptionDocumentationCategory.QUERY, @@ -193,38 +160,6 @@ public class QueryOptions extends CommonQueryOptions { public boolean strictTestSuite; @Option( - name = "relative_locations", - defaultValue = "false", - documentationCategory = OptionDocumentationCategory.QUERY, - effectTags = {OptionEffectTag.TERMINAL_OUTPUT}, - help = - "If true, the location of BUILD files in xml and proto outputs will be relative. " - + "By default, the location output is an absolute path and will not be consistent " - + "across machines. You can set this option to true to have a consistent result " - + "across machines." - ) - public boolean relativeLocations; - - @Option( - name = "aspect_deps", - converter = AspectResolutionModeConverter.class, - defaultValue = "conservative", - documentationCategory = OptionDocumentationCategory.QUERY, - effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS}, - help = - "How to resolve aspect dependencies when the output format is one of {xml,proto,record}. " - + "'off' means no aspect dependencies are resolved, 'conservative' (the default) means " - + "all declared aspect dependencies are added regardless of whether they are viable " - + "given the rule class of direct dependencies, 'precise' means that only those " - + "aspects are added that are possibly active given the rule class of the direct " - + "dependencies. Note that precise mode requires loading other packages to evaluate " - + "a single target thus making it slower than the other modes. Also note that even " - + "precise mode is not completely precise: the decision whether to compute an aspect " - + "is decided in the analysis phase, which is not run during 'blaze query'." - ) - public AspectResolver.Mode aspectDeps; - - @Option( name = "query_file", defaultValue = "", documentationCategory = OptionDocumentationCategory.QUERY, @@ -244,18 +179,6 @@ public class QueryOptions extends CommonQueryOptions { return System.lineSeparator(); } - @Option( - name = "proto:flatten_selects", - defaultValue = "true", - documentationCategory = OptionDocumentationCategory.QUERY, - effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS}, - help = - "If enabled, configurable attributes created by select() are flattened. For list types " - + "the flattened representation is a list containing each value of the select map " - + "exactly once. Scalar types are flattened to null." - ) - public boolean protoFlattenSelects; - /** Return the current options as a set of QueryEnvironment settings. */ @Override public Set<Setting> toSettings() { diff --git a/src/main/java/com/google/devtools/build/lib/query2/output/XmlOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/output/XmlOutputFormatter.java index c6102dfc28..c8f0d50d32 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/output/XmlOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/output/XmlOutputFormatter.java @@ -13,6 +13,7 @@ // limitations under the License. package com.google.devtools.build.lib.query2.output; +import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.devtools.build.lib.cmdline.Label; @@ -26,6 +27,7 @@ import com.google.devtools.build.lib.packages.OutputFile; import com.google.devtools.build.lib.packages.PackageGroup; import com.google.devtools.build.lib.packages.Rule; import com.google.devtools.build.lib.packages.Target; +import com.google.devtools.build.lib.query2.CommonQueryOptions; import com.google.devtools.build.lib.query2.FakeLoadTarget; import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback; import com.google.devtools.build.lib.query2.engine.QueryEnvironment; @@ -56,6 +58,11 @@ import org.w3c.dom.Element; * An output formatter that prints the result as XML. */ class XmlOutputFormatter extends AbstractUnorderedFormatter { + + // AbstractUnorderedFormatter also has an options field it's of type CommonQueryOptions, a + // superclass of QueryOptions. Store this here to ensure correct type is passed to this class. + private QueryOptions queryOptions; + @Override public String getName() { return "xml"; @@ -69,6 +76,14 @@ class XmlOutputFormatter extends AbstractUnorderedFormatter { } @Override + public void setOptions(CommonQueryOptions options, AspectResolver aspectResolver) { + super.setOptions(options, aspectResolver); + + Preconditions.checkArgument(options instanceof QueryOptions); + this.queryOptions = (QueryOptions) options; + } + + @Override public OutputFormatterCallback<Target> createPostFactoStreamCallback( final OutputStream out, final QueryOptions options) { return new OutputFormatterCallback<Target>() { @@ -136,7 +151,7 @@ class XmlOutputFormatter extends AbstractUnorderedFormatter { elem.setAttribute("class", rule.getRuleClass()); for (Attribute attr : rule.getAttributes()) { PossibleAttributeValues values = getPossibleAttributeValues(rule, attr); - if (values.source == AttributeValueSource.RULE || options.xmlShowDefaultValues) { + if (values.source == AttributeValueSource.RULE || queryOptions.xmlShowDefaultValues) { Element attrElem = createValueElement(doc, attr.getType(), values); attrElem.setAttribute("name", attr.getName()); elem.appendChild(attrElem); @@ -219,7 +234,7 @@ class XmlOutputFormatter extends AbstractUnorderedFormatter { elem.setAttribute("name", target.getLabel().toString()); String location = getLocation(target, options.relativeLocations); - if (!options.xmlLineNumbers) { + if (!queryOptions.xmlLineNumbers) { int firstColon = location.indexOf(':'); if (firstColon != -1) { location = location.substring(0, firstColon); |