blob: 6b19dc568568a9f59b8a660a3cc6e4a238c1b53c [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
Jingwen Chen041b1842021-02-01 00:23:25 -050051 sharedMutators := []RegisterMutatorFunc{
52 RegisterDefaultsPreArchMutators,
53 }
54
55 for _, f := range sharedMutators {
56 f(mctx)
57 }
58
Jingwen Chen73850672020-12-14 08:25:34 -050059 // Register bp2build mutators
60 for _, f := range bp2buildMutators {
61 f(mctx)
62 }
63
64 registerMutatorsToContext(ctx, mctx.mutators)
Jingwen Chen4133ce62020-12-02 04:34:15 -050065}
66
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000067func registerMutators(ctx *blueprint.Context, preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) {
Colin Crosscec81712017-07-13 14:43:27 -070068 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080069
70 register := func(funcs []RegisterMutatorFunc) {
71 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070072 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080073 }
74 }
75
Colin Crosscec81712017-07-13 14:43:27 -070076 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080077
Colin Crosscec81712017-07-13 14:43:27 -070078 register(preDeps)
79
80 mctx.BottomUp("deps", depsMutator).Parallel()
81
82 register(postDeps)
83
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000084 mctx.finalPhase = true
85 register(finalDeps)
86
Colin Crosscec81712017-07-13 14:43:27 -070087 registerMutatorsToContext(ctx, mctx.mutators)
Colin Cross795c3772017-03-16 16:50:10 -070088}
89
90type registerMutatorsContext struct {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000091 mutators []*mutator
92 finalPhase bool
Colin Cross795c3772017-03-16 16:50:10 -070093}
Colin Cross1e676be2016-10-12 14:38:15 -070094
95type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -070096 TopDown(name string, m TopDownMutator) MutatorHandle
97 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -070098 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -070099}
100
101type RegisterMutatorFunc func(RegisterMutatorsContext)
102
Colin Crosscec81712017-07-13 14:43:27 -0700103var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800104 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100105
Paul Duffinaa4162e2020-05-05 11:35:43 +0100106 // Check the visibility rules are valid.
107 //
108 // This must run after the package renamer mutators so that any issues found during
109 // validation of the package's default_visibility property are reported using the
110 // correct package name and not the synthetic name.
111 //
112 // This must also be run before defaults mutators as the rules for validation are
113 // different before checking the rules than they are afterwards. e.g.
114 // visibility: ["//visibility:private", "//visibility:public"]
115 // would be invalid if specified in a module definition but is valid if it results
116 // from something like this:
117 //
118 // defaults {
119 // name: "defaults",
120 // // Be inaccessible outside a package by default.
121 // visibility: ["//visibility:private"]
122 // }
123 //
124 // defaultable_module {
125 // name: "defaultable_module",
126 // defaults: ["defaults"],
127 // // Override the default.
128 // visibility: ["//visibility:public"]
129 // }
130 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000131 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100132
Bob Badour37af0462021-01-07 03:34:31 +0000133 // Record the default_applicable_licenses for each package.
134 //
135 // This must run before the defaults so that defaults modules can pick up the package default.
136 RegisterLicensesPackageMapper,
137
Paul Duffinaa4162e2020-05-05 11:35:43 +0100138 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100139 //
140 // Any mutators that are added before this will not see any modules created by
141 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700142 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100143
Paul Duffin44f1d842020-06-26 20:17:02 +0100144 // Add dependencies on any components so that any component references can be
145 // resolved within the deps mutator.
146 //
147 // Must be run after defaults so it can be used to create dependencies on the
148 // component modules that are creating in a DefaultableHook.
149 //
150 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
151 // renamed. That is so that if a module creates components using a prebuilt module
152 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
153 // the prebuilt module and not the source module.
154 RegisterComponentsMutator,
155
Paul Duffinc988c8e2020-04-29 18:27:14 +0100156 // Create an association between prebuilt modules and their corresponding source
157 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100158 //
159 // Must be run after defaults mutators to ensure that any modules created by
160 // a DefaultableHook can be either a prebuilt or a source module with a matching
161 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100162 RegisterPrebuiltsPreArchMutators,
163
Bob Badour37af0462021-01-07 03:34:31 +0000164 // Gather the licenses properties for all modules for use during expansion and enforcement.
165 //
166 // This must come after the defaults mutators to ensure that any licenses supplied
167 // in a defaults module has been successfully applied before the rules are gathered.
168 RegisterLicensesPropertyGatherer,
169
Paul Duffinaa4162e2020-05-05 11:35:43 +0100170 // Gather the visibility rules for all modules for us during visibility enforcement.
171 //
172 // This must come after the defaults mutators to ensure that any visibility supplied
173 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000174 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700175}
176
Colin Crossae4c6182017-09-15 17:33:55 -0700177func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700178 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800179 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700180 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700181}
182
Colin Crosscec81712017-07-13 14:43:27 -0700183var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700184 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700185}
186
187var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800188 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700189 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000190 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000191 RegisterLicensesDependencyChecker,
Artur Satayevc5570ac2020-04-09 16:06:36 +0100192 RegisterNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700193 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700194}
Colin Cross1e676be2016-10-12 14:38:15 -0700195
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000196var finalDeps = []RegisterMutatorFunc{}
197
Colin Cross1e676be2016-10-12 14:38:15 -0700198func PreArchMutators(f RegisterMutatorFunc) {
199 preArch = append(preArch, f)
200}
201
202func PreDepsMutators(f RegisterMutatorFunc) {
203 preDeps = append(preDeps, f)
204}
205
206func PostDepsMutators(f RegisterMutatorFunc) {
207 postDeps = append(postDeps, f)
208}
209
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000210func FinalDepsMutators(f RegisterMutatorFunc) {
211 finalDeps = append(finalDeps, f)
212}
213
Jingwen Chen73850672020-12-14 08:25:34 -0500214var bp2buildMutators = []RegisterMutatorFunc{}
215
216// RegisterBp2BuildMutator registers specially crafted mutators for
217// converting Blueprint/Android modules into special modules that can
218// be code-generated into Bazel BUILD targets.
219//
220// TODO(b/178068862): bring this into TestContext.
221func RegisterBp2BuildMutator(moduleType string, m func(TopDownMutatorContext)) {
222 mutatorName := moduleType + "_bp2build"
223 f := func(ctx RegisterMutatorsContext) {
224 ctx.TopDown(mutatorName, m)
225 }
226 bp2buildMutators = append(bp2buildMutators, f)
227}
228
Colin Cross9f35c3d2020-09-16 19:04:41 -0700229type BaseMutatorContext interface {
230 BaseModuleContext
231
232 // MutatorName returns the name that this mutator was registered with.
233 MutatorName() string
234
235 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
236 // AddDependency or OtherModuleName until after this mutator pass is complete.
237 Rename(name string)
238}
239
Colin Cross25de6c32019-06-06 14:29:25 -0700240type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700241
Colin Cross635c3b02016-05-18 15:37:25 -0700242type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700243 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700244
Colin Cross9f35c3d2020-09-16 19:04:41 -0700245 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
246 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700247 CreateModule(ModuleFactory, ...interface{}) Module
Colin Cross6362e272015-10-29 15:25:03 -0700248}
249
Colin Cross25de6c32019-06-06 14:29:25 -0700250type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700251 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700252 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700253}
254
Colin Cross25de6c32019-06-06 14:29:25 -0700255type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700256
Colin Cross635c3b02016-05-18 15:37:25 -0700257type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700258 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800259
Colin Cross4f1dcb02020-09-16 18:45:04 -0700260 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
261 // dependency (some entries may be nil).
262 //
263 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
264 // new dependencies have had the current mutator called on them. If the mutator is not
265 // parallel this method does not affect the ordering of the current mutator pass, but will
266 // be ordered correctly for all future mutator passes.
267 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700268
269 // AddReverseDependency adds a dependency from the destination to the given module.
270 // Does not affect the ordering of the current mutator pass, but will be ordered
271 // correctly for all future mutator passes. All reverse dependencies for a destination module are
272 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
273 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800274 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700275
276 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
277 // parameter. It returns a list of new modules in the same order as the variationNames
278 // list.
279 //
280 // If any of the dependencies of the module being operated on were already split
281 // by calling CreateVariations with the same name, the dependency will automatically
282 // be updated to point the matching variant.
283 //
284 // If a module is split, and then a module depending on the first module is not split
285 // when the Mutator is later called on it, the dependency of the depending module will
286 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800287 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700288
289 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
290 // parameter. It returns a list of new modules in the same order as the variantNames
291 // list.
292 //
293 // Local variations do not affect automatic dependency resolution - dependencies added
294 // to the split module via deps or DynamicDependerModule must exactly match a variant
295 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800296 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700297
298 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
299 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800300 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700301
302 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
303 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900304 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700305
306 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700307 // argument to select which variant of the dependency to use. It returns a slice of modules for
308 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
309 // the all of the non-local variations of the current module, plus the variations argument.
310 //
311 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
312 // new dependencies have had the current mutator called on them. If the mutator is not
313 // parallel this method does not affect the ordering of the current mutator pass, but will
314 // be ordered correctly for all future mutator passes.
315 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700316
317 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700318 // variations argument to select which variant of the dependency to use. It returns a slice of
319 // modules for each dependency (some entries may be nil). A variant of the dependency must
320 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700321 // For any unspecified variation the first variant will be used.
322 //
323 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
324 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700325 //
326 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
327 // new dependencies have had the current mutator called on them. If the mutator is not
328 // parallel this method does not affect the ordering of the current mutator pass, but will
329 // be ordered correctly for all future mutator passes.
330 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700331
332 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
333 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
334 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
335 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800336 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700337
338 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
339 // specified name with the current variant of this module. Replacements don't take effect until
340 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800341 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700342
343 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
344 // specified name with the current variant of this module as long as the supplied predicate returns
345 // true.
346 //
347 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100348 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700349
350 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
351 // and creates an alias from the current variant (before the mutator has run) to the new
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 newly created variant using the variant map from
355 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800356 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700357
358 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
359 // module, and creates an alias from a new fromVariationName variant the toVariationName
360 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
361 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
362 // be used to add dependencies on the toVariationName variant using the fromVariationName
363 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700364 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700365
366 // SetVariationProvider sets the value for a provider for the given newly created variant of
367 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
368 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
369 // if the value is not of the appropriate type, or if the module is not a newly created
370 // variant of the current module. The value should not be modified after being passed to
371 // SetVariationProvider.
372 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700373}
374
Colin Cross25de6c32019-06-06 14:29:25 -0700375type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700376 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700377 baseModuleContext
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000378 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700379}
380
Colin Cross617b88a2020-08-24 18:04:09 -0700381func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
382 finalPhase bool) BottomUpMutatorContext {
383
384 return &bottomUpMutatorContext{
385 bp: ctx,
386 baseModuleContext: a.base().baseModuleContextFactory(ctx),
387 finalPhase: finalPhase,
388 }
389}
390
Colin Cross25de6c32019-06-06 14:29:25 -0700391func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000392 finalPhase := x.finalPhase
Colin Cross798bfce2016-10-12 14:28:16 -0700393 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700394 if a, ok := ctx.Module().(Module); ok {
Colin Cross617b88a2020-08-24 18:04:09 -0700395 m(bottomUpMutatorContextFactory(ctx, a, finalPhase))
Colin Cross6362e272015-10-29 15:25:03 -0700396 }
Colin Cross798bfce2016-10-12 14:28:16 -0700397 }
398 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700399 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700400 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700401}
402
Colin Cross617b88a2020-08-24 18:04:09 -0700403func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
404 mutator := &mutator{name: name, bottomUpMutator: m}
405 x.mutators = append(x.mutators, mutator)
406 return mutator
407}
408
Colin Cross25de6c32019-06-06 14:29:25 -0700409func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700410 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700411 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700412 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700413 bp: ctx,
414 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700415 }
Colin Cross798bfce2016-10-12 14:28:16 -0700416 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700417 }
Colin Cross798bfce2016-10-12 14:28:16 -0700418 }
419 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700420 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700421 return mutator
422}
423
424type MutatorHandle interface {
425 Parallel() MutatorHandle
426}
427
428func (mutator *mutator) Parallel() MutatorHandle {
429 mutator.parallel = true
430 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700431}
Colin Cross1e676be2016-10-12 14:38:15 -0700432
Paul Duffin44f1d842020-06-26 20:17:02 +0100433func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
434 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
435}
436
437// A special mutator that runs just prior to the deps mutator to allow the dependencies
438// on component modules to be added so that they can depend directly on a prebuilt
439// module.
440func componentDepsMutator(ctx BottomUpMutatorContext) {
441 if m := ctx.Module(); m.Enabled() {
442 m.ComponentDepsMutator(ctx)
443 }
444}
445
Colin Cross1e676be2016-10-12 14:38:15 -0700446func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100447 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700448 m.DepsMutator(ctx)
449 }
450}
Colin Crossd11fcda2017-10-23 17:59:01 -0700451
Colin Cross25de6c32019-06-06 14:29:25 -0700452func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700453 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700454 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700455 p, nil)
456 if err != nil {
457 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700458 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700459 } else {
460 panic(err)
461 }
462 }
463 }
464}
465
Colin Cross25de6c32019-06-06 14:29:25 -0700466func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700467 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700468 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700469 p, nil)
470 if err != nil {
471 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700472 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700473 } else {
474 panic(err)
475 }
476 }
477 }
478}
Colin Crossdc35e212019-06-06 16:13:11 -0700479
480// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
481// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
482// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
483// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
484// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
485
Colin Crosscb55e082019-07-01 15:32:31 -0700486func (t *topDownMutatorContext) MutatorName() string {
487 return t.bp.MutatorName()
488}
489
Colin Crossdc35e212019-06-06 16:13:11 -0700490func (t *topDownMutatorContext) Rename(name string) {
491 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700492 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700493}
494
Colin Crosse003c4a2019-09-25 12:58:36 -0700495func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Colin Cross18c46802019-09-24 22:19:02 -0700496 inherited := []interface{}{&t.Module().base().commonProperties}
Colin Crosse003c4a2019-09-25 12:58:36 -0700497 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
Colin Cross18c46802019-09-24 22:19:02 -0700498
499 if t.Module().base().variableProperties != nil && module.base().variableProperties != nil {
500 src := t.Module().base().variableProperties
501 dst := []interface{}{
502 module.base().variableProperties,
503 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
504 // don't cause a "failed to find property to extend" error.
Colin Cross43e789d2020-01-28 09:46:50 -0800505 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross18c46802019-09-24 22:19:02 -0700506 }
507 err := proptools.AppendMatchingProperties(dst, src, nil)
508 if err != nil {
509 panic(err)
510 }
511 }
512
Colin Crosse003c4a2019-09-25 12:58:36 -0700513 return module
Colin Crossdc35e212019-06-06 16:13:11 -0700514}
515
Colin Crosscb55e082019-07-01 15:32:31 -0700516func (b *bottomUpMutatorContext) MutatorName() string {
517 return b.bp.MutatorName()
518}
519
Colin Crossdc35e212019-06-06 16:13:11 -0700520func (b *bottomUpMutatorContext) Rename(name string) {
521 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700522 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700523}
524
Colin Cross4f1dcb02020-09-16 18:45:04 -0700525func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
526 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700527}
528
529func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
530 b.bp.AddReverseDependency(module, tag, name)
531}
532
Colin Cross43b92e02019-11-18 15:28:57 -0800533func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000534 if b.finalPhase {
535 panic("CreateVariations not allowed in FinalDepsMutators")
536 }
537
Colin Cross9a362232019-07-01 15:32:45 -0700538 modules := b.bp.CreateVariations(variations...)
539
Colin Cross43b92e02019-11-18 15:28:57 -0800540 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700541 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800542 aModules[i] = modules[i].(Module)
543 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700544 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
545 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
546 }
547
Colin Cross43b92e02019-11-18 15:28:57 -0800548 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700549}
550
Colin Cross43b92e02019-11-18 15:28:57 -0800551func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000552 if b.finalPhase {
553 panic("CreateLocalVariations not allowed in FinalDepsMutators")
554 }
555
Colin Cross9a362232019-07-01 15:32:45 -0700556 modules := b.bp.CreateLocalVariations(variations...)
557
Colin Cross43b92e02019-11-18 15:28:57 -0800558 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700559 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800560 aModules[i] = modules[i].(Module)
561 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700562 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
563 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
564 }
565
Colin Cross43b92e02019-11-18 15:28:57 -0800566 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700567}
568
569func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
570 b.bp.SetDependencyVariation(variation)
571}
572
Jiyong Park1d1119f2019-07-29 21:27:18 +0900573func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
574 b.bp.SetDefaultDependencyVariation(variation)
575}
576
Colin Crossdc35e212019-06-06 16:13:11 -0700577func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700578 names ...string) []blueprint.Module {
Colin Crossdc35e212019-06-06 16:13:11 -0700579
Colin Cross4f1dcb02020-09-16 18:45:04 -0700580 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700581}
582
583func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700584 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Colin Crossdc35e212019-06-06 16:13:11 -0700585
Colin Cross4f1dcb02020-09-16 18:45:04 -0700586 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700587}
588
589func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
590 b.bp.AddInterVariantDependency(tag, from, to)
591}
592
593func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
594 b.bp.ReplaceDependencies(name)
595}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800596
Paul Duffin80342d72020-06-26 22:08:43 +0100597func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
598 b.bp.ReplaceDependenciesIf(name, predicate)
599}
600
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800601func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
602 b.bp.AliasVariation(variationName)
603}
Colin Cross1b9604b2020-08-11 12:03:56 -0700604
605func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
606 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
607}
Colin Crossd27e7b82020-07-02 11:38:17 -0700608
609func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
610 b.bp.SetVariationProvider(module, provider, value)
611}