blob: 9d370c14baa63d6d9628d5a915bab6b285b5cbed [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 gatherRequiredDeps(os android.OsType) string {
55 ret := `
Jeff Gaston294356f2017-09-27 17:05:30 -070056 toolchain_library {
57 name: "libatomic",
58 vendor_available: true,
Jiyong Park37b25202018-07-11 10:49:27 +090059 recovery_available: true,
Dan Willemsenfeea4df2018-10-07 18:16:48 -070060 src: "",
Jeff Gaston294356f2017-09-27 17:05:30 -070061 }
62
63 toolchain_library {
64 name: "libcompiler_rt-extras",
65 vendor_available: true,
Jiyong Park37b25202018-07-11 10:49:27 +090066 recovery_available: true,
Dan Willemsenfeea4df2018-10-07 18:16:48 -070067 src: "",
Jeff Gaston294356f2017-09-27 17:05:30 -070068 }
69
70 toolchain_library {
Yi Kong7df0f302018-10-08 22:10:12 +000071 name: "libclang_rt.builtins-arm-android",
72 vendor_available: true,
73 recovery_available: true,
74 src: "",
75 }
76
77 toolchain_library {
78 name: "libclang_rt.builtins-aarch64-android",
79 vendor_available: true,
80 recovery_available: true,
81 src: "",
82 }
83
84 toolchain_library {
85 name: "libclang_rt.builtins-i686-android",
86 vendor_available: true,
87 recovery_available: true,
88 src: "",
89 }
90
91 toolchain_library {
92 name: "libclang_rt.builtins-x86_64-android",
93 vendor_available: true,
94 recovery_available: true,
95 src: "",
96 }
97
98 toolchain_library {
Jeff Gaston294356f2017-09-27 17:05:30 -070099 name: "libgcc",
100 vendor_available: true,
Jiyong Park37b25202018-07-11 10:49:27 +0900101 recovery_available: true,
Dan Willemsenfeea4df2018-10-07 18:16:48 -0700102 src: "",
Jeff Gaston294356f2017-09-27 17:05:30 -0700103 }
104
105 cc_library {
106 name: "libc",
Logan Chienf3511742017-10-31 18:04:35 +0800107 no_libgcc: true,
108 nocrt: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700109 system_shared_libs: [],
Jiyong Park37b25202018-07-11 10:49:27 +0900110 recovery_available: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700111 }
112 llndk_library {
113 name: "libc",
114 symbol_file: "",
115 }
116 cc_library {
117 name: "libm",
Logan Chienf3511742017-10-31 18:04:35 +0800118 no_libgcc: true,
119 nocrt: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700120 system_shared_libs: [],
Jiyong Park37b25202018-07-11 10:49:27 +0900121 recovery_available: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700122 }
123 llndk_library {
124 name: "libm",
125 symbol_file: "",
126 }
127 cc_library {
128 name: "libdl",
Logan Chienf3511742017-10-31 18:04:35 +0800129 no_libgcc: true,
130 nocrt: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700131 system_shared_libs: [],
Jiyong Park37b25202018-07-11 10:49:27 +0900132 recovery_available: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700133 }
134 llndk_library {
135 name: "libdl",
136 symbol_file: "",
137 }
Jaewoong Jung3e6b1fb2018-11-02 22:56:30 +0000138 cc_library {
Jiyong Park374510b2018-03-19 18:23:01 +0900139 name: "libc++_static",
140 no_libgcc: true,
141 nocrt: true,
142 system_shared_libs: [],
143 stl: "none",
144 vendor_available: true,
Jiyong Park37b25202018-07-11 10:49:27 +0900145 recovery_available: true,
Jiyong Park374510b2018-03-19 18:23:01 +0900146 }
147 cc_library {
148 name: "libc++",
149 no_libgcc: true,
150 nocrt: true,
151 system_shared_libs: [],
152 stl: "none",
153 vendor_available: true,
Jiyong Park37b25202018-07-11 10:49:27 +0900154 recovery_available: true,
Jiyong Park374510b2018-03-19 18:23:01 +0900155 vndk: {
156 enabled: true,
157 support_system_process: true,
158 },
159 }
160 cc_library {
161 name: "libunwind_llvm",
162 no_libgcc: true,
163 nocrt: true,
164 system_shared_libs: [],
165 stl: "none",
166 vendor_available: true,
Jiyong Park37b25202018-07-11 10:49:27 +0900167 recovery_available: true,
Jiyong Park374510b2018-03-19 18:23:01 +0900168 }
Jeff Gaston294356f2017-09-27 17:05:30 -0700169
170 cc_object {
171 name: "crtbegin_so",
Jiyong Park37b25202018-07-11 10:49:27 +0900172 recovery_available: true,
Jiyong Park5baac542018-08-28 09:55:37 +0900173 vendor_available: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700174 }
175
176 cc_object {
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800177 name: "crtbegin_static",
178 recovery_available: true,
179 vendor_available: true,
180 }
181
182 cc_object {
Jeff Gaston294356f2017-09-27 17:05:30 -0700183 name: "crtend_so",
Jiyong Park37b25202018-07-11 10:49:27 +0900184 recovery_available: true,
Jiyong Park5baac542018-08-28 09:55:37 +0900185 vendor_available: true,
Jeff Gaston294356f2017-09-27 17:05:30 -0700186 }
187
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800188 cc_object {
189 name: "crtend_android",
190 recovery_available: true,
191 vendor_available: true,
192 }
193
Colin Crossad59e752017-11-16 14:29:11 -0800194 cc_library {
195 name: "libprotobuf-cpp-lite",
196 }
Doug Hornc32c6b02019-01-17 14:44:05 -0800197 `
198 if os == android.Fuchsia {
199 ret += `
200 cc_library {
201 name: "libbioniccompat",
202 stl: "none",
203 }
204 cc_library {
205 name: "libcompiler_rt",
206 stl: "none",
207 }
208 `
209 }
210 return ret
211}
Colin Crossad59e752017-11-16 14:29:11 -0800212
Doug Hornc32c6b02019-01-17 14:44:05 -0800213func createTestContext(t *testing.T, config android.Config, bp string, os android.OsType) *android.TestContext {
214 ctx := android.NewTestArchContext()
215 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(BinaryFactory))
216 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(LibraryFactory))
217 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(LibrarySharedFactory))
Jiyong Parke4bb9862019-02-01 00:31:10 +0900218 ctx.RegisterModuleType("cc_library_static", android.ModuleFactoryAdaptor(LibraryStaticFactory))
Doug Hornc32c6b02019-01-17 14:44:05 -0800219 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(LibraryHeaderFactory))
220 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(ToolchainLibraryFactory))
221 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(LlndkLibraryFactory))
222 ctx.RegisterModuleType("llndk_headers", android.ModuleFactoryAdaptor(llndkHeadersFactory))
223 ctx.RegisterModuleType("vendor_public_library", android.ModuleFactoryAdaptor(vendorPublicLibraryFactory))
224 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(ObjectFactory))
225 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
226 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
227 ctx.BottomUp("image", ImageMutator).Parallel()
228 ctx.BottomUp("link", LinkageMutator).Parallel()
229 ctx.BottomUp("vndk", VndkMutator).Parallel()
230 ctx.BottomUp("version", VersionMutator).Parallel()
231 ctx.BottomUp("begin", BeginMutator).Parallel()
232 })
233 ctx.Register()
234
235 // add some modules that are required by the compiler and/or linker
236 bp = bp + gatherRequiredDeps(os)
Jeff Gaston294356f2017-09-27 17:05:30 -0700237
Jiyong Park6a43f042017-10-12 23:05:00 +0900238 ctx.MockFileSystem(map[string][]byte{
Jiyong Park7ed9de32018-10-15 22:25:07 +0900239 "Android.bp": []byte(bp),
240 "foo.c": nil,
241 "bar.c": nil,
242 "a.proto": nil,
243 "b.aidl": nil,
244 "my_include": nil,
245 "foo.map.txt": nil,
Jiyong Park6a43f042017-10-12 23:05:00 +0900246 })
247
Logan Chienf3511742017-10-31 18:04:35 +0800248 return ctx
249}
250
251func testCcWithConfig(t *testing.T, bp string, config android.Config) *android.TestContext {
Doug Hornc32c6b02019-01-17 14:44:05 -0800252 return testCcWithConfigForOs(t, bp, config, android.Android)
253}
254
255func testCcWithConfigForOs(t *testing.T, bp string, config android.Config, os android.OsType) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800256 t.Helper()
Doug Hornc32c6b02019-01-17 14:44:05 -0800257 ctx := createTestContext(t, config, bp, os)
Logan Chienf3511742017-10-31 18:04:35 +0800258
Jeff Gastond3e141d2017-08-08 17:46:01 -0700259 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800260 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +0900261 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800262 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +0900263
264 return ctx
265}
266
Logan Chienf3511742017-10-31 18:04:35 +0800267func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800268 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800269 config := android.TestArchConfig(buildDir, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700270 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
271 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800272
273 return testCcWithConfig(t, bp, config)
274}
275
276func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800277 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800278 config := android.TestArchConfig(buildDir, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700279 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800280
281 return testCcWithConfig(t, bp, config)
282}
283
284func testCcError(t *testing.T, pattern string, bp string) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800285 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800286 config := android.TestArchConfig(buildDir, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700287 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
288 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800289
Doug Hornc32c6b02019-01-17 14:44:05 -0800290 ctx := createTestContext(t, config, bp, android.Android)
Logan Chienf3511742017-10-31 18:04:35 +0800291
292 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
293 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +0800294 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800295 return
296 }
297
298 _, errs = ctx.PrepareBuildActions(config)
299 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +0800300 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800301 return
302 }
303
304 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
305}
306
307const (
Jiyong Park5baac542018-08-28 09:55:37 +0900308 coreVariant = "android_arm64_armv8-a_core_shared"
309 vendorVariant = "android_arm64_armv8-a_vendor_shared"
310 recoveryVariant = "android_arm64_armv8-a_recovery_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800311)
312
Doug Hornc32c6b02019-01-17 14:44:05 -0800313func TestFuchsiaDeps(t *testing.T) {
314 t.Helper()
315
316 bp := `
317 cc_library {
318 name: "libTest",
319 srcs: ["foo.c"],
320 target: {
321 fuchsia: {
322 srcs: ["bar.c"],
323 },
324 },
325 }`
326
327 config := android.TestArchConfigFuchsia(buildDir, nil)
328 ctx := testCcWithConfigForOs(t, bp, config, android.Fuchsia)
329
330 rt := false
331 fb := false
332
333 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
334 implicits := ld.Implicits
335 for _, lib := range implicits {
336 if strings.Contains(lib.Rel(), "libcompiler_rt") {
337 rt = true
338 }
339
340 if strings.Contains(lib.Rel(), "libbioniccompat") {
341 fb = true
342 }
343 }
344
345 if !rt || !fb {
346 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
347 }
348}
349
350func TestFuchsiaTargetDecl(t *testing.T) {
351 t.Helper()
352
353 bp := `
354 cc_library {
355 name: "libTest",
356 srcs: ["foo.c"],
357 target: {
358 fuchsia: {
359 srcs: ["bar.c"],
360 },
361 },
362 }`
363
364 config := android.TestArchConfigFuchsia(buildDir, nil)
365 ctx := testCcWithConfigForOs(t, bp, config, android.Fuchsia)
366 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
367 var objs []string
368 for _, o := range ld.Inputs {
369 objs = append(objs, o.Base())
370 }
371 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
372 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
373 }
374}
375
Jiyong Park6a43f042017-10-12 23:05:00 +0900376func TestVendorSrc(t *testing.T) {
377 ctx := testCc(t, `
378 cc_library {
379 name: "libTest",
380 srcs: ["foo.c"],
Logan Chienf3511742017-10-31 18:04:35 +0800381 no_libgcc: true,
382 nocrt: true,
383 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900384 vendor_available: true,
385 target: {
386 vendor: {
387 srcs: ["bar.c"],
388 },
389 },
390 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900391 `)
392
Logan Chienf3511742017-10-31 18:04:35 +0800393 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900394 var objs []string
395 for _, o := range ld.Inputs {
396 objs = append(objs, o.Base())
397 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800398 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900399 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
400 }
401}
402
Logan Chienf3511742017-10-31 18:04:35 +0800403func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
404 isVndkSp bool, extends string) {
405
Logan Chiend3c59a22018-03-29 14:08:15 +0800406 t.Helper()
407
Logan Chienf3511742017-10-31 18:04:35 +0800408 mod := ctx.ModuleForTests(name, vendorVariant).Module().(*Module)
409 if !mod.hasVendorVariant() {
Colin Crossf46e37f2018-03-21 16:25:58 -0700410 t.Errorf("%q must have vendor variant", name)
Logan Chienf3511742017-10-31 18:04:35 +0800411 }
412
413 // Check library properties.
414 lib, ok := mod.compiler.(*libraryDecorator)
415 if !ok {
416 t.Errorf("%q must have libraryDecorator", name)
417 } else if lib.baseInstaller.subDir != subDir {
418 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
419 lib.baseInstaller.subDir)
420 }
421
422 // Check VNDK properties.
423 if mod.vndkdep == nil {
424 t.Fatalf("%q must have `vndkdep`", name)
425 }
426 if !mod.isVndk() {
427 t.Errorf("%q isVndk() must equal to true", name)
428 }
429 if mod.isVndkSp() != isVndkSp {
430 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
431 }
432
433 // Check VNDK extension properties.
434 isVndkExt := extends != ""
435 if mod.isVndkExt() != isVndkExt {
436 t.Errorf("%q isVndkExt() must equal to %t", name, isVndkExt)
437 }
438
439 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
440 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
441 }
442}
443
444func TestVndk(t *testing.T) {
445 ctx := testCc(t, `
446 cc_library {
447 name: "libvndk",
448 vendor_available: true,
449 vndk: {
450 enabled: true,
451 },
452 nocrt: true,
453 }
454
455 cc_library {
456 name: "libvndk_private",
457 vendor_available: false,
458 vndk: {
459 enabled: true,
460 },
461 nocrt: true,
462 }
463
464 cc_library {
465 name: "libvndk_sp",
466 vendor_available: true,
467 vndk: {
468 enabled: true,
469 support_system_process: true,
470 },
471 nocrt: true,
472 }
473
474 cc_library {
475 name: "libvndk_sp_private",
476 vendor_available: false,
477 vndk: {
478 enabled: true,
479 support_system_process: true,
480 },
481 nocrt: true,
482 }
483 `)
484
485 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "")
486 checkVndkModule(t, ctx, "libvndk_private", "vndk-VER", false, "")
487 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "")
488 checkVndkModule(t, ctx, "libvndk_sp_private", "vndk-sp-VER", true, "")
489}
490
Logan Chiend3c59a22018-03-29 14:08:15 +0800491func TestVndkDepError(t *testing.T) {
492 // Check whether an error is emitted when a VNDK lib depends on a system lib.
493 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
494 cc_library {
495 name: "libvndk",
496 vendor_available: true,
497 vndk: {
498 enabled: true,
499 },
500 shared_libs: ["libfwk"], // Cause error
501 nocrt: true,
502 }
503
504 cc_library {
505 name: "libfwk",
506 nocrt: true,
507 }
508 `)
509
510 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
511 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
512 cc_library {
513 name: "libvndk",
514 vendor_available: true,
515 vndk: {
516 enabled: true,
517 },
518 shared_libs: ["libvendor"], // Cause error
519 nocrt: true,
520 }
521
522 cc_library {
523 name: "libvendor",
524 vendor: true,
525 nocrt: true,
526 }
527 `)
528
529 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
530 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
531 cc_library {
532 name: "libvndk_sp",
533 vendor_available: true,
534 vndk: {
535 enabled: true,
536 support_system_process: true,
537 },
538 shared_libs: ["libfwk"], // Cause error
539 nocrt: true,
540 }
541
542 cc_library {
543 name: "libfwk",
544 nocrt: true,
545 }
546 `)
547
548 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
549 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
550 cc_library {
551 name: "libvndk_sp",
552 vendor_available: true,
553 vndk: {
554 enabled: true,
555 support_system_process: true,
556 },
557 shared_libs: ["libvendor"], // Cause error
558 nocrt: true,
559 }
560
561 cc_library {
562 name: "libvendor",
563 vendor: true,
564 nocrt: true,
565 }
566 `)
567
568 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
569 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
570 cc_library {
571 name: "libvndk_sp",
572 vendor_available: true,
573 vndk: {
574 enabled: true,
575 support_system_process: true,
576 },
577 shared_libs: ["libvndk"], // Cause error
578 nocrt: true,
579 }
580
581 cc_library {
582 name: "libvndk",
583 vendor_available: true,
584 vndk: {
585 enabled: true,
586 },
587 nocrt: true,
588 }
589 `)
590}
591
Justin Yun9357f4a2018-11-28 15:14:47 +0900592func TestVndkMustNotBeProductSpecific(t *testing.T) {
593 // Check whether an error is emitted when a vndk lib has 'product_specific: true'.
594 testCcError(t, "product_specific must not be true when `vndk: {enabled: true}`", `
595 cc_library {
596 name: "libvndk",
597 product_specific: true, // Cause error
598 vendor_available: true,
599 vndk: {
600 enabled: true,
601 },
602 nocrt: true,
603 }
604 `)
605}
606
Logan Chienf3511742017-10-31 18:04:35 +0800607func TestVndkExt(t *testing.T) {
608 // This test checks the VNDK-Ext properties.
609 ctx := testCc(t, `
610 cc_library {
611 name: "libvndk",
612 vendor_available: true,
613 vndk: {
614 enabled: true,
615 },
616 nocrt: true,
617 }
618
619 cc_library {
620 name: "libvndk_ext",
621 vendor: true,
622 vndk: {
623 enabled: true,
624 extends: "libvndk",
625 },
626 nocrt: true,
627 }
628 `)
629
630 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk")
631}
632
Logan Chiend3c59a22018-03-29 14:08:15 +0800633func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +0800634 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
635 ctx := testCcNoVndk(t, `
636 cc_library {
637 name: "libvndk",
638 vendor_available: true,
639 vndk: {
640 enabled: true,
641 },
642 nocrt: true,
643 }
644
645 cc_library {
646 name: "libvndk_ext",
647 vendor: true,
648 vndk: {
649 enabled: true,
650 extends: "libvndk",
651 },
652 nocrt: true,
653 }
654 `)
655
656 // Ensures that the core variant of "libvndk_ext" can be found.
657 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
658 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
659 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
660 }
661}
662
663func TestVndkExtError(t *testing.T) {
664 // This test ensures an error is emitted in ill-formed vndk-ext definition.
665 testCcError(t, "must set `vendor: true` to set `extends: \".*\"`", `
666 cc_library {
667 name: "libvndk",
668 vendor_available: true,
669 vndk: {
670 enabled: true,
671 },
672 nocrt: true,
673 }
674
675 cc_library {
676 name: "libvndk_ext",
677 vndk: {
678 enabled: true,
679 extends: "libvndk",
680 },
681 nocrt: true,
682 }
683 `)
684
685 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
686 cc_library {
687 name: "libvndk",
688 vendor_available: true,
689 vndk: {
690 enabled: true,
691 },
692 nocrt: true,
693 }
694
695 cc_library {
696 name: "libvndk_ext",
697 vendor: true,
698 vndk: {
699 enabled: true,
700 },
701 nocrt: true,
702 }
703 `)
704}
705
706func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
707 // This test ensures an error is emitted for inconsistent support_system_process.
708 testCcError(t, "module \".*\" with mismatched support_system_process", `
709 cc_library {
710 name: "libvndk",
711 vendor_available: true,
712 vndk: {
713 enabled: true,
714 },
715 nocrt: true,
716 }
717
718 cc_library {
719 name: "libvndk_sp_ext",
720 vendor: true,
721 vndk: {
722 enabled: true,
723 extends: "libvndk",
724 support_system_process: true,
725 },
726 nocrt: true,
727 }
728 `)
729
730 testCcError(t, "module \".*\" with mismatched support_system_process", `
731 cc_library {
732 name: "libvndk_sp",
733 vendor_available: true,
734 vndk: {
735 enabled: true,
736 support_system_process: true,
737 },
738 nocrt: true,
739 }
740
741 cc_library {
742 name: "libvndk_ext",
743 vendor: true,
744 vndk: {
745 enabled: true,
746 extends: "libvndk_sp",
747 },
748 nocrt: true,
749 }
750 `)
751}
752
753func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800754 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +0800755 // with `vendor_available: false`.
756 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
757 cc_library {
758 name: "libvndk",
759 vendor_available: false,
760 vndk: {
761 enabled: true,
762 },
763 nocrt: true,
764 }
765
766 cc_library {
767 name: "libvndk_ext",
768 vendor: true,
769 vndk: {
770 enabled: true,
771 extends: "libvndk",
772 },
773 nocrt: true,
774 }
775 `)
776}
777
Logan Chiend3c59a22018-03-29 14:08:15 +0800778func TestVendorModuleUseVndkExt(t *testing.T) {
779 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +0800780 testCc(t, `
781 cc_library {
782 name: "libvndk",
783 vendor_available: true,
784 vndk: {
785 enabled: true,
786 },
787 nocrt: true,
788 }
789
790 cc_library {
791 name: "libvndk_ext",
792 vendor: true,
793 vndk: {
794 enabled: true,
795 extends: "libvndk",
796 },
797 nocrt: true,
798 }
799
800 cc_library {
801
802 name: "libvndk_sp",
803 vendor_available: true,
804 vndk: {
805 enabled: true,
806 support_system_process: true,
807 },
808 nocrt: true,
809 }
810
811 cc_library {
812 name: "libvndk_sp_ext",
813 vendor: true,
814 vndk: {
815 enabled: true,
816 extends: "libvndk_sp",
817 support_system_process: true,
818 },
819 nocrt: true,
820 }
821
822 cc_library {
823 name: "libvendor",
824 vendor: true,
825 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
826 nocrt: true,
827 }
828 `)
829}
830
Logan Chiend3c59a22018-03-29 14:08:15 +0800831func TestVndkExtUseVendorLib(t *testing.T) {
832 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +0800833 testCc(t, `
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 extends: "libvndk",
849 },
850 shared_libs: ["libvendor"],
851 nocrt: true,
852 }
853
854 cc_library {
855 name: "libvendor",
856 vendor: true,
857 nocrt: true,
858 }
859 `)
Logan Chienf3511742017-10-31 18:04:35 +0800860
Logan Chiend3c59a22018-03-29 14:08:15 +0800861 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
862 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +0800863 cc_library {
864 name: "libvndk_sp",
865 vendor_available: true,
866 vndk: {
867 enabled: true,
868 support_system_process: true,
869 },
870 nocrt: true,
871 }
872
873 cc_library {
874 name: "libvndk_sp_ext",
875 vendor: true,
876 vndk: {
877 enabled: true,
878 extends: "libvndk_sp",
879 support_system_process: true,
880 },
881 shared_libs: ["libvendor"], // Cause an error
882 nocrt: true,
883 }
884
885 cc_library {
886 name: "libvendor",
887 vendor: true,
888 nocrt: true,
889 }
890 `)
891}
892
Logan Chiend3c59a22018-03-29 14:08:15 +0800893func TestVndkSpExtUseVndkError(t *testing.T) {
894 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
895 // library.
896 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
897 cc_library {
898 name: "libvndk",
899 vendor_available: true,
900 vndk: {
901 enabled: true,
902 },
903 nocrt: true,
904 }
905
906 cc_library {
907 name: "libvndk_sp",
908 vendor_available: true,
909 vndk: {
910 enabled: true,
911 support_system_process: true,
912 },
913 nocrt: true,
914 }
915
916 cc_library {
917 name: "libvndk_sp_ext",
918 vendor: true,
919 vndk: {
920 enabled: true,
921 extends: "libvndk_sp",
922 support_system_process: true,
923 },
924 shared_libs: ["libvndk"], // Cause an error
925 nocrt: true,
926 }
927 `)
928
929 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
930 // library.
931 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
932 cc_library {
933 name: "libvndk",
934 vendor_available: true,
935 vndk: {
936 enabled: true,
937 },
938 nocrt: true,
939 }
940
941 cc_library {
942 name: "libvndk_ext",
943 vendor: true,
944 vndk: {
945 enabled: true,
946 extends: "libvndk",
947 },
948 nocrt: true,
949 }
950
951 cc_library {
952 name: "libvndk_sp",
953 vendor_available: true,
954 vndk: {
955 enabled: true,
956 support_system_process: true,
957 },
958 nocrt: true,
959 }
960
961 cc_library {
962 name: "libvndk_sp_ext",
963 vendor: true,
964 vndk: {
965 enabled: true,
966 extends: "libvndk_sp",
967 support_system_process: true,
968 },
969 shared_libs: ["libvndk_ext"], // Cause an error
970 nocrt: true,
971 }
972 `)
973}
974
975func TestVndkUseVndkExtError(t *testing.T) {
976 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
977 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +0800978 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
979 cc_library {
980 name: "libvndk",
981 vendor_available: true,
982 vndk: {
983 enabled: true,
984 },
985 nocrt: true,
986 }
987
988 cc_library {
989 name: "libvndk_ext",
990 vendor: true,
991 vndk: {
992 enabled: true,
993 extends: "libvndk",
994 },
995 nocrt: true,
996 }
997
998 cc_library {
999 name: "libvndk2",
1000 vendor_available: true,
1001 vndk: {
1002 enabled: true,
1003 },
1004 shared_libs: ["libvndk_ext"],
1005 nocrt: true,
1006 }
1007 `)
1008
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001009 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001010 cc_library {
1011 name: "libvndk",
1012 vendor_available: true,
1013 vndk: {
1014 enabled: true,
1015 },
1016 nocrt: true,
1017 }
1018
1019 cc_library {
1020 name: "libvndk_ext",
1021 vendor: true,
1022 vndk: {
1023 enabled: true,
1024 extends: "libvndk",
1025 },
1026 nocrt: true,
1027 }
1028
1029 cc_library {
1030 name: "libvndk2",
1031 vendor_available: true,
1032 vndk: {
1033 enabled: true,
1034 },
1035 target: {
1036 vendor: {
1037 shared_libs: ["libvndk_ext"],
1038 },
1039 },
1040 nocrt: true,
1041 }
1042 `)
1043
1044 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1045 cc_library {
1046 name: "libvndk_sp",
1047 vendor_available: true,
1048 vndk: {
1049 enabled: true,
1050 support_system_process: true,
1051 },
1052 nocrt: true,
1053 }
1054
1055 cc_library {
1056 name: "libvndk_sp_ext",
1057 vendor: true,
1058 vndk: {
1059 enabled: true,
1060 extends: "libvndk_sp",
1061 support_system_process: true,
1062 },
1063 nocrt: true,
1064 }
1065
1066 cc_library {
1067 name: "libvndk_sp_2",
1068 vendor_available: true,
1069 vndk: {
1070 enabled: true,
1071 support_system_process: true,
1072 },
1073 shared_libs: ["libvndk_sp_ext"],
1074 nocrt: true,
1075 }
1076 `)
1077
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001078 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001079 cc_library {
1080 name: "libvndk_sp",
1081 vendor_available: true,
1082 vndk: {
1083 enabled: true,
1084 },
1085 nocrt: true,
1086 }
1087
1088 cc_library {
1089 name: "libvndk_sp_ext",
1090 vendor: true,
1091 vndk: {
1092 enabled: true,
1093 extends: "libvndk_sp",
1094 },
1095 nocrt: true,
1096 }
1097
1098 cc_library {
1099 name: "libvndk_sp2",
1100 vendor_available: true,
1101 vndk: {
1102 enabled: true,
1103 },
1104 target: {
1105 vendor: {
1106 shared_libs: ["libvndk_sp_ext"],
1107 },
1108 },
1109 nocrt: true,
1110 }
1111 `)
1112}
1113
Colin Cross0af4b842015-04-30 16:36:18 -07001114var (
1115 str11 = "01234567891"
1116 str10 = str11[:10]
1117 str9 = str11[:9]
1118 str5 = str11[:5]
1119 str4 = str11[:4]
1120)
1121
1122var splitListForSizeTestCases = []struct {
1123 in []string
1124 out [][]string
1125 size int
1126}{
1127 {
1128 in: []string{str10},
1129 out: [][]string{{str10}},
1130 size: 10,
1131 },
1132 {
1133 in: []string{str9},
1134 out: [][]string{{str9}},
1135 size: 10,
1136 },
1137 {
1138 in: []string{str5},
1139 out: [][]string{{str5}},
1140 size: 10,
1141 },
1142 {
1143 in: []string{str11},
1144 out: nil,
1145 size: 10,
1146 },
1147 {
1148 in: []string{str10, str10},
1149 out: [][]string{{str10}, {str10}},
1150 size: 10,
1151 },
1152 {
1153 in: []string{str9, str10},
1154 out: [][]string{{str9}, {str10}},
1155 size: 10,
1156 },
1157 {
1158 in: []string{str10, str9},
1159 out: [][]string{{str10}, {str9}},
1160 size: 10,
1161 },
1162 {
1163 in: []string{str5, str4},
1164 out: [][]string{{str5, str4}},
1165 size: 10,
1166 },
1167 {
1168 in: []string{str5, str4, str5},
1169 out: [][]string{{str5, str4}, {str5}},
1170 size: 10,
1171 },
1172 {
1173 in: []string{str5, str4, str5, str4},
1174 out: [][]string{{str5, str4}, {str5, str4}},
1175 size: 10,
1176 },
1177 {
1178 in: []string{str5, str4, str5, str5},
1179 out: [][]string{{str5, str4}, {str5}, {str5}},
1180 size: 10,
1181 },
1182 {
1183 in: []string{str5, str5, str5, str4},
1184 out: [][]string{{str5}, {str5}, {str5, str4}},
1185 size: 10,
1186 },
1187 {
1188 in: []string{str9, str11},
1189 out: nil,
1190 size: 10,
1191 },
1192 {
1193 in: []string{str11, str9},
1194 out: nil,
1195 size: 10,
1196 },
1197}
1198
1199func TestSplitListForSize(t *testing.T) {
1200 for _, testCase := range splitListForSizeTestCases {
Colin Cross5b529592017-05-09 13:34:34 -07001201 out, _ := splitListForSize(android.PathsForTesting(testCase.in), testCase.size)
1202
1203 var outStrings [][]string
1204
1205 if len(out) > 0 {
1206 outStrings = make([][]string, len(out))
1207 for i, o := range out {
1208 outStrings[i] = o.Strings()
1209 }
1210 }
1211
1212 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07001213 t.Errorf("incorrect output:")
1214 t.Errorf(" input: %#v", testCase.in)
1215 t.Errorf(" size: %d", testCase.size)
1216 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07001217 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07001218 }
1219 }
1220}
Jeff Gaston294356f2017-09-27 17:05:30 -07001221
1222var staticLinkDepOrderTestCases = []struct {
1223 // This is a string representation of a map[moduleName][]moduleDependency .
1224 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001225 inStatic string
1226
1227 // This is a string representation of a map[moduleName][]moduleDependency .
1228 // It models the dependencies declared in an Android.bp file.
1229 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07001230
1231 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
1232 // The keys of allOrdered specify which modules we would like to check.
1233 // The values of allOrdered specify the expected result (of the transitive closure of all
1234 // dependencies) for each module to test
1235 allOrdered string
1236
1237 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
1238 // The keys of outOrdered specify which modules we would like to check.
1239 // The values of outOrdered specify the expected result (of the ordered linker command line)
1240 // for each module to test.
1241 outOrdered string
1242}{
1243 // Simple tests
1244 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001245 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07001246 outOrdered: "",
1247 },
1248 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001249 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001250 outOrdered: "a:",
1251 },
1252 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001253 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001254 outOrdered: "a:b; b:",
1255 },
1256 // Tests of reordering
1257 {
1258 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001259 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001260 outOrdered: "a:b,c,d; b:d; c:d; d:",
1261 },
1262 {
1263 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001264 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001265 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
1266 },
1267 {
1268 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001269 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07001270 outOrdered: "a:d,b,e,c; d:b; e:c",
1271 },
1272 {
1273 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001274 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07001275 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
1276 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
1277 },
1278 {
1279 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001280 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 -07001281 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
1282 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
1283 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001284 // shared dependencies
1285 {
1286 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
1287 // So, we don't actually have to check that a shared dependency of c will change the order
1288 // of a library that depends statically on b and on c. We only need to check that if c has
1289 // a shared dependency on b, that that shows up in allOrdered.
1290 inShared: "c:b",
1291 allOrdered: "c:b",
1292 outOrdered: "c:",
1293 },
1294 {
1295 // This test doesn't actually include any shared dependencies but it's a reminder of what
1296 // the second phase of the above test would look like
1297 inStatic: "a:b,c; c:b",
1298 allOrdered: "a:c,b; c:b",
1299 outOrdered: "a:c,b; c:b",
1300 },
Jeff Gaston294356f2017-09-27 17:05:30 -07001301 // tiebreakers for when two modules specifying different orderings and there is no dependency
1302 // to dictate an order
1303 {
1304 // 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 -08001305 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07001306 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
1307 },
1308 {
1309 // 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 -08001310 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 -07001311 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
1312 },
1313 // Tests involving duplicate dependencies
1314 {
1315 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001316 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001317 outOrdered: "a:c,b",
1318 },
1319 {
1320 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001321 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001322 outOrdered: "a:d,c,b",
1323 },
1324 // Tests to confirm the nonexistence of infinite loops.
1325 // These cases should never happen, so as long as the test terminates and the
1326 // result is deterministic then that should be fine.
1327 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001328 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07001329 outOrdered: "a:a",
1330 },
1331 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001332 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07001333 allOrdered: "a:b,c; b:c,a; c:a,b",
1334 outOrdered: "a:b; b:c; c:a",
1335 },
1336 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001337 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001338 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
1339 outOrdered: "a:c,b; b:a,c; c:b,a",
1340 },
1341}
1342
1343// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
1344func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
1345 // convert from "a:b,c; d:e" to "a:b,c;d:e"
1346 strippedText := strings.Replace(text, " ", "", -1)
1347 if len(strippedText) < 1 {
1348 return []android.Path{}, make(map[android.Path][]android.Path, 0)
1349 }
1350 allDeps = make(map[android.Path][]android.Path, 0)
1351
1352 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
1353 moduleTexts := strings.Split(strippedText, ";")
1354
1355 outputForModuleName := func(moduleName string) android.Path {
1356 return android.PathForTesting(moduleName)
1357 }
1358
1359 for _, moduleText := range moduleTexts {
1360 // convert from "a:b,c" to ["a", "b,c"]
1361 components := strings.Split(moduleText, ":")
1362 if len(components) != 2 {
1363 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
1364 }
1365 moduleName := components[0]
1366 moduleOutput := outputForModuleName(moduleName)
1367 modulesInOrder = append(modulesInOrder, moduleOutput)
1368
1369 depString := components[1]
1370 // convert from "b,c" to ["b", "c"]
1371 depNames := strings.Split(depString, ",")
1372 if len(depString) < 1 {
1373 depNames = []string{}
1374 }
1375 var deps []android.Path
1376 for _, depName := range depNames {
1377 deps = append(deps, outputForModuleName(depName))
1378 }
1379 allDeps[moduleOutput] = deps
1380 }
1381 return modulesInOrder, allDeps
1382}
1383
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001384func TestLinkReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001385 for _, testCase := range staticLinkDepOrderTestCases {
1386 errs := []string{}
1387
1388 // parse testcase
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001389 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)
Jeff Gaston294356f2017-09-27 17:05:30 -07001390 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)
1391 if testCase.allOrdered == "" {
1392 // allow the test case to skip specifying allOrdered
1393 testCase.allOrdered = testCase.outOrdered
1394 }
1395 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001396 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)
Jeff Gaston294356f2017-09-27 17:05:30 -07001397
1398 // For each module whose post-reordered dependencies were specified, validate that
1399 // reordering the inputs produces the expected outputs.
1400 for _, moduleName := range expectedModuleNames {
1401 moduleDeps := givenTransitiveDeps[moduleName]
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001402 givenSharedDeps := givenAllSharedDeps[moduleName]
1403 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -07001404
1405 correctAllOrdered := expectedAllDeps[moduleName]
1406 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {
1407 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001408 "\nin static:%q"+
1409 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07001410 "\nmodule: %v"+
1411 "\nexpected: %s"+
1412 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001413 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07001414 }
1415
1416 correctOutputDeps := expectedTransitiveDeps[moduleName]
1417 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {
1418 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001419 "\nin static:%q"+
1420 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07001421 "\nmodule: %v"+
1422 "\nexpected: %s"+
1423 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001424 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07001425 }
1426 }
1427
1428 if len(errs) > 0 {
1429 sort.Strings(errs)
1430 for _, err := range errs {
1431 t.Error(err)
1432 }
1433 }
1434 }
1435}
Logan Chienf3511742017-10-31 18:04:35 +08001436
Jeff Gaston294356f2017-09-27 17:05:30 -07001437func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
1438 for _, moduleName := range moduleNames {
1439 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
1440 output := module.outputFile.Path()
1441 paths = append(paths, output)
1442 }
1443 return paths
1444}
1445
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001446func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001447 ctx := testCc(t, `
1448 cc_library {
1449 name: "a",
1450 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09001451 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001452 }
1453 cc_library {
1454 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09001455 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001456 }
1457 cc_library {
1458 name: "c",
1459 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09001460 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001461 }
1462 cc_library {
1463 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09001464 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001465 }
1466
1467 `)
1468
1469 variant := "android_arm64_armv8-a_core_static"
1470 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001471 actual := moduleA.depsInLinkOrder
Jeff Gaston294356f2017-09-27 17:05:30 -07001472 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})
1473
1474 if !reflect.DeepEqual(actual, expected) {
1475 t.Errorf("staticDeps orderings were not propagated correctly"+
1476 "\nactual: %v"+
1477 "\nexpected: %v",
1478 actual,
1479 expected,
1480 )
1481 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09001482}
Jeff Gaston294356f2017-09-27 17:05:30 -07001483
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001484func TestStaticLibDepReorderingWithShared(t *testing.T) {
1485 ctx := testCc(t, `
1486 cc_library {
1487 name: "a",
1488 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09001489 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001490 }
1491 cc_library {
1492 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09001493 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001494 }
1495 cc_library {
1496 name: "c",
1497 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09001498 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001499 }
1500
1501 `)
1502
1503 variant := "android_arm64_armv8-a_core_static"
1504 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
1505 actual := moduleA.depsInLinkOrder
1506 expected := getOutputPaths(ctx, variant, []string{"c", "b"})
1507
1508 if !reflect.DeepEqual(actual, expected) {
1509 t.Errorf("staticDeps orderings did not account for shared libs"+
1510 "\nactual: %v"+
1511 "\nexpected: %v",
1512 actual,
1513 expected,
1514 )
1515 }
1516}
1517
Jiyong Parka46a4d52017-12-14 19:54:34 +09001518func TestLlndkHeaders(t *testing.T) {
1519 ctx := testCc(t, `
1520 llndk_headers {
1521 name: "libllndk_headers",
1522 export_include_dirs: ["my_include"],
1523 }
1524 llndk_library {
1525 name: "libllndk",
1526 export_llndk_headers: ["libllndk_headers"],
1527 }
1528 cc_library {
1529 name: "libvendor",
1530 shared_libs: ["libllndk"],
1531 vendor: true,
1532 srcs: ["foo.c"],
Logan Chienf3511742017-10-31 18:04:35 +08001533 no_libgcc: true,
1534 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09001535 }
1536 `)
1537
1538 // _static variant is used since _shared reuses *.o from the static variant
1539 cc := ctx.ModuleForTests("libvendor", "android_arm_armv7-a-neon_vendor_static").Rule("cc")
1540 cflags := cc.Args["cFlags"]
1541 if !strings.Contains(cflags, "-Imy_include") {
1542 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
1543 }
1544}
1545
Logan Chien43d34c32017-12-20 01:17:32 +08001546func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
1547 actual := module.Properties.AndroidMkRuntimeLibs
1548 if !reflect.DeepEqual(actual, expected) {
1549 t.Errorf("incorrect runtime_libs for shared libs"+
1550 "\nactual: %v"+
1551 "\nexpected: %v",
1552 actual,
1553 expected,
1554 )
1555 }
1556}
1557
1558const runtimeLibAndroidBp = `
1559 cc_library {
1560 name: "libvendor_available1",
1561 vendor_available: true,
1562 no_libgcc : true,
1563 nocrt : true,
1564 system_shared_libs : [],
1565 }
1566 cc_library {
1567 name: "libvendor_available2",
1568 vendor_available: true,
1569 runtime_libs: ["libvendor_available1"],
1570 no_libgcc : true,
1571 nocrt : true,
1572 system_shared_libs : [],
1573 }
1574 cc_library {
1575 name: "libvendor_available3",
1576 vendor_available: true,
1577 runtime_libs: ["libvendor_available1"],
1578 target: {
1579 vendor: {
1580 exclude_runtime_libs: ["libvendor_available1"],
1581 }
1582 },
1583 no_libgcc : true,
1584 nocrt : true,
1585 system_shared_libs : [],
1586 }
1587 cc_library {
1588 name: "libcore",
1589 runtime_libs: ["libvendor_available1"],
1590 no_libgcc : true,
1591 nocrt : true,
1592 system_shared_libs : [],
1593 }
1594 cc_library {
1595 name: "libvendor1",
1596 vendor: true,
1597 no_libgcc : true,
1598 nocrt : true,
1599 system_shared_libs : [],
1600 }
1601 cc_library {
1602 name: "libvendor2",
1603 vendor: true,
1604 runtime_libs: ["libvendor_available1", "libvendor1"],
1605 no_libgcc : true,
1606 nocrt : true,
1607 system_shared_libs : [],
1608 }
1609`
1610
1611func TestRuntimeLibs(t *testing.T) {
1612 ctx := testCc(t, runtimeLibAndroidBp)
1613
1614 // runtime_libs for core variants use the module names without suffixes.
1615 variant := "android_arm64_armv8-a_core_shared"
1616
1617 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
1618 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1619
1620 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
1621 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1622
1623 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
1624 // and vendor variants.
1625 variant = "android_arm64_armv8-a_vendor_shared"
1626
1627 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
1628 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
1629
1630 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
1631 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
1632}
1633
1634func TestExcludeRuntimeLibs(t *testing.T) {
1635 ctx := testCc(t, runtimeLibAndroidBp)
1636
1637 variant := "android_arm64_armv8-a_core_shared"
1638 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
1639 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1640
1641 variant = "android_arm64_armv8-a_vendor_shared"
1642 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
1643 checkRuntimeLibs(t, nil, module)
1644}
1645
1646func TestRuntimeLibsNoVndk(t *testing.T) {
1647 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
1648
1649 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
1650
1651 variant := "android_arm64_armv8-a_core_shared"
1652
1653 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
1654 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1655
1656 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
1657 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
1658}
1659
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00001660func checkStaticLibs(t *testing.T, expected []string, module *Module) {
1661 actual := module.Properties.AndroidMkStaticLibs
1662 if !reflect.DeepEqual(actual, expected) {
1663 t.Errorf("incorrect static_libs"+
1664 "\nactual: %v"+
1665 "\nexpected: %v",
1666 actual,
1667 expected,
1668 )
1669 }
1670}
1671
1672const staticLibAndroidBp = `
1673 cc_library {
1674 name: "lib1",
1675 }
1676 cc_library {
1677 name: "lib2",
1678 static_libs: ["lib1"],
1679 }
1680`
1681
1682func TestStaticLibDepExport(t *testing.T) {
1683 ctx := testCc(t, staticLibAndroidBp)
1684
1685 // Check the shared version of lib2.
1686 variant := "android_arm64_armv8-a_core_shared"
1687 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
1688 checkStaticLibs(t, []string{"lib1", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc"}, module)
1689
1690 // Check the static version of lib2.
1691 variant = "android_arm64_armv8-a_core_static"
1692 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
1693 // libc++_static is linked additionally.
1694 checkStaticLibs(t, []string{"lib1", "libc++_static", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc"}, module)
1695}
1696
Jiyong Parkd08b6972017-09-26 10:50:54 +09001697var compilerFlagsTestCases = []struct {
1698 in string
1699 out bool
1700}{
1701 {
1702 in: "a",
1703 out: false,
1704 },
1705 {
1706 in: "-a",
1707 out: true,
1708 },
1709 {
1710 in: "-Ipath/to/something",
1711 out: false,
1712 },
1713 {
1714 in: "-isystempath/to/something",
1715 out: false,
1716 },
1717 {
1718 in: "--coverage",
1719 out: false,
1720 },
1721 {
1722 in: "-include a/b",
1723 out: true,
1724 },
1725 {
1726 in: "-include a/b c/d",
1727 out: false,
1728 },
1729 {
1730 in: "-DMACRO",
1731 out: true,
1732 },
1733 {
1734 in: "-DMAC RO",
1735 out: false,
1736 },
1737 {
1738 in: "-a -b",
1739 out: false,
1740 },
1741 {
1742 in: "-DMACRO=definition",
1743 out: true,
1744 },
1745 {
1746 in: "-DMACRO=defi nition",
1747 out: true, // TODO(jiyong): this should be false
1748 },
1749 {
1750 in: "-DMACRO(x)=x + 1",
1751 out: true,
1752 },
1753 {
1754 in: "-DMACRO=\"defi nition\"",
1755 out: true,
1756 },
1757}
1758
1759type mockContext struct {
1760 BaseModuleContext
1761 result bool
1762}
1763
1764func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1765 // CheckBadCompilerFlags calls this function when the flag should be rejected
1766 ctx.result = false
1767}
1768
1769func TestCompilerFlags(t *testing.T) {
1770 for _, testCase := range compilerFlagsTestCases {
1771 ctx := &mockContext{result: true}
1772 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
1773 if ctx.result != testCase.out {
1774 t.Errorf("incorrect output:")
1775 t.Errorf(" input: %#v", testCase.in)
1776 t.Errorf(" expected: %#v", testCase.out)
1777 t.Errorf(" got: %#v", ctx.result)
1778 }
1779 }
Jeff Gaston294356f2017-09-27 17:05:30 -07001780}
Jiyong Park374510b2018-03-19 18:23:01 +09001781
1782func TestVendorPublicLibraries(t *testing.T) {
1783 ctx := testCc(t, `
1784 cc_library_headers {
1785 name: "libvendorpublic_headers",
1786 export_include_dirs: ["my_include"],
1787 }
1788 vendor_public_library {
1789 name: "libvendorpublic",
1790 symbol_file: "",
1791 export_public_headers: ["libvendorpublic_headers"],
1792 }
1793 cc_library {
1794 name: "libvendorpublic",
1795 srcs: ["foo.c"],
1796 vendor: true,
1797 no_libgcc: true,
1798 nocrt: true,
1799 }
1800
1801 cc_library {
1802 name: "libsystem",
1803 shared_libs: ["libvendorpublic"],
1804 vendor: false,
1805 srcs: ["foo.c"],
1806 no_libgcc: true,
1807 nocrt: true,
1808 }
1809 cc_library {
1810 name: "libvendor",
1811 shared_libs: ["libvendorpublic"],
1812 vendor: true,
1813 srcs: ["foo.c"],
1814 no_libgcc: true,
1815 nocrt: true,
1816 }
1817 `)
1818
1819 variant := "android_arm64_armv8-a_core_shared"
1820
1821 // test if header search paths are correctly added
1822 // _static variant is used since _shared reuses *.o from the static variant
1823 cc := ctx.ModuleForTests("libsystem", strings.Replace(variant, "_shared", "_static", 1)).Rule("cc")
1824 cflags := cc.Args["cFlags"]
1825 if !strings.Contains(cflags, "-Imy_include") {
1826 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
1827 }
1828
1829 // test if libsystem is linked to the stub
1830 ld := ctx.ModuleForTests("libsystem", variant).Rule("ld")
1831 libflags := ld.Args["libFlags"]
1832 stubPaths := getOutputPaths(ctx, variant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
1833 if !strings.Contains(libflags, stubPaths[0].String()) {
1834 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
1835 }
1836
1837 // test if libvendor is linked to the real shared lib
1838 ld = ctx.ModuleForTests("libvendor", strings.Replace(variant, "_core", "_vendor", 1)).Rule("ld")
1839 libflags = ld.Args["libFlags"]
1840 stubPaths = getOutputPaths(ctx, strings.Replace(variant, "_core", "_vendor", 1), []string{"libvendorpublic"})
1841 if !strings.Contains(libflags, stubPaths[0].String()) {
1842 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
1843 }
1844
1845}
Jiyong Park37b25202018-07-11 10:49:27 +09001846
1847func TestRecovery(t *testing.T) {
1848 ctx := testCc(t, `
1849 cc_library_shared {
1850 name: "librecovery",
1851 recovery: true,
1852 }
1853 cc_library_shared {
1854 name: "librecovery32",
1855 recovery: true,
1856 compile_multilib:"32",
1857 }
Jiyong Park5baac542018-08-28 09:55:37 +09001858 cc_library_shared {
1859 name: "libHalInRecovery",
1860 recovery_available: true,
1861 vendor: true,
1862 }
Jiyong Park37b25202018-07-11 10:49:27 +09001863 `)
1864
1865 variants := ctx.ModuleVariantsForTests("librecovery")
1866 const arm64 = "android_arm64_armv8-a_recovery_shared"
1867 if len(variants) != 1 || !android.InList(arm64, variants) {
1868 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
1869 }
1870
1871 variants = ctx.ModuleVariantsForTests("librecovery32")
1872 if android.InList(arm64, variants) {
1873 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
1874 }
Jiyong Park5baac542018-08-28 09:55:37 +09001875
1876 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
1877 if !recoveryModule.Platform() {
1878 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
1879 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001880}
Jiyong Park5baac542018-08-28 09:55:37 +09001881
Jiyong Park7ed9de32018-10-15 22:25:07 +09001882func TestVersionedStubs(t *testing.T) {
1883 ctx := testCc(t, `
1884 cc_library_shared {
1885 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09001886 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09001887 stubs: {
1888 symbol_file: "foo.map.txt",
1889 versions: ["1", "2", "3"],
1890 },
1891 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09001892
Jiyong Park7ed9de32018-10-15 22:25:07 +09001893 cc_library_shared {
1894 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09001895 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09001896 shared_libs: ["libFoo#1"],
1897 }`)
1898
1899 variants := ctx.ModuleVariantsForTests("libFoo")
1900 expectedVariants := []string{
1901 "android_arm64_armv8-a_core_shared",
1902 "android_arm64_armv8-a_core_shared_1",
1903 "android_arm64_armv8-a_core_shared_2",
1904 "android_arm64_armv8-a_core_shared_3",
1905 "android_arm_armv7-a-neon_core_shared",
1906 "android_arm_armv7-a-neon_core_shared_1",
1907 "android_arm_armv7-a-neon_core_shared_2",
1908 "android_arm_armv7-a-neon_core_shared_3",
1909 }
1910 variantsMismatch := false
1911 if len(variants) != len(expectedVariants) {
1912 variantsMismatch = true
1913 } else {
1914 for _, v := range expectedVariants {
1915 if !inList(v, variants) {
1916 variantsMismatch = false
1917 }
1918 }
1919 }
1920 if variantsMismatch {
1921 t.Errorf("variants of libFoo expected:\n")
1922 for _, v := range expectedVariants {
1923 t.Errorf("%q\n", v)
1924 }
1925 t.Errorf(", but got:\n")
1926 for _, v := range variants {
1927 t.Errorf("%q\n", v)
1928 }
1929 }
1930
1931 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_core_shared").Rule("ld")
1932 libFlags := libBarLinkRule.Args["libFlags"]
1933 libFoo1StubPath := "libFoo/android_arm64_armv8-a_core_shared_1/libFoo.so"
1934 if !strings.Contains(libFlags, libFoo1StubPath) {
1935 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
1936 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09001937
1938 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_core_shared").Rule("cc")
1939 cFlags := libBarCompileRule.Args["cFlags"]
1940 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
1941 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
1942 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
1943 }
Jiyong Park37b25202018-07-11 10:49:27 +09001944}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08001945
1946func TestStaticExecutable(t *testing.T) {
1947 ctx := testCc(t, `
1948 cc_binary {
1949 name: "static_test",
1950 srcs: ["foo.c"],
1951 static_executable: true,
1952 }`)
1953
1954 variant := "android_arm64_armv8-a_core"
1955 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
1956 libFlags := binModuleRule.Args["libFlags"]
1957 systemStaticLibs := []string{"libc.a", "libm.a", "libdl.a"}
1958 for _, lib := range systemStaticLibs {
1959 if !strings.Contains(libFlags, lib) {
1960 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
1961 }
1962 }
1963 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
1964 for _, lib := range systemSharedLibs {
1965 if strings.Contains(libFlags, lib) {
1966 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
1967 }
1968 }
1969}
Jiyong Parke4bb9862019-02-01 00:31:10 +09001970
1971func TestStaticDepsOrderWithStubs(t *testing.T) {
1972 ctx := testCc(t, `
1973 cc_binary {
1974 name: "mybin",
1975 srcs: ["foo.c"],
1976 static_libs: ["libB"],
1977 static_executable: true,
1978 stl: "none",
1979 }
1980
1981 cc_library {
1982 name: "libB",
1983 srcs: ["foo.c"],
1984 shared_libs: ["libC"],
1985 stl: "none",
1986 }
1987
1988 cc_library {
1989 name: "libC",
1990 srcs: ["foo.c"],
1991 stl: "none",
1992 stubs: {
1993 versions: ["1"],
1994 },
1995 }`)
1996
1997 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a_core").Module().(*Module)
1998 actual := mybin.depsInLinkOrder
1999 expected := getOutputPaths(ctx, "android_arm64_armv8-a_core_static", []string{"libB", "libC"})
2000
2001 if !reflect.DeepEqual(actual, expected) {
2002 t.Errorf("staticDeps orderings were not propagated correctly"+
2003 "\nactual: %v"+
2004 "\nexpected: %v",
2005 actual,
2006 expected,
2007 )
2008 }
2009}