aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Damien Martin-Guillerez <dmarting@google.com>2016-01-15 11:22:31 +0000
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2016-01-15 11:42:19 +0000
commit7751d4376c888f34084a9ebb494fa3bec7b3428d (patch)
treefb5c65453c90050db8de1c1286cb5c856120e08d
parente7bc9dd46720e2f18aa09b23fad433c78152bdaa (diff)
Add a method for getting the root of a rule workspace to the Label method
This method is exposed to Skylark and will enable correct handling of protobuf skylark files. See #784 -- MOS_MIGRATED_REVID=112235357
-rw-r--r--src/main/java/com/google/devtools/build/lib/cmdline/Label.java14
-rw-r--r--src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java8
2 files changed, 22 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java
index 8fe14e780c..97955dcd84 100644
--- a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java
+++ b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java
@@ -263,6 +263,20 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin
public String getPackageName() {
return packageIdentifier.getPackageFragment().getPathString();
}
+
+ /**
+ * Returns the execution root for the workspace, relative to the execroot (e.g., for label
+ * {@code @repo//pkg:b}, it will returns {@code external/repo/pkg} and for label {@code //pkg:a},
+ * it will returns an empty string.
+ */
+ @SkylarkCallable(name = "workspace_root", structField = true,
+ doc = "Returns the execution root for the workspace of this label, relative to the execroot. "
+ + "For instance:<br>"
+ + "<pre class=language-python>Label(\"@repo//pkg/foo:abc\").workspace_root =="
+ + " \"external/repo\"</pre>")
+ public String getWorkspaceRoot() {
+ return packageIdentifier.getRepository().getPathFragment().toString();
+ }
/**
* Returns the path fragment of the package in which this rule was declared (e.g. {@code
diff --git a/src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java b/src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java
index ec10104895..1f3ce8b212 100644
--- a/src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java
+++ b/src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java
@@ -353,4 +353,12 @@ public class LabelTest {
"invalid repository name 'foo': workspace names must start with '@'");
}
}
+
+ @Test
+ public void testGetWorkspaceRoot() throws Exception {
+ Label label = Label.parseAbsolute("//bar/baz");
+ assertThat(label.getWorkspaceRoot()).isEmpty();
+ label = Label.parseAbsolute("@repo//bar/baz");
+ assertThat(label.getWorkspaceRoot()).isEqualTo("external/repo");
+ }
}