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