blob: 71237a1cd63598f91ed419f4ca2eb67351e193b0 [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,
Jaewoong Jung525443a2019-02-28 15:35:54 -080082 RegisterOverridePreArchMutators,
Colin Crosscec81712017-07-13 14:43:27 -070083}
84
Colin Crossae4c6182017-09-15 17:33:55 -070085func registerArchMutator(ctx RegisterMutatorsContext) {
86 ctx.BottomUp("arch", archMutator).Parallel()
87 ctx.TopDown("arch_hooks", archHookMutator).Parallel()
88}
89
Colin Crosscec81712017-07-13 14:43:27 -070090var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -070091 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -070092}
93
94var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -080095 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -070096 RegisterPrebuiltsPostDepsMutators,
Steven Moreland65b3fd92017-12-06 14:18:35 -080097 registerNeverallowMutator,
Colin Crosscec81712017-07-13 14:43:27 -070098}
Colin Cross1e676be2016-10-12 14:38:15 -070099
100func PreArchMutators(f RegisterMutatorFunc) {
101 preArch = append(preArch, f)
102}
103
104func PreDepsMutators(f RegisterMutatorFunc) {
105 preDeps = append(preDeps, f)
106}
107
108func PostDepsMutators(f RegisterMutatorFunc) {
109 postDeps = append(postDeps, f)
110}
111
Colin Cross635c3b02016-05-18 15:37:25 -0700112type AndroidTopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700113
Colin Cross635c3b02016-05-18 15:37:25 -0700114type TopDownMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800115 BaseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700116 androidBaseContext
Colin Cross3f68a132017-10-23 17:10:29 -0700117
118 OtherModuleExists(name string) bool
119 Rename(name string)
Colin Cross519917d2017-11-02 16:35:56 -0700120 Module() Module
Colin Cross3f68a132017-10-23 17:10:29 -0700121
122 OtherModuleName(m blueprint.Module) string
123 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
124 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
125
126 CreateModule(blueprint.ModuleFactory, ...interface{})
127
128 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
129 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
130
Colin Crossd11fcda2017-10-23 17:59:01 -0700131 VisitDirectDeps(visit func(Module))
Colin Crossee6143c2017-12-30 17:54:27 -0800132 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700133 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
134 VisitDepsDepthFirst(visit func(Module))
135 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
136 WalkDeps(visit func(Module, Module) bool)
Jooyung Hana70f0672019-01-18 15:20:43 +0900137 // GetWalkPath is supposed to be called in visit function passed in WalkDeps()
138 // and returns a top-down dependency path from a start module to current child module.
139 GetWalkPath() []Module
Colin Cross6362e272015-10-29 15:25:03 -0700140}
141
142type androidTopDownMutatorContext struct {
143 blueprint.TopDownMutatorContext
144 androidBaseContextImpl
Jooyung Hana70f0672019-01-18 15:20:43 +0900145 walkPath []Module
Colin Cross6362e272015-10-29 15:25:03 -0700146}
147
Colin Cross635c3b02016-05-18 15:37:25 -0700148type AndroidBottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700149
Colin Cross635c3b02016-05-18 15:37:25 -0700150type BottomUpMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800151 BaseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700152 androidBaseContext
Colin Crossaabf6792017-11-29 00:27:14 -0800153
154 OtherModuleExists(name string) bool
155 Rename(name string)
156 Module() blueprint.Module
157
158 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string)
159 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
160 CreateVariations(...string) []blueprint.Module
161 CreateLocalVariations(...string) []blueprint.Module
162 SetDependencyVariation(string)
163 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
164 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
165 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
166 ReplaceDependencies(string)
Colin Cross6362e272015-10-29 15:25:03 -0700167}
168
169type androidBottomUpMutatorContext struct {
170 blueprint.BottomUpMutatorContext
171 androidBaseContextImpl
172}
173
Colin Cross795c3772017-03-16 16:50:10 -0700174func (x *registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700175 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700176 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700177 actx := &androidBottomUpMutatorContext{
178 BottomUpMutatorContext: ctx,
179 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
180 }
Colin Cross798bfce2016-10-12 14:28:16 -0700181 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700182 }
Colin Cross798bfce2016-10-12 14:28:16 -0700183 }
184 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700185 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700186 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700187}
188
Colin Cross795c3772017-03-16 16:50:10 -0700189func (x *registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700190 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700191 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700192 actx := &androidTopDownMutatorContext{
193 TopDownMutatorContext: ctx,
194 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
195 }
Colin Cross798bfce2016-10-12 14:28:16 -0700196 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700197 }
Colin Cross798bfce2016-10-12 14:28:16 -0700198 }
199 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700200 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700201 return mutator
202}
203
204type MutatorHandle interface {
205 Parallel() MutatorHandle
206}
207
208func (mutator *mutator) Parallel() MutatorHandle {
209 mutator.parallel = true
210 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700211}
Colin Cross1e676be2016-10-12 14:38:15 -0700212
213func depsMutator(ctx BottomUpMutatorContext) {
Colin Cross6db4a6a2018-08-30 12:52:41 -0700214 if m, ok := ctx.Module().(Module); ok && m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700215 m.DepsMutator(ctx)
216 }
217}
Colin Crossd11fcda2017-10-23 17:59:01 -0700218
Colin Crossaabf6792017-11-29 00:27:14 -0800219func (a *androidTopDownMutatorContext) Config() Config {
220 return a.config
221}
222
223func (a *androidBottomUpMutatorContext) Config() Config {
224 return a.config
225}
226
Colin Cross519917d2017-11-02 16:35:56 -0700227func (a *androidTopDownMutatorContext) Module() Module {
228 module, _ := a.TopDownMutatorContext.Module().(Module)
229 return module
230}
231
Colin Crossd11fcda2017-10-23 17:59:01 -0700232func (a *androidTopDownMutatorContext) VisitDirectDeps(visit func(Module)) {
233 a.TopDownMutatorContext.VisitDirectDeps(func(module blueprint.Module) {
234 if aModule, _ := module.(Module); aModule != nil {
235 visit(aModule)
236 }
237 })
238}
239
Colin Crossee6143c2017-12-30 17:54:27 -0800240func (a *androidTopDownMutatorContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
241 a.TopDownMutatorContext.VisitDirectDeps(func(module blueprint.Module) {
242 if aModule, _ := module.(Module); aModule != nil {
243 if a.TopDownMutatorContext.OtherModuleDependencyTag(aModule) == tag {
244 visit(aModule)
245 }
246 }
247 })
248}
249
Colin Crossd11fcda2017-10-23 17:59:01 -0700250func (a *androidTopDownMutatorContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
251 a.TopDownMutatorContext.VisitDirectDepsIf(
252 // pred
253 func(module blueprint.Module) bool {
254 if aModule, _ := module.(Module); aModule != nil {
255 return pred(aModule)
256 } else {
257 return false
258 }
259 },
260 // visit
261 func(module blueprint.Module) {
262 visit(module.(Module))
263 })
264}
265
266func (a *androidTopDownMutatorContext) VisitDepsDepthFirst(visit func(Module)) {
267 a.TopDownMutatorContext.VisitDepsDepthFirst(func(module blueprint.Module) {
268 if aModule, _ := module.(Module); aModule != nil {
269 visit(aModule)
270 }
271 })
272}
273
274func (a *androidTopDownMutatorContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
275 a.TopDownMutatorContext.VisitDepsDepthFirstIf(
276 // pred
277 func(module blueprint.Module) bool {
278 if aModule, _ := module.(Module); aModule != nil {
279 return pred(aModule)
280 } else {
281 return false
282 }
283 },
284 // visit
285 func(module blueprint.Module) {
286 visit(module.(Module))
287 })
288}
289
290func (a *androidTopDownMutatorContext) WalkDeps(visit func(Module, Module) bool) {
Jooyung Hana70f0672019-01-18 15:20:43 +0900291 a.walkPath = []Module{a.Module()}
Colin Crossd11fcda2017-10-23 17:59:01 -0700292 a.TopDownMutatorContext.WalkDeps(func(child, parent blueprint.Module) bool {
293 childAndroidModule, _ := child.(Module)
294 parentAndroidModule, _ := parent.(Module)
295 if childAndroidModule != nil && parentAndroidModule != nil {
Jooyung Hana70f0672019-01-18 15:20:43 +0900296 // record walkPath before visit
297 for a.walkPath[len(a.walkPath)-1] != parentAndroidModule {
298 a.walkPath = a.walkPath[0 : len(a.walkPath)-1]
299 }
300 a.walkPath = append(a.walkPath, childAndroidModule)
Colin Crossd11fcda2017-10-23 17:59:01 -0700301 return visit(childAndroidModule, parentAndroidModule)
302 } else {
303 return false
304 }
305 })
306}
Colin Cross519917d2017-11-02 16:35:56 -0700307
Jooyung Hana70f0672019-01-18 15:20:43 +0900308func (a *androidTopDownMutatorContext) GetWalkPath() []Module {
309 return a.walkPath
310}
311
Colin Cross519917d2017-11-02 16:35:56 -0700312func (a *androidTopDownMutatorContext) AppendProperties(props ...interface{}) {
313 for _, p := range props {
314 err := proptools.AppendMatchingProperties(a.Module().base().customizableProperties,
315 p, nil)
316 if err != nil {
317 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
318 a.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
319 } else {
320 panic(err)
321 }
322 }
323 }
324}
325
326func (a *androidTopDownMutatorContext) PrependProperties(props ...interface{}) {
327 for _, p := range props {
328 err := proptools.PrependMatchingProperties(a.Module().base().customizableProperties,
329 p, nil)
330 if err != nil {
331 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
332 a.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
333 } else {
334 panic(err)
335 }
336 }
337 }
338}