blob: 8a1c8ed4c9b406139947b0655530a3eda2f9936f [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 }
542 `
543
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()
567 testBinaryPath := testBinary.dataPaths()[0].String()
568
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
Jooyung Han0302a842019-10-30 18:43:49 +0900579func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900580 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900581 cc_library {
582 name: "libvndk",
583 vendor_available: true,
584 vndk: {
585 enabled: true,
586 },
587 nocrt: true,
588 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900589 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900590
591 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
592 "LLNDK: libc.so",
593 "LLNDK: libdl.so",
594 "LLNDK: libft2.so",
595 "LLNDK: libm.so",
596 "VNDK-SP: libc++.so",
597 "VNDK-core: libvndk.so",
598 "VNDK-private: libft2.so",
599 })
Logan Chienf3511742017-10-31 18:04:35 +0800600}
601
Logan Chiend3c59a22018-03-29 14:08:15 +0800602func TestVndkDepError(t *testing.T) {
603 // Check whether an error is emitted when a VNDK lib depends on a system lib.
604 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
605 cc_library {
606 name: "libvndk",
607 vendor_available: true,
608 vndk: {
609 enabled: true,
610 },
611 shared_libs: ["libfwk"], // Cause error
612 nocrt: true,
613 }
614
615 cc_library {
616 name: "libfwk",
617 nocrt: true,
618 }
619 `)
620
621 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
622 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
623 cc_library {
624 name: "libvndk",
625 vendor_available: true,
626 vndk: {
627 enabled: true,
628 },
629 shared_libs: ["libvendor"], // Cause error
630 nocrt: true,
631 }
632
633 cc_library {
634 name: "libvendor",
635 vendor: true,
636 nocrt: true,
637 }
638 `)
639
640 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
641 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
642 cc_library {
643 name: "libvndk_sp",
644 vendor_available: true,
645 vndk: {
646 enabled: true,
647 support_system_process: true,
648 },
649 shared_libs: ["libfwk"], // Cause error
650 nocrt: true,
651 }
652
653 cc_library {
654 name: "libfwk",
655 nocrt: true,
656 }
657 `)
658
659 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
660 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
661 cc_library {
662 name: "libvndk_sp",
663 vendor_available: true,
664 vndk: {
665 enabled: true,
666 support_system_process: true,
667 },
668 shared_libs: ["libvendor"], // Cause error
669 nocrt: true,
670 }
671
672 cc_library {
673 name: "libvendor",
674 vendor: true,
675 nocrt: true,
676 }
677 `)
678
679 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
680 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
681 cc_library {
682 name: "libvndk_sp",
683 vendor_available: true,
684 vndk: {
685 enabled: true,
686 support_system_process: true,
687 },
688 shared_libs: ["libvndk"], // Cause error
689 nocrt: true,
690 }
691
692 cc_library {
693 name: "libvndk",
694 vendor_available: true,
695 vndk: {
696 enabled: true,
697 },
698 nocrt: true,
699 }
700 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900701
702 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
703 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
704 cc_library {
705 name: "libvndk",
706 vendor_available: true,
707 vndk: {
708 enabled: true,
709 },
710 shared_libs: ["libnonvndk"],
711 nocrt: true,
712 }
713
714 cc_library {
715 name: "libnonvndk",
716 vendor_available: true,
717 nocrt: true,
718 }
719 `)
720
721 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
722 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
723 cc_library {
724 name: "libvndkprivate",
725 vendor_available: false,
726 vndk: {
727 enabled: true,
728 },
729 shared_libs: ["libnonvndk"],
730 nocrt: true,
731 }
732
733 cc_library {
734 name: "libnonvndk",
735 vendor_available: true,
736 nocrt: true,
737 }
738 `)
739
740 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
741 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
742 cc_library {
743 name: "libvndksp",
744 vendor_available: true,
745 vndk: {
746 enabled: true,
747 support_system_process: true,
748 },
749 shared_libs: ["libnonvndk"],
750 nocrt: true,
751 }
752
753 cc_library {
754 name: "libnonvndk",
755 vendor_available: true,
756 nocrt: true,
757 }
758 `)
759
760 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
761 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
762 cc_library {
763 name: "libvndkspprivate",
764 vendor_available: false,
765 vndk: {
766 enabled: true,
767 support_system_process: true,
768 },
769 shared_libs: ["libnonvndk"],
770 nocrt: true,
771 }
772
773 cc_library {
774 name: "libnonvndk",
775 vendor_available: true,
776 nocrt: true,
777 }
778 `)
779}
780
781func TestDoubleLoadbleDep(t *testing.T) {
782 // okay to link : LLNDK -> double_loadable VNDK
783 testCc(t, `
784 cc_library {
785 name: "libllndk",
786 shared_libs: ["libdoubleloadable"],
787 }
788
789 llndk_library {
790 name: "libllndk",
791 symbol_file: "",
792 }
793
794 cc_library {
795 name: "libdoubleloadable",
796 vendor_available: true,
797 vndk: {
798 enabled: true,
799 },
800 double_loadable: true,
801 }
802 `)
803 // okay to link : LLNDK -> VNDK-SP
804 testCc(t, `
805 cc_library {
806 name: "libllndk",
807 shared_libs: ["libvndksp"],
808 }
809
810 llndk_library {
811 name: "libllndk",
812 symbol_file: "",
813 }
814
815 cc_library {
816 name: "libvndksp",
817 vendor_available: true,
818 vndk: {
819 enabled: true,
820 support_system_process: true,
821 },
822 }
823 `)
824 // okay to link : double_loadable -> double_loadable
825 testCc(t, `
826 cc_library {
827 name: "libdoubleloadable1",
828 shared_libs: ["libdoubleloadable2"],
829 vendor_available: true,
830 double_loadable: true,
831 }
832
833 cc_library {
834 name: "libdoubleloadable2",
835 vendor_available: true,
836 double_loadable: true,
837 }
838 `)
839 // okay to link : double_loadable VNDK -> double_loadable VNDK private
840 testCc(t, `
841 cc_library {
842 name: "libdoubleloadable",
843 vendor_available: true,
844 vndk: {
845 enabled: true,
846 },
847 double_loadable: true,
848 shared_libs: ["libnondoubleloadable"],
849 }
850
851 cc_library {
852 name: "libnondoubleloadable",
853 vendor_available: false,
854 vndk: {
855 enabled: true,
856 },
857 double_loadable: true,
858 }
859 `)
860 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
861 testCc(t, `
862 cc_library {
863 name: "libllndk",
864 shared_libs: ["libcoreonly"],
865 }
866
867 llndk_library {
868 name: "libllndk",
869 symbol_file: "",
870 }
871
872 cc_library {
873 name: "libcoreonly",
874 shared_libs: ["libvendoravailable"],
875 }
876
877 // indirect dependency of LLNDK
878 cc_library {
879 name: "libvendoravailable",
880 vendor_available: true,
881 double_loadable: true,
882 }
883 `)
884}
885
Inseob Kim8471cda2019-11-15 09:59:12 +0900886func TestVendorSnapshot(t *testing.T) {
887 bp := `
888 cc_library {
889 name: "libvndk",
890 vendor_available: true,
891 vndk: {
892 enabled: true,
893 },
894 nocrt: true,
895 }
896
897 cc_library {
898 name: "libvendor",
899 vendor: true,
900 nocrt: true,
901 }
902
903 cc_library {
904 name: "libvendor_available",
905 vendor_available: true,
906 nocrt: true,
907 }
908
909 cc_library_headers {
910 name: "libvendor_headers",
911 vendor_available: true,
912 nocrt: true,
913 }
914
915 cc_binary {
916 name: "vendor_bin",
917 vendor: true,
918 nocrt: true,
919 }
920
921 cc_binary {
922 name: "vendor_available_bin",
923 vendor_available: true,
924 nocrt: true,
925 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900926
927 toolchain_library {
928 name: "libb",
929 vendor_available: true,
930 src: "libb.a",
931 }
Inseob Kim1042d292020-06-01 23:23:05 +0900932
933 cc_object {
934 name: "obj",
935 vendor_available: true,
936 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900937`
938 config := TestConfig(buildDir, android.Android, nil, bp, nil)
939 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
940 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
941 ctx := testCcWithConfig(t, config)
942
943 // Check Vendor snapshot output.
944
945 snapshotDir := "vendor-snapshot"
946 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +0900947 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
948
949 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +0900950
951 for _, arch := range [][]string{
952 []string{"arm64", "armv8-a"},
953 []string{"arm", "armv7-a-neon"},
954 } {
955 archType := arch[0]
956 archVariant := arch[1]
957 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
958
959 // For shared libraries, only non-VNDK vendor_available modules are captured
960 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
961 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +0900962 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
963 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
964 jsonFiles = append(jsonFiles,
965 filepath.Join(sharedDir, "libvendor.so.json"),
966 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +0900967
968 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
969 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
970 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +0900971 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
972 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
973 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
974 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
975 jsonFiles = append(jsonFiles,
976 filepath.Join(staticDir, "libb.a.json"),
977 filepath.Join(staticDir, "libvndk.a.json"),
978 filepath.Join(staticDir, "libvendor.a.json"),
979 filepath.Join(staticDir, "libvendor_available.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +0900980
Inseob Kim7f283f42020-06-01 21:53:49 +0900981 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +0900982 if archType == "arm64" {
983 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
984 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +0900985 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
986 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
987 jsonFiles = append(jsonFiles,
988 filepath.Join(binaryDir, "vendor_bin.json"),
989 filepath.Join(binaryDir, "vendor_available_bin.json"))
990 }
991
992 // For header libraries, all vendor:true and vendor_available modules are captured.
993 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
994 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +0900995
996 // For object modules, all vendor:true and vendor_available modules are captured.
997 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
998 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
999 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1000 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001001 }
1002
1003 for _, jsonFile := range jsonFiles {
1004 // verify all json files exist
1005 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1006 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001007 }
1008 }
1009}
1010
Jooyung Hana70f0672019-01-18 15:20:43 +09001011func TestDoubleLoadableDepError(t *testing.T) {
1012 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1013 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1014 cc_library {
1015 name: "libllndk",
1016 shared_libs: ["libnondoubleloadable"],
1017 }
1018
1019 llndk_library {
1020 name: "libllndk",
1021 symbol_file: "",
1022 }
1023
1024 cc_library {
1025 name: "libnondoubleloadable",
1026 vendor_available: true,
1027 vndk: {
1028 enabled: true,
1029 },
1030 }
1031 `)
1032
1033 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1034 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1035 cc_library {
1036 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001037 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001038 shared_libs: ["libnondoubleloadable"],
1039 }
1040
1041 llndk_library {
1042 name: "libllndk",
1043 symbol_file: "",
1044 }
1045
1046 cc_library {
1047 name: "libnondoubleloadable",
1048 vendor_available: true,
1049 }
1050 `)
1051
1052 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
1053 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1054 cc_library {
1055 name: "libdoubleloadable",
1056 vendor_available: true,
1057 double_loadable: true,
1058 shared_libs: ["libnondoubleloadable"],
1059 }
1060
1061 cc_library {
1062 name: "libnondoubleloadable",
1063 vendor_available: true,
1064 }
1065 `)
1066
1067 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
1068 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1069 cc_library {
1070 name: "libdoubleloadable",
1071 vendor_available: true,
1072 double_loadable: true,
1073 shared_libs: ["libnondoubleloadable"],
1074 }
1075
1076 cc_library {
1077 name: "libnondoubleloadable",
1078 vendor_available: true,
1079 vndk: {
1080 enabled: true,
1081 },
1082 }
1083 `)
1084
1085 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
1086 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1087 cc_library {
1088 name: "libdoubleloadable",
1089 vendor_available: true,
1090 vndk: {
1091 enabled: true,
1092 },
1093 double_loadable: true,
1094 shared_libs: ["libnondoubleloadable"],
1095 }
1096
1097 cc_library {
1098 name: "libnondoubleloadable",
1099 vendor_available: false,
1100 vndk: {
1101 enabled: true,
1102 },
1103 }
1104 `)
1105
1106 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1107 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1108 cc_library {
1109 name: "libllndk",
1110 shared_libs: ["libcoreonly"],
1111 }
1112
1113 llndk_library {
1114 name: "libllndk",
1115 symbol_file: "",
1116 }
1117
1118 cc_library {
1119 name: "libcoreonly",
1120 shared_libs: ["libvendoravailable"],
1121 }
1122
1123 // indirect dependency of LLNDK
1124 cc_library {
1125 name: "libvendoravailable",
1126 vendor_available: true,
1127 }
1128 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001129}
1130
Logan Chienf3511742017-10-31 18:04:35 +08001131func TestVndkExt(t *testing.T) {
1132 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001133 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001134 cc_library {
1135 name: "libvndk",
1136 vendor_available: true,
1137 vndk: {
1138 enabled: true,
1139 },
1140 nocrt: true,
1141 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001142 cc_library {
1143 name: "libvndk2",
1144 vendor_available: true,
1145 vndk: {
1146 enabled: true,
1147 },
1148 target: {
1149 vendor: {
1150 suffix: "-suffix",
1151 },
1152 },
1153 nocrt: true,
1154 }
Logan Chienf3511742017-10-31 18:04:35 +08001155
1156 cc_library {
1157 name: "libvndk_ext",
1158 vendor: true,
1159 vndk: {
1160 enabled: true,
1161 extends: "libvndk",
1162 },
1163 nocrt: true,
1164 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001165
1166 cc_library {
1167 name: "libvndk2_ext",
1168 vendor: true,
1169 vndk: {
1170 enabled: true,
1171 extends: "libvndk2",
1172 },
1173 nocrt: true,
1174 }
Logan Chienf3511742017-10-31 18:04:35 +08001175
Justin Yun0ecf0b22020-02-28 15:07:59 +09001176 cc_library {
1177 name: "libvndk_ext_product",
1178 product_specific: true,
1179 vndk: {
1180 enabled: true,
1181 extends: "libvndk",
1182 },
1183 nocrt: true,
1184 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001185
Justin Yun0ecf0b22020-02-28 15:07:59 +09001186 cc_library {
1187 name: "libvndk2_ext_product",
1188 product_specific: true,
1189 vndk: {
1190 enabled: true,
1191 extends: "libvndk2",
1192 },
1193 nocrt: true,
1194 }
1195 `
1196 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1197 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1198 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1199 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1200
1201 ctx := testCcWithConfig(t, config)
1202
1203 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1204 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1205
1206 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1207 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1208
1209 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1210 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001211}
1212
Logan Chiend3c59a22018-03-29 14:08:15 +08001213func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001214 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1215 ctx := testCcNoVndk(t, `
1216 cc_library {
1217 name: "libvndk",
1218 vendor_available: true,
1219 vndk: {
1220 enabled: true,
1221 },
1222 nocrt: true,
1223 }
1224
1225 cc_library {
1226 name: "libvndk_ext",
1227 vendor: true,
1228 vndk: {
1229 enabled: true,
1230 extends: "libvndk",
1231 },
1232 nocrt: true,
1233 }
1234 `)
1235
1236 // Ensures that the core variant of "libvndk_ext" can be found.
1237 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1238 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1239 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1240 }
1241}
1242
Justin Yun0ecf0b22020-02-28 15:07:59 +09001243func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1244 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
1245 ctx := testCc(t, `
1246 cc_library {
1247 name: "libvndk",
1248 vendor_available: true,
1249 vndk: {
1250 enabled: true,
1251 },
1252 nocrt: true,
1253 }
1254
1255 cc_library {
1256 name: "libvndk_ext_product",
1257 product_specific: true,
1258 vndk: {
1259 enabled: true,
1260 extends: "libvndk",
1261 },
1262 nocrt: true,
1263 }
1264 `)
1265
1266 // Ensures that the core variant of "libvndk_ext_product" can be found.
1267 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1268 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1269 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1270 }
1271}
1272
Logan Chienf3511742017-10-31 18:04:35 +08001273func TestVndkExtError(t *testing.T) {
1274 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001275 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001276 cc_library {
1277 name: "libvndk",
1278 vendor_available: true,
1279 vndk: {
1280 enabled: true,
1281 },
1282 nocrt: true,
1283 }
1284
1285 cc_library {
1286 name: "libvndk_ext",
1287 vndk: {
1288 enabled: true,
1289 extends: "libvndk",
1290 },
1291 nocrt: true,
1292 }
1293 `)
1294
1295 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1296 cc_library {
1297 name: "libvndk",
1298 vendor_available: true,
1299 vndk: {
1300 enabled: true,
1301 },
1302 nocrt: true,
1303 }
1304
1305 cc_library {
1306 name: "libvndk_ext",
1307 vendor: true,
1308 vndk: {
1309 enabled: true,
1310 },
1311 nocrt: true,
1312 }
1313 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001314
1315 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1316 cc_library {
1317 name: "libvndk",
1318 vendor_available: true,
1319 vndk: {
1320 enabled: true,
1321 },
1322 nocrt: true,
1323 }
1324
1325 cc_library {
1326 name: "libvndk_ext_product",
1327 product_specific: true,
1328 vndk: {
1329 enabled: true,
1330 },
1331 nocrt: true,
1332 }
1333 `)
1334
1335 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1336 cc_library {
1337 name: "libvndk",
1338 vendor_available: true,
1339 vndk: {
1340 enabled: true,
1341 },
1342 nocrt: true,
1343 }
1344
1345 cc_library {
1346 name: "libvndk_ext_product",
1347 product_specific: true,
1348 vendor_available: true,
1349 vndk: {
1350 enabled: true,
1351 extends: "libvndk",
1352 },
1353 nocrt: true,
1354 }
1355 `)
Logan Chienf3511742017-10-31 18:04:35 +08001356}
1357
1358func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1359 // This test ensures an error is emitted for inconsistent support_system_process.
1360 testCcError(t, "module \".*\" with mismatched support_system_process", `
1361 cc_library {
1362 name: "libvndk",
1363 vendor_available: true,
1364 vndk: {
1365 enabled: true,
1366 },
1367 nocrt: true,
1368 }
1369
1370 cc_library {
1371 name: "libvndk_sp_ext",
1372 vendor: true,
1373 vndk: {
1374 enabled: true,
1375 extends: "libvndk",
1376 support_system_process: true,
1377 },
1378 nocrt: true,
1379 }
1380 `)
1381
1382 testCcError(t, "module \".*\" with mismatched support_system_process", `
1383 cc_library {
1384 name: "libvndk_sp",
1385 vendor_available: true,
1386 vndk: {
1387 enabled: true,
1388 support_system_process: true,
1389 },
1390 nocrt: true,
1391 }
1392
1393 cc_library {
1394 name: "libvndk_ext",
1395 vendor: true,
1396 vndk: {
1397 enabled: true,
1398 extends: "libvndk_sp",
1399 },
1400 nocrt: true,
1401 }
1402 `)
1403}
1404
1405func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001406 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08001407 // with `vendor_available: false`.
1408 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1409 cc_library {
1410 name: "libvndk",
1411 vendor_available: false,
1412 vndk: {
1413 enabled: true,
1414 },
1415 nocrt: true,
1416 }
1417
1418 cc_library {
1419 name: "libvndk_ext",
1420 vendor: true,
1421 vndk: {
1422 enabled: true,
1423 extends: "libvndk",
1424 },
1425 nocrt: true,
1426 }
1427 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001428
1429 testCcErrorProductVndk(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1430 cc_library {
1431 name: "libvndk",
1432 vendor_available: false,
1433 vndk: {
1434 enabled: true,
1435 },
1436 nocrt: true,
1437 }
1438
1439 cc_library {
1440 name: "libvndk_ext_product",
1441 product_specific: true,
1442 vndk: {
1443 enabled: true,
1444 extends: "libvndk",
1445 },
1446 nocrt: true,
1447 }
1448 `)
Logan Chienf3511742017-10-31 18:04:35 +08001449}
1450
Logan Chiend3c59a22018-03-29 14:08:15 +08001451func TestVendorModuleUseVndkExt(t *testing.T) {
1452 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001453 testCc(t, `
1454 cc_library {
1455 name: "libvndk",
1456 vendor_available: true,
1457 vndk: {
1458 enabled: true,
1459 },
1460 nocrt: true,
1461 }
1462
1463 cc_library {
1464 name: "libvndk_ext",
1465 vendor: true,
1466 vndk: {
1467 enabled: true,
1468 extends: "libvndk",
1469 },
1470 nocrt: true,
1471 }
1472
1473 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001474 name: "libvndk_sp",
1475 vendor_available: true,
1476 vndk: {
1477 enabled: true,
1478 support_system_process: true,
1479 },
1480 nocrt: true,
1481 }
1482
1483 cc_library {
1484 name: "libvndk_sp_ext",
1485 vendor: true,
1486 vndk: {
1487 enabled: true,
1488 extends: "libvndk_sp",
1489 support_system_process: true,
1490 },
1491 nocrt: true,
1492 }
1493
1494 cc_library {
1495 name: "libvendor",
1496 vendor: true,
1497 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1498 nocrt: true,
1499 }
1500 `)
1501}
1502
Logan Chiend3c59a22018-03-29 14:08:15 +08001503func TestVndkExtUseVendorLib(t *testing.T) {
1504 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001505 testCc(t, `
1506 cc_library {
1507 name: "libvndk",
1508 vendor_available: true,
1509 vndk: {
1510 enabled: true,
1511 },
1512 nocrt: true,
1513 }
1514
1515 cc_library {
1516 name: "libvndk_ext",
1517 vendor: true,
1518 vndk: {
1519 enabled: true,
1520 extends: "libvndk",
1521 },
1522 shared_libs: ["libvendor"],
1523 nocrt: true,
1524 }
1525
1526 cc_library {
1527 name: "libvendor",
1528 vendor: true,
1529 nocrt: true,
1530 }
1531 `)
Logan Chienf3511742017-10-31 18:04:35 +08001532
Logan Chiend3c59a22018-03-29 14:08:15 +08001533 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1534 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001535 cc_library {
1536 name: "libvndk_sp",
1537 vendor_available: true,
1538 vndk: {
1539 enabled: true,
1540 support_system_process: true,
1541 },
1542 nocrt: true,
1543 }
1544
1545 cc_library {
1546 name: "libvndk_sp_ext",
1547 vendor: true,
1548 vndk: {
1549 enabled: true,
1550 extends: "libvndk_sp",
1551 support_system_process: true,
1552 },
1553 shared_libs: ["libvendor"], // Cause an error
1554 nocrt: true,
1555 }
1556
1557 cc_library {
1558 name: "libvendor",
1559 vendor: true,
1560 nocrt: true,
1561 }
1562 `)
1563}
1564
Justin Yun0ecf0b22020-02-28 15:07:59 +09001565func TestProductVndkExtDependency(t *testing.T) {
1566 bp := `
1567 cc_library {
1568 name: "libvndk",
1569 vendor_available: true,
1570 vndk: {
1571 enabled: true,
1572 },
1573 nocrt: true,
1574 }
1575
1576 cc_library {
1577 name: "libvndk_ext_product",
1578 product_specific: true,
1579 vndk: {
1580 enabled: true,
1581 extends: "libvndk",
1582 },
1583 shared_libs: ["libproduct_for_vndklibs"],
1584 nocrt: true,
1585 }
1586
1587 cc_library {
1588 name: "libvndk_sp",
1589 vendor_available: true,
1590 vndk: {
1591 enabled: true,
1592 support_system_process: true,
1593 },
1594 nocrt: true,
1595 }
1596
1597 cc_library {
1598 name: "libvndk_sp_ext_product",
1599 product_specific: true,
1600 vndk: {
1601 enabled: true,
1602 extends: "libvndk_sp",
1603 support_system_process: true,
1604 },
1605 shared_libs: ["libproduct_for_vndklibs"],
1606 nocrt: true,
1607 }
1608
1609 cc_library {
1610 name: "libproduct",
1611 product_specific: true,
1612 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1613 nocrt: true,
1614 }
1615
1616 cc_library {
1617 name: "libproduct_for_vndklibs",
1618 product_specific: true,
1619 nocrt: true,
1620 }
1621 `
1622 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1623 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1624 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1625 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1626
1627 testCcWithConfig(t, config)
1628}
1629
Logan Chiend3c59a22018-03-29 14:08:15 +08001630func TestVndkSpExtUseVndkError(t *testing.T) {
1631 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1632 // library.
1633 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1634 cc_library {
1635 name: "libvndk",
1636 vendor_available: true,
1637 vndk: {
1638 enabled: true,
1639 },
1640 nocrt: true,
1641 }
1642
1643 cc_library {
1644 name: "libvndk_sp",
1645 vendor_available: true,
1646 vndk: {
1647 enabled: true,
1648 support_system_process: true,
1649 },
1650 nocrt: true,
1651 }
1652
1653 cc_library {
1654 name: "libvndk_sp_ext",
1655 vendor: true,
1656 vndk: {
1657 enabled: true,
1658 extends: "libvndk_sp",
1659 support_system_process: true,
1660 },
1661 shared_libs: ["libvndk"], // Cause an error
1662 nocrt: true,
1663 }
1664 `)
1665
1666 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1667 // library.
1668 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1669 cc_library {
1670 name: "libvndk",
1671 vendor_available: true,
1672 vndk: {
1673 enabled: true,
1674 },
1675 nocrt: true,
1676 }
1677
1678 cc_library {
1679 name: "libvndk_ext",
1680 vendor: true,
1681 vndk: {
1682 enabled: true,
1683 extends: "libvndk",
1684 },
1685 nocrt: true,
1686 }
1687
1688 cc_library {
1689 name: "libvndk_sp",
1690 vendor_available: true,
1691 vndk: {
1692 enabled: true,
1693 support_system_process: true,
1694 },
1695 nocrt: true,
1696 }
1697
1698 cc_library {
1699 name: "libvndk_sp_ext",
1700 vendor: true,
1701 vndk: {
1702 enabled: true,
1703 extends: "libvndk_sp",
1704 support_system_process: true,
1705 },
1706 shared_libs: ["libvndk_ext"], // Cause an error
1707 nocrt: true,
1708 }
1709 `)
1710}
1711
1712func TestVndkUseVndkExtError(t *testing.T) {
1713 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1714 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001715 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1716 cc_library {
1717 name: "libvndk",
1718 vendor_available: true,
1719 vndk: {
1720 enabled: true,
1721 },
1722 nocrt: true,
1723 }
1724
1725 cc_library {
1726 name: "libvndk_ext",
1727 vendor: true,
1728 vndk: {
1729 enabled: true,
1730 extends: "libvndk",
1731 },
1732 nocrt: true,
1733 }
1734
1735 cc_library {
1736 name: "libvndk2",
1737 vendor_available: true,
1738 vndk: {
1739 enabled: true,
1740 },
1741 shared_libs: ["libvndk_ext"],
1742 nocrt: true,
1743 }
1744 `)
1745
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001746 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001747 cc_library {
1748 name: "libvndk",
1749 vendor_available: true,
1750 vndk: {
1751 enabled: true,
1752 },
1753 nocrt: true,
1754 }
1755
1756 cc_library {
1757 name: "libvndk_ext",
1758 vendor: true,
1759 vndk: {
1760 enabled: true,
1761 extends: "libvndk",
1762 },
1763 nocrt: true,
1764 }
1765
1766 cc_library {
1767 name: "libvndk2",
1768 vendor_available: true,
1769 vndk: {
1770 enabled: true,
1771 },
1772 target: {
1773 vendor: {
1774 shared_libs: ["libvndk_ext"],
1775 },
1776 },
1777 nocrt: true,
1778 }
1779 `)
1780
1781 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1782 cc_library {
1783 name: "libvndk_sp",
1784 vendor_available: true,
1785 vndk: {
1786 enabled: true,
1787 support_system_process: true,
1788 },
1789 nocrt: true,
1790 }
1791
1792 cc_library {
1793 name: "libvndk_sp_ext",
1794 vendor: true,
1795 vndk: {
1796 enabled: true,
1797 extends: "libvndk_sp",
1798 support_system_process: true,
1799 },
1800 nocrt: true,
1801 }
1802
1803 cc_library {
1804 name: "libvndk_sp_2",
1805 vendor_available: true,
1806 vndk: {
1807 enabled: true,
1808 support_system_process: true,
1809 },
1810 shared_libs: ["libvndk_sp_ext"],
1811 nocrt: true,
1812 }
1813 `)
1814
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001815 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001816 cc_library {
1817 name: "libvndk_sp",
1818 vendor_available: true,
1819 vndk: {
1820 enabled: true,
1821 },
1822 nocrt: true,
1823 }
1824
1825 cc_library {
1826 name: "libvndk_sp_ext",
1827 vendor: true,
1828 vndk: {
1829 enabled: true,
1830 extends: "libvndk_sp",
1831 },
1832 nocrt: true,
1833 }
1834
1835 cc_library {
1836 name: "libvndk_sp2",
1837 vendor_available: true,
1838 vndk: {
1839 enabled: true,
1840 },
1841 target: {
1842 vendor: {
1843 shared_libs: ["libvndk_sp_ext"],
1844 },
1845 },
1846 nocrt: true,
1847 }
1848 `)
1849}
1850
Justin Yun5f7f7e82019-11-18 19:52:14 +09001851func TestEnforceProductVndkVersion(t *testing.T) {
1852 bp := `
1853 cc_library {
1854 name: "libllndk",
1855 }
1856 llndk_library {
1857 name: "libllndk",
1858 symbol_file: "",
1859 }
1860 cc_library {
1861 name: "libvndk",
1862 vendor_available: true,
1863 vndk: {
1864 enabled: true,
1865 },
1866 nocrt: true,
1867 }
1868 cc_library {
1869 name: "libvndk_sp",
1870 vendor_available: true,
1871 vndk: {
1872 enabled: true,
1873 support_system_process: true,
1874 },
1875 nocrt: true,
1876 }
1877 cc_library {
1878 name: "libva",
1879 vendor_available: true,
1880 nocrt: true,
1881 }
1882 cc_library {
1883 name: "libproduct_va",
1884 product_specific: true,
1885 vendor_available: true,
1886 nocrt: true,
1887 }
1888 cc_library {
1889 name: "libprod",
1890 product_specific: true,
1891 shared_libs: [
1892 "libllndk",
1893 "libvndk",
1894 "libvndk_sp",
1895 "libva",
1896 "libproduct_va",
1897 ],
1898 nocrt: true,
1899 }
1900 cc_library {
1901 name: "libvendor",
1902 vendor: true,
1903 shared_libs: [
1904 "libllndk",
1905 "libvndk",
1906 "libvndk_sp",
1907 "libva",
1908 "libproduct_va",
1909 ],
1910 nocrt: true,
1911 }
1912 `
1913
1914 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1915 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1916 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1917 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1918
1919 ctx := testCcWithConfig(t, config)
1920
Justin Yun0ecf0b22020-02-28 15:07:59 +09001921 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "", productVariant)
1922 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "", productVariant)
Justin Yun5f7f7e82019-11-18 19:52:14 +09001923}
1924
1925func TestEnforceProductVndkVersionErrors(t *testing.T) {
1926 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1927 cc_library {
1928 name: "libprod",
1929 product_specific: true,
1930 shared_libs: [
1931 "libvendor",
1932 ],
1933 nocrt: true,
1934 }
1935 cc_library {
1936 name: "libvendor",
1937 vendor: true,
1938 nocrt: true,
1939 }
1940 `)
1941 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1942 cc_library {
1943 name: "libprod",
1944 product_specific: true,
1945 shared_libs: [
1946 "libsystem",
1947 ],
1948 nocrt: true,
1949 }
1950 cc_library {
1951 name: "libsystem",
1952 nocrt: true,
1953 }
1954 `)
1955 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
1956 cc_library {
1957 name: "libprod",
1958 product_specific: true,
1959 shared_libs: [
1960 "libvndk_private",
1961 ],
1962 nocrt: true,
1963 }
1964 cc_library {
1965 name: "libvndk_private",
1966 vendor_available: false,
1967 vndk: {
1968 enabled: true,
1969 },
1970 nocrt: true,
1971 }
1972 `)
1973 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1974 cc_library {
1975 name: "libprod",
1976 product_specific: true,
1977 shared_libs: [
1978 "libsystem_ext",
1979 ],
1980 nocrt: true,
1981 }
1982 cc_library {
1983 name: "libsystem_ext",
1984 system_ext_specific: true,
1985 nocrt: true,
1986 }
1987 `)
1988 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
1989 cc_library {
1990 name: "libsystem",
1991 shared_libs: [
1992 "libproduct_va",
1993 ],
1994 nocrt: true,
1995 }
1996 cc_library {
1997 name: "libproduct_va",
1998 product_specific: true,
1999 vendor_available: true,
2000 nocrt: true,
2001 }
2002 `)
2003}
2004
Jooyung Han38002912019-05-16 04:01:54 +09002005func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002006 bp := `
2007 cc_library {
2008 name: "libvndk",
2009 vendor_available: true,
2010 vndk: {
2011 enabled: true,
2012 },
2013 }
2014 cc_library {
2015 name: "libvndksp",
2016 vendor_available: true,
2017 vndk: {
2018 enabled: true,
2019 support_system_process: true,
2020 },
2021 }
2022 cc_library {
2023 name: "libvndkprivate",
2024 vendor_available: false,
2025 vndk: {
2026 enabled: true,
2027 },
2028 }
2029 cc_library {
2030 name: "libvendor",
2031 vendor: true,
2032 }
2033 cc_library {
2034 name: "libvndkext",
2035 vendor: true,
2036 vndk: {
2037 enabled: true,
2038 extends: "libvndk",
2039 },
2040 }
2041 vndk_prebuilt_shared {
2042 name: "prevndk",
2043 version: "27",
2044 target_arch: "arm",
2045 binder32bit: true,
2046 vendor_available: true,
2047 vndk: {
2048 enabled: true,
2049 },
2050 arch: {
2051 arm: {
2052 srcs: ["liba.so"],
2053 },
2054 },
2055 }
2056 cc_library {
2057 name: "libllndk",
2058 }
2059 llndk_library {
2060 name: "libllndk",
2061 symbol_file: "",
2062 }
2063 cc_library {
2064 name: "libllndkprivate",
2065 }
2066 llndk_library {
2067 name: "libllndkprivate",
2068 vendor_available: false,
2069 symbol_file: "",
2070 }`
2071
2072 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002073 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2074 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2075 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002076 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002077
Jooyung Han0302a842019-10-30 18:43:49 +09002078 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002079 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09002080 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002081 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09002082 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002083 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09002084 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002085 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09002086
Colin Crossfb0c16e2019-11-20 17:12:35 -08002087 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002088
Jooyung Han38002912019-05-16 04:01:54 +09002089 tests := []struct {
2090 variant string
2091 name string
2092 expected string
2093 }{
2094 {vendorVariant, "libvndk", "native:vndk"},
2095 {vendorVariant, "libvndksp", "native:vndk"},
2096 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2097 {vendorVariant, "libvendor", "native:vendor"},
2098 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09002099 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002100 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002101 {coreVariant, "libvndk", "native:platform"},
2102 {coreVariant, "libvndkprivate", "native:platform"},
2103 {coreVariant, "libllndk", "native:platform"},
2104 }
2105 for _, test := range tests {
2106 t.Run(test.name, func(t *testing.T) {
2107 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2108 assertString(t, module.makeLinkType, test.expected)
2109 })
2110 }
2111}
2112
Colin Cross0af4b842015-04-30 16:36:18 -07002113var (
2114 str11 = "01234567891"
2115 str10 = str11[:10]
2116 str9 = str11[:9]
2117 str5 = str11[:5]
2118 str4 = str11[:4]
2119)
2120
2121var splitListForSizeTestCases = []struct {
2122 in []string
2123 out [][]string
2124 size int
2125}{
2126 {
2127 in: []string{str10},
2128 out: [][]string{{str10}},
2129 size: 10,
2130 },
2131 {
2132 in: []string{str9},
2133 out: [][]string{{str9}},
2134 size: 10,
2135 },
2136 {
2137 in: []string{str5},
2138 out: [][]string{{str5}},
2139 size: 10,
2140 },
2141 {
2142 in: []string{str11},
2143 out: nil,
2144 size: 10,
2145 },
2146 {
2147 in: []string{str10, str10},
2148 out: [][]string{{str10}, {str10}},
2149 size: 10,
2150 },
2151 {
2152 in: []string{str9, str10},
2153 out: [][]string{{str9}, {str10}},
2154 size: 10,
2155 },
2156 {
2157 in: []string{str10, str9},
2158 out: [][]string{{str10}, {str9}},
2159 size: 10,
2160 },
2161 {
2162 in: []string{str5, str4},
2163 out: [][]string{{str5, str4}},
2164 size: 10,
2165 },
2166 {
2167 in: []string{str5, str4, str5},
2168 out: [][]string{{str5, str4}, {str5}},
2169 size: 10,
2170 },
2171 {
2172 in: []string{str5, str4, str5, str4},
2173 out: [][]string{{str5, str4}, {str5, str4}},
2174 size: 10,
2175 },
2176 {
2177 in: []string{str5, str4, str5, str5},
2178 out: [][]string{{str5, str4}, {str5}, {str5}},
2179 size: 10,
2180 },
2181 {
2182 in: []string{str5, str5, str5, str4},
2183 out: [][]string{{str5}, {str5}, {str5, str4}},
2184 size: 10,
2185 },
2186 {
2187 in: []string{str9, str11},
2188 out: nil,
2189 size: 10,
2190 },
2191 {
2192 in: []string{str11, str9},
2193 out: nil,
2194 size: 10,
2195 },
2196}
2197
2198func TestSplitListForSize(t *testing.T) {
2199 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08002200 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07002201
2202 var outStrings [][]string
2203
2204 if len(out) > 0 {
2205 outStrings = make([][]string, len(out))
2206 for i, o := range out {
2207 outStrings[i] = o.Strings()
2208 }
2209 }
2210
2211 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07002212 t.Errorf("incorrect output:")
2213 t.Errorf(" input: %#v", testCase.in)
2214 t.Errorf(" size: %d", testCase.size)
2215 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07002216 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07002217 }
2218 }
2219}
Jeff Gaston294356f2017-09-27 17:05:30 -07002220
2221var staticLinkDepOrderTestCases = []struct {
2222 // This is a string representation of a map[moduleName][]moduleDependency .
2223 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002224 inStatic string
2225
2226 // This is a string representation of a map[moduleName][]moduleDependency .
2227 // It models the dependencies declared in an Android.bp file.
2228 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002229
2230 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2231 // The keys of allOrdered specify which modules we would like to check.
2232 // The values of allOrdered specify the expected result (of the transitive closure of all
2233 // dependencies) for each module to test
2234 allOrdered string
2235
2236 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2237 // The keys of outOrdered specify which modules we would like to check.
2238 // The values of outOrdered specify the expected result (of the ordered linker command line)
2239 // for each module to test.
2240 outOrdered string
2241}{
2242 // Simple tests
2243 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002244 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002245 outOrdered: "",
2246 },
2247 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002248 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002249 outOrdered: "a:",
2250 },
2251 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002252 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002253 outOrdered: "a:b; b:",
2254 },
2255 // Tests of reordering
2256 {
2257 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002258 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002259 outOrdered: "a:b,c,d; b:d; c:d; d:",
2260 },
2261 {
2262 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002263 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002264 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2265 },
2266 {
2267 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002268 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002269 outOrdered: "a:d,b,e,c; d:b; e:c",
2270 },
2271 {
2272 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002273 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002274 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2275 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2276 },
2277 {
2278 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002279 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 -07002280 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2281 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2282 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002283 // shared dependencies
2284 {
2285 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2286 // So, we don't actually have to check that a shared dependency of c will change the order
2287 // of a library that depends statically on b and on c. We only need to check that if c has
2288 // a shared dependency on b, that that shows up in allOrdered.
2289 inShared: "c:b",
2290 allOrdered: "c:b",
2291 outOrdered: "c:",
2292 },
2293 {
2294 // This test doesn't actually include any shared dependencies but it's a reminder of what
2295 // the second phase of the above test would look like
2296 inStatic: "a:b,c; c:b",
2297 allOrdered: "a:c,b; c:b",
2298 outOrdered: "a:c,b; c:b",
2299 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002300 // tiebreakers for when two modules specifying different orderings and there is no dependency
2301 // to dictate an order
2302 {
2303 // 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 -08002304 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002305 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2306 },
2307 {
2308 // 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 -08002309 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 -07002310 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2311 },
2312 // Tests involving duplicate dependencies
2313 {
2314 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002315 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002316 outOrdered: "a:c,b",
2317 },
2318 {
2319 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002320 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002321 outOrdered: "a:d,c,b",
2322 },
2323 // Tests to confirm the nonexistence of infinite loops.
2324 // These cases should never happen, so as long as the test terminates and the
2325 // result is deterministic then that should be fine.
2326 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002327 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002328 outOrdered: "a:a",
2329 },
2330 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002331 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002332 allOrdered: "a:b,c; b:c,a; c:a,b",
2333 outOrdered: "a:b; b:c; c:a",
2334 },
2335 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002336 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002337 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2338 outOrdered: "a:c,b; b:a,c; c:b,a",
2339 },
2340}
2341
2342// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2343func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2344 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2345 strippedText := strings.Replace(text, " ", "", -1)
2346 if len(strippedText) < 1 {
2347 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2348 }
2349 allDeps = make(map[android.Path][]android.Path, 0)
2350
2351 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2352 moduleTexts := strings.Split(strippedText, ";")
2353
2354 outputForModuleName := func(moduleName string) android.Path {
2355 return android.PathForTesting(moduleName)
2356 }
2357
2358 for _, moduleText := range moduleTexts {
2359 // convert from "a:b,c" to ["a", "b,c"]
2360 components := strings.Split(moduleText, ":")
2361 if len(components) != 2 {
2362 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2363 }
2364 moduleName := components[0]
2365 moduleOutput := outputForModuleName(moduleName)
2366 modulesInOrder = append(modulesInOrder, moduleOutput)
2367
2368 depString := components[1]
2369 // convert from "b,c" to ["b", "c"]
2370 depNames := strings.Split(depString, ",")
2371 if len(depString) < 1 {
2372 depNames = []string{}
2373 }
2374 var deps []android.Path
2375 for _, depName := range depNames {
2376 deps = append(deps, outputForModuleName(depName))
2377 }
2378 allDeps[moduleOutput] = deps
2379 }
2380 return modulesInOrder, allDeps
2381}
2382
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002383func TestLinkReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002384 for _, testCase := range staticLinkDepOrderTestCases {
2385 errs := []string{}
2386
2387 // parse testcase
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002388 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)
Jeff Gaston294356f2017-09-27 17:05:30 -07002389 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)
2390 if testCase.allOrdered == "" {
2391 // allow the test case to skip specifying allOrdered
2392 testCase.allOrdered = testCase.outOrdered
2393 }
2394 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002395 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)
Jeff Gaston294356f2017-09-27 17:05:30 -07002396
2397 // For each module whose post-reordered dependencies were specified, validate that
2398 // reordering the inputs produces the expected outputs.
2399 for _, moduleName := range expectedModuleNames {
2400 moduleDeps := givenTransitiveDeps[moduleName]
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002401 givenSharedDeps := givenAllSharedDeps[moduleName]
2402 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -07002403
2404 correctAllOrdered := expectedAllDeps[moduleName]
2405 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {
2406 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002407 "\nin static:%q"+
2408 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002409 "\nmodule: %v"+
2410 "\nexpected: %s"+
2411 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002412 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002413 }
2414
2415 correctOutputDeps := expectedTransitiveDeps[moduleName]
2416 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {
2417 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002418 "\nin static:%q"+
2419 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002420 "\nmodule: %v"+
2421 "\nexpected: %s"+
2422 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002423 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002424 }
2425 }
2426
2427 if len(errs) > 0 {
2428 sort.Strings(errs)
2429 for _, err := range errs {
2430 t.Error(err)
2431 }
2432 }
2433 }
2434}
Logan Chienf3511742017-10-31 18:04:35 +08002435
Jeff Gaston294356f2017-09-27 17:05:30 -07002436func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2437 for _, moduleName := range moduleNames {
2438 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
2439 output := module.outputFile.Path()
2440 paths = append(paths, output)
2441 }
2442 return paths
2443}
2444
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002445func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002446 ctx := testCc(t, `
2447 cc_library {
2448 name: "a",
2449 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002450 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002451 }
2452 cc_library {
2453 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002454 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002455 }
2456 cc_library {
2457 name: "c",
2458 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002459 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002460 }
2461 cc_library {
2462 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002463 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002464 }
2465
2466 `)
2467
Colin Cross7113d202019-11-20 16:39:12 -08002468 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002469 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002470 actual := moduleA.depsInLinkOrder
Jeff Gaston294356f2017-09-27 17:05:30 -07002471 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})
2472
2473 if !reflect.DeepEqual(actual, expected) {
2474 t.Errorf("staticDeps orderings were not propagated correctly"+
2475 "\nactual: %v"+
2476 "\nexpected: %v",
2477 actual,
2478 expected,
2479 )
2480 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002481}
Jeff Gaston294356f2017-09-27 17:05:30 -07002482
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002483func TestStaticLibDepReorderingWithShared(t *testing.T) {
2484 ctx := testCc(t, `
2485 cc_library {
2486 name: "a",
2487 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002488 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002489 }
2490 cc_library {
2491 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002492 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002493 }
2494 cc_library {
2495 name: "c",
2496 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002497 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002498 }
2499
2500 `)
2501
Colin Cross7113d202019-11-20 16:39:12 -08002502 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002503 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
2504 actual := moduleA.depsInLinkOrder
2505 expected := getOutputPaths(ctx, variant, []string{"c", "b"})
2506
2507 if !reflect.DeepEqual(actual, expected) {
2508 t.Errorf("staticDeps orderings did not account for shared libs"+
2509 "\nactual: %v"+
2510 "\nexpected: %v",
2511 actual,
2512 expected,
2513 )
2514 }
2515}
2516
Jooyung Hanb04a4992020-03-13 18:57:35 +09002517func checkEquals(t *testing.T, message string, expected, actual interface{}) {
2518 if !reflect.DeepEqual(actual, expected) {
2519 t.Errorf(message+
2520 "\nactual: %v"+
2521 "\nexpected: %v",
2522 actual,
2523 expected,
2524 )
2525 }
2526}
2527
Jooyung Han61b66e92020-03-21 14:21:46 +00002528func TestLlndkLibrary(t *testing.T) {
2529 ctx := testCc(t, `
2530 cc_library {
2531 name: "libllndk",
2532 stubs: { versions: ["1", "2"] },
2533 }
2534 llndk_library {
2535 name: "libllndk",
2536 }
2537 `)
2538 actual := ctx.ModuleVariantsForTests("libllndk.llndk")
2539 expected := []string{
2540 "android_vendor.VER_arm64_armv8-a_shared",
2541 "android_vendor.VER_arm64_armv8-a_shared_1",
2542 "android_vendor.VER_arm64_armv8-a_shared_2",
2543 "android_vendor.VER_arm_armv7-a-neon_shared",
2544 "android_vendor.VER_arm_armv7-a-neon_shared_1",
2545 "android_vendor.VER_arm_armv7-a-neon_shared_2",
2546 }
2547 checkEquals(t, "variants for llndk stubs", expected, actual)
2548
2549 params := ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
2550 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2551
2552 params = ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
2553 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2554}
2555
Jiyong Parka46a4d52017-12-14 19:54:34 +09002556func TestLlndkHeaders(t *testing.T) {
2557 ctx := testCc(t, `
2558 llndk_headers {
2559 name: "libllndk_headers",
2560 export_include_dirs: ["my_include"],
2561 }
2562 llndk_library {
2563 name: "libllndk",
2564 export_llndk_headers: ["libllndk_headers"],
2565 }
2566 cc_library {
2567 name: "libvendor",
2568 shared_libs: ["libllndk"],
2569 vendor: true,
2570 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002571 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002572 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002573 }
2574 `)
2575
2576 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002577 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002578 cflags := cc.Args["cFlags"]
2579 if !strings.Contains(cflags, "-Imy_include") {
2580 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2581 }
2582}
2583
Logan Chien43d34c32017-12-20 01:17:32 +08002584func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2585 actual := module.Properties.AndroidMkRuntimeLibs
2586 if !reflect.DeepEqual(actual, expected) {
2587 t.Errorf("incorrect runtime_libs for shared libs"+
2588 "\nactual: %v"+
2589 "\nexpected: %v",
2590 actual,
2591 expected,
2592 )
2593 }
2594}
2595
2596const runtimeLibAndroidBp = `
2597 cc_library {
2598 name: "libvendor_available1",
2599 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002600 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002601 nocrt : true,
2602 system_shared_libs : [],
2603 }
2604 cc_library {
2605 name: "libvendor_available2",
2606 vendor_available: true,
2607 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002608 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002609 nocrt : true,
2610 system_shared_libs : [],
2611 }
2612 cc_library {
2613 name: "libvendor_available3",
2614 vendor_available: true,
2615 runtime_libs: ["libvendor_available1"],
2616 target: {
2617 vendor: {
2618 exclude_runtime_libs: ["libvendor_available1"],
2619 }
2620 },
Yi Konge7fe9912019-06-02 00:53:50 -07002621 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002622 nocrt : true,
2623 system_shared_libs : [],
2624 }
2625 cc_library {
2626 name: "libcore",
2627 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002628 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002629 nocrt : true,
2630 system_shared_libs : [],
2631 }
2632 cc_library {
2633 name: "libvendor1",
2634 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002635 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002636 nocrt : true,
2637 system_shared_libs : [],
2638 }
2639 cc_library {
2640 name: "libvendor2",
2641 vendor: true,
2642 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002643 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002644 nocrt : true,
2645 system_shared_libs : [],
2646 }
2647`
2648
2649func TestRuntimeLibs(t *testing.T) {
2650 ctx := testCc(t, runtimeLibAndroidBp)
2651
2652 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002653 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002654
2655 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2656 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2657
2658 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
2659 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2660
2661 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2662 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002663 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002664
2665 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2666 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
2667
2668 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2669 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
2670}
2671
2672func TestExcludeRuntimeLibs(t *testing.T) {
2673 ctx := testCc(t, runtimeLibAndroidBp)
2674
Colin Cross7113d202019-11-20 16:39:12 -08002675 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002676 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2677 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2678
Colin Crossfb0c16e2019-11-20 17:12:35 -08002679 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002680 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2681 checkRuntimeLibs(t, nil, module)
2682}
2683
2684func TestRuntimeLibsNoVndk(t *testing.T) {
2685 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2686
2687 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2688
Colin Cross7113d202019-11-20 16:39:12 -08002689 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002690
2691 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2692 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2693
2694 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2695 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
2696}
2697
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002698func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002699 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002700 actual := module.Properties.AndroidMkStaticLibs
2701 if !reflect.DeepEqual(actual, expected) {
2702 t.Errorf("incorrect static_libs"+
2703 "\nactual: %v"+
2704 "\nexpected: %v",
2705 actual,
2706 expected,
2707 )
2708 }
2709}
2710
2711const staticLibAndroidBp = `
2712 cc_library {
2713 name: "lib1",
2714 }
2715 cc_library {
2716 name: "lib2",
2717 static_libs: ["lib1"],
2718 }
2719`
2720
2721func TestStaticLibDepExport(t *testing.T) {
2722 ctx := testCc(t, staticLibAndroidBp)
2723
2724 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002725 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002726 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002727 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002728
2729 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002730 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002731 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2732 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002733 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002734}
2735
Jiyong Parkd08b6972017-09-26 10:50:54 +09002736var compilerFlagsTestCases = []struct {
2737 in string
2738 out bool
2739}{
2740 {
2741 in: "a",
2742 out: false,
2743 },
2744 {
2745 in: "-a",
2746 out: true,
2747 },
2748 {
2749 in: "-Ipath/to/something",
2750 out: false,
2751 },
2752 {
2753 in: "-isystempath/to/something",
2754 out: false,
2755 },
2756 {
2757 in: "--coverage",
2758 out: false,
2759 },
2760 {
2761 in: "-include a/b",
2762 out: true,
2763 },
2764 {
2765 in: "-include a/b c/d",
2766 out: false,
2767 },
2768 {
2769 in: "-DMACRO",
2770 out: true,
2771 },
2772 {
2773 in: "-DMAC RO",
2774 out: false,
2775 },
2776 {
2777 in: "-a -b",
2778 out: false,
2779 },
2780 {
2781 in: "-DMACRO=definition",
2782 out: true,
2783 },
2784 {
2785 in: "-DMACRO=defi nition",
2786 out: true, // TODO(jiyong): this should be false
2787 },
2788 {
2789 in: "-DMACRO(x)=x + 1",
2790 out: true,
2791 },
2792 {
2793 in: "-DMACRO=\"defi nition\"",
2794 out: true,
2795 },
2796}
2797
2798type mockContext struct {
2799 BaseModuleContext
2800 result bool
2801}
2802
2803func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2804 // CheckBadCompilerFlags calls this function when the flag should be rejected
2805 ctx.result = false
2806}
2807
2808func TestCompilerFlags(t *testing.T) {
2809 for _, testCase := range compilerFlagsTestCases {
2810 ctx := &mockContext{result: true}
2811 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
2812 if ctx.result != testCase.out {
2813 t.Errorf("incorrect output:")
2814 t.Errorf(" input: %#v", testCase.in)
2815 t.Errorf(" expected: %#v", testCase.out)
2816 t.Errorf(" got: %#v", ctx.result)
2817 }
2818 }
Jeff Gaston294356f2017-09-27 17:05:30 -07002819}
Jiyong Park374510b2018-03-19 18:23:01 +09002820
2821func TestVendorPublicLibraries(t *testing.T) {
2822 ctx := testCc(t, `
2823 cc_library_headers {
2824 name: "libvendorpublic_headers",
2825 export_include_dirs: ["my_include"],
2826 }
2827 vendor_public_library {
2828 name: "libvendorpublic",
2829 symbol_file: "",
2830 export_public_headers: ["libvendorpublic_headers"],
2831 }
2832 cc_library {
2833 name: "libvendorpublic",
2834 srcs: ["foo.c"],
2835 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002836 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002837 nocrt: true,
2838 }
2839
2840 cc_library {
2841 name: "libsystem",
2842 shared_libs: ["libvendorpublic"],
2843 vendor: false,
2844 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002845 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002846 nocrt: true,
2847 }
2848 cc_library {
2849 name: "libvendor",
2850 shared_libs: ["libvendorpublic"],
2851 vendor: true,
2852 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002853 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002854 nocrt: true,
2855 }
2856 `)
2857
Colin Cross7113d202019-11-20 16:39:12 -08002858 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08002859 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09002860
2861 // test if header search paths are correctly added
2862 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08002863 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09002864 cflags := cc.Args["cFlags"]
2865 if !strings.Contains(cflags, "-Imy_include") {
2866 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
2867 }
2868
2869 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08002870 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002871 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002872 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09002873 if !strings.Contains(libflags, stubPaths[0].String()) {
2874 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
2875 }
2876
2877 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08002878 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002879 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002880 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09002881 if !strings.Contains(libflags, stubPaths[0].String()) {
2882 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
2883 }
2884
2885}
Jiyong Park37b25202018-07-11 10:49:27 +09002886
2887func TestRecovery(t *testing.T) {
2888 ctx := testCc(t, `
2889 cc_library_shared {
2890 name: "librecovery",
2891 recovery: true,
2892 }
2893 cc_library_shared {
2894 name: "librecovery32",
2895 recovery: true,
2896 compile_multilib:"32",
2897 }
Jiyong Park5baac542018-08-28 09:55:37 +09002898 cc_library_shared {
2899 name: "libHalInRecovery",
2900 recovery_available: true,
2901 vendor: true,
2902 }
Jiyong Park37b25202018-07-11 10:49:27 +09002903 `)
2904
2905 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08002906 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09002907 if len(variants) != 1 || !android.InList(arm64, variants) {
2908 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
2909 }
2910
2911 variants = ctx.ModuleVariantsForTests("librecovery32")
2912 if android.InList(arm64, variants) {
2913 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
2914 }
Jiyong Park5baac542018-08-28 09:55:37 +09002915
2916 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
2917 if !recoveryModule.Platform() {
2918 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
2919 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09002920}
Jiyong Park5baac542018-08-28 09:55:37 +09002921
Jiyong Park7ed9de32018-10-15 22:25:07 +09002922func TestVersionedStubs(t *testing.T) {
2923 ctx := testCc(t, `
2924 cc_library_shared {
2925 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002926 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002927 stubs: {
2928 symbol_file: "foo.map.txt",
2929 versions: ["1", "2", "3"],
2930 },
2931 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002932
Jiyong Park7ed9de32018-10-15 22:25:07 +09002933 cc_library_shared {
2934 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002935 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002936 shared_libs: ["libFoo#1"],
2937 }`)
2938
2939 variants := ctx.ModuleVariantsForTests("libFoo")
2940 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08002941 "android_arm64_armv8-a_shared",
2942 "android_arm64_armv8-a_shared_1",
2943 "android_arm64_armv8-a_shared_2",
2944 "android_arm64_armv8-a_shared_3",
2945 "android_arm_armv7-a-neon_shared",
2946 "android_arm_armv7-a-neon_shared_1",
2947 "android_arm_armv7-a-neon_shared_2",
2948 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09002949 }
2950 variantsMismatch := false
2951 if len(variants) != len(expectedVariants) {
2952 variantsMismatch = true
2953 } else {
2954 for _, v := range expectedVariants {
2955 if !inList(v, variants) {
2956 variantsMismatch = false
2957 }
2958 }
2959 }
2960 if variantsMismatch {
2961 t.Errorf("variants of libFoo expected:\n")
2962 for _, v := range expectedVariants {
2963 t.Errorf("%q\n", v)
2964 }
2965 t.Errorf(", but got:\n")
2966 for _, v := range variants {
2967 t.Errorf("%q\n", v)
2968 }
2969 }
2970
Colin Cross7113d202019-11-20 16:39:12 -08002971 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09002972 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002973 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09002974 if !strings.Contains(libFlags, libFoo1StubPath) {
2975 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
2976 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002977
Colin Cross7113d202019-11-20 16:39:12 -08002978 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09002979 cFlags := libBarCompileRule.Args["cFlags"]
2980 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
2981 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
2982 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
2983 }
Jiyong Park37b25202018-07-11 10:49:27 +09002984}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002985
Jooyung Hanb04a4992020-03-13 18:57:35 +09002986func TestVersioningMacro(t *testing.T) {
2987 for _, tc := range []struct{ moduleName, expected string }{
2988 {"libc", "__LIBC_API__"},
2989 {"libfoo", "__LIBFOO_API__"},
2990 {"libfoo@1", "__LIBFOO_1_API__"},
2991 {"libfoo-v1", "__LIBFOO_V1_API__"},
2992 {"libfoo.v1", "__LIBFOO_V1_API__"},
2993 } {
2994 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
2995 }
2996}
2997
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002998func TestStaticExecutable(t *testing.T) {
2999 ctx := testCc(t, `
3000 cc_binary {
3001 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003002 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003003 static_executable: true,
3004 }`)
3005
Colin Cross7113d202019-11-20 16:39:12 -08003006 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003007 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3008 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003009 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003010 for _, lib := range systemStaticLibs {
3011 if !strings.Contains(libFlags, lib) {
3012 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3013 }
3014 }
3015 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3016 for _, lib := range systemSharedLibs {
3017 if strings.Contains(libFlags, lib) {
3018 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3019 }
3020 }
3021}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003022
3023func TestStaticDepsOrderWithStubs(t *testing.T) {
3024 ctx := testCc(t, `
3025 cc_binary {
3026 name: "mybin",
3027 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003028 static_libs: ["libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003029 static_executable: true,
3030 stl: "none",
3031 }
3032
3033 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003034 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003035 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003036 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003037 stl: "none",
3038 }
3039
3040 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003041 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003042 srcs: ["foo.c"],
3043 stl: "none",
3044 stubs: {
3045 versions: ["1"],
3046 },
3047 }`)
3048
Colin Cross7113d202019-11-20 16:39:12 -08003049 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Module().(*Module)
Jiyong Parke4bb9862019-02-01 00:31:10 +09003050 actual := mybin.depsInLinkOrder
Colin Crossf9aabd72020-02-15 11:29:50 -08003051 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003052
3053 if !reflect.DeepEqual(actual, expected) {
3054 t.Errorf("staticDeps orderings were not propagated correctly"+
3055 "\nactual: %v"+
3056 "\nexpected: %v",
3057 actual,
3058 expected,
3059 )
3060 }
3061}
Jooyung Han38002912019-05-16 04:01:54 +09003062
Jooyung Hand48f3c32019-08-23 11:18:57 +09003063func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3064 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3065 cc_library {
3066 name: "libA",
3067 srcs: ["foo.c"],
3068 shared_libs: ["libB"],
3069 stl: "none",
3070 }
3071
3072 cc_library {
3073 name: "libB",
3074 srcs: ["foo.c"],
3075 enabled: false,
3076 stl: "none",
3077 }
3078 `)
3079}
3080
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003081// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3082// correctly.
3083func TestFuzzTarget(t *testing.T) {
3084 ctx := testCc(t, `
3085 cc_fuzz {
3086 name: "fuzz_smoke_test",
3087 srcs: ["foo.c"],
3088 }`)
3089
Paul Duffin075c4172019-12-19 19:06:13 +00003090 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003091 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3092}
3093
Jiyong Park29074592019-07-07 16:27:47 +09003094func TestAidl(t *testing.T) {
3095}
3096
Jooyung Han38002912019-05-16 04:01:54 +09003097func assertString(t *testing.T, got, expected string) {
3098 t.Helper()
3099 if got != expected {
3100 t.Errorf("expected %q got %q", expected, got)
3101 }
3102}
3103
3104func assertArrayString(t *testing.T, got, expected []string) {
3105 t.Helper()
3106 if len(got) != len(expected) {
3107 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3108 return
3109 }
3110 for i := range got {
3111 if got[i] != expected[i] {
3112 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3113 i, expected[i], expected, got[i], got)
3114 return
3115 }
3116 }
3117}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003118
Jooyung Han0302a842019-10-30 18:43:49 +09003119func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3120 t.Helper()
3121 assertArrayString(t, android.SortedStringKeys(m), expected)
3122}
3123
Colin Crosse1bb5d02019-09-24 14:55:04 -07003124func TestDefaults(t *testing.T) {
3125 ctx := testCc(t, `
3126 cc_defaults {
3127 name: "defaults",
3128 srcs: ["foo.c"],
3129 static: {
3130 srcs: ["bar.c"],
3131 },
3132 shared: {
3133 srcs: ["baz.c"],
3134 },
3135 }
3136
3137 cc_library_static {
3138 name: "libstatic",
3139 defaults: ["defaults"],
3140 }
3141
3142 cc_library_shared {
3143 name: "libshared",
3144 defaults: ["defaults"],
3145 }
3146
3147 cc_library {
3148 name: "libboth",
3149 defaults: ["defaults"],
3150 }
3151
3152 cc_binary {
3153 name: "binary",
3154 defaults: ["defaults"],
3155 }`)
3156
3157 pathsToBase := func(paths android.Paths) []string {
3158 var ret []string
3159 for _, p := range paths {
3160 ret = append(ret, p.Base())
3161 }
3162 return ret
3163 }
3164
Colin Cross7113d202019-11-20 16:39:12 -08003165 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003166 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3167 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3168 }
Colin Cross7113d202019-11-20 16:39:12 -08003169 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003170 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3171 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3172 }
Colin Cross7113d202019-11-20 16:39:12 -08003173 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003174 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3175 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3176 }
3177
Colin Cross7113d202019-11-20 16:39:12 -08003178 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003179 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3180 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3181 }
Colin Cross7113d202019-11-20 16:39:12 -08003182 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003183 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3184 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3185 }
3186}
Colin Crosseabaedd2020-02-06 17:01:55 -08003187
3188func TestProductVariableDefaults(t *testing.T) {
3189 bp := `
3190 cc_defaults {
3191 name: "libfoo_defaults",
3192 srcs: ["foo.c"],
3193 cppflags: ["-DFOO"],
3194 product_variables: {
3195 debuggable: {
3196 cppflags: ["-DBAR"],
3197 },
3198 },
3199 }
3200
3201 cc_library {
3202 name: "libfoo",
3203 defaults: ["libfoo_defaults"],
3204 }
3205 `
3206
3207 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3208 config.TestProductVariables.Debuggable = BoolPtr(true)
3209
3210 ctx := CreateTestContext()
3211 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
3212 ctx.BottomUp("variable", android.VariableMutator).Parallel()
3213 })
3214 ctx.Register(config)
3215
3216 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3217 android.FailIfErrored(t, errs)
3218 _, errs = ctx.PrepareBuildActions(config)
3219 android.FailIfErrored(t, errs)
3220
3221 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
3222 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
3223 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
3224 }
3225}