blob: e185cce3874cea101ade893e34651b8bb85ba8f2 [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,
Liz Kammere1b39a52023-09-18 14:32:18 -040058 RegisterPrebuiltsPostDepsMutators,
Liz Kammer356f7d42021-01-26 09:18:53 -050059 },
Spandan Das5af0bd32022-09-28 20:43:08 +000060 bp2buildMutators...)
Liz Kammer356f7d42021-01-26 09:18:53 -050061
Jingwen Chen73850672020-12-14 08:25:34 -050062 // Register bp2build mutators
Spandan Das5af0bd32022-09-28 20:43:08 +000063 for _, f := range allMutators {
Jingwen Chen73850672020-12-14 08:25:34 -050064 f(mctx)
65 }
66
Paul Duffin1d2d42f2021-03-06 20:08:12 +000067 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050068}
69
Paul Duffinc05b0342021-03-06 13:28:13 +000070// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
71// with the InitRegistrationContext and will be used at runtime.
72func collateGloballyRegisteredMutators() sortableComponents {
Liz Kammerc13f7852023-05-17 13:01:48 -040073 // ensure mixed builds mutator is the last mutator
74 finalDeps = append(finalDeps, registerMixedBuildsMutator)
Paul Duffinc05b0342021-03-06 13:28:13 +000075 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
76}
77
78// collateRegisteredMutators constructs a single list of mutators from the separate lists.
79func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070080 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080081
82 register := func(funcs []RegisterMutatorFunc) {
83 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070084 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080085 }
86 }
87
Colin Crosscec81712017-07-13 14:43:27 -070088 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080089
Colin Crosscec81712017-07-13 14:43:27 -070090 register(preDeps)
91
Liz Kammer356f7d42021-01-26 09:18:53 -050092 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070093
94 register(postDeps)
95
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000096 mctx.finalPhase = true
97 register(finalDeps)
98
Paul Duffinc05b0342021-03-06 13:28:13 +000099 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -0700100}
101
102type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000103 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -0500104 finalPhase bool
105 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -0700106}
Colin Cross1e676be2016-10-12 14:38:15 -0700107
108type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -0700109 TopDown(name string, m TopDownMutator) MutatorHandle
110 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -0700111 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200112 Transition(name string, m TransitionMutator)
Colin Cross1e676be2016-10-12 14:38:15 -0700113}
114
115type RegisterMutatorFunc func(RegisterMutatorsContext)
116
Colin Crosscec81712017-07-13 14:43:27 -0700117var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800118 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100119
Paul Duffinaa4162e2020-05-05 11:35:43 +0100120 // Check the visibility rules are valid.
121 //
122 // This must run after the package renamer mutators so that any issues found during
123 // validation of the package's default_visibility property are reported using the
124 // correct package name and not the synthetic name.
125 //
126 // This must also be run before defaults mutators as the rules for validation are
127 // different before checking the rules than they are afterwards. e.g.
128 // visibility: ["//visibility:private", "//visibility:public"]
129 // would be invalid if specified in a module definition but is valid if it results
130 // from something like this:
131 //
132 // defaults {
133 // name: "defaults",
134 // // Be inaccessible outside a package by default.
135 // visibility: ["//visibility:private"]
136 // }
137 //
138 // defaultable_module {
139 // name: "defaultable_module",
140 // defaults: ["defaults"],
141 // // Override the default.
142 // visibility: ["//visibility:public"]
143 // }
144 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000145 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100146
Bob Badour37af0462021-01-07 03:34:31 +0000147 // Record the default_applicable_licenses for each package.
148 //
149 // This must run before the defaults so that defaults modules can pick up the package default.
150 RegisterLicensesPackageMapper,
151
Paul Duffinaa4162e2020-05-05 11:35:43 +0100152 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100153 //
154 // Any mutators that are added before this will not see any modules created by
155 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700156 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100157
Paul Duffin44f1d842020-06-26 20:17:02 +0100158 // Add dependencies on any components so that any component references can be
159 // resolved within the deps mutator.
160 //
161 // Must be run after defaults so it can be used to create dependencies on the
162 // component modules that are creating in a DefaultableHook.
163 //
164 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
165 // renamed. That is so that if a module creates components using a prebuilt module
166 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
167 // the prebuilt module and not the source module.
168 RegisterComponentsMutator,
169
Paul Duffinc988c8e2020-04-29 18:27:14 +0100170 // Create an association between prebuilt modules and their corresponding source
171 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100172 //
173 // Must be run after defaults mutators to ensure that any modules created by
174 // a DefaultableHook can be either a prebuilt or a source module with a matching
175 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100176 RegisterPrebuiltsPreArchMutators,
177
Bob Badour37af0462021-01-07 03:34:31 +0000178 // Gather the licenses properties for all modules for use during expansion and enforcement.
179 //
180 // This must come after the defaults mutators to ensure that any licenses supplied
181 // in a defaults module has been successfully applied before the rules are gathered.
182 RegisterLicensesPropertyGatherer,
183
Paul Duffinaa4162e2020-05-05 11:35:43 +0100184 // Gather the visibility rules for all modules for us during visibility enforcement.
185 //
186 // This must come after the defaults mutators to ensure that any visibility supplied
187 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000188 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700189}
190
Colin Crossae4c6182017-09-15 17:33:55 -0700191func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700192 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800193 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700194 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700195}
196
Colin Crosscec81712017-07-13 14:43:27 -0700197var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700198 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700199}
200
201var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800202 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700203 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000204 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000205 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100206 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700207 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700208}
Colin Cross1e676be2016-10-12 14:38:15 -0700209
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000210var finalDeps = []RegisterMutatorFunc{}
211
Colin Cross1e676be2016-10-12 14:38:15 -0700212func PreArchMutators(f RegisterMutatorFunc) {
213 preArch = append(preArch, f)
214}
215
216func PreDepsMutators(f RegisterMutatorFunc) {
217 preDeps = append(preDeps, f)
218}
219
220func PostDepsMutators(f RegisterMutatorFunc) {
221 postDeps = append(postDeps, f)
222}
223
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000224func FinalDepsMutators(f RegisterMutatorFunc) {
225 finalDeps = append(finalDeps, f)
226}
227
Liz Kammer356f7d42021-01-26 09:18:53 -0500228var bp2buildPreArchMutators = []RegisterMutatorFunc{}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400229
Liz Kammer12615db2021-09-28 09:19:17 -0400230// A minimal context for Bp2build conversion
231type Bp2buildMutatorContext interface {
232 BazelConversionPathContext
233
234 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
Spandan Dasf26ee152023-08-24 23:21:04 +0000235 CreateBazelTargetModuleWithRestrictions(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}, bazel.BoolAttribute)
Liz Kammer12615db2021-09-28 09:19:17 -0400236}
237
Liz Kammer356f7d42021-01-26 09:18:53 -0500238// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
239// into Bazel BUILD targets that should run prior to deps and conversion.
240func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
241 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
242}
243
Colin Cross9f35c3d2020-09-16 19:04:41 -0700244type BaseMutatorContext interface {
245 BaseModuleContext
246
247 // MutatorName returns the name that this mutator was registered with.
248 MutatorName() string
249
250 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
251 // AddDependency or OtherModuleName until after this mutator pass is complete.
252 Rename(name string)
253}
254
Colin Cross25de6c32019-06-06 14:29:25 -0700255type TopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700256
Colin Cross635c3b02016-05-18 15:37:25 -0700257type TopDownMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700258 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700259
Colin Cross9f35c3d2020-09-16 19:04:41 -0700260 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
261 // the specified property structs to it as if the properties were set in a blueprint file.
Colin Crosse003c4a2019-09-25 12:58:36 -0700262 CreateModule(ModuleFactory, ...interface{}) Module
Jingwen Chen1fd14692021-02-05 03:01:50 -0500263
264 // CreateBazelTargetModule creates a BazelTargetModule by calling the
265 // factory method, just like in CreateModule, but also requires
266 // BazelTargetModuleProperties containing additional metadata for the
267 // bp2build codegenerator.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000268 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
Chris Parsons58852a02021-12-09 18:10:18 -0500269
270 // CreateBazelTargetModuleWithRestrictions creates a BazelTargetModule by calling the
271 // factory method, just like in CreateModule, but also requires
272 // BazelTargetModuleProperties containing additional metadata for the
273 // bp2build codegenerator. The generated target is restricted to only be buildable for certain
274 // platforms, as dictated by a given bool attribute: the target will not be buildable in
275 // any platform for which this bool attribute is false.
276 CreateBazelTargetModuleWithRestrictions(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}, bazel.BoolAttribute)
Spandan Dasabedff02023-03-07 19:24:34 +0000277
Chris Parsons39a16972023-06-08 14:28:51 +0000278 // MarkBp2buildUnconvertible registers the current module as "unconvertible to bp2build" for the
279 // given reason.
280 MarkBp2buildUnconvertible(reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string)
281
Spandan Dasabedff02023-03-07 19:24:34 +0000282 // CreateBazelTargetAliasInDir creates an alias definition in `dir` directory.
283 // This function can be used to create alias definitions in a directory that is different
284 // from the directory of the visited Soong module.
285 CreateBazelTargetAliasInDir(dir string, name string, actual bazel.Label)
Spandan Das6a448ec2023-04-19 17:36:12 +0000286
287 // CreateBazelConfigSetting creates a config_setting in <dir>/BUILD.bazel
288 // build/bazel has several static config_setting(s) that are used in Bazel builds.
289 // This function can be used to createa additional config_setting(s) based on the build graph
290 // (e.g. a config_setting specific to an apex variant)
291 CreateBazelConfigSetting(csa bazel.ConfigSettingAttributes, ca CommonAttributes, dir string)
Colin Cross6362e272015-10-29 15:25:03 -0700292}
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}