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