blob: ebe03a4d441068ad94304e826b3b77b06180b32e [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
Lukacs T. Berki6c716762022-06-13 20:50:39 +020073 Transition(name string, m TransitionMutator)
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{})
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 Crosscec81712017-07-13 14:43:27 -0700160}
161
162var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800163 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700164 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000165 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000166 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100167 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700168 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700169}
Colin Cross1e676be2016-10-12 14:38:15 -0700170
Colin Crossf22fe412024-10-01 14:02:12 -0700171var postApex = []RegisterMutatorFunc{}
172
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000173var finalDeps = []RegisterMutatorFunc{}
174
Colin Cross1e676be2016-10-12 14:38:15 -0700175func PreArchMutators(f RegisterMutatorFunc) {
176 preArch = append(preArch, f)
177}
178
179func PreDepsMutators(f RegisterMutatorFunc) {
180 preDeps = append(preDeps, f)
181}
182
183func PostDepsMutators(f RegisterMutatorFunc) {
184 postDeps = append(postDeps, f)
185}
186
Colin Crossf22fe412024-10-01 14:02:12 -0700187func PostApexMutators(f RegisterMutatorFunc) {
188 postApex = append(postApex, f)
189}
190
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000191func FinalDepsMutators(f RegisterMutatorFunc) {
192 finalDeps = append(finalDeps, f)
193}
194
Chris Parsons637458d2023-09-19 20:09:00 +0000195type TopDownMutator func(TopDownMutatorContext)
196
197type TopDownMutatorContext interface {
Colin Crossb2388e32024-10-07 15:05:23 -0700198 BaseModuleContext
Chris Parsons637458d2023-09-19 20:09:00 +0000199}
200
Colin Cross25de6c32019-06-06 14:29:25 -0700201type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700202 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700203 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700204}
205
Colin Cross25de6c32019-06-06 14:29:25 -0700206type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700207
Colin Cross635c3b02016-05-18 15:37:25 -0700208type BottomUpMutatorContext interface {
Colin Crossb2388e32024-10-07 15:05:23 -0700209 BaseModuleContext
Colin Crossb63d7b32023-12-07 16:54:51 -0800210
211 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
212 // dependency (some entries may be nil).
213 //
214 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
215 // new dependencies have had the current mutator called on them. If the mutator is not
216 // parallel this method does not affect the ordering of the current mutator pass, but will
217 // be ordered correctly for all future mutator passes.
218 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.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
224 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800225 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700226
Colin Cross9f35c3d2020-09-16 19:04:41 -0700227 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700228 // argument to select which variant of the dependency to use. It returns a slice of modules for
229 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
Usta Shresthac725f472022-01-11 02:44:21 -0500230 // all the non-local variations of the current module, plus the variations argument.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700231 //
232 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
233 // new dependencies have had the current mutator called on them. If the mutator is not
234 // parallel this method does not affect the ordering of the current mutator pass, but will
235 // be ordered correctly for all future mutator passes.
Usta Shresthac725f472022-01-11 02:44:21 -0500236 AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700237
Cole Faustf4992e82024-09-30 15:16:18 -0700238 // AddReverseVariationDependencies adds a dependency from the named module to the current
239 // module. The given variations will be added to the current module's varations, and then the
240 // result will be used to find the correct variation of the depending module, which must exist.
241 //
242 // Does not affect the ordering of the current mutator pass, but will be ordered
243 // correctly for all future mutator passes. All reverse dependencies for a destination module are
244 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
245 // module's dependency list.
246 AddReverseVariationDependency([]blueprint.Variation, blueprint.DependencyTag, string)
247
Colin Cross9f35c3d2020-09-16 19:04:41 -0700248 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700249 // variations argument to select which variant of the dependency to use. It returns a slice of
250 // modules for each dependency (some entries may be nil). A variant of the dependency must
251 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700252 // For any unspecified variation the first variant will be used.
253 //
254 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
255 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700256 //
257 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
258 // new dependencies have had the current mutator called on them. If the mutator is not
259 // parallel this method does not affect the ordering of the current mutator pass, but will
260 // be ordered correctly for all future mutator passes.
261 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700262
Colin Cross86771322024-05-01 14:19:51 -0700263 // ReplaceDependencies 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 // Replacements don't take effect until after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800266 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700267
Colin Cross86771322024-05-01 14:19:51 -0700268 // ReplaceDependenciesIf finds all the variants of the module with the specified name, then
269 // replaces all dependencies onto those variants with the current variant of this module
270 // as long as the supplied predicate returns true.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700271 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100272 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Crossb2388e32024-10-07 15:05:23 -0700273
274 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
275 // AddDependency or OtherModuleName until after this mutator pass is complete.
276 Rename(name string)
277
278 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
279 // the specified property structs to it as if the properties were set in a blueprint file.
280 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
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200342type IncomingTransitionContext interface {
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800343 ArchModuleContext
Colin Crossaf333f52024-04-15 15:20:23 -0700344 ModuleProviderContext
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800345
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200346 // Module returns the target of the dependency edge for which the transition
347 // is being computed
348 Module() Module
349
350 // Config returns the configuration for the build.
351 Config() Config
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800352
353 DeviceConfig() DeviceConfig
Colin Crosse1a85552024-06-14 12:17:37 -0700354
355 // IsAddingDependency returns true if the transition is being called while adding a dependency
356 // after the transition mutator has already run, or false if it is being called when the transition
357 // mutator is running. This should be used sparingly, all uses will have to be removed in order
358 // to support creating variants on demand.
359 IsAddingDependency() bool
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200360}
361
362type OutgoingTransitionContext interface {
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800363 ArchModuleContext
Colin Crossaf333f52024-04-15 15:20:23 -0700364 ModuleProviderContext
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800365
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200366 // Module returns the target of the dependency edge for which the transition
367 // is being computed
368 Module() Module
369
370 // DepTag() Returns the dependency tag through which this dependency is
371 // reached
372 DepTag() blueprint.DependencyTag
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800373
374 // Config returns the configuration for the build.
375 Config() Config
376
377 DeviceConfig() DeviceConfig
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200378}
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200379
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800380// TransitionMutator implements a top-down mechanism where a module tells its
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200381// direct dependencies what variation they should be built in but the dependency
382// has the final say.
383//
384// When implementing a transition mutator, one needs to implement four methods:
385// - Split() that tells what variations a module has by itself
386// - OutgoingTransition() where a module tells what it wants from its
387// dependency
388// - IncomingTransition() where a module has the final say about its own
389// variation
390// - Mutate() that changes the state of a module depending on its variation
391//
392// That the effective variation of module B when depended on by module A is the
393// composition the outgoing transition of module A and the incoming transition
394// of module B.
395//
396// the outgoing transition should not take the properties of the dependency into
397// account, only those of the module that depends on it. For this reason, the
398// dependency is not even passed into it as an argument. Likewise, the incoming
399// transition should not take the properties of the depending module into
400// account and is thus not informed about it. This makes for a nice
401// decomposition of the decision logic.
402//
403// A given transition mutator only affects its own variation; other variations
404// stay unchanged along the dependency edges.
405//
406// Soong makes sure that all modules are created in the desired variations and
407// that dependency edges are set up correctly. This ensures that "missing
408// variation" errors do not happen and allows for more flexible changes in the
409// value of the variation among dependency edges (as oppposed to bottom-up
410// mutators where if module A in variation X depends on module B and module B
411// has that variation X, A must depend on variation X of B)
412//
413// The limited power of the context objects passed to individual mutators
414// methods also makes it more difficult to shoot oneself in the foot. Complete
415// safety is not guaranteed because no one prevents individual transition
416// mutators from mutating modules in illegal ways and for e.g. Split() or
417// Mutate() to run their own visitations of the transitive dependency of the
418// module and both of these are bad ideas, but it's better than no guardrails at
419// all.
420//
421// This model is pretty close to Bazel's configuration transitions. The mapping
422// between concepts in Soong and Bazel is as follows:
423// - Module == configured target
424// - Variant == configuration
425// - Variation name == configuration flag
426// - Variation == configuration flag value
427// - Outgoing transition == attribute transition
428// - Incoming transition == rule transition
429//
430// The Split() method does not have a Bazel equivalent and Bazel split
431// transitions do not have a Soong equivalent.
432//
433// Mutate() does not make sense in Bazel due to the different models of the
434// two systems: when creating new variations, Soong clones the old module and
435// thus some way is needed to change it state whereas Bazel creates each
436// configuration of a given configured target anew.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200437type TransitionMutator interface {
438 // Split returns the set of variations that should be created for a module no
439 // matter who depends on it. Used when Make depends on a particular variation
440 // or when the module knows its variations just based on information given to
441 // it in the Blueprint file. This method should not mutate the module it is
442 // called on.
443 Split(ctx BaseModuleContext) []string
444
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800445 // OutgoingTransition is called on a module to determine which variation it wants
446 // from its direct dependencies. The dependency itself can override this decision.
447 // This method should not mutate the module itself.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200448 OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string
449
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800450 // IncomingTransition is called on a module to determine which variation it should
451 // be in based on the variation modules that depend on it want. This gives the module
452 // a final say about its own variations. This method should not mutate the module
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200453 // itself.
454 IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string
455
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800456 // Mutate is called after a module was split into multiple variations on each variation.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200457 // It should not split the module any further but adding new dependencies is
458 // fine. Unlike all the other methods on TransitionMutator, this method is
459 // allowed to mutate the module.
460 Mutate(ctx BottomUpMutatorContext, variation string)
461}
462
463type androidTransitionMutator struct {
Colin Crossb63d7b32023-12-07 16:54:51 -0800464 finalPhase bool
465 mutator TransitionMutator
Colin Crossd67425d2024-04-15 15:17:05 -0700466 name string
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200467}
468
469func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []string {
Colin Crossd27205e2024-09-12 22:41:37 -0700470 if a.finalPhase {
471 panic("TransitionMutator not allowed in FinalDepsMutators")
472 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200473 if m, ok := ctx.Module().(Module); ok {
474 moduleContext := m.base().baseModuleContextFactory(ctx)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200475 return a.mutator.Split(&moduleContext)
476 } else {
477 return []string{""}
478 }
479}
480
481type outgoingTransitionContextImpl struct {
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800482 archModuleContext
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200483 bp blueprint.OutgoingTransitionContext
484}
485
486func (c *outgoingTransitionContextImpl) Module() Module {
487 return c.bp.Module().(Module)
488}
489
490func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag {
491 return c.bp.DepTag()
492}
493
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800494func (c *outgoingTransitionContextImpl) Config() Config {
495 return c.bp.Config().(Config)
496}
497
498func (c *outgoingTransitionContextImpl) DeviceConfig() DeviceConfig {
499 return DeviceConfig{c.bp.Config().(Config).deviceConfig}
500}
501
Colin Crossaf333f52024-04-15 15:20:23 -0700502func (c *outgoingTransitionContextImpl) provider(provider blueprint.AnyProviderKey) (any, bool) {
503 return c.bp.Provider(provider)
504}
505
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800506func (a *androidTransitionMutator) OutgoingTransition(bpctx blueprint.OutgoingTransitionContext, sourceVariation string) string {
507 if m, ok := bpctx.Module().(Module); ok {
Colin Cross984223f2024-02-01 17:10:23 -0800508 ctx := outgoingTransitionContextPool.Get().(*outgoingTransitionContextImpl)
509 defer outgoingTransitionContextPool.Put(ctx)
510 *ctx = outgoingTransitionContextImpl{
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800511 archModuleContext: m.base().archModuleContextFactory(bpctx),
512 bp: bpctx,
513 }
514 return a.mutator.OutgoingTransition(ctx, sourceVariation)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200515 } else {
516 return ""
517 }
518}
519
520type incomingTransitionContextImpl struct {
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800521 archModuleContext
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200522 bp blueprint.IncomingTransitionContext
523}
524
525func (c *incomingTransitionContextImpl) Module() Module {
526 return c.bp.Module().(Module)
527}
528
529func (c *incomingTransitionContextImpl) Config() Config {
530 return c.bp.Config().(Config)
531}
532
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800533func (c *incomingTransitionContextImpl) DeviceConfig() DeviceConfig {
534 return DeviceConfig{c.bp.Config().(Config).deviceConfig}
535}
536
Colin Crosse1a85552024-06-14 12:17:37 -0700537func (c *incomingTransitionContextImpl) IsAddingDependency() bool {
538 return c.bp.IsAddingDependency()
539}
540
Colin Crossaf333f52024-04-15 15:20:23 -0700541func (c *incomingTransitionContextImpl) provider(provider blueprint.AnyProviderKey) (any, bool) {
542 return c.bp.Provider(provider)
543}
544
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800545func (a *androidTransitionMutator) IncomingTransition(bpctx blueprint.IncomingTransitionContext, incomingVariation string) string {
546 if m, ok := bpctx.Module().(Module); ok {
Colin Cross984223f2024-02-01 17:10:23 -0800547 ctx := incomingTransitionContextPool.Get().(*incomingTransitionContextImpl)
548 defer incomingTransitionContextPool.Put(ctx)
549 *ctx = incomingTransitionContextImpl{
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800550 archModuleContext: m.base().archModuleContextFactory(bpctx),
551 bp: bpctx,
552 }
553 return a.mutator.IncomingTransition(ctx, incomingVariation)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200554 } else {
555 return ""
556 }
557}
558
559func (a *androidTransitionMutator) Mutate(ctx blueprint.BottomUpMutatorContext, variation string) {
560 if am, ok := ctx.Module().(Module); ok {
Colin Cross888046f2024-05-01 14:20:31 -0700561 if variation != "" {
562 // TODO: this should really be checking whether the TransitionMutator affected this module, not
563 // the empty variant, but TransitionMutator has no concept of skipping a module.
564 base := am.base()
565 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, a.name)
566 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variation)
567 }
568
Colin Cross984223f2024-02-01 17:10:23 -0800569 mctx := bottomUpMutatorContextFactory(ctx, am, a.finalPhase)
570 defer bottomUpMutatorContextPool.Put(mctx)
571 a.mutator.Mutate(mctx, variation)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200572 }
573}
574
575func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) {
576 atm := &androidTransitionMutator{
Colin Crossb63d7b32023-12-07 16:54:51 -0800577 finalPhase: x.finalPhase,
578 mutator: m,
Colin Crossd67425d2024-04-15 15:17:05 -0700579 name: name,
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200580 }
581 mutator := &mutator{
582 name: name,
583 transitionMutator: atm}
584 x.mutators = append(x.mutators, mutator)
585}
586
Liz Kammer356f7d42021-01-26 09:18:53 -0500587func (x *registerMutatorsContext) mutatorName(name string) string {
Liz Kammer356f7d42021-01-26 09:18:53 -0500588 return name
589}
590
Colin Cross25de6c32019-06-06 14:29:25 -0700591func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700592 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700593 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400594 moduleContext := a.base().baseModuleContextFactory(ctx)
Colin Cross984223f2024-02-01 17:10:23 -0800595 actx := topDownMutatorContextPool.Get().(*topDownMutatorContext)
596 defer topDownMutatorContextPool.Put(actx)
597 *actx = topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700598 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400599 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700600 }
Colin Cross798bfce2016-10-12 14:28:16 -0700601 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700602 }
Colin Cross798bfce2016-10-12 14:28:16 -0700603 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500604 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700605 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700606 return mutator
607}
608
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000609func (mutator *mutator) componentName() string {
610 return mutator.name
611}
612
613func (mutator *mutator) register(ctx *Context) {
614 blueprintCtx := ctx.Context
615 var handle blueprint.MutatorHandle
616 if mutator.bottomUpMutator != nil {
617 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
618 } else if mutator.topDownMutator != nil {
619 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200620 } else if mutator.transitionMutator != nil {
621 blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000622 }
623 if mutator.parallel {
624 handle.Parallel()
625 }
626}
627
Colin Cross798bfce2016-10-12 14:28:16 -0700628type MutatorHandle interface {
629 Parallel() MutatorHandle
630}
631
632func (mutator *mutator) Parallel() MutatorHandle {
633 mutator.parallel = true
634 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700635}
Colin Cross1e676be2016-10-12 14:38:15 -0700636
Paul Duffin44f1d842020-06-26 20:17:02 +0100637func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
638 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
639}
640
641// A special mutator that runs just prior to the deps mutator to allow the dependencies
642// on component modules to be added so that they can depend directly on a prebuilt
643// module.
644func componentDepsMutator(ctx BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -0700645 ctx.Module().ComponentDepsMutator(ctx)
Paul Duffin44f1d842020-06-26 20:17:02 +0100646}
647
Colin Cross1e676be2016-10-12 14:38:15 -0700648func depsMutator(ctx BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -0700649 if m := ctx.Module(); m.Enabled(ctx) {
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800650 m.base().baseDepsMutator(ctx)
Colin Cross1e676be2016-10-12 14:38:15 -0700651 m.DepsMutator(ctx)
652 }
653}
Colin Crossd11fcda2017-10-23 17:59:01 -0700654
Liz Kammer356f7d42021-01-26 09:18:53 -0500655func registerDepsMutator(ctx RegisterMutatorsContext) {
656 ctx.BottomUp("deps", depsMutator).Parallel()
657}
658
Colin Crossdc35e212019-06-06 16:13:11 -0700659// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
660// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
661// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
662// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
663// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
664
Colin Crossdc35e212019-06-06 16:13:11 -0700665func (b *bottomUpMutatorContext) Rename(name string) {
666 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700667 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700668}
669
Colin Crossda279cf2024-09-17 14:25:45 -0700670func (b *bottomUpMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
671 return b.bp.CreateModule(factory, name, props...)
672}
673
674func (b *bottomUpMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
675 return createModule(b, factory, "_bottomUpMutatorModule", props...)
676}
677
Colin Cross4f1dcb02020-09-16 18:45:04 -0700678func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400679 if b.baseModuleContext.checkedMissingDeps() {
680 panic("Adding deps not allowed after checking for missing deps")
681 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700682 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700683}
684
685func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400686 if b.baseModuleContext.checkedMissingDeps() {
687 panic("Adding deps not allowed after checking for missing deps")
688 }
Colin Crossdc35e212019-06-06 16:13:11 -0700689 b.bp.AddReverseDependency(module, tag, name)
690}
Cole Faustf4992e82024-09-30 15:16:18 -0700691
692func (b *bottomUpMutatorContext) AddReverseVariationDependency(variations []blueprint.Variation, tag blueprint.DependencyTag, name string) {
693 if b.baseModuleContext.checkedMissingDeps() {
694 panic("Adding deps not allowed after checking for missing deps")
695 }
696 b.bp.AddReverseVariationDependency(variations, tag, name)
697}
698
Colin Crossdc35e212019-06-06 16:13:11 -0700699func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700700 names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400701 if b.baseModuleContext.checkedMissingDeps() {
702 panic("Adding deps not allowed after checking for missing deps")
703 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700704 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700705}
706
707func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700708 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400709 if b.baseModuleContext.checkedMissingDeps() {
710 panic("Adding deps not allowed after checking for missing deps")
711 }
Colin Crossdc35e212019-06-06 16:13:11 -0700712
Colin Cross4f1dcb02020-09-16 18:45:04 -0700713 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700714}
715
Colin Crossdc35e212019-06-06 16:13:11 -0700716func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400717 if b.baseModuleContext.checkedMissingDeps() {
718 panic("Adding deps not allowed after checking for missing deps")
719 }
Colin Crossdc35e212019-06-06 16:13:11 -0700720 b.bp.ReplaceDependencies(name)
721}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800722
Paul Duffin80342d72020-06-26 22:08:43 +0100723func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400724 if b.baseModuleContext.checkedMissingDeps() {
725 panic("Adding deps not allowed after checking for missing deps")
726 }
Paul Duffin80342d72020-06-26 22:08:43 +0100727 b.bp.ReplaceDependenciesIf(name, predicate)
728}