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