blob: a1ba8c9f618c0e5a3626a56fabb9a9ca467e38c2 [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...)
Chris Parsons8ccdb632020-11-17 15:41:01 -0500198 cmdFlags = append(cmdFlags, "--package_path=%workspace%/"+context.intermediatesDir())
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400199 cmdFlags = append(cmdFlags, extraFlags...)
200
201 bazelCmd := exec.Command(context.bazelPath, cmdFlags...)
202 bazelCmd.Dir = context.workspaceDir
203 bazelCmd.Env = append(os.Environ(), "HOME="+context.homeDir, pwdPrefix())
204
Colin Crossff0278b2020-10-09 19:24:15 -0700205 stderr := &bytes.Buffer{}
206 bazelCmd.Stderr = stderr
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400207
208 if output, err := bazelCmd.Output(); err != nil {
209 return "", fmt.Errorf("bazel command failed. command: [%s], error [%s]", bazelCmd, stderr)
210 } else {
211 return string(output), nil
212 }
213}
214
Chris Parsons8ccdb632020-11-17 15:41:01 -0500215// Returns the string contents of a workspace file that should be output
216// adjacent to the main bzl file and build file.
217// This workspace file allows, via local_repository rule, sourcetree-level
218// BUILD targets to be referenced via @sourceroot.
219func (context *bazelContext) workspaceFileContents() []byte {
220 formatString := `
221# This file is generated by soong_build. Do not edit.
222local_repository(
223 name = "sourceroot",
224 path = "%s",
225)
226`
227 return []byte(fmt.Sprintf(formatString, context.workspaceDir))
228}
229
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400230func (context *bazelContext) mainBzlFileContents() []byte {
231 contents := `
232# This file is generated by soong_build. Do not edit.
233def _mixed_build_root_impl(ctx):
234 return [DefaultInfo(files = depset(ctx.files.deps))]
235
236mixed_build_root = rule(
237 implementation = _mixed_build_root_impl,
238 attrs = {"deps" : attr.label_list()},
239)
240`
241 return []byte(contents)
242}
243
Chris Parsons8ccdb632020-11-17 15:41:01 -0500244// Returns a "canonicalized" corresponding to the given sourcetree-level label.
245// This abstraction is required because a sourcetree label such as //foo/bar:baz
246// must be referenced via the local repository prefix, such as
247// @sourceroot//foo/bar:baz.
248func canonicalizeLabel(label string) string {
249 if strings.HasPrefix(label, "//") {
250 return "@sourceroot" + label
251 } else {
252 return "@sourceroot//" + label
253 }
254}
255
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400256func (context *bazelContext) mainBuildFileContents() []byte {
257 formatString := `
258# This file is generated by soong_build. Do not edit.
259load(":main.bzl", "mixed_build_root")
260
261mixed_build_root(name = "buildroot",
262 deps = [%s],
263)
264`
265 var buildRootDeps []string = nil
266 for val, _ := range context.requests {
Chris Parsons8ccdb632020-11-17 15:41:01 -0500267 buildRootDeps = append(buildRootDeps, fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label)))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400268 }
269 buildRootDepsString := strings.Join(buildRootDeps, ",\n ")
270
271 return []byte(fmt.Sprintf(formatString, buildRootDepsString))
272}
273
274func (context *bazelContext) cqueryStarlarkFileContents() []byte {
275 formatString := `
276# This file is generated by soong_build. Do not edit.
277getAllFilesLabels = {
278 %s
279}
280
281def format(target):
282 if str(target.label) in getAllFilesLabels:
283 return str(target.label) + ">>" + ', '.join([f.path for f in target.files.to_list()])
284 else:
285 # This target was not requested via cquery, and thus must be a dependency
286 # of a requested target.
287 return ""
288`
289 var buildRootDeps []string = nil
290 // TODO(cparsons): Sort by request type instead of assuming all requests
291 // are of GetAllFiles type.
292 for val, _ := range context.requests {
Chris Parsons8ccdb632020-11-17 15:41:01 -0500293 buildRootDeps = append(buildRootDeps, fmt.Sprintf("\"%s\" : True", canonicalizeLabel(val.label)))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400294 }
295 buildRootDepsString := strings.Join(buildRootDeps, ",\n ")
296
297 return []byte(fmt.Sprintf(formatString, buildRootDepsString))
298}
299
Chris Parsons8ccdb632020-11-17 15:41:01 -0500300// Returns a workspace-relative path containing build-related metadata required
301// for interfacing with Bazel. Example: out/soong/bazel.
302func (context *bazelContext) intermediatesDir() string {
303 return filepath.Join(context.buildDir, "bazel")
304}
305
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400306// Issues commands to Bazel to receive results for all cquery requests
307// queued in the BazelContext.
308func (context *bazelContext) InvokeBazel() error {
309 context.results = make(map[cqueryKey]string)
310
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400311 var cqueryOutput string
312 var err error
Chris Parsons8ccdb632020-11-17 15:41:01 -0500313
314 err = os.Mkdir(absolutePath(context.intermediatesDir()), 0777)
315 if err != nil {
316 return err
317 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400318 err = ioutil.WriteFile(
Chris Parsons8ccdb632020-11-17 15:41:01 -0500319 absolutePath(filepath.Join(context.intermediatesDir(), "main.bzl")),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400320 context.mainBzlFileContents(), 0666)
321 if err != nil {
322 return err
323 }
324 err = ioutil.WriteFile(
Chris Parsons8ccdb632020-11-17 15:41:01 -0500325 absolutePath(filepath.Join(context.intermediatesDir(), "BUILD.bazel")),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400326 context.mainBuildFileContents(), 0666)
327 if err != nil {
328 return err
329 }
Chris Parsons8ccdb632020-11-17 15:41:01 -0500330 cquery_file_relpath := filepath.Join(context.intermediatesDir(), "buildroot.cquery")
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400331 err = ioutil.WriteFile(
332 absolutePath(cquery_file_relpath),
333 context.cqueryStarlarkFileContents(), 0666)
334 if err != nil {
335 return err
336 }
Chris Parsons8ccdb632020-11-17 15:41:01 -0500337 workspace_file_relpath := filepath.Join(context.intermediatesDir(), "WORKSPACE.bazel")
338 err = ioutil.WriteFile(
339 absolutePath(workspace_file_relpath),
340 context.workspaceFileContents(), 0666)
341 if err != nil {
342 return err
343 }
344 buildroot_label := "//:buildroot"
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400345 cqueryOutput, err = context.issueBazelCommand("cquery",
346 []string{fmt.Sprintf("deps(%s)", buildroot_label)},
347 "--output=starlark",
348 "--starlark:file="+cquery_file_relpath)
349
350 if err != nil {
351 return err
352 }
353
354 cqueryResults := map[string]string{}
355 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
356 if strings.Contains(outputLine, ">>") {
357 splitLine := strings.SplitN(outputLine, ">>", 2)
358 cqueryResults[splitLine[0]] = splitLine[1]
359 }
360 }
361
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400362 for val, _ := range context.requests {
Chris Parsons8ccdb632020-11-17 15:41:01 -0500363 if cqueryResult, ok := cqueryResults[canonicalizeLabel(val.label)]; ok {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400364 context.results[val] = string(cqueryResult)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400365 } else {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400366 return fmt.Errorf("missing result for bazel target %s", val.label)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400367 }
368 }
369
370 // Issue a build command.
371 // TODO(cparsons): Invoking bazel execution during soong_build should be avoided;
372 // bazel actions should either be added to the Ninja file and executed later,
373 // or bazel should handle execution.
374 // TODO(cparsons): Use --target_pattern_file to avoid command line limits.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400375 _, err = context.issueBazelCommand("build", []string{buildroot_label})
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400376
377 if err != nil {
378 return err
379 }
380
381 // Clear requests.
382 context.requests = map[cqueryKey]bool{}
383 return nil
384}
Chris Parsonsa798d962020-10-12 23:44:08 -0400385
386// Singleton used for registering BUILD file ninja dependencies (needed
387// for correctness of builds which use Bazel.
388func BazelSingleton() Singleton {
389 return &bazelSingleton{}
390}
391
392type bazelSingleton struct{}
393
394func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
395 if ctx.Config().BazelContext.BazelEnabled() {
396 bazelBuildList := absolutePath(filepath.Join(
397 filepath.Dir(bootstrap.ModuleListFile), "bazel.list"))
398 ctx.AddNinjaFileDeps(bazelBuildList)
399
400 data, err := ioutil.ReadFile(bazelBuildList)
401 if err != nil {
402 ctx.Errorf(err.Error())
403 }
404 files := strings.Split(strings.TrimSpace(string(data)), "\n")
405 for _, file := range files {
406 ctx.AddNinjaFileDeps(file)
407 }
408 }
409}