blob: 85c96eee379bbecc7bd1b04b33ede91563e06a3f [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
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -080029 V *struct {
30 W string `android:"path"`
31 }
Colin Cross1b488422019-03-04 22:33:56 -080032 }
33
Colin Cross527f3e52019-07-15 13:35:21 -070034 // A second property struct with a duplicate property name
35 props2 struct {
36 Foo string `android:"path"`
37 }
38
Jiyong Park66dd5c02021-02-24 01:22:57 +090039 // nested slices of struct
40 props3 struct {
41 X []struct {
42 Y []struct {
43 Z []string `android:"path"`
44 }
45 }
46 }
47
Colin Cross1b488422019-03-04 22:33:56 -080048 sourceDeps []string
49}
50
51func pathDepsMutatorTestModuleFactory() Module {
52 module := &pathDepsMutatorTestModule{}
Jiyong Park66dd5c02021-02-24 01:22:57 +090053 module.AddProperties(&module.props, &module.props2, &module.props3)
Colin Crossa3a97412019-03-18 12:24:29 -070054 InitAndroidArchModule(module, DeviceSupported, MultilibBoth)
Colin Cross1b488422019-03-04 22:33:56 -080055 return module
56}
57
58func (p *pathDepsMutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Cross41955e82019-05-29 14:40:35 -070059 ctx.VisitDirectDeps(func(dep Module) {
60 if _, ok := ctx.OtherModuleDependencyTag(dep).(sourceOrOutputDependencyTag); ok {
61 p.sourceDeps = append(p.sourceDeps, ctx.OtherModuleName(dep))
62 }
Colin Cross1b488422019-03-04 22:33:56 -080063 })
Colin Cross527f3e52019-07-15 13:35:21 -070064
65 if p.props.Foo != "" {
66 // Make sure there is only one dependency on a module listed in a property present in multiple property structs
67 if ctx.GetDirectDepWithTag(SrcIsModule(p.props.Foo), sourceOrOutputDepTag("")) == nil {
68 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
Colin Cross98be1bb2019-12-13 20:41:13 -0800161 config := TestArchConfig(buildDir, nil, bp, nil)
Colin Crossae8600b2020-10-29 17:09:13 -0700162 ctx := NewTestArchContext(config)
Colin Cross1b488422019-03-04 22:33:56 -0800163
Colin Cross98be1bb2019-12-13 20:41:13 -0800164 ctx.RegisterModuleType("test", pathDepsMutatorTestModuleFactory)
165 ctx.RegisterModuleType("filegroup", FileGroupFactory)
Colin Cross1b488422019-03-04 22:33:56 -0800166
Colin Crossae8600b2020-10-29 17:09:13 -0700167 ctx.Register()
Colin Cross1b488422019-03-04 22:33:56 -0800168 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
169 FailIfErrored(t, errs)
170 _, errs = ctx.PrepareBuildActions(config)
171 FailIfErrored(t, errs)
172
Colin Crossa3a97412019-03-18 12:24:29 -0700173 m := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*pathDepsMutatorTestModule)
Colin Cross1b488422019-03-04 22:33:56 -0800174
175 if g, w := m.sourceDeps, test.deps; !reflect.DeepEqual(g, w) {
176 t.Errorf("want deps %q, got %q", w, g)
177 }
178 })
179 }
180}