blob: 0284794276e8e7a685f97c547cb16f263d5eb690 [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 (
Chris Parsons637458d2023-09-19 20:09:00 +000018 "path/filepath"
19
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000020 "android/soong/bazel"
Chris Parsons39a16972023-06-08 14:28:51 +000021 "android/soong/ui/metrics/bp2build_metrics_proto"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000022
Colin Cross795c3772017-03-16 16:50:10 -070023 "github.com/google/blueprint"
24)
Colin Cross6362e272015-10-29 15:25:03 -070025
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070026// Phases:
27// run Pre-arch mutators
28// run archMutator
29// run Pre-deps mutators
30// run depsMutator
31// run PostDeps mutators
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000032// run FinalDeps mutators (CreateVariations disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070033// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070034
Jingwen Chen73850672020-12-14 08:25:34 -050035// RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
Liz Kammerbe46fcc2021-11-01 15:32:43 -040036func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) {
Spandan Das5af0bd32022-09-28 20:43:08 +000037 bp2buildMutators := append(preArchMutators, registerBp2buildConversionMutator)
38 registerMutatorsForBazelConversion(ctx, bp2buildMutators)
39}
40
41// RegisterMutatorsForApiBazelConversion is an alternate registration pipeline for api_bp2build
42// This pipeline restricts generation of Bazel targets to Soong modules that contribute APIs
43func RegisterMutatorsForApiBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) {
44 bp2buildMutators := append(preArchMutators, registerApiBp2buildConversionMutator)
45 registerMutatorsForBazelConversion(ctx, bp2buildMutators)
46}
47
48func registerMutatorsForBazelConversion(ctx *Context, bp2buildMutators []RegisterMutatorFunc) {
Liz Kammer356f7d42021-01-26 09:18:53 -050049 mctx := &registerMutatorsContext{
50 bazelConversionMode: true,
Jingwen Chen041b1842021-02-01 00:23:25 -050051 }
52
Spandan Das5af0bd32022-09-28 20:43:08 +000053 allMutators := append([]RegisterMutatorFunc{
Liz Kammer356f7d42021-01-26 09:18:53 -050054 RegisterNamespaceMutator,
55 RegisterDefaultsPreArchMutators,
56 // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should
57 // evaluate the impact on conversion.
58 RegisterPrebuiltsPreArchMutators,
59 },
Spandan Das5af0bd32022-09-28 20:43:08 +000060 bp2buildMutators...)
Liz Kammer356f7d42021-01-26 09:18:53 -050061
Jingwen Chen73850672020-12-14 08:25:34 -050062 // Register bp2build mutators
Spandan Das5af0bd32022-09-28 20:43:08 +000063 for _, f := range allMutators {
Jingwen Chen73850672020-12-14 08:25:34 -050064 f(mctx)
65 }
66
Paul Duffin1d2d42f2021-03-06 20:08:12 +000067 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050068}
69
Paul Duffinc05b0342021-03-06 13:28:13 +000070// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
71// with the InitRegistrationContext and will be used at runtime.
72func collateGloballyRegisteredMutators() sortableComponents {
Liz Kammerc13f7852023-05-17 13:01:48 -040073 // ensure mixed builds mutator is the last mutator
74 finalDeps = append(finalDeps, registerMixedBuildsMutator)
Paul Duffinc05b0342021-03-06 13:28:13 +000075 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
76}
77
78// collateRegisteredMutators constructs a single list of mutators from the separate lists.
79func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070080 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080081
82 register := func(funcs []RegisterMutatorFunc) {
83 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070084 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080085 }
86 }
87
Colin Crosscec81712017-07-13 14:43:27 -070088 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080089
Colin Crosscec81712017-07-13 14:43:27 -070090 register(preDeps)
91
Liz Kammer356f7d42021-01-26 09:18:53 -050092 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070093
94 register(postDeps)
95
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000096 mctx.finalPhase = true
97 register(finalDeps)
98
Paul Duffinc05b0342021-03-06 13:28:13 +000099 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -0700100}
101
102type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000103 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -0500104 finalPhase bool
105 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -0700106}
Colin Cross1e676be2016-10-12 14:38:15 -0700107
108type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -0700109 TopDown(name string, m TopDownMutator) MutatorHandle
110 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -0700111 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200112 Transition(name string, m TransitionMutator)
Colin Cross1e676be2016-10-12 14:38:15 -0700113}
114
115type RegisterMutatorFunc func(RegisterMutatorsContext)
116
Colin Crosscec81712017-07-13 14:43:27 -0700117var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800118 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100119
Paul Duffinaa4162e2020-05-05 11:35:43 +0100120 // Check the visibility rules are valid.
121 //
122 // This must run after the package renamer mutators so that any issues found during
123 // validation of the package's default_visibility property are reported using the
124 // correct package name and not the synthetic name.
125 //
126 // This must also be run before defaults mutators as the rules for validation are
127 // different before checking the rules than they are afterwards. e.g.
128 // visibility: ["//visibility:private", "//visibility:public"]
129 // would be invalid if specified in a module definition but is valid if it results
130 // from something like this:
131 //
132 // defaults {
133 // name: "defaults",
134 // // Be inaccessible outside a package by default.
135 // visibility: ["//visibility:private"]
136 // }
137 //
138 // defaultable_module {
139 // name: "defaultable_module",
140 // defaults: ["defaults"],
141 // // Override the default.
142 // visibility: ["//visibility:public"]
143 // }
144 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000145 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100146
Bob Badour37af0462021-01-07 03:34:31 +0000147 // Record the default_applicable_licenses for each package.
148 //
149 // This must run before the defaults so that defaults modules can pick up the package default.
150 RegisterLicensesPackageMapper,
151
Paul Duffinaa4162e2020-05-05 11:35:43 +0100152 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100153 //
154 // Any mutators that are added before this will not see any modules created by
155 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700156 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100157
Paul Duffin44f1d842020-06-26 20:17:02 +0100158 // Add dependencies on any components so that any component references can be
159 // resolved within the deps mutator.
160 //
161 // Must be run after defaults so it can be used to create dependencies on the
162 // component modules that are creating in a DefaultableHook.
163 //
164 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
165 // renamed. That is so that if a module creates components using a prebuilt module
166 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
167 // the prebuilt module and not the source module.
168 RegisterComponentsMutator,
169
Paul Duffinc988c8e2020-04-29 18:27:14 +0100170 // Create an association between prebuilt modules and their corresponding source
171 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100172 //
173 // Must be run after defaults mutators to ensure that any modules created by
174 // a DefaultableHook can be either a prebuilt or a source module with a matching
175 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100176 RegisterPrebuiltsPreArchMutators,
177
Bob Badour37af0462021-01-07 03:34:31 +0000178 // Gather the licenses properties for all modules for use during expansion and enforcement.
179 //
180 // This must come after the defaults mutators to ensure that any licenses supplied
181 // in a defaults module has been successfully applied before the rules are gathered.
182 RegisterLicensesPropertyGatherer,
183
Paul Duffinaa4162e2020-05-05 11:35:43 +0100184 // Gather the visibility rules for all modules for us during visibility enforcement.
185 //
186 // This must come after the defaults mutators to ensure that any visibility supplied
187 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000188 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700189}
190
Colin Crossae4c6182017-09-15 17:33:55 -0700191func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700192 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800193 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700194 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700195}
196
Colin Crosscec81712017-07-13 14:43:27 -0700197var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700198 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700199}
200
201var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800202 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700203 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000204 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000205 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100206 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700207 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700208}
Colin Cross1e676be2016-10-12 14:38:15 -0700209
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000210var finalDeps = []RegisterMutatorFunc{}
211
Colin Cross1e676be2016-10-12 14:38:15 -0700212func PreArchMutators(f RegisterMutatorFunc) {
213 preArch = append(preArch, f)
214}
215
216func PreDepsMutators(f RegisterMutatorFunc) {
217 preDeps = append(preDeps, f)
218}
219
220func PostDepsMutators(f RegisterMutatorFunc) {
221 postDeps = append(postDeps, f)
222}
223
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000224func FinalDepsMutators(f RegisterMutatorFunc) {
225 finalDeps = append(finalDeps, f)
226}
227
Liz Kammer356f7d42021-01-26 09:18:53 -0500228var bp2buildPreArchMutators = []RegisterMutatorFunc{}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400229
Liz Kammer12615db2021-09-28 09:19:17 -0400230// A minimal context for Bp2build conversion
231type Bp2buildMutatorContext interface {
232 BazelConversionPathContext
Colin Cross9f35c3d2020-09-16 19:04:41 -0700233 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700234
Jingwen Chen1fd14692021-02-05 03:01:50 -0500235 // CreateBazelTargetModule creates a BazelTargetModule by calling the
236 // factory method, just like in CreateModule, but also requires
237 // BazelTargetModuleProperties containing additional metadata for the
238 // bp2build codegenerator.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000239 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
Chris Parsons58852a02021-12-09 18:10:18 -0500240
241 // CreateBazelTargetModuleWithRestrictions creates a BazelTargetModule by calling the
242 // factory method, just like in CreateModule, but also requires
243 // BazelTargetModuleProperties containing additional metadata for the
244 // bp2build codegenerator. The generated target is restricted to only be buildable for certain
245 // platforms, as dictated by a given bool attribute: the target will not be buildable in
246 // any platform for which this bool attribute is false.
247 CreateBazelTargetModuleWithRestrictions(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}, bazel.BoolAttribute)
Spandan Dasabedff02023-03-07 19:24:34 +0000248
Chris Parsons39a16972023-06-08 14:28:51 +0000249 // MarkBp2buildUnconvertible registers the current module as "unconvertible to bp2build" for the
250 // given reason.
251 MarkBp2buildUnconvertible(reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string)
252
Spandan Dasabedff02023-03-07 19:24:34 +0000253 // CreateBazelTargetAliasInDir creates an alias definition in `dir` directory.
254 // This function can be used to create alias definitions in a directory that is different
255 // from the directory of the visited Soong module.
256 CreateBazelTargetAliasInDir(dir string, name string, actual bazel.Label)
Spandan Das6a448ec2023-04-19 17:36:12 +0000257
258 // CreateBazelConfigSetting creates a config_setting in <dir>/BUILD.bazel
259 // build/bazel has several static config_setting(s) that are used in Bazel builds.
260 // This function can be used to createa additional config_setting(s) based on the build graph
261 // (e.g. a config_setting specific to an apex variant)
262 CreateBazelConfigSetting(csa bazel.ConfigSettingAttributes, ca CommonAttributes, dir string)
Colin Cross6362e272015-10-29 15:25:03 -0700263}
264
Chris Parsons637458d2023-09-19 20:09:00 +0000265// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
266// into Bazel BUILD targets that should run prior to deps and conversion.
267func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
268 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
269}
270
271type BaseMutatorContext interface {
272 BaseModuleContext
273
274 // MutatorName returns the name that this mutator was registered with.
275 MutatorName() string
276
277 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
278 // AddDependency or OtherModuleName until after this mutator pass is complete.
279 Rename(name string)
280}
281
282type TopDownMutator func(TopDownMutatorContext)
283
284type TopDownMutatorContext interface {
285 BaseMutatorContext
286 Bp2buildMutatorContext
287
288 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
289 // the specified property structs to it as if the properties were set in a blueprint file.
290 CreateModule(ModuleFactory, ...interface{}) Module
291}
292
Colin Cross25de6c32019-06-06 14:29:25 -0700293type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700294 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700295 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700296}
297
Colin Cross25de6c32019-06-06 14:29:25 -0700298type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700299
Colin Cross635c3b02016-05-18 15:37:25 -0700300type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700301 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800302
Colin Cross4f1dcb02020-09-16 18:45:04 -0700303 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
304 // dependency (some entries may be nil).
305 //
306 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
307 // new dependencies have had the current mutator called on them. If the mutator is not
308 // parallel this method does not affect the ordering of the current mutator pass, but will
309 // be ordered correctly for all future mutator passes.
310 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700311
312 // AddReverseDependency adds a dependency from the destination to the given module.
313 // Does not affect the ordering of the current mutator pass, but will be ordered
314 // correctly for all future mutator passes. All reverse dependencies for a destination module are
315 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
316 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800317 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700318
319 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
320 // parameter. It returns a list of new modules in the same order as the variationNames
321 // list.
322 //
323 // If any of the dependencies of the module being operated on were already split
324 // by calling CreateVariations with the same name, the dependency will automatically
325 // be updated to point the matching variant.
326 //
327 // If a module is split, and then a module depending on the first module is not split
328 // when the Mutator is later called on it, the dependency of the depending module will
329 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800330 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700331
332 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
333 // parameter. It returns a list of new modules in the same order as the variantNames
334 // list.
335 //
336 // Local variations do not affect automatic dependency resolution - dependencies added
337 // to the split module via deps or DynamicDependerModule must exactly match a variant
338 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800339 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700340
341 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
342 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800343 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700344
345 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
346 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900347 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700348
349 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700350 // argument to select which variant of the dependency to use. It returns a slice of modules for
351 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
Usta Shresthac725f472022-01-11 02:44:21 -0500352 // all the non-local variations of the current module, plus the variations argument.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700353 //
354 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
355 // new dependencies have had the current mutator called on them. If the mutator is not
356 // parallel this method does not affect the ordering of the current mutator pass, but will
357 // be ordered correctly for all future mutator passes.
Usta Shresthac725f472022-01-11 02:44:21 -0500358 AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700359
360 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700361 // variations argument to select which variant of the dependency to use. It returns a slice of
362 // modules for each dependency (some entries may be nil). A variant of the dependency must
363 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700364 // For any unspecified variation the first variant will be used.
365 //
366 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
367 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700368 //
369 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
370 // new dependencies have had the current mutator called on them. If the mutator is not
371 // parallel this method does not affect the ordering of the current mutator pass, but will
372 // be ordered correctly for all future mutator passes.
373 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700374
375 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
376 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
377 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
378 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800379 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700380
381 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
382 // specified name with the current variant of this module. Replacements don't take effect until
383 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800384 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700385
386 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
387 // specified name with the current variant of this module as long as the supplied predicate returns
388 // true.
389 //
390 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100391 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700392
393 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
394 // and creates an alias from the current variant (before the mutator has run) to the new
395 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
396 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
397 // be used to add dependencies on the newly created variant using the variant map from
398 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800399 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700400
401 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
402 // module, and creates an alias from a new fromVariationName variant the toVariationName
403 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
404 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
405 // be used to add dependencies on the toVariationName variant using the fromVariationName
406 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700407 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700408
409 // SetVariationProvider sets the value for a provider for the given newly created variant of
410 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
411 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
412 // if the value is not of the appropriate type, or if the module is not a newly created
413 // variant of the current module. The value should not be modified after being passed to
414 // SetVariationProvider.
415 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700416}
417
Colin Cross25de6c32019-06-06 14:29:25 -0700418type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700419 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700420 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400421 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700422}
423
Colin Cross617b88a2020-08-24 18:04:09 -0700424func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500425 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700426
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400427 moduleContext := a.base().baseModuleContextFactory(ctx)
428 moduleContext.bazelConversionMode = bazelConversionMode
429
Colin Cross617b88a2020-08-24 18:04:09 -0700430 return &bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400431 bp: ctx,
Cole Faust0abd4b42023-01-10 10:49:18 -0800432 baseModuleContext: moduleContext,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400433 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700434 }
435}
436
Colin Cross25de6c32019-06-06 14:29:25 -0700437func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000438 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500439 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700440 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700441 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500442 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700443 }
Colin Cross798bfce2016-10-12 14:28:16 -0700444 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500445 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700446 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700447 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700448}
449
Colin Cross617b88a2020-08-24 18:04:09 -0700450func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
451 mutator := &mutator{name: name, bottomUpMutator: m}
452 x.mutators = append(x.mutators, mutator)
453 return mutator
454}
455
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200456type IncomingTransitionContext interface {
457 // Module returns the target of the dependency edge for which the transition
458 // is being computed
459 Module() Module
460
461 // Config returns the configuration for the build.
462 Config() Config
463}
464
465type OutgoingTransitionContext interface {
466 // Module returns the target of the dependency edge for which the transition
467 // is being computed
468 Module() Module
469
470 // DepTag() Returns the dependency tag through which this dependency is
471 // reached
472 DepTag() blueprint.DependencyTag
473}
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200474
475// Transition mutators implement a top-down mechanism where a module tells its
476// direct dependencies what variation they should be built in but the dependency
477// has the final say.
478//
479// When implementing a transition mutator, one needs to implement four methods:
480// - Split() that tells what variations a module has by itself
481// - OutgoingTransition() where a module tells what it wants from its
482// dependency
483// - IncomingTransition() where a module has the final say about its own
484// variation
485// - Mutate() that changes the state of a module depending on its variation
486//
487// That the effective variation of module B when depended on by module A is the
488// composition the outgoing transition of module A and the incoming transition
489// of module B.
490//
491// the outgoing transition should not take the properties of the dependency into
492// account, only those of the module that depends on it. For this reason, the
493// dependency is not even passed into it as an argument. Likewise, the incoming
494// transition should not take the properties of the depending module into
495// account and is thus not informed about it. This makes for a nice
496// decomposition of the decision logic.
497//
498// A given transition mutator only affects its own variation; other variations
499// stay unchanged along the dependency edges.
500//
501// Soong makes sure that all modules are created in the desired variations and
502// that dependency edges are set up correctly. This ensures that "missing
503// variation" errors do not happen and allows for more flexible changes in the
504// value of the variation among dependency edges (as oppposed to bottom-up
505// mutators where if module A in variation X depends on module B and module B
506// has that variation X, A must depend on variation X of B)
507//
508// The limited power of the context objects passed to individual mutators
509// methods also makes it more difficult to shoot oneself in the foot. Complete
510// safety is not guaranteed because no one prevents individual transition
511// mutators from mutating modules in illegal ways and for e.g. Split() or
512// Mutate() to run their own visitations of the transitive dependency of the
513// module and both of these are bad ideas, but it's better than no guardrails at
514// all.
515//
516// This model is pretty close to Bazel's configuration transitions. The mapping
517// between concepts in Soong and Bazel is as follows:
518// - Module == configured target
519// - Variant == configuration
520// - Variation name == configuration flag
521// - Variation == configuration flag value
522// - Outgoing transition == attribute transition
523// - Incoming transition == rule transition
524//
525// The Split() method does not have a Bazel equivalent and Bazel split
526// transitions do not have a Soong equivalent.
527//
528// Mutate() does not make sense in Bazel due to the different models of the
529// two systems: when creating new variations, Soong clones the old module and
530// thus some way is needed to change it state whereas Bazel creates each
531// configuration of a given configured target anew.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200532type TransitionMutator interface {
533 // Split returns the set of variations that should be created for a module no
534 // matter who depends on it. Used when Make depends on a particular variation
535 // or when the module knows its variations just based on information given to
536 // it in the Blueprint file. This method should not mutate the module it is
537 // called on.
538 Split(ctx BaseModuleContext) []string
539
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200540 // Called on a module to determine which variation it wants from its direct
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200541 // dependencies. The dependency itself can override this decision. This method
542 // should not mutate the module itself.
543 OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string
544
545 // Called on a module to determine which variation it should be in based on
546 // the variation modules that depend on it want. This gives the module a final
547 // say about its own variations. This method should not mutate the module
548 // itself.
549 IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string
550
551 // Called after a module was split into multiple variations on each variation.
552 // It should not split the module any further but adding new dependencies is
553 // fine. Unlike all the other methods on TransitionMutator, this method is
554 // allowed to mutate the module.
555 Mutate(ctx BottomUpMutatorContext, variation string)
556}
557
558type androidTransitionMutator struct {
559 finalPhase bool
560 bazelConversionMode bool
561 mutator TransitionMutator
562}
563
564func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []string {
565 if m, ok := ctx.Module().(Module); ok {
566 moduleContext := m.base().baseModuleContextFactory(ctx)
567 moduleContext.bazelConversionMode = a.bazelConversionMode
568 return a.mutator.Split(&moduleContext)
569 } else {
570 return []string{""}
571 }
572}
573
574type outgoingTransitionContextImpl struct {
575 bp blueprint.OutgoingTransitionContext
576}
577
578func (c *outgoingTransitionContextImpl) Module() Module {
579 return c.bp.Module().(Module)
580}
581
582func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag {
583 return c.bp.DepTag()
584}
585
586func (a *androidTransitionMutator) OutgoingTransition(ctx blueprint.OutgoingTransitionContext, sourceVariation string) string {
587 if _, ok := ctx.Module().(Module); ok {
588 return a.mutator.OutgoingTransition(&outgoingTransitionContextImpl{bp: ctx}, sourceVariation)
589 } else {
590 return ""
591 }
592}
593
594type incomingTransitionContextImpl struct {
595 bp blueprint.IncomingTransitionContext
596}
597
598func (c *incomingTransitionContextImpl) Module() Module {
599 return c.bp.Module().(Module)
600}
601
602func (c *incomingTransitionContextImpl) Config() Config {
603 return c.bp.Config().(Config)
604}
605
606func (a *androidTransitionMutator) IncomingTransition(ctx blueprint.IncomingTransitionContext, incomingVariation string) string {
607 if _, ok := ctx.Module().(Module); ok {
608 return a.mutator.IncomingTransition(&incomingTransitionContextImpl{bp: ctx}, incomingVariation)
609 } else {
610 return ""
611 }
612}
613
614func (a *androidTransitionMutator) Mutate(ctx blueprint.BottomUpMutatorContext, variation string) {
615 if am, ok := ctx.Module().(Module); ok {
616 a.mutator.Mutate(bottomUpMutatorContextFactory(ctx, am, a.finalPhase, a.bazelConversionMode), variation)
617 }
618}
619
620func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) {
621 atm := &androidTransitionMutator{
622 finalPhase: x.finalPhase,
623 bazelConversionMode: x.bazelConversionMode,
624 mutator: m,
625 }
626 mutator := &mutator{
627 name: name,
628 transitionMutator: atm}
629 x.mutators = append(x.mutators, mutator)
630}
631
Liz Kammer356f7d42021-01-26 09:18:53 -0500632func (x *registerMutatorsContext) mutatorName(name string) string {
633 if x.bazelConversionMode {
634 return name + "_bp2build"
635 }
636 return name
637}
638
Colin Cross25de6c32019-06-06 14:29:25 -0700639func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700640 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700641 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400642 moduleContext := a.base().baseModuleContextFactory(ctx)
643 moduleContext.bazelConversionMode = x.bazelConversionMode
Colin Cross25de6c32019-06-06 14:29:25 -0700644 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700645 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400646 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700647 }
Colin Cross798bfce2016-10-12 14:28:16 -0700648 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700649 }
Colin Cross798bfce2016-10-12 14:28:16 -0700650 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500651 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700652 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700653 return mutator
654}
655
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000656func (mutator *mutator) componentName() string {
657 return mutator.name
658}
659
660func (mutator *mutator) register(ctx *Context) {
661 blueprintCtx := ctx.Context
662 var handle blueprint.MutatorHandle
663 if mutator.bottomUpMutator != nil {
664 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
665 } else if mutator.topDownMutator != nil {
666 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200667 } else if mutator.transitionMutator != nil {
668 blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000669 }
670 if mutator.parallel {
671 handle.Parallel()
672 }
673}
674
Colin Cross798bfce2016-10-12 14:28:16 -0700675type MutatorHandle interface {
676 Parallel() MutatorHandle
677}
678
679func (mutator *mutator) Parallel() MutatorHandle {
680 mutator.parallel = true
681 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700682}
Colin Cross1e676be2016-10-12 14:38:15 -0700683
Paul Duffin44f1d842020-06-26 20:17:02 +0100684func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
685 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
686}
687
688// A special mutator that runs just prior to the deps mutator to allow the dependencies
689// on component modules to be added so that they can depend directly on a prebuilt
690// module.
691func componentDepsMutator(ctx BottomUpMutatorContext) {
692 if m := ctx.Module(); m.Enabled() {
693 m.ComponentDepsMutator(ctx)
694 }
695}
696
Colin Cross1e676be2016-10-12 14:38:15 -0700697func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100698 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700699 m.DepsMutator(ctx)
700 }
701}
Colin Crossd11fcda2017-10-23 17:59:01 -0700702
Liz Kammer356f7d42021-01-26 09:18:53 -0500703func registerDepsMutator(ctx RegisterMutatorsContext) {
704 ctx.BottomUp("deps", depsMutator).Parallel()
705}
706
707func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
708 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
709 // being converted to build targets.
710 ctx.BottomUp("deps", depsMutator).Parallel()
711}
712
Jingwen Chen1fd14692021-02-05 03:01:50 -0500713func (t *topDownMutatorContext) CreateBazelTargetModule(
Jingwen Chen1fd14692021-02-05 03:01:50 -0500714 bazelProps bazel.BazelTargetModuleProperties,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000715 commonAttrs CommonAttributes,
Liz Kammer2ada09a2021-08-11 00:17:36 -0400716 attrs interface{}) {
Chris Parsons58852a02021-12-09 18:10:18 -0500717 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, bazel.BoolAttribute{})
718}
719
720func (t *topDownMutatorContext) CreateBazelTargetModuleWithRestrictions(
721 bazelProps bazel.BazelTargetModuleProperties,
722 commonAttrs CommonAttributes,
723 attrs interface{},
724 enabledProperty bazel.BoolAttribute) {
725 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, enabledProperty)
726}
727
Chris Parsons39a16972023-06-08 14:28:51 +0000728func (t *topDownMutatorContext) MarkBp2buildUnconvertible(
729 reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string) {
730 mod := t.Module()
731 mod.base().setBp2buildUnconvertible(reasonType, detail)
732}
733
Spandan Dasabedff02023-03-07 19:24:34 +0000734var (
735 bazelAliasModuleProperties = bazel.BazelTargetModuleProperties{
736 Rule_class: "alias",
737 }
738)
739
740type bazelAliasAttributes struct {
741 Actual *bazel.LabelAttribute
742}
743
744func (t *topDownMutatorContext) CreateBazelTargetAliasInDir(
745 dir string,
746 name string,
747 actual bazel.Label) {
748 mod := t.Module()
749 attrs := &bazelAliasAttributes{
750 Actual: bazel.MakeLabelAttribute(actual.Label),
751 }
752 info := bp2buildInfo{
753 Dir: dir,
754 BazelProps: bazelAliasModuleProperties,
755 CommonAttrs: CommonAttributes{Name: name},
756 ConstraintAttrs: constraintAttributes{},
757 Attrs: attrs,
758 }
759 mod.base().addBp2buildInfo(info)
760}
761
Spandan Das3131d672023-08-03 22:33:47 +0000762// Returns the directory in which the bazel target will be generated
763// If ca.Dir is not nil, use that
764// Otherwise default to the directory of the soong module
765func dirForBazelTargetGeneration(t *topDownMutatorContext, ca *CommonAttributes) string {
766 dir := t.OtherModuleDir(t.Module())
767 if ca.Dir != nil {
768 dir = *ca.Dir
769 // Restrict its use to dirs that contain an Android.bp file.
770 // There are several places in bp2build where we use the existence of Android.bp/BUILD on the filesystem
771 // to curate a compatible label for src files (e.g. headers for cc).
772 // If we arbritrarily create BUILD files, then it might render those curated labels incompatible.
773 if exists, _, _ := t.Config().fs.Exists(filepath.Join(dir, "Android.bp")); !exists {
774 t.ModuleErrorf("Cannot use ca.Dir to create a BazelTarget in dir: %v since it does not contain an Android.bp file", dir)
775 }
776
777 // Set ca.Dir to nil so that it does not get emitted to the BUILD files
778 ca.Dir = nil
779 }
780 return dir
781}
782
Spandan Das6a448ec2023-04-19 17:36:12 +0000783func (t *topDownMutatorContext) CreateBazelConfigSetting(
784 csa bazel.ConfigSettingAttributes,
785 ca CommonAttributes,
786 dir string) {
787 mod := t.Module()
788 info := bp2buildInfo{
789 Dir: dir,
790 BazelProps: bazel.BazelTargetModuleProperties{
791 Rule_class: "config_setting",
792 },
793 CommonAttrs: ca,
794 ConstraintAttrs: constraintAttributes{},
795 Attrs: &csa,
796 }
797 mod.base().addBp2buildInfo(info)
798}
799
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000800// ApexAvailableTags converts the apex_available property value of an ApexModule
801// module and returns it as a list of keyed tags.
802func ApexAvailableTags(mod Module) bazel.StringListAttribute {
803 attr := bazel.StringListAttribute{}
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000804 // Transform specific attributes into tags.
805 if am, ok := mod.(ApexModule); ok {
806 // TODO(b/218841706): hidl_interface has the apex_available prop, but it's
807 // defined directly as a prop and not via ApexModule, so this doesn't
808 // pick those props up.
Spandan Das2dc6dfc2023-04-17 19:16:06 +0000809 apexAvailable := am.apexModuleBase().ApexAvailable()
810 // If a user does not specify apex_available in Android.bp, then soong provides a default.
811 // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
812 if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
813 apexAvailable = []string{}
814 }
815 attr.Value = ConvertApexAvailableToTags(apexAvailable)
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000816 }
817 return attr
818}
819
Spandan Dasf57a9662023-04-12 19:05:49 +0000820func ApexAvailableTagsWithoutTestApexes(ctx BaseModuleContext, mod Module) bazel.StringListAttribute {
821 attr := bazel.StringListAttribute{}
822 if am, ok := mod.(ApexModule); ok {
823 apexAvailableWithoutTestApexes := removeTestApexes(ctx, am.apexModuleBase().ApexAvailable())
824 // If a user does not specify apex_available in Android.bp, then soong provides a default.
825 // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
826 if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
827 apexAvailableWithoutTestApexes = []string{}
828 }
829 attr.Value = ConvertApexAvailableToTags(apexAvailableWithoutTestApexes)
830 }
831 return attr
832}
833
834func removeTestApexes(ctx BaseModuleContext, apex_available []string) []string {
835 testApexes := []string{}
836 for _, aa := range apex_available {
837 // ignore the wildcards
838 if InList(aa, AvailableToRecognziedWildcards) {
839 continue
840 }
841 mod, _ := ctx.ModuleFromName(aa)
842 if apex, ok := mod.(ApexTestInterface); ok && apex.IsTestApex() {
843 testApexes = append(testApexes, aa)
844 }
845 }
846 return RemoveListFromList(CopyOf(apex_available), testApexes)
847}
848
Cole Faustfb11c1c2023-02-10 11:27:46 -0800849func ConvertApexAvailableToTags(apexAvailable []string) []string {
850 if len(apexAvailable) == 0 {
851 // We need nil specifically to make bp2build not add the tags property at all,
852 // instead of adding it with an empty list
853 return nil
854 }
855 result := make([]string, 0, len(apexAvailable))
856 for _, a := range apexAvailable {
857 result = append(result, "apex_available="+a)
858 }
859 return result
860}
861
Spandan Das39b6cc52023-04-12 19:05:49 +0000862// ConvertApexAvailableToTagsWithoutTestApexes converts a list of apex names to a list of bazel tags
863// This function drops any test apexes from the input.
864func ConvertApexAvailableToTagsWithoutTestApexes(ctx BaseModuleContext, apexAvailable []string) []string {
865 noTestApexes := removeTestApexes(ctx, apexAvailable)
866 return ConvertApexAvailableToTags(noTestApexes)
867}
868
Chris Parsons58852a02021-12-09 18:10:18 -0500869func (t *topDownMutatorContext) createBazelTargetModule(
870 bazelProps bazel.BazelTargetModuleProperties,
871 commonAttrs CommonAttributes,
872 attrs interface{},
873 enabledProperty bazel.BoolAttribute) {
874 constraintAttributes := commonAttrs.fillCommonBp2BuildModuleAttrs(t, enabledProperty)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000875 mod := t.Module()
Liz Kammer2ada09a2021-08-11 00:17:36 -0400876 info := bp2buildInfo{
Spandan Das3131d672023-08-03 22:33:47 +0000877 Dir: dirForBazelTargetGeneration(t, &commonAttrs),
Chris Parsons58852a02021-12-09 18:10:18 -0500878 BazelProps: bazelProps,
879 CommonAttrs: commonAttrs,
880 ConstraintAttrs: constraintAttributes,
881 Attrs: attrs,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500882 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000883 mod.base().addBp2buildInfo(info)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500884}
885
Colin Crossdc35e212019-06-06 16:13:11 -0700886// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
887// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
888// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
889// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
890// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
891
Colin Crosscb55e082019-07-01 15:32:31 -0700892func (t *topDownMutatorContext) MutatorName() string {
893 return t.bp.MutatorName()
894}
895
Colin Crossdc35e212019-06-06 16:13:11 -0700896func (t *topDownMutatorContext) Rename(name string) {
897 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700898 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700899}
900
Liz Kammerf31c9002022-04-26 09:08:55 -0400901func (t *topDownMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
902 return t.bp.CreateModule(factory, name, props...)
903}
904
Colin Crosse003c4a2019-09-25 12:58:36 -0700905func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400906 return createModule(t, factory, "_topDownMutatorModule", props...)
Colin Crossdc35e212019-06-06 16:13:11 -0700907}
908
Liz Kammera060c452021-03-24 10:14:47 -0400909func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400910 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), "", props...).(Module)
Liz Kammera060c452021-03-24 10:14:47 -0400911 return module
912}
913
Colin Crosscb55e082019-07-01 15:32:31 -0700914func (b *bottomUpMutatorContext) MutatorName() string {
915 return b.bp.MutatorName()
916}
917
Colin Crossdc35e212019-06-06 16:13:11 -0700918func (b *bottomUpMutatorContext) Rename(name string) {
919 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700920 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700921}
922
Colin Cross4f1dcb02020-09-16 18:45:04 -0700923func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400924 if b.baseModuleContext.checkedMissingDeps() {
925 panic("Adding deps not allowed after checking for missing deps")
926 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700927 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700928}
929
930func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400931 if b.baseModuleContext.checkedMissingDeps() {
932 panic("Adding deps not allowed after checking for missing deps")
933 }
Colin Crossdc35e212019-06-06 16:13:11 -0700934 b.bp.AddReverseDependency(module, tag, name)
935}
936
Colin Cross43b92e02019-11-18 15:28:57 -0800937func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000938 if b.finalPhase {
939 panic("CreateVariations not allowed in FinalDepsMutators")
940 }
941
Colin Cross9a362232019-07-01 15:32:45 -0700942 modules := b.bp.CreateVariations(variations...)
943
Colin Cross43b92e02019-11-18 15:28:57 -0800944 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700945 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800946 aModules[i] = modules[i].(Module)
947 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700948 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
949 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
950 }
951
Colin Cross43b92e02019-11-18 15:28:57 -0800952 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700953}
954
Colin Cross43b92e02019-11-18 15:28:57 -0800955func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000956 if b.finalPhase {
957 panic("CreateLocalVariations not allowed in FinalDepsMutators")
958 }
959
Colin Cross9a362232019-07-01 15:32:45 -0700960 modules := b.bp.CreateLocalVariations(variations...)
961
Colin Cross43b92e02019-11-18 15:28:57 -0800962 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700963 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800964 aModules[i] = modules[i].(Module)
965 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700966 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
967 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
968 }
969
Colin Cross43b92e02019-11-18 15:28:57 -0800970 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700971}
972
973func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
974 b.bp.SetDependencyVariation(variation)
975}
976
Jiyong Park1d1119f2019-07-29 21:27:18 +0900977func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
978 b.bp.SetDefaultDependencyVariation(variation)
979}
980
Colin Crossdc35e212019-06-06 16:13:11 -0700981func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700982 names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400983 if b.baseModuleContext.checkedMissingDeps() {
984 panic("Adding deps not allowed after checking for missing deps")
985 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700986 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700987}
988
989func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700990 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400991 if b.baseModuleContext.checkedMissingDeps() {
992 panic("Adding deps not allowed after checking for missing deps")
993 }
Colin Crossdc35e212019-06-06 16:13:11 -0700994
Colin Cross4f1dcb02020-09-16 18:45:04 -0700995 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700996}
997
998func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
999 b.bp.AddInterVariantDependency(tag, from, to)
1000}
1001
1002func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -04001003 if b.baseModuleContext.checkedMissingDeps() {
1004 panic("Adding deps not allowed after checking for missing deps")
1005 }
Colin Crossdc35e212019-06-06 16:13:11 -07001006 b.bp.ReplaceDependencies(name)
1007}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -08001008
Paul Duffin80342d72020-06-26 22:08:43 +01001009func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
Liz Kammerc13f7852023-05-17 13:01:48 -04001010 if b.baseModuleContext.checkedMissingDeps() {
1011 panic("Adding deps not allowed after checking for missing deps")
1012 }
Paul Duffin80342d72020-06-26 22:08:43 +01001013 b.bp.ReplaceDependenciesIf(name, predicate)
1014}
1015
Jaewoong Jung9f88ce22019-11-15 10:57:34 -08001016func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
1017 b.bp.AliasVariation(variationName)
1018}
Colin Cross1b9604b2020-08-11 12:03:56 -07001019
1020func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
1021 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
1022}
Colin Crossd27e7b82020-07-02 11:38:17 -07001023
1024func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
1025 b.bp.SetVariationProvider(module, provider, value)
1026}