blob: e0d464093848f1c6cc4e3e010c2d5c04881794c3 [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
Bill Peckham945441c2020-08-31 16:07:58 -0700261func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool) {
262 t.Helper()
Jooyung Han39edb6c2019-11-06 16:53:07 +0900263 mod, ok := ctx.ModuleForTests(moduleName, variant).Module().(android.OutputFileProducer)
264 if !ok {
265 t.Errorf("%q must have output\n", moduleName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900266 return
267 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900268 outputFiles, err := mod.OutputFiles("")
269 if err != nil || len(outputFiles) != 1 {
270 t.Errorf("%q must have single output\n", moduleName)
271 return
272 }
273 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900274
Bill Peckham945441c2020-08-31 16:07:58 -0700275 if include {
276 out := singleton.Output(snapshotPath)
277 if out.Input.String() != outputFiles[0].String() {
278 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
279 }
280 } else {
281 out := singleton.MaybeOutput(snapshotPath)
282 if out.Rule != nil {
283 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
284 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900285 }
286}
287
Bill Peckham945441c2020-08-31 16:07:58 -0700288func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
289 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true)
290}
291
292func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
293 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false)
294}
295
Jooyung Han2216fb12019-11-06 16:46:15 +0900296func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
297 t.Helper()
298 assertString(t, params.Rule.String(), android.WriteFile.String())
299 actual := strings.FieldsFunc(strings.ReplaceAll(params.Args["content"], "\\n", "\n"), func(r rune) bool { return r == '\n' })
300 assertArrayString(t, actual, expected)
301}
302
Jooyung Han097087b2019-10-22 19:32:18 +0900303func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
304 t.Helper()
305 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900306 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
307}
308
309func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
310 t.Helper()
311 vndkLibraries := ctx.ModuleForTests(module, "")
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +0900312
313 var output string
314 if module != "vndkcorevariant.libraries.txt" {
315 output = insertVndkVersion(module, "VER")
316 } else {
317 output = module
318 }
319
Jooyung Han2216fb12019-11-06 16:46:15 +0900320 checkWriteFileOutput(t, vndkLibraries.Output(output), expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900321}
322
Logan Chienf3511742017-10-31 18:04:35 +0800323func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800324 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800325 cc_library {
326 name: "libvndk",
327 vendor_available: true,
328 vndk: {
329 enabled: true,
330 },
331 nocrt: true,
332 }
333
334 cc_library {
335 name: "libvndk_private",
336 vendor_available: false,
337 vndk: {
338 enabled: true,
339 },
340 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900341 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800342 }
343
344 cc_library {
345 name: "libvndk_sp",
346 vendor_available: true,
347 vndk: {
348 enabled: true,
349 support_system_process: true,
350 },
351 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900352 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800353 }
354
355 cc_library {
356 name: "libvndk_sp_private",
357 vendor_available: false,
358 vndk: {
359 enabled: true,
360 support_system_process: true,
361 },
362 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900363 target: {
364 vendor: {
365 suffix: "-x",
366 },
367 },
Logan Chienf3511742017-10-31 18:04:35 +0800368 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900369 vndk_libraries_txt {
370 name: "llndk.libraries.txt",
371 }
372 vndk_libraries_txt {
373 name: "vndkcore.libraries.txt",
374 }
375 vndk_libraries_txt {
376 name: "vndksp.libraries.txt",
377 }
378 vndk_libraries_txt {
379 name: "vndkprivate.libraries.txt",
380 }
381 vndk_libraries_txt {
382 name: "vndkcorevariant.libraries.txt",
383 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800384 `
385
386 config := TestConfig(buildDir, android.Android, nil, bp, nil)
387 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
388 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
389
390 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800391
Justin Yun0ecf0b22020-02-28 15:07:59 +0900392 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "", vendorVariant)
393 checkVndkModule(t, ctx, "libvndk_private", "vndk-VER", false, "", vendorVariant)
394 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "", vendorVariant)
395 checkVndkModule(t, ctx, "libvndk_sp_private", "vndk-sp-VER", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900396
397 // Check VNDK snapshot output.
398
399 snapshotDir := "vndk-snapshot"
400 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
401
402 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
403 "arm64", "armv8-a"))
404 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
405 "arm", "armv7-a-neon"))
406
407 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
408 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
409 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
410 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
411
Colin Crossfb0c16e2019-11-20 17:12:35 -0800412 variant := "android_vendor.VER_arm64_armv8-a_shared"
413 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900414
Inseob Kim7f283f42020-06-01 21:53:49 +0900415 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
416
417 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
418 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
419 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
420 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900421
Jooyung Han39edb6c2019-11-06 16:53:07 +0900422 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900423 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
424 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
425 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
426 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900427
Jooyung Han097087b2019-10-22 19:32:18 +0900428 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
429 "LLNDK: libc.so",
430 "LLNDK: libdl.so",
431 "LLNDK: libft2.so",
432 "LLNDK: libm.so",
433 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900434 "VNDK-SP: libvndk_sp-x.so",
435 "VNDK-SP: libvndk_sp_private-x.so",
436 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900437 "VNDK-core: libvndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900438 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900439 "VNDK-private: libvndk-private.so",
440 "VNDK-private: libvndk_sp_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900441 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900442 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
443 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so"})
444 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so"})
445 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so"})
446 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
447}
448
Yo Chiangbba545e2020-06-09 16:15:37 +0800449func TestVndkWithHostSupported(t *testing.T) {
450 ctx := testCc(t, `
451 cc_library {
452 name: "libvndk_host_supported",
453 vendor_available: true,
454 vndk: {
455 enabled: true,
456 },
457 host_supported: true,
458 }
459
460 cc_library {
461 name: "libvndk_host_supported_but_disabled_on_device",
462 vendor_available: true,
463 vndk: {
464 enabled: true,
465 },
466 host_supported: true,
467 enabled: false,
468 target: {
469 host: {
470 enabled: true,
471 }
472 }
473 }
474
475 vndk_libraries_txt {
476 name: "vndkcore.libraries.txt",
477 }
478 `)
479
480 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
481}
482
Jooyung Han2216fb12019-11-06 16:46:15 +0900483func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800484 bp := `
Jooyung Han2216fb12019-11-06 16:46:15 +0900485 vndk_libraries_txt {
486 name: "llndk.libraries.txt",
Colin Cross98be1bb2019-12-13 20:41:13 -0800487 }`
488 config := TestConfig(buildDir, android.Android, nil, bp, nil)
489 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
490 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
491 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900492
493 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900494 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900495 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900496}
497
498func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800499 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900500 cc_library {
501 name: "libvndk",
502 vendor_available: true,
503 vndk: {
504 enabled: true,
505 },
506 nocrt: true,
507 }
508
509 cc_library {
510 name: "libvndk_sp",
511 vendor_available: true,
512 vndk: {
513 enabled: true,
514 support_system_process: true,
515 },
516 nocrt: true,
517 }
518
519 cc_library {
520 name: "libvndk2",
521 vendor_available: false,
522 vndk: {
523 enabled: true,
524 },
525 nocrt: true,
526 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900527
528 vndk_libraries_txt {
529 name: "vndkcorevariant.libraries.txt",
530 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800531 `
532
533 config := TestConfig(buildDir, android.Android, nil, bp, nil)
534 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
535 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
536 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
537
538 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
539
540 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900541
Jooyung Han2216fb12019-11-06 16:46:15 +0900542 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900543}
544
Chris Parsons79d66a52020-06-05 17:26:16 -0400545func TestDataLibs(t *testing.T) {
546 bp := `
547 cc_test_library {
548 name: "test_lib",
549 srcs: ["test_lib.cpp"],
550 gtest: false,
551 }
552
553 cc_test {
554 name: "main_test",
555 data_libs: ["test_lib"],
556 gtest: false,
557 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400558 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400559
560 config := TestConfig(buildDir, android.Android, nil, bp, nil)
561 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
562 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
563 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
564
565 ctx := testCcWithConfig(t, config)
566 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
567 testBinary := module.(*Module).linker.(*testBinary)
568 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
569 if err != nil {
570 t.Errorf("Expected cc_test to produce output files, error: %s", err)
571 return
572 }
573 if len(outputFiles) != 1 {
574 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
575 return
576 }
577 if len(testBinary.dataPaths()) != 1 {
578 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
579 return
580 }
581
582 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400583 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400584
585 if !strings.HasSuffix(outputPath, "/main_test") {
586 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
587 return
588 }
589 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
590 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
591 return
592 }
593}
594
Chris Parsons216e10a2020-07-09 17:12:52 -0400595func TestDataLibsRelativeInstallPath(t *testing.T) {
596 bp := `
597 cc_test_library {
598 name: "test_lib",
599 srcs: ["test_lib.cpp"],
600 relative_install_path: "foo/bar/baz",
601 gtest: false,
602 }
603
604 cc_test {
605 name: "main_test",
606 data_libs: ["test_lib"],
607 gtest: false,
608 }
609 `
610
611 config := TestConfig(buildDir, android.Android, nil, bp, nil)
612 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
613 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
614 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
615
616 ctx := testCcWithConfig(t, config)
617 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
618 testBinary := module.(*Module).linker.(*testBinary)
619 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
620 if err != nil {
621 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
622 }
623 if len(outputFiles) != 1 {
624 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
625 }
626 if len(testBinary.dataPaths()) != 1 {
627 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
628 }
629
630 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400631
632 if !strings.HasSuffix(outputPath, "/main_test") {
633 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
634 }
635 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
636 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
637 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400638 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400639 }
640}
641
Jooyung Han0302a842019-10-30 18:43:49 +0900642func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900643 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900644 cc_library {
645 name: "libvndk",
646 vendor_available: true,
647 vndk: {
648 enabled: true,
649 },
650 nocrt: true,
651 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900652 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900653
654 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
655 "LLNDK: libc.so",
656 "LLNDK: libdl.so",
657 "LLNDK: libft2.so",
658 "LLNDK: libm.so",
659 "VNDK-SP: libc++.so",
660 "VNDK-core: libvndk.so",
661 "VNDK-private: libft2.so",
662 })
Logan Chienf3511742017-10-31 18:04:35 +0800663}
664
Logan Chiend3c59a22018-03-29 14:08:15 +0800665func TestVndkDepError(t *testing.T) {
666 // Check whether an error is emitted when a VNDK lib depends on a system lib.
667 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
668 cc_library {
669 name: "libvndk",
670 vendor_available: true,
671 vndk: {
672 enabled: true,
673 },
674 shared_libs: ["libfwk"], // Cause error
675 nocrt: true,
676 }
677
678 cc_library {
679 name: "libfwk",
680 nocrt: true,
681 }
682 `)
683
684 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
685 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
686 cc_library {
687 name: "libvndk",
688 vendor_available: true,
689 vndk: {
690 enabled: true,
691 },
692 shared_libs: ["libvendor"], // Cause error
693 nocrt: true,
694 }
695
696 cc_library {
697 name: "libvendor",
698 vendor: true,
699 nocrt: true,
700 }
701 `)
702
703 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
704 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
705 cc_library {
706 name: "libvndk_sp",
707 vendor_available: true,
708 vndk: {
709 enabled: true,
710 support_system_process: true,
711 },
712 shared_libs: ["libfwk"], // Cause error
713 nocrt: true,
714 }
715
716 cc_library {
717 name: "libfwk",
718 nocrt: true,
719 }
720 `)
721
722 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
723 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
724 cc_library {
725 name: "libvndk_sp",
726 vendor_available: true,
727 vndk: {
728 enabled: true,
729 support_system_process: true,
730 },
731 shared_libs: ["libvendor"], // Cause error
732 nocrt: true,
733 }
734
735 cc_library {
736 name: "libvendor",
737 vendor: true,
738 nocrt: true,
739 }
740 `)
741
742 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
743 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
744 cc_library {
745 name: "libvndk_sp",
746 vendor_available: true,
747 vndk: {
748 enabled: true,
749 support_system_process: true,
750 },
751 shared_libs: ["libvndk"], // Cause error
752 nocrt: true,
753 }
754
755 cc_library {
756 name: "libvndk",
757 vendor_available: true,
758 vndk: {
759 enabled: true,
760 },
761 nocrt: true,
762 }
763 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900764
765 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
766 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
767 cc_library {
768 name: "libvndk",
769 vendor_available: true,
770 vndk: {
771 enabled: true,
772 },
773 shared_libs: ["libnonvndk"],
774 nocrt: true,
775 }
776
777 cc_library {
778 name: "libnonvndk",
779 vendor_available: true,
780 nocrt: true,
781 }
782 `)
783
784 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
785 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
786 cc_library {
787 name: "libvndkprivate",
788 vendor_available: false,
789 vndk: {
790 enabled: true,
791 },
792 shared_libs: ["libnonvndk"],
793 nocrt: true,
794 }
795
796 cc_library {
797 name: "libnonvndk",
798 vendor_available: true,
799 nocrt: true,
800 }
801 `)
802
803 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
804 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
805 cc_library {
806 name: "libvndksp",
807 vendor_available: true,
808 vndk: {
809 enabled: true,
810 support_system_process: true,
811 },
812 shared_libs: ["libnonvndk"],
813 nocrt: true,
814 }
815
816 cc_library {
817 name: "libnonvndk",
818 vendor_available: true,
819 nocrt: true,
820 }
821 `)
822
823 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
824 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
825 cc_library {
826 name: "libvndkspprivate",
827 vendor_available: false,
828 vndk: {
829 enabled: true,
830 support_system_process: true,
831 },
832 shared_libs: ["libnonvndk"],
833 nocrt: true,
834 }
835
836 cc_library {
837 name: "libnonvndk",
838 vendor_available: true,
839 nocrt: true,
840 }
841 `)
842}
843
844func TestDoubleLoadbleDep(t *testing.T) {
845 // okay to link : LLNDK -> double_loadable VNDK
846 testCc(t, `
847 cc_library {
848 name: "libllndk",
849 shared_libs: ["libdoubleloadable"],
850 }
851
852 llndk_library {
853 name: "libllndk",
854 symbol_file: "",
855 }
856
857 cc_library {
858 name: "libdoubleloadable",
859 vendor_available: true,
860 vndk: {
861 enabled: true,
862 },
863 double_loadable: true,
864 }
865 `)
866 // okay to link : LLNDK -> VNDK-SP
867 testCc(t, `
868 cc_library {
869 name: "libllndk",
870 shared_libs: ["libvndksp"],
871 }
872
873 llndk_library {
874 name: "libllndk",
875 symbol_file: "",
876 }
877
878 cc_library {
879 name: "libvndksp",
880 vendor_available: true,
881 vndk: {
882 enabled: true,
883 support_system_process: true,
884 },
885 }
886 `)
887 // okay to link : double_loadable -> double_loadable
888 testCc(t, `
889 cc_library {
890 name: "libdoubleloadable1",
891 shared_libs: ["libdoubleloadable2"],
892 vendor_available: true,
893 double_loadable: true,
894 }
895
896 cc_library {
897 name: "libdoubleloadable2",
898 vendor_available: true,
899 double_loadable: true,
900 }
901 `)
902 // okay to link : double_loadable VNDK -> double_loadable VNDK private
903 testCc(t, `
904 cc_library {
905 name: "libdoubleloadable",
906 vendor_available: true,
907 vndk: {
908 enabled: true,
909 },
910 double_loadable: true,
911 shared_libs: ["libnondoubleloadable"],
912 }
913
914 cc_library {
915 name: "libnondoubleloadable",
916 vendor_available: false,
917 vndk: {
918 enabled: true,
919 },
920 double_loadable: true,
921 }
922 `)
923 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
924 testCc(t, `
925 cc_library {
926 name: "libllndk",
927 shared_libs: ["libcoreonly"],
928 }
929
930 llndk_library {
931 name: "libllndk",
932 symbol_file: "",
933 }
934
935 cc_library {
936 name: "libcoreonly",
937 shared_libs: ["libvendoravailable"],
938 }
939
940 // indirect dependency of LLNDK
941 cc_library {
942 name: "libvendoravailable",
943 vendor_available: true,
944 double_loadable: true,
945 }
946 `)
947}
948
Inseob Kim5f58ff72020-09-07 19:53:31 +0900949func TestVendorSnapshotCapture(t *testing.T) {
Inseob Kim8471cda2019-11-15 09:59:12 +0900950 bp := `
951 cc_library {
952 name: "libvndk",
953 vendor_available: true,
954 vndk: {
955 enabled: true,
956 },
957 nocrt: true,
958 }
959
960 cc_library {
961 name: "libvendor",
962 vendor: true,
963 nocrt: true,
964 }
965
966 cc_library {
967 name: "libvendor_available",
968 vendor_available: true,
969 nocrt: true,
970 }
971
972 cc_library_headers {
973 name: "libvendor_headers",
974 vendor_available: true,
975 nocrt: true,
976 }
977
978 cc_binary {
979 name: "vendor_bin",
980 vendor: true,
981 nocrt: true,
982 }
983
984 cc_binary {
985 name: "vendor_available_bin",
986 vendor_available: true,
987 nocrt: true,
988 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900989
990 toolchain_library {
991 name: "libb",
992 vendor_available: true,
993 src: "libb.a",
994 }
Inseob Kim1042d292020-06-01 23:23:05 +0900995
996 cc_object {
997 name: "obj",
998 vendor_available: true,
999 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001000`
1001 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1002 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1003 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1004 ctx := testCcWithConfig(t, config)
1005
1006 // Check Vendor snapshot output.
1007
1008 snapshotDir := "vendor-snapshot"
1009 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +09001010 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1011
1012 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +09001013
1014 for _, arch := range [][]string{
1015 []string{"arm64", "armv8-a"},
1016 []string{"arm", "armv7-a-neon"},
1017 } {
1018 archType := arch[0]
1019 archVariant := arch[1]
1020 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1021
1022 // For shared libraries, only non-VNDK vendor_available modules are captured
1023 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1024 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +09001025 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1026 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
1027 jsonFiles = append(jsonFiles,
1028 filepath.Join(sharedDir, "libvendor.so.json"),
1029 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001030
1031 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
Inseob Kimc42f2f22020-07-29 20:32:10 +09001032 // Also cfi variants are captured, except for prebuilts like toolchain_library
Inseob Kim8471cda2019-11-15 09:59:12 +09001033 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001034 staticCfiVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static_cfi", archType, archVariant)
Inseob Kim8471cda2019-11-15 09:59:12 +09001035 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +09001036 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1037 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001038 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001039 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001040 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001041 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001042 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001043 jsonFiles = append(jsonFiles,
1044 filepath.Join(staticDir, "libb.a.json"),
1045 filepath.Join(staticDir, "libvndk.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001046 filepath.Join(staticDir, "libvndk.cfi.a.json"),
Inseob Kim7f283f42020-06-01 21:53:49 +09001047 filepath.Join(staticDir, "libvendor.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001048 filepath.Join(staticDir, "libvendor.cfi.a.json"),
1049 filepath.Join(staticDir, "libvendor_available.a.json"),
1050 filepath.Join(staticDir, "libvendor_available.cfi.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001051
Inseob Kim7f283f42020-06-01 21:53:49 +09001052 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +09001053 if archType == "arm64" {
1054 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1055 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +09001056 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
1057 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
1058 jsonFiles = append(jsonFiles,
1059 filepath.Join(binaryDir, "vendor_bin.json"),
1060 filepath.Join(binaryDir, "vendor_available_bin.json"))
1061 }
1062
1063 // For header libraries, all vendor:true and vendor_available modules are captured.
1064 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1065 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +09001066
1067 // For object modules, all vendor:true and vendor_available modules are captured.
1068 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1069 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1070 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1071 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001072 }
1073
1074 for _, jsonFile := range jsonFiles {
1075 // verify all json files exist
1076 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1077 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001078 }
1079 }
1080}
1081
Inseob Kim5f58ff72020-09-07 19:53:31 +09001082func TestVendorSnapshotUse(t *testing.T) {
1083 frameworkBp := `
1084 cc_library {
1085 name: "libvndk",
1086 vendor_available: true,
1087 vndk: {
1088 enabled: true,
1089 },
1090 nocrt: true,
1091 compile_multilib: "64",
1092 }
1093
1094 cc_library {
1095 name: "libvendor",
1096 vendor: true,
1097 nocrt: true,
1098 no_libcrt: true,
1099 stl: "none",
1100 system_shared_libs: [],
1101 compile_multilib: "64",
1102 }
1103
1104 cc_binary {
1105 name: "bin",
1106 vendor: true,
1107 nocrt: true,
1108 no_libcrt: true,
1109 stl: "none",
1110 system_shared_libs: [],
1111 compile_multilib: "64",
1112 }
1113`
1114
1115 vndkBp := `
1116 vndk_prebuilt_shared {
1117 name: "libvndk",
1118 version: "BOARD",
1119 target_arch: "arm64",
1120 vendor_available: true,
1121 vndk: {
1122 enabled: true,
1123 },
1124 arch: {
1125 arm64: {
1126 srcs: ["libvndk.so"],
1127 },
1128 },
1129 }
1130`
1131
1132 vendorProprietaryBp := `
1133 cc_library {
1134 name: "libvendor_without_snapshot",
1135 vendor: true,
1136 nocrt: true,
1137 no_libcrt: true,
1138 stl: "none",
1139 system_shared_libs: [],
1140 compile_multilib: "64",
1141 }
1142
1143 cc_library_shared {
1144 name: "libclient",
1145 vendor: true,
1146 nocrt: true,
1147 no_libcrt: true,
1148 stl: "none",
1149 system_shared_libs: [],
1150 shared_libs: ["libvndk"],
1151 static_libs: ["libvendor", "libvendor_without_snapshot"],
1152 compile_multilib: "64",
1153 }
1154
1155 cc_binary {
1156 name: "bin_without_snapshot",
1157 vendor: true,
1158 nocrt: true,
1159 no_libcrt: true,
1160 stl: "none",
1161 system_shared_libs: [],
1162 static_libs: ["libvndk"],
1163 compile_multilib: "64",
1164 }
1165
1166 vendor_snapshot_static {
1167 name: "libvndk",
1168 version: "BOARD",
1169 target_arch: "arm64",
1170 vendor: true,
1171 arch: {
1172 arm64: {
1173 src: "libvndk.a",
1174 },
1175 },
1176 }
1177
1178 vendor_snapshot_shared {
1179 name: "libvendor",
1180 version: "BOARD",
1181 target_arch: "arm64",
1182 vendor: true,
1183 arch: {
1184 arm64: {
1185 src: "libvendor.so",
1186 },
1187 },
1188 }
1189
1190 vendor_snapshot_static {
1191 name: "libvendor",
1192 version: "BOARD",
1193 target_arch: "arm64",
1194 vendor: true,
1195 arch: {
1196 arm64: {
1197 src: "libvendor.a",
1198 },
1199 },
1200 }
1201
1202 vendor_snapshot_binary {
1203 name: "bin",
1204 version: "BOARD",
1205 target_arch: "arm64",
1206 vendor: true,
1207 arch: {
1208 arm64: {
1209 src: "bin",
1210 },
1211 },
1212 }
1213`
1214 depsBp := GatherRequiredDepsForTest(android.Android)
1215
1216 mockFS := map[string][]byte{
1217 "deps/Android.bp": []byte(depsBp),
1218 "framework/Android.bp": []byte(frameworkBp),
1219 "vendor/Android.bp": []byte(vendorProprietaryBp),
1220 "vendor/libvndk.a": nil,
1221 "vendor/libvendor.a": nil,
1222 "vendor/libvendor.so": nil,
1223 "vendor/bin": nil,
1224 "vndk/Android.bp": []byte(vndkBp),
1225 "vndk/libvndk.so": nil,
1226 }
1227
1228 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1229 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1230 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1231 ctx := CreateTestContext()
1232 ctx.Register(config)
1233
1234 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "vendor/Android.bp", "vndk/Android.bp"})
1235 android.FailIfErrored(t, errs)
1236 _, errs = ctx.PrepareBuildActions(config)
1237 android.FailIfErrored(t, errs)
1238
1239 sharedVariant := "android_vendor.BOARD_arm64_armv8-a_shared"
1240 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1241 binaryVariant := "android_vendor.BOARD_arm64_armv8-a"
1242
1243 // libclient uses libvndk.vndk.BOARD.arm64, libvendor.vendor_static.BOARD.arm64, libvendor_without_snapshot
1244 libclientLdRule := ctx.ModuleForTests("libclient", sharedVariant).Rule("ld")
1245 libclientFlags := libclientLdRule.Args["libFlags"]
1246
1247 for _, input := range [][]string{
1248 []string{sharedVariant, "libvndk.vndk.BOARD.arm64"},
1249 []string{staticVariant, "libvendor.vendor_static.BOARD.arm64"},
1250 []string{staticVariant, "libvendor_without_snapshot"},
1251 } {
1252 outputPaths := getOutputPaths(ctx, input[0] /* variant */, []string{input[1]} /* module name */)
1253 if !strings.Contains(libclientFlags, outputPaths[0].String()) {
1254 t.Errorf("libflags for libclient must contain %#v, but was %#v", outputPaths[0], libclientFlags)
1255 }
1256 }
1257
1258 // bin_without_snapshot uses libvndk.vendor_static.BOARD.arm64
1259 binWithoutSnapshotLdRule := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("ld")
1260 binWithoutSnapshotFlags := binWithoutSnapshotLdRule.Args["libFlags"]
1261 libVndkStaticOutputPaths := getOutputPaths(ctx, staticVariant, []string{"libvndk.vendor_static.BOARD.arm64"})
1262 if !strings.Contains(binWithoutSnapshotFlags, libVndkStaticOutputPaths[0].String()) {
1263 t.Errorf("libflags for bin_without_snapshot must contain %#v, but was %#v",
1264 libVndkStaticOutputPaths[0], binWithoutSnapshotFlags)
1265 }
1266
1267 // libvendor.so is installed by libvendor.vendor_shared.BOARD.arm64
1268 ctx.ModuleForTests("libvendor.vendor_shared.BOARD.arm64", sharedVariant).Output("libvendor.so")
1269
1270 // libvendor_without_snapshot.so is installed by libvendor_without_snapshot
1271 ctx.ModuleForTests("libvendor_without_snapshot", sharedVariant).Output("libvendor_without_snapshot.so")
1272
1273 // bin is installed by bin.vendor_binary.BOARD.arm64
1274 ctx.ModuleForTests("bin.vendor_binary.BOARD.arm64", binaryVariant).Output("bin")
1275
1276 // bin_without_snapshot is installed by bin_without_snapshot
1277 ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Output("bin_without_snapshot")
1278
1279 // libvendor and bin don't have vendor.BOARD variant
1280 libvendorVariants := ctx.ModuleVariantsForTests("libvendor")
1281 if inList(sharedVariant, libvendorVariants) {
1282 t.Errorf("libvendor must not have variant %#v, but it does", sharedVariant)
1283 }
1284
1285 binVariants := ctx.ModuleVariantsForTests("bin")
1286 if inList(binaryVariant, binVariants) {
1287 t.Errorf("bin must not have variant %#v, but it does", sharedVariant)
1288 }
1289}
1290
Inseob Kimc42f2f22020-07-29 20:32:10 +09001291func TestVendorSnapshotSanitizer(t *testing.T) {
1292 bp := `
1293 vendor_snapshot_static {
1294 name: "libsnapshot",
1295 vendor: true,
1296 target_arch: "arm64",
1297 version: "BOARD",
1298 arch: {
1299 arm64: {
1300 src: "libsnapshot.a",
1301 cfi: {
1302 src: "libsnapshot.cfi.a",
1303 }
1304 },
1305 },
1306 }
1307`
1308 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1309 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1310 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1311 ctx := testCcWithConfig(t, config)
1312
1313 // Check non-cfi and cfi variant.
1314 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1315 staticCfiVariant := "android_vendor.BOARD_arm64_armv8-a_static_cfi"
1316
1317 staticModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticVariant).Module().(*Module)
1318 assertString(t, staticModule.outputFile.Path().Base(), "libsnapshot.a")
1319
1320 staticCfiModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticCfiVariant).Module().(*Module)
1321 assertString(t, staticCfiModule.outputFile.Path().Base(), "libsnapshot.cfi.a")
1322}
1323
Bill Peckham945441c2020-08-31 16:07:58 -07001324func assertExcludeFromVendorSnapshotIs(t *testing.T, c *Module, expected bool) {
1325 t.Helper()
1326 if c.ExcludeFromVendorSnapshot() != expected {
1327 t.Errorf("expected %q ExcludeFromVendorSnapshot to be %t", c.String(), expected)
1328 }
1329}
1330
1331func TestVendorSnapshotExclude(t *testing.T) {
1332
1333 // This test verifies that the exclude_from_vendor_snapshot property
1334 // makes its way from the Android.bp source file into the module data
1335 // structure. It also verifies that modules are correctly included or
1336 // excluded in the vendor snapshot based on their path (framework or
1337 // vendor) and the exclude_from_vendor_snapshot property.
1338
1339 frameworkBp := `
1340 cc_library_shared {
1341 name: "libinclude",
1342 srcs: ["src/include.cpp"],
1343 vendor_available: true,
1344 }
1345 cc_library_shared {
1346 name: "libexclude",
1347 srcs: ["src/exclude.cpp"],
1348 vendor: true,
1349 exclude_from_vendor_snapshot: true,
1350 }
1351 `
1352
1353 vendorProprietaryBp := `
1354 cc_library_shared {
1355 name: "libvendor",
1356 srcs: ["vendor.cpp"],
1357 vendor: true,
1358 }
1359 `
1360
1361 depsBp := GatherRequiredDepsForTest(android.Android)
1362
1363 mockFS := map[string][]byte{
1364 "deps/Android.bp": []byte(depsBp),
1365 "framework/Android.bp": []byte(frameworkBp),
1366 "framework/include.cpp": nil,
1367 "framework/exclude.cpp": nil,
1368 "device/Android.bp": []byte(vendorProprietaryBp),
1369 "device/vendor.cpp": nil,
1370 }
1371
1372 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1373 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1374 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1375 ctx := CreateTestContext()
1376 ctx.Register(config)
1377
1378 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1379 android.FailIfErrored(t, errs)
1380 _, errs = ctx.PrepareBuildActions(config)
1381 android.FailIfErrored(t, errs)
1382
1383 // Test an include and exclude framework module.
1384 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1385 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", vendorVariant).Module().(*Module), false)
1386 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libexclude", vendorVariant).Module().(*Module), true)
1387
1388 // A vendor module is excluded, but by its path, not the
1389 // exclude_from_vendor_snapshot property.
1390 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libvendor", vendorVariant).Module().(*Module), false)
1391
1392 // Verify the content of the vendor snapshot.
1393
1394 snapshotDir := "vendor-snapshot"
1395 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1396 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1397
1398 var includeJsonFiles []string
1399 var excludeJsonFiles []string
1400
1401 for _, arch := range [][]string{
1402 []string{"arm64", "armv8-a"},
1403 []string{"arm", "armv7-a-neon"},
1404 } {
1405 archType := arch[0]
1406 archVariant := arch[1]
1407 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1408
1409 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1410 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1411
1412 // Included modules
1413 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1414 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1415
1416 // Excluded modules
1417 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1418 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1419 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1420 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1421 }
1422
1423 // Verify that each json file for an included module has a rule.
1424 for _, jsonFile := range includeJsonFiles {
1425 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1426 t.Errorf("include json file %q not found", jsonFile)
1427 }
1428 }
1429
1430 // Verify that each json file for an excluded module has no rule.
1431 for _, jsonFile := range excludeJsonFiles {
1432 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1433 t.Errorf("exclude json file %q found", jsonFile)
1434 }
1435 }
1436}
1437
1438func TestVendorSnapshotExcludeInVendorProprietaryPathErrors(t *testing.T) {
1439
1440 // This test verifies that using the exclude_from_vendor_snapshot
1441 // property on a module in a vendor proprietary path generates an
1442 // error. These modules are already excluded, so we prohibit using the
1443 // property in this way, which could add to confusion.
1444
1445 vendorProprietaryBp := `
1446 cc_library_shared {
1447 name: "libvendor",
1448 srcs: ["vendor.cpp"],
1449 vendor: true,
1450 exclude_from_vendor_snapshot: true,
1451 }
1452 `
1453
1454 depsBp := GatherRequiredDepsForTest(android.Android)
1455
1456 mockFS := map[string][]byte{
1457 "deps/Android.bp": []byte(depsBp),
1458 "device/Android.bp": []byte(vendorProprietaryBp),
1459 "device/vendor.cpp": nil,
1460 }
1461
1462 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1463 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1464 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1465 ctx := CreateTestContext()
1466 ctx.Register(config)
1467
1468 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "device/Android.bp"})
1469 android.FailIfErrored(t, errs)
1470
1471 _, errs = ctx.PrepareBuildActions(config)
1472 android.CheckErrorsAgainstExpectations(t, errs, []string{
1473 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1474 `module "libvendor\{.+,image:vendor.+,arch:arm_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1475 })
1476}
1477
1478func TestVendorSnapshotExcludeWithVendorAvailable(t *testing.T) {
1479
1480 // This test verifies that using the exclude_from_vendor_snapshot
1481 // property on a module that is vendor available generates an error. A
1482 // vendor available module must be captured in the vendor snapshot and
1483 // must not built from source when building the vendor image against
1484 // the vendor snapshot.
1485
1486 frameworkBp := `
1487 cc_library_shared {
1488 name: "libinclude",
1489 srcs: ["src/include.cpp"],
1490 vendor_available: true,
1491 exclude_from_vendor_snapshot: true,
1492 }
1493 `
1494
1495 depsBp := GatherRequiredDepsForTest(android.Android)
1496
1497 mockFS := map[string][]byte{
1498 "deps/Android.bp": []byte(depsBp),
1499 "framework/Android.bp": []byte(frameworkBp),
1500 "framework/include.cpp": nil,
1501 }
1502
1503 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1504 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1505 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1506 ctx := CreateTestContext()
1507 ctx.Register(config)
1508
1509 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp"})
1510 android.FailIfErrored(t, errs)
1511
1512 _, errs = ctx.PrepareBuildActions(config)
1513 android.CheckErrorsAgainstExpectations(t, errs, []string{
1514 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1515 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1516 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1517 `module "libinclude\{.+,image:vendor.+,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1518 })
1519}
1520
Jooyung Hana70f0672019-01-18 15:20:43 +09001521func TestDoubleLoadableDepError(t *testing.T) {
1522 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1523 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1524 cc_library {
1525 name: "libllndk",
1526 shared_libs: ["libnondoubleloadable"],
1527 }
1528
1529 llndk_library {
1530 name: "libllndk",
1531 symbol_file: "",
1532 }
1533
1534 cc_library {
1535 name: "libnondoubleloadable",
1536 vendor_available: true,
1537 vndk: {
1538 enabled: true,
1539 },
1540 }
1541 `)
1542
1543 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1544 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1545 cc_library {
1546 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001547 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001548 shared_libs: ["libnondoubleloadable"],
1549 }
1550
1551 llndk_library {
1552 name: "libllndk",
1553 symbol_file: "",
1554 }
1555
1556 cc_library {
1557 name: "libnondoubleloadable",
1558 vendor_available: true,
1559 }
1560 `)
1561
1562 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
1563 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1564 cc_library {
1565 name: "libdoubleloadable",
1566 vendor_available: true,
1567 double_loadable: true,
1568 shared_libs: ["libnondoubleloadable"],
1569 }
1570
1571 cc_library {
1572 name: "libnondoubleloadable",
1573 vendor_available: true,
1574 }
1575 `)
1576
1577 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
1578 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1579 cc_library {
1580 name: "libdoubleloadable",
1581 vendor_available: true,
1582 double_loadable: true,
1583 shared_libs: ["libnondoubleloadable"],
1584 }
1585
1586 cc_library {
1587 name: "libnondoubleloadable",
1588 vendor_available: true,
1589 vndk: {
1590 enabled: true,
1591 },
1592 }
1593 `)
1594
1595 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
1596 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1597 cc_library {
1598 name: "libdoubleloadable",
1599 vendor_available: true,
1600 vndk: {
1601 enabled: true,
1602 },
1603 double_loadable: true,
1604 shared_libs: ["libnondoubleloadable"],
1605 }
1606
1607 cc_library {
1608 name: "libnondoubleloadable",
1609 vendor_available: false,
1610 vndk: {
1611 enabled: true,
1612 },
1613 }
1614 `)
1615
1616 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1617 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1618 cc_library {
1619 name: "libllndk",
1620 shared_libs: ["libcoreonly"],
1621 }
1622
1623 llndk_library {
1624 name: "libllndk",
1625 symbol_file: "",
1626 }
1627
1628 cc_library {
1629 name: "libcoreonly",
1630 shared_libs: ["libvendoravailable"],
1631 }
1632
1633 // indirect dependency of LLNDK
1634 cc_library {
1635 name: "libvendoravailable",
1636 vendor_available: true,
1637 }
1638 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001639}
1640
Logan Chienf3511742017-10-31 18:04:35 +08001641func TestVndkExt(t *testing.T) {
1642 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001643 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001644 cc_library {
1645 name: "libvndk",
1646 vendor_available: true,
1647 vndk: {
1648 enabled: true,
1649 },
1650 nocrt: true,
1651 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001652 cc_library {
1653 name: "libvndk2",
1654 vendor_available: true,
1655 vndk: {
1656 enabled: true,
1657 },
1658 target: {
1659 vendor: {
1660 suffix: "-suffix",
1661 },
1662 },
1663 nocrt: true,
1664 }
Logan Chienf3511742017-10-31 18:04:35 +08001665
1666 cc_library {
1667 name: "libvndk_ext",
1668 vendor: true,
1669 vndk: {
1670 enabled: true,
1671 extends: "libvndk",
1672 },
1673 nocrt: true,
1674 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001675
1676 cc_library {
1677 name: "libvndk2_ext",
1678 vendor: true,
1679 vndk: {
1680 enabled: true,
1681 extends: "libvndk2",
1682 },
1683 nocrt: true,
1684 }
Logan Chienf3511742017-10-31 18:04:35 +08001685
Justin Yun0ecf0b22020-02-28 15:07:59 +09001686 cc_library {
1687 name: "libvndk_ext_product",
1688 product_specific: true,
1689 vndk: {
1690 enabled: true,
1691 extends: "libvndk",
1692 },
1693 nocrt: true,
1694 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001695
Justin Yun0ecf0b22020-02-28 15:07:59 +09001696 cc_library {
1697 name: "libvndk2_ext_product",
1698 product_specific: true,
1699 vndk: {
1700 enabled: true,
1701 extends: "libvndk2",
1702 },
1703 nocrt: true,
1704 }
1705 `
1706 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1707 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1708 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1709 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1710
1711 ctx := testCcWithConfig(t, config)
1712
1713 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1714 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1715
1716 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1717 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1718
1719 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1720 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001721}
1722
Logan Chiend3c59a22018-03-29 14:08:15 +08001723func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001724 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1725 ctx := testCcNoVndk(t, `
1726 cc_library {
1727 name: "libvndk",
1728 vendor_available: true,
1729 vndk: {
1730 enabled: true,
1731 },
1732 nocrt: true,
1733 }
1734
1735 cc_library {
1736 name: "libvndk_ext",
1737 vendor: true,
1738 vndk: {
1739 enabled: true,
1740 extends: "libvndk",
1741 },
1742 nocrt: true,
1743 }
1744 `)
1745
1746 // Ensures that the core variant of "libvndk_ext" can be found.
1747 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1748 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1749 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1750 }
1751}
1752
Justin Yun0ecf0b22020-02-28 15:07:59 +09001753func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1754 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
1755 ctx := testCc(t, `
1756 cc_library {
1757 name: "libvndk",
1758 vendor_available: true,
1759 vndk: {
1760 enabled: true,
1761 },
1762 nocrt: true,
1763 }
1764
1765 cc_library {
1766 name: "libvndk_ext_product",
1767 product_specific: true,
1768 vndk: {
1769 enabled: true,
1770 extends: "libvndk",
1771 },
1772 nocrt: true,
1773 }
1774 `)
1775
1776 // Ensures that the core variant of "libvndk_ext_product" can be found.
1777 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1778 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1779 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1780 }
1781}
1782
Logan Chienf3511742017-10-31 18:04:35 +08001783func TestVndkExtError(t *testing.T) {
1784 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001785 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001786 cc_library {
1787 name: "libvndk",
1788 vendor_available: true,
1789 vndk: {
1790 enabled: true,
1791 },
1792 nocrt: true,
1793 }
1794
1795 cc_library {
1796 name: "libvndk_ext",
1797 vndk: {
1798 enabled: true,
1799 extends: "libvndk",
1800 },
1801 nocrt: true,
1802 }
1803 `)
1804
1805 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1806 cc_library {
1807 name: "libvndk",
1808 vendor_available: true,
1809 vndk: {
1810 enabled: true,
1811 },
1812 nocrt: true,
1813 }
1814
1815 cc_library {
1816 name: "libvndk_ext",
1817 vendor: true,
1818 vndk: {
1819 enabled: true,
1820 },
1821 nocrt: true,
1822 }
1823 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001824
1825 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1826 cc_library {
1827 name: "libvndk",
1828 vendor_available: true,
1829 vndk: {
1830 enabled: true,
1831 },
1832 nocrt: true,
1833 }
1834
1835 cc_library {
1836 name: "libvndk_ext_product",
1837 product_specific: true,
1838 vndk: {
1839 enabled: true,
1840 },
1841 nocrt: true,
1842 }
1843 `)
1844
1845 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1846 cc_library {
1847 name: "libvndk",
1848 vendor_available: true,
1849 vndk: {
1850 enabled: true,
1851 },
1852 nocrt: true,
1853 }
1854
1855 cc_library {
1856 name: "libvndk_ext_product",
1857 product_specific: true,
1858 vendor_available: true,
1859 vndk: {
1860 enabled: true,
1861 extends: "libvndk",
1862 },
1863 nocrt: true,
1864 }
1865 `)
Logan Chienf3511742017-10-31 18:04:35 +08001866}
1867
1868func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1869 // This test ensures an error is emitted for inconsistent support_system_process.
1870 testCcError(t, "module \".*\" with mismatched support_system_process", `
1871 cc_library {
1872 name: "libvndk",
1873 vendor_available: true,
1874 vndk: {
1875 enabled: true,
1876 },
1877 nocrt: true,
1878 }
1879
1880 cc_library {
1881 name: "libvndk_sp_ext",
1882 vendor: true,
1883 vndk: {
1884 enabled: true,
1885 extends: "libvndk",
1886 support_system_process: true,
1887 },
1888 nocrt: true,
1889 }
1890 `)
1891
1892 testCcError(t, "module \".*\" with mismatched support_system_process", `
1893 cc_library {
1894 name: "libvndk_sp",
1895 vendor_available: true,
1896 vndk: {
1897 enabled: true,
1898 support_system_process: true,
1899 },
1900 nocrt: true,
1901 }
1902
1903 cc_library {
1904 name: "libvndk_ext",
1905 vendor: true,
1906 vndk: {
1907 enabled: true,
1908 extends: "libvndk_sp",
1909 },
1910 nocrt: true,
1911 }
1912 `)
1913}
1914
1915func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001916 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08001917 // with `vendor_available: false`.
1918 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1919 cc_library {
1920 name: "libvndk",
1921 vendor_available: false,
1922 vndk: {
1923 enabled: true,
1924 },
1925 nocrt: true,
1926 }
1927
1928 cc_library {
1929 name: "libvndk_ext",
1930 vendor: true,
1931 vndk: {
1932 enabled: true,
1933 extends: "libvndk",
1934 },
1935 nocrt: true,
1936 }
1937 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001938
1939 testCcErrorProductVndk(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1940 cc_library {
1941 name: "libvndk",
1942 vendor_available: false,
1943 vndk: {
1944 enabled: true,
1945 },
1946 nocrt: true,
1947 }
1948
1949 cc_library {
1950 name: "libvndk_ext_product",
1951 product_specific: true,
1952 vndk: {
1953 enabled: true,
1954 extends: "libvndk",
1955 },
1956 nocrt: true,
1957 }
1958 `)
Logan Chienf3511742017-10-31 18:04:35 +08001959}
1960
Logan Chiend3c59a22018-03-29 14:08:15 +08001961func TestVendorModuleUseVndkExt(t *testing.T) {
1962 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001963 testCc(t, `
1964 cc_library {
1965 name: "libvndk",
1966 vendor_available: true,
1967 vndk: {
1968 enabled: true,
1969 },
1970 nocrt: true,
1971 }
1972
1973 cc_library {
1974 name: "libvndk_ext",
1975 vendor: true,
1976 vndk: {
1977 enabled: true,
1978 extends: "libvndk",
1979 },
1980 nocrt: true,
1981 }
1982
1983 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001984 name: "libvndk_sp",
1985 vendor_available: true,
1986 vndk: {
1987 enabled: true,
1988 support_system_process: true,
1989 },
1990 nocrt: true,
1991 }
1992
1993 cc_library {
1994 name: "libvndk_sp_ext",
1995 vendor: true,
1996 vndk: {
1997 enabled: true,
1998 extends: "libvndk_sp",
1999 support_system_process: true,
2000 },
2001 nocrt: true,
2002 }
2003
2004 cc_library {
2005 name: "libvendor",
2006 vendor: true,
2007 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
2008 nocrt: true,
2009 }
2010 `)
2011}
2012
Logan Chiend3c59a22018-03-29 14:08:15 +08002013func TestVndkExtUseVendorLib(t *testing.T) {
2014 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08002015 testCc(t, `
2016 cc_library {
2017 name: "libvndk",
2018 vendor_available: true,
2019 vndk: {
2020 enabled: true,
2021 },
2022 nocrt: true,
2023 }
2024
2025 cc_library {
2026 name: "libvndk_ext",
2027 vendor: true,
2028 vndk: {
2029 enabled: true,
2030 extends: "libvndk",
2031 },
2032 shared_libs: ["libvendor"],
2033 nocrt: true,
2034 }
2035
2036 cc_library {
2037 name: "libvendor",
2038 vendor: true,
2039 nocrt: true,
2040 }
2041 `)
Logan Chienf3511742017-10-31 18:04:35 +08002042
Logan Chiend3c59a22018-03-29 14:08:15 +08002043 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
2044 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08002045 cc_library {
2046 name: "libvndk_sp",
2047 vendor_available: true,
2048 vndk: {
2049 enabled: true,
2050 support_system_process: true,
2051 },
2052 nocrt: true,
2053 }
2054
2055 cc_library {
2056 name: "libvndk_sp_ext",
2057 vendor: true,
2058 vndk: {
2059 enabled: true,
2060 extends: "libvndk_sp",
2061 support_system_process: true,
2062 },
2063 shared_libs: ["libvendor"], // Cause an error
2064 nocrt: true,
2065 }
2066
2067 cc_library {
2068 name: "libvendor",
2069 vendor: true,
2070 nocrt: true,
2071 }
2072 `)
2073}
2074
Justin Yun0ecf0b22020-02-28 15:07:59 +09002075func TestProductVndkExtDependency(t *testing.T) {
2076 bp := `
2077 cc_library {
2078 name: "libvndk",
2079 vendor_available: true,
2080 vndk: {
2081 enabled: true,
2082 },
2083 nocrt: true,
2084 }
2085
2086 cc_library {
2087 name: "libvndk_ext_product",
2088 product_specific: true,
2089 vndk: {
2090 enabled: true,
2091 extends: "libvndk",
2092 },
2093 shared_libs: ["libproduct_for_vndklibs"],
2094 nocrt: true,
2095 }
2096
2097 cc_library {
2098 name: "libvndk_sp",
2099 vendor_available: true,
2100 vndk: {
2101 enabled: true,
2102 support_system_process: true,
2103 },
2104 nocrt: true,
2105 }
2106
2107 cc_library {
2108 name: "libvndk_sp_ext_product",
2109 product_specific: true,
2110 vndk: {
2111 enabled: true,
2112 extends: "libvndk_sp",
2113 support_system_process: true,
2114 },
2115 shared_libs: ["libproduct_for_vndklibs"],
2116 nocrt: true,
2117 }
2118
2119 cc_library {
2120 name: "libproduct",
2121 product_specific: true,
2122 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
2123 nocrt: true,
2124 }
2125
2126 cc_library {
2127 name: "libproduct_for_vndklibs",
2128 product_specific: true,
2129 nocrt: true,
2130 }
2131 `
2132 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2133 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2134 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2135 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2136
2137 testCcWithConfig(t, config)
2138}
2139
Logan Chiend3c59a22018-03-29 14:08:15 +08002140func TestVndkSpExtUseVndkError(t *testing.T) {
2141 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
2142 // library.
2143 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2144 cc_library {
2145 name: "libvndk",
2146 vendor_available: true,
2147 vndk: {
2148 enabled: true,
2149 },
2150 nocrt: true,
2151 }
2152
2153 cc_library {
2154 name: "libvndk_sp",
2155 vendor_available: true,
2156 vndk: {
2157 enabled: true,
2158 support_system_process: true,
2159 },
2160 nocrt: true,
2161 }
2162
2163 cc_library {
2164 name: "libvndk_sp_ext",
2165 vendor: true,
2166 vndk: {
2167 enabled: true,
2168 extends: "libvndk_sp",
2169 support_system_process: true,
2170 },
2171 shared_libs: ["libvndk"], // Cause an error
2172 nocrt: true,
2173 }
2174 `)
2175
2176 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
2177 // library.
2178 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2179 cc_library {
2180 name: "libvndk",
2181 vendor_available: true,
2182 vndk: {
2183 enabled: true,
2184 },
2185 nocrt: true,
2186 }
2187
2188 cc_library {
2189 name: "libvndk_ext",
2190 vendor: true,
2191 vndk: {
2192 enabled: true,
2193 extends: "libvndk",
2194 },
2195 nocrt: true,
2196 }
2197
2198 cc_library {
2199 name: "libvndk_sp",
2200 vendor_available: true,
2201 vndk: {
2202 enabled: true,
2203 support_system_process: true,
2204 },
2205 nocrt: true,
2206 }
2207
2208 cc_library {
2209 name: "libvndk_sp_ext",
2210 vendor: true,
2211 vndk: {
2212 enabled: true,
2213 extends: "libvndk_sp",
2214 support_system_process: true,
2215 },
2216 shared_libs: ["libvndk_ext"], // Cause an error
2217 nocrt: true,
2218 }
2219 `)
2220}
2221
2222func TestVndkUseVndkExtError(t *testing.T) {
2223 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2224 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002225 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2226 cc_library {
2227 name: "libvndk",
2228 vendor_available: true,
2229 vndk: {
2230 enabled: true,
2231 },
2232 nocrt: true,
2233 }
2234
2235 cc_library {
2236 name: "libvndk_ext",
2237 vendor: true,
2238 vndk: {
2239 enabled: true,
2240 extends: "libvndk",
2241 },
2242 nocrt: true,
2243 }
2244
2245 cc_library {
2246 name: "libvndk2",
2247 vendor_available: true,
2248 vndk: {
2249 enabled: true,
2250 },
2251 shared_libs: ["libvndk_ext"],
2252 nocrt: true,
2253 }
2254 `)
2255
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002256 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002257 cc_library {
2258 name: "libvndk",
2259 vendor_available: true,
2260 vndk: {
2261 enabled: true,
2262 },
2263 nocrt: true,
2264 }
2265
2266 cc_library {
2267 name: "libvndk_ext",
2268 vendor: true,
2269 vndk: {
2270 enabled: true,
2271 extends: "libvndk",
2272 },
2273 nocrt: true,
2274 }
2275
2276 cc_library {
2277 name: "libvndk2",
2278 vendor_available: true,
2279 vndk: {
2280 enabled: true,
2281 },
2282 target: {
2283 vendor: {
2284 shared_libs: ["libvndk_ext"],
2285 },
2286 },
2287 nocrt: true,
2288 }
2289 `)
2290
2291 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2292 cc_library {
2293 name: "libvndk_sp",
2294 vendor_available: true,
2295 vndk: {
2296 enabled: true,
2297 support_system_process: true,
2298 },
2299 nocrt: true,
2300 }
2301
2302 cc_library {
2303 name: "libvndk_sp_ext",
2304 vendor: true,
2305 vndk: {
2306 enabled: true,
2307 extends: "libvndk_sp",
2308 support_system_process: true,
2309 },
2310 nocrt: true,
2311 }
2312
2313 cc_library {
2314 name: "libvndk_sp_2",
2315 vendor_available: true,
2316 vndk: {
2317 enabled: true,
2318 support_system_process: true,
2319 },
2320 shared_libs: ["libvndk_sp_ext"],
2321 nocrt: true,
2322 }
2323 `)
2324
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002325 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002326 cc_library {
2327 name: "libvndk_sp",
2328 vendor_available: true,
2329 vndk: {
2330 enabled: true,
2331 },
2332 nocrt: true,
2333 }
2334
2335 cc_library {
2336 name: "libvndk_sp_ext",
2337 vendor: true,
2338 vndk: {
2339 enabled: true,
2340 extends: "libvndk_sp",
2341 },
2342 nocrt: true,
2343 }
2344
2345 cc_library {
2346 name: "libvndk_sp2",
2347 vendor_available: true,
2348 vndk: {
2349 enabled: true,
2350 },
2351 target: {
2352 vendor: {
2353 shared_libs: ["libvndk_sp_ext"],
2354 },
2355 },
2356 nocrt: true,
2357 }
2358 `)
2359}
2360
Justin Yun5f7f7e82019-11-18 19:52:14 +09002361func TestEnforceProductVndkVersion(t *testing.T) {
2362 bp := `
2363 cc_library {
2364 name: "libllndk",
2365 }
2366 llndk_library {
2367 name: "libllndk",
2368 symbol_file: "",
2369 }
2370 cc_library {
2371 name: "libvndk",
2372 vendor_available: true,
2373 vndk: {
2374 enabled: true,
2375 },
2376 nocrt: true,
2377 }
2378 cc_library {
2379 name: "libvndk_sp",
2380 vendor_available: true,
2381 vndk: {
2382 enabled: true,
2383 support_system_process: true,
2384 },
2385 nocrt: true,
2386 }
2387 cc_library {
2388 name: "libva",
2389 vendor_available: true,
2390 nocrt: true,
2391 }
2392 cc_library {
2393 name: "libproduct_va",
2394 product_specific: true,
2395 vendor_available: true,
2396 nocrt: true,
2397 }
2398 cc_library {
2399 name: "libprod",
2400 product_specific: true,
2401 shared_libs: [
2402 "libllndk",
2403 "libvndk",
2404 "libvndk_sp",
2405 "libva",
2406 "libproduct_va",
2407 ],
2408 nocrt: true,
2409 }
2410 cc_library {
2411 name: "libvendor",
2412 vendor: true,
2413 shared_libs: [
2414 "libllndk",
2415 "libvndk",
2416 "libvndk_sp",
2417 "libva",
2418 "libproduct_va",
2419 ],
2420 nocrt: true,
2421 }
2422 `
2423
2424 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2425 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2426 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2427 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2428
2429 ctx := testCcWithConfig(t, config)
2430
Justin Yun0ecf0b22020-02-28 15:07:59 +09002431 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "", productVariant)
2432 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "", productVariant)
Justin Yun5f7f7e82019-11-18 19:52:14 +09002433}
2434
2435func TestEnforceProductVndkVersionErrors(t *testing.T) {
2436 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2437 cc_library {
2438 name: "libprod",
2439 product_specific: true,
2440 shared_libs: [
2441 "libvendor",
2442 ],
2443 nocrt: true,
2444 }
2445 cc_library {
2446 name: "libvendor",
2447 vendor: true,
2448 nocrt: true,
2449 }
2450 `)
2451 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2452 cc_library {
2453 name: "libprod",
2454 product_specific: true,
2455 shared_libs: [
2456 "libsystem",
2457 ],
2458 nocrt: true,
2459 }
2460 cc_library {
2461 name: "libsystem",
2462 nocrt: true,
2463 }
2464 `)
2465 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
2466 cc_library {
2467 name: "libprod",
2468 product_specific: true,
2469 shared_libs: [
2470 "libvndk_private",
2471 ],
2472 nocrt: true,
2473 }
2474 cc_library {
2475 name: "libvndk_private",
2476 vendor_available: false,
2477 vndk: {
2478 enabled: true,
2479 },
2480 nocrt: true,
2481 }
2482 `)
2483 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2484 cc_library {
2485 name: "libprod",
2486 product_specific: true,
2487 shared_libs: [
2488 "libsystem_ext",
2489 ],
2490 nocrt: true,
2491 }
2492 cc_library {
2493 name: "libsystem_ext",
2494 system_ext_specific: true,
2495 nocrt: true,
2496 }
2497 `)
2498 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2499 cc_library {
2500 name: "libsystem",
2501 shared_libs: [
2502 "libproduct_va",
2503 ],
2504 nocrt: true,
2505 }
2506 cc_library {
2507 name: "libproduct_va",
2508 product_specific: true,
2509 vendor_available: true,
2510 nocrt: true,
2511 }
2512 `)
2513}
2514
Jooyung Han38002912019-05-16 04:01:54 +09002515func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002516 bp := `
2517 cc_library {
2518 name: "libvndk",
2519 vendor_available: true,
2520 vndk: {
2521 enabled: true,
2522 },
2523 }
2524 cc_library {
2525 name: "libvndksp",
2526 vendor_available: true,
2527 vndk: {
2528 enabled: true,
2529 support_system_process: true,
2530 },
2531 }
2532 cc_library {
2533 name: "libvndkprivate",
2534 vendor_available: false,
2535 vndk: {
2536 enabled: true,
2537 },
2538 }
2539 cc_library {
2540 name: "libvendor",
2541 vendor: true,
2542 }
2543 cc_library {
2544 name: "libvndkext",
2545 vendor: true,
2546 vndk: {
2547 enabled: true,
2548 extends: "libvndk",
2549 },
2550 }
2551 vndk_prebuilt_shared {
2552 name: "prevndk",
2553 version: "27",
2554 target_arch: "arm",
2555 binder32bit: true,
2556 vendor_available: true,
2557 vndk: {
2558 enabled: true,
2559 },
2560 arch: {
2561 arm: {
2562 srcs: ["liba.so"],
2563 },
2564 },
2565 }
2566 cc_library {
2567 name: "libllndk",
2568 }
2569 llndk_library {
2570 name: "libllndk",
2571 symbol_file: "",
2572 }
2573 cc_library {
2574 name: "libllndkprivate",
2575 }
2576 llndk_library {
2577 name: "libllndkprivate",
2578 vendor_available: false,
2579 symbol_file: "",
2580 }`
2581
2582 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002583 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2584 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2585 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002586 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002587
Jooyung Han0302a842019-10-30 18:43:49 +09002588 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002589 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09002590 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002591 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09002592 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002593 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09002594 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002595 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09002596
Colin Crossfb0c16e2019-11-20 17:12:35 -08002597 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002598
Jooyung Han38002912019-05-16 04:01:54 +09002599 tests := []struct {
2600 variant string
2601 name string
2602 expected string
2603 }{
2604 {vendorVariant, "libvndk", "native:vndk"},
2605 {vendorVariant, "libvndksp", "native:vndk"},
2606 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2607 {vendorVariant, "libvendor", "native:vendor"},
2608 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09002609 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002610 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002611 {coreVariant, "libvndk", "native:platform"},
2612 {coreVariant, "libvndkprivate", "native:platform"},
2613 {coreVariant, "libllndk", "native:platform"},
2614 }
2615 for _, test := range tests {
2616 t.Run(test.name, func(t *testing.T) {
2617 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2618 assertString(t, module.makeLinkType, test.expected)
2619 })
2620 }
2621}
2622
Colin Cross0af4b842015-04-30 16:36:18 -07002623var (
2624 str11 = "01234567891"
2625 str10 = str11[:10]
2626 str9 = str11[:9]
2627 str5 = str11[:5]
2628 str4 = str11[:4]
2629)
2630
2631var splitListForSizeTestCases = []struct {
2632 in []string
2633 out [][]string
2634 size int
2635}{
2636 {
2637 in: []string{str10},
2638 out: [][]string{{str10}},
2639 size: 10,
2640 },
2641 {
2642 in: []string{str9},
2643 out: [][]string{{str9}},
2644 size: 10,
2645 },
2646 {
2647 in: []string{str5},
2648 out: [][]string{{str5}},
2649 size: 10,
2650 },
2651 {
2652 in: []string{str11},
2653 out: nil,
2654 size: 10,
2655 },
2656 {
2657 in: []string{str10, str10},
2658 out: [][]string{{str10}, {str10}},
2659 size: 10,
2660 },
2661 {
2662 in: []string{str9, str10},
2663 out: [][]string{{str9}, {str10}},
2664 size: 10,
2665 },
2666 {
2667 in: []string{str10, str9},
2668 out: [][]string{{str10}, {str9}},
2669 size: 10,
2670 },
2671 {
2672 in: []string{str5, str4},
2673 out: [][]string{{str5, str4}},
2674 size: 10,
2675 },
2676 {
2677 in: []string{str5, str4, str5},
2678 out: [][]string{{str5, str4}, {str5}},
2679 size: 10,
2680 },
2681 {
2682 in: []string{str5, str4, str5, str4},
2683 out: [][]string{{str5, str4}, {str5, str4}},
2684 size: 10,
2685 },
2686 {
2687 in: []string{str5, str4, str5, str5},
2688 out: [][]string{{str5, str4}, {str5}, {str5}},
2689 size: 10,
2690 },
2691 {
2692 in: []string{str5, str5, str5, str4},
2693 out: [][]string{{str5}, {str5}, {str5, str4}},
2694 size: 10,
2695 },
2696 {
2697 in: []string{str9, str11},
2698 out: nil,
2699 size: 10,
2700 },
2701 {
2702 in: []string{str11, str9},
2703 out: nil,
2704 size: 10,
2705 },
2706}
2707
2708func TestSplitListForSize(t *testing.T) {
2709 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08002710 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07002711
2712 var outStrings [][]string
2713
2714 if len(out) > 0 {
2715 outStrings = make([][]string, len(out))
2716 for i, o := range out {
2717 outStrings[i] = o.Strings()
2718 }
2719 }
2720
2721 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07002722 t.Errorf("incorrect output:")
2723 t.Errorf(" input: %#v", testCase.in)
2724 t.Errorf(" size: %d", testCase.size)
2725 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07002726 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07002727 }
2728 }
2729}
Jeff Gaston294356f2017-09-27 17:05:30 -07002730
2731var staticLinkDepOrderTestCases = []struct {
2732 // This is a string representation of a map[moduleName][]moduleDependency .
2733 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002734 inStatic string
2735
2736 // This is a string representation of a map[moduleName][]moduleDependency .
2737 // It models the dependencies declared in an Android.bp file.
2738 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002739
2740 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2741 // The keys of allOrdered specify which modules we would like to check.
2742 // The values of allOrdered specify the expected result (of the transitive closure of all
2743 // dependencies) for each module to test
2744 allOrdered string
2745
2746 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2747 // The keys of outOrdered specify which modules we would like to check.
2748 // The values of outOrdered specify the expected result (of the ordered linker command line)
2749 // for each module to test.
2750 outOrdered string
2751}{
2752 // Simple tests
2753 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002754 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002755 outOrdered: "",
2756 },
2757 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002758 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002759 outOrdered: "a:",
2760 },
2761 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002762 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002763 outOrdered: "a:b; b:",
2764 },
2765 // Tests of reordering
2766 {
2767 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002768 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002769 outOrdered: "a:b,c,d; b:d; c:d; d:",
2770 },
2771 {
2772 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002773 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002774 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2775 },
2776 {
2777 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002778 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002779 outOrdered: "a:d,b,e,c; d:b; e:c",
2780 },
2781 {
2782 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002783 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002784 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2785 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2786 },
2787 {
2788 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002789 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 -07002790 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2791 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2792 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002793 // shared dependencies
2794 {
2795 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2796 // So, we don't actually have to check that a shared dependency of c will change the order
2797 // of a library that depends statically on b and on c. We only need to check that if c has
2798 // a shared dependency on b, that that shows up in allOrdered.
2799 inShared: "c:b",
2800 allOrdered: "c:b",
2801 outOrdered: "c:",
2802 },
2803 {
2804 // This test doesn't actually include any shared dependencies but it's a reminder of what
2805 // the second phase of the above test would look like
2806 inStatic: "a:b,c; c:b",
2807 allOrdered: "a:c,b; c:b",
2808 outOrdered: "a:c,b; c:b",
2809 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002810 // tiebreakers for when two modules specifying different orderings and there is no dependency
2811 // to dictate an order
2812 {
2813 // 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 -08002814 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002815 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2816 },
2817 {
2818 // 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 -08002819 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 -07002820 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2821 },
2822 // Tests involving duplicate dependencies
2823 {
2824 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002825 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002826 outOrdered: "a:c,b",
2827 },
2828 {
2829 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002830 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002831 outOrdered: "a:d,c,b",
2832 },
2833 // Tests to confirm the nonexistence of infinite loops.
2834 // These cases should never happen, so as long as the test terminates and the
2835 // result is deterministic then that should be fine.
2836 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002837 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002838 outOrdered: "a:a",
2839 },
2840 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002841 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002842 allOrdered: "a:b,c; b:c,a; c:a,b",
2843 outOrdered: "a:b; b:c; c:a",
2844 },
2845 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002846 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002847 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2848 outOrdered: "a:c,b; b:a,c; c:b,a",
2849 },
2850}
2851
2852// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2853func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2854 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2855 strippedText := strings.Replace(text, " ", "", -1)
2856 if len(strippedText) < 1 {
2857 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2858 }
2859 allDeps = make(map[android.Path][]android.Path, 0)
2860
2861 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2862 moduleTexts := strings.Split(strippedText, ";")
2863
2864 outputForModuleName := func(moduleName string) android.Path {
2865 return android.PathForTesting(moduleName)
2866 }
2867
2868 for _, moduleText := range moduleTexts {
2869 // convert from "a:b,c" to ["a", "b,c"]
2870 components := strings.Split(moduleText, ":")
2871 if len(components) != 2 {
2872 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2873 }
2874 moduleName := components[0]
2875 moduleOutput := outputForModuleName(moduleName)
2876 modulesInOrder = append(modulesInOrder, moduleOutput)
2877
2878 depString := components[1]
2879 // convert from "b,c" to ["b", "c"]
2880 depNames := strings.Split(depString, ",")
2881 if len(depString) < 1 {
2882 depNames = []string{}
2883 }
2884 var deps []android.Path
2885 for _, depName := range depNames {
2886 deps = append(deps, outputForModuleName(depName))
2887 }
2888 allDeps[moduleOutput] = deps
2889 }
2890 return modulesInOrder, allDeps
2891}
2892
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002893func TestLinkReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002894 for _, testCase := range staticLinkDepOrderTestCases {
2895 errs := []string{}
2896
2897 // parse testcase
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002898 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)
Jeff Gaston294356f2017-09-27 17:05:30 -07002899 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)
2900 if testCase.allOrdered == "" {
2901 // allow the test case to skip specifying allOrdered
2902 testCase.allOrdered = testCase.outOrdered
2903 }
2904 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002905 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)
Jeff Gaston294356f2017-09-27 17:05:30 -07002906
2907 // For each module whose post-reordered dependencies were specified, validate that
2908 // reordering the inputs produces the expected outputs.
2909 for _, moduleName := range expectedModuleNames {
2910 moduleDeps := givenTransitiveDeps[moduleName]
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002911 givenSharedDeps := givenAllSharedDeps[moduleName]
2912 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -07002913
2914 correctAllOrdered := expectedAllDeps[moduleName]
2915 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {
2916 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002917 "\nin static:%q"+
2918 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002919 "\nmodule: %v"+
2920 "\nexpected: %s"+
2921 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002922 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002923 }
2924
2925 correctOutputDeps := expectedTransitiveDeps[moduleName]
2926 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {
2927 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002928 "\nin static:%q"+
2929 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002930 "\nmodule: %v"+
2931 "\nexpected: %s"+
2932 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002933 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002934 }
2935 }
2936
2937 if len(errs) > 0 {
2938 sort.Strings(errs)
2939 for _, err := range errs {
2940 t.Error(err)
2941 }
2942 }
2943 }
2944}
Logan Chienf3511742017-10-31 18:04:35 +08002945
Jeff Gaston294356f2017-09-27 17:05:30 -07002946func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2947 for _, moduleName := range moduleNames {
2948 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
2949 output := module.outputFile.Path()
2950 paths = append(paths, output)
2951 }
2952 return paths
2953}
2954
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002955func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002956 ctx := testCc(t, `
2957 cc_library {
2958 name: "a",
2959 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002960 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002961 }
2962 cc_library {
2963 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002964 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002965 }
2966 cc_library {
2967 name: "c",
2968 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002969 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002970 }
2971 cc_library {
2972 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002973 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002974 }
2975
2976 `)
2977
Colin Cross7113d202019-11-20 16:39:12 -08002978 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002979 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002980 actual := moduleA.depsInLinkOrder
Jeff Gaston294356f2017-09-27 17:05:30 -07002981 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})
2982
2983 if !reflect.DeepEqual(actual, expected) {
2984 t.Errorf("staticDeps orderings were not propagated correctly"+
2985 "\nactual: %v"+
2986 "\nexpected: %v",
2987 actual,
2988 expected,
2989 )
2990 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002991}
Jeff Gaston294356f2017-09-27 17:05:30 -07002992
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002993func TestStaticLibDepReorderingWithShared(t *testing.T) {
2994 ctx := testCc(t, `
2995 cc_library {
2996 name: "a",
2997 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002998 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002999 }
3000 cc_library {
3001 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003002 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003003 }
3004 cc_library {
3005 name: "c",
3006 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003007 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003008 }
3009
3010 `)
3011
Colin Cross7113d202019-11-20 16:39:12 -08003012 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003013 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
3014 actual := moduleA.depsInLinkOrder
3015 expected := getOutputPaths(ctx, variant, []string{"c", "b"})
3016
3017 if !reflect.DeepEqual(actual, expected) {
3018 t.Errorf("staticDeps orderings did not account for shared libs"+
3019 "\nactual: %v"+
3020 "\nexpected: %v",
3021 actual,
3022 expected,
3023 )
3024 }
3025}
3026
Jooyung Hanb04a4992020-03-13 18:57:35 +09003027func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07003028 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003029 if !reflect.DeepEqual(actual, expected) {
3030 t.Errorf(message+
3031 "\nactual: %v"+
3032 "\nexpected: %v",
3033 actual,
3034 expected,
3035 )
3036 }
3037}
3038
Jooyung Han61b66e92020-03-21 14:21:46 +00003039func TestLlndkLibrary(t *testing.T) {
3040 ctx := testCc(t, `
3041 cc_library {
3042 name: "libllndk",
3043 stubs: { versions: ["1", "2"] },
3044 }
3045 llndk_library {
3046 name: "libllndk",
3047 }
3048 `)
3049 actual := ctx.ModuleVariantsForTests("libllndk.llndk")
3050 expected := []string{
3051 "android_vendor.VER_arm64_armv8-a_shared",
3052 "android_vendor.VER_arm64_armv8-a_shared_1",
3053 "android_vendor.VER_arm64_armv8-a_shared_2",
3054 "android_vendor.VER_arm_armv7-a-neon_shared",
3055 "android_vendor.VER_arm_armv7-a-neon_shared_1",
3056 "android_vendor.VER_arm_armv7-a-neon_shared_2",
3057 }
3058 checkEquals(t, "variants for llndk stubs", expected, actual)
3059
3060 params := ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
3061 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
3062
3063 params = ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
3064 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
3065}
3066
Jiyong Parka46a4d52017-12-14 19:54:34 +09003067func TestLlndkHeaders(t *testing.T) {
3068 ctx := testCc(t, `
3069 llndk_headers {
3070 name: "libllndk_headers",
3071 export_include_dirs: ["my_include"],
3072 }
3073 llndk_library {
3074 name: "libllndk",
3075 export_llndk_headers: ["libllndk_headers"],
3076 }
3077 cc_library {
3078 name: "libvendor",
3079 shared_libs: ["libllndk"],
3080 vendor: true,
3081 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003082 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08003083 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09003084 }
3085 `)
3086
3087 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08003088 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09003089 cflags := cc.Args["cFlags"]
3090 if !strings.Contains(cflags, "-Imy_include") {
3091 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
3092 }
3093}
3094
Logan Chien43d34c32017-12-20 01:17:32 +08003095func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
3096 actual := module.Properties.AndroidMkRuntimeLibs
3097 if !reflect.DeepEqual(actual, expected) {
3098 t.Errorf("incorrect runtime_libs for shared libs"+
3099 "\nactual: %v"+
3100 "\nexpected: %v",
3101 actual,
3102 expected,
3103 )
3104 }
3105}
3106
3107const runtimeLibAndroidBp = `
3108 cc_library {
3109 name: "libvendor_available1",
3110 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003111 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003112 nocrt : true,
3113 system_shared_libs : [],
3114 }
3115 cc_library {
3116 name: "libvendor_available2",
3117 vendor_available: true,
3118 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003119 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003120 nocrt : true,
3121 system_shared_libs : [],
3122 }
3123 cc_library {
3124 name: "libvendor_available3",
3125 vendor_available: true,
3126 runtime_libs: ["libvendor_available1"],
3127 target: {
3128 vendor: {
3129 exclude_runtime_libs: ["libvendor_available1"],
3130 }
3131 },
Yi Konge7fe9912019-06-02 00:53:50 -07003132 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003133 nocrt : true,
3134 system_shared_libs : [],
3135 }
3136 cc_library {
3137 name: "libcore",
3138 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003139 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003140 nocrt : true,
3141 system_shared_libs : [],
3142 }
3143 cc_library {
3144 name: "libvendor1",
3145 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003146 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003147 nocrt : true,
3148 system_shared_libs : [],
3149 }
3150 cc_library {
3151 name: "libvendor2",
3152 vendor: true,
3153 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003154 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003155 nocrt : true,
3156 system_shared_libs : [],
3157 }
3158`
3159
3160func TestRuntimeLibs(t *testing.T) {
3161 ctx := testCc(t, runtimeLibAndroidBp)
3162
3163 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08003164 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003165
3166 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3167 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3168
3169 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
3170 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3171
3172 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3173 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08003174 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003175
3176 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3177 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
3178
3179 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
3180 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
3181}
3182
3183func TestExcludeRuntimeLibs(t *testing.T) {
3184 ctx := testCc(t, runtimeLibAndroidBp)
3185
Colin Cross7113d202019-11-20 16:39:12 -08003186 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003187 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
3188 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3189
Colin Crossfb0c16e2019-11-20 17:12:35 -08003190 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003191 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
3192 checkRuntimeLibs(t, nil, module)
3193}
3194
3195func TestRuntimeLibsNoVndk(t *testing.T) {
3196 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3197
3198 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3199
Colin Cross7113d202019-11-20 16:39:12 -08003200 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003201
3202 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3203 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3204
3205 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
3206 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
3207}
3208
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003209func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003210 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003211 actual := module.Properties.AndroidMkStaticLibs
3212 if !reflect.DeepEqual(actual, expected) {
3213 t.Errorf("incorrect static_libs"+
3214 "\nactual: %v"+
3215 "\nexpected: %v",
3216 actual,
3217 expected,
3218 )
3219 }
3220}
3221
3222const staticLibAndroidBp = `
3223 cc_library {
3224 name: "lib1",
3225 }
3226 cc_library {
3227 name: "lib2",
3228 static_libs: ["lib1"],
3229 }
3230`
3231
3232func TestStaticLibDepExport(t *testing.T) {
3233 ctx := testCc(t, staticLibAndroidBp)
3234
3235 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003236 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003237 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003238 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003239
3240 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003241 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003242 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3243 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003244 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003245}
3246
Jiyong Parkd08b6972017-09-26 10:50:54 +09003247var compilerFlagsTestCases = []struct {
3248 in string
3249 out bool
3250}{
3251 {
3252 in: "a",
3253 out: false,
3254 },
3255 {
3256 in: "-a",
3257 out: true,
3258 },
3259 {
3260 in: "-Ipath/to/something",
3261 out: false,
3262 },
3263 {
3264 in: "-isystempath/to/something",
3265 out: false,
3266 },
3267 {
3268 in: "--coverage",
3269 out: false,
3270 },
3271 {
3272 in: "-include a/b",
3273 out: true,
3274 },
3275 {
3276 in: "-include a/b c/d",
3277 out: false,
3278 },
3279 {
3280 in: "-DMACRO",
3281 out: true,
3282 },
3283 {
3284 in: "-DMAC RO",
3285 out: false,
3286 },
3287 {
3288 in: "-a -b",
3289 out: false,
3290 },
3291 {
3292 in: "-DMACRO=definition",
3293 out: true,
3294 },
3295 {
3296 in: "-DMACRO=defi nition",
3297 out: true, // TODO(jiyong): this should be false
3298 },
3299 {
3300 in: "-DMACRO(x)=x + 1",
3301 out: true,
3302 },
3303 {
3304 in: "-DMACRO=\"defi nition\"",
3305 out: true,
3306 },
3307}
3308
3309type mockContext struct {
3310 BaseModuleContext
3311 result bool
3312}
3313
3314func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3315 // CheckBadCompilerFlags calls this function when the flag should be rejected
3316 ctx.result = false
3317}
3318
3319func TestCompilerFlags(t *testing.T) {
3320 for _, testCase := range compilerFlagsTestCases {
3321 ctx := &mockContext{result: true}
3322 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3323 if ctx.result != testCase.out {
3324 t.Errorf("incorrect output:")
3325 t.Errorf(" input: %#v", testCase.in)
3326 t.Errorf(" expected: %#v", testCase.out)
3327 t.Errorf(" got: %#v", ctx.result)
3328 }
3329 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003330}
Jiyong Park374510b2018-03-19 18:23:01 +09003331
3332func TestVendorPublicLibraries(t *testing.T) {
3333 ctx := testCc(t, `
3334 cc_library_headers {
3335 name: "libvendorpublic_headers",
3336 export_include_dirs: ["my_include"],
3337 }
3338 vendor_public_library {
3339 name: "libvendorpublic",
3340 symbol_file: "",
3341 export_public_headers: ["libvendorpublic_headers"],
3342 }
3343 cc_library {
3344 name: "libvendorpublic",
3345 srcs: ["foo.c"],
3346 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003347 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003348 nocrt: true,
3349 }
3350
3351 cc_library {
3352 name: "libsystem",
3353 shared_libs: ["libvendorpublic"],
3354 vendor: false,
3355 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003356 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003357 nocrt: true,
3358 }
3359 cc_library {
3360 name: "libvendor",
3361 shared_libs: ["libvendorpublic"],
3362 vendor: true,
3363 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003364 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003365 nocrt: true,
3366 }
3367 `)
3368
Colin Cross7113d202019-11-20 16:39:12 -08003369 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003370 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003371
3372 // test if header search paths are correctly added
3373 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003374 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003375 cflags := cc.Args["cFlags"]
3376 if !strings.Contains(cflags, "-Imy_include") {
3377 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3378 }
3379
3380 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003381 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003382 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003383 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003384 if !strings.Contains(libflags, stubPaths[0].String()) {
3385 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3386 }
3387
3388 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003389 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003390 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003391 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003392 if !strings.Contains(libflags, stubPaths[0].String()) {
3393 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3394 }
3395
3396}
Jiyong Park37b25202018-07-11 10:49:27 +09003397
3398func TestRecovery(t *testing.T) {
3399 ctx := testCc(t, `
3400 cc_library_shared {
3401 name: "librecovery",
3402 recovery: true,
3403 }
3404 cc_library_shared {
3405 name: "librecovery32",
3406 recovery: true,
3407 compile_multilib:"32",
3408 }
Jiyong Park5baac542018-08-28 09:55:37 +09003409 cc_library_shared {
3410 name: "libHalInRecovery",
3411 recovery_available: true,
3412 vendor: true,
3413 }
Jiyong Park37b25202018-07-11 10:49:27 +09003414 `)
3415
3416 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003417 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003418 if len(variants) != 1 || !android.InList(arm64, variants) {
3419 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3420 }
3421
3422 variants = ctx.ModuleVariantsForTests("librecovery32")
3423 if android.InList(arm64, variants) {
3424 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3425 }
Jiyong Park5baac542018-08-28 09:55:37 +09003426
3427 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3428 if !recoveryModule.Platform() {
3429 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3430 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003431}
Jiyong Park5baac542018-08-28 09:55:37 +09003432
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003433func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3434 bp := `
3435 cc_prebuilt_test_library_shared {
3436 name: "test_lib",
3437 relative_install_path: "foo/bar/baz",
3438 srcs: ["srcpath/dontusethispath/baz.so"],
3439 }
3440
3441 cc_test {
3442 name: "main_test",
3443 data_libs: ["test_lib"],
3444 gtest: false,
3445 }
3446 `
3447
3448 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3449 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3450 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3451 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3452
3453 ctx := testCcWithConfig(t, config)
3454 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3455 testBinary := module.(*Module).linker.(*testBinary)
3456 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3457 if err != nil {
3458 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3459 }
3460 if len(outputFiles) != 1 {
3461 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3462 }
3463 if len(testBinary.dataPaths()) != 1 {
3464 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3465 }
3466
3467 outputPath := outputFiles[0].String()
3468
3469 if !strings.HasSuffix(outputPath, "/main_test") {
3470 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3471 }
3472 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
3473 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3474 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3475 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3476 }
3477}
3478
Jiyong Park7ed9de32018-10-15 22:25:07 +09003479func TestVersionedStubs(t *testing.T) {
3480 ctx := testCc(t, `
3481 cc_library_shared {
3482 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003483 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003484 stubs: {
3485 symbol_file: "foo.map.txt",
3486 versions: ["1", "2", "3"],
3487 },
3488 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003489
Jiyong Park7ed9de32018-10-15 22:25:07 +09003490 cc_library_shared {
3491 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003492 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003493 shared_libs: ["libFoo#1"],
3494 }`)
3495
3496 variants := ctx.ModuleVariantsForTests("libFoo")
3497 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003498 "android_arm64_armv8-a_shared",
3499 "android_arm64_armv8-a_shared_1",
3500 "android_arm64_armv8-a_shared_2",
3501 "android_arm64_armv8-a_shared_3",
3502 "android_arm_armv7-a-neon_shared",
3503 "android_arm_armv7-a-neon_shared_1",
3504 "android_arm_armv7-a-neon_shared_2",
3505 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003506 }
3507 variantsMismatch := false
3508 if len(variants) != len(expectedVariants) {
3509 variantsMismatch = true
3510 } else {
3511 for _, v := range expectedVariants {
3512 if !inList(v, variants) {
3513 variantsMismatch = false
3514 }
3515 }
3516 }
3517 if variantsMismatch {
3518 t.Errorf("variants of libFoo expected:\n")
3519 for _, v := range expectedVariants {
3520 t.Errorf("%q\n", v)
3521 }
3522 t.Errorf(", but got:\n")
3523 for _, v := range variants {
3524 t.Errorf("%q\n", v)
3525 }
3526 }
3527
Colin Cross7113d202019-11-20 16:39:12 -08003528 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003529 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003530 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003531 if !strings.Contains(libFlags, libFoo1StubPath) {
3532 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3533 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003534
Colin Cross7113d202019-11-20 16:39:12 -08003535 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003536 cFlags := libBarCompileRule.Args["cFlags"]
3537 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3538 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3539 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3540 }
Jiyong Park37b25202018-07-11 10:49:27 +09003541}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003542
Jooyung Hanb04a4992020-03-13 18:57:35 +09003543func TestVersioningMacro(t *testing.T) {
3544 for _, tc := range []struct{ moduleName, expected string }{
3545 {"libc", "__LIBC_API__"},
3546 {"libfoo", "__LIBFOO_API__"},
3547 {"libfoo@1", "__LIBFOO_1_API__"},
3548 {"libfoo-v1", "__LIBFOO_V1_API__"},
3549 {"libfoo.v1", "__LIBFOO_V1_API__"},
3550 } {
3551 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3552 }
3553}
3554
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003555func TestStaticExecutable(t *testing.T) {
3556 ctx := testCc(t, `
3557 cc_binary {
3558 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003559 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003560 static_executable: true,
3561 }`)
3562
Colin Cross7113d202019-11-20 16:39:12 -08003563 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003564 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3565 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003566 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003567 for _, lib := range systemStaticLibs {
3568 if !strings.Contains(libFlags, lib) {
3569 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3570 }
3571 }
3572 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3573 for _, lib := range systemSharedLibs {
3574 if strings.Contains(libFlags, lib) {
3575 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3576 }
3577 }
3578}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003579
3580func TestStaticDepsOrderWithStubs(t *testing.T) {
3581 ctx := testCc(t, `
3582 cc_binary {
3583 name: "mybin",
3584 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003585 static_libs: ["libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003586 static_executable: true,
3587 stl: "none",
3588 }
3589
3590 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003591 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003592 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003593 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003594 stl: "none",
3595 }
3596
3597 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003598 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003599 srcs: ["foo.c"],
3600 stl: "none",
3601 stubs: {
3602 versions: ["1"],
3603 },
3604 }`)
3605
Colin Cross7113d202019-11-20 16:39:12 -08003606 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Module().(*Module)
Jiyong Parke4bb9862019-02-01 00:31:10 +09003607 actual := mybin.depsInLinkOrder
Colin Crossf9aabd72020-02-15 11:29:50 -08003608 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003609
3610 if !reflect.DeepEqual(actual, expected) {
3611 t.Errorf("staticDeps orderings were not propagated correctly"+
3612 "\nactual: %v"+
3613 "\nexpected: %v",
3614 actual,
3615 expected,
3616 )
3617 }
3618}
Jooyung Han38002912019-05-16 04:01:54 +09003619
Jooyung Hand48f3c32019-08-23 11:18:57 +09003620func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3621 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3622 cc_library {
3623 name: "libA",
3624 srcs: ["foo.c"],
3625 shared_libs: ["libB"],
3626 stl: "none",
3627 }
3628
3629 cc_library {
3630 name: "libB",
3631 srcs: ["foo.c"],
3632 enabled: false,
3633 stl: "none",
3634 }
3635 `)
3636}
3637
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003638// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3639// correctly.
3640func TestFuzzTarget(t *testing.T) {
3641 ctx := testCc(t, `
3642 cc_fuzz {
3643 name: "fuzz_smoke_test",
3644 srcs: ["foo.c"],
3645 }`)
3646
Paul Duffin075c4172019-12-19 19:06:13 +00003647 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003648 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3649}
3650
Jiyong Park29074592019-07-07 16:27:47 +09003651func TestAidl(t *testing.T) {
3652}
3653
Jooyung Han38002912019-05-16 04:01:54 +09003654func assertString(t *testing.T, got, expected string) {
3655 t.Helper()
3656 if got != expected {
3657 t.Errorf("expected %q got %q", expected, got)
3658 }
3659}
3660
3661func assertArrayString(t *testing.T, got, expected []string) {
3662 t.Helper()
3663 if len(got) != len(expected) {
3664 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3665 return
3666 }
3667 for i := range got {
3668 if got[i] != expected[i] {
3669 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3670 i, expected[i], expected, got[i], got)
3671 return
3672 }
3673 }
3674}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003675
Jooyung Han0302a842019-10-30 18:43:49 +09003676func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3677 t.Helper()
3678 assertArrayString(t, android.SortedStringKeys(m), expected)
3679}
3680
Colin Crosse1bb5d02019-09-24 14:55:04 -07003681func TestDefaults(t *testing.T) {
3682 ctx := testCc(t, `
3683 cc_defaults {
3684 name: "defaults",
3685 srcs: ["foo.c"],
3686 static: {
3687 srcs: ["bar.c"],
3688 },
3689 shared: {
3690 srcs: ["baz.c"],
3691 },
3692 }
3693
3694 cc_library_static {
3695 name: "libstatic",
3696 defaults: ["defaults"],
3697 }
3698
3699 cc_library_shared {
3700 name: "libshared",
3701 defaults: ["defaults"],
3702 }
3703
3704 cc_library {
3705 name: "libboth",
3706 defaults: ["defaults"],
3707 }
3708
3709 cc_binary {
3710 name: "binary",
3711 defaults: ["defaults"],
3712 }`)
3713
3714 pathsToBase := func(paths android.Paths) []string {
3715 var ret []string
3716 for _, p := range paths {
3717 ret = append(ret, p.Base())
3718 }
3719 return ret
3720 }
3721
Colin Cross7113d202019-11-20 16:39:12 -08003722 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003723 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3724 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3725 }
Colin Cross7113d202019-11-20 16:39:12 -08003726 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003727 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3728 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3729 }
Colin Cross7113d202019-11-20 16:39:12 -08003730 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003731 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3732 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3733 }
3734
Colin Cross7113d202019-11-20 16:39:12 -08003735 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003736 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3737 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3738 }
Colin Cross7113d202019-11-20 16:39:12 -08003739 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003740 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3741 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3742 }
3743}
Colin Crosseabaedd2020-02-06 17:01:55 -08003744
3745func TestProductVariableDefaults(t *testing.T) {
3746 bp := `
3747 cc_defaults {
3748 name: "libfoo_defaults",
3749 srcs: ["foo.c"],
3750 cppflags: ["-DFOO"],
3751 product_variables: {
3752 debuggable: {
3753 cppflags: ["-DBAR"],
3754 },
3755 },
3756 }
3757
3758 cc_library {
3759 name: "libfoo",
3760 defaults: ["libfoo_defaults"],
3761 }
3762 `
3763
3764 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3765 config.TestProductVariables.Debuggable = BoolPtr(true)
3766
3767 ctx := CreateTestContext()
3768 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
3769 ctx.BottomUp("variable", android.VariableMutator).Parallel()
3770 })
3771 ctx.Register(config)
3772
3773 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3774 android.FailIfErrored(t, errs)
3775 _, errs = ctx.PrepareBuildActions(config)
3776 android.FailIfErrored(t, errs)
3777
3778 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
3779 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
3780 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
3781 }
3782}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003783
3784func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3785 t.Parallel()
3786 bp := `
3787 cc_library_static {
3788 name: "libfoo",
3789 srcs: ["foo.c"],
3790 whole_static_libs: ["libbar"],
3791 }
3792
3793 cc_library_static {
3794 name: "libbar",
3795 whole_static_libs: ["libmissing"],
3796 }
3797 `
3798
3799 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3800 config.TestProductVariables.Allow_missing_dependencies = BoolPtr(true)
3801
3802 ctx := CreateTestContext()
3803 ctx.SetAllowMissingDependencies(true)
3804 ctx.Register(config)
3805
3806 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3807 android.FailIfErrored(t, errs)
3808 _, errs = ctx.PrepareBuildActions(config)
3809 android.FailIfErrored(t, errs)
3810
3811 libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
3812 if g, w := libbar.Rule, android.ErrorRule; g != w {
3813 t.Fatalf("Expected libbar rule to be %q, got %q", w, g)
3814 }
3815
3816 if g, w := libbar.Args["error"], "missing dependencies: libmissing"; !strings.Contains(g, w) {
3817 t.Errorf("Expected libbar error to contain %q, was %q", w, g)
3818 }
3819
3820 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
3821 if g, w := libfoo.Inputs.Strings(), libbar.Output.String(); !android.InList(w, g) {
3822 t.Errorf("Expected libfoo.a to depend on %q, got %q", w, g)
3823 }
3824
3825}