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 15package android 16 17import ( 18 "fmt" 19 "reflect" 20 21 "github.com/google/blueprint" 22 "github.com/google/blueprint/proptools" 23) 24 25// This file implements support for automatically adding dependencies on any module referenced 26// with the ":module" module reference syntax in a property that is annotated with `android:"path"`. 27// The dependency is used by android.PathForModuleSrc to convert the module reference into the path 28// to the output file of the referenced module. 29 30func registerPathDepsMutator(ctx RegisterMutatorsContext) { 31 ctx.BottomUp("pathdeps", pathDepsMutator) 32} 33 34// The pathDepsMutator automatically adds dependencies on any module that is listed with the 35// ":module" module reference syntax in a property that is tagged with `android:"path"`. 36func pathDepsMutator(ctx BottomUpMutatorContext) { 37 if _, ok := ctx.Module().(DefaultsModule); ok { 38 // Defaults modules shouldn't have dependencies added for path properties, they have already been 39 // squashed into the real modules. 40 return 41 } 42 if !ctx.Module().Enabled(ctx) { 43 return 44 } 45 props := ctx.Module().base().GetProperties() 46 addPathDepsForProps(ctx, props) 47} 48 49func addPathDepsForProps(ctx BottomUpMutatorContext, props []interface{}) { 50 // Iterate through each property struct of the module extracting the contents of all properties 51 // tagged with `android:"path"` or one of the variant-specifying tags. 52 var pathProperties []string 53 var pathDeviceFirstProperties []string 54 var pathDeviceFirstPrefer32Properties []string 55 var pathDeviceCommonProperties []string 56 var pathCommonOsProperties []string 57 for _, ps := range props { 58 pathProperties = append(pathProperties, taggedPropertiesForPropertyStruct(ctx, ps, "path")...) 59 pathDeviceFirstProperties = append(pathDeviceFirstProperties, taggedPropertiesForPropertyStruct(ctx, ps, "path_device_first")...) 60 pathDeviceFirstPrefer32Properties = append(pathDeviceFirstPrefer32Properties, taggedPropertiesForPropertyStruct(ctx, ps, "path_device_first_prefer32")...) 61 pathDeviceCommonProperties = append(pathDeviceCommonProperties, taggedPropertiesForPropertyStruct(ctx, ps, "path_device_common")...) 62 pathCommonOsProperties = append(pathCommonOsProperties, taggedPropertiesForPropertyStruct(ctx, ps, "path_common_os")...) 63 } 64 65 // Remove duplicates to avoid multiple dependencies. 66 pathProperties = FirstUniqueStrings(pathProperties) 67 pathDeviceFirstProperties = FirstUniqueStrings(pathDeviceFirstProperties) 68 pathDeviceFirstPrefer32Properties = FirstUniqueStrings(pathDeviceFirstPrefer32Properties) 69 pathDeviceCommonProperties = FirstUniqueStrings(pathDeviceCommonProperties) 70 pathCommonOsProperties = FirstUniqueStrings(pathCommonOsProperties) 71 72 // Add dependencies to anything that is a module reference. 73 for _, s := range pathProperties { 74 if m, t := SrcIsModuleWithTag(s); m != "" { 75 ctx.AddDependency(ctx.Module(), sourceOrOutputDepTag(m, t), m) 76 } 77 } 78 // For properties tagged "path_device_first", use the first arch device variant when adding 79 // dependencies. This allows host modules to have some properties that add dependencies on 80 // device modules. 81 for _, s := range pathDeviceFirstProperties { 82 if m, t := SrcIsModuleWithTag(s); m != "" { 83 ctx.AddVariationDependencies(ctx.Config().AndroidFirstDeviceTarget.Variations(), sourceOrOutputDepTag(m, t), m) 84 } 85 } 86 // properties tagged path_device_first_prefer32 get the first 32 bit target if one is available, 87 // otherwise they use the first 64 bit target 88 if len(pathDeviceFirstPrefer32Properties) > 0 { 89 var targets []Target 90 if ctx.Config().IgnorePrefer32OnDevice() { 91 targets, _ = decodeMultilibTargets("first", ctx.Config().Targets[Android], false) 92 } else { 93 targets, _ = decodeMultilibTargets("first_prefer32", ctx.Config().Targets[Android], false) 94 } 95 if len(targets) == 0 { 96 ctx.ModuleErrorf("Could not find a first_prefer32 target") 97 } else { 98 for _, s := range pathDeviceFirstPrefer32Properties { 99 if m, t := SrcIsModuleWithTag(s); m != "" { 100 ctx.AddVariationDependencies(targets[0].Variations(), sourceOrOutputDepTag(m, t), m) 101 } 102 } 103 } 104 } 105 // properties tagged "path_device_common" get the device common variant 106 for _, s := range pathDeviceCommonProperties { 107 if m, t := SrcIsModuleWithTag(s); m != "" { 108 ctx.AddVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), sourceOrOutputDepTag(m, t), m) 109 } 110 } 111 // properties tagged "path_common_os" get the CommonOs variant 112 for _, s := range pathCommonOsProperties { 113 if m, t := SrcIsModuleWithTag(s); m != "" { 114 ctx.AddVariationDependencies([]blueprint.Variation{ 115 {Mutator: "os", Variation: "common_os"}, 116 {Mutator: "arch", Variation: ""}, 117 }, sourceOrOutputDepTag(m, t), m) 118 } 119 } 120} 121 122// taggedPropertiesForPropertyStruct uses the indexes of properties that are tagged with 123// android:"tagValue" to extract all their values from a property struct, returning them as a single 124// slice of strings. 125func taggedPropertiesForPropertyStruct(ctx BottomUpMutatorContext, ps interface{}, tagValue string) []string { 126 v := reflect.ValueOf(ps) 127 if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { 128 panic(fmt.Errorf("type %s is not a pointer to a struct", v.Type())) 129 } 130 131 // If the property struct is a nil pointer it can't have any paths set in it. 132 if v.IsNil() { 133 return nil 134 } 135 136 // v is now the reflect.Value for the concrete property struct. 137 v = v.Elem() 138 139 // Get or create the list of indexes of properties that are tagged with `android:"path"`. 140 pathPropertyIndexes := taggedPropertyIndexesForPropertyStruct(ps, tagValue) 141 142 var ret []string 143 144 for _, i := range pathPropertyIndexes { 145 var values []reflect.Value 146 fieldsByIndex(v, i, &values) 147 for _, sv := range values { 148 if !sv.IsValid() { 149 // Skip properties inside a nil pointer. 150 continue 151 } 152 153 // If the field is a non-nil pointer step into it. 154 if sv.Kind() == reflect.Ptr { 155 if sv.IsNil() { 156 continue 157 } 158 sv = sv.Elem() 159 } 160 161 // Collect paths from all strings and slices of strings. 162 switch sv.Kind() { 163 case reflect.String: 164 ret = append(ret, sv.String()) 165 case reflect.Slice: 166 ret = append(ret, sv.Interface().([]string)...) 167 case reflect.Struct: 168 intf := sv.Interface() 169 if configurable, ok := intf.(proptools.Configurable[string]); ok { 170 ret = append(ret, configurable.GetOrDefault(ctx, "")) 171 } else if configurable, ok := intf.(proptools.Configurable[[]string]); ok { 172 ret = append(ret, configurable.GetOrDefault(ctx, nil)...) 173 } else { 174 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`, 175 v.Type().FieldByIndex(i).Name, v.Type(), sv.Type())) 176 } 177 default: 178 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`, 179 v.Type().FieldByIndex(i).Name, v.Type(), sv.Type())) 180 } 181 } 182 } 183 184 return ret 185} 186 187// fieldsByIndex is similar to reflect.Value.FieldByIndex, but is more robust: it doesn't track 188// nil pointers and it returns multiple values when there's slice of struct. 189func fieldsByIndex(v reflect.Value, index []int, values *[]reflect.Value) { 190 // leaf case 191 if len(index) == 1 { 192 if isSliceOfStruct(v) { 193 for i := 0; i < v.Len(); i++ { 194 *values = append(*values, v.Index(i).Field(index[0])) 195 } 196 } else { 197 // Dereference it if it's a pointer. 198 if v.Kind() == reflect.Ptr { 199 if v.IsNil() { 200 return 201 } 202 v = v.Elem() 203 } 204 *values = append(*values, v.Field(index[0])) 205 } 206 return 207 } 208 209 // recursion 210 if v.Kind() == reflect.Ptr { 211 // don't track nil pointer 212 if v.IsNil() { 213 return 214 } 215 v = v.Elem() 216 } else if isSliceOfStruct(v) { 217 // do the recursion for all elements 218 for i := 0; i < v.Len(); i++ { 219 fieldsByIndex(v.Index(i).Field(index[0]), index[1:], values) 220 } 221 return 222 } 223 fieldsByIndex(v.Field(index[0]), index[1:], values) 224 return 225} 226 227func isSliceOfStruct(v reflect.Value) bool { 228 return v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Struct 229} 230 231var pathPropertyIndexesCache OncePer 232 233// taggedPropertyIndexesForPropertyStruct returns a list of all of the indexes of properties in 234// property struct type that are tagged with `android:"tagValue"`. Each index is a []int suitable 235// for passing to reflect.Value.FieldByIndex. The value is cached in a global cache by type and 236// tagValue. 237func taggedPropertyIndexesForPropertyStruct(ps interface{}, tagValue string) [][]int { 238 type pathPropertyIndexesOnceKey struct { 239 propStructType reflect.Type 240 tagValue string 241 } 242 key := NewCustomOnceKey(pathPropertyIndexesOnceKey{ 243 propStructType: reflect.TypeOf(ps), 244 tagValue: tagValue, 245 }) 246 return pathPropertyIndexesCache.Once(key, func() interface{} { 247 return proptools.PropertyIndexesWithTag(ps, "android", tagValue) 248 }).([][]int) 249} 250