blob: b0230011d0a96bcb74a0bbbadc1e9647facd0ccf [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"
Colin Cross18c46802019-09-24 22:19:02 -070022
Colin Cross795c3772017-03-16 16:50:10 -070023 "github.com/google/blueprint"
Colin Cross519917d2017-11-02 16:35:56 -070024 "github.com/google/blueprint/proptools"
Colin Cross795c3772017-03-16 16:50:10 -070025)
Colin Cross6362e272015-10-29 15:25:03 -070026
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070027// Phases:
28// run Pre-arch mutators
29// run archMutator
30// run Pre-deps mutators
31// run depsMutator
32// run PostDeps mutators
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000033// run FinalDeps mutators (CreateVariations disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070034// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070035
Colin Cross795c3772017-03-16 16:50:10 -070036func registerMutatorsToContext(ctx *blueprint.Context, mutators []*mutator) {
37 for _, t := range mutators {
38 var handle blueprint.MutatorHandle
39 if t.bottomUpMutator != nil {
40 handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator)
41 } else if t.topDownMutator != nil {
42 handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator)
43 }
44 if t.parallel {
45 handle.Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070046 }
47 }
Colin Cross1e676be2016-10-12 14:38:15 -070048}
49
Jingwen Chen73850672020-12-14 08:25:34 -050050// RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
Liz Kammer356f7d42021-01-26 09:18:53 -050051func RegisterMutatorsForBazelConversion(ctx *blueprint.Context, preArchMutators, depsMutators, bp2buildMutators []RegisterMutatorFunc) {
52 mctx := &registerMutatorsContext{
53 bazelConversionMode: true,
Jingwen Chen041b1842021-02-01 00:23:25 -050054 }
55
Liz Kammer356f7d42021-01-26 09:18:53 -050056 bp2buildPreArchMutators = append([]RegisterMutatorFunc{
57 RegisterNamespaceMutator,
58 RegisterDefaultsPreArchMutators,
59 // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should
60 // evaluate the impact on conversion.
61 RegisterPrebuiltsPreArchMutators,
62 },
63 preArchMutators...)
64
65 for _, f := range bp2buildPreArchMutators {
66 f(mctx)
67 }
68
69 bp2buildDepsMutators = append([]RegisterMutatorFunc{
70 registerDepsMutatorBp2Build,
71 registerPathDepsMutator,
72 }, depsMutators...)
73
74 for _, f := range bp2buildDepsMutators {
Jingwen Chen041b1842021-02-01 00:23:25 -050075 f(mctx)
76 }
77
Jingwen Chen73850672020-12-14 08:25:34 -050078 // Register bp2build mutators
79 for _, f := range bp2buildMutators {
80 f(mctx)
81 }
82
83 registerMutatorsToContext(ctx, mctx.mutators)
Jingwen Chen4133ce62020-12-02 04:34:15 -050084}
85
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000086func registerMutators(ctx *blueprint.Context, preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) {
Colin Crosscec81712017-07-13 14:43:27 -070087 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080088
89 register := func(funcs []RegisterMutatorFunc) {
90 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070091 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080092 }
93 }
94
Colin Crosscec81712017-07-13 14:43:27 -070095 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080096
Colin Crosscec81712017-07-13 14:43:27 -070097 register(preDeps)
98
Liz Kammer356f7d42021-01-26 09:18:53 -050099 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -0700100
101 register(postDeps)
102
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000103 mctx.finalPhase = true
104 register(finalDeps)
105
Colin Crosscec81712017-07-13 14:43:27 -0700106 registerMutatorsToContext(ctx, mctx.mutators)
Colin Cross795c3772017-03-16 16:50:10 -0700107}
108
109type registerMutatorsContext struct {
Liz Kammer356f7d42021-01-26 09:18:53 -0500110 mutators []*mutator
111 finalPhase bool
112 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -0700113}
Colin Cross1e676be2016-10-12 14:38:15 -0700114
115type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -0700116 TopDown(name string, m TopDownMutator) MutatorHandle
117 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -0700118 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -0700119}
120
121type RegisterMutatorFunc func(RegisterMutatorsContext)
122
Colin Crosscec81712017-07-13 14:43:27 -0700123var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800124 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100125
Paul Duffinaa4162e2020-05-05 11:35:43 +0100126 // Check the visibility rules are valid.
127 //
128 // This must run after the package renamer mutators so that any issues found during
129 // validation of the package's default_visibility property are reported using the
130 // correct package name and not the synthetic name.
131 //
132 // This must also be run before defaults mutators as the rules for validation are
133 // different before checking the rules than they are afterwards. e.g.
134 // visibility: ["//visibility:private", "//visibility:public"]
135 // would be invalid if specified in a module definition but is valid if it results
136 // from something like this:
137 //
138 // defaults {
139 // name: "defaults",
140 // // Be inaccessible outside a package by default.
141 // visibility: ["//visibility:private"]
142 // }
143 //
144 // defaultable_module {
145 // name: "defaultable_module",
146 // defaults: ["defaults"],
147 // // Override the default.
148 // visibility: ["//visibility:public"]
149 // }
150 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000151 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100152
Bob Badour37af0462021-01-07 03:34:31 +0000153 // Record the default_applicable_licenses for each package.
154 //
155 // This must run before the defaults so that defaults modules can pick up the package default.
156 RegisterLicensesPackageMapper,
157
Paul Duffinaa4162e2020-05-05 11:35:43 +0100158 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100159 //
160 // Any mutators that are added before this will not see any modules created by
161 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700162 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100163
Paul Duffin44f1d842020-06-26 20:17:02 +0100164 // Add dependencies on any components so that any component references can be
165 // resolved within the deps mutator.
166 //
167 // Must be run after defaults so it can be used to create dependencies on the
168 // component modules that are creating in a DefaultableHook.
169 //
170 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
171 // renamed. That is so that if a module creates components using a prebuilt module
172 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
173 // the prebuilt module and not the source module.
174 RegisterComponentsMutator,
175
Paul Duffinc988c8e2020-04-29 18:27:14 +0100176 // Create an association between prebuilt modules and their corresponding source
177 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100178 //
179 // Must be run after defaults mutators to ensure that any modules created by
180 // a DefaultableHook can be either a prebuilt or a source module with a matching
181 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100182 RegisterPrebuiltsPreArchMutators,
183
Bob Badour37af0462021-01-07 03:34:31 +0000184 // Gather the licenses properties for all modules for use during expansion and enforcement.
185 //
186 // This must come after the defaults mutators to ensure that any licenses supplied
187 // in a defaults module has been successfully applied before the rules are gathered.
188 RegisterLicensesPropertyGatherer,
189
Paul Duffinaa4162e2020-05-05 11:35:43 +0100190 // Gather the visibility rules for all modules for us during visibility enforcement.
191 //
192 // This must come after the defaults mutators to ensure that any visibility supplied
193 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000194 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700195}
196
Colin Crossae4c6182017-09-15 17:33:55 -0700197func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700198 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800199 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700200 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700201}
202
Colin Crosscec81712017-07-13 14:43:27 -0700203var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700204 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700205}
206
207var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800208 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700209 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000210 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000211 RegisterLicensesDependencyChecker,
Artur Satayevc5570ac2020-04-09 16:06:36 +0100212 RegisterNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700213 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700214}
Colin Cross1e676be2016-10-12 14:38:15 -0700215
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000216var finalDeps = []RegisterMutatorFunc{}
217
Colin Cross1e676be2016-10-12 14:38:15 -0700218func PreArchMutators(f RegisterMutatorFunc) {
219 preArch = append(preArch, f)
220}
221
222func PreDepsMutators(f RegisterMutatorFunc) {
223 preDeps = append(preDeps, f)
224}
225
226func PostDepsMutators(f RegisterMutatorFunc) {
227 postDeps = append(postDeps, f)
228}
229
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000230func FinalDepsMutators(f RegisterMutatorFunc) {
231 finalDeps = append(finalDeps, f)
232}
233
Liz Kammer356f7d42021-01-26 09:18:53 -0500234var bp2buildPreArchMutators = []RegisterMutatorFunc{}
235var bp2buildDepsMutators = []RegisterMutatorFunc{}
Jingwen Chen73850672020-12-14 08:25:34 -0500236var bp2buildMutators = []RegisterMutatorFunc{}
237
238// RegisterBp2BuildMutator registers specially crafted mutators for
239// converting Blueprint/Android modules into special modules that can
240// be code-generated into Bazel BUILD targets.
241//
242// TODO(b/178068862): bring this into TestContext.
243func RegisterBp2BuildMutator(moduleType string, m func(TopDownMutatorContext)) {
Jingwen Chen73850672020-12-14 08:25:34 -0500244 f := func(ctx RegisterMutatorsContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500245 ctx.TopDown(moduleType, m)
Jingwen Chen73850672020-12-14 08:25:34 -0500246 }
247 bp2buildMutators = append(bp2buildMutators, f)
248}
249
Liz Kammer356f7d42021-01-26 09:18:53 -0500250// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
251// into Bazel BUILD targets that should run prior to deps and conversion.
252func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
253 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
254}
255
256// DepsBp2BuildMutators adds mutators to be register for converting Android Blueprint modules into
257// Bazel BUILD targets that should run prior to conversion to resolve dependencies.
258func DepsBp2BuildMutators(f RegisterMutatorFunc) {
259 bp2buildDepsMutators = append(bp2buildDepsMutators, f)
260}
261
Colin Cross9f35c3d2020-09-16 19:04:41 -0700262type BaseMutatorContext interface {
263 BaseModuleContext
264
265 // MutatorName returns the name that this mutator was registered with.
266 MutatorName() string
267
268 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
269 // AddDependency or OtherModuleName until after this mutator pass is complete.
270 Rename(name string)
271}
272
Colin Cross25de6c32019-06-06 14:29:25 -0700273type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700274
Colin Cross635c3b02016-05-18 15:37:25 -0700275type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700276 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700277
Colin Cross9f35c3d2020-09-16 19:04:41 -0700278 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
279 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700280 CreateModule(ModuleFactory, ...interface{}) Module
Jingwen Chen1fd14692021-02-05 03:01:50 -0500281
282 // CreateBazelTargetModule creates a BazelTargetModule by calling the
283 // factory method, just like in CreateModule, but also requires
284 // BazelTargetModuleProperties containing additional metadata for the
285 // bp2build codegenerator.
Liz Kammerfc46bc12021-02-19 11:06:17 -0500286 CreateBazelTargetModule(ModuleFactory, string, bazel.BazelTargetModuleProperties, interface{}) BazelTargetModule
Colin Cross6362e272015-10-29 15:25:03 -0700287}
288
Colin Cross25de6c32019-06-06 14:29:25 -0700289type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700290 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700291 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700292}
293
Colin Cross25de6c32019-06-06 14:29:25 -0700294type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700295
Colin Cross635c3b02016-05-18 15:37:25 -0700296type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700297 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800298
Colin Cross4f1dcb02020-09-16 18:45:04 -0700299 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
300 // dependency (some entries may be nil).
301 //
302 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
303 // new dependencies have had the current mutator called on them. If the mutator is not
304 // parallel this method does not affect the ordering of the current mutator pass, but will
305 // be ordered correctly for all future mutator passes.
306 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700307
308 // AddReverseDependency adds a dependency from the destination to the given module.
309 // Does not affect the ordering of the current mutator pass, but will be ordered
310 // correctly for all future mutator passes. All reverse dependencies for a destination module are
311 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
312 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800313 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700314
315 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
316 // parameter. It returns a list of new modules in the same order as the variationNames
317 // list.
318 //
319 // If any of the dependencies of the module being operated on were already split
320 // by calling CreateVariations with the same name, the dependency will automatically
321 // be updated to point the matching variant.
322 //
323 // If a module is split, and then a module depending on the first module is not split
324 // when the Mutator is later called on it, the dependency of the depending module will
325 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800326 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700327
328 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
329 // parameter. It returns a list of new modules in the same order as the variantNames
330 // list.
331 //
332 // Local variations do not affect automatic dependency resolution - dependencies added
333 // to the split module via deps or DynamicDependerModule must exactly match a variant
334 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800335 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700336
337 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
338 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800339 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700340
341 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
342 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900343 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700344
345 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700346 // argument to select which variant of the dependency to use. It returns a slice of modules for
347 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
348 // the all of the non-local variations of the current module, plus the variations argument.
349 //
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 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700355
356 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700357 // variations argument to select which variant of the dependency to use. It returns a slice of
358 // modules for each dependency (some entries may be nil). A variant of the dependency must
359 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700360 // For any unspecified variation the first variant will be used.
361 //
362 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
363 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700364 //
365 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
366 // new dependencies have had the current mutator called on them. If the mutator is not
367 // parallel this method does not affect the ordering of the current mutator pass, but will
368 // be ordered correctly for all future mutator passes.
369 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700370
371 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
372 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
373 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
374 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800375 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700376
377 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
378 // specified name with the current variant of this module. Replacements don't take effect until
379 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800380 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700381
382 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
383 // specified name with the current variant of this module as long as the supplied predicate returns
384 // true.
385 //
386 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100387 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700388
389 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
390 // and creates an alias from the current variant (before the mutator has run) to the new
391 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
392 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
393 // be used to add dependencies on the newly created variant using the variant map from
394 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800395 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700396
397 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
398 // module, and creates an alias from a new fromVariationName variant the toVariationName
399 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
400 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
401 // be used to add dependencies on the toVariationName variant using the fromVariationName
402 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700403 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700404
405 // SetVariationProvider sets the value for a provider for the given newly created variant of
406 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
407 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
408 // if the value is not of the appropriate type, or if the module is not a newly created
409 // variant of the current module. The value should not be modified after being passed to
410 // SetVariationProvider.
411 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Liz Kammer356f7d42021-01-26 09:18:53 -0500412
413 // BazelConversionMode returns whether this mutator is being run as part of Bazel Conversion.
414 BazelConversionMode() bool
Colin Cross6362e272015-10-29 15:25:03 -0700415}
416
Colin Cross25de6c32019-06-06 14:29:25 -0700417type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700418 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700419 baseModuleContext
Liz Kammer356f7d42021-01-26 09:18:53 -0500420 finalPhase bool
421 bazelConversionMode bool
Colin Cross6362e272015-10-29 15:25:03 -0700422}
423
Colin Cross617b88a2020-08-24 18:04:09 -0700424func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500425 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700426
427 return &bottomUpMutatorContext{
Liz Kammer356f7d42021-01-26 09:18:53 -0500428 bp: ctx,
429 baseModuleContext: a.base().baseModuleContextFactory(ctx),
430 finalPhase: finalPhase,
431 bazelConversionMode: bazelConversionMode,
Colin Cross617b88a2020-08-24 18:04:09 -0700432 }
433}
434
Colin Cross25de6c32019-06-06 14:29:25 -0700435func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000436 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500437 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700438 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700439 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500440 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700441 }
Colin Cross798bfce2016-10-12 14:28:16 -0700442 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500443 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700444 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700445 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700446}
447
Colin Cross617b88a2020-08-24 18:04:09 -0700448func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
449 mutator := &mutator{name: name, bottomUpMutator: m}
450 x.mutators = append(x.mutators, mutator)
451 return mutator
452}
453
Liz Kammer356f7d42021-01-26 09:18:53 -0500454func (x *registerMutatorsContext) mutatorName(name string) string {
455 if x.bazelConversionMode {
456 return name + "_bp2build"
457 }
458 return name
459}
460
Colin Cross25de6c32019-06-06 14:29:25 -0700461func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700462 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700463 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700464 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700465 bp: ctx,
466 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700467 }
Colin Cross798bfce2016-10-12 14:28:16 -0700468 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700469 }
Colin Cross798bfce2016-10-12 14:28:16 -0700470 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500471 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700472 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700473 return mutator
474}
475
476type MutatorHandle interface {
477 Parallel() MutatorHandle
478}
479
480func (mutator *mutator) Parallel() MutatorHandle {
481 mutator.parallel = true
482 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700483}
Colin Cross1e676be2016-10-12 14:38:15 -0700484
Paul Duffin44f1d842020-06-26 20:17:02 +0100485func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
486 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
487}
488
489// A special mutator that runs just prior to the deps mutator to allow the dependencies
490// on component modules to be added so that they can depend directly on a prebuilt
491// module.
492func componentDepsMutator(ctx BottomUpMutatorContext) {
493 if m := ctx.Module(); m.Enabled() {
494 m.ComponentDepsMutator(ctx)
495 }
496}
497
Colin Cross1e676be2016-10-12 14:38:15 -0700498func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100499 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700500 m.DepsMutator(ctx)
501 }
502}
Colin Crossd11fcda2017-10-23 17:59:01 -0700503
Liz Kammer356f7d42021-01-26 09:18:53 -0500504func registerDepsMutator(ctx RegisterMutatorsContext) {
505 ctx.BottomUp("deps", depsMutator).Parallel()
506}
507
508func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
509 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
510 // being converted to build targets.
511 ctx.BottomUp("deps", depsMutator).Parallel()
512}
513
Jingwen Chen1fd14692021-02-05 03:01:50 -0500514func (t *topDownMutatorContext) CreateBazelTargetModule(
515 factory ModuleFactory,
Liz Kammerfc46bc12021-02-19 11:06:17 -0500516 name string,
Jingwen Chen1fd14692021-02-05 03:01:50 -0500517 bazelProps bazel.BazelTargetModuleProperties,
518 attrs interface{}) BazelTargetModule {
Liz Kammerfc46bc12021-02-19 11:06:17 -0500519 if strings.HasPrefix(name, bazel.BazelTargetModuleNamePrefix) {
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500520 panic(fmt.Errorf(
Liz Kammerfc46bc12021-02-19 11:06:17 -0500521 "The %s name prefix is added automatically, do not set it manually: %s",
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500522 bazel.BazelTargetModuleNamePrefix,
Liz Kammerfc46bc12021-02-19 11:06:17 -0500523 name))
524 }
525 name = bazel.BazelTargetModuleNamePrefix + name
526 nameProp := struct {
527 Name *string
528 }{
529 Name: &name,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500530 }
531
Liz Kammerfc46bc12021-02-19 11:06:17 -0500532 b := t.CreateModule(factory, &nameProp, attrs).(BazelTargetModule)
533 b.SetBazelTargetModuleProperties(bazelProps)
534 return b
Jingwen Chen1fd14692021-02-05 03:01:50 -0500535}
536
Colin Cross25de6c32019-06-06 14:29:25 -0700537func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700538 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700539 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700540 p, nil)
541 if err != nil {
542 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700543 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700544 } else {
545 panic(err)
546 }
547 }
548 }
549}
550
Colin Cross25de6c32019-06-06 14:29:25 -0700551func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700552 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700553 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700554 p, nil)
555 if err != nil {
556 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700557 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700558 } else {
559 panic(err)
560 }
561 }
562 }
563}
Colin Crossdc35e212019-06-06 16:13:11 -0700564
565// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
566// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
567// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
568// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
569// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
570
Colin Crosscb55e082019-07-01 15:32:31 -0700571func (t *topDownMutatorContext) MutatorName() string {
572 return t.bp.MutatorName()
573}
574
Colin Crossdc35e212019-06-06 16:13:11 -0700575func (t *topDownMutatorContext) Rename(name string) {
576 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700577 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700578}
579
Colin Crosse003c4a2019-09-25 12:58:36 -0700580func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Colin Cross18c46802019-09-24 22:19:02 -0700581 inherited := []interface{}{&t.Module().base().commonProperties}
Colin Crosse003c4a2019-09-25 12:58:36 -0700582 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
Colin Cross18c46802019-09-24 22:19:02 -0700583
584 if t.Module().base().variableProperties != nil && module.base().variableProperties != nil {
585 src := t.Module().base().variableProperties
586 dst := []interface{}{
587 module.base().variableProperties,
588 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
589 // don't cause a "failed to find property to extend" error.
Colin Cross43e789d2020-01-28 09:46:50 -0800590 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross18c46802019-09-24 22:19:02 -0700591 }
592 err := proptools.AppendMatchingProperties(dst, src, nil)
593 if err != nil {
594 panic(err)
595 }
596 }
597
Colin Crosse003c4a2019-09-25 12:58:36 -0700598 return module
Colin Crossdc35e212019-06-06 16:13:11 -0700599}
600
Colin Crosscb55e082019-07-01 15:32:31 -0700601func (b *bottomUpMutatorContext) MutatorName() string {
602 return b.bp.MutatorName()
603}
604
Colin Crossdc35e212019-06-06 16:13:11 -0700605func (b *bottomUpMutatorContext) Rename(name string) {
606 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700607 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700608}
609
Colin Cross4f1dcb02020-09-16 18:45:04 -0700610func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
611 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700612}
613
614func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
615 b.bp.AddReverseDependency(module, tag, name)
616}
617
Colin Cross43b92e02019-11-18 15:28:57 -0800618func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000619 if b.finalPhase {
620 panic("CreateVariations not allowed in FinalDepsMutators")
621 }
622
Colin Cross9a362232019-07-01 15:32:45 -0700623 modules := b.bp.CreateVariations(variations...)
624
Colin Cross43b92e02019-11-18 15:28:57 -0800625 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700626 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800627 aModules[i] = modules[i].(Module)
628 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700629 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
630 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
631 }
632
Colin Cross43b92e02019-11-18 15:28:57 -0800633 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700634}
635
Colin Cross43b92e02019-11-18 15:28:57 -0800636func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000637 if b.finalPhase {
638 panic("CreateLocalVariations not allowed in FinalDepsMutators")
639 }
640
Colin Cross9a362232019-07-01 15:32:45 -0700641 modules := b.bp.CreateLocalVariations(variations...)
642
Colin Cross43b92e02019-11-18 15:28:57 -0800643 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700644 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800645 aModules[i] = modules[i].(Module)
646 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700647 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
648 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
649 }
650
Colin Cross43b92e02019-11-18 15:28:57 -0800651 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700652}
653
654func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
655 b.bp.SetDependencyVariation(variation)
656}
657
Jiyong Park1d1119f2019-07-29 21:27:18 +0900658func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
659 b.bp.SetDefaultDependencyVariation(variation)
660}
661
Colin Crossdc35e212019-06-06 16:13:11 -0700662func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700663 names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500664 if b.bazelConversionMode {
665 _, noSelfDeps := RemoveFromList(b.ModuleName(), names)
666 if len(noSelfDeps) == 0 {
667 return []blueprint.Module(nil)
668 }
669 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
670 // dependency, the variations would not exist and the dependency could not be added, by
671 // specifying no variations, we will allow adding the dependency to succeed.
672 return b.bp.AddFarVariationDependencies(nil, tag, noSelfDeps...)
673 }
Colin Crossdc35e212019-06-06 16:13:11 -0700674
Colin Cross4f1dcb02020-09-16 18:45:04 -0700675 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700676}
677
678func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700679 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500680 if b.bazelConversionMode {
681 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
682 // dependency, the variations would not exist and the dependency could not be added, by
683 // specifying no variations, we will allow adding the dependency to succeed.
684 return b.bp.AddFarVariationDependencies(nil, tag, names...)
685 }
Colin Crossdc35e212019-06-06 16:13:11 -0700686
Colin Cross4f1dcb02020-09-16 18:45:04 -0700687 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700688}
689
690func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
691 b.bp.AddInterVariantDependency(tag, from, to)
692}
693
694func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
695 b.bp.ReplaceDependencies(name)
696}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800697
Paul Duffin80342d72020-06-26 22:08:43 +0100698func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
699 b.bp.ReplaceDependenciesIf(name, predicate)
700}
701
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800702func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
703 b.bp.AliasVariation(variationName)
704}
Colin Cross1b9604b2020-08-11 12:03:56 -0700705
706func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
707 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
708}
Colin Crossd27e7b82020-07-02 11:38:17 -0700709
710func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
711 b.bp.SetVariationProvider(module, provider, value)
712}
Liz Kammer356f7d42021-01-26 09:18:53 -0500713
714func (b *bottomUpMutatorContext) BazelConversionMode() bool {
715 return b.bazelConversionMode
716}