blob: 72c68b2a444f9a2de990b32c8f3c5ac5818bfc89 [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
Jingwen Chen4133ce62020-12-02 04:34:15 -050047func registerMutatorsForBazelConversion(ctx *blueprint.Context) {
48 // FIXME(b/171263886): Start bringing in mutators to make the Bionic
49 // module subgraph suitable for automated conversion.
50}
51
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000052func registerMutators(ctx *blueprint.Context, preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) {
Colin Crosscec81712017-07-13 14:43:27 -070053 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080054
55 register := func(funcs []RegisterMutatorFunc) {
56 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070057 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080058 }
59 }
60
Colin Crosscec81712017-07-13 14:43:27 -070061 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080062
Colin Crosscec81712017-07-13 14:43:27 -070063 register(preDeps)
64
65 mctx.BottomUp("deps", depsMutator).Parallel()
66
67 register(postDeps)
68
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000069 mctx.finalPhase = true
70 register(finalDeps)
71
Colin Crosscec81712017-07-13 14:43:27 -070072 registerMutatorsToContext(ctx, mctx.mutators)
Colin Cross795c3772017-03-16 16:50:10 -070073}
74
75type registerMutatorsContext struct {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000076 mutators []*mutator
77 finalPhase bool
Colin Cross795c3772017-03-16 16:50:10 -070078}
Colin Cross1e676be2016-10-12 14:38:15 -070079
80type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -070081 TopDown(name string, m TopDownMutator) MutatorHandle
82 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -070083 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -070084}
85
86type RegisterMutatorFunc func(RegisterMutatorsContext)
87
Colin Crosscec81712017-07-13 14:43:27 -070088var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -080089 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +010090
Paul Duffinaa4162e2020-05-05 11:35:43 +010091 // Check the visibility rules are valid.
92 //
93 // This must run after the package renamer mutators so that any issues found during
94 // validation of the package's default_visibility property are reported using the
95 // correct package name and not the synthetic name.
96 //
97 // This must also be run before defaults mutators as the rules for validation are
98 // different before checking the rules than they are afterwards. e.g.
99 // visibility: ["//visibility:private", "//visibility:public"]
100 // would be invalid if specified in a module definition but is valid if it results
101 // from something like this:
102 //
103 // defaults {
104 // name: "defaults",
105 // // Be inaccessible outside a package by default.
106 // visibility: ["//visibility:private"]
107 // }
108 //
109 // defaultable_module {
110 // name: "defaultable_module",
111 // defaults: ["defaults"],
112 // // Override the default.
113 // visibility: ["//visibility:public"]
114 // }
115 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000116 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100117
Bob Badour37af0462021-01-07 03:34:31 +0000118 // Record the default_applicable_licenses for each package.
119 //
120 // This must run before the defaults so that defaults modules can pick up the package default.
121 RegisterLicensesPackageMapper,
122
Paul Duffinaa4162e2020-05-05 11:35:43 +0100123 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100124 //
125 // Any mutators that are added before this will not see any modules created by
126 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700127 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100128
Paul Duffin44f1d842020-06-26 20:17:02 +0100129 // Add dependencies on any components so that any component references can be
130 // resolved within the deps mutator.
131 //
132 // Must be run after defaults so it can be used to create dependencies on the
133 // component modules that are creating in a DefaultableHook.
134 //
135 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
136 // renamed. That is so that if a module creates components using a prebuilt module
137 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
138 // the prebuilt module and not the source module.
139 RegisterComponentsMutator,
140
Paul Duffinc988c8e2020-04-29 18:27:14 +0100141 // Create an association between prebuilt modules and their corresponding source
142 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100143 //
144 // Must be run after defaults mutators to ensure that any modules created by
145 // a DefaultableHook can be either a prebuilt or a source module with a matching
146 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100147 RegisterPrebuiltsPreArchMutators,
148
Bob Badour37af0462021-01-07 03:34:31 +0000149 // Gather the licenses properties for all modules for use during expansion and enforcement.
150 //
151 // This must come after the defaults mutators to ensure that any licenses supplied
152 // in a defaults module has been successfully applied before the rules are gathered.
153 RegisterLicensesPropertyGatherer,
154
Paul Duffinaa4162e2020-05-05 11:35:43 +0100155 // Gather the visibility rules for all modules for us during visibility enforcement.
156 //
157 // This must come after the defaults mutators to ensure that any visibility supplied
158 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000159 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700160}
161
Colin Crossae4c6182017-09-15 17:33:55 -0700162func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700163 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800164 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700165 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700166}
167
Colin Crosscec81712017-07-13 14:43:27 -0700168var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700169 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700170}
171
172var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800173 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700174 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000175 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000176 RegisterLicensesDependencyChecker,
Artur Satayevc5570ac2020-04-09 16:06:36 +0100177 RegisterNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700178 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700179}
Colin Cross1e676be2016-10-12 14:38:15 -0700180
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000181var finalDeps = []RegisterMutatorFunc{}
182
Colin Cross1e676be2016-10-12 14:38:15 -0700183func PreArchMutators(f RegisterMutatorFunc) {
184 preArch = append(preArch, f)
185}
186
187func PreDepsMutators(f RegisterMutatorFunc) {
188 preDeps = append(preDeps, f)
189}
190
191func PostDepsMutators(f RegisterMutatorFunc) {
192 postDeps = append(postDeps, f)
193}
194
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000195func FinalDepsMutators(f RegisterMutatorFunc) {
196 finalDeps = append(finalDeps, f)
197}
198
Colin Cross9f35c3d2020-09-16 19:04:41 -0700199type BaseMutatorContext interface {
200 BaseModuleContext
201
202 // MutatorName returns the name that this mutator was registered with.
203 MutatorName() string
204
205 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
206 // AddDependency or OtherModuleName until after this mutator pass is complete.
207 Rename(name string)
208}
209
Colin Cross25de6c32019-06-06 14:29:25 -0700210type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700211
Colin Cross635c3b02016-05-18 15:37:25 -0700212type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700213 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700214
Colin Cross9f35c3d2020-09-16 19:04:41 -0700215 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
216 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700217 CreateModule(ModuleFactory, ...interface{}) Module
Colin Cross6362e272015-10-29 15:25:03 -0700218}
219
Colin Cross25de6c32019-06-06 14:29:25 -0700220type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700221 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700222 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700223}
224
Colin Cross25de6c32019-06-06 14:29:25 -0700225type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700226
Colin Cross635c3b02016-05-18 15:37:25 -0700227type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700228 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800229
Colin Cross4f1dcb02020-09-16 18:45:04 -0700230 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
231 // dependency (some entries may be nil).
232 //
233 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
234 // new dependencies have had the current mutator called on them. If the mutator is not
235 // parallel this method does not affect the ordering of the current mutator pass, but will
236 // be ordered correctly for all future mutator passes.
237 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700238
239 // AddReverseDependency adds a dependency from the destination to the given module.
240 // Does not affect the ordering of the current mutator pass, but will be ordered
241 // correctly for all future mutator passes. All reverse dependencies for a destination module are
242 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
243 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800244 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700245
246 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
247 // parameter. It returns a list of new modules in the same order as the variationNames
248 // list.
249 //
250 // If any of the dependencies of the module being operated on were already split
251 // by calling CreateVariations with the same name, the dependency will automatically
252 // be updated to point the matching variant.
253 //
254 // If a module is split, and then a module depending on the first module is not split
255 // when the Mutator is later called on it, the dependency of the depending module will
256 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800257 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700258
259 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
260 // parameter. It returns a list of new modules in the same order as the variantNames
261 // list.
262 //
263 // Local variations do not affect automatic dependency resolution - dependencies added
264 // to the split module via deps or DynamicDependerModule must exactly match a variant
265 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800266 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700267
268 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
269 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800270 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700271
272 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
273 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900274 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700275
276 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700277 // argument to select which variant of the dependency to use. It returns a slice of modules for
278 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
279 // the all of the non-local variations of the current module, plus the variations argument.
280 //
281 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
282 // new dependencies have had the current mutator called on them. If the mutator is not
283 // parallel this method does not affect the ordering of the current mutator pass, but will
284 // be ordered correctly for all future mutator passes.
285 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700286
287 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700288 // variations argument to select which variant of the dependency to use. It returns a slice of
289 // modules for each dependency (some entries may be nil). A variant of the dependency must
290 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700291 // For any unspecified variation the first variant will be used.
292 //
293 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
294 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700295 //
296 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
297 // new dependencies have had the current mutator called on them. If the mutator is not
298 // parallel this method does not affect the ordering of the current mutator pass, but will
299 // be ordered correctly for all future mutator passes.
300 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700301
302 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
303 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
304 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
305 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800306 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700307
308 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
309 // specified name with the current variant of this module. Replacements don't take effect until
310 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800311 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700312
313 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
314 // specified name with the current variant of this module as long as the supplied predicate returns
315 // true.
316 //
317 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100318 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700319
320 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
321 // and creates an alias from the current variant (before the mutator has run) to the new
322 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
323 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
324 // be used to add dependencies on the newly created variant using the variant map from
325 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800326 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700327
328 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
329 // module, and creates an alias from a new fromVariationName variant the toVariationName
330 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
331 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
332 // be used to add dependencies on the toVariationName variant using the fromVariationName
333 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700334 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700335
336 // SetVariationProvider sets the value for a provider for the given newly created variant of
337 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
338 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
339 // if the value is not of the appropriate type, or if the module is not a newly created
340 // variant of the current module. The value should not be modified after being passed to
341 // SetVariationProvider.
342 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700343}
344
Colin Cross25de6c32019-06-06 14:29:25 -0700345type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700346 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700347 baseModuleContext
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000348 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700349}
350
Colin Cross617b88a2020-08-24 18:04:09 -0700351func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
352 finalPhase bool) BottomUpMutatorContext {
353
354 return &bottomUpMutatorContext{
355 bp: ctx,
356 baseModuleContext: a.base().baseModuleContextFactory(ctx),
357 finalPhase: finalPhase,
358 }
359}
360
Colin Cross25de6c32019-06-06 14:29:25 -0700361func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000362 finalPhase := x.finalPhase
Colin Cross798bfce2016-10-12 14:28:16 -0700363 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700364 if a, ok := ctx.Module().(Module); ok {
Colin Cross617b88a2020-08-24 18:04:09 -0700365 m(bottomUpMutatorContextFactory(ctx, a, finalPhase))
Colin Cross6362e272015-10-29 15:25:03 -0700366 }
Colin Cross798bfce2016-10-12 14:28:16 -0700367 }
368 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700369 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700370 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700371}
372
Colin Cross617b88a2020-08-24 18:04:09 -0700373func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
374 mutator := &mutator{name: name, bottomUpMutator: m}
375 x.mutators = append(x.mutators, mutator)
376 return mutator
377}
378
Colin Cross25de6c32019-06-06 14:29:25 -0700379func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700380 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700381 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700382 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700383 bp: ctx,
384 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700385 }
Colin Cross798bfce2016-10-12 14:28:16 -0700386 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700387 }
Colin Cross798bfce2016-10-12 14:28:16 -0700388 }
389 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700390 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700391 return mutator
392}
393
394type MutatorHandle interface {
395 Parallel() MutatorHandle
396}
397
398func (mutator *mutator) Parallel() MutatorHandle {
399 mutator.parallel = true
400 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700401}
Colin Cross1e676be2016-10-12 14:38:15 -0700402
Paul Duffin44f1d842020-06-26 20:17:02 +0100403func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
404 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
405}
406
407// A special mutator that runs just prior to the deps mutator to allow the dependencies
408// on component modules to be added so that they can depend directly on a prebuilt
409// module.
410func componentDepsMutator(ctx BottomUpMutatorContext) {
411 if m := ctx.Module(); m.Enabled() {
412 m.ComponentDepsMutator(ctx)
413 }
414}
415
Colin Cross1e676be2016-10-12 14:38:15 -0700416func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100417 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700418 m.DepsMutator(ctx)
419 }
420}
Colin Crossd11fcda2017-10-23 17:59:01 -0700421
Colin Cross25de6c32019-06-06 14:29:25 -0700422func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700423 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700424 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700425 p, nil)
426 if err != nil {
427 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700428 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700429 } else {
430 panic(err)
431 }
432 }
433 }
434}
435
Colin Cross25de6c32019-06-06 14:29:25 -0700436func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700437 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700438 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700439 p, nil)
440 if err != nil {
441 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700442 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700443 } else {
444 panic(err)
445 }
446 }
447 }
448}
Colin Crossdc35e212019-06-06 16:13:11 -0700449
450// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
451// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
452// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
453// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
454// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
455
Colin Crosscb55e082019-07-01 15:32:31 -0700456func (t *topDownMutatorContext) MutatorName() string {
457 return t.bp.MutatorName()
458}
459
Colin Crossdc35e212019-06-06 16:13:11 -0700460func (t *topDownMutatorContext) Rename(name string) {
461 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700462 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700463}
464
Colin Crosse003c4a2019-09-25 12:58:36 -0700465func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Colin Cross18c46802019-09-24 22:19:02 -0700466 inherited := []interface{}{&t.Module().base().commonProperties}
Colin Crosse003c4a2019-09-25 12:58:36 -0700467 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
Colin Cross18c46802019-09-24 22:19:02 -0700468
469 if t.Module().base().variableProperties != nil && module.base().variableProperties != nil {
470 src := t.Module().base().variableProperties
471 dst := []interface{}{
472 module.base().variableProperties,
473 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
474 // don't cause a "failed to find property to extend" error.
Colin Cross43e789d2020-01-28 09:46:50 -0800475 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross18c46802019-09-24 22:19:02 -0700476 }
477 err := proptools.AppendMatchingProperties(dst, src, nil)
478 if err != nil {
479 panic(err)
480 }
481 }
482
Colin Crosse003c4a2019-09-25 12:58:36 -0700483 return module
Colin Crossdc35e212019-06-06 16:13:11 -0700484}
485
Colin Crosscb55e082019-07-01 15:32:31 -0700486func (b *bottomUpMutatorContext) MutatorName() string {
487 return b.bp.MutatorName()
488}
489
Colin Crossdc35e212019-06-06 16:13:11 -0700490func (b *bottomUpMutatorContext) Rename(name string) {
491 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700492 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700493}
494
Colin Cross4f1dcb02020-09-16 18:45:04 -0700495func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
496 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700497}
498
499func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
500 b.bp.AddReverseDependency(module, tag, name)
501}
502
Colin Cross43b92e02019-11-18 15:28:57 -0800503func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000504 if b.finalPhase {
505 panic("CreateVariations not allowed in FinalDepsMutators")
506 }
507
Colin Cross9a362232019-07-01 15:32:45 -0700508 modules := b.bp.CreateVariations(variations...)
509
Colin Cross43b92e02019-11-18 15:28:57 -0800510 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700511 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800512 aModules[i] = modules[i].(Module)
513 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700514 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
515 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
516 }
517
Colin Cross43b92e02019-11-18 15:28:57 -0800518 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700519}
520
Colin Cross43b92e02019-11-18 15:28:57 -0800521func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000522 if b.finalPhase {
523 panic("CreateLocalVariations not allowed in FinalDepsMutators")
524 }
525
Colin Cross9a362232019-07-01 15:32:45 -0700526 modules := b.bp.CreateLocalVariations(variations...)
527
Colin Cross43b92e02019-11-18 15:28:57 -0800528 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700529 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800530 aModules[i] = modules[i].(Module)
531 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700532 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
533 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
534 }
535
Colin Cross43b92e02019-11-18 15:28:57 -0800536 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700537}
538
539func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
540 b.bp.SetDependencyVariation(variation)
541}
542
Jiyong Park1d1119f2019-07-29 21:27:18 +0900543func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
544 b.bp.SetDefaultDependencyVariation(variation)
545}
546
Colin Crossdc35e212019-06-06 16:13:11 -0700547func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700548 names ...string) []blueprint.Module {
Colin Crossdc35e212019-06-06 16:13:11 -0700549
Colin Cross4f1dcb02020-09-16 18:45:04 -0700550 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700551}
552
553func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700554 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Colin Crossdc35e212019-06-06 16:13:11 -0700555
Colin Cross4f1dcb02020-09-16 18:45:04 -0700556 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700557}
558
559func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
560 b.bp.AddInterVariantDependency(tag, from, to)
561}
562
563func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
564 b.bp.ReplaceDependencies(name)
565}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800566
Paul Duffin80342d72020-06-26 22:08:43 +0100567func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
568 b.bp.ReplaceDependenciesIf(name, predicate)
569}
570
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800571func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
572 b.bp.AliasVariation(variationName)
573}
Colin Cross1b9604b2020-08-11 12:03:56 -0700574
575func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
576 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
577}
Colin Crossd27e7b82020-07-02 11:38:17 -0700578
579func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
580 b.bp.SetVariationProvider(module, provider, value)
581}