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