blob: 3d596554d2c69c19ce0b4e8755abd2f2037f217f [file] [log] [blame]
Colin Cross6362e272015-10-29 15:25:03 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross6362e272015-10-29 15:25:03 -070016
Colin Cross795c3772017-03-16 16:50:10 -070017import (
Chris Parsons637458d2023-09-19 20:09:00 +000018 "path/filepath"
19
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000020 "android/soong/bazel"
Chris Parsons39a16972023-06-08 14:28:51 +000021 "android/soong/ui/metrics/bp2build_metrics_proto"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000022
Colin Cross795c3772017-03-16 16:50:10 -070023 "github.com/google/blueprint"
24)
Colin Cross6362e272015-10-29 15:25:03 -070025
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070026// Phases:
27// run Pre-arch mutators
28// run archMutator
29// run Pre-deps mutators
30// run depsMutator
31// run PostDeps mutators
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000032// run FinalDeps mutators (CreateVariations disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070033// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070034
Jingwen Chen73850672020-12-14 08:25:34 -050035// RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
Liz Kammerbe46fcc2021-11-01 15:32:43 -040036func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) {
Spandan Das5af0bd32022-09-28 20:43:08 +000037 bp2buildMutators := append(preArchMutators, registerBp2buildConversionMutator)
Chris Parsons5f1b3c72023-09-28 20:41:03 +000038 if ctx.config.Bp2buildDepsMutator {
39 bp2buildMutators = append(bp2buildMutators, registerBp2buildDepsMutator)
40 }
Spandan Das5af0bd32022-09-28 20:43:08 +000041 registerMutatorsForBazelConversion(ctx, bp2buildMutators)
42}
43
Spandan Das5af0bd32022-09-28 20:43:08 +000044func registerMutatorsForBazelConversion(ctx *Context, bp2buildMutators []RegisterMutatorFunc) {
Liz Kammer356f7d42021-01-26 09:18:53 -050045 mctx := &registerMutatorsContext{
46 bazelConversionMode: true,
Jingwen Chen041b1842021-02-01 00:23:25 -050047 }
48
Spandan Das5af0bd32022-09-28 20:43:08 +000049 allMutators := append([]RegisterMutatorFunc{
Liz Kammer356f7d42021-01-26 09:18:53 -050050 RegisterNamespaceMutator,
51 RegisterDefaultsPreArchMutators,
52 // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should
53 // evaluate the impact on conversion.
54 RegisterPrebuiltsPreArchMutators,
Liz Kammere1b39a52023-09-18 14:32:18 -040055 RegisterPrebuiltsPostDepsMutators,
Liz Kammer356f7d42021-01-26 09:18:53 -050056 },
Spandan Das5af0bd32022-09-28 20:43:08 +000057 bp2buildMutators...)
Liz Kammer356f7d42021-01-26 09:18:53 -050058
Jingwen Chen73850672020-12-14 08:25:34 -050059 // Register bp2build mutators
Spandan Das5af0bd32022-09-28 20:43:08 +000060 for _, f := range allMutators {
Jingwen Chen73850672020-12-14 08:25:34 -050061 f(mctx)
62 }
63
Paul Duffin1d2d42f2021-03-06 20:08:12 +000064 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050065}
66
Paul Duffinc05b0342021-03-06 13:28:13 +000067// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
68// with the InitRegistrationContext and will be used at runtime.
69func collateGloballyRegisteredMutators() sortableComponents {
Liz Kammerc13f7852023-05-17 13:01:48 -040070 // ensure mixed builds mutator is the last mutator
71 finalDeps = append(finalDeps, registerMixedBuildsMutator)
Paul Duffinc05b0342021-03-06 13:28:13 +000072 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
73}
74
75// collateRegisteredMutators constructs a single list of mutators from the separate lists.
76func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070077 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080078
79 register := func(funcs []RegisterMutatorFunc) {
80 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070081 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080082 }
83 }
84
Colin Crosscec81712017-07-13 14:43:27 -070085 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080086
Colin Crosscec81712017-07-13 14:43:27 -070087 register(preDeps)
88
Liz Kammer356f7d42021-01-26 09:18:53 -050089 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070090
91 register(postDeps)
92
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000093 mctx.finalPhase = true
94 register(finalDeps)
95
Paul Duffinc05b0342021-03-06 13:28:13 +000096 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -070097}
98
99type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000100 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -0500101 finalPhase bool
102 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -0700103}
Colin Cross1e676be2016-10-12 14:38:15 -0700104
105type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -0700106 TopDown(name string, m TopDownMutator) MutatorHandle
107 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -0700108 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200109 Transition(name string, m TransitionMutator)
Colin Cross1e676be2016-10-12 14:38:15 -0700110}
111
112type RegisterMutatorFunc func(RegisterMutatorsContext)
113
Colin Crosscec81712017-07-13 14:43:27 -0700114var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800115 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100116
Paul Duffinaa4162e2020-05-05 11:35:43 +0100117 // Check the visibility rules are valid.
118 //
119 // This must run after the package renamer mutators so that any issues found during
120 // validation of the package's default_visibility property are reported using the
121 // correct package name and not the synthetic name.
122 //
123 // This must also be run before defaults mutators as the rules for validation are
124 // different before checking the rules than they are afterwards. e.g.
125 // visibility: ["//visibility:private", "//visibility:public"]
126 // would be invalid if specified in a module definition but is valid if it results
127 // from something like this:
128 //
129 // defaults {
130 // name: "defaults",
131 // // Be inaccessible outside a package by default.
132 // visibility: ["//visibility:private"]
133 // }
134 //
135 // defaultable_module {
136 // name: "defaultable_module",
137 // defaults: ["defaults"],
138 // // Override the default.
139 // visibility: ["//visibility:public"]
140 // }
141 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000142 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100143
Bob Badour37af0462021-01-07 03:34:31 +0000144 // Record the default_applicable_licenses for each package.
145 //
146 // This must run before the defaults so that defaults modules can pick up the package default.
147 RegisterLicensesPackageMapper,
148
Paul Duffinaa4162e2020-05-05 11:35:43 +0100149 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100150 //
151 // Any mutators that are added before this will not see any modules created by
152 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700153 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100154
Paul Duffin44f1d842020-06-26 20:17:02 +0100155 // Add dependencies on any components so that any component references can be
156 // resolved within the deps mutator.
157 //
158 // Must be run after defaults so it can be used to create dependencies on the
159 // component modules that are creating in a DefaultableHook.
160 //
161 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
162 // renamed. That is so that if a module creates components using a prebuilt module
163 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
164 // the prebuilt module and not the source module.
165 RegisterComponentsMutator,
166
Paul Duffinc988c8e2020-04-29 18:27:14 +0100167 // Create an association between prebuilt modules and their corresponding source
168 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100169 //
170 // Must be run after defaults mutators to ensure that any modules created by
171 // a DefaultableHook can be either a prebuilt or a source module with a matching
172 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100173 RegisterPrebuiltsPreArchMutators,
174
Bob Badour37af0462021-01-07 03:34:31 +0000175 // Gather the licenses properties for all modules for use during expansion and enforcement.
176 //
177 // This must come after the defaults mutators to ensure that any licenses supplied
178 // in a defaults module has been successfully applied before the rules are gathered.
179 RegisterLicensesPropertyGatherer,
180
Paul Duffinaa4162e2020-05-05 11:35:43 +0100181 // Gather the visibility rules for all modules for us during visibility enforcement.
182 //
183 // This must come after the defaults mutators to ensure that any visibility supplied
184 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000185 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700186}
187
Colin Crossae4c6182017-09-15 17:33:55 -0700188func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700189 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800190 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700191 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700192}
193
Colin Crosscec81712017-07-13 14:43:27 -0700194var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700195 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700196}
197
198var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800199 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700200 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000201 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000202 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100203 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700204 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700205}
Colin Cross1e676be2016-10-12 14:38:15 -0700206
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000207var finalDeps = []RegisterMutatorFunc{}
208
Colin Cross1e676be2016-10-12 14:38:15 -0700209func PreArchMutators(f RegisterMutatorFunc) {
210 preArch = append(preArch, f)
211}
212
213func PreDepsMutators(f RegisterMutatorFunc) {
214 preDeps = append(preDeps, f)
215}
216
217func PostDepsMutators(f RegisterMutatorFunc) {
218 postDeps = append(postDeps, f)
219}
220
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000221func FinalDepsMutators(f RegisterMutatorFunc) {
222 finalDeps = append(finalDeps, f)
223}
224
Liz Kammer356f7d42021-01-26 09:18:53 -0500225var bp2buildPreArchMutators = []RegisterMutatorFunc{}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400226
Liz Kammer12615db2021-09-28 09:19:17 -0400227// A minimal context for Bp2build conversion
228type Bp2buildMutatorContext interface {
229 BazelConversionPathContext
Colin Cross9f35c3d2020-09-16 19:04:41 -0700230 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700231
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000232 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
233 // dependency (some entries may be nil).
234 //
235 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
236 // new dependencies have had the current mutator called on them. If the mutator is not
237 // parallel this method does not affect the ordering of the current mutator pass, but will
238 // be ordered correctly for all future mutator passes.
239 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
240
Jingwen Chen1fd14692021-02-05 03:01:50 -0500241 // CreateBazelTargetModule creates a BazelTargetModule by calling the
242 // factory method, just like in CreateModule, but also requires
243 // BazelTargetModuleProperties containing additional metadata for the
244 // bp2build codegenerator.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000245 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
Chris Parsons58852a02021-12-09 18:10:18 -0500246
247 // CreateBazelTargetModuleWithRestrictions creates a BazelTargetModule by calling the
248 // factory method, just like in CreateModule, but also requires
249 // BazelTargetModuleProperties containing additional metadata for the
250 // bp2build codegenerator. The generated target is restricted to only be buildable for certain
251 // platforms, as dictated by a given bool attribute: the target will not be buildable in
252 // any platform for which this bool attribute is false.
253 CreateBazelTargetModuleWithRestrictions(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}, bazel.BoolAttribute)
Spandan Dasabedff02023-03-07 19:24:34 +0000254
Chris Parsons39a16972023-06-08 14:28:51 +0000255 // MarkBp2buildUnconvertible registers the current module as "unconvertible to bp2build" for the
256 // given reason.
257 MarkBp2buildUnconvertible(reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string)
258
Spandan Dasabedff02023-03-07 19:24:34 +0000259 // CreateBazelTargetAliasInDir creates an alias definition in `dir` directory.
260 // This function can be used to create alias definitions in a directory that is different
261 // from the directory of the visited Soong module.
262 CreateBazelTargetAliasInDir(dir string, name string, actual bazel.Label)
Spandan Das6a448ec2023-04-19 17:36:12 +0000263
264 // CreateBazelConfigSetting creates a config_setting in <dir>/BUILD.bazel
265 // build/bazel has several static config_setting(s) that are used in Bazel builds.
266 // This function can be used to createa additional config_setting(s) based on the build graph
267 // (e.g. a config_setting specific to an apex variant)
268 CreateBazelConfigSetting(csa bazel.ConfigSettingAttributes, ca CommonAttributes, dir string)
Colin Cross6362e272015-10-29 15:25:03 -0700269}
270
Chris Parsons637458d2023-09-19 20:09:00 +0000271// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
272// into Bazel BUILD targets that should run prior to deps and conversion.
273func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
274 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
275}
276
277type BaseMutatorContext interface {
278 BaseModuleContext
279
280 // MutatorName returns the name that this mutator was registered with.
281 MutatorName() string
282
283 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
284 // AddDependency or OtherModuleName until after this mutator pass is complete.
285 Rename(name string)
286}
287
288type TopDownMutator func(TopDownMutatorContext)
289
290type TopDownMutatorContext interface {
291 BaseMutatorContext
Chris Parsons637458d2023-09-19 20:09:00 +0000292
293 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
294 // the specified property structs to it as if the properties were set in a blueprint file.
295 CreateModule(ModuleFactory, ...interface{}) Module
296}
297
Colin Cross25de6c32019-06-06 14:29:25 -0700298type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700299 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700300 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700301}
302
Colin Cross25de6c32019-06-06 14:29:25 -0700303type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700304
Colin Cross635c3b02016-05-18 15:37:25 -0700305type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700306 BaseMutatorContext
Chris Parsons6666d0f2023-09-22 16:21:53 +0000307 Bp2buildMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800308
Colin Cross9f35c3d2020-09-16 19:04:41 -0700309 // AddReverseDependency adds a dependency from the destination to the given module.
310 // Does not affect the ordering of the current mutator pass, but will be ordered
311 // correctly for all future mutator passes. All reverse dependencies for a destination module are
312 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
313 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800314 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700315
316 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
317 // parameter. It returns a list of new modules in the same order as the variationNames
318 // list.
319 //
320 // If any of the dependencies of the module being operated on were already split
321 // by calling CreateVariations with the same name, the dependency will automatically
322 // be updated to point the matching variant.
323 //
324 // If a module is split, and then a module depending on the first module is not split
325 // when the Mutator is later called on it, the dependency of the depending module will
326 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800327 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700328
329 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
330 // parameter. It returns a list of new modules in the same order as the variantNames
331 // list.
332 //
333 // Local variations do not affect automatic dependency resolution - dependencies added
334 // to the split module via deps or DynamicDependerModule must exactly match a variant
335 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800336 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700337
338 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
339 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800340 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700341
342 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
343 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900344 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700345
346 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700347 // argument to select which variant of the dependency to use. It returns a slice of modules for
348 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
Usta Shresthac725f472022-01-11 02:44:21 -0500349 // all the non-local variations of the current module, plus the variations argument.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700350 //
351 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
352 // new dependencies have had the current mutator called on them. If the mutator is not
353 // parallel this method does not affect the ordering of the current mutator pass, but will
354 // be ordered correctly for all future mutator passes.
Usta Shresthac725f472022-01-11 02:44:21 -0500355 AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700356
357 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700358 // variations argument to select which variant of the dependency to use. It returns a slice of
359 // modules for each dependency (some entries may be nil). A variant of the dependency must
360 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700361 // For any unspecified variation the first variant will be used.
362 //
363 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
364 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700365 //
366 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
367 // new dependencies have had the current mutator called on them. If the mutator is not
368 // parallel this method does not affect the ordering of the current mutator pass, but will
369 // be ordered correctly for all future mutator passes.
370 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700371
372 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
373 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
374 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
375 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800376 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700377
378 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
379 // specified name with the current variant of this module. Replacements don't take effect until
380 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800381 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700382
383 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
384 // specified name with the current variant of this module as long as the supplied predicate returns
385 // true.
386 //
387 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100388 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700389
390 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
391 // and creates an alias from the current variant (before the mutator has run) to the new
392 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
393 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
394 // be used to add dependencies on the newly created variant using the variant map from
395 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800396 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700397
398 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
399 // module, and creates an alias from a new fromVariationName variant the toVariationName
400 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
401 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
402 // be used to add dependencies on the toVariationName variant using the fromVariationName
403 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700404 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700405
406 // SetVariationProvider sets the value for a provider for the given newly created variant of
407 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
408 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
409 // if the value is not of the appropriate type, or if the module is not a newly created
410 // variant of the current module. The value should not be modified after being passed to
411 // SetVariationProvider.
412 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700413}
414
Colin Cross25de6c32019-06-06 14:29:25 -0700415type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700416 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700417 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400418 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700419}
420
Colin Cross617b88a2020-08-24 18:04:09 -0700421func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500422 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700423
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400424 moduleContext := a.base().baseModuleContextFactory(ctx)
425 moduleContext.bazelConversionMode = bazelConversionMode
426
Colin Cross617b88a2020-08-24 18:04:09 -0700427 return &bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400428 bp: ctx,
Cole Faust0abd4b42023-01-10 10:49:18 -0800429 baseModuleContext: moduleContext,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400430 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700431 }
432}
433
Colin Cross25de6c32019-06-06 14:29:25 -0700434func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000435 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500436 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700437 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700438 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500439 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700440 }
Colin Cross798bfce2016-10-12 14:28:16 -0700441 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500442 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700443 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700444 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700445}
446
Colin Cross617b88a2020-08-24 18:04:09 -0700447func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
448 mutator := &mutator{name: name, bottomUpMutator: m}
449 x.mutators = append(x.mutators, mutator)
450 return mutator
451}
452
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200453type IncomingTransitionContext interface {
454 // Module returns the target of the dependency edge for which the transition
455 // is being computed
456 Module() Module
457
458 // Config returns the configuration for the build.
459 Config() Config
460}
461
462type OutgoingTransitionContext interface {
463 // Module returns the target of the dependency edge for which the transition
464 // is being computed
465 Module() Module
466
467 // DepTag() Returns the dependency tag through which this dependency is
468 // reached
469 DepTag() blueprint.DependencyTag
470}
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200471
472// Transition mutators implement a top-down mechanism where a module tells its
473// direct dependencies what variation they should be built in but the dependency
474// has the final say.
475//
476// When implementing a transition mutator, one needs to implement four methods:
477// - Split() that tells what variations a module has by itself
478// - OutgoingTransition() where a module tells what it wants from its
479// dependency
480// - IncomingTransition() where a module has the final say about its own
481// variation
482// - Mutate() that changes the state of a module depending on its variation
483//
484// That the effective variation of module B when depended on by module A is the
485// composition the outgoing transition of module A and the incoming transition
486// of module B.
487//
488// the outgoing transition should not take the properties of the dependency into
489// account, only those of the module that depends on it. For this reason, the
490// dependency is not even passed into it as an argument. Likewise, the incoming
491// transition should not take the properties of the depending module into
492// account and is thus not informed about it. This makes for a nice
493// decomposition of the decision logic.
494//
495// A given transition mutator only affects its own variation; other variations
496// stay unchanged along the dependency edges.
497//
498// Soong makes sure that all modules are created in the desired variations and
499// that dependency edges are set up correctly. This ensures that "missing
500// variation" errors do not happen and allows for more flexible changes in the
501// value of the variation among dependency edges (as oppposed to bottom-up
502// mutators where if module A in variation X depends on module B and module B
503// has that variation X, A must depend on variation X of B)
504//
505// The limited power of the context objects passed to individual mutators
506// methods also makes it more difficult to shoot oneself in the foot. Complete
507// safety is not guaranteed because no one prevents individual transition
508// mutators from mutating modules in illegal ways and for e.g. Split() or
509// Mutate() to run their own visitations of the transitive dependency of the
510// module and both of these are bad ideas, but it's better than no guardrails at
511// all.
512//
513// This model is pretty close to Bazel's configuration transitions. The mapping
514// between concepts in Soong and Bazel is as follows:
515// - Module == configured target
516// - Variant == configuration
517// - Variation name == configuration flag
518// - Variation == configuration flag value
519// - Outgoing transition == attribute transition
520// - Incoming transition == rule transition
521//
522// The Split() method does not have a Bazel equivalent and Bazel split
523// transitions do not have a Soong equivalent.
524//
525// Mutate() does not make sense in Bazel due to the different models of the
526// two systems: when creating new variations, Soong clones the old module and
527// thus some way is needed to change it state whereas Bazel creates each
528// configuration of a given configured target anew.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200529type TransitionMutator interface {
530 // Split returns the set of variations that should be created for a module no
531 // matter who depends on it. Used when Make depends on a particular variation
532 // or when the module knows its variations just based on information given to
533 // it in the Blueprint file. This method should not mutate the module it is
534 // called on.
535 Split(ctx BaseModuleContext) []string
536
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200537 // Called on a module to determine which variation it wants from its direct
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200538 // dependencies. The dependency itself can override this decision. This method
539 // should not mutate the module itself.
540 OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string
541
542 // Called on a module to determine which variation it should be in based on
543 // the variation modules that depend on it want. This gives the module a final
544 // say about its own variations. This method should not mutate the module
545 // itself.
546 IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string
547
548 // Called after a module was split into multiple variations on each variation.
549 // It should not split the module any further but adding new dependencies is
550 // fine. Unlike all the other methods on TransitionMutator, this method is
551 // allowed to mutate the module.
552 Mutate(ctx BottomUpMutatorContext, variation string)
553}
554
555type androidTransitionMutator struct {
556 finalPhase bool
557 bazelConversionMode bool
558 mutator TransitionMutator
559}
560
561func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []string {
562 if m, ok := ctx.Module().(Module); ok {
563 moduleContext := m.base().baseModuleContextFactory(ctx)
564 moduleContext.bazelConversionMode = a.bazelConversionMode
565 return a.mutator.Split(&moduleContext)
566 } else {
567 return []string{""}
568 }
569}
570
571type outgoingTransitionContextImpl struct {
572 bp blueprint.OutgoingTransitionContext
573}
574
575func (c *outgoingTransitionContextImpl) Module() Module {
576 return c.bp.Module().(Module)
577}
578
579func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag {
580 return c.bp.DepTag()
581}
582
583func (a *androidTransitionMutator) OutgoingTransition(ctx blueprint.OutgoingTransitionContext, sourceVariation string) string {
584 if _, ok := ctx.Module().(Module); ok {
585 return a.mutator.OutgoingTransition(&outgoingTransitionContextImpl{bp: ctx}, sourceVariation)
586 } else {
587 return ""
588 }
589}
590
591type incomingTransitionContextImpl struct {
592 bp blueprint.IncomingTransitionContext
593}
594
595func (c *incomingTransitionContextImpl) Module() Module {
596 return c.bp.Module().(Module)
597}
598
599func (c *incomingTransitionContextImpl) Config() Config {
600 return c.bp.Config().(Config)
601}
602
603func (a *androidTransitionMutator) IncomingTransition(ctx blueprint.IncomingTransitionContext, incomingVariation string) string {
604 if _, ok := ctx.Module().(Module); ok {
605 return a.mutator.IncomingTransition(&incomingTransitionContextImpl{bp: ctx}, incomingVariation)
606 } else {
607 return ""
608 }
609}
610
611func (a *androidTransitionMutator) Mutate(ctx blueprint.BottomUpMutatorContext, variation string) {
612 if am, ok := ctx.Module().(Module); ok {
613 a.mutator.Mutate(bottomUpMutatorContextFactory(ctx, am, a.finalPhase, a.bazelConversionMode), variation)
614 }
615}
616
617func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) {
618 atm := &androidTransitionMutator{
619 finalPhase: x.finalPhase,
620 bazelConversionMode: x.bazelConversionMode,
621 mutator: m,
622 }
623 mutator := &mutator{
624 name: name,
625 transitionMutator: atm}
626 x.mutators = append(x.mutators, mutator)
627}
628
Liz Kammer356f7d42021-01-26 09:18:53 -0500629func (x *registerMutatorsContext) mutatorName(name string) string {
630 if x.bazelConversionMode {
631 return name + "_bp2build"
632 }
633 return name
634}
635
Colin Cross25de6c32019-06-06 14:29:25 -0700636func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700637 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700638 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400639 moduleContext := a.base().baseModuleContextFactory(ctx)
640 moduleContext.bazelConversionMode = x.bazelConversionMode
Colin Cross25de6c32019-06-06 14:29:25 -0700641 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700642 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400643 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700644 }
Colin Cross798bfce2016-10-12 14:28:16 -0700645 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700646 }
Colin Cross798bfce2016-10-12 14:28:16 -0700647 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500648 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700649 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700650 return mutator
651}
652
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000653func (mutator *mutator) componentName() string {
654 return mutator.name
655}
656
657func (mutator *mutator) register(ctx *Context) {
658 blueprintCtx := ctx.Context
659 var handle blueprint.MutatorHandle
660 if mutator.bottomUpMutator != nil {
661 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
662 } else if mutator.topDownMutator != nil {
663 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200664 } else if mutator.transitionMutator != nil {
665 blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000666 }
667 if mutator.parallel {
668 handle.Parallel()
669 }
670}
671
Colin Cross798bfce2016-10-12 14:28:16 -0700672type MutatorHandle interface {
673 Parallel() MutatorHandle
674}
675
676func (mutator *mutator) Parallel() MutatorHandle {
677 mutator.parallel = true
678 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700679}
Colin Cross1e676be2016-10-12 14:38:15 -0700680
Paul Duffin44f1d842020-06-26 20:17:02 +0100681func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
682 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
683}
684
685// A special mutator that runs just prior to the deps mutator to allow the dependencies
686// on component modules to be added so that they can depend directly on a prebuilt
687// module.
688func componentDepsMutator(ctx BottomUpMutatorContext) {
689 if m := ctx.Module(); m.Enabled() {
690 m.ComponentDepsMutator(ctx)
691 }
692}
693
Colin Cross1e676be2016-10-12 14:38:15 -0700694func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100695 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700696 m.DepsMutator(ctx)
697 }
698}
Colin Crossd11fcda2017-10-23 17:59:01 -0700699
Liz Kammer356f7d42021-01-26 09:18:53 -0500700func registerDepsMutator(ctx RegisterMutatorsContext) {
701 ctx.BottomUp("deps", depsMutator).Parallel()
702}
703
704func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
705 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
706 // being converted to build targets.
707 ctx.BottomUp("deps", depsMutator).Parallel()
708}
709
Chris Parsons6666d0f2023-09-22 16:21:53 +0000710func (t *bottomUpMutatorContext) CreateBazelTargetModule(
Jingwen Chen1fd14692021-02-05 03:01:50 -0500711 bazelProps bazel.BazelTargetModuleProperties,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000712 commonAttrs CommonAttributes,
Liz Kammer2ada09a2021-08-11 00:17:36 -0400713 attrs interface{}) {
Chris Parsons58852a02021-12-09 18:10:18 -0500714 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, bazel.BoolAttribute{})
715}
716
Chris Parsons6666d0f2023-09-22 16:21:53 +0000717func (t *bottomUpMutatorContext) CreateBazelTargetModuleWithRestrictions(
Chris Parsons58852a02021-12-09 18:10:18 -0500718 bazelProps bazel.BazelTargetModuleProperties,
719 commonAttrs CommonAttributes,
720 attrs interface{},
721 enabledProperty bazel.BoolAttribute) {
722 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, enabledProperty)
723}
724
Chris Parsons6666d0f2023-09-22 16:21:53 +0000725func (t *bottomUpMutatorContext) MarkBp2buildUnconvertible(
Chris Parsons39a16972023-06-08 14:28:51 +0000726 reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string) {
727 mod := t.Module()
728 mod.base().setBp2buildUnconvertible(reasonType, detail)
729}
730
Spandan Dasabedff02023-03-07 19:24:34 +0000731var (
732 bazelAliasModuleProperties = bazel.BazelTargetModuleProperties{
733 Rule_class: "alias",
734 }
735)
736
737type bazelAliasAttributes struct {
738 Actual *bazel.LabelAttribute
739}
740
Chris Parsons6666d0f2023-09-22 16:21:53 +0000741func (t *bottomUpMutatorContext) CreateBazelTargetAliasInDir(
Spandan Dasabedff02023-03-07 19:24:34 +0000742 dir string,
743 name string,
744 actual bazel.Label) {
745 mod := t.Module()
746 attrs := &bazelAliasAttributes{
747 Actual: bazel.MakeLabelAttribute(actual.Label),
748 }
749 info := bp2buildInfo{
750 Dir: dir,
751 BazelProps: bazelAliasModuleProperties,
752 CommonAttrs: CommonAttributes{Name: name},
753 ConstraintAttrs: constraintAttributes{},
754 Attrs: attrs,
755 }
756 mod.base().addBp2buildInfo(info)
757}
758
Spandan Das3131d672023-08-03 22:33:47 +0000759// Returns the directory in which the bazel target will be generated
760// If ca.Dir is not nil, use that
761// Otherwise default to the directory of the soong module
Chris Parsons6666d0f2023-09-22 16:21:53 +0000762func dirForBazelTargetGeneration(t *bottomUpMutatorContext, ca *CommonAttributes) string {
Spandan Das3131d672023-08-03 22:33:47 +0000763 dir := t.OtherModuleDir(t.Module())
764 if ca.Dir != nil {
765 dir = *ca.Dir
766 // Restrict its use to dirs that contain an Android.bp file.
767 // There are several places in bp2build where we use the existence of Android.bp/BUILD on the filesystem
768 // to curate a compatible label for src files (e.g. headers for cc).
769 // If we arbritrarily create BUILD files, then it might render those curated labels incompatible.
770 if exists, _, _ := t.Config().fs.Exists(filepath.Join(dir, "Android.bp")); !exists {
771 t.ModuleErrorf("Cannot use ca.Dir to create a BazelTarget in dir: %v since it does not contain an Android.bp file", dir)
772 }
773
774 // Set ca.Dir to nil so that it does not get emitted to the BUILD files
775 ca.Dir = nil
776 }
777 return dir
778}
779
Chris Parsons6666d0f2023-09-22 16:21:53 +0000780func (t *bottomUpMutatorContext) CreateBazelConfigSetting(
Spandan Das6a448ec2023-04-19 17:36:12 +0000781 csa bazel.ConfigSettingAttributes,
782 ca CommonAttributes,
783 dir string) {
784 mod := t.Module()
785 info := bp2buildInfo{
786 Dir: dir,
787 BazelProps: bazel.BazelTargetModuleProperties{
788 Rule_class: "config_setting",
789 },
790 CommonAttrs: ca,
791 ConstraintAttrs: constraintAttributes{},
792 Attrs: &csa,
793 }
794 mod.base().addBp2buildInfo(info)
795}
796
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000797// ApexAvailableTags converts the apex_available property value of an ApexModule
798// module and returns it as a list of keyed tags.
799func ApexAvailableTags(mod Module) bazel.StringListAttribute {
800 attr := bazel.StringListAttribute{}
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000801 // Transform specific attributes into tags.
802 if am, ok := mod.(ApexModule); ok {
803 // TODO(b/218841706): hidl_interface has the apex_available prop, but it's
804 // defined directly as a prop and not via ApexModule, so this doesn't
805 // pick those props up.
Spandan Das2dc6dfc2023-04-17 19:16:06 +0000806 apexAvailable := am.apexModuleBase().ApexAvailable()
807 // If a user does not specify apex_available in Android.bp, then soong provides a default.
808 // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
809 if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
810 apexAvailable = []string{}
811 }
812 attr.Value = ConvertApexAvailableToTags(apexAvailable)
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000813 }
814 return attr
815}
816
Spandan Dasf57a9662023-04-12 19:05:49 +0000817func ApexAvailableTagsWithoutTestApexes(ctx BaseModuleContext, mod Module) bazel.StringListAttribute {
818 attr := bazel.StringListAttribute{}
819 if am, ok := mod.(ApexModule); ok {
820 apexAvailableWithoutTestApexes := removeTestApexes(ctx, am.apexModuleBase().ApexAvailable())
821 // If a user does not specify apex_available in Android.bp, then soong provides a default.
822 // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
823 if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
824 apexAvailableWithoutTestApexes = []string{}
825 }
826 attr.Value = ConvertApexAvailableToTags(apexAvailableWithoutTestApexes)
827 }
828 return attr
829}
830
831func removeTestApexes(ctx BaseModuleContext, apex_available []string) []string {
832 testApexes := []string{}
833 for _, aa := range apex_available {
834 // ignore the wildcards
835 if InList(aa, AvailableToRecognziedWildcards) {
836 continue
837 }
838 mod, _ := ctx.ModuleFromName(aa)
839 if apex, ok := mod.(ApexTestInterface); ok && apex.IsTestApex() {
840 testApexes = append(testApexes, aa)
841 }
842 }
843 return RemoveListFromList(CopyOf(apex_available), testApexes)
844}
845
Cole Faustfb11c1c2023-02-10 11:27:46 -0800846func ConvertApexAvailableToTags(apexAvailable []string) []string {
847 if len(apexAvailable) == 0 {
848 // We need nil specifically to make bp2build not add the tags property at all,
849 // instead of adding it with an empty list
850 return nil
851 }
852 result := make([]string, 0, len(apexAvailable))
853 for _, a := range apexAvailable {
854 result = append(result, "apex_available="+a)
855 }
856 return result
857}
858
Spandan Das39b6cc52023-04-12 19:05:49 +0000859// ConvertApexAvailableToTagsWithoutTestApexes converts a list of apex names to a list of bazel tags
860// This function drops any test apexes from the input.
861func ConvertApexAvailableToTagsWithoutTestApexes(ctx BaseModuleContext, apexAvailable []string) []string {
862 noTestApexes := removeTestApexes(ctx, apexAvailable)
863 return ConvertApexAvailableToTags(noTestApexes)
864}
865
Chris Parsons6666d0f2023-09-22 16:21:53 +0000866func (t *bottomUpMutatorContext) createBazelTargetModule(
Chris Parsons58852a02021-12-09 18:10:18 -0500867 bazelProps bazel.BazelTargetModuleProperties,
868 commonAttrs CommonAttributes,
869 attrs interface{},
870 enabledProperty bazel.BoolAttribute) {
871 constraintAttributes := commonAttrs.fillCommonBp2BuildModuleAttrs(t, enabledProperty)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000872 mod := t.Module()
Liz Kammer2ada09a2021-08-11 00:17:36 -0400873 info := bp2buildInfo{
Spandan Das3131d672023-08-03 22:33:47 +0000874 Dir: dirForBazelTargetGeneration(t, &commonAttrs),
Chris Parsons58852a02021-12-09 18:10:18 -0500875 BazelProps: bazelProps,
876 CommonAttrs: commonAttrs,
877 ConstraintAttrs: constraintAttributes,
878 Attrs: attrs,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500879 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000880 mod.base().addBp2buildInfo(info)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500881}
882
Colin Crossdc35e212019-06-06 16:13:11 -0700883// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
884// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
885// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
886// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
887// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
888
Colin Crosscb55e082019-07-01 15:32:31 -0700889func (t *topDownMutatorContext) MutatorName() string {
890 return t.bp.MutatorName()
891}
892
Colin Crossdc35e212019-06-06 16:13:11 -0700893func (t *topDownMutatorContext) Rename(name string) {
894 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700895 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700896}
897
Liz Kammerf31c9002022-04-26 09:08:55 -0400898func (t *topDownMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
899 return t.bp.CreateModule(factory, name, props...)
900}
901
Colin Crosse003c4a2019-09-25 12:58:36 -0700902func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400903 return createModule(t, factory, "_topDownMutatorModule", props...)
Colin Crossdc35e212019-06-06 16:13:11 -0700904}
905
Liz Kammera060c452021-03-24 10:14:47 -0400906func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400907 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), "", props...).(Module)
Liz Kammera060c452021-03-24 10:14:47 -0400908 return module
909}
910
Colin Crosscb55e082019-07-01 15:32:31 -0700911func (b *bottomUpMutatorContext) MutatorName() string {
912 return b.bp.MutatorName()
913}
914
Colin Crossdc35e212019-06-06 16:13:11 -0700915func (b *bottomUpMutatorContext) Rename(name string) {
916 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700917 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700918}
919
Colin Cross4f1dcb02020-09-16 18:45:04 -0700920func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400921 if b.baseModuleContext.checkedMissingDeps() {
922 panic("Adding deps not allowed after checking for missing deps")
923 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700924 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700925}
926
927func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400928 if b.baseModuleContext.checkedMissingDeps() {
929 panic("Adding deps not allowed after checking for missing deps")
930 }
Colin Crossdc35e212019-06-06 16:13:11 -0700931 b.bp.AddReverseDependency(module, tag, name)
932}
933
Colin Cross43b92e02019-11-18 15:28:57 -0800934func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000935 if b.finalPhase {
936 panic("CreateVariations not allowed in FinalDepsMutators")
937 }
938
Colin Cross9a362232019-07-01 15:32:45 -0700939 modules := b.bp.CreateVariations(variations...)
940
Colin Cross43b92e02019-11-18 15:28:57 -0800941 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700942 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800943 aModules[i] = modules[i].(Module)
944 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700945 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
946 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
947 }
948
Colin Cross43b92e02019-11-18 15:28:57 -0800949 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700950}
951
Colin Cross43b92e02019-11-18 15:28:57 -0800952func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000953 if b.finalPhase {
954 panic("CreateLocalVariations not allowed in FinalDepsMutators")
955 }
956
Colin Cross9a362232019-07-01 15:32:45 -0700957 modules := b.bp.CreateLocalVariations(variations...)
958
Colin Cross43b92e02019-11-18 15:28:57 -0800959 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700960 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800961 aModules[i] = modules[i].(Module)
962 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700963 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
964 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
965 }
966
Colin Cross43b92e02019-11-18 15:28:57 -0800967 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700968}
969
970func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
971 b.bp.SetDependencyVariation(variation)
972}
973
Jiyong Park1d1119f2019-07-29 21:27:18 +0900974func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
975 b.bp.SetDefaultDependencyVariation(variation)
976}
977
Colin Crossdc35e212019-06-06 16:13:11 -0700978func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700979 names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400980 if b.baseModuleContext.checkedMissingDeps() {
981 panic("Adding deps not allowed after checking for missing deps")
982 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700983 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700984}
985
986func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700987 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400988 if b.baseModuleContext.checkedMissingDeps() {
989 panic("Adding deps not allowed after checking for missing deps")
990 }
Colin Crossdc35e212019-06-06 16:13:11 -0700991
Colin Cross4f1dcb02020-09-16 18:45:04 -0700992 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700993}
994
995func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
996 b.bp.AddInterVariantDependency(tag, from, to)
997}
998
999func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -04001000 if b.baseModuleContext.checkedMissingDeps() {
1001 panic("Adding deps not allowed after checking for missing deps")
1002 }
Colin Crossdc35e212019-06-06 16:13:11 -07001003 b.bp.ReplaceDependencies(name)
1004}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -08001005
Paul Duffin80342d72020-06-26 22:08:43 +01001006func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
Liz Kammerc13f7852023-05-17 13:01:48 -04001007 if b.baseModuleContext.checkedMissingDeps() {
1008 panic("Adding deps not allowed after checking for missing deps")
1009 }
Paul Duffin80342d72020-06-26 22:08:43 +01001010 b.bp.ReplaceDependenciesIf(name, predicate)
1011}
1012
Jaewoong Jung9f88ce22019-11-15 10:57:34 -08001013func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
1014 b.bp.AliasVariation(variationName)
1015}
Colin Cross1b9604b2020-08-11 12:03:56 -07001016
1017func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
1018 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
1019}
Colin Crossd27e7b82020-07-02 11:38:17 -07001020
1021func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
1022 b.bp.SetVariationProvider(module, provider, value)
1023}