blob: 1e70529bf7d098e5e6e34b1f1cde93e9cec0fa00 [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,
221 isVndkSp bool, extends string) {
222
Logan Chiend3c59a22018-03-29 14:08:15 +0800223 t.Helper()
224
Logan Chienf3511742017-10-31 18:04:35 +0800225 mod := ctx.ModuleForTests(name, vendorVariant).Module().(*Module)
Ivan Lozano52767be2019-10-18 14:49:46 -0700226 if !mod.HasVendorVariant() {
Colin Crossf46e37f2018-03-21 16:25:58 -0700227 t.Errorf("%q must have vendor variant", name)
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
Jooyung Han39edb6c2019-11-06 16:53:07 +0900261func checkVndkSnapshot(t *testing.T, ctx *android.TestContext, moduleName, snapshotFilename, subDir, variant string) {
Inseob Kim1f086e22019-05-09 13:29:15 +0900262 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
263
Jooyung Han39edb6c2019-11-06 16:53:07 +0900264 mod, ok := ctx.ModuleForTests(moduleName, variant).Module().(android.OutputFileProducer)
265 if !ok {
266 t.Errorf("%q must have output\n", moduleName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900267 return
268 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900269 outputFiles, err := mod.OutputFiles("")
270 if err != nil || len(outputFiles) != 1 {
271 t.Errorf("%q must have single output\n", moduleName)
272 return
273 }
274 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900275
276 out := vndkSnapshot.Output(snapshotPath)
Jooyung Han39edb6c2019-11-06 16:53:07 +0900277 if out.Input.String() != outputFiles[0].String() {
278 t.Errorf("The input of VNDK snapshot must be %q, but %q", out.Input.String(), outputFiles[0])
Inseob Kim1f086e22019-05-09 13:29:15 +0900279 }
280}
281
Jooyung Han2216fb12019-11-06 16:46:15 +0900282func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
283 t.Helper()
284 assertString(t, params.Rule.String(), android.WriteFile.String())
285 actual := strings.FieldsFunc(strings.ReplaceAll(params.Args["content"], "\\n", "\n"), func(r rune) bool { return r == '\n' })
286 assertArrayString(t, actual, expected)
287}
288
Jooyung Han097087b2019-10-22 19:32:18 +0900289func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
290 t.Helper()
291 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900292 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
293}
294
295func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
296 t.Helper()
297 vndkLibraries := ctx.ModuleForTests(module, "")
298 output := insertVndkVersion(module, "VER")
299 checkWriteFileOutput(t, vndkLibraries.Output(output), expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900300}
301
Logan Chienf3511742017-10-31 18:04:35 +0800302func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800303 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800304 cc_library {
305 name: "libvndk",
306 vendor_available: true,
307 vndk: {
308 enabled: true,
309 },
310 nocrt: true,
311 }
312
313 cc_library {
314 name: "libvndk_private",
315 vendor_available: false,
316 vndk: {
317 enabled: true,
318 },
319 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900320 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800321 }
322
323 cc_library {
324 name: "libvndk_sp",
325 vendor_available: true,
326 vndk: {
327 enabled: true,
328 support_system_process: true,
329 },
330 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900331 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800332 }
333
334 cc_library {
335 name: "libvndk_sp_private",
336 vendor_available: false,
337 vndk: {
338 enabled: true,
339 support_system_process: true,
340 },
341 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900342 target: {
343 vendor: {
344 suffix: "-x",
345 },
346 },
Logan Chienf3511742017-10-31 18:04:35 +0800347 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900348 vndk_libraries_txt {
349 name: "llndk.libraries.txt",
350 }
351 vndk_libraries_txt {
352 name: "vndkcore.libraries.txt",
353 }
354 vndk_libraries_txt {
355 name: "vndksp.libraries.txt",
356 }
357 vndk_libraries_txt {
358 name: "vndkprivate.libraries.txt",
359 }
360 vndk_libraries_txt {
361 name: "vndkcorevariant.libraries.txt",
362 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800363 `
364
365 config := TestConfig(buildDir, android.Android, nil, bp, nil)
366 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
367 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
368
369 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800370
371 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "")
372 checkVndkModule(t, ctx, "libvndk_private", "vndk-VER", false, "")
373 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "")
374 checkVndkModule(t, ctx, "libvndk_sp_private", "vndk-sp-VER", true, "")
Inseob Kim1f086e22019-05-09 13:29:15 +0900375
376 // Check VNDK snapshot output.
377
378 snapshotDir := "vndk-snapshot"
379 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
380
381 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
382 "arm64", "armv8-a"))
383 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
384 "arm", "armv7-a-neon"))
385
386 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
387 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
388 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
389 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
390
Colin Crossfb0c16e2019-11-20 17:12:35 -0800391 variant := "android_vendor.VER_arm64_armv8-a_shared"
392 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900393
Jooyung Han39edb6c2019-11-06 16:53:07 +0900394 checkVndkSnapshot(t, ctx, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
395 checkVndkSnapshot(t, ctx, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
396 checkVndkSnapshot(t, ctx, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
397 checkVndkSnapshot(t, ctx, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900398
Jooyung Han39edb6c2019-11-06 16:53:07 +0900399 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
400 checkVndkSnapshot(t, ctx, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
401 checkVndkSnapshot(t, ctx, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
402 checkVndkSnapshot(t, ctx, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
403 checkVndkSnapshot(t, ctx, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
404
Jooyung Han097087b2019-10-22 19:32:18 +0900405 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
406 "LLNDK: libc.so",
407 "LLNDK: libdl.so",
408 "LLNDK: libft2.so",
409 "LLNDK: libm.so",
410 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900411 "VNDK-SP: libvndk_sp-x.so",
412 "VNDK-SP: libvndk_sp_private-x.so",
413 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900414 "VNDK-core: libvndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900415 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900416 "VNDK-private: libvndk-private.so",
417 "VNDK-private: libvndk_sp_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900418 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900419 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
420 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so"})
421 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so"})
422 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so"})
423 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
424}
425
426func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800427 bp := `
Jooyung Han2216fb12019-11-06 16:46:15 +0900428 vndk_libraries_txt {
429 name: "llndk.libraries.txt",
Colin Cross98be1bb2019-12-13 20:41:13 -0800430 }`
431 config := TestConfig(buildDir, android.Android, nil, bp, nil)
432 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
433 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
434 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900435
436 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900437 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900438 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900439}
440
441func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800442 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900443 cc_library {
444 name: "libvndk",
445 vendor_available: true,
446 vndk: {
447 enabled: true,
448 },
449 nocrt: true,
450 }
451
452 cc_library {
453 name: "libvndk_sp",
454 vendor_available: true,
455 vndk: {
456 enabled: true,
457 support_system_process: true,
458 },
459 nocrt: true,
460 }
461
462 cc_library {
463 name: "libvndk2",
464 vendor_available: false,
465 vndk: {
466 enabled: true,
467 },
468 nocrt: true,
469 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900470
471 vndk_libraries_txt {
472 name: "vndkcorevariant.libraries.txt",
473 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800474 `
475
476 config := TestConfig(buildDir, android.Android, nil, bp, nil)
477 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
478 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
479 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
480
481 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
482
483 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900484
Jooyung Han2216fb12019-11-06 16:46:15 +0900485 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900486}
487
488func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900489 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900490 cc_library {
491 name: "libvndk",
492 vendor_available: true,
493 vndk: {
494 enabled: true,
495 },
496 nocrt: true,
497 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900498 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900499
500 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
501 "LLNDK: libc.so",
502 "LLNDK: libdl.so",
503 "LLNDK: libft2.so",
504 "LLNDK: libm.so",
505 "VNDK-SP: libc++.so",
506 "VNDK-core: libvndk.so",
507 "VNDK-private: libft2.so",
508 })
Logan Chienf3511742017-10-31 18:04:35 +0800509}
510
Logan Chiend3c59a22018-03-29 14:08:15 +0800511func TestVndkDepError(t *testing.T) {
512 // Check whether an error is emitted when a VNDK lib depends on a system lib.
513 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
514 cc_library {
515 name: "libvndk",
516 vendor_available: true,
517 vndk: {
518 enabled: true,
519 },
520 shared_libs: ["libfwk"], // Cause error
521 nocrt: true,
522 }
523
524 cc_library {
525 name: "libfwk",
526 nocrt: true,
527 }
528 `)
529
530 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
531 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
532 cc_library {
533 name: "libvndk",
534 vendor_available: true,
535 vndk: {
536 enabled: true,
537 },
538 shared_libs: ["libvendor"], // Cause error
539 nocrt: true,
540 }
541
542 cc_library {
543 name: "libvendor",
544 vendor: true,
545 nocrt: true,
546 }
547 `)
548
549 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
550 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
551 cc_library {
552 name: "libvndk_sp",
553 vendor_available: true,
554 vndk: {
555 enabled: true,
556 support_system_process: true,
557 },
558 shared_libs: ["libfwk"], // Cause error
559 nocrt: true,
560 }
561
562 cc_library {
563 name: "libfwk",
564 nocrt: true,
565 }
566 `)
567
568 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
569 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
570 cc_library {
571 name: "libvndk_sp",
572 vendor_available: true,
573 vndk: {
574 enabled: true,
575 support_system_process: true,
576 },
577 shared_libs: ["libvendor"], // Cause error
578 nocrt: true,
579 }
580
581 cc_library {
582 name: "libvendor",
583 vendor: true,
584 nocrt: true,
585 }
586 `)
587
588 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
589 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
590 cc_library {
591 name: "libvndk_sp",
592 vendor_available: true,
593 vndk: {
594 enabled: true,
595 support_system_process: true,
596 },
597 shared_libs: ["libvndk"], // Cause error
598 nocrt: true,
599 }
600
601 cc_library {
602 name: "libvndk",
603 vendor_available: true,
604 vndk: {
605 enabled: true,
606 },
607 nocrt: true,
608 }
609 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900610
611 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
612 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
613 cc_library {
614 name: "libvndk",
615 vendor_available: true,
616 vndk: {
617 enabled: true,
618 },
619 shared_libs: ["libnonvndk"],
620 nocrt: true,
621 }
622
623 cc_library {
624 name: "libnonvndk",
625 vendor_available: true,
626 nocrt: true,
627 }
628 `)
629
630 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
631 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
632 cc_library {
633 name: "libvndkprivate",
634 vendor_available: false,
635 vndk: {
636 enabled: true,
637 },
638 shared_libs: ["libnonvndk"],
639 nocrt: true,
640 }
641
642 cc_library {
643 name: "libnonvndk",
644 vendor_available: true,
645 nocrt: true,
646 }
647 `)
648
649 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
650 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
651 cc_library {
652 name: "libvndksp",
653 vendor_available: true,
654 vndk: {
655 enabled: true,
656 support_system_process: true,
657 },
658 shared_libs: ["libnonvndk"],
659 nocrt: true,
660 }
661
662 cc_library {
663 name: "libnonvndk",
664 vendor_available: true,
665 nocrt: true,
666 }
667 `)
668
669 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
670 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
671 cc_library {
672 name: "libvndkspprivate",
673 vendor_available: false,
674 vndk: {
675 enabled: true,
676 support_system_process: true,
677 },
678 shared_libs: ["libnonvndk"],
679 nocrt: true,
680 }
681
682 cc_library {
683 name: "libnonvndk",
684 vendor_available: true,
685 nocrt: true,
686 }
687 `)
688}
689
690func TestDoubleLoadbleDep(t *testing.T) {
691 // okay to link : LLNDK -> double_loadable VNDK
692 testCc(t, `
693 cc_library {
694 name: "libllndk",
695 shared_libs: ["libdoubleloadable"],
696 }
697
698 llndk_library {
699 name: "libllndk",
700 symbol_file: "",
701 }
702
703 cc_library {
704 name: "libdoubleloadable",
705 vendor_available: true,
706 vndk: {
707 enabled: true,
708 },
709 double_loadable: true,
710 }
711 `)
712 // okay to link : LLNDK -> VNDK-SP
713 testCc(t, `
714 cc_library {
715 name: "libllndk",
716 shared_libs: ["libvndksp"],
717 }
718
719 llndk_library {
720 name: "libllndk",
721 symbol_file: "",
722 }
723
724 cc_library {
725 name: "libvndksp",
726 vendor_available: true,
727 vndk: {
728 enabled: true,
729 support_system_process: true,
730 },
731 }
732 `)
733 // okay to link : double_loadable -> double_loadable
734 testCc(t, `
735 cc_library {
736 name: "libdoubleloadable1",
737 shared_libs: ["libdoubleloadable2"],
738 vendor_available: true,
739 double_loadable: true,
740 }
741
742 cc_library {
743 name: "libdoubleloadable2",
744 vendor_available: true,
745 double_loadable: true,
746 }
747 `)
748 // okay to link : double_loadable VNDK -> double_loadable VNDK private
749 testCc(t, `
750 cc_library {
751 name: "libdoubleloadable",
752 vendor_available: true,
753 vndk: {
754 enabled: true,
755 },
756 double_loadable: true,
757 shared_libs: ["libnondoubleloadable"],
758 }
759
760 cc_library {
761 name: "libnondoubleloadable",
762 vendor_available: false,
763 vndk: {
764 enabled: true,
765 },
766 double_loadable: true,
767 }
768 `)
769 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
770 testCc(t, `
771 cc_library {
772 name: "libllndk",
773 shared_libs: ["libcoreonly"],
774 }
775
776 llndk_library {
777 name: "libllndk",
778 symbol_file: "",
779 }
780
781 cc_library {
782 name: "libcoreonly",
783 shared_libs: ["libvendoravailable"],
784 }
785
786 // indirect dependency of LLNDK
787 cc_library {
788 name: "libvendoravailable",
789 vendor_available: true,
790 double_loadable: true,
791 }
792 `)
793}
794
795func TestDoubleLoadableDepError(t *testing.T) {
796 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
797 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
798 cc_library {
799 name: "libllndk",
800 shared_libs: ["libnondoubleloadable"],
801 }
802
803 llndk_library {
804 name: "libllndk",
805 symbol_file: "",
806 }
807
808 cc_library {
809 name: "libnondoubleloadable",
810 vendor_available: true,
811 vndk: {
812 enabled: true,
813 },
814 }
815 `)
816
817 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
818 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
819 cc_library {
820 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -0700821 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900822 shared_libs: ["libnondoubleloadable"],
823 }
824
825 llndk_library {
826 name: "libllndk",
827 symbol_file: "",
828 }
829
830 cc_library {
831 name: "libnondoubleloadable",
832 vendor_available: true,
833 }
834 `)
835
836 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
837 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
838 cc_library {
839 name: "libdoubleloadable",
840 vendor_available: true,
841 double_loadable: true,
842 shared_libs: ["libnondoubleloadable"],
843 }
844
845 cc_library {
846 name: "libnondoubleloadable",
847 vendor_available: true,
848 }
849 `)
850
851 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
852 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
853 cc_library {
854 name: "libdoubleloadable",
855 vendor_available: true,
856 double_loadable: true,
857 shared_libs: ["libnondoubleloadable"],
858 }
859
860 cc_library {
861 name: "libnondoubleloadable",
862 vendor_available: true,
863 vndk: {
864 enabled: true,
865 },
866 }
867 `)
868
869 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
870 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
871 cc_library {
872 name: "libdoubleloadable",
873 vendor_available: true,
874 vndk: {
875 enabled: true,
876 },
877 double_loadable: true,
878 shared_libs: ["libnondoubleloadable"],
879 }
880
881 cc_library {
882 name: "libnondoubleloadable",
883 vendor_available: false,
884 vndk: {
885 enabled: true,
886 },
887 }
888 `)
889
890 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
891 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
892 cc_library {
893 name: "libllndk",
894 shared_libs: ["libcoreonly"],
895 }
896
897 llndk_library {
898 name: "libllndk",
899 symbol_file: "",
900 }
901
902 cc_library {
903 name: "libcoreonly",
904 shared_libs: ["libvendoravailable"],
905 }
906
907 // indirect dependency of LLNDK
908 cc_library {
909 name: "libvendoravailable",
910 vendor_available: true,
911 }
912 `)
Logan Chiend3c59a22018-03-29 14:08:15 +0800913}
914
Justin Yun9357f4a2018-11-28 15:14:47 +0900915func TestVndkMustNotBeProductSpecific(t *testing.T) {
916 // Check whether an error is emitted when a vndk lib has 'product_specific: true'.
917 testCcError(t, "product_specific must not be true when `vndk: {enabled: true}`", `
918 cc_library {
919 name: "libvndk",
920 product_specific: true, // Cause error
921 vendor_available: true,
922 vndk: {
923 enabled: true,
924 },
925 nocrt: true,
926 }
927 `)
928}
929
Logan Chienf3511742017-10-31 18:04:35 +0800930func TestVndkExt(t *testing.T) {
931 // This test checks the VNDK-Ext properties.
932 ctx := testCc(t, `
933 cc_library {
934 name: "libvndk",
935 vendor_available: true,
936 vndk: {
937 enabled: true,
938 },
939 nocrt: true,
940 }
Jooyung Han4c2b9422019-10-22 19:53:47 +0900941 cc_library {
942 name: "libvndk2",
943 vendor_available: true,
944 vndk: {
945 enabled: true,
946 },
947 target: {
948 vendor: {
949 suffix: "-suffix",
950 },
951 },
952 nocrt: true,
953 }
Logan Chienf3511742017-10-31 18:04:35 +0800954
955 cc_library {
956 name: "libvndk_ext",
957 vendor: true,
958 vndk: {
959 enabled: true,
960 extends: "libvndk",
961 },
962 nocrt: true,
963 }
Jooyung Han4c2b9422019-10-22 19:53:47 +0900964
965 cc_library {
966 name: "libvndk2_ext",
967 vendor: true,
968 vndk: {
969 enabled: true,
970 extends: "libvndk2",
971 },
972 nocrt: true,
973 }
Logan Chienf3511742017-10-31 18:04:35 +0800974 `)
975
976 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk")
Jooyung Han4c2b9422019-10-22 19:53:47 +0900977
978 mod := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
979 assertString(t, mod.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +0800980}
981
Logan Chiend3c59a22018-03-29 14:08:15 +0800982func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +0800983 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
984 ctx := testCcNoVndk(t, `
985 cc_library {
986 name: "libvndk",
987 vendor_available: true,
988 vndk: {
989 enabled: true,
990 },
991 nocrt: true,
992 }
993
994 cc_library {
995 name: "libvndk_ext",
996 vendor: true,
997 vndk: {
998 enabled: true,
999 extends: "libvndk",
1000 },
1001 nocrt: true,
1002 }
1003 `)
1004
1005 // Ensures that the core variant of "libvndk_ext" can be found.
1006 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1007 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1008 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1009 }
1010}
1011
1012func TestVndkExtError(t *testing.T) {
1013 // This test ensures an error is emitted in ill-formed vndk-ext definition.
1014 testCcError(t, "must set `vendor: true` to set `extends: \".*\"`", `
1015 cc_library {
1016 name: "libvndk",
1017 vendor_available: true,
1018 vndk: {
1019 enabled: true,
1020 },
1021 nocrt: true,
1022 }
1023
1024 cc_library {
1025 name: "libvndk_ext",
1026 vndk: {
1027 enabled: true,
1028 extends: "libvndk",
1029 },
1030 nocrt: true,
1031 }
1032 `)
1033
1034 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1035 cc_library {
1036 name: "libvndk",
1037 vendor_available: true,
1038 vndk: {
1039 enabled: true,
1040 },
1041 nocrt: true,
1042 }
1043
1044 cc_library {
1045 name: "libvndk_ext",
1046 vendor: true,
1047 vndk: {
1048 enabled: true,
1049 },
1050 nocrt: true,
1051 }
1052 `)
1053}
1054
1055func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1056 // This test ensures an error is emitted for inconsistent support_system_process.
1057 testCcError(t, "module \".*\" with mismatched support_system_process", `
1058 cc_library {
1059 name: "libvndk",
1060 vendor_available: true,
1061 vndk: {
1062 enabled: true,
1063 },
1064 nocrt: true,
1065 }
1066
1067 cc_library {
1068 name: "libvndk_sp_ext",
1069 vendor: true,
1070 vndk: {
1071 enabled: true,
1072 extends: "libvndk",
1073 support_system_process: true,
1074 },
1075 nocrt: true,
1076 }
1077 `)
1078
1079 testCcError(t, "module \".*\" with mismatched support_system_process", `
1080 cc_library {
1081 name: "libvndk_sp",
1082 vendor_available: true,
1083 vndk: {
1084 enabled: true,
1085 support_system_process: true,
1086 },
1087 nocrt: true,
1088 }
1089
1090 cc_library {
1091 name: "libvndk_ext",
1092 vendor: true,
1093 vndk: {
1094 enabled: true,
1095 extends: "libvndk_sp",
1096 },
1097 nocrt: true,
1098 }
1099 `)
1100}
1101
1102func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001103 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08001104 // with `vendor_available: false`.
1105 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1106 cc_library {
1107 name: "libvndk",
1108 vendor_available: false,
1109 vndk: {
1110 enabled: true,
1111 },
1112 nocrt: true,
1113 }
1114
1115 cc_library {
1116 name: "libvndk_ext",
1117 vendor: true,
1118 vndk: {
1119 enabled: true,
1120 extends: "libvndk",
1121 },
1122 nocrt: true,
1123 }
1124 `)
1125}
1126
Logan Chiend3c59a22018-03-29 14:08:15 +08001127func TestVendorModuleUseVndkExt(t *testing.T) {
1128 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001129 testCc(t, `
1130 cc_library {
1131 name: "libvndk",
1132 vendor_available: true,
1133 vndk: {
1134 enabled: true,
1135 },
1136 nocrt: true,
1137 }
1138
1139 cc_library {
1140 name: "libvndk_ext",
1141 vendor: true,
1142 vndk: {
1143 enabled: true,
1144 extends: "libvndk",
1145 },
1146 nocrt: true,
1147 }
1148
1149 cc_library {
1150
1151 name: "libvndk_sp",
1152 vendor_available: true,
1153 vndk: {
1154 enabled: true,
1155 support_system_process: true,
1156 },
1157 nocrt: true,
1158 }
1159
1160 cc_library {
1161 name: "libvndk_sp_ext",
1162 vendor: true,
1163 vndk: {
1164 enabled: true,
1165 extends: "libvndk_sp",
1166 support_system_process: true,
1167 },
1168 nocrt: true,
1169 }
1170
1171 cc_library {
1172 name: "libvendor",
1173 vendor: true,
1174 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1175 nocrt: true,
1176 }
1177 `)
1178}
1179
Logan Chiend3c59a22018-03-29 14:08:15 +08001180func TestVndkExtUseVendorLib(t *testing.T) {
1181 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001182 testCc(t, `
1183 cc_library {
1184 name: "libvndk",
1185 vendor_available: true,
1186 vndk: {
1187 enabled: true,
1188 },
1189 nocrt: true,
1190 }
1191
1192 cc_library {
1193 name: "libvndk_ext",
1194 vendor: true,
1195 vndk: {
1196 enabled: true,
1197 extends: "libvndk",
1198 },
1199 shared_libs: ["libvendor"],
1200 nocrt: true,
1201 }
1202
1203 cc_library {
1204 name: "libvendor",
1205 vendor: true,
1206 nocrt: true,
1207 }
1208 `)
Logan Chienf3511742017-10-31 18:04:35 +08001209
Logan Chiend3c59a22018-03-29 14:08:15 +08001210 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1211 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001212 cc_library {
1213 name: "libvndk_sp",
1214 vendor_available: true,
1215 vndk: {
1216 enabled: true,
1217 support_system_process: true,
1218 },
1219 nocrt: true,
1220 }
1221
1222 cc_library {
1223 name: "libvndk_sp_ext",
1224 vendor: true,
1225 vndk: {
1226 enabled: true,
1227 extends: "libvndk_sp",
1228 support_system_process: true,
1229 },
1230 shared_libs: ["libvendor"], // Cause an error
1231 nocrt: true,
1232 }
1233
1234 cc_library {
1235 name: "libvendor",
1236 vendor: true,
1237 nocrt: true,
1238 }
1239 `)
1240}
1241
Logan Chiend3c59a22018-03-29 14:08:15 +08001242func TestVndkSpExtUseVndkError(t *testing.T) {
1243 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1244 // library.
1245 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1246 cc_library {
1247 name: "libvndk",
1248 vendor_available: true,
1249 vndk: {
1250 enabled: true,
1251 },
1252 nocrt: true,
1253 }
1254
1255 cc_library {
1256 name: "libvndk_sp",
1257 vendor_available: true,
1258 vndk: {
1259 enabled: true,
1260 support_system_process: true,
1261 },
1262 nocrt: true,
1263 }
1264
1265 cc_library {
1266 name: "libvndk_sp_ext",
1267 vendor: true,
1268 vndk: {
1269 enabled: true,
1270 extends: "libvndk_sp",
1271 support_system_process: true,
1272 },
1273 shared_libs: ["libvndk"], // Cause an error
1274 nocrt: true,
1275 }
1276 `)
1277
1278 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1279 // library.
1280 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1281 cc_library {
1282 name: "libvndk",
1283 vendor_available: true,
1284 vndk: {
1285 enabled: true,
1286 },
1287 nocrt: true,
1288 }
1289
1290 cc_library {
1291 name: "libvndk_ext",
1292 vendor: true,
1293 vndk: {
1294 enabled: true,
1295 extends: "libvndk",
1296 },
1297 nocrt: true,
1298 }
1299
1300 cc_library {
1301 name: "libvndk_sp",
1302 vendor_available: true,
1303 vndk: {
1304 enabled: true,
1305 support_system_process: true,
1306 },
1307 nocrt: true,
1308 }
1309
1310 cc_library {
1311 name: "libvndk_sp_ext",
1312 vendor: true,
1313 vndk: {
1314 enabled: true,
1315 extends: "libvndk_sp",
1316 support_system_process: true,
1317 },
1318 shared_libs: ["libvndk_ext"], // Cause an error
1319 nocrt: true,
1320 }
1321 `)
1322}
1323
1324func TestVndkUseVndkExtError(t *testing.T) {
1325 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1326 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001327 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1328 cc_library {
1329 name: "libvndk",
1330 vendor_available: true,
1331 vndk: {
1332 enabled: true,
1333 },
1334 nocrt: true,
1335 }
1336
1337 cc_library {
1338 name: "libvndk_ext",
1339 vendor: true,
1340 vndk: {
1341 enabled: true,
1342 extends: "libvndk",
1343 },
1344 nocrt: true,
1345 }
1346
1347 cc_library {
1348 name: "libvndk2",
1349 vendor_available: true,
1350 vndk: {
1351 enabled: true,
1352 },
1353 shared_libs: ["libvndk_ext"],
1354 nocrt: true,
1355 }
1356 `)
1357
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001358 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001359 cc_library {
1360 name: "libvndk",
1361 vendor_available: true,
1362 vndk: {
1363 enabled: true,
1364 },
1365 nocrt: true,
1366 }
1367
1368 cc_library {
1369 name: "libvndk_ext",
1370 vendor: true,
1371 vndk: {
1372 enabled: true,
1373 extends: "libvndk",
1374 },
1375 nocrt: true,
1376 }
1377
1378 cc_library {
1379 name: "libvndk2",
1380 vendor_available: true,
1381 vndk: {
1382 enabled: true,
1383 },
1384 target: {
1385 vendor: {
1386 shared_libs: ["libvndk_ext"],
1387 },
1388 },
1389 nocrt: true,
1390 }
1391 `)
1392
1393 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1394 cc_library {
1395 name: "libvndk_sp",
1396 vendor_available: true,
1397 vndk: {
1398 enabled: true,
1399 support_system_process: true,
1400 },
1401 nocrt: true,
1402 }
1403
1404 cc_library {
1405 name: "libvndk_sp_ext",
1406 vendor: true,
1407 vndk: {
1408 enabled: true,
1409 extends: "libvndk_sp",
1410 support_system_process: true,
1411 },
1412 nocrt: true,
1413 }
1414
1415 cc_library {
1416 name: "libvndk_sp_2",
1417 vendor_available: true,
1418 vndk: {
1419 enabled: true,
1420 support_system_process: true,
1421 },
1422 shared_libs: ["libvndk_sp_ext"],
1423 nocrt: true,
1424 }
1425 `)
1426
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001427 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001428 cc_library {
1429 name: "libvndk_sp",
1430 vendor_available: true,
1431 vndk: {
1432 enabled: true,
1433 },
1434 nocrt: true,
1435 }
1436
1437 cc_library {
1438 name: "libvndk_sp_ext",
1439 vendor: true,
1440 vndk: {
1441 enabled: true,
1442 extends: "libvndk_sp",
1443 },
1444 nocrt: true,
1445 }
1446
1447 cc_library {
1448 name: "libvndk_sp2",
1449 vendor_available: true,
1450 vndk: {
1451 enabled: true,
1452 },
1453 target: {
1454 vendor: {
1455 shared_libs: ["libvndk_sp_ext"],
1456 },
1457 },
1458 nocrt: true,
1459 }
1460 `)
1461}
1462
Justin Yun5f7f7e82019-11-18 19:52:14 +09001463func TestEnforceProductVndkVersion(t *testing.T) {
1464 bp := `
1465 cc_library {
1466 name: "libllndk",
1467 }
1468 llndk_library {
1469 name: "libllndk",
1470 symbol_file: "",
1471 }
1472 cc_library {
1473 name: "libvndk",
1474 vendor_available: true,
1475 vndk: {
1476 enabled: true,
1477 },
1478 nocrt: true,
1479 }
1480 cc_library {
1481 name: "libvndk_sp",
1482 vendor_available: true,
1483 vndk: {
1484 enabled: true,
1485 support_system_process: true,
1486 },
1487 nocrt: true,
1488 }
1489 cc_library {
1490 name: "libva",
1491 vendor_available: true,
1492 nocrt: true,
1493 }
1494 cc_library {
1495 name: "libproduct_va",
1496 product_specific: true,
1497 vendor_available: true,
1498 nocrt: true,
1499 }
1500 cc_library {
1501 name: "libprod",
1502 product_specific: true,
1503 shared_libs: [
1504 "libllndk",
1505 "libvndk",
1506 "libvndk_sp",
1507 "libva",
1508 "libproduct_va",
1509 ],
1510 nocrt: true,
1511 }
1512 cc_library {
1513 name: "libvendor",
1514 vendor: true,
1515 shared_libs: [
1516 "libllndk",
1517 "libvndk",
1518 "libvndk_sp",
1519 "libva",
1520 "libproduct_va",
1521 ],
1522 nocrt: true,
1523 }
1524 `
1525
1526 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1527 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1528 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1529 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1530
1531 ctx := testCcWithConfig(t, config)
1532
1533 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "")
1534 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "")
1535}
1536
1537func TestEnforceProductVndkVersionErrors(t *testing.T) {
1538 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1539 cc_library {
1540 name: "libprod",
1541 product_specific: true,
1542 shared_libs: [
1543 "libvendor",
1544 ],
1545 nocrt: true,
1546 }
1547 cc_library {
1548 name: "libvendor",
1549 vendor: true,
1550 nocrt: true,
1551 }
1552 `)
1553 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1554 cc_library {
1555 name: "libprod",
1556 product_specific: true,
1557 shared_libs: [
1558 "libsystem",
1559 ],
1560 nocrt: true,
1561 }
1562 cc_library {
1563 name: "libsystem",
1564 nocrt: true,
1565 }
1566 `)
1567 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
1568 cc_library {
1569 name: "libprod",
1570 product_specific: true,
1571 shared_libs: [
1572 "libvndk_private",
1573 ],
1574 nocrt: true,
1575 }
1576 cc_library {
1577 name: "libvndk_private",
1578 vendor_available: false,
1579 vndk: {
1580 enabled: true,
1581 },
1582 nocrt: true,
1583 }
1584 `)
1585 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1586 cc_library {
1587 name: "libprod",
1588 product_specific: true,
1589 shared_libs: [
1590 "libsystem_ext",
1591 ],
1592 nocrt: true,
1593 }
1594 cc_library {
1595 name: "libsystem_ext",
1596 system_ext_specific: true,
1597 nocrt: true,
1598 }
1599 `)
1600 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
1601 cc_library {
1602 name: "libsystem",
1603 shared_libs: [
1604 "libproduct_va",
1605 ],
1606 nocrt: true,
1607 }
1608 cc_library {
1609 name: "libproduct_va",
1610 product_specific: true,
1611 vendor_available: true,
1612 nocrt: true,
1613 }
1614 `)
1615}
1616
Jooyung Han38002912019-05-16 04:01:54 +09001617func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001618 bp := `
1619 cc_library {
1620 name: "libvndk",
1621 vendor_available: true,
1622 vndk: {
1623 enabled: true,
1624 },
1625 }
1626 cc_library {
1627 name: "libvndksp",
1628 vendor_available: true,
1629 vndk: {
1630 enabled: true,
1631 support_system_process: true,
1632 },
1633 }
1634 cc_library {
1635 name: "libvndkprivate",
1636 vendor_available: false,
1637 vndk: {
1638 enabled: true,
1639 },
1640 }
1641 cc_library {
1642 name: "libvendor",
1643 vendor: true,
1644 }
1645 cc_library {
1646 name: "libvndkext",
1647 vendor: true,
1648 vndk: {
1649 enabled: true,
1650 extends: "libvndk",
1651 },
1652 }
1653 vndk_prebuilt_shared {
1654 name: "prevndk",
1655 version: "27",
1656 target_arch: "arm",
1657 binder32bit: true,
1658 vendor_available: true,
1659 vndk: {
1660 enabled: true,
1661 },
1662 arch: {
1663 arm: {
1664 srcs: ["liba.so"],
1665 },
1666 },
1667 }
1668 cc_library {
1669 name: "libllndk",
1670 }
1671 llndk_library {
1672 name: "libllndk",
1673 symbol_file: "",
1674 }
1675 cc_library {
1676 name: "libllndkprivate",
1677 }
1678 llndk_library {
1679 name: "libllndkprivate",
1680 vendor_available: false,
1681 symbol_file: "",
1682 }`
1683
1684 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09001685 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1686 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1687 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08001688 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09001689
Jooyung Han0302a842019-10-30 18:43:49 +09001690 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09001691 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09001692 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09001693 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09001694 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09001695 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09001696 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09001697 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09001698
Colin Crossfb0c16e2019-11-20 17:12:35 -08001699 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09001700
Jooyung Han38002912019-05-16 04:01:54 +09001701 tests := []struct {
1702 variant string
1703 name string
1704 expected string
1705 }{
1706 {vendorVariant, "libvndk", "native:vndk"},
1707 {vendorVariant, "libvndksp", "native:vndk"},
1708 {vendorVariant, "libvndkprivate", "native:vndk_private"},
1709 {vendorVariant, "libvendor", "native:vendor"},
1710 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09001711 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09001712 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09001713 {coreVariant, "libvndk", "native:platform"},
1714 {coreVariant, "libvndkprivate", "native:platform"},
1715 {coreVariant, "libllndk", "native:platform"},
1716 }
1717 for _, test := range tests {
1718 t.Run(test.name, func(t *testing.T) {
1719 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
1720 assertString(t, module.makeLinkType, test.expected)
1721 })
1722 }
1723}
1724
Colin Cross0af4b842015-04-30 16:36:18 -07001725var (
1726 str11 = "01234567891"
1727 str10 = str11[:10]
1728 str9 = str11[:9]
1729 str5 = str11[:5]
1730 str4 = str11[:4]
1731)
1732
1733var splitListForSizeTestCases = []struct {
1734 in []string
1735 out [][]string
1736 size int
1737}{
1738 {
1739 in: []string{str10},
1740 out: [][]string{{str10}},
1741 size: 10,
1742 },
1743 {
1744 in: []string{str9},
1745 out: [][]string{{str9}},
1746 size: 10,
1747 },
1748 {
1749 in: []string{str5},
1750 out: [][]string{{str5}},
1751 size: 10,
1752 },
1753 {
1754 in: []string{str11},
1755 out: nil,
1756 size: 10,
1757 },
1758 {
1759 in: []string{str10, str10},
1760 out: [][]string{{str10}, {str10}},
1761 size: 10,
1762 },
1763 {
1764 in: []string{str9, str10},
1765 out: [][]string{{str9}, {str10}},
1766 size: 10,
1767 },
1768 {
1769 in: []string{str10, str9},
1770 out: [][]string{{str10}, {str9}},
1771 size: 10,
1772 },
1773 {
1774 in: []string{str5, str4},
1775 out: [][]string{{str5, str4}},
1776 size: 10,
1777 },
1778 {
1779 in: []string{str5, str4, str5},
1780 out: [][]string{{str5, str4}, {str5}},
1781 size: 10,
1782 },
1783 {
1784 in: []string{str5, str4, str5, str4},
1785 out: [][]string{{str5, str4}, {str5, str4}},
1786 size: 10,
1787 },
1788 {
1789 in: []string{str5, str4, str5, str5},
1790 out: [][]string{{str5, str4}, {str5}, {str5}},
1791 size: 10,
1792 },
1793 {
1794 in: []string{str5, str5, str5, str4},
1795 out: [][]string{{str5}, {str5}, {str5, str4}},
1796 size: 10,
1797 },
1798 {
1799 in: []string{str9, str11},
1800 out: nil,
1801 size: 10,
1802 },
1803 {
1804 in: []string{str11, str9},
1805 out: nil,
1806 size: 10,
1807 },
1808}
1809
1810func TestSplitListForSize(t *testing.T) {
1811 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08001812 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07001813
1814 var outStrings [][]string
1815
1816 if len(out) > 0 {
1817 outStrings = make([][]string, len(out))
1818 for i, o := range out {
1819 outStrings[i] = o.Strings()
1820 }
1821 }
1822
1823 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07001824 t.Errorf("incorrect output:")
1825 t.Errorf(" input: %#v", testCase.in)
1826 t.Errorf(" size: %d", testCase.size)
1827 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07001828 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07001829 }
1830 }
1831}
Jeff Gaston294356f2017-09-27 17:05:30 -07001832
1833var staticLinkDepOrderTestCases = []struct {
1834 // This is a string representation of a map[moduleName][]moduleDependency .
1835 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001836 inStatic string
1837
1838 // This is a string representation of a map[moduleName][]moduleDependency .
1839 // It models the dependencies declared in an Android.bp file.
1840 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07001841
1842 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
1843 // The keys of allOrdered specify which modules we would like to check.
1844 // The values of allOrdered specify the expected result (of the transitive closure of all
1845 // dependencies) for each module to test
1846 allOrdered string
1847
1848 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
1849 // The keys of outOrdered specify which modules we would like to check.
1850 // The values of outOrdered specify the expected result (of the ordered linker command line)
1851 // for each module to test.
1852 outOrdered string
1853}{
1854 // Simple tests
1855 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001856 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07001857 outOrdered: "",
1858 },
1859 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001860 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001861 outOrdered: "a:",
1862 },
1863 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001864 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001865 outOrdered: "a:b; b:",
1866 },
1867 // Tests of reordering
1868 {
1869 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001870 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001871 outOrdered: "a:b,c,d; b:d; c:d; d:",
1872 },
1873 {
1874 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001875 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001876 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
1877 },
1878 {
1879 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001880 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07001881 outOrdered: "a:d,b,e,c; d:b; e:c",
1882 },
1883 {
1884 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001885 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07001886 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
1887 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
1888 },
1889 {
1890 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001891 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 -07001892 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
1893 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
1894 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001895 // shared dependencies
1896 {
1897 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
1898 // So, we don't actually have to check that a shared dependency of c will change the order
1899 // of a library that depends statically on b and on c. We only need to check that if c has
1900 // a shared dependency on b, that that shows up in allOrdered.
1901 inShared: "c:b",
1902 allOrdered: "c:b",
1903 outOrdered: "c:",
1904 },
1905 {
1906 // This test doesn't actually include any shared dependencies but it's a reminder of what
1907 // the second phase of the above test would look like
1908 inStatic: "a:b,c; c:b",
1909 allOrdered: "a:c,b; c:b",
1910 outOrdered: "a:c,b; c:b",
1911 },
Jeff Gaston294356f2017-09-27 17:05:30 -07001912 // tiebreakers for when two modules specifying different orderings and there is no dependency
1913 // to dictate an order
1914 {
1915 // 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 -08001916 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07001917 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
1918 },
1919 {
1920 // 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 -08001921 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 -07001922 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
1923 },
1924 // Tests involving duplicate dependencies
1925 {
1926 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001927 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001928 outOrdered: "a:c,b",
1929 },
1930 {
1931 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001932 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001933 outOrdered: "a:d,c,b",
1934 },
1935 // Tests to confirm the nonexistence of infinite loops.
1936 // These cases should never happen, so as long as the test terminates and the
1937 // result is deterministic then that should be fine.
1938 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001939 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07001940 outOrdered: "a:a",
1941 },
1942 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001943 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07001944 allOrdered: "a:b,c; b:c,a; c:a,b",
1945 outOrdered: "a:b; b:c; c:a",
1946 },
1947 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001948 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001949 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
1950 outOrdered: "a:c,b; b:a,c; c:b,a",
1951 },
1952}
1953
1954// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
1955func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
1956 // convert from "a:b,c; d:e" to "a:b,c;d:e"
1957 strippedText := strings.Replace(text, " ", "", -1)
1958 if len(strippedText) < 1 {
1959 return []android.Path{}, make(map[android.Path][]android.Path, 0)
1960 }
1961 allDeps = make(map[android.Path][]android.Path, 0)
1962
1963 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
1964 moduleTexts := strings.Split(strippedText, ";")
1965
1966 outputForModuleName := func(moduleName string) android.Path {
1967 return android.PathForTesting(moduleName)
1968 }
1969
1970 for _, moduleText := range moduleTexts {
1971 // convert from "a:b,c" to ["a", "b,c"]
1972 components := strings.Split(moduleText, ":")
1973 if len(components) != 2 {
1974 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
1975 }
1976 moduleName := components[0]
1977 moduleOutput := outputForModuleName(moduleName)
1978 modulesInOrder = append(modulesInOrder, moduleOutput)
1979
1980 depString := components[1]
1981 // convert from "b,c" to ["b", "c"]
1982 depNames := strings.Split(depString, ",")
1983 if len(depString) < 1 {
1984 depNames = []string{}
1985 }
1986 var deps []android.Path
1987 for _, depName := range depNames {
1988 deps = append(deps, outputForModuleName(depName))
1989 }
1990 allDeps[moduleOutput] = deps
1991 }
1992 return modulesInOrder, allDeps
1993}
1994
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001995func TestLinkReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001996 for _, testCase := range staticLinkDepOrderTestCases {
1997 errs := []string{}
1998
1999 // parse testcase
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002000 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)
Jeff Gaston294356f2017-09-27 17:05:30 -07002001 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)
2002 if testCase.allOrdered == "" {
2003 // allow the test case to skip specifying allOrdered
2004 testCase.allOrdered = testCase.outOrdered
2005 }
2006 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002007 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)
Jeff Gaston294356f2017-09-27 17:05:30 -07002008
2009 // For each module whose post-reordered dependencies were specified, validate that
2010 // reordering the inputs produces the expected outputs.
2011 for _, moduleName := range expectedModuleNames {
2012 moduleDeps := givenTransitiveDeps[moduleName]
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002013 givenSharedDeps := givenAllSharedDeps[moduleName]
2014 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -07002015
2016 correctAllOrdered := expectedAllDeps[moduleName]
2017 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {
2018 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002019 "\nin static:%q"+
2020 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002021 "\nmodule: %v"+
2022 "\nexpected: %s"+
2023 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002024 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002025 }
2026
2027 correctOutputDeps := expectedTransitiveDeps[moduleName]
2028 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {
2029 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002030 "\nin static:%q"+
2031 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002032 "\nmodule: %v"+
2033 "\nexpected: %s"+
2034 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002035 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002036 }
2037 }
2038
2039 if len(errs) > 0 {
2040 sort.Strings(errs)
2041 for _, err := range errs {
2042 t.Error(err)
2043 }
2044 }
2045 }
2046}
Logan Chienf3511742017-10-31 18:04:35 +08002047
Jeff Gaston294356f2017-09-27 17:05:30 -07002048func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2049 for _, moduleName := range moduleNames {
2050 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
2051 output := module.outputFile.Path()
2052 paths = append(paths, output)
2053 }
2054 return paths
2055}
2056
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002057func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002058 ctx := testCc(t, `
2059 cc_library {
2060 name: "a",
2061 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002062 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002063 }
2064 cc_library {
2065 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002066 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002067 }
2068 cc_library {
2069 name: "c",
2070 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002071 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002072 }
2073 cc_library {
2074 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002075 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002076 }
2077
2078 `)
2079
Colin Cross7113d202019-11-20 16:39:12 -08002080 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002081 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002082 actual := moduleA.depsInLinkOrder
Jeff Gaston294356f2017-09-27 17:05:30 -07002083 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})
2084
2085 if !reflect.DeepEqual(actual, expected) {
2086 t.Errorf("staticDeps orderings were not propagated correctly"+
2087 "\nactual: %v"+
2088 "\nexpected: %v",
2089 actual,
2090 expected,
2091 )
2092 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002093}
Jeff Gaston294356f2017-09-27 17:05:30 -07002094
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002095func TestStaticLibDepReorderingWithShared(t *testing.T) {
2096 ctx := testCc(t, `
2097 cc_library {
2098 name: "a",
2099 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002100 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002101 }
2102 cc_library {
2103 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002104 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002105 }
2106 cc_library {
2107 name: "c",
2108 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002109 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002110 }
2111
2112 `)
2113
Colin Cross7113d202019-11-20 16:39:12 -08002114 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002115 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
2116 actual := moduleA.depsInLinkOrder
2117 expected := getOutputPaths(ctx, variant, []string{"c", "b"})
2118
2119 if !reflect.DeepEqual(actual, expected) {
2120 t.Errorf("staticDeps orderings did not account for shared libs"+
2121 "\nactual: %v"+
2122 "\nexpected: %v",
2123 actual,
2124 expected,
2125 )
2126 }
2127}
2128
Jiyong Parka46a4d52017-12-14 19:54:34 +09002129func TestLlndkHeaders(t *testing.T) {
2130 ctx := testCc(t, `
2131 llndk_headers {
2132 name: "libllndk_headers",
2133 export_include_dirs: ["my_include"],
2134 }
2135 llndk_library {
2136 name: "libllndk",
2137 export_llndk_headers: ["libllndk_headers"],
2138 }
2139 cc_library {
2140 name: "libvendor",
2141 shared_libs: ["libllndk"],
2142 vendor: true,
2143 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002144 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002145 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002146 }
2147 `)
2148
2149 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002150 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002151 cflags := cc.Args["cFlags"]
2152 if !strings.Contains(cflags, "-Imy_include") {
2153 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2154 }
2155}
2156
Logan Chien43d34c32017-12-20 01:17:32 +08002157func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2158 actual := module.Properties.AndroidMkRuntimeLibs
2159 if !reflect.DeepEqual(actual, expected) {
2160 t.Errorf("incorrect runtime_libs for shared libs"+
2161 "\nactual: %v"+
2162 "\nexpected: %v",
2163 actual,
2164 expected,
2165 )
2166 }
2167}
2168
2169const runtimeLibAndroidBp = `
2170 cc_library {
2171 name: "libvendor_available1",
2172 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002173 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002174 nocrt : true,
2175 system_shared_libs : [],
2176 }
2177 cc_library {
2178 name: "libvendor_available2",
2179 vendor_available: true,
2180 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002181 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002182 nocrt : true,
2183 system_shared_libs : [],
2184 }
2185 cc_library {
2186 name: "libvendor_available3",
2187 vendor_available: true,
2188 runtime_libs: ["libvendor_available1"],
2189 target: {
2190 vendor: {
2191 exclude_runtime_libs: ["libvendor_available1"],
2192 }
2193 },
Yi Konge7fe9912019-06-02 00:53:50 -07002194 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002195 nocrt : true,
2196 system_shared_libs : [],
2197 }
2198 cc_library {
2199 name: "libcore",
2200 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002201 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002202 nocrt : true,
2203 system_shared_libs : [],
2204 }
2205 cc_library {
2206 name: "libvendor1",
2207 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002208 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002209 nocrt : true,
2210 system_shared_libs : [],
2211 }
2212 cc_library {
2213 name: "libvendor2",
2214 vendor: true,
2215 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002216 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002217 nocrt : true,
2218 system_shared_libs : [],
2219 }
2220`
2221
2222func TestRuntimeLibs(t *testing.T) {
2223 ctx := testCc(t, runtimeLibAndroidBp)
2224
2225 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002226 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002227
2228 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2229 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2230
2231 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
2232 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2233
2234 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2235 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002236 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002237
2238 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2239 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
2240
2241 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2242 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
2243}
2244
2245func TestExcludeRuntimeLibs(t *testing.T) {
2246 ctx := testCc(t, runtimeLibAndroidBp)
2247
Colin Cross7113d202019-11-20 16:39:12 -08002248 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002249 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2250 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2251
Colin Crossfb0c16e2019-11-20 17:12:35 -08002252 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002253 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2254 checkRuntimeLibs(t, nil, module)
2255}
2256
2257func TestRuntimeLibsNoVndk(t *testing.T) {
2258 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2259
2260 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2261
Colin Cross7113d202019-11-20 16:39:12 -08002262 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002263
2264 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2265 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2266
2267 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2268 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
2269}
2270
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002271func checkStaticLibs(t *testing.T, expected []string, module *Module) {
2272 actual := module.Properties.AndroidMkStaticLibs
2273 if !reflect.DeepEqual(actual, expected) {
2274 t.Errorf("incorrect static_libs"+
2275 "\nactual: %v"+
2276 "\nexpected: %v",
2277 actual,
2278 expected,
2279 )
2280 }
2281}
2282
2283const staticLibAndroidBp = `
2284 cc_library {
2285 name: "lib1",
2286 }
2287 cc_library {
2288 name: "lib2",
2289 static_libs: ["lib1"],
2290 }
2291`
2292
2293func TestStaticLibDepExport(t *testing.T) {
2294 ctx := testCc(t, staticLibAndroidBp)
2295
2296 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002297 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002298 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Dan Albert2da19cb2019-07-24 12:17:40 -07002299 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc_stripped"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002300
2301 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002302 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002303 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2304 // libc++_static is linked additionally.
Dan Albert2da19cb2019-07-24 12:17:40 -07002305 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc_stripped"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002306}
2307
Jiyong Parkd08b6972017-09-26 10:50:54 +09002308var compilerFlagsTestCases = []struct {
2309 in string
2310 out bool
2311}{
2312 {
2313 in: "a",
2314 out: false,
2315 },
2316 {
2317 in: "-a",
2318 out: true,
2319 },
2320 {
2321 in: "-Ipath/to/something",
2322 out: false,
2323 },
2324 {
2325 in: "-isystempath/to/something",
2326 out: false,
2327 },
2328 {
2329 in: "--coverage",
2330 out: false,
2331 },
2332 {
2333 in: "-include a/b",
2334 out: true,
2335 },
2336 {
2337 in: "-include a/b c/d",
2338 out: false,
2339 },
2340 {
2341 in: "-DMACRO",
2342 out: true,
2343 },
2344 {
2345 in: "-DMAC RO",
2346 out: false,
2347 },
2348 {
2349 in: "-a -b",
2350 out: false,
2351 },
2352 {
2353 in: "-DMACRO=definition",
2354 out: true,
2355 },
2356 {
2357 in: "-DMACRO=defi nition",
2358 out: true, // TODO(jiyong): this should be false
2359 },
2360 {
2361 in: "-DMACRO(x)=x + 1",
2362 out: true,
2363 },
2364 {
2365 in: "-DMACRO=\"defi nition\"",
2366 out: true,
2367 },
2368}
2369
2370type mockContext struct {
2371 BaseModuleContext
2372 result bool
2373}
2374
2375func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2376 // CheckBadCompilerFlags calls this function when the flag should be rejected
2377 ctx.result = false
2378}
2379
2380func TestCompilerFlags(t *testing.T) {
2381 for _, testCase := range compilerFlagsTestCases {
2382 ctx := &mockContext{result: true}
2383 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
2384 if ctx.result != testCase.out {
2385 t.Errorf("incorrect output:")
2386 t.Errorf(" input: %#v", testCase.in)
2387 t.Errorf(" expected: %#v", testCase.out)
2388 t.Errorf(" got: %#v", ctx.result)
2389 }
2390 }
Jeff Gaston294356f2017-09-27 17:05:30 -07002391}
Jiyong Park374510b2018-03-19 18:23:01 +09002392
2393func TestVendorPublicLibraries(t *testing.T) {
2394 ctx := testCc(t, `
2395 cc_library_headers {
2396 name: "libvendorpublic_headers",
2397 export_include_dirs: ["my_include"],
2398 }
2399 vendor_public_library {
2400 name: "libvendorpublic",
2401 symbol_file: "",
2402 export_public_headers: ["libvendorpublic_headers"],
2403 }
2404 cc_library {
2405 name: "libvendorpublic",
2406 srcs: ["foo.c"],
2407 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002408 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002409 nocrt: true,
2410 }
2411
2412 cc_library {
2413 name: "libsystem",
2414 shared_libs: ["libvendorpublic"],
2415 vendor: false,
2416 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002417 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002418 nocrt: true,
2419 }
2420 cc_library {
2421 name: "libvendor",
2422 shared_libs: ["libvendorpublic"],
2423 vendor: true,
2424 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002425 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002426 nocrt: true,
2427 }
2428 `)
2429
Colin Cross7113d202019-11-20 16:39:12 -08002430 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08002431 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09002432
2433 // test if header search paths are correctly added
2434 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08002435 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09002436 cflags := cc.Args["cFlags"]
2437 if !strings.Contains(cflags, "-Imy_include") {
2438 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
2439 }
2440
2441 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08002442 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002443 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002444 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09002445 if !strings.Contains(libflags, stubPaths[0].String()) {
2446 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
2447 }
2448
2449 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08002450 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002451 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002452 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09002453 if !strings.Contains(libflags, stubPaths[0].String()) {
2454 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
2455 }
2456
2457}
Jiyong Park37b25202018-07-11 10:49:27 +09002458
2459func TestRecovery(t *testing.T) {
2460 ctx := testCc(t, `
2461 cc_library_shared {
2462 name: "librecovery",
2463 recovery: true,
2464 }
2465 cc_library_shared {
2466 name: "librecovery32",
2467 recovery: true,
2468 compile_multilib:"32",
2469 }
Jiyong Park5baac542018-08-28 09:55:37 +09002470 cc_library_shared {
2471 name: "libHalInRecovery",
2472 recovery_available: true,
2473 vendor: true,
2474 }
Jiyong Park37b25202018-07-11 10:49:27 +09002475 `)
2476
2477 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08002478 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09002479 if len(variants) != 1 || !android.InList(arm64, variants) {
2480 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
2481 }
2482
2483 variants = ctx.ModuleVariantsForTests("librecovery32")
2484 if android.InList(arm64, variants) {
2485 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
2486 }
Jiyong Park5baac542018-08-28 09:55:37 +09002487
2488 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
2489 if !recoveryModule.Platform() {
2490 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
2491 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09002492}
Jiyong Park5baac542018-08-28 09:55:37 +09002493
Jiyong Park7ed9de32018-10-15 22:25:07 +09002494func TestVersionedStubs(t *testing.T) {
2495 ctx := testCc(t, `
2496 cc_library_shared {
2497 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002498 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002499 stubs: {
2500 symbol_file: "foo.map.txt",
2501 versions: ["1", "2", "3"],
2502 },
2503 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002504
Jiyong Park7ed9de32018-10-15 22:25:07 +09002505 cc_library_shared {
2506 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002507 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002508 shared_libs: ["libFoo#1"],
2509 }`)
2510
2511 variants := ctx.ModuleVariantsForTests("libFoo")
2512 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08002513 "android_arm64_armv8-a_shared",
2514 "android_arm64_armv8-a_shared_1",
2515 "android_arm64_armv8-a_shared_2",
2516 "android_arm64_armv8-a_shared_3",
2517 "android_arm_armv7-a-neon_shared",
2518 "android_arm_armv7-a-neon_shared_1",
2519 "android_arm_armv7-a-neon_shared_2",
2520 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09002521 }
2522 variantsMismatch := false
2523 if len(variants) != len(expectedVariants) {
2524 variantsMismatch = true
2525 } else {
2526 for _, v := range expectedVariants {
2527 if !inList(v, variants) {
2528 variantsMismatch = false
2529 }
2530 }
2531 }
2532 if variantsMismatch {
2533 t.Errorf("variants of libFoo expected:\n")
2534 for _, v := range expectedVariants {
2535 t.Errorf("%q\n", v)
2536 }
2537 t.Errorf(", but got:\n")
2538 for _, v := range variants {
2539 t.Errorf("%q\n", v)
2540 }
2541 }
2542
Colin Cross7113d202019-11-20 16:39:12 -08002543 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09002544 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002545 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09002546 if !strings.Contains(libFlags, libFoo1StubPath) {
2547 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
2548 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002549
Colin Cross7113d202019-11-20 16:39:12 -08002550 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09002551 cFlags := libBarCompileRule.Args["cFlags"]
2552 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
2553 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
2554 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
2555 }
Jiyong Park37b25202018-07-11 10:49:27 +09002556}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002557
2558func TestStaticExecutable(t *testing.T) {
2559 ctx := testCc(t, `
2560 cc_binary {
2561 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01002562 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002563 static_executable: true,
2564 }`)
2565
Colin Cross7113d202019-11-20 16:39:12 -08002566 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002567 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
2568 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07002569 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002570 for _, lib := range systemStaticLibs {
2571 if !strings.Contains(libFlags, lib) {
2572 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
2573 }
2574 }
2575 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
2576 for _, lib := range systemSharedLibs {
2577 if strings.Contains(libFlags, lib) {
2578 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
2579 }
2580 }
2581}
Jiyong Parke4bb9862019-02-01 00:31:10 +09002582
2583func TestStaticDepsOrderWithStubs(t *testing.T) {
2584 ctx := testCc(t, `
2585 cc_binary {
2586 name: "mybin",
2587 srcs: ["foo.c"],
2588 static_libs: ["libB"],
2589 static_executable: true,
2590 stl: "none",
2591 }
2592
2593 cc_library {
2594 name: "libB",
2595 srcs: ["foo.c"],
2596 shared_libs: ["libC"],
2597 stl: "none",
2598 }
2599
2600 cc_library {
2601 name: "libC",
2602 srcs: ["foo.c"],
2603 stl: "none",
2604 stubs: {
2605 versions: ["1"],
2606 },
2607 }`)
2608
Colin Cross7113d202019-11-20 16:39:12 -08002609 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Module().(*Module)
Jiyong Parke4bb9862019-02-01 00:31:10 +09002610 actual := mybin.depsInLinkOrder
Colin Cross7113d202019-11-20 16:39:12 -08002611 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libB", "libC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09002612
2613 if !reflect.DeepEqual(actual, expected) {
2614 t.Errorf("staticDeps orderings were not propagated correctly"+
2615 "\nactual: %v"+
2616 "\nexpected: %v",
2617 actual,
2618 expected,
2619 )
2620 }
2621}
Jooyung Han38002912019-05-16 04:01:54 +09002622
Jooyung Hand48f3c32019-08-23 11:18:57 +09002623func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
2624 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
2625 cc_library {
2626 name: "libA",
2627 srcs: ["foo.c"],
2628 shared_libs: ["libB"],
2629 stl: "none",
2630 }
2631
2632 cc_library {
2633 name: "libB",
2634 srcs: ["foo.c"],
2635 enabled: false,
2636 stl: "none",
2637 }
2638 `)
2639}
2640
Mitch Phillipsda9a4632019-07-15 09:34:09 -07002641// Simple smoke test for the cc_fuzz target that ensures the rule compiles
2642// correctly.
2643func TestFuzzTarget(t *testing.T) {
2644 ctx := testCc(t, `
2645 cc_fuzz {
2646 name: "fuzz_smoke_test",
2647 srcs: ["foo.c"],
2648 }`)
2649
Paul Duffin075c4172019-12-19 19:06:13 +00002650 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07002651 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
2652}
2653
Jiyong Park29074592019-07-07 16:27:47 +09002654func TestAidl(t *testing.T) {
2655}
2656
Jooyung Han38002912019-05-16 04:01:54 +09002657func assertString(t *testing.T, got, expected string) {
2658 t.Helper()
2659 if got != expected {
2660 t.Errorf("expected %q got %q", expected, got)
2661 }
2662}
2663
2664func assertArrayString(t *testing.T, got, expected []string) {
2665 t.Helper()
2666 if len(got) != len(expected) {
2667 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
2668 return
2669 }
2670 for i := range got {
2671 if got[i] != expected[i] {
2672 t.Errorf("expected %d-th %q (%q) got %q (%q)",
2673 i, expected[i], expected, got[i], got)
2674 return
2675 }
2676 }
2677}
Colin Crosse1bb5d02019-09-24 14:55:04 -07002678
Jooyung Han0302a842019-10-30 18:43:49 +09002679func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
2680 t.Helper()
2681 assertArrayString(t, android.SortedStringKeys(m), expected)
2682}
2683
Colin Crosse1bb5d02019-09-24 14:55:04 -07002684func TestDefaults(t *testing.T) {
2685 ctx := testCc(t, `
2686 cc_defaults {
2687 name: "defaults",
2688 srcs: ["foo.c"],
2689 static: {
2690 srcs: ["bar.c"],
2691 },
2692 shared: {
2693 srcs: ["baz.c"],
2694 },
2695 }
2696
2697 cc_library_static {
2698 name: "libstatic",
2699 defaults: ["defaults"],
2700 }
2701
2702 cc_library_shared {
2703 name: "libshared",
2704 defaults: ["defaults"],
2705 }
2706
2707 cc_library {
2708 name: "libboth",
2709 defaults: ["defaults"],
2710 }
2711
2712 cc_binary {
2713 name: "binary",
2714 defaults: ["defaults"],
2715 }`)
2716
2717 pathsToBase := func(paths android.Paths) []string {
2718 var ret []string
2719 for _, p := range paths {
2720 ret = append(ret, p.Base())
2721 }
2722 return ret
2723 }
2724
Colin Cross7113d202019-11-20 16:39:12 -08002725 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002726 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
2727 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
2728 }
Colin Cross7113d202019-11-20 16:39:12 -08002729 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002730 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
2731 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
2732 }
Colin Cross7113d202019-11-20 16:39:12 -08002733 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002734 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
2735 t.Errorf("binary ld rule wanted %q, got %q", w, g)
2736 }
2737
Colin Cross7113d202019-11-20 16:39:12 -08002738 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002739 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
2740 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
2741 }
Colin Cross7113d202019-11-20 16:39:12 -08002742 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002743 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
2744 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
2745 }
2746}