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