Add more comments to path_properties.go

Bug: 173449605
Test: m checkbuild
Change-Id: I2638da9cc56726f5d646424e72f52289ad093781
This commit is contained in:
Colin Cross 2020-11-19 14:27:44 -08:00
parent a684540945
commit 11c89c0e9e
1 changed files with 32 additions and 17 deletions

View File

@ -21,27 +21,31 @@ import (
"github.com/google/blueprint/proptools" "github.com/google/blueprint/proptools"
) )
// This file implements support for automatically adding dependencies on any module referenced
// with the ":module" module reference syntax in a property that is annotated with `android:"path"`.
// The dependency is used by android.PathForModuleSrc to convert the module reference into the path
// to the output file of the referenced module.
func registerPathDepsMutator(ctx RegisterMutatorsContext) { func registerPathDepsMutator(ctx RegisterMutatorsContext) {
ctx.BottomUp("pathdeps", pathDepsMutator).Parallel() ctx.BottomUp("pathdeps", pathDepsMutator).Parallel()
} }
// The pathDepsMutator automatically adds dependencies on any module that is listed with ":module" syntax in a // The pathDepsMutator automatically adds dependencies on any module that is listed with the
// property that is tagged with android:"path". // ":module" module reference syntax in a property that is tagged with `android:"path"`.
func pathDepsMutator(ctx BottomUpMutatorContext) { func pathDepsMutator(ctx BottomUpMutatorContext) {
m := ctx.Module().(Module) props := ctx.Module().base().generalProperties
if m == nil {
return
}
props := m.base().generalProperties
// Iterate through each property struct of the module extracting the contents of all properties
// tagged with `android:"path"`.
var pathProperties []string var pathProperties []string
for _, ps := range props { for _, ps := range props {
pathProperties = append(pathProperties, pathPropertiesForPropertyStruct(ctx, ps)...) pathProperties = append(pathProperties, pathPropertiesForPropertyStruct(ps)...)
} }
// Remove duplicates to avoid multiple dependencies.
pathProperties = FirstUniqueStrings(pathProperties) pathProperties = FirstUniqueStrings(pathProperties)
// Add dependencies to anything that is a module reference.
for _, s := range pathProperties { for _, s := range pathProperties {
if m, t := SrcIsModuleWithTag(s); m != "" { if m, t := SrcIsModuleWithTag(s); m != "" {
ctx.AddDependency(ctx.Module(), sourceOrOutputDepTag(t), m) ctx.AddDependency(ctx.Module(), sourceOrOutputDepTag(t), m)
@ -49,34 +53,45 @@ func pathDepsMutator(ctx BottomUpMutatorContext) {
} }
} }
// pathPropertiesForPropertyStruct uses the indexes of properties that are tagged with android:"path" to extract // pathPropertiesForPropertyStruct uses the indexes of properties that are tagged with
// all their values from a property struct, returning them as a single slice of strings.. // android:"path" to extract all their values from a property struct, returning them as a single
func pathPropertiesForPropertyStruct(ctx BottomUpMutatorContext, ps interface{}) []string { // slice of strings.
func pathPropertiesForPropertyStruct(ps interface{}) []string {
v := reflect.ValueOf(ps) v := reflect.ValueOf(ps)
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
panic(fmt.Errorf("type %s is not a pointer to a struct", v.Type())) panic(fmt.Errorf("type %s is not a pointer to a struct", v.Type()))
} }
// If the property struct is a nil pointer it can't have any paths set in it.
if v.IsNil() { if v.IsNil() {
return nil return nil
} }
// v is now the reflect.Value for the concrete property struct.
v = v.Elem() v = v.Elem()
// Get or create the list of indexes of properties that are tagged with `android:"path"`.
pathPropertyIndexes := pathPropertyIndexesForPropertyStruct(ps) pathPropertyIndexes := pathPropertyIndexesForPropertyStruct(ps)
var ret []string var ret []string
for _, i := range pathPropertyIndexes { for _, i := range pathPropertyIndexes {
// Turn an index into a field.
sv := fieldByIndex(v, i) sv := fieldByIndex(v, i)
if !sv.IsValid() { if !sv.IsValid() {
// Skip properties inside a nil pointer.
continue continue
} }
// If the field is a non-nil pointer step into it.
if sv.Kind() == reflect.Ptr { if sv.Kind() == reflect.Ptr {
if sv.IsNil() { if sv.IsNil() {
continue continue
} }
sv = sv.Elem() sv = sv.Elem()
} }
// Collect paths from all strings and slices of strings.
switch sv.Kind() { switch sv.Kind() {
case reflect.String: case reflect.String:
ret = append(ret, sv.String()) ret = append(ret, sv.String())
@ -91,8 +106,8 @@ func pathPropertiesForPropertyStruct(ctx BottomUpMutatorContext, ps interface{})
return ret return ret
} }
// fieldByIndex is like reflect.Value.FieldByIndex, but returns an invalid reflect.Value when traversing a nil pointer // fieldByIndex is like reflect.Value.FieldByIndex, but returns an invalid reflect.Value when
// to a struct. // traversing a nil pointer to a struct.
func fieldByIndex(v reflect.Value, index []int) reflect.Value { func fieldByIndex(v reflect.Value, index []int) reflect.Value {
if len(index) == 1 { if len(index) == 1 {
return v.Field(index[0]) return v.Field(index[0])
@ -111,9 +126,9 @@ func fieldByIndex(v reflect.Value, index []int) reflect.Value {
var pathPropertyIndexesCache OncePer var pathPropertyIndexesCache OncePer
// pathPropertyIndexesForPropertyStruct returns a list of all of the indexes of properties in property struct type that // pathPropertyIndexesForPropertyStruct returns a list of all of the indexes of properties in
// are tagged with android:"path". Each index is a []int suitable for passing to reflect.Value.FieldByIndex. The value // property struct type that are tagged with `android:"path"`. Each index is a []int suitable for
// is cached in a global cache by type. // passing to reflect.Value.FieldByIndex. The value is cached in a global cache by type.
func pathPropertyIndexesForPropertyStruct(ps interface{}) [][]int { func pathPropertyIndexesForPropertyStruct(ps interface{}) [][]int {
key := NewCustomOnceKey(reflect.TypeOf(ps)) key := NewCustomOnceKey(reflect.TypeOf(ps))
return pathPropertyIndexesCache.Once(key, func() interface{} { return pathPropertyIndexesCache.Once(key, func() interface{} {