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