blob: 081c2b248e06d45df7ddaea6b459994b7c61c1ff [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,
Colin Cross5ea9bcc2017-07-27 15:41:32 -070078 RegisterPrebuiltsPreArchMutators,
Martin Stjernholm226b20d2019-05-17 22:42:02 +010079 registerVisibilityRuleChecker,
Colin Cross89536d42017-07-07 14:35:50 -070080 RegisterDefaultsPreArchMutators,
Paul Duffin2e61fa62019-03-28 14:10:57 +000081 registerVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -070082}
83
Colin Crossae4c6182017-09-15 17:33:55 -070084func registerArchMutator(ctx RegisterMutatorsContext) {
85 ctx.BottomUp("arch", archMutator).Parallel()
86 ctx.TopDown("arch_hooks", archHookMutator).Parallel()
87}
88
Colin Crosscec81712017-07-13 14:43:27 -070089var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -070090 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -070091}
92
93var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -080094 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -070095 RegisterPrebuiltsPostDepsMutators,
Paul Duffin2e61fa62019-03-28 14:10:57 +000096 registerVisibilityRuleEnforcer,
Steven Moreland65b3fd92017-12-06 14:18:35 -080097 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070098 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -070099}
Colin Cross1e676be2016-10-12 14:38:15 -0700100
101func PreArchMutators(f RegisterMutatorFunc) {
102 preArch = append(preArch, f)
103}
104
105func PreDepsMutators(f RegisterMutatorFunc) {
106 preDeps = append(preDeps, f)
107}
108
109func PostDepsMutators(f RegisterMutatorFunc) {
110 postDeps = append(postDeps, f)
111}
112
Colin Cross25de6c32019-06-06 14:29:25 -0700113type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700114
Colin Cross635c3b02016-05-18 15:37:25 -0700115type TopDownMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800116 BaseModuleContext
Colin Cross3f68a132017-10-23 17:10:29 -0700117
Colin Cross3f68a132017-10-23 17:10:29 -0700118 Rename(name string)
Colin Cross3f68a132017-10-23 17:10:29 -0700119
120 CreateModule(blueprint.ModuleFactory, ...interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700121}
122
Colin Cross25de6c32019-06-06 14:29:25 -0700123type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700124 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700125 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700126}
127
Colin Cross25de6c32019-06-06 14:29:25 -0700128type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700129
Colin Cross635c3b02016-05-18 15:37:25 -0700130type BottomUpMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800131 BaseModuleContext
Colin Crossaabf6792017-11-29 00:27:14 -0800132
Colin Crossaabf6792017-11-29 00:27:14 -0800133 Rename(name string)
Colin Crossaabf6792017-11-29 00:27:14 -0800134
135 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string)
136 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
137 CreateVariations(...string) []blueprint.Module
138 CreateLocalVariations(...string) []blueprint.Module
139 SetDependencyVariation(string)
140 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
141 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
142 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
143 ReplaceDependencies(string)
Colin Cross6362e272015-10-29 15:25:03 -0700144}
145
Colin Cross25de6c32019-06-06 14:29:25 -0700146type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700147 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700148 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700149}
150
Colin Cross25de6c32019-06-06 14:29:25 -0700151func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700152 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700153 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700154 actx := &bottomUpMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700155 bp: ctx,
156 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700157 }
Colin Cross798bfce2016-10-12 14:28:16 -0700158 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700159 }
Colin Cross798bfce2016-10-12 14:28:16 -0700160 }
161 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700162 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700163 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700164}
165
Colin Cross25de6c32019-06-06 14:29:25 -0700166func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700167 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700168 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700169 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700170 bp: ctx,
171 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700172 }
Colin Cross798bfce2016-10-12 14:28:16 -0700173 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700174 }
Colin Cross798bfce2016-10-12 14:28:16 -0700175 }
176 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700177 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700178 return mutator
179}
180
181type MutatorHandle interface {
182 Parallel() MutatorHandle
183}
184
185func (mutator *mutator) Parallel() MutatorHandle {
186 mutator.parallel = true
187 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700188}
Colin Cross1e676be2016-10-12 14:38:15 -0700189
190func depsMutator(ctx BottomUpMutatorContext) {
Colin Cross6db4a6a2018-08-30 12:52:41 -0700191 if m, ok := ctx.Module().(Module); ok && m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700192 m.DepsMutator(ctx)
193 }
194}
Colin Crossd11fcda2017-10-23 17:59:01 -0700195
Colin Cross25de6c32019-06-06 14:29:25 -0700196func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700197 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700198 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700199 p, nil)
200 if err != nil {
201 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700202 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700203 } else {
204 panic(err)
205 }
206 }
207 }
208}
209
Colin Cross25de6c32019-06-06 14:29:25 -0700210func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700211 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700212 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700213 p, nil)
214 if err != nil {
215 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700216 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700217 } else {
218 panic(err)
219 }
220 }
221 }
222}
Colin Crossdc35e212019-06-06 16:13:11 -0700223
224// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
225// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
226// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
227// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
228// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
229
230func (t *topDownMutatorContext) Rename(name string) {
231 t.bp.Rename(name)
232}
233
234func (t *topDownMutatorContext) CreateModule(factory blueprint.ModuleFactory, props ...interface{}) {
235 t.bp.CreateModule(factory, props...)
236}
237
238func (b *bottomUpMutatorContext) Rename(name string) {
239 b.bp.Rename(name)
240}
241
242func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) {
243 b.bp.AddDependency(module, tag, name...)
244}
245
246func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
247 b.bp.AddReverseDependency(module, tag, name)
248}
249
250func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []blueprint.Module {
251 return b.bp.CreateVariations(variations...)
252}
253
254func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []blueprint.Module {
255 return b.bp.CreateLocalVariations(variations...)
256}
257
258func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
259 b.bp.SetDependencyVariation(variation)
260}
261
262func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
263 names ...string) {
264
265 b.bp.AddVariationDependencies(variations, tag, names...)
266}
267
268func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
269 tag blueprint.DependencyTag, names ...string) {
270
271 b.bp.AddFarVariationDependencies(variations, tag, names...)
272}
273
274func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
275 b.bp.AddInterVariantDependency(tag, from, to)
276}
277
278func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
279 b.bp.ReplaceDependencies(name)
280}