blob: e569698296c4d87abfa512fd38caa0ab8790ff3a [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 {
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800368 ArchModuleContext
369
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200370 // Module returns the target of the dependency edge for which the transition
371 // is being computed
372 Module() Module
373
374 // Config returns the configuration for the build.
375 Config() Config
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800376
377 DeviceConfig() DeviceConfig
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200378}
379
380type OutgoingTransitionContext interface {
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800381 ArchModuleContext
382
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200383 // Module returns the target of the dependency edge for which the transition
384 // is being computed
385 Module() Module
386
387 // DepTag() Returns the dependency tag through which this dependency is
388 // reached
389 DepTag() blueprint.DependencyTag
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800390
391 // Config returns the configuration for the build.
392 Config() Config
393
394 DeviceConfig() DeviceConfig
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200395}
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200396
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800397// TransitionMutator implements a top-down mechanism where a module tells its
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200398// direct dependencies what variation they should be built in but the dependency
399// has the final say.
400//
401// When implementing a transition mutator, one needs to implement four methods:
402// - Split() that tells what variations a module has by itself
403// - OutgoingTransition() where a module tells what it wants from its
404// dependency
405// - IncomingTransition() where a module has the final say about its own
406// variation
407// - Mutate() that changes the state of a module depending on its variation
408//
409// That the effective variation of module B when depended on by module A is the
410// composition the outgoing transition of module A and the incoming transition
411// of module B.
412//
413// the outgoing transition should not take the properties of the dependency into
414// account, only those of the module that depends on it. For this reason, the
415// dependency is not even passed into it as an argument. Likewise, the incoming
416// transition should not take the properties of the depending module into
417// account and is thus not informed about it. This makes for a nice
418// decomposition of the decision logic.
419//
420// A given transition mutator only affects its own variation; other variations
421// stay unchanged along the dependency edges.
422//
423// Soong makes sure that all modules are created in the desired variations and
424// that dependency edges are set up correctly. This ensures that "missing
425// variation" errors do not happen and allows for more flexible changes in the
426// value of the variation among dependency edges (as oppposed to bottom-up
427// mutators where if module A in variation X depends on module B and module B
428// has that variation X, A must depend on variation X of B)
429//
430// The limited power of the context objects passed to individual mutators
431// methods also makes it more difficult to shoot oneself in the foot. Complete
432// safety is not guaranteed because no one prevents individual transition
433// mutators from mutating modules in illegal ways and for e.g. Split() or
434// Mutate() to run their own visitations of the transitive dependency of the
435// module and both of these are bad ideas, but it's better than no guardrails at
436// all.
437//
438// This model is pretty close to Bazel's configuration transitions. The mapping
439// between concepts in Soong and Bazel is as follows:
440// - Module == configured target
441// - Variant == configuration
442// - Variation name == configuration flag
443// - Variation == configuration flag value
444// - Outgoing transition == attribute transition
445// - Incoming transition == rule transition
446//
447// The Split() method does not have a Bazel equivalent and Bazel split
448// transitions do not have a Soong equivalent.
449//
450// Mutate() does not make sense in Bazel due to the different models of the
451// two systems: when creating new variations, Soong clones the old module and
452// thus some way is needed to change it state whereas Bazel creates each
453// configuration of a given configured target anew.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200454type TransitionMutator interface {
455 // Split returns the set of variations that should be created for a module no
456 // matter who depends on it. Used when Make depends on a particular variation
457 // or when the module knows its variations just based on information given to
458 // it in the Blueprint file. This method should not mutate the module it is
459 // called on.
460 Split(ctx BaseModuleContext) []string
461
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800462 // OutgoingTransition is called on a module to determine which variation it wants
463 // from its direct dependencies. The dependency itself can override this decision.
464 // This method should not mutate the module itself.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200465 OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string
466
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800467 // IncomingTransition is called on a module to determine which variation it should
468 // be in based on the variation modules that depend on it want. This gives the module
469 // a final say about its own variations. This method should not mutate the module
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200470 // itself.
471 IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string
472
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800473 // Mutate is called after a module was split into multiple variations on each variation.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200474 // It should not split the module any further but adding new dependencies is
475 // fine. Unlike all the other methods on TransitionMutator, this method is
476 // allowed to mutate the module.
477 Mutate(ctx BottomUpMutatorContext, variation string)
478}
479
480type androidTransitionMutator struct {
Colin Crossb63d7b32023-12-07 16:54:51 -0800481 finalPhase bool
482 mutator TransitionMutator
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200483}
484
485func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []string {
486 if m, ok := ctx.Module().(Module); ok {
487 moduleContext := m.base().baseModuleContextFactory(ctx)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200488 return a.mutator.Split(&moduleContext)
489 } else {
490 return []string{""}
491 }
492}
493
494type outgoingTransitionContextImpl struct {
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800495 archModuleContext
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200496 bp blueprint.OutgoingTransitionContext
497}
498
499func (c *outgoingTransitionContextImpl) Module() Module {
500 return c.bp.Module().(Module)
501}
502
503func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag {
504 return c.bp.DepTag()
505}
506
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800507func (c *outgoingTransitionContextImpl) Config() Config {
508 return c.bp.Config().(Config)
509}
510
511func (c *outgoingTransitionContextImpl) DeviceConfig() DeviceConfig {
512 return DeviceConfig{c.bp.Config().(Config).deviceConfig}
513}
514
515func (a *androidTransitionMutator) OutgoingTransition(bpctx blueprint.OutgoingTransitionContext, sourceVariation string) string {
516 if m, ok := bpctx.Module().(Module); ok {
517 ctx := &outgoingTransitionContextImpl{
518 archModuleContext: m.base().archModuleContextFactory(bpctx),
519 bp: bpctx,
520 }
521 return a.mutator.OutgoingTransition(ctx, sourceVariation)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200522 } else {
523 return ""
524 }
525}
526
527type incomingTransitionContextImpl struct {
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800528 archModuleContext
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200529 bp blueprint.IncomingTransitionContext
530}
531
532func (c *incomingTransitionContextImpl) Module() Module {
533 return c.bp.Module().(Module)
534}
535
536func (c *incomingTransitionContextImpl) Config() Config {
537 return c.bp.Config().(Config)
538}
539
Colin Cross4aa3e0a2024-01-18 17:22:58 -0800540func (c *incomingTransitionContextImpl) DeviceConfig() DeviceConfig {
541 return DeviceConfig{c.bp.Config().(Config).deviceConfig}
542}
543
544func (a *androidTransitionMutator) IncomingTransition(bpctx blueprint.IncomingTransitionContext, incomingVariation string) string {
545 if m, ok := bpctx.Module().(Module); ok {
546 ctx := &incomingTransitionContextImpl{
547 archModuleContext: m.base().archModuleContextFactory(bpctx),
548 bp: bpctx,
549 }
550 return a.mutator.IncomingTransition(ctx, incomingVariation)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200551 } else {
552 return ""
553 }
554}
555
556func (a *androidTransitionMutator) Mutate(ctx blueprint.BottomUpMutatorContext, variation string) {
557 if am, ok := ctx.Module().(Module); ok {
Colin Crossb63d7b32023-12-07 16:54:51 -0800558 a.mutator.Mutate(bottomUpMutatorContextFactory(ctx, am, a.finalPhase), variation)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200559 }
560}
561
562func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) {
563 atm := &androidTransitionMutator{
Colin Crossb63d7b32023-12-07 16:54:51 -0800564 finalPhase: x.finalPhase,
565 mutator: m,
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200566 }
567 mutator := &mutator{
568 name: name,
569 transitionMutator: atm}
570 x.mutators = append(x.mutators, mutator)
571}
572
Liz Kammer356f7d42021-01-26 09:18:53 -0500573func (x *registerMutatorsContext) mutatorName(name string) string {
Liz Kammer356f7d42021-01-26 09:18:53 -0500574 return name
575}
576
Colin Cross25de6c32019-06-06 14:29:25 -0700577func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700578 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700579 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400580 moduleContext := a.base().baseModuleContextFactory(ctx)
Colin Cross25de6c32019-06-06 14:29:25 -0700581 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700582 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400583 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700584 }
Colin Cross798bfce2016-10-12 14:28:16 -0700585 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700586 }
Colin Cross798bfce2016-10-12 14:28:16 -0700587 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500588 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700589 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700590 return mutator
591}
592
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000593func (mutator *mutator) componentName() string {
594 return mutator.name
595}
596
597func (mutator *mutator) register(ctx *Context) {
598 blueprintCtx := ctx.Context
599 var handle blueprint.MutatorHandle
600 if mutator.bottomUpMutator != nil {
601 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
602 } else if mutator.topDownMutator != nil {
603 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200604 } else if mutator.transitionMutator != nil {
605 blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000606 }
607 if mutator.parallel {
608 handle.Parallel()
609 }
610}
611
Colin Cross798bfce2016-10-12 14:28:16 -0700612type MutatorHandle interface {
613 Parallel() MutatorHandle
614}
615
616func (mutator *mutator) Parallel() MutatorHandle {
617 mutator.parallel = true
618 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700619}
Colin Cross1e676be2016-10-12 14:38:15 -0700620
Paul Duffin44f1d842020-06-26 20:17:02 +0100621func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
622 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
623}
624
625// A special mutator that runs just prior to the deps mutator to allow the dependencies
626// on component modules to be added so that they can depend directly on a prebuilt
627// module.
628func componentDepsMutator(ctx BottomUpMutatorContext) {
629 if m := ctx.Module(); m.Enabled() {
630 m.ComponentDepsMutator(ctx)
631 }
632}
633
Colin Cross1e676be2016-10-12 14:38:15 -0700634func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100635 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700636 m.DepsMutator(ctx)
637 }
638}
Colin Crossd11fcda2017-10-23 17:59:01 -0700639
Liz Kammer356f7d42021-01-26 09:18:53 -0500640func registerDepsMutator(ctx RegisterMutatorsContext) {
641 ctx.BottomUp("deps", depsMutator).Parallel()
642}
643
Colin Crossdc35e212019-06-06 16:13:11 -0700644// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
645// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
646// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
647// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
648// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
649
Colin Crosscb55e082019-07-01 15:32:31 -0700650func (t *topDownMutatorContext) MutatorName() string {
651 return t.bp.MutatorName()
652}
653
Colin Crossdc35e212019-06-06 16:13:11 -0700654func (t *topDownMutatorContext) Rename(name string) {
655 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700656 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700657}
658
Liz Kammerf31c9002022-04-26 09:08:55 -0400659func (t *topDownMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
660 return t.bp.CreateModule(factory, name, props...)
661}
662
Colin Crosse003c4a2019-09-25 12:58:36 -0700663func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400664 return createModule(t, factory, "_topDownMutatorModule", props...)
Colin Crossdc35e212019-06-06 16:13:11 -0700665}
666
Liz Kammera060c452021-03-24 10:14:47 -0400667func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400668 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), "", props...).(Module)
Liz Kammera060c452021-03-24 10:14:47 -0400669 return module
670}
671
Colin Crosscb55e082019-07-01 15:32:31 -0700672func (b *bottomUpMutatorContext) MutatorName() string {
673 return b.bp.MutatorName()
674}
675
Colin Crossdc35e212019-06-06 16:13:11 -0700676func (b *bottomUpMutatorContext) Rename(name string) {
677 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700678 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700679}
680
Colin Cross4f1dcb02020-09-16 18:45:04 -0700681func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400682 if b.baseModuleContext.checkedMissingDeps() {
683 panic("Adding deps not allowed after checking for missing deps")
684 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700685 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700686}
687
688func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400689 if b.baseModuleContext.checkedMissingDeps() {
690 panic("Adding deps not allowed after checking for missing deps")
691 }
Colin Crossdc35e212019-06-06 16:13:11 -0700692 b.bp.AddReverseDependency(module, tag, name)
693}
694
Colin Cross43b92e02019-11-18 15:28:57 -0800695func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000696 if b.finalPhase {
697 panic("CreateVariations not allowed in FinalDepsMutators")
698 }
699
Colin Cross9a362232019-07-01 15:32:45 -0700700 modules := b.bp.CreateVariations(variations...)
701
Colin Cross43b92e02019-11-18 15:28:57 -0800702 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700703 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800704 aModules[i] = modules[i].(Module)
705 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700706 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
707 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
708 }
709
Colin Cross43b92e02019-11-18 15:28:57 -0800710 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700711}
712
Colin Cross43b92e02019-11-18 15:28:57 -0800713func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000714 if b.finalPhase {
715 panic("CreateLocalVariations not allowed in FinalDepsMutators")
716 }
717
Colin Cross9a362232019-07-01 15:32:45 -0700718 modules := b.bp.CreateLocalVariations(variations...)
719
Colin Cross43b92e02019-11-18 15:28:57 -0800720 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700721 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800722 aModules[i] = modules[i].(Module)
723 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700724 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
725 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
726 }
727
Colin Cross43b92e02019-11-18 15:28:57 -0800728 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700729}
730
731func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
732 b.bp.SetDependencyVariation(variation)
733}
734
Jiyong Park1d1119f2019-07-29 21:27:18 +0900735func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
736 b.bp.SetDefaultDependencyVariation(variation)
737}
738
Colin Crossdc35e212019-06-06 16:13:11 -0700739func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700740 names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400741 if b.baseModuleContext.checkedMissingDeps() {
742 panic("Adding deps not allowed after checking for missing deps")
743 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700744 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700745}
746
747func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700748 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400749 if b.baseModuleContext.checkedMissingDeps() {
750 panic("Adding deps not allowed after checking for missing deps")
751 }
Colin Crossdc35e212019-06-06 16:13:11 -0700752
Colin Cross4f1dcb02020-09-16 18:45:04 -0700753 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700754}
755
756func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
757 b.bp.AddInterVariantDependency(tag, from, to)
758}
759
760func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400761 if b.baseModuleContext.checkedMissingDeps() {
762 panic("Adding deps not allowed after checking for missing deps")
763 }
Colin Crossdc35e212019-06-06 16:13:11 -0700764 b.bp.ReplaceDependencies(name)
765}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800766
Paul Duffin80342d72020-06-26 22:08:43 +0100767func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400768 if b.baseModuleContext.checkedMissingDeps() {
769 panic("Adding deps not allowed after checking for missing deps")
770 }
Paul Duffin80342d72020-06-26 22:08:43 +0100771 b.bp.ReplaceDependenciesIf(name, predicate)
772}
773
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800774func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
775 b.bp.AliasVariation(variationName)
776}
Colin Cross1b9604b2020-08-11 12:03:56 -0700777
778func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
779 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
780}
Colin Crossd27e7b82020-07-02 11:38:17 -0700781
Colin Cross3c0a83d2023-12-12 14:13:26 -0800782func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.AnyProviderKey, value interface{}) {
Colin Crossd27e7b82020-07-02 11:38:17 -0700783 b.bp.SetVariationProvider(module, provider, value)
784}