aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build
diff options
context:
space:
mode:
authorGravatar shahan <shahan@google.com>2018-04-05 11:11:16 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-04-05 11:12:33 -0700
commitff4a4eb42f019d0e30def72aaeeeede14658da9e (patch)
treedf7e558767148c126002a9a87ff1636896966746 /src/main/java/com/google/devtools/build
parent77c5582fa0f5c829df576d9f8d66f4975fe415a6 (diff)
Introduces a SourceArtifact type.
SourceArtifact.getPath() is considered safe and eventually we may restrict getPath() to only SourceArtifact. PiperOrigin-RevId: 191768519
Diffstat (limited to 'src/main/java/com/google/devtools/build')
-rw-r--r--src/main/java/com/google/devtools/build/lib/actions/Artifact.java45
-rw-r--r--src/main/java/com/google/devtools/build/lib/actions/ArtifactFactory.java4
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java4
3 files changed, 40 insertions, 13 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/actions/Artifact.java b/src/main/java/com/google/devtools/build/lib/actions/Artifact.java
index bb8cd9875c..58c72204a3 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/Artifact.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/Artifact.java
@@ -94,20 +94,18 @@ import javax.annotation.Nullable;
* <li>A 'Fileset' special Artifact. This is a legacy type of Artifact and should not be used by
* new rule implementations.
* </ul>
- *
- * <p>This class is "theoretically" final; it should not be subclassed except by {@link
- * SpecialArtifact}.
*/
@Immutable
@SkylarkModule(
- name = "File",
- category = SkylarkModuleCategory.BUILTIN,
- doc = "This object is created during the analysis phase to represent a file or directory that "
- + "will be read or written during the execution phase. It is not an open file handle, and "
- + "cannot be used to directly read or write file contents. Rather, you use it to construct "
- + "the action graph in a rule implementation function by passing it to action-creating "
- + "functions. See the <a href='../rules.$DOC_EXT#files'>Rules page</a> for more "
- + "information."
+ name = "File",
+ category = SkylarkModuleCategory.BUILTIN,
+ doc =
+ "This object is created during the analysis phase to represent a file or directory that "
+ + "will be read or written during the execution phase. It is not an open file handle, "
+ + "and cannot be used to directly read or write file contents. Rather, you use it to "
+ + "construct the action graph in a rule implementation function by passing it to "
+ + "action-creating functions. See the "
+ + "<a href='../rules.$DOC_EXT#files'>Rules page</a> for more information."
)
@AutoCodec
public class Artifact
@@ -422,6 +420,31 @@ public class Artifact
return false;
}
+ /** Only callable if isSourceArtifact() is true. */
+ public SourceArtifact asSourceArtifact() {
+ throw new IllegalStateException("Not a source artifact!");
+ }
+
+ /** {@link Artifact#isSourceArtifact() is true.
+ *
+ * <p>Source artifacts have the property that unlike for output artifacts, direct file system
+ * access for their contents should be safe, even in a distributed context.
+ *
+ * TODO(shahan): move {@link Artifact#getPath} to this subclass.
+ * */
+ @AutoCodec
+ public static final class SourceArtifact extends Artifact {
+ @AutoCodec.VisibleForSerialization
+ SourceArtifact(ArtifactRoot root, PathFragment execPath, ArtifactOwner owner) {
+ super(root, execPath, owner);
+ }
+
+ @Override
+ public SourceArtifact asSourceArtifact() {
+ return this;
+ }
+ }
+
/**
* Special artifact types.
*
diff --git a/src/main/java/com/google/devtools/build/lib/actions/ArtifactFactory.java b/src/main/java/com/google/devtools/build/lib/actions/ArtifactFactory.java
index d23637a0fb..3011dd87b4 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/ArtifactFactory.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/ArtifactFactory.java
@@ -258,7 +258,9 @@ public class ArtifactFactory implements ArtifactResolver {
@Nullable SpecialArtifactType type) {
Preconditions.checkNotNull(owner);
if (type == null) {
- return new Artifact(root, execPath, owner);
+ return root.isSourceRoot()
+ ? new Artifact.SourceArtifact(root, execPath, owner)
+ : new Artifact(root, execPath, owner);
} else {
return new Artifact.SpecialArtifact(root, execPath, owner, type);
}
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java b/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java
index b4f3e69aa8..6fee875c33 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java
@@ -78,7 +78,9 @@ public final class EvalUtils {
&& ((SkylarkList) o1).isTuple() == ((SkylarkList) o2).isTuple()) {
return compareLists((SkylarkList) o1, (SkylarkList) o2);
}
- if (!o1.getClass().equals(o2.getClass())) {
+
+ if (!(o1.getClass().isAssignableFrom(o2.getClass())
+ || o2.getClass().isAssignableFrom(o1.getClass()))) {
throw new ComparisonException(
"Cannot compare " + getDataTypeName(o1) + " with " + getDataTypeName(o2));
}