blob: 041c4a388241ea4233a7af5da74732b296b1be7b [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 (
Jeff Gaston294356f2017-09-27 17:05:30 -070018 "fmt"
Jiyong Park6a43f042017-10-12 23:05:00 +090019 "io/ioutil"
20 "os"
Inseob Kim1f086e22019-05-09 13:29:15 +090021 "path/filepath"
Colin Cross74d1ec02015-04-28 13:30:13 -070022 "reflect"
Jeff Gaston294356f2017-09-27 17:05:30 -070023 "sort"
24 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070025 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070026
27 "android/soong/android"
Colin Cross74d1ec02015-04-28 13:30:13 -070028)
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 Cross98be1bb2019-12-13 20:41:13 -080055func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070056 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080057 ctx := CreateTestContext()
58 ctx.Register(config)
Logan Chienf3511742017-10-31 18:04:35 +080059
Jeff Gastond3e141d2017-08-08 17:46:01 -070060 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +080061 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +090062 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +080063 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +090064
65 return ctx
66}
67
Logan Chienf3511742017-10-31 18:04:35 +080068func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080069 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080070 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070071 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
72 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +080073
Colin Cross98be1bb2019-12-13 20:41:13 -080074 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080075}
76
77func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080078 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080079 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070080 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +080081
Colin Cross98be1bb2019-12-13 20:41:13 -080082 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080083}
84
Justin Yun5f7f7e82019-11-18 19:52:14 +090085func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +080086 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +080087
Colin Cross98be1bb2019-12-13 20:41:13 -080088 ctx := CreateTestContext()
89 ctx.Register(config)
Logan Chienf3511742017-10-31 18:04:35 +080090
91 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
92 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +080093 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +080094 return
95 }
96
97 _, errs = ctx.PrepareBuildActions(config)
98 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +080099 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800100 return
101 }
102
103 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
104}
105
Justin Yun5f7f7e82019-11-18 19:52:14 +0900106func testCcError(t *testing.T, pattern string, bp string) {
107 config := TestConfig(buildDir, android.Android, nil, bp, nil)
108 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
109 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
110 testCcErrorWithConfig(t, pattern, config)
111 return
112}
113
114func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
115 config := TestConfig(buildDir, android.Android, nil, bp, nil)
116 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
117 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
118 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
119 testCcErrorWithConfig(t, pattern, config)
120 return
121}
122
Logan Chienf3511742017-10-31 18:04:35 +0800123const (
Colin Cross7113d202019-11-20 16:39:12 -0800124 coreVariant = "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800125 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun5f7f7e82019-11-18 19:52:14 +0900126 productVariant = "android_product.VER_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800127 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800128)
129
Doug Hornc32c6b02019-01-17 14:44:05 -0800130func TestFuchsiaDeps(t *testing.T) {
131 t.Helper()
132
133 bp := `
134 cc_library {
135 name: "libTest",
136 srcs: ["foo.c"],
137 target: {
138 fuchsia: {
139 srcs: ["bar.c"],
140 },
141 },
142 }`
143
Colin Cross98be1bb2019-12-13 20:41:13 -0800144 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
145 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800146
147 rt := false
148 fb := false
149
150 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
151 implicits := ld.Implicits
152 for _, lib := range implicits {
153 if strings.Contains(lib.Rel(), "libcompiler_rt") {
154 rt = true
155 }
156
157 if strings.Contains(lib.Rel(), "libbioniccompat") {
158 fb = true
159 }
160 }
161
162 if !rt || !fb {
163 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
164 }
165}
166
167func TestFuchsiaTargetDecl(t *testing.T) {
168 t.Helper()
169
170 bp := `
171 cc_library {
172 name: "libTest",
173 srcs: ["foo.c"],
174 target: {
175 fuchsia: {
176 srcs: ["bar.c"],
177 },
178 },
179 }`
180
Colin Cross98be1bb2019-12-13 20:41:13 -0800181 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
182 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800183 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
184 var objs []string
185 for _, o := range ld.Inputs {
186 objs = append(objs, o.Base())
187 }
188 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
189 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
190 }
191}
192
Jiyong Park6a43f042017-10-12 23:05:00 +0900193func TestVendorSrc(t *testing.T) {
194 ctx := testCc(t, `
195 cc_library {
196 name: "libTest",
197 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700198 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800199 nocrt: true,
200 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900201 vendor_available: true,
202 target: {
203 vendor: {
204 srcs: ["bar.c"],
205 },
206 },
207 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900208 `)
209
Logan Chienf3511742017-10-31 18:04:35 +0800210 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900211 var objs []string
212 for _, o := range ld.Inputs {
213 objs = append(objs, o.Base())
214 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800215 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900216 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
217 }
218}
219
Logan Chienf3511742017-10-31 18:04:35 +0800220func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900221 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800222
Logan Chiend3c59a22018-03-29 14:08:15 +0800223 t.Helper()
224
Justin Yun0ecf0b22020-02-28 15:07:59 +0900225 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Ivan Lozano52767be2019-10-18 14:49:46 -0700226 if !mod.HasVendorVariant() {
Justin Yun0ecf0b22020-02-28 15:07:59 +0900227 t.Errorf("%q must have variant %q", name, variant)
Logan Chienf3511742017-10-31 18:04:35 +0800228 }
229
230 // Check library properties.
231 lib, ok := mod.compiler.(*libraryDecorator)
232 if !ok {
233 t.Errorf("%q must have libraryDecorator", name)
234 } else if lib.baseInstaller.subDir != subDir {
235 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
236 lib.baseInstaller.subDir)
237 }
238
239 // Check VNDK properties.
240 if mod.vndkdep == nil {
241 t.Fatalf("%q must have `vndkdep`", name)
242 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700243 if !mod.IsVndk() {
244 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800245 }
246 if mod.isVndkSp() != isVndkSp {
247 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
248 }
249
250 // Check VNDK extension properties.
251 isVndkExt := extends != ""
252 if mod.isVndkExt() != isVndkExt {
253 t.Errorf("%q isVndkExt() must equal to %t", name, isVndkExt)
254 }
255
256 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
257 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
258 }
259}
260
Inseob Kim7f283f42020-06-01 21:53:49 +0900261func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Jooyung Han39edb6c2019-11-06 16:53:07 +0900262 mod, ok := ctx.ModuleForTests(moduleName, variant).Module().(android.OutputFileProducer)
263 if !ok {
264 t.Errorf("%q must have output\n", moduleName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900265 return
266 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900267 outputFiles, err := mod.OutputFiles("")
268 if err != nil || len(outputFiles) != 1 {
269 t.Errorf("%q must have single output\n", moduleName)
270 return
271 }
272 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900273
Inseob Kim7f283f42020-06-01 21:53:49 +0900274 out := singleton.Output(snapshotPath)
Jooyung Han39edb6c2019-11-06 16:53:07 +0900275 if out.Input.String() != outputFiles[0].String() {
Inseob Kim8471cda2019-11-15 09:59:12 +0900276 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
Inseob Kim1f086e22019-05-09 13:29:15 +0900277 }
278}
279
Jooyung Han2216fb12019-11-06 16:46:15 +0900280func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
281 t.Helper()
282 assertString(t, params.Rule.String(), android.WriteFile.String())
283 actual := strings.FieldsFunc(strings.ReplaceAll(params.Args["content"], "\\n", "\n"), func(r rune) bool { return r == '\n' })
284 assertArrayString(t, actual, expected)
285}
286
Jooyung Han097087b2019-10-22 19:32:18 +0900287func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
288 t.Helper()
289 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900290 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
291}
292
293func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
294 t.Helper()
295 vndkLibraries := ctx.ModuleForTests(module, "")
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +0900296
297 var output string
298 if module != "vndkcorevariant.libraries.txt" {
299 output = insertVndkVersion(module, "VER")
300 } else {
301 output = module
302 }
303
Jooyung Han2216fb12019-11-06 16:46:15 +0900304 checkWriteFileOutput(t, vndkLibraries.Output(output), expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900305}
306
Logan Chienf3511742017-10-31 18:04:35 +0800307func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800308 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800309 cc_library {
310 name: "libvndk",
311 vendor_available: true,
312 vndk: {
313 enabled: true,
314 },
315 nocrt: true,
316 }
317
318 cc_library {
319 name: "libvndk_private",
320 vendor_available: false,
321 vndk: {
322 enabled: true,
323 },
324 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900325 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800326 }
327
328 cc_library {
329 name: "libvndk_sp",
330 vendor_available: true,
331 vndk: {
332 enabled: true,
333 support_system_process: true,
334 },
335 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900336 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800337 }
338
339 cc_library {
340 name: "libvndk_sp_private",
341 vendor_available: false,
342 vndk: {
343 enabled: true,
344 support_system_process: true,
345 },
346 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900347 target: {
348 vendor: {
349 suffix: "-x",
350 },
351 },
Logan Chienf3511742017-10-31 18:04:35 +0800352 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900353 vndk_libraries_txt {
354 name: "llndk.libraries.txt",
355 }
356 vndk_libraries_txt {
357 name: "vndkcore.libraries.txt",
358 }
359 vndk_libraries_txt {
360 name: "vndksp.libraries.txt",
361 }
362 vndk_libraries_txt {
363 name: "vndkprivate.libraries.txt",
364 }
365 vndk_libraries_txt {
366 name: "vndkcorevariant.libraries.txt",
367 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800368 `
369
370 config := TestConfig(buildDir, android.Android, nil, bp, nil)
371 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
372 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
373
374 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800375
Justin Yun0ecf0b22020-02-28 15:07:59 +0900376 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "", vendorVariant)
377 checkVndkModule(t, ctx, "libvndk_private", "vndk-VER", false, "", vendorVariant)
378 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "", vendorVariant)
379 checkVndkModule(t, ctx, "libvndk_sp_private", "vndk-sp-VER", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900380
381 // Check VNDK snapshot output.
382
383 snapshotDir := "vndk-snapshot"
384 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
385
386 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
387 "arm64", "armv8-a"))
388 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
389 "arm", "armv7-a-neon"))
390
391 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
392 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
393 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
394 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
395
Colin Crossfb0c16e2019-11-20 17:12:35 -0800396 variant := "android_vendor.VER_arm64_armv8-a_shared"
397 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900398
Inseob Kim7f283f42020-06-01 21:53:49 +0900399 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
400
401 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
402 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
403 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
404 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900405
Jooyung Han39edb6c2019-11-06 16:53:07 +0900406 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900407 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
408 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
409 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
410 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900411
Jooyung Han097087b2019-10-22 19:32:18 +0900412 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
413 "LLNDK: libc.so",
414 "LLNDK: libdl.so",
415 "LLNDK: libft2.so",
416 "LLNDK: libm.so",
417 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900418 "VNDK-SP: libvndk_sp-x.so",
419 "VNDK-SP: libvndk_sp_private-x.so",
420 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900421 "VNDK-core: libvndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900422 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900423 "VNDK-private: libvndk-private.so",
424 "VNDK-private: libvndk_sp_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900425 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900426 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
427 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so"})
428 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so"})
429 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so"})
430 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
431}
432
Yo Chiangbba545e2020-06-09 16:15:37 +0800433func TestVndkWithHostSupported(t *testing.T) {
434 ctx := testCc(t, `
435 cc_library {
436 name: "libvndk_host_supported",
437 vendor_available: true,
438 vndk: {
439 enabled: true,
440 },
441 host_supported: true,
442 }
443
444 cc_library {
445 name: "libvndk_host_supported_but_disabled_on_device",
446 vendor_available: true,
447 vndk: {
448 enabled: true,
449 },
450 host_supported: true,
451 enabled: false,
452 target: {
453 host: {
454 enabled: true,
455 }
456 }
457 }
458
459 vndk_libraries_txt {
460 name: "vndkcore.libraries.txt",
461 }
462 `)
463
464 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
465}
466
Jooyung Han2216fb12019-11-06 16:46:15 +0900467func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800468 bp := `
Jooyung Han2216fb12019-11-06 16:46:15 +0900469 vndk_libraries_txt {
470 name: "llndk.libraries.txt",
Colin Cross98be1bb2019-12-13 20:41:13 -0800471 }`
472 config := TestConfig(buildDir, android.Android, nil, bp, nil)
473 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
474 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
475 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900476
477 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900478 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900479 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900480}
481
482func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800483 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900484 cc_library {
485 name: "libvndk",
486 vendor_available: true,
487 vndk: {
488 enabled: true,
489 },
490 nocrt: true,
491 }
492
493 cc_library {
494 name: "libvndk_sp",
495 vendor_available: true,
496 vndk: {
497 enabled: true,
498 support_system_process: true,
499 },
500 nocrt: true,
501 }
502
503 cc_library {
504 name: "libvndk2",
505 vendor_available: false,
506 vndk: {
507 enabled: true,
508 },
509 nocrt: true,
510 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900511
512 vndk_libraries_txt {
513 name: "vndkcorevariant.libraries.txt",
514 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800515 `
516
517 config := TestConfig(buildDir, android.Android, nil, bp, nil)
518 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
519 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
520 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
521
522 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
523
524 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900525
Jooyung Han2216fb12019-11-06 16:46:15 +0900526 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900527}
528
Chris Parsons79d66a52020-06-05 17:26:16 -0400529func TestDataLibs(t *testing.T) {
530 bp := `
531 cc_test_library {
532 name: "test_lib",
533 srcs: ["test_lib.cpp"],
534 gtest: false,
535 }
536
537 cc_test {
538 name: "main_test",
539 data_libs: ["test_lib"],
540 gtest: false,
541 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400542 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400543
544 config := TestConfig(buildDir, android.Android, nil, bp, nil)
545 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
546 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
547 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
548
549 ctx := testCcWithConfig(t, config)
550 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
551 testBinary := module.(*Module).linker.(*testBinary)
552 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
553 if err != nil {
554 t.Errorf("Expected cc_test to produce output files, error: %s", err)
555 return
556 }
557 if len(outputFiles) != 1 {
558 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
559 return
560 }
561 if len(testBinary.dataPaths()) != 1 {
562 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
563 return
564 }
565
566 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400567 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400568
569 if !strings.HasSuffix(outputPath, "/main_test") {
570 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
571 return
572 }
573 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
574 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
575 return
576 }
577}
578
Chris Parsons216e10a2020-07-09 17:12:52 -0400579func TestDataLibsRelativeInstallPath(t *testing.T) {
580 bp := `
581 cc_test_library {
582 name: "test_lib",
583 srcs: ["test_lib.cpp"],
584 relative_install_path: "foo/bar/baz",
585 gtest: false,
586 }
587
588 cc_test {
589 name: "main_test",
590 data_libs: ["test_lib"],
591 gtest: false,
592 }
593 `
594
595 config := TestConfig(buildDir, android.Android, nil, bp, nil)
596 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
597 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
598 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
599
600 ctx := testCcWithConfig(t, config)
601 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
602 testBinary := module.(*Module).linker.(*testBinary)
603 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
604 if err != nil {
605 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
606 }
607 if len(outputFiles) != 1 {
608 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
609 }
610 if len(testBinary.dataPaths()) != 1 {
611 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
612 }
613
614 outputPath := outputFiles[0].String()
615 testBinaryPath := testBinary.dataPaths()[0]
616
617 if !strings.HasSuffix(outputPath, "/main_test") {
618 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
619 }
620 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
621 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
622 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
623 " but was '%s'", testBinaryPath)
624 }
625}
626
Jooyung Han0302a842019-10-30 18:43:49 +0900627func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900628 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900629 cc_library {
630 name: "libvndk",
631 vendor_available: true,
632 vndk: {
633 enabled: true,
634 },
635 nocrt: true,
636 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900637 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900638
639 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
640 "LLNDK: libc.so",
641 "LLNDK: libdl.so",
642 "LLNDK: libft2.so",
643 "LLNDK: libm.so",
644 "VNDK-SP: libc++.so",
645 "VNDK-core: libvndk.so",
646 "VNDK-private: libft2.so",
647 })
Logan Chienf3511742017-10-31 18:04:35 +0800648}
649
Logan Chiend3c59a22018-03-29 14:08:15 +0800650func TestVndkDepError(t *testing.T) {
651 // Check whether an error is emitted when a VNDK lib depends on a system lib.
652 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
653 cc_library {
654 name: "libvndk",
655 vendor_available: true,
656 vndk: {
657 enabled: true,
658 },
659 shared_libs: ["libfwk"], // Cause error
660 nocrt: true,
661 }
662
663 cc_library {
664 name: "libfwk",
665 nocrt: true,
666 }
667 `)
668
669 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
670 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
671 cc_library {
672 name: "libvndk",
673 vendor_available: true,
674 vndk: {
675 enabled: true,
676 },
677 shared_libs: ["libvendor"], // Cause error
678 nocrt: true,
679 }
680
681 cc_library {
682 name: "libvendor",
683 vendor: true,
684 nocrt: true,
685 }
686 `)
687
688 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
689 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
690 cc_library {
691 name: "libvndk_sp",
692 vendor_available: true,
693 vndk: {
694 enabled: true,
695 support_system_process: true,
696 },
697 shared_libs: ["libfwk"], // Cause error
698 nocrt: true,
699 }
700
701 cc_library {
702 name: "libfwk",
703 nocrt: true,
704 }
705 `)
706
707 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
708 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
709 cc_library {
710 name: "libvndk_sp",
711 vendor_available: true,
712 vndk: {
713 enabled: true,
714 support_system_process: true,
715 },
716 shared_libs: ["libvendor"], // Cause error
717 nocrt: true,
718 }
719
720 cc_library {
721 name: "libvendor",
722 vendor: true,
723 nocrt: true,
724 }
725 `)
726
727 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
728 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
729 cc_library {
730 name: "libvndk_sp",
731 vendor_available: true,
732 vndk: {
733 enabled: true,
734 support_system_process: true,
735 },
736 shared_libs: ["libvndk"], // Cause error
737 nocrt: true,
738 }
739
740 cc_library {
741 name: "libvndk",
742 vendor_available: true,
743 vndk: {
744 enabled: true,
745 },
746 nocrt: true,
747 }
748 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900749
750 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
751 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
752 cc_library {
753 name: "libvndk",
754 vendor_available: true,
755 vndk: {
756 enabled: true,
757 },
758 shared_libs: ["libnonvndk"],
759 nocrt: true,
760 }
761
762 cc_library {
763 name: "libnonvndk",
764 vendor_available: true,
765 nocrt: true,
766 }
767 `)
768
769 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
770 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
771 cc_library {
772 name: "libvndkprivate",
773 vendor_available: false,
774 vndk: {
775 enabled: true,
776 },
777 shared_libs: ["libnonvndk"],
778 nocrt: true,
779 }
780
781 cc_library {
782 name: "libnonvndk",
783 vendor_available: true,
784 nocrt: true,
785 }
786 `)
787
788 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
789 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
790 cc_library {
791 name: "libvndksp",
792 vendor_available: true,
793 vndk: {
794 enabled: true,
795 support_system_process: true,
796 },
797 shared_libs: ["libnonvndk"],
798 nocrt: true,
799 }
800
801 cc_library {
802 name: "libnonvndk",
803 vendor_available: true,
804 nocrt: true,
805 }
806 `)
807
808 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
809 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
810 cc_library {
811 name: "libvndkspprivate",
812 vendor_available: false,
813 vndk: {
814 enabled: true,
815 support_system_process: true,
816 },
817 shared_libs: ["libnonvndk"],
818 nocrt: true,
819 }
820
821 cc_library {
822 name: "libnonvndk",
823 vendor_available: true,
824 nocrt: true,
825 }
826 `)
827}
828
829func TestDoubleLoadbleDep(t *testing.T) {
830 // okay to link : LLNDK -> double_loadable VNDK
831 testCc(t, `
832 cc_library {
833 name: "libllndk",
834 shared_libs: ["libdoubleloadable"],
835 }
836
837 llndk_library {
838 name: "libllndk",
839 symbol_file: "",
840 }
841
842 cc_library {
843 name: "libdoubleloadable",
844 vendor_available: true,
845 vndk: {
846 enabled: true,
847 },
848 double_loadable: true,
849 }
850 `)
851 // okay to link : LLNDK -> VNDK-SP
852 testCc(t, `
853 cc_library {
854 name: "libllndk",
855 shared_libs: ["libvndksp"],
856 }
857
858 llndk_library {
859 name: "libllndk",
860 symbol_file: "",
861 }
862
863 cc_library {
864 name: "libvndksp",
865 vendor_available: true,
866 vndk: {
867 enabled: true,
868 support_system_process: true,
869 },
870 }
871 `)
872 // okay to link : double_loadable -> double_loadable
873 testCc(t, `
874 cc_library {
875 name: "libdoubleloadable1",
876 shared_libs: ["libdoubleloadable2"],
877 vendor_available: true,
878 double_loadable: true,
879 }
880
881 cc_library {
882 name: "libdoubleloadable2",
883 vendor_available: true,
884 double_loadable: true,
885 }
886 `)
887 // okay to link : double_loadable VNDK -> double_loadable VNDK private
888 testCc(t, `
889 cc_library {
890 name: "libdoubleloadable",
891 vendor_available: true,
892 vndk: {
893 enabled: true,
894 },
895 double_loadable: true,
896 shared_libs: ["libnondoubleloadable"],
897 }
898
899 cc_library {
900 name: "libnondoubleloadable",
901 vendor_available: false,
902 vndk: {
903 enabled: true,
904 },
905 double_loadable: true,
906 }
907 `)
908 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
909 testCc(t, `
910 cc_library {
911 name: "libllndk",
912 shared_libs: ["libcoreonly"],
913 }
914
915 llndk_library {
916 name: "libllndk",
917 symbol_file: "",
918 }
919
920 cc_library {
921 name: "libcoreonly",
922 shared_libs: ["libvendoravailable"],
923 }
924
925 // indirect dependency of LLNDK
926 cc_library {
927 name: "libvendoravailable",
928 vendor_available: true,
929 double_loadable: true,
930 }
931 `)
932}
933
Inseob Kim8471cda2019-11-15 09:59:12 +0900934func TestVendorSnapshot(t *testing.T) {
935 bp := `
936 cc_library {
937 name: "libvndk",
938 vendor_available: true,
939 vndk: {
940 enabled: true,
941 },
942 nocrt: true,
943 }
944
945 cc_library {
946 name: "libvendor",
947 vendor: true,
948 nocrt: true,
949 }
950
951 cc_library {
952 name: "libvendor_available",
953 vendor_available: true,
954 nocrt: true,
955 }
956
957 cc_library_headers {
958 name: "libvendor_headers",
959 vendor_available: true,
960 nocrt: true,
961 }
962
963 cc_binary {
964 name: "vendor_bin",
965 vendor: true,
966 nocrt: true,
967 }
968
969 cc_binary {
970 name: "vendor_available_bin",
971 vendor_available: true,
972 nocrt: true,
973 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900974
975 toolchain_library {
976 name: "libb",
977 vendor_available: true,
978 src: "libb.a",
979 }
Inseob Kim1042d292020-06-01 23:23:05 +0900980
981 cc_object {
982 name: "obj",
983 vendor_available: true,
984 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900985`
986 config := TestConfig(buildDir, android.Android, nil, bp, nil)
987 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
988 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
989 ctx := testCcWithConfig(t, config)
990
991 // Check Vendor snapshot output.
992
993 snapshotDir := "vendor-snapshot"
994 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +0900995 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
996
997 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +0900998
999 for _, arch := range [][]string{
1000 []string{"arm64", "armv8-a"},
1001 []string{"arm", "armv7-a-neon"},
1002 } {
1003 archType := arch[0]
1004 archVariant := arch[1]
1005 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1006
1007 // For shared libraries, only non-VNDK vendor_available modules are captured
1008 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1009 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +09001010 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1011 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
1012 jsonFiles = append(jsonFiles,
1013 filepath.Join(sharedDir, "libvendor.so.json"),
1014 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001015
1016 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
1017 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
1018 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +09001019 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1020 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
1021 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
1022 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
1023 jsonFiles = append(jsonFiles,
1024 filepath.Join(staticDir, "libb.a.json"),
1025 filepath.Join(staticDir, "libvndk.a.json"),
1026 filepath.Join(staticDir, "libvendor.a.json"),
1027 filepath.Join(staticDir, "libvendor_available.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001028
Inseob Kim7f283f42020-06-01 21:53:49 +09001029 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +09001030 if archType == "arm64" {
1031 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1032 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +09001033 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
1034 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
1035 jsonFiles = append(jsonFiles,
1036 filepath.Join(binaryDir, "vendor_bin.json"),
1037 filepath.Join(binaryDir, "vendor_available_bin.json"))
1038 }
1039
1040 // For header libraries, all vendor:true and vendor_available modules are captured.
1041 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1042 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +09001043
1044 // For object modules, all vendor:true and vendor_available modules are captured.
1045 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1046 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1047 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1048 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001049 }
1050
1051 for _, jsonFile := range jsonFiles {
1052 // verify all json files exist
1053 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1054 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001055 }
1056 }
1057}
1058
Jooyung Hana70f0672019-01-18 15:20:43 +09001059func TestDoubleLoadableDepError(t *testing.T) {
1060 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1061 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1062 cc_library {
1063 name: "libllndk",
1064 shared_libs: ["libnondoubleloadable"],
1065 }
1066
1067 llndk_library {
1068 name: "libllndk",
1069 symbol_file: "",
1070 }
1071
1072 cc_library {
1073 name: "libnondoubleloadable",
1074 vendor_available: true,
1075 vndk: {
1076 enabled: true,
1077 },
1078 }
1079 `)
1080
1081 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1082 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1083 cc_library {
1084 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001085 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001086 shared_libs: ["libnondoubleloadable"],
1087 }
1088
1089 llndk_library {
1090 name: "libllndk",
1091 symbol_file: "",
1092 }
1093
1094 cc_library {
1095 name: "libnondoubleloadable",
1096 vendor_available: true,
1097 }
1098 `)
1099
1100 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
1101 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1102 cc_library {
1103 name: "libdoubleloadable",
1104 vendor_available: true,
1105 double_loadable: true,
1106 shared_libs: ["libnondoubleloadable"],
1107 }
1108
1109 cc_library {
1110 name: "libnondoubleloadable",
1111 vendor_available: true,
1112 }
1113 `)
1114
1115 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
1116 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1117 cc_library {
1118 name: "libdoubleloadable",
1119 vendor_available: true,
1120 double_loadable: true,
1121 shared_libs: ["libnondoubleloadable"],
1122 }
1123
1124 cc_library {
1125 name: "libnondoubleloadable",
1126 vendor_available: true,
1127 vndk: {
1128 enabled: true,
1129 },
1130 }
1131 `)
1132
1133 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
1134 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1135 cc_library {
1136 name: "libdoubleloadable",
1137 vendor_available: true,
1138 vndk: {
1139 enabled: true,
1140 },
1141 double_loadable: true,
1142 shared_libs: ["libnondoubleloadable"],
1143 }
1144
1145 cc_library {
1146 name: "libnondoubleloadable",
1147 vendor_available: false,
1148 vndk: {
1149 enabled: true,
1150 },
1151 }
1152 `)
1153
1154 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1155 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1156 cc_library {
1157 name: "libllndk",
1158 shared_libs: ["libcoreonly"],
1159 }
1160
1161 llndk_library {
1162 name: "libllndk",
1163 symbol_file: "",
1164 }
1165
1166 cc_library {
1167 name: "libcoreonly",
1168 shared_libs: ["libvendoravailable"],
1169 }
1170
1171 // indirect dependency of LLNDK
1172 cc_library {
1173 name: "libvendoravailable",
1174 vendor_available: true,
1175 }
1176 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001177}
1178
Logan Chienf3511742017-10-31 18:04:35 +08001179func TestVndkExt(t *testing.T) {
1180 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001181 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001182 cc_library {
1183 name: "libvndk",
1184 vendor_available: true,
1185 vndk: {
1186 enabled: true,
1187 },
1188 nocrt: true,
1189 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001190 cc_library {
1191 name: "libvndk2",
1192 vendor_available: true,
1193 vndk: {
1194 enabled: true,
1195 },
1196 target: {
1197 vendor: {
1198 suffix: "-suffix",
1199 },
1200 },
1201 nocrt: true,
1202 }
Logan Chienf3511742017-10-31 18:04:35 +08001203
1204 cc_library {
1205 name: "libvndk_ext",
1206 vendor: true,
1207 vndk: {
1208 enabled: true,
1209 extends: "libvndk",
1210 },
1211 nocrt: true,
1212 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001213
1214 cc_library {
1215 name: "libvndk2_ext",
1216 vendor: true,
1217 vndk: {
1218 enabled: true,
1219 extends: "libvndk2",
1220 },
1221 nocrt: true,
1222 }
Logan Chienf3511742017-10-31 18:04:35 +08001223
Justin Yun0ecf0b22020-02-28 15:07:59 +09001224 cc_library {
1225 name: "libvndk_ext_product",
1226 product_specific: true,
1227 vndk: {
1228 enabled: true,
1229 extends: "libvndk",
1230 },
1231 nocrt: true,
1232 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001233
Justin Yun0ecf0b22020-02-28 15:07:59 +09001234 cc_library {
1235 name: "libvndk2_ext_product",
1236 product_specific: true,
1237 vndk: {
1238 enabled: true,
1239 extends: "libvndk2",
1240 },
1241 nocrt: true,
1242 }
1243 `
1244 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1245 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1246 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1247 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1248
1249 ctx := testCcWithConfig(t, config)
1250
1251 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1252 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1253
1254 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1255 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1256
1257 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1258 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001259}
1260
Logan Chiend3c59a22018-03-29 14:08:15 +08001261func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001262 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1263 ctx := testCcNoVndk(t, `
1264 cc_library {
1265 name: "libvndk",
1266 vendor_available: true,
1267 vndk: {
1268 enabled: true,
1269 },
1270 nocrt: true,
1271 }
1272
1273 cc_library {
1274 name: "libvndk_ext",
1275 vendor: true,
1276 vndk: {
1277 enabled: true,
1278 extends: "libvndk",
1279 },
1280 nocrt: true,
1281 }
1282 `)
1283
1284 // Ensures that the core variant of "libvndk_ext" can be found.
1285 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1286 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1287 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1288 }
1289}
1290
Justin Yun0ecf0b22020-02-28 15:07:59 +09001291func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1292 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
1293 ctx := testCc(t, `
1294 cc_library {
1295 name: "libvndk",
1296 vendor_available: true,
1297 vndk: {
1298 enabled: true,
1299 },
1300 nocrt: true,
1301 }
1302
1303 cc_library {
1304 name: "libvndk_ext_product",
1305 product_specific: true,
1306 vndk: {
1307 enabled: true,
1308 extends: "libvndk",
1309 },
1310 nocrt: true,
1311 }
1312 `)
1313
1314 // Ensures that the core variant of "libvndk_ext_product" can be found.
1315 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1316 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1317 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1318 }
1319}
1320
Logan Chienf3511742017-10-31 18:04:35 +08001321func TestVndkExtError(t *testing.T) {
1322 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001323 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001324 cc_library {
1325 name: "libvndk",
1326 vendor_available: true,
1327 vndk: {
1328 enabled: true,
1329 },
1330 nocrt: true,
1331 }
1332
1333 cc_library {
1334 name: "libvndk_ext",
1335 vndk: {
1336 enabled: true,
1337 extends: "libvndk",
1338 },
1339 nocrt: true,
1340 }
1341 `)
1342
1343 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1344 cc_library {
1345 name: "libvndk",
1346 vendor_available: true,
1347 vndk: {
1348 enabled: true,
1349 },
1350 nocrt: true,
1351 }
1352
1353 cc_library {
1354 name: "libvndk_ext",
1355 vendor: true,
1356 vndk: {
1357 enabled: true,
1358 },
1359 nocrt: true,
1360 }
1361 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001362
1363 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1364 cc_library {
1365 name: "libvndk",
1366 vendor_available: true,
1367 vndk: {
1368 enabled: true,
1369 },
1370 nocrt: true,
1371 }
1372
1373 cc_library {
1374 name: "libvndk_ext_product",
1375 product_specific: true,
1376 vndk: {
1377 enabled: true,
1378 },
1379 nocrt: true,
1380 }
1381 `)
1382
1383 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1384 cc_library {
1385 name: "libvndk",
1386 vendor_available: true,
1387 vndk: {
1388 enabled: true,
1389 },
1390 nocrt: true,
1391 }
1392
1393 cc_library {
1394 name: "libvndk_ext_product",
1395 product_specific: true,
1396 vendor_available: true,
1397 vndk: {
1398 enabled: true,
1399 extends: "libvndk",
1400 },
1401 nocrt: true,
1402 }
1403 `)
Logan Chienf3511742017-10-31 18:04:35 +08001404}
1405
1406func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1407 // This test ensures an error is emitted for inconsistent support_system_process.
1408 testCcError(t, "module \".*\" with mismatched support_system_process", `
1409 cc_library {
1410 name: "libvndk",
1411 vendor_available: true,
1412 vndk: {
1413 enabled: true,
1414 },
1415 nocrt: true,
1416 }
1417
1418 cc_library {
1419 name: "libvndk_sp_ext",
1420 vendor: true,
1421 vndk: {
1422 enabled: true,
1423 extends: "libvndk",
1424 support_system_process: true,
1425 },
1426 nocrt: true,
1427 }
1428 `)
1429
1430 testCcError(t, "module \".*\" with mismatched support_system_process", `
1431 cc_library {
1432 name: "libvndk_sp",
1433 vendor_available: true,
1434 vndk: {
1435 enabled: true,
1436 support_system_process: true,
1437 },
1438 nocrt: true,
1439 }
1440
1441 cc_library {
1442 name: "libvndk_ext",
1443 vendor: true,
1444 vndk: {
1445 enabled: true,
1446 extends: "libvndk_sp",
1447 },
1448 nocrt: true,
1449 }
1450 `)
1451}
1452
1453func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001454 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08001455 // with `vendor_available: false`.
1456 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1457 cc_library {
1458 name: "libvndk",
1459 vendor_available: false,
1460 vndk: {
1461 enabled: true,
1462 },
1463 nocrt: true,
1464 }
1465
1466 cc_library {
1467 name: "libvndk_ext",
1468 vendor: true,
1469 vndk: {
1470 enabled: true,
1471 extends: "libvndk",
1472 },
1473 nocrt: true,
1474 }
1475 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001476
1477 testCcErrorProductVndk(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1478 cc_library {
1479 name: "libvndk",
1480 vendor_available: false,
1481 vndk: {
1482 enabled: true,
1483 },
1484 nocrt: true,
1485 }
1486
1487 cc_library {
1488 name: "libvndk_ext_product",
1489 product_specific: true,
1490 vndk: {
1491 enabled: true,
1492 extends: "libvndk",
1493 },
1494 nocrt: true,
1495 }
1496 `)
Logan Chienf3511742017-10-31 18:04:35 +08001497}
1498
Logan Chiend3c59a22018-03-29 14:08:15 +08001499func TestVendorModuleUseVndkExt(t *testing.T) {
1500 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001501 testCc(t, `
1502 cc_library {
1503 name: "libvndk",
1504 vendor_available: true,
1505 vndk: {
1506 enabled: true,
1507 },
1508 nocrt: true,
1509 }
1510
1511 cc_library {
1512 name: "libvndk_ext",
1513 vendor: true,
1514 vndk: {
1515 enabled: true,
1516 extends: "libvndk",
1517 },
1518 nocrt: true,
1519 }
1520
1521 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001522 name: "libvndk_sp",
1523 vendor_available: true,
1524 vndk: {
1525 enabled: true,
1526 support_system_process: true,
1527 },
1528 nocrt: true,
1529 }
1530
1531 cc_library {
1532 name: "libvndk_sp_ext",
1533 vendor: true,
1534 vndk: {
1535 enabled: true,
1536 extends: "libvndk_sp",
1537 support_system_process: true,
1538 },
1539 nocrt: true,
1540 }
1541
1542 cc_library {
1543 name: "libvendor",
1544 vendor: true,
1545 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1546 nocrt: true,
1547 }
1548 `)
1549}
1550
Logan Chiend3c59a22018-03-29 14:08:15 +08001551func TestVndkExtUseVendorLib(t *testing.T) {
1552 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001553 testCc(t, `
1554 cc_library {
1555 name: "libvndk",
1556 vendor_available: true,
1557 vndk: {
1558 enabled: true,
1559 },
1560 nocrt: true,
1561 }
1562
1563 cc_library {
1564 name: "libvndk_ext",
1565 vendor: true,
1566 vndk: {
1567 enabled: true,
1568 extends: "libvndk",
1569 },
1570 shared_libs: ["libvendor"],
1571 nocrt: true,
1572 }
1573
1574 cc_library {
1575 name: "libvendor",
1576 vendor: true,
1577 nocrt: true,
1578 }
1579 `)
Logan Chienf3511742017-10-31 18:04:35 +08001580
Logan Chiend3c59a22018-03-29 14:08:15 +08001581 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1582 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001583 cc_library {
1584 name: "libvndk_sp",
1585 vendor_available: true,
1586 vndk: {
1587 enabled: true,
1588 support_system_process: true,
1589 },
1590 nocrt: true,
1591 }
1592
1593 cc_library {
1594 name: "libvndk_sp_ext",
1595 vendor: true,
1596 vndk: {
1597 enabled: true,
1598 extends: "libvndk_sp",
1599 support_system_process: true,
1600 },
1601 shared_libs: ["libvendor"], // Cause an error
1602 nocrt: true,
1603 }
1604
1605 cc_library {
1606 name: "libvendor",
1607 vendor: true,
1608 nocrt: true,
1609 }
1610 `)
1611}
1612
Justin Yun0ecf0b22020-02-28 15:07:59 +09001613func TestProductVndkExtDependency(t *testing.T) {
1614 bp := `
1615 cc_library {
1616 name: "libvndk",
1617 vendor_available: true,
1618 vndk: {
1619 enabled: true,
1620 },
1621 nocrt: true,
1622 }
1623
1624 cc_library {
1625 name: "libvndk_ext_product",
1626 product_specific: true,
1627 vndk: {
1628 enabled: true,
1629 extends: "libvndk",
1630 },
1631 shared_libs: ["libproduct_for_vndklibs"],
1632 nocrt: true,
1633 }
1634
1635 cc_library {
1636 name: "libvndk_sp",
1637 vendor_available: true,
1638 vndk: {
1639 enabled: true,
1640 support_system_process: true,
1641 },
1642 nocrt: true,
1643 }
1644
1645 cc_library {
1646 name: "libvndk_sp_ext_product",
1647 product_specific: true,
1648 vndk: {
1649 enabled: true,
1650 extends: "libvndk_sp",
1651 support_system_process: true,
1652 },
1653 shared_libs: ["libproduct_for_vndklibs"],
1654 nocrt: true,
1655 }
1656
1657 cc_library {
1658 name: "libproduct",
1659 product_specific: true,
1660 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1661 nocrt: true,
1662 }
1663
1664 cc_library {
1665 name: "libproduct_for_vndklibs",
1666 product_specific: true,
1667 nocrt: true,
1668 }
1669 `
1670 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1671 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1672 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1673 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1674
1675 testCcWithConfig(t, config)
1676}
1677
Logan Chiend3c59a22018-03-29 14:08:15 +08001678func TestVndkSpExtUseVndkError(t *testing.T) {
1679 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1680 // library.
1681 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1682 cc_library {
1683 name: "libvndk",
1684 vendor_available: true,
1685 vndk: {
1686 enabled: true,
1687 },
1688 nocrt: true,
1689 }
1690
1691 cc_library {
1692 name: "libvndk_sp",
1693 vendor_available: true,
1694 vndk: {
1695 enabled: true,
1696 support_system_process: true,
1697 },
1698 nocrt: true,
1699 }
1700
1701 cc_library {
1702 name: "libvndk_sp_ext",
1703 vendor: true,
1704 vndk: {
1705 enabled: true,
1706 extends: "libvndk_sp",
1707 support_system_process: true,
1708 },
1709 shared_libs: ["libvndk"], // Cause an error
1710 nocrt: true,
1711 }
1712 `)
1713
1714 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1715 // library.
1716 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1717 cc_library {
1718 name: "libvndk",
1719 vendor_available: true,
1720 vndk: {
1721 enabled: true,
1722 },
1723 nocrt: true,
1724 }
1725
1726 cc_library {
1727 name: "libvndk_ext",
1728 vendor: true,
1729 vndk: {
1730 enabled: true,
1731 extends: "libvndk",
1732 },
1733 nocrt: true,
1734 }
1735
1736 cc_library {
1737 name: "libvndk_sp",
1738 vendor_available: true,
1739 vndk: {
1740 enabled: true,
1741 support_system_process: true,
1742 },
1743 nocrt: true,
1744 }
1745
1746 cc_library {
1747 name: "libvndk_sp_ext",
1748 vendor: true,
1749 vndk: {
1750 enabled: true,
1751 extends: "libvndk_sp",
1752 support_system_process: true,
1753 },
1754 shared_libs: ["libvndk_ext"], // Cause an error
1755 nocrt: true,
1756 }
1757 `)
1758}
1759
1760func TestVndkUseVndkExtError(t *testing.T) {
1761 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1762 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001763 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1764 cc_library {
1765 name: "libvndk",
1766 vendor_available: true,
1767 vndk: {
1768 enabled: true,
1769 },
1770 nocrt: true,
1771 }
1772
1773 cc_library {
1774 name: "libvndk_ext",
1775 vendor: true,
1776 vndk: {
1777 enabled: true,
1778 extends: "libvndk",
1779 },
1780 nocrt: true,
1781 }
1782
1783 cc_library {
1784 name: "libvndk2",
1785 vendor_available: true,
1786 vndk: {
1787 enabled: true,
1788 },
1789 shared_libs: ["libvndk_ext"],
1790 nocrt: true,
1791 }
1792 `)
1793
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001794 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001795 cc_library {
1796 name: "libvndk",
1797 vendor_available: true,
1798 vndk: {
1799 enabled: true,
1800 },
1801 nocrt: true,
1802 }
1803
1804 cc_library {
1805 name: "libvndk_ext",
1806 vendor: true,
1807 vndk: {
1808 enabled: true,
1809 extends: "libvndk",
1810 },
1811 nocrt: true,
1812 }
1813
1814 cc_library {
1815 name: "libvndk2",
1816 vendor_available: true,
1817 vndk: {
1818 enabled: true,
1819 },
1820 target: {
1821 vendor: {
1822 shared_libs: ["libvndk_ext"],
1823 },
1824 },
1825 nocrt: true,
1826 }
1827 `)
1828
1829 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1830 cc_library {
1831 name: "libvndk_sp",
1832 vendor_available: true,
1833 vndk: {
1834 enabled: true,
1835 support_system_process: true,
1836 },
1837 nocrt: true,
1838 }
1839
1840 cc_library {
1841 name: "libvndk_sp_ext",
1842 vendor: true,
1843 vndk: {
1844 enabled: true,
1845 extends: "libvndk_sp",
1846 support_system_process: true,
1847 },
1848 nocrt: true,
1849 }
1850
1851 cc_library {
1852 name: "libvndk_sp_2",
1853 vendor_available: true,
1854 vndk: {
1855 enabled: true,
1856 support_system_process: true,
1857 },
1858 shared_libs: ["libvndk_sp_ext"],
1859 nocrt: true,
1860 }
1861 `)
1862
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001863 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001864 cc_library {
1865 name: "libvndk_sp",
1866 vendor_available: true,
1867 vndk: {
1868 enabled: true,
1869 },
1870 nocrt: true,
1871 }
1872
1873 cc_library {
1874 name: "libvndk_sp_ext",
1875 vendor: true,
1876 vndk: {
1877 enabled: true,
1878 extends: "libvndk_sp",
1879 },
1880 nocrt: true,
1881 }
1882
1883 cc_library {
1884 name: "libvndk_sp2",
1885 vendor_available: true,
1886 vndk: {
1887 enabled: true,
1888 },
1889 target: {
1890 vendor: {
1891 shared_libs: ["libvndk_sp_ext"],
1892 },
1893 },
1894 nocrt: true,
1895 }
1896 `)
1897}
1898
Justin Yun5f7f7e82019-11-18 19:52:14 +09001899func TestEnforceProductVndkVersion(t *testing.T) {
1900 bp := `
1901 cc_library {
1902 name: "libllndk",
1903 }
1904 llndk_library {
1905 name: "libllndk",
1906 symbol_file: "",
1907 }
1908 cc_library {
1909 name: "libvndk",
1910 vendor_available: true,
1911 vndk: {
1912 enabled: true,
1913 },
1914 nocrt: true,
1915 }
1916 cc_library {
1917 name: "libvndk_sp",
1918 vendor_available: true,
1919 vndk: {
1920 enabled: true,
1921 support_system_process: true,
1922 },
1923 nocrt: true,
1924 }
1925 cc_library {
1926 name: "libva",
1927 vendor_available: true,
1928 nocrt: true,
1929 }
1930 cc_library {
1931 name: "libproduct_va",
1932 product_specific: true,
1933 vendor_available: true,
1934 nocrt: true,
1935 }
1936 cc_library {
1937 name: "libprod",
1938 product_specific: true,
1939 shared_libs: [
1940 "libllndk",
1941 "libvndk",
1942 "libvndk_sp",
1943 "libva",
1944 "libproduct_va",
1945 ],
1946 nocrt: true,
1947 }
1948 cc_library {
1949 name: "libvendor",
1950 vendor: true,
1951 shared_libs: [
1952 "libllndk",
1953 "libvndk",
1954 "libvndk_sp",
1955 "libva",
1956 "libproduct_va",
1957 ],
1958 nocrt: true,
1959 }
1960 `
1961
1962 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1963 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1964 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1965 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1966
1967 ctx := testCcWithConfig(t, config)
1968
Justin Yun0ecf0b22020-02-28 15:07:59 +09001969 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "", productVariant)
1970 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "", productVariant)
Justin Yun5f7f7e82019-11-18 19:52:14 +09001971}
1972
1973func TestEnforceProductVndkVersionErrors(t *testing.T) {
1974 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1975 cc_library {
1976 name: "libprod",
1977 product_specific: true,
1978 shared_libs: [
1979 "libvendor",
1980 ],
1981 nocrt: true,
1982 }
1983 cc_library {
1984 name: "libvendor",
1985 vendor: true,
1986 nocrt: true,
1987 }
1988 `)
1989 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1990 cc_library {
1991 name: "libprod",
1992 product_specific: true,
1993 shared_libs: [
1994 "libsystem",
1995 ],
1996 nocrt: true,
1997 }
1998 cc_library {
1999 name: "libsystem",
2000 nocrt: true,
2001 }
2002 `)
2003 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
2004 cc_library {
2005 name: "libprod",
2006 product_specific: true,
2007 shared_libs: [
2008 "libvndk_private",
2009 ],
2010 nocrt: true,
2011 }
2012 cc_library {
2013 name: "libvndk_private",
2014 vendor_available: false,
2015 vndk: {
2016 enabled: true,
2017 },
2018 nocrt: true,
2019 }
2020 `)
2021 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2022 cc_library {
2023 name: "libprod",
2024 product_specific: true,
2025 shared_libs: [
2026 "libsystem_ext",
2027 ],
2028 nocrt: true,
2029 }
2030 cc_library {
2031 name: "libsystem_ext",
2032 system_ext_specific: true,
2033 nocrt: true,
2034 }
2035 `)
2036 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2037 cc_library {
2038 name: "libsystem",
2039 shared_libs: [
2040 "libproduct_va",
2041 ],
2042 nocrt: true,
2043 }
2044 cc_library {
2045 name: "libproduct_va",
2046 product_specific: true,
2047 vendor_available: true,
2048 nocrt: true,
2049 }
2050 `)
2051}
2052
Jooyung Han38002912019-05-16 04:01:54 +09002053func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002054 bp := `
2055 cc_library {
2056 name: "libvndk",
2057 vendor_available: true,
2058 vndk: {
2059 enabled: true,
2060 },
2061 }
2062 cc_library {
2063 name: "libvndksp",
2064 vendor_available: true,
2065 vndk: {
2066 enabled: true,
2067 support_system_process: true,
2068 },
2069 }
2070 cc_library {
2071 name: "libvndkprivate",
2072 vendor_available: false,
2073 vndk: {
2074 enabled: true,
2075 },
2076 }
2077 cc_library {
2078 name: "libvendor",
2079 vendor: true,
2080 }
2081 cc_library {
2082 name: "libvndkext",
2083 vendor: true,
2084 vndk: {
2085 enabled: true,
2086 extends: "libvndk",
2087 },
2088 }
2089 vndk_prebuilt_shared {
2090 name: "prevndk",
2091 version: "27",
2092 target_arch: "arm",
2093 binder32bit: true,
2094 vendor_available: true,
2095 vndk: {
2096 enabled: true,
2097 },
2098 arch: {
2099 arm: {
2100 srcs: ["liba.so"],
2101 },
2102 },
2103 }
2104 cc_library {
2105 name: "libllndk",
2106 }
2107 llndk_library {
2108 name: "libllndk",
2109 symbol_file: "",
2110 }
2111 cc_library {
2112 name: "libllndkprivate",
2113 }
2114 llndk_library {
2115 name: "libllndkprivate",
2116 vendor_available: false,
2117 symbol_file: "",
2118 }`
2119
2120 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002121 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2122 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2123 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002124 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002125
Jooyung Han0302a842019-10-30 18:43:49 +09002126 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002127 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09002128 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002129 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09002130 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002131 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09002132 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002133 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09002134
Colin Crossfb0c16e2019-11-20 17:12:35 -08002135 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002136
Jooyung Han38002912019-05-16 04:01:54 +09002137 tests := []struct {
2138 variant string
2139 name string
2140 expected string
2141 }{
2142 {vendorVariant, "libvndk", "native:vndk"},
2143 {vendorVariant, "libvndksp", "native:vndk"},
2144 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2145 {vendorVariant, "libvendor", "native:vendor"},
2146 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09002147 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002148 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002149 {coreVariant, "libvndk", "native:platform"},
2150 {coreVariant, "libvndkprivate", "native:platform"},
2151 {coreVariant, "libllndk", "native:platform"},
2152 }
2153 for _, test := range tests {
2154 t.Run(test.name, func(t *testing.T) {
2155 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2156 assertString(t, module.makeLinkType, test.expected)
2157 })
2158 }
2159}
2160
Colin Cross0af4b842015-04-30 16:36:18 -07002161var (
2162 str11 = "01234567891"
2163 str10 = str11[:10]
2164 str9 = str11[:9]
2165 str5 = str11[:5]
2166 str4 = str11[:4]
2167)
2168
2169var splitListForSizeTestCases = []struct {
2170 in []string
2171 out [][]string
2172 size int
2173}{
2174 {
2175 in: []string{str10},
2176 out: [][]string{{str10}},
2177 size: 10,
2178 },
2179 {
2180 in: []string{str9},
2181 out: [][]string{{str9}},
2182 size: 10,
2183 },
2184 {
2185 in: []string{str5},
2186 out: [][]string{{str5}},
2187 size: 10,
2188 },
2189 {
2190 in: []string{str11},
2191 out: nil,
2192 size: 10,
2193 },
2194 {
2195 in: []string{str10, str10},
2196 out: [][]string{{str10}, {str10}},
2197 size: 10,
2198 },
2199 {
2200 in: []string{str9, str10},
2201 out: [][]string{{str9}, {str10}},
2202 size: 10,
2203 },
2204 {
2205 in: []string{str10, str9},
2206 out: [][]string{{str10}, {str9}},
2207 size: 10,
2208 },
2209 {
2210 in: []string{str5, str4},
2211 out: [][]string{{str5, str4}},
2212 size: 10,
2213 },
2214 {
2215 in: []string{str5, str4, str5},
2216 out: [][]string{{str5, str4}, {str5}},
2217 size: 10,
2218 },
2219 {
2220 in: []string{str5, str4, str5, str4},
2221 out: [][]string{{str5, str4}, {str5, str4}},
2222 size: 10,
2223 },
2224 {
2225 in: []string{str5, str4, str5, str5},
2226 out: [][]string{{str5, str4}, {str5}, {str5}},
2227 size: 10,
2228 },
2229 {
2230 in: []string{str5, str5, str5, str4},
2231 out: [][]string{{str5}, {str5}, {str5, str4}},
2232 size: 10,
2233 },
2234 {
2235 in: []string{str9, str11},
2236 out: nil,
2237 size: 10,
2238 },
2239 {
2240 in: []string{str11, str9},
2241 out: nil,
2242 size: 10,
2243 },
2244}
2245
2246func TestSplitListForSize(t *testing.T) {
2247 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08002248 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07002249
2250 var outStrings [][]string
2251
2252 if len(out) > 0 {
2253 outStrings = make([][]string, len(out))
2254 for i, o := range out {
2255 outStrings[i] = o.Strings()
2256 }
2257 }
2258
2259 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07002260 t.Errorf("incorrect output:")
2261 t.Errorf(" input: %#v", testCase.in)
2262 t.Errorf(" size: %d", testCase.size)
2263 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07002264 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07002265 }
2266 }
2267}
Jeff Gaston294356f2017-09-27 17:05:30 -07002268
2269var staticLinkDepOrderTestCases = []struct {
2270 // This is a string representation of a map[moduleName][]moduleDependency .
2271 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002272 inStatic string
2273
2274 // This is a string representation of a map[moduleName][]moduleDependency .
2275 // It models the dependencies declared in an Android.bp file.
2276 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002277
2278 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2279 // The keys of allOrdered specify which modules we would like to check.
2280 // The values of allOrdered specify the expected result (of the transitive closure of all
2281 // dependencies) for each module to test
2282 allOrdered string
2283
2284 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2285 // The keys of outOrdered specify which modules we would like to check.
2286 // The values of outOrdered specify the expected result (of the ordered linker command line)
2287 // for each module to test.
2288 outOrdered string
2289}{
2290 // Simple tests
2291 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002292 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002293 outOrdered: "",
2294 },
2295 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002296 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002297 outOrdered: "a:",
2298 },
2299 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002300 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002301 outOrdered: "a:b; b:",
2302 },
2303 // Tests of reordering
2304 {
2305 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002306 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002307 outOrdered: "a:b,c,d; b:d; c:d; d:",
2308 },
2309 {
2310 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002311 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002312 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2313 },
2314 {
2315 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002316 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002317 outOrdered: "a:d,b,e,c; d:b; e:c",
2318 },
2319 {
2320 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002321 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002322 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2323 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2324 },
2325 {
2326 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002327 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 -07002328 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2329 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2330 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002331 // shared dependencies
2332 {
2333 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2334 // So, we don't actually have to check that a shared dependency of c will change the order
2335 // of a library that depends statically on b and on c. We only need to check that if c has
2336 // a shared dependency on b, that that shows up in allOrdered.
2337 inShared: "c:b",
2338 allOrdered: "c:b",
2339 outOrdered: "c:",
2340 },
2341 {
2342 // This test doesn't actually include any shared dependencies but it's a reminder of what
2343 // the second phase of the above test would look like
2344 inStatic: "a:b,c; c:b",
2345 allOrdered: "a:c,b; c:b",
2346 outOrdered: "a:c,b; c:b",
2347 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002348 // tiebreakers for when two modules specifying different orderings and there is no dependency
2349 // to dictate an order
2350 {
2351 // 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 -08002352 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002353 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2354 },
2355 {
2356 // 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 -08002357 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 -07002358 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2359 },
2360 // Tests involving duplicate dependencies
2361 {
2362 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002363 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002364 outOrdered: "a:c,b",
2365 },
2366 {
2367 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002368 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002369 outOrdered: "a:d,c,b",
2370 },
2371 // Tests to confirm the nonexistence of infinite loops.
2372 // These cases should never happen, so as long as the test terminates and the
2373 // result is deterministic then that should be fine.
2374 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002375 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002376 outOrdered: "a:a",
2377 },
2378 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002379 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002380 allOrdered: "a:b,c; b:c,a; c:a,b",
2381 outOrdered: "a:b; b:c; c:a",
2382 },
2383 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002384 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002385 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2386 outOrdered: "a:c,b; b:a,c; c:b,a",
2387 },
2388}
2389
2390// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2391func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2392 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2393 strippedText := strings.Replace(text, " ", "", -1)
2394 if len(strippedText) < 1 {
2395 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2396 }
2397 allDeps = make(map[android.Path][]android.Path, 0)
2398
2399 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2400 moduleTexts := strings.Split(strippedText, ";")
2401
2402 outputForModuleName := func(moduleName string) android.Path {
2403 return android.PathForTesting(moduleName)
2404 }
2405
2406 for _, moduleText := range moduleTexts {
2407 // convert from "a:b,c" to ["a", "b,c"]
2408 components := strings.Split(moduleText, ":")
2409 if len(components) != 2 {
2410 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2411 }
2412 moduleName := components[0]
2413 moduleOutput := outputForModuleName(moduleName)
2414 modulesInOrder = append(modulesInOrder, moduleOutput)
2415
2416 depString := components[1]
2417 // convert from "b,c" to ["b", "c"]
2418 depNames := strings.Split(depString, ",")
2419 if len(depString) < 1 {
2420 depNames = []string{}
2421 }
2422 var deps []android.Path
2423 for _, depName := range depNames {
2424 deps = append(deps, outputForModuleName(depName))
2425 }
2426 allDeps[moduleOutput] = deps
2427 }
2428 return modulesInOrder, allDeps
2429}
2430
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002431func TestLinkReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002432 for _, testCase := range staticLinkDepOrderTestCases {
2433 errs := []string{}
2434
2435 // parse testcase
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002436 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)
Jeff Gaston294356f2017-09-27 17:05:30 -07002437 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)
2438 if testCase.allOrdered == "" {
2439 // allow the test case to skip specifying allOrdered
2440 testCase.allOrdered = testCase.outOrdered
2441 }
2442 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002443 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)
Jeff Gaston294356f2017-09-27 17:05:30 -07002444
2445 // For each module whose post-reordered dependencies were specified, validate that
2446 // reordering the inputs produces the expected outputs.
2447 for _, moduleName := range expectedModuleNames {
2448 moduleDeps := givenTransitiveDeps[moduleName]
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002449 givenSharedDeps := givenAllSharedDeps[moduleName]
2450 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -07002451
2452 correctAllOrdered := expectedAllDeps[moduleName]
2453 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {
2454 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002455 "\nin static:%q"+
2456 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002457 "\nmodule: %v"+
2458 "\nexpected: %s"+
2459 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002460 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002461 }
2462
2463 correctOutputDeps := expectedTransitiveDeps[moduleName]
2464 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {
2465 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002466 "\nin static:%q"+
2467 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002468 "\nmodule: %v"+
2469 "\nexpected: %s"+
2470 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002471 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002472 }
2473 }
2474
2475 if len(errs) > 0 {
2476 sort.Strings(errs)
2477 for _, err := range errs {
2478 t.Error(err)
2479 }
2480 }
2481 }
2482}
Logan Chienf3511742017-10-31 18:04:35 +08002483
Jeff Gaston294356f2017-09-27 17:05:30 -07002484func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2485 for _, moduleName := range moduleNames {
2486 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
2487 output := module.outputFile.Path()
2488 paths = append(paths, output)
2489 }
2490 return paths
2491}
2492
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002493func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002494 ctx := testCc(t, `
2495 cc_library {
2496 name: "a",
2497 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002498 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002499 }
2500 cc_library {
2501 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002502 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002503 }
2504 cc_library {
2505 name: "c",
2506 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002507 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002508 }
2509 cc_library {
2510 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002511 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002512 }
2513
2514 `)
2515
Colin Cross7113d202019-11-20 16:39:12 -08002516 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002517 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002518 actual := moduleA.depsInLinkOrder
Jeff Gaston294356f2017-09-27 17:05:30 -07002519 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})
2520
2521 if !reflect.DeepEqual(actual, expected) {
2522 t.Errorf("staticDeps orderings were not propagated correctly"+
2523 "\nactual: %v"+
2524 "\nexpected: %v",
2525 actual,
2526 expected,
2527 )
2528 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002529}
Jeff Gaston294356f2017-09-27 17:05:30 -07002530
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002531func TestStaticLibDepReorderingWithShared(t *testing.T) {
2532 ctx := testCc(t, `
2533 cc_library {
2534 name: "a",
2535 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002536 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002537 }
2538 cc_library {
2539 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002540 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002541 }
2542 cc_library {
2543 name: "c",
2544 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002545 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002546 }
2547
2548 `)
2549
Colin Cross7113d202019-11-20 16:39:12 -08002550 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002551 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
2552 actual := moduleA.depsInLinkOrder
2553 expected := getOutputPaths(ctx, variant, []string{"c", "b"})
2554
2555 if !reflect.DeepEqual(actual, expected) {
2556 t.Errorf("staticDeps orderings did not account for shared libs"+
2557 "\nactual: %v"+
2558 "\nexpected: %v",
2559 actual,
2560 expected,
2561 )
2562 }
2563}
2564
Jooyung Hanb04a4992020-03-13 18:57:35 +09002565func checkEquals(t *testing.T, message string, expected, actual interface{}) {
2566 if !reflect.DeepEqual(actual, expected) {
2567 t.Errorf(message+
2568 "\nactual: %v"+
2569 "\nexpected: %v",
2570 actual,
2571 expected,
2572 )
2573 }
2574}
2575
Jooyung Han61b66e92020-03-21 14:21:46 +00002576func TestLlndkLibrary(t *testing.T) {
2577 ctx := testCc(t, `
2578 cc_library {
2579 name: "libllndk",
2580 stubs: { versions: ["1", "2"] },
2581 }
2582 llndk_library {
2583 name: "libllndk",
2584 }
2585 `)
2586 actual := ctx.ModuleVariantsForTests("libllndk.llndk")
2587 expected := []string{
2588 "android_vendor.VER_arm64_armv8-a_shared",
2589 "android_vendor.VER_arm64_armv8-a_shared_1",
2590 "android_vendor.VER_arm64_armv8-a_shared_2",
2591 "android_vendor.VER_arm_armv7-a-neon_shared",
2592 "android_vendor.VER_arm_armv7-a-neon_shared_1",
2593 "android_vendor.VER_arm_armv7-a-neon_shared_2",
2594 }
2595 checkEquals(t, "variants for llndk stubs", expected, actual)
2596
2597 params := ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
2598 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2599
2600 params = ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
2601 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2602}
2603
Jiyong Parka46a4d52017-12-14 19:54:34 +09002604func TestLlndkHeaders(t *testing.T) {
2605 ctx := testCc(t, `
2606 llndk_headers {
2607 name: "libllndk_headers",
2608 export_include_dirs: ["my_include"],
2609 }
2610 llndk_library {
2611 name: "libllndk",
2612 export_llndk_headers: ["libllndk_headers"],
2613 }
2614 cc_library {
2615 name: "libvendor",
2616 shared_libs: ["libllndk"],
2617 vendor: true,
2618 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002619 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002620 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002621 }
2622 `)
2623
2624 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002625 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002626 cflags := cc.Args["cFlags"]
2627 if !strings.Contains(cflags, "-Imy_include") {
2628 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2629 }
2630}
2631
Logan Chien43d34c32017-12-20 01:17:32 +08002632func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2633 actual := module.Properties.AndroidMkRuntimeLibs
2634 if !reflect.DeepEqual(actual, expected) {
2635 t.Errorf("incorrect runtime_libs for shared libs"+
2636 "\nactual: %v"+
2637 "\nexpected: %v",
2638 actual,
2639 expected,
2640 )
2641 }
2642}
2643
2644const runtimeLibAndroidBp = `
2645 cc_library {
2646 name: "libvendor_available1",
2647 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002648 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002649 nocrt : true,
2650 system_shared_libs : [],
2651 }
2652 cc_library {
2653 name: "libvendor_available2",
2654 vendor_available: true,
2655 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002656 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002657 nocrt : true,
2658 system_shared_libs : [],
2659 }
2660 cc_library {
2661 name: "libvendor_available3",
2662 vendor_available: true,
2663 runtime_libs: ["libvendor_available1"],
2664 target: {
2665 vendor: {
2666 exclude_runtime_libs: ["libvendor_available1"],
2667 }
2668 },
Yi Konge7fe9912019-06-02 00:53:50 -07002669 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002670 nocrt : true,
2671 system_shared_libs : [],
2672 }
2673 cc_library {
2674 name: "libcore",
2675 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002676 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002677 nocrt : true,
2678 system_shared_libs : [],
2679 }
2680 cc_library {
2681 name: "libvendor1",
2682 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002683 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002684 nocrt : true,
2685 system_shared_libs : [],
2686 }
2687 cc_library {
2688 name: "libvendor2",
2689 vendor: true,
2690 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002691 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002692 nocrt : true,
2693 system_shared_libs : [],
2694 }
2695`
2696
2697func TestRuntimeLibs(t *testing.T) {
2698 ctx := testCc(t, runtimeLibAndroidBp)
2699
2700 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002701 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002702
2703 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2704 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2705
2706 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
2707 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2708
2709 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2710 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002711 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002712
2713 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2714 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
2715
2716 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2717 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
2718}
2719
2720func TestExcludeRuntimeLibs(t *testing.T) {
2721 ctx := testCc(t, runtimeLibAndroidBp)
2722
Colin Cross7113d202019-11-20 16:39:12 -08002723 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002724 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2725 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2726
Colin Crossfb0c16e2019-11-20 17:12:35 -08002727 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002728 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2729 checkRuntimeLibs(t, nil, module)
2730}
2731
2732func TestRuntimeLibsNoVndk(t *testing.T) {
2733 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2734
2735 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2736
Colin Cross7113d202019-11-20 16:39:12 -08002737 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002738
2739 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2740 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2741
2742 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2743 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
2744}
2745
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002746func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002747 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002748 actual := module.Properties.AndroidMkStaticLibs
2749 if !reflect.DeepEqual(actual, expected) {
2750 t.Errorf("incorrect static_libs"+
2751 "\nactual: %v"+
2752 "\nexpected: %v",
2753 actual,
2754 expected,
2755 )
2756 }
2757}
2758
2759const staticLibAndroidBp = `
2760 cc_library {
2761 name: "lib1",
2762 }
2763 cc_library {
2764 name: "lib2",
2765 static_libs: ["lib1"],
2766 }
2767`
2768
2769func TestStaticLibDepExport(t *testing.T) {
2770 ctx := testCc(t, staticLibAndroidBp)
2771
2772 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002773 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002774 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002775 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002776
2777 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002778 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002779 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2780 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002781 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002782}
2783
Jiyong Parkd08b6972017-09-26 10:50:54 +09002784var compilerFlagsTestCases = []struct {
2785 in string
2786 out bool
2787}{
2788 {
2789 in: "a",
2790 out: false,
2791 },
2792 {
2793 in: "-a",
2794 out: true,
2795 },
2796 {
2797 in: "-Ipath/to/something",
2798 out: false,
2799 },
2800 {
2801 in: "-isystempath/to/something",
2802 out: false,
2803 },
2804 {
2805 in: "--coverage",
2806 out: false,
2807 },
2808 {
2809 in: "-include a/b",
2810 out: true,
2811 },
2812 {
2813 in: "-include a/b c/d",
2814 out: false,
2815 },
2816 {
2817 in: "-DMACRO",
2818 out: true,
2819 },
2820 {
2821 in: "-DMAC RO",
2822 out: false,
2823 },
2824 {
2825 in: "-a -b",
2826 out: false,
2827 },
2828 {
2829 in: "-DMACRO=definition",
2830 out: true,
2831 },
2832 {
2833 in: "-DMACRO=defi nition",
2834 out: true, // TODO(jiyong): this should be false
2835 },
2836 {
2837 in: "-DMACRO(x)=x + 1",
2838 out: true,
2839 },
2840 {
2841 in: "-DMACRO=\"defi nition\"",
2842 out: true,
2843 },
2844}
2845
2846type mockContext struct {
2847 BaseModuleContext
2848 result bool
2849}
2850
2851func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2852 // CheckBadCompilerFlags calls this function when the flag should be rejected
2853 ctx.result = false
2854}
2855
2856func TestCompilerFlags(t *testing.T) {
2857 for _, testCase := range compilerFlagsTestCases {
2858 ctx := &mockContext{result: true}
2859 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
2860 if ctx.result != testCase.out {
2861 t.Errorf("incorrect output:")
2862 t.Errorf(" input: %#v", testCase.in)
2863 t.Errorf(" expected: %#v", testCase.out)
2864 t.Errorf(" got: %#v", ctx.result)
2865 }
2866 }
Jeff Gaston294356f2017-09-27 17:05:30 -07002867}
Jiyong Park374510b2018-03-19 18:23:01 +09002868
2869func TestVendorPublicLibraries(t *testing.T) {
2870 ctx := testCc(t, `
2871 cc_library_headers {
2872 name: "libvendorpublic_headers",
2873 export_include_dirs: ["my_include"],
2874 }
2875 vendor_public_library {
2876 name: "libvendorpublic",
2877 symbol_file: "",
2878 export_public_headers: ["libvendorpublic_headers"],
2879 }
2880 cc_library {
2881 name: "libvendorpublic",
2882 srcs: ["foo.c"],
2883 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002884 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002885 nocrt: true,
2886 }
2887
2888 cc_library {
2889 name: "libsystem",
2890 shared_libs: ["libvendorpublic"],
2891 vendor: false,
2892 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002893 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002894 nocrt: true,
2895 }
2896 cc_library {
2897 name: "libvendor",
2898 shared_libs: ["libvendorpublic"],
2899 vendor: true,
2900 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002901 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002902 nocrt: true,
2903 }
2904 `)
2905
Colin Cross7113d202019-11-20 16:39:12 -08002906 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08002907 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09002908
2909 // test if header search paths are correctly added
2910 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08002911 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09002912 cflags := cc.Args["cFlags"]
2913 if !strings.Contains(cflags, "-Imy_include") {
2914 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
2915 }
2916
2917 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08002918 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002919 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002920 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09002921 if !strings.Contains(libflags, stubPaths[0].String()) {
2922 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
2923 }
2924
2925 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08002926 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002927 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002928 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09002929 if !strings.Contains(libflags, stubPaths[0].String()) {
2930 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
2931 }
2932
2933}
Jiyong Park37b25202018-07-11 10:49:27 +09002934
2935func TestRecovery(t *testing.T) {
2936 ctx := testCc(t, `
2937 cc_library_shared {
2938 name: "librecovery",
2939 recovery: true,
2940 }
2941 cc_library_shared {
2942 name: "librecovery32",
2943 recovery: true,
2944 compile_multilib:"32",
2945 }
Jiyong Park5baac542018-08-28 09:55:37 +09002946 cc_library_shared {
2947 name: "libHalInRecovery",
2948 recovery_available: true,
2949 vendor: true,
2950 }
Jiyong Park37b25202018-07-11 10:49:27 +09002951 `)
2952
2953 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08002954 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09002955 if len(variants) != 1 || !android.InList(arm64, variants) {
2956 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
2957 }
2958
2959 variants = ctx.ModuleVariantsForTests("librecovery32")
2960 if android.InList(arm64, variants) {
2961 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
2962 }
Jiyong Park5baac542018-08-28 09:55:37 +09002963
2964 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
2965 if !recoveryModule.Platform() {
2966 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
2967 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09002968}
Jiyong Park5baac542018-08-28 09:55:37 +09002969
Jiyong Park7ed9de32018-10-15 22:25:07 +09002970func TestVersionedStubs(t *testing.T) {
2971 ctx := testCc(t, `
2972 cc_library_shared {
2973 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002974 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002975 stubs: {
2976 symbol_file: "foo.map.txt",
2977 versions: ["1", "2", "3"],
2978 },
2979 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002980
Jiyong Park7ed9de32018-10-15 22:25:07 +09002981 cc_library_shared {
2982 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002983 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002984 shared_libs: ["libFoo#1"],
2985 }`)
2986
2987 variants := ctx.ModuleVariantsForTests("libFoo")
2988 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08002989 "android_arm64_armv8-a_shared",
2990 "android_arm64_armv8-a_shared_1",
2991 "android_arm64_armv8-a_shared_2",
2992 "android_arm64_armv8-a_shared_3",
2993 "android_arm_armv7-a-neon_shared",
2994 "android_arm_armv7-a-neon_shared_1",
2995 "android_arm_armv7-a-neon_shared_2",
2996 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09002997 }
2998 variantsMismatch := false
2999 if len(variants) != len(expectedVariants) {
3000 variantsMismatch = true
3001 } else {
3002 for _, v := range expectedVariants {
3003 if !inList(v, variants) {
3004 variantsMismatch = false
3005 }
3006 }
3007 }
3008 if variantsMismatch {
3009 t.Errorf("variants of libFoo expected:\n")
3010 for _, v := range expectedVariants {
3011 t.Errorf("%q\n", v)
3012 }
3013 t.Errorf(", but got:\n")
3014 for _, v := range variants {
3015 t.Errorf("%q\n", v)
3016 }
3017 }
3018
Colin Cross7113d202019-11-20 16:39:12 -08003019 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003020 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003021 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003022 if !strings.Contains(libFlags, libFoo1StubPath) {
3023 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3024 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003025
Colin Cross7113d202019-11-20 16:39:12 -08003026 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003027 cFlags := libBarCompileRule.Args["cFlags"]
3028 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3029 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3030 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3031 }
Jiyong Park37b25202018-07-11 10:49:27 +09003032}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003033
Jooyung Hanb04a4992020-03-13 18:57:35 +09003034func TestVersioningMacro(t *testing.T) {
3035 for _, tc := range []struct{ moduleName, expected string }{
3036 {"libc", "__LIBC_API__"},
3037 {"libfoo", "__LIBFOO_API__"},
3038 {"libfoo@1", "__LIBFOO_1_API__"},
3039 {"libfoo-v1", "__LIBFOO_V1_API__"},
3040 {"libfoo.v1", "__LIBFOO_V1_API__"},
3041 } {
3042 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3043 }
3044}
3045
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003046func TestStaticExecutable(t *testing.T) {
3047 ctx := testCc(t, `
3048 cc_binary {
3049 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003050 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003051 static_executable: true,
3052 }`)
3053
Colin Cross7113d202019-11-20 16:39:12 -08003054 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003055 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3056 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003057 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003058 for _, lib := range systemStaticLibs {
3059 if !strings.Contains(libFlags, lib) {
3060 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3061 }
3062 }
3063 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3064 for _, lib := range systemSharedLibs {
3065 if strings.Contains(libFlags, lib) {
3066 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3067 }
3068 }
3069}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003070
3071func TestStaticDepsOrderWithStubs(t *testing.T) {
3072 ctx := testCc(t, `
3073 cc_binary {
3074 name: "mybin",
3075 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003076 static_libs: ["libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003077 static_executable: true,
3078 stl: "none",
3079 }
3080
3081 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003082 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003083 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003084 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003085 stl: "none",
3086 }
3087
3088 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003089 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003090 srcs: ["foo.c"],
3091 stl: "none",
3092 stubs: {
3093 versions: ["1"],
3094 },
3095 }`)
3096
Colin Cross7113d202019-11-20 16:39:12 -08003097 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Module().(*Module)
Jiyong Parke4bb9862019-02-01 00:31:10 +09003098 actual := mybin.depsInLinkOrder
Colin Crossf9aabd72020-02-15 11:29:50 -08003099 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003100
3101 if !reflect.DeepEqual(actual, expected) {
3102 t.Errorf("staticDeps orderings were not propagated correctly"+
3103 "\nactual: %v"+
3104 "\nexpected: %v",
3105 actual,
3106 expected,
3107 )
3108 }
3109}
Jooyung Han38002912019-05-16 04:01:54 +09003110
Jooyung Hand48f3c32019-08-23 11:18:57 +09003111func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3112 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3113 cc_library {
3114 name: "libA",
3115 srcs: ["foo.c"],
3116 shared_libs: ["libB"],
3117 stl: "none",
3118 }
3119
3120 cc_library {
3121 name: "libB",
3122 srcs: ["foo.c"],
3123 enabled: false,
3124 stl: "none",
3125 }
3126 `)
3127}
3128
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003129// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3130// correctly.
3131func TestFuzzTarget(t *testing.T) {
3132 ctx := testCc(t, `
3133 cc_fuzz {
3134 name: "fuzz_smoke_test",
3135 srcs: ["foo.c"],
3136 }`)
3137
Paul Duffin075c4172019-12-19 19:06:13 +00003138 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003139 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3140}
3141
Jiyong Park29074592019-07-07 16:27:47 +09003142func TestAidl(t *testing.T) {
3143}
3144
Jooyung Han38002912019-05-16 04:01:54 +09003145func assertString(t *testing.T, got, expected string) {
3146 t.Helper()
3147 if got != expected {
3148 t.Errorf("expected %q got %q", expected, got)
3149 }
3150}
3151
3152func assertArrayString(t *testing.T, got, expected []string) {
3153 t.Helper()
3154 if len(got) != len(expected) {
3155 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3156 return
3157 }
3158 for i := range got {
3159 if got[i] != expected[i] {
3160 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3161 i, expected[i], expected, got[i], got)
3162 return
3163 }
3164 }
3165}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003166
Jooyung Han0302a842019-10-30 18:43:49 +09003167func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3168 t.Helper()
3169 assertArrayString(t, android.SortedStringKeys(m), expected)
3170}
3171
Colin Crosse1bb5d02019-09-24 14:55:04 -07003172func TestDefaults(t *testing.T) {
3173 ctx := testCc(t, `
3174 cc_defaults {
3175 name: "defaults",
3176 srcs: ["foo.c"],
3177 static: {
3178 srcs: ["bar.c"],
3179 },
3180 shared: {
3181 srcs: ["baz.c"],
3182 },
3183 }
3184
3185 cc_library_static {
3186 name: "libstatic",
3187 defaults: ["defaults"],
3188 }
3189
3190 cc_library_shared {
3191 name: "libshared",
3192 defaults: ["defaults"],
3193 }
3194
3195 cc_library {
3196 name: "libboth",
3197 defaults: ["defaults"],
3198 }
3199
3200 cc_binary {
3201 name: "binary",
3202 defaults: ["defaults"],
3203 }`)
3204
3205 pathsToBase := func(paths android.Paths) []string {
3206 var ret []string
3207 for _, p := range paths {
3208 ret = append(ret, p.Base())
3209 }
3210 return ret
3211 }
3212
Colin Cross7113d202019-11-20 16:39:12 -08003213 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003214 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3215 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3216 }
Colin Cross7113d202019-11-20 16:39:12 -08003217 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003218 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3219 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3220 }
Colin Cross7113d202019-11-20 16:39:12 -08003221 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003222 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3223 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3224 }
3225
Colin Cross7113d202019-11-20 16:39:12 -08003226 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003227 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3228 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3229 }
Colin Cross7113d202019-11-20 16:39:12 -08003230 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003231 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3232 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3233 }
3234}
Colin Crosseabaedd2020-02-06 17:01:55 -08003235
3236func TestProductVariableDefaults(t *testing.T) {
3237 bp := `
3238 cc_defaults {
3239 name: "libfoo_defaults",
3240 srcs: ["foo.c"],
3241 cppflags: ["-DFOO"],
3242 product_variables: {
3243 debuggable: {
3244 cppflags: ["-DBAR"],
3245 },
3246 },
3247 }
3248
3249 cc_library {
3250 name: "libfoo",
3251 defaults: ["libfoo_defaults"],
3252 }
3253 `
3254
3255 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3256 config.TestProductVariables.Debuggable = BoolPtr(true)
3257
3258 ctx := CreateTestContext()
3259 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
3260 ctx.BottomUp("variable", android.VariableMutator).Parallel()
3261 })
3262 ctx.Register(config)
3263
3264 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3265 android.FailIfErrored(t, errs)
3266 _, errs = ctx.PrepareBuildActions(config)
3267 android.FailIfErrored(t, errs)
3268
3269 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
3270 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
3271 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
3272 }
3273}