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; |
ajurkowski | 1e9ed64 | 2020-10-16 11:14:18 -0700 | [diff] [blame^] | 17 | import com.google.common.collect.ImmutableCollection; |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableSet; |
kush | c03c875 | 2018-09-13 12:13:20 -0700 | [diff] [blame] | 20 | import com.google.devtools.build.lib.actions.ActionAnalysisMetadata; |
mschaller | 4c7ca32 | 2018-11-20 13:22:05 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.ActionInputMapSink; |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.actions.ActionLookupData; |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.ActionLookupKey; |
kush | c03c875 | 2018-09-13 12:13:20 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.ActionLookupValue; |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.actions.Artifact; |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 26 | import com.google.devtools.build.lib.actions.Artifact.ArchivedTreeArtifact; |
Googler | 72d648a | 2020-05-21 11:56:40 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.actions.Artifact.DerivedArtifact; |
| 28 | import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact; |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.actions.FileArtifactValue; |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 30 | import com.google.devtools.build.lib.actions.FilesetOutputSymlink; |
kush | c03c875 | 2018-09-13 12:13:20 -0700 | [diff] [blame] | 31 | import com.google.devtools.build.lib.analysis.actions.SymlinkAction; |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.util.Pair; |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.skyframe.SkyFunction.Environment; |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 34 | import com.google.devtools.build.skyframe.SkyValue; |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 35 | import java.util.Map; |
| 36 | |
Googler | aed4160 | 2020-06-02 12:22:53 -0700 | [diff] [blame] | 37 | /** Static utilities for working with action inputs. */ |
| 38 | final class ActionInputMapHelper { |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 39 | |
Googler | aed4160 | 2020-06-02 12:22:53 -0700 | [diff] [blame] | 40 | private ActionInputMapHelper() {} |
| 41 | |
| 42 | /** |
| 43 | * Adds a value obtained by an Artifact skyvalue lookup to the action input map. May do Skyframe |
| 44 | * lookups. |
| 45 | */ |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 46 | static void addToMap( |
mschaller | 4c7ca32 | 2018-11-20 13:22:05 -0800 | [diff] [blame] | 47 | ActionInputMapSink inputMap, |
ajurkowski | 1e9ed64 | 2020-10-16 11:14:18 -0700 | [diff] [blame^] | 48 | Map<Artifact, ImmutableCollection<Artifact>> expandedArtifacts, |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 49 | Map<SpecialArtifact, ArchivedTreeArtifact> archivedTreeArtifacts, |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 50 | Map<Artifact, ImmutableList<FilesetOutputSymlink>> filesetsInsideRunfiles, |
| 51 | Map<Artifact, ImmutableList<FilesetOutputSymlink>> topLevelFilesets, |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 52 | Artifact key, |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 53 | SkyValue value, |
mschaller | 4c7ca32 | 2018-11-20 13:22:05 -0800 | [diff] [blame] | 54 | Environment env) |
| 55 | throws InterruptedException { |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 56 | if (value instanceof AggregatingArtifactValue) { |
| 57 | AggregatingArtifactValue aggregatingValue = (AggregatingArtifactValue) value; |
| 58 | for (Pair<Artifact, FileArtifactValue> entry : aggregatingValue.getFileArtifacts()) { |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 59 | Artifact artifact = entry.first; |
mschaller | 4c7ca32 | 2018-11-20 13:22:05 -0800 | [diff] [blame] | 60 | inputMap.put(artifact, entry.second, /*depOwner=*/ key); |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 61 | if (artifact.isFileset()) { |
janakr | 658d47f | 2019-05-29 11:11:30 -0700 | [diff] [blame] | 62 | ImmutableList<FilesetOutputSymlink> expandedFileset = |
Googler | 72d648a | 2020-05-21 11:56:40 -0700 | [diff] [blame] | 63 | getFilesets(env, (SpecialArtifact) artifact); |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 64 | if (expandedFileset != null) { |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 65 | filesetsInsideRunfiles.put(artifact, expandedFileset); |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 68 | } |
| 69 | for (Pair<Artifact, TreeArtifactValue> entry : aggregatingValue.getTreeArtifacts()) { |
| 70 | expandTreeArtifactAndPopulateArtifactData( |
| 71 | entry.getFirst(), |
| 72 | Preconditions.checkNotNull(entry.getSecond()), |
| 73 | expandedArtifacts, |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 74 | archivedTreeArtifacts, |
mschaller | 4c7ca32 | 2018-11-20 13:22:05 -0800 | [diff] [blame] | 75 | inputMap, |
| 76 | /*depOwner=*/ key); |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 77 | } |
Googler | 206d6e4 | 2020-06-18 12:39:17 -0700 | [diff] [blame] | 78 | // We have to cache the "digest" of the aggregating value itself, because the action cache |
| 79 | // checker may want it. |
| 80 | inputMap.put(key, aggregatingValue.getMetadata(), /*depOwner=*/ key); |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 81 | // While not obvious at all this code exists to ensure that we don't expand the |
| 82 | // .runfiles/MANIFEST file into the inputs. The reason for that being that the MANIFEST |
| 83 | // file contains absolute paths that don't work with remote execution. |
| 84 | // Instead, the way the SpawnInputExpander expands runfiles is via the Runfiles class |
| 85 | // which contains all artifacts in the runfiles tree minus the MANIFEST file. |
| 86 | // TODO(buchgr): Clean this up and get rid of the RunfilesArtifactValue type. |
| 87 | if (!(value instanceof RunfilesArtifactValue)) { |
| 88 | ImmutableList.Builder<Artifact> expansionBuilder = ImmutableList.builder(); |
| 89 | for (Pair<Artifact, FileArtifactValue> pair : aggregatingValue.getFileArtifacts()) { |
| 90 | expansionBuilder.add(Preconditions.checkNotNull(pair.getFirst())); |
| 91 | } |
| 92 | expandedArtifacts.put(key, expansionBuilder.build()); |
| 93 | } |
| 94 | } else if (value instanceof TreeArtifactValue) { |
| 95 | expandTreeArtifactAndPopulateArtifactData( |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 96 | key, |
| 97 | (TreeArtifactValue) value, |
| 98 | expandedArtifacts, |
| 99 | archivedTreeArtifacts, |
| 100 | inputMap, |
| 101 | /*depOwner=*/ key); |
janakr | 8541f6d | 2019-06-11 14:40:21 -0700 | [diff] [blame] | 102 | } else if (value instanceof ActionExecutionValue) { |
Googler | aed4160 | 2020-06-02 12:22:53 -0700 | [diff] [blame] | 103 | inputMap.put(key, ((ActionExecutionValue) value).getExistingFileArtifactValue(key), key); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 104 | if (key.isFileset()) { |
Googler | 72d648a | 2020-05-21 11:56:40 -0700 | [diff] [blame] | 105 | topLevelFilesets.put(key, getFilesets(env, (SpecialArtifact) key)); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 106 | } |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 107 | } else { |
Googler | 807d434 | 2020-06-29 11:58:07 -0700 | [diff] [blame] | 108 | Preconditions.checkArgument(value instanceof FileArtifactValue, "Unexpected value %s", value); |
mschaller | 4c7ca32 | 2018-11-20 13:22:05 -0800 | [diff] [blame] | 109 | inputMap.put(key, (FileArtifactValue) value, /*depOwner=*/ key); |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
janakr | 658d47f | 2019-05-29 11:11:30 -0700 | [diff] [blame] | 113 | static ImmutableList<FilesetOutputSymlink> getFilesets( |
Googler | 72d648a | 2020-05-21 11:56:40 -0700 | [diff] [blame] | 114 | Environment env, SpecialArtifact actionInput) throws InterruptedException { |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 115 | Preconditions.checkState(actionInput.isFileset(), actionInput); |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 116 | ActionLookupData generatingActionKey = actionInput.getGeneratingActionKey(); |
| 117 | ActionLookupKey filesetActionLookupKey = generatingActionKey.getActionLookupKey(); |
kush | c03c875 | 2018-09-13 12:13:20 -0700 | [diff] [blame] | 118 | |
| 119 | ActionLookupValue filesetActionLookupValue = |
| 120 | (ActionLookupValue) env.getValue(filesetActionLookupKey); |
| 121 | |
| 122 | ActionAnalysisMetadata generatingAction = |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 123 | filesetActionLookupValue.getAction(generatingActionKey.getActionIndex()); |
| 124 | ActionLookupData filesetActionKey; |
kush | c03c875 | 2018-09-13 12:13:20 -0700 | [diff] [blame] | 125 | |
| 126 | if (generatingAction instanceof SymlinkAction) { |
Googler | 72d648a | 2020-05-21 11:56:40 -0700 | [diff] [blame] | 127 | DerivedArtifact outputManifest = |
| 128 | (DerivedArtifact) generatingAction.getInputs().getSingleton(); |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 129 | ActionLookupData manifestGeneratingKey = outputManifest.getGeneratingActionKey(); |
| 130 | Preconditions.checkState( |
| 131 | manifestGeneratingKey.getActionLookupKey().equals(filesetActionLookupKey), |
| 132 | "Mismatched actions and artifacts: %s %s %s %s", |
| 133 | actionInput, |
| 134 | outputManifest, |
| 135 | filesetActionLookupKey, |
| 136 | manifestGeneratingKey); |
kush | c03c875 | 2018-09-13 12:13:20 -0700 | [diff] [blame] | 137 | ActionAnalysisMetadata symlinkTreeAction = |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 138 | filesetActionLookupValue.getAction(manifestGeneratingKey.getActionIndex()); |
Googler | 72d648a | 2020-05-21 11:56:40 -0700 | [diff] [blame] | 139 | DerivedArtifact inputManifest = |
| 140 | (DerivedArtifact) symlinkTreeAction.getInputs().getSingleton(); |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 141 | ActionLookupData inputManifestGeneratingKey = inputManifest.getGeneratingActionKey(); |
| 142 | Preconditions.checkState( |
| 143 | inputManifestGeneratingKey.getActionLookupKey().equals(filesetActionLookupKey), |
| 144 | "Mismatched actions and artifacts: %s %s %s %s", |
| 145 | actionInput, |
| 146 | inputManifest, |
| 147 | filesetActionLookupKey, |
| 148 | inputManifestGeneratingKey); |
| 149 | filesetActionKey = inputManifestGeneratingKey; |
kush | c03c875 | 2018-09-13 12:13:20 -0700 | [diff] [blame] | 150 | } else { |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 151 | filesetActionKey = generatingActionKey; |
kush | c03c875 | 2018-09-13 12:13:20 -0700 | [diff] [blame] | 152 | } |
| 153 | |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 154 | ActionExecutionValue filesetValue = (ActionExecutionValue) env.getValue(filesetActionKey); |
| 155 | if (filesetValue == null) { |
| 156 | // At this point skyframe does not guarantee that the filesetValue will be ready, since |
| 157 | // the current action does not directly depend on the outputs of the |
| 158 | // SkyframeFilesetManifestAction whose ActionExecutionValue (filesetValue) is needed here. |
felly | 8dece49 | 2018-08-14 17:53:30 -0700 | [diff] [blame] | 159 | return null; |
| 160 | } |
| 161 | return filesetValue.getOutputSymlinks(); |
| 162 | } |
| 163 | |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 164 | private static void expandTreeArtifactAndPopulateArtifactData( |
| 165 | Artifact treeArtifact, |
| 166 | TreeArtifactValue value, |
ajurkowski | 1e9ed64 | 2020-10-16 11:14:18 -0700 | [diff] [blame^] | 167 | Map<Artifact, ImmutableCollection<Artifact>> expandedArtifacts, |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 168 | Map<SpecialArtifact, ArchivedTreeArtifact> archivedTreeArtifacts, |
mschaller | 4c7ca32 | 2018-11-20 13:22:05 -0800 | [diff] [blame] | 169 | ActionInputMapSink inputMap, |
| 170 | Artifact depOwner) { |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 171 | if (TreeArtifactValue.OMITTED_TREE_MARKER.equals(value)) { |
| 172 | inputMap.put(treeArtifact, FileArtifactValue.OMITTED_FILE_MARKER, depOwner); |
| 173 | return; |
| 174 | } |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 175 | ImmutableSet.Builder<Artifact> children = ImmutableSet.builder(); |
| 176 | for (Map.Entry<Artifact.TreeFileArtifact, FileArtifactValue> child : |
| 177 | value.getChildValues().entrySet()) { |
| 178 | children.add(child.getKey()); |
mschaller | 4c7ca32 | 2018-11-20 13:22:05 -0800 | [diff] [blame] | 179 | inputMap.put(child.getKey(), child.getValue(), depOwner); |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 180 | } |
| 181 | expandedArtifacts.put(treeArtifact, children.build()); |
| 182 | // Again, we cache the "digest" of the value for cache checking. |
Googler | 206d6e4 | 2020-06-18 12:39:17 -0700 | [diff] [blame] | 183 | inputMap.put(treeArtifact, value.getMetadata(), depOwner); |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 184 | |
| 185 | value |
| 186 | .getArchivedRepresentation() |
| 187 | .ifPresent( |
| 188 | archivedRepresentation -> { |
| 189 | inputMap.put( |
| 190 | archivedRepresentation.archivedTreeFileArtifact(), |
| 191 | archivedRepresentation.archivedFileValue(), |
| 192 | depOwner); |
| 193 | archivedTreeArtifacts.put( |
| 194 | (SpecialArtifact) treeArtifact, |
| 195 | archivedRepresentation.archivedTreeFileArtifact()); |
| 196 | }); |
tomlu | 880508c | 2018-08-03 11:21:29 -0700 | [diff] [blame] | 197 | } |
| 198 | } |