blob: dbd8c04db5b292b2eab9153512b72e4aba4fc833 [file] [log] [blame]
Colin Cross6362e272015-10-29 15:25:03 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross6362e272015-10-29 15:25:03 -070016
Colin Cross795c3772017-03-16 16:50:10 -070017import (
Colin Cross18c46802019-09-24 22:19:02 -070018 "reflect"
19
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000020 "android/soong/bazel"
21
Colin Cross795c3772017-03-16 16:50:10 -070022 "github.com/google/blueprint"
Colin Cross519917d2017-11-02 16:35:56 -070023 "github.com/google/blueprint/proptools"
Colin Cross795c3772017-03-16 16:50:10 -070024)
Colin Cross6362e272015-10-29 15:25:03 -070025
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070026// Phases:
27// run Pre-arch mutators
28// run archMutator
29// run Pre-deps mutators
30// run depsMutator
31// run PostDeps mutators
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000032// run FinalDeps mutators (CreateVariations disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070033// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070034
Jingwen Chen73850672020-12-14 08:25:34 -050035// RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
Liz Kammerbe46fcc2021-11-01 15:32:43 -040036func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) {
Liz Kammer356f7d42021-01-26 09:18:53 -050037 mctx := &registerMutatorsContext{
38 bazelConversionMode: true,
Jingwen Chen041b1842021-02-01 00:23:25 -050039 }
40
Liz Kammerbe46fcc2021-11-01 15:32:43 -040041 bp2buildMutators := append([]RegisterMutatorFunc{
Liz Kammer356f7d42021-01-26 09:18:53 -050042 RegisterNamespaceMutator,
43 RegisterDefaultsPreArchMutators,
44 // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should
45 // evaluate the impact on conversion.
46 RegisterPrebuiltsPreArchMutators,
47 },
48 preArchMutators...)
Liz Kammerbe46fcc2021-11-01 15:32:43 -040049 bp2buildMutators = append(bp2buildMutators, registerBp2buildConversionMutator)
Liz Kammer356f7d42021-01-26 09:18:53 -050050
Jingwen Chen73850672020-12-14 08:25:34 -050051 // Register bp2build mutators
52 for _, f := range bp2buildMutators {
53 f(mctx)
54 }
55
Paul Duffin1d2d42f2021-03-06 20:08:12 +000056 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050057}
58
Paul Duffinc05b0342021-03-06 13:28:13 +000059// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
60// with the InitRegistrationContext and will be used at runtime.
61func collateGloballyRegisteredMutators() sortableComponents {
62 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
63}
64
65// collateRegisteredMutators constructs a single list of mutators from the separate lists.
66func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070067 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080068
69 register := func(funcs []RegisterMutatorFunc) {
70 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070071 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080072 }
73 }
74
Colin Crosscec81712017-07-13 14:43:27 -070075 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080076
Colin Crosscec81712017-07-13 14:43:27 -070077 register(preDeps)
78
Liz Kammer356f7d42021-01-26 09:18:53 -050079 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070080
81 register(postDeps)
82
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000083 mctx.finalPhase = true
84 register(finalDeps)
85
Paul Duffinc05b0342021-03-06 13:28:13 +000086 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -070087}
88
89type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +000090 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -050091 finalPhase bool
92 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -070093}
Colin Cross1e676be2016-10-12 14:38:15 -070094
95type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -070096 TopDown(name string, m TopDownMutator) MutatorHandle
97 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -070098 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -070099}
100
101type RegisterMutatorFunc func(RegisterMutatorsContext)
102
Colin Crosscec81712017-07-13 14:43:27 -0700103var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800104 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100105
Paul Duffinaa4162e2020-05-05 11:35:43 +0100106 // Check the visibility rules are valid.
107 //
108 // This must run after the package renamer mutators so that any issues found during
109 // validation of the package's default_visibility property are reported using the
110 // correct package name and not the synthetic name.
111 //
112 // This must also be run before defaults mutators as the rules for validation are
113 // different before checking the rules than they are afterwards. e.g.
114 // visibility: ["//visibility:private", "//visibility:public"]
115 // would be invalid if specified in a module definition but is valid if it results
116 // from something like this:
117 //
118 // defaults {
119 // name: "defaults",
120 // // Be inaccessible outside a package by default.
121 // visibility: ["//visibility:private"]
122 // }
123 //
124 // defaultable_module {
125 // name: "defaultable_module",
126 // defaults: ["defaults"],
127 // // Override the default.
128 // visibility: ["//visibility:public"]
129 // }
130 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000131 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100132
Bob Badour37af0462021-01-07 03:34:31 +0000133 // Record the default_applicable_licenses for each package.
134 //
135 // This must run before the defaults so that defaults modules can pick up the package default.
136 RegisterLicensesPackageMapper,
137
Paul Duffinaa4162e2020-05-05 11:35:43 +0100138 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100139 //
140 // Any mutators that are added before this will not see any modules created by
141 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700142 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100143
Paul Duffin44f1d842020-06-26 20:17:02 +0100144 // Add dependencies on any components so that any component references can be
145 // resolved within the deps mutator.
146 //
147 // Must be run after defaults so it can be used to create dependencies on the
148 // component modules that are creating in a DefaultableHook.
149 //
150 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
151 // renamed. That is so that if a module creates components using a prebuilt module
152 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
153 // the prebuilt module and not the source module.
154 RegisterComponentsMutator,
155
Paul Duffinc988c8e2020-04-29 18:27:14 +0100156 // Create an association between prebuilt modules and their corresponding source
157 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100158 //
159 // Must be run after defaults mutators to ensure that any modules created by
160 // a DefaultableHook can be either a prebuilt or a source module with a matching
161 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100162 RegisterPrebuiltsPreArchMutators,
163
Bob Badour37af0462021-01-07 03:34:31 +0000164 // Gather the licenses properties for all modules for use during expansion and enforcement.
165 //
166 // This must come after the defaults mutators to ensure that any licenses supplied
167 // in a defaults module has been successfully applied before the rules are gathered.
168 RegisterLicensesPropertyGatherer,
169
Paul Duffinaa4162e2020-05-05 11:35:43 +0100170 // Gather the visibility rules for all modules for us during visibility enforcement.
171 //
172 // This must come after the defaults mutators to ensure that any visibility supplied
173 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000174 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700175}
176
Colin Crossae4c6182017-09-15 17:33:55 -0700177func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700178 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800179 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700180 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700181}
182
Colin Crosscec81712017-07-13 14:43:27 -0700183var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700184 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700185}
186
187var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800188 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700189 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000190 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000191 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100192 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700193 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700194}
Colin Cross1e676be2016-10-12 14:38:15 -0700195
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000196var finalDeps = []RegisterMutatorFunc{}
197
Colin Cross1e676be2016-10-12 14:38:15 -0700198func PreArchMutators(f RegisterMutatorFunc) {
199 preArch = append(preArch, f)
200}
201
202func PreDepsMutators(f RegisterMutatorFunc) {
203 preDeps = append(preDeps, f)
204}
205
206func PostDepsMutators(f RegisterMutatorFunc) {
207 postDeps = append(postDeps, f)
208}
209
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000210func FinalDepsMutators(f RegisterMutatorFunc) {
211 finalDeps = append(finalDeps, f)
212}
213
Liz Kammer356f7d42021-01-26 09:18:53 -0500214var bp2buildPreArchMutators = []RegisterMutatorFunc{}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400215
Liz Kammer12615db2021-09-28 09:19:17 -0400216// A minimal context for Bp2build conversion
217type Bp2buildMutatorContext interface {
218 BazelConversionPathContext
219
220 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
221}
222
Liz Kammer356f7d42021-01-26 09:18:53 -0500223// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
224// into Bazel BUILD targets that should run prior to deps and conversion.
225func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
226 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
227}
228
Colin Cross9f35c3d2020-09-16 19:04:41 -0700229type BaseMutatorContext interface {
230 BaseModuleContext
231
232 // MutatorName returns the name that this mutator was registered with.
233 MutatorName() string
234
235 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
236 // AddDependency or OtherModuleName until after this mutator pass is complete.
237 Rename(name string)
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400238
239 // BazelConversionMode returns whether this mutator is being run as part of Bazel Conversion.
240 BazelConversionMode() bool
Colin Cross9f35c3d2020-09-16 19:04:41 -0700241}
242
Colin Cross25de6c32019-06-06 14:29:25 -0700243type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700244
Colin Cross635c3b02016-05-18 15:37:25 -0700245type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700246 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700247
Colin Cross9f35c3d2020-09-16 19:04:41 -0700248 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
249 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700250 CreateModule(ModuleFactory, ...interface{}) Module
Jingwen Chen1fd14692021-02-05 03:01:50 -0500251
252 // CreateBazelTargetModule creates a BazelTargetModule by calling the
253 // factory method, just like in CreateModule, but also requires
254 // BazelTargetModuleProperties containing additional metadata for the
255 // bp2build codegenerator.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000256 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700257}
258
Colin Cross25de6c32019-06-06 14:29:25 -0700259type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700260 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700261 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700262}
263
Colin Cross25de6c32019-06-06 14:29:25 -0700264type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700265
Colin Cross635c3b02016-05-18 15:37:25 -0700266type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700267 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800268
Colin Cross4f1dcb02020-09-16 18:45:04 -0700269 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
270 // dependency (some entries may be nil).
271 //
272 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
273 // new dependencies have had the current mutator called on them. If the mutator is not
274 // parallel this method does not affect the ordering of the current mutator pass, but will
275 // be ordered correctly for all future mutator passes.
276 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700277
278 // AddReverseDependency adds a dependency from the destination to the given module.
279 // Does not affect the ordering of the current mutator pass, but will be ordered
280 // correctly for all future mutator passes. All reverse dependencies for a destination module are
281 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
282 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800283 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700284
285 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
286 // parameter. It returns a list of new modules in the same order as the variationNames
287 // list.
288 //
289 // If any of the dependencies of the module being operated on were already split
290 // by calling CreateVariations with the same name, the dependency will automatically
291 // be updated to point the matching variant.
292 //
293 // If a module is split, and then a module depending on the first module is not split
294 // when the Mutator is later called on it, the dependency of the depending module will
295 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800296 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700297
298 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
299 // parameter. It returns a list of new modules in the same order as the variantNames
300 // list.
301 //
302 // Local variations do not affect automatic dependency resolution - dependencies added
303 // to the split module via deps or DynamicDependerModule must exactly match a variant
304 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800305 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700306
307 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
308 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800309 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700310
311 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
312 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900313 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700314
315 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700316 // argument to select which variant of the dependency to use. It returns a slice of modules for
317 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
318 // the all of the non-local variations of the current module, plus the variations argument.
319 //
320 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
321 // new dependencies have had the current mutator called on them. If the mutator is not
322 // parallel this method does not affect the ordering of the current mutator pass, but will
323 // be ordered correctly for all future mutator passes.
324 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700325
326 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700327 // variations argument to select which variant of the dependency to use. It returns a slice of
328 // modules for each dependency (some entries may be nil). A variant of the dependency must
329 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700330 // For any unspecified variation the first variant will be used.
331 //
332 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
333 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700334 //
335 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
336 // new dependencies have had the current mutator called on them. If the mutator is not
337 // parallel this method does not affect the ordering of the current mutator pass, but will
338 // be ordered correctly for all future mutator passes.
339 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700340
341 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
342 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
343 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
344 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800345 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700346
347 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
348 // specified name with the current variant of this module. Replacements don't take effect until
349 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800350 ReplaceDependencies(string)
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 as long as the supplied predicate returns
354 // true.
355 //
356 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100357 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700358
359 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
360 // and creates an alias from the current variant (before the mutator has run) to the new
361 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
362 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
363 // be used to add dependencies on the newly created variant using the variant map from
364 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800365 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700366
367 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
368 // module, and creates an alias from a new fromVariationName variant the toVariationName
369 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
370 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
371 // be used to add dependencies on the toVariationName variant using the fromVariationName
372 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700373 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700374
375 // SetVariationProvider sets the value for a provider for the given newly created variant of
376 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
377 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
378 // if the value is not of the appropriate type, or if the module is not a newly created
379 // variant of the current module. The value should not be modified after being passed to
380 // SetVariationProvider.
381 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700382}
383
Colin Cross25de6c32019-06-06 14:29:25 -0700384type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700385 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700386 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400387 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700388}
389
Colin Cross617b88a2020-08-24 18:04:09 -0700390func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500391 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700392
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400393 moduleContext := a.base().baseModuleContextFactory(ctx)
394 moduleContext.bazelConversionMode = bazelConversionMode
395
Colin Cross617b88a2020-08-24 18:04:09 -0700396 return &bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400397 bp: ctx,
398 baseModuleContext: a.base().baseModuleContextFactory(ctx),
399 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700400 }
401}
402
Colin Cross25de6c32019-06-06 14:29:25 -0700403func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000404 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500405 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700406 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700407 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500408 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700409 }
Colin Cross798bfce2016-10-12 14:28:16 -0700410 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500411 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700412 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700413 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700414}
415
Colin Cross617b88a2020-08-24 18:04:09 -0700416func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
417 mutator := &mutator{name: name, bottomUpMutator: m}
418 x.mutators = append(x.mutators, mutator)
419 return mutator
420}
421
Liz Kammer356f7d42021-01-26 09:18:53 -0500422func (x *registerMutatorsContext) mutatorName(name string) string {
423 if x.bazelConversionMode {
424 return name + "_bp2build"
425 }
426 return name
427}
428
Colin Cross25de6c32019-06-06 14:29:25 -0700429func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700430 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700431 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400432 moduleContext := a.base().baseModuleContextFactory(ctx)
433 moduleContext.bazelConversionMode = x.bazelConversionMode
Colin Cross25de6c32019-06-06 14:29:25 -0700434 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700435 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400436 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700437 }
Colin Cross798bfce2016-10-12 14:28:16 -0700438 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700439 }
Colin Cross798bfce2016-10-12 14:28:16 -0700440 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500441 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700442 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700443 return mutator
444}
445
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000446func (mutator *mutator) componentName() string {
447 return mutator.name
448}
449
450func (mutator *mutator) register(ctx *Context) {
451 blueprintCtx := ctx.Context
452 var handle blueprint.MutatorHandle
453 if mutator.bottomUpMutator != nil {
454 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
455 } else if mutator.topDownMutator != nil {
456 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
457 }
458 if mutator.parallel {
459 handle.Parallel()
460 }
461}
462
Colin Cross798bfce2016-10-12 14:28:16 -0700463type MutatorHandle interface {
464 Parallel() MutatorHandle
465}
466
467func (mutator *mutator) Parallel() MutatorHandle {
468 mutator.parallel = true
469 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700470}
Colin Cross1e676be2016-10-12 14:38:15 -0700471
Paul Duffin44f1d842020-06-26 20:17:02 +0100472func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
473 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
474}
475
476// A special mutator that runs just prior to the deps mutator to allow the dependencies
477// on component modules to be added so that they can depend directly on a prebuilt
478// module.
479func componentDepsMutator(ctx BottomUpMutatorContext) {
480 if m := ctx.Module(); m.Enabled() {
481 m.ComponentDepsMutator(ctx)
482 }
483}
484
Colin Cross1e676be2016-10-12 14:38:15 -0700485func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100486 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700487 m.DepsMutator(ctx)
488 }
489}
Colin Crossd11fcda2017-10-23 17:59:01 -0700490
Liz Kammer356f7d42021-01-26 09:18:53 -0500491func registerDepsMutator(ctx RegisterMutatorsContext) {
492 ctx.BottomUp("deps", depsMutator).Parallel()
493}
494
495func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
496 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
497 // being converted to build targets.
498 ctx.BottomUp("deps", depsMutator).Parallel()
499}
500
Jingwen Chen1fd14692021-02-05 03:01:50 -0500501func (t *topDownMutatorContext) CreateBazelTargetModule(
Jingwen Chen1fd14692021-02-05 03:01:50 -0500502 bazelProps bazel.BazelTargetModuleProperties,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000503 commonAttrs CommonAttributes,
Liz Kammer2ada09a2021-08-11 00:17:36 -0400504 attrs interface{}) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000505 commonAttrs.fillCommonBp2BuildModuleAttrs(t)
506 mod := t.Module()
Liz Kammer2ada09a2021-08-11 00:17:36 -0400507 info := bp2buildInfo{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000508 Dir: t.OtherModuleDir(mod),
509 BazelProps: bazelProps,
510 CommonAttrs: commonAttrs,
511 Attrs: attrs,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500512 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000513 mod.base().addBp2buildInfo(info)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500514}
515
Colin Crossdc35e212019-06-06 16:13:11 -0700516// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
517// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
518// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
519// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
520// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
521
Colin Crosscb55e082019-07-01 15:32:31 -0700522func (t *topDownMutatorContext) MutatorName() string {
523 return t.bp.MutatorName()
524}
525
Colin Crossdc35e212019-06-06 16:13:11 -0700526func (t *topDownMutatorContext) Rename(name string) {
527 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700528 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700529}
530
Colin Crosse003c4a2019-09-25 12:58:36 -0700531func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Colin Cross18c46802019-09-24 22:19:02 -0700532 inherited := []interface{}{&t.Module().base().commonProperties}
Colin Crosse003c4a2019-09-25 12:58:36 -0700533 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
Colin Cross18c46802019-09-24 22:19:02 -0700534
535 if t.Module().base().variableProperties != nil && module.base().variableProperties != nil {
536 src := t.Module().base().variableProperties
537 dst := []interface{}{
538 module.base().variableProperties,
539 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
540 // don't cause a "failed to find property to extend" error.
Colin Cross43e789d2020-01-28 09:46:50 -0800541 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross18c46802019-09-24 22:19:02 -0700542 }
543 err := proptools.AppendMatchingProperties(dst, src, nil)
544 if err != nil {
545 panic(err)
546 }
547 }
548
Colin Crosse003c4a2019-09-25 12:58:36 -0700549 return module
Colin Crossdc35e212019-06-06 16:13:11 -0700550}
551
Liz Kammera060c452021-03-24 10:14:47 -0400552func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
553 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), props...).(Module)
554 return module
555}
556
Colin Crosscb55e082019-07-01 15:32:31 -0700557func (b *bottomUpMutatorContext) MutatorName() string {
558 return b.bp.MutatorName()
559}
560
Colin Crossdc35e212019-06-06 16:13:11 -0700561func (b *bottomUpMutatorContext) Rename(name string) {
562 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700563 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700564}
565
Colin Cross4f1dcb02020-09-16 18:45:04 -0700566func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
567 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700568}
569
570func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
571 b.bp.AddReverseDependency(module, tag, name)
572}
573
Colin Cross43b92e02019-11-18 15:28:57 -0800574func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000575 if b.finalPhase {
576 panic("CreateVariations not allowed in FinalDepsMutators")
577 }
578
Colin Cross9a362232019-07-01 15:32:45 -0700579 modules := b.bp.CreateVariations(variations...)
580
Colin Cross43b92e02019-11-18 15:28:57 -0800581 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700582 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800583 aModules[i] = modules[i].(Module)
584 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700585 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
586 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
587 }
588
Colin Cross43b92e02019-11-18 15:28:57 -0800589 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700590}
591
Colin Cross43b92e02019-11-18 15:28:57 -0800592func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000593 if b.finalPhase {
594 panic("CreateLocalVariations not allowed in FinalDepsMutators")
595 }
596
Colin Cross9a362232019-07-01 15:32:45 -0700597 modules := b.bp.CreateLocalVariations(variations...)
598
Colin Cross43b92e02019-11-18 15:28:57 -0800599 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700600 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800601 aModules[i] = modules[i].(Module)
602 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700603 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
604 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
605 }
606
Colin Cross43b92e02019-11-18 15:28:57 -0800607 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700608}
609
610func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
611 b.bp.SetDependencyVariation(variation)
612}
613
Jiyong Park1d1119f2019-07-29 21:27:18 +0900614func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
615 b.bp.SetDefaultDependencyVariation(variation)
616}
617
Colin Crossdc35e212019-06-06 16:13:11 -0700618func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700619 names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500620 if b.bazelConversionMode {
621 _, noSelfDeps := RemoveFromList(b.ModuleName(), names)
622 if len(noSelfDeps) == 0 {
623 return []blueprint.Module(nil)
624 }
625 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
626 // dependency, the variations would not exist and the dependency could not be added, by
627 // specifying no variations, we will allow adding the dependency to succeed.
628 return b.bp.AddFarVariationDependencies(nil, tag, noSelfDeps...)
629 }
Colin Crossdc35e212019-06-06 16:13:11 -0700630
Colin Cross4f1dcb02020-09-16 18:45:04 -0700631 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700632}
633
634func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700635 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500636 if b.bazelConversionMode {
637 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
638 // dependency, the variations would not exist and the dependency could not be added, by
639 // specifying no variations, we will allow adding the dependency to succeed.
640 return b.bp.AddFarVariationDependencies(nil, tag, names...)
641 }
Colin Crossdc35e212019-06-06 16:13:11 -0700642
Colin Cross4f1dcb02020-09-16 18:45:04 -0700643 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700644}
645
646func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
647 b.bp.AddInterVariantDependency(tag, from, to)
648}
649
650func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
651 b.bp.ReplaceDependencies(name)
652}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800653
Paul Duffin80342d72020-06-26 22:08:43 +0100654func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
655 b.bp.ReplaceDependenciesIf(name, predicate)
656}
657
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800658func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
659 b.bp.AliasVariation(variationName)
660}
Colin Cross1b9604b2020-08-11 12:03:56 -0700661
662func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
663 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
664}
Colin Crossd27e7b82020-07-02 11:38:17 -0700665
666func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
667 b.bp.SetVariationProvider(module, provider, value)
668}