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