Search in sources :

Example 21 with ActionAnalysisMetadata

use of com.google.devtools.build.lib.actions.ActionAnalysisMetadata in project bazel by bazelbuild.

the class ActionsProvider method create.

/**
   * Factory method for creating instances of the Actions provider.
   */
public static SkylarkClassObject create(Iterable<ActionAnalysisMetadata> actions) {
    Map<Artifact, ActionAnalysisMetadata> map = new HashMap<>();
    for (ActionAnalysisMetadata action : actions) {
        for (Artifact artifact : action.getOutputs()) {
            // ought to be equal anyway.
            if (!map.containsKey(artifact)) {
                map.put(artifact, action);
            }
        }
    }
    ImmutableMap<String, Object> fields = ImmutableMap.<String, Object>of("by_file", map);
    return new SkylarkClassObject(SKYLARK_CONSTRUCTOR, fields);
}
Also used : HashMap(java.util.HashMap) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) ActionAnalysisMetadata(com.google.devtools.build.lib.actions.ActionAnalysisMetadata) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 22 with ActionAnalysisMetadata

use of com.google.devtools.build.lib.actions.ActionAnalysisMetadata in project bazel by bazelbuild.

the class ActionsTestUtil method artifactClosureOf.

/**
   * Returns the closure over the input files of a set of artifacts.
   */
public Set<Artifact> artifactClosureOf(Iterable<Artifact> artifacts) {
    Set<Artifact> visited = new LinkedHashSet<>();
    List<Artifact> toVisit = Lists.newArrayList(artifacts);
    while (!toVisit.isEmpty()) {
        Artifact current = toVisit.remove(0);
        if (!visited.add(current)) {
            continue;
        }
        ActionAnalysisMetadata generatingAction = actionGraph.getGeneratingAction(current);
        if (generatingAction != null) {
            Iterables.addAll(toVisit, generatingAction.getInputs());
        }
    }
    return visited;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ActionAnalysisMetadata(com.google.devtools.build.lib.actions.ActionAnalysisMetadata) Artifact(com.google.devtools.build.lib.actions.Artifact) TreeFileArtifact(com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)

Example 23 with ActionAnalysisMetadata

use of com.google.devtools.build.lib.actions.ActionAnalysisMetadata in project bazel by bazelbuild.

the class ActionsTestUtil method getActionForArtifactEndingWith.

/**
   * Looks in the given artifacts Iterable for the first Artifact whose path ends with the given
   * suffix and returns its generating Action.
   */
public Action getActionForArtifactEndingWith(Iterable<Artifact> artifacts, String suffix) {
    Artifact a = getFirstArtifactEndingWith(artifacts, suffix);
    if (a == null) {
        return null;
    }
    ActionAnalysisMetadata action = actionGraph.getGeneratingAction(a);
    if (action != null) {
        Preconditions.checkState(action instanceof Action, "%s is not a proper Action object", action.prettyPrint());
        return (Action) action;
    } else {
        return null;
    }
}
Also used : AbstractAction(com.google.devtools.build.lib.actions.AbstractAction) Action(com.google.devtools.build.lib.actions.Action) ActionAnalysisMetadata(com.google.devtools.build.lib.actions.ActionAnalysisMetadata) Artifact(com.google.devtools.build.lib.actions.Artifact) TreeFileArtifact(com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)

Example 24 with ActionAnalysisMetadata

use of com.google.devtools.build.lib.actions.ActionAnalysisMetadata in project bazel by bazelbuild.

the class SkylarkRuleContextTest method testCreatedActions.

// For created_actions() tests, the "undertest" rule represents both the code under test and the
// Skylark user test code itself.
@Test
public void testCreatedActions() throws Exception {
    // createRuleContext() gives us the context for a rule upon entry into its analysis function.
    // But we need to inspect the result of calling created_actions() after the rule context has
    // been modified by creating actions. So we'll call created_actions() from within the analysis
    // function and pass it along as a provider.
    scratch.file("test/rules.bzl", "def _undertest_impl(ctx):", "  out1 = ctx.outputs.out1", "  out2 = ctx.outputs.out2", "  ctx.action(outputs=[out1], command='echo foo123 > ' + out1.path,", "             mnemonic='foo')", "  v = ctx.created_actions().by_file", "  ctx.action(outputs=[out2], command='echo bar123 > ' + out2.path)", "  return struct(v=v, out1=out1, out2=out2)", "undertest_rule = rule(", "  implementation = _undertest_impl,", "  outputs = {'out1': '%{name}1.txt',", "             'out2': '%{name}2.txt'},", "  _skylark_testable = True,", ")", testingRuleDefinition);
    scratch.file("test/BUILD", simpleBuildDefinition);
    SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
    Object mapUnchecked = evalRuleContextCode(ruleContext, "ruleContext.attr.dep.v");
    assertThat(mapUnchecked).isInstanceOf(SkylarkDict.class);
    SkylarkDict<?, ?> map = (SkylarkDict<?, ?>) mapUnchecked;
    // Should only have the first action because created_actions() was called
    // before the second action was created.
    Object file = eval("ruleContext.attr.dep.out1");
    assertThat(map).hasSize(1);
    assertThat(map).containsKey(file);
    Object actionUnchecked = map.get(file);
    assertThat(actionUnchecked).isInstanceOf(ActionAnalysisMetadata.class);
    assertThat(((ActionAnalysisMetadata) actionUnchecked).getMnemonic()).isEqualTo("foo");
}
Also used : SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) ActionAnalysisMetadata(com.google.devtools.build.lib.actions.ActionAnalysisMetadata) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) SkylarkDict(com.google.devtools.build.lib.syntax.SkylarkDict) Test(org.junit.Test)

Aggregations

ActionAnalysisMetadata (com.google.devtools.build.lib.actions.ActionAnalysisMetadata)24 Artifact (com.google.devtools.build.lib.actions.Artifact)16 TreeFileArtifact (com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)6 Action (com.google.devtools.build.lib.actions.Action)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ArtifactPrefixConflictException (com.google.devtools.build.lib.actions.ArtifactPrefixConflictException)4 ActionConflictException (com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException)4 SkyKey (com.google.devtools.build.skyframe.SkyKey)4 Map (java.util.Map)4 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)3 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)3 Label (com.google.devtools.build.lib.cmdline.Label)3 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 Test (org.junit.Test)3 ActionGraph (com.google.devtools.build.lib.actions.ActionGraph)2 ArtifactOwner (com.google.devtools.build.lib.actions.ArtifactOwner)2 MutableActionGraph (com.google.devtools.build.lib.actions.MutableActionGraph)2 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)2 ConflictException (com.google.devtools.build.lib.skyframe.SkyframeActionExecutor.ConflictException)2