blob: 31c31fbded0e7c392846eff19087a5544abf75c8 [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).
Liz Kammerb71794d2021-04-09 14:07:00 -040070 GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool)
Liz Kammer3f9e1552021-04-02 18:47:09 -040071
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040072 // ** End cquery methods
73
74 // Issues commands to Bazel to receive results for all cquery requests
75 // queued in the BazelContext.
76 InvokeBazel() error
77
78 // Returns true if bazel is enabled for the given configuration.
79 BazelEnabled() bool
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050080
81 // Returns the bazel output base (the root directory for all bazel intermediate outputs).
82 OutputBase() string
83
84 // Returns build statements which should get registered to reflect Bazel's outputs.
85 BuildStatementsToRegister() []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040086}
87
Liz Kammer8d62a4f2021-04-08 09:47:28 -040088type bazelRunner interface {
89 issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand, extraFlags ...string) (string, string, error)
90}
91
92type bazelPaths struct {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040093 homeDir string
94 bazelPath string
95 outputBase string
96 workspaceDir string
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040097 buildDir string
Patrice Arruda05ab2d02020-12-12 06:24:26 +000098 metricsDir string
Liz Kammer8d62a4f2021-04-08 09:47:28 -040099}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400100
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400101// A context object which tracks queued requests that need to be made to Bazel,
102// and their results after the requests have been made.
103type bazelContext struct {
104 bazelRunner
105 paths *bazelPaths
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400106 requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
107 requestMutex sync.Mutex // requests can be written in parallel
108
109 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500110
111 // Build statements which should get registered to reflect Bazel's outputs.
112 buildStatements []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400113}
114
115var _ BazelContext = &bazelContext{}
116
117// A bazel context to use when Bazel is disabled.
118type noopBazelContext struct{}
119
120var _ BazelContext = noopBazelContext{}
121
122// A bazel context to use for tests.
123type MockBazelContext struct {
Liz Kammera92e8442021-04-07 20:25:21 -0400124 OutputBaseDir string
125
Liz Kammerb71794d2021-04-09 14:07:00 -0400126 LabelToOutputFiles map[string][]string
127 LabelToCcInfo map[string]cquery.CcInfo
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400128}
129
Chris Parsons944e7d02021-03-11 11:08:46 -0500130func (m MockBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Liz Kammera92e8442021-04-07 20:25:21 -0400131 result, ok := m.LabelToOutputFiles[label]
Chris Parsons8d6e4332021-02-22 16:13:50 -0500132 return result, ok
133}
134
Liz Kammerb71794d2021-04-09 14:07:00 -0400135func (m MockBazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool) {
136 result, ok := m.LabelToCcInfo[label]
Liz Kammer3f9e1552021-04-02 18:47:09 -0400137 return result, ok
138}
139
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400140func (m MockBazelContext) InvokeBazel() error {
141 panic("unimplemented")
142}
143
144func (m MockBazelContext) BazelEnabled() bool {
145 return true
146}
147
Liz Kammera92e8442021-04-07 20:25:21 -0400148func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500149
150func (m MockBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
151 return []bazel.BuildStatement{}
152}
153
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400154var _ BazelContext = MockBazelContext{}
155
Chris Parsons944e7d02021-03-11 11:08:46 -0500156func (bazelCtx *bazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
157 rawString, ok := bazelCtx.cquery(label, cquery.GetOutputFiles, archType)
158 var ret []string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400159 if ok {
Chris Parsons944e7d02021-03-11 11:08:46 -0500160 bazelOutput := strings.TrimSpace(rawString)
Liz Kammerf29df7c2021-04-02 13:37:39 -0400161 ret = cquery.GetOutputFiles.ParseResult(bazelOutput)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400162 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500163 return ret, ok
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400164}
165
Liz Kammerb71794d2021-04-09 14:07:00 -0400166func (bazelCtx *bazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool) {
167 result, ok := bazelCtx.cquery(label, cquery.GetCcInfo, archType)
Liz Kammer3f9e1552021-04-02 18:47:09 -0400168 if !ok {
Liz Kammerb71794d2021-04-09 14:07:00 -0400169 return cquery.CcInfo{}, ok
Liz Kammer3f9e1552021-04-02 18:47:09 -0400170 }
171
172 bazelOutput := strings.TrimSpace(result)
Liz Kammerb71794d2021-04-09 14:07:00 -0400173 return cquery.GetCcInfo.ParseResult(bazelOutput), ok
Liz Kammer3f9e1552021-04-02 18:47:09 -0400174}
175
Chris Parsons944e7d02021-03-11 11:08:46 -0500176func (n noopBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500177 panic("unimplemented")
178}
179
Liz Kammerb71794d2021-04-09 14:07:00 -0400180func (n noopBazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500181 panic("unimplemented")
182}
183
Liz Kammer3f9e1552021-04-02 18:47:09 -0400184func (n noopBazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) {
185 panic("unimplemented")
186}
187
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400188func (n noopBazelContext) InvokeBazel() error {
189 panic("unimplemented")
190}
191
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500192func (m noopBazelContext) OutputBase() string {
193 return ""
194}
195
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400196func (n noopBazelContext) BazelEnabled() bool {
197 return false
198}
199
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500200func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
201 return []bazel.BuildStatement{}
202}
203
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400204func NewBazelContext(c *config) (BazelContext, error) {
Chris Parsons8b77a002020-10-27 18:59:25 -0400205 // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds"
206 // are production ready.
207 if c.Getenv("USE_BAZEL_ANALYSIS") != "1" {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400208 return noopBazelContext{}, nil
209 }
210
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400211 p, err := bazelPathsFromConfig(c)
212 if err != nil {
213 return nil, err
214 }
215 return &bazelContext{
216 bazelRunner: &builtinBazelRunner{},
217 paths: p,
218 requests: make(map[cqueryKey]bool),
219 }, nil
220}
221
222func bazelPathsFromConfig(c *config) (*bazelPaths, error) {
223 p := bazelPaths{
224 buildDir: c.buildDir,
225 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400226 missingEnvVars := []string{}
227 if len(c.Getenv("BAZEL_HOME")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400228 p.homeDir = c.Getenv("BAZEL_HOME")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400229 } else {
230 missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
231 }
232 if len(c.Getenv("BAZEL_PATH")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400233 p.bazelPath = c.Getenv("BAZEL_PATH")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400234 } else {
235 missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
236 }
237 if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400238 p.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400239 } else {
240 missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
241 }
242 if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400243 p.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400244 } else {
245 missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
246 }
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000247 if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400248 p.metricsDir = c.Getenv("BAZEL_METRICS_DIR")
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000249 } else {
250 missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR")
251 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400252 if len(missingEnvVars) > 0 {
253 return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
254 } else {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400255 return &p, nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400256 }
257}
258
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400259func (p *bazelPaths) BazelMetricsDir() string {
260 return p.metricsDir
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000261}
262
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400263func (context *bazelContext) BazelEnabled() bool {
264 return true
265}
266
267// Adds a cquery request to the Bazel request queue, to be later invoked, or
268// returns the result of the given request if the request was already made.
269// If the given request was already made (and the results are available), then
270// returns (result, true). If the request is queued but no results are available,
271// then returns ("", false).
Liz Kammerf29df7c2021-04-02 13:37:39 -0400272func (context *bazelContext) cquery(label string, requestType cqueryRequest,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500273 archType ArchType) (string, bool) {
274 key := cqueryKey{label, requestType, archType}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400275 if result, ok := context.results[key]; ok {
276 return result, true
277 } else {
278 context.requestMutex.Lock()
279 defer context.requestMutex.Unlock()
280 context.requests[key] = true
281 return "", false
282 }
283}
284
285func pwdPrefix() string {
286 // Darwin doesn't have /proc
287 if runtime.GOOS != "darwin" {
288 return "PWD=/proc/self/cwd"
289 }
290 return ""
291}
292
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400293type bazelCommand struct {
294 command string
295 // query or label
296 expression string
297}
298
299type mockBazelRunner struct {
300 bazelCommandResults map[bazelCommand]string
301 commands []bazelCommand
302}
303
304func (r *mockBazelRunner) issueBazelCommand(paths *bazelPaths,
305 runName bazel.RunName,
306 command bazelCommand,
307 extraFlags ...string) (string, string, error) {
308 r.commands = append(r.commands, command)
309 if ret, ok := r.bazelCommandResults[command]; ok {
310 return ret, "", nil
311 }
312 return "", "", nil
313}
314
315type builtinBazelRunner struct{}
316
Chris Parsons808d84c2021-03-09 20:43:32 -0500317// Issues the given bazel command with given build label and additional flags.
318// Returns (stdout, stderr, error). The first and second return values are strings
319// containing the stdout and stderr of the run command, and an error is returned if
320// the invocation returned an error code.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400321func (r *builtinBazelRunner) issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand,
Chris Parsons808d84c2021-03-09 20:43:32 -0500322 extraFlags ...string) (string, string, error) {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400323 cmdFlags := []string{"--output_base=" + paths.outputBase, command.command}
324 cmdFlags = append(cmdFlags, command.expression)
325 cmdFlags = append(cmdFlags, "--package_path=%workspace%/"+paths.intermediatesDir())
326 cmdFlags = append(cmdFlags, "--profile="+shared.BazelMetricsFilename(paths, runName))
Jingwen Chen91220d72021-03-24 02:18:33 -0400327
328 // Set default platforms to canonicalized values for mixed builds requests.
329 // If these are set in the bazelrc, they will have values that are
330 // non-canonicalized to @sourceroot labels, and thus be invalid when
331 // referenced from the buildroot.
332 //
333 // The actual platform values here may be overridden by configuration
334 // transitions from the buildroot.
Chris Parsonsee423b02021-02-08 23:04:59 -0500335 cmdFlags = append(cmdFlags,
Jingwen Chen91220d72021-03-24 02:18:33 -0400336 fmt.Sprintf("--platforms=%s", canonicalizeLabel("//build/bazel/platforms:android_x86_64")))
Chris Parsonsee423b02021-02-08 23:04:59 -0500337 cmdFlags = append(cmdFlags,
338 fmt.Sprintf("--extra_toolchains=%s", canonicalizeLabel("//prebuilts/clang/host/linux-x86:all")))
Jingwen Chen91220d72021-03-24 02:18:33 -0400339 // This should be parameterized on the host OS, but let's restrict to linux
340 // to keep things simple for now.
341 cmdFlags = append(cmdFlags,
342 fmt.Sprintf("--host_platform=%s", canonicalizeLabel("//build/bazel/platforms:linux_x86_64")))
343
Chris Parsons8d6e4332021-02-22 16:13:50 -0500344 // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network.
345 cmdFlags = append(cmdFlags, "--experimental_repository_disable_download")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400346 cmdFlags = append(cmdFlags, extraFlags...)
347
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400348 bazelCmd := exec.Command(paths.bazelPath, cmdFlags...)
349 bazelCmd.Dir = paths.workspaceDir
350 bazelCmd.Env = append(os.Environ(), "HOME="+paths.homeDir, pwdPrefix(),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500351 // Disables local host detection of gcc; toolchain information is defined
352 // explicitly in BUILD files.
353 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1")
Colin Crossff0278b2020-10-09 19:24:15 -0700354 stderr := &bytes.Buffer{}
355 bazelCmd.Stderr = stderr
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400356
357 if output, err := bazelCmd.Output(); err != nil {
Chris Parsons808d84c2021-03-09 20:43:32 -0500358 return "", string(stderr.Bytes()),
359 fmt.Errorf("bazel command failed. command: [%s], env: [%s], error [%s]", bazelCmd, bazelCmd.Env, stderr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400360 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500361 return string(output), string(stderr.Bytes()), nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400362 }
363}
364
Chris Parsons8ccdb632020-11-17 15:41:01 -0500365// Returns the string contents of a workspace file that should be output
366// adjacent to the main bzl file and build file.
367// This workspace file allows, via local_repository rule, sourcetree-level
368// BUILD targets to be referenced via @sourceroot.
369func (context *bazelContext) workspaceFileContents() []byte {
370 formatString := `
371# This file is generated by soong_build. Do not edit.
372local_repository(
373 name = "sourceroot",
Jingwen Chen63930982021-03-24 10:04:33 -0400374 path = "%[1]s",
Chris Parsons8ccdb632020-11-17 15:41:01 -0500375)
Liz Kammer8206d4f2021-03-03 16:40:52 -0500376
377local_repository(
378 name = "rules_cc",
Jingwen Chen63930982021-03-24 10:04:33 -0400379 path = "%[1]s/build/bazel/rules_cc",
380)
381
382local_repository(
383 name = "bazel_skylib",
384 path = "%[1]s/build/bazel/bazel_skylib",
Liz Kammer8206d4f2021-03-03 16:40:52 -0500385)
Chris Parsons8ccdb632020-11-17 15:41:01 -0500386`
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400387 return []byte(fmt.Sprintf(formatString, context.paths.workspaceDir))
Chris Parsons8ccdb632020-11-17 15:41:01 -0500388}
389
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400390func (context *bazelContext) mainBzlFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500391 // TODO(cparsons): Define configuration transitions programmatically based
392 // on available archs.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400393 contents := `
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500394#####################################################
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400395# This file is generated by soong_build. Do not edit.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500396#####################################################
397
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400398def _config_node_transition_impl(settings, attr):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500399 return {
Jingwen Chen91220d72021-03-24 02:18:33 -0400400 "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:android_%s" % attr.arch,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500401 }
402
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400403_config_node_transition = transition(
404 implementation = _config_node_transition_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500405 inputs = [],
406 outputs = [
407 "//command_line_option:platforms",
408 ],
409)
410
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400411def _passthrough_rule_impl(ctx):
412 return [DefaultInfo(files = depset(ctx.files.deps))]
413
414config_node = rule(
415 implementation = _passthrough_rule_impl,
416 attrs = {
417 "arch" : attr.string(mandatory = True),
418 "deps" : attr.label_list(cfg = _config_node_transition),
419 "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
420 },
Chris Parsons8d6e4332021-02-22 16:13:50 -0500421)
422
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400423
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500424# Rule representing the root of the build, to depend on all Bazel targets that
425# are required for the build. Building this target will build the entire Bazel
426# build tree.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400427mixed_build_root = rule(
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400428 implementation = _passthrough_rule_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500429 attrs = {
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400430 "deps" : attr.label_list(),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500431 },
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400432)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500433
434def _phony_root_impl(ctx):
435 return []
436
437# Rule to depend on other targets but build nothing.
438# This is useful as follows: building a target of this rule will generate
439# symlink forests for all dependencies of the target, without executing any
440# actions of the build.
441phony_root = rule(
442 implementation = _phony_root_impl,
443 attrs = {"deps" : attr.label_list()},
444)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400445`
446 return []byte(contents)
447}
448
Chris Parsons8ccdb632020-11-17 15:41:01 -0500449// Returns a "canonicalized" corresponding to the given sourcetree-level label.
450// This abstraction is required because a sourcetree label such as //foo/bar:baz
451// must be referenced via the local repository prefix, such as
452// @sourceroot//foo/bar:baz.
453func canonicalizeLabel(label string) string {
454 if strings.HasPrefix(label, "//") {
455 return "@sourceroot" + label
456 } else {
457 return "@sourceroot//" + label
458 }
459}
460
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400461func (context *bazelContext) mainBuildFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500462 // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
463 // architecture mapping.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400464 formatString := `
465# This file is generated by soong_build. Do not edit.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400466load(":main.bzl", "config_node", "mixed_build_root", "phony_root")
467
468%s
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400469
470mixed_build_root(name = "buildroot",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400471 deps = [%s],
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400472)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500473
474phony_root(name = "phonyroot",
475 deps = [":buildroot"],
476)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400477`
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400478 configNodeFormatString := `
479config_node(name = "%s",
480 arch = "%s",
481 deps = [%s],
482)
483`
484
485 configNodesSection := ""
486
487 labelsByArch := map[string][]string{}
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400488 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500489 labelString := fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label))
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400490 archString := getArchString(val)
491 labelsByArch[archString] = append(labelsByArch[archString], labelString)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400492 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400493
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400494 configNodeLabels := []string{}
495 for archString, labels := range labelsByArch {
496 configNodeLabels = append(configNodeLabels, fmt.Sprintf("\":%s\"", archString))
497 labelsString := strings.Join(labels, ",\n ")
498 configNodesSection += fmt.Sprintf(configNodeFormatString, archString, archString, labelsString)
499 }
500
501 return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(configNodeLabels, ",\n ")))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400502}
503
Chris Parsons944e7d02021-03-11 11:08:46 -0500504func indent(original string) string {
505 result := ""
506 for _, line := range strings.Split(original, "\n") {
507 result += " " + line + "\n"
508 }
509 return result
510}
511
Chris Parsons808d84c2021-03-09 20:43:32 -0500512// Returns the file contents of the buildroot.cquery file that should be used for the cquery
513// expression in order to obtain information about buildroot and its dependencies.
514// The contents of this file depend on the bazelContext's requests; requests are enumerated
515// and grouped by their request type. The data retrieved for each label depends on its
516// request type.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400517func (context *bazelContext) cqueryStarlarkFileContents() []byte {
Liz Kammerf29df7c2021-04-02 13:37:39 -0400518 requestTypeToCqueryIdEntries := map[cqueryRequest][]string{}
Chris Parsons944e7d02021-03-11 11:08:46 -0500519 for val, _ := range context.requests {
520 cqueryId := getCqueryId(val)
521 mapEntryString := fmt.Sprintf("%q : True", cqueryId)
522 requestTypeToCqueryIdEntries[val.requestType] =
523 append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString)
524 }
525 labelRegistrationMapSection := ""
526 functionDefSection := ""
527 mainSwitchSection := ""
528
529 mapDeclarationFormatString := `
530%s = {
531 %s
532}
533`
534 functionDefFormatString := `
535def %s(target):
536%s
537`
538 mainSwitchSectionFormatString := `
539 if id_string in %s:
540 return id_string + ">>" + %s(target)
541`
542
Liz Kammer66ffdb72021-04-02 13:26:07 -0400543 for requestType, _ := range requestTypeToCqueryIdEntries {
Chris Parsons944e7d02021-03-11 11:08:46 -0500544 labelMapName := requestType.Name() + "_Labels"
545 functionName := requestType.Name() + "_Fn"
546 labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString,
547 labelMapName,
548 strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n "))
549 functionDefSection += fmt.Sprintf(functionDefFormatString,
550 functionName,
551 indent(requestType.StarlarkFunctionBody()))
552 mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString,
553 labelMapName, functionName)
554 }
555
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400556 formatString := `
557# This file is generated by soong_build. Do not edit.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400558
Chris Parsons944e7d02021-03-11 11:08:46 -0500559# Label Map Section
560%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500561
Chris Parsons944e7d02021-03-11 11:08:46 -0500562# Function Def Section
563%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500564
565def get_arch(target):
566 buildoptions = build_options(target)
567 platforms = build_options(target)["//command_line_option:platforms"]
568 if len(platforms) != 1:
569 # An individual configured target should have only one platform architecture.
570 # Note that it's fine for there to be multiple architectures for the same label,
571 # but each is its own configured target.
572 fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
573 platform_name = build_options(target)["//command_line_option:platforms"][0].name
574 if platform_name == "host":
575 return "HOST"
Jingwen Chen91220d72021-03-24 02:18:33 -0400576 elif not platform_name.startswith("android_"):
577 fail("expected platform name of the form 'android_<arch>', but was " + str(platforms))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500578 return "UNKNOWN"
Jingwen Chen91220d72021-03-24 02:18:33 -0400579 return platform_name[len("android_"):]
Chris Parsons8d6e4332021-02-22 16:13:50 -0500580
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400581def format(target):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500582 id_string = str(target.label) + "|" + get_arch(target)
Chris Parsons944e7d02021-03-11 11:08:46 -0500583
584 # Main switch section
585 %s
586 # This target was not requested via cquery, and thus must be a dependency
587 # of a requested target.
588 return id_string + ">>NONE"
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400589`
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400590
Chris Parsons944e7d02021-03-11 11:08:46 -0500591 return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection,
592 mainSwitchSection))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400593}
594
Chris Parsons8ccdb632020-11-17 15:41:01 -0500595// Returns a workspace-relative path containing build-related metadata required
596// for interfacing with Bazel. Example: out/soong/bazel.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400597func (p *bazelPaths) intermediatesDir() string {
598 return filepath.Join(p.buildDir, "bazel")
Chris Parsons8ccdb632020-11-17 15:41:01 -0500599}
600
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400601// Issues commands to Bazel to receive results for all cquery requests
602// queued in the BazelContext.
603func (context *bazelContext) InvokeBazel() error {
604 context.results = make(map[cqueryKey]string)
605
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400606 var cqueryOutput string
Chris Parsons808d84c2021-03-09 20:43:32 -0500607 var cqueryErr string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400608 var err error
Chris Parsons8ccdb632020-11-17 15:41:01 -0500609
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400610 intermediatesDirPath := absolutePath(context.paths.intermediatesDir())
Chris Parsons07c1e4a2021-01-19 17:19:16 -0500611 if _, err := os.Stat(intermediatesDirPath); os.IsNotExist(err) {
612 err = os.Mkdir(intermediatesDirPath, 0777)
613 }
614
Chris Parsons8ccdb632020-11-17 15:41:01 -0500615 if err != nil {
616 return err
617 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400618 err = ioutil.WriteFile(
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400619 filepath.Join(intermediatesDirPath, "main.bzl"),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400620 context.mainBzlFileContents(), 0666)
621 if err != nil {
622 return err
623 }
624 err = ioutil.WriteFile(
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400625 filepath.Join(intermediatesDirPath, "BUILD.bazel"),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400626 context.mainBuildFileContents(), 0666)
627 if err != nil {
628 return err
629 }
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400630 cqueryFileRelpath := filepath.Join(context.paths.intermediatesDir(), "buildroot.cquery")
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400631 err = ioutil.WriteFile(
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800632 absolutePath(cqueryFileRelpath),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400633 context.cqueryStarlarkFileContents(), 0666)
634 if err != nil {
635 return err
636 }
Chris Parsons8ccdb632020-11-17 15:41:01 -0500637 err = ioutil.WriteFile(
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400638 filepath.Join(intermediatesDirPath, "WORKSPACE.bazel"),
Chris Parsons8ccdb632020-11-17 15:41:01 -0500639 context.workspaceFileContents(), 0666)
640 if err != nil {
641 return err
642 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800643 buildrootLabel := "//:buildroot"
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400644 cqueryOutput, cqueryErr, err = context.issueBazelCommand(
645 context.paths,
646 bazel.CqueryBuildRootRunName,
647 bazelCommand{"cquery", fmt.Sprintf("kind(rule, deps(%s))", buildrootLabel)},
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400648 "--output=starlark",
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800649 "--starlark:file="+cqueryFileRelpath)
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400650 err = ioutil.WriteFile(filepath.Join(intermediatesDirPath, "cquery.out"),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500651 []byte(cqueryOutput), 0666)
652 if err != nil {
653 return err
654 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400655
656 if err != nil {
657 return err
658 }
659
660 cqueryResults := map[string]string{}
661 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
662 if strings.Contains(outputLine, ">>") {
663 splitLine := strings.SplitN(outputLine, ">>", 2)
664 cqueryResults[splitLine[0]] = splitLine[1]
665 }
666 }
667
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400668 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500669 if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400670 context.results[val] = string(cqueryResult)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400671 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500672 return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]",
673 getCqueryId(val), cqueryOutput, cqueryErr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400674 }
675 }
676
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500677 // Issue an aquery command to retrieve action information about the bazel build tree.
678 //
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400679 // TODO(cparsons): Use --target_pattern_file to avoid command line limits.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500680 var aqueryOutput string
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400681 aqueryOutput, _, err = context.issueBazelCommand(
682 context.paths,
683 bazel.AqueryBuildRootRunName,
684 bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)},
685 // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
686 // proto sources, which would add a number of unnecessary dependencies.
687 "--output=jsonproto")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400688
689 if err != nil {
690 return err
691 }
692
Chris Parsons4f069892021-01-15 12:22:41 -0500693 context.buildStatements, err = bazel.AqueryBuildStatements([]byte(aqueryOutput))
694 if err != nil {
695 return err
696 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500697
698 // Issue a build command of the phony root to generate symlink forests for dependencies of the
699 // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
700 // but some of symlinks may be required to resolve source dependencies of the build.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400701 _, _, err = context.issueBazelCommand(
702 context.paths,
703 bazel.BazelBuildPhonyRootRunName,
704 bazelCommand{"build", "//:phonyroot"})
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500705
706 if err != nil {
707 return err
708 }
709
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400710 // Clear requests.
711 context.requests = map[cqueryKey]bool{}
712 return nil
713}
Chris Parsonsa798d962020-10-12 23:44:08 -0400714
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500715func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
716 return context.buildStatements
717}
718
719func (context *bazelContext) OutputBase() string {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400720 return context.paths.outputBase
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500721}
722
Chris Parsonsa798d962020-10-12 23:44:08 -0400723// Singleton used for registering BUILD file ninja dependencies (needed
724// for correctness of builds which use Bazel.
725func BazelSingleton() Singleton {
726 return &bazelSingleton{}
727}
728
729type bazelSingleton struct{}
730
731func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500732 // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
733 if !ctx.Config().BazelContext.BazelEnabled() {
734 return
735 }
Chris Parsonsa798d962020-10-12 23:44:08 -0400736
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500737 // Add ninja file dependencies for files which all bazel invocations require.
738 bazelBuildList := absolutePath(filepath.Join(
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100739 filepath.Dir(bootstrap.CmdlineModuleListFile()), "bazel.list"))
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500740 ctx.AddNinjaFileDeps(bazelBuildList)
741
742 data, err := ioutil.ReadFile(bazelBuildList)
743 if err != nil {
744 ctx.Errorf(err.Error())
745 }
746 files := strings.Split(strings.TrimSpace(string(data)), "\n")
747 for _, file := range files {
748 ctx.AddNinjaFileDeps(file)
749 }
750
751 // Register bazel-owned build statements (obtained from the aquery invocation).
752 for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500753 if len(buildStatement.Command) < 1 {
Rupert Shuttlewortha29903f2021-04-06 16:17:33 +0000754 panic(fmt.Sprintf("unhandled build statement: %v", buildStatement))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500755 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500756 rule := NewRuleBuilder(pctx, ctx)
757 cmd := rule.Command()
758 cmd.Text(fmt.Sprintf("cd %s/execroot/__main__ && %s",
759 ctx.Config().BazelContext.OutputBase(), buildStatement.Command))
760
761 for _, outputPath := range buildStatement.OutputPaths {
762 cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400763 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500764 for _, inputPath := range buildStatement.InputPaths {
765 cmd.Implicit(PathForBazelOut(ctx, inputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400766 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500767
Liz Kammerde116852021-03-25 16:42:37 -0400768 if depfile := buildStatement.Depfile; depfile != nil {
769 cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile))
770 }
771
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500772 // This is required to silence warnings pertaining to unexpected timestamps. Particularly,
773 // some Bazel builtins (such as files in the bazel_tools directory) have far-future
774 // timestamps. Without restat, Ninja would emit warnings that the input files of a
775 // build statement have later timestamps than the outputs.
776 rule.Restat()
777
Liz Kammer13548d72020-12-16 11:13:30 -0800778 rule.Build(fmt.Sprintf("bazel %d", index), buildStatement.Mnemonic)
Chris Parsonsa798d962020-10-12 23:44:08 -0400779 }
780}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500781
782func getCqueryId(key cqueryKey) string {
783 return canonicalizeLabel(key.label) + "|" + getArchString(key)
784}
785
786func getArchString(key cqueryKey) string {
787 arch := key.archType.Name
788 if len(arch) > 0 {
789 return arch
790 } else {
791 return "x86_64"
792 }
793}