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 | |
| 24 | func registerPathDepsMutator(ctx RegisterMutatorsContext) { |
| 25 | ctx.BottomUp("pathdeps", pathDepsMutator).Parallel() |
| 26 | } |
| 27 | |
| 28 | // The pathDepsMutator automatically adds dependencies on any module that is listed with ":module" syntax in a |
| 29 | // property that is tagged with android:"path". |
| 30 | func pathDepsMutator(ctx BottomUpMutatorContext) { |
| 31 | m := ctx.Module().(Module) |
| 32 | if m == nil { |
| 33 | return |
| 34 | } |
| 35 | |
Colin Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 36 | props := m.base().generalProperties |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 37 | |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 38 | var pathProperties []string |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 39 | for _, ps := range props { |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 40 | pathProperties = append(pathProperties, pathPropertiesForPropertyStruct(ctx, ps)...) |
| 41 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 42 | |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 43 | pathProperties = FirstUniqueStrings(pathProperties) |
| 44 | |
| 45 | for _, s := range pathProperties { |
| 46 | if m, t := SrcIsModuleWithTag(s); m != "" { |
| 47 | ctx.AddDependency(ctx.Module(), sourceOrOutputDepTag(t), m) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 48 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | // pathPropertiesForPropertyStruct uses the indexes of properties that are tagged with android:"path" to extract |
| 53 | // all their values from a property struct, returning them as a single slice of strings.. |
| 54 | func pathPropertiesForPropertyStruct(ctx BottomUpMutatorContext, ps interface{}) []string { |
| 55 | v := reflect.ValueOf(ps) |
| 56 | if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { |
| 57 | panic(fmt.Errorf("type %s is not a pointer to a struct", v.Type())) |
| 58 | } |
| 59 | if v.IsNil() { |
| 60 | return nil |
| 61 | } |
| 62 | v = v.Elem() |
| 63 | |
| 64 | pathPropertyIndexes := pathPropertyIndexesForPropertyStruct(ps) |
| 65 | |
| 66 | var ret []string |
| 67 | |
| 68 | for _, i := range pathPropertyIndexes { |
| 69 | sv := fieldByIndex(v, i) |
| 70 | if !sv.IsValid() { |
| 71 | continue |
| 72 | } |
| 73 | |
| 74 | if sv.Kind() == reflect.Ptr { |
| 75 | if sv.IsNil() { |
| 76 | continue |
| 77 | } |
| 78 | sv = sv.Elem() |
| 79 | } |
| 80 | switch sv.Kind() { |
| 81 | case reflect.String: |
| 82 | ret = append(ret, sv.String()) |
| 83 | case reflect.Slice: |
| 84 | ret = append(ret, sv.Interface().([]string)...) |
| 85 | default: |
| 86 | 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`, |
| 87 | v.Type().FieldByIndex(i).Name, v.Type(), sv.Type())) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return ret |
| 92 | } |
| 93 | |
| 94 | // fieldByIndex is like reflect.Value.FieldByIndex, but returns an invalid reflect.Value when traversing a nil pointer |
| 95 | // to a struct. |
| 96 | func fieldByIndex(v reflect.Value, index []int) reflect.Value { |
| 97 | if len(index) == 1 { |
| 98 | return v.Field(index[0]) |
| 99 | } |
| 100 | for _, x := range index { |
| 101 | if v.Kind() == reflect.Ptr { |
| 102 | if v.IsNil() { |
| 103 | return reflect.Value{} |
| 104 | } |
| 105 | v = v.Elem() |
| 106 | } |
| 107 | v = v.Field(x) |
| 108 | } |
| 109 | return v |
| 110 | } |
| 111 | |
| 112 | var pathPropertyIndexesCache OncePer |
| 113 | |
| 114 | // pathPropertyIndexesForPropertyStruct returns a list of all of the indexes of properties in property struct type that |
| 115 | // are tagged with android:"path". Each index is a []int suitable for passing to reflect.Value.FieldByIndex. The value |
| 116 | // is cached in a global cache by type. |
| 117 | func pathPropertyIndexesForPropertyStruct(ps interface{}) [][]int { |
| 118 | key := NewCustomOnceKey(reflect.TypeOf(ps)) |
| 119 | return pathPropertyIndexesCache.Once(key, func() interface{} { |
| 120 | return proptools.PropertyIndexesWithTag(ps, "android", "path") |
| 121 | }).([][]int) |
| 122 | } |