blob: 4b72c24f9574454a454b355bca480d09cdff438a [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 {
Jiyong Park1fb7c352024-05-10 11:17:33 +090028 Deps []string
29 Build_only_deps []string
30 Skip_install *bool
Jiyong Parkdda8f692020-11-09 18:38:48 +090031 }
32}
33
Jiyong Parkd630bdd2020-11-25 11:47:24 +090034// dep tag used in this test. All dependencies are considered as installable.
35type installDepTag struct {
36 blueprint.BaseDependencyTag
37 InstallAlwaysNeededDependencyTag
38}
39
Jiyong Park1fb7c352024-05-10 11:17:33 +090040// dep tag for build_only_deps
41type buildOnlyDepTag struct {
42 blueprint.BaseDependencyTag
43 InstallAlwaysNeededDependencyTag
44}
45
46var _ SkipToTransitiveDepsTag = (*buildOnlyDepTag)(nil)
47
48func (tag buildOnlyDepTag) SkipToTransitiveDeps() bool {
49 return true
50}
51
Jiyong Parkdda8f692020-11-09 18:38:48 +090052func componentTestModuleFactory() Module {
53 m := &componentTestModule{}
54 m.AddProperties(&m.props)
55 InitAndroidArchModule(m, HostAndDeviceSupported, MultilibBoth)
56 return m
57}
58
59func (m *componentTestModule) DepsMutator(ctx BottomUpMutatorContext) {
Jiyong Parkd630bdd2020-11-25 11:47:24 +090060 ctx.AddDependency(ctx.Module(), installDepTag{}, m.props.Deps...)
Jiyong Park1fb7c352024-05-10 11:17:33 +090061 ctx.AddDependency(ctx.Module(), buildOnlyDepTag{}, m.props.Build_only_deps...)
Jiyong Parkdda8f692020-11-09 18:38:48 +090062}
63
64func (m *componentTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Jingwen Chen8ac7d7d2023-03-20 11:05:16 +000065 builtFile := PathForModuleOut(ctx, m.Name())
Jiyong Parkdda8f692020-11-09 18:38:48 +090066 dir := ctx.Target().Arch.ArchType.Multilib
67 installDir := PathForModuleInstall(ctx, dir)
Jooyung Han8707cd72021-07-23 02:49:46 +090068 if proptools.Bool(m.props.Skip_install) {
69 m.SkipInstall()
70 }
Jingwen Chen8ac7d7d2023-03-20 11:05:16 +000071 ctx.InstallFile(installDir, m.Name(), builtFile)
Jiyong Parkdda8f692020-11-09 18:38:48 +090072}
73
74// Module that itself is a package
75type packageTestModule struct {
76 ModuleBase
77 PackagingBase
Jooyung Han092ef812021-03-10 15:40:34 +090078 properties struct {
79 Install_deps []string `android:`
80 }
Jiyong Parkdda8f692020-11-09 18:38:48 +090081 entries []string
82}
83
Jiyong Park2136d152021-02-01 23:24:56 +090084func packageMultiTargetTestModuleFactory() Module {
Jiyong Parkdda8f692020-11-09 18:38:48 +090085 module := &packageTestModule{}
86 InitPackageModule(module)
87 InitAndroidMultiTargetsArchModule(module, DeviceSupported, MultilibCommon)
Jooyung Han092ef812021-03-10 15:40:34 +090088 module.AddProperties(&module.properties)
Jiyong Parkdda8f692020-11-09 18:38:48 +090089 return module
90}
91
Jiyong Park2136d152021-02-01 23:24:56 +090092func packageTestModuleFactory() Module {
93 module := &packageTestModule{}
94 InitPackageModule(module)
95 InitAndroidArchModule(module, DeviceSupported, MultilibBoth)
Jooyung Han092ef812021-03-10 15:40:34 +090096 module.AddProperties(&module.properties)
Jiyong Park2136d152021-02-01 23:24:56 +090097 return module
98}
99
Jooyung Han092ef812021-03-10 15:40:34 +0900100type packagingDepTag struct {
101 blueprint.BaseDependencyTag
102 PackagingItemAlwaysDepTag
103}
104
Jiyong Parkdda8f692020-11-09 18:38:48 +0900105func (m *packageTestModule) DepsMutator(ctx BottomUpMutatorContext) {
Jooyung Han092ef812021-03-10 15:40:34 +0900106 m.AddDeps(ctx, packagingDepTag{})
107 ctx.AddDependency(ctx.Module(), installDepTag{}, m.properties.Install_deps...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900108}
109
110func (m *packageTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Paul Duffin4076a752021-02-02 10:59:54 +0000111 zipFile := PathForModuleOut(ctx, "myzip.zip")
Jooyung Hana8834282022-03-25 11:40:12 +0900112 m.entries = m.CopyDepsToZip(ctx, m.GatherPackagingSpecs(ctx), zipFile)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900113}
114
Jiyong Park2136d152021-02-01 23:24:56 +0900115func runPackagingTest(t *testing.T, multitarget bool, bp string, expected []string) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900116 t.Helper()
117
Jiyong Park2136d152021-02-01 23:24:56 +0900118 var archVariant string
Paul Duffinc3bdd312021-03-17 00:06:49 +0000119 var moduleFactory ModuleFactory
Jiyong Park2136d152021-02-01 23:24:56 +0900120 if multitarget {
121 archVariant = "android_common"
Paul Duffinc3bdd312021-03-17 00:06:49 +0000122 moduleFactory = packageMultiTargetTestModuleFactory
Jiyong Park2136d152021-02-01 23:24:56 +0900123 } else {
124 archVariant = "android_arm64_armv8-a"
Paul Duffinc3bdd312021-03-17 00:06:49 +0000125 moduleFactory = packageTestModuleFactory
Jiyong Park2136d152021-02-01 23:24:56 +0900126 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900127
Paul Duffin30ac3e72021-03-20 00:36:14 +0000128 result := GroupFixturePreparers(
Paul Duffinc3bdd312021-03-17 00:06:49 +0000129 PrepareForTestWithArchMutator,
130 FixtureRegisterWithContext(func(ctx RegistrationContext) {
131 ctx.RegisterModuleType("component", componentTestModuleFactory)
132 ctx.RegisterModuleType("package_module", moduleFactory)
133 }),
134 FixtureWithRootAndroidBp(bp),
Paul Duffin30ac3e72021-03-20 00:36:14 +0000135 ).RunTest(t)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900136
Paul Duffinc3bdd312021-03-17 00:06:49 +0000137 p := result.Module("package", archVariant).(*packageTestModule)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900138 actual := p.entries
139 actual = SortedUniqueStrings(actual)
140 expected = SortedUniqueStrings(expected)
Paul Duffinc3bdd312021-03-17 00:06:49 +0000141 AssertDeepEquals(t, "package entries", expected, actual)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900142}
143
Jiyong Park2136d152021-02-01 23:24:56 +0900144func TestPackagingBaseMultiTarget(t *testing.T) {
145 multiTarget := true
146 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900147 `
148 component {
149 name: "foo",
150 }
151
152 package_module {
153 name: "package",
154 deps: ["foo"],
155 }
156 `, []string{"lib64/foo"})
157
Jiyong Park2136d152021-02-01 23:24:56 +0900158 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900159 `
160 component {
161 name: "foo",
162 deps: ["bar"],
163 }
164
165 component {
166 name: "bar",
167 }
168
169 package_module {
170 name: "package",
171 deps: ["foo"],
172 }
173 `, []string{"lib64/foo", "lib64/bar"})
174
Jiyong Park2136d152021-02-01 23:24:56 +0900175 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900176 `
177 component {
178 name: "foo",
179 deps: ["bar"],
180 }
181
182 component {
183 name: "bar",
184 }
185
186 package_module {
187 name: "package",
188 deps: ["foo"],
189 compile_multilib: "both",
190 }
191 `, []string{"lib32/foo", "lib32/bar", "lib64/foo", "lib64/bar"})
192
Jiyong Park2136d152021-02-01 23:24:56 +0900193 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900194 `
195 component {
196 name: "foo",
197 }
198
199 component {
200 name: "bar",
201 compile_multilib: "32",
202 }
203
204 package_module {
205 name: "package",
206 deps: ["foo"],
207 multilib: {
208 lib32: {
209 deps: ["bar"],
210 },
211 },
212 compile_multilib: "both",
213 }
214 `, []string{"lib32/foo", "lib32/bar", "lib64/foo"})
215
Jiyong Park2136d152021-02-01 23:24:56 +0900216 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900217 `
218 component {
219 name: "foo",
220 }
221
222 component {
223 name: "bar",
224 }
225
226 package_module {
227 name: "package",
228 deps: ["foo"],
229 multilib: {
230 first: {
231 deps: ["bar"],
232 },
233 },
234 compile_multilib: "both",
235 }
236 `, []string{"lib32/foo", "lib64/foo", "lib64/bar"})
Jiyong Park2136d152021-02-01 23:24:56 +0900237
238 runPackagingTest(t, multiTarget,
239 `
240 component {
241 name: "foo",
242 }
243
244 component {
245 name: "bar",
246 }
247
248 component {
249 name: "baz",
250 }
251
252 package_module {
253 name: "package",
254 deps: ["foo"],
255 arch: {
256 arm64: {
257 deps: ["bar"],
258 },
259 x86_64: {
260 deps: ["baz"],
261 },
262 },
263 compile_multilib: "both",
264 }
265 `, []string{"lib32/foo", "lib64/foo", "lib64/bar"})
266}
267
268func TestPackagingBaseSingleTarget(t *testing.T) {
269 multiTarget := false
270 runPackagingTest(t, multiTarget,
271 `
272 component {
273 name: "foo",
274 }
275
276 package_module {
277 name: "package",
278 deps: ["foo"],
279 }
280 `, []string{"lib64/foo"})
281
282 runPackagingTest(t, multiTarget,
283 `
284 component {
285 name: "foo",
286 deps: ["bar"],
287 }
288
289 component {
290 name: "bar",
291 }
292
293 package_module {
294 name: "package",
295 deps: ["foo"],
296 }
297 `, []string{"lib64/foo", "lib64/bar"})
298
299 runPackagingTest(t, multiTarget,
300 `
301 component {
302 name: "foo",
303 }
304
305 component {
306 name: "bar",
307 compile_multilib: "32",
308 }
309
310 package_module {
311 name: "package",
312 deps: ["foo"],
313 multilib: {
314 lib32: {
315 deps: ["bar"],
316 },
317 },
318 }
319 `, []string{"lib64/foo"})
320
321 runPackagingTest(t, multiTarget,
322 `
323 component {
324 name: "foo",
325 }
326
327 component {
328 name: "bar",
329 }
330
331 package_module {
332 name: "package",
333 deps: ["foo"],
334 multilib: {
335 lib64: {
336 deps: ["bar"],
337 },
338 },
339 }
340 `, []string{"lib64/foo", "lib64/bar"})
341
342 runPackagingTest(t, multiTarget,
343 `
344 component {
345 name: "foo",
346 }
347
348 component {
349 name: "bar",
350 }
351
352 component {
353 name: "baz",
354 }
355
356 package_module {
357 name: "package",
358 deps: ["foo"],
359 arch: {
360 arm64: {
361 deps: ["bar"],
362 },
363 x86_64: {
364 deps: ["baz"],
365 },
366 },
367 }
368 `, []string{"lib64/foo", "lib64/bar"})
Jooyung Han092ef812021-03-10 15:40:34 +0900369
370 runPackagingTest(t, multiTarget,
371 `
372 component {
373 name: "foo",
374 }
375
376 component {
377 name: "bar",
378 }
379
380 package_module {
381 name: "package",
382 deps: ["foo"],
383 install_deps: ["bar"],
384 }
385 `, []string{"lib64/foo"})
Jiyong Parkdda8f692020-11-09 18:38:48 +0900386}
Jooyung Han8707cd72021-07-23 02:49:46 +0900387
388func TestPackagingWithSkipInstallDeps(t *testing.T) {
389 // package -[dep]-> foo -[dep]-> bar -[dep]-> baz
Colin Crossbd3a16b2023-04-25 11:30:51 -0700390 // Packaging should continue transitively through modules that are not installed.
Jooyung Han8707cd72021-07-23 02:49:46 +0900391 multiTarget := false
392 runPackagingTest(t, multiTarget,
393 `
394 component {
395 name: "foo",
396 deps: ["bar"],
397 }
398
399 component {
400 name: "bar",
401 deps: ["baz"],
402 skip_install: true,
403 }
404
405 component {
406 name: "baz",
407 }
408
409 package_module {
410 name: "package",
411 deps: ["foo"],
412 }
Colin Crossbd3a16b2023-04-25 11:30:51 -0700413 `, []string{"lib64/foo", "lib64/bar", "lib64/baz"})
Jooyung Han8707cd72021-07-23 02:49:46 +0900414}
Jiyong Park1fb7c352024-05-10 11:17:33 +0900415
416func TestPackagingWithSkipToTransitvDeps(t *testing.T) {
417 // packag -[deps]-> foo -[build_only_deps]-> bar -[deps]-> baz
418 // bar isn't installed, but it brings baz to its parent.
419 multiTarget := false
420 runPackagingTest(t, multiTarget,
421 `
422 component {
423 name: "foo",
424 build_only_deps: ["bar"],
425 }
426
427 component {
428 name: "bar",
429 deps: ["baz"],
430 }
431
432 component {
433 name: "baz",
434 }
435
436 package_module {
437 name: "package",
438 deps: ["foo"],
439 }
440 `, []string{"lib64/foo", "lib64/baz"})
441}