blob: 41853154f29f805bbdc88717f8058fdd16055c8d [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 (
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000018 "android/soong/bazel"
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
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000029// run FinalDeps mutators (CreateVariations disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070030// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070031
Jingwen Chen73850672020-12-14 08:25:34 -050032// RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
Liz Kammerbe46fcc2021-11-01 15:32:43 -040033func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) {
Spandan Das5af0bd32022-09-28 20:43:08 +000034 bp2buildMutators := append(preArchMutators, registerBp2buildConversionMutator)
35 registerMutatorsForBazelConversion(ctx, bp2buildMutators)
36}
37
38// RegisterMutatorsForApiBazelConversion is an alternate registration pipeline for api_bp2build
39// This pipeline restricts generation of Bazel targets to Soong modules that contribute APIs
40func RegisterMutatorsForApiBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) {
41 bp2buildMutators := append(preArchMutators, registerApiBp2buildConversionMutator)
42 registerMutatorsForBazelConversion(ctx, bp2buildMutators)
43}
44
45func registerMutatorsForBazelConversion(ctx *Context, bp2buildMutators []RegisterMutatorFunc) {
Liz Kammer356f7d42021-01-26 09:18:53 -050046 mctx := &registerMutatorsContext{
47 bazelConversionMode: true,
Jingwen Chen041b1842021-02-01 00:23:25 -050048 }
49
Spandan Das5af0bd32022-09-28 20:43:08 +000050 allMutators := append([]RegisterMutatorFunc{
Liz Kammer356f7d42021-01-26 09:18:53 -050051 RegisterNamespaceMutator,
52 RegisterDefaultsPreArchMutators,
53 // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should
54 // evaluate the impact on conversion.
55 RegisterPrebuiltsPreArchMutators,
56 },
Spandan Das5af0bd32022-09-28 20:43:08 +000057 bp2buildMutators...)
Liz Kammer356f7d42021-01-26 09:18:53 -050058
Jingwen Chen73850672020-12-14 08:25:34 -050059 // Register bp2build mutators
Spandan Das5af0bd32022-09-28 20:43:08 +000060 for _, f := range allMutators {
Jingwen Chen73850672020-12-14 08:25:34 -050061 f(mctx)
62 }
63
Paul Duffin1d2d42f2021-03-06 20:08:12 +000064 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050065}
66
Paul Duffinc05b0342021-03-06 13:28:13 +000067// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
68// with the InitRegistrationContext and will be used at runtime.
69func collateGloballyRegisteredMutators() sortableComponents {
Liz Kammerc13f7852023-05-17 13:01:48 -040070 // ensure mixed builds mutator is the last mutator
71 finalDeps = append(finalDeps, registerMixedBuildsMutator)
Paul Duffinc05b0342021-03-06 13:28:13 +000072 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
73}
74
75// collateRegisteredMutators constructs a single list of mutators from the separate lists.
76func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070077 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080078
79 register := func(funcs []RegisterMutatorFunc) {
80 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070081 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080082 }
83 }
84
Colin Crosscec81712017-07-13 14:43:27 -070085 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080086
Colin Crosscec81712017-07-13 14:43:27 -070087 register(preDeps)
88
Liz Kammer356f7d42021-01-26 09:18:53 -050089 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070090
91 register(postDeps)
92
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000093 mctx.finalPhase = true
94 register(finalDeps)
95
Paul Duffinc05b0342021-03-06 13:28:13 +000096 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -070097}
98
99type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000100 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -0500101 finalPhase bool
102 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -0700103}
Colin Cross1e676be2016-10-12 14:38:15 -0700104
105type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -0700106 TopDown(name string, m TopDownMutator) MutatorHandle
107 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -0700108 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200109 Transition(name string, m TransitionMutator)
Colin Cross1e676be2016-10-12 14:38:15 -0700110}
111
112type RegisterMutatorFunc func(RegisterMutatorsContext)
113
Colin Crosscec81712017-07-13 14:43:27 -0700114var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800115 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100116
Paul Duffinaa4162e2020-05-05 11:35:43 +0100117 // Check the visibility rules are valid.
118 //
119 // This must run after the package renamer mutators so that any issues found during
120 // validation of the package's default_visibility property are reported using the
121 // correct package name and not the synthetic name.
122 //
123 // This must also be run before defaults mutators as the rules for validation are
124 // different before checking the rules than they are afterwards. e.g.
125 // visibility: ["//visibility:private", "//visibility:public"]
126 // would be invalid if specified in a module definition but is valid if it results
127 // from something like this:
128 //
129 // defaults {
130 // name: "defaults",
131 // // Be inaccessible outside a package by default.
132 // visibility: ["//visibility:private"]
133 // }
134 //
135 // defaultable_module {
136 // name: "defaultable_module",
137 // defaults: ["defaults"],
138 // // Override the default.
139 // visibility: ["//visibility:public"]
140 // }
141 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000142 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100143
Bob Badour37af0462021-01-07 03:34:31 +0000144 // Record the default_applicable_licenses for each package.
145 //
146 // This must run before the defaults so that defaults modules can pick up the package default.
147 RegisterLicensesPackageMapper,
148
Paul Duffinaa4162e2020-05-05 11:35:43 +0100149 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100150 //
151 // Any mutators that are added before this will not see any modules created by
152 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700153 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100154
Paul Duffin44f1d842020-06-26 20:17:02 +0100155 // Add dependencies on any components so that any component references can be
156 // resolved within the deps mutator.
157 //
158 // Must be run after defaults so it can be used to create dependencies on the
159 // component modules that are creating in a DefaultableHook.
160 //
161 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
162 // renamed. That is so that if a module creates components using a prebuilt module
163 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
164 // the prebuilt module and not the source module.
165 RegisterComponentsMutator,
166
Paul Duffinc988c8e2020-04-29 18:27:14 +0100167 // Create an association between prebuilt modules and their corresponding source
168 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100169 //
170 // Must be run after defaults mutators to ensure that any modules created by
171 // a DefaultableHook can be either a prebuilt or a source module with a matching
172 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100173 RegisterPrebuiltsPreArchMutators,
174
Bob Badour37af0462021-01-07 03:34:31 +0000175 // Gather the licenses properties for all modules for use during expansion and enforcement.
176 //
177 // This must come after the defaults mutators to ensure that any licenses supplied
178 // in a defaults module has been successfully applied before the rules are gathered.
179 RegisterLicensesPropertyGatherer,
180
Paul Duffinaa4162e2020-05-05 11:35:43 +0100181 // Gather the visibility rules for all modules for us during visibility enforcement.
182 //
183 // This must come after the defaults mutators to ensure that any visibility supplied
184 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000185 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700186}
187
Colin Crossae4c6182017-09-15 17:33:55 -0700188func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700189 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800190 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700191 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700192}
193
Colin Crosscec81712017-07-13 14:43:27 -0700194var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700195 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700196}
197
198var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800199 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700200 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000201 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000202 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100203 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700204 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700205}
Colin Cross1e676be2016-10-12 14:38:15 -0700206
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000207var finalDeps = []RegisterMutatorFunc{}
208
Colin Cross1e676be2016-10-12 14:38:15 -0700209func PreArchMutators(f RegisterMutatorFunc) {
210 preArch = append(preArch, f)
211}
212
213func PreDepsMutators(f RegisterMutatorFunc) {
214 preDeps = append(preDeps, f)
215}
216
217func PostDepsMutators(f RegisterMutatorFunc) {
218 postDeps = append(postDeps, f)
219}
220
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000221func FinalDepsMutators(f RegisterMutatorFunc) {
222 finalDeps = append(finalDeps, f)
223}
224
Liz Kammer356f7d42021-01-26 09:18:53 -0500225var bp2buildPreArchMutators = []RegisterMutatorFunc{}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400226
Liz Kammer12615db2021-09-28 09:19:17 -0400227// A minimal context for Bp2build conversion
228type Bp2buildMutatorContext interface {
229 BazelConversionPathContext
230
231 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
232}
233
Liz Kammer356f7d42021-01-26 09:18:53 -0500234// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
235// into Bazel BUILD targets that should run prior to deps and conversion.
236func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
237 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
238}
239
Colin Cross9f35c3d2020-09-16 19:04:41 -0700240type BaseMutatorContext interface {
241 BaseModuleContext
242
243 // MutatorName returns the name that this mutator was registered with.
244 MutatorName() string
245
246 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
247 // AddDependency or OtherModuleName until after this mutator pass is complete.
248 Rename(name string)
249}
250
Colin Cross25de6c32019-06-06 14:29:25 -0700251type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700252
Colin Cross635c3b02016-05-18 15:37:25 -0700253type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700254 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700255
Colin Cross9f35c3d2020-09-16 19:04:41 -0700256 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
257 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700258 CreateModule(ModuleFactory, ...interface{}) Module
Jingwen Chen1fd14692021-02-05 03:01:50 -0500259
260 // CreateBazelTargetModule creates a BazelTargetModule by calling the
261 // factory method, just like in CreateModule, but also requires
262 // BazelTargetModuleProperties containing additional metadata for the
263 // bp2build codegenerator.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000264 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
Chris Parsons58852a02021-12-09 18:10:18 -0500265
266 // CreateBazelTargetModuleWithRestrictions creates a BazelTargetModule by calling the
267 // factory method, just like in CreateModule, but also requires
268 // BazelTargetModuleProperties containing additional metadata for the
269 // bp2build codegenerator. The generated target is restricted to only be buildable for certain
270 // platforms, as dictated by a given bool attribute: the target will not be buildable in
271 // any platform for which this bool attribute is false.
272 CreateBazelTargetModuleWithRestrictions(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}, bazel.BoolAttribute)
Spandan Dasabedff02023-03-07 19:24:34 +0000273
274 // CreateBazelTargetAliasInDir creates an alias definition in `dir` directory.
275 // This function can be used to create alias definitions in a directory that is different
276 // from the directory of the visited Soong module.
277 CreateBazelTargetAliasInDir(dir string, name string, actual bazel.Label)
Spandan Das6a448ec2023-04-19 17:36:12 +0000278
279 // CreateBazelConfigSetting creates a config_setting in <dir>/BUILD.bazel
280 // build/bazel has several static config_setting(s) that are used in Bazel builds.
281 // This function can be used to createa additional config_setting(s) based on the build graph
282 // (e.g. a config_setting specific to an apex variant)
283 CreateBazelConfigSetting(csa bazel.ConfigSettingAttributes, ca CommonAttributes, dir string)
Colin Cross6362e272015-10-29 15:25:03 -0700284}
285
Colin Cross25de6c32019-06-06 14:29:25 -0700286type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700287 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700288 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700289}
290
Colin Cross25de6c32019-06-06 14:29:25 -0700291type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700292
Colin Cross635c3b02016-05-18 15:37:25 -0700293type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700294 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800295
Colin Cross4f1dcb02020-09-16 18:45:04 -0700296 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
297 // dependency (some entries may be nil).
298 //
299 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
300 // new dependencies have had the current mutator called on them. If the mutator is not
301 // parallel this method does not affect the ordering of the current mutator pass, but will
302 // be ordered correctly for all future mutator passes.
303 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700304
305 // AddReverseDependency adds a dependency from the destination to the given module.
306 // Does not affect the ordering of the current mutator pass, but will be ordered
307 // correctly for all future mutator passes. All reverse dependencies for a destination module are
308 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
309 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800310 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700311
312 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
313 // parameter. It returns a list of new modules in the same order as the variationNames
314 // list.
315 //
316 // If any of the dependencies of the module being operated on were already split
317 // by calling CreateVariations with the same name, the dependency will automatically
318 // be updated to point the matching variant.
319 //
320 // If a module is split, and then a module depending on the first module is not split
321 // when the Mutator is later called on it, the dependency of the depending module will
322 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800323 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700324
325 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
326 // parameter. It returns a list of new modules in the same order as the variantNames
327 // list.
328 //
329 // Local variations do not affect automatic dependency resolution - dependencies added
330 // to the split module via deps or DynamicDependerModule must exactly match a variant
331 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800332 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700333
334 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
335 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800336 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700337
338 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
339 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900340 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700341
342 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700343 // argument to select which variant of the dependency to use. It returns a slice of modules for
344 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
Usta Shresthac725f472022-01-11 02:44:21 -0500345 // all the non-local variations of the current module, plus the variations argument.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700346 //
347 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
348 // new dependencies have had the current mutator called on them. If the mutator is not
349 // parallel this method does not affect the ordering of the current mutator pass, but will
350 // be ordered correctly for all future mutator passes.
Usta Shresthac725f472022-01-11 02:44:21 -0500351 AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700352
353 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700354 // variations argument to select which variant of the dependency to use. It returns a slice of
355 // modules for each dependency (some entries may be nil). A variant of the dependency must
356 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700357 // For any unspecified variation the first variant will be used.
358 //
359 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
360 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700361 //
362 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
363 // new dependencies have had the current mutator called on them. If the mutator is not
364 // parallel this method does not affect the ordering of the current mutator pass, but will
365 // be ordered correctly for all future mutator passes.
366 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700367
368 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
369 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
370 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
371 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800372 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700373
374 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
375 // specified name with the current variant of this module. Replacements don't take effect until
376 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800377 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700378
379 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
380 // specified name with the current variant of this module as long as the supplied predicate returns
381 // true.
382 //
383 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100384 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700385
386 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
387 // and creates an alias from the current variant (before the mutator has run) to the new
388 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
389 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
390 // be used to add dependencies on the newly created variant using the variant map from
391 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800392 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700393
394 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
395 // module, and creates an alias from a new fromVariationName variant the toVariationName
396 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
397 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
398 // be used to add dependencies on the toVariationName variant using the fromVariationName
399 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700400 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700401
402 // SetVariationProvider sets the value for a provider for the given newly created variant of
403 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
404 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
405 // if the value is not of the appropriate type, or if the module is not a newly created
406 // variant of the current module. The value should not be modified after being passed to
407 // SetVariationProvider.
408 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700409}
410
Colin Cross25de6c32019-06-06 14:29:25 -0700411type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700412 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700413 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400414 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700415}
416
Colin Cross617b88a2020-08-24 18:04:09 -0700417func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500418 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700419
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400420 moduleContext := a.base().baseModuleContextFactory(ctx)
421 moduleContext.bazelConversionMode = bazelConversionMode
422
Colin Cross617b88a2020-08-24 18:04:09 -0700423 return &bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400424 bp: ctx,
Cole Faust0abd4b42023-01-10 10:49:18 -0800425 baseModuleContext: moduleContext,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400426 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700427 }
428}
429
Colin Cross25de6c32019-06-06 14:29:25 -0700430func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000431 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500432 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700433 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700434 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500435 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700436 }
Colin Cross798bfce2016-10-12 14:28:16 -0700437 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500438 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700439 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700440 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700441}
442
Colin Cross617b88a2020-08-24 18:04:09 -0700443func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
444 mutator := &mutator{name: name, bottomUpMutator: m}
445 x.mutators = append(x.mutators, mutator)
446 return mutator
447}
448
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200449type IncomingTransitionContext interface {
450 // Module returns the target of the dependency edge for which the transition
451 // is being computed
452 Module() Module
453
454 // Config returns the configuration for the build.
455 Config() Config
456}
457
458type OutgoingTransitionContext interface {
459 // Module returns the target of the dependency edge for which the transition
460 // is being computed
461 Module() Module
462
463 // DepTag() Returns the dependency tag through which this dependency is
464 // reached
465 DepTag() blueprint.DependencyTag
466}
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200467
468// Transition mutators implement a top-down mechanism where a module tells its
469// direct dependencies what variation they should be built in but the dependency
470// has the final say.
471//
472// When implementing a transition mutator, one needs to implement four methods:
473// - Split() that tells what variations a module has by itself
474// - OutgoingTransition() where a module tells what it wants from its
475// dependency
476// - IncomingTransition() where a module has the final say about its own
477// variation
478// - Mutate() that changes the state of a module depending on its variation
479//
480// That the effective variation of module B when depended on by module A is the
481// composition the outgoing transition of module A and the incoming transition
482// of module B.
483//
484// the outgoing transition should not take the properties of the dependency into
485// account, only those of the module that depends on it. For this reason, the
486// dependency is not even passed into it as an argument. Likewise, the incoming
487// transition should not take the properties of the depending module into
488// account and is thus not informed about it. This makes for a nice
489// decomposition of the decision logic.
490//
491// A given transition mutator only affects its own variation; other variations
492// stay unchanged along the dependency edges.
493//
494// Soong makes sure that all modules are created in the desired variations and
495// that dependency edges are set up correctly. This ensures that "missing
496// variation" errors do not happen and allows for more flexible changes in the
497// value of the variation among dependency edges (as oppposed to bottom-up
498// mutators where if module A in variation X depends on module B and module B
499// has that variation X, A must depend on variation X of B)
500//
501// The limited power of the context objects passed to individual mutators
502// methods also makes it more difficult to shoot oneself in the foot. Complete
503// safety is not guaranteed because no one prevents individual transition
504// mutators from mutating modules in illegal ways and for e.g. Split() or
505// Mutate() to run their own visitations of the transitive dependency of the
506// module and both of these are bad ideas, but it's better than no guardrails at
507// all.
508//
509// This model is pretty close to Bazel's configuration transitions. The mapping
510// between concepts in Soong and Bazel is as follows:
511// - Module == configured target
512// - Variant == configuration
513// - Variation name == configuration flag
514// - Variation == configuration flag value
515// - Outgoing transition == attribute transition
516// - Incoming transition == rule transition
517//
518// The Split() method does not have a Bazel equivalent and Bazel split
519// transitions do not have a Soong equivalent.
520//
521// Mutate() does not make sense in Bazel due to the different models of the
522// two systems: when creating new variations, Soong clones the old module and
523// thus some way is needed to change it state whereas Bazel creates each
524// configuration of a given configured target anew.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200525type TransitionMutator interface {
526 // Split returns the set of variations that should be created for a module no
527 // matter who depends on it. Used when Make depends on a particular variation
528 // or when the module knows its variations just based on information given to
529 // it in the Blueprint file. This method should not mutate the module it is
530 // called on.
531 Split(ctx BaseModuleContext) []string
532
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200533 // Called on a module to determine which variation it wants from its direct
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200534 // dependencies. The dependency itself can override this decision. This method
535 // should not mutate the module itself.
536 OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string
537
538 // Called on a module to determine which variation it should be in based on
539 // the variation modules that depend on it want. This gives the module a final
540 // say about its own variations. This method should not mutate the module
541 // itself.
542 IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string
543
544 // Called after a module was split into multiple variations on each variation.
545 // It should not split the module any further but adding new dependencies is
546 // fine. Unlike all the other methods on TransitionMutator, this method is
547 // allowed to mutate the module.
548 Mutate(ctx BottomUpMutatorContext, variation string)
549}
550
551type androidTransitionMutator struct {
552 finalPhase bool
553 bazelConversionMode bool
554 mutator TransitionMutator
555}
556
557func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []string {
558 if m, ok := ctx.Module().(Module); ok {
559 moduleContext := m.base().baseModuleContextFactory(ctx)
560 moduleContext.bazelConversionMode = a.bazelConversionMode
561 return a.mutator.Split(&moduleContext)
562 } else {
563 return []string{""}
564 }
565}
566
567type outgoingTransitionContextImpl struct {
568 bp blueprint.OutgoingTransitionContext
569}
570
571func (c *outgoingTransitionContextImpl) Module() Module {
572 return c.bp.Module().(Module)
573}
574
575func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag {
576 return c.bp.DepTag()
577}
578
579func (a *androidTransitionMutator) OutgoingTransition(ctx blueprint.OutgoingTransitionContext, sourceVariation string) string {
580 if _, ok := ctx.Module().(Module); ok {
581 return a.mutator.OutgoingTransition(&outgoingTransitionContextImpl{bp: ctx}, sourceVariation)
582 } else {
583 return ""
584 }
585}
586
587type incomingTransitionContextImpl struct {
588 bp blueprint.IncomingTransitionContext
589}
590
591func (c *incomingTransitionContextImpl) Module() Module {
592 return c.bp.Module().(Module)
593}
594
595func (c *incomingTransitionContextImpl) Config() Config {
596 return c.bp.Config().(Config)
597}
598
599func (a *androidTransitionMutator) IncomingTransition(ctx blueprint.IncomingTransitionContext, incomingVariation string) string {
600 if _, ok := ctx.Module().(Module); ok {
601 return a.mutator.IncomingTransition(&incomingTransitionContextImpl{bp: ctx}, incomingVariation)
602 } else {
603 return ""
604 }
605}
606
607func (a *androidTransitionMutator) Mutate(ctx blueprint.BottomUpMutatorContext, variation string) {
608 if am, ok := ctx.Module().(Module); ok {
609 a.mutator.Mutate(bottomUpMutatorContextFactory(ctx, am, a.finalPhase, a.bazelConversionMode), variation)
610 }
611}
612
613func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) {
614 atm := &androidTransitionMutator{
615 finalPhase: x.finalPhase,
616 bazelConversionMode: x.bazelConversionMode,
617 mutator: m,
618 }
619 mutator := &mutator{
620 name: name,
621 transitionMutator: atm}
622 x.mutators = append(x.mutators, mutator)
623}
624
Liz Kammer356f7d42021-01-26 09:18:53 -0500625func (x *registerMutatorsContext) mutatorName(name string) string {
626 if x.bazelConversionMode {
627 return name + "_bp2build"
628 }
629 return name
630}
631
Colin Cross25de6c32019-06-06 14:29:25 -0700632func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700633 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700634 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400635 moduleContext := a.base().baseModuleContextFactory(ctx)
636 moduleContext.bazelConversionMode = x.bazelConversionMode
Colin Cross25de6c32019-06-06 14:29:25 -0700637 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700638 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400639 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700640 }
Colin Cross798bfce2016-10-12 14:28:16 -0700641 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700642 }
Colin Cross798bfce2016-10-12 14:28:16 -0700643 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500644 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700645 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700646 return mutator
647}
648
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000649func (mutator *mutator) componentName() string {
650 return mutator.name
651}
652
653func (mutator *mutator) register(ctx *Context) {
654 blueprintCtx := ctx.Context
655 var handle blueprint.MutatorHandle
656 if mutator.bottomUpMutator != nil {
657 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
658 } else if mutator.topDownMutator != nil {
659 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200660 } else if mutator.transitionMutator != nil {
661 blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000662 }
663 if mutator.parallel {
664 handle.Parallel()
665 }
666}
667
Colin Cross798bfce2016-10-12 14:28:16 -0700668type MutatorHandle interface {
669 Parallel() MutatorHandle
670}
671
672func (mutator *mutator) Parallel() MutatorHandle {
673 mutator.parallel = true
674 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700675}
Colin Cross1e676be2016-10-12 14:38:15 -0700676
Paul Duffin44f1d842020-06-26 20:17:02 +0100677func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
678 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
679}
680
681// A special mutator that runs just prior to the deps mutator to allow the dependencies
682// on component modules to be added so that they can depend directly on a prebuilt
683// module.
684func componentDepsMutator(ctx BottomUpMutatorContext) {
685 if m := ctx.Module(); m.Enabled() {
686 m.ComponentDepsMutator(ctx)
687 }
688}
689
Colin Cross1e676be2016-10-12 14:38:15 -0700690func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100691 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700692 m.DepsMutator(ctx)
693 }
694}
Colin Crossd11fcda2017-10-23 17:59:01 -0700695
Liz Kammer356f7d42021-01-26 09:18:53 -0500696func registerDepsMutator(ctx RegisterMutatorsContext) {
697 ctx.BottomUp("deps", depsMutator).Parallel()
698}
699
700func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
701 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
702 // being converted to build targets.
703 ctx.BottomUp("deps", depsMutator).Parallel()
704}
705
Jingwen Chen1fd14692021-02-05 03:01:50 -0500706func (t *topDownMutatorContext) CreateBazelTargetModule(
Jingwen Chen1fd14692021-02-05 03:01:50 -0500707 bazelProps bazel.BazelTargetModuleProperties,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000708 commonAttrs CommonAttributes,
Liz Kammer2ada09a2021-08-11 00:17:36 -0400709 attrs interface{}) {
Chris Parsons58852a02021-12-09 18:10:18 -0500710 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, bazel.BoolAttribute{})
711}
712
713func (t *topDownMutatorContext) CreateBazelTargetModuleWithRestrictions(
714 bazelProps bazel.BazelTargetModuleProperties,
715 commonAttrs CommonAttributes,
716 attrs interface{},
717 enabledProperty bazel.BoolAttribute) {
718 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, enabledProperty)
719}
720
Spandan Dasabedff02023-03-07 19:24:34 +0000721var (
722 bazelAliasModuleProperties = bazel.BazelTargetModuleProperties{
723 Rule_class: "alias",
724 }
725)
726
727type bazelAliasAttributes struct {
728 Actual *bazel.LabelAttribute
729}
730
731func (t *topDownMutatorContext) CreateBazelTargetAliasInDir(
732 dir string,
733 name string,
734 actual bazel.Label) {
735 mod := t.Module()
736 attrs := &bazelAliasAttributes{
737 Actual: bazel.MakeLabelAttribute(actual.Label),
738 }
739 info := bp2buildInfo{
740 Dir: dir,
741 BazelProps: bazelAliasModuleProperties,
742 CommonAttrs: CommonAttributes{Name: name},
743 ConstraintAttrs: constraintAttributes{},
744 Attrs: attrs,
745 }
746 mod.base().addBp2buildInfo(info)
747}
748
Spandan Das6a448ec2023-04-19 17:36:12 +0000749func (t *topDownMutatorContext) CreateBazelConfigSetting(
750 csa bazel.ConfigSettingAttributes,
751 ca CommonAttributes,
752 dir string) {
753 mod := t.Module()
754 info := bp2buildInfo{
755 Dir: dir,
756 BazelProps: bazel.BazelTargetModuleProperties{
757 Rule_class: "config_setting",
758 },
759 CommonAttrs: ca,
760 ConstraintAttrs: constraintAttributes{},
761 Attrs: &csa,
762 }
763 mod.base().addBp2buildInfo(info)
764}
765
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000766// ApexAvailableTags converts the apex_available property value of an ApexModule
767// module and returns it as a list of keyed tags.
768func ApexAvailableTags(mod Module) bazel.StringListAttribute {
769 attr := bazel.StringListAttribute{}
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000770 // Transform specific attributes into tags.
771 if am, ok := mod.(ApexModule); ok {
772 // TODO(b/218841706): hidl_interface has the apex_available prop, but it's
773 // defined directly as a prop and not via ApexModule, so this doesn't
774 // pick those props up.
Spandan Das2dc6dfc2023-04-17 19:16:06 +0000775 apexAvailable := am.apexModuleBase().ApexAvailable()
776 // If a user does not specify apex_available in Android.bp, then soong provides a default.
777 // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
778 if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
779 apexAvailable = []string{}
780 }
781 attr.Value = ConvertApexAvailableToTags(apexAvailable)
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000782 }
783 return attr
784}
785
Spandan Dasf57a9662023-04-12 19:05:49 +0000786func ApexAvailableTagsWithoutTestApexes(ctx BaseModuleContext, mod Module) bazel.StringListAttribute {
787 attr := bazel.StringListAttribute{}
788 if am, ok := mod.(ApexModule); ok {
789 apexAvailableWithoutTestApexes := removeTestApexes(ctx, am.apexModuleBase().ApexAvailable())
790 // If a user does not specify apex_available in Android.bp, then soong provides a default.
791 // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
792 if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
793 apexAvailableWithoutTestApexes = []string{}
794 }
795 attr.Value = ConvertApexAvailableToTags(apexAvailableWithoutTestApexes)
796 }
797 return attr
798}
799
800func removeTestApexes(ctx BaseModuleContext, apex_available []string) []string {
801 testApexes := []string{}
802 for _, aa := range apex_available {
803 // ignore the wildcards
804 if InList(aa, AvailableToRecognziedWildcards) {
805 continue
806 }
807 mod, _ := ctx.ModuleFromName(aa)
808 if apex, ok := mod.(ApexTestInterface); ok && apex.IsTestApex() {
809 testApexes = append(testApexes, aa)
810 }
811 }
812 return RemoveListFromList(CopyOf(apex_available), testApexes)
813}
814
Cole Faustfb11c1c2023-02-10 11:27:46 -0800815func ConvertApexAvailableToTags(apexAvailable []string) []string {
816 if len(apexAvailable) == 0 {
817 // We need nil specifically to make bp2build not add the tags property at all,
818 // instead of adding it with an empty list
819 return nil
820 }
821 result := make([]string, 0, len(apexAvailable))
822 for _, a := range apexAvailable {
823 result = append(result, "apex_available="+a)
824 }
825 return result
826}
827
Spandan Das39b6cc52023-04-12 19:05:49 +0000828// ConvertApexAvailableToTagsWithoutTestApexes converts a list of apex names to a list of bazel tags
829// This function drops any test apexes from the input.
830func ConvertApexAvailableToTagsWithoutTestApexes(ctx BaseModuleContext, apexAvailable []string) []string {
831 noTestApexes := removeTestApexes(ctx, apexAvailable)
832 return ConvertApexAvailableToTags(noTestApexes)
833}
834
Chris Parsons58852a02021-12-09 18:10:18 -0500835func (t *topDownMutatorContext) createBazelTargetModule(
836 bazelProps bazel.BazelTargetModuleProperties,
837 commonAttrs CommonAttributes,
838 attrs interface{},
839 enabledProperty bazel.BoolAttribute) {
840 constraintAttributes := commonAttrs.fillCommonBp2BuildModuleAttrs(t, enabledProperty)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000841 mod := t.Module()
Liz Kammer2ada09a2021-08-11 00:17:36 -0400842 info := bp2buildInfo{
Chris Parsons58852a02021-12-09 18:10:18 -0500843 Dir: t.OtherModuleDir(mod),
844 BazelProps: bazelProps,
845 CommonAttrs: commonAttrs,
846 ConstraintAttrs: constraintAttributes,
847 Attrs: attrs,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500848 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000849 mod.base().addBp2buildInfo(info)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500850}
851
Colin Crossdc35e212019-06-06 16:13:11 -0700852// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
853// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
854// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
855// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
856// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
857
Colin Crosscb55e082019-07-01 15:32:31 -0700858func (t *topDownMutatorContext) MutatorName() string {
859 return t.bp.MutatorName()
860}
861
Colin Crossdc35e212019-06-06 16:13:11 -0700862func (t *topDownMutatorContext) Rename(name string) {
863 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700864 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700865}
866
Liz Kammerf31c9002022-04-26 09:08:55 -0400867func (t *topDownMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
868 return t.bp.CreateModule(factory, name, props...)
869}
870
Colin Crosse003c4a2019-09-25 12:58:36 -0700871func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400872 return createModule(t, factory, "_topDownMutatorModule", props...)
Colin Crossdc35e212019-06-06 16:13:11 -0700873}
874
Liz Kammera060c452021-03-24 10:14:47 -0400875func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400876 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), "", props...).(Module)
Liz Kammera060c452021-03-24 10:14:47 -0400877 return module
878}
879
Colin Crosscb55e082019-07-01 15:32:31 -0700880func (b *bottomUpMutatorContext) MutatorName() string {
881 return b.bp.MutatorName()
882}
883
Colin Crossdc35e212019-06-06 16:13:11 -0700884func (b *bottomUpMutatorContext) Rename(name string) {
885 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700886 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700887}
888
Colin Cross4f1dcb02020-09-16 18:45:04 -0700889func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400890 if b.baseModuleContext.checkedMissingDeps() {
891 panic("Adding deps not allowed after checking for missing deps")
892 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700893 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700894}
895
896func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400897 if b.baseModuleContext.checkedMissingDeps() {
898 panic("Adding deps not allowed after checking for missing deps")
899 }
Colin Crossdc35e212019-06-06 16:13:11 -0700900 b.bp.AddReverseDependency(module, tag, name)
901}
902
Colin Cross43b92e02019-11-18 15:28:57 -0800903func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000904 if b.finalPhase {
905 panic("CreateVariations not allowed in FinalDepsMutators")
906 }
907
Colin Cross9a362232019-07-01 15:32:45 -0700908 modules := b.bp.CreateVariations(variations...)
909
Colin Cross43b92e02019-11-18 15:28:57 -0800910 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700911 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800912 aModules[i] = modules[i].(Module)
913 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700914 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
915 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
916 }
917
Colin Cross43b92e02019-11-18 15:28:57 -0800918 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700919}
920
Colin Cross43b92e02019-11-18 15:28:57 -0800921func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000922 if b.finalPhase {
923 panic("CreateLocalVariations not allowed in FinalDepsMutators")
924 }
925
Colin Cross9a362232019-07-01 15:32:45 -0700926 modules := b.bp.CreateLocalVariations(variations...)
927
Colin Cross43b92e02019-11-18 15:28:57 -0800928 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700929 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800930 aModules[i] = modules[i].(Module)
931 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700932 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
933 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
934 }
935
Colin Cross43b92e02019-11-18 15:28:57 -0800936 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700937}
938
939func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
940 b.bp.SetDependencyVariation(variation)
941}
942
Jiyong Park1d1119f2019-07-29 21:27:18 +0900943func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
944 b.bp.SetDefaultDependencyVariation(variation)
945}
946
Colin Crossdc35e212019-06-06 16:13:11 -0700947func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700948 names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400949 if b.baseModuleContext.checkedMissingDeps() {
950 panic("Adding deps not allowed after checking for missing deps")
951 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700952 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700953}
954
955func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700956 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400957 if b.baseModuleContext.checkedMissingDeps() {
958 panic("Adding deps not allowed after checking for missing deps")
959 }
Colin Crossdc35e212019-06-06 16:13:11 -0700960
Colin Cross4f1dcb02020-09-16 18:45:04 -0700961 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700962}
963
964func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
965 b.bp.AddInterVariantDependency(tag, from, to)
966}
967
968func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400969 if b.baseModuleContext.checkedMissingDeps() {
970 panic("Adding deps not allowed after checking for missing deps")
971 }
Colin Crossdc35e212019-06-06 16:13:11 -0700972 b.bp.ReplaceDependencies(name)
973}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800974
Paul Duffin80342d72020-06-26 22:08:43 +0100975func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400976 if b.baseModuleContext.checkedMissingDeps() {
977 panic("Adding deps not allowed after checking for missing deps")
978 }
Paul Duffin80342d72020-06-26 22:08:43 +0100979 b.bp.ReplaceDependenciesIf(name, predicate)
980}
981
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800982func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
983 b.bp.AliasVariation(variationName)
984}
Colin Cross1b9604b2020-08-11 12:03:56 -0700985
986func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
987 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
988}
Colin Crossd27e7b82020-07-02 11:38:17 -0700989
990func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
991 b.bp.SetVariationProvider(module, provider, value)
992}