blob: d8558d0e539e8f6d8a8baedfe08ce93f9281c8ae [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
Yu Liu3ae96652024-12-17 22:27:38 +0000120 GetDirectDepProxyWithTag(name string, tag blueprint.DependencyTag) *ModuleProxy
121
Colin Cross648daea2024-09-12 14:35:29 -0700122 // VisitDirectDeps calls visit for each direct dependency. If there are multiple
123 // direct dependencies on the same module visit will be called multiple times on that module
124 // and OtherModuleDependencyTag will return a different tag for each. It raises an error if any of the
125 // dependencies are disabled.
126 //
127 // The Module passed to the visit function should not be retained outside of the visit
128 // function, it may be invalidated by future mutators.
129 VisitDirectDeps(visit func(Module))
130
Yu Liud3228ac2024-11-08 23:11:47 +0000131 // VisitDirectDepsProxy calls visit for each direct dependency. If there are multiple
Colin Cross69452e12023-11-15 11:20:53 -0800132 // direct dependencies on the same module visit will be called multiple times on that module
Yu Liud3228ac2024-11-08 23:11:47 +0000133 // and OtherModuleDependencyTag will return a different tag for each. It raises an error if any of the
134 // dependencies are disabled.
Colin Cross69452e12023-11-15 11:20:53 -0800135 //
Yu Liud3228ac2024-11-08 23:11:47 +0000136 // The ModuleProxy passed to the visit function should not be retained outside of the visit
Colin Cross69452e12023-11-15 11:20:53 -0800137 // function, it may be invalidated by future mutators.
Yu Liud3228ac2024-11-08 23:11:47 +0000138 VisitDirectDepsProxy(visit func(proxy ModuleProxy))
Colin Cross69452e12023-11-15 11:20:53 -0800139
Yu Liudd9ccb42024-10-07 17:07:44 +0000140 // VisitDirectDepsProxyAllowDisabled calls visit for each direct dependency. If there are
141 // multiple direct dependencies on the same module visit will be called multiple times on
142 // that module and OtherModuleDependencyTag will return a different tag for each.
143 //
Yu Liud2a95952024-10-10 00:15:26 +0000144 // 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 +0000145 // invalidated by future mutators.
Yu Liud2a95952024-10-10 00:15:26 +0000146 VisitDirectDepsProxyAllowDisabled(visit func(proxy ModuleProxy))
Yu Liudd9ccb42024-10-07 17:07:44 +0000147
Colin Cross69452e12023-11-15 11:20:53 -0800148 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
149
Yu Liud2a95952024-10-10 00:15:26 +0000150 VisitDirectDepsProxyWithTag(tag blueprint.DependencyTag, visit func(proxy ModuleProxy))
151
Colin Cross69452e12023-11-15 11:20:53 -0800152 // VisitDirectDepsIf calls pred for each direct dependency, and if pred returns true calls visit. If there are
153 // multiple direct dependencies on the same module pred and visit will be called multiple times on that module and
154 // OtherModuleDependencyTag will return a different tag for each. It skips any
155 // dependencies that are not an android.Module.
156 //
157 // The Module passed to the visit function should not be retained outside of the visit function, it may be
158 // invalidated by future mutators.
159 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
160 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
161 VisitDepsDepthFirst(visit func(Module))
162 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
163 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
164
165 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
166 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
167 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
168 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
169 // any dependencies that are not an android.Module.
170 //
171 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
172 // invalidated by future mutators.
173 WalkDeps(visit func(child, parent Module) bool)
174
Yu Liudd9ccb42024-10-07 17:07:44 +0000175 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
176 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
177 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
178 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
179 // any dependencies that are not an android.Module.
180 //
181 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
182 // invalidated by future mutators.
Yu Liud2a95952024-10-10 00:15:26 +0000183 WalkDepsProxy(visit func(child, parent ModuleProxy) bool)
Yu Liudd9ccb42024-10-07 17:07:44 +0000184
Colin Cross69452e12023-11-15 11:20:53 -0800185 // GetWalkPath is supposed to be called in visit function passed in WalkDeps()
186 // and returns a top-down dependency path from a start module to current child module.
187 GetWalkPath() []Module
188
189 // PrimaryModule returns the first variant of the current module. Variants of a module are always visited in
190 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from the
191 // Module returned by PrimaryModule without data races. This can be used to perform singleton actions that are
192 // only done once for all variants of a module.
193 PrimaryModule() Module
194
195 // FinalModule returns the last variant of the current module. Variants of a module are always visited in
196 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from all
197 // variants using VisitAllModuleVariants if the current module == FinalModule(). This can be used to perform
198 // singleton actions that are only done once for all variants of a module.
199 FinalModule() Module
200
Yu Liu88ea9ff2024-11-07 19:19:42 +0000201 // IsFinalModule returns if the current module is the last variant. Variants of a module are always visited in
202 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from all
203 // variants using VisitAllModuleVariants if the current module is the last one. This can be used to perform
204 // singleton actions that are only done once for all variants of a module.
205 IsFinalModule(module Module) bool
206
Colin Cross69452e12023-11-15 11:20:53 -0800207 // VisitAllModuleVariants calls visit for each variant of the current module. Variants of a module are always
208 // 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 +0000209 // 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 -0800210 // data modified by the current mutator.
211 VisitAllModuleVariants(visit func(Module))
212
Yu Liub5275322024-11-13 18:40:43 +0000213 // VisitAllModuleVariantProxies calls visit for each variant of the current module. Variants of a module are always
214 // visited in order by mutators and GenerateBuildActions, so the data created by the current mutator can be read
215 // from all variants if the current module is the last one. Otherwise, care must be taken to not access any
216 // data modified by the current mutator.
217 VisitAllModuleVariantProxies(visit func(proxy ModuleProxy))
218
Colin Cross69452e12023-11-15 11:20:53 -0800219 // GetTagPath is supposed to be called in visit function passed in WalkDeps()
220 // and returns a top-down dependency tags path from a start module to current child module.
221 // It has one less entry than GetWalkPath() as it contains the dependency tags that
222 // exist between each adjacent pair of modules in the GetWalkPath().
223 // GetTagPath()[i] is the tag between GetWalkPath()[i] and GetWalkPath()[i+1]
224 GetTagPath() []blueprint.DependencyTag
225
226 // GetPathString is supposed to be called in visit function passed in WalkDeps()
227 // and returns a multi-line string showing the modules and dependency tags
228 // among them along the top-down dependency path from a start module to current child module.
229 // skipFirst when set to true, the output doesn't include the start module,
230 // which is already printed when this function is used along with ModuleErrorf().
231 GetPathString(skipFirst bool) string
232
233 AddMissingDependencies(missingDeps []string)
234
235 // getMissingDependencies returns the list of missing dependencies.
236 // Calling this function prevents adding new dependencies.
237 getMissingDependencies() []string
Cole Faustbdd8aee2024-03-14 14:33:02 -0700238
239 // EvaluateConfiguration makes ModuleContext a valid proptools.ConfigurableEvaluator, so this context
240 // can be used to evaluate the final value of Configurable properties.
Cole Faustfdbf5d42024-04-10 15:01:23 -0700241 EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue
Colin Cross69452e12023-11-15 11:20:53 -0800242}
243
244type baseModuleContext struct {
245 bp blueprint.BaseModuleContext
246 earlyModuleContext
Colin Cross1d3d9f12024-01-18 14:30:22 -0800247 archModuleContext
Colin Cross69452e12023-11-15 11:20:53 -0800248
249 walkPath []Module
250 tagPath []blueprint.DependencyTag
251
252 strictVisitDeps bool // If true, enforce that all dependencies are enabled
253
Colin Cross69452e12023-11-15 11:20:53 -0800254}
255
Yu Liudd9ccb42024-10-07 17:07:44 +0000256func getWrappedModule(module blueprint.Module) blueprint.Module {
257 if mp, isProxy := module.(ModuleProxy); isProxy {
258 return mp.module
259 }
260 return module
261}
262
263func (b *baseModuleContext) EqualModules(m1, m2 Module) bool {
264 return b.bp.EqualModules(getWrappedModule(m1), getWrappedModule(m2))
265}
266
Colin Cross69452e12023-11-15 11:20:53 -0800267func (b *baseModuleContext) OtherModuleName(m blueprint.Module) string {
Yu Liudd9ccb42024-10-07 17:07:44 +0000268 return b.bp.OtherModuleName(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800269}
Yu Liud3228ac2024-11-08 23:11:47 +0000270func (b *baseModuleContext) OtherModuleDir(m blueprint.Module) string {
271 return b.bp.OtherModuleDir(getWrappedModule(m))
272}
Colin Cross69452e12023-11-15 11:20:53 -0800273func (b *baseModuleContext) OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) {
Yu Liuac483e02024-11-11 22:29:30 +0000274 b.bp.OtherModuleErrorf(getWrappedModule(m), fmt, args...)
Colin Cross69452e12023-11-15 11:20:53 -0800275}
276func (b *baseModuleContext) OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag {
Yu Liudd9ccb42024-10-07 17:07:44 +0000277 return b.bp.OtherModuleDependencyTag(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800278}
279func (b *baseModuleContext) OtherModuleExists(name string) bool { return b.bp.OtherModuleExists(name) }
280func (b *baseModuleContext) OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool {
281 return b.bp.OtherModuleDependencyVariantExists(variations, name)
282}
283func (b *baseModuleContext) OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool {
284 return b.bp.OtherModuleFarDependencyVariantExists(variations, name)
285}
286func (b *baseModuleContext) OtherModuleReverseDependencyVariantExists(name string) bool {
287 return b.bp.OtherModuleReverseDependencyVariantExists(name)
288}
289func (b *baseModuleContext) OtherModuleType(m blueprint.Module) string {
Yu Liub1bfa9d2024-12-05 18:57:51 +0000290 return b.bp.OtherModuleType(getWrappedModule(m))
Colin Cross69452e12023-11-15 11:20:53 -0800291}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800292
293func (b *baseModuleContext) otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800294 return b.bp.OtherModuleProvider(m, provider)
295}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800296
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000297func (b *baseModuleContext) OtherModuleIsAutoGenerated(m blueprint.Module) bool {
298 return b.bp.OtherModuleIsAutoGenerated(m)
299}
300
Colin Cross3c0a83d2023-12-12 14:13:26 -0800301func (b *baseModuleContext) provider(provider blueprint.AnyProviderKey) (any, bool) {
Colin Cross69452e12023-11-15 11:20:53 -0800302 return b.bp.Provider(provider)
303}
Colin Cross3c0a83d2023-12-12 14:13:26 -0800304
Colin Cross24c1cbe2023-12-21 23:42:56 +0000305func (b *baseModuleContext) setProvider(provider blueprint.AnyProviderKey, value any) {
Colin Cross69452e12023-11-15 11:20:53 -0800306 b.bp.SetProvider(provider, value)
307}
308
Yu Liud3228ac2024-11-08 23:11:47 +0000309func (b *baseModuleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) Module {
310 if module := b.bp.GetDirectDepWithTag(name, tag); module != nil {
311 return module.(Module)
312 }
313 return nil
Colin Cross69452e12023-11-15 11:20:53 -0800314}
315
Yu Liu3ae96652024-12-17 22:27:38 +0000316func (b *baseModuleContext) GetDirectDepProxyWithTag(name string, tag blueprint.DependencyTag) *ModuleProxy {
317 if module := b.bp.GetDirectDepProxyWithTag(name, tag); module != nil {
318 return &ModuleProxy{*module}
319 }
320 return nil
321}
322
Colin Cross69452e12023-11-15 11:20:53 -0800323func (b *baseModuleContext) blueprintBaseModuleContext() blueprint.BaseModuleContext {
324 return b.bp
325}
326
Colin Cross69452e12023-11-15 11:20:53 -0800327func (b *baseModuleContext) AddMissingDependencies(deps []string) {
328 if deps != nil {
329 missingDeps := &b.Module().base().commonProperties.MissingDeps
330 *missingDeps = append(*missingDeps, deps...)
331 *missingDeps = FirstUniqueStrings(*missingDeps)
332 }
333}
334
335func (b *baseModuleContext) checkedMissingDeps() bool {
336 return b.Module().base().commonProperties.CheckedMissingDeps
337}
338
339func (b *baseModuleContext) getMissingDependencies() []string {
340 checked := &b.Module().base().commonProperties.CheckedMissingDeps
341 *checked = true
342 var missingDeps []string
343 missingDeps = append(missingDeps, b.Module().base().commonProperties.MissingDeps...)
344 missingDeps = append(missingDeps, b.bp.EarlyGetMissingDependencies()...)
345 missingDeps = FirstUniqueStrings(missingDeps)
346 return missingDeps
347}
348
349type AllowDisabledModuleDependency interface {
350 blueprint.DependencyTag
351 AllowDisabledModuleDependency(target Module) bool
Yu Liud2a95952024-10-10 00:15:26 +0000352 AllowDisabledModuleDependencyProxy(ctx OtherModuleProviderContext, target ModuleProxy) bool
Colin Cross69452e12023-11-15 11:20:53 -0800353}
354
Jiyong Park8bcf3c62024-03-18 18:37:10 +0900355type AlwaysAllowDisabledModuleDependencyTag struct{}
356
357func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependency(Module) bool {
358 return true
359}
360
Yu Liud2a95952024-10-10 00:15:26 +0000361func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependencyProxy(OtherModuleProviderContext, ModuleProxy) bool {
362 return true
363}
364
Colin Cross648daea2024-09-12 14:35:29 -0700365func (b *baseModuleContext) validateAndroidModule(module blueprint.Module, tag blueprint.DependencyTag, strict bool) Module {
Colin Cross69452e12023-11-15 11:20:53 -0800366 aModule, _ := module.(Module)
367
368 if !strict {
369 return aModule
370 }
371
372 if aModule == nil {
Colin Cross648daea2024-09-12 14:35:29 -0700373 panic(fmt.Errorf("module %q (%#v) not an android module", b.OtherModuleName(module), tag))
Colin Cross69452e12023-11-15 11:20:53 -0800374 }
375
Cole Fausta963b942024-04-11 17:43:00 -0700376 if !aModule.Enabled(b) {
Colin Cross69452e12023-11-15 11:20:53 -0800377 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependency(aModule) {
378 if b.Config().AllowMissingDependencies() {
379 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
380 } else {
381 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
382 }
383 }
384 return nil
385 }
386 return aModule
387}
388
Yu Liud2a95952024-10-10 00:15:26 +0000389func (b *baseModuleContext) validateAndroidModuleProxy(
390 module blueprint.ModuleProxy, tag blueprint.DependencyTag, strict bool) *ModuleProxy {
391 aModule := ModuleProxy{module: module}
392
393 if !strict {
394 return &aModule
395 }
396
Yu Liub5275322024-11-13 18:40:43 +0000397 if !OtherModuleProviderOrDefault(b, module, CommonModuleInfoKey).Enabled {
Yu Liud2a95952024-10-10 00:15:26 +0000398 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependencyProxy(b, aModule) {
399 if b.Config().AllowMissingDependencies() {
400 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
401 } else {
402 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
403 }
404 }
405 return nil
406 }
407
408 return &aModule
409}
410
Yu Liu3ae96652024-12-17 22:27:38 +0000411func (b *baseModuleContext) getDirectDepsInternal(name string, tag blueprint.DependencyTag) []Module {
412 var deps []Module
Colin Cross648daea2024-09-12 14:35:29 -0700413 b.VisitDirectDeps(func(module Module) {
414 if module.base().BaseModuleName() == name {
Colin Cross69452e12023-11-15 11:20:53 -0800415 returnedTag := b.bp.OtherModuleDependencyTag(module)
416 if tag == nil || returnedTag == tag {
Yu Liu3ae96652024-12-17 22:27:38 +0000417 deps = append(deps, module)
Colin Cross69452e12023-11-15 11:20:53 -0800418 }
419 }
420 })
421 return deps
422}
423
Yu Liu3ae96652024-12-17 22:27:38 +0000424func (b *baseModuleContext) getDirectDepsProxyInternal(name string, tag blueprint.DependencyTag) []ModuleProxy {
425 var deps []ModuleProxy
426 b.VisitDirectDepsProxy(func(module ModuleProxy) {
427 if OtherModuleProviderOrDefault(b, module, CommonModuleInfoKey).BaseModuleName == name {
428 returnedTag := b.OtherModuleDependencyTag(module)
429 if tag == nil || returnedTag == tag {
430 deps = append(deps, module)
431 }
432 }
433 })
434 return deps
Colin Cross69452e12023-11-15 11:20:53 -0800435}
436
Colin Cross69452e12023-11-15 11:20:53 -0800437func (b *baseModuleContext) GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module {
438 var deps []Module
Colin Cross648daea2024-09-12 14:35:29 -0700439 b.VisitDirectDeps(func(module Module) {
440 if b.bp.OtherModuleDependencyTag(module) == tag {
441 deps = append(deps, module)
Colin Cross69452e12023-11-15 11:20:53 -0800442 }
443 })
444 return deps
445}
446
Yu Liuf432c2e2024-12-17 00:09:15 +0000447func (b *baseModuleContext) GetDirectDepsProxyWithTag(tag blueprint.DependencyTag) []ModuleProxy {
448 var deps []ModuleProxy
449 b.VisitDirectDepsProxy(func(module ModuleProxy) {
450 if b.OtherModuleDependencyTag(module) == tag {
451 deps = append(deps, module)
452 }
453 })
454 return deps
455}
456
Colin Cross69452e12023-11-15 11:20:53 -0800457func (b *baseModuleContext) VisitDirectDeps(visit func(Module)) {
458 b.bp.VisitDirectDeps(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700459 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800460 visit(aModule)
461 }
462 })
463}
464
Yu Liud3228ac2024-11-08 23:11:47 +0000465func (b *baseModuleContext) VisitDirectDepsProxy(visit func(ModuleProxy)) {
466 b.bp.VisitDirectDepsProxy(func(module blueprint.ModuleProxy) {
467 if aModule := b.validateAndroidModuleProxy(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
468 visit(*aModule)
469 }
Colin Cross648daea2024-09-12 14:35:29 -0700470 })
471}
472
Yu Liud2a95952024-10-10 00:15:26 +0000473func (b *baseModuleContext) VisitDirectDepsProxyAllowDisabled(visit func(proxy ModuleProxy)) {
Yu Liud3228ac2024-11-08 23:11:47 +0000474 b.bp.VisitDirectDepsProxy(visitProxyAdaptor(visit))
Yu Liudd9ccb42024-10-07 17:07:44 +0000475}
476
Colin Cross69452e12023-11-15 11:20:53 -0800477func (b *baseModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
478 b.bp.VisitDirectDeps(func(module blueprint.Module) {
479 if b.bp.OtherModuleDependencyTag(module) == tag {
Yu Liud2a95952024-10-10 00:15:26 +0000480 if aModule := b.validateAndroidModule(module, tag, b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800481 visit(aModule)
482 }
483 }
484 })
485}
486
Yu Liud2a95952024-10-10 00:15:26 +0000487func (b *baseModuleContext) VisitDirectDepsProxyWithTag(tag blueprint.DependencyTag, visit func(proxy ModuleProxy)) {
488 b.bp.VisitDirectDepsProxy(func(module blueprint.ModuleProxy) {
489 if b.bp.OtherModuleDependencyTag(module) == tag {
490 if aModule := b.validateAndroidModuleProxy(module, tag, b.strictVisitDeps); aModule != nil {
491 visit(*aModule)
492 }
493 }
494 })
495}
496
Colin Cross69452e12023-11-15 11:20:53 -0800497func (b *baseModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
498 b.bp.VisitDirectDepsIf(
499 // pred
500 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700501 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800502 return pred(aModule)
503 } else {
504 return false
505 }
506 },
507 // visit
508 func(module blueprint.Module) {
509 visit(module.(Module))
510 })
511}
512
513func (b *baseModuleContext) VisitDepsDepthFirst(visit func(Module)) {
514 b.bp.VisitDepsDepthFirst(func(module blueprint.Module) {
Colin Cross648daea2024-09-12 14:35:29 -0700515 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800516 visit(aModule)
517 }
518 })
519}
520
521func (b *baseModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
522 b.bp.VisitDepsDepthFirstIf(
523 // pred
524 func(module blueprint.Module) bool {
Colin Cross648daea2024-09-12 14:35:29 -0700525 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Cross69452e12023-11-15 11:20:53 -0800526 return pred(aModule)
527 } else {
528 return false
529 }
530 },
531 // visit
532 func(module blueprint.Module) {
533 visit(module.(Module))
534 })
535}
536
Colin Cross69452e12023-11-15 11:20:53 -0800537func (b *baseModuleContext) WalkDeps(visit func(Module, Module) bool) {
538 b.walkPath = []Module{b.Module()}
539 b.tagPath = []blueprint.DependencyTag{}
540 b.bp.WalkDeps(func(child, parent blueprint.Module) bool {
541 childAndroidModule, _ := child.(Module)
542 parentAndroidModule, _ := parent.(Module)
543 if childAndroidModule != nil && parentAndroidModule != nil {
544 // record walkPath before visit
545 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
546 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
547 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
548 }
549 b.walkPath = append(b.walkPath, childAndroidModule)
550 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
551 return visit(childAndroidModule, parentAndroidModule)
552 } else {
553 return false
554 }
555 })
556}
557
Yu Liud2a95952024-10-10 00:15:26 +0000558func (b *baseModuleContext) WalkDepsProxy(visit func(ModuleProxy, ModuleProxy) bool) {
Yu Liudd9ccb42024-10-07 17:07:44 +0000559 b.walkPath = []Module{ModuleProxy{blueprint.CreateModuleProxy(b.Module())}}
560 b.tagPath = []blueprint.DependencyTag{}
561 b.bp.WalkDepsProxy(func(child, parent blueprint.ModuleProxy) bool {
562 childAndroidModule := ModuleProxy{child}
563 parentAndroidModule := ModuleProxy{parent}
564 // record walkPath before visit
565 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
566 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
567 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
568 }
569 b.walkPath = append(b.walkPath, childAndroidModule)
570 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
571 return visit(childAndroidModule, parentAndroidModule)
572 })
573}
574
Colin Cross69452e12023-11-15 11:20:53 -0800575func (b *baseModuleContext) GetWalkPath() []Module {
Colin Crossb614cd42024-10-11 12:52:21 -0700576 return slices.Clone(b.walkPath)
Colin Cross69452e12023-11-15 11:20:53 -0800577}
578
579func (b *baseModuleContext) GetTagPath() []blueprint.DependencyTag {
580 return b.tagPath
581}
582
583func (b *baseModuleContext) VisitAllModuleVariants(visit func(Module)) {
584 b.bp.VisitAllModuleVariants(func(module blueprint.Module) {
585 visit(module.(Module))
586 })
587}
588
Yu Liub5275322024-11-13 18:40:43 +0000589func (b *baseModuleContext) VisitAllModuleVariantProxies(visit func(ModuleProxy)) {
590 b.bp.VisitAllModuleVariantProxies(visitProxyAdaptor(visit))
591}
592
Colin Cross69452e12023-11-15 11:20:53 -0800593func (b *baseModuleContext) PrimaryModule() Module {
594 return b.bp.PrimaryModule().(Module)
595}
596
597func (b *baseModuleContext) FinalModule() Module {
598 return b.bp.FinalModule().(Module)
599}
600
Yu Liu88ea9ff2024-11-07 19:19:42 +0000601func (b *baseModuleContext) IsFinalModule(module Module) bool {
602 return b.bp.IsFinalModule(module)
603}
604
Colin Cross69452e12023-11-15 11:20:53 -0800605// IsMetaDependencyTag returns true for cross-cutting metadata dependencies.
606func IsMetaDependencyTag(tag blueprint.DependencyTag) bool {
607 if tag == licenseKindTag {
608 return true
609 } else if tag == licensesTag {
610 return true
Jihoon Kanga3a05462024-04-05 00:36:44 +0000611 } else if tag == AcDepTag {
Colin Cross69452e12023-11-15 11:20:53 -0800612 return true
613 }
614 return false
615}
616
617// A regexp for removing boilerplate from BaseDependencyTag from the string representation of
618// a dependency tag.
619var tagCleaner = regexp.MustCompile(`\QBaseDependencyTag:{}\E(, )?`)
620
621// PrettyPrintTag returns string representation of the tag, but prefers
622// custom String() method if available.
623func PrettyPrintTag(tag blueprint.DependencyTag) string {
624 // Use tag's custom String() method if available.
625 if stringer, ok := tag.(fmt.Stringer); ok {
626 return stringer.String()
627 }
628
629 // Otherwise, get a default string representation of the tag's struct.
630 tagString := fmt.Sprintf("%T: %+v", tag, tag)
631
632 // Remove the boilerplate from BaseDependencyTag as it adds no value.
633 tagString = tagCleaner.ReplaceAllString(tagString, "")
634 return tagString
635}
636
637func (b *baseModuleContext) GetPathString(skipFirst bool) string {
638 sb := strings.Builder{}
639 tagPath := b.GetTagPath()
640 walkPath := b.GetWalkPath()
641 if !skipFirst {
642 sb.WriteString(walkPath[0].String())
643 }
644 for i, m := range walkPath[1:] {
645 sb.WriteString("\n")
646 sb.WriteString(fmt.Sprintf(" via tag %s\n", PrettyPrintTag(tagPath[i])))
647 sb.WriteString(fmt.Sprintf(" -> %s", m.String()))
648 }
649 return sb.String()
650}
Cole Faustbdd8aee2024-03-14 14:33:02 -0700651
Cole Faustfdbf5d42024-04-10 15:01:23 -0700652func (m *baseModuleContext) EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue {
653 return m.Module().ConfigurableEvaluator(m).EvaluateConfiguration(condition, property)
Cole Faustbdd8aee2024-03-14 14:33:02 -0700654}