blob: d288194cdf5710cc09a82ba29a7ba5ffe34670b3 [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)
146 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
147 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
148 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
149 ReplaceDependencies(string)
Colin Cross6362e272015-10-29 15:25:03 -0700150}
151
Colin Cross25de6c32019-06-06 14:29:25 -0700152type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700153 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700154 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700155}
156
Colin Cross25de6c32019-06-06 14:29:25 -0700157func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700158 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700159 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700160 actx := &bottomUpMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700161 bp: ctx,
162 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700163 }
Colin Cross798bfce2016-10-12 14:28:16 -0700164 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700165 }
Colin Cross798bfce2016-10-12 14:28:16 -0700166 }
167 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700168 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700169 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700170}
171
Colin Cross25de6c32019-06-06 14:29:25 -0700172func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700173 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700174 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700175 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700176 bp: ctx,
177 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700178 }
Colin Cross798bfce2016-10-12 14:28:16 -0700179 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700180 }
Colin Cross798bfce2016-10-12 14:28:16 -0700181 }
182 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700183 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700184 return mutator
185}
186
187type MutatorHandle interface {
188 Parallel() MutatorHandle
189}
190
191func (mutator *mutator) Parallel() MutatorHandle {
192 mutator.parallel = true
193 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700194}
Colin Cross1e676be2016-10-12 14:38:15 -0700195
196func depsMutator(ctx BottomUpMutatorContext) {
Colin Cross6db4a6a2018-08-30 12:52:41 -0700197 if m, ok := ctx.Module().(Module); ok && m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700198 m.DepsMutator(ctx)
199 }
200}
Colin Crossd11fcda2017-10-23 17:59:01 -0700201
Colin Cross25de6c32019-06-06 14:29:25 -0700202func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700203 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700204 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700205 p, nil)
206 if err != nil {
207 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700208 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700209 } else {
210 panic(err)
211 }
212 }
213 }
214}
215
Colin Cross25de6c32019-06-06 14:29:25 -0700216func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700217 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700218 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700219 p, nil)
220 if err != nil {
221 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700222 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700223 } else {
224 panic(err)
225 }
226 }
227 }
228}
Colin Crossdc35e212019-06-06 16:13:11 -0700229
230// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
231// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
232// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
233// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
234// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
235
Colin Crosscb55e082019-07-01 15:32:31 -0700236func (t *topDownMutatorContext) MutatorName() string {
237 return t.bp.MutatorName()
238}
239
Colin Crossdc35e212019-06-06 16:13:11 -0700240func (t *topDownMutatorContext) Rename(name string) {
241 t.bp.Rename(name)
242}
243
244func (t *topDownMutatorContext) CreateModule(factory blueprint.ModuleFactory, props ...interface{}) {
245 t.bp.CreateModule(factory, props...)
246}
247
Colin Crosscb55e082019-07-01 15:32:31 -0700248func (b *bottomUpMutatorContext) MutatorName() string {
249 return b.bp.MutatorName()
250}
251
Colin Crossdc35e212019-06-06 16:13:11 -0700252func (b *bottomUpMutatorContext) Rename(name string) {
253 b.bp.Rename(name)
254}
255
256func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) {
257 b.bp.AddDependency(module, tag, name...)
258}
259
260func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
261 b.bp.AddReverseDependency(module, tag, name)
262}
263
264func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []blueprint.Module {
265 return b.bp.CreateVariations(variations...)
266}
267
268func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []blueprint.Module {
269 return b.bp.CreateLocalVariations(variations...)
270}
271
272func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
273 b.bp.SetDependencyVariation(variation)
274}
275
276func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
277 names ...string) {
278
279 b.bp.AddVariationDependencies(variations, tag, names...)
280}
281
282func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
283 tag blueprint.DependencyTag, names ...string) {
284
285 b.bp.AddFarVariationDependencies(variations, tag, names...)
286}
287
288func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
289 b.bp.AddInterVariantDependency(tag, from, to)
290}
291
292func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
293 b.bp.ReplaceDependencies(name)
294}