blob: 2c47e1583ddbd7f78e65f3a56901288252264798 [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
Jihoon Kangf50d2e62025-02-19 00:47:23 +000056 // OtherModuleSubDir returns the string representing the variations of a module.
57 OtherModuleSubDir(m blueprint.Module) string
58
Colin Cross69452e12023-11-15 11:20:53 -080059 // OtherModuleExists returns true if a module with the specified name exists, as determined by the NameInterface
60 // passed to Context.SetNameInterface, or SimpleNameInterface if it was not called.
61 OtherModuleExists(name string) bool
62
63 // OtherModuleDependencyVariantExists returns true if a module with the
64 // specified name and variant exists. The variant must match the given
65 // variations. It must also match all the non-local variations of the current
66 // module. In other words, it checks for the module that AddVariationDependencies
67 // would add a dependency on with the same arguments.
68 OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool
69
70 // OtherModuleFarDependencyVariantExists returns true if a module with the
71 // specified name and variant exists. The variant must match the given
72 // variations, but not the non-local variations of the current module. In
73 // other words, it checks for the module that AddFarVariationDependencies
74 // would add a dependency on with the same arguments.
75 OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool
76
77 // OtherModuleReverseDependencyVariantExists returns true if a module with the
78 // specified name exists with the same variations as the current module. In
79 // other words, it checks for the module that AddReverseDependency would add a
80 // dependency on with the same argument.
81 OtherModuleReverseDependencyVariantExists(name string) bool
82
83 // OtherModuleType returns the type of another Module. See BaseModuleContext.ModuleType for more information.
84 // It is intended for use inside the visit functions of Visit* and WalkDeps.
85 OtherModuleType(m blueprint.Module) string
86
Colin Cross24c1cbe2023-12-21 23:42:56 +000087 // otherModuleProvider returns the value for a provider for the given module. If the value is
88 // not set it returns nil and false. The value returned may be a deep copy of the value originally
89 // passed to SetProvider.
90 //
91 // This method shouldn't be used directly, prefer the type-safe android.OtherModuleProvider instead.
Colin Cross3c0a83d2023-12-12 14:13:26 -080092 otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool)
Colin Cross69452e12023-11-15 11:20:53 -080093
Jihoon Kang2a7bf752024-11-01 21:21:25 +000094 // OtherModuleIsAutoGenerated returns true if the module is auto generated by another module
95 // instead of being defined in Android.bp file.
96 OtherModuleIsAutoGenerated(m blueprint.Module) bool
97
Colin Cross69452e12023-11-15 11:20:53 -080098 // Provider returns the value for a provider for the current module. If the value is
Colin Cross24c1cbe2023-12-21 23:42:56 +000099 // not set it returns nil and false. It panics if called before the appropriate
Colin Cross69452e12023-11-15 11:20:53 -0800100 // mutator or GenerateBuildActions pass for the provider. The value returned may be a deep
101 // copy of the value originally passed to SetProvider.
Colin Cross24c1cbe2023-12-21 23:42:56 +0000102 //
103 // This method shouldn't be used directly, prefer the type-safe android.ModuleProvider instead.
Colin Cross3c0a83d2023-12-12 14:13:26 -0800104 provider(provider blueprint.AnyProviderKey) (any, bool)
Colin Cross69452e12023-11-15 11:20:53 -0800105
Colin Cross24c1cbe2023-12-21 23:42:56 +0000106 // setProvider sets the value for a provider for the current module. It panics if not called
Colin Cross69452e12023-11-15 11:20:53 -0800107 // during the appropriate mutator or GenerateBuildActions pass for the provider, if the value
108 // is not of the appropriate type, or if the value has already been set. The value should not
109 // be modified after being passed to SetProvider.
Colin Cross24c1cbe2023-12-21 23:42:56 +0000110 //
111 // This method shouldn't be used directly, prefer the type-safe android.SetProvider instead.
112 setProvider(provider blueprint.AnyProviderKey, value any)
Colin Cross69452e12023-11-15 11:20:53 -0800113
114 GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module
115
Yu Liuf432c2e2024-12-17 00:09:15 +0000116 GetDirectDepsProxyWithTag(tag blueprint.DependencyTag) []ModuleProxy
117
Colin Cross69452e12023-11-15 11:20:53 -0800118 // GetDirectDepWithTag returns the Module the direct dependency with the specified name, or nil if
119 // none exists. It panics if the dependency does not have the specified tag. It skips any
120 // dependencies that are not an android.Module.
Yu Liud3228ac2024-11-08 23:11:47 +0000121 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) Module
Colin Cross69452e12023-11-15 11:20:53 -0800122
Yu Liu3ae96652024-12-17 22:27:38 +0000123 GetDirectDepProxyWithTag(name string, tag blueprint.DependencyTag) *ModuleProxy
124
Colin Cross648daea2024-09-12 14:35:29 -0700125 // VisitDirectDeps calls visit for each direct dependency. If there are multiple
126 // direct dependencies on the same module visit will be called multiple times on that module
127 // and OtherModuleDependencyTag will return a different tag for each. It raises an error if any of the
128 // dependencies are disabled.
129 //
130 // The Module passed to the visit function should not be retained outside of the visit
131 // function, it may be invalidated by future mutators.
132 VisitDirectDeps(visit func(Module))
133
Yu Liud3228ac2024-11-08 23:11:47 +0000134 // VisitDirectDepsProxy calls visit for each direct dependency. If there are multiple
Colin Cross69452e12023-11-15 11:20:53 -0800135 // direct dependencies on the same module visit will be called multiple times on that module
Yu Liud3228ac2024-11-08 23:11:47 +0000136 // and OtherModuleDependencyTag will return a different tag for each. It raises an error if any of the
137 // dependencies are disabled.
Colin Cross69452e12023-11-15 11:20:53 -0800138 //
Yu Liud3228ac2024-11-08 23:11:47 +0000139 // The ModuleProxy passed to the visit function should not be retained outside of the visit
Colin Cross69452e12023-11-15 11:20:53 -0800140 // function, it may be invalidated by future mutators.
Yu Liud3228ac2024-11-08 23:11:47 +0000141 VisitDirectDepsProxy(visit func(proxy ModuleProxy))
Colin Cross69452e12023-11-15 11:20:53 -0800142
Yu Liudd9ccb42024-10-07 17:07:44 +0000143 // VisitDirectDepsProxyAllowDisabled calls visit for each direct dependency. If there are
144 // multiple direct dependencies on the same module visit will be called multiple times on
145 // that module and OtherModuleDependencyTag will return a different tag for each.
146 //
Yu Liud2a95952024-10-10 00:15:26 +0000147 // 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 +0000148 // invalidated by future mutators.
Yu Liud2a95952024-10-10 00:15:26 +0000149 VisitDirectDepsProxyAllowDisabled(visit func(proxy ModuleProxy))
Yu Liudd9ccb42024-10-07 17:07:44 +0000150
Colin Cross69452e12023-11-15 11:20:53 -0800151 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
152
Yu Liud2a95952024-10-10 00:15:26 +0000153 VisitDirectDepsProxyWithTag(tag blueprint.DependencyTag, visit func(proxy ModuleProxy))
154
Colin Cross69452e12023-11-15 11:20:53 -0800155 // VisitDirectDepsIf calls pred for each direct dependency, and if pred returns true calls visit. If there are
156 // multiple direct dependencies on the same module pred and visit will be called multiple times on that module and
157 // OtherModuleDependencyTag will return a different tag for each. It skips any
158 // dependencies that are not an android.Module.
159 //
160 // The Module passed to the visit function should not be retained outside of the visit function, it may be
161 // invalidated by future mutators.
162 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
163 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
164 VisitDepsDepthFirst(visit func(Module))
165 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
166 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
167
168 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
169 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
170 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
171 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
172 // any dependencies that are not an android.Module.
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 WalkDeps(visit func(child, parent Module) bool)
177
Yu Liudd9ccb42024-10-07 17:07:44 +0000178 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
179 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
180 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
181 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
182 // any dependencies that are not an android.Module.
183 //
184 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
185 // invalidated by future mutators.
Yu Liud2a95952024-10-10 00:15:26 +0000186 WalkDepsProxy(visit func(child, parent ModuleProxy) bool)
Yu Liudd9ccb42024-10-07 17:07:44 +0000187
Colin Cross69452e12023-11-15 11:20:53 -0800188 // GetWalkPath is supposed to be called in visit function passed in WalkDeps()
189 // and returns a top-down dependency path from a start module to current child module.
190 GetWalkPath() []Module
191
192 // PrimaryModule returns the first variant of the current module. Variants of a module are always visited in
193 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from the
194 // Module returned by PrimaryModule without data races. This can be used to perform singleton actions that are
195 // only done once for all variants of a module.
196 PrimaryModule() Module
197
198 // FinalModule returns the last variant of the current module. Variants of a module are always visited in
199 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from all
200 // variants using VisitAllModuleVariants if the current module == FinalModule(). This can be used to perform
201 // singleton actions that are only done once for all variants of a module.
202 FinalModule() Module
203
Yu Liu88ea9ff2024-11-07 19:19:42 +0000204 // IsFinalModule returns if the current module is the last variant. Variants of a module are always visited in
205 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from all
206 // variants using VisitAllModuleVariants if the current module is the last one. This can be used to perform
207 // singleton actions that are only done once for all variants of a module.
208 IsFinalModule(module Module) bool
209
Colin Cross69452e12023-11-15 11:20:53 -0800210 // VisitAllModuleVariants calls visit for each variant of the current module. Variants of a module are always
211 // 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 +0000212 // 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 -0800213 // data modified by the current mutator.
214 VisitAllModuleVariants(visit func(Module))
215
Yu Liub5275322024-11-13 18:40:43 +0000216 // VisitAllModuleVariantProxies calls visit for each variant of the current module. Variants of a module are always
217 // visited in order by mutators and GenerateBuildActions, so the data created by the current mutator can be read
218 // from all variants if the current module is the last one. Otherwise, care must be taken to not access any
219 // data modified by the current mutator.
220 VisitAllModuleVariantProxies(visit func(proxy ModuleProxy))
221
Colin Cross69452e12023-11-15 11:20:53 -0800222 // GetTagPath is supposed to be called in visit function passed in WalkDeps()
223 // and returns a top-down dependency tags path from a start module to current child module.
224 // It has one less entry than GetWalkPath() as it contains the dependency tags that
225 // exist between each adjacent pair of modules in the GetWalkPath().
226 // GetTagPath()[i] is the tag between GetWalkPath()[i] and GetWalkPath()[i+1]
227 GetTagPath() []blueprint.DependencyTag
228
229 // GetPathString is supposed to be called in visit function passed in WalkDeps()
230 // and returns a multi-line string showing the modules and dependency tags
231 // among them along the top-down dependency path from a start module to current child module.
232 // skipFirst when set to true, the output doesn't include the start module,
233 // which is already printed when this function is used along with ModuleErrorf().
234 GetPathString(skipFirst bool) string
235
236 AddMissingDependencies(missingDeps []string)
237
238 // getMissingDependencies returns the list of missing dependencies.
239 // Calling this function prevents adding new dependencies.
240 getMissingDependencies() []string
Cole Faustbdd8aee2024-03-14 14:33:02 -0700241
242 // EvaluateConfiguration makes ModuleContext a valid proptools.ConfigurableEvaluator, so this context
243 // can be used to evaluate the final value of Configurable properties.
Cole Faustfdbf5d42024-04-10 15:01:23 -0700244 EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue
Colin Cross69452e12023-11-15 11:20:53 -0800245}
246
247type baseModuleContext struct {
248 bp blueprint.BaseModuleContext
249 earlyModuleContext
Colin Cross1d3d9f12024-01-18 14:30:22 -0800250 archModuleContext
Colin Cross69452e12023-11-15 11:20:53 -0800251
252 walkPath []Module
253 tagPath []blueprint.DependencyTag
254
255 strictVisitDeps bool // If true, enforce that all dependencies are enabled
256
Colin Cross69452e12023-11-15 11:20:53 -0800257}
258
Yu Liudd9ccb42024-10-07 17:07:44 +0000259func getWrappedModule(module blueprint.Module) blueprint.Module {
Cole Faust19eb09d2025-01-14 13:27:00 -0800260 if mp, isProxy := module.(*ModuleProxy); isProxy {
261 return mp.module
262 }
Yu Liudd9ccb42024-10-07 17:07:44 +0000263 if mp, isProxy := module.(ModuleProxy); isProxy {
264 return mp.module
265 }
266 return module
267}
268
269func (b *baseModuleContext) EqualModules(m1, m2 Module) bool {
270 return b.bp.EqualModules(getWrappedModule(m1), getWrappedModule(m2))
271}
272
Colin Cross69452e12023-11-15 11:20:53 -0800273func (b *baseModuleContext) OtherModuleName(m blueprint.Module) string {
Yu Liudd9ccb42024-10-07 17:07:44 +0000274 return b.bp.OtherModuleName(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800275}
Yu Liud3228ac2024-11-08 23:11:47 +0000276func (b *baseModuleContext) OtherModuleDir(m blueprint.Module) string {
277 return b.bp.OtherModuleDir(getWrappedModule(m))
278}
Colin Cross69452e12023-11-15 11:20:53 -0800279func (b *baseModuleContext) OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) {
Yu Liuac483e02024-11-11 22:29:30 +0000280 b.bp.OtherModuleErrorf(getWrappedModule(m), fmt, args...)
Colin Cross69452e12023-11-15 11:20:53 -0800281}
282func (b *baseModuleContext) OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag {
Yu Liudd9ccb42024-10-07 17:07:44 +0000283 return b.bp.OtherModuleDependencyTag(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800284}
Jihoon Kangf50d2e62025-02-19 00:47:23 +0000285func (b *baseModuleContext) OtherModuleSubDir(m blueprint.Module) string {
286 return b.bp.OtherModuleSubDir(getWrappedModule(m))
287}
Colin Cross69452e12023-11-15 11:20:53 -0800288func (b *baseModuleContext) OtherModuleExists(name string) bool { return b.bp.OtherModuleExists(name) }
289func (b *baseModuleContext) OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool {
290 return b.bp.OtherModuleDependencyVariantExists(variations, name)
291}
292func (b *baseModuleContext) OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool {
293 return b.bp.OtherModuleFarDependencyVariantExists(variations, name)
294}
295func (b *baseModuleContext) OtherModuleReverseDependencyVariantExists(name string) bool {
296 return b.bp.OtherModuleReverseDependencyVariantExists(name)
297}
298func (b *baseModuleContext) OtherModuleType(m blueprint.Module) string {
Yu Liub1bfa9d2024-12-05 18:57:51 +0000299 return b.bp.OtherModuleType(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800300}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800301
302func (b *baseModuleContext) otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) {
Cole Faust19eb09d2025-01-14 13:27:00 -0800303 return b.bp.OtherModuleProvider(getWrappedModule(m), provider)
Colin Cross69452e12023-11-15 11:20:53 -0800304}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800305
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000306func (b *baseModuleContext) OtherModuleIsAutoGenerated(m blueprint.Module) bool {
307 return b.bp.OtherModuleIsAutoGenerated(m)
308}
309
Colin Cross3c0a83d2023-12-12 14:13:26 -0800310func (b *baseModuleContext) provider(provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800311 return b.bp.Provider(provider)
312}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800313
Colin Cross24c1cbe2023-12-21 23:42:56 +0000314func (b *baseModuleContext) setProvider(provider blueprint.AnyProviderKey, value any) {
Colin Cross69452e12023-11-15 11:20:53 -0800315 b.bp.SetProvider(provider, value)
316}
317
Yu Liud3228ac2024-11-08 23:11:47 +0000318func (b *baseModuleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) Module {
319 if module := b.bp.GetDirectDepWithTag(name, tag); module != nil {
320 return module.(Module)
321 }
322 return nil
Colin Cross69452e12023-11-15 11:20:53 -0800323}
324
Yu Liu3ae96652024-12-17 22:27:38 +0000325func (b *baseModuleContext) GetDirectDepProxyWithTag(name string, tag blueprint.DependencyTag) *ModuleProxy {
326 if module := b.bp.GetDirectDepProxyWithTag(name, tag); module != nil {
327 return &ModuleProxy{*module}
328 }
329 return nil
330}
331
Colin Cross69452e12023-11-15 11:20:53 -0800332func (b *baseModuleContext) blueprintBaseModuleContext() blueprint.BaseModuleContext {
333 return b.bp
334}
335
Colin Cross69452e12023-11-15 11:20:53 -0800336func (b *baseModuleContext) AddMissingDependencies(deps []string) {
337 if deps != nil {
338 missingDeps := &b.Module().base().commonProperties.MissingDeps
339 *missingDeps = append(*missingDeps, deps...)
340 *missingDeps = FirstUniqueStrings(*missingDeps)
341 }
342}
343
344func (b *baseModuleContext) checkedMissingDeps() bool {
345 return b.Module().base().commonProperties.CheckedMissingDeps
346}
347
348func (b *baseModuleContext) getMissingDependencies() []string {
349 checked := &b.Module().base().commonProperties.CheckedMissingDeps
350 *checked = true
351 var missingDeps []string
352 missingDeps = append(missingDeps, b.Module().base().commonProperties.MissingDeps...)
353 missingDeps = append(missingDeps, b.bp.EarlyGetMissingDependencies()...)
354 missingDeps = FirstUniqueStrings(missingDeps)
355 return missingDeps
356}
357
358type AllowDisabledModuleDependency interface {
359 blueprint.DependencyTag
360 AllowDisabledModuleDependency(target Module) bool
Yu Liud2a95952024-10-10 00:15:26 +0000361 AllowDisabledModuleDependencyProxy(ctx OtherModuleProviderContext, target ModuleProxy) bool
Colin Cross69452e12023-11-15 11:20:53 -0800362}
363
Jiyong Park8bcf3c62024-03-18 18:37:10 +0900364type AlwaysAllowDisabledModuleDependencyTag struct{}
365
366func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependency(Module) bool {
367 return true
368}
369
Yu Liud2a95952024-10-10 00:15:26 +0000370func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependencyProxy(OtherModuleProviderContext, ModuleProxy) bool {
371 return true
372}
373
Colin Cross648daea2024-09-12 14:35:29 -0700374func (b *baseModuleContext) validateAndroidModule(module blueprint.Module, tag blueprint.DependencyTag, strict bool) Module {
Colin Cross69452e12023-11-15 11:20:53 -0800375 aModule, _ := module.(Module)
376
377 if !strict {
378 return aModule
379 }
380
381 if aModule == nil {
Colin Cross648daea2024-09-12 14:35:29 -0700382 panic(fmt.Errorf("module %q (%#v) not an android module", b.OtherModuleName(module), tag))
Colin Cross69452e12023-11-15 11:20:53 -0800383 }
384
Cole Fausta963b942024-04-11 17:43:00 -0700385 if !aModule.Enabled(b) {
Colin Cross69452e12023-11-15 11:20:53 -0800386 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependency(aModule) {
387 if b.Config().AllowMissingDependencies() {
388 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
389 } else {
390 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
391 }
392 }
393 return nil
394 }
395 return aModule
396}
397
Yu Liud2a95952024-10-10 00:15:26 +0000398func (b *baseModuleContext) validateAndroidModuleProxy(
399 module blueprint.ModuleProxy, tag blueprint.DependencyTag, strict bool) *ModuleProxy {
400 aModule := ModuleProxy{module: module}
401
402 if !strict {
403 return &aModule
404 }
405
Yu Liub5275322024-11-13 18:40:43 +0000406 if !OtherModuleProviderOrDefault(b, module, CommonModuleInfoKey).Enabled {
Yu Liud2a95952024-10-10 00:15:26 +0000407 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependencyProxy(b, aModule) {
408 if b.Config().AllowMissingDependencies() {
409 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
410 } else {
411 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
412 }
413 }
414 return nil
415 }
416
417 return &aModule
418}
419
Yu Liu3ae96652024-12-17 22:27:38 +0000420func (b *baseModuleContext) getDirectDepsInternal(name string, tag blueprint.DependencyTag) []Module {
421 var deps []Module
Colin Cross648daea2024-09-12 14:35:29 -0700422 b.VisitDirectDeps(func(module Module) {
423 if module.base().BaseModuleName() == name {
Colin Cross69452e12023-11-15 11:20:53 -0800424 returnedTag := b.bp.OtherModuleDependencyTag(module)
425 if tag == nil || returnedTag == tag {
Yu Liu3ae96652024-12-17 22:27:38 +0000426 deps = append(deps, module)
Colin Cross69452e12023-11-15 11:20:53 -0800427 }
428 }
429 })
430 return deps
431}
432
Yu Liu3ae96652024-12-17 22:27:38 +0000433func (b *baseModuleContext) getDirectDepsProxyInternal(name string, tag blueprint.DependencyTag) []ModuleProxy {
434 var deps []ModuleProxy
435 b.VisitDirectDepsProxy(func(module ModuleProxy) {
436 if OtherModuleProviderOrDefault(b, module, CommonModuleInfoKey).BaseModuleName == name {
437 returnedTag := b.OtherModuleDependencyTag(module)
438 if tag == nil || returnedTag == tag {
439 deps = append(deps, module)
440 }
441 }
442 })
443 return deps
Colin Cross69452e12023-11-15 11:20:53 -0800444}
445
Colin Cross69452e12023-11-15 11:20:53 -0800446func (b *baseModuleContext) GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module {
447 var deps []Module
Colin Cross648daea2024-09-12 14:35:29 -0700448 b.VisitDirectDeps(func(module Module) {
449 if b.bp.OtherModuleDependencyTag(module) == tag {
450 deps = append(deps, module)
Colin Cross69452e12023-11-15 11:20:53 -0800451 }
452 })
453 return deps
454}
455
Yu Liuf432c2e2024-12-17 00:09:15 +0000456func (b *baseModuleContext) GetDirectDepsProxyWithTag(tag blueprint.DependencyTag) []ModuleProxy {
457 var deps []ModuleProxy
458 b.VisitDirectDepsProxy(func(module ModuleProxy) {
459 if b.OtherModuleDependencyTag(module) == tag {
460 deps = append(deps, module)
461 }
462 })
463 return deps
464}
465
Colin Cross69452e12023-11-15 11:20:53 -0800466func (b *baseModuleContext) VisitDirectDeps(visit func(Module)) {
467 b.bp.VisitDirectDeps(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700468 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800469 visit(aModule)
470 }
471 })
472}
473
Yu Liud3228ac2024-11-08 23:11:47 +0000474func (b *baseModuleContext) VisitDirectDepsProxy(visit func(ModuleProxy)) {
475 b.bp.VisitDirectDepsProxy(func(module blueprint.ModuleProxy) {
476 if aModule := b.validateAndroidModuleProxy(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
477 visit(*aModule)
478 }
Colin Cross648daea2024-09-12 14:35:29 -0700479 })
480}
481
Yu Liud2a95952024-10-10 00:15:26 +0000482func (b *baseModuleContext) VisitDirectDepsProxyAllowDisabled(visit func(proxy ModuleProxy)) {
Yu Liud3228ac2024-11-08 23:11:47 +0000483 b.bp.VisitDirectDepsProxy(visitProxyAdaptor(visit))
Yu Liudd9ccb42024-10-07 17:07:44 +0000484}
485
Colin Cross69452e12023-11-15 11:20:53 -0800486func (b *baseModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
487 b.bp.VisitDirectDeps(func(module blueprint.Module) {
488 if b.bp.OtherModuleDependencyTag(module) == tag {
Yu Liud2a95952024-10-10 00:15:26 +0000489 if aModule := b.validateAndroidModule(module, tag, b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800490 visit(aModule)
491 }
492 }
493 })
494}
495
Yu Liud2a95952024-10-10 00:15:26 +0000496func (b *baseModuleContext) VisitDirectDepsProxyWithTag(tag blueprint.DependencyTag, visit func(proxy ModuleProxy)) {
497 b.bp.VisitDirectDepsProxy(func(module blueprint.ModuleProxy) {
498 if b.bp.OtherModuleDependencyTag(module) == tag {
499 if aModule := b.validateAndroidModuleProxy(module, tag, b.strictVisitDeps); aModule != nil {
500 visit(*aModule)
501 }
502 }
503 })
504}
505
Colin Cross69452e12023-11-15 11:20:53 -0800506func (b *baseModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
507 b.bp.VisitDirectDepsIf(
508 // pred
509 func(module blueprint.Module) bool {
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 return pred(aModule)
512 } else {
513 return false
514 }
515 },
516 // visit
517 func(module blueprint.Module) {
518 visit(module.(Module))
519 })
520}
521
522func (b *baseModuleContext) VisitDepsDepthFirst(visit func(Module)) {
523 b.bp.VisitDepsDepthFirst(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700524 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800525 visit(aModule)
526 }
527 })
528}
529
530func (b *baseModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
531 b.bp.VisitDepsDepthFirstIf(
532 // pred
533 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700534 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800535 return pred(aModule)
536 } else {
537 return false
538 }
539 },
540 // visit
541 func(module blueprint.Module) {
542 visit(module.(Module))
543 })
544}
545
Colin Cross69452e12023-11-15 11:20:53 -0800546func (b *baseModuleContext) WalkDeps(visit func(Module, Module) bool) {
547 b.walkPath = []Module{b.Module()}
548 b.tagPath = []blueprint.DependencyTag{}
549 b.bp.WalkDeps(func(child, parent blueprint.Module) bool {
550 childAndroidModule, _ := child.(Module)
551 parentAndroidModule, _ := parent.(Module)
552 if childAndroidModule != nil && parentAndroidModule != nil {
553 // record walkPath before visit
554 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
555 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
556 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
557 }
558 b.walkPath = append(b.walkPath, childAndroidModule)
559 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
560 return visit(childAndroidModule, parentAndroidModule)
561 } else {
562 return false
563 }
564 })
565}
566
Yu Liud2a95952024-10-10 00:15:26 +0000567func (b *baseModuleContext) WalkDepsProxy(visit func(ModuleProxy, ModuleProxy) bool) {
Yu Liudd9ccb42024-10-07 17:07:44 +0000568 b.walkPath = []Module{ModuleProxy{blueprint.CreateModuleProxy(b.Module())}}
569 b.tagPath = []blueprint.DependencyTag{}
570 b.bp.WalkDepsProxy(func(child, parent blueprint.ModuleProxy) bool {
571 childAndroidModule := ModuleProxy{child}
572 parentAndroidModule := ModuleProxy{parent}
573 // record walkPath before visit
574 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
575 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
576 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
577 }
578 b.walkPath = append(b.walkPath, childAndroidModule)
579 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
580 return visit(childAndroidModule, parentAndroidModule)
581 })
582}
583
Colin Cross69452e12023-11-15 11:20:53 -0800584func (b *baseModuleContext) GetWalkPath() []Module {
Colin Crossb614cd42024-10-11 12:52:21 -0700585 return slices.Clone(b.walkPath)
Colin Cross69452e12023-11-15 11:20:53 -0800586}
587
588func (b *baseModuleContext) GetTagPath() []blueprint.DependencyTag {
589 return b.tagPath
590}
591
592func (b *baseModuleContext) VisitAllModuleVariants(visit func(Module)) {
593 b.bp.VisitAllModuleVariants(func(module blueprint.Module) {
594 visit(module.(Module))
595 })
596}
597
Yu Liub5275322024-11-13 18:40:43 +0000598func (b *baseModuleContext) VisitAllModuleVariantProxies(visit func(ModuleProxy)) {
599 b.bp.VisitAllModuleVariantProxies(visitProxyAdaptor(visit))
600}
601
Colin Cross69452e12023-11-15 11:20:53 -0800602func (b *baseModuleContext) PrimaryModule() Module {
603 return b.bp.PrimaryModule().(Module)
604}
605
606func (b *baseModuleContext) FinalModule() Module {
607 return b.bp.FinalModule().(Module)
608}
609
Yu Liu88ea9ff2024-11-07 19:19:42 +0000610func (b *baseModuleContext) IsFinalModule(module Module) bool {
611 return b.bp.IsFinalModule(module)
612}
613
Colin Cross69452e12023-11-15 11:20:53 -0800614// IsMetaDependencyTag returns true for cross-cutting metadata dependencies.
615func IsMetaDependencyTag(tag blueprint.DependencyTag) bool {
616 if tag == licenseKindTag {
617 return true
618 } else if tag == licensesTag {
619 return true
Jihoon Kanga3a05462024-04-05 00:36:44 +0000620 } else if tag == AcDepTag {
Colin Cross69452e12023-11-15 11:20:53 -0800621 return true
622 }
623 return false
624}
625
626// A regexp for removing boilerplate from BaseDependencyTag from the string representation of
627// a dependency tag.
628var tagCleaner = regexp.MustCompile(`\QBaseDependencyTag:{}\E(, )?`)
629
630// PrettyPrintTag returns string representation of the tag, but prefers
631// custom String() method if available.
632func PrettyPrintTag(tag blueprint.DependencyTag) string {
633 // Use tag's custom String() method if available.
634 if stringer, ok := tag.(fmt.Stringer); ok {
635 return stringer.String()
636 }
637
638 // Otherwise, get a default string representation of the tag's struct.
639 tagString := fmt.Sprintf("%T: %+v", tag, tag)
640
641 // Remove the boilerplate from BaseDependencyTag as it adds no value.
642 tagString = tagCleaner.ReplaceAllString(tagString, "")
643 return tagString
644}
645
646func (b *baseModuleContext) GetPathString(skipFirst bool) string {
647 sb := strings.Builder{}
648 tagPath := b.GetTagPath()
649 walkPath := b.GetWalkPath()
650 if !skipFirst {
651 sb.WriteString(walkPath[0].String())
652 }
653 for i, m := range walkPath[1:] {
654 sb.WriteString("\n")
655 sb.WriteString(fmt.Sprintf(" via tag %s\n", PrettyPrintTag(tagPath[i])))
656 sb.WriteString(fmt.Sprintf(" -> %s", m.String()))
657 }
658 return sb.String()
659}
Cole Faustbdd8aee2024-03-14 14:33:02 -0700660
Cole Faustfdbf5d42024-04-10 15:01:23 -0700661func (m *baseModuleContext) EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue {
662 return m.Module().ConfigurableEvaluator(m).EvaluateConfiguration(condition, property)
Cole Faustbdd8aee2024-03-14 14:33:02 -0700663}