blob: bc823b36ee6dea3515fdc23350068331fd6a8e7a [file] [log] [blame]
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001// 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
15package bazel
16
17import (
Chris Parsons0bfb1c02022-05-12 16:43:01 -040018 "crypto/sha256"
Usta Shrestha2ccdb422022-06-02 10:19:13 -040019 "encoding/base64"
Chris Parsonsaffbb602020-12-23 12:02:11 -050020 "fmt"
21 "path/filepath"
Chris Parsons0bfb1c02022-05-12 16:43:01 -040022 "reflect"
Chris Parsons0bfb1c02022-05-12 16:43:01 -040023 "sort"
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050024 "strings"
25
26 "github.com/google/blueprint/proptools"
Jason Wu6fe87212022-10-21 02:57:41 -040027 "google.golang.org/protobuf/proto"
28 analysis_v2_proto "prebuilts/bazel/common/proto/analysis_v2"
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050029)
30
Usta Shrestha6298cc52022-05-27 17:40:21 -040031type artifactId int
32type depsetId int
33type pathFragmentId int
34
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050035// artifact contains relevant portions of Bazel's aquery proto, Artifact.
36// Represents a single artifact, whether it's a source file or a derived output file.
37type artifact struct {
Usta Shrestha6298cc52022-05-27 17:40:21 -040038 Id artifactId
39 PathFragmentId pathFragmentId
Chris Parsonsaffbb602020-12-23 12:02:11 -050040}
41
42type pathFragment struct {
Usta Shrestha6298cc52022-05-27 17:40:21 -040043 Id pathFragmentId
Chris Parsonsaffbb602020-12-23 12:02:11 -050044 Label string
Usta Shrestha6298cc52022-05-27 17:40:21 -040045 ParentId pathFragmentId
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050046}
47
48// KeyValuePair represents Bazel's aquery proto, KeyValuePair.
49type KeyValuePair struct {
50 Key string
51 Value string
52}
53
Chris Parsons1a7aca02022-04-25 22:35:15 -040054// AqueryDepset is a depset definition from Bazel's aquery response. This is
Chris Parsons0bfb1c02022-05-12 16:43:01 -040055// akin to the `depSetOfFiles` in the response proto, except:
Colin Crossd079e0b2022-08-16 10:27:33 -070056// - direct artifacts are enumerated by full path instead of by ID
57// - it has a hash of the depset contents, instead of an int ID (for determinism)
58//
Chris Parsons1a7aca02022-04-25 22:35:15 -040059// A depset is a data structure for efficient transitive handling of artifact
60// paths. A single depset consists of one or more artifact paths and one or
61// more "child" depsets.
62type AqueryDepset struct {
Chris Parsons0bfb1c02022-05-12 16:43:01 -040063 ContentHash string
64 DirectArtifacts []string
65 TransitiveDepSetHashes []string
Chris Parsons1a7aca02022-04-25 22:35:15 -040066}
67
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050068// depSetOfFiles contains relevant portions of Bazel's aquery proto, DepSetOfFiles.
69// Represents a data structure containing one or more files. Depsets in Bazel are an efficient
70// data structure for storing large numbers of file paths.
71type depSetOfFiles struct {
Usta Shrestha6298cc52022-05-27 17:40:21 -040072 Id depsetId
73 DirectArtifactIds []artifactId
74 TransitiveDepSetIds []depsetId
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050075}
76
77// action contains relevant portions of Bazel's aquery proto, Action.
78// Represents a single command line invocation in the Bazel build graph.
79type action struct {
80 Arguments []string
81 EnvironmentVariables []KeyValuePair
Usta Shrestha6298cc52022-05-27 17:40:21 -040082 InputDepSetIds []depsetId
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050083 Mnemonic string
Usta Shrestha6298cc52022-05-27 17:40:21 -040084 OutputIds []artifactId
Wei Li455ba832021-11-04 22:58:12 +000085 TemplateContent string
86 Substitutions []KeyValuePair
Sasha Smundak1da064c2022-06-08 16:36:16 -070087 FileContents string
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050088}
89
90// actionGraphContainer contains relevant portions of Bazel's aquery proto, ActionGraphContainer.
91// An aquery response from Bazel contains a single ActionGraphContainer proto.
92type actionGraphContainer struct {
93 Artifacts []artifact
94 Actions []action
95 DepSetOfFiles []depSetOfFiles
Chris Parsonsaffbb602020-12-23 12:02:11 -050096 PathFragments []pathFragment
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050097}
98
99// BuildStatement contains information to register a build statement corresponding (one to one)
100// with a Bazel action from Bazel's action graph.
101type BuildStatement struct {
Liz Kammerc49e6822021-06-08 15:04:11 -0400102 Command string
103 Depfile *string
104 OutputPaths []string
Liz Kammerc49e6822021-06-08 15:04:11 -0400105 SymlinkPaths []string
106 Env []KeyValuePair
107 Mnemonic string
Chris Parsons1a7aca02022-04-25 22:35:15 -0400108
109 // Inputs of this build statement, either as unexpanded depsets or expanded
110 // input paths. There should be no overlap between these fields; an input
111 // path should either be included as part of an unexpanded depset or a raw
112 // input path string, but not both.
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400113 InputDepsetHashes []string
114 InputPaths []string
Sasha Smundak1da064c2022-06-08 16:36:16 -0700115 FileContents string
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500116}
117
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400118// A helper type for aquery processing which facilitates retrieval of path IDs from their
119// less readable Bazel structures (depset and path fragment).
120type aqueryArtifactHandler struct {
Usta Shresthaef922252022-06-02 14:23:02 -0400121 // Switches to true if any depset contains only `bazelToolsDependencySentinel`
122 bazelToolsDependencySentinelNeeded bool
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400123 // Maps depset id to AqueryDepset, a representation of depset which is
124 // post-processed for middleman artifact handling, unhandled artifact
125 // dropping, content hashing, etc.
Usta Shrestha6298cc52022-05-27 17:40:21 -0400126 depsetIdToAqueryDepset map[depsetId]AqueryDepset
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400127 // Maps content hash to AqueryDepset.
128 depsetHashToAqueryDepset map[string]AqueryDepset
129
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400130 // depsetIdToArtifactIdsCache is a memoization of depset flattening, because flattening
131 // may be an expensive operation.
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400132 depsetHashToArtifactPathsCache map[string][]string
Usta Shrestha6298cc52022-05-27 17:40:21 -0400133 // Maps artifact ids to fully expanded paths.
134 artifactIdToPath map[artifactId]string
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400135}
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500136
Wei Li455ba832021-11-04 22:58:12 +0000137// The tokens should be substituted with the value specified here, instead of the
138// one returned in 'substitutions' of TemplateExpand action.
Usta Shrestha6298cc52022-05-27 17:40:21 -0400139var templateActionOverriddenTokens = map[string]string{
Wei Li455ba832021-11-04 22:58:12 +0000140 // Uses "python3" for %python_binary% instead of the value returned by aquery
141 // which is "py3wrapper.sh". See removePy3wrapperScript.
142 "%python_binary%": "python3",
143}
144
Wei Li455ba832021-11-04 22:58:12 +0000145// The file name of py3wrapper.sh, which is used by py_binary targets.
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400146const py3wrapperFileName = "/py3wrapper.sh"
Wei Li455ba832021-11-04 22:58:12 +0000147
Usta Shresthaef922252022-06-02 14:23:02 -0400148// A file to be put into depsets that are otherwise empty
149const bazelToolsDependencySentinel = "BAZEL_TOOLS_DEPENDENCY_SENTINEL"
150
Usta Shrestha6298cc52022-05-27 17:40:21 -0400151func indexBy[K comparable, V any](values []V, keyFn func(v V) K) map[K]V {
152 m := map[K]V{}
153 for _, v := range values {
154 m[keyFn(v)] = v
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500155 }
Usta Shrestha6298cc52022-05-27 17:40:21 -0400156 return m
157}
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400158
Usta Shrestha6298cc52022-05-27 17:40:21 -0400159func newAqueryHandler(aqueryResult actionGraphContainer) (*aqueryArtifactHandler, error) {
160 pathFragments := indexBy(aqueryResult.PathFragments, func(pf pathFragment) pathFragmentId {
161 return pf.Id
162 })
163
164 artifactIdToPath := map[artifactId]string{}
Chris Parsonsaffbb602020-12-23 12:02:11 -0500165 for _, artifact := range aqueryResult.Artifacts {
166 artifactPath, err := expandPathFragment(artifact.PathFragmentId, pathFragments)
167 if err != nil {
Chris Parsons4f069892021-01-15 12:22:41 -0500168 return nil, err
Chris Parsonsaffbb602020-12-23 12:02:11 -0500169 }
170 artifactIdToPath[artifact.Id] = artifactPath
171 }
Chris Parsons943f2432021-01-19 11:36:50 -0500172
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400173 // Map middleman artifact ContentHash to input artifact depset ID.
Chris Parsons1a7aca02022-04-25 22:35:15 -0400174 // Middleman artifacts are treated as "substitute" artifacts for mixed builds. For example,
Usta Shrestha16ac1352022-06-22 11:01:55 -0400175 // if we find a middleman action which has inputs [foo, bar], and output [baz_middleman], then,
Chris Parsons1a7aca02022-04-25 22:35:15 -0400176 // for each other action which has input [baz_middleman], we add [foo, bar] to the inputs for
177 // that action instead.
Usta Shrestha6298cc52022-05-27 17:40:21 -0400178 middlemanIdToDepsetIds := map[artifactId][]depsetId{}
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500179 for _, actionEntry := range aqueryResult.Actions {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500180 if actionEntry.Mnemonic == "Middleman" {
181 for _, outputId := range actionEntry.OutputIds {
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400182 middlemanIdToDepsetIds[outputId] = actionEntry.InputDepSetIds
Chris Parsons8d6e4332021-02-22 16:13:50 -0500183 }
184 }
185 }
Chris Parsons1a7aca02022-04-25 22:35:15 -0400186
Usta Shrestha6298cc52022-05-27 17:40:21 -0400187 depsetIdToDepset := indexBy(aqueryResult.DepSetOfFiles, func(d depSetOfFiles) depsetId {
188 return d.Id
189 })
Chris Parsons1a7aca02022-04-25 22:35:15 -0400190
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400191 aqueryHandler := aqueryArtifactHandler{
Usta Shrestha6298cc52022-05-27 17:40:21 -0400192 depsetIdToAqueryDepset: map[depsetId]AqueryDepset{},
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400193 depsetHashToAqueryDepset: map[string]AqueryDepset{},
194 depsetHashToArtifactPathsCache: map[string][]string{},
195 artifactIdToPath: artifactIdToPath,
196 }
197
198 // Validate and adjust aqueryResult.DepSetOfFiles values.
199 for _, depset := range aqueryResult.DepSetOfFiles {
200 _, err := aqueryHandler.populateDepsetMaps(depset, middlemanIdToDepsetIds, depsetIdToDepset)
201 if err != nil {
202 return nil, err
203 }
204 }
205
206 return &aqueryHandler, nil
207}
208
209// Ensures that the handler's depsetIdToAqueryDepset map contains an entry for the given
210// depset.
Usta Shrestha6298cc52022-05-27 17:40:21 -0400211func (a *aqueryArtifactHandler) populateDepsetMaps(depset depSetOfFiles, middlemanIdToDepsetIds map[artifactId][]depsetId, depsetIdToDepset map[depsetId]depSetOfFiles) (AqueryDepset, error) {
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400212 if aqueryDepset, containsDepset := a.depsetIdToAqueryDepset[depset.Id]; containsDepset {
213 return aqueryDepset, nil
214 }
215 transitiveDepsetIds := depset.TransitiveDepSetIds
Usta Shrestha6298cc52022-05-27 17:40:21 -0400216 var directArtifactPaths []string
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400217 for _, artifactId := range depset.DirectArtifactIds {
218 path, pathExists := a.artifactIdToPath[artifactId]
219 if !pathExists {
220 return AqueryDepset{}, fmt.Errorf("undefined input artifactId %d", artifactId)
221 }
222 // Filter out any inputs which are universally dropped, and swap middleman
223 // artifacts with their corresponding depsets.
224 if depsetsToUse, isMiddleman := middlemanIdToDepsetIds[artifactId]; isMiddleman {
225 // Swap middleman artifacts with their corresponding depsets and drop the middleman artifacts.
226 transitiveDepsetIds = append(transitiveDepsetIds, depsetsToUse...)
Usta Shresthaef922252022-06-02 14:23:02 -0400227 } else if strings.HasSuffix(path, py3wrapperFileName) ||
Usta Shresthaef922252022-06-02 14:23:02 -0400228 strings.HasPrefix(path, "../bazel_tools") {
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400229 // Drop these artifacts.
230 // See go/python-binary-host-mixed-build for more details.
Sasha Smundakc180dbd2022-07-03 14:55:58 -0700231 // 1) Drop py3wrapper.sh, just use python binary, the launcher script generated by the
232 // TemplateExpandAction handles everything necessary to launch a Pythin application.
233 // 2) ../bazel_tools: they have MODIFY timestamp 10years in the future and would cause the
Usta Shresthaef922252022-06-02 14:23:02 -0400234 // containing depset to always be considered newer than their outputs.
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400235 } else {
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400236 directArtifactPaths = append(directArtifactPaths, path)
237 }
238 }
239
Usta Shrestha6298cc52022-05-27 17:40:21 -0400240 var childDepsetHashes []string
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400241 for _, childDepsetId := range transitiveDepsetIds {
242 childDepset, exists := depsetIdToDepset[childDepsetId]
243 if !exists {
244 return AqueryDepset{}, fmt.Errorf("undefined input depsetId %d (referenced by depsetId %d)", childDepsetId, depset.Id)
245 }
246 childAqueryDepset, err := a.populateDepsetMaps(childDepset, middlemanIdToDepsetIds, depsetIdToDepset)
247 if err != nil {
248 return AqueryDepset{}, err
249 }
250 childDepsetHashes = append(childDepsetHashes, childAqueryDepset.ContentHash)
251 }
Usta Shresthaef922252022-06-02 14:23:02 -0400252 if len(directArtifactPaths) == 0 && len(childDepsetHashes) == 0 {
253 // We could omit this depset altogether but that requires cleanup on
254 // transitive dependents.
255 // As a simpler alternative, we use this sentinel file as a dependency.
256 directArtifactPaths = append(directArtifactPaths, bazelToolsDependencySentinel)
257 a.bazelToolsDependencySentinelNeeded = true
258 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400259 aqueryDepset := AqueryDepset{
260 ContentHash: depsetContentHash(directArtifactPaths, childDepsetHashes),
261 DirectArtifacts: directArtifactPaths,
262 TransitiveDepSetHashes: childDepsetHashes,
263 }
264 a.depsetIdToAqueryDepset[depset.Id] = aqueryDepset
265 a.depsetHashToAqueryDepset[aqueryDepset.ContentHash] = aqueryDepset
266 return aqueryDepset, nil
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400267}
268
Chris Parsons1a7aca02022-04-25 22:35:15 -0400269// getInputPaths flattens the depsets of the given IDs and returns all transitive
270// input paths contained in these depsets.
271// This is a potentially expensive operation, and should not be invoked except
272// for actions which need specialized input handling.
Usta Shrestha6298cc52022-05-27 17:40:21 -0400273func (a *aqueryArtifactHandler) getInputPaths(depsetIds []depsetId) ([]string, error) {
274 var inputPaths []string
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400275
276 for _, inputDepSetId := range depsetIds {
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400277 depset := a.depsetIdToAqueryDepset[inputDepSetId]
278 inputArtifacts, err := a.artifactPathsFromDepsetHash(depset.ContentHash)
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400279 if err != nil {
280 return nil, err
281 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400282 for _, inputPath := range inputArtifacts {
Chris Parsons1a7aca02022-04-25 22:35:15 -0400283 inputPaths = append(inputPaths, inputPath)
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400284 }
285 }
Wei Li455ba832021-11-04 22:58:12 +0000286
Chris Parsons1a7aca02022-04-25 22:35:15 -0400287 return inputPaths, nil
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400288}
289
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400290func (a *aqueryArtifactHandler) artifactPathsFromDepsetHash(depsetHash string) ([]string, error) {
291 if result, exists := a.depsetHashToArtifactPathsCache[depsetHash]; exists {
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400292 return result, nil
293 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400294 if depset, exists := a.depsetHashToAqueryDepset[depsetHash]; exists {
295 result := depset.DirectArtifacts
296 for _, childHash := range depset.TransitiveDepSetHashes {
297 childArtifactIds, err := a.artifactPathsFromDepsetHash(childHash)
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400298 if err != nil {
299 return nil, err
300 }
301 result = append(result, childArtifactIds...)
302 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400303 a.depsetHashToArtifactPathsCache[depsetHash] = result
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400304 return result, nil
305 } else {
Usta Shrestha2ccdb422022-06-02 10:19:13 -0400306 return nil, fmt.Errorf("undefined input depset hash %s", depsetHash)
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400307 }
308}
309
Chris Parsons1a7aca02022-04-25 22:35:15 -0400310// AqueryBuildStatements returns a slice of BuildStatements and a slice of AqueryDepset
Usta Shrestha6298cc52022-05-27 17:40:21 -0400311// which should be registered (and output to a ninja file) to correspond with Bazel's
Chris Parsons1a7aca02022-04-25 22:35:15 -0400312// action graph, as described by the given action graph json proto.
313// BuildStatements are one-to-one with actions in the given action graph, and AqueryDepsets
314// are one-to-one with Bazel's depSetOfFiles objects.
315func AqueryBuildStatements(aqueryJsonProto []byte) ([]BuildStatement, []AqueryDepset, error) {
Jason Wu6fe87212022-10-21 02:57:41 -0400316 aqueryProto := &analysis_v2_proto.ActionGraphContainer{}
317 err := proto.Unmarshal(aqueryJsonProto, aqueryProto)
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400318 if err != nil {
Chris Parsons1a7aca02022-04-25 22:35:15 -0400319 return nil, nil, err
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400320 }
Jason Wu6fe87212022-10-21 02:57:41 -0400321 aqueryResult := actionGraphContainer{}
322
323 for _, protoArtifact := range aqueryProto.Artifacts {
324 aqueryResult.Artifacts = append(aqueryResult.Artifacts, artifact{artifactId(protoArtifact.Id),
325 pathFragmentId(protoArtifact.PathFragmentId)})
326 }
327
328 for _, protoAction := range aqueryProto.Actions {
329 var environmentVariable []KeyValuePair
330 var inputDepSetIds []depsetId
331 var outputIds []artifactId
332 var substitutions []KeyValuePair
333
334 for _, protoEnvironmentVariable := range protoAction.EnvironmentVariables {
335 environmentVariable = append(environmentVariable, KeyValuePair{
336 protoEnvironmentVariable.Key, protoEnvironmentVariable.Value,
337 })
338 }
339 for _, protoInputDepSetIds := range protoAction.InputDepSetIds {
340 inputDepSetIds = append(inputDepSetIds, depsetId(protoInputDepSetIds))
341 }
342 for _, protoOutputIds := range protoAction.OutputIds {
343 outputIds = append(outputIds, artifactId(protoOutputIds))
344 }
345 for _, protoSubstitutions := range protoAction.Substitutions {
346 substitutions = append(substitutions, KeyValuePair{
347 protoSubstitutions.Key, protoSubstitutions.Value,
348 })
349 }
350
351 aqueryResult.Actions = append(aqueryResult.Actions,
352 action{
353 Arguments: protoAction.Arguments,
354 EnvironmentVariables: environmentVariable,
355 InputDepSetIds: inputDepSetIds,
356 Mnemonic: protoAction.Mnemonic,
357 OutputIds: outputIds,
358 TemplateContent: protoAction.TemplateContent,
359 Substitutions: substitutions,
360 FileContents: protoAction.FileContents})
361 }
362
363 for _, protoDepSetOfFiles := range aqueryProto.DepSetOfFiles {
364 var directArtifactIds []artifactId
365 var transitiveDepSetIds []depsetId
366
367 for _, protoDirectArtifactIds := range protoDepSetOfFiles.DirectArtifactIds {
368 directArtifactIds = append(directArtifactIds, artifactId(protoDirectArtifactIds))
369 }
370 for _, protoTransitiveDepSetIds := range protoDepSetOfFiles.TransitiveDepSetIds {
371 transitiveDepSetIds = append(transitiveDepSetIds, depsetId(protoTransitiveDepSetIds))
372 }
373 aqueryResult.DepSetOfFiles = append(aqueryResult.DepSetOfFiles,
374 depSetOfFiles{
375 Id: depsetId(protoDepSetOfFiles.Id),
376 DirectArtifactIds: directArtifactIds,
377 TransitiveDepSetIds: transitiveDepSetIds})
378
379 }
380
381 for _, protoPathFragments := range aqueryProto.PathFragments {
382 aqueryResult.PathFragments = append(aqueryResult.PathFragments,
383 pathFragment{
384 Id: pathFragmentId(protoPathFragments.Id),
385 Label: protoPathFragments.Label,
386 ParentId: pathFragmentId(protoPathFragments.ParentId)})
387
388 }
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400389 aqueryHandler, err := newAqueryHandler(aqueryResult)
390 if err != nil {
Chris Parsons1a7aca02022-04-25 22:35:15 -0400391 return nil, nil, err
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400392 }
Chris Parsons8d6e4332021-02-22 16:13:50 -0500393
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400394 var buildStatements []BuildStatement
Usta Shresthaef922252022-06-02 14:23:02 -0400395 if aqueryHandler.bazelToolsDependencySentinelNeeded {
396 buildStatements = append(buildStatements, BuildStatement{
397 Command: fmt.Sprintf("touch '%s'", bazelToolsDependencySentinel),
398 OutputPaths: []string{bazelToolsDependencySentinel},
399 Mnemonic: bazelToolsDependencySentinel,
400 })
401 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400402
Chris Parsons8d6e4332021-02-22 16:13:50 -0500403 for _, actionEntry := range aqueryResult.Actions {
404 if shouldSkipAction(actionEntry) {
405 continue
406 }
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400407
Chris Parsons1a7aca02022-04-25 22:35:15 -0400408 var buildStatement BuildStatement
Sasha Smundak1da064c2022-06-08 16:36:16 -0700409 if actionEntry.isSymlinkAction() {
Chris Parsons1a7aca02022-04-25 22:35:15 -0400410 buildStatement, err = aqueryHandler.symlinkActionBuildStatement(actionEntry)
Sasha Smundak1da064c2022-06-08 16:36:16 -0700411 } else if actionEntry.isTemplateExpandAction() && len(actionEntry.Arguments) < 1 {
Chris Parsons1a7aca02022-04-25 22:35:15 -0400412 buildStatement, err = aqueryHandler.templateExpandActionBuildStatement(actionEntry)
Sasha Smundak1da064c2022-06-08 16:36:16 -0700413 } else if actionEntry.isFileWriteAction() {
414 buildStatement, err = aqueryHandler.fileWriteActionBuildStatement(actionEntry)
Sasha Smundakc180dbd2022-07-03 14:55:58 -0700415 } else if actionEntry.isSymlinkTreeAction() {
416 buildStatement, err = aqueryHandler.symlinkTreeActionBuildStatement(actionEntry)
Liz Kammerc49e6822021-06-08 15:04:11 -0400417 } else if len(actionEntry.Arguments) < 1 {
Chris Parsons1a7aca02022-04-25 22:35:15 -0400418 return nil, nil, fmt.Errorf("received action with no command: [%s]", actionEntry.Mnemonic)
419 } else {
420 buildStatement, err = aqueryHandler.normalActionBuildStatement(actionEntry)
421 }
422
423 if err != nil {
424 return nil, nil, err
Chris Parsons8d6e4332021-02-22 16:13:50 -0500425 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500426 buildStatements = append(buildStatements, buildStatement)
427 }
428
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400429 depsetsByHash := map[string]AqueryDepset{}
Usta Shrestha6298cc52022-05-27 17:40:21 -0400430 var depsets []AqueryDepset
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400431 for _, aqueryDepset := range aqueryHandler.depsetIdToAqueryDepset {
432 if prevEntry, hasKey := depsetsByHash[aqueryDepset.ContentHash]; hasKey {
433 // Two depsets collide on hash. Ensure that their contents are identical.
434 if !reflect.DeepEqual(aqueryDepset, prevEntry) {
Usta Shrestha16ac1352022-06-22 11:01:55 -0400435 return nil, nil, fmt.Errorf("two different depsets have the same hash: %v, %v", prevEntry, aqueryDepset)
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400436 }
437 } else {
438 depsetsByHash[aqueryDepset.ContentHash] = aqueryDepset
439 depsets = append(depsets, aqueryDepset)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400440 }
Chris Parsons1a7aca02022-04-25 22:35:15 -0400441 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400442
443 // Build Statements and depsets must be sorted by their content hash to
444 // preserve determinism between builds (this will result in consistent ninja file
445 // output). Note they are not sorted by their original IDs nor their Bazel ordering,
446 // as Bazel gives nondeterministic ordering / identifiers in aquery responses.
447 sort.Slice(buildStatements, func(i, j int) bool {
448 // For build statements, compare output lists. In Bazel, each output file
449 // may only have one action which generates it, so this will provide
450 // a deterministic ordering.
451 outputs_i := buildStatements[i].OutputPaths
452 outputs_j := buildStatements[j].OutputPaths
453 if len(outputs_i) != len(outputs_j) {
454 return len(outputs_i) < len(outputs_j)
455 }
456 if len(outputs_i) == 0 {
457 // No outputs for these actions, so compare commands.
458 return buildStatements[i].Command < buildStatements[j].Command
459 }
460 // There may be multiple outputs, but the output ordering is deterministic.
461 return outputs_i[0] < outputs_j[0]
462 })
463 sort.Slice(depsets, func(i, j int) bool {
464 return depsets[i].ContentHash < depsets[j].ContentHash
465 })
Chris Parsons1a7aca02022-04-25 22:35:15 -0400466 return buildStatements, depsets, nil
467}
468
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400469// depsetContentHash computes and returns a SHA256 checksum of the contents of
470// the given depset. This content hash may serve as the depset's identifier.
471// Using a content hash for an identifier is superior for determinism. (For example,
472// using an integer identifier which depends on the order in which the depsets are
473// created would result in nondeterministic depset IDs.)
474func depsetContentHash(directPaths []string, transitiveDepsetHashes []string) string {
475 h := sha256.New()
476 // Use newline as delimiter, as paths cannot contain newline.
477 h.Write([]byte(strings.Join(directPaths, "\n")))
Usta Shrestha2ccdb422022-06-02 10:19:13 -0400478 h.Write([]byte(strings.Join(transitiveDepsetHashes, "")))
479 fullHash := base64.RawURLEncoding.EncodeToString(h.Sum(nil))
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400480 return fullHash
481}
482
Usta Shrestha6298cc52022-05-27 17:40:21 -0400483func (a *aqueryArtifactHandler) depsetContentHashes(inputDepsetIds []depsetId) ([]string, error) {
484 var hashes []string
Chris Parsons1a7aca02022-04-25 22:35:15 -0400485 for _, depsetId := range inputDepsetIds {
Usta Shresthac2372492022-05-27 10:45:00 -0400486 if aqueryDepset, exists := a.depsetIdToAqueryDepset[depsetId]; !exists {
Chris Parsons1a7aca02022-04-25 22:35:15 -0400487 return nil, fmt.Errorf("undefined input depsetId %d", depsetId)
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400488 } else {
489 hashes = append(hashes, aqueryDepset.ContentHash)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400490 }
491 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400492 return hashes, nil
Chris Parsons1a7aca02022-04-25 22:35:15 -0400493}
494
Usta Shresthac2372492022-05-27 10:45:00 -0400495func (a *aqueryArtifactHandler) normalActionBuildStatement(actionEntry action) (BuildStatement, error) {
Chris Parsons1a7aca02022-04-25 22:35:15 -0400496 command := strings.Join(proptools.ShellEscapeListIncludingSpaces(actionEntry.Arguments), " ")
Usta Shresthac2372492022-05-27 10:45:00 -0400497 inputDepsetHashes, err := a.depsetContentHashes(actionEntry.InputDepSetIds)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400498 if err != nil {
499 return BuildStatement{}, err
500 }
Usta Shresthac2372492022-05-27 10:45:00 -0400501 outputPaths, depfile, err := a.getOutputPaths(actionEntry)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400502 if err != nil {
503 return BuildStatement{}, err
504 }
505
506 buildStatement := BuildStatement{
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400507 Command: command,
508 Depfile: depfile,
509 OutputPaths: outputPaths,
510 InputDepsetHashes: inputDepsetHashes,
511 Env: actionEntry.EnvironmentVariables,
512 Mnemonic: actionEntry.Mnemonic,
Chris Parsons1a7aca02022-04-25 22:35:15 -0400513 }
514 return buildStatement, nil
515}
516
Usta Shresthac2372492022-05-27 10:45:00 -0400517func (a *aqueryArtifactHandler) templateExpandActionBuildStatement(actionEntry action) (BuildStatement, error) {
518 outputPaths, depfile, err := a.getOutputPaths(actionEntry)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400519 if err != nil {
520 return BuildStatement{}, err
521 }
522 if len(outputPaths) != 1 {
523 return BuildStatement{}, fmt.Errorf("Expect 1 output to template expand action, got: output %q", outputPaths)
524 }
525 expandedTemplateContent := expandTemplateContent(actionEntry)
526 // The expandedTemplateContent is escaped for being used in double quotes and shell unescape,
527 // and the new line characters (\n) are also changed to \\n which avoids some Ninja escape on \n, which might
528 // change \n to space and mess up the format of Python programs.
529 // sed is used to convert \\n back to \n before saving to output file.
530 // See go/python-binary-host-mixed-build for more details.
531 command := fmt.Sprintf(`/bin/bash -c 'echo "%[1]s" | sed "s/\\\\n/\\n/g" > %[2]s && chmod a+x %[2]s'`,
532 escapeCommandlineArgument(expandedTemplateContent), outputPaths[0])
Usta Shresthac2372492022-05-27 10:45:00 -0400533 inputDepsetHashes, err := a.depsetContentHashes(actionEntry.InputDepSetIds)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400534 if err != nil {
535 return BuildStatement{}, err
536 }
537
538 buildStatement := BuildStatement{
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400539 Command: command,
540 Depfile: depfile,
541 OutputPaths: outputPaths,
542 InputDepsetHashes: inputDepsetHashes,
543 Env: actionEntry.EnvironmentVariables,
544 Mnemonic: actionEntry.Mnemonic,
Chris Parsons1a7aca02022-04-25 22:35:15 -0400545 }
546 return buildStatement, nil
547}
548
Sasha Smundak1da064c2022-06-08 16:36:16 -0700549func (a *aqueryArtifactHandler) fileWriteActionBuildStatement(actionEntry action) (BuildStatement, error) {
550 outputPaths, _, err := a.getOutputPaths(actionEntry)
551 var depsetHashes []string
552 if err == nil {
553 depsetHashes, err = a.depsetContentHashes(actionEntry.InputDepSetIds)
554 }
555 if err != nil {
556 return BuildStatement{}, err
557 }
558 return BuildStatement{
559 Depfile: nil,
560 OutputPaths: outputPaths,
561 Env: actionEntry.EnvironmentVariables,
562 Mnemonic: actionEntry.Mnemonic,
563 InputDepsetHashes: depsetHashes,
564 FileContents: actionEntry.FileContents,
565 }, nil
566}
567
Sasha Smundakc180dbd2022-07-03 14:55:58 -0700568func (a *aqueryArtifactHandler) symlinkTreeActionBuildStatement(actionEntry action) (BuildStatement, error) {
569 outputPaths, _, err := a.getOutputPaths(actionEntry)
570 if err != nil {
571 return BuildStatement{}, err
572 }
573 inputPaths, err := a.getInputPaths(actionEntry.InputDepSetIds)
574 if err != nil {
575 return BuildStatement{}, err
576 }
577 if len(inputPaths) != 1 || len(outputPaths) != 1 {
578 return BuildStatement{}, fmt.Errorf("Expect 1 input and 1 output to symlink action, got: input %q, output %q", inputPaths, outputPaths)
579 }
580 // The actual command is generated in bazelSingleton.GenerateBuildActions
581 return BuildStatement{
582 Depfile: nil,
583 OutputPaths: outputPaths,
584 Env: actionEntry.EnvironmentVariables,
585 Mnemonic: actionEntry.Mnemonic,
586 InputPaths: inputPaths,
587 }, nil
588}
589
Usta Shresthac2372492022-05-27 10:45:00 -0400590func (a *aqueryArtifactHandler) symlinkActionBuildStatement(actionEntry action) (BuildStatement, error) {
591 outputPaths, depfile, err := a.getOutputPaths(actionEntry)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400592 if err != nil {
593 return BuildStatement{}, err
594 }
595
Usta Shresthac2372492022-05-27 10:45:00 -0400596 inputPaths, err := a.getInputPaths(actionEntry.InputDepSetIds)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400597 if err != nil {
598 return BuildStatement{}, err
599 }
600 if len(inputPaths) != 1 || len(outputPaths) != 1 {
601 return BuildStatement{}, fmt.Errorf("Expect 1 input and 1 output to symlink action, got: input %q, output %q", inputPaths, outputPaths)
602 }
603 out := outputPaths[0]
604 outDir := proptools.ShellEscapeIncludingSpaces(filepath.Dir(out))
605 out = proptools.ShellEscapeIncludingSpaces(out)
606 in := filepath.Join("$PWD", proptools.ShellEscapeIncludingSpaces(inputPaths[0]))
607 // Use absolute paths, because some soong actions don't play well with relative paths (for example, `cp -d`).
608 command := fmt.Sprintf("mkdir -p %[1]s && rm -f %[2]s && ln -sf %[3]s %[2]s", outDir, out, in)
609 symlinkPaths := outputPaths[:]
610
611 buildStatement := BuildStatement{
612 Command: command,
613 Depfile: depfile,
614 OutputPaths: outputPaths,
615 InputPaths: inputPaths,
616 Env: actionEntry.EnvironmentVariables,
617 Mnemonic: actionEntry.Mnemonic,
618 SymlinkPaths: symlinkPaths,
619 }
620 return buildStatement, nil
621}
622
Usta Shresthac2372492022-05-27 10:45:00 -0400623func (a *aqueryArtifactHandler) getOutputPaths(actionEntry action) (outputPaths []string, depfile *string, err error) {
Chris Parsons1a7aca02022-04-25 22:35:15 -0400624 for _, outputId := range actionEntry.OutputIds {
Usta Shresthac2372492022-05-27 10:45:00 -0400625 outputPath, exists := a.artifactIdToPath[outputId]
Chris Parsons1a7aca02022-04-25 22:35:15 -0400626 if !exists {
627 err = fmt.Errorf("undefined outputId %d", outputId)
628 return
629 }
630 ext := filepath.Ext(outputPath)
631 if ext == ".d" {
632 if depfile != nil {
633 err = fmt.Errorf("found multiple potential depfiles %q, %q", *depfile, outputPath)
634 return
635 } else {
636 depfile = &outputPath
637 }
638 } else {
639 outputPaths = append(outputPaths, outputPath)
640 }
641 }
642 return
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500643}
Chris Parsonsaffbb602020-12-23 12:02:11 -0500644
Wei Li455ba832021-11-04 22:58:12 +0000645// expandTemplateContent substitutes the tokens in a template.
646func expandTemplateContent(actionEntry action) string {
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700647 var replacerString []string
Wei Li455ba832021-11-04 22:58:12 +0000648 for _, pair := range actionEntry.Substitutions {
649 value := pair.Value
Usta Shrestha6298cc52022-05-27 17:40:21 -0400650 if val, ok := templateActionOverriddenTokens[pair.Key]; ok {
Wei Li455ba832021-11-04 22:58:12 +0000651 value = val
652 }
653 replacerString = append(replacerString, pair.Key, value)
654 }
655 replacer := strings.NewReplacer(replacerString...)
656 return replacer.Replace(actionEntry.TemplateContent)
657}
658
659func escapeCommandlineArgument(str string) string {
660 // \->\\, $->\$, `->\`, "->\", \n->\\n, '->'"'"'
661 replacer := strings.NewReplacer(
662 `\`, `\\`,
663 `$`, `\$`,
664 "`", "\\`",
665 `"`, `\"`,
666 "\n", "\\n",
667 `'`, `'"'"'`,
668 )
669 return replacer.Replace(str)
670}
671
Sasha Smundak1da064c2022-06-08 16:36:16 -0700672func (a action) isSymlinkAction() bool {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000673 return a.Mnemonic == "Symlink" || a.Mnemonic == "SolibSymlink" || a.Mnemonic == "ExecutableSymlink"
Liz Kammerc49e6822021-06-08 15:04:11 -0400674}
675
Sasha Smundak1da064c2022-06-08 16:36:16 -0700676func (a action) isTemplateExpandAction() bool {
Wei Li455ba832021-11-04 22:58:12 +0000677 return a.Mnemonic == "TemplateExpand"
678}
679
Sasha Smundak1da064c2022-06-08 16:36:16 -0700680func (a action) isFileWriteAction() bool {
681 return a.Mnemonic == "FileWrite" || a.Mnemonic == "SourceSymlinkManifest"
682}
683
Sasha Smundakc180dbd2022-07-03 14:55:58 -0700684func (a action) isSymlinkTreeAction() bool {
685 return a.Mnemonic == "SymlinkTree"
686}
687
Chris Parsons8d6e4332021-02-22 16:13:50 -0500688func shouldSkipAction(a action) bool {
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400689 // Middleman actions are not handled like other actions; they are handled separately as a
690 // preparatory step so that their inputs may be relayed to actions depending on middleman
691 // artifacts.
Chris Parsons8d6e4332021-02-22 16:13:50 -0500692 if a.Mnemonic == "Middleman" {
693 return true
694 }
Sasha Smundakc180dbd2022-07-03 14:55:58 -0700695 // PythonZipper is bogus action returned by aquery, ignore it (b/236198693)
696 if a.Mnemonic == "PythonZipper" {
697 return true
698 }
Chris Parsons8d6e4332021-02-22 16:13:50 -0500699 // Skip "Fail" actions, which are placeholder actions designed to always fail.
700 if a.Mnemonic == "Fail" {
701 return true
702 }
Yu Liu8d82ac52022-05-17 15:13:28 -0700703 if a.Mnemonic == "BaselineCoverage" {
704 return true
705 }
Chris Parsons8d6e4332021-02-22 16:13:50 -0500706 return false
707}
708
Usta Shrestha6298cc52022-05-27 17:40:21 -0400709func expandPathFragment(id pathFragmentId, pathFragmentsMap map[pathFragmentId]pathFragment) (string, error) {
710 var labels []string
Chris Parsonsaffbb602020-12-23 12:02:11 -0500711 currId := id
712 // Only positive IDs are valid for path fragments. An ID of zero indicates a terminal node.
713 for currId > 0 {
714 currFragment, ok := pathFragmentsMap[currId]
715 if !ok {
Chris Parsons4f069892021-01-15 12:22:41 -0500716 return "", fmt.Errorf("undefined path fragment id %d", currId)
Chris Parsonsaffbb602020-12-23 12:02:11 -0500717 }
718 labels = append([]string{currFragment.Label}, labels...)
Liz Kammerc49e6822021-06-08 15:04:11 -0400719 if currId == currFragment.ParentId {
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700720 return "", fmt.Errorf("fragment cannot refer to itself as parent %#v", currFragment)
Liz Kammerc49e6822021-06-08 15:04:11 -0400721 }
Chris Parsonsaffbb602020-12-23 12:02:11 -0500722 currId = currFragment.ParentId
723 }
724 return filepath.Join(labels...), nil
725}