Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1 | // Copyright 2017 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 ( |
Cole Faust | f2aab5e | 2025-02-11 13:32:51 -0800 | [diff] [blame] | 18 | "slices" |
| 19 | "sync" |
| 20 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | // SingletonContext |
| 25 | type SingletonContext interface { |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 26 | blueprintSingletonContext() blueprint.SingletonContext |
| 27 | |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 28 | Config() Config |
Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 29 | DeviceConfig() DeviceConfig |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 30 | |
| 31 | ModuleName(module blueprint.Module) string |
| 32 | ModuleDir(module blueprint.Module) string |
| 33 | ModuleSubDir(module blueprint.Module) string |
| 34 | ModuleType(module blueprint.Module) string |
| 35 | BlueprintFile(module blueprint.Module) string |
| 36 | |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 37 | // ModuleVariantsFromName returns the list of module variants named `name` in the same namespace as `referer` enforcing visibility rules. |
| 38 | // Allows generating build actions for `referer` based on the metadata for `name` deferred until the singleton context. |
| 39 | ModuleVariantsFromName(referer Module, name string) []Module |
| 40 | |
Yu Liu | 663e450 | 2024-08-12 18:23:59 +0000 | [diff] [blame] | 41 | otherModuleProvider(module blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) |
Colin Cross | d27e7b8 | 2020-07-02 11:38:17 -0700 | [diff] [blame] | 42 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 43 | ModuleErrorf(module blueprint.Module, format string, args ...interface{}) |
| 44 | Errorf(format string, args ...interface{}) |
| 45 | Failed() bool |
| 46 | |
| 47 | Variable(pctx PackageContext, name, value string) |
Colin Cross | 5901439 | 2017-12-12 11:05:06 -0800 | [diff] [blame] | 48 | Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 49 | Build(pctx PackageContext, params BuildParams) |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 50 | |
| 51 | // Phony creates a Make-style phony rule, a rule with no commands that can depend on other |
| 52 | // phony rules or real files. Phony can be called on the same name multiple times to add |
| 53 | // additional dependencies. |
| 54 | Phony(name string, deps ...Path) |
| 55 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 56 | RequireNinjaVersion(major, minor, micro int) |
| 57 | |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 58 | // SetOutDir sets the value of the top-level "builddir" Ninja variable |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 59 | // that controls where Ninja stores its build log files. This value can be |
| 60 | // set at most one time for a single build, later calls are ignored. |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 61 | SetOutDir(pctx PackageContext, value string) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 62 | |
| 63 | // Eval takes a string with embedded ninja variables, and returns a string |
| 64 | // with all of the variables recursively expanded. Any variables references |
| 65 | // are expanded in the scope of the PackageContext. |
| 66 | Eval(pctx PackageContext, ninjaStr string) (string, error) |
| 67 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 68 | VisitAllModulesBlueprint(visit func(blueprint.Module)) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 69 | VisitAllModules(visit func(Module)) |
Yu Liu | ec7043d | 2024-11-05 18:22:20 +0000 | [diff] [blame] | 70 | VisitAllModuleProxies(visit func(proxy ModuleProxy)) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 71 | VisitAllModulesIf(pred func(Module) bool, visit func(Module)) |
Mitch Phillips | 3a7a31b | 2019-10-16 15:00:12 -0700 | [diff] [blame] | 72 | |
| 73 | VisitDirectDeps(module Module, visit func(Module)) |
| 74 | VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module)) |
| 75 | |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 76 | // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 77 | VisitDepsDepthFirst(module Module, visit func(Module)) |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 78 | // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 79 | VisitDepsDepthFirstIf(module Module, pred func(Module) bool, |
| 80 | visit func(Module)) |
| 81 | |
| 82 | VisitAllModuleVariants(module Module, visit func(Module)) |
| 83 | |
Yu Liu | ec7043d | 2024-11-05 18:22:20 +0000 | [diff] [blame] | 84 | VisitAllModuleVariantProxies(module Module, visit func(proxy ModuleProxy)) |
| 85 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 86 | PrimaryModule(module Module) Module |
Yu Liu | 367827f | 2025-02-15 00:18:33 +0000 | [diff] [blame] | 87 | |
| 88 | PrimaryModuleProxy(module ModuleProxy) ModuleProxy |
| 89 | |
Yu Liu | 88ea9ff | 2024-11-07 19:19:42 +0000 | [diff] [blame] | 90 | IsFinalModule(module Module) bool |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 91 | |
| 92 | AddNinjaFileDeps(deps ...string) |
| 93 | |
| 94 | // GlobWithDeps returns a list of files that match the specified pattern but do not match any |
| 95 | // of the patterns in excludes. It also adds efficient dependencies to rerun the primary |
| 96 | // builder whenever a file matching the pattern as added or removed, without rerunning if a |
| 97 | // file that does not match the pattern is added to a searched directory. |
| 98 | GlobWithDeps(pattern string, excludes []string) ([]string, error) |
Cole Faust | 02987bd | 2024-03-21 17:58:43 -0700 | [diff] [blame] | 99 | |
| 100 | // OtherModulePropertyErrorf reports an error on the line number of the given property of the given module |
| 101 | OtherModulePropertyErrorf(module Module, property string, format string, args ...interface{}) |
Cole Faust | 4e2bf9f | 2024-09-11 13:26:20 -0700 | [diff] [blame] | 102 | |
| 103 | // HasMutatorFinished returns true if the given mutator has finished running. |
| 104 | // It will panic if given an invalid mutator name. |
| 105 | HasMutatorFinished(mutatorName string) bool |
Cole Faust | f2aab5e | 2025-02-11 13:32:51 -0800 | [diff] [blame] | 106 | |
| 107 | // DistForGoals creates a rule to copy one or more Paths to the artifacts |
| 108 | // directory on the build server when any of the specified goals are built. |
| 109 | DistForGoal(goal string, paths ...Path) |
| 110 | |
| 111 | // DistForGoalWithFilename creates a rule to copy a Path to the artifacts |
| 112 | // directory on the build server with the given filename when the specified |
| 113 | // goal is built. |
| 114 | DistForGoalWithFilename(goal string, path Path, filename string) |
| 115 | |
| 116 | // DistForGoals creates a rule to copy one or more Paths to the artifacts |
| 117 | // directory on the build server when any of the specified goals are built. |
| 118 | DistForGoals(goals []string, paths ...Path) |
| 119 | |
| 120 | // DistForGoalsWithFilename creates a rule to copy a Path to the artifacts |
| 121 | // directory on the build server with the given filename when any of the |
| 122 | // specified goals are built. |
| 123 | DistForGoalsWithFilename(goals []string, path Path, filename string) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | type singletonAdaptor struct { |
| 127 | Singleton |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 128 | |
| 129 | buildParams []BuildParams |
| 130 | ruleParams map[blueprint.Rule]blueprint.RuleParams |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 133 | var _ testBuildProvider = (*singletonAdaptor)(nil) |
| 134 | |
| 135 | func (s *singletonAdaptor) GenerateBuildActions(ctx blueprint.SingletonContext) { |
| 136 | sctx := &singletonContextAdaptor{SingletonContext: ctx} |
| 137 | if sctx.Config().captureBuild { |
| 138 | sctx.ruleParams = make(map[blueprint.Rule]blueprint.RuleParams) |
| 139 | } |
| 140 | |
| 141 | s.Singleton.GenerateBuildActions(sctx) |
| 142 | |
| 143 | s.buildParams = sctx.buildParams |
| 144 | s.ruleParams = sctx.ruleParams |
Cole Faust | f2aab5e | 2025-02-11 13:32:51 -0800 | [diff] [blame] | 145 | |
| 146 | if len(sctx.dists) > 0 { |
| 147 | dists := getSingletonDists(sctx.Config()) |
| 148 | dists.lock.Lock() |
| 149 | defer dists.lock.Unlock() |
| 150 | dists.dists = append(dists.dists, sctx.dists...) |
| 151 | } |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | func (s *singletonAdaptor) BuildParamsForTests() []BuildParams { |
| 155 | return s.buildParams |
| 156 | } |
| 157 | |
| 158 | func (s *singletonAdaptor) RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams { |
| 159 | return s.ruleParams |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Cole Faust | f2aab5e | 2025-02-11 13:32:51 -0800 | [diff] [blame] | 162 | var singletonDistsKey = NewOnceKey("singletonDistsKey") |
| 163 | |
| 164 | type singletonDistsAndLock struct { |
| 165 | dists []dist |
| 166 | lock sync.Mutex |
| 167 | } |
| 168 | |
| 169 | func getSingletonDists(config Config) *singletonDistsAndLock { |
| 170 | return config.Once(singletonDistsKey, func() interface{} { |
| 171 | return &singletonDistsAndLock{} |
| 172 | }).(*singletonDistsAndLock) |
| 173 | } |
| 174 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 175 | type Singleton interface { |
| 176 | GenerateBuildActions(SingletonContext) |
| 177 | } |
| 178 | |
| 179 | type singletonContextAdaptor struct { |
| 180 | blueprint.SingletonContext |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 181 | |
| 182 | buildParams []BuildParams |
| 183 | ruleParams map[blueprint.Rule]blueprint.RuleParams |
Cole Faust | f2aab5e | 2025-02-11 13:32:51 -0800 | [diff] [blame] | 184 | dists []dist |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 187 | func (s *singletonContextAdaptor) blueprintSingletonContext() blueprint.SingletonContext { |
| 188 | return s.SingletonContext |
| 189 | } |
| 190 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 191 | func (s *singletonContextAdaptor) Config() Config { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 192 | return s.SingletonContext.Config().(Config) |
| 193 | } |
| 194 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 195 | func (s *singletonContextAdaptor) DeviceConfig() DeviceConfig { |
Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 196 | return DeviceConfig{s.Config().deviceConfig} |
| 197 | } |
| 198 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 199 | func (s *singletonContextAdaptor) Variable(pctx PackageContext, name, value string) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 200 | s.SingletonContext.Variable(pctx.PackageContext, name, value) |
| 201 | } |
| 202 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 203 | func (s *singletonContextAdaptor) Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule { |
Ramy Medhat | 944839a | 2020-03-31 22:14:52 -0400 | [diff] [blame] | 204 | if s.Config().UseRemoteBuild() { |
| 205 | if params.Pool == nil { |
| 206 | // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by goma/RBE, restrict |
| 207 | // jobs to the local parallelism value |
| 208 | params.Pool = localPool |
| 209 | } else if params.Pool == remotePool { |
| 210 | // remotePool is a fake pool used to identify rule that are supported for remoting. If the rule's |
| 211 | // pool is the remotePool, replace with nil so that ninja runs it at NINJA_REMOTE_NUM_JOBS |
| 212 | // parallelism. |
| 213 | params.Pool = nil |
| 214 | } |
Colin Cross | 2e2dbc2 | 2019-09-25 13:31:46 -0700 | [diff] [blame] | 215 | } |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 216 | rule := s.SingletonContext.Rule(pctx.PackageContext, name, params, argNames...) |
| 217 | if s.Config().captureBuild { |
| 218 | s.ruleParams[rule] = params |
| 219 | } |
| 220 | return rule |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 223 | func (s *singletonContextAdaptor) Build(pctx PackageContext, params BuildParams) { |
| 224 | if s.Config().captureBuild { |
| 225 | s.buildParams = append(s.buildParams, params) |
| 226 | } |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 227 | bparams := convertBuildParams(params) |
| 228 | s.SingletonContext.Build(pctx.PackageContext, bparams) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 231 | func (s *singletonContextAdaptor) Phony(name string, deps ...Path) { |
Yu Liu | 5451362 | 2024-08-19 20:00:32 +0000 | [diff] [blame] | 232 | addSingletonPhony(s.Config(), name, deps...) |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 235 | func (s *singletonContextAdaptor) SetOutDir(pctx PackageContext, value string) { |
| 236 | s.SingletonContext.SetOutDir(pctx.PackageContext, value) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 239 | func (s *singletonContextAdaptor) Eval(pctx PackageContext, ninjaStr string) (string, error) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 240 | return s.SingletonContext.Eval(pctx.PackageContext, ninjaStr) |
| 241 | } |
| 242 | |
| 243 | // visitAdaptor wraps a visit function that takes an android.Module parameter into |
Yu Liu | ec7043d | 2024-11-05 18:22:20 +0000 | [diff] [blame] | 244 | // a function that takes a blueprint.Module parameter and only calls the visit function if the |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 245 | // blueprint.Module is an android.Module. |
| 246 | func visitAdaptor(visit func(Module)) func(blueprint.Module) { |
| 247 | return func(module blueprint.Module) { |
| 248 | if aModule, ok := module.(Module); ok { |
| 249 | visit(aModule) |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Yu Liu | ec7043d | 2024-11-05 18:22:20 +0000 | [diff] [blame] | 254 | // visitProxyAdaptor wraps a visit function that takes an android.ModuleProxy parameter into |
| 255 | // a function that takes a blueprint.ModuleProxy parameter. |
| 256 | func visitProxyAdaptor(visit func(proxy ModuleProxy)) func(proxy blueprint.ModuleProxy) { |
| 257 | return func(module blueprint.ModuleProxy) { |
| 258 | visit(ModuleProxy{ |
| 259 | module: module, |
| 260 | }) |
| 261 | } |
| 262 | } |
| 263 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 264 | // predAdaptor wraps a pred function that takes an android.Module parameter |
| 265 | // into a function that takes an blueprint.Module parameter and only calls the visit function if the |
| 266 | // blueprint.Module is an android.Module, otherwise returns false. |
| 267 | func predAdaptor(pred func(Module) bool) func(blueprint.Module) bool { |
| 268 | return func(module blueprint.Module) bool { |
| 269 | if aModule, ok := module.(Module); ok { |
| 270 | return pred(aModule) |
| 271 | } else { |
| 272 | return false |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
Yu Liu | 367827f | 2025-02-15 00:18:33 +0000 | [diff] [blame] | 277 | func (s *singletonContextAdaptor) ModuleName(module blueprint.Module) string { |
| 278 | return s.SingletonContext.ModuleName(getWrappedModule(module)) |
| 279 | } |
| 280 | |
| 281 | func (s *singletonContextAdaptor) ModuleDir(module blueprint.Module) string { |
| 282 | return s.SingletonContext.ModuleDir(getWrappedModule(module)) |
| 283 | } |
| 284 | |
| 285 | func (s *singletonContextAdaptor) ModuleSubDir(module blueprint.Module) string { |
| 286 | return s.SingletonContext.ModuleSubDir(getWrappedModule(module)) |
| 287 | } |
| 288 | |
| 289 | func (s *singletonContextAdaptor) ModuleType(module blueprint.Module) string { |
| 290 | return s.SingletonContext.ModuleType(getWrappedModule(module)) |
| 291 | } |
| 292 | |
Yu Liu | 95cef3a | 2025-02-25 00:54:20 +0000 | [diff] [blame^] | 293 | func (s *singletonContextAdaptor) BlueprintFile(module blueprint.Module) string { |
| 294 | return s.SingletonContext.BlueprintFile(getWrappedModule(module)) |
| 295 | } |
| 296 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 297 | func (s *singletonContextAdaptor) VisitAllModulesBlueprint(visit func(blueprint.Module)) { |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 298 | s.SingletonContext.VisitAllModules(visit) |
| 299 | } |
| 300 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 301 | func (s *singletonContextAdaptor) VisitAllModules(visit func(Module)) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 302 | s.SingletonContext.VisitAllModules(visitAdaptor(visit)) |
| 303 | } |
| 304 | |
Yu Liu | ec7043d | 2024-11-05 18:22:20 +0000 | [diff] [blame] | 305 | func (s *singletonContextAdaptor) VisitAllModuleProxies(visit func(proxy ModuleProxy)) { |
| 306 | s.SingletonContext.VisitAllModuleProxies(visitProxyAdaptor(visit)) |
| 307 | } |
| 308 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 309 | func (s *singletonContextAdaptor) VisitAllModulesIf(pred func(Module) bool, visit func(Module)) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 310 | s.SingletonContext.VisitAllModulesIf(predAdaptor(pred), visitAdaptor(visit)) |
| 311 | } |
| 312 | |
Mitch Phillips | 3a7a31b | 2019-10-16 15:00:12 -0700 | [diff] [blame] | 313 | func (s *singletonContextAdaptor) VisitDirectDeps(module Module, visit func(Module)) { |
| 314 | s.SingletonContext.VisitDirectDeps(module, visitAdaptor(visit)) |
| 315 | } |
| 316 | |
| 317 | func (s *singletonContextAdaptor) VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module)) { |
| 318 | s.SingletonContext.VisitDirectDepsIf(module, predAdaptor(pred), visitAdaptor(visit)) |
| 319 | } |
| 320 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 321 | func (s *singletonContextAdaptor) VisitDepsDepthFirst(module Module, visit func(Module)) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 322 | s.SingletonContext.VisitDepsDepthFirst(module, visitAdaptor(visit)) |
| 323 | } |
| 324 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 325 | func (s *singletonContextAdaptor) VisitDepsDepthFirstIf(module Module, pred func(Module) bool, visit func(Module)) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 326 | s.SingletonContext.VisitDepsDepthFirstIf(module, predAdaptor(pred), visitAdaptor(visit)) |
| 327 | } |
| 328 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 329 | func (s *singletonContextAdaptor) VisitAllModuleVariants(module Module, visit func(Module)) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 330 | s.SingletonContext.VisitAllModuleVariants(module, visitAdaptor(visit)) |
| 331 | } |
| 332 | |
Yu Liu | ec7043d | 2024-11-05 18:22:20 +0000 | [diff] [blame] | 333 | func (s *singletonContextAdaptor) VisitAllModuleVariantProxies(module Module, visit func(proxy ModuleProxy)) { |
| 334 | s.SingletonContext.VisitAllModuleVariantProxies(module, visitProxyAdaptor(visit)) |
| 335 | } |
| 336 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 337 | func (s *singletonContextAdaptor) PrimaryModule(module Module) Module { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 338 | return s.SingletonContext.PrimaryModule(module).(Module) |
| 339 | } |
| 340 | |
Yu Liu | 367827f | 2025-02-15 00:18:33 +0000 | [diff] [blame] | 341 | func (s *singletonContextAdaptor) PrimaryModuleProxy(module ModuleProxy) ModuleProxy { |
| 342 | return ModuleProxy{s.SingletonContext.PrimaryModuleProxy(module.module)} |
| 343 | } |
| 344 | |
Yu Liu | 88ea9ff | 2024-11-07 19:19:42 +0000 | [diff] [blame] | 345 | func (s *singletonContextAdaptor) IsFinalModule(module Module) bool { |
| 346 | return s.SingletonContext.IsFinalModule(module) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 347 | } |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 348 | |
| 349 | func (s *singletonContextAdaptor) ModuleVariantsFromName(referer Module, name string) []Module { |
Cole Faust | 894bb3b | 2024-02-07 11:28:26 -0800 | [diff] [blame] | 350 | // get module reference for visibility enforcement |
Cole Faust | 9a24d90 | 2024-03-18 15:38:12 -0700 | [diff] [blame] | 351 | qualified := createVisibilityModuleReference(s.ModuleName(referer), s.ModuleDir(referer), referer) |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 352 | |
| 353 | modules := s.SingletonContext.ModuleVariantsFromName(referer, name) |
| 354 | result := make([]Module, 0, len(modules)) |
| 355 | for _, m := range modules { |
| 356 | if module, ok := m.(Module); ok { |
| 357 | // enforce visibility |
| 358 | depName := s.ModuleName(module) |
| 359 | depDir := s.ModuleDir(module) |
| 360 | depQualified := qualifiedModuleName{depDir, depName} |
| 361 | // Targets are always visible to other targets in their own package. |
Cole Faust | 894bb3b | 2024-02-07 11:28:26 -0800 | [diff] [blame] | 362 | if depQualified.pkg != qualified.name.pkg { |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 363 | rule := effectiveVisibilityRules(s.Config(), depQualified) |
| 364 | if !rule.matches(qualified) { |
Bob Badour | a5ea247 | 2022-05-20 16:37:26 -0700 | [diff] [blame] | 365 | s.ModuleErrorf(referer, "module %q references %q which is not visible to this module\nYou may need to add %q to its visibility", |
| 366 | referer.Name(), depQualified, "//"+s.ModuleDir(referer)) |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 367 | continue |
| 368 | } |
| 369 | } |
| 370 | result = append(result, module) |
| 371 | } |
| 372 | } |
| 373 | return result |
| 374 | } |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 375 | |
Yu Liu | 663e450 | 2024-08-12 18:23:59 +0000 | [diff] [blame] | 376 | func (s *singletonContextAdaptor) otherModuleProvider(module blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) { |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 377 | return s.SingletonContext.ModuleProvider(module, provider) |
| 378 | } |
Cole Faust | 02987bd | 2024-03-21 17:58:43 -0700 | [diff] [blame] | 379 | |
| 380 | func (s *singletonContextAdaptor) OtherModulePropertyErrorf(module Module, property string, format string, args ...interface{}) { |
Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 381 | s.blueprintSingletonContext().OtherModulePropertyErrorf(module, property, format, args...) |
Cole Faust | 02987bd | 2024-03-21 17:58:43 -0700 | [diff] [blame] | 382 | } |
Cole Faust | 4e2bf9f | 2024-09-11 13:26:20 -0700 | [diff] [blame] | 383 | |
| 384 | func (s *singletonContextAdaptor) HasMutatorFinished(mutatorName string) bool { |
| 385 | return s.blueprintSingletonContext().HasMutatorFinished(mutatorName) |
| 386 | } |
Cole Faust | f2aab5e | 2025-02-11 13:32:51 -0800 | [diff] [blame] | 387 | func (s *singletonContextAdaptor) DistForGoal(goal string, paths ...Path) { |
| 388 | s.DistForGoals([]string{goal}, paths...) |
| 389 | } |
| 390 | |
| 391 | func (s *singletonContextAdaptor) DistForGoalWithFilename(goal string, path Path, filename string) { |
| 392 | s.DistForGoalsWithFilename([]string{goal}, path, filename) |
| 393 | } |
| 394 | |
| 395 | func (s *singletonContextAdaptor) DistForGoals(goals []string, paths ...Path) { |
| 396 | var copies distCopies |
| 397 | for _, path := range paths { |
| 398 | copies = append(copies, distCopy{ |
| 399 | from: path, |
| 400 | dest: path.Base(), |
| 401 | }) |
| 402 | } |
| 403 | s.dists = append(s.dists, dist{ |
| 404 | goals: slices.Clone(goals), |
| 405 | paths: copies, |
| 406 | }) |
| 407 | } |
| 408 | |
| 409 | func (s *singletonContextAdaptor) DistForGoalsWithFilename(goals []string, path Path, filename string) { |
| 410 | s.dists = append(s.dists, dist{ |
| 411 | goals: slices.Clone(goals), |
| 412 | paths: distCopies{{from: path, dest: filename}}, |
| 413 | }) |
| 414 | } |