blob: 82b39697fa1e16f6a54d016d3e1778b3a18e5e15 [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 != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500253 if mod.IsVndkExt() != isVndkExt {
254 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800255 }
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 }
Colin Cross127bb8b2020-12-16 16:46:01 -08001052
1053 cc_library {
1054 name: "libllndk",
1055 llndk_stubs: "libllndk.llndk",
1056 }
1057
1058 llndk_library {
1059 name: "libllndk.llndk",
1060 symbol_file: "",
1061 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001062`
1063 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1064 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1065 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1066 ctx := testCcWithConfig(t, config)
1067
1068 // Check Vendor snapshot output.
1069
1070 snapshotDir := "vendor-snapshot"
1071 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +09001072 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1073
1074 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +09001075
1076 for _, arch := range [][]string{
1077 []string{"arm64", "armv8-a"},
1078 []string{"arm", "armv7-a-neon"},
1079 } {
1080 archType := arch[0]
1081 archVariant := arch[1]
1082 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1083
1084 // For shared libraries, only non-VNDK vendor_available modules are captured
1085 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1086 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +09001087 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1088 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
1089 jsonFiles = append(jsonFiles,
1090 filepath.Join(sharedDir, "libvendor.so.json"),
1091 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001092
Colin Cross127bb8b2020-12-16 16:46:01 -08001093 // LLNDK modules are not captured
1094 checkSnapshotExclude(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", sharedDir, sharedVariant)
1095
Inseob Kim8471cda2019-11-15 09:59:12 +09001096 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
Inseob Kimc42f2f22020-07-29 20:32:10 +09001097 // Also cfi variants are captured, except for prebuilts like toolchain_library
Inseob Kim8471cda2019-11-15 09:59:12 +09001098 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001099 staticCfiVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static_cfi", archType, archVariant)
Inseob Kim8471cda2019-11-15 09:59:12 +09001100 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +09001101 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1102 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001103 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001104 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001105 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001106 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001107 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001108 jsonFiles = append(jsonFiles,
1109 filepath.Join(staticDir, "libb.a.json"),
1110 filepath.Join(staticDir, "libvndk.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001111 filepath.Join(staticDir, "libvndk.cfi.a.json"),
Inseob Kim7f283f42020-06-01 21:53:49 +09001112 filepath.Join(staticDir, "libvendor.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001113 filepath.Join(staticDir, "libvendor.cfi.a.json"),
1114 filepath.Join(staticDir, "libvendor_available.a.json"),
1115 filepath.Join(staticDir, "libvendor_available.cfi.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001116
Inseob Kim7f283f42020-06-01 21:53:49 +09001117 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +09001118 if archType == "arm64" {
1119 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1120 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +09001121 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
1122 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
1123 jsonFiles = append(jsonFiles,
1124 filepath.Join(binaryDir, "vendor_bin.json"),
1125 filepath.Join(binaryDir, "vendor_available_bin.json"))
1126 }
1127
1128 // For header libraries, all vendor:true and vendor_available modules are captured.
1129 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1130 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +09001131
1132 // For object modules, all vendor:true and vendor_available modules are captured.
1133 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1134 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1135 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1136 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001137 }
1138
1139 for _, jsonFile := range jsonFiles {
1140 // verify all json files exist
1141 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1142 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001143 }
1144 }
1145}
1146
Inseob Kim5f58ff72020-09-07 19:53:31 +09001147func TestVendorSnapshotUse(t *testing.T) {
1148 frameworkBp := `
1149 cc_library {
1150 name: "libvndk",
1151 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001152 product_available: true,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001153 vndk: {
1154 enabled: true,
1155 },
1156 nocrt: true,
1157 compile_multilib: "64",
1158 }
1159
1160 cc_library {
1161 name: "libvendor",
1162 vendor: true,
1163 nocrt: true,
1164 no_libcrt: true,
1165 stl: "none",
1166 system_shared_libs: [],
1167 compile_multilib: "64",
1168 }
1169
1170 cc_binary {
1171 name: "bin",
1172 vendor: true,
1173 nocrt: true,
1174 no_libcrt: true,
1175 stl: "none",
1176 system_shared_libs: [],
1177 compile_multilib: "64",
1178 }
1179`
1180
1181 vndkBp := `
1182 vndk_prebuilt_shared {
1183 name: "libvndk",
1184 version: "BOARD",
1185 target_arch: "arm64",
1186 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001187 product_available: true,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001188 vndk: {
1189 enabled: true,
1190 },
1191 arch: {
1192 arm64: {
1193 srcs: ["libvndk.so"],
Inseob Kim67be7322020-10-19 10:15:28 +09001194 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001195 },
1196 },
1197 }
1198`
1199
1200 vendorProprietaryBp := `
1201 cc_library {
1202 name: "libvendor_without_snapshot",
1203 vendor: true,
1204 nocrt: true,
1205 no_libcrt: true,
1206 stl: "none",
1207 system_shared_libs: [],
1208 compile_multilib: "64",
1209 }
1210
1211 cc_library_shared {
1212 name: "libclient",
1213 vendor: true,
1214 nocrt: true,
1215 no_libcrt: true,
1216 stl: "none",
1217 system_shared_libs: [],
1218 shared_libs: ["libvndk"],
1219 static_libs: ["libvendor", "libvendor_without_snapshot"],
1220 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001221 srcs: ["client.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001222 }
1223
1224 cc_binary {
1225 name: "bin_without_snapshot",
1226 vendor: true,
1227 nocrt: true,
1228 no_libcrt: true,
1229 stl: "none",
1230 system_shared_libs: [],
1231 static_libs: ["libvndk"],
1232 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001233 srcs: ["bin.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001234 }
1235
1236 vendor_snapshot_static {
1237 name: "libvndk",
1238 version: "BOARD",
1239 target_arch: "arm64",
1240 vendor: true,
1241 arch: {
1242 arm64: {
1243 src: "libvndk.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001244 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001245 },
1246 },
1247 }
1248
1249 vendor_snapshot_shared {
1250 name: "libvendor",
1251 version: "BOARD",
1252 target_arch: "arm64",
1253 vendor: true,
1254 arch: {
1255 arm64: {
1256 src: "libvendor.so",
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_static {
1263 name: "libvendor",
1264 version: "BOARD",
1265 target_arch: "arm64",
1266 vendor: true,
1267 arch: {
1268 arm64: {
1269 src: "libvendor.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001270 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001271 },
1272 },
1273 }
1274
1275 vendor_snapshot_binary {
1276 name: "bin",
1277 version: "BOARD",
1278 target_arch: "arm64",
1279 vendor: true,
1280 arch: {
1281 arm64: {
1282 src: "bin",
1283 },
1284 },
1285 }
1286`
1287 depsBp := GatherRequiredDepsForTest(android.Android)
1288
1289 mockFS := map[string][]byte{
Inseob Kim67be7322020-10-19 10:15:28 +09001290 "deps/Android.bp": []byte(depsBp),
1291 "framework/Android.bp": []byte(frameworkBp),
1292 "vendor/Android.bp": []byte(vendorProprietaryBp),
1293 "vendor/bin": nil,
1294 "vendor/bin.cpp": nil,
1295 "vendor/client.cpp": nil,
1296 "vendor/include/libvndk/a.h": nil,
1297 "vendor/include/libvendor/b.h": nil,
1298 "vendor/libvndk.a": nil,
1299 "vendor/libvendor.a": nil,
1300 "vendor/libvendor.so": nil,
1301 "vndk/Android.bp": []byte(vndkBp),
1302 "vndk/include/libvndk/a.h": nil,
1303 "vndk/libvndk.so": nil,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001304 }
1305
1306 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1307 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1308 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001309 ctx := CreateTestContext(config)
1310 ctx.Register()
Inseob Kim5f58ff72020-09-07 19:53:31 +09001311
1312 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "vendor/Android.bp", "vndk/Android.bp"})
1313 android.FailIfErrored(t, errs)
1314 _, errs = ctx.PrepareBuildActions(config)
1315 android.FailIfErrored(t, errs)
1316
1317 sharedVariant := "android_vendor.BOARD_arm64_armv8-a_shared"
1318 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1319 binaryVariant := "android_vendor.BOARD_arm64_armv8-a"
1320
1321 // libclient uses libvndk.vndk.BOARD.arm64, libvendor.vendor_static.BOARD.arm64, libvendor_without_snapshot
Inseob Kim67be7322020-10-19 10:15:28 +09001322 libclientCcFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("cc").Args["cFlags"]
1323 for _, includeFlags := range []string{
1324 "-Ivndk/include/libvndk", // libvndk
1325 "-Ivendor/include/libvendor", // libvendor
1326 } {
1327 if !strings.Contains(libclientCcFlags, includeFlags) {
1328 t.Errorf("flags for libclient must contain %#v, but was %#v.",
1329 includeFlags, libclientCcFlags)
1330 }
1331 }
Inseob Kim5f58ff72020-09-07 19:53:31 +09001332
Inseob Kim67be7322020-10-19 10:15:28 +09001333 libclientLdFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001334 for _, input := range [][]string{
1335 []string{sharedVariant, "libvndk.vndk.BOARD.arm64"},
1336 []string{staticVariant, "libvendor.vendor_static.BOARD.arm64"},
1337 []string{staticVariant, "libvendor_without_snapshot"},
1338 } {
1339 outputPaths := getOutputPaths(ctx, input[0] /* variant */, []string{input[1]} /* module name */)
Inseob Kim67be7322020-10-19 10:15:28 +09001340 if !strings.Contains(libclientLdFlags, outputPaths[0].String()) {
1341 t.Errorf("libflags for libclient must contain %#v, but was %#v", outputPaths[0], libclientLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001342 }
1343 }
1344
1345 // bin_without_snapshot uses libvndk.vendor_static.BOARD.arm64
Inseob Kim67be7322020-10-19 10:15:28 +09001346 binWithoutSnapshotCcFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("cc").Args["cFlags"]
1347 if !strings.Contains(binWithoutSnapshotCcFlags, "-Ivendor/include/libvndk") {
1348 t.Errorf("flags for bin_without_snapshot must contain %#v, but was %#v.",
1349 "-Ivendor/include/libvndk", binWithoutSnapshotCcFlags)
1350 }
1351
1352 binWithoutSnapshotLdFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001353 libVndkStaticOutputPaths := getOutputPaths(ctx, staticVariant, []string{"libvndk.vendor_static.BOARD.arm64"})
Inseob Kim67be7322020-10-19 10:15:28 +09001354 if !strings.Contains(binWithoutSnapshotLdFlags, libVndkStaticOutputPaths[0].String()) {
Inseob Kim5f58ff72020-09-07 19:53:31 +09001355 t.Errorf("libflags for bin_without_snapshot must contain %#v, but was %#v",
Inseob Kim67be7322020-10-19 10:15:28 +09001356 libVndkStaticOutputPaths[0], binWithoutSnapshotLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001357 }
1358
1359 // libvendor.so is installed by libvendor.vendor_shared.BOARD.arm64
1360 ctx.ModuleForTests("libvendor.vendor_shared.BOARD.arm64", sharedVariant).Output("libvendor.so")
1361
1362 // libvendor_without_snapshot.so is installed by libvendor_without_snapshot
1363 ctx.ModuleForTests("libvendor_without_snapshot", sharedVariant).Output("libvendor_without_snapshot.so")
1364
1365 // bin is installed by bin.vendor_binary.BOARD.arm64
1366 ctx.ModuleForTests("bin.vendor_binary.BOARD.arm64", binaryVariant).Output("bin")
1367
1368 // bin_without_snapshot is installed by bin_without_snapshot
1369 ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Output("bin_without_snapshot")
1370
1371 // libvendor and bin don't have vendor.BOARD variant
1372 libvendorVariants := ctx.ModuleVariantsForTests("libvendor")
1373 if inList(sharedVariant, libvendorVariants) {
1374 t.Errorf("libvendor must not have variant %#v, but it does", sharedVariant)
1375 }
1376
1377 binVariants := ctx.ModuleVariantsForTests("bin")
1378 if inList(binaryVariant, binVariants) {
1379 t.Errorf("bin must not have variant %#v, but it does", sharedVariant)
1380 }
1381}
1382
Inseob Kimc42f2f22020-07-29 20:32:10 +09001383func TestVendorSnapshotSanitizer(t *testing.T) {
1384 bp := `
1385 vendor_snapshot_static {
1386 name: "libsnapshot",
1387 vendor: true,
1388 target_arch: "arm64",
1389 version: "BOARD",
1390 arch: {
1391 arm64: {
1392 src: "libsnapshot.a",
1393 cfi: {
1394 src: "libsnapshot.cfi.a",
1395 }
1396 },
1397 },
1398 }
1399`
1400 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1401 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1402 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1403 ctx := testCcWithConfig(t, config)
1404
1405 // Check non-cfi and cfi variant.
1406 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1407 staticCfiVariant := "android_vendor.BOARD_arm64_armv8-a_static_cfi"
1408
1409 staticModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticVariant).Module().(*Module)
1410 assertString(t, staticModule.outputFile.Path().Base(), "libsnapshot.a")
1411
1412 staticCfiModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticCfiVariant).Module().(*Module)
1413 assertString(t, staticCfiModule.outputFile.Path().Base(), "libsnapshot.cfi.a")
1414}
1415
Bill Peckham945441c2020-08-31 16:07:58 -07001416func assertExcludeFromVendorSnapshotIs(t *testing.T, c *Module, expected bool) {
1417 t.Helper()
1418 if c.ExcludeFromVendorSnapshot() != expected {
1419 t.Errorf("expected %q ExcludeFromVendorSnapshot to be %t", c.String(), expected)
1420 }
1421}
1422
Jose Galmes6f843bc2020-12-11 13:36:29 -08001423func assertExcludeFromRecoverySnapshotIs(t *testing.T, c *Module, expected bool) {
1424 t.Helper()
1425 if c.ExcludeFromRecoverySnapshot() != expected {
1426 t.Errorf("expected %q ExcludeFromRecoverySnapshot to be %t", c.String(), expected)
1427 }
1428}
1429
Bill Peckham945441c2020-08-31 16:07:58 -07001430func TestVendorSnapshotExclude(t *testing.T) {
1431
1432 // This test verifies that the exclude_from_vendor_snapshot property
1433 // makes its way from the Android.bp source file into the module data
1434 // structure. It also verifies that modules are correctly included or
1435 // excluded in the vendor snapshot based on their path (framework or
1436 // vendor) and the exclude_from_vendor_snapshot property.
1437
1438 frameworkBp := `
1439 cc_library_shared {
1440 name: "libinclude",
1441 srcs: ["src/include.cpp"],
1442 vendor_available: true,
1443 }
1444 cc_library_shared {
1445 name: "libexclude",
1446 srcs: ["src/exclude.cpp"],
1447 vendor: true,
1448 exclude_from_vendor_snapshot: true,
1449 }
1450 `
1451
1452 vendorProprietaryBp := `
1453 cc_library_shared {
1454 name: "libvendor",
1455 srcs: ["vendor.cpp"],
1456 vendor: true,
1457 }
1458 `
1459
1460 depsBp := GatherRequiredDepsForTest(android.Android)
1461
1462 mockFS := map[string][]byte{
1463 "deps/Android.bp": []byte(depsBp),
1464 "framework/Android.bp": []byte(frameworkBp),
1465 "framework/include.cpp": nil,
1466 "framework/exclude.cpp": nil,
1467 "device/Android.bp": []byte(vendorProprietaryBp),
1468 "device/vendor.cpp": nil,
1469 }
1470
1471 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1472 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1473 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001474 ctx := CreateTestContext(config)
1475 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001476
1477 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1478 android.FailIfErrored(t, errs)
1479 _, errs = ctx.PrepareBuildActions(config)
1480 android.FailIfErrored(t, errs)
1481
1482 // Test an include and exclude framework module.
1483 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1484 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", vendorVariant).Module().(*Module), false)
1485 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libexclude", vendorVariant).Module().(*Module), true)
1486
1487 // A vendor module is excluded, but by its path, not the
1488 // exclude_from_vendor_snapshot property.
1489 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libvendor", vendorVariant).Module().(*Module), false)
1490
1491 // Verify the content of the vendor snapshot.
1492
1493 snapshotDir := "vendor-snapshot"
1494 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1495 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1496
1497 var includeJsonFiles []string
1498 var excludeJsonFiles []string
1499
1500 for _, arch := range [][]string{
1501 []string{"arm64", "armv8-a"},
1502 []string{"arm", "armv7-a-neon"},
1503 } {
1504 archType := arch[0]
1505 archVariant := arch[1]
1506 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1507
1508 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1509 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1510
1511 // Included modules
1512 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1513 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1514
1515 // Excluded modules
1516 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1517 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1518 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1519 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1520 }
1521
1522 // Verify that each json file for an included module has a rule.
1523 for _, jsonFile := range includeJsonFiles {
1524 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1525 t.Errorf("include json file %q not found", jsonFile)
1526 }
1527 }
1528
1529 // Verify that each json file for an excluded module has no rule.
1530 for _, jsonFile := range excludeJsonFiles {
1531 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1532 t.Errorf("exclude json file %q found", jsonFile)
1533 }
1534 }
1535}
1536
1537func TestVendorSnapshotExcludeInVendorProprietaryPathErrors(t *testing.T) {
1538
1539 // This test verifies that using the exclude_from_vendor_snapshot
1540 // property on a module in a vendor proprietary path generates an
1541 // error. These modules are already excluded, so we prohibit using the
1542 // property in this way, which could add to confusion.
1543
1544 vendorProprietaryBp := `
1545 cc_library_shared {
1546 name: "libvendor",
1547 srcs: ["vendor.cpp"],
1548 vendor: true,
1549 exclude_from_vendor_snapshot: true,
1550 }
1551 `
1552
1553 depsBp := GatherRequiredDepsForTest(android.Android)
1554
1555 mockFS := map[string][]byte{
1556 "deps/Android.bp": []byte(depsBp),
1557 "device/Android.bp": []byte(vendorProprietaryBp),
1558 "device/vendor.cpp": nil,
1559 }
1560
1561 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1562 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1563 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001564 ctx := CreateTestContext(config)
1565 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001566
1567 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "device/Android.bp"})
1568 android.FailIfErrored(t, errs)
1569
1570 _, errs = ctx.PrepareBuildActions(config)
1571 android.CheckErrorsAgainstExpectations(t, errs, []string{
1572 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1573 `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 -08001574 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1575 `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 -07001576 })
1577}
1578
1579func TestVendorSnapshotExcludeWithVendorAvailable(t *testing.T) {
1580
1581 // This test verifies that using the exclude_from_vendor_snapshot
1582 // property on a module that is vendor available generates an error. A
1583 // vendor available module must be captured in the vendor snapshot and
1584 // must not built from source when building the vendor image against
1585 // the vendor snapshot.
1586
1587 frameworkBp := `
1588 cc_library_shared {
1589 name: "libinclude",
1590 srcs: ["src/include.cpp"],
1591 vendor_available: true,
1592 exclude_from_vendor_snapshot: true,
1593 }
1594 `
1595
1596 depsBp := GatherRequiredDepsForTest(android.Android)
1597
1598 mockFS := map[string][]byte{
1599 "deps/Android.bp": []byte(depsBp),
1600 "framework/Android.bp": []byte(frameworkBp),
1601 "framework/include.cpp": nil,
1602 }
1603
1604 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1605 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1606 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001607 ctx := CreateTestContext(config)
1608 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001609
1610 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp"})
1611 android.FailIfErrored(t, errs)
1612
1613 _, errs = ctx.PrepareBuildActions(config)
1614 android.CheckErrorsAgainstExpectations(t, errs, []string{
1615 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1616 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1617 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1618 `module "libinclude\{.+,image:vendor.+,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1619 })
1620}
1621
Jose Galmesf7294582020-11-13 12:07:36 -08001622func TestRecoverySnapshotCapture(t *testing.T) {
1623 bp := `
1624 cc_library {
1625 name: "libvndk",
1626 vendor_available: true,
1627 recovery_available: true,
1628 product_available: true,
1629 vndk: {
1630 enabled: true,
1631 },
1632 nocrt: true,
1633 }
1634
1635 cc_library {
1636 name: "librecovery",
1637 recovery: true,
1638 nocrt: true,
1639 }
1640
1641 cc_library {
1642 name: "librecovery_available",
1643 recovery_available: true,
1644 nocrt: true,
1645 }
1646
1647 cc_library_headers {
1648 name: "librecovery_headers",
1649 recovery_available: true,
1650 nocrt: true,
1651 }
1652
1653 cc_binary {
1654 name: "recovery_bin",
1655 recovery: true,
1656 nocrt: true,
1657 }
1658
1659 cc_binary {
1660 name: "recovery_available_bin",
1661 recovery_available: true,
1662 nocrt: true,
1663 }
1664
1665 toolchain_library {
1666 name: "libb",
1667 recovery_available: true,
1668 src: "libb.a",
1669 }
1670
1671 cc_object {
1672 name: "obj",
1673 recovery_available: true,
1674 }
1675`
1676 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jose Galmes6f843bc2020-12-11 13:36:29 -08001677 config.TestProductVariables.RecoverySnapshotVersion = StringPtr("current")
Jose Galmesf7294582020-11-13 12:07:36 -08001678 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1679 ctx := testCcWithConfig(t, config)
1680
1681 // Check Recovery snapshot output.
1682
1683 snapshotDir := "recovery-snapshot"
1684 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1685 snapshotSingleton := ctx.SingletonForTests("recovery-snapshot")
1686
1687 var jsonFiles []string
1688
1689 for _, arch := range [][]string{
1690 []string{"arm64", "armv8-a"},
1691 } {
1692 archType := arch[0]
1693 archVariant := arch[1]
1694 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1695
1696 // For shared libraries, only recovery_available modules are captured.
1697 sharedVariant := fmt.Sprintf("android_recovery_%s_%s_shared", archType, archVariant)
1698 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1699 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", sharedDir, sharedVariant)
1700 checkSnapshot(t, ctx, snapshotSingleton, "librecovery", "librecovery.so", sharedDir, sharedVariant)
1701 checkSnapshot(t, ctx, snapshotSingleton, "librecovery_available", "librecovery_available.so", sharedDir, sharedVariant)
1702 jsonFiles = append(jsonFiles,
1703 filepath.Join(sharedDir, "libvndk.so.json"),
1704 filepath.Join(sharedDir, "librecovery.so.json"),
1705 filepath.Join(sharedDir, "librecovery_available.so.json"))
1706
1707 // For static libraries, all recovery:true and recovery_available modules are captured.
1708 staticVariant := fmt.Sprintf("android_recovery_%s_%s_static", archType, archVariant)
1709 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
1710 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1711 checkSnapshot(t, ctx, snapshotSingleton, "librecovery", "librecovery.a", staticDir, staticVariant)
1712 checkSnapshot(t, ctx, snapshotSingleton, "librecovery_available", "librecovery_available.a", staticDir, staticVariant)
1713 jsonFiles = append(jsonFiles,
1714 filepath.Join(staticDir, "libb.a.json"),
1715 filepath.Join(staticDir, "librecovery.a.json"),
1716 filepath.Join(staticDir, "librecovery_available.a.json"))
1717
1718 // For binary executables, all recovery:true and recovery_available modules are captured.
1719 if archType == "arm64" {
1720 binaryVariant := fmt.Sprintf("android_recovery_%s_%s", archType, archVariant)
1721 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
1722 checkSnapshot(t, ctx, snapshotSingleton, "recovery_bin", "recovery_bin", binaryDir, binaryVariant)
1723 checkSnapshot(t, ctx, snapshotSingleton, "recovery_available_bin", "recovery_available_bin", binaryDir, binaryVariant)
1724 jsonFiles = append(jsonFiles,
1725 filepath.Join(binaryDir, "recovery_bin.json"),
1726 filepath.Join(binaryDir, "recovery_available_bin.json"))
1727 }
1728
1729 // For header libraries, all vendor:true and vendor_available modules are captured.
1730 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1731 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "librecovery_headers.json"))
1732
1733 // For object modules, all vendor:true and vendor_available modules are captured.
1734 objectVariant := fmt.Sprintf("android_recovery_%s_%s", archType, archVariant)
1735 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1736 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1737 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
1738 }
1739
1740 for _, jsonFile := range jsonFiles {
1741 // verify all json files exist
1742 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1743 t.Errorf("%q expected but not found", jsonFile)
1744 }
1745 }
1746}
1747
Jose Galmes6f843bc2020-12-11 13:36:29 -08001748func TestRecoverySnapshotExclude(t *testing.T) {
1749 // This test verifies that the exclude_from_recovery_snapshot property
1750 // makes its way from the Android.bp source file into the module data
1751 // structure. It also verifies that modules are correctly included or
1752 // excluded in the recovery snapshot based on their path (framework or
1753 // vendor) and the exclude_from_recovery_snapshot property.
1754
1755 frameworkBp := `
1756 cc_library_shared {
1757 name: "libinclude",
1758 srcs: ["src/include.cpp"],
1759 recovery_available: true,
1760 }
1761 cc_library_shared {
1762 name: "libexclude",
1763 srcs: ["src/exclude.cpp"],
1764 recovery: true,
1765 exclude_from_recovery_snapshot: true,
1766 }
1767 `
1768
1769 vendorProprietaryBp := `
1770 cc_library_shared {
1771 name: "libvendor",
1772 srcs: ["vendor.cpp"],
1773 recovery: true,
1774 }
1775 `
1776
1777 depsBp := GatherRequiredDepsForTest(android.Android)
1778
1779 mockFS := map[string][]byte{
1780 "deps/Android.bp": []byte(depsBp),
1781 "framework/Android.bp": []byte(frameworkBp),
1782 "framework/include.cpp": nil,
1783 "framework/exclude.cpp": nil,
1784 "device/Android.bp": []byte(vendorProprietaryBp),
1785 "device/vendor.cpp": nil,
1786 }
1787
1788 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1789 config.TestProductVariables.RecoverySnapshotVersion = StringPtr("current")
1790 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1791 ctx := CreateTestContext(config)
1792 ctx.Register()
1793
1794 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1795 android.FailIfErrored(t, errs)
1796 _, errs = ctx.PrepareBuildActions(config)
1797 android.FailIfErrored(t, errs)
1798
1799 // Test an include and exclude framework module.
1800 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1801 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libinclude", recoveryVariant).Module().(*Module), false)
1802 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libexclude", recoveryVariant).Module().(*Module), true)
1803
1804 // A vendor module is excluded, but by its path, not the
1805 // exclude_from_recovery_snapshot property.
1806 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libvendor", recoveryVariant).Module().(*Module), false)
1807
1808 // Verify the content of the recovery snapshot.
1809
1810 snapshotDir := "recovery-snapshot"
1811 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1812 snapshotSingleton := ctx.SingletonForTests("recovery-snapshot")
1813
1814 var includeJsonFiles []string
1815 var excludeJsonFiles []string
1816
1817 for _, arch := range [][]string{
1818 []string{"arm64", "armv8-a"},
1819 } {
1820 archType := arch[0]
1821 archVariant := arch[1]
1822 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1823
1824 sharedVariant := fmt.Sprintf("android_recovery_%s_%s_shared", archType, archVariant)
1825 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1826
1827 // Included modules
1828 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1829 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1830
1831 // Excluded modules
1832 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1833 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1834 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1835 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1836 }
1837
1838 // Verify that each json file for an included module has a rule.
1839 for _, jsonFile := range includeJsonFiles {
1840 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1841 t.Errorf("include json file %q not found", jsonFile)
1842 }
1843 }
1844
1845 // Verify that each json file for an excluded module has no rule.
1846 for _, jsonFile := range excludeJsonFiles {
1847 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1848 t.Errorf("exclude json file %q found", jsonFile)
1849 }
1850 }
1851}
1852
Jooyung Hana70f0672019-01-18 15:20:43 +09001853func TestDoubleLoadableDepError(t *testing.T) {
1854 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1855 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1856 cc_library {
1857 name: "libllndk",
1858 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001859 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001860 }
1861
1862 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001863 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001864 symbol_file: "",
1865 }
1866
1867 cc_library {
1868 name: "libnondoubleloadable",
1869 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001870 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001871 vndk: {
1872 enabled: true,
1873 },
1874 }
1875 `)
1876
1877 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1878 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1879 cc_library {
1880 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001881 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001882 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001883 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001884 }
1885
1886 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001887 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001888 symbol_file: "",
1889 }
1890
1891 cc_library {
1892 name: "libnondoubleloadable",
1893 vendor_available: true,
1894 }
1895 `)
1896
1897 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
1898 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1899 cc_library {
1900 name: "libdoubleloadable",
1901 vendor_available: true,
1902 double_loadable: true,
1903 shared_libs: ["libnondoubleloadable"],
1904 }
1905
1906 cc_library {
1907 name: "libnondoubleloadable",
1908 vendor_available: true,
1909 }
1910 `)
1911
1912 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
1913 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1914 cc_library {
1915 name: "libdoubleloadable",
1916 vendor_available: true,
1917 double_loadable: true,
1918 shared_libs: ["libnondoubleloadable"],
1919 }
1920
1921 cc_library {
1922 name: "libnondoubleloadable",
1923 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001924 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001925 vndk: {
1926 enabled: true,
1927 },
1928 }
1929 `)
1930
1931 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
1932 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1933 cc_library {
1934 name: "libdoubleloadable",
1935 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001936 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001937 vndk: {
1938 enabled: true,
1939 },
1940 double_loadable: true,
1941 shared_libs: ["libnondoubleloadable"],
1942 }
1943
1944 cc_library {
1945 name: "libnondoubleloadable",
1946 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09001947 product_available: false,
Jooyung Hana70f0672019-01-18 15:20:43 +09001948 vndk: {
1949 enabled: true,
1950 },
1951 }
1952 `)
1953
1954 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1955 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1956 cc_library {
1957 name: "libllndk",
1958 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001959 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001960 }
1961
1962 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001963 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001964 symbol_file: "",
1965 }
1966
1967 cc_library {
1968 name: "libcoreonly",
1969 shared_libs: ["libvendoravailable"],
1970 }
1971
1972 // indirect dependency of LLNDK
1973 cc_library {
1974 name: "libvendoravailable",
1975 vendor_available: true,
1976 }
1977 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001978}
1979
Jooyung Han479ca172020-10-19 18:51:07 +09001980func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1981 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1982 cc_library {
1983 name: "libvndksp",
1984 shared_libs: ["libanothervndksp"],
1985 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001986 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001987 vndk: {
1988 enabled: true,
1989 support_system_process: true,
1990 }
1991 }
1992
1993 cc_library {
1994 name: "libllndk",
1995 shared_libs: ["libanothervndksp"],
1996 }
1997
1998 llndk_library {
1999 name: "libllndk",
2000 symbol_file: "",
2001 }
2002
2003 cc_library {
2004 name: "libanothervndksp",
2005 vendor_available: true,
2006 }
2007 `)
2008}
2009
Logan Chienf3511742017-10-31 18:04:35 +08002010func TestVndkExt(t *testing.T) {
2011 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09002012 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08002013 cc_library {
2014 name: "libvndk",
2015 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002016 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002017 vndk: {
2018 enabled: true,
2019 },
2020 nocrt: true,
2021 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002022 cc_library {
2023 name: "libvndk2",
2024 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002025 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09002026 vndk: {
2027 enabled: true,
2028 },
2029 target: {
2030 vendor: {
2031 suffix: "-suffix",
2032 },
Justin Yun63e9ec72020-10-29 16:49:43 +09002033 product: {
2034 suffix: "-suffix",
2035 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09002036 },
2037 nocrt: true,
2038 }
Logan Chienf3511742017-10-31 18:04:35 +08002039
2040 cc_library {
2041 name: "libvndk_ext",
2042 vendor: true,
2043 vndk: {
2044 enabled: true,
2045 extends: "libvndk",
2046 },
2047 nocrt: true,
2048 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002049
2050 cc_library {
2051 name: "libvndk2_ext",
2052 vendor: true,
2053 vndk: {
2054 enabled: true,
2055 extends: "libvndk2",
2056 },
2057 nocrt: true,
2058 }
Logan Chienf3511742017-10-31 18:04:35 +08002059
Justin Yun0ecf0b22020-02-28 15:07:59 +09002060 cc_library {
2061 name: "libvndk_ext_product",
2062 product_specific: true,
2063 vndk: {
2064 enabled: true,
2065 extends: "libvndk",
2066 },
2067 nocrt: true,
2068 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002069
Justin Yun0ecf0b22020-02-28 15:07:59 +09002070 cc_library {
2071 name: "libvndk2_ext_product",
2072 product_specific: true,
2073 vndk: {
2074 enabled: true,
2075 extends: "libvndk2",
2076 },
2077 nocrt: true,
2078 }
2079 `
2080 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2081 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2082 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2083 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2084
2085 ctx := testCcWithConfig(t, config)
2086
2087 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
2088 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
2089
2090 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
2091 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
2092
2093 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
2094 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08002095}
2096
Logan Chiend3c59a22018-03-29 14:08:15 +08002097func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08002098 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
2099 ctx := testCcNoVndk(t, `
2100 cc_library {
2101 name: "libvndk",
2102 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002103 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002104 vndk: {
2105 enabled: true,
2106 },
2107 nocrt: true,
2108 }
2109
2110 cc_library {
2111 name: "libvndk_ext",
2112 vendor: true,
2113 vndk: {
2114 enabled: true,
2115 extends: "libvndk",
2116 },
2117 nocrt: true,
2118 }
2119 `)
2120
2121 // Ensures that the core variant of "libvndk_ext" can be found.
2122 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
2123 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
2124 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
2125 }
2126}
2127
Justin Yun0ecf0b22020-02-28 15:07:59 +09002128func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
2129 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
2130 ctx := testCc(t, `
2131 cc_library {
2132 name: "libvndk",
2133 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002134 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002135 vndk: {
2136 enabled: true,
2137 },
2138 nocrt: true,
2139 }
2140
2141 cc_library {
2142 name: "libvndk_ext_product",
2143 product_specific: true,
2144 vndk: {
2145 enabled: true,
2146 extends: "libvndk",
2147 },
2148 nocrt: true,
2149 }
2150 `)
2151
2152 // Ensures that the core variant of "libvndk_ext_product" can be found.
2153 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
2154 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
2155 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
2156 }
2157}
2158
Logan Chienf3511742017-10-31 18:04:35 +08002159func TestVndkExtError(t *testing.T) {
2160 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09002161 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08002162 cc_library {
2163 name: "libvndk",
2164 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002165 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002166 vndk: {
2167 enabled: true,
2168 },
2169 nocrt: true,
2170 }
2171
2172 cc_library {
2173 name: "libvndk_ext",
2174 vndk: {
2175 enabled: true,
2176 extends: "libvndk",
2177 },
2178 nocrt: true,
2179 }
2180 `)
2181
2182 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
2183 cc_library {
2184 name: "libvndk",
2185 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002186 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002187 vndk: {
2188 enabled: true,
2189 },
2190 nocrt: true,
2191 }
2192
2193 cc_library {
2194 name: "libvndk_ext",
2195 vendor: true,
2196 vndk: {
2197 enabled: true,
2198 },
2199 nocrt: true,
2200 }
2201 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002202
2203 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
2204 cc_library {
2205 name: "libvndk",
2206 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002207 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002208 vndk: {
2209 enabled: true,
2210 },
2211 nocrt: true,
2212 }
2213
2214 cc_library {
2215 name: "libvndk_ext_product",
2216 product_specific: true,
2217 vndk: {
2218 enabled: true,
2219 },
2220 nocrt: true,
2221 }
2222 `)
2223
2224 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
2225 cc_library {
2226 name: "libvndk",
2227 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002228 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002229 vndk: {
2230 enabled: true,
2231 },
2232 nocrt: true,
2233 }
2234
2235 cc_library {
2236 name: "libvndk_ext_product",
2237 product_specific: true,
2238 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002239 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002240 vndk: {
2241 enabled: true,
2242 extends: "libvndk",
2243 },
2244 nocrt: true,
2245 }
2246 `)
Logan Chienf3511742017-10-31 18:04:35 +08002247}
2248
2249func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
2250 // This test ensures an error is emitted for inconsistent support_system_process.
2251 testCcError(t, "module \".*\" with mismatched support_system_process", `
2252 cc_library {
2253 name: "libvndk",
2254 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002255 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002256 vndk: {
2257 enabled: true,
2258 },
2259 nocrt: true,
2260 }
2261
2262 cc_library {
2263 name: "libvndk_sp_ext",
2264 vendor: true,
2265 vndk: {
2266 enabled: true,
2267 extends: "libvndk",
2268 support_system_process: true,
2269 },
2270 nocrt: true,
2271 }
2272 `)
2273
2274 testCcError(t, "module \".*\" with mismatched support_system_process", `
2275 cc_library {
2276 name: "libvndk_sp",
2277 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002278 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002279 vndk: {
2280 enabled: true,
2281 support_system_process: true,
2282 },
2283 nocrt: true,
2284 }
2285
2286 cc_library {
2287 name: "libvndk_ext",
2288 vendor: true,
2289 vndk: {
2290 enabled: true,
2291 extends: "libvndk_sp",
2292 },
2293 nocrt: true,
2294 }
2295 `)
2296}
2297
2298func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08002299 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08002300 // with `vendor_available: false`.
2301 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
2302 cc_library {
2303 name: "libvndk",
2304 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09002305 product_available: false,
Logan Chienf3511742017-10-31 18:04:35 +08002306 vndk: {
2307 enabled: true,
2308 },
2309 nocrt: true,
2310 }
2311
2312 cc_library {
2313 name: "libvndk_ext",
2314 vendor: true,
2315 vndk: {
2316 enabled: true,
2317 extends: "libvndk",
2318 },
2319 nocrt: true,
2320 }
2321 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002322
2323 testCcErrorProductVndk(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
2324 cc_library {
2325 name: "libvndk",
2326 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09002327 product_available: false,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002328 vndk: {
2329 enabled: true,
2330 },
2331 nocrt: true,
2332 }
2333
2334 cc_library {
2335 name: "libvndk_ext_product",
2336 product_specific: true,
2337 vndk: {
2338 enabled: true,
2339 extends: "libvndk",
2340 },
2341 nocrt: true,
2342 }
2343 `)
Logan Chienf3511742017-10-31 18:04:35 +08002344}
2345
Logan Chiend3c59a22018-03-29 14:08:15 +08002346func TestVendorModuleUseVndkExt(t *testing.T) {
2347 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002348 testCc(t, `
2349 cc_library {
2350 name: "libvndk",
2351 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002352 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002353 vndk: {
2354 enabled: true,
2355 },
2356 nocrt: true,
2357 }
2358
2359 cc_library {
2360 name: "libvndk_ext",
2361 vendor: true,
2362 vndk: {
2363 enabled: true,
2364 extends: "libvndk",
2365 },
2366 nocrt: true,
2367 }
2368
2369 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08002370 name: "libvndk_sp",
2371 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002372 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002373 vndk: {
2374 enabled: true,
2375 support_system_process: true,
2376 },
2377 nocrt: true,
2378 }
2379
2380 cc_library {
2381 name: "libvndk_sp_ext",
2382 vendor: true,
2383 vndk: {
2384 enabled: true,
2385 extends: "libvndk_sp",
2386 support_system_process: true,
2387 },
2388 nocrt: true,
2389 }
2390
2391 cc_library {
2392 name: "libvendor",
2393 vendor: true,
2394 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
2395 nocrt: true,
2396 }
2397 `)
2398}
2399
Logan Chiend3c59a22018-03-29 14:08:15 +08002400func TestVndkExtUseVendorLib(t *testing.T) {
2401 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08002402 testCc(t, `
2403 cc_library {
2404 name: "libvndk",
2405 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002406 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002407 vndk: {
2408 enabled: true,
2409 },
2410 nocrt: true,
2411 }
2412
2413 cc_library {
2414 name: "libvndk_ext",
2415 vendor: true,
2416 vndk: {
2417 enabled: true,
2418 extends: "libvndk",
2419 },
2420 shared_libs: ["libvendor"],
2421 nocrt: true,
2422 }
2423
2424 cc_library {
2425 name: "libvendor",
2426 vendor: true,
2427 nocrt: true,
2428 }
2429 `)
Logan Chienf3511742017-10-31 18:04:35 +08002430
Logan Chiend3c59a22018-03-29 14:08:15 +08002431 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
2432 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08002433 cc_library {
2434 name: "libvndk_sp",
2435 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002436 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002437 vndk: {
2438 enabled: true,
2439 support_system_process: true,
2440 },
2441 nocrt: true,
2442 }
2443
2444 cc_library {
2445 name: "libvndk_sp_ext",
2446 vendor: true,
2447 vndk: {
2448 enabled: true,
2449 extends: "libvndk_sp",
2450 support_system_process: true,
2451 },
2452 shared_libs: ["libvendor"], // Cause an error
2453 nocrt: true,
2454 }
2455
2456 cc_library {
2457 name: "libvendor",
2458 vendor: true,
2459 nocrt: true,
2460 }
2461 `)
2462}
2463
Justin Yun0ecf0b22020-02-28 15:07:59 +09002464func TestProductVndkExtDependency(t *testing.T) {
2465 bp := `
2466 cc_library {
2467 name: "libvndk",
2468 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002469 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002470 vndk: {
2471 enabled: true,
2472 },
2473 nocrt: true,
2474 }
2475
2476 cc_library {
2477 name: "libvndk_ext_product",
2478 product_specific: true,
2479 vndk: {
2480 enabled: true,
2481 extends: "libvndk",
2482 },
2483 shared_libs: ["libproduct_for_vndklibs"],
2484 nocrt: true,
2485 }
2486
2487 cc_library {
2488 name: "libvndk_sp",
2489 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002490 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002491 vndk: {
2492 enabled: true,
2493 support_system_process: true,
2494 },
2495 nocrt: true,
2496 }
2497
2498 cc_library {
2499 name: "libvndk_sp_ext_product",
2500 product_specific: true,
2501 vndk: {
2502 enabled: true,
2503 extends: "libvndk_sp",
2504 support_system_process: true,
2505 },
2506 shared_libs: ["libproduct_for_vndklibs"],
2507 nocrt: true,
2508 }
2509
2510 cc_library {
2511 name: "libproduct",
2512 product_specific: true,
2513 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
2514 nocrt: true,
2515 }
2516
2517 cc_library {
2518 name: "libproduct_for_vndklibs",
2519 product_specific: true,
2520 nocrt: true,
2521 }
2522 `
2523 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2524 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2525 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2526 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2527
2528 testCcWithConfig(t, config)
2529}
2530
Logan Chiend3c59a22018-03-29 14:08:15 +08002531func TestVndkSpExtUseVndkError(t *testing.T) {
2532 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
2533 // library.
2534 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2535 cc_library {
2536 name: "libvndk",
2537 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002538 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002539 vndk: {
2540 enabled: true,
2541 },
2542 nocrt: true,
2543 }
2544
2545 cc_library {
2546 name: "libvndk_sp",
2547 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002548 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002549 vndk: {
2550 enabled: true,
2551 support_system_process: true,
2552 },
2553 nocrt: true,
2554 }
2555
2556 cc_library {
2557 name: "libvndk_sp_ext",
2558 vendor: true,
2559 vndk: {
2560 enabled: true,
2561 extends: "libvndk_sp",
2562 support_system_process: true,
2563 },
2564 shared_libs: ["libvndk"], // Cause an error
2565 nocrt: true,
2566 }
2567 `)
2568
2569 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
2570 // library.
2571 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2572 cc_library {
2573 name: "libvndk",
2574 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002575 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002576 vndk: {
2577 enabled: true,
2578 },
2579 nocrt: true,
2580 }
2581
2582 cc_library {
2583 name: "libvndk_ext",
2584 vendor: true,
2585 vndk: {
2586 enabled: true,
2587 extends: "libvndk",
2588 },
2589 nocrt: true,
2590 }
2591
2592 cc_library {
2593 name: "libvndk_sp",
2594 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002595 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002596 vndk: {
2597 enabled: true,
2598 support_system_process: true,
2599 },
2600 nocrt: true,
2601 }
2602
2603 cc_library {
2604 name: "libvndk_sp_ext",
2605 vendor: true,
2606 vndk: {
2607 enabled: true,
2608 extends: "libvndk_sp",
2609 support_system_process: true,
2610 },
2611 shared_libs: ["libvndk_ext"], // Cause an error
2612 nocrt: true,
2613 }
2614 `)
2615}
2616
2617func TestVndkUseVndkExtError(t *testing.T) {
2618 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2619 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002620 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2621 cc_library {
2622 name: "libvndk",
2623 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002624 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002625 vndk: {
2626 enabled: true,
2627 },
2628 nocrt: true,
2629 }
2630
2631 cc_library {
2632 name: "libvndk_ext",
2633 vendor: true,
2634 vndk: {
2635 enabled: true,
2636 extends: "libvndk",
2637 },
2638 nocrt: true,
2639 }
2640
2641 cc_library {
2642 name: "libvndk2",
2643 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002644 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002645 vndk: {
2646 enabled: true,
2647 },
2648 shared_libs: ["libvndk_ext"],
2649 nocrt: true,
2650 }
2651 `)
2652
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002653 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002654 cc_library {
2655 name: "libvndk",
2656 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002657 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002658 vndk: {
2659 enabled: true,
2660 },
2661 nocrt: true,
2662 }
2663
2664 cc_library {
2665 name: "libvndk_ext",
2666 vendor: true,
2667 vndk: {
2668 enabled: true,
2669 extends: "libvndk",
2670 },
2671 nocrt: true,
2672 }
2673
2674 cc_library {
2675 name: "libvndk2",
2676 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002677 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002678 vndk: {
2679 enabled: true,
2680 },
2681 target: {
2682 vendor: {
2683 shared_libs: ["libvndk_ext"],
2684 },
2685 },
2686 nocrt: true,
2687 }
2688 `)
2689
2690 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2691 cc_library {
2692 name: "libvndk_sp",
2693 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002694 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002695 vndk: {
2696 enabled: true,
2697 support_system_process: true,
2698 },
2699 nocrt: true,
2700 }
2701
2702 cc_library {
2703 name: "libvndk_sp_ext",
2704 vendor: true,
2705 vndk: {
2706 enabled: true,
2707 extends: "libvndk_sp",
2708 support_system_process: true,
2709 },
2710 nocrt: true,
2711 }
2712
2713 cc_library {
2714 name: "libvndk_sp_2",
2715 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002716 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002717 vndk: {
2718 enabled: true,
2719 support_system_process: true,
2720 },
2721 shared_libs: ["libvndk_sp_ext"],
2722 nocrt: true,
2723 }
2724 `)
2725
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002726 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002727 cc_library {
2728 name: "libvndk_sp",
2729 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002730 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002731 vndk: {
2732 enabled: true,
2733 },
2734 nocrt: true,
2735 }
2736
2737 cc_library {
2738 name: "libvndk_sp_ext",
2739 vendor: true,
2740 vndk: {
2741 enabled: true,
2742 extends: "libvndk_sp",
2743 },
2744 nocrt: true,
2745 }
2746
2747 cc_library {
2748 name: "libvndk_sp2",
2749 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002750 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002751 vndk: {
2752 enabled: true,
2753 },
2754 target: {
2755 vendor: {
2756 shared_libs: ["libvndk_sp_ext"],
2757 },
2758 },
2759 nocrt: true,
2760 }
2761 `)
2762}
2763
Justin Yun5f7f7e82019-11-18 19:52:14 +09002764func TestEnforceProductVndkVersion(t *testing.T) {
2765 bp := `
2766 cc_library {
2767 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002768 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002769 }
2770 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002771 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002772 symbol_file: "",
2773 }
2774 cc_library {
2775 name: "libvndk",
2776 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002777 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002778 vndk: {
2779 enabled: true,
2780 },
2781 nocrt: true,
2782 }
2783 cc_library {
2784 name: "libvndk_sp",
2785 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002786 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002787 vndk: {
2788 enabled: true,
2789 support_system_process: true,
2790 },
2791 nocrt: true,
2792 }
2793 cc_library {
2794 name: "libva",
2795 vendor_available: true,
2796 nocrt: true,
2797 }
2798 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002799 name: "libpa",
2800 product_available: true,
2801 nocrt: true,
2802 }
2803 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002804 name: "libproduct_va",
2805 product_specific: true,
2806 vendor_available: true,
2807 nocrt: true,
2808 }
2809 cc_library {
2810 name: "libprod",
2811 product_specific: true,
2812 shared_libs: [
2813 "libllndk",
2814 "libvndk",
2815 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002816 "libpa",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002817 "libproduct_va",
2818 ],
2819 nocrt: true,
2820 }
2821 cc_library {
2822 name: "libvendor",
2823 vendor: true,
2824 shared_libs: [
2825 "libllndk",
2826 "libvndk",
2827 "libvndk_sp",
2828 "libva",
2829 "libproduct_va",
2830 ],
2831 nocrt: true,
2832 }
2833 `
2834
2835 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2836 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2837 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2838 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2839
2840 ctx := testCcWithConfig(t, config)
2841
Jooyung Han261e1582020-10-20 18:54:21 +09002842 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2843 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun5f7f7e82019-11-18 19:52:14 +09002844}
2845
2846func TestEnforceProductVndkVersionErrors(t *testing.T) {
2847 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2848 cc_library {
2849 name: "libprod",
2850 product_specific: true,
2851 shared_libs: [
2852 "libvendor",
2853 ],
2854 nocrt: true,
2855 }
2856 cc_library {
2857 name: "libvendor",
2858 vendor: true,
2859 nocrt: true,
2860 }
2861 `)
2862 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2863 cc_library {
2864 name: "libprod",
2865 product_specific: true,
2866 shared_libs: [
2867 "libsystem",
2868 ],
2869 nocrt: true,
2870 }
2871 cc_library {
2872 name: "libsystem",
2873 nocrt: true,
2874 }
2875 `)
2876 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
2877 cc_library {
2878 name: "libprod",
2879 product_specific: true,
2880 shared_libs: [
2881 "libvndk_private",
2882 ],
2883 nocrt: true,
2884 }
2885 cc_library {
2886 name: "libvndk_private",
2887 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09002888 product_available: false,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002889 vndk: {
2890 enabled: true,
2891 },
2892 nocrt: true,
2893 }
2894 `)
2895 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2896 cc_library {
2897 name: "libprod",
2898 product_specific: true,
2899 shared_libs: [
2900 "libsystem_ext",
2901 ],
2902 nocrt: true,
2903 }
2904 cc_library {
2905 name: "libsystem_ext",
2906 system_ext_specific: true,
2907 nocrt: true,
2908 }
2909 `)
2910 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2911 cc_library {
2912 name: "libsystem",
2913 shared_libs: [
2914 "libproduct_va",
2915 ],
2916 nocrt: true,
2917 }
2918 cc_library {
2919 name: "libproduct_va",
2920 product_specific: true,
2921 vendor_available: true,
2922 nocrt: true,
2923 }
2924 `)
2925}
2926
Jooyung Han38002912019-05-16 04:01:54 +09002927func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002928 bp := `
2929 cc_library {
2930 name: "libvndk",
2931 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002932 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002933 vndk: {
2934 enabled: true,
2935 },
2936 }
2937 cc_library {
2938 name: "libvndksp",
2939 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002940 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002941 vndk: {
2942 enabled: true,
2943 support_system_process: true,
2944 },
2945 }
2946 cc_library {
2947 name: "libvndkprivate",
2948 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09002949 product_available: false,
Colin Cross98be1bb2019-12-13 20:41:13 -08002950 vndk: {
2951 enabled: true,
2952 },
2953 }
2954 cc_library {
2955 name: "libvendor",
2956 vendor: true,
2957 }
2958 cc_library {
2959 name: "libvndkext",
2960 vendor: true,
2961 vndk: {
2962 enabled: true,
2963 extends: "libvndk",
2964 },
2965 }
2966 vndk_prebuilt_shared {
2967 name: "prevndk",
2968 version: "27",
2969 target_arch: "arm",
2970 binder32bit: true,
2971 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002972 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002973 vndk: {
2974 enabled: true,
2975 },
2976 arch: {
2977 arm: {
2978 srcs: ["liba.so"],
2979 },
2980 },
2981 }
2982 cc_library {
2983 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002984 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002985 }
2986 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002987 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002988 symbol_file: "",
2989 }
2990 cc_library {
2991 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07002992 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002993 }
2994 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002995 name: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002996 vendor_available: false,
2997 symbol_file: "",
2998 }`
2999
3000 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09003001 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3002 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3003 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08003004 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09003005
Jooyung Han0302a842019-10-30 18:43:49 +09003006 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09003007 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09003008 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09003009 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09003010 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09003011 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09003012 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09003013 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09003014
Colin Crossfb0c16e2019-11-20 17:12:35 -08003015 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09003016
Jooyung Han38002912019-05-16 04:01:54 +09003017 tests := []struct {
3018 variant string
3019 name string
3020 expected string
3021 }{
3022 {vendorVariant, "libvndk", "native:vndk"},
3023 {vendorVariant, "libvndksp", "native:vndk"},
3024 {vendorVariant, "libvndkprivate", "native:vndk_private"},
3025 {vendorVariant, "libvendor", "native:vendor"},
3026 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08003027 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09003028 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09003029 {coreVariant, "libvndk", "native:platform"},
3030 {coreVariant, "libvndkprivate", "native:platform"},
3031 {coreVariant, "libllndk", "native:platform"},
3032 }
3033 for _, test := range tests {
3034 t.Run(test.name, func(t *testing.T) {
3035 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
3036 assertString(t, module.makeLinkType, test.expected)
3037 })
3038 }
3039}
3040
Jeff Gaston294356f2017-09-27 17:05:30 -07003041var staticLinkDepOrderTestCases = []struct {
3042 // This is a string representation of a map[moduleName][]moduleDependency .
3043 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003044 inStatic string
3045
3046 // This is a string representation of a map[moduleName][]moduleDependency .
3047 // It models the dependencies declared in an Android.bp file.
3048 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07003049
3050 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
3051 // The keys of allOrdered specify which modules we would like to check.
3052 // The values of allOrdered specify the expected result (of the transitive closure of all
3053 // dependencies) for each module to test
3054 allOrdered string
3055
3056 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
3057 // The keys of outOrdered specify which modules we would like to check.
3058 // The values of outOrdered specify the expected result (of the ordered linker command line)
3059 // for each module to test.
3060 outOrdered string
3061}{
3062 // Simple tests
3063 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003064 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07003065 outOrdered: "",
3066 },
3067 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003068 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003069 outOrdered: "a:",
3070 },
3071 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003072 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003073 outOrdered: "a:b; b:",
3074 },
3075 // Tests of reordering
3076 {
3077 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003078 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003079 outOrdered: "a:b,c,d; b:d; c:d; d:",
3080 },
3081 {
3082 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003083 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003084 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
3085 },
3086 {
3087 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003088 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07003089 outOrdered: "a:d,b,e,c; d:b; e:c",
3090 },
3091 {
3092 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003093 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07003094 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
3095 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
3096 },
3097 {
3098 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003099 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 -07003100 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
3101 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
3102 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003103 // shared dependencies
3104 {
3105 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
3106 // So, we don't actually have to check that a shared dependency of c will change the order
3107 // of a library that depends statically on b and on c. We only need to check that if c has
3108 // a shared dependency on b, that that shows up in allOrdered.
3109 inShared: "c:b",
3110 allOrdered: "c:b",
3111 outOrdered: "c:",
3112 },
3113 {
3114 // This test doesn't actually include any shared dependencies but it's a reminder of what
3115 // the second phase of the above test would look like
3116 inStatic: "a:b,c; c:b",
3117 allOrdered: "a:c,b; c:b",
3118 outOrdered: "a:c,b; c:b",
3119 },
Jeff Gaston294356f2017-09-27 17:05:30 -07003120 // tiebreakers for when two modules specifying different orderings and there is no dependency
3121 // to dictate an order
3122 {
3123 // 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 -08003124 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07003125 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
3126 },
3127 {
3128 // 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 -08003129 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 -07003130 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
3131 },
3132 // Tests involving duplicate dependencies
3133 {
3134 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003135 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003136 outOrdered: "a:c,b",
3137 },
3138 {
3139 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003140 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003141 outOrdered: "a:d,c,b",
3142 },
3143 // Tests to confirm the nonexistence of infinite loops.
3144 // These cases should never happen, so as long as the test terminates and the
3145 // result is deterministic then that should be fine.
3146 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003147 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003148 outOrdered: "a:a",
3149 },
3150 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003151 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003152 allOrdered: "a:b,c; b:c,a; c:a,b",
3153 outOrdered: "a:b; b:c; c:a",
3154 },
3155 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003156 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003157 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
3158 outOrdered: "a:c,b; b:a,c; c:b,a",
3159 },
3160}
3161
3162// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
3163func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
3164 // convert from "a:b,c; d:e" to "a:b,c;d:e"
3165 strippedText := strings.Replace(text, " ", "", -1)
3166 if len(strippedText) < 1 {
3167 return []android.Path{}, make(map[android.Path][]android.Path, 0)
3168 }
3169 allDeps = make(map[android.Path][]android.Path, 0)
3170
3171 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
3172 moduleTexts := strings.Split(strippedText, ";")
3173
3174 outputForModuleName := func(moduleName string) android.Path {
3175 return android.PathForTesting(moduleName)
3176 }
3177
3178 for _, moduleText := range moduleTexts {
3179 // convert from "a:b,c" to ["a", "b,c"]
3180 components := strings.Split(moduleText, ":")
3181 if len(components) != 2 {
3182 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
3183 }
3184 moduleName := components[0]
3185 moduleOutput := outputForModuleName(moduleName)
3186 modulesInOrder = append(modulesInOrder, moduleOutput)
3187
3188 depString := components[1]
3189 // convert from "b,c" to ["b", "c"]
3190 depNames := strings.Split(depString, ",")
3191 if len(depString) < 1 {
3192 depNames = []string{}
3193 }
3194 var deps []android.Path
3195 for _, depName := range depNames {
3196 deps = append(deps, outputForModuleName(depName))
3197 }
3198 allDeps[moduleOutput] = deps
3199 }
3200 return modulesInOrder, allDeps
3201}
3202
Jeff Gaston294356f2017-09-27 17:05:30 -07003203func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
3204 for _, moduleName := range moduleNames {
3205 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
3206 output := module.outputFile.Path()
3207 paths = append(paths, output)
3208 }
3209 return paths
3210}
3211
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003212func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07003213 ctx := testCc(t, `
3214 cc_library {
3215 name: "a",
3216 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09003217 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003218 }
3219 cc_library {
3220 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003221 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003222 }
3223 cc_library {
3224 name: "c",
3225 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003226 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003227 }
3228 cc_library {
3229 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09003230 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003231 }
3232
3233 `)
3234
Colin Cross7113d202019-11-20 16:39:12 -08003235 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07003236 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003237 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3238 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07003239
3240 if !reflect.DeepEqual(actual, expected) {
3241 t.Errorf("staticDeps orderings were not propagated correctly"+
3242 "\nactual: %v"+
3243 "\nexpected: %v",
3244 actual,
3245 expected,
3246 )
3247 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09003248}
Jeff Gaston294356f2017-09-27 17:05:30 -07003249
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003250func TestStaticLibDepReorderingWithShared(t *testing.T) {
3251 ctx := testCc(t, `
3252 cc_library {
3253 name: "a",
3254 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09003255 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003256 }
3257 cc_library {
3258 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003259 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003260 }
3261 cc_library {
3262 name: "c",
3263 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003264 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003265 }
3266
3267 `)
3268
Colin Cross7113d202019-11-20 16:39:12 -08003269 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003270 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003271 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3272 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003273
3274 if !reflect.DeepEqual(actual, expected) {
3275 t.Errorf("staticDeps orderings did not account for shared libs"+
3276 "\nactual: %v"+
3277 "\nexpected: %v",
3278 actual,
3279 expected,
3280 )
3281 }
3282}
3283
Jooyung Hanb04a4992020-03-13 18:57:35 +09003284func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07003285 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003286 if !reflect.DeepEqual(actual, expected) {
3287 t.Errorf(message+
3288 "\nactual: %v"+
3289 "\nexpected: %v",
3290 actual,
3291 expected,
3292 )
3293 }
3294}
3295
Jooyung Han61b66e92020-03-21 14:21:46 +00003296func TestLlndkLibrary(t *testing.T) {
3297 ctx := testCc(t, `
3298 cc_library {
3299 name: "libllndk",
3300 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07003301 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003302 }
3303 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003304 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003305 }
Colin Cross127bb8b2020-12-16 16:46:01 -08003306
3307 cc_prebuilt_library_shared {
3308 name: "libllndkprebuilt",
3309 stubs: { versions: ["1", "2"] },
3310 llndk_stubs: "libllndkprebuilt.llndk",
3311 }
3312 llndk_library {
3313 name: "libllndkprebuilt.llndk",
3314 }
3315
3316 cc_library {
3317 name: "libllndk_with_external_headers",
3318 stubs: { versions: ["1", "2"] },
3319 llndk_stubs: "libllndk_with_external_headers.llndk",
3320 header_libs: ["libexternal_headers"],
3321 export_header_lib_headers: ["libexternal_headers"],
3322 }
3323 llndk_library {
3324 name: "libllndk_with_external_headers.llndk",
3325 }
3326 cc_library_headers {
3327 name: "libexternal_headers",
3328 export_include_dirs: ["include"],
3329 vendor_available: true,
3330 }
Jooyung Han61b66e92020-03-21 14:21:46 +00003331 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08003332 actual := ctx.ModuleVariantsForTests("libllndk")
3333 for i := 0; i < len(actual); i++ {
3334 if !strings.HasPrefix(actual[i], "android_vendor.VER_") {
3335 actual = append(actual[:i], actual[i+1:]...)
3336 i--
3337 }
3338 }
Jooyung Han61b66e92020-03-21 14:21:46 +00003339 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00003340 "android_vendor.VER_arm64_armv8-a_shared_1",
3341 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003342 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003343 "android_vendor.VER_arm_armv7-a-neon_shared_1",
3344 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003345 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003346 }
3347 checkEquals(t, "variants for llndk stubs", expected, actual)
3348
Colin Cross127bb8b2020-12-16 16:46:01 -08003349 params := ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00003350 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
3351
Colin Cross127bb8b2020-12-16 16:46:01 -08003352 params = ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00003353 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
3354}
3355
Jiyong Parka46a4d52017-12-14 19:54:34 +09003356func TestLlndkHeaders(t *testing.T) {
3357 ctx := testCc(t, `
3358 llndk_headers {
3359 name: "libllndk_headers",
3360 export_include_dirs: ["my_include"],
3361 }
3362 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003363 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09003364 export_llndk_headers: ["libllndk_headers"],
3365 }
3366 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07003367 name: "libllndk",
3368 llndk_stubs: "libllndk.llndk",
3369 }
3370
3371 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09003372 name: "libvendor",
3373 shared_libs: ["libllndk"],
3374 vendor: true,
3375 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003376 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08003377 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09003378 }
3379 `)
3380
3381 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08003382 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09003383 cflags := cc.Args["cFlags"]
3384 if !strings.Contains(cflags, "-Imy_include") {
3385 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
3386 }
3387}
3388
Logan Chien43d34c32017-12-20 01:17:32 +08003389func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
3390 actual := module.Properties.AndroidMkRuntimeLibs
3391 if !reflect.DeepEqual(actual, expected) {
3392 t.Errorf("incorrect runtime_libs for shared libs"+
3393 "\nactual: %v"+
3394 "\nexpected: %v",
3395 actual,
3396 expected,
3397 )
3398 }
3399}
3400
3401const runtimeLibAndroidBp = `
3402 cc_library {
3403 name: "libvendor_available1",
3404 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003405 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003406 nocrt : true,
3407 system_shared_libs : [],
3408 }
3409 cc_library {
3410 name: "libvendor_available2",
3411 vendor_available: true,
3412 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003413 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003414 nocrt : true,
3415 system_shared_libs : [],
3416 }
3417 cc_library {
3418 name: "libvendor_available3",
3419 vendor_available: true,
3420 runtime_libs: ["libvendor_available1"],
3421 target: {
3422 vendor: {
3423 exclude_runtime_libs: ["libvendor_available1"],
3424 }
3425 },
Yi Konge7fe9912019-06-02 00:53:50 -07003426 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003427 nocrt : true,
3428 system_shared_libs : [],
3429 }
3430 cc_library {
3431 name: "libcore",
3432 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003433 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003434 nocrt : true,
3435 system_shared_libs : [],
3436 }
3437 cc_library {
3438 name: "libvendor1",
3439 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003440 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003441 nocrt : true,
3442 system_shared_libs : [],
3443 }
3444 cc_library {
3445 name: "libvendor2",
3446 vendor: true,
3447 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003448 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003449 nocrt : true,
3450 system_shared_libs : [],
3451 }
3452`
3453
3454func TestRuntimeLibs(t *testing.T) {
3455 ctx := testCc(t, runtimeLibAndroidBp)
3456
3457 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08003458 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003459
3460 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3461 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3462
3463 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
3464 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3465
3466 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3467 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08003468 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003469
3470 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3471 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
3472
3473 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
3474 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
3475}
3476
3477func TestExcludeRuntimeLibs(t *testing.T) {
3478 ctx := testCc(t, runtimeLibAndroidBp)
3479
Colin Cross7113d202019-11-20 16:39:12 -08003480 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003481 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
3482 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3483
Colin Crossfb0c16e2019-11-20 17:12:35 -08003484 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003485 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
3486 checkRuntimeLibs(t, nil, module)
3487}
3488
3489func TestRuntimeLibsNoVndk(t *testing.T) {
3490 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3491
3492 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3493
Colin Cross7113d202019-11-20 16:39:12 -08003494 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003495
3496 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3497 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3498
3499 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
3500 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
3501}
3502
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003503func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003504 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003505 actual := module.Properties.AndroidMkStaticLibs
3506 if !reflect.DeepEqual(actual, expected) {
3507 t.Errorf("incorrect static_libs"+
3508 "\nactual: %v"+
3509 "\nexpected: %v",
3510 actual,
3511 expected,
3512 )
3513 }
3514}
3515
3516const staticLibAndroidBp = `
3517 cc_library {
3518 name: "lib1",
3519 }
3520 cc_library {
3521 name: "lib2",
3522 static_libs: ["lib1"],
3523 }
3524`
3525
3526func TestStaticLibDepExport(t *testing.T) {
3527 ctx := testCc(t, staticLibAndroidBp)
3528
3529 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003530 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003531 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003532 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003533
3534 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003535 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003536 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3537 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003538 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003539}
3540
Jiyong Parkd08b6972017-09-26 10:50:54 +09003541var compilerFlagsTestCases = []struct {
3542 in string
3543 out bool
3544}{
3545 {
3546 in: "a",
3547 out: false,
3548 },
3549 {
3550 in: "-a",
3551 out: true,
3552 },
3553 {
3554 in: "-Ipath/to/something",
3555 out: false,
3556 },
3557 {
3558 in: "-isystempath/to/something",
3559 out: false,
3560 },
3561 {
3562 in: "--coverage",
3563 out: false,
3564 },
3565 {
3566 in: "-include a/b",
3567 out: true,
3568 },
3569 {
3570 in: "-include a/b c/d",
3571 out: false,
3572 },
3573 {
3574 in: "-DMACRO",
3575 out: true,
3576 },
3577 {
3578 in: "-DMAC RO",
3579 out: false,
3580 },
3581 {
3582 in: "-a -b",
3583 out: false,
3584 },
3585 {
3586 in: "-DMACRO=definition",
3587 out: true,
3588 },
3589 {
3590 in: "-DMACRO=defi nition",
3591 out: true, // TODO(jiyong): this should be false
3592 },
3593 {
3594 in: "-DMACRO(x)=x + 1",
3595 out: true,
3596 },
3597 {
3598 in: "-DMACRO=\"defi nition\"",
3599 out: true,
3600 },
3601}
3602
3603type mockContext struct {
3604 BaseModuleContext
3605 result bool
3606}
3607
3608func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3609 // CheckBadCompilerFlags calls this function when the flag should be rejected
3610 ctx.result = false
3611}
3612
3613func TestCompilerFlags(t *testing.T) {
3614 for _, testCase := range compilerFlagsTestCases {
3615 ctx := &mockContext{result: true}
3616 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3617 if ctx.result != testCase.out {
3618 t.Errorf("incorrect output:")
3619 t.Errorf(" input: %#v", testCase.in)
3620 t.Errorf(" expected: %#v", testCase.out)
3621 t.Errorf(" got: %#v", ctx.result)
3622 }
3623 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003624}
Jiyong Park374510b2018-03-19 18:23:01 +09003625
3626func TestVendorPublicLibraries(t *testing.T) {
3627 ctx := testCc(t, `
3628 cc_library_headers {
3629 name: "libvendorpublic_headers",
3630 export_include_dirs: ["my_include"],
3631 }
3632 vendor_public_library {
3633 name: "libvendorpublic",
3634 symbol_file: "",
3635 export_public_headers: ["libvendorpublic_headers"],
3636 }
3637 cc_library {
3638 name: "libvendorpublic",
3639 srcs: ["foo.c"],
3640 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003641 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003642 nocrt: true,
3643 }
3644
3645 cc_library {
3646 name: "libsystem",
3647 shared_libs: ["libvendorpublic"],
3648 vendor: false,
3649 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003650 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003651 nocrt: true,
3652 }
3653 cc_library {
3654 name: "libvendor",
3655 shared_libs: ["libvendorpublic"],
3656 vendor: true,
3657 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003658 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003659 nocrt: true,
3660 }
3661 `)
3662
Colin Cross7113d202019-11-20 16:39:12 -08003663 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003664 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003665
3666 // test if header search paths are correctly added
3667 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003668 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003669 cflags := cc.Args["cFlags"]
3670 if !strings.Contains(cflags, "-Imy_include") {
3671 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3672 }
3673
3674 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003675 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003676 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003677 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003678 if !strings.Contains(libflags, stubPaths[0].String()) {
3679 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3680 }
3681
3682 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003683 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003684 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003685 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003686 if !strings.Contains(libflags, stubPaths[0].String()) {
3687 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3688 }
3689
3690}
Jiyong Park37b25202018-07-11 10:49:27 +09003691
3692func TestRecovery(t *testing.T) {
3693 ctx := testCc(t, `
3694 cc_library_shared {
3695 name: "librecovery",
3696 recovery: true,
3697 }
3698 cc_library_shared {
3699 name: "librecovery32",
3700 recovery: true,
3701 compile_multilib:"32",
3702 }
Jiyong Park5baac542018-08-28 09:55:37 +09003703 cc_library_shared {
3704 name: "libHalInRecovery",
3705 recovery_available: true,
3706 vendor: true,
3707 }
Jiyong Park37b25202018-07-11 10:49:27 +09003708 `)
3709
3710 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003711 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003712 if len(variants) != 1 || !android.InList(arm64, variants) {
3713 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3714 }
3715
3716 variants = ctx.ModuleVariantsForTests("librecovery32")
3717 if android.InList(arm64, variants) {
3718 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3719 }
Jiyong Park5baac542018-08-28 09:55:37 +09003720
3721 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3722 if !recoveryModule.Platform() {
3723 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3724 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003725}
Jiyong Park5baac542018-08-28 09:55:37 +09003726
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003727func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3728 bp := `
3729 cc_prebuilt_test_library_shared {
3730 name: "test_lib",
3731 relative_install_path: "foo/bar/baz",
3732 srcs: ["srcpath/dontusethispath/baz.so"],
3733 }
3734
3735 cc_test {
3736 name: "main_test",
3737 data_libs: ["test_lib"],
3738 gtest: false,
3739 }
3740 `
3741
3742 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3743 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3744 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3745 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3746
3747 ctx := testCcWithConfig(t, config)
3748 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3749 testBinary := module.(*Module).linker.(*testBinary)
3750 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3751 if err != nil {
3752 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3753 }
3754 if len(outputFiles) != 1 {
3755 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3756 }
3757 if len(testBinary.dataPaths()) != 1 {
3758 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3759 }
3760
3761 outputPath := outputFiles[0].String()
3762
3763 if !strings.HasSuffix(outputPath, "/main_test") {
3764 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3765 }
3766 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
3767 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3768 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3769 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3770 }
3771}
3772
Jiyong Park7ed9de32018-10-15 22:25:07 +09003773func TestVersionedStubs(t *testing.T) {
3774 ctx := testCc(t, `
3775 cc_library_shared {
3776 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003777 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003778 stubs: {
3779 symbol_file: "foo.map.txt",
3780 versions: ["1", "2", "3"],
3781 },
3782 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003783
Jiyong Park7ed9de32018-10-15 22:25:07 +09003784 cc_library_shared {
3785 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003786 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003787 shared_libs: ["libFoo#1"],
3788 }`)
3789
3790 variants := ctx.ModuleVariantsForTests("libFoo")
3791 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003792 "android_arm64_armv8-a_shared",
3793 "android_arm64_armv8-a_shared_1",
3794 "android_arm64_armv8-a_shared_2",
3795 "android_arm64_armv8-a_shared_3",
3796 "android_arm_armv7-a-neon_shared",
3797 "android_arm_armv7-a-neon_shared_1",
3798 "android_arm_armv7-a-neon_shared_2",
3799 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003800 }
3801 variantsMismatch := false
3802 if len(variants) != len(expectedVariants) {
3803 variantsMismatch = true
3804 } else {
3805 for _, v := range expectedVariants {
3806 if !inList(v, variants) {
3807 variantsMismatch = false
3808 }
3809 }
3810 }
3811 if variantsMismatch {
3812 t.Errorf("variants of libFoo expected:\n")
3813 for _, v := range expectedVariants {
3814 t.Errorf("%q\n", v)
3815 }
3816 t.Errorf(", but got:\n")
3817 for _, v := range variants {
3818 t.Errorf("%q\n", v)
3819 }
3820 }
3821
Colin Cross7113d202019-11-20 16:39:12 -08003822 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003823 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003824 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003825 if !strings.Contains(libFlags, libFoo1StubPath) {
3826 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3827 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003828
Colin Cross7113d202019-11-20 16:39:12 -08003829 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003830 cFlags := libBarCompileRule.Args["cFlags"]
3831 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3832 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3833 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3834 }
Jiyong Park37b25202018-07-11 10:49:27 +09003835}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003836
Jooyung Hanb04a4992020-03-13 18:57:35 +09003837func TestVersioningMacro(t *testing.T) {
3838 for _, tc := range []struct{ moduleName, expected string }{
3839 {"libc", "__LIBC_API__"},
3840 {"libfoo", "__LIBFOO_API__"},
3841 {"libfoo@1", "__LIBFOO_1_API__"},
3842 {"libfoo-v1", "__LIBFOO_V1_API__"},
3843 {"libfoo.v1", "__LIBFOO_V1_API__"},
3844 } {
3845 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3846 }
3847}
3848
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003849func TestStaticExecutable(t *testing.T) {
3850 ctx := testCc(t, `
3851 cc_binary {
3852 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003853 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003854 static_executable: true,
3855 }`)
3856
Colin Cross7113d202019-11-20 16:39:12 -08003857 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003858 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3859 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003860 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003861 for _, lib := range systemStaticLibs {
3862 if !strings.Contains(libFlags, lib) {
3863 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3864 }
3865 }
3866 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3867 for _, lib := range systemSharedLibs {
3868 if strings.Contains(libFlags, lib) {
3869 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3870 }
3871 }
3872}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003873
3874func TestStaticDepsOrderWithStubs(t *testing.T) {
3875 ctx := testCc(t, `
3876 cc_binary {
3877 name: "mybin",
3878 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003879 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003880 static_executable: true,
3881 stl: "none",
3882 }
3883
3884 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003885 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003886 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003887 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003888 stl: "none",
3889 }
3890
3891 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003892 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003893 srcs: ["foo.c"],
3894 stl: "none",
3895 stubs: {
3896 versions: ["1"],
3897 },
3898 }`)
3899
Colin Cross0de8a1e2020-09-18 14:15:30 -07003900 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3901 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003902 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003903
3904 if !reflect.DeepEqual(actual, expected) {
3905 t.Errorf("staticDeps orderings were not propagated correctly"+
3906 "\nactual: %v"+
3907 "\nexpected: %v",
3908 actual,
3909 expected,
3910 )
3911 }
3912}
Jooyung Han38002912019-05-16 04:01:54 +09003913
Jooyung Hand48f3c32019-08-23 11:18:57 +09003914func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3915 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3916 cc_library {
3917 name: "libA",
3918 srcs: ["foo.c"],
3919 shared_libs: ["libB"],
3920 stl: "none",
3921 }
3922
3923 cc_library {
3924 name: "libB",
3925 srcs: ["foo.c"],
3926 enabled: false,
3927 stl: "none",
3928 }
3929 `)
3930}
3931
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003932// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3933// correctly.
3934func TestFuzzTarget(t *testing.T) {
3935 ctx := testCc(t, `
3936 cc_fuzz {
3937 name: "fuzz_smoke_test",
3938 srcs: ["foo.c"],
3939 }`)
3940
Paul Duffin075c4172019-12-19 19:06:13 +00003941 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003942 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3943}
3944
Jiyong Park29074592019-07-07 16:27:47 +09003945func TestAidl(t *testing.T) {
3946}
3947
Jooyung Han38002912019-05-16 04:01:54 +09003948func assertString(t *testing.T, got, expected string) {
3949 t.Helper()
3950 if got != expected {
3951 t.Errorf("expected %q got %q", expected, got)
3952 }
3953}
3954
3955func assertArrayString(t *testing.T, got, expected []string) {
3956 t.Helper()
3957 if len(got) != len(expected) {
3958 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3959 return
3960 }
3961 for i := range got {
3962 if got[i] != expected[i] {
3963 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3964 i, expected[i], expected, got[i], got)
3965 return
3966 }
3967 }
3968}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003969
Jooyung Han0302a842019-10-30 18:43:49 +09003970func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3971 t.Helper()
3972 assertArrayString(t, android.SortedStringKeys(m), expected)
3973}
3974
Colin Crosse1bb5d02019-09-24 14:55:04 -07003975func TestDefaults(t *testing.T) {
3976 ctx := testCc(t, `
3977 cc_defaults {
3978 name: "defaults",
3979 srcs: ["foo.c"],
3980 static: {
3981 srcs: ["bar.c"],
3982 },
3983 shared: {
3984 srcs: ["baz.c"],
3985 },
3986 }
3987
3988 cc_library_static {
3989 name: "libstatic",
3990 defaults: ["defaults"],
3991 }
3992
3993 cc_library_shared {
3994 name: "libshared",
3995 defaults: ["defaults"],
3996 }
3997
3998 cc_library {
3999 name: "libboth",
4000 defaults: ["defaults"],
4001 }
4002
4003 cc_binary {
4004 name: "binary",
4005 defaults: ["defaults"],
4006 }`)
4007
4008 pathsToBase := func(paths android.Paths) []string {
4009 var ret []string
4010 for _, p := range paths {
4011 ret = append(ret, p.Base())
4012 }
4013 return ret
4014 }
4015
Colin Cross7113d202019-11-20 16:39:12 -08004016 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004017 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4018 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
4019 }
Colin Cross7113d202019-11-20 16:39:12 -08004020 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004021 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4022 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
4023 }
Colin Cross7113d202019-11-20 16:39:12 -08004024 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004025 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
4026 t.Errorf("binary ld rule wanted %q, got %q", w, g)
4027 }
4028
Colin Cross7113d202019-11-20 16:39:12 -08004029 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004030 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4031 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
4032 }
Colin Cross7113d202019-11-20 16:39:12 -08004033 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004034 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4035 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
4036 }
4037}
Colin Crosseabaedd2020-02-06 17:01:55 -08004038
4039func TestProductVariableDefaults(t *testing.T) {
4040 bp := `
4041 cc_defaults {
4042 name: "libfoo_defaults",
4043 srcs: ["foo.c"],
4044 cppflags: ["-DFOO"],
4045 product_variables: {
4046 debuggable: {
4047 cppflags: ["-DBAR"],
4048 },
4049 },
4050 }
4051
4052 cc_library {
4053 name: "libfoo",
4054 defaults: ["libfoo_defaults"],
4055 }
4056 `
4057
4058 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4059 config.TestProductVariables.Debuggable = BoolPtr(true)
4060
Colin Crossae8600b2020-10-29 17:09:13 -07004061 ctx := CreateTestContext(config)
Colin Crosseabaedd2020-02-06 17:01:55 -08004062 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
4063 ctx.BottomUp("variable", android.VariableMutator).Parallel()
4064 })
Colin Crossae8600b2020-10-29 17:09:13 -07004065 ctx.Register()
Colin Crosseabaedd2020-02-06 17:01:55 -08004066
4067 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
4068 android.FailIfErrored(t, errs)
4069 _, errs = ctx.PrepareBuildActions(config)
4070 android.FailIfErrored(t, errs)
4071
4072 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
4073 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
4074 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
4075 }
4076}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004077
4078func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4079 t.Parallel()
4080 bp := `
4081 cc_library_static {
4082 name: "libfoo",
4083 srcs: ["foo.c"],
4084 whole_static_libs: ["libbar"],
4085 }
4086
4087 cc_library_static {
4088 name: "libbar",
4089 whole_static_libs: ["libmissing"],
4090 }
4091 `
4092
4093 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4094 config.TestProductVariables.Allow_missing_dependencies = BoolPtr(true)
4095
Colin Crossae8600b2020-10-29 17:09:13 -07004096 ctx := CreateTestContext(config)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004097 ctx.SetAllowMissingDependencies(true)
Colin Crossae8600b2020-10-29 17:09:13 -07004098 ctx.Register()
Colin Crosse4f6eba2020-09-22 18:11:25 -07004099
4100 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
4101 android.FailIfErrored(t, errs)
4102 _, errs = ctx.PrepareBuildActions(config)
4103 android.FailIfErrored(t, errs)
4104
4105 libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
4106 if g, w := libbar.Rule, android.ErrorRule; g != w {
4107 t.Fatalf("Expected libbar rule to be %q, got %q", w, g)
4108 }
4109
4110 if g, w := libbar.Args["error"], "missing dependencies: libmissing"; !strings.Contains(g, w) {
4111 t.Errorf("Expected libbar error to contain %q, was %q", w, g)
4112 }
4113
4114 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
4115 if g, w := libfoo.Inputs.Strings(), libbar.Output.String(); !android.InList(w, g) {
4116 t.Errorf("Expected libfoo.a to depend on %q, got %q", w, g)
4117 }
4118
4119}
Colin Crosse9fe2942020-11-10 18:12:15 -08004120
4121func TestInstallSharedLibs(t *testing.T) {
4122 bp := `
4123 cc_binary {
4124 name: "bin",
4125 host_supported: true,
4126 shared_libs: ["libshared"],
4127 runtime_libs: ["libruntime"],
4128 srcs: [":gen"],
4129 }
4130
4131 cc_library_shared {
4132 name: "libshared",
4133 host_supported: true,
4134 shared_libs: ["libtransitive"],
4135 }
4136
4137 cc_library_shared {
4138 name: "libtransitive",
4139 host_supported: true,
4140 }
4141
4142 cc_library_shared {
4143 name: "libruntime",
4144 host_supported: true,
4145 }
4146
4147 cc_binary_host {
4148 name: "tool",
4149 srcs: ["foo.cpp"],
4150 }
4151
4152 genrule {
4153 name: "gen",
4154 tools: ["tool"],
4155 out: ["gen.cpp"],
4156 cmd: "$(location tool) $(out)",
4157 }
4158 `
4159
4160 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4161 ctx := testCcWithConfig(t, config)
4162
4163 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4164 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4165 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4166 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4167 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4168
4169 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4170 t.Errorf("expected host bin dependency %q, got %q", w, g)
4171 }
4172
4173 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4174 t.Errorf("expected host bin dependency %q, got %q", w, g)
4175 }
4176
4177 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4178 t.Errorf("expected host bin dependency %q, got %q", w, g)
4179 }
4180
4181 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4182 t.Errorf("expected host bin dependency %q, got %q", w, g)
4183 }
4184
4185 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4186 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4187 }
4188
4189 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4190 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4191 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4192 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4193
4194 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4195 t.Errorf("expected device bin dependency %q, got %q", w, g)
4196 }
4197
4198 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4199 t.Errorf("expected device bin dependency %q, got %q", w, g)
4200 }
4201
4202 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4203 t.Errorf("expected device bin dependency %q, got %q", w, g)
4204 }
4205
4206 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4207 t.Errorf("expected device bin dependency %q, got %q", w, g)
4208 }
4209
4210 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4211 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4212 }
4213
4214}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004215
4216func TestStubsLibReexportsHeaders(t *testing.T) {
4217 ctx := testCc(t, `
4218 cc_library_shared {
4219 name: "libclient",
4220 srcs: ["foo.c"],
4221 shared_libs: ["libfoo#1"],
4222 }
4223
4224 cc_library_shared {
4225 name: "libfoo",
4226 srcs: ["foo.c"],
4227 shared_libs: ["libbar"],
4228 export_shared_lib_headers: ["libbar"],
4229 stubs: {
4230 symbol_file: "foo.map.txt",
4231 versions: ["1", "2", "3"],
4232 },
4233 }
4234
4235 cc_library_shared {
4236 name: "libbar",
4237 export_include_dirs: ["include/libbar"],
4238 srcs: ["foo.c"],
4239 }`)
4240
4241 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4242
4243 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4244 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4245 }
4246}