blob: 405680c3988c6ae7a2ff302b82f55df05ea96329 [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 (
Colin Cross0c66bc62021-07-20 09:47:41 -070018 "runtime"
Colin Cross33b2fb72019-05-14 14:07:01 -070019 "testing"
20
21 "android/soong/android"
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -040022 "android/soong/bazel/cquery"
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +000023
Colin Cross33b2fb72019-05-14 14:07:01 -070024 "github.com/google/blueprint"
25)
26
Paul Duffin8567f222021-03-23 00:02:06 +000027var prepareForPrebuiltTest = android.GroupFixturePreparers(
28 prepareForCcTest,
Paul Duffin6a1160e2021-03-07 15:47:42 +000029 android.PrepareForTestWithAndroidMk,
30)
Martin Stjernholmadeb0882020-04-01 23:02:57 +010031
Paul Duffin6a1160e2021-03-07 15:47:42 +000032func testPrebuilt(t *testing.T, bp string, fs android.MockFS, handlers ...android.FixturePreparer) *android.TestContext {
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +000033 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000034 result := android.GroupFixturePreparers(
35 prepareForPrebuiltTest,
Paul Duffin6a1160e2021-03-07 15:47:42 +000036 fs.AddToFixture(),
Paul Duffin8567f222021-03-23 00:02:06 +000037 android.GroupFixturePreparers(handlers...),
38 ).RunTestWithBp(t, bp)
Martin Stjernholmadeb0882020-04-01 23:02:57 +010039
Paul Duffin6a1160e2021-03-07 15:47:42 +000040 return result.TestContext
Martin Stjernholmadeb0882020-04-01 23:02:57 +010041}
42
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070043type configCustomizer func(config android.Config)
44
Colin Cross33b2fb72019-05-14 14:07:01 -070045func TestPrebuilt(t *testing.T) {
46 bp := `
47 cc_library {
48 name: "liba",
49 }
50
51 cc_prebuilt_library_shared {
52 name: "liba",
53 srcs: ["liba.so"],
54 }
55
56 cc_library {
57 name: "libb",
58 }
59
60 cc_prebuilt_library_static {
61 name: "libb",
62 srcs: ["libb.a"],
63 }
64
65 cc_library_shared {
66 name: "libd",
67 }
68
69 cc_prebuilt_library_shared {
70 name: "libd",
71 srcs: ["libd.so"],
72 }
73
74 cc_library_static {
75 name: "libe",
76 }
77
78 cc_prebuilt_library_static {
79 name: "libe",
80 srcs: ["libe.a"],
81 }
Paul Duffinbce90da2020-03-12 20:17:14 +000082
83 cc_library {
84 name: "libf",
85 }
86
87 cc_prebuilt_library {
88 name: "libf",
89 static: {
90 srcs: ["libf.a"],
91 },
92 shared: {
93 srcs: ["libf.so"],
94 },
95 }
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000096
97 cc_object {
98 name: "crtx",
99 }
100
101 cc_prebuilt_object {
102 name: "crtx",
103 srcs: ["crtx.o"],
104 }
Colin Cross33b2fb72019-05-14 14:07:01 -0700105 `
106
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100107 ctx := testPrebuilt(t, bp, map[string][]byte{
108 "liba.so": nil,
109 "libb.a": nil,
110 "libd.so": nil,
111 "libe.a": nil,
112 "libf.a": nil,
113 "libf.so": nil,
114 "crtx.o": nil,
115 })
Colin Cross33b2fb72019-05-14 14:07:01 -0700116
117 // Verify that all the modules exist and that their dependencies were connected correctly
Colin Cross7113d202019-11-20 16:39:12 -0800118 liba := ctx.ModuleForTests("liba", "android_arm64_armv8-a_shared").Module()
119 libb := ctx.ModuleForTests("libb", "android_arm64_armv8-a_static").Module()
120 libd := ctx.ModuleForTests("libd", "android_arm64_armv8-a_shared").Module()
121 libe := ctx.ModuleForTests("libe", "android_arm64_armv8-a_static").Module()
Paul Duffinbce90da2020-03-12 20:17:14 +0000122 libfStatic := ctx.ModuleForTests("libf", "android_arm64_armv8-a_static").Module()
123 libfShared := ctx.ModuleForTests("libf", "android_arm64_armv8-a_shared").Module()
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000124 crtx := ctx.ModuleForTests("crtx", "android_arm64_armv8-a").Module()
Colin Cross33b2fb72019-05-14 14:07:01 -0700125
Colin Cross7113d202019-11-20 16:39:12 -0800126 prebuiltLiba := ctx.ModuleForTests("prebuilt_liba", "android_arm64_armv8-a_shared").Module()
127 prebuiltLibb := ctx.ModuleForTests("prebuilt_libb", "android_arm64_armv8-a_static").Module()
128 prebuiltLibd := ctx.ModuleForTests("prebuilt_libd", "android_arm64_armv8-a_shared").Module()
129 prebuiltLibe := ctx.ModuleForTests("prebuilt_libe", "android_arm64_armv8-a_static").Module()
Paul Duffinbce90da2020-03-12 20:17:14 +0000130 prebuiltLibfStatic := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_static").Module()
131 prebuiltLibfShared := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_shared").Module()
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000132 prebuiltCrtx := ctx.ModuleForTests("prebuilt_crtx", "android_arm64_armv8-a").Module()
Colin Cross33b2fb72019-05-14 14:07:01 -0700133
134 hasDep := func(m android.Module, wantDep android.Module) bool {
135 t.Helper()
136 var found bool
137 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
138 if dep == wantDep {
139 found = true
140 }
141 })
142 return found
143 }
144
145 if !hasDep(liba, prebuiltLiba) {
146 t.Errorf("liba missing dependency on prebuilt_liba")
147 }
148
149 if !hasDep(libb, prebuiltLibb) {
150 t.Errorf("libb missing dependency on prebuilt_libb")
151 }
152
153 if !hasDep(libd, prebuiltLibd) {
154 t.Errorf("libd missing dependency on prebuilt_libd")
155 }
156
157 if !hasDep(libe, prebuiltLibe) {
158 t.Errorf("libe missing dependency on prebuilt_libe")
159 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000160
161 if !hasDep(libfStatic, prebuiltLibfStatic) {
162 t.Errorf("libf static missing dependency on prebuilt_libf")
163 }
164
165 if !hasDep(libfShared, prebuiltLibfShared) {
166 t.Errorf("libf shared missing dependency on prebuilt_libf")
167 }
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000168
169 if !hasDep(crtx, prebuiltCrtx) {
170 t.Errorf("crtx missing dependency on prebuilt_crtx")
171 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000172}
173
Paul Duffinbce90da2020-03-12 20:17:14 +0000174func TestPrebuiltLibraryShared(t *testing.T) {
175 ctx := testPrebuilt(t, `
176 cc_prebuilt_library_shared {
177 name: "libtest",
178 srcs: ["libf.so"],
179 strip: {
180 none: true,
181 },
182 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100183 `, map[string][]byte{
184 "libf.so": nil,
185 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000186
187 shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800188 assertString(t, shared.OutputFile().Path().Base(), "libtest.so")
Paul Duffinbce90da2020-03-12 20:17:14 +0000189}
190
191func TestPrebuiltLibraryStatic(t *testing.T) {
192 ctx := testPrebuilt(t, `
193 cc_prebuilt_library_static {
194 name: "libtest",
195 srcs: ["libf.a"],
196 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100197 `, map[string][]byte{
198 "libf.a": nil,
199 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000200
201 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800202 assertString(t, static.OutputFile().Path().Base(), "libf.a")
Paul Duffinbce90da2020-03-12 20:17:14 +0000203}
204
205func TestPrebuiltLibrary(t *testing.T) {
206 ctx := testPrebuilt(t, `
207 cc_prebuilt_library {
208 name: "libtest",
209 static: {
210 srcs: ["libf.a"],
211 },
212 shared: {
213 srcs: ["libf.so"],
214 },
215 strip: {
216 none: true,
217 },
218 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100219 `, map[string][]byte{
220 "libf.a": nil,
221 "libf.so": nil,
222 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000223
224 shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800225 assertString(t, shared.OutputFile().Path().Base(), "libtest.so")
Paul Duffinbce90da2020-03-12 20:17:14 +0000226
227 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800228 assertString(t, static.OutputFile().Path().Base(), "libf.a")
229}
230
231func TestPrebuiltLibraryStem(t *testing.T) {
232 ctx := testPrebuilt(t, `
233 cc_prebuilt_library {
234 name: "libfoo",
235 stem: "libbar",
236 static: {
237 srcs: ["libfoo.a"],
238 },
239 shared: {
240 srcs: ["libfoo.so"],
241 },
242 strip: {
243 none: true,
244 },
245 }
246 `, map[string][]byte{
247 "libfoo.a": nil,
248 "libfoo.so": nil,
249 })
250
251 static := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
252 assertString(t, static.OutputFile().Path().Base(), "libfoo.a")
253
254 shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module)
255 assertString(t, shared.OutputFile().Path().Base(), "libbar.so")
256}
257
258func TestPrebuiltLibrarySharedStem(t *testing.T) {
259 ctx := testPrebuilt(t, `
260 cc_prebuilt_library_shared {
261 name: "libfoo",
262 stem: "libbar",
263 srcs: ["libfoo.so"],
264 strip: {
265 none: true,
266 },
267 }
268 `, map[string][]byte{
269 "libfoo.so": nil,
270 })
271
272 shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module)
273 assertString(t, shared.OutputFile().Path().Base(), "libbar.so")
Colin Cross33b2fb72019-05-14 14:07:01 -0700274}
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100275
276func TestPrebuiltSymlinkedHostBinary(t *testing.T) {
Colin Cross0c66bc62021-07-20 09:47:41 -0700277 if runtime.GOOS != "linux" {
278 t.Skipf("Skipping host prebuilt testing that is only supported on linux not %s", runtime.GOOS)
Martin Stjernholm6a9a1462020-09-15 02:56:19 +0100279 }
280
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100281 ctx := testPrebuilt(t, `
282 cc_prebuilt_library_shared {
283 name: "libfoo",
284 device_supported: false,
285 host_supported: true,
286 target: {
287 linux_glibc_x86_64: {
288 srcs: ["linux_glibc_x86_64/lib64/libfoo.so"],
289 },
290 },
291 }
292
293 cc_prebuilt_binary {
294 name: "foo",
295 device_supported: false,
296 host_supported: true,
297 shared_libs: ["libfoo"],
298 target: {
299 linux_glibc_x86_64: {
300 srcs: ["linux_glibc_x86_64/bin/foo"],
301 },
302 },
303 }
304 `, map[string][]byte{
305 "libfoo.so": nil,
306 "foo": nil,
307 })
308
309 fooRule := ctx.ModuleForTests("foo", "linux_glibc_x86_64").Rule("Symlink")
Paul Duffine8366da2021-03-24 10:40:38 +0000310 assertString(t, fooRule.Output.String(), "out/soong/.intermediates/foo/linux_glibc_x86_64/foo")
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100311 assertString(t, fooRule.Args["fromPath"], "$$PWD/linux_glibc_x86_64/bin/foo")
312
313 var libfooDep android.Path
314 for _, dep := range fooRule.Implicits {
315 if dep.Base() == "libfoo.so" {
316 libfooDep = dep
317 break
318 }
319 }
Paul Duffine8366da2021-03-24 10:40:38 +0000320 assertString(t, libfooDep.String(), "out/soong/.intermediates/libfoo/linux_glibc_x86_64_shared/libfoo.so")
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100321}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700322
323func TestPrebuiltLibrarySanitized(t *testing.T) {
324 bp := `cc_prebuilt_library {
325 name: "libtest",
326 static: {
327 sanitized: { none: { srcs: ["libf.a"], }, hwaddress: { srcs: ["libf.hwasan.a"], }, },
328 },
329 shared: {
330 sanitized: { none: { srcs: ["libf.so"], }, hwaddress: { srcs: ["hwasan/libf.so"], }, },
331 },
332 }
333 cc_prebuilt_library_static {
334 name: "libtest_static",
335 sanitized: { none: { srcs: ["libf.a"], }, hwaddress: { srcs: ["libf.hwasan.a"], }, },
336 }
337 cc_prebuilt_library_shared {
338 name: "libtest_shared",
339 sanitized: { none: { srcs: ["libf.so"], }, hwaddress: { srcs: ["hwasan/libf.so"], }, },
340 }`
341
342 fs := map[string][]byte{
343 "libf.a": nil,
344 "libf.hwasan.a": nil,
345 "libf.so": nil,
346 "hwasan/libf.so": nil,
347 }
348
349 // Without SANITIZE_TARGET.
350 ctx := testPrebuilt(t, bp, fs)
351
352 shared_rule := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Rule("android/soong/cc.strip")
353 assertString(t, shared_rule.Input.String(), "libf.so")
354
355 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
356 assertString(t, static.OutputFile().Path().Base(), "libf.a")
357
358 shared_rule2 := ctx.ModuleForTests("libtest_shared", "android_arm64_armv8-a_shared").Rule("android/soong/cc.strip")
359 assertString(t, shared_rule2.Input.String(), "libf.so")
360
361 static2 := ctx.ModuleForTests("libtest_static", "android_arm64_armv8-a_static").Module().(*Module)
362 assertString(t, static2.OutputFile().Path().Base(), "libf.a")
363
364 // With SANITIZE_TARGET=hwaddress
Paul Duffin6a1160e2021-03-07 15:47:42 +0000365 ctx = testPrebuilt(t, bp, fs,
366 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
367 variables.SanitizeDevice = []string{"hwaddress"}
368 }),
369 )
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700370
371 shared_rule = ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared_hwasan").Rule("android/soong/cc.strip")
372 assertString(t, shared_rule.Input.String(), "hwasan/libf.so")
373
374 static = ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static_hwasan").Module().(*Module)
375 assertString(t, static.OutputFile().Path().Base(), "libf.hwasan.a")
376
377 shared_rule2 = ctx.ModuleForTests("libtest_shared", "android_arm64_armv8-a_shared_hwasan").Rule("android/soong/cc.strip")
378 assertString(t, shared_rule2.Input.String(), "hwasan/libf.so")
379
380 static2 = ctx.ModuleForTests("libtest_static", "android_arm64_armv8-a_static_hwasan").Module().(*Module)
381 assertString(t, static2.OutputFile().Path().Base(), "libf.hwasan.a")
382}
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400383
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000384func TestPrebuiltLibraryWithBazel(t *testing.T) {
385 const bp = `
386cc_prebuilt_library {
387 name: "foo",
388 shared: {
389 srcs: ["foo.so"],
390 },
391 static: {
392 srcs: ["foo.a"],
393 },
394 bazel_module: { label: "//foo/bar:bar" },
395}`
396 outBaseDir := "outputbase"
397 result := android.GroupFixturePreparers(
398 prepareForPrebuiltTest,
399 android.FixtureModifyConfig(func(config android.Config) {
400 config.BazelContext = android.MockBazelContext{
401 OutputBaseDir: outBaseDir,
402 LabelToCcInfo: map[string]cquery.CcInfo{
403 "//foo/bar:bar": cquery.CcInfo{
404 CcSharedLibraryFiles: []string{"foo.so"},
405 },
406 "//foo/bar:bar_bp2build_cc_library_static": cquery.CcInfo{
407 CcStaticLibraryFiles: []string{"foo.a"},
408 },
409 },
410 }
411 }),
412 ).RunTestWithBp(t, bp)
413 sharedFoo := result.ModuleForTests("foo", "android_arm_armv7-a-neon_shared").Module()
414 pathPrefix := outBaseDir + "/execroot/__main__/"
415
416 sharedInfo := result.ModuleProvider(sharedFoo, SharedLibraryInfoProvider).(SharedLibraryInfo)
417 android.AssertPathRelativeToTopEquals(t,
418 "prebuilt library shared target path did not exist or did not match expected. If the base path is what does not match, it is likely that Soong built this module instead of Bazel.",
419 pathPrefix+"foo.so", sharedInfo.SharedLibrary)
420
421 outputFiles, err := sharedFoo.(android.OutputFileProducer).OutputFiles("")
422 if err != nil {
423 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
424 }
425 expectedOutputFiles := []string{pathPrefix + "foo.so"}
426 android.AssertDeepEquals(t,
427 "prebuilt library shared target output files did not match expected.",
428 expectedOutputFiles, outputFiles.Strings())
429
430 staticFoo := result.ModuleForTests("foo", "android_arm_armv7-a-neon_static").Module()
431 staticInfo := result.ModuleProvider(staticFoo, StaticLibraryInfoProvider).(StaticLibraryInfo)
432 android.AssertPathRelativeToTopEquals(t,
433 "prebuilt library static target path did not exist or did not match expected. If the base path is what does not match, it is likely that Soong built this module instead of Bazel.",
434 pathPrefix+"foo.a", staticInfo.StaticLibrary)
435
436 staticOutputFiles, err := staticFoo.(android.OutputFileProducer).OutputFiles("")
437 if err != nil {
438 t.Errorf("Unexpected error getting cc_object staticOutputFiles %s", err)
439 }
440 expectedStaticOutputFiles := []string{pathPrefix + "foo.a"}
441 android.AssertDeepEquals(t,
442 "prebuilt library static target output files did not match expected.",
443 expectedStaticOutputFiles, staticOutputFiles.Strings())
444}
445
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500446func TestPrebuiltLibraryWithBazelValidations(t *testing.T) {
447 const bp = `
448cc_prebuilt_library {
449 name: "foo",
450 shared: {
451 srcs: ["foo.so"],
452 },
453 static: {
454 srcs: ["foo.a"],
455 },
456 bazel_module: { label: "//foo/bar:bar" },
457 tidy: true,
458}`
459 outBaseDir := "outputbase"
460 result := android.GroupFixturePreparers(
461 prepareForPrebuiltTest,
462 android.FixtureMergeEnv(map[string]string{
463 "ALLOW_LOCAL_TIDY_TRUE": "1",
464 }),
465 android.FixtureModifyConfig(func(config android.Config) {
466 config.BazelContext = android.MockBazelContext{
467 OutputBaseDir: outBaseDir,
468 LabelToCcInfo: map[string]cquery.CcInfo{
469 "//foo/bar:bar": cquery.CcInfo{
470 CcSharedLibraryFiles: []string{"foo.so"},
471 TidyFiles: []string{"foo.c.tidy"},
472 },
473 "//foo/bar:bar_bp2build_cc_library_static": cquery.CcInfo{
474 CcStaticLibraryFiles: []string{"foo.a"},
475 TidyFiles: []string{"foo.c.tidy"},
476 },
477 },
478 }
479 }),
480 ).RunTestWithBp(t, bp)
481 sharedFoo := result.ModuleForTests("foo", "android_arm_armv7-a-neon_shared").Module()
482
483 expectedOutputFile := "out/soong/.intermediates/foo/android_arm_armv7-a-neon_shared/validated/foo.so"
484 sharedInfo := result.ModuleProvider(sharedFoo, SharedLibraryInfoProvider).(SharedLibraryInfo)
485 android.AssertPathRelativeToTopEquals(t,
486 "prebuilt library shared target path did not exist or did not match expected. If the base path is what does not match, it is likely that Soong built this module instead of Bazel.",
487 expectedOutputFile, sharedInfo.SharedLibrary)
488
489 outputFiles, err := sharedFoo.(android.OutputFileProducer).OutputFiles("")
490 if err != nil {
491 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
492 }
493 expectedOutputFiles := []string{expectedOutputFile}
494 android.AssertPathsRelativeToTopEquals(t,
495 "prebuilt library shared target output files did not match expected.",
496 expectedOutputFiles, outputFiles)
497
498 staticFoo := result.ModuleForTests("foo", "android_arm_armv7-a-neon_static").Module()
499 staticInfo := result.ModuleProvider(staticFoo, StaticLibraryInfoProvider).(StaticLibraryInfo)
500 expectedStaticOutputFile := "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static/validated/foo.a"
501 android.AssertPathRelativeToTopEquals(t,
502 "prebuilt library static target path did not exist or did not match expected. If the base path is what does not match, it is likely that Soong built this module instead of Bazel.",
503 expectedStaticOutputFile, staticInfo.StaticLibrary)
504
505 staticOutputFiles, err := staticFoo.(android.OutputFileProducer).OutputFiles("")
506 if err != nil {
507 t.Errorf("Unexpected error getting cc_object staticOutputFiles %s", err)
508 }
509 expectedStaticOutputFiles := []string{expectedStaticOutputFile}
510 android.AssertPathsRelativeToTopEquals(t,
511 "prebuilt library static target output files did not match expected.",
512 expectedStaticOutputFiles, staticOutputFiles)
513}
514
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000515func TestPrebuiltLibraryWithBazelStaticDisabled(t *testing.T) {
516 const bp = `
517cc_prebuilt_library {
518 name: "foo",
519 shared: {
520 srcs: ["foo.so"],
521 },
522 static: {
523 enabled: false
524 },
525 bazel_module: { label: "//foo/bar:bar" },
526}`
527 outBaseDir := "outputbase"
528 result := android.GroupFixturePreparers(
529 prepareForPrebuiltTest,
530 android.FixtureModifyConfig(func(config android.Config) {
531 config.BazelContext = android.MockBazelContext{
532 OutputBaseDir: outBaseDir,
533 LabelToCcInfo: map[string]cquery.CcInfo{
534 "//foo/bar:bar": cquery.CcInfo{
535 CcSharedLibraryFiles: []string{"foo.so"},
536 },
537 },
538 }
539 }),
540 ).RunTestWithBp(t, bp)
541 sharedFoo := result.ModuleForTests("foo", "android_arm_armv7-a-neon_shared").Module()
542 pathPrefix := outBaseDir + "/execroot/__main__/"
543
544 sharedInfo := result.ModuleProvider(sharedFoo, SharedLibraryInfoProvider).(SharedLibraryInfo)
545 android.AssertPathRelativeToTopEquals(t,
546 "prebuilt library shared target path did not exist or did not match expected. If the base path is what does not match, it is likely that Soong built this module instead of Bazel.",
547 pathPrefix+"foo.so", sharedInfo.SharedLibrary)
548
549 outputFiles, err := sharedFoo.(android.OutputFileProducer).OutputFiles("")
550 if err != nil {
551 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
552 }
553 expectedOutputFiles := []string{pathPrefix + "foo.so"}
554 android.AssertDeepEquals(t,
555 "prebuilt library shared target output files did not match expected.",
556 expectedOutputFiles, outputFiles.Strings())
557}
558
559func TestPrebuiltLibraryStaticWithBazel(t *testing.T) {
560 const bp = `
561cc_prebuilt_library_static {
562 name: "foo",
563 srcs: ["foo.so"],
564 bazel_module: { label: "//foo/bar:bar" },
565}`
566 outBaseDir := "outputbase"
567 result := android.GroupFixturePreparers(
568 prepareForPrebuiltTest,
569 android.FixtureModifyConfig(func(config android.Config) {
570 config.BazelContext = android.MockBazelContext{
571 OutputBaseDir: outBaseDir,
572 LabelToCcInfo: map[string]cquery.CcInfo{
573 "//foo/bar:bar": cquery.CcInfo{
574 CcStaticLibraryFiles: []string{"foo.so"},
575 },
576 },
577 }
578 }),
579 ).RunTestWithBp(t, bp)
580 staticFoo := result.ModuleForTests("foo", "android_arm_armv7-a-neon_static").Module()
581 pathPrefix := outBaseDir + "/execroot/__main__/"
582
583 info := result.ModuleProvider(staticFoo, StaticLibraryInfoProvider).(StaticLibraryInfo)
584 android.AssertPathRelativeToTopEquals(t,
585 "prebuilt library static path did not match expected. If the base path is what does not match, it is likely that Soong built this module instead of Bazel.",
586 pathPrefix+"foo.so", info.StaticLibrary)
587
588 outputFiles, err := staticFoo.(android.OutputFileProducer).OutputFiles("")
589 if err != nil {
590 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
591 }
592 expectedOutputFiles := []string{pathPrefix + "foo.so"}
593 android.AssertDeepEquals(t, "prebuilt library static output files did not match expected.", expectedOutputFiles, outputFiles.Strings())
594}
595
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400596func TestPrebuiltLibrarySharedWithBazelWithoutToc(t *testing.T) {
597 const bp = `
598cc_prebuilt_library_shared {
599 name: "foo",
600 srcs: ["foo.so"],
601 bazel_module: { label: "//foo/bar:bar" },
602}`
603 outBaseDir := "outputbase"
604 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
605 config.BazelContext = android.MockBazelContext{
606 OutputBaseDir: outBaseDir,
607 LabelToCcInfo: map[string]cquery.CcInfo{
608 "//foo/bar:bar": cquery.CcInfo{
609 CcSharedLibraryFiles: []string{"foo.so"},
610 },
611 },
612 }
613 ctx := testCcWithConfig(t, config)
614 sharedFoo := ctx.ModuleForTests("foo", "android_arm_armv7-a-neon_shared").Module()
615 pathPrefix := outBaseDir + "/execroot/__main__/"
616
617 info := ctx.ModuleProvider(sharedFoo, SharedLibraryInfoProvider).(SharedLibraryInfo)
618 android.AssertPathRelativeToTopEquals(t, "prebuilt shared library",
619 pathPrefix+"foo.so", info.SharedLibrary)
620 android.AssertPathRelativeToTopEquals(t, "prebuilt's 'nullary' ToC",
621 pathPrefix+"foo.so", info.TableOfContents.Path())
622
623 outputFiles, err := sharedFoo.(android.OutputFileProducer).OutputFiles("")
624 if err != nil {
625 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
626 }
627 expectedOutputFiles := []string{pathPrefix + "foo.so"}
628 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
629}
630
631func TestPrebuiltLibrarySharedWithBazelWithToc(t *testing.T) {
632 const bp = `
633cc_prebuilt_library_shared {
634 name: "foo",
635 srcs: ["foo.so"],
636 bazel_module: { label: "//foo/bar:bar" },
637}`
638 outBaseDir := "outputbase"
639 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
640 config.BazelContext = android.MockBazelContext{
641 OutputBaseDir: outBaseDir,
642 LabelToCcInfo: map[string]cquery.CcInfo{
643 "//foo/bar:bar": cquery.CcInfo{
644 CcSharedLibraryFiles: []string{"foo.so"},
645 TocFile: "toc",
646 },
647 },
648 }
649 ctx := testCcWithConfig(t, config)
650 sharedFoo := ctx.ModuleForTests("foo", "android_arm_armv7-a-neon_shared").Module()
651 pathPrefix := outBaseDir + "/execroot/__main__/"
652
653 info := ctx.ModuleProvider(sharedFoo, SharedLibraryInfoProvider).(SharedLibraryInfo)
654 android.AssertPathRelativeToTopEquals(t, "prebuilt shared library's ToC",
655 pathPrefix+"toc", info.TableOfContents.Path())
656 android.AssertPathRelativeToTopEquals(t, "prebuilt shared library",
657 pathPrefix+"foo.so", info.SharedLibrary)
658
659 outputFiles, err := sharedFoo.(android.OutputFileProducer).OutputFiles("")
660 if err != nil {
661 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
662 }
663 expectedOutputFiles := []string{pathPrefix + "foo.so"}
664 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
665}
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +0000666
667func TestPrebuiltStubNoinstall(t *testing.T) {
668 testFunc := func(t *testing.T, bp string) {
669 result := android.GroupFixturePreparers(
670 prepareForPrebuiltTest,
671 android.PrepareForTestWithMakevars,
672 ).RunTestWithBp(t, bp)
673
674 installRules := result.InstallMakeRulesForTesting(t)
675 var installedlibRule *android.InstallMakeRule
676 for i, rule := range installRules {
677 if rule.Target == "out/target/product/test_device/system/lib/installedlib.so" {
678 if installedlibRule != nil {
679 t.Errorf("Duplicate install rules for %s", rule.Target)
680 }
681 installedlibRule = &installRules[i]
682 }
683 }
684 if installedlibRule == nil {
685 t.Errorf("No install rule found for installedlib")
686 return
687 }
688
689 android.AssertStringListDoesNotContain(t,
690 "installedlib has install dependency on stub",
691 installedlibRule.Deps,
692 "out/target/product/test_device/system/lib/stublib.so")
693 android.AssertStringListDoesNotContain(t,
694 "installedlib has order-only install dependency on stub",
695 installedlibRule.OrderOnlyDeps,
696 "out/target/product/test_device/system/lib/stublib.so")
697 }
698
699 const prebuiltStublibBp = `
700 cc_prebuilt_library {
701 name: "stublib",
702 prefer: true,
703 srcs: ["foo.so"],
704 stubs: {
705 versions: ["1"],
706 },
707 }
708 `
709
710 const installedlibBp = `
711 cc_library {
712 name: "installedlib",
713 shared_libs: ["stublib"],
714 }
715 `
716
717 t.Run("prebuilt without source", func(t *testing.T) {
718 testFunc(t, prebuiltStublibBp+installedlibBp)
719 })
720
721 const disabledSourceStublibBp = `
722 cc_library {
723 name: "stublib",
724 enabled: false,
725 stubs: {
726 versions: ["1"],
727 },
728 }
729 `
730
731 t.Run("prebuilt with disabled source", func(t *testing.T) {
732 testFunc(t, disabledSourceStublibBp+prebuiltStublibBp+installedlibBp)
733 })
734}
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb1bd7702022-10-04 17:45:24 +0000735
736func TestPrebuiltBinaryNoSrcsNoError(t *testing.T) {
737 const bp = `
738cc_prebuilt_binary {
739 name: "bintest",
740 srcs: [],
741}`
742 ctx := testPrebuilt(t, bp, map[string][]byte{})
743 mod := ctx.ModuleForTests("bintest", "android_arm64_armv8-a").Module().(*Module)
744 android.AssertBoolEquals(t, `expected no srcs to yield no output file`, false, mod.OutputFile().Valid())
745}
746
747func TestPrebuiltBinaryMultipleSrcs(t *testing.T) {
748 const bp = `
749cc_prebuilt_binary {
750 name: "bintest",
751 srcs: ["foo", "bar"],
752}`
753 testCcError(t, `Android.bp:4:6: module "bintest" variant "android_arm64_armv8-a": srcs: multiple prebuilt source files`, bp)
754}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000755
756func TestPrebuiltBinaryWithBazel(t *testing.T) {
757 const bp = `
758cc_prebuilt_binary {
759 name: "bintest",
760 srcs: ["bin"],
761 bazel_module: { label: "//bin/foo:foo" },
762}`
763 const outBaseDir = "outputbase"
764 const expectedOut = outBaseDir + "/execroot/__main__/bin"
765 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
766 config.BazelContext = android.MockBazelContext{
767 OutputBaseDir: outBaseDir,
768 LabelToOutputFiles: map[string][]string{"//bin/foo:foo": []string{"bin"}},
769 }
770 ctx := testCcWithConfig(t, config)
771 bin := ctx.ModuleForTests("bintest", "android_arm64_armv8-a").Module().(*Module)
772 out := bin.OutputFile()
773 if !out.Valid() {
774 t.Error("Invalid output file")
775 return
776 }
777 android.AssertStringEquals(t, "output file", expectedOut, out.String())
778}