blob: 819dd0f2e71d2a44de1d1a9b490b296e11a97167 [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.
Paul Duffin1d2d42f2021-03-06 20:08:12 +000038func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators, depsMutators, 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
56 bp2buildDepsMutators = append([]RegisterMutatorFunc{
57 registerDepsMutatorBp2Build,
58 registerPathDepsMutator,
Liz Kammer4562a3b2021-04-21 18:15:34 -040059 registerBp2buildArchPathDepsMutator,
Liz Kammer356f7d42021-01-26 09:18:53 -050060 }, depsMutators...)
61
62 for _, f := range bp2buildDepsMutators {
Jingwen Chen041b1842021-02-01 00:23:25 -050063 f(mctx)
64 }
65
Jingwen Chen73850672020-12-14 08:25:34 -050066 // Register bp2build mutators
67 for _, f := range bp2buildMutators {
68 f(mctx)
69 }
70
Paul Duffin1d2d42f2021-03-06 20:08:12 +000071 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050072}
73
Paul Duffinc05b0342021-03-06 13:28:13 +000074// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
75// with the InitRegistrationContext and will be used at runtime.
76func collateGloballyRegisteredMutators() sortableComponents {
77 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
78}
79
80// collateRegisteredMutators constructs a single list of mutators from the separate lists.
81func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070082 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080083
84 register := func(funcs []RegisterMutatorFunc) {
85 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070086 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080087 }
88 }
89
Colin Crosscec81712017-07-13 14:43:27 -070090 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080091
Colin Crosscec81712017-07-13 14:43:27 -070092 register(preDeps)
93
Liz Kammer356f7d42021-01-26 09:18:53 -050094 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070095
96 register(postDeps)
97
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000098 mctx.finalPhase = true
99 register(finalDeps)
100
Paul Duffinc05b0342021-03-06 13:28:13 +0000101 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -0700102}
103
104type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000105 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -0500106 finalPhase bool
107 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -0700108}
Colin Cross1e676be2016-10-12 14:38:15 -0700109
110type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -0700111 TopDown(name string, m TopDownMutator) MutatorHandle
112 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -0700113 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -0700114}
115
116type RegisterMutatorFunc func(RegisterMutatorsContext)
117
Colin Crosscec81712017-07-13 14:43:27 -0700118var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800119 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100120
Paul Duffinaa4162e2020-05-05 11:35:43 +0100121 // Check the visibility rules are valid.
122 //
123 // This must run after the package renamer mutators so that any issues found during
124 // validation of the package's default_visibility property are reported using the
125 // correct package name and not the synthetic name.
126 //
127 // This must also be run before defaults mutators as the rules for validation are
128 // different before checking the rules than they are afterwards. e.g.
129 // visibility: ["//visibility:private", "//visibility:public"]
130 // would be invalid if specified in a module definition but is valid if it results
131 // from something like this:
132 //
133 // defaults {
134 // name: "defaults",
135 // // Be inaccessible outside a package by default.
136 // visibility: ["//visibility:private"]
137 // }
138 //
139 // defaultable_module {
140 // name: "defaultable_module",
141 // defaults: ["defaults"],
142 // // Override the default.
143 // visibility: ["//visibility:public"]
144 // }
145 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000146 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100147
Bob Badour37af0462021-01-07 03:34:31 +0000148 // Record the default_applicable_licenses for each package.
149 //
150 // This must run before the defaults so that defaults modules can pick up the package default.
151 RegisterLicensesPackageMapper,
152
Paul Duffinaa4162e2020-05-05 11:35:43 +0100153 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100154 //
155 // Any mutators that are added before this will not see any modules created by
156 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700157 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100158
Paul Duffin44f1d842020-06-26 20:17:02 +0100159 // Add dependencies on any components so that any component references can be
160 // resolved within the deps mutator.
161 //
162 // Must be run after defaults so it can be used to create dependencies on the
163 // component modules that are creating in a DefaultableHook.
164 //
165 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
166 // renamed. That is so that if a module creates components using a prebuilt module
167 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
168 // the prebuilt module and not the source module.
169 RegisterComponentsMutator,
170
Paul Duffinc988c8e2020-04-29 18:27:14 +0100171 // Create an association between prebuilt modules and their corresponding source
172 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100173 //
174 // Must be run after defaults mutators to ensure that any modules created by
175 // a DefaultableHook can be either a prebuilt or a source module with a matching
176 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100177 RegisterPrebuiltsPreArchMutators,
178
Bob Badour37af0462021-01-07 03:34:31 +0000179 // Gather the licenses properties for all modules for use during expansion and enforcement.
180 //
181 // This must come after the defaults mutators to ensure that any licenses supplied
182 // in a defaults module has been successfully applied before the rules are gathered.
183 RegisterLicensesPropertyGatherer,
184
Paul Duffinaa4162e2020-05-05 11:35:43 +0100185 // Gather the visibility rules for all modules for us during visibility enforcement.
186 //
187 // This must come after the defaults mutators to ensure that any visibility supplied
188 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000189 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700190}
191
Colin Crossae4c6182017-09-15 17:33:55 -0700192func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700193 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800194 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700195 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700196}
197
Colin Crosscec81712017-07-13 14:43:27 -0700198var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700199 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700200}
201
202var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800203 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700204 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000205 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000206 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100207 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700208 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700209}
Colin Cross1e676be2016-10-12 14:38:15 -0700210
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000211var finalDeps = []RegisterMutatorFunc{}
212
Colin Cross1e676be2016-10-12 14:38:15 -0700213func PreArchMutators(f RegisterMutatorFunc) {
214 preArch = append(preArch, f)
215}
216
217func PreDepsMutators(f RegisterMutatorFunc) {
218 preDeps = append(preDeps, f)
219}
220
221func PostDepsMutators(f RegisterMutatorFunc) {
222 postDeps = append(postDeps, f)
223}
224
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000225func FinalDepsMutators(f RegisterMutatorFunc) {
226 finalDeps = append(finalDeps, f)
227}
228
Liz Kammer356f7d42021-01-26 09:18:53 -0500229var bp2buildPreArchMutators = []RegisterMutatorFunc{}
230var bp2buildDepsMutators = []RegisterMutatorFunc{}
Jingwen Chen12b4c272021-03-10 02:05:59 -0500231var bp2buildMutators = map[string]RegisterMutatorFunc{}
Jingwen Chen73850672020-12-14 08:25:34 -0500232
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400233// See http://b/192523357
234var bp2buildLock sync.Mutex
235
Jingwen Chen73850672020-12-14 08:25:34 -0500236// RegisterBp2BuildMutator registers specially crafted mutators for
237// converting Blueprint/Android modules into special modules that can
238// be code-generated into Bazel BUILD targets.
239//
240// TODO(b/178068862): bring this into TestContext.
241func RegisterBp2BuildMutator(moduleType string, m func(TopDownMutatorContext)) {
Jingwen Chen73850672020-12-14 08:25:34 -0500242 f := func(ctx RegisterMutatorsContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500243 ctx.TopDown(moduleType, m)
Jingwen Chen73850672020-12-14 08:25:34 -0500244 }
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400245 // Use a lock to avoid a concurrent map write if RegisterBp2BuildMutator is called in parallel
246 bp2buildLock.Lock()
247 defer bp2buildLock.Unlock()
Jingwen Chen12b4c272021-03-10 02:05:59 -0500248 bp2buildMutators[moduleType] = f
Jingwen Chen73850672020-12-14 08:25:34 -0500249}
250
Liz Kammer356f7d42021-01-26 09:18:53 -0500251// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
252// into Bazel BUILD targets that should run prior to deps and conversion.
253func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
254 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
255}
256
257// DepsBp2BuildMutators adds mutators to be register for converting Android Blueprint modules into
258// Bazel BUILD targets that should run prior to conversion to resolve dependencies.
259func DepsBp2BuildMutators(f RegisterMutatorFunc) {
260 bp2buildDepsMutators = append(bp2buildDepsMutators, f)
261}
262
Colin Cross9f35c3d2020-09-16 19:04:41 -0700263type BaseMutatorContext interface {
264 BaseModuleContext
265
266 // MutatorName returns the name that this mutator was registered with.
267 MutatorName() string
268
269 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
270 // AddDependency or OtherModuleName until after this mutator pass is complete.
271 Rename(name string)
272}
273
Colin Cross25de6c32019-06-06 14:29:25 -0700274type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700275
Colin Cross635c3b02016-05-18 15:37:25 -0700276type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700277 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700278
Colin Cross9f35c3d2020-09-16 19:04:41 -0700279 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
280 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700281 CreateModule(ModuleFactory, ...interface{}) Module
Jingwen Chen1fd14692021-02-05 03:01:50 -0500282
283 // CreateBazelTargetModule creates a BazelTargetModule by calling the
284 // factory method, just like in CreateModule, but also requires
285 // BazelTargetModuleProperties containing additional metadata for the
286 // bp2build codegenerator.
Liz Kammerfc46bc12021-02-19 11:06:17 -0500287 CreateBazelTargetModule(ModuleFactory, string, bazel.BazelTargetModuleProperties, interface{}) BazelTargetModule
Colin Cross6362e272015-10-29 15:25:03 -0700288}
289
Colin Cross25de6c32019-06-06 14:29:25 -0700290type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700291 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700292 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700293}
294
Colin Cross25de6c32019-06-06 14:29:25 -0700295type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700296
Colin Cross635c3b02016-05-18 15:37:25 -0700297type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700298 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800299
Colin Cross4f1dcb02020-09-16 18:45:04 -0700300 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
301 // dependency (some entries may be nil).
302 //
303 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
304 // new dependencies have had the current mutator called on them. If the mutator is not
305 // parallel this method does not affect the ordering of the current mutator pass, but will
306 // be ordered correctly for all future mutator passes.
307 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700308
309 // AddReverseDependency adds a dependency from the destination to the given module.
310 // Does not affect the ordering of the current mutator pass, but will be ordered
311 // correctly for all future mutator passes. All reverse dependencies for a destination module are
312 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
313 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800314 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700315
316 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
317 // parameter. It returns a list of new modules in the same order as the variationNames
318 // list.
319 //
320 // If any of the dependencies of the module being operated on were already split
321 // by calling CreateVariations with the same name, the dependency will automatically
322 // be updated to point the matching variant.
323 //
324 // If a module is split, and then a module depending on the first module is not split
325 // when the Mutator is later called on it, the dependency of the depending module will
326 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800327 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700328
329 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
330 // parameter. It returns a list of new modules in the same order as the variantNames
331 // list.
332 //
333 // Local variations do not affect automatic dependency resolution - dependencies added
334 // to the split module via deps or DynamicDependerModule must exactly match a variant
335 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800336 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700337
338 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
339 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800340 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700341
342 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
343 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900344 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700345
346 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700347 // argument to select which variant of the dependency to use. It returns a slice of modules for
348 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
349 // the all of the non-local variations of the current module, plus the variations argument.
350 //
351 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
352 // new dependencies have had the current mutator called on them. If the mutator is not
353 // parallel this method does not affect the ordering of the current mutator pass, but will
354 // be ordered correctly for all future mutator passes.
355 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700356
357 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700358 // variations argument to select which variant of the dependency to use. It returns a slice of
359 // modules for each dependency (some entries may be nil). A variant of the dependency must
360 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700361 // For any unspecified variation the first variant will be used.
362 //
363 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
364 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700365 //
366 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
367 // new dependencies have had the current mutator called on them. If the mutator is not
368 // parallel this method does not affect the ordering of the current mutator pass, but will
369 // be ordered correctly for all future mutator passes.
370 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700371
372 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
373 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
374 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
375 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800376 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700377
378 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
379 // specified name with the current variant of this module. Replacements don't take effect until
380 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800381 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700382
383 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
384 // specified name with the current variant of this module as long as the supplied predicate returns
385 // true.
386 //
387 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100388 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700389
390 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
391 // and creates an alias from the current variant (before the mutator has run) to the new
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 newly created variant using the variant map from
395 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800396 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700397
398 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
399 // module, and creates an alias from a new fromVariationName variant the toVariationName
400 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
401 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
402 // be used to add dependencies on the toVariationName variant using the fromVariationName
403 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700404 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700405
406 // SetVariationProvider sets the value for a provider for the given newly created variant of
407 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
408 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
409 // if the value is not of the appropriate type, or if the module is not a newly created
410 // variant of the current module. The value should not be modified after being passed to
411 // SetVariationProvider.
412 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Liz Kammer356f7d42021-01-26 09:18:53 -0500413
414 // BazelConversionMode returns whether this mutator is being run as part of Bazel Conversion.
415 BazelConversionMode() bool
Colin Cross6362e272015-10-29 15:25:03 -0700416}
417
Colin Cross25de6c32019-06-06 14:29:25 -0700418type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700419 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700420 baseModuleContext
Liz Kammer356f7d42021-01-26 09:18:53 -0500421 finalPhase bool
422 bazelConversionMode bool
Colin Cross6362e272015-10-29 15:25:03 -0700423}
424
Colin Cross617b88a2020-08-24 18:04:09 -0700425func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500426 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700427
428 return &bottomUpMutatorContext{
Liz Kammer356f7d42021-01-26 09:18:53 -0500429 bp: ctx,
430 baseModuleContext: a.base().baseModuleContextFactory(ctx),
431 finalPhase: finalPhase,
432 bazelConversionMode: bazelConversionMode,
Colin Cross617b88a2020-08-24 18:04:09 -0700433 }
434}
435
Colin Cross25de6c32019-06-06 14:29:25 -0700436func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000437 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500438 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700439 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700440 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500441 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700442 }
Colin Cross798bfce2016-10-12 14:28:16 -0700443 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500444 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700445 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700446 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700447}
448
Colin Cross617b88a2020-08-24 18:04:09 -0700449func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
450 mutator := &mutator{name: name, bottomUpMutator: m}
451 x.mutators = append(x.mutators, mutator)
452 return mutator
453}
454
Liz Kammer356f7d42021-01-26 09:18:53 -0500455func (x *registerMutatorsContext) mutatorName(name string) string {
456 if x.bazelConversionMode {
457 return name + "_bp2build"
458 }
459 return name
460}
461
Colin Cross25de6c32019-06-06 14:29:25 -0700462func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700463 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700464 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700465 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700466 bp: ctx,
467 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700468 }
Colin Cross798bfce2016-10-12 14:28:16 -0700469 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700470 }
Colin Cross798bfce2016-10-12 14:28:16 -0700471 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500472 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700473 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700474 return mutator
475}
476
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000477func (mutator *mutator) componentName() string {
478 return mutator.name
479}
480
481func (mutator *mutator) register(ctx *Context) {
482 blueprintCtx := ctx.Context
483 var handle blueprint.MutatorHandle
484 if mutator.bottomUpMutator != nil {
485 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
486 } else if mutator.topDownMutator != nil {
487 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
488 }
489 if mutator.parallel {
490 handle.Parallel()
491 }
492}
493
Colin Cross798bfce2016-10-12 14:28:16 -0700494type MutatorHandle interface {
495 Parallel() MutatorHandle
496}
497
498func (mutator *mutator) Parallel() MutatorHandle {
499 mutator.parallel = true
500 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700501}
Colin Cross1e676be2016-10-12 14:38:15 -0700502
Paul Duffin44f1d842020-06-26 20:17:02 +0100503func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
504 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
505}
506
507// A special mutator that runs just prior to the deps mutator to allow the dependencies
508// on component modules to be added so that they can depend directly on a prebuilt
509// module.
510func componentDepsMutator(ctx BottomUpMutatorContext) {
511 if m := ctx.Module(); m.Enabled() {
512 m.ComponentDepsMutator(ctx)
513 }
514}
515
Colin Cross1e676be2016-10-12 14:38:15 -0700516func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100517 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700518 m.DepsMutator(ctx)
519 }
520}
Colin Crossd11fcda2017-10-23 17:59:01 -0700521
Liz Kammer356f7d42021-01-26 09:18:53 -0500522func registerDepsMutator(ctx RegisterMutatorsContext) {
523 ctx.BottomUp("deps", depsMutator).Parallel()
524}
525
526func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
527 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
528 // being converted to build targets.
529 ctx.BottomUp("deps", depsMutator).Parallel()
530}
531
Jingwen Chen1fd14692021-02-05 03:01:50 -0500532func (t *topDownMutatorContext) CreateBazelTargetModule(
533 factory ModuleFactory,
Liz Kammerfc46bc12021-02-19 11:06:17 -0500534 name string,
Jingwen Chen1fd14692021-02-05 03:01:50 -0500535 bazelProps bazel.BazelTargetModuleProperties,
536 attrs interface{}) BazelTargetModule {
Liz Kammerfc46bc12021-02-19 11:06:17 -0500537 if strings.HasPrefix(name, bazel.BazelTargetModuleNamePrefix) {
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500538 panic(fmt.Errorf(
Liz Kammerfc46bc12021-02-19 11:06:17 -0500539 "The %s name prefix is added automatically, do not set it manually: %s",
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500540 bazel.BazelTargetModuleNamePrefix,
Liz Kammerfc46bc12021-02-19 11:06:17 -0500541 name))
542 }
543 name = bazel.BazelTargetModuleNamePrefix + name
544 nameProp := struct {
545 Name *string
546 }{
547 Name: &name,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500548 }
549
Liz Kammera060c452021-03-24 10:14:47 -0400550 b := t.createModuleWithoutInheritance(factory, &nameProp, attrs).(BazelTargetModule)
Liz Kammerfc46bc12021-02-19 11:06:17 -0500551 b.SetBazelTargetModuleProperties(bazelProps)
552 return b
Jingwen Chen1fd14692021-02-05 03:01:50 -0500553}
554
Colin Cross25de6c32019-06-06 14:29:25 -0700555func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700556 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700557 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700558 p, nil)
559 if err != nil {
560 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700561 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700562 } else {
563 panic(err)
564 }
565 }
566 }
567}
568
Colin Cross25de6c32019-06-06 14:29:25 -0700569func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700570 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700571 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700572 p, nil)
573 if err != nil {
574 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700575 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700576 } else {
577 panic(err)
578 }
579 }
580 }
581}
Colin Crossdc35e212019-06-06 16:13:11 -0700582
583// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
584// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
585// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
586// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
587// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
588
Colin Crosscb55e082019-07-01 15:32:31 -0700589func (t *topDownMutatorContext) MutatorName() string {
590 return t.bp.MutatorName()
591}
592
Colin Crossdc35e212019-06-06 16:13:11 -0700593func (t *topDownMutatorContext) Rename(name string) {
594 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700595 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700596}
597
Colin Crosse003c4a2019-09-25 12:58:36 -0700598func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Colin Cross18c46802019-09-24 22:19:02 -0700599 inherited := []interface{}{&t.Module().base().commonProperties}
Colin Crosse003c4a2019-09-25 12:58:36 -0700600 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
Colin Cross18c46802019-09-24 22:19:02 -0700601
602 if t.Module().base().variableProperties != nil && module.base().variableProperties != nil {
603 src := t.Module().base().variableProperties
604 dst := []interface{}{
605 module.base().variableProperties,
606 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
607 // don't cause a "failed to find property to extend" error.
Colin Cross43e789d2020-01-28 09:46:50 -0800608 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross18c46802019-09-24 22:19:02 -0700609 }
610 err := proptools.AppendMatchingProperties(dst, src, nil)
611 if err != nil {
612 panic(err)
613 }
614 }
615
Colin Crosse003c4a2019-09-25 12:58:36 -0700616 return module
Colin Crossdc35e212019-06-06 16:13:11 -0700617}
618
Liz Kammera060c452021-03-24 10:14:47 -0400619func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
620 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), props...).(Module)
621 return module
622}
623
Colin Crosscb55e082019-07-01 15:32:31 -0700624func (b *bottomUpMutatorContext) MutatorName() string {
625 return b.bp.MutatorName()
626}
627
Colin Crossdc35e212019-06-06 16:13:11 -0700628func (b *bottomUpMutatorContext) Rename(name string) {
629 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700630 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700631}
632
Colin Cross4f1dcb02020-09-16 18:45:04 -0700633func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
634 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700635}
636
637func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
638 b.bp.AddReverseDependency(module, tag, name)
639}
640
Colin Cross43b92e02019-11-18 15:28:57 -0800641func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000642 if b.finalPhase {
643 panic("CreateVariations not allowed in FinalDepsMutators")
644 }
645
Colin Cross9a362232019-07-01 15:32:45 -0700646 modules := b.bp.CreateVariations(variations...)
647
Colin Cross43b92e02019-11-18 15:28:57 -0800648 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700649 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800650 aModules[i] = modules[i].(Module)
651 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700652 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
653 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
654 }
655
Colin Cross43b92e02019-11-18 15:28:57 -0800656 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700657}
658
Colin Cross43b92e02019-11-18 15:28:57 -0800659func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000660 if b.finalPhase {
661 panic("CreateLocalVariations not allowed in FinalDepsMutators")
662 }
663
Colin Cross9a362232019-07-01 15:32:45 -0700664 modules := b.bp.CreateLocalVariations(variations...)
665
Colin Cross43b92e02019-11-18 15:28:57 -0800666 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700667 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800668 aModules[i] = modules[i].(Module)
669 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700670 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
671 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
672 }
673
Colin Cross43b92e02019-11-18 15:28:57 -0800674 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700675}
676
677func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
678 b.bp.SetDependencyVariation(variation)
679}
680
Jiyong Park1d1119f2019-07-29 21:27:18 +0900681func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
682 b.bp.SetDefaultDependencyVariation(variation)
683}
684
Colin Crossdc35e212019-06-06 16:13:11 -0700685func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700686 names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500687 if b.bazelConversionMode {
688 _, noSelfDeps := RemoveFromList(b.ModuleName(), names)
689 if len(noSelfDeps) == 0 {
690 return []blueprint.Module(nil)
691 }
692 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
693 // dependency, the variations would not exist and the dependency could not be added, by
694 // specifying no variations, we will allow adding the dependency to succeed.
695 return b.bp.AddFarVariationDependencies(nil, tag, noSelfDeps...)
696 }
Colin Crossdc35e212019-06-06 16:13:11 -0700697
Colin Cross4f1dcb02020-09-16 18:45:04 -0700698 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700699}
700
701func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700702 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500703 if b.bazelConversionMode {
704 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
705 // dependency, the variations would not exist and the dependency could not be added, by
706 // specifying no variations, we will allow adding the dependency to succeed.
707 return b.bp.AddFarVariationDependencies(nil, tag, names...)
708 }
Colin Crossdc35e212019-06-06 16:13:11 -0700709
Colin Cross4f1dcb02020-09-16 18:45:04 -0700710 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700711}
712
713func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
714 b.bp.AddInterVariantDependency(tag, from, to)
715}
716
717func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
718 b.bp.ReplaceDependencies(name)
719}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800720
Paul Duffin80342d72020-06-26 22:08:43 +0100721func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
722 b.bp.ReplaceDependenciesIf(name, predicate)
723}
724
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800725func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
726 b.bp.AliasVariation(variationName)
727}
Colin Cross1b9604b2020-08-11 12:03:56 -0700728
729func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
730 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
731}
Colin Crossd27e7b82020-07-02 11:38:17 -0700732
733func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
734 b.bp.SetVariationProvider(module, provider, value)
735}
Liz Kammer356f7d42021-01-26 09:18:53 -0500736
737func (b *bottomUpMutatorContext) BazelConversionMode() bool {
738 return b.bazelConversionMode
739}