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