blob: 6bcac93c110b086621bb632e889de6ad0ee70336 [file] [log] [blame]
Colin Cross6362e272015-10-29 15:25:03 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross6362e272015-10-29 15:25:03 -070016
Colin Cross795c3772017-03-16 16:50:10 -070017import (
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000018 "android/soong/bazel"
Chris Parsons39a16972023-06-08 14:28:51 +000019 "android/soong/ui/metrics/bp2build_metrics_proto"
Spandan Das3131d672023-08-03 22:33:47 +000020 "path/filepath"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000021
Colin Cross795c3772017-03-16 16:50:10 -070022 "github.com/google/blueprint"
23)
Colin Cross6362e272015-10-29 15:25:03 -070024
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070025// Phases:
26// run Pre-arch mutators
27// run archMutator
28// run Pre-deps mutators
29// run depsMutator
30// run PostDeps mutators
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000031// run FinalDeps mutators (CreateVariations disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070032// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070033
Jingwen Chen73850672020-12-14 08:25:34 -050034// RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
Liz Kammerbe46fcc2021-11-01 15:32:43 -040035func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) {
Spandan Das5af0bd32022-09-28 20:43:08 +000036 bp2buildMutators := append(preArchMutators, registerBp2buildConversionMutator)
37 registerMutatorsForBazelConversion(ctx, bp2buildMutators)
38}
39
40// RegisterMutatorsForApiBazelConversion is an alternate registration pipeline for api_bp2build
41// This pipeline restricts generation of Bazel targets to Soong modules that contribute APIs
42func RegisterMutatorsForApiBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) {
43 bp2buildMutators := append(preArchMutators, registerApiBp2buildConversionMutator)
44 registerMutatorsForBazelConversion(ctx, bp2buildMutators)
45}
46
47func registerMutatorsForBazelConversion(ctx *Context, bp2buildMutators []RegisterMutatorFunc) {
Liz Kammer356f7d42021-01-26 09:18:53 -050048 mctx := &registerMutatorsContext{
49 bazelConversionMode: true,
Jingwen Chen041b1842021-02-01 00:23:25 -050050 }
51
Spandan Das5af0bd32022-09-28 20:43:08 +000052 allMutators := append([]RegisterMutatorFunc{
Liz Kammer356f7d42021-01-26 09:18:53 -050053 RegisterNamespaceMutator,
54 RegisterDefaultsPreArchMutators,
55 // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should
56 // evaluate the impact on conversion.
57 RegisterPrebuiltsPreArchMutators,
58 },
Spandan Das5af0bd32022-09-28 20:43:08 +000059 bp2buildMutators...)
Liz Kammer356f7d42021-01-26 09:18:53 -050060
Jingwen Chen73850672020-12-14 08:25:34 -050061 // Register bp2build mutators
Spandan Das5af0bd32022-09-28 20:43:08 +000062 for _, f := range allMutators {
Jingwen Chen73850672020-12-14 08:25:34 -050063 f(mctx)
64 }
65
Paul Duffin1d2d42f2021-03-06 20:08:12 +000066 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050067}
68
Paul Duffinc05b0342021-03-06 13:28:13 +000069// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
70// with the InitRegistrationContext and will be used at runtime.
71func collateGloballyRegisteredMutators() sortableComponents {
Liz Kammerc13f7852023-05-17 13:01:48 -040072 // ensure mixed builds mutator is the last mutator
73 finalDeps = append(finalDeps, registerMixedBuildsMutator)
Paul Duffinc05b0342021-03-06 13:28:13 +000074 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
75}
76
77// collateRegisteredMutators constructs a single list of mutators from the separate lists.
78func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070079 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080080
81 register := func(funcs []RegisterMutatorFunc) {
82 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070083 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080084 }
85 }
86
Colin Crosscec81712017-07-13 14:43:27 -070087 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080088
Colin Crosscec81712017-07-13 14:43:27 -070089 register(preDeps)
90
Liz Kammer356f7d42021-01-26 09:18:53 -050091 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070092
93 register(postDeps)
94
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000095 mctx.finalPhase = true
96 register(finalDeps)
97
Paul Duffinc05b0342021-03-06 13:28:13 +000098 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -070099}
100
101type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000102 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -0500103 finalPhase bool
104 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -0700105}
Colin Cross1e676be2016-10-12 14:38:15 -0700106
107type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -0700108 TopDown(name string, m TopDownMutator) MutatorHandle
109 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -0700110 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200111 Transition(name string, m TransitionMutator)
Colin Cross1e676be2016-10-12 14:38:15 -0700112}
113
114type RegisterMutatorFunc func(RegisterMutatorsContext)
115
Colin Crosscec81712017-07-13 14:43:27 -0700116var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800117 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100118
Paul Duffinaa4162e2020-05-05 11:35:43 +0100119 // Check the visibility rules are valid.
120 //
121 // This must run after the package renamer mutators so that any issues found during
122 // validation of the package's default_visibility property are reported using the
123 // correct package name and not the synthetic name.
124 //
125 // This must also be run before defaults mutators as the rules for validation are
126 // different before checking the rules than they are afterwards. e.g.
127 // visibility: ["//visibility:private", "//visibility:public"]
128 // would be invalid if specified in a module definition but is valid if it results
129 // from something like this:
130 //
131 // defaults {
132 // name: "defaults",
133 // // Be inaccessible outside a package by default.
134 // visibility: ["//visibility:private"]
135 // }
136 //
137 // defaultable_module {
138 // name: "defaultable_module",
139 // defaults: ["defaults"],
140 // // Override the default.
141 // visibility: ["//visibility:public"]
142 // }
143 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000144 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100145
Bob Badour37af0462021-01-07 03:34:31 +0000146 // Record the default_applicable_licenses for each package.
147 //
148 // This must run before the defaults so that defaults modules can pick up the package default.
149 RegisterLicensesPackageMapper,
150
Paul Duffinaa4162e2020-05-05 11:35:43 +0100151 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100152 //
153 // Any mutators that are added before this will not see any modules created by
154 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700155 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100156
Paul Duffin44f1d842020-06-26 20:17:02 +0100157 // Add dependencies on any components so that any component references can be
158 // resolved within the deps mutator.
159 //
160 // Must be run after defaults so it can be used to create dependencies on the
161 // component modules that are creating in a DefaultableHook.
162 //
163 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
164 // renamed. That is so that if a module creates components using a prebuilt module
165 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
166 // the prebuilt module and not the source module.
167 RegisterComponentsMutator,
168
Paul Duffinc988c8e2020-04-29 18:27:14 +0100169 // Create an association between prebuilt modules and their corresponding source
170 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100171 //
172 // Must be run after defaults mutators to ensure that any modules created by
173 // a DefaultableHook can be either a prebuilt or a source module with a matching
174 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100175 RegisterPrebuiltsPreArchMutators,
176
Bob Badour37af0462021-01-07 03:34:31 +0000177 // Gather the licenses properties for all modules for use during expansion and enforcement.
178 //
179 // This must come after the defaults mutators to ensure that any licenses supplied
180 // in a defaults module has been successfully applied before the rules are gathered.
181 RegisterLicensesPropertyGatherer,
182
Paul Duffinaa4162e2020-05-05 11:35:43 +0100183 // Gather the visibility rules for all modules for us during visibility enforcement.
184 //
185 // This must come after the defaults mutators to ensure that any visibility supplied
186 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000187 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700188}
189
Colin Crossae4c6182017-09-15 17:33:55 -0700190func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700191 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800192 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700193 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700194}
195
Colin Crosscec81712017-07-13 14:43:27 -0700196var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700197 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700198}
199
200var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800201 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700202 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000203 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000204 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100205 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700206 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700207}
Colin Cross1e676be2016-10-12 14:38:15 -0700208
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000209var finalDeps = []RegisterMutatorFunc{}
210
Colin Cross1e676be2016-10-12 14:38:15 -0700211func PreArchMutators(f RegisterMutatorFunc) {
212 preArch = append(preArch, f)
213}
214
215func PreDepsMutators(f RegisterMutatorFunc) {
216 preDeps = append(preDeps, f)
217}
218
219func PostDepsMutators(f RegisterMutatorFunc) {
220 postDeps = append(postDeps, f)
221}
222
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000223func FinalDepsMutators(f RegisterMutatorFunc) {
224 finalDeps = append(finalDeps, f)
225}
226
Liz Kammer356f7d42021-01-26 09:18:53 -0500227var bp2buildPreArchMutators = []RegisterMutatorFunc{}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400228
Liz Kammer12615db2021-09-28 09:19:17 -0400229// A minimal context for Bp2build conversion
230type Bp2buildMutatorContext interface {
231 BazelConversionPathContext
232
233 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
234}
235
Liz Kammer356f7d42021-01-26 09:18:53 -0500236// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
237// into Bazel BUILD targets that should run prior to deps and conversion.
238func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
239 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
240}
241
Colin Cross9f35c3d2020-09-16 19:04:41 -0700242type BaseMutatorContext interface {
243 BaseModuleContext
244
245 // MutatorName returns the name that this mutator was registered with.
246 MutatorName() string
247
248 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
249 // AddDependency or OtherModuleName until after this mutator pass is complete.
250 Rename(name string)
251}
252
Colin Cross25de6c32019-06-06 14:29:25 -0700253type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700254
Colin Cross635c3b02016-05-18 15:37:25 -0700255type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700256 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700257
Colin Cross9f35c3d2020-09-16 19:04:41 -0700258 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
259 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700260 CreateModule(ModuleFactory, ...interface{}) Module
Jingwen Chen1fd14692021-02-05 03:01:50 -0500261
262 // CreateBazelTargetModule creates a BazelTargetModule by calling the
263 // factory method, just like in CreateModule, but also requires
264 // BazelTargetModuleProperties containing additional metadata for the
265 // bp2build codegenerator.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000266 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
Chris Parsons58852a02021-12-09 18:10:18 -0500267
268 // CreateBazelTargetModuleWithRestrictions creates a BazelTargetModule by calling the
269 // factory method, just like in CreateModule, but also requires
270 // BazelTargetModuleProperties containing additional metadata for the
271 // bp2build codegenerator. The generated target is restricted to only be buildable for certain
272 // platforms, as dictated by a given bool attribute: the target will not be buildable in
273 // any platform for which this bool attribute is false.
274 CreateBazelTargetModuleWithRestrictions(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}, bazel.BoolAttribute)
Spandan Dasabedff02023-03-07 19:24:34 +0000275
Chris Parsons39a16972023-06-08 14:28:51 +0000276 // MarkBp2buildUnconvertible registers the current module as "unconvertible to bp2build" for the
277 // given reason.
278 MarkBp2buildUnconvertible(reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string)
279
Spandan Dasabedff02023-03-07 19:24:34 +0000280 // CreateBazelTargetAliasInDir creates an alias definition in `dir` directory.
281 // This function can be used to create alias definitions in a directory that is different
282 // from the directory of the visited Soong module.
283 CreateBazelTargetAliasInDir(dir string, name string, actual bazel.Label)
Spandan Das6a448ec2023-04-19 17:36:12 +0000284
285 // CreateBazelConfigSetting creates a config_setting in <dir>/BUILD.bazel
286 // build/bazel has several static config_setting(s) that are used in Bazel builds.
287 // This function can be used to createa additional config_setting(s) based on the build graph
288 // (e.g. a config_setting specific to an apex variant)
289 CreateBazelConfigSetting(csa bazel.ConfigSettingAttributes, ca CommonAttributes, dir string)
Colin Cross6362e272015-10-29 15:25:03 -0700290}
291
Colin Cross25de6c32019-06-06 14:29:25 -0700292type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700293 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700294 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700295}
296
Colin Cross25de6c32019-06-06 14:29:25 -0700297type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700298
Colin Cross635c3b02016-05-18 15:37:25 -0700299type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700300 BaseMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800301
Colin Cross4f1dcb02020-09-16 18:45:04 -0700302 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
303 // dependency (some entries may be nil).
304 //
305 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
306 // new dependencies have had the current mutator called on them. If the mutator is not
307 // parallel this method does not affect the ordering of the current mutator pass, but will
308 // be ordered correctly for all future mutator passes.
309 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700310
311 // AddReverseDependency adds a dependency from the destination to the given module.
312 // Does not affect the ordering of the current mutator pass, but will be ordered
313 // correctly for all future mutator passes. All reverse dependencies for a destination module are
314 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
315 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800316 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700317
318 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
319 // parameter. It returns a list of new modules in the same order as the variationNames
320 // list.
321 //
322 // If any of the dependencies of the module being operated on were already split
323 // by calling CreateVariations with the same name, the dependency will automatically
324 // be updated to point the matching variant.
325 //
326 // If a module is split, and then a module depending on the first module is not split
327 // when the Mutator is later called on it, the dependency of the depending module will
328 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800329 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700330
331 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
332 // parameter. It returns a list of new modules in the same order as the variantNames
333 // list.
334 //
335 // Local variations do not affect automatic dependency resolution - dependencies added
336 // to the split module via deps or DynamicDependerModule must exactly match a variant
337 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800338 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700339
340 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
341 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800342 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700343
344 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
345 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900346 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700347
348 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700349 // argument to select which variant of the dependency to use. It returns a slice of modules for
350 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
Usta Shresthac725f472022-01-11 02:44:21 -0500351 // all the non-local variations of the current module, plus the variations argument.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700352 //
353 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
354 // new dependencies have had the current mutator called on them. If the mutator is not
355 // parallel this method does not affect the ordering of the current mutator pass, but will
356 // be ordered correctly for all future mutator passes.
Usta Shresthac725f472022-01-11 02:44:21 -0500357 AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700358
359 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700360 // variations argument to select which variant of the dependency to use. It returns a slice of
361 // modules for each dependency (some entries may be nil). A variant of the dependency must
362 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700363 // For any unspecified variation the first variant will be used.
364 //
365 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
366 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700367 //
368 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
369 // new dependencies have had the current mutator called on them. If the mutator is not
370 // parallel this method does not affect the ordering of the current mutator pass, but will
371 // be ordered correctly for all future mutator passes.
372 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700373
374 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
375 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
376 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
377 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800378 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700379
380 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
381 // specified name with the current variant of this module. Replacements don't take effect until
382 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800383 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700384
385 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
386 // specified name with the current variant of this module as long as the supplied predicate returns
387 // true.
388 //
389 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100390 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700391
392 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
393 // and creates an alias from the current variant (before the mutator has run) to the new
394 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
395 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
396 // be used to add dependencies on the newly created variant using the variant map from
397 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800398 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700399
400 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
401 // module, and creates an alias from a new fromVariationName variant the toVariationName
402 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
403 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
404 // be used to add dependencies on the toVariationName variant using the fromVariationName
405 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700406 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700407
408 // SetVariationProvider sets the value for a provider for the given newly created variant of
409 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
410 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
411 // if the value is not of the appropriate type, or if the module is not a newly created
412 // variant of the current module. The value should not be modified after being passed to
413 // SetVariationProvider.
414 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700415}
416
Colin Cross25de6c32019-06-06 14:29:25 -0700417type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700418 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700419 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400420 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700421}
422
Colin Cross617b88a2020-08-24 18:04:09 -0700423func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500424 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700425
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400426 moduleContext := a.base().baseModuleContextFactory(ctx)
427 moduleContext.bazelConversionMode = bazelConversionMode
428
Colin Cross617b88a2020-08-24 18:04:09 -0700429 return &bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400430 bp: ctx,
Cole Faust0abd4b42023-01-10 10:49:18 -0800431 baseModuleContext: moduleContext,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400432 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700433 }
434}
435
Colin Cross25de6c32019-06-06 14:29:25 -0700436func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000437 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500438 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700439 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700440 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500441 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700442 }
Colin Cross798bfce2016-10-12 14:28:16 -0700443 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500444 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700445 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700446 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700447}
448
Colin Cross617b88a2020-08-24 18:04:09 -0700449func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
450 mutator := &mutator{name: name, bottomUpMutator: m}
451 x.mutators = append(x.mutators, mutator)
452 return mutator
453}
454
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200455type IncomingTransitionContext interface {
456 // Module returns the target of the dependency edge for which the transition
457 // is being computed
458 Module() Module
459
460 // Config returns the configuration for the build.
461 Config() Config
462}
463
464type OutgoingTransitionContext interface {
465 // Module returns the target of the dependency edge for which the transition
466 // is being computed
467 Module() Module
468
469 // DepTag() Returns the dependency tag through which this dependency is
470 // reached
471 DepTag() blueprint.DependencyTag
472}
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200473
474// Transition mutators implement a top-down mechanism where a module tells its
475// direct dependencies what variation they should be built in but the dependency
476// has the final say.
477//
478// When implementing a transition mutator, one needs to implement four methods:
479// - Split() that tells what variations a module has by itself
480// - OutgoingTransition() where a module tells what it wants from its
481// dependency
482// - IncomingTransition() where a module has the final say about its own
483// variation
484// - Mutate() that changes the state of a module depending on its variation
485//
486// That the effective variation of module B when depended on by module A is the
487// composition the outgoing transition of module A and the incoming transition
488// of module B.
489//
490// the outgoing transition should not take the properties of the dependency into
491// account, only those of the module that depends on it. For this reason, the
492// dependency is not even passed into it as an argument. Likewise, the incoming
493// transition should not take the properties of the depending module into
494// account and is thus not informed about it. This makes for a nice
495// decomposition of the decision logic.
496//
497// A given transition mutator only affects its own variation; other variations
498// stay unchanged along the dependency edges.
499//
500// Soong makes sure that all modules are created in the desired variations and
501// that dependency edges are set up correctly. This ensures that "missing
502// variation" errors do not happen and allows for more flexible changes in the
503// value of the variation among dependency edges (as oppposed to bottom-up
504// mutators where if module A in variation X depends on module B and module B
505// has that variation X, A must depend on variation X of B)
506//
507// The limited power of the context objects passed to individual mutators
508// methods also makes it more difficult to shoot oneself in the foot. Complete
509// safety is not guaranteed because no one prevents individual transition
510// mutators from mutating modules in illegal ways and for e.g. Split() or
511// Mutate() to run their own visitations of the transitive dependency of the
512// module and both of these are bad ideas, but it's better than no guardrails at
513// all.
514//
515// This model is pretty close to Bazel's configuration transitions. The mapping
516// between concepts in Soong and Bazel is as follows:
517// - Module == configured target
518// - Variant == configuration
519// - Variation name == configuration flag
520// - Variation == configuration flag value
521// - Outgoing transition == attribute transition
522// - Incoming transition == rule transition
523//
524// The Split() method does not have a Bazel equivalent and Bazel split
525// transitions do not have a Soong equivalent.
526//
527// Mutate() does not make sense in Bazel due to the different models of the
528// two systems: when creating new variations, Soong clones the old module and
529// thus some way is needed to change it state whereas Bazel creates each
530// configuration of a given configured target anew.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200531type TransitionMutator interface {
532 // Split returns the set of variations that should be created for a module no
533 // matter who depends on it. Used when Make depends on a particular variation
534 // or when the module knows its variations just based on information given to
535 // it in the Blueprint file. This method should not mutate the module it is
536 // called on.
537 Split(ctx BaseModuleContext) []string
538
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200539 // Called on a module to determine which variation it wants from its direct
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200540 // dependencies. The dependency itself can override this decision. This method
541 // should not mutate the module itself.
542 OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string
543
544 // Called on a module to determine which variation it should be in based on
545 // the variation modules that depend on it want. This gives the module a final
546 // say about its own variations. This method should not mutate the module
547 // itself.
548 IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string
549
550 // Called after a module was split into multiple variations on each variation.
551 // It should not split the module any further but adding new dependencies is
552 // fine. Unlike all the other methods on TransitionMutator, this method is
553 // allowed to mutate the module.
554 Mutate(ctx BottomUpMutatorContext, variation string)
555}
556
557type androidTransitionMutator struct {
558 finalPhase bool
559 bazelConversionMode bool
560 mutator TransitionMutator
561}
562
563func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []string {
564 if m, ok := ctx.Module().(Module); ok {
565 moduleContext := m.base().baseModuleContextFactory(ctx)
566 moduleContext.bazelConversionMode = a.bazelConversionMode
567 return a.mutator.Split(&moduleContext)
568 } else {
569 return []string{""}
570 }
571}
572
573type outgoingTransitionContextImpl struct {
574 bp blueprint.OutgoingTransitionContext
575}
576
577func (c *outgoingTransitionContextImpl) Module() Module {
578 return c.bp.Module().(Module)
579}
580
581func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag {
582 return c.bp.DepTag()
583}
584
585func (a *androidTransitionMutator) OutgoingTransition(ctx blueprint.OutgoingTransitionContext, sourceVariation string) string {
586 if _, ok := ctx.Module().(Module); ok {
587 return a.mutator.OutgoingTransition(&outgoingTransitionContextImpl{bp: ctx}, sourceVariation)
588 } else {
589 return ""
590 }
591}
592
593type incomingTransitionContextImpl struct {
594 bp blueprint.IncomingTransitionContext
595}
596
597func (c *incomingTransitionContextImpl) Module() Module {
598 return c.bp.Module().(Module)
599}
600
601func (c *incomingTransitionContextImpl) Config() Config {
602 return c.bp.Config().(Config)
603}
604
605func (a *androidTransitionMutator) IncomingTransition(ctx blueprint.IncomingTransitionContext, incomingVariation string) string {
606 if _, ok := ctx.Module().(Module); ok {
607 return a.mutator.IncomingTransition(&incomingTransitionContextImpl{bp: ctx}, incomingVariation)
608 } else {
609 return ""
610 }
611}
612
613func (a *androidTransitionMutator) Mutate(ctx blueprint.BottomUpMutatorContext, variation string) {
614 if am, ok := ctx.Module().(Module); ok {
615 a.mutator.Mutate(bottomUpMutatorContextFactory(ctx, am, a.finalPhase, a.bazelConversionMode), variation)
616 }
617}
618
619func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) {
620 atm := &androidTransitionMutator{
621 finalPhase: x.finalPhase,
622 bazelConversionMode: x.bazelConversionMode,
623 mutator: m,
624 }
625 mutator := &mutator{
626 name: name,
627 transitionMutator: atm}
628 x.mutators = append(x.mutators, mutator)
629}
630
Liz Kammer356f7d42021-01-26 09:18:53 -0500631func (x *registerMutatorsContext) mutatorName(name string) string {
632 if x.bazelConversionMode {
633 return name + "_bp2build"
634 }
635 return name
636}
637
Colin Cross25de6c32019-06-06 14:29:25 -0700638func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700639 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700640 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400641 moduleContext := a.base().baseModuleContextFactory(ctx)
642 moduleContext.bazelConversionMode = x.bazelConversionMode
Colin Cross25de6c32019-06-06 14:29:25 -0700643 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700644 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400645 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700646 }
Colin Cross798bfce2016-10-12 14:28:16 -0700647 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700648 }
Colin Cross798bfce2016-10-12 14:28:16 -0700649 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500650 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700651 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700652 return mutator
653}
654
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000655func (mutator *mutator) componentName() string {
656 return mutator.name
657}
658
659func (mutator *mutator) register(ctx *Context) {
660 blueprintCtx := ctx.Context
661 var handle blueprint.MutatorHandle
662 if mutator.bottomUpMutator != nil {
663 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
664 } else if mutator.topDownMutator != nil {
665 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200666 } else if mutator.transitionMutator != nil {
667 blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000668 }
669 if mutator.parallel {
670 handle.Parallel()
671 }
672}
673
Colin Cross798bfce2016-10-12 14:28:16 -0700674type MutatorHandle interface {
675 Parallel() MutatorHandle
676}
677
678func (mutator *mutator) Parallel() MutatorHandle {
679 mutator.parallel = true
680 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700681}
Colin Cross1e676be2016-10-12 14:38:15 -0700682
Paul Duffin44f1d842020-06-26 20:17:02 +0100683func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
684 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
685}
686
687// A special mutator that runs just prior to the deps mutator to allow the dependencies
688// on component modules to be added so that they can depend directly on a prebuilt
689// module.
690func componentDepsMutator(ctx BottomUpMutatorContext) {
691 if m := ctx.Module(); m.Enabled() {
692 m.ComponentDepsMutator(ctx)
693 }
694}
695
Colin Cross1e676be2016-10-12 14:38:15 -0700696func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100697 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700698 m.DepsMutator(ctx)
699 }
700}
Colin Crossd11fcda2017-10-23 17:59:01 -0700701
Liz Kammer356f7d42021-01-26 09:18:53 -0500702func registerDepsMutator(ctx RegisterMutatorsContext) {
703 ctx.BottomUp("deps", depsMutator).Parallel()
704}
705
706func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
707 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
708 // being converted to build targets.
709 ctx.BottomUp("deps", depsMutator).Parallel()
710}
711
Jingwen Chen1fd14692021-02-05 03:01:50 -0500712func (t *topDownMutatorContext) CreateBazelTargetModule(
Jingwen Chen1fd14692021-02-05 03:01:50 -0500713 bazelProps bazel.BazelTargetModuleProperties,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000714 commonAttrs CommonAttributes,
Liz Kammer2ada09a2021-08-11 00:17:36 -0400715 attrs interface{}) {
Chris Parsons58852a02021-12-09 18:10:18 -0500716 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, bazel.BoolAttribute{})
717}
718
719func (t *topDownMutatorContext) CreateBazelTargetModuleWithRestrictions(
720 bazelProps bazel.BazelTargetModuleProperties,
721 commonAttrs CommonAttributes,
722 attrs interface{},
723 enabledProperty bazel.BoolAttribute) {
724 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, enabledProperty)
725}
726
Chris Parsons39a16972023-06-08 14:28:51 +0000727func (t *topDownMutatorContext) MarkBp2buildUnconvertible(
728 reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string) {
729 mod := t.Module()
730 mod.base().setBp2buildUnconvertible(reasonType, detail)
731}
732
Spandan Dasabedff02023-03-07 19:24:34 +0000733var (
734 bazelAliasModuleProperties = bazel.BazelTargetModuleProperties{
735 Rule_class: "alias",
736 }
737)
738
739type bazelAliasAttributes struct {
740 Actual *bazel.LabelAttribute
741}
742
743func (t *topDownMutatorContext) CreateBazelTargetAliasInDir(
744 dir string,
745 name string,
746 actual bazel.Label) {
747 mod := t.Module()
748 attrs := &bazelAliasAttributes{
749 Actual: bazel.MakeLabelAttribute(actual.Label),
750 }
751 info := bp2buildInfo{
752 Dir: dir,
753 BazelProps: bazelAliasModuleProperties,
754 CommonAttrs: CommonAttributes{Name: name},
755 ConstraintAttrs: constraintAttributes{},
756 Attrs: attrs,
757 }
758 mod.base().addBp2buildInfo(info)
759}
760
Spandan Das3131d672023-08-03 22:33:47 +0000761// Returns the directory in which the bazel target will be generated
762// If ca.Dir is not nil, use that
763// Otherwise default to the directory of the soong module
764func dirForBazelTargetGeneration(t *topDownMutatorContext, ca *CommonAttributes) string {
765 dir := t.OtherModuleDir(t.Module())
766 if ca.Dir != nil {
767 dir = *ca.Dir
768 // Restrict its use to dirs that contain an Android.bp file.
769 // There are several places in bp2build where we use the existence of Android.bp/BUILD on the filesystem
770 // to curate a compatible label for src files (e.g. headers for cc).
771 // If we arbritrarily create BUILD files, then it might render those curated labels incompatible.
772 if exists, _, _ := t.Config().fs.Exists(filepath.Join(dir, "Android.bp")); !exists {
773 t.ModuleErrorf("Cannot use ca.Dir to create a BazelTarget in dir: %v since it does not contain an Android.bp file", dir)
774 }
775
776 // Set ca.Dir to nil so that it does not get emitted to the BUILD files
777 ca.Dir = nil
778 }
779 return dir
780}
781
Spandan Das6a448ec2023-04-19 17:36:12 +0000782func (t *topDownMutatorContext) CreateBazelConfigSetting(
783 csa bazel.ConfigSettingAttributes,
784 ca CommonAttributes,
785 dir string) {
786 mod := t.Module()
787 info := bp2buildInfo{
788 Dir: dir,
789 BazelProps: bazel.BazelTargetModuleProperties{
790 Rule_class: "config_setting",
791 },
792 CommonAttrs: ca,
793 ConstraintAttrs: constraintAttributes{},
794 Attrs: &csa,
795 }
796 mod.base().addBp2buildInfo(info)
797}
798
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000799// ApexAvailableTags converts the apex_available property value of an ApexModule
800// module and returns it as a list of keyed tags.
801func ApexAvailableTags(mod Module) bazel.StringListAttribute {
802 attr := bazel.StringListAttribute{}
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000803 // Transform specific attributes into tags.
804 if am, ok := mod.(ApexModule); ok {
805 // TODO(b/218841706): hidl_interface has the apex_available prop, but it's
806 // defined directly as a prop and not via ApexModule, so this doesn't
807 // pick those props up.
Spandan Das2dc6dfc2023-04-17 19:16:06 +0000808 apexAvailable := am.apexModuleBase().ApexAvailable()
809 // If a user does not specify apex_available in Android.bp, then soong provides a default.
810 // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
811 if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
812 apexAvailable = []string{}
813 }
814 attr.Value = ConvertApexAvailableToTags(apexAvailable)
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000815 }
816 return attr
817}
818
Spandan Dasf57a9662023-04-12 19:05:49 +0000819func ApexAvailableTagsWithoutTestApexes(ctx BaseModuleContext, mod Module) bazel.StringListAttribute {
820 attr := bazel.StringListAttribute{}
821 if am, ok := mod.(ApexModule); ok {
822 apexAvailableWithoutTestApexes := removeTestApexes(ctx, am.apexModuleBase().ApexAvailable())
823 // If a user does not specify apex_available in Android.bp, then soong provides a default.
824 // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
825 if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
826 apexAvailableWithoutTestApexes = []string{}
827 }
828 attr.Value = ConvertApexAvailableToTags(apexAvailableWithoutTestApexes)
829 }
830 return attr
831}
832
833func removeTestApexes(ctx BaseModuleContext, apex_available []string) []string {
834 testApexes := []string{}
835 for _, aa := range apex_available {
836 // ignore the wildcards
837 if InList(aa, AvailableToRecognziedWildcards) {
838 continue
839 }
840 mod, _ := ctx.ModuleFromName(aa)
841 if apex, ok := mod.(ApexTestInterface); ok && apex.IsTestApex() {
842 testApexes = append(testApexes, aa)
843 }
844 }
845 return RemoveListFromList(CopyOf(apex_available), testApexes)
846}
847
Cole Faustfb11c1c2023-02-10 11:27:46 -0800848func ConvertApexAvailableToTags(apexAvailable []string) []string {
849 if len(apexAvailable) == 0 {
850 // We need nil specifically to make bp2build not add the tags property at all,
851 // instead of adding it with an empty list
852 return nil
853 }
854 result := make([]string, 0, len(apexAvailable))
855 for _, a := range apexAvailable {
856 result = append(result, "apex_available="+a)
857 }
858 return result
859}
860
Spandan Das39b6cc52023-04-12 19:05:49 +0000861// ConvertApexAvailableToTagsWithoutTestApexes converts a list of apex names to a list of bazel tags
862// This function drops any test apexes from the input.
863func ConvertApexAvailableToTagsWithoutTestApexes(ctx BaseModuleContext, apexAvailable []string) []string {
864 noTestApexes := removeTestApexes(ctx, apexAvailable)
865 return ConvertApexAvailableToTags(noTestApexes)
866}
867
Chris Parsons58852a02021-12-09 18:10:18 -0500868func (t *topDownMutatorContext) createBazelTargetModule(
869 bazelProps bazel.BazelTargetModuleProperties,
870 commonAttrs CommonAttributes,
871 attrs interface{},
872 enabledProperty bazel.BoolAttribute) {
873 constraintAttributes := commonAttrs.fillCommonBp2BuildModuleAttrs(t, enabledProperty)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000874 mod := t.Module()
Liz Kammer2ada09a2021-08-11 00:17:36 -0400875 info := bp2buildInfo{
Spandan Das3131d672023-08-03 22:33:47 +0000876 Dir: dirForBazelTargetGeneration(t, &commonAttrs),
Chris Parsons58852a02021-12-09 18:10:18 -0500877 BazelProps: bazelProps,
878 CommonAttrs: commonAttrs,
879 ConstraintAttrs: constraintAttributes,
880 Attrs: attrs,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500881 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000882 mod.base().addBp2buildInfo(info)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500883}
884
Colin Crossdc35e212019-06-06 16:13:11 -0700885// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
886// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
887// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
888// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
889// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
890
Colin Crosscb55e082019-07-01 15:32:31 -0700891func (t *topDownMutatorContext) MutatorName() string {
892 return t.bp.MutatorName()
893}
894
Colin Crossdc35e212019-06-06 16:13:11 -0700895func (t *topDownMutatorContext) Rename(name string) {
896 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700897 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700898}
899
Liz Kammerf31c9002022-04-26 09:08:55 -0400900func (t *topDownMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
901 return t.bp.CreateModule(factory, name, props...)
902}
903
Colin Crosse003c4a2019-09-25 12:58:36 -0700904func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400905 return createModule(t, factory, "_topDownMutatorModule", props...)
Colin Crossdc35e212019-06-06 16:13:11 -0700906}
907
Liz Kammera060c452021-03-24 10:14:47 -0400908func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400909 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), "", props...).(Module)
Liz Kammera060c452021-03-24 10:14:47 -0400910 return module
911}
912
Colin Crosscb55e082019-07-01 15:32:31 -0700913func (b *bottomUpMutatorContext) MutatorName() string {
914 return b.bp.MutatorName()
915}
916
Colin Crossdc35e212019-06-06 16:13:11 -0700917func (b *bottomUpMutatorContext) Rename(name string) {
918 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700919 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700920}
921
Colin Cross4f1dcb02020-09-16 18:45:04 -0700922func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400923 if b.baseModuleContext.checkedMissingDeps() {
924 panic("Adding deps not allowed after checking for missing deps")
925 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700926 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700927}
928
929func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400930 if b.baseModuleContext.checkedMissingDeps() {
931 panic("Adding deps not allowed after checking for missing deps")
932 }
Colin Crossdc35e212019-06-06 16:13:11 -0700933 b.bp.AddReverseDependency(module, tag, name)
934}
935
Colin Cross43b92e02019-11-18 15:28:57 -0800936func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000937 if b.finalPhase {
938 panic("CreateVariations not allowed in FinalDepsMutators")
939 }
940
Colin Cross9a362232019-07-01 15:32:45 -0700941 modules := b.bp.CreateVariations(variations...)
942
Colin Cross43b92e02019-11-18 15:28:57 -0800943 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700944 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800945 aModules[i] = modules[i].(Module)
946 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700947 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
948 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
949 }
950
Colin Cross43b92e02019-11-18 15:28:57 -0800951 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700952}
953
Colin Cross43b92e02019-11-18 15:28:57 -0800954func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000955 if b.finalPhase {
956 panic("CreateLocalVariations not allowed in FinalDepsMutators")
957 }
958
Colin Cross9a362232019-07-01 15:32:45 -0700959 modules := b.bp.CreateLocalVariations(variations...)
960
Colin Cross43b92e02019-11-18 15:28:57 -0800961 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700962 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800963 aModules[i] = modules[i].(Module)
964 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700965 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
966 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
967 }
968
Colin Cross43b92e02019-11-18 15:28:57 -0800969 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700970}
971
972func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
973 b.bp.SetDependencyVariation(variation)
974}
975
Jiyong Park1d1119f2019-07-29 21:27:18 +0900976func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
977 b.bp.SetDefaultDependencyVariation(variation)
978}
979
Colin Crossdc35e212019-06-06 16:13:11 -0700980func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700981 names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400982 if b.baseModuleContext.checkedMissingDeps() {
983 panic("Adding deps not allowed after checking for missing deps")
984 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700985 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700986}
987
988func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700989 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400990 if b.baseModuleContext.checkedMissingDeps() {
991 panic("Adding deps not allowed after checking for missing deps")
992 }
Colin Crossdc35e212019-06-06 16:13:11 -0700993
Colin Cross4f1dcb02020-09-16 18:45:04 -0700994 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700995}
996
997func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
998 b.bp.AddInterVariantDependency(tag, from, to)
999}
1000
1001func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -04001002 if b.baseModuleContext.checkedMissingDeps() {
1003 panic("Adding deps not allowed after checking for missing deps")
1004 }
Colin Crossdc35e212019-06-06 16:13:11 -07001005 b.bp.ReplaceDependencies(name)
1006}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -08001007
Paul Duffin80342d72020-06-26 22:08:43 +01001008func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
Liz Kammerc13f7852023-05-17 13:01:48 -04001009 if b.baseModuleContext.checkedMissingDeps() {
1010 panic("Adding deps not allowed after checking for missing deps")
1011 }
Paul Duffin80342d72020-06-26 22:08:43 +01001012 b.bp.ReplaceDependenciesIf(name, predicate)
1013}
1014
Jaewoong Jung9f88ce22019-11-15 10:57:34 -08001015func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
1016 b.bp.AliasVariation(variationName)
1017}
Colin Cross1b9604b2020-08-11 12:03:56 -07001018
1019func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
1020 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
1021}
Colin Crossd27e7b82020-07-02 11:38:17 -07001022
1023func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
1024 b.bp.SetVariationProvider(module, provider, value)
1025}