blob: 7f93bafbba24bc41d407d3739158c8145a225ef7 [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 (
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000018 "android/soong/bazel"
19
Colin Cross795c3772017-03-16 16:50:10 -070020 "github.com/google/blueprint"
21)
Colin Cross6362e272015-10-29 15:25:03 -070022
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070023// Phases:
24// run Pre-arch mutators
25// run archMutator
26// run Pre-deps mutators
27// run depsMutator
28// run PostDeps mutators
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000029// run FinalDeps mutators (CreateVariations disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070030// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070031
Jingwen Chen73850672020-12-14 08:25:34 -050032// RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
Liz Kammerbe46fcc2021-11-01 15:32:43 -040033func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) {
Liz Kammer356f7d42021-01-26 09:18:53 -050034 mctx := &registerMutatorsContext{
35 bazelConversionMode: true,
Jingwen Chen041b1842021-02-01 00:23:25 -050036 }
37
Liz Kammerbe46fcc2021-11-01 15:32:43 -040038 bp2buildMutators := append([]RegisterMutatorFunc{
Liz Kammer356f7d42021-01-26 09:18:53 -050039 RegisterNamespaceMutator,
40 RegisterDefaultsPreArchMutators,
41 // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should
42 // evaluate the impact on conversion.
43 RegisterPrebuiltsPreArchMutators,
44 },
45 preArchMutators...)
Liz Kammerbe46fcc2021-11-01 15:32:43 -040046 bp2buildMutators = append(bp2buildMutators, registerBp2buildConversionMutator)
Liz Kammer356f7d42021-01-26 09:18:53 -050047
Jingwen Chen73850672020-12-14 08:25:34 -050048 // Register bp2build mutators
49 for _, f := range bp2buildMutators {
50 f(mctx)
51 }
52
Paul Duffin1d2d42f2021-03-06 20:08:12 +000053 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050054}
55
Paul Duffinc05b0342021-03-06 13:28:13 +000056// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
57// with the InitRegistrationContext and will be used at runtime.
58func collateGloballyRegisteredMutators() sortableComponents {
59 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
60}
61
62// collateRegisteredMutators constructs a single list of mutators from the separate lists.
63func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070064 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080065
66 register := func(funcs []RegisterMutatorFunc) {
67 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070068 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080069 }
70 }
71
Colin Crosscec81712017-07-13 14:43:27 -070072 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080073
Colin Crosscec81712017-07-13 14:43:27 -070074 register(preDeps)
75
Liz Kammer356f7d42021-01-26 09:18:53 -050076 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070077
78 register(postDeps)
79
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000080 mctx.finalPhase = true
81 register(finalDeps)
82
Paul Duffinc05b0342021-03-06 13:28:13 +000083 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -070084}
85
86type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +000087 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -050088 finalPhase bool
89 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -070090}
Colin Cross1e676be2016-10-12 14:38:15 -070091
92type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -070093 TopDown(name string, m TopDownMutator) MutatorHandle
94 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -070095 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Lukacs T. Berki6c716762022-06-13 20:50:39 +020096 Transition(name string, m TransitionMutator)
Colin Cross1e676be2016-10-12 14:38:15 -070097}
98
99type RegisterMutatorFunc func(RegisterMutatorsContext)
100
Colin Crosscec81712017-07-13 14:43:27 -0700101var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800102 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100103
Paul Duffinaa4162e2020-05-05 11:35:43 +0100104 // Check the visibility rules are valid.
105 //
106 // This must run after the package renamer mutators so that any issues found during
107 // validation of the package's default_visibility property are reported using the
108 // correct package name and not the synthetic name.
109 //
110 // This must also be run before defaults mutators as the rules for validation are
111 // different before checking the rules than they are afterwards. e.g.
112 // visibility: ["//visibility:private", "//visibility:public"]
113 // would be invalid if specified in a module definition but is valid if it results
114 // from something like this:
115 //
116 // defaults {
117 // name: "defaults",
118 // // Be inaccessible outside a package by default.
119 // visibility: ["//visibility:private"]
120 // }
121 //
122 // defaultable_module {
123 // name: "defaultable_module",
124 // defaults: ["defaults"],
125 // // Override the default.
126 // visibility: ["//visibility:public"]
127 // }
128 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000129 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100130
Bob Badour37af0462021-01-07 03:34:31 +0000131 // Record the default_applicable_licenses for each package.
132 //
133 // This must run before the defaults so that defaults modules can pick up the package default.
134 RegisterLicensesPackageMapper,
135
Paul Duffinaa4162e2020-05-05 11:35:43 +0100136 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100137 //
138 // Any mutators that are added before this will not see any modules created by
139 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700140 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100141
Paul Duffin44f1d842020-06-26 20:17:02 +0100142 // Add dependencies on any components so that any component references can be
143 // resolved within the deps mutator.
144 //
145 // Must be run after defaults so it can be used to create dependencies on the
146 // component modules that are creating in a DefaultableHook.
147 //
148 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
149 // renamed. That is so that if a module creates components using a prebuilt module
150 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
151 // the prebuilt module and not the source module.
152 RegisterComponentsMutator,
153
Paul Duffinc988c8e2020-04-29 18:27:14 +0100154 // Create an association between prebuilt modules and their corresponding source
155 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100156 //
157 // Must be run after defaults mutators to ensure that any modules created by
158 // a DefaultableHook can be either a prebuilt or a source module with a matching
159 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100160 RegisterPrebuiltsPreArchMutators,
161
Bob Badour37af0462021-01-07 03:34:31 +0000162 // Gather the licenses properties for all modules for use during expansion and enforcement.
163 //
164 // This must come after the defaults mutators to ensure that any licenses supplied
165 // in a defaults module has been successfully applied before the rules are gathered.
166 RegisterLicensesPropertyGatherer,
167
Paul Duffinaa4162e2020-05-05 11:35:43 +0100168 // Gather the visibility rules for all modules for us during visibility enforcement.
169 //
170 // This must come after the defaults mutators to ensure that any visibility supplied
171 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000172 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700173}
174
Colin Crossae4c6182017-09-15 17:33:55 -0700175func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700176 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800177 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700178 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700179}
180
Colin Crosscec81712017-07-13 14:43:27 -0700181var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700182 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700183}
184
185var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800186 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700187 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000188 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000189 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100190 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700191 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700192}
Colin Cross1e676be2016-10-12 14:38:15 -0700193
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000194var finalDeps = []RegisterMutatorFunc{}
195
Colin Cross1e676be2016-10-12 14:38:15 -0700196func PreArchMutators(f RegisterMutatorFunc) {
197 preArch = append(preArch, f)
198}
199
200func PreDepsMutators(f RegisterMutatorFunc) {
201 preDeps = append(preDeps, f)
202}
203
204func PostDepsMutators(f RegisterMutatorFunc) {
205 postDeps = append(postDeps, f)
206}
207
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000208func FinalDepsMutators(f RegisterMutatorFunc) {
209 finalDeps = append(finalDeps, f)
210}
211
Liz Kammer356f7d42021-01-26 09:18:53 -0500212var bp2buildPreArchMutators = []RegisterMutatorFunc{}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400213
Liz Kammer12615db2021-09-28 09:19:17 -0400214// A minimal context for Bp2build conversion
215type Bp2buildMutatorContext interface {
216 BazelConversionPathContext
217
218 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
219}
220
Liz Kammer356f7d42021-01-26 09:18:53 -0500221// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
222// into Bazel BUILD targets that should run prior to deps and conversion.
223func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
224 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
225}
226
Colin Cross9f35c3d2020-09-16 19:04:41 -0700227type BaseMutatorContext interface {
228 BaseModuleContext
229
230 // MutatorName returns the name that this mutator was registered with.
231 MutatorName() string
232
233 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
234 // AddDependency or OtherModuleName until after this mutator pass is complete.
235 Rename(name string)
236}
237
Colin Cross25de6c32019-06-06 14:29:25 -0700238type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700239
Colin Cross635c3b02016-05-18 15:37:25 -0700240type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700241 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700242
Colin Cross9f35c3d2020-09-16 19:04:41 -0700243 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
244 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700245 CreateModule(ModuleFactory, ...interface{}) Module
Jingwen Chen1fd14692021-02-05 03:01:50 -0500246
247 // CreateBazelTargetModule creates a BazelTargetModule by calling the
248 // factory method, just like in CreateModule, but also requires
249 // BazelTargetModuleProperties containing additional metadata for the
250 // bp2build codegenerator.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000251 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
Chris Parsons58852a02021-12-09 18:10:18 -0500252
253 // CreateBazelTargetModuleWithRestrictions creates a BazelTargetModule by calling the
254 // factory method, just like in CreateModule, but also requires
255 // BazelTargetModuleProperties containing additional metadata for the
256 // bp2build codegenerator. The generated target is restricted to only be buildable for certain
257 // platforms, as dictated by a given bool attribute: the target will not be buildable in
258 // any platform for which this bool attribute is false.
259 CreateBazelTargetModuleWithRestrictions(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}, bazel.BoolAttribute)
Colin Cross6362e272015-10-29 15:25:03 -0700260}
261
Colin Cross25de6c32019-06-06 14:29:25 -0700262type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700263 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700264 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700265}
266
Colin Cross25de6c32019-06-06 14:29:25 -0700267type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700268
Colin Cross635c3b02016-05-18 15:37:25 -0700269type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700270 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800271
Colin Cross4f1dcb02020-09-16 18:45:04 -0700272 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
273 // dependency (some entries may be nil).
274 //
275 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
276 // new dependencies have had the current mutator called on them. If the mutator is not
277 // parallel this method does not affect the ordering of the current mutator pass, but will
278 // be ordered correctly for all future mutator passes.
279 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700280
281 // AddReverseDependency adds a dependency from the destination to the given module.
282 // Does not affect the ordering of the current mutator pass, but will be ordered
283 // correctly for all future mutator passes. All reverse dependencies for a destination module are
284 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
285 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800286 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700287
288 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
289 // parameter. It returns a list of new modules in the same order as the variationNames
290 // list.
291 //
292 // If any of the dependencies of the module being operated on were already split
293 // by calling CreateVariations with the same name, the dependency will automatically
294 // be updated to point the matching variant.
295 //
296 // If a module is split, and then a module depending on the first module is not split
297 // when the Mutator is later called on it, the dependency of the depending module will
298 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800299 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700300
301 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
302 // parameter. It returns a list of new modules in the same order as the variantNames
303 // list.
304 //
305 // Local variations do not affect automatic dependency resolution - dependencies added
306 // to the split module via deps or DynamicDependerModule must exactly match a variant
307 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800308 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700309
310 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
311 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800312 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700313
314 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
315 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900316 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700317
318 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700319 // argument to select which variant of the dependency to use. It returns a slice of modules for
320 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
Usta Shresthac725f472022-01-11 02:44:21 -0500321 // all the non-local variations of the current module, plus the variations argument.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700322 //
323 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
324 // new dependencies have had the current mutator called on them. If the mutator is not
325 // parallel this method does not affect the ordering of the current mutator pass, but will
326 // be ordered correctly for all future mutator passes.
Usta Shresthac725f472022-01-11 02:44:21 -0500327 AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700328
329 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700330 // variations argument to select which variant of the dependency to use. It returns a slice of
331 // modules for each dependency (some entries may be nil). A variant of the dependency must
332 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700333 // For any unspecified variation the first variant will be used.
334 //
335 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
336 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700337 //
338 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
339 // new dependencies have had the current mutator called on them. If the mutator is not
340 // parallel this method does not affect the ordering of the current mutator pass, but will
341 // be ordered correctly for all future mutator passes.
342 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700343
344 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
345 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
346 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
347 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800348 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700349
350 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
351 // specified name with the current variant of this module. Replacements don't take effect until
352 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800353 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700354
355 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
356 // specified name with the current variant of this module as long as the supplied predicate returns
357 // true.
358 //
359 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100360 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700361
362 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
363 // and creates an alias from the current variant (before the mutator has run) to the new
364 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
365 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
366 // be used to add dependencies on the newly created variant using the variant map from
367 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800368 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700369
370 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
371 // module, and creates an alias from a new fromVariationName variant the toVariationName
372 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
373 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
374 // be used to add dependencies on the toVariationName variant using the fromVariationName
375 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700376 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700377
378 // SetVariationProvider sets the value for a provider for the given newly created variant of
379 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
380 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
381 // if the value is not of the appropriate type, or if the module is not a newly created
382 // variant of the current module. The value should not be modified after being passed to
383 // SetVariationProvider.
384 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700385}
386
Colin Cross25de6c32019-06-06 14:29:25 -0700387type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700388 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700389 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400390 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700391}
392
Colin Cross617b88a2020-08-24 18:04:09 -0700393func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500394 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700395
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400396 moduleContext := a.base().baseModuleContextFactory(ctx)
397 moduleContext.bazelConversionMode = bazelConversionMode
398
Colin Cross617b88a2020-08-24 18:04:09 -0700399 return &bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400400 bp: ctx,
401 baseModuleContext: a.base().baseModuleContextFactory(ctx),
402 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700403 }
404}
405
Colin Cross25de6c32019-06-06 14:29:25 -0700406func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000407 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500408 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700409 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700410 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500411 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700412 }
Colin Cross798bfce2016-10-12 14:28:16 -0700413 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500414 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700415 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700416 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700417}
418
Colin Cross617b88a2020-08-24 18:04:09 -0700419func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
420 mutator := &mutator{name: name, bottomUpMutator: m}
421 x.mutators = append(x.mutators, mutator)
422 return mutator
423}
424
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200425type IncomingTransitionContext interface {
426 // Module returns the target of the dependency edge for which the transition
427 // is being computed
428 Module() Module
429
430 // Config returns the configuration for the build.
431 Config() Config
432}
433
434type OutgoingTransitionContext interface {
435 // Module returns the target of the dependency edge for which the transition
436 // is being computed
437 Module() Module
438
439 // DepTag() Returns the dependency tag through which this dependency is
440 // reached
441 DepTag() blueprint.DependencyTag
442}
443type TransitionMutator interface {
444 // Split returns the set of variations that should be created for a module no
445 // matter who depends on it. Used when Make depends on a particular variation
446 // or when the module knows its variations just based on information given to
447 // it in the Blueprint file. This method should not mutate the module it is
448 // called on.
449 Split(ctx BaseModuleContext) []string
450
451 // OutCalled on a module to determine which variation it wants from its direct
452 // dependencies. The dependency itself can override this decision. This method
453 // should not mutate the module itself.
454 OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string
455
456 // Called on a module to determine which variation it should be in based on
457 // the variation modules that depend on it want. This gives the module a final
458 // say about its own variations. This method should not mutate the module
459 // itself.
460 IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string
461
462 // Called after a module was split into multiple variations on each variation.
463 // It should not split the module any further but adding new dependencies is
464 // fine. Unlike all the other methods on TransitionMutator, this method is
465 // allowed to mutate the module.
466 Mutate(ctx BottomUpMutatorContext, variation string)
467}
468
469type androidTransitionMutator struct {
470 finalPhase bool
471 bazelConversionMode bool
472 mutator TransitionMutator
473}
474
475func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []string {
476 if m, ok := ctx.Module().(Module); ok {
477 moduleContext := m.base().baseModuleContextFactory(ctx)
478 moduleContext.bazelConversionMode = a.bazelConversionMode
479 return a.mutator.Split(&moduleContext)
480 } else {
481 return []string{""}
482 }
483}
484
485type outgoingTransitionContextImpl struct {
486 bp blueprint.OutgoingTransitionContext
487}
488
489func (c *outgoingTransitionContextImpl) Module() Module {
490 return c.bp.Module().(Module)
491}
492
493func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag {
494 return c.bp.DepTag()
495}
496
497func (a *androidTransitionMutator) OutgoingTransition(ctx blueprint.OutgoingTransitionContext, sourceVariation string) string {
498 if _, ok := ctx.Module().(Module); ok {
499 return a.mutator.OutgoingTransition(&outgoingTransitionContextImpl{bp: ctx}, sourceVariation)
500 } else {
501 return ""
502 }
503}
504
505type incomingTransitionContextImpl struct {
506 bp blueprint.IncomingTransitionContext
507}
508
509func (c *incomingTransitionContextImpl) Module() Module {
510 return c.bp.Module().(Module)
511}
512
513func (c *incomingTransitionContextImpl) Config() Config {
514 return c.bp.Config().(Config)
515}
516
517func (a *androidTransitionMutator) IncomingTransition(ctx blueprint.IncomingTransitionContext, incomingVariation string) string {
518 if _, ok := ctx.Module().(Module); ok {
519 return a.mutator.IncomingTransition(&incomingTransitionContextImpl{bp: ctx}, incomingVariation)
520 } else {
521 return ""
522 }
523}
524
525func (a *androidTransitionMutator) Mutate(ctx blueprint.BottomUpMutatorContext, variation string) {
526 if am, ok := ctx.Module().(Module); ok {
527 a.mutator.Mutate(bottomUpMutatorContextFactory(ctx, am, a.finalPhase, a.bazelConversionMode), variation)
528 }
529}
530
531func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) {
532 atm := &androidTransitionMutator{
533 finalPhase: x.finalPhase,
534 bazelConversionMode: x.bazelConversionMode,
535 mutator: m,
536 }
537 mutator := &mutator{
538 name: name,
539 transitionMutator: atm}
540 x.mutators = append(x.mutators, mutator)
541}
542
Liz Kammer356f7d42021-01-26 09:18:53 -0500543func (x *registerMutatorsContext) mutatorName(name string) string {
544 if x.bazelConversionMode {
545 return name + "_bp2build"
546 }
547 return name
548}
549
Colin Cross25de6c32019-06-06 14:29:25 -0700550func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700551 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700552 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400553 moduleContext := a.base().baseModuleContextFactory(ctx)
554 moduleContext.bazelConversionMode = x.bazelConversionMode
Colin Cross25de6c32019-06-06 14:29:25 -0700555 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700556 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400557 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700558 }
Colin Cross798bfce2016-10-12 14:28:16 -0700559 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700560 }
Colin Cross798bfce2016-10-12 14:28:16 -0700561 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500562 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700563 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700564 return mutator
565}
566
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000567func (mutator *mutator) componentName() string {
568 return mutator.name
569}
570
571func (mutator *mutator) register(ctx *Context) {
572 blueprintCtx := ctx.Context
573 var handle blueprint.MutatorHandle
574 if mutator.bottomUpMutator != nil {
575 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
576 } else if mutator.topDownMutator != nil {
577 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200578 } else if mutator.transitionMutator != nil {
579 blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000580 }
581 if mutator.parallel {
582 handle.Parallel()
583 }
584}
585
Colin Cross798bfce2016-10-12 14:28:16 -0700586type MutatorHandle interface {
587 Parallel() MutatorHandle
588}
589
590func (mutator *mutator) Parallel() MutatorHandle {
591 mutator.parallel = true
592 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700593}
Colin Cross1e676be2016-10-12 14:38:15 -0700594
Paul Duffin44f1d842020-06-26 20:17:02 +0100595func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
596 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
597}
598
599// A special mutator that runs just prior to the deps mutator to allow the dependencies
600// on component modules to be added so that they can depend directly on a prebuilt
601// module.
602func componentDepsMutator(ctx BottomUpMutatorContext) {
603 if m := ctx.Module(); m.Enabled() {
604 m.ComponentDepsMutator(ctx)
605 }
606}
607
Colin Cross1e676be2016-10-12 14:38:15 -0700608func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100609 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700610 m.DepsMutator(ctx)
611 }
612}
Colin Crossd11fcda2017-10-23 17:59:01 -0700613
Liz Kammer356f7d42021-01-26 09:18:53 -0500614func registerDepsMutator(ctx RegisterMutatorsContext) {
615 ctx.BottomUp("deps", depsMutator).Parallel()
616}
617
618func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
619 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
620 // being converted to build targets.
621 ctx.BottomUp("deps", depsMutator).Parallel()
622}
623
Jingwen Chen1fd14692021-02-05 03:01:50 -0500624func (t *topDownMutatorContext) CreateBazelTargetModule(
Jingwen Chen1fd14692021-02-05 03:01:50 -0500625 bazelProps bazel.BazelTargetModuleProperties,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000626 commonAttrs CommonAttributes,
Liz Kammer2ada09a2021-08-11 00:17:36 -0400627 attrs interface{}) {
Chris Parsons58852a02021-12-09 18:10:18 -0500628 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, bazel.BoolAttribute{})
629}
630
631func (t *topDownMutatorContext) CreateBazelTargetModuleWithRestrictions(
632 bazelProps bazel.BazelTargetModuleProperties,
633 commonAttrs CommonAttributes,
634 attrs interface{},
635 enabledProperty bazel.BoolAttribute) {
636 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, enabledProperty)
637}
638
639func (t *topDownMutatorContext) createBazelTargetModule(
640 bazelProps bazel.BazelTargetModuleProperties,
641 commonAttrs CommonAttributes,
642 attrs interface{},
643 enabledProperty bazel.BoolAttribute) {
644 constraintAttributes := commonAttrs.fillCommonBp2BuildModuleAttrs(t, enabledProperty)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000645 mod := t.Module()
Liz Kammer2ada09a2021-08-11 00:17:36 -0400646 info := bp2buildInfo{
Chris Parsons58852a02021-12-09 18:10:18 -0500647 Dir: t.OtherModuleDir(mod),
648 BazelProps: bazelProps,
649 CommonAttrs: commonAttrs,
650 ConstraintAttrs: constraintAttributes,
651 Attrs: attrs,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500652 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000653 mod.base().addBp2buildInfo(info)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500654}
655
Colin Crossdc35e212019-06-06 16:13:11 -0700656// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
657// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
658// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
659// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
660// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
661
Colin Crosscb55e082019-07-01 15:32:31 -0700662func (t *topDownMutatorContext) MutatorName() string {
663 return t.bp.MutatorName()
664}
665
Colin Crossdc35e212019-06-06 16:13:11 -0700666func (t *topDownMutatorContext) Rename(name string) {
667 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700668 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700669}
670
Liz Kammerf31c9002022-04-26 09:08:55 -0400671func (t *topDownMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
672 return t.bp.CreateModule(factory, name, props...)
673}
674
Colin Crosse003c4a2019-09-25 12:58:36 -0700675func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400676 return createModule(t, factory, "_topDownMutatorModule", props...)
Colin Crossdc35e212019-06-06 16:13:11 -0700677}
678
Liz Kammera060c452021-03-24 10:14:47 -0400679func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400680 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), "", props...).(Module)
Liz Kammera060c452021-03-24 10:14:47 -0400681 return module
682}
683
Colin Crosscb55e082019-07-01 15:32:31 -0700684func (b *bottomUpMutatorContext) MutatorName() string {
685 return b.bp.MutatorName()
686}
687
Colin Crossdc35e212019-06-06 16:13:11 -0700688func (b *bottomUpMutatorContext) Rename(name string) {
689 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700690 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700691}
692
Colin Cross4f1dcb02020-09-16 18:45:04 -0700693func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
694 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700695}
696
697func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
698 b.bp.AddReverseDependency(module, tag, name)
699}
700
Colin Cross43b92e02019-11-18 15:28:57 -0800701func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000702 if b.finalPhase {
703 panic("CreateVariations not allowed in FinalDepsMutators")
704 }
705
Colin Cross9a362232019-07-01 15:32:45 -0700706 modules := b.bp.CreateVariations(variations...)
707
Colin Cross43b92e02019-11-18 15:28:57 -0800708 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700709 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800710 aModules[i] = modules[i].(Module)
711 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700712 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
713 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
714 }
715
Colin Cross43b92e02019-11-18 15:28:57 -0800716 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700717}
718
Colin Cross43b92e02019-11-18 15:28:57 -0800719func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000720 if b.finalPhase {
721 panic("CreateLocalVariations not allowed in FinalDepsMutators")
722 }
723
Colin Cross9a362232019-07-01 15:32:45 -0700724 modules := b.bp.CreateLocalVariations(variations...)
725
Colin Cross43b92e02019-11-18 15:28:57 -0800726 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700727 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800728 aModules[i] = modules[i].(Module)
729 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700730 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
731 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
732 }
733
Colin Cross43b92e02019-11-18 15:28:57 -0800734 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700735}
736
737func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
738 b.bp.SetDependencyVariation(variation)
739}
740
Jiyong Park1d1119f2019-07-29 21:27:18 +0900741func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
742 b.bp.SetDefaultDependencyVariation(variation)
743}
744
Colin Crossdc35e212019-06-06 16:13:11 -0700745func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700746 names ...string) []blueprint.Module {
Colin Cross4f1dcb02020-09-16 18:45:04 -0700747 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700748}
749
750func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700751 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Colin Crossdc35e212019-06-06 16:13:11 -0700752
Colin Cross4f1dcb02020-09-16 18:45:04 -0700753 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700754}
755
756func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
757 b.bp.AddInterVariantDependency(tag, from, to)
758}
759
760func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
761 b.bp.ReplaceDependencies(name)
762}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800763
Paul Duffin80342d72020-06-26 22:08:43 +0100764func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
765 b.bp.ReplaceDependenciesIf(name, predicate)
766}
767
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800768func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
769 b.bp.AliasVariation(variationName)
770}
Colin Cross1b9604b2020-08-11 12:03:56 -0700771
772func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
773 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
774}
Colin Crossd27e7b82020-07-02 11:38:17 -0700775
776func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
777 b.bp.SetVariationProvider(module, provider, value)
778}