blob: 91753d17d8ade42a8dacf112ec937a4020096a3a [file] [log] [blame]
Colin Cross6362e272015-10-29 15:25:03 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross6362e272015-10-29 15:25:03 -070016
Colin Cross795c3772017-03-16 16:50:10 -070017import (
Jingwen Chen1fd14692021-02-05 03:01:50 -050018 "android/soong/bazel"
Colin Cross18c46802019-09-24 22:19:02 -070019 "reflect"
20
Colin Cross795c3772017-03-16 16:50:10 -070021 "github.com/google/blueprint"
Colin Cross519917d2017-11-02 16:35:56 -070022 "github.com/google/blueprint/proptools"
Colin Cross795c3772017-03-16 16:50:10 -070023)
Colin Cross6362e272015-10-29 15:25:03 -070024
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070025// Phases:
26// run Pre-arch mutators
27// run archMutator
28// run Pre-deps mutators
29// run depsMutator
30// run PostDeps mutators
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000031// run FinalDeps mutators (CreateVariations disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070032// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070033
Colin Cross795c3772017-03-16 16:50:10 -070034func registerMutatorsToContext(ctx *blueprint.Context, mutators []*mutator) {
35 for _, t := range mutators {
36 var handle blueprint.MutatorHandle
37 if t.bottomUpMutator != nil {
38 handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator)
39 } else if t.topDownMutator != nil {
40 handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator)
41 }
42 if t.parallel {
43 handle.Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070044 }
45 }
Colin Cross1e676be2016-10-12 14:38:15 -070046}
47
Jingwen Chen73850672020-12-14 08:25:34 -050048// RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
Liz Kammer356f7d42021-01-26 09:18:53 -050049func RegisterMutatorsForBazelConversion(ctx *blueprint.Context, preArchMutators, depsMutators, bp2buildMutators []RegisterMutatorFunc) {
50 mctx := &registerMutatorsContext{
51 bazelConversionMode: true,
Jingwen Chen041b1842021-02-01 00:23:25 -050052 }
53
Liz Kammer356f7d42021-01-26 09:18:53 -050054 bp2buildPreArchMutators = append([]RegisterMutatorFunc{
55 RegisterNamespaceMutator,
56 RegisterDefaultsPreArchMutators,
57 // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should
58 // evaluate the impact on conversion.
59 RegisterPrebuiltsPreArchMutators,
60 },
61 preArchMutators...)
62
63 for _, f := range bp2buildPreArchMutators {
64 f(mctx)
65 }
66
67 bp2buildDepsMutators = append([]RegisterMutatorFunc{
68 registerDepsMutatorBp2Build,
69 registerPathDepsMutator,
70 }, depsMutators...)
71
72 for _, f := range bp2buildDepsMutators {
Jingwen Chen041b1842021-02-01 00:23:25 -050073 f(mctx)
74 }
75
Jingwen Chen73850672020-12-14 08:25:34 -050076 // Register bp2build mutators
77 for _, f := range bp2buildMutators {
78 f(mctx)
79 }
80
81 registerMutatorsToContext(ctx, mctx.mutators)
Jingwen Chen4133ce62020-12-02 04:34:15 -050082}
83
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000084func registerMutators(ctx *blueprint.Context, preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) {
Colin Crosscec81712017-07-13 14:43:27 -070085 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080086
87 register := func(funcs []RegisterMutatorFunc) {
88 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070089 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080090 }
91 }
92
Colin Crosscec81712017-07-13 14:43:27 -070093 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080094
Colin Crosscec81712017-07-13 14:43:27 -070095 register(preDeps)
96
Liz Kammer356f7d42021-01-26 09:18:53 -050097 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070098
99 register(postDeps)
100
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000101 mctx.finalPhase = true
102 register(finalDeps)
103
Colin Crosscec81712017-07-13 14:43:27 -0700104 registerMutatorsToContext(ctx, mctx.mutators)
Colin Cross795c3772017-03-16 16:50:10 -0700105}
106
107type registerMutatorsContext struct {
Liz Kammer356f7d42021-01-26 09:18:53 -0500108 mutators []*mutator
109 finalPhase bool
110 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -0700111}
Colin Cross1e676be2016-10-12 14:38:15 -0700112
113type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -0700114 TopDown(name string, m TopDownMutator) MutatorHandle
115 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -0700116 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Colin Cross1e676be2016-10-12 14:38:15 -0700117}
118
119type RegisterMutatorFunc func(RegisterMutatorsContext)
120
Colin Crosscec81712017-07-13 14:43:27 -0700121var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800122 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100123
Paul Duffinaa4162e2020-05-05 11:35:43 +0100124 // Check the visibility rules are valid.
125 //
126 // This must run after the package renamer mutators so that any issues found during
127 // validation of the package's default_visibility property are reported using the
128 // correct package name and not the synthetic name.
129 //
130 // This must also be run before defaults mutators as the rules for validation are
131 // different before checking the rules than they are afterwards. e.g.
132 // visibility: ["//visibility:private", "//visibility:public"]
133 // would be invalid if specified in a module definition but is valid if it results
134 // from something like this:
135 //
136 // defaults {
137 // name: "defaults",
138 // // Be inaccessible outside a package by default.
139 // visibility: ["//visibility:private"]
140 // }
141 //
142 // defaultable_module {
143 // name: "defaultable_module",
144 // defaults: ["defaults"],
145 // // Override the default.
146 // visibility: ["//visibility:public"]
147 // }
148 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000149 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100150
Bob Badour37af0462021-01-07 03:34:31 +0000151 // Record the default_applicable_licenses for each package.
152 //
153 // This must run before the defaults so that defaults modules can pick up the package default.
154 RegisterLicensesPackageMapper,
155
Paul Duffinaa4162e2020-05-05 11:35:43 +0100156 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100157 //
158 // Any mutators that are added before this will not see any modules created by
159 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700160 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100161
Paul Duffin44f1d842020-06-26 20:17:02 +0100162 // Add dependencies on any components so that any component references can be
163 // resolved within the deps mutator.
164 //
165 // Must be run after defaults so it can be used to create dependencies on the
166 // component modules that are creating in a DefaultableHook.
167 //
168 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
169 // renamed. That is so that if a module creates components using a prebuilt module
170 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
171 // the prebuilt module and not the source module.
172 RegisterComponentsMutator,
173
Paul Duffinc988c8e2020-04-29 18:27:14 +0100174 // Create an association between prebuilt modules and their corresponding source
175 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100176 //
177 // Must be run after defaults mutators to ensure that any modules created by
178 // a DefaultableHook can be either a prebuilt or a source module with a matching
179 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100180 RegisterPrebuiltsPreArchMutators,
181
Bob Badour37af0462021-01-07 03:34:31 +0000182 // Gather the licenses properties for all modules for use during expansion and enforcement.
183 //
184 // This must come after the defaults mutators to ensure that any licenses supplied
185 // in a defaults module has been successfully applied before the rules are gathered.
186 RegisterLicensesPropertyGatherer,
187
Paul Duffinaa4162e2020-05-05 11:35:43 +0100188 // Gather the visibility rules for all modules for us during visibility enforcement.
189 //
190 // This must come after the defaults mutators to ensure that any visibility supplied
191 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000192 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700193}
194
Colin Crossae4c6182017-09-15 17:33:55 -0700195func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700196 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800197 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700198 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700199}
200
Colin Crosscec81712017-07-13 14:43:27 -0700201var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700202 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700203}
204
205var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800206 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700207 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000208 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000209 RegisterLicensesDependencyChecker,
Artur Satayevc5570ac2020-04-09 16:06:36 +0100210 RegisterNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700211 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700212}
Colin Cross1e676be2016-10-12 14:38:15 -0700213
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000214var finalDeps = []RegisterMutatorFunc{}
215
Colin Cross1e676be2016-10-12 14:38:15 -0700216func PreArchMutators(f RegisterMutatorFunc) {
217 preArch = append(preArch, f)
218}
219
220func PreDepsMutators(f RegisterMutatorFunc) {
221 preDeps = append(preDeps, f)
222}
223
224func PostDepsMutators(f RegisterMutatorFunc) {
225 postDeps = append(postDeps, f)
226}
227
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000228func FinalDepsMutators(f RegisterMutatorFunc) {
229 finalDeps = append(finalDeps, f)
230}
231
Liz Kammer356f7d42021-01-26 09:18:53 -0500232var bp2buildPreArchMutators = []RegisterMutatorFunc{}
233var bp2buildDepsMutators = []RegisterMutatorFunc{}
Jingwen Chen73850672020-12-14 08:25:34 -0500234var bp2buildMutators = []RegisterMutatorFunc{}
235
236// 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 }
245 bp2buildMutators = append(bp2buildMutators, f)
246}
247
Liz Kammer356f7d42021-01-26 09:18:53 -0500248// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
249// into Bazel BUILD targets that should run prior to deps and conversion.
250func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
251 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
252}
253
254// DepsBp2BuildMutators adds mutators to be register for converting Android Blueprint modules into
255// Bazel BUILD targets that should run prior to conversion to resolve dependencies.
256func DepsBp2BuildMutators(f RegisterMutatorFunc) {
257 bp2buildDepsMutators = append(bp2buildDepsMutators, f)
258}
259
Colin Cross9f35c3d2020-09-16 19:04:41 -0700260type BaseMutatorContext interface {
261 BaseModuleContext
262
263 // MutatorName returns the name that this mutator was registered with.
264 MutatorName() string
265
266 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
267 // AddDependency or OtherModuleName until after this mutator pass is complete.
268 Rename(name string)
269}
270
Colin Cross25de6c32019-06-06 14:29:25 -0700271type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700272
Colin Cross635c3b02016-05-18 15:37:25 -0700273type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700274 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700275
Colin Cross9f35c3d2020-09-16 19:04:41 -0700276 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
277 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700278 CreateModule(ModuleFactory, ...interface{}) Module
Jingwen Chen1fd14692021-02-05 03:01:50 -0500279
280 // CreateBazelTargetModule creates a BazelTargetModule by calling the
281 // factory method, just like in CreateModule, but also requires
282 // BazelTargetModuleProperties containing additional metadata for the
283 // bp2build codegenerator.
284 CreateBazelTargetModule(ModuleFactory, bazel.BazelTargetModuleProperties, interface{}) BazelTargetModule
Colin Cross6362e272015-10-29 15:25:03 -0700285}
286
Colin Cross25de6c32019-06-06 14:29:25 -0700287type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700288 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700289 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700290}
291
Colin Cross25de6c32019-06-06 14:29:25 -0700292type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700293
Colin Cross635c3b02016-05-18 15:37:25 -0700294type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700295 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800296
Colin Cross4f1dcb02020-09-16 18:45:04 -0700297 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
298 // dependency (some entries may be nil).
299 //
300 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
301 // new dependencies have had the current mutator called on them. If the mutator is not
302 // parallel this method does not affect the ordering of the current mutator pass, but will
303 // be ordered correctly for all future mutator passes.
304 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700305
306 // AddReverseDependency adds a dependency from the destination to the given module.
307 // Does not affect the ordering of the current mutator pass, but will be ordered
308 // correctly for all future mutator passes. All reverse dependencies for a destination module are
309 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
310 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800311 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700312
313 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
314 // parameter. It returns a list of new modules in the same order as the variationNames
315 // list.
316 //
317 // If any of the dependencies of the module being operated on were already split
318 // by calling CreateVariations with the same name, the dependency will automatically
319 // be updated to point the matching variant.
320 //
321 // If a module is split, and then a module depending on the first module is not split
322 // when the Mutator is later called on it, the dependency of the depending module will
323 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800324 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700325
326 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
327 // parameter. It returns a list of new modules in the same order as the variantNames
328 // list.
329 //
330 // Local variations do not affect automatic dependency resolution - dependencies added
331 // to the split module via deps or DynamicDependerModule must exactly match a variant
332 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800333 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700334
335 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
336 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800337 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700338
339 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
340 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900341 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700342
343 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700344 // argument to select which variant of the dependency to use. It returns a slice of modules for
345 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
346 // the all of the non-local variations of the current module, plus the variations argument.
347 //
348 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
349 // new dependencies have had the current mutator called on them. If the mutator is not
350 // parallel this method does not affect the ordering of the current mutator pass, but will
351 // be ordered correctly for all future mutator passes.
352 AddVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700353
354 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700355 // variations argument to select which variant of the dependency to use. It returns a slice of
356 // modules for each dependency (some entries may be nil). A variant of the dependency must
357 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700358 // For any unspecified variation the first variant will be used.
359 //
360 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
361 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700362 //
363 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
364 // new dependencies have had the current mutator called on them. If the mutator is not
365 // parallel this method does not affect the ordering of the current mutator pass, but will
366 // be ordered correctly for all future mutator passes.
367 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700368
369 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
370 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
371 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
372 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800373 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
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. Replacements don't take effect until
377 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800378 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700379
380 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
381 // specified name with the current variant of this module as long as the supplied predicate returns
382 // true.
383 //
384 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100385 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700386
387 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
388 // and creates an alias from the current variant (before the mutator has run) to the new
389 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
390 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
391 // be used to add dependencies on the newly created variant using the variant map from
392 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800393 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700394
395 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
396 // module, and creates an alias from a new fromVariationName variant the toVariationName
397 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
398 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
399 // be used to add dependencies on the toVariationName variant using the fromVariationName
400 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700401 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700402
403 // SetVariationProvider sets the value for a provider for the given newly created variant of
404 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
405 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
406 // if the value is not of the appropriate type, or if the module is not a newly created
407 // variant of the current module. The value should not be modified after being passed to
408 // SetVariationProvider.
409 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Liz Kammer356f7d42021-01-26 09:18:53 -0500410
411 // BazelConversionMode returns whether this mutator is being run as part of Bazel Conversion.
412 BazelConversionMode() bool
Colin Cross6362e272015-10-29 15:25:03 -0700413}
414
Colin Cross25de6c32019-06-06 14:29:25 -0700415type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700416 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700417 baseModuleContext
Liz Kammer356f7d42021-01-26 09:18:53 -0500418 finalPhase bool
419 bazelConversionMode bool
Colin Cross6362e272015-10-29 15:25:03 -0700420}
421
Colin Cross617b88a2020-08-24 18:04:09 -0700422func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500423 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700424
425 return &bottomUpMutatorContext{
Liz Kammer356f7d42021-01-26 09:18:53 -0500426 bp: ctx,
427 baseModuleContext: a.base().baseModuleContextFactory(ctx),
428 finalPhase: finalPhase,
429 bazelConversionMode: bazelConversionMode,
Colin Cross617b88a2020-08-24 18:04:09 -0700430 }
431}
432
Colin Cross25de6c32019-06-06 14:29:25 -0700433func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000434 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500435 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700436 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700437 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500438 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700439 }
Colin Cross798bfce2016-10-12 14:28:16 -0700440 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500441 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700442 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700443 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700444}
445
Colin Cross617b88a2020-08-24 18:04:09 -0700446func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
447 mutator := &mutator{name: name, bottomUpMutator: m}
448 x.mutators = append(x.mutators, mutator)
449 return mutator
450}
451
Liz Kammer356f7d42021-01-26 09:18:53 -0500452func (x *registerMutatorsContext) mutatorName(name string) string {
453 if x.bazelConversionMode {
454 return name + "_bp2build"
455 }
456 return name
457}
458
Colin Cross25de6c32019-06-06 14:29:25 -0700459func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700460 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700461 if a, ok := ctx.Module().(Module); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700462 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700463 bp: ctx,
464 baseModuleContext: a.base().baseModuleContextFactory(ctx),
Colin Cross6362e272015-10-29 15:25:03 -0700465 }
Colin Cross798bfce2016-10-12 14:28:16 -0700466 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700467 }
Colin Cross798bfce2016-10-12 14:28:16 -0700468 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500469 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700470 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700471 return mutator
472}
473
474type MutatorHandle interface {
475 Parallel() MutatorHandle
476}
477
478func (mutator *mutator) Parallel() MutatorHandle {
479 mutator.parallel = true
480 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700481}
Colin Cross1e676be2016-10-12 14:38:15 -0700482
Paul Duffin44f1d842020-06-26 20:17:02 +0100483func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
484 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
485}
486
487// A special mutator that runs just prior to the deps mutator to allow the dependencies
488// on component modules to be added so that they can depend directly on a prebuilt
489// module.
490func componentDepsMutator(ctx BottomUpMutatorContext) {
491 if m := ctx.Module(); m.Enabled() {
492 m.ComponentDepsMutator(ctx)
493 }
494}
495
Colin Cross1e676be2016-10-12 14:38:15 -0700496func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100497 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700498 m.DepsMutator(ctx)
499 }
500}
Colin Crossd11fcda2017-10-23 17:59:01 -0700501
Liz Kammer356f7d42021-01-26 09:18:53 -0500502func registerDepsMutator(ctx RegisterMutatorsContext) {
503 ctx.BottomUp("deps", depsMutator).Parallel()
504}
505
506func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
507 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
508 // being converted to build targets.
509 ctx.BottomUp("deps", depsMutator).Parallel()
510}
511
Jingwen Chen1fd14692021-02-05 03:01:50 -0500512func (t *topDownMutatorContext) CreateBazelTargetModule(
513 factory ModuleFactory,
514 bazelProps bazel.BazelTargetModuleProperties,
515 attrs interface{}) BazelTargetModule {
516 return t.CreateModule(factory, &bazelProps, attrs).(BazelTargetModule)
517}
518
Colin Cross25de6c32019-06-06 14:29:25 -0700519func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700520 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700521 err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700522 p, nil)
523 if err != nil {
524 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700525 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700526 } else {
527 panic(err)
528 }
529 }
530 }
531}
532
Colin Cross25de6c32019-06-06 14:29:25 -0700533func (t *topDownMutatorContext) PrependProperties(props ...interface{}) {
Colin Cross519917d2017-11-02 16:35:56 -0700534 for _, p := range props {
Colin Cross25de6c32019-06-06 14:29:25 -0700535 err := proptools.PrependMatchingProperties(t.Module().base().customizableProperties,
Colin Cross519917d2017-11-02 16:35:56 -0700536 p, nil)
537 if err != nil {
538 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
Colin Cross25de6c32019-06-06 14:29:25 -0700539 t.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
Colin Cross519917d2017-11-02 16:35:56 -0700540 } else {
541 panic(err)
542 }
543 }
544 }
545}
Colin Crossdc35e212019-06-06 16:13:11 -0700546
547// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
548// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
549// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
550// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
551// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
552
Colin Crosscb55e082019-07-01 15:32:31 -0700553func (t *topDownMutatorContext) MutatorName() string {
554 return t.bp.MutatorName()
555}
556
Colin Crossdc35e212019-06-06 16:13:11 -0700557func (t *topDownMutatorContext) Rename(name string) {
558 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700559 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700560}
561
Colin Crosse003c4a2019-09-25 12:58:36 -0700562func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Colin Cross18c46802019-09-24 22:19:02 -0700563 inherited := []interface{}{&t.Module().base().commonProperties}
Colin Crosse003c4a2019-09-25 12:58:36 -0700564 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
Colin Cross18c46802019-09-24 22:19:02 -0700565
566 if t.Module().base().variableProperties != nil && module.base().variableProperties != nil {
567 src := t.Module().base().variableProperties
568 dst := []interface{}{
569 module.base().variableProperties,
570 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
571 // don't cause a "failed to find property to extend" error.
Colin Cross43e789d2020-01-28 09:46:50 -0800572 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross18c46802019-09-24 22:19:02 -0700573 }
574 err := proptools.AppendMatchingProperties(dst, src, nil)
575 if err != nil {
576 panic(err)
577 }
578 }
579
Colin Crosse003c4a2019-09-25 12:58:36 -0700580 return module
Colin Crossdc35e212019-06-06 16:13:11 -0700581}
582
Colin Crosscb55e082019-07-01 15:32:31 -0700583func (b *bottomUpMutatorContext) MutatorName() string {
584 return b.bp.MutatorName()
585}
586
Colin Crossdc35e212019-06-06 16:13:11 -0700587func (b *bottomUpMutatorContext) Rename(name string) {
588 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700589 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700590}
591
Colin Cross4f1dcb02020-09-16 18:45:04 -0700592func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
593 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700594}
595
596func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
597 b.bp.AddReverseDependency(module, tag, name)
598}
599
Colin Cross43b92e02019-11-18 15:28:57 -0800600func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000601 if b.finalPhase {
602 panic("CreateVariations not allowed in FinalDepsMutators")
603 }
604
Colin Cross9a362232019-07-01 15:32:45 -0700605 modules := b.bp.CreateVariations(variations...)
606
Colin Cross43b92e02019-11-18 15:28:57 -0800607 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700608 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800609 aModules[i] = modules[i].(Module)
610 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700611 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
612 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
613 }
614
Colin Cross43b92e02019-11-18 15:28:57 -0800615 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700616}
617
Colin Cross43b92e02019-11-18 15:28:57 -0800618func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000619 if b.finalPhase {
620 panic("CreateLocalVariations not allowed in FinalDepsMutators")
621 }
622
Colin Cross9a362232019-07-01 15:32:45 -0700623 modules := b.bp.CreateLocalVariations(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
636func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
637 b.bp.SetDependencyVariation(variation)
638}
639
Jiyong Park1d1119f2019-07-29 21:27:18 +0900640func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
641 b.bp.SetDefaultDependencyVariation(variation)
642}
643
Colin Crossdc35e212019-06-06 16:13:11 -0700644func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700645 names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500646 if b.bazelConversionMode {
647 _, noSelfDeps := RemoveFromList(b.ModuleName(), names)
648 if len(noSelfDeps) == 0 {
649 return []blueprint.Module(nil)
650 }
651 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
652 // dependency, the variations would not exist and the dependency could not be added, by
653 // specifying no variations, we will allow adding the dependency to succeed.
654 return b.bp.AddFarVariationDependencies(nil, tag, noSelfDeps...)
655 }
Colin Crossdc35e212019-06-06 16:13:11 -0700656
Colin Cross4f1dcb02020-09-16 18:45:04 -0700657 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700658}
659
660func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700661 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammer356f7d42021-01-26 09:18:53 -0500662 if b.bazelConversionMode {
663 // In Bazel conversion mode, mutators should not have created any variants. So, when adding a
664 // dependency, the variations would not exist and the dependency could not be added, by
665 // specifying no variations, we will allow adding the dependency to succeed.
666 return b.bp.AddFarVariationDependencies(nil, tag, names...)
667 }
Colin Crossdc35e212019-06-06 16:13:11 -0700668
Colin Cross4f1dcb02020-09-16 18:45:04 -0700669 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700670}
671
672func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
673 b.bp.AddInterVariantDependency(tag, from, to)
674}
675
676func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
677 b.bp.ReplaceDependencies(name)
678}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800679
Paul Duffin80342d72020-06-26 22:08:43 +0100680func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
681 b.bp.ReplaceDependenciesIf(name, predicate)
682}
683
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800684func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
685 b.bp.AliasVariation(variationName)
686}
Colin Cross1b9604b2020-08-11 12:03:56 -0700687
688func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
689 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
690}
Colin Crossd27e7b82020-07-02 11:38:17 -0700691
692func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
693 b.bp.SetVariationProvider(module, provider, value)
694}
Liz Kammer356f7d42021-01-26 09:18:53 -0500695
696func (b *bottomUpMutatorContext) BazelConversionMode() bool {
697 return b.bazelConversionMode
698}