diff options
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/query2')
4 files changed, 32 insertions, 2 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 7ce4c94716..9272e1d285 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 @@ -484,4 +484,20 @@ public abstract class OutputFormatter implements Serializable { return Pair.of((Iterable<Object>) values, source); } + + /** + * Returns the target location, eventually stripping out the workspace path to obtain a relative + * target (stable across machines / workspaces). + * + * @param target The target to extract location from. + * @param relative Whether to return a relative path or not. + * @return the target location + */ + protected static String getLocation(Target target, boolean relative) { + Location location = target.getLocation(); + return relative + ? location.print(target.getPackage().getPackageDirectory().asFragment(), + target.getPackage().getNameFragment()) + : location.print(); + } } 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 53fbb21023..0e22559d38 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 @@ -79,6 +79,7 @@ public class ProtoOutputFormatter extends OutputFormatter implements UnorderedFo public static final String RULE_IMPLEMENTATION_HASH_ATTR_NAME = "$rule_implementation_hash"; private BinaryPredicate<Rule, Attribute> dependencyFilter; + private boolean relativeLocations = false; protected void setDependencyFilter(QueryOptions options) { this.dependencyFilter = OutputFormatter.getDependencyFilter(options); @@ -92,6 +93,8 @@ public class ProtoOutputFormatter extends OutputFormatter implements UnorderedFo @Override public void outputUnordered(QueryOptions options, Iterable<Target> result, PrintStream out) throws IOException { + relativeLocations = options.relativeLocations; + setDependencyFilter(options); Build.QueryResult.Builder queryResult = Build.QueryResult.newBuilder(); @@ -124,7 +127,7 @@ public class ProtoOutputFormatter extends OutputFormatter implements UnorderedFo protected Build.Target toTargetProtoBuffer(Target target) { Build.Target.Builder targetPb = Build.Target.newBuilder(); - String location = target.getLocation().print(); + String location = getLocation(target, relativeLocations); if (target instanceof Rule) { Rule rule = (Rule) target; Build.Rule.Builder rulePb = Build.Rule.newBuilder() 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 c43610b909..91b973d682 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 @@ -128,6 +128,15 @@ public class QueryOptions extends OptionsBase { + "targets.") public List<String> universeScope; + @Option(name = "relative_locations", + defaultValue = "false", + category = "query", + 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; + /** * Return the current options as a set of QueryEnvironment settings. */ 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 287ad0b923..c77c7aa508 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 @@ -56,6 +56,7 @@ class XmlOutputFormatter extends OutputFormatter implements OutputFormatter.Unor private boolean xmlLineNumbers; private boolean showDefaultValues; + private boolean relativeLocations; private BinaryPredicate<Rule, Attribute> dependencyFilter; @Override @@ -67,6 +68,7 @@ class XmlOutputFormatter extends OutputFormatter implements OutputFormatter.Unor public void outputUnordered(QueryOptions options, Iterable<Target> result, PrintStream out) { this.xmlLineNumbers = options.xmlLineNumbers; this.showDefaultValues = options.xmlShowDefaultValues; + this.relativeLocations = options.relativeLocations; this.dependencyFilter = OutputFormatter.getDependencyFilter(options); Document doc; @@ -182,7 +184,7 @@ class XmlOutputFormatter extends OutputFormatter implements OutputFormatter.Unor } elem.setAttribute("name", target.getLabel().toString()); - String location = target.getLocation().print(); + String location = getLocation(target, relativeLocations); if (!xmlLineNumbers) { int firstColon = location.indexOf(':'); if (firstColon != -1) { |