blob: 9f3a882a4a69b68bdb113e6b0256e68591fcd899 [file] [log] [blame]
Jon Brandveinead58ae2016-09-29 18:41:10 +00001// Copyright 2016 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.analysis;
15
16import com.google.common.collect.ImmutableMap;
17import com.google.devtools.build.lib.actions.ActionAnalysisMetadata;
18import com.google.devtools.build.lib.actions.Artifact;
19import com.google.devtools.build.lib.packages.SkylarkClassObject;
20import com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor;
21import java.util.HashMap;
22import java.util.Map;
23
24/**
25 * This provides a view over the actions that were created during the analysis of a rule
26 * (not including actions for its transitive dependencies).
27 */
Jon Brandvein894550d2016-11-18 12:49:29 +000028public final class ActionsProvider {
Jon Brandveinead58ae2016-09-29 18:41:10 +000029
30 public static final SkylarkClassObjectConstructor ACTIONS_PROVIDER =
31 SkylarkClassObjectConstructor.createNative("Actions");
32
33 public static SkylarkClassObject create(Iterable<ActionAnalysisMetadata> actions) {
34 Map<Artifact, ActionAnalysisMetadata> map = new HashMap<>();
35 for (ActionAnalysisMetadata action : actions) {
36 for (Artifact artifact : action.getOutputs()) {
37 // In the case that two actions generated the same artifact, the first wins. They
38 // ought to be equal anyway.
39 if (!map.containsKey(artifact)) {
40 map.put(artifact, action);
41 }
42 }
43 }
Klaus Aehlig95a87d0d2016-09-30 10:49:02 +000044 ImmutableMap<String, Object> fields = ImmutableMap.<String, Object>of("by_file", map);
Jon Brandveinead58ae2016-09-29 18:41:10 +000045 return new SkylarkClassObject(ACTIONS_PROVIDER, fields);
46 }
47}