blob: abc720808896ea0449234058b70170419eb910d3 [file] [log] [blame]
tomlu880508c2018-08-03 11:21:29 -07001// 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.
14package com.google.devtools.build.lib.skyframe;
15
16import com.google.common.base.Preconditions;
17import com.google.common.collect.ImmutableList;
18import com.google.common.collect.ImmutableSet;
19import com.google.devtools.build.lib.actions.ActionInputMap;
felly8dece492018-08-14 17:53:30 -070020import com.google.devtools.build.lib.actions.ActionLookupValue.ActionLookupKey;
tomlu880508c2018-08-03 11:21:29 -070021import com.google.devtools.build.lib.actions.Artifact;
22import com.google.devtools.build.lib.actions.FileArtifactValue;
felly8dece492018-08-14 17:53:30 -070023import com.google.devtools.build.lib.actions.FilesetOutputSymlink;
tomlu880508c2018-08-03 11:21:29 -070024import com.google.devtools.build.lib.util.Pair;
felly8dece492018-08-14 17:53:30 -070025import com.google.devtools.build.skyframe.SkyFunction.Environment;
26import com.google.devtools.build.skyframe.SkyKey;
tomlu880508c2018-08-03 11:21:29 -070027import com.google.devtools.build.skyframe.SkyValue;
28import java.util.Collection;
29import java.util.Map;
30
31class ActionInputMapHelper {
32
felly8dece492018-08-14 17:53:30 -070033 // Adds a value obtained by an Artifact skyvalue lookup to the action input map. May do Skyframe
34 // lookups.
tomlu880508c2018-08-03 11:21:29 -070035 static void addToMap(
36 ActionInputMap inputMap,
37 Map<Artifact, Collection<Artifact>> expandedArtifacts,
felly8dece492018-08-14 17:53:30 -070038 Map<Artifact, ImmutableList<FilesetOutputSymlink>> expandedFilesets,
tomlu880508c2018-08-03 11:21:29 -070039 Artifact key,
felly8dece492018-08-14 17:53:30 -070040 SkyValue value,
41 Environment env) throws InterruptedException {
tomlu880508c2018-08-03 11:21:29 -070042 if (value instanceof AggregatingArtifactValue) {
43 AggregatingArtifactValue aggregatingValue = (AggregatingArtifactValue) value;
44 for (Pair<Artifact, FileArtifactValue> entry : aggregatingValue.getFileArtifacts()) {
felly8dece492018-08-14 17:53:30 -070045 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 }
tomlu880508c2018-08-03 11:21:29 -070053 }
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
felly8dece492018-08-14 17:53:30 -070087 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
tomlu880508c2018-08-03 11:21:29 -0700107 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}