blob: 80e127c29d4bb04b0517b9eed75a15ab506de59f [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"
Jingwen Chen1e347862021-09-02 12:11:49 +000030 "android/soong/shared"
Liz Kammer8206d4f2021-03-03 16:40:52 -050031
Patrice Arruda05ab2d02020-12-12 06:24:26 +000032 "android/soong/bazel"
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 Parsons787fb362021-10-14 18:43:51 -040051// Portion of cquery map key to describe target configuration.
52type configKey struct {
53 archType ArchType
54 osType OsType
55}
56
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040057// Map key to describe bazel cquery requests.
58type cqueryKey struct {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -040059 label string
Liz Kammerf29df7c2021-04-02 13:37:39 -040060 requestType cqueryRequest
Chris Parsons787fb362021-10-14 18:43:51 -040061 configKey configKey
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040062}
63
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000064// bazelHandler is the interface for a helper object related to deferring to Bazel for
65// processing a module (during Bazel mixed builds). Individual module types should define
66// their own bazel handler if they support deferring to Bazel.
67type BazelHandler interface {
68 // Issue query to Bazel to retrieve information about Bazel's view of the current module.
69 // If Bazel returns this information, set module properties on the current module to reflect
70 // the returned information.
71 // Returns true if information was available from Bazel, false if bazel invocation still needs to occur.
72 GenerateBazelBuildActions(ctx ModuleContext, label string) bool
73}
74
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040075type BazelContext interface {
76 // The below methods involve queuing cquery requests to be later invoked
77 // by bazel. If any of these methods return (_, false), then the request
78 // has been queued to be run later.
79
80 // Returns result files built by building the given bazel target label.
Chris Parsons787fb362021-10-14 18:43:51 -040081 GetOutputFiles(label string, cfgKey configKey) ([]string, bool)
Chris Parsons8d6e4332021-02-22 16:13:50 -050082
Chris Parsons944e7d02021-03-11 11:08:46 -050083 // TODO(cparsons): Other cquery-related methods should be added here.
84 // Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order).
Chris Parsons787fb362021-10-14 18:43:51 -040085 GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, bool, error)
Liz Kammer3f9e1552021-04-02 18:47:09 -040086
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +000087 // Returns the executable binary resultant from building together the python sources
Chris Parsons787fb362021-10-14 18:43:51 -040088 GetPythonBinary(label string, cfgKey configKey) (string, bool)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +000089
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040090 // ** End cquery methods
91
92 // Issues commands to Bazel to receive results for all cquery requests
93 // queued in the BazelContext.
94 InvokeBazel() error
95
96 // Returns true if bazel is enabled for the given configuration.
97 BazelEnabled() bool
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050098
99 // Returns the bazel output base (the root directory for all bazel intermediate outputs).
100 OutputBase() string
101
102 // Returns build statements which should get registered to reflect Bazel's outputs.
103 BuildStatementsToRegister() []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400104}
105
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400106type bazelRunner interface {
107 issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand, extraFlags ...string) (string, string, error)
108}
109
110type bazelPaths struct {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400111 homeDir string
112 bazelPath string
113 outputBase string
114 workspaceDir string
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200115 soongOutDir string
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000116 metricsDir string
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400117}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400118
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400119// A context object which tracks queued requests that need to be made to Bazel,
120// and their results after the requests have been made.
121type bazelContext struct {
122 bazelRunner
123 paths *bazelPaths
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400124 requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
125 requestMutex sync.Mutex // requests can be written in parallel
126
127 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500128
129 // Build statements which should get registered to reflect Bazel's outputs.
130 buildStatements []bazel.BuildStatement
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400131}
132
133var _ BazelContext = &bazelContext{}
134
135// A bazel context to use when Bazel is disabled.
136type noopBazelContext struct{}
137
138var _ BazelContext = noopBazelContext{}
139
140// A bazel context to use for tests.
141type MockBazelContext struct {
Liz Kammera92e8442021-04-07 20:25:21 -0400142 OutputBaseDir string
143
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000144 LabelToOutputFiles map[string][]string
145 LabelToCcInfo map[string]cquery.CcInfo
146 LabelToPythonBinary map[string]string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400147}
148
Chris Parsons787fb362021-10-14 18:43:51 -0400149func (m MockBazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, bool) {
Liz Kammera92e8442021-04-07 20:25:21 -0400150 result, ok := m.LabelToOutputFiles[label]
Chris Parsons8d6e4332021-02-22 16:13:50 -0500151 return result, ok
152}
153
Chris Parsons787fb362021-10-14 18:43:51 -0400154func (m MockBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, bool, error) {
Liz Kammerb71794d2021-04-09 14:07:00 -0400155 result, ok := m.LabelToCcInfo[label]
Liz Kammerfe23bf32021-04-09 16:17:05 -0400156 return result, ok, nil
Liz Kammer3f9e1552021-04-02 18:47:09 -0400157}
158
Chris Parsons787fb362021-10-14 18:43:51 -0400159func (m MockBazelContext) GetPythonBinary(label string, cfgKey configKey) (string, bool) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000160 result, ok := m.LabelToPythonBinary[label]
161 return result, ok
162}
163
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400164func (m MockBazelContext) InvokeBazel() error {
165 panic("unimplemented")
166}
167
168func (m MockBazelContext) BazelEnabled() bool {
169 return true
170}
171
Liz Kammera92e8442021-04-07 20:25:21 -0400172func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500173
174func (m MockBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
175 return []bazel.BuildStatement{}
176}
177
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400178var _ BazelContext = MockBazelContext{}
179
Chris Parsons787fb362021-10-14 18:43:51 -0400180func (bazelCtx *bazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, bool) {
181 rawString, ok := bazelCtx.cquery(label, cquery.GetOutputFiles, cfgKey)
Chris Parsons944e7d02021-03-11 11:08:46 -0500182 var ret []string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400183 if ok {
Chris Parsons944e7d02021-03-11 11:08:46 -0500184 bazelOutput := strings.TrimSpace(rawString)
Liz Kammerf29df7c2021-04-02 13:37:39 -0400185 ret = cquery.GetOutputFiles.ParseResult(bazelOutput)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400186 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500187 return ret, ok
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400188}
189
Chris Parsons787fb362021-10-14 18:43:51 -0400190func (bazelCtx *bazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, bool, error) {
191 result, ok := bazelCtx.cquery(label, cquery.GetCcInfo, cfgKey)
Liz Kammer3f9e1552021-04-02 18:47:09 -0400192 if !ok {
Liz Kammerfe23bf32021-04-09 16:17:05 -0400193 return cquery.CcInfo{}, ok, nil
Liz Kammer3f9e1552021-04-02 18:47:09 -0400194 }
195
196 bazelOutput := strings.TrimSpace(result)
Liz Kammerfe23bf32021-04-09 16:17:05 -0400197 ret, err := cquery.GetCcInfo.ParseResult(bazelOutput)
198 return ret, ok, err
Liz Kammer3f9e1552021-04-02 18:47:09 -0400199}
200
Chris Parsons787fb362021-10-14 18:43:51 -0400201func (bazelCtx *bazelContext) GetPythonBinary(label string, cfgKey configKey) (string, bool) {
202 rawString, ok := bazelCtx.cquery(label, cquery.GetPythonBinary, cfgKey)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000203 var ret string
204 if ok {
205 bazelOutput := strings.TrimSpace(rawString)
206 ret = cquery.GetPythonBinary.ParseResult(bazelOutput)
207 }
208 return ret, ok
209}
210
Chris Parsons787fb362021-10-14 18:43:51 -0400211func (n noopBazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, bool) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500212 panic("unimplemented")
213}
214
Chris Parsons787fb362021-10-14 18:43:51 -0400215func (n noopBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, bool, error) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500216 panic("unimplemented")
217}
218
Chris Parsons787fb362021-10-14 18:43:51 -0400219func (n noopBazelContext) GetPythonBinary(label string, cfgKey configKey) (string, bool) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000220 panic("unimplemented")
221}
222
Liz Kammer3f9e1552021-04-02 18:47:09 -0400223func (n noopBazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) {
224 panic("unimplemented")
225}
226
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400227func (n noopBazelContext) InvokeBazel() error {
228 panic("unimplemented")
229}
230
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500231func (m noopBazelContext) OutputBase() string {
232 return ""
233}
234
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400235func (n noopBazelContext) BazelEnabled() bool {
236 return false
237}
238
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500239func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
240 return []bazel.BuildStatement{}
241}
242
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400243func NewBazelContext(c *config) (BazelContext, error) {
Chris Parsons8b77a002020-10-27 18:59:25 -0400244 // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds"
245 // are production ready.
Jingwen Chen442b1a42021-06-17 07:02:15 +0000246 if !c.IsEnvTrue("USE_BAZEL_ANALYSIS") {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400247 return noopBazelContext{}, nil
248 }
249
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400250 p, err := bazelPathsFromConfig(c)
251 if err != nil {
252 return nil, err
253 }
254 return &bazelContext{
255 bazelRunner: &builtinBazelRunner{},
256 paths: p,
257 requests: make(map[cqueryKey]bool),
258 }, nil
259}
260
261func bazelPathsFromConfig(c *config) (*bazelPaths, error) {
262 p := bazelPaths{
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200263 soongOutDir: c.soongOutDir,
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400264 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400265 missingEnvVars := []string{}
266 if len(c.Getenv("BAZEL_HOME")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400267 p.homeDir = c.Getenv("BAZEL_HOME")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400268 } else {
269 missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
270 }
271 if len(c.Getenv("BAZEL_PATH")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400272 p.bazelPath = c.Getenv("BAZEL_PATH")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400273 } else {
274 missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
275 }
276 if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400277 p.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400278 } else {
279 missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
280 }
281 if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400282 p.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400283 } else {
284 missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
285 }
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000286 if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400287 p.metricsDir = c.Getenv("BAZEL_METRICS_DIR")
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000288 } else {
289 missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR")
290 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400291 if len(missingEnvVars) > 0 {
292 return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
293 } else {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400294 return &p, nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400295 }
296}
297
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400298func (p *bazelPaths) BazelMetricsDir() string {
299 return p.metricsDir
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000300}
301
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400302func (context *bazelContext) BazelEnabled() bool {
303 return true
304}
305
306// Adds a cquery request to the Bazel request queue, to be later invoked, or
307// returns the result of the given request if the request was already made.
308// If the given request was already made (and the results are available), then
309// returns (result, true). If the request is queued but no results are available,
310// then returns ("", false).
Liz Kammerf29df7c2021-04-02 13:37:39 -0400311func (context *bazelContext) cquery(label string, requestType cqueryRequest,
Chris Parsons787fb362021-10-14 18:43:51 -0400312 cfgKey configKey) (string, bool) {
313 key := cqueryKey{label, requestType, cfgKey}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400314 if result, ok := context.results[key]; ok {
315 return result, true
316 } else {
317 context.requestMutex.Lock()
318 defer context.requestMutex.Unlock()
319 context.requests[key] = true
320 return "", false
321 }
322}
323
324func pwdPrefix() string {
325 // Darwin doesn't have /proc
326 if runtime.GOOS != "darwin" {
327 return "PWD=/proc/self/cwd"
328 }
329 return ""
330}
331
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400332type bazelCommand struct {
333 command string
334 // query or label
335 expression string
336}
337
338type mockBazelRunner struct {
339 bazelCommandResults map[bazelCommand]string
340 commands []bazelCommand
341}
342
343func (r *mockBazelRunner) issueBazelCommand(paths *bazelPaths,
344 runName bazel.RunName,
345 command bazelCommand,
346 extraFlags ...string) (string, string, error) {
347 r.commands = append(r.commands, command)
348 if ret, ok := r.bazelCommandResults[command]; ok {
349 return ret, "", nil
350 }
351 return "", "", nil
352}
353
354type builtinBazelRunner struct{}
355
Chris Parsons808d84c2021-03-09 20:43:32 -0500356// Issues the given bazel command with given build label and additional flags.
357// Returns (stdout, stderr, error). The first and second return values are strings
358// containing the stdout and stderr of the run command, and an error is returned if
359// the invocation returned an error code.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400360func (r *builtinBazelRunner) issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand,
Chris Parsons808d84c2021-03-09 20:43:32 -0500361 extraFlags ...string) (string, string, error) {
Romain Jobredeaux41fd5e42021-08-27 15:59:39 +0000362 cmdFlags := []string{
363 // --noautodetect_server_javabase has the practical consequence of preventing Bazel from
364 // attempting to download rules_java, which is incompatible with
365 // --experimental_repository_disable_download set further below.
366 // rules_java is also not needed until mixed builds start building java targets.
367 // TODO(b/197958133): Once rules_java is pulled into AOSP, remove this flag.
368 "--noautodetect_server_javabase",
369 "--output_base=" + absolutePath(paths.outputBase),
370 command.command,
371 }
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400372 cmdFlags = append(cmdFlags, command.expression)
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400373 cmdFlags = append(cmdFlags, "--profile="+shared.BazelMetricsFilename(paths, runName))
Jingwen Chen91220d72021-03-24 02:18:33 -0400374
375 // Set default platforms to canonicalized values for mixed builds requests.
376 // If these are set in the bazelrc, they will have values that are
377 // non-canonicalized to @sourceroot labels, and thus be invalid when
378 // referenced from the buildroot.
379 //
380 // The actual platform values here may be overridden by configuration
381 // transitions from the buildroot.
Chris Parsonsee423b02021-02-08 23:04:59 -0500382 cmdFlags = append(cmdFlags,
Liz Kammerc0c66092021-07-26 17:38:47 -0400383 fmt.Sprintf("--platforms=%s", "//build/bazel/platforms:android_target"))
Chris Parsonsee423b02021-02-08 23:04:59 -0500384 cmdFlags = append(cmdFlags,
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200385 fmt.Sprintf("--extra_toolchains=%s", "//prebuilts/clang/host/linux-x86:all"))
Jingwen Chen91220d72021-03-24 02:18:33 -0400386 // This should be parameterized on the host OS, but let's restrict to linux
387 // to keep things simple for now.
388 cmdFlags = append(cmdFlags,
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200389 fmt.Sprintf("--host_platform=%s", "//build/bazel/platforms:linux_x86_64"))
Jingwen Chen91220d72021-03-24 02:18:33 -0400390
Chris Parsons8d6e4332021-02-22 16:13:50 -0500391 // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network.
392 cmdFlags = append(cmdFlags, "--experimental_repository_disable_download")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400393 cmdFlags = append(cmdFlags, extraFlags...)
394
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400395 bazelCmd := exec.Command(paths.bazelPath, cmdFlags...)
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200396 bazelCmd.Dir = absolutePath(paths.syntheticWorkspaceDir())
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200397 bazelCmd.Env = append(os.Environ(),
398 "HOME="+paths.homeDir,
399 pwdPrefix(),
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200400 "BUILD_DIR="+absolutePath(paths.soongOutDir),
Jingwen Chen8c523582021-06-01 11:19:53 +0000401 // Make OUT_DIR absolute here so tools/bazel.sh uses the correct
402 // OUT_DIR at <root>/out, instead of <root>/out/soong/workspace/out.
403 "OUT_DIR="+absolutePath(paths.outDir()),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500404 // Disables local host detection of gcc; toolchain information is defined
405 // explicitly in BUILD files.
406 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1")
Colin Crossff0278b2020-10-09 19:24:15 -0700407 stderr := &bytes.Buffer{}
408 bazelCmd.Stderr = stderr
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400409
410 if output, err := bazelCmd.Output(); err != nil {
Chris Parsons808d84c2021-03-09 20:43:32 -0500411 return "", string(stderr.Bytes()),
412 fmt.Errorf("bazel command failed. command: [%s], env: [%s], error [%s]", bazelCmd, bazelCmd.Env, stderr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400413 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500414 return string(output), string(stderr.Bytes()), nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400415 }
416}
417
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400418func (context *bazelContext) mainBzlFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500419 // TODO(cparsons): Define configuration transitions programmatically based
420 // on available archs.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400421 contents := `
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500422#####################################################
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400423# This file is generated by soong_build. Do not edit.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500424#####################################################
425
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400426def _config_node_transition_impl(settings, attr):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500427 return {
Chris Parsons787fb362021-10-14 18:43:51 -0400428 "//command_line_option:platforms": "@//build/bazel/platforms:%s_%s" % (attr.os, attr.arch),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500429 }
430
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400431_config_node_transition = transition(
432 implementation = _config_node_transition_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500433 inputs = [],
434 outputs = [
435 "//command_line_option:platforms",
436 ],
437)
438
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400439def _passthrough_rule_impl(ctx):
440 return [DefaultInfo(files = depset(ctx.files.deps))]
441
442config_node = rule(
443 implementation = _passthrough_rule_impl,
444 attrs = {
445 "arch" : attr.string(mandatory = True),
Chris Parsons787fb362021-10-14 18:43:51 -0400446 "os" : attr.string(mandatory = True),
447 "deps" : attr.label_list(cfg = _config_node_transition, allow_files = True),
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400448 "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
449 },
Chris Parsons8d6e4332021-02-22 16:13:50 -0500450)
451
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400452
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500453# Rule representing the root of the build, to depend on all Bazel targets that
454# are required for the build. Building this target will build the entire Bazel
455# build tree.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400456mixed_build_root = rule(
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400457 implementation = _passthrough_rule_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500458 attrs = {
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400459 "deps" : attr.label_list(),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500460 },
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400461)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500462
463def _phony_root_impl(ctx):
464 return []
465
466# Rule to depend on other targets but build nothing.
467# This is useful as follows: building a target of this rule will generate
468# symlink forests for all dependencies of the target, without executing any
469# actions of the build.
470phony_root = rule(
471 implementation = _phony_root_impl,
472 attrs = {"deps" : attr.label_list()},
473)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400474`
475 return []byte(contents)
476}
477
478func (context *bazelContext) mainBuildFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500479 // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
480 // architecture mapping.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400481 formatString := `
482# This file is generated by soong_build. Do not edit.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400483load(":main.bzl", "config_node", "mixed_build_root", "phony_root")
484
485%s
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400486
487mixed_build_root(name = "buildroot",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400488 deps = [%s],
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400489)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500490
491phony_root(name = "phonyroot",
492 deps = [":buildroot"],
493)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400494`
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400495 configNodeFormatString := `
496config_node(name = "%s",
497 arch = "%s",
Chris Parsons787fb362021-10-14 18:43:51 -0400498 os = "%s",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400499 deps = [%s],
500)
501`
502
503 configNodesSection := ""
504
Chris Parsons787fb362021-10-14 18:43:51 -0400505 labelsByConfig := map[string][]string{}
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400506 for val, _ := range context.requests {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200507 labelString := fmt.Sprintf("\"@%s\"", val.label)
Chris Parsons787fb362021-10-14 18:43:51 -0400508 configString := getConfigString(val)
509 labelsByConfig[configString] = append(labelsByConfig[configString], labelString)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400510 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400511
Jingwen Chen1e347862021-09-02 12:11:49 +0000512 allLabels := []string{}
Chris Parsons787fb362021-10-14 18:43:51 -0400513 for configString, labels := range labelsByConfig {
514 configTokens := strings.Split(configString, "|")
515 if len(configTokens) != 2 {
516 panic(fmt.Errorf("Unexpected config string format: %s", configString))
Jingwen Chen1e347862021-09-02 12:11:49 +0000517 }
Chris Parsons787fb362021-10-14 18:43:51 -0400518 archString := configTokens[0]
519 osString := configTokens[1]
520 targetString := fmt.Sprintf("%s_%s", osString, archString)
521 allLabels = append(allLabels, fmt.Sprintf("\":%s\"", targetString))
522 labelsString := strings.Join(labels, ",\n ")
523 configNodesSection += fmt.Sprintf(configNodeFormatString, targetString, archString, osString, labelsString)
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400524 }
525
Jingwen Chen1e347862021-09-02 12:11:49 +0000526 return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(allLabels, ",\n ")))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400527}
528
Chris Parsons944e7d02021-03-11 11:08:46 -0500529func indent(original string) string {
530 result := ""
531 for _, line := range strings.Split(original, "\n") {
532 result += " " + line + "\n"
533 }
534 return result
535}
536
Chris Parsons808d84c2021-03-09 20:43:32 -0500537// Returns the file contents of the buildroot.cquery file that should be used for the cquery
538// expression in order to obtain information about buildroot and its dependencies.
539// The contents of this file depend on the bazelContext's requests; requests are enumerated
540// and grouped by their request type. The data retrieved for each label depends on its
541// request type.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400542func (context *bazelContext) cqueryStarlarkFileContents() []byte {
Liz Kammerf29df7c2021-04-02 13:37:39 -0400543 requestTypeToCqueryIdEntries := map[cqueryRequest][]string{}
Chris Parsons944e7d02021-03-11 11:08:46 -0500544 for val, _ := range context.requests {
545 cqueryId := getCqueryId(val)
546 mapEntryString := fmt.Sprintf("%q : True", cqueryId)
547 requestTypeToCqueryIdEntries[val.requestType] =
548 append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString)
549 }
550 labelRegistrationMapSection := ""
551 functionDefSection := ""
552 mainSwitchSection := ""
553
554 mapDeclarationFormatString := `
555%s = {
556 %s
557}
558`
559 functionDefFormatString := `
560def %s(target):
561%s
562`
563 mainSwitchSectionFormatString := `
564 if id_string in %s:
565 return id_string + ">>" + %s(target)
566`
567
Liz Kammer66ffdb72021-04-02 13:26:07 -0400568 for requestType, _ := range requestTypeToCqueryIdEntries {
Chris Parsons944e7d02021-03-11 11:08:46 -0500569 labelMapName := requestType.Name() + "_Labels"
570 functionName := requestType.Name() + "_Fn"
571 labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString,
572 labelMapName,
573 strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n "))
574 functionDefSection += fmt.Sprintf(functionDefFormatString,
575 functionName,
576 indent(requestType.StarlarkFunctionBody()))
577 mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString,
578 labelMapName, functionName)
579 }
580
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400581 formatString := `
582# This file is generated by soong_build. Do not edit.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400583
Chris Parsons944e7d02021-03-11 11:08:46 -0500584# Label Map Section
585%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500586
Chris Parsons944e7d02021-03-11 11:08:46 -0500587# Function Def Section
588%s
Chris Parsons8d6e4332021-02-22 16:13:50 -0500589
590def get_arch(target):
Chris Parsons787fb362021-10-14 18:43:51 -0400591 # TODO(b/199363072): filegroups and file targets aren't associated with any
592 # specific platform architecture in mixed builds. This is consistent with how
593 # Soong treats filegroups, but it may not be the case with manually-written
594 # filegroup BUILD targets.
Chris Parsons8d6e4332021-02-22 16:13:50 -0500595 buildoptions = build_options(target)
Jingwen Chen8f222742021-10-07 12:02:23 +0000596 if buildoptions == None:
597 # File targets do not have buildoptions. File targets aren't associated with
Chris Parsons787fb362021-10-14 18:43:51 -0400598 # any specific platform architecture in mixed builds, so use the host.
599 return "x86_64|linux"
Chris Parsons8d6e4332021-02-22 16:13:50 -0500600 platforms = build_options(target)["//command_line_option:platforms"]
601 if len(platforms) != 1:
602 # An individual configured target should have only one platform architecture.
603 # Note that it's fine for there to be multiple architectures for the same label,
604 # but each is its own configured target.
605 fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
606 platform_name = build_options(target)["//command_line_option:platforms"][0].name
607 if platform_name == "host":
608 return "HOST"
Chris Parsons787fb362021-10-14 18:43:51 -0400609 elif platform_name.startswith("linux_glibc_"):
610 return platform_name[len("linux_glibc_"):] + "|" + platform_name[:len("linux_glibc_")-1]
Chris Parsons94a0bba2021-06-04 15:03:47 -0400611 elif platform_name.startswith("android_"):
Chris Parsons787fb362021-10-14 18:43:51 -0400612 return platform_name[len("android_"):] + "|" + platform_name[:len("android_")-1]
Chris Parsons94a0bba2021-06-04 15:03:47 -0400613 elif platform_name.startswith("linux_"):
Chris Parsons787fb362021-10-14 18:43:51 -0400614 return platform_name[len("linux_"):] + "|" + platform_name[:len("linux_")-1]
Chris Parsons94a0bba2021-06-04 15:03:47 -0400615 else:
616 fail("expected platform name of the form 'android_<arch>' or 'linux_<arch>', but was " + str(platforms))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500617 return "UNKNOWN"
Chris Parsons8d6e4332021-02-22 16:13:50 -0500618
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400619def format(target):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500620 id_string = str(target.label) + "|" + get_arch(target)
Chris Parsons944e7d02021-03-11 11:08:46 -0500621
622 # Main switch section
623 %s
624 # This target was not requested via cquery, and thus must be a dependency
625 # of a requested target.
626 return id_string + ">>NONE"
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400627`
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400628
Chris Parsons944e7d02021-03-11 11:08:46 -0500629 return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection,
630 mainSwitchSection))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400631}
632
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200633// Returns a path containing build-related metadata required for interfacing
634// with Bazel. Example: out/soong/bazel.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400635func (p *bazelPaths) intermediatesDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200636 return filepath.Join(p.soongOutDir, "bazel")
Chris Parsons8ccdb632020-11-17 15:41:01 -0500637}
638
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200639// Returns the path where the contents of the @soong_injection repository live.
640// It is used by Soong to tell Bazel things it cannot over the command line.
641func (p *bazelPaths) injectedFilesDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200642 return filepath.Join(p.soongOutDir, bazel.SoongInjectionDirName)
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200643}
644
645// Returns the path of the synthetic Bazel workspace that contains a symlink
646// forest composed the whole source tree and BUILD files generated by bp2build.
647func (p *bazelPaths) syntheticWorkspaceDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200648 return filepath.Join(p.soongOutDir, "workspace")
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200649}
650
Jingwen Chen8c523582021-06-01 11:19:53 +0000651// Returns the path to the top level out dir ($OUT_DIR).
652func (p *bazelPaths) outDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200653 return filepath.Dir(p.soongOutDir)
Jingwen Chen8c523582021-06-01 11:19:53 +0000654}
655
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400656// Issues commands to Bazel to receive results for all cquery requests
657// queued in the BazelContext.
658func (context *bazelContext) InvokeBazel() error {
659 context.results = make(map[cqueryKey]string)
660
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400661 var cqueryOutput string
Chris Parsons808d84c2021-03-09 20:43:32 -0500662 var cqueryErr string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400663 var err error
Chris Parsons8ccdb632020-11-17 15:41:01 -0500664
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200665 soongInjectionPath := absolutePath(context.paths.injectedFilesDir())
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200666 mixedBuildsPath := filepath.Join(soongInjectionPath, "mixed_builds")
667 if _, err := os.Stat(mixedBuildsPath); os.IsNotExist(err) {
668 err = os.MkdirAll(mixedBuildsPath, 0777)
Chris Parsons07c1e4a2021-01-19 17:19:16 -0500669 }
Chris Parsons8ccdb632020-11-17 15:41:01 -0500670 if err != nil {
671 return err
672 }
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200673
674 err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "WORKSPACE.bazel"), []byte{}, 0666)
675 if err != nil {
676 return err
677 }
678
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400679 err = ioutil.WriteFile(
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200680 filepath.Join(mixedBuildsPath, "main.bzl"),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400681 context.mainBzlFileContents(), 0666)
682 if err != nil {
683 return err
684 }
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200685
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400686 err = ioutil.WriteFile(
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200687 filepath.Join(mixedBuildsPath, "BUILD.bazel"),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400688 context.mainBuildFileContents(), 0666)
689 if err != nil {
690 return err
691 }
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200692 cqueryFileRelpath := filepath.Join(context.paths.injectedFilesDir(), "buildroot.cquery")
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400693 err = ioutil.WriteFile(
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800694 absolutePath(cqueryFileRelpath),
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400695 context.cqueryStarlarkFileContents(), 0666)
696 if err != nil {
697 return err
698 }
Jingwen Chen1e347862021-09-02 12:11:49 +0000699
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200700 buildrootLabel := "@soong_injection//mixed_builds:buildroot"
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400701 cqueryOutput, cqueryErr, err = context.issueBazelCommand(
702 context.paths,
703 bazel.CqueryBuildRootRunName,
Liz Kammerc19d5cd2021-10-06 18:16:58 -0400704 bazelCommand{"cquery", fmt.Sprintf("deps(%s, 2)", buildrootLabel)},
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400705 "--output=starlark",
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200706 "--starlark:file="+absolutePath(cqueryFileRelpath))
707 err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "cquery.out"),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500708 []byte(cqueryOutput), 0666)
709 if err != nil {
710 return err
711 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400712
713 if err != nil {
714 return err
715 }
716
717 cqueryResults := map[string]string{}
718 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
719 if strings.Contains(outputLine, ">>") {
720 splitLine := strings.SplitN(outputLine, ">>", 2)
721 cqueryResults[splitLine[0]] = splitLine[1]
722 }
723 }
724
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400725 for val, _ := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500726 if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400727 context.results[val] = string(cqueryResult)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400728 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -0500729 return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]",
730 getCqueryId(val), cqueryOutput, cqueryErr)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400731 }
732 }
733
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500734 // Issue an aquery command to retrieve action information about the bazel build tree.
735 //
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400736 // TODO(cparsons): Use --target_pattern_file to avoid command line limits.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500737 var aqueryOutput string
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400738 aqueryOutput, _, err = context.issueBazelCommand(
739 context.paths,
740 bazel.AqueryBuildRootRunName,
741 bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)},
742 // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
743 // proto sources, which would add a number of unnecessary dependencies.
744 "--output=jsonproto")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400745
746 if err != nil {
747 return err
748 }
749
Chris Parsons4f069892021-01-15 12:22:41 -0500750 context.buildStatements, err = bazel.AqueryBuildStatements([]byte(aqueryOutput))
751 if err != nil {
752 return err
753 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500754
755 // Issue a build command of the phony root to generate symlink forests for dependencies of the
756 // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
757 // but some of symlinks may be required to resolve source dependencies of the build.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400758 _, _, err = context.issueBazelCommand(
759 context.paths,
760 bazel.BazelBuildPhonyRootRunName,
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200761 bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"})
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500762
763 if err != nil {
764 return err
765 }
766
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400767 // Clear requests.
768 context.requests = map[cqueryKey]bool{}
769 return nil
770}
Chris Parsonsa798d962020-10-12 23:44:08 -0400771
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500772func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
773 return context.buildStatements
774}
775
776func (context *bazelContext) OutputBase() string {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400777 return context.paths.outputBase
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500778}
779
Chris Parsonsa798d962020-10-12 23:44:08 -0400780// Singleton used for registering BUILD file ninja dependencies (needed
781// for correctness of builds which use Bazel.
782func BazelSingleton() Singleton {
783 return &bazelSingleton{}
784}
785
786type bazelSingleton struct{}
787
788func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500789 // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
790 if !ctx.Config().BazelContext.BazelEnabled() {
791 return
792 }
Chris Parsonsa798d962020-10-12 23:44:08 -0400793
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500794 // Add ninja file dependencies for files which all bazel invocations require.
795 bazelBuildList := absolutePath(filepath.Join(
Lukacs T. Berkif9008072021-08-16 15:24:48 +0200796 filepath.Dir(ctx.Config().moduleListFile), "bazel.list"))
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500797 ctx.AddNinjaFileDeps(bazelBuildList)
798
799 data, err := ioutil.ReadFile(bazelBuildList)
800 if err != nil {
801 ctx.Errorf(err.Error())
802 }
803 files := strings.Split(strings.TrimSpace(string(data)), "\n")
804 for _, file := range files {
805 ctx.AddNinjaFileDeps(file)
806 }
807
808 // Register bazel-owned build statements (obtained from the aquery invocation).
809 for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500810 if len(buildStatement.Command) < 1 {
Rupert Shuttlewortha29903f2021-04-06 16:17:33 +0000811 panic(fmt.Sprintf("unhandled build statement: %v", buildStatement))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500812 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500813 rule := NewRuleBuilder(pctx, ctx)
814 cmd := rule.Command()
Chris Parsons94a0bba2021-06-04 15:03:47 -0400815
816 // cd into Bazel's execution root, which is the action cwd.
Chris Parsonse37a4de2021-09-23 17:10:50 -0400817 cmd.Text(fmt.Sprintf("cd %s/execroot/__main__ &&", ctx.Config().BazelContext.OutputBase()))
818
819 // Remove old outputs, as some actions might not rerun if the outputs are detected.
820 if len(buildStatement.OutputPaths) > 0 {
821 cmd.Text("rm -f")
822 for _, outputPath := range buildStatement.OutputPaths {
Liz Kammerd7d5b722021-10-01 10:33:12 -0400823 cmd.Text(outputPath)
Chris Parsonse37a4de2021-09-23 17:10:50 -0400824 }
825 cmd.Text("&&")
826 }
Chris Parsons94a0bba2021-06-04 15:03:47 -0400827
828 for _, pair := range buildStatement.Env {
829 // Set per-action env variables, if any.
830 cmd.Flag(pair.Key + "=" + pair.Value)
831 }
832
833 // The actual Bazel action.
834 cmd.Text(" " + buildStatement.Command)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500835
836 for _, outputPath := range buildStatement.OutputPaths {
837 cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400838 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500839 for _, inputPath := range buildStatement.InputPaths {
840 cmd.Implicit(PathForBazelOut(ctx, inputPath))
Chris Parsonsa798d962020-10-12 23:44:08 -0400841 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500842
Liz Kammerde116852021-03-25 16:42:37 -0400843 if depfile := buildStatement.Depfile; depfile != nil {
844 cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile))
845 }
846
Liz Kammerc49e6822021-06-08 15:04:11 -0400847 for _, symlinkPath := range buildStatement.SymlinkPaths {
848 cmd.ImplicitSymlinkOutput(PathForBazelOut(ctx, symlinkPath))
849 }
850
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500851 // This is required to silence warnings pertaining to unexpected timestamps. Particularly,
852 // some Bazel builtins (such as files in the bazel_tools directory) have far-future
853 // timestamps. Without restat, Ninja would emit warnings that the input files of a
854 // build statement have later timestamps than the outputs.
855 rule.Restat()
856
Liz Kammer13548d72020-12-16 11:13:30 -0800857 rule.Build(fmt.Sprintf("bazel %d", index), buildStatement.Mnemonic)
Chris Parsonsa798d962020-10-12 23:44:08 -0400858 }
859}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500860
861func getCqueryId(key cqueryKey) string {
Chris Parsons787fb362021-10-14 18:43:51 -0400862 return key.label + "|" + getConfigString(key)
Chris Parsons8d6e4332021-02-22 16:13:50 -0500863}
864
Chris Parsons787fb362021-10-14 18:43:51 -0400865func getConfigString(key cqueryKey) string {
866 arch := key.configKey.archType.Name
867 if len(arch) == 0 || arch == "common" {
868 // Use host platform, which is currently hardcoded to be x86_64.
869 arch = "x86_64"
Chris Parsons8d6e4332021-02-22 16:13:50 -0500870 }
Chris Parsons787fb362021-10-14 18:43:51 -0400871 os := key.configKey.osType.Name
872 if len(os) == 0 || os == "common_os" {
873 // Use host OS, which is currently hardcoded to be linux.
874 os = "linux"
875 }
876 return arch + "|" + os
877}
878
879func GetConfigKey(ctx ModuleContext) configKey {
880 return configKey{archType: ctx.Arch().ArchType, osType: ctx.Os()}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500881}