blob: c87a945bb4200d9e715d21a56e125063b1d37721 [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
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040032type CqueryRequestType int
33
34const (
35 getAllFiles CqueryRequestType = iota
36)
37
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040038// Map key to describe bazel cquery requests.
39type cqueryKey struct {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040040 label string
41 requestType CqueryRequestType
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040042}
43
44type BazelContext interface {
45 // The below methods involve queuing cquery requests to be later invoked
46 // by bazel. If any of these methods return (_, false), then the request
47 // has been queued to be run later.
48
49 // Returns result files built by building the given bazel target label.
50 GetAllFiles(label string) ([]string, bool)
51
52 // TODO(cparsons): Other cquery-related methods should be added here.
53 // ** End cquery methods
54
55 // Issues commands to Bazel to receive results for all cquery requests
56 // queued in the BazelContext.
57 InvokeBazel() error
58
59 // Returns true if bazel is enabled for the given configuration.
60 BazelEnabled() bool
61}
62
63// A context object which tracks queued requests that need to be made to Bazel,
64// and their results after the requests have been made.
65type bazelContext struct {
66 homeDir string
67 bazelPath string
68 outputBase string
69 workspaceDir string
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040070 buildDir string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040071
72 requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
73 requestMutex sync.Mutex // requests can be written in parallel
74
75 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
76}
77
78var _ BazelContext = &bazelContext{}
79
80// A bazel context to use when Bazel is disabled.
81type noopBazelContext struct{}
82
83var _ BazelContext = noopBazelContext{}
84
85// A bazel context to use for tests.
86type MockBazelContext struct {
87 AllFiles map[string][]string
88}
89
90func (m MockBazelContext) GetAllFiles(label string) ([]string, bool) {
91 result, ok := m.AllFiles[label]
92 return result, ok
93}
94
95func (m MockBazelContext) InvokeBazel() error {
96 panic("unimplemented")
97}
98
99func (m MockBazelContext) BazelEnabled() bool {
100 return true
101}
102
103var _ BazelContext = MockBazelContext{}
104
105func (bazelCtx *bazelContext) GetAllFiles(label string) ([]string, bool) {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400106 result, ok := bazelCtx.cquery(label, getAllFiles)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400107 if ok {
108 bazelOutput := strings.TrimSpace(result)
109 return strings.Split(bazelOutput, ", "), true
110 } else {
111 return nil, false
112 }
113}
114
115func (n noopBazelContext) GetAllFiles(label string) ([]string, bool) {
116 panic("unimplemented")
117}
118
119func (n noopBazelContext) InvokeBazel() error {
120 panic("unimplemented")
121}
122
123func (n noopBazelContext) BazelEnabled() bool {
124 return false
125}
126
127func NewBazelContext(c *config) (BazelContext, error) {
Chris Parsons8b77a002020-10-27 18:59:25 -0400128 // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds"
129 // are production ready.
130 if c.Getenv("USE_BAZEL_ANALYSIS") != "1" {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400131 return noopBazelContext{}, nil
132 }
133
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400134 bazelCtx := bazelContext{buildDir: c.buildDir, requests: make(map[cqueryKey]bool)}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400135 missingEnvVars := []string{}
136 if len(c.Getenv("BAZEL_HOME")) > 1 {
137 bazelCtx.homeDir = c.Getenv("BAZEL_HOME")
138 } else {
139 missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
140 }
141 if len(c.Getenv("BAZEL_PATH")) > 1 {
142 bazelCtx.bazelPath = c.Getenv("BAZEL_PATH")
143 } else {
144 missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
145 }
146 if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
147 bazelCtx.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
148 } else {
149 missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
150 }
151 if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
152 bazelCtx.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
153 } else {
154 missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
155 }
156 if len(missingEnvVars) > 0 {
157 return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
158 } else {
159 return &bazelCtx, nil
160 }
161}
162
163func (context *bazelContext) BazelEnabled() bool {
164 return true
165}
166
167// Adds a cquery request to the Bazel request queue, to be later invoked, or
168// returns the result of the given request if the request was already made.
169// If the given request was already made (and the results are available), then
170// returns (result, true). If the request is queued but no results are available,
171// then returns ("", false).
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400172func (context *bazelContext) cquery(label string, requestType CqueryRequestType) (string, bool) {
173 key := cqueryKey{label, requestType}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400174 if result, ok := context.results[key]; ok {
175 return result, true
176 } else {
177 context.requestMutex.Lock()
178 defer context.requestMutex.Unlock()
179 context.requests[key] = true
180 return "", false
181 }
182}
183
184func pwdPrefix() string {
185 // Darwin doesn't have /proc
186 if runtime.GOOS != "darwin" {
187 return "PWD=/proc/self/cwd"
188 }
189 return ""
190}
191
192func (context *bazelContext) issueBazelCommand(command string, labels []string,
193 extraFlags ...string) (string, error) {
194
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400195 cmdFlags := []string{"--bazelrc=build/bazel/common.bazelrc",
196 "--output_base=" + context.outputBase, command}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400197 cmdFlags = append(cmdFlags, labels...)
198 cmdFlags = append(cmdFlags, extraFlags...)
199
200 bazelCmd := exec.Command(context.bazelPath, cmdFlags...)
201 bazelCmd.Dir = context.workspaceDir
202 bazelCmd.Env = append(os.Environ(), "HOME="+context.homeDir, pwdPrefix())
203
Colin Crossff0278b2020-10-09 19:24:15 -0700204 stderr := &bytes.Buffer{}
205 bazelCmd.Stderr = stderr
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400206
207 if output, err := bazelCmd.Output(); err != nil {
208 return "", fmt.Errorf("bazel command failed. command: [%s], error [%s]", bazelCmd, stderr)
209 } else {
210 return string(output), nil
211 }
212}
213
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400214func (context *bazelContext) mainBzlFileContents() []byte {
215 contents := `
216# This file is generated by soong_build. Do not edit.
217def _mixed_build_root_impl(ctx):
218 return [DefaultInfo(files = depset(ctx.files.deps))]
219
220mixed_build_root = rule(
221 implementation = _mixed_build_root_impl,
222 attrs = {"deps" : attr.label_list()},
223)
224`
225 return []byte(contents)
226}
227
228func (context *bazelContext) mainBuildFileContents() []byte {
229 formatString := `
230# This file is generated by soong_build. Do not edit.
231load(":main.bzl", "mixed_build_root")
232
233mixed_build_root(name = "buildroot",
234 deps = [%s],
235)
236`
237 var buildRootDeps []string = nil
238 for val, _ := range context.requests {
239 buildRootDeps = append(buildRootDeps, fmt.Sprintf("\"%s\"", val.label))
240 }
241 buildRootDepsString := strings.Join(buildRootDeps, ",\n ")
242
243 return []byte(fmt.Sprintf(formatString, buildRootDepsString))
244}
245
246func (context *bazelContext) cqueryStarlarkFileContents() []byte {
247 formatString := `
248# This file is generated by soong_build. Do not edit.
249getAllFilesLabels = {
250 %s
251}
252
253def format(target):
254 if str(target.label) in getAllFilesLabels:
255 return str(target.label) + ">>" + ', '.join([f.path for f in target.files.to_list()])
256 else:
257 # This target was not requested via cquery, and thus must be a dependency
258 # of a requested target.
259 return ""
260`
261 var buildRootDeps []string = nil
262 // TODO(cparsons): Sort by request type instead of assuming all requests
263 // are of GetAllFiles type.
264 for val, _ := range context.requests {
265 buildRootDeps = append(buildRootDeps, fmt.Sprintf("\"%s\" : True", val.label))
266 }
267 buildRootDepsString := strings.Join(buildRootDeps, ",\n ")
268
269 return []byte(fmt.Sprintf(formatString, buildRootDepsString))
270}
271
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400272// Issues commands to Bazel to receive results for all cquery requests
273// queued in the BazelContext.
274func (context *bazelContext) InvokeBazel() error {
275 context.results = make(map[cqueryKey]string)
276
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400277 var cqueryOutput string
278 var err error
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400279 err = ioutil.WriteFile(
280 absolutePath(filepath.Join(context.buildDir, "main.bzl")),
281 context.mainBzlFileContents(), 0666)
282 if err != nil {
283 return err
284 }
285 err = ioutil.WriteFile(
286 absolutePath(filepath.Join(context.buildDir, "BUILD.bazel")),
287 context.mainBuildFileContents(), 0666)
288 if err != nil {
289 return err
290 }
291 cquery_file_relpath := filepath.Join(context.buildDir, "buildroot.cquery")
292 err = ioutil.WriteFile(
293 absolutePath(cquery_file_relpath),
294 context.cqueryStarlarkFileContents(), 0666)
295 if err != nil {
296 return err
297 }
298 buildroot_label := fmt.Sprintf("//%s:buildroot", context.buildDir)
299 cqueryOutput, err = context.issueBazelCommand("cquery",
300 []string{fmt.Sprintf("deps(%s)", buildroot_label)},
301 "--output=starlark",
302 "--starlark:file="+cquery_file_relpath)
303
304 if err != nil {
305 return err
306 }
307
308 cqueryResults := map[string]string{}
309 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
310 if strings.Contains(outputLine, ">>") {
311 splitLine := strings.SplitN(outputLine, ">>", 2)
312 cqueryResults[splitLine[0]] = splitLine[1]
313 }
314 }
315
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400316 for val, _ := range context.requests {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400317 if cqueryResult, ok := cqueryResults[val.label]; ok {
318 context.results[val] = string(cqueryResult)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400319 } else {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400320 return fmt.Errorf("missing result for bazel target %s", val.label)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400321 }
322 }
323
324 // Issue a build command.
325 // TODO(cparsons): Invoking bazel execution during soong_build should be avoided;
326 // bazel actions should either be added to the Ninja file and executed later,
327 // or bazel should handle execution.
328 // TODO(cparsons): Use --target_pattern_file to avoid command line limits.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400329 _, err = context.issueBazelCommand("build", []string{buildroot_label})
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400330
331 if err != nil {
332 return err
333 }
334
335 // Clear requests.
336 context.requests = map[cqueryKey]bool{}
337 return nil
338}
Chris Parsonsa798d962020-10-12 23:44:08 -0400339
340// Singleton used for registering BUILD file ninja dependencies (needed
341// for correctness of builds which use Bazel.
342func BazelSingleton() Singleton {
343 return &bazelSingleton{}
344}
345
346type bazelSingleton struct{}
347
348func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
349 if ctx.Config().BazelContext.BazelEnabled() {
350 bazelBuildList := absolutePath(filepath.Join(
351 filepath.Dir(bootstrap.ModuleListFile), "bazel.list"))
352 ctx.AddNinjaFileDeps(bazelBuildList)
353
354 data, err := ioutil.ReadFile(bazelBuildList)
355 if err != nil {
356 ctx.Errorf(err.Error())
357 }
358 files := strings.Split(strings.TrimSpace(string(data)), "\n")
359 for _, file := range files {
360 ctx.AddNinjaFileDeps(file)
361 }
362 }
363}