blob: b4fabeb94641a1f636d732eb1da8f3195a186c1b [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
Cole Faust65cb40a2024-10-21 15:41:42 -070021 "github.com/google/blueprint"
Colin Cross1b488422019-03-04 22:33:56 -080022 "github.com/google/blueprint/proptools"
23)
24
Colin Cross11c89c02020-11-19 14:27:44 -080025// This file implements support for automatically adding dependencies on any module referenced
26// with the ":module" module reference syntax in a property that is annotated with `android:"path"`.
27// The dependency is used by android.PathForModuleSrc to convert the module reference into the path
28// to the output file of the referenced module.
29
Colin Cross1b488422019-03-04 22:33:56 -080030func registerPathDepsMutator(ctx RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -070031 ctx.BottomUp("pathdeps", pathDepsMutator)
Colin Cross1b488422019-03-04 22:33:56 -080032}
33
Colin Cross11c89c02020-11-19 14:27:44 -080034// The pathDepsMutator automatically adds dependencies on any module that is listed with the
35// ":module" module reference syntax in a property that is tagged with `android:"path"`.
Colin Cross1b488422019-03-04 22:33:56 -080036func pathDepsMutator(ctx BottomUpMutatorContext) {
Colin Cross69a680f2024-01-29 13:18:43 -080037 if _, ok := ctx.Module().(DefaultsModule); ok {
38 // Defaults modules shouldn't have dependencies added for path properties, they have already been
39 // squashed into the real modules.
40 return
41 }
Cole Faust65cb40a2024-10-21 15:41:42 -070042 if !ctx.Module().Enabled(ctx) {
43 return
44 }
Usta851a3272022-01-05 23:42:33 -050045 props := ctx.Module().base().GetProperties()
Liz Kammer4562a3b2021-04-21 18:15:34 -040046 addPathDepsForProps(ctx, props)
47}
Colin Cross1b488422019-03-04 22:33:56 -080048
Liz Kammer4562a3b2021-04-21 18:15:34 -040049func addPathDepsForProps(ctx BottomUpMutatorContext, props []interface{}) {
Colin Cross11c89c02020-11-19 14:27:44 -080050 // Iterate through each property struct of the module extracting the contents of all properties
Cole Faust65cb40a2024-10-21 15:41:42 -070051 // tagged with `android:"path"` or one of the variant-specifying tags.
Colin Cross527f3e52019-07-15 13:35:21 -070052 var pathProperties []string
Cole Faust65cb40a2024-10-21 15:41:42 -070053 var pathDeviceFirstProperties []string
Cole Faust18f03f12024-10-23 14:51:11 -070054 var pathDeviceFirstPrefer32Properties []string
Cole Faust65cb40a2024-10-21 15:41:42 -070055 var pathDeviceCommonProperties []string
56 var pathCommonOsProperties []string
Colin Cross1b488422019-03-04 22:33:56 -080057 for _, ps := range props {
Cole Faust65cb40a2024-10-21 15:41:42 -070058 pathProperties = append(pathProperties, taggedPropertiesForPropertyStruct(ctx, ps, "path")...)
59 pathDeviceFirstProperties = append(pathDeviceFirstProperties, taggedPropertiesForPropertyStruct(ctx, ps, "path_device_first")...)
Cole Faust18f03f12024-10-23 14:51:11 -070060 pathDeviceFirstPrefer32Properties = append(pathDeviceFirstPrefer32Properties, taggedPropertiesForPropertyStruct(ctx, ps, "path_device_first_prefer32")...)
Cole Faust65cb40a2024-10-21 15:41:42 -070061 pathDeviceCommonProperties = append(pathDeviceCommonProperties, taggedPropertiesForPropertyStruct(ctx, ps, "path_device_common")...)
62 pathCommonOsProperties = append(pathCommonOsProperties, taggedPropertiesForPropertyStruct(ctx, ps, "path_common_os")...)
Colin Cross527f3e52019-07-15 13:35:21 -070063 }
Colin Cross1b488422019-03-04 22:33:56 -080064
Colin Cross11c89c02020-11-19 14:27:44 -080065 // Remove duplicates to avoid multiple dependencies.
Colin Cross527f3e52019-07-15 13:35:21 -070066 pathProperties = FirstUniqueStrings(pathProperties)
Cole Faust65cb40a2024-10-21 15:41:42 -070067 pathDeviceFirstProperties = FirstUniqueStrings(pathDeviceFirstProperties)
Cole Faust18f03f12024-10-23 14:51:11 -070068 pathDeviceFirstPrefer32Properties = FirstUniqueStrings(pathDeviceFirstPrefer32Properties)
Cole Faust65cb40a2024-10-21 15:41:42 -070069 pathDeviceCommonProperties = FirstUniqueStrings(pathDeviceCommonProperties)
70 pathCommonOsProperties = FirstUniqueStrings(pathCommonOsProperties)
Colin Cross527f3e52019-07-15 13:35:21 -070071
Colin Cross11c89c02020-11-19 14:27:44 -080072 // Add dependencies to anything that is a module reference.
Colin Cross527f3e52019-07-15 13:35:21 -070073 for _, s := range pathProperties {
74 if m, t := SrcIsModuleWithTag(s); m != "" {
Paul Duffin40131a32021-07-09 17:10:35 +010075 ctx.AddDependency(ctx.Module(), sourceOrOutputDepTag(m, t), m)
Colin Cross1b488422019-03-04 22:33:56 -080076 }
Colin Cross1b488422019-03-04 22:33:56 -080077 }
Cole Faust65cb40a2024-10-21 15:41:42 -070078 // For properties tagged "path_device_first", use the first arch device variant when adding
79 // dependencies. This allows host modules to have some properties that add dependencies on
80 // device modules.
81 for _, s := range pathDeviceFirstProperties {
82 if m, t := SrcIsModuleWithTag(s); m != "" {
83 ctx.AddVariationDependencies(ctx.Config().AndroidFirstDeviceTarget.Variations(), sourceOrOutputDepTag(m, t), m)
84 }
85 }
Cole Faust18f03f12024-10-23 14:51:11 -070086 // properties tagged path_device_first_prefer32 get the first 32 bit target if one is available,
87 // otherwise they use the first 64 bit target
88 if len(pathDeviceFirstPrefer32Properties) > 0 {
89 firstPrefer32Target := FirstTarget(ctx.Config().Targets[Android], "lib32", "lib64")
90 if len(firstPrefer32Target) == 0 {
91 ctx.ModuleErrorf("Could not find a first_prefer32 target")
92 } else {
93 for _, s := range pathDeviceFirstPrefer32Properties {
94 if m, t := SrcIsModuleWithTag(s); m != "" {
95 ctx.AddVariationDependencies(firstPrefer32Target[0].Variations(), sourceOrOutputDepTag(m, t), m)
96 }
97 }
98 }
99 }
Cole Faust65cb40a2024-10-21 15:41:42 -0700100 // properties tagged "path_device_common" get the device common variant
101 for _, s := range pathDeviceCommonProperties {
102 if m, t := SrcIsModuleWithTag(s); m != "" {
103 ctx.AddVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), sourceOrOutputDepTag(m, t), m)
104 }
105 }
Cole Faust18f03f12024-10-23 14:51:11 -0700106 // properties tagged "path_common_os" get the CommonOs variant
Cole Faust65cb40a2024-10-21 15:41:42 -0700107 for _, s := range pathCommonOsProperties {
108 if m, t := SrcIsModuleWithTag(s); m != "" {
109 ctx.AddVariationDependencies([]blueprint.Variation{
110 {Mutator: "os", Variation: "common_os"},
111 {Mutator: "arch", Variation: ""},
112 }, sourceOrOutputDepTag(m, t), m)
113 }
114 }
Colin Cross1b488422019-03-04 22:33:56 -0800115}
116
Cole Faust65cb40a2024-10-21 15:41:42 -0700117// taggedPropertiesForPropertyStruct uses the indexes of properties that are tagged with
118// android:"tagValue" to extract all their values from a property struct, returning them as a single
Colin Cross11c89c02020-11-19 14:27:44 -0800119// slice of strings.
Cole Faust65cb40a2024-10-21 15:41:42 -0700120func taggedPropertiesForPropertyStruct(ctx BottomUpMutatorContext, ps interface{}, tagValue string) []string {
Colin Cross1b488422019-03-04 22:33:56 -0800121 v := reflect.ValueOf(ps)
122 if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
123 panic(fmt.Errorf("type %s is not a pointer to a struct", v.Type()))
124 }
Colin Cross11c89c02020-11-19 14:27:44 -0800125
126 // If the property struct is a nil pointer it can't have any paths set in it.
Colin Cross1b488422019-03-04 22:33:56 -0800127 if v.IsNil() {
128 return nil
129 }
Colin Cross11c89c02020-11-19 14:27:44 -0800130
131 // v is now the reflect.Value for the concrete property struct.
Colin Cross1b488422019-03-04 22:33:56 -0800132 v = v.Elem()
133
Colin Cross11c89c02020-11-19 14:27:44 -0800134 // Get or create the list of indexes of properties that are tagged with `android:"path"`.
Cole Faust65cb40a2024-10-21 15:41:42 -0700135 pathPropertyIndexes := taggedPropertyIndexesForPropertyStruct(ps, tagValue)
Colin Cross1b488422019-03-04 22:33:56 -0800136
137 var ret []string
138
139 for _, i := range pathPropertyIndexes {
Jiyong Park66dd5c02021-02-24 01:22:57 +0900140 var values []reflect.Value
141 fieldsByIndex(v, i, &values)
142 for _, sv := range values {
143 if !sv.IsValid() {
144 // Skip properties inside a nil pointer.
Colin Cross1b488422019-03-04 22:33:56 -0800145 continue
146 }
Colin Cross11c89c02020-11-19 14:27:44 -0800147
Jiyong Park66dd5c02021-02-24 01:22:57 +0900148 // If the field is a non-nil pointer step into it.
149 if sv.Kind() == reflect.Ptr {
150 if sv.IsNil() {
151 continue
152 }
153 sv = sv.Elem()
154 }
155
156 // Collect paths from all strings and slices of strings.
157 switch sv.Kind() {
158 case reflect.String:
159 ret = append(ret, sv.String())
160 case reflect.Slice:
161 ret = append(ret, sv.Interface().([]string)...)
Cole Faustbdd8aee2024-03-14 14:33:02 -0700162 case reflect.Struct:
163 intf := sv.Interface()
164 if configurable, ok := intf.(proptools.Configurable[string]); ok {
Cole Faustb78ce432024-04-04 10:55:19 -0700165 ret = append(ret, configurable.GetOrDefault(ctx, ""))
Cole Faustbdd8aee2024-03-14 14:33:02 -0700166 } else if configurable, ok := intf.(proptools.Configurable[[]string]); ok {
Cole Faustb78ce432024-04-04 10:55:19 -0700167 ret = append(ret, configurable.GetOrDefault(ctx, nil)...)
Cole Faustbdd8aee2024-03-14 14:33:02 -0700168 } else {
169 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`,
170 v.Type().FieldByIndex(i).Name, v.Type(), sv.Type()))
171 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900172 default:
173 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`,
174 v.Type().FieldByIndex(i).Name, v.Type(), sv.Type()))
175 }
Colin Cross1b488422019-03-04 22:33:56 -0800176 }
177 }
178
179 return ret
180}
181
Jiyong Park66dd5c02021-02-24 01:22:57 +0900182// fieldsByIndex is similar to reflect.Value.FieldByIndex, but is more robust: it doesn't track
183// nil pointers and it returns multiple values when there's slice of struct.
184func fieldsByIndex(v reflect.Value, index []int, values *[]reflect.Value) {
185 // leaf case
Colin Cross1b488422019-03-04 22:33:56 -0800186 if len(index) == 1 {
Jiyong Park66dd5c02021-02-24 01:22:57 +0900187 if isSliceOfStruct(v) {
188 for i := 0; i < v.Len(); i++ {
189 *values = append(*values, v.Index(i).Field(index[0]))
Colin Cross1b488422019-03-04 22:33:56 -0800190 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900191 } else {
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800192 // Dereference it if it's a pointer.
193 if v.Kind() == reflect.Ptr {
194 if v.IsNil() {
195 return
196 }
197 v = v.Elem()
198 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900199 *values = append(*values, v.Field(index[0]))
Colin Cross1b488422019-03-04 22:33:56 -0800200 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900201 return
Colin Cross1b488422019-03-04 22:33:56 -0800202 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900203
204 // recursion
205 if v.Kind() == reflect.Ptr {
206 // don't track nil pointer
207 if v.IsNil() {
208 return
209 }
210 v = v.Elem()
211 } else if isSliceOfStruct(v) {
212 // do the recursion for all elements
213 for i := 0; i < v.Len(); i++ {
214 fieldsByIndex(v.Index(i).Field(index[0]), index[1:], values)
215 }
216 return
217 }
218 fieldsByIndex(v.Field(index[0]), index[1:], values)
219 return
220}
221
222func isSliceOfStruct(v reflect.Value) bool {
223 return v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Struct
Colin Cross1b488422019-03-04 22:33:56 -0800224}
225
226var pathPropertyIndexesCache OncePer
227
Cole Faust65cb40a2024-10-21 15:41:42 -0700228// taggedPropertyIndexesForPropertyStruct returns a list of all of the indexes of properties in
229// property struct type that are tagged with `android:"tagValue"`. Each index is a []int suitable
230// for passing to reflect.Value.FieldByIndex. The value is cached in a global cache by type and
231// tagValue.
232func taggedPropertyIndexesForPropertyStruct(ps interface{}, tagValue string) [][]int {
233 type pathPropertyIndexesOnceKey struct {
234 propStructType reflect.Type
235 tagValue string
236 }
237 key := NewCustomOnceKey(pathPropertyIndexesOnceKey{
238 propStructType: reflect.TypeOf(ps),
239 tagValue: tagValue,
240 })
Colin Cross1b488422019-03-04 22:33:56 -0800241 return pathPropertyIndexesCache.Once(key, func() interface{} {
Cole Faust65cb40a2024-10-21 15:41:42 -0700242 return proptools.PropertyIndexesWithTag(ps, "android", tagValue)
Colin Cross1b488422019-03-04 22:33:56 -0800243 }).([][]int)
244}