blob: 7d8d12f19af50116cec77c4717797768a13c8c62 [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
Jingwen Chen7c6089a2020-11-02 02:56:20 -0500195 cmdFlags := []string{"--output_base=" + context.outputBase, command}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400196 cmdFlags = append(cmdFlags, labels...)
Chris Parsons8ccdb632020-11-17 15:41:01 -0500197 cmdFlags = append(cmdFlags, "--package_path=%workspace%/"+context.intermediatesDir())
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400198 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 Parsons8ccdb632020-11-17 15:41:01 -0500214// Returns the string contents of a workspace file that should be output
215// adjacent to the main bzl file and build file.
216// This workspace file allows, via local_repository rule, sourcetree-level
217// BUILD targets to be referenced via @sourceroot.
218func (context *bazelContext) workspaceFileContents() []byte {
219 formatString := `
220# This file is generated by soong_build. Do not edit.
221local_repository(
222 name = "sourceroot",
223 path = "%s",
224)
225`
226 return []byte(fmt.Sprintf(formatString, context.workspaceDir))
227}
228
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400229func (context *bazelContext) mainBzlFileContents() []byte {
230 contents := `
231# This file is generated by soong_build. Do not edit.
232def _mixed_build_root_impl(ctx):
233 return [DefaultInfo(files = depset(ctx.files.deps))]
234
235mixed_build_root = rule(
236 implementation = _mixed_build_root_impl,
237 attrs = {"deps" : attr.label_list()},
238)
239`
240 return []byte(contents)
241}
242
Chris Parsons8ccdb632020-11-17 15:41:01 -0500243// Returns a "canonicalized" corresponding to the given sourcetree-level label.
244// This abstraction is required because a sourcetree label such as //foo/bar:baz
245// must be referenced via the local repository prefix, such as
246// @sourceroot//foo/bar:baz.
247func canonicalizeLabel(label string) string {
248 if strings.HasPrefix(label, "//") {
249 return "@sourceroot" + label
250 } else {
251 return "@sourceroot//" + label
252 }
253}
254
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400255func (context *bazelContext) mainBuildFileContents() []byte {
256 formatString := `
257# This file is generated by soong_build. Do not edit.
258load(":main.bzl", "mixed_build_root")
259
260mixed_build_root(name = "buildroot",
261 deps = [%s],
262)
263`
264 var buildRootDeps []string = nil
265 for val, _ := range context.requests {
Chris Parsons8ccdb632020-11-17 15:41:01 -0500266 buildRootDeps = append(buildRootDeps, fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label)))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400267 }
268 buildRootDepsString := strings.Join(buildRootDeps, ",\n ")
269
270 return []byte(fmt.Sprintf(formatString, buildRootDepsString))
271}
272
273func (context *bazelContext) cqueryStarlarkFileContents() []byte {
274 formatString := `
275# This file is generated by soong_build. Do not edit.
276getAllFilesLabels = {
277 %s
278}
279
280def format(target):
281 if str(target.label) in getAllFilesLabels:
282 return str(target.label) + ">>" + ', '.join([f.path for f in target.files.to_list()])
283 else:
284 # This target was not requested via cquery, and thus must be a dependency
285 # of a requested target.
286 return ""
287`
288 var buildRootDeps []string = nil
289 // TODO(cparsons): Sort by request type instead of assuming all requests
290 // are of GetAllFiles type.
291 for val, _ := range context.requests {
Chris Parsons8ccdb632020-11-17 15:41:01 -0500292 buildRootDeps = append(buildRootDeps, fmt.Sprintf("\"%s\" : True", canonicalizeLabel(val.label)))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400293 }
294 buildRootDepsString := strings.Join(buildRootDeps, ",\n ")
295
296 return []byte(fmt.Sprintf(formatString, buildRootDepsString))
297}
298
Chris Parsons8ccdb632020-11-17 15:41:01 -0500299// Returns a workspace-relative path containing build-related metadata required
300// for interfacing with Bazel. Example: out/soong/bazel.
301func (context *bazelContext) intermediatesDir() string {
302 return filepath.Join(context.buildDir, "bazel")
303}
304
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400305// Issues commands to Bazel to receive results for all cquery requests
306// queued in the BazelContext.
307func (context *bazelContext) InvokeBazel() error {
308 context.results = make(map[cqueryKey]string)
309
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400310 var cqueryOutput string
311 var err error
Chris Parsons8ccdb632020-11-17 15:41:01 -0500312
313 err = os.Mkdir(absolutePath(context.intermediatesDir()), 0777)
314 if err != nil {
315 return err
316 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400317 err = ioutil.WriteFile(
Chris Parsons8ccdb632020-11-17 15:41:01 -0500318 absolutePath(filepath.Join(context.intermediatesDir(), "main.bzl")),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400319 context.mainBzlFileContents(), 0666)
320 if err != nil {
321 return err
322 }
323 err = ioutil.WriteFile(
Chris Parsons8ccdb632020-11-17 15:41:01 -0500324 absolutePath(filepath.Join(context.intermediatesDir(), "BUILD.bazel")),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400325 context.mainBuildFileContents(), 0666)
326 if err != nil {
327 return err
328 }
Chris Parsons8ccdb632020-11-17 15:41:01 -0500329 cquery_file_relpath := filepath.Join(context.intermediatesDir(), "buildroot.cquery")
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400330 err = ioutil.WriteFile(
331 absolutePath(cquery_file_relpath),
332 context.cqueryStarlarkFileContents(), 0666)
333 if err != nil {
334 return err
335 }
Chris Parsons8ccdb632020-11-17 15:41:01 -0500336 workspace_file_relpath := filepath.Join(context.intermediatesDir(), "WORKSPACE.bazel")
337 err = ioutil.WriteFile(
338 absolutePath(workspace_file_relpath),
339 context.workspaceFileContents(), 0666)
340 if err != nil {
341 return err
342 }
343 buildroot_label := "//:buildroot"
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400344 cqueryOutput, err = context.issueBazelCommand("cquery",
345 []string{fmt.Sprintf("deps(%s)", buildroot_label)},
346 "--output=starlark",
347 "--starlark:file="+cquery_file_relpath)
348
349 if err != nil {
350 return err
351 }
352
353 cqueryResults := map[string]string{}
354 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
355 if strings.Contains(outputLine, ">>") {
356 splitLine := strings.SplitN(outputLine, ">>", 2)
357 cqueryResults[splitLine[0]] = splitLine[1]
358 }
359 }
360
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400361 for val, _ := range context.requests {
Chris Parsons8ccdb632020-11-17 15:41:01 -0500362 if cqueryResult, ok := cqueryResults[canonicalizeLabel(val.label)]; ok {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400363 context.results[val] = string(cqueryResult)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400364 } else {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400365 return fmt.Errorf("missing result for bazel target %s", val.label)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400366 }
367 }
368
369 // Issue a build command.
370 // TODO(cparsons): Invoking bazel execution during soong_build should be avoided;
371 // bazel actions should either be added to the Ninja file and executed later,
372 // or bazel should handle execution.
373 // TODO(cparsons): Use --target_pattern_file to avoid command line limits.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400374 _, err = context.issueBazelCommand("build", []string{buildroot_label})
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400375
376 if err != nil {
377 return err
378 }
379
380 // Clear requests.
381 context.requests = map[cqueryKey]bool{}
382 return nil
383}
Chris Parsonsa798d962020-10-12 23:44:08 -0400384
385// Singleton used for registering BUILD file ninja dependencies (needed
386// for correctness of builds which use Bazel.
387func BazelSingleton() Singleton {
388 return &bazelSingleton{}
389}
390
391type bazelSingleton struct{}
392
393func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
394 if ctx.Config().BazelContext.BazelEnabled() {
395 bazelBuildList := absolutePath(filepath.Join(
396 filepath.Dir(bootstrap.ModuleListFile), "bazel.list"))
397 ctx.AddNinjaFileDeps(bazelBuildList)
398
399 data, err := ioutil.ReadFile(bazelBuildList)
400 if err != nil {
401 ctx.Errorf(err.Error())
402 }
403 files := strings.Split(strings.TrimSpace(string(data)), "\n")
404 for _, file := range files {
405 ctx.AddNinjaFileDeps(file)
406 }
407 }
408}