blob: d4e941676a107292f9bd9ad570b4b1ccfdfe461c [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 Kimb6a58622020-05-20 22:53:06 +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 Kimb6a58622020-05-20 22:53:06 +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 Kimb6a58622020-05-20 22:53:06 +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 Kimb6a58622020-05-20 22:53:06 +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 }
842`
843 config := TestConfig(buildDir, android.Android, nil, bp, nil)
844 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
845 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
846 ctx := testCcWithConfig(t, config)
847
848 // Check Vendor snapshot output.
849
850 snapshotDir := "vendor-snapshot"
851 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kimb6a58622020-05-20 22:53:06 +0900852 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
853
854 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +0900855
856 for _, arch := range [][]string{
857 []string{"arm64", "armv8-a"},
858 []string{"arm", "armv7-a-neon"},
859 } {
860 archType := arch[0]
861 archVariant := arch[1]
862 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
863
864 // For shared libraries, only non-VNDK vendor_available modules are captured
865 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
866 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kimb6a58622020-05-20 22:53:06 +0900867 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
868 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
869 jsonFiles = append(jsonFiles,
870 filepath.Join(sharedDir, "libvendor.so.json"),
871 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +0900872
873 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
874 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
875 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kimb6a58622020-05-20 22:53:06 +0900876 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
877 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
878 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
879 jsonFiles = append(jsonFiles,
880 filepath.Join(staticDir, "libvndk.a.json"),
881 filepath.Join(staticDir, "libvendor.a.json"),
882 filepath.Join(staticDir, "libvendor_available.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +0900883
884 // For binary libraries, all vendor:true and vendor_available modules are captured.
885 if archType == "arm64" {
886 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
887 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kimb6a58622020-05-20 22:53:06 +0900888 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
889 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
890 jsonFiles = append(jsonFiles,
891 filepath.Join(binaryDir, "vendor_bin.json"),
892 filepath.Join(binaryDir, "vendor_available_bin.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +0900893 }
Inseob Kimb6a58622020-05-20 22:53:06 +0900894
895 // For header libraries, all vendor:true and vendor_available modules are captured.
896 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
897 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
898 }
899
900 for _, jsonFile := range jsonFiles {
901 // verify all json files exist
902 snapshotSingleton.Output(jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +0900903 }
904}
905
Jooyung Hana70f0672019-01-18 15:20:43 +0900906func TestDoubleLoadableDepError(t *testing.T) {
907 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
908 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
909 cc_library {
910 name: "libllndk",
911 shared_libs: ["libnondoubleloadable"],
912 }
913
914 llndk_library {
915 name: "libllndk",
916 symbol_file: "",
917 }
918
919 cc_library {
920 name: "libnondoubleloadable",
921 vendor_available: true,
922 vndk: {
923 enabled: true,
924 },
925 }
926 `)
927
928 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
929 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
930 cc_library {
931 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -0700932 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900933 shared_libs: ["libnondoubleloadable"],
934 }
935
936 llndk_library {
937 name: "libllndk",
938 symbol_file: "",
939 }
940
941 cc_library {
942 name: "libnondoubleloadable",
943 vendor_available: true,
944 }
945 `)
946
947 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
948 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
949 cc_library {
950 name: "libdoubleloadable",
951 vendor_available: true,
952 double_loadable: true,
953 shared_libs: ["libnondoubleloadable"],
954 }
955
956 cc_library {
957 name: "libnondoubleloadable",
958 vendor_available: true,
959 }
960 `)
961
962 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
963 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
964 cc_library {
965 name: "libdoubleloadable",
966 vendor_available: true,
967 double_loadable: true,
968 shared_libs: ["libnondoubleloadable"],
969 }
970
971 cc_library {
972 name: "libnondoubleloadable",
973 vendor_available: true,
974 vndk: {
975 enabled: true,
976 },
977 }
978 `)
979
980 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
981 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
982 cc_library {
983 name: "libdoubleloadable",
984 vendor_available: true,
985 vndk: {
986 enabled: true,
987 },
988 double_loadable: true,
989 shared_libs: ["libnondoubleloadable"],
990 }
991
992 cc_library {
993 name: "libnondoubleloadable",
994 vendor_available: false,
995 vndk: {
996 enabled: true,
997 },
998 }
999 `)
1000
1001 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1002 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1003 cc_library {
1004 name: "libllndk",
1005 shared_libs: ["libcoreonly"],
1006 }
1007
1008 llndk_library {
1009 name: "libllndk",
1010 symbol_file: "",
1011 }
1012
1013 cc_library {
1014 name: "libcoreonly",
1015 shared_libs: ["libvendoravailable"],
1016 }
1017
1018 // indirect dependency of LLNDK
1019 cc_library {
1020 name: "libvendoravailable",
1021 vendor_available: true,
1022 }
1023 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001024}
1025
Logan Chienf3511742017-10-31 18:04:35 +08001026func TestVndkExt(t *testing.T) {
1027 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001028 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001029 cc_library {
1030 name: "libvndk",
1031 vendor_available: true,
1032 vndk: {
1033 enabled: true,
1034 },
1035 nocrt: true,
1036 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001037 cc_library {
1038 name: "libvndk2",
1039 vendor_available: true,
1040 vndk: {
1041 enabled: true,
1042 },
1043 target: {
1044 vendor: {
1045 suffix: "-suffix",
1046 },
1047 },
1048 nocrt: true,
1049 }
Logan Chienf3511742017-10-31 18:04:35 +08001050
1051 cc_library {
1052 name: "libvndk_ext",
1053 vendor: true,
1054 vndk: {
1055 enabled: true,
1056 extends: "libvndk",
1057 },
1058 nocrt: true,
1059 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001060
1061 cc_library {
1062 name: "libvndk2_ext",
1063 vendor: true,
1064 vndk: {
1065 enabled: true,
1066 extends: "libvndk2",
1067 },
1068 nocrt: true,
1069 }
Logan Chienf3511742017-10-31 18:04:35 +08001070
Justin Yun0ecf0b22020-02-28 15:07:59 +09001071 cc_library {
1072 name: "libvndk_ext_product",
1073 product_specific: true,
1074 vndk: {
1075 enabled: true,
1076 extends: "libvndk",
1077 },
1078 nocrt: true,
1079 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001080
Justin Yun0ecf0b22020-02-28 15:07:59 +09001081 cc_library {
1082 name: "libvndk2_ext_product",
1083 product_specific: true,
1084 vndk: {
1085 enabled: true,
1086 extends: "libvndk2",
1087 },
1088 nocrt: true,
1089 }
1090 `
1091 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1092 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1093 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1094 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1095
1096 ctx := testCcWithConfig(t, config)
1097
1098 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1099 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1100
1101 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1102 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1103
1104 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1105 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001106}
1107
Logan Chiend3c59a22018-03-29 14:08:15 +08001108func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001109 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1110 ctx := testCcNoVndk(t, `
1111 cc_library {
1112 name: "libvndk",
1113 vendor_available: true,
1114 vndk: {
1115 enabled: true,
1116 },
1117 nocrt: true,
1118 }
1119
1120 cc_library {
1121 name: "libvndk_ext",
1122 vendor: true,
1123 vndk: {
1124 enabled: true,
1125 extends: "libvndk",
1126 },
1127 nocrt: true,
1128 }
1129 `)
1130
1131 // Ensures that the core variant of "libvndk_ext" can be found.
1132 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1133 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1134 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1135 }
1136}
1137
Justin Yun0ecf0b22020-02-28 15:07:59 +09001138func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1139 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
1140 ctx := testCc(t, `
1141 cc_library {
1142 name: "libvndk",
1143 vendor_available: true,
1144 vndk: {
1145 enabled: true,
1146 },
1147 nocrt: true,
1148 }
1149
1150 cc_library {
1151 name: "libvndk_ext_product",
1152 product_specific: true,
1153 vndk: {
1154 enabled: true,
1155 extends: "libvndk",
1156 },
1157 nocrt: true,
1158 }
1159 `)
1160
1161 // Ensures that the core variant of "libvndk_ext_product" can be found.
1162 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1163 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1164 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1165 }
1166}
1167
Logan Chienf3511742017-10-31 18:04:35 +08001168func TestVndkExtError(t *testing.T) {
1169 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001170 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001171 cc_library {
1172 name: "libvndk",
1173 vendor_available: true,
1174 vndk: {
1175 enabled: true,
1176 },
1177 nocrt: true,
1178 }
1179
1180 cc_library {
1181 name: "libvndk_ext",
1182 vndk: {
1183 enabled: true,
1184 extends: "libvndk",
1185 },
1186 nocrt: true,
1187 }
1188 `)
1189
1190 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1191 cc_library {
1192 name: "libvndk",
1193 vendor_available: true,
1194 vndk: {
1195 enabled: true,
1196 },
1197 nocrt: true,
1198 }
1199
1200 cc_library {
1201 name: "libvndk_ext",
1202 vendor: true,
1203 vndk: {
1204 enabled: true,
1205 },
1206 nocrt: true,
1207 }
1208 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001209
1210 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1211 cc_library {
1212 name: "libvndk",
1213 vendor_available: true,
1214 vndk: {
1215 enabled: true,
1216 },
1217 nocrt: true,
1218 }
1219
1220 cc_library {
1221 name: "libvndk_ext_product",
1222 product_specific: true,
1223 vndk: {
1224 enabled: true,
1225 },
1226 nocrt: true,
1227 }
1228 `)
1229
1230 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1231 cc_library {
1232 name: "libvndk",
1233 vendor_available: true,
1234 vndk: {
1235 enabled: true,
1236 },
1237 nocrt: true,
1238 }
1239
1240 cc_library {
1241 name: "libvndk_ext_product",
1242 product_specific: true,
1243 vendor_available: true,
1244 vndk: {
1245 enabled: true,
1246 extends: "libvndk",
1247 },
1248 nocrt: true,
1249 }
1250 `)
Logan Chienf3511742017-10-31 18:04:35 +08001251}
1252
1253func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1254 // This test ensures an error is emitted for inconsistent support_system_process.
1255 testCcError(t, "module \".*\" with mismatched support_system_process", `
1256 cc_library {
1257 name: "libvndk",
1258 vendor_available: true,
1259 vndk: {
1260 enabled: true,
1261 },
1262 nocrt: true,
1263 }
1264
1265 cc_library {
1266 name: "libvndk_sp_ext",
1267 vendor: true,
1268 vndk: {
1269 enabled: true,
1270 extends: "libvndk",
1271 support_system_process: true,
1272 },
1273 nocrt: true,
1274 }
1275 `)
1276
1277 testCcError(t, "module \".*\" with mismatched support_system_process", `
1278 cc_library {
1279 name: "libvndk_sp",
1280 vendor_available: true,
1281 vndk: {
1282 enabled: true,
1283 support_system_process: true,
1284 },
1285 nocrt: true,
1286 }
1287
1288 cc_library {
1289 name: "libvndk_ext",
1290 vendor: true,
1291 vndk: {
1292 enabled: true,
1293 extends: "libvndk_sp",
1294 },
1295 nocrt: true,
1296 }
1297 `)
1298}
1299
1300func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001301 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08001302 // with `vendor_available: false`.
1303 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1304 cc_library {
1305 name: "libvndk",
1306 vendor_available: false,
1307 vndk: {
1308 enabled: true,
1309 },
1310 nocrt: true,
1311 }
1312
1313 cc_library {
1314 name: "libvndk_ext",
1315 vendor: true,
1316 vndk: {
1317 enabled: true,
1318 extends: "libvndk",
1319 },
1320 nocrt: true,
1321 }
1322 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001323
1324 testCcErrorProductVndk(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1325 cc_library {
1326 name: "libvndk",
1327 vendor_available: false,
1328 vndk: {
1329 enabled: true,
1330 },
1331 nocrt: true,
1332 }
1333
1334 cc_library {
1335 name: "libvndk_ext_product",
1336 product_specific: true,
1337 vndk: {
1338 enabled: true,
1339 extends: "libvndk",
1340 },
1341 nocrt: true,
1342 }
1343 `)
Logan Chienf3511742017-10-31 18:04:35 +08001344}
1345
Logan Chiend3c59a22018-03-29 14:08:15 +08001346func TestVendorModuleUseVndkExt(t *testing.T) {
1347 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001348 testCc(t, `
1349 cc_library {
1350 name: "libvndk",
1351 vendor_available: true,
1352 vndk: {
1353 enabled: true,
1354 },
1355 nocrt: true,
1356 }
1357
1358 cc_library {
1359 name: "libvndk_ext",
1360 vendor: true,
1361 vndk: {
1362 enabled: true,
1363 extends: "libvndk",
1364 },
1365 nocrt: true,
1366 }
1367
1368 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001369 name: "libvndk_sp",
1370 vendor_available: true,
1371 vndk: {
1372 enabled: true,
1373 support_system_process: true,
1374 },
1375 nocrt: true,
1376 }
1377
1378 cc_library {
1379 name: "libvndk_sp_ext",
1380 vendor: true,
1381 vndk: {
1382 enabled: true,
1383 extends: "libvndk_sp",
1384 support_system_process: true,
1385 },
1386 nocrt: true,
1387 }
1388
1389 cc_library {
1390 name: "libvendor",
1391 vendor: true,
1392 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1393 nocrt: true,
1394 }
1395 `)
1396}
1397
Logan Chiend3c59a22018-03-29 14:08:15 +08001398func TestVndkExtUseVendorLib(t *testing.T) {
1399 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001400 testCc(t, `
1401 cc_library {
1402 name: "libvndk",
1403 vendor_available: true,
1404 vndk: {
1405 enabled: true,
1406 },
1407 nocrt: true,
1408 }
1409
1410 cc_library {
1411 name: "libvndk_ext",
1412 vendor: true,
1413 vndk: {
1414 enabled: true,
1415 extends: "libvndk",
1416 },
1417 shared_libs: ["libvendor"],
1418 nocrt: true,
1419 }
1420
1421 cc_library {
1422 name: "libvendor",
1423 vendor: true,
1424 nocrt: true,
1425 }
1426 `)
Logan Chienf3511742017-10-31 18:04:35 +08001427
Logan Chiend3c59a22018-03-29 14:08:15 +08001428 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1429 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001430 cc_library {
1431 name: "libvndk_sp",
1432 vendor_available: true,
1433 vndk: {
1434 enabled: true,
1435 support_system_process: true,
1436 },
1437 nocrt: true,
1438 }
1439
1440 cc_library {
1441 name: "libvndk_sp_ext",
1442 vendor: true,
1443 vndk: {
1444 enabled: true,
1445 extends: "libvndk_sp",
1446 support_system_process: true,
1447 },
1448 shared_libs: ["libvendor"], // Cause an error
1449 nocrt: true,
1450 }
1451
1452 cc_library {
1453 name: "libvendor",
1454 vendor: true,
1455 nocrt: true,
1456 }
1457 `)
1458}
1459
Justin Yun0ecf0b22020-02-28 15:07:59 +09001460func TestProductVndkExtDependency(t *testing.T) {
1461 bp := `
1462 cc_library {
1463 name: "libvndk",
1464 vendor_available: true,
1465 vndk: {
1466 enabled: true,
1467 },
1468 nocrt: true,
1469 }
1470
1471 cc_library {
1472 name: "libvndk_ext_product",
1473 product_specific: true,
1474 vndk: {
1475 enabled: true,
1476 extends: "libvndk",
1477 },
1478 shared_libs: ["libproduct_for_vndklibs"],
1479 nocrt: true,
1480 }
1481
1482 cc_library {
1483 name: "libvndk_sp",
1484 vendor_available: true,
1485 vndk: {
1486 enabled: true,
1487 support_system_process: true,
1488 },
1489 nocrt: true,
1490 }
1491
1492 cc_library {
1493 name: "libvndk_sp_ext_product",
1494 product_specific: true,
1495 vndk: {
1496 enabled: true,
1497 extends: "libvndk_sp",
1498 support_system_process: true,
1499 },
1500 shared_libs: ["libproduct_for_vndklibs"],
1501 nocrt: true,
1502 }
1503
1504 cc_library {
1505 name: "libproduct",
1506 product_specific: true,
1507 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1508 nocrt: true,
1509 }
1510
1511 cc_library {
1512 name: "libproduct_for_vndklibs",
1513 product_specific: true,
1514 nocrt: true,
1515 }
1516 `
1517 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1518 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1519 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1520 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1521
1522 testCcWithConfig(t, config)
1523}
1524
Logan Chiend3c59a22018-03-29 14:08:15 +08001525func TestVndkSpExtUseVndkError(t *testing.T) {
1526 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1527 // library.
1528 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1529 cc_library {
1530 name: "libvndk",
1531 vendor_available: true,
1532 vndk: {
1533 enabled: true,
1534 },
1535 nocrt: true,
1536 }
1537
1538 cc_library {
1539 name: "libvndk_sp",
1540 vendor_available: true,
1541 vndk: {
1542 enabled: true,
1543 support_system_process: true,
1544 },
1545 nocrt: true,
1546 }
1547
1548 cc_library {
1549 name: "libvndk_sp_ext",
1550 vendor: true,
1551 vndk: {
1552 enabled: true,
1553 extends: "libvndk_sp",
1554 support_system_process: true,
1555 },
1556 shared_libs: ["libvndk"], // Cause an error
1557 nocrt: true,
1558 }
1559 `)
1560
1561 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1562 // library.
1563 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1564 cc_library {
1565 name: "libvndk",
1566 vendor_available: true,
1567 vndk: {
1568 enabled: true,
1569 },
1570 nocrt: true,
1571 }
1572
1573 cc_library {
1574 name: "libvndk_ext",
1575 vendor: true,
1576 vndk: {
1577 enabled: true,
1578 extends: "libvndk",
1579 },
1580 nocrt: true,
1581 }
1582
1583 cc_library {
1584 name: "libvndk_sp",
1585 vendor_available: true,
1586 vndk: {
1587 enabled: true,
1588 support_system_process: true,
1589 },
1590 nocrt: true,
1591 }
1592
1593 cc_library {
1594 name: "libvndk_sp_ext",
1595 vendor: true,
1596 vndk: {
1597 enabled: true,
1598 extends: "libvndk_sp",
1599 support_system_process: true,
1600 },
1601 shared_libs: ["libvndk_ext"], // Cause an error
1602 nocrt: true,
1603 }
1604 `)
1605}
1606
1607func TestVndkUseVndkExtError(t *testing.T) {
1608 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1609 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001610 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1611 cc_library {
1612 name: "libvndk",
1613 vendor_available: true,
1614 vndk: {
1615 enabled: true,
1616 },
1617 nocrt: true,
1618 }
1619
1620 cc_library {
1621 name: "libvndk_ext",
1622 vendor: true,
1623 vndk: {
1624 enabled: true,
1625 extends: "libvndk",
1626 },
1627 nocrt: true,
1628 }
1629
1630 cc_library {
1631 name: "libvndk2",
1632 vendor_available: true,
1633 vndk: {
1634 enabled: true,
1635 },
1636 shared_libs: ["libvndk_ext"],
1637 nocrt: true,
1638 }
1639 `)
1640
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001641 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001642 cc_library {
1643 name: "libvndk",
1644 vendor_available: true,
1645 vndk: {
1646 enabled: true,
1647 },
1648 nocrt: true,
1649 }
1650
1651 cc_library {
1652 name: "libvndk_ext",
1653 vendor: true,
1654 vndk: {
1655 enabled: true,
1656 extends: "libvndk",
1657 },
1658 nocrt: true,
1659 }
1660
1661 cc_library {
1662 name: "libvndk2",
1663 vendor_available: true,
1664 vndk: {
1665 enabled: true,
1666 },
1667 target: {
1668 vendor: {
1669 shared_libs: ["libvndk_ext"],
1670 },
1671 },
1672 nocrt: true,
1673 }
1674 `)
1675
1676 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1677 cc_library {
1678 name: "libvndk_sp",
1679 vendor_available: true,
1680 vndk: {
1681 enabled: true,
1682 support_system_process: true,
1683 },
1684 nocrt: true,
1685 }
1686
1687 cc_library {
1688 name: "libvndk_sp_ext",
1689 vendor: true,
1690 vndk: {
1691 enabled: true,
1692 extends: "libvndk_sp",
1693 support_system_process: true,
1694 },
1695 nocrt: true,
1696 }
1697
1698 cc_library {
1699 name: "libvndk_sp_2",
1700 vendor_available: true,
1701 vndk: {
1702 enabled: true,
1703 support_system_process: true,
1704 },
1705 shared_libs: ["libvndk_sp_ext"],
1706 nocrt: true,
1707 }
1708 `)
1709
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001710 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001711 cc_library {
1712 name: "libvndk_sp",
1713 vendor_available: true,
1714 vndk: {
1715 enabled: true,
1716 },
1717 nocrt: true,
1718 }
1719
1720 cc_library {
1721 name: "libvndk_sp_ext",
1722 vendor: true,
1723 vndk: {
1724 enabled: true,
1725 extends: "libvndk_sp",
1726 },
1727 nocrt: true,
1728 }
1729
1730 cc_library {
1731 name: "libvndk_sp2",
1732 vendor_available: true,
1733 vndk: {
1734 enabled: true,
1735 },
1736 target: {
1737 vendor: {
1738 shared_libs: ["libvndk_sp_ext"],
1739 },
1740 },
1741 nocrt: true,
1742 }
1743 `)
1744}
1745
Justin Yun5f7f7e82019-11-18 19:52:14 +09001746func TestEnforceProductVndkVersion(t *testing.T) {
1747 bp := `
1748 cc_library {
1749 name: "libllndk",
1750 }
1751 llndk_library {
1752 name: "libllndk",
1753 symbol_file: "",
1754 }
1755 cc_library {
1756 name: "libvndk",
1757 vendor_available: true,
1758 vndk: {
1759 enabled: true,
1760 },
1761 nocrt: true,
1762 }
1763 cc_library {
1764 name: "libvndk_sp",
1765 vendor_available: true,
1766 vndk: {
1767 enabled: true,
1768 support_system_process: true,
1769 },
1770 nocrt: true,
1771 }
1772 cc_library {
1773 name: "libva",
1774 vendor_available: true,
1775 nocrt: true,
1776 }
1777 cc_library {
1778 name: "libproduct_va",
1779 product_specific: true,
1780 vendor_available: true,
1781 nocrt: true,
1782 }
1783 cc_library {
1784 name: "libprod",
1785 product_specific: true,
1786 shared_libs: [
1787 "libllndk",
1788 "libvndk",
1789 "libvndk_sp",
1790 "libva",
1791 "libproduct_va",
1792 ],
1793 nocrt: true,
1794 }
1795 cc_library {
1796 name: "libvendor",
1797 vendor: true,
1798 shared_libs: [
1799 "libllndk",
1800 "libvndk",
1801 "libvndk_sp",
1802 "libva",
1803 "libproduct_va",
1804 ],
1805 nocrt: true,
1806 }
1807 `
1808
1809 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1810 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1811 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1812 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1813
1814 ctx := testCcWithConfig(t, config)
1815
Justin Yun0ecf0b22020-02-28 15:07:59 +09001816 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "", productVariant)
1817 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "", productVariant)
Justin Yun5f7f7e82019-11-18 19:52:14 +09001818}
1819
1820func TestEnforceProductVndkVersionErrors(t *testing.T) {
1821 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1822 cc_library {
1823 name: "libprod",
1824 product_specific: true,
1825 shared_libs: [
1826 "libvendor",
1827 ],
1828 nocrt: true,
1829 }
1830 cc_library {
1831 name: "libvendor",
1832 vendor: true,
1833 nocrt: true,
1834 }
1835 `)
1836 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1837 cc_library {
1838 name: "libprod",
1839 product_specific: true,
1840 shared_libs: [
1841 "libsystem",
1842 ],
1843 nocrt: true,
1844 }
1845 cc_library {
1846 name: "libsystem",
1847 nocrt: true,
1848 }
1849 `)
1850 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
1851 cc_library {
1852 name: "libprod",
1853 product_specific: true,
1854 shared_libs: [
1855 "libvndk_private",
1856 ],
1857 nocrt: true,
1858 }
1859 cc_library {
1860 name: "libvndk_private",
1861 vendor_available: false,
1862 vndk: {
1863 enabled: true,
1864 },
1865 nocrt: true,
1866 }
1867 `)
1868 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1869 cc_library {
1870 name: "libprod",
1871 product_specific: true,
1872 shared_libs: [
1873 "libsystem_ext",
1874 ],
1875 nocrt: true,
1876 }
1877 cc_library {
1878 name: "libsystem_ext",
1879 system_ext_specific: true,
1880 nocrt: true,
1881 }
1882 `)
1883 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
1884 cc_library {
1885 name: "libsystem",
1886 shared_libs: [
1887 "libproduct_va",
1888 ],
1889 nocrt: true,
1890 }
1891 cc_library {
1892 name: "libproduct_va",
1893 product_specific: true,
1894 vendor_available: true,
1895 nocrt: true,
1896 }
1897 `)
1898}
1899
Jooyung Han38002912019-05-16 04:01:54 +09001900func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001901 bp := `
1902 cc_library {
1903 name: "libvndk",
1904 vendor_available: true,
1905 vndk: {
1906 enabled: true,
1907 },
1908 }
1909 cc_library {
1910 name: "libvndksp",
1911 vendor_available: true,
1912 vndk: {
1913 enabled: true,
1914 support_system_process: true,
1915 },
1916 }
1917 cc_library {
1918 name: "libvndkprivate",
1919 vendor_available: false,
1920 vndk: {
1921 enabled: true,
1922 },
1923 }
1924 cc_library {
1925 name: "libvendor",
1926 vendor: true,
1927 }
1928 cc_library {
1929 name: "libvndkext",
1930 vendor: true,
1931 vndk: {
1932 enabled: true,
1933 extends: "libvndk",
1934 },
1935 }
1936 vndk_prebuilt_shared {
1937 name: "prevndk",
1938 version: "27",
1939 target_arch: "arm",
1940 binder32bit: true,
1941 vendor_available: true,
1942 vndk: {
1943 enabled: true,
1944 },
1945 arch: {
1946 arm: {
1947 srcs: ["liba.so"],
1948 },
1949 },
1950 }
1951 cc_library {
1952 name: "libllndk",
1953 }
1954 llndk_library {
1955 name: "libllndk",
1956 symbol_file: "",
1957 }
1958 cc_library {
1959 name: "libllndkprivate",
1960 }
1961 llndk_library {
1962 name: "libllndkprivate",
1963 vendor_available: false,
1964 symbol_file: "",
1965 }`
1966
1967 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09001968 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1969 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1970 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08001971 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09001972
Jooyung Han0302a842019-10-30 18:43:49 +09001973 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09001974 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09001975 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09001976 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09001977 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09001978 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09001979 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09001980 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09001981
Colin Crossfb0c16e2019-11-20 17:12:35 -08001982 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09001983
Jooyung Han38002912019-05-16 04:01:54 +09001984 tests := []struct {
1985 variant string
1986 name string
1987 expected string
1988 }{
1989 {vendorVariant, "libvndk", "native:vndk"},
1990 {vendorVariant, "libvndksp", "native:vndk"},
1991 {vendorVariant, "libvndkprivate", "native:vndk_private"},
1992 {vendorVariant, "libvendor", "native:vendor"},
1993 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09001994 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09001995 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09001996 {coreVariant, "libvndk", "native:platform"},
1997 {coreVariant, "libvndkprivate", "native:platform"},
1998 {coreVariant, "libllndk", "native:platform"},
1999 }
2000 for _, test := range tests {
2001 t.Run(test.name, func(t *testing.T) {
2002 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2003 assertString(t, module.makeLinkType, test.expected)
2004 })
2005 }
2006}
2007
Colin Cross0af4b842015-04-30 16:36:18 -07002008var (
2009 str11 = "01234567891"
2010 str10 = str11[:10]
2011 str9 = str11[:9]
2012 str5 = str11[:5]
2013 str4 = str11[:4]
2014)
2015
2016var splitListForSizeTestCases = []struct {
2017 in []string
2018 out [][]string
2019 size int
2020}{
2021 {
2022 in: []string{str10},
2023 out: [][]string{{str10}},
2024 size: 10,
2025 },
2026 {
2027 in: []string{str9},
2028 out: [][]string{{str9}},
2029 size: 10,
2030 },
2031 {
2032 in: []string{str5},
2033 out: [][]string{{str5}},
2034 size: 10,
2035 },
2036 {
2037 in: []string{str11},
2038 out: nil,
2039 size: 10,
2040 },
2041 {
2042 in: []string{str10, str10},
2043 out: [][]string{{str10}, {str10}},
2044 size: 10,
2045 },
2046 {
2047 in: []string{str9, str10},
2048 out: [][]string{{str9}, {str10}},
2049 size: 10,
2050 },
2051 {
2052 in: []string{str10, str9},
2053 out: [][]string{{str10}, {str9}},
2054 size: 10,
2055 },
2056 {
2057 in: []string{str5, str4},
2058 out: [][]string{{str5, str4}},
2059 size: 10,
2060 },
2061 {
2062 in: []string{str5, str4, str5},
2063 out: [][]string{{str5, str4}, {str5}},
2064 size: 10,
2065 },
2066 {
2067 in: []string{str5, str4, str5, str4},
2068 out: [][]string{{str5, str4}, {str5, str4}},
2069 size: 10,
2070 },
2071 {
2072 in: []string{str5, str4, str5, str5},
2073 out: [][]string{{str5, str4}, {str5}, {str5}},
2074 size: 10,
2075 },
2076 {
2077 in: []string{str5, str5, str5, str4},
2078 out: [][]string{{str5}, {str5}, {str5, str4}},
2079 size: 10,
2080 },
2081 {
2082 in: []string{str9, str11},
2083 out: nil,
2084 size: 10,
2085 },
2086 {
2087 in: []string{str11, str9},
2088 out: nil,
2089 size: 10,
2090 },
2091}
2092
2093func TestSplitListForSize(t *testing.T) {
2094 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08002095 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07002096
2097 var outStrings [][]string
2098
2099 if len(out) > 0 {
2100 outStrings = make([][]string, len(out))
2101 for i, o := range out {
2102 outStrings[i] = o.Strings()
2103 }
2104 }
2105
2106 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07002107 t.Errorf("incorrect output:")
2108 t.Errorf(" input: %#v", testCase.in)
2109 t.Errorf(" size: %d", testCase.size)
2110 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07002111 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07002112 }
2113 }
2114}
Jeff Gaston294356f2017-09-27 17:05:30 -07002115
2116var staticLinkDepOrderTestCases = []struct {
2117 // This is a string representation of a map[moduleName][]moduleDependency .
2118 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002119 inStatic string
2120
2121 // This is a string representation of a map[moduleName][]moduleDependency .
2122 // It models the dependencies declared in an Android.bp file.
2123 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002124
2125 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2126 // The keys of allOrdered specify which modules we would like to check.
2127 // The values of allOrdered specify the expected result (of the transitive closure of all
2128 // dependencies) for each module to test
2129 allOrdered string
2130
2131 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2132 // The keys of outOrdered specify which modules we would like to check.
2133 // The values of outOrdered specify the expected result (of the ordered linker command line)
2134 // for each module to test.
2135 outOrdered string
2136}{
2137 // Simple tests
2138 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002139 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002140 outOrdered: "",
2141 },
2142 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002143 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002144 outOrdered: "a:",
2145 },
2146 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002147 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002148 outOrdered: "a:b; b:",
2149 },
2150 // Tests of reordering
2151 {
2152 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002153 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002154 outOrdered: "a:b,c,d; b:d; c:d; d:",
2155 },
2156 {
2157 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002158 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002159 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2160 },
2161 {
2162 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002163 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002164 outOrdered: "a:d,b,e,c; d:b; e:c",
2165 },
2166 {
2167 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002168 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002169 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2170 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2171 },
2172 {
2173 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002174 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 -07002175 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2176 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2177 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002178 // shared dependencies
2179 {
2180 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2181 // So, we don't actually have to check that a shared dependency of c will change the order
2182 // of a library that depends statically on b and on c. We only need to check that if c has
2183 // a shared dependency on b, that that shows up in allOrdered.
2184 inShared: "c:b",
2185 allOrdered: "c:b",
2186 outOrdered: "c:",
2187 },
2188 {
2189 // This test doesn't actually include any shared dependencies but it's a reminder of what
2190 // the second phase of the above test would look like
2191 inStatic: "a:b,c; c:b",
2192 allOrdered: "a:c,b; c:b",
2193 outOrdered: "a:c,b; c:b",
2194 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002195 // tiebreakers for when two modules specifying different orderings and there is no dependency
2196 // to dictate an order
2197 {
2198 // 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 -08002199 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002200 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2201 },
2202 {
2203 // 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 -08002204 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 -07002205 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2206 },
2207 // Tests involving duplicate dependencies
2208 {
2209 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002210 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002211 outOrdered: "a:c,b",
2212 },
2213 {
2214 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002215 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002216 outOrdered: "a:d,c,b",
2217 },
2218 // Tests to confirm the nonexistence of infinite loops.
2219 // These cases should never happen, so as long as the test terminates and the
2220 // result is deterministic then that should be fine.
2221 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002222 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002223 outOrdered: "a:a",
2224 },
2225 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002226 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002227 allOrdered: "a:b,c; b:c,a; c:a,b",
2228 outOrdered: "a:b; b:c; c:a",
2229 },
2230 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002231 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002232 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2233 outOrdered: "a:c,b; b:a,c; c:b,a",
2234 },
2235}
2236
2237// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2238func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2239 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2240 strippedText := strings.Replace(text, " ", "", -1)
2241 if len(strippedText) < 1 {
2242 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2243 }
2244 allDeps = make(map[android.Path][]android.Path, 0)
2245
2246 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2247 moduleTexts := strings.Split(strippedText, ";")
2248
2249 outputForModuleName := func(moduleName string) android.Path {
2250 return android.PathForTesting(moduleName)
2251 }
2252
2253 for _, moduleText := range moduleTexts {
2254 // convert from "a:b,c" to ["a", "b,c"]
2255 components := strings.Split(moduleText, ":")
2256 if len(components) != 2 {
2257 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2258 }
2259 moduleName := components[0]
2260 moduleOutput := outputForModuleName(moduleName)
2261 modulesInOrder = append(modulesInOrder, moduleOutput)
2262
2263 depString := components[1]
2264 // convert from "b,c" to ["b", "c"]
2265 depNames := strings.Split(depString, ",")
2266 if len(depString) < 1 {
2267 depNames = []string{}
2268 }
2269 var deps []android.Path
2270 for _, depName := range depNames {
2271 deps = append(deps, outputForModuleName(depName))
2272 }
2273 allDeps[moduleOutput] = deps
2274 }
2275 return modulesInOrder, allDeps
2276}
2277
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002278func TestLinkReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002279 for _, testCase := range staticLinkDepOrderTestCases {
2280 errs := []string{}
2281
2282 // parse testcase
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002283 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)
Jeff Gaston294356f2017-09-27 17:05:30 -07002284 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)
2285 if testCase.allOrdered == "" {
2286 // allow the test case to skip specifying allOrdered
2287 testCase.allOrdered = testCase.outOrdered
2288 }
2289 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002290 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)
Jeff Gaston294356f2017-09-27 17:05:30 -07002291
2292 // For each module whose post-reordered dependencies were specified, validate that
2293 // reordering the inputs produces the expected outputs.
2294 for _, moduleName := range expectedModuleNames {
2295 moduleDeps := givenTransitiveDeps[moduleName]
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002296 givenSharedDeps := givenAllSharedDeps[moduleName]
2297 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -07002298
2299 correctAllOrdered := expectedAllDeps[moduleName]
2300 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {
2301 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002302 "\nin static:%q"+
2303 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002304 "\nmodule: %v"+
2305 "\nexpected: %s"+
2306 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002307 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002308 }
2309
2310 correctOutputDeps := expectedTransitiveDeps[moduleName]
2311 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {
2312 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002313 "\nin static:%q"+
2314 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002315 "\nmodule: %v"+
2316 "\nexpected: %s"+
2317 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002318 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002319 }
2320 }
2321
2322 if len(errs) > 0 {
2323 sort.Strings(errs)
2324 for _, err := range errs {
2325 t.Error(err)
2326 }
2327 }
2328 }
2329}
Logan Chienf3511742017-10-31 18:04:35 +08002330
Jeff Gaston294356f2017-09-27 17:05:30 -07002331func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2332 for _, moduleName := range moduleNames {
2333 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
2334 output := module.outputFile.Path()
2335 paths = append(paths, output)
2336 }
2337 return paths
2338}
2339
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002340func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002341 ctx := testCc(t, `
2342 cc_library {
2343 name: "a",
2344 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002345 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002346 }
2347 cc_library {
2348 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002349 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002350 }
2351 cc_library {
2352 name: "c",
2353 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002354 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002355 }
2356 cc_library {
2357 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002358 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002359 }
2360
2361 `)
2362
Colin Cross7113d202019-11-20 16:39:12 -08002363 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002364 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002365 actual := moduleA.depsInLinkOrder
Jeff Gaston294356f2017-09-27 17:05:30 -07002366 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})
2367
2368 if !reflect.DeepEqual(actual, expected) {
2369 t.Errorf("staticDeps orderings were not propagated correctly"+
2370 "\nactual: %v"+
2371 "\nexpected: %v",
2372 actual,
2373 expected,
2374 )
2375 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002376}
Jeff Gaston294356f2017-09-27 17:05:30 -07002377
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002378func TestStaticLibDepReorderingWithShared(t *testing.T) {
2379 ctx := testCc(t, `
2380 cc_library {
2381 name: "a",
2382 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002383 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002384 }
2385 cc_library {
2386 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002387 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002388 }
2389 cc_library {
2390 name: "c",
2391 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002392 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002393 }
2394
2395 `)
2396
Colin Cross7113d202019-11-20 16:39:12 -08002397 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002398 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
2399 actual := moduleA.depsInLinkOrder
2400 expected := getOutputPaths(ctx, variant, []string{"c", "b"})
2401
2402 if !reflect.DeepEqual(actual, expected) {
2403 t.Errorf("staticDeps orderings did not account for shared libs"+
2404 "\nactual: %v"+
2405 "\nexpected: %v",
2406 actual,
2407 expected,
2408 )
2409 }
2410}
2411
Jooyung Hanb04a4992020-03-13 18:57:35 +09002412func checkEquals(t *testing.T, message string, expected, actual interface{}) {
2413 if !reflect.DeepEqual(actual, expected) {
2414 t.Errorf(message+
2415 "\nactual: %v"+
2416 "\nexpected: %v",
2417 actual,
2418 expected,
2419 )
2420 }
2421}
2422
Jooyung Han61b66e92020-03-21 14:21:46 +00002423func TestLlndkLibrary(t *testing.T) {
2424 ctx := testCc(t, `
2425 cc_library {
2426 name: "libllndk",
2427 stubs: { versions: ["1", "2"] },
2428 }
2429 llndk_library {
2430 name: "libllndk",
2431 }
2432 `)
2433 actual := ctx.ModuleVariantsForTests("libllndk.llndk")
2434 expected := []string{
2435 "android_vendor.VER_arm64_armv8-a_shared",
2436 "android_vendor.VER_arm64_armv8-a_shared_1",
2437 "android_vendor.VER_arm64_armv8-a_shared_2",
2438 "android_vendor.VER_arm_armv7-a-neon_shared",
2439 "android_vendor.VER_arm_armv7-a-neon_shared_1",
2440 "android_vendor.VER_arm_armv7-a-neon_shared_2",
2441 }
2442 checkEquals(t, "variants for llndk stubs", expected, actual)
2443
2444 params := ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
2445 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2446
2447 params = ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
2448 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2449}
2450
Jiyong Parka46a4d52017-12-14 19:54:34 +09002451func TestLlndkHeaders(t *testing.T) {
2452 ctx := testCc(t, `
2453 llndk_headers {
2454 name: "libllndk_headers",
2455 export_include_dirs: ["my_include"],
2456 }
2457 llndk_library {
2458 name: "libllndk",
2459 export_llndk_headers: ["libllndk_headers"],
2460 }
2461 cc_library {
2462 name: "libvendor",
2463 shared_libs: ["libllndk"],
2464 vendor: true,
2465 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002466 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002467 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002468 }
2469 `)
2470
2471 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002472 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002473 cflags := cc.Args["cFlags"]
2474 if !strings.Contains(cflags, "-Imy_include") {
2475 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2476 }
2477}
2478
Logan Chien43d34c32017-12-20 01:17:32 +08002479func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2480 actual := module.Properties.AndroidMkRuntimeLibs
2481 if !reflect.DeepEqual(actual, expected) {
2482 t.Errorf("incorrect runtime_libs for shared libs"+
2483 "\nactual: %v"+
2484 "\nexpected: %v",
2485 actual,
2486 expected,
2487 )
2488 }
2489}
2490
2491const runtimeLibAndroidBp = `
2492 cc_library {
2493 name: "libvendor_available1",
2494 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002495 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002496 nocrt : true,
2497 system_shared_libs : [],
2498 }
2499 cc_library {
2500 name: "libvendor_available2",
2501 vendor_available: true,
2502 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002503 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002504 nocrt : true,
2505 system_shared_libs : [],
2506 }
2507 cc_library {
2508 name: "libvendor_available3",
2509 vendor_available: true,
2510 runtime_libs: ["libvendor_available1"],
2511 target: {
2512 vendor: {
2513 exclude_runtime_libs: ["libvendor_available1"],
2514 }
2515 },
Yi Konge7fe9912019-06-02 00:53:50 -07002516 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002517 nocrt : true,
2518 system_shared_libs : [],
2519 }
2520 cc_library {
2521 name: "libcore",
2522 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002523 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002524 nocrt : true,
2525 system_shared_libs : [],
2526 }
2527 cc_library {
2528 name: "libvendor1",
2529 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002530 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002531 nocrt : true,
2532 system_shared_libs : [],
2533 }
2534 cc_library {
2535 name: "libvendor2",
2536 vendor: true,
2537 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002538 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002539 nocrt : true,
2540 system_shared_libs : [],
2541 }
2542`
2543
2544func TestRuntimeLibs(t *testing.T) {
2545 ctx := testCc(t, runtimeLibAndroidBp)
2546
2547 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002548 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002549
2550 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2551 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2552
2553 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
2554 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2555
2556 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2557 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002558 variant = "android_vendor.VER_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.vendor"}, module)
2562
2563 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2564 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
2565}
2566
2567func TestExcludeRuntimeLibs(t *testing.T) {
2568 ctx := testCc(t, runtimeLibAndroidBp)
2569
Colin Cross7113d202019-11-20 16:39:12 -08002570 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002571 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2572 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2573
Colin Crossfb0c16e2019-11-20 17:12:35 -08002574 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002575 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2576 checkRuntimeLibs(t, nil, module)
2577}
2578
2579func TestRuntimeLibsNoVndk(t *testing.T) {
2580 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2581
2582 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2583
Colin Cross7113d202019-11-20 16:39:12 -08002584 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002585
2586 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2587 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2588
2589 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2590 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
2591}
2592
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002593func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002594 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002595 actual := module.Properties.AndroidMkStaticLibs
2596 if !reflect.DeepEqual(actual, expected) {
2597 t.Errorf("incorrect static_libs"+
2598 "\nactual: %v"+
2599 "\nexpected: %v",
2600 actual,
2601 expected,
2602 )
2603 }
2604}
2605
2606const staticLibAndroidBp = `
2607 cc_library {
2608 name: "lib1",
2609 }
2610 cc_library {
2611 name: "lib2",
2612 static_libs: ["lib1"],
2613 }
2614`
2615
2616func TestStaticLibDepExport(t *testing.T) {
2617 ctx := testCc(t, staticLibAndroidBp)
2618
2619 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002620 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002621 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002622 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002623
2624 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002625 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002626 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2627 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002628 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002629}
2630
Jiyong Parkd08b6972017-09-26 10:50:54 +09002631var compilerFlagsTestCases = []struct {
2632 in string
2633 out bool
2634}{
2635 {
2636 in: "a",
2637 out: false,
2638 },
2639 {
2640 in: "-a",
2641 out: true,
2642 },
2643 {
2644 in: "-Ipath/to/something",
2645 out: false,
2646 },
2647 {
2648 in: "-isystempath/to/something",
2649 out: false,
2650 },
2651 {
2652 in: "--coverage",
2653 out: false,
2654 },
2655 {
2656 in: "-include a/b",
2657 out: true,
2658 },
2659 {
2660 in: "-include a/b c/d",
2661 out: false,
2662 },
2663 {
2664 in: "-DMACRO",
2665 out: true,
2666 },
2667 {
2668 in: "-DMAC RO",
2669 out: false,
2670 },
2671 {
2672 in: "-a -b",
2673 out: false,
2674 },
2675 {
2676 in: "-DMACRO=definition",
2677 out: true,
2678 },
2679 {
2680 in: "-DMACRO=defi nition",
2681 out: true, // TODO(jiyong): this should be false
2682 },
2683 {
2684 in: "-DMACRO(x)=x + 1",
2685 out: true,
2686 },
2687 {
2688 in: "-DMACRO=\"defi nition\"",
2689 out: true,
2690 },
2691}
2692
2693type mockContext struct {
2694 BaseModuleContext
2695 result bool
2696}
2697
2698func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2699 // CheckBadCompilerFlags calls this function when the flag should be rejected
2700 ctx.result = false
2701}
2702
2703func TestCompilerFlags(t *testing.T) {
2704 for _, testCase := range compilerFlagsTestCases {
2705 ctx := &mockContext{result: true}
2706 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
2707 if ctx.result != testCase.out {
2708 t.Errorf("incorrect output:")
2709 t.Errorf(" input: %#v", testCase.in)
2710 t.Errorf(" expected: %#v", testCase.out)
2711 t.Errorf(" got: %#v", ctx.result)
2712 }
2713 }
Jeff Gaston294356f2017-09-27 17:05:30 -07002714}
Jiyong Park374510b2018-03-19 18:23:01 +09002715
2716func TestVendorPublicLibraries(t *testing.T) {
2717 ctx := testCc(t, `
2718 cc_library_headers {
2719 name: "libvendorpublic_headers",
2720 export_include_dirs: ["my_include"],
2721 }
2722 vendor_public_library {
2723 name: "libvendorpublic",
2724 symbol_file: "",
2725 export_public_headers: ["libvendorpublic_headers"],
2726 }
2727 cc_library {
2728 name: "libvendorpublic",
2729 srcs: ["foo.c"],
2730 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002731 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002732 nocrt: true,
2733 }
2734
2735 cc_library {
2736 name: "libsystem",
2737 shared_libs: ["libvendorpublic"],
2738 vendor: false,
2739 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002740 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002741 nocrt: true,
2742 }
2743 cc_library {
2744 name: "libvendor",
2745 shared_libs: ["libvendorpublic"],
2746 vendor: true,
2747 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002748 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002749 nocrt: true,
2750 }
2751 `)
2752
Colin Cross7113d202019-11-20 16:39:12 -08002753 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08002754 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09002755
2756 // test if header search paths are correctly added
2757 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08002758 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09002759 cflags := cc.Args["cFlags"]
2760 if !strings.Contains(cflags, "-Imy_include") {
2761 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
2762 }
2763
2764 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08002765 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002766 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002767 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09002768 if !strings.Contains(libflags, stubPaths[0].String()) {
2769 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
2770 }
2771
2772 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08002773 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002774 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002775 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09002776 if !strings.Contains(libflags, stubPaths[0].String()) {
2777 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
2778 }
2779
2780}
Jiyong Park37b25202018-07-11 10:49:27 +09002781
2782func TestRecovery(t *testing.T) {
2783 ctx := testCc(t, `
2784 cc_library_shared {
2785 name: "librecovery",
2786 recovery: true,
2787 }
2788 cc_library_shared {
2789 name: "librecovery32",
2790 recovery: true,
2791 compile_multilib:"32",
2792 }
Jiyong Park5baac542018-08-28 09:55:37 +09002793 cc_library_shared {
2794 name: "libHalInRecovery",
2795 recovery_available: true,
2796 vendor: true,
2797 }
Jiyong Park37b25202018-07-11 10:49:27 +09002798 `)
2799
2800 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08002801 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09002802 if len(variants) != 1 || !android.InList(arm64, variants) {
2803 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
2804 }
2805
2806 variants = ctx.ModuleVariantsForTests("librecovery32")
2807 if android.InList(arm64, variants) {
2808 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
2809 }
Jiyong Park5baac542018-08-28 09:55:37 +09002810
2811 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
2812 if !recoveryModule.Platform() {
2813 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
2814 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09002815}
Jiyong Park5baac542018-08-28 09:55:37 +09002816
Jiyong Park7ed9de32018-10-15 22:25:07 +09002817func TestVersionedStubs(t *testing.T) {
2818 ctx := testCc(t, `
2819 cc_library_shared {
2820 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002821 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002822 stubs: {
2823 symbol_file: "foo.map.txt",
2824 versions: ["1", "2", "3"],
2825 },
2826 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002827
Jiyong Park7ed9de32018-10-15 22:25:07 +09002828 cc_library_shared {
2829 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002830 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002831 shared_libs: ["libFoo#1"],
2832 }`)
2833
2834 variants := ctx.ModuleVariantsForTests("libFoo")
2835 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08002836 "android_arm64_armv8-a_shared",
2837 "android_arm64_armv8-a_shared_1",
2838 "android_arm64_armv8-a_shared_2",
2839 "android_arm64_armv8-a_shared_3",
2840 "android_arm_armv7-a-neon_shared",
2841 "android_arm_armv7-a-neon_shared_1",
2842 "android_arm_armv7-a-neon_shared_2",
2843 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09002844 }
2845 variantsMismatch := false
2846 if len(variants) != len(expectedVariants) {
2847 variantsMismatch = true
2848 } else {
2849 for _, v := range expectedVariants {
2850 if !inList(v, variants) {
2851 variantsMismatch = false
2852 }
2853 }
2854 }
2855 if variantsMismatch {
2856 t.Errorf("variants of libFoo expected:\n")
2857 for _, v := range expectedVariants {
2858 t.Errorf("%q\n", v)
2859 }
2860 t.Errorf(", but got:\n")
2861 for _, v := range variants {
2862 t.Errorf("%q\n", v)
2863 }
2864 }
2865
Colin Cross7113d202019-11-20 16:39:12 -08002866 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09002867 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002868 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09002869 if !strings.Contains(libFlags, libFoo1StubPath) {
2870 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
2871 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002872
Colin Cross7113d202019-11-20 16:39:12 -08002873 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09002874 cFlags := libBarCompileRule.Args["cFlags"]
2875 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
2876 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
2877 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
2878 }
Jiyong Park37b25202018-07-11 10:49:27 +09002879}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002880
Jooyung Hanb04a4992020-03-13 18:57:35 +09002881func TestVersioningMacro(t *testing.T) {
2882 for _, tc := range []struct{ moduleName, expected string }{
2883 {"libc", "__LIBC_API__"},
2884 {"libfoo", "__LIBFOO_API__"},
2885 {"libfoo@1", "__LIBFOO_1_API__"},
2886 {"libfoo-v1", "__LIBFOO_V1_API__"},
2887 {"libfoo.v1", "__LIBFOO_V1_API__"},
2888 } {
2889 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
2890 }
2891}
2892
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002893func TestStaticExecutable(t *testing.T) {
2894 ctx := testCc(t, `
2895 cc_binary {
2896 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01002897 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002898 static_executable: true,
2899 }`)
2900
Colin Cross7113d202019-11-20 16:39:12 -08002901 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002902 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
2903 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07002904 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002905 for _, lib := range systemStaticLibs {
2906 if !strings.Contains(libFlags, lib) {
2907 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
2908 }
2909 }
2910 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
2911 for _, lib := range systemSharedLibs {
2912 if strings.Contains(libFlags, lib) {
2913 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
2914 }
2915 }
2916}
Jiyong Parke4bb9862019-02-01 00:31:10 +09002917
2918func TestStaticDepsOrderWithStubs(t *testing.T) {
2919 ctx := testCc(t, `
2920 cc_binary {
2921 name: "mybin",
2922 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08002923 static_libs: ["libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09002924 static_executable: true,
2925 stl: "none",
2926 }
2927
2928 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08002929 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09002930 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08002931 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09002932 stl: "none",
2933 }
2934
2935 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08002936 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09002937 srcs: ["foo.c"],
2938 stl: "none",
2939 stubs: {
2940 versions: ["1"],
2941 },
2942 }`)
2943
Colin Cross7113d202019-11-20 16:39:12 -08002944 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Module().(*Module)
Jiyong Parke4bb9862019-02-01 00:31:10 +09002945 actual := mybin.depsInLinkOrder
Colin Crossf9aabd72020-02-15 11:29:50 -08002946 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09002947
2948 if !reflect.DeepEqual(actual, expected) {
2949 t.Errorf("staticDeps orderings were not propagated correctly"+
2950 "\nactual: %v"+
2951 "\nexpected: %v",
2952 actual,
2953 expected,
2954 )
2955 }
2956}
Jooyung Han38002912019-05-16 04:01:54 +09002957
Jooyung Hand48f3c32019-08-23 11:18:57 +09002958func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
2959 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
2960 cc_library {
2961 name: "libA",
2962 srcs: ["foo.c"],
2963 shared_libs: ["libB"],
2964 stl: "none",
2965 }
2966
2967 cc_library {
2968 name: "libB",
2969 srcs: ["foo.c"],
2970 enabled: false,
2971 stl: "none",
2972 }
2973 `)
2974}
2975
Mitch Phillipsda9a4632019-07-15 09:34:09 -07002976// Simple smoke test for the cc_fuzz target that ensures the rule compiles
2977// correctly.
2978func TestFuzzTarget(t *testing.T) {
2979 ctx := testCc(t, `
2980 cc_fuzz {
2981 name: "fuzz_smoke_test",
2982 srcs: ["foo.c"],
2983 }`)
2984
Paul Duffin075c4172019-12-19 19:06:13 +00002985 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07002986 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
2987}
2988
Jiyong Park29074592019-07-07 16:27:47 +09002989func TestAidl(t *testing.T) {
2990}
2991
Jooyung Han38002912019-05-16 04:01:54 +09002992func assertString(t *testing.T, got, expected string) {
2993 t.Helper()
2994 if got != expected {
2995 t.Errorf("expected %q got %q", expected, got)
2996 }
2997}
2998
2999func assertArrayString(t *testing.T, got, expected []string) {
3000 t.Helper()
3001 if len(got) != len(expected) {
3002 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3003 return
3004 }
3005 for i := range got {
3006 if got[i] != expected[i] {
3007 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3008 i, expected[i], expected, got[i], got)
3009 return
3010 }
3011 }
3012}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003013
Jooyung Han0302a842019-10-30 18:43:49 +09003014func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3015 t.Helper()
3016 assertArrayString(t, android.SortedStringKeys(m), expected)
3017}
3018
Colin Crosse1bb5d02019-09-24 14:55:04 -07003019func TestDefaults(t *testing.T) {
3020 ctx := testCc(t, `
3021 cc_defaults {
3022 name: "defaults",
3023 srcs: ["foo.c"],
3024 static: {
3025 srcs: ["bar.c"],
3026 },
3027 shared: {
3028 srcs: ["baz.c"],
3029 },
3030 }
3031
3032 cc_library_static {
3033 name: "libstatic",
3034 defaults: ["defaults"],
3035 }
3036
3037 cc_library_shared {
3038 name: "libshared",
3039 defaults: ["defaults"],
3040 }
3041
3042 cc_library {
3043 name: "libboth",
3044 defaults: ["defaults"],
3045 }
3046
3047 cc_binary {
3048 name: "binary",
3049 defaults: ["defaults"],
3050 }`)
3051
3052 pathsToBase := func(paths android.Paths) []string {
3053 var ret []string
3054 for _, p := range paths {
3055 ret = append(ret, p.Base())
3056 }
3057 return ret
3058 }
3059
Colin Cross7113d202019-11-20 16:39:12 -08003060 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003061 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3062 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3063 }
Colin Cross7113d202019-11-20 16:39:12 -08003064 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003065 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3066 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3067 }
Colin Cross7113d202019-11-20 16:39:12 -08003068 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003069 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3070 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3071 }
3072
Colin Cross7113d202019-11-20 16:39:12 -08003073 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003074 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3075 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3076 }
Colin Cross7113d202019-11-20 16:39:12 -08003077 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003078 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3079 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3080 }
3081}
Colin Crosseabaedd2020-02-06 17:01:55 -08003082
3083func TestProductVariableDefaults(t *testing.T) {
3084 bp := `
3085 cc_defaults {
3086 name: "libfoo_defaults",
3087 srcs: ["foo.c"],
3088 cppflags: ["-DFOO"],
3089 product_variables: {
3090 debuggable: {
3091 cppflags: ["-DBAR"],
3092 },
3093 },
3094 }
3095
3096 cc_library {
3097 name: "libfoo",
3098 defaults: ["libfoo_defaults"],
3099 }
3100 `
3101
3102 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3103 config.TestProductVariables.Debuggable = BoolPtr(true)
3104
3105 ctx := CreateTestContext()
3106 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
3107 ctx.BottomUp("variable", android.VariableMutator).Parallel()
3108 })
3109 ctx.Register(config)
3110
3111 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3112 android.FailIfErrored(t, errs)
3113 _, errs = ctx.PrepareBuildActions(config)
3114 android.FailIfErrored(t, errs)
3115
3116 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
3117 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
3118 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
3119 }
3120}