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; |
| 19 | import com.google.devtools.build.lib.packages.SkylarkClassObject; |
| 20 | import com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor; |
| 21 | import java.util.HashMap; |
| 22 | import 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 Brandvein | 894550d | 2016-11-18 12:49:29 +0000 | [diff] [blame] | 28 | public final class ActionsProvider { |
Jon Brandvein | ead58ae | 2016-09-29 18:41:10 +0000 | [diff] [blame] | 29 | |
| 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 Aehlig | 95a87d0d | 2016-09-30 10:49:02 +0000 | [diff] [blame] | 44 | ImmutableMap<String, Object> fields = ImmutableMap.<String, Object>of("by_file", map); |
Jon Brandvein | ead58ae | 2016-09-29 18:41:10 +0000 | [diff] [blame] | 45 | return new SkylarkClassObject(ACTIONS_PROVIDER, fields); |
| 46 | } |
| 47 | } |