blob: f4b368ba4c85478056a718ca0cef4130f0ac8573 [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"
Usta Shresthaacd5a0c2022-06-22 11:20:50 -040021 "path"
Chris Parsonsa798d962020-10-12 23:44:08 -040022 "path/filepath"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040023 "runtime"
Cole Faust705968d2022-12-14 11:32:05 -080024 "sort"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040025 "strings"
26 "sync"
Chris Parsonsa798d962020-10-12 23:44:08 -040027
Chris Parsonsad876012022-08-20 14:48:32 -040028 "android/soong/android/allowlists"
Chris Parsons944e7d02021-03-11 11:08:46 -050029 "android/soong/bazel/cquery"
Jingwen Chen1e347862021-09-02 12:11:49 +000030 "android/soong/shared"
Cole Faust8a161be2023-06-14 15:45:12 -070031 "android/soong/starlark_import"
Jingwen Chen379221f2023-03-30 13:19:29 +000032
Chris Parsons1a7aca02022-04-25 22:35:15 -040033 "github.com/google/blueprint"
Liz Kammer690fbac2023-02-10 11:11:17 -050034 "github.com/google/blueprint/metrics"
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")
Sasha Smundak1da064c2022-06-08 16:36:16 -070047)
48
Liz Kammerc13f7852023-05-17 13:01:48 -040049func registerMixedBuildsMutator(ctx RegisterMutatorsContext) {
50 ctx.BottomUp("mixed_builds_prep", mixedBuildsPrepareMutator).Parallel()
Chris Parsonsf874e462022-05-10 13:50:12 -040051}
52
53func RegisterMixedBuildsMutator(ctx RegistrationContext) {
Liz Kammerc13f7852023-05-17 13:01:48 -040054 ctx.FinalDepsMutators(registerMixedBuildsMutator)
Chris Parsonsf874e462022-05-10 13:50:12 -040055}
56
57func mixedBuildsPrepareMutator(ctx BottomUpMutatorContext) {
58 if m := ctx.Module(); m.Enabled() {
59 if mixedBuildMod, ok := m.(MixedBuildBuildable); ok {
MarkDacekf47e1422023-04-19 16:47:36 +000060 mixedBuildEnabled := MixedBuildsEnabled(ctx)
61 queueMixedBuild := mixedBuildMod.IsMixedBuildSupported(ctx) && mixedBuildEnabled == MixedBuildEnabled
MarkDacek9c094ca2023-03-16 19:15:19 +000062 if queueMixedBuild {
Chris Parsonsf874e462022-05-10 13:50:12 -040063 mixedBuildMod.QueueBazelCall(ctx)
64 }
65 }
66 }
67}
68
Liz Kammerf29df7c2021-04-02 13:37:39 -040069type cqueryRequest interface {
70 // Name returns a string name for this request type. Such request type names must be unique,
71 // and must only consist of alphanumeric characters.
72 Name() string
73
74 // StarlarkFunctionBody returns a starlark function body to process this request type.
75 // The returned string is the body of a Starlark function which obtains
76 // all request-relevant information about a target and returns a string containing
77 // this information.
78 // The function should have the following properties:
Cole Faust97d15272022-11-22 14:08:59 -080079 // - The arguments are `target` (a configured target) and `id_string` (the label + configuration).
Liz Kammerf29df7c2021-04-02 13:37:39 -040080 // - The return value must be a string.
81 // - The function body should not be indented outside of its own scope.
82 StarlarkFunctionBody() string
83}
84
Chris Parsons787fb362021-10-14 18:43:51 -040085// Portion of cquery map key to describe target configuration.
86type configKey struct {
Yu Liue4312402023-01-18 09:15:31 -080087 arch string
88 osType OsType
89 apexKey ApexConfigKey
90}
91
92type ApexConfigKey struct {
93 WithinApex bool
94 ApexSdkVersion string
95}
96
97func (c ApexConfigKey) String() string {
98 return fmt.Sprintf("%s_%s", withinApexToString(c.WithinApex), c.ApexSdkVersion)
99}
100
101func withinApexToString(withinApex bool) string {
102 if withinApex {
103 return "within_apex"
104 }
105 return ""
Chris Parsons787fb362021-10-14 18:43:51 -0400106}
107
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700108func (c configKey) String() string {
Yu Liue4312402023-01-18 09:15:31 -0800109 return fmt.Sprintf("%s::%s::%s", c.arch, c.osType, c.apexKey)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700110}
111
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400112// Map key to describe bazel cquery requests.
113type cqueryKey struct {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400114 label string
Liz Kammerf29df7c2021-04-02 13:37:39 -0400115 requestType cqueryRequest
Chris Parsons787fb362021-10-14 18:43:51 -0400116 configKey configKey
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400117}
118
Chris Parsons86dc2c22022-09-28 14:58:41 -0400119func makeCqueryKey(label string, cqueryRequest cqueryRequest, cfgKey configKey) cqueryKey {
120 if strings.HasPrefix(label, "//") {
121 // Normalize Bazel labels to specify main repository explicitly.
122 label = "@" + label
123 }
124 return cqueryKey{label, cqueryRequest, cfgKey}
125}
126
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700127func (c cqueryKey) String() string {
128 return fmt.Sprintf("cquery(%s,%s,%s)", c.label, c.requestType.Name(), c.configKey)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700129}
130
Liz Kammer690fbac2023-02-10 11:11:17 -0500131type invokeBazelContext interface {
132 GetEventHandler() *metrics.EventHandler
133}
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
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700156 // Returns the results of the GetApexInfo query (including output files)
Liz Kammerbe6a7122022-11-04 16:05:11 -0400157 GetApexInfo(label string, cfgkey configKey) (cquery.ApexInfo, error)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700158
Sasha Smundakedd16662022-10-07 14:44:50 -0700159 // Returns the results of the GetCcUnstrippedInfo query
160 GetCcUnstrippedInfo(label string, cfgkey configKey) (cquery.CcUnstrippedInfo, error)
161
Spandan Dasbd156812023-06-05 22:43:13 +0000162 // Returns the results of the GetPrebuiltFileInfo query
163 GetPrebuiltFileInfo(label string, cfgKey configKey) (cquery.PrebuiltFileInfo, error)
164
Chris Parsonsf874e462022-05-10 13:50:12 -0400165 // ** end Cquery Results Retrieval Functions
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400166
167 // Issues commands to Bazel to receive results for all cquery requests
Sasha Smundak4975c822022-11-16 15:28:18 -0800168 // queued in the BazelContext. The ctx argument is optional and is only
169 // used for performance data collection
Liz Kammer690fbac2023-02-10 11:11:17 -0500170 InvokeBazel(config Config, ctx invokeBazelContext) error
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400171
Chris Parsonsad876012022-08-20 14:48:32 -0400172 // Returns true if Bazel handling is enabled for the module with the given name.
173 // Note that this only implies "bazel mixed build" allowlisting. The caller
174 // should independently verify the module is eligible for Bazel handling
175 // (for example, that it is MixedBuildBuildable).
Yu Liue4312402023-01-18 09:15:31 -0800176 IsModuleNameAllowed(moduleName string, withinApex bool) bool
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500177
Yu Liubfb23622023-02-22 10:42:15 -0800178 IsModuleDclaAllowed(moduleName string) bool
179
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500180 // Returns the bazel output base (the root directory for all bazel intermediate outputs).
181 OutputBase() string
182
183 // Returns build statements which should get registered to reflect Bazel's outputs.
Liz Kammera4655a92023-02-10 17:17:28 -0500184 BuildStatementsToRegister() []*bazel.BuildStatement
Chris Parsons1a7aca02022-04-25 22:35:15 -0400185
186 // Returns the depsets defined in Bazel's aquery response.
187 AqueryDepsets() []bazel.AqueryDepset
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400188}
189
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400190type bazelRunner interface {
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000191 issueBazelCommand(cmdRequest bazel.CmdRequest, paths *bazelPaths, eventHandler *metrics.EventHandler) (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.
Liz Kammera4655a92023-02-10 17:17:28 -0500217 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
Yu Liue4312402023-01-18 09:15:31 -0800230 // DCLA modules are enabled when used in apex.
231 bazelDclaEnabledModules map[string]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
Spandan Dasbd156812023-06-05 22:43:13 +0000248 LabelToOutputFiles map[string][]string
249 LabelToCcInfo map[string]cquery.CcInfo
250 LabelToPythonBinary map[string]string
251 LabelToApexInfo map[string]cquery.ApexInfo
252 LabelToCcBinary map[string]cquery.CcUnstrippedInfo
253 LabelToPrebuiltFileInfo map[string]cquery.PrebuiltFileInfo
Yu Liue4312402023-01-18 09:15:31 -0800254
255 BazelRequests map[string]bool
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400256}
257
Yu Liue4312402023-01-18 09:15:31 -0800258func (m MockBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) {
259 key := BuildMockBazelContextRequestKey(label, requestType, cfgKey.arch, cfgKey.osType, cfgKey.apexKey)
260 if m.BazelRequests == nil {
261 m.BazelRequests = make(map[string]bool)
262 }
263 m.BazelRequests[key] = true
Chris Parsons8d6e4332021-02-22 16:13:50 -0500264}
265
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700266func (m MockBazelContext) GetOutputFiles(label string, _ configKey) ([]string, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500267 result, ok := m.LabelToOutputFiles[label]
268 if !ok {
269 return []string{}, fmt.Errorf("no target with label %q in LabelToOutputFiles", label)
270 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400271 return result, nil
Liz Kammer3f9e1552021-04-02 18:47:09 -0400272}
273
Yu Liue4312402023-01-18 09:15:31 -0800274func (m MockBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500275 result, ok := m.LabelToCcInfo[label]
276 if !ok {
Yu Liue4312402023-01-18 09:15:31 -0800277 key := BuildMockBazelContextResultKey(label, cfgKey.arch, cfgKey.osType, cfgKey.apexKey)
278 result, ok = m.LabelToCcInfo[key]
279 if !ok {
280 return cquery.CcInfo{}, fmt.Errorf("no target with label %q in LabelToCcInfo", label)
281 }
Sam Delmericoce39f832023-01-23 14:04:24 -0500282 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400283 return result, nil
284}
285
Liz Kammerbe6a7122022-11-04 16:05:11 -0400286func (m MockBazelContext) GetApexInfo(label string, _ configKey) (cquery.ApexInfo, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500287 result, ok := m.LabelToApexInfo[label]
288 if !ok {
289 return cquery.ApexInfo{}, fmt.Errorf("no target with label %q in LabelToApexInfo", label)
290 }
Liz Kammer0e255ef2022-11-04 16:07:04 -0400291 return result, nil
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700292}
293
Sasha Smundakedd16662022-10-07 14:44:50 -0700294func (m MockBazelContext) GetCcUnstrippedInfo(label string, _ configKey) (cquery.CcUnstrippedInfo, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500295 result, ok := m.LabelToCcBinary[label]
296 if !ok {
297 return cquery.CcUnstrippedInfo{}, fmt.Errorf("no target with label %q in LabelToCcBinary", label)
298 }
Sasha Smundakedd16662022-10-07 14:44:50 -0700299 return result, nil
300}
301
Spandan Dasbd156812023-06-05 22:43:13 +0000302func (m MockBazelContext) GetPrebuiltFileInfo(label string, _ configKey) (cquery.PrebuiltFileInfo, error) {
303 result, ok := m.LabelToPrebuiltFileInfo[label]
304 if !ok {
305 return cquery.PrebuiltFileInfo{}, fmt.Errorf("no target with label %q in LabelToPrebuiltFileInfo", label)
306 }
307 return result, nil
308}
309
Liz Kammer690fbac2023-02-10 11:11:17 -0500310func (m MockBazelContext) InvokeBazel(_ Config, _ invokeBazelContext) error {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400311 panic("unimplemented")
312}
313
Yu Liue4312402023-01-18 09:15:31 -0800314func (m MockBazelContext) IsModuleNameAllowed(_ string, _ bool) bool {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400315 return true
316}
317
Yu Liubfb23622023-02-22 10:42:15 -0800318func (m MockBazelContext) IsModuleDclaAllowed(_ string) bool {
319 return true
320}
321
Liz Kammera92e8442021-04-07 20:25:21 -0400322func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500323
Liz Kammera4655a92023-02-10 17:17:28 -0500324func (m MockBazelContext) BuildStatementsToRegister() []*bazel.BuildStatement {
325 return []*bazel.BuildStatement{}
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500326}
327
Chris Parsons1a7aca02022-04-25 22:35:15 -0400328func (m MockBazelContext) AqueryDepsets() []bazel.AqueryDepset {
329 return []bazel.AqueryDepset{}
330}
331
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400332var _ BazelContext = MockBazelContext{}
333
Yu Liue4312402023-01-18 09:15:31 -0800334func BuildMockBazelContextRequestKey(label string, request cqueryRequest, arch string, osType OsType, apexKey ApexConfigKey) string {
335 cfgKey := configKey{
336 arch: arch,
337 osType: osType,
338 apexKey: apexKey,
339 }
340
341 return strings.Join([]string{label, request.Name(), cfgKey.String()}, "_")
342}
343
344func BuildMockBazelContextResultKey(label string, arch string, osType OsType, apexKey ApexConfigKey) string {
345 cfgKey := configKey{
346 arch: arch,
347 osType: osType,
348 apexKey: apexKey,
349 }
350
351 return strings.Join([]string{label, cfgKey.String()}, "_")
352}
353
Sasha Smundak39a301c2022-12-29 17:11:49 -0800354func (bazelCtx *mixedBuildBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400355 key := makeCqueryKey(label, requestType, cfgKey)
Chris Parsonsf874e462022-05-10 13:50:12 -0400356 bazelCtx.requestMutex.Lock()
357 defer bazelCtx.requestMutex.Unlock()
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500358
359 // Insert key into requests, maintaining the sort, and only if it's not duplicate.
360 keyString := key.String()
361 foundEqual := false
362 notLessThanKeyString := func(i int) bool {
363 s := bazelCtx.requests[i].String()
364 v := strings.Compare(s, keyString)
365 if v == 0 {
366 foundEqual = true
367 }
368 return v >= 0
369 }
370 targetIndex := sort.Search(len(bazelCtx.requests), notLessThanKeyString)
371 if foundEqual {
372 return
373 }
374
375 if targetIndex == len(bazelCtx.requests) {
376 bazelCtx.requests = append(bazelCtx.requests, key)
377 } else {
378 bazelCtx.requests = append(bazelCtx.requests[:targetIndex+1], bazelCtx.requests[targetIndex:]...)
379 bazelCtx.requests[targetIndex] = key
380 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400381}
382
Sasha Smundak39a301c2022-12-29 17:11:49 -0800383func (bazelCtx *mixedBuildBazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400384 key := makeCqueryKey(label, cquery.GetOutputFiles, cfgKey)
Chris Parsonsf874e462022-05-10 13:50:12 -0400385 if rawString, ok := bazelCtx.results[key]; ok {
Chris Parsons944e7d02021-03-11 11:08:46 -0500386 bazelOutput := strings.TrimSpace(rawString)
Chris Parsons86dc2c22022-09-28 14:58:41 -0400387
Chris Parsonsf874e462022-05-10 13:50:12 -0400388 return cquery.GetOutputFiles.ParseResult(bazelOutput), nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400389 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400390 return nil, fmt.Errorf("no bazel response found for %v", key)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400391}
392
Sasha Smundak39a301c2022-12-29 17:11:49 -0800393func (bazelCtx *mixedBuildBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400394 key := makeCqueryKey(label, cquery.GetCcInfo, cfgKey)
Chris Parsonsf874e462022-05-10 13:50:12 -0400395 if rawString, ok := bazelCtx.results[key]; ok {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000396 bazelOutput := strings.TrimSpace(rawString)
Chris Parsonsf874e462022-05-10 13:50:12 -0400397 return cquery.GetCcInfo.ParseResult(bazelOutput)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000398 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400399 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 +0000400}
401
Sasha Smundak39a301c2022-12-29 17:11:49 -0800402func (bazelCtx *mixedBuildBazelContext) GetApexInfo(label string, cfgKey configKey) (cquery.ApexInfo, error) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400403 key := makeCqueryKey(label, cquery.GetApexInfo, cfgKey)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700404 if rawString, ok := bazelCtx.results[key]; ok {
Liz Kammer1b7ed9b2022-11-09 10:05:05 -0500405 return cquery.GetApexInfo.ParseResult(strings.TrimSpace(rawString))
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700406 }
Liz Kammerbe6a7122022-11-04 16:05:11 -0400407 return cquery.ApexInfo{}, fmt.Errorf("no bazel response found for %v", key)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700408}
409
Sasha Smundak39a301c2022-12-29 17:11:49 -0800410func (bazelCtx *mixedBuildBazelContext) GetCcUnstrippedInfo(label string, cfgKey configKey) (cquery.CcUnstrippedInfo, error) {
Sasha Smundakedd16662022-10-07 14:44:50 -0700411 key := makeCqueryKey(label, cquery.GetCcUnstrippedInfo, cfgKey)
412 if rawString, ok := bazelCtx.results[key]; ok {
Liz Kammer1b7ed9b2022-11-09 10:05:05 -0500413 return cquery.GetCcUnstrippedInfo.ParseResult(strings.TrimSpace(rawString))
Sasha Smundakedd16662022-10-07 14:44:50 -0700414 }
415 return cquery.CcUnstrippedInfo{}, fmt.Errorf("no bazel response for %s", key)
416}
417
Spandan Dasbd156812023-06-05 22:43:13 +0000418func (bazelCtx *mixedBuildBazelContext) GetPrebuiltFileInfo(label string, cfgKey configKey) (cquery.PrebuiltFileInfo, error) {
419 key := makeCqueryKey(label, cquery.GetPrebuiltFileInfo, cfgKey)
420 if rawString, ok := bazelCtx.results[key]; ok {
421 return cquery.GetPrebuiltFileInfo.ParseResult(strings.TrimSpace(rawString))
422 }
423 return cquery.PrebuiltFileInfo{}, fmt.Errorf("no bazel response for %s", key)
424}
425
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700426func (n noopBazelContext) QueueBazelRequest(_ string, _ cqueryRequest, _ configKey) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500427 panic("unimplemented")
428}
429
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700430func (n noopBazelContext) GetOutputFiles(_ string, _ configKey) ([]string, error) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500431 panic("unimplemented")
432}
433
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700434func (n noopBazelContext) GetCcInfo(_ string, _ configKey) (cquery.CcInfo, error) {
Chris Parsonsf874e462022-05-10 13:50:12 -0400435 panic("unimplemented")
436}
437
Liz Kammerbe6a7122022-11-04 16:05:11 -0400438func (n noopBazelContext) GetApexInfo(_ string, _ configKey) (cquery.ApexInfo, error) {
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700439 panic("unimplemented")
440}
441
Sasha Smundakedd16662022-10-07 14:44:50 -0700442func (n noopBazelContext) GetCcUnstrippedInfo(_ string, _ configKey) (cquery.CcUnstrippedInfo, error) {
443 //TODO implement me
444 panic("implement me")
445}
446
Spandan Dasbd156812023-06-05 22:43:13 +0000447func (n noopBazelContext) GetPrebuiltFileInfo(_ string, _ configKey) (cquery.PrebuiltFileInfo, error) {
448 panic("implement me")
449}
450
Liz Kammer690fbac2023-02-10 11:11:17 -0500451func (n noopBazelContext) InvokeBazel(_ Config, _ invokeBazelContext) error {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400452 panic("unimplemented")
453}
454
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500455func (m noopBazelContext) OutputBase() string {
456 return ""
457}
458
Yu Liue4312402023-01-18 09:15:31 -0800459func (n noopBazelContext) IsModuleNameAllowed(_ string, _ bool) bool {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400460 return false
461}
462
Yu Liubfb23622023-02-22 10:42:15 -0800463func (n noopBazelContext) IsModuleDclaAllowed(_ string) bool {
464 return false
465}
466
Liz Kammera4655a92023-02-10 17:17:28 -0500467func (m noopBazelContext) BuildStatementsToRegister() []*bazel.BuildStatement {
468 return []*bazel.BuildStatement{}
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500469}
470
Chris Parsons1a7aca02022-04-25 22:35:15 -0400471func (m noopBazelContext) AqueryDepsets() []bazel.AqueryDepset {
472 return []bazel.AqueryDepset{}
473}
474
Yu Liu6a7940c2023-05-09 17:12:22 -0700475func AddToStringSet(set map[string]bool, items []string) {
Yu Liue4312402023-01-18 09:15:31 -0800476 for _, item := range items {
477 set[item] = true
478 }
479}
480
Cole Faust705968d2022-12-14 11:32:05 -0800481func GetBazelEnabledAndDisabledModules(buildMode SoongBuildMode, forceEnabled map[string]struct{}) (map[string]bool, map[string]bool) {
Chris Parsonsef615e52022-08-18 22:04:11 -0400482 disabledModules := map[string]bool{}
483 enabledModules := map[string]bool{}
484
Cole Faust705968d2022-12-14 11:32:05 -0800485 switch buildMode {
Chris Parsonsef615e52022-08-18 22:04:11 -0400486 case BazelProdMode:
Yu Liu6a7940c2023-05-09 17:12:22 -0700487 AddToStringSet(enabledModules, allowlists.ProdMixedBuildsEnabledList)
Cole Faust705968d2022-12-14 11:32:05 -0800488 for enabledAdHocModule := range forceEnabled {
MarkDacekd06db5d2022-11-29 00:47:59 +0000489 enabledModules[enabledAdHocModule] = true
490 }
MarkDacekb78465d2022-10-18 20:10:16 +0000491 case BazelStagingMode:
Chris Parsons66fc7452022-11-04 13:26:17 -0400492 // Staging mode includes all prod modules plus all staging modules.
Yu Liu6a7940c2023-05-09 17:12:22 -0700493 AddToStringSet(enabledModules, allowlists.ProdMixedBuildsEnabledList)
494 AddToStringSet(enabledModules, allowlists.StagingMixedBuildsEnabledList)
Cole Faust705968d2022-12-14 11:32:05 -0800495 for enabledAdHocModule := range forceEnabled {
MarkDacekd06db5d2022-11-29 00:47:59 +0000496 enabledModules[enabledAdHocModule] = true
497 }
Chris Parsonsef615e52022-08-18 22:04:11 -0400498 default:
Chris Parsons21f80272023-06-15 04:02:28 +0000499 panic("Expected BazelProdMode or BazelStagingMode")
Cole Faust705968d2022-12-14 11:32:05 -0800500 }
501 return enabledModules, disabledModules
502}
503
504func GetBazelEnabledModules(buildMode SoongBuildMode) []string {
505 enabledModules, disabledModules := GetBazelEnabledAndDisabledModules(buildMode, nil)
506 enabledList := make([]string, 0, len(enabledModules))
507 for module := range enabledModules {
508 if !disabledModules[module] {
509 enabledList = append(enabledList, module)
510 }
511 }
512 sort.Strings(enabledList)
513 return enabledList
514}
515
516func NewBazelContext(c *config) (BazelContext, error) {
Chris Parsons21f80272023-06-15 04:02:28 +0000517 if c.BuildMode != BazelProdMode && c.BuildMode != BazelStagingMode {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400518 return noopBazelContext{}, nil
519 }
520
Cole Faust705968d2022-12-14 11:32:05 -0800521 enabledModules, disabledModules := GetBazelEnabledAndDisabledModules(c.BuildMode, c.BazelModulesForceEnabledByFlag())
522
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800523 paths := bazelPaths{
524 soongOutDir: c.soongOutDir,
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400525 }
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800526 var missing []string
527 vars := []struct {
528 name string
529 ptr *string
Paul Duffin184366a2022-12-21 15:55:33 +0000530
531 // True if the environment variable needs to be tracked so that changes to the variable
532 // cause the ninja file to be regenerated, false otherwise. False should only be set for
533 // environment variables that have no effect on the generated ninja file.
534 track bool
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800535 }{
Paul Duffin184366a2022-12-21 15:55:33 +0000536 {"BAZEL_HOME", &paths.homeDir, true},
537 {"BAZEL_PATH", &paths.bazelPath, true},
538 {"BAZEL_OUTPUT_BASE", &paths.outputBase, true},
539 {"BAZEL_WORKSPACE", &paths.workspaceDir, true},
540 {"BAZEL_METRICS_DIR", &paths.metricsDir, false},
541 {"BAZEL_DEPS_FILE", &paths.bazelDepsFile, true},
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800542 }
543 for _, v := range vars {
Paul Duffin184366a2022-12-21 15:55:33 +0000544 if v.track {
545 if s := c.Getenv(v.name); len(s) > 1 {
546 *v.ptr = s
547 continue
548 }
549 } else if s, ok := c.env[v.name]; ok {
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800550 *v.ptr = s
551 } else {
552 missing = append(missing, v.name)
553 }
554 }
555 if len(missing) > 0 {
556 return nil, fmt.Errorf("missing required env vars to use bazel: %s", missing)
557 }
Cole Faustb85d1a12022-11-08 18:14:01 -0800558
559 targetBuildVariant := "user"
560 if c.Eng() {
561 targetBuildVariant = "eng"
562 } else if c.Debuggable() {
563 targetBuildVariant = "userdebug"
564 }
565 targetProduct := "unknown"
566 if c.HasDeviceProduct() {
567 targetProduct = c.DeviceProduct()
568 }
Yu Liue4312402023-01-18 09:15:31 -0800569 dclaMixedBuildsEnabledList := []string{}
570 if c.BuildMode == BazelProdMode {
571 dclaMixedBuildsEnabledList = allowlists.ProdDclaMixedBuildsEnabledList
572 } else if c.BuildMode == BazelStagingMode {
573 dclaMixedBuildsEnabledList = append(allowlists.ProdDclaMixedBuildsEnabledList,
574 allowlists.StagingDclaMixedBuildsEnabledList...)
575 }
576 dclaEnabledModules := map[string]bool{}
Yu Liu6a7940c2023-05-09 17:12:22 -0700577 AddToStringSet(dclaEnabledModules, dclaMixedBuildsEnabledList)
Sasha Smundak39a301c2022-12-29 17:11:49 -0800578 return &mixedBuildBazelContext{
Chris Parsons9402ca82023-02-23 17:28:06 -0500579 bazelRunner: &builtinBazelRunner{c.UseBazelProxy, absolutePath(c.outDir)},
Yu Liue4312402023-01-18 09:15:31 -0800580 paths: &paths,
Yu Liue4312402023-01-18 09:15:31 -0800581 bazelEnabledModules: enabledModules,
582 bazelDisabledModules: disabledModules,
583 bazelDclaEnabledModules: dclaEnabledModules,
584 targetProduct: targetProduct,
585 targetBuildVariant: targetBuildVariant,
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400586 }, nil
587}
588
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400589func (p *bazelPaths) BazelMetricsDir() string {
590 return p.metricsDir
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000591}
592
Yu Liue4312402023-01-18 09:15:31 -0800593func (context *mixedBuildBazelContext) IsModuleNameAllowed(moduleName string, withinApex bool) bool {
Chris Parsonsad876012022-08-20 14:48:32 -0400594 if context.bazelDisabledModules[moduleName] {
595 return false
596 }
597 if context.bazelEnabledModules[moduleName] {
598 return true
599 }
Yu Liubfb23622023-02-22 10:42:15 -0800600 if withinApex && context.IsModuleDclaAllowed(moduleName) {
Yu Liue4312402023-01-18 09:15:31 -0800601 return true
602 }
603
Chris Parsons21f80272023-06-15 04:02:28 +0000604 return false
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400605}
606
Yu Liubfb23622023-02-22 10:42:15 -0800607func (context *mixedBuildBazelContext) IsModuleDclaAllowed(moduleName string) bool {
608 return context.bazelDclaEnabledModules[moduleName]
609}
610
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400611func pwdPrefix() string {
612 // Darwin doesn't have /proc
613 if runtime.GOOS != "darwin" {
614 return "PWD=/proc/self/cwd"
615 }
616 return ""
617}
618
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400619type bazelCommand struct {
620 command string
621 // query or label
622 expression string
623}
624
Chris Parsons9402ca82023-02-23 17:28:06 -0500625type builtinBazelRunner struct {
626 useBazelProxy bool
627 outDir string
628}
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400629
Chris Parsons808d84c2021-03-09 20:43:32 -0500630// Issues the given bazel command with given build label and additional flags.
631// Returns (stdout, stderr, error). The first and second return values are strings
632// containing the stdout and stderr of the run command, and an error is returned if
633// the invocation returned an error code.
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000634func (r *builtinBazelRunner) issueBazelCommand(cmdRequest bazel.CmdRequest, paths *bazelPaths, eventHandler *metrics.EventHandler) (string, string, error) {
Chris Parsons9402ca82023-02-23 17:28:06 -0500635 if r.useBazelProxy {
636 eventHandler.Begin("client_proxy")
637 defer eventHandler.End("client_proxy")
638 proxyClient := bazel.NewProxyClient(r.outDir)
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000639 resp, err := proxyClient.IssueCommand(cmdRequest)
Chris Parsons9402ca82023-02-23 17:28:06 -0500640
641 if err != nil {
642 return "", "", err
643 }
644 if len(resp.ErrorString) > 0 {
645 return "", "", fmt.Errorf(resp.ErrorString)
646 }
647 return resp.Stdout, resp.Stderr, nil
Jason Wu52cd1942022-09-08 15:37:57 +0000648 } else {
Chris Parsons9402ca82023-02-23 17:28:06 -0500649 eventHandler.Begin("bazel command")
650 defer eventHandler.End("bazel command")
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000651
652 stdout, stderr, err := bazel.ExecBazel(paths.bazelPath, absolutePath(paths.syntheticWorkspaceDir()), cmdRequest)
653 return string(stdout), string(stderr), err
Jason Wu52cd1942022-09-08 15:37:57 +0000654 }
655}
656
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000657func (context *mixedBuildBazelContext) createBazelCommand(config Config, runName bazel.RunName, command bazelCommand,
658 extraFlags ...string) bazel.CmdRequest {
Cole Faust319abae2023-06-06 15:12:49 -0700659 if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
660 panic("Unknown GOOS: " + runtime.GOOS)
661 }
Romain Jobredeaux41fd5e42021-08-27 15:59:39 +0000662 cmdFlags := []string{
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000663 "--output_base=" + absolutePath(context.paths.outputBase),
Romain Jobredeaux41fd5e42021-08-27 15:59:39 +0000664 command.command,
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700665 command.expression,
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000666 "--profile=" + shared.BazelMetricsFilename(context.paths, runName),
Jingwen Chen91220d72021-03-24 02:18:33 -0400667
Cole Faust319abae2023-06-06 15:12:49 -0700668 "--host_platform=@soong_injection//product_config_platforms:mixed_builds_product-" + context.targetBuildVariant + "_" + runtime.GOOS + "_x86_64",
669 // Don't specify --platforms, because on some products/branches (like kernel-build-tools)
670 // the main platform for mixed_builds_product-variant doesn't exist because an arch isn't
671 // specified in product config. The derivative platforms that config_node transitions into
672 // will still work.
Jingwen Chen583ab212023-05-30 09:45:23 +0000673
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700674 // Suppress noise
675 "--ui_event_filters=-INFO",
Sam Delmerico658a4da2022-11-07 15:53:38 -0500676 "--noshow_progress",
677 "--norun_validations",
678 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400679 cmdFlags = append(cmdFlags, extraFlags...)
680
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700681 extraEnv := []string{
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000682 "HOME=" + context.paths.homeDir,
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200683 pwdPrefix(),
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000684 "BUILD_DIR=" + absolutePath(context.paths.soongOutDir),
Joe Onoratoba29f382022-10-24 06:38:11 -0700685 // Make OUT_DIR absolute here so build/bazel/bin/bazel uses the correct
Jingwen Chen8c523582021-06-01 11:19:53 +0000686 // OUT_DIR at <root>/out, instead of <root>/out/soong/workspace/out.
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000687 "OUT_DIR=" + absolutePath(context.paths.outDir()),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500688 // Disables local host detection of gcc; toolchain information is defined
689 // explicitly in BUILD files.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700690 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1",
691 }
Cole Faust8a161be2023-06-14 15:45:12 -0700692 capturedEnvVars, err := starlark_import.GetStarlarkValue[[]string]("captured_env_vars")
693 if err != nil {
694 panic(err)
695 }
696 for _, envvar := range capturedEnvVars {
Sam Delmericocb3c52c2023-02-03 17:40:08 -0500697 val := config.Getenv(envvar)
698 if val == "" {
699 continue
700 }
701 extraEnv = append(extraEnv, fmt.Sprintf("%s=%s", envvar, val))
702 }
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000703 envVars := append(os.Environ(), extraEnv...)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400704
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000705 return bazel.CmdRequest{cmdFlags, envVars}
Jason Wu52cd1942022-09-08 15:37:57 +0000706}
707
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000708func (context *mixedBuildBazelContext) printableCqueryCommand(bazelCmd bazel.CmdRequest) string {
709 args := append([]string{context.paths.bazelPath}, bazelCmd.Argv...)
710 outputString := strings.Join(bazelCmd.Env, " ") + " \"" + strings.Join(args, "\" \"") + "\""
Jason Wu52cd1942022-09-08 15:37:57 +0000711 return outputString
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400712}
713
Sasha Smundak39a301c2022-12-29 17:11:49 -0800714func (context *mixedBuildBazelContext) mainBzlFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500715 // TODO(cparsons): Define configuration transitions programmatically based
716 // on available archs.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400717 contents := `
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500718#####################################################
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400719# This file is generated by soong_build. Do not edit.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500720#####################################################
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400721def _config_node_transition_impl(settings, attr):
Cole Faustb85d1a12022-11-08 18:14:01 -0800722 if attr.os == "android" and attr.arch == "target":
Cole Faust319abae2023-06-06 15:12:49 -0700723 target = "mixed_builds_product-{VARIANT}"
Cole Faustb85d1a12022-11-08 18:14:01 -0800724 else:
Cole Faust319abae2023-06-06 15:12:49 -0700725 target = "mixed_builds_product-{VARIANT}_%s_%s" % (attr.os, attr.arch)
Yu Liue4312402023-01-18 09:15:31 -0800726 apex_name = ""
727 if attr.within_apex:
728 # //build/bazel/rules/apex:apex_name has to be set to a non_empty value,
729 # otherwise //build/bazel/rules/apex:non_apex will be true and the
730 # "-D__ANDROID_APEX__" compiler flag will be missing. Apex_name is used
731 # in some validation on bazel side which don't really apply in mixed
732 # build because soong will do the work, so we just set it to a fixed
733 # value here.
734 apex_name = "dcla_apex"
735 outputs = {
Jingwen Chen583ab212023-05-30 09:45:23 +0000736 "//command_line_option:platforms": "@soong_injection//product_config_platforms:%s" % target,
Yu Liue4312402023-01-18 09:15:31 -0800737 "@//build/bazel/rules/apex:within_apex": attr.within_apex,
738 "@//build/bazel/rules/apex:min_sdk_version": attr.apex_sdk_version,
739 "@//build/bazel/rules/apex:apex_name": apex_name,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500740 }
741
Yu Liue4312402023-01-18 09:15:31 -0800742 return outputs
743
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400744_config_node_transition = transition(
745 implementation = _config_node_transition_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500746 inputs = [],
747 outputs = [
748 "//command_line_option:platforms",
Yu Liue4312402023-01-18 09:15:31 -0800749 "@//build/bazel/rules/apex:within_apex",
750 "@//build/bazel/rules/apex:min_sdk_version",
751 "@//build/bazel/rules/apex:apex_name",
Chris Parsons8d6e4332021-02-22 16:13:50 -0500752 ],
753)
754
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400755def _passthrough_rule_impl(ctx):
756 return [DefaultInfo(files = depset(ctx.files.deps))]
757
758config_node = rule(
759 implementation = _passthrough_rule_impl,
760 attrs = {
Yu Liue4312402023-01-18 09:15:31 -0800761 "arch" : attr.string(mandatory = True),
762 "os" : attr.string(mandatory = True),
763 "within_apex" : attr.bool(default = False),
764 "apex_sdk_version" : attr.string(mandatory = True),
765 "deps" : attr.label_list(cfg = _config_node_transition, allow_files = True),
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400766 "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
767 },
Chris Parsons8d6e4332021-02-22 16:13:50 -0500768)
769
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400770
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500771# Rule representing the root of the build, to depend on all Bazel targets that
772# are required for the build. Building this target will build the entire Bazel
773# build tree.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400774mixed_build_root = rule(
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400775 implementation = _passthrough_rule_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500776 attrs = {
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400777 "deps" : attr.label_list(),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500778 },
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400779)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500780
781def _phony_root_impl(ctx):
782 return []
783
784# Rule to depend on other targets but build nothing.
785# This is useful as follows: building a target of this rule will generate
786# symlink forests for all dependencies of the target, without executing any
787# actions of the build.
788phony_root = rule(
789 implementation = _phony_root_impl,
790 attrs = {"deps" : attr.label_list()},
791)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400792`
Cole Faustb85d1a12022-11-08 18:14:01 -0800793
794 productReplacer := strings.NewReplacer(
795 "{PRODUCT}", context.targetProduct,
796 "{VARIANT}", context.targetBuildVariant)
797
798 return []byte(productReplacer.Replace(contents))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400799}
800
Sasha Smundak39a301c2022-12-29 17:11:49 -0800801func (context *mixedBuildBazelContext) mainBuildFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500802 // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
803 // architecture mapping.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400804 formatString := `
805# This file is generated by soong_build. Do not edit.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400806load(":main.bzl", "config_node", "mixed_build_root", "phony_root")
807
808%s
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400809
810mixed_build_root(name = "buildroot",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400811 deps = [%s],
Jingwen Chen3952a902022-12-12 12:20:58 +0000812 testonly = True, # Unblocks testonly deps.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400813)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500814
815phony_root(name = "phonyroot",
816 deps = [":buildroot"],
Jingwen Chen3952a902022-12-12 12:20:58 +0000817 testonly = True, # Unblocks testonly deps.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500818)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400819`
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400820 configNodeFormatString := `
821config_node(name = "%s",
822 arch = "%s",
Chris Parsons787fb362021-10-14 18:43:51 -0400823 os = "%s",
Yu Liue4312402023-01-18 09:15:31 -0800824 within_apex = %s,
825 apex_sdk_version = "%s",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400826 deps = [%s],
Jingwen Chen3952a902022-12-12 12:20:58 +0000827 testonly = True, # Unblocks testonly deps.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400828)
829`
830
831 configNodesSection := ""
832
Chris Parsons787fb362021-10-14 18:43:51 -0400833 labelsByConfig := map[string][]string{}
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500834
835 for _, val := range context.requests {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200836 labelString := fmt.Sprintf("\"@%s\"", val.label)
Chris Parsons787fb362021-10-14 18:43:51 -0400837 configString := getConfigString(val)
838 labelsByConfig[configString] = append(labelsByConfig[configString], labelString)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400839 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400840
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500841 // Configs need to be sorted to maintain determinism of the BUILD file.
842 sortedConfigs := make([]string, 0, len(labelsByConfig))
843 for val := range labelsByConfig {
844 sortedConfigs = append(sortedConfigs, val)
845 }
846 sort.Slice(sortedConfigs, func(i, j int) bool { return sortedConfigs[i] < sortedConfigs[j] })
847
Jingwen Chen1e347862021-09-02 12:11:49 +0000848 allLabels := []string{}
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500849 for _, configString := range sortedConfigs {
850 labels := labelsByConfig[configString]
Chris Parsons787fb362021-10-14 18:43:51 -0400851 configTokens := strings.Split(configString, "|")
Yu Liue4312402023-01-18 09:15:31 -0800852 if len(configTokens) < 2 {
Chris Parsons787fb362021-10-14 18:43:51 -0400853 panic(fmt.Errorf("Unexpected config string format: %s", configString))
Jingwen Chen1e347862021-09-02 12:11:49 +0000854 }
Chris Parsons787fb362021-10-14 18:43:51 -0400855 archString := configTokens[0]
856 osString := configTokens[1]
Yu Liue4312402023-01-18 09:15:31 -0800857 withinApex := "False"
858 apexSdkVerString := ""
Chris Parsons787fb362021-10-14 18:43:51 -0400859 targetString := fmt.Sprintf("%s_%s", osString, archString)
Yu Liue4312402023-01-18 09:15:31 -0800860 if len(configTokens) > 2 {
861 targetString += "_" + configTokens[2]
862 if configTokens[2] == withinApexToString(true) {
863 withinApex = "True"
864 }
865 }
866 if len(configTokens) > 3 {
867 targetString += "_" + configTokens[3]
868 apexSdkVerString = configTokens[3]
869 }
Chris Parsons787fb362021-10-14 18:43:51 -0400870 allLabels = append(allLabels, fmt.Sprintf("\":%s\"", targetString))
871 labelsString := strings.Join(labels, ",\n ")
Yu Liue4312402023-01-18 09:15:31 -0800872 configNodesSection += fmt.Sprintf(configNodeFormatString, targetString, archString, osString, withinApex, apexSdkVerString,
873 labelsString)
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400874 }
875
Jingwen Chen1e347862021-09-02 12:11:49 +0000876 return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(allLabels, ",\n ")))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400877}
878
Chris Parsons944e7d02021-03-11 11:08:46 -0500879func indent(original string) string {
880 result := ""
881 for _, line := range strings.Split(original, "\n") {
882 result += " " + line + "\n"
883 }
884 return result
885}
886
Chris Parsons808d84c2021-03-09 20:43:32 -0500887// Returns the file contents of the buildroot.cquery file that should be used for the cquery
888// expression in order to obtain information about buildroot and its dependencies.
Sasha Smundak39a301c2022-12-29 17:11:49 -0800889// The contents of this file depend on the mixedBuildBazelContext's requests; requests are enumerated
Chris Parsons808d84c2021-03-09 20:43:32 -0500890// and grouped by their request type. The data retrieved for each label depends on its
891// request type.
Sasha Smundak39a301c2022-12-29 17:11:49 -0800892func (context *mixedBuildBazelContext) cqueryStarlarkFileContents() []byte {
Liz Kammerf29df7c2021-04-02 13:37:39 -0400893 requestTypeToCqueryIdEntries := map[cqueryRequest][]string{}
Chris Parsons38851d82023-03-15 00:19:32 -0400894 requestTypes := []cqueryRequest{}
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500895 for _, val := range context.requests {
Chris Parsons944e7d02021-03-11 11:08:46 -0500896 cqueryId := getCqueryId(val)
897 mapEntryString := fmt.Sprintf("%q : True", cqueryId)
Chris Parsons38851d82023-03-15 00:19:32 -0400898 if _, seenKey := requestTypeToCqueryIdEntries[val.requestType]; !seenKey {
899 requestTypes = append(requestTypes, val.requestType)
900 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500901 requestTypeToCqueryIdEntries[val.requestType] =
902 append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString)
903 }
904 labelRegistrationMapSection := ""
905 functionDefSection := ""
906 mainSwitchSection := ""
907
908 mapDeclarationFormatString := `
909%s = {
910 %s
911}
912`
913 functionDefFormatString := `
Cole Faust97d15272022-11-22 14:08:59 -0800914def %s(target, id_string):
Chris Parsons944e7d02021-03-11 11:08:46 -0500915%s
916`
917 mainSwitchSectionFormatString := `
918 if id_string in %s:
Cole Faust97d15272022-11-22 14:08:59 -0800919 return id_string + ">>" + %s(target, id_string)
Chris Parsons944e7d02021-03-11 11:08:46 -0500920`
921
Chris Parsons38851d82023-03-15 00:19:32 -0400922 for _, requestType := range requestTypes {
Chris Parsons944e7d02021-03-11 11:08:46 -0500923 labelMapName := requestType.Name() + "_Labels"
924 functionName := requestType.Name() + "_Fn"
925 labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString,
926 labelMapName,
927 strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n "))
928 functionDefSection += fmt.Sprintf(functionDefFormatString,
929 functionName,
930 indent(requestType.StarlarkFunctionBody()))
931 mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString,
932 labelMapName, functionName)
933 }
934
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400935 formatString := `
936# This file is generated by soong_build. Do not edit.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400937
Cole Faustb85d1a12022-11-08 18:14:01 -0800938{LABEL_REGISTRATION_MAP_SECTION}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500939
Cole Faustb85d1a12022-11-08 18:14:01 -0800940{FUNCTION_DEF_SECTION}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500941
942def get_arch(target):
Chris Parsons787fb362021-10-14 18:43:51 -0400943 # TODO(b/199363072): filegroups and file targets aren't associated with any
944 # specific platform architecture in mixed builds. This is consistent with how
945 # Soong treats filegroups, but it may not be the case with manually-written
946 # filegroup BUILD targets.
Chris Parsons8d6e4332021-02-22 16:13:50 -0500947 buildoptions = build_options(target)
Yu Liue4312402023-01-18 09:15:31 -0800948
Jingwen Chen8f222742021-10-07 12:02:23 +0000949 if buildoptions == None:
950 # File targets do not have buildoptions. File targets aren't associated with
Chris Parsons787fb362021-10-14 18:43:51 -0400951 # any specific platform architecture in mixed builds, so use the host.
952 return "x86_64|linux"
Cole Faustb85d1a12022-11-08 18:14:01 -0800953 platforms = buildoptions["//command_line_option:platforms"]
Chris Parsons8d6e4332021-02-22 16:13:50 -0500954 if len(platforms) != 1:
955 # An individual configured target should have only one platform architecture.
956 # Note that it's fine for there to be multiple architectures for the same label,
957 # but each is its own configured target.
958 fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
Cole Faustb85d1a12022-11-08 18:14:01 -0800959 platform_name = platforms[0].name
Chris Parsons8d6e4332021-02-22 16:13:50 -0500960 if platform_name == "host":
961 return "HOST"
Cole Faust319abae2023-06-06 15:12:49 -0700962 if not platform_name.startswith("mixed_builds_product-{TARGET_BUILD_VARIANT}"):
963 fail("expected platform name of the form 'mixed_builds_product-{TARGET_BUILD_VARIANT}_android_<arch>' or 'mixed_builds_product-{TARGET_BUILD_VARIANT}_linux_<arch>', but was " + str(platforms))
964 platform_name = platform_name.removeprefix("mixed_builds_product-{TARGET_BUILD_VARIANT}").removeprefix("_")
Yu Liue4312402023-01-18 09:15:31 -0800965 config_key = ""
Cole Faustb85d1a12022-11-08 18:14:01 -0800966 if not platform_name:
Yu Liue4312402023-01-18 09:15:31 -0800967 config_key = "target|android"
Chris Parsons94a0bba2021-06-04 15:03:47 -0400968 elif platform_name.startswith("android_"):
Yu Liue4312402023-01-18 09:15:31 -0800969 config_key = platform_name.removeprefix("android_") + "|android"
Chris Parsons94a0bba2021-06-04 15:03:47 -0400970 elif platform_name.startswith("linux_"):
Yu Liue4312402023-01-18 09:15:31 -0800971 config_key = platform_name.removeprefix("linux_") + "|linux"
Chris Parsons94a0bba2021-06-04 15:03:47 -0400972 else:
Cole Faust319abae2023-06-06 15:12:49 -0700973 fail("expected platform name of the form 'mixed_builds_product-{TARGET_BUILD_VARIANT}_android_<arch>' or 'mixed_builds_product-{TARGET_BUILD_VARIANT}_linux_<arch>', but was " + str(platforms))
Chris Parsons8d6e4332021-02-22 16:13:50 -0500974
Yu Liue4312402023-01-18 09:15:31 -0800975 within_apex = buildoptions.get("//build/bazel/rules/apex:within_apex")
976 apex_sdk_version = buildoptions.get("//build/bazel/rules/apex:min_sdk_version")
977
978 if within_apex:
979 config_key += "|within_apex"
980 if apex_sdk_version != None and len(apex_sdk_version) > 0:
981 config_key += "|" + apex_sdk_version
982
983 return config_key
984
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400985def format(target):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500986 id_string = str(target.label) + "|" + get_arch(target)
Chris Parsons944e7d02021-03-11 11:08:46 -0500987
Chris Parsons86dc2c22022-09-28 14:58:41 -0400988 # TODO(b/248106697): Remove once Bazel is updated to always normalize labels.
989 if id_string.startswith("//"):
990 id_string = "@" + id_string
991
Cole Faustb85d1a12022-11-08 18:14:01 -0800992 {MAIN_SWITCH_SECTION}
993
Chris Parsons944e7d02021-03-11 11:08:46 -0500994 # This target was not requested via cquery, and thus must be a dependency
995 # of a requested target.
996 return id_string + ">>NONE"
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400997`
Cole Faustb85d1a12022-11-08 18:14:01 -0800998 replacer := strings.NewReplacer(
999 "{TARGET_PRODUCT}", context.targetProduct,
1000 "{TARGET_BUILD_VARIANT}", context.targetBuildVariant,
1001 "{LABEL_REGISTRATION_MAP_SECTION}", labelRegistrationMapSection,
1002 "{FUNCTION_DEF_SECTION}", functionDefSection,
1003 "{MAIN_SWITCH_SECTION}", mainSwitchSection)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001004
Cole Faustb85d1a12022-11-08 18:14:01 -08001005 return []byte(replacer.Replace(formatString))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001006}
1007
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001008// Returns a path containing build-related metadata required for interfacing
1009// with Bazel. Example: out/soong/bazel.
Liz Kammer8d62a4f2021-04-08 09:47:28 -04001010func (p *bazelPaths) intermediatesDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001011 return filepath.Join(p.soongOutDir, "bazel")
Chris Parsons8ccdb632020-11-17 15:41:01 -05001012}
1013
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001014// Returns the path where the contents of the @soong_injection repository live.
1015// It is used by Soong to tell Bazel things it cannot over the command line.
1016func (p *bazelPaths) injectedFilesDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001017 return filepath.Join(p.soongOutDir, bazel.SoongInjectionDirName)
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001018}
1019
1020// Returns the path of the synthetic Bazel workspace that contains a symlink
1021// forest composed the whole source tree and BUILD files generated by bp2build.
1022func (p *bazelPaths) syntheticWorkspaceDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001023 return filepath.Join(p.soongOutDir, "workspace")
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001024}
1025
Jingwen Chen8c523582021-06-01 11:19:53 +00001026// Returns the path to the top level out dir ($OUT_DIR).
1027func (p *bazelPaths) outDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001028 return filepath.Dir(p.soongOutDir)
Jingwen Chen8c523582021-06-01 11:19:53 +00001029}
1030
Sasha Smundak4975c822022-11-16 15:28:18 -08001031const buildrootLabel = "@soong_injection//mixed_builds:buildroot"
1032
1033var (
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001034 cqueryCmd = bazelCommand{"cquery", fmt.Sprintf("deps(%s, 2)", buildrootLabel)}
1035 aqueryCmd = bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)}
1036 buildCmd = bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"}
1037 allBazelCommands = []bazelCommand{aqueryCmd, cqueryCmd, buildCmd}
Sasha Smundak4975c822022-11-16 15:28:18 -08001038)
1039
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001040// Issues commands to Bazel to receive results for all cquery requests
1041// queued in the BazelContext.
Liz Kammer690fbac2023-02-10 11:11:17 -05001042func (context *mixedBuildBazelContext) InvokeBazel(config Config, ctx invokeBazelContext) error {
1043 eventHandler := ctx.GetEventHandler()
1044 eventHandler.Begin("bazel")
1045 defer eventHandler.End("bazel")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001046
Sasha Smundak4975c822022-11-16 15:28:18 -08001047 if metricsDir := context.paths.BazelMetricsDir(); metricsDir != "" {
1048 if err := os.MkdirAll(metricsDir, 0777); err != nil {
1049 return err
1050 }
1051 }
1052 context.results = make(map[cqueryKey]string)
Sam Delmericocb3c52c2023-02-03 17:40:08 -05001053 if err := context.runCquery(config, ctx); err != nil {
Sasha Smundak4975c822022-11-16 15:28:18 -08001054 return err
1055 }
1056 if err := context.runAquery(config, ctx); err != nil {
1057 return err
1058 }
Sam Delmericocb3c52c2023-02-03 17:40:08 -05001059 if err := context.generateBazelSymlinks(config, ctx); err != nil {
Sasha Smundak4975c822022-11-16 15:28:18 -08001060 return err
1061 }
1062
1063 // Clear requests.
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001064 context.requests = []cqueryKey{}
Sasha Smundak4975c822022-11-16 15:28:18 -08001065 return nil
1066}
1067
Liz Kammer690fbac2023-02-10 11:11:17 -05001068func (context *mixedBuildBazelContext) runCquery(config Config, ctx invokeBazelContext) error {
1069 eventHandler := ctx.GetEventHandler()
1070 eventHandler.Begin("cquery")
1071 defer eventHandler.End("cquery")
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001072 soongInjectionPath := absolutePath(context.paths.injectedFilesDir())
Lukacs T. Berki3069dd92021-05-11 16:54:29 +02001073 mixedBuildsPath := filepath.Join(soongInjectionPath, "mixed_builds")
1074 if _, err := os.Stat(mixedBuildsPath); os.IsNotExist(err) {
1075 err = os.MkdirAll(mixedBuildsPath, 0777)
Usta Shrestha902fd172022-03-02 15:27:49 -05001076 if err != nil {
1077 return err
1078 }
1079 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001080 if err := writeFileBytesIfChanged(filepath.Join(soongInjectionPath, "WORKSPACE.bazel"), []byte{}, 0666); err != nil {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001081 return err
1082 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001083 if err := writeFileBytesIfChanged(filepath.Join(mixedBuildsPath, "main.bzl"), context.mainBzlFileContents(), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001084 return err
1085 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001086 if err := writeFileBytesIfChanged(filepath.Join(mixedBuildsPath, "BUILD.bazel"), context.mainBuildFileContents(), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001087 return err
1088 }
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001089 cqueryFileRelpath := filepath.Join(context.paths.injectedFilesDir(), "buildroot.cquery")
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001090 if err := writeFileBytesIfChanged(absolutePath(cqueryFileRelpath), context.cqueryStarlarkFileContents(), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001091 return err
1092 }
Jingwen Chen1e347862021-09-02 12:11:49 +00001093
Yu Liue4312402023-01-18 09:15:31 -08001094 extraFlags := []string{"--output=starlark", "--starlark:file=" + absolutePath(cqueryFileRelpath)}
1095 if Bool(config.productVariables.ClangCoverage) {
1096 extraFlags = append(extraFlags, "--collect_code_coverage")
1097 }
1098
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001099 cqueryCmdRequest := context.createBazelCommand(config, bazel.CqueryBuildRootRunName, cqueryCmd, extraFlags...)
1100 cqueryOutput, cqueryErrorMessage, cqueryErr := context.issueBazelCommand(cqueryCmdRequest, context.paths, eventHandler)
Wei Licbd181c2022-11-16 08:59:23 -08001101 if cqueryErr != nil {
1102 return cqueryErr
Chris Parsons8d6e4332021-02-22 16:13:50 -05001103 }
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001104 cqueryCommandPrint := fmt.Sprintf("cquery command line:\n %s \n\n\n", context.printableCqueryCommand(cqueryCmdRequest))
Sasha Smundak0e87b182022-12-01 11:46:11 -08001105 if err := os.WriteFile(filepath.Join(soongInjectionPath, "cquery.out"), []byte(cqueryCommandPrint+cqueryOutput), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001106 return err
1107 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001108 cqueryResults := map[string]string{}
1109 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
1110 if strings.Contains(outputLine, ">>") {
1111 splitLine := strings.SplitN(outputLine, ">>", 2)
1112 cqueryResults[splitLine[0]] = splitLine[1]
1113 }
1114 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001115 for _, val := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -05001116 if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
Usta Shrestha902fd172022-03-02 15:27:49 -05001117 context.results[val] = cqueryResult
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001118 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -05001119 return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]",
Wei Licbd181c2022-11-16 08:59:23 -08001120 getCqueryId(val), cqueryOutput, cqueryErrorMessage)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001121 }
1122 }
Sasha Smundak4975c822022-11-16 15:28:18 -08001123 return nil
1124}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001125
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001126func writeFileBytesIfChanged(path string, contents []byte, perm os.FileMode) error {
1127 oldContents, err := os.ReadFile(path)
1128 if err != nil || !bytes.Equal(contents, oldContents) {
1129 err = os.WriteFile(path, contents, perm)
1130 }
1131 return nil
1132}
1133
Liz Kammer690fbac2023-02-10 11:11:17 -05001134func (context *mixedBuildBazelContext) runAquery(config Config, ctx invokeBazelContext) error {
1135 eventHandler := ctx.GetEventHandler()
1136 eventHandler.Begin("aquery")
1137 defer eventHandler.End("aquery")
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001138 // Issue an aquery command to retrieve action information about the bazel build tree.
1139 //
Sasha Smundakb43ae1e2022-07-03 15:57:36 -07001140 // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
1141 // proto sources, which would add a number of unnecessary dependencies.
Jason Wu118fd2b2022-10-27 18:41:15 +00001142 extraFlags := []string{"--output=proto", "--include_file_write_contents"}
Yu Liu8d82ac52022-05-17 15:13:28 -07001143 if Bool(config.productVariables.ClangCoverage) {
Sasha Smundakb43ae1e2022-07-03 15:57:36 -07001144 extraFlags = append(extraFlags, "--collect_code_coverage")
1145 paths := make([]string, 0, 2)
1146 if p := config.productVariables.NativeCoveragePaths; len(p) > 0 {
Sasha Smundak0e87b182022-12-01 11:46:11 -08001147 for i := range p {
Wei Licbd181c2022-11-16 08:59:23 -08001148 // TODO(b/259404593) convert path wildcard to regex values
1149 if p[i] == "*" {
1150 p[i] = ".*"
1151 }
1152 }
Sasha Smundakb43ae1e2022-07-03 15:57:36 -07001153 paths = append(paths, JoinWithPrefixAndSeparator(p, "+", ","))
1154 }
1155 if p := config.productVariables.NativeCoverageExcludePaths; len(p) > 0 {
1156 paths = append(paths, JoinWithPrefixAndSeparator(p, "-", ","))
1157 }
1158 if len(paths) > 0 {
1159 extraFlags = append(extraFlags, "--instrumentation_filter="+strings.Join(paths, ","))
Yu Liu8d82ac52022-05-17 15:13:28 -07001160 }
1161 }
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001162 aqueryOutput, _, err := context.issueBazelCommand(context.createBazelCommand(config, bazel.AqueryBuildRootRunName, aqueryCmd,
1163 extraFlags...), context.paths, eventHandler)
Sasha Smundak4975c822022-11-16 15:28:18 -08001164 if err != nil {
Chris Parsons4f069892021-01-15 12:22:41 -05001165 return err
1166 }
Liz Kammer690fbac2023-02-10 11:11:17 -05001167 context.buildStatements, context.depsets, err = bazel.AqueryBuildStatements([]byte(aqueryOutput), eventHandler)
Sasha Smundak4975c822022-11-16 15:28:18 -08001168 return err
1169}
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001170
Liz Kammer690fbac2023-02-10 11:11:17 -05001171func (context *mixedBuildBazelContext) generateBazelSymlinks(config Config, ctx invokeBazelContext) error {
1172 eventHandler := ctx.GetEventHandler()
1173 eventHandler.Begin("symlinks")
1174 defer eventHandler.End("symlinks")
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001175 // Issue a build command of the phony root to generate symlink forests for dependencies of the
1176 // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
1177 // but some of symlinks may be required to resolve source dependencies of the build.
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001178 _, _, err := context.issueBazelCommand(context.createBazelCommand(config, bazel.BazelBuildPhonyRootRunName, buildCmd), context.paths, eventHandler)
Sasha Smundak4975c822022-11-16 15:28:18 -08001179 return err
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001180}
Chris Parsonsa798d962020-10-12 23:44:08 -04001181
Liz Kammera4655a92023-02-10 17:17:28 -05001182func (context *mixedBuildBazelContext) BuildStatementsToRegister() []*bazel.BuildStatement {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001183 return context.buildStatements
1184}
1185
Sasha Smundak39a301c2022-12-29 17:11:49 -08001186func (context *mixedBuildBazelContext) AqueryDepsets() []bazel.AqueryDepset {
Chris Parsons1a7aca02022-04-25 22:35:15 -04001187 return context.depsets
1188}
1189
Sasha Smundak39a301c2022-12-29 17:11:49 -08001190func (context *mixedBuildBazelContext) OutputBase() string {
Liz Kammer8d62a4f2021-04-08 09:47:28 -04001191 return context.paths.outputBase
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001192}
1193
Chris Parsonsa798d962020-10-12 23:44:08 -04001194// Singleton used for registering BUILD file ninja dependencies (needed
1195// for correctness of builds which use Bazel.
1196func BazelSingleton() Singleton {
1197 return &bazelSingleton{}
1198}
1199
1200type bazelSingleton struct{}
1201
1202func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001203 // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
Chris Parsonsad876012022-08-20 14:48:32 -04001204 if !ctx.Config().IsMixedBuildsEnabled() {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001205 return
1206 }
Chris Parsonsa798d962020-10-12 23:44:08 -04001207
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001208 // Add ninja file dependencies for files which all bazel invocations require.
1209 bazelBuildList := absolutePath(filepath.Join(
Lukacs T. Berkif9008072021-08-16 15:24:48 +02001210 filepath.Dir(ctx.Config().moduleListFile), "bazel.list"))
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001211 ctx.AddNinjaFileDeps(bazelBuildList)
1212
Sasha Smundak0e87b182022-12-01 11:46:11 -08001213 data, err := os.ReadFile(bazelBuildList)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001214 if err != nil {
1215 ctx.Errorf(err.Error())
1216 }
1217 files := strings.Split(strings.TrimSpace(string(data)), "\n")
1218 for _, file := range files {
1219 ctx.AddNinjaFileDeps(file)
1220 }
1221
Chris Parsons1a7aca02022-04-25 22:35:15 -04001222 for _, depset := range ctx.Config().BazelContext.AqueryDepsets() {
1223 var outputs []Path
ustafdb3e342022-11-22 17:11:30 -05001224 var orderOnlies []Path
Chris Parsons0bfb1c02022-05-12 16:43:01 -04001225 for _, depsetDepHash := range depset.TransitiveDepSetHashes {
1226 otherDepsetName := bazelDepsetName(depsetDepHash)
Chris Parsons1a7aca02022-04-25 22:35:15 -04001227 outputs = append(outputs, PathForPhony(ctx, otherDepsetName))
1228 }
1229 for _, artifactPath := range depset.DirectArtifacts {
ustafdb3e342022-11-22 17:11:30 -05001230 pathInBazelOut := PathForBazelOut(ctx, artifactPath)
1231 if artifactPath == "bazel-out/volatile-status.txt" {
1232 // See https://bazel.build/docs/user-manual#workspace-status
1233 orderOnlies = append(orderOnlies, pathInBazelOut)
1234 } else {
1235 outputs = append(outputs, pathInBazelOut)
1236 }
Chris Parsons1a7aca02022-04-25 22:35:15 -04001237 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -04001238 thisDepsetName := bazelDepsetName(depset.ContentHash)
Chris Parsons1a7aca02022-04-25 22:35:15 -04001239 ctx.Build(pctx, BuildParams{
1240 Rule: blueprint.Phony,
1241 Outputs: []WritablePath{PathForPhony(ctx, thisDepsetName)},
1242 Implicits: outputs,
ustafdb3e342022-11-22 17:11:30 -05001243 OrderOnly: orderOnlies,
Chris Parsons1a7aca02022-04-25 22:35:15 -04001244 })
1245 }
1246
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001247 executionRoot := path.Join(ctx.Config().BazelContext.OutputBase(), "execroot", "__main__")
1248 bazelOutDir := path.Join(executionRoot, "bazel-out")
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001249 for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
Liz Kammera4655a92023-02-10 17:17:28 -05001250 // nil build statements are a valid case where we do not create an action because it is
1251 // unnecessary or handled by other processing
1252 if buildStatement == nil {
1253 continue
1254 }
Sasha Smundak1da064c2022-06-08 16:36:16 -07001255 if len(buildStatement.Command) > 0 {
1256 rule := NewRuleBuilder(pctx, ctx)
1257 createCommand(rule.Command(), buildStatement, executionRoot, bazelOutDir, ctx)
1258 desc := fmt.Sprintf("%s: %s", buildStatement.Mnemonic, buildStatement.OutputPaths)
1259 rule.Build(fmt.Sprintf("bazel %d", index), desc)
1260 continue
1261 }
1262 // Certain actions returned by aquery (for instance FileWrite) do not contain a command
1263 // and thus require special treatment. If BuildStatement were an interface implementing
1264 // buildRule(ctx) function, the code here would just call it.
1265 // Unfortunately, the BuildStatement is defined in
1266 // the 'bazel' package, which cannot depend on 'android' package where ctx is defined,
1267 // because this would cause circular dependency. So, until we move aquery processing
1268 // to the 'android' package, we need to handle special cases here.
Usta Shrestha13fd5ae2023-01-27 10:55:34 -05001269 switch buildStatement.Mnemonic {
1270 case "FileWrite", "SourceSymlinkManifest":
Cole Fausta7347492022-12-16 10:56:24 -08001271 out := PathForBazelOut(ctx, buildStatement.OutputPaths[0])
1272 WriteFileRuleVerbatim(ctx, out, buildStatement.FileContents)
Usta Shrestha13fd5ae2023-01-27 10:55:34 -05001273 case "SymlinkTree":
Sasha Smundakc180dbd2022-07-03 14:55:58 -07001274 // build-runfiles arguments are the manifest file and the target directory
1275 // where it creates the symlink tree according to this manifest (and then
1276 // writes the MANIFEST file to it).
1277 outManifest := PathForBazelOut(ctx, buildStatement.OutputPaths[0])
1278 outManifestPath := outManifest.String()
1279 if !strings.HasSuffix(outManifestPath, "MANIFEST") {
1280 panic("the base name of the symlink tree action should be MANIFEST, got " + outManifestPath)
1281 }
1282 outDir := filepath.Dir(outManifestPath)
1283 ctx.Build(pctx, BuildParams{
1284 Rule: buildRunfilesRule,
1285 Output: outManifest,
1286 Inputs: []Path{PathForBazelOut(ctx, buildStatement.InputPaths[0])},
1287 Description: "symlink tree for " + outDir,
1288 Args: map[string]string{
1289 "outDir": outDir,
1290 },
1291 })
Usta Shrestha13fd5ae2023-01-27 10:55:34 -05001292 default:
Rupert Shuttlewortha29903f2021-04-06 16:17:33 +00001293 panic(fmt.Sprintf("unhandled build statement: %v", buildStatement))
Chris Parsons8d6e4332021-02-22 16:13:50 -05001294 }
Chris Parsonsa798d962020-10-12 23:44:08 -04001295 }
1296}
Chris Parsons8d6e4332021-02-22 16:13:50 -05001297
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001298// Register bazel-owned build statements (obtained from the aquery invocation).
Liz Kammera4655a92023-02-10 17:17:28 -05001299func createCommand(cmd *RuleBuilderCommand, buildStatement *bazel.BuildStatement, executionRoot string, bazelOutDir string, ctx BuilderContext) {
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001300 // executionRoot is the action cwd.
1301 cmd.Text(fmt.Sprintf("cd '%s' &&", executionRoot))
1302
1303 // Remove old outputs, as some actions might not rerun if the outputs are detected.
1304 if len(buildStatement.OutputPaths) > 0 {
Jingwen Chenf3b1ec32022-11-07 15:02:48 +00001305 cmd.Text("rm -rf") // -r because outputs can be Bazel dir/tree artifacts.
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001306 for _, outputPath := range buildStatement.OutputPaths {
Usta Shresthaef922252022-06-02 14:23:02 -04001307 cmd.Text(fmt.Sprintf("'%s'", outputPath))
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001308 }
1309 cmd.Text("&&")
1310 }
1311
1312 for _, pair := range buildStatement.Env {
1313 // Set per-action env variables, if any.
1314 cmd.Flag(pair.Key + "=" + pair.Value)
1315 }
1316
1317 // The actual Bazel action.
Colin Crossd5c7ddb2022-12-06 16:27:17 -08001318 if len(buildStatement.Command) > 16*1024 {
1319 commandFile := PathForBazelOut(ctx, buildStatement.OutputPaths[0]+".sh")
1320 WriteFileRule(ctx, commandFile, buildStatement.Command)
1321
1322 cmd.Text("bash").Text(buildStatement.OutputPaths[0] + ".sh").Implicit(commandFile)
1323 } else {
1324 cmd.Text(buildStatement.Command)
1325 }
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001326
1327 for _, outputPath := range buildStatement.OutputPaths {
1328 cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
1329 }
1330 for _, inputPath := range buildStatement.InputPaths {
1331 cmd.Implicit(PathForBazelOut(ctx, inputPath))
1332 }
1333 for _, inputDepsetHash := range buildStatement.InputDepsetHashes {
1334 otherDepsetName := bazelDepsetName(inputDepsetHash)
1335 cmd.Implicit(PathForPhony(ctx, otherDepsetName))
1336 }
1337
1338 if depfile := buildStatement.Depfile; depfile != nil {
1339 // The paths in depfile are relative to `executionRoot`.
1340 // Hence, they need to be corrected by replacing "bazel-out"
1341 // with the full `bazelOutDir`.
1342 // Otherwise, implicit outputs and implicit inputs under "bazel-out/"
1343 // would be deemed missing.
1344 // (Note: The regexp uses a capture group because the version of sed
1345 // does not support a look-behind pattern.)
1346 replacement := fmt.Sprintf(`&& sed -i'' -E 's@(^|\s|")bazel-out/@\1%s/@g' '%s'`,
1347 bazelOutDir, *depfile)
1348 cmd.Text(replacement)
1349 cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile))
1350 }
1351
1352 for _, symlinkPath := range buildStatement.SymlinkPaths {
1353 cmd.ImplicitSymlinkOutput(PathForBazelOut(ctx, symlinkPath))
1354 }
1355}
1356
Chris Parsons8d6e4332021-02-22 16:13:50 -05001357func getCqueryId(key cqueryKey) string {
Chris Parsons787fb362021-10-14 18:43:51 -04001358 return key.label + "|" + getConfigString(key)
Chris Parsons8d6e4332021-02-22 16:13:50 -05001359}
1360
Chris Parsons787fb362021-10-14 18:43:51 -04001361func getConfigString(key cqueryKey) string {
Liz Kammer0940b892022-03-18 15:55:04 -04001362 arch := key.configKey.arch
Chris Parsons787fb362021-10-14 18:43:51 -04001363 if len(arch) == 0 || arch == "common" {
Sasha Smundak9d46dcf2022-06-08 12:10:36 -07001364 if key.configKey.osType.Class == Device {
1365 // For the generic Android, the expected result is "target|android", which
1366 // corresponds to the product_variable_config named "android_target" in
1367 // build/bazel/platforms/BUILD.bazel.
1368 arch = "target"
1369 } else {
1370 // Use host platform, which is currently hardcoded to be x86_64.
1371 arch = "x86_64"
1372 }
Chris Parsons8d6e4332021-02-22 16:13:50 -05001373 }
Usta Shrestha16ac1352022-06-22 11:01:55 -04001374 osName := key.configKey.osType.Name
Colin Cross046fcea2022-12-20 15:32:18 -08001375 if len(osName) == 0 || osName == "common_os" || osName == "linux_glibc" || osName == "linux_musl" {
Chris Parsons787fb362021-10-14 18:43:51 -04001376 // Use host OS, which is currently hardcoded to be linux.
Usta Shrestha16ac1352022-06-22 11:01:55 -04001377 osName = "linux"
Chris Parsons787fb362021-10-14 18:43:51 -04001378 }
Yu Liue4312402023-01-18 09:15:31 -08001379 keyString := arch + "|" + osName
1380 if key.configKey.apexKey.WithinApex {
1381 keyString += "|" + withinApexToString(key.configKey.apexKey.WithinApex)
1382 }
1383
1384 if len(key.configKey.apexKey.ApexSdkVersion) > 0 {
1385 keyString += "|" + key.configKey.apexKey.ApexSdkVersion
1386 }
1387
1388 return keyString
Chris Parsons787fb362021-10-14 18:43:51 -04001389}
1390
Chris Parsonsf874e462022-05-10 13:50:12 -04001391func GetConfigKey(ctx BaseModuleContext) configKey {
Liz Kammer0940b892022-03-18 15:55:04 -04001392 return configKey{
1393 // use string because Arch is not a valid key in go
1394 arch: ctx.Arch().String(),
1395 osType: ctx.Os(),
1396 }
Chris Parsons8d6e4332021-02-22 16:13:50 -05001397}
Chris Parsons1a7aca02022-04-25 22:35:15 -04001398
Yu Liue4312402023-01-18 09:15:31 -08001399func GetConfigKeyApexVariant(ctx BaseModuleContext, apexKey *ApexConfigKey) configKey {
1400 configKey := GetConfigKey(ctx)
1401
1402 if apexKey != nil {
1403 configKey.apexKey = ApexConfigKey{
1404 WithinApex: apexKey.WithinApex,
1405 ApexSdkVersion: apexKey.ApexSdkVersion,
1406 }
1407 }
1408
1409 return configKey
1410}
1411
Chris Parsons0bfb1c02022-05-12 16:43:01 -04001412func bazelDepsetName(contentHash string) string {
1413 return fmt.Sprintf("bazel_depset_%s", contentHash)
Chris Parsons1a7aca02022-04-25 22:35:15 -04001414}