Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 19 | "crypto/sha1" |
| 20 | "encoding/hex" |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 21 | "fmt" |
| 22 | "os" |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 23 | "path" |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 24 | "path/filepath" |
Cole Faust | 16d1094 | 2023-08-02 11:45:43 -0700 | [diff] [blame] | 25 | "regexp" |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 26 | "runtime" |
Cole Faust | 705968d | 2022-12-14 11:32:05 -0800 | [diff] [blame] | 27 | "sort" |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 28 | "strings" |
| 29 | "sync" |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 30 | |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 31 | "android/soong/android/allowlists" |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 32 | "android/soong/bazel/cquery" |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 33 | "android/soong/shared" |
Cole Faust | 8a161be | 2023-06-14 15:45:12 -0700 | [diff] [blame] | 34 | "android/soong/starlark_import" |
Jingwen Chen | 379221f | 2023-03-30 13:19:29 +0000 | [diff] [blame] | 35 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 36 | "github.com/google/blueprint" |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 37 | "github.com/google/blueprint/metrics" |
Liz Kammer | 8206d4f | 2021-03-03 16:40:52 -0500 | [diff] [blame] | 38 | |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 39 | "android/soong/bazel" |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 40 | ) |
| 41 | |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 42 | var ( |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 43 | _ = pctx.HostBinToolVariable("bazelBuildRunfilesTool", "build-runfiles") |
| 44 | buildRunfilesRule = pctx.AndroidStaticRule("bazelBuildRunfiles", blueprint.RuleParams{ |
| 45 | Command: "${bazelBuildRunfilesTool} ${in} ${outDir}", |
| 46 | Depfile: "", |
| 47 | Description: "", |
| 48 | CommandDeps: []string{"${bazelBuildRunfilesTool}"}, |
| 49 | }, "outDir") |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 50 | ) |
| 51 | |
Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 52 | func registerMixedBuildsMutator(ctx RegisterMutatorsContext) { |
| 53 | ctx.BottomUp("mixed_builds_prep", mixedBuildsPrepareMutator).Parallel() |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | func RegisterMixedBuildsMutator(ctx RegistrationContext) { |
Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 57 | ctx.FinalDepsMutators(registerMixedBuildsMutator) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | func mixedBuildsPrepareMutator(ctx BottomUpMutatorContext) { |
| 61 | if m := ctx.Module(); m.Enabled() { |
| 62 | if mixedBuildMod, ok := m.(MixedBuildBuildable); ok { |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 63 | mixedBuildEnabled := MixedBuildsEnabled(ctx) |
| 64 | queueMixedBuild := mixedBuildMod.IsMixedBuildSupported(ctx) && mixedBuildEnabled == MixedBuildEnabled |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 65 | if queueMixedBuild { |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 66 | mixedBuildMod.QueueBazelCall(ctx) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 72 | type cqueryRequest interface { |
| 73 | // Name returns a string name for this request type. Such request type names must be unique, |
| 74 | // and must only consist of alphanumeric characters. |
| 75 | Name() string |
| 76 | |
| 77 | // StarlarkFunctionBody returns a starlark function body to process this request type. |
| 78 | // The returned string is the body of a Starlark function which obtains |
| 79 | // all request-relevant information about a target and returns a string containing |
| 80 | // this information. |
| 81 | // The function should have the following properties: |
Cole Faust | 97d1527 | 2022-11-22 14:08:59 -0800 | [diff] [blame] | 82 | // - The arguments are `target` (a configured target) and `id_string` (the label + configuration). |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 83 | // - The return value must be a string. |
| 84 | // - The function body should not be indented outside of its own scope. |
| 85 | StarlarkFunctionBody() string |
| 86 | } |
| 87 | |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 88 | // Portion of cquery map key to describe target configuration. |
| 89 | type configKey struct { |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 90 | arch string |
| 91 | osType OsType |
| 92 | apexKey ApexConfigKey |
| 93 | } |
| 94 | |
| 95 | type ApexConfigKey struct { |
| 96 | WithinApex bool |
| 97 | ApexSdkVersion string |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 98 | ApiDomain string |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | func (c ApexConfigKey) String() string { |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 102 | return fmt.Sprintf("%s_%s_%s", withinApexToString(c.WithinApex), c.ApexSdkVersion, c.ApiDomain) |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | func withinApexToString(withinApex bool) string { |
| 106 | if withinApex { |
| 107 | return "within_apex" |
| 108 | } |
| 109 | return "" |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 112 | func (c configKey) String() string { |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 113 | return fmt.Sprintf("%s::%s::%s", c.arch, c.osType, c.apexKey) |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 116 | // Map key to describe bazel cquery requests. |
| 117 | type cqueryKey struct { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 118 | label string |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 119 | requestType cqueryRequest |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 120 | configKey configKey |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Chris Parsons | 86dc2c2 | 2022-09-28 14:58:41 -0400 | [diff] [blame] | 123 | func makeCqueryKey(label string, cqueryRequest cqueryRequest, cfgKey configKey) cqueryKey { |
| 124 | if strings.HasPrefix(label, "//") { |
| 125 | // Normalize Bazel labels to specify main repository explicitly. |
| 126 | label = "@" + label |
| 127 | } |
| 128 | return cqueryKey{label, cqueryRequest, cfgKey} |
| 129 | } |
| 130 | |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 131 | func (c cqueryKey) String() string { |
| 132 | return fmt.Sprintf("cquery(%s,%s,%s)", c.label, c.requestType.Name(), c.configKey) |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 135 | type invokeBazelContext interface { |
| 136 | GetEventHandler() *metrics.EventHandler |
| 137 | } |
| 138 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 139 | // BazelContext is a context object useful for interacting with Bazel during |
| 140 | // the course of a build. Use of Bazel to evaluate part of the build graph |
| 141 | // is referred to as a "mixed build". (Some modules are managed by Soong, |
| 142 | // some are managed by Bazel). To facilitate interop between these build |
| 143 | // subgraphs, Soong may make requests to Bazel and evaluate their responses |
| 144 | // so that Soong modules may accurately depend on Bazel targets. |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 145 | type BazelContext interface { |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 146 | // Add a cquery request to the bazel request queue. All queued requests |
| 147 | // will be sent to Bazel on a subsequent invocation of InvokeBazel. |
| 148 | QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) |
| 149 | |
| 150 | // ** Cquery Results Retrieval Functions |
| 151 | // The below functions pertain to retrieving cquery results from a prior |
| 152 | // InvokeBazel function call and parsing the results. |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 153 | |
| 154 | // Returns result files built by building the given bazel target label. |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 155 | GetOutputFiles(label string, cfgKey configKey) ([]string, error) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 156 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 157 | // Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order). |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 158 | GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 159 | |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 160 | // Returns the results of the GetApexInfo query (including output files) |
Liz Kammer | be6a712 | 2022-11-04 16:05:11 -0400 | [diff] [blame] | 161 | GetApexInfo(label string, cfgkey configKey) (cquery.ApexInfo, error) |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 162 | |
Sasha Smundak | edd1666 | 2022-10-07 14:44:50 -0700 | [diff] [blame] | 163 | // Returns the results of the GetCcUnstrippedInfo query |
| 164 | GetCcUnstrippedInfo(label string, cfgkey configKey) (cquery.CcUnstrippedInfo, error) |
| 165 | |
Spandan Das | bd15681 | 2023-06-05 22:43:13 +0000 | [diff] [blame] | 166 | // Returns the results of the GetPrebuiltFileInfo query |
| 167 | GetPrebuiltFileInfo(label string, cfgKey configKey) (cquery.PrebuiltFileInfo, error) |
| 168 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 169 | // ** end Cquery Results Retrieval Functions |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 170 | |
| 171 | // Issues commands to Bazel to receive results for all cquery requests |
Sasha Smundak | 4975c82 | 2022-11-16 15:28:18 -0800 | [diff] [blame] | 172 | // queued in the BazelContext. The ctx argument is optional and is only |
| 173 | // used for performance data collection |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 174 | InvokeBazel(config Config, ctx invokeBazelContext) error |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 175 | |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 176 | // Returns true if Bazel handling is enabled for the module with the given name. |
| 177 | // Note that this only implies "bazel mixed build" allowlisting. The caller |
| 178 | // should independently verify the module is eligible for Bazel handling |
| 179 | // (for example, that it is MixedBuildBuildable). |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 180 | IsModuleNameAllowed(moduleName string, withinApex bool) bool |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 181 | |
| 182 | // Returns the bazel output base (the root directory for all bazel intermediate outputs). |
| 183 | OutputBase() string |
| 184 | |
| 185 | // Returns build statements which should get registered to reflect Bazel's outputs. |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 186 | BuildStatementsToRegister() []*bazel.BuildStatement |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 187 | |
| 188 | // Returns the depsets defined in Bazel's aquery response. |
| 189 | AqueryDepsets() []bazel.AqueryDepset |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 190 | |
| 191 | QueueBazelSandwichCqueryRequests(config Config) error |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 192 | } |
| 193 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 194 | type bazelRunner interface { |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 195 | issueBazelCommand(cmdRequest bazel.CmdRequest, paths *bazelPaths, eventHandler *metrics.EventHandler) (output string, errorMessage string, error error) |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | type bazelPaths struct { |
MarkDacek | 0d5bca5 | 2022-10-10 20:07:48 +0000 | [diff] [blame] | 199 | homeDir string |
| 200 | bazelPath string |
| 201 | outputBase string |
| 202 | workspaceDir string |
| 203 | soongOutDir string |
| 204 | metricsDir string |
| 205 | bazelDepsFile string |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 206 | } |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 207 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 208 | // A context object which tracks queued requests that need to be made to Bazel, |
| 209 | // and their results after the requests have been made. |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 210 | type mixedBuildBazelContext struct { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 211 | bazelRunner |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 212 | paths *bazelPaths |
| 213 | // cquery requests that have not yet been issued to Bazel. This list is maintained |
| 214 | // in a sorted state, and is guaranteed to have no duplicates. |
| 215 | requests []cqueryKey |
| 216 | requestMutex sync.Mutex // requests can be written in parallel |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 217 | |
| 218 | results map[cqueryKey]string // Results of cquery requests after Bazel invocations |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 219 | |
| 220 | // Build statements which should get registered to reflect Bazel's outputs. |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 221 | buildStatements []*bazel.BuildStatement |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 222 | |
| 223 | // Depsets which should be used for Bazel's build statements. |
| 224 | depsets []bazel.AqueryDepset |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 225 | |
| 226 | // Per-module allowlist/denylist functionality to control whether analysis of |
| 227 | // modules are handled by Bazel. For modules which do not have a Bazel definition |
| 228 | // (or do not sufficiently support bazel handling via MixedBuildBuildable), |
| 229 | // this allowlist will have no effect, even if the module is explicitly allowlisted here. |
| 230 | // Per-module denylist to opt modules out of bazel handling. |
| 231 | bazelDisabledModules map[string]bool |
| 232 | // Per-module allowlist to opt modules in to bazel handling. |
| 233 | bazelEnabledModules map[string]bool |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 234 | // DCLA modules are enabled when used in apex. |
| 235 | bazelDclaEnabledModules map[string]bool |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 236 | |
| 237 | targetProduct string |
| 238 | targetBuildVariant string |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 239 | } |
| 240 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 241 | var _ BazelContext = &mixedBuildBazelContext{} |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 242 | |
| 243 | // A bazel context to use when Bazel is disabled. |
| 244 | type noopBazelContext struct{} |
| 245 | |
| 246 | var _ BazelContext = noopBazelContext{} |
| 247 | |
| 248 | // A bazel context to use for tests. |
| 249 | type MockBazelContext struct { |
Liz Kammer | a92e844 | 2021-04-07 20:25:21 -0400 | [diff] [blame] | 250 | OutputBaseDir string |
| 251 | |
Spandan Das | bd15681 | 2023-06-05 22:43:13 +0000 | [diff] [blame] | 252 | LabelToOutputFiles map[string][]string |
| 253 | LabelToCcInfo map[string]cquery.CcInfo |
| 254 | LabelToPythonBinary map[string]string |
| 255 | LabelToApexInfo map[string]cquery.ApexInfo |
| 256 | LabelToCcBinary map[string]cquery.CcUnstrippedInfo |
| 257 | LabelToPrebuiltFileInfo map[string]cquery.PrebuiltFileInfo |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 258 | |
| 259 | BazelRequests map[string]bool |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 260 | } |
| 261 | |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 262 | func (m MockBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) { |
| 263 | key := BuildMockBazelContextRequestKey(label, requestType, cfgKey.arch, cfgKey.osType, cfgKey.apexKey) |
| 264 | if m.BazelRequests == nil { |
| 265 | m.BazelRequests = make(map[string]bool) |
| 266 | } |
| 267 | m.BazelRequests[key] = true |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 268 | } |
| 269 | |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 270 | func (m MockBazelContext) QueueBazelSandwichCqueryRequests(config Config) error { |
| 271 | panic("unimplemented") |
| 272 | } |
| 273 | |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 274 | func (m MockBazelContext) GetOutputFiles(label string, _ configKey) ([]string, error) { |
Sam Delmerico | ce39f83 | 2023-01-23 14:04:24 -0500 | [diff] [blame] | 275 | result, ok := m.LabelToOutputFiles[label] |
| 276 | if !ok { |
| 277 | return []string{}, fmt.Errorf("no target with label %q in LabelToOutputFiles", label) |
| 278 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 279 | return result, nil |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 280 | } |
| 281 | |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 282 | func (m MockBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) { |
Sam Delmerico | ce39f83 | 2023-01-23 14:04:24 -0500 | [diff] [blame] | 283 | result, ok := m.LabelToCcInfo[label] |
| 284 | if !ok { |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 285 | key := BuildMockBazelContextResultKey(label, cfgKey.arch, cfgKey.osType, cfgKey.apexKey) |
| 286 | result, ok = m.LabelToCcInfo[key] |
| 287 | if !ok { |
| 288 | return cquery.CcInfo{}, fmt.Errorf("no target with label %q in LabelToCcInfo", label) |
| 289 | } |
Sam Delmerico | ce39f83 | 2023-01-23 14:04:24 -0500 | [diff] [blame] | 290 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 291 | return result, nil |
| 292 | } |
| 293 | |
Liz Kammer | be6a712 | 2022-11-04 16:05:11 -0400 | [diff] [blame] | 294 | func (m MockBazelContext) GetApexInfo(label string, _ configKey) (cquery.ApexInfo, error) { |
Sam Delmerico | ce39f83 | 2023-01-23 14:04:24 -0500 | [diff] [blame] | 295 | result, ok := m.LabelToApexInfo[label] |
| 296 | if !ok { |
| 297 | return cquery.ApexInfo{}, fmt.Errorf("no target with label %q in LabelToApexInfo", label) |
| 298 | } |
Liz Kammer | 0e255ef | 2022-11-04 16:07:04 -0400 | [diff] [blame] | 299 | return result, nil |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Sasha Smundak | edd1666 | 2022-10-07 14:44:50 -0700 | [diff] [blame] | 302 | func (m MockBazelContext) GetCcUnstrippedInfo(label string, _ configKey) (cquery.CcUnstrippedInfo, error) { |
Sam Delmerico | ce39f83 | 2023-01-23 14:04:24 -0500 | [diff] [blame] | 303 | result, ok := m.LabelToCcBinary[label] |
| 304 | if !ok { |
| 305 | return cquery.CcUnstrippedInfo{}, fmt.Errorf("no target with label %q in LabelToCcBinary", label) |
| 306 | } |
Sasha Smundak | edd1666 | 2022-10-07 14:44:50 -0700 | [diff] [blame] | 307 | return result, nil |
| 308 | } |
| 309 | |
Spandan Das | bd15681 | 2023-06-05 22:43:13 +0000 | [diff] [blame] | 310 | func (m MockBazelContext) GetPrebuiltFileInfo(label string, _ configKey) (cquery.PrebuiltFileInfo, error) { |
| 311 | result, ok := m.LabelToPrebuiltFileInfo[label] |
| 312 | if !ok { |
| 313 | return cquery.PrebuiltFileInfo{}, fmt.Errorf("no target with label %q in LabelToPrebuiltFileInfo", label) |
| 314 | } |
| 315 | return result, nil |
| 316 | } |
| 317 | |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 318 | func (m MockBazelContext) InvokeBazel(_ Config, _ invokeBazelContext) error { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 319 | panic("unimplemented") |
| 320 | } |
| 321 | |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 322 | func (m MockBazelContext) IsModuleNameAllowed(_ string, _ bool) bool { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 323 | return true |
| 324 | } |
| 325 | |
Liz Kammer | a92e844 | 2021-04-07 20:25:21 -0400 | [diff] [blame] | 326 | func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 327 | |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 328 | func (m MockBazelContext) BuildStatementsToRegister() []*bazel.BuildStatement { |
| 329 | return []*bazel.BuildStatement{} |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 330 | } |
| 331 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 332 | func (m MockBazelContext) AqueryDepsets() []bazel.AqueryDepset { |
| 333 | return []bazel.AqueryDepset{} |
| 334 | } |
| 335 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 336 | var _ BazelContext = MockBazelContext{} |
| 337 | |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 338 | func BuildMockBazelContextRequestKey(label string, request cqueryRequest, arch string, osType OsType, apexKey ApexConfigKey) string { |
| 339 | cfgKey := configKey{ |
| 340 | arch: arch, |
| 341 | osType: osType, |
| 342 | apexKey: apexKey, |
| 343 | } |
| 344 | |
| 345 | return strings.Join([]string{label, request.Name(), cfgKey.String()}, "_") |
| 346 | } |
| 347 | |
| 348 | func BuildMockBazelContextResultKey(label string, arch string, osType OsType, apexKey ApexConfigKey) string { |
| 349 | cfgKey := configKey{ |
| 350 | arch: arch, |
| 351 | osType: osType, |
| 352 | apexKey: apexKey, |
| 353 | } |
| 354 | |
| 355 | return strings.Join([]string{label, cfgKey.String()}, "_") |
| 356 | } |
| 357 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 358 | func (bazelCtx *mixedBuildBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) { |
Chris Parsons | 86dc2c2 | 2022-09-28 14:58:41 -0400 | [diff] [blame] | 359 | key := makeCqueryKey(label, requestType, cfgKey) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 360 | bazelCtx.requestMutex.Lock() |
| 361 | defer bazelCtx.requestMutex.Unlock() |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 362 | |
| 363 | // Insert key into requests, maintaining the sort, and only if it's not duplicate. |
| 364 | keyString := key.String() |
| 365 | foundEqual := false |
| 366 | notLessThanKeyString := func(i int) bool { |
| 367 | s := bazelCtx.requests[i].String() |
| 368 | v := strings.Compare(s, keyString) |
| 369 | if v == 0 { |
| 370 | foundEqual = true |
| 371 | } |
| 372 | return v >= 0 |
| 373 | } |
| 374 | targetIndex := sort.Search(len(bazelCtx.requests), notLessThanKeyString) |
| 375 | if foundEqual { |
| 376 | return |
| 377 | } |
| 378 | |
| 379 | if targetIndex == len(bazelCtx.requests) { |
| 380 | bazelCtx.requests = append(bazelCtx.requests, key) |
| 381 | } else { |
| 382 | bazelCtx.requests = append(bazelCtx.requests[:targetIndex+1], bazelCtx.requests[targetIndex:]...) |
| 383 | bazelCtx.requests[targetIndex] = key |
| 384 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 385 | } |
| 386 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 387 | func (bazelCtx *mixedBuildBazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) { |
Chris Parsons | 86dc2c2 | 2022-09-28 14:58:41 -0400 | [diff] [blame] | 388 | key := makeCqueryKey(label, cquery.GetOutputFiles, cfgKey) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 389 | if rawString, ok := bazelCtx.results[key]; ok { |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 390 | bazelOutput := strings.TrimSpace(rawString) |
Chris Parsons | 86dc2c2 | 2022-09-28 14:58:41 -0400 | [diff] [blame] | 391 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 392 | return cquery.GetOutputFiles.ParseResult(bazelOutput), nil |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 393 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 394 | return nil, fmt.Errorf("no bazel response found for %v", key) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 395 | } |
| 396 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 397 | func (bazelCtx *mixedBuildBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) { |
Chris Parsons | 86dc2c2 | 2022-09-28 14:58:41 -0400 | [diff] [blame] | 398 | key := makeCqueryKey(label, cquery.GetCcInfo, cfgKey) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 399 | if rawString, ok := bazelCtx.results[key]; ok { |
Alex Márquez Pérez MuñÃz DÃaz Púras Thaureaux | a05a255 | 2021-08-11 16:48:30 +0000 | [diff] [blame] | 400 | bazelOutput := strings.TrimSpace(rawString) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 401 | return cquery.GetCcInfo.ParseResult(bazelOutput) |
Alex Márquez Pérez MuñÃz DÃaz Púras Thaureaux | a05a255 | 2021-08-11 16:48:30 +0000 | [diff] [blame] | 402 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 403 | return cquery.CcInfo{}, fmt.Errorf("no bazel response found for %v", key) |
Alex Márquez Pérez MuñÃz DÃaz Púras Thaureaux | a05a255 | 2021-08-11 16:48:30 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 406 | func (bazelCtx *mixedBuildBazelContext) GetApexInfo(label string, cfgKey configKey) (cquery.ApexInfo, error) { |
Chris Parsons | 86dc2c2 | 2022-09-28 14:58:41 -0400 | [diff] [blame] | 407 | key := makeCqueryKey(label, cquery.GetApexInfo, cfgKey) |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 408 | if rawString, ok := bazelCtx.results[key]; ok { |
Liz Kammer | 1b7ed9b | 2022-11-09 10:05:05 -0500 | [diff] [blame] | 409 | return cquery.GetApexInfo.ParseResult(strings.TrimSpace(rawString)) |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 410 | } |
Liz Kammer | be6a712 | 2022-11-04 16:05:11 -0400 | [diff] [blame] | 411 | return cquery.ApexInfo{}, fmt.Errorf("no bazel response found for %v", key) |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 414 | func (bazelCtx *mixedBuildBazelContext) GetCcUnstrippedInfo(label string, cfgKey configKey) (cquery.CcUnstrippedInfo, error) { |
Sasha Smundak | edd1666 | 2022-10-07 14:44:50 -0700 | [diff] [blame] | 415 | key := makeCqueryKey(label, cquery.GetCcUnstrippedInfo, cfgKey) |
| 416 | if rawString, ok := bazelCtx.results[key]; ok { |
Liz Kammer | 1b7ed9b | 2022-11-09 10:05:05 -0500 | [diff] [blame] | 417 | return cquery.GetCcUnstrippedInfo.ParseResult(strings.TrimSpace(rawString)) |
Sasha Smundak | edd1666 | 2022-10-07 14:44:50 -0700 | [diff] [blame] | 418 | } |
| 419 | return cquery.CcUnstrippedInfo{}, fmt.Errorf("no bazel response for %s", key) |
| 420 | } |
| 421 | |
Spandan Das | bd15681 | 2023-06-05 22:43:13 +0000 | [diff] [blame] | 422 | func (bazelCtx *mixedBuildBazelContext) GetPrebuiltFileInfo(label string, cfgKey configKey) (cquery.PrebuiltFileInfo, error) { |
| 423 | key := makeCqueryKey(label, cquery.GetPrebuiltFileInfo, cfgKey) |
| 424 | if rawString, ok := bazelCtx.results[key]; ok { |
| 425 | return cquery.GetPrebuiltFileInfo.ParseResult(strings.TrimSpace(rawString)) |
| 426 | } |
| 427 | return cquery.PrebuiltFileInfo{}, fmt.Errorf("no bazel response for %s", key) |
| 428 | } |
| 429 | |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 430 | func (n noopBazelContext) QueueBazelRequest(_ string, _ cqueryRequest, _ configKey) { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 431 | panic("unimplemented") |
| 432 | } |
| 433 | |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 434 | func (n noopBazelContext) QueueBazelSandwichCqueryRequests(config Config) error { |
| 435 | panic("unimplemented") |
| 436 | } |
| 437 | |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 438 | func (n noopBazelContext) GetOutputFiles(_ string, _ configKey) ([]string, error) { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 439 | panic("unimplemented") |
| 440 | } |
| 441 | |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 442 | func (n noopBazelContext) GetCcInfo(_ string, _ configKey) (cquery.CcInfo, error) { |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 443 | panic("unimplemented") |
| 444 | } |
| 445 | |
Liz Kammer | be6a712 | 2022-11-04 16:05:11 -0400 | [diff] [blame] | 446 | func (n noopBazelContext) GetApexInfo(_ string, _ configKey) (cquery.ApexInfo, error) { |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 447 | panic("unimplemented") |
| 448 | } |
| 449 | |
Sasha Smundak | edd1666 | 2022-10-07 14:44:50 -0700 | [diff] [blame] | 450 | func (n noopBazelContext) GetCcUnstrippedInfo(_ string, _ configKey) (cquery.CcUnstrippedInfo, error) { |
| 451 | //TODO implement me |
| 452 | panic("implement me") |
| 453 | } |
| 454 | |
Spandan Das | bd15681 | 2023-06-05 22:43:13 +0000 | [diff] [blame] | 455 | func (n noopBazelContext) GetPrebuiltFileInfo(_ string, _ configKey) (cquery.PrebuiltFileInfo, error) { |
| 456 | panic("implement me") |
| 457 | } |
| 458 | |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 459 | func (n noopBazelContext) InvokeBazel(_ Config, _ invokeBazelContext) error { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 460 | panic("unimplemented") |
| 461 | } |
| 462 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 463 | func (m noopBazelContext) OutputBase() string { |
| 464 | return "" |
| 465 | } |
| 466 | |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 467 | func (n noopBazelContext) IsModuleNameAllowed(_ string, _ bool) bool { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 468 | return false |
| 469 | } |
| 470 | |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 471 | func (m noopBazelContext) BuildStatementsToRegister() []*bazel.BuildStatement { |
| 472 | return []*bazel.BuildStatement{} |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 473 | } |
| 474 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 475 | func (m noopBazelContext) AqueryDepsets() []bazel.AqueryDepset { |
| 476 | return []bazel.AqueryDepset{} |
| 477 | } |
| 478 | |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 479 | func AddToStringSet(set map[string]bool, items []string) { |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 480 | for _, item := range items { |
| 481 | set[item] = true |
| 482 | } |
| 483 | } |
| 484 | |
Cole Faust | 705968d | 2022-12-14 11:32:05 -0800 | [diff] [blame] | 485 | func GetBazelEnabledAndDisabledModules(buildMode SoongBuildMode, forceEnabled map[string]struct{}) (map[string]bool, map[string]bool) { |
Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 486 | disabledModules := map[string]bool{} |
| 487 | enabledModules := map[string]bool{} |
| 488 | |
Cole Faust | 705968d | 2022-12-14 11:32:05 -0800 | [diff] [blame] | 489 | switch buildMode { |
Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 490 | case BazelProdMode: |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 491 | AddToStringSet(enabledModules, allowlists.ProdMixedBuildsEnabledList) |
Cole Faust | 705968d | 2022-12-14 11:32:05 -0800 | [diff] [blame] | 492 | for enabledAdHocModule := range forceEnabled { |
MarkDacek | d06db5d | 2022-11-29 00:47:59 +0000 | [diff] [blame] | 493 | enabledModules[enabledAdHocModule] = true |
| 494 | } |
MarkDacek | b78465d | 2022-10-18 20:10:16 +0000 | [diff] [blame] | 495 | case BazelStagingMode: |
Chris Parsons | 66fc745 | 2022-11-04 13:26:17 -0400 | [diff] [blame] | 496 | // Staging mode includes all prod modules plus all staging modules. |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 497 | AddToStringSet(enabledModules, allowlists.ProdMixedBuildsEnabledList) |
| 498 | AddToStringSet(enabledModules, allowlists.StagingMixedBuildsEnabledList) |
Cole Faust | 705968d | 2022-12-14 11:32:05 -0800 | [diff] [blame] | 499 | for enabledAdHocModule := range forceEnabled { |
MarkDacek | d06db5d | 2022-11-29 00:47:59 +0000 | [diff] [blame] | 500 | enabledModules[enabledAdHocModule] = true |
| 501 | } |
Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 502 | default: |
Chris Parsons | 21f8027 | 2023-06-15 04:02:28 +0000 | [diff] [blame] | 503 | panic("Expected BazelProdMode or BazelStagingMode") |
Cole Faust | 705968d | 2022-12-14 11:32:05 -0800 | [diff] [blame] | 504 | } |
| 505 | return enabledModules, disabledModules |
| 506 | } |
| 507 | |
| 508 | func GetBazelEnabledModules(buildMode SoongBuildMode) []string { |
| 509 | enabledModules, disabledModules := GetBazelEnabledAndDisabledModules(buildMode, nil) |
| 510 | enabledList := make([]string, 0, len(enabledModules)) |
| 511 | for module := range enabledModules { |
| 512 | if !disabledModules[module] { |
| 513 | enabledList = append(enabledList, module) |
| 514 | } |
| 515 | } |
| 516 | sort.Strings(enabledList) |
| 517 | return enabledList |
| 518 | } |
| 519 | |
| 520 | func NewBazelContext(c *config) (BazelContext, error) { |
Chris Parsons | 21f8027 | 2023-06-15 04:02:28 +0000 | [diff] [blame] | 521 | if c.BuildMode != BazelProdMode && c.BuildMode != BazelStagingMode { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 522 | return noopBazelContext{}, nil |
| 523 | } |
| 524 | |
Cole Faust | 705968d | 2022-12-14 11:32:05 -0800 | [diff] [blame] | 525 | enabledModules, disabledModules := GetBazelEnabledAndDisabledModules(c.BuildMode, c.BazelModulesForceEnabledByFlag()) |
| 526 | |
Sasha Smundak | dc87f2d | 2022-12-06 20:27:54 -0800 | [diff] [blame] | 527 | paths := bazelPaths{ |
| 528 | soongOutDir: c.soongOutDir, |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 529 | } |
Sasha Smundak | dc87f2d | 2022-12-06 20:27:54 -0800 | [diff] [blame] | 530 | var missing []string |
| 531 | vars := []struct { |
| 532 | name string |
| 533 | ptr *string |
Paul Duffin | 184366a | 2022-12-21 15:55:33 +0000 | [diff] [blame] | 534 | |
| 535 | // True if the environment variable needs to be tracked so that changes to the variable |
| 536 | // cause the ninja file to be regenerated, false otherwise. False should only be set for |
| 537 | // environment variables that have no effect on the generated ninja file. |
| 538 | track bool |
Sasha Smundak | dc87f2d | 2022-12-06 20:27:54 -0800 | [diff] [blame] | 539 | }{ |
Paul Duffin | 184366a | 2022-12-21 15:55:33 +0000 | [diff] [blame] | 540 | {"BAZEL_HOME", &paths.homeDir, true}, |
| 541 | {"BAZEL_PATH", &paths.bazelPath, true}, |
| 542 | {"BAZEL_OUTPUT_BASE", &paths.outputBase, true}, |
| 543 | {"BAZEL_WORKSPACE", &paths.workspaceDir, true}, |
| 544 | {"BAZEL_METRICS_DIR", &paths.metricsDir, false}, |
| 545 | {"BAZEL_DEPS_FILE", &paths.bazelDepsFile, true}, |
Sasha Smundak | dc87f2d | 2022-12-06 20:27:54 -0800 | [diff] [blame] | 546 | } |
| 547 | for _, v := range vars { |
Paul Duffin | 184366a | 2022-12-21 15:55:33 +0000 | [diff] [blame] | 548 | if v.track { |
| 549 | if s := c.Getenv(v.name); len(s) > 1 { |
| 550 | *v.ptr = s |
| 551 | continue |
| 552 | } |
| 553 | } else if s, ok := c.env[v.name]; ok { |
Sasha Smundak | dc87f2d | 2022-12-06 20:27:54 -0800 | [diff] [blame] | 554 | *v.ptr = s |
| 555 | } else { |
| 556 | missing = append(missing, v.name) |
| 557 | } |
| 558 | } |
| 559 | if len(missing) > 0 { |
| 560 | return nil, fmt.Errorf("missing required env vars to use bazel: %s", missing) |
| 561 | } |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 562 | |
| 563 | targetBuildVariant := "user" |
| 564 | if c.Eng() { |
| 565 | targetBuildVariant = "eng" |
| 566 | } else if c.Debuggable() { |
| 567 | targetBuildVariant = "userdebug" |
| 568 | } |
| 569 | targetProduct := "unknown" |
| 570 | if c.HasDeviceProduct() { |
| 571 | targetProduct = c.DeviceProduct() |
| 572 | } |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 573 | dclaMixedBuildsEnabledList := []string{} |
| 574 | if c.BuildMode == BazelProdMode { |
| 575 | dclaMixedBuildsEnabledList = allowlists.ProdDclaMixedBuildsEnabledList |
| 576 | } else if c.BuildMode == BazelStagingMode { |
| 577 | dclaMixedBuildsEnabledList = append(allowlists.ProdDclaMixedBuildsEnabledList, |
| 578 | allowlists.StagingDclaMixedBuildsEnabledList...) |
| 579 | } |
| 580 | dclaEnabledModules := map[string]bool{} |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 581 | AddToStringSet(dclaEnabledModules, dclaMixedBuildsEnabledList) |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 582 | return &mixedBuildBazelContext{ |
Chris Parsons | 9402ca8 | 2023-02-23 17:28:06 -0500 | [diff] [blame] | 583 | bazelRunner: &builtinBazelRunner{c.UseBazelProxy, absolutePath(c.outDir)}, |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 584 | paths: &paths, |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 585 | bazelEnabledModules: enabledModules, |
| 586 | bazelDisabledModules: disabledModules, |
| 587 | bazelDclaEnabledModules: dclaEnabledModules, |
| 588 | targetProduct: targetProduct, |
| 589 | targetBuildVariant: targetBuildVariant, |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 590 | }, nil |
| 591 | } |
| 592 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 593 | func (p *bazelPaths) BazelMetricsDir() string { |
| 594 | return p.metricsDir |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 597 | func (context *mixedBuildBazelContext) IsModuleNameAllowed(moduleName string, withinApex bool) bool { |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 598 | if context.bazelDisabledModules[moduleName] { |
| 599 | return false |
| 600 | } |
| 601 | if context.bazelEnabledModules[moduleName] { |
| 602 | return true |
| 603 | } |
Spandan Das | 95b24b1 | 2023-06-26 22:39:19 +0000 | [diff] [blame] | 604 | if withinApex && context.bazelDclaEnabledModules[moduleName] { |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 605 | return true |
| 606 | } |
| 607 | |
Chris Parsons | 21f8027 | 2023-06-15 04:02:28 +0000 | [diff] [blame] | 608 | return false |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 609 | } |
| 610 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 611 | func pwdPrefix() string { |
| 612 | // Darwin doesn't have /proc |
| 613 | if runtime.GOOS != "darwin" { |
| 614 | return "PWD=/proc/self/cwd" |
| 615 | } |
| 616 | return "" |
| 617 | } |
| 618 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 619 | type bazelCommand struct { |
| 620 | command string |
| 621 | // query or label |
| 622 | expression string |
| 623 | } |
| 624 | |
Chris Parsons | 9402ca8 | 2023-02-23 17:28:06 -0500 | [diff] [blame] | 625 | type builtinBazelRunner struct { |
| 626 | useBazelProxy bool |
| 627 | outDir string |
| 628 | } |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 629 | |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 630 | // Issues the given bazel command with given build label and additional flags. |
| 631 | // Returns (stdout, stderr, error). The first and second return values are strings |
| 632 | // containing the stdout and stderr of the run command, and an error is returned if |
| 633 | // the invocation returned an error code. |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 634 | func (r *builtinBazelRunner) issueBazelCommand(cmdRequest bazel.CmdRequest, paths *bazelPaths, eventHandler *metrics.EventHandler) (string, string, error) { |
Chris Parsons | 9402ca8 | 2023-02-23 17:28:06 -0500 | [diff] [blame] | 635 | if r.useBazelProxy { |
| 636 | eventHandler.Begin("client_proxy") |
| 637 | defer eventHandler.End("client_proxy") |
| 638 | proxyClient := bazel.NewProxyClient(r.outDir) |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 639 | resp, err := proxyClient.IssueCommand(cmdRequest) |
Chris Parsons | 9402ca8 | 2023-02-23 17:28:06 -0500 | [diff] [blame] | 640 | |
| 641 | if err != nil { |
| 642 | return "", "", err |
| 643 | } |
| 644 | if len(resp.ErrorString) > 0 { |
| 645 | return "", "", fmt.Errorf(resp.ErrorString) |
| 646 | } |
| 647 | return resp.Stdout, resp.Stderr, nil |
Jason Wu | 52cd194 | 2022-09-08 15:37:57 +0000 | [diff] [blame] | 648 | } else { |
Chris Parsons | 9402ca8 | 2023-02-23 17:28:06 -0500 | [diff] [blame] | 649 | eventHandler.Begin("bazel command") |
| 650 | defer eventHandler.End("bazel command") |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 651 | |
| 652 | stdout, stderr, err := bazel.ExecBazel(paths.bazelPath, absolutePath(paths.syntheticWorkspaceDir()), cmdRequest) |
| 653 | return string(stdout), string(stderr), err |
Jason Wu | 52cd194 | 2022-09-08 15:37:57 +0000 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 657 | func (context *mixedBuildBazelContext) createBazelCommand(config Config, runName bazel.RunName, command bazelCommand, |
| 658 | extraFlags ...string) bazel.CmdRequest { |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 659 | if runtime.GOOS != "linux" && runtime.GOOS != "darwin" { |
| 660 | panic("Unknown GOOS: " + runtime.GOOS) |
| 661 | } |
Romain Jobredeaux | 41fd5e4 | 2021-08-27 15:59:39 +0000 | [diff] [blame] | 662 | cmdFlags := []string{ |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 663 | "--output_base=" + absolutePath(context.paths.outputBase), |
Romain Jobredeaux | 41fd5e4 | 2021-08-27 15:59:39 +0000 | [diff] [blame] | 664 | command.command, |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 665 | command.expression, |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 666 | "--profile=" + shared.BazelMetricsFilename(context.paths, runName), |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 667 | |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 668 | "--host_platform=@soong_injection//product_config_platforms:mixed_builds_product-" + context.targetBuildVariant + "_" + runtime.GOOS + "_x86_64", |
| 669 | // Don't specify --platforms, because on some products/branches (like kernel-build-tools) |
| 670 | // the main platform for mixed_builds_product-variant doesn't exist because an arch isn't |
| 671 | // specified in product config. The derivative platforms that config_node transitions into |
| 672 | // will still work. |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 673 | |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 674 | // Suppress noise |
| 675 | "--ui_event_filters=-INFO", |
Sam Delmerico | 658a4da | 2022-11-07 15:53:38 -0500 | [diff] [blame] | 676 | "--noshow_progress", |
| 677 | "--norun_validations", |
| 678 | } |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 679 | cmdFlags = append(cmdFlags, extraFlags...) |
| 680 | |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 681 | extraEnv := []string{ |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 682 | "HOME=" + context.paths.homeDir, |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 683 | pwdPrefix(), |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 684 | "BUILD_DIR=" + absolutePath(context.paths.soongOutDir), |
Joe Onorato | ba29f38 | 2022-10-24 06:38:11 -0700 | [diff] [blame] | 685 | // Make OUT_DIR absolute here so build/bazel/bin/bazel uses the correct |
Jingwen Chen | 8c52358 | 2021-06-01 11:19:53 +0000 | [diff] [blame] | 686 | // OUT_DIR at <root>/out, instead of <root>/out/soong/workspace/out. |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 687 | "OUT_DIR=" + absolutePath(context.paths.outDir()), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 688 | // Disables local host detection of gcc; toolchain information is defined |
| 689 | // explicitly in BUILD files. |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 690 | "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1", |
| 691 | } |
Cole Faust | 8a161be | 2023-06-14 15:45:12 -0700 | [diff] [blame] | 692 | capturedEnvVars, err := starlark_import.GetStarlarkValue[[]string]("captured_env_vars") |
| 693 | if err != nil { |
| 694 | panic(err) |
| 695 | } |
| 696 | for _, envvar := range capturedEnvVars { |
Sam Delmerico | cb3c52c | 2023-02-03 17:40:08 -0500 | [diff] [blame] | 697 | val := config.Getenv(envvar) |
| 698 | if val == "" { |
| 699 | continue |
| 700 | } |
| 701 | extraEnv = append(extraEnv, fmt.Sprintf("%s=%s", envvar, val)) |
| 702 | } |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 703 | envVars := append(os.Environ(), extraEnv...) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 704 | |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 705 | return bazel.CmdRequest{cmdFlags, envVars} |
Jason Wu | 52cd194 | 2022-09-08 15:37:57 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 708 | func (context *mixedBuildBazelContext) printableCqueryCommand(bazelCmd bazel.CmdRequest) string { |
| 709 | args := append([]string{context.paths.bazelPath}, bazelCmd.Argv...) |
| 710 | outputString := strings.Join(bazelCmd.Env, " ") + " \"" + strings.Join(args, "\" \"") + "\"" |
Jason Wu | 52cd194 | 2022-09-08 15:37:57 +0000 | [diff] [blame] | 711 | return outputString |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 712 | } |
| 713 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 714 | func (context *mixedBuildBazelContext) mainBzlFileContents() []byte { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 715 | // TODO(cparsons): Define configuration transitions programmatically based |
| 716 | // on available archs. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 717 | contents := ` |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 718 | ##################################################### |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 719 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 720 | ##################################################### |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 721 | def _config_node_transition_impl(settings, attr): |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 722 | if attr.os == "android" and attr.arch == "target": |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 723 | target = "mixed_builds_product-{VARIANT}" |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 724 | else: |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 725 | target = "mixed_builds_product-{VARIANT}_%s_%s" % (attr.os, attr.arch) |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 726 | apex_name = "" |
| 727 | if attr.within_apex: |
| 728 | # //build/bazel/rules/apex:apex_name has to be set to a non_empty value, |
| 729 | # otherwise //build/bazel/rules/apex:non_apex will be true and the |
| 730 | # "-D__ANDROID_APEX__" compiler flag will be missing. Apex_name is used |
| 731 | # in some validation on bazel side which don't really apply in mixed |
| 732 | # build because soong will do the work, so we just set it to a fixed |
| 733 | # value here. |
| 734 | apex_name = "dcla_apex" |
| 735 | outputs = { |
Jingwen Chen | 583ab21 | 2023-05-30 09:45:23 +0000 | [diff] [blame] | 736 | "//command_line_option:platforms": "@soong_injection//product_config_platforms:%s" % target, |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 737 | "@//build/bazel/rules/apex:within_apex": attr.within_apex, |
| 738 | "@//build/bazel/rules/apex:min_sdk_version": attr.apex_sdk_version, |
| 739 | "@//build/bazel/rules/apex:apex_name": apex_name, |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 740 | "@//build/bazel/rules/apex:api_domain": attr.api_domain, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 741 | } |
| 742 | |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 743 | return outputs |
| 744 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 745 | _config_node_transition = transition( |
| 746 | implementation = _config_node_transition_impl, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 747 | inputs = [], |
| 748 | outputs = [ |
| 749 | "//command_line_option:platforms", |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 750 | "@//build/bazel/rules/apex:within_apex", |
| 751 | "@//build/bazel/rules/apex:min_sdk_version", |
| 752 | "@//build/bazel/rules/apex:apex_name", |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 753 | "@//build/bazel/rules/apex:api_domain", |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 754 | ], |
| 755 | ) |
| 756 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 757 | def _passthrough_rule_impl(ctx): |
| 758 | return [DefaultInfo(files = depset(ctx.files.deps))] |
| 759 | |
| 760 | config_node = rule( |
| 761 | implementation = _passthrough_rule_impl, |
| 762 | attrs = { |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 763 | "arch" : attr.string(mandatory = True), |
| 764 | "os" : attr.string(mandatory = True), |
| 765 | "within_apex" : attr.bool(default = False), |
| 766 | "apex_sdk_version" : attr.string(mandatory = True), |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 767 | "api_domain" : attr.string(mandatory = True), |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 768 | "deps" : attr.label_list(cfg = _config_node_transition, allow_files = True), |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 769 | "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"), |
| 770 | }, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 771 | ) |
| 772 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 773 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 774 | # Rule representing the root of the build, to depend on all Bazel targets that |
| 775 | # are required for the build. Building this target will build the entire Bazel |
| 776 | # build tree. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 777 | mixed_build_root = rule( |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 778 | implementation = _passthrough_rule_impl, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 779 | attrs = { |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 780 | "deps" : attr.label_list(), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 781 | }, |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 782 | ) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 783 | |
| 784 | def _phony_root_impl(ctx): |
| 785 | return [] |
| 786 | |
| 787 | # Rule to depend on other targets but build nothing. |
| 788 | # This is useful as follows: building a target of this rule will generate |
| 789 | # symlink forests for all dependencies of the target, without executing any |
| 790 | # actions of the build. |
| 791 | phony_root = rule( |
| 792 | implementation = _phony_root_impl, |
| 793 | attrs = {"deps" : attr.label_list()}, |
| 794 | ) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 795 | ` |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 796 | |
| 797 | productReplacer := strings.NewReplacer( |
| 798 | "{PRODUCT}", context.targetProduct, |
| 799 | "{VARIANT}", context.targetBuildVariant) |
| 800 | |
| 801 | return []byte(productReplacer.Replace(contents)) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 802 | } |
| 803 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 804 | func (context *mixedBuildBazelContext) mainBuildFileContents() []byte { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 805 | // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded |
| 806 | // architecture mapping. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 807 | formatString := ` |
| 808 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 809 | load(":main.bzl", "config_node", "mixed_build_root", "phony_root") |
| 810 | |
| 811 | %s |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 812 | |
| 813 | mixed_build_root(name = "buildroot", |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 814 | deps = [%s], |
Jingwen Chen | 3952a90 | 2022-12-12 12:20:58 +0000 | [diff] [blame] | 815 | testonly = True, # Unblocks testonly deps. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 816 | ) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 817 | |
| 818 | phony_root(name = "phonyroot", |
| 819 | deps = [":buildroot"], |
Jingwen Chen | 3952a90 | 2022-12-12 12:20:58 +0000 | [diff] [blame] | 820 | testonly = True, # Unblocks testonly deps. |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 821 | ) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 822 | ` |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 823 | configNodeFormatString := ` |
| 824 | config_node(name = "%s", |
| 825 | arch = "%s", |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 826 | os = "%s", |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 827 | within_apex = %s, |
| 828 | apex_sdk_version = "%s", |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 829 | api_domain = "%s", |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 830 | deps = [%s], |
Jingwen Chen | 3952a90 | 2022-12-12 12:20:58 +0000 | [diff] [blame] | 831 | testonly = True, # Unblocks testonly deps. |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 832 | ) |
| 833 | ` |
| 834 | |
| 835 | configNodesSection := "" |
| 836 | |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 837 | labelsByConfig := map[string][]string{} |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 838 | |
| 839 | for _, val := range context.requests { |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 840 | labelString := fmt.Sprintf("\"@%s\"", val.label) |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 841 | configString := getConfigString(val) |
| 842 | labelsByConfig[configString] = append(labelsByConfig[configString], labelString) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 843 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 844 | |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 845 | // Configs need to be sorted to maintain determinism of the BUILD file. |
| 846 | sortedConfigs := make([]string, 0, len(labelsByConfig)) |
| 847 | for val := range labelsByConfig { |
| 848 | sortedConfigs = append(sortedConfigs, val) |
| 849 | } |
| 850 | sort.Slice(sortedConfigs, func(i, j int) bool { return sortedConfigs[i] < sortedConfigs[j] }) |
| 851 | |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 852 | allLabels := []string{} |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 853 | for _, configString := range sortedConfigs { |
| 854 | labels := labelsByConfig[configString] |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 855 | configTokens := strings.Split(configString, "|") |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 856 | if len(configTokens) < 2 { |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 857 | panic(fmt.Errorf("Unexpected config string format: %s", configString)) |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 858 | } |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 859 | archString := configTokens[0] |
| 860 | osString := configTokens[1] |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 861 | withinApex := "False" |
| 862 | apexSdkVerString := "" |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 863 | apiDomainString := "" |
| 864 | if osString == "android" { |
| 865 | // api domains are meaningful only for device variants |
| 866 | apiDomainString = "system" |
| 867 | } |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 868 | targetString := fmt.Sprintf("%s_%s", osString, archString) |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 869 | if len(configTokens) > 2 { |
| 870 | targetString += "_" + configTokens[2] |
| 871 | if configTokens[2] == withinApexToString(true) { |
| 872 | withinApex = "True" |
| 873 | } |
| 874 | } |
| 875 | if len(configTokens) > 3 { |
| 876 | targetString += "_" + configTokens[3] |
| 877 | apexSdkVerString = configTokens[3] |
| 878 | } |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 879 | if len(configTokens) > 4 { |
| 880 | apiDomainString = configTokens[4] |
| 881 | targetString += "_" + apiDomainString |
| 882 | } |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 883 | allLabels = append(allLabels, fmt.Sprintf("\":%s\"", targetString)) |
| 884 | labelsString := strings.Join(labels, ",\n ") |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 885 | configNodesSection += fmt.Sprintf(configNodeFormatString, targetString, archString, osString, withinApex, apexSdkVerString, apiDomainString, |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 886 | labelsString) |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 887 | } |
| 888 | |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 889 | return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(allLabels, ",\n "))) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 890 | } |
| 891 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 892 | func indent(original string) string { |
| 893 | result := "" |
| 894 | for _, line := range strings.Split(original, "\n") { |
| 895 | result += " " + line + "\n" |
| 896 | } |
| 897 | return result |
| 898 | } |
| 899 | |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 900 | // Returns the file contents of the buildroot.cquery file that should be used for the cquery |
| 901 | // expression in order to obtain information about buildroot and its dependencies. |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 902 | // The contents of this file depend on the mixedBuildBazelContext's requests; requests are enumerated |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 903 | // and grouped by their request type. The data retrieved for each label depends on its |
| 904 | // request type. |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 905 | func (context *mixedBuildBazelContext) cqueryStarlarkFileContents() []byte { |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 906 | requestTypeToCqueryIdEntries := map[cqueryRequest][]string{} |
Chris Parsons | 38851d8 | 2023-03-15 00:19:32 -0400 | [diff] [blame] | 907 | requestTypes := []cqueryRequest{} |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 908 | for _, val := range context.requests { |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 909 | cqueryId := getCqueryId(val) |
| 910 | mapEntryString := fmt.Sprintf("%q : True", cqueryId) |
Chris Parsons | 38851d8 | 2023-03-15 00:19:32 -0400 | [diff] [blame] | 911 | if _, seenKey := requestTypeToCqueryIdEntries[val.requestType]; !seenKey { |
| 912 | requestTypes = append(requestTypes, val.requestType) |
| 913 | } |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 914 | requestTypeToCqueryIdEntries[val.requestType] = |
| 915 | append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString) |
| 916 | } |
| 917 | labelRegistrationMapSection := "" |
| 918 | functionDefSection := "" |
| 919 | mainSwitchSection := "" |
| 920 | |
| 921 | mapDeclarationFormatString := ` |
| 922 | %s = { |
| 923 | %s |
| 924 | } |
| 925 | ` |
| 926 | functionDefFormatString := ` |
Cole Faust | 97d1527 | 2022-11-22 14:08:59 -0800 | [diff] [blame] | 927 | def %s(target, id_string): |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 928 | %s |
| 929 | ` |
| 930 | mainSwitchSectionFormatString := ` |
| 931 | if id_string in %s: |
Cole Faust | 97d1527 | 2022-11-22 14:08:59 -0800 | [diff] [blame] | 932 | return id_string + ">>" + %s(target, id_string) |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 933 | ` |
| 934 | |
Chris Parsons | 38851d8 | 2023-03-15 00:19:32 -0400 | [diff] [blame] | 935 | for _, requestType := range requestTypes { |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 936 | labelMapName := requestType.Name() + "_Labels" |
| 937 | functionName := requestType.Name() + "_Fn" |
| 938 | labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString, |
| 939 | labelMapName, |
| 940 | strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n ")) |
| 941 | functionDefSection += fmt.Sprintf(functionDefFormatString, |
| 942 | functionName, |
| 943 | indent(requestType.StarlarkFunctionBody())) |
| 944 | mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString, |
| 945 | labelMapName, functionName) |
| 946 | } |
| 947 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 948 | formatString := ` |
| 949 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 950 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 951 | {LABEL_REGISTRATION_MAP_SECTION} |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 952 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 953 | {FUNCTION_DEF_SECTION} |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 954 | |
| 955 | def get_arch(target): |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 956 | # TODO(b/199363072): filegroups and file targets aren't associated with any |
| 957 | # specific platform architecture in mixed builds. This is consistent with how |
| 958 | # Soong treats filegroups, but it may not be the case with manually-written |
| 959 | # filegroup BUILD targets. |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 960 | buildoptions = build_options(target) |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 961 | |
Jingwen Chen | 8f22274 | 2021-10-07 12:02:23 +0000 | [diff] [blame] | 962 | if buildoptions == None: |
| 963 | # File targets do not have buildoptions. File targets aren't associated with |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 964 | # any specific platform architecture in mixed builds, so use the host. |
| 965 | return "x86_64|linux" |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 966 | platforms = buildoptions["//command_line_option:platforms"] |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 967 | if len(platforms) != 1: |
| 968 | # An individual configured target should have only one platform architecture. |
| 969 | # Note that it's fine for there to be multiple architectures for the same label, |
| 970 | # but each is its own configured target. |
| 971 | fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms)) |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 972 | platform_name = platforms[0].name |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 973 | if platform_name == "host": |
| 974 | return "HOST" |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 975 | if not platform_name.startswith("mixed_builds_product-{TARGET_BUILD_VARIANT}"): |
| 976 | 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)) |
| 977 | platform_name = platform_name.removeprefix("mixed_builds_product-{TARGET_BUILD_VARIANT}").removeprefix("_") |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 978 | config_key = "" |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 979 | if not platform_name: |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 980 | config_key = "target|android" |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 981 | elif platform_name.startswith("android_"): |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 982 | config_key = platform_name.removeprefix("android_") + "|android" |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 983 | elif platform_name.startswith("linux_"): |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 984 | config_key = platform_name.removeprefix("linux_") + "|linux" |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 985 | else: |
Cole Faust | 319abae | 2023-06-06 15:12:49 -0700 | [diff] [blame] | 986 | 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 Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 987 | |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 988 | within_apex = buildoptions.get("//build/bazel/rules/apex:within_apex") |
| 989 | apex_sdk_version = buildoptions.get("//build/bazel/rules/apex:min_sdk_version") |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 990 | api_domain = buildoptions.get("//build/bazel/rules/apex:api_domain") |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 991 | |
| 992 | if within_apex: |
| 993 | config_key += "|within_apex" |
| 994 | if apex_sdk_version != None and len(apex_sdk_version) > 0: |
| 995 | config_key += "|" + apex_sdk_version |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 996 | if api_domain != None and len(api_domain) > 0: |
| 997 | config_key += "|" + api_domain |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 998 | |
| 999 | return config_key |
| 1000 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 1001 | def format(target): |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 1002 | id_string = str(target.label) + "|" + get_arch(target) |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 1003 | |
Chris Parsons | 86dc2c2 | 2022-09-28 14:58:41 -0400 | [diff] [blame] | 1004 | # TODO(b/248106697): Remove once Bazel is updated to always normalize labels. |
| 1005 | if id_string.startswith("//"): |
| 1006 | id_string = "@" + id_string |
| 1007 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1008 | {MAIN_SWITCH_SECTION} |
| 1009 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 1010 | # This target was not requested via cquery, and thus must be a dependency |
| 1011 | # of a requested target. |
| 1012 | return id_string + ">>NONE" |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 1013 | ` |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1014 | replacer := strings.NewReplacer( |
| 1015 | "{TARGET_PRODUCT}", context.targetProduct, |
| 1016 | "{TARGET_BUILD_VARIANT}", context.targetBuildVariant, |
| 1017 | "{LABEL_REGISTRATION_MAP_SECTION}", labelRegistrationMapSection, |
| 1018 | "{FUNCTION_DEF_SECTION}", functionDefSection, |
| 1019 | "{MAIN_SWITCH_SECTION}", mainSwitchSection) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 1020 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1021 | return []byte(replacer.Replace(formatString)) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 1022 | } |
| 1023 | |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 1024 | // Returns a path containing build-related metadata required for interfacing |
| 1025 | // with Bazel. Example: out/soong/bazel. |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 1026 | func (p *bazelPaths) intermediatesDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1027 | return filepath.Join(p.soongOutDir, "bazel") |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 1028 | } |
| 1029 | |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 1030 | // Returns the path where the contents of the @soong_injection repository live. |
| 1031 | // It is used by Soong to tell Bazel things it cannot over the command line. |
| 1032 | func (p *bazelPaths) injectedFilesDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1033 | return filepath.Join(p.soongOutDir, bazel.SoongInjectionDirName) |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | // Returns the path of the synthetic Bazel workspace that contains a symlink |
| 1037 | // forest composed the whole source tree and BUILD files generated by bp2build. |
| 1038 | func (p *bazelPaths) syntheticWorkspaceDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1039 | return filepath.Join(p.soongOutDir, "workspace") |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
Jingwen Chen | 8c52358 | 2021-06-01 11:19:53 +0000 | [diff] [blame] | 1042 | // Returns the path to the top level out dir ($OUT_DIR). |
| 1043 | func (p *bazelPaths) outDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1044 | return filepath.Dir(p.soongOutDir) |
Jingwen Chen | 8c52358 | 2021-06-01 11:19:53 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Sasha Smundak | 4975c82 | 2022-11-16 15:28:18 -0800 | [diff] [blame] | 1047 | const buildrootLabel = "@soong_injection//mixed_builds:buildroot" |
| 1048 | |
| 1049 | var ( |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 1050 | cqueryCmd = bazelCommand{"cquery", fmt.Sprintf("deps(%s, 2)", buildrootLabel)} |
| 1051 | aqueryCmd = bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)} |
| 1052 | buildCmd = bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"} |
| 1053 | allBazelCommands = []bazelCommand{aqueryCmd, cqueryCmd, buildCmd} |
Sasha Smundak | 4975c82 | 2022-11-16 15:28:18 -0800 | [diff] [blame] | 1054 | ) |
| 1055 | |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1056 | func GetBazelSandwichCqueryRequests(config Config) ([]cqueryKey, error) { |
| 1057 | result := make([]cqueryKey, 0, len(allowlists.BazelSandwichTargets)) |
Cole Faust | 16d1094 | 2023-08-02 11:45:43 -0700 | [diff] [blame] | 1058 | labelRegex := regexp.MustCompile("^@?//([a-zA-Z0-9/_-]+):[a-zA-Z0-9_-]+$") |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1059 | // Note that bazel "targets" are different from soong "targets", the bazel targets are |
| 1060 | // synonymous with soong modules, and soong targets are a configuration a module is built in. |
| 1061 | for _, target := range allowlists.BazelSandwichTargets { |
Cole Faust | 16d1094 | 2023-08-02 11:45:43 -0700 | [diff] [blame] | 1062 | match := labelRegex.FindStringSubmatch(target.Label) |
| 1063 | if match == nil { |
| 1064 | return nil, fmt.Errorf("invalid label, must match `^@?//([a-zA-Z0-9/_-]+):[a-zA-Z0-9_-]+$`: %s", target.Label) |
| 1065 | } |
| 1066 | if _, err := os.Stat(absolutePath(match[1])); err != nil { |
| 1067 | if os.IsNotExist(err) { |
| 1068 | // Ignore bazel sandwich targets that don't exist. |
| 1069 | continue |
| 1070 | } else { |
| 1071 | return nil, err |
| 1072 | } |
| 1073 | } |
| 1074 | |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1075 | var soongTarget Target |
| 1076 | if target.Host { |
| 1077 | soongTarget = config.BuildOSTarget |
| 1078 | } else { |
| 1079 | soongTarget = config.AndroidCommonTarget |
| 1080 | if soongTarget.Os.Class != Device { |
| 1081 | // kernel-build-tools seems to set the AndroidCommonTarget to a linux host |
| 1082 | // target for some reason, disable device builds in that case. |
| 1083 | continue |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | result = append(result, cqueryKey{ |
| 1088 | label: target.Label, |
| 1089 | requestType: cquery.GetOutputFiles, |
| 1090 | configKey: configKey{ |
| 1091 | arch: soongTarget.Arch.String(), |
| 1092 | osType: soongTarget.Os, |
| 1093 | }, |
| 1094 | }) |
| 1095 | } |
| 1096 | return result, nil |
| 1097 | } |
| 1098 | |
| 1099 | // QueueBazelSandwichCqueryRequests queues cquery requests for all the bazel labels in |
| 1100 | // bazel_sandwich_targets. These will later be given phony targets so that they can be built on the |
| 1101 | // command line. |
| 1102 | func (context *mixedBuildBazelContext) QueueBazelSandwichCqueryRequests(config Config) error { |
| 1103 | requests, err := GetBazelSandwichCqueryRequests(config) |
| 1104 | if err != nil { |
| 1105 | return err |
| 1106 | } |
| 1107 | for _, request := range requests { |
| 1108 | context.QueueBazelRequest(request.label, request.requestType, request.configKey) |
| 1109 | } |
| 1110 | |
| 1111 | return nil |
| 1112 | } |
| 1113 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 1114 | // Issues commands to Bazel to receive results for all cquery requests |
| 1115 | // queued in the BazelContext. |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 1116 | func (context *mixedBuildBazelContext) InvokeBazel(config Config, ctx invokeBazelContext) error { |
| 1117 | eventHandler := ctx.GetEventHandler() |
| 1118 | eventHandler.Begin("bazel") |
| 1119 | defer eventHandler.End("bazel") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 1120 | |
Sasha Smundak | 4975c82 | 2022-11-16 15:28:18 -0800 | [diff] [blame] | 1121 | if metricsDir := context.paths.BazelMetricsDir(); metricsDir != "" { |
| 1122 | if err := os.MkdirAll(metricsDir, 0777); err != nil { |
| 1123 | return err |
| 1124 | } |
| 1125 | } |
| 1126 | context.results = make(map[cqueryKey]string) |
Sam Delmerico | cb3c52c | 2023-02-03 17:40:08 -0500 | [diff] [blame] | 1127 | if err := context.runCquery(config, ctx); err != nil { |
Sasha Smundak | 4975c82 | 2022-11-16 15:28:18 -0800 | [diff] [blame] | 1128 | return err |
| 1129 | } |
| 1130 | if err := context.runAquery(config, ctx); err != nil { |
| 1131 | return err |
| 1132 | } |
Sam Delmerico | cb3c52c | 2023-02-03 17:40:08 -0500 | [diff] [blame] | 1133 | if err := context.generateBazelSymlinks(config, ctx); err != nil { |
Sasha Smundak | 4975c82 | 2022-11-16 15:28:18 -0800 | [diff] [blame] | 1134 | return err |
| 1135 | } |
| 1136 | |
| 1137 | // Clear requests. |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 1138 | context.requests = []cqueryKey{} |
Sasha Smundak | 4975c82 | 2022-11-16 15:28:18 -0800 | [diff] [blame] | 1139 | return nil |
| 1140 | } |
| 1141 | |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 1142 | func (context *mixedBuildBazelContext) runCquery(config Config, ctx invokeBazelContext) error { |
| 1143 | eventHandler := ctx.GetEventHandler() |
| 1144 | eventHandler.Begin("cquery") |
| 1145 | defer eventHandler.End("cquery") |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 1146 | soongInjectionPath := absolutePath(context.paths.injectedFilesDir()) |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 1147 | mixedBuildsPath := filepath.Join(soongInjectionPath, "mixed_builds") |
| 1148 | if _, err := os.Stat(mixedBuildsPath); os.IsNotExist(err) { |
| 1149 | err = os.MkdirAll(mixedBuildsPath, 0777) |
Usta Shrestha | 902fd17 | 2022-03-02 15:27:49 -0500 | [diff] [blame] | 1150 | if err != nil { |
| 1151 | return err |
| 1152 | } |
| 1153 | } |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 1154 | if err := writeFileBytesIfChanged(filepath.Join(soongInjectionPath, "WORKSPACE.bazel"), []byte{}, 0666); err != nil { |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 1155 | return err |
| 1156 | } |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 1157 | if err := writeFileBytesIfChanged(filepath.Join(mixedBuildsPath, "main.bzl"), context.mainBzlFileContents(), 0666); err != nil { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 1158 | return err |
| 1159 | } |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 1160 | if err := writeFileBytesIfChanged(filepath.Join(mixedBuildsPath, "BUILD.bazel"), context.mainBuildFileContents(), 0666); err != nil { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 1161 | return err |
| 1162 | } |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 1163 | cqueryFileRelpath := filepath.Join(context.paths.injectedFilesDir(), "buildroot.cquery") |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 1164 | if err := writeFileBytesIfChanged(absolutePath(cqueryFileRelpath), context.cqueryStarlarkFileContents(), 0666); err != nil { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 1165 | return err |
| 1166 | } |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 1167 | |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 1168 | extraFlags := []string{"--output=starlark", "--starlark:file=" + absolutePath(cqueryFileRelpath)} |
| 1169 | if Bool(config.productVariables.ClangCoverage) { |
| 1170 | extraFlags = append(extraFlags, "--collect_code_coverage") |
| 1171 | } |
| 1172 | |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 1173 | cqueryCmdRequest := context.createBazelCommand(config, bazel.CqueryBuildRootRunName, cqueryCmd, extraFlags...) |
| 1174 | cqueryOutput, cqueryErrorMessage, cqueryErr := context.issueBazelCommand(cqueryCmdRequest, context.paths, eventHandler) |
Wei Li | cbd181c | 2022-11-16 08:59:23 -0800 | [diff] [blame] | 1175 | if cqueryErr != nil { |
| 1176 | return cqueryErr |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 1177 | } |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 1178 | cqueryCommandPrint := fmt.Sprintf("cquery command line:\n %s \n\n\n", context.printableCqueryCommand(cqueryCmdRequest)) |
Sasha Smundak | 0e87b18 | 2022-12-01 11:46:11 -0800 | [diff] [blame] | 1179 | if err := os.WriteFile(filepath.Join(soongInjectionPath, "cquery.out"), []byte(cqueryCommandPrint+cqueryOutput), 0666); err != nil { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 1180 | return err |
| 1181 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 1182 | cqueryResults := map[string]string{} |
| 1183 | for _, outputLine := range strings.Split(cqueryOutput, "\n") { |
| 1184 | if strings.Contains(outputLine, ">>") { |
| 1185 | splitLine := strings.SplitN(outputLine, ">>", 2) |
| 1186 | cqueryResults[splitLine[0]] = splitLine[1] |
| 1187 | } |
| 1188 | } |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 1189 | for _, val := range context.requests { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 1190 | if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok { |
Usta Shrestha | 902fd17 | 2022-03-02 15:27:49 -0500 | [diff] [blame] | 1191 | context.results[val] = cqueryResult |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 1192 | } else { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 1193 | return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]", |
Wei Li | cbd181c | 2022-11-16 08:59:23 -0800 | [diff] [blame] | 1194 | getCqueryId(val), cqueryOutput, cqueryErrorMessage) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 1195 | } |
| 1196 | } |
Sasha Smundak | 4975c82 | 2022-11-16 15:28:18 -0800 | [diff] [blame] | 1197 | return nil |
| 1198 | } |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 1199 | |
Chris Parsons | 3a8d0fb | 2023-02-02 18:16:29 -0500 | [diff] [blame] | 1200 | func writeFileBytesIfChanged(path string, contents []byte, perm os.FileMode) error { |
| 1201 | oldContents, err := os.ReadFile(path) |
| 1202 | if err != nil || !bytes.Equal(contents, oldContents) { |
| 1203 | err = os.WriteFile(path, contents, perm) |
| 1204 | } |
| 1205 | return nil |
| 1206 | } |
| 1207 | |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 1208 | func (context *mixedBuildBazelContext) runAquery(config Config, ctx invokeBazelContext) error { |
| 1209 | eventHandler := ctx.GetEventHandler() |
| 1210 | eventHandler.Begin("aquery") |
| 1211 | defer eventHandler.End("aquery") |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1212 | // Issue an aquery command to retrieve action information about the bazel build tree. |
| 1213 | // |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 1214 | // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's |
| 1215 | // proto sources, which would add a number of unnecessary dependencies. |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 1216 | extraFlags := []string{"--output=proto", "--include_file_write_contents"} |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 1217 | if Bool(config.productVariables.ClangCoverage) { |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 1218 | extraFlags = append(extraFlags, "--collect_code_coverage") |
| 1219 | paths := make([]string, 0, 2) |
| 1220 | if p := config.productVariables.NativeCoveragePaths; len(p) > 0 { |
Sasha Smundak | 0e87b18 | 2022-12-01 11:46:11 -0800 | [diff] [blame] | 1221 | for i := range p { |
Wei Li | cbd181c | 2022-11-16 08:59:23 -0800 | [diff] [blame] | 1222 | // TODO(b/259404593) convert path wildcard to regex values |
| 1223 | if p[i] == "*" { |
| 1224 | p[i] = ".*" |
| 1225 | } |
| 1226 | } |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 1227 | paths = append(paths, JoinWithPrefixAndSeparator(p, "+", ",")) |
| 1228 | } |
| 1229 | if p := config.productVariables.NativeCoverageExcludePaths; len(p) > 0 { |
| 1230 | paths = append(paths, JoinWithPrefixAndSeparator(p, "-", ",")) |
| 1231 | } |
| 1232 | if len(paths) > 0 { |
| 1233 | extraFlags = append(extraFlags, "--instrumentation_filter="+strings.Join(paths, ",")) |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 1234 | } |
| 1235 | } |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 1236 | aqueryOutput, _, err := context.issueBazelCommand(context.createBazelCommand(config, bazel.AqueryBuildRootRunName, aqueryCmd, |
| 1237 | extraFlags...), context.paths, eventHandler) |
Sasha Smundak | 4975c82 | 2022-11-16 15:28:18 -0800 | [diff] [blame] | 1238 | if err != nil { |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1239 | return err |
| 1240 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 1241 | context.buildStatements, context.depsets, err = bazel.AqueryBuildStatements([]byte(aqueryOutput), eventHandler) |
Sasha Smundak | 4975c82 | 2022-11-16 15:28:18 -0800 | [diff] [blame] | 1242 | return err |
| 1243 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1244 | |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 1245 | func (context *mixedBuildBazelContext) generateBazelSymlinks(config Config, ctx invokeBazelContext) error { |
| 1246 | eventHandler := ctx.GetEventHandler() |
| 1247 | eventHandler.Begin("symlinks") |
| 1248 | defer eventHandler.End("symlinks") |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1249 | // Issue a build command of the phony root to generate symlink forests for dependencies of the |
| 1250 | // Bazel build. This is necessary because aquery invocations do not generate this symlink forest, |
| 1251 | // but some of symlinks may be required to resolve source dependencies of the build. |
Chris Parsons | c9089dc | 2023-04-24 16:21:27 +0000 | [diff] [blame] | 1252 | _, _, err := context.issueBazelCommand(context.createBazelCommand(config, bazel.BazelBuildPhonyRootRunName, buildCmd), context.paths, eventHandler) |
Sasha Smundak | 4975c82 | 2022-11-16 15:28:18 -0800 | [diff] [blame] | 1253 | return err |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 1254 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 1255 | |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 1256 | func (context *mixedBuildBazelContext) BuildStatementsToRegister() []*bazel.BuildStatement { |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1257 | return context.buildStatements |
| 1258 | } |
| 1259 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 1260 | func (context *mixedBuildBazelContext) AqueryDepsets() []bazel.AqueryDepset { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 1261 | return context.depsets |
| 1262 | } |
| 1263 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 1264 | func (context *mixedBuildBazelContext) OutputBase() string { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 1265 | return context.paths.outputBase |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1266 | } |
| 1267 | |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 1268 | // Singleton used for registering BUILD file ninja dependencies (needed |
| 1269 | // for correctness of builds which use Bazel. |
| 1270 | func BazelSingleton() Singleton { |
| 1271 | return &bazelSingleton{} |
| 1272 | } |
| 1273 | |
| 1274 | type bazelSingleton struct{} |
| 1275 | |
| 1276 | func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) { |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1277 | // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled. |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 1278 | if !ctx.Config().IsMixedBuildsEnabled() { |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1279 | return |
| 1280 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 1281 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1282 | // Add ninja file dependencies for files which all bazel invocations require. |
| 1283 | bazelBuildList := absolutePath(filepath.Join( |
Lukacs T. Berki | f900807 | 2021-08-16 15:24:48 +0200 | [diff] [blame] | 1284 | filepath.Dir(ctx.Config().moduleListFile), "bazel.list")) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1285 | ctx.AddNinjaFileDeps(bazelBuildList) |
| 1286 | |
Sasha Smundak | 0e87b18 | 2022-12-01 11:46:11 -0800 | [diff] [blame] | 1287 | data, err := os.ReadFile(bazelBuildList) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1288 | if err != nil { |
| 1289 | ctx.Errorf(err.Error()) |
| 1290 | } |
| 1291 | files := strings.Split(strings.TrimSpace(string(data)), "\n") |
| 1292 | for _, file := range files { |
| 1293 | ctx.AddNinjaFileDeps(file) |
| 1294 | } |
| 1295 | |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 1296 | depsetHashToDepset := map[string]bazel.AqueryDepset{} |
| 1297 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 1298 | for _, depset := range ctx.Config().BazelContext.AqueryDepsets() { |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 1299 | depsetHashToDepset[depset.ContentHash] = depset |
| 1300 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 1301 | var outputs []Path |
usta | fdb3e34 | 2022-11-22 17:11:30 -0500 | [diff] [blame] | 1302 | var orderOnlies []Path |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 1303 | for _, depsetDepHash := range depset.TransitiveDepSetHashes { |
| 1304 | otherDepsetName := bazelDepsetName(depsetDepHash) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 1305 | outputs = append(outputs, PathForPhony(ctx, otherDepsetName)) |
| 1306 | } |
| 1307 | for _, artifactPath := range depset.DirectArtifacts { |
usta | fdb3e34 | 2022-11-22 17:11:30 -0500 | [diff] [blame] | 1308 | pathInBazelOut := PathForBazelOut(ctx, artifactPath) |
| 1309 | if artifactPath == "bazel-out/volatile-status.txt" { |
| 1310 | // See https://bazel.build/docs/user-manual#workspace-status |
| 1311 | orderOnlies = append(orderOnlies, pathInBazelOut) |
| 1312 | } else { |
| 1313 | outputs = append(outputs, pathInBazelOut) |
| 1314 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 1315 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 1316 | thisDepsetName := bazelDepsetName(depset.ContentHash) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 1317 | ctx.Build(pctx, BuildParams{ |
| 1318 | Rule: blueprint.Phony, |
| 1319 | Outputs: []WritablePath{PathForPhony(ctx, thisDepsetName)}, |
| 1320 | Implicits: outputs, |
usta | fdb3e34 | 2022-11-22 17:11:30 -0500 | [diff] [blame] | 1321 | OrderOnly: orderOnlies, |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 1322 | }) |
| 1323 | } |
| 1324 | |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 1325 | executionRoot := path.Join(ctx.Config().BazelContext.OutputBase(), "execroot", "__main__") |
| 1326 | bazelOutDir := path.Join(executionRoot, "bazel-out") |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1327 | rel, err := filepath.Rel(ctx.Config().OutDir(), executionRoot) |
| 1328 | if err != nil { |
| 1329 | ctx.Errorf("%s", err.Error()) |
| 1330 | } |
| 1331 | dotdotsToOutRoot := strings.Repeat("../", strings.Count(rel, "/")+1) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1332 | for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() { |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 1333 | // nil build statements are a valid case where we do not create an action because it is |
| 1334 | // unnecessary or handled by other processing |
| 1335 | if buildStatement == nil { |
| 1336 | continue |
| 1337 | } |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 1338 | if len(buildStatement.Command) > 0 { |
| 1339 | rule := NewRuleBuilder(pctx, ctx) |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 1340 | intermediateDir, intermediateDirHash := intermediatePathForSboxMixedBuildAction(ctx, buildStatement) |
| 1341 | if buildStatement.ShouldRunInSbox { |
| 1342 | // Create a rule to build the output inside a sandbox |
| 1343 | // This will create two changes of working directory |
| 1344 | // 1. From ANDROID_BUILD_TOP to sbox top |
| 1345 | // 2. From sbox top to a a synthetic mixed build execution root relative to it |
| 1346 | // Finally, the outputs will be copied to intermediateDir |
| 1347 | rule.Sbox(intermediateDir, |
| 1348 | PathForOutput(ctx, "mixed_build_sbox_intermediates", intermediateDirHash+".textproto")). |
| 1349 | SandboxInputs(). |
| 1350 | // Since we will cd to mixed build execution root, set sbox's out subdir to empty |
| 1351 | // Without this, we will try to copy from $SBOX_SANDBOX_DIR/out/out/bazel/output/execroot/__main__/... |
| 1352 | SetSboxOutDirDirAsEmpty() |
| 1353 | |
| 1354 | // Create another set of rules to copy files from the intermediate dir to mixed build execution root |
| 1355 | for _, outputPath := range buildStatement.OutputPaths { |
| 1356 | ctx.Build(pctx, BuildParams{ |
| 1357 | Rule: CpIfChanged, |
| 1358 | Input: intermediateDir.Join(ctx, executionRoot, outputPath), |
| 1359 | Output: PathForBazelOut(ctx, outputPath), |
| 1360 | }) |
| 1361 | } |
| 1362 | } |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1363 | createCommand(rule.Command(), buildStatement, executionRoot, bazelOutDir, ctx, depsetHashToDepset, dotdotsToOutRoot) |
| 1364 | |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 1365 | desc := fmt.Sprintf("%s: %s", buildStatement.Mnemonic, buildStatement.OutputPaths) |
| 1366 | rule.Build(fmt.Sprintf("bazel %d", index), desc) |
| 1367 | continue |
| 1368 | } |
| 1369 | // Certain actions returned by aquery (for instance FileWrite) do not contain a command |
| 1370 | // and thus require special treatment. If BuildStatement were an interface implementing |
| 1371 | // buildRule(ctx) function, the code here would just call it. |
| 1372 | // Unfortunately, the BuildStatement is defined in |
| 1373 | // the 'bazel' package, which cannot depend on 'android' package where ctx is defined, |
| 1374 | // because this would cause circular dependency. So, until we move aquery processing |
| 1375 | // to the 'android' package, we need to handle special cases here. |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 1376 | switch buildStatement.Mnemonic { |
Cole Faust | 950689a | 2023-06-21 15:07:21 -0700 | [diff] [blame] | 1377 | case "RepoMappingManifest": |
| 1378 | // It appears RepoMappingManifest files currently have |
| 1379 | // non-deterministic content. Just emit empty files for |
| 1380 | // now because they're unused. |
| 1381 | out := PathForBazelOut(ctx, buildStatement.OutputPaths[0]) |
| 1382 | WriteFileRuleVerbatim(ctx, out, "") |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 1383 | case "FileWrite", "SourceSymlinkManifest": |
Cole Faust | a734749 | 2022-12-16 10:56:24 -0800 | [diff] [blame] | 1384 | out := PathForBazelOut(ctx, buildStatement.OutputPaths[0]) |
Cole Faust | 20f2030 | 2023-08-31 11:00:25 -0700 | [diff] [blame^] | 1385 | if buildStatement.IsExecutable { |
Cole Faust | 39b614a | 2023-08-23 16:11:26 -0700 | [diff] [blame] | 1386 | WriteExecutableFileRuleVerbatim(ctx, out, buildStatement.FileContents) |
| 1387 | } else { |
| 1388 | WriteFileRuleVerbatim(ctx, out, buildStatement.FileContents) |
| 1389 | } |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 1390 | case "SymlinkTree": |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 1391 | // build-runfiles arguments are the manifest file and the target directory |
| 1392 | // where it creates the symlink tree according to this manifest (and then |
| 1393 | // writes the MANIFEST file to it). |
| 1394 | outManifest := PathForBazelOut(ctx, buildStatement.OutputPaths[0]) |
| 1395 | outManifestPath := outManifest.String() |
| 1396 | if !strings.HasSuffix(outManifestPath, "MANIFEST") { |
| 1397 | panic("the base name of the symlink tree action should be MANIFEST, got " + outManifestPath) |
| 1398 | } |
| 1399 | outDir := filepath.Dir(outManifestPath) |
| 1400 | ctx.Build(pctx, BuildParams{ |
| 1401 | Rule: buildRunfilesRule, |
| 1402 | Output: outManifest, |
| 1403 | Inputs: []Path{PathForBazelOut(ctx, buildStatement.InputPaths[0])}, |
| 1404 | Description: "symlink tree for " + outDir, |
| 1405 | Args: map[string]string{ |
| 1406 | "outDir": outDir, |
| 1407 | }, |
| 1408 | }) |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 1409 | default: |
Rupert Shuttleworth | a29903f | 2021-04-06 16:17:33 +0000 | [diff] [blame] | 1410 | panic(fmt.Sprintf("unhandled build statement: %v", buildStatement)) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 1411 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 1412 | } |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1413 | |
| 1414 | // Create phony targets for all the bazel sandwich output files |
| 1415 | requests, err := GetBazelSandwichCqueryRequests(ctx.Config()) |
| 1416 | if err != nil { |
| 1417 | ctx.Errorf(err.Error()) |
| 1418 | } |
| 1419 | for _, request := range requests { |
| 1420 | files, err := ctx.Config().BazelContext.GetOutputFiles(request.label, request.configKey) |
| 1421 | if err != nil { |
| 1422 | ctx.Errorf(err.Error()) |
| 1423 | } |
| 1424 | filesAsPaths := make([]Path, 0, len(files)) |
| 1425 | for _, file := range files { |
| 1426 | filesAsPaths = append(filesAsPaths, PathForBazelOut(ctx, file)) |
| 1427 | } |
| 1428 | ctx.Phony("bazel_sandwich", filesAsPaths...) |
| 1429 | } |
| 1430 | ctx.Phony("checkbuild", PathForPhony(ctx, "bazel_sandwich")) |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 1431 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 1432 | |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 1433 | // Returns a out dir path for a sandboxed mixed build action |
| 1434 | func intermediatePathForSboxMixedBuildAction(ctx PathContext, statement *bazel.BuildStatement) (OutputPath, string) { |
| 1435 | // An artifact can be generated by a single buildstatement. |
| 1436 | // Use the hash of the first artifact to create a unique path |
| 1437 | uniqueDir := sha1.New() |
| 1438 | uniqueDir.Write([]byte(statement.OutputPaths[0])) |
| 1439 | uniqueDirHashString := hex.EncodeToString(uniqueDir.Sum(nil)) |
| 1440 | return PathForOutput(ctx, "mixed_build_sbox_intermediates", uniqueDirHashString), uniqueDirHashString |
| 1441 | } |
| 1442 | |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 1443 | // Register bazel-owned build statements (obtained from the aquery invocation). |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1444 | func createCommand(cmd *RuleBuilderCommand, buildStatement *bazel.BuildStatement, executionRoot string, bazelOutDir string, ctx BuilderContext, depsetHashToDepset map[string]bazel.AqueryDepset, dotdotsToOutRoot string) { |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 1445 | // executionRoot is the action cwd. |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 1446 | if buildStatement.ShouldRunInSbox { |
| 1447 | // mkdir -p ensures that the directory exists when run via sbox |
| 1448 | cmd.Text(fmt.Sprintf("mkdir -p '%s' && cd '%s' &&", executionRoot, executionRoot)) |
| 1449 | } else { |
| 1450 | cmd.Text(fmt.Sprintf("cd '%s' &&", executionRoot)) |
| 1451 | } |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 1452 | |
| 1453 | // Remove old outputs, as some actions might not rerun if the outputs are detected. |
| 1454 | if len(buildStatement.OutputPaths) > 0 { |
Jingwen Chen | f3b1ec3 | 2022-11-07 15:02:48 +0000 | [diff] [blame] | 1455 | cmd.Text("rm -rf") // -r because outputs can be Bazel dir/tree artifacts. |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 1456 | for _, outputPath := range buildStatement.OutputPaths { |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 1457 | cmd.Text(fmt.Sprintf("'%s'", outputPath)) |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 1458 | } |
| 1459 | cmd.Text("&&") |
| 1460 | } |
| 1461 | |
| 1462 | for _, pair := range buildStatement.Env { |
| 1463 | // Set per-action env variables, if any. |
| 1464 | cmd.Flag(pair.Key + "=" + pair.Value) |
| 1465 | } |
| 1466 | |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1467 | command := buildStatement.Command |
| 1468 | command = strings.ReplaceAll(command, "{DOTDOTS_TO_OUTPUT_ROOT}", dotdotsToOutRoot) |
| 1469 | |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 1470 | // The actual Bazel action. |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1471 | if len(command) > 16*1024 { |
Colin Cross | d5c7ddb | 2022-12-06 16:27:17 -0800 | [diff] [blame] | 1472 | commandFile := PathForBazelOut(ctx, buildStatement.OutputPaths[0]+".sh") |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1473 | WriteFileRule(ctx, commandFile, command) |
Colin Cross | d5c7ddb | 2022-12-06 16:27:17 -0800 | [diff] [blame] | 1474 | |
| 1475 | cmd.Text("bash").Text(buildStatement.OutputPaths[0] + ".sh").Implicit(commandFile) |
| 1476 | } else { |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1477 | cmd.Text(command) |
Colin Cross | d5c7ddb | 2022-12-06 16:27:17 -0800 | [diff] [blame] | 1478 | } |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 1479 | |
| 1480 | for _, outputPath := range buildStatement.OutputPaths { |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 1481 | if buildStatement.ShouldRunInSbox { |
| 1482 | // The full path has three components that get joined together |
| 1483 | // 1. intermediate output dir that `sbox` will place the artifacts at |
| 1484 | // 2. mixed build execution root |
| 1485 | // 3. artifact path returned by aquery |
| 1486 | intermediateDir, _ := intermediatePathForSboxMixedBuildAction(ctx, buildStatement) |
| 1487 | cmd.ImplicitOutput(intermediateDir.Join(ctx, executionRoot, outputPath)) |
| 1488 | } else { |
| 1489 | cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath)) |
| 1490 | } |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 1491 | } |
| 1492 | for _, inputPath := range buildStatement.InputPaths { |
| 1493 | cmd.Implicit(PathForBazelOut(ctx, inputPath)) |
| 1494 | } |
| 1495 | for _, inputDepsetHash := range buildStatement.InputDepsetHashes { |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 1496 | if buildStatement.ShouldRunInSbox { |
| 1497 | // Bazel depsets are phony targets that are used to group files. |
| 1498 | // We need to copy the grouped files into the sandbox |
| 1499 | ds, _ := depsetHashToDepset[inputDepsetHash] |
| 1500 | cmd.Implicits(PathsForBazelOut(ctx, ds.DirectArtifacts)) |
| 1501 | } else { |
| 1502 | otherDepsetName := bazelDepsetName(inputDepsetHash) |
| 1503 | cmd.Implicit(PathForPhony(ctx, otherDepsetName)) |
| 1504 | } |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 1505 | } |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1506 | for _, implicitPath := range buildStatement.ImplicitDeps { |
| 1507 | cmd.Implicit(PathForArbitraryOutput(ctx, implicitPath)) |
| 1508 | } |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 1509 | |
| 1510 | if depfile := buildStatement.Depfile; depfile != nil { |
| 1511 | // The paths in depfile are relative to `executionRoot`. |
| 1512 | // Hence, they need to be corrected by replacing "bazel-out" |
| 1513 | // with the full `bazelOutDir`. |
| 1514 | // Otherwise, implicit outputs and implicit inputs under "bazel-out/" |
| 1515 | // would be deemed missing. |
| 1516 | // (Note: The regexp uses a capture group because the version of sed |
| 1517 | // does not support a look-behind pattern.) |
| 1518 | replacement := fmt.Sprintf(`&& sed -i'' -E 's@(^|\s|")bazel-out/@\1%s/@g' '%s'`, |
| 1519 | bazelOutDir, *depfile) |
| 1520 | cmd.Text(replacement) |
| 1521 | cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile)) |
| 1522 | } |
| 1523 | |
| 1524 | for _, symlinkPath := range buildStatement.SymlinkPaths { |
| 1525 | cmd.ImplicitSymlinkOutput(PathForBazelOut(ctx, symlinkPath)) |
| 1526 | } |
| 1527 | } |
| 1528 | |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 1529 | func getCqueryId(key cqueryKey) string { |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 1530 | return key.label + "|" + getConfigString(key) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 1531 | } |
| 1532 | |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 1533 | func getConfigString(key cqueryKey) string { |
Liz Kammer | 0940b89 | 2022-03-18 15:55:04 -0400 | [diff] [blame] | 1534 | arch := key.configKey.arch |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 1535 | if len(arch) == 0 || arch == "common" { |
Sasha Smundak | 9d46dcf | 2022-06-08 12:10:36 -0700 | [diff] [blame] | 1536 | if key.configKey.osType.Class == Device { |
| 1537 | // For the generic Android, the expected result is "target|android", which |
| 1538 | // corresponds to the product_variable_config named "android_target" in |
| 1539 | // build/bazel/platforms/BUILD.bazel. |
| 1540 | arch = "target" |
| 1541 | } else { |
| 1542 | // Use host platform, which is currently hardcoded to be x86_64. |
| 1543 | arch = "x86_64" |
| 1544 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 1545 | } |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 1546 | osName := key.configKey.osType.Name |
Colin Cross | 046fcea | 2022-12-20 15:32:18 -0800 | [diff] [blame] | 1547 | if len(osName) == 0 || osName == "common_os" || osName == "linux_glibc" || osName == "linux_musl" { |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 1548 | // Use host OS, which is currently hardcoded to be linux. |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 1549 | osName = "linux" |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 1550 | } |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 1551 | keyString := arch + "|" + osName |
| 1552 | if key.configKey.apexKey.WithinApex { |
| 1553 | keyString += "|" + withinApexToString(key.configKey.apexKey.WithinApex) |
| 1554 | } |
| 1555 | |
| 1556 | if len(key.configKey.apexKey.ApexSdkVersion) > 0 { |
| 1557 | keyString += "|" + key.configKey.apexKey.ApexSdkVersion |
| 1558 | } |
| 1559 | |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 1560 | if len(key.configKey.apexKey.ApiDomain) > 0 { |
| 1561 | keyString += "|" + key.configKey.apexKey.ApiDomain |
| 1562 | } |
| 1563 | |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 1564 | return keyString |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 1565 | } |
| 1566 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 1567 | func GetConfigKey(ctx BaseModuleContext) configKey { |
Liz Kammer | 0940b89 | 2022-03-18 15:55:04 -0400 | [diff] [blame] | 1568 | return configKey{ |
| 1569 | // use string because Arch is not a valid key in go |
| 1570 | arch: ctx.Arch().String(), |
| 1571 | osType: ctx.Os(), |
| 1572 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 1573 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 1574 | |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 1575 | func GetConfigKeyApexVariant(ctx BaseModuleContext, apexKey *ApexConfigKey) configKey { |
| 1576 | configKey := GetConfigKey(ctx) |
| 1577 | |
| 1578 | if apexKey != nil { |
| 1579 | configKey.apexKey = ApexConfigKey{ |
| 1580 | WithinApex: apexKey.WithinApex, |
| 1581 | ApexSdkVersion: apexKey.ApexSdkVersion, |
Spandan Das | 40b79f8 | 2023-06-25 20:56:06 +0000 | [diff] [blame] | 1582 | ApiDomain: apexKey.ApiDomain, |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | return configKey |
| 1587 | } |
| 1588 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 1589 | func bazelDepsetName(contentHash string) string { |
| 1590 | return fmt.Sprintf("bazel_depset_%s", contentHash) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 1591 | } |