blob: 77b5c527b69d13b260c347a8b7fe7dac275223ab [file] [log] [blame]
Colin Crossd00350c2017-11-17 10:55:38 -08001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross74d1ec02015-04-28 13:30:13 -070015package cc
16
17import (
Jeff Gaston294356f2017-09-27 17:05:30 -070018 "fmt"
Jiyong Park6a43f042017-10-12 23:05:00 +090019 "io/ioutil"
20 "os"
Inseob Kim1f086e22019-05-09 13:29:15 +090021 "path/filepath"
Colin Cross74d1ec02015-04-28 13:30:13 -070022 "reflect"
Jeff Gaston294356f2017-09-27 17:05:30 -070023 "sort"
24 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070025 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070026
27 "android/soong/android"
Colin Cross74d1ec02015-04-28 13:30:13 -070028)
29
Jiyong Park6a43f042017-10-12 23:05:00 +090030var buildDir string
31
32func setUp() {
33 var err error
34 buildDir, err = ioutil.TempDir("", "soong_cc_test")
35 if err != nil {
36 panic(err)
37 }
38}
39
40func tearDown() {
41 os.RemoveAll(buildDir)
42}
43
44func TestMain(m *testing.M) {
45 run := func() int {
46 setUp()
47 defer tearDown()
48
49 return m.Run()
50 }
51
52 os.Exit(run())
53}
54
Colin Cross98be1bb2019-12-13 20:41:13 -080055func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070056 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080057 ctx := CreateTestContext()
58 ctx.Register(config)
Logan Chienf3511742017-10-31 18:04:35 +080059
Jeff Gastond3e141d2017-08-08 17:46:01 -070060 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +080061 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +090062 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +080063 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +090064
65 return ctx
66}
67
Logan Chienf3511742017-10-31 18:04:35 +080068func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080069 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080070 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070071 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
72 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +080073
Colin Cross98be1bb2019-12-13 20:41:13 -080074 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080075}
76
77func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080078 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080079 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070080 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +080081
Colin Cross98be1bb2019-12-13 20:41:13 -080082 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080083}
84
Justin Yun5f7f7e82019-11-18 19:52:14 +090085func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +080086 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +080087
Colin Cross98be1bb2019-12-13 20:41:13 -080088 ctx := CreateTestContext()
89 ctx.Register(config)
Logan Chienf3511742017-10-31 18:04:35 +080090
91 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
92 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +080093 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +080094 return
95 }
96
97 _, errs = ctx.PrepareBuildActions(config)
98 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +080099 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800100 return
101 }
102
103 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
104}
105
Justin Yun5f7f7e82019-11-18 19:52:14 +0900106func testCcError(t *testing.T, pattern string, bp string) {
107 config := TestConfig(buildDir, android.Android, nil, bp, nil)
108 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
109 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
110 testCcErrorWithConfig(t, pattern, config)
111 return
112}
113
114func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
115 config := TestConfig(buildDir, android.Android, nil, bp, nil)
116 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
117 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
118 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
119 testCcErrorWithConfig(t, pattern, config)
120 return
121}
122
Logan Chienf3511742017-10-31 18:04:35 +0800123const (
Colin Cross7113d202019-11-20 16:39:12 -0800124 coreVariant = "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800125 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun5f7f7e82019-11-18 19:52:14 +0900126 productVariant = "android_product.VER_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800127 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800128)
129
Doug Hornc32c6b02019-01-17 14:44:05 -0800130func TestFuchsiaDeps(t *testing.T) {
131 t.Helper()
132
133 bp := `
134 cc_library {
135 name: "libTest",
136 srcs: ["foo.c"],
137 target: {
138 fuchsia: {
139 srcs: ["bar.c"],
140 },
141 },
142 }`
143
Colin Cross98be1bb2019-12-13 20:41:13 -0800144 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
145 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800146
147 rt := false
148 fb := false
149
150 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
151 implicits := ld.Implicits
152 for _, lib := range implicits {
153 if strings.Contains(lib.Rel(), "libcompiler_rt") {
154 rt = true
155 }
156
157 if strings.Contains(lib.Rel(), "libbioniccompat") {
158 fb = true
159 }
160 }
161
162 if !rt || !fb {
163 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
164 }
165}
166
167func TestFuchsiaTargetDecl(t *testing.T) {
168 t.Helper()
169
170 bp := `
171 cc_library {
172 name: "libTest",
173 srcs: ["foo.c"],
174 target: {
175 fuchsia: {
176 srcs: ["bar.c"],
177 },
178 },
179 }`
180
Colin Cross98be1bb2019-12-13 20:41:13 -0800181 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
182 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800183 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
184 var objs []string
185 for _, o := range ld.Inputs {
186 objs = append(objs, o.Base())
187 }
188 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
189 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
190 }
191}
192
Jiyong Park6a43f042017-10-12 23:05:00 +0900193func TestVendorSrc(t *testing.T) {
194 ctx := testCc(t, `
195 cc_library {
196 name: "libTest",
197 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700198 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800199 nocrt: true,
200 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900201 vendor_available: true,
202 target: {
203 vendor: {
204 srcs: ["bar.c"],
205 },
206 },
207 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900208 `)
209
Logan Chienf3511742017-10-31 18:04:35 +0800210 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900211 var objs []string
212 for _, o := range ld.Inputs {
213 objs = append(objs, o.Base())
214 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800215 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900216 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
217 }
218}
219
Logan Chienf3511742017-10-31 18:04:35 +0800220func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900221 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800222
Logan Chiend3c59a22018-03-29 14:08:15 +0800223 t.Helper()
224
Justin Yun0ecf0b22020-02-28 15:07:59 +0900225 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Ivan Lozano52767be2019-10-18 14:49:46 -0700226 if !mod.HasVendorVariant() {
Justin Yun0ecf0b22020-02-28 15:07:59 +0900227 t.Errorf("%q must have variant %q", name, variant)
Logan Chienf3511742017-10-31 18:04:35 +0800228 }
229
230 // Check library properties.
231 lib, ok := mod.compiler.(*libraryDecorator)
232 if !ok {
233 t.Errorf("%q must have libraryDecorator", name)
234 } else if lib.baseInstaller.subDir != subDir {
235 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
236 lib.baseInstaller.subDir)
237 }
238
239 // Check VNDK properties.
240 if mod.vndkdep == nil {
241 t.Fatalf("%q must have `vndkdep`", name)
242 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700243 if !mod.IsVndk() {
244 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800245 }
246 if mod.isVndkSp() != isVndkSp {
247 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
248 }
249
250 // Check VNDK extension properties.
251 isVndkExt := extends != ""
252 if mod.isVndkExt() != isVndkExt {
253 t.Errorf("%q isVndkExt() must equal to %t", name, isVndkExt)
254 }
255
256 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
257 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
258 }
259}
260
Inseob Kim7f283f42020-06-01 21:53:49 +0900261func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Jooyung Han39edb6c2019-11-06 16:53:07 +0900262 mod, ok := ctx.ModuleForTests(moduleName, variant).Module().(android.OutputFileProducer)
263 if !ok {
264 t.Errorf("%q must have output\n", moduleName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900265 return
266 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900267 outputFiles, err := mod.OutputFiles("")
268 if err != nil || len(outputFiles) != 1 {
269 t.Errorf("%q must have single output\n", moduleName)
270 return
271 }
272 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900273
Inseob Kim7f283f42020-06-01 21:53:49 +0900274 out := singleton.Output(snapshotPath)
Jooyung Han39edb6c2019-11-06 16:53:07 +0900275 if out.Input.String() != outputFiles[0].String() {
Inseob Kim8471cda2019-11-15 09:59:12 +0900276 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
Inseob Kim1f086e22019-05-09 13:29:15 +0900277 }
278}
279
Jooyung Han2216fb12019-11-06 16:46:15 +0900280func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
281 t.Helper()
282 assertString(t, params.Rule.String(), android.WriteFile.String())
283 actual := strings.FieldsFunc(strings.ReplaceAll(params.Args["content"], "\\n", "\n"), func(r rune) bool { return r == '\n' })
284 assertArrayString(t, actual, expected)
285}
286
Jooyung Han097087b2019-10-22 19:32:18 +0900287func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
288 t.Helper()
289 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900290 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
291}
292
293func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
294 t.Helper()
295 vndkLibraries := ctx.ModuleForTests(module, "")
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +0900296
297 var output string
298 if module != "vndkcorevariant.libraries.txt" {
299 output = insertVndkVersion(module, "VER")
300 } else {
301 output = module
302 }
303
Jooyung Han2216fb12019-11-06 16:46:15 +0900304 checkWriteFileOutput(t, vndkLibraries.Output(output), expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900305}
306
Logan Chienf3511742017-10-31 18:04:35 +0800307func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800308 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800309 cc_library {
310 name: "libvndk",
311 vendor_available: true,
312 vndk: {
313 enabled: true,
314 },
315 nocrt: true,
316 }
317
318 cc_library {
319 name: "libvndk_private",
320 vendor_available: false,
321 vndk: {
322 enabled: true,
323 },
324 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900325 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800326 }
327
328 cc_library {
329 name: "libvndk_sp",
330 vendor_available: true,
331 vndk: {
332 enabled: true,
333 support_system_process: true,
334 },
335 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900336 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800337 }
338
339 cc_library {
340 name: "libvndk_sp_private",
341 vendor_available: false,
342 vndk: {
343 enabled: true,
344 support_system_process: true,
345 },
346 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900347 target: {
348 vendor: {
349 suffix: "-x",
350 },
351 },
Logan Chienf3511742017-10-31 18:04:35 +0800352 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900353 vndk_libraries_txt {
354 name: "llndk.libraries.txt",
355 }
356 vndk_libraries_txt {
357 name: "vndkcore.libraries.txt",
358 }
359 vndk_libraries_txt {
360 name: "vndksp.libraries.txt",
361 }
362 vndk_libraries_txt {
363 name: "vndkprivate.libraries.txt",
364 }
365 vndk_libraries_txt {
366 name: "vndkcorevariant.libraries.txt",
367 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800368 `
369
370 config := TestConfig(buildDir, android.Android, nil, bp, nil)
371 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
372 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
373
374 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800375
Justin Yun0ecf0b22020-02-28 15:07:59 +0900376 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "", vendorVariant)
377 checkVndkModule(t, ctx, "libvndk_private", "vndk-VER", false, "", vendorVariant)
378 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "", vendorVariant)
379 checkVndkModule(t, ctx, "libvndk_sp_private", "vndk-sp-VER", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900380
381 // Check VNDK snapshot output.
382
383 snapshotDir := "vndk-snapshot"
384 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
385
386 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
387 "arm64", "armv8-a"))
388 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
389 "arm", "armv7-a-neon"))
390
391 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
392 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
393 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
394 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
395
Colin Crossfb0c16e2019-11-20 17:12:35 -0800396 variant := "android_vendor.VER_arm64_armv8-a_shared"
397 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900398
Inseob Kim7f283f42020-06-01 21:53:49 +0900399 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
400
401 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
402 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
403 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
404 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900405
Jooyung Han39edb6c2019-11-06 16:53:07 +0900406 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900407 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
408 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
409 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
410 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900411
Jooyung Han097087b2019-10-22 19:32:18 +0900412 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
413 "LLNDK: libc.so",
414 "LLNDK: libdl.so",
415 "LLNDK: libft2.so",
416 "LLNDK: libm.so",
417 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900418 "VNDK-SP: libvndk_sp-x.so",
419 "VNDK-SP: libvndk_sp_private-x.so",
420 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900421 "VNDK-core: libvndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900422 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900423 "VNDK-private: libvndk-private.so",
424 "VNDK-private: libvndk_sp_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900425 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900426 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
427 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so"})
428 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so"})
429 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so"})
430 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
431}
432
Yo Chiangbba545e2020-06-09 16:15:37 +0800433func TestVndkWithHostSupported(t *testing.T) {
434 ctx := testCc(t, `
435 cc_library {
436 name: "libvndk_host_supported",
437 vendor_available: true,
438 vndk: {
439 enabled: true,
440 },
441 host_supported: true,
442 }
443
444 cc_library {
445 name: "libvndk_host_supported_but_disabled_on_device",
446 vendor_available: true,
447 vndk: {
448 enabled: true,
449 },
450 host_supported: true,
451 enabled: false,
452 target: {
453 host: {
454 enabled: true,
455 }
456 }
457 }
458
459 vndk_libraries_txt {
460 name: "vndkcore.libraries.txt",
461 }
462 `)
463
464 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
465}
466
Jooyung Han2216fb12019-11-06 16:46:15 +0900467func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800468 bp := `
Jooyung Han2216fb12019-11-06 16:46:15 +0900469 vndk_libraries_txt {
470 name: "llndk.libraries.txt",
Colin Cross98be1bb2019-12-13 20:41:13 -0800471 }`
472 config := TestConfig(buildDir, android.Android, nil, bp, nil)
473 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
474 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
475 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900476
477 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900478 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900479 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900480}
481
482func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800483 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900484 cc_library {
485 name: "libvndk",
486 vendor_available: true,
487 vndk: {
488 enabled: true,
489 },
490 nocrt: true,
491 }
492
493 cc_library {
494 name: "libvndk_sp",
495 vendor_available: true,
496 vndk: {
497 enabled: true,
498 support_system_process: true,
499 },
500 nocrt: true,
501 }
502
503 cc_library {
504 name: "libvndk2",
505 vendor_available: false,
506 vndk: {
507 enabled: true,
508 },
509 nocrt: true,
510 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900511
512 vndk_libraries_txt {
513 name: "vndkcorevariant.libraries.txt",
514 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800515 `
516
517 config := TestConfig(buildDir, android.Android, nil, bp, nil)
518 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
519 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
520 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
521
522 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
523
524 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900525
Jooyung Han2216fb12019-11-06 16:46:15 +0900526 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900527}
528
Chris Parsons79d66a52020-06-05 17:26:16 -0400529func TestDataLibs(t *testing.T) {
530 bp := `
531 cc_test_library {
532 name: "test_lib",
533 srcs: ["test_lib.cpp"],
534 gtest: false,
535 }
536
537 cc_test {
538 name: "main_test",
539 data_libs: ["test_lib"],
540 gtest: false,
541 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400542 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400543
544 config := TestConfig(buildDir, android.Android, nil, bp, nil)
545 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
546 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
547 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
548
549 ctx := testCcWithConfig(t, config)
550 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
551 testBinary := module.(*Module).linker.(*testBinary)
552 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
553 if err != nil {
554 t.Errorf("Expected cc_test to produce output files, error: %s", err)
555 return
556 }
557 if len(outputFiles) != 1 {
558 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
559 return
560 }
561 if len(testBinary.dataPaths()) != 1 {
562 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
563 return
564 }
565
566 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400567 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400568
569 if !strings.HasSuffix(outputPath, "/main_test") {
570 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
571 return
572 }
573 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
574 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
575 return
576 }
577}
578
Chris Parsons216e10a2020-07-09 17:12:52 -0400579func TestDataLibsRelativeInstallPath(t *testing.T) {
580 bp := `
581 cc_test_library {
582 name: "test_lib",
583 srcs: ["test_lib.cpp"],
584 relative_install_path: "foo/bar/baz",
585 gtest: false,
586 }
587
588 cc_test {
589 name: "main_test",
590 data_libs: ["test_lib"],
591 gtest: false,
592 }
593 `
594
595 config := TestConfig(buildDir, android.Android, nil, bp, nil)
596 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
597 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
598 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
599
600 ctx := testCcWithConfig(t, config)
601 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
602 testBinary := module.(*Module).linker.(*testBinary)
603 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
604 if err != nil {
605 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
606 }
607 if len(outputFiles) != 1 {
608 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
609 }
610 if len(testBinary.dataPaths()) != 1 {
611 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
612 }
613
614 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400615
616 if !strings.HasSuffix(outputPath, "/main_test") {
617 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
618 }
619 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
620 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
621 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400622 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400623 }
624}
625
Jooyung Han0302a842019-10-30 18:43:49 +0900626func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900627 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900628 cc_library {
629 name: "libvndk",
630 vendor_available: true,
631 vndk: {
632 enabled: true,
633 },
634 nocrt: true,
635 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900636 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900637
638 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
639 "LLNDK: libc.so",
640 "LLNDK: libdl.so",
641 "LLNDK: libft2.so",
642 "LLNDK: libm.so",
643 "VNDK-SP: libc++.so",
644 "VNDK-core: libvndk.so",
645 "VNDK-private: libft2.so",
646 })
Logan Chienf3511742017-10-31 18:04:35 +0800647}
648
Logan Chiend3c59a22018-03-29 14:08:15 +0800649func TestVndkDepError(t *testing.T) {
650 // Check whether an error is emitted when a VNDK lib depends on a system lib.
651 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
652 cc_library {
653 name: "libvndk",
654 vendor_available: true,
655 vndk: {
656 enabled: true,
657 },
658 shared_libs: ["libfwk"], // Cause error
659 nocrt: true,
660 }
661
662 cc_library {
663 name: "libfwk",
664 nocrt: true,
665 }
666 `)
667
668 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
669 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
670 cc_library {
671 name: "libvndk",
672 vendor_available: true,
673 vndk: {
674 enabled: true,
675 },
676 shared_libs: ["libvendor"], // Cause error
677 nocrt: true,
678 }
679
680 cc_library {
681 name: "libvendor",
682 vendor: true,
683 nocrt: true,
684 }
685 `)
686
687 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
688 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
689 cc_library {
690 name: "libvndk_sp",
691 vendor_available: true,
692 vndk: {
693 enabled: true,
694 support_system_process: true,
695 },
696 shared_libs: ["libfwk"], // Cause error
697 nocrt: true,
698 }
699
700 cc_library {
701 name: "libfwk",
702 nocrt: true,
703 }
704 `)
705
706 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
707 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
708 cc_library {
709 name: "libvndk_sp",
710 vendor_available: true,
711 vndk: {
712 enabled: true,
713 support_system_process: true,
714 },
715 shared_libs: ["libvendor"], // Cause error
716 nocrt: true,
717 }
718
719 cc_library {
720 name: "libvendor",
721 vendor: true,
722 nocrt: true,
723 }
724 `)
725
726 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
727 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
728 cc_library {
729 name: "libvndk_sp",
730 vendor_available: true,
731 vndk: {
732 enabled: true,
733 support_system_process: true,
734 },
735 shared_libs: ["libvndk"], // Cause error
736 nocrt: true,
737 }
738
739 cc_library {
740 name: "libvndk",
741 vendor_available: true,
742 vndk: {
743 enabled: true,
744 },
745 nocrt: true,
746 }
747 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900748
749 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
750 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
751 cc_library {
752 name: "libvndk",
753 vendor_available: true,
754 vndk: {
755 enabled: true,
756 },
757 shared_libs: ["libnonvndk"],
758 nocrt: true,
759 }
760
761 cc_library {
762 name: "libnonvndk",
763 vendor_available: true,
764 nocrt: true,
765 }
766 `)
767
768 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
769 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
770 cc_library {
771 name: "libvndkprivate",
772 vendor_available: false,
773 vndk: {
774 enabled: true,
775 },
776 shared_libs: ["libnonvndk"],
777 nocrt: true,
778 }
779
780 cc_library {
781 name: "libnonvndk",
782 vendor_available: true,
783 nocrt: true,
784 }
785 `)
786
787 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
788 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
789 cc_library {
790 name: "libvndksp",
791 vendor_available: true,
792 vndk: {
793 enabled: true,
794 support_system_process: true,
795 },
796 shared_libs: ["libnonvndk"],
797 nocrt: true,
798 }
799
800 cc_library {
801 name: "libnonvndk",
802 vendor_available: true,
803 nocrt: true,
804 }
805 `)
806
807 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
808 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
809 cc_library {
810 name: "libvndkspprivate",
811 vendor_available: false,
812 vndk: {
813 enabled: true,
814 support_system_process: true,
815 },
816 shared_libs: ["libnonvndk"],
817 nocrt: true,
818 }
819
820 cc_library {
821 name: "libnonvndk",
822 vendor_available: true,
823 nocrt: true,
824 }
825 `)
826}
827
828func TestDoubleLoadbleDep(t *testing.T) {
829 // okay to link : LLNDK -> double_loadable VNDK
830 testCc(t, `
831 cc_library {
832 name: "libllndk",
833 shared_libs: ["libdoubleloadable"],
834 }
835
836 llndk_library {
837 name: "libllndk",
838 symbol_file: "",
839 }
840
841 cc_library {
842 name: "libdoubleloadable",
843 vendor_available: true,
844 vndk: {
845 enabled: true,
846 },
847 double_loadable: true,
848 }
849 `)
850 // okay to link : LLNDK -> VNDK-SP
851 testCc(t, `
852 cc_library {
853 name: "libllndk",
854 shared_libs: ["libvndksp"],
855 }
856
857 llndk_library {
858 name: "libllndk",
859 symbol_file: "",
860 }
861
862 cc_library {
863 name: "libvndksp",
864 vendor_available: true,
865 vndk: {
866 enabled: true,
867 support_system_process: true,
868 },
869 }
870 `)
871 // okay to link : double_loadable -> double_loadable
872 testCc(t, `
873 cc_library {
874 name: "libdoubleloadable1",
875 shared_libs: ["libdoubleloadable2"],
876 vendor_available: true,
877 double_loadable: true,
878 }
879
880 cc_library {
881 name: "libdoubleloadable2",
882 vendor_available: true,
883 double_loadable: true,
884 }
885 `)
886 // okay to link : double_loadable VNDK -> double_loadable VNDK private
887 testCc(t, `
888 cc_library {
889 name: "libdoubleloadable",
890 vendor_available: true,
891 vndk: {
892 enabled: true,
893 },
894 double_loadable: true,
895 shared_libs: ["libnondoubleloadable"],
896 }
897
898 cc_library {
899 name: "libnondoubleloadable",
900 vendor_available: false,
901 vndk: {
902 enabled: true,
903 },
904 double_loadable: true,
905 }
906 `)
907 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
908 testCc(t, `
909 cc_library {
910 name: "libllndk",
911 shared_libs: ["libcoreonly"],
912 }
913
914 llndk_library {
915 name: "libllndk",
916 symbol_file: "",
917 }
918
919 cc_library {
920 name: "libcoreonly",
921 shared_libs: ["libvendoravailable"],
922 }
923
924 // indirect dependency of LLNDK
925 cc_library {
926 name: "libvendoravailable",
927 vendor_available: true,
928 double_loadable: true,
929 }
930 `)
931}
932
Inseob Kim8471cda2019-11-15 09:59:12 +0900933func TestVendorSnapshot(t *testing.T) {
934 bp := `
935 cc_library {
936 name: "libvndk",
937 vendor_available: true,
938 vndk: {
939 enabled: true,
940 },
941 nocrt: true,
942 }
943
944 cc_library {
945 name: "libvendor",
946 vendor: true,
947 nocrt: true,
948 }
949
950 cc_library {
951 name: "libvendor_available",
952 vendor_available: true,
953 nocrt: true,
954 }
955
956 cc_library_headers {
957 name: "libvendor_headers",
958 vendor_available: true,
959 nocrt: true,
960 }
961
962 cc_binary {
963 name: "vendor_bin",
964 vendor: true,
965 nocrt: true,
966 }
967
968 cc_binary {
969 name: "vendor_available_bin",
970 vendor_available: true,
971 nocrt: true,
972 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900973
974 toolchain_library {
975 name: "libb",
976 vendor_available: true,
977 src: "libb.a",
978 }
Inseob Kim1042d292020-06-01 23:23:05 +0900979
980 cc_object {
981 name: "obj",
982 vendor_available: true,
983 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900984`
985 config := TestConfig(buildDir, android.Android, nil, bp, nil)
986 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
987 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
988 ctx := testCcWithConfig(t, config)
989
990 // Check Vendor snapshot output.
991
992 snapshotDir := "vendor-snapshot"
993 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +0900994 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
995
996 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +0900997
998 for _, arch := range [][]string{
999 []string{"arm64", "armv8-a"},
1000 []string{"arm", "armv7-a-neon"},
1001 } {
1002 archType := arch[0]
1003 archVariant := arch[1]
1004 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1005
1006 // For shared libraries, only non-VNDK vendor_available modules are captured
1007 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1008 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +09001009 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1010 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
1011 jsonFiles = append(jsonFiles,
1012 filepath.Join(sharedDir, "libvendor.so.json"),
1013 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001014
1015 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
Inseob Kimc42f2f22020-07-29 20:32:10 +09001016 // Also cfi variants are captured, except for prebuilts like toolchain_library
Inseob Kim8471cda2019-11-15 09:59:12 +09001017 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001018 staticCfiVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static_cfi", archType, archVariant)
Inseob Kim8471cda2019-11-15 09:59:12 +09001019 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +09001020 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1021 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001022 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001023 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001024 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001025 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001026 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001027 jsonFiles = append(jsonFiles,
1028 filepath.Join(staticDir, "libb.a.json"),
1029 filepath.Join(staticDir, "libvndk.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001030 filepath.Join(staticDir, "libvndk.cfi.a.json"),
Inseob Kim7f283f42020-06-01 21:53:49 +09001031 filepath.Join(staticDir, "libvendor.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001032 filepath.Join(staticDir, "libvendor.cfi.a.json"),
1033 filepath.Join(staticDir, "libvendor_available.a.json"),
1034 filepath.Join(staticDir, "libvendor_available.cfi.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001035
Inseob Kim7f283f42020-06-01 21:53:49 +09001036 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +09001037 if archType == "arm64" {
1038 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1039 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +09001040 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
1041 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
1042 jsonFiles = append(jsonFiles,
1043 filepath.Join(binaryDir, "vendor_bin.json"),
1044 filepath.Join(binaryDir, "vendor_available_bin.json"))
1045 }
1046
1047 // For header libraries, all vendor:true and vendor_available modules are captured.
1048 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1049 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +09001050
1051 // For object modules, all vendor:true and vendor_available modules are captured.
1052 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1053 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1054 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1055 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001056 }
1057
1058 for _, jsonFile := range jsonFiles {
1059 // verify all json files exist
1060 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1061 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001062 }
1063 }
1064}
1065
Inseob Kimc42f2f22020-07-29 20:32:10 +09001066func TestVendorSnapshotSanitizer(t *testing.T) {
1067 bp := `
1068 vendor_snapshot_static {
1069 name: "libsnapshot",
1070 vendor: true,
1071 target_arch: "arm64",
1072 version: "BOARD",
1073 arch: {
1074 arm64: {
1075 src: "libsnapshot.a",
1076 cfi: {
1077 src: "libsnapshot.cfi.a",
1078 }
1079 },
1080 },
1081 }
1082`
1083 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1084 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1085 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1086 ctx := testCcWithConfig(t, config)
1087
1088 // Check non-cfi and cfi variant.
1089 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1090 staticCfiVariant := "android_vendor.BOARD_arm64_armv8-a_static_cfi"
1091
1092 staticModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticVariant).Module().(*Module)
1093 assertString(t, staticModule.outputFile.Path().Base(), "libsnapshot.a")
1094
1095 staticCfiModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticCfiVariant).Module().(*Module)
1096 assertString(t, staticCfiModule.outputFile.Path().Base(), "libsnapshot.cfi.a")
1097}
1098
Jooyung Hana70f0672019-01-18 15:20:43 +09001099func TestDoubleLoadableDepError(t *testing.T) {
1100 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1101 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1102 cc_library {
1103 name: "libllndk",
1104 shared_libs: ["libnondoubleloadable"],
1105 }
1106
1107 llndk_library {
1108 name: "libllndk",
1109 symbol_file: "",
1110 }
1111
1112 cc_library {
1113 name: "libnondoubleloadable",
1114 vendor_available: true,
1115 vndk: {
1116 enabled: true,
1117 },
1118 }
1119 `)
1120
1121 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1122 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1123 cc_library {
1124 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001125 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001126 shared_libs: ["libnondoubleloadable"],
1127 }
1128
1129 llndk_library {
1130 name: "libllndk",
1131 symbol_file: "",
1132 }
1133
1134 cc_library {
1135 name: "libnondoubleloadable",
1136 vendor_available: true,
1137 }
1138 `)
1139
1140 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
1141 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1142 cc_library {
1143 name: "libdoubleloadable",
1144 vendor_available: true,
1145 double_loadable: true,
1146 shared_libs: ["libnondoubleloadable"],
1147 }
1148
1149 cc_library {
1150 name: "libnondoubleloadable",
1151 vendor_available: true,
1152 }
1153 `)
1154
1155 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
1156 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1157 cc_library {
1158 name: "libdoubleloadable",
1159 vendor_available: true,
1160 double_loadable: true,
1161 shared_libs: ["libnondoubleloadable"],
1162 }
1163
1164 cc_library {
1165 name: "libnondoubleloadable",
1166 vendor_available: true,
1167 vndk: {
1168 enabled: true,
1169 },
1170 }
1171 `)
1172
1173 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
1174 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1175 cc_library {
1176 name: "libdoubleloadable",
1177 vendor_available: true,
1178 vndk: {
1179 enabled: true,
1180 },
1181 double_loadable: true,
1182 shared_libs: ["libnondoubleloadable"],
1183 }
1184
1185 cc_library {
1186 name: "libnondoubleloadable",
1187 vendor_available: false,
1188 vndk: {
1189 enabled: true,
1190 },
1191 }
1192 `)
1193
1194 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1195 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1196 cc_library {
1197 name: "libllndk",
1198 shared_libs: ["libcoreonly"],
1199 }
1200
1201 llndk_library {
1202 name: "libllndk",
1203 symbol_file: "",
1204 }
1205
1206 cc_library {
1207 name: "libcoreonly",
1208 shared_libs: ["libvendoravailable"],
1209 }
1210
1211 // indirect dependency of LLNDK
1212 cc_library {
1213 name: "libvendoravailable",
1214 vendor_available: true,
1215 }
1216 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001217}
1218
Logan Chienf3511742017-10-31 18:04:35 +08001219func TestVndkExt(t *testing.T) {
1220 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001221 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001222 cc_library {
1223 name: "libvndk",
1224 vendor_available: true,
1225 vndk: {
1226 enabled: true,
1227 },
1228 nocrt: true,
1229 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001230 cc_library {
1231 name: "libvndk2",
1232 vendor_available: true,
1233 vndk: {
1234 enabled: true,
1235 },
1236 target: {
1237 vendor: {
1238 suffix: "-suffix",
1239 },
1240 },
1241 nocrt: true,
1242 }
Logan Chienf3511742017-10-31 18:04:35 +08001243
1244 cc_library {
1245 name: "libvndk_ext",
1246 vendor: true,
1247 vndk: {
1248 enabled: true,
1249 extends: "libvndk",
1250 },
1251 nocrt: true,
1252 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001253
1254 cc_library {
1255 name: "libvndk2_ext",
1256 vendor: true,
1257 vndk: {
1258 enabled: true,
1259 extends: "libvndk2",
1260 },
1261 nocrt: true,
1262 }
Logan Chienf3511742017-10-31 18:04:35 +08001263
Justin Yun0ecf0b22020-02-28 15:07:59 +09001264 cc_library {
1265 name: "libvndk_ext_product",
1266 product_specific: true,
1267 vndk: {
1268 enabled: true,
1269 extends: "libvndk",
1270 },
1271 nocrt: true,
1272 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001273
Justin Yun0ecf0b22020-02-28 15:07:59 +09001274 cc_library {
1275 name: "libvndk2_ext_product",
1276 product_specific: true,
1277 vndk: {
1278 enabled: true,
1279 extends: "libvndk2",
1280 },
1281 nocrt: true,
1282 }
1283 `
1284 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1285 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1286 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1287 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1288
1289 ctx := testCcWithConfig(t, config)
1290
1291 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1292 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1293
1294 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1295 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1296
1297 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1298 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001299}
1300
Logan Chiend3c59a22018-03-29 14:08:15 +08001301func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001302 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1303 ctx := testCcNoVndk(t, `
1304 cc_library {
1305 name: "libvndk",
1306 vendor_available: true,
1307 vndk: {
1308 enabled: true,
1309 },
1310 nocrt: true,
1311 }
1312
1313 cc_library {
1314 name: "libvndk_ext",
1315 vendor: true,
1316 vndk: {
1317 enabled: true,
1318 extends: "libvndk",
1319 },
1320 nocrt: true,
1321 }
1322 `)
1323
1324 // Ensures that the core variant of "libvndk_ext" can be found.
1325 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1326 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1327 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1328 }
1329}
1330
Justin Yun0ecf0b22020-02-28 15:07:59 +09001331func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1332 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
1333 ctx := testCc(t, `
1334 cc_library {
1335 name: "libvndk",
1336 vendor_available: true,
1337 vndk: {
1338 enabled: true,
1339 },
1340 nocrt: true,
1341 }
1342
1343 cc_library {
1344 name: "libvndk_ext_product",
1345 product_specific: true,
1346 vndk: {
1347 enabled: true,
1348 extends: "libvndk",
1349 },
1350 nocrt: true,
1351 }
1352 `)
1353
1354 // Ensures that the core variant of "libvndk_ext_product" can be found.
1355 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1356 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1357 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1358 }
1359}
1360
Logan Chienf3511742017-10-31 18:04:35 +08001361func TestVndkExtError(t *testing.T) {
1362 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001363 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001364 cc_library {
1365 name: "libvndk",
1366 vendor_available: true,
1367 vndk: {
1368 enabled: true,
1369 },
1370 nocrt: true,
1371 }
1372
1373 cc_library {
1374 name: "libvndk_ext",
1375 vndk: {
1376 enabled: true,
1377 extends: "libvndk",
1378 },
1379 nocrt: true,
1380 }
1381 `)
1382
1383 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1384 cc_library {
1385 name: "libvndk",
1386 vendor_available: true,
1387 vndk: {
1388 enabled: true,
1389 },
1390 nocrt: true,
1391 }
1392
1393 cc_library {
1394 name: "libvndk_ext",
1395 vendor: true,
1396 vndk: {
1397 enabled: true,
1398 },
1399 nocrt: true,
1400 }
1401 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001402
1403 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1404 cc_library {
1405 name: "libvndk",
1406 vendor_available: true,
1407 vndk: {
1408 enabled: true,
1409 },
1410 nocrt: true,
1411 }
1412
1413 cc_library {
1414 name: "libvndk_ext_product",
1415 product_specific: true,
1416 vndk: {
1417 enabled: true,
1418 },
1419 nocrt: true,
1420 }
1421 `)
1422
1423 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1424 cc_library {
1425 name: "libvndk",
1426 vendor_available: true,
1427 vndk: {
1428 enabled: true,
1429 },
1430 nocrt: true,
1431 }
1432
1433 cc_library {
1434 name: "libvndk_ext_product",
1435 product_specific: true,
1436 vendor_available: true,
1437 vndk: {
1438 enabled: true,
1439 extends: "libvndk",
1440 },
1441 nocrt: true,
1442 }
1443 `)
Logan Chienf3511742017-10-31 18:04:35 +08001444}
1445
1446func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1447 // This test ensures an error is emitted for inconsistent support_system_process.
1448 testCcError(t, "module \".*\" with mismatched support_system_process", `
1449 cc_library {
1450 name: "libvndk",
1451 vendor_available: true,
1452 vndk: {
1453 enabled: true,
1454 },
1455 nocrt: true,
1456 }
1457
1458 cc_library {
1459 name: "libvndk_sp_ext",
1460 vendor: true,
1461 vndk: {
1462 enabled: true,
1463 extends: "libvndk",
1464 support_system_process: true,
1465 },
1466 nocrt: true,
1467 }
1468 `)
1469
1470 testCcError(t, "module \".*\" with mismatched support_system_process", `
1471 cc_library {
1472 name: "libvndk_sp",
1473 vendor_available: true,
1474 vndk: {
1475 enabled: true,
1476 support_system_process: true,
1477 },
1478 nocrt: true,
1479 }
1480
1481 cc_library {
1482 name: "libvndk_ext",
1483 vendor: true,
1484 vndk: {
1485 enabled: true,
1486 extends: "libvndk_sp",
1487 },
1488 nocrt: true,
1489 }
1490 `)
1491}
1492
1493func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001494 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08001495 // with `vendor_available: false`.
1496 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1497 cc_library {
1498 name: "libvndk",
1499 vendor_available: false,
1500 vndk: {
1501 enabled: true,
1502 },
1503 nocrt: true,
1504 }
1505
1506 cc_library {
1507 name: "libvndk_ext",
1508 vendor: true,
1509 vndk: {
1510 enabled: true,
1511 extends: "libvndk",
1512 },
1513 nocrt: true,
1514 }
1515 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001516
1517 testCcErrorProductVndk(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1518 cc_library {
1519 name: "libvndk",
1520 vendor_available: false,
1521 vndk: {
1522 enabled: true,
1523 },
1524 nocrt: true,
1525 }
1526
1527 cc_library {
1528 name: "libvndk_ext_product",
1529 product_specific: true,
1530 vndk: {
1531 enabled: true,
1532 extends: "libvndk",
1533 },
1534 nocrt: true,
1535 }
1536 `)
Logan Chienf3511742017-10-31 18:04:35 +08001537}
1538
Logan Chiend3c59a22018-03-29 14:08:15 +08001539func TestVendorModuleUseVndkExt(t *testing.T) {
1540 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001541 testCc(t, `
1542 cc_library {
1543 name: "libvndk",
1544 vendor_available: true,
1545 vndk: {
1546 enabled: true,
1547 },
1548 nocrt: true,
1549 }
1550
1551 cc_library {
1552 name: "libvndk_ext",
1553 vendor: true,
1554 vndk: {
1555 enabled: true,
1556 extends: "libvndk",
1557 },
1558 nocrt: true,
1559 }
1560
1561 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001562 name: "libvndk_sp",
1563 vendor_available: true,
1564 vndk: {
1565 enabled: true,
1566 support_system_process: true,
1567 },
1568 nocrt: true,
1569 }
1570
1571 cc_library {
1572 name: "libvndk_sp_ext",
1573 vendor: true,
1574 vndk: {
1575 enabled: true,
1576 extends: "libvndk_sp",
1577 support_system_process: true,
1578 },
1579 nocrt: true,
1580 }
1581
1582 cc_library {
1583 name: "libvendor",
1584 vendor: true,
1585 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1586 nocrt: true,
1587 }
1588 `)
1589}
1590
Logan Chiend3c59a22018-03-29 14:08:15 +08001591func TestVndkExtUseVendorLib(t *testing.T) {
1592 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001593 testCc(t, `
1594 cc_library {
1595 name: "libvndk",
1596 vendor_available: true,
1597 vndk: {
1598 enabled: true,
1599 },
1600 nocrt: true,
1601 }
1602
1603 cc_library {
1604 name: "libvndk_ext",
1605 vendor: true,
1606 vndk: {
1607 enabled: true,
1608 extends: "libvndk",
1609 },
1610 shared_libs: ["libvendor"],
1611 nocrt: true,
1612 }
1613
1614 cc_library {
1615 name: "libvendor",
1616 vendor: true,
1617 nocrt: true,
1618 }
1619 `)
Logan Chienf3511742017-10-31 18:04:35 +08001620
Logan Chiend3c59a22018-03-29 14:08:15 +08001621 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1622 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001623 cc_library {
1624 name: "libvndk_sp",
1625 vendor_available: true,
1626 vndk: {
1627 enabled: true,
1628 support_system_process: true,
1629 },
1630 nocrt: true,
1631 }
1632
1633 cc_library {
1634 name: "libvndk_sp_ext",
1635 vendor: true,
1636 vndk: {
1637 enabled: true,
1638 extends: "libvndk_sp",
1639 support_system_process: true,
1640 },
1641 shared_libs: ["libvendor"], // Cause an error
1642 nocrt: true,
1643 }
1644
1645 cc_library {
1646 name: "libvendor",
1647 vendor: true,
1648 nocrt: true,
1649 }
1650 `)
1651}
1652
Justin Yun0ecf0b22020-02-28 15:07:59 +09001653func TestProductVndkExtDependency(t *testing.T) {
1654 bp := `
1655 cc_library {
1656 name: "libvndk",
1657 vendor_available: true,
1658 vndk: {
1659 enabled: true,
1660 },
1661 nocrt: true,
1662 }
1663
1664 cc_library {
1665 name: "libvndk_ext_product",
1666 product_specific: true,
1667 vndk: {
1668 enabled: true,
1669 extends: "libvndk",
1670 },
1671 shared_libs: ["libproduct_for_vndklibs"],
1672 nocrt: true,
1673 }
1674
1675 cc_library {
1676 name: "libvndk_sp",
1677 vendor_available: true,
1678 vndk: {
1679 enabled: true,
1680 support_system_process: true,
1681 },
1682 nocrt: true,
1683 }
1684
1685 cc_library {
1686 name: "libvndk_sp_ext_product",
1687 product_specific: true,
1688 vndk: {
1689 enabled: true,
1690 extends: "libvndk_sp",
1691 support_system_process: true,
1692 },
1693 shared_libs: ["libproduct_for_vndklibs"],
1694 nocrt: true,
1695 }
1696
1697 cc_library {
1698 name: "libproduct",
1699 product_specific: true,
1700 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1701 nocrt: true,
1702 }
1703
1704 cc_library {
1705 name: "libproduct_for_vndklibs",
1706 product_specific: true,
1707 nocrt: true,
1708 }
1709 `
1710 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1711 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1712 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1713 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1714
1715 testCcWithConfig(t, config)
1716}
1717
Logan Chiend3c59a22018-03-29 14:08:15 +08001718func TestVndkSpExtUseVndkError(t *testing.T) {
1719 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1720 // library.
1721 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1722 cc_library {
1723 name: "libvndk",
1724 vendor_available: true,
1725 vndk: {
1726 enabled: true,
1727 },
1728 nocrt: true,
1729 }
1730
1731 cc_library {
1732 name: "libvndk_sp",
1733 vendor_available: true,
1734 vndk: {
1735 enabled: true,
1736 support_system_process: true,
1737 },
1738 nocrt: true,
1739 }
1740
1741 cc_library {
1742 name: "libvndk_sp_ext",
1743 vendor: true,
1744 vndk: {
1745 enabled: true,
1746 extends: "libvndk_sp",
1747 support_system_process: true,
1748 },
1749 shared_libs: ["libvndk"], // Cause an error
1750 nocrt: true,
1751 }
1752 `)
1753
1754 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1755 // library.
1756 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1757 cc_library {
1758 name: "libvndk",
1759 vendor_available: true,
1760 vndk: {
1761 enabled: true,
1762 },
1763 nocrt: true,
1764 }
1765
1766 cc_library {
1767 name: "libvndk_ext",
1768 vendor: true,
1769 vndk: {
1770 enabled: true,
1771 extends: "libvndk",
1772 },
1773 nocrt: true,
1774 }
1775
1776 cc_library {
1777 name: "libvndk_sp",
1778 vendor_available: true,
1779 vndk: {
1780 enabled: true,
1781 support_system_process: true,
1782 },
1783 nocrt: true,
1784 }
1785
1786 cc_library {
1787 name: "libvndk_sp_ext",
1788 vendor: true,
1789 vndk: {
1790 enabled: true,
1791 extends: "libvndk_sp",
1792 support_system_process: true,
1793 },
1794 shared_libs: ["libvndk_ext"], // Cause an error
1795 nocrt: true,
1796 }
1797 `)
1798}
1799
1800func TestVndkUseVndkExtError(t *testing.T) {
1801 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1802 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001803 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1804 cc_library {
1805 name: "libvndk",
1806 vendor_available: true,
1807 vndk: {
1808 enabled: true,
1809 },
1810 nocrt: true,
1811 }
1812
1813 cc_library {
1814 name: "libvndk_ext",
1815 vendor: true,
1816 vndk: {
1817 enabled: true,
1818 extends: "libvndk",
1819 },
1820 nocrt: true,
1821 }
1822
1823 cc_library {
1824 name: "libvndk2",
1825 vendor_available: true,
1826 vndk: {
1827 enabled: true,
1828 },
1829 shared_libs: ["libvndk_ext"],
1830 nocrt: true,
1831 }
1832 `)
1833
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001834 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001835 cc_library {
1836 name: "libvndk",
1837 vendor_available: true,
1838 vndk: {
1839 enabled: true,
1840 },
1841 nocrt: true,
1842 }
1843
1844 cc_library {
1845 name: "libvndk_ext",
1846 vendor: true,
1847 vndk: {
1848 enabled: true,
1849 extends: "libvndk",
1850 },
1851 nocrt: true,
1852 }
1853
1854 cc_library {
1855 name: "libvndk2",
1856 vendor_available: true,
1857 vndk: {
1858 enabled: true,
1859 },
1860 target: {
1861 vendor: {
1862 shared_libs: ["libvndk_ext"],
1863 },
1864 },
1865 nocrt: true,
1866 }
1867 `)
1868
1869 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1870 cc_library {
1871 name: "libvndk_sp",
1872 vendor_available: true,
1873 vndk: {
1874 enabled: true,
1875 support_system_process: true,
1876 },
1877 nocrt: true,
1878 }
1879
1880 cc_library {
1881 name: "libvndk_sp_ext",
1882 vendor: true,
1883 vndk: {
1884 enabled: true,
1885 extends: "libvndk_sp",
1886 support_system_process: true,
1887 },
1888 nocrt: true,
1889 }
1890
1891 cc_library {
1892 name: "libvndk_sp_2",
1893 vendor_available: true,
1894 vndk: {
1895 enabled: true,
1896 support_system_process: true,
1897 },
1898 shared_libs: ["libvndk_sp_ext"],
1899 nocrt: true,
1900 }
1901 `)
1902
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001903 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001904 cc_library {
1905 name: "libvndk_sp",
1906 vendor_available: true,
1907 vndk: {
1908 enabled: true,
1909 },
1910 nocrt: true,
1911 }
1912
1913 cc_library {
1914 name: "libvndk_sp_ext",
1915 vendor: true,
1916 vndk: {
1917 enabled: true,
1918 extends: "libvndk_sp",
1919 },
1920 nocrt: true,
1921 }
1922
1923 cc_library {
1924 name: "libvndk_sp2",
1925 vendor_available: true,
1926 vndk: {
1927 enabled: true,
1928 },
1929 target: {
1930 vendor: {
1931 shared_libs: ["libvndk_sp_ext"],
1932 },
1933 },
1934 nocrt: true,
1935 }
1936 `)
1937}
1938
Justin Yun5f7f7e82019-11-18 19:52:14 +09001939func TestEnforceProductVndkVersion(t *testing.T) {
1940 bp := `
1941 cc_library {
1942 name: "libllndk",
1943 }
1944 llndk_library {
1945 name: "libllndk",
1946 symbol_file: "",
1947 }
1948 cc_library {
1949 name: "libvndk",
1950 vendor_available: true,
1951 vndk: {
1952 enabled: true,
1953 },
1954 nocrt: true,
1955 }
1956 cc_library {
1957 name: "libvndk_sp",
1958 vendor_available: true,
1959 vndk: {
1960 enabled: true,
1961 support_system_process: true,
1962 },
1963 nocrt: true,
1964 }
1965 cc_library {
1966 name: "libva",
1967 vendor_available: true,
1968 nocrt: true,
1969 }
1970 cc_library {
1971 name: "libproduct_va",
1972 product_specific: true,
1973 vendor_available: true,
1974 nocrt: true,
1975 }
1976 cc_library {
1977 name: "libprod",
1978 product_specific: true,
1979 shared_libs: [
1980 "libllndk",
1981 "libvndk",
1982 "libvndk_sp",
1983 "libva",
1984 "libproduct_va",
1985 ],
1986 nocrt: true,
1987 }
1988 cc_library {
1989 name: "libvendor",
1990 vendor: true,
1991 shared_libs: [
1992 "libllndk",
1993 "libvndk",
1994 "libvndk_sp",
1995 "libva",
1996 "libproduct_va",
1997 ],
1998 nocrt: true,
1999 }
2000 `
2001
2002 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2003 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2004 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2005 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2006
2007 ctx := testCcWithConfig(t, config)
2008
Justin Yun0ecf0b22020-02-28 15:07:59 +09002009 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "", productVariant)
2010 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "", productVariant)
Justin Yun5f7f7e82019-11-18 19:52:14 +09002011}
2012
2013func TestEnforceProductVndkVersionErrors(t *testing.T) {
2014 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2015 cc_library {
2016 name: "libprod",
2017 product_specific: true,
2018 shared_libs: [
2019 "libvendor",
2020 ],
2021 nocrt: true,
2022 }
2023 cc_library {
2024 name: "libvendor",
2025 vendor: true,
2026 nocrt: true,
2027 }
2028 `)
2029 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2030 cc_library {
2031 name: "libprod",
2032 product_specific: true,
2033 shared_libs: [
2034 "libsystem",
2035 ],
2036 nocrt: true,
2037 }
2038 cc_library {
2039 name: "libsystem",
2040 nocrt: true,
2041 }
2042 `)
2043 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
2044 cc_library {
2045 name: "libprod",
2046 product_specific: true,
2047 shared_libs: [
2048 "libvndk_private",
2049 ],
2050 nocrt: true,
2051 }
2052 cc_library {
2053 name: "libvndk_private",
2054 vendor_available: false,
2055 vndk: {
2056 enabled: true,
2057 },
2058 nocrt: true,
2059 }
2060 `)
2061 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2062 cc_library {
2063 name: "libprod",
2064 product_specific: true,
2065 shared_libs: [
2066 "libsystem_ext",
2067 ],
2068 nocrt: true,
2069 }
2070 cc_library {
2071 name: "libsystem_ext",
2072 system_ext_specific: true,
2073 nocrt: true,
2074 }
2075 `)
2076 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2077 cc_library {
2078 name: "libsystem",
2079 shared_libs: [
2080 "libproduct_va",
2081 ],
2082 nocrt: true,
2083 }
2084 cc_library {
2085 name: "libproduct_va",
2086 product_specific: true,
2087 vendor_available: true,
2088 nocrt: true,
2089 }
2090 `)
2091}
2092
Jooyung Han38002912019-05-16 04:01:54 +09002093func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002094 bp := `
2095 cc_library {
2096 name: "libvndk",
2097 vendor_available: true,
2098 vndk: {
2099 enabled: true,
2100 },
2101 }
2102 cc_library {
2103 name: "libvndksp",
2104 vendor_available: true,
2105 vndk: {
2106 enabled: true,
2107 support_system_process: true,
2108 },
2109 }
2110 cc_library {
2111 name: "libvndkprivate",
2112 vendor_available: false,
2113 vndk: {
2114 enabled: true,
2115 },
2116 }
2117 cc_library {
2118 name: "libvendor",
2119 vendor: true,
2120 }
2121 cc_library {
2122 name: "libvndkext",
2123 vendor: true,
2124 vndk: {
2125 enabled: true,
2126 extends: "libvndk",
2127 },
2128 }
2129 vndk_prebuilt_shared {
2130 name: "prevndk",
2131 version: "27",
2132 target_arch: "arm",
2133 binder32bit: true,
2134 vendor_available: true,
2135 vndk: {
2136 enabled: true,
2137 },
2138 arch: {
2139 arm: {
2140 srcs: ["liba.so"],
2141 },
2142 },
2143 }
2144 cc_library {
2145 name: "libllndk",
2146 }
2147 llndk_library {
2148 name: "libllndk",
2149 symbol_file: "",
2150 }
2151 cc_library {
2152 name: "libllndkprivate",
2153 }
2154 llndk_library {
2155 name: "libllndkprivate",
2156 vendor_available: false,
2157 symbol_file: "",
2158 }`
2159
2160 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002161 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2162 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2163 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002164 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002165
Jooyung Han0302a842019-10-30 18:43:49 +09002166 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002167 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09002168 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002169 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09002170 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002171 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09002172 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002173 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09002174
Colin Crossfb0c16e2019-11-20 17:12:35 -08002175 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002176
Jooyung Han38002912019-05-16 04:01:54 +09002177 tests := []struct {
2178 variant string
2179 name string
2180 expected string
2181 }{
2182 {vendorVariant, "libvndk", "native:vndk"},
2183 {vendorVariant, "libvndksp", "native:vndk"},
2184 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2185 {vendorVariant, "libvendor", "native:vendor"},
2186 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09002187 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002188 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002189 {coreVariant, "libvndk", "native:platform"},
2190 {coreVariant, "libvndkprivate", "native:platform"},
2191 {coreVariant, "libllndk", "native:platform"},
2192 }
2193 for _, test := range tests {
2194 t.Run(test.name, func(t *testing.T) {
2195 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2196 assertString(t, module.makeLinkType, test.expected)
2197 })
2198 }
2199}
2200
Colin Cross0af4b842015-04-30 16:36:18 -07002201var (
2202 str11 = "01234567891"
2203 str10 = str11[:10]
2204 str9 = str11[:9]
2205 str5 = str11[:5]
2206 str4 = str11[:4]
2207)
2208
2209var splitListForSizeTestCases = []struct {
2210 in []string
2211 out [][]string
2212 size int
2213}{
2214 {
2215 in: []string{str10},
2216 out: [][]string{{str10}},
2217 size: 10,
2218 },
2219 {
2220 in: []string{str9},
2221 out: [][]string{{str9}},
2222 size: 10,
2223 },
2224 {
2225 in: []string{str5},
2226 out: [][]string{{str5}},
2227 size: 10,
2228 },
2229 {
2230 in: []string{str11},
2231 out: nil,
2232 size: 10,
2233 },
2234 {
2235 in: []string{str10, str10},
2236 out: [][]string{{str10}, {str10}},
2237 size: 10,
2238 },
2239 {
2240 in: []string{str9, str10},
2241 out: [][]string{{str9}, {str10}},
2242 size: 10,
2243 },
2244 {
2245 in: []string{str10, str9},
2246 out: [][]string{{str10}, {str9}},
2247 size: 10,
2248 },
2249 {
2250 in: []string{str5, str4},
2251 out: [][]string{{str5, str4}},
2252 size: 10,
2253 },
2254 {
2255 in: []string{str5, str4, str5},
2256 out: [][]string{{str5, str4}, {str5}},
2257 size: 10,
2258 },
2259 {
2260 in: []string{str5, str4, str5, str4},
2261 out: [][]string{{str5, str4}, {str5, str4}},
2262 size: 10,
2263 },
2264 {
2265 in: []string{str5, str4, str5, str5},
2266 out: [][]string{{str5, str4}, {str5}, {str5}},
2267 size: 10,
2268 },
2269 {
2270 in: []string{str5, str5, str5, str4},
2271 out: [][]string{{str5}, {str5}, {str5, str4}},
2272 size: 10,
2273 },
2274 {
2275 in: []string{str9, str11},
2276 out: nil,
2277 size: 10,
2278 },
2279 {
2280 in: []string{str11, str9},
2281 out: nil,
2282 size: 10,
2283 },
2284}
2285
2286func TestSplitListForSize(t *testing.T) {
2287 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08002288 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07002289
2290 var outStrings [][]string
2291
2292 if len(out) > 0 {
2293 outStrings = make([][]string, len(out))
2294 for i, o := range out {
2295 outStrings[i] = o.Strings()
2296 }
2297 }
2298
2299 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07002300 t.Errorf("incorrect output:")
2301 t.Errorf(" input: %#v", testCase.in)
2302 t.Errorf(" size: %d", testCase.size)
2303 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07002304 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07002305 }
2306 }
2307}
Jeff Gaston294356f2017-09-27 17:05:30 -07002308
2309var staticLinkDepOrderTestCases = []struct {
2310 // This is a string representation of a map[moduleName][]moduleDependency .
2311 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002312 inStatic string
2313
2314 // This is a string representation of a map[moduleName][]moduleDependency .
2315 // It models the dependencies declared in an Android.bp file.
2316 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002317
2318 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2319 // The keys of allOrdered specify which modules we would like to check.
2320 // The values of allOrdered specify the expected result (of the transitive closure of all
2321 // dependencies) for each module to test
2322 allOrdered string
2323
2324 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2325 // The keys of outOrdered specify which modules we would like to check.
2326 // The values of outOrdered specify the expected result (of the ordered linker command line)
2327 // for each module to test.
2328 outOrdered string
2329}{
2330 // Simple tests
2331 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002332 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002333 outOrdered: "",
2334 },
2335 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002336 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002337 outOrdered: "a:",
2338 },
2339 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002340 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002341 outOrdered: "a:b; b:",
2342 },
2343 // Tests of reordering
2344 {
2345 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002346 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002347 outOrdered: "a:b,c,d; b:d; c:d; d:",
2348 },
2349 {
2350 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002351 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002352 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2353 },
2354 {
2355 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002356 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002357 outOrdered: "a:d,b,e,c; d:b; e:c",
2358 },
2359 {
2360 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002361 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002362 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2363 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2364 },
2365 {
2366 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002367 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 -07002368 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2369 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2370 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002371 // shared dependencies
2372 {
2373 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2374 // So, we don't actually have to check that a shared dependency of c will change the order
2375 // of a library that depends statically on b and on c. We only need to check that if c has
2376 // a shared dependency on b, that that shows up in allOrdered.
2377 inShared: "c:b",
2378 allOrdered: "c:b",
2379 outOrdered: "c:",
2380 },
2381 {
2382 // This test doesn't actually include any shared dependencies but it's a reminder of what
2383 // the second phase of the above test would look like
2384 inStatic: "a:b,c; c:b",
2385 allOrdered: "a:c,b; c:b",
2386 outOrdered: "a:c,b; c:b",
2387 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002388 // tiebreakers for when two modules specifying different orderings and there is no dependency
2389 // to dictate an order
2390 {
2391 // 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 -08002392 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002393 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2394 },
2395 {
2396 // 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 -08002397 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 -07002398 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2399 },
2400 // Tests involving duplicate dependencies
2401 {
2402 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002403 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002404 outOrdered: "a:c,b",
2405 },
2406 {
2407 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002408 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002409 outOrdered: "a:d,c,b",
2410 },
2411 // Tests to confirm the nonexistence of infinite loops.
2412 // These cases should never happen, so as long as the test terminates and the
2413 // result is deterministic then that should be fine.
2414 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002415 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002416 outOrdered: "a:a",
2417 },
2418 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002419 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002420 allOrdered: "a:b,c; b:c,a; c:a,b",
2421 outOrdered: "a:b; b:c; c:a",
2422 },
2423 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002424 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002425 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2426 outOrdered: "a:c,b; b:a,c; c:b,a",
2427 },
2428}
2429
2430// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2431func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2432 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2433 strippedText := strings.Replace(text, " ", "", -1)
2434 if len(strippedText) < 1 {
2435 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2436 }
2437 allDeps = make(map[android.Path][]android.Path, 0)
2438
2439 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2440 moduleTexts := strings.Split(strippedText, ";")
2441
2442 outputForModuleName := func(moduleName string) android.Path {
2443 return android.PathForTesting(moduleName)
2444 }
2445
2446 for _, moduleText := range moduleTexts {
2447 // convert from "a:b,c" to ["a", "b,c"]
2448 components := strings.Split(moduleText, ":")
2449 if len(components) != 2 {
2450 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2451 }
2452 moduleName := components[0]
2453 moduleOutput := outputForModuleName(moduleName)
2454 modulesInOrder = append(modulesInOrder, moduleOutput)
2455
2456 depString := components[1]
2457 // convert from "b,c" to ["b", "c"]
2458 depNames := strings.Split(depString, ",")
2459 if len(depString) < 1 {
2460 depNames = []string{}
2461 }
2462 var deps []android.Path
2463 for _, depName := range depNames {
2464 deps = append(deps, outputForModuleName(depName))
2465 }
2466 allDeps[moduleOutput] = deps
2467 }
2468 return modulesInOrder, allDeps
2469}
2470
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002471func TestLinkReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002472 for _, testCase := range staticLinkDepOrderTestCases {
2473 errs := []string{}
2474
2475 // parse testcase
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002476 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)
Jeff Gaston294356f2017-09-27 17:05:30 -07002477 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)
2478 if testCase.allOrdered == "" {
2479 // allow the test case to skip specifying allOrdered
2480 testCase.allOrdered = testCase.outOrdered
2481 }
2482 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002483 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)
Jeff Gaston294356f2017-09-27 17:05:30 -07002484
2485 // For each module whose post-reordered dependencies were specified, validate that
2486 // reordering the inputs produces the expected outputs.
2487 for _, moduleName := range expectedModuleNames {
2488 moduleDeps := givenTransitiveDeps[moduleName]
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002489 givenSharedDeps := givenAllSharedDeps[moduleName]
2490 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -07002491
2492 correctAllOrdered := expectedAllDeps[moduleName]
2493 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {
2494 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002495 "\nin static:%q"+
2496 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002497 "\nmodule: %v"+
2498 "\nexpected: %s"+
2499 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002500 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002501 }
2502
2503 correctOutputDeps := expectedTransitiveDeps[moduleName]
2504 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {
2505 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002506 "\nin static:%q"+
2507 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002508 "\nmodule: %v"+
2509 "\nexpected: %s"+
2510 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002511 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002512 }
2513 }
2514
2515 if len(errs) > 0 {
2516 sort.Strings(errs)
2517 for _, err := range errs {
2518 t.Error(err)
2519 }
2520 }
2521 }
2522}
Logan Chienf3511742017-10-31 18:04:35 +08002523
Jeff Gaston294356f2017-09-27 17:05:30 -07002524func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2525 for _, moduleName := range moduleNames {
2526 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
2527 output := module.outputFile.Path()
2528 paths = append(paths, output)
2529 }
2530 return paths
2531}
2532
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002533func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002534 ctx := testCc(t, `
2535 cc_library {
2536 name: "a",
2537 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002538 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002539 }
2540 cc_library {
2541 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002542 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002543 }
2544 cc_library {
2545 name: "c",
2546 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002547 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002548 }
2549 cc_library {
2550 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002551 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002552 }
2553
2554 `)
2555
Colin Cross7113d202019-11-20 16:39:12 -08002556 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002557 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002558 actual := moduleA.depsInLinkOrder
Jeff Gaston294356f2017-09-27 17:05:30 -07002559 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})
2560
2561 if !reflect.DeepEqual(actual, expected) {
2562 t.Errorf("staticDeps orderings were not propagated correctly"+
2563 "\nactual: %v"+
2564 "\nexpected: %v",
2565 actual,
2566 expected,
2567 )
2568 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002569}
Jeff Gaston294356f2017-09-27 17:05:30 -07002570
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002571func TestStaticLibDepReorderingWithShared(t *testing.T) {
2572 ctx := testCc(t, `
2573 cc_library {
2574 name: "a",
2575 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002576 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002577 }
2578 cc_library {
2579 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002580 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002581 }
2582 cc_library {
2583 name: "c",
2584 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002585 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002586 }
2587
2588 `)
2589
Colin Cross7113d202019-11-20 16:39:12 -08002590 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002591 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
2592 actual := moduleA.depsInLinkOrder
2593 expected := getOutputPaths(ctx, variant, []string{"c", "b"})
2594
2595 if !reflect.DeepEqual(actual, expected) {
2596 t.Errorf("staticDeps orderings did not account for shared libs"+
2597 "\nactual: %v"+
2598 "\nexpected: %v",
2599 actual,
2600 expected,
2601 )
2602 }
2603}
2604
Jooyung Hanb04a4992020-03-13 18:57:35 +09002605func checkEquals(t *testing.T, message string, expected, actual interface{}) {
2606 if !reflect.DeepEqual(actual, expected) {
2607 t.Errorf(message+
2608 "\nactual: %v"+
2609 "\nexpected: %v",
2610 actual,
2611 expected,
2612 )
2613 }
2614}
2615
Jooyung Han61b66e92020-03-21 14:21:46 +00002616func TestLlndkLibrary(t *testing.T) {
2617 ctx := testCc(t, `
2618 cc_library {
2619 name: "libllndk",
2620 stubs: { versions: ["1", "2"] },
2621 }
2622 llndk_library {
2623 name: "libllndk",
2624 }
2625 `)
2626 actual := ctx.ModuleVariantsForTests("libllndk.llndk")
2627 expected := []string{
2628 "android_vendor.VER_arm64_armv8-a_shared",
2629 "android_vendor.VER_arm64_armv8-a_shared_1",
2630 "android_vendor.VER_arm64_armv8-a_shared_2",
2631 "android_vendor.VER_arm_armv7-a-neon_shared",
2632 "android_vendor.VER_arm_armv7-a-neon_shared_1",
2633 "android_vendor.VER_arm_armv7-a-neon_shared_2",
2634 }
2635 checkEquals(t, "variants for llndk stubs", expected, actual)
2636
2637 params := ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
2638 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2639
2640 params = ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
2641 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2642}
2643
Jiyong Parka46a4d52017-12-14 19:54:34 +09002644func TestLlndkHeaders(t *testing.T) {
2645 ctx := testCc(t, `
2646 llndk_headers {
2647 name: "libllndk_headers",
2648 export_include_dirs: ["my_include"],
2649 }
2650 llndk_library {
2651 name: "libllndk",
2652 export_llndk_headers: ["libllndk_headers"],
2653 }
2654 cc_library {
2655 name: "libvendor",
2656 shared_libs: ["libllndk"],
2657 vendor: true,
2658 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002659 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002660 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002661 }
2662 `)
2663
2664 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002665 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002666 cflags := cc.Args["cFlags"]
2667 if !strings.Contains(cflags, "-Imy_include") {
2668 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2669 }
2670}
2671
Logan Chien43d34c32017-12-20 01:17:32 +08002672func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2673 actual := module.Properties.AndroidMkRuntimeLibs
2674 if !reflect.DeepEqual(actual, expected) {
2675 t.Errorf("incorrect runtime_libs for shared libs"+
2676 "\nactual: %v"+
2677 "\nexpected: %v",
2678 actual,
2679 expected,
2680 )
2681 }
2682}
2683
2684const runtimeLibAndroidBp = `
2685 cc_library {
2686 name: "libvendor_available1",
2687 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002688 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002689 nocrt : true,
2690 system_shared_libs : [],
2691 }
2692 cc_library {
2693 name: "libvendor_available2",
2694 vendor_available: true,
2695 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002696 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002697 nocrt : true,
2698 system_shared_libs : [],
2699 }
2700 cc_library {
2701 name: "libvendor_available3",
2702 vendor_available: true,
2703 runtime_libs: ["libvendor_available1"],
2704 target: {
2705 vendor: {
2706 exclude_runtime_libs: ["libvendor_available1"],
2707 }
2708 },
Yi Konge7fe9912019-06-02 00:53:50 -07002709 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002710 nocrt : true,
2711 system_shared_libs : [],
2712 }
2713 cc_library {
2714 name: "libcore",
2715 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002716 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002717 nocrt : true,
2718 system_shared_libs : [],
2719 }
2720 cc_library {
2721 name: "libvendor1",
2722 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002723 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002724 nocrt : true,
2725 system_shared_libs : [],
2726 }
2727 cc_library {
2728 name: "libvendor2",
2729 vendor: true,
2730 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002731 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002732 nocrt : true,
2733 system_shared_libs : [],
2734 }
2735`
2736
2737func TestRuntimeLibs(t *testing.T) {
2738 ctx := testCc(t, runtimeLibAndroidBp)
2739
2740 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002741 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002742
2743 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2744 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2745
2746 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
2747 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2748
2749 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2750 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002751 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002752
2753 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2754 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
2755
2756 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2757 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
2758}
2759
2760func TestExcludeRuntimeLibs(t *testing.T) {
2761 ctx := testCc(t, runtimeLibAndroidBp)
2762
Colin Cross7113d202019-11-20 16:39:12 -08002763 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002764 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2765 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2766
Colin Crossfb0c16e2019-11-20 17:12:35 -08002767 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002768 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2769 checkRuntimeLibs(t, nil, module)
2770}
2771
2772func TestRuntimeLibsNoVndk(t *testing.T) {
2773 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2774
2775 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2776
Colin Cross7113d202019-11-20 16:39:12 -08002777 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002778
2779 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2780 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2781
2782 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2783 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
2784}
2785
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002786func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002787 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002788 actual := module.Properties.AndroidMkStaticLibs
2789 if !reflect.DeepEqual(actual, expected) {
2790 t.Errorf("incorrect static_libs"+
2791 "\nactual: %v"+
2792 "\nexpected: %v",
2793 actual,
2794 expected,
2795 )
2796 }
2797}
2798
2799const staticLibAndroidBp = `
2800 cc_library {
2801 name: "lib1",
2802 }
2803 cc_library {
2804 name: "lib2",
2805 static_libs: ["lib1"],
2806 }
2807`
2808
2809func TestStaticLibDepExport(t *testing.T) {
2810 ctx := testCc(t, staticLibAndroidBp)
2811
2812 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002813 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002814 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002815 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002816
2817 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002818 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002819 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2820 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002821 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002822}
2823
Jiyong Parkd08b6972017-09-26 10:50:54 +09002824var compilerFlagsTestCases = []struct {
2825 in string
2826 out bool
2827}{
2828 {
2829 in: "a",
2830 out: false,
2831 },
2832 {
2833 in: "-a",
2834 out: true,
2835 },
2836 {
2837 in: "-Ipath/to/something",
2838 out: false,
2839 },
2840 {
2841 in: "-isystempath/to/something",
2842 out: false,
2843 },
2844 {
2845 in: "--coverage",
2846 out: false,
2847 },
2848 {
2849 in: "-include a/b",
2850 out: true,
2851 },
2852 {
2853 in: "-include a/b c/d",
2854 out: false,
2855 },
2856 {
2857 in: "-DMACRO",
2858 out: true,
2859 },
2860 {
2861 in: "-DMAC RO",
2862 out: false,
2863 },
2864 {
2865 in: "-a -b",
2866 out: false,
2867 },
2868 {
2869 in: "-DMACRO=definition",
2870 out: true,
2871 },
2872 {
2873 in: "-DMACRO=defi nition",
2874 out: true, // TODO(jiyong): this should be false
2875 },
2876 {
2877 in: "-DMACRO(x)=x + 1",
2878 out: true,
2879 },
2880 {
2881 in: "-DMACRO=\"defi nition\"",
2882 out: true,
2883 },
2884}
2885
2886type mockContext struct {
2887 BaseModuleContext
2888 result bool
2889}
2890
2891func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2892 // CheckBadCompilerFlags calls this function when the flag should be rejected
2893 ctx.result = false
2894}
2895
2896func TestCompilerFlags(t *testing.T) {
2897 for _, testCase := range compilerFlagsTestCases {
2898 ctx := &mockContext{result: true}
2899 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
2900 if ctx.result != testCase.out {
2901 t.Errorf("incorrect output:")
2902 t.Errorf(" input: %#v", testCase.in)
2903 t.Errorf(" expected: %#v", testCase.out)
2904 t.Errorf(" got: %#v", ctx.result)
2905 }
2906 }
Jeff Gaston294356f2017-09-27 17:05:30 -07002907}
Jiyong Park374510b2018-03-19 18:23:01 +09002908
2909func TestVendorPublicLibraries(t *testing.T) {
2910 ctx := testCc(t, `
2911 cc_library_headers {
2912 name: "libvendorpublic_headers",
2913 export_include_dirs: ["my_include"],
2914 }
2915 vendor_public_library {
2916 name: "libvendorpublic",
2917 symbol_file: "",
2918 export_public_headers: ["libvendorpublic_headers"],
2919 }
2920 cc_library {
2921 name: "libvendorpublic",
2922 srcs: ["foo.c"],
2923 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002924 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002925 nocrt: true,
2926 }
2927
2928 cc_library {
2929 name: "libsystem",
2930 shared_libs: ["libvendorpublic"],
2931 vendor: false,
2932 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002933 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002934 nocrt: true,
2935 }
2936 cc_library {
2937 name: "libvendor",
2938 shared_libs: ["libvendorpublic"],
2939 vendor: true,
2940 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002941 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002942 nocrt: true,
2943 }
2944 `)
2945
Colin Cross7113d202019-11-20 16:39:12 -08002946 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08002947 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09002948
2949 // test if header search paths are correctly added
2950 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08002951 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09002952 cflags := cc.Args["cFlags"]
2953 if !strings.Contains(cflags, "-Imy_include") {
2954 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
2955 }
2956
2957 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08002958 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002959 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002960 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09002961 if !strings.Contains(libflags, stubPaths[0].String()) {
2962 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
2963 }
2964
2965 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08002966 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002967 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002968 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09002969 if !strings.Contains(libflags, stubPaths[0].String()) {
2970 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
2971 }
2972
2973}
Jiyong Park37b25202018-07-11 10:49:27 +09002974
2975func TestRecovery(t *testing.T) {
2976 ctx := testCc(t, `
2977 cc_library_shared {
2978 name: "librecovery",
2979 recovery: true,
2980 }
2981 cc_library_shared {
2982 name: "librecovery32",
2983 recovery: true,
2984 compile_multilib:"32",
2985 }
Jiyong Park5baac542018-08-28 09:55:37 +09002986 cc_library_shared {
2987 name: "libHalInRecovery",
2988 recovery_available: true,
2989 vendor: true,
2990 }
Jiyong Park37b25202018-07-11 10:49:27 +09002991 `)
2992
2993 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08002994 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09002995 if len(variants) != 1 || !android.InList(arm64, variants) {
2996 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
2997 }
2998
2999 variants = ctx.ModuleVariantsForTests("librecovery32")
3000 if android.InList(arm64, variants) {
3001 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3002 }
Jiyong Park5baac542018-08-28 09:55:37 +09003003
3004 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3005 if !recoveryModule.Platform() {
3006 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3007 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003008}
Jiyong Park5baac542018-08-28 09:55:37 +09003009
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003010func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3011 bp := `
3012 cc_prebuilt_test_library_shared {
3013 name: "test_lib",
3014 relative_install_path: "foo/bar/baz",
3015 srcs: ["srcpath/dontusethispath/baz.so"],
3016 }
3017
3018 cc_test {
3019 name: "main_test",
3020 data_libs: ["test_lib"],
3021 gtest: false,
3022 }
3023 `
3024
3025 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3026 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3027 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3028 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3029
3030 ctx := testCcWithConfig(t, config)
3031 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3032 testBinary := module.(*Module).linker.(*testBinary)
3033 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3034 if err != nil {
3035 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3036 }
3037 if len(outputFiles) != 1 {
3038 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3039 }
3040 if len(testBinary.dataPaths()) != 1 {
3041 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3042 }
3043
3044 outputPath := outputFiles[0].String()
3045
3046 if !strings.HasSuffix(outputPath, "/main_test") {
3047 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3048 }
3049 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
3050 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3051 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3052 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3053 }
3054}
3055
Jiyong Park7ed9de32018-10-15 22:25:07 +09003056func TestVersionedStubs(t *testing.T) {
3057 ctx := testCc(t, `
3058 cc_library_shared {
3059 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003060 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003061 stubs: {
3062 symbol_file: "foo.map.txt",
3063 versions: ["1", "2", "3"],
3064 },
3065 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003066
Jiyong Park7ed9de32018-10-15 22:25:07 +09003067 cc_library_shared {
3068 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003069 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003070 shared_libs: ["libFoo#1"],
3071 }`)
3072
3073 variants := ctx.ModuleVariantsForTests("libFoo")
3074 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003075 "android_arm64_armv8-a_shared",
3076 "android_arm64_armv8-a_shared_1",
3077 "android_arm64_armv8-a_shared_2",
3078 "android_arm64_armv8-a_shared_3",
3079 "android_arm_armv7-a-neon_shared",
3080 "android_arm_armv7-a-neon_shared_1",
3081 "android_arm_armv7-a-neon_shared_2",
3082 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003083 }
3084 variantsMismatch := false
3085 if len(variants) != len(expectedVariants) {
3086 variantsMismatch = true
3087 } else {
3088 for _, v := range expectedVariants {
3089 if !inList(v, variants) {
3090 variantsMismatch = false
3091 }
3092 }
3093 }
3094 if variantsMismatch {
3095 t.Errorf("variants of libFoo expected:\n")
3096 for _, v := range expectedVariants {
3097 t.Errorf("%q\n", v)
3098 }
3099 t.Errorf(", but got:\n")
3100 for _, v := range variants {
3101 t.Errorf("%q\n", v)
3102 }
3103 }
3104
Colin Cross7113d202019-11-20 16:39:12 -08003105 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003106 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003107 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003108 if !strings.Contains(libFlags, libFoo1StubPath) {
3109 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3110 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003111
Colin Cross7113d202019-11-20 16:39:12 -08003112 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003113 cFlags := libBarCompileRule.Args["cFlags"]
3114 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3115 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3116 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3117 }
Jiyong Park37b25202018-07-11 10:49:27 +09003118}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003119
Jooyung Hanb04a4992020-03-13 18:57:35 +09003120func TestVersioningMacro(t *testing.T) {
3121 for _, tc := range []struct{ moduleName, expected string }{
3122 {"libc", "__LIBC_API__"},
3123 {"libfoo", "__LIBFOO_API__"},
3124 {"libfoo@1", "__LIBFOO_1_API__"},
3125 {"libfoo-v1", "__LIBFOO_V1_API__"},
3126 {"libfoo.v1", "__LIBFOO_V1_API__"},
3127 } {
3128 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3129 }
3130}
3131
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003132func TestStaticExecutable(t *testing.T) {
3133 ctx := testCc(t, `
3134 cc_binary {
3135 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003136 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003137 static_executable: true,
3138 }`)
3139
Colin Cross7113d202019-11-20 16:39:12 -08003140 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003141 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3142 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003143 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003144 for _, lib := range systemStaticLibs {
3145 if !strings.Contains(libFlags, lib) {
3146 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3147 }
3148 }
3149 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3150 for _, lib := range systemSharedLibs {
3151 if strings.Contains(libFlags, lib) {
3152 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3153 }
3154 }
3155}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003156
3157func TestStaticDepsOrderWithStubs(t *testing.T) {
3158 ctx := testCc(t, `
3159 cc_binary {
3160 name: "mybin",
3161 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003162 static_libs: ["libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003163 static_executable: true,
3164 stl: "none",
3165 }
3166
3167 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003168 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003169 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003170 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003171 stl: "none",
3172 }
3173
3174 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003175 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003176 srcs: ["foo.c"],
3177 stl: "none",
3178 stubs: {
3179 versions: ["1"],
3180 },
3181 }`)
3182
Colin Cross7113d202019-11-20 16:39:12 -08003183 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Module().(*Module)
Jiyong Parke4bb9862019-02-01 00:31:10 +09003184 actual := mybin.depsInLinkOrder
Colin Crossf9aabd72020-02-15 11:29:50 -08003185 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003186
3187 if !reflect.DeepEqual(actual, expected) {
3188 t.Errorf("staticDeps orderings were not propagated correctly"+
3189 "\nactual: %v"+
3190 "\nexpected: %v",
3191 actual,
3192 expected,
3193 )
3194 }
3195}
Jooyung Han38002912019-05-16 04:01:54 +09003196
Jooyung Hand48f3c32019-08-23 11:18:57 +09003197func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3198 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3199 cc_library {
3200 name: "libA",
3201 srcs: ["foo.c"],
3202 shared_libs: ["libB"],
3203 stl: "none",
3204 }
3205
3206 cc_library {
3207 name: "libB",
3208 srcs: ["foo.c"],
3209 enabled: false,
3210 stl: "none",
3211 }
3212 `)
3213}
3214
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003215// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3216// correctly.
3217func TestFuzzTarget(t *testing.T) {
3218 ctx := testCc(t, `
3219 cc_fuzz {
3220 name: "fuzz_smoke_test",
3221 srcs: ["foo.c"],
3222 }`)
3223
Paul Duffin075c4172019-12-19 19:06:13 +00003224 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003225 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3226}
3227
Jiyong Park29074592019-07-07 16:27:47 +09003228func TestAidl(t *testing.T) {
3229}
3230
Jooyung Han38002912019-05-16 04:01:54 +09003231func assertString(t *testing.T, got, expected string) {
3232 t.Helper()
3233 if got != expected {
3234 t.Errorf("expected %q got %q", expected, got)
3235 }
3236}
3237
3238func assertArrayString(t *testing.T, got, expected []string) {
3239 t.Helper()
3240 if len(got) != len(expected) {
3241 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3242 return
3243 }
3244 for i := range got {
3245 if got[i] != expected[i] {
3246 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3247 i, expected[i], expected, got[i], got)
3248 return
3249 }
3250 }
3251}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003252
Jooyung Han0302a842019-10-30 18:43:49 +09003253func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3254 t.Helper()
3255 assertArrayString(t, android.SortedStringKeys(m), expected)
3256}
3257
Colin Crosse1bb5d02019-09-24 14:55:04 -07003258func TestDefaults(t *testing.T) {
3259 ctx := testCc(t, `
3260 cc_defaults {
3261 name: "defaults",
3262 srcs: ["foo.c"],
3263 static: {
3264 srcs: ["bar.c"],
3265 },
3266 shared: {
3267 srcs: ["baz.c"],
3268 },
3269 }
3270
3271 cc_library_static {
3272 name: "libstatic",
3273 defaults: ["defaults"],
3274 }
3275
3276 cc_library_shared {
3277 name: "libshared",
3278 defaults: ["defaults"],
3279 }
3280
3281 cc_library {
3282 name: "libboth",
3283 defaults: ["defaults"],
3284 }
3285
3286 cc_binary {
3287 name: "binary",
3288 defaults: ["defaults"],
3289 }`)
3290
3291 pathsToBase := func(paths android.Paths) []string {
3292 var ret []string
3293 for _, p := range paths {
3294 ret = append(ret, p.Base())
3295 }
3296 return ret
3297 }
3298
Colin Cross7113d202019-11-20 16:39:12 -08003299 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003300 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3301 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3302 }
Colin Cross7113d202019-11-20 16:39:12 -08003303 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003304 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3305 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3306 }
Colin Cross7113d202019-11-20 16:39:12 -08003307 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003308 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3309 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3310 }
3311
Colin Cross7113d202019-11-20 16:39:12 -08003312 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003313 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3314 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3315 }
Colin Cross7113d202019-11-20 16:39:12 -08003316 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003317 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3318 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3319 }
3320}
Colin Crosseabaedd2020-02-06 17:01:55 -08003321
3322func TestProductVariableDefaults(t *testing.T) {
3323 bp := `
3324 cc_defaults {
3325 name: "libfoo_defaults",
3326 srcs: ["foo.c"],
3327 cppflags: ["-DFOO"],
3328 product_variables: {
3329 debuggable: {
3330 cppflags: ["-DBAR"],
3331 },
3332 },
3333 }
3334
3335 cc_library {
3336 name: "libfoo",
3337 defaults: ["libfoo_defaults"],
3338 }
3339 `
3340
3341 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3342 config.TestProductVariables.Debuggable = BoolPtr(true)
3343
3344 ctx := CreateTestContext()
3345 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
3346 ctx.BottomUp("variable", android.VariableMutator).Parallel()
3347 })
3348 ctx.Register(config)
3349
3350 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3351 android.FailIfErrored(t, errs)
3352 _, errs = ctx.PrepareBuildActions(config)
3353 android.FailIfErrored(t, errs)
3354
3355 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
3356 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
3357 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
3358 }
3359}