member_type.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright (C) 2021 The Android Open Source Project
  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 sdk
  15. import (
  16. "reflect"
  17. "android/soong/android"
  18. "github.com/google/blueprint/proptools"
  19. )
  20. // Contains information about the sdk properties that list sdk members by type, e.g.
  21. // Java_header_libs.
  22. type sdkMemberTypeListProperty struct {
  23. // getter for the list of member names
  24. getter func(properties interface{}) []string
  25. // setter for the list of member names
  26. setter func(properties interface{}, list []string)
  27. // the type of member referenced in the list
  28. memberType android.SdkMemberType
  29. // the dependency tag used for items in this list that can be used to determine the memberType
  30. // for a resolved dependency.
  31. dependencyTag android.SdkMemberDependencyTag
  32. }
  33. func (p *sdkMemberTypeListProperty) propertyName() string {
  34. return p.memberType.SdkPropertyName()
  35. }
  36. // Cache of dynamically generated dynamicSdkMemberTypes objects. The key is the pointer
  37. // to a slice of SdkMemberType instances held in android.SdkMemberTypes.
  38. var dynamicSdkMemberTypesMap android.OncePer
  39. // A dynamically generated set of member list properties and associated structure type.
  40. type dynamicSdkMemberTypes struct {
  41. // The dynamically generated structure type.
  42. //
  43. // Contains one []string exported field for each android.SdkMemberTypes. The name of the field
  44. // is the exported form of the value returned by SdkMemberType.SdkPropertyName().
  45. propertiesStructType reflect.Type
  46. // Information about each of the member type specific list properties.
  47. memberTypeListProperties []*sdkMemberTypeListProperty
  48. memberTypeToProperty map[android.SdkMemberType]*sdkMemberTypeListProperty
  49. }
  50. func (d *dynamicSdkMemberTypes) createMemberTypeListProperties() interface{} {
  51. return reflect.New(d.propertiesStructType).Interface()
  52. }
  53. func getDynamicSdkMemberTypes(key android.OnceKey, registeredTypes []android.SdkMemberType) *dynamicSdkMemberTypes {
  54. // Get the cached value, creating new instance if necessary.
  55. return dynamicSdkMemberTypesMap.Once(key, func() interface{} {
  56. return createDynamicSdkMemberTypes(registeredTypes)
  57. }).(*dynamicSdkMemberTypes)
  58. }
  59. // Create the dynamicSdkMemberTypes from the list of registered member types.
  60. //
  61. // A struct is created which contains one exported field per member type corresponding to
  62. // the SdkMemberType.SdkPropertyName() value.
  63. //
  64. // A list of sdkMemberTypeListProperty instances is created, one per member type that provides:
  65. // * a reference to the member type.
  66. // * a getter for the corresponding field in the properties struct.
  67. // * a dependency tag that identifies the member type of a resolved dependency.
  68. func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynamicSdkMemberTypes {
  69. var listProperties []*sdkMemberTypeListProperty
  70. memberTypeToProperty := map[android.SdkMemberType]*sdkMemberTypeListProperty{}
  71. var fields []reflect.StructField
  72. // Iterate over the member types creating StructField and sdkMemberTypeListProperty objects.
  73. nextFieldIndex := 0
  74. for _, memberType := range sdkMemberTypes {
  75. p := memberType.SdkPropertyName()
  76. var getter func(properties interface{}) []string
  77. var setter func(properties interface{}, list []string)
  78. if memberType.RequiresBpProperty() {
  79. // Create a dynamic exported field for the member type's property.
  80. fields = append(fields, reflect.StructField{
  81. Name: proptools.FieldNameForProperty(p),
  82. Type: reflect.TypeOf([]string{}),
  83. Tag: `android:"arch_variant"`,
  84. })
  85. // Copy the field index for use in the getter func as using the loop variable directly will
  86. // cause all funcs to use the last value.
  87. fieldIndex := nextFieldIndex
  88. nextFieldIndex += 1
  89. getter = func(properties interface{}) []string {
  90. // The properties is expected to be of the following form (where
  91. // <Module_types> is the name of an SdkMemberType.SdkPropertyName().
  92. // properties *struct {<Module_types> []string, ....}
  93. //
  94. // Although it accesses the field by index the following reflection code is equivalent to:
  95. // *properties.<Module_types>
  96. //
  97. list := reflect.ValueOf(properties).Elem().Field(fieldIndex).Interface().([]string)
  98. return list
  99. }
  100. setter = func(properties interface{}, list []string) {
  101. // The properties is expected to be of the following form (where
  102. // <Module_types> is the name of an SdkMemberType.SdkPropertyName().
  103. // properties *struct {<Module_types> []string, ....}
  104. //
  105. // Although it accesses the field by index the following reflection code is equivalent to:
  106. // *properties.<Module_types> = list
  107. //
  108. reflect.ValueOf(properties).Elem().Field(fieldIndex).Set(reflect.ValueOf(list))
  109. }
  110. }
  111. // Create an sdkMemberTypeListProperty for the member type.
  112. memberListProperty := &sdkMemberTypeListProperty{
  113. getter: getter,
  114. setter: setter,
  115. memberType: memberType,
  116. // Dependencies added directly from member properties are always exported.
  117. dependencyTag: android.DependencyTagForSdkMemberType(memberType, true),
  118. }
  119. memberTypeToProperty[memberType] = memberListProperty
  120. listProperties = append(listProperties, memberListProperty)
  121. }
  122. // Create a dynamic struct from the collated fields.
  123. propertiesStructType := reflect.StructOf(fields)
  124. return &dynamicSdkMemberTypes{
  125. memberTypeListProperties: listProperties,
  126. memberTypeToProperty: memberTypeToProperty,
  127. propertiesStructType: propertiesStructType,
  128. }
  129. }