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