register.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // Copyright 2015 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. "fmt"
  17. "reflect"
  18. "github.com/google/blueprint"
  19. )
  20. // A sortable component is one whose registration order affects the order in which it is executed
  21. // and so affects the behavior of the build system. As a result it is important for the order in
  22. // which they are registered during tests to match the order used at runtime and so the test
  23. // infrastructure will sort them to match.
  24. //
  25. // The sortable components are mutators, singletons and pre-singletons. Module types are not
  26. // sortable because their order of registration does not affect the runtime behavior.
  27. type sortableComponent interface {
  28. // componentName returns the name of the component.
  29. //
  30. // Uniquely identifies the components within the set of components used at runtime and during
  31. // tests.
  32. componentName() string
  33. // registers this component in the supplied context.
  34. register(ctx *Context)
  35. }
  36. type sortableComponents []sortableComponent
  37. // registerAll registers all components in this slice with the supplied context.
  38. func (r sortableComponents) registerAll(ctx *Context) {
  39. for _, c := range r {
  40. c.register(ctx)
  41. }
  42. }
  43. type moduleType struct {
  44. name string
  45. factory ModuleFactory
  46. }
  47. func (t moduleType) register(ctx *Context) {
  48. ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory))
  49. }
  50. var moduleTypes []moduleType
  51. var moduleTypesForDocs = map[string]reflect.Value{}
  52. var moduleTypeByFactory = map[reflect.Value]string{}
  53. type singleton struct {
  54. // True if this should be registered as a pre-singleton, false otherwise.
  55. pre bool
  56. // True if this should be registered as a parallel singleton.
  57. parallel bool
  58. name string
  59. factory SingletonFactory
  60. }
  61. func newSingleton(name string, factory SingletonFactory, parallel bool) singleton {
  62. return singleton{pre: false, parallel: parallel, name: name, factory: factory}
  63. }
  64. func newPreSingleton(name string, factory SingletonFactory) singleton {
  65. return singleton{pre: true, parallel: false, name: name, factory: factory}
  66. }
  67. func (s singleton) componentName() string {
  68. return s.name
  69. }
  70. func (s singleton) register(ctx *Context) {
  71. adaptor := SingletonFactoryAdaptor(ctx, s.factory)
  72. if s.pre {
  73. ctx.RegisterPreSingletonType(s.name, adaptor)
  74. } else {
  75. ctx.RegisterSingletonType(s.name, adaptor, s.parallel)
  76. }
  77. }
  78. var _ sortableComponent = singleton{}
  79. var singletons sortableComponents
  80. var preSingletons sortableComponents
  81. type mutator struct {
  82. name string
  83. bottomUpMutator blueprint.BottomUpMutator
  84. topDownMutator blueprint.TopDownMutator
  85. transitionMutator blueprint.TransitionMutator
  86. parallel bool
  87. }
  88. var _ sortableComponent = &mutator{}
  89. type ModuleFactory func() Module
  90. // ModuleFactoryAdaptor wraps a ModuleFactory into a blueprint.ModuleFactory by converting a Module
  91. // into a blueprint.Module and a list of property structs
  92. func ModuleFactoryAdaptor(factory ModuleFactory) blueprint.ModuleFactory {
  93. return func() (blueprint.Module, []interface{}) {
  94. module := factory()
  95. return module, module.GetProperties()
  96. }
  97. }
  98. type SingletonFactory func() Singleton
  99. // SingletonFactoryAdaptor wraps a SingletonFactory into a blueprint.SingletonFactory by converting
  100. // a Singleton into a blueprint.Singleton
  101. func SingletonFactoryAdaptor(ctx *Context, factory SingletonFactory) blueprint.SingletonFactory {
  102. return func() blueprint.Singleton {
  103. singleton := factory()
  104. if makevars, ok := singleton.(SingletonMakeVarsProvider); ok {
  105. ctx.registerSingletonMakeVarsProvider(makevars)
  106. }
  107. return &singletonAdaptor{Singleton: singleton}
  108. }
  109. }
  110. func RegisterModuleType(name string, factory ModuleFactory) {
  111. moduleTypes = append(moduleTypes, moduleType{name, factory})
  112. RegisterModuleTypeForDocs(name, reflect.ValueOf(factory))
  113. }
  114. // RegisterModuleTypeForDocs associates a module type name with a reflect.Value of the factory
  115. // function that has documentation for the module type. It is normally called automatically
  116. // by RegisterModuleType, but can be called manually after RegisterModuleType in order to
  117. // override the factory method used for documentation, for example if the method passed to
  118. // RegisterModuleType was a lambda.
  119. func RegisterModuleTypeForDocs(name string, factory reflect.Value) {
  120. moduleTypesForDocs[name] = factory
  121. moduleTypeByFactory[factory] = name
  122. }
  123. func registerSingletonType(name string, factory SingletonFactory, parallel bool) {
  124. singletons = append(singletons, newSingleton(name, factory, parallel))
  125. }
  126. func RegisterSingletonType(name string, factory SingletonFactory) {
  127. registerSingletonType(name, factory, false)
  128. }
  129. func RegisterParallelSingletonType(name string, factory SingletonFactory) {
  130. registerSingletonType(name, factory, true)
  131. }
  132. func RegisterPreSingletonType(name string, factory SingletonFactory) {
  133. preSingletons = append(preSingletons, newPreSingleton(name, factory))
  134. }
  135. type Context struct {
  136. *blueprint.Context
  137. config Config
  138. }
  139. func NewContext(config Config) *Context {
  140. ctx := &Context{blueprint.NewContext(), config}
  141. ctx.SetSrcDir(absSrcDir)
  142. ctx.AddIncludeTags(config.IncludeTags()...)
  143. ctx.AddSourceRootDirs(config.SourceRootDirs()...)
  144. return ctx
  145. }
  146. // Helper function to register the module types used in bp2build and
  147. // api_bp2build.
  148. func registerModuleTypes(ctx *Context) {
  149. for _, t := range moduleTypes {
  150. t.register(ctx)
  151. }
  152. // Required for SingletonModule types, even though we are not using them.
  153. for _, t := range singletons {
  154. t.register(ctx)
  155. }
  156. }
  157. // RegisterForBazelConversion registers an alternate shadow pipeline of
  158. // singletons, module types and mutators to register for converting Blueprint
  159. // files to semantically equivalent BUILD files.
  160. func (ctx *Context) RegisterForBazelConversion() {
  161. registerModuleTypes(ctx)
  162. RegisterMutatorsForBazelConversion(ctx, bp2buildPreArchMutators)
  163. }
  164. // RegisterForApiBazelConversion is similar to RegisterForBazelConversion except that
  165. // it only generates API targets in the generated workspace
  166. func (ctx *Context) RegisterForApiBazelConversion() {
  167. registerModuleTypes(ctx)
  168. RegisterMutatorsForApiBazelConversion(ctx, bp2buildPreArchMutators)
  169. }
  170. // Register the pipeline of singletons, module types, and mutators for
  171. // generating build.ninja and other files for Kati, from Android.bp files.
  172. func (ctx *Context) Register() {
  173. preSingletons.registerAll(ctx)
  174. for _, t := range moduleTypes {
  175. t.register(ctx)
  176. }
  177. mutators := collateGloballyRegisteredMutators()
  178. mutators.registerAll(ctx)
  179. singletons := collateGloballyRegisteredSingletons()
  180. singletons.registerAll(ctx)
  181. }
  182. func (ctx *Context) Config() Config {
  183. return ctx.config
  184. }
  185. func (ctx *Context) registerSingletonMakeVarsProvider(makevars SingletonMakeVarsProvider) {
  186. registerSingletonMakeVarsProvider(ctx.config, makevars)
  187. }
  188. func collateGloballyRegisteredSingletons() sortableComponents {
  189. allSingletons := append(sortableComponents(nil), singletons...)
  190. allSingletons = append(allSingletons,
  191. singleton{pre: false, parallel: true, name: "bazeldeps", factory: BazelSingleton},
  192. // Register phony just before makevars so it can write out its phony rules as Make rules
  193. singleton{pre: false, parallel: false, name: "phony", factory: phonySingletonFactory},
  194. // Register makevars after other singletons so they can export values through makevars
  195. singleton{pre: false, parallel: false, name: "makevars", factory: makeVarsSingletonFunc},
  196. // Register env and ninjadeps last so that they can track all used environment variables and
  197. // Ninja file dependencies stored in the config.
  198. singleton{pre: false, parallel: false, name: "ninjadeps", factory: ninjaDepsSingletonFactory},
  199. )
  200. return allSingletons
  201. }
  202. func ModuleTypeFactories() map[string]ModuleFactory {
  203. ret := make(map[string]ModuleFactory)
  204. for _, t := range moduleTypes {
  205. ret[t.name] = t.factory
  206. }
  207. return ret
  208. }
  209. func ModuleTypeFactoriesForDocs() map[string]reflect.Value {
  210. return moduleTypesForDocs
  211. }
  212. func ModuleTypeByFactory() map[reflect.Value]string {
  213. return moduleTypeByFactory
  214. }
  215. // Interface for registering build components.
  216. //
  217. // Provided to allow registration of build components to be shared between the runtime
  218. // and test environments.
  219. type RegistrationContext interface {
  220. RegisterModuleType(name string, factory ModuleFactory)
  221. RegisterSingletonModuleType(name string, factory SingletonModuleFactory)
  222. RegisterParallelSingletonModuleType(name string, factory SingletonModuleFactory)
  223. RegisterPreSingletonType(name string, factory SingletonFactory)
  224. RegisterParallelSingletonType(name string, factory SingletonFactory)
  225. RegisterSingletonType(name string, factory SingletonFactory)
  226. PreArchMutators(f RegisterMutatorFunc)
  227. // Register pre arch mutators that are hard coded into mutator.go.
  228. //
  229. // Only registers mutators for testing, is a noop on the InitRegistrationContext.
  230. HardCodedPreArchMutators(f RegisterMutatorFunc)
  231. PreDepsMutators(f RegisterMutatorFunc)
  232. PostDepsMutators(f RegisterMutatorFunc)
  233. FinalDepsMutators(f RegisterMutatorFunc)
  234. }
  235. // Used to register build components from an init() method, e.g.
  236. //
  237. // init() {
  238. // RegisterBuildComponents(android.InitRegistrationContext)
  239. // }
  240. //
  241. // func RegisterBuildComponents(ctx android.RegistrationContext) {
  242. // ctx.RegisterModuleType(...)
  243. // ...
  244. // }
  245. //
  246. // Extracting the actual registration into a separate RegisterBuildComponents(ctx) function
  247. // allows it to be used to initialize test context, e.g.
  248. //
  249. // ctx := android.NewTestContext(config)
  250. // RegisterBuildComponents(ctx)
  251. var InitRegistrationContext RegistrationContext = &initRegistrationContext{
  252. moduleTypes: make(map[string]ModuleFactory),
  253. singletonTypes: make(map[string]SingletonFactory),
  254. preSingletonTypes: make(map[string]SingletonFactory),
  255. }
  256. // Make sure the TestContext implements RegistrationContext.
  257. var _ RegistrationContext = (*TestContext)(nil)
  258. type initRegistrationContext struct {
  259. moduleTypes map[string]ModuleFactory
  260. singletonTypes map[string]SingletonFactory
  261. preSingletonTypes map[string]SingletonFactory
  262. moduleTypesForDocs map[string]reflect.Value
  263. }
  264. func (ctx *initRegistrationContext) RegisterModuleType(name string, factory ModuleFactory) {
  265. if _, present := ctx.moduleTypes[name]; present {
  266. panic(fmt.Sprintf("module type %q is already registered", name))
  267. }
  268. ctx.moduleTypes[name] = factory
  269. RegisterModuleType(name, factory)
  270. RegisterModuleTypeForDocs(name, reflect.ValueOf(factory))
  271. }
  272. func (ctx *initRegistrationContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) {
  273. ctx.registerSingletonModuleType(name, factory, false)
  274. }
  275. func (ctx *initRegistrationContext) RegisterParallelSingletonModuleType(name string, factory SingletonModuleFactory) {
  276. ctx.registerSingletonModuleType(name, factory, true)
  277. }
  278. func (ctx *initRegistrationContext) registerSingletonModuleType(name string, factory SingletonModuleFactory, parallel bool) {
  279. s, m := SingletonModuleFactoryAdaptor(name, factory)
  280. ctx.registerSingletonType(name, s, parallel)
  281. ctx.RegisterModuleType(name, m)
  282. // Overwrite moduleTypesForDocs with the original factory instead of the lambda returned by
  283. // SingletonModuleFactoryAdaptor so that docs can find the module type documentation on the
  284. // factory method.
  285. RegisterModuleTypeForDocs(name, reflect.ValueOf(factory))
  286. }
  287. func (ctx *initRegistrationContext) registerSingletonType(name string, factory SingletonFactory, parallel bool) {
  288. if _, present := ctx.singletonTypes[name]; present {
  289. panic(fmt.Sprintf("singleton type %q is already registered", name))
  290. }
  291. ctx.singletonTypes[name] = factory
  292. registerSingletonType(name, factory, parallel)
  293. }
  294. func (ctx *initRegistrationContext) RegisterSingletonType(name string, factory SingletonFactory) {
  295. ctx.registerSingletonType(name, factory, false)
  296. }
  297. func (ctx *initRegistrationContext) RegisterParallelSingletonType(name string, factory SingletonFactory) {
  298. ctx.registerSingletonType(name, factory, true)
  299. }
  300. func (ctx *initRegistrationContext) RegisterPreSingletonType(name string, factory SingletonFactory) {
  301. if _, present := ctx.preSingletonTypes[name]; present {
  302. panic(fmt.Sprintf("pre singleton type %q is already registered", name))
  303. }
  304. ctx.preSingletonTypes[name] = factory
  305. RegisterPreSingletonType(name, factory)
  306. }
  307. func (ctx *initRegistrationContext) PreArchMutators(f RegisterMutatorFunc) {
  308. PreArchMutators(f)
  309. }
  310. func (ctx *initRegistrationContext) HardCodedPreArchMutators(_ RegisterMutatorFunc) {
  311. // Nothing to do as the mutators are hard code in preArch in mutator.go
  312. }
  313. func (ctx *initRegistrationContext) PreDepsMutators(f RegisterMutatorFunc) {
  314. PreDepsMutators(f)
  315. }
  316. func (ctx *initRegistrationContext) PostDepsMutators(f RegisterMutatorFunc) {
  317. PostDepsMutators(f)
  318. }
  319. func (ctx *initRegistrationContext) FinalDepsMutators(f RegisterMutatorFunc) {
  320. FinalDepsMutators(f)
  321. }