blob: c3caac81e4b348558e69e72998e11215212d64dc [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"],
849 }
850
851 llndk_library {
852 name: "libllndk",
853 symbol_file: "",
854 }
855
856 cc_library {
857 name: "libdoubleloadable",
858 vendor_available: true,
859 vndk: {
860 enabled: true,
861 },
862 double_loadable: true,
863 }
864 `)
865 // okay to link : LLNDK -> VNDK-SP
866 testCc(t, `
867 cc_library {
868 name: "libllndk",
869 shared_libs: ["libvndksp"],
870 }
871
872 llndk_library {
873 name: "libllndk",
874 symbol_file: "",
875 }
876
877 cc_library {
878 name: "libvndksp",
879 vendor_available: true,
880 vndk: {
881 enabled: true,
882 support_system_process: true,
883 },
884 }
885 `)
886 // okay to link : double_loadable -> double_loadable
887 testCc(t, `
888 cc_library {
889 name: "libdoubleloadable1",
890 shared_libs: ["libdoubleloadable2"],
891 vendor_available: true,
892 double_loadable: true,
893 }
894
895 cc_library {
896 name: "libdoubleloadable2",
897 vendor_available: true,
898 double_loadable: true,
899 }
900 `)
901 // okay to link : double_loadable VNDK -> double_loadable VNDK private
902 testCc(t, `
903 cc_library {
904 name: "libdoubleloadable",
905 vendor_available: true,
906 vndk: {
907 enabled: true,
908 },
909 double_loadable: true,
910 shared_libs: ["libnondoubleloadable"],
911 }
912
913 cc_library {
914 name: "libnondoubleloadable",
915 vendor_available: false,
916 vndk: {
917 enabled: true,
918 },
919 double_loadable: true,
920 }
921 `)
922 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
923 testCc(t, `
924 cc_library {
925 name: "libllndk",
926 shared_libs: ["libcoreonly"],
927 }
928
929 llndk_library {
930 name: "libllndk",
931 symbol_file: "",
932 }
933
934 cc_library {
935 name: "libcoreonly",
936 shared_libs: ["libvendoravailable"],
937 }
938
939 // indirect dependency of LLNDK
940 cc_library {
941 name: "libvendoravailable",
942 vendor_available: true,
943 double_loadable: true,
944 }
945 `)
946}
947
Inseob Kim5f58ff72020-09-07 19:53:31 +0900948func TestVendorSnapshotCapture(t *testing.T) {
Inseob Kim8471cda2019-11-15 09:59:12 +0900949 bp := `
950 cc_library {
951 name: "libvndk",
952 vendor_available: true,
953 vndk: {
954 enabled: true,
955 },
956 nocrt: true,
957 }
958
959 cc_library {
960 name: "libvendor",
961 vendor: true,
962 nocrt: true,
963 }
964
965 cc_library {
966 name: "libvendor_available",
967 vendor_available: true,
968 nocrt: true,
969 }
970
971 cc_library_headers {
972 name: "libvendor_headers",
973 vendor_available: true,
974 nocrt: true,
975 }
976
977 cc_binary {
978 name: "vendor_bin",
979 vendor: true,
980 nocrt: true,
981 }
982
983 cc_binary {
984 name: "vendor_available_bin",
985 vendor_available: true,
986 nocrt: true,
987 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900988
989 toolchain_library {
990 name: "libb",
991 vendor_available: true,
992 src: "libb.a",
993 }
Inseob Kim1042d292020-06-01 23:23:05 +0900994
995 cc_object {
996 name: "obj",
997 vendor_available: true,
998 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900999`
1000 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1001 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1002 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1003 ctx := testCcWithConfig(t, config)
1004
1005 // Check Vendor snapshot output.
1006
1007 snapshotDir := "vendor-snapshot"
1008 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +09001009 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1010
1011 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +09001012
1013 for _, arch := range [][]string{
1014 []string{"arm64", "armv8-a"},
1015 []string{"arm", "armv7-a-neon"},
1016 } {
1017 archType := arch[0]
1018 archVariant := arch[1]
1019 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1020
1021 // For shared libraries, only non-VNDK vendor_available modules are captured
1022 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1023 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +09001024 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1025 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
1026 jsonFiles = append(jsonFiles,
1027 filepath.Join(sharedDir, "libvendor.so.json"),
1028 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001029
1030 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
Inseob Kimc42f2f22020-07-29 20:32:10 +09001031 // Also cfi variants are captured, except for prebuilts like toolchain_library
Inseob Kim8471cda2019-11-15 09:59:12 +09001032 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001033 staticCfiVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static_cfi", archType, archVariant)
Inseob Kim8471cda2019-11-15 09:59:12 +09001034 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +09001035 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1036 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001037 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001038 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001039 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001040 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001041 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001042 jsonFiles = append(jsonFiles,
1043 filepath.Join(staticDir, "libb.a.json"),
1044 filepath.Join(staticDir, "libvndk.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001045 filepath.Join(staticDir, "libvndk.cfi.a.json"),
Inseob Kim7f283f42020-06-01 21:53:49 +09001046 filepath.Join(staticDir, "libvendor.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001047 filepath.Join(staticDir, "libvendor.cfi.a.json"),
1048 filepath.Join(staticDir, "libvendor_available.a.json"),
1049 filepath.Join(staticDir, "libvendor_available.cfi.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001050
Inseob Kim7f283f42020-06-01 21:53:49 +09001051 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +09001052 if archType == "arm64" {
1053 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1054 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +09001055 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
1056 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
1057 jsonFiles = append(jsonFiles,
1058 filepath.Join(binaryDir, "vendor_bin.json"),
1059 filepath.Join(binaryDir, "vendor_available_bin.json"))
1060 }
1061
1062 // For header libraries, all vendor:true and vendor_available modules are captured.
1063 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1064 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +09001065
1066 // For object modules, all vendor:true and vendor_available modules are captured.
1067 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1068 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1069 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1070 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001071 }
1072
1073 for _, jsonFile := range jsonFiles {
1074 // verify all json files exist
1075 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1076 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001077 }
1078 }
1079}
1080
Inseob Kim5f58ff72020-09-07 19:53:31 +09001081func TestVendorSnapshotUse(t *testing.T) {
1082 frameworkBp := `
1083 cc_library {
1084 name: "libvndk",
1085 vendor_available: true,
1086 vndk: {
1087 enabled: true,
1088 },
1089 nocrt: true,
1090 compile_multilib: "64",
1091 }
1092
1093 cc_library {
1094 name: "libvendor",
1095 vendor: true,
1096 nocrt: true,
1097 no_libcrt: true,
1098 stl: "none",
1099 system_shared_libs: [],
1100 compile_multilib: "64",
1101 }
1102
1103 cc_binary {
1104 name: "bin",
1105 vendor: true,
1106 nocrt: true,
1107 no_libcrt: true,
1108 stl: "none",
1109 system_shared_libs: [],
1110 compile_multilib: "64",
1111 }
1112`
1113
1114 vndkBp := `
1115 vndk_prebuilt_shared {
1116 name: "libvndk",
1117 version: "BOARD",
1118 target_arch: "arm64",
1119 vendor_available: true,
1120 vndk: {
1121 enabled: true,
1122 },
1123 arch: {
1124 arm64: {
1125 srcs: ["libvndk.so"],
Inseob Kim67be7322020-10-19 10:15:28 +09001126 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001127 },
1128 },
1129 }
1130`
1131
1132 vendorProprietaryBp := `
1133 cc_library {
1134 name: "libvendor_without_snapshot",
1135 vendor: true,
1136 nocrt: true,
1137 no_libcrt: true,
1138 stl: "none",
1139 system_shared_libs: [],
1140 compile_multilib: "64",
1141 }
1142
1143 cc_library_shared {
1144 name: "libclient",
1145 vendor: true,
1146 nocrt: true,
1147 no_libcrt: true,
1148 stl: "none",
1149 system_shared_libs: [],
1150 shared_libs: ["libvndk"],
1151 static_libs: ["libvendor", "libvendor_without_snapshot"],
1152 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001153 srcs: ["client.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001154 }
1155
1156 cc_binary {
1157 name: "bin_without_snapshot",
1158 vendor: true,
1159 nocrt: true,
1160 no_libcrt: true,
1161 stl: "none",
1162 system_shared_libs: [],
1163 static_libs: ["libvndk"],
1164 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001165 srcs: ["bin.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001166 }
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",
Inseob Kim67be7322020-10-19 10:15:28 +09001176 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001177 },
1178 },
1179 }
1180
1181 vendor_snapshot_shared {
1182 name: "libvendor",
1183 version: "BOARD",
1184 target_arch: "arm64",
1185 vendor: true,
1186 arch: {
1187 arm64: {
1188 src: "libvendor.so",
Inseob Kim67be7322020-10-19 10:15:28 +09001189 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001190 },
1191 },
1192 }
1193
1194 vendor_snapshot_static {
1195 name: "libvendor",
1196 version: "BOARD",
1197 target_arch: "arm64",
1198 vendor: true,
1199 arch: {
1200 arm64: {
1201 src: "libvendor.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001202 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001203 },
1204 },
1205 }
1206
1207 vendor_snapshot_binary {
1208 name: "bin",
1209 version: "BOARD",
1210 target_arch: "arm64",
1211 vendor: true,
1212 arch: {
1213 arm64: {
1214 src: "bin",
1215 },
1216 },
1217 }
1218`
1219 depsBp := GatherRequiredDepsForTest(android.Android)
1220
1221 mockFS := map[string][]byte{
Inseob Kim67be7322020-10-19 10:15:28 +09001222 "deps/Android.bp": []byte(depsBp),
1223 "framework/Android.bp": []byte(frameworkBp),
1224 "vendor/Android.bp": []byte(vendorProprietaryBp),
1225 "vendor/bin": nil,
1226 "vendor/bin.cpp": nil,
1227 "vendor/client.cpp": nil,
1228 "vendor/include/libvndk/a.h": nil,
1229 "vendor/include/libvendor/b.h": nil,
1230 "vendor/libvndk.a": nil,
1231 "vendor/libvendor.a": nil,
1232 "vendor/libvendor.so": nil,
1233 "vndk/Android.bp": []byte(vndkBp),
1234 "vndk/include/libvndk/a.h": nil,
1235 "vndk/libvndk.so": nil,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001236 }
1237
1238 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1239 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1240 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1241 ctx := CreateTestContext()
1242 ctx.Register(config)
1243
1244 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "vendor/Android.bp", "vndk/Android.bp"})
1245 android.FailIfErrored(t, errs)
1246 _, errs = ctx.PrepareBuildActions(config)
1247 android.FailIfErrored(t, errs)
1248
1249 sharedVariant := "android_vendor.BOARD_arm64_armv8-a_shared"
1250 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1251 binaryVariant := "android_vendor.BOARD_arm64_armv8-a"
1252
1253 // libclient uses libvndk.vndk.BOARD.arm64, libvendor.vendor_static.BOARD.arm64, libvendor_without_snapshot
Inseob Kim67be7322020-10-19 10:15:28 +09001254 libclientCcFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("cc").Args["cFlags"]
1255 for _, includeFlags := range []string{
1256 "-Ivndk/include/libvndk", // libvndk
1257 "-Ivendor/include/libvendor", // libvendor
1258 } {
1259 if !strings.Contains(libclientCcFlags, includeFlags) {
1260 t.Errorf("flags for libclient must contain %#v, but was %#v.",
1261 includeFlags, libclientCcFlags)
1262 }
1263 }
Inseob Kim5f58ff72020-09-07 19:53:31 +09001264
Inseob Kim67be7322020-10-19 10:15:28 +09001265 libclientLdFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001266 for _, input := range [][]string{
1267 []string{sharedVariant, "libvndk.vndk.BOARD.arm64"},
1268 []string{staticVariant, "libvendor.vendor_static.BOARD.arm64"},
1269 []string{staticVariant, "libvendor_without_snapshot"},
1270 } {
1271 outputPaths := getOutputPaths(ctx, input[0] /* variant */, []string{input[1]} /* module name */)
Inseob Kim67be7322020-10-19 10:15:28 +09001272 if !strings.Contains(libclientLdFlags, outputPaths[0].String()) {
1273 t.Errorf("libflags for libclient must contain %#v, but was %#v", outputPaths[0], libclientLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001274 }
1275 }
1276
1277 // bin_without_snapshot uses libvndk.vendor_static.BOARD.arm64
Inseob Kim67be7322020-10-19 10:15:28 +09001278 binWithoutSnapshotCcFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("cc").Args["cFlags"]
1279 if !strings.Contains(binWithoutSnapshotCcFlags, "-Ivendor/include/libvndk") {
1280 t.Errorf("flags for bin_without_snapshot must contain %#v, but was %#v.",
1281 "-Ivendor/include/libvndk", binWithoutSnapshotCcFlags)
1282 }
1283
1284 binWithoutSnapshotLdFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001285 libVndkStaticOutputPaths := getOutputPaths(ctx, staticVariant, []string{"libvndk.vendor_static.BOARD.arm64"})
Inseob Kim67be7322020-10-19 10:15:28 +09001286 if !strings.Contains(binWithoutSnapshotLdFlags, libVndkStaticOutputPaths[0].String()) {
Inseob Kim5f58ff72020-09-07 19:53:31 +09001287 t.Errorf("libflags for bin_without_snapshot must contain %#v, but was %#v",
Inseob Kim67be7322020-10-19 10:15:28 +09001288 libVndkStaticOutputPaths[0], binWithoutSnapshotLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001289 }
1290
1291 // libvendor.so is installed by libvendor.vendor_shared.BOARD.arm64
1292 ctx.ModuleForTests("libvendor.vendor_shared.BOARD.arm64", sharedVariant).Output("libvendor.so")
1293
1294 // libvendor_without_snapshot.so is installed by libvendor_without_snapshot
1295 ctx.ModuleForTests("libvendor_without_snapshot", sharedVariant).Output("libvendor_without_snapshot.so")
1296
1297 // bin is installed by bin.vendor_binary.BOARD.arm64
1298 ctx.ModuleForTests("bin.vendor_binary.BOARD.arm64", binaryVariant).Output("bin")
1299
1300 // bin_without_snapshot is installed by bin_without_snapshot
1301 ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Output("bin_without_snapshot")
1302
1303 // libvendor and bin don't have vendor.BOARD variant
1304 libvendorVariants := ctx.ModuleVariantsForTests("libvendor")
1305 if inList(sharedVariant, libvendorVariants) {
1306 t.Errorf("libvendor must not have variant %#v, but it does", sharedVariant)
1307 }
1308
1309 binVariants := ctx.ModuleVariantsForTests("bin")
1310 if inList(binaryVariant, binVariants) {
1311 t.Errorf("bin must not have variant %#v, but it does", sharedVariant)
1312 }
1313}
1314
Inseob Kimc42f2f22020-07-29 20:32:10 +09001315func TestVendorSnapshotSanitizer(t *testing.T) {
1316 bp := `
1317 vendor_snapshot_static {
1318 name: "libsnapshot",
1319 vendor: true,
1320 target_arch: "arm64",
1321 version: "BOARD",
1322 arch: {
1323 arm64: {
1324 src: "libsnapshot.a",
1325 cfi: {
1326 src: "libsnapshot.cfi.a",
1327 }
1328 },
1329 },
1330 }
1331`
1332 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1333 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1334 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1335 ctx := testCcWithConfig(t, config)
1336
1337 // Check non-cfi and cfi variant.
1338 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1339 staticCfiVariant := "android_vendor.BOARD_arm64_armv8-a_static_cfi"
1340
1341 staticModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticVariant).Module().(*Module)
1342 assertString(t, staticModule.outputFile.Path().Base(), "libsnapshot.a")
1343
1344 staticCfiModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticCfiVariant).Module().(*Module)
1345 assertString(t, staticCfiModule.outputFile.Path().Base(), "libsnapshot.cfi.a")
1346}
1347
Bill Peckham945441c2020-08-31 16:07:58 -07001348func assertExcludeFromVendorSnapshotIs(t *testing.T, c *Module, expected bool) {
1349 t.Helper()
1350 if c.ExcludeFromVendorSnapshot() != expected {
1351 t.Errorf("expected %q ExcludeFromVendorSnapshot to be %t", c.String(), expected)
1352 }
1353}
1354
1355func TestVendorSnapshotExclude(t *testing.T) {
1356
1357 // This test verifies that the exclude_from_vendor_snapshot property
1358 // makes its way from the Android.bp source file into the module data
1359 // structure. It also verifies that modules are correctly included or
1360 // excluded in the vendor snapshot based on their path (framework or
1361 // vendor) and the exclude_from_vendor_snapshot property.
1362
1363 frameworkBp := `
1364 cc_library_shared {
1365 name: "libinclude",
1366 srcs: ["src/include.cpp"],
1367 vendor_available: true,
1368 }
1369 cc_library_shared {
1370 name: "libexclude",
1371 srcs: ["src/exclude.cpp"],
1372 vendor: true,
1373 exclude_from_vendor_snapshot: true,
1374 }
1375 `
1376
1377 vendorProprietaryBp := `
1378 cc_library_shared {
1379 name: "libvendor",
1380 srcs: ["vendor.cpp"],
1381 vendor: true,
1382 }
1383 `
1384
1385 depsBp := GatherRequiredDepsForTest(android.Android)
1386
1387 mockFS := map[string][]byte{
1388 "deps/Android.bp": []byte(depsBp),
1389 "framework/Android.bp": []byte(frameworkBp),
1390 "framework/include.cpp": nil,
1391 "framework/exclude.cpp": nil,
1392 "device/Android.bp": []byte(vendorProprietaryBp),
1393 "device/vendor.cpp": nil,
1394 }
1395
1396 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1397 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1398 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1399 ctx := CreateTestContext()
1400 ctx.Register(config)
1401
1402 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1403 android.FailIfErrored(t, errs)
1404 _, errs = ctx.PrepareBuildActions(config)
1405 android.FailIfErrored(t, errs)
1406
1407 // Test an include and exclude framework module.
1408 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1409 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", vendorVariant).Module().(*Module), false)
1410 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libexclude", vendorVariant).Module().(*Module), true)
1411
1412 // A vendor module is excluded, but by its path, not the
1413 // exclude_from_vendor_snapshot property.
1414 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libvendor", vendorVariant).Module().(*Module), false)
1415
1416 // Verify the content of the vendor snapshot.
1417
1418 snapshotDir := "vendor-snapshot"
1419 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1420 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1421
1422 var includeJsonFiles []string
1423 var excludeJsonFiles []string
1424
1425 for _, arch := range [][]string{
1426 []string{"arm64", "armv8-a"},
1427 []string{"arm", "armv7-a-neon"},
1428 } {
1429 archType := arch[0]
1430 archVariant := arch[1]
1431 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1432
1433 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1434 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1435
1436 // Included modules
1437 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1438 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1439
1440 // Excluded modules
1441 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1442 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1443 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1444 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1445 }
1446
1447 // Verify that each json file for an included module has a rule.
1448 for _, jsonFile := range includeJsonFiles {
1449 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1450 t.Errorf("include json file %q not found", jsonFile)
1451 }
1452 }
1453
1454 // Verify that each json file for an excluded module has no rule.
1455 for _, jsonFile := range excludeJsonFiles {
1456 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1457 t.Errorf("exclude json file %q found", jsonFile)
1458 }
1459 }
1460}
1461
1462func TestVendorSnapshotExcludeInVendorProprietaryPathErrors(t *testing.T) {
1463
1464 // This test verifies that using the exclude_from_vendor_snapshot
1465 // property on a module in a vendor proprietary path generates an
1466 // error. These modules are already excluded, so we prohibit using the
1467 // property in this way, which could add to confusion.
1468
1469 vendorProprietaryBp := `
1470 cc_library_shared {
1471 name: "libvendor",
1472 srcs: ["vendor.cpp"],
1473 vendor: true,
1474 exclude_from_vendor_snapshot: true,
1475 }
1476 `
1477
1478 depsBp := GatherRequiredDepsForTest(android.Android)
1479
1480 mockFS := map[string][]byte{
1481 "deps/Android.bp": []byte(depsBp),
1482 "device/Android.bp": []byte(vendorProprietaryBp),
1483 "device/vendor.cpp": nil,
1484 }
1485
1486 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1487 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1488 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1489 ctx := CreateTestContext()
1490 ctx.Register(config)
1491
1492 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "device/Android.bp"})
1493 android.FailIfErrored(t, errs)
1494
1495 _, errs = ctx.PrepareBuildActions(config)
1496 android.CheckErrorsAgainstExpectations(t, errs, []string{
1497 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1498 `module "libvendor\{.+,image:vendor.+,arch:arm_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1499 })
1500}
1501
1502func TestVendorSnapshotExcludeWithVendorAvailable(t *testing.T) {
1503
1504 // This test verifies that using the exclude_from_vendor_snapshot
1505 // property on a module that is vendor available generates an error. A
1506 // vendor available module must be captured in the vendor snapshot and
1507 // must not built from source when building the vendor image against
1508 // the vendor snapshot.
1509
1510 frameworkBp := `
1511 cc_library_shared {
1512 name: "libinclude",
1513 srcs: ["src/include.cpp"],
1514 vendor_available: true,
1515 exclude_from_vendor_snapshot: true,
1516 }
1517 `
1518
1519 depsBp := GatherRequiredDepsForTest(android.Android)
1520
1521 mockFS := map[string][]byte{
1522 "deps/Android.bp": []byte(depsBp),
1523 "framework/Android.bp": []byte(frameworkBp),
1524 "framework/include.cpp": nil,
1525 }
1526
1527 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1528 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1529 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1530 ctx := CreateTestContext()
1531 ctx.Register(config)
1532
1533 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp"})
1534 android.FailIfErrored(t, errs)
1535
1536 _, errs = ctx.PrepareBuildActions(config)
1537 android.CheckErrorsAgainstExpectations(t, errs, []string{
1538 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1539 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1540 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1541 `module "libinclude\{.+,image:vendor.+,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1542 })
1543}
1544
Jooyung Hana70f0672019-01-18 15:20:43 +09001545func TestDoubleLoadableDepError(t *testing.T) {
1546 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1547 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1548 cc_library {
1549 name: "libllndk",
1550 shared_libs: ["libnondoubleloadable"],
1551 }
1552
1553 llndk_library {
1554 name: "libllndk",
1555 symbol_file: "",
1556 }
1557
1558 cc_library {
1559 name: "libnondoubleloadable",
1560 vendor_available: true,
1561 vndk: {
1562 enabled: true,
1563 },
1564 }
1565 `)
1566
1567 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1568 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1569 cc_library {
1570 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001571 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001572 shared_libs: ["libnondoubleloadable"],
1573 }
1574
1575 llndk_library {
1576 name: "libllndk",
1577 symbol_file: "",
1578 }
1579
1580 cc_library {
1581 name: "libnondoubleloadable",
1582 vendor_available: true,
1583 }
1584 `)
1585
1586 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
1587 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1588 cc_library {
1589 name: "libdoubleloadable",
1590 vendor_available: true,
1591 double_loadable: true,
1592 shared_libs: ["libnondoubleloadable"],
1593 }
1594
1595 cc_library {
1596 name: "libnondoubleloadable",
1597 vendor_available: true,
1598 }
1599 `)
1600
1601 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
1602 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1603 cc_library {
1604 name: "libdoubleloadable",
1605 vendor_available: true,
1606 double_loadable: true,
1607 shared_libs: ["libnondoubleloadable"],
1608 }
1609
1610 cc_library {
1611 name: "libnondoubleloadable",
1612 vendor_available: true,
1613 vndk: {
1614 enabled: true,
1615 },
1616 }
1617 `)
1618
1619 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
1620 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1621 cc_library {
1622 name: "libdoubleloadable",
1623 vendor_available: true,
1624 vndk: {
1625 enabled: true,
1626 },
1627 double_loadable: true,
1628 shared_libs: ["libnondoubleloadable"],
1629 }
1630
1631 cc_library {
1632 name: "libnondoubleloadable",
1633 vendor_available: false,
1634 vndk: {
1635 enabled: true,
1636 },
1637 }
1638 `)
1639
1640 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1641 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1642 cc_library {
1643 name: "libllndk",
1644 shared_libs: ["libcoreonly"],
1645 }
1646
1647 llndk_library {
1648 name: "libllndk",
1649 symbol_file: "",
1650 }
1651
1652 cc_library {
1653 name: "libcoreonly",
1654 shared_libs: ["libvendoravailable"],
1655 }
1656
1657 // indirect dependency of LLNDK
1658 cc_library {
1659 name: "libvendoravailable",
1660 vendor_available: true,
1661 }
1662 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001663}
1664
Logan Chienf3511742017-10-31 18:04:35 +08001665func TestVndkExt(t *testing.T) {
1666 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001667 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001668 cc_library {
1669 name: "libvndk",
1670 vendor_available: true,
1671 vndk: {
1672 enabled: true,
1673 },
1674 nocrt: true,
1675 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001676 cc_library {
1677 name: "libvndk2",
1678 vendor_available: true,
1679 vndk: {
1680 enabled: true,
1681 },
1682 target: {
1683 vendor: {
1684 suffix: "-suffix",
1685 },
1686 },
1687 nocrt: true,
1688 }
Logan Chienf3511742017-10-31 18:04:35 +08001689
1690 cc_library {
1691 name: "libvndk_ext",
1692 vendor: true,
1693 vndk: {
1694 enabled: true,
1695 extends: "libvndk",
1696 },
1697 nocrt: true,
1698 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001699
1700 cc_library {
1701 name: "libvndk2_ext",
1702 vendor: true,
1703 vndk: {
1704 enabled: true,
1705 extends: "libvndk2",
1706 },
1707 nocrt: true,
1708 }
Logan Chienf3511742017-10-31 18:04:35 +08001709
Justin Yun0ecf0b22020-02-28 15:07:59 +09001710 cc_library {
1711 name: "libvndk_ext_product",
1712 product_specific: true,
1713 vndk: {
1714 enabled: true,
1715 extends: "libvndk",
1716 },
1717 nocrt: true,
1718 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001719
Justin Yun0ecf0b22020-02-28 15:07:59 +09001720 cc_library {
1721 name: "libvndk2_ext_product",
1722 product_specific: true,
1723 vndk: {
1724 enabled: true,
1725 extends: "libvndk2",
1726 },
1727 nocrt: true,
1728 }
1729 `
1730 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1731 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1732 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1733 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1734
1735 ctx := testCcWithConfig(t, config)
1736
1737 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1738 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1739
1740 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1741 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1742
1743 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1744 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001745}
1746
Logan Chiend3c59a22018-03-29 14:08:15 +08001747func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001748 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1749 ctx := testCcNoVndk(t, `
1750 cc_library {
1751 name: "libvndk",
1752 vendor_available: true,
1753 vndk: {
1754 enabled: true,
1755 },
1756 nocrt: true,
1757 }
1758
1759 cc_library {
1760 name: "libvndk_ext",
1761 vendor: true,
1762 vndk: {
1763 enabled: true,
1764 extends: "libvndk",
1765 },
1766 nocrt: true,
1767 }
1768 `)
1769
1770 // Ensures that the core variant of "libvndk_ext" can be found.
1771 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1772 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1773 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1774 }
1775}
1776
Justin Yun0ecf0b22020-02-28 15:07:59 +09001777func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1778 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
1779 ctx := testCc(t, `
1780 cc_library {
1781 name: "libvndk",
1782 vendor_available: true,
1783 vndk: {
1784 enabled: true,
1785 },
1786 nocrt: true,
1787 }
1788
1789 cc_library {
1790 name: "libvndk_ext_product",
1791 product_specific: true,
1792 vndk: {
1793 enabled: true,
1794 extends: "libvndk",
1795 },
1796 nocrt: true,
1797 }
1798 `)
1799
1800 // Ensures that the core variant of "libvndk_ext_product" can be found.
1801 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1802 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1803 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1804 }
1805}
1806
Logan Chienf3511742017-10-31 18:04:35 +08001807func TestVndkExtError(t *testing.T) {
1808 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001809 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001810 cc_library {
1811 name: "libvndk",
1812 vendor_available: true,
1813 vndk: {
1814 enabled: true,
1815 },
1816 nocrt: true,
1817 }
1818
1819 cc_library {
1820 name: "libvndk_ext",
1821 vndk: {
1822 enabled: true,
1823 extends: "libvndk",
1824 },
1825 nocrt: true,
1826 }
1827 `)
1828
1829 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1830 cc_library {
1831 name: "libvndk",
1832 vendor_available: true,
1833 vndk: {
1834 enabled: true,
1835 },
1836 nocrt: true,
1837 }
1838
1839 cc_library {
1840 name: "libvndk_ext",
1841 vendor: true,
1842 vndk: {
1843 enabled: true,
1844 },
1845 nocrt: true,
1846 }
1847 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001848
1849 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1850 cc_library {
1851 name: "libvndk",
1852 vendor_available: true,
1853 vndk: {
1854 enabled: true,
1855 },
1856 nocrt: true,
1857 }
1858
1859 cc_library {
1860 name: "libvndk_ext_product",
1861 product_specific: true,
1862 vndk: {
1863 enabled: true,
1864 },
1865 nocrt: true,
1866 }
1867 `)
1868
1869 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1870 cc_library {
1871 name: "libvndk",
1872 vendor_available: true,
1873 vndk: {
1874 enabled: true,
1875 },
1876 nocrt: true,
1877 }
1878
1879 cc_library {
1880 name: "libvndk_ext_product",
1881 product_specific: true,
1882 vendor_available: true,
1883 vndk: {
1884 enabled: true,
1885 extends: "libvndk",
1886 },
1887 nocrt: true,
1888 }
1889 `)
Logan Chienf3511742017-10-31 18:04:35 +08001890}
1891
1892func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1893 // This test ensures an error is emitted for inconsistent support_system_process.
1894 testCcError(t, "module \".*\" with mismatched support_system_process", `
1895 cc_library {
1896 name: "libvndk",
1897 vendor_available: true,
1898 vndk: {
1899 enabled: true,
1900 },
1901 nocrt: true,
1902 }
1903
1904 cc_library {
1905 name: "libvndk_sp_ext",
1906 vendor: true,
1907 vndk: {
1908 enabled: true,
1909 extends: "libvndk",
1910 support_system_process: true,
1911 },
1912 nocrt: true,
1913 }
1914 `)
1915
1916 testCcError(t, "module \".*\" with mismatched support_system_process", `
1917 cc_library {
1918 name: "libvndk_sp",
1919 vendor_available: true,
1920 vndk: {
1921 enabled: true,
1922 support_system_process: true,
1923 },
1924 nocrt: true,
1925 }
1926
1927 cc_library {
1928 name: "libvndk_ext",
1929 vendor: true,
1930 vndk: {
1931 enabled: true,
1932 extends: "libvndk_sp",
1933 },
1934 nocrt: true,
1935 }
1936 `)
1937}
1938
1939func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001940 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08001941 // with `vendor_available: false`.
1942 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1943 cc_library {
1944 name: "libvndk",
1945 vendor_available: false,
1946 vndk: {
1947 enabled: true,
1948 },
1949 nocrt: true,
1950 }
1951
1952 cc_library {
1953 name: "libvndk_ext",
1954 vendor: true,
1955 vndk: {
1956 enabled: true,
1957 extends: "libvndk",
1958 },
1959 nocrt: true,
1960 }
1961 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001962
1963 testCcErrorProductVndk(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1964 cc_library {
1965 name: "libvndk",
1966 vendor_available: false,
1967 vndk: {
1968 enabled: true,
1969 },
1970 nocrt: true,
1971 }
1972
1973 cc_library {
1974 name: "libvndk_ext_product",
1975 product_specific: true,
1976 vndk: {
1977 enabled: true,
1978 extends: "libvndk",
1979 },
1980 nocrt: true,
1981 }
1982 `)
Logan Chienf3511742017-10-31 18:04:35 +08001983}
1984
Logan Chiend3c59a22018-03-29 14:08:15 +08001985func TestVendorModuleUseVndkExt(t *testing.T) {
1986 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001987 testCc(t, `
1988 cc_library {
1989 name: "libvndk",
1990 vendor_available: true,
1991 vndk: {
1992 enabled: true,
1993 },
1994 nocrt: true,
1995 }
1996
1997 cc_library {
1998 name: "libvndk_ext",
1999 vendor: true,
2000 vndk: {
2001 enabled: true,
2002 extends: "libvndk",
2003 },
2004 nocrt: true,
2005 }
2006
2007 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08002008 name: "libvndk_sp",
2009 vendor_available: true,
2010 vndk: {
2011 enabled: true,
2012 support_system_process: true,
2013 },
2014 nocrt: true,
2015 }
2016
2017 cc_library {
2018 name: "libvndk_sp_ext",
2019 vendor: true,
2020 vndk: {
2021 enabled: true,
2022 extends: "libvndk_sp",
2023 support_system_process: true,
2024 },
2025 nocrt: true,
2026 }
2027
2028 cc_library {
2029 name: "libvendor",
2030 vendor: true,
2031 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
2032 nocrt: true,
2033 }
2034 `)
2035}
2036
Logan Chiend3c59a22018-03-29 14:08:15 +08002037func TestVndkExtUseVendorLib(t *testing.T) {
2038 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08002039 testCc(t, `
2040 cc_library {
2041 name: "libvndk",
2042 vendor_available: true,
2043 vndk: {
2044 enabled: true,
2045 },
2046 nocrt: true,
2047 }
2048
2049 cc_library {
2050 name: "libvndk_ext",
2051 vendor: true,
2052 vndk: {
2053 enabled: true,
2054 extends: "libvndk",
2055 },
2056 shared_libs: ["libvendor"],
2057 nocrt: true,
2058 }
2059
2060 cc_library {
2061 name: "libvendor",
2062 vendor: true,
2063 nocrt: true,
2064 }
2065 `)
Logan Chienf3511742017-10-31 18:04:35 +08002066
Logan Chiend3c59a22018-03-29 14:08:15 +08002067 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
2068 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08002069 cc_library {
2070 name: "libvndk_sp",
2071 vendor_available: true,
2072 vndk: {
2073 enabled: true,
2074 support_system_process: true,
2075 },
2076 nocrt: true,
2077 }
2078
2079 cc_library {
2080 name: "libvndk_sp_ext",
2081 vendor: true,
2082 vndk: {
2083 enabled: true,
2084 extends: "libvndk_sp",
2085 support_system_process: true,
2086 },
2087 shared_libs: ["libvendor"], // Cause an error
2088 nocrt: true,
2089 }
2090
2091 cc_library {
2092 name: "libvendor",
2093 vendor: true,
2094 nocrt: true,
2095 }
2096 `)
2097}
2098
Justin Yun0ecf0b22020-02-28 15:07:59 +09002099func TestProductVndkExtDependency(t *testing.T) {
2100 bp := `
2101 cc_library {
2102 name: "libvndk",
2103 vendor_available: true,
2104 vndk: {
2105 enabled: true,
2106 },
2107 nocrt: true,
2108 }
2109
2110 cc_library {
2111 name: "libvndk_ext_product",
2112 product_specific: true,
2113 vndk: {
2114 enabled: true,
2115 extends: "libvndk",
2116 },
2117 shared_libs: ["libproduct_for_vndklibs"],
2118 nocrt: true,
2119 }
2120
2121 cc_library {
2122 name: "libvndk_sp",
2123 vendor_available: true,
2124 vndk: {
2125 enabled: true,
2126 support_system_process: true,
2127 },
2128 nocrt: true,
2129 }
2130
2131 cc_library {
2132 name: "libvndk_sp_ext_product",
2133 product_specific: true,
2134 vndk: {
2135 enabled: true,
2136 extends: "libvndk_sp",
2137 support_system_process: true,
2138 },
2139 shared_libs: ["libproduct_for_vndklibs"],
2140 nocrt: true,
2141 }
2142
2143 cc_library {
2144 name: "libproduct",
2145 product_specific: true,
2146 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
2147 nocrt: true,
2148 }
2149
2150 cc_library {
2151 name: "libproduct_for_vndklibs",
2152 product_specific: true,
2153 nocrt: true,
2154 }
2155 `
2156 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2157 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2158 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2159 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2160
2161 testCcWithConfig(t, config)
2162}
2163
Logan Chiend3c59a22018-03-29 14:08:15 +08002164func TestVndkSpExtUseVndkError(t *testing.T) {
2165 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
2166 // library.
2167 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2168 cc_library {
2169 name: "libvndk",
2170 vendor_available: true,
2171 vndk: {
2172 enabled: true,
2173 },
2174 nocrt: true,
2175 }
2176
2177 cc_library {
2178 name: "libvndk_sp",
2179 vendor_available: true,
2180 vndk: {
2181 enabled: true,
2182 support_system_process: true,
2183 },
2184 nocrt: true,
2185 }
2186
2187 cc_library {
2188 name: "libvndk_sp_ext",
2189 vendor: true,
2190 vndk: {
2191 enabled: true,
2192 extends: "libvndk_sp",
2193 support_system_process: true,
2194 },
2195 shared_libs: ["libvndk"], // Cause an error
2196 nocrt: true,
2197 }
2198 `)
2199
2200 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
2201 // library.
2202 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2203 cc_library {
2204 name: "libvndk",
2205 vendor_available: true,
2206 vndk: {
2207 enabled: true,
2208 },
2209 nocrt: true,
2210 }
2211
2212 cc_library {
2213 name: "libvndk_ext",
2214 vendor: true,
2215 vndk: {
2216 enabled: true,
2217 extends: "libvndk",
2218 },
2219 nocrt: true,
2220 }
2221
2222 cc_library {
2223 name: "libvndk_sp",
2224 vendor_available: true,
2225 vndk: {
2226 enabled: true,
2227 support_system_process: true,
2228 },
2229 nocrt: true,
2230 }
2231
2232 cc_library {
2233 name: "libvndk_sp_ext",
2234 vendor: true,
2235 vndk: {
2236 enabled: true,
2237 extends: "libvndk_sp",
2238 support_system_process: true,
2239 },
2240 shared_libs: ["libvndk_ext"], // Cause an error
2241 nocrt: true,
2242 }
2243 `)
2244}
2245
2246func TestVndkUseVndkExtError(t *testing.T) {
2247 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2248 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002249 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2250 cc_library {
2251 name: "libvndk",
2252 vendor_available: true,
2253 vndk: {
2254 enabled: true,
2255 },
2256 nocrt: true,
2257 }
2258
2259 cc_library {
2260 name: "libvndk_ext",
2261 vendor: true,
2262 vndk: {
2263 enabled: true,
2264 extends: "libvndk",
2265 },
2266 nocrt: true,
2267 }
2268
2269 cc_library {
2270 name: "libvndk2",
2271 vendor_available: true,
2272 vndk: {
2273 enabled: true,
2274 },
2275 shared_libs: ["libvndk_ext"],
2276 nocrt: true,
2277 }
2278 `)
2279
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002280 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002281 cc_library {
2282 name: "libvndk",
2283 vendor_available: true,
2284 vndk: {
2285 enabled: true,
2286 },
2287 nocrt: true,
2288 }
2289
2290 cc_library {
2291 name: "libvndk_ext",
2292 vendor: true,
2293 vndk: {
2294 enabled: true,
2295 extends: "libvndk",
2296 },
2297 nocrt: true,
2298 }
2299
2300 cc_library {
2301 name: "libvndk2",
2302 vendor_available: true,
2303 vndk: {
2304 enabled: true,
2305 },
2306 target: {
2307 vendor: {
2308 shared_libs: ["libvndk_ext"],
2309 },
2310 },
2311 nocrt: true,
2312 }
2313 `)
2314
2315 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2316 cc_library {
2317 name: "libvndk_sp",
2318 vendor_available: true,
2319 vndk: {
2320 enabled: true,
2321 support_system_process: true,
2322 },
2323 nocrt: true,
2324 }
2325
2326 cc_library {
2327 name: "libvndk_sp_ext",
2328 vendor: true,
2329 vndk: {
2330 enabled: true,
2331 extends: "libvndk_sp",
2332 support_system_process: true,
2333 },
2334 nocrt: true,
2335 }
2336
2337 cc_library {
2338 name: "libvndk_sp_2",
2339 vendor_available: true,
2340 vndk: {
2341 enabled: true,
2342 support_system_process: true,
2343 },
2344 shared_libs: ["libvndk_sp_ext"],
2345 nocrt: true,
2346 }
2347 `)
2348
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002349 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002350 cc_library {
2351 name: "libvndk_sp",
2352 vendor_available: true,
2353 vndk: {
2354 enabled: true,
2355 },
2356 nocrt: true,
2357 }
2358
2359 cc_library {
2360 name: "libvndk_sp_ext",
2361 vendor: true,
2362 vndk: {
2363 enabled: true,
2364 extends: "libvndk_sp",
2365 },
2366 nocrt: true,
2367 }
2368
2369 cc_library {
2370 name: "libvndk_sp2",
2371 vendor_available: true,
2372 vndk: {
2373 enabled: true,
2374 },
2375 target: {
2376 vendor: {
2377 shared_libs: ["libvndk_sp_ext"],
2378 },
2379 },
2380 nocrt: true,
2381 }
2382 `)
2383}
2384
Justin Yun5f7f7e82019-11-18 19:52:14 +09002385func TestEnforceProductVndkVersion(t *testing.T) {
2386 bp := `
2387 cc_library {
2388 name: "libllndk",
2389 }
2390 llndk_library {
2391 name: "libllndk",
2392 symbol_file: "",
2393 }
2394 cc_library {
2395 name: "libvndk",
2396 vendor_available: true,
2397 vndk: {
2398 enabled: true,
2399 },
2400 nocrt: true,
2401 }
2402 cc_library {
2403 name: "libvndk_sp",
2404 vendor_available: true,
2405 vndk: {
2406 enabled: true,
2407 support_system_process: true,
2408 },
2409 nocrt: true,
2410 }
2411 cc_library {
2412 name: "libva",
2413 vendor_available: true,
2414 nocrt: true,
2415 }
2416 cc_library {
2417 name: "libproduct_va",
2418 product_specific: true,
2419 vendor_available: true,
2420 nocrt: true,
2421 }
2422 cc_library {
2423 name: "libprod",
2424 product_specific: true,
2425 shared_libs: [
2426 "libllndk",
2427 "libvndk",
2428 "libvndk_sp",
2429 "libva",
2430 "libproduct_va",
2431 ],
2432 nocrt: true,
2433 }
2434 cc_library {
2435 name: "libvendor",
2436 vendor: true,
2437 shared_libs: [
2438 "libllndk",
2439 "libvndk",
2440 "libvndk_sp",
2441 "libva",
2442 "libproduct_va",
2443 ],
2444 nocrt: true,
2445 }
2446 `
2447
2448 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2449 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2450 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2451 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2452
2453 ctx := testCcWithConfig(t, config)
2454
Justin Yun0ecf0b22020-02-28 15:07:59 +09002455 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "", productVariant)
2456 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "", productVariant)
Justin Yun5f7f7e82019-11-18 19:52:14 +09002457}
2458
2459func TestEnforceProductVndkVersionErrors(t *testing.T) {
2460 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2461 cc_library {
2462 name: "libprod",
2463 product_specific: true,
2464 shared_libs: [
2465 "libvendor",
2466 ],
2467 nocrt: true,
2468 }
2469 cc_library {
2470 name: "libvendor",
2471 vendor: true,
2472 nocrt: true,
2473 }
2474 `)
2475 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2476 cc_library {
2477 name: "libprod",
2478 product_specific: true,
2479 shared_libs: [
2480 "libsystem",
2481 ],
2482 nocrt: true,
2483 }
2484 cc_library {
2485 name: "libsystem",
2486 nocrt: true,
2487 }
2488 `)
2489 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
2490 cc_library {
2491 name: "libprod",
2492 product_specific: true,
2493 shared_libs: [
2494 "libvndk_private",
2495 ],
2496 nocrt: true,
2497 }
2498 cc_library {
2499 name: "libvndk_private",
2500 vendor_available: false,
2501 vndk: {
2502 enabled: true,
2503 },
2504 nocrt: true,
2505 }
2506 `)
2507 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2508 cc_library {
2509 name: "libprod",
2510 product_specific: true,
2511 shared_libs: [
2512 "libsystem_ext",
2513 ],
2514 nocrt: true,
2515 }
2516 cc_library {
2517 name: "libsystem_ext",
2518 system_ext_specific: true,
2519 nocrt: true,
2520 }
2521 `)
2522 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2523 cc_library {
2524 name: "libsystem",
2525 shared_libs: [
2526 "libproduct_va",
2527 ],
2528 nocrt: true,
2529 }
2530 cc_library {
2531 name: "libproduct_va",
2532 product_specific: true,
2533 vendor_available: true,
2534 nocrt: true,
2535 }
2536 `)
2537}
2538
Jooyung Han38002912019-05-16 04:01:54 +09002539func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002540 bp := `
2541 cc_library {
2542 name: "libvndk",
2543 vendor_available: true,
2544 vndk: {
2545 enabled: true,
2546 },
2547 }
2548 cc_library {
2549 name: "libvndksp",
2550 vendor_available: true,
2551 vndk: {
2552 enabled: true,
2553 support_system_process: true,
2554 },
2555 }
2556 cc_library {
2557 name: "libvndkprivate",
2558 vendor_available: false,
2559 vndk: {
2560 enabled: true,
2561 },
2562 }
2563 cc_library {
2564 name: "libvendor",
2565 vendor: true,
2566 }
2567 cc_library {
2568 name: "libvndkext",
2569 vendor: true,
2570 vndk: {
2571 enabled: true,
2572 extends: "libvndk",
2573 },
2574 }
2575 vndk_prebuilt_shared {
2576 name: "prevndk",
2577 version: "27",
2578 target_arch: "arm",
2579 binder32bit: true,
2580 vendor_available: true,
2581 vndk: {
2582 enabled: true,
2583 },
2584 arch: {
2585 arm: {
2586 srcs: ["liba.so"],
2587 },
2588 },
2589 }
2590 cc_library {
2591 name: "libllndk",
2592 }
2593 llndk_library {
2594 name: "libllndk",
2595 symbol_file: "",
2596 }
2597 cc_library {
2598 name: "libllndkprivate",
2599 }
2600 llndk_library {
2601 name: "libllndkprivate",
2602 vendor_available: false,
2603 symbol_file: "",
2604 }`
2605
2606 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002607 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2608 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2609 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002610 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002611
Jooyung Han0302a842019-10-30 18:43:49 +09002612 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002613 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09002614 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002615 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09002616 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002617 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09002618 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002619 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09002620
Colin Crossfb0c16e2019-11-20 17:12:35 -08002621 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002622
Jooyung Han38002912019-05-16 04:01:54 +09002623 tests := []struct {
2624 variant string
2625 name string
2626 expected string
2627 }{
2628 {vendorVariant, "libvndk", "native:vndk"},
2629 {vendorVariant, "libvndksp", "native:vndk"},
2630 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2631 {vendorVariant, "libvendor", "native:vendor"},
2632 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09002633 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002634 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002635 {coreVariant, "libvndk", "native:platform"},
2636 {coreVariant, "libvndkprivate", "native:platform"},
2637 {coreVariant, "libllndk", "native:platform"},
2638 }
2639 for _, test := range tests {
2640 t.Run(test.name, func(t *testing.T) {
2641 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2642 assertString(t, module.makeLinkType, test.expected)
2643 })
2644 }
2645}
2646
Colin Cross0af4b842015-04-30 16:36:18 -07002647var (
2648 str11 = "01234567891"
2649 str10 = str11[:10]
2650 str9 = str11[:9]
2651 str5 = str11[:5]
2652 str4 = str11[:4]
2653)
2654
2655var splitListForSizeTestCases = []struct {
2656 in []string
2657 out [][]string
2658 size int
2659}{
2660 {
2661 in: []string{str10},
2662 out: [][]string{{str10}},
2663 size: 10,
2664 },
2665 {
2666 in: []string{str9},
2667 out: [][]string{{str9}},
2668 size: 10,
2669 },
2670 {
2671 in: []string{str5},
2672 out: [][]string{{str5}},
2673 size: 10,
2674 },
2675 {
2676 in: []string{str11},
2677 out: nil,
2678 size: 10,
2679 },
2680 {
2681 in: []string{str10, str10},
2682 out: [][]string{{str10}, {str10}},
2683 size: 10,
2684 },
2685 {
2686 in: []string{str9, str10},
2687 out: [][]string{{str9}, {str10}},
2688 size: 10,
2689 },
2690 {
2691 in: []string{str10, str9},
2692 out: [][]string{{str10}, {str9}},
2693 size: 10,
2694 },
2695 {
2696 in: []string{str5, str4},
2697 out: [][]string{{str5, str4}},
2698 size: 10,
2699 },
2700 {
2701 in: []string{str5, str4, str5},
2702 out: [][]string{{str5, str4}, {str5}},
2703 size: 10,
2704 },
2705 {
2706 in: []string{str5, str4, str5, str4},
2707 out: [][]string{{str5, str4}, {str5, str4}},
2708 size: 10,
2709 },
2710 {
2711 in: []string{str5, str4, str5, str5},
2712 out: [][]string{{str5, str4}, {str5}, {str5}},
2713 size: 10,
2714 },
2715 {
2716 in: []string{str5, str5, str5, str4},
2717 out: [][]string{{str5}, {str5}, {str5, str4}},
2718 size: 10,
2719 },
2720 {
2721 in: []string{str9, str11},
2722 out: nil,
2723 size: 10,
2724 },
2725 {
2726 in: []string{str11, str9},
2727 out: nil,
2728 size: 10,
2729 },
2730}
2731
2732func TestSplitListForSize(t *testing.T) {
2733 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08002734 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07002735
2736 var outStrings [][]string
2737
2738 if len(out) > 0 {
2739 outStrings = make([][]string, len(out))
2740 for i, o := range out {
2741 outStrings[i] = o.Strings()
2742 }
2743 }
2744
2745 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07002746 t.Errorf("incorrect output:")
2747 t.Errorf(" input: %#v", testCase.in)
2748 t.Errorf(" size: %d", testCase.size)
2749 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07002750 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07002751 }
2752 }
2753}
Jeff Gaston294356f2017-09-27 17:05:30 -07002754
2755var staticLinkDepOrderTestCases = []struct {
2756 // This is a string representation of a map[moduleName][]moduleDependency .
2757 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002758 inStatic string
2759
2760 // This is a string representation of a map[moduleName][]moduleDependency .
2761 // It models the dependencies declared in an Android.bp file.
2762 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002763
2764 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2765 // The keys of allOrdered specify which modules we would like to check.
2766 // The values of allOrdered specify the expected result (of the transitive closure of all
2767 // dependencies) for each module to test
2768 allOrdered string
2769
2770 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2771 // The keys of outOrdered specify which modules we would like to check.
2772 // The values of outOrdered specify the expected result (of the ordered linker command line)
2773 // for each module to test.
2774 outOrdered string
2775}{
2776 // Simple tests
2777 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002778 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002779 outOrdered: "",
2780 },
2781 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002782 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002783 outOrdered: "a:",
2784 },
2785 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002786 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002787 outOrdered: "a:b; b:",
2788 },
2789 // Tests of reordering
2790 {
2791 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002792 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002793 outOrdered: "a:b,c,d; b:d; c:d; d:",
2794 },
2795 {
2796 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002797 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002798 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2799 },
2800 {
2801 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002802 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002803 outOrdered: "a:d,b,e,c; d:b; e:c",
2804 },
2805 {
2806 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002807 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002808 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2809 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2810 },
2811 {
2812 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002813 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 -07002814 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2815 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2816 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002817 // shared dependencies
2818 {
2819 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2820 // So, we don't actually have to check that a shared dependency of c will change the order
2821 // of a library that depends statically on b and on c. We only need to check that if c has
2822 // a shared dependency on b, that that shows up in allOrdered.
2823 inShared: "c:b",
2824 allOrdered: "c:b",
2825 outOrdered: "c:",
2826 },
2827 {
2828 // This test doesn't actually include any shared dependencies but it's a reminder of what
2829 // the second phase of the above test would look like
2830 inStatic: "a:b,c; c:b",
2831 allOrdered: "a:c,b; c:b",
2832 outOrdered: "a:c,b; c:b",
2833 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002834 // tiebreakers for when two modules specifying different orderings and there is no dependency
2835 // to dictate an order
2836 {
2837 // 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 -08002838 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002839 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2840 },
2841 {
2842 // 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 -08002843 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 -07002844 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2845 },
2846 // Tests involving duplicate dependencies
2847 {
2848 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002849 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002850 outOrdered: "a:c,b",
2851 },
2852 {
2853 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002854 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002855 outOrdered: "a:d,c,b",
2856 },
2857 // Tests to confirm the nonexistence of infinite loops.
2858 // These cases should never happen, so as long as the test terminates and the
2859 // result is deterministic then that should be fine.
2860 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002861 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002862 outOrdered: "a:a",
2863 },
2864 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002865 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002866 allOrdered: "a:b,c; b:c,a; c:a,b",
2867 outOrdered: "a:b; b:c; c:a",
2868 },
2869 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002870 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002871 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2872 outOrdered: "a:c,b; b:a,c; c:b,a",
2873 },
2874}
2875
2876// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2877func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2878 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2879 strippedText := strings.Replace(text, " ", "", -1)
2880 if len(strippedText) < 1 {
2881 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2882 }
2883 allDeps = make(map[android.Path][]android.Path, 0)
2884
2885 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2886 moduleTexts := strings.Split(strippedText, ";")
2887
2888 outputForModuleName := func(moduleName string) android.Path {
2889 return android.PathForTesting(moduleName)
2890 }
2891
2892 for _, moduleText := range moduleTexts {
2893 // convert from "a:b,c" to ["a", "b,c"]
2894 components := strings.Split(moduleText, ":")
2895 if len(components) != 2 {
2896 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2897 }
2898 moduleName := components[0]
2899 moduleOutput := outputForModuleName(moduleName)
2900 modulesInOrder = append(modulesInOrder, moduleOutput)
2901
2902 depString := components[1]
2903 // convert from "b,c" to ["b", "c"]
2904 depNames := strings.Split(depString, ",")
2905 if len(depString) < 1 {
2906 depNames = []string{}
2907 }
2908 var deps []android.Path
2909 for _, depName := range depNames {
2910 deps = append(deps, outputForModuleName(depName))
2911 }
2912 allDeps[moduleOutput] = deps
2913 }
2914 return modulesInOrder, allDeps
2915}
2916
Jeff Gaston294356f2017-09-27 17:05:30 -07002917func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2918 for _, moduleName := range moduleNames {
2919 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
2920 output := module.outputFile.Path()
2921 paths = append(paths, output)
2922 }
2923 return paths
2924}
2925
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002926func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002927 ctx := testCc(t, `
2928 cc_library {
2929 name: "a",
2930 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002931 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002932 }
2933 cc_library {
2934 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002935 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002936 }
2937 cc_library {
2938 name: "c",
2939 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002940 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002941 }
2942 cc_library {
2943 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002944 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002945 }
2946
2947 `)
2948
Colin Cross7113d202019-11-20 16:39:12 -08002949 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002950 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07002951 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
2952 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002953
2954 if !reflect.DeepEqual(actual, expected) {
2955 t.Errorf("staticDeps orderings were not propagated correctly"+
2956 "\nactual: %v"+
2957 "\nexpected: %v",
2958 actual,
2959 expected,
2960 )
2961 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002962}
Jeff Gaston294356f2017-09-27 17:05:30 -07002963
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002964func TestStaticLibDepReorderingWithShared(t *testing.T) {
2965 ctx := testCc(t, `
2966 cc_library {
2967 name: "a",
2968 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002969 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002970 }
2971 cc_library {
2972 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002973 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002974 }
2975 cc_library {
2976 name: "c",
2977 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002978 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002979 }
2980
2981 `)
2982
Colin Cross7113d202019-11-20 16:39:12 -08002983 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002984 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07002985 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
2986 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002987
2988 if !reflect.DeepEqual(actual, expected) {
2989 t.Errorf("staticDeps orderings did not account for shared libs"+
2990 "\nactual: %v"+
2991 "\nexpected: %v",
2992 actual,
2993 expected,
2994 )
2995 }
2996}
2997
Jooyung Hanb04a4992020-03-13 18:57:35 +09002998func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002999 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003000 if !reflect.DeepEqual(actual, expected) {
3001 t.Errorf(message+
3002 "\nactual: %v"+
3003 "\nexpected: %v",
3004 actual,
3005 expected,
3006 )
3007 }
3008}
3009
Jooyung Han61b66e92020-03-21 14:21:46 +00003010func TestLlndkLibrary(t *testing.T) {
3011 ctx := testCc(t, `
3012 cc_library {
3013 name: "libllndk",
3014 stubs: { versions: ["1", "2"] },
3015 }
3016 llndk_library {
3017 name: "libllndk",
3018 }
3019 `)
3020 actual := ctx.ModuleVariantsForTests("libllndk.llndk")
3021 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00003022 "android_vendor.VER_arm64_armv8-a_shared_1",
3023 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003024 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003025 "android_vendor.VER_arm_armv7-a-neon_shared_1",
3026 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003027 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003028 }
3029 checkEquals(t, "variants for llndk stubs", expected, actual)
3030
3031 params := ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
3032 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
3033
3034 params = ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
3035 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
3036}
3037
Jiyong Parka46a4d52017-12-14 19:54:34 +09003038func TestLlndkHeaders(t *testing.T) {
3039 ctx := testCc(t, `
3040 llndk_headers {
3041 name: "libllndk_headers",
3042 export_include_dirs: ["my_include"],
3043 }
3044 llndk_library {
3045 name: "libllndk",
3046 export_llndk_headers: ["libllndk_headers"],
3047 }
3048 cc_library {
3049 name: "libvendor",
3050 shared_libs: ["libllndk"],
3051 vendor: true,
3052 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003053 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08003054 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09003055 }
3056 `)
3057
3058 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08003059 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09003060 cflags := cc.Args["cFlags"]
3061 if !strings.Contains(cflags, "-Imy_include") {
3062 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
3063 }
3064}
3065
Logan Chien43d34c32017-12-20 01:17:32 +08003066func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
3067 actual := module.Properties.AndroidMkRuntimeLibs
3068 if !reflect.DeepEqual(actual, expected) {
3069 t.Errorf("incorrect runtime_libs for shared libs"+
3070 "\nactual: %v"+
3071 "\nexpected: %v",
3072 actual,
3073 expected,
3074 )
3075 }
3076}
3077
3078const runtimeLibAndroidBp = `
3079 cc_library {
3080 name: "libvendor_available1",
3081 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003082 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003083 nocrt : true,
3084 system_shared_libs : [],
3085 }
3086 cc_library {
3087 name: "libvendor_available2",
3088 vendor_available: true,
3089 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003090 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003091 nocrt : true,
3092 system_shared_libs : [],
3093 }
3094 cc_library {
3095 name: "libvendor_available3",
3096 vendor_available: true,
3097 runtime_libs: ["libvendor_available1"],
3098 target: {
3099 vendor: {
3100 exclude_runtime_libs: ["libvendor_available1"],
3101 }
3102 },
Yi Konge7fe9912019-06-02 00:53:50 -07003103 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003104 nocrt : true,
3105 system_shared_libs : [],
3106 }
3107 cc_library {
3108 name: "libcore",
3109 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003110 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003111 nocrt : true,
3112 system_shared_libs : [],
3113 }
3114 cc_library {
3115 name: "libvendor1",
3116 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003117 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003118 nocrt : true,
3119 system_shared_libs : [],
3120 }
3121 cc_library {
3122 name: "libvendor2",
3123 vendor: true,
3124 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003125 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003126 nocrt : true,
3127 system_shared_libs : [],
3128 }
3129`
3130
3131func TestRuntimeLibs(t *testing.T) {
3132 ctx := testCc(t, runtimeLibAndroidBp)
3133
3134 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08003135 variant := "android_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"}, module)
3139
3140 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
3141 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3142
3143 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3144 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08003145 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003146
3147 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3148 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
3149
3150 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
3151 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
3152}
3153
3154func TestExcludeRuntimeLibs(t *testing.T) {
3155 ctx := testCc(t, runtimeLibAndroidBp)
3156
Colin Cross7113d202019-11-20 16:39:12 -08003157 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003158 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
3159 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3160
Colin Crossfb0c16e2019-11-20 17:12:35 -08003161 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003162 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
3163 checkRuntimeLibs(t, nil, module)
3164}
3165
3166func TestRuntimeLibsNoVndk(t *testing.T) {
3167 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3168
3169 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3170
Colin Cross7113d202019-11-20 16:39:12 -08003171 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003172
3173 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3174 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3175
3176 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
3177 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
3178}
3179
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003180func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003181 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003182 actual := module.Properties.AndroidMkStaticLibs
3183 if !reflect.DeepEqual(actual, expected) {
3184 t.Errorf("incorrect static_libs"+
3185 "\nactual: %v"+
3186 "\nexpected: %v",
3187 actual,
3188 expected,
3189 )
3190 }
3191}
3192
3193const staticLibAndroidBp = `
3194 cc_library {
3195 name: "lib1",
3196 }
3197 cc_library {
3198 name: "lib2",
3199 static_libs: ["lib1"],
3200 }
3201`
3202
3203func TestStaticLibDepExport(t *testing.T) {
3204 ctx := testCc(t, staticLibAndroidBp)
3205
3206 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003207 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003208 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003209 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003210
3211 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003212 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003213 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3214 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003215 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003216}
3217
Jiyong Parkd08b6972017-09-26 10:50:54 +09003218var compilerFlagsTestCases = []struct {
3219 in string
3220 out bool
3221}{
3222 {
3223 in: "a",
3224 out: false,
3225 },
3226 {
3227 in: "-a",
3228 out: true,
3229 },
3230 {
3231 in: "-Ipath/to/something",
3232 out: false,
3233 },
3234 {
3235 in: "-isystempath/to/something",
3236 out: false,
3237 },
3238 {
3239 in: "--coverage",
3240 out: false,
3241 },
3242 {
3243 in: "-include a/b",
3244 out: true,
3245 },
3246 {
3247 in: "-include a/b c/d",
3248 out: false,
3249 },
3250 {
3251 in: "-DMACRO",
3252 out: true,
3253 },
3254 {
3255 in: "-DMAC RO",
3256 out: false,
3257 },
3258 {
3259 in: "-a -b",
3260 out: false,
3261 },
3262 {
3263 in: "-DMACRO=definition",
3264 out: true,
3265 },
3266 {
3267 in: "-DMACRO=defi nition",
3268 out: true, // TODO(jiyong): this should be false
3269 },
3270 {
3271 in: "-DMACRO(x)=x + 1",
3272 out: true,
3273 },
3274 {
3275 in: "-DMACRO=\"defi nition\"",
3276 out: true,
3277 },
3278}
3279
3280type mockContext struct {
3281 BaseModuleContext
3282 result bool
3283}
3284
3285func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3286 // CheckBadCompilerFlags calls this function when the flag should be rejected
3287 ctx.result = false
3288}
3289
3290func TestCompilerFlags(t *testing.T) {
3291 for _, testCase := range compilerFlagsTestCases {
3292 ctx := &mockContext{result: true}
3293 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3294 if ctx.result != testCase.out {
3295 t.Errorf("incorrect output:")
3296 t.Errorf(" input: %#v", testCase.in)
3297 t.Errorf(" expected: %#v", testCase.out)
3298 t.Errorf(" got: %#v", ctx.result)
3299 }
3300 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003301}
Jiyong Park374510b2018-03-19 18:23:01 +09003302
3303func TestVendorPublicLibraries(t *testing.T) {
3304 ctx := testCc(t, `
3305 cc_library_headers {
3306 name: "libvendorpublic_headers",
3307 export_include_dirs: ["my_include"],
3308 }
3309 vendor_public_library {
3310 name: "libvendorpublic",
3311 symbol_file: "",
3312 export_public_headers: ["libvendorpublic_headers"],
3313 }
3314 cc_library {
3315 name: "libvendorpublic",
3316 srcs: ["foo.c"],
3317 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003318 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003319 nocrt: true,
3320 }
3321
3322 cc_library {
3323 name: "libsystem",
3324 shared_libs: ["libvendorpublic"],
3325 vendor: false,
3326 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003327 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003328 nocrt: true,
3329 }
3330 cc_library {
3331 name: "libvendor",
3332 shared_libs: ["libvendorpublic"],
3333 vendor: true,
3334 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003335 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003336 nocrt: true,
3337 }
3338 `)
3339
Colin Cross7113d202019-11-20 16:39:12 -08003340 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003341 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003342
3343 // test if header search paths are correctly added
3344 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003345 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003346 cflags := cc.Args["cFlags"]
3347 if !strings.Contains(cflags, "-Imy_include") {
3348 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3349 }
3350
3351 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003352 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003353 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003354 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003355 if !strings.Contains(libflags, stubPaths[0].String()) {
3356 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3357 }
3358
3359 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003360 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003361 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003362 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003363 if !strings.Contains(libflags, stubPaths[0].String()) {
3364 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3365 }
3366
3367}
Jiyong Park37b25202018-07-11 10:49:27 +09003368
3369func TestRecovery(t *testing.T) {
3370 ctx := testCc(t, `
3371 cc_library_shared {
3372 name: "librecovery",
3373 recovery: true,
3374 }
3375 cc_library_shared {
3376 name: "librecovery32",
3377 recovery: true,
3378 compile_multilib:"32",
3379 }
Jiyong Park5baac542018-08-28 09:55:37 +09003380 cc_library_shared {
3381 name: "libHalInRecovery",
3382 recovery_available: true,
3383 vendor: true,
3384 }
Jiyong Park37b25202018-07-11 10:49:27 +09003385 `)
3386
3387 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003388 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003389 if len(variants) != 1 || !android.InList(arm64, variants) {
3390 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3391 }
3392
3393 variants = ctx.ModuleVariantsForTests("librecovery32")
3394 if android.InList(arm64, variants) {
3395 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3396 }
Jiyong Park5baac542018-08-28 09:55:37 +09003397
3398 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3399 if !recoveryModule.Platform() {
3400 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3401 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003402}
Jiyong Park5baac542018-08-28 09:55:37 +09003403
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003404func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3405 bp := `
3406 cc_prebuilt_test_library_shared {
3407 name: "test_lib",
3408 relative_install_path: "foo/bar/baz",
3409 srcs: ["srcpath/dontusethispath/baz.so"],
3410 }
3411
3412 cc_test {
3413 name: "main_test",
3414 data_libs: ["test_lib"],
3415 gtest: false,
3416 }
3417 `
3418
3419 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3420 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3421 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3422 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3423
3424 ctx := testCcWithConfig(t, config)
3425 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3426 testBinary := module.(*Module).linker.(*testBinary)
3427 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3428 if err != nil {
3429 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3430 }
3431 if len(outputFiles) != 1 {
3432 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3433 }
3434 if len(testBinary.dataPaths()) != 1 {
3435 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3436 }
3437
3438 outputPath := outputFiles[0].String()
3439
3440 if !strings.HasSuffix(outputPath, "/main_test") {
3441 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3442 }
3443 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
3444 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3445 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3446 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3447 }
3448}
3449
Jiyong Park7ed9de32018-10-15 22:25:07 +09003450func TestVersionedStubs(t *testing.T) {
3451 ctx := testCc(t, `
3452 cc_library_shared {
3453 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003454 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003455 stubs: {
3456 symbol_file: "foo.map.txt",
3457 versions: ["1", "2", "3"],
3458 },
3459 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003460
Jiyong Park7ed9de32018-10-15 22:25:07 +09003461 cc_library_shared {
3462 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003463 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003464 shared_libs: ["libFoo#1"],
3465 }`)
3466
3467 variants := ctx.ModuleVariantsForTests("libFoo")
3468 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003469 "android_arm64_armv8-a_shared",
3470 "android_arm64_armv8-a_shared_1",
3471 "android_arm64_armv8-a_shared_2",
3472 "android_arm64_armv8-a_shared_3",
3473 "android_arm_armv7-a-neon_shared",
3474 "android_arm_armv7-a-neon_shared_1",
3475 "android_arm_armv7-a-neon_shared_2",
3476 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003477 }
3478 variantsMismatch := false
3479 if len(variants) != len(expectedVariants) {
3480 variantsMismatch = true
3481 } else {
3482 for _, v := range expectedVariants {
3483 if !inList(v, variants) {
3484 variantsMismatch = false
3485 }
3486 }
3487 }
3488 if variantsMismatch {
3489 t.Errorf("variants of libFoo expected:\n")
3490 for _, v := range expectedVariants {
3491 t.Errorf("%q\n", v)
3492 }
3493 t.Errorf(", but got:\n")
3494 for _, v := range variants {
3495 t.Errorf("%q\n", v)
3496 }
3497 }
3498
Colin Cross7113d202019-11-20 16:39:12 -08003499 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003500 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003501 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003502 if !strings.Contains(libFlags, libFoo1StubPath) {
3503 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3504 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003505
Colin Cross7113d202019-11-20 16:39:12 -08003506 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003507 cFlags := libBarCompileRule.Args["cFlags"]
3508 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3509 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3510 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3511 }
Jiyong Park37b25202018-07-11 10:49:27 +09003512}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003513
Jooyung Hanb04a4992020-03-13 18:57:35 +09003514func TestVersioningMacro(t *testing.T) {
3515 for _, tc := range []struct{ moduleName, expected string }{
3516 {"libc", "__LIBC_API__"},
3517 {"libfoo", "__LIBFOO_API__"},
3518 {"libfoo@1", "__LIBFOO_1_API__"},
3519 {"libfoo-v1", "__LIBFOO_V1_API__"},
3520 {"libfoo.v1", "__LIBFOO_V1_API__"},
3521 } {
3522 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3523 }
3524}
3525
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003526func TestStaticExecutable(t *testing.T) {
3527 ctx := testCc(t, `
3528 cc_binary {
3529 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003530 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003531 static_executable: true,
3532 }`)
3533
Colin Cross7113d202019-11-20 16:39:12 -08003534 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003535 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3536 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003537 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003538 for _, lib := range systemStaticLibs {
3539 if !strings.Contains(libFlags, lib) {
3540 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3541 }
3542 }
3543 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3544 for _, lib := range systemSharedLibs {
3545 if strings.Contains(libFlags, lib) {
3546 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3547 }
3548 }
3549}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003550
3551func TestStaticDepsOrderWithStubs(t *testing.T) {
3552 ctx := testCc(t, `
3553 cc_binary {
3554 name: "mybin",
3555 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003556 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003557 static_executable: true,
3558 stl: "none",
3559 }
3560
3561 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003562 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003563 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003564 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003565 stl: "none",
3566 }
3567
3568 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003569 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003570 srcs: ["foo.c"],
3571 stl: "none",
3572 stubs: {
3573 versions: ["1"],
3574 },
3575 }`)
3576
Colin Cross0de8a1e2020-09-18 14:15:30 -07003577 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3578 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003579 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003580
3581 if !reflect.DeepEqual(actual, expected) {
3582 t.Errorf("staticDeps orderings were not propagated correctly"+
3583 "\nactual: %v"+
3584 "\nexpected: %v",
3585 actual,
3586 expected,
3587 )
3588 }
3589}
Jooyung Han38002912019-05-16 04:01:54 +09003590
Jooyung Hand48f3c32019-08-23 11:18:57 +09003591func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3592 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3593 cc_library {
3594 name: "libA",
3595 srcs: ["foo.c"],
3596 shared_libs: ["libB"],
3597 stl: "none",
3598 }
3599
3600 cc_library {
3601 name: "libB",
3602 srcs: ["foo.c"],
3603 enabled: false,
3604 stl: "none",
3605 }
3606 `)
3607}
3608
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003609// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3610// correctly.
3611func TestFuzzTarget(t *testing.T) {
3612 ctx := testCc(t, `
3613 cc_fuzz {
3614 name: "fuzz_smoke_test",
3615 srcs: ["foo.c"],
3616 }`)
3617
Paul Duffin075c4172019-12-19 19:06:13 +00003618 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003619 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3620}
3621
Jiyong Park29074592019-07-07 16:27:47 +09003622func TestAidl(t *testing.T) {
3623}
3624
Jooyung Han38002912019-05-16 04:01:54 +09003625func assertString(t *testing.T, got, expected string) {
3626 t.Helper()
3627 if got != expected {
3628 t.Errorf("expected %q got %q", expected, got)
3629 }
3630}
3631
3632func assertArrayString(t *testing.T, got, expected []string) {
3633 t.Helper()
3634 if len(got) != len(expected) {
3635 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3636 return
3637 }
3638 for i := range got {
3639 if got[i] != expected[i] {
3640 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3641 i, expected[i], expected, got[i], got)
3642 return
3643 }
3644 }
3645}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003646
Jooyung Han0302a842019-10-30 18:43:49 +09003647func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3648 t.Helper()
3649 assertArrayString(t, android.SortedStringKeys(m), expected)
3650}
3651
Colin Crosse1bb5d02019-09-24 14:55:04 -07003652func TestDefaults(t *testing.T) {
3653 ctx := testCc(t, `
3654 cc_defaults {
3655 name: "defaults",
3656 srcs: ["foo.c"],
3657 static: {
3658 srcs: ["bar.c"],
3659 },
3660 shared: {
3661 srcs: ["baz.c"],
3662 },
3663 }
3664
3665 cc_library_static {
3666 name: "libstatic",
3667 defaults: ["defaults"],
3668 }
3669
3670 cc_library_shared {
3671 name: "libshared",
3672 defaults: ["defaults"],
3673 }
3674
3675 cc_library {
3676 name: "libboth",
3677 defaults: ["defaults"],
3678 }
3679
3680 cc_binary {
3681 name: "binary",
3682 defaults: ["defaults"],
3683 }`)
3684
3685 pathsToBase := func(paths android.Paths) []string {
3686 var ret []string
3687 for _, p := range paths {
3688 ret = append(ret, p.Base())
3689 }
3690 return ret
3691 }
3692
Colin Cross7113d202019-11-20 16:39:12 -08003693 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003694 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3695 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3696 }
Colin Cross7113d202019-11-20 16:39:12 -08003697 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003698 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3699 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3700 }
Colin Cross7113d202019-11-20 16:39:12 -08003701 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003702 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3703 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3704 }
3705
Colin Cross7113d202019-11-20 16:39:12 -08003706 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003707 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3708 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3709 }
Colin Cross7113d202019-11-20 16:39:12 -08003710 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003711 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3712 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3713 }
3714}
Colin Crosseabaedd2020-02-06 17:01:55 -08003715
3716func TestProductVariableDefaults(t *testing.T) {
3717 bp := `
3718 cc_defaults {
3719 name: "libfoo_defaults",
3720 srcs: ["foo.c"],
3721 cppflags: ["-DFOO"],
3722 product_variables: {
3723 debuggable: {
3724 cppflags: ["-DBAR"],
3725 },
3726 },
3727 }
3728
3729 cc_library {
3730 name: "libfoo",
3731 defaults: ["libfoo_defaults"],
3732 }
3733 `
3734
3735 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3736 config.TestProductVariables.Debuggable = BoolPtr(true)
3737
3738 ctx := CreateTestContext()
3739 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
3740 ctx.BottomUp("variable", android.VariableMutator).Parallel()
3741 })
3742 ctx.Register(config)
3743
3744 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3745 android.FailIfErrored(t, errs)
3746 _, errs = ctx.PrepareBuildActions(config)
3747 android.FailIfErrored(t, errs)
3748
3749 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
3750 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
3751 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
3752 }
3753}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003754
3755func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3756 t.Parallel()
3757 bp := `
3758 cc_library_static {
3759 name: "libfoo",
3760 srcs: ["foo.c"],
3761 whole_static_libs: ["libbar"],
3762 }
3763
3764 cc_library_static {
3765 name: "libbar",
3766 whole_static_libs: ["libmissing"],
3767 }
3768 `
3769
3770 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3771 config.TestProductVariables.Allow_missing_dependencies = BoolPtr(true)
3772
3773 ctx := CreateTestContext()
3774 ctx.SetAllowMissingDependencies(true)
3775 ctx.Register(config)
3776
3777 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3778 android.FailIfErrored(t, errs)
3779 _, errs = ctx.PrepareBuildActions(config)
3780 android.FailIfErrored(t, errs)
3781
3782 libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
3783 if g, w := libbar.Rule, android.ErrorRule; g != w {
3784 t.Fatalf("Expected libbar rule to be %q, got %q", w, g)
3785 }
3786
3787 if g, w := libbar.Args["error"], "missing dependencies: libmissing"; !strings.Contains(g, w) {
3788 t.Errorf("Expected libbar error to contain %q, was %q", w, g)
3789 }
3790
3791 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
3792 if g, w := libfoo.Inputs.Strings(), libbar.Output.String(); !android.InList(w, g) {
3793 t.Errorf("Expected libfoo.a to depend on %q, got %q", w, g)
3794 }
3795
3796}