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