| 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 | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 17 | import "github.com/google/blueprint" | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 18 |  | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 19 | // Mutator phases: | 
|  | 20 | //   Pre-arch | 
|  | 21 | //   Arch | 
|  | 22 | //   Pre-deps | 
|  | 23 | //   Deps | 
|  | 24 | //   PostDeps | 
|  | 25 |  | 
|  | 26 | func registerMutators() { | 
|  | 27 | ctx := registerMutatorsContext{} | 
|  | 28 |  | 
|  | 29 | register := func(funcs []RegisterMutatorFunc) { | 
|  | 30 | for _, f := range funcs { | 
|  | 31 | f(ctx) | 
|  | 32 | } | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | ctx.TopDown("load_hooks", loadHookMutator).Parallel() | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 36 | ctx.BottomUp("prebuilts", prebuiltMutator).Parallel() | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 37 | ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel() | 
|  | 38 | ctx.TopDown("defaults", defaultsMutator).Parallel() | 
|  | 39 |  | 
|  | 40 | register(preArch) | 
|  | 41 |  | 
|  | 42 | ctx.BottomUp("arch", archMutator).Parallel() | 
|  | 43 | ctx.TopDown("arch_hooks", archHookMutator).Parallel() | 
|  | 44 |  | 
|  | 45 | register(preDeps) | 
|  | 46 |  | 
|  | 47 | ctx.BottomUp("deps", depsMutator).Parallel() | 
|  | 48 |  | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 49 | ctx.BottomUp("prebuilt_replace", PrebuiltReplaceMutator).Parallel() | 
|  | 50 | ctx.TopDown("prebuilt_disable", PrebuiltDisableMutator).Parallel() | 
|  | 51 |  | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 52 | register(postDeps) | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | type registerMutatorsContext struct{} | 
|  | 56 |  | 
|  | 57 | type RegisterMutatorsContext interface { | 
|  | 58 | TopDown(name string, m AndroidTopDownMutator) MutatorHandle | 
|  | 59 | BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | type RegisterMutatorFunc func(RegisterMutatorsContext) | 
|  | 63 |  | 
|  | 64 | var preArch, preDeps, postDeps []RegisterMutatorFunc | 
|  | 65 |  | 
|  | 66 | func PreArchMutators(f RegisterMutatorFunc) { | 
|  | 67 | preArch = append(preArch, f) | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | func PreDepsMutators(f RegisterMutatorFunc) { | 
|  | 71 | preDeps = append(preDeps, f) | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | func PostDepsMutators(f RegisterMutatorFunc) { | 
|  | 75 | postDeps = append(postDeps, f) | 
|  | 76 | } | 
|  | 77 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 78 | type AndroidTopDownMutator func(TopDownMutatorContext) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 79 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 80 | type TopDownMutatorContext interface { | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 81 | blueprint.TopDownMutatorContext | 
|  | 82 | androidBaseContext | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | type androidTopDownMutatorContext struct { | 
|  | 86 | blueprint.TopDownMutatorContext | 
|  | 87 | androidBaseContextImpl | 
|  | 88 | } | 
|  | 89 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 90 | type AndroidBottomUpMutator func(BottomUpMutatorContext) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 91 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 92 | type BottomUpMutatorContext interface { | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 93 | blueprint.BottomUpMutatorContext | 
|  | 94 | androidBaseContext | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | type androidBottomUpMutatorContext struct { | 
|  | 98 | blueprint.BottomUpMutatorContext | 
|  | 99 | androidBaseContextImpl | 
|  | 100 | } | 
|  | 101 |  | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 102 | func (registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle { | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 103 | f := func(ctx blueprint.BottomUpMutatorContext) { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 104 | if a, ok := ctx.Module().(Module); ok { | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 105 | actx := &androidBottomUpMutatorContext{ | 
|  | 106 | BottomUpMutatorContext: ctx, | 
|  | 107 | androidBaseContextImpl: a.base().androidBaseContextFactory(ctx), | 
|  | 108 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 109 | m(actx) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 110 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 111 | } | 
|  | 112 | mutator := &mutator{name: name, bottomUpMutator: f} | 
|  | 113 | mutators = append(mutators, mutator) | 
|  | 114 | return mutator | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 115 | } | 
|  | 116 |  | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 117 | func (registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle { | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 118 | f := func(ctx blueprint.TopDownMutatorContext) { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 119 | if a, ok := ctx.Module().(Module); ok { | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 120 | actx := &androidTopDownMutatorContext{ | 
|  | 121 | TopDownMutatorContext:  ctx, | 
|  | 122 | androidBaseContextImpl: a.base().androidBaseContextFactory(ctx), | 
|  | 123 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 124 | m(actx) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 125 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 126 | } | 
|  | 127 | mutator := &mutator{name: name, topDownMutator: f} | 
|  | 128 | mutators = append(mutators, mutator) | 
|  | 129 | return mutator | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | type MutatorHandle interface { | 
|  | 133 | Parallel() MutatorHandle | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | func (mutator *mutator) Parallel() MutatorHandle { | 
|  | 137 | mutator.parallel = true | 
|  | 138 | return mutator | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 139 | } | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 140 |  | 
|  | 141 | func depsMutator(ctx BottomUpMutatorContext) { | 
|  | 142 | if m, ok := ctx.Module().(Module); ok { | 
|  | 143 | m.DepsMutator(ctx) | 
|  | 144 | } | 
|  | 145 | } |