tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 1 | // Copyright 2018 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.skyframe; |
| 15 | |
| 16 | import com.google.common.base.Preconditions; |
| 17 | import com.google.common.collect.ImmutableList; |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.devtools.build.lib.actions.ActionInputMap; |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 20 | import com.google.devtools.build.lib.actions.ActionLookupValue.ActionLookupKey; |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.Artifact; |
| 22 | import com.google.devtools.build.lib.actions.FileArtifactValue; |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.FilesetOutputSymlink; |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.util.Pair; |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.skyframe.SkyFunction.Environment; |
| 26 | import com.google.devtools.build.skyframe.SkyKey; |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.skyframe.SkyValue; |
| 28 | import java.util.Collection; |
| 29 | import java.util.Map; |
| 30 | |
| 31 | class ActionInputMapHelper { |
| 32 | |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 33 | // Adds a value obtained by an Artifact skyvalue lookup to the action input map. May do Skyframe |
| 34 | // lookups. |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 35 | static void addToMap( |
| 36 | ActionInputMap inputMap, |
| 37 | Map<Artifact, Collection<Artifact>> expandedArtifacts, |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 38 | Map<Artifact, ImmutableList<FilesetOutputSymlink>> expandedFilesets, |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 39 | Artifact key, |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 40 | SkyValue value, |
| 41 | Environment env) throws InterruptedException { |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 42 | if (value instanceof AggregatingArtifactValue) { |
| 43 | AggregatingArtifactValue aggregatingValue = (AggregatingArtifactValue) value; |
| 44 | for (Pair<Artifact, FileArtifactValue> entry : aggregatingValue.getFileArtifacts()) { |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 45 | Artifact artifact = entry.first; |
| 46 | inputMap.put(artifact, entry.second); |
| 47 | if (artifact.isFileset()) { |
| 48 | ImmutableList<FilesetOutputSymlink> expandedFileset = getFilesets(env, artifact); |
| 49 | if (expandedFileset != null) { |
| 50 | expandedFilesets.put(artifact, expandedFileset); |
| 51 | } |
| 52 | } |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 53 | } |
| 54 | for (Pair<Artifact, TreeArtifactValue> entry : aggregatingValue.getTreeArtifacts()) { |
| 55 | expandTreeArtifactAndPopulateArtifactData( |
| 56 | entry.getFirst(), |
| 57 | Preconditions.checkNotNull(entry.getSecond()), |
| 58 | expandedArtifacts, |
| 59 | inputMap); |
| 60 | } |
| 61 | // We have to cache the "digest" of the aggregating value itself, |
| 62 | // because the action cache checker may want it. |
| 63 | inputMap.put(key, aggregatingValue.getSelfData()); |
| 64 | // While not obvious at all this code exists to ensure that we don't expand the |
| 65 | // .runfiles/MANIFEST file into the inputs. The reason for that being that the MANIFEST |
| 66 | // file contains absolute paths that don't work with remote execution. |
| 67 | // Instead, the way the SpawnInputExpander expands runfiles is via the Runfiles class |
| 68 | // which contains all artifacts in the runfiles tree minus the MANIFEST file. |
| 69 | // TODO(buchgr): Clean this up and get rid of the RunfilesArtifactValue type. |
| 70 | if (!(value instanceof RunfilesArtifactValue)) { |
| 71 | ImmutableList.Builder<Artifact> expansionBuilder = ImmutableList.builder(); |
| 72 | for (Pair<Artifact, FileArtifactValue> pair : aggregatingValue.getFileArtifacts()) { |
| 73 | expansionBuilder.add(Preconditions.checkNotNull(pair.getFirst())); |
| 74 | } |
| 75 | expandedArtifacts.put(key, expansionBuilder.build()); |
| 76 | } |
| 77 | } else if (value instanceof TreeArtifactValue) { |
| 78 | expandTreeArtifactAndPopulateArtifactData( |
| 79 | key, (TreeArtifactValue) value, expandedArtifacts, inputMap); |
| 80 | |
| 81 | } else { |
| 82 | Preconditions.checkState(value instanceof FileArtifactValue); |
| 83 | inputMap.put(key, (FileArtifactValue) value); |
| 84 | } |
| 85 | } |
| 86 | |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 87 | static ImmutableList<FilesetOutputSymlink> getFilesets(Environment env, |
| 88 | Artifact actionInput) throws InterruptedException { |
| 89 | Preconditions.checkState(actionInput.isFileset(), actionInput); |
| 90 | ActionLookupKey filesetActionLookupKey = (ActionLookupKey) actionInput.getArtifactOwner(); |
| 91 | // Index 0 for the Fileset ConfiguredTarget indicates the SkyframeFilesetManifestAction where |
| 92 | // we compute the fileset's outputSymlinks. |
| 93 | SkyKey filesetActionKey = ActionExecutionValue.key(filesetActionLookupKey, 0); |
| 94 | ActionExecutionValue filesetValue = (ActionExecutionValue) env.getValue(filesetActionKey); |
| 95 | if (filesetValue == null) { |
| 96 | // At this point skyframe does not guarantee that the filesetValue will be ready, since |
| 97 | // the current action does not directly depend on the outputs of the |
| 98 | // SkyframeFilesetManifestAction whose ActionExecutionValue (filesetValue) is needed here. |
| 99 | // TODO(kush): Get rid of this hack by making the outputSymlinks available in the Fileset |
| 100 | // artifact, which this action depends on, so its value will be guaranteed to be present. |
| 101 | // Also, unify handling of Fileset with Artifact expansion. |
| 102 | return null; |
| 103 | } |
| 104 | return filesetValue.getOutputSymlinks(); |
| 105 | } |
| 106 | |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 107 | private static void expandTreeArtifactAndPopulateArtifactData( |
| 108 | Artifact treeArtifact, |
| 109 | TreeArtifactValue value, |
| 110 | Map<Artifact, Collection<Artifact>> expandedArtifacts, |
| 111 | ActionInputMap inputMap) { |
| 112 | ImmutableSet.Builder<Artifact> children = ImmutableSet.builder(); |
| 113 | for (Map.Entry<Artifact.TreeFileArtifact, FileArtifactValue> child : |
| 114 | value.getChildValues().entrySet()) { |
| 115 | children.add(child.getKey()); |
| 116 | inputMap.put(child.getKey(), child.getValue()); |
| 117 | } |
| 118 | expandedArtifacts.put(treeArtifact, children.build()); |
| 119 | // Again, we cache the "digest" of the value for cache checking. |
| 120 | inputMap.put(treeArtifact, value.getSelfData()); |
| 121 | } |
| 122 | } |