blob: 336f8f73ccfb2ba8e1c21c54c42e01264b7c3aad [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,
Liz Kammere1b39a52023-09-18 14:32:18 -040059 RegisterPrebuiltsPostDepsMutators,
Liz Kammer356f7d42021-01-26 09:18:53 -050060 },
Spandan Das5af0bd32022-09-28 20:43:08 +000061 bp2buildMutators...)
Liz Kammer356f7d42021-01-26 09:18:53 -050062
Jingwen Chen73850672020-12-14 08:25:34 -050063 // Register bp2build mutators
Spandan Das5af0bd32022-09-28 20:43:08 +000064 for _, f := range allMutators {
Jingwen Chen73850672020-12-14 08:25:34 -050065 f(mctx)
66 }
67
Paul Duffin1d2d42f2021-03-06 20:08:12 +000068 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050069}
70
Paul Duffinc05b0342021-03-06 13:28:13 +000071// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
72// with the InitRegistrationContext and will be used at runtime.
73func collateGloballyRegisteredMutators() sortableComponents {
Liz Kammerc13f7852023-05-17 13:01:48 -040074 // ensure mixed builds mutator is the last mutator
75 finalDeps = append(finalDeps, registerMixedBuildsMutator)
Paul Duffinc05b0342021-03-06 13:28:13 +000076 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
77}
78
79// collateRegisteredMutators constructs a single list of mutators from the separate lists.
80func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070081 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080082
83 register := func(funcs []RegisterMutatorFunc) {
84 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070085 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080086 }
87 }
88
Colin Crosscec81712017-07-13 14:43:27 -070089 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080090
Colin Crosscec81712017-07-13 14:43:27 -070091 register(preDeps)
92
Liz Kammer356f7d42021-01-26 09:18:53 -050093 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070094
95 register(postDeps)
96
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000097 mctx.finalPhase = true
98 register(finalDeps)
99
Paul Duffinc05b0342021-03-06 13:28:13 +0000100 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -0700101}
102
103type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000104 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -0500105 finalPhase bool
106 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -0700107}
Colin Cross1e676be2016-10-12 14:38:15 -0700108
109type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -0700110 TopDown(name string, m TopDownMutator) MutatorHandle
111 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -0700112 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200113 Transition(name string, m TransitionMutator)
Colin Cross1e676be2016-10-12 14:38:15 -0700114}
115
116type RegisterMutatorFunc func(RegisterMutatorsContext)
117
Colin Crosscec81712017-07-13 14:43:27 -0700118var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800119 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100120
Paul Duffinaa4162e2020-05-05 11:35:43 +0100121 // Check the visibility rules are valid.
122 //
123 // This must run after the package renamer mutators so that any issues found during
124 // validation of the package's default_visibility property are reported using the
125 // correct package name and not the synthetic name.
126 //
127 // This must also be run before defaults mutators as the rules for validation are
128 // different before checking the rules than they are afterwards. e.g.
129 // visibility: ["//visibility:private", "//visibility:public"]
130 // would be invalid if specified in a module definition but is valid if it results
131 // from something like this:
132 //
133 // defaults {
134 // name: "defaults",
135 // // Be inaccessible outside a package by default.
136 // visibility: ["//visibility:private"]
137 // }
138 //
139 // defaultable_module {
140 // name: "defaultable_module",
141 // defaults: ["defaults"],
142 // // Override the default.
143 // visibility: ["//visibility:public"]
144 // }
145 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000146 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100147
Bob Badour37af0462021-01-07 03:34:31 +0000148 // Record the default_applicable_licenses for each package.
149 //
150 // This must run before the defaults so that defaults modules can pick up the package default.
151 RegisterLicensesPackageMapper,
152
Paul Duffinaa4162e2020-05-05 11:35:43 +0100153 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100154 //
155 // Any mutators that are added before this will not see any modules created by
156 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700157 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100158
Paul Duffin44f1d842020-06-26 20:17:02 +0100159 // Add dependencies on any components so that any component references can be
160 // resolved within the deps mutator.
161 //
162 // Must be run after defaults so it can be used to create dependencies on the
163 // component modules that are creating in a DefaultableHook.
164 //
165 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
166 // renamed. That is so that if a module creates components using a prebuilt module
167 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
168 // the prebuilt module and not the source module.
169 RegisterComponentsMutator,
170
Paul Duffinc988c8e2020-04-29 18:27:14 +0100171 // Create an association between prebuilt modules and their corresponding source
172 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100173 //
174 // Must be run after defaults mutators to ensure that any modules created by
175 // a DefaultableHook can be either a prebuilt or a source module with a matching
176 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100177 RegisterPrebuiltsPreArchMutators,
178
Bob Badour37af0462021-01-07 03:34:31 +0000179 // Gather the licenses properties for all modules for use during expansion and enforcement.
180 //
181 // This must come after the defaults mutators to ensure that any licenses supplied
182 // in a defaults module has been successfully applied before the rules are gathered.
183 RegisterLicensesPropertyGatherer,
184
Paul Duffinaa4162e2020-05-05 11:35:43 +0100185 // Gather the visibility rules for all modules for us during visibility enforcement.
186 //
187 // This must come after the defaults mutators to ensure that any visibility supplied
188 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000189 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700190}
191
Colin Crossae4c6182017-09-15 17:33:55 -0700192func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700193 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800194 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700195 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700196}
197
Colin Crosscec81712017-07-13 14:43:27 -0700198var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700199 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700200}
201
202var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800203 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700204 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000205 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000206 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100207 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700208 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700209}
Colin Cross1e676be2016-10-12 14:38:15 -0700210
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000211var finalDeps = []RegisterMutatorFunc{}
212
Colin Cross1e676be2016-10-12 14:38:15 -0700213func PreArchMutators(f RegisterMutatorFunc) {
214 preArch = append(preArch, f)
215}
216
217func PreDepsMutators(f RegisterMutatorFunc) {
218 preDeps = append(preDeps, f)
219}
220
221func PostDepsMutators(f RegisterMutatorFunc) {
222 postDeps = append(postDeps, f)
223}
224
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000225func FinalDepsMutators(f RegisterMutatorFunc) {
226 finalDeps = append(finalDeps, f)
227}
228
Liz Kammer356f7d42021-01-26 09:18:53 -0500229var bp2buildPreArchMutators = []RegisterMutatorFunc{}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400230
Liz Kammer12615db2021-09-28 09:19:17 -0400231// A minimal context for Bp2build conversion
232type Bp2buildMutatorContext interface {
233 BazelConversionPathContext
Colin Cross9f35c3d2020-09-16 19:04:41 -0700234 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700235
Jingwen Chen1fd14692021-02-05 03:01:50 -0500236 // CreateBazelTargetModule creates a BazelTargetModule by calling the
237 // factory method, just like in CreateModule, but also requires
238 // BazelTargetModuleProperties containing additional metadata for the
239 // bp2build codegenerator.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000240 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
Chris Parsons58852a02021-12-09 18:10:18 -0500241
242 // CreateBazelTargetModuleWithRestrictions creates a BazelTargetModule by calling the
243 // factory method, just like in CreateModule, but also requires
244 // BazelTargetModuleProperties containing additional metadata for the
245 // bp2build codegenerator. The generated target is restricted to only be buildable for certain
246 // platforms, as dictated by a given bool attribute: the target will not be buildable in
247 // any platform for which this bool attribute is false.
248 CreateBazelTargetModuleWithRestrictions(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}, bazel.BoolAttribute)
Spandan Dasabedff02023-03-07 19:24:34 +0000249
Chris Parsons39a16972023-06-08 14:28:51 +0000250 // MarkBp2buildUnconvertible registers the current module as "unconvertible to bp2build" for the
251 // given reason.
252 MarkBp2buildUnconvertible(reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string)
253
Spandan Dasabedff02023-03-07 19:24:34 +0000254 // CreateBazelTargetAliasInDir creates an alias definition in `dir` directory.
255 // This function can be used to create alias definitions in a directory that is different
256 // from the directory of the visited Soong module.
257 CreateBazelTargetAliasInDir(dir string, name string, actual bazel.Label)
Spandan Das6a448ec2023-04-19 17:36:12 +0000258
259 // CreateBazelConfigSetting creates a config_setting in <dir>/BUILD.bazel
260 // build/bazel has several static config_setting(s) that are used in Bazel builds.
261 // This function can be used to createa additional config_setting(s) based on the build graph
262 // (e.g. a config_setting specific to an apex variant)
263 CreateBazelConfigSetting(csa bazel.ConfigSettingAttributes, ca CommonAttributes, dir string)
Colin Cross6362e272015-10-29 15:25:03 -0700264}
265
Chris Parsons637458d2023-09-19 20:09:00 +0000266// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
267// into Bazel BUILD targets that should run prior to deps and conversion.
268func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
269 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
270}
271
272type BaseMutatorContext interface {
273 BaseModuleContext
274
275 // MutatorName returns the name that this mutator was registered with.
276 MutatorName() string
277
278 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
279 // AddDependency or OtherModuleName until after this mutator pass is complete.
280 Rename(name string)
281}
282
283type TopDownMutator func(TopDownMutatorContext)
284
285type TopDownMutatorContext interface {
286 BaseMutatorContext
287 Bp2buildMutatorContext
288
289 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
290 // the specified property structs to it as if the properties were set in a blueprint file.
291 CreateModule(ModuleFactory, ...interface{}) Module
292}
293
Colin Cross25de6c32019-06-06 14:29:25 -0700294type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700295 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700296 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700297}
298
Colin Cross25de6c32019-06-06 14:29:25 -0700299type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700300
Colin Cross635c3b02016-05-18 15:37:25 -0700301type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700302 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800303
Colin Cross4f1dcb02020-09-16 18:45:04 -0700304 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
305 // dependency (some entries may be nil).
306 //
307 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
308 // new dependencies have had the current mutator called on them. If the mutator is not
309 // parallel this method does not affect the ordering of the current mutator pass, but will
310 // be ordered correctly for all future mutator passes.
311 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700312
313 // AddReverseDependency adds a dependency from the destination to the given module.
314 // Does not affect the ordering of the current mutator pass, but will be ordered
315 // correctly for all future mutator passes. All reverse dependencies for a destination module are
316 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
317 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800318 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700319
320 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
321 // parameter. It returns a list of new modules in the same order as the variationNames
322 // list.
323 //
324 // If any of the dependencies of the module being operated on were already split
325 // by calling CreateVariations with the same name, the dependency will automatically
326 // be updated to point the matching variant.
327 //
328 // If a module is split, and then a module depending on the first module is not split
329 // when the Mutator is later called on it, the dependency of the depending module will
330 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800331 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700332
333 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
334 // parameter. It returns a list of new modules in the same order as the variantNames
335 // list.
336 //
337 // Local variations do not affect automatic dependency resolution - dependencies added
338 // to the split module via deps or DynamicDependerModule must exactly match a variant
339 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800340 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700341
342 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
343 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800344 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700345
346 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
347 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900348 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700349
350 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700351 // argument to select which variant of the dependency to use. It returns a slice of modules for
352 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
Usta Shresthac725f472022-01-11 02:44:21 -0500353 // all the non-local variations of the current module, plus the variations argument.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700354 //
355 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
356 // new dependencies have had the current mutator called on them. If the mutator is not
357 // parallel this method does not affect the ordering of the current mutator pass, but will
358 // be ordered correctly for all future mutator passes.
Usta Shresthac725f472022-01-11 02:44:21 -0500359 AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700360
361 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700362 // variations argument to select which variant of the dependency to use. It returns a slice of
363 // modules for each dependency (some entries may be nil). A variant of the dependency must
364 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700365 // For any unspecified variation the first variant will be used.
366 //
367 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
368 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700369 //
370 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
371 // new dependencies have had the current mutator called on them. If the mutator is not
372 // parallel this method does not affect the ordering of the current mutator pass, but will
373 // be ordered correctly for all future mutator passes.
374 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700375
376 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
377 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
378 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
379 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800380 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700381
382 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
383 // specified name with the current variant of this module. Replacements don't take effect until
384 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800385 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700386
387 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
388 // specified name with the current variant of this module as long as the supplied predicate returns
389 // true.
390 //
391 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100392 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700393
394 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
395 // and creates an alias from the current variant (before the mutator has run) to the new
396 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
397 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
398 // be used to add dependencies on the newly created variant using the variant map from
399 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800400 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700401
402 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
403 // module, and creates an alias from a new fromVariationName variant the toVariationName
404 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
405 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
406 // be used to add dependencies on the toVariationName variant using the fromVariationName
407 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700408 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700409
410 // SetVariationProvider sets the value for a provider for the given newly created variant of
411 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
412 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
413 // if the value is not of the appropriate type, or if the module is not a newly created
414 // variant of the current module. The value should not be modified after being passed to
415 // SetVariationProvider.
416 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700417}
418
Colin Cross25de6c32019-06-06 14:29:25 -0700419type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700420 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700421 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400422 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700423}
424
Colin Cross617b88a2020-08-24 18:04:09 -0700425func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500426 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700427
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400428 moduleContext := a.base().baseModuleContextFactory(ctx)
429 moduleContext.bazelConversionMode = bazelConversionMode
430
Colin Cross617b88a2020-08-24 18:04:09 -0700431 return &bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400432 bp: ctx,
Cole Faust0abd4b42023-01-10 10:49:18 -0800433 baseModuleContext: moduleContext,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400434 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700435 }
436}
437
Colin Cross25de6c32019-06-06 14:29:25 -0700438func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000439 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500440 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700441 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700442 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500443 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700444 }
Colin Cross798bfce2016-10-12 14:28:16 -0700445 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500446 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700447 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700448 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700449}
450
Colin Cross617b88a2020-08-24 18:04:09 -0700451func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
452 mutator := &mutator{name: name, bottomUpMutator: m}
453 x.mutators = append(x.mutators, mutator)
454 return mutator
455}
456
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200457type IncomingTransitionContext interface {
458 // Module returns the target of the dependency edge for which the transition
459 // is being computed
460 Module() Module
461
462 // Config returns the configuration for the build.
463 Config() Config
464}
465
466type OutgoingTransitionContext interface {
467 // Module returns the target of the dependency edge for which the transition
468 // is being computed
469 Module() Module
470
471 // DepTag() Returns the dependency tag through which this dependency is
472 // reached
473 DepTag() blueprint.DependencyTag
474}
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200475
476// Transition mutators implement a top-down mechanism where a module tells its
477// direct dependencies what variation they should be built in but the dependency
478// has the final say.
479//
480// When implementing a transition mutator, one needs to implement four methods:
481// - Split() that tells what variations a module has by itself
482// - OutgoingTransition() where a module tells what it wants from its
483// dependency
484// - IncomingTransition() where a module has the final say about its own
485// variation
486// - Mutate() that changes the state of a module depending on its variation
487//
488// That the effective variation of module B when depended on by module A is the
489// composition the outgoing transition of module A and the incoming transition
490// of module B.
491//
492// the outgoing transition should not take the properties of the dependency into
493// account, only those of the module that depends on it. For this reason, the
494// dependency is not even passed into it as an argument. Likewise, the incoming
495// transition should not take the properties of the depending module into
496// account and is thus not informed about it. This makes for a nice
497// decomposition of the decision logic.
498//
499// A given transition mutator only affects its own variation; other variations
500// stay unchanged along the dependency edges.
501//
502// Soong makes sure that all modules are created in the desired variations and
503// that dependency edges are set up correctly. This ensures that "missing
504// variation" errors do not happen and allows for more flexible changes in the
505// value of the variation among dependency edges (as oppposed to bottom-up
506// mutators where if module A in variation X depends on module B and module B
507// has that variation X, A must depend on variation X of B)
508//
509// The limited power of the context objects passed to individual mutators
510// methods also makes it more difficult to shoot oneself in the foot. Complete
511// safety is not guaranteed because no one prevents individual transition
512// mutators from mutating modules in illegal ways and for e.g. Split() or
513// Mutate() to run their own visitations of the transitive dependency of the
514// module and both of these are bad ideas, but it's better than no guardrails at
515// all.
516//
517// This model is pretty close to Bazel's configuration transitions. The mapping
518// between concepts in Soong and Bazel is as follows:
519// - Module == configured target
520// - Variant == configuration
521// - Variation name == configuration flag
522// - Variation == configuration flag value
523// - Outgoing transition == attribute transition
524// - Incoming transition == rule transition
525//
526// The Split() method does not have a Bazel equivalent and Bazel split
527// transitions do not have a Soong equivalent.
528//
529// Mutate() does not make sense in Bazel due to the different models of the
530// two systems: when creating new variations, Soong clones the old module and
531// thus some way is needed to change it state whereas Bazel creates each
532// configuration of a given configured target anew.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200533type TransitionMutator interface {
534 // Split returns the set of variations that should be created for a module no
535 // matter who depends on it. Used when Make depends on a particular variation
536 // or when the module knows its variations just based on information given to
537 // it in the Blueprint file. This method should not mutate the module it is
538 // called on.
539 Split(ctx BaseModuleContext) []string
540
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200541 // Called on a module to determine which variation it wants from its direct
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200542 // dependencies. The dependency itself can override this decision. This method
543 // should not mutate the module itself.
544 OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string
545
546 // Called on a module to determine which variation it should be in based on
547 // the variation modules that depend on it want. This gives the module a final
548 // say about its own variations. This method should not mutate the module
549 // itself.
550 IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string
551
552 // Called after a module was split into multiple variations on each variation.
553 // It should not split the module any further but adding new dependencies is
554 // fine. Unlike all the other methods on TransitionMutator, this method is
555 // allowed to mutate the module.
556 Mutate(ctx BottomUpMutatorContext, variation string)
557}
558
559type androidTransitionMutator struct {
560 finalPhase bool
561 bazelConversionMode bool
562 mutator TransitionMutator
563}
564
565func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []string {
566 if m, ok := ctx.Module().(Module); ok {
567 moduleContext := m.base().baseModuleContextFactory(ctx)
568 moduleContext.bazelConversionMode = a.bazelConversionMode
569 return a.mutator.Split(&moduleContext)
570 } else {
571 return []string{""}
572 }
573}
574
575type outgoingTransitionContextImpl struct {
576 bp blueprint.OutgoingTransitionContext
577}
578
579func (c *outgoingTransitionContextImpl) Module() Module {
580 return c.bp.Module().(Module)
581}
582
583func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag {
584 return c.bp.DepTag()
585}
586
587func (a *androidTransitionMutator) OutgoingTransition(ctx blueprint.OutgoingTransitionContext, sourceVariation string) string {
588 if _, ok := ctx.Module().(Module); ok {
589 return a.mutator.OutgoingTransition(&outgoingTransitionContextImpl{bp: ctx}, sourceVariation)
590 } else {
591 return ""
592 }
593}
594
595type incomingTransitionContextImpl struct {
596 bp blueprint.IncomingTransitionContext
597}
598
599func (c *incomingTransitionContextImpl) Module() Module {
600 return c.bp.Module().(Module)
601}
602
603func (c *incomingTransitionContextImpl) Config() Config {
604 return c.bp.Config().(Config)
605}
606
607func (a *androidTransitionMutator) IncomingTransition(ctx blueprint.IncomingTransitionContext, incomingVariation string) string {
608 if _, ok := ctx.Module().(Module); ok {
609 return a.mutator.IncomingTransition(&incomingTransitionContextImpl{bp: ctx}, incomingVariation)
610 } else {
611 return ""
612 }
613}
614
615func (a *androidTransitionMutator) Mutate(ctx blueprint.BottomUpMutatorContext, variation string) {
616 if am, ok := ctx.Module().(Module); ok {
617 a.mutator.Mutate(bottomUpMutatorContextFactory(ctx, am, a.finalPhase, a.bazelConversionMode), variation)
618 }
619}
620
621func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) {
622 atm := &androidTransitionMutator{
623 finalPhase: x.finalPhase,
624 bazelConversionMode: x.bazelConversionMode,
625 mutator: m,
626 }
627 mutator := &mutator{
628 name: name,
629 transitionMutator: atm}
630 x.mutators = append(x.mutators, mutator)
631}
632
Liz Kammer356f7d42021-01-26 09:18:53 -0500633func (x *registerMutatorsContext) mutatorName(name string) string {
634 if x.bazelConversionMode {
635 return name + "_bp2build"
636 }
637 return name
638}
639
Colin Cross25de6c32019-06-06 14:29:25 -0700640func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700641 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700642 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400643 moduleContext := a.base().baseModuleContextFactory(ctx)
644 moduleContext.bazelConversionMode = x.bazelConversionMode
Colin Cross25de6c32019-06-06 14:29:25 -0700645 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700646 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400647 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700648 }
Colin Cross798bfce2016-10-12 14:28:16 -0700649 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700650 }
Colin Cross798bfce2016-10-12 14:28:16 -0700651 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500652 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700653 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700654 return mutator
655}
656
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000657func (mutator *mutator) componentName() string {
658 return mutator.name
659}
660
661func (mutator *mutator) register(ctx *Context) {
662 blueprintCtx := ctx.Context
663 var handle blueprint.MutatorHandle
664 if mutator.bottomUpMutator != nil {
665 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
666 } else if mutator.topDownMutator != nil {
667 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200668 } else if mutator.transitionMutator != nil {
669 blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000670 }
671 if mutator.parallel {
672 handle.Parallel()
673 }
674}
675
Colin Cross798bfce2016-10-12 14:28:16 -0700676type MutatorHandle interface {
677 Parallel() MutatorHandle
678}
679
680func (mutator *mutator) Parallel() MutatorHandle {
681 mutator.parallel = true
682 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700683}
Colin Cross1e676be2016-10-12 14:38:15 -0700684
Paul Duffin44f1d842020-06-26 20:17:02 +0100685func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
686 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
687}
688
689// A special mutator that runs just prior to the deps mutator to allow the dependencies
690// on component modules to be added so that they can depend directly on a prebuilt
691// module.
692func componentDepsMutator(ctx BottomUpMutatorContext) {
693 if m := ctx.Module(); m.Enabled() {
694 m.ComponentDepsMutator(ctx)
695 }
696}
697
Colin Cross1e676be2016-10-12 14:38:15 -0700698func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100699 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700700 m.DepsMutator(ctx)
701 }
702}
Colin Crossd11fcda2017-10-23 17:59:01 -0700703
Liz Kammer356f7d42021-01-26 09:18:53 -0500704func registerDepsMutator(ctx RegisterMutatorsContext) {
705 ctx.BottomUp("deps", depsMutator).Parallel()
706}
707
708func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
709 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
710 // being converted to build targets.
711 ctx.BottomUp("deps", depsMutator).Parallel()
712}
713
Jingwen Chen1fd14692021-02-05 03:01:50 -0500714func (t *topDownMutatorContext) CreateBazelTargetModule(
Jingwen Chen1fd14692021-02-05 03:01:50 -0500715 bazelProps bazel.BazelTargetModuleProperties,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000716 commonAttrs CommonAttributes,
Liz Kammer2ada09a2021-08-11 00:17:36 -0400717 attrs interface{}) {
Chris Parsons58852a02021-12-09 18:10:18 -0500718 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, bazel.BoolAttribute{})
719}
720
721func (t *topDownMutatorContext) CreateBazelTargetModuleWithRestrictions(
722 bazelProps bazel.BazelTargetModuleProperties,
723 commonAttrs CommonAttributes,
724 attrs interface{},
725 enabledProperty bazel.BoolAttribute) {
726 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, enabledProperty)
727}
728
Chris Parsons39a16972023-06-08 14:28:51 +0000729func (t *topDownMutatorContext) MarkBp2buildUnconvertible(
730 reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string) {
731 mod := t.Module()
732 mod.base().setBp2buildUnconvertible(reasonType, detail)
733}
734
Spandan Dasabedff02023-03-07 19:24:34 +0000735var (
736 bazelAliasModuleProperties = bazel.BazelTargetModuleProperties{
737 Rule_class: "alias",
738 }
739)
740
741type bazelAliasAttributes struct {
742 Actual *bazel.LabelAttribute
743}
744
745func (t *topDownMutatorContext) CreateBazelTargetAliasInDir(
746 dir string,
747 name string,
748 actual bazel.Label) {
749 mod := t.Module()
750 attrs := &bazelAliasAttributes{
751 Actual: bazel.MakeLabelAttribute(actual.Label),
752 }
753 info := bp2buildInfo{
754 Dir: dir,
755 BazelProps: bazelAliasModuleProperties,
756 CommonAttrs: CommonAttributes{Name: name},
757 ConstraintAttrs: constraintAttributes{},
758 Attrs: attrs,
759 }
760 mod.base().addBp2buildInfo(info)
761}
762
Spandan Das3131d672023-08-03 22:33:47 +0000763// Returns the directory in which the bazel target will be generated
764// If ca.Dir is not nil, use that
765// Otherwise default to the directory of the soong module
766func dirForBazelTargetGeneration(t *topDownMutatorContext, ca *CommonAttributes) string {
767 dir := t.OtherModuleDir(t.Module())
768 if ca.Dir != nil {
769 dir = *ca.Dir
770 // Restrict its use to dirs that contain an Android.bp file.
771 // There are several places in bp2build where we use the existence of Android.bp/BUILD on the filesystem
772 // to curate a compatible label for src files (e.g. headers for cc).
773 // If we arbritrarily create BUILD files, then it might render those curated labels incompatible.
774 if exists, _, _ := t.Config().fs.Exists(filepath.Join(dir, "Android.bp")); !exists {
775 t.ModuleErrorf("Cannot use ca.Dir to create a BazelTarget in dir: %v since it does not contain an Android.bp file", dir)
776 }
777
778 // Set ca.Dir to nil so that it does not get emitted to the BUILD files
779 ca.Dir = nil
780 }
781 return dir
782}
783
Spandan Das6a448ec2023-04-19 17:36:12 +0000784func (t *topDownMutatorContext) CreateBazelConfigSetting(
785 csa bazel.ConfigSettingAttributes,
786 ca CommonAttributes,
787 dir string) {
788 mod := t.Module()
789 info := bp2buildInfo{
790 Dir: dir,
791 BazelProps: bazel.BazelTargetModuleProperties{
792 Rule_class: "config_setting",
793 },
794 CommonAttrs: ca,
795 ConstraintAttrs: constraintAttributes{},
796 Attrs: &csa,
797 }
798 mod.base().addBp2buildInfo(info)
799}
800
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000801// ApexAvailableTags converts the apex_available property value of an ApexModule
802// module and returns it as a list of keyed tags.
803func ApexAvailableTags(mod Module) bazel.StringListAttribute {
804 attr := bazel.StringListAttribute{}
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000805 // Transform specific attributes into tags.
806 if am, ok := mod.(ApexModule); ok {
807 // TODO(b/218841706): hidl_interface has the apex_available prop, but it's
808 // defined directly as a prop and not via ApexModule, so this doesn't
809 // pick those props up.
Spandan Das2dc6dfc2023-04-17 19:16:06 +0000810 apexAvailable := am.apexModuleBase().ApexAvailable()
811 // If a user does not specify apex_available in Android.bp, then soong provides a default.
812 // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
813 if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
814 apexAvailable = []string{}
815 }
816 attr.Value = ConvertApexAvailableToTags(apexAvailable)
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000817 }
818 return attr
819}
820
Spandan Dasf57a9662023-04-12 19:05:49 +0000821func ApexAvailableTagsWithoutTestApexes(ctx BaseModuleContext, mod Module) bazel.StringListAttribute {
822 attr := bazel.StringListAttribute{}
823 if am, ok := mod.(ApexModule); ok {
824 apexAvailableWithoutTestApexes := removeTestApexes(ctx, am.apexModuleBase().ApexAvailable())
825 // If a user does not specify apex_available in Android.bp, then soong provides a default.
826 // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
827 if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
828 apexAvailableWithoutTestApexes = []string{}
829 }
830 attr.Value = ConvertApexAvailableToTags(apexAvailableWithoutTestApexes)
831 }
832 return attr
833}
834
835func removeTestApexes(ctx BaseModuleContext, apex_available []string) []string {
836 testApexes := []string{}
837 for _, aa := range apex_available {
838 // ignore the wildcards
839 if InList(aa, AvailableToRecognziedWildcards) {
840 continue
841 }
842 mod, _ := ctx.ModuleFromName(aa)
843 if apex, ok := mod.(ApexTestInterface); ok && apex.IsTestApex() {
844 testApexes = append(testApexes, aa)
845 }
846 }
847 return RemoveListFromList(CopyOf(apex_available), testApexes)
848}
849
Cole Faustfb11c1c2023-02-10 11:27:46 -0800850func ConvertApexAvailableToTags(apexAvailable []string) []string {
851 if len(apexAvailable) == 0 {
852 // We need nil specifically to make bp2build not add the tags property at all,
853 // instead of adding it with an empty list
854 return nil
855 }
856 result := make([]string, 0, len(apexAvailable))
857 for _, a := range apexAvailable {
858 result = append(result, "apex_available="+a)
859 }
860 return result
861}
862
Spandan Das39b6cc52023-04-12 19:05:49 +0000863// ConvertApexAvailableToTagsWithoutTestApexes converts a list of apex names to a list of bazel tags
864// This function drops any test apexes from the input.
865func ConvertApexAvailableToTagsWithoutTestApexes(ctx BaseModuleContext, apexAvailable []string) []string {
866 noTestApexes := removeTestApexes(ctx, apexAvailable)
867 return ConvertApexAvailableToTags(noTestApexes)
868}
869
Chris Parsons58852a02021-12-09 18:10:18 -0500870func (t *topDownMutatorContext) createBazelTargetModule(
871 bazelProps bazel.BazelTargetModuleProperties,
872 commonAttrs CommonAttributes,
873 attrs interface{},
874 enabledProperty bazel.BoolAttribute) {
875 constraintAttributes := commonAttrs.fillCommonBp2BuildModuleAttrs(t, enabledProperty)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000876 mod := t.Module()
Liz Kammer2ada09a2021-08-11 00:17:36 -0400877 info := bp2buildInfo{
Spandan Das3131d672023-08-03 22:33:47 +0000878 Dir: dirForBazelTargetGeneration(t, &commonAttrs),
Chris Parsons58852a02021-12-09 18:10:18 -0500879 BazelProps: bazelProps,
880 CommonAttrs: commonAttrs,
881 ConstraintAttrs: constraintAttributes,
882 Attrs: attrs,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500883 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000884 mod.base().addBp2buildInfo(info)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500885}
886
Colin Crossdc35e212019-06-06 16:13:11 -0700887// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
888// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
889// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
890// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
891// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
892
Colin Crosscb55e082019-07-01 15:32:31 -0700893func (t *topDownMutatorContext) MutatorName() string {
894 return t.bp.MutatorName()
895}
896
Colin Crossdc35e212019-06-06 16:13:11 -0700897func (t *topDownMutatorContext) Rename(name string) {
898 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700899 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700900}
901
Liz Kammerf31c9002022-04-26 09:08:55 -0400902func (t *topDownMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
903 return t.bp.CreateModule(factory, name, props...)
904}
905
Colin Crosse003c4a2019-09-25 12:58:36 -0700906func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400907 return createModule(t, factory, "_topDownMutatorModule", props...)
Colin Crossdc35e212019-06-06 16:13:11 -0700908}
909
Liz Kammera060c452021-03-24 10:14:47 -0400910func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400911 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), "", props...).(Module)
Liz Kammera060c452021-03-24 10:14:47 -0400912 return module
913}
914
Colin Crosscb55e082019-07-01 15:32:31 -0700915func (b *bottomUpMutatorContext) MutatorName() string {
916 return b.bp.MutatorName()
917}
918
Colin Crossdc35e212019-06-06 16:13:11 -0700919func (b *bottomUpMutatorContext) Rename(name string) {
920 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700921 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700922}
923
Colin Cross4f1dcb02020-09-16 18:45:04 -0700924func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400925 if b.baseModuleContext.checkedMissingDeps() {
926 panic("Adding deps not allowed after checking for missing deps")
927 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700928 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700929}
930
931func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400932 if b.baseModuleContext.checkedMissingDeps() {
933 panic("Adding deps not allowed after checking for missing deps")
934 }
Colin Crossdc35e212019-06-06 16:13:11 -0700935 b.bp.AddReverseDependency(module, tag, name)
936}
937
Colin Cross43b92e02019-11-18 15:28:57 -0800938func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000939 if b.finalPhase {
940 panic("CreateVariations not allowed in FinalDepsMutators")
941 }
942
Colin Cross9a362232019-07-01 15:32:45 -0700943 modules := b.bp.CreateVariations(variations...)
944
Colin Cross43b92e02019-11-18 15:28:57 -0800945 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700946 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800947 aModules[i] = modules[i].(Module)
948 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700949 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
950 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
951 }
952
Colin Cross43b92e02019-11-18 15:28:57 -0800953 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700954}
955
Colin Cross43b92e02019-11-18 15:28:57 -0800956func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000957 if b.finalPhase {
958 panic("CreateLocalVariations not allowed in FinalDepsMutators")
959 }
960
Colin Cross9a362232019-07-01 15:32:45 -0700961 modules := b.bp.CreateLocalVariations(variations...)
962
Colin Cross43b92e02019-11-18 15:28:57 -0800963 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700964 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800965 aModules[i] = modules[i].(Module)
966 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700967 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
968 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
969 }
970
Colin Cross43b92e02019-11-18 15:28:57 -0800971 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700972}
973
974func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
975 b.bp.SetDependencyVariation(variation)
976}
977
Jiyong Park1d1119f2019-07-29 21:27:18 +0900978func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
979 b.bp.SetDefaultDependencyVariation(variation)
980}
981
Colin Crossdc35e212019-06-06 16:13:11 -0700982func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700983 names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400984 if b.baseModuleContext.checkedMissingDeps() {
985 panic("Adding deps not allowed after checking for missing deps")
986 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700987 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700988}
989
990func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700991 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400992 if b.baseModuleContext.checkedMissingDeps() {
993 panic("Adding deps not allowed after checking for missing deps")
994 }
Colin Crossdc35e212019-06-06 16:13:11 -0700995
Colin Cross4f1dcb02020-09-16 18:45:04 -0700996 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700997}
998
999func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
1000 b.bp.AddInterVariantDependency(tag, from, to)
1001}
1002
1003func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -04001004 if b.baseModuleContext.checkedMissingDeps() {
1005 panic("Adding deps not allowed after checking for missing deps")
1006 }
Colin Crossdc35e212019-06-06 16:13:11 -07001007 b.bp.ReplaceDependencies(name)
1008}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -08001009
Paul Duffin80342d72020-06-26 22:08:43 +01001010func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
Liz Kammerc13f7852023-05-17 13:01:48 -04001011 if b.baseModuleContext.checkedMissingDeps() {
1012 panic("Adding deps not allowed after checking for missing deps")
1013 }
Paul Duffin80342d72020-06-26 22:08:43 +01001014 b.bp.ReplaceDependenciesIf(name, predicate)
1015}
1016
Jaewoong Jung9f88ce22019-11-15 10:57:34 -08001017func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
1018 b.bp.AliasVariation(variationName)
1019}
Colin Cross1b9604b2020-08-11 12:03:56 -07001020
1021func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
1022 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
1023}
Colin Crossd27e7b82020-07-02 11:38:17 -07001024
1025func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
1026 b.bp.SetVariationProvider(module, provider, value)
1027}