blob: 20ec621879a682cd7cdb581b2b14bf105fcda902 [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"
Jingwen Chenfb4692a2021-02-07 10:05:16 -050019 "fmt"
Colin Cross18c46802019-09-24 22:19:02 -070020 "reflect"
Jingwen Chenfb4692a2021-02-07 10:05:16 -050021 "strings"
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -040022 "sync"
Colin Cross18c46802019-09-24 22:19:02 -070023
Colin Cross795c3772017-03-16 16:50:10 -070024 "github.com/google/blueprint"
Colin Cross519917d2017-11-02 16:35:56 -070025 "github.com/google/blueprint/proptools"
Colin Cross795c3772017-03-16 16:50:10 -070026)
Colin Cross6362e272015-10-29 15:25:03 -070027
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070028// Phases:
29// run Pre-arch mutators
30// run archMutator
31// run Pre-deps mutators
32// run depsMutator
33// run PostDeps mutators
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000034// run FinalDeps mutators (CreateVariations disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070035// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070036
Jingwen Chen73850672020-12-14 08:25:34 -050037// RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
Chris Parsons5a34ffb2021-07-21 14:34:58 -040038func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators, bp2buildMutators []RegisterMutatorFunc) {
Liz Kammer356f7d42021-01-26 09:18:53 -050039 mctx := &registerMutatorsContext{
40 bazelConversionMode: true,
Jingwen Chen041b1842021-02-01 00:23:25 -050041 }
42
Liz Kammer356f7d42021-01-26 09:18:53 -050043 bp2buildPreArchMutators = append([]RegisterMutatorFunc{
44 RegisterNamespaceMutator,
45 RegisterDefaultsPreArchMutators,
46 // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should
47 // evaluate the impact on conversion.
48 RegisterPrebuiltsPreArchMutators,
49 },
50 preArchMutators...)
51
52 for _, f := range bp2buildPreArchMutators {
53 f(mctx)
54 }
55
Jingwen Chen73850672020-12-14 08:25:34 -050056 // Register bp2build mutators
57 for _, f := range bp2buildMutators {
58 f(mctx)
59 }
60
Paul Duffin1d2d42f2021-03-06 20:08:12 +000061 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050062}
63
Paul Duffinc05b0342021-03-06 13:28:13 +000064// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
65// with the InitRegistrationContext and will be used at runtime.
66func collateGloballyRegisteredMutators() sortableComponents {
67 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
68}
69
70// collateRegisteredMutators constructs a single list of mutators from the separate lists.
71func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070072 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080073
74 register := func(funcs []RegisterMutatorFunc) {
75 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070076 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080077 }
78 }
79
Colin Crosscec81712017-07-13 14:43:27 -070080 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080081
Colin Crosscec81712017-07-13 14:43:27 -070082 register(preDeps)
83
Liz Kammer356f7d42021-01-26 09:18:53 -050084 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070085
86 register(postDeps)
87
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000088 mctx.finalPhase = true
89 register(finalDeps)
90
Paul Duffinc05b0342021-03-06 13:28:13 +000091 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -070092}
93
94type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +000095 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -050096 finalPhase bool
97 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -070098}
Colin Cross1e676be2016-10-12 14:38:15 -070099
100type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -0700101 TopDown(name string, m TopDownMutator) MutatorHandle
102 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -0700103 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -0700104}
105
106type RegisterMutatorFunc func(RegisterMutatorsContext)
107
Colin Crosscec81712017-07-13 14:43:27 -0700108var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800109 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100110
Paul Duffinaa4162e2020-05-05 11:35:43 +0100111 // Check the visibility rules are valid.
112 //
113 // This must run after the package renamer mutators so that any issues found during
114 // validation of the package's default_visibility property are reported using the
115 // correct package name and not the synthetic name.
116 //
117 // This must also be run before defaults mutators as the rules for validation are
118 // different before checking the rules than they are afterwards. e.g.
119 // visibility: ["//visibility:private", "//visibility:public"]
120 // would be invalid if specified in a module definition but is valid if it results
121 // from something like this:
122 //
123 // defaults {
124 // name: "defaults",
125 // // Be inaccessible outside a package by default.
126 // visibility: ["//visibility:private"]
127 // }
128 //
129 // defaultable_module {
130 // name: "defaultable_module",
131 // defaults: ["defaults"],
132 // // Override the default.
133 // visibility: ["//visibility:public"]
134 // }
135 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000136 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100137
Bob Badour37af0462021-01-07 03:34:31 +0000138 // Record the default_applicable_licenses for each package.
139 //
140 // This must run before the defaults so that defaults modules can pick up the package default.
141 RegisterLicensesPackageMapper,
142
Paul Duffinaa4162e2020-05-05 11:35:43 +0100143 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100144 //
145 // Any mutators that are added before this will not see any modules created by
146 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700147 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100148
Paul Duffin44f1d842020-06-26 20:17:02 +0100149 // Add dependencies on any components so that any component references can be
150 // resolved within the deps mutator.
151 //
152 // Must be run after defaults so it can be used to create dependencies on the
153 // component modules that are creating in a DefaultableHook.
154 //
155 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
156 // renamed. That is so that if a module creates components using a prebuilt module
157 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
158 // the prebuilt module and not the source module.
159 RegisterComponentsMutator,
160
Paul Duffinc988c8e2020-04-29 18:27:14 +0100161 // Create an association between prebuilt modules and their corresponding source
162 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100163 //
164 // Must be run after defaults mutators to ensure that any modules created by
165 // a DefaultableHook can be either a prebuilt or a source module with a matching
166 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100167 RegisterPrebuiltsPreArchMutators,
168
Bob Badour37af0462021-01-07 03:34:31 +0000169 // Gather the licenses properties for all modules for use during expansion and enforcement.
170 //
171 // This must come after the defaults mutators to ensure that any licenses supplied
172 // in a defaults module has been successfully applied before the rules are gathered.
173 RegisterLicensesPropertyGatherer,
174
Paul Duffinaa4162e2020-05-05 11:35:43 +0100175 // Gather the visibility rules for all modules for us during visibility enforcement.
176 //
177 // This must come after the defaults mutators to ensure that any visibility supplied
178 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000179 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700180}
181
Colin Crossae4c6182017-09-15 17:33:55 -0700182func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700183 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800184 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700185 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700186}
187
Colin Crosscec81712017-07-13 14:43:27 -0700188var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700189 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700190}
191
192var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800193 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700194 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000195 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000196 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100197 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700198 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700199}
Colin Cross1e676be2016-10-12 14:38:15 -0700200
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000201var finalDeps = []RegisterMutatorFunc{}
202
Colin Cross1e676be2016-10-12 14:38:15 -0700203func PreArchMutators(f RegisterMutatorFunc) {
204 preArch = append(preArch, f)
205}
206
207func PreDepsMutators(f RegisterMutatorFunc) {
208 preDeps = append(preDeps, f)
209}
210
211func PostDepsMutators(f RegisterMutatorFunc) {
212 postDeps = append(postDeps, f)
213}
214
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000215func FinalDepsMutators(f RegisterMutatorFunc) {
216 finalDeps = append(finalDeps, f)
217}
218
Liz Kammer356f7d42021-01-26 09:18:53 -0500219var bp2buildPreArchMutators = []RegisterMutatorFunc{}
Jingwen Chen12b4c272021-03-10 02:05:59 -0500220var bp2buildMutators = map[string]RegisterMutatorFunc{}
Jingwen Chen73850672020-12-14 08:25:34 -0500221
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400222// See http://b/192523357
223var bp2buildLock sync.Mutex
224
Jingwen Chen73850672020-12-14 08:25:34 -0500225// RegisterBp2BuildMutator registers specially crafted mutators for
226// converting Blueprint/Android modules into special modules that can
227// be code-generated into Bazel BUILD targets.
228//
229// TODO(b/178068862): bring this into TestContext.
230func RegisterBp2BuildMutator(moduleType string, m func(TopDownMutatorContext)) {
Jingwen Chen73850672020-12-14 08:25:34 -0500231 f := func(ctx RegisterMutatorsContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500232 ctx.TopDown(moduleType, m)
Jingwen Chen73850672020-12-14 08:25:34 -0500233 }
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400234 // Use a lock to avoid a concurrent map write if RegisterBp2BuildMutator is called in parallel
235 bp2buildLock.Lock()
236 defer bp2buildLock.Unlock()
Jingwen Chen12b4c272021-03-10 02:05:59 -0500237 bp2buildMutators[moduleType] = f
Jingwen Chen73850672020-12-14 08:25:34 -0500238}
239
Liz Kammer356f7d42021-01-26 09:18:53 -0500240// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
241// into Bazel BUILD targets that should run prior to deps and conversion.
242func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
243 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
244}
245
Colin Cross9f35c3d2020-09-16 19:04:41 -0700246type BaseMutatorContext interface {
247 BaseModuleContext
248
249 // MutatorName returns the name that this mutator was registered with.
250 MutatorName() string
251
252 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
253 // AddDependency or OtherModuleName until after this mutator pass is complete.
254 Rename(name string)
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400255
256 // BazelConversionMode returns whether this mutator is being run as part of Bazel Conversion.
257 BazelConversionMode() bool
Colin Cross9f35c3d2020-09-16 19:04:41 -0700258}
259
Colin Cross25de6c32019-06-06 14:29:25 -0700260type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700261
Colin Cross635c3b02016-05-18 15:37:25 -0700262type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700263 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700264
Colin Cross9f35c3d2020-09-16 19:04:41 -0700265 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
266 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700267 CreateModule(ModuleFactory, ...interface{}) Module
Jingwen Chen1fd14692021-02-05 03:01:50 -0500268
269 // CreateBazelTargetModule creates a BazelTargetModule by calling the
270 // factory method, just like in CreateModule, but also requires
271 // BazelTargetModuleProperties containing additional metadata for the
272 // bp2build codegenerator.
Liz Kammer2ada09a2021-08-11 00:17:36 -0400273 CreateBazelTargetModule(string, bazel.BazelTargetModuleProperties, interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700274}
275
Colin Cross25de6c32019-06-06 14:29:25 -0700276type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700277 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700278 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700279}
280
Colin Cross25de6c32019-06-06 14:29:25 -0700281type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700282
Colin Cross635c3b02016-05-18 15:37:25 -0700283type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700284 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800285
Colin Cross4f1dcb02020-09-16 18:45:04 -0700286 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
287 // dependency (some entries may be nil).
288 //
289 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
290 // new dependencies have had the current mutator called on them. If the mutator is not
291 // parallel this method does not affect the ordering of the current mutator pass, but will
292 // be ordered correctly for all future mutator passes.
293 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700294
295 // AddReverseDependency adds a dependency from the destination to the given module.
296 // Does not affect the ordering of the current mutator pass, but will be ordered
297 // correctly for all future mutator passes. All reverse dependencies for a destination module are
298 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
299 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800300 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700301
302 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
303 // parameter. It returns a list of new modules in the same order as the variationNames
304 // list.
305 //
306 // If any of the dependencies of the module being operated on were already split
307 // by calling CreateVariations with the same name, the dependency will automatically
308 // be updated to point the matching variant.
309 //
310 // If a module is split, and then a module depending on the first module is not split
311 // when the Mutator is later called on it, the dependency of the depending module will
312 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800313 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700314
315 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
316 // parameter. It returns a list of new modules in the same order as the variantNames
317 // list.
318 //
319 // Local variations do not affect automatic dependency resolution - dependencies added
320 // to the split module via deps or DynamicDependerModule must exactly match a variant
321 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800322 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700323
324 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
325 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800326 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700327
328 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
329 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900330 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700331
332 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700333 // argument to select which variant of the dependency to use. It returns a slice of modules for
334 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
335 // the all of the non-local variations of the current module, plus the variations argument.
336 //
337 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
338 // new dependencies have had the current mutator called on them. If the mutator is not
339 // parallel this method does not affect the ordering of the current mutator pass, but will
340 // be ordered correctly for all future mutator passes.
341 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700342
343 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700344 // variations argument to select which variant of the dependency to use. It returns a slice of
345 // modules for each dependency (some entries may be nil). A variant of the dependency must
346 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700347 // For any unspecified variation the first variant will be used.
348 //
349 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
350 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700351 //
352 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
353 // new dependencies have had the current mutator called on them. If the mutator is not
354 // parallel this method does not affect the ordering of the current mutator pass, but will
355 // be ordered correctly for all future mutator passes.
356 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700357
358 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
359 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
360 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
361 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800362 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700363
364 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
365 // specified name with the current variant of this module. Replacements don't take effect until
366 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800367 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700368
369 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
370 // specified name with the current variant of this module as long as the supplied predicate returns
371 // true.
372 //
373 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100374 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700375
376 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
377 // and creates an alias from the current variant (before the mutator has run) to the new
378 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
379 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
380 // be used to add dependencies on the newly created variant using the variant map from
381 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800382 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700383
384 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
385 // module, and creates an alias from a new fromVariationName variant the toVariationName
386 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
387 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
388 // be used to add dependencies on the toVariationName variant using the fromVariationName
389 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700390 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700391
392 // SetVariationProvider sets the value for a provider for the given newly created variant of
393 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
394 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
395 // if the value is not of the appropriate type, or if the module is not a newly created
396 // variant of the current module. The value should not be modified after being passed to
397 // SetVariationProvider.
398 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700399}
400
Colin Cross25de6c32019-06-06 14:29:25 -0700401type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700402 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700403 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400404 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700405}
406
Colin Cross617b88a2020-08-24 18:04:09 -0700407func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500408 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700409
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400410 moduleContext := a.base().baseModuleContextFactory(ctx)
411 moduleContext.bazelConversionMode = bazelConversionMode
412
Colin Cross617b88a2020-08-24 18:04:09 -0700413 return &bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400414 bp: ctx,
415 baseModuleContext: a.base().baseModuleContextFactory(ctx),
416 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700417 }
418}
419
Colin Cross25de6c32019-06-06 14:29:25 -0700420func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000421 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500422 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700423 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700424 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500425 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700426 }
Colin Cross798bfce2016-10-12 14:28:16 -0700427 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500428 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700429 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700430 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700431}
432
Colin Cross617b88a2020-08-24 18:04:09 -0700433func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
434 mutator := &mutator{name: name, bottomUpMutator: m}
435 x.mutators = append(x.mutators, mutator)
436 return mutator
437}
438
Liz Kammer356f7d42021-01-26 09:18:53 -0500439func (x *registerMutatorsContext) mutatorName(name string) string {
440 if x.bazelConversionMode {
441 return name + "_bp2build"
442 }
443 return name
444}
445
Colin Cross25de6c32019-06-06 14:29:25 -0700446func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700447 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700448 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400449 moduleContext := a.base().baseModuleContextFactory(ctx)
450 moduleContext.bazelConversionMode = x.bazelConversionMode
Colin Cross25de6c32019-06-06 14:29:25 -0700451 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700452 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400453 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700454 }
Colin Cross798bfce2016-10-12 14:28:16 -0700455 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700456 }
Colin Cross798bfce2016-10-12 14:28:16 -0700457 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500458 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700459 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700460 return mutator
461}
462
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000463func (mutator *mutator) componentName() string {
464 return mutator.name
465}
466
467func (mutator *mutator) register(ctx *Context) {
468 blueprintCtx := ctx.Context
469 var handle blueprint.MutatorHandle
470 if mutator.bottomUpMutator != nil {
471 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
472 } else if mutator.topDownMutator != nil {
473 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
474 }
475 if mutator.parallel {
476 handle.Parallel()
477 }
478}
479
Colin Cross798bfce2016-10-12 14:28:16 -0700480type MutatorHandle interface {
481 Parallel() MutatorHandle
482}
483
484func (mutator *mutator) Parallel() MutatorHandle {
485 mutator.parallel = true
486 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700487}
Colin Cross1e676be2016-10-12 14:38:15 -0700488
Paul Duffin44f1d842020-06-26 20:17:02 +0100489func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
490 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
491}
492
493// A special mutator that runs just prior to the deps mutator to allow the dependencies
494// on component modules to be added so that they can depend directly on a prebuilt
495// module.
496func componentDepsMutator(ctx BottomUpMutatorContext) {
497 if m := ctx.Module(); m.Enabled() {
498 m.ComponentDepsMutator(ctx)
499 }
500}
501
Colin Cross1e676be2016-10-12 14:38:15 -0700502func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100503 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700504 m.DepsMutator(ctx)
505 }
506}
Colin Crossd11fcda2017-10-23 17:59:01 -0700507
Liz Kammer356f7d42021-01-26 09:18:53 -0500508func registerDepsMutator(ctx RegisterMutatorsContext) {
509 ctx.BottomUp("deps", depsMutator).Parallel()
510}
511
512func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
513 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
514 // being converted to build targets.
515 ctx.BottomUp("deps", depsMutator).Parallel()
516}
517
Jingwen Chen1fd14692021-02-05 03:01:50 -0500518func (t *topDownMutatorContext) CreateBazelTargetModule(
Liz Kammerfc46bc12021-02-19 11:06:17 -0500519 name string,
Jingwen Chen1fd14692021-02-05 03:01:50 -0500520 bazelProps bazel.BazelTargetModuleProperties,
Liz Kammer2ada09a2021-08-11 00:17:36 -0400521 attrs interface{}) {
Liz Kammerfc46bc12021-02-19 11:06:17 -0500522 if strings.HasPrefix(name, bazel.BazelTargetModuleNamePrefix) {
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500523 panic(fmt.Errorf(
Liz Kammerfc46bc12021-02-19 11:06:17 -0500524 "The %s name prefix is added automatically, do not set it manually: %s",
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500525 bazel.BazelTargetModuleNamePrefix,
Liz Kammerfc46bc12021-02-19 11:06:17 -0500526 name))
527 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400528
529 info := bp2buildInfo{
530 Name: name,
531 Dir: t.OtherModuleDir(t.Module()),
532 BazelProps: bazelProps,
533 Attrs: attrs,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500534 }
535
Liz Kammer2ada09a2021-08-11 00:17:36 -0400536 t.Module().base().addBp2buildInfo(info)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500537}
538
Colin Cross25de6c32019-06-06 14:29:25 -0700539func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700540 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700541 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700542 p, nil)
543 if err != nil {
544 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700545 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700546 } else {
547 panic(err)
548 }
549 }
550 }
551}
552
Colin Cross25de6c32019-06-06 14:29:25 -0700553func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700554 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700555 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700556 p, nil)
557 if err != nil {
558 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700559 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700560 } else {
561 panic(err)
562 }
563 }
564 }
565}
Colin Crossdc35e212019-06-06 16:13:11 -0700566
567// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
568// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
569// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
570// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
571// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
572
Colin Crosscb55e082019-07-01 15:32:31 -0700573func (t *topDownMutatorContext) MutatorName() string {
574 return t.bp.MutatorName()
575}
576
Colin Crossdc35e212019-06-06 16:13:11 -0700577func (t *topDownMutatorContext) Rename(name string) {
578 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700579 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700580}
581
Colin Crosse003c4a2019-09-25 12:58:36 -0700582func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Colin Cross18c46802019-09-24 22:19:02 -0700583 inherited := []interface{}{&t.Module().base().commonProperties}
Colin Crosse003c4a2019-09-25 12:58:36 -0700584 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
Colin Cross18c46802019-09-24 22:19:02 -0700585
586 if t.Module().base().variableProperties != nil && module.base().variableProperties != nil {
587 src := t.Module().base().variableProperties
588 dst := []interface{}{
589 module.base().variableProperties,
590 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
591 // don't cause a "failed to find property to extend" error.
Colin Cross43e789d2020-01-28 09:46:50 -0800592 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross18c46802019-09-24 22:19:02 -0700593 }
594 err := proptools.AppendMatchingProperties(dst, src, nil)
595 if err != nil {
596 panic(err)
597 }
598 }
599
Colin Crosse003c4a2019-09-25 12:58:36 -0700600 return module
Colin Crossdc35e212019-06-06 16:13:11 -0700601}
602
Liz Kammera060c452021-03-24 10:14:47 -0400603func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
604 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), props...).(Module)
605 return module
606}
607
Colin Crosscb55e082019-07-01 15:32:31 -0700608func (b *bottomUpMutatorContext) MutatorName() string {
609 return b.bp.MutatorName()
610}
611
Colin Crossdc35e212019-06-06 16:13:11 -0700612func (b *bottomUpMutatorContext) Rename(name string) {
613 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700614 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700615}
616
Colin Cross4f1dcb02020-09-16 18:45:04 -0700617func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
618 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700619}
620
621func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
622 b.bp.AddReverseDependency(module, tag, name)
623}
624
Colin Cross43b92e02019-11-18 15:28:57 -0800625func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000626 if b.finalPhase {
627 panic("CreateVariations not allowed in FinalDepsMutators")
628 }
629
Colin Cross9a362232019-07-01 15:32:45 -0700630 modules := b.bp.CreateVariations(variations...)
631
Colin Cross43b92e02019-11-18 15:28:57 -0800632 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700633 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800634 aModules[i] = modules[i].(Module)
635 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700636 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
637 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
638 }
639
Colin Cross43b92e02019-11-18 15:28:57 -0800640 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700641}
642
Colin Cross43b92e02019-11-18 15:28:57 -0800643func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000644 if b.finalPhase {
645 panic("CreateLocalVariations not allowed in FinalDepsMutators")
646 }
647
Colin Cross9a362232019-07-01 15:32:45 -0700648 modules := b.bp.CreateLocalVariations(variations...)
649
Colin Cross43b92e02019-11-18 15:28:57 -0800650 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700651 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800652 aModules[i] = modules[i].(Module)
653 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700654 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
655 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
656 }
657
Colin Cross43b92e02019-11-18 15:28:57 -0800658 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700659}
660
661func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
662 b.bp.SetDependencyVariation(variation)
663}
664
Jiyong Park1d1119f2019-07-29 21:27:18 +0900665func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
666 b.bp.SetDefaultDependencyVariation(variation)
667}
668
Colin Crossdc35e212019-06-06 16:13:11 -0700669func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700670 names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500671 if b.bazelConversionMode {
672 _, noSelfDeps := RemoveFromList(b.ModuleName(), names)
673 if len(noSelfDeps) == 0 {
674 return []blueprint.Module(nil)
675 }
676 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
677 // dependency, the variations would not exist and the dependency could not be added, by
678 // specifying no variations, we will allow adding the dependency to succeed.
679 return b.bp.AddFarVariationDependencies(nil, tag, noSelfDeps...)
680 }
Colin Crossdc35e212019-06-06 16:13:11 -0700681
Colin Cross4f1dcb02020-09-16 18:45:04 -0700682 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700683}
684
685func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700686 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500687 if b.bazelConversionMode {
688 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
689 // dependency, the variations would not exist and the dependency could not be added, by
690 // specifying no variations, we will allow adding the dependency to succeed.
691 return b.bp.AddFarVariationDependencies(nil, tag, names...)
692 }
Colin Crossdc35e212019-06-06 16:13:11 -0700693
Colin Cross4f1dcb02020-09-16 18:45:04 -0700694 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700695}
696
697func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
698 b.bp.AddInterVariantDependency(tag, from, to)
699}
700
701func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
702 b.bp.ReplaceDependencies(name)
703}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800704
Paul Duffin80342d72020-06-26 22:08:43 +0100705func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
706 b.bp.ReplaceDependenciesIf(name, predicate)
707}
708
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800709func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
710 b.bp.AliasVariation(variationName)
711}
Colin Cross1b9604b2020-08-11 12:03:56 -0700712
713func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
714 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
715}
Colin Crossd27e7b82020-07-02 11:38:17 -0700716
717func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
718 b.bp.SetVariationProvider(module, provider, value)
719}