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