blob: 3d1bb4f7d3f0b50b72c61ae7a7daaf268821df19 [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
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000030// run FinalDeps mutators (CreateVariations disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070031// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070032
Colin Cross795c3772017-03-16 16:50:10 -070033func registerMutatorsToContext(ctx *blueprint.Context, mutators []*mutator) {
34 for _, t := range mutators {
35 var handle blueprint.MutatorHandle
36 if t.bottomUpMutator != nil {
37 handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator)
38 } else if t.topDownMutator != nil {
39 handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator)
40 }
41 if t.parallel {
42 handle.Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070043 }
44 }
Colin Cross1e676be2016-10-12 14:38:15 -070045}
46
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000047func registerMutators(ctx *blueprint.Context, preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) {
Colin Crosscec81712017-07-13 14:43:27 -070048 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080049
50 register := func(funcs []RegisterMutatorFunc) {
51 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070052 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080053 }
54 }
55
Colin Crosscec81712017-07-13 14:43:27 -070056 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080057
Colin Crosscec81712017-07-13 14:43:27 -070058 register(preDeps)
59
60 mctx.BottomUp("deps", depsMutator).Parallel()
61
62 register(postDeps)
63
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000064 mctx.finalPhase = true
65 register(finalDeps)
66
Colin Crosscec81712017-07-13 14:43:27 -070067 registerMutatorsToContext(ctx, mctx.mutators)
Colin Cross795c3772017-03-16 16:50:10 -070068}
69
70type registerMutatorsContext struct {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000071 mutators []*mutator
72 finalPhase bool
Colin Cross795c3772017-03-16 16:50:10 -070073}
Colin Cross1e676be2016-10-12 14:38:15 -070074
75type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -070076 TopDown(name string, m TopDownMutator) MutatorHandle
77 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -070078}
79
80type RegisterMutatorFunc func(RegisterMutatorsContext)
81
Colin Crosscec81712017-07-13 14:43:27 -070082var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -080083 RegisterNamespaceMutator,
Paul Duffine2453c72019-05-31 14:00:04 +010084 // Rename package module types.
Paul Duffin593b3c92019-12-05 14:31:48 +000085 RegisterPackageRenamer,
Colin Cross5ea9bcc2017-07-27 15:41:32 -070086 RegisterPrebuiltsPreArchMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +000087 RegisterVisibilityRuleChecker,
Colin Cross89536d42017-07-07 14:35:50 -070088 RegisterDefaultsPreArchMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +000089 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -070090}
91
Colin Crossae4c6182017-09-15 17:33:55 -070092func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Crossa195f912019-10-16 11:07:20 -070093 ctx.BottomUp("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -080094 ctx.BottomUp("image", imageMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -070095 ctx.BottomUp("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -070096}
97
Colin Crosscec81712017-07-13 14:43:27 -070098var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -070099 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700100}
101
102var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800103 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700104 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000105 RegisterVisibilityRuleEnforcer,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800106 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700107 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700108}
Colin Cross1e676be2016-10-12 14:38:15 -0700109
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000110var finalDeps = []RegisterMutatorFunc{}
111
Colin Cross1e676be2016-10-12 14:38:15 -0700112func PreArchMutators(f RegisterMutatorFunc) {
113 preArch = append(preArch, f)
114}
115
116func PreDepsMutators(f RegisterMutatorFunc) {
117 preDeps = append(preDeps, f)
118}
119
120func PostDepsMutators(f RegisterMutatorFunc) {
121 postDeps = append(postDeps, f)
122}
123
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000124func FinalDepsMutators(f RegisterMutatorFunc) {
125 finalDeps = append(finalDeps, f)
126}
127
Colin Cross25de6c32019-06-06 14:29:25 -0700128type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700129
Colin Cross635c3b02016-05-18 15:37:25 -0700130type TopDownMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800131 BaseModuleContext
Colin Cross3f68a132017-10-23 17:10:29 -0700132
Colin Crosscb55e082019-07-01 15:32:31 -0700133 MutatorName() string
134
Colin Cross3f68a132017-10-23 17:10:29 -0700135 Rename(name string)
Colin Cross3f68a132017-10-23 17:10:29 -0700136
Colin Crosse003c4a2019-09-25 12:58:36 -0700137 CreateModule(ModuleFactory, ...interface{}) Module
Colin Cross6362e272015-10-29 15:25:03 -0700138}
139
Colin Cross25de6c32019-06-06 14:29:25 -0700140type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700141 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700142 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700143}
144
Colin Cross25de6c32019-06-06 14:29:25 -0700145type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700146
Colin Cross635c3b02016-05-18 15:37:25 -0700147type BottomUpMutatorContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800148 BaseModuleContext
Colin Crossaabf6792017-11-29 00:27:14 -0800149
Colin Crosscb55e082019-07-01 15:32:31 -0700150 MutatorName() string
151
Colin Crossaabf6792017-11-29 00:27:14 -0800152 Rename(name string)
Colin Crossaabf6792017-11-29 00:27:14 -0800153
154 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string)
155 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross43b92e02019-11-18 15:28:57 -0800156 CreateVariations(...string) []Module
157 CreateLocalVariations(...string) []Module
Colin Crossaabf6792017-11-29 00:27:14 -0800158 SetDependencyVariation(string)
Jiyong Park1d1119f2019-07-29 21:27:18 +0900159 SetDefaultDependencyVariation(*string)
Colin Crossaabf6792017-11-29 00:27:14 -0800160 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
161 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string)
162 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
163 ReplaceDependencies(string)
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800164 AliasVariation(variationName string)
Colin Cross6362e272015-10-29 15:25:03 -0700165}
166
Colin Cross25de6c32019-06-06 14:29:25 -0700167type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700168 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700169 baseModuleContext
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000170 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700171}
172
Colin Cross25de6c32019-06-06 14:29:25 -0700173func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000174 finalPhase := x.finalPhase
Colin Cross798bfce2016-10-12 14:28:16 -0700175 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700176 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700177 actx := &bottomUpMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700178 bp: ctx,
179 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000180 finalPhase: finalPhase,
Colin Cross6362e272015-10-29 15:25:03 -0700181 }
Colin Cross798bfce2016-10-12 14:28:16 -0700182 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700183 }
Colin Cross798bfce2016-10-12 14:28:16 -0700184 }
185 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700186 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700187 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700188}
189
Colin Cross25de6c32019-06-06 14:29:25 -0700190func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700191 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700192 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700193 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700194 bp: ctx,
195 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700196 }
Colin Cross798bfce2016-10-12 14:28:16 -0700197 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700198 }
Colin Cross798bfce2016-10-12 14:28:16 -0700199 }
200 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700201 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700202 return mutator
203}
204
205type MutatorHandle interface {
206 Parallel() MutatorHandle
207}
208
209func (mutator *mutator) Parallel() MutatorHandle {
210 mutator.parallel = true
211 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700212}
Colin Cross1e676be2016-10-12 14:38:15 -0700213
214func depsMutator(ctx BottomUpMutatorContext) {
Colin Cross6db4a6a2018-08-30 12:52:41 -0700215 if m, ok := ctx.Module().(Module); ok && m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700216 m.DepsMutator(ctx)
217 }
218}
Colin Crossd11fcda2017-10-23 17:59:01 -0700219
Colin Cross25de6c32019-06-06 14:29:25 -0700220func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700221 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700222 err := proptools.AppendMatchingProperties(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}
233
Colin Cross25de6c32019-06-06 14:29:25 -0700234func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700235 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700236 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700237 p, nil)
238 if err != nil {
239 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700240 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700241 } else {
242 panic(err)
243 }
244 }
245 }
246}
Colin Crossdc35e212019-06-06 16:13:11 -0700247
248// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
249// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
250// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
251// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
252// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
253
Colin Crosscb55e082019-07-01 15:32:31 -0700254func (t *topDownMutatorContext) MutatorName() string {
255 return t.bp.MutatorName()
256}
257
Colin Crossdc35e212019-06-06 16:13:11 -0700258func (t *topDownMutatorContext) Rename(name string) {
259 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700260 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700261}
262
Colin Crosse003c4a2019-09-25 12:58:36 -0700263func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Colin Cross18c46802019-09-24 22:19:02 -0700264 inherited := []interface{}{&t.Module().base().commonProperties}
Colin Crosse003c4a2019-09-25 12:58:36 -0700265 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
Colin Cross18c46802019-09-24 22:19:02 -0700266
267 if t.Module().base().variableProperties != nil && module.base().variableProperties != nil {
268 src := t.Module().base().variableProperties
269 dst := []interface{}{
270 module.base().variableProperties,
271 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
272 // don't cause a "failed to find property to extend" error.
273 proptools.CloneEmptyProperties(reflect.ValueOf(src).Elem()).Interface(),
274 }
275 err := proptools.AppendMatchingProperties(dst, src, nil)
276 if err != nil {
277 panic(err)
278 }
279 }
280
Colin Crosse003c4a2019-09-25 12:58:36 -0700281 return module
Colin Crossdc35e212019-06-06 16:13:11 -0700282}
283
Colin Crosscb55e082019-07-01 15:32:31 -0700284func (b *bottomUpMutatorContext) MutatorName() string {
285 return b.bp.MutatorName()
286}
287
Colin Crossdc35e212019-06-06 16:13:11 -0700288func (b *bottomUpMutatorContext) Rename(name string) {
289 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700290 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700291}
292
293func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) {
294 b.bp.AddDependency(module, tag, name...)
295}
296
297func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
298 b.bp.AddReverseDependency(module, tag, name)
299}
300
Colin Cross43b92e02019-11-18 15:28:57 -0800301func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000302 if b.finalPhase {
303 panic("CreateVariations not allowed in FinalDepsMutators")
304 }
305
Colin Cross9a362232019-07-01 15:32:45 -0700306 modules := b.bp.CreateVariations(variations...)
307
Colin Cross43b92e02019-11-18 15:28:57 -0800308 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700309 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800310 aModules[i] = modules[i].(Module)
311 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700312 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
313 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
314 }
315
Colin Cross43b92e02019-11-18 15:28:57 -0800316 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700317}
318
Colin Cross43b92e02019-11-18 15:28:57 -0800319func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000320 if b.finalPhase {
321 panic("CreateLocalVariations not allowed in FinalDepsMutators")
322 }
323
Colin Cross9a362232019-07-01 15:32:45 -0700324 modules := b.bp.CreateLocalVariations(variations...)
325
Colin Cross43b92e02019-11-18 15:28:57 -0800326 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700327 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800328 aModules[i] = modules[i].(Module)
329 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700330 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
331 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
332 }
333
Colin Cross43b92e02019-11-18 15:28:57 -0800334 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700335}
336
337func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
338 b.bp.SetDependencyVariation(variation)
339}
340
Jiyong Park1d1119f2019-07-29 21:27:18 +0900341func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
342 b.bp.SetDefaultDependencyVariation(variation)
343}
344
Colin Crossdc35e212019-06-06 16:13:11 -0700345func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
346 names ...string) {
347
348 b.bp.AddVariationDependencies(variations, tag, names...)
349}
350
351func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
352 tag blueprint.DependencyTag, names ...string) {
353
354 b.bp.AddFarVariationDependencies(variations, tag, names...)
355}
356
357func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
358 b.bp.AddInterVariantDependency(tag, from, to)
359}
360
361func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
362 b.bp.ReplaceDependencies(name)
363}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800364
365func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
366 b.bp.AliasVariation(variationName)
367}