Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 16 | |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 17 | import ( |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 18 | "github.com/google/blueprint" |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 19 | "github.com/google/blueprint/proptools" |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 20 | ) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 21 | |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 22 | // Phases: |
| 23 | // run Pre-arch mutators |
| 24 | // run archMutator |
| 25 | // run Pre-deps mutators |
| 26 | // run depsMutator |
| 27 | // run PostDeps mutators |
| 28 | // continue on to GenerateAndroidBuildActions |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 29 | |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 30 | func registerMutatorsToContext(ctx *blueprint.Context, mutators []*mutator) { |
| 31 | for _, t := range mutators { |
| 32 | var handle blueprint.MutatorHandle |
| 33 | if t.bottomUpMutator != nil { |
| 34 | handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator) |
| 35 | } else if t.topDownMutator != nil { |
| 36 | handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator) |
| 37 | } |
| 38 | if t.parallel { |
| 39 | handle.Parallel() |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 40 | } |
| 41 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 44 | func registerMutators(ctx *blueprint.Context, preArch, preDeps, postDeps []RegisterMutatorFunc) { |
| 45 | mctx := ®isterMutatorsContext{} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 46 | |
| 47 | register := func(funcs []RegisterMutatorFunc) { |
| 48 | for _, f := range funcs { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 49 | f(mctx) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 53 | register(preArch) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 54 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 55 | register(preDeps) |
| 56 | |
| 57 | mctx.BottomUp("deps", depsMutator).Parallel() |
| 58 | |
| 59 | register(postDeps) |
| 60 | |
| 61 | registerMutatorsToContext(ctx, mctx.mutators) |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | type registerMutatorsContext struct { |
| 65 | mutators []*mutator |
| 66 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 67 | |
| 68 | type RegisterMutatorsContext interface { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 69 | TopDown(name string, m TopDownMutator) MutatorHandle |
| 70 | BottomUp(name string, m BottomUpMutator) MutatorHandle |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | type RegisterMutatorFunc func(RegisterMutatorsContext) |
| 74 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 75 | var preArch = []RegisterMutatorFunc{ |
Colin Cross | f8b860a | 2019-04-16 14:43:28 -0700 | [diff] [blame] | 76 | registerLoadHookMutator, |
Dan Willemsen | 6e72ef7 | 2018-01-26 18:27:02 -0800 | [diff] [blame] | 77 | RegisterNamespaceMutator, |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame^] | 78 | // Rename package module types. |
| 79 | registerPackageRenamer, |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 80 | RegisterPrebuiltsPreArchMutators, |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 81 | registerVisibilityRuleChecker, |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 82 | RegisterDefaultsPreArchMutators, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 83 | registerVisibilityRuleGatherer, |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 86 | func registerArchMutator(ctx RegisterMutatorsContext) { |
| 87 | ctx.BottomUp("arch", archMutator).Parallel() |
| 88 | ctx.TopDown("arch_hooks", archHookMutator).Parallel() |
| 89 | } |
| 90 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 91 | var preDeps = []RegisterMutatorFunc{ |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 92 | registerArchMutator, |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | var postDeps = []RegisterMutatorFunc{ |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 96 | registerPathDepsMutator, |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 97 | RegisterPrebuiltsPostDepsMutators, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 98 | registerVisibilityRuleEnforcer, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 99 | registerNeverallowMutator, |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 100 | RegisterOverridePostDepsMutators, |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 101 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 102 | |
| 103 | func PreArchMutators(f RegisterMutatorFunc) { |
| 104 | preArch = append(preArch, f) |
| 105 | } |
| 106 | |
| 107 | func PreDepsMutators(f RegisterMutatorFunc) { |
| 108 | preDeps = append(preDeps, f) |
| 109 | } |
| 110 | |
| 111 | func PostDepsMutators(f RegisterMutatorFunc) { |
| 112 | postDeps = append(postDeps, f) |
| 113 | } |
| 114 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 115 | type TopDownMutator func(TopDownMutatorContext) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 116 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 117 | type TopDownMutatorContext interface { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 118 | BaseModuleContext |
Colin Cross | 3f68a13 | 2017-10-23 17:10:29 -0700 | [diff] [blame] | 119 | |
Colin Cross | 3f68a13 | 2017-10-23 17:10:29 -0700 | [diff] [blame] | 120 | Rename(name string) |
Colin Cross | 3f68a13 | 2017-10-23 17:10:29 -0700 | [diff] [blame] | 121 | |
| 122 | CreateModule(blueprint.ModuleFactory, ...interface{}) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 125 | type topDownMutatorContext struct { |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 126 | bp blueprint.TopDownMutatorContext |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 127 | baseModuleContext |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 130 | type BottomUpMutator func(BottomUpMutatorContext) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 131 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 132 | type BottomUpMutatorContext interface { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 133 | BaseModuleContext |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 134 | |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 135 | Rename(name string) |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 136 | |
| 137 | AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) |
| 138 | AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) |
| 139 | CreateVariations(...string) []blueprint.Module |
| 140 | CreateLocalVariations(...string) []blueprint.Module |
| 141 | SetDependencyVariation(string) |
| 142 | AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) |
| 143 | AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) |
| 144 | AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) |
| 145 | ReplaceDependencies(string) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 148 | type bottomUpMutatorContext struct { |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 149 | bp blueprint.BottomUpMutatorContext |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 150 | baseModuleContext |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 153 | func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 154 | f := func(ctx blueprint.BottomUpMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 155 | if a, ok := ctx.Module().(Module); ok { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 156 | actx := &bottomUpMutatorContext{ |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 157 | bp: ctx, |
| 158 | baseModuleContext: a.base().baseModuleContextFactory(ctx), |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 159 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 160 | m(actx) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 161 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 162 | } |
| 163 | mutator := &mutator{name: name, bottomUpMutator: f} |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 164 | x.mutators = append(x.mutators, mutator) |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 165 | return mutator |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 168 | func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 169 | f := func(ctx blueprint.TopDownMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 170 | if a, ok := ctx.Module().(Module); ok { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 171 | actx := &topDownMutatorContext{ |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 172 | bp: ctx, |
| 173 | baseModuleContext: a.base().baseModuleContextFactory(ctx), |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 174 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 175 | m(actx) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 176 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 177 | } |
| 178 | mutator := &mutator{name: name, topDownMutator: f} |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 179 | x.mutators = append(x.mutators, mutator) |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 180 | return mutator |
| 181 | } |
| 182 | |
| 183 | type MutatorHandle interface { |
| 184 | Parallel() MutatorHandle |
| 185 | } |
| 186 | |
| 187 | func (mutator *mutator) Parallel() MutatorHandle { |
| 188 | mutator.parallel = true |
| 189 | return mutator |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 190 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 191 | |
| 192 | func depsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | 6db4a6a | 2018-08-30 12:52:41 -0700 | [diff] [blame] | 193 | if m, ok := ctx.Module().(Module); ok && m.Enabled() { |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 194 | m.DepsMutator(ctx) |
| 195 | } |
| 196 | } |
Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 197 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 198 | func (t *topDownMutatorContext) AppendProperties(props ...interface{}) { |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 199 | for _, p := range props { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 200 | err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties, |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 201 | p, nil) |
| 202 | if err != nil { |
| 203 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 204 | t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 205 | } else { |
| 206 | panic(err) |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 212 | func (t *topDownMutatorContext) PrependProperties(props ...interface{}) { |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 213 | for _, p := range props { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 214 | err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties, |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 215 | p, nil) |
| 216 | if err != nil { |
| 217 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 218 | t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 219 | } else { |
| 220 | panic(err) |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 225 | |
| 226 | // android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that |
| 227 | // has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid |
| 228 | // ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every |
| 229 | // non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following |
| 230 | // methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext. |
| 231 | |
| 232 | func (t *topDownMutatorContext) Rename(name string) { |
| 233 | t.bp.Rename(name) |
| 234 | } |
| 235 | |
| 236 | func (t *topDownMutatorContext) CreateModule(factory blueprint.ModuleFactory, props ...interface{}) { |
| 237 | t.bp.CreateModule(factory, props...) |
| 238 | } |
| 239 | |
| 240 | func (b *bottomUpMutatorContext) Rename(name string) { |
| 241 | b.bp.Rename(name) |
| 242 | } |
| 243 | |
| 244 | func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) { |
| 245 | b.bp.AddDependency(module, tag, name...) |
| 246 | } |
| 247 | |
| 248 | func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) { |
| 249 | b.bp.AddReverseDependency(module, tag, name) |
| 250 | } |
| 251 | |
| 252 | func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []blueprint.Module { |
| 253 | return b.bp.CreateVariations(variations...) |
| 254 | } |
| 255 | |
| 256 | func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []blueprint.Module { |
| 257 | return b.bp.CreateLocalVariations(variations...) |
| 258 | } |
| 259 | |
| 260 | func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) { |
| 261 | b.bp.SetDependencyVariation(variation) |
| 262 | } |
| 263 | |
| 264 | func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, |
| 265 | names ...string) { |
| 266 | |
| 267 | b.bp.AddVariationDependencies(variations, tag, names...) |
| 268 | } |
| 269 | |
| 270 | func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation, |
| 271 | tag blueprint.DependencyTag, names ...string) { |
| 272 | |
| 273 | b.bp.AddFarVariationDependencies(variations, tag, names...) |
| 274 | } |
| 275 | |
| 276 | func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) { |
| 277 | b.bp.AddInterVariantDependency(tag, from, to) |
| 278 | } |
| 279 | |
| 280 | func (b *bottomUpMutatorContext) ReplaceDependencies(name string) { |
| 281 | b.bp.ReplaceDependencies(name) |
| 282 | } |