blob: 568f8682b8fd7bbd0a2bdbe297819491587a5763 [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 "testing"
19)
20
21type pathDepsMutatorTestModule struct {
22 ModuleBase
23 props struct {
24 Foo string `android:"path"`
Colin Crossa3a97412019-03-18 12:24:29 -070025 Bar []string `android:"path,arch_variant"`
Colin Cross1b488422019-03-04 22:33:56 -080026 Baz *string `android:"path"`
27 Qux string
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -080028 V *struct {
29 W string `android:"path"`
30 }
Colin Cross1b488422019-03-04 22:33:56 -080031 }
32
Colin Cross527f3e52019-07-15 13:35:21 -070033 // A second property struct with a duplicate property name
34 props2 struct {
35 Foo string `android:"path"`
36 }
37
Jiyong Park66dd5c02021-02-24 01:22:57 +090038 // nested slices of struct
39 props3 struct {
40 X []struct {
41 Y []struct {
42 Z []string `android:"path"`
43 }
44 }
45 }
46
Colin Cross1b488422019-03-04 22:33:56 -080047 sourceDeps []string
48}
49
50func pathDepsMutatorTestModuleFactory() Module {
51 module := &pathDepsMutatorTestModule{}
Jiyong Park66dd5c02021-02-24 01:22:57 +090052 module.AddProperties(&module.props, &module.props2, &module.props3)
Colin Crossa3a97412019-03-18 12:24:29 -070053 InitAndroidArchModule(module, DeviceSupported, MultilibBoth)
Colin Cross1b488422019-03-04 22:33:56 -080054 return module
55}
56
57func (p *pathDepsMutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Cross41955e82019-05-29 14:40:35 -070058 ctx.VisitDirectDeps(func(dep Module) {
59 if _, ok := ctx.OtherModuleDependencyTag(dep).(sourceOrOutputDependencyTag); ok {
60 p.sourceDeps = append(p.sourceDeps, ctx.OtherModuleName(dep))
61 }
Colin Cross1b488422019-03-04 22:33:56 -080062 })
Colin Cross527f3e52019-07-15 13:35:21 -070063
64 if p.props.Foo != "" {
65 // Make sure there is only one dependency on a module listed in a property present in multiple property structs
66 if ctx.GetDirectDepWithTag(SrcIsModule(p.props.Foo), sourceOrOutputDepTag("")) == nil {
67 ctx.ModuleErrorf("GetDirectDepWithTag failed")
68 }
69 }
Colin Cross1b488422019-03-04 22:33:56 -080070}
71
72func TestPathDepsMutator(t *testing.T) {
73 tests := []struct {
74 name string
75 bp string
76 deps []string
77 }{
78 {
79 name: "all",
80 bp: `
81 test {
82 name: "foo",
83 foo: ":a",
84 bar: [":b"],
Colin Cross41955e82019-05-29 14:40:35 -070085 baz: ":c{.bar}",
Colin Cross1b488422019-03-04 22:33:56 -080086 qux: ":d",
Jiyong Park66dd5c02021-02-24 01:22:57 +090087 x: [
88 {
89 y: [
90 {
91 z: [":x", ":y"],
92 },
93 {
94 z: [":z"],
95 },
96 ],
97 },
98 ],
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -080099 v: {
100 w: ":w",
101 },
Colin Cross1b488422019-03-04 22:33:56 -0800102 }`,
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800103 deps: []string{"a", "b", "c", "w", "x", "y", "z"},
Colin Cross1b488422019-03-04 22:33:56 -0800104 },
Colin Crossa3a97412019-03-18 12:24:29 -0700105 {
106 name: "arch variant",
107 bp: `
108 test {
109 name: "foo",
110 arch: {
111 arm64: {
112 bar: [":a"],
113 },
114 arm: {
115 bar: [":b"],
116 },
117 },
118 bar: [":c"],
119 }`,
120 deps: []string{"c", "a"},
121 },
Colin Cross1b488422019-03-04 22:33:56 -0800122 }
123
Colin Cross1b488422019-03-04 22:33:56 -0800124 for _, test := range tests {
125 t.Run(test.name, func(t *testing.T) {
Colin Cross1b488422019-03-04 22:33:56 -0800126 bp := test.bp + `
127 filegroup {
128 name: "a",
129 }
130
131 filegroup {
132 name: "b",
133 }
134
135 filegroup {
136 name: "c",
137 }
138
139 filegroup {
140 name: "d",
141 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900142
143 filegroup {
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800144 name: "w",
145 }
146
147 filegroup {
Jiyong Park66dd5c02021-02-24 01:22:57 +0900148 name: "x",
149 }
150
151 filegroup {
152 name: "y",
153 }
154
155 filegroup {
156 name: "z",
157 }
Colin Cross1b488422019-03-04 22:33:56 -0800158 `
159
Paul Duffin30ac3e72021-03-20 00:36:14 +0000160 result := GroupFixturePreparers(
Paul Duffind518b7e2021-03-17 00:09:35 +0000161 PrepareForTestWithArchMutator,
162 PrepareForTestWithFilegroup,
163 FixtureRegisterWithContext(func(ctx RegistrationContext) {
164 ctx.RegisterModuleType("test", pathDepsMutatorTestModuleFactory)
165 }),
166 FixtureWithRootAndroidBp(bp),
Paul Duffin30ac3e72021-03-20 00:36:14 +0000167 ).RunTest(t)
Colin Cross1b488422019-03-04 22:33:56 -0800168
Paul Duffind518b7e2021-03-17 00:09:35 +0000169 m := result.Module("foo", "android_arm64_armv8-a").(*pathDepsMutatorTestModule)
Colin Cross1b488422019-03-04 22:33:56 -0800170
Paul Duffind518b7e2021-03-17 00:09:35 +0000171 AssertDeepEquals(t, "deps", test.deps, m.sourceDeps)
Colin Cross1b488422019-03-04 22:33:56 -0800172 })
173 }
174}