blob: 0a25b5e02f2d08c3978dd8e03df1260f6f2e6530 [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
433func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800434 bp := `
Jooyung Han2216fb12019-11-06 16:46:15 +0900435 vndk_libraries_txt {
436 name: "llndk.libraries.txt",
Colin Cross98be1bb2019-12-13 20:41:13 -0800437 }`
438 config := TestConfig(buildDir, android.Android, nil, bp, nil)
439 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
440 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
441 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900442
443 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900444 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900445 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900446}
447
448func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800449 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900450 cc_library {
451 name: "libvndk",
452 vendor_available: true,
453 vndk: {
454 enabled: true,
455 },
456 nocrt: true,
457 }
458
459 cc_library {
460 name: "libvndk_sp",
461 vendor_available: true,
462 vndk: {
463 enabled: true,
464 support_system_process: true,
465 },
466 nocrt: true,
467 }
468
469 cc_library {
470 name: "libvndk2",
471 vendor_available: false,
472 vndk: {
473 enabled: true,
474 },
475 nocrt: true,
476 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900477
478 vndk_libraries_txt {
479 name: "vndkcorevariant.libraries.txt",
480 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800481 `
482
483 config := TestConfig(buildDir, android.Android, nil, bp, nil)
484 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
485 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
486 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
487
488 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
489
490 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900491
Jooyung Han2216fb12019-11-06 16:46:15 +0900492 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900493}
494
495func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900496 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900497 cc_library {
498 name: "libvndk",
499 vendor_available: true,
500 vndk: {
501 enabled: true,
502 },
503 nocrt: true,
504 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900505 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900506
507 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
508 "LLNDK: libc.so",
509 "LLNDK: libdl.so",
510 "LLNDK: libft2.so",
511 "LLNDK: libm.so",
512 "VNDK-SP: libc++.so",
513 "VNDK-core: libvndk.so",
514 "VNDK-private: libft2.so",
515 })
Logan Chienf3511742017-10-31 18:04:35 +0800516}
517
Logan Chiend3c59a22018-03-29 14:08:15 +0800518func TestVndkDepError(t *testing.T) {
519 // Check whether an error is emitted when a VNDK lib depends on a system lib.
520 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
521 cc_library {
522 name: "libvndk",
523 vendor_available: true,
524 vndk: {
525 enabled: true,
526 },
527 shared_libs: ["libfwk"], // Cause error
528 nocrt: true,
529 }
530
531 cc_library {
532 name: "libfwk",
533 nocrt: true,
534 }
535 `)
536
537 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
538 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
539 cc_library {
540 name: "libvndk",
541 vendor_available: true,
542 vndk: {
543 enabled: true,
544 },
545 shared_libs: ["libvendor"], // Cause error
546 nocrt: true,
547 }
548
549 cc_library {
550 name: "libvendor",
551 vendor: true,
552 nocrt: true,
553 }
554 `)
555
556 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
557 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
558 cc_library {
559 name: "libvndk_sp",
560 vendor_available: true,
561 vndk: {
562 enabled: true,
563 support_system_process: true,
564 },
565 shared_libs: ["libfwk"], // Cause error
566 nocrt: true,
567 }
568
569 cc_library {
570 name: "libfwk",
571 nocrt: true,
572 }
573 `)
574
575 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
576 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
577 cc_library {
578 name: "libvndk_sp",
579 vendor_available: true,
580 vndk: {
581 enabled: true,
582 support_system_process: true,
583 },
584 shared_libs: ["libvendor"], // Cause error
585 nocrt: true,
586 }
587
588 cc_library {
589 name: "libvendor",
590 vendor: true,
591 nocrt: true,
592 }
593 `)
594
595 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
596 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
597 cc_library {
598 name: "libvndk_sp",
599 vendor_available: true,
600 vndk: {
601 enabled: true,
602 support_system_process: true,
603 },
604 shared_libs: ["libvndk"], // Cause error
605 nocrt: true,
606 }
607
608 cc_library {
609 name: "libvndk",
610 vendor_available: true,
611 vndk: {
612 enabled: true,
613 },
614 nocrt: true,
615 }
616 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900617
618 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
619 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
620 cc_library {
621 name: "libvndk",
622 vendor_available: true,
623 vndk: {
624 enabled: true,
625 },
626 shared_libs: ["libnonvndk"],
627 nocrt: true,
628 }
629
630 cc_library {
631 name: "libnonvndk",
632 vendor_available: true,
633 nocrt: true,
634 }
635 `)
636
637 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
638 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
639 cc_library {
640 name: "libvndkprivate",
641 vendor_available: false,
642 vndk: {
643 enabled: true,
644 },
645 shared_libs: ["libnonvndk"],
646 nocrt: true,
647 }
648
649 cc_library {
650 name: "libnonvndk",
651 vendor_available: true,
652 nocrt: true,
653 }
654 `)
655
656 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
657 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
658 cc_library {
659 name: "libvndksp",
660 vendor_available: true,
661 vndk: {
662 enabled: true,
663 support_system_process: true,
664 },
665 shared_libs: ["libnonvndk"],
666 nocrt: true,
667 }
668
669 cc_library {
670 name: "libnonvndk",
671 vendor_available: true,
672 nocrt: true,
673 }
674 `)
675
676 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
677 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
678 cc_library {
679 name: "libvndkspprivate",
680 vendor_available: false,
681 vndk: {
682 enabled: true,
683 support_system_process: true,
684 },
685 shared_libs: ["libnonvndk"],
686 nocrt: true,
687 }
688
689 cc_library {
690 name: "libnonvndk",
691 vendor_available: true,
692 nocrt: true,
693 }
694 `)
695}
696
697func TestDoubleLoadbleDep(t *testing.T) {
698 // okay to link : LLNDK -> double_loadable VNDK
699 testCc(t, `
700 cc_library {
701 name: "libllndk",
702 shared_libs: ["libdoubleloadable"],
703 }
704
705 llndk_library {
706 name: "libllndk",
707 symbol_file: "",
708 }
709
710 cc_library {
711 name: "libdoubleloadable",
712 vendor_available: true,
713 vndk: {
714 enabled: true,
715 },
716 double_loadable: true,
717 }
718 `)
719 // okay to link : LLNDK -> VNDK-SP
720 testCc(t, `
721 cc_library {
722 name: "libllndk",
723 shared_libs: ["libvndksp"],
724 }
725
726 llndk_library {
727 name: "libllndk",
728 symbol_file: "",
729 }
730
731 cc_library {
732 name: "libvndksp",
733 vendor_available: true,
734 vndk: {
735 enabled: true,
736 support_system_process: true,
737 },
738 }
739 `)
740 // okay to link : double_loadable -> double_loadable
741 testCc(t, `
742 cc_library {
743 name: "libdoubleloadable1",
744 shared_libs: ["libdoubleloadable2"],
745 vendor_available: true,
746 double_loadable: true,
747 }
748
749 cc_library {
750 name: "libdoubleloadable2",
751 vendor_available: true,
752 double_loadable: true,
753 }
754 `)
755 // okay to link : double_loadable VNDK -> double_loadable VNDK private
756 testCc(t, `
757 cc_library {
758 name: "libdoubleloadable",
759 vendor_available: true,
760 vndk: {
761 enabled: true,
762 },
763 double_loadable: true,
764 shared_libs: ["libnondoubleloadable"],
765 }
766
767 cc_library {
768 name: "libnondoubleloadable",
769 vendor_available: false,
770 vndk: {
771 enabled: true,
772 },
773 double_loadable: true,
774 }
775 `)
776 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
777 testCc(t, `
778 cc_library {
779 name: "libllndk",
780 shared_libs: ["libcoreonly"],
781 }
782
783 llndk_library {
784 name: "libllndk",
785 symbol_file: "",
786 }
787
788 cc_library {
789 name: "libcoreonly",
790 shared_libs: ["libvendoravailable"],
791 }
792
793 // indirect dependency of LLNDK
794 cc_library {
795 name: "libvendoravailable",
796 vendor_available: true,
797 double_loadable: true,
798 }
799 `)
800}
801
Inseob Kim8471cda2019-11-15 09:59:12 +0900802func TestVendorSnapshot(t *testing.T) {
803 bp := `
804 cc_library {
805 name: "libvndk",
806 vendor_available: true,
807 vndk: {
808 enabled: true,
809 },
810 nocrt: true,
811 }
812
813 cc_library {
814 name: "libvendor",
815 vendor: true,
816 nocrt: true,
817 }
818
819 cc_library {
820 name: "libvendor_available",
821 vendor_available: true,
822 nocrt: true,
823 }
824
825 cc_library_headers {
826 name: "libvendor_headers",
827 vendor_available: true,
828 nocrt: true,
829 }
830
831 cc_binary {
832 name: "vendor_bin",
833 vendor: true,
834 nocrt: true,
835 }
836
837 cc_binary {
838 name: "vendor_available_bin",
839 vendor_available: true,
840 nocrt: true,
841 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900842
843 toolchain_library {
844 name: "libb",
845 vendor_available: true,
846 src: "libb.a",
847 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900848`
849 config := TestConfig(buildDir, android.Android, nil, bp, nil)
850 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
851 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
852 ctx := testCcWithConfig(t, config)
853
854 // Check Vendor snapshot output.
855
856 snapshotDir := "vendor-snapshot"
857 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +0900858 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
859
860 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +0900861
862 for _, arch := range [][]string{
863 []string{"arm64", "armv8-a"},
864 []string{"arm", "armv7-a-neon"},
865 } {
866 archType := arch[0]
867 archVariant := arch[1]
868 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
869
870 // For shared libraries, only non-VNDK vendor_available modules are captured
871 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
872 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +0900873 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
874 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
875 jsonFiles = append(jsonFiles,
876 filepath.Join(sharedDir, "libvendor.so.json"),
877 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +0900878
879 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
880 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
881 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +0900882 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
883 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
884 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
885 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
886 jsonFiles = append(jsonFiles,
887 filepath.Join(staticDir, "libb.a.json"),
888 filepath.Join(staticDir, "libvndk.a.json"),
889 filepath.Join(staticDir, "libvendor.a.json"),
890 filepath.Join(staticDir, "libvendor_available.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +0900891
Inseob Kim7f283f42020-06-01 21:53:49 +0900892 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +0900893 if archType == "arm64" {
894 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
895 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +0900896 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
897 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
898 jsonFiles = append(jsonFiles,
899 filepath.Join(binaryDir, "vendor_bin.json"),
900 filepath.Join(binaryDir, "vendor_available_bin.json"))
901 }
902
903 // For header libraries, all vendor:true and vendor_available modules are captured.
904 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
905 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
906 }
907
908 for _, jsonFile := range jsonFiles {
909 // verify all json files exist
910 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
911 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +0900912 }
913 }
914}
915
Jooyung Hana70f0672019-01-18 15:20:43 +0900916func TestDoubleLoadableDepError(t *testing.T) {
917 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
918 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
919 cc_library {
920 name: "libllndk",
921 shared_libs: ["libnondoubleloadable"],
922 }
923
924 llndk_library {
925 name: "libllndk",
926 symbol_file: "",
927 }
928
929 cc_library {
930 name: "libnondoubleloadable",
931 vendor_available: true,
932 vndk: {
933 enabled: true,
934 },
935 }
936 `)
937
938 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
939 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
940 cc_library {
941 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -0700942 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900943 shared_libs: ["libnondoubleloadable"],
944 }
945
946 llndk_library {
947 name: "libllndk",
948 symbol_file: "",
949 }
950
951 cc_library {
952 name: "libnondoubleloadable",
953 vendor_available: true,
954 }
955 `)
956
957 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
958 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
959 cc_library {
960 name: "libdoubleloadable",
961 vendor_available: true,
962 double_loadable: true,
963 shared_libs: ["libnondoubleloadable"],
964 }
965
966 cc_library {
967 name: "libnondoubleloadable",
968 vendor_available: true,
969 }
970 `)
971
972 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
973 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
974 cc_library {
975 name: "libdoubleloadable",
976 vendor_available: true,
977 double_loadable: true,
978 shared_libs: ["libnondoubleloadable"],
979 }
980
981 cc_library {
982 name: "libnondoubleloadable",
983 vendor_available: true,
984 vndk: {
985 enabled: true,
986 },
987 }
988 `)
989
990 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
991 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
992 cc_library {
993 name: "libdoubleloadable",
994 vendor_available: true,
995 vndk: {
996 enabled: true,
997 },
998 double_loadable: true,
999 shared_libs: ["libnondoubleloadable"],
1000 }
1001
1002 cc_library {
1003 name: "libnondoubleloadable",
1004 vendor_available: false,
1005 vndk: {
1006 enabled: true,
1007 },
1008 }
1009 `)
1010
1011 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1012 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1013 cc_library {
1014 name: "libllndk",
1015 shared_libs: ["libcoreonly"],
1016 }
1017
1018 llndk_library {
1019 name: "libllndk",
1020 symbol_file: "",
1021 }
1022
1023 cc_library {
1024 name: "libcoreonly",
1025 shared_libs: ["libvendoravailable"],
1026 }
1027
1028 // indirect dependency of LLNDK
1029 cc_library {
1030 name: "libvendoravailable",
1031 vendor_available: true,
1032 }
1033 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001034}
1035
Logan Chienf3511742017-10-31 18:04:35 +08001036func TestVndkExt(t *testing.T) {
1037 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001038 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001039 cc_library {
1040 name: "libvndk",
1041 vendor_available: true,
1042 vndk: {
1043 enabled: true,
1044 },
1045 nocrt: true,
1046 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001047 cc_library {
1048 name: "libvndk2",
1049 vendor_available: true,
1050 vndk: {
1051 enabled: true,
1052 },
1053 target: {
1054 vendor: {
1055 suffix: "-suffix",
1056 },
1057 },
1058 nocrt: true,
1059 }
Logan Chienf3511742017-10-31 18:04:35 +08001060
1061 cc_library {
1062 name: "libvndk_ext",
1063 vendor: true,
1064 vndk: {
1065 enabled: true,
1066 extends: "libvndk",
1067 },
1068 nocrt: true,
1069 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001070
1071 cc_library {
1072 name: "libvndk2_ext",
1073 vendor: true,
1074 vndk: {
1075 enabled: true,
1076 extends: "libvndk2",
1077 },
1078 nocrt: true,
1079 }
Logan Chienf3511742017-10-31 18:04:35 +08001080
Justin Yun0ecf0b22020-02-28 15:07:59 +09001081 cc_library {
1082 name: "libvndk_ext_product",
1083 product_specific: true,
1084 vndk: {
1085 enabled: true,
1086 extends: "libvndk",
1087 },
1088 nocrt: true,
1089 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001090
Justin Yun0ecf0b22020-02-28 15:07:59 +09001091 cc_library {
1092 name: "libvndk2_ext_product",
1093 product_specific: true,
1094 vndk: {
1095 enabled: true,
1096 extends: "libvndk2",
1097 },
1098 nocrt: true,
1099 }
1100 `
1101 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1102 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1103 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1104 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1105
1106 ctx := testCcWithConfig(t, config)
1107
1108 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1109 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1110
1111 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1112 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1113
1114 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1115 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001116}
1117
Logan Chiend3c59a22018-03-29 14:08:15 +08001118func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001119 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1120 ctx := testCcNoVndk(t, `
1121 cc_library {
1122 name: "libvndk",
1123 vendor_available: true,
1124 vndk: {
1125 enabled: true,
1126 },
1127 nocrt: true,
1128 }
1129
1130 cc_library {
1131 name: "libvndk_ext",
1132 vendor: true,
1133 vndk: {
1134 enabled: true,
1135 extends: "libvndk",
1136 },
1137 nocrt: true,
1138 }
1139 `)
1140
1141 // Ensures that the core variant of "libvndk_ext" can be found.
1142 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1143 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1144 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1145 }
1146}
1147
Justin Yun0ecf0b22020-02-28 15:07:59 +09001148func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1149 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
1150 ctx := testCc(t, `
1151 cc_library {
1152 name: "libvndk",
1153 vendor_available: true,
1154 vndk: {
1155 enabled: true,
1156 },
1157 nocrt: true,
1158 }
1159
1160 cc_library {
1161 name: "libvndk_ext_product",
1162 product_specific: true,
1163 vndk: {
1164 enabled: true,
1165 extends: "libvndk",
1166 },
1167 nocrt: true,
1168 }
1169 `)
1170
1171 // Ensures that the core variant of "libvndk_ext_product" can be found.
1172 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1173 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1174 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1175 }
1176}
1177
Logan Chienf3511742017-10-31 18:04:35 +08001178func TestVndkExtError(t *testing.T) {
1179 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001180 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001181 cc_library {
1182 name: "libvndk",
1183 vendor_available: true,
1184 vndk: {
1185 enabled: true,
1186 },
1187 nocrt: true,
1188 }
1189
1190 cc_library {
1191 name: "libvndk_ext",
1192 vndk: {
1193 enabled: true,
1194 extends: "libvndk",
1195 },
1196 nocrt: true,
1197 }
1198 `)
1199
1200 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1201 cc_library {
1202 name: "libvndk",
1203 vendor_available: true,
1204 vndk: {
1205 enabled: true,
1206 },
1207 nocrt: true,
1208 }
1209
1210 cc_library {
1211 name: "libvndk_ext",
1212 vendor: true,
1213 vndk: {
1214 enabled: true,
1215 },
1216 nocrt: true,
1217 }
1218 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001219
1220 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1221 cc_library {
1222 name: "libvndk",
1223 vendor_available: true,
1224 vndk: {
1225 enabled: true,
1226 },
1227 nocrt: true,
1228 }
1229
1230 cc_library {
1231 name: "libvndk_ext_product",
1232 product_specific: true,
1233 vndk: {
1234 enabled: true,
1235 },
1236 nocrt: true,
1237 }
1238 `)
1239
1240 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1241 cc_library {
1242 name: "libvndk",
1243 vendor_available: true,
1244 vndk: {
1245 enabled: true,
1246 },
1247 nocrt: true,
1248 }
1249
1250 cc_library {
1251 name: "libvndk_ext_product",
1252 product_specific: true,
1253 vendor_available: true,
1254 vndk: {
1255 enabled: true,
1256 extends: "libvndk",
1257 },
1258 nocrt: true,
1259 }
1260 `)
Logan Chienf3511742017-10-31 18:04:35 +08001261}
1262
1263func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1264 // This test ensures an error is emitted for inconsistent support_system_process.
1265 testCcError(t, "module \".*\" with mismatched support_system_process", `
1266 cc_library {
1267 name: "libvndk",
1268 vendor_available: true,
1269 vndk: {
1270 enabled: true,
1271 },
1272 nocrt: true,
1273 }
1274
1275 cc_library {
1276 name: "libvndk_sp_ext",
1277 vendor: true,
1278 vndk: {
1279 enabled: true,
1280 extends: "libvndk",
1281 support_system_process: true,
1282 },
1283 nocrt: true,
1284 }
1285 `)
1286
1287 testCcError(t, "module \".*\" with mismatched support_system_process", `
1288 cc_library {
1289 name: "libvndk_sp",
1290 vendor_available: true,
1291 vndk: {
1292 enabled: true,
1293 support_system_process: true,
1294 },
1295 nocrt: true,
1296 }
1297
1298 cc_library {
1299 name: "libvndk_ext",
1300 vendor: true,
1301 vndk: {
1302 enabled: true,
1303 extends: "libvndk_sp",
1304 },
1305 nocrt: true,
1306 }
1307 `)
1308}
1309
1310func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001311 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08001312 // with `vendor_available: false`.
1313 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1314 cc_library {
1315 name: "libvndk",
1316 vendor_available: false,
1317 vndk: {
1318 enabled: true,
1319 },
1320 nocrt: true,
1321 }
1322
1323 cc_library {
1324 name: "libvndk_ext",
1325 vendor: true,
1326 vndk: {
1327 enabled: true,
1328 extends: "libvndk",
1329 },
1330 nocrt: true,
1331 }
1332 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001333
1334 testCcErrorProductVndk(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1335 cc_library {
1336 name: "libvndk",
1337 vendor_available: false,
1338 vndk: {
1339 enabled: true,
1340 },
1341 nocrt: true,
1342 }
1343
1344 cc_library {
1345 name: "libvndk_ext_product",
1346 product_specific: true,
1347 vndk: {
1348 enabled: true,
1349 extends: "libvndk",
1350 },
1351 nocrt: true,
1352 }
1353 `)
Logan Chienf3511742017-10-31 18:04:35 +08001354}
1355
Logan Chiend3c59a22018-03-29 14:08:15 +08001356func TestVendorModuleUseVndkExt(t *testing.T) {
1357 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001358 testCc(t, `
1359 cc_library {
1360 name: "libvndk",
1361 vendor_available: true,
1362 vndk: {
1363 enabled: true,
1364 },
1365 nocrt: true,
1366 }
1367
1368 cc_library {
1369 name: "libvndk_ext",
1370 vendor: true,
1371 vndk: {
1372 enabled: true,
1373 extends: "libvndk",
1374 },
1375 nocrt: true,
1376 }
1377
1378 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001379 name: "libvndk_sp",
1380 vendor_available: true,
1381 vndk: {
1382 enabled: true,
1383 support_system_process: true,
1384 },
1385 nocrt: true,
1386 }
1387
1388 cc_library {
1389 name: "libvndk_sp_ext",
1390 vendor: true,
1391 vndk: {
1392 enabled: true,
1393 extends: "libvndk_sp",
1394 support_system_process: true,
1395 },
1396 nocrt: true,
1397 }
1398
1399 cc_library {
1400 name: "libvendor",
1401 vendor: true,
1402 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1403 nocrt: true,
1404 }
1405 `)
1406}
1407
Logan Chiend3c59a22018-03-29 14:08:15 +08001408func TestVndkExtUseVendorLib(t *testing.T) {
1409 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001410 testCc(t, `
1411 cc_library {
1412 name: "libvndk",
1413 vendor_available: true,
1414 vndk: {
1415 enabled: true,
1416 },
1417 nocrt: true,
1418 }
1419
1420 cc_library {
1421 name: "libvndk_ext",
1422 vendor: true,
1423 vndk: {
1424 enabled: true,
1425 extends: "libvndk",
1426 },
1427 shared_libs: ["libvendor"],
1428 nocrt: true,
1429 }
1430
1431 cc_library {
1432 name: "libvendor",
1433 vendor: true,
1434 nocrt: true,
1435 }
1436 `)
Logan Chienf3511742017-10-31 18:04:35 +08001437
Logan Chiend3c59a22018-03-29 14:08:15 +08001438 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1439 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001440 cc_library {
1441 name: "libvndk_sp",
1442 vendor_available: true,
1443 vndk: {
1444 enabled: true,
1445 support_system_process: true,
1446 },
1447 nocrt: true,
1448 }
1449
1450 cc_library {
1451 name: "libvndk_sp_ext",
1452 vendor: true,
1453 vndk: {
1454 enabled: true,
1455 extends: "libvndk_sp",
1456 support_system_process: true,
1457 },
1458 shared_libs: ["libvendor"], // Cause an error
1459 nocrt: true,
1460 }
1461
1462 cc_library {
1463 name: "libvendor",
1464 vendor: true,
1465 nocrt: true,
1466 }
1467 `)
1468}
1469
Justin Yun0ecf0b22020-02-28 15:07:59 +09001470func TestProductVndkExtDependency(t *testing.T) {
1471 bp := `
1472 cc_library {
1473 name: "libvndk",
1474 vendor_available: true,
1475 vndk: {
1476 enabled: true,
1477 },
1478 nocrt: true,
1479 }
1480
1481 cc_library {
1482 name: "libvndk_ext_product",
1483 product_specific: true,
1484 vndk: {
1485 enabled: true,
1486 extends: "libvndk",
1487 },
1488 shared_libs: ["libproduct_for_vndklibs"],
1489 nocrt: true,
1490 }
1491
1492 cc_library {
1493 name: "libvndk_sp",
1494 vendor_available: true,
1495 vndk: {
1496 enabled: true,
1497 support_system_process: true,
1498 },
1499 nocrt: true,
1500 }
1501
1502 cc_library {
1503 name: "libvndk_sp_ext_product",
1504 product_specific: true,
1505 vndk: {
1506 enabled: true,
1507 extends: "libvndk_sp",
1508 support_system_process: true,
1509 },
1510 shared_libs: ["libproduct_for_vndklibs"],
1511 nocrt: true,
1512 }
1513
1514 cc_library {
1515 name: "libproduct",
1516 product_specific: true,
1517 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1518 nocrt: true,
1519 }
1520
1521 cc_library {
1522 name: "libproduct_for_vndklibs",
1523 product_specific: true,
1524 nocrt: true,
1525 }
1526 `
1527 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1528 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1529 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1530 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1531
1532 testCcWithConfig(t, config)
1533}
1534
Logan Chiend3c59a22018-03-29 14:08:15 +08001535func TestVndkSpExtUseVndkError(t *testing.T) {
1536 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1537 // library.
1538 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1539 cc_library {
1540 name: "libvndk",
1541 vendor_available: true,
1542 vndk: {
1543 enabled: true,
1544 },
1545 nocrt: true,
1546 }
1547
1548 cc_library {
1549 name: "libvndk_sp",
1550 vendor_available: true,
1551 vndk: {
1552 enabled: true,
1553 support_system_process: true,
1554 },
1555 nocrt: true,
1556 }
1557
1558 cc_library {
1559 name: "libvndk_sp_ext",
1560 vendor: true,
1561 vndk: {
1562 enabled: true,
1563 extends: "libvndk_sp",
1564 support_system_process: true,
1565 },
1566 shared_libs: ["libvndk"], // Cause an error
1567 nocrt: true,
1568 }
1569 `)
1570
1571 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1572 // library.
1573 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1574 cc_library {
1575 name: "libvndk",
1576 vendor_available: true,
1577 vndk: {
1578 enabled: true,
1579 },
1580 nocrt: true,
1581 }
1582
1583 cc_library {
1584 name: "libvndk_ext",
1585 vendor: true,
1586 vndk: {
1587 enabled: true,
1588 extends: "libvndk",
1589 },
1590 nocrt: true,
1591 }
1592
1593 cc_library {
1594 name: "libvndk_sp",
1595 vendor_available: true,
1596 vndk: {
1597 enabled: true,
1598 support_system_process: true,
1599 },
1600 nocrt: true,
1601 }
1602
1603 cc_library {
1604 name: "libvndk_sp_ext",
1605 vendor: true,
1606 vndk: {
1607 enabled: true,
1608 extends: "libvndk_sp",
1609 support_system_process: true,
1610 },
1611 shared_libs: ["libvndk_ext"], // Cause an error
1612 nocrt: true,
1613 }
1614 `)
1615}
1616
1617func TestVndkUseVndkExtError(t *testing.T) {
1618 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1619 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001620 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1621 cc_library {
1622 name: "libvndk",
1623 vendor_available: true,
1624 vndk: {
1625 enabled: true,
1626 },
1627 nocrt: true,
1628 }
1629
1630 cc_library {
1631 name: "libvndk_ext",
1632 vendor: true,
1633 vndk: {
1634 enabled: true,
1635 extends: "libvndk",
1636 },
1637 nocrt: true,
1638 }
1639
1640 cc_library {
1641 name: "libvndk2",
1642 vendor_available: true,
1643 vndk: {
1644 enabled: true,
1645 },
1646 shared_libs: ["libvndk_ext"],
1647 nocrt: true,
1648 }
1649 `)
1650
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001651 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001652 cc_library {
1653 name: "libvndk",
1654 vendor_available: true,
1655 vndk: {
1656 enabled: true,
1657 },
1658 nocrt: true,
1659 }
1660
1661 cc_library {
1662 name: "libvndk_ext",
1663 vendor: true,
1664 vndk: {
1665 enabled: true,
1666 extends: "libvndk",
1667 },
1668 nocrt: true,
1669 }
1670
1671 cc_library {
1672 name: "libvndk2",
1673 vendor_available: true,
1674 vndk: {
1675 enabled: true,
1676 },
1677 target: {
1678 vendor: {
1679 shared_libs: ["libvndk_ext"],
1680 },
1681 },
1682 nocrt: true,
1683 }
1684 `)
1685
1686 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1687 cc_library {
1688 name: "libvndk_sp",
1689 vendor_available: true,
1690 vndk: {
1691 enabled: true,
1692 support_system_process: true,
1693 },
1694 nocrt: true,
1695 }
1696
1697 cc_library {
1698 name: "libvndk_sp_ext",
1699 vendor: true,
1700 vndk: {
1701 enabled: true,
1702 extends: "libvndk_sp",
1703 support_system_process: true,
1704 },
1705 nocrt: true,
1706 }
1707
1708 cc_library {
1709 name: "libvndk_sp_2",
1710 vendor_available: true,
1711 vndk: {
1712 enabled: true,
1713 support_system_process: true,
1714 },
1715 shared_libs: ["libvndk_sp_ext"],
1716 nocrt: true,
1717 }
1718 `)
1719
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001720 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001721 cc_library {
1722 name: "libvndk_sp",
1723 vendor_available: true,
1724 vndk: {
1725 enabled: true,
1726 },
1727 nocrt: true,
1728 }
1729
1730 cc_library {
1731 name: "libvndk_sp_ext",
1732 vendor: true,
1733 vndk: {
1734 enabled: true,
1735 extends: "libvndk_sp",
1736 },
1737 nocrt: true,
1738 }
1739
1740 cc_library {
1741 name: "libvndk_sp2",
1742 vendor_available: true,
1743 vndk: {
1744 enabled: true,
1745 },
1746 target: {
1747 vendor: {
1748 shared_libs: ["libvndk_sp_ext"],
1749 },
1750 },
1751 nocrt: true,
1752 }
1753 `)
1754}
1755
Justin Yun5f7f7e82019-11-18 19:52:14 +09001756func TestEnforceProductVndkVersion(t *testing.T) {
1757 bp := `
1758 cc_library {
1759 name: "libllndk",
1760 }
1761 llndk_library {
1762 name: "libllndk",
1763 symbol_file: "",
1764 }
1765 cc_library {
1766 name: "libvndk",
1767 vendor_available: true,
1768 vndk: {
1769 enabled: true,
1770 },
1771 nocrt: true,
1772 }
1773 cc_library {
1774 name: "libvndk_sp",
1775 vendor_available: true,
1776 vndk: {
1777 enabled: true,
1778 support_system_process: true,
1779 },
1780 nocrt: true,
1781 }
1782 cc_library {
1783 name: "libva",
1784 vendor_available: true,
1785 nocrt: true,
1786 }
1787 cc_library {
1788 name: "libproduct_va",
1789 product_specific: true,
1790 vendor_available: true,
1791 nocrt: true,
1792 }
1793 cc_library {
1794 name: "libprod",
1795 product_specific: true,
1796 shared_libs: [
1797 "libllndk",
1798 "libvndk",
1799 "libvndk_sp",
1800 "libva",
1801 "libproduct_va",
1802 ],
1803 nocrt: true,
1804 }
1805 cc_library {
1806 name: "libvendor",
1807 vendor: true,
1808 shared_libs: [
1809 "libllndk",
1810 "libvndk",
1811 "libvndk_sp",
1812 "libva",
1813 "libproduct_va",
1814 ],
1815 nocrt: true,
1816 }
1817 `
1818
1819 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1820 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1821 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1822 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1823
1824 ctx := testCcWithConfig(t, config)
1825
Justin Yun0ecf0b22020-02-28 15:07:59 +09001826 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "", productVariant)
1827 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "", productVariant)
Justin Yun5f7f7e82019-11-18 19:52:14 +09001828}
1829
1830func TestEnforceProductVndkVersionErrors(t *testing.T) {
1831 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1832 cc_library {
1833 name: "libprod",
1834 product_specific: true,
1835 shared_libs: [
1836 "libvendor",
1837 ],
1838 nocrt: true,
1839 }
1840 cc_library {
1841 name: "libvendor",
1842 vendor: true,
1843 nocrt: true,
1844 }
1845 `)
1846 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1847 cc_library {
1848 name: "libprod",
1849 product_specific: true,
1850 shared_libs: [
1851 "libsystem",
1852 ],
1853 nocrt: true,
1854 }
1855 cc_library {
1856 name: "libsystem",
1857 nocrt: true,
1858 }
1859 `)
1860 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
1861 cc_library {
1862 name: "libprod",
1863 product_specific: true,
1864 shared_libs: [
1865 "libvndk_private",
1866 ],
1867 nocrt: true,
1868 }
1869 cc_library {
1870 name: "libvndk_private",
1871 vendor_available: false,
1872 vndk: {
1873 enabled: true,
1874 },
1875 nocrt: true,
1876 }
1877 `)
1878 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1879 cc_library {
1880 name: "libprod",
1881 product_specific: true,
1882 shared_libs: [
1883 "libsystem_ext",
1884 ],
1885 nocrt: true,
1886 }
1887 cc_library {
1888 name: "libsystem_ext",
1889 system_ext_specific: true,
1890 nocrt: true,
1891 }
1892 `)
1893 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
1894 cc_library {
1895 name: "libsystem",
1896 shared_libs: [
1897 "libproduct_va",
1898 ],
1899 nocrt: true,
1900 }
1901 cc_library {
1902 name: "libproduct_va",
1903 product_specific: true,
1904 vendor_available: true,
1905 nocrt: true,
1906 }
1907 `)
1908}
1909
Jooyung Han38002912019-05-16 04:01:54 +09001910func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001911 bp := `
1912 cc_library {
1913 name: "libvndk",
1914 vendor_available: true,
1915 vndk: {
1916 enabled: true,
1917 },
1918 }
1919 cc_library {
1920 name: "libvndksp",
1921 vendor_available: true,
1922 vndk: {
1923 enabled: true,
1924 support_system_process: true,
1925 },
1926 }
1927 cc_library {
1928 name: "libvndkprivate",
1929 vendor_available: false,
1930 vndk: {
1931 enabled: true,
1932 },
1933 }
1934 cc_library {
1935 name: "libvendor",
1936 vendor: true,
1937 }
1938 cc_library {
1939 name: "libvndkext",
1940 vendor: true,
1941 vndk: {
1942 enabled: true,
1943 extends: "libvndk",
1944 },
1945 }
1946 vndk_prebuilt_shared {
1947 name: "prevndk",
1948 version: "27",
1949 target_arch: "arm",
1950 binder32bit: true,
1951 vendor_available: true,
1952 vndk: {
1953 enabled: true,
1954 },
1955 arch: {
1956 arm: {
1957 srcs: ["liba.so"],
1958 },
1959 },
1960 }
1961 cc_library {
1962 name: "libllndk",
1963 }
1964 llndk_library {
1965 name: "libllndk",
1966 symbol_file: "",
1967 }
1968 cc_library {
1969 name: "libllndkprivate",
1970 }
1971 llndk_library {
1972 name: "libllndkprivate",
1973 vendor_available: false,
1974 symbol_file: "",
1975 }`
1976
1977 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09001978 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1979 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1980 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08001981 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09001982
Jooyung Han0302a842019-10-30 18:43:49 +09001983 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09001984 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09001985 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09001986 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09001987 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09001988 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09001989 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09001990 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09001991
Colin Crossfb0c16e2019-11-20 17:12:35 -08001992 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09001993
Jooyung Han38002912019-05-16 04:01:54 +09001994 tests := []struct {
1995 variant string
1996 name string
1997 expected string
1998 }{
1999 {vendorVariant, "libvndk", "native:vndk"},
2000 {vendorVariant, "libvndksp", "native:vndk"},
2001 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2002 {vendorVariant, "libvendor", "native:vendor"},
2003 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09002004 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002005 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002006 {coreVariant, "libvndk", "native:platform"},
2007 {coreVariant, "libvndkprivate", "native:platform"},
2008 {coreVariant, "libllndk", "native:platform"},
2009 }
2010 for _, test := range tests {
2011 t.Run(test.name, func(t *testing.T) {
2012 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2013 assertString(t, module.makeLinkType, test.expected)
2014 })
2015 }
2016}
2017
Colin Cross0af4b842015-04-30 16:36:18 -07002018var (
2019 str11 = "01234567891"
2020 str10 = str11[:10]
2021 str9 = str11[:9]
2022 str5 = str11[:5]
2023 str4 = str11[:4]
2024)
2025
2026var splitListForSizeTestCases = []struct {
2027 in []string
2028 out [][]string
2029 size int
2030}{
2031 {
2032 in: []string{str10},
2033 out: [][]string{{str10}},
2034 size: 10,
2035 },
2036 {
2037 in: []string{str9},
2038 out: [][]string{{str9}},
2039 size: 10,
2040 },
2041 {
2042 in: []string{str5},
2043 out: [][]string{{str5}},
2044 size: 10,
2045 },
2046 {
2047 in: []string{str11},
2048 out: nil,
2049 size: 10,
2050 },
2051 {
2052 in: []string{str10, str10},
2053 out: [][]string{{str10}, {str10}},
2054 size: 10,
2055 },
2056 {
2057 in: []string{str9, str10},
2058 out: [][]string{{str9}, {str10}},
2059 size: 10,
2060 },
2061 {
2062 in: []string{str10, str9},
2063 out: [][]string{{str10}, {str9}},
2064 size: 10,
2065 },
2066 {
2067 in: []string{str5, str4},
2068 out: [][]string{{str5, str4}},
2069 size: 10,
2070 },
2071 {
2072 in: []string{str5, str4, str5},
2073 out: [][]string{{str5, str4}, {str5}},
2074 size: 10,
2075 },
2076 {
2077 in: []string{str5, str4, str5, str4},
2078 out: [][]string{{str5, str4}, {str5, str4}},
2079 size: 10,
2080 },
2081 {
2082 in: []string{str5, str4, str5, str5},
2083 out: [][]string{{str5, str4}, {str5}, {str5}},
2084 size: 10,
2085 },
2086 {
2087 in: []string{str5, str5, str5, str4},
2088 out: [][]string{{str5}, {str5}, {str5, str4}},
2089 size: 10,
2090 },
2091 {
2092 in: []string{str9, str11},
2093 out: nil,
2094 size: 10,
2095 },
2096 {
2097 in: []string{str11, str9},
2098 out: nil,
2099 size: 10,
2100 },
2101}
2102
2103func TestSplitListForSize(t *testing.T) {
2104 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08002105 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07002106
2107 var outStrings [][]string
2108
2109 if len(out) > 0 {
2110 outStrings = make([][]string, len(out))
2111 for i, o := range out {
2112 outStrings[i] = o.Strings()
2113 }
2114 }
2115
2116 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07002117 t.Errorf("incorrect output:")
2118 t.Errorf(" input: %#v", testCase.in)
2119 t.Errorf(" size: %d", testCase.size)
2120 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07002121 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07002122 }
2123 }
2124}
Jeff Gaston294356f2017-09-27 17:05:30 -07002125
2126var staticLinkDepOrderTestCases = []struct {
2127 // This is a string representation of a map[moduleName][]moduleDependency .
2128 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002129 inStatic string
2130
2131 // This is a string representation of a map[moduleName][]moduleDependency .
2132 // It models the dependencies declared in an Android.bp file.
2133 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002134
2135 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2136 // The keys of allOrdered specify which modules we would like to check.
2137 // The values of allOrdered specify the expected result (of the transitive closure of all
2138 // dependencies) for each module to test
2139 allOrdered string
2140
2141 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2142 // The keys of outOrdered specify which modules we would like to check.
2143 // The values of outOrdered specify the expected result (of the ordered linker command line)
2144 // for each module to test.
2145 outOrdered string
2146}{
2147 // Simple tests
2148 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002149 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002150 outOrdered: "",
2151 },
2152 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002153 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002154 outOrdered: "a:",
2155 },
2156 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002157 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002158 outOrdered: "a:b; b:",
2159 },
2160 // Tests of reordering
2161 {
2162 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002163 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002164 outOrdered: "a:b,c,d; b:d; c:d; d:",
2165 },
2166 {
2167 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002168 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002169 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2170 },
2171 {
2172 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002173 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002174 outOrdered: "a:d,b,e,c; d:b; e:c",
2175 },
2176 {
2177 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002178 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002179 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2180 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2181 },
2182 {
2183 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002184 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 -07002185 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2186 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2187 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002188 // shared dependencies
2189 {
2190 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2191 // So, we don't actually have to check that a shared dependency of c will change the order
2192 // of a library that depends statically on b and on c. We only need to check that if c has
2193 // a shared dependency on b, that that shows up in allOrdered.
2194 inShared: "c:b",
2195 allOrdered: "c:b",
2196 outOrdered: "c:",
2197 },
2198 {
2199 // This test doesn't actually include any shared dependencies but it's a reminder of what
2200 // the second phase of the above test would look like
2201 inStatic: "a:b,c; c:b",
2202 allOrdered: "a:c,b; c:b",
2203 outOrdered: "a:c,b; c:b",
2204 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002205 // tiebreakers for when two modules specifying different orderings and there is no dependency
2206 // to dictate an order
2207 {
2208 // 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 -08002209 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002210 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2211 },
2212 {
2213 // 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 -08002214 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 -07002215 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2216 },
2217 // Tests involving duplicate dependencies
2218 {
2219 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002220 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002221 outOrdered: "a:c,b",
2222 },
2223 {
2224 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002225 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002226 outOrdered: "a:d,c,b",
2227 },
2228 // Tests to confirm the nonexistence of infinite loops.
2229 // These cases should never happen, so as long as the test terminates and the
2230 // result is deterministic then that should be fine.
2231 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002232 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002233 outOrdered: "a:a",
2234 },
2235 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002236 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002237 allOrdered: "a:b,c; b:c,a; c:a,b",
2238 outOrdered: "a:b; b:c; c:a",
2239 },
2240 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002241 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002242 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2243 outOrdered: "a:c,b; b:a,c; c:b,a",
2244 },
2245}
2246
2247// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2248func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2249 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2250 strippedText := strings.Replace(text, " ", "", -1)
2251 if len(strippedText) < 1 {
2252 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2253 }
2254 allDeps = make(map[android.Path][]android.Path, 0)
2255
2256 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2257 moduleTexts := strings.Split(strippedText, ";")
2258
2259 outputForModuleName := func(moduleName string) android.Path {
2260 return android.PathForTesting(moduleName)
2261 }
2262
2263 for _, moduleText := range moduleTexts {
2264 // convert from "a:b,c" to ["a", "b,c"]
2265 components := strings.Split(moduleText, ":")
2266 if len(components) != 2 {
2267 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2268 }
2269 moduleName := components[0]
2270 moduleOutput := outputForModuleName(moduleName)
2271 modulesInOrder = append(modulesInOrder, moduleOutput)
2272
2273 depString := components[1]
2274 // convert from "b,c" to ["b", "c"]
2275 depNames := strings.Split(depString, ",")
2276 if len(depString) < 1 {
2277 depNames = []string{}
2278 }
2279 var deps []android.Path
2280 for _, depName := range depNames {
2281 deps = append(deps, outputForModuleName(depName))
2282 }
2283 allDeps[moduleOutput] = deps
2284 }
2285 return modulesInOrder, allDeps
2286}
2287
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002288func TestLinkReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002289 for _, testCase := range staticLinkDepOrderTestCases {
2290 errs := []string{}
2291
2292 // parse testcase
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002293 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)
Jeff Gaston294356f2017-09-27 17:05:30 -07002294 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)
2295 if testCase.allOrdered == "" {
2296 // allow the test case to skip specifying allOrdered
2297 testCase.allOrdered = testCase.outOrdered
2298 }
2299 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002300 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)
Jeff Gaston294356f2017-09-27 17:05:30 -07002301
2302 // For each module whose post-reordered dependencies were specified, validate that
2303 // reordering the inputs produces the expected outputs.
2304 for _, moduleName := range expectedModuleNames {
2305 moduleDeps := givenTransitiveDeps[moduleName]
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002306 givenSharedDeps := givenAllSharedDeps[moduleName]
2307 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -07002308
2309 correctAllOrdered := expectedAllDeps[moduleName]
2310 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {
2311 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002312 "\nin static:%q"+
2313 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002314 "\nmodule: %v"+
2315 "\nexpected: %s"+
2316 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002317 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002318 }
2319
2320 correctOutputDeps := expectedTransitiveDeps[moduleName]
2321 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {
2322 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002323 "\nin static:%q"+
2324 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002325 "\nmodule: %v"+
2326 "\nexpected: %s"+
2327 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002328 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002329 }
2330 }
2331
2332 if len(errs) > 0 {
2333 sort.Strings(errs)
2334 for _, err := range errs {
2335 t.Error(err)
2336 }
2337 }
2338 }
2339}
Logan Chienf3511742017-10-31 18:04:35 +08002340
Jeff Gaston294356f2017-09-27 17:05:30 -07002341func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2342 for _, moduleName := range moduleNames {
2343 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
2344 output := module.outputFile.Path()
2345 paths = append(paths, output)
2346 }
2347 return paths
2348}
2349
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002350func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002351 ctx := testCc(t, `
2352 cc_library {
2353 name: "a",
2354 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002355 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002356 }
2357 cc_library {
2358 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002359 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002360 }
2361 cc_library {
2362 name: "c",
2363 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002364 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002365 }
2366 cc_library {
2367 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002368 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002369 }
2370
2371 `)
2372
Colin Cross7113d202019-11-20 16:39:12 -08002373 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002374 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002375 actual := moduleA.depsInLinkOrder
Jeff Gaston294356f2017-09-27 17:05:30 -07002376 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})
2377
2378 if !reflect.DeepEqual(actual, expected) {
2379 t.Errorf("staticDeps orderings were not propagated correctly"+
2380 "\nactual: %v"+
2381 "\nexpected: %v",
2382 actual,
2383 expected,
2384 )
2385 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002386}
Jeff Gaston294356f2017-09-27 17:05:30 -07002387
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002388func TestStaticLibDepReorderingWithShared(t *testing.T) {
2389 ctx := testCc(t, `
2390 cc_library {
2391 name: "a",
2392 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002393 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002394 }
2395 cc_library {
2396 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002397 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002398 }
2399 cc_library {
2400 name: "c",
2401 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002402 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002403 }
2404
2405 `)
2406
Colin Cross7113d202019-11-20 16:39:12 -08002407 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002408 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
2409 actual := moduleA.depsInLinkOrder
2410 expected := getOutputPaths(ctx, variant, []string{"c", "b"})
2411
2412 if !reflect.DeepEqual(actual, expected) {
2413 t.Errorf("staticDeps orderings did not account for shared libs"+
2414 "\nactual: %v"+
2415 "\nexpected: %v",
2416 actual,
2417 expected,
2418 )
2419 }
2420}
2421
Jooyung Hanb04a4992020-03-13 18:57:35 +09002422func checkEquals(t *testing.T, message string, expected, actual interface{}) {
2423 if !reflect.DeepEqual(actual, expected) {
2424 t.Errorf(message+
2425 "\nactual: %v"+
2426 "\nexpected: %v",
2427 actual,
2428 expected,
2429 )
2430 }
2431}
2432
Jooyung Han61b66e92020-03-21 14:21:46 +00002433func TestLlndkLibrary(t *testing.T) {
2434 ctx := testCc(t, `
2435 cc_library {
2436 name: "libllndk",
2437 stubs: { versions: ["1", "2"] },
2438 }
2439 llndk_library {
2440 name: "libllndk",
2441 }
2442 `)
2443 actual := ctx.ModuleVariantsForTests("libllndk.llndk")
2444 expected := []string{
2445 "android_vendor.VER_arm64_armv8-a_shared",
2446 "android_vendor.VER_arm64_armv8-a_shared_1",
2447 "android_vendor.VER_arm64_armv8-a_shared_2",
2448 "android_vendor.VER_arm_armv7-a-neon_shared",
2449 "android_vendor.VER_arm_armv7-a-neon_shared_1",
2450 "android_vendor.VER_arm_armv7-a-neon_shared_2",
2451 }
2452 checkEquals(t, "variants for llndk stubs", expected, actual)
2453
2454 params := ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
2455 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2456
2457 params = ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
2458 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2459}
2460
Jiyong Parka46a4d52017-12-14 19:54:34 +09002461func TestLlndkHeaders(t *testing.T) {
2462 ctx := testCc(t, `
2463 llndk_headers {
2464 name: "libllndk_headers",
2465 export_include_dirs: ["my_include"],
2466 }
2467 llndk_library {
2468 name: "libllndk",
2469 export_llndk_headers: ["libllndk_headers"],
2470 }
2471 cc_library {
2472 name: "libvendor",
2473 shared_libs: ["libllndk"],
2474 vendor: true,
2475 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002476 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002477 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002478 }
2479 `)
2480
2481 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002482 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002483 cflags := cc.Args["cFlags"]
2484 if !strings.Contains(cflags, "-Imy_include") {
2485 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2486 }
2487}
2488
Logan Chien43d34c32017-12-20 01:17:32 +08002489func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2490 actual := module.Properties.AndroidMkRuntimeLibs
2491 if !reflect.DeepEqual(actual, expected) {
2492 t.Errorf("incorrect runtime_libs for shared libs"+
2493 "\nactual: %v"+
2494 "\nexpected: %v",
2495 actual,
2496 expected,
2497 )
2498 }
2499}
2500
2501const runtimeLibAndroidBp = `
2502 cc_library {
2503 name: "libvendor_available1",
2504 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002505 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002506 nocrt : true,
2507 system_shared_libs : [],
2508 }
2509 cc_library {
2510 name: "libvendor_available2",
2511 vendor_available: true,
2512 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002513 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002514 nocrt : true,
2515 system_shared_libs : [],
2516 }
2517 cc_library {
2518 name: "libvendor_available3",
2519 vendor_available: true,
2520 runtime_libs: ["libvendor_available1"],
2521 target: {
2522 vendor: {
2523 exclude_runtime_libs: ["libvendor_available1"],
2524 }
2525 },
Yi Konge7fe9912019-06-02 00:53:50 -07002526 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002527 nocrt : true,
2528 system_shared_libs : [],
2529 }
2530 cc_library {
2531 name: "libcore",
2532 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002533 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002534 nocrt : true,
2535 system_shared_libs : [],
2536 }
2537 cc_library {
2538 name: "libvendor1",
2539 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002540 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002541 nocrt : true,
2542 system_shared_libs : [],
2543 }
2544 cc_library {
2545 name: "libvendor2",
2546 vendor: true,
2547 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002548 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002549 nocrt : true,
2550 system_shared_libs : [],
2551 }
2552`
2553
2554func TestRuntimeLibs(t *testing.T) {
2555 ctx := testCc(t, runtimeLibAndroidBp)
2556
2557 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002558 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002559
2560 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2561 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2562
2563 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
2564 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2565
2566 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2567 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002568 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002569
2570 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2571 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
2572
2573 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2574 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
2575}
2576
2577func TestExcludeRuntimeLibs(t *testing.T) {
2578 ctx := testCc(t, runtimeLibAndroidBp)
2579
Colin Cross7113d202019-11-20 16:39:12 -08002580 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002581 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2582 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2583
Colin Crossfb0c16e2019-11-20 17:12:35 -08002584 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002585 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2586 checkRuntimeLibs(t, nil, module)
2587}
2588
2589func TestRuntimeLibsNoVndk(t *testing.T) {
2590 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2591
2592 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2593
Colin Cross7113d202019-11-20 16:39:12 -08002594 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002595
2596 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2597 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2598
2599 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2600 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
2601}
2602
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002603func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002604 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002605 actual := module.Properties.AndroidMkStaticLibs
2606 if !reflect.DeepEqual(actual, expected) {
2607 t.Errorf("incorrect static_libs"+
2608 "\nactual: %v"+
2609 "\nexpected: %v",
2610 actual,
2611 expected,
2612 )
2613 }
2614}
2615
2616const staticLibAndroidBp = `
2617 cc_library {
2618 name: "lib1",
2619 }
2620 cc_library {
2621 name: "lib2",
2622 static_libs: ["lib1"],
2623 }
2624`
2625
2626func TestStaticLibDepExport(t *testing.T) {
2627 ctx := testCc(t, staticLibAndroidBp)
2628
2629 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002630 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002631 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002632 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002633
2634 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002635 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002636 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2637 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002638 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002639}
2640
Jiyong Parkd08b6972017-09-26 10:50:54 +09002641var compilerFlagsTestCases = []struct {
2642 in string
2643 out bool
2644}{
2645 {
2646 in: "a",
2647 out: false,
2648 },
2649 {
2650 in: "-a",
2651 out: true,
2652 },
2653 {
2654 in: "-Ipath/to/something",
2655 out: false,
2656 },
2657 {
2658 in: "-isystempath/to/something",
2659 out: false,
2660 },
2661 {
2662 in: "--coverage",
2663 out: false,
2664 },
2665 {
2666 in: "-include a/b",
2667 out: true,
2668 },
2669 {
2670 in: "-include a/b c/d",
2671 out: false,
2672 },
2673 {
2674 in: "-DMACRO",
2675 out: true,
2676 },
2677 {
2678 in: "-DMAC RO",
2679 out: false,
2680 },
2681 {
2682 in: "-a -b",
2683 out: false,
2684 },
2685 {
2686 in: "-DMACRO=definition",
2687 out: true,
2688 },
2689 {
2690 in: "-DMACRO=defi nition",
2691 out: true, // TODO(jiyong): this should be false
2692 },
2693 {
2694 in: "-DMACRO(x)=x + 1",
2695 out: true,
2696 },
2697 {
2698 in: "-DMACRO=\"defi nition\"",
2699 out: true,
2700 },
2701}
2702
2703type mockContext struct {
2704 BaseModuleContext
2705 result bool
2706}
2707
2708func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2709 // CheckBadCompilerFlags calls this function when the flag should be rejected
2710 ctx.result = false
2711}
2712
2713func TestCompilerFlags(t *testing.T) {
2714 for _, testCase := range compilerFlagsTestCases {
2715 ctx := &mockContext{result: true}
2716 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
2717 if ctx.result != testCase.out {
2718 t.Errorf("incorrect output:")
2719 t.Errorf(" input: %#v", testCase.in)
2720 t.Errorf(" expected: %#v", testCase.out)
2721 t.Errorf(" got: %#v", ctx.result)
2722 }
2723 }
Jeff Gaston294356f2017-09-27 17:05:30 -07002724}
Jiyong Park374510b2018-03-19 18:23:01 +09002725
2726func TestVendorPublicLibraries(t *testing.T) {
2727 ctx := testCc(t, `
2728 cc_library_headers {
2729 name: "libvendorpublic_headers",
2730 export_include_dirs: ["my_include"],
2731 }
2732 vendor_public_library {
2733 name: "libvendorpublic",
2734 symbol_file: "",
2735 export_public_headers: ["libvendorpublic_headers"],
2736 }
2737 cc_library {
2738 name: "libvendorpublic",
2739 srcs: ["foo.c"],
2740 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002741 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002742 nocrt: true,
2743 }
2744
2745 cc_library {
2746 name: "libsystem",
2747 shared_libs: ["libvendorpublic"],
2748 vendor: false,
2749 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002750 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002751 nocrt: true,
2752 }
2753 cc_library {
2754 name: "libvendor",
2755 shared_libs: ["libvendorpublic"],
2756 vendor: true,
2757 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002758 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002759 nocrt: true,
2760 }
2761 `)
2762
Colin Cross7113d202019-11-20 16:39:12 -08002763 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08002764 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09002765
2766 // test if header search paths are correctly added
2767 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08002768 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09002769 cflags := cc.Args["cFlags"]
2770 if !strings.Contains(cflags, "-Imy_include") {
2771 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
2772 }
2773
2774 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08002775 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002776 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002777 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09002778 if !strings.Contains(libflags, stubPaths[0].String()) {
2779 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
2780 }
2781
2782 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08002783 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002784 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002785 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09002786 if !strings.Contains(libflags, stubPaths[0].String()) {
2787 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
2788 }
2789
2790}
Jiyong Park37b25202018-07-11 10:49:27 +09002791
2792func TestRecovery(t *testing.T) {
2793 ctx := testCc(t, `
2794 cc_library_shared {
2795 name: "librecovery",
2796 recovery: true,
2797 }
2798 cc_library_shared {
2799 name: "librecovery32",
2800 recovery: true,
2801 compile_multilib:"32",
2802 }
Jiyong Park5baac542018-08-28 09:55:37 +09002803 cc_library_shared {
2804 name: "libHalInRecovery",
2805 recovery_available: true,
2806 vendor: true,
2807 }
Jiyong Park37b25202018-07-11 10:49:27 +09002808 `)
2809
2810 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08002811 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09002812 if len(variants) != 1 || !android.InList(arm64, variants) {
2813 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
2814 }
2815
2816 variants = ctx.ModuleVariantsForTests("librecovery32")
2817 if android.InList(arm64, variants) {
2818 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
2819 }
Jiyong Park5baac542018-08-28 09:55:37 +09002820
2821 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
2822 if !recoveryModule.Platform() {
2823 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
2824 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09002825}
Jiyong Park5baac542018-08-28 09:55:37 +09002826
Jiyong Park7ed9de32018-10-15 22:25:07 +09002827func TestVersionedStubs(t *testing.T) {
2828 ctx := testCc(t, `
2829 cc_library_shared {
2830 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002831 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002832 stubs: {
2833 symbol_file: "foo.map.txt",
2834 versions: ["1", "2", "3"],
2835 },
2836 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002837
Jiyong Park7ed9de32018-10-15 22:25:07 +09002838 cc_library_shared {
2839 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002840 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002841 shared_libs: ["libFoo#1"],
2842 }`)
2843
2844 variants := ctx.ModuleVariantsForTests("libFoo")
2845 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08002846 "android_arm64_armv8-a_shared",
2847 "android_arm64_armv8-a_shared_1",
2848 "android_arm64_armv8-a_shared_2",
2849 "android_arm64_armv8-a_shared_3",
2850 "android_arm_armv7-a-neon_shared",
2851 "android_arm_armv7-a-neon_shared_1",
2852 "android_arm_armv7-a-neon_shared_2",
2853 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09002854 }
2855 variantsMismatch := false
2856 if len(variants) != len(expectedVariants) {
2857 variantsMismatch = true
2858 } else {
2859 for _, v := range expectedVariants {
2860 if !inList(v, variants) {
2861 variantsMismatch = false
2862 }
2863 }
2864 }
2865 if variantsMismatch {
2866 t.Errorf("variants of libFoo expected:\n")
2867 for _, v := range expectedVariants {
2868 t.Errorf("%q\n", v)
2869 }
2870 t.Errorf(", but got:\n")
2871 for _, v := range variants {
2872 t.Errorf("%q\n", v)
2873 }
2874 }
2875
Colin Cross7113d202019-11-20 16:39:12 -08002876 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09002877 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002878 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09002879 if !strings.Contains(libFlags, libFoo1StubPath) {
2880 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
2881 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002882
Colin Cross7113d202019-11-20 16:39:12 -08002883 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09002884 cFlags := libBarCompileRule.Args["cFlags"]
2885 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
2886 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
2887 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
2888 }
Jiyong Park37b25202018-07-11 10:49:27 +09002889}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002890
Jooyung Hanb04a4992020-03-13 18:57:35 +09002891func TestVersioningMacro(t *testing.T) {
2892 for _, tc := range []struct{ moduleName, expected string }{
2893 {"libc", "__LIBC_API__"},
2894 {"libfoo", "__LIBFOO_API__"},
2895 {"libfoo@1", "__LIBFOO_1_API__"},
2896 {"libfoo-v1", "__LIBFOO_V1_API__"},
2897 {"libfoo.v1", "__LIBFOO_V1_API__"},
2898 } {
2899 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
2900 }
2901}
2902
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002903func TestStaticExecutable(t *testing.T) {
2904 ctx := testCc(t, `
2905 cc_binary {
2906 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01002907 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002908 static_executable: true,
2909 }`)
2910
Colin Cross7113d202019-11-20 16:39:12 -08002911 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002912 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
2913 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07002914 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002915 for _, lib := range systemStaticLibs {
2916 if !strings.Contains(libFlags, lib) {
2917 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
2918 }
2919 }
2920 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
2921 for _, lib := range systemSharedLibs {
2922 if strings.Contains(libFlags, lib) {
2923 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
2924 }
2925 }
2926}
Jiyong Parke4bb9862019-02-01 00:31:10 +09002927
2928func TestStaticDepsOrderWithStubs(t *testing.T) {
2929 ctx := testCc(t, `
2930 cc_binary {
2931 name: "mybin",
2932 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08002933 static_libs: ["libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09002934 static_executable: true,
2935 stl: "none",
2936 }
2937
2938 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08002939 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09002940 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08002941 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09002942 stl: "none",
2943 }
2944
2945 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08002946 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09002947 srcs: ["foo.c"],
2948 stl: "none",
2949 stubs: {
2950 versions: ["1"],
2951 },
2952 }`)
2953
Colin Cross7113d202019-11-20 16:39:12 -08002954 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Module().(*Module)
Jiyong Parke4bb9862019-02-01 00:31:10 +09002955 actual := mybin.depsInLinkOrder
Colin Crossf9aabd72020-02-15 11:29:50 -08002956 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09002957
2958 if !reflect.DeepEqual(actual, expected) {
2959 t.Errorf("staticDeps orderings were not propagated correctly"+
2960 "\nactual: %v"+
2961 "\nexpected: %v",
2962 actual,
2963 expected,
2964 )
2965 }
2966}
Jooyung Han38002912019-05-16 04:01:54 +09002967
Jooyung Hand48f3c32019-08-23 11:18:57 +09002968func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
2969 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
2970 cc_library {
2971 name: "libA",
2972 srcs: ["foo.c"],
2973 shared_libs: ["libB"],
2974 stl: "none",
2975 }
2976
2977 cc_library {
2978 name: "libB",
2979 srcs: ["foo.c"],
2980 enabled: false,
2981 stl: "none",
2982 }
2983 `)
2984}
2985
Mitch Phillipsda9a4632019-07-15 09:34:09 -07002986// Simple smoke test for the cc_fuzz target that ensures the rule compiles
2987// correctly.
2988func TestFuzzTarget(t *testing.T) {
2989 ctx := testCc(t, `
2990 cc_fuzz {
2991 name: "fuzz_smoke_test",
2992 srcs: ["foo.c"],
2993 }`)
2994
Paul Duffin075c4172019-12-19 19:06:13 +00002995 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07002996 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
2997}
2998
Jiyong Park29074592019-07-07 16:27:47 +09002999func TestAidl(t *testing.T) {
3000}
3001
Jooyung Han38002912019-05-16 04:01:54 +09003002func assertString(t *testing.T, got, expected string) {
3003 t.Helper()
3004 if got != expected {
3005 t.Errorf("expected %q got %q", expected, got)
3006 }
3007}
3008
3009func assertArrayString(t *testing.T, got, expected []string) {
3010 t.Helper()
3011 if len(got) != len(expected) {
3012 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3013 return
3014 }
3015 for i := range got {
3016 if got[i] != expected[i] {
3017 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3018 i, expected[i], expected, got[i], got)
3019 return
3020 }
3021 }
3022}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003023
Jooyung Han0302a842019-10-30 18:43:49 +09003024func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3025 t.Helper()
3026 assertArrayString(t, android.SortedStringKeys(m), expected)
3027}
3028
Colin Crosse1bb5d02019-09-24 14:55:04 -07003029func TestDefaults(t *testing.T) {
3030 ctx := testCc(t, `
3031 cc_defaults {
3032 name: "defaults",
3033 srcs: ["foo.c"],
3034 static: {
3035 srcs: ["bar.c"],
3036 },
3037 shared: {
3038 srcs: ["baz.c"],
3039 },
3040 }
3041
3042 cc_library_static {
3043 name: "libstatic",
3044 defaults: ["defaults"],
3045 }
3046
3047 cc_library_shared {
3048 name: "libshared",
3049 defaults: ["defaults"],
3050 }
3051
3052 cc_library {
3053 name: "libboth",
3054 defaults: ["defaults"],
3055 }
3056
3057 cc_binary {
3058 name: "binary",
3059 defaults: ["defaults"],
3060 }`)
3061
3062 pathsToBase := func(paths android.Paths) []string {
3063 var ret []string
3064 for _, p := range paths {
3065 ret = append(ret, p.Base())
3066 }
3067 return ret
3068 }
3069
Colin Cross7113d202019-11-20 16:39:12 -08003070 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003071 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3072 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3073 }
Colin Cross7113d202019-11-20 16:39:12 -08003074 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003075 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3076 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3077 }
Colin Cross7113d202019-11-20 16:39:12 -08003078 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003079 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3080 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3081 }
3082
Colin Cross7113d202019-11-20 16:39:12 -08003083 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003084 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3085 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3086 }
Colin Cross7113d202019-11-20 16:39:12 -08003087 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003088 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3089 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3090 }
3091}
Colin Crosseabaedd2020-02-06 17:01:55 -08003092
3093func TestProductVariableDefaults(t *testing.T) {
3094 bp := `
3095 cc_defaults {
3096 name: "libfoo_defaults",
3097 srcs: ["foo.c"],
3098 cppflags: ["-DFOO"],
3099 product_variables: {
3100 debuggable: {
3101 cppflags: ["-DBAR"],
3102 },
3103 },
3104 }
3105
3106 cc_library {
3107 name: "libfoo",
3108 defaults: ["libfoo_defaults"],
3109 }
3110 `
3111
3112 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3113 config.TestProductVariables.Debuggable = BoolPtr(true)
3114
3115 ctx := CreateTestContext()
3116 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
3117 ctx.BottomUp("variable", android.VariableMutator).Parallel()
3118 })
3119 ctx.Register(config)
3120
3121 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3122 android.FailIfErrored(t, errs)
3123 _, errs = ctx.PrepareBuildActions(config)
3124 android.FailIfErrored(t, errs)
3125
3126 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
3127 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
3128 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
3129 }
3130}