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