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 | |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
| 23 | ) |
| 24 | |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame] | 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 | |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 30 | func registerPathDepsMutator(ctx RegisterMutatorsContext) { |
Colin Cross | 8a96280 | 2024-10-09 15:29:27 -0700 | [diff] [blame] | 31 | ctx.BottomUp("pathdeps", pathDepsMutator) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame] | 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"`. |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 36 | func pathDepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | 69a680f | 2024-01-29 13:18:43 -0800 | [diff] [blame] | 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 | } |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 42 | if !ctx.Module().Enabled(ctx) { |
| 43 | return |
| 44 | } |
Usta | 851a327 | 2022-01-05 23:42:33 -0500 | [diff] [blame] | 45 | props := ctx.Module().base().GetProperties() |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 46 | addPathDepsForProps(ctx, props) |
| 47 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 48 | |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 49 | func addPathDepsForProps(ctx BottomUpMutatorContext, props []interface{}) { |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame] | 50 | // Iterate through each property struct of the module extracting the contents of all properties |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 51 | // tagged with `android:"path"` or one of the variant-specifying tags. |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 52 | var pathProperties []string |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 53 | var pathDeviceFirstProperties []string |
Cole Faust | 18f03f1 | 2024-10-23 14:51:11 -0700 | [diff] [blame] | 54 | var pathDeviceFirstPrefer32Properties []string |
Cole Faust | a8bf946 | 2024-10-24 15:59:01 -0700 | [diff] [blame] | 55 | var pathDeviceFirstVendorProperties []string |
| 56 | var pathDeviceFirstVendorSharedProperties []string |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 57 | var pathDeviceCommonProperties []string |
| 58 | var pathCommonOsProperties []string |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 59 | for _, ps := range props { |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 60 | pathProperties = append(pathProperties, taggedPropertiesForPropertyStruct(ctx, ps, "path")...) |
| 61 | pathDeviceFirstProperties = append(pathDeviceFirstProperties, taggedPropertiesForPropertyStruct(ctx, ps, "path_device_first")...) |
Cole Faust | 18f03f1 | 2024-10-23 14:51:11 -0700 | [diff] [blame] | 62 | pathDeviceFirstPrefer32Properties = append(pathDeviceFirstPrefer32Properties, taggedPropertiesForPropertyStruct(ctx, ps, "path_device_first_prefer32")...) |
Cole Faust | a8bf946 | 2024-10-24 15:59:01 -0700 | [diff] [blame] | 63 | pathDeviceFirstVendorProperties = append(pathDeviceFirstVendorProperties, taggedPropertiesForPropertyStruct(ctx, ps, "path_device_first_vendor")...) |
| 64 | pathDeviceFirstVendorSharedProperties = append(pathDeviceFirstVendorSharedProperties, taggedPropertiesForPropertyStruct(ctx, ps, "path_device_first_vendor_shared")...) |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 65 | pathDeviceCommonProperties = append(pathDeviceCommonProperties, taggedPropertiesForPropertyStruct(ctx, ps, "path_device_common")...) |
| 66 | pathCommonOsProperties = append(pathCommonOsProperties, taggedPropertiesForPropertyStruct(ctx, ps, "path_common_os")...) |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 67 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 68 | |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame] | 69 | // Remove duplicates to avoid multiple dependencies. |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 70 | pathProperties = FirstUniqueStrings(pathProperties) |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 71 | pathDeviceFirstProperties = FirstUniqueStrings(pathDeviceFirstProperties) |
Cole Faust | 18f03f1 | 2024-10-23 14:51:11 -0700 | [diff] [blame] | 72 | pathDeviceFirstPrefer32Properties = FirstUniqueStrings(pathDeviceFirstPrefer32Properties) |
Cole Faust | a8bf946 | 2024-10-24 15:59:01 -0700 | [diff] [blame] | 73 | pathDeviceFirstVendorProperties = FirstUniqueStrings(pathDeviceFirstVendorProperties) |
| 74 | pathDeviceFirstVendorSharedProperties = FirstUniqueStrings(pathDeviceFirstVendorSharedProperties) |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 75 | pathDeviceCommonProperties = FirstUniqueStrings(pathDeviceCommonProperties) |
| 76 | pathCommonOsProperties = FirstUniqueStrings(pathCommonOsProperties) |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 77 | |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame] | 78 | // Add dependencies to anything that is a module reference. |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 79 | for _, s := range pathProperties { |
| 80 | if m, t := SrcIsModuleWithTag(s); m != "" { |
Paul Duffin | 40131a3 | 2021-07-09 17:10:35 +0100 | [diff] [blame] | 81 | ctx.AddDependency(ctx.Module(), sourceOrOutputDepTag(m, t), m) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 82 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 83 | } |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 84 | // For properties tagged "path_device_first", use the first arch device variant when adding |
| 85 | // dependencies. This allows host modules to have some properties that add dependencies on |
| 86 | // device modules. |
| 87 | for _, s := range pathDeviceFirstProperties { |
| 88 | if m, t := SrcIsModuleWithTag(s); m != "" { |
| 89 | ctx.AddVariationDependencies(ctx.Config().AndroidFirstDeviceTarget.Variations(), sourceOrOutputDepTag(m, t), m) |
| 90 | } |
| 91 | } |
Cole Faust | 18f03f1 | 2024-10-23 14:51:11 -0700 | [diff] [blame] | 92 | // properties tagged path_device_first_prefer32 get the first 32 bit target if one is available, |
| 93 | // otherwise they use the first 64 bit target |
| 94 | if len(pathDeviceFirstPrefer32Properties) > 0 { |
Cole Faust | b304ea9 | 2024-10-25 12:47:16 -0700 | [diff] [blame] | 95 | var targets []Target |
| 96 | if ctx.Config().IgnorePrefer32OnDevice() { |
| 97 | targets, _ = decodeMultilibTargets("first", ctx.Config().Targets[Android], false) |
| 98 | } else { |
| 99 | targets, _ = decodeMultilibTargets("first_prefer32", ctx.Config().Targets[Android], false) |
| 100 | } |
| 101 | if len(targets) == 0 { |
Cole Faust | 18f03f1 | 2024-10-23 14:51:11 -0700 | [diff] [blame] | 102 | ctx.ModuleErrorf("Could not find a first_prefer32 target") |
| 103 | } else { |
| 104 | for _, s := range pathDeviceFirstPrefer32Properties { |
| 105 | if m, t := SrcIsModuleWithTag(s); m != "" { |
Cole Faust | b304ea9 | 2024-10-25 12:47:16 -0700 | [diff] [blame] | 106 | ctx.AddVariationDependencies(targets[0].Variations(), sourceOrOutputDepTag(m, t), m) |
Cole Faust | 18f03f1 | 2024-10-23 14:51:11 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
Cole Faust | a8bf946 | 2024-10-24 15:59:01 -0700 | [diff] [blame] | 111 | // path_device_first_vendor is path_device_first + vendor variation |
| 112 | deviceFirstVendorVariations := ctx.Config().AndroidFirstDeviceTarget.Variations() |
| 113 | deviceFirstVendorVariations = append(deviceFirstVendorVariations, |
| 114 | blueprint.Variation{Mutator: "image", Variation: "vendor"}) |
| 115 | for _, s := range pathDeviceFirstVendorProperties { |
| 116 | if m, t := SrcIsModuleWithTag(s); m != "" { |
| 117 | ctx.AddVariationDependencies(deviceFirstVendorVariations, sourceOrOutputDepTag(m, t), m) |
| 118 | } |
| 119 | } |
| 120 | // path_device_first_vendor_shared is path_device_first_vendor + shared linkage variation |
| 121 | deviceFirstVendorSharedVariations := append(deviceFirstVendorVariations, |
| 122 | blueprint.Variation{Mutator: "link", Variation: "shared"}) |
| 123 | for _, s := range pathDeviceFirstVendorSharedProperties { |
| 124 | if m, t := SrcIsModuleWithTag(s); m != "" { |
| 125 | ctx.AddVariationDependencies(deviceFirstVendorSharedVariations, sourceOrOutputDepTag(m, t), m) |
| 126 | } |
| 127 | } |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 128 | // properties tagged "path_device_common" get the device common variant |
| 129 | for _, s := range pathDeviceCommonProperties { |
| 130 | if m, t := SrcIsModuleWithTag(s); m != "" { |
| 131 | ctx.AddVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), sourceOrOutputDepTag(m, t), m) |
| 132 | } |
| 133 | } |
Cole Faust | 18f03f1 | 2024-10-23 14:51:11 -0700 | [diff] [blame] | 134 | // properties tagged "path_common_os" get the CommonOs variant |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 135 | for _, s := range pathCommonOsProperties { |
| 136 | if m, t := SrcIsModuleWithTag(s); m != "" { |
| 137 | ctx.AddVariationDependencies([]blueprint.Variation{ |
| 138 | {Mutator: "os", Variation: "common_os"}, |
| 139 | {Mutator: "arch", Variation: ""}, |
| 140 | }, sourceOrOutputDepTag(m, t), m) |
| 141 | } |
| 142 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 145 | // taggedPropertiesForPropertyStruct uses the indexes of properties that are tagged with |
| 146 | // android:"tagValue" to extract all their values from a property struct, returning them as a single |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame] | 147 | // slice of strings. |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 148 | func taggedPropertiesForPropertyStruct(ctx BottomUpMutatorContext, ps interface{}, tagValue string) []string { |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 149 | v := reflect.ValueOf(ps) |
| 150 | if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { |
| 151 | panic(fmt.Errorf("type %s is not a pointer to a struct", v.Type())) |
| 152 | } |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame] | 153 | |
| 154 | // 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] | 155 | if v.IsNil() { |
| 156 | return nil |
| 157 | } |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame] | 158 | |
| 159 | // v is now the reflect.Value for the concrete property struct. |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 160 | v = v.Elem() |
| 161 | |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame] | 162 | // Get or create the list of indexes of properties that are tagged with `android:"path"`. |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 163 | pathPropertyIndexes := taggedPropertyIndexesForPropertyStruct(ps, tagValue) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 164 | |
| 165 | var ret []string |
| 166 | |
| 167 | for _, i := range pathPropertyIndexes { |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 168 | var values []reflect.Value |
| 169 | fieldsByIndex(v, i, &values) |
| 170 | for _, sv := range values { |
| 171 | if !sv.IsValid() { |
| 172 | // Skip properties inside a nil pointer. |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 173 | continue |
| 174 | } |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame] | 175 | |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 176 | // If the field is a non-nil pointer step into it. |
| 177 | if sv.Kind() == reflect.Ptr { |
| 178 | if sv.IsNil() { |
| 179 | continue |
| 180 | } |
| 181 | sv = sv.Elem() |
| 182 | } |
| 183 | |
| 184 | // Collect paths from all strings and slices of strings. |
| 185 | switch sv.Kind() { |
| 186 | case reflect.String: |
| 187 | ret = append(ret, sv.String()) |
| 188 | case reflect.Slice: |
| 189 | ret = append(ret, sv.Interface().([]string)...) |
Cole Faust | bdd8aee | 2024-03-14 14:33:02 -0700 | [diff] [blame] | 190 | case reflect.Struct: |
| 191 | intf := sv.Interface() |
| 192 | if configurable, ok := intf.(proptools.Configurable[string]); ok { |
Cole Faust | b78ce43 | 2024-04-04 10:55:19 -0700 | [diff] [blame] | 193 | ret = append(ret, configurable.GetOrDefault(ctx, "")) |
Cole Faust | bdd8aee | 2024-03-14 14:33:02 -0700 | [diff] [blame] | 194 | } else if configurable, ok := intf.(proptools.Configurable[[]string]); ok { |
Cole Faust | b78ce43 | 2024-04-04 10:55:19 -0700 | [diff] [blame] | 195 | ret = append(ret, configurable.GetOrDefault(ctx, nil)...) |
Cole Faust | bdd8aee | 2024-03-14 14:33:02 -0700 | [diff] [blame] | 196 | } else { |
| 197 | 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`, |
| 198 | v.Type().FieldByIndex(i).Name, v.Type(), sv.Type())) |
| 199 | } |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 200 | default: |
| 201 | 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`, |
| 202 | v.Type().FieldByIndex(i).Name, v.Type(), sv.Type())) |
| 203 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | return ret |
| 208 | } |
| 209 | |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 210 | // fieldsByIndex is similar to reflect.Value.FieldByIndex, but is more robust: it doesn't track |
| 211 | // nil pointers and it returns multiple values when there's slice of struct. |
| 212 | func fieldsByIndex(v reflect.Value, index []int, values *[]reflect.Value) { |
| 213 | // leaf case |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 214 | if len(index) == 1 { |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 215 | if isSliceOfStruct(v) { |
| 216 | for i := 0; i < v.Len(); i++ { |
| 217 | *values = append(*values, v.Index(i).Field(index[0])) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 218 | } |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 219 | } else { |
Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame] | 220 | // Dereference it if it's a pointer. |
| 221 | if v.Kind() == reflect.Ptr { |
| 222 | if v.IsNil() { |
| 223 | return |
| 224 | } |
| 225 | v = v.Elem() |
| 226 | } |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 227 | *values = append(*values, v.Field(index[0])) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 228 | } |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 229 | return |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 230 | } |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 231 | |
| 232 | // recursion |
| 233 | if v.Kind() == reflect.Ptr { |
| 234 | // don't track nil pointer |
| 235 | if v.IsNil() { |
| 236 | return |
| 237 | } |
| 238 | v = v.Elem() |
| 239 | } else if isSliceOfStruct(v) { |
| 240 | // do the recursion for all elements |
| 241 | for i := 0; i < v.Len(); i++ { |
| 242 | fieldsByIndex(v.Index(i).Field(index[0]), index[1:], values) |
| 243 | } |
| 244 | return |
| 245 | } |
| 246 | fieldsByIndex(v.Field(index[0]), index[1:], values) |
| 247 | return |
| 248 | } |
| 249 | |
| 250 | func isSliceOfStruct(v reflect.Value) bool { |
| 251 | return v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Struct |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | var pathPropertyIndexesCache OncePer |
| 255 | |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 256 | // taggedPropertyIndexesForPropertyStruct returns a list of all of the indexes of properties in |
| 257 | // property struct type that are tagged with `android:"tagValue"`. Each index is a []int suitable |
| 258 | // for passing to reflect.Value.FieldByIndex. The value is cached in a global cache by type and |
| 259 | // tagValue. |
| 260 | func taggedPropertyIndexesForPropertyStruct(ps interface{}, tagValue string) [][]int { |
| 261 | type pathPropertyIndexesOnceKey struct { |
| 262 | propStructType reflect.Type |
| 263 | tagValue string |
| 264 | } |
| 265 | key := NewCustomOnceKey(pathPropertyIndexesOnceKey{ |
| 266 | propStructType: reflect.TypeOf(ps), |
| 267 | tagValue: tagValue, |
| 268 | }) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 269 | return pathPropertyIndexesCache.Once(key, func() interface{} { |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 270 | return proptools.PropertyIndexesWithTag(ps, "android", tagValue) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 271 | }).([][]int) |
| 272 | } |