blob: 88ac52117a5e1db9adc16fcf6da9aa8b790ef673 [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 Cross18c46802019-09-24 22:19:02 -070018 "reflect"
19
Colin Cross795c3772017-03-16 16:50:10 -070020 "github.com/google/blueprint"
Colin Cross519917d2017-11-02 16:35:56 -070021 "github.com/google/blueprint/proptools"
Colin Cross795c3772017-03-16 16:50:10 -070022)
Colin Cross6362e272015-10-29 15:25:03 -070023
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070024// Phases:
25// run Pre-arch mutators
26// run archMutator
27// run Pre-deps mutators
28// run depsMutator
29// run PostDeps mutators
30// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070031
Colin Cross795c3772017-03-16 16:50:10 -070032func registerMutatorsToContext(ctx *blueprint.Context, mutators []*mutator) {
33 for _, t := range mutators {
34 var handle blueprint.MutatorHandle
35 if t.bottomUpMutator != nil {
36 handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator)
37 } else if t.topDownMutator != nil {
38 handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator)
39 }
40 if t.parallel {
41 handle.Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070042 }
43 }
Colin Cross1e676be2016-10-12 14:38:15 -070044}
45
Colin Crosscec81712017-07-13 14:43:27 -070046func registerMutators(ctx *blueprint.Context, preArch, preDeps, postDeps []RegisterMutatorFunc) {
47 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080048
49 register := func(funcs []RegisterMutatorFunc) {
50 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070051 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080052 }
53 }
54
Colin Crosscec81712017-07-13 14:43:27 -070055 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080056
Colin Crosscec81712017-07-13 14:43:27 -070057 register(preDeps)
58
59 mctx.BottomUp("deps", depsMutator).Parallel()
60
61 register(postDeps)
62
63 registerMutatorsToContext(ctx, mctx.mutators)
Colin Cross795c3772017-03-16 16:50:10 -070064}
65
66type registerMutatorsContext struct {
67 mutators []*mutator
68}
Colin Cross1e676be2016-10-12 14:38:15 -070069
70type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -070071 TopDown(name string, m TopDownMutator) MutatorHandle
72 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -070073}
74
75type RegisterMutatorFunc func(RegisterMutatorsContext)
76
Colin Crosscec81712017-07-13 14:43:27 -070077var preArch = []RegisterMutatorFunc{
Colin Crossf8b860a2019-04-16 14:43:28 -070078 registerLoadHookMutator,
Dan Willemsen6e72ef72018-01-26 18:27:02 -080079 RegisterNamespaceMutator,
Paul Duffine2453c72019-05-31 14:00:04 +010080 // Rename package module types.
81 registerPackageRenamer,
Colin Cross5ea9bcc2017-07-27 15:41:32 -070082 RegisterPrebuiltsPreArchMutators,
Martin Stjernholm226b20d2019-05-17 22:42:02 +010083 registerVisibilityRuleChecker,
Colin Cross89536d42017-07-07 14:35:50 -070084 RegisterDefaultsPreArchMutators,
Paul Duffin2e61fa62019-03-28 14:10:57 +000085 registerVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -070086}
87
Colin Crossae4c6182017-09-15 17:33:55 -070088func registerArchMutator(ctx RegisterMutatorsContext) {
89 ctx.BottomUp("arch", archMutator).Parallel()
90 ctx.TopDown("arch_hooks", archHookMutator).Parallel()
91}
92
Colin Crosscec81712017-07-13 14:43:27 -070093var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -070094 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -070095}
96
97var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -080098 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -070099 RegisterPrebuiltsPostDepsMutators,
Paul Duffin2e61fa62019-03-28 14:10:57 +0000100 registerVisibilityRuleEnforcer,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800101 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700102 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700103}
Colin Cross1e676be2016-10-12 14:38:15 -0700104
105func PreArchMutators(f RegisterMutatorFunc) {
106 preArch = append(preArch, f)
107}
108
109func PreDepsMutators(f RegisterMutatorFunc) {
110 preDeps = append(preDeps, f)
111}
112
113func PostDepsMutators(f RegisterMutatorFunc) {
114 postDeps = append(postDeps, f)
115}
116
Colin Cross25de6c32019-06-06 14:29:25 -0700117type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700118
Colin Cross635c3b02016-05-18 15:37:25 -0700119type TopDownMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800120 BaseModuleContext
Colin Cross3f68a132017-10-23 17:10:29 -0700121
Colin Crosscb55e082019-07-01 15:32:31 -0700122 MutatorName() string
123
Colin Cross3f68a132017-10-23 17:10:29 -0700124 Rename(name string)
Colin Cross3f68a132017-10-23 17:10:29 -0700125
Colin Crosse003c4a2019-09-25 12:58:36 -0700126 CreateModule(ModuleFactory, ...interface{}) Module
Colin Cross6362e272015-10-29 15:25:03 -0700127}
128
Colin Cross25de6c32019-06-06 14:29:25 -0700129type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700130 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700131 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700132}
133
Colin Cross25de6c32019-06-06 14:29:25 -0700134type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700135
Colin Cross635c3b02016-05-18 15:37:25 -0700136type BottomUpMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800137 BaseModuleContext
Colin Crossaabf6792017-11-29 00:27:14 -0800138
Colin Crosscb55e082019-07-01 15:32:31 -0700139 MutatorName() string
140
Colin Crossaabf6792017-11-29 00:27:14 -0800141 Rename(name string)
Colin Crossaabf6792017-11-29 00:27:14 -0800142
143 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string)
144 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
145 CreateVariations(...string) []blueprint.Module
146 CreateLocalVariations(...string) []blueprint.Module
147 SetDependencyVariation(string)
Jiyong Park1d1119f2019-07-29 21:27:18 +0900148 SetDefaultDependencyVariation(*string)
Colin Crossaabf6792017-11-29 00:27:14 -0800149 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
150 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
151 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
152 ReplaceDependencies(string)
Colin Cross6362e272015-10-29 15:25:03 -0700153}
154
Colin Cross25de6c32019-06-06 14:29:25 -0700155type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700156 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700157 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700158}
159
Colin Cross25de6c32019-06-06 14:29:25 -0700160func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700161 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700162 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700163 actx := &bottomUpMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700164 bp: ctx,
165 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700166 }
Colin Cross798bfce2016-10-12 14:28:16 -0700167 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700168 }
Colin Cross798bfce2016-10-12 14:28:16 -0700169 }
170 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700171 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700172 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700173}
174
Colin Cross25de6c32019-06-06 14:29:25 -0700175func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700176 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700177 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700178 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700179 bp: ctx,
180 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700181 }
Colin Cross798bfce2016-10-12 14:28:16 -0700182 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700183 }
Colin Cross798bfce2016-10-12 14:28:16 -0700184 }
185 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700186 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700187 return mutator
188}
189
190type MutatorHandle interface {
191 Parallel() MutatorHandle
192}
193
194func (mutator *mutator) Parallel() MutatorHandle {
195 mutator.parallel = true
196 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700197}
Colin Cross1e676be2016-10-12 14:38:15 -0700198
199func depsMutator(ctx BottomUpMutatorContext) {
Colin Cross6db4a6a2018-08-30 12:52:41 -0700200 if m, ok := ctx.Module().(Module); ok && m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700201 m.DepsMutator(ctx)
202 }
203}
Colin Crossd11fcda2017-10-23 17:59:01 -0700204
Colin Cross25de6c32019-06-06 14:29:25 -0700205func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700206 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700207 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700208 p, nil)
209 if err != nil {
210 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700211 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700212 } else {
213 panic(err)
214 }
215 }
216 }
217}
218
Colin Cross25de6c32019-06-06 14:29:25 -0700219func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700220 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700221 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700222 p, nil)
223 if err != nil {
224 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700225 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700226 } else {
227 panic(err)
228 }
229 }
230 }
231}
Colin Crossdc35e212019-06-06 16:13:11 -0700232
233// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
234// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
235// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
236// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
237// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
238
Colin Crosscb55e082019-07-01 15:32:31 -0700239func (t *topDownMutatorContext) MutatorName() string {
240 return t.bp.MutatorName()
241}
242
Colin Crossdc35e212019-06-06 16:13:11 -0700243func (t *topDownMutatorContext) Rename(name string) {
244 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700245 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700246}
247
Colin Crosse003c4a2019-09-25 12:58:36 -0700248func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Colin Cross18c46802019-09-24 22:19:02 -0700249 inherited := []interface{}{&t.Module().base().commonProperties}
Colin Crosse003c4a2019-09-25 12:58:36 -0700250 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
Colin Cross18c46802019-09-24 22:19:02 -0700251
252 if t.Module().base().variableProperties != nil && module.base().variableProperties != nil {
253 src := t.Module().base().variableProperties
254 dst := []interface{}{
255 module.base().variableProperties,
256 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
257 // don't cause a "failed to find property to extend" error.
258 proptools.CloneEmptyProperties(reflect.ValueOf(src).Elem()).Interface(),
259 }
260 err := proptools.AppendMatchingProperties(dst, src, nil)
261 if err != nil {
262 panic(err)
263 }
264 }
265
Colin Crosse003c4a2019-09-25 12:58:36 -0700266 return module
Colin Crossdc35e212019-06-06 16:13:11 -0700267}
268
Colin Crosscb55e082019-07-01 15:32:31 -0700269func (b *bottomUpMutatorContext) MutatorName() string {
270 return b.bp.MutatorName()
271}
272
Colin Crossdc35e212019-06-06 16:13:11 -0700273func (b *bottomUpMutatorContext) Rename(name string) {
274 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700275 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700276}
277
278func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) {
279 b.bp.AddDependency(module, tag, name...)
280}
281
282func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
283 b.bp.AddReverseDependency(module, tag, name)
284}
285
286func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []blueprint.Module {
Colin Cross9a362232019-07-01 15:32:45 -0700287 modules := b.bp.CreateVariations(variations...)
288
289 for i := range variations {
290 base := modules[i].(Module).base()
291 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
292 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
293 }
294
295 return modules
Colin Crossdc35e212019-06-06 16:13:11 -0700296}
297
298func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []blueprint.Module {
Colin Cross9a362232019-07-01 15:32:45 -0700299 modules := b.bp.CreateLocalVariations(variations...)
300
301 for i := range variations {
302 base := modules[i].(Module).base()
303 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
304 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
305 }
306
307 return modules
Colin Crossdc35e212019-06-06 16:13:11 -0700308}
309
310func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
311 b.bp.SetDependencyVariation(variation)
312}
313
Jiyong Park1d1119f2019-07-29 21:27:18 +0900314func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
315 b.bp.SetDefaultDependencyVariation(variation)
316}
317
Colin Crossdc35e212019-06-06 16:13:11 -0700318func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
319 names ...string) {
320
321 b.bp.AddVariationDependencies(variations, tag, names...)
322}
323
324func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
325 tag blueprint.DependencyTag, names ...string) {
326
327 b.bp.AddFarVariationDependencies(variations, tag, names...)
328}
329
330func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
331 b.bp.AddInterVariantDependency(tag, from, to)
332}
333
334func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
335 b.bp.ReplaceDependencies(name)
336}