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