blob: 6c9f17a34b6775fe1b36713e0cf23993c66fc8fa [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"
Colin Cross519917d2017-11-02 16:35:56 -070019 "github.com/google/blueprint/proptools"
Colin Cross795c3772017-03-16 16:50:10 -070020)
Colin Cross6362e272015-10-29 15:25:03 -070021
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070022// Phases:
23// run Pre-arch mutators
24// run archMutator
25// run Pre-deps mutators
26// run depsMutator
27// run PostDeps mutators
28// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070029
Colin Cross795c3772017-03-16 16:50:10 -070030func registerMutatorsToContext(ctx *blueprint.Context, mutators []*mutator) {
31 for _, t := range mutators {
32 var handle blueprint.MutatorHandle
33 if t.bottomUpMutator != nil {
34 handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator)
35 } else if t.topDownMutator != nil {
36 handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator)
37 }
38 if t.parallel {
39 handle.Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070040 }
41 }
Colin Cross1e676be2016-10-12 14:38:15 -070042}
43
Colin Crosscec81712017-07-13 14:43:27 -070044func registerMutators(ctx *blueprint.Context, preArch, preDeps, postDeps []RegisterMutatorFunc) {
45 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080046
47 register := func(funcs []RegisterMutatorFunc) {
48 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070049 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080050 }
51 }
52
Colin Crosscec81712017-07-13 14:43:27 -070053 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080054
Colin Crosscec81712017-07-13 14:43:27 -070055 register(preDeps)
56
57 mctx.BottomUp("deps", depsMutator).Parallel()
58
59 register(postDeps)
60
61 registerMutatorsToContext(ctx, mctx.mutators)
Colin Cross795c3772017-03-16 16:50:10 -070062}
63
64type registerMutatorsContext struct {
65 mutators []*mutator
66}
Colin Cross1e676be2016-10-12 14:38:15 -070067
68type RegisterMutatorsContext interface {
69 TopDown(name string, m AndroidTopDownMutator) MutatorHandle
70 BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle
71}
72
73type RegisterMutatorFunc func(RegisterMutatorsContext)
74
Colin Crosscec81712017-07-13 14:43:27 -070075var preArch = []RegisterMutatorFunc{
76 func(ctx RegisterMutatorsContext) {
Inseob Kimc0907f12019-02-08 21:00:45 +090077 ctx.TopDown("load_hooks", LoadHookMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -070078 },
Dan Willemsen6e72ef72018-01-26 18:27:02 -080079 RegisterNamespaceMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -070080 RegisterPrebuiltsPreArchMutators,
Colin Cross89536d42017-07-07 14:35:50 -070081 RegisterDefaultsPreArchMutators,
Colin Crosscec81712017-07-13 14:43:27 -070082}
83
Colin Crossae4c6182017-09-15 17:33:55 -070084func registerArchMutator(ctx RegisterMutatorsContext) {
85 ctx.BottomUp("arch", archMutator).Parallel()
86 ctx.TopDown("arch_hooks", archHookMutator).Parallel()
87}
88
Colin Crosscec81712017-07-13 14:43:27 -070089var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -070090 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -070091}
92
93var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -080094 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -070095 RegisterPrebuiltsPostDepsMutators,
Steven Moreland65b3fd92017-12-06 14:18:35 -080096 registerNeverallowMutator,
Colin Crosscec81712017-07-13 14:43:27 -070097}
Colin Cross1e676be2016-10-12 14:38:15 -070098
99func PreArchMutators(f RegisterMutatorFunc) {
100 preArch = append(preArch, f)
101}
102
103func PreDepsMutators(f RegisterMutatorFunc) {
104 preDeps = append(preDeps, f)
105}
106
107func PostDepsMutators(f RegisterMutatorFunc) {
108 postDeps = append(postDeps, f)
109}
110
Colin Cross635c3b02016-05-18 15:37:25 -0700111type AndroidTopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700112
Colin Cross635c3b02016-05-18 15:37:25 -0700113type TopDownMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800114 BaseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700115 androidBaseContext
Colin Cross3f68a132017-10-23 17:10:29 -0700116
117 OtherModuleExists(name string) bool
118 Rename(name string)
Colin Cross519917d2017-11-02 16:35:56 -0700119 Module() Module
Colin Cross3f68a132017-10-23 17:10:29 -0700120
121 OtherModuleName(m blueprint.Module) string
122 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
123 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
124
125 CreateModule(blueprint.ModuleFactory, ...interface{})
126
127 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
128 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
129
Colin Crossd11fcda2017-10-23 17:59:01 -0700130 VisitDirectDeps(visit func(Module))
Colin Crossee6143c2017-12-30 17:54:27 -0800131 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700132 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
133 VisitDepsDepthFirst(visit func(Module))
134 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
135 WalkDeps(visit func(Module, Module) bool)
Jooyung Hana70f0672019-01-18 15:20:43 +0900136 // GetWalkPath is supposed to be called in visit function passed in WalkDeps()
137 // and returns a top-down dependency path from a start module to current child module.
138 GetWalkPath() []Module
Colin Cross6362e272015-10-29 15:25:03 -0700139}
140
141type androidTopDownMutatorContext struct {
142 blueprint.TopDownMutatorContext
143 androidBaseContextImpl
Jooyung Hana70f0672019-01-18 15:20:43 +0900144 walkPath []Module
Colin Cross6362e272015-10-29 15:25:03 -0700145}
146
Colin Cross635c3b02016-05-18 15:37:25 -0700147type AndroidBottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700148
Colin Cross635c3b02016-05-18 15:37:25 -0700149type BottomUpMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800150 BaseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700151 androidBaseContext
Colin Crossaabf6792017-11-29 00:27:14 -0800152
153 OtherModuleExists(name string) bool
154 Rename(name string)
155 Module() blueprint.Module
156
157 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string)
158 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
159 CreateVariations(...string) []blueprint.Module
160 CreateLocalVariations(...string) []blueprint.Module
161 SetDependencyVariation(string)
162 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
163 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
164 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
165 ReplaceDependencies(string)
Colin Cross6362e272015-10-29 15:25:03 -0700166}
167
168type androidBottomUpMutatorContext struct {
169 blueprint.BottomUpMutatorContext
170 androidBaseContextImpl
171}
172
Colin Cross795c3772017-03-16 16:50:10 -0700173func (x *registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700174 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700175 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700176 actx := &androidBottomUpMutatorContext{
177 BottomUpMutatorContext: ctx,
178 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
179 }
Colin Cross798bfce2016-10-12 14:28:16 -0700180 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700181 }
Colin Cross798bfce2016-10-12 14:28:16 -0700182 }
183 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700184 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700185 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700186}
187
Colin Cross795c3772017-03-16 16:50:10 -0700188func (x *registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700189 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700190 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700191 actx := &androidTopDownMutatorContext{
192 TopDownMutatorContext: ctx,
193 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
194 }
Colin Cross798bfce2016-10-12 14:28:16 -0700195 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700196 }
Colin Cross798bfce2016-10-12 14:28:16 -0700197 }
198 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700199 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700200 return mutator
201}
202
203type MutatorHandle interface {
204 Parallel() MutatorHandle
205}
206
207func (mutator *mutator) Parallel() MutatorHandle {
208 mutator.parallel = true
209 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700210}
Colin Cross1e676be2016-10-12 14:38:15 -0700211
212func depsMutator(ctx BottomUpMutatorContext) {
Colin Cross6db4a6a2018-08-30 12:52:41 -0700213 if m, ok := ctx.Module().(Module); ok && m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700214 m.DepsMutator(ctx)
215 }
216}
Colin Crossd11fcda2017-10-23 17:59:01 -0700217
Colin Crossaabf6792017-11-29 00:27:14 -0800218func (a *androidTopDownMutatorContext) Config() Config {
219 return a.config
220}
221
222func (a *androidBottomUpMutatorContext) Config() Config {
223 return a.config
224}
225
Colin Cross519917d2017-11-02 16:35:56 -0700226func (a *androidTopDownMutatorContext) Module() Module {
227 module, _ := a.TopDownMutatorContext.Module().(Module)
228 return module
229}
230
Colin Crossd11fcda2017-10-23 17:59:01 -0700231func (a *androidTopDownMutatorContext) VisitDirectDeps(visit func(Module)) {
232 a.TopDownMutatorContext.VisitDirectDeps(func(module blueprint.Module) {
233 if aModule, _ := module.(Module); aModule != nil {
234 visit(aModule)
235 }
236 })
237}
238
Colin Crossee6143c2017-12-30 17:54:27 -0800239func (a *androidTopDownMutatorContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
240 a.TopDownMutatorContext.VisitDirectDeps(func(module blueprint.Module) {
241 if aModule, _ := module.(Module); aModule != nil {
242 if a.TopDownMutatorContext.OtherModuleDependencyTag(aModule) == tag {
243 visit(aModule)
244 }
245 }
246 })
247}
248
Colin Crossd11fcda2017-10-23 17:59:01 -0700249func (a *androidTopDownMutatorContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
250 a.TopDownMutatorContext.VisitDirectDepsIf(
251 // pred
252 func(module blueprint.Module) bool {
253 if aModule, _ := module.(Module); aModule != nil {
254 return pred(aModule)
255 } else {
256 return false
257 }
258 },
259 // visit
260 func(module blueprint.Module) {
261 visit(module.(Module))
262 })
263}
264
265func (a *androidTopDownMutatorContext) VisitDepsDepthFirst(visit func(Module)) {
266 a.TopDownMutatorContext.VisitDepsDepthFirst(func(module blueprint.Module) {
267 if aModule, _ := module.(Module); aModule != nil {
268 visit(aModule)
269 }
270 })
271}
272
273func (a *androidTopDownMutatorContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
274 a.TopDownMutatorContext.VisitDepsDepthFirstIf(
275 // pred
276 func(module blueprint.Module) bool {
277 if aModule, _ := module.(Module); aModule != nil {
278 return pred(aModule)
279 } else {
280 return false
281 }
282 },
283 // visit
284 func(module blueprint.Module) {
285 visit(module.(Module))
286 })
287}
288
289func (a *androidTopDownMutatorContext) WalkDeps(visit func(Module, Module) bool) {
Jooyung Hana70f0672019-01-18 15:20:43 +0900290 a.walkPath = []Module{a.Module()}
Colin Crossd11fcda2017-10-23 17:59:01 -0700291 a.TopDownMutatorContext.WalkDeps(func(child, parent blueprint.Module) bool {
292 childAndroidModule, _ := child.(Module)
293 parentAndroidModule, _ := parent.(Module)
294 if childAndroidModule != nil && parentAndroidModule != nil {
Jooyung Hana70f0672019-01-18 15:20:43 +0900295 // record walkPath before visit
296 for a.walkPath[len(a.walkPath)-1] != parentAndroidModule {
297 a.walkPath = a.walkPath[0 : len(a.walkPath)-1]
298 }
299 a.walkPath = append(a.walkPath, childAndroidModule)
Colin Crossd11fcda2017-10-23 17:59:01 -0700300 return visit(childAndroidModule, parentAndroidModule)
301 } else {
302 return false
303 }
304 })
305}
Colin Cross519917d2017-11-02 16:35:56 -0700306
Jooyung Hana70f0672019-01-18 15:20:43 +0900307func (a *androidTopDownMutatorContext) GetWalkPath() []Module {
308 return a.walkPath
309}
310
Colin Cross519917d2017-11-02 16:35:56 -0700311func (a *androidTopDownMutatorContext) AppendProperties(props ...interface{}) {
312 for _, p := range props {
313 err := proptools.AppendMatchingProperties(a.Module().base().customizableProperties,
314 p, nil)
315 if err != nil {
316 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
317 a.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
318 } else {
319 panic(err)
320 }
321 }
322 }
323}
324
325func (a *androidTopDownMutatorContext) PrependProperties(props ...interface{}) {
326 for _, p := range props {
327 err := proptools.PrependMatchingProperties(a.Module().base().customizableProperties,
328 p, nil)
329 if err != nil {
330 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
331 a.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
332 } else {
333 panic(err)
334 }
335 }
336 }
337}