blob: 5b76d209ce6618ec5ba7b212d4b69eb3bd81642d [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"
19)
Colin Cross6362e272015-10-29 15:25:03 -070020
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070021// Phases:
22// run Pre-arch mutators
23// run archMutator
24// run Pre-deps mutators
25// run depsMutator
26// run PostDeps mutators
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000027// run FinalDeps mutators (CreateVariations disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070028// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070029
Paul Duffinc05b0342021-03-06 13:28:13 +000030// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
31// with the InitRegistrationContext and will be used at runtime.
32func collateGloballyRegisteredMutators() sortableComponents {
33 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
34}
35
36// collateRegisteredMutators constructs a single list of mutators from the separate lists.
37func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070038 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080039
40 register := func(funcs []RegisterMutatorFunc) {
41 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070042 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080043 }
44 }
45
Colin Crosscec81712017-07-13 14:43:27 -070046 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080047
Colin Crosscec81712017-07-13 14:43:27 -070048 register(preDeps)
49
Liz Kammer356f7d42021-01-26 09:18:53 -050050 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070051
52 register(postDeps)
53
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000054 mctx.finalPhase = true
55 register(finalDeps)
56
Paul Duffinc05b0342021-03-06 13:28:13 +000057 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -070058}
59
60type registerMutatorsContext struct {
Colin Crossb63d7b32023-12-07 16:54:51 -080061 mutators sortableComponents
62 finalPhase bool
Colin Cross795c3772017-03-16 16:50:10 -070063}
Colin Cross1e676be2016-10-12 14:38:15 -070064
65type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -070066 TopDown(name string, m TopDownMutator) MutatorHandle
67 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -070068 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Lukacs T. Berki6c716762022-06-13 20:50:39 +020069 Transition(name string, m TransitionMutator)
Colin Cross1e676be2016-10-12 14:38:15 -070070}
71
72type RegisterMutatorFunc func(RegisterMutatorsContext)
73
Colin Crosscec81712017-07-13 14:43:27 -070074var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -080075 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +010076
Paul Duffinaa4162e2020-05-05 11:35:43 +010077 // Check the visibility rules are valid.
78 //
79 // This must run after the package renamer mutators so that any issues found during
80 // validation of the package's default_visibility property are reported using the
81 // correct package name and not the synthetic name.
82 //
83 // This must also be run before defaults mutators as the rules for validation are
84 // different before checking the rules than they are afterwards. e.g.
85 // visibility: ["//visibility:private", "//visibility:public"]
86 // would be invalid if specified in a module definition but is valid if it results
87 // from something like this:
88 //
89 // defaults {
90 // name: "defaults",
91 // // Be inaccessible outside a package by default.
92 // visibility: ["//visibility:private"]
93 // }
94 //
95 // defaultable_module {
96 // name: "defaultable_module",
97 // defaults: ["defaults"],
98 // // Override the default.
99 // visibility: ["//visibility:public"]
100 // }
101 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000102 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100103
Bob Badour37af0462021-01-07 03:34:31 +0000104 // Record the default_applicable_licenses for each package.
105 //
106 // This must run before the defaults so that defaults modules can pick up the package default.
107 RegisterLicensesPackageMapper,
108
Paul Duffinaa4162e2020-05-05 11:35:43 +0100109 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100110 //
111 // Any mutators that are added before this will not see any modules created by
112 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700113 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100114
Paul Duffin44f1d842020-06-26 20:17:02 +0100115 // Add dependencies on any components so that any component references can be
116 // resolved within the deps mutator.
117 //
118 // Must be run after defaults so it can be used to create dependencies on the
119 // component modules that are creating in a DefaultableHook.
120 //
121 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
122 // renamed. That is so that if a module creates components using a prebuilt module
123 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
124 // the prebuilt module and not the source module.
125 RegisterComponentsMutator,
126
Paul Duffinc988c8e2020-04-29 18:27:14 +0100127 // Create an association between prebuilt modules and their corresponding source
128 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100129 //
130 // Must be run after defaults mutators to ensure that any modules created by
131 // a DefaultableHook can be either a prebuilt or a source module with a matching
132 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100133 RegisterPrebuiltsPreArchMutators,
134
Bob Badour37af0462021-01-07 03:34:31 +0000135 // Gather the licenses properties for all modules for use during expansion and enforcement.
136 //
137 // This must come after the defaults mutators to ensure that any licenses supplied
138 // in a defaults module has been successfully applied before the rules are gathered.
139 RegisterLicensesPropertyGatherer,
140
Paul Duffinaa4162e2020-05-05 11:35:43 +0100141 // Gather the visibility rules for all modules for us during visibility enforcement.
142 //
143 // This must come after the defaults mutators to ensure that any visibility supplied
144 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000145 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700146}
147
Colin Crossae4c6182017-09-15 17:33:55 -0700148func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700149 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800150 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700151 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700152}
153
Colin Crosscec81712017-07-13 14:43:27 -0700154var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700155 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700156}
157
158var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800159 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700160 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000161 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000162 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100163 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700164 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700165}
Colin Cross1e676be2016-10-12 14:38:15 -0700166
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000167var finalDeps = []RegisterMutatorFunc{}
168
Colin Cross1e676be2016-10-12 14:38:15 -0700169func PreArchMutators(f RegisterMutatorFunc) {
170 preArch = append(preArch, f)
171}
172
173func PreDepsMutators(f RegisterMutatorFunc) {
174 preDeps = append(preDeps, f)
175}
176
177func PostDepsMutators(f RegisterMutatorFunc) {
178 postDeps = append(postDeps, f)
179}
180
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000181func FinalDepsMutators(f RegisterMutatorFunc) {
182 finalDeps = append(finalDeps, f)
183}
184
Chris Parsons637458d2023-09-19 20:09:00 +0000185type BaseMutatorContext interface {
186 BaseModuleContext
187
188 // MutatorName returns the name that this mutator was registered with.
189 MutatorName() string
190
191 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
192 // AddDependency or OtherModuleName until after this mutator pass is complete.
193 Rename(name string)
194}
195
196type TopDownMutator func(TopDownMutatorContext)
197
198type TopDownMutatorContext interface {
199 BaseMutatorContext
Chris Parsons637458d2023-09-19 20:09:00 +0000200
201 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
202 // the specified property structs to it as if the properties were set in a blueprint file.
203 CreateModule(ModuleFactory, ...interface{}) Module
204}
205
Colin Cross25de6c32019-06-06 14:29:25 -0700206type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700207 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700208 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700209}
210
Colin Cross25de6c32019-06-06 14:29:25 -0700211type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700212
Colin Cross635c3b02016-05-18 15:37:25 -0700213type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700214 BaseMutatorContext
Colin Crossb63d7b32023-12-07 16:54:51 -0800215
216 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
217 // dependency (some entries may be nil).
218 //
219 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
220 // new dependencies have had the current mutator called on them. If the mutator is not
221 // parallel this method does not affect the ordering of the current mutator pass, but will
222 // be ordered correctly for all future mutator passes.
223 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Crossaabf6792017-11-29 00:27:14 -0800224
Colin Cross9f35c3d2020-09-16 19:04:41 -0700225 // AddReverseDependency adds a dependency from the destination to the given module.
226 // Does not affect the ordering of the current mutator pass, but will be ordered
227 // correctly for all future mutator passes. All reverse dependencies for a destination module are
228 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
229 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800230 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700231
232 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
233 // parameter. It returns a list of new modules in the same order as the variationNames
234 // list.
235 //
236 // If any of the dependencies of the module being operated on were already split
237 // by calling CreateVariations with the same name, the dependency will automatically
238 // be updated to point the matching variant.
239 //
240 // If a module is split, and then a module depending on the first module is not split
241 // when the Mutator is later called on it, the dependency of the depending module will
242 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800243 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700244
245 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
246 // parameter. It returns a list of new modules in the same order as the variantNames
247 // list.
248 //
249 // Local variations do not affect automatic dependency resolution - dependencies added
250 // to the split module via deps or DynamicDependerModule must exactly match a variant
251 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800252 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700253
254 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
255 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800256 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700257
258 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
259 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900260 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700261
262 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700263 // argument to select which variant of the dependency to use. It returns a slice of modules for
264 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
Usta Shresthac725f472022-01-11 02:44:21 -0500265 // all the non-local variations of the current module, plus the variations argument.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700266 //
267 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
268 // new dependencies have had the current mutator called on them. If the mutator is not
269 // parallel this method does not affect the ordering of the current mutator pass, but will
270 // be ordered correctly for all future mutator passes.
Usta Shresthac725f472022-01-11 02:44:21 -0500271 AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700272
273 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700274 // variations argument to select which variant of the dependency to use. It returns a slice of
275 // modules for each dependency (some entries may be nil). A variant of the dependency must
276 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700277 // For any unspecified variation the first variant will be used.
278 //
279 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
280 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700281 //
282 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
283 // new dependencies have had the current mutator called on them. If the mutator is not
284 // parallel this method does not affect the ordering of the current mutator pass, but will
285 // be ordered correctly for all future mutator passes.
286 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700287
288 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
289 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
290 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
291 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800292 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700293
294 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
295 // specified name with the current variant of this module. Replacements don't take effect until
296 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800297 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700298
299 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
300 // specified name with the current variant of this module as long as the supplied predicate returns
301 // true.
302 //
303 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100304 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700305
306 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
307 // and creates an alias from the current variant (before the mutator has run) to the new
308 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
309 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
310 // be used to add dependencies on the newly created variant using the variant map from
311 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800312 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700313
314 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
315 // module, and creates an alias from a new fromVariationName variant the toVariationName
316 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
317 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
318 // be used to add dependencies on the toVariationName variant using the fromVariationName
319 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700320 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700321
322 // SetVariationProvider sets the value for a provider for the given newly created variant of
323 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
324 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
325 // if the value is not of the appropriate type, or if the module is not a newly created
326 // variant of the current module. The value should not be modified after being passed to
327 // SetVariationProvider.
Colin Cross3c0a83d2023-12-12 14:13:26 -0800328 SetVariationProvider(module blueprint.Module, provider blueprint.AnyProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700329}
330
Colin Cross25de6c32019-06-06 14:29:25 -0700331type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700332 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700333 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400334 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700335}
336
Colin Cross617b88a2020-08-24 18:04:09 -0700337func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Colin Crossb63d7b32023-12-07 16:54:51 -0800338 finalPhase bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700339
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400340 moduleContext := a.base().baseModuleContextFactory(ctx)
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400341
Colin Cross617b88a2020-08-24 18:04:09 -0700342 return &bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400343 bp: ctx,
Cole Faust0abd4b42023-01-10 10:49:18 -0800344 baseModuleContext: moduleContext,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400345 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700346 }
347}
348
Colin Cross25de6c32019-06-06 14:29:25 -0700349func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000350 finalPhase := x.finalPhase
Colin Cross798bfce2016-10-12 14:28:16 -0700351 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700352 if a, ok := ctx.Module().(Module); ok {
Colin Crossb63d7b32023-12-07 16:54:51 -0800353 m(bottomUpMutatorContextFactory(ctx, a, finalPhase))
Colin Cross6362e272015-10-29 15:25:03 -0700354 }
Colin Cross798bfce2016-10-12 14:28:16 -0700355 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500356 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700357 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700358 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700359}
360
Colin Cross617b88a2020-08-24 18:04:09 -0700361func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
362 mutator := &mutator{name: name, bottomUpMutator: m}
363 x.mutators = append(x.mutators, mutator)
364 return mutator
365}
366
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200367type IncomingTransitionContext interface {
368 // Module returns the target of the dependency edge for which the transition
369 // is being computed
370 Module() Module
371
372 // Config returns the configuration for the build.
373 Config() Config
374}
375
376type OutgoingTransitionContext interface {
377 // Module returns the target of the dependency edge for which the transition
378 // is being computed
379 Module() Module
380
381 // DepTag() Returns the dependency tag through which this dependency is
382 // reached
383 DepTag() blueprint.DependencyTag
384}
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200385
386// Transition mutators implement a top-down mechanism where a module tells its
387// direct dependencies what variation they should be built in but the dependency
388// has the final say.
389//
390// When implementing a transition mutator, one needs to implement four methods:
391// - Split() that tells what variations a module has by itself
392// - OutgoingTransition() where a module tells what it wants from its
393// dependency
394// - IncomingTransition() where a module has the final say about its own
395// variation
396// - Mutate() that changes the state of a module depending on its variation
397//
398// That the effective variation of module B when depended on by module A is the
399// composition the outgoing transition of module A and the incoming transition
400// of module B.
401//
402// the outgoing transition should not take the properties of the dependency into
403// account, only those of the module that depends on it. For this reason, the
404// dependency is not even passed into it as an argument. Likewise, the incoming
405// transition should not take the properties of the depending module into
406// account and is thus not informed about it. This makes for a nice
407// decomposition of the decision logic.
408//
409// A given transition mutator only affects its own variation; other variations
410// stay unchanged along the dependency edges.
411//
412// Soong makes sure that all modules are created in the desired variations and
413// that dependency edges are set up correctly. This ensures that "missing
414// variation" errors do not happen and allows for more flexible changes in the
415// value of the variation among dependency edges (as oppposed to bottom-up
416// mutators where if module A in variation X depends on module B and module B
417// has that variation X, A must depend on variation X of B)
418//
419// The limited power of the context objects passed to individual mutators
420// methods also makes it more difficult to shoot oneself in the foot. Complete
421// safety is not guaranteed because no one prevents individual transition
422// mutators from mutating modules in illegal ways and for e.g. Split() or
423// Mutate() to run their own visitations of the transitive dependency of the
424// module and both of these are bad ideas, but it's better than no guardrails at
425// all.
426//
427// This model is pretty close to Bazel's configuration transitions. The mapping
428// between concepts in Soong and Bazel is as follows:
429// - Module == configured target
430// - Variant == configuration
431// - Variation name == configuration flag
432// - Variation == configuration flag value
433// - Outgoing transition == attribute transition
434// - Incoming transition == rule transition
435//
436// The Split() method does not have a Bazel equivalent and Bazel split
437// transitions do not have a Soong equivalent.
438//
439// Mutate() does not make sense in Bazel due to the different models of the
440// two systems: when creating new variations, Soong clones the old module and
441// thus some way is needed to change it state whereas Bazel creates each
442// configuration of a given configured target anew.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200443type TransitionMutator interface {
444 // Split returns the set of variations that should be created for a module no
445 // matter who depends on it. Used when Make depends on a particular variation
446 // or when the module knows its variations just based on information given to
447 // it in the Blueprint file. This method should not mutate the module it is
448 // called on.
449 Split(ctx BaseModuleContext) []string
450
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200451 // Called on a module to determine which variation it wants from its direct
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200452 // dependencies. The dependency itself can override this decision. This method
453 // should not mutate the module itself.
454 OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string
455
456 // Called on a module to determine which variation it should be in based on
457 // the variation modules that depend on it want. This gives the module a final
458 // say about its own variations. This method should not mutate the module
459 // itself.
460 IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string
461
462 // Called after a module was split into multiple variations on each variation.
463 // It should not split the module any further but adding new dependencies is
464 // fine. Unlike all the other methods on TransitionMutator, this method is
465 // allowed to mutate the module.
466 Mutate(ctx BottomUpMutatorContext, variation string)
467}
468
469type androidTransitionMutator struct {
Colin Crossb63d7b32023-12-07 16:54:51 -0800470 finalPhase bool
471 mutator TransitionMutator
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200472}
473
474func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []string {
475 if m, ok := ctx.Module().(Module); ok {
476 moduleContext := m.base().baseModuleContextFactory(ctx)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200477 return a.mutator.Split(&moduleContext)
478 } else {
479 return []string{""}
480 }
481}
482
483type outgoingTransitionContextImpl struct {
484 bp blueprint.OutgoingTransitionContext
485}
486
487func (c *outgoingTransitionContextImpl) Module() Module {
488 return c.bp.Module().(Module)
489}
490
491func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag {
492 return c.bp.DepTag()
493}
494
495func (a *androidTransitionMutator) OutgoingTransition(ctx blueprint.OutgoingTransitionContext, sourceVariation string) string {
496 if _, ok := ctx.Module().(Module); ok {
497 return a.mutator.OutgoingTransition(&outgoingTransitionContextImpl{bp: ctx}, sourceVariation)
498 } else {
499 return ""
500 }
501}
502
503type incomingTransitionContextImpl struct {
504 bp blueprint.IncomingTransitionContext
505}
506
507func (c *incomingTransitionContextImpl) Module() Module {
508 return c.bp.Module().(Module)
509}
510
511func (c *incomingTransitionContextImpl) Config() Config {
512 return c.bp.Config().(Config)
513}
514
515func (a *androidTransitionMutator) IncomingTransition(ctx blueprint.IncomingTransitionContext, incomingVariation string) string {
516 if _, ok := ctx.Module().(Module); ok {
517 return a.mutator.IncomingTransition(&incomingTransitionContextImpl{bp: ctx}, incomingVariation)
518 } else {
519 return ""
520 }
521}
522
523func (a *androidTransitionMutator) Mutate(ctx blueprint.BottomUpMutatorContext, variation string) {
524 if am, ok := ctx.Module().(Module); ok {
Colin Crossb63d7b32023-12-07 16:54:51 -0800525 a.mutator.Mutate(bottomUpMutatorContextFactory(ctx, am, a.finalPhase), variation)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200526 }
527}
528
529func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) {
530 atm := &androidTransitionMutator{
Colin Crossb63d7b32023-12-07 16:54:51 -0800531 finalPhase: x.finalPhase,
532 mutator: m,
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200533 }
534 mutator := &mutator{
535 name: name,
536 transitionMutator: atm}
537 x.mutators = append(x.mutators, mutator)
538}
539
Liz Kammer356f7d42021-01-26 09:18:53 -0500540func (x *registerMutatorsContext) mutatorName(name string) string {
Liz Kammer356f7d42021-01-26 09:18:53 -0500541 return name
542}
543
Colin Cross25de6c32019-06-06 14:29:25 -0700544func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700545 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700546 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400547 moduleContext := a.base().baseModuleContextFactory(ctx)
Colin Cross25de6c32019-06-06 14:29:25 -0700548 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700549 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400550 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700551 }
Colin Cross798bfce2016-10-12 14:28:16 -0700552 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700553 }
Colin Cross798bfce2016-10-12 14:28:16 -0700554 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500555 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700556 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700557 return mutator
558}
559
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000560func (mutator *mutator) componentName() string {
561 return mutator.name
562}
563
564func (mutator *mutator) register(ctx *Context) {
565 blueprintCtx := ctx.Context
566 var handle blueprint.MutatorHandle
567 if mutator.bottomUpMutator != nil {
568 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
569 } else if mutator.topDownMutator != nil {
570 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200571 } else if mutator.transitionMutator != nil {
572 blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000573 }
574 if mutator.parallel {
575 handle.Parallel()
576 }
577}
578
Colin Cross798bfce2016-10-12 14:28:16 -0700579type MutatorHandle interface {
580 Parallel() MutatorHandle
581}
582
583func (mutator *mutator) Parallel() MutatorHandle {
584 mutator.parallel = true
585 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700586}
Colin Cross1e676be2016-10-12 14:38:15 -0700587
Paul Duffin44f1d842020-06-26 20:17:02 +0100588func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
589 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
590}
591
592// A special mutator that runs just prior to the deps mutator to allow the dependencies
593// on component modules to be added so that they can depend directly on a prebuilt
594// module.
595func componentDepsMutator(ctx BottomUpMutatorContext) {
596 if m := ctx.Module(); m.Enabled() {
597 m.ComponentDepsMutator(ctx)
598 }
599}
600
Colin Cross1e676be2016-10-12 14:38:15 -0700601func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100602 if m := ctx.Module(); m.Enabled() {
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800603 m.base().baseDepsMutator(ctx)
Colin Cross1e676be2016-10-12 14:38:15 -0700604 m.DepsMutator(ctx)
605 }
606}
Colin Crossd11fcda2017-10-23 17:59:01 -0700607
Liz Kammer356f7d42021-01-26 09:18:53 -0500608func registerDepsMutator(ctx RegisterMutatorsContext) {
609 ctx.BottomUp("deps", depsMutator).Parallel()
610}
611
Colin Crossdc35e212019-06-06 16:13:11 -0700612// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
613// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
614// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
615// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
616// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
617
Colin Crosscb55e082019-07-01 15:32:31 -0700618func (t *topDownMutatorContext) MutatorName() string {
619 return t.bp.MutatorName()
620}
621
Colin Crossdc35e212019-06-06 16:13:11 -0700622func (t *topDownMutatorContext) Rename(name string) {
623 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700624 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700625}
626
Liz Kammerf31c9002022-04-26 09:08:55 -0400627func (t *topDownMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
628 return t.bp.CreateModule(factory, name, props...)
629}
630
Colin Crosse003c4a2019-09-25 12:58:36 -0700631func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400632 return createModule(t, factory, "_topDownMutatorModule", props...)
Colin Crossdc35e212019-06-06 16:13:11 -0700633}
634
Liz Kammera060c452021-03-24 10:14:47 -0400635func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400636 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), "", props...).(Module)
Liz Kammera060c452021-03-24 10:14:47 -0400637 return module
638}
639
Colin Crosscb55e082019-07-01 15:32:31 -0700640func (b *bottomUpMutatorContext) MutatorName() string {
641 return b.bp.MutatorName()
642}
643
Colin Crossdc35e212019-06-06 16:13:11 -0700644func (b *bottomUpMutatorContext) Rename(name string) {
645 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700646 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700647}
648
Colin Cross4f1dcb02020-09-16 18:45:04 -0700649func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400650 if b.baseModuleContext.checkedMissingDeps() {
651 panic("Adding deps not allowed after checking for missing deps")
652 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700653 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700654}
655
656func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400657 if b.baseModuleContext.checkedMissingDeps() {
658 panic("Adding deps not allowed after checking for missing deps")
659 }
Colin Crossdc35e212019-06-06 16:13:11 -0700660 b.bp.AddReverseDependency(module, tag, name)
661}
662
Colin Cross43b92e02019-11-18 15:28:57 -0800663func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000664 if b.finalPhase {
665 panic("CreateVariations not allowed in FinalDepsMutators")
666 }
667
Colin Cross9a362232019-07-01 15:32:45 -0700668 modules := b.bp.CreateVariations(variations...)
669
Colin Cross43b92e02019-11-18 15:28:57 -0800670 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700671 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800672 aModules[i] = modules[i].(Module)
673 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700674 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
675 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
676 }
677
Colin Cross43b92e02019-11-18 15:28:57 -0800678 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700679}
680
Colin Cross43b92e02019-11-18 15:28:57 -0800681func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000682 if b.finalPhase {
683 panic("CreateLocalVariations not allowed in FinalDepsMutators")
684 }
685
Colin Cross9a362232019-07-01 15:32:45 -0700686 modules := b.bp.CreateLocalVariations(variations...)
687
Colin Cross43b92e02019-11-18 15:28:57 -0800688 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700689 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800690 aModules[i] = modules[i].(Module)
691 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700692 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
693 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
694 }
695
Colin Cross43b92e02019-11-18 15:28:57 -0800696 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700697}
698
699func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
700 b.bp.SetDependencyVariation(variation)
701}
702
Jiyong Park1d1119f2019-07-29 21:27:18 +0900703func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
704 b.bp.SetDefaultDependencyVariation(variation)
705}
706
Colin Crossdc35e212019-06-06 16:13:11 -0700707func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700708 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 Cross4f1dcb02020-09-16 18:45:04 -0700712 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700713}
714
715func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700716 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
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
Colin Cross4f1dcb02020-09-16 18:45:04 -0700721 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700722}
723
724func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
725 b.bp.AddInterVariantDependency(tag, from, to)
726}
727
728func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400729 if b.baseModuleContext.checkedMissingDeps() {
730 panic("Adding deps not allowed after checking for missing deps")
731 }
Colin Crossdc35e212019-06-06 16:13:11 -0700732 b.bp.ReplaceDependencies(name)
733}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800734
Paul Duffin80342d72020-06-26 22:08:43 +0100735func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400736 if b.baseModuleContext.checkedMissingDeps() {
737 panic("Adding deps not allowed after checking for missing deps")
738 }
Paul Duffin80342d72020-06-26 22:08:43 +0100739 b.bp.ReplaceDependenciesIf(name, predicate)
740}
741
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800742func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
743 b.bp.AliasVariation(variationName)
744}
Colin Cross1b9604b2020-08-11 12:03:56 -0700745
746func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
747 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
748}
Colin Crossd27e7b82020-07-02 11:38:17 -0700749
Colin Cross3c0a83d2023-12-12 14:13:26 -0800750func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.AnyProviderKey, value interface{}) {
Colin Crossd27e7b82020-07-02 11:38:17 -0700751 b.bp.SetVariationProvider(module, provider, value)
752}