blob: 067d6c3df96154ce852b5c6f452cde0d6cdbe081 [file] [log] [blame]
Colin Cross6362e272015-10-29 15:25:03 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross6362e272015-10-29 15:25:03 -070016
Colin Cross795c3772017-03-16 16:50:10 -070017import (
Chris Parsons637458d2023-09-19 20:09:00 +000018 "path/filepath"
19
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000020 "android/soong/bazel"
Chris Parsons39a16972023-06-08 14:28:51 +000021 "android/soong/ui/metrics/bp2build_metrics_proto"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000022
Colin Cross795c3772017-03-16 16:50:10 -070023 "github.com/google/blueprint"
24)
Colin Cross6362e272015-10-29 15:25:03 -070025
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070026// Phases:
27// run Pre-arch mutators
28// run archMutator
29// run Pre-deps mutators
30// run depsMutator
31// run PostDeps mutators
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000032// run FinalDeps mutators (CreateVariations disallowed in this phase)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070033// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070034
Jingwen Chen73850672020-12-14 08:25:34 -050035// RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
Liz Kammerbe46fcc2021-11-01 15:32:43 -040036func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) {
Spandan Das5af0bd32022-09-28 20:43:08 +000037 bp2buildMutators := append(preArchMutators, registerBp2buildConversionMutator)
38 registerMutatorsForBazelConversion(ctx, bp2buildMutators)
39}
40
Spandan Das5af0bd32022-09-28 20:43:08 +000041func registerMutatorsForBazelConversion(ctx *Context, bp2buildMutators []RegisterMutatorFunc) {
Liz Kammer356f7d42021-01-26 09:18:53 -050042 mctx := &registerMutatorsContext{
43 bazelConversionMode: true,
Jingwen Chen041b1842021-02-01 00:23:25 -050044 }
45
Spandan Das5af0bd32022-09-28 20:43:08 +000046 allMutators := append([]RegisterMutatorFunc{
Liz Kammer356f7d42021-01-26 09:18:53 -050047 RegisterNamespaceMutator,
48 RegisterDefaultsPreArchMutators,
49 // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should
50 // evaluate the impact on conversion.
51 RegisterPrebuiltsPreArchMutators,
Liz Kammere1b39a52023-09-18 14:32:18 -040052 RegisterPrebuiltsPostDepsMutators,
Liz Kammer356f7d42021-01-26 09:18:53 -050053 },
Spandan Das5af0bd32022-09-28 20:43:08 +000054 bp2buildMutators...)
Liz Kammer356f7d42021-01-26 09:18:53 -050055
Jingwen Chen73850672020-12-14 08:25:34 -050056 // Register bp2build mutators
Spandan Das5af0bd32022-09-28 20:43:08 +000057 for _, f := range allMutators {
Jingwen Chen73850672020-12-14 08:25:34 -050058 f(mctx)
59 }
60
Paul Duffin1d2d42f2021-03-06 20:08:12 +000061 mctx.mutators.registerAll(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -050062}
63
Paul Duffinc05b0342021-03-06 13:28:13 +000064// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
65// with the InitRegistrationContext and will be used at runtime.
66func collateGloballyRegisteredMutators() sortableComponents {
Liz Kammerc13f7852023-05-17 13:01:48 -040067 // ensure mixed builds mutator is the last mutator
68 finalDeps = append(finalDeps, registerMixedBuildsMutator)
Paul Duffinc05b0342021-03-06 13:28:13 +000069 return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
70}
71
72// collateRegisteredMutators constructs a single list of mutators from the separate lists.
73func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
Colin Crosscec81712017-07-13 14:43:27 -070074 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080075
76 register := func(funcs []RegisterMutatorFunc) {
77 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070078 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080079 }
80 }
81
Colin Crosscec81712017-07-13 14:43:27 -070082 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080083
Colin Crosscec81712017-07-13 14:43:27 -070084 register(preDeps)
85
Liz Kammer356f7d42021-01-26 09:18:53 -050086 register([]RegisterMutatorFunc{registerDepsMutator})
Colin Crosscec81712017-07-13 14:43:27 -070087
88 register(postDeps)
89
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000090 mctx.finalPhase = true
91 register(finalDeps)
92
Paul Duffinc05b0342021-03-06 13:28:13 +000093 return mctx.mutators
Colin Cross795c3772017-03-16 16:50:10 -070094}
95
96type registerMutatorsContext struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +000097 mutators sortableComponents
Liz Kammer356f7d42021-01-26 09:18:53 -050098 finalPhase bool
99 bazelConversionMode bool
Colin Cross795c3772017-03-16 16:50:10 -0700100}
Colin Cross1e676be2016-10-12 14:38:15 -0700101
102type RegisterMutatorsContext interface {
Colin Cross25de6c32019-06-06 14:29:25 -0700103 TopDown(name string, m TopDownMutator) MutatorHandle
104 BottomUp(name string, m BottomUpMutator) MutatorHandle
Colin Cross617b88a2020-08-24 18:04:09 -0700105 BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200106 Transition(name string, m TransitionMutator)
Colin Cross1e676be2016-10-12 14:38:15 -0700107}
108
109type RegisterMutatorFunc func(RegisterMutatorsContext)
110
Colin Crosscec81712017-07-13 14:43:27 -0700111var preArch = []RegisterMutatorFunc{
Dan Willemsen6e72ef72018-01-26 18:27:02 -0800112 RegisterNamespaceMutator,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100113
Paul Duffinaa4162e2020-05-05 11:35:43 +0100114 // Check the visibility rules are valid.
115 //
116 // This must run after the package renamer mutators so that any issues found during
117 // validation of the package's default_visibility property are reported using the
118 // correct package name and not the synthetic name.
119 //
120 // This must also be run before defaults mutators as the rules for validation are
121 // different before checking the rules than they are afterwards. e.g.
122 // visibility: ["//visibility:private", "//visibility:public"]
123 // would be invalid if specified in a module definition but is valid if it results
124 // from something like this:
125 //
126 // defaults {
127 // name: "defaults",
128 // // Be inaccessible outside a package by default.
129 // visibility: ["//visibility:private"]
130 // }
131 //
132 // defaultable_module {
133 // name: "defaultable_module",
134 // defaults: ["defaults"],
135 // // Override the default.
136 // visibility: ["//visibility:public"]
137 // }
138 //
Paul Duffin593b3c92019-12-05 14:31:48 +0000139 RegisterVisibilityRuleChecker,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100140
Bob Badour37af0462021-01-07 03:34:31 +0000141 // Record the default_applicable_licenses for each package.
142 //
143 // This must run before the defaults so that defaults modules can pick up the package default.
144 RegisterLicensesPackageMapper,
145
Paul Duffinaa4162e2020-05-05 11:35:43 +0100146 // Apply properties from defaults modules to the referencing modules.
Paul Duffinafa9fa12020-04-29 16:47:28 +0100147 //
148 // Any mutators that are added before this will not see any modules created by
149 // a DefaultableHook.
Colin Cross89536d42017-07-07 14:35:50 -0700150 RegisterDefaultsPreArchMutators,
Paul Duffinaa4162e2020-05-05 11:35:43 +0100151
Paul Duffin44f1d842020-06-26 20:17:02 +0100152 // Add dependencies on any components so that any component references can be
153 // resolved within the deps mutator.
154 //
155 // Must be run after defaults so it can be used to create dependencies on the
156 // component modules that are creating in a DefaultableHook.
157 //
158 // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
159 // renamed. That is so that if a module creates components using a prebuilt module
160 // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
161 // the prebuilt module and not the source module.
162 RegisterComponentsMutator,
163
Paul Duffinc988c8e2020-04-29 18:27:14 +0100164 // Create an association between prebuilt modules and their corresponding source
165 // modules (if any).
Paul Duffinafa9fa12020-04-29 16:47:28 +0100166 //
167 // Must be run after defaults mutators to ensure that any modules created by
168 // a DefaultableHook can be either a prebuilt or a source module with a matching
169 // prebuilt.
Paul Duffinc988c8e2020-04-29 18:27:14 +0100170 RegisterPrebuiltsPreArchMutators,
171
Bob Badour37af0462021-01-07 03:34:31 +0000172 // Gather the licenses properties for all modules for use during expansion and enforcement.
173 //
174 // This must come after the defaults mutators to ensure that any licenses supplied
175 // in a defaults module has been successfully applied before the rules are gathered.
176 RegisterLicensesPropertyGatherer,
177
Paul Duffinaa4162e2020-05-05 11:35:43 +0100178 // Gather the visibility rules for all modules for us during visibility enforcement.
179 //
180 // This must come after the defaults mutators to ensure that any visibility supplied
181 // in a defaults module has been successfully applied before the rules are gathered.
Paul Duffin593b3c92019-12-05 14:31:48 +0000182 RegisterVisibilityRuleGatherer,
Colin Crosscec81712017-07-13 14:43:27 -0700183}
184
Colin Crossae4c6182017-09-15 17:33:55 -0700185func registerArchMutator(ctx RegisterMutatorsContext) {
Colin Cross617b88a2020-08-24 18:04:09 -0700186 ctx.BottomUpBlueprint("os", osMutator).Parallel()
Colin Crossfb0c16e2019-11-20 17:12:35 -0800187 ctx.BottomUp("image", imageMutator).Parallel()
Colin Cross617b88a2020-08-24 18:04:09 -0700188 ctx.BottomUpBlueprint("arch", archMutator).Parallel()
Colin Crossae4c6182017-09-15 17:33:55 -0700189}
190
Colin Crosscec81712017-07-13 14:43:27 -0700191var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -0700192 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -0700193}
194
195var postDeps = []RegisterMutatorFunc{
Colin Cross1b488422019-03-04 22:33:56 -0800196 registerPathDepsMutator,
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700197 RegisterPrebuiltsPostDepsMutators,
Paul Duffin593b3c92019-12-05 14:31:48 +0000198 RegisterVisibilityRuleEnforcer,
Bob Badour37af0462021-01-07 03:34:31 +0000199 RegisterLicensesDependencyChecker,
Paul Duffin45338f02021-03-30 23:07:52 +0100200 registerNeverallowMutator,
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700201 RegisterOverridePostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -0700202}
Colin Cross1e676be2016-10-12 14:38:15 -0700203
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000204var finalDeps = []RegisterMutatorFunc{}
205
Colin Cross1e676be2016-10-12 14:38:15 -0700206func PreArchMutators(f RegisterMutatorFunc) {
207 preArch = append(preArch, f)
208}
209
210func PreDepsMutators(f RegisterMutatorFunc) {
211 preDeps = append(preDeps, f)
212}
213
214func PostDepsMutators(f RegisterMutatorFunc) {
215 postDeps = append(postDeps, f)
216}
217
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000218func FinalDepsMutators(f RegisterMutatorFunc) {
219 finalDeps = append(finalDeps, f)
220}
221
Liz Kammer356f7d42021-01-26 09:18:53 -0500222var bp2buildPreArchMutators = []RegisterMutatorFunc{}
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400223
Liz Kammer12615db2021-09-28 09:19:17 -0400224// A minimal context for Bp2build conversion
225type Bp2buildMutatorContext interface {
226 BazelConversionPathContext
Colin Cross9f35c3d2020-09-16 19:04:41 -0700227 BaseMutatorContext
Colin Cross3f68a132017-10-23 17:10:29 -0700228
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000229 // AddDependency adds a dependency to the given module. It returns a slice of modules for each
230 // dependency (some entries may be nil).
231 //
232 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
233 // new dependencies have had the current mutator called on them. If the mutator is not
234 // parallel this method does not affect the ordering of the current mutator pass, but will
235 // be ordered correctly for all future mutator passes.
236 AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
237
Jingwen Chen1fd14692021-02-05 03:01:50 -0500238 // CreateBazelTargetModule creates a BazelTargetModule by calling the
239 // factory method, just like in CreateModule, but also requires
240 // BazelTargetModuleProperties containing additional metadata for the
241 // bp2build codegenerator.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000242 CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
Chris Parsons58852a02021-12-09 18:10:18 -0500243
244 // CreateBazelTargetModuleWithRestrictions creates a BazelTargetModule by calling the
245 // factory method, just like in CreateModule, but also requires
246 // BazelTargetModuleProperties containing additional metadata for the
247 // bp2build codegenerator. The generated target is restricted to only be buildable for certain
248 // platforms, as dictated by a given bool attribute: the target will not be buildable in
249 // any platform for which this bool attribute is false.
250 CreateBazelTargetModuleWithRestrictions(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}, bazel.BoolAttribute)
Spandan Dasabedff02023-03-07 19:24:34 +0000251
Chris Parsons39a16972023-06-08 14:28:51 +0000252 // MarkBp2buildUnconvertible registers the current module as "unconvertible to bp2build" for the
253 // given reason.
254 MarkBp2buildUnconvertible(reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string)
255
Spandan Dasabedff02023-03-07 19:24:34 +0000256 // CreateBazelTargetAliasInDir creates an alias definition in `dir` directory.
257 // This function can be used to create alias definitions in a directory that is different
258 // from the directory of the visited Soong module.
259 CreateBazelTargetAliasInDir(dir string, name string, actual bazel.Label)
Spandan Das6a448ec2023-04-19 17:36:12 +0000260
261 // CreateBazelConfigSetting creates a config_setting in <dir>/BUILD.bazel
262 // build/bazel has several static config_setting(s) that are used in Bazel builds.
263 // This function can be used to createa additional config_setting(s) based on the build graph
264 // (e.g. a config_setting specific to an apex variant)
265 CreateBazelConfigSetting(csa bazel.ConfigSettingAttributes, ca CommonAttributes, dir string)
Colin Cross6362e272015-10-29 15:25:03 -0700266}
267
Chris Parsons637458d2023-09-19 20:09:00 +0000268// PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
269// into Bazel BUILD targets that should run prior to deps and conversion.
270func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
271 bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
272}
273
274type BaseMutatorContext interface {
275 BaseModuleContext
276
277 // MutatorName returns the name that this mutator was registered with.
278 MutatorName() string
279
280 // Rename all variants of a module. The new name is not visible to calls to ModuleName,
281 // AddDependency or OtherModuleName until after this mutator pass is complete.
282 Rename(name string)
283}
284
285type TopDownMutator func(TopDownMutatorContext)
286
287type TopDownMutatorContext interface {
288 BaseMutatorContext
Chris Parsons637458d2023-09-19 20:09:00 +0000289
290 // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
291 // the specified property structs to it as if the properties were set in a blueprint file.
292 CreateModule(ModuleFactory, ...interface{}) Module
293}
294
Colin Cross25de6c32019-06-06 14:29:25 -0700295type topDownMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700296 bp blueprint.TopDownMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700297 baseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700298}
299
Colin Cross25de6c32019-06-06 14:29:25 -0700300type BottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700301
Colin Cross635c3b02016-05-18 15:37:25 -0700302type BottomUpMutatorContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -0700303 BaseMutatorContext
Chris Parsons6666d0f2023-09-22 16:21:53 +0000304 Bp2buildMutatorContext
Colin Crossaabf6792017-11-29 00:27:14 -0800305
Colin Cross9f35c3d2020-09-16 19:04:41 -0700306 // AddReverseDependency adds a dependency from the destination to the given module.
307 // Does not affect the ordering of the current mutator pass, but will be ordered
308 // correctly for all future mutator passes. All reverse dependencies for a destination module are
309 // collected until the end of the mutator pass, sorted by name, and then appended to the destination
310 // module's dependency list.
Colin Crossaabf6792017-11-29 00:27:14 -0800311 AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700312
313 // CreateVariations splits a module into multiple variants, one for each name in the variationNames
314 // parameter. It returns a list of new modules in the same order as the variationNames
315 // list.
316 //
317 // If any of the dependencies of the module being operated on were already split
318 // by calling CreateVariations with the same name, the dependency will automatically
319 // be updated to point the matching variant.
320 //
321 // If a module is split, and then a module depending on the first module is not split
322 // when the Mutator is later called on it, the dependency of the depending module will
323 // automatically be updated to point to the first variant.
Colin Cross43b92e02019-11-18 15:28:57 -0800324 CreateVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700325
326 // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
327 // parameter. It returns a list of new modules in the same order as the variantNames
328 // list.
329 //
330 // Local variations do not affect automatic dependency resolution - dependencies added
331 // to the split module via deps or DynamicDependerModule must exactly match a variant
332 // that contains all the non-local variations.
Colin Cross43b92e02019-11-18 15:28:57 -0800333 CreateLocalVariations(...string) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700334
335 // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
336 // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
Colin Crossaabf6792017-11-29 00:27:14 -0800337 SetDependencyVariation(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700338
339 // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
340 // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
Jiyong Park1d1119f2019-07-29 21:27:18 +0900341 SetDefaultDependencyVariation(*string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700342
343 // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
Colin Cross4f1dcb02020-09-16 18:45:04 -0700344 // argument to select which variant of the dependency to use. It returns a slice of modules for
345 // each dependency (some entries may be nil). A variant of the dependency must exist that matches
Usta Shresthac725f472022-01-11 02:44:21 -0500346 // all the non-local variations of the current module, plus the variations argument.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700347 //
348 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
349 // new dependencies have had the current mutator called on them. If the mutator is not
350 // parallel this method does not affect the ordering of the current mutator pass, but will
351 // be ordered correctly for all future mutator passes.
Usta Shresthac725f472022-01-11 02:44:21 -0500352 AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700353
354 // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
Colin Cross4f1dcb02020-09-16 18:45:04 -0700355 // variations argument to select which variant of the dependency to use. It returns a slice of
356 // modules for each dependency (some entries may be nil). A variant of the dependency must
357 // exist that matches the variations argument, but may also have other variations.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700358 // For any unspecified variation the first variant will be used.
359 //
360 // Unlike AddVariationDependencies, the variations of the current module are ignored - the
361 // dependency only needs to match the supplied variations.
Colin Cross4f1dcb02020-09-16 18:45:04 -0700362 //
363 // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
364 // new dependencies have had the current mutator called on them. If the mutator is not
365 // parallel this method does not affect the ordering of the current mutator pass, but will
366 // be ordered correctly for all future mutator passes.
367 AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700368
369 // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
370 // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
371 // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
372 // WalkDeps, etc.
Colin Crossaabf6792017-11-29 00:27:14 -0800373 AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700374
375 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
376 // specified name with the current variant of this module. Replacements don't take effect until
377 // after the mutator pass is finished.
Colin Crossaabf6792017-11-29 00:27:14 -0800378 ReplaceDependencies(string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700379
380 // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
381 // specified name with the current variant of this module as long as the supplied predicate returns
382 // true.
383 //
384 // Replacements don't take effect until after the mutator pass is finished.
Paul Duffin80342d72020-06-26 22:08:43 +0100385 ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700386
387 // AliasVariation takes a variationName that was passed to CreateVariations for this module,
388 // and creates an alias from the current variant (before the mutator has run) to the new
389 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
390 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
391 // be used to add dependencies on the newly created variant using the variant map from
392 // before CreateVariations was run.
Jaewoong Jung9f88ce22019-11-15 10:57:34 -0800393 AliasVariation(variationName string)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700394
395 // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
396 // module, and creates an alias from a new fromVariationName variant the toVariationName
397 // variant. The alias will be valid until the next time a mutator calls CreateVariations or
398 // CreateLocalVariations on this module without also calling AliasVariation. The alias can
399 // be used to add dependencies on the toVariationName variant using the fromVariationName
400 // variant.
Colin Cross1b9604b2020-08-11 12:03:56 -0700401 CreateAliasVariation(fromVariationName, toVariationName string)
Colin Crossd27e7b82020-07-02 11:38:17 -0700402
403 // SetVariationProvider sets the value for a provider for the given newly created variant of
404 // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
405 // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
406 // if the value is not of the appropriate type, or if the module is not a newly created
407 // variant of the current module. The value should not be modified after being passed to
408 // SetVariationProvider.
409 SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
Colin Cross6362e272015-10-29 15:25:03 -0700410}
411
Colin Cross25de6c32019-06-06 14:29:25 -0700412type bottomUpMutatorContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -0700413 bp blueprint.BottomUpMutatorContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700414 baseModuleContext
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400415 finalPhase bool
Colin Cross6362e272015-10-29 15:25:03 -0700416}
417
Colin Cross617b88a2020-08-24 18:04:09 -0700418func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
Liz Kammer356f7d42021-01-26 09:18:53 -0500419 finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
Colin Cross617b88a2020-08-24 18:04:09 -0700420
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400421 moduleContext := a.base().baseModuleContextFactory(ctx)
422 moduleContext.bazelConversionMode = bazelConversionMode
423
Colin Cross617b88a2020-08-24 18:04:09 -0700424 return &bottomUpMutatorContext{
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400425 bp: ctx,
Cole Faust0abd4b42023-01-10 10:49:18 -0800426 baseModuleContext: moduleContext,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400427 finalPhase: finalPhase,
Colin Cross617b88a2020-08-24 18:04:09 -0700428 }
429}
430
Colin Cross25de6c32019-06-06 14:29:25 -0700431func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000432 finalPhase := x.finalPhase
Liz Kammer356f7d42021-01-26 09:18:53 -0500433 bazelConversionMode := x.bazelConversionMode
Colin Cross798bfce2016-10-12 14:28:16 -0700434 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700435 if a, ok := ctx.Module().(Module); ok {
Liz Kammer356f7d42021-01-26 09:18:53 -0500436 m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
Colin Cross6362e272015-10-29 15:25:03 -0700437 }
Colin Cross798bfce2016-10-12 14:28:16 -0700438 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500439 mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700440 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700441 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700442}
443
Colin Cross617b88a2020-08-24 18:04:09 -0700444func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
445 mutator := &mutator{name: name, bottomUpMutator: m}
446 x.mutators = append(x.mutators, mutator)
447 return mutator
448}
449
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200450type IncomingTransitionContext interface {
451 // Module returns the target of the dependency edge for which the transition
452 // is being computed
453 Module() Module
454
455 // Config returns the configuration for the build.
456 Config() Config
457}
458
459type OutgoingTransitionContext interface {
460 // Module returns the target of the dependency edge for which the transition
461 // is being computed
462 Module() Module
463
464 // DepTag() Returns the dependency tag through which this dependency is
465 // reached
466 DepTag() blueprint.DependencyTag
467}
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200468
469// Transition mutators implement a top-down mechanism where a module tells its
470// direct dependencies what variation they should be built in but the dependency
471// has the final say.
472//
473// When implementing a transition mutator, one needs to implement four methods:
474// - Split() that tells what variations a module has by itself
475// - OutgoingTransition() where a module tells what it wants from its
476// dependency
477// - IncomingTransition() where a module has the final say about its own
478// variation
479// - Mutate() that changes the state of a module depending on its variation
480//
481// That the effective variation of module B when depended on by module A is the
482// composition the outgoing transition of module A and the incoming transition
483// of module B.
484//
485// the outgoing transition should not take the properties of the dependency into
486// account, only those of the module that depends on it. For this reason, the
487// dependency is not even passed into it as an argument. Likewise, the incoming
488// transition should not take the properties of the depending module into
489// account and is thus not informed about it. This makes for a nice
490// decomposition of the decision logic.
491//
492// A given transition mutator only affects its own variation; other variations
493// stay unchanged along the dependency edges.
494//
495// Soong makes sure that all modules are created in the desired variations and
496// that dependency edges are set up correctly. This ensures that "missing
497// variation" errors do not happen and allows for more flexible changes in the
498// value of the variation among dependency edges (as oppposed to bottom-up
499// mutators where if module A in variation X depends on module B and module B
500// has that variation X, A must depend on variation X of B)
501//
502// The limited power of the context objects passed to individual mutators
503// methods also makes it more difficult to shoot oneself in the foot. Complete
504// safety is not guaranteed because no one prevents individual transition
505// mutators from mutating modules in illegal ways and for e.g. Split() or
506// Mutate() to run their own visitations of the transitive dependency of the
507// module and both of these are bad ideas, but it's better than no guardrails at
508// all.
509//
510// This model is pretty close to Bazel's configuration transitions. The mapping
511// between concepts in Soong and Bazel is as follows:
512// - Module == configured target
513// - Variant == configuration
514// - Variation name == configuration flag
515// - Variation == configuration flag value
516// - Outgoing transition == attribute transition
517// - Incoming transition == rule transition
518//
519// The Split() method does not have a Bazel equivalent and Bazel split
520// transitions do not have a Soong equivalent.
521//
522// Mutate() does not make sense in Bazel due to the different models of the
523// two systems: when creating new variations, Soong clones the old module and
524// thus some way is needed to change it state whereas Bazel creates each
525// configuration of a given configured target anew.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200526type TransitionMutator interface {
527 // Split returns the set of variations that should be created for a module no
528 // matter who depends on it. Used when Make depends on a particular variation
529 // or when the module knows its variations just based on information given to
530 // it in the Blueprint file. This method should not mutate the module it is
531 // called on.
532 Split(ctx BaseModuleContext) []string
533
Lukacs T. Berki0e691c12022-06-24 10:15:55 +0200534 // Called on a module to determine which variation it wants from its direct
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200535 // dependencies. The dependency itself can override this decision. This method
536 // should not mutate the module itself.
537 OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string
538
539 // Called on a module to determine which variation it should be in based on
540 // the variation modules that depend on it want. This gives the module a final
541 // say about its own variations. This method should not mutate the module
542 // itself.
543 IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string
544
545 // Called after a module was split into multiple variations on each variation.
546 // It should not split the module any further but adding new dependencies is
547 // fine. Unlike all the other methods on TransitionMutator, this method is
548 // allowed to mutate the module.
549 Mutate(ctx BottomUpMutatorContext, variation string)
550}
551
552type androidTransitionMutator struct {
553 finalPhase bool
554 bazelConversionMode bool
555 mutator TransitionMutator
556}
557
558func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []string {
559 if m, ok := ctx.Module().(Module); ok {
560 moduleContext := m.base().baseModuleContextFactory(ctx)
561 moduleContext.bazelConversionMode = a.bazelConversionMode
562 return a.mutator.Split(&moduleContext)
563 } else {
564 return []string{""}
565 }
566}
567
568type outgoingTransitionContextImpl struct {
569 bp blueprint.OutgoingTransitionContext
570}
571
572func (c *outgoingTransitionContextImpl) Module() Module {
573 return c.bp.Module().(Module)
574}
575
576func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag {
577 return c.bp.DepTag()
578}
579
580func (a *androidTransitionMutator) OutgoingTransition(ctx blueprint.OutgoingTransitionContext, sourceVariation string) string {
581 if _, ok := ctx.Module().(Module); ok {
582 return a.mutator.OutgoingTransition(&outgoingTransitionContextImpl{bp: ctx}, sourceVariation)
583 } else {
584 return ""
585 }
586}
587
588type incomingTransitionContextImpl struct {
589 bp blueprint.IncomingTransitionContext
590}
591
592func (c *incomingTransitionContextImpl) Module() Module {
593 return c.bp.Module().(Module)
594}
595
596func (c *incomingTransitionContextImpl) Config() Config {
597 return c.bp.Config().(Config)
598}
599
600func (a *androidTransitionMutator) IncomingTransition(ctx blueprint.IncomingTransitionContext, incomingVariation string) string {
601 if _, ok := ctx.Module().(Module); ok {
602 return a.mutator.IncomingTransition(&incomingTransitionContextImpl{bp: ctx}, incomingVariation)
603 } else {
604 return ""
605 }
606}
607
608func (a *androidTransitionMutator) Mutate(ctx blueprint.BottomUpMutatorContext, variation string) {
609 if am, ok := ctx.Module().(Module); ok {
610 a.mutator.Mutate(bottomUpMutatorContextFactory(ctx, am, a.finalPhase, a.bazelConversionMode), variation)
611 }
612}
613
614func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) {
615 atm := &androidTransitionMutator{
616 finalPhase: x.finalPhase,
617 bazelConversionMode: x.bazelConversionMode,
618 mutator: m,
619 }
620 mutator := &mutator{
621 name: name,
622 transitionMutator: atm}
623 x.mutators = append(x.mutators, mutator)
624}
625
Liz Kammer356f7d42021-01-26 09:18:53 -0500626func (x *registerMutatorsContext) mutatorName(name string) string {
627 if x.bazelConversionMode {
628 return name + "_bp2build"
629 }
630 return name
631}
632
Colin Cross25de6c32019-06-06 14:29:25 -0700633func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700634 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700635 if a, ok := ctx.Module().(Module); ok {
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400636 moduleContext := a.base().baseModuleContextFactory(ctx)
637 moduleContext.bazelConversionMode = x.bazelConversionMode
Colin Cross25de6c32019-06-06 14:29:25 -0700638 actx := &topDownMutatorContext{
Colin Crossdc35e212019-06-06 16:13:11 -0700639 bp: ctx,
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400640 baseModuleContext: moduleContext,
Colin Cross6362e272015-10-29 15:25:03 -0700641 }
Colin Cross798bfce2016-10-12 14:28:16 -0700642 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700643 }
Colin Cross798bfce2016-10-12 14:28:16 -0700644 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500645 mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700646 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700647 return mutator
648}
649
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000650func (mutator *mutator) componentName() string {
651 return mutator.name
652}
653
654func (mutator *mutator) register(ctx *Context) {
655 blueprintCtx := ctx.Context
656 var handle blueprint.MutatorHandle
657 if mutator.bottomUpMutator != nil {
658 handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
659 } else if mutator.topDownMutator != nil {
660 handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200661 } else if mutator.transitionMutator != nil {
662 blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000663 }
664 if mutator.parallel {
665 handle.Parallel()
666 }
667}
668
Colin Cross798bfce2016-10-12 14:28:16 -0700669type MutatorHandle interface {
670 Parallel() MutatorHandle
671}
672
673func (mutator *mutator) Parallel() MutatorHandle {
674 mutator.parallel = true
675 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700676}
Colin Cross1e676be2016-10-12 14:38:15 -0700677
Paul Duffin44f1d842020-06-26 20:17:02 +0100678func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
679 ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
680}
681
682// A special mutator that runs just prior to the deps mutator to allow the dependencies
683// on component modules to be added so that they can depend directly on a prebuilt
684// module.
685func componentDepsMutator(ctx BottomUpMutatorContext) {
686 if m := ctx.Module(); m.Enabled() {
687 m.ComponentDepsMutator(ctx)
688 }
689}
690
Colin Cross1e676be2016-10-12 14:38:15 -0700691func depsMutator(ctx BottomUpMutatorContext) {
Paul Duffin44f1d842020-06-26 20:17:02 +0100692 if m := ctx.Module(); m.Enabled() {
Colin Cross1e676be2016-10-12 14:38:15 -0700693 m.DepsMutator(ctx)
694 }
695}
Colin Crossd11fcda2017-10-23 17:59:01 -0700696
Liz Kammer356f7d42021-01-26 09:18:53 -0500697func registerDepsMutator(ctx RegisterMutatorsContext) {
698 ctx.BottomUp("deps", depsMutator).Parallel()
699}
700
701func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
702 // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
703 // being converted to build targets.
704 ctx.BottomUp("deps", depsMutator).Parallel()
705}
706
Chris Parsons6666d0f2023-09-22 16:21:53 +0000707func (t *bottomUpMutatorContext) CreateBazelTargetModule(
Jingwen Chen1fd14692021-02-05 03:01:50 -0500708 bazelProps bazel.BazelTargetModuleProperties,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000709 commonAttrs CommonAttributes,
Liz Kammer2ada09a2021-08-11 00:17:36 -0400710 attrs interface{}) {
Chris Parsons58852a02021-12-09 18:10:18 -0500711 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, bazel.BoolAttribute{})
712}
713
Chris Parsons6666d0f2023-09-22 16:21:53 +0000714func (t *bottomUpMutatorContext) CreateBazelTargetModuleWithRestrictions(
Chris Parsons58852a02021-12-09 18:10:18 -0500715 bazelProps bazel.BazelTargetModuleProperties,
716 commonAttrs CommonAttributes,
717 attrs interface{},
718 enabledProperty bazel.BoolAttribute) {
719 t.createBazelTargetModule(bazelProps, commonAttrs, attrs, enabledProperty)
720}
721
Chris Parsons6666d0f2023-09-22 16:21:53 +0000722func (t *bottomUpMutatorContext) MarkBp2buildUnconvertible(
Chris Parsons39a16972023-06-08 14:28:51 +0000723 reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string) {
724 mod := t.Module()
725 mod.base().setBp2buildUnconvertible(reasonType, detail)
726}
727
Spandan Dasabedff02023-03-07 19:24:34 +0000728var (
729 bazelAliasModuleProperties = bazel.BazelTargetModuleProperties{
730 Rule_class: "alias",
731 }
732)
733
734type bazelAliasAttributes struct {
735 Actual *bazel.LabelAttribute
736}
737
Chris Parsons6666d0f2023-09-22 16:21:53 +0000738func (t *bottomUpMutatorContext) CreateBazelTargetAliasInDir(
Spandan Dasabedff02023-03-07 19:24:34 +0000739 dir string,
740 name string,
741 actual bazel.Label) {
742 mod := t.Module()
743 attrs := &bazelAliasAttributes{
744 Actual: bazel.MakeLabelAttribute(actual.Label),
745 }
746 info := bp2buildInfo{
747 Dir: dir,
748 BazelProps: bazelAliasModuleProperties,
749 CommonAttrs: CommonAttributes{Name: name},
750 ConstraintAttrs: constraintAttributes{},
751 Attrs: attrs,
752 }
753 mod.base().addBp2buildInfo(info)
754}
755
Spandan Das3131d672023-08-03 22:33:47 +0000756// Returns the directory in which the bazel target will be generated
757// If ca.Dir is not nil, use that
758// Otherwise default to the directory of the soong module
Chris Parsons6666d0f2023-09-22 16:21:53 +0000759func dirForBazelTargetGeneration(t *bottomUpMutatorContext, ca *CommonAttributes) string {
Spandan Das3131d672023-08-03 22:33:47 +0000760 dir := t.OtherModuleDir(t.Module())
761 if ca.Dir != nil {
762 dir = *ca.Dir
763 // Restrict its use to dirs that contain an Android.bp file.
764 // There are several places in bp2build where we use the existence of Android.bp/BUILD on the filesystem
765 // to curate a compatible label for src files (e.g. headers for cc).
766 // If we arbritrarily create BUILD files, then it might render those curated labels incompatible.
767 if exists, _, _ := t.Config().fs.Exists(filepath.Join(dir, "Android.bp")); !exists {
768 t.ModuleErrorf("Cannot use ca.Dir to create a BazelTarget in dir: %v since it does not contain an Android.bp file", dir)
769 }
770
771 // Set ca.Dir to nil so that it does not get emitted to the BUILD files
772 ca.Dir = nil
773 }
774 return dir
775}
776
Chris Parsons6666d0f2023-09-22 16:21:53 +0000777func (t *bottomUpMutatorContext) CreateBazelConfigSetting(
Spandan Das6a448ec2023-04-19 17:36:12 +0000778 csa bazel.ConfigSettingAttributes,
779 ca CommonAttributes,
780 dir string) {
781 mod := t.Module()
782 info := bp2buildInfo{
783 Dir: dir,
784 BazelProps: bazel.BazelTargetModuleProperties{
785 Rule_class: "config_setting",
786 },
787 CommonAttrs: ca,
788 ConstraintAttrs: constraintAttributes{},
789 Attrs: &csa,
790 }
791 mod.base().addBp2buildInfo(info)
792}
793
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000794// ApexAvailableTags converts the apex_available property value of an ApexModule
795// module and returns it as a list of keyed tags.
796func ApexAvailableTags(mod Module) bazel.StringListAttribute {
797 attr := bazel.StringListAttribute{}
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000798 // Transform specific attributes into tags.
799 if am, ok := mod.(ApexModule); ok {
800 // TODO(b/218841706): hidl_interface has the apex_available prop, but it's
801 // defined directly as a prop and not via ApexModule, so this doesn't
802 // pick those props up.
Spandan Das2dc6dfc2023-04-17 19:16:06 +0000803 apexAvailable := am.apexModuleBase().ApexAvailable()
804 // If a user does not specify apex_available in Android.bp, then soong provides a default.
805 // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
806 if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
807 apexAvailable = []string{}
808 }
809 attr.Value = ConvertApexAvailableToTags(apexAvailable)
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000810 }
811 return attr
812}
813
Spandan Dasf57a9662023-04-12 19:05:49 +0000814func ApexAvailableTagsWithoutTestApexes(ctx BaseModuleContext, mod Module) bazel.StringListAttribute {
815 attr := bazel.StringListAttribute{}
816 if am, ok := mod.(ApexModule); ok {
817 apexAvailableWithoutTestApexes := removeTestApexes(ctx, am.apexModuleBase().ApexAvailable())
818 // If a user does not specify apex_available in Android.bp, then soong provides a default.
819 // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
820 if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
821 apexAvailableWithoutTestApexes = []string{}
822 }
823 attr.Value = ConvertApexAvailableToTags(apexAvailableWithoutTestApexes)
824 }
825 return attr
826}
827
828func removeTestApexes(ctx BaseModuleContext, apex_available []string) []string {
829 testApexes := []string{}
830 for _, aa := range apex_available {
831 // ignore the wildcards
832 if InList(aa, AvailableToRecognziedWildcards) {
833 continue
834 }
835 mod, _ := ctx.ModuleFromName(aa)
836 if apex, ok := mod.(ApexTestInterface); ok && apex.IsTestApex() {
837 testApexes = append(testApexes, aa)
838 }
839 }
840 return RemoveListFromList(CopyOf(apex_available), testApexes)
841}
842
Cole Faustfb11c1c2023-02-10 11:27:46 -0800843func ConvertApexAvailableToTags(apexAvailable []string) []string {
844 if len(apexAvailable) == 0 {
845 // We need nil specifically to make bp2build not add the tags property at all,
846 // instead of adding it with an empty list
847 return nil
848 }
849 result := make([]string, 0, len(apexAvailable))
850 for _, a := range apexAvailable {
851 result = append(result, "apex_available="+a)
852 }
853 return result
854}
855
Spandan Das39b6cc52023-04-12 19:05:49 +0000856// ConvertApexAvailableToTagsWithoutTestApexes converts a list of apex names to a list of bazel tags
857// This function drops any test apexes from the input.
858func ConvertApexAvailableToTagsWithoutTestApexes(ctx BaseModuleContext, apexAvailable []string) []string {
859 noTestApexes := removeTestApexes(ctx, apexAvailable)
860 return ConvertApexAvailableToTags(noTestApexes)
861}
862
Chris Parsons6666d0f2023-09-22 16:21:53 +0000863func (t *bottomUpMutatorContext) createBazelTargetModule(
Chris Parsons58852a02021-12-09 18:10:18 -0500864 bazelProps bazel.BazelTargetModuleProperties,
865 commonAttrs CommonAttributes,
866 attrs interface{},
867 enabledProperty bazel.BoolAttribute) {
868 constraintAttributes := commonAttrs.fillCommonBp2BuildModuleAttrs(t, enabledProperty)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000869 mod := t.Module()
Liz Kammer2ada09a2021-08-11 00:17:36 -0400870 info := bp2buildInfo{
Spandan Das3131d672023-08-03 22:33:47 +0000871 Dir: dirForBazelTargetGeneration(t, &commonAttrs),
Chris Parsons58852a02021-12-09 18:10:18 -0500872 BazelProps: bazelProps,
873 CommonAttrs: commonAttrs,
874 ConstraintAttrs: constraintAttributes,
875 Attrs: attrs,
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500876 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000877 mod.base().addBp2buildInfo(info)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500878}
879
Colin Crossdc35e212019-06-06 16:13:11 -0700880// android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
881// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
882// ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
883// non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
884// methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
885
Colin Crosscb55e082019-07-01 15:32:31 -0700886func (t *topDownMutatorContext) MutatorName() string {
887 return t.bp.MutatorName()
888}
889
Colin Crossdc35e212019-06-06 16:13:11 -0700890func (t *topDownMutatorContext) Rename(name string) {
891 t.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700892 t.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700893}
894
Liz Kammerf31c9002022-04-26 09:08:55 -0400895func (t *topDownMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
896 return t.bp.CreateModule(factory, name, props...)
897}
898
Colin Crosse003c4a2019-09-25 12:58:36 -0700899func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400900 return createModule(t, factory, "_topDownMutatorModule", props...)
Colin Crossdc35e212019-06-06 16:13:11 -0700901}
902
Liz Kammera060c452021-03-24 10:14:47 -0400903func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
Liz Kammerf31c9002022-04-26 09:08:55 -0400904 module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), "", props...).(Module)
Liz Kammera060c452021-03-24 10:14:47 -0400905 return module
906}
907
Colin Crosscb55e082019-07-01 15:32:31 -0700908func (b *bottomUpMutatorContext) MutatorName() string {
909 return b.bp.MutatorName()
910}
911
Colin Crossdc35e212019-06-06 16:13:11 -0700912func (b *bottomUpMutatorContext) Rename(name string) {
913 b.bp.Rename(name)
Colin Cross9a362232019-07-01 15:32:45 -0700914 b.Module().base().commonProperties.DebugName = name
Colin Crossdc35e212019-06-06 16:13:11 -0700915}
916
Colin Cross4f1dcb02020-09-16 18:45:04 -0700917func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400918 if b.baseModuleContext.checkedMissingDeps() {
919 panic("Adding deps not allowed after checking for missing deps")
920 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700921 return b.bp.AddDependency(module, tag, name...)
Colin Crossdc35e212019-06-06 16:13:11 -0700922}
923
924func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
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 Crossdc35e212019-06-06 16:13:11 -0700928 b.bp.AddReverseDependency(module, tag, name)
929}
930
Colin Cross43b92e02019-11-18 15:28:57 -0800931func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000932 if b.finalPhase {
933 panic("CreateVariations not allowed in FinalDepsMutators")
934 }
935
Colin Cross9a362232019-07-01 15:32:45 -0700936 modules := b.bp.CreateVariations(variations...)
937
Colin Cross43b92e02019-11-18 15:28:57 -0800938 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700939 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800940 aModules[i] = modules[i].(Module)
941 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700942 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
943 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
944 }
945
Colin Cross43b92e02019-11-18 15:28:57 -0800946 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700947}
948
Colin Cross43b92e02019-11-18 15:28:57 -0800949func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000950 if b.finalPhase {
951 panic("CreateLocalVariations not allowed in FinalDepsMutators")
952 }
953
Colin Cross9a362232019-07-01 15:32:45 -0700954 modules := b.bp.CreateLocalVariations(variations...)
955
Colin Cross43b92e02019-11-18 15:28:57 -0800956 aModules := make([]Module, len(modules))
Colin Cross9a362232019-07-01 15:32:45 -0700957 for i := range variations {
Colin Cross43b92e02019-11-18 15:28:57 -0800958 aModules[i] = modules[i].(Module)
959 base := aModules[i].base()
Colin Cross9a362232019-07-01 15:32:45 -0700960 base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
961 base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
962 }
963
Colin Cross43b92e02019-11-18 15:28:57 -0800964 return aModules
Colin Crossdc35e212019-06-06 16:13:11 -0700965}
966
967func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
968 b.bp.SetDependencyVariation(variation)
969}
970
Jiyong Park1d1119f2019-07-29 21:27:18 +0900971func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
972 b.bp.SetDefaultDependencyVariation(variation)
973}
974
Colin Crossdc35e212019-06-06 16:13:11 -0700975func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700976 names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400977 if b.baseModuleContext.checkedMissingDeps() {
978 panic("Adding deps not allowed after checking for missing deps")
979 }
Colin Cross4f1dcb02020-09-16 18:45:04 -0700980 return b.bp.AddVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700981}
982
983func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
Colin Cross4f1dcb02020-09-16 18:45:04 -0700984 tag blueprint.DependencyTag, names ...string) []blueprint.Module {
Liz Kammerc13f7852023-05-17 13:01:48 -0400985 if b.baseModuleContext.checkedMissingDeps() {
986 panic("Adding deps not allowed after checking for missing deps")
987 }
Colin Crossdc35e212019-06-06 16:13:11 -0700988
Colin Cross4f1dcb02020-09-16 18:45:04 -0700989 return b.bp.AddFarVariationDependencies(variations, tag, names...)
Colin Crossdc35e212019-06-06 16:13:11 -0700990}
991
992func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
993 b.bp.AddInterVariantDependency(tag, from, to)
994}
995
996func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
Liz Kammerc13f7852023-05-17 13:01:48 -0400997 if b.baseModuleContext.checkedMissingDeps() {
998 panic("Adding deps not allowed after checking for missing deps")
999 }
Colin Crossdc35e212019-06-06 16:13:11 -07001000 b.bp.ReplaceDependencies(name)
1001}
Jaewoong Jung9f88ce22019-11-15 10:57:34 -08001002
Paul Duffin80342d72020-06-26 22:08:43 +01001003func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
Liz Kammerc13f7852023-05-17 13:01:48 -04001004 if b.baseModuleContext.checkedMissingDeps() {
1005 panic("Adding deps not allowed after checking for missing deps")
1006 }
Paul Duffin80342d72020-06-26 22:08:43 +01001007 b.bp.ReplaceDependenciesIf(name, predicate)
1008}
1009
Jaewoong Jung9f88ce22019-11-15 10:57:34 -08001010func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
1011 b.bp.AliasVariation(variationName)
1012}
Colin Cross1b9604b2020-08-11 12:03:56 -07001013
1014func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
1015 b.bp.CreateAliasVariation(fromVariationName, toVariationName)
1016}
Colin Crossd27e7b82020-07-02 11:38:17 -07001017
1018func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
1019 b.bp.SetVariationProvider(module, provider, value)
1020}