| 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" | 
|  | 19 | ) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 20 |  | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 21 | // Mutator phases: | 
|  | 22 | //   Pre-arch | 
|  | 23 | //   Arch | 
|  | 24 | //   Pre-deps | 
|  | 25 | //   Deps | 
|  | 26 | //   PostDeps | 
|  | 27 |  | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 28 | func registerMutatorsToContext(ctx *blueprint.Context, mutators []*mutator) { | 
|  | 29 | for _, t := range mutators { | 
|  | 30 | var handle blueprint.MutatorHandle | 
|  | 31 | if t.bottomUpMutator != nil { | 
|  | 32 | handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator) | 
|  | 33 | } else if t.topDownMutator != nil { | 
|  | 34 | handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator) | 
|  | 35 | } | 
|  | 36 | if t.parallel { | 
|  | 37 | handle.Parallel() | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 38 | } | 
|  | 39 | } | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 40 | } | 
|  | 41 |  | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 42 | func registerMutators(ctx *blueprint.Context, preArch, preDeps, postDeps []RegisterMutatorFunc) { | 
|  | 43 | mctx := ®isterMutatorsContext{} | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 44 |  | 
|  | 45 | register := func(funcs []RegisterMutatorFunc) { | 
|  | 46 | for _, f := range funcs { | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 47 | f(mctx) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 48 | } | 
|  | 49 | } | 
|  | 50 |  | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 51 | register(preArch) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 52 |  | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 53 | register(preDeps) | 
|  | 54 |  | 
|  | 55 | mctx.BottomUp("deps", depsMutator).Parallel() | 
|  | 56 |  | 
|  | 57 | register(postDeps) | 
|  | 58 |  | 
|  | 59 | registerMutatorsToContext(ctx, mctx.mutators) | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
|  | 62 | type registerMutatorsContext struct { | 
|  | 63 | mutators []*mutator | 
|  | 64 | } | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 65 |  | 
|  | 66 | type RegisterMutatorsContext interface { | 
|  | 67 | TopDown(name string, m AndroidTopDownMutator) MutatorHandle | 
|  | 68 | BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | type RegisterMutatorFunc func(RegisterMutatorsContext) | 
|  | 72 |  | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 73 | var preArch = []RegisterMutatorFunc{ | 
|  | 74 | func(ctx RegisterMutatorsContext) { | 
|  | 75 | ctx.TopDown("load_hooks", loadHookMutator).Parallel() | 
|  | 76 | }, | 
|  | 77 | registerPrebuiltsPreArchMutators, | 
| Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 78 | RegisterDefaultsPreArchMutators, | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 79 | } | 
|  | 80 |  | 
|  | 81 | var preDeps = []RegisterMutatorFunc{ | 
|  | 82 | func(ctx RegisterMutatorsContext) { | 
|  | 83 | ctx.BottomUp("arch", archMutator).Parallel() | 
|  | 84 | ctx.TopDown("arch_hooks", archHookMutator).Parallel() | 
|  | 85 | }, | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | var postDeps = []RegisterMutatorFunc{ | 
|  | 89 | registerPrebuiltsPostDepsMutators, | 
|  | 90 | } | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 91 |  | 
|  | 92 | func PreArchMutators(f RegisterMutatorFunc) { | 
|  | 93 | preArch = append(preArch, f) | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | func PreDepsMutators(f RegisterMutatorFunc) { | 
|  | 97 | preDeps = append(preDeps, f) | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | func PostDepsMutators(f RegisterMutatorFunc) { | 
|  | 101 | postDeps = append(postDeps, f) | 
|  | 102 | } | 
|  | 103 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 104 | type AndroidTopDownMutator func(TopDownMutatorContext) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 105 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 106 | type TopDownMutatorContext interface { | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 107 | blueprint.TopDownMutatorContext | 
|  | 108 | androidBaseContext | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | type androidTopDownMutatorContext struct { | 
|  | 112 | blueprint.TopDownMutatorContext | 
|  | 113 | androidBaseContextImpl | 
|  | 114 | } | 
|  | 115 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 116 | type AndroidBottomUpMutator func(BottomUpMutatorContext) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 117 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 118 | type BottomUpMutatorContext interface { | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 119 | blueprint.BottomUpMutatorContext | 
|  | 120 | androidBaseContext | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | type androidBottomUpMutatorContext struct { | 
|  | 124 | blueprint.BottomUpMutatorContext | 
|  | 125 | androidBaseContextImpl | 
|  | 126 | } | 
|  | 127 |  | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 128 | func (x *registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle { | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 129 | f := func(ctx blueprint.BottomUpMutatorContext) { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 130 | if a, ok := ctx.Module().(Module); ok { | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 131 | actx := &androidBottomUpMutatorContext{ | 
|  | 132 | BottomUpMutatorContext: ctx, | 
|  | 133 | androidBaseContextImpl: a.base().androidBaseContextFactory(ctx), | 
|  | 134 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 135 | m(actx) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 136 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 137 | } | 
|  | 138 | mutator := &mutator{name: name, bottomUpMutator: f} | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 139 | x.mutators = append(x.mutators, mutator) | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 140 | return mutator | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 141 | } | 
|  | 142 |  | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 143 | func (x *registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle { | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 144 | f := func(ctx blueprint.TopDownMutatorContext) { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 145 | if a, ok := ctx.Module().(Module); ok { | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 146 | actx := &androidTopDownMutatorContext{ | 
|  | 147 | TopDownMutatorContext:  ctx, | 
|  | 148 | androidBaseContextImpl: a.base().androidBaseContextFactory(ctx), | 
|  | 149 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 150 | m(actx) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 151 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 152 | } | 
|  | 153 | mutator := &mutator{name: name, topDownMutator: f} | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 154 | x.mutators = append(x.mutators, mutator) | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 155 | return mutator | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | type MutatorHandle interface { | 
|  | 159 | Parallel() MutatorHandle | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | func (mutator *mutator) Parallel() MutatorHandle { | 
|  | 163 | mutator.parallel = true | 
|  | 164 | return mutator | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 165 | } | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 166 |  | 
|  | 167 | func depsMutator(ctx BottomUpMutatorContext) { | 
|  | 168 | if m, ok := ctx.Module().(Module); ok { | 
|  | 169 | m.DepsMutator(ctx) | 
|  | 170 | } | 
|  | 171 | } |