Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1 | // Copyright 2020 Google Inc. 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 | |
| 15 | package bazel |
| 16 | |
| 17 | import ( |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 18 | "crypto/sha256" |
Usta Shrestha | 2ccdb42 | 2022-06-02 10:19:13 -0400 | [diff] [blame] | 19 | "encoding/base64" |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 20 | "encoding/json" |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 21 | "fmt" |
| 22 | "path/filepath" |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 23 | "reflect" |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 24 | "sort" |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 25 | "strings" |
| 26 | |
| 27 | "github.com/google/blueprint/proptools" |
| 28 | ) |
| 29 | |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 30 | type artifactId int |
| 31 | type depsetId int |
| 32 | type pathFragmentId int |
| 33 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 34 | // artifact contains relevant portions of Bazel's aquery proto, Artifact. |
| 35 | // Represents a single artifact, whether it's a source file or a derived output file. |
| 36 | type artifact struct { |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 37 | Id artifactId |
| 38 | PathFragmentId pathFragmentId |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | type pathFragment struct { |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 42 | Id pathFragmentId |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 43 | Label string |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 44 | ParentId pathFragmentId |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // KeyValuePair represents Bazel's aquery proto, KeyValuePair. |
| 48 | type KeyValuePair struct { |
| 49 | Key string |
| 50 | Value string |
| 51 | } |
| 52 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 53 | // AqueryDepset is a depset definition from Bazel's aquery response. This is |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 54 | // akin to the `depSetOfFiles` in the response proto, except: |
| 55 | // * direct artifacts are enumerated by full path instead of by ID |
MarkDacek | 7564127 | 2022-05-13 20:44:07 +0000 | [diff] [blame] | 56 | // * it has a hash of the depset contents, instead of an int ID (for determinism) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 57 | // A depset is a data structure for efficient transitive handling of artifact |
| 58 | // paths. A single depset consists of one or more artifact paths and one or |
| 59 | // more "child" depsets. |
| 60 | type AqueryDepset struct { |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 61 | ContentHash string |
| 62 | DirectArtifacts []string |
| 63 | TransitiveDepSetHashes []string |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 66 | // depSetOfFiles contains relevant portions of Bazel's aquery proto, DepSetOfFiles. |
| 67 | // Represents a data structure containing one or more files. Depsets in Bazel are an efficient |
| 68 | // data structure for storing large numbers of file paths. |
| 69 | type depSetOfFiles struct { |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 70 | Id depsetId |
| 71 | DirectArtifactIds []artifactId |
| 72 | TransitiveDepSetIds []depsetId |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // action contains relevant portions of Bazel's aquery proto, Action. |
| 76 | // Represents a single command line invocation in the Bazel build graph. |
| 77 | type action struct { |
| 78 | Arguments []string |
| 79 | EnvironmentVariables []KeyValuePair |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 80 | InputDepSetIds []depsetId |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 81 | Mnemonic string |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 82 | OutputIds []artifactId |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 83 | TemplateContent string |
| 84 | Substitutions []KeyValuePair |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 85 | FileContents string |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // actionGraphContainer contains relevant portions of Bazel's aquery proto, ActionGraphContainer. |
| 89 | // An aquery response from Bazel contains a single ActionGraphContainer proto. |
| 90 | type actionGraphContainer struct { |
| 91 | Artifacts []artifact |
| 92 | Actions []action |
| 93 | DepSetOfFiles []depSetOfFiles |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 94 | PathFragments []pathFragment |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // BuildStatement contains information to register a build statement corresponding (one to one) |
| 98 | // with a Bazel action from Bazel's action graph. |
| 99 | type BuildStatement struct { |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 100 | Command string |
| 101 | Depfile *string |
| 102 | OutputPaths []string |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 103 | SymlinkPaths []string |
| 104 | Env []KeyValuePair |
| 105 | Mnemonic string |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 106 | |
| 107 | // Inputs of this build statement, either as unexpanded depsets or expanded |
| 108 | // input paths. There should be no overlap between these fields; an input |
| 109 | // path should either be included as part of an unexpanded depset or a raw |
| 110 | // input path string, but not both. |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 111 | InputDepsetHashes []string |
| 112 | InputPaths []string |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 113 | FileContents string |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 114 | } |
| 115 | |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 116 | // A helper type for aquery processing which facilitates retrieval of path IDs from their |
| 117 | // less readable Bazel structures (depset and path fragment). |
| 118 | type aqueryArtifactHandler struct { |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 119 | // Switches to true if any depset contains only `bazelToolsDependencySentinel` |
| 120 | bazelToolsDependencySentinelNeeded bool |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 121 | // Maps depset id to AqueryDepset, a representation of depset which is |
| 122 | // post-processed for middleman artifact handling, unhandled artifact |
| 123 | // dropping, content hashing, etc. |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 124 | depsetIdToAqueryDepset map[depsetId]AqueryDepset |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 125 | // Maps content hash to AqueryDepset. |
| 126 | depsetHashToAqueryDepset map[string]AqueryDepset |
| 127 | |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 128 | // depsetIdToArtifactIdsCache is a memoization of depset flattening, because flattening |
| 129 | // may be an expensive operation. |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 130 | depsetHashToArtifactPathsCache map[string][]string |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 131 | // Maps artifact ids to fully expanded paths. |
| 132 | artifactIdToPath map[artifactId]string |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 133 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 134 | |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 135 | // The tokens should be substituted with the value specified here, instead of the |
| 136 | // one returned in 'substitutions' of TemplateExpand action. |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 137 | var templateActionOverriddenTokens = map[string]string{ |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 138 | // Uses "python3" for %python_binary% instead of the value returned by aquery |
| 139 | // which is "py3wrapper.sh". See removePy3wrapperScript. |
| 140 | "%python_binary%": "python3", |
| 141 | } |
| 142 | |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 143 | // The file name of py3wrapper.sh, which is used by py_binary targets. |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 144 | const py3wrapperFileName = "/py3wrapper.sh" |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 145 | |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 146 | // A file to be put into depsets that are otherwise empty |
| 147 | const bazelToolsDependencySentinel = "BAZEL_TOOLS_DEPENDENCY_SENTINEL" |
| 148 | |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 149 | func indexBy[K comparable, V any](values []V, keyFn func(v V) K) map[K]V { |
| 150 | m := map[K]V{} |
| 151 | for _, v := range values { |
| 152 | m[keyFn(v)] = v |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 153 | } |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 154 | return m |
| 155 | } |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 156 | |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 157 | func newAqueryHandler(aqueryResult actionGraphContainer) (*aqueryArtifactHandler, error) { |
| 158 | pathFragments := indexBy(aqueryResult.PathFragments, func(pf pathFragment) pathFragmentId { |
| 159 | return pf.Id |
| 160 | }) |
| 161 | |
| 162 | artifactIdToPath := map[artifactId]string{} |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 163 | for _, artifact := range aqueryResult.Artifacts { |
| 164 | artifactPath, err := expandPathFragment(artifact.PathFragmentId, pathFragments) |
| 165 | if err != nil { |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 166 | return nil, err |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 167 | } |
| 168 | artifactIdToPath[artifact.Id] = artifactPath |
| 169 | } |
Chris Parsons | 943f243 | 2021-01-19 11:36:50 -0500 | [diff] [blame] | 170 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 171 | // Map middleman artifact ContentHash to input artifact depset ID. |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 172 | // Middleman artifacts are treated as "substitute" artifacts for mixed builds. For example, |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 173 | // if we find a middleman action which has inputs [foo, bar], and output [baz_middleman], then, |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 174 | // for each other action which has input [baz_middleman], we add [foo, bar] to the inputs for |
| 175 | // that action instead. |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 176 | middlemanIdToDepsetIds := map[artifactId][]depsetId{} |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 177 | for _, actionEntry := range aqueryResult.Actions { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 178 | if actionEntry.Mnemonic == "Middleman" { |
| 179 | for _, outputId := range actionEntry.OutputIds { |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 180 | middlemanIdToDepsetIds[outputId] = actionEntry.InputDepSetIds |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 184 | |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 185 | depsetIdToDepset := indexBy(aqueryResult.DepSetOfFiles, func(d depSetOfFiles) depsetId { |
| 186 | return d.Id |
| 187 | }) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 188 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 189 | aqueryHandler := aqueryArtifactHandler{ |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 190 | depsetIdToAqueryDepset: map[depsetId]AqueryDepset{}, |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 191 | depsetHashToAqueryDepset: map[string]AqueryDepset{}, |
| 192 | depsetHashToArtifactPathsCache: map[string][]string{}, |
| 193 | artifactIdToPath: artifactIdToPath, |
| 194 | } |
| 195 | |
| 196 | // Validate and adjust aqueryResult.DepSetOfFiles values. |
| 197 | for _, depset := range aqueryResult.DepSetOfFiles { |
| 198 | _, err := aqueryHandler.populateDepsetMaps(depset, middlemanIdToDepsetIds, depsetIdToDepset) |
| 199 | if err != nil { |
| 200 | return nil, err |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return &aqueryHandler, nil |
| 205 | } |
| 206 | |
| 207 | // Ensures that the handler's depsetIdToAqueryDepset map contains an entry for the given |
| 208 | // depset. |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 209 | func (a *aqueryArtifactHandler) populateDepsetMaps(depset depSetOfFiles, middlemanIdToDepsetIds map[artifactId][]depsetId, depsetIdToDepset map[depsetId]depSetOfFiles) (AqueryDepset, error) { |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 210 | if aqueryDepset, containsDepset := a.depsetIdToAqueryDepset[depset.Id]; containsDepset { |
| 211 | return aqueryDepset, nil |
| 212 | } |
| 213 | transitiveDepsetIds := depset.TransitiveDepSetIds |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 214 | var directArtifactPaths []string |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 215 | for _, artifactId := range depset.DirectArtifactIds { |
| 216 | path, pathExists := a.artifactIdToPath[artifactId] |
| 217 | if !pathExists { |
| 218 | return AqueryDepset{}, fmt.Errorf("undefined input artifactId %d", artifactId) |
| 219 | } |
| 220 | // Filter out any inputs which are universally dropped, and swap middleman |
| 221 | // artifacts with their corresponding depsets. |
| 222 | if depsetsToUse, isMiddleman := middlemanIdToDepsetIds[artifactId]; isMiddleman { |
| 223 | // Swap middleman artifacts with their corresponding depsets and drop the middleman artifacts. |
| 224 | transitiveDepsetIds = append(transitiveDepsetIds, depsetsToUse...) |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 225 | } else if strings.HasSuffix(path, py3wrapperFileName) || |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 226 | strings.HasPrefix(path, "../bazel_tools") { |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 227 | // Drop these artifacts. |
| 228 | // See go/python-binary-host-mixed-build for more details. |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame^] | 229 | // 1) Drop py3wrapper.sh, just use python binary, the launcher script generated by the |
| 230 | // TemplateExpandAction handles everything necessary to launch a Pythin application. |
| 231 | // 2) ../bazel_tools: they have MODIFY timestamp 10years in the future and would cause the |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 232 | // containing depset to always be considered newer than their outputs. |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 233 | } else { |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 234 | directArtifactPaths = append(directArtifactPaths, path) |
| 235 | } |
| 236 | } |
| 237 | |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 238 | var childDepsetHashes []string |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 239 | for _, childDepsetId := range transitiveDepsetIds { |
| 240 | childDepset, exists := depsetIdToDepset[childDepsetId] |
| 241 | if !exists { |
| 242 | return AqueryDepset{}, fmt.Errorf("undefined input depsetId %d (referenced by depsetId %d)", childDepsetId, depset.Id) |
| 243 | } |
| 244 | childAqueryDepset, err := a.populateDepsetMaps(childDepset, middlemanIdToDepsetIds, depsetIdToDepset) |
| 245 | if err != nil { |
| 246 | return AqueryDepset{}, err |
| 247 | } |
| 248 | childDepsetHashes = append(childDepsetHashes, childAqueryDepset.ContentHash) |
| 249 | } |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 250 | if len(directArtifactPaths) == 0 && len(childDepsetHashes) == 0 { |
| 251 | // We could omit this depset altogether but that requires cleanup on |
| 252 | // transitive dependents. |
| 253 | // As a simpler alternative, we use this sentinel file as a dependency. |
| 254 | directArtifactPaths = append(directArtifactPaths, bazelToolsDependencySentinel) |
| 255 | a.bazelToolsDependencySentinelNeeded = true |
| 256 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 257 | aqueryDepset := AqueryDepset{ |
| 258 | ContentHash: depsetContentHash(directArtifactPaths, childDepsetHashes), |
| 259 | DirectArtifacts: directArtifactPaths, |
| 260 | TransitiveDepSetHashes: childDepsetHashes, |
| 261 | } |
| 262 | a.depsetIdToAqueryDepset[depset.Id] = aqueryDepset |
| 263 | a.depsetHashToAqueryDepset[aqueryDepset.ContentHash] = aqueryDepset |
| 264 | return aqueryDepset, nil |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 265 | } |
| 266 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 267 | // getInputPaths flattens the depsets of the given IDs and returns all transitive |
| 268 | // input paths contained in these depsets. |
| 269 | // This is a potentially expensive operation, and should not be invoked except |
| 270 | // for actions which need specialized input handling. |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 271 | func (a *aqueryArtifactHandler) getInputPaths(depsetIds []depsetId) ([]string, error) { |
| 272 | var inputPaths []string |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 273 | |
| 274 | for _, inputDepSetId := range depsetIds { |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 275 | depset := a.depsetIdToAqueryDepset[inputDepSetId] |
| 276 | inputArtifacts, err := a.artifactPathsFromDepsetHash(depset.ContentHash) |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 277 | if err != nil { |
| 278 | return nil, err |
| 279 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 280 | for _, inputPath := range inputArtifacts { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 281 | inputPaths = append(inputPaths, inputPath) |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 282 | } |
| 283 | } |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 284 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 285 | return inputPaths, nil |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 286 | } |
| 287 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 288 | func (a *aqueryArtifactHandler) artifactPathsFromDepsetHash(depsetHash string) ([]string, error) { |
| 289 | if result, exists := a.depsetHashToArtifactPathsCache[depsetHash]; exists { |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 290 | return result, nil |
| 291 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 292 | if depset, exists := a.depsetHashToAqueryDepset[depsetHash]; exists { |
| 293 | result := depset.DirectArtifacts |
| 294 | for _, childHash := range depset.TransitiveDepSetHashes { |
| 295 | childArtifactIds, err := a.artifactPathsFromDepsetHash(childHash) |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 296 | if err != nil { |
| 297 | return nil, err |
| 298 | } |
| 299 | result = append(result, childArtifactIds...) |
| 300 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 301 | a.depsetHashToArtifactPathsCache[depsetHash] = result |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 302 | return result, nil |
| 303 | } else { |
Usta Shrestha | 2ccdb42 | 2022-06-02 10:19:13 -0400 | [diff] [blame] | 304 | return nil, fmt.Errorf("undefined input depset hash %s", depsetHash) |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 308 | // AqueryBuildStatements returns a slice of BuildStatements and a slice of AqueryDepset |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 309 | // which should be registered (and output to a ninja file) to correspond with Bazel's |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 310 | // action graph, as described by the given action graph json proto. |
| 311 | // BuildStatements are one-to-one with actions in the given action graph, and AqueryDepsets |
| 312 | // are one-to-one with Bazel's depSetOfFiles objects. |
| 313 | func AqueryBuildStatements(aqueryJsonProto []byte) ([]BuildStatement, []AqueryDepset, error) { |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 314 | var aqueryResult actionGraphContainer |
| 315 | err := json.Unmarshal(aqueryJsonProto, &aqueryResult) |
| 316 | if err != nil { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 317 | return nil, nil, err |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 318 | } |
| 319 | aqueryHandler, err := newAqueryHandler(aqueryResult) |
| 320 | if err != nil { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 321 | return nil, nil, err |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 322 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 323 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 324 | var buildStatements []BuildStatement |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 325 | if aqueryHandler.bazelToolsDependencySentinelNeeded { |
| 326 | buildStatements = append(buildStatements, BuildStatement{ |
| 327 | Command: fmt.Sprintf("touch '%s'", bazelToolsDependencySentinel), |
| 328 | OutputPaths: []string{bazelToolsDependencySentinel}, |
| 329 | Mnemonic: bazelToolsDependencySentinel, |
| 330 | }) |
| 331 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 332 | |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 333 | for _, actionEntry := range aqueryResult.Actions { |
| 334 | if shouldSkipAction(actionEntry) { |
| 335 | continue |
| 336 | } |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 337 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 338 | var buildStatement BuildStatement |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 339 | if actionEntry.isSymlinkAction() { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 340 | buildStatement, err = aqueryHandler.symlinkActionBuildStatement(actionEntry) |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 341 | } else if actionEntry.isTemplateExpandAction() && len(actionEntry.Arguments) < 1 { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 342 | buildStatement, err = aqueryHandler.templateExpandActionBuildStatement(actionEntry) |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 343 | } else if actionEntry.isFileWriteAction() { |
| 344 | buildStatement, err = aqueryHandler.fileWriteActionBuildStatement(actionEntry) |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame^] | 345 | } else if actionEntry.isSymlinkTreeAction() { |
| 346 | buildStatement, err = aqueryHandler.symlinkTreeActionBuildStatement(actionEntry) |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 347 | } else if len(actionEntry.Arguments) < 1 { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 348 | return nil, nil, fmt.Errorf("received action with no command: [%s]", actionEntry.Mnemonic) |
| 349 | } else { |
| 350 | buildStatement, err = aqueryHandler.normalActionBuildStatement(actionEntry) |
| 351 | } |
| 352 | |
| 353 | if err != nil { |
| 354 | return nil, nil, err |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 355 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 356 | buildStatements = append(buildStatements, buildStatement) |
| 357 | } |
| 358 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 359 | depsetsByHash := map[string]AqueryDepset{} |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 360 | var depsets []AqueryDepset |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 361 | for _, aqueryDepset := range aqueryHandler.depsetIdToAqueryDepset { |
| 362 | if prevEntry, hasKey := depsetsByHash[aqueryDepset.ContentHash]; hasKey { |
| 363 | // Two depsets collide on hash. Ensure that their contents are identical. |
| 364 | if !reflect.DeepEqual(aqueryDepset, prevEntry) { |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 365 | return nil, nil, fmt.Errorf("two different depsets have the same hash: %v, %v", prevEntry, aqueryDepset) |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 366 | } |
| 367 | } else { |
| 368 | depsetsByHash[aqueryDepset.ContentHash] = aqueryDepset |
| 369 | depsets = append(depsets, aqueryDepset) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 370 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 371 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 372 | |
| 373 | // Build Statements and depsets must be sorted by their content hash to |
| 374 | // preserve determinism between builds (this will result in consistent ninja file |
| 375 | // output). Note they are not sorted by their original IDs nor their Bazel ordering, |
| 376 | // as Bazel gives nondeterministic ordering / identifiers in aquery responses. |
| 377 | sort.Slice(buildStatements, func(i, j int) bool { |
| 378 | // For build statements, compare output lists. In Bazel, each output file |
| 379 | // may only have one action which generates it, so this will provide |
| 380 | // a deterministic ordering. |
| 381 | outputs_i := buildStatements[i].OutputPaths |
| 382 | outputs_j := buildStatements[j].OutputPaths |
| 383 | if len(outputs_i) != len(outputs_j) { |
| 384 | return len(outputs_i) < len(outputs_j) |
| 385 | } |
| 386 | if len(outputs_i) == 0 { |
| 387 | // No outputs for these actions, so compare commands. |
| 388 | return buildStatements[i].Command < buildStatements[j].Command |
| 389 | } |
| 390 | // There may be multiple outputs, but the output ordering is deterministic. |
| 391 | return outputs_i[0] < outputs_j[0] |
| 392 | }) |
| 393 | sort.Slice(depsets, func(i, j int) bool { |
| 394 | return depsets[i].ContentHash < depsets[j].ContentHash |
| 395 | }) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 396 | return buildStatements, depsets, nil |
| 397 | } |
| 398 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 399 | // depsetContentHash computes and returns a SHA256 checksum of the contents of |
| 400 | // the given depset. This content hash may serve as the depset's identifier. |
| 401 | // Using a content hash for an identifier is superior for determinism. (For example, |
| 402 | // using an integer identifier which depends on the order in which the depsets are |
| 403 | // created would result in nondeterministic depset IDs.) |
| 404 | func depsetContentHash(directPaths []string, transitiveDepsetHashes []string) string { |
| 405 | h := sha256.New() |
| 406 | // Use newline as delimiter, as paths cannot contain newline. |
| 407 | h.Write([]byte(strings.Join(directPaths, "\n"))) |
Usta Shrestha | 2ccdb42 | 2022-06-02 10:19:13 -0400 | [diff] [blame] | 408 | h.Write([]byte(strings.Join(transitiveDepsetHashes, ""))) |
| 409 | fullHash := base64.RawURLEncoding.EncodeToString(h.Sum(nil)) |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 410 | return fullHash |
| 411 | } |
| 412 | |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 413 | func (a *aqueryArtifactHandler) depsetContentHashes(inputDepsetIds []depsetId) ([]string, error) { |
| 414 | var hashes []string |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 415 | for _, depsetId := range inputDepsetIds { |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 416 | if aqueryDepset, exists := a.depsetIdToAqueryDepset[depsetId]; !exists { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 417 | return nil, fmt.Errorf("undefined input depsetId %d", depsetId) |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 418 | } else { |
| 419 | hashes = append(hashes, aqueryDepset.ContentHash) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 420 | } |
| 421 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 422 | return hashes, nil |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 423 | } |
| 424 | |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 425 | func (a *aqueryArtifactHandler) normalActionBuildStatement(actionEntry action) (BuildStatement, error) { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 426 | command := strings.Join(proptools.ShellEscapeListIncludingSpaces(actionEntry.Arguments), " ") |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 427 | inputDepsetHashes, err := a.depsetContentHashes(actionEntry.InputDepSetIds) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 428 | if err != nil { |
| 429 | return BuildStatement{}, err |
| 430 | } |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 431 | outputPaths, depfile, err := a.getOutputPaths(actionEntry) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 432 | if err != nil { |
| 433 | return BuildStatement{}, err |
| 434 | } |
| 435 | |
| 436 | buildStatement := BuildStatement{ |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 437 | Command: command, |
| 438 | Depfile: depfile, |
| 439 | OutputPaths: outputPaths, |
| 440 | InputDepsetHashes: inputDepsetHashes, |
| 441 | Env: actionEntry.EnvironmentVariables, |
| 442 | Mnemonic: actionEntry.Mnemonic, |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 443 | } |
| 444 | return buildStatement, nil |
| 445 | } |
| 446 | |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 447 | func (a *aqueryArtifactHandler) templateExpandActionBuildStatement(actionEntry action) (BuildStatement, error) { |
| 448 | outputPaths, depfile, err := a.getOutputPaths(actionEntry) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 449 | if err != nil { |
| 450 | return BuildStatement{}, err |
| 451 | } |
| 452 | if len(outputPaths) != 1 { |
| 453 | return BuildStatement{}, fmt.Errorf("Expect 1 output to template expand action, got: output %q", outputPaths) |
| 454 | } |
| 455 | expandedTemplateContent := expandTemplateContent(actionEntry) |
| 456 | // The expandedTemplateContent is escaped for being used in double quotes and shell unescape, |
| 457 | // and the new line characters (\n) are also changed to \\n which avoids some Ninja escape on \n, which might |
| 458 | // change \n to space and mess up the format of Python programs. |
| 459 | // sed is used to convert \\n back to \n before saving to output file. |
| 460 | // See go/python-binary-host-mixed-build for more details. |
| 461 | command := fmt.Sprintf(`/bin/bash -c 'echo "%[1]s" | sed "s/\\\\n/\\n/g" > %[2]s && chmod a+x %[2]s'`, |
| 462 | escapeCommandlineArgument(expandedTemplateContent), outputPaths[0]) |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 463 | inputDepsetHashes, err := a.depsetContentHashes(actionEntry.InputDepSetIds) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 464 | if err != nil { |
| 465 | return BuildStatement{}, err |
| 466 | } |
| 467 | |
| 468 | buildStatement := BuildStatement{ |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 469 | Command: command, |
| 470 | Depfile: depfile, |
| 471 | OutputPaths: outputPaths, |
| 472 | InputDepsetHashes: inputDepsetHashes, |
| 473 | Env: actionEntry.EnvironmentVariables, |
| 474 | Mnemonic: actionEntry.Mnemonic, |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 475 | } |
| 476 | return buildStatement, nil |
| 477 | } |
| 478 | |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 479 | func (a *aqueryArtifactHandler) fileWriteActionBuildStatement(actionEntry action) (BuildStatement, error) { |
| 480 | outputPaths, _, err := a.getOutputPaths(actionEntry) |
| 481 | var depsetHashes []string |
| 482 | if err == nil { |
| 483 | depsetHashes, err = a.depsetContentHashes(actionEntry.InputDepSetIds) |
| 484 | } |
| 485 | if err != nil { |
| 486 | return BuildStatement{}, err |
| 487 | } |
| 488 | return BuildStatement{ |
| 489 | Depfile: nil, |
| 490 | OutputPaths: outputPaths, |
| 491 | Env: actionEntry.EnvironmentVariables, |
| 492 | Mnemonic: actionEntry.Mnemonic, |
| 493 | InputDepsetHashes: depsetHashes, |
| 494 | FileContents: actionEntry.FileContents, |
| 495 | }, nil |
| 496 | } |
| 497 | |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame^] | 498 | func (a *aqueryArtifactHandler) symlinkTreeActionBuildStatement(actionEntry action) (BuildStatement, error) { |
| 499 | outputPaths, _, err := a.getOutputPaths(actionEntry) |
| 500 | if err != nil { |
| 501 | return BuildStatement{}, err |
| 502 | } |
| 503 | inputPaths, err := a.getInputPaths(actionEntry.InputDepSetIds) |
| 504 | if err != nil { |
| 505 | return BuildStatement{}, err |
| 506 | } |
| 507 | if len(inputPaths) != 1 || len(outputPaths) != 1 { |
| 508 | return BuildStatement{}, fmt.Errorf("Expect 1 input and 1 output to symlink action, got: input %q, output %q", inputPaths, outputPaths) |
| 509 | } |
| 510 | // The actual command is generated in bazelSingleton.GenerateBuildActions |
| 511 | return BuildStatement{ |
| 512 | Depfile: nil, |
| 513 | OutputPaths: outputPaths, |
| 514 | Env: actionEntry.EnvironmentVariables, |
| 515 | Mnemonic: actionEntry.Mnemonic, |
| 516 | InputPaths: inputPaths, |
| 517 | }, nil |
| 518 | } |
| 519 | |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 520 | func (a *aqueryArtifactHandler) symlinkActionBuildStatement(actionEntry action) (BuildStatement, error) { |
| 521 | outputPaths, depfile, err := a.getOutputPaths(actionEntry) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 522 | if err != nil { |
| 523 | return BuildStatement{}, err |
| 524 | } |
| 525 | |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 526 | inputPaths, err := a.getInputPaths(actionEntry.InputDepSetIds) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 527 | if err != nil { |
| 528 | return BuildStatement{}, err |
| 529 | } |
| 530 | if len(inputPaths) != 1 || len(outputPaths) != 1 { |
| 531 | return BuildStatement{}, fmt.Errorf("Expect 1 input and 1 output to symlink action, got: input %q, output %q", inputPaths, outputPaths) |
| 532 | } |
| 533 | out := outputPaths[0] |
| 534 | outDir := proptools.ShellEscapeIncludingSpaces(filepath.Dir(out)) |
| 535 | out = proptools.ShellEscapeIncludingSpaces(out) |
| 536 | in := filepath.Join("$PWD", proptools.ShellEscapeIncludingSpaces(inputPaths[0])) |
| 537 | // Use absolute paths, because some soong actions don't play well with relative paths (for example, `cp -d`). |
| 538 | command := fmt.Sprintf("mkdir -p %[1]s && rm -f %[2]s && ln -sf %[3]s %[2]s", outDir, out, in) |
| 539 | symlinkPaths := outputPaths[:] |
| 540 | |
| 541 | buildStatement := BuildStatement{ |
| 542 | Command: command, |
| 543 | Depfile: depfile, |
| 544 | OutputPaths: outputPaths, |
| 545 | InputPaths: inputPaths, |
| 546 | Env: actionEntry.EnvironmentVariables, |
| 547 | Mnemonic: actionEntry.Mnemonic, |
| 548 | SymlinkPaths: symlinkPaths, |
| 549 | } |
| 550 | return buildStatement, nil |
| 551 | } |
| 552 | |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 553 | func (a *aqueryArtifactHandler) getOutputPaths(actionEntry action) (outputPaths []string, depfile *string, err error) { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 554 | for _, outputId := range actionEntry.OutputIds { |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 555 | outputPath, exists := a.artifactIdToPath[outputId] |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 556 | if !exists { |
| 557 | err = fmt.Errorf("undefined outputId %d", outputId) |
| 558 | return |
| 559 | } |
| 560 | ext := filepath.Ext(outputPath) |
| 561 | if ext == ".d" { |
| 562 | if depfile != nil { |
| 563 | err = fmt.Errorf("found multiple potential depfiles %q, %q", *depfile, outputPath) |
| 564 | return |
| 565 | } else { |
| 566 | depfile = &outputPath |
| 567 | } |
| 568 | } else { |
| 569 | outputPaths = append(outputPaths, outputPath) |
| 570 | } |
| 571 | } |
| 572 | return |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 573 | } |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 574 | |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 575 | // expandTemplateContent substitutes the tokens in a template. |
| 576 | func expandTemplateContent(actionEntry action) string { |
| 577 | replacerString := []string{} |
| 578 | for _, pair := range actionEntry.Substitutions { |
| 579 | value := pair.Value |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 580 | if val, ok := templateActionOverriddenTokens[pair.Key]; ok { |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 581 | value = val |
| 582 | } |
| 583 | replacerString = append(replacerString, pair.Key, value) |
| 584 | } |
| 585 | replacer := strings.NewReplacer(replacerString...) |
| 586 | return replacer.Replace(actionEntry.TemplateContent) |
| 587 | } |
| 588 | |
| 589 | func escapeCommandlineArgument(str string) string { |
| 590 | // \->\\, $->\$, `->\`, "->\", \n->\\n, '->'"'"' |
| 591 | replacer := strings.NewReplacer( |
| 592 | `\`, `\\`, |
| 593 | `$`, `\$`, |
| 594 | "`", "\\`", |
| 595 | `"`, `\"`, |
| 596 | "\n", "\\n", |
| 597 | `'`, `'"'"'`, |
| 598 | ) |
| 599 | return replacer.Replace(str) |
| 600 | } |
| 601 | |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 602 | func (a action) isSymlinkAction() bool { |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 603 | return a.Mnemonic == "Symlink" || a.Mnemonic == "SolibSymlink" || a.Mnemonic == "ExecutableSymlink" |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 604 | } |
| 605 | |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 606 | func (a action) isTemplateExpandAction() bool { |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 607 | return a.Mnemonic == "TemplateExpand" |
| 608 | } |
| 609 | |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 610 | func (a action) isFileWriteAction() bool { |
| 611 | return a.Mnemonic == "FileWrite" || a.Mnemonic == "SourceSymlinkManifest" |
| 612 | } |
| 613 | |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame^] | 614 | func (a action) isSymlinkTreeAction() bool { |
| 615 | return a.Mnemonic == "SymlinkTree" |
| 616 | } |
| 617 | |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 618 | func shouldSkipAction(a action) bool { |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 619 | // Middleman actions are not handled like other actions; they are handled separately as a |
| 620 | // preparatory step so that their inputs may be relayed to actions depending on middleman |
| 621 | // artifacts. |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 622 | if a.Mnemonic == "Middleman" { |
| 623 | return true |
| 624 | } |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame^] | 625 | // PythonZipper is bogus action returned by aquery, ignore it (b/236198693) |
| 626 | if a.Mnemonic == "PythonZipper" { |
| 627 | return true |
| 628 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 629 | // Skip "Fail" actions, which are placeholder actions designed to always fail. |
| 630 | if a.Mnemonic == "Fail" { |
| 631 | return true |
| 632 | } |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 633 | if a.Mnemonic == "BaselineCoverage" { |
| 634 | return true |
| 635 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 636 | return false |
| 637 | } |
| 638 | |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 639 | func expandPathFragment(id pathFragmentId, pathFragmentsMap map[pathFragmentId]pathFragment) (string, error) { |
| 640 | var labels []string |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 641 | currId := id |
| 642 | // Only positive IDs are valid for path fragments. An ID of zero indicates a terminal node. |
| 643 | for currId > 0 { |
| 644 | currFragment, ok := pathFragmentsMap[currId] |
| 645 | if !ok { |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 646 | return "", fmt.Errorf("undefined path fragment id %d", currId) |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 647 | } |
| 648 | labels = append([]string{currFragment.Label}, labels...) |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 649 | if currId == currFragment.ParentId { |
| 650 | return "", fmt.Errorf("Fragment cannot refer to itself as parent %#v", currFragment) |
| 651 | } |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 652 | currId = currFragment.ParentId |
| 653 | } |
| 654 | return filepath.Join(labels...), nil |
| 655 | } |