blob: 69d4fde14e9f9b3a0732d076c0530992a4771b73 [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 (
18 "encoding/json"
19 "strings"
20
21 "github.com/google/blueprint/proptools"
22)
23
24// artifact contains relevant portions of Bazel's aquery proto, Artifact.
25// Represents a single artifact, whether it's a source file or a derived output file.
26type artifact struct {
27 Id string
28 ExecPath string
29}
30
31// KeyValuePair represents Bazel's aquery proto, KeyValuePair.
32type KeyValuePair struct {
33 Key string
34 Value string
35}
36
37// depSetOfFiles contains relevant portions of Bazel's aquery proto, DepSetOfFiles.
38// Represents a data structure containing one or more files. Depsets in Bazel are an efficient
39// data structure for storing large numbers of file paths.
40type depSetOfFiles struct {
41 Id string
42 // TODO(cparsons): Handle non-flat depsets.
43 DirectArtifactIds []string
44}
45
46// action contains relevant portions of Bazel's aquery proto, Action.
47// Represents a single command line invocation in the Bazel build graph.
48type action struct {
49 Arguments []string
50 EnvironmentVariables []KeyValuePair
51 InputDepSetIds []string
52 Mnemonic string
53 OutputIds []string
54}
55
56// actionGraphContainer contains relevant portions of Bazel's aquery proto, ActionGraphContainer.
57// An aquery response from Bazel contains a single ActionGraphContainer proto.
58type actionGraphContainer struct {
59 Artifacts []artifact
60 Actions []action
61 DepSetOfFiles []depSetOfFiles
62}
63
64// BuildStatement contains information to register a build statement corresponding (one to one)
65// with a Bazel action from Bazel's action graph.
66type BuildStatement struct {
67 Command string
68 OutputPaths []string
69 InputPaths []string
70 Env []KeyValuePair
71 Mnemonic string
72}
73
74// AqueryBuildStatements returns an array of BuildStatements which should be registered (and output
75// to a ninja file) to correspond one-to-one with the given action graph json proto (from a bazel
76// aquery invocation).
77func AqueryBuildStatements(aqueryJsonProto []byte) []BuildStatement {
78 buildStatements := []BuildStatement{}
79
80 var aqueryResult actionGraphContainer
81 json.Unmarshal(aqueryJsonProto, &aqueryResult)
82
83 artifactIdToPath := map[string]string{}
84 for _, artifact := range aqueryResult.Artifacts {
85 artifactIdToPath[artifact.Id] = artifact.ExecPath
86 }
87 depsetIdToArtifactIds := map[string][]string{}
88 for _, depset := range aqueryResult.DepSetOfFiles {
89 depsetIdToArtifactIds[depset.Id] = depset.DirectArtifactIds
90 }
91
92 for _, actionEntry := range aqueryResult.Actions {
93 outputPaths := []string{}
94 for _, outputId := range actionEntry.OutputIds {
95 // TODO(cparsons): Validate the id is present.
96 outputPaths = append(outputPaths, artifactIdToPath[outputId])
97 }
98 inputPaths := []string{}
99 for _, inputDepSetId := range actionEntry.InputDepSetIds {
100 // TODO(cparsons): Validate the id is present.
101 for _, inputId := range depsetIdToArtifactIds[inputDepSetId] {
102 // TODO(cparsons): Validate the id is present.
103 inputPaths = append(inputPaths, artifactIdToPath[inputId])
104 }
105 }
106 buildStatement := BuildStatement{
107 Command: strings.Join(proptools.ShellEscapeList(actionEntry.Arguments), " "),
108 OutputPaths: outputPaths,
109 InputPaths: inputPaths,
110 Env: actionEntry.EnvironmentVariables,
111 Mnemonic: actionEntry.Mnemonic}
112 buildStatements = append(buildStatements, buildStatement)
113 }
114
115 return buildStatements
116}