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 ( |
| 18 | "github.com/google/blueprint" |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | // SingletonContext |
| 22 | type SingletonContext interface { |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 23 | blueprintSingletonContext() blueprint.SingletonContext |
| 24 | |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 25 | Config() Config |
Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 26 | DeviceConfig() DeviceConfig |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 27 | |
| 28 | ModuleName(module blueprint.Module) string |
| 29 | ModuleDir(module blueprint.Module) string |
| 30 | ModuleSubDir(module blueprint.Module) string |
| 31 | ModuleType(module blueprint.Module) string |
| 32 | BlueprintFile(module blueprint.Module) string |
| 33 | |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 34 | // ModuleVariantsFromName returns the list of module variants named `name` in the same namespace as `referer` enforcing visibility rules. |
| 35 | // Allows generating build actions for `referer` based on the metadata for `name` deferred until the singleton context. |
| 36 | ModuleVariantsFromName(referer Module, name string) []Module |
| 37 | |
Yu Liu | 663e450 | 2024-08-12 18:23:59 +0000 | [diff] [blame^] | 38 | otherModuleProvider(module blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) |
Colin Cross | d27e7b8 | 2020-07-02 11:38:17 -0700 | [diff] [blame] | 39 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 40 | ModuleErrorf(module blueprint.Module, format string, args ...interface{}) |
| 41 | Errorf(format string, args ...interface{}) |
| 42 | Failed() bool |
| 43 | |
| 44 | Variable(pctx PackageContext, name, value string) |
Colin Cross | 5901439 | 2017-12-12 11:05:06 -0800 | [diff] [blame] | 45 | Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 46 | Build(pctx PackageContext, params BuildParams) |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 47 | |
| 48 | // Phony creates a Make-style phony rule, a rule with no commands that can depend on other |
| 49 | // phony rules or real files. Phony can be called on the same name multiple times to add |
| 50 | // additional dependencies. |
| 51 | Phony(name string, deps ...Path) |
| 52 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 53 | RequireNinjaVersion(major, minor, micro int) |
| 54 | |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 55 | // SetOutDir sets the value of the top-level "builddir" Ninja variable |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 56 | // that controls where Ninja stores its build log files. This value can be |
| 57 | // 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] | 58 | SetOutDir(pctx PackageContext, value string) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 59 | |
| 60 | // Eval takes a string with embedded ninja variables, and returns a string |
| 61 | // with all of the variables recursively expanded. Any variables references |
| 62 | // are expanded in the scope of the PackageContext. |
| 63 | Eval(pctx PackageContext, ninjaStr string) (string, error) |
| 64 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 65 | VisitAllModulesBlueprint(visit func(blueprint.Module)) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 66 | VisitAllModules(visit func(Module)) |
| 67 | VisitAllModulesIf(pred func(Module) bool, visit func(Module)) |
Mitch Phillips | 3a7a31b | 2019-10-16 15:00:12 -0700 | [diff] [blame] | 68 | |
| 69 | VisitDirectDeps(module Module, visit func(Module)) |
| 70 | VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module)) |
| 71 | |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 72 | // 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] | 73 | VisitDepsDepthFirst(module Module, visit func(Module)) |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 74 | // 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] | 75 | VisitDepsDepthFirstIf(module Module, pred func(Module) bool, |
| 76 | visit func(Module)) |
| 77 | |
| 78 | VisitAllModuleVariants(module Module, visit func(Module)) |
| 79 | |
| 80 | PrimaryModule(module Module) Module |
| 81 | FinalModule(module Module) Module |
| 82 | |
| 83 | AddNinjaFileDeps(deps ...string) |
| 84 | |
| 85 | // GlobWithDeps returns a list of files that match the specified pattern but do not match any |
| 86 | // of the patterns in excludes. It also adds efficient dependencies to rerun the primary |
| 87 | // builder whenever a file matching the pattern as added or removed, without rerunning if a |
| 88 | // file that does not match the pattern is added to a searched directory. |
| 89 | GlobWithDeps(pattern string, excludes []string) ([]string, error) |
Cole Faust | 02987bd | 2024-03-21 17:58:43 -0700 | [diff] [blame] | 90 | |
| 91 | // OtherModulePropertyErrorf reports an error on the line number of the given property of the given module |
| 92 | OtherModulePropertyErrorf(module Module, property string, format string, args ...interface{}) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | type singletonAdaptor struct { |
| 96 | Singleton |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 97 | |
| 98 | buildParams []BuildParams |
| 99 | ruleParams map[blueprint.Rule]blueprint.RuleParams |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 102 | var _ testBuildProvider = (*singletonAdaptor)(nil) |
| 103 | |
| 104 | func (s *singletonAdaptor) GenerateBuildActions(ctx blueprint.SingletonContext) { |
| 105 | sctx := &singletonContextAdaptor{SingletonContext: ctx} |
| 106 | if sctx.Config().captureBuild { |
| 107 | sctx.ruleParams = make(map[blueprint.Rule]blueprint.RuleParams) |
| 108 | } |
| 109 | |
| 110 | s.Singleton.GenerateBuildActions(sctx) |
| 111 | |
| 112 | s.buildParams = sctx.buildParams |
| 113 | s.ruleParams = sctx.ruleParams |
| 114 | } |
| 115 | |
| 116 | func (s *singletonAdaptor) BuildParamsForTests() []BuildParams { |
| 117 | return s.buildParams |
| 118 | } |
| 119 | |
| 120 | func (s *singletonAdaptor) RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams { |
| 121 | return s.ruleParams |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | type Singleton interface { |
| 125 | GenerateBuildActions(SingletonContext) |
| 126 | } |
| 127 | |
| 128 | type singletonContextAdaptor struct { |
| 129 | blueprint.SingletonContext |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 130 | |
| 131 | buildParams []BuildParams |
| 132 | ruleParams map[blueprint.Rule]blueprint.RuleParams |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 135 | func (s *singletonContextAdaptor) blueprintSingletonContext() blueprint.SingletonContext { |
| 136 | return s.SingletonContext |
| 137 | } |
| 138 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 139 | func (s *singletonContextAdaptor) Config() Config { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 140 | return s.SingletonContext.Config().(Config) |
| 141 | } |
| 142 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 143 | func (s *singletonContextAdaptor) DeviceConfig() DeviceConfig { |
Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 144 | return DeviceConfig{s.Config().deviceConfig} |
| 145 | } |
| 146 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 147 | func (s *singletonContextAdaptor) Variable(pctx PackageContext, name, value string) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 148 | s.SingletonContext.Variable(pctx.PackageContext, name, value) |
| 149 | } |
| 150 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 151 | 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] | 152 | if s.Config().UseRemoteBuild() { |
| 153 | if params.Pool == nil { |
| 154 | // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by goma/RBE, restrict |
| 155 | // jobs to the local parallelism value |
| 156 | params.Pool = localPool |
| 157 | } else if params.Pool == remotePool { |
| 158 | // remotePool is a fake pool used to identify rule that are supported for remoting. If the rule's |
| 159 | // pool is the remotePool, replace with nil so that ninja runs it at NINJA_REMOTE_NUM_JOBS |
| 160 | // parallelism. |
| 161 | params.Pool = nil |
| 162 | } |
Colin Cross | 2e2dbc2 | 2019-09-25 13:31:46 -0700 | [diff] [blame] | 163 | } |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 164 | rule := s.SingletonContext.Rule(pctx.PackageContext, name, params, argNames...) |
| 165 | if s.Config().captureBuild { |
| 166 | s.ruleParams[rule] = params |
| 167 | } |
| 168 | return rule |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 171 | func (s *singletonContextAdaptor) Build(pctx PackageContext, params BuildParams) { |
| 172 | if s.Config().captureBuild { |
| 173 | s.buildParams = append(s.buildParams, params) |
| 174 | } |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 175 | bparams := convertBuildParams(params) |
| 176 | s.SingletonContext.Build(pctx.PackageContext, bparams) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 179 | func (s *singletonContextAdaptor) Phony(name string, deps ...Path) { |
| 180 | addPhony(s.Config(), name, deps...) |
| 181 | } |
| 182 | |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 183 | func (s *singletonContextAdaptor) SetOutDir(pctx PackageContext, value string) { |
| 184 | s.SingletonContext.SetOutDir(pctx.PackageContext, value) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 187 | func (s *singletonContextAdaptor) Eval(pctx PackageContext, ninjaStr string) (string, error) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 188 | return s.SingletonContext.Eval(pctx.PackageContext, ninjaStr) |
| 189 | } |
| 190 | |
| 191 | // visitAdaptor wraps a visit function that takes an android.Module parameter into |
| 192 | // a function that takes an blueprint.Module parameter and only calls the visit function if the |
| 193 | // blueprint.Module is an android.Module. |
| 194 | func visitAdaptor(visit func(Module)) func(blueprint.Module) { |
| 195 | return func(module blueprint.Module) { |
| 196 | if aModule, ok := module.(Module); ok { |
| 197 | visit(aModule) |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // predAdaptor wraps a pred function that takes an android.Module parameter |
| 203 | // into a function that takes an blueprint.Module parameter and only calls the visit function if the |
| 204 | // blueprint.Module is an android.Module, otherwise returns false. |
| 205 | func predAdaptor(pred func(Module) bool) func(blueprint.Module) bool { |
| 206 | return func(module blueprint.Module) bool { |
| 207 | if aModule, ok := module.(Module); ok { |
| 208 | return pred(aModule) |
| 209 | } else { |
| 210 | return false |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 215 | func (s *singletonContextAdaptor) VisitAllModulesBlueprint(visit func(blueprint.Module)) { |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 216 | s.SingletonContext.VisitAllModules(visit) |
| 217 | } |
| 218 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 219 | func (s *singletonContextAdaptor) VisitAllModules(visit func(Module)) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 220 | s.SingletonContext.VisitAllModules(visitAdaptor(visit)) |
| 221 | } |
| 222 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 223 | func (s *singletonContextAdaptor) VisitAllModulesIf(pred func(Module) bool, visit func(Module)) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 224 | s.SingletonContext.VisitAllModulesIf(predAdaptor(pred), visitAdaptor(visit)) |
| 225 | } |
| 226 | |
Mitch Phillips | 3a7a31b | 2019-10-16 15:00:12 -0700 | [diff] [blame] | 227 | func (s *singletonContextAdaptor) VisitDirectDeps(module Module, visit func(Module)) { |
| 228 | s.SingletonContext.VisitDirectDeps(module, visitAdaptor(visit)) |
| 229 | } |
| 230 | |
| 231 | func (s *singletonContextAdaptor) VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module)) { |
| 232 | s.SingletonContext.VisitDirectDepsIf(module, predAdaptor(pred), visitAdaptor(visit)) |
| 233 | } |
| 234 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 235 | func (s *singletonContextAdaptor) VisitDepsDepthFirst(module Module, visit func(Module)) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 236 | s.SingletonContext.VisitDepsDepthFirst(module, visitAdaptor(visit)) |
| 237 | } |
| 238 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 239 | 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] | 240 | s.SingletonContext.VisitDepsDepthFirstIf(module, predAdaptor(pred), visitAdaptor(visit)) |
| 241 | } |
| 242 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 243 | func (s *singletonContextAdaptor) VisitAllModuleVariants(module Module, visit func(Module)) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 244 | s.SingletonContext.VisitAllModuleVariants(module, visitAdaptor(visit)) |
| 245 | } |
| 246 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 247 | func (s *singletonContextAdaptor) PrimaryModule(module Module) Module { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 248 | return s.SingletonContext.PrimaryModule(module).(Module) |
| 249 | } |
| 250 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 251 | func (s *singletonContextAdaptor) FinalModule(module Module) Module { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 252 | return s.SingletonContext.FinalModule(module).(Module) |
| 253 | } |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 254 | |
| 255 | func (s *singletonContextAdaptor) ModuleVariantsFromName(referer Module, name string) []Module { |
Cole Faust | 894bb3b | 2024-02-07 11:28:26 -0800 | [diff] [blame] | 256 | // get module reference for visibility enforcement |
Cole Faust | 9a24d90 | 2024-03-18 15:38:12 -0700 | [diff] [blame] | 257 | qualified := createVisibilityModuleReference(s.ModuleName(referer), s.ModuleDir(referer), referer) |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 258 | |
| 259 | modules := s.SingletonContext.ModuleVariantsFromName(referer, name) |
| 260 | result := make([]Module, 0, len(modules)) |
| 261 | for _, m := range modules { |
| 262 | if module, ok := m.(Module); ok { |
| 263 | // enforce visibility |
| 264 | depName := s.ModuleName(module) |
| 265 | depDir := s.ModuleDir(module) |
| 266 | depQualified := qualifiedModuleName{depDir, depName} |
| 267 | // Targets are always visible to other targets in their own package. |
Cole Faust | 894bb3b | 2024-02-07 11:28:26 -0800 | [diff] [blame] | 268 | if depQualified.pkg != qualified.name.pkg { |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 269 | rule := effectiveVisibilityRules(s.Config(), depQualified) |
| 270 | if !rule.matches(qualified) { |
Bob Badour | a5ea247 | 2022-05-20 16:37:26 -0700 | [diff] [blame] | 271 | s.ModuleErrorf(referer, "module %q references %q which is not visible to this module\nYou may need to add %q to its visibility", |
| 272 | referer.Name(), depQualified, "//"+s.ModuleDir(referer)) |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 273 | continue |
| 274 | } |
| 275 | } |
| 276 | result = append(result, module) |
| 277 | } |
| 278 | } |
| 279 | return result |
| 280 | } |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 281 | |
Yu Liu | 663e450 | 2024-08-12 18:23:59 +0000 | [diff] [blame^] | 282 | func (s *singletonContextAdaptor) otherModuleProvider(module blueprint.Module, provider blueprint.AnyProviderKey) (any, bool) { |
Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 283 | return s.SingletonContext.ModuleProvider(module, provider) |
| 284 | } |
Cole Faust | 02987bd | 2024-03-21 17:58:43 -0700 | [diff] [blame] | 285 | |
| 286 | func (s *singletonContextAdaptor) OtherModulePropertyErrorf(module Module, property string, format string, args ...interface{}) { |
Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 287 | s.blueprintSingletonContext().OtherModulePropertyErrorf(module, property, format, args...) |
Cole Faust | 02987bd | 2024-03-21 17:58:43 -0700 | [diff] [blame] | 288 | } |