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