blob: 5ac69240a241b324e6d4b759dffc2d2424fcb82d [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
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
88// A context object which tracks queued requests that need to be made to Bazel,
89// and their results after the requests have been made.
90type bazelContext struct {
91 homeDir string
92 bazelPath string
93 outputBase string
94 workspaceDir string
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040095 buildDir string
Patrice Arruda05ab2d02020-12-12 06:24:26 +000096 metricsDir string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040097
98 requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
99 requestMutex sync.Mutex // requests can be written in parallel
100
101 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500102
103 // Build statements which should get registered to reflect Bazel's outputs.
104 buildStatements []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400105}
106
107var _ BazelContext = &bazelContext{}
108
109// A bazel context to use when Bazel is disabled.
110type noopBazelContext struct{}
111
112var _ BazelContext = noopBazelContext{}
113
114// A bazel context to use for tests.
115type MockBazelContext struct {
116 AllFiles map[string][]string
117}
118
Chris Parsons944e7d02021-03-11 11:08:46 -0500119func (m MockBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500120 result, ok := m.AllFiles[label]
121 return result, ok
122}
123
Chris Parsons944e7d02021-03-11 11:08:46 -0500124func (m MockBazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500125 result, ok := m.AllFiles[label]
126 return result, result, ok
127}
128
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400129func (m MockBazelContext) InvokeBazel() error {
130 panic("unimplemented")
131}
132
133func (m MockBazelContext) BazelEnabled() bool {
134 return true
135}
136
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500137func (m MockBazelContext) OutputBase() string {
138 return "outputbase"
139}
140
141func (m MockBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
142 return []bazel.BuildStatement{}
143}
144
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400145var _ BazelContext = MockBazelContext{}
146
Chris Parsons944e7d02021-03-11 11:08:46 -0500147func (bazelCtx *bazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
148 rawString, ok := bazelCtx.cquery(label, cquery.GetOutputFiles, archType)
149 var ret []string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400150 if ok {
Chris Parsons944e7d02021-03-11 11:08:46 -0500151 bazelOutput := strings.TrimSpace(rawString)
Liz Kammerf29df7c2021-04-02 13:37:39 -0400152 ret = cquery.GetOutputFiles.ParseResult(bazelOutput)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400153 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500154 return ret, ok
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400155}
156
Chris Parsons944e7d02021-03-11 11:08:46 -0500157func (bazelCtx *bazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
158 var outputFiles []string
Chris Parsons808d84c2021-03-09 20:43:32 -0500159 var ccObjects []string
160
Chris Parsons944e7d02021-03-11 11:08:46 -0500161 result, ok := bazelCtx.cquery(label, cquery.GetOutputFilesAndCcObjectFiles, archType)
Chris Parsons808d84c2021-03-09 20:43:32 -0500162 if ok {
163 bazelOutput := strings.TrimSpace(result)
Liz Kammerf29df7c2021-04-02 13:37:39 -0400164 returnResult := cquery.GetOutputFilesAndCcObjectFiles.ParseResult(bazelOutput)
Chris Parsons944e7d02021-03-11 11:08:46 -0500165 outputFiles = returnResult.OutputFiles
166 ccObjects = returnResult.CcObjectFiles
Chris Parsons808d84c2021-03-09 20:43:32 -0500167 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500168
169 return outputFiles, ccObjects, ok
Chris Parsons808d84c2021-03-09 20:43:32 -0500170}
171
Chris Parsons944e7d02021-03-11 11:08:46 -0500172func (n noopBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500173 panic("unimplemented")
174}
175
Chris Parsons944e7d02021-03-11 11:08:46 -0500176func (n noopBazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500177 panic("unimplemented")
178}
179
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400180func (n noopBazelContext) InvokeBazel() error {
181 panic("unimplemented")
182}
183
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500184func (m noopBazelContext) OutputBase() string {
185 return ""
186}
187
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400188func (n noopBazelContext) BazelEnabled() bool {
189 return false
190}
191
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500192func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
193 return []bazel.BuildStatement{}
194}
195
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400196func NewBazelContext(c *config) (BazelContext, error) {
Chris Parsons8b77a002020-10-27 18:59:25 -0400197 // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds"
198 // are production ready.
199 if c.Getenv("USE_BAZEL_ANALYSIS") != "1" {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400200 return noopBazelContext{}, nil
201 }
202
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400203 bazelCtx := bazelContext{buildDir: c.buildDir, requests: make(map[cqueryKey]bool)}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400204 missingEnvVars := []string{}
205 if len(c.Getenv("BAZEL_HOME")) > 1 {
206 bazelCtx.homeDir = c.Getenv("BAZEL_HOME")
207 } else {
208 missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
209 }
210 if len(c.Getenv("BAZEL_PATH")) > 1 {
211 bazelCtx.bazelPath = c.Getenv("BAZEL_PATH")
212 } else {
213 missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
214 }
215 if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
216 bazelCtx.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
217 } else {
218 missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
219 }
220 if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
221 bazelCtx.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
222 } else {
223 missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
224 }
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000225 if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 {
226 bazelCtx.metricsDir = c.Getenv("BAZEL_METRICS_DIR")
227 } else {
228 missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR")
229 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400230 if len(missingEnvVars) > 0 {
231 return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
232 } else {
233 return &bazelCtx, nil
234 }
235}
236
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000237func (context *bazelContext) BazelMetricsDir() string {
238 return context.metricsDir
239}
240
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400241func (context *bazelContext) BazelEnabled() bool {
242 return true
243}
244
245// Adds a cquery request to the Bazel request queue, to be later invoked, or
246// returns the result of the given request if the request was already made.
247// If the given request was already made (and the results are available), then
248// returns (result, true). If the request is queued but no results are available,
249// then returns ("", false).
Liz Kammerf29df7c2021-04-02 13:37:39 -0400250func (context *bazelContext) cquery(label string, requestType cqueryRequest,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500251 archType ArchType) (string, bool) {
252 key := cqueryKey{label, requestType, archType}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400253 if result, ok := context.results[key]; ok {
254 return result, true
255 } else {
256 context.requestMutex.Lock()
257 defer context.requestMutex.Unlock()
258 context.requests[key] = true
259 return "", false
260 }
261}
262
263func pwdPrefix() string {
264 // Darwin doesn't have /proc
265 if runtime.GOOS != "darwin" {
266 return "PWD=/proc/self/cwd"
267 }
268 return ""
269}
270
Chris Parsons808d84c2021-03-09 20:43:32 -0500271// Issues the given bazel command with given build label and additional flags.
272// Returns (stdout, stderr, error). The first and second return values are strings
273// containing the stdout and stderr of the run command, and an error is returned if
274// the invocation returned an error code.
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000275func (context *bazelContext) issueBazelCommand(runName bazel.RunName, command string, labels []string,
Chris Parsons808d84c2021-03-09 20:43:32 -0500276 extraFlags ...string) (string, string, error) {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400277
Jingwen Chen7c6089a2020-11-02 02:56:20 -0500278 cmdFlags := []string{"--output_base=" + context.outputBase, command}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400279 cmdFlags = append(cmdFlags, labels...)
Chris Parsons8ccdb632020-11-17 15:41:01 -0500280 cmdFlags = append(cmdFlags, "--package_path=%workspace%/"+context.intermediatesDir())
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000281 cmdFlags = append(cmdFlags, "--profile="+shared.BazelMetricsFilename(context, runName))
Jingwen Chen91220d72021-03-24 02:18:33 -0400282
283 // Set default platforms to canonicalized values for mixed builds requests.
284 // If these are set in the bazelrc, they will have values that are
285 // non-canonicalized to @sourceroot labels, and thus be invalid when
286 // referenced from the buildroot.
287 //
288 // The actual platform values here may be overridden by configuration
289 // transitions from the buildroot.
Chris Parsonsee423b02021-02-08 23:04:59 -0500290 cmdFlags = append(cmdFlags,
Jingwen Chen91220d72021-03-24 02:18:33 -0400291 fmt.Sprintf("--platforms=%s", canonicalizeLabel("//build/bazel/platforms:android_x86_64")))
Chris Parsonsee423b02021-02-08 23:04:59 -0500292 cmdFlags = append(cmdFlags,
293 fmt.Sprintf("--extra_toolchains=%s", canonicalizeLabel("//prebuilts/clang/host/linux-x86:all")))
Jingwen Chen91220d72021-03-24 02:18:33 -0400294 // This should be parameterized on the host OS, but let's restrict to linux
295 // to keep things simple for now.
296 cmdFlags = append(cmdFlags,
297 fmt.Sprintf("--host_platform=%s", canonicalizeLabel("//build/bazel/platforms:linux_x86_64")))
298
Chris Parsons8d6e4332021-02-22 16:13:50 -0500299 // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network.
300 cmdFlags = append(cmdFlags, "--experimental_repository_disable_download")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400301 cmdFlags = append(cmdFlags, extraFlags...)
302
303 bazelCmd := exec.Command(context.bazelPath, cmdFlags...)
304 bazelCmd.Dir = context.workspaceDir
Chris Parsons8d6e4332021-02-22 16:13:50 -0500305 bazelCmd.Env = append(os.Environ(), "HOME="+context.homeDir, pwdPrefix(),
306 // Disables local host detection of gcc; toolchain information is defined
307 // explicitly in BUILD files.
308 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1")
Colin Crossff0278b2020-10-09 19:24:15 -0700309 stderr := &bytes.Buffer{}
310 bazelCmd.Stderr = stderr
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400311
312 if output, err := bazelCmd.Output(); err != nil {
Chris Parsons808d84c2021-03-09 20:43:32 -0500313 return "", string(stderr.Bytes()),
314 fmt.Errorf("bazel command failed. command: [%s], env: [%s], error [%s]", bazelCmd, bazelCmd.Env, stderr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400315 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500316 return string(output), string(stderr.Bytes()), nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400317 }
318}
319
Chris Parsons8ccdb632020-11-17 15:41:01 -0500320// Returns the string contents of a workspace file that should be output
321// adjacent to the main bzl file and build file.
322// This workspace file allows, via local_repository rule, sourcetree-level
323// BUILD targets to be referenced via @sourceroot.
324func (context *bazelContext) workspaceFileContents() []byte {
325 formatString := `
326# This file is generated by soong_build. Do not edit.
327local_repository(
328 name = "sourceroot",
329 path = "%s",
330)
Liz Kammer8206d4f2021-03-03 16:40:52 -0500331
332local_repository(
333 name = "rules_cc",
334 path = "%s/build/bazel/rules_cc",
335)
Chris Parsons8ccdb632020-11-17 15:41:01 -0500336`
Liz Kammer8206d4f2021-03-03 16:40:52 -0500337 return []byte(fmt.Sprintf(formatString, context.workspaceDir, context.workspaceDir))
Chris Parsons8ccdb632020-11-17 15:41:01 -0500338}
339
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400340func (context *bazelContext) mainBzlFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500341 // TODO(cparsons): Define configuration transitions programmatically based
342 // on available archs.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400343 contents := `
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500344#####################################################
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400345# This file is generated by soong_build. Do not edit.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500346#####################################################
347
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400348def _config_node_transition_impl(settings, attr):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500349 return {
Jingwen Chen91220d72021-03-24 02:18:33 -0400350 "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:android_%s" % attr.arch,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500351 }
352
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400353_config_node_transition = transition(
354 implementation = _config_node_transition_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500355 inputs = [],
356 outputs = [
357 "//command_line_option:platforms",
358 ],
359)
360
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400361def _passthrough_rule_impl(ctx):
362 return [DefaultInfo(files = depset(ctx.files.deps))]
363
364config_node = rule(
365 implementation = _passthrough_rule_impl,
366 attrs = {
367 "arch" : attr.string(mandatory = True),
368 "deps" : attr.label_list(cfg = _config_node_transition),
369 "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
370 },
Chris Parsons8d6e4332021-02-22 16:13:50 -0500371)
372
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400373
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500374# Rule representing the root of the build, to depend on all Bazel targets that
375# are required for the build. Building this target will build the entire Bazel
376# build tree.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400377mixed_build_root = rule(
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400378 implementation = _passthrough_rule_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500379 attrs = {
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400380 "deps" : attr.label_list(),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500381 },
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400382)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500383
384def _phony_root_impl(ctx):
385 return []
386
387# Rule to depend on other targets but build nothing.
388# This is useful as follows: building a target of this rule will generate
389# symlink forests for all dependencies of the target, without executing any
390# actions of the build.
391phony_root = rule(
392 implementation = _phony_root_impl,
393 attrs = {"deps" : attr.label_list()},
394)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400395`
396 return []byte(contents)
397}
398
Chris Parsons8ccdb632020-11-17 15:41:01 -0500399// Returns a "canonicalized" corresponding to the given sourcetree-level label.
400// This abstraction is required because a sourcetree label such as //foo/bar:baz
401// must be referenced via the local repository prefix, such as
402// @sourceroot//foo/bar:baz.
403func canonicalizeLabel(label string) string {
404 if strings.HasPrefix(label, "//") {
405 return "@sourceroot" + label
406 } else {
407 return "@sourceroot//" + label
408 }
409}
410
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400411func (context *bazelContext) mainBuildFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500412 // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
413 // architecture mapping.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400414 formatString := `
415# This file is generated by soong_build. Do not edit.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400416load(":main.bzl", "config_node", "mixed_build_root", "phony_root")
417
418%s
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400419
420mixed_build_root(name = "buildroot",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400421 deps = [%s],
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400422)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500423
424phony_root(name = "phonyroot",
425 deps = [":buildroot"],
426)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400427`
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400428 configNodeFormatString := `
429config_node(name = "%s",
430 arch = "%s",
431 deps = [%s],
432)
433`
434
435 configNodesSection := ""
436
437 labelsByArch := map[string][]string{}
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400438 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500439 labelString := fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label))
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400440 archString := getArchString(val)
441 labelsByArch[archString] = append(labelsByArch[archString], labelString)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400442 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400443
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400444 configNodeLabels := []string{}
445 for archString, labels := range labelsByArch {
446 configNodeLabels = append(configNodeLabels, fmt.Sprintf("\":%s\"", archString))
447 labelsString := strings.Join(labels, ",\n ")
448 configNodesSection += fmt.Sprintf(configNodeFormatString, archString, archString, labelsString)
449 }
450
451 return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(configNodeLabels, ",\n ")))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400452}
453
Chris Parsons944e7d02021-03-11 11:08:46 -0500454func indent(original string) string {
455 result := ""
456 for _, line := range strings.Split(original, "\n") {
457 result += " " + line + "\n"
458 }
459 return result
460}
461
Chris Parsons808d84c2021-03-09 20:43:32 -0500462// Returns the file contents of the buildroot.cquery file that should be used for the cquery
463// expression in order to obtain information about buildroot and its dependencies.
464// The contents of this file depend on the bazelContext's requests; requests are enumerated
465// and grouped by their request type. The data retrieved for each label depends on its
466// request type.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400467func (context *bazelContext) cqueryStarlarkFileContents() []byte {
Liz Kammerf29df7c2021-04-02 13:37:39 -0400468 requestTypeToCqueryIdEntries := map[cqueryRequest][]string{}
Chris Parsons944e7d02021-03-11 11:08:46 -0500469 for val, _ := range context.requests {
470 cqueryId := getCqueryId(val)
471 mapEntryString := fmt.Sprintf("%q : True", cqueryId)
472 requestTypeToCqueryIdEntries[val.requestType] =
473 append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString)
474 }
475 labelRegistrationMapSection := ""
476 functionDefSection := ""
477 mainSwitchSection := ""
478
479 mapDeclarationFormatString := `
480%s = {
481 %s
482}
483`
484 functionDefFormatString := `
485def %s(target):
486%s
487`
488 mainSwitchSectionFormatString := `
489 if id_string in %s:
490 return id_string + ">>" + %s(target)
491`
492
Liz Kammer66ffdb72021-04-02 13:26:07 -0400493 for requestType, _ := range requestTypeToCqueryIdEntries {
Chris Parsons944e7d02021-03-11 11:08:46 -0500494 labelMapName := requestType.Name() + "_Labels"
495 functionName := requestType.Name() + "_Fn"
496 labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString,
497 labelMapName,
498 strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n "))
499 functionDefSection += fmt.Sprintf(functionDefFormatString,
500 functionName,
501 indent(requestType.StarlarkFunctionBody()))
502 mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString,
503 labelMapName, functionName)
504 }
505
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400506 formatString := `
507# This file is generated by soong_build. Do not edit.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400508
Chris Parsons944e7d02021-03-11 11:08:46 -0500509# Label Map Section
510%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500511
Chris Parsons944e7d02021-03-11 11:08:46 -0500512# Function Def Section
513%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500514
515def get_arch(target):
516 buildoptions = build_options(target)
517 platforms = build_options(target)["//command_line_option:platforms"]
518 if len(platforms) != 1:
519 # An individual configured target should have only one platform architecture.
520 # Note that it's fine for there to be multiple architectures for the same label,
521 # but each is its own configured target.
522 fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
523 platform_name = build_options(target)["//command_line_option:platforms"][0].name
524 if platform_name == "host":
525 return "HOST"
Jingwen Chen91220d72021-03-24 02:18:33 -0400526 elif not platform_name.startswith("android_"):
527 fail("expected platform name of the form 'android_<arch>', but was " + str(platforms))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500528 return "UNKNOWN"
Jingwen Chen91220d72021-03-24 02:18:33 -0400529 return platform_name[len("android_"):]
Chris Parsons8d6e4332021-02-22 16:13:50 -0500530
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400531def format(target):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500532 id_string = str(target.label) + "|" + get_arch(target)
Chris Parsons944e7d02021-03-11 11:08:46 -0500533
534 # Main switch section
535 %s
536 # This target was not requested via cquery, and thus must be a dependency
537 # of a requested target.
538 return id_string + ">>NONE"
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400539`
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400540
Chris Parsons944e7d02021-03-11 11:08:46 -0500541 return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection,
542 mainSwitchSection))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400543}
544
Chris Parsons8ccdb632020-11-17 15:41:01 -0500545// Returns a workspace-relative path containing build-related metadata required
546// for interfacing with Bazel. Example: out/soong/bazel.
547func (context *bazelContext) intermediatesDir() string {
548 return filepath.Join(context.buildDir, "bazel")
549}
550
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400551// Issues commands to Bazel to receive results for all cquery requests
552// queued in the BazelContext.
553func (context *bazelContext) InvokeBazel() error {
554 context.results = make(map[cqueryKey]string)
555
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400556 var cqueryOutput string
Chris Parsons808d84c2021-03-09 20:43:32 -0500557 var cqueryErr string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400558 var err error
Chris Parsons8ccdb632020-11-17 15:41:01 -0500559
Chris Parsons07c1e4a2021-01-19 17:19:16 -0500560 intermediatesDirPath := absolutePath(context.intermediatesDir())
561 if _, err := os.Stat(intermediatesDirPath); os.IsNotExist(err) {
562 err = os.Mkdir(intermediatesDirPath, 0777)
563 }
564
Chris Parsons8ccdb632020-11-17 15:41:01 -0500565 if err != nil {
566 return err
567 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400568 err = ioutil.WriteFile(
Chris Parsons8ccdb632020-11-17 15:41:01 -0500569 absolutePath(filepath.Join(context.intermediatesDir(), "main.bzl")),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400570 context.mainBzlFileContents(), 0666)
571 if err != nil {
572 return err
573 }
574 err = ioutil.WriteFile(
Chris Parsons8ccdb632020-11-17 15:41:01 -0500575 absolutePath(filepath.Join(context.intermediatesDir(), "BUILD.bazel")),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400576 context.mainBuildFileContents(), 0666)
577 if err != nil {
578 return err
579 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800580 cqueryFileRelpath := filepath.Join(context.intermediatesDir(), "buildroot.cquery")
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400581 err = ioutil.WriteFile(
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800582 absolutePath(cqueryFileRelpath),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400583 context.cqueryStarlarkFileContents(), 0666)
584 if err != nil {
585 return err
586 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800587 workspaceFileRelpath := filepath.Join(context.intermediatesDir(), "WORKSPACE.bazel")
Chris Parsons8ccdb632020-11-17 15:41:01 -0500588 err = ioutil.WriteFile(
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800589 absolutePath(workspaceFileRelpath),
Chris Parsons8ccdb632020-11-17 15:41:01 -0500590 context.workspaceFileContents(), 0666)
591 if err != nil {
592 return err
593 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800594 buildrootLabel := "//:buildroot"
Chris Parsons808d84c2021-03-09 20:43:32 -0500595 cqueryOutput, cqueryErr, err = context.issueBazelCommand(bazel.CqueryBuildRootRunName, "cquery",
Chris Parsons8d6e4332021-02-22 16:13:50 -0500596 []string{fmt.Sprintf("kind(rule, deps(%s))", buildrootLabel)},
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400597 "--output=starlark",
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800598 "--starlark:file="+cqueryFileRelpath)
Chris Parsons8d6e4332021-02-22 16:13:50 -0500599 err = ioutil.WriteFile(
600 absolutePath(filepath.Join(context.intermediatesDir(), "cquery.out")),
601 []byte(cqueryOutput), 0666)
602 if err != nil {
603 return err
604 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400605
606 if err != nil {
607 return err
608 }
609
610 cqueryResults := map[string]string{}
611 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
612 if strings.Contains(outputLine, ">>") {
613 splitLine := strings.SplitN(outputLine, ">>", 2)
614 cqueryResults[splitLine[0]] = splitLine[1]
615 }
616 }
617
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400618 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500619 if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400620 context.results[val] = string(cqueryResult)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400621 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500622 return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]",
623 getCqueryId(val), cqueryOutput, cqueryErr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400624 }
625 }
626
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500627 // Issue an aquery command to retrieve action information about the bazel build tree.
628 //
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400629 // TODO(cparsons): Use --target_pattern_file to avoid command line limits.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500630 var aqueryOutput string
Chris Parsons808d84c2021-03-09 20:43:32 -0500631 aqueryOutput, _, err = context.issueBazelCommand(bazel.AqueryBuildRootRunName, "aquery",
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800632 []string{fmt.Sprintf("deps(%s)", buildrootLabel),
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500633 // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
634 // proto sources, which would add a number of unnecessary dependencies.
635 "--output=jsonproto"})
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400636
637 if err != nil {
638 return err
639 }
640
Chris Parsons4f069892021-01-15 12:22:41 -0500641 context.buildStatements, err = bazel.AqueryBuildStatements([]byte(aqueryOutput))
642 if err != nil {
643 return err
644 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500645
646 // Issue a build command of the phony root to generate symlink forests for dependencies of the
647 // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
648 // but some of symlinks may be required to resolve source dependencies of the build.
Chris Parsons808d84c2021-03-09 20:43:32 -0500649 _, _, err = context.issueBazelCommand(bazel.BazelBuildPhonyRootRunName, "build",
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500650 []string{"//:phonyroot"})
651
652 if err != nil {
653 return err
654 }
655
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400656 // Clear requests.
657 context.requests = map[cqueryKey]bool{}
658 return nil
659}
Chris Parsonsa798d962020-10-12 23:44:08 -0400660
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500661func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
662 return context.buildStatements
663}
664
665func (context *bazelContext) OutputBase() string {
666 return context.outputBase
667}
668
Chris Parsonsa798d962020-10-12 23:44:08 -0400669// Singleton used for registering BUILD file ninja dependencies (needed
670// for correctness of builds which use Bazel.
671func BazelSingleton() Singleton {
672 return &bazelSingleton{}
673}
674
675type bazelSingleton struct{}
676
677func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500678 // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
679 if !ctx.Config().BazelContext.BazelEnabled() {
680 return
681 }
Chris Parsonsa798d962020-10-12 23:44:08 -0400682
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500683 // Add ninja file dependencies for files which all bazel invocations require.
684 bazelBuildList := absolutePath(filepath.Join(
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100685 filepath.Dir(bootstrap.CmdlineModuleListFile()), "bazel.list"))
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500686 ctx.AddNinjaFileDeps(bazelBuildList)
687
688 data, err := ioutil.ReadFile(bazelBuildList)
689 if err != nil {
690 ctx.Errorf(err.Error())
691 }
692 files := strings.Split(strings.TrimSpace(string(data)), "\n")
693 for _, file := range files {
694 ctx.AddNinjaFileDeps(file)
695 }
696
697 // Register bazel-owned build statements (obtained from the aquery invocation).
698 for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500699 if len(buildStatement.Command) < 1 {
Rupert Shuttlewortha29903f2021-04-06 16:17:33 +0000700 panic(fmt.Sprintf("unhandled build statement: %v", buildStatement))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500701 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500702 rule := NewRuleBuilder(pctx, ctx)
703 cmd := rule.Command()
704 cmd.Text(fmt.Sprintf("cd %s/execroot/__main__ && %s",
705 ctx.Config().BazelContext.OutputBase(), buildStatement.Command))
706
707 for _, outputPath := range buildStatement.OutputPaths {
708 cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400709 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500710 for _, inputPath := range buildStatement.InputPaths {
711 cmd.Implicit(PathForBazelOut(ctx, inputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400712 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500713
Liz Kammerde116852021-03-25 16:42:37 -0400714 if depfile := buildStatement.Depfile; depfile != nil {
715 cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile))
716 }
717
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500718 // This is required to silence warnings pertaining to unexpected timestamps. Particularly,
719 // some Bazel builtins (such as files in the bazel_tools directory) have far-future
720 // timestamps. Without restat, Ninja would emit warnings that the input files of a
721 // build statement have later timestamps than the outputs.
722 rule.Restat()
723
Liz Kammer13548d72020-12-16 11:13:30 -0800724 rule.Build(fmt.Sprintf("bazel %d", index), buildStatement.Mnemonic)
Chris Parsonsa798d962020-10-12 23:44:08 -0400725 }
726}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500727
728func getCqueryId(key cqueryKey) string {
729 return canonicalizeLabel(key.label) + "|" + getArchString(key)
730}
731
732func getArchString(key cqueryKey) string {
733 arch := key.archType.Name
734 if len(arch) > 0 {
735 return arch
736 } else {
737 return "x86_64"
738 }
739}