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, |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 78 | RegisterPrebuiltsPreArchMutators, |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 79 | registerVisibilityRuleChecker, |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 80 | RegisterDefaultsPreArchMutators, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 81 | registerVisibilityRuleGatherer, |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 84 | func registerArchMutator(ctx RegisterMutatorsContext) { |
| 85 | ctx.BottomUp("arch", archMutator).Parallel() |
| 86 | ctx.TopDown("arch_hooks", archHookMutator).Parallel() |
| 87 | } |
| 88 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 89 | var preDeps = []RegisterMutatorFunc{ |
Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 90 | registerArchMutator, |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | var postDeps = []RegisterMutatorFunc{ |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 94 | registerPathDepsMutator, |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 95 | RegisterPrebuiltsPostDepsMutators, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 96 | registerVisibilityRuleEnforcer, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 97 | registerNeverallowMutator, |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 98 | RegisterOverridePostDepsMutators, |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 99 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 100 | |
| 101 | func PreArchMutators(f RegisterMutatorFunc) { |
| 102 | preArch = append(preArch, f) |
| 103 | } |
| 104 | |
| 105 | func PreDepsMutators(f RegisterMutatorFunc) { |
| 106 | preDeps = append(preDeps, f) |
| 107 | } |
| 108 | |
| 109 | func PostDepsMutators(f RegisterMutatorFunc) { |
| 110 | postDeps = append(postDeps, f) |
| 111 | } |
| 112 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 113 | type TopDownMutator func(TopDownMutatorContext) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 114 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 115 | type TopDownMutatorContext interface { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 116 | BaseModuleContext |
Colin Cross | 3f68a13 | 2017-10-23 17:10:29 -0700 | [diff] [blame] | 117 | |
Colin Cross | 3f68a13 | 2017-10-23 17:10:29 -0700 | [diff] [blame] | 118 | Rename(name string) |
Colin Cross | 3f68a13 | 2017-10-23 17:10:29 -0700 | [diff] [blame] | 119 | |
| 120 | CreateModule(blueprint.ModuleFactory, ...interface{}) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 123 | type topDownMutatorContext struct { |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 124 | bp blueprint.TopDownMutatorContext |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 125 | baseModuleContext |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 128 | type BottomUpMutator func(BottomUpMutatorContext) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 129 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 130 | type BottomUpMutatorContext interface { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 131 | BaseModuleContext |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 132 | |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 133 | Rename(name string) |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 134 | |
| 135 | AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) |
| 136 | AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) |
| 137 | CreateVariations(...string) []blueprint.Module |
| 138 | CreateLocalVariations(...string) []blueprint.Module |
| 139 | SetDependencyVariation(string) |
| 140 | AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) |
| 141 | AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) |
| 142 | AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) |
| 143 | ReplaceDependencies(string) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 146 | type bottomUpMutatorContext struct { |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 147 | bp blueprint.BottomUpMutatorContext |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 148 | baseModuleContext |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 151 | func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 152 | f := func(ctx blueprint.BottomUpMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 153 | if a, ok := ctx.Module().(Module); ok { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 154 | actx := &bottomUpMutatorContext{ |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 155 | bp: ctx, |
| 156 | baseModuleContext: a.base().baseModuleContextFactory(ctx), |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 157 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 158 | m(actx) |
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 | } |
| 161 | mutator := &mutator{name: name, bottomUpMutator: f} |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 162 | x.mutators = append(x.mutators, mutator) |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 163 | return mutator |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 166 | func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 167 | f := func(ctx blueprint.TopDownMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 168 | if a, ok := ctx.Module().(Module); ok { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 169 | actx := &topDownMutatorContext{ |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 170 | bp: ctx, |
| 171 | baseModuleContext: a.base().baseModuleContextFactory(ctx), |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 172 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 173 | m(actx) |
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 | } |
| 176 | mutator := &mutator{name: name, topDownMutator: f} |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 177 | x.mutators = append(x.mutators, mutator) |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 178 | return mutator |
| 179 | } |
| 180 | |
| 181 | type MutatorHandle interface { |
| 182 | Parallel() MutatorHandle |
| 183 | } |
| 184 | |
| 185 | func (mutator *mutator) Parallel() MutatorHandle { |
| 186 | mutator.parallel = true |
| 187 | return mutator |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 188 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 189 | |
| 190 | func depsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | 6db4a6a | 2018-08-30 12:52:41 -0700 | [diff] [blame] | 191 | if m, ok := ctx.Module().(Module); ok && m.Enabled() { |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 192 | m.DepsMutator(ctx) |
| 193 | } |
| 194 | } |
Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 195 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 196 | func (t *topDownMutatorContext) AppendProperties(props ...interface{}) { |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 197 | for _, p := range props { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 198 | err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties, |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 199 | p, nil) |
| 200 | if err != nil { |
| 201 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 202 | t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 203 | } else { |
| 204 | panic(err) |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 210 | func (t *topDownMutatorContext) PrependProperties(props ...interface{}) { |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 211 | for _, p := range props { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 212 | err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties, |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 213 | p, nil) |
| 214 | if err != nil { |
| 215 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 216 | t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 217 | } else { |
| 218 | panic(err) |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 223 | |
| 224 | // android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that |
| 225 | // has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid |
| 226 | // ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every |
| 227 | // non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following |
| 228 | // methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext. |
| 229 | |
| 230 | func (t *topDownMutatorContext) Rename(name string) { |
| 231 | t.bp.Rename(name) |
| 232 | } |
| 233 | |
| 234 | func (t *topDownMutatorContext) CreateModule(factory blueprint.ModuleFactory, props ...interface{}) { |
| 235 | t.bp.CreateModule(factory, props...) |
| 236 | } |
| 237 | |
| 238 | func (b *bottomUpMutatorContext) Rename(name string) { |
| 239 | b.bp.Rename(name) |
| 240 | } |
| 241 | |
| 242 | func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) { |
| 243 | b.bp.AddDependency(module, tag, name...) |
| 244 | } |
| 245 | |
| 246 | func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) { |
| 247 | b.bp.AddReverseDependency(module, tag, name) |
| 248 | } |
| 249 | |
| 250 | func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []blueprint.Module { |
| 251 | return b.bp.CreateVariations(variations...) |
| 252 | } |
| 253 | |
| 254 | func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []blueprint.Module { |
| 255 | return b.bp.CreateLocalVariations(variations...) |
| 256 | } |
| 257 | |
| 258 | func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) { |
| 259 | b.bp.SetDependencyVariation(variation) |
| 260 | } |
| 261 | |
| 262 | func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, |
| 263 | names ...string) { |
| 264 | |
| 265 | b.bp.AddVariationDependencies(variations, tag, names...) |
| 266 | } |
| 267 | |
| 268 | func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation, |
| 269 | tag blueprint.DependencyTag, names ...string) { |
| 270 | |
| 271 | b.bp.AddFarVariationDependencies(variations, tag, names...) |
| 272 | } |
| 273 | |
| 274 | func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) { |
| 275 | b.bp.AddInterVariantDependency(tag, from, to) |
| 276 | } |
| 277 | |
| 278 | func (b *bottomUpMutatorContext) ReplaceDependencies(name string) { |
| 279 | b.bp.ReplaceDependencies(name) |
| 280 | } |