blob: 9552aa15c106ec2ae07ca68adffe1745cb5436d5 [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
Jingwen Chen73850672020-12-14 08:25:34 -050036// RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
Paul Duffin1d2d42f2021-03-06 20:08:12 +000037func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators, depsMutators, bp2buildMutators []RegisterMutatorFunc) {
Liz Kammer356f7d42021-01-26 09:18:53 -050038 mctx := &registerMutatorsContext{
39 bazelConversionMode: true,
Jingwen Chen041b1842021-02-01 00:23:25 -050040 }
41
Liz Kammer356f7d42021-01-26 09:18:53 -050042 bp2buildPreArchMutators = append([]RegisterMutatorFunc{
43 RegisterNamespaceMutator,
44 RegisterDefaultsPreArchMutators,
45 // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should
46 // evaluate the impact on conversion.
47 RegisterPrebuiltsPreArchMutators,
48 },
49 preArchMutators...)
50
51 for _, f := range bp2buildPreArchMutators {
52 f(mctx)
53 }
54
55 bp2buildDepsMutators = append([]RegisterMutatorFunc{
56 registerDepsMutatorBp2Build,
57 registerPathDepsMutator,
58 }, depsMutators...)
59
60 for _, f := range bp2buildDepsMutators {
Jingwen Chen041b1842021-02-01 00:23:25 -050061 f(mctx)
62 }
63
Jingwen Chen73850672020-12-14 08:25:34 -050064 // Register bp2build mutators
65 for _, f := range bp2buildMutators {
66 f(mctx)
67 }
68
Paul Duffin1d2d42f2021-03-06 20:08:12 +000069 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050070}
71
Paul Duffinc05b0342021-03-06 13:28:13 +000072// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
73// with the InitRegistrationContext and will be used at runtime.
74func collateGloballyRegisteredMutators() sortableComponents {
75 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
76}
77
78// collateRegisteredMutators constructs a single list of mutators from the separate lists.
79func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070080 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080081
82 register := func(funcs []RegisterMutatorFunc) {
83 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070084 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080085 }
86 }
87
Colin Crosscec81712017-07-13 14:43:27 -070088 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080089
Colin Crosscec81712017-07-13 14:43:27 -070090 register(preDeps)
91
Liz Kammer356f7d42021-01-26 09:18:53 -050092 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070093
94 register(postDeps)
95
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000096 mctx.finalPhase = true
97 register(finalDeps)
98
Paul Duffinc05b0342021-03-06 13:28:13 +000099 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -0700100}
101
102type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000103 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -0500104 finalPhase bool
105 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -0700106}
Colin Cross1e676be2016-10-12 14:38:15 -0700107
108type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -0700109 TopDown(name string, m TopDownMutator) MutatorHandle
110 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -0700111 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -0700112}
113
114type RegisterMutatorFunc func(RegisterMutatorsContext)
115
Colin Crosscec81712017-07-13 14:43:27 -0700116var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800117 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100118
Paul Duffinaa4162e2020-05-05 11:35:43 +0100119 // Check the visibility rules are valid.
120 //
121 // This must run after the package renamer mutators so that any issues found during
122 // validation of the package's default_visibility property are reported using the
123 // correct package name and not the synthetic name.
124 //
125 // This must also be run before defaults mutators as the rules for validation are
126 // different before checking the rules than they are afterwards. e.g.
127 // visibility: ["//visibility:private", "//visibility:public"]
128 // would be invalid if specified in a module definition but is valid if it results
129 // from something like this:
130 //
131 // defaults {
132 // name: "defaults",
133 // // Be inaccessible outside a package by default.
134 // visibility: ["//visibility:private"]
135 // }
136 //
137 // defaultable_module {
138 // name: "defaultable_module",
139 // defaults: ["defaults"],
140 // // Override the default.
141 // visibility: ["//visibility:public"]
142 // }
143 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000144 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100145
Bob Badour37af0462021-01-07 03:34:31 +0000146 // Record the default_applicable_licenses for each package.
147 //
148 // This must run before the defaults so that defaults modules can pick up the package default.
149 RegisterLicensesPackageMapper,
150
Paul Duffinaa4162e2020-05-05 11:35:43 +0100151 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100152 //
153 // Any mutators that are added before this will not see any modules created by
154 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700155 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100156
Paul Duffin44f1d842020-06-26 20:17:02 +0100157 // Add dependencies on any components so that any component references can be
158 // resolved within the deps mutator.
159 //
160 // Must be run after defaults so it can be used to create dependencies on the
161 // component modules that are creating in a DefaultableHook.
162 //
163 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
164 // renamed. That is so that if a module creates components using a prebuilt module
165 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
166 // the prebuilt module and not the source module.
167 RegisterComponentsMutator,
168
Paul Duffinc988c8e2020-04-29 18:27:14 +0100169 // Create an association between prebuilt modules and their corresponding source
170 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100171 //
172 // Must be run after defaults mutators to ensure that any modules created by
173 // a DefaultableHook can be either a prebuilt or a source module with a matching
174 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100175 RegisterPrebuiltsPreArchMutators,
176
Bob Badour37af0462021-01-07 03:34:31 +0000177 // Gather the licenses properties for all modules for use during expansion and enforcement.
178 //
179 // This must come after the defaults mutators to ensure that any licenses supplied
180 // in a defaults module has been successfully applied before the rules are gathered.
181 RegisterLicensesPropertyGatherer,
182
Paul Duffinaa4162e2020-05-05 11:35:43 +0100183 // Gather the visibility rules for all modules for us during visibility enforcement.
184 //
185 // This must come after the defaults mutators to ensure that any visibility supplied
186 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000187 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700188}
189
Colin Crossae4c6182017-09-15 17:33:55 -0700190func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700191 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800192 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700193 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700194}
195
Colin Crosscec81712017-07-13 14:43:27 -0700196var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700197 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700198}
199
200var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800201 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700202 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000203 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000204 RegisterLicensesDependencyChecker,
Artur Satayevc5570ac2020-04-09 16:06:36 +0100205 RegisterNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700206 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700207}
Colin Cross1e676be2016-10-12 14:38:15 -0700208
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000209var finalDeps = []RegisterMutatorFunc{}
210
Colin Cross1e676be2016-10-12 14:38:15 -0700211func PreArchMutators(f RegisterMutatorFunc) {
212 preArch = append(preArch, f)
213}
214
215func PreDepsMutators(f RegisterMutatorFunc) {
216 preDeps = append(preDeps, f)
217}
218
219func PostDepsMutators(f RegisterMutatorFunc) {
220 postDeps = append(postDeps, f)
221}
222
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000223func FinalDepsMutators(f RegisterMutatorFunc) {
224 finalDeps = append(finalDeps, f)
225}
226
Liz Kammer356f7d42021-01-26 09:18:53 -0500227var bp2buildPreArchMutators = []RegisterMutatorFunc{}
228var bp2buildDepsMutators = []RegisterMutatorFunc{}
Jingwen Chen73850672020-12-14 08:25:34 -0500229var bp2buildMutators = []RegisterMutatorFunc{}
230
231// RegisterBp2BuildMutator registers specially crafted mutators for
232// converting Blueprint/Android modules into special modules that can
233// be code-generated into Bazel BUILD targets.
234//
235// TODO(b/178068862): bring this into TestContext.
236func RegisterBp2BuildMutator(moduleType string, m func(TopDownMutatorContext)) {
Jingwen Chen73850672020-12-14 08:25:34 -0500237 f := func(ctx RegisterMutatorsContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500238 ctx.TopDown(moduleType, m)
Jingwen Chen73850672020-12-14 08:25:34 -0500239 }
240 bp2buildMutators = append(bp2buildMutators, f)
241}
242
Liz Kammer356f7d42021-01-26 09:18:53 -0500243// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
244// into Bazel BUILD targets that should run prior to deps and conversion.
245func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
246 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
247}
248
249// DepsBp2BuildMutators adds mutators to be register for converting Android Blueprint modules into
250// Bazel BUILD targets that should run prior to conversion to resolve dependencies.
251func DepsBp2BuildMutators(f RegisterMutatorFunc) {
252 bp2buildDepsMutators = append(bp2buildDepsMutators, f)
253}
254
Colin Cross9f35c3d2020-09-16 19:04:41 -0700255type BaseMutatorContext interface {
256 BaseModuleContext
257
258 // MutatorName returns the name that this mutator was registered with.
259 MutatorName() string
260
261 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
262 // AddDependency or OtherModuleName until after this mutator pass is complete.
263 Rename(name string)
264}
265
Colin Cross25de6c32019-06-06 14:29:25 -0700266type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700267
Colin Cross635c3b02016-05-18 15:37:25 -0700268type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700269 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700270
Colin Cross9f35c3d2020-09-16 19:04:41 -0700271 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
272 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700273 CreateModule(ModuleFactory, ...interface{}) Module
Jingwen Chen1fd14692021-02-05 03:01:50 -0500274
275 // CreateBazelTargetModule creates a BazelTargetModule by calling the
276 // factory method, just like in CreateModule, but also requires
277 // BazelTargetModuleProperties containing additional metadata for the
278 // bp2build codegenerator.
Liz Kammerfc46bc12021-02-19 11:06:17 -0500279 CreateBazelTargetModule(ModuleFactory, string, bazel.BazelTargetModuleProperties, interface{}) BazelTargetModule
Colin Cross6362e272015-10-29 15:25:03 -0700280}
281
Colin Cross25de6c32019-06-06 14:29:25 -0700282type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700283 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700284 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700285}
286
Colin Cross25de6c32019-06-06 14:29:25 -0700287type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700288
Colin Cross635c3b02016-05-18 15:37:25 -0700289type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700290 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800291
Colin Cross4f1dcb02020-09-16 18:45:04 -0700292 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
293 // dependency (some entries may be nil).
294 //
295 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
296 // new dependencies have had the current mutator called on them. If the mutator is not
297 // parallel this method does not affect the ordering of the current mutator pass, but will
298 // be ordered correctly for all future mutator passes.
299 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700300
301 // AddReverseDependency adds a dependency from the destination to the given module.
302 // Does not affect the ordering of the current mutator pass, but will be ordered
303 // correctly for all future mutator passes. All reverse dependencies for a destination module are
304 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
305 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800306 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700307
308 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
309 // parameter. It returns a list of new modules in the same order as the variationNames
310 // list.
311 //
312 // If any of the dependencies of the module being operated on were already split
313 // by calling CreateVariations with the same name, the dependency will automatically
314 // be updated to point the matching variant.
315 //
316 // If a module is split, and then a module depending on the first module is not split
317 // when the Mutator is later called on it, the dependency of the depending module will
318 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800319 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700320
321 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
322 // parameter. It returns a list of new modules in the same order as the variantNames
323 // list.
324 //
325 // Local variations do not affect automatic dependency resolution - dependencies added
326 // to the split module via deps or DynamicDependerModule must exactly match a variant
327 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800328 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700329
330 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
331 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800332 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700333
334 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
335 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900336 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700337
338 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700339 // argument to select which variant of the dependency to use. It returns a slice of modules for
340 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
341 // the all of the non-local variations of the current module, plus the variations argument.
342 //
343 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
344 // new dependencies have had the current mutator called on them. If the mutator is not
345 // parallel this method does not affect the ordering of the current mutator pass, but will
346 // be ordered correctly for all future mutator passes.
347 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700348
349 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700350 // variations argument to select which variant of the dependency to use. It returns a slice of
351 // modules for each dependency (some entries may be nil). A variant of the dependency must
352 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700353 // For any unspecified variation the first variant will be used.
354 //
355 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
356 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700357 //
358 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
359 // new dependencies have had the current mutator called on them. If the mutator is not
360 // parallel this method does not affect the ordering of the current mutator pass, but will
361 // be ordered correctly for all future mutator passes.
362 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700363
364 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
365 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
366 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
367 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800368 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700369
370 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
371 // specified name with the current variant of this module. Replacements don't take effect until
372 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800373 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700374
375 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
376 // specified name with the current variant of this module as long as the supplied predicate returns
377 // true.
378 //
379 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100380 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700381
382 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
383 // and creates an alias from the current variant (before the mutator has run) to the new
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 newly created variant using the variant map from
387 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800388 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700389
390 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
391 // module, and creates an alias from a new fromVariationName variant the toVariationName
392 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
393 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
394 // be used to add dependencies on the toVariationName variant using the fromVariationName
395 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700396 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700397
398 // SetVariationProvider sets the value for a provider for the given newly created variant of
399 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
400 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
401 // if the value is not of the appropriate type, or if the module is not a newly created
402 // variant of the current module. The value should not be modified after being passed to
403 // SetVariationProvider.
404 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Liz Kammer356f7d42021-01-26 09:18:53 -0500405
406 // BazelConversionMode returns whether this mutator is being run as part of Bazel Conversion.
407 BazelConversionMode() bool
Colin Cross6362e272015-10-29 15:25:03 -0700408}
409
Colin Cross25de6c32019-06-06 14:29:25 -0700410type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700411 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700412 baseModuleContext
Liz Kammer356f7d42021-01-26 09:18:53 -0500413 finalPhase bool
414 bazelConversionMode bool
Colin Cross6362e272015-10-29 15:25:03 -0700415}
416
Colin Cross617b88a2020-08-24 18:04:09 -0700417func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500418 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700419
420 return &bottomUpMutatorContext{
Liz Kammer356f7d42021-01-26 09:18:53 -0500421 bp: ctx,
422 baseModuleContext: a.base().baseModuleContextFactory(ctx),
423 finalPhase: finalPhase,
424 bazelConversionMode: bazelConversionMode,
Colin Cross617b88a2020-08-24 18:04:09 -0700425 }
426}
427
Colin Cross25de6c32019-06-06 14:29:25 -0700428func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000429 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500430 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700431 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700432 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500433 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700434 }
Colin Cross798bfce2016-10-12 14:28:16 -0700435 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500436 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700437 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700438 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700439}
440
Colin Cross617b88a2020-08-24 18:04:09 -0700441func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
442 mutator := &mutator{name: name, bottomUpMutator: m}
443 x.mutators = append(x.mutators, mutator)
444 return mutator
445}
446
Liz Kammer356f7d42021-01-26 09:18:53 -0500447func (x *registerMutatorsContext) mutatorName(name string) string {
448 if x.bazelConversionMode {
449 return name + "_bp2build"
450 }
451 return name
452}
453
Colin Cross25de6c32019-06-06 14:29:25 -0700454func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700455 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700456 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700457 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700458 bp: ctx,
459 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700460 }
Colin Cross798bfce2016-10-12 14:28:16 -0700461 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700462 }
Colin Cross798bfce2016-10-12 14:28:16 -0700463 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500464 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700465 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700466 return mutator
467}
468
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000469func (mutator *mutator) componentName() string {
470 return mutator.name
471}
472
473func (mutator *mutator) register(ctx *Context) {
474 blueprintCtx := ctx.Context
475 var handle blueprint.MutatorHandle
476 if mutator.bottomUpMutator != nil {
477 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
478 } else if mutator.topDownMutator != nil {
479 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
480 }
481 if mutator.parallel {
482 handle.Parallel()
483 }
484}
485
Colin Cross798bfce2016-10-12 14:28:16 -0700486type MutatorHandle interface {
487 Parallel() MutatorHandle
488}
489
490func (mutator *mutator) Parallel() MutatorHandle {
491 mutator.parallel = true
492 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700493}
Colin Cross1e676be2016-10-12 14:38:15 -0700494
Paul Duffin44f1d842020-06-26 20:17:02 +0100495func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
496 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
497}
498
499// A special mutator that runs just prior to the deps mutator to allow the dependencies
500// on component modules to be added so that they can depend directly on a prebuilt
501// module.
502func componentDepsMutator(ctx BottomUpMutatorContext) {
503 if m := ctx.Module(); m.Enabled() {
504 m.ComponentDepsMutator(ctx)
505 }
506}
507
Colin Cross1e676be2016-10-12 14:38:15 -0700508func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100509 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700510 m.DepsMutator(ctx)
511 }
512}
Colin Crossd11fcda2017-10-23 17:59:01 -0700513
Liz Kammer356f7d42021-01-26 09:18:53 -0500514func registerDepsMutator(ctx RegisterMutatorsContext) {
515 ctx.BottomUp("deps", depsMutator).Parallel()
516}
517
518func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
519 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
520 // being converted to build targets.
521 ctx.BottomUp("deps", depsMutator).Parallel()
522}
523
Jingwen Chen1fd14692021-02-05 03:01:50 -0500524func (t *topDownMutatorContext) CreateBazelTargetModule(
525 factory ModuleFactory,
Liz Kammerfc46bc12021-02-19 11:06:17 -0500526 name string,
Jingwen Chen1fd14692021-02-05 03:01:50 -0500527 bazelProps bazel.BazelTargetModuleProperties,
528 attrs interface{}) BazelTargetModule {
Liz Kammerfc46bc12021-02-19 11:06:17 -0500529 if strings.HasPrefix(name, bazel.BazelTargetModuleNamePrefix) {
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500530 panic(fmt.Errorf(
Liz Kammerfc46bc12021-02-19 11:06:17 -0500531 "The %s name prefix is added automatically, do not set it manually: %s",
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500532 bazel.BazelTargetModuleNamePrefix,
Liz Kammerfc46bc12021-02-19 11:06:17 -0500533 name))
534 }
535 name = bazel.BazelTargetModuleNamePrefix + name
536 nameProp := struct {
537 Name *string
538 }{
539 Name: &name,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500540 }
541
Liz Kammerfc46bc12021-02-19 11:06:17 -0500542 b := t.CreateModule(factory, &nameProp, attrs).(BazelTargetModule)
543 b.SetBazelTargetModuleProperties(bazelProps)
544 return b
Jingwen Chen1fd14692021-02-05 03:01:50 -0500545}
546
Colin Cross25de6c32019-06-06 14:29:25 -0700547func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700548 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700549 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700550 p, nil)
551 if err != nil {
552 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700553 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700554 } else {
555 panic(err)
556 }
557 }
558 }
559}
560
Colin Cross25de6c32019-06-06 14:29:25 -0700561func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700562 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700563 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700564 p, nil)
565 if err != nil {
566 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700567 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700568 } else {
569 panic(err)
570 }
571 }
572 }
573}
Colin Crossdc35e212019-06-06 16:13:11 -0700574
575// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
576// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
577// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
578// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
579// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
580
Colin Crosscb55e082019-07-01 15:32:31 -0700581func (t *topDownMutatorContext) MutatorName() string {
582 return t.bp.MutatorName()
583}
584
Colin Crossdc35e212019-06-06 16:13:11 -0700585func (t *topDownMutatorContext) Rename(name string) {
586 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700587 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700588}
589
Colin Crosse003c4a2019-09-25 12:58:36 -0700590func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Colin Cross18c46802019-09-24 22:19:02 -0700591 inherited := []interface{}{&t.Module().base().commonProperties}
Colin Crosse003c4a2019-09-25 12:58:36 -0700592 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
Colin Cross18c46802019-09-24 22:19:02 -0700593
594 if t.Module().base().variableProperties != nil && module.base().variableProperties != nil {
595 src := t.Module().base().variableProperties
596 dst := []interface{}{
597 module.base().variableProperties,
598 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
599 // don't cause a "failed to find property to extend" error.
Colin Cross43e789d2020-01-28 09:46:50 -0800600 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross18c46802019-09-24 22:19:02 -0700601 }
602 err := proptools.AppendMatchingProperties(dst, src, nil)
603 if err != nil {
604 panic(err)
605 }
606 }
607
Colin Crosse003c4a2019-09-25 12:58:36 -0700608 return module
Colin Crossdc35e212019-06-06 16:13:11 -0700609}
610
Colin Crosscb55e082019-07-01 15:32:31 -0700611func (b *bottomUpMutatorContext) MutatorName() string {
612 return b.bp.MutatorName()
613}
614
Colin Crossdc35e212019-06-06 16:13:11 -0700615func (b *bottomUpMutatorContext) Rename(name string) {
616 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700617 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700618}
619
Colin Cross4f1dcb02020-09-16 18:45:04 -0700620func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
621 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700622}
623
624func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
625 b.bp.AddReverseDependency(module, tag, name)
626}
627
Colin Cross43b92e02019-11-18 15:28:57 -0800628func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000629 if b.finalPhase {
630 panic("CreateVariations not allowed in FinalDepsMutators")
631 }
632
Colin Cross9a362232019-07-01 15:32:45 -0700633 modules := b.bp.CreateVariations(variations...)
634
Colin Cross43b92e02019-11-18 15:28:57 -0800635 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700636 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800637 aModules[i] = modules[i].(Module)
638 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700639 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
640 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
641 }
642
Colin Cross43b92e02019-11-18 15:28:57 -0800643 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700644}
645
Colin Cross43b92e02019-11-18 15:28:57 -0800646func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000647 if b.finalPhase {
648 panic("CreateLocalVariations not allowed in FinalDepsMutators")
649 }
650
Colin Cross9a362232019-07-01 15:32:45 -0700651 modules := b.bp.CreateLocalVariations(variations...)
652
Colin Cross43b92e02019-11-18 15:28:57 -0800653 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700654 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800655 aModules[i] = modules[i].(Module)
656 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700657 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
658 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
659 }
660
Colin Cross43b92e02019-11-18 15:28:57 -0800661 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700662}
663
664func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
665 b.bp.SetDependencyVariation(variation)
666}
667
Jiyong Park1d1119f2019-07-29 21:27:18 +0900668func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
669 b.bp.SetDefaultDependencyVariation(variation)
670}
671
Colin Crossdc35e212019-06-06 16:13:11 -0700672func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700673 names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500674 if b.bazelConversionMode {
675 _, noSelfDeps := RemoveFromList(b.ModuleName(), names)
676 if len(noSelfDeps) == 0 {
677 return []blueprint.Module(nil)
678 }
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, noSelfDeps...)
683 }
Colin Crossdc35e212019-06-06 16:13:11 -0700684
Colin Cross4f1dcb02020-09-16 18:45:04 -0700685 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700686}
687
688func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700689 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500690 if b.bazelConversionMode {
691 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
692 // dependency, the variations would not exist and the dependency could not be added, by
693 // specifying no variations, we will allow adding the dependency to succeed.
694 return b.bp.AddFarVariationDependencies(nil, tag, names...)
695 }
Colin Crossdc35e212019-06-06 16:13:11 -0700696
Colin Cross4f1dcb02020-09-16 18:45:04 -0700697 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700698}
699
700func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
701 b.bp.AddInterVariantDependency(tag, from, to)
702}
703
704func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
705 b.bp.ReplaceDependencies(name)
706}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800707
Paul Duffin80342d72020-06-26 22:08:43 +0100708func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
709 b.bp.ReplaceDependenciesIf(name, predicate)
710}
711
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800712func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
713 b.bp.AliasVariation(variationName)
714}
Colin Cross1b9604b2020-08-11 12:03:56 -0700715
716func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
717 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
718}
Colin Crossd27e7b82020-07-02 11:38:17 -0700719
720func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
721 b.bp.SetVariationProvider(module, provider, value)
722}
Liz Kammer356f7d42021-01-26 09:18:53 -0500723
724func (b *bottomUpMutatorContext) BazelConversionMode() bool {
725 return b.bazelConversionMode
726}