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