Jon Brandvein | ead58ae | 2016-09-29 18:41:10 +0000 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.analysis; |
| 15 | |
| 16 | import com.google.common.collect.ImmutableMap; |
| 17 | import com.google.devtools.build.lib.actions.ActionAnalysisMetadata; |
| 18 | import com.google.devtools.build.lib.actions.Artifact; |
cparsons | e06e9d4 | 2018-06-20 09:41:11 -0700 | [diff] [blame] | 19 | import com.google.devtools.build.lib.packages.BuiltinProvider; |
gregce | d7c1cef | 2020-05-12 07:51:48 -0700 | [diff] [blame^] | 20 | import com.google.devtools.build.lib.packages.StarlarkInfo; |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.packages.StructImpl; |
cparsons | e06e9d4 | 2018-06-20 09:41:11 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.skylarkbuildapi.ActionsInfoProviderApi; |
adonovan | f5262c5 | 2020-04-02 09:25:14 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.syntax.Location; |
adonovan | 553dc6c | 2019-12-10 11:22:48 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.syntax.Starlark; |
Jon Brandvein | ead58ae | 2016-09-29 18:41:10 +0000 | [diff] [blame] | 25 | import java.util.HashMap; |
| 26 | import 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 | */ |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 32 | public final class ActionsProvider extends BuiltinProvider<StructImpl> |
| 33 | implements ActionsInfoProviderApi { |
Jon Brandvein | ead58ae | 2016-09-29 18:41:10 +0000 | [diff] [blame] | 34 | |
cparsons | e06e9d4 | 2018-06-20 09:41:11 -0700 | [diff] [blame] | 35 | /** The ActionsProvider singleton instance. */ |
| 36 | public static final ActionsProvider INSTANCE = new ActionsProvider(); |
| 37 | |
| 38 | public ActionsProvider() { |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 39 | super("Actions", StructImpl.class); |
cparsons | e06e9d4 | 2018-06-20 09:41:11 -0700 | [diff] [blame] | 40 | } |
Jon Brandvein | ead58ae | 2016-09-29 18:41:10 +0000 | [diff] [blame] | 41 | |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 42 | /** Factory method for creating instances of the Actions provider. */ |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 43 | public static StructImpl create(Iterable<ActionAnalysisMetadata> actions) { |
Jon Brandvein | ead58ae | 2016-09-29 18:41:10 +0000 | [diff] [blame] | 44 | 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. |
adonovan | 553dc6c | 2019-12-10 11:22:48 -0800 | [diff] [blame] | 49 | map.putIfAbsent(artifact, action); |
Jon Brandvein | ead58ae | 2016-09-29 18:41:10 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
adonovan | 553dc6c | 2019-12-10 11:22:48 -0800 | [diff] [blame] | 52 | ImmutableMap<String, Object> fields = |
| 53 | ImmutableMap.<String, Object>of("by_file", Starlark.fromJava(map, /*mutability=*/ null)); |
gregce | d7c1cef | 2020-05-12 07:51:48 -0700 | [diff] [blame^] | 54 | return StarlarkInfo.create(INSTANCE, fields, Location.BUILTIN); |
Jon Brandvein | ead58ae | 2016-09-29 18:41:10 +0000 | [diff] [blame] | 55 | } |
| 56 | } |