blob: 78d84289736d12834c4cf2fa08ffb90ed11a42df [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 "sort"
24 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070025 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070026
27 "android/soong/android"
Colin Cross74d1ec02015-04-28 13:30:13 -070028)
29
Jiyong Park6a43f042017-10-12 23:05:00 +090030var buildDir string
31
32func setUp() {
33 var err error
34 buildDir, err = ioutil.TempDir("", "soong_cc_test")
35 if err != nil {
36 panic(err)
37 }
38}
39
40func tearDown() {
41 os.RemoveAll(buildDir)
42}
43
44func TestMain(m *testing.M) {
45 run := func() int {
46 setUp()
47 defer tearDown()
48
49 return m.Run()
50 }
51
52 os.Exit(run())
53}
54
Colin Cross98be1bb2019-12-13 20:41:13 -080055func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070056 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080057 ctx := CreateTestContext()
58 ctx.Register(config)
Logan Chienf3511742017-10-31 18:04:35 +080059
Jeff Gastond3e141d2017-08-08 17:46:01 -070060 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +080061 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +090062 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +080063 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +090064
65 return ctx
66}
67
Logan Chienf3511742017-10-31 18:04:35 +080068func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080069 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080070 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070071 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
72 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +080073
Colin Cross98be1bb2019-12-13 20:41:13 -080074 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080075}
76
77func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080078 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080079 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070080 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +080081
Colin Cross98be1bb2019-12-13 20:41:13 -080082 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080083}
84
Justin Yun5f7f7e82019-11-18 19:52:14 +090085func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +080086 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +080087
Colin Cross98be1bb2019-12-13 20:41:13 -080088 ctx := CreateTestContext()
89 ctx.Register(config)
Logan Chienf3511742017-10-31 18:04:35 +080090
91 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
92 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +080093 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +080094 return
95 }
96
97 _, errs = ctx.PrepareBuildActions(config)
98 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +080099 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800100 return
101 }
102
103 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
104}
105
Justin Yun5f7f7e82019-11-18 19:52:14 +0900106func testCcError(t *testing.T, pattern string, bp string) {
107 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) {
115 config := TestConfig(buildDir, android.Android, nil, bp, nil)
116 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
117 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
118 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
119 testCcErrorWithConfig(t, pattern, config)
120 return
121}
122
Logan Chienf3511742017-10-31 18:04:35 +0800123const (
Colin Cross7113d202019-11-20 16:39:12 -0800124 coreVariant = "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800125 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun5f7f7e82019-11-18 19:52:14 +0900126 productVariant = "android_product.VER_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800127 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800128)
129
Doug Hornc32c6b02019-01-17 14:44:05 -0800130func TestFuchsiaDeps(t *testing.T) {
131 t.Helper()
132
133 bp := `
134 cc_library {
135 name: "libTest",
136 srcs: ["foo.c"],
137 target: {
138 fuchsia: {
139 srcs: ["bar.c"],
140 },
141 },
142 }`
143
Colin Cross98be1bb2019-12-13 20:41:13 -0800144 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
145 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800146
147 rt := false
148 fb := false
149
150 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
151 implicits := ld.Implicits
152 for _, lib := range implicits {
153 if strings.Contains(lib.Rel(), "libcompiler_rt") {
154 rt = true
155 }
156
157 if strings.Contains(lib.Rel(), "libbioniccompat") {
158 fb = true
159 }
160 }
161
162 if !rt || !fb {
163 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
164 }
165}
166
167func TestFuchsiaTargetDecl(t *testing.T) {
168 t.Helper()
169
170 bp := `
171 cc_library {
172 name: "libTest",
173 srcs: ["foo.c"],
174 target: {
175 fuchsia: {
176 srcs: ["bar.c"],
177 },
178 },
179 }`
180
Colin Cross98be1bb2019-12-13 20:41:13 -0800181 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
182 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800183 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
184 var objs []string
185 for _, o := range ld.Inputs {
186 objs = append(objs, o.Base())
187 }
188 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
189 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
190 }
191}
192
Jiyong Park6a43f042017-10-12 23:05:00 +0900193func TestVendorSrc(t *testing.T) {
194 ctx := testCc(t, `
195 cc_library {
196 name: "libTest",
197 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700198 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800199 nocrt: true,
200 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900201 vendor_available: true,
202 target: {
203 vendor: {
204 srcs: ["bar.c"],
205 },
206 },
207 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900208 `)
209
Logan Chienf3511742017-10-31 18:04:35 +0800210 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900211 var objs []string
212 for _, o := range ld.Inputs {
213 objs = append(objs, o.Base())
214 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800215 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900216 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
217 }
218}
219
Logan Chienf3511742017-10-31 18:04:35 +0800220func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900221 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800222
Logan Chiend3c59a22018-03-29 14:08:15 +0800223 t.Helper()
224
Justin Yun0ecf0b22020-02-28 15:07:59 +0900225 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Ivan Lozano52767be2019-10-18 14:49:46 -0700226 if !mod.HasVendorVariant() {
Justin Yun0ecf0b22020-02-28 15:07:59 +0900227 t.Errorf("%q must have variant %q", name, variant)
Logan Chienf3511742017-10-31 18:04:35 +0800228 }
229
230 // Check library properties.
231 lib, ok := mod.compiler.(*libraryDecorator)
232 if !ok {
233 t.Errorf("%q must have libraryDecorator", name)
234 } else if lib.baseInstaller.subDir != subDir {
235 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
236 lib.baseInstaller.subDir)
237 }
238
239 // Check VNDK properties.
240 if mod.vndkdep == nil {
241 t.Fatalf("%q must have `vndkdep`", name)
242 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700243 if !mod.IsVndk() {
244 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800245 }
246 if mod.isVndkSp() != isVndkSp {
247 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
248 }
249
250 // Check VNDK extension properties.
251 isVndkExt := extends != ""
252 if mod.isVndkExt() != isVndkExt {
253 t.Errorf("%q isVndkExt() must equal to %t", name, isVndkExt)
254 }
255
256 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
257 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
258 }
259}
260
Bill Peckham945441c2020-08-31 16:07:58 -0700261func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool) {
262 t.Helper()
Jooyung Han39edb6c2019-11-06 16:53:07 +0900263 mod, ok := ctx.ModuleForTests(moduleName, variant).Module().(android.OutputFileProducer)
264 if !ok {
265 t.Errorf("%q must have output\n", moduleName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900266 return
267 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900268 outputFiles, err := mod.OutputFiles("")
269 if err != nil || len(outputFiles) != 1 {
270 t.Errorf("%q must have single output\n", moduleName)
271 return
272 }
273 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900274
Bill Peckham945441c2020-08-31 16:07:58 -0700275 if include {
276 out := singleton.Output(snapshotPath)
277 if out.Input.String() != outputFiles[0].String() {
278 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
279 }
280 } else {
281 out := singleton.MaybeOutput(snapshotPath)
282 if out.Rule != nil {
283 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
284 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900285 }
286}
287
Bill Peckham945441c2020-08-31 16:07:58 -0700288func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
289 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true)
290}
291
292func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
293 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false)
294}
295
Jooyung Han2216fb12019-11-06 16:46:15 +0900296func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
297 t.Helper()
298 assertString(t, params.Rule.String(), android.WriteFile.String())
299 actual := strings.FieldsFunc(strings.ReplaceAll(params.Args["content"], "\\n", "\n"), func(r rune) bool { return r == '\n' })
300 assertArrayString(t, actual, expected)
301}
302
Jooyung Han097087b2019-10-22 19:32:18 +0900303func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
304 t.Helper()
305 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900306 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
307}
308
309func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
310 t.Helper()
311 vndkLibraries := ctx.ModuleForTests(module, "")
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +0900312
313 var output string
314 if module != "vndkcorevariant.libraries.txt" {
315 output = insertVndkVersion(module, "VER")
316 } else {
317 output = module
318 }
319
Jooyung Han2216fb12019-11-06 16:46:15 +0900320 checkWriteFileOutput(t, vndkLibraries.Output(output), expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900321}
322
Logan Chienf3511742017-10-31 18:04:35 +0800323func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800324 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800325 cc_library {
326 name: "libvndk",
327 vendor_available: true,
328 vndk: {
329 enabled: true,
330 },
331 nocrt: true,
332 }
333
334 cc_library {
335 name: "libvndk_private",
336 vendor_available: false,
337 vndk: {
338 enabled: true,
339 },
340 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900341 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800342 }
343
344 cc_library {
345 name: "libvndk_sp",
346 vendor_available: true,
347 vndk: {
348 enabled: true,
349 support_system_process: true,
350 },
351 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900352 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800353 }
354
355 cc_library {
356 name: "libvndk_sp_private",
357 vendor_available: false,
358 vndk: {
359 enabled: true,
360 support_system_process: true,
361 },
362 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900363 target: {
364 vendor: {
365 suffix: "-x",
366 },
367 },
Logan Chienf3511742017-10-31 18:04:35 +0800368 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900369 vndk_libraries_txt {
370 name: "llndk.libraries.txt",
371 }
372 vndk_libraries_txt {
373 name: "vndkcore.libraries.txt",
374 }
375 vndk_libraries_txt {
376 name: "vndksp.libraries.txt",
377 }
378 vndk_libraries_txt {
379 name: "vndkprivate.libraries.txt",
380 }
381 vndk_libraries_txt {
382 name: "vndkcorevariant.libraries.txt",
383 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800384 `
385
386 config := TestConfig(buildDir, android.Android, nil, bp, nil)
387 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
388 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
389
390 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800391
Justin Yun0ecf0b22020-02-28 15:07:59 +0900392 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "", vendorVariant)
393 checkVndkModule(t, ctx, "libvndk_private", "vndk-VER", false, "", vendorVariant)
394 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "", vendorVariant)
395 checkVndkModule(t, ctx, "libvndk_sp_private", "vndk-sp-VER", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900396
397 // Check VNDK snapshot output.
398
399 snapshotDir := "vndk-snapshot"
400 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
401
402 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
403 "arm64", "armv8-a"))
404 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
405 "arm", "armv7-a-neon"))
406
407 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
408 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
409 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
410 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
411
Colin Crossfb0c16e2019-11-20 17:12:35 -0800412 variant := "android_vendor.VER_arm64_armv8-a_shared"
413 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900414
Inseob Kim7f283f42020-06-01 21:53:49 +0900415 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
416
417 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
418 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
419 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
420 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900421
Jooyung Han39edb6c2019-11-06 16:53:07 +0900422 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900423 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
424 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
425 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
426 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900427
Jooyung Han097087b2019-10-22 19:32:18 +0900428 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
429 "LLNDK: libc.so",
430 "LLNDK: libdl.so",
431 "LLNDK: libft2.so",
432 "LLNDK: libm.so",
433 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900434 "VNDK-SP: libvndk_sp-x.so",
435 "VNDK-SP: libvndk_sp_private-x.so",
436 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900437 "VNDK-core: libvndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900438 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900439 "VNDK-private: libvndk-private.so",
440 "VNDK-private: libvndk_sp_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900441 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900442 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
443 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so"})
444 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so"})
445 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so"})
446 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
447}
448
Yo Chiangbba545e2020-06-09 16:15:37 +0800449func TestVndkWithHostSupported(t *testing.T) {
450 ctx := testCc(t, `
451 cc_library {
452 name: "libvndk_host_supported",
453 vendor_available: true,
454 vndk: {
455 enabled: true,
456 },
457 host_supported: true,
458 }
459
460 cc_library {
461 name: "libvndk_host_supported_but_disabled_on_device",
462 vendor_available: true,
463 vndk: {
464 enabled: true,
465 },
466 host_supported: true,
467 enabled: false,
468 target: {
469 host: {
470 enabled: true,
471 }
472 }
473 }
474
475 vndk_libraries_txt {
476 name: "vndkcore.libraries.txt",
477 }
478 `)
479
480 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
481}
482
Jooyung Han2216fb12019-11-06 16:46:15 +0900483func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800484 bp := `
Jooyung Han2216fb12019-11-06 16:46:15 +0900485 vndk_libraries_txt {
486 name: "llndk.libraries.txt",
Colin Cross98be1bb2019-12-13 20:41:13 -0800487 }`
488 config := TestConfig(buildDir, android.Android, nil, bp, nil)
489 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
490 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
491 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900492
493 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900494 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900495 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900496}
497
498func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800499 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900500 cc_library {
501 name: "libvndk",
502 vendor_available: true,
503 vndk: {
504 enabled: true,
505 },
506 nocrt: true,
507 }
508
509 cc_library {
510 name: "libvndk_sp",
511 vendor_available: true,
512 vndk: {
513 enabled: true,
514 support_system_process: true,
515 },
516 nocrt: true,
517 }
518
519 cc_library {
520 name: "libvndk2",
521 vendor_available: false,
522 vndk: {
523 enabled: true,
524 },
525 nocrt: true,
526 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900527
528 vndk_libraries_txt {
529 name: "vndkcorevariant.libraries.txt",
530 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800531 `
532
533 config := TestConfig(buildDir, android.Android, nil, bp, nil)
534 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
535 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
536 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
537
538 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
539
540 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900541
Jooyung Han2216fb12019-11-06 16:46:15 +0900542 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900543}
544
Chris Parsons79d66a52020-06-05 17:26:16 -0400545func TestDataLibs(t *testing.T) {
546 bp := `
547 cc_test_library {
548 name: "test_lib",
549 srcs: ["test_lib.cpp"],
550 gtest: false,
551 }
552
553 cc_test {
554 name: "main_test",
555 data_libs: ["test_lib"],
556 gtest: false,
557 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400558 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400559
560 config := TestConfig(buildDir, android.Android, nil, bp, nil)
561 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
562 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
563 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
564
565 ctx := testCcWithConfig(t, config)
566 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
567 testBinary := module.(*Module).linker.(*testBinary)
568 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
569 if err != nil {
570 t.Errorf("Expected cc_test to produce output files, error: %s", err)
571 return
572 }
573 if len(outputFiles) != 1 {
574 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
575 return
576 }
577 if len(testBinary.dataPaths()) != 1 {
578 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
579 return
580 }
581
582 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400583 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400584
585 if !strings.HasSuffix(outputPath, "/main_test") {
586 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
587 return
588 }
589 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
590 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
591 return
592 }
593}
594
Chris Parsons216e10a2020-07-09 17:12:52 -0400595func TestDataLibsRelativeInstallPath(t *testing.T) {
596 bp := `
597 cc_test_library {
598 name: "test_lib",
599 srcs: ["test_lib.cpp"],
600 relative_install_path: "foo/bar/baz",
601 gtest: false,
602 }
603
604 cc_test {
605 name: "main_test",
606 data_libs: ["test_lib"],
607 gtest: false,
608 }
609 `
610
611 config := TestConfig(buildDir, android.Android, nil, bp, nil)
612 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
613 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
614 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
615
616 ctx := testCcWithConfig(t, config)
617 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
618 testBinary := module.(*Module).linker.(*testBinary)
619 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
620 if err != nil {
621 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
622 }
623 if len(outputFiles) != 1 {
624 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
625 }
626 if len(testBinary.dataPaths()) != 1 {
627 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
628 }
629
630 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400631
632 if !strings.HasSuffix(outputPath, "/main_test") {
633 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
634 }
635 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
636 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
637 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400638 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400639 }
640}
641
Jooyung Han0302a842019-10-30 18:43:49 +0900642func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900643 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900644 cc_library {
645 name: "libvndk",
646 vendor_available: true,
647 vndk: {
648 enabled: true,
649 },
650 nocrt: true,
651 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900652 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900653
654 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
655 "LLNDK: libc.so",
656 "LLNDK: libdl.so",
657 "LLNDK: libft2.so",
658 "LLNDK: libm.so",
659 "VNDK-SP: libc++.so",
660 "VNDK-core: libvndk.so",
661 "VNDK-private: libft2.so",
662 })
Logan Chienf3511742017-10-31 18:04:35 +0800663}
664
Logan Chiend3c59a22018-03-29 14:08:15 +0800665func TestVndkDepError(t *testing.T) {
666 // Check whether an error is emitted when a VNDK lib depends on a system lib.
667 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
668 cc_library {
669 name: "libvndk",
670 vendor_available: true,
671 vndk: {
672 enabled: true,
673 },
674 shared_libs: ["libfwk"], // Cause error
675 nocrt: true,
676 }
677
678 cc_library {
679 name: "libfwk",
680 nocrt: true,
681 }
682 `)
683
684 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
685 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
686 cc_library {
687 name: "libvndk",
688 vendor_available: true,
689 vndk: {
690 enabled: true,
691 },
692 shared_libs: ["libvendor"], // Cause error
693 nocrt: true,
694 }
695
696 cc_library {
697 name: "libvendor",
698 vendor: true,
699 nocrt: true,
700 }
701 `)
702
703 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
704 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
705 cc_library {
706 name: "libvndk_sp",
707 vendor_available: true,
708 vndk: {
709 enabled: true,
710 support_system_process: true,
711 },
712 shared_libs: ["libfwk"], // Cause error
713 nocrt: true,
714 }
715
716 cc_library {
717 name: "libfwk",
718 nocrt: true,
719 }
720 `)
721
722 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
723 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
724 cc_library {
725 name: "libvndk_sp",
726 vendor_available: true,
727 vndk: {
728 enabled: true,
729 support_system_process: true,
730 },
731 shared_libs: ["libvendor"], // Cause error
732 nocrt: true,
733 }
734
735 cc_library {
736 name: "libvendor",
737 vendor: true,
738 nocrt: true,
739 }
740 `)
741
742 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
743 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
744 cc_library {
745 name: "libvndk_sp",
746 vendor_available: true,
747 vndk: {
748 enabled: true,
749 support_system_process: true,
750 },
751 shared_libs: ["libvndk"], // Cause error
752 nocrt: true,
753 }
754
755 cc_library {
756 name: "libvndk",
757 vendor_available: true,
758 vndk: {
759 enabled: true,
760 },
761 nocrt: true,
762 }
763 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900764
765 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
766 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
767 cc_library {
768 name: "libvndk",
769 vendor_available: true,
770 vndk: {
771 enabled: true,
772 },
773 shared_libs: ["libnonvndk"],
774 nocrt: true,
775 }
776
777 cc_library {
778 name: "libnonvndk",
779 vendor_available: true,
780 nocrt: true,
781 }
782 `)
783
784 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
785 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
786 cc_library {
787 name: "libvndkprivate",
788 vendor_available: false,
789 vndk: {
790 enabled: true,
791 },
792 shared_libs: ["libnonvndk"],
793 nocrt: true,
794 }
795
796 cc_library {
797 name: "libnonvndk",
798 vendor_available: true,
799 nocrt: true,
800 }
801 `)
802
803 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
804 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
805 cc_library {
806 name: "libvndksp",
807 vendor_available: true,
808 vndk: {
809 enabled: true,
810 support_system_process: true,
811 },
812 shared_libs: ["libnonvndk"],
813 nocrt: true,
814 }
815
816 cc_library {
817 name: "libnonvndk",
818 vendor_available: true,
819 nocrt: true,
820 }
821 `)
822
823 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
824 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
825 cc_library {
826 name: "libvndkspprivate",
827 vendor_available: false,
828 vndk: {
829 enabled: true,
830 support_system_process: true,
831 },
832 shared_libs: ["libnonvndk"],
833 nocrt: true,
834 }
835
836 cc_library {
837 name: "libnonvndk",
838 vendor_available: true,
839 nocrt: true,
840 }
841 `)
842}
843
844func TestDoubleLoadbleDep(t *testing.T) {
845 // okay to link : LLNDK -> double_loadable VNDK
846 testCc(t, `
847 cc_library {
848 name: "libllndk",
849 shared_libs: ["libdoubleloadable"],
850 }
851
852 llndk_library {
853 name: "libllndk",
854 symbol_file: "",
855 }
856
857 cc_library {
858 name: "libdoubleloadable",
859 vendor_available: true,
860 vndk: {
861 enabled: true,
862 },
863 double_loadable: true,
864 }
865 `)
866 // okay to link : LLNDK -> VNDK-SP
867 testCc(t, `
868 cc_library {
869 name: "libllndk",
870 shared_libs: ["libvndksp"],
871 }
872
873 llndk_library {
874 name: "libllndk",
875 symbol_file: "",
876 }
877
878 cc_library {
879 name: "libvndksp",
880 vendor_available: true,
881 vndk: {
882 enabled: true,
883 support_system_process: true,
884 },
885 }
886 `)
887 // okay to link : double_loadable -> double_loadable
888 testCc(t, `
889 cc_library {
890 name: "libdoubleloadable1",
891 shared_libs: ["libdoubleloadable2"],
892 vendor_available: true,
893 double_loadable: true,
894 }
895
896 cc_library {
897 name: "libdoubleloadable2",
898 vendor_available: true,
899 double_loadable: true,
900 }
901 `)
902 // okay to link : double_loadable VNDK -> double_loadable VNDK private
903 testCc(t, `
904 cc_library {
905 name: "libdoubleloadable",
906 vendor_available: true,
907 vndk: {
908 enabled: true,
909 },
910 double_loadable: true,
911 shared_libs: ["libnondoubleloadable"],
912 }
913
914 cc_library {
915 name: "libnondoubleloadable",
916 vendor_available: false,
917 vndk: {
918 enabled: true,
919 },
920 double_loadable: true,
921 }
922 `)
923 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
924 testCc(t, `
925 cc_library {
926 name: "libllndk",
927 shared_libs: ["libcoreonly"],
928 }
929
930 llndk_library {
931 name: "libllndk",
932 symbol_file: "",
933 }
934
935 cc_library {
936 name: "libcoreonly",
937 shared_libs: ["libvendoravailable"],
938 }
939
940 // indirect dependency of LLNDK
941 cc_library {
942 name: "libvendoravailable",
943 vendor_available: true,
944 double_loadable: true,
945 }
946 `)
947}
948
Inseob Kim8471cda2019-11-15 09:59:12 +0900949func TestVendorSnapshot(t *testing.T) {
950 bp := `
951 cc_library {
952 name: "libvndk",
953 vendor_available: true,
954 vndk: {
955 enabled: true,
956 },
957 nocrt: true,
958 }
959
960 cc_library {
961 name: "libvendor",
962 vendor: true,
963 nocrt: true,
964 }
965
966 cc_library {
967 name: "libvendor_available",
968 vendor_available: true,
969 nocrt: true,
970 }
971
972 cc_library_headers {
973 name: "libvendor_headers",
974 vendor_available: true,
975 nocrt: true,
976 }
977
978 cc_binary {
979 name: "vendor_bin",
980 vendor: true,
981 nocrt: true,
982 }
983
984 cc_binary {
985 name: "vendor_available_bin",
986 vendor_available: true,
987 nocrt: true,
988 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900989
990 toolchain_library {
991 name: "libb",
992 vendor_available: true,
993 src: "libb.a",
994 }
Inseob Kim1042d292020-06-01 23:23:05 +0900995
996 cc_object {
997 name: "obj",
998 vendor_available: true,
999 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001000`
1001 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1002 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1003 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1004 ctx := testCcWithConfig(t, config)
1005
1006 // Check Vendor snapshot output.
1007
1008 snapshotDir := "vendor-snapshot"
1009 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +09001010 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1011
1012 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +09001013
1014 for _, arch := range [][]string{
1015 []string{"arm64", "armv8-a"},
1016 []string{"arm", "armv7-a-neon"},
1017 } {
1018 archType := arch[0]
1019 archVariant := arch[1]
1020 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1021
1022 // For shared libraries, only non-VNDK vendor_available modules are captured
1023 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1024 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +09001025 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1026 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
1027 jsonFiles = append(jsonFiles,
1028 filepath.Join(sharedDir, "libvendor.so.json"),
1029 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001030
1031 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
Inseob Kimc42f2f22020-07-29 20:32:10 +09001032 // Also cfi variants are captured, except for prebuilts like toolchain_library
Inseob Kim8471cda2019-11-15 09:59:12 +09001033 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001034 staticCfiVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static_cfi", archType, archVariant)
Inseob Kim8471cda2019-11-15 09:59:12 +09001035 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +09001036 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1037 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001038 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001039 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001040 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001041 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001042 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001043 jsonFiles = append(jsonFiles,
1044 filepath.Join(staticDir, "libb.a.json"),
1045 filepath.Join(staticDir, "libvndk.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001046 filepath.Join(staticDir, "libvndk.cfi.a.json"),
Inseob Kim7f283f42020-06-01 21:53:49 +09001047 filepath.Join(staticDir, "libvendor.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001048 filepath.Join(staticDir, "libvendor.cfi.a.json"),
1049 filepath.Join(staticDir, "libvendor_available.a.json"),
1050 filepath.Join(staticDir, "libvendor_available.cfi.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001051
Inseob Kim7f283f42020-06-01 21:53:49 +09001052 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +09001053 if archType == "arm64" {
1054 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1055 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +09001056 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
1057 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
1058 jsonFiles = append(jsonFiles,
1059 filepath.Join(binaryDir, "vendor_bin.json"),
1060 filepath.Join(binaryDir, "vendor_available_bin.json"))
1061 }
1062
1063 // For header libraries, all vendor:true and vendor_available modules are captured.
1064 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1065 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +09001066
1067 // For object modules, all vendor:true and vendor_available modules are captured.
1068 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1069 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1070 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1071 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001072 }
1073
1074 for _, jsonFile := range jsonFiles {
1075 // verify all json files exist
1076 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1077 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001078 }
1079 }
1080}
1081
Inseob Kimc42f2f22020-07-29 20:32:10 +09001082func TestVendorSnapshotSanitizer(t *testing.T) {
1083 bp := `
1084 vendor_snapshot_static {
1085 name: "libsnapshot",
1086 vendor: true,
1087 target_arch: "arm64",
1088 version: "BOARD",
1089 arch: {
1090 arm64: {
1091 src: "libsnapshot.a",
1092 cfi: {
1093 src: "libsnapshot.cfi.a",
1094 }
1095 },
1096 },
1097 }
1098`
1099 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1100 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1101 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1102 ctx := testCcWithConfig(t, config)
1103
1104 // Check non-cfi and cfi variant.
1105 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1106 staticCfiVariant := "android_vendor.BOARD_arm64_armv8-a_static_cfi"
1107
1108 staticModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticVariant).Module().(*Module)
1109 assertString(t, staticModule.outputFile.Path().Base(), "libsnapshot.a")
1110
1111 staticCfiModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticCfiVariant).Module().(*Module)
1112 assertString(t, staticCfiModule.outputFile.Path().Base(), "libsnapshot.cfi.a")
1113}
1114
Bill Peckham945441c2020-08-31 16:07:58 -07001115func assertExcludeFromVendorSnapshotIs(t *testing.T, c *Module, expected bool) {
1116 t.Helper()
1117 if c.ExcludeFromVendorSnapshot() != expected {
1118 t.Errorf("expected %q ExcludeFromVendorSnapshot to be %t", c.String(), expected)
1119 }
1120}
1121
1122func TestVendorSnapshotExclude(t *testing.T) {
1123
1124 // This test verifies that the exclude_from_vendor_snapshot property
1125 // makes its way from the Android.bp source file into the module data
1126 // structure. It also verifies that modules are correctly included or
1127 // excluded in the vendor snapshot based on their path (framework or
1128 // vendor) and the exclude_from_vendor_snapshot property.
1129
1130 frameworkBp := `
1131 cc_library_shared {
1132 name: "libinclude",
1133 srcs: ["src/include.cpp"],
1134 vendor_available: true,
1135 }
1136 cc_library_shared {
1137 name: "libexclude",
1138 srcs: ["src/exclude.cpp"],
1139 vendor: true,
1140 exclude_from_vendor_snapshot: true,
1141 }
1142 `
1143
1144 vendorProprietaryBp := `
1145 cc_library_shared {
1146 name: "libvendor",
1147 srcs: ["vendor.cpp"],
1148 vendor: true,
1149 }
1150 `
1151
1152 depsBp := GatherRequiredDepsForTest(android.Android)
1153
1154 mockFS := map[string][]byte{
1155 "deps/Android.bp": []byte(depsBp),
1156 "framework/Android.bp": []byte(frameworkBp),
1157 "framework/include.cpp": nil,
1158 "framework/exclude.cpp": nil,
1159 "device/Android.bp": []byte(vendorProprietaryBp),
1160 "device/vendor.cpp": nil,
1161 }
1162
1163 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1164 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1165 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1166 ctx := CreateTestContext()
1167 ctx.Register(config)
1168
1169 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1170 android.FailIfErrored(t, errs)
1171 _, errs = ctx.PrepareBuildActions(config)
1172 android.FailIfErrored(t, errs)
1173
1174 // Test an include and exclude framework module.
1175 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1176 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", vendorVariant).Module().(*Module), false)
1177 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libexclude", vendorVariant).Module().(*Module), true)
1178
1179 // A vendor module is excluded, but by its path, not the
1180 // exclude_from_vendor_snapshot property.
1181 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libvendor", vendorVariant).Module().(*Module), false)
1182
1183 // Verify the content of the vendor snapshot.
1184
1185 snapshotDir := "vendor-snapshot"
1186 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1187 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1188
1189 var includeJsonFiles []string
1190 var excludeJsonFiles []string
1191
1192 for _, arch := range [][]string{
1193 []string{"arm64", "armv8-a"},
1194 []string{"arm", "armv7-a-neon"},
1195 } {
1196 archType := arch[0]
1197 archVariant := arch[1]
1198 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1199
1200 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1201 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1202
1203 // Included modules
1204 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1205 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1206
1207 // Excluded modules
1208 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1209 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1210 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1211 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1212 }
1213
1214 // Verify that each json file for an included module has a rule.
1215 for _, jsonFile := range includeJsonFiles {
1216 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1217 t.Errorf("include json file %q not found", jsonFile)
1218 }
1219 }
1220
1221 // Verify that each json file for an excluded module has no rule.
1222 for _, jsonFile := range excludeJsonFiles {
1223 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1224 t.Errorf("exclude json file %q found", jsonFile)
1225 }
1226 }
1227}
1228
1229func TestVendorSnapshotExcludeInVendorProprietaryPathErrors(t *testing.T) {
1230
1231 // This test verifies that using the exclude_from_vendor_snapshot
1232 // property on a module in a vendor proprietary path generates an
1233 // error. These modules are already excluded, so we prohibit using the
1234 // property in this way, which could add to confusion.
1235
1236 vendorProprietaryBp := `
1237 cc_library_shared {
1238 name: "libvendor",
1239 srcs: ["vendor.cpp"],
1240 vendor: true,
1241 exclude_from_vendor_snapshot: true,
1242 }
1243 `
1244
1245 depsBp := GatherRequiredDepsForTest(android.Android)
1246
1247 mockFS := map[string][]byte{
1248 "deps/Android.bp": []byte(depsBp),
1249 "device/Android.bp": []byte(vendorProprietaryBp),
1250 "device/vendor.cpp": nil,
1251 }
1252
1253 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1254 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1255 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1256 ctx := CreateTestContext()
1257 ctx.Register(config)
1258
1259 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "device/Android.bp"})
1260 android.FailIfErrored(t, errs)
1261
1262 _, errs = ctx.PrepareBuildActions(config)
1263 android.CheckErrorsAgainstExpectations(t, errs, []string{
1264 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1265 `module "libvendor\{.+,image:vendor.+,arch:arm_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1266 })
1267}
1268
1269func TestVendorSnapshotExcludeWithVendorAvailable(t *testing.T) {
1270
1271 // This test verifies that using the exclude_from_vendor_snapshot
1272 // property on a module that is vendor available generates an error. A
1273 // vendor available module must be captured in the vendor snapshot and
1274 // must not built from source when building the vendor image against
1275 // the vendor snapshot.
1276
1277 frameworkBp := `
1278 cc_library_shared {
1279 name: "libinclude",
1280 srcs: ["src/include.cpp"],
1281 vendor_available: true,
1282 exclude_from_vendor_snapshot: true,
1283 }
1284 `
1285
1286 depsBp := GatherRequiredDepsForTest(android.Android)
1287
1288 mockFS := map[string][]byte{
1289 "deps/Android.bp": []byte(depsBp),
1290 "framework/Android.bp": []byte(frameworkBp),
1291 "framework/include.cpp": nil,
1292 }
1293
1294 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1295 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1296 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1297 ctx := CreateTestContext()
1298 ctx.Register(config)
1299
1300 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp"})
1301 android.FailIfErrored(t, errs)
1302
1303 _, errs = ctx.PrepareBuildActions(config)
1304 android.CheckErrorsAgainstExpectations(t, errs, []string{
1305 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1306 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1307 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1308 `module "libinclude\{.+,image:vendor.+,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1309 })
1310}
1311
Jooyung Hana70f0672019-01-18 15:20:43 +09001312func TestDoubleLoadableDepError(t *testing.T) {
1313 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1314 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1315 cc_library {
1316 name: "libllndk",
1317 shared_libs: ["libnondoubleloadable"],
1318 }
1319
1320 llndk_library {
1321 name: "libllndk",
1322 symbol_file: "",
1323 }
1324
1325 cc_library {
1326 name: "libnondoubleloadable",
1327 vendor_available: true,
1328 vndk: {
1329 enabled: true,
1330 },
1331 }
1332 `)
1333
1334 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1335 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1336 cc_library {
1337 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001338 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001339 shared_libs: ["libnondoubleloadable"],
1340 }
1341
1342 llndk_library {
1343 name: "libllndk",
1344 symbol_file: "",
1345 }
1346
1347 cc_library {
1348 name: "libnondoubleloadable",
1349 vendor_available: true,
1350 }
1351 `)
1352
1353 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
1354 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1355 cc_library {
1356 name: "libdoubleloadable",
1357 vendor_available: true,
1358 double_loadable: true,
1359 shared_libs: ["libnondoubleloadable"],
1360 }
1361
1362 cc_library {
1363 name: "libnondoubleloadable",
1364 vendor_available: true,
1365 }
1366 `)
1367
1368 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
1369 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1370 cc_library {
1371 name: "libdoubleloadable",
1372 vendor_available: true,
1373 double_loadable: true,
1374 shared_libs: ["libnondoubleloadable"],
1375 }
1376
1377 cc_library {
1378 name: "libnondoubleloadable",
1379 vendor_available: true,
1380 vndk: {
1381 enabled: true,
1382 },
1383 }
1384 `)
1385
1386 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
1387 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1388 cc_library {
1389 name: "libdoubleloadable",
1390 vendor_available: true,
1391 vndk: {
1392 enabled: true,
1393 },
1394 double_loadable: true,
1395 shared_libs: ["libnondoubleloadable"],
1396 }
1397
1398 cc_library {
1399 name: "libnondoubleloadable",
1400 vendor_available: false,
1401 vndk: {
1402 enabled: true,
1403 },
1404 }
1405 `)
1406
1407 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1408 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1409 cc_library {
1410 name: "libllndk",
1411 shared_libs: ["libcoreonly"],
1412 }
1413
1414 llndk_library {
1415 name: "libllndk",
1416 symbol_file: "",
1417 }
1418
1419 cc_library {
1420 name: "libcoreonly",
1421 shared_libs: ["libvendoravailable"],
1422 }
1423
1424 // indirect dependency of LLNDK
1425 cc_library {
1426 name: "libvendoravailable",
1427 vendor_available: true,
1428 }
1429 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001430}
1431
Logan Chienf3511742017-10-31 18:04:35 +08001432func TestVndkExt(t *testing.T) {
1433 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001434 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001435 cc_library {
1436 name: "libvndk",
1437 vendor_available: true,
1438 vndk: {
1439 enabled: true,
1440 },
1441 nocrt: true,
1442 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001443 cc_library {
1444 name: "libvndk2",
1445 vendor_available: true,
1446 vndk: {
1447 enabled: true,
1448 },
1449 target: {
1450 vendor: {
1451 suffix: "-suffix",
1452 },
1453 },
1454 nocrt: true,
1455 }
Logan Chienf3511742017-10-31 18:04:35 +08001456
1457 cc_library {
1458 name: "libvndk_ext",
1459 vendor: true,
1460 vndk: {
1461 enabled: true,
1462 extends: "libvndk",
1463 },
1464 nocrt: true,
1465 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001466
1467 cc_library {
1468 name: "libvndk2_ext",
1469 vendor: true,
1470 vndk: {
1471 enabled: true,
1472 extends: "libvndk2",
1473 },
1474 nocrt: true,
1475 }
Logan Chienf3511742017-10-31 18:04:35 +08001476
Justin Yun0ecf0b22020-02-28 15:07:59 +09001477 cc_library {
1478 name: "libvndk_ext_product",
1479 product_specific: true,
1480 vndk: {
1481 enabled: true,
1482 extends: "libvndk",
1483 },
1484 nocrt: true,
1485 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001486
Justin Yun0ecf0b22020-02-28 15:07:59 +09001487 cc_library {
1488 name: "libvndk2_ext_product",
1489 product_specific: true,
1490 vndk: {
1491 enabled: true,
1492 extends: "libvndk2",
1493 },
1494 nocrt: true,
1495 }
1496 `
1497 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1498 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1499 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1500 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1501
1502 ctx := testCcWithConfig(t, config)
1503
1504 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1505 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1506
1507 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1508 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1509
1510 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1511 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001512}
1513
Logan Chiend3c59a22018-03-29 14:08:15 +08001514func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001515 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1516 ctx := testCcNoVndk(t, `
1517 cc_library {
1518 name: "libvndk",
1519 vendor_available: true,
1520 vndk: {
1521 enabled: true,
1522 },
1523 nocrt: true,
1524 }
1525
1526 cc_library {
1527 name: "libvndk_ext",
1528 vendor: true,
1529 vndk: {
1530 enabled: true,
1531 extends: "libvndk",
1532 },
1533 nocrt: true,
1534 }
1535 `)
1536
1537 // Ensures that the core variant of "libvndk_ext" can be found.
1538 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1539 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1540 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1541 }
1542}
1543
Justin Yun0ecf0b22020-02-28 15:07:59 +09001544func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1545 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
1546 ctx := testCc(t, `
1547 cc_library {
1548 name: "libvndk",
1549 vendor_available: true,
1550 vndk: {
1551 enabled: true,
1552 },
1553 nocrt: true,
1554 }
1555
1556 cc_library {
1557 name: "libvndk_ext_product",
1558 product_specific: true,
1559 vndk: {
1560 enabled: true,
1561 extends: "libvndk",
1562 },
1563 nocrt: true,
1564 }
1565 `)
1566
1567 // Ensures that the core variant of "libvndk_ext_product" can be found.
1568 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1569 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1570 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1571 }
1572}
1573
Logan Chienf3511742017-10-31 18:04:35 +08001574func TestVndkExtError(t *testing.T) {
1575 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001576 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001577 cc_library {
1578 name: "libvndk",
1579 vendor_available: true,
1580 vndk: {
1581 enabled: true,
1582 },
1583 nocrt: true,
1584 }
1585
1586 cc_library {
1587 name: "libvndk_ext",
1588 vndk: {
1589 enabled: true,
1590 extends: "libvndk",
1591 },
1592 nocrt: true,
1593 }
1594 `)
1595
1596 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1597 cc_library {
1598 name: "libvndk",
1599 vendor_available: true,
1600 vndk: {
1601 enabled: true,
1602 },
1603 nocrt: true,
1604 }
1605
1606 cc_library {
1607 name: "libvndk_ext",
1608 vendor: true,
1609 vndk: {
1610 enabled: true,
1611 },
1612 nocrt: true,
1613 }
1614 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001615
1616 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1617 cc_library {
1618 name: "libvndk",
1619 vendor_available: true,
1620 vndk: {
1621 enabled: true,
1622 },
1623 nocrt: true,
1624 }
1625
1626 cc_library {
1627 name: "libvndk_ext_product",
1628 product_specific: true,
1629 vndk: {
1630 enabled: true,
1631 },
1632 nocrt: true,
1633 }
1634 `)
1635
1636 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1637 cc_library {
1638 name: "libvndk",
1639 vendor_available: true,
1640 vndk: {
1641 enabled: true,
1642 },
1643 nocrt: true,
1644 }
1645
1646 cc_library {
1647 name: "libvndk_ext_product",
1648 product_specific: true,
1649 vendor_available: true,
1650 vndk: {
1651 enabled: true,
1652 extends: "libvndk",
1653 },
1654 nocrt: true,
1655 }
1656 `)
Logan Chienf3511742017-10-31 18:04:35 +08001657}
1658
1659func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1660 // This test ensures an error is emitted for inconsistent support_system_process.
1661 testCcError(t, "module \".*\" with mismatched support_system_process", `
1662 cc_library {
1663 name: "libvndk",
1664 vendor_available: true,
1665 vndk: {
1666 enabled: true,
1667 },
1668 nocrt: true,
1669 }
1670
1671 cc_library {
1672 name: "libvndk_sp_ext",
1673 vendor: true,
1674 vndk: {
1675 enabled: true,
1676 extends: "libvndk",
1677 support_system_process: true,
1678 },
1679 nocrt: true,
1680 }
1681 `)
1682
1683 testCcError(t, "module \".*\" with mismatched support_system_process", `
1684 cc_library {
1685 name: "libvndk_sp",
1686 vendor_available: true,
1687 vndk: {
1688 enabled: true,
1689 support_system_process: true,
1690 },
1691 nocrt: true,
1692 }
1693
1694 cc_library {
1695 name: "libvndk_ext",
1696 vendor: true,
1697 vndk: {
1698 enabled: true,
1699 extends: "libvndk_sp",
1700 },
1701 nocrt: true,
1702 }
1703 `)
1704}
1705
1706func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001707 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08001708 // with `vendor_available: false`.
1709 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1710 cc_library {
1711 name: "libvndk",
1712 vendor_available: false,
1713 vndk: {
1714 enabled: true,
1715 },
1716 nocrt: true,
1717 }
1718
1719 cc_library {
1720 name: "libvndk_ext",
1721 vendor: true,
1722 vndk: {
1723 enabled: true,
1724 extends: "libvndk",
1725 },
1726 nocrt: true,
1727 }
1728 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001729
1730 testCcErrorProductVndk(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1731 cc_library {
1732 name: "libvndk",
1733 vendor_available: false,
1734 vndk: {
1735 enabled: true,
1736 },
1737 nocrt: true,
1738 }
1739
1740 cc_library {
1741 name: "libvndk_ext_product",
1742 product_specific: true,
1743 vndk: {
1744 enabled: true,
1745 extends: "libvndk",
1746 },
1747 nocrt: true,
1748 }
1749 `)
Logan Chienf3511742017-10-31 18:04:35 +08001750}
1751
Logan Chiend3c59a22018-03-29 14:08:15 +08001752func TestVendorModuleUseVndkExt(t *testing.T) {
1753 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001754 testCc(t, `
1755 cc_library {
1756 name: "libvndk",
1757 vendor_available: true,
1758 vndk: {
1759 enabled: true,
1760 },
1761 nocrt: true,
1762 }
1763
1764 cc_library {
1765 name: "libvndk_ext",
1766 vendor: true,
1767 vndk: {
1768 enabled: true,
1769 extends: "libvndk",
1770 },
1771 nocrt: true,
1772 }
1773
1774 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001775 name: "libvndk_sp",
1776 vendor_available: true,
1777 vndk: {
1778 enabled: true,
1779 support_system_process: true,
1780 },
1781 nocrt: true,
1782 }
1783
1784 cc_library {
1785 name: "libvndk_sp_ext",
1786 vendor: true,
1787 vndk: {
1788 enabled: true,
1789 extends: "libvndk_sp",
1790 support_system_process: true,
1791 },
1792 nocrt: true,
1793 }
1794
1795 cc_library {
1796 name: "libvendor",
1797 vendor: true,
1798 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1799 nocrt: true,
1800 }
1801 `)
1802}
1803
Logan Chiend3c59a22018-03-29 14:08:15 +08001804func TestVndkExtUseVendorLib(t *testing.T) {
1805 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001806 testCc(t, `
1807 cc_library {
1808 name: "libvndk",
1809 vendor_available: true,
1810 vndk: {
1811 enabled: true,
1812 },
1813 nocrt: true,
1814 }
1815
1816 cc_library {
1817 name: "libvndk_ext",
1818 vendor: true,
1819 vndk: {
1820 enabled: true,
1821 extends: "libvndk",
1822 },
1823 shared_libs: ["libvendor"],
1824 nocrt: true,
1825 }
1826
1827 cc_library {
1828 name: "libvendor",
1829 vendor: true,
1830 nocrt: true,
1831 }
1832 `)
Logan Chienf3511742017-10-31 18:04:35 +08001833
Logan Chiend3c59a22018-03-29 14:08:15 +08001834 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1835 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001836 cc_library {
1837 name: "libvndk_sp",
1838 vendor_available: true,
1839 vndk: {
1840 enabled: true,
1841 support_system_process: true,
1842 },
1843 nocrt: true,
1844 }
1845
1846 cc_library {
1847 name: "libvndk_sp_ext",
1848 vendor: true,
1849 vndk: {
1850 enabled: true,
1851 extends: "libvndk_sp",
1852 support_system_process: true,
1853 },
1854 shared_libs: ["libvendor"], // Cause an error
1855 nocrt: true,
1856 }
1857
1858 cc_library {
1859 name: "libvendor",
1860 vendor: true,
1861 nocrt: true,
1862 }
1863 `)
1864}
1865
Justin Yun0ecf0b22020-02-28 15:07:59 +09001866func TestProductVndkExtDependency(t *testing.T) {
1867 bp := `
1868 cc_library {
1869 name: "libvndk",
1870 vendor_available: true,
1871 vndk: {
1872 enabled: true,
1873 },
1874 nocrt: true,
1875 }
1876
1877 cc_library {
1878 name: "libvndk_ext_product",
1879 product_specific: true,
1880 vndk: {
1881 enabled: true,
1882 extends: "libvndk",
1883 },
1884 shared_libs: ["libproduct_for_vndklibs"],
1885 nocrt: true,
1886 }
1887
1888 cc_library {
1889 name: "libvndk_sp",
1890 vendor_available: true,
1891 vndk: {
1892 enabled: true,
1893 support_system_process: true,
1894 },
1895 nocrt: true,
1896 }
1897
1898 cc_library {
1899 name: "libvndk_sp_ext_product",
1900 product_specific: true,
1901 vndk: {
1902 enabled: true,
1903 extends: "libvndk_sp",
1904 support_system_process: true,
1905 },
1906 shared_libs: ["libproduct_for_vndklibs"],
1907 nocrt: true,
1908 }
1909
1910 cc_library {
1911 name: "libproduct",
1912 product_specific: true,
1913 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1914 nocrt: true,
1915 }
1916
1917 cc_library {
1918 name: "libproduct_for_vndklibs",
1919 product_specific: true,
1920 nocrt: true,
1921 }
1922 `
1923 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1924 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1925 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1926 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1927
1928 testCcWithConfig(t, config)
1929}
1930
Logan Chiend3c59a22018-03-29 14:08:15 +08001931func TestVndkSpExtUseVndkError(t *testing.T) {
1932 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1933 // library.
1934 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1935 cc_library {
1936 name: "libvndk",
1937 vendor_available: true,
1938 vndk: {
1939 enabled: true,
1940 },
1941 nocrt: true,
1942 }
1943
1944 cc_library {
1945 name: "libvndk_sp",
1946 vendor_available: true,
1947 vndk: {
1948 enabled: true,
1949 support_system_process: true,
1950 },
1951 nocrt: true,
1952 }
1953
1954 cc_library {
1955 name: "libvndk_sp_ext",
1956 vendor: true,
1957 vndk: {
1958 enabled: true,
1959 extends: "libvndk_sp",
1960 support_system_process: true,
1961 },
1962 shared_libs: ["libvndk"], // Cause an error
1963 nocrt: true,
1964 }
1965 `)
1966
1967 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1968 // library.
1969 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1970 cc_library {
1971 name: "libvndk",
1972 vendor_available: true,
1973 vndk: {
1974 enabled: true,
1975 },
1976 nocrt: true,
1977 }
1978
1979 cc_library {
1980 name: "libvndk_ext",
1981 vendor: true,
1982 vndk: {
1983 enabled: true,
1984 extends: "libvndk",
1985 },
1986 nocrt: true,
1987 }
1988
1989 cc_library {
1990 name: "libvndk_sp",
1991 vendor_available: true,
1992 vndk: {
1993 enabled: true,
1994 support_system_process: true,
1995 },
1996 nocrt: true,
1997 }
1998
1999 cc_library {
2000 name: "libvndk_sp_ext",
2001 vendor: true,
2002 vndk: {
2003 enabled: true,
2004 extends: "libvndk_sp",
2005 support_system_process: true,
2006 },
2007 shared_libs: ["libvndk_ext"], // Cause an error
2008 nocrt: true,
2009 }
2010 `)
2011}
2012
2013func TestVndkUseVndkExtError(t *testing.T) {
2014 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2015 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002016 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2017 cc_library {
2018 name: "libvndk",
2019 vendor_available: true,
2020 vndk: {
2021 enabled: true,
2022 },
2023 nocrt: true,
2024 }
2025
2026 cc_library {
2027 name: "libvndk_ext",
2028 vendor: true,
2029 vndk: {
2030 enabled: true,
2031 extends: "libvndk",
2032 },
2033 nocrt: true,
2034 }
2035
2036 cc_library {
2037 name: "libvndk2",
2038 vendor_available: true,
2039 vndk: {
2040 enabled: true,
2041 },
2042 shared_libs: ["libvndk_ext"],
2043 nocrt: true,
2044 }
2045 `)
2046
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002047 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002048 cc_library {
2049 name: "libvndk",
2050 vendor_available: true,
2051 vndk: {
2052 enabled: true,
2053 },
2054 nocrt: true,
2055 }
2056
2057 cc_library {
2058 name: "libvndk_ext",
2059 vendor: true,
2060 vndk: {
2061 enabled: true,
2062 extends: "libvndk",
2063 },
2064 nocrt: true,
2065 }
2066
2067 cc_library {
2068 name: "libvndk2",
2069 vendor_available: true,
2070 vndk: {
2071 enabled: true,
2072 },
2073 target: {
2074 vendor: {
2075 shared_libs: ["libvndk_ext"],
2076 },
2077 },
2078 nocrt: true,
2079 }
2080 `)
2081
2082 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2083 cc_library {
2084 name: "libvndk_sp",
2085 vendor_available: true,
2086 vndk: {
2087 enabled: true,
2088 support_system_process: true,
2089 },
2090 nocrt: true,
2091 }
2092
2093 cc_library {
2094 name: "libvndk_sp_ext",
2095 vendor: true,
2096 vndk: {
2097 enabled: true,
2098 extends: "libvndk_sp",
2099 support_system_process: true,
2100 },
2101 nocrt: true,
2102 }
2103
2104 cc_library {
2105 name: "libvndk_sp_2",
2106 vendor_available: true,
2107 vndk: {
2108 enabled: true,
2109 support_system_process: true,
2110 },
2111 shared_libs: ["libvndk_sp_ext"],
2112 nocrt: true,
2113 }
2114 `)
2115
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002116 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002117 cc_library {
2118 name: "libvndk_sp",
2119 vendor_available: true,
2120 vndk: {
2121 enabled: true,
2122 },
2123 nocrt: true,
2124 }
2125
2126 cc_library {
2127 name: "libvndk_sp_ext",
2128 vendor: true,
2129 vndk: {
2130 enabled: true,
2131 extends: "libvndk_sp",
2132 },
2133 nocrt: true,
2134 }
2135
2136 cc_library {
2137 name: "libvndk_sp2",
2138 vendor_available: true,
2139 vndk: {
2140 enabled: true,
2141 },
2142 target: {
2143 vendor: {
2144 shared_libs: ["libvndk_sp_ext"],
2145 },
2146 },
2147 nocrt: true,
2148 }
2149 `)
2150}
2151
Justin Yun5f7f7e82019-11-18 19:52:14 +09002152func TestEnforceProductVndkVersion(t *testing.T) {
2153 bp := `
2154 cc_library {
2155 name: "libllndk",
2156 }
2157 llndk_library {
2158 name: "libllndk",
2159 symbol_file: "",
2160 }
2161 cc_library {
2162 name: "libvndk",
2163 vendor_available: true,
2164 vndk: {
2165 enabled: true,
2166 },
2167 nocrt: true,
2168 }
2169 cc_library {
2170 name: "libvndk_sp",
2171 vendor_available: true,
2172 vndk: {
2173 enabled: true,
2174 support_system_process: true,
2175 },
2176 nocrt: true,
2177 }
2178 cc_library {
2179 name: "libva",
2180 vendor_available: true,
2181 nocrt: true,
2182 }
2183 cc_library {
2184 name: "libproduct_va",
2185 product_specific: true,
2186 vendor_available: true,
2187 nocrt: true,
2188 }
2189 cc_library {
2190 name: "libprod",
2191 product_specific: true,
2192 shared_libs: [
2193 "libllndk",
2194 "libvndk",
2195 "libvndk_sp",
2196 "libva",
2197 "libproduct_va",
2198 ],
2199 nocrt: true,
2200 }
2201 cc_library {
2202 name: "libvendor",
2203 vendor: true,
2204 shared_libs: [
2205 "libllndk",
2206 "libvndk",
2207 "libvndk_sp",
2208 "libva",
2209 "libproduct_va",
2210 ],
2211 nocrt: true,
2212 }
2213 `
2214
2215 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2216 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2217 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2218 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2219
2220 ctx := testCcWithConfig(t, config)
2221
Justin Yun0ecf0b22020-02-28 15:07:59 +09002222 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "", productVariant)
2223 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "", productVariant)
Justin Yun5f7f7e82019-11-18 19:52:14 +09002224}
2225
2226func TestEnforceProductVndkVersionErrors(t *testing.T) {
2227 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2228 cc_library {
2229 name: "libprod",
2230 product_specific: true,
2231 shared_libs: [
2232 "libvendor",
2233 ],
2234 nocrt: true,
2235 }
2236 cc_library {
2237 name: "libvendor",
2238 vendor: true,
2239 nocrt: true,
2240 }
2241 `)
2242 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2243 cc_library {
2244 name: "libprod",
2245 product_specific: true,
2246 shared_libs: [
2247 "libsystem",
2248 ],
2249 nocrt: true,
2250 }
2251 cc_library {
2252 name: "libsystem",
2253 nocrt: true,
2254 }
2255 `)
2256 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
2257 cc_library {
2258 name: "libprod",
2259 product_specific: true,
2260 shared_libs: [
2261 "libvndk_private",
2262 ],
2263 nocrt: true,
2264 }
2265 cc_library {
2266 name: "libvndk_private",
2267 vendor_available: false,
2268 vndk: {
2269 enabled: true,
2270 },
2271 nocrt: true,
2272 }
2273 `)
2274 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2275 cc_library {
2276 name: "libprod",
2277 product_specific: true,
2278 shared_libs: [
2279 "libsystem_ext",
2280 ],
2281 nocrt: true,
2282 }
2283 cc_library {
2284 name: "libsystem_ext",
2285 system_ext_specific: true,
2286 nocrt: true,
2287 }
2288 `)
2289 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2290 cc_library {
2291 name: "libsystem",
2292 shared_libs: [
2293 "libproduct_va",
2294 ],
2295 nocrt: true,
2296 }
2297 cc_library {
2298 name: "libproduct_va",
2299 product_specific: true,
2300 vendor_available: true,
2301 nocrt: true,
2302 }
2303 `)
2304}
2305
Jooyung Han38002912019-05-16 04:01:54 +09002306func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002307 bp := `
2308 cc_library {
2309 name: "libvndk",
2310 vendor_available: true,
2311 vndk: {
2312 enabled: true,
2313 },
2314 }
2315 cc_library {
2316 name: "libvndksp",
2317 vendor_available: true,
2318 vndk: {
2319 enabled: true,
2320 support_system_process: true,
2321 },
2322 }
2323 cc_library {
2324 name: "libvndkprivate",
2325 vendor_available: false,
2326 vndk: {
2327 enabled: true,
2328 },
2329 }
2330 cc_library {
2331 name: "libvendor",
2332 vendor: true,
2333 }
2334 cc_library {
2335 name: "libvndkext",
2336 vendor: true,
2337 vndk: {
2338 enabled: true,
2339 extends: "libvndk",
2340 },
2341 }
2342 vndk_prebuilt_shared {
2343 name: "prevndk",
2344 version: "27",
2345 target_arch: "arm",
2346 binder32bit: true,
2347 vendor_available: true,
2348 vndk: {
2349 enabled: true,
2350 },
2351 arch: {
2352 arm: {
2353 srcs: ["liba.so"],
2354 },
2355 },
2356 }
2357 cc_library {
2358 name: "libllndk",
2359 }
2360 llndk_library {
2361 name: "libllndk",
2362 symbol_file: "",
2363 }
2364 cc_library {
2365 name: "libllndkprivate",
2366 }
2367 llndk_library {
2368 name: "libllndkprivate",
2369 vendor_available: false,
2370 symbol_file: "",
2371 }`
2372
2373 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002374 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2375 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2376 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002377 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002378
Jooyung Han0302a842019-10-30 18:43:49 +09002379 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002380 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09002381 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09002382 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09002383 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002384 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09002385 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09002386 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09002387
Colin Crossfb0c16e2019-11-20 17:12:35 -08002388 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002389
Jooyung Han38002912019-05-16 04:01:54 +09002390 tests := []struct {
2391 variant string
2392 name string
2393 expected string
2394 }{
2395 {vendorVariant, "libvndk", "native:vndk"},
2396 {vendorVariant, "libvndksp", "native:vndk"},
2397 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2398 {vendorVariant, "libvendor", "native:vendor"},
2399 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09002400 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002401 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002402 {coreVariant, "libvndk", "native:platform"},
2403 {coreVariant, "libvndkprivate", "native:platform"},
2404 {coreVariant, "libllndk", "native:platform"},
2405 }
2406 for _, test := range tests {
2407 t.Run(test.name, func(t *testing.T) {
2408 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2409 assertString(t, module.makeLinkType, test.expected)
2410 })
2411 }
2412}
2413
Colin Cross0af4b842015-04-30 16:36:18 -07002414var (
2415 str11 = "01234567891"
2416 str10 = str11[:10]
2417 str9 = str11[:9]
2418 str5 = str11[:5]
2419 str4 = str11[:4]
2420)
2421
2422var splitListForSizeTestCases = []struct {
2423 in []string
2424 out [][]string
2425 size int
2426}{
2427 {
2428 in: []string{str10},
2429 out: [][]string{{str10}},
2430 size: 10,
2431 },
2432 {
2433 in: []string{str9},
2434 out: [][]string{{str9}},
2435 size: 10,
2436 },
2437 {
2438 in: []string{str5},
2439 out: [][]string{{str5}},
2440 size: 10,
2441 },
2442 {
2443 in: []string{str11},
2444 out: nil,
2445 size: 10,
2446 },
2447 {
2448 in: []string{str10, str10},
2449 out: [][]string{{str10}, {str10}},
2450 size: 10,
2451 },
2452 {
2453 in: []string{str9, str10},
2454 out: [][]string{{str9}, {str10}},
2455 size: 10,
2456 },
2457 {
2458 in: []string{str10, str9},
2459 out: [][]string{{str10}, {str9}},
2460 size: 10,
2461 },
2462 {
2463 in: []string{str5, str4},
2464 out: [][]string{{str5, str4}},
2465 size: 10,
2466 },
2467 {
2468 in: []string{str5, str4, str5},
2469 out: [][]string{{str5, str4}, {str5}},
2470 size: 10,
2471 },
2472 {
2473 in: []string{str5, str4, str5, str4},
2474 out: [][]string{{str5, str4}, {str5, str4}},
2475 size: 10,
2476 },
2477 {
2478 in: []string{str5, str4, str5, str5},
2479 out: [][]string{{str5, str4}, {str5}, {str5}},
2480 size: 10,
2481 },
2482 {
2483 in: []string{str5, str5, str5, str4},
2484 out: [][]string{{str5}, {str5}, {str5, str4}},
2485 size: 10,
2486 },
2487 {
2488 in: []string{str9, str11},
2489 out: nil,
2490 size: 10,
2491 },
2492 {
2493 in: []string{str11, str9},
2494 out: nil,
2495 size: 10,
2496 },
2497}
2498
2499func TestSplitListForSize(t *testing.T) {
2500 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08002501 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07002502
2503 var outStrings [][]string
2504
2505 if len(out) > 0 {
2506 outStrings = make([][]string, len(out))
2507 for i, o := range out {
2508 outStrings[i] = o.Strings()
2509 }
2510 }
2511
2512 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07002513 t.Errorf("incorrect output:")
2514 t.Errorf(" input: %#v", testCase.in)
2515 t.Errorf(" size: %d", testCase.size)
2516 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07002517 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07002518 }
2519 }
2520}
Jeff Gaston294356f2017-09-27 17:05:30 -07002521
2522var staticLinkDepOrderTestCases = []struct {
2523 // This is a string representation of a map[moduleName][]moduleDependency .
2524 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002525 inStatic string
2526
2527 // This is a string representation of a map[moduleName][]moduleDependency .
2528 // It models the dependencies declared in an Android.bp file.
2529 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002530
2531 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2532 // The keys of allOrdered specify which modules we would like to check.
2533 // The values of allOrdered specify the expected result (of the transitive closure of all
2534 // dependencies) for each module to test
2535 allOrdered string
2536
2537 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2538 // The keys of outOrdered specify which modules we would like to check.
2539 // The values of outOrdered specify the expected result (of the ordered linker command line)
2540 // for each module to test.
2541 outOrdered string
2542}{
2543 // Simple tests
2544 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002545 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002546 outOrdered: "",
2547 },
2548 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002549 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002550 outOrdered: "a:",
2551 },
2552 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002553 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002554 outOrdered: "a:b; b:",
2555 },
2556 // Tests of reordering
2557 {
2558 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002559 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002560 outOrdered: "a:b,c,d; b:d; c:d; d:",
2561 },
2562 {
2563 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002564 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002565 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2566 },
2567 {
2568 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002569 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002570 outOrdered: "a:d,b,e,c; d:b; e:c",
2571 },
2572 {
2573 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002574 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002575 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2576 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2577 },
2578 {
2579 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002580 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 -07002581 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2582 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2583 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002584 // shared dependencies
2585 {
2586 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2587 // So, we don't actually have to check that a shared dependency of c will change the order
2588 // of a library that depends statically on b and on c. We only need to check that if c has
2589 // a shared dependency on b, that that shows up in allOrdered.
2590 inShared: "c:b",
2591 allOrdered: "c:b",
2592 outOrdered: "c:",
2593 },
2594 {
2595 // This test doesn't actually include any shared dependencies but it's a reminder of what
2596 // the second phase of the above test would look like
2597 inStatic: "a:b,c; c:b",
2598 allOrdered: "a:c,b; c:b",
2599 outOrdered: "a:c,b; c:b",
2600 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002601 // tiebreakers for when two modules specifying different orderings and there is no dependency
2602 // to dictate an order
2603 {
2604 // 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 -08002605 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002606 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2607 },
2608 {
2609 // 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 -08002610 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 -07002611 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2612 },
2613 // Tests involving duplicate dependencies
2614 {
2615 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002616 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002617 outOrdered: "a:c,b",
2618 },
2619 {
2620 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002621 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002622 outOrdered: "a:d,c,b",
2623 },
2624 // Tests to confirm the nonexistence of infinite loops.
2625 // These cases should never happen, so as long as the test terminates and the
2626 // result is deterministic then that should be fine.
2627 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002628 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002629 outOrdered: "a:a",
2630 },
2631 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002632 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002633 allOrdered: "a:b,c; b:c,a; c:a,b",
2634 outOrdered: "a:b; b:c; c:a",
2635 },
2636 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002637 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002638 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2639 outOrdered: "a:c,b; b:a,c; c:b,a",
2640 },
2641}
2642
2643// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2644func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2645 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2646 strippedText := strings.Replace(text, " ", "", -1)
2647 if len(strippedText) < 1 {
2648 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2649 }
2650 allDeps = make(map[android.Path][]android.Path, 0)
2651
2652 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2653 moduleTexts := strings.Split(strippedText, ";")
2654
2655 outputForModuleName := func(moduleName string) android.Path {
2656 return android.PathForTesting(moduleName)
2657 }
2658
2659 for _, moduleText := range moduleTexts {
2660 // convert from "a:b,c" to ["a", "b,c"]
2661 components := strings.Split(moduleText, ":")
2662 if len(components) != 2 {
2663 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2664 }
2665 moduleName := components[0]
2666 moduleOutput := outputForModuleName(moduleName)
2667 modulesInOrder = append(modulesInOrder, moduleOutput)
2668
2669 depString := components[1]
2670 // convert from "b,c" to ["b", "c"]
2671 depNames := strings.Split(depString, ",")
2672 if len(depString) < 1 {
2673 depNames = []string{}
2674 }
2675 var deps []android.Path
2676 for _, depName := range depNames {
2677 deps = append(deps, outputForModuleName(depName))
2678 }
2679 allDeps[moduleOutput] = deps
2680 }
2681 return modulesInOrder, allDeps
2682}
2683
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002684func TestLinkReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002685 for _, testCase := range staticLinkDepOrderTestCases {
2686 errs := []string{}
2687
2688 // parse testcase
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002689 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)
Jeff Gaston294356f2017-09-27 17:05:30 -07002690 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)
2691 if testCase.allOrdered == "" {
2692 // allow the test case to skip specifying allOrdered
2693 testCase.allOrdered = testCase.outOrdered
2694 }
2695 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002696 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)
Jeff Gaston294356f2017-09-27 17:05:30 -07002697
2698 // For each module whose post-reordered dependencies were specified, validate that
2699 // reordering the inputs produces the expected outputs.
2700 for _, moduleName := range expectedModuleNames {
2701 moduleDeps := givenTransitiveDeps[moduleName]
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002702 givenSharedDeps := givenAllSharedDeps[moduleName]
2703 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -07002704
2705 correctAllOrdered := expectedAllDeps[moduleName]
2706 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {
2707 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002708 "\nin static:%q"+
2709 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002710 "\nmodule: %v"+
2711 "\nexpected: %s"+
2712 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002713 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002714 }
2715
2716 correctOutputDeps := expectedTransitiveDeps[moduleName]
2717 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {
2718 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002719 "\nin static:%q"+
2720 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002721 "\nmodule: %v"+
2722 "\nexpected: %s"+
2723 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002724 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002725 }
2726 }
2727
2728 if len(errs) > 0 {
2729 sort.Strings(errs)
2730 for _, err := range errs {
2731 t.Error(err)
2732 }
2733 }
2734 }
2735}
Logan Chienf3511742017-10-31 18:04:35 +08002736
Jeff Gaston294356f2017-09-27 17:05:30 -07002737func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2738 for _, moduleName := range moduleNames {
2739 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
2740 output := module.outputFile.Path()
2741 paths = append(paths, output)
2742 }
2743 return paths
2744}
2745
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002746func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002747 ctx := testCc(t, `
2748 cc_library {
2749 name: "a",
2750 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002751 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002752 }
2753 cc_library {
2754 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002755 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002756 }
2757 cc_library {
2758 name: "c",
2759 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002760 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002761 }
2762 cc_library {
2763 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002764 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002765 }
2766
2767 `)
2768
Colin Cross7113d202019-11-20 16:39:12 -08002769 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002770 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002771 actual := moduleA.depsInLinkOrder
Jeff Gaston294356f2017-09-27 17:05:30 -07002772 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})
2773
2774 if !reflect.DeepEqual(actual, expected) {
2775 t.Errorf("staticDeps orderings were not propagated correctly"+
2776 "\nactual: %v"+
2777 "\nexpected: %v",
2778 actual,
2779 expected,
2780 )
2781 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002782}
Jeff Gaston294356f2017-09-27 17:05:30 -07002783
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002784func TestStaticLibDepReorderingWithShared(t *testing.T) {
2785 ctx := testCc(t, `
2786 cc_library {
2787 name: "a",
2788 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002789 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002790 }
2791 cc_library {
2792 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002793 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002794 }
2795 cc_library {
2796 name: "c",
2797 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002798 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002799 }
2800
2801 `)
2802
Colin Cross7113d202019-11-20 16:39:12 -08002803 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002804 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
2805 actual := moduleA.depsInLinkOrder
2806 expected := getOutputPaths(ctx, variant, []string{"c", "b"})
2807
2808 if !reflect.DeepEqual(actual, expected) {
2809 t.Errorf("staticDeps orderings did not account for shared libs"+
2810 "\nactual: %v"+
2811 "\nexpected: %v",
2812 actual,
2813 expected,
2814 )
2815 }
2816}
2817
Jooyung Hanb04a4992020-03-13 18:57:35 +09002818func checkEquals(t *testing.T, message string, expected, actual interface{}) {
2819 if !reflect.DeepEqual(actual, expected) {
2820 t.Errorf(message+
2821 "\nactual: %v"+
2822 "\nexpected: %v",
2823 actual,
2824 expected,
2825 )
2826 }
2827}
2828
Jooyung Han61b66e92020-03-21 14:21:46 +00002829func TestLlndkLibrary(t *testing.T) {
2830 ctx := testCc(t, `
2831 cc_library {
2832 name: "libllndk",
2833 stubs: { versions: ["1", "2"] },
2834 }
2835 llndk_library {
2836 name: "libllndk",
2837 }
2838 `)
2839 actual := ctx.ModuleVariantsForTests("libllndk.llndk")
2840 expected := []string{
2841 "android_vendor.VER_arm64_armv8-a_shared",
2842 "android_vendor.VER_arm64_armv8-a_shared_1",
2843 "android_vendor.VER_arm64_armv8-a_shared_2",
2844 "android_vendor.VER_arm_armv7-a-neon_shared",
2845 "android_vendor.VER_arm_armv7-a-neon_shared_1",
2846 "android_vendor.VER_arm_armv7-a-neon_shared_2",
2847 }
2848 checkEquals(t, "variants for llndk stubs", expected, actual)
2849
2850 params := ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
2851 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2852
2853 params = ctx.ModuleForTests("libllndk.llndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
2854 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2855}
2856
Jiyong Parka46a4d52017-12-14 19:54:34 +09002857func TestLlndkHeaders(t *testing.T) {
2858 ctx := testCc(t, `
2859 llndk_headers {
2860 name: "libllndk_headers",
2861 export_include_dirs: ["my_include"],
2862 }
2863 llndk_library {
2864 name: "libllndk",
2865 export_llndk_headers: ["libllndk_headers"],
2866 }
2867 cc_library {
2868 name: "libvendor",
2869 shared_libs: ["libllndk"],
2870 vendor: true,
2871 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002872 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002873 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002874 }
2875 `)
2876
2877 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002878 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002879 cflags := cc.Args["cFlags"]
2880 if !strings.Contains(cflags, "-Imy_include") {
2881 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2882 }
2883}
2884
Logan Chien43d34c32017-12-20 01:17:32 +08002885func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2886 actual := module.Properties.AndroidMkRuntimeLibs
2887 if !reflect.DeepEqual(actual, expected) {
2888 t.Errorf("incorrect runtime_libs for shared libs"+
2889 "\nactual: %v"+
2890 "\nexpected: %v",
2891 actual,
2892 expected,
2893 )
2894 }
2895}
2896
2897const runtimeLibAndroidBp = `
2898 cc_library {
2899 name: "libvendor_available1",
2900 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002901 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002902 nocrt : true,
2903 system_shared_libs : [],
2904 }
2905 cc_library {
2906 name: "libvendor_available2",
2907 vendor_available: true,
2908 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002909 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002910 nocrt : true,
2911 system_shared_libs : [],
2912 }
2913 cc_library {
2914 name: "libvendor_available3",
2915 vendor_available: true,
2916 runtime_libs: ["libvendor_available1"],
2917 target: {
2918 vendor: {
2919 exclude_runtime_libs: ["libvendor_available1"],
2920 }
2921 },
Yi Konge7fe9912019-06-02 00:53:50 -07002922 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002923 nocrt : true,
2924 system_shared_libs : [],
2925 }
2926 cc_library {
2927 name: "libcore",
2928 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002929 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002930 nocrt : true,
2931 system_shared_libs : [],
2932 }
2933 cc_library {
2934 name: "libvendor1",
2935 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002936 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002937 nocrt : true,
2938 system_shared_libs : [],
2939 }
2940 cc_library {
2941 name: "libvendor2",
2942 vendor: true,
2943 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002944 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002945 nocrt : true,
2946 system_shared_libs : [],
2947 }
2948`
2949
2950func TestRuntimeLibs(t *testing.T) {
2951 ctx := testCc(t, runtimeLibAndroidBp)
2952
2953 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002954 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002955
2956 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2957 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2958
2959 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
2960 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2961
2962 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2963 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002964 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002965
2966 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2967 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
2968
2969 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2970 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
2971}
2972
2973func TestExcludeRuntimeLibs(t *testing.T) {
2974 ctx := testCc(t, runtimeLibAndroidBp)
2975
Colin Cross7113d202019-11-20 16:39:12 -08002976 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002977 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2978 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2979
Colin Crossfb0c16e2019-11-20 17:12:35 -08002980 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002981 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2982 checkRuntimeLibs(t, nil, module)
2983}
2984
2985func TestRuntimeLibsNoVndk(t *testing.T) {
2986 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2987
2988 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2989
Colin Cross7113d202019-11-20 16:39:12 -08002990 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002991
2992 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2993 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2994
2995 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2996 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
2997}
2998
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002999func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003000 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003001 actual := module.Properties.AndroidMkStaticLibs
3002 if !reflect.DeepEqual(actual, expected) {
3003 t.Errorf("incorrect static_libs"+
3004 "\nactual: %v"+
3005 "\nexpected: %v",
3006 actual,
3007 expected,
3008 )
3009 }
3010}
3011
3012const staticLibAndroidBp = `
3013 cc_library {
3014 name: "lib1",
3015 }
3016 cc_library {
3017 name: "lib2",
3018 static_libs: ["lib1"],
3019 }
3020`
3021
3022func TestStaticLibDepExport(t *testing.T) {
3023 ctx := testCc(t, staticLibAndroidBp)
3024
3025 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003026 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003027 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003028 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003029
3030 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003031 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003032 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3033 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003034 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003035}
3036
Jiyong Parkd08b6972017-09-26 10:50:54 +09003037var compilerFlagsTestCases = []struct {
3038 in string
3039 out bool
3040}{
3041 {
3042 in: "a",
3043 out: false,
3044 },
3045 {
3046 in: "-a",
3047 out: true,
3048 },
3049 {
3050 in: "-Ipath/to/something",
3051 out: false,
3052 },
3053 {
3054 in: "-isystempath/to/something",
3055 out: false,
3056 },
3057 {
3058 in: "--coverage",
3059 out: false,
3060 },
3061 {
3062 in: "-include a/b",
3063 out: true,
3064 },
3065 {
3066 in: "-include a/b c/d",
3067 out: false,
3068 },
3069 {
3070 in: "-DMACRO",
3071 out: true,
3072 },
3073 {
3074 in: "-DMAC RO",
3075 out: false,
3076 },
3077 {
3078 in: "-a -b",
3079 out: false,
3080 },
3081 {
3082 in: "-DMACRO=definition",
3083 out: true,
3084 },
3085 {
3086 in: "-DMACRO=defi nition",
3087 out: true, // TODO(jiyong): this should be false
3088 },
3089 {
3090 in: "-DMACRO(x)=x + 1",
3091 out: true,
3092 },
3093 {
3094 in: "-DMACRO=\"defi nition\"",
3095 out: true,
3096 },
3097}
3098
3099type mockContext struct {
3100 BaseModuleContext
3101 result bool
3102}
3103
3104func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3105 // CheckBadCompilerFlags calls this function when the flag should be rejected
3106 ctx.result = false
3107}
3108
3109func TestCompilerFlags(t *testing.T) {
3110 for _, testCase := range compilerFlagsTestCases {
3111 ctx := &mockContext{result: true}
3112 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3113 if ctx.result != testCase.out {
3114 t.Errorf("incorrect output:")
3115 t.Errorf(" input: %#v", testCase.in)
3116 t.Errorf(" expected: %#v", testCase.out)
3117 t.Errorf(" got: %#v", ctx.result)
3118 }
3119 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003120}
Jiyong Park374510b2018-03-19 18:23:01 +09003121
3122func TestVendorPublicLibraries(t *testing.T) {
3123 ctx := testCc(t, `
3124 cc_library_headers {
3125 name: "libvendorpublic_headers",
3126 export_include_dirs: ["my_include"],
3127 }
3128 vendor_public_library {
3129 name: "libvendorpublic",
3130 symbol_file: "",
3131 export_public_headers: ["libvendorpublic_headers"],
3132 }
3133 cc_library {
3134 name: "libvendorpublic",
3135 srcs: ["foo.c"],
3136 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003137 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003138 nocrt: true,
3139 }
3140
3141 cc_library {
3142 name: "libsystem",
3143 shared_libs: ["libvendorpublic"],
3144 vendor: false,
3145 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003146 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003147 nocrt: true,
3148 }
3149 cc_library {
3150 name: "libvendor",
3151 shared_libs: ["libvendorpublic"],
3152 vendor: true,
3153 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003154 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003155 nocrt: true,
3156 }
3157 `)
3158
Colin Cross7113d202019-11-20 16:39:12 -08003159 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003160 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003161
3162 // test if header search paths are correctly added
3163 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003164 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003165 cflags := cc.Args["cFlags"]
3166 if !strings.Contains(cflags, "-Imy_include") {
3167 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3168 }
3169
3170 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003171 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003172 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003173 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003174 if !strings.Contains(libflags, stubPaths[0].String()) {
3175 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3176 }
3177
3178 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003179 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003180 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003181 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003182 if !strings.Contains(libflags, stubPaths[0].String()) {
3183 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3184 }
3185
3186}
Jiyong Park37b25202018-07-11 10:49:27 +09003187
3188func TestRecovery(t *testing.T) {
3189 ctx := testCc(t, `
3190 cc_library_shared {
3191 name: "librecovery",
3192 recovery: true,
3193 }
3194 cc_library_shared {
3195 name: "librecovery32",
3196 recovery: true,
3197 compile_multilib:"32",
3198 }
Jiyong Park5baac542018-08-28 09:55:37 +09003199 cc_library_shared {
3200 name: "libHalInRecovery",
3201 recovery_available: true,
3202 vendor: true,
3203 }
Jiyong Park37b25202018-07-11 10:49:27 +09003204 `)
3205
3206 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003207 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003208 if len(variants) != 1 || !android.InList(arm64, variants) {
3209 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3210 }
3211
3212 variants = ctx.ModuleVariantsForTests("librecovery32")
3213 if android.InList(arm64, variants) {
3214 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3215 }
Jiyong Park5baac542018-08-28 09:55:37 +09003216
3217 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3218 if !recoveryModule.Platform() {
3219 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3220 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003221}
Jiyong Park5baac542018-08-28 09:55:37 +09003222
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003223func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3224 bp := `
3225 cc_prebuilt_test_library_shared {
3226 name: "test_lib",
3227 relative_install_path: "foo/bar/baz",
3228 srcs: ["srcpath/dontusethispath/baz.so"],
3229 }
3230
3231 cc_test {
3232 name: "main_test",
3233 data_libs: ["test_lib"],
3234 gtest: false,
3235 }
3236 `
3237
3238 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3239 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3240 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3241 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3242
3243 ctx := testCcWithConfig(t, config)
3244 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3245 testBinary := module.(*Module).linker.(*testBinary)
3246 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3247 if err != nil {
3248 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3249 }
3250 if len(outputFiles) != 1 {
3251 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3252 }
3253 if len(testBinary.dataPaths()) != 1 {
3254 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3255 }
3256
3257 outputPath := outputFiles[0].String()
3258
3259 if !strings.HasSuffix(outputPath, "/main_test") {
3260 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3261 }
3262 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
3263 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3264 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3265 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3266 }
3267}
3268
Jiyong Park7ed9de32018-10-15 22:25:07 +09003269func TestVersionedStubs(t *testing.T) {
3270 ctx := testCc(t, `
3271 cc_library_shared {
3272 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003273 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003274 stubs: {
3275 symbol_file: "foo.map.txt",
3276 versions: ["1", "2", "3"],
3277 },
3278 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003279
Jiyong Park7ed9de32018-10-15 22:25:07 +09003280 cc_library_shared {
3281 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003282 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003283 shared_libs: ["libFoo#1"],
3284 }`)
3285
3286 variants := ctx.ModuleVariantsForTests("libFoo")
3287 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003288 "android_arm64_armv8-a_shared",
3289 "android_arm64_armv8-a_shared_1",
3290 "android_arm64_armv8-a_shared_2",
3291 "android_arm64_armv8-a_shared_3",
3292 "android_arm_armv7-a-neon_shared",
3293 "android_arm_armv7-a-neon_shared_1",
3294 "android_arm_armv7-a-neon_shared_2",
3295 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003296 }
3297 variantsMismatch := false
3298 if len(variants) != len(expectedVariants) {
3299 variantsMismatch = true
3300 } else {
3301 for _, v := range expectedVariants {
3302 if !inList(v, variants) {
3303 variantsMismatch = false
3304 }
3305 }
3306 }
3307 if variantsMismatch {
3308 t.Errorf("variants of libFoo expected:\n")
3309 for _, v := range expectedVariants {
3310 t.Errorf("%q\n", v)
3311 }
3312 t.Errorf(", but got:\n")
3313 for _, v := range variants {
3314 t.Errorf("%q\n", v)
3315 }
3316 }
3317
Colin Cross7113d202019-11-20 16:39:12 -08003318 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003319 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003320 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003321 if !strings.Contains(libFlags, libFoo1StubPath) {
3322 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3323 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003324
Colin Cross7113d202019-11-20 16:39:12 -08003325 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003326 cFlags := libBarCompileRule.Args["cFlags"]
3327 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3328 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3329 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3330 }
Jiyong Park37b25202018-07-11 10:49:27 +09003331}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003332
Jooyung Hanb04a4992020-03-13 18:57:35 +09003333func TestVersioningMacro(t *testing.T) {
3334 for _, tc := range []struct{ moduleName, expected string }{
3335 {"libc", "__LIBC_API__"},
3336 {"libfoo", "__LIBFOO_API__"},
3337 {"libfoo@1", "__LIBFOO_1_API__"},
3338 {"libfoo-v1", "__LIBFOO_V1_API__"},
3339 {"libfoo.v1", "__LIBFOO_V1_API__"},
3340 } {
3341 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3342 }
3343}
3344
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003345func TestStaticExecutable(t *testing.T) {
3346 ctx := testCc(t, `
3347 cc_binary {
3348 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003349 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003350 static_executable: true,
3351 }`)
3352
Colin Cross7113d202019-11-20 16:39:12 -08003353 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003354 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3355 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003356 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003357 for _, lib := range systemStaticLibs {
3358 if !strings.Contains(libFlags, lib) {
3359 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3360 }
3361 }
3362 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3363 for _, lib := range systemSharedLibs {
3364 if strings.Contains(libFlags, lib) {
3365 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3366 }
3367 }
3368}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003369
3370func TestStaticDepsOrderWithStubs(t *testing.T) {
3371 ctx := testCc(t, `
3372 cc_binary {
3373 name: "mybin",
3374 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003375 static_libs: ["libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003376 static_executable: true,
3377 stl: "none",
3378 }
3379
3380 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003381 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003382 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003383 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003384 stl: "none",
3385 }
3386
3387 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003388 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003389 srcs: ["foo.c"],
3390 stl: "none",
3391 stubs: {
3392 versions: ["1"],
3393 },
3394 }`)
3395
Colin Cross7113d202019-11-20 16:39:12 -08003396 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Module().(*Module)
Jiyong Parke4bb9862019-02-01 00:31:10 +09003397 actual := mybin.depsInLinkOrder
Colin Crossf9aabd72020-02-15 11:29:50 -08003398 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003399
3400 if !reflect.DeepEqual(actual, expected) {
3401 t.Errorf("staticDeps orderings were not propagated correctly"+
3402 "\nactual: %v"+
3403 "\nexpected: %v",
3404 actual,
3405 expected,
3406 )
3407 }
3408}
Jooyung Han38002912019-05-16 04:01:54 +09003409
Jooyung Hand48f3c32019-08-23 11:18:57 +09003410func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3411 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3412 cc_library {
3413 name: "libA",
3414 srcs: ["foo.c"],
3415 shared_libs: ["libB"],
3416 stl: "none",
3417 }
3418
3419 cc_library {
3420 name: "libB",
3421 srcs: ["foo.c"],
3422 enabled: false,
3423 stl: "none",
3424 }
3425 `)
3426}
3427
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003428// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3429// correctly.
3430func TestFuzzTarget(t *testing.T) {
3431 ctx := testCc(t, `
3432 cc_fuzz {
3433 name: "fuzz_smoke_test",
3434 srcs: ["foo.c"],
3435 }`)
3436
Paul Duffin075c4172019-12-19 19:06:13 +00003437 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003438 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3439}
3440
Jiyong Park29074592019-07-07 16:27:47 +09003441func TestAidl(t *testing.T) {
3442}
3443
Jooyung Han38002912019-05-16 04:01:54 +09003444func assertString(t *testing.T, got, expected string) {
3445 t.Helper()
3446 if got != expected {
3447 t.Errorf("expected %q got %q", expected, got)
3448 }
3449}
3450
3451func assertArrayString(t *testing.T, got, expected []string) {
3452 t.Helper()
3453 if len(got) != len(expected) {
3454 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3455 return
3456 }
3457 for i := range got {
3458 if got[i] != expected[i] {
3459 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3460 i, expected[i], expected, got[i], got)
3461 return
3462 }
3463 }
3464}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003465
Jooyung Han0302a842019-10-30 18:43:49 +09003466func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3467 t.Helper()
3468 assertArrayString(t, android.SortedStringKeys(m), expected)
3469}
3470
Colin Crosse1bb5d02019-09-24 14:55:04 -07003471func TestDefaults(t *testing.T) {
3472 ctx := testCc(t, `
3473 cc_defaults {
3474 name: "defaults",
3475 srcs: ["foo.c"],
3476 static: {
3477 srcs: ["bar.c"],
3478 },
3479 shared: {
3480 srcs: ["baz.c"],
3481 },
3482 }
3483
3484 cc_library_static {
3485 name: "libstatic",
3486 defaults: ["defaults"],
3487 }
3488
3489 cc_library_shared {
3490 name: "libshared",
3491 defaults: ["defaults"],
3492 }
3493
3494 cc_library {
3495 name: "libboth",
3496 defaults: ["defaults"],
3497 }
3498
3499 cc_binary {
3500 name: "binary",
3501 defaults: ["defaults"],
3502 }`)
3503
3504 pathsToBase := func(paths android.Paths) []string {
3505 var ret []string
3506 for _, p := range paths {
3507 ret = append(ret, p.Base())
3508 }
3509 return ret
3510 }
3511
Colin Cross7113d202019-11-20 16:39:12 -08003512 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003513 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3514 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3515 }
Colin Cross7113d202019-11-20 16:39:12 -08003516 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003517 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3518 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3519 }
Colin Cross7113d202019-11-20 16:39:12 -08003520 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003521 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3522 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3523 }
3524
Colin Cross7113d202019-11-20 16:39:12 -08003525 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003526 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3527 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3528 }
Colin Cross7113d202019-11-20 16:39:12 -08003529 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003530 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3531 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3532 }
3533}
Colin Crosseabaedd2020-02-06 17:01:55 -08003534
3535func TestProductVariableDefaults(t *testing.T) {
3536 bp := `
3537 cc_defaults {
3538 name: "libfoo_defaults",
3539 srcs: ["foo.c"],
3540 cppflags: ["-DFOO"],
3541 product_variables: {
3542 debuggable: {
3543 cppflags: ["-DBAR"],
3544 },
3545 },
3546 }
3547
3548 cc_library {
3549 name: "libfoo",
3550 defaults: ["libfoo_defaults"],
3551 }
3552 `
3553
3554 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3555 config.TestProductVariables.Debuggable = BoolPtr(true)
3556
3557 ctx := CreateTestContext()
3558 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
3559 ctx.BottomUp("variable", android.VariableMutator).Parallel()
3560 })
3561 ctx.Register(config)
3562
3563 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
3564 android.FailIfErrored(t, errs)
3565 _, errs = ctx.PrepareBuildActions(config)
3566 android.FailIfErrored(t, errs)
3567
3568 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
3569 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
3570 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
3571 }
3572}