Skip to content

Commit

Permalink
Add API for individual actions
Browse files Browse the repository at this point in the history
This exposes action inputs, outputs, argv, content, and substitutions (if applicable).

--
MOS_MIGRATED_REVID=135226123
  • Loading branch information
brandjon authored and damienmg committed Oct 5, 2016
1 parent ffceb00 commit e025939
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@
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.skylarkinterface.SkylarkCallable;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
import com.google.devtools.build.lib.syntax.Printer;
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;
Expand All @@ -42,6 +49,10 @@
* immutable - see the documentation on {@link Action}.
*/
@Immutable @ThreadSafe
@SkylarkModule(
name = "Action",
doc = "Base class for actions.",
documented = false)
public abstract class AbstractAction implements Action, SkylarkValue {

/**
Expand Down Expand Up @@ -455,4 +466,52 @@ public Iterable<Artifact> getInputFilesForExtraAction(
return getInputs();
}

@SkylarkCallable(
name = "inputs",
doc = "A set of the input files of this action.",
structField = true)
public SkylarkNestedSet getSkylarkInputs() {
return SkylarkNestedSet.of(Artifact.class, NestedSetBuilder.wrap(
Order.STABLE_ORDER, getInputs()));
}

@SkylarkCallable(
name = "outputs",
doc = "A set of the output files of this action.",
structField = true)
public SkylarkNestedSet getSkylarkOutputs() {
return SkylarkNestedSet.of(Artifact.class, NestedSetBuilder.wrap(
Order.STABLE_ORDER, getOutputs()));
}

@SkylarkCallable(
name = "argv",
doc = "For actions created by <a href=\"ctx.html#action\">ctx.action()</a>, an immutable "
+ "list of the command line arguments. For all other actions, None.",
structField = true,
allowReturnNones = true)
public SkylarkList<String> getSkylarkArgv() {
return null;
}

@SkylarkCallable(
name = "content",
doc = "For actions created by <a href=\"ctx.html#file_action\">ctx.file_action()</a> or "
+ "<a href=\"ctx.html#template_action\">ctx.template_action()</a>, the contents of the "
+ "file to be written. For all other actions, None.",
structField = true,
allowReturnNones = true)
public String getSkylarkContent() throws IOException {
return null;
}

@SkylarkCallable(
name = "substitutions",
doc = "For actions created by <a href=\"ctx#template_action\">ctx.template_action()</a>, "
+ "an immutable dict holding the substitution mapping. For all other actions, None.",
structField = true,
allowReturnNones = true)
public SkylarkDict<String, String> getSkylarkSubstitutions() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.util.Fingerprint;
import com.google.devtools.build.lib.util.LazyString;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
Expand Down Expand Up @@ -113,6 +112,11 @@ public String getFileContents() {
return fileContents.toString();
}

@Override
public String getSkylarkContent() {
return getFileContents();
}

/**
* Create a DeterministicWriter for the content of the output file as provided by
* {@link #getFileContents()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.google.devtools.build.lib.collect.IterablesChain;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.syntax.SkylarkList;
import com.google.devtools.build.lib.util.Fingerprint;
import com.google.devtools.build.lib.util.Preconditions;
import com.google.devtools.build.lib.util.ShellEscaper;
Expand All @@ -65,7 +66,7 @@

/** An Action representing an arbitrary subprocess to be forked and exec'd. */
public class SpawnAction extends AbstractAction implements ExecutionInfoSpecifier, CommandAction {

/** Sets extensions on ExtraActionInfo **/
protected static class ExtraActionInfoSupplier<T> {
private final GeneratedExtension<ExtraActionInfo, T> extension;
Expand Down Expand Up @@ -205,6 +206,11 @@ public List<String> getArguments() {
return ImmutableList.copyOf(argv.arguments());
}

@Override
public SkylarkList<String> getSkylarkArgv() {
return SkylarkList.createImmutable(getArguments());
}

/**
* Returns the list of options written to the parameter file. Don't use this method outside tests.
* The list is often huge, resulting in significant garbage collection overhead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
import com.google.devtools.build.lib.actions.ActionOwner;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.syntax.SkylarkDict;
import com.google.devtools.build.lib.util.Fingerprint;
import com.google.devtools.build.lib.util.ResourceFileLoader;
import com.google.devtools.build.lib.util.StringUtilities;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -97,7 +97,7 @@ public String getValue() {
}
};
}

/**
* Returns an immutable Substitution instance for the key and map of values. Corresponding
* values in the map will be joined with "=", and pairs will be joined by spaces before
Expand All @@ -119,7 +119,7 @@ public String getValue() {
}
};
}

@Override
public boolean equals(Object object) {
if (this == object) {
Expand Down Expand Up @@ -346,6 +346,11 @@ public String getFileContents() throws IOException {
return expandTemplate(template.getContent());
}

@Override
public String getSkylarkContent() throws IOException {
return getFileContents();
}

@Override
public DeterministicWriter newDeterministicWriter(ActionExecutionContext ctx) throws IOException {
final byte[] bytes = getFileContents().getBytes(Template.DEFAULT_CHARSET);
Expand Down Expand Up @@ -384,4 +389,13 @@ protected String getRawProgressMessage() {
public List<Substitution> getSubstitutions() {
return substitutions;
}

@Override
public SkylarkDict<String, String> getSkylarkSubstitutions() {
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
for (Substitution entry : substitutions) {
builder.put(entry.getKey(), entry.getValue());
}
return SkylarkDict.copyOf(null, builder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// 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.rules;

import com.google.common.base.Function;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ public Runtime.NoneType invoke(
// input file that is in HOST configuration to the action as a precaution.
addRequiredIndirectRunfiles(ctx, builder);

if (mnemonicUnchecked != Runtime.NONE) {
builder.setMnemonic((String) mnemonicUnchecked);
}
String mnemonic = mnemonicUnchecked == Runtime.NONE
? "Generating" : (String) mnemonicUnchecked;
builder.setMnemonic(mnemonic);
if (envUnchecked != Runtime.NONE) {
builder.setEnvironment(
ImmutableMap.copyOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,6 @@ public FuncallException(String msg) {

private final int numPositionalArgs;

/**
* Note: the grammar definition restricts the function value in a function
* call expression to be a global identifier; however, the representation of
* values in the interpreter is flexible enough to allow functions to be
* arbitrary expressions. In any case, the "func" expression is always
* evaluated, so functions and variables share a common namespace.
*/
public FuncallExpression(@Nullable Expression obj, Identifier func,
List<Argument.Passed> args) {
this.obj = obj;
Expand All @@ -223,13 +216,6 @@ public FuncallExpression(@Nullable Expression obj, Identifier func,
this.numPositionalArgs = countPositionalArguments();
}

/**
* Note: the grammar definition restricts the function value in a function
* call expression to be a global identifier; however, the representation of
* values in the interpreter is flexible enough to allow functions to be
* arbitrary expressions. In any case, the "func" expression is always
* evaluated, so functions and variables share a common namespace.
*/
public FuncallExpression(Identifier func, List<Argument.Passed> args) {
this(null, func, args);
}
Expand Down
Loading

0 comments on commit e025939

Please sign in to comment.