blob: 02c01586bf761f5c4df9c03c2b595aa2bf60c2cc [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"
Colin Crossb614cd42024-10-11 12:52:21 -070020 "slices"
Colin Cross69452e12023-11-15 11:20:53 -080021 "strings"
Cole Faustbdd8aee2024-03-14 14:33:02 -070022
23 "github.com/google/blueprint"
Cole Faustfdbf5d42024-04-10 15:01:23 -070024 "github.com/google/blueprint/proptools"
Colin Cross69452e12023-11-15 11:20:53 -080025)
26
27// BaseModuleContext is the same as blueprint.BaseModuleContext except that Config() returns
28// a Config instead of an interface{}, and some methods have been wrapped to use an android.Module
29// instead of a blueprint.Module, plus some extra methods that return Android-specific information
30// about the current module.
31type BaseModuleContext interface {
Colin Cross1d3d9f12024-01-18 14:30:22 -080032 ArchModuleContext
Colin Cross69452e12023-11-15 11:20:53 -080033 EarlyModuleContext
34
35 blueprintBaseModuleContext() blueprint.BaseModuleContext
36
Yu Liudd9ccb42024-10-07 17:07:44 +000037 EqualModules(m1, m2 Module) bool
38
Colin Cross69452e12023-11-15 11:20:53 -080039 // OtherModuleName returns the name of another Module. See BaseModuleContext.ModuleName for more information.
40 // It is intended for use inside the visit functions of Visit* and WalkDeps.
41 OtherModuleName(m blueprint.Module) string
42
43 // OtherModuleDir returns the directory of another Module. See BaseModuleContext.ModuleDir for more information.
44 // It is intended for use inside the visit functions of Visit* and WalkDeps.
45 OtherModuleDir(m blueprint.Module) string
46
47 // OtherModuleErrorf reports an error on another Module. See BaseModuleContext.ModuleErrorf for more information.
48 // It is intended for use inside the visit functions of Visit* and WalkDeps.
49 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
50
51 // OtherModuleDependencyTag returns the dependency tag used to depend on a module, or nil if there is no dependency
52 // on the module. When called inside a Visit* method with current module being visited, and there are multiple
53 // dependencies on the module being visited, it returns the dependency tag used for the current dependency.
54 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
55
56 // OtherModuleExists returns true if a module with the specified name exists, as determined by the NameInterface
57 // passed to Context.SetNameInterface, or SimpleNameInterface if it was not called.
58 OtherModuleExists(name string) bool
59
60 // OtherModuleDependencyVariantExists returns true if a module with the
61 // specified name and variant exists. The variant must match the given
62 // variations. It must also match all the non-local variations of the current
63 // module. In other words, it checks for the module that AddVariationDependencies
64 // would add a dependency on with the same arguments.
65 OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool
66
67 // OtherModuleFarDependencyVariantExists returns true if a module with the
68 // specified name and variant exists. The variant must match the given
69 // variations, but not the non-local variations of the current module. In
70 // other words, it checks for the module that AddFarVariationDependencies
71 // would add a dependency on with the same arguments.
72 OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool
73
74 // OtherModuleReverseDependencyVariantExists returns true if a module with the
75 // specified name exists with the same variations as the current module. In
76 // other words, it checks for the module that AddReverseDependency would add a
77 // dependency on with the same argument.
78 OtherModuleReverseDependencyVariantExists(name string) bool
79
80 // OtherModuleType returns the type of another Module. See BaseModuleContext.ModuleType for more information.
81 // It is intended for use inside the visit functions of Visit* and WalkDeps.
82 OtherModuleType(m blueprint.Module) string
83
Colin Cross24c1cbe2023-12-21 23:42:56 +000084 // otherModuleProvider returns the value for a provider for the given module. If the value is
85 // not set it returns nil and false. The value returned may be a deep copy of the value originally
86 // passed to SetProvider.
87 //
88 // This method shouldn't be used directly, prefer the type-safe android.OtherModuleProvider instead.
Colin Cross3c0a83d2023-12-12 14:13:26 -080089 otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool)
Colin Cross69452e12023-11-15 11:20:53 -080090
Jihoon Kang2a7bf752024-11-01 21:21:25 +000091 // OtherModuleIsAutoGenerated returns true if the module is auto generated by another module
92 // instead of being defined in Android.bp file.
93 OtherModuleIsAutoGenerated(m blueprint.Module) bool
94
Colin Cross69452e12023-11-15 11:20:53 -080095 // Provider returns the value for a provider for the current module. If the value is
Colin Cross24c1cbe2023-12-21 23:42:56 +000096 // not set it returns nil and false. It panics if called before the appropriate
Colin Cross69452e12023-11-15 11:20:53 -080097 // mutator or GenerateBuildActions pass for the provider. The value returned may be a deep
98 // copy of the value originally passed to SetProvider.
Colin Cross24c1cbe2023-12-21 23:42:56 +000099 //
100 // This method shouldn't be used directly, prefer the type-safe android.ModuleProvider instead.
Colin Cross3c0a83d2023-12-12 14:13:26 -0800101 provider(provider blueprint.AnyProviderKey) (any, bool)
Colin Cross69452e12023-11-15 11:20:53 -0800102
Colin Cross24c1cbe2023-12-21 23:42:56 +0000103 // setProvider sets the value for a provider for the current module. It panics if not called
Colin Cross69452e12023-11-15 11:20:53 -0800104 // during the appropriate mutator or GenerateBuildActions pass for the provider, if the value
105 // is not of the appropriate type, or if the value has already been set. The value should not
106 // be modified after being passed to SetProvider.
Colin Cross24c1cbe2023-12-21 23:42:56 +0000107 //
108 // This method shouldn't be used directly, prefer the type-safe android.SetProvider instead.
109 setProvider(provider blueprint.AnyProviderKey, value any)
Colin Cross69452e12023-11-15 11:20:53 -0800110
111 GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module
112
113 // GetDirectDepWithTag returns the Module the direct dependency with the specified name, or nil if
114 // none exists. It panics if the dependency does not have the specified tag. It skips any
115 // dependencies that are not an android.Module.
116 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
117
LaMont Jones34314b72024-01-18 18:27:35 +0000118 // GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
Colin Cross69452e12023-11-15 11:20:53 -0800119 // name, or nil if none exists. If there are multiple dependencies on the same module it returns
120 // the first DependencyTag.
121 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
122
Colin Cross648daea2024-09-12 14:35:29 -0700123 // VisitDirectDeps calls visit for each direct dependency. If there are multiple
124 // direct dependencies on the same module visit will be called multiple times on that module
125 // and OtherModuleDependencyTag will return a different tag for each. It raises an error if any of the
126 // dependencies are disabled.
127 //
128 // The Module passed to the visit function should not be retained outside of the visit
129 // function, it may be invalidated by future mutators.
130 VisitDirectDeps(visit func(Module))
131
132 // VisitDirectDeps calls visit for each direct dependency. If there are multiple
Colin Cross69452e12023-11-15 11:20:53 -0800133 // direct dependencies on the same module visit will be called multiple times on that module
134 // and OtherModuleDependencyTag will return a different tag for each.
135 //
136 // The Module passed to the visit function should not be retained outside of the visit
137 // function, it may be invalidated by future mutators.
Colin Cross648daea2024-09-12 14:35:29 -0700138 VisitDirectDepsAllowDisabled(visit func(Module))
Colin Cross69452e12023-11-15 11:20:53 -0800139
Yu Liudd9ccb42024-10-07 17:07:44 +0000140 // VisitDirectDepsProxyAllowDisabled calls visit for each direct dependency. If there are
141 // multiple direct dependencies on the same module visit will be called multiple times on
142 // that module and OtherModuleDependencyTag will return a different tag for each.
143 //
Yu Liud2a95952024-10-10 00:15:26 +0000144 // The ModuleProxy passed to the visit function should not be retained outside of the visit function, it may be
Yu Liudd9ccb42024-10-07 17:07:44 +0000145 // invalidated by future mutators.
Yu Liud2a95952024-10-10 00:15:26 +0000146 VisitDirectDepsProxyAllowDisabled(visit func(proxy ModuleProxy))
Yu Liudd9ccb42024-10-07 17:07:44 +0000147
Colin Cross69452e12023-11-15 11:20:53 -0800148 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
149
Yu Liud2a95952024-10-10 00:15:26 +0000150 VisitDirectDepsProxyWithTag(tag blueprint.DependencyTag, visit func(proxy ModuleProxy))
151
Colin Cross69452e12023-11-15 11:20:53 -0800152 // VisitDirectDepsIf calls pred for each direct dependency, and if pred returns true calls visit. If there are
153 // multiple direct dependencies on the same module pred and visit will be called multiple times on that module and
154 // OtherModuleDependencyTag will return a different tag for each. It skips any
155 // dependencies that are not an android.Module.
156 //
157 // The Module passed to the visit function should not be retained outside of the visit function, it may be
158 // invalidated by future mutators.
159 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
160 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
161 VisitDepsDepthFirst(visit func(Module))
162 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
163 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
164
165 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
166 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
167 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
168 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
169 // any dependencies that are not an android.Module.
170 //
171 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
172 // invalidated by future mutators.
173 WalkDeps(visit func(child, parent Module) bool)
174
Yu Liudd9ccb42024-10-07 17:07:44 +0000175 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
176 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
177 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
178 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
179 // any dependencies that are not an android.Module.
180 //
181 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
182 // invalidated by future mutators.
Yu Liud2a95952024-10-10 00:15:26 +0000183 WalkDepsProxy(visit func(child, parent ModuleProxy) bool)
Yu Liudd9ccb42024-10-07 17:07:44 +0000184
Colin Cross69452e12023-11-15 11:20:53 -0800185 // GetWalkPath is supposed to be called in visit function passed in WalkDeps()
186 // and returns a top-down dependency path from a start module to current child module.
187 GetWalkPath() []Module
188
189 // PrimaryModule returns the first variant of the current module. Variants of a module are always visited in
190 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from the
191 // Module returned by PrimaryModule without data races. This can be used to perform singleton actions that are
192 // only done once for all variants of a module.
193 PrimaryModule() Module
194
195 // FinalModule returns the last variant of the current module. Variants of a module are always visited in
196 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from all
197 // variants using VisitAllModuleVariants if the current module == FinalModule(). This can be used to perform
198 // singleton actions that are only done once for all variants of a module.
199 FinalModule() Module
200
201 // VisitAllModuleVariants calls visit for each variant of the current module. Variants of a module are always
202 // visited in order by mutators and GenerateBuildActions, so the data created by the current mutator can be read
203 // from all variants if the current module == FinalModule(). Otherwise, care must be taken to not access any
204 // data modified by the current mutator.
205 VisitAllModuleVariants(visit func(Module))
206
207 // GetTagPath is supposed to be called in visit function passed in WalkDeps()
208 // and returns a top-down dependency tags path from a start module to current child module.
209 // It has one less entry than GetWalkPath() as it contains the dependency tags that
210 // exist between each adjacent pair of modules in the GetWalkPath().
211 // GetTagPath()[i] is the tag between GetWalkPath()[i] and GetWalkPath()[i+1]
212 GetTagPath() []blueprint.DependencyTag
213
214 // GetPathString is supposed to be called in visit function passed in WalkDeps()
215 // and returns a multi-line string showing the modules and dependency tags
216 // among them along the top-down dependency path from a start module to current child module.
217 // skipFirst when set to true, the output doesn't include the start module,
218 // which is already printed when this function is used along with ModuleErrorf().
219 GetPathString(skipFirst bool) string
220
221 AddMissingDependencies(missingDeps []string)
222
223 // getMissingDependencies returns the list of missing dependencies.
224 // Calling this function prevents adding new dependencies.
225 getMissingDependencies() []string
Cole Faustbdd8aee2024-03-14 14:33:02 -0700226
227 // EvaluateConfiguration makes ModuleContext a valid proptools.ConfigurableEvaluator, so this context
228 // can be used to evaluate the final value of Configurable properties.
Cole Faustfdbf5d42024-04-10 15:01:23 -0700229 EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue
Colin Cross69452e12023-11-15 11:20:53 -0800230}
231
232type baseModuleContext struct {
233 bp blueprint.BaseModuleContext
234 earlyModuleContext
Colin Cross1d3d9f12024-01-18 14:30:22 -0800235 archModuleContext
Colin Cross69452e12023-11-15 11:20:53 -0800236
237 walkPath []Module
238 tagPath []blueprint.DependencyTag
239
240 strictVisitDeps bool // If true, enforce that all dependencies are enabled
241
Colin Cross69452e12023-11-15 11:20:53 -0800242}
243
Yu Liudd9ccb42024-10-07 17:07:44 +0000244func getWrappedModule(module blueprint.Module) blueprint.Module {
245 if mp, isProxy := module.(ModuleProxy); isProxy {
246 return mp.module
247 }
248 return module
249}
250
251func (b *baseModuleContext) EqualModules(m1, m2 Module) bool {
252 return b.bp.EqualModules(getWrappedModule(m1), getWrappedModule(m2))
253}
254
Colin Cross69452e12023-11-15 11:20:53 -0800255func (b *baseModuleContext) OtherModuleName(m blueprint.Module) string {
Yu Liudd9ccb42024-10-07 17:07:44 +0000256 return b.bp.OtherModuleName(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800257}
258func (b *baseModuleContext) OtherModuleDir(m blueprint.Module) string { return b.bp.OtherModuleDir(m) }
259func (b *baseModuleContext) OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) {
260 b.bp.OtherModuleErrorf(m, fmt, args...)
261}
262func (b *baseModuleContext) OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag {
Yu Liudd9ccb42024-10-07 17:07:44 +0000263 return b.bp.OtherModuleDependencyTag(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800264}
265func (b *baseModuleContext) OtherModuleExists(name string) bool { return b.bp.OtherModuleExists(name) }
266func (b *baseModuleContext) OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool {
267 return b.bp.OtherModuleDependencyVariantExists(variations, name)
268}
269func (b *baseModuleContext) OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool {
270 return b.bp.OtherModuleFarDependencyVariantExists(variations, name)
271}
272func (b *baseModuleContext) OtherModuleReverseDependencyVariantExists(name string) bool {
273 return b.bp.OtherModuleReverseDependencyVariantExists(name)
274}
275func (b *baseModuleContext) OtherModuleType(m blueprint.Module) string {
276 return b.bp.OtherModuleType(m)
277}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800278
279func (b *baseModuleContext) otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800280 return b.bp.OtherModuleProvider(m, provider)
281}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800282
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000283func (b *baseModuleContext) OtherModuleIsAutoGenerated(m blueprint.Module) bool {
284 return b.bp.OtherModuleIsAutoGenerated(m)
285}
286
Colin Cross3c0a83d2023-12-12 14:13:26 -0800287func (b *baseModuleContext) provider(provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800288 return b.bp.Provider(provider)
289}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800290
Colin Cross24c1cbe2023-12-21 23:42:56 +0000291func (b *baseModuleContext) setProvider(provider blueprint.AnyProviderKey, value any) {
Colin Cross69452e12023-11-15 11:20:53 -0800292 b.bp.SetProvider(provider, value)
293}
294
295func (b *baseModuleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
296 return b.bp.GetDirectDepWithTag(name, tag)
297}
298
299func (b *baseModuleContext) blueprintBaseModuleContext() blueprint.BaseModuleContext {
300 return b.bp
301}
302
Colin Cross69452e12023-11-15 11:20:53 -0800303func (b *baseModuleContext) AddMissingDependencies(deps []string) {
304 if deps != nil {
305 missingDeps := &b.Module().base().commonProperties.MissingDeps
306 *missingDeps = append(*missingDeps, deps...)
307 *missingDeps = FirstUniqueStrings(*missingDeps)
308 }
309}
310
311func (b *baseModuleContext) checkedMissingDeps() bool {
312 return b.Module().base().commonProperties.CheckedMissingDeps
313}
314
315func (b *baseModuleContext) getMissingDependencies() []string {
316 checked := &b.Module().base().commonProperties.CheckedMissingDeps
317 *checked = true
318 var missingDeps []string
319 missingDeps = append(missingDeps, b.Module().base().commonProperties.MissingDeps...)
320 missingDeps = append(missingDeps, b.bp.EarlyGetMissingDependencies()...)
321 missingDeps = FirstUniqueStrings(missingDeps)
322 return missingDeps
323}
324
325type AllowDisabledModuleDependency interface {
326 blueprint.DependencyTag
327 AllowDisabledModuleDependency(target Module) bool
Yu Liud2a95952024-10-10 00:15:26 +0000328 AllowDisabledModuleDependencyProxy(ctx OtherModuleProviderContext, target ModuleProxy) bool
Colin Cross69452e12023-11-15 11:20:53 -0800329}
330
Jiyong Park8bcf3c62024-03-18 18:37:10 +0900331type AlwaysAllowDisabledModuleDependencyTag struct{}
332
333func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependency(Module) bool {
334 return true
335}
336
Yu Liud2a95952024-10-10 00:15:26 +0000337func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependencyProxy(OtherModuleProviderContext, ModuleProxy) bool {
338 return true
339}
340
Colin Cross648daea2024-09-12 14:35:29 -0700341func (b *baseModuleContext) validateAndroidModule(module blueprint.Module, tag blueprint.DependencyTag, strict bool) Module {
Colin Cross69452e12023-11-15 11:20:53 -0800342 aModule, _ := module.(Module)
343
344 if !strict {
345 return aModule
346 }
347
348 if aModule == nil {
Colin Cross648daea2024-09-12 14:35:29 -0700349 panic(fmt.Errorf("module %q (%#v) not an android module", b.OtherModuleName(module), tag))
Colin Cross69452e12023-11-15 11:20:53 -0800350 }
351
Cole Fausta963b942024-04-11 17:43:00 -0700352 if !aModule.Enabled(b) {
Colin Cross69452e12023-11-15 11:20:53 -0800353 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependency(aModule) {
354 if b.Config().AllowMissingDependencies() {
355 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
356 } else {
357 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
358 }
359 }
360 return nil
361 }
362 return aModule
363}
364
Yu Liud2a95952024-10-10 00:15:26 +0000365func (b *baseModuleContext) validateAndroidModuleProxy(
366 module blueprint.ModuleProxy, tag blueprint.DependencyTag, strict bool) *ModuleProxy {
367 aModule := ModuleProxy{module: module}
368
369 if !strict {
370 return &aModule
371 }
372
373 if !OtherModuleProviderOrDefault(b, module, CommonPropertiesProviderKey).Enabled {
374 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependencyProxy(b, aModule) {
375 if b.Config().AllowMissingDependencies() {
376 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
377 } else {
378 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
379 }
380 }
381 return nil
382 }
383
384 return &aModule
385}
386
Colin Cross69452e12023-11-15 11:20:53 -0800387type dep struct {
388 mod blueprint.Module
389 tag blueprint.DependencyTag
390}
391
392func (b *baseModuleContext) getDirectDepsInternal(name string, tag blueprint.DependencyTag) []dep {
393 var deps []dep
Colin Cross648daea2024-09-12 14:35:29 -0700394 b.VisitDirectDeps(func(module Module) {
395 if module.base().BaseModuleName() == name {
Colin Cross69452e12023-11-15 11:20:53 -0800396 returnedTag := b.bp.OtherModuleDependencyTag(module)
397 if tag == nil || returnedTag == tag {
398 deps = append(deps, dep{module, returnedTag})
399 }
400 }
401 })
402 return deps
403}
404
405func (b *baseModuleContext) getDirectDepInternal(name string, tag blueprint.DependencyTag) (blueprint.Module, blueprint.DependencyTag) {
406 deps := b.getDirectDepsInternal(name, tag)
407 if len(deps) == 1 {
408 return deps[0].mod, deps[0].tag
409 } else if len(deps) >= 2 {
410 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
411 name, b.ModuleName()))
412 } else {
413 return nil, nil
414 }
415}
416
417func (b *baseModuleContext) getDirectDepFirstTag(name string) (blueprint.Module, blueprint.DependencyTag) {
418 foundDeps := b.getDirectDepsInternal(name, nil)
419 deps := map[blueprint.Module]bool{}
420 for _, dep := range foundDeps {
421 deps[dep.mod] = true
422 }
423 if len(deps) == 1 {
424 return foundDeps[0].mod, foundDeps[0].tag
425 } else if len(deps) >= 2 {
426 // this could happen if two dependencies have the same name in different namespaces
427 // TODO(b/186554727): this should not occur if namespaces are handled within
428 // getDirectDepsInternal.
429 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
430 name, b.ModuleName()))
431 } else {
432 return nil, nil
433 }
434}
435
436func (b *baseModuleContext) GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module {
437 var deps []Module
Colin Cross648daea2024-09-12 14:35:29 -0700438 b.VisitDirectDeps(func(module Module) {
439 if b.bp.OtherModuleDependencyTag(module) == tag {
440 deps = append(deps, module)
Colin Cross69452e12023-11-15 11:20:53 -0800441 }
442 })
443 return deps
444}
445
446// GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
447// name, or nil if none exists. If there are multiple dependencies on the same module it returns the
448// first DependencyTag.
449func (b *baseModuleContext) GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) {
450 return b.getDirectDepFirstTag(name)
451}
452
Colin Cross69452e12023-11-15 11:20:53 -0800453func (b *baseModuleContext) VisitDirectDeps(visit func(Module)) {
454 b.bp.VisitDirectDeps(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700455 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800456 visit(aModule)
457 }
458 })
459}
460
Colin Cross648daea2024-09-12 14:35:29 -0700461func (b *baseModuleContext) VisitDirectDepsAllowDisabled(visit func(Module)) {
462 b.bp.VisitDirectDeps(func(module blueprint.Module) {
463 visit(module.(Module))
464 })
465}
466
Yu Liud2a95952024-10-10 00:15:26 +0000467func (b *baseModuleContext) VisitDirectDepsProxyAllowDisabled(visit func(proxy ModuleProxy)) {
Yu Liudd9ccb42024-10-07 17:07:44 +0000468 b.bp.VisitDirectDepsProxy(func(module blueprint.ModuleProxy) {
469 visit(ModuleProxy{
470 module: module,
471 })
472 })
473}
474
Colin Cross69452e12023-11-15 11:20:53 -0800475func (b *baseModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
476 b.bp.VisitDirectDeps(func(module blueprint.Module) {
477 if b.bp.OtherModuleDependencyTag(module) == tag {
Yu Liud2a95952024-10-10 00:15:26 +0000478 if aModule := b.validateAndroidModule(module, tag, b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800479 visit(aModule)
480 }
481 }
482 })
483}
484
Yu Liud2a95952024-10-10 00:15:26 +0000485func (b *baseModuleContext) VisitDirectDepsProxyWithTag(tag blueprint.DependencyTag, visit func(proxy ModuleProxy)) {
486 b.bp.VisitDirectDepsProxy(func(module blueprint.ModuleProxy) {
487 if b.bp.OtherModuleDependencyTag(module) == tag {
488 if aModule := b.validateAndroidModuleProxy(module, tag, b.strictVisitDeps); aModule != nil {
489 visit(*aModule)
490 }
491 }
492 })
493}
494
Colin Cross69452e12023-11-15 11:20:53 -0800495func (b *baseModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
496 b.bp.VisitDirectDepsIf(
497 // pred
498 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700499 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800500 return pred(aModule)
501 } else {
502 return false
503 }
504 },
505 // visit
506 func(module blueprint.Module) {
507 visit(module.(Module))
508 })
509}
510
511func (b *baseModuleContext) VisitDepsDepthFirst(visit func(Module)) {
512 b.bp.VisitDepsDepthFirst(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700513 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800514 visit(aModule)
515 }
516 })
517}
518
519func (b *baseModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
520 b.bp.VisitDepsDepthFirstIf(
521 // pred
522 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700523 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800524 return pred(aModule)
525 } else {
526 return false
527 }
528 },
529 // visit
530 func(module blueprint.Module) {
531 visit(module.(Module))
532 })
533}
534
Colin Cross69452e12023-11-15 11:20:53 -0800535func (b *baseModuleContext) WalkDeps(visit func(Module, Module) bool) {
536 b.walkPath = []Module{b.Module()}
537 b.tagPath = []blueprint.DependencyTag{}
538 b.bp.WalkDeps(func(child, parent blueprint.Module) bool {
539 childAndroidModule, _ := child.(Module)
540 parentAndroidModule, _ := parent.(Module)
541 if childAndroidModule != nil && parentAndroidModule != nil {
542 // record walkPath before visit
543 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
544 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
545 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
546 }
547 b.walkPath = append(b.walkPath, childAndroidModule)
548 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
549 return visit(childAndroidModule, parentAndroidModule)
550 } else {
551 return false
552 }
553 })
554}
555
Yu Liud2a95952024-10-10 00:15:26 +0000556func (b *baseModuleContext) WalkDepsProxy(visit func(ModuleProxy, ModuleProxy) bool) {
Yu Liudd9ccb42024-10-07 17:07:44 +0000557 b.walkPath = []Module{ModuleProxy{blueprint.CreateModuleProxy(b.Module())}}
558 b.tagPath = []blueprint.DependencyTag{}
559 b.bp.WalkDepsProxy(func(child, parent blueprint.ModuleProxy) bool {
560 childAndroidModule := ModuleProxy{child}
561 parentAndroidModule := ModuleProxy{parent}
562 // record walkPath before visit
563 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
564 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
565 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
566 }
567 b.walkPath = append(b.walkPath, childAndroidModule)
568 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
569 return visit(childAndroidModule, parentAndroidModule)
570 })
571}
572
Colin Cross69452e12023-11-15 11:20:53 -0800573func (b *baseModuleContext) GetWalkPath() []Module {
Colin Crossb614cd42024-10-11 12:52:21 -0700574 return slices.Clone(b.walkPath)
Colin Cross69452e12023-11-15 11:20:53 -0800575}
576
577func (b *baseModuleContext) GetTagPath() []blueprint.DependencyTag {
578 return b.tagPath
579}
580
581func (b *baseModuleContext) VisitAllModuleVariants(visit func(Module)) {
582 b.bp.VisitAllModuleVariants(func(module blueprint.Module) {
583 visit(module.(Module))
584 })
585}
586
587func (b *baseModuleContext) PrimaryModule() Module {
588 return b.bp.PrimaryModule().(Module)
589}
590
591func (b *baseModuleContext) FinalModule() Module {
592 return b.bp.FinalModule().(Module)
593}
594
595// IsMetaDependencyTag returns true for cross-cutting metadata dependencies.
596func IsMetaDependencyTag(tag blueprint.DependencyTag) bool {
597 if tag == licenseKindTag {
598 return true
599 } else if tag == licensesTag {
600 return true
Jihoon Kanga3a05462024-04-05 00:36:44 +0000601 } else if tag == AcDepTag {
Colin Cross69452e12023-11-15 11:20:53 -0800602 return true
603 }
604 return false
605}
606
607// A regexp for removing boilerplate from BaseDependencyTag from the string representation of
608// a dependency tag.
609var tagCleaner = regexp.MustCompile(`\QBaseDependencyTag:{}\E(, )?`)
610
611// PrettyPrintTag returns string representation of the tag, but prefers
612// custom String() method if available.
613func PrettyPrintTag(tag blueprint.DependencyTag) string {
614 // Use tag's custom String() method if available.
615 if stringer, ok := tag.(fmt.Stringer); ok {
616 return stringer.String()
617 }
618
619 // Otherwise, get a default string representation of the tag's struct.
620 tagString := fmt.Sprintf("%T: %+v", tag, tag)
621
622 // Remove the boilerplate from BaseDependencyTag as it adds no value.
623 tagString = tagCleaner.ReplaceAllString(tagString, "")
624 return tagString
625}
626
627func (b *baseModuleContext) GetPathString(skipFirst bool) string {
628 sb := strings.Builder{}
629 tagPath := b.GetTagPath()
630 walkPath := b.GetWalkPath()
631 if !skipFirst {
632 sb.WriteString(walkPath[0].String())
633 }
634 for i, m := range walkPath[1:] {
635 sb.WriteString("\n")
636 sb.WriteString(fmt.Sprintf(" via tag %s\n", PrettyPrintTag(tagPath[i])))
637 sb.WriteString(fmt.Sprintf(" -> %s", m.String()))
638 }
639 return sb.String()
640}
Cole Faustbdd8aee2024-03-14 14:33:02 -0700641
Cole Faustfdbf5d42024-04-10 15:01:23 -0700642func (m *baseModuleContext) EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue {
643 return m.Module().ConfigurableEvaluator(m).EvaluateConfiguration(condition, property)
Cole Faustbdd8aee2024-03-14 14:33:02 -0700644}