Make Rule know about the name of the workspace it is in.
This is needed for taking the runfiles prefix from the WORKSPACE file instead of hardcoding it.
--
MOS_MIGRATED_REVID=87347883
diff --git a/src/main/java/com/google/devtools/build/lib/packages/Package.java b/src/main/java/com/google/devtools/build/lib/packages/Package.java
index a2216e0..fd48e5d 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/Package.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/Package.java
@@ -24,6 +24,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
+import com.google.devtools.build.lib.Constants;
import com.google.devtools.build.lib.collect.CollectionUtils;
import com.google.devtools.build.lib.collect.ImmutableSortedKeyMap;
import com.google.devtools.build.lib.events.Event;
@@ -33,7 +34,6 @@
import com.google.devtools.build.lib.packages.License.DistributionType;
import com.google.devtools.build.lib.packages.PackageDeserializer.PackageDeserializationException;
import com.google.devtools.build.lib.packages.PackageFactory.Globber;
-
import com.google.devtools.build.lib.syntax.FuncallExpression;
import com.google.devtools.build.lib.syntax.Label;
import com.google.devtools.build.lib.syntax.Label.SyntaxException;
@@ -103,6 +103,12 @@
private Path packageDirectory;
/**
+ * The name of the workspace this package is in. Used as a prefix for the runfiles directory.
+ * This can be set in the WORKSPACE file. This must be a valid target name.
+ */
+ protected String workspaceName = Constants.RUNFILES_PREFIX;
+
+ /**
* The root of the source tree in which this package was found. It is an invariant that
* {@code sourceRoot.getRelative(name).equals(packageDirectory)}.
*/
@@ -545,6 +551,16 @@
}
/**
+ * Returns this package's workspace name.
+ *
+ * <p>Package-private to encourage callers to get their workspace name from a rule, not a
+ * package.</p>
+ */
+ String getWorkspaceName() {
+ return workspaceName;
+ }
+
+ /**
* Returns the features specified in the <code>package()</code> declaration.
*/
public ImmutableSet<String> getFeatures() {
@@ -826,7 +842,7 @@
protected Map<Label, EnvironmentGroup> environmentGroups = new HashMap<>();
protected Map<Label, Path> subincludes = null;
- protected ImmutableList<Label> skylarkFileDependencies = null;
+ protected ImmutableList<Label> skylarkFileDependencies = ImmutableList.of();
/**
* True iff the "package" function has already been called in this package.
@@ -942,6 +958,14 @@
}
/**
+ * Uses the workspace name from {@code //external} to set this package's workspace name.
+ */
+ B setWorkspaceName(String workspaceName) {
+ pkg.workspaceName = workspaceName;
+ return self();
+ }
+
+ /**
* Returns whether the "package" function has been called yet
*/
public boolean isPackageFunctionUsed() {