Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 16 | |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 17 | import ( |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 18 | "android/soong/bazel" |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 19 | "fmt" |
Colin Cross | 18c4680 | 2019-09-24 22:19:02 -0700 | [diff] [blame] | 20 | "reflect" |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 21 | "strings" |
Colin Cross | 18c4680 | 2019-09-24 22:19:02 -0700 | [diff] [blame] | 22 | |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint" |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 25 | ) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 26 | |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 27 | // Phases: |
| 28 | // run Pre-arch mutators |
| 29 | // run archMutator |
| 30 | // run Pre-deps mutators |
| 31 | // run depsMutator |
| 32 | // run PostDeps mutators |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 33 | // run FinalDeps mutators (CreateVariations disallowed in this phase) |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 34 | // continue on to GenerateAndroidBuildActions |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 35 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 36 | // RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing. |
Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame^] | 37 | func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators, depsMutators, bp2buildMutators []RegisterMutatorFunc) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 38 | mctx := ®isterMutatorsContext{ |
| 39 | bazelConversionMode: true, |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 40 | } |
| 41 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 42 | bp2buildPreArchMutators = append([]RegisterMutatorFunc{ |
| 43 | RegisterNamespaceMutator, |
| 44 | RegisterDefaultsPreArchMutators, |
| 45 | // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should |
| 46 | // evaluate the impact on conversion. |
| 47 | RegisterPrebuiltsPreArchMutators, |
| 48 | }, |
| 49 | preArchMutators...) |
| 50 | |
| 51 | for _, f := range bp2buildPreArchMutators { |
| 52 | f(mctx) |
| 53 | } |
| 54 | |
| 55 | bp2buildDepsMutators = append([]RegisterMutatorFunc{ |
| 56 | registerDepsMutatorBp2Build, |
| 57 | registerPathDepsMutator, |
| 58 | }, depsMutators...) |
| 59 | |
| 60 | for _, f := range bp2buildDepsMutators { |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 61 | f(mctx) |
| 62 | } |
| 63 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 64 | // Register bp2build mutators |
| 65 | for _, f := range bp2buildMutators { |
| 66 | f(mctx) |
| 67 | } |
| 68 | |
Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame^] | 69 | mctx.mutators.registerAll(ctx) |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame^] | 72 | func registerMutators(ctx *Context, preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 73 | mctx := ®isterMutatorsContext{} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 74 | |
| 75 | register := func(funcs []RegisterMutatorFunc) { |
| 76 | for _, f := range funcs { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 77 | f(mctx) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 81 | register(preArch) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 82 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 83 | register(preDeps) |
| 84 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 85 | register([]RegisterMutatorFunc{registerDepsMutator}) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 86 | |
| 87 | register(postDeps) |
| 88 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 89 | mctx.finalPhase = true |
| 90 | register(finalDeps) |
| 91 | |
Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame^] | 92 | mctx.mutators.registerAll(ctx) |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | type registerMutatorsContext struct { |
Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame^] | 96 | mutators sortableComponents |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 97 | finalPhase bool |
| 98 | bazelConversionMode bool |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 99 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 100 | |
| 101 | type RegisterMutatorsContext interface { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 102 | TopDown(name string, m TopDownMutator) MutatorHandle |
| 103 | BottomUp(name string, m BottomUpMutator) MutatorHandle |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 104 | BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | type RegisterMutatorFunc func(RegisterMutatorsContext) |
| 108 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 109 | var preArch = []RegisterMutatorFunc{ |
Dan Willemsen | 6e72ef7 | 2018-01-26 18:27:02 -0800 | [diff] [blame] | 110 | RegisterNamespaceMutator, |
Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 111 | |
Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 112 | // Check the visibility rules are valid. |
| 113 | // |
| 114 | // This must run after the package renamer mutators so that any issues found during |
| 115 | // validation of the package's default_visibility property are reported using the |
| 116 | // correct package name and not the synthetic name. |
| 117 | // |
| 118 | // This must also be run before defaults mutators as the rules for validation are |
| 119 | // different before checking the rules than they are afterwards. e.g. |
| 120 | // visibility: ["//visibility:private", "//visibility:public"] |
| 121 | // would be invalid if specified in a module definition but is valid if it results |
| 122 | // from something like this: |
| 123 | // |
| 124 | // defaults { |
| 125 | // name: "defaults", |
| 126 | // // Be inaccessible outside a package by default. |
| 127 | // visibility: ["//visibility:private"] |
| 128 | // } |
| 129 | // |
| 130 | // defaultable_module { |
| 131 | // name: "defaultable_module", |
| 132 | // defaults: ["defaults"], |
| 133 | // // Override the default. |
| 134 | // visibility: ["//visibility:public"] |
| 135 | // } |
| 136 | // |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 137 | RegisterVisibilityRuleChecker, |
Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 138 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 139 | // Record the default_applicable_licenses for each package. |
| 140 | // |
| 141 | // This must run before the defaults so that defaults modules can pick up the package default. |
| 142 | RegisterLicensesPackageMapper, |
| 143 | |
Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 144 | // Apply properties from defaults modules to the referencing modules. |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 145 | // |
| 146 | // Any mutators that are added before this will not see any modules created by |
| 147 | // a DefaultableHook. |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 148 | RegisterDefaultsPreArchMutators, |
Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 149 | |
Paul Duffin | 44f1d84 | 2020-06-26 20:17:02 +0100 | [diff] [blame] | 150 | // Add dependencies on any components so that any component references can be |
| 151 | // resolved within the deps mutator. |
| 152 | // |
| 153 | // Must be run after defaults so it can be used to create dependencies on the |
| 154 | // component modules that are creating in a DefaultableHook. |
| 155 | // |
| 156 | // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are |
| 157 | // renamed. That is so that if a module creates components using a prebuilt module |
| 158 | // type that any dependencies (which must use prebuilt_ prefixes) are resolved to |
| 159 | // the prebuilt module and not the source module. |
| 160 | RegisterComponentsMutator, |
| 161 | |
Paul Duffin | c988c8e | 2020-04-29 18:27:14 +0100 | [diff] [blame] | 162 | // Create an association between prebuilt modules and their corresponding source |
| 163 | // modules (if any). |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 164 | // |
| 165 | // Must be run after defaults mutators to ensure that any modules created by |
| 166 | // a DefaultableHook can be either a prebuilt or a source module with a matching |
| 167 | // prebuilt. |
Paul Duffin | c988c8e | 2020-04-29 18:27:14 +0100 | [diff] [blame] | 168 | RegisterPrebuiltsPreArchMutators, |
| 169 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 170 | // Gather the licenses properties for all modules for use during expansion and enforcement. |
| 171 | // |
| 172 | // This must come after the defaults mutators to ensure that any licenses supplied |
| 173 | // in a defaults module has been successfully applied before the rules are gathered. |
| 174 | RegisterLicensesPropertyGatherer, |
| 175 | |
Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 176 | // Gather the visibility rules for all modules for us during visibility enforcement. |
| 177 | // |
| 178 | // This must come after the defaults mutators to ensure that any visibility supplied |
| 179 | // in a defaults module has been successfully applied before the rules are gathered. |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 180 | RegisterVisibilityRuleGatherer, |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 183 | func registerArchMutator(ctx RegisterMutatorsContext) { |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 184 | ctx.BottomUpBlueprint("os", osMutator).Parallel() |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 185 | ctx.BottomUp("image", imageMutator).Parallel() |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 186 | ctx.BottomUpBlueprint("arch", archMutator).Parallel() |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 189 | var preDeps = []RegisterMutatorFunc{ |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 190 | registerArchMutator, |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | var postDeps = []RegisterMutatorFunc{ |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 194 | registerPathDepsMutator, |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 195 | RegisterPrebuiltsPostDepsMutators, |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 196 | RegisterVisibilityRuleEnforcer, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 197 | RegisterLicensesDependencyChecker, |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 198 | RegisterNeverallowMutator, |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 199 | RegisterOverridePostDepsMutators, |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 200 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 201 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 202 | var finalDeps = []RegisterMutatorFunc{} |
| 203 | |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 204 | func PreArchMutators(f RegisterMutatorFunc) { |
| 205 | preArch = append(preArch, f) |
| 206 | } |
| 207 | |
| 208 | func PreDepsMutators(f RegisterMutatorFunc) { |
| 209 | preDeps = append(preDeps, f) |
| 210 | } |
| 211 | |
| 212 | func PostDepsMutators(f RegisterMutatorFunc) { |
| 213 | postDeps = append(postDeps, f) |
| 214 | } |
| 215 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 216 | func FinalDepsMutators(f RegisterMutatorFunc) { |
| 217 | finalDeps = append(finalDeps, f) |
| 218 | } |
| 219 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 220 | var bp2buildPreArchMutators = []RegisterMutatorFunc{} |
| 221 | var bp2buildDepsMutators = []RegisterMutatorFunc{} |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 222 | var bp2buildMutators = []RegisterMutatorFunc{} |
| 223 | |
| 224 | // RegisterBp2BuildMutator registers specially crafted mutators for |
| 225 | // converting Blueprint/Android modules into special modules that can |
| 226 | // be code-generated into Bazel BUILD targets. |
| 227 | // |
| 228 | // TODO(b/178068862): bring this into TestContext. |
| 229 | func RegisterBp2BuildMutator(moduleType string, m func(TopDownMutatorContext)) { |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 230 | f := func(ctx RegisterMutatorsContext) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 231 | ctx.TopDown(moduleType, m) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 232 | } |
| 233 | bp2buildMutators = append(bp2buildMutators, f) |
| 234 | } |
| 235 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 236 | // PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules |
| 237 | // into Bazel BUILD targets that should run prior to deps and conversion. |
| 238 | func PreArchBp2BuildMutators(f RegisterMutatorFunc) { |
| 239 | bp2buildPreArchMutators = append(bp2buildPreArchMutators, f) |
| 240 | } |
| 241 | |
| 242 | // DepsBp2BuildMutators adds mutators to be register for converting Android Blueprint modules into |
| 243 | // Bazel BUILD targets that should run prior to conversion to resolve dependencies. |
| 244 | func DepsBp2BuildMutators(f RegisterMutatorFunc) { |
| 245 | bp2buildDepsMutators = append(bp2buildDepsMutators, f) |
| 246 | } |
| 247 | |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 248 | type BaseMutatorContext interface { |
| 249 | BaseModuleContext |
| 250 | |
| 251 | // MutatorName returns the name that this mutator was registered with. |
| 252 | MutatorName() string |
| 253 | |
| 254 | // Rename all variants of a module. The new name is not visible to calls to ModuleName, |
| 255 | // AddDependency or OtherModuleName until after this mutator pass is complete. |
| 256 | Rename(name string) |
| 257 | } |
| 258 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 259 | type TopDownMutator func(TopDownMutatorContext) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 260 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 261 | type TopDownMutatorContext interface { |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 262 | BaseMutatorContext |
Colin Cross | 3f68a13 | 2017-10-23 17:10:29 -0700 | [diff] [blame] | 263 | |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 264 | // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies |
| 265 | // the specified property structs to it as if the properties were set in a blueprint file. |
Colin Cross | e003c4a | 2019-09-25 12:58:36 -0700 | [diff] [blame] | 266 | CreateModule(ModuleFactory, ...interface{}) Module |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 267 | |
| 268 | // CreateBazelTargetModule creates a BazelTargetModule by calling the |
| 269 | // factory method, just like in CreateModule, but also requires |
| 270 | // BazelTargetModuleProperties containing additional metadata for the |
| 271 | // bp2build codegenerator. |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 272 | CreateBazelTargetModule(ModuleFactory, string, bazel.BazelTargetModuleProperties, interface{}) BazelTargetModule |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 275 | type topDownMutatorContext struct { |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 276 | bp blueprint.TopDownMutatorContext |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 277 | baseModuleContext |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 280 | type BottomUpMutator func(BottomUpMutatorContext) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 281 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 282 | type BottomUpMutatorContext interface { |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 283 | BaseMutatorContext |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 284 | |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 285 | // AddDependency adds a dependency to the given module. It returns a slice of modules for each |
| 286 | // dependency (some entries may be nil). |
| 287 | // |
| 288 | // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the |
| 289 | // new dependencies have had the current mutator called on them. If the mutator is not |
| 290 | // parallel this method does not affect the ordering of the current mutator pass, but will |
| 291 | // be ordered correctly for all future mutator passes. |
| 292 | AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 293 | |
| 294 | // AddReverseDependency adds a dependency from the destination to the given module. |
| 295 | // Does not affect the ordering of the current mutator pass, but will be ordered |
| 296 | // correctly for all future mutator passes. All reverse dependencies for a destination module are |
| 297 | // collected until the end of the mutator pass, sorted by name, and then appended to the destination |
| 298 | // module's dependency list. |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 299 | AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 300 | |
| 301 | // CreateVariations splits a module into multiple variants, one for each name in the variationNames |
| 302 | // parameter. It returns a list of new modules in the same order as the variationNames |
| 303 | // list. |
| 304 | // |
| 305 | // If any of the dependencies of the module being operated on were already split |
| 306 | // by calling CreateVariations with the same name, the dependency will automatically |
| 307 | // be updated to point the matching variant. |
| 308 | // |
| 309 | // If a module is split, and then a module depending on the first module is not split |
| 310 | // when the Mutator is later called on it, the dependency of the depending module will |
| 311 | // automatically be updated to point to the first variant. |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 312 | CreateVariations(...string) []Module |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 313 | |
| 314 | // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames |
| 315 | // parameter. It returns a list of new modules in the same order as the variantNames |
| 316 | // list. |
| 317 | // |
| 318 | // Local variations do not affect automatic dependency resolution - dependencies added |
| 319 | // to the split module via deps or DynamicDependerModule must exactly match a variant |
| 320 | // that contains all the non-local variations. |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 321 | CreateLocalVariations(...string) []Module |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 322 | |
| 323 | // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation |
| 324 | // with given name. This function ignores the default variation set by SetDefaultDependencyVariation. |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 325 | SetDependencyVariation(string) |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 326 | |
| 327 | // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected |
| 328 | // during the subsequent calls on Create*Variations* functions. To reset, set it to nil. |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 329 | SetDefaultDependencyVariation(*string) |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 330 | |
| 331 | // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 332 | // argument to select which variant of the dependency to use. It returns a slice of modules for |
| 333 | // each dependency (some entries may be nil). A variant of the dependency must exist that matches |
| 334 | // the all of the non-local variations of the current module, plus the variations argument. |
| 335 | // |
| 336 | // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the |
| 337 | // new dependencies have had the current mutator called on them. If the mutator is not |
| 338 | // parallel this method does not affect the ordering of the current mutator pass, but will |
| 339 | // be ordered correctly for all future mutator passes. |
| 340 | AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 341 | |
| 342 | // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 343 | // variations argument to select which variant of the dependency to use. It returns a slice of |
| 344 | // modules for each dependency (some entries may be nil). A variant of the dependency must |
| 345 | // exist that matches the variations argument, but may also have other variations. |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 346 | // For any unspecified variation the first variant will be used. |
| 347 | // |
| 348 | // Unlike AddVariationDependencies, the variations of the current module are ignored - the |
| 349 | // dependency only needs to match the supplied variations. |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 350 | // |
| 351 | // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the |
| 352 | // new dependencies have had the current mutator called on them. If the mutator is not |
| 353 | // parallel this method does not affect the ordering of the current mutator pass, but will |
| 354 | // be ordered correctly for all future mutator passes. |
| 355 | AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 356 | |
| 357 | // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always |
| 358 | // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change |
| 359 | // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps, |
| 360 | // WalkDeps, etc. |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 361 | AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 362 | |
| 363 | // ReplaceDependencies replaces all dependencies on the identical variant of the module with the |
| 364 | // specified name with the current variant of this module. Replacements don't take effect until |
| 365 | // after the mutator pass is finished. |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 366 | ReplaceDependencies(string) |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 367 | |
| 368 | // ReplaceDependencies replaces all dependencies on the identical variant of the module with the |
| 369 | // specified name with the current variant of this module as long as the supplied predicate returns |
| 370 | // true. |
| 371 | // |
| 372 | // Replacements don't take effect until after the mutator pass is finished. |
Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 373 | ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate) |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 374 | |
| 375 | // AliasVariation takes a variationName that was passed to CreateVariations for this module, |
| 376 | // and creates an alias from the current variant (before the mutator has run) to the new |
| 377 | // variant. The alias will be valid until the next time a mutator calls CreateVariations or |
| 378 | // CreateLocalVariations on this module without also calling AliasVariation. The alias can |
| 379 | // be used to add dependencies on the newly created variant using the variant map from |
| 380 | // before CreateVariations was run. |
Jaewoong Jung | 9f88ce2 | 2019-11-15 10:57:34 -0800 | [diff] [blame] | 381 | AliasVariation(variationName string) |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 382 | |
| 383 | // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this |
| 384 | // module, and creates an alias from a new fromVariationName variant the toVariationName |
| 385 | // variant. The alias will be valid until the next time a mutator calls CreateVariations or |
| 386 | // CreateLocalVariations on this module without also calling AliasVariation. The alias can |
| 387 | // be used to add dependencies on the toVariationName variant using the fromVariationName |
| 388 | // variant. |
Colin Cross | 1b9604b | 2020-08-11 12:03:56 -0700 | [diff] [blame] | 389 | CreateAliasVariation(fromVariationName, toVariationName string) |
Colin Cross | d27e7b8 | 2020-07-02 11:38:17 -0700 | [diff] [blame] | 390 | |
| 391 | // SetVariationProvider sets the value for a provider for the given newly created variant of |
| 392 | // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if |
| 393 | // not called during the appropriate mutator or GenerateBuildActions pass for the provider, |
| 394 | // if the value is not of the appropriate type, or if the module is not a newly created |
| 395 | // variant of the current module. The value should not be modified after being passed to |
| 396 | // SetVariationProvider. |
| 397 | SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 398 | |
| 399 | // BazelConversionMode returns whether this mutator is being run as part of Bazel Conversion. |
| 400 | BazelConversionMode() bool |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 403 | type bottomUpMutatorContext struct { |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 404 | bp blueprint.BottomUpMutatorContext |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 405 | baseModuleContext |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 406 | finalPhase bool |
| 407 | bazelConversionMode bool |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 410 | func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 411 | finalPhase, bazelConversionMode bool) BottomUpMutatorContext { |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 412 | |
| 413 | return &bottomUpMutatorContext{ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 414 | bp: ctx, |
| 415 | baseModuleContext: a.base().baseModuleContextFactory(ctx), |
| 416 | finalPhase: finalPhase, |
| 417 | bazelConversionMode: bazelConversionMode, |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 421 | func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle { |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 422 | finalPhase := x.finalPhase |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 423 | bazelConversionMode := x.bazelConversionMode |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 424 | f := func(ctx blueprint.BottomUpMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 425 | if a, ok := ctx.Module().(Module); ok { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 426 | m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode)) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 427 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 428 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 429 | mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f} |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 430 | x.mutators = append(x.mutators, mutator) |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 431 | return mutator |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 434 | func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle { |
| 435 | mutator := &mutator{name: name, bottomUpMutator: m} |
| 436 | x.mutators = append(x.mutators, mutator) |
| 437 | return mutator |
| 438 | } |
| 439 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 440 | func (x *registerMutatorsContext) mutatorName(name string) string { |
| 441 | if x.bazelConversionMode { |
| 442 | return name + "_bp2build" |
| 443 | } |
| 444 | return name |
| 445 | } |
| 446 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 447 | func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 448 | f := func(ctx blueprint.TopDownMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 449 | if a, ok := ctx.Module().(Module); ok { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 450 | actx := &topDownMutatorContext{ |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 451 | bp: ctx, |
| 452 | baseModuleContext: a.base().baseModuleContextFactory(ctx), |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 453 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 454 | m(actx) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 455 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 456 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 457 | mutator := &mutator{name: x.mutatorName(name), topDownMutator: f} |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 458 | x.mutators = append(x.mutators, mutator) |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 459 | return mutator |
| 460 | } |
| 461 | |
Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame^] | 462 | func (mutator *mutator) componentName() string { |
| 463 | return mutator.name |
| 464 | } |
| 465 | |
| 466 | func (mutator *mutator) register(ctx *Context) { |
| 467 | blueprintCtx := ctx.Context |
| 468 | var handle blueprint.MutatorHandle |
| 469 | if mutator.bottomUpMutator != nil { |
| 470 | handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator) |
| 471 | } else if mutator.topDownMutator != nil { |
| 472 | handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator) |
| 473 | } |
| 474 | if mutator.parallel { |
| 475 | handle.Parallel() |
| 476 | } |
| 477 | } |
| 478 | |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 479 | type MutatorHandle interface { |
| 480 | Parallel() MutatorHandle |
| 481 | } |
| 482 | |
| 483 | func (mutator *mutator) Parallel() MutatorHandle { |
| 484 | mutator.parallel = true |
| 485 | return mutator |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 486 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 487 | |
Paul Duffin | 44f1d84 | 2020-06-26 20:17:02 +0100 | [diff] [blame] | 488 | func RegisterComponentsMutator(ctx RegisterMutatorsContext) { |
| 489 | ctx.BottomUp("component-deps", componentDepsMutator).Parallel() |
| 490 | } |
| 491 | |
| 492 | // A special mutator that runs just prior to the deps mutator to allow the dependencies |
| 493 | // on component modules to be added so that they can depend directly on a prebuilt |
| 494 | // module. |
| 495 | func componentDepsMutator(ctx BottomUpMutatorContext) { |
| 496 | if m := ctx.Module(); m.Enabled() { |
| 497 | m.ComponentDepsMutator(ctx) |
| 498 | } |
| 499 | } |
| 500 | |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 501 | func depsMutator(ctx BottomUpMutatorContext) { |
Paul Duffin | 44f1d84 | 2020-06-26 20:17:02 +0100 | [diff] [blame] | 502 | if m := ctx.Module(); m.Enabled() { |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 503 | m.DepsMutator(ctx) |
| 504 | } |
| 505 | } |
Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 506 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 507 | func registerDepsMutator(ctx RegisterMutatorsContext) { |
| 508 | ctx.BottomUp("deps", depsMutator).Parallel() |
| 509 | } |
| 510 | |
| 511 | func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) { |
| 512 | // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are |
| 513 | // being converted to build targets. |
| 514 | ctx.BottomUp("deps", depsMutator).Parallel() |
| 515 | } |
| 516 | |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 517 | func (t *topDownMutatorContext) CreateBazelTargetModule( |
| 518 | factory ModuleFactory, |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 519 | name string, |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 520 | bazelProps bazel.BazelTargetModuleProperties, |
| 521 | attrs interface{}) BazelTargetModule { |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 522 | if strings.HasPrefix(name, bazel.BazelTargetModuleNamePrefix) { |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 523 | panic(fmt.Errorf( |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 524 | "The %s name prefix is added automatically, do not set it manually: %s", |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 525 | bazel.BazelTargetModuleNamePrefix, |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 526 | name)) |
| 527 | } |
| 528 | name = bazel.BazelTargetModuleNamePrefix + name |
| 529 | nameProp := struct { |
| 530 | Name *string |
| 531 | }{ |
| 532 | Name: &name, |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 533 | } |
| 534 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 535 | b := t.CreateModule(factory, &nameProp, attrs).(BazelTargetModule) |
| 536 | b.SetBazelTargetModuleProperties(bazelProps) |
| 537 | return b |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 538 | } |
| 539 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 540 | func (t *topDownMutatorContext) AppendProperties(props ...interface{}) { |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 541 | for _, p := range props { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 542 | err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties, |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 543 | p, nil) |
| 544 | if err != nil { |
| 545 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 546 | t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 547 | } else { |
| 548 | panic(err) |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 554 | func (t *topDownMutatorContext) PrependProperties(props ...interface{}) { |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 555 | for _, p := range props { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 556 | err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties, |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 557 | p, nil) |
| 558 | if err != nil { |
| 559 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 560 | t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 561 | } else { |
| 562 | panic(err) |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 567 | |
| 568 | // android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that |
| 569 | // has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid |
| 570 | // ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every |
| 571 | // non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following |
| 572 | // methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext. |
| 573 | |
Colin Cross | cb55e08 | 2019-07-01 15:32:31 -0700 | [diff] [blame] | 574 | func (t *topDownMutatorContext) MutatorName() string { |
| 575 | return t.bp.MutatorName() |
| 576 | } |
| 577 | |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 578 | func (t *topDownMutatorContext) Rename(name string) { |
| 579 | t.bp.Rename(name) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 580 | t.Module().base().commonProperties.DebugName = name |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Colin Cross | e003c4a | 2019-09-25 12:58:36 -0700 | [diff] [blame] | 583 | func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module { |
Colin Cross | 18c4680 | 2019-09-24 22:19:02 -0700 | [diff] [blame] | 584 | inherited := []interface{}{&t.Module().base().commonProperties} |
Colin Cross | e003c4a | 2019-09-25 12:58:36 -0700 | [diff] [blame] | 585 | module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module) |
Colin Cross | 18c4680 | 2019-09-24 22:19:02 -0700 | [diff] [blame] | 586 | |
| 587 | if t.Module().base().variableProperties != nil && module.base().variableProperties != nil { |
| 588 | src := t.Module().base().variableProperties |
| 589 | dst := []interface{}{ |
| 590 | module.base().variableProperties, |
| 591 | // Put an empty copy of the src properties into dst so that properties in src that are not in dst |
| 592 | // don't cause a "failed to find property to extend" error. |
Colin Cross | 43e789d | 2020-01-28 09:46:50 -0800 | [diff] [blame] | 593 | proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(), |
Colin Cross | 18c4680 | 2019-09-24 22:19:02 -0700 | [diff] [blame] | 594 | } |
| 595 | err := proptools.AppendMatchingProperties(dst, src, nil) |
| 596 | if err != nil { |
| 597 | panic(err) |
| 598 | } |
| 599 | } |
| 600 | |
Colin Cross | e003c4a | 2019-09-25 12:58:36 -0700 | [diff] [blame] | 601 | return module |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Colin Cross | cb55e08 | 2019-07-01 15:32:31 -0700 | [diff] [blame] | 604 | func (b *bottomUpMutatorContext) MutatorName() string { |
| 605 | return b.bp.MutatorName() |
| 606 | } |
| 607 | |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 608 | func (b *bottomUpMutatorContext) Rename(name string) { |
| 609 | b.bp.Rename(name) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 610 | b.Module().base().commonProperties.DebugName = name |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 613 | func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module { |
| 614 | return b.bp.AddDependency(module, tag, name...) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) { |
| 618 | b.bp.AddReverseDependency(module, tag, name) |
| 619 | } |
| 620 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 621 | func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module { |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 622 | if b.finalPhase { |
| 623 | panic("CreateVariations not allowed in FinalDepsMutators") |
| 624 | } |
| 625 | |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 626 | modules := b.bp.CreateVariations(variations...) |
| 627 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 628 | aModules := make([]Module, len(modules)) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 629 | for i := range variations { |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 630 | aModules[i] = modules[i].(Module) |
| 631 | base := aModules[i].base() |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 632 | base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName()) |
| 633 | base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i]) |
| 634 | } |
| 635 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 636 | return aModules |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 639 | func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module { |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 640 | if b.finalPhase { |
| 641 | panic("CreateLocalVariations not allowed in FinalDepsMutators") |
| 642 | } |
| 643 | |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 644 | modules := b.bp.CreateLocalVariations(variations...) |
| 645 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 646 | aModules := make([]Module, len(modules)) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 647 | for i := range variations { |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 648 | aModules[i] = modules[i].(Module) |
| 649 | base := aModules[i].base() |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 650 | base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName()) |
| 651 | base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i]) |
| 652 | } |
| 653 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 654 | return aModules |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) { |
| 658 | b.bp.SetDependencyVariation(variation) |
| 659 | } |
| 660 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 661 | func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) { |
| 662 | b.bp.SetDefaultDependencyVariation(variation) |
| 663 | } |
| 664 | |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 665 | func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 666 | names ...string) []blueprint.Module { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 667 | if b.bazelConversionMode { |
| 668 | _, noSelfDeps := RemoveFromList(b.ModuleName(), names) |
| 669 | if len(noSelfDeps) == 0 { |
| 670 | return []blueprint.Module(nil) |
| 671 | } |
| 672 | // In Bazel conversion mode, mutators should not have created any variants. So, when adding a |
| 673 | // dependency, the variations would not exist and the dependency could not be added, by |
| 674 | // specifying no variations, we will allow adding the dependency to succeed. |
| 675 | return b.bp.AddFarVariationDependencies(nil, tag, noSelfDeps...) |
| 676 | } |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 677 | |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 678 | return b.bp.AddVariationDependencies(variations, tag, names...) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation, |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 682 | tag blueprint.DependencyTag, names ...string) []blueprint.Module { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 683 | if b.bazelConversionMode { |
| 684 | // In Bazel conversion mode, mutators should not have created any variants. So, when adding a |
| 685 | // dependency, the variations would not exist and the dependency could not be added, by |
| 686 | // specifying no variations, we will allow adding the dependency to succeed. |
| 687 | return b.bp.AddFarVariationDependencies(nil, tag, names...) |
| 688 | } |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 689 | |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 690 | return b.bp.AddFarVariationDependencies(variations, tag, names...) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) { |
| 694 | b.bp.AddInterVariantDependency(tag, from, to) |
| 695 | } |
| 696 | |
| 697 | func (b *bottomUpMutatorContext) ReplaceDependencies(name string) { |
| 698 | b.bp.ReplaceDependencies(name) |
| 699 | } |
Jaewoong Jung | 9f88ce2 | 2019-11-15 10:57:34 -0800 | [diff] [blame] | 700 | |
Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 701 | func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) { |
| 702 | b.bp.ReplaceDependenciesIf(name, predicate) |
| 703 | } |
| 704 | |
Jaewoong Jung | 9f88ce2 | 2019-11-15 10:57:34 -0800 | [diff] [blame] | 705 | func (b *bottomUpMutatorContext) AliasVariation(variationName string) { |
| 706 | b.bp.AliasVariation(variationName) |
| 707 | } |
Colin Cross | 1b9604b | 2020-08-11 12:03:56 -0700 | [diff] [blame] | 708 | |
| 709 | func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) { |
| 710 | b.bp.CreateAliasVariation(fromVariationName, toVariationName) |
| 711 | } |
Colin Cross | d27e7b8 | 2020-07-02 11:38:17 -0700 | [diff] [blame] | 712 | |
| 713 | func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) { |
| 714 | b.bp.SetVariationProvider(module, provider, value) |
| 715 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 716 | |
| 717 | func (b *bottomUpMutatorContext) BazelConversionMode() bool { |
| 718 | return b.bazelConversionMode |
| 719 | } |