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