blob: 76487fbade9d32526d92aa0209a46f4ea50caca5 [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
Colin Cross90d7df92025-01-31 11:29:33 -080073 Transition(name string, m VariationTransitionMutator) TransitionMutatorHandle
74 InfoBasedTransition(name string, m androidTransitionMutator) TransitionMutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -070075}
76
77type RegisterMutatorFunc func(RegisterMutatorsContext)
78
Colin Crosscec81712017-07-13 14:43:27 -070079var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -080080 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +010081
Paul Duffinaa4162e2020-05-05 11:35:43 +010082 // Check the visibility rules are valid.
83 //
84 // This must run after the package renamer mutators so that any issues found during
85 // validation of the package's default_visibility property are reported using the
86 // correct package name and not the synthetic name.
87 //
88 // This must also be run before defaults mutators as the rules for validation are
89 // different before checking the rules than they are afterwards. e.g.
90 // visibility: ["//visibility:private", "//visibility:public"]
91 // would be invalid if specified in a module definition but is valid if it results
92 // from something like this:
93 //
94 // defaults {
95 // name: "defaults",
96 // // Be inaccessible outside a package by default.
97 // visibility: ["//visibility:private"]
98 // }
99 //
100 // defaultable_module {
101 // name: "defaultable_module",
102 // defaults: ["defaults"],
103 // // Override the default.
104 // visibility: ["//visibility:public"]
105 // }
106 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000107 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100108
Bob Badour37af0462021-01-07 03:34:31 +0000109 // Record the default_applicable_licenses for each package.
110 //
111 // This must run before the defaults so that defaults modules can pick up the package default.
112 RegisterLicensesPackageMapper,
113
Paul Duffinaa4162e2020-05-05 11:35:43 +0100114 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100115 //
116 // Any mutators that are added before this will not see any modules created by
117 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700118 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100119
Paul Duffin44f1d842020-06-26 20:17:02 +0100120 // Add dependencies on any components so that any component references can be
121 // resolved within the deps mutator.
122 //
123 // Must be run after defaults so it can be used to create dependencies on the
124 // component modules that are creating in a DefaultableHook.
125 //
126 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
127 // renamed. That is so that if a module creates components using a prebuilt module
128 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
129 // the prebuilt module and not the source module.
130 RegisterComponentsMutator,
131
Paul Duffinc988c8e2020-04-29 18:27:14 +0100132 // Create an association between prebuilt modules and their corresponding source
133 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100134 //
135 // Must be run after defaults mutators to ensure that any modules created by
136 // a DefaultableHook can be either a prebuilt or a source module with a matching
137 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100138 RegisterPrebuiltsPreArchMutators,
139
Bob Badour37af0462021-01-07 03:34:31 +0000140 // Gather the licenses properties for all modules for use during expansion and enforcement.
141 //
142 // This must come after the defaults mutators to ensure that any licenses supplied
143 // in a defaults module has been successfully applied before the rules are gathered.
144 RegisterLicensesPropertyGatherer,
145
Paul Duffinaa4162e2020-05-05 11:35:43 +0100146 // Gather the visibility rules for all modules for us during visibility enforcement.
147 //
148 // This must come after the defaults mutators to ensure that any visibility supplied
149 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000150 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700151}
152
Colin Crossae4c6182017-09-15 17:33:55 -0700153func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700154 ctx.Transition("os", &osTransitionMutator{})
Cole Faust5b7635d2024-10-28 13:01:12 -0700155 ctx.BottomUp("image_begin", imageMutatorBeginMutator)
Jihoon Kang5402bbd2024-07-31 18:37:49 +0000156 ctx.Transition("image", &imageTransitionMutator{})
Colin Cross8bbc3d52024-09-11 15:33:54 -0700157 ctx.Transition("arch", &archTransitionMutator{})
Colin Crossae4c6182017-09-15 17:33:55 -0700158}
159
Colin Crosscec81712017-07-13 14:43:27 -0700160var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700161 registerArchMutator,
Colin Crossd8d8b852024-12-20 16:32:37 -0800162 RegisterPrebuiltsPreDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700163}
164
165var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800166 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700167 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000168 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000169 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100170 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700171 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700172}
Colin Cross1e676be2016-10-12 14:38:15 -0700173
Colin Crossf22fe412024-10-01 14:02:12 -0700174var postApex = []RegisterMutatorFunc{}
175
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000176var finalDeps = []RegisterMutatorFunc{}
177
Colin Cross1e676be2016-10-12 14:38:15 -0700178func PreArchMutators(f RegisterMutatorFunc) {
179 preArch = append(preArch, f)
180}
181
182func PreDepsMutators(f RegisterMutatorFunc) {
183 preDeps = append(preDeps, f)
184}
185
186func PostDepsMutators(f RegisterMutatorFunc) {
187 postDeps = append(postDeps, f)
188}
189
Colin Crossf22fe412024-10-01 14:02:12 -0700190func PostApexMutators(f RegisterMutatorFunc) {
191 postApex = append(postApex, f)
192}
193
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000194func FinalDepsMutators(f RegisterMutatorFunc) {
195 finalDeps = append(finalDeps, f)
196}
197
Chris Parsons637458d2023-09-19 20:09:00 +0000198type TopDownMutator func(TopDownMutatorContext)
199
200type TopDownMutatorContext interface {
Colin Crossb2388e32024-10-07 15:05:23 -0700201 BaseModuleContext
Chris Parsons637458d2023-09-19 20:09:00 +0000202}
203
Colin Cross25de6c32019-06-06 14:29:25 -0700204type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700205 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700206 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700207}
208
Colin Cross25de6c32019-06-06 14:29:25 -0700209type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700210
Colin Cross635c3b02016-05-18 15:37:25 -0700211type BottomUpMutatorContext interface {
Colin Crossb2388e32024-10-07 15:05:23 -0700212 BaseModuleContext
Colin Crossb63d7b32023-12-07 16:54:51 -0800213
214 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
215 // dependency (some entries may be nil).
216 //
Colin Cross8a962802024-10-09 15:29:27 -0700217 // This method will pause until the new dependencies have had the current mutator called on them.
Colin Crossd5c64312024-12-18 16:04:52 -0800218 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []Module
Colin Crossaabf6792017-11-29 00:27:14 -0800219
Colin Cross9f35c3d2020-09-16 19:04:41 -0700220 // AddReverseDependency adds a dependency from the destination to the given module.
221 // Does not affect the ordering of the current mutator pass, but will be ordered
222 // correctly for all future mutator passes. All reverse dependencies for a destination module are
223 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
Colin Crossb8533a82024-10-05 15:25:09 -0700224 // module's dependency list. May only be called by mutators that were marked with
225 // UsesReverseDependencies during registration.
Colin Crossaabf6792017-11-29 00:27:14 -0800226 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700227
Colin Cross9f35c3d2020-09-16 19:04:41 -0700228 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700229 // argument to select which variant of the dependency to use. It returns a slice of modules for
230 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
Usta Shresthac725f472022-01-11 02:44:21 -0500231 // all the non-local variations of the current module, plus the variations argument.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700232 //
Colin Cross8a962802024-10-09 15:29:27 -0700233 // This method will pause until the new dependencies have had the current mutator called on them.
Colin Crossd5c64312024-12-18 16:04:52 -0800234 AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700235
Colin Crossb8533a82024-10-05 15:25:09 -0700236 // AddReverseVariationDependency adds a dependency from the named module to the current
Cole Faustf4992e82024-09-30 15:16:18 -0700237 // module. The given variations will be added to the current module's varations, and then the
238 // result will be used to find the correct variation of the depending module, which must exist.
239 //
240 // Does not affect the ordering of the current mutator pass, but will be ordered
241 // correctly for all future mutator passes. All reverse dependencies for a destination module are
242 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
Colin Crossb8533a82024-10-05 15:25:09 -0700243 // module's dependency list. May only be called by mutators that were marked with
244 // UsesReverseDependencies during registration.
Cole Faustf4992e82024-09-30 15:16:18 -0700245 AddReverseVariationDependency([]blueprint.Variation, blueprint.DependencyTag, string)
246
Colin Cross9f35c3d2020-09-16 19:04:41 -0700247 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700248 // variations argument to select which variant of the dependency to use. It returns a slice of
249 // modules for each dependency (some entries may be nil). A variant of the dependency must
250 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700251 // For any unspecified variation the first variant will be used.
252 //
253 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
254 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700255 //
Colin Cross8a962802024-10-09 15:29:27 -0700256 // This method will pause until the new dependencies have had the current mutator called on them.
Colin Crossd5c64312024-12-18 16:04:52 -0800257 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700258
Colin Cross86771322024-05-01 14:19:51 -0700259 // ReplaceDependencies finds all the variants of the module with the specified name, then
260 // replaces all dependencies onto those variants with the current variant of this module.
Colin Crossb8533a82024-10-05 15:25:09 -0700261 // Replacements don't take effect until after the mutator pass is finished. May only
262 // be called by mutators that were marked with UsesReplaceDependencies during registration.
Colin Crossaabf6792017-11-29 00:27:14 -0800263 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700264
Colin Cross86771322024-05-01 14:19:51 -0700265 // ReplaceDependenciesIf finds all the variants of the module with the specified name, then
266 // replaces all dependencies onto those variants with the current variant of this module
267 // as long as the supplied predicate returns true.
Colin Crossb8533a82024-10-05 15:25:09 -0700268 // Replacements don't take effect until after the mutator pass is finished. May only
269 // be called by mutators that were marked with UsesReplaceDependencies during registration.
Paul Duffin80342d72020-06-26 22:08:43 +0100270 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Crossb2388e32024-10-07 15:05:23 -0700271
272 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
Colin Crossb8533a82024-10-05 15:25:09 -0700273 // AddDependency or OtherModuleName until after this mutator pass is complete. May only be called
274 // by mutators that were marked with UsesRename during registration.
Colin Crossb2388e32024-10-07 15:05:23 -0700275 Rename(name string)
276
277 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
Colin Crossb8533a82024-10-05 15:25:09 -0700278 // the specified property structs to it as if the properties were set in a blueprint file. May only
279 // be called by mutators that were marked with UsesCreateModule during registration.
Colin Crossb2388e32024-10-07 15:05:23 -0700280 CreateModule(ModuleFactory, ...interface{}) Module
Colin Cross6362e272015-10-29 15:25:03 -0700281}
282
Colin Cross984223f2024-02-01 17:10:23 -0800283// An outgoingTransitionContextImpl and incomingTransitionContextImpl is created for every dependency of every module
284// for each transition mutator. bottomUpMutatorContext and topDownMutatorContext are created once for every module
285// for every BottomUp or TopDown mutator. Use a global pool for each to avoid reallocating every time.
286var (
287 outgoingTransitionContextPool = sync.Pool{
288 New: func() any { return &outgoingTransitionContextImpl{} },
289 }
290 incomingTransitionContextPool = sync.Pool{
291 New: func() any { return &incomingTransitionContextImpl{} },
292 }
293 bottomUpMutatorContextPool = sync.Pool{
294 New: func() any { return &bottomUpMutatorContext{} },
295 }
296
297 topDownMutatorContextPool = sync.Pool{
298 New: func() any { return &topDownMutatorContext{} },
299 }
300)
301
Colin Cross25de6c32019-06-06 14:29:25 -0700302type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700303 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700304 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400305 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700306}
307
Colin Cross984223f2024-02-01 17:10:23 -0800308// callers must immediately follow the call to this function with defer bottomUpMutatorContextPool.Put(mctx).
Colin Cross617b88a2020-08-24 18:04:09 -0700309func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Colin Crossb63d7b32023-12-07 16:54:51 -0800310 finalPhase bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700311
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400312 moduleContext := a.base().baseModuleContextFactory(ctx)
Colin Cross984223f2024-02-01 17:10:23 -0800313 mctx := bottomUpMutatorContextPool.Get().(*bottomUpMutatorContext)
314 *mctx = bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400315 bp: ctx,
Cole Faust0abd4b42023-01-10 10:49:18 -0800316 baseModuleContext: moduleContext,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400317 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700318 }
Colin Cross984223f2024-02-01 17:10:23 -0800319 return mctx
Colin Cross617b88a2020-08-24 18:04:09 -0700320}
321
Colin Cross25de6c32019-06-06 14:29:25 -0700322func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000323 finalPhase := x.finalPhase
Colin Cross798bfce2016-10-12 14:28:16 -0700324 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700325 if a, ok := ctx.Module().(Module); ok {
Colin Cross984223f2024-02-01 17:10:23 -0800326 mctx := bottomUpMutatorContextFactory(ctx, a, finalPhase)
327 defer bottomUpMutatorContextPool.Put(mctx)
328 m(mctx)
Colin Cross6362e272015-10-29 15:25:03 -0700329 }
Colin Cross798bfce2016-10-12 14:28:16 -0700330 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500331 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700332 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700333 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700334}
335
Colin Cross617b88a2020-08-24 18:04:09 -0700336func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
337 mutator := &mutator{name: name, bottomUpMutator: m}
338 x.mutators = append(x.mutators, mutator)
339 return mutator
340}
341
Colin Cross90d7df92025-01-31 11:29:33 -0800342func (x *registerMutatorsContext) Transition(name string, m VariationTransitionMutator) TransitionMutatorHandle {
Colin Crossc6f3f022025-01-31 11:16:26 -0800343 atm := &androidTransitionMutatorAdapter{
Colin Crossb63d7b32023-12-07 16:54:51 -0800344 finalPhase: x.finalPhase,
Colin Crossc6f3f022025-01-31 11:16:26 -0800345 mutator: variationTransitionMutatorAdapter{m},
Colin Crossd67425d2024-04-15 15:17:05 -0700346 name: name,
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200347 }
348 mutator := &mutator{
349 name: name,
Cole Faustfc7a4112024-11-04 15:07:12 -0800350 transitionMutator: atm,
351 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200352 x.mutators = append(x.mutators, mutator)
Cole Faustfc7a4112024-11-04 15:07:12 -0800353 return mutator
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200354}
355
Colin Cross90d7df92025-01-31 11:29:33 -0800356func (x *registerMutatorsContext) InfoBasedTransition(name string, m androidTransitionMutator) TransitionMutatorHandle {
357 atm := &androidTransitionMutatorAdapter{
358 finalPhase: x.finalPhase,
359 mutator: m,
360 name: name,
361 }
362 mutator := &mutator{
363 name: name,
364 transitionMutator: atm,
365 }
366 x.mutators = append(x.mutators, mutator)
367 return mutator
368}
369
Liz Kammer356f7d42021-01-26 09:18:53 -0500370func (x *registerMutatorsContext) mutatorName(name string) string {
Liz Kammer356f7d42021-01-26 09:18:53 -0500371 return name
372}
373
Colin Cross25de6c32019-06-06 14:29:25 -0700374func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700375 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700376 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400377 moduleContext := a.base().baseModuleContextFactory(ctx)
Colin Cross984223f2024-02-01 17:10:23 -0800378 actx := topDownMutatorContextPool.Get().(*topDownMutatorContext)
379 defer topDownMutatorContextPool.Put(actx)
380 *actx = topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700381 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400382 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700383 }
Colin Cross798bfce2016-10-12 14:28:16 -0700384 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700385 }
Colin Cross798bfce2016-10-12 14:28:16 -0700386 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500387 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700388 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700389 return mutator
390}
391
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000392func (mutator *mutator) componentName() string {
393 return mutator.name
394}
395
396func (mutator *mutator) register(ctx *Context) {
397 blueprintCtx := ctx.Context
398 var handle blueprint.MutatorHandle
399 if mutator.bottomUpMutator != nil {
400 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
401 } else if mutator.topDownMutator != nil {
402 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200403 } else if mutator.transitionMutator != nil {
Cole Faustfc7a4112024-11-04 15:07:12 -0800404 handle := blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
405 if mutator.neverFar {
406 handle.NeverFar()
407 }
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000408 }
Colin Crossb8533a82024-10-05 15:25:09 -0700409
410 // Forward booleans set on the MutatorHandle to the blueprint.MutatorHandle.
Colin Crossb8533a82024-10-05 15:25:09 -0700411 if mutator.usesRename {
412 handle.UsesRename()
413 }
414 if mutator.usesReverseDependencies {
415 handle.UsesReverseDependencies()
416 }
417 if mutator.usesReplaceDependencies {
418 handle.UsesReplaceDependencies()
419 }
420 if mutator.usesCreateModule {
421 handle.UsesCreateModule()
422 }
423 if mutator.mutatesDependencies {
424 handle.MutatesDependencies()
425 }
426 if mutator.mutatesGlobalState {
427 handle.MutatesGlobalState()
428 }
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000429}
430
Colin Cross798bfce2016-10-12 14:28:16 -0700431type MutatorHandle interface {
Colin Crossb8533a82024-10-05 15:25:09 -0700432 // Parallel sets the mutator to visit modules in parallel while maintaining ordering. Calling any
433 // method on the mutator context is thread-safe, but the mutator must handle synchronization
434 // for any modifications to global state or any modules outside the one it was invoked on.
Colin Cross8a962802024-10-09 15:29:27 -0700435 // Deprecated: all Mutators are parallel by default.
Colin Cross798bfce2016-10-12 14:28:16 -0700436 Parallel() MutatorHandle
Colin Crossb8533a82024-10-05 15:25:09 -0700437
438 // UsesRename marks the mutator as using the BottomUpMutatorContext.Rename method, which prevents
439 // coalescing adjacent mutators into a single mutator pass.
440 UsesRename() MutatorHandle
441
442 // UsesReverseDependencies marks the mutator as using the BottomUpMutatorContext.AddReverseDependency
443 // method, which prevents coalescing adjacent mutators into a single mutator pass.
444 UsesReverseDependencies() MutatorHandle
445
446 // UsesReplaceDependencies marks the mutator as using the BottomUpMutatorContext.ReplaceDependencies
447 // method, which prevents coalescing adjacent mutators into a single mutator pass.
448 UsesReplaceDependencies() MutatorHandle
449
450 // UsesCreateModule marks the mutator as using the BottomUpMutatorContext.CreateModule method,
451 // which prevents coalescing adjacent mutators into a single mutator pass.
452 UsesCreateModule() MutatorHandle
453
454 // MutatesDependencies marks the mutator as modifying properties in dependencies, which prevents
455 // coalescing adjacent mutators into a single mutator pass.
456 MutatesDependencies() MutatorHandle
457
458 // MutatesGlobalState marks the mutator as modifying global state, which prevents coalescing
459 // adjacent mutators into a single mutator pass.
460 MutatesGlobalState() MutatorHandle
Colin Cross798bfce2016-10-12 14:28:16 -0700461}
462
Cole Faustfc7a4112024-11-04 15:07:12 -0800463type TransitionMutatorHandle interface {
464 // NeverFar causes the variations created by this mutator to never be ignored when adding
465 // far variation dependencies. Normally, far variation dependencies ignore all the variants
466 // of the source module, and only use the variants explicitly requested by the
467 // AddFarVariationDependencies call.
468 NeverFar() MutatorHandle
469}
470
Colin Cross798bfce2016-10-12 14:28:16 -0700471func (mutator *mutator) Parallel() MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700472 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700473}
Colin Cross1e676be2016-10-12 14:38:15 -0700474
Colin Crossb8533a82024-10-05 15:25:09 -0700475func (mutator *mutator) UsesRename() MutatorHandle {
476 mutator.usesRename = true
477 return mutator
478}
479
480func (mutator *mutator) UsesReverseDependencies() MutatorHandle {
481 mutator.usesReverseDependencies = true
482 return mutator
483}
484
485func (mutator *mutator) UsesReplaceDependencies() MutatorHandle {
486 mutator.usesReplaceDependencies = true
487 return mutator
488}
489
490func (mutator *mutator) UsesCreateModule() MutatorHandle {
491 mutator.usesCreateModule = true
492 return mutator
493}
494
495func (mutator *mutator) MutatesDependencies() MutatorHandle {
496 mutator.mutatesDependencies = true
497 return mutator
498}
499
500func (mutator *mutator) MutatesGlobalState() MutatorHandle {
501 mutator.mutatesGlobalState = true
502 return mutator
503}
504
Cole Faustfc7a4112024-11-04 15:07:12 -0800505func (mutator *mutator) NeverFar() MutatorHandle {
506 mutator.neverFar = true
507 return mutator
508}
509
Paul Duffin44f1d842020-06-26 20:17:02 +0100510func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -0700511 ctx.BottomUp("component-deps", componentDepsMutator)
Paul Duffin44f1d842020-06-26 20:17:02 +0100512}
513
514// A special mutator that runs just prior to the deps mutator to allow the dependencies
515// on component modules to be added so that they can depend directly on a prebuilt
516// module.
517func componentDepsMutator(ctx BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -0700518 ctx.Module().ComponentDepsMutator(ctx)
Paul Duffin44f1d842020-06-26 20:17:02 +0100519}
520
Colin Cross1e676be2016-10-12 14:38:15 -0700521func depsMutator(ctx BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -0700522 if m := ctx.Module(); m.Enabled(ctx) {
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800523 m.base().baseDepsMutator(ctx)
Colin Cross1e676be2016-10-12 14:38:15 -0700524 m.DepsMutator(ctx)
525 }
526}
Colin Crossd11fcda2017-10-23 17:59:01 -0700527
Liz Kammer356f7d42021-01-26 09:18:53 -0500528func registerDepsMutator(ctx RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -0700529 ctx.BottomUp("deps", depsMutator).UsesReverseDependencies()
Liz Kammer356f7d42021-01-26 09:18:53 -0500530}
531
Colin Crossdc35e212019-06-06 16:13:11 -0700532// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
533// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
534// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
535// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
536// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
537
Colin Crossdc35e212019-06-06 16:13:11 -0700538func (b *bottomUpMutatorContext) Rename(name string) {
539 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700540 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700541}
542
Colin Crossd5c64312024-12-18 16:04:52 -0800543func (b *bottomUpMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) Module {
544 return bpModuleToModule(b.bp.CreateModule(factory, name, props...))
Colin Crossda279cf2024-09-17 14:25:45 -0700545}
546
Colin Crossd5c64312024-12-18 16:04:52 -0800547func (b *bottomUpMutatorContext) createModuleInDirectory(factory blueprint.ModuleFactory, name string, _ string, props ...interface{}) Module {
Jihoon Kang222874d2024-10-23 23:38:05 +0000548 panic("createModuleInDirectory is not implemented for bottomUpMutatorContext")
549}
550
Colin Crossda279cf2024-09-17 14:25:45 -0700551func (b *bottomUpMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Jihoon Kang222874d2024-10-23 23:38:05 +0000552 return createModule(b, factory, "_bottomUpMutatorModule", doesNotSpecifyDirectory(), props...)
Colin Crossda279cf2024-09-17 14:25:45 -0700553}
554
Colin Crossd5c64312024-12-18 16:04:52 -0800555func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400556 if b.baseModuleContext.checkedMissingDeps() {
557 panic("Adding deps not allowed after checking for missing deps")
558 }
Colin Crossd5c64312024-12-18 16:04:52 -0800559 return bpModulesToModules(b.bp.AddDependency(module, tag, name...))
Colin Crossdc35e212019-06-06 16:13:11 -0700560}
561
562func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400563 if b.baseModuleContext.checkedMissingDeps() {
564 panic("Adding deps not allowed after checking for missing deps")
565 }
Colin Crossdc35e212019-06-06 16:13:11 -0700566 b.bp.AddReverseDependency(module, tag, name)
567}
Cole Faustf4992e82024-09-30 15:16:18 -0700568
569func (b *bottomUpMutatorContext) AddReverseVariationDependency(variations []blueprint.Variation, tag blueprint.DependencyTag, name string) {
570 if b.baseModuleContext.checkedMissingDeps() {
571 panic("Adding deps not allowed after checking for missing deps")
572 }
573 b.bp.AddReverseVariationDependency(variations, tag, name)
574}
575
Colin Crossdc35e212019-06-06 16:13:11 -0700576func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Crossd5c64312024-12-18 16:04:52 -0800577 names ...string) []Module {
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 Crossd5c64312024-12-18 16:04:52 -0800581 return bpModulesToModules(b.bp.AddVariationDependencies(variations, tag, names...))
Colin Crossdc35e212019-06-06 16:13:11 -0700582}
583
584func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Crossd5c64312024-12-18 16:04:52 -0800585 tag blueprint.DependencyTag, names ...string) []Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400586 if b.baseModuleContext.checkedMissingDeps() {
587 panic("Adding deps not allowed after checking for missing deps")
588 }
Colin Crossdc35e212019-06-06 16:13:11 -0700589
Colin Crossd5c64312024-12-18 16:04:52 -0800590 return bpModulesToModules(b.bp.AddFarVariationDependencies(variations, tag, names...))
Colin Crossdc35e212019-06-06 16:13:11 -0700591}
592
Colin Crossdc35e212019-06-06 16:13:11 -0700593func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400594 if b.baseModuleContext.checkedMissingDeps() {
595 panic("Adding deps not allowed after checking for missing deps")
596 }
Colin Crossdc35e212019-06-06 16:13:11 -0700597 b.bp.ReplaceDependencies(name)
598}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800599
Paul Duffin80342d72020-06-26 22:08:43 +0100600func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400601 if b.baseModuleContext.checkedMissingDeps() {
602 panic("Adding deps not allowed after checking for missing deps")
603 }
Paul Duffin80342d72020-06-26 22:08:43 +0100604 b.bp.ReplaceDependenciesIf(name, predicate)
605}
Colin Crossd5c64312024-12-18 16:04:52 -0800606
607func bpModulesToModules(bpModules []blueprint.Module) []Module {
608 modules := make([]Module, len(bpModules))
609 for i, bpModule := range bpModules {
610 modules[i] = bpModuleToModule(bpModule)
611 }
612 return modules
613}
614
615func bpModuleToModule(bpModule blueprint.Module) Module {
616 if bpModule != nil {
617 return bpModule.(Module)
618 }
619 return nil
620}