blob: 2a2be6c3396d6d63010077ce0d854b8eaa9bccee [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 Chen73850672020-12-14 08:25:34 -050047// RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
48func RegisterMutatorsForBazelConversion(ctx *blueprint.Context, bp2buildMutators []RegisterMutatorFunc) {
49 mctx := &registerMutatorsContext{}
50
51 // Register bp2build mutators
52 for _, f := range bp2buildMutators {
53 f(mctx)
54 }
55
56 registerMutatorsToContext(ctx, mctx.mutators)
Jingwen Chen4133ce62020-12-02 04:34:15 -050057}
58
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000059func registerMutators(ctx *blueprint.Context, preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) {
Colin Crosscec81712017-07-13 14:43:27 -070060 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080061
62 register := func(funcs []RegisterMutatorFunc) {
63 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070064 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080065 }
66 }
67
Colin Crosscec81712017-07-13 14:43:27 -070068 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080069
Colin Crosscec81712017-07-13 14:43:27 -070070 register(preDeps)
71
72 mctx.BottomUp("deps", depsMutator).Parallel()
73
74 register(postDeps)
75
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000076 mctx.finalPhase = true
77 register(finalDeps)
78
Colin Crosscec81712017-07-13 14:43:27 -070079 registerMutatorsToContext(ctx, mctx.mutators)
Colin Cross795c3772017-03-16 16:50:10 -070080}
81
82type registerMutatorsContext struct {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000083 mutators []*mutator
84 finalPhase bool
Colin Cross795c3772017-03-16 16:50:10 -070085}
Colin Cross1e676be2016-10-12 14:38:15 -070086
87type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -070088 TopDown(name string, m TopDownMutator) MutatorHandle
89 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -070090 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -070091}
92
93type RegisterMutatorFunc func(RegisterMutatorsContext)
94
Colin Crosscec81712017-07-13 14:43:27 -070095var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -080096 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +010097
Paul Duffinaa4162e2020-05-05 11:35:43 +010098 // Check the visibility rules are valid.
99 //
100 // This must run after the package renamer mutators so that any issues found during
101 // validation of the package's default_visibility property are reported using the
102 // correct package name and not the synthetic name.
103 //
104 // This must also be run before defaults mutators as the rules for validation are
105 // different before checking the rules than they are afterwards. e.g.
106 // visibility: ["//visibility:private", "//visibility:public"]
107 // would be invalid if specified in a module definition but is valid if it results
108 // from something like this:
109 //
110 // defaults {
111 // name: "defaults",
112 // // Be inaccessible outside a package by default.
113 // visibility: ["//visibility:private"]
114 // }
115 //
116 // defaultable_module {
117 // name: "defaultable_module",
118 // defaults: ["defaults"],
119 // // Override the default.
120 // visibility: ["//visibility:public"]
121 // }
122 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000123 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100124
Bob Badour37af0462021-01-07 03:34:31 +0000125 // Record the default_applicable_licenses for each package.
126 //
127 // This must run before the defaults so that defaults modules can pick up the package default.
128 RegisterLicensesPackageMapper,
129
Paul Duffinaa4162e2020-05-05 11:35:43 +0100130 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100131 //
132 // Any mutators that are added before this will not see any modules created by
133 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700134 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100135
Paul Duffin44f1d842020-06-26 20:17:02 +0100136 // Add dependencies on any components so that any component references can be
137 // resolved within the deps mutator.
138 //
139 // Must be run after defaults so it can be used to create dependencies on the
140 // component modules that are creating in a DefaultableHook.
141 //
142 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
143 // renamed. That is so that if a module creates components using a prebuilt module
144 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
145 // the prebuilt module and not the source module.
146 RegisterComponentsMutator,
147
Paul Duffinc988c8e2020-04-29 18:27:14 +0100148 // Create an association between prebuilt modules and their corresponding source
149 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100150 //
151 // Must be run after defaults mutators to ensure that any modules created by
152 // a DefaultableHook can be either a prebuilt or a source module with a matching
153 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100154 RegisterPrebuiltsPreArchMutators,
155
Bob Badour37af0462021-01-07 03:34:31 +0000156 // Gather the licenses properties for all modules for use during expansion and enforcement.
157 //
158 // This must come after the defaults mutators to ensure that any licenses supplied
159 // in a defaults module has been successfully applied before the rules are gathered.
160 RegisterLicensesPropertyGatherer,
161
Paul Duffinaa4162e2020-05-05 11:35:43 +0100162 // Gather the visibility rules for all modules for us during visibility enforcement.
163 //
164 // This must come after the defaults mutators to ensure that any visibility supplied
165 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000166 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700167}
168
Colin Crossae4c6182017-09-15 17:33:55 -0700169func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700170 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800171 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700172 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700173}
174
Colin Crosscec81712017-07-13 14:43:27 -0700175var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700176 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700177}
178
179var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800180 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700181 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000182 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000183 RegisterLicensesDependencyChecker,
Artur Satayevc5570ac2020-04-09 16:06:36 +0100184 RegisterNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700185 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700186}
Colin Cross1e676be2016-10-12 14:38:15 -0700187
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000188var finalDeps = []RegisterMutatorFunc{}
189
Colin Cross1e676be2016-10-12 14:38:15 -0700190func PreArchMutators(f RegisterMutatorFunc) {
191 preArch = append(preArch, f)
192}
193
194func PreDepsMutators(f RegisterMutatorFunc) {
195 preDeps = append(preDeps, f)
196}
197
198func PostDepsMutators(f RegisterMutatorFunc) {
199 postDeps = append(postDeps, f)
200}
201
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000202func FinalDepsMutators(f RegisterMutatorFunc) {
203 finalDeps = append(finalDeps, f)
204}
205
Jingwen Chen73850672020-12-14 08:25:34 -0500206var bp2buildMutators = []RegisterMutatorFunc{}
207
208// RegisterBp2BuildMutator registers specially crafted mutators for
209// converting Blueprint/Android modules into special modules that can
210// be code-generated into Bazel BUILD targets.
211//
212// TODO(b/178068862): bring this into TestContext.
213func RegisterBp2BuildMutator(moduleType string, m func(TopDownMutatorContext)) {
214 mutatorName := moduleType + "_bp2build"
215 f := func(ctx RegisterMutatorsContext) {
216 ctx.TopDown(mutatorName, m)
217 }
218 bp2buildMutators = append(bp2buildMutators, f)
219}
220
Colin Cross9f35c3d2020-09-16 19:04:41 -0700221type BaseMutatorContext interface {
222 BaseModuleContext
223
224 // MutatorName returns the name that this mutator was registered with.
225 MutatorName() string
226
227 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
228 // AddDependency or OtherModuleName until after this mutator pass is complete.
229 Rename(name string)
230}
231
Colin Cross25de6c32019-06-06 14:29:25 -0700232type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700233
Colin Cross635c3b02016-05-18 15:37:25 -0700234type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700235 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700236
Colin Cross9f35c3d2020-09-16 19:04:41 -0700237 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
238 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700239 CreateModule(ModuleFactory, ...interface{}) Module
Colin Cross6362e272015-10-29 15:25:03 -0700240}
241
Colin Cross25de6c32019-06-06 14:29:25 -0700242type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700243 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700244 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700245}
246
Colin Cross25de6c32019-06-06 14:29:25 -0700247type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700248
Colin Cross635c3b02016-05-18 15:37:25 -0700249type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700250 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800251
Colin Cross4f1dcb02020-09-16 18:45:04 -0700252 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
253 // dependency (some entries may be nil).
254 //
255 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
256 // new dependencies have had the current mutator called on them. If the mutator is not
257 // parallel this method does not affect the ordering of the current mutator pass, but will
258 // be ordered correctly for all future mutator passes.
259 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700260
261 // AddReverseDependency adds a dependency from the destination to the given module.
262 // Does not affect the ordering of the current mutator pass, but will be ordered
263 // correctly for all future mutator passes. All reverse dependencies for a destination module are
264 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
265 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800266 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700267
268 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
269 // parameter. It returns a list of new modules in the same order as the variationNames
270 // list.
271 //
272 // If any of the dependencies of the module being operated on were already split
273 // by calling CreateVariations with the same name, the dependency will automatically
274 // be updated to point the matching variant.
275 //
276 // If a module is split, and then a module depending on the first module is not split
277 // when the Mutator is later called on it, the dependency of the depending module will
278 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800279 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700280
281 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
282 // parameter. It returns a list of new modules in the same order as the variantNames
283 // list.
284 //
285 // Local variations do not affect automatic dependency resolution - dependencies added
286 // to the split module via deps or DynamicDependerModule must exactly match a variant
287 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800288 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700289
290 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
291 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800292 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700293
294 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
295 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900296 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700297
298 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700299 // argument to select which variant of the dependency to use. It returns a slice of modules for
300 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
301 // the all of the non-local variations of the current module, plus the variations argument.
302 //
303 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
304 // new dependencies have had the current mutator called on them. If the mutator is not
305 // parallel this method does not affect the ordering of the current mutator pass, but will
306 // be ordered correctly for all future mutator passes.
307 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700308
309 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700310 // variations argument to select which variant of the dependency to use. It returns a slice of
311 // modules for each dependency (some entries may be nil). A variant of the dependency must
312 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700313 // For any unspecified variation the first variant will be used.
314 //
315 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
316 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700317 //
318 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
319 // new dependencies have had the current mutator called on them. If the mutator is not
320 // parallel this method does not affect the ordering of the current mutator pass, but will
321 // be ordered correctly for all future mutator passes.
322 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700323
324 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
325 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
326 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
327 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800328 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700329
330 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
331 // specified name with the current variant of this module. Replacements don't take effect until
332 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800333 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700334
335 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
336 // specified name with the current variant of this module as long as the supplied predicate returns
337 // true.
338 //
339 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100340 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700341
342 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
343 // and creates an alias from the current variant (before the mutator has run) to the new
344 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
345 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
346 // be used to add dependencies on the newly created variant using the variant map from
347 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800348 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700349
350 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
351 // module, and creates an alias from a new fromVariationName variant the toVariationName
352 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
353 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
354 // be used to add dependencies on the toVariationName variant using the fromVariationName
355 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700356 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700357
358 // SetVariationProvider sets the value for a provider for the given newly created variant of
359 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
360 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
361 // if the value is not of the appropriate type, or if the module is not a newly created
362 // variant of the current module. The value should not be modified after being passed to
363 // SetVariationProvider.
364 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700365}
366
Colin Cross25de6c32019-06-06 14:29:25 -0700367type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700368 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700369 baseModuleContext
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000370 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700371}
372
Colin Cross617b88a2020-08-24 18:04:09 -0700373func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
374 finalPhase bool) BottomUpMutatorContext {
375
376 return &bottomUpMutatorContext{
377 bp: ctx,
378 baseModuleContext: a.base().baseModuleContextFactory(ctx),
379 finalPhase: finalPhase,
380 }
381}
382
Colin Cross25de6c32019-06-06 14:29:25 -0700383func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000384 finalPhase := x.finalPhase
Colin Cross798bfce2016-10-12 14:28:16 -0700385 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700386 if a, ok := ctx.Module().(Module); ok {
Colin Cross617b88a2020-08-24 18:04:09 -0700387 m(bottomUpMutatorContextFactory(ctx, a, finalPhase))
Colin Cross6362e272015-10-29 15:25:03 -0700388 }
Colin Cross798bfce2016-10-12 14:28:16 -0700389 }
390 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700391 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700392 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700393}
394
Colin Cross617b88a2020-08-24 18:04:09 -0700395func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
396 mutator := &mutator{name: name, bottomUpMutator: m}
397 x.mutators = append(x.mutators, mutator)
398 return mutator
399}
400
Colin Cross25de6c32019-06-06 14:29:25 -0700401func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700402 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700403 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700404 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700405 bp: ctx,
406 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700407 }
Colin Cross798bfce2016-10-12 14:28:16 -0700408 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700409 }
Colin Cross798bfce2016-10-12 14:28:16 -0700410 }
411 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700412 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700413 return mutator
414}
415
416type MutatorHandle interface {
417 Parallel() MutatorHandle
418}
419
420func (mutator *mutator) Parallel() MutatorHandle {
421 mutator.parallel = true
422 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700423}
Colin Cross1e676be2016-10-12 14:38:15 -0700424
Paul Duffin44f1d842020-06-26 20:17:02 +0100425func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
426 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
427}
428
429// A special mutator that runs just prior to the deps mutator to allow the dependencies
430// on component modules to be added so that they can depend directly on a prebuilt
431// module.
432func componentDepsMutator(ctx BottomUpMutatorContext) {
433 if m := ctx.Module(); m.Enabled() {
434 m.ComponentDepsMutator(ctx)
435 }
436}
437
Colin Cross1e676be2016-10-12 14:38:15 -0700438func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100439 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700440 m.DepsMutator(ctx)
441 }
442}
Colin Crossd11fcda2017-10-23 17:59:01 -0700443
Colin Cross25de6c32019-06-06 14:29:25 -0700444func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700445 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700446 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700447 p, nil)
448 if err != nil {
449 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700450 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700451 } else {
452 panic(err)
453 }
454 }
455 }
456}
457
Colin Cross25de6c32019-06-06 14:29:25 -0700458func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700459 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700460 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700461 p, nil)
462 if err != nil {
463 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700464 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700465 } else {
466 panic(err)
467 }
468 }
469 }
470}
Colin Crossdc35e212019-06-06 16:13:11 -0700471
472// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
473// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
474// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
475// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
476// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
477
Colin Crosscb55e082019-07-01 15:32:31 -0700478func (t *topDownMutatorContext) MutatorName() string {
479 return t.bp.MutatorName()
480}
481
Colin Crossdc35e212019-06-06 16:13:11 -0700482func (t *topDownMutatorContext) Rename(name string) {
483 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700484 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700485}
486
Colin Crosse003c4a2019-09-25 12:58:36 -0700487func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Colin Cross18c46802019-09-24 22:19:02 -0700488 inherited := []interface{}{&t.Module().base().commonProperties}
Colin Crosse003c4a2019-09-25 12:58:36 -0700489 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
Colin Cross18c46802019-09-24 22:19:02 -0700490
491 if t.Module().base().variableProperties != nil && module.base().variableProperties != nil {
492 src := t.Module().base().variableProperties
493 dst := []interface{}{
494 module.base().variableProperties,
495 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
496 // don't cause a "failed to find property to extend" error.
Colin Cross43e789d2020-01-28 09:46:50 -0800497 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross18c46802019-09-24 22:19:02 -0700498 }
499 err := proptools.AppendMatchingProperties(dst, src, nil)
500 if err != nil {
501 panic(err)
502 }
503 }
504
Colin Crosse003c4a2019-09-25 12:58:36 -0700505 return module
Colin Crossdc35e212019-06-06 16:13:11 -0700506}
507
Colin Crosscb55e082019-07-01 15:32:31 -0700508func (b *bottomUpMutatorContext) MutatorName() string {
509 return b.bp.MutatorName()
510}
511
Colin Crossdc35e212019-06-06 16:13:11 -0700512func (b *bottomUpMutatorContext) Rename(name string) {
513 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700514 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700515}
516
Colin Cross4f1dcb02020-09-16 18:45:04 -0700517func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
518 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700519}
520
521func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
522 b.bp.AddReverseDependency(module, tag, name)
523}
524
Colin Cross43b92e02019-11-18 15:28:57 -0800525func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000526 if b.finalPhase {
527 panic("CreateVariations not allowed in FinalDepsMutators")
528 }
529
Colin Cross9a362232019-07-01 15:32:45 -0700530 modules := b.bp.CreateVariations(variations...)
531
Colin Cross43b92e02019-11-18 15:28:57 -0800532 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700533 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800534 aModules[i] = modules[i].(Module)
535 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700536 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
537 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
538 }
539
Colin Cross43b92e02019-11-18 15:28:57 -0800540 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700541}
542
Colin Cross43b92e02019-11-18 15:28:57 -0800543func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000544 if b.finalPhase {
545 panic("CreateLocalVariations not allowed in FinalDepsMutators")
546 }
547
Colin Cross9a362232019-07-01 15:32:45 -0700548 modules := b.bp.CreateLocalVariations(variations...)
549
Colin Cross43b92e02019-11-18 15:28:57 -0800550 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700551 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800552 aModules[i] = modules[i].(Module)
553 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700554 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
555 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
556 }
557
Colin Cross43b92e02019-11-18 15:28:57 -0800558 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700559}
560
561func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
562 b.bp.SetDependencyVariation(variation)
563}
564
Jiyong Park1d1119f2019-07-29 21:27:18 +0900565func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
566 b.bp.SetDefaultDependencyVariation(variation)
567}
568
Colin Crossdc35e212019-06-06 16:13:11 -0700569func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700570 names ...string) []blueprint.Module {
Colin Crossdc35e212019-06-06 16:13:11 -0700571
Colin Cross4f1dcb02020-09-16 18:45:04 -0700572 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700573}
574
575func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700576 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Colin Crossdc35e212019-06-06 16:13:11 -0700577
Colin Cross4f1dcb02020-09-16 18:45:04 -0700578 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700579}
580
581func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
582 b.bp.AddInterVariantDependency(tag, from, to)
583}
584
585func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
586 b.bp.ReplaceDependencies(name)
587}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800588
Paul Duffin80342d72020-06-26 22:08:43 +0100589func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
590 b.bp.ReplaceDependenciesIf(name, predicate)
591}
592
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800593func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
594 b.bp.AliasVariation(variationName)
595}
Colin Cross1b9604b2020-08-11 12:03:56 -0700596
597func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
598 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
599}
Colin Crossd27e7b82020-07-02 11:38:17 -0700600
601func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
602 b.bp.SetVariationProvider(module, provider, value)
603}