singleton.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. package android
  15. import (
  16. "github.com/google/blueprint"
  17. )
  18. // SingletonContext
  19. type SingletonContext interface {
  20. Config() Config
  21. DeviceConfig() DeviceConfig
  22. ModuleName(module blueprint.Module) string
  23. ModuleDir(module blueprint.Module) string
  24. ModuleSubDir(module blueprint.Module) string
  25. ModuleType(module blueprint.Module) string
  26. BlueprintFile(module blueprint.Module) string
  27. // ModuleVariantsFromName returns the list of module variants named `name` in the same namespace as `referer` enforcing visibility rules.
  28. // Allows generating build actions for `referer` based on the metadata for `name` deferred until the singleton context.
  29. ModuleVariantsFromName(referer Module, name string) []Module
  30. // ModuleProvider returns the value, if any, for the provider for a module. If the value for the
  31. // provider was not set it returns the zero value of the type of the provider, which means the
  32. // return value can always be type-asserted to the type of the provider. The return value should
  33. // always be considered read-only. It panics if called before the appropriate mutator or
  34. // GenerateBuildActions pass for the provider on the module.
  35. ModuleProvider(module blueprint.Module, provider blueprint.ProviderKey) interface{}
  36. // ModuleHasProvider returns true if the provider for the given module has been set.
  37. ModuleHasProvider(module blueprint.Module, provider blueprint.ProviderKey) bool
  38. ModuleErrorf(module blueprint.Module, format string, args ...interface{})
  39. Errorf(format string, args ...interface{})
  40. Failed() bool
  41. Variable(pctx PackageContext, name, value string)
  42. Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
  43. Build(pctx PackageContext, params BuildParams)
  44. // Phony creates a Make-style phony rule, a rule with no commands that can depend on other
  45. // phony rules or real files. Phony can be called on the same name multiple times to add
  46. // additional dependencies.
  47. Phony(name string, deps ...Path)
  48. RequireNinjaVersion(major, minor, micro int)
  49. // SetOutDir sets the value of the top-level "builddir" Ninja variable
  50. // that controls where Ninja stores its build log files. This value can be
  51. // set at most one time for a single build, later calls are ignored.
  52. SetOutDir(pctx PackageContext, value string)
  53. // Eval takes a string with embedded ninja variables, and returns a string
  54. // with all of the variables recursively expanded. Any variables references
  55. // are expanded in the scope of the PackageContext.
  56. Eval(pctx PackageContext, ninjaStr string) (string, error)
  57. VisitAllModulesBlueprint(visit func(blueprint.Module))
  58. VisitAllModules(visit func(Module))
  59. VisitAllModulesIf(pred func(Module) bool, visit func(Module))
  60. VisitDirectDeps(module Module, visit func(Module))
  61. VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module))
  62. // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
  63. VisitDepsDepthFirst(module Module, visit func(Module))
  64. // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
  65. VisitDepsDepthFirstIf(module Module, pred func(Module) bool,
  66. visit func(Module))
  67. VisitAllModuleVariants(module Module, visit func(Module))
  68. PrimaryModule(module Module) Module
  69. FinalModule(module Module) Module
  70. AddNinjaFileDeps(deps ...string)
  71. // GlobWithDeps returns a list of files that match the specified pattern but do not match any
  72. // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
  73. // builder whenever a file matching the pattern as added or removed, without rerunning if a
  74. // file that does not match the pattern is added to a searched directory.
  75. GlobWithDeps(pattern string, excludes []string) ([]string, error)
  76. }
  77. type singletonAdaptor struct {
  78. Singleton
  79. buildParams []BuildParams
  80. ruleParams map[blueprint.Rule]blueprint.RuleParams
  81. }
  82. var _ testBuildProvider = (*singletonAdaptor)(nil)
  83. func (s *singletonAdaptor) GenerateBuildActions(ctx blueprint.SingletonContext) {
  84. sctx := &singletonContextAdaptor{SingletonContext: ctx}
  85. if sctx.Config().captureBuild {
  86. sctx.ruleParams = make(map[blueprint.Rule]blueprint.RuleParams)
  87. }
  88. s.Singleton.GenerateBuildActions(sctx)
  89. s.buildParams = sctx.buildParams
  90. s.ruleParams = sctx.ruleParams
  91. }
  92. func (s *singletonAdaptor) BuildParamsForTests() []BuildParams {
  93. return s.buildParams
  94. }
  95. func (s *singletonAdaptor) RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams {
  96. return s.ruleParams
  97. }
  98. type Singleton interface {
  99. GenerateBuildActions(SingletonContext)
  100. }
  101. type singletonContextAdaptor struct {
  102. blueprint.SingletonContext
  103. buildParams []BuildParams
  104. ruleParams map[blueprint.Rule]blueprint.RuleParams
  105. }
  106. func (s *singletonContextAdaptor) Config() Config {
  107. return s.SingletonContext.Config().(Config)
  108. }
  109. func (s *singletonContextAdaptor) DeviceConfig() DeviceConfig {
  110. return DeviceConfig{s.Config().deviceConfig}
  111. }
  112. func (s *singletonContextAdaptor) Variable(pctx PackageContext, name, value string) {
  113. s.SingletonContext.Variable(pctx.PackageContext, name, value)
  114. }
  115. func (s *singletonContextAdaptor) Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule {
  116. if s.Config().UseRemoteBuild() {
  117. if params.Pool == nil {
  118. // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by goma/RBE, restrict
  119. // jobs to the local parallelism value
  120. params.Pool = localPool
  121. } else if params.Pool == remotePool {
  122. // remotePool is a fake pool used to identify rule that are supported for remoting. If the rule's
  123. // pool is the remotePool, replace with nil so that ninja runs it at NINJA_REMOTE_NUM_JOBS
  124. // parallelism.
  125. params.Pool = nil
  126. }
  127. }
  128. rule := s.SingletonContext.Rule(pctx.PackageContext, name, params, argNames...)
  129. if s.Config().captureBuild {
  130. s.ruleParams[rule] = params
  131. }
  132. return rule
  133. }
  134. func (s *singletonContextAdaptor) Build(pctx PackageContext, params BuildParams) {
  135. if s.Config().captureBuild {
  136. s.buildParams = append(s.buildParams, params)
  137. }
  138. bparams := convertBuildParams(params)
  139. err := validateBuildParams(bparams)
  140. if err != nil {
  141. s.Errorf("%s: build parameter validation failed: %s", s.Name(), err.Error())
  142. }
  143. s.SingletonContext.Build(pctx.PackageContext, bparams)
  144. }
  145. func (s *singletonContextAdaptor) Phony(name string, deps ...Path) {
  146. addPhony(s.Config(), name, deps...)
  147. }
  148. func (s *singletonContextAdaptor) SetOutDir(pctx PackageContext, value string) {
  149. s.SingletonContext.SetOutDir(pctx.PackageContext, value)
  150. }
  151. func (s *singletonContextAdaptor) Eval(pctx PackageContext, ninjaStr string) (string, error) {
  152. return s.SingletonContext.Eval(pctx.PackageContext, ninjaStr)
  153. }
  154. // visitAdaptor wraps a visit function that takes an android.Module parameter into
  155. // a function that takes an blueprint.Module parameter and only calls the visit function if the
  156. // blueprint.Module is an android.Module.
  157. func visitAdaptor(visit func(Module)) func(blueprint.Module) {
  158. return func(module blueprint.Module) {
  159. if aModule, ok := module.(Module); ok {
  160. visit(aModule)
  161. }
  162. }
  163. }
  164. // predAdaptor wraps a pred function that takes an android.Module parameter
  165. // into a function that takes an blueprint.Module parameter and only calls the visit function if the
  166. // blueprint.Module is an android.Module, otherwise returns false.
  167. func predAdaptor(pred func(Module) bool) func(blueprint.Module) bool {
  168. return func(module blueprint.Module) bool {
  169. if aModule, ok := module.(Module); ok {
  170. return pred(aModule)
  171. } else {
  172. return false
  173. }
  174. }
  175. }
  176. func (s *singletonContextAdaptor) VisitAllModulesBlueprint(visit func(blueprint.Module)) {
  177. s.SingletonContext.VisitAllModules(visit)
  178. }
  179. func (s *singletonContextAdaptor) VisitAllModules(visit func(Module)) {
  180. s.SingletonContext.VisitAllModules(visitAdaptor(visit))
  181. }
  182. func (s *singletonContextAdaptor) VisitAllModulesIf(pred func(Module) bool, visit func(Module)) {
  183. s.SingletonContext.VisitAllModulesIf(predAdaptor(pred), visitAdaptor(visit))
  184. }
  185. func (s *singletonContextAdaptor) VisitDirectDeps(module Module, visit func(Module)) {
  186. s.SingletonContext.VisitDirectDeps(module, visitAdaptor(visit))
  187. }
  188. func (s *singletonContextAdaptor) VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module)) {
  189. s.SingletonContext.VisitDirectDepsIf(module, predAdaptor(pred), visitAdaptor(visit))
  190. }
  191. func (s *singletonContextAdaptor) VisitDepsDepthFirst(module Module, visit func(Module)) {
  192. s.SingletonContext.VisitDepsDepthFirst(module, visitAdaptor(visit))
  193. }
  194. func (s *singletonContextAdaptor) VisitDepsDepthFirstIf(module Module, pred func(Module) bool, visit func(Module)) {
  195. s.SingletonContext.VisitDepsDepthFirstIf(module, predAdaptor(pred), visitAdaptor(visit))
  196. }
  197. func (s *singletonContextAdaptor) VisitAllModuleVariants(module Module, visit func(Module)) {
  198. s.SingletonContext.VisitAllModuleVariants(module, visitAdaptor(visit))
  199. }
  200. func (s *singletonContextAdaptor) PrimaryModule(module Module) Module {
  201. return s.SingletonContext.PrimaryModule(module).(Module)
  202. }
  203. func (s *singletonContextAdaptor) FinalModule(module Module) Module {
  204. return s.SingletonContext.FinalModule(module).(Module)
  205. }
  206. func (s *singletonContextAdaptor) ModuleVariantsFromName(referer Module, name string) []Module {
  207. // get qualified module name for visibility enforcement
  208. qualified := createQualifiedModuleName(s.ModuleName(referer), s.ModuleDir(referer))
  209. modules := s.SingletonContext.ModuleVariantsFromName(referer, name)
  210. result := make([]Module, 0, len(modules))
  211. for _, m := range modules {
  212. if module, ok := m.(Module); ok {
  213. // enforce visibility
  214. depName := s.ModuleName(module)
  215. depDir := s.ModuleDir(module)
  216. depQualified := qualifiedModuleName{depDir, depName}
  217. // Targets are always visible to other targets in their own package.
  218. if depQualified.pkg != qualified.pkg {
  219. rule := effectiveVisibilityRules(s.Config(), depQualified)
  220. if !rule.matches(qualified) {
  221. s.ModuleErrorf(referer, "module %q references %q which is not visible to this module\nYou may need to add %q to its visibility",
  222. referer.Name(), depQualified, "//"+s.ModuleDir(referer))
  223. continue
  224. }
  225. }
  226. result = append(result, module)
  227. }
  228. }
  229. return result
  230. }