blob: 2ec051e59de480046d495077506f995f043367c1 [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"
Chris Parsons39a16972023-06-08 14:28:51 +000019 "android/soong/ui/metrics/bp2build_metrics_proto"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000020
Colin Cross795c3772017-03-16 16:50:10 -070021 "github.com/google/blueprint"
22)
Colin Cross6362e272015-10-29 15:25:03 -070023
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070024// Phases:
25// run Pre-arch mutators
26// run archMutator
27// run Pre-deps mutators
28// run depsMutator
29// run PostDeps mutators
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000030// run FinalDeps mutators (CreateVariations disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070031// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070032
Jingwen Chen73850672020-12-14 08:25:34 -050033// RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
Liz Kammerbe46fcc2021-11-01 15:32:43 -040034func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) {
Spandan Das5af0bd32022-09-28 20:43:08 +000035 bp2buildMutators := append(preArchMutators, registerBp2buildConversionMutator)
36 registerMutatorsForBazelConversion(ctx, bp2buildMutators)
37}
38
39// RegisterMutatorsForApiBazelConversion is an alternate registration pipeline for api_bp2build
40// This pipeline restricts generation of Bazel targets to Soong modules that contribute APIs
41func RegisterMutatorsForApiBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) {
42 bp2buildMutators := append(preArchMutators, registerApiBp2buildConversionMutator)
43 registerMutatorsForBazelConversion(ctx, bp2buildMutators)
44}
45
46func registerMutatorsForBazelConversion(ctx *Context, bp2buildMutators []RegisterMutatorFunc) {
Liz Kammer356f7d42021-01-26 09:18:53 -050047 mctx := &registerMutatorsContext{
48 bazelConversionMode: true,
Jingwen Chen041b1842021-02-01 00:23:25 -050049 }
50
Spandan Das5af0bd32022-09-28 20:43:08 +000051 allMutators := append([]RegisterMutatorFunc{
Liz Kammer356f7d42021-01-26 09:18:53 -050052 RegisterNamespaceMutator,
53 RegisterDefaultsPreArchMutators,
54 // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should
55 // evaluate the impact on conversion.
56 RegisterPrebuiltsPreArchMutators,
57 },
Spandan Das5af0bd32022-09-28 20:43:08 +000058 bp2buildMutators...)
Liz Kammer356f7d42021-01-26 09:18:53 -050059
Jingwen Chen73850672020-12-14 08:25:34 -050060 // Register bp2build mutators
Spandan Das5af0bd32022-09-28 20:43:08 +000061 for _, f := range allMutators {
Jingwen Chen73850672020-12-14 08:25:34 -050062 f(mctx)
63 }
64
Paul Duffin1d2d42f2021-03-06 20:08:12 +000065 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050066}
67
Paul Duffinc05b0342021-03-06 13:28:13 +000068// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
69// with the InitRegistrationContext and will be used at runtime.
70func collateGloballyRegisteredMutators() sortableComponents {
Liz Kammerc13f7852023-05-17 13:01:48 -040071 // ensure mixed builds mutator is the last mutator
72 finalDeps = append(finalDeps, registerMixedBuildsMutator)
Paul Duffinc05b0342021-03-06 13:28:13 +000073 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
74}
75
76// collateRegisteredMutators constructs a single list of mutators from the separate lists.
77func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070078 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080079
80 register := func(funcs []RegisterMutatorFunc) {
81 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070082 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080083 }
84 }
85
Colin Crosscec81712017-07-13 14:43:27 -070086 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080087
Colin Crosscec81712017-07-13 14:43:27 -070088 register(preDeps)
89
Liz Kammer356f7d42021-01-26 09:18:53 -050090 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070091
92 register(postDeps)
93
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000094 mctx.finalPhase = true
95 register(finalDeps)
96
Paul Duffinc05b0342021-03-06 13:28:13 +000097 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -070098}
99
100type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000101 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -0500102 finalPhase bool
103 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -0700104}
Colin Cross1e676be2016-10-12 14:38:15 -0700105
106type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -0700107 TopDown(name string, m TopDownMutator) MutatorHandle
108 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -0700109 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200110 Transition(name string, m TransitionMutator)
Colin Cross1e676be2016-10-12 14:38:15 -0700111}
112
113type RegisterMutatorFunc func(RegisterMutatorsContext)
114
Colin Crosscec81712017-07-13 14:43:27 -0700115var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800116 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100117
Paul Duffinaa4162e2020-05-05 11:35:43 +0100118 // Check the visibility rules are valid.
119 //
120 // This must run after the package renamer mutators so that any issues found during
121 // validation of the package's default_visibility property are reported using the
122 // correct package name and not the synthetic name.
123 //
124 // This must also be run before defaults mutators as the rules for validation are
125 // different before checking the rules than they are afterwards. e.g.
126 // visibility: ["//visibility:private", "//visibility:public"]
127 // would be invalid if specified in a module definition but is valid if it results
128 // from something like this:
129 //
130 // defaults {
131 // name: "defaults",
132 // // Be inaccessible outside a package by default.
133 // visibility: ["//visibility:private"]
134 // }
135 //
136 // defaultable_module {
137 // name: "defaultable_module",
138 // defaults: ["defaults"],
139 // // Override the default.
140 // visibility: ["//visibility:public"]
141 // }
142 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000143 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100144
Bob Badour37af0462021-01-07 03:34:31 +0000145 // Record the default_applicable_licenses for each package.
146 //
147 // This must run before the defaults so that defaults modules can pick up the package default.
148 RegisterLicensesPackageMapper,
149
Paul Duffinaa4162e2020-05-05 11:35:43 +0100150 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100151 //
152 // Any mutators that are added before this will not see any modules created by
153 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700154 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100155
Paul Duffin44f1d842020-06-26 20:17:02 +0100156 // Add dependencies on any components so that any component references can be
157 // resolved within the deps mutator.
158 //
159 // Must be run after defaults so it can be used to create dependencies on the
160 // component modules that are creating in a DefaultableHook.
161 //
162 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
163 // renamed. That is so that if a module creates components using a prebuilt module
164 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
165 // the prebuilt module and not the source module.
166 RegisterComponentsMutator,
167
Paul Duffinc988c8e2020-04-29 18:27:14 +0100168 // Create an association between prebuilt modules and their corresponding source
169 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100170 //
171 // Must be run after defaults mutators to ensure that any modules created by
172 // a DefaultableHook can be either a prebuilt or a source module with a matching
173 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100174 RegisterPrebuiltsPreArchMutators,
175
Bob Badour37af0462021-01-07 03:34:31 +0000176 // Gather the licenses properties for all modules for use during expansion and enforcement.
177 //
178 // This must come after the defaults mutators to ensure that any licenses supplied
179 // in a defaults module has been successfully applied before the rules are gathered.
180 RegisterLicensesPropertyGatherer,
181
Paul Duffinaa4162e2020-05-05 11:35:43 +0100182 // Gather the visibility rules for all modules for us during visibility enforcement.
183 //
184 // This must come after the defaults mutators to ensure that any visibility supplied
185 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000186 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700187}
188
Colin Crossae4c6182017-09-15 17:33:55 -0700189func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700190 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800191 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700192 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700193}
194
Colin Crosscec81712017-07-13 14:43:27 -0700195var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700196 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700197}
198
199var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800200 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700201 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000202 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000203 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100204 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700205 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700206}
Colin Cross1e676be2016-10-12 14:38:15 -0700207
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000208var finalDeps = []RegisterMutatorFunc{}
209
Colin Cross1e676be2016-10-12 14:38:15 -0700210func PreArchMutators(f RegisterMutatorFunc) {
211 preArch = append(preArch, f)
212}
213
214func PreDepsMutators(f RegisterMutatorFunc) {
215 preDeps = append(preDeps, f)
216}
217
218func PostDepsMutators(f RegisterMutatorFunc) {
219 postDeps = append(postDeps, f)
220}
221
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000222func FinalDepsMutators(f RegisterMutatorFunc) {
223 finalDeps = append(finalDeps, f)
224}
225
Liz Kammer356f7d42021-01-26 09:18:53 -0500226var bp2buildPreArchMutators = []RegisterMutatorFunc{}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400227
Liz Kammer12615db2021-09-28 09:19:17 -0400228// A minimal context for Bp2build conversion
229type Bp2buildMutatorContext interface {
230 BazelConversionPathContext
231
232 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
233}
234
Liz Kammer356f7d42021-01-26 09:18:53 -0500235// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
236// into Bazel BUILD targets that should run prior to deps and conversion.
237func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
238 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
239}
240
Colin Cross9f35c3d2020-09-16 19:04:41 -0700241type BaseMutatorContext interface {
242 BaseModuleContext
243
244 // MutatorName returns the name that this mutator was registered with.
245 MutatorName() string
246
247 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
248 // AddDependency or OtherModuleName until after this mutator pass is complete.
249 Rename(name string)
250}
251
Colin Cross25de6c32019-06-06 14:29:25 -0700252type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700253
Colin Cross635c3b02016-05-18 15:37:25 -0700254type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700255 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700256
Colin Cross9f35c3d2020-09-16 19:04:41 -0700257 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
258 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700259 CreateModule(ModuleFactory, ...interface{}) Module
Jingwen Chen1fd14692021-02-05 03:01:50 -0500260
261 // CreateBazelTargetModule creates a BazelTargetModule by calling the
262 // factory method, just like in CreateModule, but also requires
263 // BazelTargetModuleProperties containing additional metadata for the
264 // bp2build codegenerator.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000265 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
Chris Parsons58852a02021-12-09 18:10:18 -0500266
267 // CreateBazelTargetModuleWithRestrictions creates a BazelTargetModule by calling the
268 // factory method, just like in CreateModule, but also requires
269 // BazelTargetModuleProperties containing additional metadata for the
270 // bp2build codegenerator. The generated target is restricted to only be buildable for certain
271 // platforms, as dictated by a given bool attribute: the target will not be buildable in
272 // any platform for which this bool attribute is false.
273 CreateBazelTargetModuleWithRestrictions(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}, bazel.BoolAttribute)
Spandan Dasabedff02023-03-07 19:24:34 +0000274
Chris Parsons39a16972023-06-08 14:28:51 +0000275 // MarkBp2buildUnconvertible registers the current module as "unconvertible to bp2build" for the
276 // given reason.
277 MarkBp2buildUnconvertible(reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string)
278
Spandan Dasabedff02023-03-07 19:24:34 +0000279 // CreateBazelTargetAliasInDir creates an alias definition in `dir` directory.
280 // This function can be used to create alias definitions in a directory that is different
281 // from the directory of the visited Soong module.
282 CreateBazelTargetAliasInDir(dir string, name string, actual bazel.Label)
Spandan Das6a448ec2023-04-19 17:36:12 +0000283
284 // CreateBazelConfigSetting creates a config_setting in <dir>/BUILD.bazel
285 // build/bazel has several static config_setting(s) that are used in Bazel builds.
286 // This function can be used to createa additional config_setting(s) based on the build graph
287 // (e.g. a config_setting specific to an apex variant)
288 CreateBazelConfigSetting(csa bazel.ConfigSettingAttributes, ca CommonAttributes, dir string)
Colin Cross6362e272015-10-29 15:25:03 -0700289}
290
Colin Cross25de6c32019-06-06 14:29:25 -0700291type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700292 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700293 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700294}
295
Colin Cross25de6c32019-06-06 14:29:25 -0700296type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700297
Colin Cross635c3b02016-05-18 15:37:25 -0700298type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700299 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800300
Colin Cross4f1dcb02020-09-16 18:45:04 -0700301 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
302 // dependency (some entries may be nil).
303 //
304 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
305 // new dependencies have had the current mutator called on them. If the mutator is not
306 // parallel this method does not affect the ordering of the current mutator pass, but will
307 // be ordered correctly for all future mutator passes.
308 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700309
310 // AddReverseDependency adds a dependency from the destination to the given module.
311 // Does not affect the ordering of the current mutator pass, but will be ordered
312 // correctly for all future mutator passes. All reverse dependencies for a destination module are
313 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
314 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800315 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700316
317 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
318 // parameter. It returns a list of new modules in the same order as the variationNames
319 // list.
320 //
321 // If any of the dependencies of the module being operated on were already split
322 // by calling CreateVariations with the same name, the dependency will automatically
323 // be updated to point the matching variant.
324 //
325 // If a module is split, and then a module depending on the first module is not split
326 // when the Mutator is later called on it, the dependency of the depending module will
327 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800328 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700329
330 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
331 // parameter. It returns a list of new modules in the same order as the variantNames
332 // list.
333 //
334 // Local variations do not affect automatic dependency resolution - dependencies added
335 // to the split module via deps or DynamicDependerModule must exactly match a variant
336 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800337 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700338
339 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
340 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800341 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700342
343 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
344 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900345 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700346
347 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700348 // argument to select which variant of the dependency to use. It returns a slice of modules for
349 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
Usta Shresthac725f472022-01-11 02:44:21 -0500350 // all the non-local variations of the current module, plus the variations argument.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700351 //
352 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
353 // new dependencies have had the current mutator called on them. If the mutator is not
354 // parallel this method does not affect the ordering of the current mutator pass, but will
355 // be ordered correctly for all future mutator passes.
Usta Shresthac725f472022-01-11 02:44:21 -0500356 AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700357
358 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700359 // variations argument to select which variant of the dependency to use. It returns a slice of
360 // modules for each dependency (some entries may be nil). A variant of the dependency must
361 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700362 // For any unspecified variation the first variant will be used.
363 //
364 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
365 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700366 //
367 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
368 // new dependencies have had the current mutator called on them. If the mutator is not
369 // parallel this method does not affect the ordering of the current mutator pass, but will
370 // be ordered correctly for all future mutator passes.
371 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700372
373 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
374 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
375 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
376 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800377 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700378
379 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
380 // specified name with the current variant of this module. Replacements don't take effect until
381 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800382 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700383
384 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
385 // specified name with the current variant of this module as long as the supplied predicate returns
386 // true.
387 //
388 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100389 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700390
391 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
392 // and creates an alias from the current variant (before the mutator has run) to the new
393 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
394 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
395 // be used to add dependencies on the newly created variant using the variant map from
396 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800397 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700398
399 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
400 // module, and creates an alias from a new fromVariationName variant the toVariationName
401 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
402 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
403 // be used to add dependencies on the toVariationName variant using the fromVariationName
404 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700405 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700406
407 // SetVariationProvider sets the value for a provider for the given newly created variant of
408 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
409 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
410 // if the value is not of the appropriate type, or if the module is not a newly created
411 // variant of the current module. The value should not be modified after being passed to
412 // SetVariationProvider.
413 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700414}
415
Colin Cross25de6c32019-06-06 14:29:25 -0700416type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700417 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700418 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400419 finalPhase 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
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400425 moduleContext := a.base().baseModuleContextFactory(ctx)
426 moduleContext.bazelConversionMode = bazelConversionMode
427
Colin Cross617b88a2020-08-24 18:04:09 -0700428 return &bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400429 bp: ctx,
Cole Faust0abd4b42023-01-10 10:49:18 -0800430 baseModuleContext: moduleContext,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400431 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700432 }
433}
434
Colin Cross25de6c32019-06-06 14:29:25 -0700435func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000436 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500437 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700438 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700439 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500440 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700441 }
Colin Cross798bfce2016-10-12 14:28:16 -0700442 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500443 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700444 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700445 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700446}
447
Colin Cross617b88a2020-08-24 18:04:09 -0700448func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
449 mutator := &mutator{name: name, bottomUpMutator: m}
450 x.mutators = append(x.mutators, mutator)
451 return mutator
452}
453
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200454type IncomingTransitionContext interface {
455 // Module returns the target of the dependency edge for which the transition
456 // is being computed
457 Module() Module
458
459 // Config returns the configuration for the build.
460 Config() Config
461}
462
463type OutgoingTransitionContext interface {
464 // Module returns the target of the dependency edge for which the transition
465 // is being computed
466 Module() Module
467
468 // DepTag() Returns the dependency tag through which this dependency is
469 // reached
470 DepTag() blueprint.DependencyTag
471}
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200472
473// Transition mutators implement a top-down mechanism where a module tells its
474// direct dependencies what variation they should be built in but the dependency
475// has the final say.
476//
477// When implementing a transition mutator, one needs to implement four methods:
478// - Split() that tells what variations a module has by itself
479// - OutgoingTransition() where a module tells what it wants from its
480// dependency
481// - IncomingTransition() where a module has the final say about its own
482// variation
483// - Mutate() that changes the state of a module depending on its variation
484//
485// That the effective variation of module B when depended on by module A is the
486// composition the outgoing transition of module A and the incoming transition
487// of module B.
488//
489// the outgoing transition should not take the properties of the dependency into
490// account, only those of the module that depends on it. For this reason, the
491// dependency is not even passed into it as an argument. Likewise, the incoming
492// transition should not take the properties of the depending module into
493// account and is thus not informed about it. This makes for a nice
494// decomposition of the decision logic.
495//
496// A given transition mutator only affects its own variation; other variations
497// stay unchanged along the dependency edges.
498//
499// Soong makes sure that all modules are created in the desired variations and
500// that dependency edges are set up correctly. This ensures that "missing
501// variation" errors do not happen and allows for more flexible changes in the
502// value of the variation among dependency edges (as oppposed to bottom-up
503// mutators where if module A in variation X depends on module B and module B
504// has that variation X, A must depend on variation X of B)
505//
506// The limited power of the context objects passed to individual mutators
507// methods also makes it more difficult to shoot oneself in the foot. Complete
508// safety is not guaranteed because no one prevents individual transition
509// mutators from mutating modules in illegal ways and for e.g. Split() or
510// Mutate() to run their own visitations of the transitive dependency of the
511// module and both of these are bad ideas, but it's better than no guardrails at
512// all.
513//
514// This model is pretty close to Bazel's configuration transitions. The mapping
515// between concepts in Soong and Bazel is as follows:
516// - Module == configured target
517// - Variant == configuration
518// - Variation name == configuration flag
519// - Variation == configuration flag value
520// - Outgoing transition == attribute transition
521// - Incoming transition == rule transition
522//
523// The Split() method does not have a Bazel equivalent and Bazel split
524// transitions do not have a Soong equivalent.
525//
526// Mutate() does not make sense in Bazel due to the different models of the
527// two systems: when creating new variations, Soong clones the old module and
528// thus some way is needed to change it state whereas Bazel creates each
529// configuration of a given configured target anew.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200530type TransitionMutator interface {
531 // Split returns the set of variations that should be created for a module no
532 // matter who depends on it. Used when Make depends on a particular variation
533 // or when the module knows its variations just based on information given to
534 // it in the Blueprint file. This method should not mutate the module it is
535 // called on.
536 Split(ctx BaseModuleContext) []string
537
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200538 // Called on a module to determine which variation it wants from its direct
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200539 // dependencies. The dependency itself can override this decision. This method
540 // should not mutate the module itself.
541 OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string
542
543 // Called on a module to determine which variation it should be in based on
544 // the variation modules that depend on it want. This gives the module a final
545 // say about its own variations. This method should not mutate the module
546 // itself.
547 IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string
548
549 // Called after a module was split into multiple variations on each variation.
550 // It should not split the module any further but adding new dependencies is
551 // fine. Unlike all the other methods on TransitionMutator, this method is
552 // allowed to mutate the module.
553 Mutate(ctx BottomUpMutatorContext, variation string)
554}
555
556type androidTransitionMutator struct {
557 finalPhase bool
558 bazelConversionMode bool
559 mutator TransitionMutator
560}
561
562func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []string {
563 if m, ok := ctx.Module().(Module); ok {
564 moduleContext := m.base().baseModuleContextFactory(ctx)
565 moduleContext.bazelConversionMode = a.bazelConversionMode
566 return a.mutator.Split(&moduleContext)
567 } else {
568 return []string{""}
569 }
570}
571
572type outgoingTransitionContextImpl struct {
573 bp blueprint.OutgoingTransitionContext
574}
575
576func (c *outgoingTransitionContextImpl) Module() Module {
577 return c.bp.Module().(Module)
578}
579
580func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag {
581 return c.bp.DepTag()
582}
583
584func (a *androidTransitionMutator) OutgoingTransition(ctx blueprint.OutgoingTransitionContext, sourceVariation string) string {
585 if _, ok := ctx.Module().(Module); ok {
586 return a.mutator.OutgoingTransition(&outgoingTransitionContextImpl{bp: ctx}, sourceVariation)
587 } else {
588 return ""
589 }
590}
591
592type incomingTransitionContextImpl struct {
593 bp blueprint.IncomingTransitionContext
594}
595
596func (c *incomingTransitionContextImpl) Module() Module {
597 return c.bp.Module().(Module)
598}
599
600func (c *incomingTransitionContextImpl) Config() Config {
601 return c.bp.Config().(Config)
602}
603
604func (a *androidTransitionMutator) IncomingTransition(ctx blueprint.IncomingTransitionContext, incomingVariation string) string {
605 if _, ok := ctx.Module().(Module); ok {
606 return a.mutator.IncomingTransition(&incomingTransitionContextImpl{bp: ctx}, incomingVariation)
607 } else {
608 return ""
609 }
610}
611
612func (a *androidTransitionMutator) Mutate(ctx blueprint.BottomUpMutatorContext, variation string) {
613 if am, ok := ctx.Module().(Module); ok {
614 a.mutator.Mutate(bottomUpMutatorContextFactory(ctx, am, a.finalPhase, a.bazelConversionMode), variation)
615 }
616}
617
618func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) {
619 atm := &androidTransitionMutator{
620 finalPhase: x.finalPhase,
621 bazelConversionMode: x.bazelConversionMode,
622 mutator: m,
623 }
624 mutator := &mutator{
625 name: name,
626 transitionMutator: atm}
627 x.mutators = append(x.mutators, mutator)
628}
629
Liz Kammer356f7d42021-01-26 09:18:53 -0500630func (x *registerMutatorsContext) mutatorName(name string) string {
631 if x.bazelConversionMode {
632 return name + "_bp2build"
633 }
634 return name
635}
636
Colin Cross25de6c32019-06-06 14:29:25 -0700637func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700638 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700639 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400640 moduleContext := a.base().baseModuleContextFactory(ctx)
641 moduleContext.bazelConversionMode = x.bazelConversionMode
Colin Cross25de6c32019-06-06 14:29:25 -0700642 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700643 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400644 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700645 }
Colin Cross798bfce2016-10-12 14:28:16 -0700646 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700647 }
Colin Cross798bfce2016-10-12 14:28:16 -0700648 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500649 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700650 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700651 return mutator
652}
653
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000654func (mutator *mutator) componentName() string {
655 return mutator.name
656}
657
658func (mutator *mutator) register(ctx *Context) {
659 blueprintCtx := ctx.Context
660 var handle blueprint.MutatorHandle
661 if mutator.bottomUpMutator != nil {
662 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
663 } else if mutator.topDownMutator != nil {
664 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200665 } else if mutator.transitionMutator != nil {
666 blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000667 }
668 if mutator.parallel {
669 handle.Parallel()
670 }
671}
672
Colin Cross798bfce2016-10-12 14:28:16 -0700673type MutatorHandle interface {
674 Parallel() MutatorHandle
675}
676
677func (mutator *mutator) Parallel() MutatorHandle {
678 mutator.parallel = true
679 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700680}
Colin Cross1e676be2016-10-12 14:38:15 -0700681
Paul Duffin44f1d842020-06-26 20:17:02 +0100682func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
683 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
684}
685
686// A special mutator that runs just prior to the deps mutator to allow the dependencies
687// on component modules to be added so that they can depend directly on a prebuilt
688// module.
689func componentDepsMutator(ctx BottomUpMutatorContext) {
690 if m := ctx.Module(); m.Enabled() {
691 m.ComponentDepsMutator(ctx)
692 }
693}
694
Colin Cross1e676be2016-10-12 14:38:15 -0700695func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100696 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700697 m.DepsMutator(ctx)
698 }
699}
Colin Crossd11fcda2017-10-23 17:59:01 -0700700
Liz Kammer356f7d42021-01-26 09:18:53 -0500701func registerDepsMutator(ctx RegisterMutatorsContext) {
702 ctx.BottomUp("deps", depsMutator).Parallel()
703}
704
705func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
706 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
707 // being converted to build targets.
708 ctx.BottomUp("deps", depsMutator).Parallel()
709}
710
Jingwen Chen1fd14692021-02-05 03:01:50 -0500711func (t *topDownMutatorContext) CreateBazelTargetModule(
Jingwen Chen1fd14692021-02-05 03:01:50 -0500712 bazelProps bazel.BazelTargetModuleProperties,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000713 commonAttrs CommonAttributes,
Liz Kammer2ada09a2021-08-11 00:17:36 -0400714 attrs interface{}) {
Chris Parsons58852a02021-12-09 18:10:18 -0500715 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, bazel.BoolAttribute{})
716}
717
718func (t *topDownMutatorContext) CreateBazelTargetModuleWithRestrictions(
719 bazelProps bazel.BazelTargetModuleProperties,
720 commonAttrs CommonAttributes,
721 attrs interface{},
722 enabledProperty bazel.BoolAttribute) {
723 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, enabledProperty)
724}
725
Chris Parsons39a16972023-06-08 14:28:51 +0000726func (t *topDownMutatorContext) MarkBp2buildUnconvertible(
727 reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string) {
728 mod := t.Module()
729 mod.base().setBp2buildUnconvertible(reasonType, detail)
730}
731
Spandan Dasabedff02023-03-07 19:24:34 +0000732var (
733 bazelAliasModuleProperties = bazel.BazelTargetModuleProperties{
734 Rule_class: "alias",
735 }
736)
737
738type bazelAliasAttributes struct {
739 Actual *bazel.LabelAttribute
740}
741
742func (t *topDownMutatorContext) CreateBazelTargetAliasInDir(
743 dir string,
744 name string,
745 actual bazel.Label) {
746 mod := t.Module()
747 attrs := &bazelAliasAttributes{
748 Actual: bazel.MakeLabelAttribute(actual.Label),
749 }
750 info := bp2buildInfo{
751 Dir: dir,
752 BazelProps: bazelAliasModuleProperties,
753 CommonAttrs: CommonAttributes{Name: name},
754 ConstraintAttrs: constraintAttributes{},
755 Attrs: attrs,
756 }
757 mod.base().addBp2buildInfo(info)
758}
759
Spandan Das6a448ec2023-04-19 17:36:12 +0000760func (t *topDownMutatorContext) CreateBazelConfigSetting(
761 csa bazel.ConfigSettingAttributes,
762 ca CommonAttributes,
763 dir string) {
764 mod := t.Module()
765 info := bp2buildInfo{
766 Dir: dir,
767 BazelProps: bazel.BazelTargetModuleProperties{
768 Rule_class: "config_setting",
769 },
770 CommonAttrs: ca,
771 ConstraintAttrs: constraintAttributes{},
772 Attrs: &csa,
773 }
774 mod.base().addBp2buildInfo(info)
775}
776
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000777// ApexAvailableTags converts the apex_available property value of an ApexModule
778// module and returns it as a list of keyed tags.
779func ApexAvailableTags(mod Module) bazel.StringListAttribute {
780 attr := bazel.StringListAttribute{}
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000781 // Transform specific attributes into tags.
782 if am, ok := mod.(ApexModule); ok {
783 // TODO(b/218841706): hidl_interface has the apex_available prop, but it's
784 // defined directly as a prop and not via ApexModule, so this doesn't
785 // pick those props up.
Spandan Das2dc6dfc2023-04-17 19:16:06 +0000786 apexAvailable := am.apexModuleBase().ApexAvailable()
787 // If a user does not specify apex_available in Android.bp, then soong provides a default.
788 // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
789 if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
790 apexAvailable = []string{}
791 }
792 attr.Value = ConvertApexAvailableToTags(apexAvailable)
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000793 }
794 return attr
795}
796
Spandan Dasf57a9662023-04-12 19:05:49 +0000797func ApexAvailableTagsWithoutTestApexes(ctx BaseModuleContext, mod Module) bazel.StringListAttribute {
798 attr := bazel.StringListAttribute{}
799 if am, ok := mod.(ApexModule); ok {
800 apexAvailableWithoutTestApexes := removeTestApexes(ctx, am.apexModuleBase().ApexAvailable())
801 // If a user does not specify apex_available in Android.bp, then soong provides a default.
802 // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
803 if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
804 apexAvailableWithoutTestApexes = []string{}
805 }
806 attr.Value = ConvertApexAvailableToTags(apexAvailableWithoutTestApexes)
807 }
808 return attr
809}
810
811func removeTestApexes(ctx BaseModuleContext, apex_available []string) []string {
812 testApexes := []string{}
813 for _, aa := range apex_available {
814 // ignore the wildcards
815 if InList(aa, AvailableToRecognziedWildcards) {
816 continue
817 }
818 mod, _ := ctx.ModuleFromName(aa)
819 if apex, ok := mod.(ApexTestInterface); ok && apex.IsTestApex() {
820 testApexes = append(testApexes, aa)
821 }
822 }
823 return RemoveListFromList(CopyOf(apex_available), testApexes)
824}
825
Cole Faustfb11c1c2023-02-10 11:27:46 -0800826func ConvertApexAvailableToTags(apexAvailable []string) []string {
827 if len(apexAvailable) == 0 {
828 // We need nil specifically to make bp2build not add the tags property at all,
829 // instead of adding it with an empty list
830 return nil
831 }
832 result := make([]string, 0, len(apexAvailable))
833 for _, a := range apexAvailable {
834 result = append(result, "apex_available="+a)
835 }
836 return result
837}
838
Spandan Das39b6cc52023-04-12 19:05:49 +0000839// ConvertApexAvailableToTagsWithoutTestApexes converts a list of apex names to a list of bazel tags
840// This function drops any test apexes from the input.
841func ConvertApexAvailableToTagsWithoutTestApexes(ctx BaseModuleContext, apexAvailable []string) []string {
842 noTestApexes := removeTestApexes(ctx, apexAvailable)
843 return ConvertApexAvailableToTags(noTestApexes)
844}
845
Chris Parsons58852a02021-12-09 18:10:18 -0500846func (t *topDownMutatorContext) createBazelTargetModule(
847 bazelProps bazel.BazelTargetModuleProperties,
848 commonAttrs CommonAttributes,
849 attrs interface{},
850 enabledProperty bazel.BoolAttribute) {
851 constraintAttributes := commonAttrs.fillCommonBp2BuildModuleAttrs(t, enabledProperty)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000852 mod := t.Module()
Liz Kammer2ada09a2021-08-11 00:17:36 -0400853 info := bp2buildInfo{
Chris Parsons58852a02021-12-09 18:10:18 -0500854 Dir: t.OtherModuleDir(mod),
855 BazelProps: bazelProps,
856 CommonAttrs: commonAttrs,
857 ConstraintAttrs: constraintAttributes,
858 Attrs: attrs,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500859 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000860 mod.base().addBp2buildInfo(info)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500861}
862
Colin Crossdc35e212019-06-06 16:13:11 -0700863// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
864// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
865// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
866// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
867// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
868
Colin Crosscb55e082019-07-01 15:32:31 -0700869func (t *topDownMutatorContext) MutatorName() string {
870 return t.bp.MutatorName()
871}
872
Colin Crossdc35e212019-06-06 16:13:11 -0700873func (t *topDownMutatorContext) Rename(name string) {
874 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700875 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700876}
877
Liz Kammerf31c9002022-04-26 09:08:55 -0400878func (t *topDownMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
879 return t.bp.CreateModule(factory, name, props...)
880}
881
Colin Crosse003c4a2019-09-25 12:58:36 -0700882func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400883 return createModule(t, factory, "_topDownMutatorModule", props...)
Colin Crossdc35e212019-06-06 16:13:11 -0700884}
885
Liz Kammera060c452021-03-24 10:14:47 -0400886func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400887 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), "", props...).(Module)
Liz Kammera060c452021-03-24 10:14:47 -0400888 return module
889}
890
Colin Crosscb55e082019-07-01 15:32:31 -0700891func (b *bottomUpMutatorContext) MutatorName() string {
892 return b.bp.MutatorName()
893}
894
Colin Crossdc35e212019-06-06 16:13:11 -0700895func (b *bottomUpMutatorContext) Rename(name string) {
896 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700897 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700898}
899
Colin Cross4f1dcb02020-09-16 18:45:04 -0700900func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400901 if b.baseModuleContext.checkedMissingDeps() {
902 panic("Adding deps not allowed after checking for missing deps")
903 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700904 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700905}
906
907func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400908 if b.baseModuleContext.checkedMissingDeps() {
909 panic("Adding deps not allowed after checking for missing deps")
910 }
Colin Crossdc35e212019-06-06 16:13:11 -0700911 b.bp.AddReverseDependency(module, tag, name)
912}
913
Colin Cross43b92e02019-11-18 15:28:57 -0800914func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000915 if b.finalPhase {
916 panic("CreateVariations not allowed in FinalDepsMutators")
917 }
918
Colin Cross9a362232019-07-01 15:32:45 -0700919 modules := b.bp.CreateVariations(variations...)
920
Colin Cross43b92e02019-11-18 15:28:57 -0800921 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700922 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800923 aModules[i] = modules[i].(Module)
924 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700925 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
926 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
927 }
928
Colin Cross43b92e02019-11-18 15:28:57 -0800929 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700930}
931
Colin Cross43b92e02019-11-18 15:28:57 -0800932func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000933 if b.finalPhase {
934 panic("CreateLocalVariations not allowed in FinalDepsMutators")
935 }
936
Colin Cross9a362232019-07-01 15:32:45 -0700937 modules := b.bp.CreateLocalVariations(variations...)
938
Colin Cross43b92e02019-11-18 15:28:57 -0800939 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700940 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800941 aModules[i] = modules[i].(Module)
942 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700943 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
944 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
945 }
946
Colin Cross43b92e02019-11-18 15:28:57 -0800947 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700948}
949
950func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
951 b.bp.SetDependencyVariation(variation)
952}
953
Jiyong Park1d1119f2019-07-29 21:27:18 +0900954func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
955 b.bp.SetDefaultDependencyVariation(variation)
956}
957
Colin Crossdc35e212019-06-06 16:13:11 -0700958func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700959 names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400960 if b.baseModuleContext.checkedMissingDeps() {
961 panic("Adding deps not allowed after checking for missing deps")
962 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700963 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700964}
965
966func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700967 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400968 if b.baseModuleContext.checkedMissingDeps() {
969 panic("Adding deps not allowed after checking for missing deps")
970 }
Colin Crossdc35e212019-06-06 16:13:11 -0700971
Colin Cross4f1dcb02020-09-16 18:45:04 -0700972 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700973}
974
975func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
976 b.bp.AddInterVariantDependency(tag, from, to)
977}
978
979func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400980 if b.baseModuleContext.checkedMissingDeps() {
981 panic("Adding deps not allowed after checking for missing deps")
982 }
Colin Crossdc35e212019-06-06 16:13:11 -0700983 b.bp.ReplaceDependencies(name)
984}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800985
Paul Duffin80342d72020-06-26 22:08:43 +0100986func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400987 if b.baseModuleContext.checkedMissingDeps() {
988 panic("Adding deps not allowed after checking for missing deps")
989 }
Paul Duffin80342d72020-06-26 22:08:43 +0100990 b.bp.ReplaceDependenciesIf(name, predicate)
991}
992
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800993func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
994 b.bp.AliasVariation(variationName)
995}
Colin Cross1b9604b2020-08-11 12:03:56 -0700996
997func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
998 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
999}
Colin Crossd27e7b82020-07-02 11:38:17 -07001000
1001func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
1002 b.bp.SetVariationProvider(module, provider, value)
1003}