blob: 2aab748355874f21a608fa7ca2d2705981fa5b59 [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
Jiyong Park66dd5c02021-02-24 01:22:57 +090036 // nested slices of struct
37 props3 struct {
38 X []struct {
39 Y []struct {
40 Z []string `android:"path"`
41 }
42 }
43 }
44
Colin Cross1b488422019-03-04 22:33:56 -080045 sourceDeps []string
46}
47
48func pathDepsMutatorTestModuleFactory() Module {
49 module := &pathDepsMutatorTestModule{}
Jiyong Park66dd5c02021-02-24 01:22:57 +090050 module.AddProperties(&module.props, &module.props2, &module.props3)
Colin Crossa3a97412019-03-18 12:24:29 -070051 InitAndroidArchModule(module, DeviceSupported, MultilibBoth)
Colin Cross1b488422019-03-04 22:33:56 -080052 return module
53}
54
55func (p *pathDepsMutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Cross41955e82019-05-29 14:40:35 -070056 ctx.VisitDirectDeps(func(dep Module) {
57 if _, ok := ctx.OtherModuleDependencyTag(dep).(sourceOrOutputDependencyTag); ok {
58 p.sourceDeps = append(p.sourceDeps, ctx.OtherModuleName(dep))
59 }
Colin Cross1b488422019-03-04 22:33:56 -080060 })
Colin Cross527f3e52019-07-15 13:35:21 -070061
62 if p.props.Foo != "" {
63 // Make sure there is only one dependency on a module listed in a property present in multiple property structs
64 if ctx.GetDirectDepWithTag(SrcIsModule(p.props.Foo), sourceOrOutputDepTag("")) == nil {
65 ctx.ModuleErrorf("GetDirectDepWithTag failed")
66 }
67 }
Colin Cross1b488422019-03-04 22:33:56 -080068}
69
70func TestPathDepsMutator(t *testing.T) {
71 tests := []struct {
72 name string
73 bp string
74 deps []string
75 }{
76 {
77 name: "all",
78 bp: `
79 test {
80 name: "foo",
81 foo: ":a",
82 bar: [":b"],
Colin Cross41955e82019-05-29 14:40:35 -070083 baz: ":c{.bar}",
Colin Cross1b488422019-03-04 22:33:56 -080084 qux: ":d",
Jiyong Park66dd5c02021-02-24 01:22:57 +090085 x: [
86 {
87 y: [
88 {
89 z: [":x", ":y"],
90 },
91 {
92 z: [":z"],
93 },
94 ],
95 },
96 ],
Colin Cross1b488422019-03-04 22:33:56 -080097 }`,
Jiyong Park66dd5c02021-02-24 01:22:57 +090098 deps: []string{"a", "b", "c", "x", "y", "z"},
Colin Cross1b488422019-03-04 22:33:56 -080099 },
Colin Crossa3a97412019-03-18 12:24:29 -0700100 {
101 name: "arch variant",
102 bp: `
103 test {
104 name: "foo",
105 arch: {
106 arm64: {
107 bar: [":a"],
108 },
109 arm: {
110 bar: [":b"],
111 },
112 },
113 bar: [":c"],
114 }`,
115 deps: []string{"c", "a"},
116 },
Colin Cross1b488422019-03-04 22:33:56 -0800117 }
118
Colin Cross1b488422019-03-04 22:33:56 -0800119 for _, test := range tests {
120 t.Run(test.name, func(t *testing.T) {
Colin Cross1b488422019-03-04 22:33:56 -0800121 bp := test.bp + `
122 filegroup {
123 name: "a",
124 }
125
126 filegroup {
127 name: "b",
128 }
129
130 filegroup {
131 name: "c",
132 }
133
134 filegroup {
135 name: "d",
136 }
Jiyong Park66dd5c02021-02-24 01:22:57 +0900137
138 filegroup {
139 name: "x",
140 }
141
142 filegroup {
143 name: "y",
144 }
145
146 filegroup {
147 name: "z",
148 }
Colin Cross1b488422019-03-04 22:33:56 -0800149 `
150
Colin Cross98be1bb2019-12-13 20:41:13 -0800151 config := TestArchConfig(buildDir, nil, bp, nil)
Colin Crossae8600b2020-10-29 17:09:13 -0700152 ctx := NewTestArchContext(config)
Colin Cross1b488422019-03-04 22:33:56 -0800153
Colin Cross98be1bb2019-12-13 20:41:13 -0800154 ctx.RegisterModuleType("test", pathDepsMutatorTestModuleFactory)
155 ctx.RegisterModuleType("filegroup", FileGroupFactory)
Colin Cross1b488422019-03-04 22:33:56 -0800156
Colin Crossae8600b2020-10-29 17:09:13 -0700157 ctx.Register()
Colin Cross1b488422019-03-04 22:33:56 -0800158 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
159 FailIfErrored(t, errs)
160 _, errs = ctx.PrepareBuildActions(config)
161 FailIfErrored(t, errs)
162
Colin Crossa3a97412019-03-18 12:24:29 -0700163 m := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*pathDepsMutatorTestModule)
Colin Cross1b488422019-03-04 22:33:56 -0800164
165 if g, w := m.sourceDeps, test.deps; !reflect.DeepEqual(g, w) {
166 t.Errorf("want deps %q, got %q", w, g)
167 }
168 })
169 }
170}