blob: b272daa53fca201b98fe24c1fb375843c648b885 [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
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000060// bazelHandler is the interface for a helper object related to deferring to Bazel for
61// processing a module (during Bazel mixed builds). Individual module types should define
62// their own bazel handler if they support deferring to Bazel.
63type BazelHandler interface {
64 // Issue query to Bazel to retrieve information about Bazel's view of the current module.
65 // If Bazel returns this information, set module properties on the current module to reflect
66 // the returned information.
67 // Returns true if information was available from Bazel, false if bazel invocation still needs to occur.
68 GenerateBazelBuildActions(ctx ModuleContext, label string) bool
69}
70
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040071type BazelContext interface {
72 // The below methods involve queuing cquery requests to be later invoked
73 // by bazel. If any of these methods return (_, false), then the request
74 // has been queued to be run later.
75
76 // Returns result files built by building the given bazel target label.
Chris Parsons944e7d02021-03-11 11:08:46 -050077 GetOutputFiles(label string, archType ArchType) ([]string, bool)
Chris Parsons8d6e4332021-02-22 16:13:50 -050078
Chris Parsons944e7d02021-03-11 11:08:46 -050079 // TODO(cparsons): Other cquery-related methods should be added here.
80 // Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order).
Liz Kammerfe23bf32021-04-09 16:17:05 -040081 GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool, error)
Liz Kammer3f9e1552021-04-02 18:47:09 -040082
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040083 // ** End cquery methods
84
85 // Issues commands to Bazel to receive results for all cquery requests
86 // queued in the BazelContext.
87 InvokeBazel() error
88
89 // Returns true if bazel is enabled for the given configuration.
90 BazelEnabled() bool
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050091
92 // Returns the bazel output base (the root directory for all bazel intermediate outputs).
93 OutputBase() string
94
95 // Returns build statements which should get registered to reflect Bazel's outputs.
96 BuildStatementsToRegister() []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040097}
98
Liz Kammer8d62a4f2021-04-08 09:47:28 -040099type bazelRunner interface {
100 issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand, extraFlags ...string) (string, string, error)
101}
102
103type bazelPaths struct {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400104 homeDir string
105 bazelPath string
106 outputBase string
107 workspaceDir string
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400108 buildDir string
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000109 metricsDir string
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400110}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400111
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400112// A context object which tracks queued requests that need to be made to Bazel,
113// and their results after the requests have been made.
114type bazelContext struct {
115 bazelRunner
116 paths *bazelPaths
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400117 requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
118 requestMutex sync.Mutex // requests can be written in parallel
119
120 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500121
122 // Build statements which should get registered to reflect Bazel's outputs.
123 buildStatements []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400124}
125
126var _ BazelContext = &bazelContext{}
127
128// A bazel context to use when Bazel is disabled.
129type noopBazelContext struct{}
130
131var _ BazelContext = noopBazelContext{}
132
133// A bazel context to use for tests.
134type MockBazelContext struct {
Liz Kammera92e8442021-04-07 20:25:21 -0400135 OutputBaseDir string
136
Liz Kammerb71794d2021-04-09 14:07:00 -0400137 LabelToOutputFiles map[string][]string
138 LabelToCcInfo map[string]cquery.CcInfo
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400139}
140
Chris Parsons944e7d02021-03-11 11:08:46 -0500141func (m MockBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Liz Kammera92e8442021-04-07 20:25:21 -0400142 result, ok := m.LabelToOutputFiles[label]
Chris Parsons8d6e4332021-02-22 16:13:50 -0500143 return result, ok
144}
145
Liz Kammerfe23bf32021-04-09 16:17:05 -0400146func (m MockBazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool, error) {
Liz Kammerb71794d2021-04-09 14:07:00 -0400147 result, ok := m.LabelToCcInfo[label]
Liz Kammerfe23bf32021-04-09 16:17:05 -0400148 return result, ok, nil
Liz Kammer3f9e1552021-04-02 18:47:09 -0400149}
150
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400151func (m MockBazelContext) InvokeBazel() error {
152 panic("unimplemented")
153}
154
155func (m MockBazelContext) BazelEnabled() bool {
156 return true
157}
158
Liz Kammera92e8442021-04-07 20:25:21 -0400159func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500160
161func (m MockBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
162 return []bazel.BuildStatement{}
163}
164
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400165var _ BazelContext = MockBazelContext{}
166
Chris Parsons944e7d02021-03-11 11:08:46 -0500167func (bazelCtx *bazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
168 rawString, ok := bazelCtx.cquery(label, cquery.GetOutputFiles, archType)
169 var ret []string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400170 if ok {
Chris Parsons944e7d02021-03-11 11:08:46 -0500171 bazelOutput := strings.TrimSpace(rawString)
Liz Kammerf29df7c2021-04-02 13:37:39 -0400172 ret = cquery.GetOutputFiles.ParseResult(bazelOutput)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400173 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500174 return ret, ok
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400175}
176
Liz Kammerfe23bf32021-04-09 16:17:05 -0400177func (bazelCtx *bazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool, error) {
Liz Kammerb71794d2021-04-09 14:07:00 -0400178 result, ok := bazelCtx.cquery(label, cquery.GetCcInfo, archType)
Liz Kammer3f9e1552021-04-02 18:47:09 -0400179 if !ok {
Liz Kammerfe23bf32021-04-09 16:17:05 -0400180 return cquery.CcInfo{}, ok, nil
Liz Kammer3f9e1552021-04-02 18:47:09 -0400181 }
182
183 bazelOutput := strings.TrimSpace(result)
Liz Kammerfe23bf32021-04-09 16:17:05 -0400184 ret, err := cquery.GetCcInfo.ParseResult(bazelOutput)
185 return ret, ok, err
Liz Kammer3f9e1552021-04-02 18:47:09 -0400186}
187
Chris Parsons944e7d02021-03-11 11:08:46 -0500188func (n noopBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500189 panic("unimplemented")
190}
191
Liz Kammerfe23bf32021-04-09 16:17:05 -0400192func (n noopBazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool, error) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500193 panic("unimplemented")
194}
195
Liz Kammer3f9e1552021-04-02 18:47:09 -0400196func (n noopBazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) {
197 panic("unimplemented")
198}
199
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400200func (n noopBazelContext) InvokeBazel() error {
201 panic("unimplemented")
202}
203
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500204func (m noopBazelContext) OutputBase() string {
205 return ""
206}
207
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400208func (n noopBazelContext) BazelEnabled() bool {
209 return false
210}
211
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500212func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
213 return []bazel.BuildStatement{}
214}
215
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400216func NewBazelContext(c *config) (BazelContext, error) {
Chris Parsons8b77a002020-10-27 18:59:25 -0400217 // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds"
218 // are production ready.
Jingwen Chen442b1a42021-06-17 07:02:15 +0000219 if !c.IsEnvTrue("USE_BAZEL_ANALYSIS") {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400220 return noopBazelContext{}, nil
221 }
222
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400223 p, err := bazelPathsFromConfig(c)
224 if err != nil {
225 return nil, err
226 }
227 return &bazelContext{
228 bazelRunner: &builtinBazelRunner{},
229 paths: p,
230 requests: make(map[cqueryKey]bool),
231 }, nil
232}
233
234func bazelPathsFromConfig(c *config) (*bazelPaths, error) {
235 p := bazelPaths{
236 buildDir: c.buildDir,
237 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400238 missingEnvVars := []string{}
239 if len(c.Getenv("BAZEL_HOME")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400240 p.homeDir = c.Getenv("BAZEL_HOME")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400241 } else {
242 missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
243 }
244 if len(c.Getenv("BAZEL_PATH")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400245 p.bazelPath = c.Getenv("BAZEL_PATH")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400246 } else {
247 missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
248 }
249 if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400250 p.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400251 } else {
252 missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
253 }
254 if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400255 p.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400256 } else {
257 missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
258 }
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000259 if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400260 p.metricsDir = c.Getenv("BAZEL_METRICS_DIR")
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000261 } else {
262 missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR")
263 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400264 if len(missingEnvVars) > 0 {
265 return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
266 } else {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400267 return &p, nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400268 }
269}
270
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400271func (p *bazelPaths) BazelMetricsDir() string {
272 return p.metricsDir
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000273}
274
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400275func (context *bazelContext) BazelEnabled() bool {
276 return true
277}
278
279// Adds a cquery request to the Bazel request queue, to be later invoked, or
280// returns the result of the given request if the request was already made.
281// If the given request was already made (and the results are available), then
282// returns (result, true). If the request is queued but no results are available,
283// then returns ("", false).
Liz Kammerf29df7c2021-04-02 13:37:39 -0400284func (context *bazelContext) cquery(label string, requestType cqueryRequest,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500285 archType ArchType) (string, bool) {
286 key := cqueryKey{label, requestType, archType}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400287 if result, ok := context.results[key]; ok {
288 return result, true
289 } else {
290 context.requestMutex.Lock()
291 defer context.requestMutex.Unlock()
292 context.requests[key] = true
293 return "", false
294 }
295}
296
297func pwdPrefix() string {
298 // Darwin doesn't have /proc
299 if runtime.GOOS != "darwin" {
300 return "PWD=/proc/self/cwd"
301 }
302 return ""
303}
304
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400305type bazelCommand struct {
306 command string
307 // query or label
308 expression string
309}
310
311type mockBazelRunner struct {
312 bazelCommandResults map[bazelCommand]string
313 commands []bazelCommand
314}
315
316func (r *mockBazelRunner) issueBazelCommand(paths *bazelPaths,
317 runName bazel.RunName,
318 command bazelCommand,
319 extraFlags ...string) (string, string, error) {
320 r.commands = append(r.commands, command)
321 if ret, ok := r.bazelCommandResults[command]; ok {
322 return ret, "", nil
323 }
324 return "", "", nil
325}
326
327type builtinBazelRunner struct{}
328
Chris Parsons808d84c2021-03-09 20:43:32 -0500329// Issues the given bazel command with given build label and additional flags.
330// Returns (stdout, stderr, error). The first and second return values are strings
331// containing the stdout and stderr of the run command, and an error is returned if
332// the invocation returned an error code.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400333func (r *builtinBazelRunner) issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand,
Chris Parsons808d84c2021-03-09 20:43:32 -0500334 extraFlags ...string) (string, string, error) {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200335 cmdFlags := []string{"--output_base=" + absolutePath(paths.outputBase), command.command}
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400336 cmdFlags = append(cmdFlags, command.expression)
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400337 cmdFlags = append(cmdFlags, "--profile="+shared.BazelMetricsFilename(paths, runName))
Jingwen Chen91220d72021-03-24 02:18:33 -0400338
339 // Set default platforms to canonicalized values for mixed builds requests.
340 // If these are set in the bazelrc, they will have values that are
341 // non-canonicalized to @sourceroot labels, and thus be invalid when
342 // referenced from the buildroot.
343 //
344 // The actual platform values here may be overridden by configuration
345 // transitions from the buildroot.
Chris Parsonsee423b02021-02-08 23:04:59 -0500346 cmdFlags = append(cmdFlags,
Liz Kammerc0c66092021-07-26 17:38:47 -0400347 fmt.Sprintf("--platforms=%s", "//build/bazel/platforms:android_target"))
Chris Parsonsee423b02021-02-08 23:04:59 -0500348 cmdFlags = append(cmdFlags,
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200349 fmt.Sprintf("--extra_toolchains=%s", "//prebuilts/clang/host/linux-x86:all"))
Jingwen Chen91220d72021-03-24 02:18:33 -0400350 // This should be parameterized on the host OS, but let's restrict to linux
351 // to keep things simple for now.
352 cmdFlags = append(cmdFlags,
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200353 fmt.Sprintf("--host_platform=%s", "//build/bazel/platforms:linux_x86_64"))
Jingwen Chen91220d72021-03-24 02:18:33 -0400354
Chris Parsons8d6e4332021-02-22 16:13:50 -0500355 // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network.
356 cmdFlags = append(cmdFlags, "--experimental_repository_disable_download")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400357 cmdFlags = append(cmdFlags, extraFlags...)
358
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400359 bazelCmd := exec.Command(paths.bazelPath, cmdFlags...)
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200360 bazelCmd.Dir = absolutePath(paths.syntheticWorkspaceDir())
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200361 bazelCmd.Env = append(os.Environ(),
362 "HOME="+paths.homeDir,
363 pwdPrefix(),
364 "BUILD_DIR="+absolutePath(paths.buildDir),
Jingwen Chen8c523582021-06-01 11:19:53 +0000365 // Make OUT_DIR absolute here so tools/bazel.sh uses the correct
366 // OUT_DIR at <root>/out, instead of <root>/out/soong/workspace/out.
367 "OUT_DIR="+absolutePath(paths.outDir()),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500368 // Disables local host detection of gcc; toolchain information is defined
369 // explicitly in BUILD files.
370 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1")
Colin Crossff0278b2020-10-09 19:24:15 -0700371 stderr := &bytes.Buffer{}
372 bazelCmd.Stderr = stderr
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400373
374 if output, err := bazelCmd.Output(); err != nil {
Chris Parsons808d84c2021-03-09 20:43:32 -0500375 return "", string(stderr.Bytes()),
376 fmt.Errorf("bazel command failed. command: [%s], env: [%s], error [%s]", bazelCmd, bazelCmd.Env, stderr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400377 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500378 return string(output), string(stderr.Bytes()), nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400379 }
380}
381
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400382func (context *bazelContext) mainBzlFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500383 // TODO(cparsons): Define configuration transitions programmatically based
384 // on available archs.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400385 contents := `
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500386#####################################################
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400387# This file is generated by soong_build. Do not edit.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500388#####################################################
389
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400390def _config_node_transition_impl(settings, attr):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500391 return {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200392 "//command_line_option:platforms": "@//build/bazel/platforms:android_%s" % attr.arch,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500393 }
394
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400395_config_node_transition = transition(
396 implementation = _config_node_transition_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500397 inputs = [],
398 outputs = [
399 "//command_line_option:platforms",
400 ],
401)
402
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400403def _passthrough_rule_impl(ctx):
404 return [DefaultInfo(files = depset(ctx.files.deps))]
405
406config_node = rule(
407 implementation = _passthrough_rule_impl,
408 attrs = {
409 "arch" : attr.string(mandatory = True),
410 "deps" : attr.label_list(cfg = _config_node_transition),
411 "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
412 },
Chris Parsons8d6e4332021-02-22 16:13:50 -0500413)
414
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400415
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500416# Rule representing the root of the build, to depend on all Bazel targets that
417# are required for the build. Building this target will build the entire Bazel
418# build tree.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400419mixed_build_root = rule(
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400420 implementation = _passthrough_rule_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500421 attrs = {
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400422 "deps" : attr.label_list(),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500423 },
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400424)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500425
426def _phony_root_impl(ctx):
427 return []
428
429# Rule to depend on other targets but build nothing.
430# This is useful as follows: building a target of this rule will generate
431# symlink forests for all dependencies of the target, without executing any
432# actions of the build.
433phony_root = rule(
434 implementation = _phony_root_impl,
435 attrs = {"deps" : attr.label_list()},
436)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400437`
438 return []byte(contents)
439}
440
441func (context *bazelContext) mainBuildFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500442 // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
443 // architecture mapping.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400444 formatString := `
445# This file is generated by soong_build. Do not edit.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400446load(":main.bzl", "config_node", "mixed_build_root", "phony_root")
447
448%s
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400449
450mixed_build_root(name = "buildroot",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400451 deps = [%s],
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400452)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500453
454phony_root(name = "phonyroot",
455 deps = [":buildroot"],
456)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400457`
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400458 configNodeFormatString := `
459config_node(name = "%s",
460 arch = "%s",
461 deps = [%s],
462)
463`
464
465 configNodesSection := ""
466
467 labelsByArch := map[string][]string{}
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400468 for val, _ := range context.requests {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200469 labelString := fmt.Sprintf("\"@%s\"", val.label)
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400470 archString := getArchString(val)
471 labelsByArch[archString] = append(labelsByArch[archString], labelString)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400472 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400473
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400474 configNodeLabels := []string{}
475 for archString, labels := range labelsByArch {
476 configNodeLabels = append(configNodeLabels, fmt.Sprintf("\":%s\"", archString))
477 labelsString := strings.Join(labels, ",\n ")
478 configNodesSection += fmt.Sprintf(configNodeFormatString, archString, archString, labelsString)
479 }
480
481 return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(configNodeLabels, ",\n ")))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400482}
483
Chris Parsons944e7d02021-03-11 11:08:46 -0500484func indent(original string) string {
485 result := ""
486 for _, line := range strings.Split(original, "\n") {
487 result += " " + line + "\n"
488 }
489 return result
490}
491
Chris Parsons808d84c2021-03-09 20:43:32 -0500492// Returns the file contents of the buildroot.cquery file that should be used for the cquery
493// expression in order to obtain information about buildroot and its dependencies.
494// The contents of this file depend on the bazelContext's requests; requests are enumerated
495// and grouped by their request type. The data retrieved for each label depends on its
496// request type.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400497func (context *bazelContext) cqueryStarlarkFileContents() []byte {
Liz Kammerf29df7c2021-04-02 13:37:39 -0400498 requestTypeToCqueryIdEntries := map[cqueryRequest][]string{}
Chris Parsons944e7d02021-03-11 11:08:46 -0500499 for val, _ := range context.requests {
500 cqueryId := getCqueryId(val)
501 mapEntryString := fmt.Sprintf("%q : True", cqueryId)
502 requestTypeToCqueryIdEntries[val.requestType] =
503 append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString)
504 }
505 labelRegistrationMapSection := ""
506 functionDefSection := ""
507 mainSwitchSection := ""
508
509 mapDeclarationFormatString := `
510%s = {
511 %s
512}
513`
514 functionDefFormatString := `
515def %s(target):
516%s
517`
518 mainSwitchSectionFormatString := `
519 if id_string in %s:
520 return id_string + ">>" + %s(target)
521`
522
Liz Kammer66ffdb72021-04-02 13:26:07 -0400523 for requestType, _ := range requestTypeToCqueryIdEntries {
Chris Parsons944e7d02021-03-11 11:08:46 -0500524 labelMapName := requestType.Name() + "_Labels"
525 functionName := requestType.Name() + "_Fn"
526 labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString,
527 labelMapName,
528 strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n "))
529 functionDefSection += fmt.Sprintf(functionDefFormatString,
530 functionName,
531 indent(requestType.StarlarkFunctionBody()))
532 mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString,
533 labelMapName, functionName)
534 }
535
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400536 formatString := `
537# This file is generated by soong_build. Do not edit.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400538
Chris Parsons944e7d02021-03-11 11:08:46 -0500539# Label Map Section
540%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500541
Chris Parsons944e7d02021-03-11 11:08:46 -0500542# Function Def Section
543%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500544
545def get_arch(target):
546 buildoptions = build_options(target)
547 platforms = build_options(target)["//command_line_option:platforms"]
548 if len(platforms) != 1:
549 # An individual configured target should have only one platform architecture.
550 # Note that it's fine for there to be multiple architectures for the same label,
551 # but each is its own configured target.
552 fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
553 platform_name = build_options(target)["//command_line_option:platforms"][0].name
554 if platform_name == "host":
555 return "HOST"
Chris Parsons94a0bba2021-06-04 15:03:47 -0400556 elif platform_name.startswith("android_"):
557 return platform_name[len("android_"):]
558 elif platform_name.startswith("linux_"):
559 return platform_name[len("linux_"):]
560 else:
561 fail("expected platform name of the form 'android_<arch>' or 'linux_<arch>', but was " + str(platforms))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500562 return "UNKNOWN"
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
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200578// Returns a path containing build-related metadata required for interfacing
579// with Bazel. Example: out/soong/bazel.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400580func (p *bazelPaths) intermediatesDir() string {
581 return filepath.Join(p.buildDir, "bazel")
Chris Parsons8ccdb632020-11-17 15:41:01 -0500582}
583
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200584// Returns the path where the contents of the @soong_injection repository live.
585// It is used by Soong to tell Bazel things it cannot over the command line.
586func (p *bazelPaths) injectedFilesDir() string {
Liz Kammer09f947d2021-05-12 14:51:49 -0400587 return filepath.Join(p.buildDir, bazel.SoongInjectionDirName)
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200588}
589
590// Returns the path of the synthetic Bazel workspace that contains a symlink
591// forest composed the whole source tree and BUILD files generated by bp2build.
592func (p *bazelPaths) syntheticWorkspaceDir() string {
593 return filepath.Join(p.buildDir, "workspace")
594}
595
Jingwen Chen8c523582021-06-01 11:19:53 +0000596// Returns the path to the top level out dir ($OUT_DIR).
597func (p *bazelPaths) outDir() string {
598 return filepath.Dir(p.buildDir)
599}
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
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200610 soongInjectionPath := absolutePath(context.paths.injectedFilesDir())
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200611 mixedBuildsPath := filepath.Join(soongInjectionPath, "mixed_builds")
612 if _, err := os.Stat(mixedBuildsPath); os.IsNotExist(err) {
613 err = os.MkdirAll(mixedBuildsPath, 0777)
Chris Parsons07c1e4a2021-01-19 17:19:16 -0500614 }
Chris Parsons8ccdb632020-11-17 15:41:01 -0500615 if err != nil {
616 return err
617 }
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200618
619 err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "WORKSPACE.bazel"), []byte{}, 0666)
620 if err != nil {
621 return err
622 }
623
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400624 err = ioutil.WriteFile(
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200625 filepath.Join(mixedBuildsPath, "main.bzl"),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400626 context.mainBzlFileContents(), 0666)
627 if err != nil {
628 return err
629 }
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200630
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400631 err = ioutil.WriteFile(
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200632 filepath.Join(mixedBuildsPath, "BUILD.bazel"),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400633 context.mainBuildFileContents(), 0666)
634 if err != nil {
635 return err
636 }
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200637 cqueryFileRelpath := filepath.Join(context.paths.injectedFilesDir(), "buildroot.cquery")
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400638 err = ioutil.WriteFile(
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800639 absolutePath(cqueryFileRelpath),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400640 context.cqueryStarlarkFileContents(), 0666)
641 if err != nil {
642 return err
643 }
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200644 buildrootLabel := "@soong_injection//mixed_builds:buildroot"
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400645 cqueryOutput, cqueryErr, err = context.issueBazelCommand(
646 context.paths,
647 bazel.CqueryBuildRootRunName,
648 bazelCommand{"cquery", fmt.Sprintf("kind(rule, deps(%s))", buildrootLabel)},
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400649 "--output=starlark",
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200650 "--starlark:file="+absolutePath(cqueryFileRelpath))
651 err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "cquery.out"),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500652 []byte(cqueryOutput), 0666)
653 if err != nil {
654 return err
655 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400656
657 if err != nil {
658 return err
659 }
660
661 cqueryResults := map[string]string{}
662 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
663 if strings.Contains(outputLine, ">>") {
664 splitLine := strings.SplitN(outputLine, ">>", 2)
665 cqueryResults[splitLine[0]] = splitLine[1]
666 }
667 }
668
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400669 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500670 if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400671 context.results[val] = string(cqueryResult)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400672 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500673 return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]",
674 getCqueryId(val), cqueryOutput, cqueryErr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400675 }
676 }
677
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500678 // Issue an aquery command to retrieve action information about the bazel build tree.
679 //
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400680 // TODO(cparsons): Use --target_pattern_file to avoid command line limits.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500681 var aqueryOutput string
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400682 aqueryOutput, _, err = context.issueBazelCommand(
683 context.paths,
684 bazel.AqueryBuildRootRunName,
685 bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)},
686 // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
687 // proto sources, which would add a number of unnecessary dependencies.
688 "--output=jsonproto")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400689
690 if err != nil {
691 return err
692 }
693
Chris Parsons4f069892021-01-15 12:22:41 -0500694 context.buildStatements, err = bazel.AqueryBuildStatements([]byte(aqueryOutput))
695 if err != nil {
696 return err
697 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500698
699 // Issue a build command of the phony root to generate symlink forests for dependencies of the
700 // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
701 // but some of symlinks may be required to resolve source dependencies of the build.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400702 _, _, err = context.issueBazelCommand(
703 context.paths,
704 bazel.BazelBuildPhonyRootRunName,
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200705 bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"})
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500706
707 if err != nil {
708 return err
709 }
710
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400711 // Clear requests.
712 context.requests = map[cqueryKey]bool{}
713 return nil
714}
Chris Parsonsa798d962020-10-12 23:44:08 -0400715
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500716func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
717 return context.buildStatements
718}
719
720func (context *bazelContext) OutputBase() string {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400721 return context.paths.outputBase
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500722}
723
Chris Parsonsa798d962020-10-12 23:44:08 -0400724// Singleton used for registering BUILD file ninja dependencies (needed
725// for correctness of builds which use Bazel.
726func BazelSingleton() Singleton {
727 return &bazelSingleton{}
728}
729
730type bazelSingleton struct{}
731
732func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500733 // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
734 if !ctx.Config().BazelContext.BazelEnabled() {
735 return
736 }
Chris Parsonsa798d962020-10-12 23:44:08 -0400737
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500738 // Add ninja file dependencies for files which all bazel invocations require.
739 bazelBuildList := absolutePath(filepath.Join(
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200740 filepath.Dir(bootstrap.CmdlineArgs.ModuleListFile), "bazel.list"))
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500741 ctx.AddNinjaFileDeps(bazelBuildList)
742
743 data, err := ioutil.ReadFile(bazelBuildList)
744 if err != nil {
745 ctx.Errorf(err.Error())
746 }
747 files := strings.Split(strings.TrimSpace(string(data)), "\n")
748 for _, file := range files {
749 ctx.AddNinjaFileDeps(file)
750 }
751
752 // Register bazel-owned build statements (obtained from the aquery invocation).
753 for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500754 if len(buildStatement.Command) < 1 {
Rupert Shuttlewortha29903f2021-04-06 16:17:33 +0000755 panic(fmt.Sprintf("unhandled build statement: %v", buildStatement))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500756 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500757 rule := NewRuleBuilder(pctx, ctx)
758 cmd := rule.Command()
Chris Parsons94a0bba2021-06-04 15:03:47 -0400759
760 // cd into Bazel's execution root, which is the action cwd.
761 cmd.Text(fmt.Sprintf("cd %s/execroot/__main__ && ", ctx.Config().BazelContext.OutputBase()))
762
763 for _, pair := range buildStatement.Env {
764 // Set per-action env variables, if any.
765 cmd.Flag(pair.Key + "=" + pair.Value)
766 }
767
768 // The actual Bazel action.
769 cmd.Text(" " + buildStatement.Command)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500770
771 for _, outputPath := range buildStatement.OutputPaths {
772 cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400773 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500774 for _, inputPath := range buildStatement.InputPaths {
775 cmd.Implicit(PathForBazelOut(ctx, inputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400776 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500777
Liz Kammerde116852021-03-25 16:42:37 -0400778 if depfile := buildStatement.Depfile; depfile != nil {
779 cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile))
780 }
781
Liz Kammerc49e6822021-06-08 15:04:11 -0400782 for _, symlinkPath := range buildStatement.SymlinkPaths {
783 cmd.ImplicitSymlinkOutput(PathForBazelOut(ctx, symlinkPath))
784 }
785
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500786 // This is required to silence warnings pertaining to unexpected timestamps. Particularly,
787 // some Bazel builtins (such as files in the bazel_tools directory) have far-future
788 // timestamps. Without restat, Ninja would emit warnings that the input files of a
789 // build statement have later timestamps than the outputs.
790 rule.Restat()
791
Liz Kammer13548d72020-12-16 11:13:30 -0800792 rule.Build(fmt.Sprintf("bazel %d", index), buildStatement.Mnemonic)
Chris Parsonsa798d962020-10-12 23:44:08 -0400793 }
794}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500795
796func getCqueryId(key cqueryKey) string {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200797 return key.label + "|" + getArchString(key)
Chris Parsons8d6e4332021-02-22 16:13:50 -0500798}
799
800func getArchString(key cqueryKey) string {
801 arch := key.archType.Name
802 if len(arch) > 0 {
803 return arch
804 } else {
805 return "x86_64"
806 }
807}