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