blob: 12861c074e723121bc47a3d6016ee0f307c2dc73 [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 Cross795c3772017-03-16 16:50:10 -070018 "github.com/google/blueprint"
Colin Cross0496b102025-02-11 14:25:22 -080019 "github.com/google/blueprint/pool"
Colin Cross795c3772017-03-16 16:50:10 -070020)
Colin Cross6362e272015-10-29 15:25:03 -070021
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070022// Phases:
23// run Pre-arch mutators
24// run archMutator
25// run Pre-deps mutators
26// run depsMutator
27// run PostDeps mutators
Colin Cross1e954b62024-09-13 13:50:00 -070028// run FinalDeps mutators (TransitionMutators disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070029// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070030
Paul Duffinc05b0342021-03-06 13:28:13 +000031// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
32// with the InitRegistrationContext and will be used at runtime.
33func collateGloballyRegisteredMutators() sortableComponents {
Colin Crossf22fe412024-10-01 14:02:12 -070034 return collateRegisteredMutators(preArch, preDeps, postDeps, postApex, finalDeps)
Paul Duffinc05b0342021-03-06 13:28:13 +000035}
36
37// collateRegisteredMutators constructs a single list of mutators from the separate lists.
Colin Crossf22fe412024-10-01 14:02:12 -070038func collateRegisteredMutators(preArch, preDeps, postDeps, postApex, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070039 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080040
41 register := func(funcs []RegisterMutatorFunc) {
42 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070043 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080044 }
45 }
46
Colin Crosscec81712017-07-13 14:43:27 -070047 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080048
Colin Crosscec81712017-07-13 14:43:27 -070049 register(preDeps)
50
Liz Kammer356f7d42021-01-26 09:18:53 -050051 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070052
53 register(postDeps)
54
Colin Crossf22fe412024-10-01 14:02:12 -070055 register(postApex)
56
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000057 mctx.finalPhase = true
58 register(finalDeps)
59
Paul Duffinc05b0342021-03-06 13:28:13 +000060 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -070061}
62
63type registerMutatorsContext struct {
Colin Crossb63d7b32023-12-07 16:54:51 -080064 mutators sortableComponents
65 finalPhase bool
Colin Cross795c3772017-03-16 16:50:10 -070066}
Colin Cross1e676be2016-10-12 14:38:15 -070067
68type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -070069 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -070070 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Colin Cross90d7df92025-01-31 11:29:33 -080071 Transition(name string, m VariationTransitionMutator) TransitionMutatorHandle
72 InfoBasedTransition(name string, m androidTransitionMutator) TransitionMutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -070073}
74
75type RegisterMutatorFunc func(RegisterMutatorsContext)
76
Colin Crosscec81712017-07-13 14:43:27 -070077var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -080078 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +010079
Paul Duffinaa4162e2020-05-05 11:35:43 +010080 // Check the visibility rules are valid.
81 //
82 // This must run after the package renamer mutators so that any issues found during
83 // validation of the package's default_visibility property are reported using the
84 // correct package name and not the synthetic name.
85 //
86 // This must also be run before defaults mutators as the rules for validation are
87 // different before checking the rules than they are afterwards. e.g.
88 // visibility: ["//visibility:private", "//visibility:public"]
89 // would be invalid if specified in a module definition but is valid if it results
90 // from something like this:
91 //
92 // defaults {
93 // name: "defaults",
94 // // Be inaccessible outside a package by default.
95 // visibility: ["//visibility:private"]
96 // }
97 //
98 // defaultable_module {
99 // name: "defaultable_module",
100 // defaults: ["defaults"],
101 // // Override the default.
102 // visibility: ["//visibility:public"]
103 // }
104 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000105 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100106
Bob Badour37af0462021-01-07 03:34:31 +0000107 // Record the default_applicable_licenses for each package.
108 //
109 // This must run before the defaults so that defaults modules can pick up the package default.
110 RegisterLicensesPackageMapper,
111
Paul Duffinaa4162e2020-05-05 11:35:43 +0100112 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100113 //
114 // Any mutators that are added before this will not see any modules created by
115 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700116 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100117
Paul Duffin44f1d842020-06-26 20:17:02 +0100118 // Add dependencies on any components so that any component references can be
119 // resolved within the deps mutator.
120 //
121 // Must be run after defaults so it can be used to create dependencies on the
122 // component modules that are creating in a DefaultableHook.
123 //
124 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
125 // renamed. That is so that if a module creates components using a prebuilt module
126 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
127 // the prebuilt module and not the source module.
128 RegisterComponentsMutator,
129
Paul Duffinc988c8e2020-04-29 18:27:14 +0100130 // Create an association between prebuilt modules and their corresponding source
131 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100132 //
133 // Must be run after defaults mutators to ensure that any modules created by
134 // a DefaultableHook can be either a prebuilt or a source module with a matching
135 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100136 RegisterPrebuiltsPreArchMutators,
137
Bob Badour37af0462021-01-07 03:34:31 +0000138 // Gather the licenses properties for all modules for use during expansion and enforcement.
139 //
140 // This must come after the defaults mutators to ensure that any licenses supplied
141 // in a defaults module has been successfully applied before the rules are gathered.
142 RegisterLicensesPropertyGatherer,
143
Paul Duffinaa4162e2020-05-05 11:35:43 +0100144 // Gather the visibility rules for all modules for us during visibility enforcement.
145 //
146 // This must come after the defaults mutators to ensure that any visibility supplied
147 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000148 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700149}
150
Colin Crossae4c6182017-09-15 17:33:55 -0700151func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700152 ctx.Transition("os", &osTransitionMutator{})
Cole Faust5b7635d2024-10-28 13:01:12 -0700153 ctx.BottomUp("image_begin", imageMutatorBeginMutator)
Jihoon Kang5402bbd2024-07-31 18:37:49 +0000154 ctx.Transition("image", &imageTransitionMutator{})
Colin Cross8bbc3d52024-09-11 15:33:54 -0700155 ctx.Transition("arch", &archTransitionMutator{})
Colin Crossae4c6182017-09-15 17:33:55 -0700156}
157
Colin Crosscec81712017-07-13 14:43:27 -0700158var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700159 registerArchMutator,
Colin Crossd8d8b852024-12-20 16:32:37 -0800160 RegisterPrebuiltsPreDepsMutators,
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
Colin Cross25de6c32019-06-06 14:29:25 -0700196type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700197
Colin Cross635c3b02016-05-18 15:37:25 -0700198type BottomUpMutatorContext interface {
Colin Crossb2388e32024-10-07 15:05:23 -0700199 BaseModuleContext
Colin Crossb63d7b32023-12-07 16:54:51 -0800200
201 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
202 // dependency (some entries may be nil).
203 //
Colin Cross8a962802024-10-09 15:29:27 -0700204 // This method will pause until the new dependencies have had the current mutator called on them.
Colin Crossd5c64312024-12-18 16:04:52 -0800205 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []Module
Colin Crossaabf6792017-11-29 00:27:14 -0800206
Colin Cross9f35c3d2020-09-16 19:04:41 -0700207 // AddReverseDependency adds a dependency from the destination to the given module.
208 // Does not affect the ordering of the current mutator pass, but will be ordered
209 // correctly for all future mutator passes. All reverse dependencies for a destination module are
210 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
Colin Crossb8533a82024-10-05 15:25:09 -0700211 // module's dependency list. May only be called by mutators that were marked with
212 // UsesReverseDependencies during registration.
Colin Crossaabf6792017-11-29 00:27:14 -0800213 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700214
Colin Cross9f35c3d2020-09-16 19:04:41 -0700215 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700216 // argument to select which variant of the dependency to use. It returns a slice of modules for
217 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
Usta Shresthac725f472022-01-11 02:44:21 -0500218 // all the non-local variations of the current module, plus the variations argument.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700219 //
Colin Cross8a962802024-10-09 15:29:27 -0700220 // This method will pause until the new dependencies have had the current mutator called on them.
Colin Crossd5c64312024-12-18 16:04:52 -0800221 AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700222
Colin Crossb8533a82024-10-05 15:25:09 -0700223 // AddReverseVariationDependency adds a dependency from the named module to the current
Cole Faustf4992e82024-09-30 15:16:18 -0700224 // module. The given variations will be added to the current module's varations, and then the
225 // result will be used to find the correct variation of the depending module, which must exist.
226 //
227 // Does not affect the ordering of the current mutator pass, but will be ordered
228 // correctly for all future mutator passes. All reverse dependencies for a destination module are
229 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
Colin Crossb8533a82024-10-05 15:25:09 -0700230 // module's dependency list. May only be called by mutators that were marked with
231 // UsesReverseDependencies during registration.
Cole Faustf4992e82024-09-30 15:16:18 -0700232 AddReverseVariationDependency([]blueprint.Variation, blueprint.DependencyTag, string)
233
Colin Cross9f35c3d2020-09-16 19:04:41 -0700234 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700235 // variations argument to select which variant of the dependency to use. It returns a slice of
236 // modules for each dependency (some entries may be nil). A variant of the dependency must
237 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700238 // For any unspecified variation the first variant will be used.
239 //
240 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
241 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700242 //
Colin Cross8a962802024-10-09 15:29:27 -0700243 // This method will pause until the new dependencies have had the current mutator called on them.
Colin Crossd5c64312024-12-18 16:04:52 -0800244 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700245
Colin Cross86771322024-05-01 14:19:51 -0700246 // ReplaceDependencies finds all the variants of the module with the specified name, then
247 // replaces all dependencies onto those variants with the current variant of this module.
Colin Crossb8533a82024-10-05 15:25:09 -0700248 // Replacements don't take effect until after the mutator pass is finished. May only
249 // be called by mutators that were marked with UsesReplaceDependencies during registration.
Colin Crossaabf6792017-11-29 00:27:14 -0800250 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700251
Colin Cross86771322024-05-01 14:19:51 -0700252 // ReplaceDependenciesIf finds all the variants of the module with the specified name, then
253 // replaces all dependencies onto those variants with the current variant of this module
254 // as long as the supplied predicate returns true.
Colin Crossb8533a82024-10-05 15:25:09 -0700255 // Replacements don't take effect until after the mutator pass is finished. May only
256 // be called by mutators that were marked with UsesReplaceDependencies during registration.
Paul Duffin80342d72020-06-26 22:08:43 +0100257 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Crossb2388e32024-10-07 15:05:23 -0700258
259 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
Colin Crossb8533a82024-10-05 15:25:09 -0700260 // AddDependency or OtherModuleName until after this mutator pass is complete. May only be called
261 // by mutators that were marked with UsesRename during registration.
Colin Crossb2388e32024-10-07 15:05:23 -0700262 Rename(name string)
263
264 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
Colin Crossb8533a82024-10-05 15:25:09 -0700265 // the specified property structs to it as if the properties were set in a blueprint file. May only
266 // be called by mutators that were marked with UsesCreateModule during registration.
Colin Crossb2388e32024-10-07 15:05:23 -0700267 CreateModule(ModuleFactory, ...interface{}) Module
Colin Cross6362e272015-10-29 15:25:03 -0700268}
269
Colin Cross984223f2024-02-01 17:10:23 -0800270// An outgoingTransitionContextImpl and incomingTransitionContextImpl is created for every dependency of every module
Colin Crossa6792772025-02-11 12:03:24 -0800271// for each transition mutator. bottomUpMutatorContext is created once for every module for every BottomUp mutator.
272// Use a global pool for each to avoid reallocating every time.
Colin Cross984223f2024-02-01 17:10:23 -0800273var (
Colin Cross0496b102025-02-11 14:25:22 -0800274 outgoingTransitionContextPool = pool.New[outgoingTransitionContextImpl]()
275 incomingTransitionContextPool = pool.New[incomingTransitionContextImpl]()
276 bottomUpMutatorContextPool = pool.New[bottomUpMutatorContext]()
Colin Cross984223f2024-02-01 17:10:23 -0800277)
278
Colin Cross25de6c32019-06-06 14:29:25 -0700279type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700280 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700281 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400282 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700283}
284
Colin Cross984223f2024-02-01 17:10:23 -0800285// callers must immediately follow the call to this function with defer bottomUpMutatorContextPool.Put(mctx).
Colin Cross617b88a2020-08-24 18:04:09 -0700286func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Colin Cross0496b102025-02-11 14:25:22 -0800287 finalPhase bool) *bottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700288
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400289 moduleContext := a.base().baseModuleContextFactory(ctx)
Colin Cross0496b102025-02-11 14:25:22 -0800290 mctx := bottomUpMutatorContextPool.Get()
Colin Cross984223f2024-02-01 17:10:23 -0800291 *mctx = bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400292 bp: ctx,
Cole Faust0abd4b42023-01-10 10:49:18 -0800293 baseModuleContext: moduleContext,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400294 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700295 }
Colin Cross984223f2024-02-01 17:10:23 -0800296 return mctx
Colin Cross617b88a2020-08-24 18:04:09 -0700297}
298
Colin Cross25de6c32019-06-06 14:29:25 -0700299func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000300 finalPhase := x.finalPhase
Colin Cross798bfce2016-10-12 14:28:16 -0700301 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700302 if a, ok := ctx.Module().(Module); ok {
Colin Cross984223f2024-02-01 17:10:23 -0800303 mctx := bottomUpMutatorContextFactory(ctx, a, finalPhase)
304 defer bottomUpMutatorContextPool.Put(mctx)
305 m(mctx)
Colin Cross6362e272015-10-29 15:25:03 -0700306 }
Colin Cross798bfce2016-10-12 14:28:16 -0700307 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500308 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700309 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700310 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700311}
312
Colin Cross617b88a2020-08-24 18:04:09 -0700313func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
314 mutator := &mutator{name: name, bottomUpMutator: m}
315 x.mutators = append(x.mutators, mutator)
316 return mutator
317}
318
Colin Cross90d7df92025-01-31 11:29:33 -0800319func (x *registerMutatorsContext) Transition(name string, m VariationTransitionMutator) TransitionMutatorHandle {
Colin Crossc6f3f022025-01-31 11:16:26 -0800320 atm := &androidTransitionMutatorAdapter{
Colin Crossb63d7b32023-12-07 16:54:51 -0800321 finalPhase: x.finalPhase,
Colin Crossc6f3f022025-01-31 11:16:26 -0800322 mutator: variationTransitionMutatorAdapter{m},
Colin Crossd67425d2024-04-15 15:17:05 -0700323 name: name,
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200324 }
325 mutator := &mutator{
326 name: name,
Cole Faustfc7a4112024-11-04 15:07:12 -0800327 transitionMutator: atm,
328 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200329 x.mutators = append(x.mutators, mutator)
Cole Faustfc7a4112024-11-04 15:07:12 -0800330 return mutator
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200331}
332
Colin Cross90d7df92025-01-31 11:29:33 -0800333func (x *registerMutatorsContext) InfoBasedTransition(name string, m androidTransitionMutator) TransitionMutatorHandle {
334 atm := &androidTransitionMutatorAdapter{
335 finalPhase: x.finalPhase,
336 mutator: m,
337 name: name,
338 }
339 mutator := &mutator{
340 name: name,
341 transitionMutator: atm,
342 }
343 x.mutators = append(x.mutators, mutator)
344 return mutator
345}
346
Liz Kammer356f7d42021-01-26 09:18:53 -0500347func (x *registerMutatorsContext) mutatorName(name string) string {
Liz Kammer356f7d42021-01-26 09:18:53 -0500348 return name
349}
350
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000351func (mutator *mutator) componentName() string {
352 return mutator.name
353}
354
355func (mutator *mutator) register(ctx *Context) {
356 blueprintCtx := ctx.Context
357 var handle blueprint.MutatorHandle
358 if mutator.bottomUpMutator != nil {
359 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200360 } else if mutator.transitionMutator != nil {
Cole Faustfc7a4112024-11-04 15:07:12 -0800361 handle := blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
362 if mutator.neverFar {
363 handle.NeverFar()
364 }
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000365 }
Colin Crossb8533a82024-10-05 15:25:09 -0700366
367 // Forward booleans set on the MutatorHandle to the blueprint.MutatorHandle.
Colin Crossb8533a82024-10-05 15:25:09 -0700368 if mutator.usesRename {
369 handle.UsesRename()
370 }
371 if mutator.usesReverseDependencies {
372 handle.UsesReverseDependencies()
373 }
374 if mutator.usesReplaceDependencies {
375 handle.UsesReplaceDependencies()
376 }
377 if mutator.usesCreateModule {
378 handle.UsesCreateModule()
379 }
380 if mutator.mutatesDependencies {
381 handle.MutatesDependencies()
382 }
383 if mutator.mutatesGlobalState {
384 handle.MutatesGlobalState()
385 }
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000386}
387
Colin Cross798bfce2016-10-12 14:28:16 -0700388type MutatorHandle interface {
Colin Crossb8533a82024-10-05 15:25:09 -0700389 // Parallel sets the mutator to visit modules in parallel while maintaining ordering. Calling any
390 // method on the mutator context is thread-safe, but the mutator must handle synchronization
391 // for any modifications to global state or any modules outside the one it was invoked on.
Colin Cross8a962802024-10-09 15:29:27 -0700392 // Deprecated: all Mutators are parallel by default.
Colin Cross798bfce2016-10-12 14:28:16 -0700393 Parallel() MutatorHandle
Colin Crossb8533a82024-10-05 15:25:09 -0700394
395 // UsesRename marks the mutator as using the BottomUpMutatorContext.Rename method, which prevents
396 // coalescing adjacent mutators into a single mutator pass.
397 UsesRename() MutatorHandle
398
399 // UsesReverseDependencies marks the mutator as using the BottomUpMutatorContext.AddReverseDependency
400 // method, which prevents coalescing adjacent mutators into a single mutator pass.
401 UsesReverseDependencies() MutatorHandle
402
403 // UsesReplaceDependencies marks the mutator as using the BottomUpMutatorContext.ReplaceDependencies
404 // method, which prevents coalescing adjacent mutators into a single mutator pass.
405 UsesReplaceDependencies() MutatorHandle
406
407 // UsesCreateModule marks the mutator as using the BottomUpMutatorContext.CreateModule method,
408 // which prevents coalescing adjacent mutators into a single mutator pass.
409 UsesCreateModule() MutatorHandle
410
411 // MutatesDependencies marks the mutator as modifying properties in dependencies, which prevents
412 // coalescing adjacent mutators into a single mutator pass.
413 MutatesDependencies() MutatorHandle
414
415 // MutatesGlobalState marks the mutator as modifying global state, which prevents coalescing
416 // adjacent mutators into a single mutator pass.
417 MutatesGlobalState() MutatorHandle
Colin Cross798bfce2016-10-12 14:28:16 -0700418}
419
Cole Faustfc7a4112024-11-04 15:07:12 -0800420type TransitionMutatorHandle interface {
421 // NeverFar causes the variations created by this mutator to never be ignored when adding
422 // far variation dependencies. Normally, far variation dependencies ignore all the variants
423 // of the source module, and only use the variants explicitly requested by the
424 // AddFarVariationDependencies call.
425 NeverFar() MutatorHandle
426}
427
Colin Cross798bfce2016-10-12 14:28:16 -0700428func (mutator *mutator) Parallel() MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700429 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700430}
Colin Cross1e676be2016-10-12 14:38:15 -0700431
Colin Crossb8533a82024-10-05 15:25:09 -0700432func (mutator *mutator) UsesRename() MutatorHandle {
433 mutator.usesRename = true
434 return mutator
435}
436
437func (mutator *mutator) UsesReverseDependencies() MutatorHandle {
438 mutator.usesReverseDependencies = true
439 return mutator
440}
441
442func (mutator *mutator) UsesReplaceDependencies() MutatorHandle {
443 mutator.usesReplaceDependencies = true
444 return mutator
445}
446
447func (mutator *mutator) UsesCreateModule() MutatorHandle {
448 mutator.usesCreateModule = true
449 return mutator
450}
451
452func (mutator *mutator) MutatesDependencies() MutatorHandle {
453 mutator.mutatesDependencies = true
454 return mutator
455}
456
457func (mutator *mutator) MutatesGlobalState() MutatorHandle {
458 mutator.mutatesGlobalState = true
459 return mutator
460}
461
Cole Faustfc7a4112024-11-04 15:07:12 -0800462func (mutator *mutator) NeverFar() MutatorHandle {
463 mutator.neverFar = true
464 return mutator
465}
466
Paul Duffin44f1d842020-06-26 20:17:02 +0100467func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -0700468 ctx.BottomUp("component-deps", componentDepsMutator)
Paul Duffin44f1d842020-06-26 20:17:02 +0100469}
470
471// A special mutator that runs just prior to the deps mutator to allow the dependencies
472// on component modules to be added so that they can depend directly on a prebuilt
473// module.
474func componentDepsMutator(ctx BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -0700475 ctx.Module().ComponentDepsMutator(ctx)
Paul Duffin44f1d842020-06-26 20:17:02 +0100476}
477
Colin Cross1e676be2016-10-12 14:38:15 -0700478func depsMutator(ctx BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -0700479 if m := ctx.Module(); m.Enabled(ctx) {
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800480 m.base().baseDepsMutator(ctx)
Colin Cross1e676be2016-10-12 14:38:15 -0700481 m.DepsMutator(ctx)
482 }
483}
Colin Crossd11fcda2017-10-23 17:59:01 -0700484
Liz Kammer356f7d42021-01-26 09:18:53 -0500485func registerDepsMutator(ctx RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -0700486 ctx.BottomUp("deps", depsMutator).UsesReverseDependencies()
Liz Kammer356f7d42021-01-26 09:18:53 -0500487}
488
Colin Crossa6792772025-02-11 12:03:24 -0800489// android.bottomUpMutatorContext either has to embed blueprint.BottomUpMutatorContext, in which case every method that
Colin Crossdc35e212019-06-06 16:13:11 -0700490// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
Colin Crossa6792772025-02-11 12:03:24 -0800491// ambiguous method errors, or it has to store a blueprint.BottomUpMutatorContext non-embedded, in which case every
Colin Crossdc35e212019-06-06 16:13:11 -0700492// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
Colin Crossa6792772025-02-11 12:03:24 -0800493// methods forward to the identical blueprint versions for bottomUpMutatorContext.
Colin Crossdc35e212019-06-06 16:13:11 -0700494
Colin Crossdc35e212019-06-06 16:13:11 -0700495func (b *bottomUpMutatorContext) Rename(name string) {
496 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700497 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700498}
499
Colin Crossd5c64312024-12-18 16:04:52 -0800500func (b *bottomUpMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) Module {
501 return bpModuleToModule(b.bp.CreateModule(factory, name, props...))
Colin Crossda279cf2024-09-17 14:25:45 -0700502}
503
Colin Crossd5c64312024-12-18 16:04:52 -0800504func (b *bottomUpMutatorContext) createModuleInDirectory(factory blueprint.ModuleFactory, name string, _ string, props ...interface{}) Module {
Jihoon Kang222874d2024-10-23 23:38:05 +0000505 panic("createModuleInDirectory is not implemented for bottomUpMutatorContext")
506}
507
Colin Crossda279cf2024-09-17 14:25:45 -0700508func (b *bottomUpMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Jihoon Kang222874d2024-10-23 23:38:05 +0000509 return createModule(b, factory, "_bottomUpMutatorModule", doesNotSpecifyDirectory(), props...)
Colin Crossda279cf2024-09-17 14:25:45 -0700510}
511
Colin Crossd5c64312024-12-18 16:04:52 -0800512func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400513 if b.baseModuleContext.checkedMissingDeps() {
514 panic("Adding deps not allowed after checking for missing deps")
515 }
Colin Crossd5c64312024-12-18 16:04:52 -0800516 return bpModulesToModules(b.bp.AddDependency(module, tag, name...))
Colin Crossdc35e212019-06-06 16:13:11 -0700517}
518
519func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400520 if b.baseModuleContext.checkedMissingDeps() {
521 panic("Adding deps not allowed after checking for missing deps")
522 }
Colin Crossdc35e212019-06-06 16:13:11 -0700523 b.bp.AddReverseDependency(module, tag, name)
524}
Cole Faustf4992e82024-09-30 15:16:18 -0700525
526func (b *bottomUpMutatorContext) AddReverseVariationDependency(variations []blueprint.Variation, tag blueprint.DependencyTag, name string) {
527 if b.baseModuleContext.checkedMissingDeps() {
528 panic("Adding deps not allowed after checking for missing deps")
529 }
530 b.bp.AddReverseVariationDependency(variations, tag, name)
531}
532
Colin Crossdc35e212019-06-06 16:13:11 -0700533func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Crossd5c64312024-12-18 16:04:52 -0800534 names ...string) []Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400535 if b.baseModuleContext.checkedMissingDeps() {
536 panic("Adding deps not allowed after checking for missing deps")
537 }
Colin Crossd5c64312024-12-18 16:04:52 -0800538 return bpModulesToModules(b.bp.AddVariationDependencies(variations, tag, names...))
Colin Crossdc35e212019-06-06 16:13:11 -0700539}
540
541func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Crossd5c64312024-12-18 16:04:52 -0800542 tag blueprint.DependencyTag, names ...string) []Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400543 if b.baseModuleContext.checkedMissingDeps() {
544 panic("Adding deps not allowed after checking for missing deps")
545 }
Colin Crossdc35e212019-06-06 16:13:11 -0700546
Colin Crossd5c64312024-12-18 16:04:52 -0800547 return bpModulesToModules(b.bp.AddFarVariationDependencies(variations, tag, names...))
Colin Crossdc35e212019-06-06 16:13:11 -0700548}
549
Colin Crossdc35e212019-06-06 16:13:11 -0700550func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400551 if b.baseModuleContext.checkedMissingDeps() {
552 panic("Adding deps not allowed after checking for missing deps")
553 }
Colin Crossdc35e212019-06-06 16:13:11 -0700554 b.bp.ReplaceDependencies(name)
555}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800556
Paul Duffin80342d72020-06-26 22:08:43 +0100557func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400558 if b.baseModuleContext.checkedMissingDeps() {
559 panic("Adding deps not allowed after checking for missing deps")
560 }
Paul Duffin80342d72020-06-26 22:08:43 +0100561 b.bp.ReplaceDependenciesIf(name, predicate)
562}
Colin Crossd5c64312024-12-18 16:04:52 -0800563
564func bpModulesToModules(bpModules []blueprint.Module) []Module {
565 modules := make([]Module, len(bpModules))
566 for i, bpModule := range bpModules {
567 modules[i] = bpModuleToModule(bpModule)
568 }
569 return modules
570}
571
572func bpModuleToModule(bpModule blueprint.Module) Module {
573 if bpModule != nil {
574 return bpModule.(Module)
575 }
576 return nil
577}