blob: 0595d68a18d0e3f6ec761847c62ab0ed17aa38b6 [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"
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050030 "github.com/google/blueprint/bootstrap"
31
Patrice Arruda05ab2d02020-12-12 06:24:26 +000032 "android/soong/bazel"
33 "android/soong/shared"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040034)
35
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040036type CqueryRequestType int
37
38const (
39 getAllFiles CqueryRequestType = iota
Chris Parsons8d6e4332021-02-22 16:13:50 -050040 getCcObjectFiles
Chris Parsons808d84c2021-03-09 20:43:32 -050041 getAllFilesAndCcObjectFiles
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040042)
43
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040044// Map key to describe bazel cquery requests.
45type cqueryKey struct {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040046 label string
Chris Parsons944e7d02021-03-11 11:08:46 -050047 requestType cquery.RequestType
Chris Parsons8d6e4332021-02-22 16:13:50 -050048 archType ArchType
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040049}
50
51type BazelContext interface {
52 // The below methods involve queuing cquery requests to be later invoked
53 // by bazel. If any of these methods return (_, false), then the request
54 // has been queued to be run later.
55
56 // Returns result files built by building the given bazel target label.
Chris Parsons944e7d02021-03-11 11:08:46 -050057 GetOutputFiles(label string, archType ArchType) ([]string, bool)
Chris Parsons8d6e4332021-02-22 16:13:50 -050058
59 // Returns object files produced by compiling the given cc-related target.
60 // Retrieves these files from Bazel's CcInfo provider.
61 GetCcObjectFiles(label string, archType ArchType) ([]string, bool)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040062
Chris Parsons944e7d02021-03-11 11:08:46 -050063 // TODO(cparsons): Other cquery-related methods should be added here.
64 // Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order).
65 GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool)
Chris Parsons808d84c2021-03-09 20:43:32 -050066
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040067 // ** End cquery methods
68
69 // Issues commands to Bazel to receive results for all cquery requests
70 // queued in the BazelContext.
71 InvokeBazel() error
72
73 // Returns true if bazel is enabled for the given configuration.
74 BazelEnabled() bool
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050075
76 // Returns the bazel output base (the root directory for all bazel intermediate outputs).
77 OutputBase() string
78
79 // Returns build statements which should get registered to reflect Bazel's outputs.
80 BuildStatementsToRegister() []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040081}
82
83// A context object which tracks queued requests that need to be made to Bazel,
84// and their results after the requests have been made.
85type bazelContext struct {
86 homeDir string
87 bazelPath string
88 outputBase string
89 workspaceDir string
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040090 buildDir string
Patrice Arruda05ab2d02020-12-12 06:24:26 +000091 metricsDir string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040092
93 requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
94 requestMutex sync.Mutex // requests can be written in parallel
95
96 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050097
98 // Build statements which should get registered to reflect Bazel's outputs.
99 buildStatements []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400100}
101
102var _ BazelContext = &bazelContext{}
103
104// A bazel context to use when Bazel is disabled.
105type noopBazelContext struct{}
106
107var _ BazelContext = noopBazelContext{}
108
109// A bazel context to use for tests.
110type MockBazelContext struct {
111 AllFiles map[string][]string
112}
113
Chris Parsons944e7d02021-03-11 11:08:46 -0500114func (m MockBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500115 result, ok := m.AllFiles[label]
116 return result, ok
117}
118
119func (m MockBazelContext) GetCcObjectFiles(label string, archType ArchType) ([]string, bool) {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400120 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)
152 ret = cquery.GetOutputFiles.ParseResult(bazelOutput).([]string)
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 Parsons8d6e4332021-02-22 16:13:50 -0500157func (bazelCtx *bazelContext) GetCcObjectFiles(label string, archType ArchType) ([]string, bool) {
Chris Parsons944e7d02021-03-11 11:08:46 -0500158 rawString, ok := bazelCtx.cquery(label, cquery.GetCcObjectFiles, archType)
159 var returnResult []string
Chris Parsons8d6e4332021-02-22 16:13:50 -0500160 if ok {
Chris Parsons944e7d02021-03-11 11:08:46 -0500161 bazelOutput := strings.TrimSpace(rawString)
162 returnResult = cquery.GetCcObjectFiles.ParseResult(bazelOutput).([]string)
Chris Parsons8d6e4332021-02-22 16:13:50 -0500163 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500164 return returnResult, ok
Chris Parsons8d6e4332021-02-22 16:13:50 -0500165}
166
Chris Parsons944e7d02021-03-11 11:08:46 -0500167func (bazelCtx *bazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
168 var outputFiles []string
Chris Parsons808d84c2021-03-09 20:43:32 -0500169 var ccObjects []string
170
Chris Parsons944e7d02021-03-11 11:08:46 -0500171 result, ok := bazelCtx.cquery(label, cquery.GetOutputFilesAndCcObjectFiles, archType)
Chris Parsons808d84c2021-03-09 20:43:32 -0500172 if ok {
173 bazelOutput := strings.TrimSpace(result)
Chris Parsons944e7d02021-03-11 11:08:46 -0500174 returnResult := cquery.GetOutputFilesAndCcObjectFiles.ParseResult(bazelOutput).(cquery.GetOutputFilesAndCcObjectFiles_Result)
175 outputFiles = returnResult.OutputFiles
176 ccObjects = returnResult.CcObjectFiles
Chris Parsons808d84c2021-03-09 20:43:32 -0500177 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500178
179 return outputFiles, ccObjects, ok
Chris Parsons808d84c2021-03-09 20:43:32 -0500180}
181
Chris Parsons944e7d02021-03-11 11:08:46 -0500182func (n noopBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500183 panic("unimplemented")
184}
185
186func (n noopBazelContext) GetCcObjectFiles(label string, archType ArchType) ([]string, bool) {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400187 panic("unimplemented")
188}
189
Chris Parsons944e7d02021-03-11 11:08:46 -0500190func (n noopBazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500191 panic("unimplemented")
192}
193
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400194func (n noopBazelContext) InvokeBazel() error {
195 panic("unimplemented")
196}
197
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500198func (m noopBazelContext) OutputBase() string {
199 return ""
200}
201
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400202func (n noopBazelContext) BazelEnabled() bool {
203 return false
204}
205
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500206func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
207 return []bazel.BuildStatement{}
208}
209
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400210func NewBazelContext(c *config) (BazelContext, error) {
Chris Parsons8b77a002020-10-27 18:59:25 -0400211 // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds"
212 // are production ready.
213 if c.Getenv("USE_BAZEL_ANALYSIS") != "1" {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400214 return noopBazelContext{}, nil
215 }
216
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400217 bazelCtx := bazelContext{buildDir: c.buildDir, requests: make(map[cqueryKey]bool)}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400218 missingEnvVars := []string{}
219 if len(c.Getenv("BAZEL_HOME")) > 1 {
220 bazelCtx.homeDir = c.Getenv("BAZEL_HOME")
221 } else {
222 missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
223 }
224 if len(c.Getenv("BAZEL_PATH")) > 1 {
225 bazelCtx.bazelPath = c.Getenv("BAZEL_PATH")
226 } else {
227 missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
228 }
229 if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
230 bazelCtx.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
231 } else {
232 missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
233 }
234 if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
235 bazelCtx.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
236 } else {
237 missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
238 }
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000239 if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 {
240 bazelCtx.metricsDir = c.Getenv("BAZEL_METRICS_DIR")
241 } else {
242 missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR")
243 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400244 if len(missingEnvVars) > 0 {
245 return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
246 } else {
247 return &bazelCtx, nil
248 }
249}
250
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000251func (context *bazelContext) BazelMetricsDir() string {
252 return context.metricsDir
253}
254
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400255func (context *bazelContext) BazelEnabled() bool {
256 return true
257}
258
259// Adds a cquery request to the Bazel request queue, to be later invoked, or
260// returns the result of the given request if the request was already made.
261// If the given request was already made (and the results are available), then
262// returns (result, true). If the request is queued but no results are available,
263// then returns ("", false).
Chris Parsons944e7d02021-03-11 11:08:46 -0500264func (context *bazelContext) cquery(label string, requestType cquery.RequestType,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500265 archType ArchType) (string, bool) {
266 key := cqueryKey{label, requestType, archType}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400267 if result, ok := context.results[key]; ok {
268 return result, true
269 } else {
270 context.requestMutex.Lock()
271 defer context.requestMutex.Unlock()
272 context.requests[key] = true
273 return "", false
274 }
275}
276
277func pwdPrefix() string {
278 // Darwin doesn't have /proc
279 if runtime.GOOS != "darwin" {
280 return "PWD=/proc/self/cwd"
281 }
282 return ""
283}
284
Chris Parsons808d84c2021-03-09 20:43:32 -0500285// Issues the given bazel command with given build label and additional flags.
286// Returns (stdout, stderr, error). The first and second return values are strings
287// containing the stdout and stderr of the run command, and an error is returned if
288// the invocation returned an error code.
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000289func (context *bazelContext) issueBazelCommand(runName bazel.RunName, command string, labels []string,
Chris Parsons808d84c2021-03-09 20:43:32 -0500290 extraFlags ...string) (string, string, error) {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400291
Jingwen Chen7c6089a2020-11-02 02:56:20 -0500292 cmdFlags := []string{"--output_base=" + context.outputBase, command}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400293 cmdFlags = append(cmdFlags, labels...)
Chris Parsons8ccdb632020-11-17 15:41:01 -0500294 cmdFlags = append(cmdFlags, "--package_path=%workspace%/"+context.intermediatesDir())
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000295 cmdFlags = append(cmdFlags, "--profile="+shared.BazelMetricsFilename(context, runName))
Chris Parsonsee423b02021-02-08 23:04:59 -0500296 // Set default platforms to canonicalized values for mixed builds requests. If these are set
297 // in the bazelrc, they will have values that are non-canonicalized, and thus be invalid.
298 // The actual platform values here may be overridden by configuration transitions from the buildroot.
299 cmdFlags = append(cmdFlags,
300 fmt.Sprintf("--platforms=%s", canonicalizeLabel("//build/bazel/platforms:generic_x86_64")))
301 cmdFlags = append(cmdFlags,
302 fmt.Sprintf("--extra_toolchains=%s", canonicalizeLabel("//prebuilts/clang/host/linux-x86:all")))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500303 // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network.
304 cmdFlags = append(cmdFlags, "--experimental_repository_disable_download")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400305 cmdFlags = append(cmdFlags, extraFlags...)
306
307 bazelCmd := exec.Command(context.bazelPath, cmdFlags...)
308 bazelCmd.Dir = context.workspaceDir
Chris Parsons8d6e4332021-02-22 16:13:50 -0500309 bazelCmd.Env = append(os.Environ(), "HOME="+context.homeDir, pwdPrefix(),
310 // Disables local host detection of gcc; toolchain information is defined
311 // explicitly in BUILD files.
312 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1")
Colin Crossff0278b2020-10-09 19:24:15 -0700313 stderr := &bytes.Buffer{}
314 bazelCmd.Stderr = stderr
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400315
316 if output, err := bazelCmd.Output(); err != nil {
Chris Parsons808d84c2021-03-09 20:43:32 -0500317 return "", string(stderr.Bytes()),
318 fmt.Errorf("bazel command failed. command: [%s], env: [%s], error [%s]", bazelCmd, bazelCmd.Env, stderr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400319 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500320 return string(output), string(stderr.Bytes()), nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400321 }
322}
323
Chris Parsons8ccdb632020-11-17 15:41:01 -0500324// Returns the string contents of a workspace file that should be output
325// adjacent to the main bzl file and build file.
326// This workspace file allows, via local_repository rule, sourcetree-level
327// BUILD targets to be referenced via @sourceroot.
328func (context *bazelContext) workspaceFileContents() []byte {
329 formatString := `
330# This file is generated by soong_build. Do not edit.
331local_repository(
332 name = "sourceroot",
333 path = "%s",
334)
335`
336 return []byte(fmt.Sprintf(formatString, context.workspaceDir))
337}
338
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400339func (context *bazelContext) mainBzlFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500340 // TODO(cparsons): Define configuration transitions programmatically based
341 // on available archs.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400342 contents := `
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500343#####################################################
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400344# This file is generated by soong_build. Do not edit.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500345#####################################################
346
Chris Parsons8d6e4332021-02-22 16:13:50 -0500347def _x86_64_transition_impl(settings, attr):
348 return {
349 "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_x86_64",
350 }
351
352def _x86_transition_impl(settings, attr):
353 return {
354 "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_x86",
355 }
356
357def _arm64_transition_impl(settings, attr):
358 return {
359 "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_arm64",
360 }
361
362def _arm_transition_impl(settings, attr):
363 return {
364 "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_arm",
365 }
366
367x86_64_transition = transition(
368 implementation = _x86_64_transition_impl,
369 inputs = [],
370 outputs = [
371 "//command_line_option:platforms",
372 ],
373)
374
375x86_transition = transition(
376 implementation = _x86_transition_impl,
377 inputs = [],
378 outputs = [
379 "//command_line_option:platforms",
380 ],
381)
382
383arm64_transition = transition(
384 implementation = _arm64_transition_impl,
385 inputs = [],
386 outputs = [
387 "//command_line_option:platforms",
388 ],
389)
390
391arm_transition = transition(
392 implementation = _arm_transition_impl,
393 inputs = [],
394 outputs = [
395 "//command_line_option:platforms",
396 ],
397)
398
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400399def _mixed_build_root_impl(ctx):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500400 all_files = ctx.files.deps_x86_64 + ctx.files.deps_x86 + ctx.files.deps_arm64 + ctx.files.deps_arm
401 return [DefaultInfo(files = depset(all_files))]
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400402
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500403# Rule representing the root of the build, to depend on all Bazel targets that
404# are required for the build. Building this target will build the entire Bazel
405# build tree.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400406mixed_build_root = rule(
407 implementation = _mixed_build_root_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500408 attrs = {
409 "deps_x86_64" : attr.label_list(cfg = x86_64_transition),
410 "deps_x86" : attr.label_list(cfg = x86_transition),
411 "deps_arm64" : attr.label_list(cfg = arm64_transition),
412 "deps_arm" : attr.label_list(cfg = arm_transition),
413 "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
414 },
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 Parsonsdbcb1ff2020-12-10 17:19:18 -0500449load(":main.bzl", "mixed_build_root", "phony_root")
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400450
451mixed_build_root(name = "buildroot",
Chris Parsons8d6e4332021-02-22 16:13:50 -0500452 deps_x86_64 = [%s],
453 deps_x86 = [%s],
454 deps_arm64 = [%s],
455 deps_arm = [%s],
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400456)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500457
458phony_root(name = "phonyroot",
459 deps = [":buildroot"],
460)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400461`
Chris Parsons8d6e4332021-02-22 16:13:50 -0500462 var deps_x86_64 []string = nil
463 var deps_x86 []string = nil
464 var deps_arm64 []string = nil
465 var deps_arm []string = nil
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400466 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500467 labelString := fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label))
468 switch getArchString(val) {
469 case "x86_64":
470 deps_x86_64 = append(deps_x86_64, labelString)
471 case "x86":
472 deps_x86 = append(deps_x86, labelString)
473 case "arm64":
474 deps_arm64 = append(deps_arm64, labelString)
475 case "arm":
476 deps_arm = append(deps_arm, labelString)
477 default:
Liz Kammer15b04e22021-03-04 09:45:21 -0500478 panic(fmt.Sprintf("unhandled architecture %s for %v", getArchString(val), val))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500479 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400480 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400481
Chris Parsons8d6e4332021-02-22 16:13:50 -0500482 return []byte(fmt.Sprintf(formatString,
483 strings.Join(deps_x86_64, ",\n "),
484 strings.Join(deps_x86, ",\n "),
485 strings.Join(deps_arm64, ",\n "),
486 strings.Join(deps_arm, ",\n ")))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400487}
488
Chris Parsons944e7d02021-03-11 11:08:46 -0500489func indent(original string) string {
490 result := ""
491 for _, line := range strings.Split(original, "\n") {
492 result += " " + line + "\n"
493 }
494 return result
495}
496
Chris Parsons808d84c2021-03-09 20:43:32 -0500497// Returns the file contents of the buildroot.cquery file that should be used for the cquery
498// expression in order to obtain information about buildroot and its dependencies.
499// The contents of this file depend on the bazelContext's requests; requests are enumerated
500// and grouped by their request type. The data retrieved for each label depends on its
501// request type.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400502func (context *bazelContext) cqueryStarlarkFileContents() []byte {
Chris Parsons944e7d02021-03-11 11:08:46 -0500503 requestTypeToCqueryIdEntries := map[cquery.RequestType][]string{}
504 for val, _ := range context.requests {
505 cqueryId := getCqueryId(val)
506 mapEntryString := fmt.Sprintf("%q : True", cqueryId)
507 requestTypeToCqueryIdEntries[val.requestType] =
508 append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString)
509 }
510 labelRegistrationMapSection := ""
511 functionDefSection := ""
512 mainSwitchSection := ""
513
514 mapDeclarationFormatString := `
515%s = {
516 %s
517}
518`
519 functionDefFormatString := `
520def %s(target):
521%s
522`
523 mainSwitchSectionFormatString := `
524 if id_string in %s:
525 return id_string + ">>" + %s(target)
526`
527
528 for _, requestType := range cquery.RequestTypes {
529 labelMapName := requestType.Name() + "_Labels"
530 functionName := requestType.Name() + "_Fn"
531 labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString,
532 labelMapName,
533 strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n "))
534 functionDefSection += fmt.Sprintf(functionDefFormatString,
535 functionName,
536 indent(requestType.StarlarkFunctionBody()))
537 mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString,
538 labelMapName, functionName)
539 }
540
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400541 formatString := `
542# This file is generated by soong_build. Do not edit.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400543
Chris Parsons944e7d02021-03-11 11:08:46 -0500544# Label Map Section
545%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500546
Chris Parsons944e7d02021-03-11 11:08:46 -0500547# Function Def Section
548%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500549
550def get_arch(target):
551 buildoptions = build_options(target)
552 platforms = build_options(target)["//command_line_option:platforms"]
553 if len(platforms) != 1:
554 # An individual configured target should have only one platform architecture.
555 # Note that it's fine for there to be multiple architectures for the same label,
556 # but each is its own configured target.
557 fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
558 platform_name = build_options(target)["//command_line_option:platforms"][0].name
559 if platform_name == "host":
560 return "HOST"
561 elif not platform_name.startswith("generic_"):
562 fail("expected platform name of the form 'generic_<arch>', but was " + str(platforms))
563 return "UNKNOWN"
564 return platform_name[len("generic_"):]
565
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400566def format(target):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500567 id_string = str(target.label) + "|" + get_arch(target)
Chris Parsons944e7d02021-03-11 11:08:46 -0500568
569 # Main switch section
570 %s
571 # This target was not requested via cquery, and thus must be a dependency
572 # of a requested target.
573 return id_string + ">>NONE"
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400574`
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400575
Chris Parsons944e7d02021-03-11 11:08:46 -0500576 return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection,
577 mainSwitchSection))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400578}
579
Chris Parsons8ccdb632020-11-17 15:41:01 -0500580// Returns a workspace-relative path containing build-related metadata required
581// for interfacing with Bazel. Example: out/soong/bazel.
582func (context *bazelContext) intermediatesDir() string {
583 return filepath.Join(context.buildDir, "bazel")
584}
585
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400586// Issues commands to Bazel to receive results for all cquery requests
587// queued in the BazelContext.
588func (context *bazelContext) InvokeBazel() error {
589 context.results = make(map[cqueryKey]string)
590
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400591 var cqueryOutput string
Chris Parsons808d84c2021-03-09 20:43:32 -0500592 var cqueryErr string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400593 var err error
Chris Parsons8ccdb632020-11-17 15:41:01 -0500594
Chris Parsons07c1e4a2021-01-19 17:19:16 -0500595 intermediatesDirPath := absolutePath(context.intermediatesDir())
596 if _, err := os.Stat(intermediatesDirPath); os.IsNotExist(err) {
597 err = os.Mkdir(intermediatesDirPath, 0777)
598 }
599
Chris Parsons8ccdb632020-11-17 15:41:01 -0500600 if err != nil {
601 return err
602 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400603 err = ioutil.WriteFile(
Chris Parsons8ccdb632020-11-17 15:41:01 -0500604 absolutePath(filepath.Join(context.intermediatesDir(), "main.bzl")),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400605 context.mainBzlFileContents(), 0666)
606 if err != nil {
607 return err
608 }
609 err = ioutil.WriteFile(
Chris Parsons8ccdb632020-11-17 15:41:01 -0500610 absolutePath(filepath.Join(context.intermediatesDir(), "BUILD.bazel")),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400611 context.mainBuildFileContents(), 0666)
612 if err != nil {
613 return err
614 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800615 cqueryFileRelpath := filepath.Join(context.intermediatesDir(), "buildroot.cquery")
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400616 err = ioutil.WriteFile(
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800617 absolutePath(cqueryFileRelpath),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400618 context.cqueryStarlarkFileContents(), 0666)
619 if err != nil {
620 return err
621 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800622 workspaceFileRelpath := filepath.Join(context.intermediatesDir(), "WORKSPACE.bazel")
Chris Parsons8ccdb632020-11-17 15:41:01 -0500623 err = ioutil.WriteFile(
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800624 absolutePath(workspaceFileRelpath),
Chris Parsons8ccdb632020-11-17 15:41:01 -0500625 context.workspaceFileContents(), 0666)
626 if err != nil {
627 return err
628 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800629 buildrootLabel := "//:buildroot"
Chris Parsons808d84c2021-03-09 20:43:32 -0500630 cqueryOutput, cqueryErr, err = context.issueBazelCommand(bazel.CqueryBuildRootRunName, "cquery",
Chris Parsons8d6e4332021-02-22 16:13:50 -0500631 []string{fmt.Sprintf("kind(rule, deps(%s))", buildrootLabel)},
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400632 "--output=starlark",
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800633 "--starlark:file="+cqueryFileRelpath)
Chris Parsons8d6e4332021-02-22 16:13:50 -0500634 err = ioutil.WriteFile(
635 absolutePath(filepath.Join(context.intermediatesDir(), "cquery.out")),
636 []byte(cqueryOutput), 0666)
637 if err != nil {
638 return err
639 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400640
641 if err != nil {
642 return err
643 }
644
645 cqueryResults := map[string]string{}
646 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
647 if strings.Contains(outputLine, ">>") {
648 splitLine := strings.SplitN(outputLine, ">>", 2)
649 cqueryResults[splitLine[0]] = splitLine[1]
650 }
651 }
652
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400653 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500654 if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400655 context.results[val] = string(cqueryResult)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400656 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500657 return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]",
658 getCqueryId(val), cqueryOutput, cqueryErr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400659 }
660 }
661
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500662 // Issue an aquery command to retrieve action information about the bazel build tree.
663 //
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400664 // TODO(cparsons): Use --target_pattern_file to avoid command line limits.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500665 var aqueryOutput string
Chris Parsons808d84c2021-03-09 20:43:32 -0500666 aqueryOutput, _, err = context.issueBazelCommand(bazel.AqueryBuildRootRunName, "aquery",
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800667 []string{fmt.Sprintf("deps(%s)", buildrootLabel),
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500668 // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
669 // proto sources, which would add a number of unnecessary dependencies.
670 "--output=jsonproto"})
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400671
672 if err != nil {
673 return err
674 }
675
Chris Parsons4f069892021-01-15 12:22:41 -0500676 context.buildStatements, err = bazel.AqueryBuildStatements([]byte(aqueryOutput))
677 if err != nil {
678 return err
679 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500680
681 // Issue a build command of the phony root to generate symlink forests for dependencies of the
682 // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
683 // but some of symlinks may be required to resolve source dependencies of the build.
Chris Parsons808d84c2021-03-09 20:43:32 -0500684 _, _, err = context.issueBazelCommand(bazel.BazelBuildPhonyRootRunName, "build",
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500685 []string{"//:phonyroot"})
686
687 if err != nil {
688 return err
689 }
690
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400691 // Clear requests.
692 context.requests = map[cqueryKey]bool{}
693 return nil
694}
Chris Parsonsa798d962020-10-12 23:44:08 -0400695
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500696func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
697 return context.buildStatements
698}
699
700func (context *bazelContext) OutputBase() string {
701 return context.outputBase
702}
703
Chris Parsonsa798d962020-10-12 23:44:08 -0400704// Singleton used for registering BUILD file ninja dependencies (needed
705// for correctness of builds which use Bazel.
706func BazelSingleton() Singleton {
707 return &bazelSingleton{}
708}
709
710type bazelSingleton struct{}
711
712func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500713 // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
714 if !ctx.Config().BazelContext.BazelEnabled() {
715 return
716 }
Chris Parsonsa798d962020-10-12 23:44:08 -0400717
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500718 // Add ninja file dependencies for files which all bazel invocations require.
719 bazelBuildList := absolutePath(filepath.Join(
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100720 filepath.Dir(bootstrap.CmdlineModuleListFile()), "bazel.list"))
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500721 ctx.AddNinjaFileDeps(bazelBuildList)
722
723 data, err := ioutil.ReadFile(bazelBuildList)
724 if err != nil {
725 ctx.Errorf(err.Error())
726 }
727 files := strings.Split(strings.TrimSpace(string(data)), "\n")
728 for _, file := range files {
729 ctx.AddNinjaFileDeps(file)
730 }
731
732 // Register bazel-owned build statements (obtained from the aquery invocation).
733 for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500734 if len(buildStatement.Command) < 1 {
735 panic(fmt.Sprintf("unhandled build statement: %s", buildStatement))
736 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500737 rule := NewRuleBuilder(pctx, ctx)
738 cmd := rule.Command()
739 cmd.Text(fmt.Sprintf("cd %s/execroot/__main__ && %s",
740 ctx.Config().BazelContext.OutputBase(), buildStatement.Command))
741
742 for _, outputPath := range buildStatement.OutputPaths {
743 cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400744 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500745 for _, inputPath := range buildStatement.InputPaths {
746 cmd.Implicit(PathForBazelOut(ctx, inputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400747 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500748
749 // This is required to silence warnings pertaining to unexpected timestamps. Particularly,
750 // some Bazel builtins (such as files in the bazel_tools directory) have far-future
751 // timestamps. Without restat, Ninja would emit warnings that the input files of a
752 // build statement have later timestamps than the outputs.
753 rule.Restat()
754
Liz Kammer13548d72020-12-16 11:13:30 -0800755 rule.Build(fmt.Sprintf("bazel %d", index), buildStatement.Mnemonic)
Chris Parsonsa798d962020-10-12 23:44:08 -0400756 }
757}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500758
759func getCqueryId(key cqueryKey) string {
760 return canonicalizeLabel(key.label) + "|" + getArchString(key)
761}
762
763func getArchString(key cqueryKey) string {
764 arch := key.archType.Name
765 if len(arch) > 0 {
766 return arch
767 } else {
768 return "x86_64"
769 }
770}