blob: f1f1ee60788bb7baf419b461b187d4302069fbc3 [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;
cparsonse06e9d42018-06-20 09:41:11 -070019import com.google.devtools.build.lib.packages.BuiltinProvider;
gregced7c1cef2020-05-12 07:51:48 -070020import com.google.devtools.build.lib.packages.StarlarkInfo;
cparsons4ebf6c02018-08-17 14:49:36 -070021import com.google.devtools.build.lib.packages.StructImpl;
cparsonse06e9d42018-06-20 09:41:11 -070022import com.google.devtools.build.lib.skylarkbuildapi.ActionsInfoProviderApi;
adonovanf5262c52020-04-02 09:25:14 -070023import com.google.devtools.build.lib.syntax.Location;
adonovan553dc6c2019-12-10 11:22:48 -080024import com.google.devtools.build.lib.syntax.Starlark;
Jon Brandveinead58ae2016-09-29 18:41:10 +000025import java.util.HashMap;
26import java.util.Map;
27
28/**
29 * This provides a view over the actions that were created during the analysis of a rule
30 * (not including actions for its transitive dependencies).
31 */
cparsons4ebf6c02018-08-17 14:49:36 -070032public final class ActionsProvider extends BuiltinProvider<StructImpl>
33 implements ActionsInfoProviderApi {
Jon Brandveinead58ae2016-09-29 18:41:10 +000034
cparsonse06e9d42018-06-20 09:41:11 -070035 /** The ActionsProvider singleton instance. */
36 public static final ActionsProvider INSTANCE = new ActionsProvider();
37
38 public ActionsProvider() {
cparsons4ebf6c02018-08-17 14:49:36 -070039 super("Actions", StructImpl.class);
cparsonse06e9d42018-06-20 09:41:11 -070040 }
Jon Brandveinead58ae2016-09-29 18:41:10 +000041
dslomovde965ac2017-07-31 21:07:51 +020042 /** Factory method for creating instances of the Actions provider. */
cparsons4ebf6c02018-08-17 14:49:36 -070043 public static StructImpl create(Iterable<ActionAnalysisMetadata> actions) {
Jon Brandveinead58ae2016-09-29 18:41:10 +000044 Map<Artifact, ActionAnalysisMetadata> map = new HashMap<>();
45 for (ActionAnalysisMetadata action : actions) {
46 for (Artifact artifact : action.getOutputs()) {
47 // In the case that two actions generated the same artifact, the first wins. They
48 // ought to be equal anyway.
adonovan553dc6c2019-12-10 11:22:48 -080049 map.putIfAbsent(artifact, action);
Jon Brandveinead58ae2016-09-29 18:41:10 +000050 }
51 }
adonovan553dc6c2019-12-10 11:22:48 -080052 ImmutableMap<String, Object> fields =
53 ImmutableMap.<String, Object>of("by_file", Starlark.fromJava(map, /*mutability=*/ null));
gregced7c1cef2020-05-12 07:51:48 -070054 return StarlarkInfo.create(INSTANCE, fields, Location.BUILTIN);
Jon Brandveinead58ae2016-09-29 18:41:10 +000055 }
56}