// 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.devtools.build.lib.actions.extra.ExtraActionInfo; import com.google.devtools.build.lib.concurrent.ThreadSafety.ConditionallyThreadCompatible; import com.google.devtools.build.lib.vfs.FileSystem; import com.google.devtools.build.lib.vfs.Path; import java.io.IOException; import javax.annotation.Nullable; /** * An Action represents a function from Artifacts to Artifacts executed as an atomic build step. * Examples include compilation of a single C++ source file, or linking a single library. * *

All subclasses of Action need to follow a strict set of invariants to ensure correctness on * incremental builds. In our experience, getting this wrong is a lot more expensive than any * benefits it might entail. * *

Use {@link com.google.devtools.build.lib.analysis.actions.SpawnAction} or {@link * com.google.devtools.build.lib.analysis.actions.FileWriteAction} where possible, and avoid writing * a new custom subclass. * *

These are the most important requirements for subclasses: * *

* *

These constraints are not easily enforced or tested for (e.g., ActionTester only checks that a * known set of fields is covered, not that all fields are covered), so carefully check all changes * to action subclasses. */ public interface Action extends ActionExecutionMetadata { /** * Prepares for executing this action; called by the Builder prior to executing the Action itself. * This method should prepare the file system, so that the execution of the Action can write the * output files. At a minimum any pre-existing and write protected output files should be removed * or the permissions should be changed, so that they can be safely overwritten by the action. * * @throws IOException if there is an error deleting the outputs. */ void prepare(FileSystem fileSystem, Path execRoot) throws IOException; /** * Executes this action; called by the Builder when all of this Action's inputs have been * successfully created. (Behaviour is undefined if the prerequisites are not up to date.) This * method actually does the work of the Action, unconditionally; in other words, it is * invoked by the Builder only when dependency analysis has deemed it necessary. * *

The framework guarantees that the output directory for each file in getOutputs() * has already been created, and will check to ensure that each of those files is indeed * created. * *

Implementations of this method should try to honour the {@link java.lang.Thread#interrupted} * contract: if an interrupt is delivered to the thread in which execution occurs, the action * should detect this on a best-effort basis and terminate as quickly as possible by throwing an * ActionExecutionException. * *

Action execution must be ThreadCompatible in order to be safely used with a concurrent * Builder implementation such as ParallelBuilder. * * @param actionExecutionContext services in the scope of the action, like the output and error * streams to use for messages arising during action execution * @return returns an ActionResult containing action execution metadata * @throws ActionExecutionException if execution fails for any reason * @throws InterruptedException */ @ConditionallyThreadCompatible ActionResult execute(ActionExecutionContext actionExecutionContext) throws ActionExecutionException, InterruptedException; /** * Returns true iff action must be executed regardless of its current state. * Default implementation can be overridden by some actions that might be * executed unconditionally under certain circumstances - e.g., if caching of * test results is not requested, this method could be used to force test * execution even if all dependencies are up-to-date. * *

Note, it is very important not to abuse this method, since it * completely overrides dependency checking. Any use of this method must * be carefully reviewed and proved to be necessary. * *

Note that the definition of {@link #isVolatile} depends on the * definition of this method, so be sure to consider both methods together * when making changes. */ boolean executeUnconditionally(); /** * Returns true if it's ever possible that {@link #executeUnconditionally} * could evaluate to true during the lifetime of this instance, false * otherwise. */ boolean isVolatile(); /** * Method used to find inputs before execution for an action that {@link * ActionExecutionMetadata#discoversInputs}. Returns the set of discovered inputs (may be the * empty set) or null if this action declared additional Skyframe dependencies that must be * computed before it can make a decision. */ @Nullable Iterable discoverInputs(ActionExecutionContext actionExecutionContext) throws ActionExecutionException, InterruptedException; /** * Returns the set of artifacts that can possibly be inputs. It will be called iff inputsKnown() * is false for the given action instance and there is a related cache entry in the action cache. * *

Method must be redefined for any action that may return inputsKnown() == false. * *

The method is allowed to return source artifacts. They are useless, though, since exec paths * in the action cache referring to source artifacts are always resolved. */ Iterable getAllowedDerivedInputs(); /** * Informs the action that its inputs are {@code inputs}, and that its inputs are now known. Can * only be called for actions that discover inputs. After this method is called, * {@link ActionExecutionMetadata#inputsDiscovered} should return true. */ void updateInputs(Iterable inputs); /** * Returns true if the output should bypass output filtering. This is used for test actions. */ boolean showsOutputUnconditionally(); /** * Called by {@link com.google.devtools.build.lib.analysis.extra.ExtraAction} at execution time to * extract information from this action into a protocol buffer to be used by extra_action rules. * *

As this method is called from the ExtraAction, make sure it is ok to call this method from a * different thread than the one this action is executed on. */ ExtraActionInfo.Builder getExtraActionInfo(ActionKeyContext actionKeyContext) throws CommandLineExpansionException; }