path_properties.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2019 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/proptools"
  19. )
  20. // This file implements support for automatically adding dependencies on any module referenced
  21. // with the ":module" module reference syntax in a property that is annotated with `android:"path"`.
  22. // The dependency is used by android.PathForModuleSrc to convert the module reference into the path
  23. // to the output file of the referenced module.
  24. func registerPathDepsMutator(ctx RegisterMutatorsContext) {
  25. ctx.BottomUp("pathdeps", pathDepsMutator).Parallel()
  26. }
  27. // The pathDepsMutator automatically adds dependencies on any module that is listed with the
  28. // ":module" module reference syntax in a property that is tagged with `android:"path"`.
  29. func pathDepsMutator(ctx BottomUpMutatorContext) {
  30. props := ctx.Module().base().GetProperties()
  31. addPathDepsForProps(ctx, props)
  32. }
  33. func addPathDepsForProps(ctx BottomUpMutatorContext, props []interface{}) {
  34. // Iterate through each property struct of the module extracting the contents of all properties
  35. // tagged with `android:"path"`.
  36. var pathProperties []string
  37. for _, ps := range props {
  38. pathProperties = append(pathProperties, pathPropertiesForPropertyStruct(ps)...)
  39. }
  40. // Remove duplicates to avoid multiple dependencies.
  41. pathProperties = FirstUniqueStrings(pathProperties)
  42. // Add dependencies to anything that is a module reference.
  43. for _, s := range pathProperties {
  44. if m, t := SrcIsModuleWithTag(s); m != "" {
  45. ctx.AddDependency(ctx.Module(), sourceOrOutputDepTag(m, t), m)
  46. }
  47. }
  48. }
  49. // pathPropertiesForPropertyStruct uses the indexes of properties that are tagged with
  50. // android:"path" to extract all their values from a property struct, returning them as a single
  51. // slice of strings.
  52. func pathPropertiesForPropertyStruct(ps interface{}) []string {
  53. v := reflect.ValueOf(ps)
  54. if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
  55. panic(fmt.Errorf("type %s is not a pointer to a struct", v.Type()))
  56. }
  57. // If the property struct is a nil pointer it can't have any paths set in it.
  58. if v.IsNil() {
  59. return nil
  60. }
  61. // v is now the reflect.Value for the concrete property struct.
  62. v = v.Elem()
  63. // Get or create the list of indexes of properties that are tagged with `android:"path"`.
  64. pathPropertyIndexes := pathPropertyIndexesForPropertyStruct(ps)
  65. var ret []string
  66. for _, i := range pathPropertyIndexes {
  67. var values []reflect.Value
  68. fieldsByIndex(v, i, &values)
  69. for _, sv := range values {
  70. if !sv.IsValid() {
  71. // Skip properties inside a nil pointer.
  72. continue
  73. }
  74. // If the field is a non-nil pointer step into it.
  75. if sv.Kind() == reflect.Ptr {
  76. if sv.IsNil() {
  77. continue
  78. }
  79. sv = sv.Elem()
  80. }
  81. // Collect paths from all strings and slices of strings.
  82. switch sv.Kind() {
  83. case reflect.String:
  84. ret = append(ret, sv.String())
  85. case reflect.Slice:
  86. ret = append(ret, sv.Interface().([]string)...)
  87. default:
  88. panic(fmt.Errorf(`field %s in type %s has tag android:"path" but is not a string or slice of strings, it is a %s`,
  89. v.Type().FieldByIndex(i).Name, v.Type(), sv.Type()))
  90. }
  91. }
  92. }
  93. return ret
  94. }
  95. // fieldsByIndex is similar to reflect.Value.FieldByIndex, but is more robust: it doesn't track
  96. // nil pointers and it returns multiple values when there's slice of struct.
  97. func fieldsByIndex(v reflect.Value, index []int, values *[]reflect.Value) {
  98. // leaf case
  99. if len(index) == 1 {
  100. if isSliceOfStruct(v) {
  101. for i := 0; i < v.Len(); i++ {
  102. *values = append(*values, v.Index(i).Field(index[0]))
  103. }
  104. } else {
  105. // Dereference it if it's a pointer.
  106. if v.Kind() == reflect.Ptr {
  107. if v.IsNil() {
  108. return
  109. }
  110. v = v.Elem()
  111. }
  112. *values = append(*values, v.Field(index[0]))
  113. }
  114. return
  115. }
  116. // recursion
  117. if v.Kind() == reflect.Ptr {
  118. // don't track nil pointer
  119. if v.IsNil() {
  120. return
  121. }
  122. v = v.Elem()
  123. } else if isSliceOfStruct(v) {
  124. // do the recursion for all elements
  125. for i := 0; i < v.Len(); i++ {
  126. fieldsByIndex(v.Index(i).Field(index[0]), index[1:], values)
  127. }
  128. return
  129. }
  130. fieldsByIndex(v.Field(index[0]), index[1:], values)
  131. return
  132. }
  133. func isSliceOfStruct(v reflect.Value) bool {
  134. return v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Struct
  135. }
  136. var pathPropertyIndexesCache OncePer
  137. // pathPropertyIndexesForPropertyStruct returns a list of all of the indexes of properties in
  138. // property struct type that are tagged with `android:"path"`. Each index is a []int suitable for
  139. // passing to reflect.Value.FieldByIndex. The value is cached in a global cache by type.
  140. func pathPropertyIndexesForPropertyStruct(ps interface{}) [][]int {
  141. key := NewCustomOnceKey(reflect.TypeOf(ps))
  142. return pathPropertyIndexesCache.Once(key, func() interface{} {
  143. return proptools.PropertyIndexesWithTag(ps, "android", "path")
  144. }).([][]int)
  145. }