blob: b9c44e83acc18f0c1f4a77dff3217b9491d15c75 [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) {
77 ctx.TopDown("load_hooks", loadHookMutator).Parallel()
78 },
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 Cross5ea9bcc2017-07-27 15:41:32 -070094 RegisterPrebuiltsPostDepsMutators,
Steven Moreland65b3fd92017-12-06 14:18:35 -080095 registerNeverallowMutator,
Colin Crosscec81712017-07-13 14:43:27 -070096}
Colin Cross1e676be2016-10-12 14:38:15 -070097
98func PreArchMutators(f RegisterMutatorFunc) {
99 preArch = append(preArch, f)
100}
101
102func PreDepsMutators(f RegisterMutatorFunc) {
103 preDeps = append(preDeps, f)
104}
105
106func PostDepsMutators(f RegisterMutatorFunc) {
107 postDeps = append(postDeps, f)
108}
109
Colin Cross635c3b02016-05-18 15:37:25 -0700110type AndroidTopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700111
Colin Cross635c3b02016-05-18 15:37:25 -0700112type TopDownMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800113 BaseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700114 androidBaseContext
Colin Cross3f68a132017-10-23 17:10:29 -0700115
116 OtherModuleExists(name string) bool
117 Rename(name string)
Colin Cross519917d2017-11-02 16:35:56 -0700118 Module() Module
Colin Cross3f68a132017-10-23 17:10:29 -0700119
120 OtherModuleName(m blueprint.Module) string
121 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
122 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
123
124 CreateModule(blueprint.ModuleFactory, ...interface{})
125
126 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
127 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
128
Colin Crossd11fcda2017-10-23 17:59:01 -0700129 VisitDirectDeps(visit func(Module))
Colin Crossee6143c2017-12-30 17:54:27 -0800130 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700131 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
132 VisitDepsDepthFirst(visit func(Module))
133 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
134 WalkDeps(visit func(Module, Module) bool)
Colin Cross6362e272015-10-29 15:25:03 -0700135}
136
137type androidTopDownMutatorContext struct {
138 blueprint.TopDownMutatorContext
139 androidBaseContextImpl
140}
141
Colin Cross635c3b02016-05-18 15:37:25 -0700142type AndroidBottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700143
Colin Cross635c3b02016-05-18 15:37:25 -0700144type BottomUpMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800145 BaseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700146 androidBaseContext
Colin Crossaabf6792017-11-29 00:27:14 -0800147
148 OtherModuleExists(name string) bool
149 Rename(name string)
150 Module() blueprint.Module
151
152 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string)
153 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
154 CreateVariations(...string) []blueprint.Module
155 CreateLocalVariations(...string) []blueprint.Module
156 SetDependencyVariation(string)
157 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
158 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
159 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
160 ReplaceDependencies(string)
Colin Cross6362e272015-10-29 15:25:03 -0700161}
162
163type androidBottomUpMutatorContext struct {
164 blueprint.BottomUpMutatorContext
165 androidBaseContextImpl
166}
167
Colin Cross795c3772017-03-16 16:50:10 -0700168func (x *registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700169 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700170 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700171 actx := &androidBottomUpMutatorContext{
172 BottomUpMutatorContext: ctx,
173 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
174 }
Colin Cross798bfce2016-10-12 14:28:16 -0700175 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700176 }
Colin Cross798bfce2016-10-12 14:28:16 -0700177 }
178 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700179 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700180 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700181}
182
Colin Cross795c3772017-03-16 16:50:10 -0700183func (x *registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700184 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700185 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700186 actx := &androidTopDownMutatorContext{
187 TopDownMutatorContext: ctx,
188 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
189 }
Colin Cross798bfce2016-10-12 14:28:16 -0700190 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700191 }
Colin Cross798bfce2016-10-12 14:28:16 -0700192 }
193 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700194 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700195 return mutator
196}
197
198type MutatorHandle interface {
199 Parallel() MutatorHandle
200}
201
202func (mutator *mutator) Parallel() MutatorHandle {
203 mutator.parallel = true
204 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700205}
Colin Cross1e676be2016-10-12 14:38:15 -0700206
207func depsMutator(ctx BottomUpMutatorContext) {
Colin Cross6db4a6a2018-08-30 12:52:41 -0700208 if m, ok := ctx.Module().(Module); ok && m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700209 m.DepsMutator(ctx)
210 }
211}
Colin Crossd11fcda2017-10-23 17:59:01 -0700212
Colin Crossaabf6792017-11-29 00:27:14 -0800213func (a *androidTopDownMutatorContext) Config() Config {
214 return a.config
215}
216
217func (a *androidBottomUpMutatorContext) Config() Config {
218 return a.config
219}
220
Colin Cross519917d2017-11-02 16:35:56 -0700221func (a *androidTopDownMutatorContext) Module() Module {
222 module, _ := a.TopDownMutatorContext.Module().(Module)
223 return module
224}
225
Colin Crossd11fcda2017-10-23 17:59:01 -0700226func (a *androidTopDownMutatorContext) VisitDirectDeps(visit func(Module)) {
227 a.TopDownMutatorContext.VisitDirectDeps(func(module blueprint.Module) {
228 if aModule, _ := module.(Module); aModule != nil {
229 visit(aModule)
230 }
231 })
232}
233
Colin Crossee6143c2017-12-30 17:54:27 -0800234func (a *androidTopDownMutatorContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
235 a.TopDownMutatorContext.VisitDirectDeps(func(module blueprint.Module) {
236 if aModule, _ := module.(Module); aModule != nil {
237 if a.TopDownMutatorContext.OtherModuleDependencyTag(aModule) == tag {
238 visit(aModule)
239 }
240 }
241 })
242}
243
Colin Crossd11fcda2017-10-23 17:59:01 -0700244func (a *androidTopDownMutatorContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
245 a.TopDownMutatorContext.VisitDirectDepsIf(
246 // pred
247 func(module blueprint.Module) bool {
248 if aModule, _ := module.(Module); aModule != nil {
249 return pred(aModule)
250 } else {
251 return false
252 }
253 },
254 // visit
255 func(module blueprint.Module) {
256 visit(module.(Module))
257 })
258}
259
260func (a *androidTopDownMutatorContext) VisitDepsDepthFirst(visit func(Module)) {
261 a.TopDownMutatorContext.VisitDepsDepthFirst(func(module blueprint.Module) {
262 if aModule, _ := module.(Module); aModule != nil {
263 visit(aModule)
264 }
265 })
266}
267
268func (a *androidTopDownMutatorContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
269 a.TopDownMutatorContext.VisitDepsDepthFirstIf(
270 // pred
271 func(module blueprint.Module) bool {
272 if aModule, _ := module.(Module); aModule != nil {
273 return pred(aModule)
274 } else {
275 return false
276 }
277 },
278 // visit
279 func(module blueprint.Module) {
280 visit(module.(Module))
281 })
282}
283
284func (a *androidTopDownMutatorContext) WalkDeps(visit func(Module, Module) bool) {
285 a.TopDownMutatorContext.WalkDeps(func(child, parent blueprint.Module) bool {
286 childAndroidModule, _ := child.(Module)
287 parentAndroidModule, _ := parent.(Module)
288 if childAndroidModule != nil && parentAndroidModule != nil {
289 return visit(childAndroidModule, parentAndroidModule)
290 } else {
291 return false
292 }
293 })
294}
Colin Cross519917d2017-11-02 16:35:56 -0700295
296func (a *androidTopDownMutatorContext) AppendProperties(props ...interface{}) {
297 for _, p := range props {
298 err := proptools.AppendMatchingProperties(a.Module().base().customizableProperties,
299 p, nil)
300 if err != nil {
301 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
302 a.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
303 } else {
304 panic(err)
305 }
306 }
307 }
308}
309
310func (a *androidTopDownMutatorContext) PrependProperties(props ...interface{}) {
311 for _, p := range props {
312 err := proptools.PrependMatchingProperties(a.Module().base().customizableProperties,
313 p, nil)
314 if err != nil {
315 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
316 a.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
317 } else {
318 panic(err)
319 }
320 }
321 }
322}