// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.actions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.cache.MetadataHandler;
import com.google.devtools.build.lib.actions.extra.ExtraActionInfo;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.CollectionUtils;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.packages.AspectDescriptor;
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
import com.google.devtools.build.lib.syntax.SkylarkDict;
import com.google.devtools.build.lib.syntax.SkylarkList;
import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
import com.google.devtools.build.lib.util.Preconditions;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.Symlinks;
import com.google.devtools.build.skyframe.SkyFunction;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.concurrent.GuardedBy;
/**
* Abstract implementation of Action which implements basic functionality: the inputs, outputs, and
* toString method. Both input and output sets are immutable. Subclasses must be generally
* immutable - see the documentation on {@link Action}.
*/
@Immutable @ThreadSafe
@SkylarkModule(
name = "Action",
category = SkylarkModuleCategory.BUILTIN,
doc = "An action created on a ctx object. You can retrieve these "
+ "using the Actions provider. Some fields are only "
+ "applicable for certain kinds of actions. Fields that are inapplicable are set to "
+ "None
."
)
public abstract class AbstractAction implements Action, SkylarkValue {
/**
* An arbitrary default resource set. Currently 250MB of memory, 50% CPU and 0% of total I/O.
*/
public static final ResourceSet DEFAULT_RESOURCE_SET =
ResourceSet.createWithRamCpuIo(250, 0.5, 0);
/**
* The owner/inputs/outputs attributes below should never be directly accessed even within
* AbstractAction itself. The appropriate getter methods should be used instead. This has to be
* done due to the fact that the getter methods can be overridden in subclasses.
*/
private final ActionOwner owner;
/**
* Tools are a subset of inputs and used by the WorkerSpawnStrategy to determine whether a
* compiler has changed since the last time it was used. This should include all artifacts that
* the tool does not dynamically reload / check on each unit of work - e.g. its own binary, the
* JDK for Java binaries, shared libraries, ... but not a configuration file, if it reloads that
* when it has changed.
*
*
If the "tools" set does not contain exactly the right set of artifacts, the following can
* happen: If an artifact that should be included is missing, the tool might not be restarted when
* it should, and builds can become incorrect (example: The compiler binary is not part of this
* set, then the compiler gets upgraded, but the worker strategy still reuses the old version).
* If an artifact that should *not* be included is accidentally part of this set, the worker
* process will be restarted more often that is necessary - e.g. if a file that is unique to each
* unit of work, e.g. the source code that a compiler should compile for a compile action, is
* part of this set, then the worker will never be reused and will be restarted for each unit of
* work.
*/
private final Iterable The value returned by each instance should be constant over the lifetime of that instance.
*
* If this returns true, {@link #discoverInputs(ActionExecutionContext)} must also be
* implemented.
*/
@Override
public boolean discoversInputs() {
return false;
}
/**
* Run input discovery on the action.
*
* Called by Blaze if {@link #discoversInputs()} returns true. It must return the set of
* input artifacts that were not known at analysis time. May also call
* {@link #updateInputs(Iterable Since keeping state within an action bad, don't do that unless there is a very good reason
* to do so.
*/
@Override
public Iterable When an action discovers inputs, it must have been called by the time {@code #execute()}
* returns. It can be called both during {@code discoverInputs} and during {@code execute()}.
*
* In addition to being called from action implementations, it will also be called by Bazel
* itself when an action is loaded from the on-disk action cache.
*/
@Override
public final synchronized void updateInputs(Iterable A return value of null indicates no message should be reported.
*/
protected String getRawProgressMessage() {
// A cheesy default implementation. Subclasses are invited to do something
// more meaningful.
return defaultProgressMessage();
}
private String defaultProgressMessage() {
return getMnemonic() + " " + getPrimaryOutput().prettyPrint();
}
@Override
public String prettyPrint() {
return "action '" + describe() + "'";
}
@Override
public void repr(SkylarkPrinter printer) {
printer.append(prettyPrint()); // TODO(bazel-team): implement a readable representation
}
/**
* Deletes all of the action's output files, if they exist. If any of the
* Artifacts refers to a directory recursively removes the contents of the
* directory.
*
* @param execRoot the exec root in which this action is executed
*/
protected void deleteOutputs(Path execRoot) throws IOException {
for (Artifact output : getOutputs()) {
deleteOutput(output);
}
}
/**
* Helper method to remove an Artifact. If the Artifact refers to a directory
* recursively removes the contents of the directory.
*/
protected void deleteOutput(Artifact output) throws IOException {
Path path = output.getPath();
try {
// Optimize for the common case: output artifacts are files.
path.delete();
} catch (IOException e) {
// Handle a couple of scenarios where the output can still be deleted, but make sure we're not
// deleting random files on the filesystem.
if (output.getRoot() == null) {
throw e;
}
String outputRootDir = output.getRoot().getPath().getPathString();
if (!path.getPathString().startsWith(outputRootDir)) {
throw e;
}
Path parentDir = path.getParentDirectory();
if (!parentDir.isWritable() && parentDir.getPathString().startsWith(outputRootDir)) {
// Retry deleting after making the parent writable.
parentDir.setWritable(true);
deleteOutput(output);
} else if (path.isDirectory(Symlinks.NOFOLLOW)) {
FileSystemUtils.deleteTree(path);
} else {
throw e;
}
}
}
/**
* If the action might read directories as inputs in a way that is unsound wrt dependency
* checking, this method must be called.
*/
protected void checkInputsForDirectories(EventHandler eventHandler,
MetadataHandler metadataHandler) {
// Report "directory dependency checking" warning only for non-generated directories (generated
// ones will be reported earlier).
for (Artifact input : getMandatoryInputs()) {
// Assume that if the file did not exist, we would not have gotten here.
if (input.isSourceArtifact() && !metadataHandler.isRegularFile(input)) {
eventHandler.handle(Event.warn(getOwner().getLocation(), "input '"
+ input.prettyPrint() + "' to " + getOwner().getLabel()
+ " is a directory; dependency checking of directories is unsound"));
}
}
}
@Override
public MiddlemanType getActionType() {
return MiddlemanType.NORMAL;
}
@Override
public boolean canRemoveAfterExecution() {
return true;
}
/**
* If the action might create directories as outputs this method must be called.
*/
protected void checkOutputsForDirectories(EventHandler eventHandler) {
for (Artifact output : getOutputs()) {
Path path = output.getPath();
String ownerString = Label.print(getOwner().getLabel());
if (path.isDirectory()) {
eventHandler.handle(
Event.warn(
getOwner().getLocation(),
"output '" + output.prettyPrint() + "' of " + ownerString
+ " is a directory; dependency checking of directories is unsound")
.withTag(ownerString));
}
}
}
@Override
public void prepare(Path execRoot) throws IOException {
deleteOutputs(execRoot);
}
@Override
public String describe() {
String progressMessage = getProgressMessage();
return progressMessage != null ? progressMessage : defaultProgressMessage();
}
@Override
public boolean shouldReportPathPrefixConflict(ActionAnalysisMetadata action) {
return this != action;
}
@Override
public boolean extraActionCanAttach() {
return true;
}
@Override
public ExtraActionInfo.Builder getExtraActionInfo() {
ActionOwner owner = getOwner();
ExtraActionInfo.Builder result =
ExtraActionInfo.newBuilder()
.setOwner(owner.getLabel().toString())
.setId(getKey())
.setMnemonic(getMnemonic());
Iterable\"-c\"
.",
structField = true,
allowReturnNones = true)
public SkylarkList