blob: fba38a29c7961cfd189ae60fc43de8e189b93135 [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
Logan Chienf3511742017-10-31 18:04:35 +080054func createTestContext(t *testing.T, config android.Config, bp string) *android.TestContext {
Jiyong Park6a43f042017-10-12 23:05:00 +090055 ctx := android.NewTestArchContext()
Steven Morelandf9e62162017-11-02 17:00:50 -070056 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(LibraryFactory))
Colin Crossf18e1102017-11-16 14:33:08 -080057 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(LibrarySharedFactory))
Jiyong Park374510b2018-03-19 18:23:01 +090058 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(LibraryHeaderFactory))
Colin Crosse40b4ea2018-10-02 22:25:58 -070059 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(ToolchainLibraryFactory))
Jiyong Park6a43f042017-10-12 23:05:00 +090060 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(llndkLibraryFactory))
Jiyong Parka46a4d52017-12-14 19:54:34 +090061 ctx.RegisterModuleType("llndk_headers", android.ModuleFactoryAdaptor(llndkHeadersFactory))
Jiyong Park374510b2018-03-19 18:23:01 +090062 ctx.RegisterModuleType("vendor_public_library", android.ModuleFactoryAdaptor(vendorPublicLibraryFactory))
Colin Crosse40b4ea2018-10-02 22:25:58 -070063 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(ObjectFactory))
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070064 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Jiyong Park6a43f042017-10-12 23:05:00 +090065 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkf9332f12018-02-01 00:54:12 +090066 ctx.BottomUp("image", imageMutator).Parallel()
Colin Crosse40b4ea2018-10-02 22:25:58 -070067 ctx.BottomUp("link", LinkageMutator).Parallel()
Jiyong Park6a43f042017-10-12 23:05:00 +090068 ctx.BottomUp("vndk", vndkMutator).Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +090069 ctx.BottomUp("version", versionMutator).Parallel()
Colin Crosse40b4ea2018-10-02 22:25:58 -070070 ctx.BottomUp("begin", BeginMutator).Parallel()
Jiyong Park6a43f042017-10-12 23:05:00 +090071 })
72 ctx.Register()
73
Jeff Gaston294356f2017-09-27 17:05:30 -070074 // add some modules that are required by the compiler and/or linker
75 bp = bp + `
76 toolchain_library {
77 name: "libatomic",
78 vendor_available: true,
Jiyong Park37b25202018-07-11 10:49:27 +090079 recovery_available: true,
Dan Willemsenfeea4df2018-10-07 18:16:48 -070080 src: "",
Jeff Gaston294356f2017-09-27 17:05:30 -070081 }
82
83 toolchain_library {
84 name: "libcompiler_rt-extras",
85 vendor_available: true,
Jiyong Park37b25202018-07-11 10:49:27 +090086 recovery_available: true,
Dan Willemsenfeea4df2018-10-07 18:16:48 -070087 src: "",
Jeff Gaston294356f2017-09-27 17:05:30 -070088 }
89
90 toolchain_library {
Yi Kong7df0f302018-10-08 22:10:12 +000091 name: "libclang_rt.builtins-arm-android",
92 vendor_available: true,
93 recovery_available: true,
94 src: "",
95 }
96
97 toolchain_library {
98 name: "libclang_rt.builtins-aarch64-android",
99 vendor_available: true,
100 recovery_available: true,
101 src: "",
102 }
103
104 toolchain_library {
105 name: "libclang_rt.builtins-i686-android",
106 vendor_available: true,
107 recovery_available: true,
108 src: "",
109 }
110
111 toolchain_library {
112 name: "libclang_rt.builtins-x86_64-android",
113 vendor_available: true,
114 recovery_available: true,
115 src: "",
116 }
117
118 toolchain_library {
Jeff Gaston294356f2017-09-27 17:05:30 -0700119 name: "libgcc",
120 vendor_available: true,
Jiyong Park37b25202018-07-11 10:49:27 +0900121 recovery_available: true,
Dan Willemsenfeea4df2018-10-07 18:16:48 -0700122 src: "",
Jeff Gaston294356f2017-09-27 17:05:30 -0700123 }
124
125 cc_library {
126 name: "libc",
Logan Chienf3511742017-10-31 18:04:35 +0800127 no_libgcc: true,
128 nocrt: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700129 system_shared_libs: [],
Jiyong Park37b25202018-07-11 10:49:27 +0900130 recovery_available: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700131 }
132 llndk_library {
133 name: "libc",
134 symbol_file: "",
135 }
136 cc_library {
137 name: "libm",
Logan Chienf3511742017-10-31 18:04:35 +0800138 no_libgcc: true,
139 nocrt: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700140 system_shared_libs: [],
Jiyong Park37b25202018-07-11 10:49:27 +0900141 recovery_available: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700142 }
143 llndk_library {
144 name: "libm",
145 symbol_file: "",
146 }
147 cc_library {
148 name: "libdl",
Logan Chienf3511742017-10-31 18:04:35 +0800149 no_libgcc: true,
150 nocrt: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700151 system_shared_libs: [],
Jiyong Park37b25202018-07-11 10:49:27 +0900152 recovery_available: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700153 }
154 llndk_library {
155 name: "libdl",
156 symbol_file: "",
157 }
Jiyong Park374510b2018-03-19 18:23:01 +0900158 cc_library {
159 name: "libc++_static",
160 no_libgcc: true,
161 nocrt: true,
162 system_shared_libs: [],
163 stl: "none",
164 vendor_available: true,
Jiyong Park37b25202018-07-11 10:49:27 +0900165 recovery_available: true,
Jiyong Park374510b2018-03-19 18:23:01 +0900166 }
167 cc_library {
168 name: "libc++",
169 no_libgcc: true,
170 nocrt: true,
171 system_shared_libs: [],
172 stl: "none",
173 vendor_available: true,
Jiyong Park37b25202018-07-11 10:49:27 +0900174 recovery_available: true,
Jiyong Park374510b2018-03-19 18:23:01 +0900175 vndk: {
176 enabled: true,
177 support_system_process: true,
178 },
179 }
180 cc_library {
181 name: "libunwind_llvm",
182 no_libgcc: true,
183 nocrt: true,
184 system_shared_libs: [],
185 stl: "none",
186 vendor_available: true,
Jiyong Park37b25202018-07-11 10:49:27 +0900187 recovery_available: true,
Jiyong Park374510b2018-03-19 18:23:01 +0900188 }
Jeff Gaston294356f2017-09-27 17:05:30 -0700189
190 cc_object {
191 name: "crtbegin_so",
Jiyong Park37b25202018-07-11 10:49:27 +0900192 recovery_available: true,
Jiyong Park5baac542018-08-28 09:55:37 +0900193 vendor_available: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700194 }
195
196 cc_object {
197 name: "crtend_so",
Jiyong Park37b25202018-07-11 10:49:27 +0900198 recovery_available: true,
Jiyong Park5baac542018-08-28 09:55:37 +0900199 vendor_available: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700200 }
201
Colin Crossad59e752017-11-16 14:29:11 -0800202 cc_library {
203 name: "libprotobuf-cpp-lite",
204 }
205
Jeff Gaston294356f2017-09-27 17:05:30 -0700206`
207
Jiyong Park6a43f042017-10-12 23:05:00 +0900208 ctx.MockFileSystem(map[string][]byte{
Jiyong Park7ed9de32018-10-15 22:25:07 +0900209 "Android.bp": []byte(bp),
210 "foo.c": nil,
211 "bar.c": nil,
212 "a.proto": nil,
213 "b.aidl": nil,
214 "my_include": nil,
215 "foo.map.txt": nil,
Jiyong Park6a43f042017-10-12 23:05:00 +0900216 })
217
Logan Chienf3511742017-10-31 18:04:35 +0800218 return ctx
219}
220
221func testCcWithConfig(t *testing.T, bp string, config android.Config) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800222 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800223 ctx := createTestContext(t, config, bp)
224
Jeff Gastond3e141d2017-08-08 17:46:01 -0700225 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800226 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +0900227 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800228 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +0900229
230 return ctx
231}
232
Logan Chienf3511742017-10-31 18:04:35 +0800233func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800234 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800235 config := android.TestArchConfig(buildDir, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700236 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
237 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800238
239 return testCcWithConfig(t, bp, config)
240}
241
242func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800243 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800244 config := android.TestArchConfig(buildDir, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700245 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800246
247 return testCcWithConfig(t, bp, config)
248}
249
250func testCcError(t *testing.T, pattern string, bp string) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800251 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800252 config := android.TestArchConfig(buildDir, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700253 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
254 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800255
256 ctx := createTestContext(t, config, bp)
257
258 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
259 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +0800260 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800261 return
262 }
263
264 _, errs = ctx.PrepareBuildActions(config)
265 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +0800266 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800267 return
268 }
269
270 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
271}
272
273const (
Jiyong Park5baac542018-08-28 09:55:37 +0900274 coreVariant = "android_arm64_armv8-a_core_shared"
275 vendorVariant = "android_arm64_armv8-a_vendor_shared"
276 recoveryVariant = "android_arm64_armv8-a_recovery_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800277)
278
Jiyong Park6a43f042017-10-12 23:05:00 +0900279func TestVendorSrc(t *testing.T) {
280 ctx := testCc(t, `
281 cc_library {
282 name: "libTest",
283 srcs: ["foo.c"],
Logan Chienf3511742017-10-31 18:04:35 +0800284 no_libgcc: true,
285 nocrt: true,
286 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900287 vendor_available: true,
288 target: {
289 vendor: {
290 srcs: ["bar.c"],
291 },
292 },
293 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900294 `)
295
Logan Chienf3511742017-10-31 18:04:35 +0800296 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900297 var objs []string
298 for _, o := range ld.Inputs {
299 objs = append(objs, o.Base())
300 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800301 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900302 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
303 }
304}
305
Logan Chienf3511742017-10-31 18:04:35 +0800306func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
307 isVndkSp bool, extends string) {
308
Logan Chiend3c59a22018-03-29 14:08:15 +0800309 t.Helper()
310
Logan Chienf3511742017-10-31 18:04:35 +0800311 mod := ctx.ModuleForTests(name, vendorVariant).Module().(*Module)
312 if !mod.hasVendorVariant() {
Colin Crossf46e37f2018-03-21 16:25:58 -0700313 t.Errorf("%q must have vendor variant", name)
Logan Chienf3511742017-10-31 18:04:35 +0800314 }
315
316 // Check library properties.
317 lib, ok := mod.compiler.(*libraryDecorator)
318 if !ok {
319 t.Errorf("%q must have libraryDecorator", name)
320 } else if lib.baseInstaller.subDir != subDir {
321 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
322 lib.baseInstaller.subDir)
323 }
324
325 // Check VNDK properties.
326 if mod.vndkdep == nil {
327 t.Fatalf("%q must have `vndkdep`", name)
328 }
329 if !mod.isVndk() {
330 t.Errorf("%q isVndk() must equal to true", name)
331 }
332 if mod.isVndkSp() != isVndkSp {
333 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
334 }
335
336 // Check VNDK extension properties.
337 isVndkExt := extends != ""
338 if mod.isVndkExt() != isVndkExt {
339 t.Errorf("%q isVndkExt() must equal to %t", name, isVndkExt)
340 }
341
342 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
343 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
344 }
345}
346
347func TestVndk(t *testing.T) {
348 ctx := testCc(t, `
349 cc_library {
350 name: "libvndk",
351 vendor_available: true,
352 vndk: {
353 enabled: true,
354 },
355 nocrt: true,
356 }
357
358 cc_library {
359 name: "libvndk_private",
360 vendor_available: false,
361 vndk: {
362 enabled: true,
363 },
364 nocrt: true,
365 }
366
367 cc_library {
368 name: "libvndk_sp",
369 vendor_available: true,
370 vndk: {
371 enabled: true,
372 support_system_process: true,
373 },
374 nocrt: true,
375 }
376
377 cc_library {
378 name: "libvndk_sp_private",
379 vendor_available: false,
380 vndk: {
381 enabled: true,
382 support_system_process: true,
383 },
384 nocrt: true,
385 }
386 `)
387
388 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "")
389 checkVndkModule(t, ctx, "libvndk_private", "vndk-VER", false, "")
390 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "")
391 checkVndkModule(t, ctx, "libvndk_sp_private", "vndk-sp-VER", true, "")
392}
393
Logan Chiend3c59a22018-03-29 14:08:15 +0800394func TestVndkDepError(t *testing.T) {
395 // Check whether an error is emitted when a VNDK lib depends on a system lib.
396 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
397 cc_library {
398 name: "libvndk",
399 vendor_available: true,
400 vndk: {
401 enabled: true,
402 },
403 shared_libs: ["libfwk"], // Cause error
404 nocrt: true,
405 }
406
407 cc_library {
408 name: "libfwk",
409 nocrt: true,
410 }
411 `)
412
413 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
414 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
415 cc_library {
416 name: "libvndk",
417 vendor_available: true,
418 vndk: {
419 enabled: true,
420 },
421 shared_libs: ["libvendor"], // Cause error
422 nocrt: true,
423 }
424
425 cc_library {
426 name: "libvendor",
427 vendor: true,
428 nocrt: true,
429 }
430 `)
431
432 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
433 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
434 cc_library {
435 name: "libvndk_sp",
436 vendor_available: true,
437 vndk: {
438 enabled: true,
439 support_system_process: true,
440 },
441 shared_libs: ["libfwk"], // Cause error
442 nocrt: true,
443 }
444
445 cc_library {
446 name: "libfwk",
447 nocrt: true,
448 }
449 `)
450
451 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
452 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
453 cc_library {
454 name: "libvndk_sp",
455 vendor_available: true,
456 vndk: {
457 enabled: true,
458 support_system_process: true,
459 },
460 shared_libs: ["libvendor"], // Cause error
461 nocrt: true,
462 }
463
464 cc_library {
465 name: "libvendor",
466 vendor: true,
467 nocrt: true,
468 }
469 `)
470
471 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
472 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
473 cc_library {
474 name: "libvndk_sp",
475 vendor_available: true,
476 vndk: {
477 enabled: true,
478 support_system_process: true,
479 },
480 shared_libs: ["libvndk"], // Cause error
481 nocrt: true,
482 }
483
484 cc_library {
485 name: "libvndk",
486 vendor_available: true,
487 vndk: {
488 enabled: true,
489 },
490 nocrt: true,
491 }
492 `)
493}
494
Logan Chienf3511742017-10-31 18:04:35 +0800495func TestVndkExt(t *testing.T) {
496 // This test checks the VNDK-Ext properties.
497 ctx := testCc(t, `
498 cc_library {
499 name: "libvndk",
500 vendor_available: true,
501 vndk: {
502 enabled: true,
503 },
504 nocrt: true,
505 }
506
507 cc_library {
508 name: "libvndk_ext",
509 vendor: true,
510 vndk: {
511 enabled: true,
512 extends: "libvndk",
513 },
514 nocrt: true,
515 }
516 `)
517
518 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk")
519}
520
Logan Chiend3c59a22018-03-29 14:08:15 +0800521func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +0800522 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
523 ctx := testCcNoVndk(t, `
524 cc_library {
525 name: "libvndk",
526 vendor_available: true,
527 vndk: {
528 enabled: true,
529 },
530 nocrt: true,
531 }
532
533 cc_library {
534 name: "libvndk_ext",
535 vendor: true,
536 vndk: {
537 enabled: true,
538 extends: "libvndk",
539 },
540 nocrt: true,
541 }
542 `)
543
544 // Ensures that the core variant of "libvndk_ext" can be found.
545 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
546 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
547 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
548 }
549}
550
551func TestVndkExtError(t *testing.T) {
552 // This test ensures an error is emitted in ill-formed vndk-ext definition.
553 testCcError(t, "must set `vendor: true` to set `extends: \".*\"`", `
554 cc_library {
555 name: "libvndk",
556 vendor_available: true,
557 vndk: {
558 enabled: true,
559 },
560 nocrt: true,
561 }
562
563 cc_library {
564 name: "libvndk_ext",
565 vndk: {
566 enabled: true,
567 extends: "libvndk",
568 },
569 nocrt: true,
570 }
571 `)
572
573 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
574 cc_library {
575 name: "libvndk",
576 vendor_available: true,
577 vndk: {
578 enabled: true,
579 },
580 nocrt: true,
581 }
582
583 cc_library {
584 name: "libvndk_ext",
585 vendor: true,
586 vndk: {
587 enabled: true,
588 },
589 nocrt: true,
590 }
591 `)
592}
593
594func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
595 // This test ensures an error is emitted for inconsistent support_system_process.
596 testCcError(t, "module \".*\" with mismatched support_system_process", `
597 cc_library {
598 name: "libvndk",
599 vendor_available: true,
600 vndk: {
601 enabled: true,
602 },
603 nocrt: true,
604 }
605
606 cc_library {
607 name: "libvndk_sp_ext",
608 vendor: true,
609 vndk: {
610 enabled: true,
611 extends: "libvndk",
612 support_system_process: true,
613 },
614 nocrt: true,
615 }
616 `)
617
618 testCcError(t, "module \".*\" with mismatched support_system_process", `
619 cc_library {
620 name: "libvndk_sp",
621 vendor_available: true,
622 vndk: {
623 enabled: true,
624 support_system_process: true,
625 },
626 nocrt: true,
627 }
628
629 cc_library {
630 name: "libvndk_ext",
631 vendor: true,
632 vndk: {
633 enabled: true,
634 extends: "libvndk_sp",
635 },
636 nocrt: true,
637 }
638 `)
639}
640
641func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800642 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +0800643 // with `vendor_available: false`.
644 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
645 cc_library {
646 name: "libvndk",
647 vendor_available: false,
648 vndk: {
649 enabled: true,
650 },
651 nocrt: true,
652 }
653
654 cc_library {
655 name: "libvndk_ext",
656 vendor: true,
657 vndk: {
658 enabled: true,
659 extends: "libvndk",
660 },
661 nocrt: true,
662 }
663 `)
664}
665
Logan Chiend3c59a22018-03-29 14:08:15 +0800666func TestVendorModuleUseVndkExt(t *testing.T) {
667 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +0800668 testCc(t, `
669 cc_library {
670 name: "libvndk",
671 vendor_available: true,
672 vndk: {
673 enabled: true,
674 },
675 nocrt: true,
676 }
677
678 cc_library {
679 name: "libvndk_ext",
680 vendor: true,
681 vndk: {
682 enabled: true,
683 extends: "libvndk",
684 },
685 nocrt: true,
686 }
687
688 cc_library {
689
690 name: "libvndk_sp",
691 vendor_available: true,
692 vndk: {
693 enabled: true,
694 support_system_process: true,
695 },
696 nocrt: true,
697 }
698
699 cc_library {
700 name: "libvndk_sp_ext",
701 vendor: true,
702 vndk: {
703 enabled: true,
704 extends: "libvndk_sp",
705 support_system_process: true,
706 },
707 nocrt: true,
708 }
709
710 cc_library {
711 name: "libvendor",
712 vendor: true,
713 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
714 nocrt: true,
715 }
716 `)
717}
718
Logan Chiend3c59a22018-03-29 14:08:15 +0800719func TestVndkExtUseVendorLib(t *testing.T) {
720 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +0800721 testCc(t, `
722 cc_library {
723 name: "libvndk",
724 vendor_available: true,
725 vndk: {
726 enabled: true,
727 },
728 nocrt: true,
729 }
730
731 cc_library {
732 name: "libvndk_ext",
733 vendor: true,
734 vndk: {
735 enabled: true,
736 extends: "libvndk",
737 },
738 shared_libs: ["libvendor"],
739 nocrt: true,
740 }
741
742 cc_library {
743 name: "libvendor",
744 vendor: true,
745 nocrt: true,
746 }
747 `)
Logan Chienf3511742017-10-31 18:04:35 +0800748
Logan Chiend3c59a22018-03-29 14:08:15 +0800749 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
750 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +0800751 cc_library {
752 name: "libvndk_sp",
753 vendor_available: true,
754 vndk: {
755 enabled: true,
756 support_system_process: true,
757 },
758 nocrt: true,
759 }
760
761 cc_library {
762 name: "libvndk_sp_ext",
763 vendor: true,
764 vndk: {
765 enabled: true,
766 extends: "libvndk_sp",
767 support_system_process: true,
768 },
769 shared_libs: ["libvendor"], // Cause an error
770 nocrt: true,
771 }
772
773 cc_library {
774 name: "libvendor",
775 vendor: true,
776 nocrt: true,
777 }
778 `)
779}
780
Logan Chiend3c59a22018-03-29 14:08:15 +0800781func TestVndkSpExtUseVndkError(t *testing.T) {
782 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
783 // library.
784 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
785 cc_library {
786 name: "libvndk",
787 vendor_available: true,
788 vndk: {
789 enabled: true,
790 },
791 nocrt: true,
792 }
793
794 cc_library {
795 name: "libvndk_sp",
796 vendor_available: true,
797 vndk: {
798 enabled: true,
799 support_system_process: true,
800 },
801 nocrt: true,
802 }
803
804 cc_library {
805 name: "libvndk_sp_ext",
806 vendor: true,
807 vndk: {
808 enabled: true,
809 extends: "libvndk_sp",
810 support_system_process: true,
811 },
812 shared_libs: ["libvndk"], // Cause an error
813 nocrt: true,
814 }
815 `)
816
817 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
818 // library.
819 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
820 cc_library {
821 name: "libvndk",
822 vendor_available: true,
823 vndk: {
824 enabled: true,
825 },
826 nocrt: true,
827 }
828
829 cc_library {
830 name: "libvndk_ext",
831 vendor: true,
832 vndk: {
833 enabled: true,
834 extends: "libvndk",
835 },
836 nocrt: true,
837 }
838
839 cc_library {
840 name: "libvndk_sp",
841 vendor_available: true,
842 vndk: {
843 enabled: true,
844 support_system_process: true,
845 },
846 nocrt: true,
847 }
848
849 cc_library {
850 name: "libvndk_sp_ext",
851 vendor: true,
852 vndk: {
853 enabled: true,
854 extends: "libvndk_sp",
855 support_system_process: true,
856 },
857 shared_libs: ["libvndk_ext"], // Cause an error
858 nocrt: true,
859 }
860 `)
861}
862
863func TestVndkUseVndkExtError(t *testing.T) {
864 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
865 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +0800866 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
867 cc_library {
868 name: "libvndk",
869 vendor_available: true,
870 vndk: {
871 enabled: true,
872 },
873 nocrt: true,
874 }
875
876 cc_library {
877 name: "libvndk_ext",
878 vendor: true,
879 vndk: {
880 enabled: true,
881 extends: "libvndk",
882 },
883 nocrt: true,
884 }
885
886 cc_library {
887 name: "libvndk2",
888 vendor_available: true,
889 vndk: {
890 enabled: true,
891 },
892 shared_libs: ["libvndk_ext"],
893 nocrt: true,
894 }
895 `)
896
897 // The pattern should be "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\""
898 // but target.vendor.shared_libs has not been supported yet.
899 testCcError(t, "unrecognized property \"target.vendor.shared_libs\"", `
900 cc_library {
901 name: "libvndk",
902 vendor_available: true,
903 vndk: {
904 enabled: true,
905 },
906 nocrt: true,
907 }
908
909 cc_library {
910 name: "libvndk_ext",
911 vendor: true,
912 vndk: {
913 enabled: true,
914 extends: "libvndk",
915 },
916 nocrt: true,
917 }
918
919 cc_library {
920 name: "libvndk2",
921 vendor_available: true,
922 vndk: {
923 enabled: true,
924 },
925 target: {
926 vendor: {
927 shared_libs: ["libvndk_ext"],
928 },
929 },
930 nocrt: true,
931 }
932 `)
933
934 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
935 cc_library {
936 name: "libvndk_sp",
937 vendor_available: true,
938 vndk: {
939 enabled: true,
940 support_system_process: true,
941 },
942 nocrt: true,
943 }
944
945 cc_library {
946 name: "libvndk_sp_ext",
947 vendor: true,
948 vndk: {
949 enabled: true,
950 extends: "libvndk_sp",
951 support_system_process: true,
952 },
953 nocrt: true,
954 }
955
956 cc_library {
957 name: "libvndk_sp_2",
958 vendor_available: true,
959 vndk: {
960 enabled: true,
961 support_system_process: true,
962 },
963 shared_libs: ["libvndk_sp_ext"],
964 nocrt: true,
965 }
966 `)
967
968 // The pattern should be "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\""
969 // but target.vendor.shared_libs has not been supported yet.
970 testCcError(t, "unrecognized property \"target.vendor.shared_libs\"", `
971 cc_library {
972 name: "libvndk_sp",
973 vendor_available: true,
974 vndk: {
975 enabled: true,
976 },
977 nocrt: true,
978 }
979
980 cc_library {
981 name: "libvndk_sp_ext",
982 vendor: true,
983 vndk: {
984 enabled: true,
985 extends: "libvndk_sp",
986 },
987 nocrt: true,
988 }
989
990 cc_library {
991 name: "libvndk_sp2",
992 vendor_available: true,
993 vndk: {
994 enabled: true,
995 },
996 target: {
997 vendor: {
998 shared_libs: ["libvndk_sp_ext"],
999 },
1000 },
1001 nocrt: true,
1002 }
1003 `)
1004}
1005
Colin Cross0af4b842015-04-30 16:36:18 -07001006var (
1007 str11 = "01234567891"
1008 str10 = str11[:10]
1009 str9 = str11[:9]
1010 str5 = str11[:5]
1011 str4 = str11[:4]
1012)
1013
1014var splitListForSizeTestCases = []struct {
1015 in []string
1016 out [][]string
1017 size int
1018}{
1019 {
1020 in: []string{str10},
1021 out: [][]string{{str10}},
1022 size: 10,
1023 },
1024 {
1025 in: []string{str9},
1026 out: [][]string{{str9}},
1027 size: 10,
1028 },
1029 {
1030 in: []string{str5},
1031 out: [][]string{{str5}},
1032 size: 10,
1033 },
1034 {
1035 in: []string{str11},
1036 out: nil,
1037 size: 10,
1038 },
1039 {
1040 in: []string{str10, str10},
1041 out: [][]string{{str10}, {str10}},
1042 size: 10,
1043 },
1044 {
1045 in: []string{str9, str10},
1046 out: [][]string{{str9}, {str10}},
1047 size: 10,
1048 },
1049 {
1050 in: []string{str10, str9},
1051 out: [][]string{{str10}, {str9}},
1052 size: 10,
1053 },
1054 {
1055 in: []string{str5, str4},
1056 out: [][]string{{str5, str4}},
1057 size: 10,
1058 },
1059 {
1060 in: []string{str5, str4, str5},
1061 out: [][]string{{str5, str4}, {str5}},
1062 size: 10,
1063 },
1064 {
1065 in: []string{str5, str4, str5, str4},
1066 out: [][]string{{str5, str4}, {str5, str4}},
1067 size: 10,
1068 },
1069 {
1070 in: []string{str5, str4, str5, str5},
1071 out: [][]string{{str5, str4}, {str5}, {str5}},
1072 size: 10,
1073 },
1074 {
1075 in: []string{str5, str5, str5, str4},
1076 out: [][]string{{str5}, {str5}, {str5, str4}},
1077 size: 10,
1078 },
1079 {
1080 in: []string{str9, str11},
1081 out: nil,
1082 size: 10,
1083 },
1084 {
1085 in: []string{str11, str9},
1086 out: nil,
1087 size: 10,
1088 },
1089}
1090
1091func TestSplitListForSize(t *testing.T) {
1092 for _, testCase := range splitListForSizeTestCases {
Colin Cross5b529592017-05-09 13:34:34 -07001093 out, _ := splitListForSize(android.PathsForTesting(testCase.in), testCase.size)
1094
1095 var outStrings [][]string
1096
1097 if len(out) > 0 {
1098 outStrings = make([][]string, len(out))
1099 for i, o := range out {
1100 outStrings[i] = o.Strings()
1101 }
1102 }
1103
1104 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07001105 t.Errorf("incorrect output:")
1106 t.Errorf(" input: %#v", testCase.in)
1107 t.Errorf(" size: %d", testCase.size)
1108 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07001109 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07001110 }
1111 }
1112}
Jeff Gaston294356f2017-09-27 17:05:30 -07001113
1114var staticLinkDepOrderTestCases = []struct {
1115 // This is a string representation of a map[moduleName][]moduleDependency .
1116 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001117 inStatic string
1118
1119 // This is a string representation of a map[moduleName][]moduleDependency .
1120 // It models the dependencies declared in an Android.bp file.
1121 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07001122
1123 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
1124 // The keys of allOrdered specify which modules we would like to check.
1125 // The values of allOrdered specify the expected result (of the transitive closure of all
1126 // dependencies) for each module to test
1127 allOrdered string
1128
1129 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
1130 // The keys of outOrdered specify which modules we would like to check.
1131 // The values of outOrdered specify the expected result (of the ordered linker command line)
1132 // for each module to test.
1133 outOrdered string
1134}{
1135 // Simple tests
1136 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001137 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07001138 outOrdered: "",
1139 },
1140 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001141 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001142 outOrdered: "a:",
1143 },
1144 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001145 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001146 outOrdered: "a:b; b:",
1147 },
1148 // Tests of reordering
1149 {
1150 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001151 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001152 outOrdered: "a:b,c,d; b:d; c:d; d:",
1153 },
1154 {
1155 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001156 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001157 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
1158 },
1159 {
1160 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001161 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07001162 outOrdered: "a:d,b,e,c; d:b; e:c",
1163 },
1164 {
1165 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001166 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07001167 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
1168 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
1169 },
1170 {
1171 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001172 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 -07001173 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
1174 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
1175 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001176 // shared dependencies
1177 {
1178 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
1179 // So, we don't actually have to check that a shared dependency of c will change the order
1180 // of a library that depends statically on b and on c. We only need to check that if c has
1181 // a shared dependency on b, that that shows up in allOrdered.
1182 inShared: "c:b",
1183 allOrdered: "c:b",
1184 outOrdered: "c:",
1185 },
1186 {
1187 // This test doesn't actually include any shared dependencies but it's a reminder of what
1188 // the second phase of the above test would look like
1189 inStatic: "a:b,c; c:b",
1190 allOrdered: "a:c,b; c:b",
1191 outOrdered: "a:c,b; c:b",
1192 },
Jeff Gaston294356f2017-09-27 17:05:30 -07001193 // tiebreakers for when two modules specifying different orderings and there is no dependency
1194 // to dictate an order
1195 {
1196 // 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 -08001197 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07001198 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
1199 },
1200 {
1201 // 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 -08001202 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 -07001203 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
1204 },
1205 // Tests involving duplicate dependencies
1206 {
1207 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001208 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001209 outOrdered: "a:c,b",
1210 },
1211 {
1212 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001213 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001214 outOrdered: "a:d,c,b",
1215 },
1216 // Tests to confirm the nonexistence of infinite loops.
1217 // These cases should never happen, so as long as the test terminates and the
1218 // result is deterministic then that should be fine.
1219 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001220 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07001221 outOrdered: "a:a",
1222 },
1223 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001224 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07001225 allOrdered: "a:b,c; b:c,a; c:a,b",
1226 outOrdered: "a:b; b:c; c:a",
1227 },
1228 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001229 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001230 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
1231 outOrdered: "a:c,b; b:a,c; c:b,a",
1232 },
1233}
1234
1235// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
1236func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
1237 // convert from "a:b,c; d:e" to "a:b,c;d:e"
1238 strippedText := strings.Replace(text, " ", "", -1)
1239 if len(strippedText) < 1 {
1240 return []android.Path{}, make(map[android.Path][]android.Path, 0)
1241 }
1242 allDeps = make(map[android.Path][]android.Path, 0)
1243
1244 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
1245 moduleTexts := strings.Split(strippedText, ";")
1246
1247 outputForModuleName := func(moduleName string) android.Path {
1248 return android.PathForTesting(moduleName)
1249 }
1250
1251 for _, moduleText := range moduleTexts {
1252 // convert from "a:b,c" to ["a", "b,c"]
1253 components := strings.Split(moduleText, ":")
1254 if len(components) != 2 {
1255 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
1256 }
1257 moduleName := components[0]
1258 moduleOutput := outputForModuleName(moduleName)
1259 modulesInOrder = append(modulesInOrder, moduleOutput)
1260
1261 depString := components[1]
1262 // convert from "b,c" to ["b", "c"]
1263 depNames := strings.Split(depString, ",")
1264 if len(depString) < 1 {
1265 depNames = []string{}
1266 }
1267 var deps []android.Path
1268 for _, depName := range depNames {
1269 deps = append(deps, outputForModuleName(depName))
1270 }
1271 allDeps[moduleOutput] = deps
1272 }
1273 return modulesInOrder, allDeps
1274}
1275
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001276func TestLinkReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001277 for _, testCase := range staticLinkDepOrderTestCases {
1278 errs := []string{}
1279
1280 // parse testcase
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001281 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)
Jeff Gaston294356f2017-09-27 17:05:30 -07001282 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)
1283 if testCase.allOrdered == "" {
1284 // allow the test case to skip specifying allOrdered
1285 testCase.allOrdered = testCase.outOrdered
1286 }
1287 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001288 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)
Jeff Gaston294356f2017-09-27 17:05:30 -07001289
1290 // For each module whose post-reordered dependencies were specified, validate that
1291 // reordering the inputs produces the expected outputs.
1292 for _, moduleName := range expectedModuleNames {
1293 moduleDeps := givenTransitiveDeps[moduleName]
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001294 givenSharedDeps := givenAllSharedDeps[moduleName]
1295 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -07001296
1297 correctAllOrdered := expectedAllDeps[moduleName]
1298 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {
1299 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001300 "\nin static:%q"+
1301 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07001302 "\nmodule: %v"+
1303 "\nexpected: %s"+
1304 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001305 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07001306 }
1307
1308 correctOutputDeps := expectedTransitiveDeps[moduleName]
1309 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {
1310 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001311 "\nin static:%q"+
1312 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07001313 "\nmodule: %v"+
1314 "\nexpected: %s"+
1315 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001316 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07001317 }
1318 }
1319
1320 if len(errs) > 0 {
1321 sort.Strings(errs)
1322 for _, err := range errs {
1323 t.Error(err)
1324 }
1325 }
1326 }
1327}
Logan Chienf3511742017-10-31 18:04:35 +08001328
Jeff Gaston294356f2017-09-27 17:05:30 -07001329func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
1330 for _, moduleName := range moduleNames {
1331 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
1332 output := module.outputFile.Path()
1333 paths = append(paths, output)
1334 }
1335 return paths
1336}
1337
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001338func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001339 ctx := testCc(t, `
1340 cc_library {
1341 name: "a",
1342 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09001343 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001344 }
1345 cc_library {
1346 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09001347 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001348 }
1349 cc_library {
1350 name: "c",
1351 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09001352 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001353 }
1354 cc_library {
1355 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09001356 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001357 }
1358
1359 `)
1360
1361 variant := "android_arm64_armv8-a_core_static"
1362 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001363 actual := moduleA.depsInLinkOrder
Jeff Gaston294356f2017-09-27 17:05:30 -07001364 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})
1365
1366 if !reflect.DeepEqual(actual, expected) {
1367 t.Errorf("staticDeps orderings were not propagated correctly"+
1368 "\nactual: %v"+
1369 "\nexpected: %v",
1370 actual,
1371 expected,
1372 )
1373 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09001374}
Jeff Gaston294356f2017-09-27 17:05:30 -07001375
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001376func TestStaticLibDepReorderingWithShared(t *testing.T) {
1377 ctx := testCc(t, `
1378 cc_library {
1379 name: "a",
1380 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09001381 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001382 }
1383 cc_library {
1384 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09001385 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001386 }
1387 cc_library {
1388 name: "c",
1389 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09001390 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001391 }
1392
1393 `)
1394
1395 variant := "android_arm64_armv8-a_core_static"
1396 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
1397 actual := moduleA.depsInLinkOrder
1398 expected := getOutputPaths(ctx, variant, []string{"c", "b"})
1399
1400 if !reflect.DeepEqual(actual, expected) {
1401 t.Errorf("staticDeps orderings did not account for shared libs"+
1402 "\nactual: %v"+
1403 "\nexpected: %v",
1404 actual,
1405 expected,
1406 )
1407 }
1408}
1409
Jiyong Parka46a4d52017-12-14 19:54:34 +09001410func TestLlndkHeaders(t *testing.T) {
1411 ctx := testCc(t, `
1412 llndk_headers {
1413 name: "libllndk_headers",
1414 export_include_dirs: ["my_include"],
1415 }
1416 llndk_library {
1417 name: "libllndk",
1418 export_llndk_headers: ["libllndk_headers"],
1419 }
1420 cc_library {
1421 name: "libvendor",
1422 shared_libs: ["libllndk"],
1423 vendor: true,
1424 srcs: ["foo.c"],
Logan Chienf3511742017-10-31 18:04:35 +08001425 no_libgcc: true,
1426 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09001427 }
1428 `)
1429
1430 // _static variant is used since _shared reuses *.o from the static variant
1431 cc := ctx.ModuleForTests("libvendor", "android_arm_armv7-a-neon_vendor_static").Rule("cc")
1432 cflags := cc.Args["cFlags"]
1433 if !strings.Contains(cflags, "-Imy_include") {
1434 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
1435 }
1436}
1437
Logan Chien43d34c32017-12-20 01:17:32 +08001438func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
1439 actual := module.Properties.AndroidMkRuntimeLibs
1440 if !reflect.DeepEqual(actual, expected) {
1441 t.Errorf("incorrect runtime_libs for shared libs"+
1442 "\nactual: %v"+
1443 "\nexpected: %v",
1444 actual,
1445 expected,
1446 )
1447 }
1448}
1449
1450const runtimeLibAndroidBp = `
1451 cc_library {
1452 name: "libvendor_available1",
1453 vendor_available: true,
1454 no_libgcc : true,
1455 nocrt : true,
1456 system_shared_libs : [],
1457 }
1458 cc_library {
1459 name: "libvendor_available2",
1460 vendor_available: true,
1461 runtime_libs: ["libvendor_available1"],
1462 no_libgcc : true,
1463 nocrt : true,
1464 system_shared_libs : [],
1465 }
1466 cc_library {
1467 name: "libvendor_available3",
1468 vendor_available: true,
1469 runtime_libs: ["libvendor_available1"],
1470 target: {
1471 vendor: {
1472 exclude_runtime_libs: ["libvendor_available1"],
1473 }
1474 },
1475 no_libgcc : true,
1476 nocrt : true,
1477 system_shared_libs : [],
1478 }
1479 cc_library {
1480 name: "libcore",
1481 runtime_libs: ["libvendor_available1"],
1482 no_libgcc : true,
1483 nocrt : true,
1484 system_shared_libs : [],
1485 }
1486 cc_library {
1487 name: "libvendor1",
1488 vendor: true,
1489 no_libgcc : true,
1490 nocrt : true,
1491 system_shared_libs : [],
1492 }
1493 cc_library {
1494 name: "libvendor2",
1495 vendor: true,
1496 runtime_libs: ["libvendor_available1", "libvendor1"],
1497 no_libgcc : true,
1498 nocrt : true,
1499 system_shared_libs : [],
1500 }
1501`
1502
1503func TestRuntimeLibs(t *testing.T) {
1504 ctx := testCc(t, runtimeLibAndroidBp)
1505
1506 // runtime_libs for core variants use the module names without suffixes.
1507 variant := "android_arm64_armv8-a_core_shared"
1508
1509 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
1510 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1511
1512 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
1513 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1514
1515 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
1516 // and vendor variants.
1517 variant = "android_arm64_armv8-a_vendor_shared"
1518
1519 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
1520 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
1521
1522 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
1523 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
1524}
1525
1526func TestExcludeRuntimeLibs(t *testing.T) {
1527 ctx := testCc(t, runtimeLibAndroidBp)
1528
1529 variant := "android_arm64_armv8-a_core_shared"
1530 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
1531 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1532
1533 variant = "android_arm64_armv8-a_vendor_shared"
1534 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
1535 checkRuntimeLibs(t, nil, module)
1536}
1537
1538func TestRuntimeLibsNoVndk(t *testing.T) {
1539 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
1540
1541 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
1542
1543 variant := "android_arm64_armv8-a_core_shared"
1544
1545 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
1546 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1547
1548 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
1549 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
1550}
1551
Jiyong Parkd08b6972017-09-26 10:50:54 +09001552var compilerFlagsTestCases = []struct {
1553 in string
1554 out bool
1555}{
1556 {
1557 in: "a",
1558 out: false,
1559 },
1560 {
1561 in: "-a",
1562 out: true,
1563 },
1564 {
1565 in: "-Ipath/to/something",
1566 out: false,
1567 },
1568 {
1569 in: "-isystempath/to/something",
1570 out: false,
1571 },
1572 {
1573 in: "--coverage",
1574 out: false,
1575 },
1576 {
1577 in: "-include a/b",
1578 out: true,
1579 },
1580 {
1581 in: "-include a/b c/d",
1582 out: false,
1583 },
1584 {
1585 in: "-DMACRO",
1586 out: true,
1587 },
1588 {
1589 in: "-DMAC RO",
1590 out: false,
1591 },
1592 {
1593 in: "-a -b",
1594 out: false,
1595 },
1596 {
1597 in: "-DMACRO=definition",
1598 out: true,
1599 },
1600 {
1601 in: "-DMACRO=defi nition",
1602 out: true, // TODO(jiyong): this should be false
1603 },
1604 {
1605 in: "-DMACRO(x)=x + 1",
1606 out: true,
1607 },
1608 {
1609 in: "-DMACRO=\"defi nition\"",
1610 out: true,
1611 },
1612}
1613
1614type mockContext struct {
1615 BaseModuleContext
1616 result bool
1617}
1618
1619func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1620 // CheckBadCompilerFlags calls this function when the flag should be rejected
1621 ctx.result = false
1622}
1623
1624func TestCompilerFlags(t *testing.T) {
1625 for _, testCase := range compilerFlagsTestCases {
1626 ctx := &mockContext{result: true}
1627 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
1628 if ctx.result != testCase.out {
1629 t.Errorf("incorrect output:")
1630 t.Errorf(" input: %#v", testCase.in)
1631 t.Errorf(" expected: %#v", testCase.out)
1632 t.Errorf(" got: %#v", ctx.result)
1633 }
1634 }
Jeff Gaston294356f2017-09-27 17:05:30 -07001635}
Jiyong Park374510b2018-03-19 18:23:01 +09001636
1637func TestVendorPublicLibraries(t *testing.T) {
1638 ctx := testCc(t, `
1639 cc_library_headers {
1640 name: "libvendorpublic_headers",
1641 export_include_dirs: ["my_include"],
1642 }
1643 vendor_public_library {
1644 name: "libvendorpublic",
1645 symbol_file: "",
1646 export_public_headers: ["libvendorpublic_headers"],
1647 }
1648 cc_library {
1649 name: "libvendorpublic",
1650 srcs: ["foo.c"],
1651 vendor: true,
1652 no_libgcc: true,
1653 nocrt: true,
1654 }
1655
1656 cc_library {
1657 name: "libsystem",
1658 shared_libs: ["libvendorpublic"],
1659 vendor: false,
1660 srcs: ["foo.c"],
1661 no_libgcc: true,
1662 nocrt: true,
1663 }
1664 cc_library {
1665 name: "libvendor",
1666 shared_libs: ["libvendorpublic"],
1667 vendor: true,
1668 srcs: ["foo.c"],
1669 no_libgcc: true,
1670 nocrt: true,
1671 }
1672 `)
1673
1674 variant := "android_arm64_armv8-a_core_shared"
1675
1676 // test if header search paths are correctly added
1677 // _static variant is used since _shared reuses *.o from the static variant
1678 cc := ctx.ModuleForTests("libsystem", strings.Replace(variant, "_shared", "_static", 1)).Rule("cc")
1679 cflags := cc.Args["cFlags"]
1680 if !strings.Contains(cflags, "-Imy_include") {
1681 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
1682 }
1683
1684 // test if libsystem is linked to the stub
1685 ld := ctx.ModuleForTests("libsystem", variant).Rule("ld")
1686 libflags := ld.Args["libFlags"]
1687 stubPaths := getOutputPaths(ctx, variant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
1688 if !strings.Contains(libflags, stubPaths[0].String()) {
1689 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
1690 }
1691
1692 // test if libvendor is linked to the real shared lib
1693 ld = ctx.ModuleForTests("libvendor", strings.Replace(variant, "_core", "_vendor", 1)).Rule("ld")
1694 libflags = ld.Args["libFlags"]
1695 stubPaths = getOutputPaths(ctx, strings.Replace(variant, "_core", "_vendor", 1), []string{"libvendorpublic"})
1696 if !strings.Contains(libflags, stubPaths[0].String()) {
1697 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
1698 }
1699
1700}
Jiyong Park37b25202018-07-11 10:49:27 +09001701
1702func TestRecovery(t *testing.T) {
1703 ctx := testCc(t, `
1704 cc_library_shared {
1705 name: "librecovery",
1706 recovery: true,
1707 }
1708 cc_library_shared {
1709 name: "librecovery32",
1710 recovery: true,
1711 compile_multilib:"32",
1712 }
Jiyong Park5baac542018-08-28 09:55:37 +09001713 cc_library_shared {
1714 name: "libHalInRecovery",
1715 recovery_available: true,
1716 vendor: true,
1717 }
Jiyong Park37b25202018-07-11 10:49:27 +09001718 `)
1719
1720 variants := ctx.ModuleVariantsForTests("librecovery")
1721 const arm64 = "android_arm64_armv8-a_recovery_shared"
1722 if len(variants) != 1 || !android.InList(arm64, variants) {
1723 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
1724 }
1725
1726 variants = ctx.ModuleVariantsForTests("librecovery32")
1727 if android.InList(arm64, variants) {
1728 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
1729 }
Jiyong Park5baac542018-08-28 09:55:37 +09001730
1731 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
1732 if !recoveryModule.Platform() {
1733 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
1734 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001735}
Jiyong Park5baac542018-08-28 09:55:37 +09001736
Jiyong Park7ed9de32018-10-15 22:25:07 +09001737func TestVersionedStubs(t *testing.T) {
1738 ctx := testCc(t, `
1739 cc_library_shared {
1740 name: "libFoo",
1741 stubs: {
1742 symbol_file: "foo.map.txt",
1743 versions: ["1", "2", "3"],
1744 },
1745 }
1746 cc_library_shared {
1747 name: "libBar",
1748 shared_libs: ["libFoo#1"],
1749 }`)
1750
1751 variants := ctx.ModuleVariantsForTests("libFoo")
1752 expectedVariants := []string{
1753 "android_arm64_armv8-a_core_shared",
1754 "android_arm64_armv8-a_core_shared_1",
1755 "android_arm64_armv8-a_core_shared_2",
1756 "android_arm64_armv8-a_core_shared_3",
1757 "android_arm_armv7-a-neon_core_shared",
1758 "android_arm_armv7-a-neon_core_shared_1",
1759 "android_arm_armv7-a-neon_core_shared_2",
1760 "android_arm_armv7-a-neon_core_shared_3",
1761 }
1762 variantsMismatch := false
1763 if len(variants) != len(expectedVariants) {
1764 variantsMismatch = true
1765 } else {
1766 for _, v := range expectedVariants {
1767 if !inList(v, variants) {
1768 variantsMismatch = false
1769 }
1770 }
1771 }
1772 if variantsMismatch {
1773 t.Errorf("variants of libFoo expected:\n")
1774 for _, v := range expectedVariants {
1775 t.Errorf("%q\n", v)
1776 }
1777 t.Errorf(", but got:\n")
1778 for _, v := range variants {
1779 t.Errorf("%q\n", v)
1780 }
1781 }
1782
1783 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_core_shared").Rule("ld")
1784 libFlags := libBarLinkRule.Args["libFlags"]
1785 libFoo1StubPath := "libFoo/android_arm64_armv8-a_core_shared_1/libFoo.so"
1786 if !strings.Contains(libFlags, libFoo1StubPath) {
1787 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
1788 }
Jiyong Park37b25202018-07-11 10:49:27 +09001789}