blob: 94bc88b42c87154a8efaca60b0cc636e1ccf8ad0 [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"
Spandan Dasaf4ccaa2023-06-29 01:15:51 +000019 "crypto/sha1"
20 "encoding/hex"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040021 "fmt"
22 "os"
Usta Shresthaacd5a0c2022-06-22 11:20:50 -040023 "path"
Chris Parsonsa798d962020-10-12 23:44:08 -040024 "path/filepath"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040025 "runtime"
Cole Faust705968d2022-12-14 11:32:05 -080026 "sort"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040027 "strings"
28 "sync"
Chris Parsonsa798d962020-10-12 23:44:08 -040029
Chris Parsonsad876012022-08-20 14:48:32 -040030 "android/soong/android/allowlists"
Chris Parsons944e7d02021-03-11 11:08:46 -050031 "android/soong/bazel/cquery"
Jingwen Chen1e347862021-09-02 12:11:49 +000032 "android/soong/shared"
Cole Faust8a161be2023-06-14 15:45:12 -070033 "android/soong/starlark_import"
Jingwen Chen379221f2023-03-30 13:19:29 +000034
Chris Parsons1a7aca02022-04-25 22:35:15 -040035 "github.com/google/blueprint"
Liz Kammer690fbac2023-02-10 11:11:17 -050036 "github.com/google/blueprint/metrics"
Liz Kammer8206d4f2021-03-03 16:40:52 -050037
Patrice Arruda05ab2d02020-12-12 06:24:26 +000038 "android/soong/bazel"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040039)
40
Sasha Smundak1da064c2022-06-08 16:36:16 -070041var (
Sasha Smundakc180dbd2022-07-03 14:55:58 -070042 _ = pctx.HostBinToolVariable("bazelBuildRunfilesTool", "build-runfiles")
43 buildRunfilesRule = pctx.AndroidStaticRule("bazelBuildRunfiles", blueprint.RuleParams{
44 Command: "${bazelBuildRunfilesTool} ${in} ${outDir}",
45 Depfile: "",
46 Description: "",
47 CommandDeps: []string{"${bazelBuildRunfilesTool}"},
48 }, "outDir")
Sasha Smundak1da064c2022-06-08 16:36:16 -070049)
50
Liz Kammerc13f7852023-05-17 13:01:48 -040051func registerMixedBuildsMutator(ctx RegisterMutatorsContext) {
52 ctx.BottomUp("mixed_builds_prep", mixedBuildsPrepareMutator).Parallel()
Chris Parsonsf874e462022-05-10 13:50:12 -040053}
54
55func RegisterMixedBuildsMutator(ctx RegistrationContext) {
Liz Kammerc13f7852023-05-17 13:01:48 -040056 ctx.FinalDepsMutators(registerMixedBuildsMutator)
Chris Parsonsf874e462022-05-10 13:50:12 -040057}
58
59func mixedBuildsPrepareMutator(ctx BottomUpMutatorContext) {
60 if m := ctx.Module(); m.Enabled() {
61 if mixedBuildMod, ok := m.(MixedBuildBuildable); ok {
MarkDacekf47e1422023-04-19 16:47:36 +000062 mixedBuildEnabled := MixedBuildsEnabled(ctx)
63 queueMixedBuild := mixedBuildMod.IsMixedBuildSupported(ctx) && mixedBuildEnabled == MixedBuildEnabled
MarkDacek9c094ca2023-03-16 19:15:19 +000064 if queueMixedBuild {
Chris Parsonsf874e462022-05-10 13:50:12 -040065 mixedBuildMod.QueueBazelCall(ctx)
66 }
67 }
68 }
69}
70
Liz Kammerf29df7c2021-04-02 13:37:39 -040071type cqueryRequest interface {
72 // Name returns a string name for this request type. Such request type names must be unique,
73 // and must only consist of alphanumeric characters.
74 Name() string
75
76 // StarlarkFunctionBody returns a starlark function body to process this request type.
77 // The returned string is the body of a Starlark function which obtains
78 // all request-relevant information about a target and returns a string containing
79 // this information.
80 // The function should have the following properties:
Cole Faust97d15272022-11-22 14:08:59 -080081 // - The arguments are `target` (a configured target) and `id_string` (the label + configuration).
Liz Kammerf29df7c2021-04-02 13:37:39 -040082 // - The return value must be a string.
83 // - The function body should not be indented outside of its own scope.
84 StarlarkFunctionBody() string
85}
86
Chris Parsons787fb362021-10-14 18:43:51 -040087// Portion of cquery map key to describe target configuration.
88type configKey struct {
Yu Liue4312402023-01-18 09:15:31 -080089 arch string
90 osType OsType
91 apexKey ApexConfigKey
92}
93
94type ApexConfigKey struct {
95 WithinApex bool
96 ApexSdkVersion string
Spandan Das40b79f82023-06-25 20:56:06 +000097 ApiDomain string
Yu Liue4312402023-01-18 09:15:31 -080098}
99
100func (c ApexConfigKey) String() string {
Spandan Das40b79f82023-06-25 20:56:06 +0000101 return fmt.Sprintf("%s_%s_%s", withinApexToString(c.WithinApex), c.ApexSdkVersion, c.ApiDomain)
Yu Liue4312402023-01-18 09:15:31 -0800102}
103
104func withinApexToString(withinApex bool) string {
105 if withinApex {
106 return "within_apex"
107 }
108 return ""
Chris Parsons787fb362021-10-14 18:43:51 -0400109}
110
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700111func (c configKey) String() string {
Yu Liue4312402023-01-18 09:15:31 -0800112 return fmt.Sprintf("%s::%s::%s", c.arch, c.osType, c.apexKey)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700113}
114
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400115// Map key to describe bazel cquery requests.
116type cqueryKey struct {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400117 label string
Liz Kammerf29df7c2021-04-02 13:37:39 -0400118 requestType cqueryRequest
Chris Parsons787fb362021-10-14 18:43:51 -0400119 configKey configKey
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400120}
121
Chris Parsons86dc2c22022-09-28 14:58:41 -0400122func makeCqueryKey(label string, cqueryRequest cqueryRequest, cfgKey configKey) cqueryKey {
123 if strings.HasPrefix(label, "//") {
124 // Normalize Bazel labels to specify main repository explicitly.
125 label = "@" + label
126 }
127 return cqueryKey{label, cqueryRequest, cfgKey}
128}
129
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700130func (c cqueryKey) String() string {
131 return fmt.Sprintf("cquery(%s,%s,%s)", c.label, c.requestType.Name(), c.configKey)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700132}
133
Liz Kammer690fbac2023-02-10 11:11:17 -0500134type invokeBazelContext interface {
135 GetEventHandler() *metrics.EventHandler
136}
137
Chris Parsonsf874e462022-05-10 13:50:12 -0400138// BazelContext is a context object useful for interacting with Bazel during
139// the course of a build. Use of Bazel to evaluate part of the build graph
140// is referred to as a "mixed build". (Some modules are managed by Soong,
141// some are managed by Bazel). To facilitate interop between these build
142// subgraphs, Soong may make requests to Bazel and evaluate their responses
143// so that Soong modules may accurately depend on Bazel targets.
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400144type BazelContext interface {
Chris Parsonsf874e462022-05-10 13:50:12 -0400145 // Add a cquery request to the bazel request queue. All queued requests
146 // will be sent to Bazel on a subsequent invocation of InvokeBazel.
147 QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey)
148
149 // ** Cquery Results Retrieval Functions
150 // The below functions pertain to retrieving cquery results from a prior
151 // InvokeBazel function call and parsing the results.
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400152
153 // Returns result files built by building the given bazel target label.
Chris Parsonsf874e462022-05-10 13:50:12 -0400154 GetOutputFiles(label string, cfgKey configKey) ([]string, error)
Chris Parsons8d6e4332021-02-22 16:13:50 -0500155
Chris Parsons944e7d02021-03-11 11:08:46 -0500156 // Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order).
Chris Parsonsf874e462022-05-10 13:50:12 -0400157 GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error)
Liz Kammer3f9e1552021-04-02 18:47:09 -0400158
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700159 // Returns the results of the GetApexInfo query (including output files)
Liz Kammerbe6a7122022-11-04 16:05:11 -0400160 GetApexInfo(label string, cfgkey configKey) (cquery.ApexInfo, error)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700161
Sasha Smundakedd16662022-10-07 14:44:50 -0700162 // Returns the results of the GetCcUnstrippedInfo query
163 GetCcUnstrippedInfo(label string, cfgkey configKey) (cquery.CcUnstrippedInfo, error)
164
Spandan Dasbd156812023-06-05 22:43:13 +0000165 // Returns the results of the GetPrebuiltFileInfo query
166 GetPrebuiltFileInfo(label string, cfgKey configKey) (cquery.PrebuiltFileInfo, error)
167
Chris Parsonsf874e462022-05-10 13:50:12 -0400168 // ** end Cquery Results Retrieval Functions
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400169
170 // Issues commands to Bazel to receive results for all cquery requests
Sasha Smundak4975c822022-11-16 15:28:18 -0800171 // queued in the BazelContext. The ctx argument is optional and is only
172 // used for performance data collection
Liz Kammer690fbac2023-02-10 11:11:17 -0500173 InvokeBazel(config Config, ctx invokeBazelContext) error
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400174
Chris Parsonsad876012022-08-20 14:48:32 -0400175 // Returns true if Bazel handling is enabled for the module with the given name.
176 // Note that this only implies "bazel mixed build" allowlisting. The caller
177 // should independently verify the module is eligible for Bazel handling
178 // (for example, that it is MixedBuildBuildable).
Yu Liue4312402023-01-18 09:15:31 -0800179 IsModuleNameAllowed(moduleName string, withinApex bool) bool
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500180
181 // Returns the bazel output base (the root directory for all bazel intermediate outputs).
182 OutputBase() string
183
184 // Returns build statements which should get registered to reflect Bazel's outputs.
Liz Kammera4655a92023-02-10 17:17:28 -0500185 BuildStatementsToRegister() []*bazel.BuildStatement
Chris Parsons1a7aca02022-04-25 22:35:15 -0400186
187 // Returns the depsets defined in Bazel's aquery response.
188 AqueryDepsets() []bazel.AqueryDepset
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400189}
190
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400191type bazelRunner interface {
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000192 issueBazelCommand(cmdRequest bazel.CmdRequest, paths *bazelPaths, eventHandler *metrics.EventHandler) (output string, errorMessage string, error error)
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400193}
194
195type bazelPaths struct {
MarkDacek0d5bca52022-10-10 20:07:48 +0000196 homeDir string
197 bazelPath string
198 outputBase string
199 workspaceDir string
200 soongOutDir string
201 metricsDir string
202 bazelDepsFile string
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400203}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400204
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400205// A context object which tracks queued requests that need to be made to Bazel,
206// and their results after the requests have been made.
Sasha Smundak39a301c2022-12-29 17:11:49 -0800207type mixedBuildBazelContext struct {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400208 bazelRunner
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500209 paths *bazelPaths
210 // cquery requests that have not yet been issued to Bazel. This list is maintained
211 // in a sorted state, and is guaranteed to have no duplicates.
212 requests []cqueryKey
213 requestMutex sync.Mutex // requests can be written in parallel
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400214
215 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500216
217 // Build statements which should get registered to reflect Bazel's outputs.
Liz Kammera4655a92023-02-10 17:17:28 -0500218 buildStatements []*bazel.BuildStatement
Chris Parsons1a7aca02022-04-25 22:35:15 -0400219
220 // Depsets which should be used for Bazel's build statements.
221 depsets []bazel.AqueryDepset
Chris Parsonsad876012022-08-20 14:48:32 -0400222
223 // Per-module allowlist/denylist functionality to control whether analysis of
224 // modules are handled by Bazel. For modules which do not have a Bazel definition
225 // (or do not sufficiently support bazel handling via MixedBuildBuildable),
226 // this allowlist will have no effect, even if the module is explicitly allowlisted here.
227 // Per-module denylist to opt modules out of bazel handling.
228 bazelDisabledModules map[string]bool
229 // Per-module allowlist to opt modules in to bazel handling.
230 bazelEnabledModules map[string]bool
Yu Liue4312402023-01-18 09:15:31 -0800231 // DCLA modules are enabled when used in apex.
232 bazelDclaEnabledModules map[string]bool
Cole Faustb85d1a12022-11-08 18:14:01 -0800233
234 targetProduct string
235 targetBuildVariant string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400236}
237
Sasha Smundak39a301c2022-12-29 17:11:49 -0800238var _ BazelContext = &mixedBuildBazelContext{}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400239
240// A bazel context to use when Bazel is disabled.
241type noopBazelContext struct{}
242
243var _ BazelContext = noopBazelContext{}
244
245// A bazel context to use for tests.
246type MockBazelContext struct {
Liz Kammera92e8442021-04-07 20:25:21 -0400247 OutputBaseDir string
248
Spandan Dasbd156812023-06-05 22:43:13 +0000249 LabelToOutputFiles map[string][]string
250 LabelToCcInfo map[string]cquery.CcInfo
251 LabelToPythonBinary map[string]string
252 LabelToApexInfo map[string]cquery.ApexInfo
253 LabelToCcBinary map[string]cquery.CcUnstrippedInfo
254 LabelToPrebuiltFileInfo map[string]cquery.PrebuiltFileInfo
Yu Liue4312402023-01-18 09:15:31 -0800255
256 BazelRequests map[string]bool
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400257}
258
Yu Liue4312402023-01-18 09:15:31 -0800259func (m MockBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) {
260 key := BuildMockBazelContextRequestKey(label, requestType, cfgKey.arch, cfgKey.osType, cfgKey.apexKey)
261 if m.BazelRequests == nil {
262 m.BazelRequests = make(map[string]bool)
263 }
264 m.BazelRequests[key] = true
Chris Parsons8d6e4332021-02-22 16:13:50 -0500265}
266
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700267func (m MockBazelContext) GetOutputFiles(label string, _ configKey) ([]string, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500268 result, ok := m.LabelToOutputFiles[label]
269 if !ok {
270 return []string{}, fmt.Errorf("no target with label %q in LabelToOutputFiles", label)
271 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400272 return result, nil
Liz Kammer3f9e1552021-04-02 18:47:09 -0400273}
274
Yu Liue4312402023-01-18 09:15:31 -0800275func (m MockBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500276 result, ok := m.LabelToCcInfo[label]
277 if !ok {
Yu Liue4312402023-01-18 09:15:31 -0800278 key := BuildMockBazelContextResultKey(label, cfgKey.arch, cfgKey.osType, cfgKey.apexKey)
279 result, ok = m.LabelToCcInfo[key]
280 if !ok {
281 return cquery.CcInfo{}, fmt.Errorf("no target with label %q in LabelToCcInfo", label)
282 }
Sam Delmericoce39f832023-01-23 14:04:24 -0500283 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400284 return result, nil
285}
286
Liz Kammerbe6a7122022-11-04 16:05:11 -0400287func (m MockBazelContext) GetApexInfo(label string, _ configKey) (cquery.ApexInfo, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500288 result, ok := m.LabelToApexInfo[label]
289 if !ok {
290 return cquery.ApexInfo{}, fmt.Errorf("no target with label %q in LabelToApexInfo", label)
291 }
Liz Kammer0e255ef2022-11-04 16:07:04 -0400292 return result, nil
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700293}
294
Sasha Smundakedd16662022-10-07 14:44:50 -0700295func (m MockBazelContext) GetCcUnstrippedInfo(label string, _ configKey) (cquery.CcUnstrippedInfo, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500296 result, ok := m.LabelToCcBinary[label]
297 if !ok {
298 return cquery.CcUnstrippedInfo{}, fmt.Errorf("no target with label %q in LabelToCcBinary", label)
299 }
Sasha Smundakedd16662022-10-07 14:44:50 -0700300 return result, nil
301}
302
Spandan Dasbd156812023-06-05 22:43:13 +0000303func (m MockBazelContext) GetPrebuiltFileInfo(label string, _ configKey) (cquery.PrebuiltFileInfo, error) {
304 result, ok := m.LabelToPrebuiltFileInfo[label]
305 if !ok {
306 return cquery.PrebuiltFileInfo{}, fmt.Errorf("no target with label %q in LabelToPrebuiltFileInfo", label)
307 }
308 return result, nil
309}
310
Liz Kammer690fbac2023-02-10 11:11:17 -0500311func (m MockBazelContext) InvokeBazel(_ Config, _ invokeBazelContext) error {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400312 panic("unimplemented")
313}
314
Yu Liue4312402023-01-18 09:15:31 -0800315func (m MockBazelContext) IsModuleNameAllowed(_ string, _ bool) bool {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400316 return true
317}
318
Liz Kammera92e8442021-04-07 20:25:21 -0400319func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500320
Liz Kammera4655a92023-02-10 17:17:28 -0500321func (m MockBazelContext) BuildStatementsToRegister() []*bazel.BuildStatement {
322 return []*bazel.BuildStatement{}
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500323}
324
Chris Parsons1a7aca02022-04-25 22:35:15 -0400325func (m MockBazelContext) AqueryDepsets() []bazel.AqueryDepset {
326 return []bazel.AqueryDepset{}
327}
328
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400329var _ BazelContext = MockBazelContext{}
330
Yu Liue4312402023-01-18 09:15:31 -0800331func BuildMockBazelContextRequestKey(label string, request cqueryRequest, arch string, osType OsType, apexKey ApexConfigKey) string {
332 cfgKey := configKey{
333 arch: arch,
334 osType: osType,
335 apexKey: apexKey,
336 }
337
338 return strings.Join([]string{label, request.Name(), cfgKey.String()}, "_")
339}
340
341func BuildMockBazelContextResultKey(label string, arch string, osType OsType, apexKey ApexConfigKey) string {
342 cfgKey := configKey{
343 arch: arch,
344 osType: osType,
345 apexKey: apexKey,
346 }
347
348 return strings.Join([]string{label, cfgKey.String()}, "_")
349}
350
Sasha Smundak39a301c2022-12-29 17:11:49 -0800351func (bazelCtx *mixedBuildBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400352 key := makeCqueryKey(label, requestType, cfgKey)
Chris Parsonsf874e462022-05-10 13:50:12 -0400353 bazelCtx.requestMutex.Lock()
354 defer bazelCtx.requestMutex.Unlock()
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500355
356 // Insert key into requests, maintaining the sort, and only if it's not duplicate.
357 keyString := key.String()
358 foundEqual := false
359 notLessThanKeyString := func(i int) bool {
360 s := bazelCtx.requests[i].String()
361 v := strings.Compare(s, keyString)
362 if v == 0 {
363 foundEqual = true
364 }
365 return v >= 0
366 }
367 targetIndex := sort.Search(len(bazelCtx.requests), notLessThanKeyString)
368 if foundEqual {
369 return
370 }
371
372 if targetIndex == len(bazelCtx.requests) {
373 bazelCtx.requests = append(bazelCtx.requests, key)
374 } else {
375 bazelCtx.requests = append(bazelCtx.requests[:targetIndex+1], bazelCtx.requests[targetIndex:]...)
376 bazelCtx.requests[targetIndex] = key
377 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400378}
379
Sasha Smundak39a301c2022-12-29 17:11:49 -0800380func (bazelCtx *mixedBuildBazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400381 key := makeCqueryKey(label, cquery.GetOutputFiles, cfgKey)
Chris Parsonsf874e462022-05-10 13:50:12 -0400382 if rawString, ok := bazelCtx.results[key]; ok {
Chris Parsons944e7d02021-03-11 11:08:46 -0500383 bazelOutput := strings.TrimSpace(rawString)
Chris Parsons86dc2c22022-09-28 14:58:41 -0400384
Chris Parsonsf874e462022-05-10 13:50:12 -0400385 return cquery.GetOutputFiles.ParseResult(bazelOutput), nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400386 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400387 return nil, fmt.Errorf("no bazel response found for %v", key)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400388}
389
Sasha Smundak39a301c2022-12-29 17:11:49 -0800390func (bazelCtx *mixedBuildBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400391 key := makeCqueryKey(label, cquery.GetCcInfo, cfgKey)
Chris Parsonsf874e462022-05-10 13:50:12 -0400392 if rawString, ok := bazelCtx.results[key]; ok {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000393 bazelOutput := strings.TrimSpace(rawString)
Chris Parsonsf874e462022-05-10 13:50:12 -0400394 return cquery.GetCcInfo.ParseResult(bazelOutput)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000395 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400396 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 +0000397}
398
Sasha Smundak39a301c2022-12-29 17:11:49 -0800399func (bazelCtx *mixedBuildBazelContext) GetApexInfo(label string, cfgKey configKey) (cquery.ApexInfo, error) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400400 key := makeCqueryKey(label, cquery.GetApexInfo, cfgKey)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700401 if rawString, ok := bazelCtx.results[key]; ok {
Liz Kammer1b7ed9b2022-11-09 10:05:05 -0500402 return cquery.GetApexInfo.ParseResult(strings.TrimSpace(rawString))
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700403 }
Liz Kammerbe6a7122022-11-04 16:05:11 -0400404 return cquery.ApexInfo{}, fmt.Errorf("no bazel response found for %v", key)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700405}
406
Sasha Smundak39a301c2022-12-29 17:11:49 -0800407func (bazelCtx *mixedBuildBazelContext) GetCcUnstrippedInfo(label string, cfgKey configKey) (cquery.CcUnstrippedInfo, error) {
Sasha Smundakedd16662022-10-07 14:44:50 -0700408 key := makeCqueryKey(label, cquery.GetCcUnstrippedInfo, cfgKey)
409 if rawString, ok := bazelCtx.results[key]; ok {
Liz Kammer1b7ed9b2022-11-09 10:05:05 -0500410 return cquery.GetCcUnstrippedInfo.ParseResult(strings.TrimSpace(rawString))
Sasha Smundakedd16662022-10-07 14:44:50 -0700411 }
412 return cquery.CcUnstrippedInfo{}, fmt.Errorf("no bazel response for %s", key)
413}
414
Spandan Dasbd156812023-06-05 22:43:13 +0000415func (bazelCtx *mixedBuildBazelContext) GetPrebuiltFileInfo(label string, cfgKey configKey) (cquery.PrebuiltFileInfo, error) {
416 key := makeCqueryKey(label, cquery.GetPrebuiltFileInfo, cfgKey)
417 if rawString, ok := bazelCtx.results[key]; ok {
418 return cquery.GetPrebuiltFileInfo.ParseResult(strings.TrimSpace(rawString))
419 }
420 return cquery.PrebuiltFileInfo{}, fmt.Errorf("no bazel response for %s", key)
421}
422
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700423func (n noopBazelContext) QueueBazelRequest(_ string, _ cqueryRequest, _ configKey) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500424 panic("unimplemented")
425}
426
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700427func (n noopBazelContext) GetOutputFiles(_ string, _ configKey) ([]string, error) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500428 panic("unimplemented")
429}
430
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700431func (n noopBazelContext) GetCcInfo(_ string, _ configKey) (cquery.CcInfo, error) {
Chris Parsonsf874e462022-05-10 13:50:12 -0400432 panic("unimplemented")
433}
434
Liz Kammerbe6a7122022-11-04 16:05:11 -0400435func (n noopBazelContext) GetApexInfo(_ string, _ configKey) (cquery.ApexInfo, error) {
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700436 panic("unimplemented")
437}
438
Sasha Smundakedd16662022-10-07 14:44:50 -0700439func (n noopBazelContext) GetCcUnstrippedInfo(_ string, _ configKey) (cquery.CcUnstrippedInfo, error) {
440 //TODO implement me
441 panic("implement me")
442}
443
Spandan Dasbd156812023-06-05 22:43:13 +0000444func (n noopBazelContext) GetPrebuiltFileInfo(_ string, _ configKey) (cquery.PrebuiltFileInfo, error) {
445 panic("implement me")
446}
447
Liz Kammer690fbac2023-02-10 11:11:17 -0500448func (n noopBazelContext) InvokeBazel(_ Config, _ invokeBazelContext) error {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400449 panic("unimplemented")
450}
451
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500452func (m noopBazelContext) OutputBase() string {
453 return ""
454}
455
Yu Liue4312402023-01-18 09:15:31 -0800456func (n noopBazelContext) IsModuleNameAllowed(_ string, _ bool) bool {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400457 return false
458}
459
Liz Kammera4655a92023-02-10 17:17:28 -0500460func (m noopBazelContext) BuildStatementsToRegister() []*bazel.BuildStatement {
461 return []*bazel.BuildStatement{}
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500462}
463
Chris Parsons1a7aca02022-04-25 22:35:15 -0400464func (m noopBazelContext) AqueryDepsets() []bazel.AqueryDepset {
465 return []bazel.AqueryDepset{}
466}
467
Yu Liu6a7940c2023-05-09 17:12:22 -0700468func AddToStringSet(set map[string]bool, items []string) {
Yu Liue4312402023-01-18 09:15:31 -0800469 for _, item := range items {
470 set[item] = true
471 }
472}
473
Cole Faust705968d2022-12-14 11:32:05 -0800474func GetBazelEnabledAndDisabledModules(buildMode SoongBuildMode, forceEnabled map[string]struct{}) (map[string]bool, map[string]bool) {
Chris Parsonsef615e52022-08-18 22:04:11 -0400475 disabledModules := map[string]bool{}
476 enabledModules := map[string]bool{}
477
Cole Faust705968d2022-12-14 11:32:05 -0800478 switch buildMode {
Chris Parsonsef615e52022-08-18 22:04:11 -0400479 case BazelProdMode:
Yu Liu6a7940c2023-05-09 17:12:22 -0700480 AddToStringSet(enabledModules, allowlists.ProdMixedBuildsEnabledList)
Cole Faust705968d2022-12-14 11:32:05 -0800481 for enabledAdHocModule := range forceEnabled {
MarkDacekd06db5d2022-11-29 00:47:59 +0000482 enabledModules[enabledAdHocModule] = true
483 }
MarkDacekb78465d2022-10-18 20:10:16 +0000484 case BazelStagingMode:
Chris Parsons66fc7452022-11-04 13:26:17 -0400485 // Staging mode includes all prod modules plus all staging modules.
Yu Liu6a7940c2023-05-09 17:12:22 -0700486 AddToStringSet(enabledModules, allowlists.ProdMixedBuildsEnabledList)
487 AddToStringSet(enabledModules, allowlists.StagingMixedBuildsEnabledList)
Cole Faust705968d2022-12-14 11:32:05 -0800488 for enabledAdHocModule := range forceEnabled {
MarkDacekd06db5d2022-11-29 00:47:59 +0000489 enabledModules[enabledAdHocModule] = true
490 }
Chris Parsonsef615e52022-08-18 22:04:11 -0400491 default:
Chris Parsons21f80272023-06-15 04:02:28 +0000492 panic("Expected BazelProdMode or BazelStagingMode")
Cole Faust705968d2022-12-14 11:32:05 -0800493 }
494 return enabledModules, disabledModules
495}
496
497func GetBazelEnabledModules(buildMode SoongBuildMode) []string {
498 enabledModules, disabledModules := GetBazelEnabledAndDisabledModules(buildMode, nil)
499 enabledList := make([]string, 0, len(enabledModules))
500 for module := range enabledModules {
501 if !disabledModules[module] {
502 enabledList = append(enabledList, module)
503 }
504 }
505 sort.Strings(enabledList)
506 return enabledList
507}
508
509func NewBazelContext(c *config) (BazelContext, error) {
Chris Parsons21f80272023-06-15 04:02:28 +0000510 if c.BuildMode != BazelProdMode && c.BuildMode != BazelStagingMode {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400511 return noopBazelContext{}, nil
512 }
513
Cole Faust705968d2022-12-14 11:32:05 -0800514 enabledModules, disabledModules := GetBazelEnabledAndDisabledModules(c.BuildMode, c.BazelModulesForceEnabledByFlag())
515
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800516 paths := bazelPaths{
517 soongOutDir: c.soongOutDir,
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400518 }
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800519 var missing []string
520 vars := []struct {
521 name string
522 ptr *string
Paul Duffin184366a2022-12-21 15:55:33 +0000523
524 // True if the environment variable needs to be tracked so that changes to the variable
525 // cause the ninja file to be regenerated, false otherwise. False should only be set for
526 // environment variables that have no effect on the generated ninja file.
527 track bool
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800528 }{
Paul Duffin184366a2022-12-21 15:55:33 +0000529 {"BAZEL_HOME", &paths.homeDir, true},
530 {"BAZEL_PATH", &paths.bazelPath, true},
531 {"BAZEL_OUTPUT_BASE", &paths.outputBase, true},
532 {"BAZEL_WORKSPACE", &paths.workspaceDir, true},
533 {"BAZEL_METRICS_DIR", &paths.metricsDir, false},
534 {"BAZEL_DEPS_FILE", &paths.bazelDepsFile, true},
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800535 }
536 for _, v := range vars {
Paul Duffin184366a2022-12-21 15:55:33 +0000537 if v.track {
538 if s := c.Getenv(v.name); len(s) > 1 {
539 *v.ptr = s
540 continue
541 }
542 } else if s, ok := c.env[v.name]; ok {
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800543 *v.ptr = s
544 } else {
545 missing = append(missing, v.name)
546 }
547 }
548 if len(missing) > 0 {
549 return nil, fmt.Errorf("missing required env vars to use bazel: %s", missing)
550 }
Cole Faustb85d1a12022-11-08 18:14:01 -0800551
552 targetBuildVariant := "user"
553 if c.Eng() {
554 targetBuildVariant = "eng"
555 } else if c.Debuggable() {
556 targetBuildVariant = "userdebug"
557 }
558 targetProduct := "unknown"
559 if c.HasDeviceProduct() {
560 targetProduct = c.DeviceProduct()
561 }
Yu Liue4312402023-01-18 09:15:31 -0800562 dclaMixedBuildsEnabledList := []string{}
563 if c.BuildMode == BazelProdMode {
564 dclaMixedBuildsEnabledList = allowlists.ProdDclaMixedBuildsEnabledList
565 } else if c.BuildMode == BazelStagingMode {
566 dclaMixedBuildsEnabledList = append(allowlists.ProdDclaMixedBuildsEnabledList,
567 allowlists.StagingDclaMixedBuildsEnabledList...)
568 }
569 dclaEnabledModules := map[string]bool{}
Yu Liu6a7940c2023-05-09 17:12:22 -0700570 AddToStringSet(dclaEnabledModules, dclaMixedBuildsEnabledList)
Sasha Smundak39a301c2022-12-29 17:11:49 -0800571 return &mixedBuildBazelContext{
Chris Parsons9402ca82023-02-23 17:28:06 -0500572 bazelRunner: &builtinBazelRunner{c.UseBazelProxy, absolutePath(c.outDir)},
Yu Liue4312402023-01-18 09:15:31 -0800573 paths: &paths,
Yu Liue4312402023-01-18 09:15:31 -0800574 bazelEnabledModules: enabledModules,
575 bazelDisabledModules: disabledModules,
576 bazelDclaEnabledModules: dclaEnabledModules,
577 targetProduct: targetProduct,
578 targetBuildVariant: targetBuildVariant,
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400579 }, nil
580}
581
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400582func (p *bazelPaths) BazelMetricsDir() string {
583 return p.metricsDir
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000584}
585
Yu Liue4312402023-01-18 09:15:31 -0800586func (context *mixedBuildBazelContext) IsModuleNameAllowed(moduleName string, withinApex bool) bool {
Chris Parsonsad876012022-08-20 14:48:32 -0400587 if context.bazelDisabledModules[moduleName] {
588 return false
589 }
590 if context.bazelEnabledModules[moduleName] {
591 return true
592 }
Spandan Das95b24b12023-06-26 22:39:19 +0000593 if withinApex && context.bazelDclaEnabledModules[moduleName] {
Yu Liue4312402023-01-18 09:15:31 -0800594 return true
595 }
596
Chris Parsons21f80272023-06-15 04:02:28 +0000597 return false
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400598}
599
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400600func pwdPrefix() string {
601 // Darwin doesn't have /proc
602 if runtime.GOOS != "darwin" {
603 return "PWD=/proc/self/cwd"
604 }
605 return ""
606}
607
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400608type bazelCommand struct {
609 command string
610 // query or label
611 expression string
612}
613
Chris Parsons9402ca82023-02-23 17:28:06 -0500614type builtinBazelRunner struct {
615 useBazelProxy bool
616 outDir string
617}
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400618
Chris Parsons808d84c2021-03-09 20:43:32 -0500619// Issues the given bazel command with given build label and additional flags.
620// Returns (stdout, stderr, error). The first and second return values are strings
621// containing the stdout and stderr of the run command, and an error is returned if
622// the invocation returned an error code.
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000623func (r *builtinBazelRunner) issueBazelCommand(cmdRequest bazel.CmdRequest, paths *bazelPaths, eventHandler *metrics.EventHandler) (string, string, error) {
Chris Parsons9402ca82023-02-23 17:28:06 -0500624 if r.useBazelProxy {
625 eventHandler.Begin("client_proxy")
626 defer eventHandler.End("client_proxy")
627 proxyClient := bazel.NewProxyClient(r.outDir)
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000628 resp, err := proxyClient.IssueCommand(cmdRequest)
Chris Parsons9402ca82023-02-23 17:28:06 -0500629
630 if err != nil {
631 return "", "", err
632 }
633 if len(resp.ErrorString) > 0 {
634 return "", "", fmt.Errorf(resp.ErrorString)
635 }
636 return resp.Stdout, resp.Stderr, nil
Jason Wu52cd1942022-09-08 15:37:57 +0000637 } else {
Chris Parsons9402ca82023-02-23 17:28:06 -0500638 eventHandler.Begin("bazel command")
639 defer eventHandler.End("bazel command")
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000640
641 stdout, stderr, err := bazel.ExecBazel(paths.bazelPath, absolutePath(paths.syntheticWorkspaceDir()), cmdRequest)
642 return string(stdout), string(stderr), err
Jason Wu52cd1942022-09-08 15:37:57 +0000643 }
644}
645
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000646func (context *mixedBuildBazelContext) createBazelCommand(config Config, runName bazel.RunName, command bazelCommand,
647 extraFlags ...string) bazel.CmdRequest {
Cole Faust319abae2023-06-06 15:12:49 -0700648 if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
649 panic("Unknown GOOS: " + runtime.GOOS)
650 }
Romain Jobredeaux41fd5e42021-08-27 15:59:39 +0000651 cmdFlags := []string{
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000652 "--output_base=" + absolutePath(context.paths.outputBase),
Romain Jobredeaux41fd5e42021-08-27 15:59:39 +0000653 command.command,
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700654 command.expression,
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000655 "--profile=" + shared.BazelMetricsFilename(context.paths, runName),
Jingwen Chen91220d72021-03-24 02:18:33 -0400656
Cole Faust319abae2023-06-06 15:12:49 -0700657 "--host_platform=@soong_injection//product_config_platforms:mixed_builds_product-" + context.targetBuildVariant + "_" + runtime.GOOS + "_x86_64",
658 // Don't specify --platforms, because on some products/branches (like kernel-build-tools)
659 // the main platform for mixed_builds_product-variant doesn't exist because an arch isn't
660 // specified in product config. The derivative platforms that config_node transitions into
661 // will still work.
Jingwen Chen583ab212023-05-30 09:45:23 +0000662
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700663 // Suppress noise
664 "--ui_event_filters=-INFO",
Sam Delmerico658a4da2022-11-07 15:53:38 -0500665 "--noshow_progress",
666 "--norun_validations",
667 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400668 cmdFlags = append(cmdFlags, extraFlags...)
669
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700670 extraEnv := []string{
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000671 "HOME=" + context.paths.homeDir,
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200672 pwdPrefix(),
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000673 "BUILD_DIR=" + absolutePath(context.paths.soongOutDir),
Joe Onoratoba29f382022-10-24 06:38:11 -0700674 // Make OUT_DIR absolute here so build/bazel/bin/bazel uses the correct
Jingwen Chen8c523582021-06-01 11:19:53 +0000675 // OUT_DIR at <root>/out, instead of <root>/out/soong/workspace/out.
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000676 "OUT_DIR=" + absolutePath(context.paths.outDir()),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500677 // Disables local host detection of gcc; toolchain information is defined
678 // explicitly in BUILD files.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700679 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1",
680 }
Cole Faust8a161be2023-06-14 15:45:12 -0700681 capturedEnvVars, err := starlark_import.GetStarlarkValue[[]string]("captured_env_vars")
682 if err != nil {
683 panic(err)
684 }
685 for _, envvar := range capturedEnvVars {
Sam Delmericocb3c52c2023-02-03 17:40:08 -0500686 val := config.Getenv(envvar)
687 if val == "" {
688 continue
689 }
690 extraEnv = append(extraEnv, fmt.Sprintf("%s=%s", envvar, val))
691 }
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000692 envVars := append(os.Environ(), extraEnv...)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400693
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000694 return bazel.CmdRequest{cmdFlags, envVars}
Jason Wu52cd1942022-09-08 15:37:57 +0000695}
696
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000697func (context *mixedBuildBazelContext) printableCqueryCommand(bazelCmd bazel.CmdRequest) string {
698 args := append([]string{context.paths.bazelPath}, bazelCmd.Argv...)
699 outputString := strings.Join(bazelCmd.Env, " ") + " \"" + strings.Join(args, "\" \"") + "\""
Jason Wu52cd1942022-09-08 15:37:57 +0000700 return outputString
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400701}
702
Sasha Smundak39a301c2022-12-29 17:11:49 -0800703func (context *mixedBuildBazelContext) mainBzlFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500704 // TODO(cparsons): Define configuration transitions programmatically based
705 // on available archs.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400706 contents := `
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500707#####################################################
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400708# This file is generated by soong_build. Do not edit.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500709#####################################################
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400710def _config_node_transition_impl(settings, attr):
Cole Faustb85d1a12022-11-08 18:14:01 -0800711 if attr.os == "android" and attr.arch == "target":
Cole Faust319abae2023-06-06 15:12:49 -0700712 target = "mixed_builds_product-{VARIANT}"
Cole Faustb85d1a12022-11-08 18:14:01 -0800713 else:
Cole Faust319abae2023-06-06 15:12:49 -0700714 target = "mixed_builds_product-{VARIANT}_%s_%s" % (attr.os, attr.arch)
Yu Liue4312402023-01-18 09:15:31 -0800715 apex_name = ""
716 if attr.within_apex:
717 # //build/bazel/rules/apex:apex_name has to be set to a non_empty value,
718 # otherwise //build/bazel/rules/apex:non_apex will be true and the
719 # "-D__ANDROID_APEX__" compiler flag will be missing. Apex_name is used
720 # in some validation on bazel side which don't really apply in mixed
721 # build because soong will do the work, so we just set it to a fixed
722 # value here.
723 apex_name = "dcla_apex"
724 outputs = {
Jingwen Chen583ab212023-05-30 09:45:23 +0000725 "//command_line_option:platforms": "@soong_injection//product_config_platforms:%s" % target,
Yu Liue4312402023-01-18 09:15:31 -0800726 "@//build/bazel/rules/apex:within_apex": attr.within_apex,
727 "@//build/bazel/rules/apex:min_sdk_version": attr.apex_sdk_version,
728 "@//build/bazel/rules/apex:apex_name": apex_name,
Spandan Das40b79f82023-06-25 20:56:06 +0000729 "@//build/bazel/rules/apex:api_domain": attr.api_domain,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500730 }
731
Yu Liue4312402023-01-18 09:15:31 -0800732 return outputs
733
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400734_config_node_transition = transition(
735 implementation = _config_node_transition_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500736 inputs = [],
737 outputs = [
738 "//command_line_option:platforms",
Yu Liue4312402023-01-18 09:15:31 -0800739 "@//build/bazel/rules/apex:within_apex",
740 "@//build/bazel/rules/apex:min_sdk_version",
741 "@//build/bazel/rules/apex:apex_name",
Spandan Das40b79f82023-06-25 20:56:06 +0000742 "@//build/bazel/rules/apex:api_domain",
Chris Parsons8d6e4332021-02-22 16:13:50 -0500743 ],
744)
745
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400746def _passthrough_rule_impl(ctx):
747 return [DefaultInfo(files = depset(ctx.files.deps))]
748
749config_node = rule(
750 implementation = _passthrough_rule_impl,
751 attrs = {
Yu Liue4312402023-01-18 09:15:31 -0800752 "arch" : attr.string(mandatory = True),
753 "os" : attr.string(mandatory = True),
754 "within_apex" : attr.bool(default = False),
755 "apex_sdk_version" : attr.string(mandatory = True),
Spandan Das40b79f82023-06-25 20:56:06 +0000756 "api_domain" : attr.string(mandatory = True),
Yu Liue4312402023-01-18 09:15:31 -0800757 "deps" : attr.label_list(cfg = _config_node_transition, allow_files = True),
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400758 "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
759 },
Chris Parsons8d6e4332021-02-22 16:13:50 -0500760)
761
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400762
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500763# Rule representing the root of the build, to depend on all Bazel targets that
764# are required for the build. Building this target will build the entire Bazel
765# build tree.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400766mixed_build_root = rule(
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400767 implementation = _passthrough_rule_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500768 attrs = {
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400769 "deps" : attr.label_list(),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500770 },
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400771)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500772
773def _phony_root_impl(ctx):
774 return []
775
776# Rule to depend on other targets but build nothing.
777# This is useful as follows: building a target of this rule will generate
778# symlink forests for all dependencies of the target, without executing any
779# actions of the build.
780phony_root = rule(
781 implementation = _phony_root_impl,
782 attrs = {"deps" : attr.label_list()},
783)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400784`
Cole Faustb85d1a12022-11-08 18:14:01 -0800785
786 productReplacer := strings.NewReplacer(
787 "{PRODUCT}", context.targetProduct,
788 "{VARIANT}", context.targetBuildVariant)
789
790 return []byte(productReplacer.Replace(contents))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400791}
792
Sasha Smundak39a301c2022-12-29 17:11:49 -0800793func (context *mixedBuildBazelContext) mainBuildFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500794 // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
795 // architecture mapping.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400796 formatString := `
797# This file is generated by soong_build. Do not edit.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400798load(":main.bzl", "config_node", "mixed_build_root", "phony_root")
799
800%s
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400801
802mixed_build_root(name = "buildroot",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400803 deps = [%s],
Jingwen Chen3952a902022-12-12 12:20:58 +0000804 testonly = True, # Unblocks testonly deps.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400805)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500806
807phony_root(name = "phonyroot",
808 deps = [":buildroot"],
Jingwen Chen3952a902022-12-12 12:20:58 +0000809 testonly = True, # Unblocks testonly deps.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500810)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400811`
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400812 configNodeFormatString := `
813config_node(name = "%s",
814 arch = "%s",
Chris Parsons787fb362021-10-14 18:43:51 -0400815 os = "%s",
Yu Liue4312402023-01-18 09:15:31 -0800816 within_apex = %s,
817 apex_sdk_version = "%s",
Spandan Das40b79f82023-06-25 20:56:06 +0000818 api_domain = "%s",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400819 deps = [%s],
Jingwen Chen3952a902022-12-12 12:20:58 +0000820 testonly = True, # Unblocks testonly deps.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400821)
822`
823
824 configNodesSection := ""
825
Chris Parsons787fb362021-10-14 18:43:51 -0400826 labelsByConfig := map[string][]string{}
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500827
828 for _, val := range context.requests {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200829 labelString := fmt.Sprintf("\"@%s\"", val.label)
Chris Parsons787fb362021-10-14 18:43:51 -0400830 configString := getConfigString(val)
831 labelsByConfig[configString] = append(labelsByConfig[configString], labelString)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400832 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400833
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500834 // Configs need to be sorted to maintain determinism of the BUILD file.
835 sortedConfigs := make([]string, 0, len(labelsByConfig))
836 for val := range labelsByConfig {
837 sortedConfigs = append(sortedConfigs, val)
838 }
839 sort.Slice(sortedConfigs, func(i, j int) bool { return sortedConfigs[i] < sortedConfigs[j] })
840
Jingwen Chen1e347862021-09-02 12:11:49 +0000841 allLabels := []string{}
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500842 for _, configString := range sortedConfigs {
843 labels := labelsByConfig[configString]
Chris Parsons787fb362021-10-14 18:43:51 -0400844 configTokens := strings.Split(configString, "|")
Yu Liue4312402023-01-18 09:15:31 -0800845 if len(configTokens) < 2 {
Chris Parsons787fb362021-10-14 18:43:51 -0400846 panic(fmt.Errorf("Unexpected config string format: %s", configString))
Jingwen Chen1e347862021-09-02 12:11:49 +0000847 }
Chris Parsons787fb362021-10-14 18:43:51 -0400848 archString := configTokens[0]
849 osString := configTokens[1]
Yu Liue4312402023-01-18 09:15:31 -0800850 withinApex := "False"
851 apexSdkVerString := ""
Spandan Das40b79f82023-06-25 20:56:06 +0000852 apiDomainString := ""
853 if osString == "android" {
854 // api domains are meaningful only for device variants
855 apiDomainString = "system"
856 }
Chris Parsons787fb362021-10-14 18:43:51 -0400857 targetString := fmt.Sprintf("%s_%s", osString, archString)
Yu Liue4312402023-01-18 09:15:31 -0800858 if len(configTokens) > 2 {
859 targetString += "_" + configTokens[2]
860 if configTokens[2] == withinApexToString(true) {
861 withinApex = "True"
862 }
863 }
864 if len(configTokens) > 3 {
865 targetString += "_" + configTokens[3]
866 apexSdkVerString = configTokens[3]
867 }
Spandan Das40b79f82023-06-25 20:56:06 +0000868 if len(configTokens) > 4 {
869 apiDomainString = configTokens[4]
870 targetString += "_" + apiDomainString
871 }
Chris Parsons787fb362021-10-14 18:43:51 -0400872 allLabels = append(allLabels, fmt.Sprintf("\":%s\"", targetString))
873 labelsString := strings.Join(labels, ",\n ")
Spandan Das40b79f82023-06-25 20:56:06 +0000874 configNodesSection += fmt.Sprintf(configNodeFormatString, targetString, archString, osString, withinApex, apexSdkVerString, apiDomainString,
Yu Liue4312402023-01-18 09:15:31 -0800875 labelsString)
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400876 }
877
Jingwen Chen1e347862021-09-02 12:11:49 +0000878 return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(allLabels, ",\n ")))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400879}
880
Chris Parsons944e7d02021-03-11 11:08:46 -0500881func indent(original string) string {
882 result := ""
883 for _, line := range strings.Split(original, "\n") {
884 result += " " + line + "\n"
885 }
886 return result
887}
888
Chris Parsons808d84c2021-03-09 20:43:32 -0500889// Returns the file contents of the buildroot.cquery file that should be used for the cquery
890// expression in order to obtain information about buildroot and its dependencies.
Sasha Smundak39a301c2022-12-29 17:11:49 -0800891// The contents of this file depend on the mixedBuildBazelContext's requests; requests are enumerated
Chris Parsons808d84c2021-03-09 20:43:32 -0500892// and grouped by their request type. The data retrieved for each label depends on its
893// request type.
Sasha Smundak39a301c2022-12-29 17:11:49 -0800894func (context *mixedBuildBazelContext) cqueryStarlarkFileContents() []byte {
Liz Kammerf29df7c2021-04-02 13:37:39 -0400895 requestTypeToCqueryIdEntries := map[cqueryRequest][]string{}
Chris Parsons38851d82023-03-15 00:19:32 -0400896 requestTypes := []cqueryRequest{}
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500897 for _, val := range context.requests {
Chris Parsons944e7d02021-03-11 11:08:46 -0500898 cqueryId := getCqueryId(val)
899 mapEntryString := fmt.Sprintf("%q : True", cqueryId)
Chris Parsons38851d82023-03-15 00:19:32 -0400900 if _, seenKey := requestTypeToCqueryIdEntries[val.requestType]; !seenKey {
901 requestTypes = append(requestTypes, val.requestType)
902 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500903 requestTypeToCqueryIdEntries[val.requestType] =
904 append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString)
905 }
906 labelRegistrationMapSection := ""
907 functionDefSection := ""
908 mainSwitchSection := ""
909
910 mapDeclarationFormatString := `
911%s = {
912 %s
913}
914`
915 functionDefFormatString := `
Cole Faust97d15272022-11-22 14:08:59 -0800916def %s(target, id_string):
Chris Parsons944e7d02021-03-11 11:08:46 -0500917%s
918`
919 mainSwitchSectionFormatString := `
920 if id_string in %s:
Cole Faust97d15272022-11-22 14:08:59 -0800921 return id_string + ">>" + %s(target, id_string)
Chris Parsons944e7d02021-03-11 11:08:46 -0500922`
923
Chris Parsons38851d82023-03-15 00:19:32 -0400924 for _, requestType := range requestTypes {
Chris Parsons944e7d02021-03-11 11:08:46 -0500925 labelMapName := requestType.Name() + "_Labels"
926 functionName := requestType.Name() + "_Fn"
927 labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString,
928 labelMapName,
929 strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n "))
930 functionDefSection += fmt.Sprintf(functionDefFormatString,
931 functionName,
932 indent(requestType.StarlarkFunctionBody()))
933 mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString,
934 labelMapName, functionName)
935 }
936
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400937 formatString := `
938# This file is generated by soong_build. Do not edit.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400939
Cole Faustb85d1a12022-11-08 18:14:01 -0800940{LABEL_REGISTRATION_MAP_SECTION}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500941
Cole Faustb85d1a12022-11-08 18:14:01 -0800942{FUNCTION_DEF_SECTION}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500943
944def get_arch(target):
Chris Parsons787fb362021-10-14 18:43:51 -0400945 # TODO(b/199363072): filegroups and file targets aren't associated with any
946 # specific platform architecture in mixed builds. This is consistent with how
947 # Soong treats filegroups, but it may not be the case with manually-written
948 # filegroup BUILD targets.
Chris Parsons8d6e4332021-02-22 16:13:50 -0500949 buildoptions = build_options(target)
Yu Liue4312402023-01-18 09:15:31 -0800950
Jingwen Chen8f222742021-10-07 12:02:23 +0000951 if buildoptions == None:
952 # File targets do not have buildoptions. File targets aren't associated with
Chris Parsons787fb362021-10-14 18:43:51 -0400953 # any specific platform architecture in mixed builds, so use the host.
954 return "x86_64|linux"
Cole Faustb85d1a12022-11-08 18:14:01 -0800955 platforms = buildoptions["//command_line_option:platforms"]
Chris Parsons8d6e4332021-02-22 16:13:50 -0500956 if len(platforms) != 1:
957 # An individual configured target should have only one platform architecture.
958 # Note that it's fine for there to be multiple architectures for the same label,
959 # but each is its own configured target.
960 fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
Cole Faustb85d1a12022-11-08 18:14:01 -0800961 platform_name = platforms[0].name
Chris Parsons8d6e4332021-02-22 16:13:50 -0500962 if platform_name == "host":
963 return "HOST"
Cole Faust319abae2023-06-06 15:12:49 -0700964 if not platform_name.startswith("mixed_builds_product-{TARGET_BUILD_VARIANT}"):
965 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))
966 platform_name = platform_name.removeprefix("mixed_builds_product-{TARGET_BUILD_VARIANT}").removeprefix("_")
Yu Liue4312402023-01-18 09:15:31 -0800967 config_key = ""
Cole Faustb85d1a12022-11-08 18:14:01 -0800968 if not platform_name:
Yu Liue4312402023-01-18 09:15:31 -0800969 config_key = "target|android"
Chris Parsons94a0bba2021-06-04 15:03:47 -0400970 elif platform_name.startswith("android_"):
Yu Liue4312402023-01-18 09:15:31 -0800971 config_key = platform_name.removeprefix("android_") + "|android"
Chris Parsons94a0bba2021-06-04 15:03:47 -0400972 elif platform_name.startswith("linux_"):
Yu Liue4312402023-01-18 09:15:31 -0800973 config_key = platform_name.removeprefix("linux_") + "|linux"
Chris Parsons94a0bba2021-06-04 15:03:47 -0400974 else:
Cole Faust319abae2023-06-06 15:12:49 -0700975 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 -0500976
Yu Liue4312402023-01-18 09:15:31 -0800977 within_apex = buildoptions.get("//build/bazel/rules/apex:within_apex")
978 apex_sdk_version = buildoptions.get("//build/bazel/rules/apex:min_sdk_version")
Spandan Das40b79f82023-06-25 20:56:06 +0000979 api_domain = buildoptions.get("//build/bazel/rules/apex:api_domain")
Yu Liue4312402023-01-18 09:15:31 -0800980
981 if within_apex:
982 config_key += "|within_apex"
983 if apex_sdk_version != None and len(apex_sdk_version) > 0:
984 config_key += "|" + apex_sdk_version
Spandan Das40b79f82023-06-25 20:56:06 +0000985 if api_domain != None and len(api_domain) > 0:
986 config_key += "|" + api_domain
Yu Liue4312402023-01-18 09:15:31 -0800987
988 return config_key
989
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400990def format(target):
Chris Parsons8d6e4332021-02-22 16:13:50 -0500991 id_string = str(target.label) + "|" + get_arch(target)
Chris Parsons944e7d02021-03-11 11:08:46 -0500992
Chris Parsons86dc2c22022-09-28 14:58:41 -0400993 # TODO(b/248106697): Remove once Bazel is updated to always normalize labels.
994 if id_string.startswith("//"):
995 id_string = "@" + id_string
996
Cole Faustb85d1a12022-11-08 18:14:01 -0800997 {MAIN_SWITCH_SECTION}
998
Chris Parsons944e7d02021-03-11 11:08:46 -0500999 # This target was not requested via cquery, and thus must be a dependency
1000 # of a requested target.
1001 return id_string + ">>NONE"
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001002`
Cole Faustb85d1a12022-11-08 18:14:01 -08001003 replacer := strings.NewReplacer(
1004 "{TARGET_PRODUCT}", context.targetProduct,
1005 "{TARGET_BUILD_VARIANT}", context.targetBuildVariant,
1006 "{LABEL_REGISTRATION_MAP_SECTION}", labelRegistrationMapSection,
1007 "{FUNCTION_DEF_SECTION}", functionDefSection,
1008 "{MAIN_SWITCH_SECTION}", mainSwitchSection)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001009
Cole Faustb85d1a12022-11-08 18:14:01 -08001010 return []byte(replacer.Replace(formatString))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001011}
1012
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001013// Returns a path containing build-related metadata required for interfacing
1014// with Bazel. Example: out/soong/bazel.
Liz Kammer8d62a4f2021-04-08 09:47:28 -04001015func (p *bazelPaths) intermediatesDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001016 return filepath.Join(p.soongOutDir, "bazel")
Chris Parsons8ccdb632020-11-17 15:41:01 -05001017}
1018
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001019// Returns the path where the contents of the @soong_injection repository live.
1020// It is used by Soong to tell Bazel things it cannot over the command line.
1021func (p *bazelPaths) injectedFilesDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001022 return filepath.Join(p.soongOutDir, bazel.SoongInjectionDirName)
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001023}
1024
1025// Returns the path of the synthetic Bazel workspace that contains a symlink
1026// forest composed the whole source tree and BUILD files generated by bp2build.
1027func (p *bazelPaths) syntheticWorkspaceDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001028 return filepath.Join(p.soongOutDir, "workspace")
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001029}
1030
Jingwen Chen8c523582021-06-01 11:19:53 +00001031// Returns the path to the top level out dir ($OUT_DIR).
1032func (p *bazelPaths) outDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001033 return filepath.Dir(p.soongOutDir)
Jingwen Chen8c523582021-06-01 11:19:53 +00001034}
1035
Sasha Smundak4975c822022-11-16 15:28:18 -08001036const buildrootLabel = "@soong_injection//mixed_builds:buildroot"
1037
1038var (
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001039 cqueryCmd = bazelCommand{"cquery", fmt.Sprintf("deps(%s, 2)", buildrootLabel)}
1040 aqueryCmd = bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)}
1041 buildCmd = bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"}
1042 allBazelCommands = []bazelCommand{aqueryCmd, cqueryCmd, buildCmd}
Sasha Smundak4975c822022-11-16 15:28:18 -08001043)
1044
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001045// Issues commands to Bazel to receive results for all cquery requests
1046// queued in the BazelContext.
Liz Kammer690fbac2023-02-10 11:11:17 -05001047func (context *mixedBuildBazelContext) InvokeBazel(config Config, ctx invokeBazelContext) error {
1048 eventHandler := ctx.GetEventHandler()
1049 eventHandler.Begin("bazel")
1050 defer eventHandler.End("bazel")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001051
Sasha Smundak4975c822022-11-16 15:28:18 -08001052 if metricsDir := context.paths.BazelMetricsDir(); metricsDir != "" {
1053 if err := os.MkdirAll(metricsDir, 0777); err != nil {
1054 return err
1055 }
1056 }
1057 context.results = make(map[cqueryKey]string)
Sam Delmericocb3c52c2023-02-03 17:40:08 -05001058 if err := context.runCquery(config, ctx); err != nil {
Sasha Smundak4975c822022-11-16 15:28:18 -08001059 return err
1060 }
1061 if err := context.runAquery(config, ctx); err != nil {
1062 return err
1063 }
Sam Delmericocb3c52c2023-02-03 17:40:08 -05001064 if err := context.generateBazelSymlinks(config, ctx); err != nil {
Sasha Smundak4975c822022-11-16 15:28:18 -08001065 return err
1066 }
1067
1068 // Clear requests.
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001069 context.requests = []cqueryKey{}
Sasha Smundak4975c822022-11-16 15:28:18 -08001070 return nil
1071}
1072
Liz Kammer690fbac2023-02-10 11:11:17 -05001073func (context *mixedBuildBazelContext) runCquery(config Config, ctx invokeBazelContext) error {
1074 eventHandler := ctx.GetEventHandler()
1075 eventHandler.Begin("cquery")
1076 defer eventHandler.End("cquery")
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001077 soongInjectionPath := absolutePath(context.paths.injectedFilesDir())
Lukacs T. Berki3069dd92021-05-11 16:54:29 +02001078 mixedBuildsPath := filepath.Join(soongInjectionPath, "mixed_builds")
1079 if _, err := os.Stat(mixedBuildsPath); os.IsNotExist(err) {
1080 err = os.MkdirAll(mixedBuildsPath, 0777)
Usta Shrestha902fd172022-03-02 15:27:49 -05001081 if err != nil {
1082 return err
1083 }
1084 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001085 if err := writeFileBytesIfChanged(filepath.Join(soongInjectionPath, "WORKSPACE.bazel"), []byte{}, 0666); err != nil {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001086 return err
1087 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001088 if err := writeFileBytesIfChanged(filepath.Join(mixedBuildsPath, "main.bzl"), context.mainBzlFileContents(), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001089 return err
1090 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001091 if err := writeFileBytesIfChanged(filepath.Join(mixedBuildsPath, "BUILD.bazel"), context.mainBuildFileContents(), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001092 return err
1093 }
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001094 cqueryFileRelpath := filepath.Join(context.paths.injectedFilesDir(), "buildroot.cquery")
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001095 if err := writeFileBytesIfChanged(absolutePath(cqueryFileRelpath), context.cqueryStarlarkFileContents(), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001096 return err
1097 }
Jingwen Chen1e347862021-09-02 12:11:49 +00001098
Yu Liue4312402023-01-18 09:15:31 -08001099 extraFlags := []string{"--output=starlark", "--starlark:file=" + absolutePath(cqueryFileRelpath)}
1100 if Bool(config.productVariables.ClangCoverage) {
1101 extraFlags = append(extraFlags, "--collect_code_coverage")
1102 }
1103
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001104 cqueryCmdRequest := context.createBazelCommand(config, bazel.CqueryBuildRootRunName, cqueryCmd, extraFlags...)
1105 cqueryOutput, cqueryErrorMessage, cqueryErr := context.issueBazelCommand(cqueryCmdRequest, context.paths, eventHandler)
Wei Licbd181c2022-11-16 08:59:23 -08001106 if cqueryErr != nil {
1107 return cqueryErr
Chris Parsons8d6e4332021-02-22 16:13:50 -05001108 }
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001109 cqueryCommandPrint := fmt.Sprintf("cquery command line:\n %s \n\n\n", context.printableCqueryCommand(cqueryCmdRequest))
Sasha Smundak0e87b182022-12-01 11:46:11 -08001110 if err := os.WriteFile(filepath.Join(soongInjectionPath, "cquery.out"), []byte(cqueryCommandPrint+cqueryOutput), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001111 return err
1112 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001113 cqueryResults := map[string]string{}
1114 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
1115 if strings.Contains(outputLine, ">>") {
1116 splitLine := strings.SplitN(outputLine, ">>", 2)
1117 cqueryResults[splitLine[0]] = splitLine[1]
1118 }
1119 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001120 for _, val := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -05001121 if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
Usta Shrestha902fd172022-03-02 15:27:49 -05001122 context.results[val] = cqueryResult
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001123 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -05001124 return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]",
Wei Licbd181c2022-11-16 08:59:23 -08001125 getCqueryId(val), cqueryOutput, cqueryErrorMessage)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001126 }
1127 }
Sasha Smundak4975c822022-11-16 15:28:18 -08001128 return nil
1129}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001130
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001131func writeFileBytesIfChanged(path string, contents []byte, perm os.FileMode) error {
1132 oldContents, err := os.ReadFile(path)
1133 if err != nil || !bytes.Equal(contents, oldContents) {
1134 err = os.WriteFile(path, contents, perm)
1135 }
1136 return nil
1137}
1138
Liz Kammer690fbac2023-02-10 11:11:17 -05001139func (context *mixedBuildBazelContext) runAquery(config Config, ctx invokeBazelContext) error {
1140 eventHandler := ctx.GetEventHandler()
1141 eventHandler.Begin("aquery")
1142 defer eventHandler.End("aquery")
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001143 // Issue an aquery command to retrieve action information about the bazel build tree.
1144 //
Sasha Smundakb43ae1e2022-07-03 15:57:36 -07001145 // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
1146 // proto sources, which would add a number of unnecessary dependencies.
Jason Wu118fd2b2022-10-27 18:41:15 +00001147 extraFlags := []string{"--output=proto", "--include_file_write_contents"}
Yu Liu8d82ac52022-05-17 15:13:28 -07001148 if Bool(config.productVariables.ClangCoverage) {
Sasha Smundakb43ae1e2022-07-03 15:57:36 -07001149 extraFlags = append(extraFlags, "--collect_code_coverage")
1150 paths := make([]string, 0, 2)
1151 if p := config.productVariables.NativeCoveragePaths; len(p) > 0 {
Sasha Smundak0e87b182022-12-01 11:46:11 -08001152 for i := range p {
Wei Licbd181c2022-11-16 08:59:23 -08001153 // TODO(b/259404593) convert path wildcard to regex values
1154 if p[i] == "*" {
1155 p[i] = ".*"
1156 }
1157 }
Sasha Smundakb43ae1e2022-07-03 15:57:36 -07001158 paths = append(paths, JoinWithPrefixAndSeparator(p, "+", ","))
1159 }
1160 if p := config.productVariables.NativeCoverageExcludePaths; len(p) > 0 {
1161 paths = append(paths, JoinWithPrefixAndSeparator(p, "-", ","))
1162 }
1163 if len(paths) > 0 {
1164 extraFlags = append(extraFlags, "--instrumentation_filter="+strings.Join(paths, ","))
Yu Liu8d82ac52022-05-17 15:13:28 -07001165 }
1166 }
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001167 aqueryOutput, _, err := context.issueBazelCommand(context.createBazelCommand(config, bazel.AqueryBuildRootRunName, aqueryCmd,
1168 extraFlags...), context.paths, eventHandler)
Sasha Smundak4975c822022-11-16 15:28:18 -08001169 if err != nil {
Chris Parsons4f069892021-01-15 12:22:41 -05001170 return err
1171 }
Liz Kammer690fbac2023-02-10 11:11:17 -05001172 context.buildStatements, context.depsets, err = bazel.AqueryBuildStatements([]byte(aqueryOutput), eventHandler)
Sasha Smundak4975c822022-11-16 15:28:18 -08001173 return err
1174}
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001175
Liz Kammer690fbac2023-02-10 11:11:17 -05001176func (context *mixedBuildBazelContext) generateBazelSymlinks(config Config, ctx invokeBazelContext) error {
1177 eventHandler := ctx.GetEventHandler()
1178 eventHandler.Begin("symlinks")
1179 defer eventHandler.End("symlinks")
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001180 // Issue a build command of the phony root to generate symlink forests for dependencies of the
1181 // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
1182 // but some of symlinks may be required to resolve source dependencies of the build.
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001183 _, _, err := context.issueBazelCommand(context.createBazelCommand(config, bazel.BazelBuildPhonyRootRunName, buildCmd), context.paths, eventHandler)
Sasha Smundak4975c822022-11-16 15:28:18 -08001184 return err
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001185}
Chris Parsonsa798d962020-10-12 23:44:08 -04001186
Liz Kammera4655a92023-02-10 17:17:28 -05001187func (context *mixedBuildBazelContext) BuildStatementsToRegister() []*bazel.BuildStatement {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001188 return context.buildStatements
1189}
1190
Sasha Smundak39a301c2022-12-29 17:11:49 -08001191func (context *mixedBuildBazelContext) AqueryDepsets() []bazel.AqueryDepset {
Chris Parsons1a7aca02022-04-25 22:35:15 -04001192 return context.depsets
1193}
1194
Sasha Smundak39a301c2022-12-29 17:11:49 -08001195func (context *mixedBuildBazelContext) OutputBase() string {
Liz Kammer8d62a4f2021-04-08 09:47:28 -04001196 return context.paths.outputBase
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001197}
1198
Chris Parsonsa798d962020-10-12 23:44:08 -04001199// Singleton used for registering BUILD file ninja dependencies (needed
1200// for correctness of builds which use Bazel.
1201func BazelSingleton() Singleton {
1202 return &bazelSingleton{}
1203}
1204
1205type bazelSingleton struct{}
1206
1207func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001208 // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
Chris Parsonsad876012022-08-20 14:48:32 -04001209 if !ctx.Config().IsMixedBuildsEnabled() {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001210 return
1211 }
Chris Parsonsa798d962020-10-12 23:44:08 -04001212
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001213 // Add ninja file dependencies for files which all bazel invocations require.
1214 bazelBuildList := absolutePath(filepath.Join(
Lukacs T. Berkif9008072021-08-16 15:24:48 +02001215 filepath.Dir(ctx.Config().moduleListFile), "bazel.list"))
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001216 ctx.AddNinjaFileDeps(bazelBuildList)
1217
Sasha Smundak0e87b182022-12-01 11:46:11 -08001218 data, err := os.ReadFile(bazelBuildList)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001219 if err != nil {
1220 ctx.Errorf(err.Error())
1221 }
1222 files := strings.Split(strings.TrimSpace(string(data)), "\n")
1223 for _, file := range files {
1224 ctx.AddNinjaFileDeps(file)
1225 }
1226
Spandan Dasaf4ccaa2023-06-29 01:15:51 +00001227 depsetHashToDepset := map[string]bazel.AqueryDepset{}
1228
Chris Parsons1a7aca02022-04-25 22:35:15 -04001229 for _, depset := range ctx.Config().BazelContext.AqueryDepsets() {
Spandan Dasaf4ccaa2023-06-29 01:15:51 +00001230 depsetHashToDepset[depset.ContentHash] = depset
1231
Chris Parsons1a7aca02022-04-25 22:35:15 -04001232 var outputs []Path
ustafdb3e342022-11-22 17:11:30 -05001233 var orderOnlies []Path
Chris Parsons0bfb1c02022-05-12 16:43:01 -04001234 for _, depsetDepHash := range depset.TransitiveDepSetHashes {
1235 otherDepsetName := bazelDepsetName(depsetDepHash)
Chris Parsons1a7aca02022-04-25 22:35:15 -04001236 outputs = append(outputs, PathForPhony(ctx, otherDepsetName))
1237 }
1238 for _, artifactPath := range depset.DirectArtifacts {
ustafdb3e342022-11-22 17:11:30 -05001239 pathInBazelOut := PathForBazelOut(ctx, artifactPath)
1240 if artifactPath == "bazel-out/volatile-status.txt" {
1241 // See https://bazel.build/docs/user-manual#workspace-status
1242 orderOnlies = append(orderOnlies, pathInBazelOut)
1243 } else {
1244 outputs = append(outputs, pathInBazelOut)
1245 }
Chris Parsons1a7aca02022-04-25 22:35:15 -04001246 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -04001247 thisDepsetName := bazelDepsetName(depset.ContentHash)
Chris Parsons1a7aca02022-04-25 22:35:15 -04001248 ctx.Build(pctx, BuildParams{
1249 Rule: blueprint.Phony,
1250 Outputs: []WritablePath{PathForPhony(ctx, thisDepsetName)},
1251 Implicits: outputs,
ustafdb3e342022-11-22 17:11:30 -05001252 OrderOnly: orderOnlies,
Chris Parsons1a7aca02022-04-25 22:35:15 -04001253 })
1254 }
1255
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001256 executionRoot := path.Join(ctx.Config().BazelContext.OutputBase(), "execroot", "__main__")
1257 bazelOutDir := path.Join(executionRoot, "bazel-out")
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001258 for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
Liz Kammera4655a92023-02-10 17:17:28 -05001259 // nil build statements are a valid case where we do not create an action because it is
1260 // unnecessary or handled by other processing
1261 if buildStatement == nil {
1262 continue
1263 }
Sasha Smundak1da064c2022-06-08 16:36:16 -07001264 if len(buildStatement.Command) > 0 {
1265 rule := NewRuleBuilder(pctx, ctx)
Spandan Dasaf4ccaa2023-06-29 01:15:51 +00001266 intermediateDir, intermediateDirHash := intermediatePathForSboxMixedBuildAction(ctx, buildStatement)
1267 if buildStatement.ShouldRunInSbox {
1268 // Create a rule to build the output inside a sandbox
1269 // This will create two changes of working directory
1270 // 1. From ANDROID_BUILD_TOP to sbox top
1271 // 2. From sbox top to a a synthetic mixed build execution root relative to it
1272 // Finally, the outputs will be copied to intermediateDir
1273 rule.Sbox(intermediateDir,
1274 PathForOutput(ctx, "mixed_build_sbox_intermediates", intermediateDirHash+".textproto")).
1275 SandboxInputs().
1276 // Since we will cd to mixed build execution root, set sbox's out subdir to empty
1277 // Without this, we will try to copy from $SBOX_SANDBOX_DIR/out/out/bazel/output/execroot/__main__/...
1278 SetSboxOutDirDirAsEmpty()
1279
1280 // Create another set of rules to copy files from the intermediate dir to mixed build execution root
1281 for _, outputPath := range buildStatement.OutputPaths {
1282 ctx.Build(pctx, BuildParams{
1283 Rule: CpIfChanged,
1284 Input: intermediateDir.Join(ctx, executionRoot, outputPath),
1285 Output: PathForBazelOut(ctx, outputPath),
1286 })
1287 }
1288 }
1289 createCommand(rule.Command(), buildStatement, executionRoot, bazelOutDir, ctx, depsetHashToDepset)
Sasha Smundak1da064c2022-06-08 16:36:16 -07001290 desc := fmt.Sprintf("%s: %s", buildStatement.Mnemonic, buildStatement.OutputPaths)
1291 rule.Build(fmt.Sprintf("bazel %d", index), desc)
1292 continue
1293 }
1294 // Certain actions returned by aquery (for instance FileWrite) do not contain a command
1295 // and thus require special treatment. If BuildStatement were an interface implementing
1296 // buildRule(ctx) function, the code here would just call it.
1297 // Unfortunately, the BuildStatement is defined in
1298 // the 'bazel' package, which cannot depend on 'android' package where ctx is defined,
1299 // because this would cause circular dependency. So, until we move aquery processing
1300 // to the 'android' package, we need to handle special cases here.
Usta Shrestha13fd5ae2023-01-27 10:55:34 -05001301 switch buildStatement.Mnemonic {
Cole Faust950689a2023-06-21 15:07:21 -07001302 case "RepoMappingManifest":
1303 // It appears RepoMappingManifest files currently have
1304 // non-deterministic content. Just emit empty files for
1305 // now because they're unused.
1306 out := PathForBazelOut(ctx, buildStatement.OutputPaths[0])
1307 WriteFileRuleVerbatim(ctx, out, "")
Usta Shrestha13fd5ae2023-01-27 10:55:34 -05001308 case "FileWrite", "SourceSymlinkManifest":
Cole Fausta7347492022-12-16 10:56:24 -08001309 out := PathForBazelOut(ctx, buildStatement.OutputPaths[0])
1310 WriteFileRuleVerbatim(ctx, out, buildStatement.FileContents)
Usta Shrestha13fd5ae2023-01-27 10:55:34 -05001311 case "SymlinkTree":
Sasha Smundakc180dbd2022-07-03 14:55:58 -07001312 // build-runfiles arguments are the manifest file and the target directory
1313 // where it creates the symlink tree according to this manifest (and then
1314 // writes the MANIFEST file to it).
1315 outManifest := PathForBazelOut(ctx, buildStatement.OutputPaths[0])
1316 outManifestPath := outManifest.String()
1317 if !strings.HasSuffix(outManifestPath, "MANIFEST") {
1318 panic("the base name of the symlink tree action should be MANIFEST, got " + outManifestPath)
1319 }
1320 outDir := filepath.Dir(outManifestPath)
1321 ctx.Build(pctx, BuildParams{
1322 Rule: buildRunfilesRule,
1323 Output: outManifest,
1324 Inputs: []Path{PathForBazelOut(ctx, buildStatement.InputPaths[0])},
1325 Description: "symlink tree for " + outDir,
1326 Args: map[string]string{
1327 "outDir": outDir,
1328 },
1329 })
Usta Shrestha13fd5ae2023-01-27 10:55:34 -05001330 default:
Rupert Shuttlewortha29903f2021-04-06 16:17:33 +00001331 panic(fmt.Sprintf("unhandled build statement: %v", buildStatement))
Chris Parsons8d6e4332021-02-22 16:13:50 -05001332 }
Chris Parsonsa798d962020-10-12 23:44:08 -04001333 }
1334}
Chris Parsons8d6e4332021-02-22 16:13:50 -05001335
Spandan Dasaf4ccaa2023-06-29 01:15:51 +00001336// Returns a out dir path for a sandboxed mixed build action
1337func intermediatePathForSboxMixedBuildAction(ctx PathContext, statement *bazel.BuildStatement) (OutputPath, string) {
1338 // An artifact can be generated by a single buildstatement.
1339 // Use the hash of the first artifact to create a unique path
1340 uniqueDir := sha1.New()
1341 uniqueDir.Write([]byte(statement.OutputPaths[0]))
1342 uniqueDirHashString := hex.EncodeToString(uniqueDir.Sum(nil))
1343 return PathForOutput(ctx, "mixed_build_sbox_intermediates", uniqueDirHashString), uniqueDirHashString
1344}
1345
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001346// Register bazel-owned build statements (obtained from the aquery invocation).
Spandan Dasaf4ccaa2023-06-29 01:15:51 +00001347func createCommand(cmd *RuleBuilderCommand, buildStatement *bazel.BuildStatement, executionRoot string, bazelOutDir string, ctx BuilderContext, depsetHashToDepset map[string]bazel.AqueryDepset) {
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001348 // executionRoot is the action cwd.
Spandan Dasaf4ccaa2023-06-29 01:15:51 +00001349 if buildStatement.ShouldRunInSbox {
1350 // mkdir -p ensures that the directory exists when run via sbox
1351 cmd.Text(fmt.Sprintf("mkdir -p '%s' && cd '%s' &&", executionRoot, executionRoot))
1352 } else {
1353 cmd.Text(fmt.Sprintf("cd '%s' &&", executionRoot))
1354 }
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001355
1356 // Remove old outputs, as some actions might not rerun if the outputs are detected.
1357 if len(buildStatement.OutputPaths) > 0 {
Jingwen Chenf3b1ec32022-11-07 15:02:48 +00001358 cmd.Text("rm -rf") // -r because outputs can be Bazel dir/tree artifacts.
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001359 for _, outputPath := range buildStatement.OutputPaths {
Usta Shresthaef922252022-06-02 14:23:02 -04001360 cmd.Text(fmt.Sprintf("'%s'", outputPath))
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001361 }
1362 cmd.Text("&&")
1363 }
1364
1365 for _, pair := range buildStatement.Env {
1366 // Set per-action env variables, if any.
1367 cmd.Flag(pair.Key + "=" + pair.Value)
1368 }
1369
1370 // The actual Bazel action.
Colin Crossd5c7ddb2022-12-06 16:27:17 -08001371 if len(buildStatement.Command) > 16*1024 {
1372 commandFile := PathForBazelOut(ctx, buildStatement.OutputPaths[0]+".sh")
1373 WriteFileRule(ctx, commandFile, buildStatement.Command)
1374
1375 cmd.Text("bash").Text(buildStatement.OutputPaths[0] + ".sh").Implicit(commandFile)
1376 } else {
1377 cmd.Text(buildStatement.Command)
1378 }
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001379
1380 for _, outputPath := range buildStatement.OutputPaths {
Spandan Dasaf4ccaa2023-06-29 01:15:51 +00001381 if buildStatement.ShouldRunInSbox {
1382 // The full path has three components that get joined together
1383 // 1. intermediate output dir that `sbox` will place the artifacts at
1384 // 2. mixed build execution root
1385 // 3. artifact path returned by aquery
1386 intermediateDir, _ := intermediatePathForSboxMixedBuildAction(ctx, buildStatement)
1387 cmd.ImplicitOutput(intermediateDir.Join(ctx, executionRoot, outputPath))
1388 } else {
1389 cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
1390 }
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001391 }
1392 for _, inputPath := range buildStatement.InputPaths {
1393 cmd.Implicit(PathForBazelOut(ctx, inputPath))
1394 }
1395 for _, inputDepsetHash := range buildStatement.InputDepsetHashes {
Spandan Dasaf4ccaa2023-06-29 01:15:51 +00001396 if buildStatement.ShouldRunInSbox {
1397 // Bazel depsets are phony targets that are used to group files.
1398 // We need to copy the grouped files into the sandbox
1399 ds, _ := depsetHashToDepset[inputDepsetHash]
1400 cmd.Implicits(PathsForBazelOut(ctx, ds.DirectArtifacts))
1401 } else {
1402 otherDepsetName := bazelDepsetName(inputDepsetHash)
1403 cmd.Implicit(PathForPhony(ctx, otherDepsetName))
1404 }
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001405 }
1406
1407 if depfile := buildStatement.Depfile; depfile != nil {
1408 // The paths in depfile are relative to `executionRoot`.
1409 // Hence, they need to be corrected by replacing "bazel-out"
1410 // with the full `bazelOutDir`.
1411 // Otherwise, implicit outputs and implicit inputs under "bazel-out/"
1412 // would be deemed missing.
1413 // (Note: The regexp uses a capture group because the version of sed
1414 // does not support a look-behind pattern.)
1415 replacement := fmt.Sprintf(`&& sed -i'' -E 's@(^|\s|")bazel-out/@\1%s/@g' '%s'`,
1416 bazelOutDir, *depfile)
1417 cmd.Text(replacement)
1418 cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile))
1419 }
1420
1421 for _, symlinkPath := range buildStatement.SymlinkPaths {
1422 cmd.ImplicitSymlinkOutput(PathForBazelOut(ctx, symlinkPath))
1423 }
1424}
1425
Chris Parsons8d6e4332021-02-22 16:13:50 -05001426func getCqueryId(key cqueryKey) string {
Chris Parsons787fb362021-10-14 18:43:51 -04001427 return key.label + "|" + getConfigString(key)
Chris Parsons8d6e4332021-02-22 16:13:50 -05001428}
1429
Chris Parsons787fb362021-10-14 18:43:51 -04001430func getConfigString(key cqueryKey) string {
Liz Kammer0940b892022-03-18 15:55:04 -04001431 arch := key.configKey.arch
Chris Parsons787fb362021-10-14 18:43:51 -04001432 if len(arch) == 0 || arch == "common" {
Sasha Smundak9d46dcf2022-06-08 12:10:36 -07001433 if key.configKey.osType.Class == Device {
1434 // For the generic Android, the expected result is "target|android", which
1435 // corresponds to the product_variable_config named "android_target" in
1436 // build/bazel/platforms/BUILD.bazel.
1437 arch = "target"
1438 } else {
1439 // Use host platform, which is currently hardcoded to be x86_64.
1440 arch = "x86_64"
1441 }
Chris Parsons8d6e4332021-02-22 16:13:50 -05001442 }
Usta Shrestha16ac1352022-06-22 11:01:55 -04001443 osName := key.configKey.osType.Name
Colin Cross046fcea2022-12-20 15:32:18 -08001444 if len(osName) == 0 || osName == "common_os" || osName == "linux_glibc" || osName == "linux_musl" {
Chris Parsons787fb362021-10-14 18:43:51 -04001445 // Use host OS, which is currently hardcoded to be linux.
Usta Shrestha16ac1352022-06-22 11:01:55 -04001446 osName = "linux"
Chris Parsons787fb362021-10-14 18:43:51 -04001447 }
Yu Liue4312402023-01-18 09:15:31 -08001448 keyString := arch + "|" + osName
1449 if key.configKey.apexKey.WithinApex {
1450 keyString += "|" + withinApexToString(key.configKey.apexKey.WithinApex)
1451 }
1452
1453 if len(key.configKey.apexKey.ApexSdkVersion) > 0 {
1454 keyString += "|" + key.configKey.apexKey.ApexSdkVersion
1455 }
1456
Spandan Das40b79f82023-06-25 20:56:06 +00001457 if len(key.configKey.apexKey.ApiDomain) > 0 {
1458 keyString += "|" + key.configKey.apexKey.ApiDomain
1459 }
1460
Yu Liue4312402023-01-18 09:15:31 -08001461 return keyString
Chris Parsons787fb362021-10-14 18:43:51 -04001462}
1463
Chris Parsonsf874e462022-05-10 13:50:12 -04001464func GetConfigKey(ctx BaseModuleContext) configKey {
Liz Kammer0940b892022-03-18 15:55:04 -04001465 return configKey{
1466 // use string because Arch is not a valid key in go
1467 arch: ctx.Arch().String(),
1468 osType: ctx.Os(),
1469 }
Chris Parsons8d6e4332021-02-22 16:13:50 -05001470}
Chris Parsons1a7aca02022-04-25 22:35:15 -04001471
Yu Liue4312402023-01-18 09:15:31 -08001472func GetConfigKeyApexVariant(ctx BaseModuleContext, apexKey *ApexConfigKey) configKey {
1473 configKey := GetConfigKey(ctx)
1474
1475 if apexKey != nil {
1476 configKey.apexKey = ApexConfigKey{
1477 WithinApex: apexKey.WithinApex,
1478 ApexSdkVersion: apexKey.ApexSdkVersion,
Spandan Das40b79f82023-06-25 20:56:06 +00001479 ApiDomain: apexKey.ApiDomain,
Yu Liue4312402023-01-18 09:15:31 -08001480 }
1481 }
1482
1483 return configKey
1484}
1485
Chris Parsons0bfb1c02022-05-12 16:43:01 -04001486func bazelDepsetName(contentHash string) string {
1487 return fmt.Sprintf("bazel_depset_%s", contentHash)
Chris Parsons1a7aca02022-04-25 22:35:15 -04001488}