blob: c7d7573f1946c9d6a68c2b50218938596a42a0cf [file] [log] [blame]
Colin Cross69452e12023-11-15 11:20:53 -08001// 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
15package android
16
17import (
18 "fmt"
Colin Cross69452e12023-11-15 11:20:53 -080019 "regexp"
20 "strings"
Cole Faustbdd8aee2024-03-14 14:33:02 -070021
22 "github.com/google/blueprint"
Cole Faustfdbf5d42024-04-10 15:01:23 -070023 "github.com/google/blueprint/proptools"
Colin Cross69452e12023-11-15 11:20:53 -080024)
25
26// BaseModuleContext is the same as blueprint.BaseModuleContext except that Config() returns
27// a Config instead of an interface{}, and some methods have been wrapped to use an android.Module
28// instead of a blueprint.Module, plus some extra methods that return Android-specific information
29// about the current module.
30type BaseModuleContext interface {
Colin Cross1d3d9f12024-01-18 14:30:22 -080031 ArchModuleContext
Colin Cross69452e12023-11-15 11:20:53 -080032 EarlyModuleContext
33
34 blueprintBaseModuleContext() blueprint.BaseModuleContext
35
36 // OtherModuleName returns the name of another Module. See BaseModuleContext.ModuleName for more information.
37 // It is intended for use inside the visit functions of Visit* and WalkDeps.
38 OtherModuleName(m blueprint.Module) string
39
40 // OtherModuleDir returns the directory of another Module. See BaseModuleContext.ModuleDir for more information.
41 // It is intended for use inside the visit functions of Visit* and WalkDeps.
42 OtherModuleDir(m blueprint.Module) string
43
44 // OtherModuleErrorf reports an error on another Module. See BaseModuleContext.ModuleErrorf for more information.
45 // It is intended for use inside the visit functions of Visit* and WalkDeps.
46 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
47
48 // OtherModuleDependencyTag returns the dependency tag used to depend on a module, or nil if there is no dependency
49 // on the module. When called inside a Visit* method with current module being visited, and there are multiple
50 // dependencies on the module being visited, it returns the dependency tag used for the current dependency.
51 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
52
53 // OtherModuleExists returns true if a module with the specified name exists, as determined by the NameInterface
54 // passed to Context.SetNameInterface, or SimpleNameInterface if it was not called.
55 OtherModuleExists(name string) bool
56
57 // OtherModuleDependencyVariantExists returns true if a module with the
58 // specified name and variant exists. The variant must match the given
59 // variations. It must also match all the non-local variations of the current
60 // module. In other words, it checks for the module that AddVariationDependencies
61 // would add a dependency on with the same arguments.
62 OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool
63
64 // OtherModuleFarDependencyVariantExists returns true if a module with the
65 // specified name and variant exists. The variant must match the given
66 // variations, but not the non-local variations of the current module. In
67 // other words, it checks for the module that AddFarVariationDependencies
68 // would add a dependency on with the same arguments.
69 OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool
70
71 // OtherModuleReverseDependencyVariantExists returns true if a module with the
72 // specified name exists with the same variations as the current module. In
73 // other words, it checks for the module that AddReverseDependency would add a
74 // dependency on with the same argument.
75 OtherModuleReverseDependencyVariantExists(name string) bool
76
77 // OtherModuleType returns the type of another Module. See BaseModuleContext.ModuleType for more information.
78 // It is intended for use inside the visit functions of Visit* and WalkDeps.
79 OtherModuleType(m blueprint.Module) string
80
Colin Cross24c1cbe2023-12-21 23:42:56 +000081 // otherModuleProvider returns the value for a provider for the given module. If the value is
82 // not set it returns nil and false. The value returned may be a deep copy of the value originally
83 // passed to SetProvider.
84 //
85 // This method shouldn't be used directly, prefer the type-safe android.OtherModuleProvider instead.
Colin Cross3c0a83d2023-12-12 14:13:26 -080086 otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool)
Colin Cross69452e12023-11-15 11:20:53 -080087
88 // Provider returns the value for a provider for the current module. If the value is
Colin Cross24c1cbe2023-12-21 23:42:56 +000089 // not set it returns nil and false. It panics if called before the appropriate
Colin Cross69452e12023-11-15 11:20:53 -080090 // mutator or GenerateBuildActions pass for the provider. The value returned may be a deep
91 // copy of the value originally passed to SetProvider.
Colin Cross24c1cbe2023-12-21 23:42:56 +000092 //
93 // This method shouldn't be used directly, prefer the type-safe android.ModuleProvider instead.
Colin Cross3c0a83d2023-12-12 14:13:26 -080094 provider(provider blueprint.AnyProviderKey) (any, bool)
Colin Cross69452e12023-11-15 11:20:53 -080095
Colin Cross24c1cbe2023-12-21 23:42:56 +000096 // setProvider sets the value for a provider for the current module. It panics if not called
Colin Cross69452e12023-11-15 11:20:53 -080097 // during the appropriate mutator or GenerateBuildActions pass for the provider, if the value
98 // is not of the appropriate type, or if the value has already been set. The value should not
99 // be modified after being passed to SetProvider.
Colin Cross24c1cbe2023-12-21 23:42:56 +0000100 //
101 // This method shouldn't be used directly, prefer the type-safe android.SetProvider instead.
102 setProvider(provider blueprint.AnyProviderKey, value any)
Colin Cross69452e12023-11-15 11:20:53 -0800103
104 GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module
105
106 // GetDirectDepWithTag returns the Module the direct dependency with the specified name, or nil if
107 // none exists. It panics if the dependency does not have the specified tag. It skips any
108 // dependencies that are not an android.Module.
109 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
110
LaMont Jones34314b72024-01-18 18:27:35 +0000111 // GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
Colin Cross69452e12023-11-15 11:20:53 -0800112 // name, or nil if none exists. If there are multiple dependencies on the same module it returns
113 // the first DependencyTag.
114 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
115
Colin Cross648daea2024-09-12 14:35:29 -0700116 // VisitDirectDeps calls visit for each direct dependency. If there are multiple
117 // direct dependencies on the same module visit will be called multiple times on that module
118 // and OtherModuleDependencyTag will return a different tag for each. It raises an error if any of the
119 // dependencies are disabled.
120 //
121 // The Module passed to the visit function should not be retained outside of the visit
122 // function, it may be invalidated by future mutators.
123 VisitDirectDeps(visit func(Module))
124
125 // VisitDirectDeps calls visit for each direct dependency. If there are multiple
Colin Cross69452e12023-11-15 11:20:53 -0800126 // direct dependencies on the same module visit will be called multiple times on that module
127 // and OtherModuleDependencyTag will return a different tag for each.
128 //
129 // The Module passed to the visit function should not be retained outside of the visit
130 // function, it may be invalidated by future mutators.
Colin Cross648daea2024-09-12 14:35:29 -0700131 VisitDirectDepsAllowDisabled(visit func(Module))
Colin Cross69452e12023-11-15 11:20:53 -0800132
133 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
134
135 // VisitDirectDepsIf calls pred for each direct dependency, and if pred returns true calls visit. If there are
136 // multiple direct dependencies on the same module pred and visit will be called multiple times on that module and
137 // OtherModuleDependencyTag will return a different tag for each. It skips any
138 // dependencies that are not an android.Module.
139 //
140 // The Module passed to the visit function should not be retained outside of the visit function, it may be
141 // invalidated by future mutators.
142 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
143 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
144 VisitDepsDepthFirst(visit func(Module))
145 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
146 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
147
148 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
149 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
150 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
151 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
152 // any dependencies that are not an android.Module.
153 //
154 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
155 // invalidated by future mutators.
156 WalkDeps(visit func(child, parent Module) bool)
157
Colin Cross69452e12023-11-15 11:20:53 -0800158 // GetWalkPath is supposed to be called in visit function passed in WalkDeps()
159 // and returns a top-down dependency path from a start module to current child module.
160 GetWalkPath() []Module
161
162 // PrimaryModule returns the first variant of the current module. Variants of a module are always visited in
163 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from the
164 // Module returned by PrimaryModule without data races. This can be used to perform singleton actions that are
165 // only done once for all variants of a module.
166 PrimaryModule() Module
167
168 // FinalModule returns the last variant of the current module. Variants of a module are always visited in
169 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from all
170 // variants using VisitAllModuleVariants if the current module == FinalModule(). This can be used to perform
171 // singleton actions that are only done once for all variants of a module.
172 FinalModule() Module
173
174 // VisitAllModuleVariants calls visit for each variant of the current module. Variants of a module are always
175 // visited in order by mutators and GenerateBuildActions, so the data created by the current mutator can be read
176 // from all variants if the current module == FinalModule(). Otherwise, care must be taken to not access any
177 // data modified by the current mutator.
178 VisitAllModuleVariants(visit func(Module))
179
180 // GetTagPath is supposed to be called in visit function passed in WalkDeps()
181 // and returns a top-down dependency tags path from a start module to current child module.
182 // It has one less entry than GetWalkPath() as it contains the dependency tags that
183 // exist between each adjacent pair of modules in the GetWalkPath().
184 // GetTagPath()[i] is the tag between GetWalkPath()[i] and GetWalkPath()[i+1]
185 GetTagPath() []blueprint.DependencyTag
186
187 // GetPathString is supposed to be called in visit function passed in WalkDeps()
188 // and returns a multi-line string showing the modules and dependency tags
189 // among them along the top-down dependency path from a start module to current child module.
190 // skipFirst when set to true, the output doesn't include the start module,
191 // which is already printed when this function is used along with ModuleErrorf().
192 GetPathString(skipFirst bool) string
193
194 AddMissingDependencies(missingDeps []string)
195
196 // getMissingDependencies returns the list of missing dependencies.
197 // Calling this function prevents adding new dependencies.
198 getMissingDependencies() []string
Cole Faustbdd8aee2024-03-14 14:33:02 -0700199
200 // EvaluateConfiguration makes ModuleContext a valid proptools.ConfigurableEvaluator, so this context
201 // can be used to evaluate the final value of Configurable properties.
Cole Faustfdbf5d42024-04-10 15:01:23 -0700202 EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue
Colin Cross69452e12023-11-15 11:20:53 -0800203}
204
205type baseModuleContext struct {
206 bp blueprint.BaseModuleContext
207 earlyModuleContext
Colin Cross1d3d9f12024-01-18 14:30:22 -0800208 archModuleContext
Colin Cross69452e12023-11-15 11:20:53 -0800209
210 walkPath []Module
211 tagPath []blueprint.DependencyTag
212
213 strictVisitDeps bool // If true, enforce that all dependencies are enabled
214
Colin Cross69452e12023-11-15 11:20:53 -0800215}
216
Colin Cross69452e12023-11-15 11:20:53 -0800217func (b *baseModuleContext) OtherModuleName(m blueprint.Module) string {
218 return b.bp.OtherModuleName(m)
219}
220func (b *baseModuleContext) OtherModuleDir(m blueprint.Module) string { return b.bp.OtherModuleDir(m) }
221func (b *baseModuleContext) OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) {
222 b.bp.OtherModuleErrorf(m, fmt, args...)
223}
224func (b *baseModuleContext) OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag {
225 return b.bp.OtherModuleDependencyTag(m)
226}
227func (b *baseModuleContext) OtherModuleExists(name string) bool { return b.bp.OtherModuleExists(name) }
228func (b *baseModuleContext) OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool {
229 return b.bp.OtherModuleDependencyVariantExists(variations, name)
230}
231func (b *baseModuleContext) OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool {
232 return b.bp.OtherModuleFarDependencyVariantExists(variations, name)
233}
234func (b *baseModuleContext) OtherModuleReverseDependencyVariantExists(name string) bool {
235 return b.bp.OtherModuleReverseDependencyVariantExists(name)
236}
237func (b *baseModuleContext) OtherModuleType(m blueprint.Module) string {
238 return b.bp.OtherModuleType(m)
239}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800240
241func (b *baseModuleContext) otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800242 return b.bp.OtherModuleProvider(m, provider)
243}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800244
Colin Cross3c0a83d2023-12-12 14:13:26 -0800245func (b *baseModuleContext) provider(provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800246 return b.bp.Provider(provider)
247}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800248
Colin Cross24c1cbe2023-12-21 23:42:56 +0000249func (b *baseModuleContext) setProvider(provider blueprint.AnyProviderKey, value any) {
Colin Cross69452e12023-11-15 11:20:53 -0800250 b.bp.SetProvider(provider, value)
251}
252
253func (b *baseModuleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
254 return b.bp.GetDirectDepWithTag(name, tag)
255}
256
257func (b *baseModuleContext) blueprintBaseModuleContext() blueprint.BaseModuleContext {
258 return b.bp
259}
260
Colin Cross69452e12023-11-15 11:20:53 -0800261func (b *baseModuleContext) AddMissingDependencies(deps []string) {
262 if deps != nil {
263 missingDeps := &b.Module().base().commonProperties.MissingDeps
264 *missingDeps = append(*missingDeps, deps...)
265 *missingDeps = FirstUniqueStrings(*missingDeps)
266 }
267}
268
269func (b *baseModuleContext) checkedMissingDeps() bool {
270 return b.Module().base().commonProperties.CheckedMissingDeps
271}
272
273func (b *baseModuleContext) getMissingDependencies() []string {
274 checked := &b.Module().base().commonProperties.CheckedMissingDeps
275 *checked = true
276 var missingDeps []string
277 missingDeps = append(missingDeps, b.Module().base().commonProperties.MissingDeps...)
278 missingDeps = append(missingDeps, b.bp.EarlyGetMissingDependencies()...)
279 missingDeps = FirstUniqueStrings(missingDeps)
280 return missingDeps
281}
282
283type AllowDisabledModuleDependency interface {
284 blueprint.DependencyTag
285 AllowDisabledModuleDependency(target Module) bool
286}
287
Jiyong Park8bcf3c62024-03-18 18:37:10 +0900288type AlwaysAllowDisabledModuleDependencyTag struct{}
289
290func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependency(Module) bool {
291 return true
292}
293
Colin Cross648daea2024-09-12 14:35:29 -0700294func (b *baseModuleContext) validateAndroidModule(module blueprint.Module, tag blueprint.DependencyTag, strict bool) Module {
Colin Cross69452e12023-11-15 11:20:53 -0800295 aModule, _ := module.(Module)
296
297 if !strict {
298 return aModule
299 }
300
301 if aModule == nil {
Colin Cross648daea2024-09-12 14:35:29 -0700302 panic(fmt.Errorf("module %q (%#v) not an android module", b.OtherModuleName(module), tag))
Colin Cross69452e12023-11-15 11:20:53 -0800303 }
304
Cole Fausta963b942024-04-11 17:43:00 -0700305 if !aModule.Enabled(b) {
Colin Cross69452e12023-11-15 11:20:53 -0800306 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependency(aModule) {
307 if b.Config().AllowMissingDependencies() {
308 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
309 } else {
310 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
311 }
312 }
313 return nil
314 }
315 return aModule
316}
317
318type dep struct {
319 mod blueprint.Module
320 tag blueprint.DependencyTag
321}
322
323func (b *baseModuleContext) getDirectDepsInternal(name string, tag blueprint.DependencyTag) []dep {
324 var deps []dep
Colin Cross648daea2024-09-12 14:35:29 -0700325 b.VisitDirectDeps(func(module Module) {
326 if module.base().BaseModuleName() == name {
Colin Cross69452e12023-11-15 11:20:53 -0800327 returnedTag := b.bp.OtherModuleDependencyTag(module)
328 if tag == nil || returnedTag == tag {
329 deps = append(deps, dep{module, returnedTag})
330 }
331 }
332 })
333 return deps
334}
335
336func (b *baseModuleContext) getDirectDepInternal(name string, tag blueprint.DependencyTag) (blueprint.Module, blueprint.DependencyTag) {
337 deps := b.getDirectDepsInternal(name, tag)
338 if len(deps) == 1 {
339 return deps[0].mod, deps[0].tag
340 } else if len(deps) >= 2 {
341 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
342 name, b.ModuleName()))
343 } else {
344 return nil, nil
345 }
346}
347
348func (b *baseModuleContext) getDirectDepFirstTag(name string) (blueprint.Module, blueprint.DependencyTag) {
349 foundDeps := b.getDirectDepsInternal(name, nil)
350 deps := map[blueprint.Module]bool{}
351 for _, dep := range foundDeps {
352 deps[dep.mod] = true
353 }
354 if len(deps) == 1 {
355 return foundDeps[0].mod, foundDeps[0].tag
356 } else if len(deps) >= 2 {
357 // this could happen if two dependencies have the same name in different namespaces
358 // TODO(b/186554727): this should not occur if namespaces are handled within
359 // getDirectDepsInternal.
360 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
361 name, b.ModuleName()))
362 } else {
363 return nil, nil
364 }
365}
366
367func (b *baseModuleContext) GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module {
368 var deps []Module
Colin Cross648daea2024-09-12 14:35:29 -0700369 b.VisitDirectDeps(func(module Module) {
370 if b.bp.OtherModuleDependencyTag(module) == tag {
371 deps = append(deps, module)
Colin Cross69452e12023-11-15 11:20:53 -0800372 }
373 })
374 return deps
375}
376
377// GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
378// name, or nil if none exists. If there are multiple dependencies on the same module it returns the
379// first DependencyTag.
380func (b *baseModuleContext) GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) {
381 return b.getDirectDepFirstTag(name)
382}
383
Colin Cross69452e12023-11-15 11:20:53 -0800384func (b *baseModuleContext) VisitDirectDeps(visit func(Module)) {
385 b.bp.VisitDirectDeps(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700386 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800387 visit(aModule)
388 }
389 })
390}
391
Colin Cross648daea2024-09-12 14:35:29 -0700392func (b *baseModuleContext) VisitDirectDepsAllowDisabled(visit func(Module)) {
393 b.bp.VisitDirectDeps(func(module blueprint.Module) {
394 visit(module.(Module))
395 })
396}
397
Colin Cross69452e12023-11-15 11:20:53 -0800398func (b *baseModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
399 b.bp.VisitDirectDeps(func(module blueprint.Module) {
400 if b.bp.OtherModuleDependencyTag(module) == tag {
Colin Cross648daea2024-09-12 14:35:29 -0700401 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800402 visit(aModule)
403 }
404 }
405 })
406}
407
408func (b *baseModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
409 b.bp.VisitDirectDepsIf(
410 // pred
411 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700412 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800413 return pred(aModule)
414 } else {
415 return false
416 }
417 },
418 // visit
419 func(module blueprint.Module) {
420 visit(module.(Module))
421 })
422}
423
424func (b *baseModuleContext) VisitDepsDepthFirst(visit func(Module)) {
425 b.bp.VisitDepsDepthFirst(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700426 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800427 visit(aModule)
428 }
429 })
430}
431
432func (b *baseModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
433 b.bp.VisitDepsDepthFirstIf(
434 // pred
435 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700436 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800437 return pred(aModule)
438 } else {
439 return false
440 }
441 },
442 // visit
443 func(module blueprint.Module) {
444 visit(module.(Module))
445 })
446}
447
Colin Cross69452e12023-11-15 11:20:53 -0800448func (b *baseModuleContext) WalkDeps(visit func(Module, Module) bool) {
449 b.walkPath = []Module{b.Module()}
450 b.tagPath = []blueprint.DependencyTag{}
451 b.bp.WalkDeps(func(child, parent blueprint.Module) bool {
452 childAndroidModule, _ := child.(Module)
453 parentAndroidModule, _ := parent.(Module)
454 if childAndroidModule != nil && parentAndroidModule != nil {
455 // record walkPath before visit
456 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
457 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
458 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
459 }
460 b.walkPath = append(b.walkPath, childAndroidModule)
461 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
462 return visit(childAndroidModule, parentAndroidModule)
463 } else {
464 return false
465 }
466 })
467}
468
469func (b *baseModuleContext) GetWalkPath() []Module {
470 return b.walkPath
471}
472
473func (b *baseModuleContext) GetTagPath() []blueprint.DependencyTag {
474 return b.tagPath
475}
476
477func (b *baseModuleContext) VisitAllModuleVariants(visit func(Module)) {
478 b.bp.VisitAllModuleVariants(func(module blueprint.Module) {
479 visit(module.(Module))
480 })
481}
482
483func (b *baseModuleContext) PrimaryModule() Module {
484 return b.bp.PrimaryModule().(Module)
485}
486
487func (b *baseModuleContext) FinalModule() Module {
488 return b.bp.FinalModule().(Module)
489}
490
491// IsMetaDependencyTag returns true for cross-cutting metadata dependencies.
492func IsMetaDependencyTag(tag blueprint.DependencyTag) bool {
493 if tag == licenseKindTag {
494 return true
495 } else if tag == licensesTag {
496 return true
Jihoon Kanga3a05462024-04-05 00:36:44 +0000497 } else if tag == AcDepTag {
Colin Cross69452e12023-11-15 11:20:53 -0800498 return true
499 }
500 return false
501}
502
503// A regexp for removing boilerplate from BaseDependencyTag from the string representation of
504// a dependency tag.
505var tagCleaner = regexp.MustCompile(`\QBaseDependencyTag:{}\E(, )?`)
506
507// PrettyPrintTag returns string representation of the tag, but prefers
508// custom String() method if available.
509func PrettyPrintTag(tag blueprint.DependencyTag) string {
510 // Use tag's custom String() method if available.
511 if stringer, ok := tag.(fmt.Stringer); ok {
512 return stringer.String()
513 }
514
515 // Otherwise, get a default string representation of the tag's struct.
516 tagString := fmt.Sprintf("%T: %+v", tag, tag)
517
518 // Remove the boilerplate from BaseDependencyTag as it adds no value.
519 tagString = tagCleaner.ReplaceAllString(tagString, "")
520 return tagString
521}
522
523func (b *baseModuleContext) GetPathString(skipFirst bool) string {
524 sb := strings.Builder{}
525 tagPath := b.GetTagPath()
526 walkPath := b.GetWalkPath()
527 if !skipFirst {
528 sb.WriteString(walkPath[0].String())
529 }
530 for i, m := range walkPath[1:] {
531 sb.WriteString("\n")
532 sb.WriteString(fmt.Sprintf(" via tag %s\n", PrettyPrintTag(tagPath[i])))
533 sb.WriteString(fmt.Sprintf(" -> %s", m.String()))
534 }
535 return sb.String()
536}
Cole Faustbdd8aee2024-03-14 14:33:02 -0700537
Cole Faustfdbf5d42024-04-10 15:01:23 -0700538func (m *baseModuleContext) EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue {
539 return m.Module().ConfigurableEvaluator(m).EvaluateConfiguration(condition, property)
Cole Faustbdd8aee2024-03-14 14:33:02 -0700540}