blob: df68726d2ee883790206bf779efed814c20b378d [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)
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800154 AliasVariation(variationName string)
Colin Cross6362e272015-10-29 15:25:03 -0700155}
156
Colin Cross25de6c32019-06-06 14:29:25 -0700157type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700158 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700159 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700160}
161
Colin Cross25de6c32019-06-06 14:29:25 -0700162func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700163 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700164 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700165 actx := &bottomUpMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700166 bp: ctx,
167 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700168 }
Colin Cross798bfce2016-10-12 14:28:16 -0700169 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700170 }
Colin Cross798bfce2016-10-12 14:28:16 -0700171 }
172 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700173 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700174 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700175}
176
Colin Cross25de6c32019-06-06 14:29:25 -0700177func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700178 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700179 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700180 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700181 bp: ctx,
182 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700183 }
Colin Cross798bfce2016-10-12 14:28:16 -0700184 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700185 }
Colin Cross798bfce2016-10-12 14:28:16 -0700186 }
187 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700188 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700189 return mutator
190}
191
192type MutatorHandle interface {
193 Parallel() MutatorHandle
194}
195
196func (mutator *mutator) Parallel() MutatorHandle {
197 mutator.parallel = true
198 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700199}
Colin Cross1e676be2016-10-12 14:38:15 -0700200
201func depsMutator(ctx BottomUpMutatorContext) {
Colin Cross6db4a6a2018-08-30 12:52:41 -0700202 if m, ok := ctx.Module().(Module); ok && m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700203 m.DepsMutator(ctx)
204 }
205}
Colin Crossd11fcda2017-10-23 17:59:01 -0700206
Colin Cross25de6c32019-06-06 14:29:25 -0700207func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700208 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700209 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700210 p, nil)
211 if err != nil {
212 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700213 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700214 } else {
215 panic(err)
216 }
217 }
218 }
219}
220
Colin Cross25de6c32019-06-06 14:29:25 -0700221func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700222 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700223 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700224 p, nil)
225 if err != nil {
226 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700227 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700228 } else {
229 panic(err)
230 }
231 }
232 }
233}
Colin Crossdc35e212019-06-06 16:13:11 -0700234
235// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
236// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
237// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
238// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
239// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
240
Colin Crosscb55e082019-07-01 15:32:31 -0700241func (t *topDownMutatorContext) MutatorName() string {
242 return t.bp.MutatorName()
243}
244
Colin Crossdc35e212019-06-06 16:13:11 -0700245func (t *topDownMutatorContext) Rename(name string) {
246 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700247 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700248}
249
Colin Crosse003c4a2019-09-25 12:58:36 -0700250func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Colin Cross18c46802019-09-24 22:19:02 -0700251 inherited := []interface{}{&t.Module().base().commonProperties}
Colin Crosse003c4a2019-09-25 12:58:36 -0700252 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
Colin Cross18c46802019-09-24 22:19:02 -0700253
254 if t.Module().base().variableProperties != nil && module.base().variableProperties != nil {
255 src := t.Module().base().variableProperties
256 dst := []interface{}{
257 module.base().variableProperties,
258 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
259 // don't cause a "failed to find property to extend" error.
260 proptools.CloneEmptyProperties(reflect.ValueOf(src).Elem()).Interface(),
261 }
262 err := proptools.AppendMatchingProperties(dst, src, nil)
263 if err != nil {
264 panic(err)
265 }
266 }
267
Colin Crosse003c4a2019-09-25 12:58:36 -0700268 return module
Colin Crossdc35e212019-06-06 16:13:11 -0700269}
270
Colin Crosscb55e082019-07-01 15:32:31 -0700271func (b *bottomUpMutatorContext) MutatorName() string {
272 return b.bp.MutatorName()
273}
274
Colin Crossdc35e212019-06-06 16:13:11 -0700275func (b *bottomUpMutatorContext) Rename(name string) {
276 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700277 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700278}
279
280func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) {
281 b.bp.AddDependency(module, tag, name...)
282}
283
284func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
285 b.bp.AddReverseDependency(module, tag, name)
286}
287
288func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []blueprint.Module {
Colin Cross9a362232019-07-01 15:32:45 -0700289 modules := b.bp.CreateVariations(variations...)
290
291 for i := range variations {
292 base := modules[i].(Module).base()
293 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
294 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
295 }
296
297 return modules
Colin Crossdc35e212019-06-06 16:13:11 -0700298}
299
300func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []blueprint.Module {
Colin Cross9a362232019-07-01 15:32:45 -0700301 modules := b.bp.CreateLocalVariations(variations...)
302
303 for i := range variations {
304 base := modules[i].(Module).base()
305 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
306 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
307 }
308
309 return modules
Colin Crossdc35e212019-06-06 16:13:11 -0700310}
311
312func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
313 b.bp.SetDependencyVariation(variation)
314}
315
Jiyong Park1d1119f2019-07-29 21:27:18 +0900316func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
317 b.bp.SetDefaultDependencyVariation(variation)
318}
319
Colin Crossdc35e212019-06-06 16:13:11 -0700320func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
321 names ...string) {
322
323 b.bp.AddVariationDependencies(variations, tag, names...)
324}
325
326func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
327 tag blueprint.DependencyTag, names ...string) {
328
329 b.bp.AddFarVariationDependencies(variations, tag, names...)
330}
331
332func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
333 b.bp.AddInterVariantDependency(tag, from, to)
334}
335
336func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
337 b.bp.ReplaceDependencies(name)
338}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800339
340func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
341 b.bp.AliasVariation(variationName)
342}