blob: f616cf3a7831c104b6aff616045bfd67365ed655 [file] [log] [blame]
Colin Crossd00350c2017-11-17 10:55:38 -08001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross74d1ec02015-04-28 13:30:13 -070015package cc
16
17import (
Jeff Gaston294356f2017-09-27 17:05:30 -070018 "fmt"
Jiyong Park6a43f042017-10-12 23:05:00 +090019 "io/ioutil"
20 "os"
Inseob Kim1f086e22019-05-09 13:29:15 +090021 "path/filepath"
Colin Cross74d1ec02015-04-28 13:30:13 -070022 "reflect"
Jeff Gaston294356f2017-09-27 17:05:30 -070023 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070024 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070025
26 "android/soong/android"
Colin Cross74d1ec02015-04-28 13:30:13 -070027)
28
Jiyong Park6a43f042017-10-12 23:05:00 +090029var buildDir string
30
31func setUp() {
32 var err error
33 buildDir, err = ioutil.TempDir("", "soong_cc_test")
34 if err != nil {
35 panic(err)
36 }
37}
38
39func tearDown() {
40 os.RemoveAll(buildDir)
41}
42
43func TestMain(m *testing.M) {
44 run := func() int {
45 setUp()
46 defer tearDown()
47
48 return m.Run()
49 }
50
51 os.Exit(run())
52}
53
Colin Cross98be1bb2019-12-13 20:41:13 -080054func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070055 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -070056 ctx := CreateTestContext(config)
57 ctx.Register()
Logan Chienf3511742017-10-31 18:04:35 +080058
Jeff Gastond3e141d2017-08-08 17:46:01 -070059 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +080060 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +090061 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +080062 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +090063
64 return ctx
65}
66
Logan Chienf3511742017-10-31 18:04:35 +080067func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080068 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080069 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070070 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
71 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +080072
Colin Cross98be1bb2019-12-13 20:41:13 -080073 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080074}
75
76func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080077 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080078 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070079 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +080080
Colin Cross98be1bb2019-12-13 20:41:13 -080081 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080082}
83
Justin Yun5f7f7e82019-11-18 19:52:14 +090084func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +080085 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +080086
Colin Crossae8600b2020-10-29 17:09:13 -070087 ctx := CreateTestContext(config)
88 ctx.Register()
Logan Chienf3511742017-10-31 18:04:35 +080089
90 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
91 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +080092 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +080093 return
94 }
95
96 _, errs = ctx.PrepareBuildActions(config)
97 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +080098 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +080099 return
100 }
101
102 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
103}
104
Justin Yun5f7f7e82019-11-18 19:52:14 +0900105func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900106 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900107 config := TestConfig(buildDir, android.Android, nil, bp, nil)
108 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
109 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
110 testCcErrorWithConfig(t, pattern, config)
111 return
112}
113
114func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900115 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900116 config := TestConfig(buildDir, android.Android, nil, bp, nil)
117 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
118 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
119 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
120 testCcErrorWithConfig(t, pattern, config)
121 return
122}
123
Logan Chienf3511742017-10-31 18:04:35 +0800124const (
Colin Cross7113d202019-11-20 16:39:12 -0800125 coreVariant = "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800126 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun5f7f7e82019-11-18 19:52:14 +0900127 productVariant = "android_product.VER_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800128 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800129)
130
Doug Hornc32c6b02019-01-17 14:44:05 -0800131func TestFuchsiaDeps(t *testing.T) {
132 t.Helper()
133
134 bp := `
135 cc_library {
136 name: "libTest",
137 srcs: ["foo.c"],
138 target: {
139 fuchsia: {
140 srcs: ["bar.c"],
141 },
142 },
143 }`
144
Colin Cross98be1bb2019-12-13 20:41:13 -0800145 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
146 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800147
148 rt := false
149 fb := false
150
151 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
152 implicits := ld.Implicits
153 for _, lib := range implicits {
154 if strings.Contains(lib.Rel(), "libcompiler_rt") {
155 rt = true
156 }
157
158 if strings.Contains(lib.Rel(), "libbioniccompat") {
159 fb = true
160 }
161 }
162
163 if !rt || !fb {
164 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
165 }
166}
167
168func TestFuchsiaTargetDecl(t *testing.T) {
169 t.Helper()
170
171 bp := `
172 cc_library {
173 name: "libTest",
174 srcs: ["foo.c"],
175 target: {
176 fuchsia: {
177 srcs: ["bar.c"],
178 },
179 },
180 }`
181
Colin Cross98be1bb2019-12-13 20:41:13 -0800182 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
183 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800184 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
185 var objs []string
186 for _, o := range ld.Inputs {
187 objs = append(objs, o.Base())
188 }
189 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
190 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
191 }
192}
193
Jiyong Park6a43f042017-10-12 23:05:00 +0900194func TestVendorSrc(t *testing.T) {
195 ctx := testCc(t, `
196 cc_library {
197 name: "libTest",
198 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700199 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800200 nocrt: true,
201 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900202 vendor_available: true,
203 target: {
204 vendor: {
205 srcs: ["bar.c"],
206 },
207 },
208 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900209 `)
210
Logan Chienf3511742017-10-31 18:04:35 +0800211 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900212 var objs []string
213 for _, o := range ld.Inputs {
214 objs = append(objs, o.Base())
215 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800216 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900217 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
218 }
219}
220
Logan Chienf3511742017-10-31 18:04:35 +0800221func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900222 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800223
Logan Chiend3c59a22018-03-29 14:08:15 +0800224 t.Helper()
225
Justin Yun0ecf0b22020-02-28 15:07:59 +0900226 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Ivan Lozano52767be2019-10-18 14:49:46 -0700227 if !mod.HasVendorVariant() {
Justin Yun0ecf0b22020-02-28 15:07:59 +0900228 t.Errorf("%q must have variant %q", name, variant)
Logan Chienf3511742017-10-31 18:04:35 +0800229 }
230
231 // Check library properties.
232 lib, ok := mod.compiler.(*libraryDecorator)
233 if !ok {
234 t.Errorf("%q must have libraryDecorator", name)
235 } else if lib.baseInstaller.subDir != subDir {
236 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
237 lib.baseInstaller.subDir)
238 }
239
240 // Check VNDK properties.
241 if mod.vndkdep == nil {
242 t.Fatalf("%q must have `vndkdep`", name)
243 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700244 if !mod.IsVndk() {
245 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800246 }
247 if mod.isVndkSp() != isVndkSp {
248 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
249 }
250
251 // Check VNDK extension properties.
252 isVndkExt := extends != ""
253 if mod.isVndkExt() != isVndkExt {
254 t.Errorf("%q isVndkExt() must equal to %t", name, isVndkExt)
255 }
256
257 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
258 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
259 }
260}
261
Bill Peckham945441c2020-08-31 16:07:58 -0700262func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool) {
263 t.Helper()
Jooyung Han39edb6c2019-11-06 16:53:07 +0900264 mod, ok := ctx.ModuleForTests(moduleName, variant).Module().(android.OutputFileProducer)
265 if !ok {
266 t.Errorf("%q must have output\n", moduleName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900267 return
268 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900269 outputFiles, err := mod.OutputFiles("")
270 if err != nil || len(outputFiles) != 1 {
271 t.Errorf("%q must have single output\n", moduleName)
272 return
273 }
274 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900275
Bill Peckham945441c2020-08-31 16:07:58 -0700276 if include {
277 out := singleton.Output(snapshotPath)
278 if out.Input.String() != outputFiles[0].String() {
279 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
280 }
281 } else {
282 out := singleton.MaybeOutput(snapshotPath)
283 if out.Rule != nil {
284 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
285 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900286 }
287}
288
Bill Peckham945441c2020-08-31 16:07:58 -0700289func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
290 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true)
291}
292
293func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
294 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false)
295}
296
Jooyung Han2216fb12019-11-06 16:46:15 +0900297func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
298 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800299 content := android.ContentFromFileRuleForTests(t, params)
300 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900301 assertArrayString(t, actual, expected)
302}
303
Jooyung Han097087b2019-10-22 19:32:18 +0900304func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
305 t.Helper()
306 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900307 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
308}
309
310func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
311 t.Helper()
312 vndkLibraries := ctx.ModuleForTests(module, "")
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +0900313
314 var output string
315 if module != "vndkcorevariant.libraries.txt" {
316 output = insertVndkVersion(module, "VER")
317 } else {
318 output = module
319 }
320
Jooyung Han2216fb12019-11-06 16:46:15 +0900321 checkWriteFileOutput(t, vndkLibraries.Output(output), expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900322}
323
Logan Chienf3511742017-10-31 18:04:35 +0800324func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800325 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800326 cc_library {
327 name: "libvndk",
328 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900329 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800330 vndk: {
331 enabled: true,
332 },
333 nocrt: true,
334 }
335
336 cc_library {
337 name: "libvndk_private",
338 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +0900339 product_available: false,
Logan Chienf3511742017-10-31 18:04:35 +0800340 vndk: {
341 enabled: true,
342 },
343 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900344 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800345 }
346
347 cc_library {
348 name: "libvndk_sp",
349 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900350 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800351 vndk: {
352 enabled: true,
353 support_system_process: true,
354 },
355 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900356 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800357 }
358
359 cc_library {
360 name: "libvndk_sp_private",
361 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +0900362 product_available: false,
Logan Chienf3511742017-10-31 18:04:35 +0800363 vndk: {
364 enabled: true,
365 support_system_process: true,
366 },
367 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900368 target: {
369 vendor: {
370 suffix: "-x",
371 },
372 },
Logan Chienf3511742017-10-31 18:04:35 +0800373 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900374 vndk_libraries_txt {
375 name: "llndk.libraries.txt",
376 }
377 vndk_libraries_txt {
378 name: "vndkcore.libraries.txt",
379 }
380 vndk_libraries_txt {
381 name: "vndksp.libraries.txt",
382 }
383 vndk_libraries_txt {
384 name: "vndkprivate.libraries.txt",
385 }
386 vndk_libraries_txt {
387 name: "vndkcorevariant.libraries.txt",
388 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800389 `
390
391 config := TestConfig(buildDir, android.Android, nil, bp, nil)
392 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900393 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Colin Cross98be1bb2019-12-13 20:41:13 -0800394 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
395
396 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800397
Jooyung Han261e1582020-10-20 18:54:21 +0900398 // subdir == "" because VNDK libs are not supposed to be installed separately.
399 // They are installed as part of VNDK APEX instead.
400 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
401 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
402 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
403 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900404
Justin Yun63e9ec72020-10-29 16:49:43 +0900405 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
406 checkVndkModule(t, ctx, "libvndk_private", "", false, "", productVariant)
407 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
408 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", productVariant)
409
Inseob Kim1f086e22019-05-09 13:29:15 +0900410 // Check VNDK snapshot output.
411
412 snapshotDir := "vndk-snapshot"
413 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
414
415 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
416 "arm64", "armv8-a"))
417 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
418 "arm", "armv7-a-neon"))
419
420 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
421 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
422 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
423 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
424
Colin Crossfb0c16e2019-11-20 17:12:35 -0800425 variant := "android_vendor.VER_arm64_armv8-a_shared"
426 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900427
Inseob Kim7f283f42020-06-01 21:53:49 +0900428 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
429
430 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
431 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
432 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
433 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900434
Jooyung Han39edb6c2019-11-06 16:53:07 +0900435 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900436 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
437 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
438 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
439 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900440
Jooyung Han097087b2019-10-22 19:32:18 +0900441 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
442 "LLNDK: libc.so",
443 "LLNDK: libdl.so",
444 "LLNDK: libft2.so",
445 "LLNDK: libm.so",
446 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900447 "VNDK-SP: libvndk_sp-x.so",
448 "VNDK-SP: libvndk_sp_private-x.so",
449 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900450 "VNDK-core: libvndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900451 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900452 "VNDK-private: libvndk-private.so",
453 "VNDK-private: libvndk_sp_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900454 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900455 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
456 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so"})
457 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so"})
458 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so"})
459 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
460}
461
Yo Chiangbba545e2020-06-09 16:15:37 +0800462func TestVndkWithHostSupported(t *testing.T) {
463 ctx := testCc(t, `
464 cc_library {
465 name: "libvndk_host_supported",
466 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900467 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800468 vndk: {
469 enabled: true,
470 },
471 host_supported: true,
472 }
473
474 cc_library {
475 name: "libvndk_host_supported_but_disabled_on_device",
476 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900477 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800478 vndk: {
479 enabled: true,
480 },
481 host_supported: true,
482 enabled: false,
483 target: {
484 host: {
485 enabled: true,
486 }
487 }
488 }
489
490 vndk_libraries_txt {
491 name: "vndkcore.libraries.txt",
492 }
493 `)
494
495 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
496}
497
Jooyung Han2216fb12019-11-06 16:46:15 +0900498func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800499 bp := `
Jooyung Han2216fb12019-11-06 16:46:15 +0900500 vndk_libraries_txt {
501 name: "llndk.libraries.txt",
Colin Cross98be1bb2019-12-13 20:41:13 -0800502 }`
503 config := TestConfig(buildDir, android.Android, nil, bp, nil)
504 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
505 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
506 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900507
508 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900509 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900510 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900511}
512
513func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800514 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900515 cc_library {
516 name: "libvndk",
517 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900518 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900519 vndk: {
520 enabled: true,
521 },
522 nocrt: true,
523 }
524
525 cc_library {
526 name: "libvndk_sp",
527 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900528 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900529 vndk: {
530 enabled: true,
531 support_system_process: true,
532 },
533 nocrt: true,
534 }
535
536 cc_library {
537 name: "libvndk2",
538 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +0900539 product_available: false,
Jooyung Han097087b2019-10-22 19:32:18 +0900540 vndk: {
541 enabled: true,
542 },
543 nocrt: true,
544 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900545
546 vndk_libraries_txt {
547 name: "vndkcorevariant.libraries.txt",
548 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800549 `
550
551 config := TestConfig(buildDir, android.Android, nil, bp, nil)
552 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
553 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
554 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
555
556 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
557
558 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900559
Jooyung Han2216fb12019-11-06 16:46:15 +0900560 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900561}
562
Chris Parsons79d66a52020-06-05 17:26:16 -0400563func TestDataLibs(t *testing.T) {
564 bp := `
565 cc_test_library {
566 name: "test_lib",
567 srcs: ["test_lib.cpp"],
568 gtest: false,
569 }
570
571 cc_test {
572 name: "main_test",
573 data_libs: ["test_lib"],
574 gtest: false,
575 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400576 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400577
578 config := TestConfig(buildDir, android.Android, nil, bp, nil)
579 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
580 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
581 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
582
583 ctx := testCcWithConfig(t, config)
584 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
585 testBinary := module.(*Module).linker.(*testBinary)
586 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
587 if err != nil {
588 t.Errorf("Expected cc_test to produce output files, error: %s", err)
589 return
590 }
591 if len(outputFiles) != 1 {
592 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
593 return
594 }
595 if len(testBinary.dataPaths()) != 1 {
596 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
597 return
598 }
599
600 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400601 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400602
603 if !strings.HasSuffix(outputPath, "/main_test") {
604 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
605 return
606 }
607 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
608 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
609 return
610 }
611}
612
Chris Parsons216e10a2020-07-09 17:12:52 -0400613func TestDataLibsRelativeInstallPath(t *testing.T) {
614 bp := `
615 cc_test_library {
616 name: "test_lib",
617 srcs: ["test_lib.cpp"],
618 relative_install_path: "foo/bar/baz",
619 gtest: false,
620 }
621
622 cc_test {
623 name: "main_test",
624 data_libs: ["test_lib"],
625 gtest: false,
626 }
627 `
628
629 config := TestConfig(buildDir, android.Android, nil, bp, nil)
630 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
631 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
632 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
633
634 ctx := testCcWithConfig(t, config)
635 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
636 testBinary := module.(*Module).linker.(*testBinary)
637 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
638 if err != nil {
639 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
640 }
641 if len(outputFiles) != 1 {
642 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
643 }
644 if len(testBinary.dataPaths()) != 1 {
645 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
646 }
647
648 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400649
650 if !strings.HasSuffix(outputPath, "/main_test") {
651 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
652 }
653 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
654 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
655 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400656 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400657 }
658}
659
Jooyung Han0302a842019-10-30 18:43:49 +0900660func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900661 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900662 cc_library {
663 name: "libvndk",
664 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900665 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900666 vndk: {
667 enabled: true,
668 },
669 nocrt: true,
670 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900671 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900672
673 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
674 "LLNDK: libc.so",
675 "LLNDK: libdl.so",
676 "LLNDK: libft2.so",
677 "LLNDK: libm.so",
678 "VNDK-SP: libc++.so",
679 "VNDK-core: libvndk.so",
680 "VNDK-private: libft2.so",
681 })
Logan Chienf3511742017-10-31 18:04:35 +0800682}
683
Justin Yun63e9ec72020-10-29 16:49:43 +0900684func TestVndkModuleError(t *testing.T) {
685 // Check the error message for vendor_available and product_available properties.
686 testCcError(t, "product_available: may not have different value than `vendor_available`", `
687 cc_library {
688 name: "libvndk",
689 vendor_available: true,
690 product_available: false,
691 vndk: {
692 enabled: true,
693 },
694 nocrt: true,
695 }
696 `)
697}
698
Logan Chiend3c59a22018-03-29 14:08:15 +0800699func TestVndkDepError(t *testing.T) {
700 // Check whether an error is emitted when a VNDK lib depends on a system lib.
701 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
702 cc_library {
703 name: "libvndk",
704 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900705 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800706 vndk: {
707 enabled: true,
708 },
709 shared_libs: ["libfwk"], // Cause error
710 nocrt: true,
711 }
712
713 cc_library {
714 name: "libfwk",
715 nocrt: true,
716 }
717 `)
718
719 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
720 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
721 cc_library {
722 name: "libvndk",
723 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900724 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800725 vndk: {
726 enabled: true,
727 },
728 shared_libs: ["libvendor"], // Cause error
729 nocrt: true,
730 }
731
732 cc_library {
733 name: "libvendor",
734 vendor: true,
735 nocrt: true,
736 }
737 `)
738
739 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
740 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
741 cc_library {
742 name: "libvndk_sp",
743 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900744 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800745 vndk: {
746 enabled: true,
747 support_system_process: true,
748 },
749 shared_libs: ["libfwk"], // Cause error
750 nocrt: true,
751 }
752
753 cc_library {
754 name: "libfwk",
755 nocrt: true,
756 }
757 `)
758
759 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
760 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
761 cc_library {
762 name: "libvndk_sp",
763 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900764 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800765 vndk: {
766 enabled: true,
767 support_system_process: true,
768 },
769 shared_libs: ["libvendor"], // Cause error
770 nocrt: true,
771 }
772
773 cc_library {
774 name: "libvendor",
775 vendor: true,
776 nocrt: true,
777 }
778 `)
779
780 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
781 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
782 cc_library {
783 name: "libvndk_sp",
784 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900785 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800786 vndk: {
787 enabled: true,
788 support_system_process: true,
789 },
790 shared_libs: ["libvndk"], // Cause error
791 nocrt: true,
792 }
793
794 cc_library {
795 name: "libvndk",
796 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900797 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800798 vndk: {
799 enabled: true,
800 },
801 nocrt: true,
802 }
803 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900804
805 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
806 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
807 cc_library {
808 name: "libvndk",
809 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900810 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900811 vndk: {
812 enabled: true,
813 },
814 shared_libs: ["libnonvndk"],
815 nocrt: true,
816 }
817
818 cc_library {
819 name: "libnonvndk",
820 vendor_available: true,
821 nocrt: true,
822 }
823 `)
824
825 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
826 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
827 cc_library {
828 name: "libvndkprivate",
829 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +0900830 product_available: false,
Jooyung Hana70f0672019-01-18 15:20:43 +0900831 vndk: {
832 enabled: true,
833 },
834 shared_libs: ["libnonvndk"],
835 nocrt: true,
836 }
837
838 cc_library {
839 name: "libnonvndk",
840 vendor_available: true,
841 nocrt: true,
842 }
843 `)
844
845 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
846 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
847 cc_library {
848 name: "libvndksp",
849 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900850 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900851 vndk: {
852 enabled: true,
853 support_system_process: true,
854 },
855 shared_libs: ["libnonvndk"],
856 nocrt: true,
857 }
858
859 cc_library {
860 name: "libnonvndk",
861 vendor_available: true,
862 nocrt: true,
863 }
864 `)
865
866 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
867 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
868 cc_library {
869 name: "libvndkspprivate",
870 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +0900871 product_available: false,
Jooyung Hana70f0672019-01-18 15:20:43 +0900872 vndk: {
873 enabled: true,
874 support_system_process: true,
875 },
876 shared_libs: ["libnonvndk"],
877 nocrt: true,
878 }
879
880 cc_library {
881 name: "libnonvndk",
882 vendor_available: true,
883 nocrt: true,
884 }
885 `)
886}
887
888func TestDoubleLoadbleDep(t *testing.T) {
889 // okay to link : LLNDK -> double_loadable VNDK
890 testCc(t, `
891 cc_library {
892 name: "libllndk",
893 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -0700894 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900895 }
896
897 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -0700898 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900899 symbol_file: "",
900 }
901
902 cc_library {
903 name: "libdoubleloadable",
904 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900905 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900906 vndk: {
907 enabled: true,
908 },
909 double_loadable: true,
910 }
911 `)
912 // okay to link : LLNDK -> VNDK-SP
913 testCc(t, `
914 cc_library {
915 name: "libllndk",
916 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -0700917 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900918 }
919
920 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -0700921 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900922 symbol_file: "",
923 }
924
925 cc_library {
926 name: "libvndksp",
927 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900928 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900929 vndk: {
930 enabled: true,
931 support_system_process: true,
932 },
933 }
934 `)
935 // okay to link : double_loadable -> double_loadable
936 testCc(t, `
937 cc_library {
938 name: "libdoubleloadable1",
939 shared_libs: ["libdoubleloadable2"],
940 vendor_available: true,
941 double_loadable: true,
942 }
943
944 cc_library {
945 name: "libdoubleloadable2",
946 vendor_available: true,
947 double_loadable: true,
948 }
949 `)
950 // okay to link : double_loadable VNDK -> double_loadable VNDK private
951 testCc(t, `
952 cc_library {
953 name: "libdoubleloadable",
954 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900955 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900956 vndk: {
957 enabled: true,
958 },
959 double_loadable: true,
960 shared_libs: ["libnondoubleloadable"],
961 }
962
963 cc_library {
964 name: "libnondoubleloadable",
965 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +0900966 product_available: false,
Jooyung Hana70f0672019-01-18 15:20:43 +0900967 vndk: {
968 enabled: true,
969 },
970 double_loadable: true,
971 }
972 `)
973 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
974 testCc(t, `
975 cc_library {
976 name: "libllndk",
977 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -0700978 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900979 }
980
981 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -0700982 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900983 symbol_file: "",
984 }
985
986 cc_library {
987 name: "libcoreonly",
988 shared_libs: ["libvendoravailable"],
989 }
990
991 // indirect dependency of LLNDK
992 cc_library {
993 name: "libvendoravailable",
994 vendor_available: true,
995 double_loadable: true,
996 }
997 `)
998}
999
Inseob Kim5f58ff72020-09-07 19:53:31 +09001000func TestVendorSnapshotCapture(t *testing.T) {
Inseob Kim8471cda2019-11-15 09:59:12 +09001001 bp := `
1002 cc_library {
1003 name: "libvndk",
1004 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001005 product_available: true,
Inseob Kim8471cda2019-11-15 09:59:12 +09001006 vndk: {
1007 enabled: true,
1008 },
1009 nocrt: true,
1010 }
1011
1012 cc_library {
1013 name: "libvendor",
1014 vendor: true,
1015 nocrt: true,
1016 }
1017
1018 cc_library {
1019 name: "libvendor_available",
1020 vendor_available: true,
1021 nocrt: true,
1022 }
1023
1024 cc_library_headers {
1025 name: "libvendor_headers",
1026 vendor_available: true,
1027 nocrt: true,
1028 }
1029
1030 cc_binary {
1031 name: "vendor_bin",
1032 vendor: true,
1033 nocrt: true,
1034 }
1035
1036 cc_binary {
1037 name: "vendor_available_bin",
1038 vendor_available: true,
1039 nocrt: true,
1040 }
Inseob Kim7f283f42020-06-01 21:53:49 +09001041
1042 toolchain_library {
1043 name: "libb",
1044 vendor_available: true,
1045 src: "libb.a",
1046 }
Inseob Kim1042d292020-06-01 23:23:05 +09001047
1048 cc_object {
1049 name: "obj",
1050 vendor_available: true,
1051 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001052`
1053 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1054 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1055 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1056 ctx := testCcWithConfig(t, config)
1057
1058 // Check Vendor snapshot output.
1059
1060 snapshotDir := "vendor-snapshot"
1061 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +09001062 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1063
1064 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +09001065
1066 for _, arch := range [][]string{
1067 []string{"arm64", "armv8-a"},
1068 []string{"arm", "armv7-a-neon"},
1069 } {
1070 archType := arch[0]
1071 archVariant := arch[1]
1072 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1073
1074 // For shared libraries, only non-VNDK vendor_available modules are captured
1075 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1076 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +09001077 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1078 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
1079 jsonFiles = append(jsonFiles,
1080 filepath.Join(sharedDir, "libvendor.so.json"),
1081 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001082
1083 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
Inseob Kimc42f2f22020-07-29 20:32:10 +09001084 // Also cfi variants are captured, except for prebuilts like toolchain_library
Inseob Kim8471cda2019-11-15 09:59:12 +09001085 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001086 staticCfiVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static_cfi", archType, archVariant)
Inseob Kim8471cda2019-11-15 09:59:12 +09001087 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +09001088 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1089 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001090 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001091 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001092 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001093 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001094 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001095 jsonFiles = append(jsonFiles,
1096 filepath.Join(staticDir, "libb.a.json"),
1097 filepath.Join(staticDir, "libvndk.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001098 filepath.Join(staticDir, "libvndk.cfi.a.json"),
Inseob Kim7f283f42020-06-01 21:53:49 +09001099 filepath.Join(staticDir, "libvendor.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001100 filepath.Join(staticDir, "libvendor.cfi.a.json"),
1101 filepath.Join(staticDir, "libvendor_available.a.json"),
1102 filepath.Join(staticDir, "libvendor_available.cfi.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001103
Inseob Kim7f283f42020-06-01 21:53:49 +09001104 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +09001105 if archType == "arm64" {
1106 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1107 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +09001108 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
1109 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
1110 jsonFiles = append(jsonFiles,
1111 filepath.Join(binaryDir, "vendor_bin.json"),
1112 filepath.Join(binaryDir, "vendor_available_bin.json"))
1113 }
1114
1115 // For header libraries, all vendor:true and vendor_available modules are captured.
1116 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1117 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +09001118
1119 // For object modules, all vendor:true and vendor_available modules are captured.
1120 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1121 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1122 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1123 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001124 }
1125
1126 for _, jsonFile := range jsonFiles {
1127 // verify all json files exist
1128 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1129 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001130 }
1131 }
1132}
1133
Inseob Kim5f58ff72020-09-07 19:53:31 +09001134func TestVendorSnapshotUse(t *testing.T) {
1135 frameworkBp := `
1136 cc_library {
1137 name: "libvndk",
1138 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001139 product_available: true,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001140 vndk: {
1141 enabled: true,
1142 },
1143 nocrt: true,
1144 compile_multilib: "64",
1145 }
1146
1147 cc_library {
1148 name: "libvendor",
1149 vendor: true,
1150 nocrt: true,
1151 no_libcrt: true,
1152 stl: "none",
1153 system_shared_libs: [],
1154 compile_multilib: "64",
1155 }
1156
1157 cc_binary {
1158 name: "bin",
1159 vendor: true,
1160 nocrt: true,
1161 no_libcrt: true,
1162 stl: "none",
1163 system_shared_libs: [],
1164 compile_multilib: "64",
1165 }
1166`
1167
1168 vndkBp := `
1169 vndk_prebuilt_shared {
1170 name: "libvndk",
1171 version: "BOARD",
1172 target_arch: "arm64",
1173 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001174 product_available: true,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001175 vndk: {
1176 enabled: true,
1177 },
1178 arch: {
1179 arm64: {
1180 srcs: ["libvndk.so"],
Inseob Kim67be7322020-10-19 10:15:28 +09001181 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001182 },
1183 },
1184 }
1185`
1186
1187 vendorProprietaryBp := `
1188 cc_library {
1189 name: "libvendor_without_snapshot",
1190 vendor: true,
1191 nocrt: true,
1192 no_libcrt: true,
1193 stl: "none",
1194 system_shared_libs: [],
1195 compile_multilib: "64",
1196 }
1197
1198 cc_library_shared {
1199 name: "libclient",
1200 vendor: true,
1201 nocrt: true,
1202 no_libcrt: true,
1203 stl: "none",
1204 system_shared_libs: [],
1205 shared_libs: ["libvndk"],
1206 static_libs: ["libvendor", "libvendor_without_snapshot"],
1207 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001208 srcs: ["client.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001209 }
1210
1211 cc_binary {
1212 name: "bin_without_snapshot",
1213 vendor: true,
1214 nocrt: true,
1215 no_libcrt: true,
1216 stl: "none",
1217 system_shared_libs: [],
1218 static_libs: ["libvndk"],
1219 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001220 srcs: ["bin.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001221 }
1222
1223 vendor_snapshot_static {
1224 name: "libvndk",
1225 version: "BOARD",
1226 target_arch: "arm64",
1227 vendor: true,
1228 arch: {
1229 arm64: {
1230 src: "libvndk.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001231 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001232 },
1233 },
1234 }
1235
1236 vendor_snapshot_shared {
1237 name: "libvendor",
1238 version: "BOARD",
1239 target_arch: "arm64",
1240 vendor: true,
1241 arch: {
1242 arm64: {
1243 src: "libvendor.so",
Inseob Kim67be7322020-10-19 10:15:28 +09001244 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001245 },
1246 },
1247 }
1248
1249 vendor_snapshot_static {
1250 name: "libvendor",
1251 version: "BOARD",
1252 target_arch: "arm64",
1253 vendor: true,
1254 arch: {
1255 arm64: {
1256 src: "libvendor.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001257 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001258 },
1259 },
1260 }
1261
1262 vendor_snapshot_binary {
1263 name: "bin",
1264 version: "BOARD",
1265 target_arch: "arm64",
1266 vendor: true,
1267 arch: {
1268 arm64: {
1269 src: "bin",
1270 },
1271 },
1272 }
1273`
1274 depsBp := GatherRequiredDepsForTest(android.Android)
1275
1276 mockFS := map[string][]byte{
Inseob Kim67be7322020-10-19 10:15:28 +09001277 "deps/Android.bp": []byte(depsBp),
1278 "framework/Android.bp": []byte(frameworkBp),
1279 "vendor/Android.bp": []byte(vendorProprietaryBp),
1280 "vendor/bin": nil,
1281 "vendor/bin.cpp": nil,
1282 "vendor/client.cpp": nil,
1283 "vendor/include/libvndk/a.h": nil,
1284 "vendor/include/libvendor/b.h": nil,
1285 "vendor/libvndk.a": nil,
1286 "vendor/libvendor.a": nil,
1287 "vendor/libvendor.so": nil,
1288 "vndk/Android.bp": []byte(vndkBp),
1289 "vndk/include/libvndk/a.h": nil,
1290 "vndk/libvndk.so": nil,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001291 }
1292
1293 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1294 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1295 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001296 ctx := CreateTestContext(config)
1297 ctx.Register()
Inseob Kim5f58ff72020-09-07 19:53:31 +09001298
1299 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "vendor/Android.bp", "vndk/Android.bp"})
1300 android.FailIfErrored(t, errs)
1301 _, errs = ctx.PrepareBuildActions(config)
1302 android.FailIfErrored(t, errs)
1303
1304 sharedVariant := "android_vendor.BOARD_arm64_armv8-a_shared"
1305 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1306 binaryVariant := "android_vendor.BOARD_arm64_armv8-a"
1307
1308 // libclient uses libvndk.vndk.BOARD.arm64, libvendor.vendor_static.BOARD.arm64, libvendor_without_snapshot
Inseob Kim67be7322020-10-19 10:15:28 +09001309 libclientCcFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("cc").Args["cFlags"]
1310 for _, includeFlags := range []string{
1311 "-Ivndk/include/libvndk", // libvndk
1312 "-Ivendor/include/libvendor", // libvendor
1313 } {
1314 if !strings.Contains(libclientCcFlags, includeFlags) {
1315 t.Errorf("flags for libclient must contain %#v, but was %#v.",
1316 includeFlags, libclientCcFlags)
1317 }
1318 }
Inseob Kim5f58ff72020-09-07 19:53:31 +09001319
Inseob Kim67be7322020-10-19 10:15:28 +09001320 libclientLdFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001321 for _, input := range [][]string{
1322 []string{sharedVariant, "libvndk.vndk.BOARD.arm64"},
1323 []string{staticVariant, "libvendor.vendor_static.BOARD.arm64"},
1324 []string{staticVariant, "libvendor_without_snapshot"},
1325 } {
1326 outputPaths := getOutputPaths(ctx, input[0] /* variant */, []string{input[1]} /* module name */)
Inseob Kim67be7322020-10-19 10:15:28 +09001327 if !strings.Contains(libclientLdFlags, outputPaths[0].String()) {
1328 t.Errorf("libflags for libclient must contain %#v, but was %#v", outputPaths[0], libclientLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001329 }
1330 }
1331
1332 // bin_without_snapshot uses libvndk.vendor_static.BOARD.arm64
Inseob Kim67be7322020-10-19 10:15:28 +09001333 binWithoutSnapshotCcFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("cc").Args["cFlags"]
1334 if !strings.Contains(binWithoutSnapshotCcFlags, "-Ivendor/include/libvndk") {
1335 t.Errorf("flags for bin_without_snapshot must contain %#v, but was %#v.",
1336 "-Ivendor/include/libvndk", binWithoutSnapshotCcFlags)
1337 }
1338
1339 binWithoutSnapshotLdFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001340 libVndkStaticOutputPaths := getOutputPaths(ctx, staticVariant, []string{"libvndk.vendor_static.BOARD.arm64"})
Inseob Kim67be7322020-10-19 10:15:28 +09001341 if !strings.Contains(binWithoutSnapshotLdFlags, libVndkStaticOutputPaths[0].String()) {
Inseob Kim5f58ff72020-09-07 19:53:31 +09001342 t.Errorf("libflags for bin_without_snapshot must contain %#v, but was %#v",
Inseob Kim67be7322020-10-19 10:15:28 +09001343 libVndkStaticOutputPaths[0], binWithoutSnapshotLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001344 }
1345
1346 // libvendor.so is installed by libvendor.vendor_shared.BOARD.arm64
1347 ctx.ModuleForTests("libvendor.vendor_shared.BOARD.arm64", sharedVariant).Output("libvendor.so")
1348
1349 // libvendor_without_snapshot.so is installed by libvendor_without_snapshot
1350 ctx.ModuleForTests("libvendor_without_snapshot", sharedVariant).Output("libvendor_without_snapshot.so")
1351
1352 // bin is installed by bin.vendor_binary.BOARD.arm64
1353 ctx.ModuleForTests("bin.vendor_binary.BOARD.arm64", binaryVariant).Output("bin")
1354
1355 // bin_without_snapshot is installed by bin_without_snapshot
1356 ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Output("bin_without_snapshot")
1357
1358 // libvendor and bin don't have vendor.BOARD variant
1359 libvendorVariants := ctx.ModuleVariantsForTests("libvendor")
1360 if inList(sharedVariant, libvendorVariants) {
1361 t.Errorf("libvendor must not have variant %#v, but it does", sharedVariant)
1362 }
1363
1364 binVariants := ctx.ModuleVariantsForTests("bin")
1365 if inList(binaryVariant, binVariants) {
1366 t.Errorf("bin must not have variant %#v, but it does", sharedVariant)
1367 }
1368}
1369
Inseob Kimc42f2f22020-07-29 20:32:10 +09001370func TestVendorSnapshotSanitizer(t *testing.T) {
1371 bp := `
1372 vendor_snapshot_static {
1373 name: "libsnapshot",
1374 vendor: true,
1375 target_arch: "arm64",
1376 version: "BOARD",
1377 arch: {
1378 arm64: {
1379 src: "libsnapshot.a",
1380 cfi: {
1381 src: "libsnapshot.cfi.a",
1382 }
1383 },
1384 },
1385 }
1386`
1387 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1388 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1389 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1390 ctx := testCcWithConfig(t, config)
1391
1392 // Check non-cfi and cfi variant.
1393 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1394 staticCfiVariant := "android_vendor.BOARD_arm64_armv8-a_static_cfi"
1395
1396 staticModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticVariant).Module().(*Module)
1397 assertString(t, staticModule.outputFile.Path().Base(), "libsnapshot.a")
1398
1399 staticCfiModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticCfiVariant).Module().(*Module)
1400 assertString(t, staticCfiModule.outputFile.Path().Base(), "libsnapshot.cfi.a")
1401}
1402
Bill Peckham945441c2020-08-31 16:07:58 -07001403func assertExcludeFromVendorSnapshotIs(t *testing.T, c *Module, expected bool) {
1404 t.Helper()
1405 if c.ExcludeFromVendorSnapshot() != expected {
1406 t.Errorf("expected %q ExcludeFromVendorSnapshot to be %t", c.String(), expected)
1407 }
1408}
1409
1410func TestVendorSnapshotExclude(t *testing.T) {
1411
1412 // This test verifies that the exclude_from_vendor_snapshot property
1413 // makes its way from the Android.bp source file into the module data
1414 // structure. It also verifies that modules are correctly included or
1415 // excluded in the vendor snapshot based on their path (framework or
1416 // vendor) and the exclude_from_vendor_snapshot property.
1417
1418 frameworkBp := `
1419 cc_library_shared {
1420 name: "libinclude",
1421 srcs: ["src/include.cpp"],
1422 vendor_available: true,
1423 }
1424 cc_library_shared {
1425 name: "libexclude",
1426 srcs: ["src/exclude.cpp"],
1427 vendor: true,
1428 exclude_from_vendor_snapshot: true,
1429 }
1430 `
1431
1432 vendorProprietaryBp := `
1433 cc_library_shared {
1434 name: "libvendor",
1435 srcs: ["vendor.cpp"],
1436 vendor: true,
1437 }
1438 `
1439
1440 depsBp := GatherRequiredDepsForTest(android.Android)
1441
1442 mockFS := map[string][]byte{
1443 "deps/Android.bp": []byte(depsBp),
1444 "framework/Android.bp": []byte(frameworkBp),
1445 "framework/include.cpp": nil,
1446 "framework/exclude.cpp": nil,
1447 "device/Android.bp": []byte(vendorProprietaryBp),
1448 "device/vendor.cpp": nil,
1449 }
1450
1451 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1452 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1453 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001454 ctx := CreateTestContext(config)
1455 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001456
1457 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1458 android.FailIfErrored(t, errs)
1459 _, errs = ctx.PrepareBuildActions(config)
1460 android.FailIfErrored(t, errs)
1461
1462 // Test an include and exclude framework module.
1463 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1464 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", vendorVariant).Module().(*Module), false)
1465 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libexclude", vendorVariant).Module().(*Module), true)
1466
1467 // A vendor module is excluded, but by its path, not the
1468 // exclude_from_vendor_snapshot property.
1469 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libvendor", vendorVariant).Module().(*Module), false)
1470
1471 // Verify the content of the vendor snapshot.
1472
1473 snapshotDir := "vendor-snapshot"
1474 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1475 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1476
1477 var includeJsonFiles []string
1478 var excludeJsonFiles []string
1479
1480 for _, arch := range [][]string{
1481 []string{"arm64", "armv8-a"},
1482 []string{"arm", "armv7-a-neon"},
1483 } {
1484 archType := arch[0]
1485 archVariant := arch[1]
1486 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1487
1488 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1489 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1490
1491 // Included modules
1492 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1493 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1494
1495 // Excluded modules
1496 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1497 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1498 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1499 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1500 }
1501
1502 // Verify that each json file for an included module has a rule.
1503 for _, jsonFile := range includeJsonFiles {
1504 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1505 t.Errorf("include json file %q not found", jsonFile)
1506 }
1507 }
1508
1509 // Verify that each json file for an excluded module has no rule.
1510 for _, jsonFile := range excludeJsonFiles {
1511 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1512 t.Errorf("exclude json file %q found", jsonFile)
1513 }
1514 }
1515}
1516
1517func TestVendorSnapshotExcludeInVendorProprietaryPathErrors(t *testing.T) {
1518
1519 // This test verifies that using the exclude_from_vendor_snapshot
1520 // property on a module in a vendor proprietary path generates an
1521 // error. These modules are already excluded, so we prohibit using the
1522 // property in this way, which could add to confusion.
1523
1524 vendorProprietaryBp := `
1525 cc_library_shared {
1526 name: "libvendor",
1527 srcs: ["vendor.cpp"],
1528 vendor: true,
1529 exclude_from_vendor_snapshot: true,
1530 }
1531 `
1532
1533 depsBp := GatherRequiredDepsForTest(android.Android)
1534
1535 mockFS := map[string][]byte{
1536 "deps/Android.bp": []byte(depsBp),
1537 "device/Android.bp": []byte(vendorProprietaryBp),
1538 "device/vendor.cpp": nil,
1539 }
1540
1541 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1542 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1543 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001544 ctx := CreateTestContext(config)
1545 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001546
1547 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "device/Android.bp"})
1548 android.FailIfErrored(t, errs)
1549
1550 _, errs = ctx.PrepareBuildActions(config)
1551 android.CheckErrorsAgainstExpectations(t, errs, []string{
1552 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1553 `module "libvendor\{.+,image:vendor.+,arch:arm_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1554 })
1555}
1556
1557func TestVendorSnapshotExcludeWithVendorAvailable(t *testing.T) {
1558
1559 // This test verifies that using the exclude_from_vendor_snapshot
1560 // property on a module that is vendor available generates an error. A
1561 // vendor available module must be captured in the vendor snapshot and
1562 // must not built from source when building the vendor image against
1563 // the vendor snapshot.
1564
1565 frameworkBp := `
1566 cc_library_shared {
1567 name: "libinclude",
1568 srcs: ["src/include.cpp"],
1569 vendor_available: true,
1570 exclude_from_vendor_snapshot: true,
1571 }
1572 `
1573
1574 depsBp := GatherRequiredDepsForTest(android.Android)
1575
1576 mockFS := map[string][]byte{
1577 "deps/Android.bp": []byte(depsBp),
1578 "framework/Android.bp": []byte(frameworkBp),
1579 "framework/include.cpp": nil,
1580 }
1581
1582 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1583 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1584 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001585 ctx := CreateTestContext(config)
1586 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001587
1588 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp"})
1589 android.FailIfErrored(t, errs)
1590
1591 _, errs = ctx.PrepareBuildActions(config)
1592 android.CheckErrorsAgainstExpectations(t, errs, []string{
1593 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1594 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1595 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1596 `module "libinclude\{.+,image:vendor.+,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1597 })
1598}
1599
Jooyung Hana70f0672019-01-18 15:20:43 +09001600func TestDoubleLoadableDepError(t *testing.T) {
1601 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1602 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1603 cc_library {
1604 name: "libllndk",
1605 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001606 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001607 }
1608
1609 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001610 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001611 symbol_file: "",
1612 }
1613
1614 cc_library {
1615 name: "libnondoubleloadable",
1616 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001617 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001618 vndk: {
1619 enabled: true,
1620 },
1621 }
1622 `)
1623
1624 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1625 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1626 cc_library {
1627 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001628 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001629 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001630 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001631 }
1632
1633 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001634 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001635 symbol_file: "",
1636 }
1637
1638 cc_library {
1639 name: "libnondoubleloadable",
1640 vendor_available: true,
1641 }
1642 `)
1643
1644 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
1645 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1646 cc_library {
1647 name: "libdoubleloadable",
1648 vendor_available: true,
1649 double_loadable: true,
1650 shared_libs: ["libnondoubleloadable"],
1651 }
1652
1653 cc_library {
1654 name: "libnondoubleloadable",
1655 vendor_available: true,
1656 }
1657 `)
1658
1659 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
1660 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1661 cc_library {
1662 name: "libdoubleloadable",
1663 vendor_available: true,
1664 double_loadable: true,
1665 shared_libs: ["libnondoubleloadable"],
1666 }
1667
1668 cc_library {
1669 name: "libnondoubleloadable",
1670 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001671 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001672 vndk: {
1673 enabled: true,
1674 },
1675 }
1676 `)
1677
1678 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
1679 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1680 cc_library {
1681 name: "libdoubleloadable",
1682 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001683 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001684 vndk: {
1685 enabled: true,
1686 },
1687 double_loadable: true,
1688 shared_libs: ["libnondoubleloadable"],
1689 }
1690
1691 cc_library {
1692 name: "libnondoubleloadable",
1693 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09001694 product_available: false,
Jooyung Hana70f0672019-01-18 15:20:43 +09001695 vndk: {
1696 enabled: true,
1697 },
1698 }
1699 `)
1700
1701 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1702 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1703 cc_library {
1704 name: "libllndk",
1705 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001706 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001707 }
1708
1709 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001710 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001711 symbol_file: "",
1712 }
1713
1714 cc_library {
1715 name: "libcoreonly",
1716 shared_libs: ["libvendoravailable"],
1717 }
1718
1719 // indirect dependency of LLNDK
1720 cc_library {
1721 name: "libvendoravailable",
1722 vendor_available: true,
1723 }
1724 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001725}
1726
Jooyung Han479ca172020-10-19 18:51:07 +09001727func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1728 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1729 cc_library {
1730 name: "libvndksp",
1731 shared_libs: ["libanothervndksp"],
1732 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001733 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001734 vndk: {
1735 enabled: true,
1736 support_system_process: true,
1737 }
1738 }
1739
1740 cc_library {
1741 name: "libllndk",
1742 shared_libs: ["libanothervndksp"],
1743 }
1744
1745 llndk_library {
1746 name: "libllndk",
1747 symbol_file: "",
1748 }
1749
1750 cc_library {
1751 name: "libanothervndksp",
1752 vendor_available: true,
1753 }
1754 `)
1755}
1756
Logan Chienf3511742017-10-31 18:04:35 +08001757func TestVndkExt(t *testing.T) {
1758 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001759 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001760 cc_library {
1761 name: "libvndk",
1762 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001763 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001764 vndk: {
1765 enabled: true,
1766 },
1767 nocrt: true,
1768 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001769 cc_library {
1770 name: "libvndk2",
1771 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001772 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001773 vndk: {
1774 enabled: true,
1775 },
1776 target: {
1777 vendor: {
1778 suffix: "-suffix",
1779 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001780 product: {
1781 suffix: "-suffix",
1782 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001783 },
1784 nocrt: true,
1785 }
Logan Chienf3511742017-10-31 18:04:35 +08001786
1787 cc_library {
1788 name: "libvndk_ext",
1789 vendor: true,
1790 vndk: {
1791 enabled: true,
1792 extends: "libvndk",
1793 },
1794 nocrt: true,
1795 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001796
1797 cc_library {
1798 name: "libvndk2_ext",
1799 vendor: true,
1800 vndk: {
1801 enabled: true,
1802 extends: "libvndk2",
1803 },
1804 nocrt: true,
1805 }
Logan Chienf3511742017-10-31 18:04:35 +08001806
Justin Yun0ecf0b22020-02-28 15:07:59 +09001807 cc_library {
1808 name: "libvndk_ext_product",
1809 product_specific: true,
1810 vndk: {
1811 enabled: true,
1812 extends: "libvndk",
1813 },
1814 nocrt: true,
1815 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001816
Justin Yun0ecf0b22020-02-28 15:07:59 +09001817 cc_library {
1818 name: "libvndk2_ext_product",
1819 product_specific: true,
1820 vndk: {
1821 enabled: true,
1822 extends: "libvndk2",
1823 },
1824 nocrt: true,
1825 }
1826 `
1827 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1828 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1829 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1830 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1831
1832 ctx := testCcWithConfig(t, config)
1833
1834 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1835 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1836
1837 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1838 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1839
1840 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1841 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001842}
1843
Logan Chiend3c59a22018-03-29 14:08:15 +08001844func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001845 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1846 ctx := testCcNoVndk(t, `
1847 cc_library {
1848 name: "libvndk",
1849 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001850 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001851 vndk: {
1852 enabled: true,
1853 },
1854 nocrt: true,
1855 }
1856
1857 cc_library {
1858 name: "libvndk_ext",
1859 vendor: true,
1860 vndk: {
1861 enabled: true,
1862 extends: "libvndk",
1863 },
1864 nocrt: true,
1865 }
1866 `)
1867
1868 // Ensures that the core variant of "libvndk_ext" can be found.
1869 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1870 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1871 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1872 }
1873}
1874
Justin Yun0ecf0b22020-02-28 15:07:59 +09001875func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1876 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
1877 ctx := testCc(t, `
1878 cc_library {
1879 name: "libvndk",
1880 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001881 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001882 vndk: {
1883 enabled: true,
1884 },
1885 nocrt: true,
1886 }
1887
1888 cc_library {
1889 name: "libvndk_ext_product",
1890 product_specific: true,
1891 vndk: {
1892 enabled: true,
1893 extends: "libvndk",
1894 },
1895 nocrt: true,
1896 }
1897 `)
1898
1899 // Ensures that the core variant of "libvndk_ext_product" can be found.
1900 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1901 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1902 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1903 }
1904}
1905
Logan Chienf3511742017-10-31 18:04:35 +08001906func TestVndkExtError(t *testing.T) {
1907 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001908 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001909 cc_library {
1910 name: "libvndk",
1911 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001912 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001913 vndk: {
1914 enabled: true,
1915 },
1916 nocrt: true,
1917 }
1918
1919 cc_library {
1920 name: "libvndk_ext",
1921 vndk: {
1922 enabled: true,
1923 extends: "libvndk",
1924 },
1925 nocrt: true,
1926 }
1927 `)
1928
1929 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1930 cc_library {
1931 name: "libvndk",
1932 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001933 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001934 vndk: {
1935 enabled: true,
1936 },
1937 nocrt: true,
1938 }
1939
1940 cc_library {
1941 name: "libvndk_ext",
1942 vendor: true,
1943 vndk: {
1944 enabled: true,
1945 },
1946 nocrt: true,
1947 }
1948 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001949
1950 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1951 cc_library {
1952 name: "libvndk",
1953 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001954 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001955 vndk: {
1956 enabled: true,
1957 },
1958 nocrt: true,
1959 }
1960
1961 cc_library {
1962 name: "libvndk_ext_product",
1963 product_specific: true,
1964 vndk: {
1965 enabled: true,
1966 },
1967 nocrt: true,
1968 }
1969 `)
1970
1971 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1972 cc_library {
1973 name: "libvndk",
1974 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001975 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001976 vndk: {
1977 enabled: true,
1978 },
1979 nocrt: true,
1980 }
1981
1982 cc_library {
1983 name: "libvndk_ext_product",
1984 product_specific: true,
1985 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001986 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001987 vndk: {
1988 enabled: true,
1989 extends: "libvndk",
1990 },
1991 nocrt: true,
1992 }
1993 `)
Logan Chienf3511742017-10-31 18:04:35 +08001994}
1995
1996func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1997 // This test ensures an error is emitted for inconsistent support_system_process.
1998 testCcError(t, "module \".*\" with mismatched support_system_process", `
1999 cc_library {
2000 name: "libvndk",
2001 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002002 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002003 vndk: {
2004 enabled: true,
2005 },
2006 nocrt: true,
2007 }
2008
2009 cc_library {
2010 name: "libvndk_sp_ext",
2011 vendor: true,
2012 vndk: {
2013 enabled: true,
2014 extends: "libvndk",
2015 support_system_process: true,
2016 },
2017 nocrt: true,
2018 }
2019 `)
2020
2021 testCcError(t, "module \".*\" with mismatched support_system_process", `
2022 cc_library {
2023 name: "libvndk_sp",
2024 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002025 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002026 vndk: {
2027 enabled: true,
2028 support_system_process: true,
2029 },
2030 nocrt: true,
2031 }
2032
2033 cc_library {
2034 name: "libvndk_ext",
2035 vendor: true,
2036 vndk: {
2037 enabled: true,
2038 extends: "libvndk_sp",
2039 },
2040 nocrt: true,
2041 }
2042 `)
2043}
2044
2045func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08002046 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08002047 // with `vendor_available: false`.
2048 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
2049 cc_library {
2050 name: "libvndk",
2051 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09002052 product_available: false,
Logan Chienf3511742017-10-31 18:04:35 +08002053 vndk: {
2054 enabled: true,
2055 },
2056 nocrt: true,
2057 }
2058
2059 cc_library {
2060 name: "libvndk_ext",
2061 vendor: true,
2062 vndk: {
2063 enabled: true,
2064 extends: "libvndk",
2065 },
2066 nocrt: true,
2067 }
2068 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002069
2070 testCcErrorProductVndk(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
2071 cc_library {
2072 name: "libvndk",
2073 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09002074 product_available: false,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002075 vndk: {
2076 enabled: true,
2077 },
2078 nocrt: true,
2079 }
2080
2081 cc_library {
2082 name: "libvndk_ext_product",
2083 product_specific: true,
2084 vndk: {
2085 enabled: true,
2086 extends: "libvndk",
2087 },
2088 nocrt: true,
2089 }
2090 `)
Logan Chienf3511742017-10-31 18:04:35 +08002091}
2092
Logan Chiend3c59a22018-03-29 14:08:15 +08002093func TestVendorModuleUseVndkExt(t *testing.T) {
2094 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002095 testCc(t, `
2096 cc_library {
2097 name: "libvndk",
2098 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002099 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002100 vndk: {
2101 enabled: true,
2102 },
2103 nocrt: true,
2104 }
2105
2106 cc_library {
2107 name: "libvndk_ext",
2108 vendor: true,
2109 vndk: {
2110 enabled: true,
2111 extends: "libvndk",
2112 },
2113 nocrt: true,
2114 }
2115
2116 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08002117 name: "libvndk_sp",
2118 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002119 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002120 vndk: {
2121 enabled: true,
2122 support_system_process: true,
2123 },
2124 nocrt: true,
2125 }
2126
2127 cc_library {
2128 name: "libvndk_sp_ext",
2129 vendor: true,
2130 vndk: {
2131 enabled: true,
2132 extends: "libvndk_sp",
2133 support_system_process: true,
2134 },
2135 nocrt: true,
2136 }
2137
2138 cc_library {
2139 name: "libvendor",
2140 vendor: true,
2141 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
2142 nocrt: true,
2143 }
2144 `)
2145}
2146
Logan Chiend3c59a22018-03-29 14:08:15 +08002147func TestVndkExtUseVendorLib(t *testing.T) {
2148 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08002149 testCc(t, `
2150 cc_library {
2151 name: "libvndk",
2152 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002153 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002154 vndk: {
2155 enabled: true,
2156 },
2157 nocrt: true,
2158 }
2159
2160 cc_library {
2161 name: "libvndk_ext",
2162 vendor: true,
2163 vndk: {
2164 enabled: true,
2165 extends: "libvndk",
2166 },
2167 shared_libs: ["libvendor"],
2168 nocrt: true,
2169 }
2170
2171 cc_library {
2172 name: "libvendor",
2173 vendor: true,
2174 nocrt: true,
2175 }
2176 `)
Logan Chienf3511742017-10-31 18:04:35 +08002177
Logan Chiend3c59a22018-03-29 14:08:15 +08002178 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
2179 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08002180 cc_library {
2181 name: "libvndk_sp",
2182 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002183 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002184 vndk: {
2185 enabled: true,
2186 support_system_process: true,
2187 },
2188 nocrt: true,
2189 }
2190
2191 cc_library {
2192 name: "libvndk_sp_ext",
2193 vendor: true,
2194 vndk: {
2195 enabled: true,
2196 extends: "libvndk_sp",
2197 support_system_process: true,
2198 },
2199 shared_libs: ["libvendor"], // Cause an error
2200 nocrt: true,
2201 }
2202
2203 cc_library {
2204 name: "libvendor",
2205 vendor: true,
2206 nocrt: true,
2207 }
2208 `)
2209}
2210
Justin Yun0ecf0b22020-02-28 15:07:59 +09002211func TestProductVndkExtDependency(t *testing.T) {
2212 bp := `
2213 cc_library {
2214 name: "libvndk",
2215 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002216 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002217 vndk: {
2218 enabled: true,
2219 },
2220 nocrt: true,
2221 }
2222
2223 cc_library {
2224 name: "libvndk_ext_product",
2225 product_specific: true,
2226 vndk: {
2227 enabled: true,
2228 extends: "libvndk",
2229 },
2230 shared_libs: ["libproduct_for_vndklibs"],
2231 nocrt: true,
2232 }
2233
2234 cc_library {
2235 name: "libvndk_sp",
2236 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002237 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002238 vndk: {
2239 enabled: true,
2240 support_system_process: true,
2241 },
2242 nocrt: true,
2243 }
2244
2245 cc_library {
2246 name: "libvndk_sp_ext_product",
2247 product_specific: true,
2248 vndk: {
2249 enabled: true,
2250 extends: "libvndk_sp",
2251 support_system_process: true,
2252 },
2253 shared_libs: ["libproduct_for_vndklibs"],
2254 nocrt: true,
2255 }
2256
2257 cc_library {
2258 name: "libproduct",
2259 product_specific: true,
2260 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
2261 nocrt: true,
2262 }
2263
2264 cc_library {
2265 name: "libproduct_for_vndklibs",
2266 product_specific: true,
2267 nocrt: true,
2268 }
2269 `
2270 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2271 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2272 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2273 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2274
2275 testCcWithConfig(t, config)
2276}
2277
Logan Chiend3c59a22018-03-29 14:08:15 +08002278func TestVndkSpExtUseVndkError(t *testing.T) {
2279 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
2280 // library.
2281 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2282 cc_library {
2283 name: "libvndk",
2284 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002285 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002286 vndk: {
2287 enabled: true,
2288 },
2289 nocrt: true,
2290 }
2291
2292 cc_library {
2293 name: "libvndk_sp",
2294 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002295 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002296 vndk: {
2297 enabled: true,
2298 support_system_process: true,
2299 },
2300 nocrt: true,
2301 }
2302
2303 cc_library {
2304 name: "libvndk_sp_ext",
2305 vendor: true,
2306 vndk: {
2307 enabled: true,
2308 extends: "libvndk_sp",
2309 support_system_process: true,
2310 },
2311 shared_libs: ["libvndk"], // Cause an error
2312 nocrt: true,
2313 }
2314 `)
2315
2316 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
2317 // library.
2318 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2319 cc_library {
2320 name: "libvndk",
2321 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002322 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002323 vndk: {
2324 enabled: true,
2325 },
2326 nocrt: true,
2327 }
2328
2329 cc_library {
2330 name: "libvndk_ext",
2331 vendor: true,
2332 vndk: {
2333 enabled: true,
2334 extends: "libvndk",
2335 },
2336 nocrt: true,
2337 }
2338
2339 cc_library {
2340 name: "libvndk_sp",
2341 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002342 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002343 vndk: {
2344 enabled: true,
2345 support_system_process: true,
2346 },
2347 nocrt: true,
2348 }
2349
2350 cc_library {
2351 name: "libvndk_sp_ext",
2352 vendor: true,
2353 vndk: {
2354 enabled: true,
2355 extends: "libvndk_sp",
2356 support_system_process: true,
2357 },
2358 shared_libs: ["libvndk_ext"], // Cause an error
2359 nocrt: true,
2360 }
2361 `)
2362}
2363
2364func TestVndkUseVndkExtError(t *testing.T) {
2365 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2366 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002367 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2368 cc_library {
2369 name: "libvndk",
2370 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002371 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002372 vndk: {
2373 enabled: true,
2374 },
2375 nocrt: true,
2376 }
2377
2378 cc_library {
2379 name: "libvndk_ext",
2380 vendor: true,
2381 vndk: {
2382 enabled: true,
2383 extends: "libvndk",
2384 },
2385 nocrt: true,
2386 }
2387
2388 cc_library {
2389 name: "libvndk2",
2390 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002391 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002392 vndk: {
2393 enabled: true,
2394 },
2395 shared_libs: ["libvndk_ext"],
2396 nocrt: true,
2397 }
2398 `)
2399
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002400 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002401 cc_library {
2402 name: "libvndk",
2403 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002404 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002405 vndk: {
2406 enabled: true,
2407 },
2408 nocrt: true,
2409 }
2410
2411 cc_library {
2412 name: "libvndk_ext",
2413 vendor: true,
2414 vndk: {
2415 enabled: true,
2416 extends: "libvndk",
2417 },
2418 nocrt: true,
2419 }
2420
2421 cc_library {
2422 name: "libvndk2",
2423 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002424 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002425 vndk: {
2426 enabled: true,
2427 },
2428 target: {
2429 vendor: {
2430 shared_libs: ["libvndk_ext"],
2431 },
2432 },
2433 nocrt: true,
2434 }
2435 `)
2436
2437 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2438 cc_library {
2439 name: "libvndk_sp",
2440 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002441 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002442 vndk: {
2443 enabled: true,
2444 support_system_process: true,
2445 },
2446 nocrt: true,
2447 }
2448
2449 cc_library {
2450 name: "libvndk_sp_ext",
2451 vendor: true,
2452 vndk: {
2453 enabled: true,
2454 extends: "libvndk_sp",
2455 support_system_process: true,
2456 },
2457 nocrt: true,
2458 }
2459
2460 cc_library {
2461 name: "libvndk_sp_2",
2462 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002463 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002464 vndk: {
2465 enabled: true,
2466 support_system_process: true,
2467 },
2468 shared_libs: ["libvndk_sp_ext"],
2469 nocrt: true,
2470 }
2471 `)
2472
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002473 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002474 cc_library {
2475 name: "libvndk_sp",
2476 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002477 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002478 vndk: {
2479 enabled: true,
2480 },
2481 nocrt: true,
2482 }
2483
2484 cc_library {
2485 name: "libvndk_sp_ext",
2486 vendor: true,
2487 vndk: {
2488 enabled: true,
2489 extends: "libvndk_sp",
2490 },
2491 nocrt: true,
2492 }
2493
2494 cc_library {
2495 name: "libvndk_sp2",
2496 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002497 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002498 vndk: {
2499 enabled: true,
2500 },
2501 target: {
2502 vendor: {
2503 shared_libs: ["libvndk_sp_ext"],
2504 },
2505 },
2506 nocrt: true,
2507 }
2508 `)
2509}
2510
Justin Yun5f7f7e82019-11-18 19:52:14 +09002511func TestEnforceProductVndkVersion(t *testing.T) {
2512 bp := `
2513 cc_library {
2514 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002515 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002516 }
2517 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002518 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002519 symbol_file: "",
2520 }
2521 cc_library {
2522 name: "libvndk",
2523 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002524 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002525 vndk: {
2526 enabled: true,
2527 },
2528 nocrt: true,
2529 }
2530 cc_library {
2531 name: "libvndk_sp",
2532 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002533 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002534 vndk: {
2535 enabled: true,
2536 support_system_process: true,
2537 },
2538 nocrt: true,
2539 }
2540 cc_library {
2541 name: "libva",
2542 vendor_available: true,
2543 nocrt: true,
2544 }
2545 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002546 name: "libpa",
2547 product_available: true,
2548 nocrt: true,
2549 }
2550 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002551 name: "libproduct_va",
2552 product_specific: true,
2553 vendor_available: true,
2554 nocrt: true,
2555 }
2556 cc_library {
2557 name: "libprod",
2558 product_specific: true,
2559 shared_libs: [
2560 "libllndk",
2561 "libvndk",
2562 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002563 "libpa",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002564 "libproduct_va",
2565 ],
2566 nocrt: true,
2567 }
2568 cc_library {
2569 name: "libvendor",
2570 vendor: true,
2571 shared_libs: [
2572 "libllndk",
2573 "libvndk",
2574 "libvndk_sp",
2575 "libva",
2576 "libproduct_va",
2577 ],
2578 nocrt: true,
2579 }
2580 `
2581
2582 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2583 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2584 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2585 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2586
2587 ctx := testCcWithConfig(t, config)
2588
Jooyung Han261e1582020-10-20 18:54:21 +09002589 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2590 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun5f7f7e82019-11-18 19:52:14 +09002591}
2592
2593func TestEnforceProductVndkVersionErrors(t *testing.T) {
2594 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2595 cc_library {
2596 name: "libprod",
2597 product_specific: true,
2598 shared_libs: [
2599 "libvendor",
2600 ],
2601 nocrt: true,
2602 }
2603 cc_library {
2604 name: "libvendor",
2605 vendor: true,
2606 nocrt: true,
2607 }
2608 `)
2609 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2610 cc_library {
2611 name: "libprod",
2612 product_specific: true,
2613 shared_libs: [
2614 "libsystem",
2615 ],
2616 nocrt: true,
2617 }
2618 cc_library {
2619 name: "libsystem",
2620 nocrt: true,
2621 }
2622 `)
2623 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
2624 cc_library {
2625 name: "libprod",
2626 product_specific: true,
2627 shared_libs: [
2628 "libvndk_private",
2629 ],
2630 nocrt: true,
2631 }
2632 cc_library {
2633 name: "libvndk_private",
2634 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09002635 product_available: false,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002636 vndk: {
2637 enabled: true,
2638 },
2639 nocrt: true,
2640 }
2641 `)
2642 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2643 cc_library {
2644 name: "libprod",
2645 product_specific: true,
2646 shared_libs: [
2647 "libsystem_ext",
2648 ],
2649 nocrt: true,
2650 }
2651 cc_library {
2652 name: "libsystem_ext",
2653 system_ext_specific: true,
2654 nocrt: true,
2655 }
2656 `)
2657 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2658 cc_library {
2659 name: "libsystem",
2660 shared_libs: [
2661 "libproduct_va",
2662 ],
2663 nocrt: true,
2664 }
2665 cc_library {
2666 name: "libproduct_va",
2667 product_specific: true,
2668 vendor_available: true,
2669 nocrt: true,
2670 }
2671 `)
2672}
2673
Jooyung Han38002912019-05-16 04:01:54 +09002674func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002675 bp := `
2676 cc_library {
2677 name: "libvndk",
2678 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002679 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002680 vndk: {
2681 enabled: true,
2682 },
2683 }
2684 cc_library {
2685 name: "libvndksp",
2686 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002687 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002688 vndk: {
2689 enabled: true,
2690 support_system_process: true,
2691 },
2692 }
2693 cc_library {
2694 name: "libvndkprivate",
2695 vendor_available: false,
Justin Yun63e9ec72020-10-29 16:49:43 +09002696 product_available: false,
Colin Cross98be1bb2019-12-13 20:41:13 -08002697 vndk: {
2698 enabled: true,
2699 },
2700 }
2701 cc_library {
2702 name: "libvendor",
2703 vendor: true,
2704 }
2705 cc_library {
2706 name: "libvndkext",
2707 vendor: true,
2708 vndk: {
2709 enabled: true,
2710 extends: "libvndk",
2711 },
2712 }
2713 vndk_prebuilt_shared {
2714 name: "prevndk",
2715 version: "27",
2716 target_arch: "arm",
2717 binder32bit: true,
2718 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002719 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002720 vndk: {
2721 enabled: true,
2722 },
2723 arch: {
2724 arm: {
2725 srcs: ["liba.so"],
2726 },
2727 },
2728 }
2729 cc_library {
2730 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002731 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002732 }
2733 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002734 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002735 symbol_file: "",
2736 }
2737 cc_library {
2738 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07002739 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002740 }
2741 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002742 name: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002743 vendor_available: false,
2744 symbol_file: "",
2745 }`
2746
2747 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002748 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2749 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2750 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002751 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002752
Jooyung Han0302a842019-10-30 18:43:49 +09002753 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002754 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09002755 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002756 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09002757 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002758 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09002759 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002760 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09002761
Colin Crossfb0c16e2019-11-20 17:12:35 -08002762 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002763
Jooyung Han38002912019-05-16 04:01:54 +09002764 tests := []struct {
2765 variant string
2766 name string
2767 expected string
2768 }{
2769 {vendorVariant, "libvndk", "native:vndk"},
2770 {vendorVariant, "libvndksp", "native:vndk"},
2771 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2772 {vendorVariant, "libvendor", "native:vendor"},
2773 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09002774 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002775 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002776 {coreVariant, "libvndk", "native:platform"},
2777 {coreVariant, "libvndkprivate", "native:platform"},
2778 {coreVariant, "libllndk", "native:platform"},
2779 }
2780 for _, test := range tests {
2781 t.Run(test.name, func(t *testing.T) {
2782 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2783 assertString(t, module.makeLinkType, test.expected)
2784 })
2785 }
2786}
2787
Colin Cross0af4b842015-04-30 16:36:18 -07002788var (
2789 str11 = "01234567891"
2790 str10 = str11[:10]
2791 str9 = str11[:9]
2792 str5 = str11[:5]
2793 str4 = str11[:4]
2794)
2795
2796var splitListForSizeTestCases = []struct {
2797 in []string
2798 out [][]string
2799 size int
2800}{
2801 {
2802 in: []string{str10},
2803 out: [][]string{{str10}},
2804 size: 10,
2805 },
2806 {
2807 in: []string{str9},
2808 out: [][]string{{str9}},
2809 size: 10,
2810 },
2811 {
2812 in: []string{str5},
2813 out: [][]string{{str5}},
2814 size: 10,
2815 },
2816 {
2817 in: []string{str11},
2818 out: nil,
2819 size: 10,
2820 },
2821 {
2822 in: []string{str10, str10},
2823 out: [][]string{{str10}, {str10}},
2824 size: 10,
2825 },
2826 {
2827 in: []string{str9, str10},
2828 out: [][]string{{str9}, {str10}},
2829 size: 10,
2830 },
2831 {
2832 in: []string{str10, str9},
2833 out: [][]string{{str10}, {str9}},
2834 size: 10,
2835 },
2836 {
2837 in: []string{str5, str4},
2838 out: [][]string{{str5, str4}},
2839 size: 10,
2840 },
2841 {
2842 in: []string{str5, str4, str5},
2843 out: [][]string{{str5, str4}, {str5}},
2844 size: 10,
2845 },
2846 {
2847 in: []string{str5, str4, str5, str4},
2848 out: [][]string{{str5, str4}, {str5, str4}},
2849 size: 10,
2850 },
2851 {
2852 in: []string{str5, str4, str5, str5},
2853 out: [][]string{{str5, str4}, {str5}, {str5}},
2854 size: 10,
2855 },
2856 {
2857 in: []string{str5, str5, str5, str4},
2858 out: [][]string{{str5}, {str5}, {str5, str4}},
2859 size: 10,
2860 },
2861 {
2862 in: []string{str9, str11},
2863 out: nil,
2864 size: 10,
2865 },
2866 {
2867 in: []string{str11, str9},
2868 out: nil,
2869 size: 10,
2870 },
2871}
2872
2873func TestSplitListForSize(t *testing.T) {
2874 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08002875 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07002876
2877 var outStrings [][]string
2878
2879 if len(out) > 0 {
2880 outStrings = make([][]string, len(out))
2881 for i, o := range out {
2882 outStrings[i] = o.Strings()
2883 }
2884 }
2885
2886 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07002887 t.Errorf("incorrect output:")
2888 t.Errorf(" input: %#v", testCase.in)
2889 t.Errorf(" size: %d", testCase.size)
2890 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07002891 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07002892 }
2893 }
2894}
Jeff Gaston294356f2017-09-27 17:05:30 -07002895
2896var staticLinkDepOrderTestCases = []struct {
2897 // This is a string representation of a map[moduleName][]moduleDependency .
2898 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002899 inStatic string
2900
2901 // This is a string representation of a map[moduleName][]moduleDependency .
2902 // It models the dependencies declared in an Android.bp file.
2903 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002904
2905 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2906 // The keys of allOrdered specify which modules we would like to check.
2907 // The values of allOrdered specify the expected result (of the transitive closure of all
2908 // dependencies) for each module to test
2909 allOrdered string
2910
2911 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2912 // The keys of outOrdered specify which modules we would like to check.
2913 // The values of outOrdered specify the expected result (of the ordered linker command line)
2914 // for each module to test.
2915 outOrdered string
2916}{
2917 // Simple tests
2918 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002919 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002920 outOrdered: "",
2921 },
2922 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002923 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002924 outOrdered: "a:",
2925 },
2926 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002927 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002928 outOrdered: "a:b; b:",
2929 },
2930 // Tests of reordering
2931 {
2932 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002933 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002934 outOrdered: "a:b,c,d; b:d; c:d; d:",
2935 },
2936 {
2937 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002938 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002939 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2940 },
2941 {
2942 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002943 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002944 outOrdered: "a:d,b,e,c; d:b; e:c",
2945 },
2946 {
2947 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002948 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002949 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2950 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2951 },
2952 {
2953 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002954 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 -07002955 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2956 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2957 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002958 // shared dependencies
2959 {
2960 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2961 // So, we don't actually have to check that a shared dependency of c will change the order
2962 // of a library that depends statically on b and on c. We only need to check that if c has
2963 // a shared dependency on b, that that shows up in allOrdered.
2964 inShared: "c:b",
2965 allOrdered: "c:b",
2966 outOrdered: "c:",
2967 },
2968 {
2969 // This test doesn't actually include any shared dependencies but it's a reminder of what
2970 // the second phase of the above test would look like
2971 inStatic: "a:b,c; c:b",
2972 allOrdered: "a:c,b; c:b",
2973 outOrdered: "a:c,b; c:b",
2974 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002975 // tiebreakers for when two modules specifying different orderings and there is no dependency
2976 // to dictate an order
2977 {
2978 // 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 -08002979 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002980 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2981 },
2982 {
2983 // 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 -08002984 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 -07002985 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2986 },
2987 // Tests involving duplicate dependencies
2988 {
2989 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002990 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002991 outOrdered: "a:c,b",
2992 },
2993 {
2994 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002995 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002996 outOrdered: "a:d,c,b",
2997 },
2998 // Tests to confirm the nonexistence of infinite loops.
2999 // These cases should never happen, so as long as the test terminates and the
3000 // result is deterministic then that should be fine.
3001 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003002 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003003 outOrdered: "a:a",
3004 },
3005 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003006 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003007 allOrdered: "a:b,c; b:c,a; c:a,b",
3008 outOrdered: "a:b; b:c; c:a",
3009 },
3010 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003011 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003012 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
3013 outOrdered: "a:c,b; b:a,c; c:b,a",
3014 },
3015}
3016
3017// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
3018func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
3019 // convert from "a:b,c; d:e" to "a:b,c;d:e"
3020 strippedText := strings.Replace(text, " ", "", -1)
3021 if len(strippedText) < 1 {
3022 return []android.Path{}, make(map[android.Path][]android.Path, 0)
3023 }
3024 allDeps = make(map[android.Path][]android.Path, 0)
3025
3026 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
3027 moduleTexts := strings.Split(strippedText, ";")
3028
3029 outputForModuleName := func(moduleName string) android.Path {
3030 return android.PathForTesting(moduleName)
3031 }
3032
3033 for _, moduleText := range moduleTexts {
3034 // convert from "a:b,c" to ["a", "b,c"]
3035 components := strings.Split(moduleText, ":")
3036 if len(components) != 2 {
3037 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
3038 }
3039 moduleName := components[0]
3040 moduleOutput := outputForModuleName(moduleName)
3041 modulesInOrder = append(modulesInOrder, moduleOutput)
3042
3043 depString := components[1]
3044 // convert from "b,c" to ["b", "c"]
3045 depNames := strings.Split(depString, ",")
3046 if len(depString) < 1 {
3047 depNames = []string{}
3048 }
3049 var deps []android.Path
3050 for _, depName := range depNames {
3051 deps = append(deps, outputForModuleName(depName))
3052 }
3053 allDeps[moduleOutput] = deps
3054 }
3055 return modulesInOrder, allDeps
3056}
3057
Jeff Gaston294356f2017-09-27 17:05:30 -07003058func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
3059 for _, moduleName := range moduleNames {
3060 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
3061 output := module.outputFile.Path()
3062 paths = append(paths, output)
3063 }
3064 return paths
3065}
3066
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003067func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07003068 ctx := testCc(t, `
3069 cc_library {
3070 name: "a",
3071 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09003072 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003073 }
3074 cc_library {
3075 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003076 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003077 }
3078 cc_library {
3079 name: "c",
3080 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003081 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003082 }
3083 cc_library {
3084 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09003085 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003086 }
3087
3088 `)
3089
Colin Cross7113d202019-11-20 16:39:12 -08003090 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07003091 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003092 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3093 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07003094
3095 if !reflect.DeepEqual(actual, expected) {
3096 t.Errorf("staticDeps orderings were not propagated correctly"+
3097 "\nactual: %v"+
3098 "\nexpected: %v",
3099 actual,
3100 expected,
3101 )
3102 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09003103}
Jeff Gaston294356f2017-09-27 17:05:30 -07003104
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003105func TestStaticLibDepReorderingWithShared(t *testing.T) {
3106 ctx := testCc(t, `
3107 cc_library {
3108 name: "a",
3109 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09003110 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003111 }
3112 cc_library {
3113 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003114 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003115 }
3116 cc_library {
3117 name: "c",
3118 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003119 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003120 }
3121
3122 `)
3123
Colin Cross7113d202019-11-20 16:39:12 -08003124 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003125 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003126 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3127 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003128
3129 if !reflect.DeepEqual(actual, expected) {
3130 t.Errorf("staticDeps orderings did not account for shared libs"+
3131 "\nactual: %v"+
3132 "\nexpected: %v",
3133 actual,
3134 expected,
3135 )
3136 }
3137}
3138
Jooyung Hanb04a4992020-03-13 18:57:35 +09003139func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07003140 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003141 if !reflect.DeepEqual(actual, expected) {
3142 t.Errorf(message+
3143 "\nactual: %v"+
3144 "\nexpected: %v",
3145 actual,
3146 expected,
3147 )
3148 }
3149}
3150
Jooyung Han61b66e92020-03-21 14:21:46 +00003151func TestLlndkLibrary(t *testing.T) {
3152 ctx := testCc(t, `
3153 cc_library {
3154 name: "libllndk",
3155 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07003156 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003157 }
3158 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003159 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003160 }
3161 `)
3162 actual := ctx.ModuleVariantsForTests("libllndk.llndk")
3163 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00003164 "android_vendor.VER_arm64_armv8-a_shared_1",
3165 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003166 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003167 "android_vendor.VER_arm_armv7-a-neon_shared_1",
3168 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003169 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003170 }
3171 checkEquals(t, "variants for llndk stubs", expected, actual)
3172
3173 params := ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
3174 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
3175
3176 params = ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
3177 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
3178}
3179
Jiyong Parka46a4d52017-12-14 19:54:34 +09003180func TestLlndkHeaders(t *testing.T) {
3181 ctx := testCc(t, `
3182 llndk_headers {
3183 name: "libllndk_headers",
3184 export_include_dirs: ["my_include"],
3185 }
3186 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003187 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09003188 export_llndk_headers: ["libllndk_headers"],
3189 }
3190 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07003191 name: "libllndk",
3192 llndk_stubs: "libllndk.llndk",
3193 }
3194
3195 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09003196 name: "libvendor",
3197 shared_libs: ["libllndk"],
3198 vendor: true,
3199 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003200 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08003201 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09003202 }
3203 `)
3204
3205 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08003206 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09003207 cflags := cc.Args["cFlags"]
3208 if !strings.Contains(cflags, "-Imy_include") {
3209 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
3210 }
3211}
3212
Logan Chien43d34c32017-12-20 01:17:32 +08003213func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
3214 actual := module.Properties.AndroidMkRuntimeLibs
3215 if !reflect.DeepEqual(actual, expected) {
3216 t.Errorf("incorrect runtime_libs for shared libs"+
3217 "\nactual: %v"+
3218 "\nexpected: %v",
3219 actual,
3220 expected,
3221 )
3222 }
3223}
3224
3225const runtimeLibAndroidBp = `
3226 cc_library {
3227 name: "libvendor_available1",
3228 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003229 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003230 nocrt : true,
3231 system_shared_libs : [],
3232 }
3233 cc_library {
3234 name: "libvendor_available2",
3235 vendor_available: true,
3236 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003237 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003238 nocrt : true,
3239 system_shared_libs : [],
3240 }
3241 cc_library {
3242 name: "libvendor_available3",
3243 vendor_available: true,
3244 runtime_libs: ["libvendor_available1"],
3245 target: {
3246 vendor: {
3247 exclude_runtime_libs: ["libvendor_available1"],
3248 }
3249 },
Yi Konge7fe9912019-06-02 00:53:50 -07003250 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003251 nocrt : true,
3252 system_shared_libs : [],
3253 }
3254 cc_library {
3255 name: "libcore",
3256 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003257 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003258 nocrt : true,
3259 system_shared_libs : [],
3260 }
3261 cc_library {
3262 name: "libvendor1",
3263 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003264 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003265 nocrt : true,
3266 system_shared_libs : [],
3267 }
3268 cc_library {
3269 name: "libvendor2",
3270 vendor: true,
3271 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003272 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003273 nocrt : true,
3274 system_shared_libs : [],
3275 }
3276`
3277
3278func TestRuntimeLibs(t *testing.T) {
3279 ctx := testCc(t, runtimeLibAndroidBp)
3280
3281 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08003282 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003283
3284 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3285 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3286
3287 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
3288 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3289
3290 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3291 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08003292 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003293
3294 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3295 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
3296
3297 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
3298 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
3299}
3300
3301func TestExcludeRuntimeLibs(t *testing.T) {
3302 ctx := testCc(t, runtimeLibAndroidBp)
3303
Colin Cross7113d202019-11-20 16:39:12 -08003304 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003305 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
3306 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3307
Colin Crossfb0c16e2019-11-20 17:12:35 -08003308 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003309 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
3310 checkRuntimeLibs(t, nil, module)
3311}
3312
3313func TestRuntimeLibsNoVndk(t *testing.T) {
3314 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3315
3316 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3317
Colin Cross7113d202019-11-20 16:39:12 -08003318 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003319
3320 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3321 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
3322
3323 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
3324 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
3325}
3326
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003327func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003328 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003329 actual := module.Properties.AndroidMkStaticLibs
3330 if !reflect.DeepEqual(actual, expected) {
3331 t.Errorf("incorrect static_libs"+
3332 "\nactual: %v"+
3333 "\nexpected: %v",
3334 actual,
3335 expected,
3336 )
3337 }
3338}
3339
3340const staticLibAndroidBp = `
3341 cc_library {
3342 name: "lib1",
3343 }
3344 cc_library {
3345 name: "lib2",
3346 static_libs: ["lib1"],
3347 }
3348`
3349
3350func TestStaticLibDepExport(t *testing.T) {
3351 ctx := testCc(t, staticLibAndroidBp)
3352
3353 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003354 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003355 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003356 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003357
3358 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003359 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003360 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3361 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003362 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003363}
3364
Jiyong Parkd08b6972017-09-26 10:50:54 +09003365var compilerFlagsTestCases = []struct {
3366 in string
3367 out bool
3368}{
3369 {
3370 in: "a",
3371 out: false,
3372 },
3373 {
3374 in: "-a",
3375 out: true,
3376 },
3377 {
3378 in: "-Ipath/to/something",
3379 out: false,
3380 },
3381 {
3382 in: "-isystempath/to/something",
3383 out: false,
3384 },
3385 {
3386 in: "--coverage",
3387 out: false,
3388 },
3389 {
3390 in: "-include a/b",
3391 out: true,
3392 },
3393 {
3394 in: "-include a/b c/d",
3395 out: false,
3396 },
3397 {
3398 in: "-DMACRO",
3399 out: true,
3400 },
3401 {
3402 in: "-DMAC RO",
3403 out: false,
3404 },
3405 {
3406 in: "-a -b",
3407 out: false,
3408 },
3409 {
3410 in: "-DMACRO=definition",
3411 out: true,
3412 },
3413 {
3414 in: "-DMACRO=defi nition",
3415 out: true, // TODO(jiyong): this should be false
3416 },
3417 {
3418 in: "-DMACRO(x)=x + 1",
3419 out: true,
3420 },
3421 {
3422 in: "-DMACRO=\"defi nition\"",
3423 out: true,
3424 },
3425}
3426
3427type mockContext struct {
3428 BaseModuleContext
3429 result bool
3430}
3431
3432func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3433 // CheckBadCompilerFlags calls this function when the flag should be rejected
3434 ctx.result = false
3435}
3436
3437func TestCompilerFlags(t *testing.T) {
3438 for _, testCase := range compilerFlagsTestCases {
3439 ctx := &mockContext{result: true}
3440 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3441 if ctx.result != testCase.out {
3442 t.Errorf("incorrect output:")
3443 t.Errorf(" input: %#v", testCase.in)
3444 t.Errorf(" expected: %#v", testCase.out)
3445 t.Errorf(" got: %#v", ctx.result)
3446 }
3447 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003448}
Jiyong Park374510b2018-03-19 18:23:01 +09003449
3450func TestVendorPublicLibraries(t *testing.T) {
3451 ctx := testCc(t, `
3452 cc_library_headers {
3453 name: "libvendorpublic_headers",
3454 export_include_dirs: ["my_include"],
3455 }
3456 vendor_public_library {
3457 name: "libvendorpublic",
3458 symbol_file: "",
3459 export_public_headers: ["libvendorpublic_headers"],
3460 }
3461 cc_library {
3462 name: "libvendorpublic",
3463 srcs: ["foo.c"],
3464 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003465 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003466 nocrt: true,
3467 }
3468
3469 cc_library {
3470 name: "libsystem",
3471 shared_libs: ["libvendorpublic"],
3472 vendor: false,
3473 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003474 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003475 nocrt: true,
3476 }
3477 cc_library {
3478 name: "libvendor",
3479 shared_libs: ["libvendorpublic"],
3480 vendor: true,
3481 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003482 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003483 nocrt: true,
3484 }
3485 `)
3486
Colin Cross7113d202019-11-20 16:39:12 -08003487 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003488 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003489
3490 // test if header search paths are correctly added
3491 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003492 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003493 cflags := cc.Args["cFlags"]
3494 if !strings.Contains(cflags, "-Imy_include") {
3495 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3496 }
3497
3498 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003499 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003500 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003501 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003502 if !strings.Contains(libflags, stubPaths[0].String()) {
3503 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3504 }
3505
3506 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003507 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003508 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003509 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003510 if !strings.Contains(libflags, stubPaths[0].String()) {
3511 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3512 }
3513
3514}
Jiyong Park37b25202018-07-11 10:49:27 +09003515
3516func TestRecovery(t *testing.T) {
3517 ctx := testCc(t, `
3518 cc_library_shared {
3519 name: "librecovery",
3520 recovery: true,
3521 }
3522 cc_library_shared {
3523 name: "librecovery32",
3524 recovery: true,
3525 compile_multilib:"32",
3526 }
Jiyong Park5baac542018-08-28 09:55:37 +09003527 cc_library_shared {
3528 name: "libHalInRecovery",
3529 recovery_available: true,
3530 vendor: true,
3531 }
Jiyong Park37b25202018-07-11 10:49:27 +09003532 `)
3533
3534 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003535 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003536 if len(variants) != 1 || !android.InList(arm64, variants) {
3537 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3538 }
3539
3540 variants = ctx.ModuleVariantsForTests("librecovery32")
3541 if android.InList(arm64, variants) {
3542 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3543 }
Jiyong Park5baac542018-08-28 09:55:37 +09003544
3545 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3546 if !recoveryModule.Platform() {
3547 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3548 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003549}
Jiyong Park5baac542018-08-28 09:55:37 +09003550
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003551func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3552 bp := `
3553 cc_prebuilt_test_library_shared {
3554 name: "test_lib",
3555 relative_install_path: "foo/bar/baz",
3556 srcs: ["srcpath/dontusethispath/baz.so"],
3557 }
3558
3559 cc_test {
3560 name: "main_test",
3561 data_libs: ["test_lib"],
3562 gtest: false,
3563 }
3564 `
3565
3566 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3567 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3568 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3569 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3570
3571 ctx := testCcWithConfig(t, config)
3572 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3573 testBinary := module.(*Module).linker.(*testBinary)
3574 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3575 if err != nil {
3576 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3577 }
3578 if len(outputFiles) != 1 {
3579 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3580 }
3581 if len(testBinary.dataPaths()) != 1 {
3582 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3583 }
3584
3585 outputPath := outputFiles[0].String()
3586
3587 if !strings.HasSuffix(outputPath, "/main_test") {
3588 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3589 }
3590 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
3591 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3592 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3593 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3594 }
3595}
3596
Jiyong Park7ed9de32018-10-15 22:25:07 +09003597func TestVersionedStubs(t *testing.T) {
3598 ctx := testCc(t, `
3599 cc_library_shared {
3600 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003601 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003602 stubs: {
3603 symbol_file: "foo.map.txt",
3604 versions: ["1", "2", "3"],
3605 },
3606 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003607
Jiyong Park7ed9de32018-10-15 22:25:07 +09003608 cc_library_shared {
3609 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003610 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003611 shared_libs: ["libFoo#1"],
3612 }`)
3613
3614 variants := ctx.ModuleVariantsForTests("libFoo")
3615 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003616 "android_arm64_armv8-a_shared",
3617 "android_arm64_armv8-a_shared_1",
3618 "android_arm64_armv8-a_shared_2",
3619 "android_arm64_armv8-a_shared_3",
3620 "android_arm_armv7-a-neon_shared",
3621 "android_arm_armv7-a-neon_shared_1",
3622 "android_arm_armv7-a-neon_shared_2",
3623 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003624 }
3625 variantsMismatch := false
3626 if len(variants) != len(expectedVariants) {
3627 variantsMismatch = true
3628 } else {
3629 for _, v := range expectedVariants {
3630 if !inList(v, variants) {
3631 variantsMismatch = false
3632 }
3633 }
3634 }
3635 if variantsMismatch {
3636 t.Errorf("variants of libFoo expected:\n")
3637 for _, v := range expectedVariants {
3638 t.Errorf("%q\n", v)
3639 }
3640 t.Errorf(", but got:\n")
3641 for _, v := range variants {
3642 t.Errorf("%q\n", v)
3643 }
3644 }
3645
Colin Cross7113d202019-11-20 16:39:12 -08003646 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003647 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003648 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003649 if !strings.Contains(libFlags, libFoo1StubPath) {
3650 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3651 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003652
Colin Cross7113d202019-11-20 16:39:12 -08003653 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003654 cFlags := libBarCompileRule.Args["cFlags"]
3655 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3656 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3657 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3658 }
Jiyong Park37b25202018-07-11 10:49:27 +09003659}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003660
Jooyung Hanb04a4992020-03-13 18:57:35 +09003661func TestVersioningMacro(t *testing.T) {
3662 for _, tc := range []struct{ moduleName, expected string }{
3663 {"libc", "__LIBC_API__"},
3664 {"libfoo", "__LIBFOO_API__"},
3665 {"libfoo@1", "__LIBFOO_1_API__"},
3666 {"libfoo-v1", "__LIBFOO_V1_API__"},
3667 {"libfoo.v1", "__LIBFOO_V1_API__"},
3668 } {
3669 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3670 }
3671}
3672
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003673func TestStaticExecutable(t *testing.T) {
3674 ctx := testCc(t, `
3675 cc_binary {
3676 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003677 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003678 static_executable: true,
3679 }`)
3680
Colin Cross7113d202019-11-20 16:39:12 -08003681 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003682 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3683 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003684 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003685 for _, lib := range systemStaticLibs {
3686 if !strings.Contains(libFlags, lib) {
3687 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3688 }
3689 }
3690 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3691 for _, lib := range systemSharedLibs {
3692 if strings.Contains(libFlags, lib) {
3693 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3694 }
3695 }
3696}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003697
3698func TestStaticDepsOrderWithStubs(t *testing.T) {
3699 ctx := testCc(t, `
3700 cc_binary {
3701 name: "mybin",
3702 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003703 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003704 static_executable: true,
3705 stl: "none",
3706 }
3707
3708 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003709 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003710 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003711 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003712 stl: "none",
3713 }
3714
3715 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003716 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003717 srcs: ["foo.c"],
3718 stl: "none",
3719 stubs: {
3720 versions: ["1"],
3721 },
3722 }`)
3723
Colin Cross0de8a1e2020-09-18 14:15:30 -07003724 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3725 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003726 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003727
3728 if !reflect.DeepEqual(actual, expected) {
3729 t.Errorf("staticDeps orderings were not propagated correctly"+
3730 "\nactual: %v"+
3731 "\nexpected: %v",
3732 actual,
3733 expected,
3734 )
3735 }
3736}
Jooyung Han38002912019-05-16 04:01:54 +09003737
Jooyung Hand48f3c32019-08-23 11:18:57 +09003738func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3739 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3740 cc_library {
3741 name: "libA",
3742 srcs: ["foo.c"],
3743 shared_libs: ["libB"],
3744 stl: "none",
3745 }
3746
3747 cc_library {
3748 name: "libB",
3749 srcs: ["foo.c"],
3750 enabled: false,
3751 stl: "none",
3752 }
3753 `)
3754}
3755
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003756// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3757// correctly.
3758func TestFuzzTarget(t *testing.T) {
3759 ctx := testCc(t, `
3760 cc_fuzz {
3761 name: "fuzz_smoke_test",
3762 srcs: ["foo.c"],
3763 }`)
3764
Paul Duffin075c4172019-12-19 19:06:13 +00003765 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003766 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3767}
3768
Jiyong Park29074592019-07-07 16:27:47 +09003769func TestAidl(t *testing.T) {
3770}
3771
Jooyung Han38002912019-05-16 04:01:54 +09003772func assertString(t *testing.T, got, expected string) {
3773 t.Helper()
3774 if got != expected {
3775 t.Errorf("expected %q got %q", expected, got)
3776 }
3777}
3778
3779func assertArrayString(t *testing.T, got, expected []string) {
3780 t.Helper()
3781 if len(got) != len(expected) {
3782 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3783 return
3784 }
3785 for i := range got {
3786 if got[i] != expected[i] {
3787 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3788 i, expected[i], expected, got[i], got)
3789 return
3790 }
3791 }
3792}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003793
Jooyung Han0302a842019-10-30 18:43:49 +09003794func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3795 t.Helper()
3796 assertArrayString(t, android.SortedStringKeys(m), expected)
3797}
3798
Colin Crosse1bb5d02019-09-24 14:55:04 -07003799func TestDefaults(t *testing.T) {
3800 ctx := testCc(t, `
3801 cc_defaults {
3802 name: "defaults",
3803 srcs: ["foo.c"],
3804 static: {
3805 srcs: ["bar.c"],
3806 },
3807 shared: {
3808 srcs: ["baz.c"],
3809 },
3810 }
3811
3812 cc_library_static {
3813 name: "libstatic",
3814 defaults: ["defaults"],
3815 }
3816
3817 cc_library_shared {
3818 name: "libshared",
3819 defaults: ["defaults"],
3820 }
3821
3822 cc_library {
3823 name: "libboth",
3824 defaults: ["defaults"],
3825 }
3826
3827 cc_binary {
3828 name: "binary",
3829 defaults: ["defaults"],
3830 }`)
3831
3832 pathsToBase := func(paths android.Paths) []string {
3833 var ret []string
3834 for _, p := range paths {
3835 ret = append(ret, p.Base())
3836 }
3837 return ret
3838 }
3839
Colin Cross7113d202019-11-20 16:39:12 -08003840 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003841 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3842 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3843 }
Colin Cross7113d202019-11-20 16:39:12 -08003844 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003845 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3846 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3847 }
Colin Cross7113d202019-11-20 16:39:12 -08003848 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003849 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3850 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3851 }
3852
Colin Cross7113d202019-11-20 16:39:12 -08003853 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003854 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3855 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3856 }
Colin Cross7113d202019-11-20 16:39:12 -08003857 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003858 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3859 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3860 }
3861}
Colin Crosseabaedd2020-02-06 17:01:55 -08003862
3863func TestProductVariableDefaults(t *testing.T) {
3864 bp := `
3865 cc_defaults {
3866 name: "libfoo_defaults",
3867 srcs: ["foo.c"],
3868 cppflags: ["-DFOO"],
3869 product_variables: {
3870 debuggable: {
3871 cppflags: ["-DBAR"],
3872 },
3873 },
3874 }
3875
3876 cc_library {
3877 name: "libfoo",
3878 defaults: ["libfoo_defaults"],
3879 }
3880 `
3881
3882 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3883 config.TestProductVariables.Debuggable = BoolPtr(true)
3884
Colin Crossae8600b2020-10-29 17:09:13 -07003885 ctx := CreateTestContext(config)
Colin Crosseabaedd2020-02-06 17:01:55 -08003886 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
3887 ctx.BottomUp("variable", android.VariableMutator).Parallel()
3888 })
Colin Crossae8600b2020-10-29 17:09:13 -07003889 ctx.Register()
Colin Crosseabaedd2020-02-06 17:01:55 -08003890
3891 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3892 android.FailIfErrored(t, errs)
3893 _, errs = ctx.PrepareBuildActions(config)
3894 android.FailIfErrored(t, errs)
3895
3896 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
3897 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
3898 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
3899 }
3900}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003901
3902func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3903 t.Parallel()
3904 bp := `
3905 cc_library_static {
3906 name: "libfoo",
3907 srcs: ["foo.c"],
3908 whole_static_libs: ["libbar"],
3909 }
3910
3911 cc_library_static {
3912 name: "libbar",
3913 whole_static_libs: ["libmissing"],
3914 }
3915 `
3916
3917 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3918 config.TestProductVariables.Allow_missing_dependencies = BoolPtr(true)
3919
Colin Crossae8600b2020-10-29 17:09:13 -07003920 ctx := CreateTestContext(config)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003921 ctx.SetAllowMissingDependencies(true)
Colin Crossae8600b2020-10-29 17:09:13 -07003922 ctx.Register()
Colin Crosse4f6eba2020-09-22 18:11:25 -07003923
3924 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3925 android.FailIfErrored(t, errs)
3926 _, errs = ctx.PrepareBuildActions(config)
3927 android.FailIfErrored(t, errs)
3928
3929 libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
3930 if g, w := libbar.Rule, android.ErrorRule; g != w {
3931 t.Fatalf("Expected libbar rule to be %q, got %q", w, g)
3932 }
3933
3934 if g, w := libbar.Args["error"], "missing dependencies: libmissing"; !strings.Contains(g, w) {
3935 t.Errorf("Expected libbar error to contain %q, was %q", w, g)
3936 }
3937
3938 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
3939 if g, w := libfoo.Inputs.Strings(), libbar.Output.String(); !android.InList(w, g) {
3940 t.Errorf("Expected libfoo.a to depend on %q, got %q", w, g)
3941 }
3942
3943}