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