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