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) { |
| 84 | mutators := registerMutatorsContext{} |
| 85 | mutators.BottomUp("deps", depsMutator).Parallel() |
| 86 | registerMutatorsToContext(ctx, mutators.mutators) |
| 87 | } |
| 88 | |
| 89 | type registerMutatorsContext struct { |
| 90 | mutators []*mutator |
| 91 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 92 | |
| 93 | type RegisterMutatorsContext interface { |
| 94 | TopDown(name string, m AndroidTopDownMutator) MutatorHandle |
| 95 | BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle |
| 96 | } |
| 97 | |
| 98 | type RegisterMutatorFunc func(RegisterMutatorsContext) |
| 99 | |
| 100 | var preArch, preDeps, postDeps []RegisterMutatorFunc |
| 101 | |
| 102 | func PreArchMutators(f RegisterMutatorFunc) { |
| 103 | preArch = append(preArch, f) |
| 104 | } |
| 105 | |
| 106 | func PreDepsMutators(f RegisterMutatorFunc) { |
| 107 | preDeps = append(preDeps, f) |
| 108 | } |
| 109 | |
| 110 | func PostDepsMutators(f RegisterMutatorFunc) { |
| 111 | postDeps = append(postDeps, f) |
| 112 | } |
| 113 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 114 | type AndroidTopDownMutator func(TopDownMutatorContext) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 115 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 116 | type TopDownMutatorContext interface { |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 117 | blueprint.TopDownMutatorContext |
| 118 | androidBaseContext |
| 119 | } |
| 120 | |
| 121 | type androidTopDownMutatorContext struct { |
| 122 | blueprint.TopDownMutatorContext |
| 123 | androidBaseContextImpl |
| 124 | } |
| 125 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 126 | type AndroidBottomUpMutator func(BottomUpMutatorContext) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 127 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 128 | type BottomUpMutatorContext interface { |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 129 | blueprint.BottomUpMutatorContext |
| 130 | androidBaseContext |
| 131 | } |
| 132 | |
| 133 | type androidBottomUpMutatorContext struct { |
| 134 | blueprint.BottomUpMutatorContext |
| 135 | androidBaseContextImpl |
| 136 | } |
| 137 | |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame^] | 138 | func (x *registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 139 | f := func(ctx blueprint.BottomUpMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 140 | if a, ok := ctx.Module().(Module); ok { |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 141 | actx := &androidBottomUpMutatorContext{ |
| 142 | BottomUpMutatorContext: ctx, |
| 143 | androidBaseContextImpl: a.base().androidBaseContextFactory(ctx), |
| 144 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 145 | m(actx) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 146 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 147 | } |
| 148 | mutator := &mutator{name: name, bottomUpMutator: f} |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame^] | 149 | x.mutators = append(x.mutators, mutator) |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 150 | return mutator |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame^] | 153 | func (x *registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 154 | f := func(ctx blueprint.TopDownMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 155 | if a, ok := ctx.Module().(Module); ok { |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 156 | actx := &androidTopDownMutatorContext{ |
| 157 | TopDownMutatorContext: ctx, |
| 158 | androidBaseContextImpl: a.base().androidBaseContextFactory(ctx), |
| 159 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 160 | m(actx) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 161 | } |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 162 | } |
| 163 | mutator := &mutator{name: name, topDownMutator: f} |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame^] | 164 | x.mutators = append(x.mutators, mutator) |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 165 | return mutator |
| 166 | } |
| 167 | |
| 168 | type MutatorHandle interface { |
| 169 | Parallel() MutatorHandle |
| 170 | } |
| 171 | |
| 172 | func (mutator *mutator) Parallel() MutatorHandle { |
| 173 | mutator.parallel = true |
| 174 | return mutator |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 175 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 176 | |
| 177 | func depsMutator(ctx BottomUpMutatorContext) { |
| 178 | if m, ok := ctx.Module().(Module); ok { |
| 179 | m.DepsMutator(ctx) |
| 180 | } |
| 181 | } |