blob: ef6364b58b6b426f6f32bd68bd4a3e815c0112ee [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"
Inseob Kim1f086e22019-05-09 13:29:15 +090023 "path/filepath"
Colin Cross74d1ec02015-04-28 13:30:13 -070024 "reflect"
Jeff Gaston294356f2017-09-27 17:05:30 -070025 "sort"
26 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070027 "testing"
28)
29
Jiyong Park6a43f042017-10-12 23:05:00 +090030var buildDir string
31
32func setUp() {
33 var err error
34 buildDir, err = ioutil.TempDir("", "soong_cc_test")
35 if err != nil {
36 panic(err)
37 }
38}
39
40func tearDown() {
41 os.RemoveAll(buildDir)
42}
43
44func TestMain(m *testing.M) {
45 run := func() int {
46 setUp()
47 defer tearDown()
48
49 return m.Run()
50 }
51
52 os.Exit(run())
53}
54
Colin Cross33b2fb72019-05-14 14:07:01 -070055func createTestContext(t *testing.T, config android.Config, bp string, fs map[string][]byte,
56 os android.OsType) *android.TestContext {
57
Doug Hornc32c6b02019-01-17 14:44:05 -080058 ctx := android.NewTestArchContext()
59 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(BinaryFactory))
Colin Crossfe17f6f2019-03-28 19:30:56 -070060 ctx.RegisterModuleType("cc_binary_host", android.ModuleFactoryAdaptor(binaryHostFactory))
Doug Hornc32c6b02019-01-17 14:44:05 -080061 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(LibraryFactory))
62 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(LibrarySharedFactory))
Jiyong Parke4bb9862019-02-01 00:31:10 +090063 ctx.RegisterModuleType("cc_library_static", android.ModuleFactoryAdaptor(LibraryStaticFactory))
Doug Hornc32c6b02019-01-17 14:44:05 -080064 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(LibraryHeaderFactory))
65 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(ToolchainLibraryFactory))
66 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(LlndkLibraryFactory))
67 ctx.RegisterModuleType("llndk_headers", android.ModuleFactoryAdaptor(llndkHeadersFactory))
68 ctx.RegisterModuleType("vendor_public_library", android.ModuleFactoryAdaptor(vendorPublicLibraryFactory))
69 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(ObjectFactory))
70 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
71 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
72 ctx.BottomUp("image", ImageMutator).Parallel()
73 ctx.BottomUp("link", LinkageMutator).Parallel()
74 ctx.BottomUp("vndk", VndkMutator).Parallel()
75 ctx.BottomUp("version", VersionMutator).Parallel()
76 ctx.BottomUp("begin", BeginMutator).Parallel()
77 })
Jooyung Hana70f0672019-01-18 15:20:43 +090078 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
79 ctx.TopDown("double_loadable", checkDoubleLoadableLibraries).Parallel()
80 })
Inseob Kim1f086e22019-05-09 13:29:15 +090081 ctx.RegisterSingletonType("vndk-snapshot", android.SingletonFactoryAdaptor(VndkSnapshotSingleton))
Doug Hornc32c6b02019-01-17 14:44:05 -080082
83 // add some modules that are required by the compiler and/or linker
Inseob Kimc0907f12019-02-08 21:00:45 +090084 bp = bp + GatherRequiredDepsForTest(os)
Jeff Gaston294356f2017-09-27 17:05:30 -070085
Colin Cross33b2fb72019-05-14 14:07:01 -070086 mockFS := map[string][]byte{
Jiyong Park7ed9de32018-10-15 22:25:07 +090087 "Android.bp": []byte(bp),
88 "foo.c": nil,
89 "bar.c": nil,
90 "a.proto": nil,
91 "b.aidl": nil,
92 "my_include": nil,
93 "foo.map.txt": nil,
Colin Cross33b2fb72019-05-14 14:07:01 -070094 "liba.so": nil,
95 }
96
97 for k, v := range fs {
98 mockFS[k] = v
99 }
100
101 ctx.MockFileSystem(mockFS)
Jiyong Park6a43f042017-10-12 23:05:00 +0900102
Logan Chienf3511742017-10-31 18:04:35 +0800103 return ctx
104}
105
106func testCcWithConfig(t *testing.T, bp string, config android.Config) *android.TestContext {
Doug Hornc32c6b02019-01-17 14:44:05 -0800107 return testCcWithConfigForOs(t, bp, config, android.Android)
108}
109
110func testCcWithConfigForOs(t *testing.T, bp string, config android.Config, os android.OsType) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800111 t.Helper()
Colin Cross33b2fb72019-05-14 14:07:01 -0700112 ctx := createTestContext(t, config, bp, nil, os)
113 ctx.Register()
Logan Chienf3511742017-10-31 18:04:35 +0800114
Jeff Gastond3e141d2017-08-08 17:46:01 -0700115 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800116 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +0900117 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800118 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +0900119
120 return ctx
121}
122
Logan Chienf3511742017-10-31 18:04:35 +0800123func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800124 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800125 config := android.TestArchConfig(buildDir, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700126 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
127 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800128
129 return testCcWithConfig(t, bp, config)
130}
131
132func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800133 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800134 config := android.TestArchConfig(buildDir, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700135 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800136
137 return testCcWithConfig(t, bp, config)
138}
139
140func testCcError(t *testing.T, pattern string, bp string) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800141 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800142 config := android.TestArchConfig(buildDir, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700143 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
144 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800145
Colin Cross33b2fb72019-05-14 14:07:01 -0700146 ctx := createTestContext(t, config, bp, nil, android.Android)
147 ctx.Register()
Logan Chienf3511742017-10-31 18:04:35 +0800148
149 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
150 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +0800151 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800152 return
153 }
154
155 _, errs = ctx.PrepareBuildActions(config)
156 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +0800157 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800158 return
159 }
160
161 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
162}
163
164const (
Jiyong Park5baac542018-08-28 09:55:37 +0900165 coreVariant = "android_arm64_armv8-a_core_shared"
166 vendorVariant = "android_arm64_armv8-a_vendor_shared"
167 recoveryVariant = "android_arm64_armv8-a_recovery_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800168)
169
Doug Hornc32c6b02019-01-17 14:44:05 -0800170func TestFuchsiaDeps(t *testing.T) {
171 t.Helper()
172
173 bp := `
174 cc_library {
175 name: "libTest",
176 srcs: ["foo.c"],
177 target: {
178 fuchsia: {
179 srcs: ["bar.c"],
180 },
181 },
182 }`
183
184 config := android.TestArchConfigFuchsia(buildDir, nil)
185 ctx := testCcWithConfigForOs(t, bp, config, android.Fuchsia)
186
187 rt := false
188 fb := false
189
190 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
191 implicits := ld.Implicits
192 for _, lib := range implicits {
193 if strings.Contains(lib.Rel(), "libcompiler_rt") {
194 rt = true
195 }
196
197 if strings.Contains(lib.Rel(), "libbioniccompat") {
198 fb = true
199 }
200 }
201
202 if !rt || !fb {
203 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
204 }
205}
206
207func TestFuchsiaTargetDecl(t *testing.T) {
208 t.Helper()
209
210 bp := `
211 cc_library {
212 name: "libTest",
213 srcs: ["foo.c"],
214 target: {
215 fuchsia: {
216 srcs: ["bar.c"],
217 },
218 },
219 }`
220
221 config := android.TestArchConfigFuchsia(buildDir, nil)
222 ctx := testCcWithConfigForOs(t, bp, config, android.Fuchsia)
223 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
224 var objs []string
225 for _, o := range ld.Inputs {
226 objs = append(objs, o.Base())
227 }
228 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
229 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
230 }
231}
232
Jiyong Park6a43f042017-10-12 23:05:00 +0900233func TestVendorSrc(t *testing.T) {
234 ctx := testCc(t, `
235 cc_library {
236 name: "libTest",
237 srcs: ["foo.c"],
Logan Chienf3511742017-10-31 18:04:35 +0800238 no_libgcc: true,
239 nocrt: true,
240 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900241 vendor_available: true,
242 target: {
243 vendor: {
244 srcs: ["bar.c"],
245 },
246 },
247 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900248 `)
249
Logan Chienf3511742017-10-31 18:04:35 +0800250 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900251 var objs []string
252 for _, o := range ld.Inputs {
253 objs = append(objs, o.Base())
254 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800255 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900256 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
257 }
258}
259
Logan Chienf3511742017-10-31 18:04:35 +0800260func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
261 isVndkSp bool, extends string) {
262
Logan Chiend3c59a22018-03-29 14:08:15 +0800263 t.Helper()
264
Logan Chienf3511742017-10-31 18:04:35 +0800265 mod := ctx.ModuleForTests(name, vendorVariant).Module().(*Module)
266 if !mod.hasVendorVariant() {
Colin Crossf46e37f2018-03-21 16:25:58 -0700267 t.Errorf("%q must have vendor variant", name)
Logan Chienf3511742017-10-31 18:04:35 +0800268 }
269
270 // Check library properties.
271 lib, ok := mod.compiler.(*libraryDecorator)
272 if !ok {
273 t.Errorf("%q must have libraryDecorator", name)
274 } else if lib.baseInstaller.subDir != subDir {
275 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
276 lib.baseInstaller.subDir)
277 }
278
279 // Check VNDK properties.
280 if mod.vndkdep == nil {
281 t.Fatalf("%q must have `vndkdep`", name)
282 }
283 if !mod.isVndk() {
284 t.Errorf("%q isVndk() must equal to true", name)
285 }
286 if mod.isVndkSp() != isVndkSp {
287 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
288 }
289
290 // Check VNDK extension properties.
291 isVndkExt := extends != ""
292 if mod.isVndkExt() != isVndkExt {
293 t.Errorf("%q isVndkExt() must equal to %t", name, isVndkExt)
294 }
295
296 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
297 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
298 }
299}
300
Inseob Kim1f086e22019-05-09 13:29:15 +0900301func checkVndkSnapshot(t *testing.T, ctx *android.TestContext, name, subDir, variant string) {
302 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
303
304 snapshotPath := filepath.Join(subDir, name+".so")
305 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
306 if !mod.outputFile.Valid() {
307 t.Errorf("%q must have output\n", name)
308 return
309 }
310
311 out := vndkSnapshot.Output(snapshotPath)
312 if out.Input != mod.outputFile.Path() {
313 t.Errorf("The input of VNDK snapshot must be %q, but %q", out.Input.String(), mod.outputFile.String())
314 }
315}
316
Logan Chienf3511742017-10-31 18:04:35 +0800317func TestVndk(t *testing.T) {
Inseob Kim1f086e22019-05-09 13:29:15 +0900318 config := android.TestArchConfig(buildDir, nil)
319 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
320 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
321
322 ctx := testCcWithConfig(t, `
Logan Chienf3511742017-10-31 18:04:35 +0800323 cc_library {
324 name: "libvndk",
325 vendor_available: true,
326 vndk: {
327 enabled: true,
328 },
329 nocrt: true,
330 }
331
332 cc_library {
333 name: "libvndk_private",
334 vendor_available: false,
335 vndk: {
336 enabled: true,
337 },
338 nocrt: true,
339 }
340
341 cc_library {
342 name: "libvndk_sp",
343 vendor_available: true,
344 vndk: {
345 enabled: true,
346 support_system_process: true,
347 },
348 nocrt: true,
349 }
350
351 cc_library {
352 name: "libvndk_sp_private",
353 vendor_available: false,
354 vndk: {
355 enabled: true,
356 support_system_process: true,
357 },
358 nocrt: true,
359 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900360 `, config)
Logan Chienf3511742017-10-31 18:04:35 +0800361
362 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "")
363 checkVndkModule(t, ctx, "libvndk_private", "vndk-VER", false, "")
364 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "")
365 checkVndkModule(t, ctx, "libvndk_sp_private", "vndk-sp-VER", true, "")
Inseob Kim1f086e22019-05-09 13:29:15 +0900366
367 // Check VNDK snapshot output.
368
369 snapshotDir := "vndk-snapshot"
370 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
371
372 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
373 "arm64", "armv8-a"))
374 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
375 "arm", "armv7-a-neon"))
376
377 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
378 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
379 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
380 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
381
382 variant := "android_arm64_armv8-a_vendor_shared"
383 variant2nd := "android_arm_armv7-a-neon_vendor_shared"
384
385 checkVndkSnapshot(t, ctx, "libvndk", vndkCoreLibPath, variant)
386 checkVndkSnapshot(t, ctx, "libvndk", vndkCoreLib2ndPath, variant2nd)
387 checkVndkSnapshot(t, ctx, "libvndk_sp", vndkSpLibPath, variant)
388 checkVndkSnapshot(t, ctx, "libvndk_sp", vndkSpLib2ndPath, variant2nd)
Logan Chienf3511742017-10-31 18:04:35 +0800389}
390
Logan Chiend3c59a22018-03-29 14:08:15 +0800391func TestVndkDepError(t *testing.T) {
392 // Check whether an error is emitted when a VNDK lib depends on a system lib.
393 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
394 cc_library {
395 name: "libvndk",
396 vendor_available: true,
397 vndk: {
398 enabled: true,
399 },
400 shared_libs: ["libfwk"], // Cause error
401 nocrt: true,
402 }
403
404 cc_library {
405 name: "libfwk",
406 nocrt: true,
407 }
408 `)
409
410 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
411 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
412 cc_library {
413 name: "libvndk",
414 vendor_available: true,
415 vndk: {
416 enabled: true,
417 },
418 shared_libs: ["libvendor"], // Cause error
419 nocrt: true,
420 }
421
422 cc_library {
423 name: "libvendor",
424 vendor: true,
425 nocrt: true,
426 }
427 `)
428
429 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
430 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
431 cc_library {
432 name: "libvndk_sp",
433 vendor_available: true,
434 vndk: {
435 enabled: true,
436 support_system_process: true,
437 },
438 shared_libs: ["libfwk"], // Cause error
439 nocrt: true,
440 }
441
442 cc_library {
443 name: "libfwk",
444 nocrt: true,
445 }
446 `)
447
448 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
449 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
450 cc_library {
451 name: "libvndk_sp",
452 vendor_available: true,
453 vndk: {
454 enabled: true,
455 support_system_process: true,
456 },
457 shared_libs: ["libvendor"], // Cause error
458 nocrt: true,
459 }
460
461 cc_library {
462 name: "libvendor",
463 vendor: true,
464 nocrt: true,
465 }
466 `)
467
468 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
469 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
470 cc_library {
471 name: "libvndk_sp",
472 vendor_available: true,
473 vndk: {
474 enabled: true,
475 support_system_process: true,
476 },
477 shared_libs: ["libvndk"], // Cause error
478 nocrt: true,
479 }
480
481 cc_library {
482 name: "libvndk",
483 vendor_available: true,
484 vndk: {
485 enabled: true,
486 },
487 nocrt: true,
488 }
489 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900490
491 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
492 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
493 cc_library {
494 name: "libvndk",
495 vendor_available: true,
496 vndk: {
497 enabled: true,
498 },
499 shared_libs: ["libnonvndk"],
500 nocrt: true,
501 }
502
503 cc_library {
504 name: "libnonvndk",
505 vendor_available: true,
506 nocrt: true,
507 }
508 `)
509
510 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
511 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
512 cc_library {
513 name: "libvndkprivate",
514 vendor_available: false,
515 vndk: {
516 enabled: true,
517 },
518 shared_libs: ["libnonvndk"],
519 nocrt: true,
520 }
521
522 cc_library {
523 name: "libnonvndk",
524 vendor_available: true,
525 nocrt: true,
526 }
527 `)
528
529 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
530 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
531 cc_library {
532 name: "libvndksp",
533 vendor_available: true,
534 vndk: {
535 enabled: true,
536 support_system_process: true,
537 },
538 shared_libs: ["libnonvndk"],
539 nocrt: true,
540 }
541
542 cc_library {
543 name: "libnonvndk",
544 vendor_available: true,
545 nocrt: true,
546 }
547 `)
548
549 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
550 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
551 cc_library {
552 name: "libvndkspprivate",
553 vendor_available: false,
554 vndk: {
555 enabled: true,
556 support_system_process: true,
557 },
558 shared_libs: ["libnonvndk"],
559 nocrt: true,
560 }
561
562 cc_library {
563 name: "libnonvndk",
564 vendor_available: true,
565 nocrt: true,
566 }
567 `)
568}
569
570func TestDoubleLoadbleDep(t *testing.T) {
571 // okay to link : LLNDK -> double_loadable VNDK
572 testCc(t, `
573 cc_library {
574 name: "libllndk",
575 shared_libs: ["libdoubleloadable"],
576 }
577
578 llndk_library {
579 name: "libllndk",
580 symbol_file: "",
581 }
582
583 cc_library {
584 name: "libdoubleloadable",
585 vendor_available: true,
586 vndk: {
587 enabled: true,
588 },
589 double_loadable: true,
590 }
591 `)
592 // okay to link : LLNDK -> VNDK-SP
593 testCc(t, `
594 cc_library {
595 name: "libllndk",
596 shared_libs: ["libvndksp"],
597 }
598
599 llndk_library {
600 name: "libllndk",
601 symbol_file: "",
602 }
603
604 cc_library {
605 name: "libvndksp",
606 vendor_available: true,
607 vndk: {
608 enabled: true,
609 support_system_process: true,
610 },
611 }
612 `)
613 // okay to link : double_loadable -> double_loadable
614 testCc(t, `
615 cc_library {
616 name: "libdoubleloadable1",
617 shared_libs: ["libdoubleloadable2"],
618 vendor_available: true,
619 double_loadable: true,
620 }
621
622 cc_library {
623 name: "libdoubleloadable2",
624 vendor_available: true,
625 double_loadable: true,
626 }
627 `)
628 // okay to link : double_loadable VNDK -> double_loadable VNDK private
629 testCc(t, `
630 cc_library {
631 name: "libdoubleloadable",
632 vendor_available: true,
633 vndk: {
634 enabled: true,
635 },
636 double_loadable: true,
637 shared_libs: ["libnondoubleloadable"],
638 }
639
640 cc_library {
641 name: "libnondoubleloadable",
642 vendor_available: false,
643 vndk: {
644 enabled: true,
645 },
646 double_loadable: true,
647 }
648 `)
649 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
650 testCc(t, `
651 cc_library {
652 name: "libllndk",
653 shared_libs: ["libcoreonly"],
654 }
655
656 llndk_library {
657 name: "libllndk",
658 symbol_file: "",
659 }
660
661 cc_library {
662 name: "libcoreonly",
663 shared_libs: ["libvendoravailable"],
664 }
665
666 // indirect dependency of LLNDK
667 cc_library {
668 name: "libvendoravailable",
669 vendor_available: true,
670 double_loadable: true,
671 }
672 `)
673}
674
675func TestDoubleLoadableDepError(t *testing.T) {
676 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
677 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
678 cc_library {
679 name: "libllndk",
680 shared_libs: ["libnondoubleloadable"],
681 }
682
683 llndk_library {
684 name: "libllndk",
685 symbol_file: "",
686 }
687
688 cc_library {
689 name: "libnondoubleloadable",
690 vendor_available: true,
691 vndk: {
692 enabled: true,
693 },
694 }
695 `)
696
697 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
698 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
699 cc_library {
700 name: "libllndk",
701 no_libgcc: true,
702 shared_libs: ["libnondoubleloadable"],
703 }
704
705 llndk_library {
706 name: "libllndk",
707 symbol_file: "",
708 }
709
710 cc_library {
711 name: "libnondoubleloadable",
712 vendor_available: true,
713 }
714 `)
715
716 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
717 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
718 cc_library {
719 name: "libdoubleloadable",
720 vendor_available: true,
721 double_loadable: true,
722 shared_libs: ["libnondoubleloadable"],
723 }
724
725 cc_library {
726 name: "libnondoubleloadable",
727 vendor_available: true,
728 }
729 `)
730
731 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
732 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
733 cc_library {
734 name: "libdoubleloadable",
735 vendor_available: true,
736 double_loadable: true,
737 shared_libs: ["libnondoubleloadable"],
738 }
739
740 cc_library {
741 name: "libnondoubleloadable",
742 vendor_available: true,
743 vndk: {
744 enabled: true,
745 },
746 }
747 `)
748
749 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
750 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
751 cc_library {
752 name: "libdoubleloadable",
753 vendor_available: true,
754 vndk: {
755 enabled: true,
756 },
757 double_loadable: true,
758 shared_libs: ["libnondoubleloadable"],
759 }
760
761 cc_library {
762 name: "libnondoubleloadable",
763 vendor_available: false,
764 vndk: {
765 enabled: true,
766 },
767 }
768 `)
769
770 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
771 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
772 cc_library {
773 name: "libllndk",
774 shared_libs: ["libcoreonly"],
775 }
776
777 llndk_library {
778 name: "libllndk",
779 symbol_file: "",
780 }
781
782 cc_library {
783 name: "libcoreonly",
784 shared_libs: ["libvendoravailable"],
785 }
786
787 // indirect dependency of LLNDK
788 cc_library {
789 name: "libvendoravailable",
790 vendor_available: true,
791 }
792 `)
Logan Chiend3c59a22018-03-29 14:08:15 +0800793}
794
Justin Yun9357f4a2018-11-28 15:14:47 +0900795func TestVndkMustNotBeProductSpecific(t *testing.T) {
796 // Check whether an error is emitted when a vndk lib has 'product_specific: true'.
797 testCcError(t, "product_specific must not be true when `vndk: {enabled: true}`", `
798 cc_library {
799 name: "libvndk",
800 product_specific: true, // Cause error
801 vendor_available: true,
802 vndk: {
803 enabled: true,
804 },
805 nocrt: true,
806 }
807 `)
808}
809
Logan Chienf3511742017-10-31 18:04:35 +0800810func TestVndkExt(t *testing.T) {
811 // This test checks the VNDK-Ext properties.
812 ctx := testCc(t, `
813 cc_library {
814 name: "libvndk",
815 vendor_available: true,
816 vndk: {
817 enabled: true,
818 },
819 nocrt: true,
820 }
821
822 cc_library {
823 name: "libvndk_ext",
824 vendor: true,
825 vndk: {
826 enabled: true,
827 extends: "libvndk",
828 },
829 nocrt: true,
830 }
831 `)
832
833 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk")
834}
835
Logan Chiend3c59a22018-03-29 14:08:15 +0800836func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +0800837 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
838 ctx := testCcNoVndk(t, `
839 cc_library {
840 name: "libvndk",
841 vendor_available: true,
842 vndk: {
843 enabled: true,
844 },
845 nocrt: true,
846 }
847
848 cc_library {
849 name: "libvndk_ext",
850 vendor: true,
851 vndk: {
852 enabled: true,
853 extends: "libvndk",
854 },
855 nocrt: true,
856 }
857 `)
858
859 // Ensures that the core variant of "libvndk_ext" can be found.
860 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
861 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
862 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
863 }
864}
865
866func TestVndkExtError(t *testing.T) {
867 // This test ensures an error is emitted in ill-formed vndk-ext definition.
868 testCcError(t, "must set `vendor: true` to set `extends: \".*\"`", `
869 cc_library {
870 name: "libvndk",
871 vendor_available: true,
872 vndk: {
873 enabled: true,
874 },
875 nocrt: true,
876 }
877
878 cc_library {
879 name: "libvndk_ext",
880 vndk: {
881 enabled: true,
882 extends: "libvndk",
883 },
884 nocrt: true,
885 }
886 `)
887
888 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
889 cc_library {
890 name: "libvndk",
891 vendor_available: true,
892 vndk: {
893 enabled: true,
894 },
895 nocrt: true,
896 }
897
898 cc_library {
899 name: "libvndk_ext",
900 vendor: true,
901 vndk: {
902 enabled: true,
903 },
904 nocrt: true,
905 }
906 `)
907}
908
909func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
910 // This test ensures an error is emitted for inconsistent support_system_process.
911 testCcError(t, "module \".*\" with mismatched support_system_process", `
912 cc_library {
913 name: "libvndk",
914 vendor_available: true,
915 vndk: {
916 enabled: true,
917 },
918 nocrt: true,
919 }
920
921 cc_library {
922 name: "libvndk_sp_ext",
923 vendor: true,
924 vndk: {
925 enabled: true,
926 extends: "libvndk",
927 support_system_process: true,
928 },
929 nocrt: true,
930 }
931 `)
932
933 testCcError(t, "module \".*\" with mismatched support_system_process", `
934 cc_library {
935 name: "libvndk_sp",
936 vendor_available: true,
937 vndk: {
938 enabled: true,
939 support_system_process: true,
940 },
941 nocrt: true,
942 }
943
944 cc_library {
945 name: "libvndk_ext",
946 vendor: true,
947 vndk: {
948 enabled: true,
949 extends: "libvndk_sp",
950 },
951 nocrt: true,
952 }
953 `)
954}
955
956func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800957 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +0800958 // with `vendor_available: false`.
959 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
960 cc_library {
961 name: "libvndk",
962 vendor_available: false,
963 vndk: {
964 enabled: true,
965 },
966 nocrt: true,
967 }
968
969 cc_library {
970 name: "libvndk_ext",
971 vendor: true,
972 vndk: {
973 enabled: true,
974 extends: "libvndk",
975 },
976 nocrt: true,
977 }
978 `)
979}
980
Logan Chiend3c59a22018-03-29 14:08:15 +0800981func TestVendorModuleUseVndkExt(t *testing.T) {
982 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +0800983 testCc(t, `
984 cc_library {
985 name: "libvndk",
986 vendor_available: true,
987 vndk: {
988 enabled: true,
989 },
990 nocrt: true,
991 }
992
993 cc_library {
994 name: "libvndk_ext",
995 vendor: true,
996 vndk: {
997 enabled: true,
998 extends: "libvndk",
999 },
1000 nocrt: true,
1001 }
1002
1003 cc_library {
1004
1005 name: "libvndk_sp",
1006 vendor_available: true,
1007 vndk: {
1008 enabled: true,
1009 support_system_process: true,
1010 },
1011 nocrt: true,
1012 }
1013
1014 cc_library {
1015 name: "libvndk_sp_ext",
1016 vendor: true,
1017 vndk: {
1018 enabled: true,
1019 extends: "libvndk_sp",
1020 support_system_process: true,
1021 },
1022 nocrt: true,
1023 }
1024
1025 cc_library {
1026 name: "libvendor",
1027 vendor: true,
1028 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1029 nocrt: true,
1030 }
1031 `)
1032}
1033
Logan Chiend3c59a22018-03-29 14:08:15 +08001034func TestVndkExtUseVendorLib(t *testing.T) {
1035 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001036 testCc(t, `
1037 cc_library {
1038 name: "libvndk",
1039 vendor_available: true,
1040 vndk: {
1041 enabled: true,
1042 },
1043 nocrt: true,
1044 }
1045
1046 cc_library {
1047 name: "libvndk_ext",
1048 vendor: true,
1049 vndk: {
1050 enabled: true,
1051 extends: "libvndk",
1052 },
1053 shared_libs: ["libvendor"],
1054 nocrt: true,
1055 }
1056
1057 cc_library {
1058 name: "libvendor",
1059 vendor: true,
1060 nocrt: true,
1061 }
1062 `)
Logan Chienf3511742017-10-31 18:04:35 +08001063
Logan Chiend3c59a22018-03-29 14:08:15 +08001064 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1065 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001066 cc_library {
1067 name: "libvndk_sp",
1068 vendor_available: true,
1069 vndk: {
1070 enabled: true,
1071 support_system_process: true,
1072 },
1073 nocrt: true,
1074 }
1075
1076 cc_library {
1077 name: "libvndk_sp_ext",
1078 vendor: true,
1079 vndk: {
1080 enabled: true,
1081 extends: "libvndk_sp",
1082 support_system_process: true,
1083 },
1084 shared_libs: ["libvendor"], // Cause an error
1085 nocrt: true,
1086 }
1087
1088 cc_library {
1089 name: "libvendor",
1090 vendor: true,
1091 nocrt: true,
1092 }
1093 `)
1094}
1095
Logan Chiend3c59a22018-03-29 14:08:15 +08001096func TestVndkSpExtUseVndkError(t *testing.T) {
1097 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1098 // library.
1099 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1100 cc_library {
1101 name: "libvndk",
1102 vendor_available: true,
1103 vndk: {
1104 enabled: true,
1105 },
1106 nocrt: true,
1107 }
1108
1109 cc_library {
1110 name: "libvndk_sp",
1111 vendor_available: true,
1112 vndk: {
1113 enabled: true,
1114 support_system_process: true,
1115 },
1116 nocrt: true,
1117 }
1118
1119 cc_library {
1120 name: "libvndk_sp_ext",
1121 vendor: true,
1122 vndk: {
1123 enabled: true,
1124 extends: "libvndk_sp",
1125 support_system_process: true,
1126 },
1127 shared_libs: ["libvndk"], // Cause an error
1128 nocrt: true,
1129 }
1130 `)
1131
1132 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1133 // library.
1134 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1135 cc_library {
1136 name: "libvndk",
1137 vendor_available: true,
1138 vndk: {
1139 enabled: true,
1140 },
1141 nocrt: true,
1142 }
1143
1144 cc_library {
1145 name: "libvndk_ext",
1146 vendor: true,
1147 vndk: {
1148 enabled: true,
1149 extends: "libvndk",
1150 },
1151 nocrt: true,
1152 }
1153
1154 cc_library {
1155 name: "libvndk_sp",
1156 vendor_available: true,
1157 vndk: {
1158 enabled: true,
1159 support_system_process: true,
1160 },
1161 nocrt: true,
1162 }
1163
1164 cc_library {
1165 name: "libvndk_sp_ext",
1166 vendor: true,
1167 vndk: {
1168 enabled: true,
1169 extends: "libvndk_sp",
1170 support_system_process: true,
1171 },
1172 shared_libs: ["libvndk_ext"], // Cause an error
1173 nocrt: true,
1174 }
1175 `)
1176}
1177
1178func TestVndkUseVndkExtError(t *testing.T) {
1179 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1180 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001181 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1182 cc_library {
1183 name: "libvndk",
1184 vendor_available: true,
1185 vndk: {
1186 enabled: true,
1187 },
1188 nocrt: true,
1189 }
1190
1191 cc_library {
1192 name: "libvndk_ext",
1193 vendor: true,
1194 vndk: {
1195 enabled: true,
1196 extends: "libvndk",
1197 },
1198 nocrt: true,
1199 }
1200
1201 cc_library {
1202 name: "libvndk2",
1203 vendor_available: true,
1204 vndk: {
1205 enabled: true,
1206 },
1207 shared_libs: ["libvndk_ext"],
1208 nocrt: true,
1209 }
1210 `)
1211
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001212 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001213 cc_library {
1214 name: "libvndk",
1215 vendor_available: true,
1216 vndk: {
1217 enabled: true,
1218 },
1219 nocrt: true,
1220 }
1221
1222 cc_library {
1223 name: "libvndk_ext",
1224 vendor: true,
1225 vndk: {
1226 enabled: true,
1227 extends: "libvndk",
1228 },
1229 nocrt: true,
1230 }
1231
1232 cc_library {
1233 name: "libvndk2",
1234 vendor_available: true,
1235 vndk: {
1236 enabled: true,
1237 },
1238 target: {
1239 vendor: {
1240 shared_libs: ["libvndk_ext"],
1241 },
1242 },
1243 nocrt: true,
1244 }
1245 `)
1246
1247 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1248 cc_library {
1249 name: "libvndk_sp",
1250 vendor_available: true,
1251 vndk: {
1252 enabled: true,
1253 support_system_process: true,
1254 },
1255 nocrt: true,
1256 }
1257
1258 cc_library {
1259 name: "libvndk_sp_ext",
1260 vendor: true,
1261 vndk: {
1262 enabled: true,
1263 extends: "libvndk_sp",
1264 support_system_process: true,
1265 },
1266 nocrt: true,
1267 }
1268
1269 cc_library {
1270 name: "libvndk_sp_2",
1271 vendor_available: true,
1272 vndk: {
1273 enabled: true,
1274 support_system_process: true,
1275 },
1276 shared_libs: ["libvndk_sp_ext"],
1277 nocrt: true,
1278 }
1279 `)
1280
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001281 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001282 cc_library {
1283 name: "libvndk_sp",
1284 vendor_available: true,
1285 vndk: {
1286 enabled: true,
1287 },
1288 nocrt: true,
1289 }
1290
1291 cc_library {
1292 name: "libvndk_sp_ext",
1293 vendor: true,
1294 vndk: {
1295 enabled: true,
1296 extends: "libvndk_sp",
1297 },
1298 nocrt: true,
1299 }
1300
1301 cc_library {
1302 name: "libvndk_sp2",
1303 vendor_available: true,
1304 vndk: {
1305 enabled: true,
1306 },
1307 target: {
1308 vendor: {
1309 shared_libs: ["libvndk_sp_ext"],
1310 },
1311 },
1312 nocrt: true,
1313 }
1314 `)
1315}
1316
Colin Cross0af4b842015-04-30 16:36:18 -07001317var (
1318 str11 = "01234567891"
1319 str10 = str11[:10]
1320 str9 = str11[:9]
1321 str5 = str11[:5]
1322 str4 = str11[:4]
1323)
1324
1325var splitListForSizeTestCases = []struct {
1326 in []string
1327 out [][]string
1328 size int
1329}{
1330 {
1331 in: []string{str10},
1332 out: [][]string{{str10}},
1333 size: 10,
1334 },
1335 {
1336 in: []string{str9},
1337 out: [][]string{{str9}},
1338 size: 10,
1339 },
1340 {
1341 in: []string{str5},
1342 out: [][]string{{str5}},
1343 size: 10,
1344 },
1345 {
1346 in: []string{str11},
1347 out: nil,
1348 size: 10,
1349 },
1350 {
1351 in: []string{str10, str10},
1352 out: [][]string{{str10}, {str10}},
1353 size: 10,
1354 },
1355 {
1356 in: []string{str9, str10},
1357 out: [][]string{{str9}, {str10}},
1358 size: 10,
1359 },
1360 {
1361 in: []string{str10, str9},
1362 out: [][]string{{str10}, {str9}},
1363 size: 10,
1364 },
1365 {
1366 in: []string{str5, str4},
1367 out: [][]string{{str5, str4}},
1368 size: 10,
1369 },
1370 {
1371 in: []string{str5, str4, str5},
1372 out: [][]string{{str5, str4}, {str5}},
1373 size: 10,
1374 },
1375 {
1376 in: []string{str5, str4, str5, str4},
1377 out: [][]string{{str5, str4}, {str5, str4}},
1378 size: 10,
1379 },
1380 {
1381 in: []string{str5, str4, str5, str5},
1382 out: [][]string{{str5, str4}, {str5}, {str5}},
1383 size: 10,
1384 },
1385 {
1386 in: []string{str5, str5, str5, str4},
1387 out: [][]string{{str5}, {str5}, {str5, str4}},
1388 size: 10,
1389 },
1390 {
1391 in: []string{str9, str11},
1392 out: nil,
1393 size: 10,
1394 },
1395 {
1396 in: []string{str11, str9},
1397 out: nil,
1398 size: 10,
1399 },
1400}
1401
1402func TestSplitListForSize(t *testing.T) {
1403 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08001404 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07001405
1406 var outStrings [][]string
1407
1408 if len(out) > 0 {
1409 outStrings = make([][]string, len(out))
1410 for i, o := range out {
1411 outStrings[i] = o.Strings()
1412 }
1413 }
1414
1415 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07001416 t.Errorf("incorrect output:")
1417 t.Errorf(" input: %#v", testCase.in)
1418 t.Errorf(" size: %d", testCase.size)
1419 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07001420 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07001421 }
1422 }
1423}
Jeff Gaston294356f2017-09-27 17:05:30 -07001424
1425var staticLinkDepOrderTestCases = []struct {
1426 // This is a string representation of a map[moduleName][]moduleDependency .
1427 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001428 inStatic string
1429
1430 // This is a string representation of a map[moduleName][]moduleDependency .
1431 // It models the dependencies declared in an Android.bp file.
1432 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07001433
1434 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
1435 // The keys of allOrdered specify which modules we would like to check.
1436 // The values of allOrdered specify the expected result (of the transitive closure of all
1437 // dependencies) for each module to test
1438 allOrdered string
1439
1440 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
1441 // The keys of outOrdered specify which modules we would like to check.
1442 // The values of outOrdered specify the expected result (of the ordered linker command line)
1443 // for each module to test.
1444 outOrdered string
1445}{
1446 // Simple tests
1447 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001448 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07001449 outOrdered: "",
1450 },
1451 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001452 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001453 outOrdered: "a:",
1454 },
1455 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001456 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001457 outOrdered: "a:b; b:",
1458 },
1459 // Tests of reordering
1460 {
1461 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001462 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001463 outOrdered: "a:b,c,d; b:d; c:d; d:",
1464 },
1465 {
1466 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001467 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001468 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
1469 },
1470 {
1471 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001472 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07001473 outOrdered: "a:d,b,e,c; d:b; e:c",
1474 },
1475 {
1476 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001477 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07001478 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
1479 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
1480 },
1481 {
1482 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001483 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 -07001484 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
1485 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
1486 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001487 // shared dependencies
1488 {
1489 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
1490 // So, we don't actually have to check that a shared dependency of c will change the order
1491 // of a library that depends statically on b and on c. We only need to check that if c has
1492 // a shared dependency on b, that that shows up in allOrdered.
1493 inShared: "c:b",
1494 allOrdered: "c:b",
1495 outOrdered: "c:",
1496 },
1497 {
1498 // This test doesn't actually include any shared dependencies but it's a reminder of what
1499 // the second phase of the above test would look like
1500 inStatic: "a:b,c; c:b",
1501 allOrdered: "a:c,b; c:b",
1502 outOrdered: "a:c,b; c:b",
1503 },
Jeff Gaston294356f2017-09-27 17:05:30 -07001504 // tiebreakers for when two modules specifying different orderings and there is no dependency
1505 // to dictate an order
1506 {
1507 // 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 -08001508 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07001509 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
1510 },
1511 {
1512 // 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 -08001513 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 -07001514 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
1515 },
1516 // Tests involving duplicate dependencies
1517 {
1518 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001519 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001520 outOrdered: "a:c,b",
1521 },
1522 {
1523 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001524 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001525 outOrdered: "a:d,c,b",
1526 },
1527 // Tests to confirm the nonexistence of infinite loops.
1528 // These cases should never happen, so as long as the test terminates and the
1529 // result is deterministic then that should be fine.
1530 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001531 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07001532 outOrdered: "a:a",
1533 },
1534 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001535 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07001536 allOrdered: "a:b,c; b:c,a; c:a,b",
1537 outOrdered: "a:b; b:c; c:a",
1538 },
1539 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001540 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001541 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
1542 outOrdered: "a:c,b; b:a,c; c:b,a",
1543 },
1544}
1545
1546// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
1547func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
1548 // convert from "a:b,c; d:e" to "a:b,c;d:e"
1549 strippedText := strings.Replace(text, " ", "", -1)
1550 if len(strippedText) < 1 {
1551 return []android.Path{}, make(map[android.Path][]android.Path, 0)
1552 }
1553 allDeps = make(map[android.Path][]android.Path, 0)
1554
1555 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
1556 moduleTexts := strings.Split(strippedText, ";")
1557
1558 outputForModuleName := func(moduleName string) android.Path {
1559 return android.PathForTesting(moduleName)
1560 }
1561
1562 for _, moduleText := range moduleTexts {
1563 // convert from "a:b,c" to ["a", "b,c"]
1564 components := strings.Split(moduleText, ":")
1565 if len(components) != 2 {
1566 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
1567 }
1568 moduleName := components[0]
1569 moduleOutput := outputForModuleName(moduleName)
1570 modulesInOrder = append(modulesInOrder, moduleOutput)
1571
1572 depString := components[1]
1573 // convert from "b,c" to ["b", "c"]
1574 depNames := strings.Split(depString, ",")
1575 if len(depString) < 1 {
1576 depNames = []string{}
1577 }
1578 var deps []android.Path
1579 for _, depName := range depNames {
1580 deps = append(deps, outputForModuleName(depName))
1581 }
1582 allDeps[moduleOutput] = deps
1583 }
1584 return modulesInOrder, allDeps
1585}
1586
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001587func TestLinkReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001588 for _, testCase := range staticLinkDepOrderTestCases {
1589 errs := []string{}
1590
1591 // parse testcase
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001592 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)
Jeff Gaston294356f2017-09-27 17:05:30 -07001593 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)
1594 if testCase.allOrdered == "" {
1595 // allow the test case to skip specifying allOrdered
1596 testCase.allOrdered = testCase.outOrdered
1597 }
1598 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001599 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)
Jeff Gaston294356f2017-09-27 17:05:30 -07001600
1601 // For each module whose post-reordered dependencies were specified, validate that
1602 // reordering the inputs produces the expected outputs.
1603 for _, moduleName := range expectedModuleNames {
1604 moduleDeps := givenTransitiveDeps[moduleName]
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001605 givenSharedDeps := givenAllSharedDeps[moduleName]
1606 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -07001607
1608 correctAllOrdered := expectedAllDeps[moduleName]
1609 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {
1610 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001611 "\nin static:%q"+
1612 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07001613 "\nmodule: %v"+
1614 "\nexpected: %s"+
1615 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001616 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07001617 }
1618
1619 correctOutputDeps := expectedTransitiveDeps[moduleName]
1620 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {
1621 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001622 "\nin static:%q"+
1623 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07001624 "\nmodule: %v"+
1625 "\nexpected: %s"+
1626 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001627 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07001628 }
1629 }
1630
1631 if len(errs) > 0 {
1632 sort.Strings(errs)
1633 for _, err := range errs {
1634 t.Error(err)
1635 }
1636 }
1637 }
1638}
Logan Chienf3511742017-10-31 18:04:35 +08001639
Jeff Gaston294356f2017-09-27 17:05:30 -07001640func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
1641 for _, moduleName := range moduleNames {
1642 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
1643 output := module.outputFile.Path()
1644 paths = append(paths, output)
1645 }
1646 return paths
1647}
1648
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001649func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001650 ctx := testCc(t, `
1651 cc_library {
1652 name: "a",
1653 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09001654 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001655 }
1656 cc_library {
1657 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09001658 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001659 }
1660 cc_library {
1661 name: "c",
1662 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09001663 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001664 }
1665 cc_library {
1666 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09001667 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001668 }
1669
1670 `)
1671
1672 variant := "android_arm64_armv8-a_core_static"
1673 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001674 actual := moduleA.depsInLinkOrder
Jeff Gaston294356f2017-09-27 17:05:30 -07001675 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})
1676
1677 if !reflect.DeepEqual(actual, expected) {
1678 t.Errorf("staticDeps orderings were not propagated correctly"+
1679 "\nactual: %v"+
1680 "\nexpected: %v",
1681 actual,
1682 expected,
1683 )
1684 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09001685}
Jeff Gaston294356f2017-09-27 17:05:30 -07001686
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001687func TestStaticLibDepReorderingWithShared(t *testing.T) {
1688 ctx := testCc(t, `
1689 cc_library {
1690 name: "a",
1691 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09001692 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001693 }
1694 cc_library {
1695 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09001696 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001697 }
1698 cc_library {
1699 name: "c",
1700 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09001701 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001702 }
1703
1704 `)
1705
1706 variant := "android_arm64_armv8-a_core_static"
1707 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
1708 actual := moduleA.depsInLinkOrder
1709 expected := getOutputPaths(ctx, variant, []string{"c", "b"})
1710
1711 if !reflect.DeepEqual(actual, expected) {
1712 t.Errorf("staticDeps orderings did not account for shared libs"+
1713 "\nactual: %v"+
1714 "\nexpected: %v",
1715 actual,
1716 expected,
1717 )
1718 }
1719}
1720
Jiyong Parka46a4d52017-12-14 19:54:34 +09001721func TestLlndkHeaders(t *testing.T) {
1722 ctx := testCc(t, `
1723 llndk_headers {
1724 name: "libllndk_headers",
1725 export_include_dirs: ["my_include"],
1726 }
1727 llndk_library {
1728 name: "libllndk",
1729 export_llndk_headers: ["libllndk_headers"],
1730 }
1731 cc_library {
1732 name: "libvendor",
1733 shared_libs: ["libllndk"],
1734 vendor: true,
1735 srcs: ["foo.c"],
Logan Chienf3511742017-10-31 18:04:35 +08001736 no_libgcc: true,
1737 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09001738 }
1739 `)
1740
1741 // _static variant is used since _shared reuses *.o from the static variant
1742 cc := ctx.ModuleForTests("libvendor", "android_arm_armv7-a-neon_vendor_static").Rule("cc")
1743 cflags := cc.Args["cFlags"]
1744 if !strings.Contains(cflags, "-Imy_include") {
1745 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
1746 }
1747}
1748
Logan Chien43d34c32017-12-20 01:17:32 +08001749func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
1750 actual := module.Properties.AndroidMkRuntimeLibs
1751 if !reflect.DeepEqual(actual, expected) {
1752 t.Errorf("incorrect runtime_libs for shared libs"+
1753 "\nactual: %v"+
1754 "\nexpected: %v",
1755 actual,
1756 expected,
1757 )
1758 }
1759}
1760
1761const runtimeLibAndroidBp = `
1762 cc_library {
1763 name: "libvendor_available1",
1764 vendor_available: true,
1765 no_libgcc : true,
1766 nocrt : true,
1767 system_shared_libs : [],
1768 }
1769 cc_library {
1770 name: "libvendor_available2",
1771 vendor_available: true,
1772 runtime_libs: ["libvendor_available1"],
1773 no_libgcc : true,
1774 nocrt : true,
1775 system_shared_libs : [],
1776 }
1777 cc_library {
1778 name: "libvendor_available3",
1779 vendor_available: true,
1780 runtime_libs: ["libvendor_available1"],
1781 target: {
1782 vendor: {
1783 exclude_runtime_libs: ["libvendor_available1"],
1784 }
1785 },
1786 no_libgcc : true,
1787 nocrt : true,
1788 system_shared_libs : [],
1789 }
1790 cc_library {
1791 name: "libcore",
1792 runtime_libs: ["libvendor_available1"],
1793 no_libgcc : true,
1794 nocrt : true,
1795 system_shared_libs : [],
1796 }
1797 cc_library {
1798 name: "libvendor1",
1799 vendor: true,
1800 no_libgcc : true,
1801 nocrt : true,
1802 system_shared_libs : [],
1803 }
1804 cc_library {
1805 name: "libvendor2",
1806 vendor: true,
1807 runtime_libs: ["libvendor_available1", "libvendor1"],
1808 no_libgcc : true,
1809 nocrt : true,
1810 system_shared_libs : [],
1811 }
1812`
1813
1814func TestRuntimeLibs(t *testing.T) {
1815 ctx := testCc(t, runtimeLibAndroidBp)
1816
1817 // runtime_libs for core variants use the module names without suffixes.
1818 variant := "android_arm64_armv8-a_core_shared"
1819
1820 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
1821 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1822
1823 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
1824 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1825
1826 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
1827 // and vendor variants.
1828 variant = "android_arm64_armv8-a_vendor_shared"
1829
1830 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
1831 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
1832
1833 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
1834 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
1835}
1836
1837func TestExcludeRuntimeLibs(t *testing.T) {
1838 ctx := testCc(t, runtimeLibAndroidBp)
1839
1840 variant := "android_arm64_armv8-a_core_shared"
1841 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
1842 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1843
1844 variant = "android_arm64_armv8-a_vendor_shared"
1845 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
1846 checkRuntimeLibs(t, nil, module)
1847}
1848
1849func TestRuntimeLibsNoVndk(t *testing.T) {
1850 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
1851
1852 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
1853
1854 variant := "android_arm64_armv8-a_core_shared"
1855
1856 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
1857 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
1858
1859 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
1860 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
1861}
1862
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00001863func checkStaticLibs(t *testing.T, expected []string, module *Module) {
1864 actual := module.Properties.AndroidMkStaticLibs
1865 if !reflect.DeepEqual(actual, expected) {
1866 t.Errorf("incorrect static_libs"+
1867 "\nactual: %v"+
1868 "\nexpected: %v",
1869 actual,
1870 expected,
1871 )
1872 }
1873}
1874
1875const staticLibAndroidBp = `
1876 cc_library {
1877 name: "lib1",
1878 }
1879 cc_library {
1880 name: "lib2",
1881 static_libs: ["lib1"],
1882 }
1883`
1884
1885func TestStaticLibDepExport(t *testing.T) {
1886 ctx := testCc(t, staticLibAndroidBp)
1887
1888 // Check the shared version of lib2.
1889 variant := "android_arm64_armv8-a_core_shared"
1890 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Yi Kongacee27c2019-03-29 20:05:14 -07001891 checkStaticLibs(t, []string{"lib1", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc_stripped"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00001892
1893 // Check the static version of lib2.
1894 variant = "android_arm64_armv8-a_core_static"
1895 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
1896 // libc++_static is linked additionally.
Yi Kongacee27c2019-03-29 20:05:14 -07001897 checkStaticLibs(t, []string{"lib1", "libc++_static", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc_stripped"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00001898}
1899
Jiyong Parkd08b6972017-09-26 10:50:54 +09001900var compilerFlagsTestCases = []struct {
1901 in string
1902 out bool
1903}{
1904 {
1905 in: "a",
1906 out: false,
1907 },
1908 {
1909 in: "-a",
1910 out: true,
1911 },
1912 {
1913 in: "-Ipath/to/something",
1914 out: false,
1915 },
1916 {
1917 in: "-isystempath/to/something",
1918 out: false,
1919 },
1920 {
1921 in: "--coverage",
1922 out: false,
1923 },
1924 {
1925 in: "-include a/b",
1926 out: true,
1927 },
1928 {
1929 in: "-include a/b c/d",
1930 out: false,
1931 },
1932 {
1933 in: "-DMACRO",
1934 out: true,
1935 },
1936 {
1937 in: "-DMAC RO",
1938 out: false,
1939 },
1940 {
1941 in: "-a -b",
1942 out: false,
1943 },
1944 {
1945 in: "-DMACRO=definition",
1946 out: true,
1947 },
1948 {
1949 in: "-DMACRO=defi nition",
1950 out: true, // TODO(jiyong): this should be false
1951 },
1952 {
1953 in: "-DMACRO(x)=x + 1",
1954 out: true,
1955 },
1956 {
1957 in: "-DMACRO=\"defi nition\"",
1958 out: true,
1959 },
1960}
1961
1962type mockContext struct {
1963 BaseModuleContext
1964 result bool
1965}
1966
1967func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1968 // CheckBadCompilerFlags calls this function when the flag should be rejected
1969 ctx.result = false
1970}
1971
1972func TestCompilerFlags(t *testing.T) {
1973 for _, testCase := range compilerFlagsTestCases {
1974 ctx := &mockContext{result: true}
1975 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
1976 if ctx.result != testCase.out {
1977 t.Errorf("incorrect output:")
1978 t.Errorf(" input: %#v", testCase.in)
1979 t.Errorf(" expected: %#v", testCase.out)
1980 t.Errorf(" got: %#v", ctx.result)
1981 }
1982 }
Jeff Gaston294356f2017-09-27 17:05:30 -07001983}
Jiyong Park374510b2018-03-19 18:23:01 +09001984
1985func TestVendorPublicLibraries(t *testing.T) {
1986 ctx := testCc(t, `
1987 cc_library_headers {
1988 name: "libvendorpublic_headers",
1989 export_include_dirs: ["my_include"],
1990 }
1991 vendor_public_library {
1992 name: "libvendorpublic",
1993 symbol_file: "",
1994 export_public_headers: ["libvendorpublic_headers"],
1995 }
1996 cc_library {
1997 name: "libvendorpublic",
1998 srcs: ["foo.c"],
1999 vendor: true,
2000 no_libgcc: true,
2001 nocrt: true,
2002 }
2003
2004 cc_library {
2005 name: "libsystem",
2006 shared_libs: ["libvendorpublic"],
2007 vendor: false,
2008 srcs: ["foo.c"],
2009 no_libgcc: true,
2010 nocrt: true,
2011 }
2012 cc_library {
2013 name: "libvendor",
2014 shared_libs: ["libvendorpublic"],
2015 vendor: true,
2016 srcs: ["foo.c"],
2017 no_libgcc: true,
2018 nocrt: true,
2019 }
2020 `)
2021
2022 variant := "android_arm64_armv8-a_core_shared"
2023
2024 // test if header search paths are correctly added
2025 // _static variant is used since _shared reuses *.o from the static variant
2026 cc := ctx.ModuleForTests("libsystem", strings.Replace(variant, "_shared", "_static", 1)).Rule("cc")
2027 cflags := cc.Args["cFlags"]
2028 if !strings.Contains(cflags, "-Imy_include") {
2029 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
2030 }
2031
2032 // test if libsystem is linked to the stub
2033 ld := ctx.ModuleForTests("libsystem", variant).Rule("ld")
2034 libflags := ld.Args["libFlags"]
2035 stubPaths := getOutputPaths(ctx, variant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
2036 if !strings.Contains(libflags, stubPaths[0].String()) {
2037 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
2038 }
2039
2040 // test if libvendor is linked to the real shared lib
2041 ld = ctx.ModuleForTests("libvendor", strings.Replace(variant, "_core", "_vendor", 1)).Rule("ld")
2042 libflags = ld.Args["libFlags"]
2043 stubPaths = getOutputPaths(ctx, strings.Replace(variant, "_core", "_vendor", 1), []string{"libvendorpublic"})
2044 if !strings.Contains(libflags, stubPaths[0].String()) {
2045 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
2046 }
2047
2048}
Jiyong Park37b25202018-07-11 10:49:27 +09002049
2050func TestRecovery(t *testing.T) {
2051 ctx := testCc(t, `
2052 cc_library_shared {
2053 name: "librecovery",
2054 recovery: true,
2055 }
2056 cc_library_shared {
2057 name: "librecovery32",
2058 recovery: true,
2059 compile_multilib:"32",
2060 }
Jiyong Park5baac542018-08-28 09:55:37 +09002061 cc_library_shared {
2062 name: "libHalInRecovery",
2063 recovery_available: true,
2064 vendor: true,
2065 }
Jiyong Park37b25202018-07-11 10:49:27 +09002066 `)
2067
2068 variants := ctx.ModuleVariantsForTests("librecovery")
2069 const arm64 = "android_arm64_armv8-a_recovery_shared"
2070 if len(variants) != 1 || !android.InList(arm64, variants) {
2071 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
2072 }
2073
2074 variants = ctx.ModuleVariantsForTests("librecovery32")
2075 if android.InList(arm64, variants) {
2076 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
2077 }
Jiyong Park5baac542018-08-28 09:55:37 +09002078
2079 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
2080 if !recoveryModule.Platform() {
2081 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
2082 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09002083}
Jiyong Park5baac542018-08-28 09:55:37 +09002084
Jiyong Park7ed9de32018-10-15 22:25:07 +09002085func TestVersionedStubs(t *testing.T) {
2086 ctx := testCc(t, `
2087 cc_library_shared {
2088 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002089 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002090 stubs: {
2091 symbol_file: "foo.map.txt",
2092 versions: ["1", "2", "3"],
2093 },
2094 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002095
Jiyong Park7ed9de32018-10-15 22:25:07 +09002096 cc_library_shared {
2097 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002098 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002099 shared_libs: ["libFoo#1"],
2100 }`)
2101
2102 variants := ctx.ModuleVariantsForTests("libFoo")
2103 expectedVariants := []string{
2104 "android_arm64_armv8-a_core_shared",
2105 "android_arm64_armv8-a_core_shared_1",
2106 "android_arm64_armv8-a_core_shared_2",
2107 "android_arm64_armv8-a_core_shared_3",
2108 "android_arm_armv7-a-neon_core_shared",
2109 "android_arm_armv7-a-neon_core_shared_1",
2110 "android_arm_armv7-a-neon_core_shared_2",
2111 "android_arm_armv7-a-neon_core_shared_3",
2112 }
2113 variantsMismatch := false
2114 if len(variants) != len(expectedVariants) {
2115 variantsMismatch = true
2116 } else {
2117 for _, v := range expectedVariants {
2118 if !inList(v, variants) {
2119 variantsMismatch = false
2120 }
2121 }
2122 }
2123 if variantsMismatch {
2124 t.Errorf("variants of libFoo expected:\n")
2125 for _, v := range expectedVariants {
2126 t.Errorf("%q\n", v)
2127 }
2128 t.Errorf(", but got:\n")
2129 for _, v := range variants {
2130 t.Errorf("%q\n", v)
2131 }
2132 }
2133
2134 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_core_shared").Rule("ld")
2135 libFlags := libBarLinkRule.Args["libFlags"]
2136 libFoo1StubPath := "libFoo/android_arm64_armv8-a_core_shared_1/libFoo.so"
2137 if !strings.Contains(libFlags, libFoo1StubPath) {
2138 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
2139 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002140
2141 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_core_shared").Rule("cc")
2142 cFlags := libBarCompileRule.Args["cFlags"]
2143 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
2144 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
2145 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
2146 }
Jiyong Park37b25202018-07-11 10:49:27 +09002147}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002148
2149func TestStaticExecutable(t *testing.T) {
2150 ctx := testCc(t, `
2151 cc_binary {
2152 name: "static_test",
2153 srcs: ["foo.c"],
2154 static_executable: true,
2155 }`)
2156
2157 variant := "android_arm64_armv8-a_core"
2158 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
2159 libFlags := binModuleRule.Args["libFlags"]
2160 systemStaticLibs := []string{"libc.a", "libm.a", "libdl.a"}
2161 for _, lib := range systemStaticLibs {
2162 if !strings.Contains(libFlags, lib) {
2163 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
2164 }
2165 }
2166 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
2167 for _, lib := range systemSharedLibs {
2168 if strings.Contains(libFlags, lib) {
2169 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
2170 }
2171 }
2172}
Jiyong Parke4bb9862019-02-01 00:31:10 +09002173
2174func TestStaticDepsOrderWithStubs(t *testing.T) {
2175 ctx := testCc(t, `
2176 cc_binary {
2177 name: "mybin",
2178 srcs: ["foo.c"],
2179 static_libs: ["libB"],
2180 static_executable: true,
2181 stl: "none",
2182 }
2183
2184 cc_library {
2185 name: "libB",
2186 srcs: ["foo.c"],
2187 shared_libs: ["libC"],
2188 stl: "none",
2189 }
2190
2191 cc_library {
2192 name: "libC",
2193 srcs: ["foo.c"],
2194 stl: "none",
2195 stubs: {
2196 versions: ["1"],
2197 },
2198 }`)
2199
2200 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a_core").Module().(*Module)
2201 actual := mybin.depsInLinkOrder
2202 expected := getOutputPaths(ctx, "android_arm64_armv8-a_core_static", []string{"libB", "libC"})
2203
2204 if !reflect.DeepEqual(actual, expected) {
2205 t.Errorf("staticDeps orderings were not propagated correctly"+
2206 "\nactual: %v"+
2207 "\nexpected: %v",
2208 actual,
2209 expected,
2210 )
2211 }
2212}