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