blob: d80a8ed131eeae0f9d5e14fac7492c14dc832899 [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) {
Colin Cross8a962802024-10-09 15:29:27 -070030 ctx.BottomUp("pathdeps", pathDepsMutator)
Colin Cross1b488422019-03-04 22:33:56 -080031}
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 Cross69a680f2024-01-29 13:18:43 -080036 if _, ok := ctx.Module().(DefaultsModule); ok {
37 // Defaults modules shouldn't have dependencies added for path properties, they have already been
38 // squashed into the real modules.
39 return
40 }
Usta851a3272022-01-05 23:42:33 -050041 props := ctx.Module().base().GetProperties()
Liz Kammer4562a3b2021-04-21 18:15:34 -040042 addPathDepsForProps(ctx, props)
43}
Colin Cross1b488422019-03-04 22:33:56 -080044
Liz Kammer4562a3b2021-04-21 18:15:34 -040045func addPathDepsForProps(ctx BottomUpMutatorContext, props []interface{}) {
Colin Cross11c89c02020-11-19 14:27:44 -080046 // Iterate through each property struct of the module extracting the contents of all properties
47 // tagged with `android:"path"`.
Colin Cross527f3e52019-07-15 13:35:21 -070048 var pathProperties []string
Colin Cross1b488422019-03-04 22:33:56 -080049 for _, ps := range props {
Cole Faustbdd8aee2024-03-14 14:33:02 -070050 pathProperties = append(pathProperties, pathPropertiesForPropertyStruct(ctx, ps)...)
Colin Cross527f3e52019-07-15 13:35:21 -070051 }
Colin Cross1b488422019-03-04 22:33:56 -080052
Colin Cross11c89c02020-11-19 14:27:44 -080053 // Remove duplicates to avoid multiple dependencies.
Colin Cross527f3e52019-07-15 13:35:21 -070054 pathProperties = FirstUniqueStrings(pathProperties)
55
Colin Cross11c89c02020-11-19 14:27:44 -080056 // Add dependencies to anything that is a module reference.
Colin Cross527f3e52019-07-15 13:35:21 -070057 for _, s := range pathProperties {
58 if m, t := SrcIsModuleWithTag(s); m != "" {
Paul Duffin40131a32021-07-09 17:10:35 +010059 ctx.AddDependency(ctx.Module(), sourceOrOutputDepTag(m, t), m)
Colin Cross1b488422019-03-04 22:33:56 -080060 }
Colin Cross1b488422019-03-04 22:33:56 -080061 }
62}
63
Colin Cross11c89c02020-11-19 14:27:44 -080064// pathPropertiesForPropertyStruct uses the indexes of properties that are tagged with
65// android:"path" to extract all their values from a property struct, returning them as a single
66// slice of strings.
Cole Faustbdd8aee2024-03-14 14:33:02 -070067func pathPropertiesForPropertyStruct(ctx BottomUpMutatorContext, ps interface{}) []string {
Colin Cross1b488422019-03-04 22:33:56 -080068 v := reflect.ValueOf(ps)
69 if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
70 panic(fmt.Errorf("type %s is not a pointer to a struct", v.Type()))
71 }
Colin Cross11c89c02020-11-19 14:27:44 -080072
73 // If the property struct is a nil pointer it can't have any paths set in it.
Colin Cross1b488422019-03-04 22:33:56 -080074 if v.IsNil() {
75 return nil
76 }
Colin Cross11c89c02020-11-19 14:27:44 -080077
78 // v is now the reflect.Value for the concrete property struct.
Colin Cross1b488422019-03-04 22:33:56 -080079 v = v.Elem()
80
Colin Cross11c89c02020-11-19 14:27:44 -080081 // Get or create the list of indexes of properties that are tagged with `android:"path"`.
Colin Cross1b488422019-03-04 22:33:56 -080082 pathPropertyIndexes := pathPropertyIndexesForPropertyStruct(ps)
83
84 var ret []string
85
86 for _, i := range pathPropertyIndexes {
Jiyong Park66dd5c02021-02-24 01:22:57 +090087 var values []reflect.Value
88 fieldsByIndex(v, i, &values)
89 for _, sv := range values {
90 if !sv.IsValid() {
91 // Skip properties inside a nil pointer.
Colin Cross1b488422019-03-04 22:33:56 -080092 continue
93 }
Colin Cross11c89c02020-11-19 14:27:44 -080094
Jiyong Park66dd5c02021-02-24 01:22:57 +090095 // If the field is a non-nil pointer step into it.
96 if sv.Kind() == reflect.Ptr {
97 if sv.IsNil() {
98 continue
99 }
100 sv = sv.Elem()
101 }
102
103 // Collect paths from all strings and slices of strings.
104 switch sv.Kind() {
105 case reflect.String:
106 ret = append(ret, sv.String())
107 case reflect.Slice:
108 ret = append(ret, sv.Interface().([]string)...)
Cole Faustbdd8aee2024-03-14 14:33:02 -0700109 case reflect.Struct:
110 intf := sv.Interface()
111 if configurable, ok := intf.(proptools.Configurable[string]); ok {
Cole Faustb78ce432024-04-04 10:55:19 -0700112 ret = append(ret, configurable.GetOrDefault(ctx, ""))
Cole Faustbdd8aee2024-03-14 14:33:02 -0700113 } else if configurable, ok := intf.(proptools.Configurable[[]string]); ok {
Cole Faustb78ce432024-04-04 10:55:19 -0700114 ret = append(ret, configurable.GetOrDefault(ctx, nil)...)
Cole Faustbdd8aee2024-03-14 14:33:02 -0700115 } else {
116 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`,
117 v.Type().FieldByIndex(i).Name, v.Type(), sv.Type()))
118 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900119 default:
120 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`,
121 v.Type().FieldByIndex(i).Name, v.Type(), sv.Type()))
122 }
Colin Cross1b488422019-03-04 22:33:56 -0800123 }
124 }
125
126 return ret
127}
128
Jiyong Park66dd5c02021-02-24 01:22:57 +0900129// fieldsByIndex is similar to reflect.Value.FieldByIndex, but is more robust: it doesn't track
130// nil pointers and it returns multiple values when there's slice of struct.
131func fieldsByIndex(v reflect.Value, index []int, values *[]reflect.Value) {
132 // leaf case
Colin Cross1b488422019-03-04 22:33:56 -0800133 if len(index) == 1 {
Jiyong Park66dd5c02021-02-24 01:22:57 +0900134 if isSliceOfStruct(v) {
135 for i := 0; i < v.Len(); i++ {
136 *values = append(*values, v.Index(i).Field(index[0]))
Colin Cross1b488422019-03-04 22:33:56 -0800137 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900138 } else {
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800139 // Dereference it if it's a pointer.
140 if v.Kind() == reflect.Ptr {
141 if v.IsNil() {
142 return
143 }
144 v = v.Elem()
145 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900146 *values = append(*values, v.Field(index[0]))
Colin Cross1b488422019-03-04 22:33:56 -0800147 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900148 return
Colin Cross1b488422019-03-04 22:33:56 -0800149 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900150
151 // recursion
152 if v.Kind() == reflect.Ptr {
153 // don't track nil pointer
154 if v.IsNil() {
155 return
156 }
157 v = v.Elem()
158 } else if isSliceOfStruct(v) {
159 // do the recursion for all elements
160 for i := 0; i < v.Len(); i++ {
161 fieldsByIndex(v.Index(i).Field(index[0]), index[1:], values)
162 }
163 return
164 }
165 fieldsByIndex(v.Field(index[0]), index[1:], values)
166 return
167}
168
169func isSliceOfStruct(v reflect.Value) bool {
170 return v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Struct
Colin Cross1b488422019-03-04 22:33:56 -0800171}
172
173var pathPropertyIndexesCache OncePer
174
Colin Cross11c89c02020-11-19 14:27:44 -0800175// pathPropertyIndexesForPropertyStruct returns a list of all of the indexes of properties in
176// property struct type that are tagged with `android:"path"`. Each index is a []int suitable for
177// passing to reflect.Value.FieldByIndex. The value is cached in a global cache by type.
Colin Cross1b488422019-03-04 22:33:56 -0800178func pathPropertyIndexesForPropertyStruct(ps interface{}) [][]int {
179 key := NewCustomOnceKey(reflect.TypeOf(ps))
180 return pathPropertyIndexesCache.Once(key, func() interface{} {
181 return proptools.PropertyIndexesWithTag(ps, "android", "path")
182 }).([][]int)
183}