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