blob: f964d9f1e96a386ba6e649d504e5cb1c4d7b33f2 [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 (
Colin Cross1b488422019-03-04 22:33:56 -080018 "reflect"
19 "testing"
20)
21
22type pathDepsMutatorTestModule struct {
23 ModuleBase
24 props struct {
25 Foo string `android:"path"`
Colin Crossa3a97412019-03-18 12:24:29 -070026 Bar []string `android:"path,arch_variant"`
Colin Cross1b488422019-03-04 22:33:56 -080027 Baz *string `android:"path"`
28 Qux string
29 }
30
Colin Cross527f3e52019-07-15 13:35:21 -070031 // A second property struct with a duplicate property name
32 props2 struct {
33 Foo string `android:"path"`
34 }
35
Colin Cross1b488422019-03-04 22:33:56 -080036 sourceDeps []string
37}
38
39func pathDepsMutatorTestModuleFactory() Module {
40 module := &pathDepsMutatorTestModule{}
Colin Cross527f3e52019-07-15 13:35:21 -070041 module.AddProperties(&module.props, &module.props2)
Colin Crossa3a97412019-03-18 12:24:29 -070042 InitAndroidArchModule(module, DeviceSupported, MultilibBoth)
Colin Cross1b488422019-03-04 22:33:56 -080043 return module
44}
45
46func (p *pathDepsMutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Cross41955e82019-05-29 14:40:35 -070047 ctx.VisitDirectDeps(func(dep Module) {
48 if _, ok := ctx.OtherModuleDependencyTag(dep).(sourceOrOutputDependencyTag); ok {
49 p.sourceDeps = append(p.sourceDeps, ctx.OtherModuleName(dep))
50 }
Colin Cross1b488422019-03-04 22:33:56 -080051 })
Colin Cross527f3e52019-07-15 13:35:21 -070052
53 if p.props.Foo != "" {
54 // Make sure there is only one dependency on a module listed in a property present in multiple property structs
55 if ctx.GetDirectDepWithTag(SrcIsModule(p.props.Foo), sourceOrOutputDepTag("")) == nil {
56 ctx.ModuleErrorf("GetDirectDepWithTag failed")
57 }
58 }
Colin Cross1b488422019-03-04 22:33:56 -080059}
60
61func TestPathDepsMutator(t *testing.T) {
62 tests := []struct {
63 name string
64 bp string
65 deps []string
66 }{
67 {
68 name: "all",
69 bp: `
70 test {
71 name: "foo",
72 foo: ":a",
73 bar: [":b"],
Colin Cross41955e82019-05-29 14:40:35 -070074 baz: ":c{.bar}",
Colin Cross1b488422019-03-04 22:33:56 -080075 qux: ":d",
76 }`,
77 deps: []string{"a", "b", "c"},
78 },
Colin Crossa3a97412019-03-18 12:24:29 -070079 {
80 name: "arch variant",
81 bp: `
82 test {
83 name: "foo",
84 arch: {
85 arm64: {
86 bar: [":a"],
87 },
88 arm: {
89 bar: [":b"],
90 },
91 },
92 bar: [":c"],
93 }`,
94 deps: []string{"c", "a"},
95 },
Colin Cross1b488422019-03-04 22:33:56 -080096 }
97
Colin Cross1b488422019-03-04 22:33:56 -080098 for _, test := range tests {
99 t.Run(test.name, func(t *testing.T) {
Colin Cross1b488422019-03-04 22:33:56 -0800100 bp := test.bp + `
101 filegroup {
102 name: "a",
103 }
104
105 filegroup {
106 name: "b",
107 }
108
109 filegroup {
110 name: "c",
111 }
112
113 filegroup {
114 name: "d",
115 }
116 `
117
Colin Cross98be1bb2019-12-13 20:41:13 -0800118 config := TestArchConfig(buildDir, nil, bp, nil)
Colin Crossae8600b2020-10-29 17:09:13 -0700119 ctx := NewTestArchContext(config)
Colin Cross1b488422019-03-04 22:33:56 -0800120
Colin Cross98be1bb2019-12-13 20:41:13 -0800121 ctx.RegisterModuleType("test", pathDepsMutatorTestModuleFactory)
122 ctx.RegisterModuleType("filegroup", FileGroupFactory)
Colin Cross1b488422019-03-04 22:33:56 -0800123
Colin Crossae8600b2020-10-29 17:09:13 -0700124 ctx.Register()
Colin Cross1b488422019-03-04 22:33:56 -0800125 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
126 FailIfErrored(t, errs)
127 _, errs = ctx.PrepareBuildActions(config)
128 FailIfErrored(t, errs)
129
Colin Crossa3a97412019-03-18 12:24:29 -0700130 m := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*pathDepsMutatorTestModule)
Colin Cross1b488422019-03-04 22:33:56 -0800131
132 if g, w := m.sourceDeps, test.deps; !reflect.DeepEqual(g, w) {
133 t.Errorf("want deps %q, got %q", w, g)
134 }
135 })
136 }
137}