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