1// Copyright 2014 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 15package blueprint 16 17import ( 18 "fmt" 19 20 "github.com/google/blueprint/pathtools" 21) 22 23type Singleton interface { 24 GenerateBuildActions(SingletonContext) 25} 26 27type SingletonContext interface { 28 // Config returns the config object that was passed to Context.PrepareBuildActions. 29 Config() interface{} 30 31 // Name returns the name of the current singleton passed to Context.RegisterSingletonType 32 Name() string 33 34 // ModuleName returns the name of the given Module. See BaseModuleContext.ModuleName for more information. 35 ModuleName(module Module) string 36 37 // ModuleDir returns the directory of the given Module. See BaseModuleContext.ModuleDir for more information. 38 ModuleDir(module Module) string 39 40 // ModuleSubDir returns the unique subdirectory name of the given Module. See ModuleContext.ModuleSubDir for 41 // more information. 42 ModuleSubDir(module Module) string 43 44 // ModuleType returns the type of the given Module. See BaseModuleContext.ModuleType for more information. 45 ModuleType(module Module) string 46 47 // BlueprintFile returns the path of the Blueprint file that defined the given module. 48 BlueprintFile(module Module) string 49 50 // ModuleProvider returns the value, if any, for the provider for a module. If the value for the 51 // provider was not set it returns the zero value of the type of the provider, which means the 52 // return value can always be type-asserted to the type of the provider. The return value should 53 // always be considered read-only. It panics if called before the appropriate mutator or 54 // GenerateBuildActions pass for the provider on the module. 55 ModuleProvider(module Module, provider AnyProviderKey) (any, bool) 56 57 // ModuleErrorf reports an error at the line number of the module type in the module definition. 58 ModuleErrorf(module Module, format string, args ...interface{}) 59 60 // Errorf reports an error at the specified position of the module definition file. 61 Errorf(format string, args ...interface{}) 62 63 // OtherModulePropertyErrorf reports an error on the line number of the given property of the given module 64 OtherModulePropertyErrorf(module Module, property string, format string, args ...interface{}) 65 66 // Failed returns true if any errors have been reported. In most cases the singleton can continue with generating 67 // build rules after an error, allowing it to report additional errors in a single run, but in cases where the error 68 // has prevented the singleton from creating necessary data it can return early when Failed returns true. 69 Failed() bool 70 71 // Variable creates a new ninja variable scoped to the singleton. It can be referenced by calls to Rule and Build 72 // in the same singleton. 73 Variable(pctx PackageContext, name, value string) 74 75 // Rule creates a new ninja rule scoped to the singleton. It can be referenced by calls to Build in the same 76 // singleton. 77 Rule(pctx PackageContext, name string, params RuleParams, argNames ...string) Rule 78 79 // Build creates a new ninja build statement. 80 Build(pctx PackageContext, params BuildParams) 81 82 // RequireNinjaVersion sets the generated ninja manifest to require at least the specified version of ninja. 83 RequireNinjaVersion(major, minor, micro int) 84 85 // SetOutDir sets the value of the top-level "builddir" Ninja variable 86 // that controls where Ninja stores its build log files. This value can be 87 // set at most one time for a single build, later calls are ignored. 88 SetOutDir(pctx PackageContext, value string) 89 90 // AddSubninja adds a ninja file to include with subninja. This should likely 91 // only ever be used inside bootstrap to handle glob rules. 92 AddSubninja(file string) 93 94 // Eval takes a string with embedded ninja variables, and returns a string 95 // with all of the variables recursively expanded. Any variables references 96 // are expanded in the scope of the PackageContext. 97 Eval(pctx PackageContext, ninjaStr string) (string, error) 98 99 // VisitAllModules calls visit for each defined variant of each module in an unspecified order. 100 VisitAllModules(visit func(Module)) 101 102 // VisitAllModules calls pred for each defined variant of each module in an unspecified order, and if pred returns 103 // true calls visit. 104 VisitAllModulesIf(pred func(Module) bool, visit func(Module)) 105 106 // VisitDirectDeps calls visit for each direct dependency of the Module. If there are 107 // multiple direct dependencies on the same module visit will be called multiple times on 108 // that module and OtherModuleDependencyTag will return a different tag for each. 109 // 110 // The Module passed to the visit function should not be retained outside of the visit 111 // function, it may be invalidated by future mutators. 112 VisitDirectDeps(module Module, visit func(Module)) 113 114 // VisitDirectDepsIf calls pred for each direct dependency of the Module, and if pred 115 // returns true calls visit. If there are multiple direct dependencies on the same module 116 // pred and visit will be called multiple times on that module and OtherModuleDependencyTag 117 // will return a different tag for each. 118 // 119 // The Module passed to the visit function should not be retained outside of the visit 120 // function, it may be invalidated by future mutators. 121 VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module)) 122 123 // VisitDepsDepthFirst calls visit for each transitive dependency, traversing the dependency tree in depth first 124 // order. visit will only be called once for any given module, even if there are multiple paths through the 125 // dependency tree to the module or multiple direct dependencies with different tags. 126 VisitDepsDepthFirst(module Module, visit func(Module)) 127 128 // VisitDepsDepthFirst calls pred for each transitive dependency, and if pred returns true calls visit, traversing 129 // the dependency tree in depth first order. visit will only be called once for any given module, even if there are 130 // multiple paths through the dependency tree to the module or multiple direct dependencies with different tags. 131 VisitDepsDepthFirstIf(module Module, pred func(Module) bool, 132 visit func(Module)) 133 134 // VisitAllModuleVariants calls visit for each variant of the given module. 135 VisitAllModuleVariants(module Module, visit func(Module)) 136 137 // PrimaryModule returns the first variant of the given module. This can be used to perform 138 // // singleton actions that are only done once for all variants of a module. 139 PrimaryModule(module Module) Module 140 141 // FinalModule returns the last variant of the given module. This can be used to perform 142 // singleton actions that are only done once for all variants of a module. 143 FinalModule(module Module) Module 144 145 // AddNinjaFileDeps adds dependencies on the specified files to the rule that creates the ninja manifest. The 146 // primary builder will be rerun whenever the specified files are modified. 147 AddNinjaFileDeps(deps ...string) 148 149 // GlobWithDeps returns a list of files and directories that match the 150 // specified pattern but do not match any of the patterns in excludes. 151 // Any directories will have a '/' suffix. It also adds efficient 152 // dependencies to rerun the primary builder whenever a file matching 153 // the pattern as added or removed, without rerunning if a file that 154 // does not match the pattern is added to a searched directory. 155 GlobWithDeps(pattern string, excludes []string) ([]string, error) 156 157 // Fs returns a pathtools.Filesystem that can be used to interact with files. Using the Filesystem interface allows 158 // the singleton to be used in build system tests that run against a mock filesystem. 159 Fs() pathtools.FileSystem 160 161 // ModuleVariantsFromName returns the list of module variants named `name` in the same namespace as `referer`. 162 // Allows generating build actions for `referer` based on the metadata for `name` deferred until the singleton context. 163 ModuleVariantsFromName(referer Module, name string) []Module 164} 165 166var _ SingletonContext = (*singletonContext)(nil) 167 168type singletonContext struct { 169 name string 170 context *Context 171 config interface{} 172 scope *localScope 173 globals *liveTracker 174 175 ninjaFileDeps []string 176 errs []error 177 178 actionDefs localBuildActions 179} 180 181func (s *singletonContext) Config() interface{} { 182 return s.config 183} 184 185func (s *singletonContext) Name() string { 186 return s.name 187} 188 189func (s *singletonContext) ModuleName(logicModule Module) string { 190 return s.context.ModuleName(logicModule) 191} 192 193func (s *singletonContext) ModuleDir(logicModule Module) string { 194 return s.context.ModuleDir(logicModule) 195} 196 197func (s *singletonContext) ModuleSubDir(logicModule Module) string { 198 return s.context.ModuleSubDir(logicModule) 199} 200 201func (s *singletonContext) ModuleType(logicModule Module) string { 202 return s.context.ModuleType(logicModule) 203} 204 205func (s *singletonContext) ModuleProvider(logicModule Module, provider AnyProviderKey) (any, bool) { 206 return s.context.ModuleProvider(logicModule, provider) 207} 208 209func (s *singletonContext) BlueprintFile(logicModule Module) string { 210 return s.context.BlueprintFile(logicModule) 211} 212 213func (s *singletonContext) error(err error) { 214 if err != nil { 215 s.errs = append(s.errs, err) 216 } 217} 218 219func (s *singletonContext) ModuleErrorf(logicModule Module, format string, 220 args ...interface{}) { 221 222 s.error(s.context.ModuleErrorf(logicModule, format, args...)) 223} 224 225func (s *singletonContext) Errorf(format string, args ...interface{}) { 226 // TODO: Make this not result in the error being printed as "internal error" 227 s.error(fmt.Errorf(format, args...)) 228} 229 230func (s *singletonContext) OtherModulePropertyErrorf(logicModule Module, property string, format string, 231 args ...interface{}) { 232 233 s.error(s.context.PropertyErrorf(logicModule, property, format, args...)) 234} 235 236func (s *singletonContext) Failed() bool { 237 return len(s.errs) > 0 238} 239 240func (s *singletonContext) Variable(pctx PackageContext, name, value string) { 241 s.scope.ReparentTo(pctx) 242 243 v, err := s.scope.AddLocalVariable(name, value) 244 if err != nil { 245 panic(err) 246 } 247 248 s.actionDefs.variables = append(s.actionDefs.variables, v) 249} 250 251func (s *singletonContext) Rule(pctx PackageContext, name string, 252 params RuleParams, argNames ...string) Rule { 253 254 s.scope.ReparentTo(pctx) 255 256 r, err := s.scope.AddLocalRule(name, ¶ms, argNames...) 257 if err != nil { 258 panic(err) 259 } 260 261 s.actionDefs.rules = append(s.actionDefs.rules, r) 262 263 return r 264} 265 266func (s *singletonContext) Build(pctx PackageContext, params BuildParams) { 267 s.scope.ReparentTo(pctx) 268 269 def, err := parseBuildParams(s.scope, ¶ms, map[string]string{ 270 "module_name": s.name, 271 "module_type": "singleton", 272 }) 273 if err != nil { 274 panic(err) 275 } 276 277 s.actionDefs.buildDefs = append(s.actionDefs.buildDefs, def) 278} 279 280func (s *singletonContext) Eval(pctx PackageContext, str string) (string, error) { 281 s.scope.ReparentTo(pctx) 282 283 ninjaStr, err := parseNinjaString(s.scope, str) 284 if err != nil { 285 return "", err 286 } 287 288 err = s.globals.addNinjaStringDeps(ninjaStr) 289 if err != nil { 290 return "", err 291 } 292 293 return s.globals.Eval(ninjaStr) 294} 295 296func (s *singletonContext) RequireNinjaVersion(major, minor, micro int) { 297 s.context.requireNinjaVersion(major, minor, micro) 298} 299 300func (s *singletonContext) SetOutDir(pctx PackageContext, value string) { 301 s.scope.ReparentTo(pctx) 302 303 ninjaValue, err := parseNinjaString(s.scope, value) 304 if err != nil { 305 panic(err) 306 } 307 308 s.context.setOutDir(ninjaValue) 309} 310 311func (s *singletonContext) AddSubninja(file string) { 312 s.context.subninjas = append(s.context.subninjas, file) 313} 314 315func (s *singletonContext) VisitAllModules(visit func(Module)) { 316 var visitingModule Module 317 defer func() { 318 if r := recover(); r != nil { 319 panic(newPanicErrorf(r, "VisitAllModules(%s) for module %s", 320 funcName(visit), s.context.moduleInfo[visitingModule])) 321 } 322 }() 323 324 s.context.VisitAllModules(func(m Module) { 325 visitingModule = m 326 visit(m) 327 }) 328} 329 330func (s *singletonContext) VisitAllModulesIf(pred func(Module) bool, 331 visit func(Module)) { 332 333 s.context.VisitAllModulesIf(pred, visit) 334} 335 336func (s *singletonContext) VisitDirectDeps(module Module, visit func(Module)) { 337 s.context.VisitDirectDeps(module, visit) 338} 339 340func (s *singletonContext) VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module)) { 341 s.context.VisitDirectDepsIf(module, pred, visit) 342} 343 344func (s *singletonContext) VisitDepsDepthFirst(module Module, 345 visit func(Module)) { 346 347 s.context.VisitDepsDepthFirst(module, visit) 348} 349 350func (s *singletonContext) VisitDepsDepthFirstIf(module Module, 351 pred func(Module) bool, visit func(Module)) { 352 353 s.context.VisitDepsDepthFirstIf(module, pred, visit) 354} 355 356func (s *singletonContext) PrimaryModule(module Module) Module { 357 return s.context.PrimaryModule(module) 358} 359 360func (s *singletonContext) FinalModule(module Module) Module { 361 return s.context.FinalModule(module) 362} 363 364func (s *singletonContext) VisitAllModuleVariants(module Module, visit func(Module)) { 365 s.context.VisitAllModuleVariants(module, visit) 366} 367 368func (s *singletonContext) AddNinjaFileDeps(deps ...string) { 369 s.ninjaFileDeps = append(s.ninjaFileDeps, deps...) 370} 371 372func (s *singletonContext) GlobWithDeps(pattern string, 373 excludes []string) ([]string, error) { 374 return s.context.glob(pattern, excludes) 375} 376 377func (s *singletonContext) Fs() pathtools.FileSystem { 378 return s.context.fs 379} 380 381func (s *singletonContext) ModuleVariantsFromName(referer Module, name string) []Module { 382 c := s.context 383 384 refererInfo := c.moduleInfo[referer] 385 if refererInfo == nil { 386 s.ModuleErrorf(referer, "could not find module %q", referer.Name()) 387 return nil 388 } 389 390 moduleGroup, exists := c.nameInterface.ModuleFromName(name, refererInfo.namespace()) 391 if !exists { 392 return nil 393 } 394 result := make([]Module, 0, len(moduleGroup.modules)) 395 for _, module := range moduleGroup.modules { 396 moduleInfo := module.module() 397 if moduleInfo != nil { 398 result = append(result, moduleInfo.logicModule) 399 } 400 } 401 return result 402} 403