blob: 97eec3006962ef3f8a4d50fbb6fc1bfca3570e42 [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
92// A context object which tracks queued requests that need to be made to Bazel,
93// and their results after the requests have been made.
94type bazelContext struct {
95 homeDir string
96 bazelPath string
97 outputBase string
98 workspaceDir string
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040099 buildDir string
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000100 metricsDir string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400101
102 requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
103 requestMutex sync.Mutex // requests can be written in parallel
104
105 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500106
107 // Build statements which should get registered to reflect Bazel's outputs.
108 buildStatements []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400109}
110
111var _ BazelContext = &bazelContext{}
112
113// A bazel context to use when Bazel is disabled.
114type noopBazelContext struct{}
115
116var _ BazelContext = noopBazelContext{}
117
118// A bazel context to use for tests.
119type MockBazelContext struct {
Liz Kammera92e8442021-04-07 20:25:21 -0400120 OutputBaseDir string
121
122 LabelToOutputFiles map[string][]string
123 LabelToOutputFilesAndCcObjectFiles map[string]cquery.GetOutputFilesAndCcObjectFiles_Result
124 LabelToCcStaticLibraryFiles map[string][]string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400125}
126
Chris Parsons944e7d02021-03-11 11:08:46 -0500127func (m MockBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Liz Kammera92e8442021-04-07 20:25:21 -0400128 result, ok := m.LabelToOutputFiles[label]
Chris Parsons8d6e4332021-02-22 16:13:50 -0500129 return result, ok
130}
131
Chris Parsons944e7d02021-03-11 11:08:46 -0500132func (m MockBazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
Liz Kammera92e8442021-04-07 20:25:21 -0400133 result, ok := m.LabelToOutputFilesAndCcObjectFiles[label]
134 return result.OutputFiles, result.CcObjectFiles, ok
Chris Parsons808d84c2021-03-09 20:43:32 -0500135}
136
Liz Kammer3f9e1552021-04-02 18:47:09 -0400137func (m MockBazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) {
Liz Kammera92e8442021-04-07 20:25:21 -0400138 result, ok := m.LabelToCcStaticLibraryFiles[label]
Liz Kammer3f9e1552021-04-02 18:47:09 -0400139 return result, ok
140}
141
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400142func (m MockBazelContext) InvokeBazel() error {
143 panic("unimplemented")
144}
145
146func (m MockBazelContext) BazelEnabled() bool {
147 return true
148}
149
Liz Kammera92e8442021-04-07 20:25:21 -0400150func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500151
152func (m MockBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
153 return []bazel.BuildStatement{}
154}
155
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400156var _ BazelContext = MockBazelContext{}
157
Chris Parsons944e7d02021-03-11 11:08:46 -0500158func (bazelCtx *bazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
159 rawString, ok := bazelCtx.cquery(label, cquery.GetOutputFiles, archType)
160 var ret []string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400161 if ok {
Chris Parsons944e7d02021-03-11 11:08:46 -0500162 bazelOutput := strings.TrimSpace(rawString)
Liz Kammerf29df7c2021-04-02 13:37:39 -0400163 ret = cquery.GetOutputFiles.ParseResult(bazelOutput)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400164 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500165 return ret, ok
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400166}
167
Chris Parsons944e7d02021-03-11 11:08:46 -0500168func (bazelCtx *bazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
169 var outputFiles []string
Chris Parsons808d84c2021-03-09 20:43:32 -0500170 var ccObjects []string
171
Chris Parsons944e7d02021-03-11 11:08:46 -0500172 result, ok := bazelCtx.cquery(label, cquery.GetOutputFilesAndCcObjectFiles, archType)
Chris Parsons808d84c2021-03-09 20:43:32 -0500173 if ok {
174 bazelOutput := strings.TrimSpace(result)
Liz Kammerf29df7c2021-04-02 13:37:39 -0400175 returnResult := cquery.GetOutputFilesAndCcObjectFiles.ParseResult(bazelOutput)
Chris Parsons944e7d02021-03-11 11:08:46 -0500176 outputFiles = returnResult.OutputFiles
177 ccObjects = returnResult.CcObjectFiles
Chris Parsons808d84c2021-03-09 20:43:32 -0500178 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500179
180 return outputFiles, ccObjects, ok
Chris Parsons808d84c2021-03-09 20:43:32 -0500181}
182
Liz Kammer3f9e1552021-04-02 18:47:09 -0400183// GetPrebuiltCcStaticLibraryFiles returns a slice of prebuilt static libraries for the given
184// label/archType if there are query results; otherwise, it enqueues the query and returns false.
185func (bazelCtx *bazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) {
186 result, ok := bazelCtx.cquery(label, cquery.GetPrebuiltCcStaticLibraryFiles, archType)
187 if !ok {
188 return nil, false
189 }
190
191 bazelOutput := strings.TrimSpace(result)
192 ret := cquery.GetPrebuiltCcStaticLibraryFiles.ParseResult(bazelOutput)
193 return ret, ok
194}
195
Chris Parsons944e7d02021-03-11 11:08:46 -0500196func (n noopBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500197 panic("unimplemented")
198}
199
Chris Parsons944e7d02021-03-11 11:08:46 -0500200func (n noopBazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500201 panic("unimplemented")
202}
203
Liz Kammer3f9e1552021-04-02 18:47:09 -0400204func (n noopBazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) {
205 panic("unimplemented")
206}
207
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400208func (n noopBazelContext) InvokeBazel() error {
209 panic("unimplemented")
210}
211
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500212func (m noopBazelContext) OutputBase() string {
213 return ""
214}
215
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400216func (n noopBazelContext) BazelEnabled() bool {
217 return false
218}
219
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500220func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
221 return []bazel.BuildStatement{}
222}
223
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400224func NewBazelContext(c *config) (BazelContext, error) {
Chris Parsons8b77a002020-10-27 18:59:25 -0400225 // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds"
226 // are production ready.
227 if c.Getenv("USE_BAZEL_ANALYSIS") != "1" {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400228 return noopBazelContext{}, nil
229 }
230
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400231 bazelCtx := bazelContext{buildDir: c.buildDir, requests: make(map[cqueryKey]bool)}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400232 missingEnvVars := []string{}
233 if len(c.Getenv("BAZEL_HOME")) > 1 {
234 bazelCtx.homeDir = c.Getenv("BAZEL_HOME")
235 } else {
236 missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
237 }
238 if len(c.Getenv("BAZEL_PATH")) > 1 {
239 bazelCtx.bazelPath = c.Getenv("BAZEL_PATH")
240 } else {
241 missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
242 }
243 if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
244 bazelCtx.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
245 } else {
246 missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
247 }
248 if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
249 bazelCtx.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
250 } else {
251 missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
252 }
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000253 if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 {
254 bazelCtx.metricsDir = c.Getenv("BAZEL_METRICS_DIR")
255 } else {
256 missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR")
257 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400258 if len(missingEnvVars) > 0 {
259 return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
260 } else {
261 return &bazelCtx, nil
262 }
263}
264
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000265func (context *bazelContext) BazelMetricsDir() string {
266 return context.metricsDir
267}
268
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400269func (context *bazelContext) BazelEnabled() bool {
270 return true
271}
272
273// Adds a cquery request to the Bazel request queue, to be later invoked, or
274// returns the result of the given request if the request was already made.
275// If the given request was already made (and the results are available), then
276// returns (result, true). If the request is queued but no results are available,
277// then returns ("", false).
Liz Kammerf29df7c2021-04-02 13:37:39 -0400278func (context *bazelContext) cquery(label string, requestType cqueryRequest,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500279 archType ArchType) (string, bool) {
280 key := cqueryKey{label, requestType, archType}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400281 if result, ok := context.results[key]; ok {
282 return result, true
283 } else {
284 context.requestMutex.Lock()
285 defer context.requestMutex.Unlock()
286 context.requests[key] = true
287 return "", false
288 }
289}
290
291func pwdPrefix() string {
292 // Darwin doesn't have /proc
293 if runtime.GOOS != "darwin" {
294 return "PWD=/proc/self/cwd"
295 }
296 return ""
297}
298
Chris Parsons808d84c2021-03-09 20:43:32 -0500299// Issues the given bazel command with given build label and additional flags.
300// Returns (stdout, stderr, error). The first and second return values are strings
301// containing the stdout and stderr of the run command, and an error is returned if
302// the invocation returned an error code.
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000303func (context *bazelContext) issueBazelCommand(runName bazel.RunName, command string, labels []string,
Chris Parsons808d84c2021-03-09 20:43:32 -0500304 extraFlags ...string) (string, string, error) {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400305
Jingwen Chen7c6089a2020-11-02 02:56:20 -0500306 cmdFlags := []string{"--output_base=" + context.outputBase, command}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400307 cmdFlags = append(cmdFlags, labels...)
Chris Parsons8ccdb632020-11-17 15:41:01 -0500308 cmdFlags = append(cmdFlags, "--package_path=%workspace%/"+context.intermediatesDir())
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000309 cmdFlags = append(cmdFlags, "--profile="+shared.BazelMetricsFilename(context, runName))
Jingwen Chen91220d72021-03-24 02:18:33 -0400310
311 // Set default platforms to canonicalized values for mixed builds requests.
312 // If these are set in the bazelrc, they will have values that are
313 // non-canonicalized to @sourceroot labels, and thus be invalid when
314 // referenced from the buildroot.
315 //
316 // The actual platform values here may be overridden by configuration
317 // transitions from the buildroot.
Chris Parsonsee423b02021-02-08 23:04:59 -0500318 cmdFlags = append(cmdFlags,
Jingwen Chen91220d72021-03-24 02:18:33 -0400319 fmt.Sprintf("--platforms=%s", canonicalizeLabel("//build/bazel/platforms:android_x86_64")))
Chris Parsonsee423b02021-02-08 23:04:59 -0500320 cmdFlags = append(cmdFlags,
321 fmt.Sprintf("--extra_toolchains=%s", canonicalizeLabel("//prebuilts/clang/host/linux-x86:all")))
Jingwen Chen91220d72021-03-24 02:18:33 -0400322 // This should be parameterized on the host OS, but let's restrict to linux
323 // to keep things simple for now.
324 cmdFlags = append(cmdFlags,
325 fmt.Sprintf("--host_platform=%s", canonicalizeLabel("//build/bazel/platforms:linux_x86_64")))
326
Chris Parsons8d6e4332021-02-22 16:13:50 -0500327 // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network.
328 cmdFlags = append(cmdFlags, "--experimental_repository_disable_download")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400329 cmdFlags = append(cmdFlags, extraFlags...)
330
331 bazelCmd := exec.Command(context.bazelPath, cmdFlags...)
332 bazelCmd.Dir = context.workspaceDir
Chris Parsons8d6e4332021-02-22 16:13:50 -0500333 bazelCmd.Env = append(os.Environ(), "HOME="+context.homeDir, pwdPrefix(),
334 // Disables local host detection of gcc; toolchain information is defined
335 // explicitly in BUILD files.
336 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1")
Colin Crossff0278b2020-10-09 19:24:15 -0700337 stderr := &bytes.Buffer{}
338 bazelCmd.Stderr = stderr
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400339
340 if output, err := bazelCmd.Output(); err != nil {
Chris Parsons808d84c2021-03-09 20:43:32 -0500341 return "", string(stderr.Bytes()),
342 fmt.Errorf("bazel command failed. command: [%s], env: [%s], error [%s]", bazelCmd, bazelCmd.Env, stderr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400343 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500344 return string(output), string(stderr.Bytes()), nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400345 }
346}
347
Chris Parsons8ccdb632020-11-17 15:41:01 -0500348// Returns the string contents of a workspace file that should be output
349// adjacent to the main bzl file and build file.
350// This workspace file allows, via local_repository rule, sourcetree-level
351// BUILD targets to be referenced via @sourceroot.
352func (context *bazelContext) workspaceFileContents() []byte {
353 formatString := `
354# This file is generated by soong_build. Do not edit.
355local_repository(
356 name = "sourceroot",
Jingwen Chen63930982021-03-24 10:04:33 -0400357 path = "%[1]s",
Chris Parsons8ccdb632020-11-17 15:41:01 -0500358)
Liz Kammer8206d4f2021-03-03 16:40:52 -0500359
360local_repository(
361 name = "rules_cc",
Jingwen Chen63930982021-03-24 10:04:33 -0400362 path = "%[1]s/build/bazel/rules_cc",
363)
364
365local_repository(
366 name = "bazel_skylib",
367 path = "%[1]s/build/bazel/bazel_skylib",
Liz Kammer8206d4f2021-03-03 16:40:52 -0500368)
Chris Parsons8ccdb632020-11-17 15:41:01 -0500369`
Jingwen Chen63930982021-03-24 10:04:33 -0400370 return []byte(fmt.Sprintf(formatString, context.workspaceDir))
Chris Parsons8ccdb632020-11-17 15:41:01 -0500371}
372
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400373func (context *bazelContext) mainBzlFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500374 // TODO(cparsons): Define configuration transitions programmatically based
375 // on available archs.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400376 contents := `
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500377#####################################################
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400378# This file is generated by soong_build. Do not edit.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500379#####################################################
380
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400381def _config_node_transition_impl(settings, attr):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500382 return {
Jingwen Chen91220d72021-03-24 02:18:33 -0400383 "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:android_%s" % attr.arch,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500384 }
385
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400386_config_node_transition = transition(
387 implementation = _config_node_transition_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500388 inputs = [],
389 outputs = [
390 "//command_line_option:platforms",
391 ],
392)
393
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400394def _passthrough_rule_impl(ctx):
395 return [DefaultInfo(files = depset(ctx.files.deps))]
396
397config_node = rule(
398 implementation = _passthrough_rule_impl,
399 attrs = {
400 "arch" : attr.string(mandatory = True),
401 "deps" : attr.label_list(cfg = _config_node_transition),
402 "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
403 },
Chris Parsons8d6e4332021-02-22 16:13:50 -0500404)
405
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400406
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500407# Rule representing the root of the build, to depend on all Bazel targets that
408# are required for the build. Building this target will build the entire Bazel
409# build tree.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400410mixed_build_root = rule(
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400411 implementation = _passthrough_rule_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500412 attrs = {
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400413 "deps" : attr.label_list(),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500414 },
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400415)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500416
417def _phony_root_impl(ctx):
418 return []
419
420# Rule to depend on other targets but build nothing.
421# This is useful as follows: building a target of this rule will generate
422# symlink forests for all dependencies of the target, without executing any
423# actions of the build.
424phony_root = rule(
425 implementation = _phony_root_impl,
426 attrs = {"deps" : attr.label_list()},
427)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400428`
429 return []byte(contents)
430}
431
Chris Parsons8ccdb632020-11-17 15:41:01 -0500432// Returns a "canonicalized" corresponding to the given sourcetree-level label.
433// This abstraction is required because a sourcetree label such as //foo/bar:baz
434// must be referenced via the local repository prefix, such as
435// @sourceroot//foo/bar:baz.
436func canonicalizeLabel(label string) string {
437 if strings.HasPrefix(label, "//") {
438 return "@sourceroot" + label
439 } else {
440 return "@sourceroot//" + label
441 }
442}
443
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400444func (context *bazelContext) mainBuildFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500445 // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
446 // architecture mapping.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400447 formatString := `
448# This file is generated by soong_build. Do not edit.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400449load(":main.bzl", "config_node", "mixed_build_root", "phony_root")
450
451%s
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400452
453mixed_build_root(name = "buildroot",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400454 deps = [%s],
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400455)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500456
457phony_root(name = "phonyroot",
458 deps = [":buildroot"],
459)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400460`
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400461 configNodeFormatString := `
462config_node(name = "%s",
463 arch = "%s",
464 deps = [%s],
465)
466`
467
468 configNodesSection := ""
469
470 labelsByArch := map[string][]string{}
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400471 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500472 labelString := fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label))
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400473 archString := getArchString(val)
474 labelsByArch[archString] = append(labelsByArch[archString], labelString)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400475 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400476
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400477 configNodeLabels := []string{}
478 for archString, labels := range labelsByArch {
479 configNodeLabels = append(configNodeLabels, fmt.Sprintf("\":%s\"", archString))
480 labelsString := strings.Join(labels, ",\n ")
481 configNodesSection += fmt.Sprintf(configNodeFormatString, archString, archString, labelsString)
482 }
483
484 return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(configNodeLabels, ",\n ")))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400485}
486
Chris Parsons944e7d02021-03-11 11:08:46 -0500487func indent(original string) string {
488 result := ""
489 for _, line := range strings.Split(original, "\n") {
490 result += " " + line + "\n"
491 }
492 return result
493}
494
Chris Parsons808d84c2021-03-09 20:43:32 -0500495// Returns the file contents of the buildroot.cquery file that should be used for the cquery
496// expression in order to obtain information about buildroot and its dependencies.
497// The contents of this file depend on the bazelContext's requests; requests are enumerated
498// and grouped by their request type. The data retrieved for each label depends on its
499// request type.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400500func (context *bazelContext) cqueryStarlarkFileContents() []byte {
Liz Kammerf29df7c2021-04-02 13:37:39 -0400501 requestTypeToCqueryIdEntries := map[cqueryRequest][]string{}
Chris Parsons944e7d02021-03-11 11:08:46 -0500502 for val, _ := range context.requests {
503 cqueryId := getCqueryId(val)
504 mapEntryString := fmt.Sprintf("%q : True", cqueryId)
505 requestTypeToCqueryIdEntries[val.requestType] =
506 append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString)
507 }
508 labelRegistrationMapSection := ""
509 functionDefSection := ""
510 mainSwitchSection := ""
511
512 mapDeclarationFormatString := `
513%s = {
514 %s
515}
516`
517 functionDefFormatString := `
518def %s(target):
519%s
520`
521 mainSwitchSectionFormatString := `
522 if id_string in %s:
523 return id_string + ">>" + %s(target)
524`
525
Liz Kammer66ffdb72021-04-02 13:26:07 -0400526 for requestType, _ := range requestTypeToCqueryIdEntries {
Chris Parsons944e7d02021-03-11 11:08:46 -0500527 labelMapName := requestType.Name() + "_Labels"
528 functionName := requestType.Name() + "_Fn"
529 labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString,
530 labelMapName,
531 strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n "))
532 functionDefSection += fmt.Sprintf(functionDefFormatString,
533 functionName,
534 indent(requestType.StarlarkFunctionBody()))
535 mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString,
536 labelMapName, functionName)
537 }
538
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400539 formatString := `
540# This file is generated by soong_build. Do not edit.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400541
Chris Parsons944e7d02021-03-11 11:08:46 -0500542# Label Map Section
543%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500544
Chris Parsons944e7d02021-03-11 11:08:46 -0500545# Function Def Section
546%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500547
548def get_arch(target):
549 buildoptions = build_options(target)
550 platforms = build_options(target)["//command_line_option:platforms"]
551 if len(platforms) != 1:
552 # An individual configured target should have only one platform architecture.
553 # Note that it's fine for there to be multiple architectures for the same label,
554 # but each is its own configured target.
555 fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
556 platform_name = build_options(target)["//command_line_option:platforms"][0].name
557 if platform_name == "host":
558 return "HOST"
Jingwen Chen91220d72021-03-24 02:18:33 -0400559 elif not platform_name.startswith("android_"):
560 fail("expected platform name of the form 'android_<arch>', but was " + str(platforms))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500561 return "UNKNOWN"
Jingwen Chen91220d72021-03-24 02:18:33 -0400562 return platform_name[len("android_"):]
Chris Parsons8d6e4332021-02-22 16:13:50 -0500563
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400564def format(target):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500565 id_string = str(target.label) + "|" + get_arch(target)
Chris Parsons944e7d02021-03-11 11:08:46 -0500566
567 # Main switch section
568 %s
569 # This target was not requested via cquery, and thus must be a dependency
570 # of a requested target.
571 return id_string + ">>NONE"
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400572`
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400573
Chris Parsons944e7d02021-03-11 11:08:46 -0500574 return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection,
575 mainSwitchSection))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400576}
577
Chris Parsons8ccdb632020-11-17 15:41:01 -0500578// Returns a workspace-relative path containing build-related metadata required
579// for interfacing with Bazel. Example: out/soong/bazel.
580func (context *bazelContext) intermediatesDir() string {
581 return filepath.Join(context.buildDir, "bazel")
582}
583
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400584// Issues commands to Bazel to receive results for all cquery requests
585// queued in the BazelContext.
586func (context *bazelContext) InvokeBazel() error {
587 context.results = make(map[cqueryKey]string)
588
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400589 var cqueryOutput string
Chris Parsons808d84c2021-03-09 20:43:32 -0500590 var cqueryErr string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400591 var err error
Chris Parsons8ccdb632020-11-17 15:41:01 -0500592
Chris Parsons07c1e4a2021-01-19 17:19:16 -0500593 intermediatesDirPath := absolutePath(context.intermediatesDir())
594 if _, err := os.Stat(intermediatesDirPath); os.IsNotExist(err) {
595 err = os.Mkdir(intermediatesDirPath, 0777)
596 }
597
Chris Parsons8ccdb632020-11-17 15:41:01 -0500598 if err != nil {
599 return err
600 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400601 err = ioutil.WriteFile(
Chris Parsons8ccdb632020-11-17 15:41:01 -0500602 absolutePath(filepath.Join(context.intermediatesDir(), "main.bzl")),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400603 context.mainBzlFileContents(), 0666)
604 if err != nil {
605 return err
606 }
607 err = ioutil.WriteFile(
Chris Parsons8ccdb632020-11-17 15:41:01 -0500608 absolutePath(filepath.Join(context.intermediatesDir(), "BUILD.bazel")),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400609 context.mainBuildFileContents(), 0666)
610 if err != nil {
611 return err
612 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800613 cqueryFileRelpath := filepath.Join(context.intermediatesDir(), "buildroot.cquery")
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400614 err = ioutil.WriteFile(
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800615 absolutePath(cqueryFileRelpath),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400616 context.cqueryStarlarkFileContents(), 0666)
617 if err != nil {
618 return err
619 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800620 workspaceFileRelpath := filepath.Join(context.intermediatesDir(), "WORKSPACE.bazel")
Chris Parsons8ccdb632020-11-17 15:41:01 -0500621 err = ioutil.WriteFile(
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800622 absolutePath(workspaceFileRelpath),
Chris Parsons8ccdb632020-11-17 15:41:01 -0500623 context.workspaceFileContents(), 0666)
624 if err != nil {
625 return err
626 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800627 buildrootLabel := "//:buildroot"
Chris Parsons808d84c2021-03-09 20:43:32 -0500628 cqueryOutput, cqueryErr, err = context.issueBazelCommand(bazel.CqueryBuildRootRunName, "cquery",
Chris Parsons8d6e4332021-02-22 16:13:50 -0500629 []string{fmt.Sprintf("kind(rule, deps(%s))", buildrootLabel)},
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400630 "--output=starlark",
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800631 "--starlark:file="+cqueryFileRelpath)
Chris Parsons8d6e4332021-02-22 16:13:50 -0500632 err = ioutil.WriteFile(
633 absolutePath(filepath.Join(context.intermediatesDir(), "cquery.out")),
634 []byte(cqueryOutput), 0666)
635 if err != nil {
636 return err
637 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400638
639 if err != nil {
640 return err
641 }
642
643 cqueryResults := map[string]string{}
644 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
645 if strings.Contains(outputLine, ">>") {
646 splitLine := strings.SplitN(outputLine, ">>", 2)
647 cqueryResults[splitLine[0]] = splitLine[1]
648 }
649 }
650
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400651 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500652 if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400653 context.results[val] = string(cqueryResult)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400654 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500655 return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]",
656 getCqueryId(val), cqueryOutput, cqueryErr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400657 }
658 }
659
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500660 // Issue an aquery command to retrieve action information about the bazel build tree.
661 //
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400662 // TODO(cparsons): Use --target_pattern_file to avoid command line limits.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500663 var aqueryOutput string
Chris Parsons808d84c2021-03-09 20:43:32 -0500664 aqueryOutput, _, err = context.issueBazelCommand(bazel.AqueryBuildRootRunName, "aquery",
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800665 []string{fmt.Sprintf("deps(%s)", buildrootLabel),
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500666 // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
667 // proto sources, which would add a number of unnecessary dependencies.
668 "--output=jsonproto"})
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400669
670 if err != nil {
671 return err
672 }
673
Chris Parsons4f069892021-01-15 12:22:41 -0500674 context.buildStatements, err = bazel.AqueryBuildStatements([]byte(aqueryOutput))
675 if err != nil {
676 return err
677 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500678
679 // Issue a build command of the phony root to generate symlink forests for dependencies of the
680 // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
681 // but some of symlinks may be required to resolve source dependencies of the build.
Chris Parsons808d84c2021-03-09 20:43:32 -0500682 _, _, err = context.issueBazelCommand(bazel.BazelBuildPhonyRootRunName, "build",
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500683 []string{"//:phonyroot"})
684
685 if err != nil {
686 return err
687 }
688
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400689 // Clear requests.
690 context.requests = map[cqueryKey]bool{}
691 return nil
692}
Chris Parsonsa798d962020-10-12 23:44:08 -0400693
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500694func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
695 return context.buildStatements
696}
697
698func (context *bazelContext) OutputBase() string {
699 return context.outputBase
700}
701
Chris Parsonsa798d962020-10-12 23:44:08 -0400702// Singleton used for registering BUILD file ninja dependencies (needed
703// for correctness of builds which use Bazel.
704func BazelSingleton() Singleton {
705 return &bazelSingleton{}
706}
707
708type bazelSingleton struct{}
709
710func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500711 // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
712 if !ctx.Config().BazelContext.BazelEnabled() {
713 return
714 }
Chris Parsonsa798d962020-10-12 23:44:08 -0400715
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500716 // Add ninja file dependencies for files which all bazel invocations require.
717 bazelBuildList := absolutePath(filepath.Join(
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100718 filepath.Dir(bootstrap.CmdlineModuleListFile()), "bazel.list"))
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500719 ctx.AddNinjaFileDeps(bazelBuildList)
720
721 data, err := ioutil.ReadFile(bazelBuildList)
722 if err != nil {
723 ctx.Errorf(err.Error())
724 }
725 files := strings.Split(strings.TrimSpace(string(data)), "\n")
726 for _, file := range files {
727 ctx.AddNinjaFileDeps(file)
728 }
729
730 // Register bazel-owned build statements (obtained from the aquery invocation).
731 for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500732 if len(buildStatement.Command) < 1 {
Rupert Shuttlewortha29903f2021-04-06 16:17:33 +0000733 panic(fmt.Sprintf("unhandled build statement: %v", buildStatement))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500734 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500735 rule := NewRuleBuilder(pctx, ctx)
736 cmd := rule.Command()
737 cmd.Text(fmt.Sprintf("cd %s/execroot/__main__ && %s",
738 ctx.Config().BazelContext.OutputBase(), buildStatement.Command))
739
740 for _, outputPath := range buildStatement.OutputPaths {
741 cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400742 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500743 for _, inputPath := range buildStatement.InputPaths {
744 cmd.Implicit(PathForBazelOut(ctx, inputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400745 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500746
Liz Kammerde116852021-03-25 16:42:37 -0400747 if depfile := buildStatement.Depfile; depfile != nil {
748 cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile))
749 }
750
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500751 // This is required to silence warnings pertaining to unexpected timestamps. Particularly,
752 // some Bazel builtins (such as files in the bazel_tools directory) have far-future
753 // timestamps. Without restat, Ninja would emit warnings that the input files of a
754 // build statement have later timestamps than the outputs.
755 rule.Restat()
756
Liz Kammer13548d72020-12-16 11:13:30 -0800757 rule.Build(fmt.Sprintf("bazel %d", index), buildStatement.Mnemonic)
Chris Parsonsa798d962020-10-12 23:44:08 -0400758 }
759}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500760
761func getCqueryId(key cqueryKey) string {
762 return canonicalizeLabel(key.label) + "|" + getArchString(key)
763}
764
765func getArchString(key cqueryKey) string {
766 arch := key.archType.Name
767 if len(arch) > 0 {
768 return arch
769 } else {
770 return "x86_64"
771 }
772}