blob: b361c51cee561b8df1b072072e9e1f613150b9e9 [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 (
Jingwen Chen1fd14692021-02-05 03:01:50 -050018 "android/soong/bazel"
Colin Cross18c46802019-09-24 22:19:02 -070019 "reflect"
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -040020 "sync"
Colin Cross18c46802019-09-24 22:19:02 -070021
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.
Chris Parsons5a34ffb2021-07-21 14:34:58 -040036func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators, bp2buildMutators []RegisterMutatorFunc) {
Liz Kammer356f7d42021-01-26 09:18:53 -050037 mctx := &registerMutatorsContext{
38 bazelConversionMode: true,
Jingwen Chen041b1842021-02-01 00:23:25 -050039 }
40
Liz Kammer356f7d42021-01-26 09:18:53 -050041 bp2buildPreArchMutators = append([]RegisterMutatorFunc{
42 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...)
49
50 for _, f := range bp2buildPreArchMutators {
51 f(mctx)
52 }
53
Jingwen Chen73850672020-12-14 08:25:34 -050054 // Register bp2build mutators
55 for _, f := range bp2buildMutators {
56 f(mctx)
57 }
58
Paul Duffin1d2d42f2021-03-06 20:08:12 +000059 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050060}
61
Paul Duffinc05b0342021-03-06 13:28:13 +000062// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
63// with the InitRegistrationContext and will be used at runtime.
64func collateGloballyRegisteredMutators() sortableComponents {
65 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
66}
67
68// collateRegisteredMutators constructs a single list of mutators from the separate lists.
69func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070070 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080071
72 register := func(funcs []RegisterMutatorFunc) {
73 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070074 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080075 }
76 }
77
Colin Crosscec81712017-07-13 14:43:27 -070078 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080079
Colin Crosscec81712017-07-13 14:43:27 -070080 register(preDeps)
81
Liz Kammer356f7d42021-01-26 09:18:53 -050082 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070083
84 register(postDeps)
85
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000086 mctx.finalPhase = true
87 register(finalDeps)
88
Paul Duffinc05b0342021-03-06 13:28:13 +000089 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -070090}
91
92type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +000093 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -050094 finalPhase bool
95 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -070096}
Colin Cross1e676be2016-10-12 14:38:15 -070097
98type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -070099 TopDown(name string, m TopDownMutator) MutatorHandle
100 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -0700101 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -0700102}
103
104type RegisterMutatorFunc func(RegisterMutatorsContext)
105
Colin Crosscec81712017-07-13 14:43:27 -0700106var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800107 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100108
Paul Duffinaa4162e2020-05-05 11:35:43 +0100109 // Check the visibility rules are valid.
110 //
111 // This must run after the package renamer mutators so that any issues found during
112 // validation of the package's default_visibility property are reported using the
113 // correct package name and not the synthetic name.
114 //
115 // This must also be run before defaults mutators as the rules for validation are
116 // different before checking the rules than they are afterwards. e.g.
117 // visibility: ["//visibility:private", "//visibility:public"]
118 // would be invalid if specified in a module definition but is valid if it results
119 // from something like this:
120 //
121 // defaults {
122 // name: "defaults",
123 // // Be inaccessible outside a package by default.
124 // visibility: ["//visibility:private"]
125 // }
126 //
127 // defaultable_module {
128 // name: "defaultable_module",
129 // defaults: ["defaults"],
130 // // Override the default.
131 // visibility: ["//visibility:public"]
132 // }
133 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000134 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100135
Bob Badour37af0462021-01-07 03:34:31 +0000136 // Record the default_applicable_licenses for each package.
137 //
138 // This must run before the defaults so that defaults modules can pick up the package default.
139 RegisterLicensesPackageMapper,
140
Paul Duffinaa4162e2020-05-05 11:35:43 +0100141 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100142 //
143 // Any mutators that are added before this will not see any modules created by
144 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700145 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100146
Paul Duffin44f1d842020-06-26 20:17:02 +0100147 // Add dependencies on any components so that any component references can be
148 // resolved within the deps mutator.
149 //
150 // Must be run after defaults so it can be used to create dependencies on the
151 // component modules that are creating in a DefaultableHook.
152 //
153 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
154 // renamed. That is so that if a module creates components using a prebuilt module
155 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
156 // the prebuilt module and not the source module.
157 RegisterComponentsMutator,
158
Paul Duffinc988c8e2020-04-29 18:27:14 +0100159 // Create an association between prebuilt modules and their corresponding source
160 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100161 //
162 // Must be run after defaults mutators to ensure that any modules created by
163 // a DefaultableHook can be either a prebuilt or a source module with a matching
164 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100165 RegisterPrebuiltsPreArchMutators,
166
Bob Badour37af0462021-01-07 03:34:31 +0000167 // Gather the licenses properties for all modules for use during expansion and enforcement.
168 //
169 // This must come after the defaults mutators to ensure that any licenses supplied
170 // in a defaults module has been successfully applied before the rules are gathered.
171 RegisterLicensesPropertyGatherer,
172
Paul Duffinaa4162e2020-05-05 11:35:43 +0100173 // Gather the visibility rules for all modules for us during visibility enforcement.
174 //
175 // This must come after the defaults mutators to ensure that any visibility supplied
176 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000177 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700178}
179
Colin Crossae4c6182017-09-15 17:33:55 -0700180func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700181 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800182 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700183 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700184}
185
Colin Crosscec81712017-07-13 14:43:27 -0700186var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700187 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700188}
189
190var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800191 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700192 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000193 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000194 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100195 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700196 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700197}
Colin Cross1e676be2016-10-12 14:38:15 -0700198
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000199var finalDeps = []RegisterMutatorFunc{}
200
Colin Cross1e676be2016-10-12 14:38:15 -0700201func PreArchMutators(f RegisterMutatorFunc) {
202 preArch = append(preArch, f)
203}
204
205func PreDepsMutators(f RegisterMutatorFunc) {
206 preDeps = append(preDeps, f)
207}
208
209func PostDepsMutators(f RegisterMutatorFunc) {
210 postDeps = append(postDeps, f)
211}
212
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000213func FinalDepsMutators(f RegisterMutatorFunc) {
214 finalDeps = append(finalDeps, f)
215}
216
Liz Kammer356f7d42021-01-26 09:18:53 -0500217var bp2buildPreArchMutators = []RegisterMutatorFunc{}
Jingwen Chen12b4c272021-03-10 02:05:59 -0500218var bp2buildMutators = map[string]RegisterMutatorFunc{}
Jingwen Chen73850672020-12-14 08:25:34 -0500219
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400220// See http://b/192523357
221var bp2buildLock sync.Mutex
222
Jingwen Chen73850672020-12-14 08:25:34 -0500223// RegisterBp2BuildMutator registers specially crafted mutators for
224// converting Blueprint/Android modules into special modules that can
225// be code-generated into Bazel BUILD targets.
226//
227// TODO(b/178068862): bring this into TestContext.
228func RegisterBp2BuildMutator(moduleType string, m func(TopDownMutatorContext)) {
Jingwen Chen73850672020-12-14 08:25:34 -0500229 f := func(ctx RegisterMutatorsContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500230 ctx.TopDown(moduleType, m)
Jingwen Chen73850672020-12-14 08:25:34 -0500231 }
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400232 // Use a lock to avoid a concurrent map write if RegisterBp2BuildMutator is called in parallel
233 bp2buildLock.Lock()
234 defer bp2buildLock.Unlock()
Jingwen Chen12b4c272021-03-10 02:05:59 -0500235 bp2buildMutators[moduleType] = f
Jingwen Chen73850672020-12-14 08:25:34 -0500236}
237
Liz Kammer356f7d42021-01-26 09:18:53 -0500238// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
239// into Bazel BUILD targets that should run prior to deps and conversion.
240func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
241 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
242}
243
Colin Cross9f35c3d2020-09-16 19:04:41 -0700244type BaseMutatorContext interface {
245 BaseModuleContext
246
247 // MutatorName returns the name that this mutator was registered with.
248 MutatorName() string
249
250 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
251 // AddDependency or OtherModuleName until after this mutator pass is complete.
252 Rename(name string)
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400253
254 // BazelConversionMode returns whether this mutator is being run as part of Bazel Conversion.
255 BazelConversionMode() bool
Colin Cross9f35c3d2020-09-16 19:04:41 -0700256}
257
Colin Cross25de6c32019-06-06 14:29:25 -0700258type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700259
Colin Cross635c3b02016-05-18 15:37:25 -0700260type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700261 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700262
Colin Cross9f35c3d2020-09-16 19:04:41 -0700263 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
264 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700265 CreateModule(ModuleFactory, ...interface{}) Module
Jingwen Chen1fd14692021-02-05 03:01:50 -0500266
267 // CreateBazelTargetModule creates a BazelTargetModule by calling the
268 // factory method, just like in CreateModule, but also requires
269 // BazelTargetModuleProperties containing additional metadata for the
270 // bp2build codegenerator.
Liz Kammer2ada09a2021-08-11 00:17:36 -0400271 CreateBazelTargetModule(string, bazel.BazelTargetModuleProperties, interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700272}
273
Colin Cross25de6c32019-06-06 14:29:25 -0700274type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700275 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700276 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700277}
278
Colin Cross25de6c32019-06-06 14:29:25 -0700279type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700280
Colin Cross635c3b02016-05-18 15:37:25 -0700281type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700282 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800283
Colin Cross4f1dcb02020-09-16 18:45:04 -0700284 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
285 // dependency (some entries may be nil).
286 //
287 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
288 // new dependencies have had the current mutator called on them. If the mutator is not
289 // parallel this method does not affect the ordering of the current mutator pass, but will
290 // be ordered correctly for all future mutator passes.
291 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700292
293 // AddReverseDependency adds a dependency from the destination to the given module.
294 // Does not affect the ordering of the current mutator pass, but will be ordered
295 // correctly for all future mutator passes. All reverse dependencies for a destination module are
296 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
297 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800298 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700299
300 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
301 // parameter. It returns a list of new modules in the same order as the variationNames
302 // list.
303 //
304 // If any of the dependencies of the module being operated on were already split
305 // by calling CreateVariations with the same name, the dependency will automatically
306 // be updated to point the matching variant.
307 //
308 // If a module is split, and then a module depending on the first module is not split
309 // when the Mutator is later called on it, the dependency of the depending module will
310 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800311 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700312
313 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
314 // parameter. It returns a list of new modules in the same order as the variantNames
315 // list.
316 //
317 // Local variations do not affect automatic dependency resolution - dependencies added
318 // to the split module via deps or DynamicDependerModule must exactly match a variant
319 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800320 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700321
322 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
323 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800324 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700325
326 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
327 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900328 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700329
330 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700331 // argument to select which variant of the dependency to use. It returns a slice of modules for
332 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
333 // the all of the non-local variations of the current module, plus the variations argument.
334 //
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 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700340
341 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700342 // variations argument to select which variant of the dependency to use. It returns a slice of
343 // modules for each dependency (some entries may be nil). A variant of the dependency must
344 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700345 // For any unspecified variation the first variant will be used.
346 //
347 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
348 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700349 //
350 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
351 // new dependencies have had the current mutator called on them. If the mutator is not
352 // parallel this method does not affect the ordering of the current mutator pass, but will
353 // be ordered correctly for all future mutator passes.
354 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700355
356 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
357 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
358 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
359 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800360 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700361
362 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
363 // specified name with the current variant of this module. Replacements don't take effect until
364 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800365 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700366
367 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
368 // specified name with the current variant of this module as long as the supplied predicate returns
369 // true.
370 //
371 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100372 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700373
374 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
375 // and creates an alias from the current variant (before the mutator has run) to the new
376 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
377 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
378 // be used to add dependencies on the newly created variant using the variant map from
379 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800380 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700381
382 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
383 // module, and creates an alias from a new fromVariationName variant the toVariationName
384 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
385 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
386 // be used to add dependencies on the toVariationName variant using the fromVariationName
387 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700388 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700389
390 // SetVariationProvider sets the value for a provider for the given newly created variant of
391 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
392 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
393 // if the value is not of the appropriate type, or if the module is not a newly created
394 // variant of the current module. The value should not be modified after being passed to
395 // SetVariationProvider.
396 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700397}
398
Colin Cross25de6c32019-06-06 14:29:25 -0700399type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700400 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700401 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400402 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700403}
404
Colin Cross617b88a2020-08-24 18:04:09 -0700405func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500406 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700407
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400408 moduleContext := a.base().baseModuleContextFactory(ctx)
409 moduleContext.bazelConversionMode = bazelConversionMode
410
Colin Cross617b88a2020-08-24 18:04:09 -0700411 return &bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400412 bp: ctx,
413 baseModuleContext: a.base().baseModuleContextFactory(ctx),
414 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700415 }
416}
417
Colin Cross25de6c32019-06-06 14:29:25 -0700418func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000419 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500420 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700421 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700422 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500423 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700424 }
Colin Cross798bfce2016-10-12 14:28:16 -0700425 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500426 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700427 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700428 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700429}
430
Colin Cross617b88a2020-08-24 18:04:09 -0700431func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
432 mutator := &mutator{name: name, bottomUpMutator: m}
433 x.mutators = append(x.mutators, mutator)
434 return mutator
435}
436
Liz Kammer356f7d42021-01-26 09:18:53 -0500437func (x *registerMutatorsContext) mutatorName(name string) string {
438 if x.bazelConversionMode {
439 return name + "_bp2build"
440 }
441 return name
442}
443
Colin Cross25de6c32019-06-06 14:29:25 -0700444func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700445 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700446 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400447 moduleContext := a.base().baseModuleContextFactory(ctx)
448 moduleContext.bazelConversionMode = x.bazelConversionMode
Colin Cross25de6c32019-06-06 14:29:25 -0700449 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700450 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400451 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700452 }
Colin Cross798bfce2016-10-12 14:28:16 -0700453 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700454 }
Colin Cross798bfce2016-10-12 14:28:16 -0700455 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500456 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700457 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700458 return mutator
459}
460
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000461func (mutator *mutator) componentName() string {
462 return mutator.name
463}
464
465func (mutator *mutator) register(ctx *Context) {
466 blueprintCtx := ctx.Context
467 var handle blueprint.MutatorHandle
468 if mutator.bottomUpMutator != nil {
469 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
470 } else if mutator.topDownMutator != nil {
471 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
472 }
473 if mutator.parallel {
474 handle.Parallel()
475 }
476}
477
Colin Cross798bfce2016-10-12 14:28:16 -0700478type MutatorHandle interface {
479 Parallel() MutatorHandle
480}
481
482func (mutator *mutator) Parallel() MutatorHandle {
483 mutator.parallel = true
484 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700485}
Colin Cross1e676be2016-10-12 14:38:15 -0700486
Paul Duffin44f1d842020-06-26 20:17:02 +0100487func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
488 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
489}
490
491// A special mutator that runs just prior to the deps mutator to allow the dependencies
492// on component modules to be added so that they can depend directly on a prebuilt
493// module.
494func componentDepsMutator(ctx BottomUpMutatorContext) {
495 if m := ctx.Module(); m.Enabled() {
496 m.ComponentDepsMutator(ctx)
497 }
498}
499
Colin Cross1e676be2016-10-12 14:38:15 -0700500func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100501 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700502 m.DepsMutator(ctx)
503 }
504}
Colin Crossd11fcda2017-10-23 17:59:01 -0700505
Liz Kammer356f7d42021-01-26 09:18:53 -0500506func registerDepsMutator(ctx RegisterMutatorsContext) {
507 ctx.BottomUp("deps", depsMutator).Parallel()
508}
509
510func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
511 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
512 // being converted to build targets.
513 ctx.BottomUp("deps", depsMutator).Parallel()
514}
515
Jingwen Chen1fd14692021-02-05 03:01:50 -0500516func (t *topDownMutatorContext) CreateBazelTargetModule(
Liz Kammerfc46bc12021-02-19 11:06:17 -0500517 name string,
Jingwen Chen1fd14692021-02-05 03:01:50 -0500518 bazelProps bazel.BazelTargetModuleProperties,
Liz Kammer2ada09a2021-08-11 00:17:36 -0400519 attrs interface{}) {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400520
521 info := bp2buildInfo{
522 Name: name,
523 Dir: t.OtherModuleDir(t.Module()),
524 BazelProps: bazelProps,
525 Attrs: attrs,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500526 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400527 t.Module().base().addBp2buildInfo(info)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500528}
529
Colin Cross25de6c32019-06-06 14:29:25 -0700530func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700531 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700532 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700533 p, nil)
534 if err != nil {
535 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700536 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700537 } else {
538 panic(err)
539 }
540 }
541 }
542}
543
Colin Cross25de6c32019-06-06 14:29:25 -0700544func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700545 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700546 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700547 p, nil)
548 if err != nil {
549 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700550 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700551 } else {
552 panic(err)
553 }
554 }
555 }
556}
Colin Crossdc35e212019-06-06 16:13:11 -0700557
558// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
559// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
560// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
561// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
562// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
563
Colin Crosscb55e082019-07-01 15:32:31 -0700564func (t *topDownMutatorContext) MutatorName() string {
565 return t.bp.MutatorName()
566}
567
Colin Crossdc35e212019-06-06 16:13:11 -0700568func (t *topDownMutatorContext) Rename(name string) {
569 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700570 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700571}
572
Colin Crosse003c4a2019-09-25 12:58:36 -0700573func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Colin Cross18c46802019-09-24 22:19:02 -0700574 inherited := []interface{}{&t.Module().base().commonProperties}
Colin Crosse003c4a2019-09-25 12:58:36 -0700575 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
Colin Cross18c46802019-09-24 22:19:02 -0700576
577 if t.Module().base().variableProperties != nil && module.base().variableProperties != nil {
578 src := t.Module().base().variableProperties
579 dst := []interface{}{
580 module.base().variableProperties,
581 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
582 // don't cause a "failed to find property to extend" error.
Colin Cross43e789d2020-01-28 09:46:50 -0800583 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross18c46802019-09-24 22:19:02 -0700584 }
585 err := proptools.AppendMatchingProperties(dst, src, nil)
586 if err != nil {
587 panic(err)
588 }
589 }
590
Colin Crosse003c4a2019-09-25 12:58:36 -0700591 return module
Colin Crossdc35e212019-06-06 16:13:11 -0700592}
593
Liz Kammera060c452021-03-24 10:14:47 -0400594func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
595 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), props...).(Module)
596 return module
597}
598
Colin Crosscb55e082019-07-01 15:32:31 -0700599func (b *bottomUpMutatorContext) MutatorName() string {
600 return b.bp.MutatorName()
601}
602
Colin Crossdc35e212019-06-06 16:13:11 -0700603func (b *bottomUpMutatorContext) Rename(name string) {
604 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700605 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700606}
607
Colin Cross4f1dcb02020-09-16 18:45:04 -0700608func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
609 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700610}
611
612func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
613 b.bp.AddReverseDependency(module, tag, name)
614}
615
Colin Cross43b92e02019-11-18 15:28:57 -0800616func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000617 if b.finalPhase {
618 panic("CreateVariations not allowed in FinalDepsMutators")
619 }
620
Colin Cross9a362232019-07-01 15:32:45 -0700621 modules := b.bp.CreateVariations(variations...)
622
Colin Cross43b92e02019-11-18 15:28:57 -0800623 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700624 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800625 aModules[i] = modules[i].(Module)
626 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700627 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
628 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
629 }
630
Colin Cross43b92e02019-11-18 15:28:57 -0800631 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700632}
633
Colin Cross43b92e02019-11-18 15:28:57 -0800634func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000635 if b.finalPhase {
636 panic("CreateLocalVariations not allowed in FinalDepsMutators")
637 }
638
Colin Cross9a362232019-07-01 15:32:45 -0700639 modules := b.bp.CreateLocalVariations(variations...)
640
Colin Cross43b92e02019-11-18 15:28:57 -0800641 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700642 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800643 aModules[i] = modules[i].(Module)
644 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700645 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
646 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
647 }
648
Colin Cross43b92e02019-11-18 15:28:57 -0800649 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700650}
651
652func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
653 b.bp.SetDependencyVariation(variation)
654}
655
Jiyong Park1d1119f2019-07-29 21:27:18 +0900656func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
657 b.bp.SetDefaultDependencyVariation(variation)
658}
659
Colin Crossdc35e212019-06-06 16:13:11 -0700660func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700661 names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500662 if b.bazelConversionMode {
663 _, noSelfDeps := RemoveFromList(b.ModuleName(), names)
664 if len(noSelfDeps) == 0 {
665 return []blueprint.Module(nil)
666 }
667 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
668 // dependency, the variations would not exist and the dependency could not be added, by
669 // specifying no variations, we will allow adding the dependency to succeed.
670 return b.bp.AddFarVariationDependencies(nil, tag, noSelfDeps...)
671 }
Colin Crossdc35e212019-06-06 16:13:11 -0700672
Colin Cross4f1dcb02020-09-16 18:45:04 -0700673 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700674}
675
676func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700677 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500678 if b.bazelConversionMode {
679 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
680 // dependency, the variations would not exist and the dependency could not be added, by
681 // specifying no variations, we will allow adding the dependency to succeed.
682 return b.bp.AddFarVariationDependencies(nil, tag, names...)
683 }
Colin Crossdc35e212019-06-06 16:13:11 -0700684
Colin Cross4f1dcb02020-09-16 18:45:04 -0700685 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700686}
687
688func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
689 b.bp.AddInterVariantDependency(tag, from, to)
690}
691
692func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
693 b.bp.ReplaceDependencies(name)
694}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800695
Paul Duffin80342d72020-06-26 22:08:43 +0100696func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
697 b.bp.ReplaceDependenciesIf(name, predicate)
698}
699
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800700func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
701 b.bp.AliasVariation(variationName)
702}
Colin Cross1b9604b2020-08-11 12:03:56 -0700703
704func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
705 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
706}
Colin Crossd27e7b82020-07-02 11:38:17 -0700707
708func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
709 b.bp.SetVariationProvider(module, provider, value)
710}