defaults.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. "reflect"
  17. "github.com/google/blueprint"
  18. "github.com/google/blueprint/proptools"
  19. )
  20. type defaultsDependencyTag struct {
  21. blueprint.BaseDependencyTag
  22. }
  23. var DefaultsDepTag defaultsDependencyTag
  24. type defaultsProperties struct {
  25. Defaults []string
  26. }
  27. type DefaultableModuleBase struct {
  28. defaultsProperties defaultsProperties
  29. defaultableProperties []interface{}
  30. defaultableVariableProperties interface{}
  31. // The optional hook to call after any defaults have been applied.
  32. hook DefaultableHook
  33. }
  34. func (d *DefaultableModuleBase) defaults() *defaultsProperties {
  35. return &d.defaultsProperties
  36. }
  37. func (d *DefaultableModuleBase) setProperties(props []interface{}, variableProperties interface{}) {
  38. d.defaultableProperties = props
  39. d.defaultableVariableProperties = variableProperties
  40. }
  41. func (d *DefaultableModuleBase) SetDefaultableHook(hook DefaultableHook) {
  42. d.hook = hook
  43. }
  44. func (d *DefaultableModuleBase) callHookIfAvailable(ctx DefaultableHookContext) {
  45. if d.hook != nil {
  46. d.hook(ctx)
  47. }
  48. }
  49. // Interface that must be supported by any module to which defaults can be applied.
  50. type Defaultable interface {
  51. // Get a pointer to the struct containing the Defaults property.
  52. defaults() *defaultsProperties
  53. // Set the property structures into which defaults will be added.
  54. setProperties(props []interface{}, variableProperties interface{})
  55. // Apply defaults from the supplied Defaults to the property structures supplied to
  56. // setProperties(...).
  57. applyDefaults(TopDownMutatorContext, []Defaults)
  58. // Set the hook to be called after any defaults have been applied.
  59. //
  60. // Should be used in preference to a AddLoadHook when the behavior of the load
  61. // hook is dependent on properties supplied in the Android.bp file.
  62. SetDefaultableHook(hook DefaultableHook)
  63. // Call the hook if specified.
  64. callHookIfAvailable(context DefaultableHookContext)
  65. }
  66. type DefaultableModule interface {
  67. Module
  68. Defaultable
  69. }
  70. var _ Defaultable = (*DefaultableModuleBase)(nil)
  71. func InitDefaultableModule(module DefaultableModule) {
  72. if module.base().module == nil {
  73. panic("InitAndroidModule must be called before InitDefaultableModule")
  74. }
  75. module.setProperties(module.GetProperties(), module.base().variableProperties)
  76. module.AddProperties(module.defaults())
  77. }
  78. // A restricted subset of context methods, similar to LoadHookContext.
  79. type DefaultableHookContext interface {
  80. EarlyModuleContext
  81. CreateModule(ModuleFactory, ...interface{}) Module
  82. AddMissingDependencies(missingDeps []string)
  83. }
  84. type DefaultableHook func(ctx DefaultableHookContext)
  85. // The Defaults_visibility property.
  86. type DefaultsVisibilityProperties struct {
  87. // Controls the visibility of the defaults module itself.
  88. Defaults_visibility []string
  89. }
  90. type DefaultsModuleBase struct {
  91. DefaultableModuleBase
  92. // Included to support setting bazel_module.label for multiple Soong modules to the same Bazel
  93. // target. This is primarily useful for modules that were architecture specific and instead are
  94. // handled in Bazel as a select().
  95. BazelModuleBase
  96. }
  97. // The common pattern for defaults modules is to register separate instances of
  98. // the xxxProperties structs in the AddProperties calls, rather than reusing the
  99. // ones inherited from Module.
  100. //
  101. // The effect is that e.g. myDefaultsModuleInstance.base().xxxProperties won't
  102. // contain the values that have been set for the defaults module. Rather, to
  103. // retrieve the values it is necessary to iterate over properties(). E.g. to get
  104. // the commonProperties instance that have the real values:
  105. //
  106. // d := myModule.(Defaults)
  107. // for _, props := range d.properties() {
  108. // if cp, ok := props.(*commonProperties); ok {
  109. // ... access property values in cp ...
  110. // }
  111. // }
  112. //
  113. // The rationale is that the properties on a defaults module apply to the
  114. // defaultable modules using it, not to the defaults module itself. E.g. setting
  115. // the "enabled" property false makes inheriting modules disabled by default,
  116. // rather than disabling the defaults module itself.
  117. type Defaults interface {
  118. Defaultable
  119. // Although this function is unused it is actually needed to ensure that only modules that embed
  120. // DefaultsModuleBase will type-assert to the Defaults interface.
  121. isDefaults() bool
  122. // Get the structures containing the properties for which defaults can be provided.
  123. properties() []interface{}
  124. productVariableProperties() interface{}
  125. }
  126. func (d *DefaultsModuleBase) isDefaults() bool {
  127. return true
  128. }
  129. type DefaultsModule interface {
  130. Module
  131. Defaults
  132. Bazelable
  133. }
  134. func (d *DefaultsModuleBase) properties() []interface{} {
  135. return d.defaultableProperties
  136. }
  137. func (d *DefaultsModuleBase) productVariableProperties() interface{} {
  138. return d.defaultableVariableProperties
  139. }
  140. func (d *DefaultsModuleBase) GenerateAndroidBuildActions(ctx ModuleContext) {}
  141. // ConvertWithBp2build to fulfill Bazelable interface; however, at this time defaults module are
  142. // *NOT* converted with bp2build
  143. func (defaultable *DefaultsModuleBase) ConvertWithBp2build(ctx TopDownMutatorContext) {}
  144. func InitDefaultsModule(module DefaultsModule) {
  145. commonProperties := &commonProperties{}
  146. module.AddProperties(
  147. &hostAndDeviceProperties{},
  148. commonProperties,
  149. &ApexProperties{},
  150. &distProperties{})
  151. // Bazel module must be initialized _before_ Defaults to be included in cc_defaults module.
  152. InitBazelModule(module)
  153. initAndroidModuleBase(module)
  154. initProductVariableModule(module)
  155. initArchModule(module)
  156. InitDefaultableModule(module)
  157. // Add properties that will not have defaults applied to them.
  158. base := module.base()
  159. defaultsVisibility := &DefaultsVisibilityProperties{}
  160. module.AddProperties(&base.nameProperties, defaultsVisibility)
  161. // Unlike non-defaults modules the visibility property is not stored in m.base().commonProperties.
  162. // Instead it is stored in a separate instance of commonProperties created above so clear the
  163. // existing list of properties.
  164. clearVisibilityProperties(module)
  165. // The defaults_visibility property controls the visibility of a defaults module so it must be
  166. // set as the primary property, which also adds it to the list.
  167. setPrimaryVisibilityProperty(module, "defaults_visibility", &defaultsVisibility.Defaults_visibility)
  168. // The visibility property needs to be checked (but not parsed) by the visibility module during
  169. // its checking phase and parsing phase so add it to the list as a normal property.
  170. AddVisibilityProperty(module, "visibility", &commonProperties.Visibility)
  171. // The applicable licenses property for defaults is 'licenses'.
  172. setPrimaryLicensesProperty(module, "licenses", &commonProperties.Licenses)
  173. }
  174. var _ Defaults = (*DefaultsModuleBase)(nil)
  175. // applyNamespacedVariableDefaults only runs in bp2build mode for
  176. // defaultable/defaults modules. Its purpose is to merge namespaced product
  177. // variable props from defaults deps, even if those defaults are custom module
  178. // types created from soong_config_module_type, e.g. one that's wrapping a
  179. // cc_defaults or java_defaults.
  180. func applyNamespacedVariableDefaults(defaultDep Defaults, ctx TopDownMutatorContext) {
  181. var dep, b Bazelable
  182. dep, ok := defaultDep.(Bazelable)
  183. if !ok {
  184. if depMod, ok := defaultDep.(Module); ok {
  185. // Track that this dependency hasn't been converted to bp2build yet.
  186. ctx.AddUnconvertedBp2buildDep(depMod.Name())
  187. return
  188. } else {
  189. panic("Expected default dep to be a Module.")
  190. }
  191. }
  192. b, ok = ctx.Module().(Bazelable)
  193. if !ok {
  194. return
  195. }
  196. // namespacedVariableProps is a map from namespaces (e.g. acme, android,
  197. // vendor_foo) to a slice of soong_config_variable struct pointers,
  198. // containing properties for that particular module.
  199. src := dep.namespacedVariableProps()
  200. dst := b.namespacedVariableProps()
  201. if dst == nil {
  202. dst = make(namespacedVariableProperties)
  203. }
  204. // Propagate all soong_config_variable structs from the dep. We'll merge the
  205. // actual property values later in variable.go.
  206. for namespace := range src {
  207. if dst[namespace] == nil {
  208. dst[namespace] = []interface{}{}
  209. }
  210. for _, i := range src[namespace] {
  211. dst[namespace] = append(dst[namespace], i)
  212. }
  213. }
  214. b.setNamespacedVariableProps(dst)
  215. }
  216. func (defaultable *DefaultableModuleBase) applyDefaults(ctx TopDownMutatorContext,
  217. defaultsList []Defaults) {
  218. for _, defaults := range defaultsList {
  219. if ctx.Config().runningAsBp2Build {
  220. applyNamespacedVariableDefaults(defaults, ctx)
  221. }
  222. for _, prop := range defaultable.defaultableProperties {
  223. if prop == defaultable.defaultableVariableProperties {
  224. defaultable.applyDefaultVariableProperties(ctx, defaults, prop)
  225. } else {
  226. defaultable.applyDefaultProperties(ctx, defaults, prop)
  227. }
  228. }
  229. }
  230. }
  231. // Product variable properties need special handling, the type of the filtered product variable
  232. // property struct may not be identical between the defaults module and the defaultable module.
  233. // Use PrependMatchingProperties to apply whichever properties match.
  234. func (defaultable *DefaultableModuleBase) applyDefaultVariableProperties(ctx TopDownMutatorContext,
  235. defaults Defaults, defaultableProp interface{}) {
  236. if defaultableProp == nil {
  237. return
  238. }
  239. defaultsProp := defaults.productVariableProperties()
  240. if defaultsProp == nil {
  241. return
  242. }
  243. dst := []interface{}{
  244. defaultableProp,
  245. // Put an empty copy of the src properties into dst so that properties in src that are not in dst
  246. // don't cause a "failed to find property to extend" error.
  247. proptools.CloneEmptyProperties(reflect.ValueOf(defaultsProp)).Interface(),
  248. }
  249. err := proptools.PrependMatchingProperties(dst, defaultsProp, nil)
  250. if err != nil {
  251. if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
  252. ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
  253. } else {
  254. panic(err)
  255. }
  256. }
  257. }
  258. func (defaultable *DefaultableModuleBase) applyDefaultProperties(ctx TopDownMutatorContext,
  259. defaults Defaults, defaultableProp interface{}) {
  260. for _, def := range defaults.properties() {
  261. if proptools.TypeEqual(defaultableProp, def) {
  262. err := proptools.PrependProperties(defaultableProp, def, nil)
  263. if err != nil {
  264. if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
  265. ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
  266. } else {
  267. panic(err)
  268. }
  269. }
  270. }
  271. }
  272. }
  273. func RegisterDefaultsPreArchMutators(ctx RegisterMutatorsContext) {
  274. ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel()
  275. ctx.TopDown("defaults", defaultsMutator).Parallel()
  276. }
  277. func defaultsDepsMutator(ctx BottomUpMutatorContext) {
  278. if defaultable, ok := ctx.Module().(Defaultable); ok {
  279. ctx.AddDependency(ctx.Module(), DefaultsDepTag, defaultable.defaults().Defaults...)
  280. }
  281. }
  282. func defaultsMutator(ctx TopDownMutatorContext) {
  283. if defaultable, ok := ctx.Module().(Defaultable); ok {
  284. if len(defaultable.defaults().Defaults) > 0 {
  285. var defaultsList []Defaults
  286. seen := make(map[Defaults]bool)
  287. ctx.WalkDeps(func(module, parent Module) bool {
  288. if ctx.OtherModuleDependencyTag(module) == DefaultsDepTag {
  289. if defaults, ok := module.(Defaults); ok {
  290. if !seen[defaults] {
  291. seen[defaults] = true
  292. defaultsList = append(defaultsList, defaults)
  293. return len(defaults.defaults().Defaults) > 0
  294. }
  295. } else {
  296. ctx.PropertyErrorf("defaults", "module %s is not an defaults module",
  297. ctx.OtherModuleName(module))
  298. }
  299. }
  300. return false
  301. })
  302. defaultable.applyDefaults(ctx, defaultsList)
  303. }
  304. defaultable.callHookIfAvailable(ctx)
  305. }
  306. }