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 ( |
Colin Cross | 984223f | 2024-02-01 17:10:23 -0800 | [diff] [blame] | 18 | "sync" |
| 19 | |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 20 | "github.com/google/blueprint" |
| 21 | ) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 22 | |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 23 | // Phases: |
| 24 | // run Pre-arch mutators |
| 25 | // run archMutator |
| 26 | // run Pre-deps mutators |
| 27 | // run depsMutator |
| 28 | // run PostDeps mutators |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 29 | // run FinalDeps mutators (CreateVariations disallowed in this phase) |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 30 | // continue on to GenerateAndroidBuildActions |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 31 | |
Paul Duffin | c05b034 | 2021-03-06 13:28:13 +0000 | [diff] [blame] | 32 | // collateGloballyRegisteredMutators constructs the list of mutators that have been registered |
| 33 | // with the InitRegistrationContext and will be used at runtime. |
| 34 | func collateGloballyRegisteredMutators() sortableComponents { |
| 35 | return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps) |
| 36 | } |
| 37 | |
| 38 | // collateRegisteredMutators constructs a single list of mutators from the separate lists. |
| 39 | func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 40 | mctx := ®isterMutatorsContext{} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 41 | |
| 42 | register := func(funcs []RegisterMutatorFunc) { |
| 43 | for _, f := range funcs { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 44 | f(mctx) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 48 | register(preArch) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 49 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 50 | register(preDeps) |
| 51 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 52 | register([]RegisterMutatorFunc{registerDepsMutator}) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 53 | |
| 54 | register(postDeps) |
| 55 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 56 | mctx.finalPhase = true |
| 57 | register(finalDeps) |
| 58 | |
Paul Duffin | c05b034 | 2021-03-06 13:28:13 +0000 | [diff] [blame] | 59 | return mctx.mutators |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | type registerMutatorsContext struct { |
Colin Cross | b63d7b3 | 2023-12-07 16:54:51 -0800 | [diff] [blame] | 63 | mutators sortableComponents |
| 64 | finalPhase bool |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 65 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 66 | |
| 67 | type RegisterMutatorsContext interface { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 68 | TopDown(name string, m TopDownMutator) MutatorHandle |
| 69 | BottomUp(name string, m BottomUpMutator) MutatorHandle |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 70 | BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 71 | Transition(name string, m TransitionMutator) |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | type RegisterMutatorFunc func(RegisterMutatorsContext) |
| 75 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 76 | var preArch = []RegisterMutatorFunc{ |
Dan Willemsen | 6e72ef7 | 2018-01-26 18:27:02 -0800 | [diff] [blame] | 77 | RegisterNamespaceMutator, |
Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 78 | |
Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 79 | // Check the visibility rules are valid. |
| 80 | // |
| 81 | // This must run after the package renamer mutators so that any issues found during |
| 82 | // validation of the package's default_visibility property are reported using the |
| 83 | // correct package name and not the synthetic name. |
| 84 | // |
| 85 | // This must also be run before defaults mutators as the rules for validation are |
| 86 | // different before checking the rules than they are afterwards. e.g. |
| 87 | // visibility: ["//visibility:private", "//visibility:public"] |
| 88 | // would be invalid if specified in a module definition but is valid if it results |
| 89 | // from something like this: |
| 90 | // |
| 91 | // defaults { |
| 92 | // name: "defaults", |
| 93 | // // Be inaccessible outside a package by default. |
| 94 | // visibility: ["//visibility:private"] |
| 95 | // } |
| 96 | // |
| 97 | // defaultable_module { |
| 98 | // name: "defaultable_module", |
| 99 | // defaults: ["defaults"], |
| 100 | // // Override the default. |
| 101 | // visibility: ["//visibility:public"] |
| 102 | // } |
| 103 | // |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 104 | RegisterVisibilityRuleChecker, |
Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 105 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 106 | // Record the default_applicable_licenses for each package. |
| 107 | // |
| 108 | // This must run before the defaults so that defaults modules can pick up the package default. |
| 109 | RegisterLicensesPackageMapper, |
| 110 | |
Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 111 | // Apply properties from defaults modules to the referencing modules. |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 112 | // |
| 113 | // Any mutators that are added before this will not see any modules created by |
| 114 | // a DefaultableHook. |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 115 | RegisterDefaultsPreArchMutators, |
Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 116 | |
Paul Duffin | 44f1d84 | 2020-06-26 20:17:02 +0100 | [diff] [blame] | 117 | // Add dependencies on any components so that any component references can be |
| 118 | // resolved within the deps mutator. |
| 119 | // |
| 120 | // Must be run after defaults so it can be used to create dependencies on the |
| 121 | // component modules that are creating in a DefaultableHook. |
| 122 | // |
| 123 | // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are |
| 124 | // renamed. That is so that if a module creates components using a prebuilt module |
| 125 | // type that any dependencies (which must use prebuilt_ prefixes) are resolved to |
| 126 | // the prebuilt module and not the source module. |
| 127 | RegisterComponentsMutator, |
| 128 | |
Paul Duffin | c988c8e | 2020-04-29 18:27:14 +0100 | [diff] [blame] | 129 | // Create an association between prebuilt modules and their corresponding source |
| 130 | // modules (if any). |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 131 | // |
| 132 | // Must be run after defaults mutators to ensure that any modules created by |
| 133 | // a DefaultableHook can be either a prebuilt or a source module with a matching |
| 134 | // prebuilt. |
Paul Duffin | c988c8e | 2020-04-29 18:27:14 +0100 | [diff] [blame] | 135 | RegisterPrebuiltsPreArchMutators, |
| 136 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 137 | // Gather the licenses properties for all modules for use during expansion and enforcement. |
| 138 | // |
| 139 | // This must come after the defaults mutators to ensure that any licenses supplied |
| 140 | // in a defaults module has been successfully applied before the rules are gathered. |
| 141 | RegisterLicensesPropertyGatherer, |
| 142 | |
Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 143 | // Gather the visibility rules for all modules for us during visibility enforcement. |
| 144 | // |
| 145 | // This must come after the defaults mutators to ensure that any visibility supplied |
| 146 | // 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] | 147 | RegisterVisibilityRuleGatherer, |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 150 | func registerArchMutator(ctx RegisterMutatorsContext) { |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 151 | ctx.BottomUpBlueprint("os", osMutator).Parallel() |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 152 | ctx.BottomUp("image", imageMutator).Parallel() |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 153 | ctx.BottomUpBlueprint("arch", archMutator).Parallel() |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 156 | var preDeps = []RegisterMutatorFunc{ |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 157 | registerArchMutator, |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | var postDeps = []RegisterMutatorFunc{ |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 161 | registerPathDepsMutator, |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 162 | RegisterPrebuiltsPostDepsMutators, |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 163 | RegisterVisibilityRuleEnforcer, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 164 | RegisterLicensesDependencyChecker, |
Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 165 | registerNeverallowMutator, |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 166 | RegisterOverridePostDepsMutators, |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 167 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 168 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 169 | var finalDeps = []RegisterMutatorFunc{} |
| 170 | |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 171 | func PreArchMutators(f RegisterMutatorFunc) { |
| 172 | preArch = append(preArch, f) |
| 173 | } |
| 174 | |
| 175 | func PreDepsMutators(f RegisterMutatorFunc) { |
| 176 | preDeps = append(preDeps, f) |
| 177 | } |
| 178 | |
| 179 | func PostDepsMutators(f RegisterMutatorFunc) { |
| 180 | postDeps = append(postDeps, f) |
| 181 | } |
| 182 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 183 | func FinalDepsMutators(f RegisterMutatorFunc) { |
| 184 | finalDeps = append(finalDeps, f) |
| 185 | } |
| 186 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 187 | type BaseMutatorContext interface { |
| 188 | BaseModuleContext |
| 189 | |
| 190 | // MutatorName returns the name that this mutator was registered with. |
| 191 | MutatorName() string |
| 192 | |
| 193 | // Rename all variants of a module. The new name is not visible to calls to ModuleName, |
| 194 | // AddDependency or OtherModuleName until after this mutator pass is complete. |
| 195 | Rename(name string) |
| 196 | } |
| 197 | |
| 198 | type TopDownMutator func(TopDownMutatorContext) |
| 199 | |
| 200 | type TopDownMutatorContext interface { |
| 201 | BaseMutatorContext |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 202 | |
| 203 | // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies |
| 204 | // the specified property structs to it as if the properties were set in a blueprint file. |
| 205 | CreateModule(ModuleFactory, ...interface{}) Module |
| 206 | } |
| 207 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 208 | type topDownMutatorContext struct { |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 209 | bp blueprint.TopDownMutatorContext |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 210 | baseModuleContext |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 213 | type BottomUpMutator func(BottomUpMutatorContext) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 214 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 215 | type BottomUpMutatorContext interface { |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 216 | BaseMutatorContext |
Colin Cross | b63d7b3 | 2023-12-07 16:54:51 -0800 | [diff] [blame] | 217 | |
| 218 | // AddDependency adds a dependency to the given module. It returns a slice of modules for each |
| 219 | // dependency (some entries may be nil). |
| 220 | // |
| 221 | // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the |
| 222 | // new dependencies have had the current mutator called on them. If the mutator is not |
| 223 | // parallel this method does not affect the ordering of the current mutator pass, but will |
| 224 | // be ordered correctly for all future mutator passes. |
| 225 | AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 226 | |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 227 | // AddReverseDependency adds a dependency from the destination to the given module. |
| 228 | // Does not affect the ordering of the current mutator pass, but will be ordered |
| 229 | // correctly for all future mutator passes. All reverse dependencies for a destination module are |
| 230 | // collected until the end of the mutator pass, sorted by name, and then appended to the destination |
| 231 | // module's dependency list. |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 232 | AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 233 | |
| 234 | // CreateVariations splits a module into multiple variants, one for each name in the variationNames |
| 235 | // parameter. It returns a list of new modules in the same order as the variationNames |
| 236 | // list. |
| 237 | // |
| 238 | // If any of the dependencies of the module being operated on were already split |
| 239 | // by calling CreateVariations with the same name, the dependency will automatically |
| 240 | // be updated to point the matching variant. |
| 241 | // |
| 242 | // If a module is split, and then a module depending on the first module is not split |
| 243 | // when the Mutator is later called on it, the dependency of the depending module will |
| 244 | // automatically be updated to point to the first variant. |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 245 | CreateVariations(...string) []Module |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 246 | |
| 247 | // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames |
| 248 | // parameter. It returns a list of new modules in the same order as the variantNames |
| 249 | // list. |
| 250 | // |
| 251 | // Local variations do not affect automatic dependency resolution - dependencies added |
| 252 | // to the split module via deps or DynamicDependerModule must exactly match a variant |
| 253 | // that contains all the non-local variations. |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 254 | CreateLocalVariations(...string) []Module |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 255 | |
| 256 | // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation |
| 257 | // with given name. This function ignores the default variation set by SetDefaultDependencyVariation. |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 258 | SetDependencyVariation(string) |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 259 | |
| 260 | // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected |
| 261 | // 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] | 262 | SetDefaultDependencyVariation(*string) |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 263 | |
| 264 | // 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] | 265 | // argument to select which variant of the dependency to use. It returns a slice of modules for |
| 266 | // each dependency (some entries may be nil). A variant of the dependency must exist that matches |
Usta Shrestha | c725f47 | 2022-01-11 02:44:21 -0500 | [diff] [blame] | 267 | // all the non-local variations of the current module, plus the variations argument. |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 268 | // |
| 269 | // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the |
| 270 | // new dependencies have had the current mutator called on them. If the mutator is not |
| 271 | // parallel this method does not affect the ordering of the current mutator pass, but will |
| 272 | // be ordered correctly for all future mutator passes. |
Usta Shrestha | c725f47 | 2022-01-11 02:44:21 -0500 | [diff] [blame] | 273 | AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []blueprint.Module |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 274 | |
| 275 | // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 276 | // variations argument to select which variant of the dependency to use. It returns a slice of |
| 277 | // modules for each dependency (some entries may be nil). A variant of the dependency must |
| 278 | // exist that matches the variations argument, but may also have other variations. |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 279 | // For any unspecified variation the first variant will be used. |
| 280 | // |
| 281 | // Unlike AddVariationDependencies, the variations of the current module are ignored - the |
| 282 | // dependency only needs to match the supplied variations. |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 283 | // |
| 284 | // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the |
| 285 | // new dependencies have had the current mutator called on them. If the mutator is not |
| 286 | // parallel this method does not affect the ordering of the current mutator pass, but will |
| 287 | // be ordered correctly for all future mutator passes. |
| 288 | AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 289 | |
| 290 | // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always |
| 291 | // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change |
| 292 | // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps, |
| 293 | // WalkDeps, etc. |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 294 | AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 295 | |
| 296 | // ReplaceDependencies replaces all dependencies on the identical variant of the module with the |
| 297 | // specified name with the current variant of this module. Replacements don't take effect until |
| 298 | // after the mutator pass is finished. |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 299 | ReplaceDependencies(string) |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 300 | |
| 301 | // ReplaceDependencies replaces all dependencies on the identical variant of the module with the |
| 302 | // specified name with the current variant of this module as long as the supplied predicate returns |
| 303 | // true. |
| 304 | // |
| 305 | // Replacements don't take effect until after the mutator pass is finished. |
Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 306 | ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate) |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 307 | |
| 308 | // AliasVariation takes a variationName that was passed to CreateVariations for this module, |
| 309 | // and creates an alias from the current variant (before the mutator has run) to the new |
| 310 | // variant. The alias will be valid until the next time a mutator calls CreateVariations or |
| 311 | // CreateLocalVariations on this module without also calling AliasVariation. The alias can |
| 312 | // be used to add dependencies on the newly created variant using the variant map from |
| 313 | // before CreateVariations was run. |
Jaewoong Jung | 9f88ce2 | 2019-11-15 10:57:34 -0800 | [diff] [blame] | 314 | AliasVariation(variationName string) |
Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 315 | |
| 316 | // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this |
| 317 | // module, and creates an alias from a new fromVariationName variant the toVariationName |
| 318 | // variant. The alias will be valid until the next time a mutator calls CreateVariations or |
| 319 | // CreateLocalVariations on this module without also calling AliasVariation. The alias can |
| 320 | // be used to add dependencies on the toVariationName variant using the fromVariationName |
| 321 | // variant. |
Colin Cross | 1b9604b | 2020-08-11 12:03:56 -0700 | [diff] [blame] | 322 | CreateAliasVariation(fromVariationName, toVariationName string) |
Colin Cross | d27e7b8 | 2020-07-02 11:38:17 -0700 | [diff] [blame] | 323 | |
| 324 | // SetVariationProvider sets the value for a provider for the given newly created variant of |
| 325 | // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if |
| 326 | // not called during the appropriate mutator or GenerateBuildActions pass for the provider, |
| 327 | // if the value is not of the appropriate type, or if the module is not a newly created |
| 328 | // variant of the current module. The value should not be modified after being passed to |
| 329 | // SetVariationProvider. |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 330 | SetVariationProvider(module blueprint.Module, provider blueprint.AnyProviderKey, value interface{}) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Colin Cross | 984223f | 2024-02-01 17:10:23 -0800 | [diff] [blame] | 333 | // An outgoingTransitionContextImpl and incomingTransitionContextImpl is created for every dependency of every module |
| 334 | // for each transition mutator. bottomUpMutatorContext and topDownMutatorContext are created once for every module |
| 335 | // for every BottomUp or TopDown mutator. Use a global pool for each to avoid reallocating every time. |
| 336 | var ( |
| 337 | outgoingTransitionContextPool = sync.Pool{ |
| 338 | New: func() any { return &outgoingTransitionContextImpl{} }, |
| 339 | } |
| 340 | incomingTransitionContextPool = sync.Pool{ |
| 341 | New: func() any { return &incomingTransitionContextImpl{} }, |
| 342 | } |
| 343 | bottomUpMutatorContextPool = sync.Pool{ |
| 344 | New: func() any { return &bottomUpMutatorContext{} }, |
| 345 | } |
| 346 | |
| 347 | topDownMutatorContextPool = sync.Pool{ |
| 348 | New: func() any { return &topDownMutatorContext{} }, |
| 349 | } |
| 350 | ) |
| 351 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 352 | type bottomUpMutatorContext struct { |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 353 | bp blueprint.BottomUpMutatorContext |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 354 | baseModuleContext |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 355 | finalPhase bool |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Colin Cross | 984223f | 2024-02-01 17:10:23 -0800 | [diff] [blame] | 358 | // callers must immediately follow the call to this function with defer bottomUpMutatorContextPool.Put(mctx). |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 359 | func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module, |
Colin Cross | b63d7b3 | 2023-12-07 16:54:51 -0800 | [diff] [blame] | 360 | finalPhase bool) BottomUpMutatorContext { |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 361 | |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 362 | moduleContext := a.base().baseModuleContextFactory(ctx) |
Colin Cross | 984223f | 2024-02-01 17:10:23 -0800 | [diff] [blame] | 363 | mctx := bottomUpMutatorContextPool.Get().(*bottomUpMutatorContext) |
| 364 | *mctx = bottomUpMutatorContext{ |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 365 | bp: ctx, |
Cole Faust | 0abd4b4 | 2023-01-10 10:49:18 -0800 | [diff] [blame] | 366 | baseModuleContext: moduleContext, |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 367 | finalPhase: finalPhase, |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 368 | } |
Colin Cross | 984223f | 2024-02-01 17:10:23 -0800 | [diff] [blame] | 369 | return mctx |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 372 | func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle { |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 373 | finalPhase := x.finalPhase |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 374 | f := func(ctx blueprint.BottomUpMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 375 | if a, ok := ctx.Module().(Module); ok { |
Colin Cross | 984223f | 2024-02-01 17:10:23 -0800 | [diff] [blame] | 376 | mctx := bottomUpMutatorContextFactory(ctx, a, finalPhase) |
| 377 | defer bottomUpMutatorContextPool.Put(mctx) |
| 378 | m(mctx) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 379 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 380 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 381 | mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f} |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 382 | x.mutators = append(x.mutators, mutator) |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 383 | return mutator |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 386 | func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle { |
| 387 | mutator := &mutator{name: name, bottomUpMutator: m} |
| 388 | x.mutators = append(x.mutators, mutator) |
| 389 | return mutator |
| 390 | } |
| 391 | |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 392 | type IncomingTransitionContext interface { |
Colin Cross | 4aa3e0a | 2024-01-18 17:22:58 -0800 | [diff] [blame] | 393 | ArchModuleContext |
| 394 | |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 395 | // Module returns the target of the dependency edge for which the transition |
| 396 | // is being computed |
| 397 | Module() Module |
| 398 | |
| 399 | // Config returns the configuration for the build. |
| 400 | Config() Config |
Colin Cross | 4aa3e0a | 2024-01-18 17:22:58 -0800 | [diff] [blame] | 401 | |
| 402 | DeviceConfig() DeviceConfig |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | type OutgoingTransitionContext interface { |
Colin Cross | 4aa3e0a | 2024-01-18 17:22:58 -0800 | [diff] [blame] | 406 | ArchModuleContext |
| 407 | |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 408 | // Module returns the target of the dependency edge for which the transition |
| 409 | // is being computed |
| 410 | Module() Module |
| 411 | |
| 412 | // DepTag() Returns the dependency tag through which this dependency is |
| 413 | // reached |
| 414 | DepTag() blueprint.DependencyTag |
Colin Cross | 4aa3e0a | 2024-01-18 17:22:58 -0800 | [diff] [blame] | 415 | |
| 416 | // Config returns the configuration for the build. |
| 417 | Config() Config |
| 418 | |
| 419 | DeviceConfig() DeviceConfig |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 420 | } |
Lukacs T. Berki | 0e691c1 | 2022-06-24 10:15:55 +0200 | [diff] [blame] | 421 | |
Colin Cross | 4aa3e0a | 2024-01-18 17:22:58 -0800 | [diff] [blame] | 422 | // TransitionMutator implements a top-down mechanism where a module tells its |
Lukacs T. Berki | 0e691c1 | 2022-06-24 10:15:55 +0200 | [diff] [blame] | 423 | // direct dependencies what variation they should be built in but the dependency |
| 424 | // has the final say. |
| 425 | // |
| 426 | // When implementing a transition mutator, one needs to implement four methods: |
| 427 | // - Split() that tells what variations a module has by itself |
| 428 | // - OutgoingTransition() where a module tells what it wants from its |
| 429 | // dependency |
| 430 | // - IncomingTransition() where a module has the final say about its own |
| 431 | // variation |
| 432 | // - Mutate() that changes the state of a module depending on its variation |
| 433 | // |
| 434 | // That the effective variation of module B when depended on by module A is the |
| 435 | // composition the outgoing transition of module A and the incoming transition |
| 436 | // of module B. |
| 437 | // |
| 438 | // the outgoing transition should not take the properties of the dependency into |
| 439 | // account, only those of the module that depends on it. For this reason, the |
| 440 | // dependency is not even passed into it as an argument. Likewise, the incoming |
| 441 | // transition should not take the properties of the depending module into |
| 442 | // account and is thus not informed about it. This makes for a nice |
| 443 | // decomposition of the decision logic. |
| 444 | // |
| 445 | // A given transition mutator only affects its own variation; other variations |
| 446 | // stay unchanged along the dependency edges. |
| 447 | // |
| 448 | // Soong makes sure that all modules are created in the desired variations and |
| 449 | // that dependency edges are set up correctly. This ensures that "missing |
| 450 | // variation" errors do not happen and allows for more flexible changes in the |
| 451 | // value of the variation among dependency edges (as oppposed to bottom-up |
| 452 | // mutators where if module A in variation X depends on module B and module B |
| 453 | // has that variation X, A must depend on variation X of B) |
| 454 | // |
| 455 | // The limited power of the context objects passed to individual mutators |
| 456 | // methods also makes it more difficult to shoot oneself in the foot. Complete |
| 457 | // safety is not guaranteed because no one prevents individual transition |
| 458 | // mutators from mutating modules in illegal ways and for e.g. Split() or |
| 459 | // Mutate() to run their own visitations of the transitive dependency of the |
| 460 | // module and both of these are bad ideas, but it's better than no guardrails at |
| 461 | // all. |
| 462 | // |
| 463 | // This model is pretty close to Bazel's configuration transitions. The mapping |
| 464 | // between concepts in Soong and Bazel is as follows: |
| 465 | // - Module == configured target |
| 466 | // - Variant == configuration |
| 467 | // - Variation name == configuration flag |
| 468 | // - Variation == configuration flag value |
| 469 | // - Outgoing transition == attribute transition |
| 470 | // - Incoming transition == rule transition |
| 471 | // |
| 472 | // The Split() method does not have a Bazel equivalent and Bazel split |
| 473 | // transitions do not have a Soong equivalent. |
| 474 | // |
| 475 | // Mutate() does not make sense in Bazel due to the different models of the |
| 476 | // two systems: when creating new variations, Soong clones the old module and |
| 477 | // thus some way is needed to change it state whereas Bazel creates each |
| 478 | // configuration of a given configured target anew. |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 479 | type TransitionMutator interface { |
| 480 | // Split returns the set of variations that should be created for a module no |
| 481 | // matter who depends on it. Used when Make depends on a particular variation |
| 482 | // or when the module knows its variations just based on information given to |
| 483 | // it in the Blueprint file. This method should not mutate the module it is |
| 484 | // called on. |
| 485 | Split(ctx BaseModuleContext) []string |
| 486 | |
Colin Cross | 4aa3e0a | 2024-01-18 17:22:58 -0800 | [diff] [blame] | 487 | // OutgoingTransition is called on a module to determine which variation it wants |
| 488 | // from its direct dependencies. The dependency itself can override this decision. |
| 489 | // This method should not mutate the module itself. |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 490 | OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string |
| 491 | |
Colin Cross | 4aa3e0a | 2024-01-18 17:22:58 -0800 | [diff] [blame] | 492 | // IncomingTransition is called on a module to determine which variation it should |
| 493 | // be in based on the variation modules that depend on it want. This gives the module |
| 494 | // a final say about its own variations. This method should not mutate the module |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 495 | // itself. |
| 496 | IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string |
| 497 | |
Colin Cross | 4aa3e0a | 2024-01-18 17:22:58 -0800 | [diff] [blame] | 498 | // Mutate is called after a module was split into multiple variations on each variation. |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 499 | // It should not split the module any further but adding new dependencies is |
| 500 | // fine. Unlike all the other methods on TransitionMutator, this method is |
| 501 | // allowed to mutate the module. |
| 502 | Mutate(ctx BottomUpMutatorContext, variation string) |
| 503 | } |
| 504 | |
| 505 | type androidTransitionMutator struct { |
Colin Cross | b63d7b3 | 2023-12-07 16:54:51 -0800 | [diff] [blame] | 506 | finalPhase bool |
| 507 | mutator TransitionMutator |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []string { |
| 511 | if m, ok := ctx.Module().(Module); ok { |
| 512 | moduleContext := m.base().baseModuleContextFactory(ctx) |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 513 | return a.mutator.Split(&moduleContext) |
| 514 | } else { |
| 515 | return []string{""} |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | type outgoingTransitionContextImpl struct { |
Colin Cross | 4aa3e0a | 2024-01-18 17:22:58 -0800 | [diff] [blame] | 520 | archModuleContext |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 521 | bp blueprint.OutgoingTransitionContext |
| 522 | } |
| 523 | |
| 524 | func (c *outgoingTransitionContextImpl) Module() Module { |
| 525 | return c.bp.Module().(Module) |
| 526 | } |
| 527 | |
| 528 | func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag { |
| 529 | return c.bp.DepTag() |
| 530 | } |
| 531 | |
Colin Cross | 4aa3e0a | 2024-01-18 17:22:58 -0800 | [diff] [blame] | 532 | func (c *outgoingTransitionContextImpl) Config() Config { |
| 533 | return c.bp.Config().(Config) |
| 534 | } |
| 535 | |
| 536 | func (c *outgoingTransitionContextImpl) DeviceConfig() DeviceConfig { |
| 537 | return DeviceConfig{c.bp.Config().(Config).deviceConfig} |
| 538 | } |
| 539 | |
| 540 | func (a *androidTransitionMutator) OutgoingTransition(bpctx blueprint.OutgoingTransitionContext, sourceVariation string) string { |
| 541 | if m, ok := bpctx.Module().(Module); ok { |
Colin Cross | 984223f | 2024-02-01 17:10:23 -0800 | [diff] [blame] | 542 | ctx := outgoingTransitionContextPool.Get().(*outgoingTransitionContextImpl) |
| 543 | defer outgoingTransitionContextPool.Put(ctx) |
| 544 | *ctx = outgoingTransitionContextImpl{ |
Colin Cross | 4aa3e0a | 2024-01-18 17:22:58 -0800 | [diff] [blame] | 545 | archModuleContext: m.base().archModuleContextFactory(bpctx), |
| 546 | bp: bpctx, |
| 547 | } |
| 548 | return a.mutator.OutgoingTransition(ctx, sourceVariation) |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 549 | } else { |
| 550 | return "" |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | type incomingTransitionContextImpl struct { |
Colin Cross | 4aa3e0a | 2024-01-18 17:22:58 -0800 | [diff] [blame] | 555 | archModuleContext |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 556 | bp blueprint.IncomingTransitionContext |
| 557 | } |
| 558 | |
| 559 | func (c *incomingTransitionContextImpl) Module() Module { |
| 560 | return c.bp.Module().(Module) |
| 561 | } |
| 562 | |
| 563 | func (c *incomingTransitionContextImpl) Config() Config { |
| 564 | return c.bp.Config().(Config) |
| 565 | } |
| 566 | |
Colin Cross | 4aa3e0a | 2024-01-18 17:22:58 -0800 | [diff] [blame] | 567 | func (c *incomingTransitionContextImpl) DeviceConfig() DeviceConfig { |
| 568 | return DeviceConfig{c.bp.Config().(Config).deviceConfig} |
| 569 | } |
| 570 | |
| 571 | func (a *androidTransitionMutator) IncomingTransition(bpctx blueprint.IncomingTransitionContext, incomingVariation string) string { |
| 572 | if m, ok := bpctx.Module().(Module); ok { |
Colin Cross | 984223f | 2024-02-01 17:10:23 -0800 | [diff] [blame] | 573 | ctx := incomingTransitionContextPool.Get().(*incomingTransitionContextImpl) |
| 574 | defer incomingTransitionContextPool.Put(ctx) |
| 575 | *ctx = incomingTransitionContextImpl{ |
Colin Cross | 4aa3e0a | 2024-01-18 17:22:58 -0800 | [diff] [blame] | 576 | archModuleContext: m.base().archModuleContextFactory(bpctx), |
| 577 | bp: bpctx, |
| 578 | } |
| 579 | return a.mutator.IncomingTransition(ctx, incomingVariation) |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 580 | } else { |
| 581 | return "" |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | func (a *androidTransitionMutator) Mutate(ctx blueprint.BottomUpMutatorContext, variation string) { |
| 586 | if am, ok := ctx.Module().(Module); ok { |
Colin Cross | 984223f | 2024-02-01 17:10:23 -0800 | [diff] [blame] | 587 | mctx := bottomUpMutatorContextFactory(ctx, am, a.finalPhase) |
| 588 | defer bottomUpMutatorContextPool.Put(mctx) |
| 589 | a.mutator.Mutate(mctx, variation) |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
| 593 | func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) { |
| 594 | atm := &androidTransitionMutator{ |
Colin Cross | b63d7b3 | 2023-12-07 16:54:51 -0800 | [diff] [blame] | 595 | finalPhase: x.finalPhase, |
| 596 | mutator: m, |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 597 | } |
| 598 | mutator := &mutator{ |
| 599 | name: name, |
| 600 | transitionMutator: atm} |
| 601 | x.mutators = append(x.mutators, mutator) |
| 602 | } |
| 603 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 604 | func (x *registerMutatorsContext) mutatorName(name string) string { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 605 | return name |
| 606 | } |
| 607 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 608 | func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 609 | f := func(ctx blueprint.TopDownMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 610 | if a, ok := ctx.Module().(Module); ok { |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 611 | moduleContext := a.base().baseModuleContextFactory(ctx) |
Colin Cross | 984223f | 2024-02-01 17:10:23 -0800 | [diff] [blame] | 612 | actx := topDownMutatorContextPool.Get().(*topDownMutatorContext) |
| 613 | defer topDownMutatorContextPool.Put(actx) |
| 614 | *actx = topDownMutatorContext{ |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 615 | bp: ctx, |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 616 | baseModuleContext: moduleContext, |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 617 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 618 | m(actx) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 619 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 620 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 621 | mutator := &mutator{name: x.mutatorName(name), topDownMutator: f} |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 622 | x.mutators = append(x.mutators, mutator) |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 623 | return mutator |
| 624 | } |
| 625 | |
Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 626 | func (mutator *mutator) componentName() string { |
| 627 | return mutator.name |
| 628 | } |
| 629 | |
| 630 | func (mutator *mutator) register(ctx *Context) { |
| 631 | blueprintCtx := ctx.Context |
| 632 | var handle blueprint.MutatorHandle |
| 633 | if mutator.bottomUpMutator != nil { |
| 634 | handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator) |
| 635 | } else if mutator.topDownMutator != nil { |
| 636 | handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator) |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 637 | } else if mutator.transitionMutator != nil { |
| 638 | blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator) |
Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 639 | } |
| 640 | if mutator.parallel { |
| 641 | handle.Parallel() |
| 642 | } |
| 643 | } |
| 644 | |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 645 | type MutatorHandle interface { |
| 646 | Parallel() MutatorHandle |
| 647 | } |
| 648 | |
| 649 | func (mutator *mutator) Parallel() MutatorHandle { |
| 650 | mutator.parallel = true |
| 651 | return mutator |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 652 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 653 | |
Paul Duffin | 44f1d84 | 2020-06-26 20:17:02 +0100 | [diff] [blame] | 654 | func RegisterComponentsMutator(ctx RegisterMutatorsContext) { |
| 655 | ctx.BottomUp("component-deps", componentDepsMutator).Parallel() |
| 656 | } |
| 657 | |
| 658 | // A special mutator that runs just prior to the deps mutator to allow the dependencies |
| 659 | // on component modules to be added so that they can depend directly on a prebuilt |
| 660 | // module. |
| 661 | func componentDepsMutator(ctx BottomUpMutatorContext) { |
| 662 | if m := ctx.Module(); m.Enabled() { |
| 663 | m.ComponentDepsMutator(ctx) |
| 664 | } |
| 665 | } |
| 666 | |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 667 | func depsMutator(ctx BottomUpMutatorContext) { |
Paul Duffin | 44f1d84 | 2020-06-26 20:17:02 +0100 | [diff] [blame] | 668 | if m := ctx.Module(); m.Enabled() { |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 669 | m.base().baseDepsMutator(ctx) |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 670 | m.DepsMutator(ctx) |
| 671 | } |
| 672 | } |
Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 673 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 674 | func registerDepsMutator(ctx RegisterMutatorsContext) { |
| 675 | ctx.BottomUp("deps", depsMutator).Parallel() |
| 676 | } |
| 677 | |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 678 | // android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that |
| 679 | // has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid |
| 680 | // ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every |
| 681 | // non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following |
| 682 | // methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext. |
| 683 | |
Colin Cross | cb55e08 | 2019-07-01 15:32:31 -0700 | [diff] [blame] | 684 | func (t *topDownMutatorContext) MutatorName() string { |
| 685 | return t.bp.MutatorName() |
| 686 | } |
| 687 | |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 688 | func (t *topDownMutatorContext) Rename(name string) { |
| 689 | t.bp.Rename(name) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 690 | t.Module().base().commonProperties.DebugName = name |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 693 | func (t *topDownMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module { |
| 694 | return t.bp.CreateModule(factory, name, props...) |
| 695 | } |
| 696 | |
Colin Cross | e003c4a | 2019-09-25 12:58:36 -0700 | [diff] [blame] | 697 | func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module { |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 698 | return createModule(t, factory, "_topDownMutatorModule", props...) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 701 | func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module { |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 702 | module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), "", props...).(Module) |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 703 | return module |
| 704 | } |
| 705 | |
Colin Cross | cb55e08 | 2019-07-01 15:32:31 -0700 | [diff] [blame] | 706 | func (b *bottomUpMutatorContext) MutatorName() string { |
| 707 | return b.bp.MutatorName() |
| 708 | } |
| 709 | |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 710 | func (b *bottomUpMutatorContext) Rename(name string) { |
| 711 | b.bp.Rename(name) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 712 | b.Module().base().commonProperties.DebugName = name |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 715 | func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module { |
Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 716 | if b.baseModuleContext.checkedMissingDeps() { |
| 717 | panic("Adding deps not allowed after checking for missing deps") |
| 718 | } |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 719 | return b.bp.AddDependency(module, tag, name...) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) { |
Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 723 | if b.baseModuleContext.checkedMissingDeps() { |
| 724 | panic("Adding deps not allowed after checking for missing deps") |
| 725 | } |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 726 | b.bp.AddReverseDependency(module, tag, name) |
| 727 | } |
| 728 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 729 | func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module { |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 730 | if b.finalPhase { |
| 731 | panic("CreateVariations not allowed in FinalDepsMutators") |
| 732 | } |
| 733 | |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 734 | modules := b.bp.CreateVariations(variations...) |
| 735 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 736 | aModules := make([]Module, len(modules)) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 737 | for i := range variations { |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 738 | aModules[i] = modules[i].(Module) |
| 739 | base := aModules[i].base() |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 740 | base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName()) |
| 741 | base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i]) |
| 742 | } |
| 743 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 744 | return aModules |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 747 | func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module { |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 748 | if b.finalPhase { |
| 749 | panic("CreateLocalVariations not allowed in FinalDepsMutators") |
| 750 | } |
| 751 | |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 752 | modules := b.bp.CreateLocalVariations(variations...) |
| 753 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 754 | aModules := make([]Module, len(modules)) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 755 | for i := range variations { |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 756 | aModules[i] = modules[i].(Module) |
| 757 | base := aModules[i].base() |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 758 | base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName()) |
| 759 | base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i]) |
| 760 | } |
| 761 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 762 | return aModules |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) { |
| 766 | b.bp.SetDependencyVariation(variation) |
| 767 | } |
| 768 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 769 | func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) { |
| 770 | b.bp.SetDefaultDependencyVariation(variation) |
| 771 | } |
| 772 | |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 773 | func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 774 | names ...string) []blueprint.Module { |
Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 775 | if b.baseModuleContext.checkedMissingDeps() { |
| 776 | panic("Adding deps not allowed after checking for missing deps") |
| 777 | } |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 778 | return b.bp.AddVariationDependencies(variations, tag, names...) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation, |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 782 | tag blueprint.DependencyTag, names ...string) []blueprint.Module { |
Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 783 | if b.baseModuleContext.checkedMissingDeps() { |
| 784 | panic("Adding deps not allowed after checking for missing deps") |
| 785 | } |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 786 | |
Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 787 | return b.bp.AddFarVariationDependencies(variations, tag, names...) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) { |
| 791 | b.bp.AddInterVariantDependency(tag, from, to) |
| 792 | } |
| 793 | |
| 794 | func (b *bottomUpMutatorContext) ReplaceDependencies(name string) { |
Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 795 | if b.baseModuleContext.checkedMissingDeps() { |
| 796 | panic("Adding deps not allowed after checking for missing deps") |
| 797 | } |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 798 | b.bp.ReplaceDependencies(name) |
| 799 | } |
Jaewoong Jung | 9f88ce2 | 2019-11-15 10:57:34 -0800 | [diff] [blame] | 800 | |
Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 801 | func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) { |
Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 802 | if b.baseModuleContext.checkedMissingDeps() { |
| 803 | panic("Adding deps not allowed after checking for missing deps") |
| 804 | } |
Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 805 | b.bp.ReplaceDependenciesIf(name, predicate) |
| 806 | } |
| 807 | |
Jaewoong Jung | 9f88ce2 | 2019-11-15 10:57:34 -0800 | [diff] [blame] | 808 | func (b *bottomUpMutatorContext) AliasVariation(variationName string) { |
| 809 | b.bp.AliasVariation(variationName) |
| 810 | } |
Colin Cross | 1b9604b | 2020-08-11 12:03:56 -0700 | [diff] [blame] | 811 | |
| 812 | func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) { |
| 813 | b.bp.CreateAliasVariation(fromVariationName, toVariationName) |
| 814 | } |
Colin Cross | d27e7b8 | 2020-07-02 11:38:17 -0700 | [diff] [blame] | 815 | |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 816 | func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.AnyProviderKey, value interface{}) { |
Colin Cross | d27e7b8 | 2020-07-02 11:38:17 -0700 | [diff] [blame] | 817 | b.bp.SetVariationProvider(module, provider, value) |
| 818 | } |