blob: 5951fb3d58d6b79dadd02ab0c04711578b41b604 [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 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070024 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070025
26 "android/soong/android"
Colin Cross74d1ec02015-04-28 13:30:13 -070027)
28
Jiyong Park6a43f042017-10-12 23:05:00 +090029var buildDir string
30
31func setUp() {
32 var err error
33 buildDir, err = ioutil.TempDir("", "soong_cc_test")
34 if err != nil {
35 panic(err)
36 }
37}
38
39func tearDown() {
40 os.RemoveAll(buildDir)
41}
42
43func TestMain(m *testing.M) {
44 run := func() int {
45 setUp()
46 defer tearDown()
47
48 return m.Run()
49 }
50
51 os.Exit(run())
52}
53
Colin Cross98be1bb2019-12-13 20:41:13 -080054func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070055 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080056 ctx := CreateTestContext()
57 ctx.Register(config)
Logan Chienf3511742017-10-31 18:04:35 +080058
Jeff Gastond3e141d2017-08-08 17:46:01 -070059 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +080060 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +090061 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +080062 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +090063
64 return ctx
65}
66
Logan Chienf3511742017-10-31 18:04:35 +080067func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080068 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080069 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070070 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
71 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +080072
Colin Cross98be1bb2019-12-13 20:41:13 -080073 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080074}
75
76func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080077 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080078 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070079 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +080080
Colin Cross98be1bb2019-12-13 20:41:13 -080081 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080082}
83
Justin Yun5f7f7e82019-11-18 19:52:14 +090084func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +080085 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +080086
Colin Cross98be1bb2019-12-13 20:41:13 -080087 ctx := CreateTestContext()
88 ctx.Register(config)
Logan Chienf3511742017-10-31 18:04:35 +080089
90 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
91 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +080092 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +080093 return
94 }
95
96 _, errs = ctx.PrepareBuildActions(config)
97 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +080098 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +080099 return
100 }
101
102 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
103}
104
Justin Yun5f7f7e82019-11-18 19:52:14 +0900105func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900106 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900107 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) {
Jooyung Han261e1582020-10-20 18:54:21 +0900115 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900116 config := TestConfig(buildDir, android.Android, nil, bp, nil)
117 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
118 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
119 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
120 testCcErrorWithConfig(t, pattern, config)
121 return
122}
123
Logan Chienf3511742017-10-31 18:04:35 +0800124const (
Colin Cross7113d202019-11-20 16:39:12 -0800125 coreVariant = "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800126 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun5f7f7e82019-11-18 19:52:14 +0900127 productVariant = "android_product.VER_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800128 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800129)
130
Doug Hornc32c6b02019-01-17 14:44:05 -0800131func TestFuchsiaDeps(t *testing.T) {
132 t.Helper()
133
134 bp := `
135 cc_library {
136 name: "libTest",
137 srcs: ["foo.c"],
138 target: {
139 fuchsia: {
140 srcs: ["bar.c"],
141 },
142 },
143 }`
144
Colin Cross98be1bb2019-12-13 20:41:13 -0800145 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
146 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800147
148 rt := false
149 fb := false
150
151 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
152 implicits := ld.Implicits
153 for _, lib := range implicits {
154 if strings.Contains(lib.Rel(), "libcompiler_rt") {
155 rt = true
156 }
157
158 if strings.Contains(lib.Rel(), "libbioniccompat") {
159 fb = true
160 }
161 }
162
163 if !rt || !fb {
164 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
165 }
166}
167
168func TestFuchsiaTargetDecl(t *testing.T) {
169 t.Helper()
170
171 bp := `
172 cc_library {
173 name: "libTest",
174 srcs: ["foo.c"],
175 target: {
176 fuchsia: {
177 srcs: ["bar.c"],
178 },
179 },
180 }`
181
Colin Cross98be1bb2019-12-13 20:41:13 -0800182 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
183 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800184 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
185 var objs []string
186 for _, o := range ld.Inputs {
187 objs = append(objs, o.Base())
188 }
189 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
190 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
191 }
192}
193
Jiyong Park6a43f042017-10-12 23:05:00 +0900194func TestVendorSrc(t *testing.T) {
195 ctx := testCc(t, `
196 cc_library {
197 name: "libTest",
198 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700199 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800200 nocrt: true,
201 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900202 vendor_available: true,
203 target: {
204 vendor: {
205 srcs: ["bar.c"],
206 },
207 },
208 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900209 `)
210
Logan Chienf3511742017-10-31 18:04:35 +0800211 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900212 var objs []string
213 for _, o := range ld.Inputs {
214 objs = append(objs, o.Base())
215 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800216 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900217 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
218 }
219}
220
Logan Chienf3511742017-10-31 18:04:35 +0800221func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900222 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800223
Logan Chiend3c59a22018-03-29 14:08:15 +0800224 t.Helper()
225
Justin Yun0ecf0b22020-02-28 15:07:59 +0900226 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Ivan Lozano52767be2019-10-18 14:49:46 -0700227 if !mod.HasVendorVariant() {
Justin Yun0ecf0b22020-02-28 15:07:59 +0900228 t.Errorf("%q must have variant %q", name, variant)
Logan Chienf3511742017-10-31 18:04:35 +0800229 }
230
231 // Check library properties.
232 lib, ok := mod.compiler.(*libraryDecorator)
233 if !ok {
234 t.Errorf("%q must have libraryDecorator", name)
235 } else if lib.baseInstaller.subDir != subDir {
236 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
237 lib.baseInstaller.subDir)
238 }
239
240 // Check VNDK properties.
241 if mod.vndkdep == nil {
242 t.Fatalf("%q must have `vndkdep`", name)
243 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700244 if !mod.IsVndk() {
245 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800246 }
247 if mod.isVndkSp() != isVndkSp {
248 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
249 }
250
251 // Check VNDK extension properties.
252 isVndkExt := extends != ""
253 if mod.isVndkExt() != isVndkExt {
254 t.Errorf("%q isVndkExt() must equal to %t", name, isVndkExt)
255 }
256
257 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
258 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
259 }
260}
261
Bill Peckham945441c2020-08-31 16:07:58 -0700262func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool) {
263 t.Helper()
Jooyung Han39edb6c2019-11-06 16:53:07 +0900264 mod, ok := ctx.ModuleForTests(moduleName, variant).Module().(android.OutputFileProducer)
265 if !ok {
266 t.Errorf("%q must have output\n", moduleName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900267 return
268 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900269 outputFiles, err := mod.OutputFiles("")
270 if err != nil || len(outputFiles) != 1 {
271 t.Errorf("%q must have single output\n", moduleName)
272 return
273 }
274 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900275
Bill Peckham945441c2020-08-31 16:07:58 -0700276 if include {
277 out := singleton.Output(snapshotPath)
278 if out.Input.String() != outputFiles[0].String() {
279 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
280 }
281 } else {
282 out := singleton.MaybeOutput(snapshotPath)
283 if out.Rule != nil {
284 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
285 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900286 }
287}
288
Bill Peckham945441c2020-08-31 16:07:58 -0700289func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
290 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true)
291}
292
293func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
294 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false)
295}
296
Jooyung Han2216fb12019-11-06 16:46:15 +0900297func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
298 t.Helper()
299 assertString(t, params.Rule.String(), android.WriteFile.String())
300 actual := strings.FieldsFunc(strings.ReplaceAll(params.Args["content"], "\\n", "\n"), func(r rune) bool { return r == '\n' })
301 assertArrayString(t, actual, expected)
302}
303
Jooyung Han097087b2019-10-22 19:32:18 +0900304func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
305 t.Helper()
306 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900307 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
308}
309
310func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
311 t.Helper()
312 vndkLibraries := ctx.ModuleForTests(module, "")
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +0900313
314 var output string
315 if module != "vndkcorevariant.libraries.txt" {
316 output = insertVndkVersion(module, "VER")
317 } else {
318 output = module
319 }
320
Jooyung Han2216fb12019-11-06 16:46:15 +0900321 checkWriteFileOutput(t, vndkLibraries.Output(output), expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900322}
323
Logan Chienf3511742017-10-31 18:04:35 +0800324func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800325 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800326 cc_library {
327 name: "libvndk",
328 vendor_available: true,
329 vndk: {
330 enabled: true,
331 },
332 nocrt: true,
333 }
334
335 cc_library {
336 name: "libvndk_private",
337 vendor_available: false,
338 vndk: {
339 enabled: true,
340 },
341 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900342 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800343 }
344
345 cc_library {
346 name: "libvndk_sp",
347 vendor_available: true,
348 vndk: {
349 enabled: true,
350 support_system_process: true,
351 },
352 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900353 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800354 }
355
356 cc_library {
357 name: "libvndk_sp_private",
358 vendor_available: false,
359 vndk: {
360 enabled: true,
361 support_system_process: true,
362 },
363 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900364 target: {
365 vendor: {
366 suffix: "-x",
367 },
368 },
Logan Chienf3511742017-10-31 18:04:35 +0800369 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900370 vndk_libraries_txt {
371 name: "llndk.libraries.txt",
372 }
373 vndk_libraries_txt {
374 name: "vndkcore.libraries.txt",
375 }
376 vndk_libraries_txt {
377 name: "vndksp.libraries.txt",
378 }
379 vndk_libraries_txt {
380 name: "vndkprivate.libraries.txt",
381 }
382 vndk_libraries_txt {
383 name: "vndkcorevariant.libraries.txt",
384 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800385 `
386
387 config := TestConfig(buildDir, android.Android, nil, bp, nil)
388 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
389 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
390
391 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800392
Jooyung Han261e1582020-10-20 18:54:21 +0900393 // subdir == "" because VNDK libs are not supposed to be installed separately.
394 // They are installed as part of VNDK APEX instead.
395 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
396 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
397 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
398 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900399
400 // Check VNDK snapshot output.
401
402 snapshotDir := "vndk-snapshot"
403 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
404
405 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
406 "arm64", "armv8-a"))
407 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
408 "arm", "armv7-a-neon"))
409
410 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
411 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
412 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
413 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
414
Colin Crossfb0c16e2019-11-20 17:12:35 -0800415 variant := "android_vendor.VER_arm64_armv8-a_shared"
416 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900417
Inseob Kim7f283f42020-06-01 21:53:49 +0900418 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
419
420 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
421 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
422 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
423 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900424
Jooyung Han39edb6c2019-11-06 16:53:07 +0900425 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900426 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
427 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
428 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
429 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900430
Jooyung Han097087b2019-10-22 19:32:18 +0900431 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
432 "LLNDK: libc.so",
433 "LLNDK: libdl.so",
434 "LLNDK: libft2.so",
435 "LLNDK: libm.so",
436 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900437 "VNDK-SP: libvndk_sp-x.so",
438 "VNDK-SP: libvndk_sp_private-x.so",
439 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900440 "VNDK-core: libvndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900441 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900442 "VNDK-private: libvndk-private.so",
443 "VNDK-private: libvndk_sp_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900444 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900445 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
446 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so"})
447 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so"})
448 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so"})
449 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
450}
451
Yo Chiangbba545e2020-06-09 16:15:37 +0800452func TestVndkWithHostSupported(t *testing.T) {
453 ctx := testCc(t, `
454 cc_library {
455 name: "libvndk_host_supported",
456 vendor_available: true,
457 vndk: {
458 enabled: true,
459 },
460 host_supported: true,
461 }
462
463 cc_library {
464 name: "libvndk_host_supported_but_disabled_on_device",
465 vendor_available: true,
466 vndk: {
467 enabled: true,
468 },
469 host_supported: true,
470 enabled: false,
471 target: {
472 host: {
473 enabled: true,
474 }
475 }
476 }
477
478 vndk_libraries_txt {
479 name: "vndkcore.libraries.txt",
480 }
481 `)
482
483 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
484}
485
Jooyung Han2216fb12019-11-06 16:46:15 +0900486func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800487 bp := `
Jooyung Han2216fb12019-11-06 16:46:15 +0900488 vndk_libraries_txt {
489 name: "llndk.libraries.txt",
Colin Cross98be1bb2019-12-13 20:41:13 -0800490 }`
491 config := TestConfig(buildDir, android.Android, nil, bp, nil)
492 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
493 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
494 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900495
496 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900497 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900498 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900499}
500
501func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800502 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900503 cc_library {
504 name: "libvndk",
505 vendor_available: true,
506 vndk: {
507 enabled: true,
508 },
509 nocrt: true,
510 }
511
512 cc_library {
513 name: "libvndk_sp",
514 vendor_available: true,
515 vndk: {
516 enabled: true,
517 support_system_process: true,
518 },
519 nocrt: true,
520 }
521
522 cc_library {
523 name: "libvndk2",
524 vendor_available: false,
525 vndk: {
526 enabled: true,
527 },
528 nocrt: true,
529 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900530
531 vndk_libraries_txt {
532 name: "vndkcorevariant.libraries.txt",
533 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800534 `
535
536 config := TestConfig(buildDir, android.Android, nil, bp, nil)
537 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
538 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
539 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
540
541 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
542
543 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900544
Jooyung Han2216fb12019-11-06 16:46:15 +0900545 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900546}
547
Chris Parsons79d66a52020-06-05 17:26:16 -0400548func TestDataLibs(t *testing.T) {
549 bp := `
550 cc_test_library {
551 name: "test_lib",
552 srcs: ["test_lib.cpp"],
553 gtest: false,
554 }
555
556 cc_test {
557 name: "main_test",
558 data_libs: ["test_lib"],
559 gtest: false,
560 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400561 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400562
563 config := TestConfig(buildDir, android.Android, nil, bp, nil)
564 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
565 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
566 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
567
568 ctx := testCcWithConfig(t, config)
569 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
570 testBinary := module.(*Module).linker.(*testBinary)
571 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
572 if err != nil {
573 t.Errorf("Expected cc_test to produce output files, error: %s", err)
574 return
575 }
576 if len(outputFiles) != 1 {
577 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
578 return
579 }
580 if len(testBinary.dataPaths()) != 1 {
581 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
582 return
583 }
584
585 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400586 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400587
588 if !strings.HasSuffix(outputPath, "/main_test") {
589 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
590 return
591 }
592 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
593 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
594 return
595 }
596}
597
Chris Parsons216e10a2020-07-09 17:12:52 -0400598func TestDataLibsRelativeInstallPath(t *testing.T) {
599 bp := `
600 cc_test_library {
601 name: "test_lib",
602 srcs: ["test_lib.cpp"],
603 relative_install_path: "foo/bar/baz",
604 gtest: false,
605 }
606
607 cc_test {
608 name: "main_test",
609 data_libs: ["test_lib"],
610 gtest: false,
611 }
612 `
613
614 config := TestConfig(buildDir, android.Android, nil, bp, nil)
615 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
616 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
617 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
618
619 ctx := testCcWithConfig(t, config)
620 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
621 testBinary := module.(*Module).linker.(*testBinary)
622 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
623 if err != nil {
624 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
625 }
626 if len(outputFiles) != 1 {
627 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
628 }
629 if len(testBinary.dataPaths()) != 1 {
630 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
631 }
632
633 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400634
635 if !strings.HasSuffix(outputPath, "/main_test") {
636 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
637 }
638 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
639 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
640 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400641 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400642 }
643}
644
Jooyung Han0302a842019-10-30 18:43:49 +0900645func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900646 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900647 cc_library {
648 name: "libvndk",
649 vendor_available: true,
650 vndk: {
651 enabled: true,
652 },
653 nocrt: true,
654 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900655 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900656
657 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
658 "LLNDK: libc.so",
659 "LLNDK: libdl.so",
660 "LLNDK: libft2.so",
661 "LLNDK: libm.so",
662 "VNDK-SP: libc++.so",
663 "VNDK-core: libvndk.so",
664 "VNDK-private: libft2.so",
665 })
Logan Chienf3511742017-10-31 18:04:35 +0800666}
667
Logan Chiend3c59a22018-03-29 14:08:15 +0800668func TestVndkDepError(t *testing.T) {
669 // Check whether an error is emitted when a VNDK lib depends on a system lib.
670 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
671 cc_library {
672 name: "libvndk",
673 vendor_available: true,
674 vndk: {
675 enabled: true,
676 },
677 shared_libs: ["libfwk"], // Cause error
678 nocrt: true,
679 }
680
681 cc_library {
682 name: "libfwk",
683 nocrt: true,
684 }
685 `)
686
687 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
688 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
689 cc_library {
690 name: "libvndk",
691 vendor_available: true,
692 vndk: {
693 enabled: true,
694 },
695 shared_libs: ["libvendor"], // Cause error
696 nocrt: true,
697 }
698
699 cc_library {
700 name: "libvendor",
701 vendor: true,
702 nocrt: true,
703 }
704 `)
705
706 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
707 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
708 cc_library {
709 name: "libvndk_sp",
710 vendor_available: true,
711 vndk: {
712 enabled: true,
713 support_system_process: true,
714 },
715 shared_libs: ["libfwk"], // Cause error
716 nocrt: true,
717 }
718
719 cc_library {
720 name: "libfwk",
721 nocrt: true,
722 }
723 `)
724
725 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
726 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
727 cc_library {
728 name: "libvndk_sp",
729 vendor_available: true,
730 vndk: {
731 enabled: true,
732 support_system_process: true,
733 },
734 shared_libs: ["libvendor"], // Cause error
735 nocrt: true,
736 }
737
738 cc_library {
739 name: "libvendor",
740 vendor: true,
741 nocrt: true,
742 }
743 `)
744
745 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
746 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
747 cc_library {
748 name: "libvndk_sp",
749 vendor_available: true,
750 vndk: {
751 enabled: true,
752 support_system_process: true,
753 },
754 shared_libs: ["libvndk"], // Cause error
755 nocrt: true,
756 }
757
758 cc_library {
759 name: "libvndk",
760 vendor_available: true,
761 vndk: {
762 enabled: true,
763 },
764 nocrt: true,
765 }
766 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900767
768 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
769 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
770 cc_library {
771 name: "libvndk",
772 vendor_available: true,
773 vndk: {
774 enabled: true,
775 },
776 shared_libs: ["libnonvndk"],
777 nocrt: true,
778 }
779
780 cc_library {
781 name: "libnonvndk",
782 vendor_available: true,
783 nocrt: true,
784 }
785 `)
786
787 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
788 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
789 cc_library {
790 name: "libvndkprivate",
791 vendor_available: false,
792 vndk: {
793 enabled: true,
794 },
795 shared_libs: ["libnonvndk"],
796 nocrt: true,
797 }
798
799 cc_library {
800 name: "libnonvndk",
801 vendor_available: true,
802 nocrt: true,
803 }
804 `)
805
806 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
807 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
808 cc_library {
809 name: "libvndksp",
810 vendor_available: true,
811 vndk: {
812 enabled: true,
813 support_system_process: true,
814 },
815 shared_libs: ["libnonvndk"],
816 nocrt: true,
817 }
818
819 cc_library {
820 name: "libnonvndk",
821 vendor_available: true,
822 nocrt: true,
823 }
824 `)
825
826 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
827 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
828 cc_library {
829 name: "libvndkspprivate",
830 vendor_available: false,
831 vndk: {
832 enabled: true,
833 support_system_process: true,
834 },
835 shared_libs: ["libnonvndk"],
836 nocrt: true,
837 }
838
839 cc_library {
840 name: "libnonvndk",
841 vendor_available: true,
842 nocrt: true,
843 }
844 `)
845}
846
847func TestDoubleLoadbleDep(t *testing.T) {
848 // okay to link : LLNDK -> double_loadable VNDK
849 testCc(t, `
850 cc_library {
851 name: "libllndk",
852 shared_libs: ["libdoubleloadable"],
853 }
854
855 llndk_library {
856 name: "libllndk",
857 symbol_file: "",
858 }
859
860 cc_library {
861 name: "libdoubleloadable",
862 vendor_available: true,
863 vndk: {
864 enabled: true,
865 },
866 double_loadable: true,
867 }
868 `)
869 // okay to link : LLNDK -> VNDK-SP
870 testCc(t, `
871 cc_library {
872 name: "libllndk",
873 shared_libs: ["libvndksp"],
874 }
875
876 llndk_library {
877 name: "libllndk",
878 symbol_file: "",
879 }
880
881 cc_library {
882 name: "libvndksp",
883 vendor_available: true,
884 vndk: {
885 enabled: true,
886 support_system_process: true,
887 },
888 }
889 `)
890 // okay to link : double_loadable -> double_loadable
891 testCc(t, `
892 cc_library {
893 name: "libdoubleloadable1",
894 shared_libs: ["libdoubleloadable2"],
895 vendor_available: true,
896 double_loadable: true,
897 }
898
899 cc_library {
900 name: "libdoubleloadable2",
901 vendor_available: true,
902 double_loadable: true,
903 }
904 `)
905 // okay to link : double_loadable VNDK -> double_loadable VNDK private
906 testCc(t, `
907 cc_library {
908 name: "libdoubleloadable",
909 vendor_available: true,
910 vndk: {
911 enabled: true,
912 },
913 double_loadable: true,
914 shared_libs: ["libnondoubleloadable"],
915 }
916
917 cc_library {
918 name: "libnondoubleloadable",
919 vendor_available: false,
920 vndk: {
921 enabled: true,
922 },
923 double_loadable: true,
924 }
925 `)
926 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
927 testCc(t, `
928 cc_library {
929 name: "libllndk",
930 shared_libs: ["libcoreonly"],
931 }
932
933 llndk_library {
934 name: "libllndk",
935 symbol_file: "",
936 }
937
938 cc_library {
939 name: "libcoreonly",
940 shared_libs: ["libvendoravailable"],
941 }
942
943 // indirect dependency of LLNDK
944 cc_library {
945 name: "libvendoravailable",
946 vendor_available: true,
947 double_loadable: true,
948 }
949 `)
950}
951
Inseob Kim5f58ff72020-09-07 19:53:31 +0900952func TestVendorSnapshotCapture(t *testing.T) {
Inseob Kim8471cda2019-11-15 09:59:12 +0900953 bp := `
954 cc_library {
955 name: "libvndk",
956 vendor_available: true,
957 vndk: {
958 enabled: true,
959 },
960 nocrt: true,
961 }
962
963 cc_library {
964 name: "libvendor",
965 vendor: true,
966 nocrt: true,
967 }
968
969 cc_library {
970 name: "libvendor_available",
971 vendor_available: true,
972 nocrt: true,
973 }
974
975 cc_library_headers {
976 name: "libvendor_headers",
977 vendor_available: true,
978 nocrt: true,
979 }
980
981 cc_binary {
982 name: "vendor_bin",
983 vendor: true,
984 nocrt: true,
985 }
986
987 cc_binary {
988 name: "vendor_available_bin",
989 vendor_available: true,
990 nocrt: true,
991 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900992
993 toolchain_library {
994 name: "libb",
995 vendor_available: true,
996 src: "libb.a",
997 }
Inseob Kim1042d292020-06-01 23:23:05 +0900998
999 cc_object {
1000 name: "obj",
1001 vendor_available: true,
1002 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001003`
1004 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1005 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1006 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1007 ctx := testCcWithConfig(t, config)
1008
1009 // Check Vendor snapshot output.
1010
1011 snapshotDir := "vendor-snapshot"
1012 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +09001013 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1014
1015 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +09001016
1017 for _, arch := range [][]string{
1018 []string{"arm64", "armv8-a"},
1019 []string{"arm", "armv7-a-neon"},
1020 } {
1021 archType := arch[0]
1022 archVariant := arch[1]
1023 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1024
1025 // For shared libraries, only non-VNDK vendor_available modules are captured
1026 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1027 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +09001028 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1029 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
1030 jsonFiles = append(jsonFiles,
1031 filepath.Join(sharedDir, "libvendor.so.json"),
1032 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001033
1034 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
Inseob Kimc42f2f22020-07-29 20:32:10 +09001035 // Also cfi variants are captured, except for prebuilts like toolchain_library
Inseob Kim8471cda2019-11-15 09:59:12 +09001036 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001037 staticCfiVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static_cfi", archType, archVariant)
Inseob Kim8471cda2019-11-15 09:59:12 +09001038 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +09001039 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1040 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001041 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001042 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001043 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001044 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001045 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001046 jsonFiles = append(jsonFiles,
1047 filepath.Join(staticDir, "libb.a.json"),
1048 filepath.Join(staticDir, "libvndk.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001049 filepath.Join(staticDir, "libvndk.cfi.a.json"),
Inseob Kim7f283f42020-06-01 21:53:49 +09001050 filepath.Join(staticDir, "libvendor.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001051 filepath.Join(staticDir, "libvendor.cfi.a.json"),
1052 filepath.Join(staticDir, "libvendor_available.a.json"),
1053 filepath.Join(staticDir, "libvendor_available.cfi.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001054
Inseob Kim7f283f42020-06-01 21:53:49 +09001055 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +09001056 if archType == "arm64" {
1057 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1058 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +09001059 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
1060 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
1061 jsonFiles = append(jsonFiles,
1062 filepath.Join(binaryDir, "vendor_bin.json"),
1063 filepath.Join(binaryDir, "vendor_available_bin.json"))
1064 }
1065
1066 // For header libraries, all vendor:true and vendor_available modules are captured.
1067 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1068 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +09001069
1070 // For object modules, all vendor:true and vendor_available modules are captured.
1071 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1072 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1073 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1074 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001075 }
1076
1077 for _, jsonFile := range jsonFiles {
1078 // verify all json files exist
1079 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1080 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001081 }
1082 }
1083}
1084
Inseob Kim5f58ff72020-09-07 19:53:31 +09001085func TestVendorSnapshotUse(t *testing.T) {
1086 frameworkBp := `
1087 cc_library {
1088 name: "libvndk",
1089 vendor_available: true,
1090 vndk: {
1091 enabled: true,
1092 },
1093 nocrt: true,
1094 compile_multilib: "64",
1095 }
1096
1097 cc_library {
1098 name: "libvendor",
1099 vendor: true,
1100 nocrt: true,
1101 no_libcrt: true,
1102 stl: "none",
1103 system_shared_libs: [],
1104 compile_multilib: "64",
1105 }
1106
1107 cc_binary {
1108 name: "bin",
1109 vendor: true,
1110 nocrt: true,
1111 no_libcrt: true,
1112 stl: "none",
1113 system_shared_libs: [],
1114 compile_multilib: "64",
1115 }
1116`
1117
1118 vndkBp := `
1119 vndk_prebuilt_shared {
1120 name: "libvndk",
1121 version: "BOARD",
1122 target_arch: "arm64",
1123 vendor_available: true,
1124 vndk: {
1125 enabled: true,
1126 },
1127 arch: {
1128 arm64: {
1129 srcs: ["libvndk.so"],
Inseob Kim67be7322020-10-19 10:15:28 +09001130 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001131 },
1132 },
1133 }
1134`
1135
1136 vendorProprietaryBp := `
1137 cc_library {
1138 name: "libvendor_without_snapshot",
1139 vendor: true,
1140 nocrt: true,
1141 no_libcrt: true,
1142 stl: "none",
1143 system_shared_libs: [],
1144 compile_multilib: "64",
1145 }
1146
1147 cc_library_shared {
1148 name: "libclient",
1149 vendor: true,
1150 nocrt: true,
1151 no_libcrt: true,
1152 stl: "none",
1153 system_shared_libs: [],
1154 shared_libs: ["libvndk"],
1155 static_libs: ["libvendor", "libvendor_without_snapshot"],
1156 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001157 srcs: ["client.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001158 }
1159
1160 cc_binary {
1161 name: "bin_without_snapshot",
1162 vendor: true,
1163 nocrt: true,
1164 no_libcrt: true,
1165 stl: "none",
1166 system_shared_libs: [],
1167 static_libs: ["libvndk"],
1168 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001169 srcs: ["bin.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001170 }
1171
1172 vendor_snapshot_static {
1173 name: "libvndk",
1174 version: "BOARD",
1175 target_arch: "arm64",
1176 vendor: true,
1177 arch: {
1178 arm64: {
1179 src: "libvndk.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001180 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001181 },
1182 },
1183 }
1184
1185 vendor_snapshot_shared {
1186 name: "libvendor",
1187 version: "BOARD",
1188 target_arch: "arm64",
1189 vendor: true,
1190 arch: {
1191 arm64: {
1192 src: "libvendor.so",
Inseob Kim67be7322020-10-19 10:15:28 +09001193 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001194 },
1195 },
1196 }
1197
1198 vendor_snapshot_static {
1199 name: "libvendor",
1200 version: "BOARD",
1201 target_arch: "arm64",
1202 vendor: true,
1203 arch: {
1204 arm64: {
1205 src: "libvendor.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001206 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001207 },
1208 },
1209 }
1210
1211 vendor_snapshot_binary {
1212 name: "bin",
1213 version: "BOARD",
1214 target_arch: "arm64",
1215 vendor: true,
1216 arch: {
1217 arm64: {
1218 src: "bin",
1219 },
1220 },
1221 }
1222`
1223 depsBp := GatherRequiredDepsForTest(android.Android)
1224
1225 mockFS := map[string][]byte{
Inseob Kim67be7322020-10-19 10:15:28 +09001226 "deps/Android.bp": []byte(depsBp),
1227 "framework/Android.bp": []byte(frameworkBp),
1228 "vendor/Android.bp": []byte(vendorProprietaryBp),
1229 "vendor/bin": nil,
1230 "vendor/bin.cpp": nil,
1231 "vendor/client.cpp": nil,
1232 "vendor/include/libvndk/a.h": nil,
1233 "vendor/include/libvendor/b.h": nil,
1234 "vendor/libvndk.a": nil,
1235 "vendor/libvendor.a": nil,
1236 "vendor/libvendor.so": nil,
1237 "vndk/Android.bp": []byte(vndkBp),
1238 "vndk/include/libvndk/a.h": nil,
1239 "vndk/libvndk.so": nil,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001240 }
1241
1242 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1243 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1244 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1245 ctx := CreateTestContext()
1246 ctx.Register(config)
1247
1248 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "vendor/Android.bp", "vndk/Android.bp"})
1249 android.FailIfErrored(t, errs)
1250 _, errs = ctx.PrepareBuildActions(config)
1251 android.FailIfErrored(t, errs)
1252
1253 sharedVariant := "android_vendor.BOARD_arm64_armv8-a_shared"
1254 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1255 binaryVariant := "android_vendor.BOARD_arm64_armv8-a"
1256
1257 // libclient uses libvndk.vndk.BOARD.arm64, libvendor.vendor_static.BOARD.arm64, libvendor_without_snapshot
Inseob Kim67be7322020-10-19 10:15:28 +09001258 libclientCcFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("cc").Args["cFlags"]
1259 for _, includeFlags := range []string{
1260 "-Ivndk/include/libvndk", // libvndk
1261 "-Ivendor/include/libvendor", // libvendor
1262 } {
1263 if !strings.Contains(libclientCcFlags, includeFlags) {
1264 t.Errorf("flags for libclient must contain %#v, but was %#v.",
1265 includeFlags, libclientCcFlags)
1266 }
1267 }
Inseob Kim5f58ff72020-09-07 19:53:31 +09001268
Inseob Kim67be7322020-10-19 10:15:28 +09001269 libclientLdFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001270 for _, input := range [][]string{
1271 []string{sharedVariant, "libvndk.vndk.BOARD.arm64"},
1272 []string{staticVariant, "libvendor.vendor_static.BOARD.arm64"},
1273 []string{staticVariant, "libvendor_without_snapshot"},
1274 } {
1275 outputPaths := getOutputPaths(ctx, input[0] /* variant */, []string{input[1]} /* module name */)
Inseob Kim67be7322020-10-19 10:15:28 +09001276 if !strings.Contains(libclientLdFlags, outputPaths[0].String()) {
1277 t.Errorf("libflags for libclient must contain %#v, but was %#v", outputPaths[0], libclientLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001278 }
1279 }
1280
1281 // bin_without_snapshot uses libvndk.vendor_static.BOARD.arm64
Inseob Kim67be7322020-10-19 10:15:28 +09001282 binWithoutSnapshotCcFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("cc").Args["cFlags"]
1283 if !strings.Contains(binWithoutSnapshotCcFlags, "-Ivendor/include/libvndk") {
1284 t.Errorf("flags for bin_without_snapshot must contain %#v, but was %#v.",
1285 "-Ivendor/include/libvndk", binWithoutSnapshotCcFlags)
1286 }
1287
1288 binWithoutSnapshotLdFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001289 libVndkStaticOutputPaths := getOutputPaths(ctx, staticVariant, []string{"libvndk.vendor_static.BOARD.arm64"})
Inseob Kim67be7322020-10-19 10:15:28 +09001290 if !strings.Contains(binWithoutSnapshotLdFlags, libVndkStaticOutputPaths[0].String()) {
Inseob Kim5f58ff72020-09-07 19:53:31 +09001291 t.Errorf("libflags for bin_without_snapshot must contain %#v, but was %#v",
Inseob Kim67be7322020-10-19 10:15:28 +09001292 libVndkStaticOutputPaths[0], binWithoutSnapshotLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001293 }
1294
1295 // libvendor.so is installed by libvendor.vendor_shared.BOARD.arm64
1296 ctx.ModuleForTests("libvendor.vendor_shared.BOARD.arm64", sharedVariant).Output("libvendor.so")
1297
1298 // libvendor_without_snapshot.so is installed by libvendor_without_snapshot
1299 ctx.ModuleForTests("libvendor_without_snapshot", sharedVariant).Output("libvendor_without_snapshot.so")
1300
1301 // bin is installed by bin.vendor_binary.BOARD.arm64
1302 ctx.ModuleForTests("bin.vendor_binary.BOARD.arm64", binaryVariant).Output("bin")
1303
1304 // bin_without_snapshot is installed by bin_without_snapshot
1305 ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Output("bin_without_snapshot")
1306
1307 // libvendor and bin don't have vendor.BOARD variant
1308 libvendorVariants := ctx.ModuleVariantsForTests("libvendor")
1309 if inList(sharedVariant, libvendorVariants) {
1310 t.Errorf("libvendor must not have variant %#v, but it does", sharedVariant)
1311 }
1312
1313 binVariants := ctx.ModuleVariantsForTests("bin")
1314 if inList(binaryVariant, binVariants) {
1315 t.Errorf("bin must not have variant %#v, but it does", sharedVariant)
1316 }
1317}
1318
Inseob Kimc42f2f22020-07-29 20:32:10 +09001319func TestVendorSnapshotSanitizer(t *testing.T) {
1320 bp := `
1321 vendor_snapshot_static {
1322 name: "libsnapshot",
1323 vendor: true,
1324 target_arch: "arm64",
1325 version: "BOARD",
1326 arch: {
1327 arm64: {
1328 src: "libsnapshot.a",
1329 cfi: {
1330 src: "libsnapshot.cfi.a",
1331 }
1332 },
1333 },
1334 }
1335`
1336 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1337 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1338 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1339 ctx := testCcWithConfig(t, config)
1340
1341 // Check non-cfi and cfi variant.
1342 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1343 staticCfiVariant := "android_vendor.BOARD_arm64_armv8-a_static_cfi"
1344
1345 staticModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticVariant).Module().(*Module)
1346 assertString(t, staticModule.outputFile.Path().Base(), "libsnapshot.a")
1347
1348 staticCfiModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticCfiVariant).Module().(*Module)
1349 assertString(t, staticCfiModule.outputFile.Path().Base(), "libsnapshot.cfi.a")
1350}
1351
Bill Peckham945441c2020-08-31 16:07:58 -07001352func assertExcludeFromVendorSnapshotIs(t *testing.T, c *Module, expected bool) {
1353 t.Helper()
1354 if c.ExcludeFromVendorSnapshot() != expected {
1355 t.Errorf("expected %q ExcludeFromVendorSnapshot to be %t", c.String(), expected)
1356 }
1357}
1358
1359func TestVendorSnapshotExclude(t *testing.T) {
1360
1361 // This test verifies that the exclude_from_vendor_snapshot property
1362 // makes its way from the Android.bp source file into the module data
1363 // structure. It also verifies that modules are correctly included or
1364 // excluded in the vendor snapshot based on their path (framework or
1365 // vendor) and the exclude_from_vendor_snapshot property.
1366
1367 frameworkBp := `
1368 cc_library_shared {
1369 name: "libinclude",
1370 srcs: ["src/include.cpp"],
1371 vendor_available: true,
1372 }
1373 cc_library_shared {
1374 name: "libexclude",
1375 srcs: ["src/exclude.cpp"],
1376 vendor: true,
1377 exclude_from_vendor_snapshot: true,
1378 }
1379 `
1380
1381 vendorProprietaryBp := `
1382 cc_library_shared {
1383 name: "libvendor",
1384 srcs: ["vendor.cpp"],
1385 vendor: true,
1386 }
1387 `
1388
1389 depsBp := GatherRequiredDepsForTest(android.Android)
1390
1391 mockFS := map[string][]byte{
1392 "deps/Android.bp": []byte(depsBp),
1393 "framework/Android.bp": []byte(frameworkBp),
1394 "framework/include.cpp": nil,
1395 "framework/exclude.cpp": nil,
1396 "device/Android.bp": []byte(vendorProprietaryBp),
1397 "device/vendor.cpp": nil,
1398 }
1399
1400 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1401 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1402 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1403 ctx := CreateTestContext()
1404 ctx.Register(config)
1405
1406 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1407 android.FailIfErrored(t, errs)
1408 _, errs = ctx.PrepareBuildActions(config)
1409 android.FailIfErrored(t, errs)
1410
1411 // Test an include and exclude framework module.
1412 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1413 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", vendorVariant).Module().(*Module), false)
1414 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libexclude", vendorVariant).Module().(*Module), true)
1415
1416 // A vendor module is excluded, but by its path, not the
1417 // exclude_from_vendor_snapshot property.
1418 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libvendor", vendorVariant).Module().(*Module), false)
1419
1420 // Verify the content of the vendor snapshot.
1421
1422 snapshotDir := "vendor-snapshot"
1423 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1424 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1425
1426 var includeJsonFiles []string
1427 var excludeJsonFiles []string
1428
1429 for _, arch := range [][]string{
1430 []string{"arm64", "armv8-a"},
1431 []string{"arm", "armv7-a-neon"},
1432 } {
1433 archType := arch[0]
1434 archVariant := arch[1]
1435 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1436
1437 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1438 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1439
1440 // Included modules
1441 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1442 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1443
1444 // Excluded modules
1445 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1446 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1447 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1448 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1449 }
1450
1451 // Verify that each json file for an included module has a rule.
1452 for _, jsonFile := range includeJsonFiles {
1453 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1454 t.Errorf("include json file %q not found", jsonFile)
1455 }
1456 }
1457
1458 // Verify that each json file for an excluded module has no rule.
1459 for _, jsonFile := range excludeJsonFiles {
1460 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1461 t.Errorf("exclude json file %q found", jsonFile)
1462 }
1463 }
1464}
1465
1466func TestVendorSnapshotExcludeInVendorProprietaryPathErrors(t *testing.T) {
1467
1468 // This test verifies that using the exclude_from_vendor_snapshot
1469 // property on a module in a vendor proprietary path generates an
1470 // error. These modules are already excluded, so we prohibit using the
1471 // property in this way, which could add to confusion.
1472
1473 vendorProprietaryBp := `
1474 cc_library_shared {
1475 name: "libvendor",
1476 srcs: ["vendor.cpp"],
1477 vendor: true,
1478 exclude_from_vendor_snapshot: true,
1479 }
1480 `
1481
1482 depsBp := GatherRequiredDepsForTest(android.Android)
1483
1484 mockFS := map[string][]byte{
1485 "deps/Android.bp": []byte(depsBp),
1486 "device/Android.bp": []byte(vendorProprietaryBp),
1487 "device/vendor.cpp": nil,
1488 }
1489
1490 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1491 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1492 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1493 ctx := CreateTestContext()
1494 ctx.Register(config)
1495
1496 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "device/Android.bp"})
1497 android.FailIfErrored(t, errs)
1498
1499 _, errs = ctx.PrepareBuildActions(config)
1500 android.CheckErrorsAgainstExpectations(t, errs, []string{
1501 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1502 `module "libvendor\{.+,image:vendor.+,arch:arm_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1503 })
1504}
1505
1506func TestVendorSnapshotExcludeWithVendorAvailable(t *testing.T) {
1507
1508 // This test verifies that using the exclude_from_vendor_snapshot
1509 // property on a module that is vendor available generates an error. A
1510 // vendor available module must be captured in the vendor snapshot and
1511 // must not built from source when building the vendor image against
1512 // the vendor snapshot.
1513
1514 frameworkBp := `
1515 cc_library_shared {
1516 name: "libinclude",
1517 srcs: ["src/include.cpp"],
1518 vendor_available: true,
1519 exclude_from_vendor_snapshot: true,
1520 }
1521 `
1522
1523 depsBp := GatherRequiredDepsForTest(android.Android)
1524
1525 mockFS := map[string][]byte{
1526 "deps/Android.bp": []byte(depsBp),
1527 "framework/Android.bp": []byte(frameworkBp),
1528 "framework/include.cpp": nil,
1529 }
1530
1531 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1532 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1533 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1534 ctx := CreateTestContext()
1535 ctx.Register(config)
1536
1537 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp"})
1538 android.FailIfErrored(t, errs)
1539
1540 _, errs = ctx.PrepareBuildActions(config)
1541 android.CheckErrorsAgainstExpectations(t, errs, []string{
1542 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1543 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1544 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1545 `module "libinclude\{.+,image:vendor.+,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1546 })
1547}
1548
Jooyung Hana70f0672019-01-18 15:20:43 +09001549func TestDoubleLoadableDepError(t *testing.T) {
1550 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1551 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1552 cc_library {
1553 name: "libllndk",
1554 shared_libs: ["libnondoubleloadable"],
1555 }
1556
1557 llndk_library {
1558 name: "libllndk",
1559 symbol_file: "",
1560 }
1561
1562 cc_library {
1563 name: "libnondoubleloadable",
1564 vendor_available: true,
1565 vndk: {
1566 enabled: true,
1567 },
1568 }
1569 `)
1570
1571 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1572 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1573 cc_library {
1574 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001575 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001576 shared_libs: ["libnondoubleloadable"],
1577 }
1578
1579 llndk_library {
1580 name: "libllndk",
1581 symbol_file: "",
1582 }
1583
1584 cc_library {
1585 name: "libnondoubleloadable",
1586 vendor_available: true,
1587 }
1588 `)
1589
1590 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
1591 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1592 cc_library {
1593 name: "libdoubleloadable",
1594 vendor_available: true,
1595 double_loadable: true,
1596 shared_libs: ["libnondoubleloadable"],
1597 }
1598
1599 cc_library {
1600 name: "libnondoubleloadable",
1601 vendor_available: true,
1602 }
1603 `)
1604
1605 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
1606 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1607 cc_library {
1608 name: "libdoubleloadable",
1609 vendor_available: true,
1610 double_loadable: true,
1611 shared_libs: ["libnondoubleloadable"],
1612 }
1613
1614 cc_library {
1615 name: "libnondoubleloadable",
1616 vendor_available: true,
1617 vndk: {
1618 enabled: true,
1619 },
1620 }
1621 `)
1622
1623 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
1624 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1625 cc_library {
1626 name: "libdoubleloadable",
1627 vendor_available: true,
1628 vndk: {
1629 enabled: true,
1630 },
1631 double_loadable: true,
1632 shared_libs: ["libnondoubleloadable"],
1633 }
1634
1635 cc_library {
1636 name: "libnondoubleloadable",
1637 vendor_available: false,
1638 vndk: {
1639 enabled: true,
1640 },
1641 }
1642 `)
1643
1644 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1645 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1646 cc_library {
1647 name: "libllndk",
1648 shared_libs: ["libcoreonly"],
1649 }
1650
1651 llndk_library {
1652 name: "libllndk",
1653 symbol_file: "",
1654 }
1655
1656 cc_library {
1657 name: "libcoreonly",
1658 shared_libs: ["libvendoravailable"],
1659 }
1660
1661 // indirect dependency of LLNDK
1662 cc_library {
1663 name: "libvendoravailable",
1664 vendor_available: true,
1665 }
1666 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001667}
1668
Jooyung Han479ca172020-10-19 18:51:07 +09001669func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1670 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1671 cc_library {
1672 name: "libvndksp",
1673 shared_libs: ["libanothervndksp"],
1674 vendor_available: true,
1675 vndk: {
1676 enabled: true,
1677 support_system_process: true,
1678 }
1679 }
1680
1681 cc_library {
1682 name: "libllndk",
1683 shared_libs: ["libanothervndksp"],
1684 }
1685
1686 llndk_library {
1687 name: "libllndk",
1688 symbol_file: "",
1689 }
1690
1691 cc_library {
1692 name: "libanothervndksp",
1693 vendor_available: true,
1694 }
1695 `)
1696}
1697
Logan Chienf3511742017-10-31 18:04:35 +08001698func TestVndkExt(t *testing.T) {
1699 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001700 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001701 cc_library {
1702 name: "libvndk",
1703 vendor_available: true,
1704 vndk: {
1705 enabled: true,
1706 },
1707 nocrt: true,
1708 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001709 cc_library {
1710 name: "libvndk2",
1711 vendor_available: true,
1712 vndk: {
1713 enabled: true,
1714 },
1715 target: {
1716 vendor: {
1717 suffix: "-suffix",
1718 },
1719 },
1720 nocrt: true,
1721 }
Logan Chienf3511742017-10-31 18:04:35 +08001722
1723 cc_library {
1724 name: "libvndk_ext",
1725 vendor: true,
1726 vndk: {
1727 enabled: true,
1728 extends: "libvndk",
1729 },
1730 nocrt: true,
1731 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001732
1733 cc_library {
1734 name: "libvndk2_ext",
1735 vendor: true,
1736 vndk: {
1737 enabled: true,
1738 extends: "libvndk2",
1739 },
1740 nocrt: true,
1741 }
Logan Chienf3511742017-10-31 18:04:35 +08001742
Justin Yun0ecf0b22020-02-28 15:07:59 +09001743 cc_library {
1744 name: "libvndk_ext_product",
1745 product_specific: true,
1746 vndk: {
1747 enabled: true,
1748 extends: "libvndk",
1749 },
1750 nocrt: true,
1751 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001752
Justin Yun0ecf0b22020-02-28 15:07:59 +09001753 cc_library {
1754 name: "libvndk2_ext_product",
1755 product_specific: true,
1756 vndk: {
1757 enabled: true,
1758 extends: "libvndk2",
1759 },
1760 nocrt: true,
1761 }
1762 `
1763 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1764 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1765 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1766 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1767
1768 ctx := testCcWithConfig(t, config)
1769
1770 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1771 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1772
1773 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1774 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1775
1776 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1777 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001778}
1779
Logan Chiend3c59a22018-03-29 14:08:15 +08001780func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001781 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1782 ctx := testCcNoVndk(t, `
1783 cc_library {
1784 name: "libvndk",
1785 vendor_available: true,
1786 vndk: {
1787 enabled: true,
1788 },
1789 nocrt: true,
1790 }
1791
1792 cc_library {
1793 name: "libvndk_ext",
1794 vendor: true,
1795 vndk: {
1796 enabled: true,
1797 extends: "libvndk",
1798 },
1799 nocrt: true,
1800 }
1801 `)
1802
1803 // Ensures that the core variant of "libvndk_ext" can be found.
1804 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1805 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1806 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1807 }
1808}
1809
Justin Yun0ecf0b22020-02-28 15:07:59 +09001810func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1811 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
1812 ctx := testCc(t, `
1813 cc_library {
1814 name: "libvndk",
1815 vendor_available: true,
1816 vndk: {
1817 enabled: true,
1818 },
1819 nocrt: true,
1820 }
1821
1822 cc_library {
1823 name: "libvndk_ext_product",
1824 product_specific: true,
1825 vndk: {
1826 enabled: true,
1827 extends: "libvndk",
1828 },
1829 nocrt: true,
1830 }
1831 `)
1832
1833 // Ensures that the core variant of "libvndk_ext_product" can be found.
1834 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1835 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1836 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1837 }
1838}
1839
Logan Chienf3511742017-10-31 18:04:35 +08001840func TestVndkExtError(t *testing.T) {
1841 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001842 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001843 cc_library {
1844 name: "libvndk",
1845 vendor_available: true,
1846 vndk: {
1847 enabled: true,
1848 },
1849 nocrt: true,
1850 }
1851
1852 cc_library {
1853 name: "libvndk_ext",
1854 vndk: {
1855 enabled: true,
1856 extends: "libvndk",
1857 },
1858 nocrt: true,
1859 }
1860 `)
1861
1862 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1863 cc_library {
1864 name: "libvndk",
1865 vendor_available: true,
1866 vndk: {
1867 enabled: true,
1868 },
1869 nocrt: true,
1870 }
1871
1872 cc_library {
1873 name: "libvndk_ext",
1874 vendor: true,
1875 vndk: {
1876 enabled: true,
1877 },
1878 nocrt: true,
1879 }
1880 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001881
1882 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1883 cc_library {
1884 name: "libvndk",
1885 vendor_available: true,
1886 vndk: {
1887 enabled: true,
1888 },
1889 nocrt: true,
1890 }
1891
1892 cc_library {
1893 name: "libvndk_ext_product",
1894 product_specific: true,
1895 vndk: {
1896 enabled: true,
1897 },
1898 nocrt: true,
1899 }
1900 `)
1901
1902 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1903 cc_library {
1904 name: "libvndk",
1905 vendor_available: true,
1906 vndk: {
1907 enabled: true,
1908 },
1909 nocrt: true,
1910 }
1911
1912 cc_library {
1913 name: "libvndk_ext_product",
1914 product_specific: true,
1915 vendor_available: true,
1916 vndk: {
1917 enabled: true,
1918 extends: "libvndk",
1919 },
1920 nocrt: true,
1921 }
1922 `)
Logan Chienf3511742017-10-31 18:04:35 +08001923}
1924
1925func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1926 // This test ensures an error is emitted for inconsistent support_system_process.
1927 testCcError(t, "module \".*\" with mismatched support_system_process", `
1928 cc_library {
1929 name: "libvndk",
1930 vendor_available: true,
1931 vndk: {
1932 enabled: true,
1933 },
1934 nocrt: true,
1935 }
1936
1937 cc_library {
1938 name: "libvndk_sp_ext",
1939 vendor: true,
1940 vndk: {
1941 enabled: true,
1942 extends: "libvndk",
1943 support_system_process: true,
1944 },
1945 nocrt: true,
1946 }
1947 `)
1948
1949 testCcError(t, "module \".*\" with mismatched support_system_process", `
1950 cc_library {
1951 name: "libvndk_sp",
1952 vendor_available: true,
1953 vndk: {
1954 enabled: true,
1955 support_system_process: true,
1956 },
1957 nocrt: true,
1958 }
1959
1960 cc_library {
1961 name: "libvndk_ext",
1962 vendor: true,
1963 vndk: {
1964 enabled: true,
1965 extends: "libvndk_sp",
1966 },
1967 nocrt: true,
1968 }
1969 `)
1970}
1971
1972func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001973 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08001974 // with `vendor_available: false`.
1975 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1976 cc_library {
1977 name: "libvndk",
1978 vendor_available: false,
1979 vndk: {
1980 enabled: true,
1981 },
1982 nocrt: true,
1983 }
1984
1985 cc_library {
1986 name: "libvndk_ext",
1987 vendor: true,
1988 vndk: {
1989 enabled: true,
1990 extends: "libvndk",
1991 },
1992 nocrt: true,
1993 }
1994 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001995
1996 testCcErrorProductVndk(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1997 cc_library {
1998 name: "libvndk",
1999 vendor_available: false,
2000 vndk: {
2001 enabled: true,
2002 },
2003 nocrt: true,
2004 }
2005
2006 cc_library {
2007 name: "libvndk_ext_product",
2008 product_specific: true,
2009 vndk: {
2010 enabled: true,
2011 extends: "libvndk",
2012 },
2013 nocrt: true,
2014 }
2015 `)
Logan Chienf3511742017-10-31 18:04:35 +08002016}
2017
Logan Chiend3c59a22018-03-29 14:08:15 +08002018func TestVendorModuleUseVndkExt(t *testing.T) {
2019 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002020 testCc(t, `
2021 cc_library {
2022 name: "libvndk",
2023 vendor_available: true,
2024 vndk: {
2025 enabled: true,
2026 },
2027 nocrt: true,
2028 }
2029
2030 cc_library {
2031 name: "libvndk_ext",
2032 vendor: true,
2033 vndk: {
2034 enabled: true,
2035 extends: "libvndk",
2036 },
2037 nocrt: true,
2038 }
2039
2040 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08002041 name: "libvndk_sp",
2042 vendor_available: true,
2043 vndk: {
2044 enabled: true,
2045 support_system_process: true,
2046 },
2047 nocrt: true,
2048 }
2049
2050 cc_library {
2051 name: "libvndk_sp_ext",
2052 vendor: true,
2053 vndk: {
2054 enabled: true,
2055 extends: "libvndk_sp",
2056 support_system_process: true,
2057 },
2058 nocrt: true,
2059 }
2060
2061 cc_library {
2062 name: "libvendor",
2063 vendor: true,
2064 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
2065 nocrt: true,
2066 }
2067 `)
2068}
2069
Logan Chiend3c59a22018-03-29 14:08:15 +08002070func TestVndkExtUseVendorLib(t *testing.T) {
2071 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08002072 testCc(t, `
2073 cc_library {
2074 name: "libvndk",
2075 vendor_available: true,
2076 vndk: {
2077 enabled: true,
2078 },
2079 nocrt: true,
2080 }
2081
2082 cc_library {
2083 name: "libvndk_ext",
2084 vendor: true,
2085 vndk: {
2086 enabled: true,
2087 extends: "libvndk",
2088 },
2089 shared_libs: ["libvendor"],
2090 nocrt: true,
2091 }
2092
2093 cc_library {
2094 name: "libvendor",
2095 vendor: true,
2096 nocrt: true,
2097 }
2098 `)
Logan Chienf3511742017-10-31 18:04:35 +08002099
Logan Chiend3c59a22018-03-29 14:08:15 +08002100 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
2101 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08002102 cc_library {
2103 name: "libvndk_sp",
2104 vendor_available: true,
2105 vndk: {
2106 enabled: true,
2107 support_system_process: true,
2108 },
2109 nocrt: true,
2110 }
2111
2112 cc_library {
2113 name: "libvndk_sp_ext",
2114 vendor: true,
2115 vndk: {
2116 enabled: true,
2117 extends: "libvndk_sp",
2118 support_system_process: true,
2119 },
2120 shared_libs: ["libvendor"], // Cause an error
2121 nocrt: true,
2122 }
2123
2124 cc_library {
2125 name: "libvendor",
2126 vendor: true,
2127 nocrt: true,
2128 }
2129 `)
2130}
2131
Justin Yun0ecf0b22020-02-28 15:07:59 +09002132func TestProductVndkExtDependency(t *testing.T) {
2133 bp := `
2134 cc_library {
2135 name: "libvndk",
2136 vendor_available: true,
2137 vndk: {
2138 enabled: true,
2139 },
2140 nocrt: true,
2141 }
2142
2143 cc_library {
2144 name: "libvndk_ext_product",
2145 product_specific: true,
2146 vndk: {
2147 enabled: true,
2148 extends: "libvndk",
2149 },
2150 shared_libs: ["libproduct_for_vndklibs"],
2151 nocrt: true,
2152 }
2153
2154 cc_library {
2155 name: "libvndk_sp",
2156 vendor_available: true,
2157 vndk: {
2158 enabled: true,
2159 support_system_process: true,
2160 },
2161 nocrt: true,
2162 }
2163
2164 cc_library {
2165 name: "libvndk_sp_ext_product",
2166 product_specific: true,
2167 vndk: {
2168 enabled: true,
2169 extends: "libvndk_sp",
2170 support_system_process: true,
2171 },
2172 shared_libs: ["libproduct_for_vndklibs"],
2173 nocrt: true,
2174 }
2175
2176 cc_library {
2177 name: "libproduct",
2178 product_specific: true,
2179 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
2180 nocrt: true,
2181 }
2182
2183 cc_library {
2184 name: "libproduct_for_vndklibs",
2185 product_specific: true,
2186 nocrt: true,
2187 }
2188 `
2189 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2190 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2191 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2192 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2193
2194 testCcWithConfig(t, config)
2195}
2196
Logan Chiend3c59a22018-03-29 14:08:15 +08002197func TestVndkSpExtUseVndkError(t *testing.T) {
2198 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
2199 // library.
2200 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2201 cc_library {
2202 name: "libvndk",
2203 vendor_available: true,
2204 vndk: {
2205 enabled: true,
2206 },
2207 nocrt: true,
2208 }
2209
2210 cc_library {
2211 name: "libvndk_sp",
2212 vendor_available: true,
2213 vndk: {
2214 enabled: true,
2215 support_system_process: true,
2216 },
2217 nocrt: true,
2218 }
2219
2220 cc_library {
2221 name: "libvndk_sp_ext",
2222 vendor: true,
2223 vndk: {
2224 enabled: true,
2225 extends: "libvndk_sp",
2226 support_system_process: true,
2227 },
2228 shared_libs: ["libvndk"], // Cause an error
2229 nocrt: true,
2230 }
2231 `)
2232
2233 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
2234 // library.
2235 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2236 cc_library {
2237 name: "libvndk",
2238 vendor_available: true,
2239 vndk: {
2240 enabled: true,
2241 },
2242 nocrt: true,
2243 }
2244
2245 cc_library {
2246 name: "libvndk_ext",
2247 vendor: true,
2248 vndk: {
2249 enabled: true,
2250 extends: "libvndk",
2251 },
2252 nocrt: true,
2253 }
2254
2255 cc_library {
2256 name: "libvndk_sp",
2257 vendor_available: true,
2258 vndk: {
2259 enabled: true,
2260 support_system_process: true,
2261 },
2262 nocrt: true,
2263 }
2264
2265 cc_library {
2266 name: "libvndk_sp_ext",
2267 vendor: true,
2268 vndk: {
2269 enabled: true,
2270 extends: "libvndk_sp",
2271 support_system_process: true,
2272 },
2273 shared_libs: ["libvndk_ext"], // Cause an error
2274 nocrt: true,
2275 }
2276 `)
2277}
2278
2279func TestVndkUseVndkExtError(t *testing.T) {
2280 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2281 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002282 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2283 cc_library {
2284 name: "libvndk",
2285 vendor_available: true,
2286 vndk: {
2287 enabled: true,
2288 },
2289 nocrt: true,
2290 }
2291
2292 cc_library {
2293 name: "libvndk_ext",
2294 vendor: true,
2295 vndk: {
2296 enabled: true,
2297 extends: "libvndk",
2298 },
2299 nocrt: true,
2300 }
2301
2302 cc_library {
2303 name: "libvndk2",
2304 vendor_available: true,
2305 vndk: {
2306 enabled: true,
2307 },
2308 shared_libs: ["libvndk_ext"],
2309 nocrt: true,
2310 }
2311 `)
2312
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002313 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002314 cc_library {
2315 name: "libvndk",
2316 vendor_available: true,
2317 vndk: {
2318 enabled: true,
2319 },
2320 nocrt: true,
2321 }
2322
2323 cc_library {
2324 name: "libvndk_ext",
2325 vendor: true,
2326 vndk: {
2327 enabled: true,
2328 extends: "libvndk",
2329 },
2330 nocrt: true,
2331 }
2332
2333 cc_library {
2334 name: "libvndk2",
2335 vendor_available: true,
2336 vndk: {
2337 enabled: true,
2338 },
2339 target: {
2340 vendor: {
2341 shared_libs: ["libvndk_ext"],
2342 },
2343 },
2344 nocrt: true,
2345 }
2346 `)
2347
2348 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2349 cc_library {
2350 name: "libvndk_sp",
2351 vendor_available: true,
2352 vndk: {
2353 enabled: true,
2354 support_system_process: true,
2355 },
2356 nocrt: true,
2357 }
2358
2359 cc_library {
2360 name: "libvndk_sp_ext",
2361 vendor: true,
2362 vndk: {
2363 enabled: true,
2364 extends: "libvndk_sp",
2365 support_system_process: true,
2366 },
2367 nocrt: true,
2368 }
2369
2370 cc_library {
2371 name: "libvndk_sp_2",
2372 vendor_available: true,
2373 vndk: {
2374 enabled: true,
2375 support_system_process: true,
2376 },
2377 shared_libs: ["libvndk_sp_ext"],
2378 nocrt: true,
2379 }
2380 `)
2381
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002382 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002383 cc_library {
2384 name: "libvndk_sp",
2385 vendor_available: true,
2386 vndk: {
2387 enabled: true,
2388 },
2389 nocrt: true,
2390 }
2391
2392 cc_library {
2393 name: "libvndk_sp_ext",
2394 vendor: true,
2395 vndk: {
2396 enabled: true,
2397 extends: "libvndk_sp",
2398 },
2399 nocrt: true,
2400 }
2401
2402 cc_library {
2403 name: "libvndk_sp2",
2404 vendor_available: true,
2405 vndk: {
2406 enabled: true,
2407 },
2408 target: {
2409 vendor: {
2410 shared_libs: ["libvndk_sp_ext"],
2411 },
2412 },
2413 nocrt: true,
2414 }
2415 `)
2416}
2417
Justin Yun5f7f7e82019-11-18 19:52:14 +09002418func TestEnforceProductVndkVersion(t *testing.T) {
2419 bp := `
2420 cc_library {
2421 name: "libllndk",
2422 }
2423 llndk_library {
2424 name: "libllndk",
2425 symbol_file: "",
2426 }
2427 cc_library {
2428 name: "libvndk",
2429 vendor_available: true,
2430 vndk: {
2431 enabled: true,
2432 },
2433 nocrt: true,
2434 }
2435 cc_library {
2436 name: "libvndk_sp",
2437 vendor_available: true,
2438 vndk: {
2439 enabled: true,
2440 support_system_process: true,
2441 },
2442 nocrt: true,
2443 }
2444 cc_library {
2445 name: "libva",
2446 vendor_available: true,
2447 nocrt: true,
2448 }
2449 cc_library {
2450 name: "libproduct_va",
2451 product_specific: true,
2452 vendor_available: true,
2453 nocrt: true,
2454 }
2455 cc_library {
2456 name: "libprod",
2457 product_specific: true,
2458 shared_libs: [
2459 "libllndk",
2460 "libvndk",
2461 "libvndk_sp",
2462 "libva",
2463 "libproduct_va",
2464 ],
2465 nocrt: true,
2466 }
2467 cc_library {
2468 name: "libvendor",
2469 vendor: true,
2470 shared_libs: [
2471 "libllndk",
2472 "libvndk",
2473 "libvndk_sp",
2474 "libva",
2475 "libproduct_va",
2476 ],
2477 nocrt: true,
2478 }
2479 `
2480
2481 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2482 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2483 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2484 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2485
2486 ctx := testCcWithConfig(t, config)
2487
Jooyung Han261e1582020-10-20 18:54:21 +09002488 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2489 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun5f7f7e82019-11-18 19:52:14 +09002490}
2491
2492func TestEnforceProductVndkVersionErrors(t *testing.T) {
2493 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2494 cc_library {
2495 name: "libprod",
2496 product_specific: true,
2497 shared_libs: [
2498 "libvendor",
2499 ],
2500 nocrt: true,
2501 }
2502 cc_library {
2503 name: "libvendor",
2504 vendor: true,
2505 nocrt: true,
2506 }
2507 `)
2508 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2509 cc_library {
2510 name: "libprod",
2511 product_specific: true,
2512 shared_libs: [
2513 "libsystem",
2514 ],
2515 nocrt: true,
2516 }
2517 cc_library {
2518 name: "libsystem",
2519 nocrt: true,
2520 }
2521 `)
2522 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
2523 cc_library {
2524 name: "libprod",
2525 product_specific: true,
2526 shared_libs: [
2527 "libvndk_private",
2528 ],
2529 nocrt: true,
2530 }
2531 cc_library {
2532 name: "libvndk_private",
2533 vendor_available: false,
2534 vndk: {
2535 enabled: true,
2536 },
2537 nocrt: true,
2538 }
2539 `)
2540 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2541 cc_library {
2542 name: "libprod",
2543 product_specific: true,
2544 shared_libs: [
2545 "libsystem_ext",
2546 ],
2547 nocrt: true,
2548 }
2549 cc_library {
2550 name: "libsystem_ext",
2551 system_ext_specific: true,
2552 nocrt: true,
2553 }
2554 `)
2555 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2556 cc_library {
2557 name: "libsystem",
2558 shared_libs: [
2559 "libproduct_va",
2560 ],
2561 nocrt: true,
2562 }
2563 cc_library {
2564 name: "libproduct_va",
2565 product_specific: true,
2566 vendor_available: true,
2567 nocrt: true,
2568 }
2569 `)
2570}
2571
Jooyung Han38002912019-05-16 04:01:54 +09002572func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002573 bp := `
2574 cc_library {
2575 name: "libvndk",
2576 vendor_available: true,
2577 vndk: {
2578 enabled: true,
2579 },
2580 }
2581 cc_library {
2582 name: "libvndksp",
2583 vendor_available: true,
2584 vndk: {
2585 enabled: true,
2586 support_system_process: true,
2587 },
2588 }
2589 cc_library {
2590 name: "libvndkprivate",
2591 vendor_available: false,
2592 vndk: {
2593 enabled: true,
2594 },
2595 }
2596 cc_library {
2597 name: "libvendor",
2598 vendor: true,
2599 }
2600 cc_library {
2601 name: "libvndkext",
2602 vendor: true,
2603 vndk: {
2604 enabled: true,
2605 extends: "libvndk",
2606 },
2607 }
2608 vndk_prebuilt_shared {
2609 name: "prevndk",
2610 version: "27",
2611 target_arch: "arm",
2612 binder32bit: true,
2613 vendor_available: true,
2614 vndk: {
2615 enabled: true,
2616 },
2617 arch: {
2618 arm: {
2619 srcs: ["liba.so"],
2620 },
2621 },
2622 }
2623 cc_library {
2624 name: "libllndk",
2625 }
2626 llndk_library {
2627 name: "libllndk",
2628 symbol_file: "",
2629 }
2630 cc_library {
2631 name: "libllndkprivate",
2632 }
2633 llndk_library {
2634 name: "libllndkprivate",
2635 vendor_available: false,
2636 symbol_file: "",
2637 }`
2638
2639 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002640 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2641 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2642 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002643 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002644
Jooyung Han0302a842019-10-30 18:43:49 +09002645 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002646 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09002647 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002648 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09002649 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002650 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09002651 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002652 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09002653
Colin Crossfb0c16e2019-11-20 17:12:35 -08002654 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002655
Jooyung Han38002912019-05-16 04:01:54 +09002656 tests := []struct {
2657 variant string
2658 name string
2659 expected string
2660 }{
2661 {vendorVariant, "libvndk", "native:vndk"},
2662 {vendorVariant, "libvndksp", "native:vndk"},
2663 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2664 {vendorVariant, "libvendor", "native:vendor"},
2665 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09002666 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002667 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002668 {coreVariant, "libvndk", "native:platform"},
2669 {coreVariant, "libvndkprivate", "native:platform"},
2670 {coreVariant, "libllndk", "native:platform"},
2671 }
2672 for _, test := range tests {
2673 t.Run(test.name, func(t *testing.T) {
2674 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2675 assertString(t, module.makeLinkType, test.expected)
2676 })
2677 }
2678}
2679
Colin Cross0af4b842015-04-30 16:36:18 -07002680var (
2681 str11 = "01234567891"
2682 str10 = str11[:10]
2683 str9 = str11[:9]
2684 str5 = str11[:5]
2685 str4 = str11[:4]
2686)
2687
2688var splitListForSizeTestCases = []struct {
2689 in []string
2690 out [][]string
2691 size int
2692}{
2693 {
2694 in: []string{str10},
2695 out: [][]string{{str10}},
2696 size: 10,
2697 },
2698 {
2699 in: []string{str9},
2700 out: [][]string{{str9}},
2701 size: 10,
2702 },
2703 {
2704 in: []string{str5},
2705 out: [][]string{{str5}},
2706 size: 10,
2707 },
2708 {
2709 in: []string{str11},
2710 out: nil,
2711 size: 10,
2712 },
2713 {
2714 in: []string{str10, str10},
2715 out: [][]string{{str10}, {str10}},
2716 size: 10,
2717 },
2718 {
2719 in: []string{str9, str10},
2720 out: [][]string{{str9}, {str10}},
2721 size: 10,
2722 },
2723 {
2724 in: []string{str10, str9},
2725 out: [][]string{{str10}, {str9}},
2726 size: 10,
2727 },
2728 {
2729 in: []string{str5, str4},
2730 out: [][]string{{str5, str4}},
2731 size: 10,
2732 },
2733 {
2734 in: []string{str5, str4, str5},
2735 out: [][]string{{str5, str4}, {str5}},
2736 size: 10,
2737 },
2738 {
2739 in: []string{str5, str4, str5, str4},
2740 out: [][]string{{str5, str4}, {str5, str4}},
2741 size: 10,
2742 },
2743 {
2744 in: []string{str5, str4, str5, str5},
2745 out: [][]string{{str5, str4}, {str5}, {str5}},
2746 size: 10,
2747 },
2748 {
2749 in: []string{str5, str5, str5, str4},
2750 out: [][]string{{str5}, {str5}, {str5, str4}},
2751 size: 10,
2752 },
2753 {
2754 in: []string{str9, str11},
2755 out: nil,
2756 size: 10,
2757 },
2758 {
2759 in: []string{str11, str9},
2760 out: nil,
2761 size: 10,
2762 },
2763}
2764
2765func TestSplitListForSize(t *testing.T) {
2766 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08002767 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07002768
2769 var outStrings [][]string
2770
2771 if len(out) > 0 {
2772 outStrings = make([][]string, len(out))
2773 for i, o := range out {
2774 outStrings[i] = o.Strings()
2775 }
2776 }
2777
2778 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07002779 t.Errorf("incorrect output:")
2780 t.Errorf(" input: %#v", testCase.in)
2781 t.Errorf(" size: %d", testCase.size)
2782 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07002783 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07002784 }
2785 }
2786}
Jeff Gaston294356f2017-09-27 17:05:30 -07002787
2788var staticLinkDepOrderTestCases = []struct {
2789 // This is a string representation of a map[moduleName][]moduleDependency .
2790 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002791 inStatic string
2792
2793 // This is a string representation of a map[moduleName][]moduleDependency .
2794 // It models the dependencies declared in an Android.bp file.
2795 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002796
2797 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2798 // The keys of allOrdered specify which modules we would like to check.
2799 // The values of allOrdered specify the expected result (of the transitive closure of all
2800 // dependencies) for each module to test
2801 allOrdered string
2802
2803 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2804 // The keys of outOrdered specify which modules we would like to check.
2805 // The values of outOrdered specify the expected result (of the ordered linker command line)
2806 // for each module to test.
2807 outOrdered string
2808}{
2809 // Simple tests
2810 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002811 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002812 outOrdered: "",
2813 },
2814 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002815 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002816 outOrdered: "a:",
2817 },
2818 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002819 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002820 outOrdered: "a:b; b:",
2821 },
2822 // Tests of reordering
2823 {
2824 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002825 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002826 outOrdered: "a:b,c,d; b:d; c:d; d:",
2827 },
2828 {
2829 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002830 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002831 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2832 },
2833 {
2834 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002835 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002836 outOrdered: "a:d,b,e,c; d:b; e:c",
2837 },
2838 {
2839 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002840 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002841 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2842 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2843 },
2844 {
2845 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002846 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 -07002847 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2848 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2849 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002850 // shared dependencies
2851 {
2852 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2853 // So, we don't actually have to check that a shared dependency of c will change the order
2854 // of a library that depends statically on b and on c. We only need to check that if c has
2855 // a shared dependency on b, that that shows up in allOrdered.
2856 inShared: "c:b",
2857 allOrdered: "c:b",
2858 outOrdered: "c:",
2859 },
2860 {
2861 // This test doesn't actually include any shared dependencies but it's a reminder of what
2862 // the second phase of the above test would look like
2863 inStatic: "a:b,c; c:b",
2864 allOrdered: "a:c,b; c:b",
2865 outOrdered: "a:c,b; c:b",
2866 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002867 // tiebreakers for when two modules specifying different orderings and there is no dependency
2868 // to dictate an order
2869 {
2870 // 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 -08002871 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002872 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2873 },
2874 {
2875 // 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 -08002876 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 -07002877 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2878 },
2879 // Tests involving duplicate dependencies
2880 {
2881 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002882 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002883 outOrdered: "a:c,b",
2884 },
2885 {
2886 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002887 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002888 outOrdered: "a:d,c,b",
2889 },
2890 // Tests to confirm the nonexistence of infinite loops.
2891 // These cases should never happen, so as long as the test terminates and the
2892 // result is deterministic then that should be fine.
2893 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002894 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002895 outOrdered: "a:a",
2896 },
2897 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002898 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002899 allOrdered: "a:b,c; b:c,a; c:a,b",
2900 outOrdered: "a:b; b:c; c:a",
2901 },
2902 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002903 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002904 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2905 outOrdered: "a:c,b; b:a,c; c:b,a",
2906 },
2907}
2908
2909// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2910func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2911 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2912 strippedText := strings.Replace(text, " ", "", -1)
2913 if len(strippedText) < 1 {
2914 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2915 }
2916 allDeps = make(map[android.Path][]android.Path, 0)
2917
2918 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2919 moduleTexts := strings.Split(strippedText, ";")
2920
2921 outputForModuleName := func(moduleName string) android.Path {
2922 return android.PathForTesting(moduleName)
2923 }
2924
2925 for _, moduleText := range moduleTexts {
2926 // convert from "a:b,c" to ["a", "b,c"]
2927 components := strings.Split(moduleText, ":")
2928 if len(components) != 2 {
2929 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2930 }
2931 moduleName := components[0]
2932 moduleOutput := outputForModuleName(moduleName)
2933 modulesInOrder = append(modulesInOrder, moduleOutput)
2934
2935 depString := components[1]
2936 // convert from "b,c" to ["b", "c"]
2937 depNames := strings.Split(depString, ",")
2938 if len(depString) < 1 {
2939 depNames = []string{}
2940 }
2941 var deps []android.Path
2942 for _, depName := range depNames {
2943 deps = append(deps, outputForModuleName(depName))
2944 }
2945 allDeps[moduleOutput] = deps
2946 }
2947 return modulesInOrder, allDeps
2948}
2949
Jeff Gaston294356f2017-09-27 17:05:30 -07002950func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2951 for _, moduleName := range moduleNames {
2952 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
2953 output := module.outputFile.Path()
2954 paths = append(paths, output)
2955 }
2956 return paths
2957}
2958
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002959func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002960 ctx := testCc(t, `
2961 cc_library {
2962 name: "a",
2963 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002964 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002965 }
2966 cc_library {
2967 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002968 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002969 }
2970 cc_library {
2971 name: "c",
2972 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002973 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002974 }
2975 cc_library {
2976 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002977 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002978 }
2979
2980 `)
2981
Colin Cross7113d202019-11-20 16:39:12 -08002982 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002983 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07002984 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
2985 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002986
2987 if !reflect.DeepEqual(actual, expected) {
2988 t.Errorf("staticDeps orderings were not propagated correctly"+
2989 "\nactual: %v"+
2990 "\nexpected: %v",
2991 actual,
2992 expected,
2993 )
2994 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002995}
Jeff Gaston294356f2017-09-27 17:05:30 -07002996
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002997func TestStaticLibDepReorderingWithShared(t *testing.T) {
2998 ctx := testCc(t, `
2999 cc_library {
3000 name: "a",
3001 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09003002 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003003 }
3004 cc_library {
3005 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003006 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003007 }
3008 cc_library {
3009 name: "c",
3010 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003011 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003012 }
3013
3014 `)
3015
Colin Cross7113d202019-11-20 16:39:12 -08003016 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003017 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003018 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3019 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003020
3021 if !reflect.DeepEqual(actual, expected) {
3022 t.Errorf("staticDeps orderings did not account for shared libs"+
3023 "\nactual: %v"+
3024 "\nexpected: %v",
3025 actual,
3026 expected,
3027 )
3028 }
3029}
3030
Jooyung Hanb04a4992020-03-13 18:57:35 +09003031func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07003032 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003033 if !reflect.DeepEqual(actual, expected) {
3034 t.Errorf(message+
3035 "\nactual: %v"+
3036 "\nexpected: %v",
3037 actual,
3038 expected,
3039 )
3040 }
3041}
3042
Jooyung Han61b66e92020-03-21 14:21:46 +00003043func TestLlndkLibrary(t *testing.T) {
3044 ctx := testCc(t, `
3045 cc_library {
3046 name: "libllndk",
3047 stubs: { versions: ["1", "2"] },
3048 }
3049 llndk_library {
3050 name: "libllndk",
3051 }
3052 `)
3053 actual := ctx.ModuleVariantsForTests("libllndk.llndk")
3054 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00003055 "android_vendor.VER_arm64_armv8-a_shared_1",
3056 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003057 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003058 "android_vendor.VER_arm_armv7-a-neon_shared_1",
3059 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003060 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003061 }
3062 checkEquals(t, "variants for llndk stubs", expected, actual)
3063
3064 params := ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
3065 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
3066
3067 params = ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
3068 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
3069}
3070
Jiyong Parka46a4d52017-12-14 19:54:34 +09003071func TestLlndkHeaders(t *testing.T) {
3072 ctx := testCc(t, `
3073 llndk_headers {
3074 name: "libllndk_headers",
3075 export_include_dirs: ["my_include"],
3076 }
3077 llndk_library {
3078 name: "libllndk",
3079 export_llndk_headers: ["libllndk_headers"],
3080 }
3081 cc_library {
3082 name: "libvendor",
3083 shared_libs: ["libllndk"],
3084 vendor: true,
3085 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003086 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08003087 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09003088 }
3089 `)
3090
3091 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08003092 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09003093 cflags := cc.Args["cFlags"]
3094 if !strings.Contains(cflags, "-Imy_include") {
3095 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
3096 }
3097}
3098
Logan Chien43d34c32017-12-20 01:17:32 +08003099func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
3100 actual := module.Properties.AndroidMkRuntimeLibs
3101 if !reflect.DeepEqual(actual, expected) {
3102 t.Errorf("incorrect runtime_libs for shared libs"+
3103 "\nactual: %v"+
3104 "\nexpected: %v",
3105 actual,
3106 expected,
3107 )
3108 }
3109}
3110
3111const runtimeLibAndroidBp = `
3112 cc_library {
3113 name: "libvendor_available1",
3114 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003115 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003116 nocrt : true,
3117 system_shared_libs : [],
3118 }
3119 cc_library {
3120 name: "libvendor_available2",
3121 vendor_available: true,
3122 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003123 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003124 nocrt : true,
3125 system_shared_libs : [],
3126 }
3127 cc_library {
3128 name: "libvendor_available3",
3129 vendor_available: true,
3130 runtime_libs: ["libvendor_available1"],
3131 target: {
3132 vendor: {
3133 exclude_runtime_libs: ["libvendor_available1"],
3134 }
3135 },
Yi Konge7fe9912019-06-02 00:53:50 -07003136 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003137 nocrt : true,
3138 system_shared_libs : [],
3139 }
3140 cc_library {
3141 name: "libcore",
3142 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003143 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003144 nocrt : true,
3145 system_shared_libs : [],
3146 }
3147 cc_library {
3148 name: "libvendor1",
3149 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003150 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003151 nocrt : true,
3152 system_shared_libs : [],
3153 }
3154 cc_library {
3155 name: "libvendor2",
3156 vendor: true,
3157 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003158 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003159 nocrt : true,
3160 system_shared_libs : [],
3161 }
3162`
3163
3164func TestRuntimeLibs(t *testing.T) {
3165 ctx := testCc(t, runtimeLibAndroidBp)
3166
3167 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08003168 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003169
3170 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3171 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3172
3173 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
3174 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3175
3176 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3177 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08003178 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003179
3180 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3181 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
3182
3183 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
3184 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
3185}
3186
3187func TestExcludeRuntimeLibs(t *testing.T) {
3188 ctx := testCc(t, runtimeLibAndroidBp)
3189
Colin Cross7113d202019-11-20 16:39:12 -08003190 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003191 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
3192 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3193
Colin Crossfb0c16e2019-11-20 17:12:35 -08003194 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003195 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
3196 checkRuntimeLibs(t, nil, module)
3197}
3198
3199func TestRuntimeLibsNoVndk(t *testing.T) {
3200 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3201
3202 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3203
Colin Cross7113d202019-11-20 16:39:12 -08003204 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003205
3206 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3207 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3208
3209 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
3210 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
3211}
3212
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003213func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003214 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003215 actual := module.Properties.AndroidMkStaticLibs
3216 if !reflect.DeepEqual(actual, expected) {
3217 t.Errorf("incorrect static_libs"+
3218 "\nactual: %v"+
3219 "\nexpected: %v",
3220 actual,
3221 expected,
3222 )
3223 }
3224}
3225
3226const staticLibAndroidBp = `
3227 cc_library {
3228 name: "lib1",
3229 }
3230 cc_library {
3231 name: "lib2",
3232 static_libs: ["lib1"],
3233 }
3234`
3235
3236func TestStaticLibDepExport(t *testing.T) {
3237 ctx := testCc(t, staticLibAndroidBp)
3238
3239 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003240 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003241 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003242 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003243
3244 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003245 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003246 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3247 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003248 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003249}
3250
Jiyong Parkd08b6972017-09-26 10:50:54 +09003251var compilerFlagsTestCases = []struct {
3252 in string
3253 out bool
3254}{
3255 {
3256 in: "a",
3257 out: false,
3258 },
3259 {
3260 in: "-a",
3261 out: true,
3262 },
3263 {
3264 in: "-Ipath/to/something",
3265 out: false,
3266 },
3267 {
3268 in: "-isystempath/to/something",
3269 out: false,
3270 },
3271 {
3272 in: "--coverage",
3273 out: false,
3274 },
3275 {
3276 in: "-include a/b",
3277 out: true,
3278 },
3279 {
3280 in: "-include a/b c/d",
3281 out: false,
3282 },
3283 {
3284 in: "-DMACRO",
3285 out: true,
3286 },
3287 {
3288 in: "-DMAC RO",
3289 out: false,
3290 },
3291 {
3292 in: "-a -b",
3293 out: false,
3294 },
3295 {
3296 in: "-DMACRO=definition",
3297 out: true,
3298 },
3299 {
3300 in: "-DMACRO=defi nition",
3301 out: true, // TODO(jiyong): this should be false
3302 },
3303 {
3304 in: "-DMACRO(x)=x + 1",
3305 out: true,
3306 },
3307 {
3308 in: "-DMACRO=\"defi nition\"",
3309 out: true,
3310 },
3311}
3312
3313type mockContext struct {
3314 BaseModuleContext
3315 result bool
3316}
3317
3318func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3319 // CheckBadCompilerFlags calls this function when the flag should be rejected
3320 ctx.result = false
3321}
3322
3323func TestCompilerFlags(t *testing.T) {
3324 for _, testCase := range compilerFlagsTestCases {
3325 ctx := &mockContext{result: true}
3326 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3327 if ctx.result != testCase.out {
3328 t.Errorf("incorrect output:")
3329 t.Errorf(" input: %#v", testCase.in)
3330 t.Errorf(" expected: %#v", testCase.out)
3331 t.Errorf(" got: %#v", ctx.result)
3332 }
3333 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003334}
Jiyong Park374510b2018-03-19 18:23:01 +09003335
3336func TestVendorPublicLibraries(t *testing.T) {
3337 ctx := testCc(t, `
3338 cc_library_headers {
3339 name: "libvendorpublic_headers",
3340 export_include_dirs: ["my_include"],
3341 }
3342 vendor_public_library {
3343 name: "libvendorpublic",
3344 symbol_file: "",
3345 export_public_headers: ["libvendorpublic_headers"],
3346 }
3347 cc_library {
3348 name: "libvendorpublic",
3349 srcs: ["foo.c"],
3350 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003351 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003352 nocrt: true,
3353 }
3354
3355 cc_library {
3356 name: "libsystem",
3357 shared_libs: ["libvendorpublic"],
3358 vendor: false,
3359 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003360 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003361 nocrt: true,
3362 }
3363 cc_library {
3364 name: "libvendor",
3365 shared_libs: ["libvendorpublic"],
3366 vendor: true,
3367 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003368 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003369 nocrt: true,
3370 }
3371 `)
3372
Colin Cross7113d202019-11-20 16:39:12 -08003373 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003374 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003375
3376 // test if header search paths are correctly added
3377 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003378 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003379 cflags := cc.Args["cFlags"]
3380 if !strings.Contains(cflags, "-Imy_include") {
3381 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3382 }
3383
3384 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003385 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003386 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003387 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003388 if !strings.Contains(libflags, stubPaths[0].String()) {
3389 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3390 }
3391
3392 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003393 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003394 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003395 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003396 if !strings.Contains(libflags, stubPaths[0].String()) {
3397 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3398 }
3399
3400}
Jiyong Park37b25202018-07-11 10:49:27 +09003401
3402func TestRecovery(t *testing.T) {
3403 ctx := testCc(t, `
3404 cc_library_shared {
3405 name: "librecovery",
3406 recovery: true,
3407 }
3408 cc_library_shared {
3409 name: "librecovery32",
3410 recovery: true,
3411 compile_multilib:"32",
3412 }
Jiyong Park5baac542018-08-28 09:55:37 +09003413 cc_library_shared {
3414 name: "libHalInRecovery",
3415 recovery_available: true,
3416 vendor: true,
3417 }
Jiyong Park37b25202018-07-11 10:49:27 +09003418 `)
3419
3420 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003421 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003422 if len(variants) != 1 || !android.InList(arm64, variants) {
3423 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3424 }
3425
3426 variants = ctx.ModuleVariantsForTests("librecovery32")
3427 if android.InList(arm64, variants) {
3428 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3429 }
Jiyong Park5baac542018-08-28 09:55:37 +09003430
3431 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3432 if !recoveryModule.Platform() {
3433 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3434 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003435}
Jiyong Park5baac542018-08-28 09:55:37 +09003436
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003437func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3438 bp := `
3439 cc_prebuilt_test_library_shared {
3440 name: "test_lib",
3441 relative_install_path: "foo/bar/baz",
3442 srcs: ["srcpath/dontusethispath/baz.so"],
3443 }
3444
3445 cc_test {
3446 name: "main_test",
3447 data_libs: ["test_lib"],
3448 gtest: false,
3449 }
3450 `
3451
3452 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3453 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3454 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3455 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3456
3457 ctx := testCcWithConfig(t, config)
3458 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3459 testBinary := module.(*Module).linker.(*testBinary)
3460 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3461 if err != nil {
3462 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3463 }
3464 if len(outputFiles) != 1 {
3465 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3466 }
3467 if len(testBinary.dataPaths()) != 1 {
3468 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3469 }
3470
3471 outputPath := outputFiles[0].String()
3472
3473 if !strings.HasSuffix(outputPath, "/main_test") {
3474 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3475 }
3476 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
3477 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3478 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3479 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3480 }
3481}
3482
Jiyong Park7ed9de32018-10-15 22:25:07 +09003483func TestVersionedStubs(t *testing.T) {
3484 ctx := testCc(t, `
3485 cc_library_shared {
3486 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003487 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003488 stubs: {
3489 symbol_file: "foo.map.txt",
3490 versions: ["1", "2", "3"],
3491 },
3492 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003493
Jiyong Park7ed9de32018-10-15 22:25:07 +09003494 cc_library_shared {
3495 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003496 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003497 shared_libs: ["libFoo#1"],
3498 }`)
3499
3500 variants := ctx.ModuleVariantsForTests("libFoo")
3501 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003502 "android_arm64_armv8-a_shared",
3503 "android_arm64_armv8-a_shared_1",
3504 "android_arm64_armv8-a_shared_2",
3505 "android_arm64_armv8-a_shared_3",
3506 "android_arm_armv7-a-neon_shared",
3507 "android_arm_armv7-a-neon_shared_1",
3508 "android_arm_armv7-a-neon_shared_2",
3509 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003510 }
3511 variantsMismatch := false
3512 if len(variants) != len(expectedVariants) {
3513 variantsMismatch = true
3514 } else {
3515 for _, v := range expectedVariants {
3516 if !inList(v, variants) {
3517 variantsMismatch = false
3518 }
3519 }
3520 }
3521 if variantsMismatch {
3522 t.Errorf("variants of libFoo expected:\n")
3523 for _, v := range expectedVariants {
3524 t.Errorf("%q\n", v)
3525 }
3526 t.Errorf(", but got:\n")
3527 for _, v := range variants {
3528 t.Errorf("%q\n", v)
3529 }
3530 }
3531
Colin Cross7113d202019-11-20 16:39:12 -08003532 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003533 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003534 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003535 if !strings.Contains(libFlags, libFoo1StubPath) {
3536 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3537 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003538
Colin Cross7113d202019-11-20 16:39:12 -08003539 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003540 cFlags := libBarCompileRule.Args["cFlags"]
3541 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3542 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3543 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3544 }
Jiyong Park37b25202018-07-11 10:49:27 +09003545}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003546
Jooyung Hanb04a4992020-03-13 18:57:35 +09003547func TestVersioningMacro(t *testing.T) {
3548 for _, tc := range []struct{ moduleName, expected string }{
3549 {"libc", "__LIBC_API__"},
3550 {"libfoo", "__LIBFOO_API__"},
3551 {"libfoo@1", "__LIBFOO_1_API__"},
3552 {"libfoo-v1", "__LIBFOO_V1_API__"},
3553 {"libfoo.v1", "__LIBFOO_V1_API__"},
3554 } {
3555 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3556 }
3557}
3558
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003559func TestStaticExecutable(t *testing.T) {
3560 ctx := testCc(t, `
3561 cc_binary {
3562 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003563 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003564 static_executable: true,
3565 }`)
3566
Colin Cross7113d202019-11-20 16:39:12 -08003567 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003568 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3569 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003570 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003571 for _, lib := range systemStaticLibs {
3572 if !strings.Contains(libFlags, lib) {
3573 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3574 }
3575 }
3576 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3577 for _, lib := range systemSharedLibs {
3578 if strings.Contains(libFlags, lib) {
3579 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3580 }
3581 }
3582}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003583
3584func TestStaticDepsOrderWithStubs(t *testing.T) {
3585 ctx := testCc(t, `
3586 cc_binary {
3587 name: "mybin",
3588 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003589 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003590 static_executable: true,
3591 stl: "none",
3592 }
3593
3594 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003595 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003596 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003597 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003598 stl: "none",
3599 }
3600
3601 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003602 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003603 srcs: ["foo.c"],
3604 stl: "none",
3605 stubs: {
3606 versions: ["1"],
3607 },
3608 }`)
3609
Colin Cross0de8a1e2020-09-18 14:15:30 -07003610 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3611 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003612 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003613
3614 if !reflect.DeepEqual(actual, expected) {
3615 t.Errorf("staticDeps orderings were not propagated correctly"+
3616 "\nactual: %v"+
3617 "\nexpected: %v",
3618 actual,
3619 expected,
3620 )
3621 }
3622}
Jooyung Han38002912019-05-16 04:01:54 +09003623
Jooyung Hand48f3c32019-08-23 11:18:57 +09003624func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3625 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3626 cc_library {
3627 name: "libA",
3628 srcs: ["foo.c"],
3629 shared_libs: ["libB"],
3630 stl: "none",
3631 }
3632
3633 cc_library {
3634 name: "libB",
3635 srcs: ["foo.c"],
3636 enabled: false,
3637 stl: "none",
3638 }
3639 `)
3640}
3641
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003642// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3643// correctly.
3644func TestFuzzTarget(t *testing.T) {
3645 ctx := testCc(t, `
3646 cc_fuzz {
3647 name: "fuzz_smoke_test",
3648 srcs: ["foo.c"],
3649 }`)
3650
Paul Duffin075c4172019-12-19 19:06:13 +00003651 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003652 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3653}
3654
Jiyong Park29074592019-07-07 16:27:47 +09003655func TestAidl(t *testing.T) {
3656}
3657
Jooyung Han38002912019-05-16 04:01:54 +09003658func assertString(t *testing.T, got, expected string) {
3659 t.Helper()
3660 if got != expected {
3661 t.Errorf("expected %q got %q", expected, got)
3662 }
3663}
3664
3665func assertArrayString(t *testing.T, got, expected []string) {
3666 t.Helper()
3667 if len(got) != len(expected) {
3668 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3669 return
3670 }
3671 for i := range got {
3672 if got[i] != expected[i] {
3673 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3674 i, expected[i], expected, got[i], got)
3675 return
3676 }
3677 }
3678}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003679
Jooyung Han0302a842019-10-30 18:43:49 +09003680func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3681 t.Helper()
3682 assertArrayString(t, android.SortedStringKeys(m), expected)
3683}
3684
Colin Crosse1bb5d02019-09-24 14:55:04 -07003685func TestDefaults(t *testing.T) {
3686 ctx := testCc(t, `
3687 cc_defaults {
3688 name: "defaults",
3689 srcs: ["foo.c"],
3690 static: {
3691 srcs: ["bar.c"],
3692 },
3693 shared: {
3694 srcs: ["baz.c"],
3695 },
3696 }
3697
3698 cc_library_static {
3699 name: "libstatic",
3700 defaults: ["defaults"],
3701 }
3702
3703 cc_library_shared {
3704 name: "libshared",
3705 defaults: ["defaults"],
3706 }
3707
3708 cc_library {
3709 name: "libboth",
3710 defaults: ["defaults"],
3711 }
3712
3713 cc_binary {
3714 name: "binary",
3715 defaults: ["defaults"],
3716 }`)
3717
3718 pathsToBase := func(paths android.Paths) []string {
3719 var ret []string
3720 for _, p := range paths {
3721 ret = append(ret, p.Base())
3722 }
3723 return ret
3724 }
3725
Colin Cross7113d202019-11-20 16:39:12 -08003726 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003727 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3728 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3729 }
Colin Cross7113d202019-11-20 16:39:12 -08003730 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003731 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3732 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3733 }
Colin Cross7113d202019-11-20 16:39:12 -08003734 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003735 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3736 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3737 }
3738
Colin Cross7113d202019-11-20 16:39:12 -08003739 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003740 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3741 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3742 }
Colin Cross7113d202019-11-20 16:39:12 -08003743 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003744 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3745 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3746 }
3747}
Colin Crosseabaedd2020-02-06 17:01:55 -08003748
3749func TestProductVariableDefaults(t *testing.T) {
3750 bp := `
3751 cc_defaults {
3752 name: "libfoo_defaults",
3753 srcs: ["foo.c"],
3754 cppflags: ["-DFOO"],
3755 product_variables: {
3756 debuggable: {
3757 cppflags: ["-DBAR"],
3758 },
3759 },
3760 }
3761
3762 cc_library {
3763 name: "libfoo",
3764 defaults: ["libfoo_defaults"],
3765 }
3766 `
3767
3768 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3769 config.TestProductVariables.Debuggable = BoolPtr(true)
3770
3771 ctx := CreateTestContext()
3772 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
3773 ctx.BottomUp("variable", android.VariableMutator).Parallel()
3774 })
3775 ctx.Register(config)
3776
3777 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3778 android.FailIfErrored(t, errs)
3779 _, errs = ctx.PrepareBuildActions(config)
3780 android.FailIfErrored(t, errs)
3781
3782 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
3783 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
3784 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
3785 }
3786}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003787
3788func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3789 t.Parallel()
3790 bp := `
3791 cc_library_static {
3792 name: "libfoo",
3793 srcs: ["foo.c"],
3794 whole_static_libs: ["libbar"],
3795 }
3796
3797 cc_library_static {
3798 name: "libbar",
3799 whole_static_libs: ["libmissing"],
3800 }
3801 `
3802
3803 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3804 config.TestProductVariables.Allow_missing_dependencies = BoolPtr(true)
3805
3806 ctx := CreateTestContext()
3807 ctx.SetAllowMissingDependencies(true)
3808 ctx.Register(config)
3809
3810 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3811 android.FailIfErrored(t, errs)
3812 _, errs = ctx.PrepareBuildActions(config)
3813 android.FailIfErrored(t, errs)
3814
3815 libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
3816 if g, w := libbar.Rule, android.ErrorRule; g != w {
3817 t.Fatalf("Expected libbar rule to be %q, got %q", w, g)
3818 }
3819
3820 if g, w := libbar.Args["error"], "missing dependencies: libmissing"; !strings.Contains(g, w) {
3821 t.Errorf("Expected libbar error to contain %q, was %q", w, g)
3822 }
3823
3824 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
3825 if g, w := libfoo.Inputs.Strings(), libbar.Output.String(); !android.InList(w, g) {
3826 t.Errorf("Expected libfoo.a to depend on %q, got %q", w, g)
3827 }
3828
3829}