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