diff options
Diffstat (limited to 'src/main/java')
5 files changed, 69 insertions, 4 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/events/Location.java b/src/main/java/com/google/devtools/build/lib/events/Location.java index 39508d17e7..4cc0ae53f3 100644 --- a/src/main/java/com/google/devtools/build/lib/events/Location.java +++ b/src/main/java/com/google/devtools/build/lib/events/Location.java @@ -147,9 +147,13 @@ public abstract class Location implements Serializable { * </pre> */ public String print() { + return printWithPath(getPath()); + } + + private String printWithPath(PathFragment path) { StringBuilder buf = new StringBuilder(); - if (getPath() != null) { - buf.append(getPath()).append(':'); + if (path != null) { + buf.append(path).append(':'); } LineAndColumn start = getStartLineAndColumn(); if (start == null) { @@ -166,6 +170,37 @@ public abstract class Location implements Serializable { } /** + * A default implementation of toString() that formats the location in the following ways based on + * the amount of information available: + * + * <pre> + * "foo.cc:23:2" + * "23:2" + * "foo.cc:char offsets 123--456" + * "char offsets 123--456" + *</pre> + * + * <p>This version replace the package's path with the relative package path. I.e., if {@code + * packagePath} is equivalent to "/absolute/path/to/workspace/pack/age" and {@code + * relativePackage} is equivalent to "pack/age" then the result for the 2nd character of the 23rd + * line of the "foo/bar.cc" file in "pack/age" would be "pack/age/foo/bar.cc:23:2" whereas with + * {@link #print()} the result would be "/absolute/path/to/workspace/pack/age/foo/bar.cc:23:2". + * + * <p>If {@code packagePath} is not a parent of the location path, then the result of this + * function is the same as the result of {@link #print()}. + */ + public String print(PathFragment packagePath, PathFragment relativePackage) { + PathFragment path = getPath(); + if (path == null) { + return printWithPath(null); + } else if (path.startsWith(packagePath)) { + return printWithPath(relativePackage.getRelative(path.relativeTo(packagePath))); + } else { + return printWithPath(path); + } + } + + /** * Prints the object in a sort of reasonable way. This should never be used in user-visible * places, only for debugging and testing. */ 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) { |