blob: 86e6af96a554e9e8d57406c1ee426b16b79c1c2e [file] [log] [blame]
Colin Cross33b2fb72019-05-14 14:07:01 -07001// Copyright 2019 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 cc
16
17import (
Spandan Das2b6dfb52024-01-19 00:22:22 +000018 "fmt"
Colin Cross0c66bc62021-07-20 09:47:41 -070019 "runtime"
Colin Cross33b2fb72019-05-14 14:07:01 -070020 "testing"
21
22 "android/soong/android"
Colin Cross33b2fb72019-05-14 14:07:01 -070023 "github.com/google/blueprint"
24)
25
Paul Duffin8567f222021-03-23 00:02:06 +000026var prepareForPrebuiltTest = android.GroupFixturePreparers(
27 prepareForCcTest,
Paul Duffin6a1160e2021-03-07 15:47:42 +000028 android.PrepareForTestWithAndroidMk,
29)
Martin Stjernholmadeb0882020-04-01 23:02:57 +010030
Paul Duffin6a1160e2021-03-07 15:47:42 +000031func testPrebuilt(t *testing.T, bp string, fs android.MockFS, handlers ...android.FixturePreparer) *android.TestContext {
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +000032 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000033 result := android.GroupFixturePreparers(
34 prepareForPrebuiltTest,
Paul Duffin6a1160e2021-03-07 15:47:42 +000035 fs.AddToFixture(),
Paul Duffin8567f222021-03-23 00:02:06 +000036 android.GroupFixturePreparers(handlers...),
37 ).RunTestWithBp(t, bp)
Martin Stjernholmadeb0882020-04-01 23:02:57 +010038
Paul Duffin6a1160e2021-03-07 15:47:42 +000039 return result.TestContext
Martin Stjernholmadeb0882020-04-01 23:02:57 +010040}
41
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070042type configCustomizer func(config android.Config)
43
Colin Cross33b2fb72019-05-14 14:07:01 -070044func TestPrebuilt(t *testing.T) {
45 bp := `
46 cc_library {
47 name: "liba",
48 }
49
50 cc_prebuilt_library_shared {
51 name: "liba",
52 srcs: ["liba.so"],
53 }
54
55 cc_library {
56 name: "libb",
57 }
58
59 cc_prebuilt_library_static {
60 name: "libb",
61 srcs: ["libb.a"],
62 }
63
64 cc_library_shared {
65 name: "libd",
66 }
67
68 cc_prebuilt_library_shared {
69 name: "libd",
70 srcs: ["libd.so"],
71 }
72
73 cc_library_static {
74 name: "libe",
75 }
76
77 cc_prebuilt_library_static {
78 name: "libe",
79 srcs: ["libe.a"],
80 }
Paul Duffinbce90da2020-03-12 20:17:14 +000081
82 cc_library {
83 name: "libf",
84 }
85
86 cc_prebuilt_library {
87 name: "libf",
88 static: {
89 srcs: ["libf.a"],
90 },
91 shared: {
92 srcs: ["libf.so"],
93 },
94 }
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000095
96 cc_object {
97 name: "crtx",
98 }
99
100 cc_prebuilt_object {
101 name: "crtx",
102 srcs: ["crtx.o"],
103 }
Colin Cross33b2fb72019-05-14 14:07:01 -0700104 `
105
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100106 ctx := testPrebuilt(t, bp, map[string][]byte{
107 "liba.so": nil,
108 "libb.a": nil,
109 "libd.so": nil,
110 "libe.a": nil,
111 "libf.a": nil,
112 "libf.so": nil,
113 "crtx.o": nil,
114 })
Colin Cross33b2fb72019-05-14 14:07:01 -0700115
116 // Verify that all the modules exist and that their dependencies were connected correctly
Colin Cross7113d202019-11-20 16:39:12 -0800117 liba := ctx.ModuleForTests("liba", "android_arm64_armv8-a_shared").Module()
118 libb := ctx.ModuleForTests("libb", "android_arm64_armv8-a_static").Module()
119 libd := ctx.ModuleForTests("libd", "android_arm64_armv8-a_shared").Module()
120 libe := ctx.ModuleForTests("libe", "android_arm64_armv8-a_static").Module()
Paul Duffinbce90da2020-03-12 20:17:14 +0000121 libfStatic := ctx.ModuleForTests("libf", "android_arm64_armv8-a_static").Module()
122 libfShared := ctx.ModuleForTests("libf", "android_arm64_armv8-a_shared").Module()
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000123 crtx := ctx.ModuleForTests("crtx", "android_arm64_armv8-a").Module()
Colin Cross33b2fb72019-05-14 14:07:01 -0700124
Colin Cross7113d202019-11-20 16:39:12 -0800125 prebuiltLiba := ctx.ModuleForTests("prebuilt_liba", "android_arm64_armv8-a_shared").Module()
126 prebuiltLibb := ctx.ModuleForTests("prebuilt_libb", "android_arm64_armv8-a_static").Module()
127 prebuiltLibd := ctx.ModuleForTests("prebuilt_libd", "android_arm64_armv8-a_shared").Module()
128 prebuiltLibe := ctx.ModuleForTests("prebuilt_libe", "android_arm64_armv8-a_static").Module()
Paul Duffinbce90da2020-03-12 20:17:14 +0000129 prebuiltLibfStatic := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_static").Module()
130 prebuiltLibfShared := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_shared").Module()
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000131 prebuiltCrtx := ctx.ModuleForTests("prebuilt_crtx", "android_arm64_armv8-a").Module()
Colin Cross33b2fb72019-05-14 14:07:01 -0700132
133 hasDep := func(m android.Module, wantDep android.Module) bool {
134 t.Helper()
135 var found bool
136 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
137 if dep == wantDep {
138 found = true
139 }
140 })
141 return found
142 }
143
144 if !hasDep(liba, prebuiltLiba) {
145 t.Errorf("liba missing dependency on prebuilt_liba")
146 }
147
148 if !hasDep(libb, prebuiltLibb) {
149 t.Errorf("libb missing dependency on prebuilt_libb")
150 }
151
152 if !hasDep(libd, prebuiltLibd) {
153 t.Errorf("libd missing dependency on prebuilt_libd")
154 }
155
156 if !hasDep(libe, prebuiltLibe) {
157 t.Errorf("libe missing dependency on prebuilt_libe")
158 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000159
160 if !hasDep(libfStatic, prebuiltLibfStatic) {
161 t.Errorf("libf static missing dependency on prebuilt_libf")
162 }
163
164 if !hasDep(libfShared, prebuiltLibfShared) {
165 t.Errorf("libf shared missing dependency on prebuilt_libf")
166 }
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000167
168 if !hasDep(crtx, prebuiltCrtx) {
169 t.Errorf("crtx missing dependency on prebuilt_crtx")
170 }
Wei Li598f92d2023-01-04 17:12:24 -0800171
172 entries := android.AndroidMkEntriesForTest(t, ctx, prebuiltLiba)[0]
173 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "cc_prebuilt_library_shared", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
174 entries = android.AndroidMkEntriesForTest(t, ctx, prebuiltLibb)[0]
175 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "cc_prebuilt_library_static", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Paul Duffinbce90da2020-03-12 20:17:14 +0000176}
177
Paul Duffinbce90da2020-03-12 20:17:14 +0000178func TestPrebuiltLibraryShared(t *testing.T) {
179 ctx := testPrebuilt(t, `
180 cc_prebuilt_library_shared {
181 name: "libtest",
182 srcs: ["libf.so"],
183 strip: {
184 none: true,
185 },
186 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100187 `, map[string][]byte{
188 "libf.so": nil,
189 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000190
191 shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800192 assertString(t, shared.OutputFile().Path().Base(), "libtest.so")
Paul Duffinbce90da2020-03-12 20:17:14 +0000193}
194
195func TestPrebuiltLibraryStatic(t *testing.T) {
196 ctx := testPrebuilt(t, `
197 cc_prebuilt_library_static {
198 name: "libtest",
199 srcs: ["libf.a"],
200 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100201 `, map[string][]byte{
202 "libf.a": nil,
203 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000204
205 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800206 assertString(t, static.OutputFile().Path().Base(), "libf.a")
Paul Duffinbce90da2020-03-12 20:17:14 +0000207}
208
209func TestPrebuiltLibrary(t *testing.T) {
210 ctx := testPrebuilt(t, `
211 cc_prebuilt_library {
212 name: "libtest",
213 static: {
214 srcs: ["libf.a"],
215 },
216 shared: {
217 srcs: ["libf.so"],
218 },
219 strip: {
220 none: true,
221 },
222 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100223 `, map[string][]byte{
224 "libf.a": nil,
225 "libf.so": nil,
226 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000227
228 shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800229 assertString(t, shared.OutputFile().Path().Base(), "libtest.so")
Paul Duffinbce90da2020-03-12 20:17:14 +0000230
231 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800232 assertString(t, static.OutputFile().Path().Base(), "libf.a")
233}
234
235func TestPrebuiltLibraryStem(t *testing.T) {
236 ctx := testPrebuilt(t, `
237 cc_prebuilt_library {
238 name: "libfoo",
239 stem: "libbar",
240 static: {
241 srcs: ["libfoo.a"],
242 },
243 shared: {
244 srcs: ["libfoo.so"],
245 },
246 strip: {
247 none: true,
248 },
249 }
250 `, map[string][]byte{
251 "libfoo.a": nil,
252 "libfoo.so": nil,
253 })
254
255 static := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
256 assertString(t, static.OutputFile().Path().Base(), "libfoo.a")
257
258 shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module)
259 assertString(t, shared.OutputFile().Path().Base(), "libbar.so")
260}
261
262func TestPrebuiltLibrarySharedStem(t *testing.T) {
263 ctx := testPrebuilt(t, `
264 cc_prebuilt_library_shared {
265 name: "libfoo",
266 stem: "libbar",
267 srcs: ["libfoo.so"],
268 strip: {
269 none: true,
270 },
271 }
272 `, map[string][]byte{
273 "libfoo.so": nil,
274 })
275
276 shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module)
277 assertString(t, shared.OutputFile().Path().Base(), "libbar.so")
Colin Cross33b2fb72019-05-14 14:07:01 -0700278}
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100279
280func TestPrebuiltSymlinkedHostBinary(t *testing.T) {
Colin Cross0c66bc62021-07-20 09:47:41 -0700281 if runtime.GOOS != "linux" {
282 t.Skipf("Skipping host prebuilt testing that is only supported on linux not %s", runtime.GOOS)
Martin Stjernholm6a9a1462020-09-15 02:56:19 +0100283 }
284
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100285 ctx := testPrebuilt(t, `
286 cc_prebuilt_library_shared {
287 name: "libfoo",
288 device_supported: false,
289 host_supported: true,
290 target: {
291 linux_glibc_x86_64: {
292 srcs: ["linux_glibc_x86_64/lib64/libfoo.so"],
293 },
294 },
295 }
296
297 cc_prebuilt_binary {
298 name: "foo",
299 device_supported: false,
300 host_supported: true,
301 shared_libs: ["libfoo"],
302 target: {
303 linux_glibc_x86_64: {
304 srcs: ["linux_glibc_x86_64/bin/foo"],
305 },
306 },
307 }
308 `, map[string][]byte{
309 "libfoo.so": nil,
310 "foo": nil,
311 })
312
313 fooRule := ctx.ModuleForTests("foo", "linux_glibc_x86_64").Rule("Symlink")
Paul Duffine8366da2021-03-24 10:40:38 +0000314 assertString(t, fooRule.Output.String(), "out/soong/.intermediates/foo/linux_glibc_x86_64/foo")
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100315 assertString(t, fooRule.Args["fromPath"], "$$PWD/linux_glibc_x86_64/bin/foo")
316
317 var libfooDep android.Path
318 for _, dep := range fooRule.Implicits {
319 if dep.Base() == "libfoo.so" {
320 libfooDep = dep
321 break
322 }
323 }
Paul Duffine8366da2021-03-24 10:40:38 +0000324 assertString(t, libfooDep.String(), "out/soong/.intermediates/libfoo/linux_glibc_x86_64_shared/libfoo.so")
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100325}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700326
327func TestPrebuiltLibrarySanitized(t *testing.T) {
328 bp := `cc_prebuilt_library {
329 name: "libtest",
330 static: {
331 sanitized: { none: { srcs: ["libf.a"], }, hwaddress: { srcs: ["libf.hwasan.a"], }, },
332 },
333 shared: {
334 sanitized: { none: { srcs: ["libf.so"], }, hwaddress: { srcs: ["hwasan/libf.so"], }, },
335 },
336 }
337 cc_prebuilt_library_static {
338 name: "libtest_static",
339 sanitized: { none: { srcs: ["libf.a"], }, hwaddress: { srcs: ["libf.hwasan.a"], }, },
340 }
341 cc_prebuilt_library_shared {
342 name: "libtest_shared",
343 sanitized: { none: { srcs: ["libf.so"], }, hwaddress: { srcs: ["hwasan/libf.so"], }, },
344 }`
345
346 fs := map[string][]byte{
347 "libf.a": nil,
348 "libf.hwasan.a": nil,
349 "libf.so": nil,
350 "hwasan/libf.so": nil,
351 }
352
353 // Without SANITIZE_TARGET.
354 ctx := testPrebuilt(t, bp, fs)
355
356 shared_rule := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Rule("android/soong/cc.strip")
357 assertString(t, shared_rule.Input.String(), "libf.so")
358
359 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
360 assertString(t, static.OutputFile().Path().Base(), "libf.a")
361
362 shared_rule2 := ctx.ModuleForTests("libtest_shared", "android_arm64_armv8-a_shared").Rule("android/soong/cc.strip")
363 assertString(t, shared_rule2.Input.String(), "libf.so")
364
365 static2 := ctx.ModuleForTests("libtest_static", "android_arm64_armv8-a_static").Module().(*Module)
366 assertString(t, static2.OutputFile().Path().Base(), "libf.a")
367
368 // With SANITIZE_TARGET=hwaddress
Paul Duffin6a1160e2021-03-07 15:47:42 +0000369 ctx = testPrebuilt(t, bp, fs,
370 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
371 variables.SanitizeDevice = []string{"hwaddress"}
372 }),
373 )
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700374
375 shared_rule = ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared_hwasan").Rule("android/soong/cc.strip")
376 assertString(t, shared_rule.Input.String(), "hwasan/libf.so")
377
378 static = ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static_hwasan").Module().(*Module)
379 assertString(t, static.OutputFile().Path().Base(), "libf.hwasan.a")
380
381 shared_rule2 = ctx.ModuleForTests("libtest_shared", "android_arm64_armv8-a_shared_hwasan").Rule("android/soong/cc.strip")
382 assertString(t, shared_rule2.Input.String(), "hwasan/libf.so")
383
384 static2 = ctx.ModuleForTests("libtest_static", "android_arm64_armv8-a_static_hwasan").Module().(*Module)
385 assertString(t, static2.OutputFile().Path().Base(), "libf.hwasan.a")
386}
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400387
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb1bd7702022-10-04 17:45:24 +0000388func TestPrebuiltBinaryNoSrcsNoError(t *testing.T) {
389 const bp = `
390cc_prebuilt_binary {
391 name: "bintest",
392 srcs: [],
393}`
394 ctx := testPrebuilt(t, bp, map[string][]byte{})
395 mod := ctx.ModuleForTests("bintest", "android_arm64_armv8-a").Module().(*Module)
396 android.AssertBoolEquals(t, `expected no srcs to yield no output file`, false, mod.OutputFile().Valid())
397}
398
399func TestPrebuiltBinaryMultipleSrcs(t *testing.T) {
400 const bp = `
401cc_prebuilt_binary {
402 name: "bintest",
403 srcs: ["foo", "bar"],
404}`
405 testCcError(t, `Android.bp:4:6: module "bintest" variant "android_arm64_armv8-a": srcs: multiple prebuilt source files`, bp)
406}
Spandan Das2b6dfb52024-01-19 00:22:22 +0000407
408func TestMultiplePrebuilts(t *testing.T) {
409 bp := `
410 // an rdep
411 cc_library {
412 name: "libfoo",
413 shared_libs: ["libbar"],
414 }
415
416 // multiple variations of dep
417 // source
418 cc_library {
419 name: "libbar",
420 }
421 // prebuilt "v1"
422 cc_prebuilt_library_shared {
423 name: "libbar",
424 srcs: ["libbar.so"],
425 }
426 // prebuilt "v2"
427 cc_prebuilt_library_shared {
428 name: "libbar.v2",
429 stem: "libbar",
430 source_module_name: "libbar",
431 srcs: ["libbar.so"],
432 }
433
434 // selectors
435 apex_contributions {
436 name: "myapex_contributions",
437 contents: ["%v"],
438 }
439 all_apex_contributions {name: "all_apex_contributions"}
440 `
441 hasDep := func(ctx *android.TestContext, m android.Module, wantDep android.Module) bool {
442 t.Helper()
443 var found bool
444 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
445 if dep == wantDep {
446 found = true
447 }
448 })
449 return found
450 }
451
452 testCases := []struct {
453 desc string
454 selectedDependencyName string
455 expectedDependencyName string
456 }{
457 {
458 desc: "Source library is selected using apex_contributions",
459 selectedDependencyName: "libbar",
460 expectedDependencyName: "libbar",
461 },
462 {
463 desc: "Prebuilt library v1 is selected using apex_contributions",
464 selectedDependencyName: "prebuilt_libbar",
465 expectedDependencyName: "prebuilt_libbar",
466 },
467 {
468 desc: "Prebuilt library v2 is selected using apex_contributions",
469 selectedDependencyName: "prebuilt_libbar.v2",
470 expectedDependencyName: "prebuilt_libbar.v2",
471 },
472 }
473
474 for _, tc := range testCases {
475 preparer := android.GroupFixturePreparers(
476 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
477 android.RegisterApexContributionsBuildComponents(ctx)
478 }),
479 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
480 variables.BuildFlags = map[string]string{
481 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": "myapex_contributions",
482 }
483 }),
484 )
485 ctx := testPrebuilt(t, fmt.Sprintf(bp, tc.selectedDependencyName), map[string][]byte{
486 "libbar.so": nil,
487 "crtx.o": nil,
488 }, preparer)
489 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
490 expectedDependency := ctx.ModuleForTests(tc.expectedDependencyName, "android_arm64_armv8-a_shared").Module()
491 android.AssertBoolEquals(t, fmt.Sprintf("expected dependency from %s to %s\n", libfoo.Name(), tc.expectedDependencyName), true, hasDep(ctx, libfoo, expectedDependency))
Spandan Das3faa7922024-02-26 19:42:32 +0000492 // check that LOCAL_SHARED_LIBRARIES contains libbar and not libbar.v<N>
493 entries := android.AndroidMkEntriesForTest(t, ctx, libfoo)[0]
494 android.AssertStringListContains(t, "Version should not be present in LOCAL_SHARED_LIBRARIES", entries.EntryMap["LOCAL_SHARED_LIBRARIES"], "libbar")
Spandan Das2b6dfb52024-01-19 00:22:22 +0000495
496 // check installation rules
497 // the selected soong module should be exported to make
498 libbar := ctx.ModuleForTests(tc.expectedDependencyName, "android_arm64_armv8-a_shared").Module()
499 android.AssertBoolEquals(t, fmt.Sprintf("dependency %s should be exported to make\n", expectedDependency), true, !libbar.IsHideFromMake())
500
501 // check LOCAL_MODULE of the selected module name
502 // the prebuilt should have the same LOCAL_MODULE when exported to make
Spandan Das3faa7922024-02-26 19:42:32 +0000503 entries = android.AndroidMkEntriesForTest(t, ctx, libbar)[0]
Spandan Das2b6dfb52024-01-19 00:22:22 +0000504 android.AssertStringEquals(t, "unexpected LOCAL_MODULE", "libbar", entries.EntryMap["LOCAL_MODULE"][0])
505 }
506}
Spandan Dasf2c10572024-02-27 04:49:52 +0000507
508// Setting prefer on multiple prebuilts is an error, unless one of them is also listed in apex_contributions
509func TestMultiplePrebuiltsPreferredUsingLegacyFlags(t *testing.T) {
510 bp := `
511 // an rdep
512 cc_library {
513 name: "libfoo",
514 shared_libs: ["libbar"],
515 }
516
517 // multiple variations of dep
518 // source
519 cc_library {
520 name: "libbar",
521 }
522 // prebuilt "v1"
523 cc_prebuilt_library_shared {
524 name: "libbar",
525 srcs: ["libbar.so"],
526 prefer: true,
527 }
528 // prebuilt "v2"
529 cc_prebuilt_library_shared {
530 name: "libbar.v2",
531 stem: "libbar",
532 source_module_name: "libbar",
533 srcs: ["libbar.so"],
534 prefer: true,
535 }
536
537 // selectors
538 apex_contributions {
539 name: "myapex_contributions",
540 contents: [%v],
541 }
542 all_apex_contributions {name: "all_apex_contributions"}
543 `
544 hasDep := func(ctx *android.TestContext, m android.Module, wantDep android.Module) bool {
545 t.Helper()
546 var found bool
547 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
548 if dep == wantDep {
549 found = true
550 }
551 })
552 return found
553 }
554
555 testCases := []struct {
556 desc string
557 selectedDependencyName string
558 expectedDependencyName string
559 expectedErr string
560 }{
561 {
562 desc: "Multiple prebuilts have prefer: true",
563 expectedErr: "Multiple prebuilt modules prebuilt_libbar and prebuilt_libbar.v2 have been marked as preferred for this source module",
564 },
565 {
566 desc: "Multiple prebuilts have prefer: true. The prebuilt listed in apex_contributions wins.",
567 selectedDependencyName: `"prebuilt_libbar"`,
568 expectedDependencyName: "prebuilt_libbar",
569 },
570 }
571
572 for _, tc := range testCases {
573 preparer := android.GroupFixturePreparers(
574 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
575 android.RegisterApexContributionsBuildComponents(ctx)
576 }),
577 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
578 variables.BuildFlags = map[string]string{
579 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": "myapex_contributions",
580 }
581 }),
582 )
583 if tc.expectedErr != "" {
584 preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(tc.expectedErr))
585 }
586
587 ctx := testPrebuilt(t, fmt.Sprintf(bp, tc.selectedDependencyName), map[string][]byte{
588 "libbar.so": nil,
589 "crtx.o": nil,
590 }, preparer)
591 if tc.expectedErr != "" {
592 return // the fixture will assert that the excepted err has been raised
593 }
594 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
595 expectedDependency := ctx.ModuleForTests(tc.expectedDependencyName, "android_arm64_armv8-a_shared").Module()
596 android.AssertBoolEquals(t, fmt.Sprintf("expected dependency from %s to %s\n", libfoo.Name(), tc.expectedDependencyName), true, hasDep(ctx, libfoo, expectedDependency))
597 }
598}
Spandan Das972917d2024-02-27 09:31:51 +0000599
600// If module sdk cannot provide a cc module variant (e.g. static), then the module variant from source should be used
601func TestMissingVariantInModuleSdk(t *testing.T) {
602 bp := `
603 // an rdep
604 cc_library {
605 name: "libfoo",
606 static_libs: ["libbar"],
607 }
608
609 // source
610 cc_library {
611 name: "libbar",
612 }
613 // prebuilt
614 // libbar only exists as a shared library
615 cc_prebuilt_library_shared {
616 name: "libbar",
617 srcs: ["libbar.so"],
618 }
619 // selectors
620 apex_contributions {
621 name: "myapex_contributions",
622 contents: ["prebuilt_libbar"],
623 }
624 all_apex_contributions {name: "all_apex_contributions"}
625 `
626 hasDep := func(ctx *android.TestContext, m android.Module, wantDep android.Module) bool {
627 t.Helper()
628 var found bool
629 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
630 if dep == wantDep {
631 found = true
632 }
633 })
634 return found
635 }
636
637 preparer := android.GroupFixturePreparers(
638 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
639 android.RegisterApexContributionsBuildComponents(ctx)
640 }),
641 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
642 variables.BuildFlags = map[string]string{
643 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": "myapex_contributions",
644 }
645 }),
646 )
647 ctx := testPrebuilt(t, bp, map[string][]byte{
648 "libbar.so": nil,
649 "crtx.o": nil,
650 }, preparer)
651 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
652 sourceLibBar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Module()
653 // Even though the prebuilt is listed in apex_contributions, the prebuilt does not have a static variant.
654 // Therefore source of libbar should be used.
655 android.AssertBoolEquals(t, fmt.Sprintf("expected dependency from libfoo to source libbar"), true, hasDep(ctx, libfoo, sourceLibBar))
656}