blob: 4a5338fcdb4ff58fd9db9969c6448b88151e2a83 [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) {
Colin Crossa195f912019-10-16 11:07:20 -070089 ctx.BottomUp("os", osMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -070090 ctx.BottomUp("arch", archMutator).Parallel()
91 ctx.TopDown("arch_hooks", archHookMutator).Parallel()
92}
93
Colin Crosscec81712017-07-13 14:43:27 -070094var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -070095 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -070096}
97
98var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -080099 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700100 RegisterPrebuiltsPostDepsMutators,
Paul Duffin2e61fa62019-03-28 14:10:57 +0000101 registerVisibilityRuleEnforcer,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800102 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700103 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700104}
Colin Cross1e676be2016-10-12 14:38:15 -0700105
106func PreArchMutators(f RegisterMutatorFunc) {
107 preArch = append(preArch, f)
108}
109
110func PreDepsMutators(f RegisterMutatorFunc) {
111 preDeps = append(preDeps, f)
112}
113
114func PostDepsMutators(f RegisterMutatorFunc) {
115 postDeps = append(postDeps, f)
116}
117
Colin Cross25de6c32019-06-06 14:29:25 -0700118type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700119
Colin Cross635c3b02016-05-18 15:37:25 -0700120type TopDownMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800121 BaseModuleContext
Colin Cross3f68a132017-10-23 17:10:29 -0700122
Colin Crosscb55e082019-07-01 15:32:31 -0700123 MutatorName() string
124
Colin Cross3f68a132017-10-23 17:10:29 -0700125 Rename(name string)
Colin Cross3f68a132017-10-23 17:10:29 -0700126
Colin Crosse003c4a2019-09-25 12:58:36 -0700127 CreateModule(ModuleFactory, ...interface{}) Module
Colin Cross6362e272015-10-29 15:25:03 -0700128}
129
Colin Cross25de6c32019-06-06 14:29:25 -0700130type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700131 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700132 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700133}
134
Colin Cross25de6c32019-06-06 14:29:25 -0700135type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700136
Colin Cross635c3b02016-05-18 15:37:25 -0700137type BottomUpMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800138 BaseModuleContext
Colin Crossaabf6792017-11-29 00:27:14 -0800139
Colin Crosscb55e082019-07-01 15:32:31 -0700140 MutatorName() string
141
Colin Crossaabf6792017-11-29 00:27:14 -0800142 Rename(name string)
Colin Crossaabf6792017-11-29 00:27:14 -0800143
144 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string)
145 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
146 CreateVariations(...string) []blueprint.Module
147 CreateLocalVariations(...string) []blueprint.Module
148 SetDependencyVariation(string)
Jiyong Park1d1119f2019-07-29 21:27:18 +0900149 SetDefaultDependencyVariation(*string)
Colin Crossaabf6792017-11-29 00:27:14 -0800150 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
151 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
152 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
153 ReplaceDependencies(string)
Colin Cross6362e272015-10-29 15:25:03 -0700154}
155
Colin Cross25de6c32019-06-06 14:29:25 -0700156type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700157 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700158 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700159}
160
Colin Cross25de6c32019-06-06 14:29:25 -0700161func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700162 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700163 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700164 actx := &bottomUpMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700165 bp: ctx,
166 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700167 }
Colin Cross798bfce2016-10-12 14:28:16 -0700168 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700169 }
Colin Cross798bfce2016-10-12 14:28:16 -0700170 }
171 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700172 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700173 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700174}
175
Colin Cross25de6c32019-06-06 14:29:25 -0700176func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700177 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700178 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700179 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700180 bp: ctx,
181 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700182 }
Colin Cross798bfce2016-10-12 14:28:16 -0700183 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700184 }
Colin Cross798bfce2016-10-12 14:28:16 -0700185 }
186 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700187 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700188 return mutator
189}
190
191type MutatorHandle interface {
192 Parallel() MutatorHandle
193}
194
195func (mutator *mutator) Parallel() MutatorHandle {
196 mutator.parallel = true
197 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700198}
Colin Cross1e676be2016-10-12 14:38:15 -0700199
200func depsMutator(ctx BottomUpMutatorContext) {
Colin Cross6db4a6a2018-08-30 12:52:41 -0700201 if m, ok := ctx.Module().(Module); ok && m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700202 m.DepsMutator(ctx)
203 }
204}
Colin Crossd11fcda2017-10-23 17:59:01 -0700205
Colin Cross25de6c32019-06-06 14:29:25 -0700206func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700207 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700208 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700209 p, nil)
210 if err != nil {
211 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700212 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700213 } else {
214 panic(err)
215 }
216 }
217 }
218}
219
Colin Cross25de6c32019-06-06 14:29:25 -0700220func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700221 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700222 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700223 p, nil)
224 if err != nil {
225 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700226 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700227 } else {
228 panic(err)
229 }
230 }
231 }
232}
Colin Crossdc35e212019-06-06 16:13:11 -0700233
234// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
235// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
236// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
237// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
238// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
239
Colin Crosscb55e082019-07-01 15:32:31 -0700240func (t *topDownMutatorContext) MutatorName() string {
241 return t.bp.MutatorName()
242}
243
Colin Crossdc35e212019-06-06 16:13:11 -0700244func (t *topDownMutatorContext) Rename(name string) {
245 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700246 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700247}
248
Colin Crosse003c4a2019-09-25 12:58:36 -0700249func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Colin Cross18c46802019-09-24 22:19:02 -0700250 inherited := []interface{}{&t.Module().base().commonProperties}
Colin Crosse003c4a2019-09-25 12:58:36 -0700251 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
Colin Cross18c46802019-09-24 22:19:02 -0700252
253 if t.Module().base().variableProperties != nil && module.base().variableProperties != nil {
254 src := t.Module().base().variableProperties
255 dst := []interface{}{
256 module.base().variableProperties,
257 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
258 // don't cause a "failed to find property to extend" error.
259 proptools.CloneEmptyProperties(reflect.ValueOf(src).Elem()).Interface(),
260 }
261 err := proptools.AppendMatchingProperties(dst, src, nil)
262 if err != nil {
263 panic(err)
264 }
265 }
266
Colin Crosse003c4a2019-09-25 12:58:36 -0700267 return module
Colin Crossdc35e212019-06-06 16:13:11 -0700268}
269
Colin Crosscb55e082019-07-01 15:32:31 -0700270func (b *bottomUpMutatorContext) MutatorName() string {
271 return b.bp.MutatorName()
272}
273
Colin Crossdc35e212019-06-06 16:13:11 -0700274func (b *bottomUpMutatorContext) Rename(name string) {
275 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700276 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700277}
278
279func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) {
280 b.bp.AddDependency(module, tag, name...)
281}
282
283func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
284 b.bp.AddReverseDependency(module, tag, name)
285}
286
287func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []blueprint.Module {
Colin Cross9a362232019-07-01 15:32:45 -0700288 modules := b.bp.CreateVariations(variations...)
289
290 for i := range variations {
291 base := modules[i].(Module).base()
292 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
293 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
294 }
295
296 return modules
Colin Crossdc35e212019-06-06 16:13:11 -0700297}
298
299func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []blueprint.Module {
Colin Cross9a362232019-07-01 15:32:45 -0700300 modules := b.bp.CreateLocalVariations(variations...)
301
302 for i := range variations {
303 base := modules[i].(Module).base()
304 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
305 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
306 }
307
308 return modules
Colin Crossdc35e212019-06-06 16:13:11 -0700309}
310
311func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
312 b.bp.SetDependencyVariation(variation)
313}
314
Jiyong Park1d1119f2019-07-29 21:27:18 +0900315func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
316 b.bp.SetDefaultDependencyVariation(variation)
317}
318
Colin Crossdc35e212019-06-06 16:13:11 -0700319func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
320 names ...string) {
321
322 b.bp.AddVariationDependencies(variations, tag, names...)
323}
324
325func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
326 tag blueprint.DependencyTag, names ...string) {
327
328 b.bp.AddFarVariationDependencies(variations, tag, names...)
329}
330
331func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
332 b.bp.AddInterVariantDependency(tag, from, to)
333}
334
335func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
336 b.bp.ReplaceDependencies(name)
337}