blob: 02a61435332bfd25d102f620725650fe2f1e6f1f [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) {
Liz Kammer356f7d42021-01-26 09:18:53 -050034 mctx := &registerMutatorsContext{
35 bazelConversionMode: true,
Jingwen Chen041b1842021-02-01 00:23:25 -050036 }
37
Liz Kammerbe46fcc2021-11-01 15:32:43 -040038 bp2buildMutators := append([]RegisterMutatorFunc{
Liz Kammer356f7d42021-01-26 09:18:53 -050039 RegisterNamespaceMutator,
40 RegisterDefaultsPreArchMutators,
41 // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should
42 // evaluate the impact on conversion.
43 RegisterPrebuiltsPreArchMutators,
44 },
45 preArchMutators...)
Liz Kammerbe46fcc2021-11-01 15:32:43 -040046 bp2buildMutators = append(bp2buildMutators, registerBp2buildConversionMutator)
Liz Kammer356f7d42021-01-26 09:18:53 -050047
Jingwen Chen73850672020-12-14 08:25:34 -050048 // Register bp2build mutators
49 for _, f := range bp2buildMutators {
50 f(mctx)
51 }
52
Paul Duffin1d2d42f2021-03-06 20:08:12 +000053 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050054}
55
Paul Duffinc05b0342021-03-06 13:28:13 +000056// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
57// with the InitRegistrationContext and will be used at runtime.
58func collateGloballyRegisteredMutators() sortableComponents {
59 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
60}
61
62// collateRegisteredMutators constructs a single list of mutators from the separate lists.
63func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070064 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080065
66 register := func(funcs []RegisterMutatorFunc) {
67 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070068 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080069 }
70 }
71
Colin Crosscec81712017-07-13 14:43:27 -070072 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080073
Colin Crosscec81712017-07-13 14:43:27 -070074 register(preDeps)
75
Liz Kammer356f7d42021-01-26 09:18:53 -050076 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070077
78 register(postDeps)
79
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000080 mctx.finalPhase = true
81 register(finalDeps)
82
Paul Duffinc05b0342021-03-06 13:28:13 +000083 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -070084}
85
86type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +000087 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -050088 finalPhase bool
89 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -070090}
Colin Cross1e676be2016-10-12 14:38:15 -070091
92type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -070093 TopDown(name string, m TopDownMutator) MutatorHandle
94 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -070095 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -070096}
97
98type RegisterMutatorFunc func(RegisterMutatorsContext)
99
Colin Crosscec81712017-07-13 14:43:27 -0700100var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800101 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100102
Paul Duffinaa4162e2020-05-05 11:35:43 +0100103 // Check the visibility rules are valid.
104 //
105 // This must run after the package renamer mutators so that any issues found during
106 // validation of the package's default_visibility property are reported using the
107 // correct package name and not the synthetic name.
108 //
109 // This must also be run before defaults mutators as the rules for validation are
110 // different before checking the rules than they are afterwards. e.g.
111 // visibility: ["//visibility:private", "//visibility:public"]
112 // would be invalid if specified in a module definition but is valid if it results
113 // from something like this:
114 //
115 // defaults {
116 // name: "defaults",
117 // // Be inaccessible outside a package by default.
118 // visibility: ["//visibility:private"]
119 // }
120 //
121 // defaultable_module {
122 // name: "defaultable_module",
123 // defaults: ["defaults"],
124 // // Override the default.
125 // visibility: ["//visibility:public"]
126 // }
127 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000128 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100129
Bob Badour37af0462021-01-07 03:34:31 +0000130 // Record the default_applicable_licenses for each package.
131 //
132 // This must run before the defaults so that defaults modules can pick up the package default.
133 RegisterLicensesPackageMapper,
134
Paul Duffinaa4162e2020-05-05 11:35:43 +0100135 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100136 //
137 // Any mutators that are added before this will not see any modules created by
138 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700139 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100140
Paul Duffin44f1d842020-06-26 20:17:02 +0100141 // Add dependencies on any components so that any component references can be
142 // resolved within the deps mutator.
143 //
144 // Must be run after defaults so it can be used to create dependencies on the
145 // component modules that are creating in a DefaultableHook.
146 //
147 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
148 // renamed. That is so that if a module creates components using a prebuilt module
149 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
150 // the prebuilt module and not the source module.
151 RegisterComponentsMutator,
152
Paul Duffinc988c8e2020-04-29 18:27:14 +0100153 // Create an association between prebuilt modules and their corresponding source
154 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100155 //
156 // Must be run after defaults mutators to ensure that any modules created by
157 // a DefaultableHook can be either a prebuilt or a source module with a matching
158 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100159 RegisterPrebuiltsPreArchMutators,
160
Bob Badour37af0462021-01-07 03:34:31 +0000161 // Gather the licenses properties for all modules for use during expansion and enforcement.
162 //
163 // This must come after the defaults mutators to ensure that any licenses supplied
164 // in a defaults module has been successfully applied before the rules are gathered.
165 RegisterLicensesPropertyGatherer,
166
Paul Duffinaa4162e2020-05-05 11:35:43 +0100167 // Gather the visibility rules for all modules for us during visibility enforcement.
168 //
169 // This must come after the defaults mutators to ensure that any visibility supplied
170 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000171 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700172}
173
Colin Crossae4c6182017-09-15 17:33:55 -0700174func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700175 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800176 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700177 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700178}
179
Colin Crosscec81712017-07-13 14:43:27 -0700180var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700181 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700182}
183
184var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800185 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700186 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000187 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000188 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100189 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700190 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700191}
Colin Cross1e676be2016-10-12 14:38:15 -0700192
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000193var finalDeps = []RegisterMutatorFunc{}
194
Colin Cross1e676be2016-10-12 14:38:15 -0700195func PreArchMutators(f RegisterMutatorFunc) {
196 preArch = append(preArch, f)
197}
198
199func PreDepsMutators(f RegisterMutatorFunc) {
200 preDeps = append(preDeps, f)
201}
202
203func PostDepsMutators(f RegisterMutatorFunc) {
204 postDeps = append(postDeps, f)
205}
206
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000207func FinalDepsMutators(f RegisterMutatorFunc) {
208 finalDeps = append(finalDeps, f)
209}
210
Liz Kammer356f7d42021-01-26 09:18:53 -0500211var bp2buildPreArchMutators = []RegisterMutatorFunc{}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400212
Liz Kammer12615db2021-09-28 09:19:17 -0400213// A minimal context for Bp2build conversion
214type Bp2buildMutatorContext interface {
215 BazelConversionPathContext
216
217 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
218}
219
Liz Kammer356f7d42021-01-26 09:18:53 -0500220// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
221// into Bazel BUILD targets that should run prior to deps and conversion.
222func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
223 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
224}
225
Colin Cross9f35c3d2020-09-16 19:04:41 -0700226type BaseMutatorContext interface {
227 BaseModuleContext
228
229 // MutatorName returns the name that this mutator was registered with.
230 MutatorName() string
231
232 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
233 // AddDependency or OtherModuleName until after this mutator pass is complete.
234 Rename(name string)
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400235
236 // BazelConversionMode returns whether this mutator is being run as part of Bazel Conversion.
237 BazelConversionMode() bool
Colin Cross9f35c3d2020-09-16 19:04:41 -0700238}
239
Colin Cross25de6c32019-06-06 14:29:25 -0700240type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700241
Colin Cross635c3b02016-05-18 15:37:25 -0700242type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700243 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700244
Colin Cross9f35c3d2020-09-16 19:04:41 -0700245 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
246 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700247 CreateModule(ModuleFactory, ...interface{}) Module
Jingwen Chen1fd14692021-02-05 03:01:50 -0500248
249 // CreateBazelTargetModule creates a BazelTargetModule by calling the
250 // factory method, just like in CreateModule, but also requires
251 // BazelTargetModuleProperties containing additional metadata for the
252 // bp2build codegenerator.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000253 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
Chris Parsons58852a02021-12-09 18:10:18 -0500254
255 // CreateBazelTargetModuleWithRestrictions creates a BazelTargetModule by calling the
256 // factory method, just like in CreateModule, but also requires
257 // BazelTargetModuleProperties containing additional metadata for the
258 // bp2build codegenerator. The generated target is restricted to only be buildable for certain
259 // platforms, as dictated by a given bool attribute: the target will not be buildable in
260 // any platform for which this bool attribute is false.
261 CreateBazelTargetModuleWithRestrictions(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}, bazel.BoolAttribute)
Colin Cross6362e272015-10-29 15:25:03 -0700262}
263
Colin Cross25de6c32019-06-06 14:29:25 -0700264type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700265 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700266 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700267}
268
Colin Cross25de6c32019-06-06 14:29:25 -0700269type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700270
Colin Cross635c3b02016-05-18 15:37:25 -0700271type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700272 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800273
Colin Cross4f1dcb02020-09-16 18:45:04 -0700274 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
275 // dependency (some entries may be nil).
276 //
277 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
278 // new dependencies have had the current mutator called on them. If the mutator is not
279 // parallel this method does not affect the ordering of the current mutator pass, but will
280 // be ordered correctly for all future mutator passes.
281 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700282
283 // AddReverseDependency adds a dependency from the destination to the given module.
284 // Does not affect the ordering of the current mutator pass, but will be ordered
285 // correctly for all future mutator passes. All reverse dependencies for a destination module are
286 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
287 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800288 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700289
290 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
291 // parameter. It returns a list of new modules in the same order as the variationNames
292 // list.
293 //
294 // If any of the dependencies of the module being operated on were already split
295 // by calling CreateVariations with the same name, the dependency will automatically
296 // be updated to point the matching variant.
297 //
298 // If a module is split, and then a module depending on the first module is not split
299 // when the Mutator is later called on it, the dependency of the depending module will
300 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800301 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700302
303 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
304 // parameter. It returns a list of new modules in the same order as the variantNames
305 // list.
306 //
307 // Local variations do not affect automatic dependency resolution - dependencies added
308 // to the split module via deps or DynamicDependerModule must exactly match a variant
309 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800310 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700311
312 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
313 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800314 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700315
316 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
317 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900318 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700319
320 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700321 // argument to select which variant of the dependency to use. It returns a slice of modules for
322 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
Usta Shresthac725f472022-01-11 02:44:21 -0500323 // all the non-local variations of the current module, plus the variations argument.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700324 //
325 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
326 // new dependencies have had the current mutator called on them. If the mutator is not
327 // parallel this method does not affect the ordering of the current mutator pass, but will
328 // be ordered correctly for all future mutator passes.
Usta Shresthac725f472022-01-11 02:44:21 -0500329 AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700330
331 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700332 // variations argument to select which variant of the dependency to use. It returns a slice of
333 // modules for each dependency (some entries may be nil). A variant of the dependency must
334 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700335 // For any unspecified variation the first variant will be used.
336 //
337 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
338 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700339 //
340 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
341 // new dependencies have had the current mutator called on them. If the mutator is not
342 // parallel this method does not affect the ordering of the current mutator pass, but will
343 // be ordered correctly for all future mutator passes.
344 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700345
346 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
347 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
348 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
349 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800350 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700351
352 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
353 // specified name with the current variant of this module. Replacements don't take effect until
354 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800355 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700356
357 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
358 // specified name with the current variant of this module as long as the supplied predicate returns
359 // true.
360 //
361 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100362 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700363
364 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
365 // and creates an alias from the current variant (before the mutator has run) to the new
366 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
367 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
368 // be used to add dependencies on the newly created variant using the variant map from
369 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800370 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700371
372 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
373 // module, and creates an alias from a new fromVariationName variant the toVariationName
374 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
375 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
376 // be used to add dependencies on the toVariationName variant using the fromVariationName
377 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700378 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700379
380 // SetVariationProvider sets the value for a provider for the given newly created variant of
381 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
382 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
383 // if the value is not of the appropriate type, or if the module is not a newly created
384 // variant of the current module. The value should not be modified after being passed to
385 // SetVariationProvider.
386 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700387}
388
Colin Cross25de6c32019-06-06 14:29:25 -0700389type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700390 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700391 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400392 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700393}
394
Colin Cross617b88a2020-08-24 18:04:09 -0700395func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500396 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700397
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400398 moduleContext := a.base().baseModuleContextFactory(ctx)
399 moduleContext.bazelConversionMode = bazelConversionMode
400
Colin Cross617b88a2020-08-24 18:04:09 -0700401 return &bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400402 bp: ctx,
403 baseModuleContext: a.base().baseModuleContextFactory(ctx),
404 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700405 }
406}
407
Colin Cross25de6c32019-06-06 14:29:25 -0700408func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000409 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500410 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700411 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700412 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500413 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700414 }
Colin Cross798bfce2016-10-12 14:28:16 -0700415 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500416 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700417 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700418 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700419}
420
Colin Cross617b88a2020-08-24 18:04:09 -0700421func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
422 mutator := &mutator{name: name, bottomUpMutator: m}
423 x.mutators = append(x.mutators, mutator)
424 return mutator
425}
426
Liz Kammer356f7d42021-01-26 09:18:53 -0500427func (x *registerMutatorsContext) mutatorName(name string) string {
428 if x.bazelConversionMode {
429 return name + "_bp2build"
430 }
431 return name
432}
433
Colin Cross25de6c32019-06-06 14:29:25 -0700434func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700435 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700436 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400437 moduleContext := a.base().baseModuleContextFactory(ctx)
438 moduleContext.bazelConversionMode = x.bazelConversionMode
Colin Cross25de6c32019-06-06 14:29:25 -0700439 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700440 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400441 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700442 }
Colin Cross798bfce2016-10-12 14:28:16 -0700443 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700444 }
Colin Cross798bfce2016-10-12 14:28:16 -0700445 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500446 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700447 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700448 return mutator
449}
450
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000451func (mutator *mutator) componentName() string {
452 return mutator.name
453}
454
455func (mutator *mutator) register(ctx *Context) {
456 blueprintCtx := ctx.Context
457 var handle blueprint.MutatorHandle
458 if mutator.bottomUpMutator != nil {
459 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
460 } else if mutator.topDownMutator != nil {
461 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
462 }
463 if mutator.parallel {
464 handle.Parallel()
465 }
466}
467
Colin Cross798bfce2016-10-12 14:28:16 -0700468type MutatorHandle interface {
469 Parallel() MutatorHandle
470}
471
472func (mutator *mutator) Parallel() MutatorHandle {
473 mutator.parallel = true
474 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700475}
Colin Cross1e676be2016-10-12 14:38:15 -0700476
Paul Duffin44f1d842020-06-26 20:17:02 +0100477func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
478 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
479}
480
481// A special mutator that runs just prior to the deps mutator to allow the dependencies
482// on component modules to be added so that they can depend directly on a prebuilt
483// module.
484func componentDepsMutator(ctx BottomUpMutatorContext) {
485 if m := ctx.Module(); m.Enabled() {
486 m.ComponentDepsMutator(ctx)
487 }
488}
489
Colin Cross1e676be2016-10-12 14:38:15 -0700490func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100491 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700492 m.DepsMutator(ctx)
493 }
494}
Colin Crossd11fcda2017-10-23 17:59:01 -0700495
Liz Kammer356f7d42021-01-26 09:18:53 -0500496func registerDepsMutator(ctx RegisterMutatorsContext) {
497 ctx.BottomUp("deps", depsMutator).Parallel()
498}
499
500func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
501 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
502 // being converted to build targets.
503 ctx.BottomUp("deps", depsMutator).Parallel()
504}
505
Jingwen Chen1fd14692021-02-05 03:01:50 -0500506func (t *topDownMutatorContext) CreateBazelTargetModule(
Jingwen Chen1fd14692021-02-05 03:01:50 -0500507 bazelProps bazel.BazelTargetModuleProperties,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000508 commonAttrs CommonAttributes,
Liz Kammer2ada09a2021-08-11 00:17:36 -0400509 attrs interface{}) {
Chris Parsons58852a02021-12-09 18:10:18 -0500510 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, bazel.BoolAttribute{})
511}
512
513func (t *topDownMutatorContext) CreateBazelTargetModuleWithRestrictions(
514 bazelProps bazel.BazelTargetModuleProperties,
515 commonAttrs CommonAttributes,
516 attrs interface{},
517 enabledProperty bazel.BoolAttribute) {
518 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, enabledProperty)
519}
520
521func (t *topDownMutatorContext) createBazelTargetModule(
522 bazelProps bazel.BazelTargetModuleProperties,
523 commonAttrs CommonAttributes,
524 attrs interface{},
525 enabledProperty bazel.BoolAttribute) {
526 constraintAttributes := commonAttrs.fillCommonBp2BuildModuleAttrs(t, enabledProperty)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000527 mod := t.Module()
Liz Kammer2ada09a2021-08-11 00:17:36 -0400528 info := bp2buildInfo{
Chris Parsons58852a02021-12-09 18:10:18 -0500529 Dir: t.OtherModuleDir(mod),
530 BazelProps: bazelProps,
531 CommonAttrs: commonAttrs,
532 ConstraintAttrs: constraintAttributes,
533 Attrs: attrs,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500534 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000535 mod.base().addBp2buildInfo(info)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500536}
537
Colin Crossdc35e212019-06-06 16:13:11 -0700538// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
539// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
540// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
541// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
542// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
543
Colin Crosscb55e082019-07-01 15:32:31 -0700544func (t *topDownMutatorContext) MutatorName() string {
545 return t.bp.MutatorName()
546}
547
Colin Crossdc35e212019-06-06 16:13:11 -0700548func (t *topDownMutatorContext) Rename(name string) {
549 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700550 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700551}
552
Liz Kammerf31c9002022-04-26 09:08:55 -0400553func (t *topDownMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
554 return t.bp.CreateModule(factory, name, props...)
555}
556
Colin Crosse003c4a2019-09-25 12:58:36 -0700557func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400558 return createModule(t, factory, "_topDownMutatorModule", props...)
Colin Crossdc35e212019-06-06 16:13:11 -0700559}
560
Liz Kammera060c452021-03-24 10:14:47 -0400561func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400562 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), "", props...).(Module)
Liz Kammera060c452021-03-24 10:14:47 -0400563 return module
564}
565
Colin Crosscb55e082019-07-01 15:32:31 -0700566func (b *bottomUpMutatorContext) MutatorName() string {
567 return b.bp.MutatorName()
568}
569
Colin Crossdc35e212019-06-06 16:13:11 -0700570func (b *bottomUpMutatorContext) Rename(name string) {
571 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700572 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700573}
574
Colin Cross4f1dcb02020-09-16 18:45:04 -0700575func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
576 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700577}
578
579func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
580 b.bp.AddReverseDependency(module, tag, name)
581}
582
Colin Cross43b92e02019-11-18 15:28:57 -0800583func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000584 if b.finalPhase {
585 panic("CreateVariations not allowed in FinalDepsMutators")
586 }
587
Colin Cross9a362232019-07-01 15:32:45 -0700588 modules := b.bp.CreateVariations(variations...)
589
Colin Cross43b92e02019-11-18 15:28:57 -0800590 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700591 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800592 aModules[i] = modules[i].(Module)
593 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700594 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
595 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
596 }
597
Colin Cross43b92e02019-11-18 15:28:57 -0800598 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700599}
600
Colin Cross43b92e02019-11-18 15:28:57 -0800601func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000602 if b.finalPhase {
603 panic("CreateLocalVariations not allowed in FinalDepsMutators")
604 }
605
Colin Cross9a362232019-07-01 15:32:45 -0700606 modules := b.bp.CreateLocalVariations(variations...)
607
Colin Cross43b92e02019-11-18 15:28:57 -0800608 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700609 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800610 aModules[i] = modules[i].(Module)
611 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700612 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
613 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
614 }
615
Colin Cross43b92e02019-11-18 15:28:57 -0800616 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700617}
618
619func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
620 b.bp.SetDependencyVariation(variation)
621}
622
Jiyong Park1d1119f2019-07-29 21:27:18 +0900623func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
624 b.bp.SetDefaultDependencyVariation(variation)
625}
626
Colin Crossdc35e212019-06-06 16:13:11 -0700627func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700628 names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500629 if b.bazelConversionMode {
630 _, noSelfDeps := RemoveFromList(b.ModuleName(), names)
631 if len(noSelfDeps) == 0 {
632 return []blueprint.Module(nil)
633 }
634 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
635 // dependency, the variations would not exist and the dependency could not be added, by
636 // specifying no variations, we will allow adding the dependency to succeed.
637 return b.bp.AddFarVariationDependencies(nil, tag, noSelfDeps...)
638 }
Colin Crossdc35e212019-06-06 16:13:11 -0700639
Colin Cross4f1dcb02020-09-16 18:45:04 -0700640 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700641}
642
643func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700644 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500645 if b.bazelConversionMode {
646 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
647 // dependency, the variations would not exist and the dependency could not be added, by
648 // specifying no variations, we will allow adding the dependency to succeed.
649 return b.bp.AddFarVariationDependencies(nil, tag, names...)
650 }
Colin Crossdc35e212019-06-06 16:13:11 -0700651
Colin Cross4f1dcb02020-09-16 18:45:04 -0700652 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700653}
654
655func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
656 b.bp.AddInterVariantDependency(tag, from, to)
657}
658
659func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
660 b.bp.ReplaceDependencies(name)
661}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800662
Paul Duffin80342d72020-06-26 22:08:43 +0100663func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
664 b.bp.ReplaceDependenciesIf(name, predicate)
665}
666
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800667func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
668 b.bp.AliasVariation(variationName)
669}
Colin Cross1b9604b2020-08-11 12:03:56 -0700670
671func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
672 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
673}
Colin Crossd27e7b82020-07-02 11:38:17 -0700674
675func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
676 b.bp.SetVariationProvider(module, provider, value)
677}