blob: 2c99b97a2932900dd1f976032112fa89b39a46a8 [file] [log] [blame]
Jiyong Parkdda8f692020-11-09 18:38:48 +09001// Copyright 2020 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 (
18 "reflect"
19 "testing"
Jiyong Park65b62242020-11-25 12:44:59 +090020
21 "github.com/google/blueprint"
Jiyong Parkdda8f692020-11-09 18:38:48 +090022)
23
24// Module to be packaged
25type componentTestModule struct {
26 ModuleBase
27 props struct {
28 Deps []string
29 }
30}
31
Jiyong Parkd630bdd2020-11-25 11:47:24 +090032// dep tag used in this test. All dependencies are considered as installable.
33type installDepTag struct {
34 blueprint.BaseDependencyTag
35 InstallAlwaysNeededDependencyTag
36}
37
Jiyong Parkdda8f692020-11-09 18:38:48 +090038func componentTestModuleFactory() Module {
39 m := &componentTestModule{}
40 m.AddProperties(&m.props)
41 InitAndroidArchModule(m, HostAndDeviceSupported, MultilibBoth)
42 return m
43}
44
45func (m *componentTestModule) DepsMutator(ctx BottomUpMutatorContext) {
Jiyong Parkd630bdd2020-11-25 11:47:24 +090046 ctx.AddDependency(ctx.Module(), installDepTag{}, m.props.Deps...)
Jiyong Parkdda8f692020-11-09 18:38:48 +090047}
48
49func (m *componentTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
50 builtFile := PathForModuleOut(ctx, m.Name())
51 dir := ctx.Target().Arch.ArchType.Multilib
52 installDir := PathForModuleInstall(ctx, dir)
53 ctx.InstallFile(installDir, m.Name(), builtFile)
54}
55
56// Module that itself is a package
57type packageTestModule struct {
58 ModuleBase
59 PackagingBase
60
61 entries []string
62}
63
Jiyong Park2136d152021-02-01 23:24:56 +090064func packageMultiTargetTestModuleFactory() Module {
Jiyong Parkdda8f692020-11-09 18:38:48 +090065 module := &packageTestModule{}
66 InitPackageModule(module)
67 InitAndroidMultiTargetsArchModule(module, DeviceSupported, MultilibCommon)
68 return module
69}
70
Jiyong Park2136d152021-02-01 23:24:56 +090071func packageTestModuleFactory() Module {
72 module := &packageTestModule{}
73 InitPackageModule(module)
74 InitAndroidArchModule(module, DeviceSupported, MultilibBoth)
75 return module
76}
77
Jiyong Parkdda8f692020-11-09 18:38:48 +090078func (m *packageTestModule) DepsMutator(ctx BottomUpMutatorContext) {
Jiyong Parkd630bdd2020-11-25 11:47:24 +090079 m.AddDeps(ctx, installDepTag{})
Jiyong Parkdda8f692020-11-09 18:38:48 +090080}
81
82func (m *packageTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Paul Duffin4076a752021-02-02 10:59:54 +000083 zipFile := PathForModuleOut(ctx, "myzip.zip")
Jiyong Parkdda8f692020-11-09 18:38:48 +090084 m.entries = m.CopyDepsToZip(ctx, zipFile)
85}
86
Jiyong Park2136d152021-02-01 23:24:56 +090087func runPackagingTest(t *testing.T, multitarget bool, bp string, expected []string) {
Jiyong Parkdda8f692020-11-09 18:38:48 +090088 t.Helper()
89
90 config := TestArchConfig(buildDir, nil, bp, nil)
91
92 ctx := NewTestArchContext(config)
93 ctx.RegisterModuleType("component", componentTestModuleFactory)
Jiyong Park2136d152021-02-01 23:24:56 +090094
95 var archVariant string
96 if multitarget {
97 archVariant = "android_common"
98 ctx.RegisterModuleType("package_module", packageMultiTargetTestModuleFactory)
99 } else {
100 archVariant = "android_arm64_armv8-a"
101 ctx.RegisterModuleType("package_module", packageTestModuleFactory)
102 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900103 ctx.Register()
104
105 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
106 FailIfErrored(t, errs)
107 _, errs = ctx.PrepareBuildActions(config)
108 FailIfErrored(t, errs)
109
Jiyong Park2136d152021-02-01 23:24:56 +0900110 p := ctx.ModuleForTests("package", archVariant).Module().(*packageTestModule)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900111 actual := p.entries
112 actual = SortedUniqueStrings(actual)
113 expected = SortedUniqueStrings(expected)
114 if !reflect.DeepEqual(actual, expected) {
115 t.Errorf("\ngot: %v\nexpected: %v\n", actual, expected)
116 }
117}
118
Jiyong Park2136d152021-02-01 23:24:56 +0900119func TestPackagingBaseMultiTarget(t *testing.T) {
120 multiTarget := true
121 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900122 `
123 component {
124 name: "foo",
125 }
126
127 package_module {
128 name: "package",
129 deps: ["foo"],
130 }
131 `, []string{"lib64/foo"})
132
Jiyong Park2136d152021-02-01 23:24:56 +0900133 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900134 `
135 component {
136 name: "foo",
137 deps: ["bar"],
138 }
139
140 component {
141 name: "bar",
142 }
143
144 package_module {
145 name: "package",
146 deps: ["foo"],
147 }
148 `, []string{"lib64/foo", "lib64/bar"})
149
Jiyong Park2136d152021-02-01 23:24:56 +0900150 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900151 `
152 component {
153 name: "foo",
154 deps: ["bar"],
155 }
156
157 component {
158 name: "bar",
159 }
160
161 package_module {
162 name: "package",
163 deps: ["foo"],
164 compile_multilib: "both",
165 }
166 `, []string{"lib32/foo", "lib32/bar", "lib64/foo", "lib64/bar"})
167
Jiyong Park2136d152021-02-01 23:24:56 +0900168 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900169 `
170 component {
171 name: "foo",
172 }
173
174 component {
175 name: "bar",
176 compile_multilib: "32",
177 }
178
179 package_module {
180 name: "package",
181 deps: ["foo"],
182 multilib: {
183 lib32: {
184 deps: ["bar"],
185 },
186 },
187 compile_multilib: "both",
188 }
189 `, []string{"lib32/foo", "lib32/bar", "lib64/foo"})
190
Jiyong Park2136d152021-02-01 23:24:56 +0900191 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900192 `
193 component {
194 name: "foo",
195 }
196
197 component {
198 name: "bar",
199 }
200
201 package_module {
202 name: "package",
203 deps: ["foo"],
204 multilib: {
205 first: {
206 deps: ["bar"],
207 },
208 },
209 compile_multilib: "both",
210 }
211 `, []string{"lib32/foo", "lib64/foo", "lib64/bar"})
Jiyong Park2136d152021-02-01 23:24:56 +0900212
213 runPackagingTest(t, multiTarget,
214 `
215 component {
216 name: "foo",
217 }
218
219 component {
220 name: "bar",
221 }
222
223 component {
224 name: "baz",
225 }
226
227 package_module {
228 name: "package",
229 deps: ["foo"],
230 arch: {
231 arm64: {
232 deps: ["bar"],
233 },
234 x86_64: {
235 deps: ["baz"],
236 },
237 },
238 compile_multilib: "both",
239 }
240 `, []string{"lib32/foo", "lib64/foo", "lib64/bar"})
241}
242
243func TestPackagingBaseSingleTarget(t *testing.T) {
244 multiTarget := false
245 runPackagingTest(t, multiTarget,
246 `
247 component {
248 name: "foo",
249 }
250
251 package_module {
252 name: "package",
253 deps: ["foo"],
254 }
255 `, []string{"lib64/foo"})
256
257 runPackagingTest(t, multiTarget,
258 `
259 component {
260 name: "foo",
261 deps: ["bar"],
262 }
263
264 component {
265 name: "bar",
266 }
267
268 package_module {
269 name: "package",
270 deps: ["foo"],
271 }
272 `, []string{"lib64/foo", "lib64/bar"})
273
274 runPackagingTest(t, multiTarget,
275 `
276 component {
277 name: "foo",
278 }
279
280 component {
281 name: "bar",
282 compile_multilib: "32",
283 }
284
285 package_module {
286 name: "package",
287 deps: ["foo"],
288 multilib: {
289 lib32: {
290 deps: ["bar"],
291 },
292 },
293 }
294 `, []string{"lib64/foo"})
295
296 runPackagingTest(t, multiTarget,
297 `
298 component {
299 name: "foo",
300 }
301
302 component {
303 name: "bar",
304 }
305
306 package_module {
307 name: "package",
308 deps: ["foo"],
309 multilib: {
310 lib64: {
311 deps: ["bar"],
312 },
313 },
314 }
315 `, []string{"lib64/foo", "lib64/bar"})
316
317 runPackagingTest(t, multiTarget,
318 `
319 component {
320 name: "foo",
321 }
322
323 component {
324 name: "bar",
325 }
326
327 component {
328 name: "baz",
329 }
330
331 package_module {
332 name: "package",
333 deps: ["foo"],
334 arch: {
335 arm64: {
336 deps: ["bar"],
337 },
338 x86_64: {
339 deps: ["baz"],
340 },
341 },
342 }
343 `, []string{"lib64/foo", "lib64/bar"})
Jiyong Parkdda8f692020-11-09 18:38:48 +0900344}