blob: f32d1c396906f32c3428ce8ec72d0cc115427924 [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 (
Cole Faust502da392023-02-28 17:54:17 -080018 "strings"
Jiyong Parkdda8f692020-11-09 18:38:48 +090019 "testing"
Jiyong Park65b62242020-11-25 12:44:59 +090020
21 "github.com/google/blueprint"
Jooyung Han8707cd72021-07-23 02:49:46 +090022 "github.com/google/blueprint/proptools"
Jiyong Parkdda8f692020-11-09 18:38:48 +090023)
24
25// Module to be packaged
26type componentTestModule struct {
27 ModuleBase
28 props struct {
Jooyung Han8707cd72021-07-23 02:49:46 +090029 Deps []string
30 Skip_install *bool
Jiyong Parkdda8f692020-11-09 18:38:48 +090031 }
Cole Faust502da392023-02-28 17:54:17 -080032
33 builtFile Path
Jiyong Parkdda8f692020-11-09 18:38:48 +090034}
35
Jiyong Parkd630bdd2020-11-25 11:47:24 +090036// dep tag used in this test. All dependencies are considered as installable.
37type installDepTag struct {
38 blueprint.BaseDependencyTag
39 InstallAlwaysNeededDependencyTag
40}
41
Jiyong Parkdda8f692020-11-09 18:38:48 +090042func componentTestModuleFactory() Module {
43 m := &componentTestModule{}
44 m.AddProperties(&m.props)
45 InitAndroidArchModule(m, HostAndDeviceSupported, MultilibBoth)
46 return m
47}
48
49func (m *componentTestModule) DepsMutator(ctx BottomUpMutatorContext) {
Jiyong Parkd630bdd2020-11-25 11:47:24 +090050 ctx.AddDependency(ctx.Module(), installDepTag{}, m.props.Deps...)
Jiyong Parkdda8f692020-11-09 18:38:48 +090051}
52
53func (m *componentTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Cole Faust502da392023-02-28 17:54:17 -080054 m.builtFile = PathForModuleOut(ctx, m.Name())
Jiyong Parkdda8f692020-11-09 18:38:48 +090055 dir := ctx.Target().Arch.ArchType.Multilib
56 installDir := PathForModuleInstall(ctx, dir)
Jooyung Han8707cd72021-07-23 02:49:46 +090057 if proptools.Bool(m.props.Skip_install) {
58 m.SkipInstall()
59 }
Cole Faust502da392023-02-28 17:54:17 -080060 ctx.InstallFile(installDir, m.Name(), m.builtFile)
61}
62
63func (m *componentTestModule) AndroidMkEntries() []AndroidMkEntries {
64 return []AndroidMkEntries{
65 {
66 OutputFile: OptionalPathForPath(m.builtFile),
67 },
68 }
Jiyong Parkdda8f692020-11-09 18:38:48 +090069}
70
71// Module that itself is a package
72type packageTestModule struct {
73 ModuleBase
74 PackagingBase
Jooyung Han092ef812021-03-10 15:40:34 +090075 properties struct {
76 Install_deps []string `android:`
77 }
Jiyong Parkdda8f692020-11-09 18:38:48 +090078 entries []string
79}
80
Jiyong Park2136d152021-02-01 23:24:56 +090081func packageMultiTargetTestModuleFactory() Module {
Jiyong Parkdda8f692020-11-09 18:38:48 +090082 module := &packageTestModule{}
83 InitPackageModule(module)
84 InitAndroidMultiTargetsArchModule(module, DeviceSupported, MultilibCommon)
Jooyung Han092ef812021-03-10 15:40:34 +090085 module.AddProperties(&module.properties)
Jiyong Parkdda8f692020-11-09 18:38:48 +090086 return module
87}
88
Jiyong Park2136d152021-02-01 23:24:56 +090089func packageTestModuleFactory() Module {
90 module := &packageTestModule{}
91 InitPackageModule(module)
92 InitAndroidArchModule(module, DeviceSupported, MultilibBoth)
Jooyung Han092ef812021-03-10 15:40:34 +090093 module.AddProperties(&module.properties)
Jiyong Park2136d152021-02-01 23:24:56 +090094 return module
95}
96
Jooyung Han092ef812021-03-10 15:40:34 +090097type packagingDepTag struct {
98 blueprint.BaseDependencyTag
99 PackagingItemAlwaysDepTag
100}
101
Jiyong Parkdda8f692020-11-09 18:38:48 +0900102func (m *packageTestModule) DepsMutator(ctx BottomUpMutatorContext) {
Jooyung Han092ef812021-03-10 15:40:34 +0900103 m.AddDeps(ctx, packagingDepTag{})
104 ctx.AddDependency(ctx.Module(), installDepTag{}, m.properties.Install_deps...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900105}
106
107func (m *packageTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Paul Duffin4076a752021-02-02 10:59:54 +0000108 zipFile := PathForModuleOut(ctx, "myzip.zip")
Jooyung Hana8834282022-03-25 11:40:12 +0900109 m.entries = m.CopyDepsToZip(ctx, m.GatherPackagingSpecs(ctx), zipFile)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900110}
111
Jiyong Park2136d152021-02-01 23:24:56 +0900112func runPackagingTest(t *testing.T, multitarget bool, bp string, expected []string) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900113 t.Helper()
114
Jiyong Park2136d152021-02-01 23:24:56 +0900115 var archVariant string
Paul Duffinc3bdd312021-03-17 00:06:49 +0000116 var moduleFactory ModuleFactory
Jiyong Park2136d152021-02-01 23:24:56 +0900117 if multitarget {
118 archVariant = "android_common"
Paul Duffinc3bdd312021-03-17 00:06:49 +0000119 moduleFactory = packageMultiTargetTestModuleFactory
Jiyong Park2136d152021-02-01 23:24:56 +0900120 } else {
121 archVariant = "android_arm64_armv8-a"
Paul Duffinc3bdd312021-03-17 00:06:49 +0000122 moduleFactory = packageTestModuleFactory
Jiyong Park2136d152021-02-01 23:24:56 +0900123 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900124
Paul Duffin30ac3e72021-03-20 00:36:14 +0000125 result := GroupFixturePreparers(
Paul Duffinc3bdd312021-03-17 00:06:49 +0000126 PrepareForTestWithArchMutator,
127 FixtureRegisterWithContext(func(ctx RegistrationContext) {
128 ctx.RegisterModuleType("component", componentTestModuleFactory)
129 ctx.RegisterModuleType("package_module", moduleFactory)
130 }),
131 FixtureWithRootAndroidBp(bp),
Paul Duffin30ac3e72021-03-20 00:36:14 +0000132 ).RunTest(t)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900133
Paul Duffinc3bdd312021-03-17 00:06:49 +0000134 p := result.Module("package", archVariant).(*packageTestModule)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900135 actual := p.entries
136 actual = SortedUniqueStrings(actual)
137 expected = SortedUniqueStrings(expected)
Paul Duffinc3bdd312021-03-17 00:06:49 +0000138 AssertDeepEquals(t, "package entries", expected, actual)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900139}
140
Jiyong Park2136d152021-02-01 23:24:56 +0900141func TestPackagingBaseMultiTarget(t *testing.T) {
142 multiTarget := true
143 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900144 `
145 component {
146 name: "foo",
147 }
148
149 package_module {
150 name: "package",
151 deps: ["foo"],
152 }
153 `, []string{"lib64/foo"})
154
Jiyong Park2136d152021-02-01 23:24:56 +0900155 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900156 `
157 component {
158 name: "foo",
159 deps: ["bar"],
160 }
161
162 component {
163 name: "bar",
164 }
165
166 package_module {
167 name: "package",
168 deps: ["foo"],
169 }
170 `, []string{"lib64/foo", "lib64/bar"})
171
Jiyong Park2136d152021-02-01 23:24:56 +0900172 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900173 `
174 component {
175 name: "foo",
176 deps: ["bar"],
177 }
178
179 component {
180 name: "bar",
181 }
182
183 package_module {
184 name: "package",
185 deps: ["foo"],
186 compile_multilib: "both",
187 }
188 `, []string{"lib32/foo", "lib32/bar", "lib64/foo", "lib64/bar"})
189
Jiyong Park2136d152021-02-01 23:24:56 +0900190 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900191 `
192 component {
193 name: "foo",
194 }
195
196 component {
197 name: "bar",
198 compile_multilib: "32",
199 }
200
201 package_module {
202 name: "package",
203 deps: ["foo"],
204 multilib: {
205 lib32: {
206 deps: ["bar"],
207 },
208 },
209 compile_multilib: "both",
210 }
211 `, []string{"lib32/foo", "lib32/bar", "lib64/foo"})
212
Jiyong Park2136d152021-02-01 23:24:56 +0900213 runPackagingTest(t, multiTarget,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900214 `
215 component {
216 name: "foo",
217 }
218
219 component {
220 name: "bar",
221 }
222
223 package_module {
224 name: "package",
225 deps: ["foo"],
226 multilib: {
227 first: {
228 deps: ["bar"],
229 },
230 },
231 compile_multilib: "both",
232 }
233 `, []string{"lib32/foo", "lib64/foo", "lib64/bar"})
Jiyong Park2136d152021-02-01 23:24:56 +0900234
235 runPackagingTest(t, multiTarget,
236 `
237 component {
238 name: "foo",
239 }
240
241 component {
242 name: "bar",
243 }
244
245 component {
246 name: "baz",
247 }
248
249 package_module {
250 name: "package",
251 deps: ["foo"],
252 arch: {
253 arm64: {
254 deps: ["bar"],
255 },
256 x86_64: {
257 deps: ["baz"],
258 },
259 },
260 compile_multilib: "both",
261 }
262 `, []string{"lib32/foo", "lib64/foo", "lib64/bar"})
263}
264
Cole Faust502da392023-02-28 17:54:17 -0800265func TestSkipInstallProducesLocalUninstallableModule(t *testing.T) {
266 result := GroupFixturePreparers(
267 PrepareForTestWithArchMutator,
268 FixtureRegisterWithContext(func(ctx RegistrationContext) {
269 ctx.RegisterModuleType("component", componentTestModuleFactory)
270 ctx.RegisterModuleType("package_module", packageTestModuleFactory)
271 }),
272 FixtureWithRootAndroidBp(`
273component {
274 name: "foo",
275 skip_install: true,
276}
277
278package_module {
279 name: "package",
280 deps: ["foo"],
281}
282`),
283 ).RunTest(t)
284 module := result.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*componentTestModule)
285 entries := AndroidMkEntriesForTest(t, result.TestContext, module)
286 builder := &strings.Builder{}
287 entries[0].write(builder)
288 androidMkString := builder.String()
289 if !strings.Contains(androidMkString, "LOCAL_UNINSTALLABLE_MODULE := true") {
290 t.Errorf("Expected android mk entries to contain \"LOCAL_UNINSTALLABLE_MODULE := true\", got: \n%s", androidMkString)
291 }
292}
293
Jiyong Park2136d152021-02-01 23:24:56 +0900294func TestPackagingBaseSingleTarget(t *testing.T) {
295 multiTarget := false
296 runPackagingTest(t, multiTarget,
297 `
298 component {
299 name: "foo",
300 }
301
302 package_module {
303 name: "package",
304 deps: ["foo"],
305 }
306 `, []string{"lib64/foo"})
307
308 runPackagingTest(t, multiTarget,
309 `
310 component {
311 name: "foo",
312 deps: ["bar"],
313 }
314
315 component {
316 name: "bar",
317 }
318
319 package_module {
320 name: "package",
321 deps: ["foo"],
322 }
323 `, []string{"lib64/foo", "lib64/bar"})
324
325 runPackagingTest(t, multiTarget,
326 `
327 component {
328 name: "foo",
329 }
330
331 component {
332 name: "bar",
333 compile_multilib: "32",
334 }
335
336 package_module {
337 name: "package",
338 deps: ["foo"],
339 multilib: {
340 lib32: {
341 deps: ["bar"],
342 },
343 },
344 }
345 `, []string{"lib64/foo"})
346
347 runPackagingTest(t, multiTarget,
348 `
349 component {
350 name: "foo",
351 }
352
353 component {
354 name: "bar",
355 }
356
357 package_module {
358 name: "package",
359 deps: ["foo"],
360 multilib: {
361 lib64: {
362 deps: ["bar"],
363 },
364 },
365 }
366 `, []string{"lib64/foo", "lib64/bar"})
367
368 runPackagingTest(t, multiTarget,
369 `
370 component {
371 name: "foo",
372 }
373
374 component {
375 name: "bar",
376 }
377
378 component {
379 name: "baz",
380 }
381
382 package_module {
383 name: "package",
384 deps: ["foo"],
385 arch: {
386 arm64: {
387 deps: ["bar"],
388 },
389 x86_64: {
390 deps: ["baz"],
391 },
392 },
393 }
394 `, []string{"lib64/foo", "lib64/bar"})
Jooyung Han092ef812021-03-10 15:40:34 +0900395
396 runPackagingTest(t, multiTarget,
397 `
398 component {
399 name: "foo",
400 }
401
402 component {
403 name: "bar",
404 }
405
406 package_module {
407 name: "package",
408 deps: ["foo"],
409 install_deps: ["bar"],
410 }
411 `, []string{"lib64/foo"})
Jiyong Parkdda8f692020-11-09 18:38:48 +0900412}
Jooyung Han8707cd72021-07-23 02:49:46 +0900413
414func TestPackagingWithSkipInstallDeps(t *testing.T) {
415 // package -[dep]-> foo -[dep]-> bar -[dep]-> baz
416 // OK SKIPPED
417 multiTarget := false
418 runPackagingTest(t, multiTarget,
419 `
420 component {
421 name: "foo",
422 deps: ["bar"],
423 }
424
425 component {
426 name: "bar",
427 deps: ["baz"],
428 skip_install: true,
429 }
430
431 component {
432 name: "baz",
433 }
434
435 package_module {
436 name: "package",
437 deps: ["foo"],
438 }
439 `, []string{"lib64/foo"})
440}