blob: 8e4343d0fa894c1ac69ad0fac4a865527879c208 [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 {
Colin Cross25de6c32019-06-06 14:29:25 -070069 TopDown(name string, m TopDownMutator) MutatorHandle
70 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -070071}
72
73type RegisterMutatorFunc func(RegisterMutatorsContext)
74
Colin Crosscec81712017-07-13 14:43:27 -070075var preArch = []RegisterMutatorFunc{
Colin Crossf8b860a2019-04-16 14:43:28 -070076 registerLoadHookMutator,
Dan Willemsen6e72ef72018-01-26 18:27:02 -080077 RegisterNamespaceMutator,
Paul Duffine2453c72019-05-31 14:00:04 +010078 // Rename package module types.
79 registerPackageRenamer,
Colin Cross5ea9bcc2017-07-27 15:41:32 -070080 RegisterPrebuiltsPreArchMutators,
Martin Stjernholm226b20d2019-05-17 22:42:02 +010081 registerVisibilityRuleChecker,
Colin Cross89536d42017-07-07 14:35:50 -070082 RegisterDefaultsPreArchMutators,
Paul Duffin2e61fa62019-03-28 14:10:57 +000083 registerVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -070084}
85
Colin Crossae4c6182017-09-15 17:33:55 -070086func registerArchMutator(ctx RegisterMutatorsContext) {
87 ctx.BottomUp("arch", archMutator).Parallel()
88 ctx.TopDown("arch_hooks", archHookMutator).Parallel()
89}
90
Colin Crosscec81712017-07-13 14:43:27 -070091var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -070092 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -070093}
94
95var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -080096 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -070097 RegisterPrebuiltsPostDepsMutators,
Paul Duffin2e61fa62019-03-28 14:10:57 +000098 registerVisibilityRuleEnforcer,
Steven Moreland65b3fd92017-12-06 14:18:35 -080099 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700100 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700101}
Colin Cross1e676be2016-10-12 14:38:15 -0700102
103func PreArchMutators(f RegisterMutatorFunc) {
104 preArch = append(preArch, f)
105}
106
107func PreDepsMutators(f RegisterMutatorFunc) {
108 preDeps = append(preDeps, f)
109}
110
111func PostDepsMutators(f RegisterMutatorFunc) {
112 postDeps = append(postDeps, f)
113}
114
Colin Cross25de6c32019-06-06 14:29:25 -0700115type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700116
Colin Cross635c3b02016-05-18 15:37:25 -0700117type TopDownMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800118 BaseModuleContext
Colin Cross3f68a132017-10-23 17:10:29 -0700119
Colin Crosscb55e082019-07-01 15:32:31 -0700120 MutatorName() string
121
Colin Cross3f68a132017-10-23 17:10:29 -0700122 Rename(name string)
Colin Cross3f68a132017-10-23 17:10:29 -0700123
124 CreateModule(blueprint.ModuleFactory, ...interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700125}
126
Colin Cross25de6c32019-06-06 14:29:25 -0700127type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700128 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700129 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700130}
131
Colin Cross25de6c32019-06-06 14:29:25 -0700132type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700133
Colin Cross635c3b02016-05-18 15:37:25 -0700134type BottomUpMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800135 BaseModuleContext
Colin Crossaabf6792017-11-29 00:27:14 -0800136
Colin Crosscb55e082019-07-01 15:32:31 -0700137 MutatorName() string
138
Colin Crossaabf6792017-11-29 00:27:14 -0800139 Rename(name string)
Colin Crossaabf6792017-11-29 00:27:14 -0800140
141 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string)
142 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
143 CreateVariations(...string) []blueprint.Module
144 CreateLocalVariations(...string) []blueprint.Module
145 SetDependencyVariation(string)
Jiyong Park1d1119f2019-07-29 21:27:18 +0900146 SetDefaultDependencyVariation(*string)
Colin Crossaabf6792017-11-29 00:27:14 -0800147 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
148 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
149 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
150 ReplaceDependencies(string)
Colin Cross6362e272015-10-29 15:25:03 -0700151}
152
Colin Cross25de6c32019-06-06 14:29:25 -0700153type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700154 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700155 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700156}
157
Colin Cross25de6c32019-06-06 14:29:25 -0700158func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700159 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700160 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700161 actx := &bottomUpMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700162 bp: ctx,
163 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700164 }
Colin Cross798bfce2016-10-12 14:28:16 -0700165 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700166 }
Colin Cross798bfce2016-10-12 14:28:16 -0700167 }
168 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700169 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700170 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700171}
172
Colin Cross25de6c32019-06-06 14:29:25 -0700173func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700174 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700175 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700176 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700177 bp: ctx,
178 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700179 }
Colin Cross798bfce2016-10-12 14:28:16 -0700180 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700181 }
Colin Cross798bfce2016-10-12 14:28:16 -0700182 }
183 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700184 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700185 return mutator
186}
187
188type MutatorHandle interface {
189 Parallel() MutatorHandle
190}
191
192func (mutator *mutator) Parallel() MutatorHandle {
193 mutator.parallel = true
194 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700195}
Colin Cross1e676be2016-10-12 14:38:15 -0700196
197func depsMutator(ctx BottomUpMutatorContext) {
Colin Cross6db4a6a2018-08-30 12:52:41 -0700198 if m, ok := ctx.Module().(Module); ok && m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700199 m.DepsMutator(ctx)
200 }
201}
Colin Crossd11fcda2017-10-23 17:59:01 -0700202
Colin Cross25de6c32019-06-06 14:29:25 -0700203func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700204 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700205 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700206 p, nil)
207 if err != nil {
208 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700209 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700210 } else {
211 panic(err)
212 }
213 }
214 }
215}
216
Colin Cross25de6c32019-06-06 14:29:25 -0700217func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700218 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700219 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700220 p, nil)
221 if err != nil {
222 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700223 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700224 } else {
225 panic(err)
226 }
227 }
228 }
229}
Colin Crossdc35e212019-06-06 16:13:11 -0700230
231// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
232// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
233// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
234// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
235// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
236
Colin Crosscb55e082019-07-01 15:32:31 -0700237func (t *topDownMutatorContext) MutatorName() string {
238 return t.bp.MutatorName()
239}
240
Colin Crossdc35e212019-06-06 16:13:11 -0700241func (t *topDownMutatorContext) Rename(name string) {
242 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700243 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700244}
245
246func (t *topDownMutatorContext) CreateModule(factory blueprint.ModuleFactory, props ...interface{}) {
Steven Moreland12f23e02019-07-29 16:44:57 -0700247 common := []interface{}{&t.Module().base().commonProperties}
248 t.bp.CreateModule(factory, append(common, props...)...)
Colin Crossdc35e212019-06-06 16:13:11 -0700249}
250
Colin Crosscb55e082019-07-01 15:32:31 -0700251func (b *bottomUpMutatorContext) MutatorName() string {
252 return b.bp.MutatorName()
253}
254
Colin Crossdc35e212019-06-06 16:13:11 -0700255func (b *bottomUpMutatorContext) Rename(name string) {
256 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700257 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700258}
259
260func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) {
261 b.bp.AddDependency(module, tag, name...)
262}
263
264func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
265 b.bp.AddReverseDependency(module, tag, name)
266}
267
268func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []blueprint.Module {
Colin Cross9a362232019-07-01 15:32:45 -0700269 modules := b.bp.CreateVariations(variations...)
270
271 for i := range variations {
272 base := modules[i].(Module).base()
273 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
274 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
275 }
276
277 return modules
Colin Crossdc35e212019-06-06 16:13:11 -0700278}
279
280func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []blueprint.Module {
Colin Cross9a362232019-07-01 15:32:45 -0700281 modules := b.bp.CreateLocalVariations(variations...)
282
283 for i := range variations {
284 base := modules[i].(Module).base()
285 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
286 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
287 }
288
289 return modules
Colin Crossdc35e212019-06-06 16:13:11 -0700290}
291
292func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
293 b.bp.SetDependencyVariation(variation)
294}
295
Jiyong Park1d1119f2019-07-29 21:27:18 +0900296func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
297 b.bp.SetDefaultDependencyVariation(variation)
298}
299
Colin Crossdc35e212019-06-06 16:13:11 -0700300func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
301 names ...string) {
302
303 b.bp.AddVariationDependencies(variations, tag, names...)
304}
305
306func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
307 tag blueprint.DependencyTag, names ...string) {
308
309 b.bp.AddFarVariationDependencies(variations, tag, names...)
310}
311
312func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
313 b.bp.AddInterVariantDependency(tag, from, to)
314}
315
316func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
317 b.bp.ReplaceDependencies(name)
318}