blob: 640106363d6f8abfca883863155bacf90b2be422 [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
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040037type CqueryRequestType int
38
39const (
40 getAllFiles CqueryRequestType = iota
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
Chris Parsons944e7d02021-03-11 11:08:46 -050059 // TODO(cparsons): Other cquery-related methods should be added here.
60 // Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order).
61 GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool)
Chris Parsons808d84c2021-03-09 20:43:32 -050062
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040063 // ** End cquery methods
64
65 // Issues commands to Bazel to receive results for all cquery requests
66 // queued in the BazelContext.
67 InvokeBazel() error
68
69 // Returns true if bazel is enabled for the given configuration.
70 BazelEnabled() bool
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050071
72 // Returns the bazel output base (the root directory for all bazel intermediate outputs).
73 OutputBase() string
74
75 // Returns build statements which should get registered to reflect Bazel's outputs.
76 BuildStatementsToRegister() []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040077}
78
79// A context object which tracks queued requests that need to be made to Bazel,
80// and their results after the requests have been made.
81type bazelContext struct {
82 homeDir string
83 bazelPath string
84 outputBase string
85 workspaceDir string
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040086 buildDir string
Patrice Arruda05ab2d02020-12-12 06:24:26 +000087 metricsDir string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040088
89 requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
90 requestMutex sync.Mutex // requests can be written in parallel
91
92 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050093
94 // Build statements which should get registered to reflect Bazel's outputs.
95 buildStatements []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040096}
97
98var _ BazelContext = &bazelContext{}
99
100// A bazel context to use when Bazel is disabled.
101type noopBazelContext struct{}
102
103var _ BazelContext = noopBazelContext{}
104
105// A bazel context to use for tests.
106type MockBazelContext struct {
107 AllFiles map[string][]string
108}
109
Chris Parsons944e7d02021-03-11 11:08:46 -0500110func (m MockBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500111 result, ok := m.AllFiles[label]
112 return result, ok
113}
114
Chris Parsons944e7d02021-03-11 11:08:46 -0500115func (m MockBazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500116 result, ok := m.AllFiles[label]
117 return result, result, ok
118}
119
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400120func (m MockBazelContext) InvokeBazel() error {
121 panic("unimplemented")
122}
123
124func (m MockBazelContext) BazelEnabled() bool {
125 return true
126}
127
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500128func (m MockBazelContext) OutputBase() string {
129 return "outputbase"
130}
131
132func (m MockBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
133 return []bazel.BuildStatement{}
134}
135
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400136var _ BazelContext = MockBazelContext{}
137
Chris Parsons944e7d02021-03-11 11:08:46 -0500138func (bazelCtx *bazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
139 rawString, ok := bazelCtx.cquery(label, cquery.GetOutputFiles, archType)
140 var ret []string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400141 if ok {
Chris Parsons944e7d02021-03-11 11:08:46 -0500142 bazelOutput := strings.TrimSpace(rawString)
143 ret = cquery.GetOutputFiles.ParseResult(bazelOutput).([]string)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400144 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500145 return ret, ok
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400146}
147
Chris Parsons944e7d02021-03-11 11:08:46 -0500148func (bazelCtx *bazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
149 var outputFiles []string
Chris Parsons808d84c2021-03-09 20:43:32 -0500150 var ccObjects []string
151
Chris Parsons944e7d02021-03-11 11:08:46 -0500152 result, ok := bazelCtx.cquery(label, cquery.GetOutputFilesAndCcObjectFiles, archType)
Chris Parsons808d84c2021-03-09 20:43:32 -0500153 if ok {
154 bazelOutput := strings.TrimSpace(result)
Chris Parsons944e7d02021-03-11 11:08:46 -0500155 returnResult := cquery.GetOutputFilesAndCcObjectFiles.ParseResult(bazelOutput).(cquery.GetOutputFilesAndCcObjectFiles_Result)
156 outputFiles = returnResult.OutputFiles
157 ccObjects = returnResult.CcObjectFiles
Chris Parsons808d84c2021-03-09 20:43:32 -0500158 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500159
160 return outputFiles, ccObjects, ok
Chris Parsons808d84c2021-03-09 20:43:32 -0500161}
162
Chris Parsons944e7d02021-03-11 11:08:46 -0500163func (n noopBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500164 panic("unimplemented")
165}
166
Chris Parsons944e7d02021-03-11 11:08:46 -0500167func (n noopBazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500168 panic("unimplemented")
169}
170
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400171func (n noopBazelContext) InvokeBazel() error {
172 panic("unimplemented")
173}
174
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500175func (m noopBazelContext) OutputBase() string {
176 return ""
177}
178
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400179func (n noopBazelContext) BazelEnabled() bool {
180 return false
181}
182
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500183func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
184 return []bazel.BuildStatement{}
185}
186
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400187func NewBazelContext(c *config) (BazelContext, error) {
Chris Parsons8b77a002020-10-27 18:59:25 -0400188 // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds"
189 // are production ready.
190 if c.Getenv("USE_BAZEL_ANALYSIS") != "1" {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400191 return noopBazelContext{}, nil
192 }
193
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400194 bazelCtx := bazelContext{buildDir: c.buildDir, requests: make(map[cqueryKey]bool)}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400195 missingEnvVars := []string{}
196 if len(c.Getenv("BAZEL_HOME")) > 1 {
197 bazelCtx.homeDir = c.Getenv("BAZEL_HOME")
198 } else {
199 missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
200 }
201 if len(c.Getenv("BAZEL_PATH")) > 1 {
202 bazelCtx.bazelPath = c.Getenv("BAZEL_PATH")
203 } else {
204 missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
205 }
206 if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
207 bazelCtx.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
208 } else {
209 missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
210 }
211 if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
212 bazelCtx.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
213 } else {
214 missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
215 }
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000216 if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 {
217 bazelCtx.metricsDir = c.Getenv("BAZEL_METRICS_DIR")
218 } else {
219 missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR")
220 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400221 if len(missingEnvVars) > 0 {
222 return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
223 } else {
224 return &bazelCtx, nil
225 }
226}
227
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000228func (context *bazelContext) BazelMetricsDir() string {
229 return context.metricsDir
230}
231
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400232func (context *bazelContext) BazelEnabled() bool {
233 return true
234}
235
236// Adds a cquery request to the Bazel request queue, to be later invoked, or
237// returns the result of the given request if the request was already made.
238// If the given request was already made (and the results are available), then
239// returns (result, true). If the request is queued but no results are available,
240// then returns ("", false).
Chris Parsons944e7d02021-03-11 11:08:46 -0500241func (context *bazelContext) cquery(label string, requestType cquery.RequestType,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500242 archType ArchType) (string, bool) {
243 key := cqueryKey{label, requestType, archType}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400244 if result, ok := context.results[key]; ok {
245 return result, true
246 } else {
247 context.requestMutex.Lock()
248 defer context.requestMutex.Unlock()
249 context.requests[key] = true
250 return "", false
251 }
252}
253
254func pwdPrefix() string {
255 // Darwin doesn't have /proc
256 if runtime.GOOS != "darwin" {
257 return "PWD=/proc/self/cwd"
258 }
259 return ""
260}
261
Chris Parsons808d84c2021-03-09 20:43:32 -0500262// Issues the given bazel command with given build label and additional flags.
263// Returns (stdout, stderr, error). The first and second return values are strings
264// containing the stdout and stderr of the run command, and an error is returned if
265// the invocation returned an error code.
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000266func (context *bazelContext) issueBazelCommand(runName bazel.RunName, command string, labels []string,
Chris Parsons808d84c2021-03-09 20:43:32 -0500267 extraFlags ...string) (string, string, error) {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400268
Jingwen Chen7c6089a2020-11-02 02:56:20 -0500269 cmdFlags := []string{"--output_base=" + context.outputBase, command}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400270 cmdFlags = append(cmdFlags, labels...)
Chris Parsons8ccdb632020-11-17 15:41:01 -0500271 cmdFlags = append(cmdFlags, "--package_path=%workspace%/"+context.intermediatesDir())
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000272 cmdFlags = append(cmdFlags, "--profile="+shared.BazelMetricsFilename(context, runName))
Chris Parsonsee423b02021-02-08 23:04:59 -0500273 // Set default platforms to canonicalized values for mixed builds requests. If these are set
274 // in the bazelrc, they will have values that are non-canonicalized, and thus be invalid.
275 // The actual platform values here may be overridden by configuration transitions from the buildroot.
276 cmdFlags = append(cmdFlags,
277 fmt.Sprintf("--platforms=%s", canonicalizeLabel("//build/bazel/platforms:generic_x86_64")))
278 cmdFlags = append(cmdFlags,
279 fmt.Sprintf("--extra_toolchains=%s", canonicalizeLabel("//prebuilts/clang/host/linux-x86:all")))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500280 // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network.
281 cmdFlags = append(cmdFlags, "--experimental_repository_disable_download")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400282 cmdFlags = append(cmdFlags, extraFlags...)
283
284 bazelCmd := exec.Command(context.bazelPath, cmdFlags...)
285 bazelCmd.Dir = context.workspaceDir
Chris Parsons8d6e4332021-02-22 16:13:50 -0500286 bazelCmd.Env = append(os.Environ(), "HOME="+context.homeDir, pwdPrefix(),
287 // Disables local host detection of gcc; toolchain information is defined
288 // explicitly in BUILD files.
289 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1")
Colin Crossff0278b2020-10-09 19:24:15 -0700290 stderr := &bytes.Buffer{}
291 bazelCmd.Stderr = stderr
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400292
293 if output, err := bazelCmd.Output(); err != nil {
Chris Parsons808d84c2021-03-09 20:43:32 -0500294 return "", string(stderr.Bytes()),
295 fmt.Errorf("bazel command failed. command: [%s], env: [%s], error [%s]", bazelCmd, bazelCmd.Env, stderr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400296 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500297 return string(output), string(stderr.Bytes()), nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400298 }
299}
300
Chris Parsons8ccdb632020-11-17 15:41:01 -0500301// Returns the string contents of a workspace file that should be output
302// adjacent to the main bzl file and build file.
303// This workspace file allows, via local_repository rule, sourcetree-level
304// BUILD targets to be referenced via @sourceroot.
305func (context *bazelContext) workspaceFileContents() []byte {
306 formatString := `
307# This file is generated by soong_build. Do not edit.
308local_repository(
309 name = "sourceroot",
310 path = "%s",
311)
Liz Kammer8206d4f2021-03-03 16:40:52 -0500312
313local_repository(
314 name = "rules_cc",
315 path = "%s/build/bazel/rules_cc",
316)
Chris Parsons8ccdb632020-11-17 15:41:01 -0500317`
Liz Kammer8206d4f2021-03-03 16:40:52 -0500318 return []byte(fmt.Sprintf(formatString, context.workspaceDir, context.workspaceDir))
Chris Parsons8ccdb632020-11-17 15:41:01 -0500319}
320
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400321func (context *bazelContext) mainBzlFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500322 // TODO(cparsons): Define configuration transitions programmatically based
323 // on available archs.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400324 contents := `
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500325#####################################################
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400326# This file is generated by soong_build. Do not edit.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500327#####################################################
328
Chris Parsons8d6e4332021-02-22 16:13:50 -0500329def _x86_64_transition_impl(settings, attr):
330 return {
331 "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_x86_64",
332 }
333
334def _x86_transition_impl(settings, attr):
335 return {
336 "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_x86",
337 }
338
339def _arm64_transition_impl(settings, attr):
340 return {
341 "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_arm64",
342 }
343
344def _arm_transition_impl(settings, attr):
345 return {
346 "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_arm",
347 }
348
349x86_64_transition = transition(
350 implementation = _x86_64_transition_impl,
351 inputs = [],
352 outputs = [
353 "//command_line_option:platforms",
354 ],
355)
356
357x86_transition = transition(
358 implementation = _x86_transition_impl,
359 inputs = [],
360 outputs = [
361 "//command_line_option:platforms",
362 ],
363)
364
365arm64_transition = transition(
366 implementation = _arm64_transition_impl,
367 inputs = [],
368 outputs = [
369 "//command_line_option:platforms",
370 ],
371)
372
373arm_transition = transition(
374 implementation = _arm_transition_impl,
375 inputs = [],
376 outputs = [
377 "//command_line_option:platforms",
378 ],
379)
380
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400381def _mixed_build_root_impl(ctx):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500382 all_files = ctx.files.deps_x86_64 + ctx.files.deps_x86 + ctx.files.deps_arm64 + ctx.files.deps_arm
383 return [DefaultInfo(files = depset(all_files))]
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400384
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500385# Rule representing the root of the build, to depend on all Bazel targets that
386# are required for the build. Building this target will build the entire Bazel
387# build tree.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400388mixed_build_root = rule(
389 implementation = _mixed_build_root_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500390 attrs = {
391 "deps_x86_64" : attr.label_list(cfg = x86_64_transition),
392 "deps_x86" : attr.label_list(cfg = x86_transition),
393 "deps_arm64" : attr.label_list(cfg = arm64_transition),
394 "deps_arm" : attr.label_list(cfg = arm_transition),
395 "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
396 },
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400397)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500398
399def _phony_root_impl(ctx):
400 return []
401
402# Rule to depend on other targets but build nothing.
403# This is useful as follows: building a target of this rule will generate
404# symlink forests for all dependencies of the target, without executing any
405# actions of the build.
406phony_root = rule(
407 implementation = _phony_root_impl,
408 attrs = {"deps" : attr.label_list()},
409)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400410`
411 return []byte(contents)
412}
413
Chris Parsons8ccdb632020-11-17 15:41:01 -0500414// Returns a "canonicalized" corresponding to the given sourcetree-level label.
415// This abstraction is required because a sourcetree label such as //foo/bar:baz
416// must be referenced via the local repository prefix, such as
417// @sourceroot//foo/bar:baz.
418func canonicalizeLabel(label string) string {
419 if strings.HasPrefix(label, "//") {
420 return "@sourceroot" + label
421 } else {
422 return "@sourceroot//" + label
423 }
424}
425
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400426func (context *bazelContext) mainBuildFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500427 // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
428 // architecture mapping.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400429 formatString := `
430# This file is generated by soong_build. Do not edit.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500431load(":main.bzl", "mixed_build_root", "phony_root")
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400432
433mixed_build_root(name = "buildroot",
Chris Parsons8d6e4332021-02-22 16:13:50 -0500434 deps_x86_64 = [%s],
435 deps_x86 = [%s],
436 deps_arm64 = [%s],
437 deps_arm = [%s],
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400438)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500439
440phony_root(name = "phonyroot",
441 deps = [":buildroot"],
442)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400443`
Chris Parsons8d6e4332021-02-22 16:13:50 -0500444 var deps_x86_64 []string = nil
445 var deps_x86 []string = nil
446 var deps_arm64 []string = nil
447 var deps_arm []string = nil
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400448 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500449 labelString := fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label))
450 switch getArchString(val) {
451 case "x86_64":
452 deps_x86_64 = append(deps_x86_64, labelString)
453 case "x86":
454 deps_x86 = append(deps_x86, labelString)
455 case "arm64":
456 deps_arm64 = append(deps_arm64, labelString)
457 case "arm":
458 deps_arm = append(deps_arm, labelString)
459 default:
Liz Kammer15b04e22021-03-04 09:45:21 -0500460 panic(fmt.Sprintf("unhandled architecture %s for %v", getArchString(val), val))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500461 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400462 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400463
Chris Parsons8d6e4332021-02-22 16:13:50 -0500464 return []byte(fmt.Sprintf(formatString,
465 strings.Join(deps_x86_64, ",\n "),
466 strings.Join(deps_x86, ",\n "),
467 strings.Join(deps_arm64, ",\n "),
468 strings.Join(deps_arm, ",\n ")))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400469}
470
Chris Parsons944e7d02021-03-11 11:08:46 -0500471func indent(original string) string {
472 result := ""
473 for _, line := range strings.Split(original, "\n") {
474 result += " " + line + "\n"
475 }
476 return result
477}
478
Chris Parsons808d84c2021-03-09 20:43:32 -0500479// Returns the file contents of the buildroot.cquery file that should be used for the cquery
480// expression in order to obtain information about buildroot and its dependencies.
481// The contents of this file depend on the bazelContext's requests; requests are enumerated
482// and grouped by their request type. The data retrieved for each label depends on its
483// request type.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400484func (context *bazelContext) cqueryStarlarkFileContents() []byte {
Chris Parsons944e7d02021-03-11 11:08:46 -0500485 requestTypeToCqueryIdEntries := map[cquery.RequestType][]string{}
486 for val, _ := range context.requests {
487 cqueryId := getCqueryId(val)
488 mapEntryString := fmt.Sprintf("%q : True", cqueryId)
489 requestTypeToCqueryIdEntries[val.requestType] =
490 append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString)
491 }
492 labelRegistrationMapSection := ""
493 functionDefSection := ""
494 mainSwitchSection := ""
495
496 mapDeclarationFormatString := `
497%s = {
498 %s
499}
500`
501 functionDefFormatString := `
502def %s(target):
503%s
504`
505 mainSwitchSectionFormatString := `
506 if id_string in %s:
507 return id_string + ">>" + %s(target)
508`
509
510 for _, requestType := range cquery.RequestTypes {
511 labelMapName := requestType.Name() + "_Labels"
512 functionName := requestType.Name() + "_Fn"
513 labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString,
514 labelMapName,
515 strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n "))
516 functionDefSection += fmt.Sprintf(functionDefFormatString,
517 functionName,
518 indent(requestType.StarlarkFunctionBody()))
519 mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString,
520 labelMapName, functionName)
521 }
522
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400523 formatString := `
524# This file is generated by soong_build. Do not edit.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400525
Chris Parsons944e7d02021-03-11 11:08:46 -0500526# Label Map Section
527%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500528
Chris Parsons944e7d02021-03-11 11:08:46 -0500529# Function Def Section
530%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500531
532def get_arch(target):
533 buildoptions = build_options(target)
534 platforms = build_options(target)["//command_line_option:platforms"]
535 if len(platforms) != 1:
536 # An individual configured target should have only one platform architecture.
537 # Note that it's fine for there to be multiple architectures for the same label,
538 # but each is its own configured target.
539 fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
540 platform_name = build_options(target)["//command_line_option:platforms"][0].name
541 if platform_name == "host":
542 return "HOST"
543 elif not platform_name.startswith("generic_"):
544 fail("expected platform name of the form 'generic_<arch>', but was " + str(platforms))
545 return "UNKNOWN"
546 return platform_name[len("generic_"):]
547
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400548def format(target):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500549 id_string = str(target.label) + "|" + get_arch(target)
Chris Parsons944e7d02021-03-11 11:08:46 -0500550
551 # Main switch section
552 %s
553 # This target was not requested via cquery, and thus must be a dependency
554 # of a requested target.
555 return id_string + ">>NONE"
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400556`
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400557
Chris Parsons944e7d02021-03-11 11:08:46 -0500558 return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection,
559 mainSwitchSection))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400560}
561
Chris Parsons8ccdb632020-11-17 15:41:01 -0500562// Returns a workspace-relative path containing build-related metadata required
563// for interfacing with Bazel. Example: out/soong/bazel.
564func (context *bazelContext) intermediatesDir() string {
565 return filepath.Join(context.buildDir, "bazel")
566}
567
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400568// Issues commands to Bazel to receive results for all cquery requests
569// queued in the BazelContext.
570func (context *bazelContext) InvokeBazel() error {
571 context.results = make(map[cqueryKey]string)
572
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400573 var cqueryOutput string
Chris Parsons808d84c2021-03-09 20:43:32 -0500574 var cqueryErr string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400575 var err error
Chris Parsons8ccdb632020-11-17 15:41:01 -0500576
Chris Parsons07c1e4a2021-01-19 17:19:16 -0500577 intermediatesDirPath := absolutePath(context.intermediatesDir())
578 if _, err := os.Stat(intermediatesDirPath); os.IsNotExist(err) {
579 err = os.Mkdir(intermediatesDirPath, 0777)
580 }
581
Chris Parsons8ccdb632020-11-17 15:41:01 -0500582 if err != nil {
583 return err
584 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400585 err = ioutil.WriteFile(
Chris Parsons8ccdb632020-11-17 15:41:01 -0500586 absolutePath(filepath.Join(context.intermediatesDir(), "main.bzl")),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400587 context.mainBzlFileContents(), 0666)
588 if err != nil {
589 return err
590 }
591 err = ioutil.WriteFile(
Chris Parsons8ccdb632020-11-17 15:41:01 -0500592 absolutePath(filepath.Join(context.intermediatesDir(), "BUILD.bazel")),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400593 context.mainBuildFileContents(), 0666)
594 if err != nil {
595 return err
596 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800597 cqueryFileRelpath := filepath.Join(context.intermediatesDir(), "buildroot.cquery")
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400598 err = ioutil.WriteFile(
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800599 absolutePath(cqueryFileRelpath),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400600 context.cqueryStarlarkFileContents(), 0666)
601 if err != nil {
602 return err
603 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800604 workspaceFileRelpath := filepath.Join(context.intermediatesDir(), "WORKSPACE.bazel")
Chris Parsons8ccdb632020-11-17 15:41:01 -0500605 err = ioutil.WriteFile(
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800606 absolutePath(workspaceFileRelpath),
Chris Parsons8ccdb632020-11-17 15:41:01 -0500607 context.workspaceFileContents(), 0666)
608 if err != nil {
609 return err
610 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800611 buildrootLabel := "//:buildroot"
Chris Parsons808d84c2021-03-09 20:43:32 -0500612 cqueryOutput, cqueryErr, err = context.issueBazelCommand(bazel.CqueryBuildRootRunName, "cquery",
Chris Parsons8d6e4332021-02-22 16:13:50 -0500613 []string{fmt.Sprintf("kind(rule, deps(%s))", buildrootLabel)},
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400614 "--output=starlark",
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800615 "--starlark:file="+cqueryFileRelpath)
Chris Parsons8d6e4332021-02-22 16:13:50 -0500616 err = ioutil.WriteFile(
617 absolutePath(filepath.Join(context.intermediatesDir(), "cquery.out")),
618 []byte(cqueryOutput), 0666)
619 if err != nil {
620 return err
621 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400622
623 if err != nil {
624 return err
625 }
626
627 cqueryResults := map[string]string{}
628 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
629 if strings.Contains(outputLine, ">>") {
630 splitLine := strings.SplitN(outputLine, ">>", 2)
631 cqueryResults[splitLine[0]] = splitLine[1]
632 }
633 }
634
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400635 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500636 if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400637 context.results[val] = string(cqueryResult)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400638 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500639 return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]",
640 getCqueryId(val), cqueryOutput, cqueryErr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400641 }
642 }
643
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500644 // Issue an aquery command to retrieve action information about the bazel build tree.
645 //
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400646 // TODO(cparsons): Use --target_pattern_file to avoid command line limits.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500647 var aqueryOutput string
Chris Parsons808d84c2021-03-09 20:43:32 -0500648 aqueryOutput, _, err = context.issueBazelCommand(bazel.AqueryBuildRootRunName, "aquery",
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800649 []string{fmt.Sprintf("deps(%s)", buildrootLabel),
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500650 // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
651 // proto sources, which would add a number of unnecessary dependencies.
652 "--output=jsonproto"})
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400653
654 if err != nil {
655 return err
656 }
657
Chris Parsons4f069892021-01-15 12:22:41 -0500658 context.buildStatements, err = bazel.AqueryBuildStatements([]byte(aqueryOutput))
659 if err != nil {
660 return err
661 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500662
663 // Issue a build command of the phony root to generate symlink forests for dependencies of the
664 // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
665 // but some of symlinks may be required to resolve source dependencies of the build.
Chris Parsons808d84c2021-03-09 20:43:32 -0500666 _, _, err = context.issueBazelCommand(bazel.BazelBuildPhonyRootRunName, "build",
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500667 []string{"//:phonyroot"})
668
669 if err != nil {
670 return err
671 }
672
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400673 // Clear requests.
674 context.requests = map[cqueryKey]bool{}
675 return nil
676}
Chris Parsonsa798d962020-10-12 23:44:08 -0400677
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500678func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
679 return context.buildStatements
680}
681
682func (context *bazelContext) OutputBase() string {
683 return context.outputBase
684}
685
Chris Parsonsa798d962020-10-12 23:44:08 -0400686// Singleton used for registering BUILD file ninja dependencies (needed
687// for correctness of builds which use Bazel.
688func BazelSingleton() Singleton {
689 return &bazelSingleton{}
690}
691
692type bazelSingleton struct{}
693
694func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500695 // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
696 if !ctx.Config().BazelContext.BazelEnabled() {
697 return
698 }
Chris Parsonsa798d962020-10-12 23:44:08 -0400699
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500700 // Add ninja file dependencies for files which all bazel invocations require.
701 bazelBuildList := absolutePath(filepath.Join(
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100702 filepath.Dir(bootstrap.CmdlineModuleListFile()), "bazel.list"))
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500703 ctx.AddNinjaFileDeps(bazelBuildList)
704
705 data, err := ioutil.ReadFile(bazelBuildList)
706 if err != nil {
707 ctx.Errorf(err.Error())
708 }
709 files := strings.Split(strings.TrimSpace(string(data)), "\n")
710 for _, file := range files {
711 ctx.AddNinjaFileDeps(file)
712 }
713
714 // Register bazel-owned build statements (obtained from the aquery invocation).
715 for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500716 if len(buildStatement.Command) < 1 {
717 panic(fmt.Sprintf("unhandled build statement: %s", buildStatement))
718 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500719 rule := NewRuleBuilder(pctx, ctx)
720 cmd := rule.Command()
721 cmd.Text(fmt.Sprintf("cd %s/execroot/__main__ && %s",
722 ctx.Config().BazelContext.OutputBase(), buildStatement.Command))
723
724 for _, outputPath := range buildStatement.OutputPaths {
725 cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400726 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500727 for _, inputPath := range buildStatement.InputPaths {
728 cmd.Implicit(PathForBazelOut(ctx, inputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400729 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500730
731 // This is required to silence warnings pertaining to unexpected timestamps. Particularly,
732 // some Bazel builtins (such as files in the bazel_tools directory) have far-future
733 // timestamps. Without restat, Ninja would emit warnings that the input files of a
734 // build statement have later timestamps than the outputs.
735 rule.Restat()
736
Liz Kammer13548d72020-12-16 11:13:30 -0800737 rule.Build(fmt.Sprintf("bazel %d", index), buildStatement.Mnemonic)
Chris Parsonsa798d962020-10-12 23:44:08 -0400738 }
739}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500740
741func getCqueryId(key cqueryKey) string {
742 return canonicalizeLabel(key.label) + "|" + getArchString(key)
743}
744
745func getArchString(key cqueryKey) string {
746 arch := key.archType.Name
747 if len(arch) > 0 {
748 return arch
749 } else {
750 return "x86_64"
751 }
752}