aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/query2
diff options
context:
space:
mode:
authorGravatar Damien Martin-Guillerez <dmarting@google.com>2015-03-12 11:11:08 +0000
committerGravatar Han-Wen Nienhuys <hanwen@google.com>2015-03-13 14:17:18 +0000
commitc498c98c5b5dccb37639d3901ea7ab8154c2a14c (patch)
treea28de347471642d8e198ef601f20a0fbc9487e82 /src/main/java/com/google/devtools/build/lib/query2
parenta07d918b3a3b35d600a7fc81d4549067af5092cb (diff)
RELNOTES[NEW]: Added --relative_locations flag to the query command to make the locations of build files relative to the workspace root with protobuf and XML outputs
Protobuf and XML outputs of query are non-deterministic. This flag is a first step to make genquery deterministic accross machine. -- MOS_MIGRATED_REVID=88428100
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/query2')
-rw-r--r--src/main/java/com/google/devtools/build/lib/query2/output/OutputFormatter.java16
-rw-r--r--src/main/java/com/google/devtools/build/lib/query2/output/ProtoOutputFormatter.java5
-rw-r--r--src/main/java/com/google/devtools/build/lib/query2/output/QueryOptions.java9
-rw-r--r--src/main/java/com/google/devtools/build/lib/query2/output/XmlOutputFormatter.java4
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) {