blob: 853e5a9a7319a69f2de06095a82eb9300fb73466 [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
Colin Cross11c89c02020-11-19 14:27:44 -080024// 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 Cross1b488422019-03-04 22:33:56 -080029func registerPathDepsMutator(ctx RegisterMutatorsContext) {
30 ctx.BottomUp("pathdeps", pathDepsMutator).Parallel()
31}
32
Colin Cross11c89c02020-11-19 14:27:44 -080033// 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 Cross1b488422019-03-04 22:33:56 -080035func pathDepsMutator(ctx BottomUpMutatorContext) {
Colin Cross11c89c02020-11-19 14:27:44 -080036 props := ctx.Module().base().generalProperties
Colin Cross1b488422019-03-04 22:33:56 -080037
Colin Cross11c89c02020-11-19 14:27:44 -080038 // Iterate through each property struct of the module extracting the contents of all properties
39 // tagged with `android:"path"`.
Colin Cross527f3e52019-07-15 13:35:21 -070040 var pathProperties []string
Colin Cross1b488422019-03-04 22:33:56 -080041 for _, ps := range props {
Colin Cross11c89c02020-11-19 14:27:44 -080042 pathProperties = append(pathProperties, pathPropertiesForPropertyStruct(ps)...)
Colin Cross527f3e52019-07-15 13:35:21 -070043 }
Colin Cross1b488422019-03-04 22:33:56 -080044
Colin Cross11c89c02020-11-19 14:27:44 -080045 // Remove duplicates to avoid multiple dependencies.
Colin Cross527f3e52019-07-15 13:35:21 -070046 pathProperties = FirstUniqueStrings(pathProperties)
47
Colin Cross11c89c02020-11-19 14:27:44 -080048 // Add dependencies to anything that is a module reference.
Colin Cross527f3e52019-07-15 13:35:21 -070049 for _, s := range pathProperties {
50 if m, t := SrcIsModuleWithTag(s); m != "" {
51 ctx.AddDependency(ctx.Module(), sourceOrOutputDepTag(t), m)
Colin Cross1b488422019-03-04 22:33:56 -080052 }
Colin Cross1b488422019-03-04 22:33:56 -080053 }
54}
55
Colin Cross11c89c02020-11-19 14:27:44 -080056// 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.
59func pathPropertiesForPropertyStruct(ps interface{}) []string {
Colin Cross1b488422019-03-04 22:33:56 -080060 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 Cross11c89c02020-11-19 14:27:44 -080064
65 // If the property struct is a nil pointer it can't have any paths set in it.
Colin Cross1b488422019-03-04 22:33:56 -080066 if v.IsNil() {
67 return nil
68 }
Colin Cross11c89c02020-11-19 14:27:44 -080069
70 // v is now the reflect.Value for the concrete property struct.
Colin Cross1b488422019-03-04 22:33:56 -080071 v = v.Elem()
72
Colin Cross11c89c02020-11-19 14:27:44 -080073 // Get or create the list of indexes of properties that are tagged with `android:"path"`.
Colin Cross1b488422019-03-04 22:33:56 -080074 pathPropertyIndexes := pathPropertyIndexesForPropertyStruct(ps)
75
76 var ret []string
77
78 for _, i := range pathPropertyIndexes {
Jiyong Park66dd5c02021-02-24 01:22:57 +090079 var values []reflect.Value
80 fieldsByIndex(v, i, &values)
81 for _, sv := range values {
82 if !sv.IsValid() {
83 // Skip properties inside a nil pointer.
Colin Cross1b488422019-03-04 22:33:56 -080084 continue
85 }
Colin Cross11c89c02020-11-19 14:27:44 -080086
Jiyong Park66dd5c02021-02-24 01:22:57 +090087 // If the field is a non-nil pointer step into it.
88 if sv.Kind() == reflect.Ptr {
89 if sv.IsNil() {
90 continue
91 }
92 sv = sv.Elem()
93 }
94
95 // Collect paths from all strings and slices of strings.
96 switch sv.Kind() {
97 case reflect.String:
98 ret = append(ret, sv.String())
99 case reflect.Slice:
100 ret = append(ret, sv.Interface().([]string)...)
101 default:
102 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`,
103 v.Type().FieldByIndex(i).Name, v.Type(), sv.Type()))
104 }
Colin Cross1b488422019-03-04 22:33:56 -0800105 }
106 }
107
108 return ret
109}
110
Jiyong Park66dd5c02021-02-24 01:22:57 +0900111// fieldsByIndex is similar to reflect.Value.FieldByIndex, but is more robust: it doesn't track
112// nil pointers and it returns multiple values when there's slice of struct.
113func fieldsByIndex(v reflect.Value, index []int, values *[]reflect.Value) {
114 // leaf case
Colin Cross1b488422019-03-04 22:33:56 -0800115 if len(index) == 1 {
Jiyong Park66dd5c02021-02-24 01:22:57 +0900116 if isSliceOfStruct(v) {
117 for i := 0; i < v.Len(); i++ {
118 *values = append(*values, v.Index(i).Field(index[0]))
Colin Cross1b488422019-03-04 22:33:56 -0800119 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900120 } else {
121 *values = append(*values, v.Field(index[0]))
Colin Cross1b488422019-03-04 22:33:56 -0800122 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900123 return
Colin Cross1b488422019-03-04 22:33:56 -0800124 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900125
126 // recursion
127 if v.Kind() == reflect.Ptr {
128 // don't track nil pointer
129 if v.IsNil() {
130 return
131 }
132 v = v.Elem()
133 } else if isSliceOfStruct(v) {
134 // do the recursion for all elements
135 for i := 0; i < v.Len(); i++ {
136 fieldsByIndex(v.Index(i).Field(index[0]), index[1:], values)
137 }
138 return
139 }
140 fieldsByIndex(v.Field(index[0]), index[1:], values)
141 return
142}
143
144func isSliceOfStruct(v reflect.Value) bool {
145 return v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Struct
Colin Cross1b488422019-03-04 22:33:56 -0800146}
147
148var pathPropertyIndexesCache OncePer
149
Colin Cross11c89c02020-11-19 14:27:44 -0800150// pathPropertyIndexesForPropertyStruct returns a list of all of the indexes of properties in
151// property struct type that are tagged with `android:"path"`. Each index is a []int suitable for
152// passing to reflect.Value.FieldByIndex. The value is cached in a global cache by type.
Colin Cross1b488422019-03-04 22:33:56 -0800153func pathPropertyIndexesForPropertyStruct(ps interface{}) [][]int {
154 key := NewCustomOnceKey(reflect.TypeOf(ps))
155 return pathPropertyIndexesCache.Once(key, func() interface{} {
156 return proptools.PropertyIndexesWithTag(ps, "android", "path")
157 }).([][]int)
158}