blob: af262aba439dd952bc9b10dc4bcf3ae2a89727e0 [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"
19 "github.com/google/blueprint"
20 "regexp"
21 "strings"
22)
23
24// BaseModuleContext is the same as blueprint.BaseModuleContext except that Config() returns
25// a Config instead of an interface{}, and some methods have been wrapped to use an android.Module
26// instead of a blueprint.Module, plus some extra methods that return Android-specific information
27// about the current module.
28type BaseModuleContext interface {
Colin Cross1d3d9f12024-01-18 14:30:22 -080029 ArchModuleContext
Colin Cross69452e12023-11-15 11:20:53 -080030 EarlyModuleContext
31
32 blueprintBaseModuleContext() blueprint.BaseModuleContext
33
34 // OtherModuleName returns the name of another Module. See BaseModuleContext.ModuleName for more information.
35 // It is intended for use inside the visit functions of Visit* and WalkDeps.
36 OtherModuleName(m blueprint.Module) string
37
38 // OtherModuleDir returns the directory of another Module. See BaseModuleContext.ModuleDir for more information.
39 // It is intended for use inside the visit functions of Visit* and WalkDeps.
40 OtherModuleDir(m blueprint.Module) string
41
42 // OtherModuleErrorf reports an error on another Module. See BaseModuleContext.ModuleErrorf for more information.
43 // It is intended for use inside the visit functions of Visit* and WalkDeps.
44 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
45
46 // OtherModuleDependencyTag returns the dependency tag used to depend on a module, or nil if there is no dependency
47 // on the module. When called inside a Visit* method with current module being visited, and there are multiple
48 // dependencies on the module being visited, it returns the dependency tag used for the current dependency.
49 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
50
51 // OtherModuleExists returns true if a module with the specified name exists, as determined by the NameInterface
52 // passed to Context.SetNameInterface, or SimpleNameInterface if it was not called.
53 OtherModuleExists(name string) bool
54
55 // OtherModuleDependencyVariantExists returns true if a module with the
56 // specified name and variant exists. The variant must match the given
57 // variations. It must also match all the non-local variations of the current
58 // module. In other words, it checks for the module that AddVariationDependencies
59 // would add a dependency on with the same arguments.
60 OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool
61
62 // OtherModuleFarDependencyVariantExists returns true if a module with the
63 // specified name and variant exists. The variant must match the given
64 // variations, but not the non-local variations of the current module. In
65 // other words, it checks for the module that AddFarVariationDependencies
66 // would add a dependency on with the same arguments.
67 OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool
68
69 // OtherModuleReverseDependencyVariantExists returns true if a module with the
70 // specified name exists with the same variations as the current module. In
71 // other words, it checks for the module that AddReverseDependency would add a
72 // dependency on with the same argument.
73 OtherModuleReverseDependencyVariantExists(name string) bool
74
75 // OtherModuleType returns the type of another Module. See BaseModuleContext.ModuleType for more information.
76 // It is intended for use inside the visit functions of Visit* and WalkDeps.
77 OtherModuleType(m blueprint.Module) string
78
Colin Cross24c1cbe2023-12-21 23:42:56 +000079 // otherModuleProvider returns the value for a provider for the given module. If the value is
80 // not set it returns nil and false. The value returned may be a deep copy of the value originally
81 // passed to SetProvider.
82 //
83 // This method shouldn't be used directly, prefer the type-safe android.OtherModuleProvider instead.
Colin Cross3c0a83d2023-12-12 14:13:26 -080084 otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool)
Colin Cross69452e12023-11-15 11:20:53 -080085
86 // Provider returns the value for a provider for the current module. If the value is
Colin Cross24c1cbe2023-12-21 23:42:56 +000087 // not set it returns nil and false. It panics if called before the appropriate
Colin Cross69452e12023-11-15 11:20:53 -080088 // mutator or GenerateBuildActions pass for the provider. The value returned may be a deep
89 // copy of the value originally passed to SetProvider.
Colin Cross24c1cbe2023-12-21 23:42:56 +000090 //
91 // This method shouldn't be used directly, prefer the type-safe android.ModuleProvider instead.
Colin Cross3c0a83d2023-12-12 14:13:26 -080092 provider(provider blueprint.AnyProviderKey) (any, bool)
Colin Cross69452e12023-11-15 11:20:53 -080093
Colin Cross24c1cbe2023-12-21 23:42:56 +000094 // setProvider sets the value for a provider for the current module. It panics if not called
Colin Cross69452e12023-11-15 11:20:53 -080095 // during the appropriate mutator or GenerateBuildActions pass for the provider, if the value
96 // is not of the appropriate type, or if the value has already been set. The value should not
97 // be modified after being passed to SetProvider.
Colin Cross24c1cbe2023-12-21 23:42:56 +000098 //
99 // This method shouldn't be used directly, prefer the type-safe android.SetProvider instead.
100 setProvider(provider blueprint.AnyProviderKey, value any)
Colin Cross69452e12023-11-15 11:20:53 -0800101
102 GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module
103
104 // GetDirectDepWithTag returns the Module the direct dependency with the specified name, or nil if
105 // none exists. It panics if the dependency does not have the specified tag. It skips any
106 // dependencies that are not an android.Module.
107 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
108
109 // GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
110 // name, or nil if none exists. If there are multiple dependencies on the same module it returns
111 // the first DependencyTag.
112 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
113
Colin Cross69452e12023-11-15 11:20:53 -0800114 // VisitDirectDepsBlueprint calls visit for each direct dependency. If there are multiple
115 // direct dependencies on the same module visit will be called multiple times on that module
116 // and OtherModuleDependencyTag will return a different tag for each.
117 //
118 // The Module passed to the visit function should not be retained outside of the visit
119 // function, it may be invalidated by future mutators.
120 VisitDirectDepsBlueprint(visit func(blueprint.Module))
121
122 // VisitDirectDeps calls visit for each direct dependency. If there are multiple
123 // direct dependencies on the same module visit will be called multiple times on that module
124 // and OtherModuleDependencyTag will return a different tag for each. It raises an error if any of the
125 // dependencies are not an android.Module.
126 //
127 // The Module passed to the visit function should not be retained outside of the visit
128 // function, it may be invalidated by future mutators.
129 VisitDirectDeps(visit func(Module))
130
131 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
132
133 // VisitDirectDepsIf calls pred for each direct dependency, and if pred returns true calls visit. If there are
134 // multiple direct dependencies on the same module pred and visit will be called multiple times on that module and
135 // OtherModuleDependencyTag will return a different tag for each. It skips any
136 // dependencies that are not an android.Module.
137 //
138 // The Module passed to the visit function should not be retained outside of the visit function, it may be
139 // invalidated by future mutators.
140 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
141 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
142 VisitDepsDepthFirst(visit func(Module))
143 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
144 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
145
146 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
147 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
148 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
149 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
150 // any dependencies that are not an android.Module.
151 //
152 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
153 // invalidated by future mutators.
154 WalkDeps(visit func(child, parent Module) bool)
155
156 // WalkDepsBlueprint calls visit for each transitive dependency, traversing the dependency
157 // tree in top down order. visit may be called multiple times for the same (child, parent)
158 // pair if there are multiple direct dependencies between the child and parent with different
159 // tags. OtherModuleDependencyTag will return the tag for the currently visited
160 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down
161 // to child.
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 WalkDepsBlueprint(visit func(blueprint.Module, blueprint.Module) bool)
166
167 // GetWalkPath is supposed to be called in visit function passed in WalkDeps()
168 // and returns a top-down dependency path from a start module to current child module.
169 GetWalkPath() []Module
170
171 // PrimaryModule returns the first variant of the current module. Variants of a module are always visited in
172 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from the
173 // Module returned by PrimaryModule without data races. This can be used to perform singleton actions that are
174 // only done once for all variants of a module.
175 PrimaryModule() Module
176
177 // FinalModule returns the last variant of the current module. Variants of a module are always visited in
178 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from all
179 // variants using VisitAllModuleVariants if the current module == FinalModule(). This can be used to perform
180 // singleton actions that are only done once for all variants of a module.
181 FinalModule() Module
182
183 // VisitAllModuleVariants calls visit for each variant of the current module. Variants of a module are always
184 // visited in order by mutators and GenerateBuildActions, so the data created by the current mutator can be read
185 // from all variants if the current module == FinalModule(). Otherwise, care must be taken to not access any
186 // data modified by the current mutator.
187 VisitAllModuleVariants(visit func(Module))
188
189 // GetTagPath is supposed to be called in visit function passed in WalkDeps()
190 // and returns a top-down dependency tags path from a start module to current child module.
191 // It has one less entry than GetWalkPath() as it contains the dependency tags that
192 // exist between each adjacent pair of modules in the GetWalkPath().
193 // GetTagPath()[i] is the tag between GetWalkPath()[i] and GetWalkPath()[i+1]
194 GetTagPath() []blueprint.DependencyTag
195
196 // GetPathString is supposed to be called in visit function passed in WalkDeps()
197 // and returns a multi-line string showing the modules and dependency tags
198 // among them along the top-down dependency path from a start module to current child module.
199 // skipFirst when set to true, the output doesn't include the start module,
200 // which is already printed when this function is used along with ModuleErrorf().
201 GetPathString(skipFirst bool) string
202
203 AddMissingDependencies(missingDeps []string)
204
205 // getMissingDependencies returns the list of missing dependencies.
206 // Calling this function prevents adding new dependencies.
207 getMissingDependencies() []string
Colin Cross69452e12023-11-15 11:20:53 -0800208}
209
210type baseModuleContext struct {
211 bp blueprint.BaseModuleContext
212 earlyModuleContext
Colin Cross1d3d9f12024-01-18 14:30:22 -0800213 archModuleContext
Colin Cross69452e12023-11-15 11:20:53 -0800214
215 walkPath []Module
216 tagPath []blueprint.DependencyTag
217
218 strictVisitDeps bool // If true, enforce that all dependencies are enabled
219
Colin Cross69452e12023-11-15 11:20:53 -0800220}
221
Colin Cross69452e12023-11-15 11:20:53 -0800222func (b *baseModuleContext) OtherModuleName(m blueprint.Module) string {
223 return b.bp.OtherModuleName(m)
224}
225func (b *baseModuleContext) OtherModuleDir(m blueprint.Module) string { return b.bp.OtherModuleDir(m) }
226func (b *baseModuleContext) OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) {
227 b.bp.OtherModuleErrorf(m, fmt, args...)
228}
229func (b *baseModuleContext) OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag {
230 return b.bp.OtherModuleDependencyTag(m)
231}
232func (b *baseModuleContext) OtherModuleExists(name string) bool { return b.bp.OtherModuleExists(name) }
233func (b *baseModuleContext) OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool {
234 return b.bp.OtherModuleDependencyVariantExists(variations, name)
235}
236func (b *baseModuleContext) OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool {
237 return b.bp.OtherModuleFarDependencyVariantExists(variations, name)
238}
239func (b *baseModuleContext) OtherModuleReverseDependencyVariantExists(name string) bool {
240 return b.bp.OtherModuleReverseDependencyVariantExists(name)
241}
242func (b *baseModuleContext) OtherModuleType(m blueprint.Module) string {
243 return b.bp.OtherModuleType(m)
244}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800245
246func (b *baseModuleContext) otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800247 return b.bp.OtherModuleProvider(m, provider)
248}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800249
Colin Cross3c0a83d2023-12-12 14:13:26 -0800250func (b *baseModuleContext) provider(provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800251 return b.bp.Provider(provider)
252}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800253
Colin Cross24c1cbe2023-12-21 23:42:56 +0000254func (b *baseModuleContext) setProvider(provider blueprint.AnyProviderKey, value any) {
Colin Cross69452e12023-11-15 11:20:53 -0800255 b.bp.SetProvider(provider, value)
256}
257
258func (b *baseModuleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
259 return b.bp.GetDirectDepWithTag(name, tag)
260}
261
262func (b *baseModuleContext) blueprintBaseModuleContext() blueprint.BaseModuleContext {
263 return b.bp
264}
265
Colin Cross69452e12023-11-15 11:20:53 -0800266func (b *baseModuleContext) AddMissingDependencies(deps []string) {
267 if deps != nil {
268 missingDeps := &b.Module().base().commonProperties.MissingDeps
269 *missingDeps = append(*missingDeps, deps...)
270 *missingDeps = FirstUniqueStrings(*missingDeps)
271 }
272}
273
274func (b *baseModuleContext) checkedMissingDeps() bool {
275 return b.Module().base().commonProperties.CheckedMissingDeps
276}
277
278func (b *baseModuleContext) getMissingDependencies() []string {
279 checked := &b.Module().base().commonProperties.CheckedMissingDeps
280 *checked = true
281 var missingDeps []string
282 missingDeps = append(missingDeps, b.Module().base().commonProperties.MissingDeps...)
283 missingDeps = append(missingDeps, b.bp.EarlyGetMissingDependencies()...)
284 missingDeps = FirstUniqueStrings(missingDeps)
285 return missingDeps
286}
287
288type AllowDisabledModuleDependency interface {
289 blueprint.DependencyTag
290 AllowDisabledModuleDependency(target Module) bool
291}
292
293func (b *baseModuleContext) validateAndroidModule(module blueprint.Module, tag blueprint.DependencyTag, strict bool) Module {
294 aModule, _ := module.(Module)
295
296 if !strict {
297 return aModule
298 }
299
300 if aModule == nil {
301 b.ModuleErrorf("module %q (%#v) not an android module", b.OtherModuleName(module), tag)
302 return nil
303 }
304
305 if !aModule.Enabled() {
306 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependency(aModule) {
307 if b.Config().AllowMissingDependencies() {
308 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
309 } else {
310 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
311 }
312 }
313 return nil
314 }
315 return aModule
316}
317
318type dep struct {
319 mod blueprint.Module
320 tag blueprint.DependencyTag
321}
322
323func (b *baseModuleContext) getDirectDepsInternal(name string, tag blueprint.DependencyTag) []dep {
324 var deps []dep
325 b.VisitDirectDepsBlueprint(func(module blueprint.Module) {
326 if aModule, _ := module.(Module); aModule != nil {
327 if aModule.base().BaseModuleName() == name {
328 returnedTag := b.bp.OtherModuleDependencyTag(aModule)
329 if tag == nil || returnedTag == tag {
330 deps = append(deps, dep{aModule, returnedTag})
331 }
332 }
333 } else if b.bp.OtherModuleName(module) == name {
334 returnedTag := b.bp.OtherModuleDependencyTag(module)
335 if tag == nil || returnedTag == tag {
336 deps = append(deps, dep{module, returnedTag})
337 }
338 }
339 })
340 return deps
341}
342
343func (b *baseModuleContext) getDirectDepInternal(name string, tag blueprint.DependencyTag) (blueprint.Module, blueprint.DependencyTag) {
344 deps := b.getDirectDepsInternal(name, tag)
345 if len(deps) == 1 {
346 return deps[0].mod, deps[0].tag
347 } else if len(deps) >= 2 {
348 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
349 name, b.ModuleName()))
350 } else {
351 return nil, nil
352 }
353}
354
355func (b *baseModuleContext) getDirectDepFirstTag(name string) (blueprint.Module, blueprint.DependencyTag) {
356 foundDeps := b.getDirectDepsInternal(name, nil)
357 deps := map[blueprint.Module]bool{}
358 for _, dep := range foundDeps {
359 deps[dep.mod] = true
360 }
361 if len(deps) == 1 {
362 return foundDeps[0].mod, foundDeps[0].tag
363 } else if len(deps) >= 2 {
364 // this could happen if two dependencies have the same name in different namespaces
365 // TODO(b/186554727): this should not occur if namespaces are handled within
366 // getDirectDepsInternal.
367 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
368 name, b.ModuleName()))
369 } else {
370 return nil, nil
371 }
372}
373
374func (b *baseModuleContext) GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module {
375 var deps []Module
376 b.VisitDirectDepsBlueprint(func(module blueprint.Module) {
377 if aModule, _ := module.(Module); aModule != nil {
378 if b.bp.OtherModuleDependencyTag(aModule) == tag {
379 deps = append(deps, aModule)
380 }
381 }
382 })
383 return deps
384}
385
386// GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
387// name, or nil if none exists. If there are multiple dependencies on the same module it returns the
388// first DependencyTag.
389func (b *baseModuleContext) GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) {
390 return b.getDirectDepFirstTag(name)
391}
392
Colin Cross69452e12023-11-15 11:20:53 -0800393func (b *baseModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
394 b.bp.VisitDirectDeps(visit)
395}
396
397func (b *baseModuleContext) VisitDirectDeps(visit func(Module)) {
398 b.bp.VisitDirectDeps(func(module blueprint.Module) {
399 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
400 visit(aModule)
401 }
402 })
403}
404
405func (b *baseModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
406 b.bp.VisitDirectDeps(func(module blueprint.Module) {
407 if b.bp.OtherModuleDependencyTag(module) == tag {
408 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
409 visit(aModule)
410 }
411 }
412 })
413}
414
415func (b *baseModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
416 b.bp.VisitDirectDepsIf(
417 // pred
418 func(module blueprint.Module) bool {
419 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
420 return pred(aModule)
421 } else {
422 return false
423 }
424 },
425 // visit
426 func(module blueprint.Module) {
427 visit(module.(Module))
428 })
429}
430
431func (b *baseModuleContext) VisitDepsDepthFirst(visit func(Module)) {
432 b.bp.VisitDepsDepthFirst(func(module blueprint.Module) {
433 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
434 visit(aModule)
435 }
436 })
437}
438
439func (b *baseModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
440 b.bp.VisitDepsDepthFirstIf(
441 // pred
442 func(module blueprint.Module) bool {
443 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
444 return pred(aModule)
445 } else {
446 return false
447 }
448 },
449 // visit
450 func(module blueprint.Module) {
451 visit(module.(Module))
452 })
453}
454
455func (b *baseModuleContext) WalkDepsBlueprint(visit func(blueprint.Module, blueprint.Module) bool) {
456 b.bp.WalkDeps(visit)
457}
458
459func (b *baseModuleContext) WalkDeps(visit func(Module, Module) bool) {
460 b.walkPath = []Module{b.Module()}
461 b.tagPath = []blueprint.DependencyTag{}
462 b.bp.WalkDeps(func(child, parent blueprint.Module) bool {
463 childAndroidModule, _ := child.(Module)
464 parentAndroidModule, _ := parent.(Module)
465 if childAndroidModule != nil && parentAndroidModule != nil {
466 // record walkPath before visit
467 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
468 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
469 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
470 }
471 b.walkPath = append(b.walkPath, childAndroidModule)
472 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
473 return visit(childAndroidModule, parentAndroidModule)
474 } else {
475 return false
476 }
477 })
478}
479
480func (b *baseModuleContext) GetWalkPath() []Module {
481 return b.walkPath
482}
483
484func (b *baseModuleContext) GetTagPath() []blueprint.DependencyTag {
485 return b.tagPath
486}
487
488func (b *baseModuleContext) VisitAllModuleVariants(visit func(Module)) {
489 b.bp.VisitAllModuleVariants(func(module blueprint.Module) {
490 visit(module.(Module))
491 })
492}
493
494func (b *baseModuleContext) PrimaryModule() Module {
495 return b.bp.PrimaryModule().(Module)
496}
497
498func (b *baseModuleContext) FinalModule() Module {
499 return b.bp.FinalModule().(Module)
500}
501
502// IsMetaDependencyTag returns true for cross-cutting metadata dependencies.
503func IsMetaDependencyTag(tag blueprint.DependencyTag) bool {
504 if tag == licenseKindTag {
505 return true
506 } else if tag == licensesTag {
507 return true
508 } else if tag == acDepTag {
509 return true
510 }
511 return false
512}
513
514// A regexp for removing boilerplate from BaseDependencyTag from the string representation of
515// a dependency tag.
516var tagCleaner = regexp.MustCompile(`\QBaseDependencyTag:{}\E(, )?`)
517
518// PrettyPrintTag returns string representation of the tag, but prefers
519// custom String() method if available.
520func PrettyPrintTag(tag blueprint.DependencyTag) string {
521 // Use tag's custom String() method if available.
522 if stringer, ok := tag.(fmt.Stringer); ok {
523 return stringer.String()
524 }
525
526 // Otherwise, get a default string representation of the tag's struct.
527 tagString := fmt.Sprintf("%T: %+v", tag, tag)
528
529 // Remove the boilerplate from BaseDependencyTag as it adds no value.
530 tagString = tagCleaner.ReplaceAllString(tagString, "")
531 return tagString
532}
533
534func (b *baseModuleContext) GetPathString(skipFirst bool) string {
535 sb := strings.Builder{}
536 tagPath := b.GetTagPath()
537 walkPath := b.GetWalkPath()
538 if !skipFirst {
539 sb.WriteString(walkPath[0].String())
540 }
541 for i, m := range walkPath[1:] {
542 sb.WriteString("\n")
543 sb.WriteString(fmt.Sprintf(" via tag %s\n", PrettyPrintTag(tagPath[i])))
544 sb.WriteString(fmt.Sprintf(" -> %s", m.String()))
545 }
546 return sb.String()
547}