Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 | |
| 15 | package com.google.devtools.build.lib.actions; |
| 16 | |
| 17 | import com.google.common.annotations.VisibleForTesting; |
| 18 | import com.google.common.base.Function; |
| 19 | import com.google.common.base.Functions; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 20 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.common.collect.Collections2; |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import com.google.common.collect.Iterables; |
Michael Thvedt | 434e68e | 2016-02-09 00:57:46 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander; |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 25 | import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact; |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact; |
Michael Thvedt | e3b1cb7 | 2016-02-08 23:32:27 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.vfs.PathFragment; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import java.util.ArrayList; |
| 29 | import java.util.Collection; |
| 30 | import java.util.List; |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 31 | import java.util.Set; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Helper utility to create ActionInput instances. |
| 35 | */ |
| 36 | public final class ActionInputHelper { |
| 37 | private ActionInputHelper() { |
| 38 | } |
| 39 | |
| 40 | @VisibleForTesting |
Michael Thvedt | 434e68e | 2016-02-09 00:57:46 +0000 | [diff] [blame] | 41 | public static ArtifactExpander actionGraphArtifactExpander( |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | final ActionGraph actionGraph) { |
Michael Thvedt | 434e68e | 2016-02-09 00:57:46 +0000 | [diff] [blame] | 43 | return new ArtifactExpander() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | @Override |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 45 | public void expand(Artifact mm, Collection<? super Artifact> output) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | // Skyframe is stricter in that it checks that "mm" is a input of the action, because |
| 47 | // it cannot expand arbitrary middlemen without access to a global action graph. |
| 48 | // We could check this constraint here too, but it seems unnecessary. This code is |
| 49 | // going away anyway. |
| 50 | Preconditions.checkArgument(mm.isMiddlemanArtifact(), |
| 51 | "%s is not a middleman artifact", mm); |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 52 | ActionAnalysisMetadata middlemanAction = actionGraph.getGeneratingAction(mm); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | Preconditions.checkState(middlemanAction != null, mm); |
| 54 | // TODO(bazel-team): Consider expanding recursively or throwing an exception here. |
| 55 | // Most likely, this code will cause silent errors if we ever have a middleman that |
| 56 | // contains a middleman. |
| 57 | if (middlemanAction.getActionType() == Action.MiddlemanType.AGGREGATING_MIDDLEMAN) { |
| 58 | Artifact.addNonMiddlemanArtifacts(middlemanAction.getInputs(), output, |
| 59 | Functions.<Artifact>identity()); |
| 60 | } |
| 61 | |
| 62 | } |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Most ActionInputs are created and never used again. On the off chance that one is, however, we |
| 68 | * implement equality via path comparison. Since file caches are keyed by ActionInput, equality |
| 69 | * checking does come up. |
| 70 | */ |
| 71 | private static class BasicActionInput implements ActionInput { |
| 72 | private final String path; |
Ulf Adams | 14db507 | 2017-03-20 16:00:51 +0000 | [diff] [blame] | 73 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | public BasicActionInput(String path) { |
| 75 | this.path = Preconditions.checkNotNull(path); |
ulfjack | 2918e78 | 2017-12-19 06:59:55 -0800 | [diff] [blame] | 76 | Preconditions.checkArgument(!path.isEmpty()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public String getExecPathString() { |
| 81 | return path; |
| 82 | } |
| 83 | |
| 84 | @Override |
Ulf Adams | 14db507 | 2017-03-20 16:00:51 +0000 | [diff] [blame] | 85 | public PathFragment getExecPath() { |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 86 | return PathFragment.create(path); |
Ulf Adams | 14db507 | 2017-03-20 16:00:51 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 90 | public int hashCode() { |
| 91 | return path.hashCode(); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public boolean equals(Object other) { |
| 96 | if (this == other) { |
| 97 | return true; |
| 98 | } |
| 99 | if (other == null) { |
| 100 | return false; |
| 101 | } |
| 102 | if (!this.getClass().equals(other.getClass())) { |
| 103 | return false; |
| 104 | } |
| 105 | return this.path.equals(((BasicActionInput) other).path); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public String toString() { |
| 110 | return "BasicActionInput: " + path; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Creates an ActionInput with just the given relative path and no digest. |
| 116 | * |
| 117 | * @param path the relative path of the input. |
| 118 | * @return a ActionInput. |
| 119 | */ |
| 120 | public static ActionInput fromPath(String path) { |
| 121 | return new BasicActionInput(path); |
| 122 | } |
| 123 | |
Ulf Adams | 11e52b4 | 2017-01-11 11:00:24 +0000 | [diff] [blame] | 124 | /** |
| 125 | * Creates an ActionInput with just the given relative path and no digest. |
| 126 | * |
| 127 | * @param path the relative path of the input. |
| 128 | * @return a ActionInput. |
| 129 | */ |
| 130 | public static ActionInput fromPath(PathFragment path) { |
| 131 | return fromPath(path.getPathString()); |
| 132 | } |
| 133 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 134 | /** |
| 135 | * Creates a sequence of {@link ActionInput}s from a sequence of string paths. |
| 136 | */ |
| 137 | public static Collection<ActionInput> fromPaths(Collection<String> paths) { |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 138 | return Collections2.transform(paths, ActionInputHelper::fromPath); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 142 | * Instantiates a concrete TreeFileArtifact with the given parent Artifact and path relative to |
| 143 | * that Artifact. |
Michael Thvedt | e3b1cb7 | 2016-02-08 23:32:27 +0000 | [diff] [blame] | 144 | */ |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 145 | public static TreeFileArtifact treeFileArtifact( |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 146 | SpecialArtifact parent, PathFragment relativePath) { |
Michael Thvedt | e3b1cb7 | 2016-02-08 23:32:27 +0000 | [diff] [blame] | 147 | Preconditions.checkState(parent.isTreeArtifact(), |
| 148 | "Given parent %s must be a TreeArtifact", parent); |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 149 | return new TreeFileArtifact(parent, relativePath); |
| 150 | } |
| 151 | |
| 152 | public static TreeFileArtifact treeFileArtifact( |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 153 | SpecialArtifact parent, PathFragment relativePath, ArtifactOwner artifactOwner) { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 154 | Preconditions.checkState(parent.isTreeArtifact(), |
| 155 | "Given parent %s must be a TreeArtifact", parent); |
| 156 | return new TreeFileArtifact( |
| 157 | parent, |
| 158 | relativePath, |
| 159 | artifactOwner); |
Michael Thvedt | e3b1cb7 | 2016-02-08 23:32:27 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 163 | * Instantiates a concrete TreeFileArtifact with the given parent Artifact and path relative to |
| 164 | * that Artifact. |
Michael Thvedt | e3b1cb7 | 2016-02-08 23:32:27 +0000 | [diff] [blame] | 165 | */ |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 166 | public static TreeFileArtifact treeFileArtifact(SpecialArtifact parent, String relativePath) { |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 167 | return treeFileArtifact(parent, PathFragment.create(relativePath)); |
Michael Thvedt | e3b1cb7 | 2016-02-08 23:32:27 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 170 | /** Returns an Iterable of TreeFileArtifacts with the given parent and parent relative paths. */ |
| 171 | public static Iterable<TreeFileArtifact> asTreeFileArtifacts( |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 172 | final SpecialArtifact parent, Iterable<? extends PathFragment> parentRelativePaths) { |
Michael Thvedt | e3b1cb7 | 2016-02-08 23:32:27 +0000 | [diff] [blame] | 173 | Preconditions.checkState(parent.isTreeArtifact(), |
| 174 | "Given parent %s must be a TreeArtifact", parent); |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 175 | return Iterables.transform( |
| 176 | parentRelativePaths, pathFragment -> treeFileArtifact(parent, pathFragment)); |
Michael Thvedt | e3b1cb7 | 2016-02-08 23:32:27 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 179 | /** Returns a Set of TreeFileArtifacts with the given parent and parent-relative paths. */ |
| 180 | public static Set<TreeFileArtifact> asTreeFileArtifacts( |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 181 | final SpecialArtifact parent, Set<? extends PathFragment> parentRelativePaths) { |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 182 | Preconditions.checkState(parent.isTreeArtifact(), |
| 183 | "Given parent %s must be a TreeArtifact", parent); |
| 184 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 185 | ImmutableSet.Builder<TreeFileArtifact> builder = ImmutableSet.builder(); |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 186 | for (PathFragment path : parentRelativePaths) { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 187 | builder.add(treeFileArtifact(parent, path)); |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | return builder.build(); |
| 191 | } |
| 192 | |
Michael Thvedt | e3b1cb7 | 2016-02-08 23:32:27 +0000 | [diff] [blame] | 193 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 194 | * Expands middleman artifacts in a sequence of {@link ActionInput}s. |
| 195 | * |
| 196 | * <p>Non-middleman artifacts are returned untouched. |
| 197 | */ |
Michael Thvedt | 434e68e | 2016-02-09 00:57:46 +0000 | [diff] [blame] | 198 | public static List<ActionInput> expandArtifacts(Iterable<? extends ActionInput> inputs, |
| 199 | ArtifactExpander artifactExpander) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 200 | |
| 201 | List<ActionInput> result = new ArrayList<>(); |
| 202 | List<Artifact> containedArtifacts = new ArrayList<>(); |
| 203 | for (ActionInput input : inputs) { |
| 204 | if (!(input instanceof Artifact)) { |
| 205 | result.add(input); |
| 206 | continue; |
| 207 | } |
| 208 | containedArtifacts.add((Artifact) input); |
| 209 | } |
Michael Thvedt | 434e68e | 2016-02-09 00:57:46 +0000 | [diff] [blame] | 210 | Artifact.addExpandedArtifacts(containedArtifacts, result, artifactExpander); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 211 | return result; |
| 212 | } |
| 213 | |
Michael Thvedt | e3b1cb7 | 2016-02-08 23:32:27 +0000 | [diff] [blame] | 214 | /** Formatter for execPath String output. Public because {@link Artifact} uses it directly. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 215 | public static final Function<ActionInput, String> EXEC_PATH_STRING_FORMATTER = |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 216 | ActionInput::getExecPathString; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 217 | |
| 218 | public static Iterable<String> toExecPaths(Iterable<? extends ActionInput> artifacts) { |
| 219 | return Iterables.transform(artifacts, EXEC_PATH_STRING_FORMATTER); |
| 220 | } |
| 221 | } |