Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 19 | "regexp" |
| 20 | "strings" |
Cole Faust | bdd8aee | 2024-03-14 14:33:02 -0700 | [diff] [blame] | 21 | |
| 22 | "github.com/google/blueprint" |
Cole Faust | fdbf5d4 | 2024-04-10 15:01:23 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | // BaseModuleContext is the same as blueprint.BaseModuleContext except that Config() returns |
| 27 | // a Config instead of an interface{}, and some methods have been wrapped to use an android.Module |
| 28 | // instead of a blueprint.Module, plus some extra methods that return Android-specific information |
| 29 | // about the current module. |
| 30 | type BaseModuleContext interface { |
Colin Cross | 1d3d9f1 | 2024-01-18 14:30:22 -0800 | [diff] [blame] | 31 | ArchModuleContext |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 32 | EarlyModuleContext |
| 33 | |
| 34 | blueprintBaseModuleContext() blueprint.BaseModuleContext |
| 35 | |
Yu Liu | dd9ccb4 | 2024-10-07 17:07:44 +0000 | [diff] [blame] | 36 | EqualModules(m1, m2 Module) bool |
| 37 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 38 | // OtherModuleName returns the name of another Module. See BaseModuleContext.ModuleName for more information. |
| 39 | // It is intended for use inside the visit functions of Visit* and WalkDeps. |
| 40 | OtherModuleName(m blueprint.Module) string |
| 41 | |
| 42 | // OtherModuleDir returns the directory of another Module. See BaseModuleContext.ModuleDir for more information. |
| 43 | // It is intended for use inside the visit functions of Visit* and WalkDeps. |
| 44 | OtherModuleDir(m blueprint.Module) string |
| 45 | |
| 46 | // OtherModuleErrorf reports an error on another Module. See BaseModuleContext.ModuleErrorf for more information. |
| 47 | // It is intended for use inside the visit functions of Visit* and WalkDeps. |
| 48 | OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) |
| 49 | |
| 50 | // OtherModuleDependencyTag returns the dependency tag used to depend on a module, or nil if there is no dependency |
| 51 | // on the module. When called inside a Visit* method with current module being visited, and there are multiple |
| 52 | // dependencies on the module being visited, it returns the dependency tag used for the current dependency. |
| 53 | OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag |
| 54 | |
| 55 | // OtherModuleExists returns true if a module with the specified name exists, as determined by the NameInterface |
| 56 | // passed to Context.SetNameInterface, or SimpleNameInterface if it was not called. |
| 57 | OtherModuleExists(name string) bool |
| 58 | |
| 59 | // OtherModuleDependencyVariantExists returns true if a module with the |
| 60 | // specified name and variant exists. The variant must match the given |
| 61 | // variations. It must also match all the non-local variations of the current |
| 62 | // module. In other words, it checks for the module that AddVariationDependencies |
| 63 | // would add a dependency on with the same arguments. |
| 64 | OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool |
| 65 | |
| 66 | // OtherModuleFarDependencyVariantExists returns true if a module with the |
| 67 | // specified name and variant exists. The variant must match the given |
| 68 | // variations, but not the non-local variations of the current module. In |
| 69 | // other words, it checks for the module that AddFarVariationDependencies |
| 70 | // would add a dependency on with the same arguments. |
| 71 | OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool |
| 72 | |
| 73 | // OtherModuleReverseDependencyVariantExists returns true if a module with the |
| 74 | // specified name exists with the same variations as the current module. In |
| 75 | // other words, it checks for the module that AddReverseDependency would add a |
| 76 | // dependency on with the same argument. |
| 77 | OtherModuleReverseDependencyVariantExists(name string) bool |
| 78 | |
| 79 | // OtherModuleType returns the type of another Module. See BaseModuleContext.ModuleType for more information. |
| 80 | // It is intended for use inside the visit functions of Visit* and WalkDeps. |
| 81 | OtherModuleType(m blueprint.Module) string |
| 82 | |
Colin Cross | 24c1cbe | 2023-12-21 23:42:56 +0000 | [diff] [blame] | 83 | // otherModuleProvider returns the value for a provider for the given module. If the value is |
| 84 | // not set it returns nil and false. The value returned may be a deep copy of the value originally |
| 85 | // passed to SetProvider. |
| 86 | // |
| 87 | // This method shouldn't be used directly, prefer the type-safe android.OtherModuleProvider instead. |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 88 | otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 89 | |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame^] | 90 | // OtherModuleIsAutoGenerated returns true if the module is auto generated by another module |
| 91 | // instead of being defined in Android.bp file. |
| 92 | OtherModuleIsAutoGenerated(m blueprint.Module) bool |
| 93 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 94 | // Provider returns the value for a provider for the current module. If the value is |
Colin Cross | 24c1cbe | 2023-12-21 23:42:56 +0000 | [diff] [blame] | 95 | // not set it returns nil and false. It panics if called before the appropriate |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 96 | // mutator or GenerateBuildActions pass for the provider. The value returned may be a deep |
| 97 | // copy of the value originally passed to SetProvider. |
Colin Cross | 24c1cbe | 2023-12-21 23:42:56 +0000 | [diff] [blame] | 98 | // |
| 99 | // This method shouldn't be used directly, prefer the type-safe android.ModuleProvider instead. |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 100 | provider(provider blueprint.AnyProviderKey) (any, bool) |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 101 | |
Colin Cross | 24c1cbe | 2023-12-21 23:42:56 +0000 | [diff] [blame] | 102 | // setProvider sets the value for a provider for the current module. It panics if not called |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 103 | // during the appropriate mutator or GenerateBuildActions pass for the provider, if the value |
| 104 | // is not of the appropriate type, or if the value has already been set. The value should not |
| 105 | // be modified after being passed to SetProvider. |
Colin Cross | 24c1cbe | 2023-12-21 23:42:56 +0000 | [diff] [blame] | 106 | // |
| 107 | // This method shouldn't be used directly, prefer the type-safe android.SetProvider instead. |
| 108 | setProvider(provider blueprint.AnyProviderKey, value any) |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 109 | |
| 110 | GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module |
| 111 | |
| 112 | // GetDirectDepWithTag returns the Module the direct dependency with the specified name, or nil if |
| 113 | // none exists. It panics if the dependency does not have the specified tag. It skips any |
| 114 | // dependencies that are not an android.Module. |
| 115 | GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module |
| 116 | |
LaMont Jones | 34314b7 | 2024-01-18 18:27:35 +0000 | [diff] [blame] | 117 | // GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 118 | // name, or nil if none exists. If there are multiple dependencies on the same module it returns |
| 119 | // the first DependencyTag. |
| 120 | GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) |
| 121 | |
Colin Cross | 648daea | 2024-09-12 14:35:29 -0700 | [diff] [blame] | 122 | // 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 | |
| 131 | // VisitDirectDeps calls visit for each direct dependency. If there are multiple |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 132 | // direct dependencies on the same module visit will be called multiple times on that module |
| 133 | // and OtherModuleDependencyTag will return a different tag for each. |
| 134 | // |
| 135 | // The Module passed to the visit function should not be retained outside of the visit |
| 136 | // function, it may be invalidated by future mutators. |
Colin Cross | 648daea | 2024-09-12 14:35:29 -0700 | [diff] [blame] | 137 | VisitDirectDepsAllowDisabled(visit func(Module)) |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 138 | |
Yu Liu | dd9ccb4 | 2024-10-07 17:07:44 +0000 | [diff] [blame] | 139 | // VisitDirectDepsProxyAllowDisabled calls visit for each direct dependency. If there are |
| 140 | // multiple direct dependencies on the same module visit will be called multiple times on |
| 141 | // that module and OtherModuleDependencyTag will return a different tag for each. |
| 142 | // |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 143 | // The ModuleProxy passed to the visit function should not be retained outside of the visit function, it may be |
Yu Liu | dd9ccb4 | 2024-10-07 17:07:44 +0000 | [diff] [blame] | 144 | // invalidated by future mutators. |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 145 | VisitDirectDepsProxyAllowDisabled(visit func(proxy ModuleProxy)) |
Yu Liu | dd9ccb4 | 2024-10-07 17:07:44 +0000 | [diff] [blame] | 146 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 147 | VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) |
| 148 | |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 149 | VisitDirectDepsProxyWithTag(tag blueprint.DependencyTag, visit func(proxy ModuleProxy)) |
| 150 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 151 | // VisitDirectDepsIf calls pred for each direct dependency, and if pred returns true calls visit. If there are |
| 152 | // multiple direct dependencies on the same module pred and visit will be called multiple times on that module and |
| 153 | // OtherModuleDependencyTag will return a different tag for each. It skips any |
| 154 | // dependencies that are not an android.Module. |
| 155 | // |
| 156 | // The Module passed to the visit function should not be retained outside of the visit function, it may be |
| 157 | // invalidated by future mutators. |
| 158 | VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) |
| 159 | // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module |
| 160 | VisitDepsDepthFirst(visit func(Module)) |
| 161 | // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module |
| 162 | VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) |
| 163 | |
| 164 | // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may |
| 165 | // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the |
| 166 | // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited |
| 167 | // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips |
| 168 | // any dependencies that are not an android.Module. |
| 169 | // |
| 170 | // The Modules passed to the visit function should not be retained outside of the visit function, they may be |
| 171 | // invalidated by future mutators. |
| 172 | WalkDeps(visit func(child, parent Module) bool) |
| 173 | |
Yu Liu | dd9ccb4 | 2024-10-07 17:07:44 +0000 | [diff] [blame] | 174 | // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may |
| 175 | // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the |
| 176 | // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited |
| 177 | // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips |
| 178 | // any dependencies that are not an android.Module. |
| 179 | // |
| 180 | // The Modules passed to the visit function should not be retained outside of the visit function, they may be |
| 181 | // invalidated by future mutators. |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 182 | WalkDepsProxy(visit func(child, parent ModuleProxy) bool) |
Yu Liu | dd9ccb4 | 2024-10-07 17:07:44 +0000 | [diff] [blame] | 183 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 184 | // GetWalkPath is supposed to be called in visit function passed in WalkDeps() |
| 185 | // and returns a top-down dependency path from a start module to current child module. |
| 186 | GetWalkPath() []Module |
| 187 | |
| 188 | // PrimaryModule returns the first variant of the current module. Variants of a module are always visited in |
| 189 | // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from the |
| 190 | // Module returned by PrimaryModule without data races. This can be used to perform singleton actions that are |
| 191 | // only done once for all variants of a module. |
| 192 | PrimaryModule() Module |
| 193 | |
| 194 | // FinalModule returns the last variant of the current module. Variants of a module are always visited in |
| 195 | // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from all |
| 196 | // variants using VisitAllModuleVariants if the current module == FinalModule(). This can be used to perform |
| 197 | // singleton actions that are only done once for all variants of a module. |
| 198 | FinalModule() Module |
| 199 | |
| 200 | // VisitAllModuleVariants calls visit for each variant of the current module. Variants of a module are always |
| 201 | // visited in order by mutators and GenerateBuildActions, so the data created by the current mutator can be read |
| 202 | // from all variants if the current module == FinalModule(). Otherwise, care must be taken to not access any |
| 203 | // data modified by the current mutator. |
| 204 | VisitAllModuleVariants(visit func(Module)) |
| 205 | |
| 206 | // GetTagPath is supposed to be called in visit function passed in WalkDeps() |
| 207 | // and returns a top-down dependency tags path from a start module to current child module. |
| 208 | // It has one less entry than GetWalkPath() as it contains the dependency tags that |
| 209 | // exist between each adjacent pair of modules in the GetWalkPath(). |
| 210 | // GetTagPath()[i] is the tag between GetWalkPath()[i] and GetWalkPath()[i+1] |
| 211 | GetTagPath() []blueprint.DependencyTag |
| 212 | |
| 213 | // GetPathString is supposed to be called in visit function passed in WalkDeps() |
| 214 | // and returns a multi-line string showing the modules and dependency tags |
| 215 | // among them along the top-down dependency path from a start module to current child module. |
| 216 | // skipFirst when set to true, the output doesn't include the start module, |
| 217 | // which is already printed when this function is used along with ModuleErrorf(). |
| 218 | GetPathString(skipFirst bool) string |
| 219 | |
| 220 | AddMissingDependencies(missingDeps []string) |
| 221 | |
| 222 | // getMissingDependencies returns the list of missing dependencies. |
| 223 | // Calling this function prevents adding new dependencies. |
| 224 | getMissingDependencies() []string |
Cole Faust | bdd8aee | 2024-03-14 14:33:02 -0700 | [diff] [blame] | 225 | |
| 226 | // EvaluateConfiguration makes ModuleContext a valid proptools.ConfigurableEvaluator, so this context |
| 227 | // can be used to evaluate the final value of Configurable properties. |
Cole Faust | fdbf5d4 | 2024-04-10 15:01:23 -0700 | [diff] [blame] | 228 | EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | type baseModuleContext struct { |
| 232 | bp blueprint.BaseModuleContext |
| 233 | earlyModuleContext |
Colin Cross | 1d3d9f1 | 2024-01-18 14:30:22 -0800 | [diff] [blame] | 234 | archModuleContext |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 235 | |
| 236 | walkPath []Module |
| 237 | tagPath []blueprint.DependencyTag |
| 238 | |
| 239 | strictVisitDeps bool // If true, enforce that all dependencies are enabled |
| 240 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Yu Liu | dd9ccb4 | 2024-10-07 17:07:44 +0000 | [diff] [blame] | 243 | func getWrappedModule(module blueprint.Module) blueprint.Module { |
| 244 | if mp, isProxy := module.(ModuleProxy); isProxy { |
| 245 | return mp.module |
| 246 | } |
| 247 | return module |
| 248 | } |
| 249 | |
| 250 | func (b *baseModuleContext) EqualModules(m1, m2 Module) bool { |
| 251 | return b.bp.EqualModules(getWrappedModule(m1), getWrappedModule(m2)) |
| 252 | } |
| 253 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 254 | func (b *baseModuleContext) OtherModuleName(m blueprint.Module) string { |
Yu Liu | dd9ccb4 | 2024-10-07 17:07:44 +0000 | [diff] [blame] | 255 | return b.bp.OtherModuleName(getWrappedModule(m)) |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 256 | } |
| 257 | func (b *baseModuleContext) OtherModuleDir(m blueprint.Module) string { return b.bp.OtherModuleDir(m) } |
| 258 | func (b *baseModuleContext) OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) { |
| 259 | b.bp.OtherModuleErrorf(m, fmt, args...) |
| 260 | } |
| 261 | func (b *baseModuleContext) OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag { |
Yu Liu | dd9ccb4 | 2024-10-07 17:07:44 +0000 | [diff] [blame] | 262 | return b.bp.OtherModuleDependencyTag(getWrappedModule(m)) |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 263 | } |
| 264 | func (b *baseModuleContext) OtherModuleExists(name string) bool { return b.bp.OtherModuleExists(name) } |
| 265 | func (b *baseModuleContext) OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool { |
| 266 | return b.bp.OtherModuleDependencyVariantExists(variations, name) |
| 267 | } |
| 268 | func (b *baseModuleContext) OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool { |
| 269 | return b.bp.OtherModuleFarDependencyVariantExists(variations, name) |
| 270 | } |
| 271 | func (b *baseModuleContext) OtherModuleReverseDependencyVariantExists(name string) bool { |
| 272 | return b.bp.OtherModuleReverseDependencyVariantExists(name) |
| 273 | } |
| 274 | func (b *baseModuleContext) OtherModuleType(m blueprint.Module) string { |
| 275 | return b.bp.OtherModuleType(m) |
| 276 | } |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 277 | |
| 278 | func (b *baseModuleContext) otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) { |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 279 | return b.bp.OtherModuleProvider(m, provider) |
| 280 | } |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 281 | |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame^] | 282 | func (b *baseModuleContext) OtherModuleIsAutoGenerated(m blueprint.Module) bool { |
| 283 | return b.bp.OtherModuleIsAutoGenerated(m) |
| 284 | } |
| 285 | |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 286 | func (b *baseModuleContext) provider(provider blueprint.AnyProviderKey) (any, bool) { |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 287 | return b.bp.Provider(provider) |
| 288 | } |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 289 | |
Colin Cross | 24c1cbe | 2023-12-21 23:42:56 +0000 | [diff] [blame] | 290 | func (b *baseModuleContext) setProvider(provider blueprint.AnyProviderKey, value any) { |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 291 | b.bp.SetProvider(provider, value) |
| 292 | } |
| 293 | |
| 294 | func (b *baseModuleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module { |
| 295 | return b.bp.GetDirectDepWithTag(name, tag) |
| 296 | } |
| 297 | |
| 298 | func (b *baseModuleContext) blueprintBaseModuleContext() blueprint.BaseModuleContext { |
| 299 | return b.bp |
| 300 | } |
| 301 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 302 | func (b *baseModuleContext) AddMissingDependencies(deps []string) { |
| 303 | if deps != nil { |
| 304 | missingDeps := &b.Module().base().commonProperties.MissingDeps |
| 305 | *missingDeps = append(*missingDeps, deps...) |
| 306 | *missingDeps = FirstUniqueStrings(*missingDeps) |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | func (b *baseModuleContext) checkedMissingDeps() bool { |
| 311 | return b.Module().base().commonProperties.CheckedMissingDeps |
| 312 | } |
| 313 | |
| 314 | func (b *baseModuleContext) getMissingDependencies() []string { |
| 315 | checked := &b.Module().base().commonProperties.CheckedMissingDeps |
| 316 | *checked = true |
| 317 | var missingDeps []string |
| 318 | missingDeps = append(missingDeps, b.Module().base().commonProperties.MissingDeps...) |
| 319 | missingDeps = append(missingDeps, b.bp.EarlyGetMissingDependencies()...) |
| 320 | missingDeps = FirstUniqueStrings(missingDeps) |
| 321 | return missingDeps |
| 322 | } |
| 323 | |
| 324 | type AllowDisabledModuleDependency interface { |
| 325 | blueprint.DependencyTag |
| 326 | AllowDisabledModuleDependency(target Module) bool |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 327 | AllowDisabledModuleDependencyProxy(ctx OtherModuleProviderContext, target ModuleProxy) bool |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Jiyong Park | 8bcf3c6 | 2024-03-18 18:37:10 +0900 | [diff] [blame] | 330 | type AlwaysAllowDisabledModuleDependencyTag struct{} |
| 331 | |
| 332 | func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependency(Module) bool { |
| 333 | return true |
| 334 | } |
| 335 | |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 336 | func (t AlwaysAllowDisabledModuleDependencyTag) AllowDisabledModuleDependencyProxy(OtherModuleProviderContext, ModuleProxy) bool { |
| 337 | return true |
| 338 | } |
| 339 | |
Colin Cross | 648daea | 2024-09-12 14:35:29 -0700 | [diff] [blame] | 340 | func (b *baseModuleContext) validateAndroidModule(module blueprint.Module, tag blueprint.DependencyTag, strict bool) Module { |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 341 | aModule, _ := module.(Module) |
| 342 | |
| 343 | if !strict { |
| 344 | return aModule |
| 345 | } |
| 346 | |
| 347 | if aModule == nil { |
Colin Cross | 648daea | 2024-09-12 14:35:29 -0700 | [diff] [blame] | 348 | panic(fmt.Errorf("module %q (%#v) not an android module", b.OtherModuleName(module), tag)) |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 351 | if !aModule.Enabled(b) { |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 352 | if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependency(aModule) { |
| 353 | if b.Config().AllowMissingDependencies() { |
| 354 | b.AddMissingDependencies([]string{b.OtherModuleName(aModule)}) |
| 355 | } else { |
| 356 | b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule)) |
| 357 | } |
| 358 | } |
| 359 | return nil |
| 360 | } |
| 361 | return aModule |
| 362 | } |
| 363 | |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 364 | func (b *baseModuleContext) validateAndroidModuleProxy( |
| 365 | module blueprint.ModuleProxy, tag blueprint.DependencyTag, strict bool) *ModuleProxy { |
| 366 | aModule := ModuleProxy{module: module} |
| 367 | |
| 368 | if !strict { |
| 369 | return &aModule |
| 370 | } |
| 371 | |
| 372 | if !OtherModuleProviderOrDefault(b, module, CommonPropertiesProviderKey).Enabled { |
| 373 | if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependencyProxy(b, 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 | |
| 383 | return &aModule |
| 384 | } |
| 385 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 386 | type dep struct { |
| 387 | mod blueprint.Module |
| 388 | tag blueprint.DependencyTag |
| 389 | } |
| 390 | |
| 391 | func (b *baseModuleContext) getDirectDepsInternal(name string, tag blueprint.DependencyTag) []dep { |
| 392 | var deps []dep |
Colin Cross | 648daea | 2024-09-12 14:35:29 -0700 | [diff] [blame] | 393 | b.VisitDirectDeps(func(module Module) { |
| 394 | if module.base().BaseModuleName() == name { |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 395 | returnedTag := b.bp.OtherModuleDependencyTag(module) |
| 396 | if tag == nil || returnedTag == tag { |
| 397 | deps = append(deps, dep{module, returnedTag}) |
| 398 | } |
| 399 | } |
| 400 | }) |
| 401 | return deps |
| 402 | } |
| 403 | |
| 404 | func (b *baseModuleContext) getDirectDepInternal(name string, tag blueprint.DependencyTag) (blueprint.Module, blueprint.DependencyTag) { |
| 405 | deps := b.getDirectDepsInternal(name, tag) |
| 406 | if len(deps) == 1 { |
| 407 | return deps[0].mod, deps[0].tag |
| 408 | } else if len(deps) >= 2 { |
| 409 | panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q", |
| 410 | name, b.ModuleName())) |
| 411 | } else { |
| 412 | return nil, nil |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | func (b *baseModuleContext) getDirectDepFirstTag(name string) (blueprint.Module, blueprint.DependencyTag) { |
| 417 | foundDeps := b.getDirectDepsInternal(name, nil) |
| 418 | deps := map[blueprint.Module]bool{} |
| 419 | for _, dep := range foundDeps { |
| 420 | deps[dep.mod] = true |
| 421 | } |
| 422 | if len(deps) == 1 { |
| 423 | return foundDeps[0].mod, foundDeps[0].tag |
| 424 | } else if len(deps) >= 2 { |
| 425 | // this could happen if two dependencies have the same name in different namespaces |
| 426 | // TODO(b/186554727): this should not occur if namespaces are handled within |
| 427 | // getDirectDepsInternal. |
| 428 | panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q", |
| 429 | name, b.ModuleName())) |
| 430 | } else { |
| 431 | return nil, nil |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | func (b *baseModuleContext) GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module { |
| 436 | var deps []Module |
Colin Cross | 648daea | 2024-09-12 14:35:29 -0700 | [diff] [blame] | 437 | b.VisitDirectDeps(func(module Module) { |
| 438 | if b.bp.OtherModuleDependencyTag(module) == tag { |
| 439 | deps = append(deps, module) |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 440 | } |
| 441 | }) |
| 442 | return deps |
| 443 | } |
| 444 | |
| 445 | // GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified |
| 446 | // name, or nil if none exists. If there are multiple dependencies on the same module it returns the |
| 447 | // first DependencyTag. |
| 448 | func (b *baseModuleContext) GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) { |
| 449 | return b.getDirectDepFirstTag(name) |
| 450 | } |
| 451 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 452 | func (b *baseModuleContext) VisitDirectDeps(visit func(Module)) { |
| 453 | b.bp.VisitDirectDeps(func(module blueprint.Module) { |
Colin Cross | 648daea | 2024-09-12 14:35:29 -0700 | [diff] [blame] | 454 | if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil { |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 455 | visit(aModule) |
| 456 | } |
| 457 | }) |
| 458 | } |
| 459 | |
Colin Cross | 648daea | 2024-09-12 14:35:29 -0700 | [diff] [blame] | 460 | func (b *baseModuleContext) VisitDirectDepsAllowDisabled(visit func(Module)) { |
| 461 | b.bp.VisitDirectDeps(func(module blueprint.Module) { |
| 462 | visit(module.(Module)) |
| 463 | }) |
| 464 | } |
| 465 | |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 466 | func (b *baseModuleContext) VisitDirectDepsProxyAllowDisabled(visit func(proxy ModuleProxy)) { |
Yu Liu | dd9ccb4 | 2024-10-07 17:07:44 +0000 | [diff] [blame] | 467 | b.bp.VisitDirectDepsProxy(func(module blueprint.ModuleProxy) { |
| 468 | visit(ModuleProxy{ |
| 469 | module: module, |
| 470 | }) |
| 471 | }) |
| 472 | } |
| 473 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 474 | func (b *baseModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) { |
| 475 | b.bp.VisitDirectDeps(func(module blueprint.Module) { |
| 476 | if b.bp.OtherModuleDependencyTag(module) == tag { |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 477 | if aModule := b.validateAndroidModule(module, tag, b.strictVisitDeps); aModule != nil { |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 478 | visit(aModule) |
| 479 | } |
| 480 | } |
| 481 | }) |
| 482 | } |
| 483 | |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 484 | func (b *baseModuleContext) VisitDirectDepsProxyWithTag(tag blueprint.DependencyTag, visit func(proxy ModuleProxy)) { |
| 485 | b.bp.VisitDirectDepsProxy(func(module blueprint.ModuleProxy) { |
| 486 | if b.bp.OtherModuleDependencyTag(module) == tag { |
| 487 | if aModule := b.validateAndroidModuleProxy(module, tag, b.strictVisitDeps); aModule != nil { |
| 488 | visit(*aModule) |
| 489 | } |
| 490 | } |
| 491 | }) |
| 492 | } |
| 493 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 494 | func (b *baseModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) { |
| 495 | b.bp.VisitDirectDepsIf( |
| 496 | // pred |
| 497 | func(module blueprint.Module) bool { |
Colin Cross | 648daea | 2024-09-12 14:35:29 -0700 | [diff] [blame] | 498 | if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil { |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 499 | return pred(aModule) |
| 500 | } else { |
| 501 | return false |
| 502 | } |
| 503 | }, |
| 504 | // visit |
| 505 | func(module blueprint.Module) { |
| 506 | visit(module.(Module)) |
| 507 | }) |
| 508 | } |
| 509 | |
| 510 | func (b *baseModuleContext) VisitDepsDepthFirst(visit func(Module)) { |
| 511 | b.bp.VisitDepsDepthFirst(func(module blueprint.Module) { |
Colin Cross | 648daea | 2024-09-12 14:35:29 -0700 | [diff] [blame] | 512 | if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil { |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 513 | visit(aModule) |
| 514 | } |
| 515 | }) |
| 516 | } |
| 517 | |
| 518 | func (b *baseModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) { |
| 519 | b.bp.VisitDepsDepthFirstIf( |
| 520 | // pred |
| 521 | func(module blueprint.Module) bool { |
Colin Cross | 648daea | 2024-09-12 14:35:29 -0700 | [diff] [blame] | 522 | if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil { |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 523 | return pred(aModule) |
| 524 | } else { |
| 525 | return false |
| 526 | } |
| 527 | }, |
| 528 | // visit |
| 529 | func(module blueprint.Module) { |
| 530 | visit(module.(Module)) |
| 531 | }) |
| 532 | } |
| 533 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 534 | func (b *baseModuleContext) WalkDeps(visit func(Module, Module) bool) { |
| 535 | b.walkPath = []Module{b.Module()} |
| 536 | b.tagPath = []blueprint.DependencyTag{} |
| 537 | b.bp.WalkDeps(func(child, parent blueprint.Module) bool { |
| 538 | childAndroidModule, _ := child.(Module) |
| 539 | parentAndroidModule, _ := parent.(Module) |
| 540 | if childAndroidModule != nil && parentAndroidModule != nil { |
| 541 | // record walkPath before visit |
| 542 | for b.walkPath[len(b.walkPath)-1] != parentAndroidModule { |
| 543 | b.walkPath = b.walkPath[0 : len(b.walkPath)-1] |
| 544 | b.tagPath = b.tagPath[0 : len(b.tagPath)-1] |
| 545 | } |
| 546 | b.walkPath = append(b.walkPath, childAndroidModule) |
| 547 | b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule)) |
| 548 | return visit(childAndroidModule, parentAndroidModule) |
| 549 | } else { |
| 550 | return false |
| 551 | } |
| 552 | }) |
| 553 | } |
| 554 | |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 555 | func (b *baseModuleContext) WalkDepsProxy(visit func(ModuleProxy, ModuleProxy) bool) { |
Yu Liu | dd9ccb4 | 2024-10-07 17:07:44 +0000 | [diff] [blame] | 556 | b.walkPath = []Module{ModuleProxy{blueprint.CreateModuleProxy(b.Module())}} |
| 557 | b.tagPath = []blueprint.DependencyTag{} |
| 558 | b.bp.WalkDepsProxy(func(child, parent blueprint.ModuleProxy) bool { |
| 559 | childAndroidModule := ModuleProxy{child} |
| 560 | parentAndroidModule := ModuleProxy{parent} |
| 561 | // record walkPath before visit |
| 562 | for b.walkPath[len(b.walkPath)-1] != parentAndroidModule { |
| 563 | b.walkPath = b.walkPath[0 : len(b.walkPath)-1] |
| 564 | b.tagPath = b.tagPath[0 : len(b.tagPath)-1] |
| 565 | } |
| 566 | b.walkPath = append(b.walkPath, childAndroidModule) |
| 567 | b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule)) |
| 568 | return visit(childAndroidModule, parentAndroidModule) |
| 569 | }) |
| 570 | } |
| 571 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 572 | func (b *baseModuleContext) GetWalkPath() []Module { |
| 573 | return b.walkPath |
| 574 | } |
| 575 | |
| 576 | func (b *baseModuleContext) GetTagPath() []blueprint.DependencyTag { |
| 577 | return b.tagPath |
| 578 | } |
| 579 | |
| 580 | func (b *baseModuleContext) VisitAllModuleVariants(visit func(Module)) { |
| 581 | b.bp.VisitAllModuleVariants(func(module blueprint.Module) { |
| 582 | visit(module.(Module)) |
| 583 | }) |
| 584 | } |
| 585 | |
| 586 | func (b *baseModuleContext) PrimaryModule() Module { |
| 587 | return b.bp.PrimaryModule().(Module) |
| 588 | } |
| 589 | |
| 590 | func (b *baseModuleContext) FinalModule() Module { |
| 591 | return b.bp.FinalModule().(Module) |
| 592 | } |
| 593 | |
| 594 | // IsMetaDependencyTag returns true for cross-cutting metadata dependencies. |
| 595 | func IsMetaDependencyTag(tag blueprint.DependencyTag) bool { |
| 596 | if tag == licenseKindTag { |
| 597 | return true |
| 598 | } else if tag == licensesTag { |
| 599 | return true |
Jihoon Kang | a3a0546 | 2024-04-05 00:36:44 +0000 | [diff] [blame] | 600 | } else if tag == AcDepTag { |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 601 | return true |
| 602 | } |
| 603 | return false |
| 604 | } |
| 605 | |
| 606 | // A regexp for removing boilerplate from BaseDependencyTag from the string representation of |
| 607 | // a dependency tag. |
| 608 | var tagCleaner = regexp.MustCompile(`\QBaseDependencyTag:{}\E(, )?`) |
| 609 | |
| 610 | // PrettyPrintTag returns string representation of the tag, but prefers |
| 611 | // custom String() method if available. |
| 612 | func PrettyPrintTag(tag blueprint.DependencyTag) string { |
| 613 | // Use tag's custom String() method if available. |
| 614 | if stringer, ok := tag.(fmt.Stringer); ok { |
| 615 | return stringer.String() |
| 616 | } |
| 617 | |
| 618 | // Otherwise, get a default string representation of the tag's struct. |
| 619 | tagString := fmt.Sprintf("%T: %+v", tag, tag) |
| 620 | |
| 621 | // Remove the boilerplate from BaseDependencyTag as it adds no value. |
| 622 | tagString = tagCleaner.ReplaceAllString(tagString, "") |
| 623 | return tagString |
| 624 | } |
| 625 | |
| 626 | func (b *baseModuleContext) GetPathString(skipFirst bool) string { |
| 627 | sb := strings.Builder{} |
| 628 | tagPath := b.GetTagPath() |
| 629 | walkPath := b.GetWalkPath() |
| 630 | if !skipFirst { |
| 631 | sb.WriteString(walkPath[0].String()) |
| 632 | } |
| 633 | for i, m := range walkPath[1:] { |
| 634 | sb.WriteString("\n") |
| 635 | sb.WriteString(fmt.Sprintf(" via tag %s\n", PrettyPrintTag(tagPath[i]))) |
| 636 | sb.WriteString(fmt.Sprintf(" -> %s", m.String())) |
| 637 | } |
| 638 | return sb.String() |
| 639 | } |
Cole Faust | bdd8aee | 2024-03-14 14:33:02 -0700 | [diff] [blame] | 640 | |
Cole Faust | fdbf5d4 | 2024-04-10 15:01:23 -0700 | [diff] [blame] | 641 | func (m *baseModuleContext) EvaluateConfiguration(condition proptools.ConfigurableCondition, property string) proptools.ConfigurableValue { |
| 642 | return m.Module().ConfigurableEvaluator(m).EvaluateConfiguration(condition, property) |
Cole Faust | bdd8aee | 2024-03-14 14:33:02 -0700 | [diff] [blame] | 643 | } |