blob: 8c0e4d2c8656691a590fbd7051f4b95a185404d3 [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
LaMont Jones34314b72024-01-18 18:27:35 +0000120 // GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
Colin Cross69452e12023-11-15 11:20:53 -0800121 // name, or nil if none exists. If there are multiple dependencies on the same module it returns
122 // the first DependencyTag.
123 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
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 {
260 if mp, isProxy := module.(ModuleProxy); isProxy {
261 return mp.module
262 }
263 return module
264}
265
266func (b *baseModuleContext) EqualModules(m1, m2 Module) bool {
267 return b.bp.EqualModules(getWrappedModule(m1), getWrappedModule(m2))
268}
269
Colin Cross69452e12023-11-15 11:20:53 -0800270func (b *baseModuleContext) OtherModuleName(m blueprint.Module) string {
Yu Liudd9ccb42024-10-07 17:07:44 +0000271 return b.bp.OtherModuleName(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800272}
Yu Liud3228ac2024-11-08 23:11:47 +0000273func (b *baseModuleContext) OtherModuleDir(m blueprint.Module) string {
274 return b.bp.OtherModuleDir(getWrappedModule(m))
275}
Colin Cross69452e12023-11-15 11:20:53 -0800276func (b *baseModuleContext) OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) {
Yu Liuac483e02024-11-11 22:29:30 +0000277 b.bp.OtherModuleErrorf(getWrappedModule(m), fmt, args...)
Colin Cross69452e12023-11-15 11:20:53 -0800278}
279func (b *baseModuleContext) OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag {
Yu Liudd9ccb42024-10-07 17:07:44 +0000280 return b.bp.OtherModuleDependencyTag(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800281}
282func (b *baseModuleContext) OtherModuleExists(name string) bool { return b.bp.OtherModuleExists(name) }
283func (b *baseModuleContext) OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool {
284 return b.bp.OtherModuleDependencyVariantExists(variations, name)
285}
286func (b *baseModuleContext) OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool {
287 return b.bp.OtherModuleFarDependencyVariantExists(variations, name)
288}
289func (b *baseModuleContext) OtherModuleReverseDependencyVariantExists(name string) bool {
290 return b.bp.OtherModuleReverseDependencyVariantExists(name)
291}
292func (b *baseModuleContext) OtherModuleType(m blueprint.Module) string {
Yu Liub1bfa9d2024-12-05 18:57:51 +0000293 return b.bp.OtherModuleType(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800294}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800295
296func (b *baseModuleContext) otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800297 return b.bp.OtherModuleProvider(m, provider)
298}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800299
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000300func (b *baseModuleContext) OtherModuleIsAutoGenerated(m blueprint.Module) bool {
301 return b.bp.OtherModuleIsAutoGenerated(m)
302}
303
Colin Cross3c0a83d2023-12-12 14:13:26 -0800304func (b *baseModuleContext) provider(provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800305 return b.bp.Provider(provider)
306}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800307
Colin Cross24c1cbe2023-12-21 23:42:56 +0000308func (b *baseModuleContext) setProvider(provider blueprint.AnyProviderKey, value any) {
Colin Cross69452e12023-11-15 11:20:53 -0800309 b.bp.SetProvider(provider, value)
310}
311
Yu Liud3228ac2024-11-08 23:11:47 +0000312func (b *baseModuleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) Module {
313 if module := b.bp.GetDirectDepWithTag(name, tag); module != nil {
314 return module.(Module)
315 }
316 return nil
Colin Cross69452e12023-11-15 11:20:53 -0800317}
318
319func (b *baseModuleContext) blueprintBaseModuleContext() blueprint.BaseModuleContext {
320 return b.bp
321}
322
Colin Cross69452e12023-11-15 11:20:53 -0800323func (b *baseModuleContext) AddMissingDependencies(deps []string) {
324 if deps != nil {
325 missingDeps := &b.Module().base().commonProperties.MissingDeps
326 *missingDeps = append(*missingDeps, deps...)
327 *missingDeps = FirstUniqueStrings(*missingDeps)
328 }
329}
330
331func (b *baseModuleContext) checkedMissingDeps() bool {
332 return b.Module().base().commonProperties.CheckedMissingDeps
333}
334
335func (b *baseModuleContext) getMissingDependencies() []string {
336 checked := &b.Module().base().commonProperties.CheckedMissingDeps
337 *checked = true
338 var missingDeps []string
339 missingDeps = append(missingDeps, b.Module().base().commonProperties.MissingDeps...)
340 missingDeps = append(missingDeps, b.bp.EarlyGetMissingDependencies()...)
341 missingDeps = FirstUniqueStrings(missingDeps)
342 return missingDeps
343}
344
345type AllowDisabledModuleDependency interface {
346 blueprint.DependencyTag
347 AllowDisabledModuleDependency(target Module) bool
Yu Liud2a95952024-10-10 00:15:26 +0000348 AllowDisabledModuleDependencyProxy(ctx OtherModuleProviderContext, target ModuleProxy) bool
Colin Cross69452e12023-11-15 11:20:53 -0800349}
350
Jiyong Park8bcf3c62024-03-18 18:37:10 +0900351type AlwaysAllowDisabledModuleDependencyTag struct{}
352
353func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependency(Module) bool {
354 return true
355}
356
Yu Liud2a95952024-10-10 00:15:26 +0000357func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependencyProxy(OtherModuleProviderContext, ModuleProxy) bool {
358 return true
359}
360
Colin Cross648daea2024-09-12 14:35:29 -0700361func (b *baseModuleContext) validateAndroidModule(module blueprint.Module, tag blueprint.DependencyTag, strict bool) Module {
Colin Cross69452e12023-11-15 11:20:53 -0800362 aModule, _ := module.(Module)
363
364 if !strict {
365 return aModule
366 }
367
368 if aModule == nil {
Colin Cross648daea2024-09-12 14:35:29 -0700369 panic(fmt.Errorf("module %q (%#v) not an android module", b.OtherModuleName(module), tag))
Colin Cross69452e12023-11-15 11:20:53 -0800370 }
371
Cole Fausta963b942024-04-11 17:43:00 -0700372 if !aModule.Enabled(b) {
Colin Cross69452e12023-11-15 11:20:53 -0800373 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependency(aModule) {
374 if b.Config().AllowMissingDependencies() {
375 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
376 } else {
377 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
378 }
379 }
380 return nil
381 }
382 return aModule
383}
384
Yu Liud2a95952024-10-10 00:15:26 +0000385func (b *baseModuleContext) validateAndroidModuleProxy(
386 module blueprint.ModuleProxy, tag blueprint.DependencyTag, strict bool) *ModuleProxy {
387 aModule := ModuleProxy{module: module}
388
389 if !strict {
390 return &aModule
391 }
392
Yu Liub5275322024-11-13 18:40:43 +0000393 if !OtherModuleProviderOrDefault(b, module, CommonModuleInfoKey).Enabled {
Yu Liud2a95952024-10-10 00:15:26 +0000394 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependencyProxy(b, aModule) {
395 if b.Config().AllowMissingDependencies() {
396 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
397 } else {
398 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
399 }
400 }
401 return nil
402 }
403
404 return &aModule
405}
406
Colin Cross69452e12023-11-15 11:20:53 -0800407type dep struct {
408 mod blueprint.Module
409 tag blueprint.DependencyTag
410}
411
412func (b *baseModuleContext) getDirectDepsInternal(name string, tag blueprint.DependencyTag) []dep {
413 var deps []dep
Colin Cross648daea2024-09-12 14:35:29 -0700414 b.VisitDirectDeps(func(module Module) {
415 if module.base().BaseModuleName() == name {
Colin Cross69452e12023-11-15 11:20:53 -0800416 returnedTag := b.bp.OtherModuleDependencyTag(module)
417 if tag == nil || returnedTag == tag {
418 deps = append(deps, dep{module, returnedTag})
419 }
420 }
421 })
422 return deps
423}
424
425func (b *baseModuleContext) getDirectDepInternal(name string, tag blueprint.DependencyTag) (blueprint.Module, blueprint.DependencyTag) {
426 deps := b.getDirectDepsInternal(name, tag)
427 if len(deps) == 1 {
428 return deps[0].mod, deps[0].tag
429 } else if len(deps) >= 2 {
430 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
431 name, b.ModuleName()))
432 } else {
433 return nil, nil
434 }
435}
436
437func (b *baseModuleContext) getDirectDepFirstTag(name string) (blueprint.Module, blueprint.DependencyTag) {
438 foundDeps := b.getDirectDepsInternal(name, nil)
439 deps := map[blueprint.Module]bool{}
440 for _, dep := range foundDeps {
441 deps[dep.mod] = true
442 }
443 if len(deps) == 1 {
444 return foundDeps[0].mod, foundDeps[0].tag
445 } else if len(deps) >= 2 {
446 // this could happen if two dependencies have the same name in different namespaces
447 // TODO(b/186554727): this should not occur if namespaces are handled within
448 // getDirectDepsInternal.
449 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
450 name, b.ModuleName()))
451 } else {
452 return nil, nil
453 }
454}
455
456func (b *baseModuleContext) GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module {
457 var deps []Module
Colin Cross648daea2024-09-12 14:35:29 -0700458 b.VisitDirectDeps(func(module Module) {
459 if b.bp.OtherModuleDependencyTag(module) == tag {
460 deps = append(deps, module)
Colin Cross69452e12023-11-15 11:20:53 -0800461 }
462 })
463 return deps
464}
465
Yu Liuf432c2e2024-12-17 00:09:15 +0000466func (b *baseModuleContext) GetDirectDepsProxyWithTag(tag blueprint.DependencyTag) []ModuleProxy {
467 var deps []ModuleProxy
468 b.VisitDirectDepsProxy(func(module ModuleProxy) {
469 if b.OtherModuleDependencyTag(module) == tag {
470 deps = append(deps, module)
471 }
472 })
473 return deps
474}
475
Colin Cross69452e12023-11-15 11:20:53 -0800476// GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
477// name, or nil if none exists. If there are multiple dependencies on the same module it returns the
478// first DependencyTag.
479func (b *baseModuleContext) GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) {
480 return b.getDirectDepFirstTag(name)
481}
482
Colin Cross69452e12023-11-15 11:20:53 -0800483func (b *baseModuleContext) VisitDirectDeps(visit func(Module)) {
484 b.bp.VisitDirectDeps(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700485 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800486 visit(aModule)
487 }
488 })
489}
490
Yu Liud3228ac2024-11-08 23:11:47 +0000491func (b *baseModuleContext) VisitDirectDepsProxy(visit func(ModuleProxy)) {
492 b.bp.VisitDirectDepsProxy(func(module blueprint.ModuleProxy) {
493 if aModule := b.validateAndroidModuleProxy(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
494 visit(*aModule)
495 }
Colin Cross648daea2024-09-12 14:35:29 -0700496 })
497}
498
Yu Liud2a95952024-10-10 00:15:26 +0000499func (b *baseModuleContext) VisitDirectDepsProxyAllowDisabled(visit func(proxy ModuleProxy)) {
Yu Liud3228ac2024-11-08 23:11:47 +0000500 b.bp.VisitDirectDepsProxy(visitProxyAdaptor(visit))
Yu Liudd9ccb42024-10-07 17:07:44 +0000501}
502
Colin Cross69452e12023-11-15 11:20:53 -0800503func (b *baseModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
504 b.bp.VisitDirectDeps(func(module blueprint.Module) {
505 if b.bp.OtherModuleDependencyTag(module) == tag {
Yu Liud2a95952024-10-10 00:15:26 +0000506 if aModule := b.validateAndroidModule(module, tag, b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800507 visit(aModule)
508 }
509 }
510 })
511}
512
Yu Liud2a95952024-10-10 00:15:26 +0000513func (b *baseModuleContext) VisitDirectDepsProxyWithTag(tag blueprint.DependencyTag, visit func(proxy ModuleProxy)) {
514 b.bp.VisitDirectDepsProxy(func(module blueprint.ModuleProxy) {
515 if b.bp.OtherModuleDependencyTag(module) == tag {
516 if aModule := b.validateAndroidModuleProxy(module, tag, b.strictVisitDeps); aModule != nil {
517 visit(*aModule)
518 }
519 }
520 })
521}
522
Colin Cross69452e12023-11-15 11:20:53 -0800523func (b *baseModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
524 b.bp.VisitDirectDepsIf(
525 // pred
526 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700527 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800528 return pred(aModule)
529 } else {
530 return false
531 }
532 },
533 // visit
534 func(module blueprint.Module) {
535 visit(module.(Module))
536 })
537}
538
539func (b *baseModuleContext) VisitDepsDepthFirst(visit func(Module)) {
540 b.bp.VisitDepsDepthFirst(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700541 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800542 visit(aModule)
543 }
544 })
545}
546
547func (b *baseModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
548 b.bp.VisitDepsDepthFirstIf(
549 // pred
550 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700551 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800552 return pred(aModule)
553 } else {
554 return false
555 }
556 },
557 // visit
558 func(module blueprint.Module) {
559 visit(module.(Module))
560 })
561}
562
Colin Cross69452e12023-11-15 11:20:53 -0800563func (b *baseModuleContext) WalkDeps(visit func(Module, Module) bool) {
564 b.walkPath = []Module{b.Module()}
565 b.tagPath = []blueprint.DependencyTag{}
566 b.bp.WalkDeps(func(child, parent blueprint.Module) bool {
567 childAndroidModule, _ := child.(Module)
568 parentAndroidModule, _ := parent.(Module)
569 if childAndroidModule != nil && parentAndroidModule != nil {
570 // record walkPath before visit
571 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
572 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
573 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
574 }
575 b.walkPath = append(b.walkPath, childAndroidModule)
576 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
577 return visit(childAndroidModule, parentAndroidModule)
578 } else {
579 return false
580 }
581 })
582}
583
Yu Liud2a95952024-10-10 00:15:26 +0000584func (b *baseModuleContext) WalkDepsProxy(visit func(ModuleProxy, ModuleProxy) bool) {
Yu Liudd9ccb42024-10-07 17:07:44 +0000585 b.walkPath = []Module{ModuleProxy{blueprint.CreateModuleProxy(b.Module())}}
586 b.tagPath = []blueprint.DependencyTag{}
587 b.bp.WalkDepsProxy(func(child, parent blueprint.ModuleProxy) bool {
588 childAndroidModule := ModuleProxy{child}
589 parentAndroidModule := ModuleProxy{parent}
590 // record walkPath before visit
591 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
592 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
593 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
594 }
595 b.walkPath = append(b.walkPath, childAndroidModule)
596 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
597 return visit(childAndroidModule, parentAndroidModule)
598 })
599}
600
Colin Cross69452e12023-11-15 11:20:53 -0800601func (b *baseModuleContext) GetWalkPath() []Module {
Colin Crossb614cd42024-10-11 12:52:21 -0700602 return slices.Clone(b.walkPath)
Colin Cross69452e12023-11-15 11:20:53 -0800603}
604
605func (b *baseModuleContext) GetTagPath() []blueprint.DependencyTag {
606 return b.tagPath
607}
608
609func (b *baseModuleContext) VisitAllModuleVariants(visit func(Module)) {
610 b.bp.VisitAllModuleVariants(func(module blueprint.Module) {
611 visit(module.(Module))
612 })
613}
614
Yu Liub5275322024-11-13 18:40:43 +0000615func (b *baseModuleContext) VisitAllModuleVariantProxies(visit func(ModuleProxy)) {
616 b.bp.VisitAllModuleVariantProxies(visitProxyAdaptor(visit))
617}
618
Colin Cross69452e12023-11-15 11:20:53 -0800619func (b *baseModuleContext) PrimaryModule() Module {
620 return b.bp.PrimaryModule().(Module)
621}
622
623func (b *baseModuleContext) FinalModule() Module {
624 return b.bp.FinalModule().(Module)
625}
626
Yu Liu88ea9ff2024-11-07 19:19:42 +0000627func (b *baseModuleContext) IsFinalModule(module Module) bool {
628 return b.bp.IsFinalModule(module)
629}
630
Colin Cross69452e12023-11-15 11:20:53 -0800631// IsMetaDependencyTag returns true for cross-cutting metadata dependencies.
632func IsMetaDependencyTag(tag blueprint.DependencyTag) bool {
633 if tag == licenseKindTag {
634 return true
635 } else if tag == licensesTag {
636 return true
Jihoon Kanga3a05462024-04-05 00:36:44 +0000637 } else if tag == AcDepTag {
Colin Cross69452e12023-11-15 11:20:53 -0800638 return true
639 }
640 return false
641}
642
643// A regexp for removing boilerplate from BaseDependencyTag from the string representation of
644// a dependency tag.
645var tagCleaner = regexp.MustCompile(`\QBaseDependencyTag:{}\E(, )?`)
646
647// PrettyPrintTag returns string representation of the tag, but prefers
648// custom String() method if available.
649func PrettyPrintTag(tag blueprint.DependencyTag) string {
650 // Use tag's custom String() method if available.
651 if stringer, ok := tag.(fmt.Stringer); ok {
652 return stringer.String()
653 }
654
655 // Otherwise, get a default string representation of the tag's struct.
656 tagString := fmt.Sprintf("%T: %+v", tag, tag)
657
658 // Remove the boilerplate from BaseDependencyTag as it adds no value.
659 tagString = tagCleaner.ReplaceAllString(tagString, "")
660 return tagString
661}
662
663func (b *baseModuleContext) GetPathString(skipFirst bool) string {
664 sb := strings.Builder{}
665 tagPath := b.GetTagPath()
666 walkPath := b.GetWalkPath()
667 if !skipFirst {
668 sb.WriteString(walkPath[0].String())
669 }
670 for i, m := range walkPath[1:] {
671 sb.WriteString("\n")
672 sb.WriteString(fmt.Sprintf(" via tag %s\n", PrettyPrintTag(tagPath[i])))
673 sb.WriteString(fmt.Sprintf(" -> %s", m.String()))
674 }
675 return sb.String()
676}
Cole Faustbdd8aee2024-03-14 14:33:02 -0700677
Cole Faustfdbf5d42024-04-10 15:01:23 -0700678func (m *baseModuleContext) EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue {
679 return m.Module().ConfigurableEvaluator(m).EvaluateConfiguration(condition, property)
Cole Faustbdd8aee2024-03-14 14:33:02 -0700680}