blob: e5a06a26bd3eb23c56a44b9563728be82224a400 [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
Jooyung Han092ef812021-03-10 15:40:34 +090060 properties struct {
61 Install_deps []string `android:`
62 }
Jiyong Parkdda8f692020-11-09 18:38:48 +090063 entries []string
64}
65
Jiyong Park2136d152021-02-01 23:24:56 +090066func packageMultiTargetTestModuleFactory() Module {
Jiyong Parkdda8f692020-11-09 18:38:48 +090067 module := &packageTestModule{}
68 InitPackageModule(module)
69 InitAndroidMultiTargetsArchModule(module, DeviceSupported, MultilibCommon)
Jooyung Han092ef812021-03-10 15:40:34 +090070 module.AddProperties(&module.properties)
Jiyong Parkdda8f692020-11-09 18:38:48 +090071 return module
72}
73
Jiyong Park2136d152021-02-01 23:24:56 +090074func packageTestModuleFactory() Module {
75 module := &packageTestModule{}
76 InitPackageModule(module)
77 InitAndroidArchModule(module, DeviceSupported, MultilibBoth)
Jooyung Han092ef812021-03-10 15:40:34 +090078 module.AddProperties(&module.properties)
Jiyong Park2136d152021-02-01 23:24:56 +090079 return module
80}
81
Jooyung Han092ef812021-03-10 15:40:34 +090082type packagingDepTag struct {
83 blueprint.BaseDependencyTag
84 PackagingItemAlwaysDepTag
85}
86
Jiyong Parkdda8f692020-11-09 18:38:48 +090087func (m *packageTestModule) DepsMutator(ctx BottomUpMutatorContext) {
Jooyung Han092ef812021-03-10 15:40:34 +090088 m.AddDeps(ctx, packagingDepTag{})
89 ctx.AddDependency(ctx.Module(), installDepTag{}, m.properties.Install_deps...)
Jiyong Parkdda8f692020-11-09 18:38:48 +090090}
91
92func (m *packageTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Paul Duffin4076a752021-02-02 10:59:54 +000093 zipFile := PathForModuleOut(ctx, "myzip.zip")
Jiyong Parkdda8f692020-11-09 18:38:48 +090094 m.entries = m.CopyDepsToZip(ctx, zipFile)
95}
96
Jiyong Park2136d152021-02-01 23:24:56 +090097func runPackagingTest(t *testing.T, multitarget bool, bp string, expected []string) {
Jiyong Parkdda8f692020-11-09 18:38:48 +090098 t.Helper()
99
100 config := TestArchConfig(buildDir, nil, bp, nil)
101
102 ctx := NewTestArchContext(config)
103 ctx.RegisterModuleType("component", componentTestModuleFactory)
Jiyong Park2136d152021-02-01 23:24:56 +0900104
105 var archVariant string
106 if multitarget {
107 archVariant = "android_common"
108 ctx.RegisterModuleType("package_module", packageMultiTargetTestModuleFactory)
109 } else {
110 archVariant = "android_arm64_armv8-a"
111 ctx.RegisterModuleType("package_module", packageTestModuleFactory)
112 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900113 ctx.Register()
114
115 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
116 FailIfErrored(t, errs)
117 _, errs = ctx.PrepareBuildActions(config)
118 FailIfErrored(t, errs)
119
Jiyong Park2136d152021-02-01 23:24:56 +0900120 p := ctx.ModuleForTests("package", archVariant).Module().(*packageTestModule)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900121 actual := p.entries
122 actual = SortedUniqueStrings(actual)
123 expected = SortedUniqueStrings(expected)
124 if !reflect.DeepEqual(actual, expected) {
125 t.Errorf("\ngot: %v\nexpected: %v\n", actual, expected)
126 }
127}
128
Jiyong Park2136d152021-02-01 23:24:56 +0900129func TestPackagingBaseMultiTarget(t *testing.T) {
130 multiTarget := true
131 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900132 `
133 component {
134 name: "foo",
135 }
136
137 package_module {
138 name: "package",
139 deps: ["foo"],
140 }
141 `, []string{"lib64/foo"})
142
Jiyong Park2136d152021-02-01 23:24:56 +0900143 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900144 `
145 component {
146 name: "foo",
147 deps: ["bar"],
148 }
149
150 component {
151 name: "bar",
152 }
153
154 package_module {
155 name: "package",
156 deps: ["foo"],
157 }
158 `, []string{"lib64/foo", "lib64/bar"})
159
Jiyong Park2136d152021-02-01 23:24:56 +0900160 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900161 `
162 component {
163 name: "foo",
164 deps: ["bar"],
165 }
166
167 component {
168 name: "bar",
169 }
170
171 package_module {
172 name: "package",
173 deps: ["foo"],
174 compile_multilib: "both",
175 }
176 `, []string{"lib32/foo", "lib32/bar", "lib64/foo", "lib64/bar"})
177
Jiyong Park2136d152021-02-01 23:24:56 +0900178 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900179 `
180 component {
181 name: "foo",
182 }
183
184 component {
185 name: "bar",
186 compile_multilib: "32",
187 }
188
189 package_module {
190 name: "package",
191 deps: ["foo"],
192 multilib: {
193 lib32: {
194 deps: ["bar"],
195 },
196 },
197 compile_multilib: "both",
198 }
199 `, []string{"lib32/foo", "lib32/bar", "lib64/foo"})
200
Jiyong Park2136d152021-02-01 23:24:56 +0900201 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900202 `
203 component {
204 name: "foo",
205 }
206
207 component {
208 name: "bar",
209 }
210
211 package_module {
212 name: "package",
213 deps: ["foo"],
214 multilib: {
215 first: {
216 deps: ["bar"],
217 },
218 },
219 compile_multilib: "both",
220 }
221 `, []string{"lib32/foo", "lib64/foo", "lib64/bar"})
Jiyong Park2136d152021-02-01 23:24:56 +0900222
223 runPackagingTest(t, multiTarget,
224 `
225 component {
226 name: "foo",
227 }
228
229 component {
230 name: "bar",
231 }
232
233 component {
234 name: "baz",
235 }
236
237 package_module {
238 name: "package",
239 deps: ["foo"],
240 arch: {
241 arm64: {
242 deps: ["bar"],
243 },
244 x86_64: {
245 deps: ["baz"],
246 },
247 },
248 compile_multilib: "both",
249 }
250 `, []string{"lib32/foo", "lib64/foo", "lib64/bar"})
251}
252
253func TestPackagingBaseSingleTarget(t *testing.T) {
254 multiTarget := false
255 runPackagingTest(t, multiTarget,
256 `
257 component {
258 name: "foo",
259 }
260
261 package_module {
262 name: "package",
263 deps: ["foo"],
264 }
265 `, []string{"lib64/foo"})
266
267 runPackagingTest(t, multiTarget,
268 `
269 component {
270 name: "foo",
271 deps: ["bar"],
272 }
273
274 component {
275 name: "bar",
276 }
277
278 package_module {
279 name: "package",
280 deps: ["foo"],
281 }
282 `, []string{"lib64/foo", "lib64/bar"})
283
284 runPackagingTest(t, multiTarget,
285 `
286 component {
287 name: "foo",
288 }
289
290 component {
291 name: "bar",
292 compile_multilib: "32",
293 }
294
295 package_module {
296 name: "package",
297 deps: ["foo"],
298 multilib: {
299 lib32: {
300 deps: ["bar"],
301 },
302 },
303 }
304 `, []string{"lib64/foo"})
305
306 runPackagingTest(t, multiTarget,
307 `
308 component {
309 name: "foo",
310 }
311
312 component {
313 name: "bar",
314 }
315
316 package_module {
317 name: "package",
318 deps: ["foo"],
319 multilib: {
320 lib64: {
321 deps: ["bar"],
322 },
323 },
324 }
325 `, []string{"lib64/foo", "lib64/bar"})
326
327 runPackagingTest(t, multiTarget,
328 `
329 component {
330 name: "foo",
331 }
332
333 component {
334 name: "bar",
335 }
336
337 component {
338 name: "baz",
339 }
340
341 package_module {
342 name: "package",
343 deps: ["foo"],
344 arch: {
345 arm64: {
346 deps: ["bar"],
347 },
348 x86_64: {
349 deps: ["baz"],
350 },
351 },
352 }
353 `, []string{"lib64/foo", "lib64/bar"})
Jooyung Han092ef812021-03-10 15:40:34 +0900354
355 runPackagingTest(t, multiTarget,
356 `
357 component {
358 name: "foo",
359 }
360
361 component {
362 name: "bar",
363 }
364
365 package_module {
366 name: "package",
367 deps: ["foo"],
368 install_deps: ["bar"],
369 }
370 `, []string{"lib64/foo"})
Jiyong Parkdda8f692020-11-09 18:38:48 +0900371}