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