blob: e24ce9d2b2a9291bf2647399ed4bfbbbb4d2bfcf [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
Yu Liudd9ccb42024-10-07 17:07:44 +000036 EqualModules(m1, m2 Module) bool
37
Colin Cross69452e12023-11-15 11:20:53 -080038 // OtherModuleName returns the name of another Module. See BaseModuleContext.ModuleName for more information.
39 // It is intended for use inside the visit functions of Visit* and WalkDeps.
40 OtherModuleName(m blueprint.Module) string
41
42 // OtherModuleDir returns the directory of another Module. See BaseModuleContext.ModuleDir for more information.
43 // It is intended for use inside the visit functions of Visit* and WalkDeps.
44 OtherModuleDir(m blueprint.Module) string
45
46 // OtherModuleErrorf reports an error on another Module. See BaseModuleContext.ModuleErrorf for more information.
47 // It is intended for use inside the visit functions of Visit* and WalkDeps.
48 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
49
50 // OtherModuleDependencyTag returns the dependency tag used to depend on a module, or nil if there is no dependency
51 // on the module. When called inside a Visit* method with current module being visited, and there are multiple
52 // dependencies on the module being visited, it returns the dependency tag used for the current dependency.
53 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
54
55 // OtherModuleExists returns true if a module with the specified name exists, as determined by the NameInterface
56 // passed to Context.SetNameInterface, or SimpleNameInterface if it was not called.
57 OtherModuleExists(name string) bool
58
59 // OtherModuleDependencyVariantExists returns true if a module with the
60 // specified name and variant exists. The variant must match the given
61 // variations. It must also match all the non-local variations of the current
62 // module. In other words, it checks for the module that AddVariationDependencies
63 // would add a dependency on with the same arguments.
64 OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool
65
66 // OtherModuleFarDependencyVariantExists returns true if a module with the
67 // specified name and variant exists. The variant must match the given
68 // variations, but not the non-local variations of the current module. In
69 // other words, it checks for the module that AddFarVariationDependencies
70 // would add a dependency on with the same arguments.
71 OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool
72
73 // OtherModuleReverseDependencyVariantExists returns true if a module with the
74 // specified name exists with the same variations as the current module. In
75 // other words, it checks for the module that AddReverseDependency would add a
76 // dependency on with the same argument.
77 OtherModuleReverseDependencyVariantExists(name string) bool
78
79 // OtherModuleType returns the type of another Module. See BaseModuleContext.ModuleType for more information.
80 // It is intended for use inside the visit functions of Visit* and WalkDeps.
81 OtherModuleType(m blueprint.Module) string
82
Colin Cross24c1cbe2023-12-21 23:42:56 +000083 // otherModuleProvider returns the value for a provider for the given module. If the value is
84 // not set it returns nil and false. The value returned may be a deep copy of the value originally
85 // passed to SetProvider.
86 //
87 // This method shouldn't be used directly, prefer the type-safe android.OtherModuleProvider instead.
Colin Cross3c0a83d2023-12-12 14:13:26 -080088 otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool)
Colin Cross69452e12023-11-15 11:20:53 -080089
90 // Provider returns the value for a provider for the current module. If the value is
Colin Cross24c1cbe2023-12-21 23:42:56 +000091 // not set it returns nil and false. It panics if called before the appropriate
Colin Cross69452e12023-11-15 11:20:53 -080092 // mutator or GenerateBuildActions pass for the provider. The value returned may be a deep
93 // copy of the value originally passed to SetProvider.
Colin Cross24c1cbe2023-12-21 23:42:56 +000094 //
95 // This method shouldn't be used directly, prefer the type-safe android.ModuleProvider instead.
Colin Cross3c0a83d2023-12-12 14:13:26 -080096 provider(provider blueprint.AnyProviderKey) (any, bool)
Colin Cross69452e12023-11-15 11:20:53 -080097
Colin Cross24c1cbe2023-12-21 23:42:56 +000098 // setProvider sets the value for a provider for the current module. It panics if not called
Colin Cross69452e12023-11-15 11:20:53 -080099 // during the appropriate mutator or GenerateBuildActions pass for the provider, if the value
100 // is not of the appropriate type, or if the value has already been set. The value should not
101 // be modified after being passed to SetProvider.
Colin Cross24c1cbe2023-12-21 23:42:56 +0000102 //
103 // This method shouldn't be used directly, prefer the type-safe android.SetProvider instead.
104 setProvider(provider blueprint.AnyProviderKey, value any)
Colin Cross69452e12023-11-15 11:20:53 -0800105
106 GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module
107
108 // GetDirectDepWithTag returns the Module the direct dependency with the specified name, or nil if
109 // none exists. It panics if the dependency does not have the specified tag. It skips any
110 // dependencies that are not an android.Module.
111 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
112
LaMont Jones34314b72024-01-18 18:27:35 +0000113 // GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
Colin Cross69452e12023-11-15 11:20:53 -0800114 // name, or nil if none exists. If there are multiple dependencies on the same module it returns
115 // the first DependencyTag.
116 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
117
Colin Cross648daea2024-09-12 14:35:29 -0700118 // VisitDirectDeps calls visit for each direct dependency. If there are multiple
119 // direct dependencies on the same module visit will be called multiple times on that module
120 // and OtherModuleDependencyTag will return a different tag for each. It raises an error if any of the
121 // dependencies are disabled.
122 //
123 // The Module passed to the visit function should not be retained outside of the visit
124 // function, it may be invalidated by future mutators.
125 VisitDirectDeps(visit func(Module))
126
127 // VisitDirectDeps calls visit for each direct dependency. If there are multiple
Colin Cross69452e12023-11-15 11:20:53 -0800128 // direct dependencies on the same module visit will be called multiple times on that module
129 // and OtherModuleDependencyTag will return a different tag for each.
130 //
131 // The Module passed to the visit function should not be retained outside of the visit
132 // function, it may be invalidated by future mutators.
Colin Cross648daea2024-09-12 14:35:29 -0700133 VisitDirectDepsAllowDisabled(visit func(Module))
Colin Cross69452e12023-11-15 11:20:53 -0800134
Yu Liudd9ccb42024-10-07 17:07:44 +0000135 // VisitDirectDepsProxyAllowDisabled calls visit for each direct dependency. If there are
136 // multiple direct dependencies on the same module visit will be called multiple times on
137 // that module and OtherModuleDependencyTag will return a different tag for each.
138 //
Yu Liud2a95952024-10-10 00:15:26 +0000139 // 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 +0000140 // invalidated by future mutators.
Yu Liud2a95952024-10-10 00:15:26 +0000141 VisitDirectDepsProxyAllowDisabled(visit func(proxy ModuleProxy))
Yu Liudd9ccb42024-10-07 17:07:44 +0000142
Colin Cross69452e12023-11-15 11:20:53 -0800143 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
144
Yu Liud2a95952024-10-10 00:15:26 +0000145 VisitDirectDepsProxyWithTag(tag blueprint.DependencyTag, visit func(proxy ModuleProxy))
146
Colin Cross69452e12023-11-15 11:20:53 -0800147 // VisitDirectDepsIf calls pred for each direct dependency, and if pred returns true calls visit. If there are
148 // multiple direct dependencies on the same module pred and visit will be called multiple times on that module and
149 // OtherModuleDependencyTag will return a different tag for each. It skips any
150 // dependencies that are not an android.Module.
151 //
152 // The Module passed to the visit function should not be retained outside of the visit function, it may be
153 // invalidated by future mutators.
154 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
155 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
156 VisitDepsDepthFirst(visit func(Module))
157 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
158 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
159
160 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
161 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
162 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
163 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
164 // any dependencies that are not an android.Module.
165 //
166 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
167 // invalidated by future mutators.
168 WalkDeps(visit func(child, parent Module) bool)
169
Yu Liudd9ccb42024-10-07 17:07:44 +0000170 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
171 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
172 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
173 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
174 // any dependencies that are not an android.Module.
175 //
176 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
177 // invalidated by future mutators.
Yu Liud2a95952024-10-10 00:15:26 +0000178 WalkDepsProxy(visit func(child, parent ModuleProxy) bool)
Yu Liudd9ccb42024-10-07 17:07:44 +0000179
Colin Cross69452e12023-11-15 11:20:53 -0800180 // GetWalkPath is supposed to be called in visit function passed in WalkDeps()
181 // and returns a top-down dependency path from a start module to current child module.
182 GetWalkPath() []Module
183
184 // PrimaryModule returns the first variant of the current module. Variants of a module are always visited in
185 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from the
186 // Module returned by PrimaryModule without data races. This can be used to perform singleton actions that are
187 // only done once for all variants of a module.
188 PrimaryModule() Module
189
190 // FinalModule returns the last variant of the current module. Variants of a module are always visited in
191 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from all
192 // variants using VisitAllModuleVariants if the current module == FinalModule(). This can be used to perform
193 // singleton actions that are only done once for all variants of a module.
194 FinalModule() Module
195
196 // VisitAllModuleVariants calls visit for each variant of the current module. Variants of a module are always
197 // visited in order by mutators and GenerateBuildActions, so the data created by the current mutator can be read
198 // from all variants if the current module == FinalModule(). Otherwise, care must be taken to not access any
199 // data modified by the current mutator.
200 VisitAllModuleVariants(visit func(Module))
201
202 // GetTagPath is supposed to be called in visit function passed in WalkDeps()
203 // and returns a top-down dependency tags path from a start module to current child module.
204 // It has one less entry than GetWalkPath() as it contains the dependency tags that
205 // exist between each adjacent pair of modules in the GetWalkPath().
206 // GetTagPath()[i] is the tag between GetWalkPath()[i] and GetWalkPath()[i+1]
207 GetTagPath() []blueprint.DependencyTag
208
209 // GetPathString is supposed to be called in visit function passed in WalkDeps()
210 // and returns a multi-line string showing the modules and dependency tags
211 // among them along the top-down dependency path from a start module to current child module.
212 // skipFirst when set to true, the output doesn't include the start module,
213 // which is already printed when this function is used along with ModuleErrorf().
214 GetPathString(skipFirst bool) string
215
216 AddMissingDependencies(missingDeps []string)
217
218 // getMissingDependencies returns the list of missing dependencies.
219 // Calling this function prevents adding new dependencies.
220 getMissingDependencies() []string
Cole Faustbdd8aee2024-03-14 14:33:02 -0700221
222 // EvaluateConfiguration makes ModuleContext a valid proptools.ConfigurableEvaluator, so this context
223 // can be used to evaluate the final value of Configurable properties.
Cole Faustfdbf5d42024-04-10 15:01:23 -0700224 EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue
Colin Cross69452e12023-11-15 11:20:53 -0800225}
226
227type baseModuleContext struct {
228 bp blueprint.BaseModuleContext
229 earlyModuleContext
Colin Cross1d3d9f12024-01-18 14:30:22 -0800230 archModuleContext
Colin Cross69452e12023-11-15 11:20:53 -0800231
232 walkPath []Module
233 tagPath []blueprint.DependencyTag
234
235 strictVisitDeps bool // If true, enforce that all dependencies are enabled
236
Colin Cross69452e12023-11-15 11:20:53 -0800237}
238
Yu Liudd9ccb42024-10-07 17:07:44 +0000239func getWrappedModule(module blueprint.Module) blueprint.Module {
240 if mp, isProxy := module.(ModuleProxy); isProxy {
241 return mp.module
242 }
243 return module
244}
245
246func (b *baseModuleContext) EqualModules(m1, m2 Module) bool {
247 return b.bp.EqualModules(getWrappedModule(m1), getWrappedModule(m2))
248}
249
Colin Cross69452e12023-11-15 11:20:53 -0800250func (b *baseModuleContext) OtherModuleName(m blueprint.Module) string {
Yu Liudd9ccb42024-10-07 17:07:44 +0000251 return b.bp.OtherModuleName(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800252}
253func (b *baseModuleContext) OtherModuleDir(m blueprint.Module) string { return b.bp.OtherModuleDir(m) }
254func (b *baseModuleContext) OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) {
255 b.bp.OtherModuleErrorf(m, fmt, args...)
256}
257func (b *baseModuleContext) OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag {
Yu Liudd9ccb42024-10-07 17:07:44 +0000258 return b.bp.OtherModuleDependencyTag(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800259}
260func (b *baseModuleContext) OtherModuleExists(name string) bool { return b.bp.OtherModuleExists(name) }
261func (b *baseModuleContext) OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool {
262 return b.bp.OtherModuleDependencyVariantExists(variations, name)
263}
264func (b *baseModuleContext) OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool {
265 return b.bp.OtherModuleFarDependencyVariantExists(variations, name)
266}
267func (b *baseModuleContext) OtherModuleReverseDependencyVariantExists(name string) bool {
268 return b.bp.OtherModuleReverseDependencyVariantExists(name)
269}
270func (b *baseModuleContext) OtherModuleType(m blueprint.Module) string {
271 return b.bp.OtherModuleType(m)
272}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800273
274func (b *baseModuleContext) otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800275 return b.bp.OtherModuleProvider(m, provider)
276}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800277
Colin Cross3c0a83d2023-12-12 14:13:26 -0800278func (b *baseModuleContext) provider(provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800279 return b.bp.Provider(provider)
280}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800281
Colin Cross24c1cbe2023-12-21 23:42:56 +0000282func (b *baseModuleContext) setProvider(provider blueprint.AnyProviderKey, value any) {
Colin Cross69452e12023-11-15 11:20:53 -0800283 b.bp.SetProvider(provider, value)
284}
285
286func (b *baseModuleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
287 return b.bp.GetDirectDepWithTag(name, tag)
288}
289
290func (b *baseModuleContext) blueprintBaseModuleContext() blueprint.BaseModuleContext {
291 return b.bp
292}
293
Colin Cross69452e12023-11-15 11:20:53 -0800294func (b *baseModuleContext) AddMissingDependencies(deps []string) {
295 if deps != nil {
296 missingDeps := &b.Module().base().commonProperties.MissingDeps
297 *missingDeps = append(*missingDeps, deps...)
298 *missingDeps = FirstUniqueStrings(*missingDeps)
299 }
300}
301
302func (b *baseModuleContext) checkedMissingDeps() bool {
303 return b.Module().base().commonProperties.CheckedMissingDeps
304}
305
306func (b *baseModuleContext) getMissingDependencies() []string {
307 checked := &b.Module().base().commonProperties.CheckedMissingDeps
308 *checked = true
309 var missingDeps []string
310 missingDeps = append(missingDeps, b.Module().base().commonProperties.MissingDeps...)
311 missingDeps = append(missingDeps, b.bp.EarlyGetMissingDependencies()...)
312 missingDeps = FirstUniqueStrings(missingDeps)
313 return missingDeps
314}
315
316type AllowDisabledModuleDependency interface {
317 blueprint.DependencyTag
318 AllowDisabledModuleDependency(target Module) bool
Yu Liud2a95952024-10-10 00:15:26 +0000319 AllowDisabledModuleDependencyProxy(ctx OtherModuleProviderContext, target ModuleProxy) bool
Colin Cross69452e12023-11-15 11:20:53 -0800320}
321
Jiyong Park8bcf3c62024-03-18 18:37:10 +0900322type AlwaysAllowDisabledModuleDependencyTag struct{}
323
324func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependency(Module) bool {
325 return true
326}
327
Yu Liud2a95952024-10-10 00:15:26 +0000328func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependencyProxy(OtherModuleProviderContext, ModuleProxy) bool {
329 return true
330}
331
Colin Cross648daea2024-09-12 14:35:29 -0700332func (b *baseModuleContext) validateAndroidModule(module blueprint.Module, tag blueprint.DependencyTag, strict bool) Module {
Colin Cross69452e12023-11-15 11:20:53 -0800333 aModule, _ := module.(Module)
334
335 if !strict {
336 return aModule
337 }
338
339 if aModule == nil {
Colin Cross648daea2024-09-12 14:35:29 -0700340 panic(fmt.Errorf("module %q (%#v) not an android module", b.OtherModuleName(module), tag))
Colin Cross69452e12023-11-15 11:20:53 -0800341 }
342
Cole Fausta963b942024-04-11 17:43:00 -0700343 if !aModule.Enabled(b) {
Colin Cross69452e12023-11-15 11:20:53 -0800344 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependency(aModule) {
345 if b.Config().AllowMissingDependencies() {
346 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
347 } else {
348 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
349 }
350 }
351 return nil
352 }
353 return aModule
354}
355
Yu Liud2a95952024-10-10 00:15:26 +0000356func (b *baseModuleContext) validateAndroidModuleProxy(
357 module blueprint.ModuleProxy, tag blueprint.DependencyTag, strict bool) *ModuleProxy {
358 aModule := ModuleProxy{module: module}
359
360 if !strict {
361 return &aModule
362 }
363
364 if !OtherModuleProviderOrDefault(b, module, CommonPropertiesProviderKey).Enabled {
365 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependencyProxy(b, aModule) {
366 if b.Config().AllowMissingDependencies() {
367 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
368 } else {
369 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
370 }
371 }
372 return nil
373 }
374
375 return &aModule
376}
377
Colin Cross69452e12023-11-15 11:20:53 -0800378type dep struct {
379 mod blueprint.Module
380 tag blueprint.DependencyTag
381}
382
383func (b *baseModuleContext) getDirectDepsInternal(name string, tag blueprint.DependencyTag) []dep {
384 var deps []dep
Colin Cross648daea2024-09-12 14:35:29 -0700385 b.VisitDirectDeps(func(module Module) {
386 if module.base().BaseModuleName() == name {
Colin Cross69452e12023-11-15 11:20:53 -0800387 returnedTag := b.bp.OtherModuleDependencyTag(module)
388 if tag == nil || returnedTag == tag {
389 deps = append(deps, dep{module, returnedTag})
390 }
391 }
392 })
393 return deps
394}
395
396func (b *baseModuleContext) getDirectDepInternal(name string, tag blueprint.DependencyTag) (blueprint.Module, blueprint.DependencyTag) {
397 deps := b.getDirectDepsInternal(name, tag)
398 if len(deps) == 1 {
399 return deps[0].mod, deps[0].tag
400 } else if len(deps) >= 2 {
401 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
402 name, b.ModuleName()))
403 } else {
404 return nil, nil
405 }
406}
407
408func (b *baseModuleContext) getDirectDepFirstTag(name string) (blueprint.Module, blueprint.DependencyTag) {
409 foundDeps := b.getDirectDepsInternal(name, nil)
410 deps := map[blueprint.Module]bool{}
411 for _, dep := range foundDeps {
412 deps[dep.mod] = true
413 }
414 if len(deps) == 1 {
415 return foundDeps[0].mod, foundDeps[0].tag
416 } else if len(deps) >= 2 {
417 // this could happen if two dependencies have the same name in different namespaces
418 // TODO(b/186554727): this should not occur if namespaces are handled within
419 // getDirectDepsInternal.
420 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
421 name, b.ModuleName()))
422 } else {
423 return nil, nil
424 }
425}
426
427func (b *baseModuleContext) GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module {
428 var deps []Module
Colin Cross648daea2024-09-12 14:35:29 -0700429 b.VisitDirectDeps(func(module Module) {
430 if b.bp.OtherModuleDependencyTag(module) == tag {
431 deps = append(deps, module)
Colin Cross69452e12023-11-15 11:20:53 -0800432 }
433 })
434 return deps
435}
436
437// GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
438// name, or nil if none exists. If there are multiple dependencies on the same module it returns the
439// first DependencyTag.
440func (b *baseModuleContext) GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) {
441 return b.getDirectDepFirstTag(name)
442}
443
Colin Cross69452e12023-11-15 11:20:53 -0800444func (b *baseModuleContext) VisitDirectDeps(visit func(Module)) {
445 b.bp.VisitDirectDeps(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700446 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800447 visit(aModule)
448 }
449 })
450}
451
Colin Cross648daea2024-09-12 14:35:29 -0700452func (b *baseModuleContext) VisitDirectDepsAllowDisabled(visit func(Module)) {
453 b.bp.VisitDirectDeps(func(module blueprint.Module) {
454 visit(module.(Module))
455 })
456}
457
Yu Liud2a95952024-10-10 00:15:26 +0000458func (b *baseModuleContext) VisitDirectDepsProxyAllowDisabled(visit func(proxy ModuleProxy)) {
Yu Liudd9ccb42024-10-07 17:07:44 +0000459 b.bp.VisitDirectDepsProxy(func(module blueprint.ModuleProxy) {
460 visit(ModuleProxy{
461 module: module,
462 })
463 })
464}
465
Colin Cross69452e12023-11-15 11:20:53 -0800466func (b *baseModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
467 b.bp.VisitDirectDeps(func(module blueprint.Module) {
468 if b.bp.OtherModuleDependencyTag(module) == tag {
Yu Liud2a95952024-10-10 00:15:26 +0000469 if aModule := b.validateAndroidModule(module, tag, b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800470 visit(aModule)
471 }
472 }
473 })
474}
475
Yu Liud2a95952024-10-10 00:15:26 +0000476func (b *baseModuleContext) VisitDirectDepsProxyWithTag(tag blueprint.DependencyTag, visit func(proxy ModuleProxy)) {
477 b.bp.VisitDirectDepsProxy(func(module blueprint.ModuleProxy) {
478 if b.bp.OtherModuleDependencyTag(module) == tag {
479 if aModule := b.validateAndroidModuleProxy(module, tag, b.strictVisitDeps); aModule != nil {
480 visit(*aModule)
481 }
482 }
483 })
484}
485
Colin Cross69452e12023-11-15 11:20:53 -0800486func (b *baseModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
487 b.bp.VisitDirectDepsIf(
488 // pred
489 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700490 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800491 return pred(aModule)
492 } else {
493 return false
494 }
495 },
496 // visit
497 func(module blueprint.Module) {
498 visit(module.(Module))
499 })
500}
501
502func (b *baseModuleContext) VisitDepsDepthFirst(visit func(Module)) {
503 b.bp.VisitDepsDepthFirst(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700504 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800505 visit(aModule)
506 }
507 })
508}
509
510func (b *baseModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
511 b.bp.VisitDepsDepthFirstIf(
512 // pred
513 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700514 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800515 return pred(aModule)
516 } else {
517 return false
518 }
519 },
520 // visit
521 func(module blueprint.Module) {
522 visit(module.(Module))
523 })
524}
525
Colin Cross69452e12023-11-15 11:20:53 -0800526func (b *baseModuleContext) WalkDeps(visit func(Module, Module) bool) {
527 b.walkPath = []Module{b.Module()}
528 b.tagPath = []blueprint.DependencyTag{}
529 b.bp.WalkDeps(func(child, parent blueprint.Module) bool {
530 childAndroidModule, _ := child.(Module)
531 parentAndroidModule, _ := parent.(Module)
532 if childAndroidModule != nil && parentAndroidModule != nil {
533 // record walkPath before visit
534 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
535 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
536 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
537 }
538 b.walkPath = append(b.walkPath, childAndroidModule)
539 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
540 return visit(childAndroidModule, parentAndroidModule)
541 } else {
542 return false
543 }
544 })
545}
546
Yu Liud2a95952024-10-10 00:15:26 +0000547func (b *baseModuleContext) WalkDepsProxy(visit func(ModuleProxy, ModuleProxy) bool) {
Yu Liudd9ccb42024-10-07 17:07:44 +0000548 b.walkPath = []Module{ModuleProxy{blueprint.CreateModuleProxy(b.Module())}}
549 b.tagPath = []blueprint.DependencyTag{}
550 b.bp.WalkDepsProxy(func(child, parent blueprint.ModuleProxy) bool {
551 childAndroidModule := ModuleProxy{child}
552 parentAndroidModule := ModuleProxy{parent}
553 // record walkPath before visit
554 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
555 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
556 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
557 }
558 b.walkPath = append(b.walkPath, childAndroidModule)
559 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
560 return visit(childAndroidModule, parentAndroidModule)
561 })
562}
563
Colin Cross69452e12023-11-15 11:20:53 -0800564func (b *baseModuleContext) GetWalkPath() []Module {
565 return b.walkPath
566}
567
568func (b *baseModuleContext) GetTagPath() []blueprint.DependencyTag {
569 return b.tagPath
570}
571
572func (b *baseModuleContext) VisitAllModuleVariants(visit func(Module)) {
573 b.bp.VisitAllModuleVariants(func(module blueprint.Module) {
574 visit(module.(Module))
575 })
576}
577
578func (b *baseModuleContext) PrimaryModule() Module {
579 return b.bp.PrimaryModule().(Module)
580}
581
582func (b *baseModuleContext) FinalModule() Module {
583 return b.bp.FinalModule().(Module)
584}
585
586// IsMetaDependencyTag returns true for cross-cutting metadata dependencies.
587func IsMetaDependencyTag(tag blueprint.DependencyTag) bool {
588 if tag == licenseKindTag {
589 return true
590 } else if tag == licensesTag {
591 return true
Jihoon Kanga3a05462024-04-05 00:36:44 +0000592 } else if tag == AcDepTag {
Colin Cross69452e12023-11-15 11:20:53 -0800593 return true
594 }
595 return false
596}
597
598// A regexp for removing boilerplate from BaseDependencyTag from the string representation of
599// a dependency tag.
600var tagCleaner = regexp.MustCompile(`\QBaseDependencyTag:{}\E(, )?`)
601
602// PrettyPrintTag returns string representation of the tag, but prefers
603// custom String() method if available.
604func PrettyPrintTag(tag blueprint.DependencyTag) string {
605 // Use tag's custom String() method if available.
606 if stringer, ok := tag.(fmt.Stringer); ok {
607 return stringer.String()
608 }
609
610 // Otherwise, get a default string representation of the tag's struct.
611 tagString := fmt.Sprintf("%T: %+v", tag, tag)
612
613 // Remove the boilerplate from BaseDependencyTag as it adds no value.
614 tagString = tagCleaner.ReplaceAllString(tagString, "")
615 return tagString
616}
617
618func (b *baseModuleContext) GetPathString(skipFirst bool) string {
619 sb := strings.Builder{}
620 tagPath := b.GetTagPath()
621 walkPath := b.GetWalkPath()
622 if !skipFirst {
623 sb.WriteString(walkPath[0].String())
624 }
625 for i, m := range walkPath[1:] {
626 sb.WriteString("\n")
627 sb.WriteString(fmt.Sprintf(" via tag %s\n", PrettyPrintTag(tagPath[i])))
628 sb.WriteString(fmt.Sprintf(" -> %s", m.String()))
629 }
630 return sb.String()
631}
Cole Faustbdd8aee2024-03-14 14:33:02 -0700632
Cole Faustfdbf5d42024-04-10 15:01:23 -0700633func (m *baseModuleContext) EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue {
634 return m.Module().ConfigurableEvaluator(m).EvaluateConfiguration(condition, property)
Cole Faustbdd8aee2024-03-14 14:33:02 -0700635}