blob: afff7001e36eee8076f7cff46dbb0e662b801bb6 [file] [log] [blame]
Colin Cross6362e272015-10-29 15:25:03 -07001// 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 Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross6362e272015-10-29 15:25:03 -070016
Colin Cross795c3772017-03-16 16:50:10 -070017import (
Colin Cross795c3772017-03-16 16:50:10 -070018 "github.com/google/blueprint"
19)
Colin Cross6362e272015-10-29 15:25:03 -070020
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070021// Phases:
22// run Pre-arch mutators
23// run archMutator
24// run Pre-deps mutators
25// run depsMutator
26// run PostDeps mutators
27// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070028
Colin Cross795c3772017-03-16 16:50:10 -070029func registerMutatorsToContext(ctx *blueprint.Context, mutators []*mutator) {
30 for _, t := range mutators {
31 var handle blueprint.MutatorHandle
32 if t.bottomUpMutator != nil {
33 handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator)
34 } else if t.topDownMutator != nil {
35 handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator)
36 }
37 if t.parallel {
38 handle.Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070039 }
40 }
Colin Cross1e676be2016-10-12 14:38:15 -070041}
42
Colin Crosscec81712017-07-13 14:43:27 -070043func registerMutators(ctx *blueprint.Context, preArch, preDeps, postDeps []RegisterMutatorFunc) {
44 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080045
46 register := func(funcs []RegisterMutatorFunc) {
47 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070048 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080049 }
50 }
51
Colin Crosscec81712017-07-13 14:43:27 -070052 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080053
Colin Crosscec81712017-07-13 14:43:27 -070054 register(preDeps)
55
56 mctx.BottomUp("deps", depsMutator).Parallel()
57
58 register(postDeps)
59
60 registerMutatorsToContext(ctx, mctx.mutators)
Colin Cross795c3772017-03-16 16:50:10 -070061}
62
63type registerMutatorsContext struct {
64 mutators []*mutator
65}
Colin Cross1e676be2016-10-12 14:38:15 -070066
67type RegisterMutatorsContext interface {
68 TopDown(name string, m AndroidTopDownMutator) MutatorHandle
69 BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle
70}
71
72type RegisterMutatorFunc func(RegisterMutatorsContext)
73
Colin Crosscec81712017-07-13 14:43:27 -070074var preArch = []RegisterMutatorFunc{
75 func(ctx RegisterMutatorsContext) {
76 ctx.TopDown("load_hooks", loadHookMutator).Parallel()
77 },
Colin Cross5ea9bcc2017-07-27 15:41:32 -070078 RegisterPrebuiltsPreArchMutators,
Colin Cross89536d42017-07-07 14:35:50 -070079 RegisterDefaultsPreArchMutators,
Colin Crosscec81712017-07-13 14:43:27 -070080}
81
Colin Crossae4c6182017-09-15 17:33:55 -070082func registerArchMutator(ctx RegisterMutatorsContext) {
83 ctx.BottomUp("arch", archMutator).Parallel()
84 ctx.TopDown("arch_hooks", archHookMutator).Parallel()
85}
86
Colin Crosscec81712017-07-13 14:43:27 -070087var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -070088 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -070089}
90
91var postDeps = []RegisterMutatorFunc{
Colin Cross5ea9bcc2017-07-27 15:41:32 -070092 RegisterPrebuiltsPostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -070093}
Colin Cross1e676be2016-10-12 14:38:15 -070094
95func PreArchMutators(f RegisterMutatorFunc) {
96 preArch = append(preArch, f)
97}
98
99func PreDepsMutators(f RegisterMutatorFunc) {
100 preDeps = append(preDeps, f)
101}
102
103func PostDepsMutators(f RegisterMutatorFunc) {
104 postDeps = append(postDeps, f)
105}
106
Colin Cross635c3b02016-05-18 15:37:25 -0700107type AndroidTopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700108
Colin Cross635c3b02016-05-18 15:37:25 -0700109type TopDownMutatorContext interface {
Colin Cross3f68a132017-10-23 17:10:29 -0700110 blueprint.BaseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700111 androidBaseContext
Colin Cross3f68a132017-10-23 17:10:29 -0700112
113 OtherModuleExists(name string) bool
114 Rename(name string)
115 Module() blueprint.Module
116
117 OtherModuleName(m blueprint.Module) string
118 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
119 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
120
121 CreateModule(blueprint.ModuleFactory, ...interface{})
122
123 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
124 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
125
Colin Crossd11fcda2017-10-23 17:59:01 -0700126 VisitDirectDeps(visit func(Module))
127 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
128 VisitDepsDepthFirst(visit func(Module))
129 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
130 WalkDeps(visit func(Module, Module) bool)
Colin Cross6362e272015-10-29 15:25:03 -0700131}
132
133type androidTopDownMutatorContext struct {
134 blueprint.TopDownMutatorContext
135 androidBaseContextImpl
136}
137
Colin Cross635c3b02016-05-18 15:37:25 -0700138type AndroidBottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700139
Colin Cross635c3b02016-05-18 15:37:25 -0700140type BottomUpMutatorContext interface {
Colin Cross6362e272015-10-29 15:25:03 -0700141 blueprint.BottomUpMutatorContext
142 androidBaseContext
143}
144
145type androidBottomUpMutatorContext struct {
146 blueprint.BottomUpMutatorContext
147 androidBaseContextImpl
148}
149
Colin Cross795c3772017-03-16 16:50:10 -0700150func (x *registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700151 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700152 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700153 actx := &androidBottomUpMutatorContext{
154 BottomUpMutatorContext: ctx,
155 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
156 }
Colin Cross798bfce2016-10-12 14:28:16 -0700157 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700158 }
Colin Cross798bfce2016-10-12 14:28:16 -0700159 }
160 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700161 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700162 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700163}
164
Colin Cross795c3772017-03-16 16:50:10 -0700165func (x *registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700166 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700167 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700168 actx := &androidTopDownMutatorContext{
169 TopDownMutatorContext: ctx,
170 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
171 }
Colin Cross798bfce2016-10-12 14:28:16 -0700172 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700173 }
Colin Cross798bfce2016-10-12 14:28:16 -0700174 }
175 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700176 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700177 return mutator
178}
179
180type MutatorHandle interface {
181 Parallel() MutatorHandle
182}
183
184func (mutator *mutator) Parallel() MutatorHandle {
185 mutator.parallel = true
186 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700187}
Colin Cross1e676be2016-10-12 14:38:15 -0700188
189func depsMutator(ctx BottomUpMutatorContext) {
190 if m, ok := ctx.Module().(Module); ok {
191 m.DepsMutator(ctx)
192 }
193}
Colin Crossd11fcda2017-10-23 17:59:01 -0700194
195func (a *androidTopDownMutatorContext) VisitDirectDeps(visit func(Module)) {
196 a.TopDownMutatorContext.VisitDirectDeps(func(module blueprint.Module) {
197 if aModule, _ := module.(Module); aModule != nil {
198 visit(aModule)
199 }
200 })
201}
202
203func (a *androidTopDownMutatorContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
204 a.TopDownMutatorContext.VisitDirectDepsIf(
205 // pred
206 func(module blueprint.Module) bool {
207 if aModule, _ := module.(Module); aModule != nil {
208 return pred(aModule)
209 } else {
210 return false
211 }
212 },
213 // visit
214 func(module blueprint.Module) {
215 visit(module.(Module))
216 })
217}
218
219func (a *androidTopDownMutatorContext) VisitDepsDepthFirst(visit func(Module)) {
220 a.TopDownMutatorContext.VisitDepsDepthFirst(func(module blueprint.Module) {
221 if aModule, _ := module.(Module); aModule != nil {
222 visit(aModule)
223 }
224 })
225}
226
227func (a *androidTopDownMutatorContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
228 a.TopDownMutatorContext.VisitDepsDepthFirstIf(
229 // pred
230 func(module blueprint.Module) bool {
231 if aModule, _ := module.(Module); aModule != nil {
232 return pred(aModule)
233 } else {
234 return false
235 }
236 },
237 // visit
238 func(module blueprint.Module) {
239 visit(module.(Module))
240 })
241}
242
243func (a *androidTopDownMutatorContext) WalkDeps(visit func(Module, Module) bool) {
244 a.TopDownMutatorContext.WalkDeps(func(child, parent blueprint.Module) bool {
245 childAndroidModule, _ := child.(Module)
246 parentAndroidModule, _ := parent.(Module)
247 if childAndroidModule != nil && parentAndroidModule != nil {
248 return visit(childAndroidModule, parentAndroidModule)
249 } else {
250 return false
251 }
252 })
253}