blob: f3d5e60d54cdc9553fa2320355eb28c16135b31f [file] [log] [blame]
Colin Crossd00350c2017-11-17 10:55:38 -08001// Copyright 2017 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
Colin Cross74d1ec02015-04-28 13:30:13 -070015package cc
16
17import (
Colin Cross5b529592017-05-09 13:34:34 -070018 "android/soong/android"
Colin Crossf18e1102017-11-16 14:33:08 -080019
Jeff Gaston294356f2017-09-27 17:05:30 -070020 "fmt"
Jiyong Park6a43f042017-10-12 23:05:00 +090021 "io/ioutil"
22 "os"
Colin Cross74d1ec02015-04-28 13:30:13 -070023 "reflect"
Jeff Gaston294356f2017-09-27 17:05:30 -070024 "sort"
25 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070026 "testing"
27)
28
Jiyong Park6a43f042017-10-12 23:05:00 +090029var buildDir string
30
31func setUp() {
32 var err error
33 buildDir, err = ioutil.TempDir("", "soong_cc_test")
34 if err != nil {
35 panic(err)
36 }
37}
38
39func tearDown() {
40 os.RemoveAll(buildDir)
41}
42
43func TestMain(m *testing.M) {
44 run := func() int {
45 setUp()
46 defer tearDown()
47
48 return m.Run()
49 }
50
51 os.Exit(run())
52}
53
Doug Hornc32c6b02019-01-17 14:44:05 -080054func createTestContext(t *testing.T, config android.Config, bp string, os android.OsType) *android.TestContext {
55 ctx := android.NewTestArchContext()
56 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(BinaryFactory))
Colin Crossfe17f6f2019-03-28 19:30:56 -070057 ctx.RegisterModuleType("cc_binary_host", android.ModuleFactoryAdaptor(binaryHostFactory))
Doug Hornc32c6b02019-01-17 14:44:05 -080058 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(LibraryFactory))
59 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(LibrarySharedFactory))
Jiyong Parke4bb9862019-02-01 00:31:10 +090060 ctx.RegisterModuleType("cc_library_static", android.ModuleFactoryAdaptor(LibraryStaticFactory))
Doug Hornc32c6b02019-01-17 14:44:05 -080061 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(LibraryHeaderFactory))
62 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(ToolchainLibraryFactory))
63 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(LlndkLibraryFactory))
64 ctx.RegisterModuleType("llndk_headers", android.ModuleFactoryAdaptor(llndkHeadersFactory))
65 ctx.RegisterModuleType("vendor_public_library", android.ModuleFactoryAdaptor(vendorPublicLibraryFactory))
66 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(ObjectFactory))
67 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
68 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
69 ctx.BottomUp("image", ImageMutator).Parallel()
70 ctx.BottomUp("link", LinkageMutator).Parallel()
71 ctx.BottomUp("vndk", VndkMutator).Parallel()
72 ctx.BottomUp("version", VersionMutator).Parallel()
73 ctx.BottomUp("begin", BeginMutator).Parallel()
74 })
Jooyung Hana70f0672019-01-18 15:20:43 +090075 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
76 ctx.TopDown("double_loadable", checkDoubleLoadableLibraries).Parallel()
77 })
Doug Hornc32c6b02019-01-17 14:44:05 -080078 ctx.Register()
79
80 // add some modules that are required by the compiler and/or linker
Inseob Kimc0907f12019-02-08 21:00:45 +090081 bp = bp + GatherRequiredDepsForTest(os)
Jeff Gaston294356f2017-09-27 17:05:30 -070082
Jiyong Park6a43f042017-10-12 23:05:00 +090083 ctx.MockFileSystem(map[string][]byte{
Jiyong Park7ed9de32018-10-15 22:25:07 +090084 "Android.bp": []byte(bp),
85 "foo.c": nil,
86 "bar.c": nil,
87 "a.proto": nil,
88 "b.aidl": nil,
89 "my_include": nil,
90 "foo.map.txt": nil,
Jiyong Park6a43f042017-10-12 23:05:00 +090091 })
92
Logan Chienf3511742017-10-31 18:04:35 +080093 return ctx
94}
95
96func testCcWithConfig(t *testing.T, bp string, config android.Config) *android.TestContext {
Doug Hornc32c6b02019-01-17 14:44:05 -080097 return testCcWithConfigForOs(t, bp, config, android.Android)
98}
99
100func testCcWithConfigForOs(t *testing.T, bp string, config android.Config, os android.OsType) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800101 t.Helper()
Doug Hornc32c6b02019-01-17 14:44:05 -0800102 ctx := createTestContext(t, config, bp, os)
Logan Chienf3511742017-10-31 18:04:35 +0800103
Jeff Gastond3e141d2017-08-08 17:46:01 -0700104 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800105 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +0900106 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800107 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +0900108
109 return ctx
110}
111
Logan Chienf3511742017-10-31 18:04:35 +0800112func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800113 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800114 config := android.TestArchConfig(buildDir, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700115 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
116 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800117
118 return testCcWithConfig(t, bp, config)
119}
120
121func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800122 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800123 config := android.TestArchConfig(buildDir, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700124 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800125
126 return testCcWithConfig(t, bp, config)
127}
128
129func testCcError(t *testing.T, pattern string, bp string) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800130 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800131 config := android.TestArchConfig(buildDir, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700132 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
133 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800134
Doug Hornc32c6b02019-01-17 14:44:05 -0800135 ctx := createTestContext(t, config, bp, android.Android)
Logan Chienf3511742017-10-31 18:04:35 +0800136
137 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
138 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +0800139 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800140 return
141 }
142
143 _, errs = ctx.PrepareBuildActions(config)
144 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +0800145 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800146 return
147 }
148
149 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
150}
151
152const (
Jiyong Park5baac542018-08-28 09:55:37 +0900153 coreVariant = "android_arm64_armv8-a_core_shared"
154 vendorVariant = "android_arm64_armv8-a_vendor_shared"
155 recoveryVariant = "android_arm64_armv8-a_recovery_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800156)
157
Doug Hornc32c6b02019-01-17 14:44:05 -0800158func TestFuchsiaDeps(t *testing.T) {
159 t.Helper()
160
161 bp := `
162 cc_library {
163 name: "libTest",
164 srcs: ["foo.c"],
165 target: {
166 fuchsia: {
167 srcs: ["bar.c"],
168 },
169 },
170 }`
171
172 config := android.TestArchConfigFuchsia(buildDir, nil)
173 ctx := testCcWithConfigForOs(t, bp, config, android.Fuchsia)
174
175 rt := false
176 fb := false
177
178 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
179 implicits := ld.Implicits
180 for _, lib := range implicits {
181 if strings.Contains(lib.Rel(), "libcompiler_rt") {
182 rt = true
183 }
184
185 if strings.Contains(lib.Rel(), "libbioniccompat") {
186 fb = true
187 }
188 }
189
190 if !rt || !fb {
191 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
192 }
193}
194
195func TestFuchsiaTargetDecl(t *testing.T) {
196 t.Helper()
197
198 bp := `
199 cc_library {
200 name: "libTest",
201 srcs: ["foo.c"],
202 target: {
203 fuchsia: {
204 srcs: ["bar.c"],
205 },
206 },
207 }`
208
209 config := android.TestArchConfigFuchsia(buildDir, nil)
210 ctx := testCcWithConfigForOs(t, bp, config, android.Fuchsia)
211 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
212 var objs []string
213 for _, o := range ld.Inputs {
214 objs = append(objs, o.Base())
215 }
216 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
217 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
218 }
219}
220
Jiyong Park6a43f042017-10-12 23:05:00 +0900221func TestVendorSrc(t *testing.T) {
222 ctx := testCc(t, `
223 cc_library {
224 name: "libTest",
225 srcs: ["foo.c"],
Logan Chienf3511742017-10-31 18:04:35 +0800226 no_libgcc: true,
227 nocrt: true,
228 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900229 vendor_available: true,
230 target: {
231 vendor: {
232 srcs: ["bar.c"],
233 },
234 },
235 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900236 `)
237
Logan Chienf3511742017-10-31 18:04:35 +0800238 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900239 var objs []string
240 for _, o := range ld.Inputs {
241 objs = append(objs, o.Base())
242 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800243 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900244 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
245 }
246}
247
Logan Chienf3511742017-10-31 18:04:35 +0800248func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
249 isVndkSp bool, extends string) {
250
Logan Chiend3c59a22018-03-29 14:08:15 +0800251 t.Helper()
252
Logan Chienf3511742017-10-31 18:04:35 +0800253 mod := ctx.ModuleForTests(name, vendorVariant).Module().(*Module)
254 if !mod.hasVendorVariant() {
Colin Crossf46e37f2018-03-21 16:25:58 -0700255 t.Errorf("%q must have vendor variant", name)
Logan Chienf3511742017-10-31 18:04:35 +0800256 }
257
258 // Check library properties.
259 lib, ok := mod.compiler.(*libraryDecorator)
260 if !ok {
261 t.Errorf("%q must have libraryDecorator", name)
262 } else if lib.baseInstaller.subDir != subDir {
263 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
264 lib.baseInstaller.subDir)
265 }
266
267 // Check VNDK properties.
268 if mod.vndkdep == nil {
269 t.Fatalf("%q must have `vndkdep`", name)
270 }
271 if !mod.isVndk() {
272 t.Errorf("%q isVndk() must equal to true", name)
273 }
274 if mod.isVndkSp() != isVndkSp {
275 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
276 }
277
278 // Check VNDK extension properties.
279 isVndkExt := extends != ""
280 if mod.isVndkExt() != isVndkExt {
281 t.Errorf("%q isVndkExt() must equal to %t", name, isVndkExt)
282 }
283
284 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
285 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
286 }
287}
288
289func TestVndk(t *testing.T) {
290 ctx := testCc(t, `
291 cc_library {
292 name: "libvndk",
293 vendor_available: true,
294 vndk: {
295 enabled: true,
296 },
297 nocrt: true,
298 }
299
300 cc_library {
301 name: "libvndk_private",
302 vendor_available: false,
303 vndk: {
304 enabled: true,
305 },
306 nocrt: true,
307 }
308
309 cc_library {
310 name: "libvndk_sp",
311 vendor_available: true,
312 vndk: {
313 enabled: true,
314 support_system_process: true,
315 },
316 nocrt: true,
317 }
318
319 cc_library {
320 name: "libvndk_sp_private",
321 vendor_available: false,
322 vndk: {
323 enabled: true,
324 support_system_process: true,
325 },
326 nocrt: true,
327 }
328 `)
329
330 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "")
331 checkVndkModule(t, ctx, "libvndk_private", "vndk-VER", false, "")
332 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "")
333 checkVndkModule(t, ctx, "libvndk_sp_private", "vndk-sp-VER", true, "")
334}
335
Logan Chiend3c59a22018-03-29 14:08:15 +0800336func TestVndkDepError(t *testing.T) {
337 // Check whether an error is emitted when a VNDK lib depends on a system lib.
338 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
339 cc_library {
340 name: "libvndk",
341 vendor_available: true,
342 vndk: {
343 enabled: true,
344 },
345 shared_libs: ["libfwk"], // Cause error
346 nocrt: true,
347 }
348
349 cc_library {
350 name: "libfwk",
351 nocrt: true,
352 }
353 `)
354
355 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
356 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
357 cc_library {
358 name: "libvndk",
359 vendor_available: true,
360 vndk: {
361 enabled: true,
362 },
363 shared_libs: ["libvendor"], // Cause error
364 nocrt: true,
365 }
366
367 cc_library {
368 name: "libvendor",
369 vendor: true,
370 nocrt: true,
371 }
372 `)
373
374 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
375 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
376 cc_library {
377 name: "libvndk_sp",
378 vendor_available: true,
379 vndk: {
380 enabled: true,
381 support_system_process: true,
382 },
383 shared_libs: ["libfwk"], // Cause error
384 nocrt: true,
385 }
386
387 cc_library {
388 name: "libfwk",
389 nocrt: true,
390 }
391 `)
392
393 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
394 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
395 cc_library {
396 name: "libvndk_sp",
397 vendor_available: true,
398 vndk: {
399 enabled: true,
400 support_system_process: true,
401 },
402 shared_libs: ["libvendor"], // Cause error
403 nocrt: true,
404 }
405
406 cc_library {
407 name: "libvendor",
408 vendor: true,
409 nocrt: true,
410 }
411 `)
412
413 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
414 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
415 cc_library {
416 name: "libvndk_sp",
417 vendor_available: true,
418 vndk: {
419 enabled: true,
420 support_system_process: true,
421 },
422 shared_libs: ["libvndk"], // Cause error
423 nocrt: true,
424 }
425
426 cc_library {
427 name: "libvndk",
428 vendor_available: true,
429 vndk: {
430 enabled: true,
431 },
432 nocrt: true,
433 }
434 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900435
436 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
437 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
438 cc_library {
439 name: "libvndk",
440 vendor_available: true,
441 vndk: {
442 enabled: true,
443 },
444 shared_libs: ["libnonvndk"],
445 nocrt: true,
446 }
447
448 cc_library {
449 name: "libnonvndk",
450 vendor_available: true,
451 nocrt: true,
452 }
453 `)
454
455 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
456 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
457 cc_library {
458 name: "libvndkprivate",
459 vendor_available: false,
460 vndk: {
461 enabled: true,
462 },
463 shared_libs: ["libnonvndk"],
464 nocrt: true,
465 }
466
467 cc_library {
468 name: "libnonvndk",
469 vendor_available: true,
470 nocrt: true,
471 }
472 `)
473
474 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
475 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
476 cc_library {
477 name: "libvndksp",
478 vendor_available: true,
479 vndk: {
480 enabled: true,
481 support_system_process: true,
482 },
483 shared_libs: ["libnonvndk"],
484 nocrt: true,
485 }
486
487 cc_library {
488 name: "libnonvndk",
489 vendor_available: true,
490 nocrt: true,
491 }
492 `)
493
494 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
495 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
496 cc_library {
497 name: "libvndkspprivate",
498 vendor_available: false,
499 vndk: {
500 enabled: true,
501 support_system_process: true,
502 },
503 shared_libs: ["libnonvndk"],
504 nocrt: true,
505 }
506
507 cc_library {
508 name: "libnonvndk",
509 vendor_available: true,
510 nocrt: true,
511 }
512 `)
513}
514
515func TestDoubleLoadbleDep(t *testing.T) {
516 // okay to link : LLNDK -> double_loadable VNDK
517 testCc(t, `
518 cc_library {
519 name: "libllndk",
520 shared_libs: ["libdoubleloadable"],
521 }
522
523 llndk_library {
524 name: "libllndk",
525 symbol_file: "",
526 }
527
528 cc_library {
529 name: "libdoubleloadable",
530 vendor_available: true,
531 vndk: {
532 enabled: true,
533 },
534 double_loadable: true,
535 }
536 `)
537 // okay to link : LLNDK -> VNDK-SP
538 testCc(t, `
539 cc_library {
540 name: "libllndk",
541 shared_libs: ["libvndksp"],
542 }
543
544 llndk_library {
545 name: "libllndk",
546 symbol_file: "",
547 }
548
549 cc_library {
550 name: "libvndksp",
551 vendor_available: true,
552 vndk: {
553 enabled: true,
554 support_system_process: true,
555 },
556 }
557 `)
558 // okay to link : double_loadable -> double_loadable
559 testCc(t, `
560 cc_library {
561 name: "libdoubleloadable1",
562 shared_libs: ["libdoubleloadable2"],
563 vendor_available: true,
564 double_loadable: true,
565 }
566
567 cc_library {
568 name: "libdoubleloadable2",
569 vendor_available: true,
570 double_loadable: true,
571 }
572 `)
573 // okay to link : double_loadable VNDK -> double_loadable VNDK private
574 testCc(t, `
575 cc_library {
576 name: "libdoubleloadable",
577 vendor_available: true,
578 vndk: {
579 enabled: true,
580 },
581 double_loadable: true,
582 shared_libs: ["libnondoubleloadable"],
583 }
584
585 cc_library {
586 name: "libnondoubleloadable",
587 vendor_available: false,
588 vndk: {
589 enabled: true,
590 },
591 double_loadable: true,
592 }
593 `)
594 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
595 testCc(t, `
596 cc_library {
597 name: "libllndk",
598 shared_libs: ["libcoreonly"],
599 }
600
601 llndk_library {
602 name: "libllndk",
603 symbol_file: "",
604 }
605
606 cc_library {
607 name: "libcoreonly",
608 shared_libs: ["libvendoravailable"],
609 }
610
611 // indirect dependency of LLNDK
612 cc_library {
613 name: "libvendoravailable",
614 vendor_available: true,
615 double_loadable: true,
616 }
617 `)
618}
619
620func TestDoubleLoadableDepError(t *testing.T) {
621 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
622 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
623 cc_library {
624 name: "libllndk",
625 shared_libs: ["libnondoubleloadable"],
626 }
627
628 llndk_library {
629 name: "libllndk",
630 symbol_file: "",
631 }
632
633 cc_library {
634 name: "libnondoubleloadable",
635 vendor_available: true,
636 vndk: {
637 enabled: true,
638 },
639 }
640 `)
641
642 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
643 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
644 cc_library {
645 name: "libllndk",
646 no_libgcc: true,
647 shared_libs: ["libnondoubleloadable"],
648 }
649
650 llndk_library {
651 name: "libllndk",
652 symbol_file: "",
653 }
654
655 cc_library {
656 name: "libnondoubleloadable",
657 vendor_available: true,
658 }
659 `)
660
661 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
662 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
663 cc_library {
664 name: "libdoubleloadable",
665 vendor_available: true,
666 double_loadable: true,
667 shared_libs: ["libnondoubleloadable"],
668 }
669
670 cc_library {
671 name: "libnondoubleloadable",
672 vendor_available: true,
673 }
674 `)
675
676 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
677 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
678 cc_library {
679 name: "libdoubleloadable",
680 vendor_available: true,
681 double_loadable: true,
682 shared_libs: ["libnondoubleloadable"],
683 }
684
685 cc_library {
686 name: "libnondoubleloadable",
687 vendor_available: true,
688 vndk: {
689 enabled: true,
690 },
691 }
692 `)
693
694 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
695 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
696 cc_library {
697 name: "libdoubleloadable",
698 vendor_available: true,
699 vndk: {
700 enabled: true,
701 },
702 double_loadable: true,
703 shared_libs: ["libnondoubleloadable"],
704 }
705
706 cc_library {
707 name: "libnondoubleloadable",
708 vendor_available: false,
709 vndk: {
710 enabled: true,
711 },
712 }
713 `)
714
715 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
716 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
717 cc_library {
718 name: "libllndk",
719 shared_libs: ["libcoreonly"],
720 }
721
722 llndk_library {
723 name: "libllndk",
724 symbol_file: "",
725 }
726
727 cc_library {
728 name: "libcoreonly",
729 shared_libs: ["libvendoravailable"],
730 }
731
732 // indirect dependency of LLNDK
733 cc_library {
734 name: "libvendoravailable",
735 vendor_available: true,
736 }
737 `)
Logan Chiend3c59a22018-03-29 14:08:15 +0800738}
739
Justin Yun9357f4a2018-11-28 15:14:47 +0900740func TestVndkMustNotBeProductSpecific(t *testing.T) {
741 // Check whether an error is emitted when a vndk lib has 'product_specific: true'.
742 testCcError(t, "product_specific must not be true when `vndk: {enabled: true}`", `
743 cc_library {
744 name: "libvndk",
745 product_specific: true, // Cause error
746 vendor_available: true,
747 vndk: {
748 enabled: true,
749 },
750 nocrt: true,
751 }
752 `)
753}
754
Logan Chienf3511742017-10-31 18:04:35 +0800755func TestVndkExt(t *testing.T) {
756 // This test checks the VNDK-Ext properties.
757 ctx := testCc(t, `
758 cc_library {
759 name: "libvndk",
760 vendor_available: true,
761 vndk: {
762 enabled: true,
763 },
764 nocrt: true,
765 }
766
767 cc_library {
768 name: "libvndk_ext",
769 vendor: true,
770 vndk: {
771 enabled: true,
772 extends: "libvndk",
773 },
774 nocrt: true,
775 }
776 `)
777
778 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk")
779}
780
Logan Chiend3c59a22018-03-29 14:08:15 +0800781func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +0800782 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
783 ctx := testCcNoVndk(t, `
784 cc_library {
785 name: "libvndk",
786 vendor_available: true,
787 vndk: {
788 enabled: true,
789 },
790 nocrt: true,
791 }
792
793 cc_library {
794 name: "libvndk_ext",
795 vendor: true,
796 vndk: {
797 enabled: true,
798 extends: "libvndk",
799 },
800 nocrt: true,
801 }
802 `)
803
804 // Ensures that the core variant of "libvndk_ext" can be found.
805 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
806 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
807 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
808 }
809}
810
811func TestVndkExtError(t *testing.T) {
812 // This test ensures an error is emitted in ill-formed vndk-ext definition.
813 testCcError(t, "must set `vendor: true` to set `extends: \".*\"`", `
814 cc_library {
815 name: "libvndk",
816 vendor_available: true,
817 vndk: {
818 enabled: true,
819 },
820 nocrt: true,
821 }
822
823 cc_library {
824 name: "libvndk_ext",
825 vndk: {
826 enabled: true,
827 extends: "libvndk",
828 },
829 nocrt: true,
830 }
831 `)
832
833 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
834 cc_library {
835 name: "libvndk",
836 vendor_available: true,
837 vndk: {
838 enabled: true,
839 },
840 nocrt: true,
841 }
842
843 cc_library {
844 name: "libvndk_ext",
845 vendor: true,
846 vndk: {
847 enabled: true,
848 },
849 nocrt: true,
850 }
851 `)
852}
853
854func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
855 // This test ensures an error is emitted for inconsistent support_system_process.
856 testCcError(t, "module \".*\" with mismatched support_system_process", `
857 cc_library {
858 name: "libvndk",
859 vendor_available: true,
860 vndk: {
861 enabled: true,
862 },
863 nocrt: true,
864 }
865
866 cc_library {
867 name: "libvndk_sp_ext",
868 vendor: true,
869 vndk: {
870 enabled: true,
871 extends: "libvndk",
872 support_system_process: true,
873 },
874 nocrt: true,
875 }
876 `)
877
878 testCcError(t, "module \".*\" with mismatched support_system_process", `
879 cc_library {
880 name: "libvndk_sp",
881 vendor_available: true,
882 vndk: {
883 enabled: true,
884 support_system_process: true,
885 },
886 nocrt: true,
887 }
888
889 cc_library {
890 name: "libvndk_ext",
891 vendor: true,
892 vndk: {
893 enabled: true,
894 extends: "libvndk_sp",
895 },
896 nocrt: true,
897 }
898 `)
899}
900
901func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800902 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +0800903 // with `vendor_available: false`.
904 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
905 cc_library {
906 name: "libvndk",
907 vendor_available: false,
908 vndk: {
909 enabled: true,
910 },
911 nocrt: true,
912 }
913
914 cc_library {
915 name: "libvndk_ext",
916 vendor: true,
917 vndk: {
918 enabled: true,
919 extends: "libvndk",
920 },
921 nocrt: true,
922 }
923 `)
924}
925
Logan Chiend3c59a22018-03-29 14:08:15 +0800926func TestVendorModuleUseVndkExt(t *testing.T) {
927 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +0800928 testCc(t, `
929 cc_library {
930 name: "libvndk",
931 vendor_available: true,
932 vndk: {
933 enabled: true,
934 },
935 nocrt: true,
936 }
937
938 cc_library {
939 name: "libvndk_ext",
940 vendor: true,
941 vndk: {
942 enabled: true,
943 extends: "libvndk",
944 },
945 nocrt: true,
946 }
947
948 cc_library {
949
950 name: "libvndk_sp",
951 vendor_available: true,
952 vndk: {
953 enabled: true,
954 support_system_process: true,
955 },
956 nocrt: true,
957 }
958
959 cc_library {
960 name: "libvndk_sp_ext",
961 vendor: true,
962 vndk: {
963 enabled: true,
964 extends: "libvndk_sp",
965 support_system_process: true,
966 },
967 nocrt: true,
968 }
969
970 cc_library {
971 name: "libvendor",
972 vendor: true,
973 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
974 nocrt: true,
975 }
976 `)
977}
978
Logan Chiend3c59a22018-03-29 14:08:15 +0800979func TestVndkExtUseVendorLib(t *testing.T) {
980 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +0800981 testCc(t, `
982 cc_library {
983 name: "libvndk",
984 vendor_available: true,
985 vndk: {
986 enabled: true,
987 },
988 nocrt: true,
989 }
990
991 cc_library {
992 name: "libvndk_ext",
993 vendor: true,
994 vndk: {
995 enabled: true,
996 extends: "libvndk",
997 },
998 shared_libs: ["libvendor"],
999 nocrt: true,
1000 }
1001
1002 cc_library {
1003 name: "libvendor",
1004 vendor: true,
1005 nocrt: true,
1006 }
1007 `)
Logan Chienf3511742017-10-31 18:04:35 +08001008
Logan Chiend3c59a22018-03-29 14:08:15 +08001009 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1010 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001011 cc_library {
1012 name: "libvndk_sp",
1013 vendor_available: true,
1014 vndk: {
1015 enabled: true,
1016 support_system_process: true,
1017 },
1018 nocrt: true,
1019 }
1020
1021 cc_library {
1022 name: "libvndk_sp_ext",
1023 vendor: true,
1024 vndk: {
1025 enabled: true,
1026 extends: "libvndk_sp",
1027 support_system_process: true,
1028 },
1029 shared_libs: ["libvendor"], // Cause an error
1030 nocrt: true,
1031 }
1032
1033 cc_library {
1034 name: "libvendor",
1035 vendor: true,
1036 nocrt: true,
1037 }
1038 `)
1039}
1040
Logan Chiend3c59a22018-03-29 14:08:15 +08001041func TestVndkSpExtUseVndkError(t *testing.T) {
1042 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1043 // library.
1044 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1045 cc_library {
1046 name: "libvndk",
1047 vendor_available: true,
1048 vndk: {
1049 enabled: true,
1050 },
1051 nocrt: true,
1052 }
1053
1054 cc_library {
1055 name: "libvndk_sp",
1056 vendor_available: true,
1057 vndk: {
1058 enabled: true,
1059 support_system_process: true,
1060 },
1061 nocrt: true,
1062 }
1063
1064 cc_library {
1065 name: "libvndk_sp_ext",
1066 vendor: true,
1067 vndk: {
1068 enabled: true,
1069 extends: "libvndk_sp",
1070 support_system_process: true,
1071 },
1072 shared_libs: ["libvndk"], // Cause an error
1073 nocrt: true,
1074 }
1075 `)
1076
1077 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1078 // library.
1079 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1080 cc_library {
1081 name: "libvndk",
1082 vendor_available: true,
1083 vndk: {
1084 enabled: true,
1085 },
1086 nocrt: true,
1087 }
1088
1089 cc_library {
1090 name: "libvndk_ext",
1091 vendor: true,
1092 vndk: {
1093 enabled: true,
1094 extends: "libvndk",
1095 },
1096 nocrt: true,
1097 }
1098
1099 cc_library {
1100 name: "libvndk_sp",
1101 vendor_available: true,
1102 vndk: {
1103 enabled: true,
1104 support_system_process: true,
1105 },
1106 nocrt: true,
1107 }
1108
1109 cc_library {
1110 name: "libvndk_sp_ext",
1111 vendor: true,
1112 vndk: {
1113 enabled: true,
1114 extends: "libvndk_sp",
1115 support_system_process: true,
1116 },
1117 shared_libs: ["libvndk_ext"], // Cause an error
1118 nocrt: true,
1119 }
1120 `)
1121}
1122
1123func TestVndkUseVndkExtError(t *testing.T) {
1124 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1125 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001126 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1127 cc_library {
1128 name: "libvndk",
1129 vendor_available: true,
1130 vndk: {
1131 enabled: true,
1132 },
1133 nocrt: true,
1134 }
1135
1136 cc_library {
1137 name: "libvndk_ext",
1138 vendor: true,
1139 vndk: {
1140 enabled: true,
1141 extends: "libvndk",
1142 },
1143 nocrt: true,
1144 }
1145
1146 cc_library {
1147 name: "libvndk2",
1148 vendor_available: true,
1149 vndk: {
1150 enabled: true,
1151 },
1152 shared_libs: ["libvndk_ext"],
1153 nocrt: true,
1154 }
1155 `)
1156
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001157 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001158 cc_library {
1159 name: "libvndk",
1160 vendor_available: true,
1161 vndk: {
1162 enabled: true,
1163 },
1164 nocrt: true,
1165 }
1166
1167 cc_library {
1168 name: "libvndk_ext",
1169 vendor: true,
1170 vndk: {
1171 enabled: true,
1172 extends: "libvndk",
1173 },
1174 nocrt: true,
1175 }
1176
1177 cc_library {
1178 name: "libvndk2",
1179 vendor_available: true,
1180 vndk: {
1181 enabled: true,
1182 },
1183 target: {
1184 vendor: {
1185 shared_libs: ["libvndk_ext"],
1186 },
1187 },
1188 nocrt: true,
1189 }
1190 `)
1191
1192 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1193 cc_library {
1194 name: "libvndk_sp",
1195 vendor_available: true,
1196 vndk: {
1197 enabled: true,
1198 support_system_process: true,
1199 },
1200 nocrt: true,
1201 }
1202
1203 cc_library {
1204 name: "libvndk_sp_ext",
1205 vendor: true,
1206 vndk: {
1207 enabled: true,
1208 extends: "libvndk_sp",
1209 support_system_process: true,
1210 },
1211 nocrt: true,
1212 }
1213
1214 cc_library {
1215 name: "libvndk_sp_2",
1216 vendor_available: true,
1217 vndk: {
1218 enabled: true,
1219 support_system_process: true,
1220 },
1221 shared_libs: ["libvndk_sp_ext"],
1222 nocrt: true,
1223 }
1224 `)
1225
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001226 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001227 cc_library {
1228 name: "libvndk_sp",
1229 vendor_available: true,
1230 vndk: {
1231 enabled: true,
1232 },
1233 nocrt: true,
1234 }
1235
1236 cc_library {
1237 name: "libvndk_sp_ext",
1238 vendor: true,
1239 vndk: {
1240 enabled: true,
1241 extends: "libvndk_sp",
1242 },
1243 nocrt: true,
1244 }
1245
1246 cc_library {
1247 name: "libvndk_sp2",
1248 vendor_available: true,
1249 vndk: {
1250 enabled: true,
1251 },
1252 target: {
1253 vendor: {
1254 shared_libs: ["libvndk_sp_ext"],
1255 },
1256 },
1257 nocrt: true,
1258 }
1259 `)
1260}
1261
Colin Cross0af4b842015-04-30 16:36:18 -07001262var (
1263 str11 = "01234567891"
1264 str10 = str11[:10]
1265 str9 = str11[:9]
1266 str5 = str11[:5]
1267 str4 = str11[:4]
1268)
1269
1270var splitListForSizeTestCases = []struct {
1271 in []string
1272 out [][]string
1273 size int
1274}{
1275 {
1276 in: []string{str10},
1277 out: [][]string{{str10}},
1278 size: 10,
1279 },
1280 {
1281 in: []string{str9},
1282 out: [][]string{{str9}},
1283 size: 10,
1284 },
1285 {
1286 in: []string{str5},
1287 out: [][]string{{str5}},
1288 size: 10,
1289 },
1290 {
1291 in: []string{str11},
1292 out: nil,
1293 size: 10,
1294 },
1295 {
1296 in: []string{str10, str10},
1297 out: [][]string{{str10}, {str10}},
1298 size: 10,
1299 },
1300 {
1301 in: []string{str9, str10},
1302 out: [][]string{{str9}, {str10}},
1303 size: 10,
1304 },
1305 {
1306 in: []string{str10, str9},
1307 out: [][]string{{str10}, {str9}},
1308 size: 10,
1309 },
1310 {
1311 in: []string{str5, str4},
1312 out: [][]string{{str5, str4}},
1313 size: 10,
1314 },
1315 {
1316 in: []string{str5, str4, str5},
1317 out: [][]string{{str5, str4}, {str5}},
1318 size: 10,
1319 },
1320 {
1321 in: []string{str5, str4, str5, str4},
1322 out: [][]string{{str5, str4}, {str5, str4}},
1323 size: 10,
1324 },
1325 {
1326 in: []string{str5, str4, str5, str5},
1327 out: [][]string{{str5, str4}, {str5}, {str5}},
1328 size: 10,
1329 },
1330 {
1331 in: []string{str5, str5, str5, str4},
1332 out: [][]string{{str5}, {str5}, {str5, str4}},
1333 size: 10,
1334 },
1335 {
1336 in: []string{str9, str11},
1337 out: nil,
1338 size: 10,
1339 },
1340 {
1341 in: []string{str11, str9},
1342 out: nil,
1343 size: 10,
1344 },
1345}
1346
1347func TestSplitListForSize(t *testing.T) {
1348 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08001349 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07001350
1351 var outStrings [][]string
1352
1353 if len(out) > 0 {
1354 outStrings = make([][]string, len(out))
1355 for i, o := range out {
1356 outStrings[i] = o.Strings()
1357 }
1358 }
1359
1360 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07001361 t.Errorf("incorrect output:")
1362 t.Errorf(" input: %#v", testCase.in)
1363 t.Errorf(" size: %d", testCase.size)
1364 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07001365 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07001366 }
1367 }
1368}
Jeff Gaston294356f2017-09-27 17:05:30 -07001369
1370var staticLinkDepOrderTestCases = []struct {
1371 // This is a string representation of a map[moduleName][]moduleDependency .
1372 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001373 inStatic string
1374
1375 // This is a string representation of a map[moduleName][]moduleDependency .
1376 // It models the dependencies declared in an Android.bp file.
1377 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07001378
1379 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
1380 // The keys of allOrdered specify which modules we would like to check.
1381 // The values of allOrdered specify the expected result (of the transitive closure of all
1382 // dependencies) for each module to test
1383 allOrdered string
1384
1385 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
1386 // The keys of outOrdered specify which modules we would like to check.
1387 // The values of outOrdered specify the expected result (of the ordered linker command line)
1388 // for each module to test.
1389 outOrdered string
1390}{
1391 // Simple tests
1392 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001393 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07001394 outOrdered: "",
1395 },
1396 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001397 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001398 outOrdered: "a:",
1399 },
1400 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001401 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001402 outOrdered: "a:b; b:",
1403 },
1404 // Tests of reordering
1405 {
1406 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001407 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001408 outOrdered: "a:b,c,d; b:d; c:d; d:",
1409 },
1410 {
1411 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001412 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001413 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
1414 },
1415 {
1416 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001417 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07001418 outOrdered: "a:d,b,e,c; d:b; e:c",
1419 },
1420 {
1421 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001422 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07001423 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
1424 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
1425 },
1426 {
1427 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001428 inStatic: "a:b,c,d,e,f,g,h; f:b,c,d; b:c,d; c:d",
Jeff Gaston294356f2017-09-27 17:05:30 -07001429 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
1430 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
1431 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001432 // shared dependencies
1433 {
1434 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
1435 // So, we don't actually have to check that a shared dependency of c will change the order
1436 // of a library that depends statically on b and on c. We only need to check that if c has
1437 // a shared dependency on b, that that shows up in allOrdered.
1438 inShared: "c:b",
1439 allOrdered: "c:b",
1440 outOrdered: "c:",
1441 },
1442 {
1443 // This test doesn't actually include any shared dependencies but it's a reminder of what
1444 // the second phase of the above test would look like
1445 inStatic: "a:b,c; c:b",
1446 allOrdered: "a:c,b; c:b",
1447 outOrdered: "a:c,b; c:b",
1448 },
Jeff Gaston294356f2017-09-27 17:05:30 -07001449 // tiebreakers for when two modules specifying different orderings and there is no dependency
1450 // to dictate an order
1451 {
1452 // if the tie is between two modules at the end of a's deps, then a's order wins
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001453 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07001454 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
1455 },
1456 {
1457 // if the tie is between two modules at the start of a's deps, then c's order is used
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001458 inStatic: "a1:d,e,b1,c1; b1:d,e; c1:e,d; a2:d,e,b2,c2; b2:d,e; c2:d,e",
Jeff Gaston294356f2017-09-27 17:05:30 -07001459 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
1460 },
1461 // Tests involving duplicate dependencies
1462 {
1463 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001464 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001465 outOrdered: "a:c,b",
1466 },
1467 {
1468 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001469 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001470 outOrdered: "a:d,c,b",
1471 },
1472 // Tests to confirm the nonexistence of infinite loops.
1473 // These cases should never happen, so as long as the test terminates and the
1474 // result is deterministic then that should be fine.
1475 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001476 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07001477 outOrdered: "a:a",
1478 },
1479 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001480 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07001481 allOrdered: "a:b,c; b:c,a; c:a,b",
1482 outOrdered: "a:b; b:c; c:a",
1483 },
1484 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001485 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001486 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
1487 outOrdered: "a:c,b; b:a,c; c:b,a",
1488 },
1489}
1490
1491// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
1492func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
1493 // convert from "a:b,c; d:e" to "a:b,c;d:e"
1494 strippedText := strings.Replace(text, " ", "", -1)
1495 if len(strippedText) < 1 {
1496 return []android.Path{}, make(map[android.Path][]android.Path, 0)
1497 }
1498 allDeps = make(map[android.Path][]android.Path, 0)
1499
1500 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
1501 moduleTexts := strings.Split(strippedText, ";")
1502
1503 outputForModuleName := func(moduleName string) android.Path {
1504 return android.PathForTesting(moduleName)
1505 }
1506
1507 for _, moduleText := range moduleTexts {
1508 // convert from "a:b,c" to ["a", "b,c"]
1509 components := strings.Split(moduleText, ":")
1510 if len(components) != 2 {
1511 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
1512 }
1513 moduleName := components[0]
1514 moduleOutput := outputForModuleName(moduleName)
1515 modulesInOrder = append(modulesInOrder, moduleOutput)
1516
1517 depString := components[1]
1518 // convert from "b,c" to ["b", "c"]
1519 depNames := strings.Split(depString, ",")
1520 if len(depString) < 1 {
1521 depNames = []string{}
1522 }
1523 var deps []android.Path
1524 for _, depName := range depNames {
1525 deps = append(deps, outputForModuleName(depName))
1526 }
1527 allDeps[moduleOutput] = deps
1528 }
1529 return modulesInOrder, allDeps
1530}
1531
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001532func TestLinkReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001533 for _, testCase := range staticLinkDepOrderTestCases {
1534 errs := []string{}
1535
1536 // parse testcase
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001537 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)
Jeff Gaston294356f2017-09-27 17:05:30 -07001538 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)
1539 if testCase.allOrdered == "" {
1540 // allow the test case to skip specifying allOrdered
1541 testCase.allOrdered = testCase.outOrdered
1542 }
1543 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001544 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)
Jeff Gaston294356f2017-09-27 17:05:30 -07001545
1546 // For each module whose post-reordered dependencies were specified, validate that
1547 // reordering the inputs produces the expected outputs.
1548 for _, moduleName := range expectedModuleNames {
1549 moduleDeps := givenTransitiveDeps[moduleName]
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001550 givenSharedDeps := givenAllSharedDeps[moduleName]
1551 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -07001552
1553 correctAllOrdered := expectedAllDeps[moduleName]
1554 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {
1555 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001556 "\nin static:%q"+
1557 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07001558 "\nmodule: %v"+
1559 "\nexpected: %s"+
1560 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001561 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07001562 }
1563
1564 correctOutputDeps := expectedTransitiveDeps[moduleName]
1565 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {
1566 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001567 "\nin static:%q"+
1568 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07001569 "\nmodule: %v"+
1570 "\nexpected: %s"+
1571 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001572 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07001573 }
1574 }
1575
1576 if len(errs) > 0 {
1577 sort.Strings(errs)
1578 for _, err := range errs {
1579 t.Error(err)
1580 }
1581 }
1582 }
1583}
Logan Chienf3511742017-10-31 18:04:35 +08001584
Jeff Gaston294356f2017-09-27 17:05:30 -07001585func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
1586 for _, moduleName := range moduleNames {
1587 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
1588 output := module.outputFile.Path()
1589 paths = append(paths, output)
1590 }
1591 return paths
1592}
1593
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001594func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001595 ctx := testCc(t, `
1596 cc_library {
1597 name: "a",
1598 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09001599 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001600 }
1601 cc_library {
1602 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09001603 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001604 }
1605 cc_library {
1606 name: "c",
1607 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09001608 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001609 }
1610 cc_library {
1611 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09001612 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001613 }
1614
1615 `)
1616
1617 variant := "android_arm64_armv8-a_core_static"
1618 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001619 actual := moduleA.depsInLinkOrder
Jeff Gaston294356f2017-09-27 17:05:30 -07001620 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})
1621
1622 if !reflect.DeepEqual(actual, expected) {
1623 t.Errorf("staticDeps orderings were not propagated correctly"+
1624 "\nactual: %v"+
1625 "\nexpected: %v",
1626 actual,
1627 expected,
1628 )
1629 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09001630}
Jeff Gaston294356f2017-09-27 17:05:30 -07001631
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001632func TestStaticLibDepReorderingWithShared(t *testing.T) {
1633 ctx := testCc(t, `
1634 cc_library {
1635 name: "a",
1636 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09001637 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001638 }
1639 cc_library {
1640 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09001641 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001642 }
1643 cc_library {
1644 name: "c",
1645 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09001646 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001647 }
1648
1649 `)
1650
1651 variant := "android_arm64_armv8-a_core_static"
1652 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
1653 actual := moduleA.depsInLinkOrder
1654 expected := getOutputPaths(ctx, variant, []string{"c", "b"})
1655
1656 if !reflect.DeepEqual(actual, expected) {
1657 t.Errorf("staticDeps orderings did not account for shared libs"+
1658 "\nactual: %v"+
1659 "\nexpected: %v",
1660 actual,
1661 expected,
1662 )
1663 }
1664}
1665
Jiyong Parka46a4d52017-12-14 19:54:34 +09001666func TestLlndkHeaders(t *testing.T) {
1667 ctx := testCc(t, `
1668 llndk_headers {
1669 name: "libllndk_headers",
1670 export_include_dirs: ["my_include"],
1671 }
1672 llndk_library {
1673 name: "libllndk",
1674 export_llndk_headers: ["libllndk_headers"],
1675 }
1676 cc_library {
1677 name: "libvendor",
1678 shared_libs: ["libllndk"],
1679 vendor: true,
1680 srcs: ["foo.c"],
Logan Chienf3511742017-10-31 18:04:35 +08001681 no_libgcc: true,
1682 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09001683 }
1684 `)
1685
1686 // _static variant is used since _shared reuses *.o from the static variant
1687 cc := ctx.ModuleForTests("libvendor", "android_arm_armv7-a-neon_vendor_static").Rule("cc")
1688 cflags := cc.Args["cFlags"]
1689 if !strings.Contains(cflags, "-Imy_include") {
1690 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
1691 }
1692}
1693
Logan Chien43d34c32017-12-20 01:17:32 +08001694func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
1695 actual := module.Properties.AndroidMkRuntimeLibs
1696 if !reflect.DeepEqual(actual, expected) {
1697 t.Errorf("incorrect runtime_libs for shared libs"+
1698 "\nactual: %v"+
1699 "\nexpected: %v",
1700 actual,
1701 expected,
1702 )
1703 }
1704}
1705
1706const runtimeLibAndroidBp = `
1707 cc_library {
1708 name: "libvendor_available1",
1709 vendor_available: true,
1710 no_libgcc : true,
1711 nocrt : true,
1712 system_shared_libs : [],
1713 }
1714 cc_library {
1715 name: "libvendor_available2",
1716 vendor_available: true,
1717 runtime_libs: ["libvendor_available1"],
1718 no_libgcc : true,
1719 nocrt : true,
1720 system_shared_libs : [],
1721 }
1722 cc_library {
1723 name: "libvendor_available3",
1724 vendor_available: true,
1725 runtime_libs: ["libvendor_available1"],
1726 target: {
1727 vendor: {
1728 exclude_runtime_libs: ["libvendor_available1"],
1729 }
1730 },
1731 no_libgcc : true,
1732 nocrt : true,
1733 system_shared_libs : [],
1734 }
1735 cc_library {
1736 name: "libcore",
1737 runtime_libs: ["libvendor_available1"],
1738 no_libgcc : true,
1739 nocrt : true,
1740 system_shared_libs : [],
1741 }
1742 cc_library {
1743 name: "libvendor1",
1744 vendor: true,
1745 no_libgcc : true,
1746 nocrt : true,
1747 system_shared_libs : [],
1748 }
1749 cc_library {
1750 name: "libvendor2",
1751 vendor: true,
1752 runtime_libs: ["libvendor_available1", "libvendor1"],
1753 no_libgcc : true,
1754 nocrt : true,
1755 system_shared_libs : [],
1756 }
1757`
1758
1759func TestRuntimeLibs(t *testing.T) {
1760 ctx := testCc(t, runtimeLibAndroidBp)
1761
1762 // runtime_libs for core variants use the module names without suffixes.
1763 variant := "android_arm64_armv8-a_core_shared"
1764
1765 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
1766 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1767
1768 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
1769 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1770
1771 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
1772 // and vendor variants.
1773 variant = "android_arm64_armv8-a_vendor_shared"
1774
1775 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
1776 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
1777
1778 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
1779 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
1780}
1781
1782func TestExcludeRuntimeLibs(t *testing.T) {
1783 ctx := testCc(t, runtimeLibAndroidBp)
1784
1785 variant := "android_arm64_armv8-a_core_shared"
1786 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
1787 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1788
1789 variant = "android_arm64_armv8-a_vendor_shared"
1790 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
1791 checkRuntimeLibs(t, nil, module)
1792}
1793
1794func TestRuntimeLibsNoVndk(t *testing.T) {
1795 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
1796
1797 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
1798
1799 variant := "android_arm64_armv8-a_core_shared"
1800
1801 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
1802 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1803
1804 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
1805 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
1806}
1807
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00001808func checkStaticLibs(t *testing.T, expected []string, module *Module) {
1809 actual := module.Properties.AndroidMkStaticLibs
1810 if !reflect.DeepEqual(actual, expected) {
1811 t.Errorf("incorrect static_libs"+
1812 "\nactual: %v"+
1813 "\nexpected: %v",
1814 actual,
1815 expected,
1816 )
1817 }
1818}
1819
1820const staticLibAndroidBp = `
1821 cc_library {
1822 name: "lib1",
1823 }
1824 cc_library {
1825 name: "lib2",
1826 static_libs: ["lib1"],
1827 }
1828`
1829
1830func TestStaticLibDepExport(t *testing.T) {
1831 ctx := testCc(t, staticLibAndroidBp)
1832
1833 // Check the shared version of lib2.
1834 variant := "android_arm64_armv8-a_core_shared"
1835 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Yi Kongacee27c2019-03-29 20:05:14 -07001836 checkStaticLibs(t, []string{"lib1", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc_stripped"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00001837
1838 // Check the static version of lib2.
1839 variant = "android_arm64_armv8-a_core_static"
1840 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
1841 // libc++_static is linked additionally.
Yi Kongacee27c2019-03-29 20:05:14 -07001842 checkStaticLibs(t, []string{"lib1", "libc++_static", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc_stripped"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00001843}
1844
Jiyong Parkd08b6972017-09-26 10:50:54 +09001845var compilerFlagsTestCases = []struct {
1846 in string
1847 out bool
1848}{
1849 {
1850 in: "a",
1851 out: false,
1852 },
1853 {
1854 in: "-a",
1855 out: true,
1856 },
1857 {
1858 in: "-Ipath/to/something",
1859 out: false,
1860 },
1861 {
1862 in: "-isystempath/to/something",
1863 out: false,
1864 },
1865 {
1866 in: "--coverage",
1867 out: false,
1868 },
1869 {
1870 in: "-include a/b",
1871 out: true,
1872 },
1873 {
1874 in: "-include a/b c/d",
1875 out: false,
1876 },
1877 {
1878 in: "-DMACRO",
1879 out: true,
1880 },
1881 {
1882 in: "-DMAC RO",
1883 out: false,
1884 },
1885 {
1886 in: "-a -b",
1887 out: false,
1888 },
1889 {
1890 in: "-DMACRO=definition",
1891 out: true,
1892 },
1893 {
1894 in: "-DMACRO=defi nition",
1895 out: true, // TODO(jiyong): this should be false
1896 },
1897 {
1898 in: "-DMACRO(x)=x + 1",
1899 out: true,
1900 },
1901 {
1902 in: "-DMACRO=\"defi nition\"",
1903 out: true,
1904 },
1905}
1906
1907type mockContext struct {
1908 BaseModuleContext
1909 result bool
1910}
1911
1912func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1913 // CheckBadCompilerFlags calls this function when the flag should be rejected
1914 ctx.result = false
1915}
1916
1917func TestCompilerFlags(t *testing.T) {
1918 for _, testCase := range compilerFlagsTestCases {
1919 ctx := &mockContext{result: true}
1920 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
1921 if ctx.result != testCase.out {
1922 t.Errorf("incorrect output:")
1923 t.Errorf(" input: %#v", testCase.in)
1924 t.Errorf(" expected: %#v", testCase.out)
1925 t.Errorf(" got: %#v", ctx.result)
1926 }
1927 }
Jeff Gaston294356f2017-09-27 17:05:30 -07001928}
Jiyong Park374510b2018-03-19 18:23:01 +09001929
1930func TestVendorPublicLibraries(t *testing.T) {
1931 ctx := testCc(t, `
1932 cc_library_headers {
1933 name: "libvendorpublic_headers",
1934 export_include_dirs: ["my_include"],
1935 }
1936 vendor_public_library {
1937 name: "libvendorpublic",
1938 symbol_file: "",
1939 export_public_headers: ["libvendorpublic_headers"],
1940 }
1941 cc_library {
1942 name: "libvendorpublic",
1943 srcs: ["foo.c"],
1944 vendor: true,
1945 no_libgcc: true,
1946 nocrt: true,
1947 }
1948
1949 cc_library {
1950 name: "libsystem",
1951 shared_libs: ["libvendorpublic"],
1952 vendor: false,
1953 srcs: ["foo.c"],
1954 no_libgcc: true,
1955 nocrt: true,
1956 }
1957 cc_library {
1958 name: "libvendor",
1959 shared_libs: ["libvendorpublic"],
1960 vendor: true,
1961 srcs: ["foo.c"],
1962 no_libgcc: true,
1963 nocrt: true,
1964 }
1965 `)
1966
1967 variant := "android_arm64_armv8-a_core_shared"
1968
1969 // test if header search paths are correctly added
1970 // _static variant is used since _shared reuses *.o from the static variant
1971 cc := ctx.ModuleForTests("libsystem", strings.Replace(variant, "_shared", "_static", 1)).Rule("cc")
1972 cflags := cc.Args["cFlags"]
1973 if !strings.Contains(cflags, "-Imy_include") {
1974 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
1975 }
1976
1977 // test if libsystem is linked to the stub
1978 ld := ctx.ModuleForTests("libsystem", variant).Rule("ld")
1979 libflags := ld.Args["libFlags"]
1980 stubPaths := getOutputPaths(ctx, variant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
1981 if !strings.Contains(libflags, stubPaths[0].String()) {
1982 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
1983 }
1984
1985 // test if libvendor is linked to the real shared lib
1986 ld = ctx.ModuleForTests("libvendor", strings.Replace(variant, "_core", "_vendor", 1)).Rule("ld")
1987 libflags = ld.Args["libFlags"]
1988 stubPaths = getOutputPaths(ctx, strings.Replace(variant, "_core", "_vendor", 1), []string{"libvendorpublic"})
1989 if !strings.Contains(libflags, stubPaths[0].String()) {
1990 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
1991 }
1992
1993}
Jiyong Park37b25202018-07-11 10:49:27 +09001994
1995func TestRecovery(t *testing.T) {
1996 ctx := testCc(t, `
1997 cc_library_shared {
1998 name: "librecovery",
1999 recovery: true,
2000 }
2001 cc_library_shared {
2002 name: "librecovery32",
2003 recovery: true,
2004 compile_multilib:"32",
2005 }
Jiyong Park5baac542018-08-28 09:55:37 +09002006 cc_library_shared {
2007 name: "libHalInRecovery",
2008 recovery_available: true,
2009 vendor: true,
2010 }
Jiyong Park37b25202018-07-11 10:49:27 +09002011 `)
2012
2013 variants := ctx.ModuleVariantsForTests("librecovery")
2014 const arm64 = "android_arm64_armv8-a_recovery_shared"
2015 if len(variants) != 1 || !android.InList(arm64, variants) {
2016 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
2017 }
2018
2019 variants = ctx.ModuleVariantsForTests("librecovery32")
2020 if android.InList(arm64, variants) {
2021 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
2022 }
Jiyong Park5baac542018-08-28 09:55:37 +09002023
2024 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
2025 if !recoveryModule.Platform() {
2026 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
2027 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09002028}
Jiyong Park5baac542018-08-28 09:55:37 +09002029
Jiyong Park7ed9de32018-10-15 22:25:07 +09002030func TestVersionedStubs(t *testing.T) {
2031 ctx := testCc(t, `
2032 cc_library_shared {
2033 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002034 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002035 stubs: {
2036 symbol_file: "foo.map.txt",
2037 versions: ["1", "2", "3"],
2038 },
2039 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002040
Jiyong Park7ed9de32018-10-15 22:25:07 +09002041 cc_library_shared {
2042 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002043 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002044 shared_libs: ["libFoo#1"],
2045 }`)
2046
2047 variants := ctx.ModuleVariantsForTests("libFoo")
2048 expectedVariants := []string{
2049 "android_arm64_armv8-a_core_shared",
2050 "android_arm64_armv8-a_core_shared_1",
2051 "android_arm64_armv8-a_core_shared_2",
2052 "android_arm64_armv8-a_core_shared_3",
2053 "android_arm_armv7-a-neon_core_shared",
2054 "android_arm_armv7-a-neon_core_shared_1",
2055 "android_arm_armv7-a-neon_core_shared_2",
2056 "android_arm_armv7-a-neon_core_shared_3",
2057 }
2058 variantsMismatch := false
2059 if len(variants) != len(expectedVariants) {
2060 variantsMismatch = true
2061 } else {
2062 for _, v := range expectedVariants {
2063 if !inList(v, variants) {
2064 variantsMismatch = false
2065 }
2066 }
2067 }
2068 if variantsMismatch {
2069 t.Errorf("variants of libFoo expected:\n")
2070 for _, v := range expectedVariants {
2071 t.Errorf("%q\n", v)
2072 }
2073 t.Errorf(", but got:\n")
2074 for _, v := range variants {
2075 t.Errorf("%q\n", v)
2076 }
2077 }
2078
2079 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_core_shared").Rule("ld")
2080 libFlags := libBarLinkRule.Args["libFlags"]
2081 libFoo1StubPath := "libFoo/android_arm64_armv8-a_core_shared_1/libFoo.so"
2082 if !strings.Contains(libFlags, libFoo1StubPath) {
2083 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
2084 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002085
2086 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_core_shared").Rule("cc")
2087 cFlags := libBarCompileRule.Args["cFlags"]
2088 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
2089 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
2090 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
2091 }
Jiyong Park37b25202018-07-11 10:49:27 +09002092}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002093
2094func TestStaticExecutable(t *testing.T) {
2095 ctx := testCc(t, `
2096 cc_binary {
2097 name: "static_test",
2098 srcs: ["foo.c"],
2099 static_executable: true,
2100 }`)
2101
2102 variant := "android_arm64_armv8-a_core"
2103 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
2104 libFlags := binModuleRule.Args["libFlags"]
2105 systemStaticLibs := []string{"libc.a", "libm.a", "libdl.a"}
2106 for _, lib := range systemStaticLibs {
2107 if !strings.Contains(libFlags, lib) {
2108 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
2109 }
2110 }
2111 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
2112 for _, lib := range systemSharedLibs {
2113 if strings.Contains(libFlags, lib) {
2114 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
2115 }
2116 }
2117}
Jiyong Parke4bb9862019-02-01 00:31:10 +09002118
2119func TestStaticDepsOrderWithStubs(t *testing.T) {
2120 ctx := testCc(t, `
2121 cc_binary {
2122 name: "mybin",
2123 srcs: ["foo.c"],
2124 static_libs: ["libB"],
2125 static_executable: true,
2126 stl: "none",
2127 }
2128
2129 cc_library {
2130 name: "libB",
2131 srcs: ["foo.c"],
2132 shared_libs: ["libC"],
2133 stl: "none",
2134 }
2135
2136 cc_library {
2137 name: "libC",
2138 srcs: ["foo.c"],
2139 stl: "none",
2140 stubs: {
2141 versions: ["1"],
2142 },
2143 }`)
2144
2145 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a_core").Module().(*Module)
2146 actual := mybin.depsInLinkOrder
2147 expected := getOutputPaths(ctx, "android_arm64_armv8-a_core_static", []string{"libB", "libC"})
2148
2149 if !reflect.DeepEqual(actual, expected) {
2150 t.Errorf("staticDeps orderings were not propagated correctly"+
2151 "\nactual: %v"+
2152 "\nexpected: %v",
2153 actual,
2154 expected,
2155 )
2156 }
2157}