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