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