blob: 91ac1f386ac1df6878c5ffab5f63897a6c343bd2 [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 (
Jiyong Parkdda8f692020-11-09 18:38:48 +090018 "testing"
Jiyong Park65b62242020-11-25 12:44:59 +090019
20 "github.com/google/blueprint"
Jooyung Han8707cd72021-07-23 02:49:46 +090021 "github.com/google/blueprint/proptools"
Jiyong Parkdda8f692020-11-09 18:38:48 +090022)
23
24// Module to be packaged
25type componentTestModule struct {
26 ModuleBase
27 props struct {
Jooyung Han8707cd72021-07-23 02:49:46 +090028 Deps []string
29 Skip_install *bool
Jiyong Parkdda8f692020-11-09 18:38:48 +090030 }
31}
32
Jiyong Parkd630bdd2020-11-25 11:47:24 +090033// dep tag used in this test. All dependencies are considered as installable.
34type installDepTag struct {
35 blueprint.BaseDependencyTag
36 InstallAlwaysNeededDependencyTag
37}
38
Jiyong Parkdda8f692020-11-09 18:38:48 +090039func componentTestModuleFactory() Module {
40 m := &componentTestModule{}
41 m.AddProperties(&m.props)
42 InitAndroidArchModule(m, HostAndDeviceSupported, MultilibBoth)
43 return m
44}
45
46func (m *componentTestModule) DepsMutator(ctx BottomUpMutatorContext) {
Jiyong Parkd630bdd2020-11-25 11:47:24 +090047 ctx.AddDependency(ctx.Module(), installDepTag{}, m.props.Deps...)
Jiyong Parkdda8f692020-11-09 18:38:48 +090048}
49
50func (m *componentTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
51 builtFile := PathForModuleOut(ctx, m.Name())
52 dir := ctx.Target().Arch.ArchType.Multilib
53 installDir := PathForModuleInstall(ctx, dir)
Jooyung Han8707cd72021-07-23 02:49:46 +090054 if proptools.Bool(m.props.Skip_install) {
55 m.SkipInstall()
56 }
Jiyong Parkdda8f692020-11-09 18:38:48 +090057 ctx.InstallFile(installDir, m.Name(), builtFile)
58}
59
60// Module that itself is a package
61type packageTestModule struct {
62 ModuleBase
63 PackagingBase
Jooyung Han092ef812021-03-10 15:40:34 +090064 properties struct {
65 Install_deps []string `android:`
66 }
Jiyong Parkdda8f692020-11-09 18:38:48 +090067 entries []string
68}
69
Jiyong Park2136d152021-02-01 23:24:56 +090070func packageMultiTargetTestModuleFactory() Module {
Jiyong Parkdda8f692020-11-09 18:38:48 +090071 module := &packageTestModule{}
72 InitPackageModule(module)
73 InitAndroidMultiTargetsArchModule(module, DeviceSupported, MultilibCommon)
Jooyung Han092ef812021-03-10 15:40:34 +090074 module.AddProperties(&module.properties)
Jiyong Parkdda8f692020-11-09 18:38:48 +090075 return module
76}
77
Jiyong Park2136d152021-02-01 23:24:56 +090078func packageTestModuleFactory() Module {
79 module := &packageTestModule{}
80 InitPackageModule(module)
81 InitAndroidArchModule(module, DeviceSupported, MultilibBoth)
Jooyung Han092ef812021-03-10 15:40:34 +090082 module.AddProperties(&module.properties)
Jiyong Park2136d152021-02-01 23:24:56 +090083 return module
84}
85
Jooyung Han092ef812021-03-10 15:40:34 +090086type packagingDepTag struct {
87 blueprint.BaseDependencyTag
88 PackagingItemAlwaysDepTag
89}
90
Jiyong Parkdda8f692020-11-09 18:38:48 +090091func (m *packageTestModule) DepsMutator(ctx BottomUpMutatorContext) {
Jooyung Han092ef812021-03-10 15:40:34 +090092 m.AddDeps(ctx, packagingDepTag{})
93 ctx.AddDependency(ctx.Module(), installDepTag{}, m.properties.Install_deps...)
Jiyong Parkdda8f692020-11-09 18:38:48 +090094}
95
96func (m *packageTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Paul Duffin4076a752021-02-02 10:59:54 +000097 zipFile := PathForModuleOut(ctx, "myzip.zip")
Jooyung Hana8834282022-03-25 11:40:12 +090098 m.entries = m.CopyDepsToZip(ctx, m.GatherPackagingSpecs(ctx), zipFile)
Jiyong Parkdda8f692020-11-09 18:38:48 +090099}
100
Jiyong Park2136d152021-02-01 23:24:56 +0900101func runPackagingTest(t *testing.T, multitarget bool, bp string, expected []string) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900102 t.Helper()
103
Jiyong Park2136d152021-02-01 23:24:56 +0900104 var archVariant string
Paul Duffinc3bdd312021-03-17 00:06:49 +0000105 var moduleFactory ModuleFactory
Jiyong Park2136d152021-02-01 23:24:56 +0900106 if multitarget {
107 archVariant = "android_common"
Paul Duffinc3bdd312021-03-17 00:06:49 +0000108 moduleFactory = packageMultiTargetTestModuleFactory
Jiyong Park2136d152021-02-01 23:24:56 +0900109 } else {
110 archVariant = "android_arm64_armv8-a"
Paul Duffinc3bdd312021-03-17 00:06:49 +0000111 moduleFactory = packageTestModuleFactory
Jiyong Park2136d152021-02-01 23:24:56 +0900112 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900113
Paul Duffin30ac3e72021-03-20 00:36:14 +0000114 result := GroupFixturePreparers(
Paul Duffinc3bdd312021-03-17 00:06:49 +0000115 PrepareForTestWithArchMutator,
116 FixtureRegisterWithContext(func(ctx RegistrationContext) {
117 ctx.RegisterModuleType("component", componentTestModuleFactory)
118 ctx.RegisterModuleType("package_module", moduleFactory)
119 }),
120 FixtureWithRootAndroidBp(bp),
Paul Duffin30ac3e72021-03-20 00:36:14 +0000121 ).RunTest(t)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900122
Paul Duffinc3bdd312021-03-17 00:06:49 +0000123 p := result.Module("package", archVariant).(*packageTestModule)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900124 actual := p.entries
125 actual = SortedUniqueStrings(actual)
126 expected = SortedUniqueStrings(expected)
Paul Duffinc3bdd312021-03-17 00:06:49 +0000127 AssertDeepEquals(t, "package entries", expected, actual)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900128}
129
Jiyong Park2136d152021-02-01 23:24:56 +0900130func TestPackagingBaseMultiTarget(t *testing.T) {
131 multiTarget := true
132 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900133 `
134 component {
135 name: "foo",
136 }
137
138 package_module {
139 name: "package",
140 deps: ["foo"],
141 }
142 `, []string{"lib64/foo"})
143
Jiyong Park2136d152021-02-01 23:24:56 +0900144 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900145 `
146 component {
147 name: "foo",
148 deps: ["bar"],
149 }
150
151 component {
152 name: "bar",
153 }
154
155 package_module {
156 name: "package",
157 deps: ["foo"],
158 }
159 `, []string{"lib64/foo", "lib64/bar"})
160
Jiyong Park2136d152021-02-01 23:24:56 +0900161 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900162 `
163 component {
164 name: "foo",
165 deps: ["bar"],
166 }
167
168 component {
169 name: "bar",
170 }
171
172 package_module {
173 name: "package",
174 deps: ["foo"],
175 compile_multilib: "both",
176 }
177 `, []string{"lib32/foo", "lib32/bar", "lib64/foo", "lib64/bar"})
178
Jiyong Park2136d152021-02-01 23:24:56 +0900179 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900180 `
181 component {
182 name: "foo",
183 }
184
185 component {
186 name: "bar",
187 compile_multilib: "32",
188 }
189
190 package_module {
191 name: "package",
192 deps: ["foo"],
193 multilib: {
194 lib32: {
195 deps: ["bar"],
196 },
197 },
198 compile_multilib: "both",
199 }
200 `, []string{"lib32/foo", "lib32/bar", "lib64/foo"})
201
Jiyong Park2136d152021-02-01 23:24:56 +0900202 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900203 `
204 component {
205 name: "foo",
206 }
207
208 component {
209 name: "bar",
210 }
211
212 package_module {
213 name: "package",
214 deps: ["foo"],
215 multilib: {
216 first: {
217 deps: ["bar"],
218 },
219 },
220 compile_multilib: "both",
221 }
222 `, []string{"lib32/foo", "lib64/foo", "lib64/bar"})
Jiyong Park2136d152021-02-01 23:24:56 +0900223
224 runPackagingTest(t, multiTarget,
225 `
226 component {
227 name: "foo",
228 }
229
230 component {
231 name: "bar",
232 }
233
234 component {
235 name: "baz",
236 }
237
238 package_module {
239 name: "package",
240 deps: ["foo"],
241 arch: {
242 arm64: {
243 deps: ["bar"],
244 },
245 x86_64: {
246 deps: ["baz"],
247 },
248 },
249 compile_multilib: "both",
250 }
251 `, []string{"lib32/foo", "lib64/foo", "lib64/bar"})
252}
253
254func TestPackagingBaseSingleTarget(t *testing.T) {
255 multiTarget := false
256 runPackagingTest(t, multiTarget,
257 `
258 component {
259 name: "foo",
260 }
261
262 package_module {
263 name: "package",
264 deps: ["foo"],
265 }
266 `, []string{"lib64/foo"})
267
268 runPackagingTest(t, multiTarget,
269 `
270 component {
271 name: "foo",
272 deps: ["bar"],
273 }
274
275 component {
276 name: "bar",
277 }
278
279 package_module {
280 name: "package",
281 deps: ["foo"],
282 }
283 `, []string{"lib64/foo", "lib64/bar"})
284
285 runPackagingTest(t, multiTarget,
286 `
287 component {
288 name: "foo",
289 }
290
291 component {
292 name: "bar",
293 compile_multilib: "32",
294 }
295
296 package_module {
297 name: "package",
298 deps: ["foo"],
299 multilib: {
300 lib32: {
301 deps: ["bar"],
302 },
303 },
304 }
305 `, []string{"lib64/foo"})
306
307 runPackagingTest(t, multiTarget,
308 `
309 component {
310 name: "foo",
311 }
312
313 component {
314 name: "bar",
315 }
316
317 package_module {
318 name: "package",
319 deps: ["foo"],
320 multilib: {
321 lib64: {
322 deps: ["bar"],
323 },
324 },
325 }
326 `, []string{"lib64/foo", "lib64/bar"})
327
328 runPackagingTest(t, multiTarget,
329 `
330 component {
331 name: "foo",
332 }
333
334 component {
335 name: "bar",
336 }
337
338 component {
339 name: "baz",
340 }
341
342 package_module {
343 name: "package",
344 deps: ["foo"],
345 arch: {
346 arm64: {
347 deps: ["bar"],
348 },
349 x86_64: {
350 deps: ["baz"],
351 },
352 },
353 }
354 `, []string{"lib64/foo", "lib64/bar"})
Jooyung Han092ef812021-03-10 15:40:34 +0900355
356 runPackagingTest(t, multiTarget,
357 `
358 component {
359 name: "foo",
360 }
361
362 component {
363 name: "bar",
364 }
365
366 package_module {
367 name: "package",
368 deps: ["foo"],
369 install_deps: ["bar"],
370 }
371 `, []string{"lib64/foo"})
Jiyong Parkdda8f692020-11-09 18:38:48 +0900372}
Jooyung Han8707cd72021-07-23 02:49:46 +0900373
374func TestPackagingWithSkipInstallDeps(t *testing.T) {
375 // package -[dep]-> foo -[dep]-> bar -[dep]-> baz
376 // OK SKIPPED
377 multiTarget := false
378 runPackagingTest(t, multiTarget,
379 `
380 component {
381 name: "foo",
382 deps: ["bar"],
383 }
384
385 component {
386 name: "bar",
387 deps: ["baz"],
388 skip_install: true,
389 }
390
391 component {
392 name: "baz",
393 }
394
395 package_module {
396 name: "package",
397 deps: ["foo"],
398 }
399 `, []string{"lib64/foo"})
400}