blob: 7c985850ea422ffc9be07033c6464c0adc5ef133 [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 Crossae8600b2020-10-29 17:09:13 -070056 ctx := CreateTestContext(config)
57 ctx.Register()
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 Crossae8600b2020-10-29 17:09:13 -070087 ctx := CreateTestContext(config)
88 ctx.Register()
Logan Chienf3511742017-10-31 18:04:35 +080089
90 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
91 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +080092 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +080093 return
94 }
95
96 _, errs = ctx.PrepareBuildActions(config)
97 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +080098 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +080099 return
100 }
101
102 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
103}
104
Justin Yun5f7f7e82019-11-18 19:52:14 +0900105func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900106 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900107 config := TestConfig(buildDir, android.Android, nil, bp, nil)
108 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
109 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
110 testCcErrorWithConfig(t, pattern, config)
111 return
112}
113
114func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900115 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900116 config := TestConfig(buildDir, android.Android, nil, bp, nil)
117 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
118 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
119 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
120 testCcErrorWithConfig(t, pattern, config)
121 return
122}
123
Logan Chienf3511742017-10-31 18:04:35 +0800124const (
Colin Cross7113d202019-11-20 16:39:12 -0800125 coreVariant = "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800126 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun5f7f7e82019-11-18 19:52:14 +0900127 productVariant = "android_product.VER_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800128 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800129)
130
Doug Hornc32c6b02019-01-17 14:44:05 -0800131func TestFuchsiaDeps(t *testing.T) {
132 t.Helper()
133
134 bp := `
135 cc_library {
136 name: "libTest",
137 srcs: ["foo.c"],
138 target: {
139 fuchsia: {
140 srcs: ["bar.c"],
141 },
142 },
143 }`
144
Colin Cross98be1bb2019-12-13 20:41:13 -0800145 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
146 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800147
148 rt := false
149 fb := false
150
151 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
152 implicits := ld.Implicits
153 for _, lib := range implicits {
154 if strings.Contains(lib.Rel(), "libcompiler_rt") {
155 rt = true
156 }
157
158 if strings.Contains(lib.Rel(), "libbioniccompat") {
159 fb = true
160 }
161 }
162
163 if !rt || !fb {
164 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
165 }
166}
167
168func TestFuchsiaTargetDecl(t *testing.T) {
169 t.Helper()
170
171 bp := `
172 cc_library {
173 name: "libTest",
174 srcs: ["foo.c"],
175 target: {
176 fuchsia: {
177 srcs: ["bar.c"],
178 },
179 },
180 }`
181
Colin Cross98be1bb2019-12-13 20:41:13 -0800182 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
183 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800184 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
185 var objs []string
186 for _, o := range ld.Inputs {
187 objs = append(objs, o.Base())
188 }
189 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
190 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
191 }
192}
193
Jiyong Park6a43f042017-10-12 23:05:00 +0900194func TestVendorSrc(t *testing.T) {
195 ctx := testCc(t, `
196 cc_library {
197 name: "libTest",
198 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700199 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800200 nocrt: true,
201 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900202 vendor_available: true,
203 target: {
204 vendor: {
205 srcs: ["bar.c"],
206 },
207 },
208 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900209 `)
210
Logan Chienf3511742017-10-31 18:04:35 +0800211 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900212 var objs []string
213 for _, o := range ld.Inputs {
214 objs = append(objs, o.Base())
215 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800216 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900217 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
218 }
219}
220
Logan Chienf3511742017-10-31 18:04:35 +0800221func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900222 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800223
Logan Chiend3c59a22018-03-29 14:08:15 +0800224 t.Helper()
225
Justin Yun0ecf0b22020-02-28 15:07:59 +0900226 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Ivan Lozano52767be2019-10-18 14:49:46 -0700227 if !mod.HasVendorVariant() {
Justin Yun0ecf0b22020-02-28 15:07:59 +0900228 t.Errorf("%q must have variant %q", name, variant)
Logan Chienf3511742017-10-31 18:04:35 +0800229 }
230
231 // Check library properties.
232 lib, ok := mod.compiler.(*libraryDecorator)
233 if !ok {
234 t.Errorf("%q must have libraryDecorator", name)
235 } else if lib.baseInstaller.subDir != subDir {
236 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
237 lib.baseInstaller.subDir)
238 }
239
240 // Check VNDK properties.
241 if mod.vndkdep == nil {
242 t.Fatalf("%q must have `vndkdep`", name)
243 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700244 if !mod.IsVndk() {
245 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800246 }
247 if mod.isVndkSp() != isVndkSp {
248 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
249 }
250
251 // Check VNDK extension properties.
252 isVndkExt := extends != ""
253 if mod.isVndkExt() != isVndkExt {
254 t.Errorf("%q isVndkExt() must equal to %t", name, isVndkExt)
255 }
256
257 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
258 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
259 }
260}
261
Bill Peckham945441c2020-08-31 16:07:58 -0700262func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool) {
263 t.Helper()
Jooyung Han39edb6c2019-11-06 16:53:07 +0900264 mod, ok := ctx.ModuleForTests(moduleName, variant).Module().(android.OutputFileProducer)
265 if !ok {
266 t.Errorf("%q must have output\n", moduleName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900267 return
268 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900269 outputFiles, err := mod.OutputFiles("")
270 if err != nil || len(outputFiles) != 1 {
271 t.Errorf("%q must have single output\n", moduleName)
272 return
273 }
274 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900275
Bill Peckham945441c2020-08-31 16:07:58 -0700276 if include {
277 out := singleton.Output(snapshotPath)
278 if out.Input.String() != outputFiles[0].String() {
279 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
280 }
281 } else {
282 out := singleton.MaybeOutput(snapshotPath)
283 if out.Rule != nil {
284 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
285 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900286 }
287}
288
Bill Peckham945441c2020-08-31 16:07:58 -0700289func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
290 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true)
291}
292
293func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
294 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false)
295}
296
Jooyung Han2216fb12019-11-06 16:46:15 +0900297func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
298 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800299 content := android.ContentFromFileRuleForTests(t, params)
300 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900301 assertArrayString(t, actual, expected)
302}
303
Jooyung Han097087b2019-10-22 19:32:18 +0900304func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
305 t.Helper()
306 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900307 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
308}
309
310func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
311 t.Helper()
312 vndkLibraries := ctx.ModuleForTests(module, "")
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +0900313
314 var output string
315 if module != "vndkcorevariant.libraries.txt" {
316 output = insertVndkVersion(module, "VER")
317 } else {
318 output = module
319 }
320
Jooyung Han2216fb12019-11-06 16:46:15 +0900321 checkWriteFileOutput(t, vndkLibraries.Output(output), expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900322}
323
Logan Chienf3511742017-10-31 18:04:35 +0800324func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800325 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800326 cc_library {
327 name: "libvndk",
328 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900329 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800330 vndk: {
331 enabled: true,
332 },
333 nocrt: true,
334 }
335
336 cc_library {
337 name: "libvndk_private",
338 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +0900339 product_available: false,
Logan Chienf3511742017-10-31 18:04:35 +0800340 vndk: {
341 enabled: true,
342 },
343 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900344 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800345 }
346
347 cc_library {
348 name: "libvndk_sp",
349 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900350 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800351 vndk: {
352 enabled: true,
353 support_system_process: true,
354 },
355 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900356 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800357 }
358
359 cc_library {
360 name: "libvndk_sp_private",
361 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +0900362 product_available: false,
Logan Chienf3511742017-10-31 18:04:35 +0800363 vndk: {
364 enabled: true,
365 support_system_process: true,
366 },
367 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900368 target: {
369 vendor: {
370 suffix: "-x",
371 },
372 },
Logan Chienf3511742017-10-31 18:04:35 +0800373 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900374 vndk_libraries_txt {
375 name: "llndk.libraries.txt",
376 }
377 vndk_libraries_txt {
378 name: "vndkcore.libraries.txt",
379 }
380 vndk_libraries_txt {
381 name: "vndksp.libraries.txt",
382 }
383 vndk_libraries_txt {
384 name: "vndkprivate.libraries.txt",
385 }
386 vndk_libraries_txt {
387 name: "vndkcorevariant.libraries.txt",
388 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800389 `
390
391 config := TestConfig(buildDir, android.Android, nil, bp, nil)
392 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900393 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Colin Cross98be1bb2019-12-13 20:41:13 -0800394 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
395
396 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800397
Jooyung Han261e1582020-10-20 18:54:21 +0900398 // subdir == "" because VNDK libs are not supposed to be installed separately.
399 // They are installed as part of VNDK APEX instead.
400 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
401 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
402 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
403 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900404
Justin Yun63e9ec72020-10-29 16:49:43 +0900405 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
406 checkVndkModule(t, ctx, "libvndk_private", "", false, "", productVariant)
407 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
408 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", productVariant)
409
Inseob Kim1f086e22019-05-09 13:29:15 +0900410 // Check VNDK snapshot output.
411
412 snapshotDir := "vndk-snapshot"
413 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
414
415 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
416 "arm64", "armv8-a"))
417 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
418 "arm", "armv7-a-neon"))
419
420 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
421 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
422 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
423 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
424
Colin Crossfb0c16e2019-11-20 17:12:35 -0800425 variant := "android_vendor.VER_arm64_armv8-a_shared"
426 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900427
Inseob Kim7f283f42020-06-01 21:53:49 +0900428 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
429
430 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
431 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
432 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
433 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900434
Jooyung Han39edb6c2019-11-06 16:53:07 +0900435 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900436 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
437 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
438 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
439 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900440
Jooyung Han097087b2019-10-22 19:32:18 +0900441 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
442 "LLNDK: libc.so",
443 "LLNDK: libdl.so",
444 "LLNDK: libft2.so",
445 "LLNDK: libm.so",
446 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900447 "VNDK-SP: libvndk_sp-x.so",
448 "VNDK-SP: libvndk_sp_private-x.so",
449 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900450 "VNDK-core: libvndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900451 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900452 "VNDK-private: libvndk-private.so",
453 "VNDK-private: libvndk_sp_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900454 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900455 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
456 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so"})
457 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so"})
458 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so"})
459 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
460}
461
Yo Chiangbba545e2020-06-09 16:15:37 +0800462func TestVndkWithHostSupported(t *testing.T) {
463 ctx := testCc(t, `
464 cc_library {
465 name: "libvndk_host_supported",
466 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900467 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800468 vndk: {
469 enabled: true,
470 },
471 host_supported: true,
472 }
473
474 cc_library {
475 name: "libvndk_host_supported_but_disabled_on_device",
476 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900477 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800478 vndk: {
479 enabled: true,
480 },
481 host_supported: true,
482 enabled: false,
483 target: {
484 host: {
485 enabled: true,
486 }
487 }
488 }
489
490 vndk_libraries_txt {
491 name: "vndkcore.libraries.txt",
492 }
493 `)
494
495 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
496}
497
Jooyung Han2216fb12019-11-06 16:46:15 +0900498func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800499 bp := `
Jooyung Han2216fb12019-11-06 16:46:15 +0900500 vndk_libraries_txt {
501 name: "llndk.libraries.txt",
Colin Cross98be1bb2019-12-13 20:41:13 -0800502 }`
503 config := TestConfig(buildDir, android.Android, nil, bp, nil)
504 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
505 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
506 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900507
508 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900509 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900510 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900511}
512
513func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800514 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900515 cc_library {
516 name: "libvndk",
517 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900518 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900519 vndk: {
520 enabled: true,
521 },
522 nocrt: true,
523 }
524
525 cc_library {
526 name: "libvndk_sp",
527 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900528 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900529 vndk: {
530 enabled: true,
531 support_system_process: true,
532 },
533 nocrt: true,
534 }
535
536 cc_library {
537 name: "libvndk2",
538 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +0900539 product_available: false,
Jooyung Han097087b2019-10-22 19:32:18 +0900540 vndk: {
541 enabled: true,
542 },
543 nocrt: true,
544 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900545
546 vndk_libraries_txt {
547 name: "vndkcorevariant.libraries.txt",
548 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800549 `
550
551 config := TestConfig(buildDir, android.Android, nil, bp, nil)
552 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
553 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
554 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
555
556 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
557
558 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900559
Jooyung Han2216fb12019-11-06 16:46:15 +0900560 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900561}
562
Chris Parsons79d66a52020-06-05 17:26:16 -0400563func TestDataLibs(t *testing.T) {
564 bp := `
565 cc_test_library {
566 name: "test_lib",
567 srcs: ["test_lib.cpp"],
568 gtest: false,
569 }
570
571 cc_test {
572 name: "main_test",
573 data_libs: ["test_lib"],
574 gtest: false,
575 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400576 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400577
578 config := TestConfig(buildDir, android.Android, nil, bp, nil)
579 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
580 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
581 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
582
583 ctx := testCcWithConfig(t, config)
584 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
585 testBinary := module.(*Module).linker.(*testBinary)
586 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
587 if err != nil {
588 t.Errorf("Expected cc_test to produce output files, error: %s", err)
589 return
590 }
591 if len(outputFiles) != 1 {
592 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
593 return
594 }
595 if len(testBinary.dataPaths()) != 1 {
596 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
597 return
598 }
599
600 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400601 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400602
603 if !strings.HasSuffix(outputPath, "/main_test") {
604 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
605 return
606 }
607 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
608 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
609 return
610 }
611}
612
Chris Parsons216e10a2020-07-09 17:12:52 -0400613func TestDataLibsRelativeInstallPath(t *testing.T) {
614 bp := `
615 cc_test_library {
616 name: "test_lib",
617 srcs: ["test_lib.cpp"],
618 relative_install_path: "foo/bar/baz",
619 gtest: false,
620 }
621
622 cc_test {
623 name: "main_test",
624 data_libs: ["test_lib"],
625 gtest: false,
626 }
627 `
628
629 config := TestConfig(buildDir, android.Android, nil, bp, nil)
630 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
631 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
632 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
633
634 ctx := testCcWithConfig(t, config)
635 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
636 testBinary := module.(*Module).linker.(*testBinary)
637 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
638 if err != nil {
639 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
640 }
641 if len(outputFiles) != 1 {
642 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
643 }
644 if len(testBinary.dataPaths()) != 1 {
645 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
646 }
647
648 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400649
650 if !strings.HasSuffix(outputPath, "/main_test") {
651 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
652 }
653 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
654 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
655 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400656 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400657 }
658}
659
Jooyung Han0302a842019-10-30 18:43:49 +0900660func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900661 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900662 cc_library {
663 name: "libvndk",
664 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900665 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900666 vndk: {
667 enabled: true,
668 },
669 nocrt: true,
670 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900671 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900672
673 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
674 "LLNDK: libc.so",
675 "LLNDK: libdl.so",
676 "LLNDK: libft2.so",
677 "LLNDK: libm.so",
678 "VNDK-SP: libc++.so",
679 "VNDK-core: libvndk.so",
680 "VNDK-private: libft2.so",
681 })
Logan Chienf3511742017-10-31 18:04:35 +0800682}
683
Justin Yun63e9ec72020-10-29 16:49:43 +0900684func TestVndkModuleError(t *testing.T) {
685 // Check the error message for vendor_available and product_available properties.
686 testCcError(t, "product_available: may not have different value than `vendor_available`", `
687 cc_library {
688 name: "libvndk",
689 vendor_available: true,
690 product_available: false,
691 vndk: {
692 enabled: true,
693 },
694 nocrt: true,
695 }
696 `)
697}
698
Logan Chiend3c59a22018-03-29 14:08:15 +0800699func TestVndkDepError(t *testing.T) {
700 // Check whether an error is emitted when a VNDK lib depends on a system lib.
701 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
702 cc_library {
703 name: "libvndk",
704 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900705 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800706 vndk: {
707 enabled: true,
708 },
709 shared_libs: ["libfwk"], // Cause error
710 nocrt: true,
711 }
712
713 cc_library {
714 name: "libfwk",
715 nocrt: true,
716 }
717 `)
718
719 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
720 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
721 cc_library {
722 name: "libvndk",
723 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900724 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800725 vndk: {
726 enabled: true,
727 },
728 shared_libs: ["libvendor"], // Cause error
729 nocrt: true,
730 }
731
732 cc_library {
733 name: "libvendor",
734 vendor: true,
735 nocrt: true,
736 }
737 `)
738
739 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
740 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
741 cc_library {
742 name: "libvndk_sp",
743 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900744 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800745 vndk: {
746 enabled: true,
747 support_system_process: true,
748 },
749 shared_libs: ["libfwk"], // Cause error
750 nocrt: true,
751 }
752
753 cc_library {
754 name: "libfwk",
755 nocrt: true,
756 }
757 `)
758
759 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
760 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
761 cc_library {
762 name: "libvndk_sp",
763 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900764 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800765 vndk: {
766 enabled: true,
767 support_system_process: true,
768 },
769 shared_libs: ["libvendor"], // Cause error
770 nocrt: true,
771 }
772
773 cc_library {
774 name: "libvendor",
775 vendor: true,
776 nocrt: true,
777 }
778 `)
779
780 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
781 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
782 cc_library {
783 name: "libvndk_sp",
784 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900785 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800786 vndk: {
787 enabled: true,
788 support_system_process: true,
789 },
790 shared_libs: ["libvndk"], // Cause error
791 nocrt: true,
792 }
793
794 cc_library {
795 name: "libvndk",
796 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900797 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800798 vndk: {
799 enabled: true,
800 },
801 nocrt: true,
802 }
803 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900804
805 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
806 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
807 cc_library {
808 name: "libvndk",
809 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900810 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900811 vndk: {
812 enabled: true,
813 },
814 shared_libs: ["libnonvndk"],
815 nocrt: true,
816 }
817
818 cc_library {
819 name: "libnonvndk",
820 vendor_available: true,
821 nocrt: true,
822 }
823 `)
824
825 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
826 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
827 cc_library {
828 name: "libvndkprivate",
829 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +0900830 product_available: false,
Jooyung Hana70f0672019-01-18 15:20:43 +0900831 vndk: {
832 enabled: true,
833 },
834 shared_libs: ["libnonvndk"],
835 nocrt: true,
836 }
837
838 cc_library {
839 name: "libnonvndk",
840 vendor_available: true,
841 nocrt: true,
842 }
843 `)
844
845 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
846 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
847 cc_library {
848 name: "libvndksp",
849 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900850 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900851 vndk: {
852 enabled: true,
853 support_system_process: true,
854 },
855 shared_libs: ["libnonvndk"],
856 nocrt: true,
857 }
858
859 cc_library {
860 name: "libnonvndk",
861 vendor_available: true,
862 nocrt: true,
863 }
864 `)
865
866 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
867 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
868 cc_library {
869 name: "libvndkspprivate",
870 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +0900871 product_available: false,
Jooyung Hana70f0672019-01-18 15:20:43 +0900872 vndk: {
873 enabled: true,
874 support_system_process: true,
875 },
876 shared_libs: ["libnonvndk"],
877 nocrt: true,
878 }
879
880 cc_library {
881 name: "libnonvndk",
882 vendor_available: true,
883 nocrt: true,
884 }
885 `)
886}
887
888func TestDoubleLoadbleDep(t *testing.T) {
889 // okay to link : LLNDK -> double_loadable VNDK
890 testCc(t, `
891 cc_library {
892 name: "libllndk",
893 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -0700894 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900895 }
896
897 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -0700898 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900899 symbol_file: "",
900 }
901
902 cc_library {
903 name: "libdoubleloadable",
904 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900905 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900906 vndk: {
907 enabled: true,
908 },
909 double_loadable: true,
910 }
911 `)
912 // okay to link : LLNDK -> VNDK-SP
913 testCc(t, `
914 cc_library {
915 name: "libllndk",
916 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -0700917 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900918 }
919
920 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -0700921 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900922 symbol_file: "",
923 }
924
925 cc_library {
926 name: "libvndksp",
927 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900928 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900929 vndk: {
930 enabled: true,
931 support_system_process: true,
932 },
933 }
934 `)
935 // okay to link : double_loadable -> double_loadable
936 testCc(t, `
937 cc_library {
938 name: "libdoubleloadable1",
939 shared_libs: ["libdoubleloadable2"],
940 vendor_available: true,
941 double_loadable: true,
942 }
943
944 cc_library {
945 name: "libdoubleloadable2",
946 vendor_available: true,
947 double_loadable: true,
948 }
949 `)
950 // okay to link : double_loadable VNDK -> double_loadable VNDK private
951 testCc(t, `
952 cc_library {
953 name: "libdoubleloadable",
954 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900955 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900956 vndk: {
957 enabled: true,
958 },
959 double_loadable: true,
960 shared_libs: ["libnondoubleloadable"],
961 }
962
963 cc_library {
964 name: "libnondoubleloadable",
965 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +0900966 product_available: false,
Jooyung Hana70f0672019-01-18 15:20:43 +0900967 vndk: {
968 enabled: true,
969 },
970 double_loadable: true,
971 }
972 `)
973 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
974 testCc(t, `
975 cc_library {
976 name: "libllndk",
977 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -0700978 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900979 }
980
981 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -0700982 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900983 symbol_file: "",
984 }
985
986 cc_library {
987 name: "libcoreonly",
988 shared_libs: ["libvendoravailable"],
989 }
990
991 // indirect dependency of LLNDK
992 cc_library {
993 name: "libvendoravailable",
994 vendor_available: true,
995 double_loadable: true,
996 }
997 `)
998}
999
Inseob Kim5f58ff72020-09-07 19:53:31 +09001000func TestVendorSnapshotCapture(t *testing.T) {
Inseob Kim8471cda2019-11-15 09:59:12 +09001001 bp := `
1002 cc_library {
1003 name: "libvndk",
1004 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001005 product_available: true,
Inseob Kim8471cda2019-11-15 09:59:12 +09001006 vndk: {
1007 enabled: true,
1008 },
1009 nocrt: true,
1010 }
1011
1012 cc_library {
1013 name: "libvendor",
1014 vendor: true,
1015 nocrt: true,
1016 }
1017
1018 cc_library {
1019 name: "libvendor_available",
1020 vendor_available: true,
1021 nocrt: true,
1022 }
1023
1024 cc_library_headers {
1025 name: "libvendor_headers",
1026 vendor_available: true,
1027 nocrt: true,
1028 }
1029
1030 cc_binary {
1031 name: "vendor_bin",
1032 vendor: true,
1033 nocrt: true,
1034 }
1035
1036 cc_binary {
1037 name: "vendor_available_bin",
1038 vendor_available: true,
1039 nocrt: true,
1040 }
Inseob Kim7f283f42020-06-01 21:53:49 +09001041
1042 toolchain_library {
1043 name: "libb",
1044 vendor_available: true,
1045 src: "libb.a",
1046 }
Inseob Kim1042d292020-06-01 23:23:05 +09001047
1048 cc_object {
1049 name: "obj",
1050 vendor_available: true,
1051 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001052`
1053 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1054 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1055 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1056 ctx := testCcWithConfig(t, config)
1057
1058 // Check Vendor snapshot output.
1059
1060 snapshotDir := "vendor-snapshot"
1061 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +09001062 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1063
1064 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +09001065
1066 for _, arch := range [][]string{
1067 []string{"arm64", "armv8-a"},
1068 []string{"arm", "armv7-a-neon"},
1069 } {
1070 archType := arch[0]
1071 archVariant := arch[1]
1072 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1073
1074 // For shared libraries, only non-VNDK vendor_available modules are captured
1075 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1076 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +09001077 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1078 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
1079 jsonFiles = append(jsonFiles,
1080 filepath.Join(sharedDir, "libvendor.so.json"),
1081 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001082
1083 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
Inseob Kimc42f2f22020-07-29 20:32:10 +09001084 // Also cfi variants are captured, except for prebuilts like toolchain_library
Inseob Kim8471cda2019-11-15 09:59:12 +09001085 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001086 staticCfiVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static_cfi", archType, archVariant)
Inseob Kim8471cda2019-11-15 09:59:12 +09001087 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +09001088 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1089 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001090 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001091 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001092 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001093 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001094 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001095 jsonFiles = append(jsonFiles,
1096 filepath.Join(staticDir, "libb.a.json"),
1097 filepath.Join(staticDir, "libvndk.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001098 filepath.Join(staticDir, "libvndk.cfi.a.json"),
Inseob Kim7f283f42020-06-01 21:53:49 +09001099 filepath.Join(staticDir, "libvendor.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001100 filepath.Join(staticDir, "libvendor.cfi.a.json"),
1101 filepath.Join(staticDir, "libvendor_available.a.json"),
1102 filepath.Join(staticDir, "libvendor_available.cfi.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001103
Inseob Kim7f283f42020-06-01 21:53:49 +09001104 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +09001105 if archType == "arm64" {
1106 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1107 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +09001108 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
1109 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
1110 jsonFiles = append(jsonFiles,
1111 filepath.Join(binaryDir, "vendor_bin.json"),
1112 filepath.Join(binaryDir, "vendor_available_bin.json"))
1113 }
1114
1115 // For header libraries, all vendor:true and vendor_available modules are captured.
1116 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1117 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +09001118
1119 // For object modules, all vendor:true and vendor_available modules are captured.
1120 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1121 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1122 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1123 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001124 }
1125
1126 for _, jsonFile := range jsonFiles {
1127 // verify all json files exist
1128 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1129 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001130 }
1131 }
1132}
1133
Inseob Kim5f58ff72020-09-07 19:53:31 +09001134func TestVendorSnapshotUse(t *testing.T) {
1135 frameworkBp := `
1136 cc_library {
1137 name: "libvndk",
1138 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001139 product_available: true,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001140 vndk: {
1141 enabled: true,
1142 },
1143 nocrt: true,
1144 compile_multilib: "64",
1145 }
1146
1147 cc_library {
1148 name: "libvendor",
1149 vendor: true,
1150 nocrt: true,
1151 no_libcrt: true,
1152 stl: "none",
1153 system_shared_libs: [],
1154 compile_multilib: "64",
1155 }
1156
1157 cc_binary {
1158 name: "bin",
1159 vendor: true,
1160 nocrt: true,
1161 no_libcrt: true,
1162 stl: "none",
1163 system_shared_libs: [],
1164 compile_multilib: "64",
1165 }
1166`
1167
1168 vndkBp := `
1169 vndk_prebuilt_shared {
1170 name: "libvndk",
1171 version: "BOARD",
1172 target_arch: "arm64",
1173 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001174 product_available: true,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001175 vndk: {
1176 enabled: true,
1177 },
1178 arch: {
1179 arm64: {
1180 srcs: ["libvndk.so"],
Inseob Kim67be7322020-10-19 10:15:28 +09001181 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001182 },
1183 },
1184 }
1185`
1186
1187 vendorProprietaryBp := `
1188 cc_library {
1189 name: "libvendor_without_snapshot",
1190 vendor: true,
1191 nocrt: true,
1192 no_libcrt: true,
1193 stl: "none",
1194 system_shared_libs: [],
1195 compile_multilib: "64",
1196 }
1197
1198 cc_library_shared {
1199 name: "libclient",
1200 vendor: true,
1201 nocrt: true,
1202 no_libcrt: true,
1203 stl: "none",
1204 system_shared_libs: [],
1205 shared_libs: ["libvndk"],
1206 static_libs: ["libvendor", "libvendor_without_snapshot"],
1207 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001208 srcs: ["client.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001209 }
1210
1211 cc_binary {
1212 name: "bin_without_snapshot",
1213 vendor: true,
1214 nocrt: true,
1215 no_libcrt: true,
1216 stl: "none",
1217 system_shared_libs: [],
1218 static_libs: ["libvndk"],
1219 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001220 srcs: ["bin.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001221 }
1222
1223 vendor_snapshot_static {
1224 name: "libvndk",
1225 version: "BOARD",
1226 target_arch: "arm64",
1227 vendor: true,
1228 arch: {
1229 arm64: {
1230 src: "libvndk.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001231 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001232 },
1233 },
1234 }
1235
1236 vendor_snapshot_shared {
1237 name: "libvendor",
1238 version: "BOARD",
1239 target_arch: "arm64",
1240 vendor: true,
1241 arch: {
1242 arm64: {
1243 src: "libvendor.so",
Inseob Kim67be7322020-10-19 10:15:28 +09001244 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001245 },
1246 },
1247 }
1248
1249 vendor_snapshot_static {
1250 name: "libvendor",
1251 version: "BOARD",
1252 target_arch: "arm64",
1253 vendor: true,
1254 arch: {
1255 arm64: {
1256 src: "libvendor.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001257 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001258 },
1259 },
1260 }
1261
1262 vendor_snapshot_binary {
1263 name: "bin",
1264 version: "BOARD",
1265 target_arch: "arm64",
1266 vendor: true,
1267 arch: {
1268 arm64: {
1269 src: "bin",
1270 },
1271 },
1272 }
1273`
1274 depsBp := GatherRequiredDepsForTest(android.Android)
1275
1276 mockFS := map[string][]byte{
Inseob Kim67be7322020-10-19 10:15:28 +09001277 "deps/Android.bp": []byte(depsBp),
1278 "framework/Android.bp": []byte(frameworkBp),
1279 "vendor/Android.bp": []byte(vendorProprietaryBp),
1280 "vendor/bin": nil,
1281 "vendor/bin.cpp": nil,
1282 "vendor/client.cpp": nil,
1283 "vendor/include/libvndk/a.h": nil,
1284 "vendor/include/libvendor/b.h": nil,
1285 "vendor/libvndk.a": nil,
1286 "vendor/libvendor.a": nil,
1287 "vendor/libvendor.so": nil,
1288 "vndk/Android.bp": []byte(vndkBp),
1289 "vndk/include/libvndk/a.h": nil,
1290 "vndk/libvndk.so": nil,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001291 }
1292
1293 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1294 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1295 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001296 ctx := CreateTestContext(config)
1297 ctx.Register()
Inseob Kim5f58ff72020-09-07 19:53:31 +09001298
1299 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "vendor/Android.bp", "vndk/Android.bp"})
1300 android.FailIfErrored(t, errs)
1301 _, errs = ctx.PrepareBuildActions(config)
1302 android.FailIfErrored(t, errs)
1303
1304 sharedVariant := "android_vendor.BOARD_arm64_armv8-a_shared"
1305 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1306 binaryVariant := "android_vendor.BOARD_arm64_armv8-a"
1307
1308 // libclient uses libvndk.vndk.BOARD.arm64, libvendor.vendor_static.BOARD.arm64, libvendor_without_snapshot
Inseob Kim67be7322020-10-19 10:15:28 +09001309 libclientCcFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("cc").Args["cFlags"]
1310 for _, includeFlags := range []string{
1311 "-Ivndk/include/libvndk", // libvndk
1312 "-Ivendor/include/libvendor", // libvendor
1313 } {
1314 if !strings.Contains(libclientCcFlags, includeFlags) {
1315 t.Errorf("flags for libclient must contain %#v, but was %#v.",
1316 includeFlags, libclientCcFlags)
1317 }
1318 }
Inseob Kim5f58ff72020-09-07 19:53:31 +09001319
Inseob Kim67be7322020-10-19 10:15:28 +09001320 libclientLdFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001321 for _, input := range [][]string{
1322 []string{sharedVariant, "libvndk.vndk.BOARD.arm64"},
1323 []string{staticVariant, "libvendor.vendor_static.BOARD.arm64"},
1324 []string{staticVariant, "libvendor_without_snapshot"},
1325 } {
1326 outputPaths := getOutputPaths(ctx, input[0] /* variant */, []string{input[1]} /* module name */)
Inseob Kim67be7322020-10-19 10:15:28 +09001327 if !strings.Contains(libclientLdFlags, outputPaths[0].String()) {
1328 t.Errorf("libflags for libclient must contain %#v, but was %#v", outputPaths[0], libclientLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001329 }
1330 }
1331
1332 // bin_without_snapshot uses libvndk.vendor_static.BOARD.arm64
Inseob Kim67be7322020-10-19 10:15:28 +09001333 binWithoutSnapshotCcFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("cc").Args["cFlags"]
1334 if !strings.Contains(binWithoutSnapshotCcFlags, "-Ivendor/include/libvndk") {
1335 t.Errorf("flags for bin_without_snapshot must contain %#v, but was %#v.",
1336 "-Ivendor/include/libvndk", binWithoutSnapshotCcFlags)
1337 }
1338
1339 binWithoutSnapshotLdFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001340 libVndkStaticOutputPaths := getOutputPaths(ctx, staticVariant, []string{"libvndk.vendor_static.BOARD.arm64"})
Inseob Kim67be7322020-10-19 10:15:28 +09001341 if !strings.Contains(binWithoutSnapshotLdFlags, libVndkStaticOutputPaths[0].String()) {
Inseob Kim5f58ff72020-09-07 19:53:31 +09001342 t.Errorf("libflags for bin_without_snapshot must contain %#v, but was %#v",
Inseob Kim67be7322020-10-19 10:15:28 +09001343 libVndkStaticOutputPaths[0], binWithoutSnapshotLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001344 }
1345
1346 // libvendor.so is installed by libvendor.vendor_shared.BOARD.arm64
1347 ctx.ModuleForTests("libvendor.vendor_shared.BOARD.arm64", sharedVariant).Output("libvendor.so")
1348
1349 // libvendor_without_snapshot.so is installed by libvendor_without_snapshot
1350 ctx.ModuleForTests("libvendor_without_snapshot", sharedVariant).Output("libvendor_without_snapshot.so")
1351
1352 // bin is installed by bin.vendor_binary.BOARD.arm64
1353 ctx.ModuleForTests("bin.vendor_binary.BOARD.arm64", binaryVariant).Output("bin")
1354
1355 // bin_without_snapshot is installed by bin_without_snapshot
1356 ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Output("bin_without_snapshot")
1357
1358 // libvendor and bin don't have vendor.BOARD variant
1359 libvendorVariants := ctx.ModuleVariantsForTests("libvendor")
1360 if inList(sharedVariant, libvendorVariants) {
1361 t.Errorf("libvendor must not have variant %#v, but it does", sharedVariant)
1362 }
1363
1364 binVariants := ctx.ModuleVariantsForTests("bin")
1365 if inList(binaryVariant, binVariants) {
1366 t.Errorf("bin must not have variant %#v, but it does", sharedVariant)
1367 }
1368}
1369
Inseob Kimc42f2f22020-07-29 20:32:10 +09001370func TestVendorSnapshotSanitizer(t *testing.T) {
1371 bp := `
1372 vendor_snapshot_static {
1373 name: "libsnapshot",
1374 vendor: true,
1375 target_arch: "arm64",
1376 version: "BOARD",
1377 arch: {
1378 arm64: {
1379 src: "libsnapshot.a",
1380 cfi: {
1381 src: "libsnapshot.cfi.a",
1382 }
1383 },
1384 },
1385 }
1386`
1387 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1388 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1389 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1390 ctx := testCcWithConfig(t, config)
1391
1392 // Check non-cfi and cfi variant.
1393 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1394 staticCfiVariant := "android_vendor.BOARD_arm64_armv8-a_static_cfi"
1395
1396 staticModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticVariant).Module().(*Module)
1397 assertString(t, staticModule.outputFile.Path().Base(), "libsnapshot.a")
1398
1399 staticCfiModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticCfiVariant).Module().(*Module)
1400 assertString(t, staticCfiModule.outputFile.Path().Base(), "libsnapshot.cfi.a")
1401}
1402
Bill Peckham945441c2020-08-31 16:07:58 -07001403func assertExcludeFromVendorSnapshotIs(t *testing.T, c *Module, expected bool) {
1404 t.Helper()
1405 if c.ExcludeFromVendorSnapshot() != expected {
1406 t.Errorf("expected %q ExcludeFromVendorSnapshot to be %t", c.String(), expected)
1407 }
1408}
1409
1410func TestVendorSnapshotExclude(t *testing.T) {
1411
1412 // This test verifies that the exclude_from_vendor_snapshot property
1413 // makes its way from the Android.bp source file into the module data
1414 // structure. It also verifies that modules are correctly included or
1415 // excluded in the vendor snapshot based on their path (framework or
1416 // vendor) and the exclude_from_vendor_snapshot property.
1417
1418 frameworkBp := `
1419 cc_library_shared {
1420 name: "libinclude",
1421 srcs: ["src/include.cpp"],
1422 vendor_available: true,
1423 }
1424 cc_library_shared {
1425 name: "libexclude",
1426 srcs: ["src/exclude.cpp"],
1427 vendor: true,
1428 exclude_from_vendor_snapshot: true,
1429 }
1430 `
1431
1432 vendorProprietaryBp := `
1433 cc_library_shared {
1434 name: "libvendor",
1435 srcs: ["vendor.cpp"],
1436 vendor: true,
1437 }
1438 `
1439
1440 depsBp := GatherRequiredDepsForTest(android.Android)
1441
1442 mockFS := map[string][]byte{
1443 "deps/Android.bp": []byte(depsBp),
1444 "framework/Android.bp": []byte(frameworkBp),
1445 "framework/include.cpp": nil,
1446 "framework/exclude.cpp": nil,
1447 "device/Android.bp": []byte(vendorProprietaryBp),
1448 "device/vendor.cpp": nil,
1449 }
1450
1451 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1452 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1453 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001454 ctx := CreateTestContext(config)
1455 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001456
1457 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1458 android.FailIfErrored(t, errs)
1459 _, errs = ctx.PrepareBuildActions(config)
1460 android.FailIfErrored(t, errs)
1461
1462 // Test an include and exclude framework module.
1463 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1464 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", vendorVariant).Module().(*Module), false)
1465 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libexclude", vendorVariant).Module().(*Module), true)
1466
1467 // A vendor module is excluded, but by its path, not the
1468 // exclude_from_vendor_snapshot property.
1469 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libvendor", vendorVariant).Module().(*Module), false)
1470
1471 // Verify the content of the vendor snapshot.
1472
1473 snapshotDir := "vendor-snapshot"
1474 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1475 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1476
1477 var includeJsonFiles []string
1478 var excludeJsonFiles []string
1479
1480 for _, arch := range [][]string{
1481 []string{"arm64", "armv8-a"},
1482 []string{"arm", "armv7-a-neon"},
1483 } {
1484 archType := arch[0]
1485 archVariant := arch[1]
1486 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1487
1488 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1489 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1490
1491 // Included modules
1492 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1493 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1494
1495 // Excluded modules
1496 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1497 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1498 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1499 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1500 }
1501
1502 // Verify that each json file for an included module has a rule.
1503 for _, jsonFile := range includeJsonFiles {
1504 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1505 t.Errorf("include json file %q not found", jsonFile)
1506 }
1507 }
1508
1509 // Verify that each json file for an excluded module has no rule.
1510 for _, jsonFile := range excludeJsonFiles {
1511 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1512 t.Errorf("exclude json file %q found", jsonFile)
1513 }
1514 }
1515}
1516
1517func TestVendorSnapshotExcludeInVendorProprietaryPathErrors(t *testing.T) {
1518
1519 // This test verifies that using the exclude_from_vendor_snapshot
1520 // property on a module in a vendor proprietary path generates an
1521 // error. These modules are already excluded, so we prohibit using the
1522 // property in this way, which could add to confusion.
1523
1524 vendorProprietaryBp := `
1525 cc_library_shared {
1526 name: "libvendor",
1527 srcs: ["vendor.cpp"],
1528 vendor: true,
1529 exclude_from_vendor_snapshot: true,
1530 }
1531 `
1532
1533 depsBp := GatherRequiredDepsForTest(android.Android)
1534
1535 mockFS := map[string][]byte{
1536 "deps/Android.bp": []byte(depsBp),
1537 "device/Android.bp": []byte(vendorProprietaryBp),
1538 "device/vendor.cpp": nil,
1539 }
1540
1541 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1542 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1543 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001544 ctx := CreateTestContext(config)
1545 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001546
1547 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "device/Android.bp"})
1548 android.FailIfErrored(t, errs)
1549
1550 _, errs = ctx.PrepareBuildActions(config)
1551 android.CheckErrorsAgainstExpectations(t, errs, []string{
1552 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1553 `module "libvendor\{.+,image:vendor.+,arch:arm_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
Jose Galmesf7294582020-11-13 12:07:36 -08001554 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1555 `module "libvendor\{.+,image:vendor.+,arch:arm_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
Bill Peckham945441c2020-08-31 16:07:58 -07001556 })
1557}
1558
1559func TestVendorSnapshotExcludeWithVendorAvailable(t *testing.T) {
1560
1561 // This test verifies that using the exclude_from_vendor_snapshot
1562 // property on a module that is vendor available generates an error. A
1563 // vendor available module must be captured in the vendor snapshot and
1564 // must not built from source when building the vendor image against
1565 // the vendor snapshot.
1566
1567 frameworkBp := `
1568 cc_library_shared {
1569 name: "libinclude",
1570 srcs: ["src/include.cpp"],
1571 vendor_available: true,
1572 exclude_from_vendor_snapshot: true,
1573 }
1574 `
1575
1576 depsBp := GatherRequiredDepsForTest(android.Android)
1577
1578 mockFS := map[string][]byte{
1579 "deps/Android.bp": []byte(depsBp),
1580 "framework/Android.bp": []byte(frameworkBp),
1581 "framework/include.cpp": nil,
1582 }
1583
1584 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1585 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1586 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001587 ctx := CreateTestContext(config)
1588 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001589
1590 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp"})
1591 android.FailIfErrored(t, errs)
1592
1593 _, errs = ctx.PrepareBuildActions(config)
1594 android.CheckErrorsAgainstExpectations(t, errs, []string{
1595 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1596 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1597 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1598 `module "libinclude\{.+,image:vendor.+,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1599 })
1600}
1601
Jose Galmesf7294582020-11-13 12:07:36 -08001602func TestRecoverySnapshotCapture(t *testing.T) {
1603 bp := `
1604 cc_library {
1605 name: "libvndk",
1606 vendor_available: true,
1607 recovery_available: true,
1608 product_available: true,
1609 vndk: {
1610 enabled: true,
1611 },
1612 nocrt: true,
1613 }
1614
1615 cc_library {
1616 name: "librecovery",
1617 recovery: true,
1618 nocrt: true,
1619 }
1620
1621 cc_library {
1622 name: "librecovery_available",
1623 recovery_available: true,
1624 nocrt: true,
1625 }
1626
1627 cc_library_headers {
1628 name: "librecovery_headers",
1629 recovery_available: true,
1630 nocrt: true,
1631 }
1632
1633 cc_binary {
1634 name: "recovery_bin",
1635 recovery: true,
1636 nocrt: true,
1637 }
1638
1639 cc_binary {
1640 name: "recovery_available_bin",
1641 recovery_available: true,
1642 nocrt: true,
1643 }
1644
1645 toolchain_library {
1646 name: "libb",
1647 recovery_available: true,
1648 src: "libb.a",
1649 }
1650
1651 cc_object {
1652 name: "obj",
1653 recovery_available: true,
1654 }
1655`
1656 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1657 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1658 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1659 ctx := testCcWithConfig(t, config)
1660
1661 // Check Recovery snapshot output.
1662
1663 snapshotDir := "recovery-snapshot"
1664 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1665 snapshotSingleton := ctx.SingletonForTests("recovery-snapshot")
1666
1667 var jsonFiles []string
1668
1669 for _, arch := range [][]string{
1670 []string{"arm64", "armv8-a"},
1671 } {
1672 archType := arch[0]
1673 archVariant := arch[1]
1674 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1675
1676 // For shared libraries, only recovery_available modules are captured.
1677 sharedVariant := fmt.Sprintf("android_recovery_%s_%s_shared", archType, archVariant)
1678 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1679 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", sharedDir, sharedVariant)
1680 checkSnapshot(t, ctx, snapshotSingleton, "librecovery", "librecovery.so", sharedDir, sharedVariant)
1681 checkSnapshot(t, ctx, snapshotSingleton, "librecovery_available", "librecovery_available.so", sharedDir, sharedVariant)
1682 jsonFiles = append(jsonFiles,
1683 filepath.Join(sharedDir, "libvndk.so.json"),
1684 filepath.Join(sharedDir, "librecovery.so.json"),
1685 filepath.Join(sharedDir, "librecovery_available.so.json"))
1686
1687 // For static libraries, all recovery:true and recovery_available modules are captured.
1688 staticVariant := fmt.Sprintf("android_recovery_%s_%s_static", archType, archVariant)
1689 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
1690 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1691 checkSnapshot(t, ctx, snapshotSingleton, "librecovery", "librecovery.a", staticDir, staticVariant)
1692 checkSnapshot(t, ctx, snapshotSingleton, "librecovery_available", "librecovery_available.a", staticDir, staticVariant)
1693 jsonFiles = append(jsonFiles,
1694 filepath.Join(staticDir, "libb.a.json"),
1695 filepath.Join(staticDir, "librecovery.a.json"),
1696 filepath.Join(staticDir, "librecovery_available.a.json"))
1697
1698 // For binary executables, all recovery:true and recovery_available modules are captured.
1699 if archType == "arm64" {
1700 binaryVariant := fmt.Sprintf("android_recovery_%s_%s", archType, archVariant)
1701 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
1702 checkSnapshot(t, ctx, snapshotSingleton, "recovery_bin", "recovery_bin", binaryDir, binaryVariant)
1703 checkSnapshot(t, ctx, snapshotSingleton, "recovery_available_bin", "recovery_available_bin", binaryDir, binaryVariant)
1704 jsonFiles = append(jsonFiles,
1705 filepath.Join(binaryDir, "recovery_bin.json"),
1706 filepath.Join(binaryDir, "recovery_available_bin.json"))
1707 }
1708
1709 // For header libraries, all vendor:true and vendor_available modules are captured.
1710 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1711 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "librecovery_headers.json"))
1712
1713 // For object modules, all vendor:true and vendor_available modules are captured.
1714 objectVariant := fmt.Sprintf("android_recovery_%s_%s", archType, archVariant)
1715 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1716 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1717 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
1718 }
1719
1720 for _, jsonFile := range jsonFiles {
1721 // verify all json files exist
1722 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1723 t.Errorf("%q expected but not found", jsonFile)
1724 }
1725 }
1726}
1727
Jooyung Hana70f0672019-01-18 15:20:43 +09001728func TestDoubleLoadableDepError(t *testing.T) {
1729 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1730 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1731 cc_library {
1732 name: "libllndk",
1733 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001734 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001735 }
1736
1737 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001738 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001739 symbol_file: "",
1740 }
1741
1742 cc_library {
1743 name: "libnondoubleloadable",
1744 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001745 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001746 vndk: {
1747 enabled: true,
1748 },
1749 }
1750 `)
1751
1752 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1753 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1754 cc_library {
1755 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001756 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001757 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001758 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001759 }
1760
1761 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001762 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001763 symbol_file: "",
1764 }
1765
1766 cc_library {
1767 name: "libnondoubleloadable",
1768 vendor_available: true,
1769 }
1770 `)
1771
1772 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
1773 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1774 cc_library {
1775 name: "libdoubleloadable",
1776 vendor_available: true,
1777 double_loadable: true,
1778 shared_libs: ["libnondoubleloadable"],
1779 }
1780
1781 cc_library {
1782 name: "libnondoubleloadable",
1783 vendor_available: true,
1784 }
1785 `)
1786
1787 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
1788 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1789 cc_library {
1790 name: "libdoubleloadable",
1791 vendor_available: true,
1792 double_loadable: true,
1793 shared_libs: ["libnondoubleloadable"],
1794 }
1795
1796 cc_library {
1797 name: "libnondoubleloadable",
1798 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001799 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001800 vndk: {
1801 enabled: true,
1802 },
1803 }
1804 `)
1805
1806 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
1807 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1808 cc_library {
1809 name: "libdoubleloadable",
1810 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001811 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001812 vndk: {
1813 enabled: true,
1814 },
1815 double_loadable: true,
1816 shared_libs: ["libnondoubleloadable"],
1817 }
1818
1819 cc_library {
1820 name: "libnondoubleloadable",
1821 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09001822 product_available: false,
Jooyung Hana70f0672019-01-18 15:20:43 +09001823 vndk: {
1824 enabled: true,
1825 },
1826 }
1827 `)
1828
1829 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1830 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1831 cc_library {
1832 name: "libllndk",
1833 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001834 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001835 }
1836
1837 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001838 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001839 symbol_file: "",
1840 }
1841
1842 cc_library {
1843 name: "libcoreonly",
1844 shared_libs: ["libvendoravailable"],
1845 }
1846
1847 // indirect dependency of LLNDK
1848 cc_library {
1849 name: "libvendoravailable",
1850 vendor_available: true,
1851 }
1852 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001853}
1854
Jooyung Han479ca172020-10-19 18:51:07 +09001855func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1856 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1857 cc_library {
1858 name: "libvndksp",
1859 shared_libs: ["libanothervndksp"],
1860 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001861 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001862 vndk: {
1863 enabled: true,
1864 support_system_process: true,
1865 }
1866 }
1867
1868 cc_library {
1869 name: "libllndk",
1870 shared_libs: ["libanothervndksp"],
1871 }
1872
1873 llndk_library {
1874 name: "libllndk",
1875 symbol_file: "",
1876 }
1877
1878 cc_library {
1879 name: "libanothervndksp",
1880 vendor_available: true,
1881 }
1882 `)
1883}
1884
Logan Chienf3511742017-10-31 18:04:35 +08001885func TestVndkExt(t *testing.T) {
1886 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001887 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001888 cc_library {
1889 name: "libvndk",
1890 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001891 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001892 vndk: {
1893 enabled: true,
1894 },
1895 nocrt: true,
1896 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001897 cc_library {
1898 name: "libvndk2",
1899 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001900 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001901 vndk: {
1902 enabled: true,
1903 },
1904 target: {
1905 vendor: {
1906 suffix: "-suffix",
1907 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001908 product: {
1909 suffix: "-suffix",
1910 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001911 },
1912 nocrt: true,
1913 }
Logan Chienf3511742017-10-31 18:04:35 +08001914
1915 cc_library {
1916 name: "libvndk_ext",
1917 vendor: true,
1918 vndk: {
1919 enabled: true,
1920 extends: "libvndk",
1921 },
1922 nocrt: true,
1923 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001924
1925 cc_library {
1926 name: "libvndk2_ext",
1927 vendor: true,
1928 vndk: {
1929 enabled: true,
1930 extends: "libvndk2",
1931 },
1932 nocrt: true,
1933 }
Logan Chienf3511742017-10-31 18:04:35 +08001934
Justin Yun0ecf0b22020-02-28 15:07:59 +09001935 cc_library {
1936 name: "libvndk_ext_product",
1937 product_specific: true,
1938 vndk: {
1939 enabled: true,
1940 extends: "libvndk",
1941 },
1942 nocrt: true,
1943 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001944
Justin Yun0ecf0b22020-02-28 15:07:59 +09001945 cc_library {
1946 name: "libvndk2_ext_product",
1947 product_specific: true,
1948 vndk: {
1949 enabled: true,
1950 extends: "libvndk2",
1951 },
1952 nocrt: true,
1953 }
1954 `
1955 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1956 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1957 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1958 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1959
1960 ctx := testCcWithConfig(t, config)
1961
1962 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1963 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1964
1965 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1966 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1967
1968 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1969 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001970}
1971
Logan Chiend3c59a22018-03-29 14:08:15 +08001972func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001973 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1974 ctx := testCcNoVndk(t, `
1975 cc_library {
1976 name: "libvndk",
1977 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001978 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001979 vndk: {
1980 enabled: true,
1981 },
1982 nocrt: true,
1983 }
1984
1985 cc_library {
1986 name: "libvndk_ext",
1987 vendor: true,
1988 vndk: {
1989 enabled: true,
1990 extends: "libvndk",
1991 },
1992 nocrt: true,
1993 }
1994 `)
1995
1996 // Ensures that the core variant of "libvndk_ext" can be found.
1997 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1998 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1999 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
2000 }
2001}
2002
Justin Yun0ecf0b22020-02-28 15:07:59 +09002003func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
2004 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
2005 ctx := testCc(t, `
2006 cc_library {
2007 name: "libvndk",
2008 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002009 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002010 vndk: {
2011 enabled: true,
2012 },
2013 nocrt: true,
2014 }
2015
2016 cc_library {
2017 name: "libvndk_ext_product",
2018 product_specific: true,
2019 vndk: {
2020 enabled: true,
2021 extends: "libvndk",
2022 },
2023 nocrt: true,
2024 }
2025 `)
2026
2027 // Ensures that the core variant of "libvndk_ext_product" can be found.
2028 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
2029 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
2030 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
2031 }
2032}
2033
Logan Chienf3511742017-10-31 18:04:35 +08002034func TestVndkExtError(t *testing.T) {
2035 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09002036 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08002037 cc_library {
2038 name: "libvndk",
2039 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002040 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002041 vndk: {
2042 enabled: true,
2043 },
2044 nocrt: true,
2045 }
2046
2047 cc_library {
2048 name: "libvndk_ext",
2049 vndk: {
2050 enabled: true,
2051 extends: "libvndk",
2052 },
2053 nocrt: true,
2054 }
2055 `)
2056
2057 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
2058 cc_library {
2059 name: "libvndk",
2060 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002061 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002062 vndk: {
2063 enabled: true,
2064 },
2065 nocrt: true,
2066 }
2067
2068 cc_library {
2069 name: "libvndk_ext",
2070 vendor: true,
2071 vndk: {
2072 enabled: true,
2073 },
2074 nocrt: true,
2075 }
2076 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002077
2078 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
2079 cc_library {
2080 name: "libvndk",
2081 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002082 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002083 vndk: {
2084 enabled: true,
2085 },
2086 nocrt: true,
2087 }
2088
2089 cc_library {
2090 name: "libvndk_ext_product",
2091 product_specific: true,
2092 vndk: {
2093 enabled: true,
2094 },
2095 nocrt: true,
2096 }
2097 `)
2098
2099 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
2100 cc_library {
2101 name: "libvndk",
2102 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002103 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002104 vndk: {
2105 enabled: true,
2106 },
2107 nocrt: true,
2108 }
2109
2110 cc_library {
2111 name: "libvndk_ext_product",
2112 product_specific: true,
2113 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002114 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002115 vndk: {
2116 enabled: true,
2117 extends: "libvndk",
2118 },
2119 nocrt: true,
2120 }
2121 `)
Logan Chienf3511742017-10-31 18:04:35 +08002122}
2123
2124func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
2125 // This test ensures an error is emitted for inconsistent support_system_process.
2126 testCcError(t, "module \".*\" with mismatched support_system_process", `
2127 cc_library {
2128 name: "libvndk",
2129 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002130 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002131 vndk: {
2132 enabled: true,
2133 },
2134 nocrt: true,
2135 }
2136
2137 cc_library {
2138 name: "libvndk_sp_ext",
2139 vendor: true,
2140 vndk: {
2141 enabled: true,
2142 extends: "libvndk",
2143 support_system_process: true,
2144 },
2145 nocrt: true,
2146 }
2147 `)
2148
2149 testCcError(t, "module \".*\" with mismatched support_system_process", `
2150 cc_library {
2151 name: "libvndk_sp",
2152 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002153 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002154 vndk: {
2155 enabled: true,
2156 support_system_process: true,
2157 },
2158 nocrt: true,
2159 }
2160
2161 cc_library {
2162 name: "libvndk_ext",
2163 vendor: true,
2164 vndk: {
2165 enabled: true,
2166 extends: "libvndk_sp",
2167 },
2168 nocrt: true,
2169 }
2170 `)
2171}
2172
2173func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08002174 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08002175 // with `vendor_available: false`.
2176 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
2177 cc_library {
2178 name: "libvndk",
2179 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09002180 product_available: false,
Logan Chienf3511742017-10-31 18:04:35 +08002181 vndk: {
2182 enabled: true,
2183 },
2184 nocrt: true,
2185 }
2186
2187 cc_library {
2188 name: "libvndk_ext",
2189 vendor: true,
2190 vndk: {
2191 enabled: true,
2192 extends: "libvndk",
2193 },
2194 nocrt: true,
2195 }
2196 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002197
2198 testCcErrorProductVndk(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
2199 cc_library {
2200 name: "libvndk",
2201 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09002202 product_available: false,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002203 vndk: {
2204 enabled: true,
2205 },
2206 nocrt: true,
2207 }
2208
2209 cc_library {
2210 name: "libvndk_ext_product",
2211 product_specific: true,
2212 vndk: {
2213 enabled: true,
2214 extends: "libvndk",
2215 },
2216 nocrt: true,
2217 }
2218 `)
Logan Chienf3511742017-10-31 18:04:35 +08002219}
2220
Logan Chiend3c59a22018-03-29 14:08:15 +08002221func TestVendorModuleUseVndkExt(t *testing.T) {
2222 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002223 testCc(t, `
2224 cc_library {
2225 name: "libvndk",
2226 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002227 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002228 vndk: {
2229 enabled: true,
2230 },
2231 nocrt: true,
2232 }
2233
2234 cc_library {
2235 name: "libvndk_ext",
2236 vendor: true,
2237 vndk: {
2238 enabled: true,
2239 extends: "libvndk",
2240 },
2241 nocrt: true,
2242 }
2243
2244 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08002245 name: "libvndk_sp",
2246 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002247 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002248 vndk: {
2249 enabled: true,
2250 support_system_process: true,
2251 },
2252 nocrt: true,
2253 }
2254
2255 cc_library {
2256 name: "libvndk_sp_ext",
2257 vendor: true,
2258 vndk: {
2259 enabled: true,
2260 extends: "libvndk_sp",
2261 support_system_process: true,
2262 },
2263 nocrt: true,
2264 }
2265
2266 cc_library {
2267 name: "libvendor",
2268 vendor: true,
2269 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
2270 nocrt: true,
2271 }
2272 `)
2273}
2274
Logan Chiend3c59a22018-03-29 14:08:15 +08002275func TestVndkExtUseVendorLib(t *testing.T) {
2276 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08002277 testCc(t, `
2278 cc_library {
2279 name: "libvndk",
2280 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002281 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002282 vndk: {
2283 enabled: true,
2284 },
2285 nocrt: true,
2286 }
2287
2288 cc_library {
2289 name: "libvndk_ext",
2290 vendor: true,
2291 vndk: {
2292 enabled: true,
2293 extends: "libvndk",
2294 },
2295 shared_libs: ["libvendor"],
2296 nocrt: true,
2297 }
2298
2299 cc_library {
2300 name: "libvendor",
2301 vendor: true,
2302 nocrt: true,
2303 }
2304 `)
Logan Chienf3511742017-10-31 18:04:35 +08002305
Logan Chiend3c59a22018-03-29 14:08:15 +08002306 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
2307 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08002308 cc_library {
2309 name: "libvndk_sp",
2310 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002311 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002312 vndk: {
2313 enabled: true,
2314 support_system_process: true,
2315 },
2316 nocrt: true,
2317 }
2318
2319 cc_library {
2320 name: "libvndk_sp_ext",
2321 vendor: true,
2322 vndk: {
2323 enabled: true,
2324 extends: "libvndk_sp",
2325 support_system_process: true,
2326 },
2327 shared_libs: ["libvendor"], // Cause an error
2328 nocrt: true,
2329 }
2330
2331 cc_library {
2332 name: "libvendor",
2333 vendor: true,
2334 nocrt: true,
2335 }
2336 `)
2337}
2338
Justin Yun0ecf0b22020-02-28 15:07:59 +09002339func TestProductVndkExtDependency(t *testing.T) {
2340 bp := `
2341 cc_library {
2342 name: "libvndk",
2343 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002344 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002345 vndk: {
2346 enabled: true,
2347 },
2348 nocrt: true,
2349 }
2350
2351 cc_library {
2352 name: "libvndk_ext_product",
2353 product_specific: true,
2354 vndk: {
2355 enabled: true,
2356 extends: "libvndk",
2357 },
2358 shared_libs: ["libproduct_for_vndklibs"],
2359 nocrt: true,
2360 }
2361
2362 cc_library {
2363 name: "libvndk_sp",
2364 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002365 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002366 vndk: {
2367 enabled: true,
2368 support_system_process: true,
2369 },
2370 nocrt: true,
2371 }
2372
2373 cc_library {
2374 name: "libvndk_sp_ext_product",
2375 product_specific: true,
2376 vndk: {
2377 enabled: true,
2378 extends: "libvndk_sp",
2379 support_system_process: true,
2380 },
2381 shared_libs: ["libproduct_for_vndklibs"],
2382 nocrt: true,
2383 }
2384
2385 cc_library {
2386 name: "libproduct",
2387 product_specific: true,
2388 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
2389 nocrt: true,
2390 }
2391
2392 cc_library {
2393 name: "libproduct_for_vndklibs",
2394 product_specific: true,
2395 nocrt: true,
2396 }
2397 `
2398 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2399 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2400 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2401 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2402
2403 testCcWithConfig(t, config)
2404}
2405
Logan Chiend3c59a22018-03-29 14:08:15 +08002406func TestVndkSpExtUseVndkError(t *testing.T) {
2407 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
2408 // library.
2409 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2410 cc_library {
2411 name: "libvndk",
2412 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002413 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002414 vndk: {
2415 enabled: true,
2416 },
2417 nocrt: true,
2418 }
2419
2420 cc_library {
2421 name: "libvndk_sp",
2422 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002423 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002424 vndk: {
2425 enabled: true,
2426 support_system_process: true,
2427 },
2428 nocrt: true,
2429 }
2430
2431 cc_library {
2432 name: "libvndk_sp_ext",
2433 vendor: true,
2434 vndk: {
2435 enabled: true,
2436 extends: "libvndk_sp",
2437 support_system_process: true,
2438 },
2439 shared_libs: ["libvndk"], // Cause an error
2440 nocrt: true,
2441 }
2442 `)
2443
2444 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
2445 // library.
2446 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2447 cc_library {
2448 name: "libvndk",
2449 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002450 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002451 vndk: {
2452 enabled: true,
2453 },
2454 nocrt: true,
2455 }
2456
2457 cc_library {
2458 name: "libvndk_ext",
2459 vendor: true,
2460 vndk: {
2461 enabled: true,
2462 extends: "libvndk",
2463 },
2464 nocrt: true,
2465 }
2466
2467 cc_library {
2468 name: "libvndk_sp",
2469 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002470 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002471 vndk: {
2472 enabled: true,
2473 support_system_process: true,
2474 },
2475 nocrt: true,
2476 }
2477
2478 cc_library {
2479 name: "libvndk_sp_ext",
2480 vendor: true,
2481 vndk: {
2482 enabled: true,
2483 extends: "libvndk_sp",
2484 support_system_process: true,
2485 },
2486 shared_libs: ["libvndk_ext"], // Cause an error
2487 nocrt: true,
2488 }
2489 `)
2490}
2491
2492func TestVndkUseVndkExtError(t *testing.T) {
2493 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2494 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002495 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2496 cc_library {
2497 name: "libvndk",
2498 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002499 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002500 vndk: {
2501 enabled: true,
2502 },
2503 nocrt: true,
2504 }
2505
2506 cc_library {
2507 name: "libvndk_ext",
2508 vendor: true,
2509 vndk: {
2510 enabled: true,
2511 extends: "libvndk",
2512 },
2513 nocrt: true,
2514 }
2515
2516 cc_library {
2517 name: "libvndk2",
2518 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002519 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002520 vndk: {
2521 enabled: true,
2522 },
2523 shared_libs: ["libvndk_ext"],
2524 nocrt: true,
2525 }
2526 `)
2527
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002528 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002529 cc_library {
2530 name: "libvndk",
2531 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002532 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002533 vndk: {
2534 enabled: true,
2535 },
2536 nocrt: true,
2537 }
2538
2539 cc_library {
2540 name: "libvndk_ext",
2541 vendor: true,
2542 vndk: {
2543 enabled: true,
2544 extends: "libvndk",
2545 },
2546 nocrt: true,
2547 }
2548
2549 cc_library {
2550 name: "libvndk2",
2551 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002552 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002553 vndk: {
2554 enabled: true,
2555 },
2556 target: {
2557 vendor: {
2558 shared_libs: ["libvndk_ext"],
2559 },
2560 },
2561 nocrt: true,
2562 }
2563 `)
2564
2565 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2566 cc_library {
2567 name: "libvndk_sp",
2568 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002569 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002570 vndk: {
2571 enabled: true,
2572 support_system_process: true,
2573 },
2574 nocrt: true,
2575 }
2576
2577 cc_library {
2578 name: "libvndk_sp_ext",
2579 vendor: true,
2580 vndk: {
2581 enabled: true,
2582 extends: "libvndk_sp",
2583 support_system_process: true,
2584 },
2585 nocrt: true,
2586 }
2587
2588 cc_library {
2589 name: "libvndk_sp_2",
2590 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002591 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002592 vndk: {
2593 enabled: true,
2594 support_system_process: true,
2595 },
2596 shared_libs: ["libvndk_sp_ext"],
2597 nocrt: true,
2598 }
2599 `)
2600
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002601 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002602 cc_library {
2603 name: "libvndk_sp",
2604 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002605 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002606 vndk: {
2607 enabled: true,
2608 },
2609 nocrt: true,
2610 }
2611
2612 cc_library {
2613 name: "libvndk_sp_ext",
2614 vendor: true,
2615 vndk: {
2616 enabled: true,
2617 extends: "libvndk_sp",
2618 },
2619 nocrt: true,
2620 }
2621
2622 cc_library {
2623 name: "libvndk_sp2",
2624 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002625 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002626 vndk: {
2627 enabled: true,
2628 },
2629 target: {
2630 vendor: {
2631 shared_libs: ["libvndk_sp_ext"],
2632 },
2633 },
2634 nocrt: true,
2635 }
2636 `)
2637}
2638
Justin Yun5f7f7e82019-11-18 19:52:14 +09002639func TestEnforceProductVndkVersion(t *testing.T) {
2640 bp := `
2641 cc_library {
2642 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002643 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002644 }
2645 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002646 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002647 symbol_file: "",
2648 }
2649 cc_library {
2650 name: "libvndk",
2651 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002652 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002653 vndk: {
2654 enabled: true,
2655 },
2656 nocrt: true,
2657 }
2658 cc_library {
2659 name: "libvndk_sp",
2660 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002661 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002662 vndk: {
2663 enabled: true,
2664 support_system_process: true,
2665 },
2666 nocrt: true,
2667 }
2668 cc_library {
2669 name: "libva",
2670 vendor_available: true,
2671 nocrt: true,
2672 }
2673 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002674 name: "libpa",
2675 product_available: true,
2676 nocrt: true,
2677 }
2678 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002679 name: "libproduct_va",
2680 product_specific: true,
2681 vendor_available: true,
2682 nocrt: true,
2683 }
2684 cc_library {
2685 name: "libprod",
2686 product_specific: true,
2687 shared_libs: [
2688 "libllndk",
2689 "libvndk",
2690 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002691 "libpa",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002692 "libproduct_va",
2693 ],
2694 nocrt: true,
2695 }
2696 cc_library {
2697 name: "libvendor",
2698 vendor: true,
2699 shared_libs: [
2700 "libllndk",
2701 "libvndk",
2702 "libvndk_sp",
2703 "libva",
2704 "libproduct_va",
2705 ],
2706 nocrt: true,
2707 }
2708 `
2709
2710 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2711 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2712 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2713 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2714
2715 ctx := testCcWithConfig(t, config)
2716
Jooyung Han261e1582020-10-20 18:54:21 +09002717 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2718 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun5f7f7e82019-11-18 19:52:14 +09002719}
2720
2721func TestEnforceProductVndkVersionErrors(t *testing.T) {
2722 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2723 cc_library {
2724 name: "libprod",
2725 product_specific: true,
2726 shared_libs: [
2727 "libvendor",
2728 ],
2729 nocrt: true,
2730 }
2731 cc_library {
2732 name: "libvendor",
2733 vendor: true,
2734 nocrt: true,
2735 }
2736 `)
2737 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2738 cc_library {
2739 name: "libprod",
2740 product_specific: true,
2741 shared_libs: [
2742 "libsystem",
2743 ],
2744 nocrt: true,
2745 }
2746 cc_library {
2747 name: "libsystem",
2748 nocrt: true,
2749 }
2750 `)
2751 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
2752 cc_library {
2753 name: "libprod",
2754 product_specific: true,
2755 shared_libs: [
2756 "libvndk_private",
2757 ],
2758 nocrt: true,
2759 }
2760 cc_library {
2761 name: "libvndk_private",
2762 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09002763 product_available: false,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002764 vndk: {
2765 enabled: true,
2766 },
2767 nocrt: true,
2768 }
2769 `)
2770 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2771 cc_library {
2772 name: "libprod",
2773 product_specific: true,
2774 shared_libs: [
2775 "libsystem_ext",
2776 ],
2777 nocrt: true,
2778 }
2779 cc_library {
2780 name: "libsystem_ext",
2781 system_ext_specific: true,
2782 nocrt: true,
2783 }
2784 `)
2785 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2786 cc_library {
2787 name: "libsystem",
2788 shared_libs: [
2789 "libproduct_va",
2790 ],
2791 nocrt: true,
2792 }
2793 cc_library {
2794 name: "libproduct_va",
2795 product_specific: true,
2796 vendor_available: true,
2797 nocrt: true,
2798 }
2799 `)
2800}
2801
Jooyung Han38002912019-05-16 04:01:54 +09002802func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002803 bp := `
2804 cc_library {
2805 name: "libvndk",
2806 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002807 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002808 vndk: {
2809 enabled: true,
2810 },
2811 }
2812 cc_library {
2813 name: "libvndksp",
2814 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002815 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002816 vndk: {
2817 enabled: true,
2818 support_system_process: true,
2819 },
2820 }
2821 cc_library {
2822 name: "libvndkprivate",
2823 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09002824 product_available: false,
Colin Cross98be1bb2019-12-13 20:41:13 -08002825 vndk: {
2826 enabled: true,
2827 },
2828 }
2829 cc_library {
2830 name: "libvendor",
2831 vendor: true,
2832 }
2833 cc_library {
2834 name: "libvndkext",
2835 vendor: true,
2836 vndk: {
2837 enabled: true,
2838 extends: "libvndk",
2839 },
2840 }
2841 vndk_prebuilt_shared {
2842 name: "prevndk",
2843 version: "27",
2844 target_arch: "arm",
2845 binder32bit: true,
2846 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002847 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002848 vndk: {
2849 enabled: true,
2850 },
2851 arch: {
2852 arm: {
2853 srcs: ["liba.so"],
2854 },
2855 },
2856 }
2857 cc_library {
2858 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002859 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002860 }
2861 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002862 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002863 symbol_file: "",
2864 }
2865 cc_library {
2866 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07002867 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002868 }
2869 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002870 name: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002871 vendor_available: false,
2872 symbol_file: "",
2873 }`
2874
2875 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002876 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2877 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2878 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002879 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002880
Jooyung Han0302a842019-10-30 18:43:49 +09002881 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002882 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09002883 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002884 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09002885 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002886 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09002887 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002888 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09002889
Colin Crossfb0c16e2019-11-20 17:12:35 -08002890 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002891
Jooyung Han38002912019-05-16 04:01:54 +09002892 tests := []struct {
2893 variant string
2894 name string
2895 expected string
2896 }{
2897 {vendorVariant, "libvndk", "native:vndk"},
2898 {vendorVariant, "libvndksp", "native:vndk"},
2899 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2900 {vendorVariant, "libvendor", "native:vendor"},
2901 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09002902 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002903 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002904 {coreVariant, "libvndk", "native:platform"},
2905 {coreVariant, "libvndkprivate", "native:platform"},
2906 {coreVariant, "libllndk", "native:platform"},
2907 }
2908 for _, test := range tests {
2909 t.Run(test.name, func(t *testing.T) {
2910 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2911 assertString(t, module.makeLinkType, test.expected)
2912 })
2913 }
2914}
2915
Colin Cross0af4b842015-04-30 16:36:18 -07002916var (
2917 str11 = "01234567891"
2918 str10 = str11[:10]
2919 str9 = str11[:9]
2920 str5 = str11[:5]
2921 str4 = str11[:4]
2922)
2923
2924var splitListForSizeTestCases = []struct {
2925 in []string
2926 out [][]string
2927 size int
2928}{
2929 {
2930 in: []string{str10},
2931 out: [][]string{{str10}},
2932 size: 10,
2933 },
2934 {
2935 in: []string{str9},
2936 out: [][]string{{str9}},
2937 size: 10,
2938 },
2939 {
2940 in: []string{str5},
2941 out: [][]string{{str5}},
2942 size: 10,
2943 },
2944 {
2945 in: []string{str11},
2946 out: nil,
2947 size: 10,
2948 },
2949 {
2950 in: []string{str10, str10},
2951 out: [][]string{{str10}, {str10}},
2952 size: 10,
2953 },
2954 {
2955 in: []string{str9, str10},
2956 out: [][]string{{str9}, {str10}},
2957 size: 10,
2958 },
2959 {
2960 in: []string{str10, str9},
2961 out: [][]string{{str10}, {str9}},
2962 size: 10,
2963 },
2964 {
2965 in: []string{str5, str4},
2966 out: [][]string{{str5, str4}},
2967 size: 10,
2968 },
2969 {
2970 in: []string{str5, str4, str5},
2971 out: [][]string{{str5, str4}, {str5}},
2972 size: 10,
2973 },
2974 {
2975 in: []string{str5, str4, str5, str4},
2976 out: [][]string{{str5, str4}, {str5, str4}},
2977 size: 10,
2978 },
2979 {
2980 in: []string{str5, str4, str5, str5},
2981 out: [][]string{{str5, str4}, {str5}, {str5}},
2982 size: 10,
2983 },
2984 {
2985 in: []string{str5, str5, str5, str4},
2986 out: [][]string{{str5}, {str5}, {str5, str4}},
2987 size: 10,
2988 },
2989 {
2990 in: []string{str9, str11},
2991 out: nil,
2992 size: 10,
2993 },
2994 {
2995 in: []string{str11, str9},
2996 out: nil,
2997 size: 10,
2998 },
2999}
3000
3001func TestSplitListForSize(t *testing.T) {
3002 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08003003 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07003004
3005 var outStrings [][]string
3006
3007 if len(out) > 0 {
3008 outStrings = make([][]string, len(out))
3009 for i, o := range out {
3010 outStrings[i] = o.Strings()
3011 }
3012 }
3013
3014 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07003015 t.Errorf("incorrect output:")
3016 t.Errorf(" input: %#v", testCase.in)
3017 t.Errorf(" size: %d", testCase.size)
3018 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07003019 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07003020 }
3021 }
3022}
Jeff Gaston294356f2017-09-27 17:05:30 -07003023
3024var staticLinkDepOrderTestCases = []struct {
3025 // This is a string representation of a map[moduleName][]moduleDependency .
3026 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003027 inStatic string
3028
3029 // This is a string representation of a map[moduleName][]moduleDependency .
3030 // It models the dependencies declared in an Android.bp file.
3031 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07003032
3033 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
3034 // The keys of allOrdered specify which modules we would like to check.
3035 // The values of allOrdered specify the expected result (of the transitive closure of all
3036 // dependencies) for each module to test
3037 allOrdered string
3038
3039 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
3040 // The keys of outOrdered specify which modules we would like to check.
3041 // The values of outOrdered specify the expected result (of the ordered linker command line)
3042 // for each module to test.
3043 outOrdered string
3044}{
3045 // Simple tests
3046 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003047 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07003048 outOrdered: "",
3049 },
3050 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003051 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003052 outOrdered: "a:",
3053 },
3054 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003055 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003056 outOrdered: "a:b; b:",
3057 },
3058 // Tests of reordering
3059 {
3060 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003061 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003062 outOrdered: "a:b,c,d; b:d; c:d; d:",
3063 },
3064 {
3065 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003066 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003067 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
3068 },
3069 {
3070 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003071 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07003072 outOrdered: "a:d,b,e,c; d:b; e:c",
3073 },
3074 {
3075 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003076 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07003077 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
3078 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
3079 },
3080 {
3081 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003082 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 -07003083 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
3084 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
3085 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003086 // shared dependencies
3087 {
3088 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
3089 // So, we don't actually have to check that a shared dependency of c will change the order
3090 // of a library that depends statically on b and on c. We only need to check that if c has
3091 // a shared dependency on b, that that shows up in allOrdered.
3092 inShared: "c:b",
3093 allOrdered: "c:b",
3094 outOrdered: "c:",
3095 },
3096 {
3097 // This test doesn't actually include any shared dependencies but it's a reminder of what
3098 // the second phase of the above test would look like
3099 inStatic: "a:b,c; c:b",
3100 allOrdered: "a:c,b; c:b",
3101 outOrdered: "a:c,b; c:b",
3102 },
Jeff Gaston294356f2017-09-27 17:05:30 -07003103 // tiebreakers for when two modules specifying different orderings and there is no dependency
3104 // to dictate an order
3105 {
3106 // 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 -08003107 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07003108 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
3109 },
3110 {
3111 // 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 -08003112 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 -07003113 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
3114 },
3115 // Tests involving duplicate dependencies
3116 {
3117 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003118 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003119 outOrdered: "a:c,b",
3120 },
3121 {
3122 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003123 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003124 outOrdered: "a:d,c,b",
3125 },
3126 // Tests to confirm the nonexistence of infinite loops.
3127 // These cases should never happen, so as long as the test terminates and the
3128 // result is deterministic then that should be fine.
3129 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003130 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003131 outOrdered: "a:a",
3132 },
3133 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003134 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003135 allOrdered: "a:b,c; b:c,a; c:a,b",
3136 outOrdered: "a:b; b:c; c:a",
3137 },
3138 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003139 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003140 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
3141 outOrdered: "a:c,b; b:a,c; c:b,a",
3142 },
3143}
3144
3145// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
3146func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
3147 // convert from "a:b,c; d:e" to "a:b,c;d:e"
3148 strippedText := strings.Replace(text, " ", "", -1)
3149 if len(strippedText) < 1 {
3150 return []android.Path{}, make(map[android.Path][]android.Path, 0)
3151 }
3152 allDeps = make(map[android.Path][]android.Path, 0)
3153
3154 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
3155 moduleTexts := strings.Split(strippedText, ";")
3156
3157 outputForModuleName := func(moduleName string) android.Path {
3158 return android.PathForTesting(moduleName)
3159 }
3160
3161 for _, moduleText := range moduleTexts {
3162 // convert from "a:b,c" to ["a", "b,c"]
3163 components := strings.Split(moduleText, ":")
3164 if len(components) != 2 {
3165 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
3166 }
3167 moduleName := components[0]
3168 moduleOutput := outputForModuleName(moduleName)
3169 modulesInOrder = append(modulesInOrder, moduleOutput)
3170
3171 depString := components[1]
3172 // convert from "b,c" to ["b", "c"]
3173 depNames := strings.Split(depString, ",")
3174 if len(depString) < 1 {
3175 depNames = []string{}
3176 }
3177 var deps []android.Path
3178 for _, depName := range depNames {
3179 deps = append(deps, outputForModuleName(depName))
3180 }
3181 allDeps[moduleOutput] = deps
3182 }
3183 return modulesInOrder, allDeps
3184}
3185
Jeff Gaston294356f2017-09-27 17:05:30 -07003186func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
3187 for _, moduleName := range moduleNames {
3188 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
3189 output := module.outputFile.Path()
3190 paths = append(paths, output)
3191 }
3192 return paths
3193}
3194
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003195func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07003196 ctx := testCc(t, `
3197 cc_library {
3198 name: "a",
3199 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09003200 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003201 }
3202 cc_library {
3203 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003204 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003205 }
3206 cc_library {
3207 name: "c",
3208 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003209 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003210 }
3211 cc_library {
3212 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09003213 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003214 }
3215
3216 `)
3217
Colin Cross7113d202019-11-20 16:39:12 -08003218 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07003219 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003220 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3221 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07003222
3223 if !reflect.DeepEqual(actual, expected) {
3224 t.Errorf("staticDeps orderings were not propagated correctly"+
3225 "\nactual: %v"+
3226 "\nexpected: %v",
3227 actual,
3228 expected,
3229 )
3230 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09003231}
Jeff Gaston294356f2017-09-27 17:05:30 -07003232
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003233func TestStaticLibDepReorderingWithShared(t *testing.T) {
3234 ctx := testCc(t, `
3235 cc_library {
3236 name: "a",
3237 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09003238 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003239 }
3240 cc_library {
3241 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003242 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003243 }
3244 cc_library {
3245 name: "c",
3246 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003247 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003248 }
3249
3250 `)
3251
Colin Cross7113d202019-11-20 16:39:12 -08003252 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003253 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003254 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3255 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003256
3257 if !reflect.DeepEqual(actual, expected) {
3258 t.Errorf("staticDeps orderings did not account for shared libs"+
3259 "\nactual: %v"+
3260 "\nexpected: %v",
3261 actual,
3262 expected,
3263 )
3264 }
3265}
3266
Jooyung Hanb04a4992020-03-13 18:57:35 +09003267func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07003268 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003269 if !reflect.DeepEqual(actual, expected) {
3270 t.Errorf(message+
3271 "\nactual: %v"+
3272 "\nexpected: %v",
3273 actual,
3274 expected,
3275 )
3276 }
3277}
3278
Jooyung Han61b66e92020-03-21 14:21:46 +00003279func TestLlndkLibrary(t *testing.T) {
3280 ctx := testCc(t, `
3281 cc_library {
3282 name: "libllndk",
3283 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07003284 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003285 }
3286 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003287 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003288 }
3289 `)
3290 actual := ctx.ModuleVariantsForTests("libllndk.llndk")
3291 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00003292 "android_vendor.VER_arm64_armv8-a_shared_1",
3293 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003294 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003295 "android_vendor.VER_arm_armv7-a-neon_shared_1",
3296 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003297 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003298 }
3299 checkEquals(t, "variants for llndk stubs", expected, actual)
3300
3301 params := ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
3302 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
3303
3304 params = ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
3305 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
3306}
3307
Jiyong Parka46a4d52017-12-14 19:54:34 +09003308func TestLlndkHeaders(t *testing.T) {
3309 ctx := testCc(t, `
3310 llndk_headers {
3311 name: "libllndk_headers",
3312 export_include_dirs: ["my_include"],
3313 }
3314 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003315 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09003316 export_llndk_headers: ["libllndk_headers"],
3317 }
3318 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07003319 name: "libllndk",
3320 llndk_stubs: "libllndk.llndk",
3321 }
3322
3323 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09003324 name: "libvendor",
3325 shared_libs: ["libllndk"],
3326 vendor: true,
3327 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003328 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08003329 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09003330 }
3331 `)
3332
3333 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08003334 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09003335 cflags := cc.Args["cFlags"]
3336 if !strings.Contains(cflags, "-Imy_include") {
3337 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
3338 }
3339}
3340
Logan Chien43d34c32017-12-20 01:17:32 +08003341func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
3342 actual := module.Properties.AndroidMkRuntimeLibs
3343 if !reflect.DeepEqual(actual, expected) {
3344 t.Errorf("incorrect runtime_libs for shared libs"+
3345 "\nactual: %v"+
3346 "\nexpected: %v",
3347 actual,
3348 expected,
3349 )
3350 }
3351}
3352
3353const runtimeLibAndroidBp = `
3354 cc_library {
3355 name: "libvendor_available1",
3356 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003357 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003358 nocrt : true,
3359 system_shared_libs : [],
3360 }
3361 cc_library {
3362 name: "libvendor_available2",
3363 vendor_available: true,
3364 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003365 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003366 nocrt : true,
3367 system_shared_libs : [],
3368 }
3369 cc_library {
3370 name: "libvendor_available3",
3371 vendor_available: true,
3372 runtime_libs: ["libvendor_available1"],
3373 target: {
3374 vendor: {
3375 exclude_runtime_libs: ["libvendor_available1"],
3376 }
3377 },
Yi Konge7fe9912019-06-02 00:53:50 -07003378 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003379 nocrt : true,
3380 system_shared_libs : [],
3381 }
3382 cc_library {
3383 name: "libcore",
3384 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003385 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003386 nocrt : true,
3387 system_shared_libs : [],
3388 }
3389 cc_library {
3390 name: "libvendor1",
3391 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003392 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003393 nocrt : true,
3394 system_shared_libs : [],
3395 }
3396 cc_library {
3397 name: "libvendor2",
3398 vendor: true,
3399 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003400 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003401 nocrt : true,
3402 system_shared_libs : [],
3403 }
3404`
3405
3406func TestRuntimeLibs(t *testing.T) {
3407 ctx := testCc(t, runtimeLibAndroidBp)
3408
3409 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08003410 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003411
3412 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3413 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3414
3415 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
3416 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3417
3418 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3419 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08003420 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003421
3422 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3423 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
3424
3425 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
3426 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
3427}
3428
3429func TestExcludeRuntimeLibs(t *testing.T) {
3430 ctx := testCc(t, runtimeLibAndroidBp)
3431
Colin Cross7113d202019-11-20 16:39:12 -08003432 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003433 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
3434 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3435
Colin Crossfb0c16e2019-11-20 17:12:35 -08003436 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003437 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
3438 checkRuntimeLibs(t, nil, module)
3439}
3440
3441func TestRuntimeLibsNoVndk(t *testing.T) {
3442 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3443
3444 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3445
Colin Cross7113d202019-11-20 16:39:12 -08003446 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003447
3448 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3449 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3450
3451 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
3452 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
3453}
3454
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003455func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003456 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003457 actual := module.Properties.AndroidMkStaticLibs
3458 if !reflect.DeepEqual(actual, expected) {
3459 t.Errorf("incorrect static_libs"+
3460 "\nactual: %v"+
3461 "\nexpected: %v",
3462 actual,
3463 expected,
3464 )
3465 }
3466}
3467
3468const staticLibAndroidBp = `
3469 cc_library {
3470 name: "lib1",
3471 }
3472 cc_library {
3473 name: "lib2",
3474 static_libs: ["lib1"],
3475 }
3476`
3477
3478func TestStaticLibDepExport(t *testing.T) {
3479 ctx := testCc(t, staticLibAndroidBp)
3480
3481 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003482 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003483 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003484 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003485
3486 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003487 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003488 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3489 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003490 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003491}
3492
Jiyong Parkd08b6972017-09-26 10:50:54 +09003493var compilerFlagsTestCases = []struct {
3494 in string
3495 out bool
3496}{
3497 {
3498 in: "a",
3499 out: false,
3500 },
3501 {
3502 in: "-a",
3503 out: true,
3504 },
3505 {
3506 in: "-Ipath/to/something",
3507 out: false,
3508 },
3509 {
3510 in: "-isystempath/to/something",
3511 out: false,
3512 },
3513 {
3514 in: "--coverage",
3515 out: false,
3516 },
3517 {
3518 in: "-include a/b",
3519 out: true,
3520 },
3521 {
3522 in: "-include a/b c/d",
3523 out: false,
3524 },
3525 {
3526 in: "-DMACRO",
3527 out: true,
3528 },
3529 {
3530 in: "-DMAC RO",
3531 out: false,
3532 },
3533 {
3534 in: "-a -b",
3535 out: false,
3536 },
3537 {
3538 in: "-DMACRO=definition",
3539 out: true,
3540 },
3541 {
3542 in: "-DMACRO=defi nition",
3543 out: true, // TODO(jiyong): this should be false
3544 },
3545 {
3546 in: "-DMACRO(x)=x + 1",
3547 out: true,
3548 },
3549 {
3550 in: "-DMACRO=\"defi nition\"",
3551 out: true,
3552 },
3553}
3554
3555type mockContext struct {
3556 BaseModuleContext
3557 result bool
3558}
3559
3560func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3561 // CheckBadCompilerFlags calls this function when the flag should be rejected
3562 ctx.result = false
3563}
3564
3565func TestCompilerFlags(t *testing.T) {
3566 for _, testCase := range compilerFlagsTestCases {
3567 ctx := &mockContext{result: true}
3568 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3569 if ctx.result != testCase.out {
3570 t.Errorf("incorrect output:")
3571 t.Errorf(" input: %#v", testCase.in)
3572 t.Errorf(" expected: %#v", testCase.out)
3573 t.Errorf(" got: %#v", ctx.result)
3574 }
3575 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003576}
Jiyong Park374510b2018-03-19 18:23:01 +09003577
3578func TestVendorPublicLibraries(t *testing.T) {
3579 ctx := testCc(t, `
3580 cc_library_headers {
3581 name: "libvendorpublic_headers",
3582 export_include_dirs: ["my_include"],
3583 }
3584 vendor_public_library {
3585 name: "libvendorpublic",
3586 symbol_file: "",
3587 export_public_headers: ["libvendorpublic_headers"],
3588 }
3589 cc_library {
3590 name: "libvendorpublic",
3591 srcs: ["foo.c"],
3592 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003593 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003594 nocrt: true,
3595 }
3596
3597 cc_library {
3598 name: "libsystem",
3599 shared_libs: ["libvendorpublic"],
3600 vendor: false,
3601 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003602 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003603 nocrt: true,
3604 }
3605 cc_library {
3606 name: "libvendor",
3607 shared_libs: ["libvendorpublic"],
3608 vendor: true,
3609 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003610 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003611 nocrt: true,
3612 }
3613 `)
3614
Colin Cross7113d202019-11-20 16:39:12 -08003615 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003616 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003617
3618 // test if header search paths are correctly added
3619 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003620 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003621 cflags := cc.Args["cFlags"]
3622 if !strings.Contains(cflags, "-Imy_include") {
3623 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3624 }
3625
3626 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003627 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003628 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003629 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003630 if !strings.Contains(libflags, stubPaths[0].String()) {
3631 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3632 }
3633
3634 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003635 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003636 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003637 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003638 if !strings.Contains(libflags, stubPaths[0].String()) {
3639 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3640 }
3641
3642}
Jiyong Park37b25202018-07-11 10:49:27 +09003643
3644func TestRecovery(t *testing.T) {
3645 ctx := testCc(t, `
3646 cc_library_shared {
3647 name: "librecovery",
3648 recovery: true,
3649 }
3650 cc_library_shared {
3651 name: "librecovery32",
3652 recovery: true,
3653 compile_multilib:"32",
3654 }
Jiyong Park5baac542018-08-28 09:55:37 +09003655 cc_library_shared {
3656 name: "libHalInRecovery",
3657 recovery_available: true,
3658 vendor: true,
3659 }
Jiyong Park37b25202018-07-11 10:49:27 +09003660 `)
3661
3662 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003663 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003664 if len(variants) != 1 || !android.InList(arm64, variants) {
3665 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3666 }
3667
3668 variants = ctx.ModuleVariantsForTests("librecovery32")
3669 if android.InList(arm64, variants) {
3670 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3671 }
Jiyong Park5baac542018-08-28 09:55:37 +09003672
3673 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3674 if !recoveryModule.Platform() {
3675 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3676 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003677}
Jiyong Park5baac542018-08-28 09:55:37 +09003678
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003679func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3680 bp := `
3681 cc_prebuilt_test_library_shared {
3682 name: "test_lib",
3683 relative_install_path: "foo/bar/baz",
3684 srcs: ["srcpath/dontusethispath/baz.so"],
3685 }
3686
3687 cc_test {
3688 name: "main_test",
3689 data_libs: ["test_lib"],
3690 gtest: false,
3691 }
3692 `
3693
3694 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3695 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3696 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3697 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3698
3699 ctx := testCcWithConfig(t, config)
3700 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3701 testBinary := module.(*Module).linker.(*testBinary)
3702 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3703 if err != nil {
3704 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3705 }
3706 if len(outputFiles) != 1 {
3707 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3708 }
3709 if len(testBinary.dataPaths()) != 1 {
3710 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3711 }
3712
3713 outputPath := outputFiles[0].String()
3714
3715 if !strings.HasSuffix(outputPath, "/main_test") {
3716 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3717 }
3718 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
3719 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3720 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3721 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3722 }
3723}
3724
Jiyong Park7ed9de32018-10-15 22:25:07 +09003725func TestVersionedStubs(t *testing.T) {
3726 ctx := testCc(t, `
3727 cc_library_shared {
3728 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003729 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003730 stubs: {
3731 symbol_file: "foo.map.txt",
3732 versions: ["1", "2", "3"],
3733 },
3734 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003735
Jiyong Park7ed9de32018-10-15 22:25:07 +09003736 cc_library_shared {
3737 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003738 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003739 shared_libs: ["libFoo#1"],
3740 }`)
3741
3742 variants := ctx.ModuleVariantsForTests("libFoo")
3743 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003744 "android_arm64_armv8-a_shared",
3745 "android_arm64_armv8-a_shared_1",
3746 "android_arm64_armv8-a_shared_2",
3747 "android_arm64_armv8-a_shared_3",
3748 "android_arm_armv7-a-neon_shared",
3749 "android_arm_armv7-a-neon_shared_1",
3750 "android_arm_armv7-a-neon_shared_2",
3751 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003752 }
3753 variantsMismatch := false
3754 if len(variants) != len(expectedVariants) {
3755 variantsMismatch = true
3756 } else {
3757 for _, v := range expectedVariants {
3758 if !inList(v, variants) {
3759 variantsMismatch = false
3760 }
3761 }
3762 }
3763 if variantsMismatch {
3764 t.Errorf("variants of libFoo expected:\n")
3765 for _, v := range expectedVariants {
3766 t.Errorf("%q\n", v)
3767 }
3768 t.Errorf(", but got:\n")
3769 for _, v := range variants {
3770 t.Errorf("%q\n", v)
3771 }
3772 }
3773
Colin Cross7113d202019-11-20 16:39:12 -08003774 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003775 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003776 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003777 if !strings.Contains(libFlags, libFoo1StubPath) {
3778 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3779 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003780
Colin Cross7113d202019-11-20 16:39:12 -08003781 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003782 cFlags := libBarCompileRule.Args["cFlags"]
3783 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3784 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3785 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3786 }
Jiyong Park37b25202018-07-11 10:49:27 +09003787}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003788
Jooyung Hanb04a4992020-03-13 18:57:35 +09003789func TestVersioningMacro(t *testing.T) {
3790 for _, tc := range []struct{ moduleName, expected string }{
3791 {"libc", "__LIBC_API__"},
3792 {"libfoo", "__LIBFOO_API__"},
3793 {"libfoo@1", "__LIBFOO_1_API__"},
3794 {"libfoo-v1", "__LIBFOO_V1_API__"},
3795 {"libfoo.v1", "__LIBFOO_V1_API__"},
3796 } {
3797 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3798 }
3799}
3800
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003801func TestStaticExecutable(t *testing.T) {
3802 ctx := testCc(t, `
3803 cc_binary {
3804 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003805 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003806 static_executable: true,
3807 }`)
3808
Colin Cross7113d202019-11-20 16:39:12 -08003809 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003810 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3811 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003812 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003813 for _, lib := range systemStaticLibs {
3814 if !strings.Contains(libFlags, lib) {
3815 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3816 }
3817 }
3818 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3819 for _, lib := range systemSharedLibs {
3820 if strings.Contains(libFlags, lib) {
3821 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3822 }
3823 }
3824}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003825
3826func TestStaticDepsOrderWithStubs(t *testing.T) {
3827 ctx := testCc(t, `
3828 cc_binary {
3829 name: "mybin",
3830 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003831 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003832 static_executable: true,
3833 stl: "none",
3834 }
3835
3836 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003837 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003838 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003839 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003840 stl: "none",
3841 }
3842
3843 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003844 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003845 srcs: ["foo.c"],
3846 stl: "none",
3847 stubs: {
3848 versions: ["1"],
3849 },
3850 }`)
3851
Colin Cross0de8a1e2020-09-18 14:15:30 -07003852 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3853 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003854 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003855
3856 if !reflect.DeepEqual(actual, expected) {
3857 t.Errorf("staticDeps orderings were not propagated correctly"+
3858 "\nactual: %v"+
3859 "\nexpected: %v",
3860 actual,
3861 expected,
3862 )
3863 }
3864}
Jooyung Han38002912019-05-16 04:01:54 +09003865
Jooyung Hand48f3c32019-08-23 11:18:57 +09003866func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3867 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3868 cc_library {
3869 name: "libA",
3870 srcs: ["foo.c"],
3871 shared_libs: ["libB"],
3872 stl: "none",
3873 }
3874
3875 cc_library {
3876 name: "libB",
3877 srcs: ["foo.c"],
3878 enabled: false,
3879 stl: "none",
3880 }
3881 `)
3882}
3883
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003884// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3885// correctly.
3886func TestFuzzTarget(t *testing.T) {
3887 ctx := testCc(t, `
3888 cc_fuzz {
3889 name: "fuzz_smoke_test",
3890 srcs: ["foo.c"],
3891 }`)
3892
Paul Duffin075c4172019-12-19 19:06:13 +00003893 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003894 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3895}
3896
Jiyong Park29074592019-07-07 16:27:47 +09003897func TestAidl(t *testing.T) {
3898}
3899
Jooyung Han38002912019-05-16 04:01:54 +09003900func assertString(t *testing.T, got, expected string) {
3901 t.Helper()
3902 if got != expected {
3903 t.Errorf("expected %q got %q", expected, got)
3904 }
3905}
3906
3907func assertArrayString(t *testing.T, got, expected []string) {
3908 t.Helper()
3909 if len(got) != len(expected) {
3910 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3911 return
3912 }
3913 for i := range got {
3914 if got[i] != expected[i] {
3915 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3916 i, expected[i], expected, got[i], got)
3917 return
3918 }
3919 }
3920}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003921
Jooyung Han0302a842019-10-30 18:43:49 +09003922func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3923 t.Helper()
3924 assertArrayString(t, android.SortedStringKeys(m), expected)
3925}
3926
Colin Crosse1bb5d02019-09-24 14:55:04 -07003927func TestDefaults(t *testing.T) {
3928 ctx := testCc(t, `
3929 cc_defaults {
3930 name: "defaults",
3931 srcs: ["foo.c"],
3932 static: {
3933 srcs: ["bar.c"],
3934 },
3935 shared: {
3936 srcs: ["baz.c"],
3937 },
3938 }
3939
3940 cc_library_static {
3941 name: "libstatic",
3942 defaults: ["defaults"],
3943 }
3944
3945 cc_library_shared {
3946 name: "libshared",
3947 defaults: ["defaults"],
3948 }
3949
3950 cc_library {
3951 name: "libboth",
3952 defaults: ["defaults"],
3953 }
3954
3955 cc_binary {
3956 name: "binary",
3957 defaults: ["defaults"],
3958 }`)
3959
3960 pathsToBase := func(paths android.Paths) []string {
3961 var ret []string
3962 for _, p := range paths {
3963 ret = append(ret, p.Base())
3964 }
3965 return ret
3966 }
3967
Colin Cross7113d202019-11-20 16:39:12 -08003968 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003969 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3970 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3971 }
Colin Cross7113d202019-11-20 16:39:12 -08003972 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003973 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3974 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3975 }
Colin Cross7113d202019-11-20 16:39:12 -08003976 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003977 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3978 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3979 }
3980
Colin Cross7113d202019-11-20 16:39:12 -08003981 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003982 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3983 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3984 }
Colin Cross7113d202019-11-20 16:39:12 -08003985 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003986 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3987 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3988 }
3989}
Colin Crosseabaedd2020-02-06 17:01:55 -08003990
3991func TestProductVariableDefaults(t *testing.T) {
3992 bp := `
3993 cc_defaults {
3994 name: "libfoo_defaults",
3995 srcs: ["foo.c"],
3996 cppflags: ["-DFOO"],
3997 product_variables: {
3998 debuggable: {
3999 cppflags: ["-DBAR"],
4000 },
4001 },
4002 }
4003
4004 cc_library {
4005 name: "libfoo",
4006 defaults: ["libfoo_defaults"],
4007 }
4008 `
4009
4010 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4011 config.TestProductVariables.Debuggable = BoolPtr(true)
4012
Colin Crossae8600b2020-10-29 17:09:13 -07004013 ctx := CreateTestContext(config)
Colin Crosseabaedd2020-02-06 17:01:55 -08004014 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
4015 ctx.BottomUp("variable", android.VariableMutator).Parallel()
4016 })
Colin Crossae8600b2020-10-29 17:09:13 -07004017 ctx.Register()
Colin Crosseabaedd2020-02-06 17:01:55 -08004018
4019 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
4020 android.FailIfErrored(t, errs)
4021 _, errs = ctx.PrepareBuildActions(config)
4022 android.FailIfErrored(t, errs)
4023
4024 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
4025 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
4026 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
4027 }
4028}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004029
4030func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4031 t.Parallel()
4032 bp := `
4033 cc_library_static {
4034 name: "libfoo",
4035 srcs: ["foo.c"],
4036 whole_static_libs: ["libbar"],
4037 }
4038
4039 cc_library_static {
4040 name: "libbar",
4041 whole_static_libs: ["libmissing"],
4042 }
4043 `
4044
4045 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4046 config.TestProductVariables.Allow_missing_dependencies = BoolPtr(true)
4047
Colin Crossae8600b2020-10-29 17:09:13 -07004048 ctx := CreateTestContext(config)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004049 ctx.SetAllowMissingDependencies(true)
Colin Crossae8600b2020-10-29 17:09:13 -07004050 ctx.Register()
Colin Crosse4f6eba2020-09-22 18:11:25 -07004051
4052 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
4053 android.FailIfErrored(t, errs)
4054 _, errs = ctx.PrepareBuildActions(config)
4055 android.FailIfErrored(t, errs)
4056
4057 libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
4058 if g, w := libbar.Rule, android.ErrorRule; g != w {
4059 t.Fatalf("Expected libbar rule to be %q, got %q", w, g)
4060 }
4061
4062 if g, w := libbar.Args["error"], "missing dependencies: libmissing"; !strings.Contains(g, w) {
4063 t.Errorf("Expected libbar error to contain %q, was %q", w, g)
4064 }
4065
4066 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
4067 if g, w := libfoo.Inputs.Strings(), libbar.Output.String(); !android.InList(w, g) {
4068 t.Errorf("Expected libfoo.a to depend on %q, got %q", w, g)
4069 }
4070
4071}
Colin Crosse9fe2942020-11-10 18:12:15 -08004072
4073func TestInstallSharedLibs(t *testing.T) {
4074 bp := `
4075 cc_binary {
4076 name: "bin",
4077 host_supported: true,
4078 shared_libs: ["libshared"],
4079 runtime_libs: ["libruntime"],
4080 srcs: [":gen"],
4081 }
4082
4083 cc_library_shared {
4084 name: "libshared",
4085 host_supported: true,
4086 shared_libs: ["libtransitive"],
4087 }
4088
4089 cc_library_shared {
4090 name: "libtransitive",
4091 host_supported: true,
4092 }
4093
4094 cc_library_shared {
4095 name: "libruntime",
4096 host_supported: true,
4097 }
4098
4099 cc_binary_host {
4100 name: "tool",
4101 srcs: ["foo.cpp"],
4102 }
4103
4104 genrule {
4105 name: "gen",
4106 tools: ["tool"],
4107 out: ["gen.cpp"],
4108 cmd: "$(location tool) $(out)",
4109 }
4110 `
4111
4112 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4113 ctx := testCcWithConfig(t, config)
4114
4115 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4116 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4117 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4118 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4119 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4120
4121 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4122 t.Errorf("expected host bin dependency %q, got %q", w, g)
4123 }
4124
4125 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4126 t.Errorf("expected host bin dependency %q, got %q", w, g)
4127 }
4128
4129 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4130 t.Errorf("expected host bin dependency %q, got %q", w, g)
4131 }
4132
4133 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4134 t.Errorf("expected host bin dependency %q, got %q", w, g)
4135 }
4136
4137 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4138 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4139 }
4140
4141 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4142 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4143 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4144 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4145
4146 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4147 t.Errorf("expected device bin dependency %q, got %q", w, g)
4148 }
4149
4150 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4151 t.Errorf("expected device bin dependency %q, got %q", w, g)
4152 }
4153
4154 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4155 t.Errorf("expected device bin dependency %q, got %q", w, g)
4156 }
4157
4158 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4159 t.Errorf("expected device bin dependency %q, got %q", w, g)
4160 }
4161
4162 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4163 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4164 }
4165
4166}