blob: e895d76f6b3372bfef73c3075464e459474233c9 [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
Cole Faust4e2bf9f2024-09-11 13:26:20 -0700203
204 // HasMutatorFinished returns true if the given mutator has finished running.
205 // It will panic if given an invalid mutator name.
206 HasMutatorFinished(mutatorName string) bool
Colin Cross69452e12023-11-15 11:20:53 -0800207}
208
209type baseModuleContext struct {
210 bp blueprint.BaseModuleContext
211 earlyModuleContext
Colin Cross1d3d9f12024-01-18 14:30:22 -0800212 archModuleContext
Colin Cross69452e12023-11-15 11:20:53 -0800213
214 walkPath []Module
215 tagPath []blueprint.DependencyTag
216
217 strictVisitDeps bool // If true, enforce that all dependencies are enabled
218
Colin Cross69452e12023-11-15 11:20:53 -0800219}
220
Colin Cross69452e12023-11-15 11:20:53 -0800221func (b *baseModuleContext) OtherModuleName(m blueprint.Module) string {
222 return b.bp.OtherModuleName(m)
223}
224func (b *baseModuleContext) OtherModuleDir(m blueprint.Module) string { return b.bp.OtherModuleDir(m) }
225func (b *baseModuleContext) OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) {
226 b.bp.OtherModuleErrorf(m, fmt, args...)
227}
228func (b *baseModuleContext) OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag {
229 return b.bp.OtherModuleDependencyTag(m)
230}
231func (b *baseModuleContext) OtherModuleExists(name string) bool { return b.bp.OtherModuleExists(name) }
232func (b *baseModuleContext) OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool {
233 return b.bp.OtherModuleDependencyVariantExists(variations, name)
234}
235func (b *baseModuleContext) OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool {
236 return b.bp.OtherModuleFarDependencyVariantExists(variations, name)
237}
238func (b *baseModuleContext) OtherModuleReverseDependencyVariantExists(name string) bool {
239 return b.bp.OtherModuleReverseDependencyVariantExists(name)
240}
241func (b *baseModuleContext) OtherModuleType(m blueprint.Module) string {
242 return b.bp.OtherModuleType(m)
243}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800244
245func (b *baseModuleContext) otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800246 return b.bp.OtherModuleProvider(m, provider)
247}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800248
Colin Cross3c0a83d2023-12-12 14:13:26 -0800249func (b *baseModuleContext) provider(provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800250 return b.bp.Provider(provider)
251}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800252
Colin Cross24c1cbe2023-12-21 23:42:56 +0000253func (b *baseModuleContext) setProvider(provider blueprint.AnyProviderKey, value any) {
Colin Cross69452e12023-11-15 11:20:53 -0800254 b.bp.SetProvider(provider, value)
255}
256
Cole Faust4e2bf9f2024-09-11 13:26:20 -0700257func (b *baseModuleContext) HasMutatorFinished(mutatorName string) bool {
258 return b.bp.HasMutatorFinished(mutatorName)
259}
260
Colin Cross69452e12023-11-15 11:20:53 -0800261func (b *baseModuleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
262 return b.bp.GetDirectDepWithTag(name, tag)
263}
264
265func (b *baseModuleContext) blueprintBaseModuleContext() blueprint.BaseModuleContext {
266 return b.bp
267}
268
Colin Cross69452e12023-11-15 11:20:53 -0800269func (b *baseModuleContext) AddMissingDependencies(deps []string) {
270 if deps != nil {
271 missingDeps := &b.Module().base().commonProperties.MissingDeps
272 *missingDeps = append(*missingDeps, deps...)
273 *missingDeps = FirstUniqueStrings(*missingDeps)
274 }
275}
276
277func (b *baseModuleContext) checkedMissingDeps() bool {
278 return b.Module().base().commonProperties.CheckedMissingDeps
279}
280
281func (b *baseModuleContext) getMissingDependencies() []string {
282 checked := &b.Module().base().commonProperties.CheckedMissingDeps
283 *checked = true
284 var missingDeps []string
285 missingDeps = append(missingDeps, b.Module().base().commonProperties.MissingDeps...)
286 missingDeps = append(missingDeps, b.bp.EarlyGetMissingDependencies()...)
287 missingDeps = FirstUniqueStrings(missingDeps)
288 return missingDeps
289}
290
291type AllowDisabledModuleDependency interface {
292 blueprint.DependencyTag
293 AllowDisabledModuleDependency(target Module) bool
294}
295
Jiyong Park8bcf3c62024-03-18 18:37:10 +0900296type AlwaysAllowDisabledModuleDependencyTag struct{}
297
298func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependency(Module) bool {
299 return true
300}
301
Colin Cross648daea2024-09-12 14:35:29 -0700302func (b *baseModuleContext) validateAndroidModule(module blueprint.Module, tag blueprint.DependencyTag, strict bool) Module {
Colin Cross69452e12023-11-15 11:20:53 -0800303 aModule, _ := module.(Module)
304
305 if !strict {
306 return aModule
307 }
308
309 if aModule == nil {
Colin Cross648daea2024-09-12 14:35:29 -0700310 panic(fmt.Errorf("module %q (%#v) not an android module", b.OtherModuleName(module), tag))
Colin Cross69452e12023-11-15 11:20:53 -0800311 }
312
Cole Fausta963b942024-04-11 17:43:00 -0700313 if !aModule.Enabled(b) {
Colin Cross69452e12023-11-15 11:20:53 -0800314 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependency(aModule) {
315 if b.Config().AllowMissingDependencies() {
316 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
317 } else {
318 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
319 }
320 }
321 return nil
322 }
323 return aModule
324}
325
326type dep struct {
327 mod blueprint.Module
328 tag blueprint.DependencyTag
329}
330
331func (b *baseModuleContext) getDirectDepsInternal(name string, tag blueprint.DependencyTag) []dep {
332 var deps []dep
Colin Cross648daea2024-09-12 14:35:29 -0700333 b.VisitDirectDeps(func(module Module) {
334 if module.base().BaseModuleName() == name {
Colin Cross69452e12023-11-15 11:20:53 -0800335 returnedTag := b.bp.OtherModuleDependencyTag(module)
336 if tag == nil || returnedTag == tag {
337 deps = append(deps, dep{module, returnedTag})
338 }
339 }
340 })
341 return deps
342}
343
344func (b *baseModuleContext) getDirectDepInternal(name string, tag blueprint.DependencyTag) (blueprint.Module, blueprint.DependencyTag) {
345 deps := b.getDirectDepsInternal(name, tag)
346 if len(deps) == 1 {
347 return deps[0].mod, deps[0].tag
348 } else if len(deps) >= 2 {
349 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
350 name, b.ModuleName()))
351 } else {
352 return nil, nil
353 }
354}
355
356func (b *baseModuleContext) getDirectDepFirstTag(name string) (blueprint.Module, blueprint.DependencyTag) {
357 foundDeps := b.getDirectDepsInternal(name, nil)
358 deps := map[blueprint.Module]bool{}
359 for _, dep := range foundDeps {
360 deps[dep.mod] = true
361 }
362 if len(deps) == 1 {
363 return foundDeps[0].mod, foundDeps[0].tag
364 } else if len(deps) >= 2 {
365 // this could happen if two dependencies have the same name in different namespaces
366 // TODO(b/186554727): this should not occur if namespaces are handled within
367 // getDirectDepsInternal.
368 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
369 name, b.ModuleName()))
370 } else {
371 return nil, nil
372 }
373}
374
375func (b *baseModuleContext) GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module {
376 var deps []Module
Colin Cross648daea2024-09-12 14:35:29 -0700377 b.VisitDirectDeps(func(module Module) {
378 if b.bp.OtherModuleDependencyTag(module) == tag {
379 deps = append(deps, module)
Colin Cross69452e12023-11-15 11:20:53 -0800380 }
381 })
382 return deps
383}
384
385// GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
386// name, or nil if none exists. If there are multiple dependencies on the same module it returns the
387// first DependencyTag.
388func (b *baseModuleContext) GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) {
389 return b.getDirectDepFirstTag(name)
390}
391
Colin Cross69452e12023-11-15 11:20:53 -0800392func (b *baseModuleContext) VisitDirectDeps(visit func(Module)) {
393 b.bp.VisitDirectDeps(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700394 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800395 visit(aModule)
396 }
397 })
398}
399
Colin Cross648daea2024-09-12 14:35:29 -0700400func (b *baseModuleContext) VisitDirectDepsAllowDisabled(visit func(Module)) {
401 b.bp.VisitDirectDeps(func(module blueprint.Module) {
402 visit(module.(Module))
403 })
404}
405
Colin Cross69452e12023-11-15 11:20:53 -0800406func (b *baseModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
407 b.bp.VisitDirectDeps(func(module blueprint.Module) {
408 if b.bp.OtherModuleDependencyTag(module) == tag {
Colin Cross648daea2024-09-12 14:35:29 -0700409 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800410 visit(aModule)
411 }
412 }
413 })
414}
415
416func (b *baseModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
417 b.bp.VisitDirectDepsIf(
418 // pred
419 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700420 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800421 return pred(aModule)
422 } else {
423 return false
424 }
425 },
426 // visit
427 func(module blueprint.Module) {
428 visit(module.(Module))
429 })
430}
431
432func (b *baseModuleContext) VisitDepsDepthFirst(visit func(Module)) {
433 b.bp.VisitDepsDepthFirst(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700434 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800435 visit(aModule)
436 }
437 })
438}
439
440func (b *baseModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
441 b.bp.VisitDepsDepthFirstIf(
442 // pred
443 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700444 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800445 return pred(aModule)
446 } else {
447 return false
448 }
449 },
450 // visit
451 func(module blueprint.Module) {
452 visit(module.(Module))
453 })
454}
455
Colin Cross69452e12023-11-15 11:20:53 -0800456func (b *baseModuleContext) WalkDeps(visit func(Module, Module) bool) {
457 b.walkPath = []Module{b.Module()}
458 b.tagPath = []blueprint.DependencyTag{}
459 b.bp.WalkDeps(func(child, parent blueprint.Module) bool {
460 childAndroidModule, _ := child.(Module)
461 parentAndroidModule, _ := parent.(Module)
462 if childAndroidModule != nil && parentAndroidModule != nil {
463 // record walkPath before visit
464 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
465 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
466 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
467 }
468 b.walkPath = append(b.walkPath, childAndroidModule)
469 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
470 return visit(childAndroidModule, parentAndroidModule)
471 } else {
472 return false
473 }
474 })
475}
476
477func (b *baseModuleContext) GetWalkPath() []Module {
478 return b.walkPath
479}
480
481func (b *baseModuleContext) GetTagPath() []blueprint.DependencyTag {
482 return b.tagPath
483}
484
485func (b *baseModuleContext) VisitAllModuleVariants(visit func(Module)) {
486 b.bp.VisitAllModuleVariants(func(module blueprint.Module) {
487 visit(module.(Module))
488 })
489}
490
491func (b *baseModuleContext) PrimaryModule() Module {
492 return b.bp.PrimaryModule().(Module)
493}
494
495func (b *baseModuleContext) FinalModule() Module {
496 return b.bp.FinalModule().(Module)
497}
498
499// IsMetaDependencyTag returns true for cross-cutting metadata dependencies.
500func IsMetaDependencyTag(tag blueprint.DependencyTag) bool {
501 if tag == licenseKindTag {
502 return true
503 } else if tag == licensesTag {
504 return true
Jihoon Kanga3a05462024-04-05 00:36:44 +0000505 } else if tag == AcDepTag {
Colin Cross69452e12023-11-15 11:20:53 -0800506 return true
507 }
508 return false
509}
510
511// A regexp for removing boilerplate from BaseDependencyTag from the string representation of
512// a dependency tag.
513var tagCleaner = regexp.MustCompile(`\QBaseDependencyTag:{}\E(, )?`)
514
515// PrettyPrintTag returns string representation of the tag, but prefers
516// custom String() method if available.
517func PrettyPrintTag(tag blueprint.DependencyTag) string {
518 // Use tag's custom String() method if available.
519 if stringer, ok := tag.(fmt.Stringer); ok {
520 return stringer.String()
521 }
522
523 // Otherwise, get a default string representation of the tag's struct.
524 tagString := fmt.Sprintf("%T: %+v", tag, tag)
525
526 // Remove the boilerplate from BaseDependencyTag as it adds no value.
527 tagString = tagCleaner.ReplaceAllString(tagString, "")
528 return tagString
529}
530
531func (b *baseModuleContext) GetPathString(skipFirst bool) string {
532 sb := strings.Builder{}
533 tagPath := b.GetTagPath()
534 walkPath := b.GetWalkPath()
535 if !skipFirst {
536 sb.WriteString(walkPath[0].String())
537 }
538 for i, m := range walkPath[1:] {
539 sb.WriteString("\n")
540 sb.WriteString(fmt.Sprintf(" via tag %s\n", PrettyPrintTag(tagPath[i])))
541 sb.WriteString(fmt.Sprintf(" -> %s", m.String()))
542 }
543 return sb.String()
544}
Cole Faustbdd8aee2024-03-14 14:33:02 -0700545
Cole Faustfdbf5d42024-04-10 15:01:23 -0700546func (m *baseModuleContext) EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue {
547 return m.Module().ConfigurableEvaluator(m).EvaluateConfiguration(condition, property)
Cole Faustbdd8aee2024-03-14 14:33:02 -0700548}