| 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 { | 
|  | 69 | TopDown(name string, m AndroidTopDownMutator) MutatorHandle | 
|  | 70 | BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | type RegisterMutatorFunc func(RegisterMutatorsContext) | 
|  | 74 |  | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 75 | var preArch = []RegisterMutatorFunc{ | 
|  | 76 | func(ctx RegisterMutatorsContext) { | 
|  | 77 | ctx.TopDown("load_hooks", loadHookMutator).Parallel() | 
|  | 78 | }, | 
| Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 79 | RegisterPrebuiltsPreArchMutators, | 
| Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 80 | RegisterDefaultsPreArchMutators, | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 83 | func registerArchMutator(ctx RegisterMutatorsContext) { | 
|  | 84 | ctx.BottomUp("arch", archMutator).Parallel() | 
|  | 85 | ctx.TopDown("arch_hooks", archHookMutator).Parallel() | 
|  | 86 | } | 
|  | 87 |  | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 88 | var preDeps = []RegisterMutatorFunc{ | 
| Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 89 | registerArchMutator, | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
|  | 92 | var postDeps = []RegisterMutatorFunc{ | 
| Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 93 | RegisterPrebuiltsPostDepsMutators, | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 94 | } | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 95 |  | 
|  | 96 | func PreArchMutators(f RegisterMutatorFunc) { | 
|  | 97 | preArch = append(preArch, f) | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | func PreDepsMutators(f RegisterMutatorFunc) { | 
|  | 101 | preDeps = append(preDeps, f) | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | func PostDepsMutators(f RegisterMutatorFunc) { | 
|  | 105 | postDeps = append(postDeps, f) | 
|  | 106 | } | 
|  | 107 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 108 | type AndroidTopDownMutator func(TopDownMutatorContext) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 109 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 110 | type TopDownMutatorContext interface { | 
| Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 111 | BaseModuleContext | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 112 | androidBaseContext | 
| Colin Cross | 3f68a13 | 2017-10-23 17:10:29 -0700 | [diff] [blame] | 113 |  | 
|  | 114 | OtherModuleExists(name string) bool | 
|  | 115 | Rename(name string) | 
| Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 116 | Module() Module | 
| Colin Cross | 3f68a13 | 2017-10-23 17:10:29 -0700 | [diff] [blame] | 117 |  | 
|  | 118 | OtherModuleName(m blueprint.Module) string | 
|  | 119 | OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) | 
|  | 120 | OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag | 
|  | 121 |  | 
|  | 122 | CreateModule(blueprint.ModuleFactory, ...interface{}) | 
|  | 123 |  | 
|  | 124 | GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module | 
|  | 125 | GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) | 
|  | 126 |  | 
| Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 127 | VisitDirectDeps(visit func(Module)) | 
|  | 128 | VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) | 
|  | 129 | VisitDepsDepthFirst(visit func(Module)) | 
|  | 130 | VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) | 
|  | 131 | WalkDeps(visit func(Module, Module) bool) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
|  | 134 | type androidTopDownMutatorContext struct { | 
|  | 135 | blueprint.TopDownMutatorContext | 
|  | 136 | androidBaseContextImpl | 
|  | 137 | } | 
|  | 138 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 139 | type AndroidBottomUpMutator func(BottomUpMutatorContext) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 140 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 141 | type BottomUpMutatorContext interface { | 
| Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 142 | BaseModuleContext | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 143 | androidBaseContext | 
| Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 144 |  | 
|  | 145 | OtherModuleExists(name string) bool | 
|  | 146 | Rename(name string) | 
|  | 147 | Module() blueprint.Module | 
|  | 148 |  | 
|  | 149 | AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) | 
|  | 150 | AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) | 
|  | 151 | CreateVariations(...string) []blueprint.Module | 
|  | 152 | CreateLocalVariations(...string) []blueprint.Module | 
|  | 153 | SetDependencyVariation(string) | 
|  | 154 | AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) | 
|  | 155 | AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) | 
|  | 156 | AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) | 
|  | 157 | ReplaceDependencies(string) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 158 | } | 
|  | 159 |  | 
|  | 160 | type androidBottomUpMutatorContext struct { | 
|  | 161 | blueprint.BottomUpMutatorContext | 
|  | 162 | androidBaseContextImpl | 
|  | 163 | } | 
|  | 164 |  | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 165 | func (x *registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle { | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 166 | f := func(ctx blueprint.BottomUpMutatorContext) { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 167 | if a, ok := ctx.Module().(Module); ok { | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 168 | actx := &androidBottomUpMutatorContext{ | 
|  | 169 | BottomUpMutatorContext: ctx, | 
|  | 170 | androidBaseContextImpl: a.base().androidBaseContextFactory(ctx), | 
|  | 171 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 172 | m(actx) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 173 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 174 | } | 
|  | 175 | mutator := &mutator{name: name, bottomUpMutator: f} | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 176 | x.mutators = append(x.mutators, mutator) | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 177 | return mutator | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 178 | } | 
|  | 179 |  | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 180 | func (x *registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle { | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 181 | f := func(ctx blueprint.TopDownMutatorContext) { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 182 | if a, ok := ctx.Module().(Module); ok { | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 183 | actx := &androidTopDownMutatorContext{ | 
|  | 184 | TopDownMutatorContext:  ctx, | 
|  | 185 | androidBaseContextImpl: a.base().androidBaseContextFactory(ctx), | 
|  | 186 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 187 | m(actx) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 188 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 189 | } | 
|  | 190 | mutator := &mutator{name: name, topDownMutator: f} | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 191 | x.mutators = append(x.mutators, mutator) | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 192 | return mutator | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | type MutatorHandle interface { | 
|  | 196 | Parallel() MutatorHandle | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | func (mutator *mutator) Parallel() MutatorHandle { | 
|  | 200 | mutator.parallel = true | 
|  | 201 | return mutator | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 202 | } | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 203 |  | 
|  | 204 | func depsMutator(ctx BottomUpMutatorContext) { | 
|  | 205 | if m, ok := ctx.Module().(Module); ok { | 
|  | 206 | m.DepsMutator(ctx) | 
|  | 207 | } | 
|  | 208 | } | 
| Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 209 |  | 
| Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 210 | func (a *androidTopDownMutatorContext) Config() Config { | 
|  | 211 | return a.config | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | func (a *androidBottomUpMutatorContext) Config() Config { | 
|  | 215 | return a.config | 
|  | 216 | } | 
|  | 217 |  | 
| Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 218 | func (a *androidTopDownMutatorContext) Module() Module { | 
|  | 219 | module, _ := a.TopDownMutatorContext.Module().(Module) | 
|  | 220 | return module | 
|  | 221 | } | 
|  | 222 |  | 
| Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 223 | func (a *androidTopDownMutatorContext) VisitDirectDeps(visit func(Module)) { | 
|  | 224 | a.TopDownMutatorContext.VisitDirectDeps(func(module blueprint.Module) { | 
|  | 225 | if aModule, _ := module.(Module); aModule != nil { | 
|  | 226 | visit(aModule) | 
|  | 227 | } | 
|  | 228 | }) | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | func (a *androidTopDownMutatorContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) { | 
|  | 232 | a.TopDownMutatorContext.VisitDirectDepsIf( | 
|  | 233 | // pred | 
|  | 234 | func(module blueprint.Module) bool { | 
|  | 235 | if aModule, _ := module.(Module); aModule != nil { | 
|  | 236 | return pred(aModule) | 
|  | 237 | } else { | 
|  | 238 | return false | 
|  | 239 | } | 
|  | 240 | }, | 
|  | 241 | // visit | 
|  | 242 | func(module blueprint.Module) { | 
|  | 243 | visit(module.(Module)) | 
|  | 244 | }) | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | func (a *androidTopDownMutatorContext) VisitDepsDepthFirst(visit func(Module)) { | 
|  | 248 | a.TopDownMutatorContext.VisitDepsDepthFirst(func(module blueprint.Module) { | 
|  | 249 | if aModule, _ := module.(Module); aModule != nil { | 
|  | 250 | visit(aModule) | 
|  | 251 | } | 
|  | 252 | }) | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | func (a *androidTopDownMutatorContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) { | 
|  | 256 | a.TopDownMutatorContext.VisitDepsDepthFirstIf( | 
|  | 257 | // pred | 
|  | 258 | func(module blueprint.Module) bool { | 
|  | 259 | if aModule, _ := module.(Module); aModule != nil { | 
|  | 260 | return pred(aModule) | 
|  | 261 | } else { | 
|  | 262 | return false | 
|  | 263 | } | 
|  | 264 | }, | 
|  | 265 | // visit | 
|  | 266 | func(module blueprint.Module) { | 
|  | 267 | visit(module.(Module)) | 
|  | 268 | }) | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | func (a *androidTopDownMutatorContext) WalkDeps(visit func(Module, Module) bool) { | 
|  | 272 | a.TopDownMutatorContext.WalkDeps(func(child, parent blueprint.Module) bool { | 
|  | 273 | childAndroidModule, _ := child.(Module) | 
|  | 274 | parentAndroidModule, _ := parent.(Module) | 
|  | 275 | if childAndroidModule != nil && parentAndroidModule != nil { | 
|  | 276 | return visit(childAndroidModule, parentAndroidModule) | 
|  | 277 | } else { | 
|  | 278 | return false | 
|  | 279 | } | 
|  | 280 | }) | 
|  | 281 | } | 
| Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 282 |  | 
|  | 283 | func (a *androidTopDownMutatorContext) AppendProperties(props ...interface{}) { | 
|  | 284 | for _, p := range props { | 
|  | 285 | err := proptools.AppendMatchingProperties(a.Module().base().customizableProperties, | 
|  | 286 | p, nil) | 
|  | 287 | if err != nil { | 
|  | 288 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { | 
|  | 289 | a.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) | 
|  | 290 | } else { | 
|  | 291 | panic(err) | 
|  | 292 | } | 
|  | 293 | } | 
|  | 294 | } | 
|  | 295 | } | 
|  | 296 |  | 
|  | 297 | func (a *androidTopDownMutatorContext) PrependProperties(props ...interface{}) { | 
|  | 298 | for _, p := range props { | 
|  | 299 | err := proptools.PrependMatchingProperties(a.Module().base().customizableProperties, | 
|  | 300 | p, nil) | 
|  | 301 | if err != nil { | 
|  | 302 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { | 
|  | 303 | a.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) | 
|  | 304 | } else { | 
|  | 305 | panic(err) | 
|  | 306 | } | 
|  | 307 | } | 
|  | 308 | } | 
|  | 309 | } |