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