blob: 839afaa66b491e065167b85062876f58baa28112 [file] [log] [blame]
Colin Cross69452e12023-11-15 11:20:53 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
17import (
18 "fmt"
Colin Cross69452e12023-11-15 11:20:53 -080019 "regexp"
Colin Crossb614cd42024-10-11 12:52:21 -070020 "slices"
Colin Cross69452e12023-11-15 11:20:53 -080021 "strings"
Cole Faustbdd8aee2024-03-14 14:33:02 -070022
23 "github.com/google/blueprint"
Cole Faustfdbf5d42024-04-10 15:01:23 -070024 "github.com/google/blueprint/proptools"
Colin Cross69452e12023-11-15 11:20:53 -080025)
26
27// BaseModuleContext is the same as blueprint.BaseModuleContext except that Config() returns
28// a Config instead of an interface{}, and some methods have been wrapped to use an android.Module
29// instead of a blueprint.Module, plus some extra methods that return Android-specific information
30// about the current module.
31type BaseModuleContext interface {
Colin Cross1d3d9f12024-01-18 14:30:22 -080032 ArchModuleContext
Colin Cross69452e12023-11-15 11:20:53 -080033 EarlyModuleContext
34
35 blueprintBaseModuleContext() blueprint.BaseModuleContext
36
Yu Liudd9ccb42024-10-07 17:07:44 +000037 EqualModules(m1, m2 Module) bool
38
Colin Cross69452e12023-11-15 11:20:53 -080039 // OtherModuleName returns the name of another Module. See BaseModuleContext.ModuleName for more information.
40 // It is intended for use inside the visit functions of Visit* and WalkDeps.
41 OtherModuleName(m blueprint.Module) string
42
43 // OtherModuleDir returns the directory of another Module. See BaseModuleContext.ModuleDir for more information.
44 // It is intended for use inside the visit functions of Visit* and WalkDeps.
45 OtherModuleDir(m blueprint.Module) string
46
47 // OtherModuleErrorf reports an error on another Module. See BaseModuleContext.ModuleErrorf for more information.
48 // It is intended for use inside the visit functions of Visit* and WalkDeps.
49 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
50
51 // OtherModuleDependencyTag returns the dependency tag used to depend on a module, or nil if there is no dependency
52 // on the module. When called inside a Visit* method with current module being visited, and there are multiple
53 // dependencies on the module being visited, it returns the dependency tag used for the current dependency.
54 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
55
56 // OtherModuleExists returns true if a module with the specified name exists, as determined by the NameInterface
57 // passed to Context.SetNameInterface, or SimpleNameInterface if it was not called.
58 OtherModuleExists(name string) bool
59
60 // OtherModuleDependencyVariantExists returns true if a module with the
61 // specified name and variant exists. The variant must match the given
62 // variations. It must also match all the non-local variations of the current
63 // module. In other words, it checks for the module that AddVariationDependencies
64 // would add a dependency on with the same arguments.
65 OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool
66
67 // OtherModuleFarDependencyVariantExists returns true if a module with the
68 // specified name and variant exists. The variant must match the given
69 // variations, but not the non-local variations of the current module. In
70 // other words, it checks for the module that AddFarVariationDependencies
71 // would add a dependency on with the same arguments.
72 OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool
73
74 // OtherModuleReverseDependencyVariantExists returns true if a module with the
75 // specified name exists with the same variations as the current module. In
76 // other words, it checks for the module that AddReverseDependency would add a
77 // dependency on with the same argument.
78 OtherModuleReverseDependencyVariantExists(name string) bool
79
80 // OtherModuleType returns the type of another Module. See BaseModuleContext.ModuleType for more information.
81 // It is intended for use inside the visit functions of Visit* and WalkDeps.
82 OtherModuleType(m blueprint.Module) string
83
Colin Cross24c1cbe2023-12-21 23:42:56 +000084 // otherModuleProvider returns the value for a provider for the given module. If the value is
85 // not set it returns nil and false. The value returned may be a deep copy of the value originally
86 // passed to SetProvider.
87 //
88 // This method shouldn't be used directly, prefer the type-safe android.OtherModuleProvider instead.
Colin Cross3c0a83d2023-12-12 14:13:26 -080089 otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool)
Colin Cross69452e12023-11-15 11:20:53 -080090
Jihoon Kang2a7bf752024-11-01 21:21:25 +000091 // OtherModuleIsAutoGenerated returns true if the module is auto generated by another module
92 // instead of being defined in Android.bp file.
93 OtherModuleIsAutoGenerated(m blueprint.Module) bool
94
Colin Cross69452e12023-11-15 11:20:53 -080095 // Provider returns the value for a provider for the current module. If the value is
Colin Cross24c1cbe2023-12-21 23:42:56 +000096 // not set it returns nil and false. It panics if called before the appropriate
Colin Cross69452e12023-11-15 11:20:53 -080097 // mutator or GenerateBuildActions pass for the provider. The value returned may be a deep
98 // copy of the value originally passed to SetProvider.
Colin Cross24c1cbe2023-12-21 23:42:56 +000099 //
100 // This method shouldn't be used directly, prefer the type-safe android.ModuleProvider instead.
Colin Cross3c0a83d2023-12-12 14:13:26 -0800101 provider(provider blueprint.AnyProviderKey) (any, bool)
Colin Cross69452e12023-11-15 11:20:53 -0800102
Colin Cross24c1cbe2023-12-21 23:42:56 +0000103 // setProvider sets the value for a provider for the current module. It panics if not called
Colin Cross69452e12023-11-15 11:20:53 -0800104 // during the appropriate mutator or GenerateBuildActions pass for the provider, if the value
105 // is not of the appropriate type, or if the value has already been set. The value should not
106 // be modified after being passed to SetProvider.
Colin Cross24c1cbe2023-12-21 23:42:56 +0000107 //
108 // This method shouldn't be used directly, prefer the type-safe android.SetProvider instead.
109 setProvider(provider blueprint.AnyProviderKey, value any)
Colin Cross69452e12023-11-15 11:20:53 -0800110
111 GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module
112
Yu Liuf432c2e2024-12-17 00:09:15 +0000113 GetDirectDepsProxyWithTag(tag blueprint.DependencyTag) []ModuleProxy
114
Colin Cross69452e12023-11-15 11:20:53 -0800115 // GetDirectDepWithTag returns the Module the direct dependency with the specified name, or nil if
116 // none exists. It panics if the dependency does not have the specified tag. It skips any
117 // dependencies that are not an android.Module.
Yu Liud3228ac2024-11-08 23:11:47 +0000118 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) Module
Colin Cross69452e12023-11-15 11:20:53 -0800119
Colin Cross648daea2024-09-12 14:35:29 -0700120 // VisitDirectDeps calls visit for each direct dependency. If there are multiple
121 // direct dependencies on the same module visit will be called multiple times on that module
122 // and OtherModuleDependencyTag will return a different tag for each. It raises an error if any of the
123 // dependencies are disabled.
124 //
125 // The Module passed to the visit function should not be retained outside of the visit
126 // function, it may be invalidated by future mutators.
127 VisitDirectDeps(visit func(Module))
128
Yu Liud3228ac2024-11-08 23:11:47 +0000129 // VisitDirectDepsProxy calls visit for each direct dependency. If there are multiple
Colin Cross69452e12023-11-15 11:20:53 -0800130 // direct dependencies on the same module visit will be called multiple times on that module
Yu Liud3228ac2024-11-08 23:11:47 +0000131 // and OtherModuleDependencyTag will return a different tag for each. It raises an error if any of the
132 // dependencies are disabled.
Colin Cross69452e12023-11-15 11:20:53 -0800133 //
Yu Liud3228ac2024-11-08 23:11:47 +0000134 // The ModuleProxy passed to the visit function should not be retained outside of the visit
Colin Cross69452e12023-11-15 11:20:53 -0800135 // function, it may be invalidated by future mutators.
Yu Liud3228ac2024-11-08 23:11:47 +0000136 VisitDirectDepsProxy(visit func(proxy ModuleProxy))
Colin Cross69452e12023-11-15 11:20:53 -0800137
Yu Liudd9ccb42024-10-07 17:07:44 +0000138 // VisitDirectDepsProxyAllowDisabled calls visit for each direct dependency. If there are
139 // multiple direct dependencies on the same module visit will be called multiple times on
140 // that module and OtherModuleDependencyTag will return a different tag for each.
141 //
Yu Liud2a95952024-10-10 00:15:26 +0000142 // 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 +0000143 // invalidated by future mutators.
Yu Liud2a95952024-10-10 00:15:26 +0000144 VisitDirectDepsProxyAllowDisabled(visit func(proxy ModuleProxy))
Yu Liudd9ccb42024-10-07 17:07:44 +0000145
Colin Cross69452e12023-11-15 11:20:53 -0800146 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
147
Yu Liud2a95952024-10-10 00:15:26 +0000148 VisitDirectDepsProxyWithTag(tag blueprint.DependencyTag, visit func(proxy ModuleProxy))
149
Colin Cross69452e12023-11-15 11:20:53 -0800150 // VisitDirectDepsIf calls pred for each direct dependency, and if pred returns true calls visit. If there are
151 // multiple direct dependencies on the same module pred and visit will be called multiple times on that module and
152 // OtherModuleDependencyTag will return a different tag for each. It skips any
153 // dependencies that are not an android.Module.
154 //
155 // The Module passed to the visit function should not be retained outside of the visit function, it may be
156 // invalidated by future mutators.
157 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
158 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
159 VisitDepsDepthFirst(visit func(Module))
160 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
161 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
162
163 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
164 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
165 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
166 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
167 // any dependencies that are not an android.Module.
168 //
169 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
170 // invalidated by future mutators.
171 WalkDeps(visit func(child, parent Module) bool)
172
Yu Liudd9ccb42024-10-07 17:07:44 +0000173 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
174 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
175 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
176 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
177 // any dependencies that are not an android.Module.
178 //
179 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
180 // invalidated by future mutators.
Yu Liud2a95952024-10-10 00:15:26 +0000181 WalkDepsProxy(visit func(child, parent ModuleProxy) bool)
Yu Liudd9ccb42024-10-07 17:07:44 +0000182
Colin Cross69452e12023-11-15 11:20:53 -0800183 // GetWalkPath is supposed to be called in visit function passed in WalkDeps()
184 // and returns a top-down dependency path from a start module to current child module.
185 GetWalkPath() []Module
186
187 // PrimaryModule returns the first variant of the current module. Variants of a module are always visited in
188 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from the
189 // Module returned by PrimaryModule without data races. This can be used to perform singleton actions that are
190 // only done once for all variants of a module.
191 PrimaryModule() Module
192
193 // FinalModule returns the last variant of the current module. Variants of a module are always visited in
194 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from all
195 // variants using VisitAllModuleVariants if the current module == FinalModule(). This can be used to perform
196 // singleton actions that are only done once for all variants of a module.
197 FinalModule() Module
198
Yu Liu88ea9ff2024-11-07 19:19:42 +0000199 // IsFinalModule returns if the current module is the last variant. Variants of a module are always visited in
200 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from all
201 // variants using VisitAllModuleVariants if the current module is the last one. This can be used to perform
202 // singleton actions that are only done once for all variants of a module.
203 IsFinalModule(module Module) bool
204
Colin Cross69452e12023-11-15 11:20:53 -0800205 // VisitAllModuleVariants calls visit for each variant of the current module. Variants of a module are always
206 // visited in order by mutators and GenerateBuildActions, so the data created by the current mutator can be read
Yu Liu88ea9ff2024-11-07 19:19:42 +0000207 // from all variants if the current module is the last one. Otherwise, care must be taken to not access any
Colin Cross69452e12023-11-15 11:20:53 -0800208 // data modified by the current mutator.
209 VisitAllModuleVariants(visit func(Module))
210
Yu Liub5275322024-11-13 18:40:43 +0000211 // VisitAllModuleVariantProxies calls visit for each variant of the current module. Variants of a module are always
212 // visited in order by mutators and GenerateBuildActions, so the data created by the current mutator can be read
213 // from all variants if the current module is the last one. Otherwise, care must be taken to not access any
214 // data modified by the current mutator.
215 VisitAllModuleVariantProxies(visit func(proxy ModuleProxy))
216
Colin Cross69452e12023-11-15 11:20:53 -0800217 // GetTagPath is supposed to be called in visit function passed in WalkDeps()
218 // and returns a top-down dependency tags path from a start module to current child module.
219 // It has one less entry than GetWalkPath() as it contains the dependency tags that
220 // exist between each adjacent pair of modules in the GetWalkPath().
221 // GetTagPath()[i] is the tag between GetWalkPath()[i] and GetWalkPath()[i+1]
222 GetTagPath() []blueprint.DependencyTag
223
224 // GetPathString is supposed to be called in visit function passed in WalkDeps()
225 // and returns a multi-line string showing the modules and dependency tags
226 // among them along the top-down dependency path from a start module to current child module.
227 // skipFirst when set to true, the output doesn't include the start module,
228 // which is already printed when this function is used along with ModuleErrorf().
229 GetPathString(skipFirst bool) string
230
231 AddMissingDependencies(missingDeps []string)
232
233 // getMissingDependencies returns the list of missing dependencies.
234 // Calling this function prevents adding new dependencies.
235 getMissingDependencies() []string
Cole Faustbdd8aee2024-03-14 14:33:02 -0700236
237 // EvaluateConfiguration makes ModuleContext a valid proptools.ConfigurableEvaluator, so this context
238 // can be used to evaluate the final value of Configurable properties.
Cole Faustfdbf5d42024-04-10 15:01:23 -0700239 EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue
Colin Cross69452e12023-11-15 11:20:53 -0800240}
241
242type baseModuleContext struct {
243 bp blueprint.BaseModuleContext
244 earlyModuleContext
Colin Cross1d3d9f12024-01-18 14:30:22 -0800245 archModuleContext
Colin Cross69452e12023-11-15 11:20:53 -0800246
247 walkPath []Module
248 tagPath []blueprint.DependencyTag
249
250 strictVisitDeps bool // If true, enforce that all dependencies are enabled
251
Colin Cross69452e12023-11-15 11:20:53 -0800252}
253
Yu Liudd9ccb42024-10-07 17:07:44 +0000254func getWrappedModule(module blueprint.Module) blueprint.Module {
255 if mp, isProxy := module.(ModuleProxy); isProxy {
256 return mp.module
257 }
258 return module
259}
260
261func (b *baseModuleContext) EqualModules(m1, m2 Module) bool {
262 return b.bp.EqualModules(getWrappedModule(m1), getWrappedModule(m2))
263}
264
Colin Cross69452e12023-11-15 11:20:53 -0800265func (b *baseModuleContext) OtherModuleName(m blueprint.Module) string {
Yu Liudd9ccb42024-10-07 17:07:44 +0000266 return b.bp.OtherModuleName(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800267}
Yu Liud3228ac2024-11-08 23:11:47 +0000268func (b *baseModuleContext) OtherModuleDir(m blueprint.Module) string {
269 return b.bp.OtherModuleDir(getWrappedModule(m))
270}
Colin Cross69452e12023-11-15 11:20:53 -0800271func (b *baseModuleContext) OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) {
Yu Liuac483e02024-11-11 22:29:30 +0000272 b.bp.OtherModuleErrorf(getWrappedModule(m), fmt, args...)
Colin Cross69452e12023-11-15 11:20:53 -0800273}
274func (b *baseModuleContext) OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag {
Yu Liudd9ccb42024-10-07 17:07:44 +0000275 return b.bp.OtherModuleDependencyTag(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800276}
277func (b *baseModuleContext) OtherModuleExists(name string) bool { return b.bp.OtherModuleExists(name) }
278func (b *baseModuleContext) OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool {
279 return b.bp.OtherModuleDependencyVariantExists(variations, name)
280}
281func (b *baseModuleContext) OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool {
282 return b.bp.OtherModuleFarDependencyVariantExists(variations, name)
283}
284func (b *baseModuleContext) OtherModuleReverseDependencyVariantExists(name string) bool {
285 return b.bp.OtherModuleReverseDependencyVariantExists(name)
286}
287func (b *baseModuleContext) OtherModuleType(m blueprint.Module) string {
Yu Liub1bfa9d2024-12-05 18:57:51 +0000288 return b.bp.OtherModuleType(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800289}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800290
291func (b *baseModuleContext) otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800292 return b.bp.OtherModuleProvider(m, provider)
293}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800294
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000295func (b *baseModuleContext) OtherModuleIsAutoGenerated(m blueprint.Module) bool {
296 return b.bp.OtherModuleIsAutoGenerated(m)
297}
298
Colin Cross3c0a83d2023-12-12 14:13:26 -0800299func (b *baseModuleContext) provider(provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800300 return b.bp.Provider(provider)
301}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800302
Colin Cross24c1cbe2023-12-21 23:42:56 +0000303func (b *baseModuleContext) setProvider(provider blueprint.AnyProviderKey, value any) {
Colin Cross69452e12023-11-15 11:20:53 -0800304 b.bp.SetProvider(provider, value)
305}
306
Yu Liud3228ac2024-11-08 23:11:47 +0000307func (b *baseModuleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) Module {
308 if module := b.bp.GetDirectDepWithTag(name, tag); module != nil {
309 return module.(Module)
310 }
311 return nil
Colin Cross69452e12023-11-15 11:20:53 -0800312}
313
314func (b *baseModuleContext) blueprintBaseModuleContext() blueprint.BaseModuleContext {
315 return b.bp
316}
317
Colin Cross69452e12023-11-15 11:20:53 -0800318func (b *baseModuleContext) AddMissingDependencies(deps []string) {
319 if deps != nil {
320 missingDeps := &b.Module().base().commonProperties.MissingDeps
321 *missingDeps = append(*missingDeps, deps...)
322 *missingDeps = FirstUniqueStrings(*missingDeps)
323 }
324}
325
326func (b *baseModuleContext) checkedMissingDeps() bool {
327 return b.Module().base().commonProperties.CheckedMissingDeps
328}
329
330func (b *baseModuleContext) getMissingDependencies() []string {
331 checked := &b.Module().base().commonProperties.CheckedMissingDeps
332 *checked = true
333 var missingDeps []string
334 missingDeps = append(missingDeps, b.Module().base().commonProperties.MissingDeps...)
335 missingDeps = append(missingDeps, b.bp.EarlyGetMissingDependencies()...)
336 missingDeps = FirstUniqueStrings(missingDeps)
337 return missingDeps
338}
339
340type AllowDisabledModuleDependency interface {
341 blueprint.DependencyTag
342 AllowDisabledModuleDependency(target Module) bool
Yu Liud2a95952024-10-10 00:15:26 +0000343 AllowDisabledModuleDependencyProxy(ctx OtherModuleProviderContext, target ModuleProxy) bool
Colin Cross69452e12023-11-15 11:20:53 -0800344}
345
Jiyong Park8bcf3c62024-03-18 18:37:10 +0900346type AlwaysAllowDisabledModuleDependencyTag struct{}
347
348func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependency(Module) bool {
349 return true
350}
351
Yu Liud2a95952024-10-10 00:15:26 +0000352func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependencyProxy(OtherModuleProviderContext, ModuleProxy) bool {
353 return true
354}
355
Colin Cross648daea2024-09-12 14:35:29 -0700356func (b *baseModuleContext) validateAndroidModule(module blueprint.Module, tag blueprint.DependencyTag, strict bool) Module {
Colin Cross69452e12023-11-15 11:20:53 -0800357 aModule, _ := module.(Module)
358
359 if !strict {
360 return aModule
361 }
362
363 if aModule == nil {
Colin Cross648daea2024-09-12 14:35:29 -0700364 panic(fmt.Errorf("module %q (%#v) not an android module", b.OtherModuleName(module), tag))
Colin Cross69452e12023-11-15 11:20:53 -0800365 }
366
Cole Fausta963b942024-04-11 17:43:00 -0700367 if !aModule.Enabled(b) {
Colin Cross69452e12023-11-15 11:20:53 -0800368 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependency(aModule) {
369 if b.Config().AllowMissingDependencies() {
370 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
371 } else {
372 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
373 }
374 }
375 return nil
376 }
377 return aModule
378}
379
Yu Liud2a95952024-10-10 00:15:26 +0000380func (b *baseModuleContext) validateAndroidModuleProxy(
381 module blueprint.ModuleProxy, tag blueprint.DependencyTag, strict bool) *ModuleProxy {
382 aModule := ModuleProxy{module: module}
383
384 if !strict {
385 return &aModule
386 }
387
Yu Liub5275322024-11-13 18:40:43 +0000388 if !OtherModuleProviderOrDefault(b, module, CommonModuleInfoKey).Enabled {
Yu Liud2a95952024-10-10 00:15:26 +0000389 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependencyProxy(b, aModule) {
390 if b.Config().AllowMissingDependencies() {
391 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
392 } else {
393 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
394 }
395 }
396 return nil
397 }
398
399 return &aModule
400}
401
Colin Cross69452e12023-11-15 11:20:53 -0800402type dep struct {
403 mod blueprint.Module
404 tag blueprint.DependencyTag
405}
406
407func (b *baseModuleContext) getDirectDepsInternal(name string, tag blueprint.DependencyTag) []dep {
408 var deps []dep
Colin Cross648daea2024-09-12 14:35:29 -0700409 b.VisitDirectDeps(func(module Module) {
410 if module.base().BaseModuleName() == name {
Colin Cross69452e12023-11-15 11:20:53 -0800411 returnedTag := b.bp.OtherModuleDependencyTag(module)
412 if tag == nil || returnedTag == tag {
413 deps = append(deps, dep{module, returnedTag})
414 }
415 }
416 })
417 return deps
418}
419
420func (b *baseModuleContext) getDirectDepInternal(name string, tag blueprint.DependencyTag) (blueprint.Module, blueprint.DependencyTag) {
421 deps := b.getDirectDepsInternal(name, tag)
422 if len(deps) == 1 {
423 return deps[0].mod, deps[0].tag
424 } else if len(deps) >= 2 {
425 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
426 name, b.ModuleName()))
427 } else {
428 return nil, nil
429 }
430}
431
Colin Cross69452e12023-11-15 11:20:53 -0800432func (b *baseModuleContext) GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module {
433 var deps []Module
Colin Cross648daea2024-09-12 14:35:29 -0700434 b.VisitDirectDeps(func(module Module) {
435 if b.bp.OtherModuleDependencyTag(module) == tag {
436 deps = append(deps, module)
Colin Cross69452e12023-11-15 11:20:53 -0800437 }
438 })
439 return deps
440}
441
Yu Liuf432c2e2024-12-17 00:09:15 +0000442func (b *baseModuleContext) GetDirectDepsProxyWithTag(tag blueprint.DependencyTag) []ModuleProxy {
443 var deps []ModuleProxy
444 b.VisitDirectDepsProxy(func(module ModuleProxy) {
445 if b.OtherModuleDependencyTag(module) == tag {
446 deps = append(deps, module)
447 }
448 })
449 return deps
450}
451
Colin Cross69452e12023-11-15 11:20:53 -0800452func (b *baseModuleContext) VisitDirectDeps(visit func(Module)) {
453 b.bp.VisitDirectDeps(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700454 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800455 visit(aModule)
456 }
457 })
458}
459
Yu Liud3228ac2024-11-08 23:11:47 +0000460func (b *baseModuleContext) VisitDirectDepsProxy(visit func(ModuleProxy)) {
461 b.bp.VisitDirectDepsProxy(func(module blueprint.ModuleProxy) {
462 if aModule := b.validateAndroidModuleProxy(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
463 visit(*aModule)
464 }
Colin Cross648daea2024-09-12 14:35:29 -0700465 })
466}
467
Yu Liud2a95952024-10-10 00:15:26 +0000468func (b *baseModuleContext) VisitDirectDepsProxyAllowDisabled(visit func(proxy ModuleProxy)) {
Yu Liud3228ac2024-11-08 23:11:47 +0000469 b.bp.VisitDirectDepsProxy(visitProxyAdaptor(visit))
Yu Liudd9ccb42024-10-07 17:07:44 +0000470}
471
Colin Cross69452e12023-11-15 11:20:53 -0800472func (b *baseModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
473 b.bp.VisitDirectDeps(func(module blueprint.Module) {
474 if b.bp.OtherModuleDependencyTag(module) == tag {
Yu Liud2a95952024-10-10 00:15:26 +0000475 if aModule := b.validateAndroidModule(module, tag, b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800476 visit(aModule)
477 }
478 }
479 })
480}
481
Yu Liud2a95952024-10-10 00:15:26 +0000482func (b *baseModuleContext) VisitDirectDepsProxyWithTag(tag blueprint.DependencyTag, visit func(proxy ModuleProxy)) {
483 b.bp.VisitDirectDepsProxy(func(module blueprint.ModuleProxy) {
484 if b.bp.OtherModuleDependencyTag(module) == tag {
485 if aModule := b.validateAndroidModuleProxy(module, tag, b.strictVisitDeps); aModule != nil {
486 visit(*aModule)
487 }
488 }
489 })
490}
491
Colin Cross69452e12023-11-15 11:20:53 -0800492func (b *baseModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
493 b.bp.VisitDirectDepsIf(
494 // pred
495 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700496 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800497 return pred(aModule)
498 } else {
499 return false
500 }
501 },
502 // visit
503 func(module blueprint.Module) {
504 visit(module.(Module))
505 })
506}
507
508func (b *baseModuleContext) VisitDepsDepthFirst(visit func(Module)) {
509 b.bp.VisitDepsDepthFirst(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700510 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800511 visit(aModule)
512 }
513 })
514}
515
516func (b *baseModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
517 b.bp.VisitDepsDepthFirstIf(
518 // pred
519 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700520 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800521 return pred(aModule)
522 } else {
523 return false
524 }
525 },
526 // visit
527 func(module blueprint.Module) {
528 visit(module.(Module))
529 })
530}
531
Colin Cross69452e12023-11-15 11:20:53 -0800532func (b *baseModuleContext) WalkDeps(visit func(Module, Module) bool) {
533 b.walkPath = []Module{b.Module()}
534 b.tagPath = []blueprint.DependencyTag{}
535 b.bp.WalkDeps(func(child, parent blueprint.Module) bool {
536 childAndroidModule, _ := child.(Module)
537 parentAndroidModule, _ := parent.(Module)
538 if childAndroidModule != nil && parentAndroidModule != nil {
539 // record walkPath before visit
540 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
541 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
542 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
543 }
544 b.walkPath = append(b.walkPath, childAndroidModule)
545 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
546 return visit(childAndroidModule, parentAndroidModule)
547 } else {
548 return false
549 }
550 })
551}
552
Yu Liud2a95952024-10-10 00:15:26 +0000553func (b *baseModuleContext) WalkDepsProxy(visit func(ModuleProxy, ModuleProxy) bool) {
Yu Liudd9ccb42024-10-07 17:07:44 +0000554 b.walkPath = []Module{ModuleProxy{blueprint.CreateModuleProxy(b.Module())}}
555 b.tagPath = []blueprint.DependencyTag{}
556 b.bp.WalkDepsProxy(func(child, parent blueprint.ModuleProxy) bool {
557 childAndroidModule := ModuleProxy{child}
558 parentAndroidModule := ModuleProxy{parent}
559 // record walkPath before visit
560 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
561 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
562 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
563 }
564 b.walkPath = append(b.walkPath, childAndroidModule)
565 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
566 return visit(childAndroidModule, parentAndroidModule)
567 })
568}
569
Colin Cross69452e12023-11-15 11:20:53 -0800570func (b *baseModuleContext) GetWalkPath() []Module {
Colin Crossb614cd42024-10-11 12:52:21 -0700571 return slices.Clone(b.walkPath)
Colin Cross69452e12023-11-15 11:20:53 -0800572}
573
574func (b *baseModuleContext) GetTagPath() []blueprint.DependencyTag {
575 return b.tagPath
576}
577
578func (b *baseModuleContext) VisitAllModuleVariants(visit func(Module)) {
579 b.bp.VisitAllModuleVariants(func(module blueprint.Module) {
580 visit(module.(Module))
581 })
582}
583
Yu Liub5275322024-11-13 18:40:43 +0000584func (b *baseModuleContext) VisitAllModuleVariantProxies(visit func(ModuleProxy)) {
585 b.bp.VisitAllModuleVariantProxies(visitProxyAdaptor(visit))
586}
587
Colin Cross69452e12023-11-15 11:20:53 -0800588func (b *baseModuleContext) PrimaryModule() Module {
589 return b.bp.PrimaryModule().(Module)
590}
591
592func (b *baseModuleContext) FinalModule() Module {
593 return b.bp.FinalModule().(Module)
594}
595
Yu Liu88ea9ff2024-11-07 19:19:42 +0000596func (b *baseModuleContext) IsFinalModule(module Module) bool {
597 return b.bp.IsFinalModule(module)
598}
599
Colin Cross69452e12023-11-15 11:20:53 -0800600// IsMetaDependencyTag returns true for cross-cutting metadata dependencies.
601func IsMetaDependencyTag(tag blueprint.DependencyTag) bool {
602 if tag == licenseKindTag {
603 return true
604 } else if tag == licensesTag {
605 return true
Jihoon Kanga3a05462024-04-05 00:36:44 +0000606 } else if tag == AcDepTag {
Colin Cross69452e12023-11-15 11:20:53 -0800607 return true
608 }
609 return false
610}
611
612// A regexp for removing boilerplate from BaseDependencyTag from the string representation of
613// a dependency tag.
614var tagCleaner = regexp.MustCompile(`\QBaseDependencyTag:{}\E(, )?`)
615
616// PrettyPrintTag returns string representation of the tag, but prefers
617// custom String() method if available.
618func PrettyPrintTag(tag blueprint.DependencyTag) string {
619 // Use tag's custom String() method if available.
620 if stringer, ok := tag.(fmt.Stringer); ok {
621 return stringer.String()
622 }
623
624 // Otherwise, get a default string representation of the tag's struct.
625 tagString := fmt.Sprintf("%T: %+v", tag, tag)
626
627 // Remove the boilerplate from BaseDependencyTag as it adds no value.
628 tagString = tagCleaner.ReplaceAllString(tagString, "")
629 return tagString
630}
631
632func (b *baseModuleContext) GetPathString(skipFirst bool) string {
633 sb := strings.Builder{}
634 tagPath := b.GetTagPath()
635 walkPath := b.GetWalkPath()
636 if !skipFirst {
637 sb.WriteString(walkPath[0].String())
638 }
639 for i, m := range walkPath[1:] {
640 sb.WriteString("\n")
641 sb.WriteString(fmt.Sprintf(" via tag %s\n", PrettyPrintTag(tagPath[i])))
642 sb.WriteString(fmt.Sprintf(" -> %s", m.String()))
643 }
644 return sb.String()
645}
Cole Faustbdd8aee2024-03-14 14:33:02 -0700646
Cole Faustfdbf5d42024-04-10 15:01:23 -0700647func (m *baseModuleContext) EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue {
648 return m.Module().ConfigurableEvaluator(m).EvaluateConfiguration(condition, property)
Cole Faustbdd8aee2024-03-14 14:33:02 -0700649}