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) { |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 36 | props := ctx.Module().base().generalProperties |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 37 | |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 38 | // Iterate through each property struct of the module extracting the contents of all properties |
| 39 | // tagged with `android:"path"`. |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 40 | var pathProperties []string |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 41 | for _, ps := range props { |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 42 | pathProperties = append(pathProperties, pathPropertiesForPropertyStruct(ps)...) |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 43 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 44 | |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 45 | // Remove duplicates to avoid multiple dependencies. |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 46 | pathProperties = FirstUniqueStrings(pathProperties) |
| 47 | |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 48 | // Add dependencies to anything that is a module reference. |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 49 | for _, s := range pathProperties { |
| 50 | if m, t := SrcIsModuleWithTag(s); m != "" { |
| 51 | ctx.AddDependency(ctx.Module(), sourceOrOutputDepTag(t), m) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 52 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 56 | // pathPropertiesForPropertyStruct uses the indexes of properties that are tagged with |
| 57 | // android:"path" to extract all their values from a property struct, returning them as a single |
| 58 | // slice of strings. |
| 59 | func pathPropertiesForPropertyStruct(ps interface{}) []string { |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 60 | v := reflect.ValueOf(ps) |
| 61 | if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { |
| 62 | panic(fmt.Errorf("type %s is not a pointer to a struct", v.Type())) |
| 63 | } |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 64 | |
| 65 | // 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] | 66 | if v.IsNil() { |
| 67 | return nil |
| 68 | } |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 69 | |
| 70 | // v is now the reflect.Value for the concrete property struct. |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 71 | v = v.Elem() |
| 72 | |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 73 | // 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] | 74 | pathPropertyIndexes := pathPropertyIndexesForPropertyStruct(ps) |
| 75 | |
| 76 | var ret []string |
| 77 | |
| 78 | for _, i := range pathPropertyIndexes { |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 79 | // Turn an index into a field. |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 80 | sv := fieldByIndex(v, i) |
| 81 | if !sv.IsValid() { |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 82 | // Skip properties inside a nil pointer. |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 83 | continue |
| 84 | } |
| 85 | |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 86 | // If the field is a non-nil pointer step into it. |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 87 | if sv.Kind() == reflect.Ptr { |
| 88 | if sv.IsNil() { |
| 89 | continue |
| 90 | } |
| 91 | sv = sv.Elem() |
| 92 | } |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 93 | |
| 94 | // Collect paths from all strings and slices of strings. |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 95 | switch sv.Kind() { |
| 96 | case reflect.String: |
| 97 | ret = append(ret, sv.String()) |
| 98 | case reflect.Slice: |
| 99 | ret = append(ret, sv.Interface().([]string)...) |
| 100 | default: |
| 101 | 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`, |
| 102 | v.Type().FieldByIndex(i).Name, v.Type(), sv.Type())) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return ret |
| 107 | } |
| 108 | |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 109 | // fieldByIndex is like reflect.Value.FieldByIndex, but returns an invalid reflect.Value when |
| 110 | // traversing a nil pointer to a struct. |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 111 | func fieldByIndex(v reflect.Value, index []int) reflect.Value { |
| 112 | if len(index) == 1 { |
| 113 | return v.Field(index[0]) |
| 114 | } |
| 115 | for _, x := range index { |
| 116 | if v.Kind() == reflect.Ptr { |
| 117 | if v.IsNil() { |
| 118 | return reflect.Value{} |
| 119 | } |
| 120 | v = v.Elem() |
| 121 | } |
| 122 | v = v.Field(x) |
| 123 | } |
| 124 | return v |
| 125 | } |
| 126 | |
| 127 | var pathPropertyIndexesCache OncePer |
| 128 | |
Colin Cross | 11c89c0 | 2020-11-19 14:27:44 -0800 | [diff] [blame^] | 129 | // pathPropertyIndexesForPropertyStruct returns a list of all of the indexes of properties in |
| 130 | // property struct type that are tagged with `android:"path"`. Each index is a []int suitable for |
| 131 | // 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] | 132 | func pathPropertyIndexesForPropertyStruct(ps interface{}) [][]int { |
| 133 | key := NewCustomOnceKey(reflect.TypeOf(ps)) |
| 134 | return pathPropertyIndexesCache.Once(key, func() interface{} { |
| 135 | return proptools.PropertyIndexesWithTag(ps, "android", "path") |
| 136 | }).([][]int) |
| 137 | } |