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