blob: 9b44ae398447dce63dc9fe0a0b894856b0380626 [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
Chris Parsons944e7d02021-03-11 11:08:46 -050029 "android/soong/bazel/cquery"
Liz Kammer8206d4f2021-03-03 16:40:52 -050030
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050031 "github.com/google/blueprint/bootstrap"
32
Patrice Arruda05ab2d02020-12-12 06:24:26 +000033 "android/soong/bazel"
34 "android/soong/shared"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040035)
36
Liz Kammerf29df7c2021-04-02 13:37:39 -040037type cqueryRequest interface {
38 // Name returns a string name for this request type. Such request type names must be unique,
39 // and must only consist of alphanumeric characters.
40 Name() string
41
42 // StarlarkFunctionBody returns a starlark function body to process this request type.
43 // The returned string is the body of a Starlark function which obtains
44 // all request-relevant information about a target and returns a string containing
45 // this information.
46 // The function should have the following properties:
47 // - `target` is the only parameter to this function (a configured target).
48 // - The return value must be a string.
49 // - The function body should not be indented outside of its own scope.
50 StarlarkFunctionBody() string
51}
52
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040053// Map key to describe bazel cquery requests.
54type cqueryKey struct {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040055 label string
Liz Kammerf29df7c2021-04-02 13:37:39 -040056 requestType cqueryRequest
Chris Parsons8d6e4332021-02-22 16:13:50 -050057 archType ArchType
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040058}
59
60type BazelContext interface {
61 // The below methods involve queuing cquery requests to be later invoked
62 // by bazel. If any of these methods return (_, false), then the request
63 // has been queued to be run later.
64
65 // Returns result files built by building the given bazel target label.
Chris Parsons944e7d02021-03-11 11:08:46 -050066 GetOutputFiles(label string, archType ArchType) ([]string, bool)
Chris Parsons8d6e4332021-02-22 16:13:50 -050067
Chris Parsons944e7d02021-03-11 11:08:46 -050068 // TODO(cparsons): Other cquery-related methods should be added here.
69 // Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order).
70 GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool)
Chris Parsons808d84c2021-03-09 20:43:32 -050071
Liz Kammer3f9e1552021-04-02 18:47:09 -040072 // GetPrebuiltCcStaticLibraryFiles returns paths to prebuilt cc static libraries, and whether the
73 // results were available
74 GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool)
75
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040076 // ** End cquery methods
77
78 // Issues commands to Bazel to receive results for all cquery requests
79 // queued in the BazelContext.
80 InvokeBazel() error
81
82 // Returns true if bazel is enabled for the given configuration.
83 BazelEnabled() bool
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050084
85 // Returns the bazel output base (the root directory for all bazel intermediate outputs).
86 OutputBase() string
87
88 // Returns build statements which should get registered to reflect Bazel's outputs.
89 BuildStatementsToRegister() []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040090}
91
Liz Kammer8d62a4f2021-04-08 09:47:28 -040092type bazelRunner interface {
93 issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand, extraFlags ...string) (string, string, error)
94}
95
96type bazelPaths struct {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040097 homeDir string
98 bazelPath string
99 outputBase string
100 workspaceDir string
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400101 buildDir string
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000102 metricsDir string
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400103}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400104
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400105// A context object which tracks queued requests that need to be made to Bazel,
106// and their results after the requests have been made.
107type bazelContext struct {
108 bazelRunner
109 paths *bazelPaths
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400110 requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
111 requestMutex sync.Mutex // requests can be written in parallel
112
113 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500114
115 // Build statements which should get registered to reflect Bazel's outputs.
116 buildStatements []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400117}
118
119var _ BazelContext = &bazelContext{}
120
121// A bazel context to use when Bazel is disabled.
122type noopBazelContext struct{}
123
124var _ BazelContext = noopBazelContext{}
125
126// A bazel context to use for tests.
127type MockBazelContext struct {
Liz Kammera92e8442021-04-07 20:25:21 -0400128 OutputBaseDir string
129
130 LabelToOutputFiles map[string][]string
131 LabelToOutputFilesAndCcObjectFiles map[string]cquery.GetOutputFilesAndCcObjectFiles_Result
132 LabelToCcStaticLibraryFiles map[string][]string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400133}
134
Chris Parsons944e7d02021-03-11 11:08:46 -0500135func (m MockBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Liz Kammera92e8442021-04-07 20:25:21 -0400136 result, ok := m.LabelToOutputFiles[label]
Chris Parsons8d6e4332021-02-22 16:13:50 -0500137 return result, ok
138}
139
Chris Parsons944e7d02021-03-11 11:08:46 -0500140func (m MockBazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
Liz Kammera92e8442021-04-07 20:25:21 -0400141 result, ok := m.LabelToOutputFilesAndCcObjectFiles[label]
142 return result.OutputFiles, result.CcObjectFiles, ok
Chris Parsons808d84c2021-03-09 20:43:32 -0500143}
144
Liz Kammer3f9e1552021-04-02 18:47:09 -0400145func (m MockBazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) {
Liz Kammera92e8442021-04-07 20:25:21 -0400146 result, ok := m.LabelToCcStaticLibraryFiles[label]
Liz Kammer3f9e1552021-04-02 18:47:09 -0400147 return result, ok
148}
149
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400150func (m MockBazelContext) InvokeBazel() error {
151 panic("unimplemented")
152}
153
154func (m MockBazelContext) BazelEnabled() bool {
155 return true
156}
157
Liz Kammera92e8442021-04-07 20:25:21 -0400158func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500159
160func (m MockBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
161 return []bazel.BuildStatement{}
162}
163
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400164var _ BazelContext = MockBazelContext{}
165
Chris Parsons944e7d02021-03-11 11:08:46 -0500166func (bazelCtx *bazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
167 rawString, ok := bazelCtx.cquery(label, cquery.GetOutputFiles, archType)
168 var ret []string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400169 if ok {
Chris Parsons944e7d02021-03-11 11:08:46 -0500170 bazelOutput := strings.TrimSpace(rawString)
Liz Kammerf29df7c2021-04-02 13:37:39 -0400171 ret = cquery.GetOutputFiles.ParseResult(bazelOutput)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400172 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500173 return ret, ok
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400174}
175
Chris Parsons944e7d02021-03-11 11:08:46 -0500176func (bazelCtx *bazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
177 var outputFiles []string
Chris Parsons808d84c2021-03-09 20:43:32 -0500178 var ccObjects []string
179
Chris Parsons944e7d02021-03-11 11:08:46 -0500180 result, ok := bazelCtx.cquery(label, cquery.GetOutputFilesAndCcObjectFiles, archType)
Chris Parsons808d84c2021-03-09 20:43:32 -0500181 if ok {
182 bazelOutput := strings.TrimSpace(result)
Liz Kammerf29df7c2021-04-02 13:37:39 -0400183 returnResult := cquery.GetOutputFilesAndCcObjectFiles.ParseResult(bazelOutput)
Chris Parsons944e7d02021-03-11 11:08:46 -0500184 outputFiles = returnResult.OutputFiles
185 ccObjects = returnResult.CcObjectFiles
Chris Parsons808d84c2021-03-09 20:43:32 -0500186 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500187
188 return outputFiles, ccObjects, ok
Chris Parsons808d84c2021-03-09 20:43:32 -0500189}
190
Liz Kammer3f9e1552021-04-02 18:47:09 -0400191// GetPrebuiltCcStaticLibraryFiles returns a slice of prebuilt static libraries for the given
192// label/archType if there are query results; otherwise, it enqueues the query and returns false.
193func (bazelCtx *bazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) {
194 result, ok := bazelCtx.cquery(label, cquery.GetPrebuiltCcStaticLibraryFiles, archType)
195 if !ok {
196 return nil, false
197 }
198
199 bazelOutput := strings.TrimSpace(result)
200 ret := cquery.GetPrebuiltCcStaticLibraryFiles.ParseResult(bazelOutput)
201 return ret, ok
202}
203
Chris Parsons944e7d02021-03-11 11:08:46 -0500204func (n noopBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500205 panic("unimplemented")
206}
207
Chris Parsons944e7d02021-03-11 11:08:46 -0500208func (n noopBazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500209 panic("unimplemented")
210}
211
Liz Kammer3f9e1552021-04-02 18:47:09 -0400212func (n noopBazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) {
213 panic("unimplemented")
214}
215
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400216func (n noopBazelContext) InvokeBazel() error {
217 panic("unimplemented")
218}
219
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500220func (m noopBazelContext) OutputBase() string {
221 return ""
222}
223
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400224func (n noopBazelContext) BazelEnabled() bool {
225 return false
226}
227
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500228func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
229 return []bazel.BuildStatement{}
230}
231
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400232func NewBazelContext(c *config) (BazelContext, error) {
Chris Parsons8b77a002020-10-27 18:59:25 -0400233 // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds"
234 // are production ready.
235 if c.Getenv("USE_BAZEL_ANALYSIS") != "1" {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400236 return noopBazelContext{}, nil
237 }
238
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400239 p, err := bazelPathsFromConfig(c)
240 if err != nil {
241 return nil, err
242 }
243 return &bazelContext{
244 bazelRunner: &builtinBazelRunner{},
245 paths: p,
246 requests: make(map[cqueryKey]bool),
247 }, nil
248}
249
250func bazelPathsFromConfig(c *config) (*bazelPaths, error) {
251 p := bazelPaths{
252 buildDir: c.buildDir,
253 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400254 missingEnvVars := []string{}
255 if len(c.Getenv("BAZEL_HOME")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400256 p.homeDir = c.Getenv("BAZEL_HOME")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400257 } else {
258 missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
259 }
260 if len(c.Getenv("BAZEL_PATH")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400261 p.bazelPath = c.Getenv("BAZEL_PATH")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400262 } else {
263 missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
264 }
265 if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400266 p.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400267 } else {
268 missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
269 }
270 if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400271 p.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400272 } else {
273 missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
274 }
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000275 if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400276 p.metricsDir = c.Getenv("BAZEL_METRICS_DIR")
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000277 } else {
278 missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR")
279 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400280 if len(missingEnvVars) > 0 {
281 return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
282 } else {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400283 return &p, nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400284 }
285}
286
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400287func (p *bazelPaths) BazelMetricsDir() string {
288 return p.metricsDir
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000289}
290
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400291func (context *bazelContext) BazelEnabled() bool {
292 return true
293}
294
295// Adds a cquery request to the Bazel request queue, to be later invoked, or
296// returns the result of the given request if the request was already made.
297// If the given request was already made (and the results are available), then
298// returns (result, true). If the request is queued but no results are available,
299// then returns ("", false).
Liz Kammerf29df7c2021-04-02 13:37:39 -0400300func (context *bazelContext) cquery(label string, requestType cqueryRequest,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500301 archType ArchType) (string, bool) {
302 key := cqueryKey{label, requestType, archType}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400303 if result, ok := context.results[key]; ok {
304 return result, true
305 } else {
306 context.requestMutex.Lock()
307 defer context.requestMutex.Unlock()
308 context.requests[key] = true
309 return "", false
310 }
311}
312
313func pwdPrefix() string {
314 // Darwin doesn't have /proc
315 if runtime.GOOS != "darwin" {
316 return "PWD=/proc/self/cwd"
317 }
318 return ""
319}
320
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400321type bazelCommand struct {
322 command string
323 // query or label
324 expression string
325}
326
327type mockBazelRunner struct {
328 bazelCommandResults map[bazelCommand]string
329 commands []bazelCommand
330}
331
332func (r *mockBazelRunner) issueBazelCommand(paths *bazelPaths,
333 runName bazel.RunName,
334 command bazelCommand,
335 extraFlags ...string) (string, string, error) {
336 r.commands = append(r.commands, command)
337 if ret, ok := r.bazelCommandResults[command]; ok {
338 return ret, "", nil
339 }
340 return "", "", nil
341}
342
343type builtinBazelRunner struct{}
344
Chris Parsons808d84c2021-03-09 20:43:32 -0500345// Issues the given bazel command with given build label and additional flags.
346// Returns (stdout, stderr, error). The first and second return values are strings
347// containing the stdout and stderr of the run command, and an error is returned if
348// the invocation returned an error code.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400349func (r *builtinBazelRunner) issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand,
Chris Parsons808d84c2021-03-09 20:43:32 -0500350 extraFlags ...string) (string, string, error) {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400351 cmdFlags := []string{"--output_base=" + paths.outputBase, command.command}
352 cmdFlags = append(cmdFlags, command.expression)
353 cmdFlags = append(cmdFlags, "--package_path=%workspace%/"+paths.intermediatesDir())
354 cmdFlags = append(cmdFlags, "--profile="+shared.BazelMetricsFilename(paths, runName))
Jingwen Chen91220d72021-03-24 02:18:33 -0400355
356 // Set default platforms to canonicalized values for mixed builds requests.
357 // If these are set in the bazelrc, they will have values that are
358 // non-canonicalized to @sourceroot labels, and thus be invalid when
359 // referenced from the buildroot.
360 //
361 // The actual platform values here may be overridden by configuration
362 // transitions from the buildroot.
Chris Parsonsee423b02021-02-08 23:04:59 -0500363 cmdFlags = append(cmdFlags,
Jingwen Chen91220d72021-03-24 02:18:33 -0400364 fmt.Sprintf("--platforms=%s", canonicalizeLabel("//build/bazel/platforms:android_x86_64")))
Chris Parsonsee423b02021-02-08 23:04:59 -0500365 cmdFlags = append(cmdFlags,
366 fmt.Sprintf("--extra_toolchains=%s", canonicalizeLabel("//prebuilts/clang/host/linux-x86:all")))
Jingwen Chen91220d72021-03-24 02:18:33 -0400367 // This should be parameterized on the host OS, but let's restrict to linux
368 // to keep things simple for now.
369 cmdFlags = append(cmdFlags,
370 fmt.Sprintf("--host_platform=%s", canonicalizeLabel("//build/bazel/platforms:linux_x86_64")))
371
Chris Parsons8d6e4332021-02-22 16:13:50 -0500372 // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network.
373 cmdFlags = append(cmdFlags, "--experimental_repository_disable_download")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400374 cmdFlags = append(cmdFlags, extraFlags...)
375
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400376 bazelCmd := exec.Command(paths.bazelPath, cmdFlags...)
377 bazelCmd.Dir = paths.workspaceDir
378 bazelCmd.Env = append(os.Environ(), "HOME="+paths.homeDir, pwdPrefix(),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500379 // Disables local host detection of gcc; toolchain information is defined
380 // explicitly in BUILD files.
381 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1")
Colin Crossff0278b2020-10-09 19:24:15 -0700382 stderr := &bytes.Buffer{}
383 bazelCmd.Stderr = stderr
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400384
385 if output, err := bazelCmd.Output(); err != nil {
Chris Parsons808d84c2021-03-09 20:43:32 -0500386 return "", string(stderr.Bytes()),
387 fmt.Errorf("bazel command failed. command: [%s], env: [%s], error [%s]", bazelCmd, bazelCmd.Env, stderr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400388 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500389 return string(output), string(stderr.Bytes()), nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400390 }
391}
392
Chris Parsons8ccdb632020-11-17 15:41:01 -0500393// Returns the string contents of a workspace file that should be output
394// adjacent to the main bzl file and build file.
395// This workspace file allows, via local_repository rule, sourcetree-level
396// BUILD targets to be referenced via @sourceroot.
397func (context *bazelContext) workspaceFileContents() []byte {
398 formatString := `
399# This file is generated by soong_build. Do not edit.
400local_repository(
401 name = "sourceroot",
Jingwen Chen63930982021-03-24 10:04:33 -0400402 path = "%[1]s",
Chris Parsons8ccdb632020-11-17 15:41:01 -0500403)
Liz Kammer8206d4f2021-03-03 16:40:52 -0500404
405local_repository(
406 name = "rules_cc",
Jingwen Chen63930982021-03-24 10:04:33 -0400407 path = "%[1]s/build/bazel/rules_cc",
408)
409
410local_repository(
411 name = "bazel_skylib",
412 path = "%[1]s/build/bazel/bazel_skylib",
Liz Kammer8206d4f2021-03-03 16:40:52 -0500413)
Chris Parsons8ccdb632020-11-17 15:41:01 -0500414`
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400415 return []byte(fmt.Sprintf(formatString, context.paths.workspaceDir))
Chris Parsons8ccdb632020-11-17 15:41:01 -0500416}
417
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400418func (context *bazelContext) mainBzlFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500419 // TODO(cparsons): Define configuration transitions programmatically based
420 // on available archs.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400421 contents := `
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500422#####################################################
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400423# This file is generated by soong_build. Do not edit.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500424#####################################################
425
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400426def _config_node_transition_impl(settings, attr):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500427 return {
Jingwen Chen91220d72021-03-24 02:18:33 -0400428 "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:android_%s" % attr.arch,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500429 }
430
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400431_config_node_transition = transition(
432 implementation = _config_node_transition_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500433 inputs = [],
434 outputs = [
435 "//command_line_option:platforms",
436 ],
437)
438
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400439def _passthrough_rule_impl(ctx):
440 return [DefaultInfo(files = depset(ctx.files.deps))]
441
442config_node = rule(
443 implementation = _passthrough_rule_impl,
444 attrs = {
445 "arch" : attr.string(mandatory = True),
446 "deps" : attr.label_list(cfg = _config_node_transition),
447 "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
448 },
Chris Parsons8d6e4332021-02-22 16:13:50 -0500449)
450
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400451
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500452# Rule representing the root of the build, to depend on all Bazel targets that
453# are required for the build. Building this target will build the entire Bazel
454# build tree.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400455mixed_build_root = rule(
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400456 implementation = _passthrough_rule_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500457 attrs = {
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400458 "deps" : attr.label_list(),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500459 },
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400460)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500461
462def _phony_root_impl(ctx):
463 return []
464
465# Rule to depend on other targets but build nothing.
466# This is useful as follows: building a target of this rule will generate
467# symlink forests for all dependencies of the target, without executing any
468# actions of the build.
469phony_root = rule(
470 implementation = _phony_root_impl,
471 attrs = {"deps" : attr.label_list()},
472)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400473`
474 return []byte(contents)
475}
476
Chris Parsons8ccdb632020-11-17 15:41:01 -0500477// Returns a "canonicalized" corresponding to the given sourcetree-level label.
478// This abstraction is required because a sourcetree label such as //foo/bar:baz
479// must be referenced via the local repository prefix, such as
480// @sourceroot//foo/bar:baz.
481func canonicalizeLabel(label string) string {
482 if strings.HasPrefix(label, "//") {
483 return "@sourceroot" + label
484 } else {
485 return "@sourceroot//" + label
486 }
487}
488
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400489func (context *bazelContext) mainBuildFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500490 // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
491 // architecture mapping.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400492 formatString := `
493# This file is generated by soong_build. Do not edit.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400494load(":main.bzl", "config_node", "mixed_build_root", "phony_root")
495
496%s
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400497
498mixed_build_root(name = "buildroot",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400499 deps = [%s],
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400500)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500501
502phony_root(name = "phonyroot",
503 deps = [":buildroot"],
504)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400505`
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400506 configNodeFormatString := `
507config_node(name = "%s",
508 arch = "%s",
509 deps = [%s],
510)
511`
512
513 configNodesSection := ""
514
515 labelsByArch := map[string][]string{}
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400516 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500517 labelString := fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label))
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400518 archString := getArchString(val)
519 labelsByArch[archString] = append(labelsByArch[archString], labelString)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400520 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400521
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400522 configNodeLabels := []string{}
523 for archString, labels := range labelsByArch {
524 configNodeLabels = append(configNodeLabels, fmt.Sprintf("\":%s\"", archString))
525 labelsString := strings.Join(labels, ",\n ")
526 configNodesSection += fmt.Sprintf(configNodeFormatString, archString, archString, labelsString)
527 }
528
529 return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(configNodeLabels, ",\n ")))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400530}
531
Chris Parsons944e7d02021-03-11 11:08:46 -0500532func indent(original string) string {
533 result := ""
534 for _, line := range strings.Split(original, "\n") {
535 result += " " + line + "\n"
536 }
537 return result
538}
539
Chris Parsons808d84c2021-03-09 20:43:32 -0500540// Returns the file contents of the buildroot.cquery file that should be used for the cquery
541// expression in order to obtain information about buildroot and its dependencies.
542// The contents of this file depend on the bazelContext's requests; requests are enumerated
543// and grouped by their request type. The data retrieved for each label depends on its
544// request type.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400545func (context *bazelContext) cqueryStarlarkFileContents() []byte {
Liz Kammerf29df7c2021-04-02 13:37:39 -0400546 requestTypeToCqueryIdEntries := map[cqueryRequest][]string{}
Chris Parsons944e7d02021-03-11 11:08:46 -0500547 for val, _ := range context.requests {
548 cqueryId := getCqueryId(val)
549 mapEntryString := fmt.Sprintf("%q : True", cqueryId)
550 requestTypeToCqueryIdEntries[val.requestType] =
551 append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString)
552 }
553 labelRegistrationMapSection := ""
554 functionDefSection := ""
555 mainSwitchSection := ""
556
557 mapDeclarationFormatString := `
558%s = {
559 %s
560}
561`
562 functionDefFormatString := `
563def %s(target):
564%s
565`
566 mainSwitchSectionFormatString := `
567 if id_string in %s:
568 return id_string + ">>" + %s(target)
569`
570
Liz Kammer66ffdb72021-04-02 13:26:07 -0400571 for requestType, _ := range requestTypeToCqueryIdEntries {
Chris Parsons944e7d02021-03-11 11:08:46 -0500572 labelMapName := requestType.Name() + "_Labels"
573 functionName := requestType.Name() + "_Fn"
574 labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString,
575 labelMapName,
576 strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n "))
577 functionDefSection += fmt.Sprintf(functionDefFormatString,
578 functionName,
579 indent(requestType.StarlarkFunctionBody()))
580 mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString,
581 labelMapName, functionName)
582 }
583
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400584 formatString := `
585# This file is generated by soong_build. Do not edit.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400586
Chris Parsons944e7d02021-03-11 11:08:46 -0500587# Label Map Section
588%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500589
Chris Parsons944e7d02021-03-11 11:08:46 -0500590# Function Def Section
591%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500592
593def get_arch(target):
594 buildoptions = build_options(target)
595 platforms = build_options(target)["//command_line_option:platforms"]
596 if len(platforms) != 1:
597 # An individual configured target should have only one platform architecture.
598 # Note that it's fine for there to be multiple architectures for the same label,
599 # but each is its own configured target.
600 fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
601 platform_name = build_options(target)["//command_line_option:platforms"][0].name
602 if platform_name == "host":
603 return "HOST"
Jingwen Chen91220d72021-03-24 02:18:33 -0400604 elif not platform_name.startswith("android_"):
605 fail("expected platform name of the form 'android_<arch>', but was " + str(platforms))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500606 return "UNKNOWN"
Jingwen Chen91220d72021-03-24 02:18:33 -0400607 return platform_name[len("android_"):]
Chris Parsons8d6e4332021-02-22 16:13:50 -0500608
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400609def format(target):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500610 id_string = str(target.label) + "|" + get_arch(target)
Chris Parsons944e7d02021-03-11 11:08:46 -0500611
612 # Main switch section
613 %s
614 # This target was not requested via cquery, and thus must be a dependency
615 # of a requested target.
616 return id_string + ">>NONE"
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400617`
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400618
Chris Parsons944e7d02021-03-11 11:08:46 -0500619 return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection,
620 mainSwitchSection))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400621}
622
Chris Parsons8ccdb632020-11-17 15:41:01 -0500623// Returns a workspace-relative path containing build-related metadata required
624// for interfacing with Bazel. Example: out/soong/bazel.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400625func (p *bazelPaths) intermediatesDir() string {
626 return filepath.Join(p.buildDir, "bazel")
Chris Parsons8ccdb632020-11-17 15:41:01 -0500627}
628
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400629// Issues commands to Bazel to receive results for all cquery requests
630// queued in the BazelContext.
631func (context *bazelContext) InvokeBazel() error {
632 context.results = make(map[cqueryKey]string)
633
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400634 var cqueryOutput string
Chris Parsons808d84c2021-03-09 20:43:32 -0500635 var cqueryErr string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400636 var err error
Chris Parsons8ccdb632020-11-17 15:41:01 -0500637
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400638 intermediatesDirPath := absolutePath(context.paths.intermediatesDir())
Chris Parsons07c1e4a2021-01-19 17:19:16 -0500639 if _, err := os.Stat(intermediatesDirPath); os.IsNotExist(err) {
640 err = os.Mkdir(intermediatesDirPath, 0777)
641 }
642
Chris Parsons8ccdb632020-11-17 15:41:01 -0500643 if err != nil {
644 return err
645 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400646 err = ioutil.WriteFile(
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400647 filepath.Join(intermediatesDirPath, "main.bzl"),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400648 context.mainBzlFileContents(), 0666)
649 if err != nil {
650 return err
651 }
652 err = ioutil.WriteFile(
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400653 filepath.Join(intermediatesDirPath, "BUILD.bazel"),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400654 context.mainBuildFileContents(), 0666)
655 if err != nil {
656 return err
657 }
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400658 cqueryFileRelpath := filepath.Join(context.paths.intermediatesDir(), "buildroot.cquery")
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400659 err = ioutil.WriteFile(
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800660 absolutePath(cqueryFileRelpath),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400661 context.cqueryStarlarkFileContents(), 0666)
662 if err != nil {
663 return err
664 }
Chris Parsons8ccdb632020-11-17 15:41:01 -0500665 err = ioutil.WriteFile(
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400666 filepath.Join(intermediatesDirPath, "WORKSPACE.bazel"),
Chris Parsons8ccdb632020-11-17 15:41:01 -0500667 context.workspaceFileContents(), 0666)
668 if err != nil {
669 return err
670 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800671 buildrootLabel := "//:buildroot"
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400672 cqueryOutput, cqueryErr, err = context.issueBazelCommand(
673 context.paths,
674 bazel.CqueryBuildRootRunName,
675 bazelCommand{"cquery", fmt.Sprintf("kind(rule, deps(%s))", buildrootLabel)},
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400676 "--output=starlark",
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800677 "--starlark:file="+cqueryFileRelpath)
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400678 err = ioutil.WriteFile(filepath.Join(intermediatesDirPath, "cquery.out"),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500679 []byte(cqueryOutput), 0666)
680 if err != nil {
681 return err
682 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400683
684 if err != nil {
685 return err
686 }
687
688 cqueryResults := map[string]string{}
689 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
690 if strings.Contains(outputLine, ">>") {
691 splitLine := strings.SplitN(outputLine, ">>", 2)
692 cqueryResults[splitLine[0]] = splitLine[1]
693 }
694 }
695
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400696 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500697 if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400698 context.results[val] = string(cqueryResult)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400699 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500700 return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]",
701 getCqueryId(val), cqueryOutput, cqueryErr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400702 }
703 }
704
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500705 // Issue an aquery command to retrieve action information about the bazel build tree.
706 //
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400707 // TODO(cparsons): Use --target_pattern_file to avoid command line limits.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500708 var aqueryOutput string
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400709 aqueryOutput, _, err = context.issueBazelCommand(
710 context.paths,
711 bazel.AqueryBuildRootRunName,
712 bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)},
713 // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
714 // proto sources, which would add a number of unnecessary dependencies.
715 "--output=jsonproto")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400716
717 if err != nil {
718 return err
719 }
720
Chris Parsons4f069892021-01-15 12:22:41 -0500721 context.buildStatements, err = bazel.AqueryBuildStatements([]byte(aqueryOutput))
722 if err != nil {
723 return err
724 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500725
726 // Issue a build command of the phony root to generate symlink forests for dependencies of the
727 // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
728 // but some of symlinks may be required to resolve source dependencies of the build.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400729 _, _, err = context.issueBazelCommand(
730 context.paths,
731 bazel.BazelBuildPhonyRootRunName,
732 bazelCommand{"build", "//:phonyroot"})
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500733
734 if err != nil {
735 return err
736 }
737
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400738 // Clear requests.
739 context.requests = map[cqueryKey]bool{}
740 return nil
741}
Chris Parsonsa798d962020-10-12 23:44:08 -0400742
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500743func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
744 return context.buildStatements
745}
746
747func (context *bazelContext) OutputBase() string {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400748 return context.paths.outputBase
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500749}
750
Chris Parsonsa798d962020-10-12 23:44:08 -0400751// Singleton used for registering BUILD file ninja dependencies (needed
752// for correctness of builds which use Bazel.
753func BazelSingleton() Singleton {
754 return &bazelSingleton{}
755}
756
757type bazelSingleton struct{}
758
759func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500760 // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
761 if !ctx.Config().BazelContext.BazelEnabled() {
762 return
763 }
Chris Parsonsa798d962020-10-12 23:44:08 -0400764
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500765 // Add ninja file dependencies for files which all bazel invocations require.
766 bazelBuildList := absolutePath(filepath.Join(
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100767 filepath.Dir(bootstrap.CmdlineModuleListFile()), "bazel.list"))
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500768 ctx.AddNinjaFileDeps(bazelBuildList)
769
770 data, err := ioutil.ReadFile(bazelBuildList)
771 if err != nil {
772 ctx.Errorf(err.Error())
773 }
774 files := strings.Split(strings.TrimSpace(string(data)), "\n")
775 for _, file := range files {
776 ctx.AddNinjaFileDeps(file)
777 }
778
779 // Register bazel-owned build statements (obtained from the aquery invocation).
780 for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500781 if len(buildStatement.Command) < 1 {
Rupert Shuttlewortha29903f2021-04-06 16:17:33 +0000782 panic(fmt.Sprintf("unhandled build statement: %v", buildStatement))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500783 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500784 rule := NewRuleBuilder(pctx, ctx)
785 cmd := rule.Command()
786 cmd.Text(fmt.Sprintf("cd %s/execroot/__main__ && %s",
787 ctx.Config().BazelContext.OutputBase(), buildStatement.Command))
788
789 for _, outputPath := range buildStatement.OutputPaths {
790 cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400791 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500792 for _, inputPath := range buildStatement.InputPaths {
793 cmd.Implicit(PathForBazelOut(ctx, inputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400794 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500795
Liz Kammerde116852021-03-25 16:42:37 -0400796 if depfile := buildStatement.Depfile; depfile != nil {
797 cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile))
798 }
799
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500800 // This is required to silence warnings pertaining to unexpected timestamps. Particularly,
801 // some Bazel builtins (such as files in the bazel_tools directory) have far-future
802 // timestamps. Without restat, Ninja would emit warnings that the input files of a
803 // build statement have later timestamps than the outputs.
804 rule.Restat()
805
Liz Kammer13548d72020-12-16 11:13:30 -0800806 rule.Build(fmt.Sprintf("bazel %d", index), buildStatement.Mnemonic)
Chris Parsonsa798d962020-10-12 23:44:08 -0400807 }
808}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500809
810func getCqueryId(key cqueryKey) string {
811 return canonicalizeLabel(key.label) + "|" + getArchString(key)
812}
813
814func getArchString(key cqueryKey) string {
815 arch := key.archType.Name
816 if len(arch) > 0 {
817 return arch
818 } else {
819 return "x86_64"
820 }
821}