blob: 221aabc28f058a49c1fde04dd38818a8dbea59c1 [file] [log] [blame]
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001// 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 android
16
17import (
18 "bytes"
19 "errors"
20 "fmt"
Chris Parsonsa798d962020-10-12 23:44:08 -040021 "io/ioutil"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040022 "os"
23 "os/exec"
Chris Parsonsa798d962020-10-12 23:44:08 -040024 "path/filepath"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040025 "runtime"
26 "strings"
27 "sync"
Chris Parsonsa798d962020-10-12 23:44:08 -040028
29 "github.com/google/blueprint/bootstrap"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040030)
31
32// Map key to describe bazel cquery requests.
33type cqueryKey struct {
34 label string
35 starlarkExpr string
36}
37
38type BazelContext interface {
39 // The below methods involve queuing cquery requests to be later invoked
40 // by bazel. If any of these methods return (_, false), then the request
41 // has been queued to be run later.
42
43 // Returns result files built by building the given bazel target label.
44 GetAllFiles(label string) ([]string, bool)
45
46 // TODO(cparsons): Other cquery-related methods should be added here.
47 // ** End cquery methods
48
49 // Issues commands to Bazel to receive results for all cquery requests
50 // queued in the BazelContext.
51 InvokeBazel() error
52
53 // Returns true if bazel is enabled for the given configuration.
54 BazelEnabled() bool
55}
56
57// A context object which tracks queued requests that need to be made to Bazel,
58// and their results after the requests have been made.
59type bazelContext struct {
60 homeDir string
61 bazelPath string
62 outputBase string
63 workspaceDir string
64
65 requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
66 requestMutex sync.Mutex // requests can be written in parallel
67
68 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
69}
70
71var _ BazelContext = &bazelContext{}
72
73// A bazel context to use when Bazel is disabled.
74type noopBazelContext struct{}
75
76var _ BazelContext = noopBazelContext{}
77
78// A bazel context to use for tests.
79type MockBazelContext struct {
80 AllFiles map[string][]string
81}
82
83func (m MockBazelContext) GetAllFiles(label string) ([]string, bool) {
84 result, ok := m.AllFiles[label]
85 return result, ok
86}
87
88func (m MockBazelContext) InvokeBazel() error {
89 panic("unimplemented")
90}
91
92func (m MockBazelContext) BazelEnabled() bool {
93 return true
94}
95
96var _ BazelContext = MockBazelContext{}
97
98func (bazelCtx *bazelContext) GetAllFiles(label string) ([]string, bool) {
99 starlarkExpr := "', '.join([f.path for f in target.files.to_list()])"
100 result, ok := bazelCtx.cquery(label, starlarkExpr)
101 if ok {
102 bazelOutput := strings.TrimSpace(result)
103 return strings.Split(bazelOutput, ", "), true
104 } else {
105 return nil, false
106 }
107}
108
109func (n noopBazelContext) GetAllFiles(label string) ([]string, bool) {
110 panic("unimplemented")
111}
112
113func (n noopBazelContext) InvokeBazel() error {
114 panic("unimplemented")
115}
116
117func (n noopBazelContext) BazelEnabled() bool {
118 return false
119}
120
121func NewBazelContext(c *config) (BazelContext, error) {
Chris Parsons8b77a002020-10-27 18:59:25 -0400122 // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds"
123 // are production ready.
124 if c.Getenv("USE_BAZEL_ANALYSIS") != "1" {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400125 return noopBazelContext{}, nil
126 }
127
128 bazelCtx := bazelContext{requests: make(map[cqueryKey]bool)}
129 missingEnvVars := []string{}
130 if len(c.Getenv("BAZEL_HOME")) > 1 {
131 bazelCtx.homeDir = c.Getenv("BAZEL_HOME")
132 } else {
133 missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
134 }
135 if len(c.Getenv("BAZEL_PATH")) > 1 {
136 bazelCtx.bazelPath = c.Getenv("BAZEL_PATH")
137 } else {
138 missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
139 }
140 if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
141 bazelCtx.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
142 } else {
143 missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
144 }
145 if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
146 bazelCtx.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
147 } else {
148 missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
149 }
150 if len(missingEnvVars) > 0 {
151 return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
152 } else {
153 return &bazelCtx, nil
154 }
155}
156
157func (context *bazelContext) BazelEnabled() bool {
158 return true
159}
160
161// Adds a cquery request to the Bazel request queue, to be later invoked, or
162// returns the result of the given request if the request was already made.
163// If the given request was already made (and the results are available), then
164// returns (result, true). If the request is queued but no results are available,
165// then returns ("", false).
166func (context *bazelContext) cquery(label string, starlarkExpr string) (string, bool) {
167 key := cqueryKey{label, starlarkExpr}
168 if result, ok := context.results[key]; ok {
169 return result, true
170 } else {
171 context.requestMutex.Lock()
172 defer context.requestMutex.Unlock()
173 context.requests[key] = true
174 return "", false
175 }
176}
177
178func pwdPrefix() string {
179 // Darwin doesn't have /proc
180 if runtime.GOOS != "darwin" {
181 return "PWD=/proc/self/cwd"
182 }
183 return ""
184}
185
186func (context *bazelContext) issueBazelCommand(command string, labels []string,
187 extraFlags ...string) (string, error) {
188
189 cmdFlags := []string{"--output_base=" + context.outputBase, command}
190 cmdFlags = append(cmdFlags, labels...)
191 cmdFlags = append(cmdFlags, extraFlags...)
192
193 bazelCmd := exec.Command(context.bazelPath, cmdFlags...)
194 bazelCmd.Dir = context.workspaceDir
195 bazelCmd.Env = append(os.Environ(), "HOME="+context.homeDir, pwdPrefix())
196
Colin Crossff0278b2020-10-09 19:24:15 -0700197 stderr := &bytes.Buffer{}
198 bazelCmd.Stderr = stderr
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400199
200 if output, err := bazelCmd.Output(); err != nil {
201 return "", fmt.Errorf("bazel command failed. command: [%s], error [%s]", bazelCmd, stderr)
202 } else {
203 return string(output), nil
204 }
205}
206
207// Issues commands to Bazel to receive results for all cquery requests
208// queued in the BazelContext.
209func (context *bazelContext) InvokeBazel() error {
210 context.results = make(map[cqueryKey]string)
211
212 var labels []string
213 var cqueryOutput string
214 var err error
215 for val, _ := range context.requests {
216 labels = append(labels, val.label)
217
218 // TODO(cparsons): Combine requests into a batch cquery request.
219 // TODO(cparsons): Use --query_file to avoid command line limits.
220 cqueryOutput, err = context.issueBazelCommand("cquery", []string{val.label},
221 "--output=starlark",
222 "--starlark:expr="+val.starlarkExpr)
223
224 if err != nil {
225 return err
226 } else {
227 context.results[val] = string(cqueryOutput)
228 }
229 }
230
231 // Issue a build command.
232 // TODO(cparsons): Invoking bazel execution during soong_build should be avoided;
233 // bazel actions should either be added to the Ninja file and executed later,
234 // or bazel should handle execution.
235 // TODO(cparsons): Use --target_pattern_file to avoid command line limits.
236 _, err = context.issueBazelCommand("build", labels)
237
238 if err != nil {
239 return err
240 }
241
242 // Clear requests.
243 context.requests = map[cqueryKey]bool{}
244 return nil
245}
Chris Parsonsa798d962020-10-12 23:44:08 -0400246
247// Singleton used for registering BUILD file ninja dependencies (needed
248// for correctness of builds which use Bazel.
249func BazelSingleton() Singleton {
250 return &bazelSingleton{}
251}
252
253type bazelSingleton struct{}
254
255func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
256 if ctx.Config().BazelContext.BazelEnabled() {
257 bazelBuildList := absolutePath(filepath.Join(
258 filepath.Dir(bootstrap.ModuleListFile), "bazel.list"))
259 ctx.AddNinjaFileDeps(bazelBuildList)
260
261 data, err := ioutil.ReadFile(bazelBuildList)
262 if err != nil {
263 ctx.Errorf(err.Error())
264 }
265 files := strings.Split(strings.TrimSpace(string(data)), "\n")
266 for _, file := range files {
267 ctx.AddNinjaFileDeps(file)
268 }
269 }
270}