blob: fef0c3412d45c03620618c0ed014f99fda87f7eb [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) {
106 config := TestConfig(buildDir, android.Android, nil, bp, nil)
107 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
108 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
109 testCcErrorWithConfig(t, pattern, config)
110 return
111}
112
113func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
114 config := TestConfig(buildDir, android.Android, nil, bp, nil)
115 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
116 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
117 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
118 testCcErrorWithConfig(t, pattern, config)
119 return
120}
121
Logan Chienf3511742017-10-31 18:04:35 +0800122const (
Colin Cross7113d202019-11-20 16:39:12 -0800123 coreVariant = "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800124 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun5f7f7e82019-11-18 19:52:14 +0900125 productVariant = "android_product.VER_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800126 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800127)
128
Doug Hornc32c6b02019-01-17 14:44:05 -0800129func TestFuchsiaDeps(t *testing.T) {
130 t.Helper()
131
132 bp := `
133 cc_library {
134 name: "libTest",
135 srcs: ["foo.c"],
136 target: {
137 fuchsia: {
138 srcs: ["bar.c"],
139 },
140 },
141 }`
142
Colin Cross98be1bb2019-12-13 20:41:13 -0800143 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
144 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800145
146 rt := false
147 fb := false
148
149 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
150 implicits := ld.Implicits
151 for _, lib := range implicits {
152 if strings.Contains(lib.Rel(), "libcompiler_rt") {
153 rt = true
154 }
155
156 if strings.Contains(lib.Rel(), "libbioniccompat") {
157 fb = true
158 }
159 }
160
161 if !rt || !fb {
162 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
163 }
164}
165
166func TestFuchsiaTargetDecl(t *testing.T) {
167 t.Helper()
168
169 bp := `
170 cc_library {
171 name: "libTest",
172 srcs: ["foo.c"],
173 target: {
174 fuchsia: {
175 srcs: ["bar.c"],
176 },
177 },
178 }`
179
Colin Cross98be1bb2019-12-13 20:41:13 -0800180 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
181 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800182 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
183 var objs []string
184 for _, o := range ld.Inputs {
185 objs = append(objs, o.Base())
186 }
187 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
188 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
189 }
190}
191
Jiyong Park6a43f042017-10-12 23:05:00 +0900192func TestVendorSrc(t *testing.T) {
193 ctx := testCc(t, `
194 cc_library {
195 name: "libTest",
196 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700197 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800198 nocrt: true,
199 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900200 vendor_available: true,
201 target: {
202 vendor: {
203 srcs: ["bar.c"],
204 },
205 },
206 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900207 `)
208
Logan Chienf3511742017-10-31 18:04:35 +0800209 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900210 var objs []string
211 for _, o := range ld.Inputs {
212 objs = append(objs, o.Base())
213 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800214 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900215 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
216 }
217}
218
Logan Chienf3511742017-10-31 18:04:35 +0800219func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900220 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800221
Logan Chiend3c59a22018-03-29 14:08:15 +0800222 t.Helper()
223
Justin Yun0ecf0b22020-02-28 15:07:59 +0900224 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Ivan Lozano52767be2019-10-18 14:49:46 -0700225 if !mod.HasVendorVariant() {
Justin Yun0ecf0b22020-02-28 15:07:59 +0900226 t.Errorf("%q must have variant %q", name, variant)
Logan Chienf3511742017-10-31 18:04:35 +0800227 }
228
229 // Check library properties.
230 lib, ok := mod.compiler.(*libraryDecorator)
231 if !ok {
232 t.Errorf("%q must have libraryDecorator", name)
233 } else if lib.baseInstaller.subDir != subDir {
234 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
235 lib.baseInstaller.subDir)
236 }
237
238 // Check VNDK properties.
239 if mod.vndkdep == nil {
240 t.Fatalf("%q must have `vndkdep`", name)
241 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700242 if !mod.IsVndk() {
243 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800244 }
245 if mod.isVndkSp() != isVndkSp {
246 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
247 }
248
249 // Check VNDK extension properties.
250 isVndkExt := extends != ""
251 if mod.isVndkExt() != isVndkExt {
252 t.Errorf("%q isVndkExt() must equal to %t", name, isVndkExt)
253 }
254
255 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
256 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
257 }
258}
259
Bill Peckham945441c2020-08-31 16:07:58 -0700260func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool) {
261 t.Helper()
Jooyung Han39edb6c2019-11-06 16:53:07 +0900262 mod, ok := ctx.ModuleForTests(moduleName, variant).Module().(android.OutputFileProducer)
263 if !ok {
264 t.Errorf("%q must have output\n", moduleName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900265 return
266 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900267 outputFiles, err := mod.OutputFiles("")
268 if err != nil || len(outputFiles) != 1 {
269 t.Errorf("%q must have single output\n", moduleName)
270 return
271 }
272 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900273
Bill Peckham945441c2020-08-31 16:07:58 -0700274 if include {
275 out := singleton.Output(snapshotPath)
276 if out.Input.String() != outputFiles[0].String() {
277 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
278 }
279 } else {
280 out := singleton.MaybeOutput(snapshotPath)
281 if out.Rule != nil {
282 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
283 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900284 }
285}
286
Bill Peckham945441c2020-08-31 16:07:58 -0700287func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
288 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true)
289}
290
291func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
292 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false)
293}
294
Jooyung Han2216fb12019-11-06 16:46:15 +0900295func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
296 t.Helper()
297 assertString(t, params.Rule.String(), android.WriteFile.String())
298 actual := strings.FieldsFunc(strings.ReplaceAll(params.Args["content"], "\\n", "\n"), func(r rune) bool { return r == '\n' })
299 assertArrayString(t, actual, expected)
300}
301
Jooyung Han097087b2019-10-22 19:32:18 +0900302func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
303 t.Helper()
304 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900305 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
306}
307
308func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
309 t.Helper()
310 vndkLibraries := ctx.ModuleForTests(module, "")
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +0900311
312 var output string
313 if module != "vndkcorevariant.libraries.txt" {
314 output = insertVndkVersion(module, "VER")
315 } else {
316 output = module
317 }
318
Jooyung Han2216fb12019-11-06 16:46:15 +0900319 checkWriteFileOutput(t, vndkLibraries.Output(output), expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900320}
321
Logan Chienf3511742017-10-31 18:04:35 +0800322func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800323 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800324 cc_library {
325 name: "libvndk",
326 vendor_available: true,
327 vndk: {
328 enabled: true,
329 },
330 nocrt: true,
331 }
332
333 cc_library {
334 name: "libvndk_private",
335 vendor_available: false,
336 vndk: {
337 enabled: true,
338 },
339 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900340 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800341 }
342
343 cc_library {
344 name: "libvndk_sp",
345 vendor_available: true,
346 vndk: {
347 enabled: true,
348 support_system_process: true,
349 },
350 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900351 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800352 }
353
354 cc_library {
355 name: "libvndk_sp_private",
356 vendor_available: false,
357 vndk: {
358 enabled: true,
359 support_system_process: true,
360 },
361 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900362 target: {
363 vendor: {
364 suffix: "-x",
365 },
366 },
Logan Chienf3511742017-10-31 18:04:35 +0800367 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900368 vndk_libraries_txt {
369 name: "llndk.libraries.txt",
370 }
371 vndk_libraries_txt {
372 name: "vndkcore.libraries.txt",
373 }
374 vndk_libraries_txt {
375 name: "vndksp.libraries.txt",
376 }
377 vndk_libraries_txt {
378 name: "vndkprivate.libraries.txt",
379 }
380 vndk_libraries_txt {
381 name: "vndkcorevariant.libraries.txt",
382 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800383 `
384
385 config := TestConfig(buildDir, android.Android, nil, bp, nil)
386 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
387 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
388
389 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800390
Justin Yun0ecf0b22020-02-28 15:07:59 +0900391 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "", vendorVariant)
392 checkVndkModule(t, ctx, "libvndk_private", "vndk-VER", false, "", vendorVariant)
393 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "", vendorVariant)
394 checkVndkModule(t, ctx, "libvndk_sp_private", "vndk-sp-VER", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900395
396 // Check VNDK snapshot output.
397
398 snapshotDir := "vndk-snapshot"
399 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
400
401 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
402 "arm64", "armv8-a"))
403 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
404 "arm", "armv7-a-neon"))
405
406 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
407 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
408 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
409 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
410
Colin Crossfb0c16e2019-11-20 17:12:35 -0800411 variant := "android_vendor.VER_arm64_armv8-a_shared"
412 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900413
Inseob Kim7f283f42020-06-01 21:53:49 +0900414 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
415
416 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
417 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
418 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
419 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900420
Jooyung Han39edb6c2019-11-06 16:53:07 +0900421 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900422 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
423 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
424 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
425 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900426
Jooyung Han097087b2019-10-22 19:32:18 +0900427 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
428 "LLNDK: libc.so",
429 "LLNDK: libdl.so",
430 "LLNDK: libft2.so",
431 "LLNDK: libm.so",
432 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900433 "VNDK-SP: libvndk_sp-x.so",
434 "VNDK-SP: libvndk_sp_private-x.so",
435 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900436 "VNDK-core: libvndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900437 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900438 "VNDK-private: libvndk-private.so",
439 "VNDK-private: libvndk_sp_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900440 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900441 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
442 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so"})
443 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so"})
444 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so"})
445 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
446}
447
Yo Chiangbba545e2020-06-09 16:15:37 +0800448func TestVndkWithHostSupported(t *testing.T) {
449 ctx := testCc(t, `
450 cc_library {
451 name: "libvndk_host_supported",
452 vendor_available: true,
453 vndk: {
454 enabled: true,
455 },
456 host_supported: true,
457 }
458
459 cc_library {
460 name: "libvndk_host_supported_but_disabled_on_device",
461 vendor_available: true,
462 vndk: {
463 enabled: true,
464 },
465 host_supported: true,
466 enabled: false,
467 target: {
468 host: {
469 enabled: true,
470 }
471 }
472 }
473
474 vndk_libraries_txt {
475 name: "vndkcore.libraries.txt",
476 }
477 `)
478
479 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
480}
481
Jooyung Han2216fb12019-11-06 16:46:15 +0900482func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800483 bp := `
Jooyung Han2216fb12019-11-06 16:46:15 +0900484 vndk_libraries_txt {
485 name: "llndk.libraries.txt",
Colin Cross98be1bb2019-12-13 20:41:13 -0800486 }`
487 config := TestConfig(buildDir, android.Android, nil, bp, nil)
488 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
489 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
490 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900491
492 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900493 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900494 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900495}
496
497func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800498 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900499 cc_library {
500 name: "libvndk",
501 vendor_available: true,
502 vndk: {
503 enabled: true,
504 },
505 nocrt: true,
506 }
507
508 cc_library {
509 name: "libvndk_sp",
510 vendor_available: true,
511 vndk: {
512 enabled: true,
513 support_system_process: true,
514 },
515 nocrt: true,
516 }
517
518 cc_library {
519 name: "libvndk2",
520 vendor_available: false,
521 vndk: {
522 enabled: true,
523 },
524 nocrt: true,
525 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900526
527 vndk_libraries_txt {
528 name: "vndkcorevariant.libraries.txt",
529 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800530 `
531
532 config := TestConfig(buildDir, android.Android, nil, bp, nil)
533 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
534 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
535 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
536
537 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
538
539 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900540
Jooyung Han2216fb12019-11-06 16:46:15 +0900541 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900542}
543
Chris Parsons79d66a52020-06-05 17:26:16 -0400544func TestDataLibs(t *testing.T) {
545 bp := `
546 cc_test_library {
547 name: "test_lib",
548 srcs: ["test_lib.cpp"],
549 gtest: false,
550 }
551
552 cc_test {
553 name: "main_test",
554 data_libs: ["test_lib"],
555 gtest: false,
556 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400557 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400558
559 config := TestConfig(buildDir, android.Android, nil, bp, nil)
560 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
561 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
562 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
563
564 ctx := testCcWithConfig(t, config)
565 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
566 testBinary := module.(*Module).linker.(*testBinary)
567 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
568 if err != nil {
569 t.Errorf("Expected cc_test to produce output files, error: %s", err)
570 return
571 }
572 if len(outputFiles) != 1 {
573 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
574 return
575 }
576 if len(testBinary.dataPaths()) != 1 {
577 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
578 return
579 }
580
581 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400582 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400583
584 if !strings.HasSuffix(outputPath, "/main_test") {
585 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
586 return
587 }
588 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
589 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
590 return
591 }
592}
593
Chris Parsons216e10a2020-07-09 17:12:52 -0400594func TestDataLibsRelativeInstallPath(t *testing.T) {
595 bp := `
596 cc_test_library {
597 name: "test_lib",
598 srcs: ["test_lib.cpp"],
599 relative_install_path: "foo/bar/baz",
600 gtest: false,
601 }
602
603 cc_test {
604 name: "main_test",
605 data_libs: ["test_lib"],
606 gtest: false,
607 }
608 `
609
610 config := TestConfig(buildDir, android.Android, nil, bp, nil)
611 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
612 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
613 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
614
615 ctx := testCcWithConfig(t, config)
616 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
617 testBinary := module.(*Module).linker.(*testBinary)
618 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
619 if err != nil {
620 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
621 }
622 if len(outputFiles) != 1 {
623 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
624 }
625 if len(testBinary.dataPaths()) != 1 {
626 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
627 }
628
629 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400630
631 if !strings.HasSuffix(outputPath, "/main_test") {
632 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
633 }
634 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
635 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
636 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400637 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400638 }
639}
640
Jooyung Han0302a842019-10-30 18:43:49 +0900641func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900642 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900643 cc_library {
644 name: "libvndk",
645 vendor_available: true,
646 vndk: {
647 enabled: true,
648 },
649 nocrt: true,
650 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900651 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900652
653 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
654 "LLNDK: libc.so",
655 "LLNDK: libdl.so",
656 "LLNDK: libft2.so",
657 "LLNDK: libm.so",
658 "VNDK-SP: libc++.so",
659 "VNDK-core: libvndk.so",
660 "VNDK-private: libft2.so",
661 })
Logan Chienf3511742017-10-31 18:04:35 +0800662}
663
Logan Chiend3c59a22018-03-29 14:08:15 +0800664func TestVndkDepError(t *testing.T) {
665 // Check whether an error is emitted when a VNDK lib depends on a system lib.
666 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
667 cc_library {
668 name: "libvndk",
669 vendor_available: true,
670 vndk: {
671 enabled: true,
672 },
673 shared_libs: ["libfwk"], // Cause error
674 nocrt: true,
675 }
676
677 cc_library {
678 name: "libfwk",
679 nocrt: true,
680 }
681 `)
682
683 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
684 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
685 cc_library {
686 name: "libvndk",
687 vendor_available: true,
688 vndk: {
689 enabled: true,
690 },
691 shared_libs: ["libvendor"], // Cause error
692 nocrt: true,
693 }
694
695 cc_library {
696 name: "libvendor",
697 vendor: true,
698 nocrt: true,
699 }
700 `)
701
702 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
703 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
704 cc_library {
705 name: "libvndk_sp",
706 vendor_available: true,
707 vndk: {
708 enabled: true,
709 support_system_process: true,
710 },
711 shared_libs: ["libfwk"], // Cause error
712 nocrt: true,
713 }
714
715 cc_library {
716 name: "libfwk",
717 nocrt: true,
718 }
719 `)
720
721 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
722 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
723 cc_library {
724 name: "libvndk_sp",
725 vendor_available: true,
726 vndk: {
727 enabled: true,
728 support_system_process: true,
729 },
730 shared_libs: ["libvendor"], // Cause error
731 nocrt: true,
732 }
733
734 cc_library {
735 name: "libvendor",
736 vendor: true,
737 nocrt: true,
738 }
739 `)
740
741 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
742 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
743 cc_library {
744 name: "libvndk_sp",
745 vendor_available: true,
746 vndk: {
747 enabled: true,
748 support_system_process: true,
749 },
750 shared_libs: ["libvndk"], // Cause error
751 nocrt: true,
752 }
753
754 cc_library {
755 name: "libvndk",
756 vendor_available: true,
757 vndk: {
758 enabled: true,
759 },
760 nocrt: true,
761 }
762 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900763
764 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
765 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
766 cc_library {
767 name: "libvndk",
768 vendor_available: true,
769 vndk: {
770 enabled: true,
771 },
772 shared_libs: ["libnonvndk"],
773 nocrt: true,
774 }
775
776 cc_library {
777 name: "libnonvndk",
778 vendor_available: true,
779 nocrt: true,
780 }
781 `)
782
783 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
784 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
785 cc_library {
786 name: "libvndkprivate",
787 vendor_available: false,
788 vndk: {
789 enabled: true,
790 },
791 shared_libs: ["libnonvndk"],
792 nocrt: true,
793 }
794
795 cc_library {
796 name: "libnonvndk",
797 vendor_available: true,
798 nocrt: true,
799 }
800 `)
801
802 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
803 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
804 cc_library {
805 name: "libvndksp",
806 vendor_available: true,
807 vndk: {
808 enabled: true,
809 support_system_process: true,
810 },
811 shared_libs: ["libnonvndk"],
812 nocrt: true,
813 }
814
815 cc_library {
816 name: "libnonvndk",
817 vendor_available: true,
818 nocrt: true,
819 }
820 `)
821
822 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
823 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
824 cc_library {
825 name: "libvndkspprivate",
826 vendor_available: false,
827 vndk: {
828 enabled: true,
829 support_system_process: true,
830 },
831 shared_libs: ["libnonvndk"],
832 nocrt: true,
833 }
834
835 cc_library {
836 name: "libnonvndk",
837 vendor_available: true,
838 nocrt: true,
839 }
840 `)
841}
842
843func TestDoubleLoadbleDep(t *testing.T) {
844 // okay to link : LLNDK -> double_loadable VNDK
845 testCc(t, `
846 cc_library {
847 name: "libllndk",
848 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -0700849 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900850 }
851
852 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -0700853 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900854 symbol_file: "",
855 }
856
857 cc_library {
858 name: "libdoubleloadable",
859 vendor_available: true,
860 vndk: {
861 enabled: true,
862 },
863 double_loadable: true,
864 }
865 `)
866 // okay to link : LLNDK -> VNDK-SP
867 testCc(t, `
868 cc_library {
869 name: "libllndk",
870 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -0700871 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900872 }
873
874 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -0700875 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900876 symbol_file: "",
877 }
878
879 cc_library {
880 name: "libvndksp",
881 vendor_available: true,
882 vndk: {
883 enabled: true,
884 support_system_process: true,
885 },
886 }
887 `)
888 // okay to link : double_loadable -> double_loadable
889 testCc(t, `
890 cc_library {
891 name: "libdoubleloadable1",
892 shared_libs: ["libdoubleloadable2"],
893 vendor_available: true,
894 double_loadable: true,
895 }
896
897 cc_library {
898 name: "libdoubleloadable2",
899 vendor_available: true,
900 double_loadable: true,
901 }
902 `)
903 // okay to link : double_loadable VNDK -> double_loadable VNDK private
904 testCc(t, `
905 cc_library {
906 name: "libdoubleloadable",
907 vendor_available: true,
908 vndk: {
909 enabled: true,
910 },
911 double_loadable: true,
912 shared_libs: ["libnondoubleloadable"],
913 }
914
915 cc_library {
916 name: "libnondoubleloadable",
917 vendor_available: false,
918 vndk: {
919 enabled: true,
920 },
921 double_loadable: true,
922 }
923 `)
924 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
925 testCc(t, `
926 cc_library {
927 name: "libllndk",
928 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -0700929 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900930 }
931
932 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -0700933 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900934 symbol_file: "",
935 }
936
937 cc_library {
938 name: "libcoreonly",
939 shared_libs: ["libvendoravailable"],
940 }
941
942 // indirect dependency of LLNDK
943 cc_library {
944 name: "libvendoravailable",
945 vendor_available: true,
946 double_loadable: true,
947 }
948 `)
949}
950
Inseob Kim5f58ff72020-09-07 19:53:31 +0900951func TestVendorSnapshotCapture(t *testing.T) {
Inseob Kim8471cda2019-11-15 09:59:12 +0900952 bp := `
953 cc_library {
954 name: "libvndk",
955 vendor_available: true,
956 vndk: {
957 enabled: true,
958 },
959 nocrt: true,
960 }
961
962 cc_library {
963 name: "libvendor",
964 vendor: true,
965 nocrt: true,
966 }
967
968 cc_library {
969 name: "libvendor_available",
970 vendor_available: true,
971 nocrt: true,
972 }
973
974 cc_library_headers {
975 name: "libvendor_headers",
976 vendor_available: true,
977 nocrt: true,
978 }
979
980 cc_binary {
981 name: "vendor_bin",
982 vendor: true,
983 nocrt: true,
984 }
985
986 cc_binary {
987 name: "vendor_available_bin",
988 vendor_available: true,
989 nocrt: true,
990 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900991
992 toolchain_library {
993 name: "libb",
994 vendor_available: true,
995 src: "libb.a",
996 }
Inseob Kim1042d292020-06-01 23:23:05 +0900997
998 cc_object {
999 name: "obj",
1000 vendor_available: true,
1001 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001002`
1003 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1004 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1005 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1006 ctx := testCcWithConfig(t, config)
1007
1008 // Check Vendor snapshot output.
1009
1010 snapshotDir := "vendor-snapshot"
1011 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +09001012 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1013
1014 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +09001015
1016 for _, arch := range [][]string{
1017 []string{"arm64", "armv8-a"},
1018 []string{"arm", "armv7-a-neon"},
1019 } {
1020 archType := arch[0]
1021 archVariant := arch[1]
1022 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1023
1024 // For shared libraries, only non-VNDK vendor_available modules are captured
1025 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1026 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +09001027 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1028 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
1029 jsonFiles = append(jsonFiles,
1030 filepath.Join(sharedDir, "libvendor.so.json"),
1031 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001032
1033 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
Inseob Kimc42f2f22020-07-29 20:32:10 +09001034 // Also cfi variants are captured, except for prebuilts like toolchain_library
Inseob Kim8471cda2019-11-15 09:59:12 +09001035 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001036 staticCfiVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static_cfi", archType, archVariant)
Inseob Kim8471cda2019-11-15 09:59:12 +09001037 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +09001038 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1039 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001040 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001041 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001042 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001043 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001044 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001045 jsonFiles = append(jsonFiles,
1046 filepath.Join(staticDir, "libb.a.json"),
1047 filepath.Join(staticDir, "libvndk.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001048 filepath.Join(staticDir, "libvndk.cfi.a.json"),
Inseob Kim7f283f42020-06-01 21:53:49 +09001049 filepath.Join(staticDir, "libvendor.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001050 filepath.Join(staticDir, "libvendor.cfi.a.json"),
1051 filepath.Join(staticDir, "libvendor_available.a.json"),
1052 filepath.Join(staticDir, "libvendor_available.cfi.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001053
Inseob Kim7f283f42020-06-01 21:53:49 +09001054 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +09001055 if archType == "arm64" {
1056 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1057 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +09001058 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
1059 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
1060 jsonFiles = append(jsonFiles,
1061 filepath.Join(binaryDir, "vendor_bin.json"),
1062 filepath.Join(binaryDir, "vendor_available_bin.json"))
1063 }
1064
1065 // For header libraries, all vendor:true and vendor_available modules are captured.
1066 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1067 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +09001068
1069 // For object modules, all vendor:true and vendor_available modules are captured.
1070 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1071 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1072 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1073 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001074 }
1075
1076 for _, jsonFile := range jsonFiles {
1077 // verify all json files exist
1078 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1079 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001080 }
1081 }
1082}
1083
Inseob Kim5f58ff72020-09-07 19:53:31 +09001084func TestVendorSnapshotUse(t *testing.T) {
1085 frameworkBp := `
1086 cc_library {
1087 name: "libvndk",
1088 vendor_available: true,
1089 vndk: {
1090 enabled: true,
1091 },
1092 nocrt: true,
1093 compile_multilib: "64",
1094 }
1095
1096 cc_library {
1097 name: "libvendor",
1098 vendor: true,
1099 nocrt: true,
1100 no_libcrt: true,
1101 stl: "none",
1102 system_shared_libs: [],
1103 compile_multilib: "64",
1104 }
1105
1106 cc_binary {
1107 name: "bin",
1108 vendor: true,
1109 nocrt: true,
1110 no_libcrt: true,
1111 stl: "none",
1112 system_shared_libs: [],
1113 compile_multilib: "64",
1114 }
1115`
1116
1117 vndkBp := `
1118 vndk_prebuilt_shared {
1119 name: "libvndk",
1120 version: "BOARD",
1121 target_arch: "arm64",
1122 vendor_available: true,
1123 vndk: {
1124 enabled: true,
1125 },
1126 arch: {
1127 arm64: {
1128 srcs: ["libvndk.so"],
1129 },
1130 },
1131 }
1132`
1133
1134 vendorProprietaryBp := `
1135 cc_library {
1136 name: "libvendor_without_snapshot",
1137 vendor: true,
1138 nocrt: true,
1139 no_libcrt: true,
1140 stl: "none",
1141 system_shared_libs: [],
1142 compile_multilib: "64",
1143 }
1144
1145 cc_library_shared {
1146 name: "libclient",
1147 vendor: true,
1148 nocrt: true,
1149 no_libcrt: true,
1150 stl: "none",
1151 system_shared_libs: [],
1152 shared_libs: ["libvndk"],
1153 static_libs: ["libvendor", "libvendor_without_snapshot"],
1154 compile_multilib: "64",
1155 }
1156
1157 cc_binary {
1158 name: "bin_without_snapshot",
1159 vendor: true,
1160 nocrt: true,
1161 no_libcrt: true,
1162 stl: "none",
1163 system_shared_libs: [],
1164 static_libs: ["libvndk"],
1165 compile_multilib: "64",
1166 }
1167
1168 vendor_snapshot_static {
1169 name: "libvndk",
1170 version: "BOARD",
1171 target_arch: "arm64",
1172 vendor: true,
1173 arch: {
1174 arm64: {
1175 src: "libvndk.a",
1176 },
1177 },
1178 }
1179
1180 vendor_snapshot_shared {
1181 name: "libvendor",
1182 version: "BOARD",
1183 target_arch: "arm64",
1184 vendor: true,
1185 arch: {
1186 arm64: {
1187 src: "libvendor.so",
1188 },
1189 },
1190 }
1191
1192 vendor_snapshot_static {
1193 name: "libvendor",
1194 version: "BOARD",
1195 target_arch: "arm64",
1196 vendor: true,
1197 arch: {
1198 arm64: {
1199 src: "libvendor.a",
1200 },
1201 },
1202 }
1203
1204 vendor_snapshot_binary {
1205 name: "bin",
1206 version: "BOARD",
1207 target_arch: "arm64",
1208 vendor: true,
1209 arch: {
1210 arm64: {
1211 src: "bin",
1212 },
1213 },
1214 }
1215`
1216 depsBp := GatherRequiredDepsForTest(android.Android)
1217
1218 mockFS := map[string][]byte{
1219 "deps/Android.bp": []byte(depsBp),
1220 "framework/Android.bp": []byte(frameworkBp),
1221 "vendor/Android.bp": []byte(vendorProprietaryBp),
1222 "vendor/libvndk.a": nil,
1223 "vendor/libvendor.a": nil,
1224 "vendor/libvendor.so": nil,
1225 "vendor/bin": nil,
1226 "vndk/Android.bp": []byte(vndkBp),
1227 "vndk/libvndk.so": nil,
1228 }
1229
1230 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1231 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1232 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1233 ctx := CreateTestContext()
1234 ctx.Register(config)
1235
1236 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "vendor/Android.bp", "vndk/Android.bp"})
1237 android.FailIfErrored(t, errs)
1238 _, errs = ctx.PrepareBuildActions(config)
1239 android.FailIfErrored(t, errs)
1240
1241 sharedVariant := "android_vendor.BOARD_arm64_armv8-a_shared"
1242 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1243 binaryVariant := "android_vendor.BOARD_arm64_armv8-a"
1244
1245 // libclient uses libvndk.vndk.BOARD.arm64, libvendor.vendor_static.BOARD.arm64, libvendor_without_snapshot
1246 libclientLdRule := ctx.ModuleForTests("libclient", sharedVariant).Rule("ld")
1247 libclientFlags := libclientLdRule.Args["libFlags"]
1248
1249 for _, input := range [][]string{
1250 []string{sharedVariant, "libvndk.vndk.BOARD.arm64"},
1251 []string{staticVariant, "libvendor.vendor_static.BOARD.arm64"},
1252 []string{staticVariant, "libvendor_without_snapshot"},
1253 } {
1254 outputPaths := getOutputPaths(ctx, input[0] /* variant */, []string{input[1]} /* module name */)
1255 if !strings.Contains(libclientFlags, outputPaths[0].String()) {
1256 t.Errorf("libflags for libclient must contain %#v, but was %#v", outputPaths[0], libclientFlags)
1257 }
1258 }
1259
1260 // bin_without_snapshot uses libvndk.vendor_static.BOARD.arm64
1261 binWithoutSnapshotLdRule := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("ld")
1262 binWithoutSnapshotFlags := binWithoutSnapshotLdRule.Args["libFlags"]
1263 libVndkStaticOutputPaths := getOutputPaths(ctx, staticVariant, []string{"libvndk.vendor_static.BOARD.arm64"})
1264 if !strings.Contains(binWithoutSnapshotFlags, libVndkStaticOutputPaths[0].String()) {
1265 t.Errorf("libflags for bin_without_snapshot must contain %#v, but was %#v",
1266 libVndkStaticOutputPaths[0], binWithoutSnapshotFlags)
1267 }
1268
1269 // libvendor.so is installed by libvendor.vendor_shared.BOARD.arm64
1270 ctx.ModuleForTests("libvendor.vendor_shared.BOARD.arm64", sharedVariant).Output("libvendor.so")
1271
1272 // libvendor_without_snapshot.so is installed by libvendor_without_snapshot
1273 ctx.ModuleForTests("libvendor_without_snapshot", sharedVariant).Output("libvendor_without_snapshot.so")
1274
1275 // bin is installed by bin.vendor_binary.BOARD.arm64
1276 ctx.ModuleForTests("bin.vendor_binary.BOARD.arm64", binaryVariant).Output("bin")
1277
1278 // bin_without_snapshot is installed by bin_without_snapshot
1279 ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Output("bin_without_snapshot")
1280
1281 // libvendor and bin don't have vendor.BOARD variant
1282 libvendorVariants := ctx.ModuleVariantsForTests("libvendor")
1283 if inList(sharedVariant, libvendorVariants) {
1284 t.Errorf("libvendor must not have variant %#v, but it does", sharedVariant)
1285 }
1286
1287 binVariants := ctx.ModuleVariantsForTests("bin")
1288 if inList(binaryVariant, binVariants) {
1289 t.Errorf("bin must not have variant %#v, but it does", sharedVariant)
1290 }
1291}
1292
Inseob Kimc42f2f22020-07-29 20:32:10 +09001293func TestVendorSnapshotSanitizer(t *testing.T) {
1294 bp := `
1295 vendor_snapshot_static {
1296 name: "libsnapshot",
1297 vendor: true,
1298 target_arch: "arm64",
1299 version: "BOARD",
1300 arch: {
1301 arm64: {
1302 src: "libsnapshot.a",
1303 cfi: {
1304 src: "libsnapshot.cfi.a",
1305 }
1306 },
1307 },
1308 }
1309`
1310 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1311 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1312 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1313 ctx := testCcWithConfig(t, config)
1314
1315 // Check non-cfi and cfi variant.
1316 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1317 staticCfiVariant := "android_vendor.BOARD_arm64_armv8-a_static_cfi"
1318
1319 staticModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticVariant).Module().(*Module)
1320 assertString(t, staticModule.outputFile.Path().Base(), "libsnapshot.a")
1321
1322 staticCfiModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticCfiVariant).Module().(*Module)
1323 assertString(t, staticCfiModule.outputFile.Path().Base(), "libsnapshot.cfi.a")
1324}
1325
Bill Peckham945441c2020-08-31 16:07:58 -07001326func assertExcludeFromVendorSnapshotIs(t *testing.T, c *Module, expected bool) {
1327 t.Helper()
1328 if c.ExcludeFromVendorSnapshot() != expected {
1329 t.Errorf("expected %q ExcludeFromVendorSnapshot to be %t", c.String(), expected)
1330 }
1331}
1332
1333func TestVendorSnapshotExclude(t *testing.T) {
1334
1335 // This test verifies that the exclude_from_vendor_snapshot property
1336 // makes its way from the Android.bp source file into the module data
1337 // structure. It also verifies that modules are correctly included or
1338 // excluded in the vendor snapshot based on their path (framework or
1339 // vendor) and the exclude_from_vendor_snapshot property.
1340
1341 frameworkBp := `
1342 cc_library_shared {
1343 name: "libinclude",
1344 srcs: ["src/include.cpp"],
1345 vendor_available: true,
1346 }
1347 cc_library_shared {
1348 name: "libexclude",
1349 srcs: ["src/exclude.cpp"],
1350 vendor: true,
1351 exclude_from_vendor_snapshot: true,
1352 }
1353 `
1354
1355 vendorProprietaryBp := `
1356 cc_library_shared {
1357 name: "libvendor",
1358 srcs: ["vendor.cpp"],
1359 vendor: true,
1360 }
1361 `
1362
1363 depsBp := GatherRequiredDepsForTest(android.Android)
1364
1365 mockFS := map[string][]byte{
1366 "deps/Android.bp": []byte(depsBp),
1367 "framework/Android.bp": []byte(frameworkBp),
1368 "framework/include.cpp": nil,
1369 "framework/exclude.cpp": nil,
1370 "device/Android.bp": []byte(vendorProprietaryBp),
1371 "device/vendor.cpp": nil,
1372 }
1373
1374 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1375 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1376 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1377 ctx := CreateTestContext()
1378 ctx.Register(config)
1379
1380 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1381 android.FailIfErrored(t, errs)
1382 _, errs = ctx.PrepareBuildActions(config)
1383 android.FailIfErrored(t, errs)
1384
1385 // Test an include and exclude framework module.
1386 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1387 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", vendorVariant).Module().(*Module), false)
1388 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libexclude", vendorVariant).Module().(*Module), true)
1389
1390 // A vendor module is excluded, but by its path, not the
1391 // exclude_from_vendor_snapshot property.
1392 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libvendor", vendorVariant).Module().(*Module), false)
1393
1394 // Verify the content of the vendor snapshot.
1395
1396 snapshotDir := "vendor-snapshot"
1397 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1398 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1399
1400 var includeJsonFiles []string
1401 var excludeJsonFiles []string
1402
1403 for _, arch := range [][]string{
1404 []string{"arm64", "armv8-a"},
1405 []string{"arm", "armv7-a-neon"},
1406 } {
1407 archType := arch[0]
1408 archVariant := arch[1]
1409 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1410
1411 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1412 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1413
1414 // Included modules
1415 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1416 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1417
1418 // Excluded modules
1419 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1420 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1421 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1422 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1423 }
1424
1425 // Verify that each json file for an included module has a rule.
1426 for _, jsonFile := range includeJsonFiles {
1427 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1428 t.Errorf("include json file %q not found", jsonFile)
1429 }
1430 }
1431
1432 // Verify that each json file for an excluded module has no rule.
1433 for _, jsonFile := range excludeJsonFiles {
1434 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1435 t.Errorf("exclude json file %q found", jsonFile)
1436 }
1437 }
1438}
1439
1440func TestVendorSnapshotExcludeInVendorProprietaryPathErrors(t *testing.T) {
1441
1442 // This test verifies that using the exclude_from_vendor_snapshot
1443 // property on a module in a vendor proprietary path generates an
1444 // error. These modules are already excluded, so we prohibit using the
1445 // property in this way, which could add to confusion.
1446
1447 vendorProprietaryBp := `
1448 cc_library_shared {
1449 name: "libvendor",
1450 srcs: ["vendor.cpp"],
1451 vendor: true,
1452 exclude_from_vendor_snapshot: true,
1453 }
1454 `
1455
1456 depsBp := GatherRequiredDepsForTest(android.Android)
1457
1458 mockFS := map[string][]byte{
1459 "deps/Android.bp": []byte(depsBp),
1460 "device/Android.bp": []byte(vendorProprietaryBp),
1461 "device/vendor.cpp": nil,
1462 }
1463
1464 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1465 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1466 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1467 ctx := CreateTestContext()
1468 ctx.Register(config)
1469
1470 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "device/Android.bp"})
1471 android.FailIfErrored(t, errs)
1472
1473 _, errs = ctx.PrepareBuildActions(config)
1474 android.CheckErrorsAgainstExpectations(t, errs, []string{
1475 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1476 `module "libvendor\{.+,image:vendor.+,arch:arm_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1477 })
1478}
1479
1480func TestVendorSnapshotExcludeWithVendorAvailable(t *testing.T) {
1481
1482 // This test verifies that using the exclude_from_vendor_snapshot
1483 // property on a module that is vendor available generates an error. A
1484 // vendor available module must be captured in the vendor snapshot and
1485 // must not built from source when building the vendor image against
1486 // the vendor snapshot.
1487
1488 frameworkBp := `
1489 cc_library_shared {
1490 name: "libinclude",
1491 srcs: ["src/include.cpp"],
1492 vendor_available: true,
1493 exclude_from_vendor_snapshot: true,
1494 }
1495 `
1496
1497 depsBp := GatherRequiredDepsForTest(android.Android)
1498
1499 mockFS := map[string][]byte{
1500 "deps/Android.bp": []byte(depsBp),
1501 "framework/Android.bp": []byte(frameworkBp),
1502 "framework/include.cpp": nil,
1503 }
1504
1505 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1506 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1507 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1508 ctx := CreateTestContext()
1509 ctx.Register(config)
1510
1511 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp"})
1512 android.FailIfErrored(t, errs)
1513
1514 _, errs = ctx.PrepareBuildActions(config)
1515 android.CheckErrorsAgainstExpectations(t, errs, []string{
1516 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1517 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1518 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1519 `module "libinclude\{.+,image:vendor.+,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1520 })
1521}
1522
Jooyung Hana70f0672019-01-18 15:20:43 +09001523func TestDoubleLoadableDepError(t *testing.T) {
1524 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1525 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1526 cc_library {
1527 name: "libllndk",
1528 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001529 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001530 }
1531
1532 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001533 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001534 symbol_file: "",
1535 }
1536
1537 cc_library {
1538 name: "libnondoubleloadable",
1539 vendor_available: true,
1540 vndk: {
1541 enabled: true,
1542 },
1543 }
1544 `)
1545
1546 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1547 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1548 cc_library {
1549 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001550 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001551 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001552 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001553 }
1554
1555 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001556 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001557 symbol_file: "",
1558 }
1559
1560 cc_library {
1561 name: "libnondoubleloadable",
1562 vendor_available: true,
1563 }
1564 `)
1565
1566 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
1567 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1568 cc_library {
1569 name: "libdoubleloadable",
1570 vendor_available: true,
1571 double_loadable: true,
1572 shared_libs: ["libnondoubleloadable"],
1573 }
1574
1575 cc_library {
1576 name: "libnondoubleloadable",
1577 vendor_available: true,
1578 }
1579 `)
1580
1581 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
1582 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1583 cc_library {
1584 name: "libdoubleloadable",
1585 vendor_available: true,
1586 double_loadable: true,
1587 shared_libs: ["libnondoubleloadable"],
1588 }
1589
1590 cc_library {
1591 name: "libnondoubleloadable",
1592 vendor_available: true,
1593 vndk: {
1594 enabled: true,
1595 },
1596 }
1597 `)
1598
1599 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
1600 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1601 cc_library {
1602 name: "libdoubleloadable",
1603 vendor_available: true,
1604 vndk: {
1605 enabled: true,
1606 },
1607 double_loadable: true,
1608 shared_libs: ["libnondoubleloadable"],
1609 }
1610
1611 cc_library {
1612 name: "libnondoubleloadable",
1613 vendor_available: false,
1614 vndk: {
1615 enabled: true,
1616 },
1617 }
1618 `)
1619
1620 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1621 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1622 cc_library {
1623 name: "libllndk",
1624 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001625 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001626 }
1627
1628 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001629 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001630 symbol_file: "",
1631 }
1632
1633 cc_library {
1634 name: "libcoreonly",
1635 shared_libs: ["libvendoravailable"],
1636 }
1637
1638 // indirect dependency of LLNDK
1639 cc_library {
1640 name: "libvendoravailable",
1641 vendor_available: true,
1642 }
1643 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001644}
1645
Logan Chienf3511742017-10-31 18:04:35 +08001646func TestVndkExt(t *testing.T) {
1647 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001648 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001649 cc_library {
1650 name: "libvndk",
1651 vendor_available: true,
1652 vndk: {
1653 enabled: true,
1654 },
1655 nocrt: true,
1656 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001657 cc_library {
1658 name: "libvndk2",
1659 vendor_available: true,
1660 vndk: {
1661 enabled: true,
1662 },
1663 target: {
1664 vendor: {
1665 suffix: "-suffix",
1666 },
1667 },
1668 nocrt: true,
1669 }
Logan Chienf3511742017-10-31 18:04:35 +08001670
1671 cc_library {
1672 name: "libvndk_ext",
1673 vendor: true,
1674 vndk: {
1675 enabled: true,
1676 extends: "libvndk",
1677 },
1678 nocrt: true,
1679 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001680
1681 cc_library {
1682 name: "libvndk2_ext",
1683 vendor: true,
1684 vndk: {
1685 enabled: true,
1686 extends: "libvndk2",
1687 },
1688 nocrt: true,
1689 }
Logan Chienf3511742017-10-31 18:04:35 +08001690
Justin Yun0ecf0b22020-02-28 15:07:59 +09001691 cc_library {
1692 name: "libvndk_ext_product",
1693 product_specific: true,
1694 vndk: {
1695 enabled: true,
1696 extends: "libvndk",
1697 },
1698 nocrt: true,
1699 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001700
Justin Yun0ecf0b22020-02-28 15:07:59 +09001701 cc_library {
1702 name: "libvndk2_ext_product",
1703 product_specific: true,
1704 vndk: {
1705 enabled: true,
1706 extends: "libvndk2",
1707 },
1708 nocrt: true,
1709 }
1710 `
1711 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1712 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1713 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1714 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1715
1716 ctx := testCcWithConfig(t, config)
1717
1718 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1719 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1720
1721 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1722 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1723
1724 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1725 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001726}
1727
Logan Chiend3c59a22018-03-29 14:08:15 +08001728func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001729 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1730 ctx := testCcNoVndk(t, `
1731 cc_library {
1732 name: "libvndk",
1733 vendor_available: true,
1734 vndk: {
1735 enabled: true,
1736 },
1737 nocrt: true,
1738 }
1739
1740 cc_library {
1741 name: "libvndk_ext",
1742 vendor: true,
1743 vndk: {
1744 enabled: true,
1745 extends: "libvndk",
1746 },
1747 nocrt: true,
1748 }
1749 `)
1750
1751 // Ensures that the core variant of "libvndk_ext" can be found.
1752 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1753 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1754 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1755 }
1756}
1757
Justin Yun0ecf0b22020-02-28 15:07:59 +09001758func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1759 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
1760 ctx := testCc(t, `
1761 cc_library {
1762 name: "libvndk",
1763 vendor_available: true,
1764 vndk: {
1765 enabled: true,
1766 },
1767 nocrt: true,
1768 }
1769
1770 cc_library {
1771 name: "libvndk_ext_product",
1772 product_specific: true,
1773 vndk: {
1774 enabled: true,
1775 extends: "libvndk",
1776 },
1777 nocrt: true,
1778 }
1779 `)
1780
1781 // Ensures that the core variant of "libvndk_ext_product" can be found.
1782 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1783 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1784 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1785 }
1786}
1787
Logan Chienf3511742017-10-31 18:04:35 +08001788func TestVndkExtError(t *testing.T) {
1789 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001790 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001791 cc_library {
1792 name: "libvndk",
1793 vendor_available: true,
1794 vndk: {
1795 enabled: true,
1796 },
1797 nocrt: true,
1798 }
1799
1800 cc_library {
1801 name: "libvndk_ext",
1802 vndk: {
1803 enabled: true,
1804 extends: "libvndk",
1805 },
1806 nocrt: true,
1807 }
1808 `)
1809
1810 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1811 cc_library {
1812 name: "libvndk",
1813 vendor_available: true,
1814 vndk: {
1815 enabled: true,
1816 },
1817 nocrt: true,
1818 }
1819
1820 cc_library {
1821 name: "libvndk_ext",
1822 vendor: true,
1823 vndk: {
1824 enabled: true,
1825 },
1826 nocrt: true,
1827 }
1828 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001829
1830 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1831 cc_library {
1832 name: "libvndk",
1833 vendor_available: true,
1834 vndk: {
1835 enabled: true,
1836 },
1837 nocrt: true,
1838 }
1839
1840 cc_library {
1841 name: "libvndk_ext_product",
1842 product_specific: true,
1843 vndk: {
1844 enabled: true,
1845 },
1846 nocrt: true,
1847 }
1848 `)
1849
1850 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1851 cc_library {
1852 name: "libvndk",
1853 vendor_available: true,
1854 vndk: {
1855 enabled: true,
1856 },
1857 nocrt: true,
1858 }
1859
1860 cc_library {
1861 name: "libvndk_ext_product",
1862 product_specific: true,
1863 vendor_available: true,
1864 vndk: {
1865 enabled: true,
1866 extends: "libvndk",
1867 },
1868 nocrt: true,
1869 }
1870 `)
Logan Chienf3511742017-10-31 18:04:35 +08001871}
1872
1873func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1874 // This test ensures an error is emitted for inconsistent support_system_process.
1875 testCcError(t, "module \".*\" with mismatched support_system_process", `
1876 cc_library {
1877 name: "libvndk",
1878 vendor_available: true,
1879 vndk: {
1880 enabled: true,
1881 },
1882 nocrt: true,
1883 }
1884
1885 cc_library {
1886 name: "libvndk_sp_ext",
1887 vendor: true,
1888 vndk: {
1889 enabled: true,
1890 extends: "libvndk",
1891 support_system_process: true,
1892 },
1893 nocrt: true,
1894 }
1895 `)
1896
1897 testCcError(t, "module \".*\" with mismatched support_system_process", `
1898 cc_library {
1899 name: "libvndk_sp",
1900 vendor_available: true,
1901 vndk: {
1902 enabled: true,
1903 support_system_process: true,
1904 },
1905 nocrt: true,
1906 }
1907
1908 cc_library {
1909 name: "libvndk_ext",
1910 vendor: true,
1911 vndk: {
1912 enabled: true,
1913 extends: "libvndk_sp",
1914 },
1915 nocrt: true,
1916 }
1917 `)
1918}
1919
1920func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001921 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08001922 // with `vendor_available: false`.
1923 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1924 cc_library {
1925 name: "libvndk",
1926 vendor_available: false,
1927 vndk: {
1928 enabled: true,
1929 },
1930 nocrt: true,
1931 }
1932
1933 cc_library {
1934 name: "libvndk_ext",
1935 vendor: true,
1936 vndk: {
1937 enabled: true,
1938 extends: "libvndk",
1939 },
1940 nocrt: true,
1941 }
1942 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001943
1944 testCcErrorProductVndk(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1945 cc_library {
1946 name: "libvndk",
1947 vendor_available: false,
1948 vndk: {
1949 enabled: true,
1950 },
1951 nocrt: true,
1952 }
1953
1954 cc_library {
1955 name: "libvndk_ext_product",
1956 product_specific: true,
1957 vndk: {
1958 enabled: true,
1959 extends: "libvndk",
1960 },
1961 nocrt: true,
1962 }
1963 `)
Logan Chienf3511742017-10-31 18:04:35 +08001964}
1965
Logan Chiend3c59a22018-03-29 14:08:15 +08001966func TestVendorModuleUseVndkExt(t *testing.T) {
1967 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001968 testCc(t, `
1969 cc_library {
1970 name: "libvndk",
1971 vendor_available: true,
1972 vndk: {
1973 enabled: true,
1974 },
1975 nocrt: true,
1976 }
1977
1978 cc_library {
1979 name: "libvndk_ext",
1980 vendor: true,
1981 vndk: {
1982 enabled: true,
1983 extends: "libvndk",
1984 },
1985 nocrt: true,
1986 }
1987
1988 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001989 name: "libvndk_sp",
1990 vendor_available: true,
1991 vndk: {
1992 enabled: true,
1993 support_system_process: true,
1994 },
1995 nocrt: true,
1996 }
1997
1998 cc_library {
1999 name: "libvndk_sp_ext",
2000 vendor: true,
2001 vndk: {
2002 enabled: true,
2003 extends: "libvndk_sp",
2004 support_system_process: true,
2005 },
2006 nocrt: true,
2007 }
2008
2009 cc_library {
2010 name: "libvendor",
2011 vendor: true,
2012 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
2013 nocrt: true,
2014 }
2015 `)
2016}
2017
Logan Chiend3c59a22018-03-29 14:08:15 +08002018func TestVndkExtUseVendorLib(t *testing.T) {
2019 // This test ensures a VNDK-Ext library can depend on a vendor 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 shared_libs: ["libvendor"],
2038 nocrt: true,
2039 }
2040
2041 cc_library {
2042 name: "libvendor",
2043 vendor: true,
2044 nocrt: true,
2045 }
2046 `)
Logan Chienf3511742017-10-31 18:04:35 +08002047
Logan Chiend3c59a22018-03-29 14:08:15 +08002048 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
2049 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08002050 cc_library {
2051 name: "libvndk_sp",
2052 vendor_available: true,
2053 vndk: {
2054 enabled: true,
2055 support_system_process: true,
2056 },
2057 nocrt: true,
2058 }
2059
2060 cc_library {
2061 name: "libvndk_sp_ext",
2062 vendor: true,
2063 vndk: {
2064 enabled: true,
2065 extends: "libvndk_sp",
2066 support_system_process: true,
2067 },
2068 shared_libs: ["libvendor"], // Cause an error
2069 nocrt: true,
2070 }
2071
2072 cc_library {
2073 name: "libvendor",
2074 vendor: true,
2075 nocrt: true,
2076 }
2077 `)
2078}
2079
Justin Yun0ecf0b22020-02-28 15:07:59 +09002080func TestProductVndkExtDependency(t *testing.T) {
2081 bp := `
2082 cc_library {
2083 name: "libvndk",
2084 vendor_available: true,
2085 vndk: {
2086 enabled: true,
2087 },
2088 nocrt: true,
2089 }
2090
2091 cc_library {
2092 name: "libvndk_ext_product",
2093 product_specific: true,
2094 vndk: {
2095 enabled: true,
2096 extends: "libvndk",
2097 },
2098 shared_libs: ["libproduct_for_vndklibs"],
2099 nocrt: true,
2100 }
2101
2102 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_product",
2114 product_specific: true,
2115 vndk: {
2116 enabled: true,
2117 extends: "libvndk_sp",
2118 support_system_process: true,
2119 },
2120 shared_libs: ["libproduct_for_vndklibs"],
2121 nocrt: true,
2122 }
2123
2124 cc_library {
2125 name: "libproduct",
2126 product_specific: true,
2127 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
2128 nocrt: true,
2129 }
2130
2131 cc_library {
2132 name: "libproduct_for_vndklibs",
2133 product_specific: true,
2134 nocrt: true,
2135 }
2136 `
2137 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2138 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2139 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2140 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2141
2142 testCcWithConfig(t, config)
2143}
2144
Logan Chiend3c59a22018-03-29 14:08:15 +08002145func TestVndkSpExtUseVndkError(t *testing.T) {
2146 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
2147 // library.
2148 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2149 cc_library {
2150 name: "libvndk",
2151 vendor_available: true,
2152 vndk: {
2153 enabled: true,
2154 },
2155 nocrt: true,
2156 }
2157
2158 cc_library {
2159 name: "libvndk_sp",
2160 vendor_available: true,
2161 vndk: {
2162 enabled: true,
2163 support_system_process: true,
2164 },
2165 nocrt: true,
2166 }
2167
2168 cc_library {
2169 name: "libvndk_sp_ext",
2170 vendor: true,
2171 vndk: {
2172 enabled: true,
2173 extends: "libvndk_sp",
2174 support_system_process: true,
2175 },
2176 shared_libs: ["libvndk"], // Cause an error
2177 nocrt: true,
2178 }
2179 `)
2180
2181 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
2182 // library.
2183 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2184 cc_library {
2185 name: "libvndk",
2186 vendor_available: true,
2187 vndk: {
2188 enabled: true,
2189 },
2190 nocrt: true,
2191 }
2192
2193 cc_library {
2194 name: "libvndk_ext",
2195 vendor: true,
2196 vndk: {
2197 enabled: true,
2198 extends: "libvndk",
2199 },
2200 nocrt: true,
2201 }
2202
2203 cc_library {
2204 name: "libvndk_sp",
2205 vendor_available: true,
2206 vndk: {
2207 enabled: true,
2208 support_system_process: true,
2209 },
2210 nocrt: true,
2211 }
2212
2213 cc_library {
2214 name: "libvndk_sp_ext",
2215 vendor: true,
2216 vndk: {
2217 enabled: true,
2218 extends: "libvndk_sp",
2219 support_system_process: true,
2220 },
2221 shared_libs: ["libvndk_ext"], // Cause an error
2222 nocrt: true,
2223 }
2224 `)
2225}
2226
2227func TestVndkUseVndkExtError(t *testing.T) {
2228 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2229 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002230 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2231 cc_library {
2232 name: "libvndk",
2233 vendor_available: true,
2234 vndk: {
2235 enabled: true,
2236 },
2237 nocrt: true,
2238 }
2239
2240 cc_library {
2241 name: "libvndk_ext",
2242 vendor: true,
2243 vndk: {
2244 enabled: true,
2245 extends: "libvndk",
2246 },
2247 nocrt: true,
2248 }
2249
2250 cc_library {
2251 name: "libvndk2",
2252 vendor_available: true,
2253 vndk: {
2254 enabled: true,
2255 },
2256 shared_libs: ["libvndk_ext"],
2257 nocrt: true,
2258 }
2259 `)
2260
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002261 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002262 cc_library {
2263 name: "libvndk",
2264 vendor_available: true,
2265 vndk: {
2266 enabled: true,
2267 },
2268 nocrt: true,
2269 }
2270
2271 cc_library {
2272 name: "libvndk_ext",
2273 vendor: true,
2274 vndk: {
2275 enabled: true,
2276 extends: "libvndk",
2277 },
2278 nocrt: true,
2279 }
2280
2281 cc_library {
2282 name: "libvndk2",
2283 vendor_available: true,
2284 vndk: {
2285 enabled: true,
2286 },
2287 target: {
2288 vendor: {
2289 shared_libs: ["libvndk_ext"],
2290 },
2291 },
2292 nocrt: true,
2293 }
2294 `)
2295
2296 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2297 cc_library {
2298 name: "libvndk_sp",
2299 vendor_available: true,
2300 vndk: {
2301 enabled: true,
2302 support_system_process: true,
2303 },
2304 nocrt: true,
2305 }
2306
2307 cc_library {
2308 name: "libvndk_sp_ext",
2309 vendor: true,
2310 vndk: {
2311 enabled: true,
2312 extends: "libvndk_sp",
2313 support_system_process: true,
2314 },
2315 nocrt: true,
2316 }
2317
2318 cc_library {
2319 name: "libvndk_sp_2",
2320 vendor_available: true,
2321 vndk: {
2322 enabled: true,
2323 support_system_process: true,
2324 },
2325 shared_libs: ["libvndk_sp_ext"],
2326 nocrt: true,
2327 }
2328 `)
2329
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002330 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002331 cc_library {
2332 name: "libvndk_sp",
2333 vendor_available: true,
2334 vndk: {
2335 enabled: true,
2336 },
2337 nocrt: true,
2338 }
2339
2340 cc_library {
2341 name: "libvndk_sp_ext",
2342 vendor: true,
2343 vndk: {
2344 enabled: true,
2345 extends: "libvndk_sp",
2346 },
2347 nocrt: true,
2348 }
2349
2350 cc_library {
2351 name: "libvndk_sp2",
2352 vendor_available: true,
2353 vndk: {
2354 enabled: true,
2355 },
2356 target: {
2357 vendor: {
2358 shared_libs: ["libvndk_sp_ext"],
2359 },
2360 },
2361 nocrt: true,
2362 }
2363 `)
2364}
2365
Justin Yun5f7f7e82019-11-18 19:52:14 +09002366func TestEnforceProductVndkVersion(t *testing.T) {
2367 bp := `
2368 cc_library {
2369 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002370 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002371 }
2372 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002373 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002374 symbol_file: "",
2375 }
2376 cc_library {
2377 name: "libvndk",
2378 vendor_available: true,
2379 vndk: {
2380 enabled: true,
2381 },
2382 nocrt: true,
2383 }
2384 cc_library {
2385 name: "libvndk_sp",
2386 vendor_available: true,
2387 vndk: {
2388 enabled: true,
2389 support_system_process: true,
2390 },
2391 nocrt: true,
2392 }
2393 cc_library {
2394 name: "libva",
2395 vendor_available: true,
2396 nocrt: true,
2397 }
2398 cc_library {
2399 name: "libproduct_va",
2400 product_specific: true,
2401 vendor_available: true,
2402 nocrt: true,
2403 }
2404 cc_library {
2405 name: "libprod",
2406 product_specific: true,
2407 shared_libs: [
2408 "libllndk",
2409 "libvndk",
2410 "libvndk_sp",
2411 "libva",
2412 "libproduct_va",
2413 ],
2414 nocrt: true,
2415 }
2416 cc_library {
2417 name: "libvendor",
2418 vendor: true,
2419 shared_libs: [
2420 "libllndk",
2421 "libvndk",
2422 "libvndk_sp",
2423 "libva",
2424 "libproduct_va",
2425 ],
2426 nocrt: true,
2427 }
2428 `
2429
2430 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2431 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2432 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2433 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2434
2435 ctx := testCcWithConfig(t, config)
2436
Justin Yun0ecf0b22020-02-28 15:07:59 +09002437 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "", productVariant)
2438 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "", productVariant)
Justin Yun5f7f7e82019-11-18 19:52:14 +09002439}
2440
2441func TestEnforceProductVndkVersionErrors(t *testing.T) {
2442 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2443 cc_library {
2444 name: "libprod",
2445 product_specific: true,
2446 shared_libs: [
2447 "libvendor",
2448 ],
2449 nocrt: true,
2450 }
2451 cc_library {
2452 name: "libvendor",
2453 vendor: true,
2454 nocrt: true,
2455 }
2456 `)
2457 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2458 cc_library {
2459 name: "libprod",
2460 product_specific: true,
2461 shared_libs: [
2462 "libsystem",
2463 ],
2464 nocrt: true,
2465 }
2466 cc_library {
2467 name: "libsystem",
2468 nocrt: true,
2469 }
2470 `)
2471 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
2472 cc_library {
2473 name: "libprod",
2474 product_specific: true,
2475 shared_libs: [
2476 "libvndk_private",
2477 ],
2478 nocrt: true,
2479 }
2480 cc_library {
2481 name: "libvndk_private",
2482 vendor_available: false,
2483 vndk: {
2484 enabled: true,
2485 },
2486 nocrt: true,
2487 }
2488 `)
2489 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2490 cc_library {
2491 name: "libprod",
2492 product_specific: true,
2493 shared_libs: [
2494 "libsystem_ext",
2495 ],
2496 nocrt: true,
2497 }
2498 cc_library {
2499 name: "libsystem_ext",
2500 system_ext_specific: true,
2501 nocrt: true,
2502 }
2503 `)
2504 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2505 cc_library {
2506 name: "libsystem",
2507 shared_libs: [
2508 "libproduct_va",
2509 ],
2510 nocrt: true,
2511 }
2512 cc_library {
2513 name: "libproduct_va",
2514 product_specific: true,
2515 vendor_available: true,
2516 nocrt: true,
2517 }
2518 `)
2519}
2520
Jooyung Han38002912019-05-16 04:01:54 +09002521func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002522 bp := `
2523 cc_library {
2524 name: "libvndk",
2525 vendor_available: true,
2526 vndk: {
2527 enabled: true,
2528 },
2529 }
2530 cc_library {
2531 name: "libvndksp",
2532 vendor_available: true,
2533 vndk: {
2534 enabled: true,
2535 support_system_process: true,
2536 },
2537 }
2538 cc_library {
2539 name: "libvndkprivate",
2540 vendor_available: false,
2541 vndk: {
2542 enabled: true,
2543 },
2544 }
2545 cc_library {
2546 name: "libvendor",
2547 vendor: true,
2548 }
2549 cc_library {
2550 name: "libvndkext",
2551 vendor: true,
2552 vndk: {
2553 enabled: true,
2554 extends: "libvndk",
2555 },
2556 }
2557 vndk_prebuilt_shared {
2558 name: "prevndk",
2559 version: "27",
2560 target_arch: "arm",
2561 binder32bit: true,
2562 vendor_available: true,
2563 vndk: {
2564 enabled: true,
2565 },
2566 arch: {
2567 arm: {
2568 srcs: ["liba.so"],
2569 },
2570 },
2571 }
2572 cc_library {
2573 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002574 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002575 }
2576 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002577 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002578 symbol_file: "",
2579 }
2580 cc_library {
2581 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07002582 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002583 }
2584 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002585 name: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002586 vendor_available: false,
2587 symbol_file: "",
2588 }`
2589
2590 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002591 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2592 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2593 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002594 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002595
Jooyung Han0302a842019-10-30 18:43:49 +09002596 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002597 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09002598 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002599 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09002600 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002601 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09002602 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002603 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09002604
Colin Crossfb0c16e2019-11-20 17:12:35 -08002605 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002606
Jooyung Han38002912019-05-16 04:01:54 +09002607 tests := []struct {
2608 variant string
2609 name string
2610 expected string
2611 }{
2612 {vendorVariant, "libvndk", "native:vndk"},
2613 {vendorVariant, "libvndksp", "native:vndk"},
2614 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2615 {vendorVariant, "libvendor", "native:vendor"},
2616 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09002617 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002618 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002619 {coreVariant, "libvndk", "native:platform"},
2620 {coreVariant, "libvndkprivate", "native:platform"},
2621 {coreVariant, "libllndk", "native:platform"},
2622 }
2623 for _, test := range tests {
2624 t.Run(test.name, func(t *testing.T) {
2625 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2626 assertString(t, module.makeLinkType, test.expected)
2627 })
2628 }
2629}
2630
Colin Cross0af4b842015-04-30 16:36:18 -07002631var (
2632 str11 = "01234567891"
2633 str10 = str11[:10]
2634 str9 = str11[:9]
2635 str5 = str11[:5]
2636 str4 = str11[:4]
2637)
2638
2639var splitListForSizeTestCases = []struct {
2640 in []string
2641 out [][]string
2642 size int
2643}{
2644 {
2645 in: []string{str10},
2646 out: [][]string{{str10}},
2647 size: 10,
2648 },
2649 {
2650 in: []string{str9},
2651 out: [][]string{{str9}},
2652 size: 10,
2653 },
2654 {
2655 in: []string{str5},
2656 out: [][]string{{str5}},
2657 size: 10,
2658 },
2659 {
2660 in: []string{str11},
2661 out: nil,
2662 size: 10,
2663 },
2664 {
2665 in: []string{str10, str10},
2666 out: [][]string{{str10}, {str10}},
2667 size: 10,
2668 },
2669 {
2670 in: []string{str9, str10},
2671 out: [][]string{{str9}, {str10}},
2672 size: 10,
2673 },
2674 {
2675 in: []string{str10, str9},
2676 out: [][]string{{str10}, {str9}},
2677 size: 10,
2678 },
2679 {
2680 in: []string{str5, str4},
2681 out: [][]string{{str5, str4}},
2682 size: 10,
2683 },
2684 {
2685 in: []string{str5, str4, str5},
2686 out: [][]string{{str5, str4}, {str5}},
2687 size: 10,
2688 },
2689 {
2690 in: []string{str5, str4, str5, str4},
2691 out: [][]string{{str5, str4}, {str5, str4}},
2692 size: 10,
2693 },
2694 {
2695 in: []string{str5, str4, str5, str5},
2696 out: [][]string{{str5, str4}, {str5}, {str5}},
2697 size: 10,
2698 },
2699 {
2700 in: []string{str5, str5, str5, str4},
2701 out: [][]string{{str5}, {str5}, {str5, str4}},
2702 size: 10,
2703 },
2704 {
2705 in: []string{str9, str11},
2706 out: nil,
2707 size: 10,
2708 },
2709 {
2710 in: []string{str11, str9},
2711 out: nil,
2712 size: 10,
2713 },
2714}
2715
2716func TestSplitListForSize(t *testing.T) {
2717 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08002718 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07002719
2720 var outStrings [][]string
2721
2722 if len(out) > 0 {
2723 outStrings = make([][]string, len(out))
2724 for i, o := range out {
2725 outStrings[i] = o.Strings()
2726 }
2727 }
2728
2729 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07002730 t.Errorf("incorrect output:")
2731 t.Errorf(" input: %#v", testCase.in)
2732 t.Errorf(" size: %d", testCase.size)
2733 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07002734 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07002735 }
2736 }
2737}
Jeff Gaston294356f2017-09-27 17:05:30 -07002738
2739var staticLinkDepOrderTestCases = []struct {
2740 // This is a string representation of a map[moduleName][]moduleDependency .
2741 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002742 inStatic string
2743
2744 // This is a string representation of a map[moduleName][]moduleDependency .
2745 // It models the dependencies declared in an Android.bp file.
2746 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002747
2748 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2749 // The keys of allOrdered specify which modules we would like to check.
2750 // The values of allOrdered specify the expected result (of the transitive closure of all
2751 // dependencies) for each module to test
2752 allOrdered string
2753
2754 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2755 // The keys of outOrdered specify which modules we would like to check.
2756 // The values of outOrdered specify the expected result (of the ordered linker command line)
2757 // for each module to test.
2758 outOrdered string
2759}{
2760 // Simple tests
2761 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002762 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002763 outOrdered: "",
2764 },
2765 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002766 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002767 outOrdered: "a:",
2768 },
2769 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002770 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002771 outOrdered: "a:b; b:",
2772 },
2773 // Tests of reordering
2774 {
2775 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002776 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002777 outOrdered: "a:b,c,d; b:d; c:d; d:",
2778 },
2779 {
2780 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002781 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002782 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2783 },
2784 {
2785 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002786 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002787 outOrdered: "a:d,b,e,c; d:b; e:c",
2788 },
2789 {
2790 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002791 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002792 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2793 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2794 },
2795 {
2796 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002797 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 -07002798 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2799 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2800 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002801 // shared dependencies
2802 {
2803 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2804 // So, we don't actually have to check that a shared dependency of c will change the order
2805 // of a library that depends statically on b and on c. We only need to check that if c has
2806 // a shared dependency on b, that that shows up in allOrdered.
2807 inShared: "c:b",
2808 allOrdered: "c:b",
2809 outOrdered: "c:",
2810 },
2811 {
2812 // This test doesn't actually include any shared dependencies but it's a reminder of what
2813 // the second phase of the above test would look like
2814 inStatic: "a:b,c; c:b",
2815 allOrdered: "a:c,b; c:b",
2816 outOrdered: "a:c,b; c:b",
2817 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002818 // tiebreakers for when two modules specifying different orderings and there is no dependency
2819 // to dictate an order
2820 {
2821 // 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 -08002822 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002823 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2824 },
2825 {
2826 // 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 -08002827 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 -07002828 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2829 },
2830 // Tests involving duplicate dependencies
2831 {
2832 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002833 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002834 outOrdered: "a:c,b",
2835 },
2836 {
2837 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002838 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002839 outOrdered: "a:d,c,b",
2840 },
2841 // Tests to confirm the nonexistence of infinite loops.
2842 // These cases should never happen, so as long as the test terminates and the
2843 // result is deterministic then that should be fine.
2844 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002845 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002846 outOrdered: "a:a",
2847 },
2848 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002849 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002850 allOrdered: "a:b,c; b:c,a; c:a,b",
2851 outOrdered: "a:b; b:c; c:a",
2852 },
2853 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002854 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002855 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2856 outOrdered: "a:c,b; b:a,c; c:b,a",
2857 },
2858}
2859
2860// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2861func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2862 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2863 strippedText := strings.Replace(text, " ", "", -1)
2864 if len(strippedText) < 1 {
2865 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2866 }
2867 allDeps = make(map[android.Path][]android.Path, 0)
2868
2869 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2870 moduleTexts := strings.Split(strippedText, ";")
2871
2872 outputForModuleName := func(moduleName string) android.Path {
2873 return android.PathForTesting(moduleName)
2874 }
2875
2876 for _, moduleText := range moduleTexts {
2877 // convert from "a:b,c" to ["a", "b,c"]
2878 components := strings.Split(moduleText, ":")
2879 if len(components) != 2 {
2880 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2881 }
2882 moduleName := components[0]
2883 moduleOutput := outputForModuleName(moduleName)
2884 modulesInOrder = append(modulesInOrder, moduleOutput)
2885
2886 depString := components[1]
2887 // convert from "b,c" to ["b", "c"]
2888 depNames := strings.Split(depString, ",")
2889 if len(depString) < 1 {
2890 depNames = []string{}
2891 }
2892 var deps []android.Path
2893 for _, depName := range depNames {
2894 deps = append(deps, outputForModuleName(depName))
2895 }
2896 allDeps[moduleOutput] = deps
2897 }
2898 return modulesInOrder, allDeps
2899}
2900
Jeff Gaston294356f2017-09-27 17:05:30 -07002901func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2902 for _, moduleName := range moduleNames {
2903 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
2904 output := module.outputFile.Path()
2905 paths = append(paths, output)
2906 }
2907 return paths
2908}
2909
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002910func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002911 ctx := testCc(t, `
2912 cc_library {
2913 name: "a",
2914 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002915 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002916 }
2917 cc_library {
2918 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002919 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002920 }
2921 cc_library {
2922 name: "c",
2923 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002924 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002925 }
2926 cc_library {
2927 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002928 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002929 }
2930
2931 `)
2932
Colin Cross7113d202019-11-20 16:39:12 -08002933 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002934 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07002935 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
2936 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002937
2938 if !reflect.DeepEqual(actual, expected) {
2939 t.Errorf("staticDeps orderings were not propagated correctly"+
2940 "\nactual: %v"+
2941 "\nexpected: %v",
2942 actual,
2943 expected,
2944 )
2945 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002946}
Jeff Gaston294356f2017-09-27 17:05:30 -07002947
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002948func TestStaticLibDepReorderingWithShared(t *testing.T) {
2949 ctx := testCc(t, `
2950 cc_library {
2951 name: "a",
2952 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002953 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002954 }
2955 cc_library {
2956 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002957 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002958 }
2959 cc_library {
2960 name: "c",
2961 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002962 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002963 }
2964
2965 `)
2966
Colin Cross7113d202019-11-20 16:39:12 -08002967 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002968 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07002969 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
2970 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002971
2972 if !reflect.DeepEqual(actual, expected) {
2973 t.Errorf("staticDeps orderings did not account for shared libs"+
2974 "\nactual: %v"+
2975 "\nexpected: %v",
2976 actual,
2977 expected,
2978 )
2979 }
2980}
2981
Jooyung Hanb04a4992020-03-13 18:57:35 +09002982func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002983 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002984 if !reflect.DeepEqual(actual, expected) {
2985 t.Errorf(message+
2986 "\nactual: %v"+
2987 "\nexpected: %v",
2988 actual,
2989 expected,
2990 )
2991 }
2992}
2993
Jooyung Han61b66e92020-03-21 14:21:46 +00002994func TestLlndkLibrary(t *testing.T) {
2995 ctx := testCc(t, `
2996 cc_library {
2997 name: "libllndk",
2998 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07002999 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003000 }
3001 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003002 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003003 }
3004 `)
3005 actual := ctx.ModuleVariantsForTests("libllndk.llndk")
3006 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00003007 "android_vendor.VER_arm64_armv8-a_shared_1",
3008 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003009 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003010 "android_vendor.VER_arm_armv7-a-neon_shared_1",
3011 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003012 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003013 }
3014 checkEquals(t, "variants for llndk stubs", expected, actual)
3015
3016 params := ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
3017 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
3018
3019 params = ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
3020 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
3021}
3022
Jiyong Parka46a4d52017-12-14 19:54:34 +09003023func TestLlndkHeaders(t *testing.T) {
3024 ctx := testCc(t, `
3025 llndk_headers {
3026 name: "libllndk_headers",
3027 export_include_dirs: ["my_include"],
3028 }
3029 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003030 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09003031 export_llndk_headers: ["libllndk_headers"],
3032 }
3033 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07003034 name: "libllndk",
3035 llndk_stubs: "libllndk.llndk",
3036 }
3037
3038 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09003039 name: "libvendor",
3040 shared_libs: ["libllndk"],
3041 vendor: true,
3042 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003043 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08003044 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09003045 }
3046 `)
3047
3048 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08003049 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09003050 cflags := cc.Args["cFlags"]
3051 if !strings.Contains(cflags, "-Imy_include") {
3052 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
3053 }
3054}
3055
Logan Chien43d34c32017-12-20 01:17:32 +08003056func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
3057 actual := module.Properties.AndroidMkRuntimeLibs
3058 if !reflect.DeepEqual(actual, expected) {
3059 t.Errorf("incorrect runtime_libs for shared libs"+
3060 "\nactual: %v"+
3061 "\nexpected: %v",
3062 actual,
3063 expected,
3064 )
3065 }
3066}
3067
3068const runtimeLibAndroidBp = `
3069 cc_library {
3070 name: "libvendor_available1",
3071 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003072 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003073 nocrt : true,
3074 system_shared_libs : [],
3075 }
3076 cc_library {
3077 name: "libvendor_available2",
3078 vendor_available: true,
3079 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003080 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003081 nocrt : true,
3082 system_shared_libs : [],
3083 }
3084 cc_library {
3085 name: "libvendor_available3",
3086 vendor_available: true,
3087 runtime_libs: ["libvendor_available1"],
3088 target: {
3089 vendor: {
3090 exclude_runtime_libs: ["libvendor_available1"],
3091 }
3092 },
Yi Konge7fe9912019-06-02 00:53:50 -07003093 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003094 nocrt : true,
3095 system_shared_libs : [],
3096 }
3097 cc_library {
3098 name: "libcore",
3099 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003100 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003101 nocrt : true,
3102 system_shared_libs : [],
3103 }
3104 cc_library {
3105 name: "libvendor1",
3106 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003107 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003108 nocrt : true,
3109 system_shared_libs : [],
3110 }
3111 cc_library {
3112 name: "libvendor2",
3113 vendor: true,
3114 runtime_libs: ["libvendor_available1", "libvendor1"],
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`
3120
3121func TestRuntimeLibs(t *testing.T) {
3122 ctx := testCc(t, runtimeLibAndroidBp)
3123
3124 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08003125 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003126
3127 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3128 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3129
3130 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
3131 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3132
3133 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3134 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08003135 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003136
3137 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3138 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
3139
3140 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
3141 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
3142}
3143
3144func TestExcludeRuntimeLibs(t *testing.T) {
3145 ctx := testCc(t, runtimeLibAndroidBp)
3146
Colin Cross7113d202019-11-20 16:39:12 -08003147 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003148 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
3149 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3150
Colin Crossfb0c16e2019-11-20 17:12:35 -08003151 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003152 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
3153 checkRuntimeLibs(t, nil, module)
3154}
3155
3156func TestRuntimeLibsNoVndk(t *testing.T) {
3157 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3158
3159 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3160
Colin Cross7113d202019-11-20 16:39:12 -08003161 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003162
3163 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3164 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3165
3166 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
3167 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
3168}
3169
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003170func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003171 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003172 actual := module.Properties.AndroidMkStaticLibs
3173 if !reflect.DeepEqual(actual, expected) {
3174 t.Errorf("incorrect static_libs"+
3175 "\nactual: %v"+
3176 "\nexpected: %v",
3177 actual,
3178 expected,
3179 )
3180 }
3181}
3182
3183const staticLibAndroidBp = `
3184 cc_library {
3185 name: "lib1",
3186 }
3187 cc_library {
3188 name: "lib2",
3189 static_libs: ["lib1"],
3190 }
3191`
3192
3193func TestStaticLibDepExport(t *testing.T) {
3194 ctx := testCc(t, staticLibAndroidBp)
3195
3196 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003197 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003198 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003199 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003200
3201 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003202 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003203 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3204 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003205 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003206}
3207
Jiyong Parkd08b6972017-09-26 10:50:54 +09003208var compilerFlagsTestCases = []struct {
3209 in string
3210 out bool
3211}{
3212 {
3213 in: "a",
3214 out: false,
3215 },
3216 {
3217 in: "-a",
3218 out: true,
3219 },
3220 {
3221 in: "-Ipath/to/something",
3222 out: false,
3223 },
3224 {
3225 in: "-isystempath/to/something",
3226 out: false,
3227 },
3228 {
3229 in: "--coverage",
3230 out: false,
3231 },
3232 {
3233 in: "-include a/b",
3234 out: true,
3235 },
3236 {
3237 in: "-include a/b c/d",
3238 out: false,
3239 },
3240 {
3241 in: "-DMACRO",
3242 out: true,
3243 },
3244 {
3245 in: "-DMAC RO",
3246 out: false,
3247 },
3248 {
3249 in: "-a -b",
3250 out: false,
3251 },
3252 {
3253 in: "-DMACRO=definition",
3254 out: true,
3255 },
3256 {
3257 in: "-DMACRO=defi nition",
3258 out: true, // TODO(jiyong): this should be false
3259 },
3260 {
3261 in: "-DMACRO(x)=x + 1",
3262 out: true,
3263 },
3264 {
3265 in: "-DMACRO=\"defi nition\"",
3266 out: true,
3267 },
3268}
3269
3270type mockContext struct {
3271 BaseModuleContext
3272 result bool
3273}
3274
3275func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3276 // CheckBadCompilerFlags calls this function when the flag should be rejected
3277 ctx.result = false
3278}
3279
3280func TestCompilerFlags(t *testing.T) {
3281 for _, testCase := range compilerFlagsTestCases {
3282 ctx := &mockContext{result: true}
3283 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3284 if ctx.result != testCase.out {
3285 t.Errorf("incorrect output:")
3286 t.Errorf(" input: %#v", testCase.in)
3287 t.Errorf(" expected: %#v", testCase.out)
3288 t.Errorf(" got: %#v", ctx.result)
3289 }
3290 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003291}
Jiyong Park374510b2018-03-19 18:23:01 +09003292
3293func TestVendorPublicLibraries(t *testing.T) {
3294 ctx := testCc(t, `
3295 cc_library_headers {
3296 name: "libvendorpublic_headers",
3297 export_include_dirs: ["my_include"],
3298 }
3299 vendor_public_library {
3300 name: "libvendorpublic",
3301 symbol_file: "",
3302 export_public_headers: ["libvendorpublic_headers"],
3303 }
3304 cc_library {
3305 name: "libvendorpublic",
3306 srcs: ["foo.c"],
3307 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003308 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003309 nocrt: true,
3310 }
3311
3312 cc_library {
3313 name: "libsystem",
3314 shared_libs: ["libvendorpublic"],
3315 vendor: false,
3316 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003317 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003318 nocrt: true,
3319 }
3320 cc_library {
3321 name: "libvendor",
3322 shared_libs: ["libvendorpublic"],
3323 vendor: true,
3324 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003325 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003326 nocrt: true,
3327 }
3328 `)
3329
Colin Cross7113d202019-11-20 16:39:12 -08003330 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003331 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003332
3333 // test if header search paths are correctly added
3334 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003335 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003336 cflags := cc.Args["cFlags"]
3337 if !strings.Contains(cflags, "-Imy_include") {
3338 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3339 }
3340
3341 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003342 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003343 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003344 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003345 if !strings.Contains(libflags, stubPaths[0].String()) {
3346 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3347 }
3348
3349 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003350 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003351 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003352 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003353 if !strings.Contains(libflags, stubPaths[0].String()) {
3354 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3355 }
3356
3357}
Jiyong Park37b25202018-07-11 10:49:27 +09003358
3359func TestRecovery(t *testing.T) {
3360 ctx := testCc(t, `
3361 cc_library_shared {
3362 name: "librecovery",
3363 recovery: true,
3364 }
3365 cc_library_shared {
3366 name: "librecovery32",
3367 recovery: true,
3368 compile_multilib:"32",
3369 }
Jiyong Park5baac542018-08-28 09:55:37 +09003370 cc_library_shared {
3371 name: "libHalInRecovery",
3372 recovery_available: true,
3373 vendor: true,
3374 }
Jiyong Park37b25202018-07-11 10:49:27 +09003375 `)
3376
3377 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003378 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003379 if len(variants) != 1 || !android.InList(arm64, variants) {
3380 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3381 }
3382
3383 variants = ctx.ModuleVariantsForTests("librecovery32")
3384 if android.InList(arm64, variants) {
3385 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3386 }
Jiyong Park5baac542018-08-28 09:55:37 +09003387
3388 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3389 if !recoveryModule.Platform() {
3390 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3391 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003392}
Jiyong Park5baac542018-08-28 09:55:37 +09003393
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003394func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3395 bp := `
3396 cc_prebuilt_test_library_shared {
3397 name: "test_lib",
3398 relative_install_path: "foo/bar/baz",
3399 srcs: ["srcpath/dontusethispath/baz.so"],
3400 }
3401
3402 cc_test {
3403 name: "main_test",
3404 data_libs: ["test_lib"],
3405 gtest: false,
3406 }
3407 `
3408
3409 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3410 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3411 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3412 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3413
3414 ctx := testCcWithConfig(t, config)
3415 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3416 testBinary := module.(*Module).linker.(*testBinary)
3417 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3418 if err != nil {
3419 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3420 }
3421 if len(outputFiles) != 1 {
3422 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3423 }
3424 if len(testBinary.dataPaths()) != 1 {
3425 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3426 }
3427
3428 outputPath := outputFiles[0].String()
3429
3430 if !strings.HasSuffix(outputPath, "/main_test") {
3431 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3432 }
3433 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
3434 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3435 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3436 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3437 }
3438}
3439
Jiyong Park7ed9de32018-10-15 22:25:07 +09003440func TestVersionedStubs(t *testing.T) {
3441 ctx := testCc(t, `
3442 cc_library_shared {
3443 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003444 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003445 stubs: {
3446 symbol_file: "foo.map.txt",
3447 versions: ["1", "2", "3"],
3448 },
3449 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003450
Jiyong Park7ed9de32018-10-15 22:25:07 +09003451 cc_library_shared {
3452 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003453 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003454 shared_libs: ["libFoo#1"],
3455 }`)
3456
3457 variants := ctx.ModuleVariantsForTests("libFoo")
3458 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003459 "android_arm64_armv8-a_shared",
3460 "android_arm64_armv8-a_shared_1",
3461 "android_arm64_armv8-a_shared_2",
3462 "android_arm64_armv8-a_shared_3",
3463 "android_arm_armv7-a-neon_shared",
3464 "android_arm_armv7-a-neon_shared_1",
3465 "android_arm_armv7-a-neon_shared_2",
3466 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003467 }
3468 variantsMismatch := false
3469 if len(variants) != len(expectedVariants) {
3470 variantsMismatch = true
3471 } else {
3472 for _, v := range expectedVariants {
3473 if !inList(v, variants) {
3474 variantsMismatch = false
3475 }
3476 }
3477 }
3478 if variantsMismatch {
3479 t.Errorf("variants of libFoo expected:\n")
3480 for _, v := range expectedVariants {
3481 t.Errorf("%q\n", v)
3482 }
3483 t.Errorf(", but got:\n")
3484 for _, v := range variants {
3485 t.Errorf("%q\n", v)
3486 }
3487 }
3488
Colin Cross7113d202019-11-20 16:39:12 -08003489 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003490 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003491 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003492 if !strings.Contains(libFlags, libFoo1StubPath) {
3493 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3494 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003495
Colin Cross7113d202019-11-20 16:39:12 -08003496 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003497 cFlags := libBarCompileRule.Args["cFlags"]
3498 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3499 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3500 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3501 }
Jiyong Park37b25202018-07-11 10:49:27 +09003502}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003503
Jooyung Hanb04a4992020-03-13 18:57:35 +09003504func TestVersioningMacro(t *testing.T) {
3505 for _, tc := range []struct{ moduleName, expected string }{
3506 {"libc", "__LIBC_API__"},
3507 {"libfoo", "__LIBFOO_API__"},
3508 {"libfoo@1", "__LIBFOO_1_API__"},
3509 {"libfoo-v1", "__LIBFOO_V1_API__"},
3510 {"libfoo.v1", "__LIBFOO_V1_API__"},
3511 } {
3512 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3513 }
3514}
3515
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003516func TestStaticExecutable(t *testing.T) {
3517 ctx := testCc(t, `
3518 cc_binary {
3519 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003520 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003521 static_executable: true,
3522 }`)
3523
Colin Cross7113d202019-11-20 16:39:12 -08003524 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003525 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3526 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003527 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003528 for _, lib := range systemStaticLibs {
3529 if !strings.Contains(libFlags, lib) {
3530 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3531 }
3532 }
3533 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3534 for _, lib := range systemSharedLibs {
3535 if strings.Contains(libFlags, lib) {
3536 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3537 }
3538 }
3539}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003540
3541func TestStaticDepsOrderWithStubs(t *testing.T) {
3542 ctx := testCc(t, `
3543 cc_binary {
3544 name: "mybin",
3545 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003546 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003547 static_executable: true,
3548 stl: "none",
3549 }
3550
3551 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003552 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003553 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003554 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003555 stl: "none",
3556 }
3557
3558 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003559 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003560 srcs: ["foo.c"],
3561 stl: "none",
3562 stubs: {
3563 versions: ["1"],
3564 },
3565 }`)
3566
Colin Cross0de8a1e2020-09-18 14:15:30 -07003567 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3568 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003569 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003570
3571 if !reflect.DeepEqual(actual, expected) {
3572 t.Errorf("staticDeps orderings were not propagated correctly"+
3573 "\nactual: %v"+
3574 "\nexpected: %v",
3575 actual,
3576 expected,
3577 )
3578 }
3579}
Jooyung Han38002912019-05-16 04:01:54 +09003580
Jooyung Hand48f3c32019-08-23 11:18:57 +09003581func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3582 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3583 cc_library {
3584 name: "libA",
3585 srcs: ["foo.c"],
3586 shared_libs: ["libB"],
3587 stl: "none",
3588 }
3589
3590 cc_library {
3591 name: "libB",
3592 srcs: ["foo.c"],
3593 enabled: false,
3594 stl: "none",
3595 }
3596 `)
3597}
3598
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003599// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3600// correctly.
3601func TestFuzzTarget(t *testing.T) {
3602 ctx := testCc(t, `
3603 cc_fuzz {
3604 name: "fuzz_smoke_test",
3605 srcs: ["foo.c"],
3606 }`)
3607
Paul Duffin075c4172019-12-19 19:06:13 +00003608 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003609 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3610}
3611
Jiyong Park29074592019-07-07 16:27:47 +09003612func TestAidl(t *testing.T) {
3613}
3614
Jooyung Han38002912019-05-16 04:01:54 +09003615func assertString(t *testing.T, got, expected string) {
3616 t.Helper()
3617 if got != expected {
3618 t.Errorf("expected %q got %q", expected, got)
3619 }
3620}
3621
3622func assertArrayString(t *testing.T, got, expected []string) {
3623 t.Helper()
3624 if len(got) != len(expected) {
3625 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3626 return
3627 }
3628 for i := range got {
3629 if got[i] != expected[i] {
3630 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3631 i, expected[i], expected, got[i], got)
3632 return
3633 }
3634 }
3635}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003636
Jooyung Han0302a842019-10-30 18:43:49 +09003637func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3638 t.Helper()
3639 assertArrayString(t, android.SortedStringKeys(m), expected)
3640}
3641
Colin Crosse1bb5d02019-09-24 14:55:04 -07003642func TestDefaults(t *testing.T) {
3643 ctx := testCc(t, `
3644 cc_defaults {
3645 name: "defaults",
3646 srcs: ["foo.c"],
3647 static: {
3648 srcs: ["bar.c"],
3649 },
3650 shared: {
3651 srcs: ["baz.c"],
3652 },
3653 }
3654
3655 cc_library_static {
3656 name: "libstatic",
3657 defaults: ["defaults"],
3658 }
3659
3660 cc_library_shared {
3661 name: "libshared",
3662 defaults: ["defaults"],
3663 }
3664
3665 cc_library {
3666 name: "libboth",
3667 defaults: ["defaults"],
3668 }
3669
3670 cc_binary {
3671 name: "binary",
3672 defaults: ["defaults"],
3673 }`)
3674
3675 pathsToBase := func(paths android.Paths) []string {
3676 var ret []string
3677 for _, p := range paths {
3678 ret = append(ret, p.Base())
3679 }
3680 return ret
3681 }
3682
Colin Cross7113d202019-11-20 16:39:12 -08003683 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003684 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3685 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3686 }
Colin Cross7113d202019-11-20 16:39:12 -08003687 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003688 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3689 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3690 }
Colin Cross7113d202019-11-20 16:39:12 -08003691 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003692 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3693 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3694 }
3695
Colin Cross7113d202019-11-20 16:39:12 -08003696 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003697 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3698 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3699 }
Colin Cross7113d202019-11-20 16:39:12 -08003700 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003701 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3702 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3703 }
3704}
Colin Crosseabaedd2020-02-06 17:01:55 -08003705
3706func TestProductVariableDefaults(t *testing.T) {
3707 bp := `
3708 cc_defaults {
3709 name: "libfoo_defaults",
3710 srcs: ["foo.c"],
3711 cppflags: ["-DFOO"],
3712 product_variables: {
3713 debuggable: {
3714 cppflags: ["-DBAR"],
3715 },
3716 },
3717 }
3718
3719 cc_library {
3720 name: "libfoo",
3721 defaults: ["libfoo_defaults"],
3722 }
3723 `
3724
3725 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3726 config.TestProductVariables.Debuggable = BoolPtr(true)
3727
3728 ctx := CreateTestContext()
3729 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
3730 ctx.BottomUp("variable", android.VariableMutator).Parallel()
3731 })
3732 ctx.Register(config)
3733
3734 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3735 android.FailIfErrored(t, errs)
3736 _, errs = ctx.PrepareBuildActions(config)
3737 android.FailIfErrored(t, errs)
3738
3739 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
3740 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
3741 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
3742 }
3743}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003744
3745func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3746 t.Parallel()
3747 bp := `
3748 cc_library_static {
3749 name: "libfoo",
3750 srcs: ["foo.c"],
3751 whole_static_libs: ["libbar"],
3752 }
3753
3754 cc_library_static {
3755 name: "libbar",
3756 whole_static_libs: ["libmissing"],
3757 }
3758 `
3759
3760 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3761 config.TestProductVariables.Allow_missing_dependencies = BoolPtr(true)
3762
3763 ctx := CreateTestContext()
3764 ctx.SetAllowMissingDependencies(true)
3765 ctx.Register(config)
3766
3767 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3768 android.FailIfErrored(t, errs)
3769 _, errs = ctx.PrepareBuildActions(config)
3770 android.FailIfErrored(t, errs)
3771
3772 libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
3773 if g, w := libbar.Rule, android.ErrorRule; g != w {
3774 t.Fatalf("Expected libbar rule to be %q, got %q", w, g)
3775 }
3776
3777 if g, w := libbar.Args["error"], "missing dependencies: libmissing"; !strings.Contains(g, w) {
3778 t.Errorf("Expected libbar error to contain %q, was %q", w, g)
3779 }
3780
3781 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
3782 if g, w := libfoo.Inputs.Strings(), libbar.Output.String(); !android.InList(w, g) {
3783 t.Errorf("Expected libfoo.a to depend on %q, got %q", w, g)
3784 }
3785
3786}