blob: 16b9ba021218a9f041af4f25921bc2cceb866d3d [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 Cross984223f2024-02-01 17:10:23 -080018 "sync"
19
Colin Cross795c3772017-03-16 16:50:10 -070020 "github.com/google/blueprint"
21)
Colin Cross6362e272015-10-29 15:25:03 -070022
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070023// Phases:
24// run Pre-arch mutators
25// run archMutator
26// run Pre-deps mutators
27// run depsMutator
28// run PostDeps mutators
Colin Cross1e954b62024-09-13 13:50:00 -070029// run FinalDeps mutators (TransitionMutators disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070030// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070031
Paul Duffinc05b0342021-03-06 13:28:13 +000032// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
33// with the InitRegistrationContext and will be used at runtime.
34func collateGloballyRegisteredMutators() sortableComponents {
Colin Crossf22fe412024-10-01 14:02:12 -070035 return collateRegisteredMutators(preArch, preDeps, postDeps, postApex, finalDeps)
Paul Duffinc05b0342021-03-06 13:28:13 +000036}
37
38// collateRegisteredMutators constructs a single list of mutators from the separate lists.
Colin Crossf22fe412024-10-01 14:02:12 -070039func collateRegisteredMutators(preArch, preDeps, postDeps, postApex, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070040 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080041
42 register := func(funcs []RegisterMutatorFunc) {
43 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070044 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080045 }
46 }
47
Colin Crosscec81712017-07-13 14:43:27 -070048 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080049
Colin Crosscec81712017-07-13 14:43:27 -070050 register(preDeps)
51
Liz Kammer356f7d42021-01-26 09:18:53 -050052 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070053
54 register(postDeps)
55
Colin Crossf22fe412024-10-01 14:02:12 -070056 register(postApex)
57
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000058 mctx.finalPhase = true
59 register(finalDeps)
60
Paul Duffinc05b0342021-03-06 13:28:13 +000061 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -070062}
63
64type registerMutatorsContext struct {
Colin Crossb63d7b32023-12-07 16:54:51 -080065 mutators sortableComponents
66 finalPhase bool
Colin Cross795c3772017-03-16 16:50:10 -070067}
Colin Cross1e676be2016-10-12 14:38:15 -070068
69type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -070070 TopDown(name string, m TopDownMutator) MutatorHandle
71 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -070072 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Cole Faustfc7a4112024-11-04 15:07:12 -080073 Transition(name string, m TransitionMutator) TransitionMutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -070074}
75
76type RegisterMutatorFunc func(RegisterMutatorsContext)
77
Colin Crosscec81712017-07-13 14:43:27 -070078var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -080079 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +010080
Paul Duffinaa4162e2020-05-05 11:35:43 +010081 // Check the visibility rules are valid.
82 //
83 // This must run after the package renamer mutators so that any issues found during
84 // validation of the package's default_visibility property are reported using the
85 // correct package name and not the synthetic name.
86 //
87 // This must also be run before defaults mutators as the rules for validation are
88 // different before checking the rules than they are afterwards. e.g.
89 // visibility: ["//visibility:private", "//visibility:public"]
90 // would be invalid if specified in a module definition but is valid if it results
91 // from something like this:
92 //
93 // defaults {
94 // name: "defaults",
95 // // Be inaccessible outside a package by default.
96 // visibility: ["//visibility:private"]
97 // }
98 //
99 // defaultable_module {
100 // name: "defaultable_module",
101 // defaults: ["defaults"],
102 // // Override the default.
103 // visibility: ["//visibility:public"]
104 // }
105 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000106 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100107
Bob Badour37af0462021-01-07 03:34:31 +0000108 // Record the default_applicable_licenses for each package.
109 //
110 // This must run before the defaults so that defaults modules can pick up the package default.
111 RegisterLicensesPackageMapper,
112
Paul Duffinaa4162e2020-05-05 11:35:43 +0100113 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100114 //
115 // Any mutators that are added before this will not see any modules created by
116 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700117 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100118
Paul Duffin44f1d842020-06-26 20:17:02 +0100119 // Add dependencies on any components so that any component references can be
120 // resolved within the deps mutator.
121 //
122 // Must be run after defaults so it can be used to create dependencies on the
123 // component modules that are creating in a DefaultableHook.
124 //
125 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
126 // renamed. That is so that if a module creates components using a prebuilt module
127 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
128 // the prebuilt module and not the source module.
129 RegisterComponentsMutator,
130
Paul Duffinc988c8e2020-04-29 18:27:14 +0100131 // Create an association between prebuilt modules and their corresponding source
132 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100133 //
134 // Must be run after defaults mutators to ensure that any modules created by
135 // a DefaultableHook can be either a prebuilt or a source module with a matching
136 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100137 RegisterPrebuiltsPreArchMutators,
138
Bob Badour37af0462021-01-07 03:34:31 +0000139 // Gather the licenses properties for all modules for use during expansion and enforcement.
140 //
141 // This must come after the defaults mutators to ensure that any licenses supplied
142 // in a defaults module has been successfully applied before the rules are gathered.
143 RegisterLicensesPropertyGatherer,
144
Paul Duffinaa4162e2020-05-05 11:35:43 +0100145 // Gather the visibility rules for all modules for us during visibility enforcement.
146 //
147 // This must come after the defaults mutators to ensure that any visibility supplied
148 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000149 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700150}
151
Colin Crossae4c6182017-09-15 17:33:55 -0700152func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700153 ctx.Transition("os", &osTransitionMutator{})
Cole Faust5b7635d2024-10-28 13:01:12 -0700154 ctx.BottomUp("image_begin", imageMutatorBeginMutator)
Jihoon Kang5402bbd2024-07-31 18:37:49 +0000155 ctx.Transition("image", &imageTransitionMutator{})
Colin Cross8bbc3d52024-09-11 15:33:54 -0700156 ctx.Transition("arch", &archTransitionMutator{})
Colin Crossae4c6182017-09-15 17:33:55 -0700157}
158
Colin Crosscec81712017-07-13 14:43:27 -0700159var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700160 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700161}
162
163var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800164 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700165 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000166 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000167 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100168 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700169 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700170}
Colin Cross1e676be2016-10-12 14:38:15 -0700171
Colin Crossf22fe412024-10-01 14:02:12 -0700172var postApex = []RegisterMutatorFunc{}
173
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000174var finalDeps = []RegisterMutatorFunc{}
175
Colin Cross1e676be2016-10-12 14:38:15 -0700176func PreArchMutators(f RegisterMutatorFunc) {
177 preArch = append(preArch, f)
178}
179
180func PreDepsMutators(f RegisterMutatorFunc) {
181 preDeps = append(preDeps, f)
182}
183
184func PostDepsMutators(f RegisterMutatorFunc) {
185 postDeps = append(postDeps, f)
186}
187
Colin Crossf22fe412024-10-01 14:02:12 -0700188func PostApexMutators(f RegisterMutatorFunc) {
189 postApex = append(postApex, f)
190}
191
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000192func FinalDepsMutators(f RegisterMutatorFunc) {
193 finalDeps = append(finalDeps, f)
194}
195
Chris Parsons637458d2023-09-19 20:09:00 +0000196type TopDownMutator func(TopDownMutatorContext)
197
198type TopDownMutatorContext interface {
Colin Crossb2388e32024-10-07 15:05:23 -0700199 BaseModuleContext
Chris Parsons637458d2023-09-19 20:09:00 +0000200}
201
Colin Cross25de6c32019-06-06 14:29:25 -0700202type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700203 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700204 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700205}
206
Colin Cross25de6c32019-06-06 14:29:25 -0700207type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700208
Colin Cross635c3b02016-05-18 15:37:25 -0700209type BottomUpMutatorContext interface {
Colin Crossb2388e32024-10-07 15:05:23 -0700210 BaseModuleContext
Colin Crossb63d7b32023-12-07 16:54:51 -0800211
212 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
213 // dependency (some entries may be nil).
214 //
Colin Cross8a962802024-10-09 15:29:27 -0700215 // This method will pause until the new dependencies have had the current mutator called on them.
Colin Crossd5c64312024-12-18 16:04:52 -0800216 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []Module
Colin Crossaabf6792017-11-29 00:27:14 -0800217
Colin Cross9f35c3d2020-09-16 19:04:41 -0700218 // AddReverseDependency adds a dependency from the destination to the given module.
219 // Does not affect the ordering of the current mutator pass, but will be ordered
220 // correctly for all future mutator passes. All reverse dependencies for a destination module are
221 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
Colin Crossb8533a82024-10-05 15:25:09 -0700222 // module's dependency list. May only be called by mutators that were marked with
223 // UsesReverseDependencies during registration.
Colin Crossaabf6792017-11-29 00:27:14 -0800224 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700225
Colin Cross9f35c3d2020-09-16 19:04:41 -0700226 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700227 // argument to select which variant of the dependency to use. It returns a slice of modules for
228 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
Usta Shresthac725f472022-01-11 02:44:21 -0500229 // all the non-local variations of the current module, plus the variations argument.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700230 //
Colin Cross8a962802024-10-09 15:29:27 -0700231 // This method will pause until the new dependencies have had the current mutator called on them.
Colin Crossd5c64312024-12-18 16:04:52 -0800232 AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700233
Colin Crossb8533a82024-10-05 15:25:09 -0700234 // AddReverseVariationDependency adds a dependency from the named module to the current
Cole Faustf4992e82024-09-30 15:16:18 -0700235 // module. The given variations will be added to the current module's varations, and then the
236 // result will be used to find the correct variation of the depending module, which must exist.
237 //
238 // Does not affect the ordering of the current mutator pass, but will be ordered
239 // correctly for all future mutator passes. All reverse dependencies for a destination module are
240 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
Colin Crossb8533a82024-10-05 15:25:09 -0700241 // module's dependency list. May only be called by mutators that were marked with
242 // UsesReverseDependencies during registration.
Cole Faustf4992e82024-09-30 15:16:18 -0700243 AddReverseVariationDependency([]blueprint.Variation, blueprint.DependencyTag, string)
244
Colin Cross9f35c3d2020-09-16 19:04:41 -0700245 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700246 // variations argument to select which variant of the dependency to use. It returns a slice of
247 // modules for each dependency (some entries may be nil). A variant of the dependency must
248 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700249 // For any unspecified variation the first variant will be used.
250 //
251 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
252 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700253 //
Colin Cross8a962802024-10-09 15:29:27 -0700254 // This method will pause until the new dependencies have had the current mutator called on them.
Colin Crossd5c64312024-12-18 16:04:52 -0800255 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700256
Colin Cross86771322024-05-01 14:19:51 -0700257 // ReplaceDependencies finds all the variants of the module with the specified name, then
258 // replaces all dependencies onto those variants with the current variant of this module.
Colin Crossb8533a82024-10-05 15:25:09 -0700259 // Replacements don't take effect until after the mutator pass is finished. May only
260 // be called by mutators that were marked with UsesReplaceDependencies during registration.
Colin Crossaabf6792017-11-29 00:27:14 -0800261 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700262
Colin Cross86771322024-05-01 14:19:51 -0700263 // ReplaceDependenciesIf finds all the variants of the module with the specified name, then
264 // replaces all dependencies onto those variants with the current variant of this module
265 // as long as the supplied predicate returns true.
Colin Crossb8533a82024-10-05 15:25:09 -0700266 // Replacements don't take effect until after the mutator pass is finished. May only
267 // be called by mutators that were marked with UsesReplaceDependencies during registration.
Paul Duffin80342d72020-06-26 22:08:43 +0100268 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Crossb2388e32024-10-07 15:05:23 -0700269
270 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
Colin Crossb8533a82024-10-05 15:25:09 -0700271 // AddDependency or OtherModuleName until after this mutator pass is complete. May only be called
272 // by mutators that were marked with UsesRename during registration.
Colin Crossb2388e32024-10-07 15:05:23 -0700273 Rename(name string)
274
275 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
Colin Crossb8533a82024-10-05 15:25:09 -0700276 // the specified property structs to it as if the properties were set in a blueprint file. May only
277 // be called by mutators that were marked with UsesCreateModule during registration.
Colin Crossb2388e32024-10-07 15:05:23 -0700278 CreateModule(ModuleFactory, ...interface{}) Module
Colin Cross6362e272015-10-29 15:25:03 -0700279}
280
Colin Cross984223f2024-02-01 17:10:23 -0800281// An outgoingTransitionContextImpl and incomingTransitionContextImpl is created for every dependency of every module
282// for each transition mutator. bottomUpMutatorContext and topDownMutatorContext are created once for every module
283// for every BottomUp or TopDown mutator. Use a global pool for each to avoid reallocating every time.
284var (
285 outgoingTransitionContextPool = sync.Pool{
286 New: func() any { return &outgoingTransitionContextImpl{} },
287 }
288 incomingTransitionContextPool = sync.Pool{
289 New: func() any { return &incomingTransitionContextImpl{} },
290 }
291 bottomUpMutatorContextPool = sync.Pool{
292 New: func() any { return &bottomUpMutatorContext{} },
293 }
294
295 topDownMutatorContextPool = sync.Pool{
296 New: func() any { return &topDownMutatorContext{} },
297 }
298)
299
Colin Cross25de6c32019-06-06 14:29:25 -0700300type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700301 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700302 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400303 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700304}
305
Colin Cross984223f2024-02-01 17:10:23 -0800306// callers must immediately follow the call to this function with defer bottomUpMutatorContextPool.Put(mctx).
Colin Cross617b88a2020-08-24 18:04:09 -0700307func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Colin Crossb63d7b32023-12-07 16:54:51 -0800308 finalPhase bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700309
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400310 moduleContext := a.base().baseModuleContextFactory(ctx)
Colin Cross984223f2024-02-01 17:10:23 -0800311 mctx := bottomUpMutatorContextPool.Get().(*bottomUpMutatorContext)
312 *mctx = bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400313 bp: ctx,
Cole Faust0abd4b42023-01-10 10:49:18 -0800314 baseModuleContext: moduleContext,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400315 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700316 }
Colin Cross984223f2024-02-01 17:10:23 -0800317 return mctx
Colin Cross617b88a2020-08-24 18:04:09 -0700318}
319
Colin Cross25de6c32019-06-06 14:29:25 -0700320func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000321 finalPhase := x.finalPhase
Colin Cross798bfce2016-10-12 14:28:16 -0700322 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700323 if a, ok := ctx.Module().(Module); ok {
Colin Cross984223f2024-02-01 17:10:23 -0800324 mctx := bottomUpMutatorContextFactory(ctx, a, finalPhase)
325 defer bottomUpMutatorContextPool.Put(mctx)
326 m(mctx)
Colin Cross6362e272015-10-29 15:25:03 -0700327 }
Colin Cross798bfce2016-10-12 14:28:16 -0700328 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500329 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700330 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700331 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700332}
333
Colin Cross617b88a2020-08-24 18:04:09 -0700334func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
335 mutator := &mutator{name: name, bottomUpMutator: m}
336 x.mutators = append(x.mutators, mutator)
337 return mutator
338}
339
Cole Faustfc7a4112024-11-04 15:07:12 -0800340func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) TransitionMutatorHandle {
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200341 atm := &androidTransitionMutator{
Colin Crossb63d7b32023-12-07 16:54:51 -0800342 finalPhase: x.finalPhase,
343 mutator: m,
Colin Crossd67425d2024-04-15 15:17:05 -0700344 name: name,
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200345 }
346 mutator := &mutator{
347 name: name,
Cole Faustfc7a4112024-11-04 15:07:12 -0800348 transitionMutator: atm,
349 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200350 x.mutators = append(x.mutators, mutator)
Cole Faustfc7a4112024-11-04 15:07:12 -0800351 return mutator
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200352}
353
Liz Kammer356f7d42021-01-26 09:18:53 -0500354func (x *registerMutatorsContext) mutatorName(name string) string {
Liz Kammer356f7d42021-01-26 09:18:53 -0500355 return name
356}
357
Colin Cross25de6c32019-06-06 14:29:25 -0700358func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700359 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700360 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400361 moduleContext := a.base().baseModuleContextFactory(ctx)
Colin Cross984223f2024-02-01 17:10:23 -0800362 actx := topDownMutatorContextPool.Get().(*topDownMutatorContext)
363 defer topDownMutatorContextPool.Put(actx)
364 *actx = topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700365 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400366 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700367 }
Colin Cross798bfce2016-10-12 14:28:16 -0700368 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700369 }
Colin Cross798bfce2016-10-12 14:28:16 -0700370 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500371 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700372 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700373 return mutator
374}
375
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000376func (mutator *mutator) componentName() string {
377 return mutator.name
378}
379
380func (mutator *mutator) register(ctx *Context) {
381 blueprintCtx := ctx.Context
382 var handle blueprint.MutatorHandle
383 if mutator.bottomUpMutator != nil {
384 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
385 } else if mutator.topDownMutator != nil {
386 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200387 } else if mutator.transitionMutator != nil {
Cole Faustfc7a4112024-11-04 15:07:12 -0800388 handle := blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
389 if mutator.neverFar {
390 handle.NeverFar()
391 }
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000392 }
Colin Crossb8533a82024-10-05 15:25:09 -0700393
394 // Forward booleans set on the MutatorHandle to the blueprint.MutatorHandle.
Colin Crossb8533a82024-10-05 15:25:09 -0700395 if mutator.usesRename {
396 handle.UsesRename()
397 }
398 if mutator.usesReverseDependencies {
399 handle.UsesReverseDependencies()
400 }
401 if mutator.usesReplaceDependencies {
402 handle.UsesReplaceDependencies()
403 }
404 if mutator.usesCreateModule {
405 handle.UsesCreateModule()
406 }
407 if mutator.mutatesDependencies {
408 handle.MutatesDependencies()
409 }
410 if mutator.mutatesGlobalState {
411 handle.MutatesGlobalState()
412 }
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000413}
414
Colin Cross798bfce2016-10-12 14:28:16 -0700415type MutatorHandle interface {
Colin Crossb8533a82024-10-05 15:25:09 -0700416 // Parallel sets the mutator to visit modules in parallel while maintaining ordering. Calling any
417 // method on the mutator context is thread-safe, but the mutator must handle synchronization
418 // for any modifications to global state or any modules outside the one it was invoked on.
Colin Cross8a962802024-10-09 15:29:27 -0700419 // Deprecated: all Mutators are parallel by default.
Colin Cross798bfce2016-10-12 14:28:16 -0700420 Parallel() MutatorHandle
Colin Crossb8533a82024-10-05 15:25:09 -0700421
422 // UsesRename marks the mutator as using the BottomUpMutatorContext.Rename method, which prevents
423 // coalescing adjacent mutators into a single mutator pass.
424 UsesRename() MutatorHandle
425
426 // UsesReverseDependencies marks the mutator as using the BottomUpMutatorContext.AddReverseDependency
427 // method, which prevents coalescing adjacent mutators into a single mutator pass.
428 UsesReverseDependencies() MutatorHandle
429
430 // UsesReplaceDependencies marks the mutator as using the BottomUpMutatorContext.ReplaceDependencies
431 // method, which prevents coalescing adjacent mutators into a single mutator pass.
432 UsesReplaceDependencies() MutatorHandle
433
434 // UsesCreateModule marks the mutator as using the BottomUpMutatorContext.CreateModule method,
435 // which prevents coalescing adjacent mutators into a single mutator pass.
436 UsesCreateModule() MutatorHandle
437
438 // MutatesDependencies marks the mutator as modifying properties in dependencies, which prevents
439 // coalescing adjacent mutators into a single mutator pass.
440 MutatesDependencies() MutatorHandle
441
442 // MutatesGlobalState marks the mutator as modifying global state, which prevents coalescing
443 // adjacent mutators into a single mutator pass.
444 MutatesGlobalState() MutatorHandle
Colin Cross798bfce2016-10-12 14:28:16 -0700445}
446
Cole Faustfc7a4112024-11-04 15:07:12 -0800447type TransitionMutatorHandle interface {
448 // NeverFar causes the variations created by this mutator to never be ignored when adding
449 // far variation dependencies. Normally, far variation dependencies ignore all the variants
450 // of the source module, and only use the variants explicitly requested by the
451 // AddFarVariationDependencies call.
452 NeverFar() MutatorHandle
453}
454
Colin Cross798bfce2016-10-12 14:28:16 -0700455func (mutator *mutator) Parallel() MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700456 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700457}
Colin Cross1e676be2016-10-12 14:38:15 -0700458
Colin Crossb8533a82024-10-05 15:25:09 -0700459func (mutator *mutator) UsesRename() MutatorHandle {
460 mutator.usesRename = true
461 return mutator
462}
463
464func (mutator *mutator) UsesReverseDependencies() MutatorHandle {
465 mutator.usesReverseDependencies = true
466 return mutator
467}
468
469func (mutator *mutator) UsesReplaceDependencies() MutatorHandle {
470 mutator.usesReplaceDependencies = true
471 return mutator
472}
473
474func (mutator *mutator) UsesCreateModule() MutatorHandle {
475 mutator.usesCreateModule = true
476 return mutator
477}
478
479func (mutator *mutator) MutatesDependencies() MutatorHandle {
480 mutator.mutatesDependencies = true
481 return mutator
482}
483
484func (mutator *mutator) MutatesGlobalState() MutatorHandle {
485 mutator.mutatesGlobalState = true
486 return mutator
487}
488
Cole Faustfc7a4112024-11-04 15:07:12 -0800489func (mutator *mutator) NeverFar() MutatorHandle {
490 mutator.neverFar = true
491 return mutator
492}
493
Paul Duffin44f1d842020-06-26 20:17:02 +0100494func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -0700495 ctx.BottomUp("component-deps", componentDepsMutator)
Paul Duffin44f1d842020-06-26 20:17:02 +0100496}
497
498// A special mutator that runs just prior to the deps mutator to allow the dependencies
499// on component modules to be added so that they can depend directly on a prebuilt
500// module.
501func componentDepsMutator(ctx BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -0700502 ctx.Module().ComponentDepsMutator(ctx)
Paul Duffin44f1d842020-06-26 20:17:02 +0100503}
504
Colin Cross1e676be2016-10-12 14:38:15 -0700505func depsMutator(ctx BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -0700506 if m := ctx.Module(); m.Enabled(ctx) {
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800507 m.base().baseDepsMutator(ctx)
Colin Cross1e676be2016-10-12 14:38:15 -0700508 m.DepsMutator(ctx)
509 }
510}
Colin Crossd11fcda2017-10-23 17:59:01 -0700511
Liz Kammer356f7d42021-01-26 09:18:53 -0500512func registerDepsMutator(ctx RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -0700513 ctx.BottomUp("deps", depsMutator).UsesReverseDependencies()
Liz Kammer356f7d42021-01-26 09:18:53 -0500514}
515
Colin Crossdc35e212019-06-06 16:13:11 -0700516// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
517// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
518// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
519// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
520// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
521
Colin Crossdc35e212019-06-06 16:13:11 -0700522func (b *bottomUpMutatorContext) Rename(name string) {
523 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700524 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700525}
526
Colin Crossd5c64312024-12-18 16:04:52 -0800527func (b *bottomUpMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) Module {
528 return bpModuleToModule(b.bp.CreateModule(factory, name, props...))
Colin Crossda279cf2024-09-17 14:25:45 -0700529}
530
Colin Crossd5c64312024-12-18 16:04:52 -0800531func (b *bottomUpMutatorContext) createModuleInDirectory(factory blueprint.ModuleFactory, name string, _ string, props ...interface{}) Module {
Jihoon Kang222874d2024-10-23 23:38:05 +0000532 panic("createModuleInDirectory is not implemented for bottomUpMutatorContext")
533}
534
Colin Crossda279cf2024-09-17 14:25:45 -0700535func (b *bottomUpMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Jihoon Kang222874d2024-10-23 23:38:05 +0000536 return createModule(b, factory, "_bottomUpMutatorModule", doesNotSpecifyDirectory(), props...)
Colin Crossda279cf2024-09-17 14:25:45 -0700537}
538
Colin Crossd5c64312024-12-18 16:04:52 -0800539func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400540 if b.baseModuleContext.checkedMissingDeps() {
541 panic("Adding deps not allowed after checking for missing deps")
542 }
Colin Crossd5c64312024-12-18 16:04:52 -0800543 return bpModulesToModules(b.bp.AddDependency(module, tag, name...))
Colin Crossdc35e212019-06-06 16:13:11 -0700544}
545
546func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400547 if b.baseModuleContext.checkedMissingDeps() {
548 panic("Adding deps not allowed after checking for missing deps")
549 }
Colin Crossdc35e212019-06-06 16:13:11 -0700550 b.bp.AddReverseDependency(module, tag, name)
551}
Cole Faustf4992e82024-09-30 15:16:18 -0700552
553func (b *bottomUpMutatorContext) AddReverseVariationDependency(variations []blueprint.Variation, tag blueprint.DependencyTag, name string) {
554 if b.baseModuleContext.checkedMissingDeps() {
555 panic("Adding deps not allowed after checking for missing deps")
556 }
557 b.bp.AddReverseVariationDependency(variations, tag, name)
558}
559
Colin Crossdc35e212019-06-06 16:13:11 -0700560func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Crossd5c64312024-12-18 16:04:52 -0800561 names ...string) []Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400562 if b.baseModuleContext.checkedMissingDeps() {
563 panic("Adding deps not allowed after checking for missing deps")
564 }
Colin Crossd5c64312024-12-18 16:04:52 -0800565 return bpModulesToModules(b.bp.AddVariationDependencies(variations, tag, names...))
Colin Crossdc35e212019-06-06 16:13:11 -0700566}
567
568func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Crossd5c64312024-12-18 16:04:52 -0800569 tag blueprint.DependencyTag, names ...string) []Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400570 if b.baseModuleContext.checkedMissingDeps() {
571 panic("Adding deps not allowed after checking for missing deps")
572 }
Colin Crossdc35e212019-06-06 16:13:11 -0700573
Colin Crossd5c64312024-12-18 16:04:52 -0800574 return bpModulesToModules(b.bp.AddFarVariationDependencies(variations, tag, names...))
Colin Crossdc35e212019-06-06 16:13:11 -0700575}
576
Colin Crossdc35e212019-06-06 16:13:11 -0700577func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400578 if b.baseModuleContext.checkedMissingDeps() {
579 panic("Adding deps not allowed after checking for missing deps")
580 }
Colin Crossdc35e212019-06-06 16:13:11 -0700581 b.bp.ReplaceDependencies(name)
582}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800583
Paul Duffin80342d72020-06-26 22:08:43 +0100584func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400585 if b.baseModuleContext.checkedMissingDeps() {
586 panic("Adding deps not allowed after checking for missing deps")
587 }
Paul Duffin80342d72020-06-26 22:08:43 +0100588 b.bp.ReplaceDependenciesIf(name, predicate)
589}
Colin Crossd5c64312024-12-18 16:04:52 -0800590
591func bpModulesToModules(bpModules []blueprint.Module) []Module {
592 modules := make([]Module, len(bpModules))
593 for i, bpModule := range bpModules {
594 modules[i] = bpModuleToModule(bpModule)
595 }
596 return modules
597}
598
599func bpModuleToModule(bpModule blueprint.Module) Module {
600 if bpModule != nil {
601 return bpModule.(Module)
602 }
603 return nil
604}