blob: 9ff6b52c289354cf0eff997eb62f54ef484377a3 [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"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040019 "fmt"
20 "os"
21 "os/exec"
Usta Shresthaacd5a0c2022-06-22 11:20:50 -040022 "path"
Chris Parsonsa798d962020-10-12 23:44:08 -040023 "path/filepath"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040024 "runtime"
Cole Faust705968d2022-12-14 11:32:05 -080025 "sort"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040026 "strings"
27 "sync"
Chris Parsonsa798d962020-10-12 23:44:08 -040028
Chris Parsonsad876012022-08-20 14:48:32 -040029 "android/soong/android/allowlists"
Chris Parsons944e7d02021-03-11 11:08:46 -050030 "android/soong/bazel/cquery"
Jingwen Chen1e347862021-09-02 12:11:49 +000031 "android/soong/shared"
Sam Delmericocb3c52c2023-02-03 17:40:08 -050032 "android/soong/starlark_fmt"
Liz Kammer337e9032022-08-03 15:49:43 -040033
Chris Parsons1a7aca02022-04-25 22:35:15 -040034 "github.com/google/blueprint"
Liz Kammer8206d4f2021-03-03 16:40:52 -050035
Patrice Arruda05ab2d02020-12-12 06:24:26 +000036 "android/soong/bazel"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040037)
38
Sasha Smundak1da064c2022-06-08 16:36:16 -070039var (
Sasha Smundakc180dbd2022-07-03 14:55:58 -070040 _ = pctx.HostBinToolVariable("bazelBuildRunfilesTool", "build-runfiles")
41 buildRunfilesRule = pctx.AndroidStaticRule("bazelBuildRunfiles", blueprint.RuleParams{
42 Command: "${bazelBuildRunfilesTool} ${in} ${outDir}",
43 Depfile: "",
44 Description: "",
45 CommandDeps: []string{"${bazelBuildRunfilesTool}"},
46 }, "outDir")
Sam Delmericocb3c52c2023-02-03 17:40:08 -050047 allowedBazelEnvironmentVars = []string{
48 "ALLOW_LOCAL_TIDY_TRUE",
49 "DEFAULT_TIDY_HEADER_DIRS",
50 "TIDY_TIMEOUT",
51 "WITH_TIDY",
52 "WITH_TIDY_FLAGS",
53 "SKIP_ABI_CHECKS",
54 "UNSAFE_DISABLE_APEX_ALLOWED_DEPS_CHECK",
55 "AUTO_ZERO_INITIALIZE",
56 "AUTO_PATTERN_INITIALIZE",
57 "AUTO_UNINITIALIZE",
58 "USE_CCACHE",
59 "LLVM_NEXT",
60 "ALLOW_UNKNOWN_WARNING_OPTION",
61
62 // Overrides the version in the apex_manifest.json. The version is unique for
63 // each branch (internal, aosp, mainline releases, dessert releases). This
64 // enables modules built on an older branch to be installed against a newer
65 // device for development purposes.
66 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION",
67 }
Sasha Smundak1da064c2022-06-08 16:36:16 -070068)
69
Chris Parsonsf874e462022-05-10 13:50:12 -040070func init() {
71 RegisterMixedBuildsMutator(InitRegistrationContext)
72}
73
74func RegisterMixedBuildsMutator(ctx RegistrationContext) {
Liz Kammer337e9032022-08-03 15:49:43 -040075 ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) {
Chris Parsonsf874e462022-05-10 13:50:12 -040076 ctx.BottomUp("mixed_builds_prep", mixedBuildsPrepareMutator).Parallel()
77 })
78}
79
80func mixedBuildsPrepareMutator(ctx BottomUpMutatorContext) {
81 if m := ctx.Module(); m.Enabled() {
82 if mixedBuildMod, ok := m.(MixedBuildBuildable); ok {
83 if mixedBuildMod.IsMixedBuildSupported(ctx) && MixedBuildsEnabled(ctx) {
84 mixedBuildMod.QueueBazelCall(ctx)
85 }
86 }
87 }
88}
89
Liz Kammerf29df7c2021-04-02 13:37:39 -040090type cqueryRequest interface {
91 // Name returns a string name for this request type. Such request type names must be unique,
92 // and must only consist of alphanumeric characters.
93 Name() string
94
95 // StarlarkFunctionBody returns a starlark function body to process this request type.
96 // The returned string is the body of a Starlark function which obtains
97 // all request-relevant information about a target and returns a string containing
98 // this information.
99 // The function should have the following properties:
Cole Faust97d15272022-11-22 14:08:59 -0800100 // - The arguments are `target` (a configured target) and `id_string` (the label + configuration).
Liz Kammerf29df7c2021-04-02 13:37:39 -0400101 // - The return value must be a string.
102 // - The function body should not be indented outside of its own scope.
103 StarlarkFunctionBody() string
104}
105
Chris Parsons787fb362021-10-14 18:43:51 -0400106// Portion of cquery map key to describe target configuration.
107type configKey struct {
Liz Kammer0940b892022-03-18 15:55:04 -0400108 arch string
109 osType OsType
Chris Parsons787fb362021-10-14 18:43:51 -0400110}
111
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700112func (c configKey) String() string {
113 return fmt.Sprintf("%s::%s", c.arch, c.osType)
114}
115
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400116// Map key to describe bazel cquery requests.
117type cqueryKey struct {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400118 label string
Liz Kammerf29df7c2021-04-02 13:37:39 -0400119 requestType cqueryRequest
Chris Parsons787fb362021-10-14 18:43:51 -0400120 configKey configKey
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400121}
122
Chris Parsons86dc2c22022-09-28 14:58:41 -0400123func makeCqueryKey(label string, cqueryRequest cqueryRequest, cfgKey configKey) cqueryKey {
124 if strings.HasPrefix(label, "//") {
125 // Normalize Bazel labels to specify main repository explicitly.
126 label = "@" + label
127 }
128 return cqueryKey{label, cqueryRequest, cfgKey}
129}
130
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700131func (c cqueryKey) String() string {
132 return fmt.Sprintf("cquery(%s,%s,%s)", c.label, c.requestType.Name(), c.configKey)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700133}
134
Chris Parsonsf874e462022-05-10 13:50:12 -0400135// BazelContext is a context object useful for interacting with Bazel during
136// the course of a build. Use of Bazel to evaluate part of the build graph
137// is referred to as a "mixed build". (Some modules are managed by Soong,
138// some are managed by Bazel). To facilitate interop between these build
139// subgraphs, Soong may make requests to Bazel and evaluate their responses
140// so that Soong modules may accurately depend on Bazel targets.
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400141type BazelContext interface {
Chris Parsonsf874e462022-05-10 13:50:12 -0400142 // Add a cquery request to the bazel request queue. All queued requests
143 // will be sent to Bazel on a subsequent invocation of InvokeBazel.
144 QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey)
145
146 // ** Cquery Results Retrieval Functions
147 // The below functions pertain to retrieving cquery results from a prior
148 // InvokeBazel function call and parsing the results.
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400149
150 // Returns result files built by building the given bazel target label.
Chris Parsonsf874e462022-05-10 13:50:12 -0400151 GetOutputFiles(label string, cfgKey configKey) ([]string, error)
Chris Parsons8d6e4332021-02-22 16:13:50 -0500152
Chris Parsons944e7d02021-03-11 11:08:46 -0500153 // Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order).
Chris Parsonsf874e462022-05-10 13:50:12 -0400154 GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error)
Liz Kammer3f9e1552021-04-02 18:47:09 -0400155
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000156 // Returns the executable binary resultant from building together the python sources
Chris Parsonsf874e462022-05-10 13:50:12 -0400157 // TODO(b/232976601): Remove.
158 GetPythonBinary(label string, cfgKey configKey) (string, error)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000159
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700160 // Returns the results of the GetApexInfo query (including output files)
Liz Kammerbe6a7122022-11-04 16:05:11 -0400161 GetApexInfo(label string, cfgkey configKey) (cquery.ApexInfo, error)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700162
Sasha Smundakedd16662022-10-07 14:44:50 -0700163 // Returns the results of the GetCcUnstrippedInfo query
164 GetCcUnstrippedInfo(label string, cfgkey configKey) (cquery.CcUnstrippedInfo, error)
165
Chris Parsonsf874e462022-05-10 13:50:12 -0400166 // ** end Cquery Results Retrieval Functions
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400167
168 // Issues commands to Bazel to receive results for all cquery requests
Sasha Smundak4975c822022-11-16 15:28:18 -0800169 // queued in the BazelContext. The ctx argument is optional and is only
170 // used for performance data collection
171 InvokeBazel(config Config, ctx *Context) error
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400172
Chris Parsonsad876012022-08-20 14:48:32 -0400173 // Returns true if Bazel handling is enabled for the module with the given name.
174 // Note that this only implies "bazel mixed build" allowlisting. The caller
175 // should independently verify the module is eligible for Bazel handling
176 // (for example, that it is MixedBuildBuildable).
Sasha Smundak39a301c2022-12-29 17:11:49 -0800177 IsModuleNameAllowed(moduleName string) bool
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500178
179 // Returns the bazel output base (the root directory for all bazel intermediate outputs).
180 OutputBase() string
181
182 // Returns build statements which should get registered to reflect Bazel's outputs.
183 BuildStatementsToRegister() []bazel.BuildStatement
Chris Parsons1a7aca02022-04-25 22:35:15 -0400184
185 // Returns the depsets defined in Bazel's aquery response.
186 AqueryDepsets() []bazel.AqueryDepset
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400187}
188
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400189type bazelRunner interface {
Sam Delmericocb3c52c2023-02-03 17:40:08 -0500190 createBazelCommand(config Config, paths *bazelPaths, runName bazel.RunName, command bazelCommand, extraFlags ...string) *exec.Cmd
Jason Wu52cd1942022-09-08 15:37:57 +0000191 issueBazelCommand(bazelCmd *exec.Cmd) (output string, errorMessage string, error error)
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400192}
193
194type bazelPaths struct {
MarkDacek0d5bca52022-10-10 20:07:48 +0000195 homeDir string
196 bazelPath string
197 outputBase string
198 workspaceDir string
199 soongOutDir string
200 metricsDir string
201 bazelDepsFile string
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400202}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400203
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400204// A context object which tracks queued requests that need to be made to Bazel,
205// and their results after the requests have been made.
Sasha Smundak39a301c2022-12-29 17:11:49 -0800206type mixedBuildBazelContext struct {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400207 bazelRunner
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500208 paths *bazelPaths
209 // cquery requests that have not yet been issued to Bazel. This list is maintained
210 // in a sorted state, and is guaranteed to have no duplicates.
211 requests []cqueryKey
212 requestMutex sync.Mutex // requests can be written in parallel
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400213
214 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500215
216 // Build statements which should get registered to reflect Bazel's outputs.
217 buildStatements []bazel.BuildStatement
Chris Parsons1a7aca02022-04-25 22:35:15 -0400218
219 // Depsets which should be used for Bazel's build statements.
220 depsets []bazel.AqueryDepset
Chris Parsonsad876012022-08-20 14:48:32 -0400221
222 // Per-module allowlist/denylist functionality to control whether analysis of
223 // modules are handled by Bazel. For modules which do not have a Bazel definition
224 // (or do not sufficiently support bazel handling via MixedBuildBuildable),
225 // this allowlist will have no effect, even if the module is explicitly allowlisted here.
226 // Per-module denylist to opt modules out of bazel handling.
227 bazelDisabledModules map[string]bool
228 // Per-module allowlist to opt modules in to bazel handling.
229 bazelEnabledModules map[string]bool
230 // If true, modules are bazel-enabled by default, unless present in bazelDisabledModules.
231 modulesDefaultToBazel bool
Cole Faustb85d1a12022-11-08 18:14:01 -0800232
233 targetProduct string
234 targetBuildVariant string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400235}
236
Sasha Smundak39a301c2022-12-29 17:11:49 -0800237var _ BazelContext = &mixedBuildBazelContext{}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400238
239// A bazel context to use when Bazel is disabled.
240type noopBazelContext struct{}
241
242var _ BazelContext = noopBazelContext{}
243
244// A bazel context to use for tests.
245type MockBazelContext struct {
Liz Kammera92e8442021-04-07 20:25:21 -0400246 OutputBaseDir string
247
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000248 LabelToOutputFiles map[string][]string
249 LabelToCcInfo map[string]cquery.CcInfo
250 LabelToPythonBinary map[string]string
Liz Kammerbe6a7122022-11-04 16:05:11 -0400251 LabelToApexInfo map[string]cquery.ApexInfo
Sasha Smundakedd16662022-10-07 14:44:50 -0700252 LabelToCcBinary map[string]cquery.CcUnstrippedInfo
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400253}
254
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700255func (m MockBazelContext) QueueBazelRequest(_ string, _ cqueryRequest, _ configKey) {
Chris Parsonsf874e462022-05-10 13:50:12 -0400256 panic("unimplemented")
Chris Parsons8d6e4332021-02-22 16:13:50 -0500257}
258
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700259func (m MockBazelContext) GetOutputFiles(label string, _ configKey) ([]string, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500260 result, ok := m.LabelToOutputFiles[label]
261 if !ok {
262 return []string{}, fmt.Errorf("no target with label %q in LabelToOutputFiles", label)
263 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400264 return result, nil
Liz Kammer3f9e1552021-04-02 18:47:09 -0400265}
266
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700267func (m MockBazelContext) GetCcInfo(label string, _ configKey) (cquery.CcInfo, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500268 result, ok := m.LabelToCcInfo[label]
269 if !ok {
270 return cquery.CcInfo{}, fmt.Errorf("no target with label %q in LabelToCcInfo", label)
271 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400272 return result, nil
273}
274
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700275func (m MockBazelContext) GetPythonBinary(label string, _ configKey) (string, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500276 result, ok := m.LabelToPythonBinary[label]
277 if !ok {
278 return "", fmt.Errorf("no target with label %q in LabelToPythonBinary", label)
279 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400280 return result, nil
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000281}
282
Liz Kammerbe6a7122022-11-04 16:05:11 -0400283func (m MockBazelContext) GetApexInfo(label string, _ configKey) (cquery.ApexInfo, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500284 result, ok := m.LabelToApexInfo[label]
285 if !ok {
286 return cquery.ApexInfo{}, fmt.Errorf("no target with label %q in LabelToApexInfo", label)
287 }
Liz Kammer0e255ef2022-11-04 16:07:04 -0400288 return result, nil
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700289}
290
Sasha Smundakedd16662022-10-07 14:44:50 -0700291func (m MockBazelContext) GetCcUnstrippedInfo(label string, _ configKey) (cquery.CcUnstrippedInfo, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500292 result, ok := m.LabelToCcBinary[label]
293 if !ok {
294 return cquery.CcUnstrippedInfo{}, fmt.Errorf("no target with label %q in LabelToCcBinary", label)
295 }
Sasha Smundakedd16662022-10-07 14:44:50 -0700296 return result, nil
297}
298
Sasha Smundak0e87b182022-12-01 11:46:11 -0800299func (m MockBazelContext) InvokeBazel(_ Config, _ *Context) error {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400300 panic("unimplemented")
301}
302
Sasha Smundak39a301c2022-12-29 17:11:49 -0800303func (m MockBazelContext) IsModuleNameAllowed(_ string) bool {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400304 return true
305}
306
Liz Kammera92e8442021-04-07 20:25:21 -0400307func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500308
309func (m MockBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
310 return []bazel.BuildStatement{}
311}
312
Chris Parsons1a7aca02022-04-25 22:35:15 -0400313func (m MockBazelContext) AqueryDepsets() []bazel.AqueryDepset {
314 return []bazel.AqueryDepset{}
315}
316
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400317var _ BazelContext = MockBazelContext{}
318
Sasha Smundak39a301c2022-12-29 17:11:49 -0800319func (bazelCtx *mixedBuildBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400320 key := makeCqueryKey(label, requestType, cfgKey)
Chris Parsonsf874e462022-05-10 13:50:12 -0400321 bazelCtx.requestMutex.Lock()
322 defer bazelCtx.requestMutex.Unlock()
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500323
324 // Insert key into requests, maintaining the sort, and only if it's not duplicate.
325 keyString := key.String()
326 foundEqual := false
327 notLessThanKeyString := func(i int) bool {
328 s := bazelCtx.requests[i].String()
329 v := strings.Compare(s, keyString)
330 if v == 0 {
331 foundEqual = true
332 }
333 return v >= 0
334 }
335 targetIndex := sort.Search(len(bazelCtx.requests), notLessThanKeyString)
336 if foundEqual {
337 return
338 }
339
340 if targetIndex == len(bazelCtx.requests) {
341 bazelCtx.requests = append(bazelCtx.requests, key)
342 } else {
343 bazelCtx.requests = append(bazelCtx.requests[:targetIndex+1], bazelCtx.requests[targetIndex:]...)
344 bazelCtx.requests[targetIndex] = key
345 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400346}
347
Sasha Smundak39a301c2022-12-29 17:11:49 -0800348func (bazelCtx *mixedBuildBazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400349 key := makeCqueryKey(label, cquery.GetOutputFiles, cfgKey)
Chris Parsonsf874e462022-05-10 13:50:12 -0400350 if rawString, ok := bazelCtx.results[key]; ok {
Chris Parsons944e7d02021-03-11 11:08:46 -0500351 bazelOutput := strings.TrimSpace(rawString)
Chris Parsons86dc2c22022-09-28 14:58:41 -0400352
Chris Parsonsf874e462022-05-10 13:50:12 -0400353 return cquery.GetOutputFiles.ParseResult(bazelOutput), nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400354 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400355 return nil, fmt.Errorf("no bazel response found for %v", key)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400356}
357
Sasha Smundak39a301c2022-12-29 17:11:49 -0800358func (bazelCtx *mixedBuildBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400359 key := makeCqueryKey(label, cquery.GetCcInfo, cfgKey)
Chris Parsonsf874e462022-05-10 13:50:12 -0400360 if rawString, ok := bazelCtx.results[key]; ok {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000361 bazelOutput := strings.TrimSpace(rawString)
Chris Parsonsf874e462022-05-10 13:50:12 -0400362 return cquery.GetCcInfo.ParseResult(bazelOutput)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000363 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400364 return cquery.CcInfo{}, fmt.Errorf("no bazel response found for %v", key)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000365}
366
Sasha Smundak39a301c2022-12-29 17:11:49 -0800367func (bazelCtx *mixedBuildBazelContext) GetPythonBinary(label string, cfgKey configKey) (string, error) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400368 key := makeCqueryKey(label, cquery.GetPythonBinary, cfgKey)
Chris Parsonsf874e462022-05-10 13:50:12 -0400369 if rawString, ok := bazelCtx.results[key]; ok {
370 bazelOutput := strings.TrimSpace(rawString)
371 return cquery.GetPythonBinary.ParseResult(bazelOutput), nil
372 }
373 return "", fmt.Errorf("no bazel response found for %v", key)
374}
375
Sasha Smundak39a301c2022-12-29 17:11:49 -0800376func (bazelCtx *mixedBuildBazelContext) GetApexInfo(label string, cfgKey configKey) (cquery.ApexInfo, error) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400377 key := makeCqueryKey(label, cquery.GetApexInfo, cfgKey)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700378 if rawString, ok := bazelCtx.results[key]; ok {
Liz Kammer1b7ed9b2022-11-09 10:05:05 -0500379 return cquery.GetApexInfo.ParseResult(strings.TrimSpace(rawString))
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700380 }
Liz Kammerbe6a7122022-11-04 16:05:11 -0400381 return cquery.ApexInfo{}, fmt.Errorf("no bazel response found for %v", key)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700382}
383
Sasha Smundak39a301c2022-12-29 17:11:49 -0800384func (bazelCtx *mixedBuildBazelContext) GetCcUnstrippedInfo(label string, cfgKey configKey) (cquery.CcUnstrippedInfo, error) {
Sasha Smundakedd16662022-10-07 14:44:50 -0700385 key := makeCqueryKey(label, cquery.GetCcUnstrippedInfo, cfgKey)
386 if rawString, ok := bazelCtx.results[key]; ok {
Liz Kammer1b7ed9b2022-11-09 10:05:05 -0500387 return cquery.GetCcUnstrippedInfo.ParseResult(strings.TrimSpace(rawString))
Sasha Smundakedd16662022-10-07 14:44:50 -0700388 }
389 return cquery.CcUnstrippedInfo{}, fmt.Errorf("no bazel response for %s", key)
390}
391
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700392func (n noopBazelContext) QueueBazelRequest(_ string, _ cqueryRequest, _ configKey) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500393 panic("unimplemented")
394}
395
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700396func (n noopBazelContext) GetOutputFiles(_ string, _ configKey) ([]string, error) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500397 panic("unimplemented")
398}
399
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700400func (n noopBazelContext) GetCcInfo(_ string, _ configKey) (cquery.CcInfo, error) {
Chris Parsonsf874e462022-05-10 13:50:12 -0400401 panic("unimplemented")
402}
403
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700404func (n noopBazelContext) GetPythonBinary(_ string, _ configKey) (string, error) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000405 panic("unimplemented")
406}
407
Liz Kammerbe6a7122022-11-04 16:05:11 -0400408func (n noopBazelContext) GetApexInfo(_ string, _ configKey) (cquery.ApexInfo, error) {
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700409 panic("unimplemented")
410}
411
Sasha Smundakedd16662022-10-07 14:44:50 -0700412func (n noopBazelContext) GetCcUnstrippedInfo(_ string, _ configKey) (cquery.CcUnstrippedInfo, error) {
413 //TODO implement me
414 panic("implement me")
415}
416
Sasha Smundak0e87b182022-12-01 11:46:11 -0800417func (n noopBazelContext) InvokeBazel(_ Config, _ *Context) error {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400418 panic("unimplemented")
419}
420
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500421func (m noopBazelContext) OutputBase() string {
422 return ""
423}
424
Sasha Smundak39a301c2022-12-29 17:11:49 -0800425func (n noopBazelContext) IsModuleNameAllowed(_ string) bool {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400426 return false
427}
428
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500429func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
430 return []bazel.BuildStatement{}
431}
432
Chris Parsons1a7aca02022-04-25 22:35:15 -0400433func (m noopBazelContext) AqueryDepsets() []bazel.AqueryDepset {
434 return []bazel.AqueryDepset{}
435}
436
Cole Faust705968d2022-12-14 11:32:05 -0800437func GetBazelEnabledAndDisabledModules(buildMode SoongBuildMode, forceEnabled map[string]struct{}) (map[string]bool, map[string]bool) {
Chris Parsonsef615e52022-08-18 22:04:11 -0400438 disabledModules := map[string]bool{}
439 enabledModules := map[string]bool{}
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800440 addToStringSet := func(set map[string]bool, items []string) {
441 for _, item := range items {
442 set[item] = true
443 }
444 }
Chris Parsonsef615e52022-08-18 22:04:11 -0400445
Cole Faust705968d2022-12-14 11:32:05 -0800446 switch buildMode {
Chris Parsonsef615e52022-08-18 22:04:11 -0400447 case BazelProdMode:
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800448 addToStringSet(enabledModules, allowlists.ProdMixedBuildsEnabledList)
Cole Faust705968d2022-12-14 11:32:05 -0800449 for enabledAdHocModule := range forceEnabled {
MarkDacekd06db5d2022-11-29 00:47:59 +0000450 enabledModules[enabledAdHocModule] = true
451 }
MarkDacekb78465d2022-10-18 20:10:16 +0000452 case BazelStagingMode:
Chris Parsons66fc7452022-11-04 13:26:17 -0400453 // Staging mode includes all prod modules plus all staging modules.
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800454 addToStringSet(enabledModules, allowlists.ProdMixedBuildsEnabledList)
455 addToStringSet(enabledModules, allowlists.StagingMixedBuildsEnabledList)
Cole Faust705968d2022-12-14 11:32:05 -0800456 for enabledAdHocModule := range forceEnabled {
MarkDacekd06db5d2022-11-29 00:47:59 +0000457 enabledModules[enabledAdHocModule] = true
458 }
Chris Parsonsef615e52022-08-18 22:04:11 -0400459 case BazelDevMode:
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800460 addToStringSet(disabledModules, allowlists.MixedBuildsDisabledList)
Chris Parsonsef615e52022-08-18 22:04:11 -0400461 default:
Cole Faust705968d2022-12-14 11:32:05 -0800462 panic("Expected BazelProdMode, BazelStagingMode, or BazelDevMode")
463 }
464 return enabledModules, disabledModules
465}
466
467func GetBazelEnabledModules(buildMode SoongBuildMode) []string {
468 enabledModules, disabledModules := GetBazelEnabledAndDisabledModules(buildMode, nil)
469 enabledList := make([]string, 0, len(enabledModules))
470 for module := range enabledModules {
471 if !disabledModules[module] {
472 enabledList = append(enabledList, module)
473 }
474 }
475 sort.Strings(enabledList)
476 return enabledList
477}
478
479func NewBazelContext(c *config) (BazelContext, error) {
480 if c.BuildMode != BazelProdMode && c.BuildMode != BazelStagingMode && c.BuildMode != BazelDevMode {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400481 return noopBazelContext{}, nil
482 }
483
Cole Faust705968d2022-12-14 11:32:05 -0800484 enabledModules, disabledModules := GetBazelEnabledAndDisabledModules(c.BuildMode, c.BazelModulesForceEnabledByFlag())
485
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800486 paths := bazelPaths{
487 soongOutDir: c.soongOutDir,
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400488 }
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800489 var missing []string
490 vars := []struct {
491 name string
492 ptr *string
Paul Duffin184366a2022-12-21 15:55:33 +0000493
494 // True if the environment variable needs to be tracked so that changes to the variable
495 // cause the ninja file to be regenerated, false otherwise. False should only be set for
496 // environment variables that have no effect on the generated ninja file.
497 track bool
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800498 }{
Paul Duffin184366a2022-12-21 15:55:33 +0000499 {"BAZEL_HOME", &paths.homeDir, true},
500 {"BAZEL_PATH", &paths.bazelPath, true},
501 {"BAZEL_OUTPUT_BASE", &paths.outputBase, true},
502 {"BAZEL_WORKSPACE", &paths.workspaceDir, true},
503 {"BAZEL_METRICS_DIR", &paths.metricsDir, false},
504 {"BAZEL_DEPS_FILE", &paths.bazelDepsFile, true},
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800505 }
506 for _, v := range vars {
Paul Duffin184366a2022-12-21 15:55:33 +0000507 if v.track {
508 if s := c.Getenv(v.name); len(s) > 1 {
509 *v.ptr = s
510 continue
511 }
512 } else if s, ok := c.env[v.name]; ok {
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800513 *v.ptr = s
514 } else {
515 missing = append(missing, v.name)
516 }
517 }
518 if len(missing) > 0 {
519 return nil, fmt.Errorf("missing required env vars to use bazel: %s", missing)
520 }
Cole Faustb85d1a12022-11-08 18:14:01 -0800521
522 targetBuildVariant := "user"
523 if c.Eng() {
524 targetBuildVariant = "eng"
525 } else if c.Debuggable() {
526 targetBuildVariant = "userdebug"
527 }
528 targetProduct := "unknown"
529 if c.HasDeviceProduct() {
530 targetProduct = c.DeviceProduct()
531 }
532
Sasha Smundak39a301c2022-12-29 17:11:49 -0800533 return &mixedBuildBazelContext{
Chris Parsonsad876012022-08-20 14:48:32 -0400534 bazelRunner: &builtinBazelRunner{},
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800535 paths: &paths,
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800536 modulesDefaultToBazel: c.BuildMode == BazelDevMode,
Chris Parsonsef615e52022-08-18 22:04:11 -0400537 bazelEnabledModules: enabledModules,
Chris Parsonsad876012022-08-20 14:48:32 -0400538 bazelDisabledModules: disabledModules,
Cole Faustb85d1a12022-11-08 18:14:01 -0800539 targetProduct: targetProduct,
540 targetBuildVariant: targetBuildVariant,
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400541 }, nil
542}
543
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400544func (p *bazelPaths) BazelMetricsDir() string {
545 return p.metricsDir
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000546}
547
Sasha Smundak39a301c2022-12-29 17:11:49 -0800548func (context *mixedBuildBazelContext) IsModuleNameAllowed(moduleName string) bool {
Chris Parsonsad876012022-08-20 14:48:32 -0400549 if context.bazelDisabledModules[moduleName] {
550 return false
551 }
552 if context.bazelEnabledModules[moduleName] {
553 return true
554 }
555 return context.modulesDefaultToBazel
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400556}
557
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400558func pwdPrefix() string {
559 // Darwin doesn't have /proc
560 if runtime.GOOS != "darwin" {
561 return "PWD=/proc/self/cwd"
562 }
563 return ""
564}
565
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400566type bazelCommand struct {
567 command string
568 // query or label
569 expression string
570}
571
572type mockBazelRunner struct {
573 bazelCommandResults map[bazelCommand]string
Jason Wu52cd1942022-09-08 15:37:57 +0000574 // use *exec.Cmd as a key to get the bazelCommand, the map will be used in issueBazelCommand()
575 // Register createBazelCommand() invocations. Later, an
576 // issueBazelCommand() invocation can be mapped to the *exec.Cmd instance
577 // and then to the expected result via bazelCommandResults
578 tokens map[*exec.Cmd]bazelCommand
579 commands []bazelCommand
580 extraFlags []string
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400581}
582
Sam Delmericocb3c52c2023-02-03 17:40:08 -0500583func (r *mockBazelRunner) createBazelCommand(_ Config, _ *bazelPaths, _ bazel.RunName,
Jason Wu52cd1942022-09-08 15:37:57 +0000584 command bazelCommand, extraFlags ...string) *exec.Cmd {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400585 r.commands = append(r.commands, command)
Yu Liu8d82ac52022-05-17 15:13:28 -0700586 r.extraFlags = append(r.extraFlags, strings.Join(extraFlags, " "))
Jason Wu52cd1942022-09-08 15:37:57 +0000587 cmd := &exec.Cmd{}
588 if r.tokens == nil {
589 r.tokens = make(map[*exec.Cmd]bazelCommand)
590 }
591 r.tokens[cmd] = command
592 return cmd
593}
594
595func (r *mockBazelRunner) issueBazelCommand(bazelCmd *exec.Cmd) (string, string, error) {
596 if command, ok := r.tokens[bazelCmd]; ok {
597 return r.bazelCommandResults[command], "", nil
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400598 }
599 return "", "", nil
600}
601
602type builtinBazelRunner struct{}
603
Chris Parsons808d84c2021-03-09 20:43:32 -0500604// Issues the given bazel command with given build label and additional flags.
605// Returns (stdout, stderr, error). The first and second return values are strings
606// containing the stdout and stderr of the run command, and an error is returned if
607// the invocation returned an error code.
Jason Wu52cd1942022-09-08 15:37:57 +0000608func (r *builtinBazelRunner) issueBazelCommand(bazelCmd *exec.Cmd) (string, string, error) {
609 stderr := &bytes.Buffer{}
610 bazelCmd.Stderr = stderr
611 if output, err := bazelCmd.Output(); err != nil {
612 return "", string(stderr.Bytes()),
Sasha Smundak0e87b182022-12-01 11:46:11 -0800613 fmt.Errorf("bazel command failed: %s\n---command---\n%s\n---env---\n%s\n---stderr---\n%s---",
614 err, bazelCmd, strings.Join(bazelCmd.Env, "\n"), stderr)
Jason Wu52cd1942022-09-08 15:37:57 +0000615 } else {
616 return string(output), string(stderr.Bytes()), nil
617 }
618}
619
Sam Delmericocb3c52c2023-02-03 17:40:08 -0500620func (r *builtinBazelRunner) createBazelCommand(config Config, paths *bazelPaths, runName bazel.RunName, command bazelCommand,
Jason Wu52cd1942022-09-08 15:37:57 +0000621 extraFlags ...string) *exec.Cmd {
Romain Jobredeaux41fd5e42021-08-27 15:59:39 +0000622 cmdFlags := []string{
Romain Jobredeaux41fd5e42021-08-27 15:59:39 +0000623 "--output_base=" + absolutePath(paths.outputBase),
624 command.command,
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700625 command.expression,
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700626 // TODO(asmundak): is it needed in every build?
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700627 "--profile=" + shared.BazelMetricsFilename(paths, runName),
Jingwen Chen91220d72021-03-24 02:18:33 -0400628
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700629 // Set default platforms to canonicalized values for mixed builds requests.
630 // If these are set in the bazelrc, they will have values that are
631 // non-canonicalized to @sourceroot labels, and thus be invalid when
632 // referenced from the buildroot.
633 //
634 // The actual platform values here may be overridden by configuration
635 // transitions from the buildroot.
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700636 fmt.Sprintf("--extra_toolchains=%s", "//prebuilts/clang/host/linux-x86:all"),
Cole Faustb85d1a12022-11-08 18:14:01 -0800637
638 // We don't need to set --host_platforms because it's set in bazelrc files
639 // that the bazel shell script wrapper passes
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700640
641 // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network.
642 "--experimental_repository_disable_download",
643
644 // Suppress noise
645 "--ui_event_filters=-INFO",
Sam Delmerico658a4da2022-11-07 15:53:38 -0500646 "--noshow_progress",
647 "--norun_validations",
648 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400649 cmdFlags = append(cmdFlags, extraFlags...)
650
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400651 bazelCmd := exec.Command(paths.bazelPath, cmdFlags...)
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200652 bazelCmd.Dir = absolutePath(paths.syntheticWorkspaceDir())
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700653 extraEnv := []string{
654 "HOME=" + paths.homeDir,
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200655 pwdPrefix(),
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700656 "BUILD_DIR=" + absolutePath(paths.soongOutDir),
Joe Onoratoba29f382022-10-24 06:38:11 -0700657 // Make OUT_DIR absolute here so build/bazel/bin/bazel uses the correct
Jingwen Chen8c523582021-06-01 11:19:53 +0000658 // OUT_DIR at <root>/out, instead of <root>/out/soong/workspace/out.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700659 "OUT_DIR=" + absolutePath(paths.outDir()),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500660 // Disables local host detection of gcc; toolchain information is defined
661 // explicitly in BUILD files.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700662 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1",
663 }
Sam Delmericocb3c52c2023-02-03 17:40:08 -0500664 for _, envvar := range allowedBazelEnvironmentVars {
665 val := config.Getenv(envvar)
666 if val == "" {
667 continue
668 }
669 extraEnv = append(extraEnv, fmt.Sprintf("%s=%s", envvar, val))
670 }
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700671 bazelCmd.Env = append(os.Environ(), extraEnv...)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400672
Jason Wu52cd1942022-09-08 15:37:57 +0000673 return bazelCmd
674}
675
676func printableCqueryCommand(bazelCmd *exec.Cmd) string {
677 outputString := strings.Join(bazelCmd.Env, " ") + " \"" + strings.Join(bazelCmd.Args, "\" \"") + "\""
678 return outputString
679
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400680}
681
Sasha Smundak39a301c2022-12-29 17:11:49 -0800682func (context *mixedBuildBazelContext) mainBzlFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500683 // TODO(cparsons): Define configuration transitions programmatically based
684 // on available archs.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400685 contents := `
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500686#####################################################
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400687# This file is generated by soong_build. Do not edit.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500688#####################################################
689
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400690def _config_node_transition_impl(settings, attr):
Cole Faustb85d1a12022-11-08 18:14:01 -0800691 if attr.os == "android" and attr.arch == "target":
692 target = "{PRODUCT}-{VARIANT}"
693 else:
694 target = "{PRODUCT}-{VARIANT}_%s_%s" % (attr.os, attr.arch)
Chris Parsons8d6e4332021-02-22 16:13:50 -0500695 return {
Cole Faustb85d1a12022-11-08 18:14:01 -0800696 "//command_line_option:platforms": "@soong_injection//product_config_platforms/products/{PRODUCT}-{VARIANT}:%s" % target,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500697 }
698
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400699_config_node_transition = transition(
700 implementation = _config_node_transition_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500701 inputs = [],
702 outputs = [
703 "//command_line_option:platforms",
704 ],
705)
706
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400707def _passthrough_rule_impl(ctx):
708 return [DefaultInfo(files = depset(ctx.files.deps))]
709
710config_node = rule(
711 implementation = _passthrough_rule_impl,
712 attrs = {
713 "arch" : attr.string(mandatory = True),
Chris Parsons787fb362021-10-14 18:43:51 -0400714 "os" : attr.string(mandatory = True),
715 "deps" : attr.label_list(cfg = _config_node_transition, allow_files = True),
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400716 "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
717 },
Chris Parsons8d6e4332021-02-22 16:13:50 -0500718)
719
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400720
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500721# Rule representing the root of the build, to depend on all Bazel targets that
722# are required for the build. Building this target will build the entire Bazel
723# build tree.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400724mixed_build_root = rule(
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400725 implementation = _passthrough_rule_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500726 attrs = {
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400727 "deps" : attr.label_list(),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500728 },
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400729)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500730
731def _phony_root_impl(ctx):
732 return []
733
734# Rule to depend on other targets but build nothing.
735# This is useful as follows: building a target of this rule will generate
736# symlink forests for all dependencies of the target, without executing any
737# actions of the build.
738phony_root = rule(
739 implementation = _phony_root_impl,
740 attrs = {"deps" : attr.label_list()},
741)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400742`
Cole Faustb85d1a12022-11-08 18:14:01 -0800743
744 productReplacer := strings.NewReplacer(
745 "{PRODUCT}", context.targetProduct,
746 "{VARIANT}", context.targetBuildVariant)
747
748 return []byte(productReplacer.Replace(contents))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400749}
750
Sasha Smundak39a301c2022-12-29 17:11:49 -0800751func (context *mixedBuildBazelContext) mainBuildFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500752 // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
753 // architecture mapping.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400754 formatString := `
755# This file is generated by soong_build. Do not edit.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400756load(":main.bzl", "config_node", "mixed_build_root", "phony_root")
757
758%s
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400759
760mixed_build_root(name = "buildroot",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400761 deps = [%s],
Jingwen Chen3952a902022-12-12 12:20:58 +0000762 testonly = True, # Unblocks testonly deps.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400763)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500764
765phony_root(name = "phonyroot",
766 deps = [":buildroot"],
Jingwen Chen3952a902022-12-12 12:20:58 +0000767 testonly = True, # Unblocks testonly deps.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500768)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400769`
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400770 configNodeFormatString := `
771config_node(name = "%s",
772 arch = "%s",
Chris Parsons787fb362021-10-14 18:43:51 -0400773 os = "%s",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400774 deps = [%s],
Jingwen Chen3952a902022-12-12 12:20:58 +0000775 testonly = True, # Unblocks testonly deps.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400776)
777`
778
779 configNodesSection := ""
780
Chris Parsons787fb362021-10-14 18:43:51 -0400781 labelsByConfig := map[string][]string{}
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500782
783 for _, val := range context.requests {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200784 labelString := fmt.Sprintf("\"@%s\"", val.label)
Chris Parsons787fb362021-10-14 18:43:51 -0400785 configString := getConfigString(val)
786 labelsByConfig[configString] = append(labelsByConfig[configString], labelString)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400787 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400788
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500789 // Configs need to be sorted to maintain determinism of the BUILD file.
790 sortedConfigs := make([]string, 0, len(labelsByConfig))
791 for val := range labelsByConfig {
792 sortedConfigs = append(sortedConfigs, val)
793 }
794 sort.Slice(sortedConfigs, func(i, j int) bool { return sortedConfigs[i] < sortedConfigs[j] })
795
Jingwen Chen1e347862021-09-02 12:11:49 +0000796 allLabels := []string{}
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500797 for _, configString := range sortedConfigs {
798 labels := labelsByConfig[configString]
Chris Parsons787fb362021-10-14 18:43:51 -0400799 configTokens := strings.Split(configString, "|")
800 if len(configTokens) != 2 {
801 panic(fmt.Errorf("Unexpected config string format: %s", configString))
Jingwen Chen1e347862021-09-02 12:11:49 +0000802 }
Chris Parsons787fb362021-10-14 18:43:51 -0400803 archString := configTokens[0]
804 osString := configTokens[1]
805 targetString := fmt.Sprintf("%s_%s", osString, archString)
806 allLabels = append(allLabels, fmt.Sprintf("\":%s\"", targetString))
807 labelsString := strings.Join(labels, ",\n ")
808 configNodesSection += fmt.Sprintf(configNodeFormatString, targetString, archString, osString, labelsString)
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400809 }
810
Jingwen Chen1e347862021-09-02 12:11:49 +0000811 return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(allLabels, ",\n ")))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400812}
813
Chris Parsons944e7d02021-03-11 11:08:46 -0500814func indent(original string) string {
815 result := ""
816 for _, line := range strings.Split(original, "\n") {
817 result += " " + line + "\n"
818 }
819 return result
820}
821
Chris Parsons808d84c2021-03-09 20:43:32 -0500822// Returns the file contents of the buildroot.cquery file that should be used for the cquery
823// expression in order to obtain information about buildroot and its dependencies.
Sasha Smundak39a301c2022-12-29 17:11:49 -0800824// The contents of this file depend on the mixedBuildBazelContext's requests; requests are enumerated
Chris Parsons808d84c2021-03-09 20:43:32 -0500825// and grouped by their request type. The data retrieved for each label depends on its
826// request type.
Sasha Smundak39a301c2022-12-29 17:11:49 -0800827func (context *mixedBuildBazelContext) cqueryStarlarkFileContents() []byte {
Liz Kammerf29df7c2021-04-02 13:37:39 -0400828 requestTypeToCqueryIdEntries := map[cqueryRequest][]string{}
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500829 for _, val := range context.requests {
Chris Parsons944e7d02021-03-11 11:08:46 -0500830 cqueryId := getCqueryId(val)
831 mapEntryString := fmt.Sprintf("%q : True", cqueryId)
832 requestTypeToCqueryIdEntries[val.requestType] =
833 append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString)
834 }
835 labelRegistrationMapSection := ""
836 functionDefSection := ""
837 mainSwitchSection := ""
838
839 mapDeclarationFormatString := `
840%s = {
841 %s
842}
843`
844 functionDefFormatString := `
Cole Faust97d15272022-11-22 14:08:59 -0800845def %s(target, id_string):
Chris Parsons944e7d02021-03-11 11:08:46 -0500846%s
847`
848 mainSwitchSectionFormatString := `
849 if id_string in %s:
Cole Faust97d15272022-11-22 14:08:59 -0800850 return id_string + ">>" + %s(target, id_string)
Chris Parsons944e7d02021-03-11 11:08:46 -0500851`
852
Usta Shrestha0b52d832022-02-04 21:37:39 -0500853 for requestType := range requestTypeToCqueryIdEntries {
Chris Parsons944e7d02021-03-11 11:08:46 -0500854 labelMapName := requestType.Name() + "_Labels"
855 functionName := requestType.Name() + "_Fn"
856 labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString,
857 labelMapName,
858 strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n "))
859 functionDefSection += fmt.Sprintf(functionDefFormatString,
860 functionName,
861 indent(requestType.StarlarkFunctionBody()))
862 mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString,
863 labelMapName, functionName)
864 }
865
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400866 formatString := `
867# This file is generated by soong_build. Do not edit.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400868
Usta Shrestha79fccef2022-09-02 18:37:40 -0400869# a drop-in replacement for json.encode(), not available in cquery environment
870# TODO(cparsons): bring json module in and remove this function
871def json_encode(input):
872 # Avoiding recursion by limiting
873 # - a dict to contain anything except a dict
874 # - a list to contain only primitives
875 def encode_primitive(p):
876 t = type(p)
877 if t == "string" or t == "int":
878 return repr(p)
Cole Faustb85d1a12022-11-08 18:14:01 -0800879 fail("unsupported value '%s' of type '%s'" % (p, type(p)))
Usta Shrestha79fccef2022-09-02 18:37:40 -0400880
881 def encode_list(list):
Cole Faustb85d1a12022-11-08 18:14:01 -0800882 return "[%s]" % ", ".join([encode_primitive(item) for item in list])
Usta Shrestha79fccef2022-09-02 18:37:40 -0400883
884 def encode_list_or_primitive(v):
885 return encode_list(v) if type(v) == "list" else encode_primitive(v)
886
887 if type(input) == "dict":
888 # TODO(juu): the result is read line by line so can't use '\n' yet
Cole Faustb85d1a12022-11-08 18:14:01 -0800889 kv_pairs = [("%s: %s" % (encode_primitive(k), encode_list_or_primitive(v))) for (k, v) in input.items()]
890 return "{ %s }" % ", ".join(kv_pairs)
Usta Shrestha79fccef2022-09-02 18:37:40 -0400891 else:
892 return encode_list_or_primitive(input)
893
Cole Faustb85d1a12022-11-08 18:14:01 -0800894{LABEL_REGISTRATION_MAP_SECTION}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500895
Cole Faustb85d1a12022-11-08 18:14:01 -0800896{FUNCTION_DEF_SECTION}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500897
898def get_arch(target):
Chris Parsons787fb362021-10-14 18:43:51 -0400899 # TODO(b/199363072): filegroups and file targets aren't associated with any
900 # specific platform architecture in mixed builds. This is consistent with how
901 # Soong treats filegroups, but it may not be the case with manually-written
902 # filegroup BUILD targets.
Chris Parsons8d6e4332021-02-22 16:13:50 -0500903 buildoptions = build_options(target)
Jingwen Chen8f222742021-10-07 12:02:23 +0000904 if buildoptions == None:
905 # File targets do not have buildoptions. File targets aren't associated with
Chris Parsons787fb362021-10-14 18:43:51 -0400906 # any specific platform architecture in mixed builds, so use the host.
907 return "x86_64|linux"
Cole Faustb85d1a12022-11-08 18:14:01 -0800908 platforms = buildoptions["//command_line_option:platforms"]
Chris Parsons8d6e4332021-02-22 16:13:50 -0500909 if len(platforms) != 1:
910 # An individual configured target should have only one platform architecture.
911 # Note that it's fine for there to be multiple architectures for the same label,
912 # but each is its own configured target.
913 fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
Cole Faustb85d1a12022-11-08 18:14:01 -0800914 platform_name = platforms[0].name
Chris Parsons8d6e4332021-02-22 16:13:50 -0500915 if platform_name == "host":
916 return "HOST"
Cole Faustb85d1a12022-11-08 18:14:01 -0800917 if not platform_name.startswith("{TARGET_PRODUCT}-{TARGET_BUILD_VARIANT}"):
918 fail("expected platform name of the form '{TARGET_PRODUCT}-{TARGET_BUILD_VARIANT}_android_<arch>' or '{TARGET_PRODUCT}-{TARGET_BUILD_VARIANT}_linux_<arch>', but was " + str(platforms))
919 platform_name = platform_name.removeprefix("{TARGET_PRODUCT}-{TARGET_BUILD_VARIANT}").removeprefix("_")
920 if not platform_name:
921 return "target|android"
Chris Parsons94a0bba2021-06-04 15:03:47 -0400922 elif platform_name.startswith("android_"):
Cole Faustb85d1a12022-11-08 18:14:01 -0800923 return platform_name.removeprefix("android_") + "|android"
Chris Parsons94a0bba2021-06-04 15:03:47 -0400924 elif platform_name.startswith("linux_"):
Cole Faustb85d1a12022-11-08 18:14:01 -0800925 return platform_name.removeprefix("linux_") + "|linux"
Chris Parsons94a0bba2021-06-04 15:03:47 -0400926 else:
Cole Faustb85d1a12022-11-08 18:14:01 -0800927 fail("expected platform name of the form '{TARGET_PRODUCT}-{TARGET_BUILD_VARIANT}_android_<arch>' or '{TARGET_PRODUCT}-{TARGET_BUILD_VARIANT}_linux_<arch>', but was " + str(platforms))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500928
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400929def format(target):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500930 id_string = str(target.label) + "|" + get_arch(target)
Chris Parsons944e7d02021-03-11 11:08:46 -0500931
Chris Parsons86dc2c22022-09-28 14:58:41 -0400932 # TODO(b/248106697): Remove once Bazel is updated to always normalize labels.
933 if id_string.startswith("//"):
934 id_string = "@" + id_string
935
Cole Faustb85d1a12022-11-08 18:14:01 -0800936 {MAIN_SWITCH_SECTION}
937
Chris Parsons944e7d02021-03-11 11:08:46 -0500938 # This target was not requested via cquery, and thus must be a dependency
939 # of a requested target.
940 return id_string + ">>NONE"
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400941`
Cole Faustb85d1a12022-11-08 18:14:01 -0800942 replacer := strings.NewReplacer(
943 "{TARGET_PRODUCT}", context.targetProduct,
944 "{TARGET_BUILD_VARIANT}", context.targetBuildVariant,
945 "{LABEL_REGISTRATION_MAP_SECTION}", labelRegistrationMapSection,
946 "{FUNCTION_DEF_SECTION}", functionDefSection,
947 "{MAIN_SWITCH_SECTION}", mainSwitchSection)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400948
Cole Faustb85d1a12022-11-08 18:14:01 -0800949 return []byte(replacer.Replace(formatString))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400950}
951
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200952// Returns a path containing build-related metadata required for interfacing
953// with Bazel. Example: out/soong/bazel.
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400954func (p *bazelPaths) intermediatesDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200955 return filepath.Join(p.soongOutDir, "bazel")
Chris Parsons8ccdb632020-11-17 15:41:01 -0500956}
957
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200958// Returns the path where the contents of the @soong_injection repository live.
959// It is used by Soong to tell Bazel things it cannot over the command line.
960func (p *bazelPaths) injectedFilesDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200961 return filepath.Join(p.soongOutDir, bazel.SoongInjectionDirName)
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200962}
963
964// Returns the path of the synthetic Bazel workspace that contains a symlink
965// forest composed the whole source tree and BUILD files generated by bp2build.
966func (p *bazelPaths) syntheticWorkspaceDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200967 return filepath.Join(p.soongOutDir, "workspace")
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200968}
969
Jingwen Chen8c523582021-06-01 11:19:53 +0000970// Returns the path to the top level out dir ($OUT_DIR).
971func (p *bazelPaths) outDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200972 return filepath.Dir(p.soongOutDir)
Jingwen Chen8c523582021-06-01 11:19:53 +0000973}
974
Sasha Smundak4975c822022-11-16 15:28:18 -0800975const buildrootLabel = "@soong_injection//mixed_builds:buildroot"
976
977var (
978 cqueryCmd = bazelCommand{"cquery", fmt.Sprintf("deps(%s, 2)", buildrootLabel)}
979 aqueryCmd = bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)}
980 buildCmd = bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"}
981)
982
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400983// Issues commands to Bazel to receive results for all cquery requests
984// queued in the BazelContext.
Sasha Smundak39a301c2022-12-29 17:11:49 -0800985func (context *mixedBuildBazelContext) InvokeBazel(config Config, ctx *Context) error {
Sasha Smundak4975c822022-11-16 15:28:18 -0800986 if ctx != nil {
987 ctx.EventHandler.Begin("bazel")
988 defer ctx.EventHandler.End("bazel")
989 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400990
Sasha Smundak4975c822022-11-16 15:28:18 -0800991 if metricsDir := context.paths.BazelMetricsDir(); metricsDir != "" {
992 if err := os.MkdirAll(metricsDir, 0777); err != nil {
993 return err
994 }
995 }
996 context.results = make(map[cqueryKey]string)
Sam Delmericocb3c52c2023-02-03 17:40:08 -0500997 if err := context.runCquery(config, ctx); err != nil {
Sasha Smundak4975c822022-11-16 15:28:18 -0800998 return err
999 }
1000 if err := context.runAquery(config, ctx); err != nil {
1001 return err
1002 }
Sam Delmericocb3c52c2023-02-03 17:40:08 -05001003 if err := context.generateBazelSymlinks(config, ctx); err != nil {
Sasha Smundak4975c822022-11-16 15:28:18 -08001004 return err
1005 }
1006
1007 // Clear requests.
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001008 context.requests = []cqueryKey{}
Sasha Smundak4975c822022-11-16 15:28:18 -08001009 return nil
1010}
1011
Sam Delmericocb3c52c2023-02-03 17:40:08 -05001012func (context *mixedBuildBazelContext) runCquery(config Config, ctx *Context) error {
Sasha Smundak4975c822022-11-16 15:28:18 -08001013 if ctx != nil {
1014 ctx.EventHandler.Begin("cquery")
1015 defer ctx.EventHandler.End("cquery")
1016 }
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001017 soongInjectionPath := absolutePath(context.paths.injectedFilesDir())
Lukacs T. Berki3069dd92021-05-11 16:54:29 +02001018 mixedBuildsPath := filepath.Join(soongInjectionPath, "mixed_builds")
1019 if _, err := os.Stat(mixedBuildsPath); os.IsNotExist(err) {
1020 err = os.MkdirAll(mixedBuildsPath, 0777)
Usta Shrestha902fd172022-03-02 15:27:49 -05001021 if err != nil {
1022 return err
1023 }
1024 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001025 if err := writeFileBytesIfChanged(filepath.Join(soongInjectionPath, "WORKSPACE.bazel"), []byte{}, 0666); err != nil {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001026 return err
1027 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001028 if err := writeFileBytesIfChanged(filepath.Join(mixedBuildsPath, "main.bzl"), context.mainBzlFileContents(), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001029 return err
1030 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001031 if err := writeFileBytesIfChanged(filepath.Join(mixedBuildsPath, "BUILD.bazel"), context.mainBuildFileContents(), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001032 return err
1033 }
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001034 cqueryFileRelpath := filepath.Join(context.paths.injectedFilesDir(), "buildroot.cquery")
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001035 if err := writeFileBytesIfChanged(absolutePath(cqueryFileRelpath), context.cqueryStarlarkFileContents(), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001036 return err
1037 }
Jingwen Chen1e347862021-09-02 12:11:49 +00001038
Sam Delmericocb3c52c2023-02-03 17:40:08 -05001039 cqueryCommandWithFlag := context.createBazelCommand(config, context.paths, bazel.CqueryBuildRootRunName, cqueryCmd,
Sasha Smundakb43ae1e2022-07-03 15:57:36 -07001040 "--output=starlark", "--starlark:file="+absolutePath(cqueryFileRelpath))
Wei Licbd181c2022-11-16 08:59:23 -08001041 cqueryOutput, cqueryErrorMessage, cqueryErr := context.issueBazelCommand(cqueryCommandWithFlag)
1042 if cqueryErr != nil {
1043 return cqueryErr
Chris Parsons8d6e4332021-02-22 16:13:50 -05001044 }
Jason Wu52cd1942022-09-08 15:37:57 +00001045 cqueryCommandPrint := fmt.Sprintf("cquery command line:\n %s \n\n\n", printableCqueryCommand(cqueryCommandWithFlag))
Sasha Smundak0e87b182022-12-01 11:46:11 -08001046 if err := os.WriteFile(filepath.Join(soongInjectionPath, "cquery.out"), []byte(cqueryCommandPrint+cqueryOutput), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001047 return err
1048 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001049 cqueryResults := map[string]string{}
1050 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
1051 if strings.Contains(outputLine, ">>") {
1052 splitLine := strings.SplitN(outputLine, ">>", 2)
1053 cqueryResults[splitLine[0]] = splitLine[1]
1054 }
1055 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001056 for _, val := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -05001057 if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
Usta Shrestha902fd172022-03-02 15:27:49 -05001058 context.results[val] = cqueryResult
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001059 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -05001060 return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]",
Wei Licbd181c2022-11-16 08:59:23 -08001061 getCqueryId(val), cqueryOutput, cqueryErrorMessage)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001062 }
1063 }
Sasha Smundak4975c822022-11-16 15:28:18 -08001064 return nil
1065}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001066
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001067func writeFileBytesIfChanged(path string, contents []byte, perm os.FileMode) error {
1068 oldContents, err := os.ReadFile(path)
1069 if err != nil || !bytes.Equal(contents, oldContents) {
1070 err = os.WriteFile(path, contents, perm)
1071 }
1072 return nil
1073}
1074
Sasha Smundak39a301c2022-12-29 17:11:49 -08001075func (context *mixedBuildBazelContext) runAquery(config Config, ctx *Context) error {
Sasha Smundak4975c822022-11-16 15:28:18 -08001076 if ctx != nil {
1077 ctx.EventHandler.Begin("aquery")
1078 defer ctx.EventHandler.End("aquery")
1079 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001080 // Issue an aquery command to retrieve action information about the bazel build tree.
1081 //
Sasha Smundakb43ae1e2022-07-03 15:57:36 -07001082 // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
1083 // proto sources, which would add a number of unnecessary dependencies.
Jason Wu118fd2b2022-10-27 18:41:15 +00001084 extraFlags := []string{"--output=proto", "--include_file_write_contents"}
Yu Liu8d82ac52022-05-17 15:13:28 -07001085 if Bool(config.productVariables.ClangCoverage) {
Sasha Smundakb43ae1e2022-07-03 15:57:36 -07001086 extraFlags = append(extraFlags, "--collect_code_coverage")
1087 paths := make([]string, 0, 2)
1088 if p := config.productVariables.NativeCoveragePaths; len(p) > 0 {
Sasha Smundak0e87b182022-12-01 11:46:11 -08001089 for i := range p {
Wei Licbd181c2022-11-16 08:59:23 -08001090 // TODO(b/259404593) convert path wildcard to regex values
1091 if p[i] == "*" {
1092 p[i] = ".*"
1093 }
1094 }
Sasha Smundakb43ae1e2022-07-03 15:57:36 -07001095 paths = append(paths, JoinWithPrefixAndSeparator(p, "+", ","))
1096 }
1097 if p := config.productVariables.NativeCoverageExcludePaths; len(p) > 0 {
1098 paths = append(paths, JoinWithPrefixAndSeparator(p, "-", ","))
1099 }
1100 if len(paths) > 0 {
1101 extraFlags = append(extraFlags, "--instrumentation_filter="+strings.Join(paths, ","))
Yu Liu8d82ac52022-05-17 15:13:28 -07001102 }
1103 }
Sam Delmericocb3c52c2023-02-03 17:40:08 -05001104 aqueryOutput, _, err := context.issueBazelCommand(context.createBazelCommand(config, context.paths, bazel.AqueryBuildRootRunName, aqueryCmd,
Sasha Smundak4975c822022-11-16 15:28:18 -08001105 extraFlags...))
1106 if err != nil {
Chris Parsons4f069892021-01-15 12:22:41 -05001107 return err
1108 }
Sasha Smundak4975c822022-11-16 15:28:18 -08001109 context.buildStatements, context.depsets, err = bazel.AqueryBuildStatements([]byte(aqueryOutput))
1110 return err
1111}
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001112
Sam Delmericocb3c52c2023-02-03 17:40:08 -05001113func (context *mixedBuildBazelContext) generateBazelSymlinks(config Config, ctx *Context) error {
Sasha Smundak4975c822022-11-16 15:28:18 -08001114 if ctx != nil {
1115 ctx.EventHandler.Begin("symlinks")
1116 defer ctx.EventHandler.End("symlinks")
1117 }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001118 // Issue a build command of the phony root to generate symlink forests for dependencies of the
1119 // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
1120 // but some of symlinks may be required to resolve source dependencies of the build.
Sam Delmericocb3c52c2023-02-03 17:40:08 -05001121 _, _, err := context.issueBazelCommand(context.createBazelCommand(config, context.paths, bazel.BazelBuildPhonyRootRunName, buildCmd))
Sasha Smundak4975c822022-11-16 15:28:18 -08001122 return err
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001123}
Chris Parsonsa798d962020-10-12 23:44:08 -04001124
Sasha Smundak39a301c2022-12-29 17:11:49 -08001125func (context *mixedBuildBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001126 return context.buildStatements
1127}
1128
Sasha Smundak39a301c2022-12-29 17:11:49 -08001129func (context *mixedBuildBazelContext) AqueryDepsets() []bazel.AqueryDepset {
Chris Parsons1a7aca02022-04-25 22:35:15 -04001130 return context.depsets
1131}
1132
Sasha Smundak39a301c2022-12-29 17:11:49 -08001133func (context *mixedBuildBazelContext) OutputBase() string {
Liz Kammer8d62a4f2021-04-08 09:47:28 -04001134 return context.paths.outputBase
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001135}
1136
Chris Parsonsa798d962020-10-12 23:44:08 -04001137// Singleton used for registering BUILD file ninja dependencies (needed
1138// for correctness of builds which use Bazel.
1139func BazelSingleton() Singleton {
1140 return &bazelSingleton{}
1141}
1142
1143type bazelSingleton struct{}
1144
1145func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001146 // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
Chris Parsonsad876012022-08-20 14:48:32 -04001147 if !ctx.Config().IsMixedBuildsEnabled() {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001148 return
1149 }
Chris Parsonsa798d962020-10-12 23:44:08 -04001150
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001151 // Add ninja file dependencies for files which all bazel invocations require.
1152 bazelBuildList := absolutePath(filepath.Join(
Lukacs T. Berkif9008072021-08-16 15:24:48 +02001153 filepath.Dir(ctx.Config().moduleListFile), "bazel.list"))
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001154 ctx.AddNinjaFileDeps(bazelBuildList)
1155
Sasha Smundak0e87b182022-12-01 11:46:11 -08001156 data, err := os.ReadFile(bazelBuildList)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001157 if err != nil {
1158 ctx.Errorf(err.Error())
1159 }
1160 files := strings.Split(strings.TrimSpace(string(data)), "\n")
1161 for _, file := range files {
1162 ctx.AddNinjaFileDeps(file)
1163 }
1164
Chris Parsons1a7aca02022-04-25 22:35:15 -04001165 for _, depset := range ctx.Config().BazelContext.AqueryDepsets() {
1166 var outputs []Path
ustafdb3e342022-11-22 17:11:30 -05001167 var orderOnlies []Path
Chris Parsons0bfb1c02022-05-12 16:43:01 -04001168 for _, depsetDepHash := range depset.TransitiveDepSetHashes {
1169 otherDepsetName := bazelDepsetName(depsetDepHash)
Chris Parsons1a7aca02022-04-25 22:35:15 -04001170 outputs = append(outputs, PathForPhony(ctx, otherDepsetName))
1171 }
1172 for _, artifactPath := range depset.DirectArtifacts {
ustafdb3e342022-11-22 17:11:30 -05001173 pathInBazelOut := PathForBazelOut(ctx, artifactPath)
1174 if artifactPath == "bazel-out/volatile-status.txt" {
1175 // See https://bazel.build/docs/user-manual#workspace-status
1176 orderOnlies = append(orderOnlies, pathInBazelOut)
1177 } else {
1178 outputs = append(outputs, pathInBazelOut)
1179 }
Chris Parsons1a7aca02022-04-25 22:35:15 -04001180 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -04001181 thisDepsetName := bazelDepsetName(depset.ContentHash)
Chris Parsons1a7aca02022-04-25 22:35:15 -04001182 ctx.Build(pctx, BuildParams{
1183 Rule: blueprint.Phony,
1184 Outputs: []WritablePath{PathForPhony(ctx, thisDepsetName)},
1185 Implicits: outputs,
ustafdb3e342022-11-22 17:11:30 -05001186 OrderOnly: orderOnlies,
Chris Parsons1a7aca02022-04-25 22:35:15 -04001187 })
1188 }
1189
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001190 executionRoot := path.Join(ctx.Config().BazelContext.OutputBase(), "execroot", "__main__")
1191 bazelOutDir := path.Join(executionRoot, "bazel-out")
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001192 for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
Sasha Smundak1da064c2022-06-08 16:36:16 -07001193 if len(buildStatement.Command) > 0 {
1194 rule := NewRuleBuilder(pctx, ctx)
1195 createCommand(rule.Command(), buildStatement, executionRoot, bazelOutDir, ctx)
1196 desc := fmt.Sprintf("%s: %s", buildStatement.Mnemonic, buildStatement.OutputPaths)
1197 rule.Build(fmt.Sprintf("bazel %d", index), desc)
1198 continue
1199 }
1200 // Certain actions returned by aquery (for instance FileWrite) do not contain a command
1201 // and thus require special treatment. If BuildStatement were an interface implementing
1202 // buildRule(ctx) function, the code here would just call it.
1203 // Unfortunately, the BuildStatement is defined in
1204 // the 'bazel' package, which cannot depend on 'android' package where ctx is defined,
1205 // because this would cause circular dependency. So, until we move aquery processing
1206 // to the 'android' package, we need to handle special cases here.
Usta Shrestha13fd5ae2023-01-27 10:55:34 -05001207 switch buildStatement.Mnemonic {
1208 case "FileWrite", "SourceSymlinkManifest":
Cole Fausta7347492022-12-16 10:56:24 -08001209 out := PathForBazelOut(ctx, buildStatement.OutputPaths[0])
1210 WriteFileRuleVerbatim(ctx, out, buildStatement.FileContents)
Usta Shrestha13fd5ae2023-01-27 10:55:34 -05001211 case "SymlinkTree":
Sasha Smundakc180dbd2022-07-03 14:55:58 -07001212 // build-runfiles arguments are the manifest file and the target directory
1213 // where it creates the symlink tree according to this manifest (and then
1214 // writes the MANIFEST file to it).
1215 outManifest := PathForBazelOut(ctx, buildStatement.OutputPaths[0])
1216 outManifestPath := outManifest.String()
1217 if !strings.HasSuffix(outManifestPath, "MANIFEST") {
1218 panic("the base name of the symlink tree action should be MANIFEST, got " + outManifestPath)
1219 }
1220 outDir := filepath.Dir(outManifestPath)
1221 ctx.Build(pctx, BuildParams{
1222 Rule: buildRunfilesRule,
1223 Output: outManifest,
1224 Inputs: []Path{PathForBazelOut(ctx, buildStatement.InputPaths[0])},
1225 Description: "symlink tree for " + outDir,
1226 Args: map[string]string{
1227 "outDir": outDir,
1228 },
1229 })
Usta Shrestha13fd5ae2023-01-27 10:55:34 -05001230 default:
Rupert Shuttlewortha29903f2021-04-06 16:17:33 +00001231 panic(fmt.Sprintf("unhandled build statement: %v", buildStatement))
Chris Parsons8d6e4332021-02-22 16:13:50 -05001232 }
Chris Parsonsa798d962020-10-12 23:44:08 -04001233 }
1234}
Chris Parsons8d6e4332021-02-22 16:13:50 -05001235
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001236// Register bazel-owned build statements (obtained from the aquery invocation).
Colin Crossd5c7ddb2022-12-06 16:27:17 -08001237func createCommand(cmd *RuleBuilderCommand, buildStatement bazel.BuildStatement, executionRoot string, bazelOutDir string, ctx BuilderContext) {
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001238 // executionRoot is the action cwd.
1239 cmd.Text(fmt.Sprintf("cd '%s' &&", executionRoot))
1240
1241 // Remove old outputs, as some actions might not rerun if the outputs are detected.
1242 if len(buildStatement.OutputPaths) > 0 {
Jingwen Chenf3b1ec32022-11-07 15:02:48 +00001243 cmd.Text("rm -rf") // -r because outputs can be Bazel dir/tree artifacts.
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001244 for _, outputPath := range buildStatement.OutputPaths {
Usta Shresthaef922252022-06-02 14:23:02 -04001245 cmd.Text(fmt.Sprintf("'%s'", outputPath))
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001246 }
1247 cmd.Text("&&")
1248 }
1249
1250 for _, pair := range buildStatement.Env {
1251 // Set per-action env variables, if any.
1252 cmd.Flag(pair.Key + "=" + pair.Value)
1253 }
1254
1255 // The actual Bazel action.
Colin Crossd5c7ddb2022-12-06 16:27:17 -08001256 if len(buildStatement.Command) > 16*1024 {
1257 commandFile := PathForBazelOut(ctx, buildStatement.OutputPaths[0]+".sh")
1258 WriteFileRule(ctx, commandFile, buildStatement.Command)
1259
1260 cmd.Text("bash").Text(buildStatement.OutputPaths[0] + ".sh").Implicit(commandFile)
1261 } else {
1262 cmd.Text(buildStatement.Command)
1263 }
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001264
1265 for _, outputPath := range buildStatement.OutputPaths {
1266 cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
1267 }
1268 for _, inputPath := range buildStatement.InputPaths {
1269 cmd.Implicit(PathForBazelOut(ctx, inputPath))
1270 }
1271 for _, inputDepsetHash := range buildStatement.InputDepsetHashes {
1272 otherDepsetName := bazelDepsetName(inputDepsetHash)
1273 cmd.Implicit(PathForPhony(ctx, otherDepsetName))
1274 }
1275
1276 if depfile := buildStatement.Depfile; depfile != nil {
1277 // The paths in depfile are relative to `executionRoot`.
1278 // Hence, they need to be corrected by replacing "bazel-out"
1279 // with the full `bazelOutDir`.
1280 // Otherwise, implicit outputs and implicit inputs under "bazel-out/"
1281 // would be deemed missing.
1282 // (Note: The regexp uses a capture group because the version of sed
1283 // does not support a look-behind pattern.)
1284 replacement := fmt.Sprintf(`&& sed -i'' -E 's@(^|\s|")bazel-out/@\1%s/@g' '%s'`,
1285 bazelOutDir, *depfile)
1286 cmd.Text(replacement)
1287 cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile))
1288 }
1289
1290 for _, symlinkPath := range buildStatement.SymlinkPaths {
1291 cmd.ImplicitSymlinkOutput(PathForBazelOut(ctx, symlinkPath))
1292 }
1293}
1294
Chris Parsons8d6e4332021-02-22 16:13:50 -05001295func getCqueryId(key cqueryKey) string {
Chris Parsons787fb362021-10-14 18:43:51 -04001296 return key.label + "|" + getConfigString(key)
Chris Parsons8d6e4332021-02-22 16:13:50 -05001297}
1298
Chris Parsons787fb362021-10-14 18:43:51 -04001299func getConfigString(key cqueryKey) string {
Liz Kammer0940b892022-03-18 15:55:04 -04001300 arch := key.configKey.arch
Chris Parsons787fb362021-10-14 18:43:51 -04001301 if len(arch) == 0 || arch == "common" {
Sasha Smundak9d46dcf2022-06-08 12:10:36 -07001302 if key.configKey.osType.Class == Device {
1303 // For the generic Android, the expected result is "target|android", which
1304 // corresponds to the product_variable_config named "android_target" in
1305 // build/bazel/platforms/BUILD.bazel.
1306 arch = "target"
1307 } else {
1308 // Use host platform, which is currently hardcoded to be x86_64.
1309 arch = "x86_64"
1310 }
Chris Parsons8d6e4332021-02-22 16:13:50 -05001311 }
Usta Shrestha16ac1352022-06-22 11:01:55 -04001312 osName := key.configKey.osType.Name
Colin Cross046fcea2022-12-20 15:32:18 -08001313 if len(osName) == 0 || osName == "common_os" || osName == "linux_glibc" || osName == "linux_musl" {
Chris Parsons787fb362021-10-14 18:43:51 -04001314 // Use host OS, which is currently hardcoded to be linux.
Usta Shrestha16ac1352022-06-22 11:01:55 -04001315 osName = "linux"
Chris Parsons787fb362021-10-14 18:43:51 -04001316 }
Usta Shrestha16ac1352022-06-22 11:01:55 -04001317 return arch + "|" + osName
Chris Parsons787fb362021-10-14 18:43:51 -04001318}
1319
Chris Parsonsf874e462022-05-10 13:50:12 -04001320func GetConfigKey(ctx BaseModuleContext) configKey {
Liz Kammer0940b892022-03-18 15:55:04 -04001321 return configKey{
1322 // use string because Arch is not a valid key in go
1323 arch: ctx.Arch().String(),
1324 osType: ctx.Os(),
1325 }
Chris Parsons8d6e4332021-02-22 16:13:50 -05001326}
Chris Parsons1a7aca02022-04-25 22:35:15 -04001327
Chris Parsons0bfb1c02022-05-12 16:43:01 -04001328func bazelDepsetName(contentHash string) string {
1329 return fmt.Sprintf("bazel_depset_%s", contentHash)
Chris Parsons1a7aca02022-04-25 22:35:15 -04001330}
Sam Delmericocb3c52c2023-02-03 17:40:08 -05001331
1332func EnvironmentVarsFile(config Config) string {
1333 return fmt.Sprintf(bazel.GeneratedBazelFileWarning+`
1334_env = %s
1335
1336env = _env
1337`,
1338 starlark_fmt.PrintStringList(allowedBazelEnvironmentVars, 0),
1339 )
1340}