blob: 40a5a7378b4dde021fdcce90a38b66239303c0d0 [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
Patrice Arruda05ab2d02020-12-12 06:24:26 +000031 "android/soong/bazel"
32 "android/soong/shared"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040033)
34
Liz Kammerf29df7c2021-04-02 13:37:39 -040035type cqueryRequest interface {
36 // Name returns a string name for this request type. Such request type names must be unique,
37 // and must only consist of alphanumeric characters.
38 Name() string
39
40 // StarlarkFunctionBody returns a starlark function body to process this request type.
41 // The returned string is the body of a Starlark function which obtains
42 // all request-relevant information about a target and returns a string containing
43 // this information.
44 // The function should have the following properties:
45 // - `target` is the only parameter to this function (a configured target).
46 // - The return value must be a string.
47 // - The function body should not be indented outside of its own scope.
48 StarlarkFunctionBody() string
49}
50
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040051// Map key to describe bazel cquery requests.
52type cqueryKey struct {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040053 label string
Liz Kammerf29df7c2021-04-02 13:37:39 -040054 requestType cqueryRequest
Chris Parsons8d6e4332021-02-22 16:13:50 -050055 archType ArchType
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040056}
57
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000058// bazelHandler is the interface for a helper object related to deferring to Bazel for
59// processing a module (during Bazel mixed builds). Individual module types should define
60// their own bazel handler if they support deferring to Bazel.
61type BazelHandler interface {
62 // Issue query to Bazel to retrieve information about Bazel's view of the current module.
63 // If Bazel returns this information, set module properties on the current module to reflect
64 // the returned information.
65 // Returns true if information was available from Bazel, false if bazel invocation still needs to occur.
66 GenerateBazelBuildActions(ctx ModuleContext, label string) bool
67}
68
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040069type BazelContext interface {
70 // The below methods involve queuing cquery requests to be later invoked
71 // by bazel. If any of these methods return (_, false), then the request
72 // has been queued to be run later.
73
74 // Returns result files built by building the given bazel target label.
Chris Parsons944e7d02021-03-11 11:08:46 -050075 GetOutputFiles(label string, archType ArchType) ([]string, bool)
Chris Parsons8d6e4332021-02-22 16:13:50 -050076
Chris Parsons944e7d02021-03-11 11:08:46 -050077 // TODO(cparsons): Other cquery-related methods should be added here.
78 // Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order).
Liz Kammerfe23bf32021-04-09 16:17:05 -040079 GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool, error)
Liz Kammer3f9e1552021-04-02 18:47:09 -040080
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +000081 // Returns the executable binary resultant from building together the python sources
82 GetPythonBinary(label string, archType ArchType) (string, bool)
83
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040084 // ** End cquery methods
85
86 // Issues commands to Bazel to receive results for all cquery requests
87 // queued in the BazelContext.
88 InvokeBazel() error
89
90 // Returns true if bazel is enabled for the given configuration.
91 BazelEnabled() bool
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050092
93 // Returns the bazel output base (the root directory for all bazel intermediate outputs).
94 OutputBase() string
95
96 // Returns build statements which should get registered to reflect Bazel's outputs.
97 BuildStatementsToRegister() []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040098}
99
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400100type bazelRunner interface {
101 issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand, extraFlags ...string) (string, string, error)
102}
103
104type bazelPaths struct {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400105 homeDir string
106 bazelPath string
107 outputBase string
108 workspaceDir string
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200109 soongOutDir string
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000110 metricsDir string
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400111}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400112
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400113// A context object which tracks queued requests that need to be made to Bazel,
114// and their results after the requests have been made.
115type bazelContext struct {
116 bazelRunner
117 paths *bazelPaths
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400118 requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
119 requestMutex sync.Mutex // requests can be written in parallel
120
121 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500122
123 // Build statements which should get registered to reflect Bazel's outputs.
124 buildStatements []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400125}
126
127var _ BazelContext = &bazelContext{}
128
129// A bazel context to use when Bazel is disabled.
130type noopBazelContext struct{}
131
132var _ BazelContext = noopBazelContext{}
133
134// A bazel context to use for tests.
135type MockBazelContext struct {
Liz Kammera92e8442021-04-07 20:25:21 -0400136 OutputBaseDir string
137
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000138 LabelToOutputFiles map[string][]string
139 LabelToCcInfo map[string]cquery.CcInfo
140 LabelToPythonBinary map[string]string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400141}
142
Chris Parsons944e7d02021-03-11 11:08:46 -0500143func (m MockBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Liz Kammera92e8442021-04-07 20:25:21 -0400144 result, ok := m.LabelToOutputFiles[label]
Chris Parsons8d6e4332021-02-22 16:13:50 -0500145 return result, ok
146}
147
Liz Kammerfe23bf32021-04-09 16:17:05 -0400148func (m MockBazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool, error) {
Liz Kammerb71794d2021-04-09 14:07:00 -0400149 result, ok := m.LabelToCcInfo[label]
Liz Kammerfe23bf32021-04-09 16:17:05 -0400150 return result, ok, nil
Liz Kammer3f9e1552021-04-02 18:47:09 -0400151}
152
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000153func (m MockBazelContext) GetPythonBinary(label string, archType ArchType) (string, bool) {
154 result, ok := m.LabelToPythonBinary[label]
155 return result, ok
156}
157
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400158func (m MockBazelContext) InvokeBazel() error {
159 panic("unimplemented")
160}
161
162func (m MockBazelContext) BazelEnabled() bool {
163 return true
164}
165
Liz Kammera92e8442021-04-07 20:25:21 -0400166func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500167
168func (m MockBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
169 return []bazel.BuildStatement{}
170}
171
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400172var _ BazelContext = MockBazelContext{}
173
Chris Parsons944e7d02021-03-11 11:08:46 -0500174func (bazelCtx *bazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
175 rawString, ok := bazelCtx.cquery(label, cquery.GetOutputFiles, archType)
176 var ret []string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400177 if ok {
Chris Parsons944e7d02021-03-11 11:08:46 -0500178 bazelOutput := strings.TrimSpace(rawString)
Liz Kammerf29df7c2021-04-02 13:37:39 -0400179 ret = cquery.GetOutputFiles.ParseResult(bazelOutput)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400180 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500181 return ret, ok
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400182}
183
Liz Kammerfe23bf32021-04-09 16:17:05 -0400184func (bazelCtx *bazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool, error) {
Liz Kammerb71794d2021-04-09 14:07:00 -0400185 result, ok := bazelCtx.cquery(label, cquery.GetCcInfo, archType)
Liz Kammer3f9e1552021-04-02 18:47:09 -0400186 if !ok {
Liz Kammerfe23bf32021-04-09 16:17:05 -0400187 return cquery.CcInfo{}, ok, nil
Liz Kammer3f9e1552021-04-02 18:47:09 -0400188 }
189
190 bazelOutput := strings.TrimSpace(result)
Liz Kammerfe23bf32021-04-09 16:17:05 -0400191 ret, err := cquery.GetCcInfo.ParseResult(bazelOutput)
192 return ret, ok, err
Liz Kammer3f9e1552021-04-02 18:47:09 -0400193}
194
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000195func (bazelCtx *bazelContext) GetPythonBinary(label string, archType ArchType) (string, bool) {
196 rawString, ok := bazelCtx.cquery(label, cquery.GetPythonBinary, archType)
197 var ret string
198 if ok {
199 bazelOutput := strings.TrimSpace(rawString)
200 ret = cquery.GetPythonBinary.ParseResult(bazelOutput)
201 }
202 return ret, ok
203}
204
Chris Parsons944e7d02021-03-11 11:08:46 -0500205func (n noopBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500206 panic("unimplemented")
207}
208
Liz Kammerfe23bf32021-04-09 16:17:05 -0400209func (n noopBazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool, error) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500210 panic("unimplemented")
211}
212
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000213func (n noopBazelContext) GetPythonBinary(label string, archType ArchType) (string, bool) {
214 panic("unimplemented")
215}
216
Liz Kammer3f9e1552021-04-02 18:47:09 -0400217func (n noopBazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) {
218 panic("unimplemented")
219}
220
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400221func (n noopBazelContext) InvokeBazel() error {
222 panic("unimplemented")
223}
224
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500225func (m noopBazelContext) OutputBase() string {
226 return ""
227}
228
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400229func (n noopBazelContext) BazelEnabled() bool {
230 return false
231}
232
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500233func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
234 return []bazel.BuildStatement{}
235}
236
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400237func NewBazelContext(c *config) (BazelContext, error) {
Chris Parsons8b77a002020-10-27 18:59:25 -0400238 // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds"
239 // are production ready.
Jingwen Chen442b1a42021-06-17 07:02:15 +0000240 if !c.IsEnvTrue("USE_BAZEL_ANALYSIS") {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400241 return noopBazelContext{}, nil
242 }
243
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400244 p, err := bazelPathsFromConfig(c)
245 if err != nil {
246 return nil, err
247 }
248 return &bazelContext{
249 bazelRunner: &builtinBazelRunner{},
250 paths: p,
251 requests: make(map[cqueryKey]bool),
252 }, nil
253}
254
255func bazelPathsFromConfig(c *config) (*bazelPaths, error) {
256 p := bazelPaths{
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200257 soongOutDir: c.soongOutDir,
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400258 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400259 missingEnvVars := []string{}
260 if len(c.Getenv("BAZEL_HOME")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400261 p.homeDir = c.Getenv("BAZEL_HOME")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400262 } else {
263 missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
264 }
265 if len(c.Getenv("BAZEL_PATH")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400266 p.bazelPath = c.Getenv("BAZEL_PATH")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400267 } else {
268 missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
269 }
270 if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400271 p.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400272 } else {
273 missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
274 }
275 if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400276 p.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400277 } else {
278 missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
279 }
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000280 if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400281 p.metricsDir = c.Getenv("BAZEL_METRICS_DIR")
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000282 } else {
283 missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR")
284 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400285 if len(missingEnvVars) > 0 {
286 return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
287 } else {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400288 return &p, nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400289 }
290}
291
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400292func (p *bazelPaths) BazelMetricsDir() string {
293 return p.metricsDir
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000294}
295
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400296func (context *bazelContext) BazelEnabled() bool {
297 return true
298}
299
300// Adds a cquery request to the Bazel request queue, to be later invoked, or
301// returns the result of the given request if the request was already made.
302// If the given request was already made (and the results are available), then
303// returns (result, true). If the request is queued but no results are available,
304// then returns ("", false).
Liz Kammerf29df7c2021-04-02 13:37:39 -0400305func (context *bazelContext) cquery(label string, requestType cqueryRequest,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500306 archType ArchType) (string, bool) {
307 key := cqueryKey{label, requestType, archType}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400308 if result, ok := context.results[key]; ok {
309 return result, true
310 } else {
311 context.requestMutex.Lock()
312 defer context.requestMutex.Unlock()
313 context.requests[key] = true
314 return "", false
315 }
316}
317
318func pwdPrefix() string {
319 // Darwin doesn't have /proc
320 if runtime.GOOS != "darwin" {
321 return "PWD=/proc/self/cwd"
322 }
323 return ""
324}
325
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400326type bazelCommand struct {
327 command string
328 // query or label
329 expression string
330}
331
332type mockBazelRunner struct {
333 bazelCommandResults map[bazelCommand]string
334 commands []bazelCommand
335}
336
337func (r *mockBazelRunner) issueBazelCommand(paths *bazelPaths,
338 runName bazel.RunName,
339 command bazelCommand,
340 extraFlags ...string) (string, string, error) {
341 r.commands = append(r.commands, command)
342 if ret, ok := r.bazelCommandResults[command]; ok {
343 return ret, "", nil
344 }
345 return "", "", nil
346}
347
348type builtinBazelRunner struct{}
349
Chris Parsons808d84c2021-03-09 20:43:32 -0500350// Issues the given bazel command with given build label and additional flags.
351// Returns (stdout, stderr, error). The first and second return values are strings
352// containing the stdout and stderr of the run command, and an error is returned if
353// the invocation returned an error code.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400354func (r *builtinBazelRunner) issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand,
Chris Parsons808d84c2021-03-09 20:43:32 -0500355 extraFlags ...string) (string, string, error) {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200356 cmdFlags := []string{"--output_base=" + absolutePath(paths.outputBase), command.command}
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400357 cmdFlags = append(cmdFlags, command.expression)
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400358 cmdFlags = append(cmdFlags, "--profile="+shared.BazelMetricsFilename(paths, runName))
Jingwen Chen91220d72021-03-24 02:18:33 -0400359
360 // Set default platforms to canonicalized values for mixed builds requests.
361 // If these are set in the bazelrc, they will have values that are
362 // non-canonicalized to @sourceroot labels, and thus be invalid when
363 // referenced from the buildroot.
364 //
365 // The actual platform values here may be overridden by configuration
366 // transitions from the buildroot.
Chris Parsonsee423b02021-02-08 23:04:59 -0500367 cmdFlags = append(cmdFlags,
Liz Kammerc0c66092021-07-26 17:38:47 -0400368 fmt.Sprintf("--platforms=%s", "//build/bazel/platforms:android_target"))
Chris Parsonsee423b02021-02-08 23:04:59 -0500369 cmdFlags = append(cmdFlags,
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200370 fmt.Sprintf("--extra_toolchains=%s", "//prebuilts/clang/host/linux-x86:all"))
Jingwen Chen91220d72021-03-24 02:18:33 -0400371 // This should be parameterized on the host OS, but let's restrict to linux
372 // to keep things simple for now.
373 cmdFlags = append(cmdFlags,
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200374 fmt.Sprintf("--host_platform=%s", "//build/bazel/platforms:linux_x86_64"))
Jingwen Chen91220d72021-03-24 02:18:33 -0400375
Chris Parsons8d6e4332021-02-22 16:13:50 -0500376 // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network.
377 cmdFlags = append(cmdFlags, "--experimental_repository_disable_download")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400378 cmdFlags = append(cmdFlags, extraFlags...)
379
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400380 bazelCmd := exec.Command(paths.bazelPath, cmdFlags...)
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200381 bazelCmd.Dir = absolutePath(paths.syntheticWorkspaceDir())
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200382 bazelCmd.Env = append(os.Environ(),
383 "HOME="+paths.homeDir,
384 pwdPrefix(),
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200385 "BUILD_DIR="+absolutePath(paths.soongOutDir),
Jingwen Chen8c523582021-06-01 11:19:53 +0000386 // Make OUT_DIR absolute here so tools/bazel.sh uses the correct
387 // OUT_DIR at <root>/out, instead of <root>/out/soong/workspace/out.
388 "OUT_DIR="+absolutePath(paths.outDir()),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500389 // Disables local host detection of gcc; toolchain information is defined
390 // explicitly in BUILD files.
391 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1")
Colin Crossff0278b2020-10-09 19:24:15 -0700392 stderr := &bytes.Buffer{}
393 bazelCmd.Stderr = stderr
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400394
395 if output, err := bazelCmd.Output(); err != nil {
Chris Parsons808d84c2021-03-09 20:43:32 -0500396 return "", string(stderr.Bytes()),
397 fmt.Errorf("bazel command failed. command: [%s], env: [%s], error [%s]", bazelCmd, bazelCmd.Env, stderr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400398 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500399 return string(output), string(stderr.Bytes()), nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400400 }
401}
402
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400403func (context *bazelContext) mainBzlFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500404 // TODO(cparsons): Define configuration transitions programmatically based
405 // on available archs.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400406 contents := `
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500407#####################################################
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400408# This file is generated by soong_build. Do not edit.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500409#####################################################
410
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400411def _config_node_transition_impl(settings, attr):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500412 return {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200413 "//command_line_option:platforms": "@//build/bazel/platforms:android_%s" % attr.arch,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500414 }
415
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400416_config_node_transition = transition(
417 implementation = _config_node_transition_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500418 inputs = [],
419 outputs = [
420 "//command_line_option:platforms",
421 ],
422)
423
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400424def _passthrough_rule_impl(ctx):
425 return [DefaultInfo(files = depset(ctx.files.deps))]
426
427config_node = rule(
428 implementation = _passthrough_rule_impl,
429 attrs = {
430 "arch" : attr.string(mandatory = True),
431 "deps" : attr.label_list(cfg = _config_node_transition),
432 "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
433 },
Chris Parsons8d6e4332021-02-22 16:13:50 -0500434)
435
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400436
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500437# Rule representing the root of the build, to depend on all Bazel targets that
438# are required for the build. Building this target will build the entire Bazel
439# build tree.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400440mixed_build_root = rule(
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400441 implementation = _passthrough_rule_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500442 attrs = {
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400443 "deps" : attr.label_list(),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500444 },
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400445)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500446
447def _phony_root_impl(ctx):
448 return []
449
450# Rule to depend on other targets but build nothing.
451# This is useful as follows: building a target of this rule will generate
452# symlink forests for all dependencies of the target, without executing any
453# actions of the build.
454phony_root = rule(
455 implementation = _phony_root_impl,
456 attrs = {"deps" : attr.label_list()},
457)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400458`
459 return []byte(contents)
460}
461
462func (context *bazelContext) mainBuildFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500463 // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
464 // architecture mapping.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400465 formatString := `
466# This file is generated by soong_build. Do not edit.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400467load(":main.bzl", "config_node", "mixed_build_root", "phony_root")
468
469%s
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400470
471mixed_build_root(name = "buildroot",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400472 deps = [%s],
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400473)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500474
475phony_root(name = "phonyroot",
476 deps = [":buildroot"],
477)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400478`
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400479 configNodeFormatString := `
480config_node(name = "%s",
481 arch = "%s",
482 deps = [%s],
483)
484`
485
486 configNodesSection := ""
487
488 labelsByArch := map[string][]string{}
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400489 for val, _ := range context.requests {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200490 labelString := fmt.Sprintf("\"@%s\"", val.label)
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400491 archString := getArchString(val)
492 labelsByArch[archString] = append(labelsByArch[archString], labelString)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400493 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400494
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400495 configNodeLabels := []string{}
496 for archString, labels := range labelsByArch {
497 configNodeLabels = append(configNodeLabels, fmt.Sprintf("\":%s\"", archString))
498 labelsString := strings.Join(labels, ",\n ")
499 configNodesSection += fmt.Sprintf(configNodeFormatString, archString, archString, labelsString)
500 }
501
502 return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(configNodeLabels, ",\n ")))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400503}
504
Chris Parsons944e7d02021-03-11 11:08:46 -0500505func indent(original string) string {
506 result := ""
507 for _, line := range strings.Split(original, "\n") {
508 result += " " + line + "\n"
509 }
510 return result
511}
512
Chris Parsons808d84c2021-03-09 20:43:32 -0500513// Returns the file contents of the buildroot.cquery file that should be used for the cquery
514// expression in order to obtain information about buildroot and its dependencies.
515// The contents of this file depend on the bazelContext's requests; requests are enumerated
516// and grouped by their request type. The data retrieved for each label depends on its
517// request type.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400518func (context *bazelContext) cqueryStarlarkFileContents() []byte {
Liz Kammerf29df7c2021-04-02 13:37:39 -0400519 requestTypeToCqueryIdEntries := map[cqueryRequest][]string{}
Chris Parsons944e7d02021-03-11 11:08:46 -0500520 for val, _ := range context.requests {
521 cqueryId := getCqueryId(val)
522 mapEntryString := fmt.Sprintf("%q : True", cqueryId)
523 requestTypeToCqueryIdEntries[val.requestType] =
524 append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString)
525 }
526 labelRegistrationMapSection := ""
527 functionDefSection := ""
528 mainSwitchSection := ""
529
530 mapDeclarationFormatString := `
531%s = {
532 %s
533}
534`
535 functionDefFormatString := `
536def %s(target):
537%s
538`
539 mainSwitchSectionFormatString := `
540 if id_string in %s:
541 return id_string + ">>" + %s(target)
542`
543
Liz Kammer66ffdb72021-04-02 13:26:07 -0400544 for requestType, _ := range requestTypeToCqueryIdEntries {
Chris Parsons944e7d02021-03-11 11:08:46 -0500545 labelMapName := requestType.Name() + "_Labels"
546 functionName := requestType.Name() + "_Fn"
547 labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString,
548 labelMapName,
549 strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n "))
550 functionDefSection += fmt.Sprintf(functionDefFormatString,
551 functionName,
552 indent(requestType.StarlarkFunctionBody()))
553 mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString,
554 labelMapName, functionName)
555 }
556
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400557 formatString := `
558# This file is generated by soong_build. Do not edit.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400559
Chris Parsons944e7d02021-03-11 11:08:46 -0500560# Label Map Section
561%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500562
Chris Parsons944e7d02021-03-11 11:08:46 -0500563# Function Def Section
564%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500565
566def get_arch(target):
567 buildoptions = build_options(target)
568 platforms = build_options(target)["//command_line_option:platforms"]
569 if len(platforms) != 1:
570 # An individual configured target should have only one platform architecture.
571 # Note that it's fine for there to be multiple architectures for the same label,
572 # but each is its own configured target.
573 fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
574 platform_name = build_options(target)["//command_line_option:platforms"][0].name
575 if platform_name == "host":
576 return "HOST"
Chris Parsons94a0bba2021-06-04 15:03:47 -0400577 elif platform_name.startswith("android_"):
578 return platform_name[len("android_"):]
579 elif platform_name.startswith("linux_"):
580 return platform_name[len("linux_"):]
581 else:
582 fail("expected platform name of the form 'android_<arch>' or 'linux_<arch>', but was " + str(platforms))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500583 return "UNKNOWN"
Chris Parsons8d6e4332021-02-22 16:13:50 -0500584
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400585def format(target):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500586 id_string = str(target.label) + "|" + get_arch(target)
Chris Parsons944e7d02021-03-11 11:08:46 -0500587
588 # Main switch section
589 %s
590 # This target was not requested via cquery, and thus must be a dependency
591 # of a requested target.
592 return id_string + ">>NONE"
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400593`
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400594
Chris Parsons944e7d02021-03-11 11:08:46 -0500595 return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection,
596 mainSwitchSection))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400597}
598
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200599// Returns a path containing build-related metadata required for interfacing
600// with Bazel. Example: out/soong/bazel.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400601func (p *bazelPaths) intermediatesDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200602 return filepath.Join(p.soongOutDir, "bazel")
Chris Parsons8ccdb632020-11-17 15:41:01 -0500603}
604
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200605// Returns the path where the contents of the @soong_injection repository live.
606// It is used by Soong to tell Bazel things it cannot over the command line.
607func (p *bazelPaths) injectedFilesDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200608 return filepath.Join(p.soongOutDir, bazel.SoongInjectionDirName)
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200609}
610
611// Returns the path of the synthetic Bazel workspace that contains a symlink
612// forest composed the whole source tree and BUILD files generated by bp2build.
613func (p *bazelPaths) syntheticWorkspaceDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200614 return filepath.Join(p.soongOutDir, "workspace")
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200615}
616
Jingwen Chen8c523582021-06-01 11:19:53 +0000617// Returns the path to the top level out dir ($OUT_DIR).
618func (p *bazelPaths) outDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200619 return filepath.Dir(p.soongOutDir)
Jingwen Chen8c523582021-06-01 11:19:53 +0000620}
621
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400622// Issues commands to Bazel to receive results for all cquery requests
623// queued in the BazelContext.
624func (context *bazelContext) InvokeBazel() error {
625 context.results = make(map[cqueryKey]string)
626
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400627 var cqueryOutput string
Chris Parsons808d84c2021-03-09 20:43:32 -0500628 var cqueryErr string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400629 var err error
Chris Parsons8ccdb632020-11-17 15:41:01 -0500630
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200631 soongInjectionPath := absolutePath(context.paths.injectedFilesDir())
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200632 mixedBuildsPath := filepath.Join(soongInjectionPath, "mixed_builds")
633 if _, err := os.Stat(mixedBuildsPath); os.IsNotExist(err) {
634 err = os.MkdirAll(mixedBuildsPath, 0777)
Chris Parsons07c1e4a2021-01-19 17:19:16 -0500635 }
Chris Parsons8ccdb632020-11-17 15:41:01 -0500636 if err != nil {
637 return err
638 }
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200639
640 err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "WORKSPACE.bazel"), []byte{}, 0666)
641 if err != nil {
642 return err
643 }
644
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400645 err = ioutil.WriteFile(
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200646 filepath.Join(mixedBuildsPath, "main.bzl"),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400647 context.mainBzlFileContents(), 0666)
648 if err != nil {
649 return err
650 }
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200651
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400652 err = ioutil.WriteFile(
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200653 filepath.Join(mixedBuildsPath, "BUILD.bazel"),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400654 context.mainBuildFileContents(), 0666)
655 if err != nil {
656 return err
657 }
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200658 cqueryFileRelpath := filepath.Join(context.paths.injectedFilesDir(), "buildroot.cquery")
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400659 err = ioutil.WriteFile(
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800660 absolutePath(cqueryFileRelpath),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400661 context.cqueryStarlarkFileContents(), 0666)
662 if err != nil {
663 return err
664 }
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200665 buildrootLabel := "@soong_injection//mixed_builds:buildroot"
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400666 cqueryOutput, cqueryErr, err = context.issueBazelCommand(
667 context.paths,
668 bazel.CqueryBuildRootRunName,
669 bazelCommand{"cquery", fmt.Sprintf("kind(rule, deps(%s))", buildrootLabel)},
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400670 "--output=starlark",
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200671 "--starlark:file="+absolutePath(cqueryFileRelpath))
672 err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "cquery.out"),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500673 []byte(cqueryOutput), 0666)
674 if err != nil {
675 return err
676 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400677
678 if err != nil {
679 return err
680 }
681
682 cqueryResults := map[string]string{}
683 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
684 if strings.Contains(outputLine, ">>") {
685 splitLine := strings.SplitN(outputLine, ">>", 2)
686 cqueryResults[splitLine[0]] = splitLine[1]
687 }
688 }
689
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400690 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500691 if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400692 context.results[val] = string(cqueryResult)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400693 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500694 return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]",
695 getCqueryId(val), cqueryOutput, cqueryErr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400696 }
697 }
698
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500699 // Issue an aquery command to retrieve action information about the bazel build tree.
700 //
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400701 // TODO(cparsons): Use --target_pattern_file to avoid command line limits.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500702 var aqueryOutput string
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400703 aqueryOutput, _, err = context.issueBazelCommand(
704 context.paths,
705 bazel.AqueryBuildRootRunName,
706 bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)},
707 // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
708 // proto sources, which would add a number of unnecessary dependencies.
709 "--output=jsonproto")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400710
711 if err != nil {
712 return err
713 }
714
Chris Parsons4f069892021-01-15 12:22:41 -0500715 context.buildStatements, err = bazel.AqueryBuildStatements([]byte(aqueryOutput))
716 if err != nil {
717 return err
718 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500719
720 // Issue a build command of the phony root to generate symlink forests for dependencies of the
721 // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
722 // but some of symlinks may be required to resolve source dependencies of the build.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400723 _, _, err = context.issueBazelCommand(
724 context.paths,
725 bazel.BazelBuildPhonyRootRunName,
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200726 bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"})
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500727
728 if err != nil {
729 return err
730 }
731
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400732 // Clear requests.
733 context.requests = map[cqueryKey]bool{}
734 return nil
735}
Chris Parsonsa798d962020-10-12 23:44:08 -0400736
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500737func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
738 return context.buildStatements
739}
740
741func (context *bazelContext) OutputBase() string {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400742 return context.paths.outputBase
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500743}
744
Chris Parsonsa798d962020-10-12 23:44:08 -0400745// Singleton used for registering BUILD file ninja dependencies (needed
746// for correctness of builds which use Bazel.
747func BazelSingleton() Singleton {
748 return &bazelSingleton{}
749}
750
751type bazelSingleton struct{}
752
753func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500754 // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
755 if !ctx.Config().BazelContext.BazelEnabled() {
756 return
757 }
Chris Parsonsa798d962020-10-12 23:44:08 -0400758
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500759 // Add ninja file dependencies for files which all bazel invocations require.
760 bazelBuildList := absolutePath(filepath.Join(
Lukacs T. Berkif9008072021-08-16 15:24:48 +0200761 filepath.Dir(ctx.Config().moduleListFile), "bazel.list"))
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500762 ctx.AddNinjaFileDeps(bazelBuildList)
763
764 data, err := ioutil.ReadFile(bazelBuildList)
765 if err != nil {
766 ctx.Errorf(err.Error())
767 }
768 files := strings.Split(strings.TrimSpace(string(data)), "\n")
769 for _, file := range files {
770 ctx.AddNinjaFileDeps(file)
771 }
772
773 // Register bazel-owned build statements (obtained from the aquery invocation).
774 for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500775 if len(buildStatement.Command) < 1 {
Rupert Shuttlewortha29903f2021-04-06 16:17:33 +0000776 panic(fmt.Sprintf("unhandled build statement: %v", buildStatement))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500777 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500778 rule := NewRuleBuilder(pctx, ctx)
779 cmd := rule.Command()
Chris Parsons94a0bba2021-06-04 15:03:47 -0400780
781 // cd into Bazel's execution root, which is the action cwd.
782 cmd.Text(fmt.Sprintf("cd %s/execroot/__main__ && ", ctx.Config().BazelContext.OutputBase()))
783
784 for _, pair := range buildStatement.Env {
785 // Set per-action env variables, if any.
786 cmd.Flag(pair.Key + "=" + pair.Value)
787 }
788
789 // The actual Bazel action.
790 cmd.Text(" " + buildStatement.Command)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500791
792 for _, outputPath := range buildStatement.OutputPaths {
793 cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400794 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500795 for _, inputPath := range buildStatement.InputPaths {
796 cmd.Implicit(PathForBazelOut(ctx, inputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400797 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500798
Liz Kammerde116852021-03-25 16:42:37 -0400799 if depfile := buildStatement.Depfile; depfile != nil {
800 cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile))
801 }
802
Liz Kammerc49e6822021-06-08 15:04:11 -0400803 for _, symlinkPath := range buildStatement.SymlinkPaths {
804 cmd.ImplicitSymlinkOutput(PathForBazelOut(ctx, symlinkPath))
805 }
806
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500807 // This is required to silence warnings pertaining to unexpected timestamps. Particularly,
808 // some Bazel builtins (such as files in the bazel_tools directory) have far-future
809 // timestamps. Without restat, Ninja would emit warnings that the input files of a
810 // build statement have later timestamps than the outputs.
811 rule.Restat()
812
Liz Kammer13548d72020-12-16 11:13:30 -0800813 rule.Build(fmt.Sprintf("bazel %d", index), buildStatement.Mnemonic)
Chris Parsonsa798d962020-10-12 23:44:08 -0400814 }
815}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500816
817func getCqueryId(key cqueryKey) string {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200818 return key.label + "|" + getArchString(key)
Chris Parsons8d6e4332021-02-22 16:13:50 -0500819}
820
821func getArchString(key cqueryKey) string {
822 arch := key.archType.Name
823 if len(arch) > 0 {
824 return arch
825 } else {
826 return "x86_64"
827 }
828}