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