Add path properties mutator
am: 1b48842a4b
Change-Id: Ib7322a2e6a402f9e2e0b833b5e577fa3289df3eb
This commit is contained in:
@@ -56,6 +56,7 @@ bootstrap_go_package {
|
|||||||
"android/neverallow.go",
|
"android/neverallow.go",
|
||||||
"android/onceper.go",
|
"android/onceper.go",
|
||||||
"android/package_ctx.go",
|
"android/package_ctx.go",
|
||||||
|
"android/path_properties.go",
|
||||||
"android/paths.go",
|
"android/paths.go",
|
||||||
"android/prebuilt.go",
|
"android/prebuilt.go",
|
||||||
"android/prebuilt_etc.go",
|
"android/prebuilt_etc.go",
|
||||||
@@ -80,6 +81,7 @@ bootstrap_go_package {
|
|||||||
"android/namespace_test.go",
|
"android/namespace_test.go",
|
||||||
"android/neverallow_test.go",
|
"android/neverallow_test.go",
|
||||||
"android/onceper_test.go",
|
"android/onceper_test.go",
|
||||||
|
"android/path_properties_test.go",
|
||||||
"android/paths_test.go",
|
"android/paths_test.go",
|
||||||
"android/prebuilt_test.go",
|
"android/prebuilt_test.go",
|
||||||
"android/prebuilt_etc_test.go",
|
"android/prebuilt_etc_test.go",
|
||||||
|
@@ -1437,7 +1437,7 @@ func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string
|
|||||||
if m := SrcIsModule(e); m != "" {
|
if m := SrcIsModule(e); m != "" {
|
||||||
module := ctx.GetDirectDepWithTag(m, SourceDepTag)
|
module := ctx.GetDirectDepWithTag(m, SourceDepTag)
|
||||||
if module == nil {
|
if module == nil {
|
||||||
// Error will have been handled by ExtractSourcesDeps
|
ctx.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if srcProducer, ok := module.(SourceFileProducer); ok {
|
if srcProducer, ok := module.(SourceFileProducer); ok {
|
||||||
@@ -1454,7 +1454,7 @@ func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string
|
|||||||
if m := SrcIsModule(s); m != "" {
|
if m := SrcIsModule(s); m != "" {
|
||||||
module := ctx.GetDirectDepWithTag(m, SourceDepTag)
|
module := ctx.GetDirectDepWithTag(m, SourceDepTag)
|
||||||
if module == nil {
|
if module == nil {
|
||||||
// Error will have been handled by ExtractSourcesDeps
|
ctx.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if srcProducer, ok := module.(SourceFileProducer); ok {
|
if srcProducer, ok := module.(SourceFileProducer); ok {
|
||||||
|
@@ -91,6 +91,7 @@ var preDeps = []RegisterMutatorFunc{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var postDeps = []RegisterMutatorFunc{
|
var postDeps = []RegisterMutatorFunc{
|
||||||
|
registerPathDepsMutator,
|
||||||
RegisterPrebuiltsPostDepsMutators,
|
RegisterPrebuiltsPostDepsMutators,
|
||||||
registerNeverallowMutator,
|
registerNeverallowMutator,
|
||||||
}
|
}
|
||||||
|
123
android/path_properties.go
Normal file
123
android/path_properties.go
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
// Copyright 2019 Google Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package android
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/google/blueprint/proptools"
|
||||||
|
)
|
||||||
|
|
||||||
|
func registerPathDepsMutator(ctx RegisterMutatorsContext) {
|
||||||
|
ctx.BottomUp("pathdeps", pathDepsMutator).Parallel()
|
||||||
|
}
|
||||||
|
|
||||||
|
// The pathDepsMutator automatically adds dependencies on any module that is listed with ":module" syntax in a
|
||||||
|
// property that is tagged with android:"path".
|
||||||
|
func pathDepsMutator(ctx BottomUpMutatorContext) {
|
||||||
|
m := ctx.Module().(Module)
|
||||||
|
if m == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
props := m.base().customizableProperties
|
||||||
|
|
||||||
|
for _, ps := range props {
|
||||||
|
pathProperties := pathPropertiesForPropertyStruct(ctx, ps)
|
||||||
|
pathProperties = FirstUniqueStrings(pathProperties)
|
||||||
|
|
||||||
|
var deps []string
|
||||||
|
for _, s := range pathProperties {
|
||||||
|
if m := SrcIsModule(s); m != "" {
|
||||||
|
deps = append(deps, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pathPropertiesForPropertyStruct uses the indexes of properties that are tagged with android:"path" to extract
|
||||||
|
// all their values from a property struct, returning them as a single slice of strings..
|
||||||
|
func pathPropertiesForPropertyStruct(ctx BottomUpMutatorContext, ps interface{}) []string {
|
||||||
|
v := reflect.ValueOf(ps)
|
||||||
|
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
|
||||||
|
panic(fmt.Errorf("type %s is not a pointer to a struct", v.Type()))
|
||||||
|
}
|
||||||
|
if v.IsNil() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
v = v.Elem()
|
||||||
|
|
||||||
|
pathPropertyIndexes := pathPropertyIndexesForPropertyStruct(ps)
|
||||||
|
|
||||||
|
var ret []string
|
||||||
|
|
||||||
|
for _, i := range pathPropertyIndexes {
|
||||||
|
sv := fieldByIndex(v, i)
|
||||||
|
if !sv.IsValid() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if sv.Kind() == reflect.Ptr {
|
||||||
|
if sv.IsNil() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sv = sv.Elem()
|
||||||
|
}
|
||||||
|
switch sv.Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
ret = append(ret, sv.String())
|
||||||
|
case reflect.Slice:
|
||||||
|
ret = append(ret, sv.Interface().([]string)...)
|
||||||
|
default:
|
||||||
|
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`,
|
||||||
|
v.Type().FieldByIndex(i).Name, v.Type(), sv.Type()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// fieldByIndex is like reflect.Value.FieldByIndex, but returns an invalid reflect.Value when traversing a nil pointer
|
||||||
|
// to a struct.
|
||||||
|
func fieldByIndex(v reflect.Value, index []int) reflect.Value {
|
||||||
|
if len(index) == 1 {
|
||||||
|
return v.Field(index[0])
|
||||||
|
}
|
||||||
|
for _, x := range index {
|
||||||
|
if v.Kind() == reflect.Ptr {
|
||||||
|
if v.IsNil() {
|
||||||
|
return reflect.Value{}
|
||||||
|
}
|
||||||
|
v = v.Elem()
|
||||||
|
}
|
||||||
|
v = v.Field(x)
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
var pathPropertyIndexesCache OncePer
|
||||||
|
|
||||||
|
// pathPropertyIndexesForPropertyStruct returns a list of all of the indexes of properties in property struct type that
|
||||||
|
// are tagged with android:"path". Each index is a []int suitable for passing to reflect.Value.FieldByIndex. The value
|
||||||
|
// is cached in a global cache by type.
|
||||||
|
func pathPropertyIndexesForPropertyStruct(ps interface{}) [][]int {
|
||||||
|
key := NewCustomOnceKey(reflect.TypeOf(ps))
|
||||||
|
return pathPropertyIndexesCache.Once(key, func() interface{} {
|
||||||
|
return proptools.PropertyIndexesWithTag(ps, "android", "path")
|
||||||
|
}).([][]int)
|
||||||
|
}
|
120
android/path_properties_test.go
Normal file
120
android/path_properties_test.go
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
// Copyright 2019 Google Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package android
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type pathDepsMutatorTestModule struct {
|
||||||
|
ModuleBase
|
||||||
|
props struct {
|
||||||
|
Foo string `android:"path"`
|
||||||
|
Bar []string `android:"path"`
|
||||||
|
Baz *string `android:"path"`
|
||||||
|
Qux string
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceDeps []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func pathDepsMutatorTestModuleFactory() Module {
|
||||||
|
module := &pathDepsMutatorTestModule{}
|
||||||
|
module.AddProperties(&module.props)
|
||||||
|
InitAndroidModule(module)
|
||||||
|
return module
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *pathDepsMutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
|
||||||
|
ctx.VisitDirectDepsWithTag(SourceDepTag, func(dep Module) {
|
||||||
|
p.sourceDeps = append(p.sourceDeps, ctx.OtherModuleName(dep))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPathDepsMutator(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
bp string
|
||||||
|
deps []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "all",
|
||||||
|
bp: `
|
||||||
|
test {
|
||||||
|
name: "foo",
|
||||||
|
foo: ":a",
|
||||||
|
bar: [":b"],
|
||||||
|
baz: ":c",
|
||||||
|
qux: ":d",
|
||||||
|
}`,
|
||||||
|
deps: []string{"a", "b", "c"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
buildDir, err := ioutil.TempDir("", "soong_path_properties_test")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(buildDir)
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
config := TestConfig(buildDir, nil)
|
||||||
|
ctx := NewTestContext()
|
||||||
|
|
||||||
|
ctx.RegisterModuleType("test", ModuleFactoryAdaptor(pathDepsMutatorTestModuleFactory))
|
||||||
|
ctx.RegisterModuleType("filegroup", ModuleFactoryAdaptor(FileGroupFactory))
|
||||||
|
|
||||||
|
bp := test.bp + `
|
||||||
|
filegroup {
|
||||||
|
name: "a",
|
||||||
|
}
|
||||||
|
|
||||||
|
filegroup {
|
||||||
|
name: "b",
|
||||||
|
}
|
||||||
|
|
||||||
|
filegroup {
|
||||||
|
name: "c",
|
||||||
|
}
|
||||||
|
|
||||||
|
filegroup {
|
||||||
|
name: "d",
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
mockFS := map[string][]byte{
|
||||||
|
"Android.bp": []byte(bp),
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.MockFileSystem(mockFS)
|
||||||
|
|
||||||
|
ctx.Register()
|
||||||
|
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
|
||||||
|
FailIfErrored(t, errs)
|
||||||
|
_, errs = ctx.PrepareBuildActions(config)
|
||||||
|
FailIfErrored(t, errs)
|
||||||
|
|
||||||
|
m := ctx.ModuleForTests("foo", "").Module().(*pathDepsMutatorTestModule)
|
||||||
|
|
||||||
|
if g, w := m.sourceDeps, test.deps; !reflect.DeepEqual(g, w) {
|
||||||
|
t.Errorf("want deps %q, got %q", w, g)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@@ -37,6 +37,8 @@ func NewTestContext() *TestContext {
|
|||||||
|
|
||||||
ctx.SetNameInterface(nameResolver)
|
ctx.SetNameInterface(nameResolver)
|
||||||
|
|
||||||
|
ctx.postDeps = append(ctx.postDeps, registerPathDepsMutator)
|
||||||
|
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user