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