blob: dd38a4ec83aa805797204eaa435ee4f8e95a1f38 [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"
23 "github.com/google/blueprint/parser"
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 Cross69452e12023-11-15 11:20:53 -0800116 // VisitDirectDepsBlueprint 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.
119 //
120 // The Module passed to the visit function should not be retained outside of the visit
121 // function, it may be invalidated by future mutators.
122 VisitDirectDepsBlueprint(visit func(blueprint.Module))
123
LaMont Jones34314b72024-01-18 18:27:35 +0000124 // VisitDirectDepsIgnoreBlueprint calls visit for each direct dependency. If there are multiple
125 // direct dependencies on the same module visit will be called multiple times on that module
126 // and OtherModuleDependencyTag will return a different tag for each. It silently ignores any
127 // dependencies that are not an android.Module.
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.
131 VisitDirectDepsIgnoreBlueprint(visit func(Module))
132
Colin Cross69452e12023-11-15 11:20:53 -0800133 // VisitDirectDeps calls visit for each direct dependency. If there are multiple
134 // direct dependencies on the same module visit will be called multiple times on that module
135 // and OtherModuleDependencyTag will return a different tag for each. It raises an error if any of the
136 // dependencies are not an android.Module.
137 //
138 // The Module passed to the visit function should not be retained outside of the visit
139 // function, it may be invalidated by future mutators.
140 VisitDirectDeps(visit func(Module))
141
142 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
143
144 // VisitDirectDepsIf calls pred for each direct dependency, and if pred returns true calls visit. If there are
145 // multiple direct dependencies on the same module pred and visit will be called multiple times on that module and
146 // OtherModuleDependencyTag will return a different tag for each. It skips any
147 // dependencies that are not an android.Module.
148 //
149 // The Module passed to the visit function should not be retained outside of the visit function, it may be
150 // invalidated by future mutators.
151 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
152 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
153 VisitDepsDepthFirst(visit func(Module))
154 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
155 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
156
157 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
158 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
159 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
160 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
161 // any dependencies that are not an android.Module.
162 //
163 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
164 // invalidated by future mutators.
165 WalkDeps(visit func(child, parent Module) bool)
166
167 // WalkDepsBlueprint calls visit for each transitive dependency, traversing the dependency
168 // tree in top down order. visit may be called multiple times for the same (child, parent)
169 // pair if there are multiple direct dependencies between the child and parent with different
170 // tags. OtherModuleDependencyTag will return the tag for the currently visited
171 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down
172 // to child.
173 //
174 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
175 // invalidated by future mutators.
176 WalkDepsBlueprint(visit func(blueprint.Module, blueprint.Module) bool)
177
178 // GetWalkPath is supposed to be called in visit function passed in WalkDeps()
179 // and returns a top-down dependency path from a start module to current child module.
180 GetWalkPath() []Module
181
182 // PrimaryModule returns the first variant of the current module. Variants of a module are always visited in
183 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from the
184 // Module returned by PrimaryModule without data races. This can be used to perform singleton actions that are
185 // only done once for all variants of a module.
186 PrimaryModule() Module
187
188 // FinalModule returns the last variant of the current module. Variants of a module are always visited in
189 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from all
190 // variants using VisitAllModuleVariants if the current module == FinalModule(). This can be used to perform
191 // singleton actions that are only done once for all variants of a module.
192 FinalModule() Module
193
194 // VisitAllModuleVariants calls visit for each variant of the current module. Variants of a module are always
195 // visited in order by mutators and GenerateBuildActions, so the data created by the current mutator can be read
196 // from all variants if the current module == FinalModule(). Otherwise, care must be taken to not access any
197 // data modified by the current mutator.
198 VisitAllModuleVariants(visit func(Module))
199
200 // GetTagPath is supposed to be called in visit function passed in WalkDeps()
201 // and returns a top-down dependency tags path from a start module to current child module.
202 // It has one less entry than GetWalkPath() as it contains the dependency tags that
203 // exist between each adjacent pair of modules in the GetWalkPath().
204 // GetTagPath()[i] is the tag between GetWalkPath()[i] and GetWalkPath()[i+1]
205 GetTagPath() []blueprint.DependencyTag
206
207 // GetPathString is supposed to be called in visit function passed in WalkDeps()
208 // and returns a multi-line string showing the modules and dependency tags
209 // among them along the top-down dependency path from a start module to current child module.
210 // skipFirst when set to true, the output doesn't include the start module,
211 // which is already printed when this function is used along with ModuleErrorf().
212 GetPathString(skipFirst bool) string
213
214 AddMissingDependencies(missingDeps []string)
215
216 // getMissingDependencies returns the list of missing dependencies.
217 // Calling this function prevents adding new dependencies.
218 getMissingDependencies() []string
Cole Faustbdd8aee2024-03-14 14:33:02 -0700219
220 // EvaluateConfiguration makes ModuleContext a valid proptools.ConfigurableEvaluator, so this context
221 // can be used to evaluate the final value of Configurable properties.
222 EvaluateConfiguration(parser.SelectType, string) (string, bool)
Colin Cross69452e12023-11-15 11:20:53 -0800223}
224
225type baseModuleContext struct {
226 bp blueprint.BaseModuleContext
227 earlyModuleContext
Colin Cross1d3d9f12024-01-18 14:30:22 -0800228 archModuleContext
Colin Cross69452e12023-11-15 11:20:53 -0800229
230 walkPath []Module
231 tagPath []blueprint.DependencyTag
232
233 strictVisitDeps bool // If true, enforce that all dependencies are enabled
234
Colin Cross69452e12023-11-15 11:20:53 -0800235}
236
Colin Cross69452e12023-11-15 11:20:53 -0800237func (b *baseModuleContext) OtherModuleName(m blueprint.Module) string {
238 return b.bp.OtherModuleName(m)
239}
240func (b *baseModuleContext) OtherModuleDir(m blueprint.Module) string { return b.bp.OtherModuleDir(m) }
241func (b *baseModuleContext) OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) {
242 b.bp.OtherModuleErrorf(m, fmt, args...)
243}
244func (b *baseModuleContext) OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag {
245 return b.bp.OtherModuleDependencyTag(m)
246}
247func (b *baseModuleContext) OtherModuleExists(name string) bool { return b.bp.OtherModuleExists(name) }
248func (b *baseModuleContext) OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool {
249 return b.bp.OtherModuleDependencyVariantExists(variations, name)
250}
251func (b *baseModuleContext) OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool {
252 return b.bp.OtherModuleFarDependencyVariantExists(variations, name)
253}
254func (b *baseModuleContext) OtherModuleReverseDependencyVariantExists(name string) bool {
255 return b.bp.OtherModuleReverseDependencyVariantExists(name)
256}
257func (b *baseModuleContext) OtherModuleType(m blueprint.Module) string {
258 return b.bp.OtherModuleType(m)
259}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800260
261func (b *baseModuleContext) otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800262 return b.bp.OtherModuleProvider(m, provider)
263}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800264
Colin Cross3c0a83d2023-12-12 14:13:26 -0800265func (b *baseModuleContext) provider(provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800266 return b.bp.Provider(provider)
267}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800268
Colin Cross24c1cbe2023-12-21 23:42:56 +0000269func (b *baseModuleContext) setProvider(provider blueprint.AnyProviderKey, value any) {
Colin Cross69452e12023-11-15 11:20:53 -0800270 b.bp.SetProvider(provider, value)
271}
272
273func (b *baseModuleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
274 return b.bp.GetDirectDepWithTag(name, tag)
275}
276
277func (b *baseModuleContext) blueprintBaseModuleContext() blueprint.BaseModuleContext {
278 return b.bp
279}
280
Colin Cross69452e12023-11-15 11:20:53 -0800281func (b *baseModuleContext) AddMissingDependencies(deps []string) {
282 if deps != nil {
283 missingDeps := &b.Module().base().commonProperties.MissingDeps
284 *missingDeps = append(*missingDeps, deps...)
285 *missingDeps = FirstUniqueStrings(*missingDeps)
286 }
287}
288
289func (b *baseModuleContext) checkedMissingDeps() bool {
290 return b.Module().base().commonProperties.CheckedMissingDeps
291}
292
293func (b *baseModuleContext) getMissingDependencies() []string {
294 checked := &b.Module().base().commonProperties.CheckedMissingDeps
295 *checked = true
296 var missingDeps []string
297 missingDeps = append(missingDeps, b.Module().base().commonProperties.MissingDeps...)
298 missingDeps = append(missingDeps, b.bp.EarlyGetMissingDependencies()...)
299 missingDeps = FirstUniqueStrings(missingDeps)
300 return missingDeps
301}
302
303type AllowDisabledModuleDependency interface {
304 blueprint.DependencyTag
305 AllowDisabledModuleDependency(target Module) bool
306}
307
LaMont Jones34314b72024-01-18 18:27:35 +0000308func (b *baseModuleContext) validateAndroidModule(module blueprint.Module, tag blueprint.DependencyTag, strict bool, ignoreBlueprint bool) Module {
Colin Cross69452e12023-11-15 11:20:53 -0800309 aModule, _ := module.(Module)
310
311 if !strict {
312 return aModule
313 }
314
315 if aModule == nil {
LaMont Jones34314b72024-01-18 18:27:35 +0000316 if !ignoreBlueprint {
317 b.ModuleErrorf("module %q (%#v) not an android module", b.OtherModuleName(module), tag)
318 }
Colin Cross69452e12023-11-15 11:20:53 -0800319 return nil
320 }
321
322 if !aModule.Enabled() {
323 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependency(aModule) {
324 if b.Config().AllowMissingDependencies() {
325 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
326 } else {
327 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
328 }
329 }
330 return nil
331 }
332 return aModule
333}
334
335type dep struct {
336 mod blueprint.Module
337 tag blueprint.DependencyTag
338}
339
340func (b *baseModuleContext) getDirectDepsInternal(name string, tag blueprint.DependencyTag) []dep {
341 var deps []dep
342 b.VisitDirectDepsBlueprint(func(module blueprint.Module) {
343 if aModule, _ := module.(Module); aModule != nil {
344 if aModule.base().BaseModuleName() == name {
345 returnedTag := b.bp.OtherModuleDependencyTag(aModule)
346 if tag == nil || returnedTag == tag {
347 deps = append(deps, dep{aModule, returnedTag})
348 }
349 }
350 } else if b.bp.OtherModuleName(module) == name {
351 returnedTag := b.bp.OtherModuleDependencyTag(module)
352 if tag == nil || returnedTag == tag {
353 deps = append(deps, dep{module, returnedTag})
354 }
355 }
356 })
357 return deps
358}
359
360func (b *baseModuleContext) getDirectDepInternal(name string, tag blueprint.DependencyTag) (blueprint.Module, blueprint.DependencyTag) {
361 deps := b.getDirectDepsInternal(name, tag)
362 if len(deps) == 1 {
363 return deps[0].mod, deps[0].tag
364 } else if len(deps) >= 2 {
365 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
366 name, b.ModuleName()))
367 } else {
368 return nil, nil
369 }
370}
371
372func (b *baseModuleContext) getDirectDepFirstTag(name string) (blueprint.Module, blueprint.DependencyTag) {
373 foundDeps := b.getDirectDepsInternal(name, nil)
374 deps := map[blueprint.Module]bool{}
375 for _, dep := range foundDeps {
376 deps[dep.mod] = true
377 }
378 if len(deps) == 1 {
379 return foundDeps[0].mod, foundDeps[0].tag
380 } else if len(deps) >= 2 {
381 // this could happen if two dependencies have the same name in different namespaces
382 // TODO(b/186554727): this should not occur if namespaces are handled within
383 // getDirectDepsInternal.
384 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
385 name, b.ModuleName()))
386 } else {
387 return nil, nil
388 }
389}
390
391func (b *baseModuleContext) GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module {
392 var deps []Module
393 b.VisitDirectDepsBlueprint(func(module blueprint.Module) {
394 if aModule, _ := module.(Module); aModule != nil {
395 if b.bp.OtherModuleDependencyTag(aModule) == tag {
396 deps = append(deps, aModule)
397 }
398 }
399 })
400 return deps
401}
402
403// GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
404// name, or nil if none exists. If there are multiple dependencies on the same module it returns the
405// first DependencyTag.
406func (b *baseModuleContext) GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) {
407 return b.getDirectDepFirstTag(name)
408}
409
Colin Cross69452e12023-11-15 11:20:53 -0800410func (b *baseModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
411 b.bp.VisitDirectDeps(visit)
412}
413
414func (b *baseModuleContext) VisitDirectDeps(visit func(Module)) {
LaMont Jones34314b72024-01-18 18:27:35 +0000415 b.visitDirectDeps(visit, false)
416}
417
418func (b *baseModuleContext) VisitDirectDepsIgnoreBlueprint(visit func(Module)) {
419 b.visitDirectDeps(visit, true)
420}
421
422func (b *baseModuleContext) visitDirectDeps(visit func(Module), ignoreBlueprint bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800423 b.bp.VisitDirectDeps(func(module blueprint.Module) {
LaMont Jones34314b72024-01-18 18:27:35 +0000424 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps, ignoreBlueprint); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800425 visit(aModule)
426 }
427 })
428}
429
430func (b *baseModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
431 b.bp.VisitDirectDeps(func(module blueprint.Module) {
432 if b.bp.OtherModuleDependencyTag(module) == tag {
LaMont Jones34314b72024-01-18 18:27:35 +0000433 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps, false); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800434 visit(aModule)
435 }
436 }
437 })
438}
439
440func (b *baseModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
441 b.bp.VisitDirectDepsIf(
442 // pred
443 func(module blueprint.Module) bool {
LaMont Jones34314b72024-01-18 18:27:35 +0000444 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps, false); 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
456func (b *baseModuleContext) VisitDepsDepthFirst(visit func(Module)) {
457 b.bp.VisitDepsDepthFirst(func(module blueprint.Module) {
LaMont Jones34314b72024-01-18 18:27:35 +0000458 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps, false); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800459 visit(aModule)
460 }
461 })
462}
463
464func (b *baseModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
465 b.bp.VisitDepsDepthFirstIf(
466 // pred
467 func(module blueprint.Module) bool {
LaMont Jones34314b72024-01-18 18:27:35 +0000468 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps, false); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800469 return pred(aModule)
470 } else {
471 return false
472 }
473 },
474 // visit
475 func(module blueprint.Module) {
476 visit(module.(Module))
477 })
478}
479
480func (b *baseModuleContext) WalkDepsBlueprint(visit func(blueprint.Module, blueprint.Module) bool) {
481 b.bp.WalkDeps(visit)
482}
483
484func (b *baseModuleContext) WalkDeps(visit func(Module, Module) bool) {
485 b.walkPath = []Module{b.Module()}
486 b.tagPath = []blueprint.DependencyTag{}
487 b.bp.WalkDeps(func(child, parent blueprint.Module) bool {
488 childAndroidModule, _ := child.(Module)
489 parentAndroidModule, _ := parent.(Module)
490 if childAndroidModule != nil && parentAndroidModule != nil {
491 // record walkPath before visit
492 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
493 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
494 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
495 }
496 b.walkPath = append(b.walkPath, childAndroidModule)
497 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
498 return visit(childAndroidModule, parentAndroidModule)
499 } else {
500 return false
501 }
502 })
503}
504
505func (b *baseModuleContext) GetWalkPath() []Module {
506 return b.walkPath
507}
508
509func (b *baseModuleContext) GetTagPath() []blueprint.DependencyTag {
510 return b.tagPath
511}
512
513func (b *baseModuleContext) VisitAllModuleVariants(visit func(Module)) {
514 b.bp.VisitAllModuleVariants(func(module blueprint.Module) {
515 visit(module.(Module))
516 })
517}
518
519func (b *baseModuleContext) PrimaryModule() Module {
520 return b.bp.PrimaryModule().(Module)
521}
522
523func (b *baseModuleContext) FinalModule() Module {
524 return b.bp.FinalModule().(Module)
525}
526
527// IsMetaDependencyTag returns true for cross-cutting metadata dependencies.
528func IsMetaDependencyTag(tag blueprint.DependencyTag) bool {
529 if tag == licenseKindTag {
530 return true
531 } else if tag == licensesTag {
532 return true
533 } else if tag == acDepTag {
534 return true
535 }
536 return false
537}
538
539// A regexp for removing boilerplate from BaseDependencyTag from the string representation of
540// a dependency tag.
541var tagCleaner = regexp.MustCompile(`\QBaseDependencyTag:{}\E(, )?`)
542
543// PrettyPrintTag returns string representation of the tag, but prefers
544// custom String() method if available.
545func PrettyPrintTag(tag blueprint.DependencyTag) string {
546 // Use tag's custom String() method if available.
547 if stringer, ok := tag.(fmt.Stringer); ok {
548 return stringer.String()
549 }
550
551 // Otherwise, get a default string representation of the tag's struct.
552 tagString := fmt.Sprintf("%T: %+v", tag, tag)
553
554 // Remove the boilerplate from BaseDependencyTag as it adds no value.
555 tagString = tagCleaner.ReplaceAllString(tagString, "")
556 return tagString
557}
558
559func (b *baseModuleContext) GetPathString(skipFirst bool) string {
560 sb := strings.Builder{}
561 tagPath := b.GetTagPath()
562 walkPath := b.GetWalkPath()
563 if !skipFirst {
564 sb.WriteString(walkPath[0].String())
565 }
566 for i, m := range walkPath[1:] {
567 sb.WriteString("\n")
568 sb.WriteString(fmt.Sprintf(" via tag %s\n", PrettyPrintTag(tagPath[i])))
569 sb.WriteString(fmt.Sprintf(" -> %s", m.String()))
570 }
571 return sb.String()
572}
Cole Faustbdd8aee2024-03-14 14:33:02 -0700573
574func (m *baseModuleContext) EvaluateConfiguration(ty parser.SelectType, condition string) (string, bool) {
575 switch ty {
576 case parser.SelectTypeReleaseVariable:
577 if v, ok := m.Config().productVariables.BuildFlags[condition]; ok {
578 return v, true
579 }
580 return "", false
581 case parser.SelectTypeProductVariable:
582 // TODO(b/323382414): Might add these on a case-by-case basis
583 m.ModuleErrorf("TODO(b/323382414): Product variables are not yet supported in selects")
584 return "", false
585 case parser.SelectTypeSoongConfigVariable:
586 parts := strings.Split(condition, ":")
587 namespace := parts[0]
588 variable := parts[1]
589 if n, ok := m.Config().productVariables.VendorVars[namespace]; ok {
590 if v, ok := n[variable]; ok {
591 return v, true
592 }
593 }
594 return "", false
595 case parser.SelectTypeVariant:
596 m.ModuleErrorf("TODO(b/323382414): Variants are not yet supported in selects")
597 return "", false
598 default:
599 panic("Should be unreachable")
600 }
601}