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