blob: 07b48696ca10fad5f22aeed6a22b443e32edf8e4 [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
Paul Duffind5cf92e2021-07-09 17:38:55 +010066 m := SrcIsModule(p.props.Foo)
67 if GetModuleFromPathDep(ctx, m, "") == nil {
Colin Cross527f3e52019-07-15 13:35:21 -070068 ctx.ModuleErrorf("GetDirectDepWithTag failed")
69 }
70 }
Colin Cross1b488422019-03-04 22:33:56 -080071}
72
73func TestPathDepsMutator(t *testing.T) {
74 tests := []struct {
75 name string
76 bp string
77 deps []string
78 }{
79 {
80 name: "all",
81 bp: `
82 test {
83 name: "foo",
84 foo: ":a",
85 bar: [":b"],
Colin Cross41955e82019-05-29 14:40:35 -070086 baz: ":c{.bar}",
Colin Cross1b488422019-03-04 22:33:56 -080087 qux: ":d",
Jiyong Park66dd5c02021-02-24 01:22:57 +090088 x: [
89 {
90 y: [
91 {
92 z: [":x", ":y"],
93 },
94 {
95 z: [":z"],
96 },
97 ],
98 },
99 ],
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800100 v: {
101 w: ":w",
102 },
Colin Cross1b488422019-03-04 22:33:56 -0800103 }`,
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800104 deps: []string{"a", "b", "c", "w", "x", "y", "z"},
Colin Cross1b488422019-03-04 22:33:56 -0800105 },
Colin Crossa3a97412019-03-18 12:24:29 -0700106 {
107 name: "arch variant",
108 bp: `
109 test {
110 name: "foo",
111 arch: {
112 arm64: {
113 bar: [":a"],
114 },
115 arm: {
116 bar: [":b"],
117 },
118 },
119 bar: [":c"],
120 }`,
121 deps: []string{"c", "a"},
122 },
Colin Cross1b488422019-03-04 22:33:56 -0800123 }
124
Colin Cross1b488422019-03-04 22:33:56 -0800125 for _, test := range tests {
126 t.Run(test.name, func(t *testing.T) {
Colin Cross1b488422019-03-04 22:33:56 -0800127 bp := test.bp + `
128 filegroup {
129 name: "a",
130 }
131
132 filegroup {
133 name: "b",
134 }
135
136 filegroup {
137 name: "c",
138 }
139
140 filegroup {
141 name: "d",
142 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900143
144 filegroup {
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800145 name: "w",
146 }
147
148 filegroup {
Jiyong Park66dd5c02021-02-24 01:22:57 +0900149 name: "x",
150 }
151
152 filegroup {
153 name: "y",
154 }
155
156 filegroup {
157 name: "z",
158 }
Colin Cross1b488422019-03-04 22:33:56 -0800159 `
160
Paul Duffin30ac3e72021-03-20 00:36:14 +0000161 result := GroupFixturePreparers(
Paul Duffind518b7e2021-03-17 00:09:35 +0000162 PrepareForTestWithArchMutator,
163 PrepareForTestWithFilegroup,
164 FixtureRegisterWithContext(func(ctx RegistrationContext) {
165 ctx.RegisterModuleType("test", pathDepsMutatorTestModuleFactory)
166 }),
167 FixtureWithRootAndroidBp(bp),
Paul Duffin30ac3e72021-03-20 00:36:14 +0000168 ).RunTest(t)
Colin Cross1b488422019-03-04 22:33:56 -0800169
Paul Duffind518b7e2021-03-17 00:09:35 +0000170 m := result.Module("foo", "android_arm64_armv8-a").(*pathDepsMutatorTestModule)
Colin Cross1b488422019-03-04 22:33:56 -0800171
Paul Duffind518b7e2021-03-17 00:09:35 +0000172 AssertDeepEquals(t, "deps", test.deps, m.sourceDeps)
Colin Cross1b488422019-03-04 22:33:56 -0800173 })
174 }
175}