blob: 5291ecae94ecc454d0abdd301aa7a5f0cf61ff92 [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"
Sam Delmericocb3c52c2023-02-03 17:40:08 -050031 "android/soong/starlark_fmt"
Chris Parsons1a7aca02022-04-25 22:35:15 -040032 "github.com/google/blueprint"
Liz Kammer690fbac2023-02-10 11:11:17 -050033 "github.com/google/blueprint/metrics"
Liz Kammer8206d4f2021-03-03 16:40:52 -050034
Patrice Arruda05ab2d02020-12-12 06:24:26 +000035 "android/soong/bazel"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040036)
37
Sasha Smundak1da064c2022-06-08 16:36:16 -070038var (
Sasha Smundakc180dbd2022-07-03 14:55:58 -070039 _ = pctx.HostBinToolVariable("bazelBuildRunfilesTool", "build-runfiles")
40 buildRunfilesRule = pctx.AndroidStaticRule("bazelBuildRunfiles", blueprint.RuleParams{
41 Command: "${bazelBuildRunfilesTool} ${in} ${outDir}",
42 Depfile: "",
43 Description: "",
44 CommandDeps: []string{"${bazelBuildRunfilesTool}"},
45 }, "outDir")
Sam Delmericocb3c52c2023-02-03 17:40:08 -050046 allowedBazelEnvironmentVars = []string{
Sam Delmerico700b4d32023-02-10 16:46:28 -050047 // clang-tidy
Sam Delmericocb3c52c2023-02-03 17:40:08 -050048 "ALLOW_LOCAL_TIDY_TRUE",
49 "DEFAULT_TIDY_HEADER_DIRS",
50 "TIDY_TIMEOUT",
51 "WITH_TIDY",
52 "WITH_TIDY_FLAGS",
Sam Delmerico700b4d32023-02-10 16:46:28 -050053 "TIDY_EXTERNAL_VENDOR",
54
Sam Delmericocb3c52c2023-02-03 17:40:08 -050055 "SKIP_ABI_CHECKS",
56 "UNSAFE_DISABLE_APEX_ALLOWED_DEPS_CHECK",
57 "AUTO_ZERO_INITIALIZE",
58 "AUTO_PATTERN_INITIALIZE",
59 "AUTO_UNINITIALIZE",
60 "USE_CCACHE",
61 "LLVM_NEXT",
Sam Delmerico0ffa1f32023-04-19 14:18:20 -040062 "LLVM_PREBUILTS_VERSION",
63 "LLVM_RELEASE_VERSION",
Sam Delmericocb3c52c2023-02-03 17:40:08 -050064 "ALLOW_UNKNOWN_WARNING_OPTION",
65
Zi Wanga4f7dae2023-04-17 20:07:26 -070066 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT",
67
Sam Delmericocb3c52c2023-02-03 17:40:08 -050068 // Overrides the version in the apex_manifest.json. The version is unique for
69 // each branch (internal, aosp, mainline releases, dessert releases). This
70 // enables modules built on an older branch to be installed against a newer
71 // device for development purposes.
72 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION",
73 }
Sasha Smundak1da064c2022-06-08 16:36:16 -070074)
75
Chris Parsonsf874e462022-05-10 13:50:12 -040076func init() {
77 RegisterMixedBuildsMutator(InitRegistrationContext)
78}
79
80func RegisterMixedBuildsMutator(ctx RegistrationContext) {
Liz Kammer337e9032022-08-03 15:49:43 -040081 ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) {
Chris Parsonsf874e462022-05-10 13:50:12 -040082 ctx.BottomUp("mixed_builds_prep", mixedBuildsPrepareMutator).Parallel()
83 })
84}
85
86func mixedBuildsPrepareMutator(ctx BottomUpMutatorContext) {
87 if m := ctx.Module(); m.Enabled() {
88 if mixedBuildMod, ok := m.(MixedBuildBuildable); ok {
MarkDacekf47e1422023-04-19 16:47:36 +000089 mixedBuildEnabled := MixedBuildsEnabled(ctx)
90 queueMixedBuild := mixedBuildMod.IsMixedBuildSupported(ctx) && mixedBuildEnabled == MixedBuildEnabled
MarkDacek9c094ca2023-03-16 19:15:19 +000091 if queueMixedBuild {
Chris Parsonsf874e462022-05-10 13:50:12 -040092 mixedBuildMod.QueueBazelCall(ctx)
93 }
94 }
95 }
96}
97
Liz Kammerf29df7c2021-04-02 13:37:39 -040098type cqueryRequest interface {
99 // Name returns a string name for this request type. Such request type names must be unique,
100 // and must only consist of alphanumeric characters.
101 Name() string
102
103 // StarlarkFunctionBody returns a starlark function body to process this request type.
104 // The returned string is the body of a Starlark function which obtains
105 // all request-relevant information about a target and returns a string containing
106 // this information.
107 // The function should have the following properties:
Cole Faust97d15272022-11-22 14:08:59 -0800108 // - The arguments are `target` (a configured target) and `id_string` (the label + configuration).
Liz Kammerf29df7c2021-04-02 13:37:39 -0400109 // - The return value must be a string.
110 // - The function body should not be indented outside of its own scope.
111 StarlarkFunctionBody() string
112}
113
Chris Parsons787fb362021-10-14 18:43:51 -0400114// Portion of cquery map key to describe target configuration.
115type configKey struct {
Yu Liue4312402023-01-18 09:15:31 -0800116 arch string
117 osType OsType
118 apexKey ApexConfigKey
119}
120
121type ApexConfigKey struct {
122 WithinApex bool
123 ApexSdkVersion string
124}
125
126func (c ApexConfigKey) String() string {
127 return fmt.Sprintf("%s_%s", withinApexToString(c.WithinApex), c.ApexSdkVersion)
128}
129
130func withinApexToString(withinApex bool) string {
131 if withinApex {
132 return "within_apex"
133 }
134 return ""
Chris Parsons787fb362021-10-14 18:43:51 -0400135}
136
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700137func (c configKey) String() string {
Yu Liue4312402023-01-18 09:15:31 -0800138 return fmt.Sprintf("%s::%s::%s", c.arch, c.osType, c.apexKey)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700139}
140
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400141// Map key to describe bazel cquery requests.
142type cqueryKey struct {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400143 label string
Liz Kammerf29df7c2021-04-02 13:37:39 -0400144 requestType cqueryRequest
Chris Parsons787fb362021-10-14 18:43:51 -0400145 configKey configKey
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400146}
147
Chris Parsons86dc2c22022-09-28 14:58:41 -0400148func makeCqueryKey(label string, cqueryRequest cqueryRequest, cfgKey configKey) cqueryKey {
149 if strings.HasPrefix(label, "//") {
150 // Normalize Bazel labels to specify main repository explicitly.
151 label = "@" + label
152 }
153 return cqueryKey{label, cqueryRequest, cfgKey}
154}
155
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700156func (c cqueryKey) String() string {
157 return fmt.Sprintf("cquery(%s,%s,%s)", c.label, c.requestType.Name(), c.configKey)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700158}
159
Liz Kammer690fbac2023-02-10 11:11:17 -0500160type invokeBazelContext interface {
161 GetEventHandler() *metrics.EventHandler
162}
163
Chris Parsonsf874e462022-05-10 13:50:12 -0400164// BazelContext is a context object useful for interacting with Bazel during
165// the course of a build. Use of Bazel to evaluate part of the build graph
166// is referred to as a "mixed build". (Some modules are managed by Soong,
167// some are managed by Bazel). To facilitate interop between these build
168// subgraphs, Soong may make requests to Bazel and evaluate their responses
169// so that Soong modules may accurately depend on Bazel targets.
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400170type BazelContext interface {
Chris Parsonsf874e462022-05-10 13:50:12 -0400171 // Add a cquery request to the bazel request queue. All queued requests
172 // will be sent to Bazel on a subsequent invocation of InvokeBazel.
173 QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey)
174
175 // ** Cquery Results Retrieval Functions
176 // The below functions pertain to retrieving cquery results from a prior
177 // InvokeBazel function call and parsing the results.
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400178
179 // Returns result files built by building the given bazel target label.
Chris Parsonsf874e462022-05-10 13:50:12 -0400180 GetOutputFiles(label string, cfgKey configKey) ([]string, error)
Chris Parsons8d6e4332021-02-22 16:13:50 -0500181
Chris Parsons944e7d02021-03-11 11:08:46 -0500182 // Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order).
Chris Parsonsf874e462022-05-10 13:50:12 -0400183 GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error)
Liz Kammer3f9e1552021-04-02 18:47:09 -0400184
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000185 // Returns the executable binary resultant from building together the python sources
Chris Parsonsf874e462022-05-10 13:50:12 -0400186 // TODO(b/232976601): Remove.
187 GetPythonBinary(label string, cfgKey configKey) (string, error)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000188
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700189 // Returns the results of the GetApexInfo query (including output files)
Liz Kammerbe6a7122022-11-04 16:05:11 -0400190 GetApexInfo(label string, cfgkey configKey) (cquery.ApexInfo, error)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700191
Sasha Smundakedd16662022-10-07 14:44:50 -0700192 // Returns the results of the GetCcUnstrippedInfo query
193 GetCcUnstrippedInfo(label string, cfgkey configKey) (cquery.CcUnstrippedInfo, error)
194
Chris Parsonsf874e462022-05-10 13:50:12 -0400195 // ** end Cquery Results Retrieval Functions
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400196
197 // Issues commands to Bazel to receive results for all cquery requests
Sasha Smundak4975c822022-11-16 15:28:18 -0800198 // queued in the BazelContext. The ctx argument is optional and is only
199 // used for performance data collection
Liz Kammer690fbac2023-02-10 11:11:17 -0500200 InvokeBazel(config Config, ctx invokeBazelContext) error
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400201
Chris Parsonsad876012022-08-20 14:48:32 -0400202 // Returns true if Bazel handling is enabled for the module with the given name.
203 // Note that this only implies "bazel mixed build" allowlisting. The caller
204 // should independently verify the module is eligible for Bazel handling
205 // (for example, that it is MixedBuildBuildable).
Yu Liue4312402023-01-18 09:15:31 -0800206 IsModuleNameAllowed(moduleName string, withinApex bool) bool
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500207
Yu Liubfb23622023-02-22 10:42:15 -0800208 IsModuleDclaAllowed(moduleName string) bool
209
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500210 // Returns the bazel output base (the root directory for all bazel intermediate outputs).
211 OutputBase() string
212
213 // Returns build statements which should get registered to reflect Bazel's outputs.
Liz Kammera4655a92023-02-10 17:17:28 -0500214 BuildStatementsToRegister() []*bazel.BuildStatement
Chris Parsons1a7aca02022-04-25 22:35:15 -0400215
216 // Returns the depsets defined in Bazel's aquery response.
217 AqueryDepsets() []bazel.AqueryDepset
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400218}
219
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400220type bazelRunner interface {
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000221 issueBazelCommand(cmdRequest bazel.CmdRequest, paths *bazelPaths, eventHandler *metrics.EventHandler) (output string, errorMessage string, error error)
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400222}
223
224type bazelPaths struct {
MarkDacek0d5bca52022-10-10 20:07:48 +0000225 homeDir string
226 bazelPath string
227 outputBase string
228 workspaceDir string
229 soongOutDir string
230 metricsDir string
231 bazelDepsFile string
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400232}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400233
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400234// A context object which tracks queued requests that need to be made to Bazel,
235// and their results after the requests have been made.
Sasha Smundak39a301c2022-12-29 17:11:49 -0800236type mixedBuildBazelContext struct {
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400237 bazelRunner
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500238 paths *bazelPaths
239 // cquery requests that have not yet been issued to Bazel. This list is maintained
240 // in a sorted state, and is guaranteed to have no duplicates.
241 requests []cqueryKey
242 requestMutex sync.Mutex // requests can be written in parallel
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400243
244 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500245
246 // Build statements which should get registered to reflect Bazel's outputs.
Liz Kammera4655a92023-02-10 17:17:28 -0500247 buildStatements []*bazel.BuildStatement
Chris Parsons1a7aca02022-04-25 22:35:15 -0400248
249 // Depsets which should be used for Bazel's build statements.
250 depsets []bazel.AqueryDepset
Chris Parsonsad876012022-08-20 14:48:32 -0400251
252 // Per-module allowlist/denylist functionality to control whether analysis of
253 // modules are handled by Bazel. For modules which do not have a Bazel definition
254 // (or do not sufficiently support bazel handling via MixedBuildBuildable),
255 // this allowlist will have no effect, even if the module is explicitly allowlisted here.
256 // Per-module denylist to opt modules out of bazel handling.
257 bazelDisabledModules map[string]bool
258 // Per-module allowlist to opt modules in to bazel handling.
259 bazelEnabledModules map[string]bool
Yu Liue4312402023-01-18 09:15:31 -0800260 // DCLA modules are enabled when used in apex.
261 bazelDclaEnabledModules map[string]bool
Chris Parsonsad876012022-08-20 14:48:32 -0400262 // If true, modules are bazel-enabled by default, unless present in bazelDisabledModules.
263 modulesDefaultToBazel bool
Cole Faustb85d1a12022-11-08 18:14:01 -0800264
265 targetProduct string
266 targetBuildVariant string
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400267}
268
Sasha Smundak39a301c2022-12-29 17:11:49 -0800269var _ BazelContext = &mixedBuildBazelContext{}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400270
271// A bazel context to use when Bazel is disabled.
272type noopBazelContext struct{}
273
274var _ BazelContext = noopBazelContext{}
275
276// A bazel context to use for tests.
277type MockBazelContext struct {
Liz Kammera92e8442021-04-07 20:25:21 -0400278 OutputBaseDir string
279
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000280 LabelToOutputFiles map[string][]string
281 LabelToCcInfo map[string]cquery.CcInfo
282 LabelToPythonBinary map[string]string
Liz Kammerbe6a7122022-11-04 16:05:11 -0400283 LabelToApexInfo map[string]cquery.ApexInfo
Sasha Smundakedd16662022-10-07 14:44:50 -0700284 LabelToCcBinary map[string]cquery.CcUnstrippedInfo
Yu Liue4312402023-01-18 09:15:31 -0800285
286 BazelRequests map[string]bool
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400287}
288
Yu Liue4312402023-01-18 09:15:31 -0800289func (m MockBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) {
290 key := BuildMockBazelContextRequestKey(label, requestType, cfgKey.arch, cfgKey.osType, cfgKey.apexKey)
291 if m.BazelRequests == nil {
292 m.BazelRequests = make(map[string]bool)
293 }
294 m.BazelRequests[key] = true
Chris Parsons8d6e4332021-02-22 16:13:50 -0500295}
296
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700297func (m MockBazelContext) GetOutputFiles(label string, _ configKey) ([]string, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500298 result, ok := m.LabelToOutputFiles[label]
299 if !ok {
300 return []string{}, fmt.Errorf("no target with label %q in LabelToOutputFiles", label)
301 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400302 return result, nil
Liz Kammer3f9e1552021-04-02 18:47:09 -0400303}
304
Yu Liue4312402023-01-18 09:15:31 -0800305func (m MockBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500306 result, ok := m.LabelToCcInfo[label]
307 if !ok {
Yu Liue4312402023-01-18 09:15:31 -0800308 key := BuildMockBazelContextResultKey(label, cfgKey.arch, cfgKey.osType, cfgKey.apexKey)
309 result, ok = m.LabelToCcInfo[key]
310 if !ok {
311 return cquery.CcInfo{}, fmt.Errorf("no target with label %q in LabelToCcInfo", label)
312 }
Sam Delmericoce39f832023-01-23 14:04:24 -0500313 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400314 return result, nil
315}
316
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700317func (m MockBazelContext) GetPythonBinary(label string, _ configKey) (string, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500318 result, ok := m.LabelToPythonBinary[label]
319 if !ok {
320 return "", fmt.Errorf("no target with label %q in LabelToPythonBinary", label)
321 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400322 return result, nil
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000323}
324
Liz Kammerbe6a7122022-11-04 16:05:11 -0400325func (m MockBazelContext) GetApexInfo(label string, _ configKey) (cquery.ApexInfo, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500326 result, ok := m.LabelToApexInfo[label]
327 if !ok {
328 return cquery.ApexInfo{}, fmt.Errorf("no target with label %q in LabelToApexInfo", label)
329 }
Liz Kammer0e255ef2022-11-04 16:07:04 -0400330 return result, nil
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700331}
332
Sasha Smundakedd16662022-10-07 14:44:50 -0700333func (m MockBazelContext) GetCcUnstrippedInfo(label string, _ configKey) (cquery.CcUnstrippedInfo, error) {
Sam Delmericoce39f832023-01-23 14:04:24 -0500334 result, ok := m.LabelToCcBinary[label]
335 if !ok {
336 return cquery.CcUnstrippedInfo{}, fmt.Errorf("no target with label %q in LabelToCcBinary", label)
337 }
Sasha Smundakedd16662022-10-07 14:44:50 -0700338 return result, nil
339}
340
Liz Kammer690fbac2023-02-10 11:11:17 -0500341func (m MockBazelContext) InvokeBazel(_ Config, _ invokeBazelContext) error {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400342 panic("unimplemented")
343}
344
Yu Liue4312402023-01-18 09:15:31 -0800345func (m MockBazelContext) IsModuleNameAllowed(_ string, _ bool) bool {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400346 return true
347}
348
Yu Liubfb23622023-02-22 10:42:15 -0800349func (m MockBazelContext) IsModuleDclaAllowed(_ string) bool {
350 return true
351}
352
Liz Kammera92e8442021-04-07 20:25:21 -0400353func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir }
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500354
Liz Kammera4655a92023-02-10 17:17:28 -0500355func (m MockBazelContext) BuildStatementsToRegister() []*bazel.BuildStatement {
356 return []*bazel.BuildStatement{}
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500357}
358
Chris Parsons1a7aca02022-04-25 22:35:15 -0400359func (m MockBazelContext) AqueryDepsets() []bazel.AqueryDepset {
360 return []bazel.AqueryDepset{}
361}
362
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400363var _ BazelContext = MockBazelContext{}
364
Yu Liue4312402023-01-18 09:15:31 -0800365func BuildMockBazelContextRequestKey(label string, request cqueryRequest, arch string, osType OsType, apexKey ApexConfigKey) string {
366 cfgKey := configKey{
367 arch: arch,
368 osType: osType,
369 apexKey: apexKey,
370 }
371
372 return strings.Join([]string{label, request.Name(), cfgKey.String()}, "_")
373}
374
375func BuildMockBazelContextResultKey(label string, arch string, osType OsType, apexKey ApexConfigKey) string {
376 cfgKey := configKey{
377 arch: arch,
378 osType: osType,
379 apexKey: apexKey,
380 }
381
382 return strings.Join([]string{label, cfgKey.String()}, "_")
383}
384
Sasha Smundak39a301c2022-12-29 17:11:49 -0800385func (bazelCtx *mixedBuildBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400386 key := makeCqueryKey(label, requestType, cfgKey)
Chris Parsonsf874e462022-05-10 13:50:12 -0400387 bazelCtx.requestMutex.Lock()
388 defer bazelCtx.requestMutex.Unlock()
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500389
390 // Insert key into requests, maintaining the sort, and only if it's not duplicate.
391 keyString := key.String()
392 foundEqual := false
393 notLessThanKeyString := func(i int) bool {
394 s := bazelCtx.requests[i].String()
395 v := strings.Compare(s, keyString)
396 if v == 0 {
397 foundEqual = true
398 }
399 return v >= 0
400 }
401 targetIndex := sort.Search(len(bazelCtx.requests), notLessThanKeyString)
402 if foundEqual {
403 return
404 }
405
406 if targetIndex == len(bazelCtx.requests) {
407 bazelCtx.requests = append(bazelCtx.requests, key)
408 } else {
409 bazelCtx.requests = append(bazelCtx.requests[:targetIndex+1], bazelCtx.requests[targetIndex:]...)
410 bazelCtx.requests[targetIndex] = key
411 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400412}
413
Sasha Smundak39a301c2022-12-29 17:11:49 -0800414func (bazelCtx *mixedBuildBazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400415 key := makeCqueryKey(label, cquery.GetOutputFiles, cfgKey)
Chris Parsonsf874e462022-05-10 13:50:12 -0400416 if rawString, ok := bazelCtx.results[key]; ok {
Chris Parsons944e7d02021-03-11 11:08:46 -0500417 bazelOutput := strings.TrimSpace(rawString)
Chris Parsons86dc2c22022-09-28 14:58:41 -0400418
Chris Parsonsf874e462022-05-10 13:50:12 -0400419 return cquery.GetOutputFiles.ParseResult(bazelOutput), nil
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400420 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400421 return nil, fmt.Errorf("no bazel response found for %v", key)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400422}
423
Sasha Smundak39a301c2022-12-29 17:11:49 -0800424func (bazelCtx *mixedBuildBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400425 key := makeCqueryKey(label, cquery.GetCcInfo, cfgKey)
Chris Parsonsf874e462022-05-10 13:50:12 -0400426 if rawString, ok := bazelCtx.results[key]; ok {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000427 bazelOutput := strings.TrimSpace(rawString)
Chris Parsonsf874e462022-05-10 13:50:12 -0400428 return cquery.GetCcInfo.ParseResult(bazelOutput)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000429 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400430 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 +0000431}
432
Sasha Smundak39a301c2022-12-29 17:11:49 -0800433func (bazelCtx *mixedBuildBazelContext) GetPythonBinary(label string, cfgKey configKey) (string, error) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400434 key := makeCqueryKey(label, cquery.GetPythonBinary, cfgKey)
Chris Parsonsf874e462022-05-10 13:50:12 -0400435 if rawString, ok := bazelCtx.results[key]; ok {
436 bazelOutput := strings.TrimSpace(rawString)
437 return cquery.GetPythonBinary.ParseResult(bazelOutput), nil
438 }
439 return "", fmt.Errorf("no bazel response found for %v", key)
440}
441
Sasha Smundak39a301c2022-12-29 17:11:49 -0800442func (bazelCtx *mixedBuildBazelContext) GetApexInfo(label string, cfgKey configKey) (cquery.ApexInfo, error) {
Chris Parsons86dc2c22022-09-28 14:58:41 -0400443 key := makeCqueryKey(label, cquery.GetApexInfo, cfgKey)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700444 if rawString, ok := bazelCtx.results[key]; ok {
Liz Kammer1b7ed9b2022-11-09 10:05:05 -0500445 return cquery.GetApexInfo.ParseResult(strings.TrimSpace(rawString))
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700446 }
Liz Kammerbe6a7122022-11-04 16:05:11 -0400447 return cquery.ApexInfo{}, fmt.Errorf("no bazel response found for %v", key)
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700448}
449
Sasha Smundak39a301c2022-12-29 17:11:49 -0800450func (bazelCtx *mixedBuildBazelContext) GetCcUnstrippedInfo(label string, cfgKey configKey) (cquery.CcUnstrippedInfo, error) {
Sasha Smundakedd16662022-10-07 14:44:50 -0700451 key := makeCqueryKey(label, cquery.GetCcUnstrippedInfo, cfgKey)
452 if rawString, ok := bazelCtx.results[key]; ok {
Liz Kammer1b7ed9b2022-11-09 10:05:05 -0500453 return cquery.GetCcUnstrippedInfo.ParseResult(strings.TrimSpace(rawString))
Sasha Smundakedd16662022-10-07 14:44:50 -0700454 }
455 return cquery.CcUnstrippedInfo{}, fmt.Errorf("no bazel response for %s", key)
456}
457
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700458func (n noopBazelContext) QueueBazelRequest(_ string, _ cqueryRequest, _ configKey) {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500459 panic("unimplemented")
460}
461
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700462func (n noopBazelContext) GetOutputFiles(_ string, _ configKey) ([]string, error) {
Chris Parsons808d84c2021-03-09 20:43:32 -0500463 panic("unimplemented")
464}
465
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700466func (n noopBazelContext) GetCcInfo(_ string, _ configKey) (cquery.CcInfo, error) {
Chris Parsonsf874e462022-05-10 13:50:12 -0400467 panic("unimplemented")
468}
469
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700470func (n noopBazelContext) GetPythonBinary(_ string, _ configKey) (string, error) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa05a2552021-08-11 16:48:30 +0000471 panic("unimplemented")
472}
473
Liz Kammerbe6a7122022-11-04 16:05:11 -0400474func (n noopBazelContext) GetApexInfo(_ string, _ configKey) (cquery.ApexInfo, error) {
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700475 panic("unimplemented")
476}
477
Sasha Smundakedd16662022-10-07 14:44:50 -0700478func (n noopBazelContext) GetCcUnstrippedInfo(_ string, _ configKey) (cquery.CcUnstrippedInfo, error) {
479 //TODO implement me
480 panic("implement me")
481}
482
Liz Kammer690fbac2023-02-10 11:11:17 -0500483func (n noopBazelContext) InvokeBazel(_ Config, _ invokeBazelContext) error {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400484 panic("unimplemented")
485}
486
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500487func (m noopBazelContext) OutputBase() string {
488 return ""
489}
490
Yu Liue4312402023-01-18 09:15:31 -0800491func (n noopBazelContext) IsModuleNameAllowed(_ string, _ bool) bool {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400492 return false
493}
494
Yu Liubfb23622023-02-22 10:42:15 -0800495func (n noopBazelContext) IsModuleDclaAllowed(_ string) bool {
496 return false
497}
498
Liz Kammera4655a92023-02-10 17:17:28 -0500499func (m noopBazelContext) BuildStatementsToRegister() []*bazel.BuildStatement {
500 return []*bazel.BuildStatement{}
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500501}
502
Chris Parsons1a7aca02022-04-25 22:35:15 -0400503func (m noopBazelContext) AqueryDepsets() []bazel.AqueryDepset {
504 return []bazel.AqueryDepset{}
505}
506
Yu Liue4312402023-01-18 09:15:31 -0800507func addToStringSet(set map[string]bool, items []string) {
508 for _, item := range items {
509 set[item] = true
510 }
511}
512
Cole Faust705968d2022-12-14 11:32:05 -0800513func GetBazelEnabledAndDisabledModules(buildMode SoongBuildMode, forceEnabled map[string]struct{}) (map[string]bool, map[string]bool) {
Chris Parsonsef615e52022-08-18 22:04:11 -0400514 disabledModules := map[string]bool{}
515 enabledModules := map[string]bool{}
516
Cole Faust705968d2022-12-14 11:32:05 -0800517 switch buildMode {
Chris Parsonsef615e52022-08-18 22:04:11 -0400518 case BazelProdMode:
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800519 addToStringSet(enabledModules, allowlists.ProdMixedBuildsEnabledList)
Cole Faust705968d2022-12-14 11:32:05 -0800520 for enabledAdHocModule := range forceEnabled {
MarkDacekd06db5d2022-11-29 00:47:59 +0000521 enabledModules[enabledAdHocModule] = true
522 }
MarkDacekb78465d2022-10-18 20:10:16 +0000523 case BazelStagingMode:
Chris Parsons66fc7452022-11-04 13:26:17 -0400524 // Staging mode includes all prod modules plus all staging modules.
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800525 addToStringSet(enabledModules, allowlists.ProdMixedBuildsEnabledList)
526 addToStringSet(enabledModules, allowlists.StagingMixedBuildsEnabledList)
Cole Faust705968d2022-12-14 11:32:05 -0800527 for enabledAdHocModule := range forceEnabled {
MarkDacekd06db5d2022-11-29 00:47:59 +0000528 enabledModules[enabledAdHocModule] = true
529 }
Chris Parsonsef615e52022-08-18 22:04:11 -0400530 case BazelDevMode:
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800531 addToStringSet(disabledModules, allowlists.MixedBuildsDisabledList)
Chris Parsonsef615e52022-08-18 22:04:11 -0400532 default:
Cole Faust705968d2022-12-14 11:32:05 -0800533 panic("Expected BazelProdMode, BazelStagingMode, or BazelDevMode")
534 }
535 return enabledModules, disabledModules
536}
537
538func GetBazelEnabledModules(buildMode SoongBuildMode) []string {
539 enabledModules, disabledModules := GetBazelEnabledAndDisabledModules(buildMode, nil)
540 enabledList := make([]string, 0, len(enabledModules))
541 for module := range enabledModules {
542 if !disabledModules[module] {
543 enabledList = append(enabledList, module)
544 }
545 }
546 sort.Strings(enabledList)
547 return enabledList
548}
549
550func NewBazelContext(c *config) (BazelContext, error) {
551 if c.BuildMode != BazelProdMode && c.BuildMode != BazelStagingMode && c.BuildMode != BazelDevMode {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400552 return noopBazelContext{}, nil
553 }
554
Cole Faust705968d2022-12-14 11:32:05 -0800555 enabledModules, disabledModules := GetBazelEnabledAndDisabledModules(c.BuildMode, c.BazelModulesForceEnabledByFlag())
556
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800557 paths := bazelPaths{
558 soongOutDir: c.soongOutDir,
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400559 }
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800560 var missing []string
561 vars := []struct {
562 name string
563 ptr *string
Paul Duffin184366a2022-12-21 15:55:33 +0000564
565 // True if the environment variable needs to be tracked so that changes to the variable
566 // cause the ninja file to be regenerated, false otherwise. False should only be set for
567 // environment variables that have no effect on the generated ninja file.
568 track bool
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800569 }{
Paul Duffin184366a2022-12-21 15:55:33 +0000570 {"BAZEL_HOME", &paths.homeDir, true},
571 {"BAZEL_PATH", &paths.bazelPath, true},
572 {"BAZEL_OUTPUT_BASE", &paths.outputBase, true},
573 {"BAZEL_WORKSPACE", &paths.workspaceDir, true},
574 {"BAZEL_METRICS_DIR", &paths.metricsDir, false},
575 {"BAZEL_DEPS_FILE", &paths.bazelDepsFile, true},
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800576 }
577 for _, v := range vars {
Paul Duffin184366a2022-12-21 15:55:33 +0000578 if v.track {
579 if s := c.Getenv(v.name); len(s) > 1 {
580 *v.ptr = s
581 continue
582 }
583 } else if s, ok := c.env[v.name]; ok {
Sasha Smundakdc87f2d2022-12-06 20:27:54 -0800584 *v.ptr = s
585 } else {
586 missing = append(missing, v.name)
587 }
588 }
589 if len(missing) > 0 {
590 return nil, fmt.Errorf("missing required env vars to use bazel: %s", missing)
591 }
Cole Faustb85d1a12022-11-08 18:14:01 -0800592
593 targetBuildVariant := "user"
594 if c.Eng() {
595 targetBuildVariant = "eng"
596 } else if c.Debuggable() {
597 targetBuildVariant = "userdebug"
598 }
599 targetProduct := "unknown"
600 if c.HasDeviceProduct() {
601 targetProduct = c.DeviceProduct()
602 }
Yu Liue4312402023-01-18 09:15:31 -0800603 dclaMixedBuildsEnabledList := []string{}
604 if c.BuildMode == BazelProdMode {
605 dclaMixedBuildsEnabledList = allowlists.ProdDclaMixedBuildsEnabledList
606 } else if c.BuildMode == BazelStagingMode {
607 dclaMixedBuildsEnabledList = append(allowlists.ProdDclaMixedBuildsEnabledList,
608 allowlists.StagingDclaMixedBuildsEnabledList...)
609 }
610 dclaEnabledModules := map[string]bool{}
611 addToStringSet(dclaEnabledModules, dclaMixedBuildsEnabledList)
Sasha Smundak39a301c2022-12-29 17:11:49 -0800612 return &mixedBuildBazelContext{
Chris Parsons9402ca82023-02-23 17:28:06 -0500613 bazelRunner: &builtinBazelRunner{c.UseBazelProxy, absolutePath(c.outDir)},
Yu Liue4312402023-01-18 09:15:31 -0800614 paths: &paths,
615 modulesDefaultToBazel: c.BuildMode == BazelDevMode,
616 bazelEnabledModules: enabledModules,
617 bazelDisabledModules: disabledModules,
618 bazelDclaEnabledModules: dclaEnabledModules,
619 targetProduct: targetProduct,
620 targetBuildVariant: targetBuildVariant,
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400621 }, nil
622}
623
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400624func (p *bazelPaths) BazelMetricsDir() string {
625 return p.metricsDir
Patrice Arruda05ab2d02020-12-12 06:24:26 +0000626}
627
Yu Liue4312402023-01-18 09:15:31 -0800628func (context *mixedBuildBazelContext) IsModuleNameAllowed(moduleName string, withinApex bool) bool {
Chris Parsonsad876012022-08-20 14:48:32 -0400629 if context.bazelDisabledModules[moduleName] {
630 return false
631 }
632 if context.bazelEnabledModules[moduleName] {
633 return true
634 }
Yu Liubfb23622023-02-22 10:42:15 -0800635 if withinApex && context.IsModuleDclaAllowed(moduleName) {
Yu Liue4312402023-01-18 09:15:31 -0800636 return true
637 }
638
Chris Parsonsad876012022-08-20 14:48:32 -0400639 return context.modulesDefaultToBazel
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400640}
641
Yu Liubfb23622023-02-22 10:42:15 -0800642func (context *mixedBuildBazelContext) IsModuleDclaAllowed(moduleName string) bool {
643 return context.bazelDclaEnabledModules[moduleName]
644}
645
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400646func pwdPrefix() string {
647 // Darwin doesn't have /proc
648 if runtime.GOOS != "darwin" {
649 return "PWD=/proc/self/cwd"
650 }
651 return ""
652}
653
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400654type bazelCommand struct {
655 command string
656 // query or label
657 expression string
658}
659
Chris Parsons9402ca82023-02-23 17:28:06 -0500660type builtinBazelRunner struct {
661 useBazelProxy bool
662 outDir string
663}
Liz Kammer8d62a4f2021-04-08 09:47:28 -0400664
Chris Parsons808d84c2021-03-09 20:43:32 -0500665// Issues the given bazel command with given build label and additional flags.
666// Returns (stdout, stderr, error). The first and second return values are strings
667// containing the stdout and stderr of the run command, and an error is returned if
668// the invocation returned an error code.
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000669func (r *builtinBazelRunner) issueBazelCommand(cmdRequest bazel.CmdRequest, paths *bazelPaths, eventHandler *metrics.EventHandler) (string, string, error) {
Chris Parsons9402ca82023-02-23 17:28:06 -0500670 if r.useBazelProxy {
671 eventHandler.Begin("client_proxy")
672 defer eventHandler.End("client_proxy")
673 proxyClient := bazel.NewProxyClient(r.outDir)
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000674 resp, err := proxyClient.IssueCommand(cmdRequest)
Chris Parsons9402ca82023-02-23 17:28:06 -0500675
676 if err != nil {
677 return "", "", err
678 }
679 if len(resp.ErrorString) > 0 {
680 return "", "", fmt.Errorf(resp.ErrorString)
681 }
682 return resp.Stdout, resp.Stderr, nil
Jason Wu52cd1942022-09-08 15:37:57 +0000683 } else {
Chris Parsons9402ca82023-02-23 17:28:06 -0500684 eventHandler.Begin("bazel command")
685 defer eventHandler.End("bazel command")
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000686
687 stdout, stderr, err := bazel.ExecBazel(paths.bazelPath, absolutePath(paths.syntheticWorkspaceDir()), cmdRequest)
688 return string(stdout), string(stderr), err
Jason Wu52cd1942022-09-08 15:37:57 +0000689 }
690}
691
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000692func (context *mixedBuildBazelContext) createBazelCommand(config Config, runName bazel.RunName, command bazelCommand,
693 extraFlags ...string) bazel.CmdRequest {
Romain Jobredeaux41fd5e42021-08-27 15:59:39 +0000694 cmdFlags := []string{
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000695 "--output_base=" + absolutePath(context.paths.outputBase),
Romain Jobredeaux41fd5e42021-08-27 15:59:39 +0000696 command.command,
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700697 command.expression,
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000698 "--profile=" + shared.BazelMetricsFilename(context.paths, runName),
Jingwen Chen91220d72021-03-24 02:18:33 -0400699
Cole Faustb85d1a12022-11-08 18:14:01 -0800700 // We don't need to set --host_platforms because it's set in bazelrc files
701 // that the bazel shell script wrapper passes
Sasha Smundakb43ae1e2022-07-03 15:57:36 -0700702
703 // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network.
704 "--experimental_repository_disable_download",
705
706 // Suppress noise
707 "--ui_event_filters=-INFO",
Sam Delmerico658a4da2022-11-07 15:53:38 -0500708 "--noshow_progress",
709 "--norun_validations",
710 }
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400711 cmdFlags = append(cmdFlags, extraFlags...)
712
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700713 extraEnv := []string{
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000714 "HOME=" + context.paths.homeDir,
Lukacs T. Berki3069dd92021-05-11 16:54:29 +0200715 pwdPrefix(),
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000716 "BUILD_DIR=" + absolutePath(context.paths.soongOutDir),
Joe Onoratoba29f382022-10-24 06:38:11 -0700717 // Make OUT_DIR absolute here so build/bazel/bin/bazel uses the correct
Jingwen Chen8c523582021-06-01 11:19:53 +0000718 // OUT_DIR at <root>/out, instead of <root>/out/soong/workspace/out.
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000719 "OUT_DIR=" + absolutePath(context.paths.outDir()),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500720 // Disables local host detection of gcc; toolchain information is defined
721 // explicitly in BUILD files.
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700722 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1",
723 }
Sam Delmericocb3c52c2023-02-03 17:40:08 -0500724 for _, envvar := range allowedBazelEnvironmentVars {
725 val := config.Getenv(envvar)
726 if val == "" {
727 continue
728 }
729 extraEnv = append(extraEnv, fmt.Sprintf("%s=%s", envvar, val))
730 }
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000731 envVars := append(os.Environ(), extraEnv...)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400732
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000733 return bazel.CmdRequest{cmdFlags, envVars}
Jason Wu52cd1942022-09-08 15:37:57 +0000734}
735
Chris Parsonsc9089dc2023-04-24 16:21:27 +0000736func (context *mixedBuildBazelContext) printableCqueryCommand(bazelCmd bazel.CmdRequest) string {
737 args := append([]string{context.paths.bazelPath}, bazelCmd.Argv...)
738 outputString := strings.Join(bazelCmd.Env, " ") + " \"" + strings.Join(args, "\" \"") + "\""
Jason Wu52cd1942022-09-08 15:37:57 +0000739 return outputString
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400740}
741
Sasha Smundak39a301c2022-12-29 17:11:49 -0800742func (context *mixedBuildBazelContext) mainBzlFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500743 // TODO(cparsons): Define configuration transitions programmatically based
744 // on available archs.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400745 contents := `
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500746#####################################################
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400747# This file is generated by soong_build. Do not edit.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500748#####################################################
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400749def _config_node_transition_impl(settings, attr):
Cole Faustb85d1a12022-11-08 18:14:01 -0800750 if attr.os == "android" and attr.arch == "target":
751 target = "{PRODUCT}-{VARIANT}"
752 else:
753 target = "{PRODUCT}-{VARIANT}_%s_%s" % (attr.os, attr.arch)
Yu Liue4312402023-01-18 09:15:31 -0800754 apex_name = ""
755 if attr.within_apex:
756 # //build/bazel/rules/apex:apex_name has to be set to a non_empty value,
757 # otherwise //build/bazel/rules/apex:non_apex will be true and the
758 # "-D__ANDROID_APEX__" compiler flag will be missing. Apex_name is used
759 # in some validation on bazel side which don't really apply in mixed
760 # build because soong will do the work, so we just set it to a fixed
761 # value here.
762 apex_name = "dcla_apex"
763 outputs = {
Cole Faustb85d1a12022-11-08 18:14:01 -0800764 "//command_line_option:platforms": "@soong_injection//product_config_platforms/products/{PRODUCT}-{VARIANT}:%s" % target,
Yu Liue4312402023-01-18 09:15:31 -0800765 "@//build/bazel/rules/apex:within_apex": attr.within_apex,
766 "@//build/bazel/rules/apex:min_sdk_version": attr.apex_sdk_version,
767 "@//build/bazel/rules/apex:apex_name": apex_name,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500768 }
769
Yu Liue4312402023-01-18 09:15:31 -0800770 return outputs
771
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400772_config_node_transition = transition(
773 implementation = _config_node_transition_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500774 inputs = [],
775 outputs = [
776 "//command_line_option:platforms",
Yu Liue4312402023-01-18 09:15:31 -0800777 "@//build/bazel/rules/apex:within_apex",
778 "@//build/bazel/rules/apex:min_sdk_version",
779 "@//build/bazel/rules/apex:apex_name",
Chris Parsons8d6e4332021-02-22 16:13:50 -0500780 ],
781)
782
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400783def _passthrough_rule_impl(ctx):
784 return [DefaultInfo(files = depset(ctx.files.deps))]
785
786config_node = rule(
787 implementation = _passthrough_rule_impl,
788 attrs = {
Yu Liue4312402023-01-18 09:15:31 -0800789 "arch" : attr.string(mandatory = True),
790 "os" : attr.string(mandatory = True),
791 "within_apex" : attr.bool(default = False),
792 "apex_sdk_version" : attr.string(mandatory = True),
793 "deps" : attr.label_list(cfg = _config_node_transition, allow_files = True),
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400794 "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
795 },
Chris Parsons8d6e4332021-02-22 16:13:50 -0500796)
797
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400798
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500799# Rule representing the root of the build, to depend on all Bazel targets that
800# are required for the build. Building this target will build the entire Bazel
801# build tree.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400802mixed_build_root = rule(
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400803 implementation = _passthrough_rule_impl,
Chris Parsons8d6e4332021-02-22 16:13:50 -0500804 attrs = {
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400805 "deps" : attr.label_list(),
Chris Parsons8d6e4332021-02-22 16:13:50 -0500806 },
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400807)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500808
809def _phony_root_impl(ctx):
810 return []
811
812# Rule to depend on other targets but build nothing.
813# This is useful as follows: building a target of this rule will generate
814# symlink forests for all dependencies of the target, without executing any
815# actions of the build.
816phony_root = rule(
817 implementation = _phony_root_impl,
818 attrs = {"deps" : attr.label_list()},
819)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400820`
Cole Faustb85d1a12022-11-08 18:14:01 -0800821
822 productReplacer := strings.NewReplacer(
823 "{PRODUCT}", context.targetProduct,
824 "{VARIANT}", context.targetBuildVariant)
825
826 return []byte(productReplacer.Replace(contents))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400827}
828
Sasha Smundak39a301c2022-12-29 17:11:49 -0800829func (context *mixedBuildBazelContext) mainBuildFileContents() []byte {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500830 // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
831 // architecture mapping.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400832 formatString := `
833# This file is generated by soong_build. Do not edit.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400834load(":main.bzl", "config_node", "mixed_build_root", "phony_root")
835
836%s
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400837
838mixed_build_root(name = "buildroot",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400839 deps = [%s],
Jingwen Chen3952a902022-12-12 12:20:58 +0000840 testonly = True, # Unblocks testonly deps.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400841)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500842
843phony_root(name = "phonyroot",
844 deps = [":buildroot"],
Jingwen Chen3952a902022-12-12 12:20:58 +0000845 testonly = True, # Unblocks testonly deps.
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500846)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400847`
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400848 configNodeFormatString := `
849config_node(name = "%s",
850 arch = "%s",
Chris Parsons787fb362021-10-14 18:43:51 -0400851 os = "%s",
Yu Liue4312402023-01-18 09:15:31 -0800852 within_apex = %s,
853 apex_sdk_version = "%s",
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400854 deps = [%s],
Jingwen Chen3952a902022-12-12 12:20:58 +0000855 testonly = True, # Unblocks testonly deps.
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400856)
857`
858
859 configNodesSection := ""
860
Chris Parsons787fb362021-10-14 18:43:51 -0400861 labelsByConfig := map[string][]string{}
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500862
863 for _, val := range context.requests {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +0200864 labelString := fmt.Sprintf("\"@%s\"", val.label)
Chris Parsons787fb362021-10-14 18:43:51 -0400865 configString := getConfigString(val)
866 labelsByConfig[configString] = append(labelsByConfig[configString], labelString)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400867 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400868
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500869 // Configs need to be sorted to maintain determinism of the BUILD file.
870 sortedConfigs := make([]string, 0, len(labelsByConfig))
871 for val := range labelsByConfig {
872 sortedConfigs = append(sortedConfigs, val)
873 }
874 sort.Slice(sortedConfigs, func(i, j int) bool { return sortedConfigs[i] < sortedConfigs[j] })
875
Jingwen Chen1e347862021-09-02 12:11:49 +0000876 allLabels := []string{}
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500877 for _, configString := range sortedConfigs {
878 labels := labelsByConfig[configString]
Chris Parsons787fb362021-10-14 18:43:51 -0400879 configTokens := strings.Split(configString, "|")
Yu Liue4312402023-01-18 09:15:31 -0800880 if len(configTokens) < 2 {
Chris Parsons787fb362021-10-14 18:43:51 -0400881 panic(fmt.Errorf("Unexpected config string format: %s", configString))
Jingwen Chen1e347862021-09-02 12:11:49 +0000882 }
Chris Parsons787fb362021-10-14 18:43:51 -0400883 archString := configTokens[0]
884 osString := configTokens[1]
Yu Liue4312402023-01-18 09:15:31 -0800885 withinApex := "False"
886 apexSdkVerString := ""
Chris Parsons787fb362021-10-14 18:43:51 -0400887 targetString := fmt.Sprintf("%s_%s", osString, archString)
Yu Liue4312402023-01-18 09:15:31 -0800888 if len(configTokens) > 2 {
889 targetString += "_" + configTokens[2]
890 if configTokens[2] == withinApexToString(true) {
891 withinApex = "True"
892 }
893 }
894 if len(configTokens) > 3 {
895 targetString += "_" + configTokens[3]
896 apexSdkVerString = configTokens[3]
897 }
Chris Parsons787fb362021-10-14 18:43:51 -0400898 allLabels = append(allLabels, fmt.Sprintf("\":%s\"", targetString))
899 labelsString := strings.Join(labels, ",\n ")
Yu Liue4312402023-01-18 09:15:31 -0800900 configNodesSection += fmt.Sprintf(configNodeFormatString, targetString, archString, osString, withinApex, apexSdkVerString,
901 labelsString)
Chris Parsonsad0b5ba2021-03-29 21:09:24 -0400902 }
903
Jingwen Chen1e347862021-09-02 12:11:49 +0000904 return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(allLabels, ",\n ")))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400905}
906
Chris Parsons944e7d02021-03-11 11:08:46 -0500907func indent(original string) string {
908 result := ""
909 for _, line := range strings.Split(original, "\n") {
910 result += " " + line + "\n"
911 }
912 return result
913}
914
Chris Parsons808d84c2021-03-09 20:43:32 -0500915// Returns the file contents of the buildroot.cquery file that should be used for the cquery
916// expression in order to obtain information about buildroot and its dependencies.
Sasha Smundak39a301c2022-12-29 17:11:49 -0800917// The contents of this file depend on the mixedBuildBazelContext's requests; requests are enumerated
Chris Parsons808d84c2021-03-09 20:43:32 -0500918// and grouped by their request type. The data retrieved for each label depends on its
919// request type.
Sasha Smundak39a301c2022-12-29 17:11:49 -0800920func (context *mixedBuildBazelContext) cqueryStarlarkFileContents() []byte {
Liz Kammerf29df7c2021-04-02 13:37:39 -0400921 requestTypeToCqueryIdEntries := map[cqueryRequest][]string{}
Chris Parsons38851d82023-03-15 00:19:32 -0400922 requestTypes := []cqueryRequest{}
Chris Parsons3a8d0fb2023-02-02 18:16:29 -0500923 for _, val := range context.requests {
Chris Parsons944e7d02021-03-11 11:08:46 -0500924 cqueryId := getCqueryId(val)
925 mapEntryString := fmt.Sprintf("%q : True", cqueryId)
Chris Parsons38851d82023-03-15 00:19:32 -0400926 if _, seenKey := requestTypeToCqueryIdEntries[val.requestType]; !seenKey {
927 requestTypes = append(requestTypes, val.requestType)
928 }
Chris Parsons944e7d02021-03-11 11:08:46 -0500929 requestTypeToCqueryIdEntries[val.requestType] =
930 append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString)
931 }
932 labelRegistrationMapSection := ""
933 functionDefSection := ""
934 mainSwitchSection := ""
935
936 mapDeclarationFormatString := `
937%s = {
938 %s
939}
940`
941 functionDefFormatString := `
Cole Faust97d15272022-11-22 14:08:59 -0800942def %s(target, id_string):
Chris Parsons944e7d02021-03-11 11:08:46 -0500943%s
944`
945 mainSwitchSectionFormatString := `
946 if id_string in %s:
Cole Faust97d15272022-11-22 14:08:59 -0800947 return id_string + ">>" + %s(target, id_string)
Chris Parsons944e7d02021-03-11 11:08:46 -0500948`
949
Chris Parsons38851d82023-03-15 00:19:32 -0400950 for _, requestType := range requestTypes {
Chris Parsons944e7d02021-03-11 11:08:46 -0500951 labelMapName := requestType.Name() + "_Labels"
952 functionName := requestType.Name() + "_Fn"
953 labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString,
954 labelMapName,
955 strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n "))
956 functionDefSection += fmt.Sprintf(functionDefFormatString,
957 functionName,
958 indent(requestType.StarlarkFunctionBody()))
959 mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString,
960 labelMapName, functionName)
961 }
962
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400963 formatString := `
964# This file is generated by soong_build. Do not edit.
Chris Parsonsb0f8ac42020-10-23 16:48:08 -0400965
Cole Faustb85d1a12022-11-08 18:14:01 -0800966{LABEL_REGISTRATION_MAP_SECTION}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500967
Cole Faustb85d1a12022-11-08 18:14:01 -0800968{FUNCTION_DEF_SECTION}
Chris Parsons8d6e4332021-02-22 16:13:50 -0500969
970def get_arch(target):
Chris Parsons787fb362021-10-14 18:43:51 -0400971 # TODO(b/199363072): filegroups and file targets aren't associated with any
972 # specific platform architecture in mixed builds. This is consistent with how
973 # Soong treats filegroups, but it may not be the case with manually-written
974 # filegroup BUILD targets.
Chris Parsons8d6e4332021-02-22 16:13:50 -0500975 buildoptions = build_options(target)
Yu Liue4312402023-01-18 09:15:31 -0800976
Jingwen Chen8f222742021-10-07 12:02:23 +0000977 if buildoptions == None:
978 # File targets do not have buildoptions. File targets aren't associated with
Chris Parsons787fb362021-10-14 18:43:51 -0400979 # any specific platform architecture in mixed builds, so use the host.
980 return "x86_64|linux"
Cole Faustb85d1a12022-11-08 18:14:01 -0800981 platforms = buildoptions["//command_line_option:platforms"]
Chris Parsons8d6e4332021-02-22 16:13:50 -0500982 if len(platforms) != 1:
983 # An individual configured target should have only one platform architecture.
984 # Note that it's fine for there to be multiple architectures for the same label,
985 # but each is its own configured target.
986 fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
Cole Faustb85d1a12022-11-08 18:14:01 -0800987 platform_name = platforms[0].name
Chris Parsons8d6e4332021-02-22 16:13:50 -0500988 if platform_name == "host":
989 return "HOST"
Cole Faustb85d1a12022-11-08 18:14:01 -0800990 if not platform_name.startswith("{TARGET_PRODUCT}-{TARGET_BUILD_VARIANT}"):
991 fail("expected platform name of the form '{TARGET_PRODUCT}-{TARGET_BUILD_VARIANT}_android_<arch>' or '{TARGET_PRODUCT}-{TARGET_BUILD_VARIANT}_linux_<arch>', but was " + str(platforms))
992 platform_name = platform_name.removeprefix("{TARGET_PRODUCT}-{TARGET_BUILD_VARIANT}").removeprefix("_")
Yu Liue4312402023-01-18 09:15:31 -0800993 config_key = ""
Cole Faustb85d1a12022-11-08 18:14:01 -0800994 if not platform_name:
Yu Liue4312402023-01-18 09:15:31 -0800995 config_key = "target|android"
Chris Parsons94a0bba2021-06-04 15:03:47 -0400996 elif platform_name.startswith("android_"):
Yu Liue4312402023-01-18 09:15:31 -0800997 config_key = platform_name.removeprefix("android_") + "|android"
Chris Parsons94a0bba2021-06-04 15:03:47 -0400998 elif platform_name.startswith("linux_"):
Yu Liue4312402023-01-18 09:15:31 -0800999 config_key = platform_name.removeprefix("linux_") + "|linux"
Chris Parsons94a0bba2021-06-04 15:03:47 -04001000 else:
Cole Faustb85d1a12022-11-08 18:14:01 -08001001 fail("expected platform name of the form '{TARGET_PRODUCT}-{TARGET_BUILD_VARIANT}_android_<arch>' or '{TARGET_PRODUCT}-{TARGET_BUILD_VARIANT}_linux_<arch>', but was " + str(platforms))
Chris Parsons8d6e4332021-02-22 16:13:50 -05001002
Yu Liue4312402023-01-18 09:15:31 -08001003 within_apex = buildoptions.get("//build/bazel/rules/apex:within_apex")
1004 apex_sdk_version = buildoptions.get("//build/bazel/rules/apex:min_sdk_version")
1005
1006 if within_apex:
1007 config_key += "|within_apex"
1008 if apex_sdk_version != None and len(apex_sdk_version) > 0:
1009 config_key += "|" + apex_sdk_version
1010
1011 return config_key
1012
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001013def format(target):
Chris Parsons8d6e4332021-02-22 16:13:50 -05001014 id_string = str(target.label) + "|" + get_arch(target)
Chris Parsons944e7d02021-03-11 11:08:46 -05001015
Chris Parsons86dc2c22022-09-28 14:58:41 -04001016 # TODO(b/248106697): Remove once Bazel is updated to always normalize labels.
1017 if id_string.startswith("//"):
1018 id_string = "@" + id_string
1019
Cole Faustb85d1a12022-11-08 18:14:01 -08001020 {MAIN_SWITCH_SECTION}
1021
Chris Parsons944e7d02021-03-11 11:08:46 -05001022 # This target was not requested via cquery, and thus must be a dependency
1023 # of a requested target.
1024 return id_string + ">>NONE"
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001025`
Cole Faustb85d1a12022-11-08 18:14:01 -08001026 replacer := strings.NewReplacer(
1027 "{TARGET_PRODUCT}", context.targetProduct,
1028 "{TARGET_BUILD_VARIANT}", context.targetBuildVariant,
1029 "{LABEL_REGISTRATION_MAP_SECTION}", labelRegistrationMapSection,
1030 "{FUNCTION_DEF_SECTION}", functionDefSection,
1031 "{MAIN_SWITCH_SECTION}", mainSwitchSection)
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001032
Cole Faustb85d1a12022-11-08 18:14:01 -08001033 return []byte(replacer.Replace(formatString))
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001034}
1035
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001036// Returns a path containing build-related metadata required for interfacing
1037// with Bazel. Example: out/soong/bazel.
Liz Kammer8d62a4f2021-04-08 09:47:28 -04001038func (p *bazelPaths) intermediatesDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001039 return filepath.Join(p.soongOutDir, "bazel")
Chris Parsons8ccdb632020-11-17 15:41:01 -05001040}
1041
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001042// Returns the path where the contents of the @soong_injection repository live.
1043// It is used by Soong to tell Bazel things it cannot over the command line.
1044func (p *bazelPaths) injectedFilesDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001045 return filepath.Join(p.soongOutDir, bazel.SoongInjectionDirName)
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001046}
1047
1048// Returns the path of the synthetic Bazel workspace that contains a symlink
1049// forest composed the whole source tree and BUILD files generated by bp2build.
1050func (p *bazelPaths) syntheticWorkspaceDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001051 return filepath.Join(p.soongOutDir, "workspace")
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001052}
1053
Jingwen Chen8c523582021-06-01 11:19:53 +00001054// Returns the path to the top level out dir ($OUT_DIR).
1055func (p *bazelPaths) outDir() string {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001056 return filepath.Dir(p.soongOutDir)
Jingwen Chen8c523582021-06-01 11:19:53 +00001057}
1058
Sasha Smundak4975c822022-11-16 15:28:18 -08001059const buildrootLabel = "@soong_injection//mixed_builds:buildroot"
1060
1061var (
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001062 cqueryCmd = bazelCommand{"cquery", fmt.Sprintf("deps(%s, 2)", buildrootLabel)}
1063 aqueryCmd = bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)}
1064 buildCmd = bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"}
1065 allBazelCommands = []bazelCommand{aqueryCmd, cqueryCmd, buildCmd}
Sasha Smundak4975c822022-11-16 15:28:18 -08001066)
1067
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001068// Issues commands to Bazel to receive results for all cquery requests
1069// queued in the BazelContext.
Liz Kammer690fbac2023-02-10 11:11:17 -05001070func (context *mixedBuildBazelContext) InvokeBazel(config Config, ctx invokeBazelContext) error {
1071 eventHandler := ctx.GetEventHandler()
1072 eventHandler.Begin("bazel")
1073 defer eventHandler.End("bazel")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001074
Sasha Smundak4975c822022-11-16 15:28:18 -08001075 if metricsDir := context.paths.BazelMetricsDir(); metricsDir != "" {
1076 if err := os.MkdirAll(metricsDir, 0777); err != nil {
1077 return err
1078 }
1079 }
1080 context.results = make(map[cqueryKey]string)
Sam Delmericocb3c52c2023-02-03 17:40:08 -05001081 if err := context.runCquery(config, ctx); err != nil {
Sasha Smundak4975c822022-11-16 15:28:18 -08001082 return err
1083 }
1084 if err := context.runAquery(config, ctx); err != nil {
1085 return err
1086 }
Sam Delmericocb3c52c2023-02-03 17:40:08 -05001087 if err := context.generateBazelSymlinks(config, ctx); err != nil {
Sasha Smundak4975c822022-11-16 15:28:18 -08001088 return err
1089 }
1090
1091 // Clear requests.
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001092 context.requests = []cqueryKey{}
Sasha Smundak4975c822022-11-16 15:28:18 -08001093 return nil
1094}
1095
Liz Kammer690fbac2023-02-10 11:11:17 -05001096func (context *mixedBuildBazelContext) runCquery(config Config, ctx invokeBazelContext) error {
1097 eventHandler := ctx.GetEventHandler()
1098 eventHandler.Begin("cquery")
1099 defer eventHandler.End("cquery")
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001100 soongInjectionPath := absolutePath(context.paths.injectedFilesDir())
Lukacs T. Berki3069dd92021-05-11 16:54:29 +02001101 mixedBuildsPath := filepath.Join(soongInjectionPath, "mixed_builds")
1102 if _, err := os.Stat(mixedBuildsPath); os.IsNotExist(err) {
1103 err = os.MkdirAll(mixedBuildsPath, 0777)
Usta Shrestha902fd172022-03-02 15:27:49 -05001104 if err != nil {
1105 return err
1106 }
1107 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001108 if err := writeFileBytesIfChanged(filepath.Join(soongInjectionPath, "WORKSPACE.bazel"), []byte{}, 0666); err != nil {
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001109 return err
1110 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001111 if err := writeFileBytesIfChanged(filepath.Join(mixedBuildsPath, "main.bzl"), context.mainBzlFileContents(), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001112 return err
1113 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001114 if err := writeFileBytesIfChanged(filepath.Join(mixedBuildsPath, "BUILD.bazel"), context.mainBuildFileContents(), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001115 return err
1116 }
Lukacs T. Berkid6cd8132021-04-20 13:01:07 +02001117 cqueryFileRelpath := filepath.Join(context.paths.injectedFilesDir(), "buildroot.cquery")
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001118 if err := writeFileBytesIfChanged(absolutePath(cqueryFileRelpath), context.cqueryStarlarkFileContents(), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001119 return err
1120 }
Jingwen Chen1e347862021-09-02 12:11:49 +00001121
Yu Liue4312402023-01-18 09:15:31 -08001122 extraFlags := []string{"--output=starlark", "--starlark:file=" + absolutePath(cqueryFileRelpath)}
1123 if Bool(config.productVariables.ClangCoverage) {
1124 extraFlags = append(extraFlags, "--collect_code_coverage")
1125 }
1126
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001127 cqueryCmdRequest := context.createBazelCommand(config, bazel.CqueryBuildRootRunName, cqueryCmd, extraFlags...)
1128 cqueryOutput, cqueryErrorMessage, cqueryErr := context.issueBazelCommand(cqueryCmdRequest, context.paths, eventHandler)
Wei Licbd181c2022-11-16 08:59:23 -08001129 if cqueryErr != nil {
1130 return cqueryErr
Chris Parsons8d6e4332021-02-22 16:13:50 -05001131 }
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001132 cqueryCommandPrint := fmt.Sprintf("cquery command line:\n %s \n\n\n", context.printableCqueryCommand(cqueryCmdRequest))
Sasha Smundak0e87b182022-12-01 11:46:11 -08001133 if err := os.WriteFile(filepath.Join(soongInjectionPath, "cquery.out"), []byte(cqueryCommandPrint+cqueryOutput), 0666); err != nil {
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001134 return err
1135 }
Chris Parsonsb0f8ac42020-10-23 16:48:08 -04001136 cqueryResults := map[string]string{}
1137 for _, outputLine := range strings.Split(cqueryOutput, "\n") {
1138 if strings.Contains(outputLine, ">>") {
1139 splitLine := strings.SplitN(outputLine, ">>", 2)
1140 cqueryResults[splitLine[0]] = splitLine[1]
1141 }
1142 }
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001143 for _, val := range context.requests {
Chris Parsons8d6e4332021-02-22 16:13:50 -05001144 if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
Usta Shrestha902fd172022-03-02 15:27:49 -05001145 context.results[val] = cqueryResult
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001146 } else {
Chris Parsons808d84c2021-03-09 20:43:32 -05001147 return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]",
Wei Licbd181c2022-11-16 08:59:23 -08001148 getCqueryId(val), cqueryOutput, cqueryErrorMessage)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001149 }
1150 }
Sasha Smundak4975c822022-11-16 15:28:18 -08001151 return nil
1152}
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001153
Chris Parsons3a8d0fb2023-02-02 18:16:29 -05001154func writeFileBytesIfChanged(path string, contents []byte, perm os.FileMode) error {
1155 oldContents, err := os.ReadFile(path)
1156 if err != nil || !bytes.Equal(contents, oldContents) {
1157 err = os.WriteFile(path, contents, perm)
1158 }
1159 return nil
1160}
1161
Liz Kammer690fbac2023-02-10 11:11:17 -05001162func (context *mixedBuildBazelContext) runAquery(config Config, ctx invokeBazelContext) error {
1163 eventHandler := ctx.GetEventHandler()
1164 eventHandler.Begin("aquery")
1165 defer eventHandler.End("aquery")
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001166 // Issue an aquery command to retrieve action information about the bazel build tree.
1167 //
Sasha Smundakb43ae1e2022-07-03 15:57:36 -07001168 // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
1169 // proto sources, which would add a number of unnecessary dependencies.
Jason Wu118fd2b2022-10-27 18:41:15 +00001170 extraFlags := []string{"--output=proto", "--include_file_write_contents"}
Yu Liu8d82ac52022-05-17 15:13:28 -07001171 if Bool(config.productVariables.ClangCoverage) {
Sasha Smundakb43ae1e2022-07-03 15:57:36 -07001172 extraFlags = append(extraFlags, "--collect_code_coverage")
1173 paths := make([]string, 0, 2)
1174 if p := config.productVariables.NativeCoveragePaths; len(p) > 0 {
Sasha Smundak0e87b182022-12-01 11:46:11 -08001175 for i := range p {
Wei Licbd181c2022-11-16 08:59:23 -08001176 // TODO(b/259404593) convert path wildcard to regex values
1177 if p[i] == "*" {
1178 p[i] = ".*"
1179 }
1180 }
Sasha Smundakb43ae1e2022-07-03 15:57:36 -07001181 paths = append(paths, JoinWithPrefixAndSeparator(p, "+", ","))
1182 }
1183 if p := config.productVariables.NativeCoverageExcludePaths; len(p) > 0 {
1184 paths = append(paths, JoinWithPrefixAndSeparator(p, "-", ","))
1185 }
1186 if len(paths) > 0 {
1187 extraFlags = append(extraFlags, "--instrumentation_filter="+strings.Join(paths, ","))
Yu Liu8d82ac52022-05-17 15:13:28 -07001188 }
1189 }
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001190 aqueryOutput, _, err := context.issueBazelCommand(context.createBazelCommand(config, bazel.AqueryBuildRootRunName, aqueryCmd,
1191 extraFlags...), context.paths, eventHandler)
Sasha Smundak4975c822022-11-16 15:28:18 -08001192 if err != nil {
Chris Parsons4f069892021-01-15 12:22:41 -05001193 return err
1194 }
Liz Kammer690fbac2023-02-10 11:11:17 -05001195 context.buildStatements, context.depsets, err = bazel.AqueryBuildStatements([]byte(aqueryOutput), eventHandler)
Sasha Smundak4975c822022-11-16 15:28:18 -08001196 return err
1197}
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001198
Liz Kammer690fbac2023-02-10 11:11:17 -05001199func (context *mixedBuildBazelContext) generateBazelSymlinks(config Config, ctx invokeBazelContext) error {
1200 eventHandler := ctx.GetEventHandler()
1201 eventHandler.Begin("symlinks")
1202 defer eventHandler.End("symlinks")
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001203 // Issue a build command of the phony root to generate symlink forests for dependencies of the
1204 // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
1205 // but some of symlinks may be required to resolve source dependencies of the build.
Chris Parsonsc9089dc2023-04-24 16:21:27 +00001206 _, _, err := context.issueBazelCommand(context.createBazelCommand(config, bazel.BazelBuildPhonyRootRunName, buildCmd), context.paths, eventHandler)
Sasha Smundak4975c822022-11-16 15:28:18 -08001207 return err
Chris Parsonsf3c96ef2020-09-29 02:23:17 -04001208}
Chris Parsonsa798d962020-10-12 23:44:08 -04001209
Liz Kammera4655a92023-02-10 17:17:28 -05001210func (context *mixedBuildBazelContext) BuildStatementsToRegister() []*bazel.BuildStatement {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001211 return context.buildStatements
1212}
1213
Sasha Smundak39a301c2022-12-29 17:11:49 -08001214func (context *mixedBuildBazelContext) AqueryDepsets() []bazel.AqueryDepset {
Chris Parsons1a7aca02022-04-25 22:35:15 -04001215 return context.depsets
1216}
1217
Sasha Smundak39a301c2022-12-29 17:11:49 -08001218func (context *mixedBuildBazelContext) OutputBase() string {
Liz Kammer8d62a4f2021-04-08 09:47:28 -04001219 return context.paths.outputBase
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001220}
1221
Chris Parsonsa798d962020-10-12 23:44:08 -04001222// Singleton used for registering BUILD file ninja dependencies (needed
1223// for correctness of builds which use Bazel.
1224func BazelSingleton() Singleton {
1225 return &bazelSingleton{}
1226}
1227
1228type bazelSingleton struct{}
1229
1230func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001231 // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
Chris Parsonsad876012022-08-20 14:48:32 -04001232 if !ctx.Config().IsMixedBuildsEnabled() {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001233 return
1234 }
Chris Parsonsa798d962020-10-12 23:44:08 -04001235
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001236 // Add ninja file dependencies for files which all bazel invocations require.
1237 bazelBuildList := absolutePath(filepath.Join(
Lukacs T. Berkif9008072021-08-16 15:24:48 +02001238 filepath.Dir(ctx.Config().moduleListFile), "bazel.list"))
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001239 ctx.AddNinjaFileDeps(bazelBuildList)
1240
Sasha Smundak0e87b182022-12-01 11:46:11 -08001241 data, err := os.ReadFile(bazelBuildList)
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001242 if err != nil {
1243 ctx.Errorf(err.Error())
1244 }
1245 files := strings.Split(strings.TrimSpace(string(data)), "\n")
1246 for _, file := range files {
1247 ctx.AddNinjaFileDeps(file)
1248 }
1249
Chris Parsons1a7aca02022-04-25 22:35:15 -04001250 for _, depset := range ctx.Config().BazelContext.AqueryDepsets() {
1251 var outputs []Path
ustafdb3e342022-11-22 17:11:30 -05001252 var orderOnlies []Path
Chris Parsons0bfb1c02022-05-12 16:43:01 -04001253 for _, depsetDepHash := range depset.TransitiveDepSetHashes {
1254 otherDepsetName := bazelDepsetName(depsetDepHash)
Chris Parsons1a7aca02022-04-25 22:35:15 -04001255 outputs = append(outputs, PathForPhony(ctx, otherDepsetName))
1256 }
1257 for _, artifactPath := range depset.DirectArtifacts {
ustafdb3e342022-11-22 17:11:30 -05001258 pathInBazelOut := PathForBazelOut(ctx, artifactPath)
1259 if artifactPath == "bazel-out/volatile-status.txt" {
1260 // See https://bazel.build/docs/user-manual#workspace-status
1261 orderOnlies = append(orderOnlies, pathInBazelOut)
1262 } else {
1263 outputs = append(outputs, pathInBazelOut)
1264 }
Chris Parsons1a7aca02022-04-25 22:35:15 -04001265 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -04001266 thisDepsetName := bazelDepsetName(depset.ContentHash)
Chris Parsons1a7aca02022-04-25 22:35:15 -04001267 ctx.Build(pctx, BuildParams{
1268 Rule: blueprint.Phony,
1269 Outputs: []WritablePath{PathForPhony(ctx, thisDepsetName)},
1270 Implicits: outputs,
ustafdb3e342022-11-22 17:11:30 -05001271 OrderOnly: orderOnlies,
Chris Parsons1a7aca02022-04-25 22:35:15 -04001272 })
1273 }
1274
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001275 executionRoot := path.Join(ctx.Config().BazelContext.OutputBase(), "execroot", "__main__")
1276 bazelOutDir := path.Join(executionRoot, "bazel-out")
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001277 for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
Liz Kammera4655a92023-02-10 17:17:28 -05001278 // nil build statements are a valid case where we do not create an action because it is
1279 // unnecessary or handled by other processing
1280 if buildStatement == nil {
1281 continue
1282 }
Sasha Smundak1da064c2022-06-08 16:36:16 -07001283 if len(buildStatement.Command) > 0 {
1284 rule := NewRuleBuilder(pctx, ctx)
1285 createCommand(rule.Command(), buildStatement, executionRoot, bazelOutDir, ctx)
1286 desc := fmt.Sprintf("%s: %s", buildStatement.Mnemonic, buildStatement.OutputPaths)
1287 rule.Build(fmt.Sprintf("bazel %d", index), desc)
1288 continue
1289 }
1290 // Certain actions returned by aquery (for instance FileWrite) do not contain a command
1291 // and thus require special treatment. If BuildStatement were an interface implementing
1292 // buildRule(ctx) function, the code here would just call it.
1293 // Unfortunately, the BuildStatement is defined in
1294 // the 'bazel' package, which cannot depend on 'android' package where ctx is defined,
1295 // because this would cause circular dependency. So, until we move aquery processing
1296 // to the 'android' package, we need to handle special cases here.
Usta Shrestha13fd5ae2023-01-27 10:55:34 -05001297 switch buildStatement.Mnemonic {
1298 case "FileWrite", "SourceSymlinkManifest":
Cole Fausta7347492022-12-16 10:56:24 -08001299 out := PathForBazelOut(ctx, buildStatement.OutputPaths[0])
1300 WriteFileRuleVerbatim(ctx, out, buildStatement.FileContents)
Usta Shrestha13fd5ae2023-01-27 10:55:34 -05001301 case "SymlinkTree":
Sasha Smundakc180dbd2022-07-03 14:55:58 -07001302 // build-runfiles arguments are the manifest file and the target directory
1303 // where it creates the symlink tree according to this manifest (and then
1304 // writes the MANIFEST file to it).
1305 outManifest := PathForBazelOut(ctx, buildStatement.OutputPaths[0])
1306 outManifestPath := outManifest.String()
1307 if !strings.HasSuffix(outManifestPath, "MANIFEST") {
1308 panic("the base name of the symlink tree action should be MANIFEST, got " + outManifestPath)
1309 }
1310 outDir := filepath.Dir(outManifestPath)
1311 ctx.Build(pctx, BuildParams{
1312 Rule: buildRunfilesRule,
1313 Output: outManifest,
1314 Inputs: []Path{PathForBazelOut(ctx, buildStatement.InputPaths[0])},
1315 Description: "symlink tree for " + outDir,
1316 Args: map[string]string{
1317 "outDir": outDir,
1318 },
1319 })
Usta Shrestha13fd5ae2023-01-27 10:55:34 -05001320 default:
Rupert Shuttlewortha29903f2021-04-06 16:17:33 +00001321 panic(fmt.Sprintf("unhandled build statement: %v", buildStatement))
Chris Parsons8d6e4332021-02-22 16:13:50 -05001322 }
Chris Parsonsa798d962020-10-12 23:44:08 -04001323 }
1324}
Chris Parsons8d6e4332021-02-22 16:13:50 -05001325
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001326// Register bazel-owned build statements (obtained from the aquery invocation).
Liz Kammera4655a92023-02-10 17:17:28 -05001327func createCommand(cmd *RuleBuilderCommand, buildStatement *bazel.BuildStatement, executionRoot string, bazelOutDir string, ctx BuilderContext) {
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001328 // executionRoot is the action cwd.
1329 cmd.Text(fmt.Sprintf("cd '%s' &&", executionRoot))
1330
1331 // Remove old outputs, as some actions might not rerun if the outputs are detected.
1332 if len(buildStatement.OutputPaths) > 0 {
Jingwen Chenf3b1ec32022-11-07 15:02:48 +00001333 cmd.Text("rm -rf") // -r because outputs can be Bazel dir/tree artifacts.
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001334 for _, outputPath := range buildStatement.OutputPaths {
Usta Shresthaef922252022-06-02 14:23:02 -04001335 cmd.Text(fmt.Sprintf("'%s'", outputPath))
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001336 }
1337 cmd.Text("&&")
1338 }
1339
1340 for _, pair := range buildStatement.Env {
1341 // Set per-action env variables, if any.
1342 cmd.Flag(pair.Key + "=" + pair.Value)
1343 }
1344
1345 // The actual Bazel action.
Colin Crossd5c7ddb2022-12-06 16:27:17 -08001346 if len(buildStatement.Command) > 16*1024 {
1347 commandFile := PathForBazelOut(ctx, buildStatement.OutputPaths[0]+".sh")
1348 WriteFileRule(ctx, commandFile, buildStatement.Command)
1349
1350 cmd.Text("bash").Text(buildStatement.OutputPaths[0] + ".sh").Implicit(commandFile)
1351 } else {
1352 cmd.Text(buildStatement.Command)
1353 }
Usta Shresthaacd5a0c2022-06-22 11:20:50 -04001354
1355 for _, outputPath := range buildStatement.OutputPaths {
1356 cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
1357 }
1358 for _, inputPath := range buildStatement.InputPaths {
1359 cmd.Implicit(PathForBazelOut(ctx, inputPath))
1360 }
1361 for _, inputDepsetHash := range buildStatement.InputDepsetHashes {
1362 otherDepsetName := bazelDepsetName(inputDepsetHash)
1363 cmd.Implicit(PathForPhony(ctx, otherDepsetName))
1364 }
1365
1366 if depfile := buildStatement.Depfile; depfile != nil {
1367 // The paths in depfile are relative to `executionRoot`.
1368 // Hence, they need to be corrected by replacing "bazel-out"
1369 // with the full `bazelOutDir`.
1370 // Otherwise, implicit outputs and implicit inputs under "bazel-out/"
1371 // would be deemed missing.
1372 // (Note: The regexp uses a capture group because the version of sed
1373 // does not support a look-behind pattern.)
1374 replacement := fmt.Sprintf(`&& sed -i'' -E 's@(^|\s|")bazel-out/@\1%s/@g' '%s'`,
1375 bazelOutDir, *depfile)
1376 cmd.Text(replacement)
1377 cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile))
1378 }
1379
1380 for _, symlinkPath := range buildStatement.SymlinkPaths {
1381 cmd.ImplicitSymlinkOutput(PathForBazelOut(ctx, symlinkPath))
1382 }
1383}
1384
Chris Parsons8d6e4332021-02-22 16:13:50 -05001385func getCqueryId(key cqueryKey) string {
Chris Parsons787fb362021-10-14 18:43:51 -04001386 return key.label + "|" + getConfigString(key)
Chris Parsons8d6e4332021-02-22 16:13:50 -05001387}
1388
Chris Parsons787fb362021-10-14 18:43:51 -04001389func getConfigString(key cqueryKey) string {
Liz Kammer0940b892022-03-18 15:55:04 -04001390 arch := key.configKey.arch
Chris Parsons787fb362021-10-14 18:43:51 -04001391 if len(arch) == 0 || arch == "common" {
Sasha Smundak9d46dcf2022-06-08 12:10:36 -07001392 if key.configKey.osType.Class == Device {
1393 // For the generic Android, the expected result is "target|android", which
1394 // corresponds to the product_variable_config named "android_target" in
1395 // build/bazel/platforms/BUILD.bazel.
1396 arch = "target"
1397 } else {
1398 // Use host platform, which is currently hardcoded to be x86_64.
1399 arch = "x86_64"
1400 }
Chris Parsons8d6e4332021-02-22 16:13:50 -05001401 }
Usta Shrestha16ac1352022-06-22 11:01:55 -04001402 osName := key.configKey.osType.Name
Colin Cross046fcea2022-12-20 15:32:18 -08001403 if len(osName) == 0 || osName == "common_os" || osName == "linux_glibc" || osName == "linux_musl" {
Chris Parsons787fb362021-10-14 18:43:51 -04001404 // Use host OS, which is currently hardcoded to be linux.
Usta Shrestha16ac1352022-06-22 11:01:55 -04001405 osName = "linux"
Chris Parsons787fb362021-10-14 18:43:51 -04001406 }
Yu Liue4312402023-01-18 09:15:31 -08001407 keyString := arch + "|" + osName
1408 if key.configKey.apexKey.WithinApex {
1409 keyString += "|" + withinApexToString(key.configKey.apexKey.WithinApex)
1410 }
1411
1412 if len(key.configKey.apexKey.ApexSdkVersion) > 0 {
1413 keyString += "|" + key.configKey.apexKey.ApexSdkVersion
1414 }
1415
1416 return keyString
Chris Parsons787fb362021-10-14 18:43:51 -04001417}
1418
Chris Parsonsf874e462022-05-10 13:50:12 -04001419func GetConfigKey(ctx BaseModuleContext) configKey {
Liz Kammer0940b892022-03-18 15:55:04 -04001420 return configKey{
1421 // use string because Arch is not a valid key in go
1422 arch: ctx.Arch().String(),
1423 osType: ctx.Os(),
1424 }
Chris Parsons8d6e4332021-02-22 16:13:50 -05001425}
Chris Parsons1a7aca02022-04-25 22:35:15 -04001426
Yu Liue4312402023-01-18 09:15:31 -08001427func GetConfigKeyApexVariant(ctx BaseModuleContext, apexKey *ApexConfigKey) configKey {
1428 configKey := GetConfigKey(ctx)
1429
1430 if apexKey != nil {
1431 configKey.apexKey = ApexConfigKey{
1432 WithinApex: apexKey.WithinApex,
1433 ApexSdkVersion: apexKey.ApexSdkVersion,
1434 }
1435 }
1436
1437 return configKey
1438}
1439
Chris Parsons0bfb1c02022-05-12 16:43:01 -04001440func bazelDepsetName(contentHash string) string {
1441 return fmt.Sprintf("bazel_depset_%s", contentHash)
Chris Parsons1a7aca02022-04-25 22:35:15 -04001442}
Sam Delmericocb3c52c2023-02-03 17:40:08 -05001443
1444func EnvironmentVarsFile(config Config) string {
1445 return fmt.Sprintf(bazel.GeneratedBazelFileWarning+`
1446_env = %s
1447
1448env = _env
1449`,
1450 starlark_fmt.PrintStringList(allowedBazelEnvironmentVars, 0),
1451 )
1452}