From c9bdc1fab5b218d5049edbbb09facff2aeac96b3 Mon Sep 17 00:00:00 2001 From: juliexxia Date: Tue, 3 Apr 2018 10:27:08 -0700 Subject: 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 --- .../com/google/devtools/build/lib/query2/BUILD | 36 ++++++++- .../build/lib/query2/CommonQueryOptions.java | 86 ++++++++++++++++++++++ .../build/lib/query2/output/OutputFormatter.java | 9 ++- .../lib/query2/output/ProtoOutputFormatter.java | 3 +- .../build/lib/query2/output/QueryOptions.java | 77 ------------------- .../lib/query2/output/XmlOutputFormatter.java | 19 ++++- 6 files changed, 145 insertions(+), 85 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/query2/BUILD b/src/main/java/com/google/devtools/build/lib/query2/BUILD index 2bf456c22a..196a23d5c9 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/BUILD +++ b/src/main/java/com/google/devtools/build/lib/query2/BUILD @@ -17,6 +17,8 @@ java_library( ), deps = [ ":abstract-blaze-query-env", + ":aspect-resolver", + ":common-query-options", ":fake-load-target", ":query-engine", ":query-output", @@ -41,9 +43,18 @@ java_library( java_library( name = "query-output", - srcs = glob(["output/*.java"]), + srcs = glob( + ["output/*.java"], + exclude = [ + "output/AspectResolver.java", + "output/PresciseAspectResolver.java", + "output/ConservativeAspectResolver.java", + "output/NullAspectResolver.java", + ], + ), deps = [ ":abstract-blaze-query-env", + ":aspect-resolver", ":common-query-options", ":fake-load-target", ":query-engine", @@ -81,8 +92,31 @@ java_library( "CommonQueryOptions.java", ], deps = [ + ":aspect-resolver", ":query-engine", + "//src/main/java/com/google/devtools/build/lib:packages-internal", + "//src/main/java/com/google/devtools/build/lib:util", "//src/main/java/com/google/devtools/common/options", + "//src/main/protobuf:build_java_proto", + "//third_party:guava", + "//third_party:jsr305", + ], +) + +java_library( + name = "aspect-resolver", + srcs = [ + "output/AspectResolver.java", + "output/ConservativeAspectResolver.java", + "output/NullAspectResolver.java", + "output/PreciseAspectResolver.java", + ], + deps = [ + "//src/main/java/com/google/devtools/build/lib:events", + "//src/main/java/com/google/devtools/build/lib:packages-internal", + "//src/main/java/com/google/devtools/build/lib:util", + "//third_party:guava", + "//third_party:jsr305", ], ) diff --git a/src/main/java/com/google/devtools/build/lib/query2/CommonQueryOptions.java b/src/main/java/com/google/devtools/build/lib/query2/CommonQueryOptions.java index 3a258e84e3..c1261d057d 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/CommonQueryOptions.java +++ b/src/main/java/com/google/devtools/build/lib/query2/CommonQueryOptions.java @@ -13,8 +13,13 @@ // limitations under the License. package com.google.devtools.build.lib.query2; +import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.query2.engine.QueryEnvironment.Setting; +import com.google.devtools.build.lib.query2.output.AspectResolver; +import com.google.devtools.build.lib.query2.output.AspectResolver.Mode; import com.google.devtools.common.options.Converters; +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; @@ -99,4 +104,85 @@ public class CommonQueryOptions extends OptionsBase { } return settings; } + + /////////////////////////////////////////////////////////// + // PROTO OUTPUT FORMATTER OPTIONS // + /////////////////////////////////////////////////////////// + + @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 = "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: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; + + @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 protoOutputRuleAttributes = ImmutableList.of("all"); + + /** An enum converter for {@code AspectResolver.Mode} . Should be used internally only. */ + public static class AspectResolutionModeConverter extends EnumConverter { + public AspectResolutionModeConverter() { + super(AspectResolver.Mode.class, "Aspect resolution mode"); + } + } + + @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; } 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 { - 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 { @@ -135,30 +126,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 protoOutputRuleAttributes = ImmutableList.of("all"); - @Option( name = "xml:line_numbers", defaultValue = "true", @@ -192,38 +159,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 = "", @@ -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 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"; @@ -68,6 +75,14 @@ class XmlOutputFormatter extends AbstractUnorderedFormatter { createPostFactoStreamCallback(out, options)); } + @Override + public void setOptions(CommonQueryOptions options, AspectResolver aspectResolver) { + super.setOptions(options, aspectResolver); + + Preconditions.checkArgument(options instanceof QueryOptions); + this.queryOptions = (QueryOptions) options; + } + @Override public OutputFormatterCallback createPostFactoStreamCallback( final OutputStream out, final QueryOptions options) { @@ -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); -- cgit v1.2.3