blob: 27ff38fb17d7ba2abc2bfca63d9952d98d376d6a [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
85func testCcError(t *testing.T, pattern string, bp string) {
Logan Chiend3c59a22018-03-29 14:08:15 +080086 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080087 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070088 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
89 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +080090
Colin Cross98be1bb2019-12-13 20:41:13 -080091 ctx := CreateTestContext()
92 ctx.Register(config)
Logan Chienf3511742017-10-31 18:04:35 +080093
94 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
95 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +080096 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +080097 return
98 }
99
100 _, errs = ctx.PrepareBuildActions(config)
101 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +0800102 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800103 return
104 }
105
106 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
107}
108
109const (
Colin Cross7113d202019-11-20 16:39:12 -0800110 coreVariant = "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800111 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
112 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800113)
114
Doug Hornc32c6b02019-01-17 14:44:05 -0800115func TestFuchsiaDeps(t *testing.T) {
116 t.Helper()
117
118 bp := `
119 cc_library {
120 name: "libTest",
121 srcs: ["foo.c"],
122 target: {
123 fuchsia: {
124 srcs: ["bar.c"],
125 },
126 },
127 }`
128
Colin Cross98be1bb2019-12-13 20:41:13 -0800129 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
130 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800131
132 rt := false
133 fb := false
134
135 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
136 implicits := ld.Implicits
137 for _, lib := range implicits {
138 if strings.Contains(lib.Rel(), "libcompiler_rt") {
139 rt = true
140 }
141
142 if strings.Contains(lib.Rel(), "libbioniccompat") {
143 fb = true
144 }
145 }
146
147 if !rt || !fb {
148 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
149 }
150}
151
152func TestFuchsiaTargetDecl(t *testing.T) {
153 t.Helper()
154
155 bp := `
156 cc_library {
157 name: "libTest",
158 srcs: ["foo.c"],
159 target: {
160 fuchsia: {
161 srcs: ["bar.c"],
162 },
163 },
164 }`
165
Colin Cross98be1bb2019-12-13 20:41:13 -0800166 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
167 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800168 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
169 var objs []string
170 for _, o := range ld.Inputs {
171 objs = append(objs, o.Base())
172 }
173 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
174 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
175 }
176}
177
Jiyong Park6a43f042017-10-12 23:05:00 +0900178func TestVendorSrc(t *testing.T) {
179 ctx := testCc(t, `
180 cc_library {
181 name: "libTest",
182 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700183 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800184 nocrt: true,
185 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900186 vendor_available: true,
187 target: {
188 vendor: {
189 srcs: ["bar.c"],
190 },
191 },
192 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900193 `)
194
Logan Chienf3511742017-10-31 18:04:35 +0800195 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900196 var objs []string
197 for _, o := range ld.Inputs {
198 objs = append(objs, o.Base())
199 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800200 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900201 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
202 }
203}
204
Logan Chienf3511742017-10-31 18:04:35 +0800205func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
206 isVndkSp bool, extends string) {
207
Logan Chiend3c59a22018-03-29 14:08:15 +0800208 t.Helper()
209
Logan Chienf3511742017-10-31 18:04:35 +0800210 mod := ctx.ModuleForTests(name, vendorVariant).Module().(*Module)
Ivan Lozano52767be2019-10-18 14:49:46 -0700211 if !mod.HasVendorVariant() {
Colin Crossf46e37f2018-03-21 16:25:58 -0700212 t.Errorf("%q must have vendor variant", name)
Logan Chienf3511742017-10-31 18:04:35 +0800213 }
214
215 // Check library properties.
216 lib, ok := mod.compiler.(*libraryDecorator)
217 if !ok {
218 t.Errorf("%q must have libraryDecorator", name)
219 } else if lib.baseInstaller.subDir != subDir {
220 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
221 lib.baseInstaller.subDir)
222 }
223
224 // Check VNDK properties.
225 if mod.vndkdep == nil {
226 t.Fatalf("%q must have `vndkdep`", name)
227 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700228 if !mod.IsVndk() {
229 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800230 }
231 if mod.isVndkSp() != isVndkSp {
232 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
233 }
234
235 // Check VNDK extension properties.
236 isVndkExt := extends != ""
237 if mod.isVndkExt() != isVndkExt {
238 t.Errorf("%q isVndkExt() must equal to %t", name, isVndkExt)
239 }
240
241 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
242 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
243 }
244}
245
Jooyung Han39edb6c2019-11-06 16:53:07 +0900246func checkVndkSnapshot(t *testing.T, ctx *android.TestContext, moduleName, snapshotFilename, subDir, variant string) {
Inseob Kim1f086e22019-05-09 13:29:15 +0900247 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
248
Jooyung Han39edb6c2019-11-06 16:53:07 +0900249 mod, ok := ctx.ModuleForTests(moduleName, variant).Module().(android.OutputFileProducer)
250 if !ok {
251 t.Errorf("%q must have output\n", moduleName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900252 return
253 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900254 outputFiles, err := mod.OutputFiles("")
255 if err != nil || len(outputFiles) != 1 {
256 t.Errorf("%q must have single output\n", moduleName)
257 return
258 }
259 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900260
261 out := vndkSnapshot.Output(snapshotPath)
Jooyung Han39edb6c2019-11-06 16:53:07 +0900262 if out.Input.String() != outputFiles[0].String() {
263 t.Errorf("The input of VNDK snapshot must be %q, but %q", out.Input.String(), outputFiles[0])
Inseob Kim1f086e22019-05-09 13:29:15 +0900264 }
265}
266
Jooyung Han2216fb12019-11-06 16:46:15 +0900267func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
268 t.Helper()
269 assertString(t, params.Rule.String(), android.WriteFile.String())
270 actual := strings.FieldsFunc(strings.ReplaceAll(params.Args["content"], "\\n", "\n"), func(r rune) bool { return r == '\n' })
271 assertArrayString(t, actual, expected)
272}
273
Jooyung Han097087b2019-10-22 19:32:18 +0900274func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
275 t.Helper()
276 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900277 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
278}
279
280func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
281 t.Helper()
282 vndkLibraries := ctx.ModuleForTests(module, "")
283 output := insertVndkVersion(module, "VER")
284 checkWriteFileOutput(t, vndkLibraries.Output(output), expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900285}
286
Logan Chienf3511742017-10-31 18:04:35 +0800287func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800288 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800289 cc_library {
290 name: "libvndk",
291 vendor_available: true,
292 vndk: {
293 enabled: true,
294 },
295 nocrt: true,
296 }
297
298 cc_library {
299 name: "libvndk_private",
300 vendor_available: false,
301 vndk: {
302 enabled: true,
303 },
304 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900305 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800306 }
307
308 cc_library {
309 name: "libvndk_sp",
310 vendor_available: true,
311 vndk: {
312 enabled: true,
313 support_system_process: true,
314 },
315 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900316 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800317 }
318
319 cc_library {
320 name: "libvndk_sp_private",
321 vendor_available: false,
322 vndk: {
323 enabled: true,
324 support_system_process: true,
325 },
326 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900327 target: {
328 vendor: {
329 suffix: "-x",
330 },
331 },
Logan Chienf3511742017-10-31 18:04:35 +0800332 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900333 vndk_libraries_txt {
334 name: "llndk.libraries.txt",
335 }
336 vndk_libraries_txt {
337 name: "vndkcore.libraries.txt",
338 }
339 vndk_libraries_txt {
340 name: "vndksp.libraries.txt",
341 }
342 vndk_libraries_txt {
343 name: "vndkprivate.libraries.txt",
344 }
345 vndk_libraries_txt {
346 name: "vndkcorevariant.libraries.txt",
347 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800348 `
349
350 config := TestConfig(buildDir, android.Android, nil, bp, nil)
351 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
352 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
353
354 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800355
356 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "")
357 checkVndkModule(t, ctx, "libvndk_private", "vndk-VER", false, "")
358 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "")
359 checkVndkModule(t, ctx, "libvndk_sp_private", "vndk-sp-VER", true, "")
Inseob Kim1f086e22019-05-09 13:29:15 +0900360
361 // Check VNDK snapshot output.
362
363 snapshotDir := "vndk-snapshot"
364 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
365
366 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
367 "arm64", "armv8-a"))
368 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
369 "arm", "armv7-a-neon"))
370
371 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
372 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
373 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
374 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
375
Colin Crossfb0c16e2019-11-20 17:12:35 -0800376 variant := "android_vendor.VER_arm64_armv8-a_shared"
377 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900378
Jooyung Han39edb6c2019-11-06 16:53:07 +0900379 checkVndkSnapshot(t, ctx, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
380 checkVndkSnapshot(t, ctx, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
381 checkVndkSnapshot(t, ctx, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
382 checkVndkSnapshot(t, ctx, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900383
Jooyung Han39edb6c2019-11-06 16:53:07 +0900384 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
385 checkVndkSnapshot(t, ctx, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
386 checkVndkSnapshot(t, ctx, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
387 checkVndkSnapshot(t, ctx, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
388 checkVndkSnapshot(t, ctx, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
389
Jooyung Han097087b2019-10-22 19:32:18 +0900390 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
391 "LLNDK: libc.so",
392 "LLNDK: libdl.so",
393 "LLNDK: libft2.so",
394 "LLNDK: libm.so",
395 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900396 "VNDK-SP: libvndk_sp-x.so",
397 "VNDK-SP: libvndk_sp_private-x.so",
398 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900399 "VNDK-core: libvndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900400 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900401 "VNDK-private: libvndk-private.so",
402 "VNDK-private: libvndk_sp_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900403 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900404 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
405 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so"})
406 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so"})
407 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so"})
408 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
409}
410
411func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800412 bp := `
Jooyung Han2216fb12019-11-06 16:46:15 +0900413 vndk_libraries_txt {
414 name: "llndk.libraries.txt",
Colin Cross98be1bb2019-12-13 20:41:13 -0800415 }`
416 config := TestConfig(buildDir, android.Android, nil, bp, nil)
417 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
418 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
419 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900420
421 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900422 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900423 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900424}
425
426func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800427 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900428 cc_library {
429 name: "libvndk",
430 vendor_available: true,
431 vndk: {
432 enabled: true,
433 },
434 nocrt: true,
435 }
436
437 cc_library {
438 name: "libvndk_sp",
439 vendor_available: true,
440 vndk: {
441 enabled: true,
442 support_system_process: true,
443 },
444 nocrt: true,
445 }
446
447 cc_library {
448 name: "libvndk2",
449 vendor_available: false,
450 vndk: {
451 enabled: true,
452 },
453 nocrt: true,
454 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900455
456 vndk_libraries_txt {
457 name: "vndkcorevariant.libraries.txt",
458 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800459 `
460
461 config := TestConfig(buildDir, android.Android, nil, bp, nil)
462 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
463 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
464 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
465
466 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
467
468 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900469
Jooyung Han2216fb12019-11-06 16:46:15 +0900470 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900471}
472
473func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900474 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900475 cc_library {
476 name: "libvndk",
477 vendor_available: true,
478 vndk: {
479 enabled: true,
480 },
481 nocrt: true,
482 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900483 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900484
485 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
486 "LLNDK: libc.so",
487 "LLNDK: libdl.so",
488 "LLNDK: libft2.so",
489 "LLNDK: libm.so",
490 "VNDK-SP: libc++.so",
491 "VNDK-core: libvndk.so",
492 "VNDK-private: libft2.so",
493 })
Logan Chienf3511742017-10-31 18:04:35 +0800494}
495
Logan Chiend3c59a22018-03-29 14:08:15 +0800496func TestVndkDepError(t *testing.T) {
497 // Check whether an error is emitted when a VNDK lib depends on a system lib.
498 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
499 cc_library {
500 name: "libvndk",
501 vendor_available: true,
502 vndk: {
503 enabled: true,
504 },
505 shared_libs: ["libfwk"], // Cause error
506 nocrt: true,
507 }
508
509 cc_library {
510 name: "libfwk",
511 nocrt: true,
512 }
513 `)
514
515 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
516 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
517 cc_library {
518 name: "libvndk",
519 vendor_available: true,
520 vndk: {
521 enabled: true,
522 },
523 shared_libs: ["libvendor"], // Cause error
524 nocrt: true,
525 }
526
527 cc_library {
528 name: "libvendor",
529 vendor: true,
530 nocrt: true,
531 }
532 `)
533
534 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
535 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
536 cc_library {
537 name: "libvndk_sp",
538 vendor_available: true,
539 vndk: {
540 enabled: true,
541 support_system_process: true,
542 },
543 shared_libs: ["libfwk"], // Cause error
544 nocrt: true,
545 }
546
547 cc_library {
548 name: "libfwk",
549 nocrt: true,
550 }
551 `)
552
553 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
554 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
555 cc_library {
556 name: "libvndk_sp",
557 vendor_available: true,
558 vndk: {
559 enabled: true,
560 support_system_process: true,
561 },
562 shared_libs: ["libvendor"], // Cause error
563 nocrt: true,
564 }
565
566 cc_library {
567 name: "libvendor",
568 vendor: true,
569 nocrt: true,
570 }
571 `)
572
573 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
574 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
575 cc_library {
576 name: "libvndk_sp",
577 vendor_available: true,
578 vndk: {
579 enabled: true,
580 support_system_process: true,
581 },
582 shared_libs: ["libvndk"], // Cause error
583 nocrt: true,
584 }
585
586 cc_library {
587 name: "libvndk",
588 vendor_available: true,
589 vndk: {
590 enabled: true,
591 },
592 nocrt: true,
593 }
594 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900595
596 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
597 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
598 cc_library {
599 name: "libvndk",
600 vendor_available: true,
601 vndk: {
602 enabled: true,
603 },
604 shared_libs: ["libnonvndk"],
605 nocrt: true,
606 }
607
608 cc_library {
609 name: "libnonvndk",
610 vendor_available: true,
611 nocrt: true,
612 }
613 `)
614
615 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
616 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
617 cc_library {
618 name: "libvndkprivate",
619 vendor_available: false,
620 vndk: {
621 enabled: true,
622 },
623 shared_libs: ["libnonvndk"],
624 nocrt: true,
625 }
626
627 cc_library {
628 name: "libnonvndk",
629 vendor_available: true,
630 nocrt: true,
631 }
632 `)
633
634 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
635 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
636 cc_library {
637 name: "libvndksp",
638 vendor_available: true,
639 vndk: {
640 enabled: true,
641 support_system_process: true,
642 },
643 shared_libs: ["libnonvndk"],
644 nocrt: true,
645 }
646
647 cc_library {
648 name: "libnonvndk",
649 vendor_available: true,
650 nocrt: true,
651 }
652 `)
653
654 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
655 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
656 cc_library {
657 name: "libvndkspprivate",
658 vendor_available: false,
659 vndk: {
660 enabled: true,
661 support_system_process: true,
662 },
663 shared_libs: ["libnonvndk"],
664 nocrt: true,
665 }
666
667 cc_library {
668 name: "libnonvndk",
669 vendor_available: true,
670 nocrt: true,
671 }
672 `)
673}
674
675func TestDoubleLoadbleDep(t *testing.T) {
676 // okay to link : LLNDK -> double_loadable VNDK
677 testCc(t, `
678 cc_library {
679 name: "libllndk",
680 shared_libs: ["libdoubleloadable"],
681 }
682
683 llndk_library {
684 name: "libllndk",
685 symbol_file: "",
686 }
687
688 cc_library {
689 name: "libdoubleloadable",
690 vendor_available: true,
691 vndk: {
692 enabled: true,
693 },
694 double_loadable: true,
695 }
696 `)
697 // okay to link : LLNDK -> VNDK-SP
698 testCc(t, `
699 cc_library {
700 name: "libllndk",
701 shared_libs: ["libvndksp"],
702 }
703
704 llndk_library {
705 name: "libllndk",
706 symbol_file: "",
707 }
708
709 cc_library {
710 name: "libvndksp",
711 vendor_available: true,
712 vndk: {
713 enabled: true,
714 support_system_process: true,
715 },
716 }
717 `)
718 // okay to link : double_loadable -> double_loadable
719 testCc(t, `
720 cc_library {
721 name: "libdoubleloadable1",
722 shared_libs: ["libdoubleloadable2"],
723 vendor_available: true,
724 double_loadable: true,
725 }
726
727 cc_library {
728 name: "libdoubleloadable2",
729 vendor_available: true,
730 double_loadable: true,
731 }
732 `)
733 // okay to link : double_loadable VNDK -> double_loadable VNDK private
734 testCc(t, `
735 cc_library {
736 name: "libdoubleloadable",
737 vendor_available: true,
738 vndk: {
739 enabled: true,
740 },
741 double_loadable: true,
742 shared_libs: ["libnondoubleloadable"],
743 }
744
745 cc_library {
746 name: "libnondoubleloadable",
747 vendor_available: false,
748 vndk: {
749 enabled: true,
750 },
751 double_loadable: true,
752 }
753 `)
754 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
755 testCc(t, `
756 cc_library {
757 name: "libllndk",
758 shared_libs: ["libcoreonly"],
759 }
760
761 llndk_library {
762 name: "libllndk",
763 symbol_file: "",
764 }
765
766 cc_library {
767 name: "libcoreonly",
768 shared_libs: ["libvendoravailable"],
769 }
770
771 // indirect dependency of LLNDK
772 cc_library {
773 name: "libvendoravailable",
774 vendor_available: true,
775 double_loadable: true,
776 }
777 `)
778}
779
780func TestDoubleLoadableDepError(t *testing.T) {
781 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
782 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
783 cc_library {
784 name: "libllndk",
785 shared_libs: ["libnondoubleloadable"],
786 }
787
788 llndk_library {
789 name: "libllndk",
790 symbol_file: "",
791 }
792
793 cc_library {
794 name: "libnondoubleloadable",
795 vendor_available: true,
796 vndk: {
797 enabled: true,
798 },
799 }
800 `)
801
802 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
803 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
804 cc_library {
805 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -0700806 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900807 shared_libs: ["libnondoubleloadable"],
808 }
809
810 llndk_library {
811 name: "libllndk",
812 symbol_file: "",
813 }
814
815 cc_library {
816 name: "libnondoubleloadable",
817 vendor_available: true,
818 }
819 `)
820
821 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
822 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
823 cc_library {
824 name: "libdoubleloadable",
825 vendor_available: true,
826 double_loadable: true,
827 shared_libs: ["libnondoubleloadable"],
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 VNDK 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 vndk: {
849 enabled: true,
850 },
851 }
852 `)
853
854 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
855 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
856 cc_library {
857 name: "libdoubleloadable",
858 vendor_available: true,
859 vndk: {
860 enabled: true,
861 },
862 double_loadable: true,
863 shared_libs: ["libnondoubleloadable"],
864 }
865
866 cc_library {
867 name: "libnondoubleloadable",
868 vendor_available: false,
869 vndk: {
870 enabled: true,
871 },
872 }
873 `)
874
875 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
876 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
877 cc_library {
878 name: "libllndk",
879 shared_libs: ["libcoreonly"],
880 }
881
882 llndk_library {
883 name: "libllndk",
884 symbol_file: "",
885 }
886
887 cc_library {
888 name: "libcoreonly",
889 shared_libs: ["libvendoravailable"],
890 }
891
892 // indirect dependency of LLNDK
893 cc_library {
894 name: "libvendoravailable",
895 vendor_available: true,
896 }
897 `)
Logan Chiend3c59a22018-03-29 14:08:15 +0800898}
899
Justin Yun9357f4a2018-11-28 15:14:47 +0900900func TestVndkMustNotBeProductSpecific(t *testing.T) {
901 // Check whether an error is emitted when a vndk lib has 'product_specific: true'.
902 testCcError(t, "product_specific must not be true when `vndk: {enabled: true}`", `
903 cc_library {
904 name: "libvndk",
905 product_specific: true, // Cause error
906 vendor_available: true,
907 vndk: {
908 enabled: true,
909 },
910 nocrt: true,
911 }
912 `)
913}
914
Logan Chienf3511742017-10-31 18:04:35 +0800915func TestVndkExt(t *testing.T) {
916 // This test checks the VNDK-Ext properties.
917 ctx := testCc(t, `
918 cc_library {
919 name: "libvndk",
920 vendor_available: true,
921 vndk: {
922 enabled: true,
923 },
924 nocrt: true,
925 }
Jooyung Han4c2b9422019-10-22 19:53:47 +0900926 cc_library {
927 name: "libvndk2",
928 vendor_available: true,
929 vndk: {
930 enabled: true,
931 },
932 target: {
933 vendor: {
934 suffix: "-suffix",
935 },
936 },
937 nocrt: true,
938 }
Logan Chienf3511742017-10-31 18:04:35 +0800939
940 cc_library {
941 name: "libvndk_ext",
942 vendor: true,
943 vndk: {
944 enabled: true,
945 extends: "libvndk",
946 },
947 nocrt: true,
948 }
Jooyung Han4c2b9422019-10-22 19:53:47 +0900949
950 cc_library {
951 name: "libvndk2_ext",
952 vendor: true,
953 vndk: {
954 enabled: true,
955 extends: "libvndk2",
956 },
957 nocrt: true,
958 }
Logan Chienf3511742017-10-31 18:04:35 +0800959 `)
960
961 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk")
Jooyung Han4c2b9422019-10-22 19:53:47 +0900962
963 mod := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
964 assertString(t, mod.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +0800965}
966
Logan Chiend3c59a22018-03-29 14:08:15 +0800967func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +0800968 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
969 ctx := testCcNoVndk(t, `
970 cc_library {
971 name: "libvndk",
972 vendor_available: true,
973 vndk: {
974 enabled: true,
975 },
976 nocrt: true,
977 }
978
979 cc_library {
980 name: "libvndk_ext",
981 vendor: true,
982 vndk: {
983 enabled: true,
984 extends: "libvndk",
985 },
986 nocrt: true,
987 }
988 `)
989
990 // Ensures that the core variant of "libvndk_ext" can be found.
991 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
992 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
993 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
994 }
995}
996
997func TestVndkExtError(t *testing.T) {
998 // This test ensures an error is emitted in ill-formed vndk-ext definition.
999 testCcError(t, "must set `vendor: true` to set `extends: \".*\"`", `
1000 cc_library {
1001 name: "libvndk",
1002 vendor_available: true,
1003 vndk: {
1004 enabled: true,
1005 },
1006 nocrt: true,
1007 }
1008
1009 cc_library {
1010 name: "libvndk_ext",
1011 vndk: {
1012 enabled: true,
1013 extends: "libvndk",
1014 },
1015 nocrt: true,
1016 }
1017 `)
1018
1019 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1020 cc_library {
1021 name: "libvndk",
1022 vendor_available: true,
1023 vndk: {
1024 enabled: true,
1025 },
1026 nocrt: true,
1027 }
1028
1029 cc_library {
1030 name: "libvndk_ext",
1031 vendor: true,
1032 vndk: {
1033 enabled: true,
1034 },
1035 nocrt: true,
1036 }
1037 `)
1038}
1039
1040func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1041 // This test ensures an error is emitted for inconsistent support_system_process.
1042 testCcError(t, "module \".*\" with mismatched support_system_process", `
1043 cc_library {
1044 name: "libvndk",
1045 vendor_available: true,
1046 vndk: {
1047 enabled: true,
1048 },
1049 nocrt: true,
1050 }
1051
1052 cc_library {
1053 name: "libvndk_sp_ext",
1054 vendor: true,
1055 vndk: {
1056 enabled: true,
1057 extends: "libvndk",
1058 support_system_process: true,
1059 },
1060 nocrt: true,
1061 }
1062 `)
1063
1064 testCcError(t, "module \".*\" with mismatched support_system_process", `
1065 cc_library {
1066 name: "libvndk_sp",
1067 vendor_available: true,
1068 vndk: {
1069 enabled: true,
1070 support_system_process: true,
1071 },
1072 nocrt: true,
1073 }
1074
1075 cc_library {
1076 name: "libvndk_ext",
1077 vendor: true,
1078 vndk: {
1079 enabled: true,
1080 extends: "libvndk_sp",
1081 },
1082 nocrt: true,
1083 }
1084 `)
1085}
1086
1087func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001088 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08001089 // with `vendor_available: false`.
1090 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1091 cc_library {
1092 name: "libvndk",
1093 vendor_available: false,
1094 vndk: {
1095 enabled: true,
1096 },
1097 nocrt: true,
1098 }
1099
1100 cc_library {
1101 name: "libvndk_ext",
1102 vendor: true,
1103 vndk: {
1104 enabled: true,
1105 extends: "libvndk",
1106 },
1107 nocrt: true,
1108 }
1109 `)
1110}
1111
Logan Chiend3c59a22018-03-29 14:08:15 +08001112func TestVendorModuleUseVndkExt(t *testing.T) {
1113 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001114 testCc(t, `
1115 cc_library {
1116 name: "libvndk",
1117 vendor_available: true,
1118 vndk: {
1119 enabled: true,
1120 },
1121 nocrt: true,
1122 }
1123
1124 cc_library {
1125 name: "libvndk_ext",
1126 vendor: true,
1127 vndk: {
1128 enabled: true,
1129 extends: "libvndk",
1130 },
1131 nocrt: true,
1132 }
1133
1134 cc_library {
1135
1136 name: "libvndk_sp",
1137 vendor_available: true,
1138 vndk: {
1139 enabled: true,
1140 support_system_process: true,
1141 },
1142 nocrt: true,
1143 }
1144
1145 cc_library {
1146 name: "libvndk_sp_ext",
1147 vendor: true,
1148 vndk: {
1149 enabled: true,
1150 extends: "libvndk_sp",
1151 support_system_process: true,
1152 },
1153 nocrt: true,
1154 }
1155
1156 cc_library {
1157 name: "libvendor",
1158 vendor: true,
1159 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1160 nocrt: true,
1161 }
1162 `)
1163}
1164
Logan Chiend3c59a22018-03-29 14:08:15 +08001165func TestVndkExtUseVendorLib(t *testing.T) {
1166 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001167 testCc(t, `
1168 cc_library {
1169 name: "libvndk",
1170 vendor_available: true,
1171 vndk: {
1172 enabled: true,
1173 },
1174 nocrt: true,
1175 }
1176
1177 cc_library {
1178 name: "libvndk_ext",
1179 vendor: true,
1180 vndk: {
1181 enabled: true,
1182 extends: "libvndk",
1183 },
1184 shared_libs: ["libvendor"],
1185 nocrt: true,
1186 }
1187
1188 cc_library {
1189 name: "libvendor",
1190 vendor: true,
1191 nocrt: true,
1192 }
1193 `)
Logan Chienf3511742017-10-31 18:04:35 +08001194
Logan Chiend3c59a22018-03-29 14:08:15 +08001195 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1196 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001197 cc_library {
1198 name: "libvndk_sp",
1199 vendor_available: true,
1200 vndk: {
1201 enabled: true,
1202 support_system_process: true,
1203 },
1204 nocrt: true,
1205 }
1206
1207 cc_library {
1208 name: "libvndk_sp_ext",
1209 vendor: true,
1210 vndk: {
1211 enabled: true,
1212 extends: "libvndk_sp",
1213 support_system_process: true,
1214 },
1215 shared_libs: ["libvendor"], // Cause an error
1216 nocrt: true,
1217 }
1218
1219 cc_library {
1220 name: "libvendor",
1221 vendor: true,
1222 nocrt: true,
1223 }
1224 `)
1225}
1226
Logan Chiend3c59a22018-03-29 14:08:15 +08001227func TestVndkSpExtUseVndkError(t *testing.T) {
1228 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1229 // library.
1230 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1231 cc_library {
1232 name: "libvndk",
1233 vendor_available: true,
1234 vndk: {
1235 enabled: true,
1236 },
1237 nocrt: true,
1238 }
1239
1240 cc_library {
1241 name: "libvndk_sp",
1242 vendor_available: true,
1243 vndk: {
1244 enabled: true,
1245 support_system_process: true,
1246 },
1247 nocrt: true,
1248 }
1249
1250 cc_library {
1251 name: "libvndk_sp_ext",
1252 vendor: true,
1253 vndk: {
1254 enabled: true,
1255 extends: "libvndk_sp",
1256 support_system_process: true,
1257 },
1258 shared_libs: ["libvndk"], // Cause an error
1259 nocrt: true,
1260 }
1261 `)
1262
1263 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1264 // library.
1265 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1266 cc_library {
1267 name: "libvndk",
1268 vendor_available: true,
1269 vndk: {
1270 enabled: true,
1271 },
1272 nocrt: true,
1273 }
1274
1275 cc_library {
1276 name: "libvndk_ext",
1277 vendor: true,
1278 vndk: {
1279 enabled: true,
1280 extends: "libvndk",
1281 },
1282 nocrt: true,
1283 }
1284
1285 cc_library {
1286 name: "libvndk_sp",
1287 vendor_available: true,
1288 vndk: {
1289 enabled: true,
1290 support_system_process: true,
1291 },
1292 nocrt: true,
1293 }
1294
1295 cc_library {
1296 name: "libvndk_sp_ext",
1297 vendor: true,
1298 vndk: {
1299 enabled: true,
1300 extends: "libvndk_sp",
1301 support_system_process: true,
1302 },
1303 shared_libs: ["libvndk_ext"], // Cause an error
1304 nocrt: true,
1305 }
1306 `)
1307}
1308
1309func TestVndkUseVndkExtError(t *testing.T) {
1310 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1311 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001312 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1313 cc_library {
1314 name: "libvndk",
1315 vendor_available: true,
1316 vndk: {
1317 enabled: true,
1318 },
1319 nocrt: true,
1320 }
1321
1322 cc_library {
1323 name: "libvndk_ext",
1324 vendor: true,
1325 vndk: {
1326 enabled: true,
1327 extends: "libvndk",
1328 },
1329 nocrt: true,
1330 }
1331
1332 cc_library {
1333 name: "libvndk2",
1334 vendor_available: true,
1335 vndk: {
1336 enabled: true,
1337 },
1338 shared_libs: ["libvndk_ext"],
1339 nocrt: true,
1340 }
1341 `)
1342
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001343 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001344 cc_library {
1345 name: "libvndk",
1346 vendor_available: true,
1347 vndk: {
1348 enabled: true,
1349 },
1350 nocrt: true,
1351 }
1352
1353 cc_library {
1354 name: "libvndk_ext",
1355 vendor: true,
1356 vndk: {
1357 enabled: true,
1358 extends: "libvndk",
1359 },
1360 nocrt: true,
1361 }
1362
1363 cc_library {
1364 name: "libvndk2",
1365 vendor_available: true,
1366 vndk: {
1367 enabled: true,
1368 },
1369 target: {
1370 vendor: {
1371 shared_libs: ["libvndk_ext"],
1372 },
1373 },
1374 nocrt: true,
1375 }
1376 `)
1377
1378 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1379 cc_library {
1380 name: "libvndk_sp",
1381 vendor_available: true,
1382 vndk: {
1383 enabled: true,
1384 support_system_process: true,
1385 },
1386 nocrt: true,
1387 }
1388
1389 cc_library {
1390 name: "libvndk_sp_ext",
1391 vendor: true,
1392 vndk: {
1393 enabled: true,
1394 extends: "libvndk_sp",
1395 support_system_process: true,
1396 },
1397 nocrt: true,
1398 }
1399
1400 cc_library {
1401 name: "libvndk_sp_2",
1402 vendor_available: true,
1403 vndk: {
1404 enabled: true,
1405 support_system_process: true,
1406 },
1407 shared_libs: ["libvndk_sp_ext"],
1408 nocrt: true,
1409 }
1410 `)
1411
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001412 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001413 cc_library {
1414 name: "libvndk_sp",
1415 vendor_available: true,
1416 vndk: {
1417 enabled: true,
1418 },
1419 nocrt: true,
1420 }
1421
1422 cc_library {
1423 name: "libvndk_sp_ext",
1424 vendor: true,
1425 vndk: {
1426 enabled: true,
1427 extends: "libvndk_sp",
1428 },
1429 nocrt: true,
1430 }
1431
1432 cc_library {
1433 name: "libvndk_sp2",
1434 vendor_available: true,
1435 vndk: {
1436 enabled: true,
1437 },
1438 target: {
1439 vendor: {
1440 shared_libs: ["libvndk_sp_ext"],
1441 },
1442 },
1443 nocrt: true,
1444 }
1445 `)
1446}
1447
Jooyung Han38002912019-05-16 04:01:54 +09001448func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001449 bp := `
1450 cc_library {
1451 name: "libvndk",
1452 vendor_available: true,
1453 vndk: {
1454 enabled: true,
1455 },
1456 }
1457 cc_library {
1458 name: "libvndksp",
1459 vendor_available: true,
1460 vndk: {
1461 enabled: true,
1462 support_system_process: true,
1463 },
1464 }
1465 cc_library {
1466 name: "libvndkprivate",
1467 vendor_available: false,
1468 vndk: {
1469 enabled: true,
1470 },
1471 }
1472 cc_library {
1473 name: "libvendor",
1474 vendor: true,
1475 }
1476 cc_library {
1477 name: "libvndkext",
1478 vendor: true,
1479 vndk: {
1480 enabled: true,
1481 extends: "libvndk",
1482 },
1483 }
1484 vndk_prebuilt_shared {
1485 name: "prevndk",
1486 version: "27",
1487 target_arch: "arm",
1488 binder32bit: true,
1489 vendor_available: true,
1490 vndk: {
1491 enabled: true,
1492 },
1493 arch: {
1494 arm: {
1495 srcs: ["liba.so"],
1496 },
1497 },
1498 }
1499 cc_library {
1500 name: "libllndk",
1501 }
1502 llndk_library {
1503 name: "libllndk",
1504 symbol_file: "",
1505 }
1506 cc_library {
1507 name: "libllndkprivate",
1508 }
1509 llndk_library {
1510 name: "libllndkprivate",
1511 vendor_available: false,
1512 symbol_file: "",
1513 }`
1514
1515 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09001516 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1517 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1518 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08001519 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09001520
Jooyung Han0302a842019-10-30 18:43:49 +09001521 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09001522 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09001523 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09001524 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09001525 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09001526 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09001527 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09001528 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09001529
Colin Crossfb0c16e2019-11-20 17:12:35 -08001530 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09001531
Jooyung Han38002912019-05-16 04:01:54 +09001532 tests := []struct {
1533 variant string
1534 name string
1535 expected string
1536 }{
1537 {vendorVariant, "libvndk", "native:vndk"},
1538 {vendorVariant, "libvndksp", "native:vndk"},
1539 {vendorVariant, "libvndkprivate", "native:vndk_private"},
1540 {vendorVariant, "libvendor", "native:vendor"},
1541 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09001542 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09001543 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09001544 {coreVariant, "libvndk", "native:platform"},
1545 {coreVariant, "libvndkprivate", "native:platform"},
1546 {coreVariant, "libllndk", "native:platform"},
1547 }
1548 for _, test := range tests {
1549 t.Run(test.name, func(t *testing.T) {
1550 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
1551 assertString(t, module.makeLinkType, test.expected)
1552 })
1553 }
1554}
1555
Colin Cross0af4b842015-04-30 16:36:18 -07001556var (
1557 str11 = "01234567891"
1558 str10 = str11[:10]
1559 str9 = str11[:9]
1560 str5 = str11[:5]
1561 str4 = str11[:4]
1562)
1563
1564var splitListForSizeTestCases = []struct {
1565 in []string
1566 out [][]string
1567 size int
1568}{
1569 {
1570 in: []string{str10},
1571 out: [][]string{{str10}},
1572 size: 10,
1573 },
1574 {
1575 in: []string{str9},
1576 out: [][]string{{str9}},
1577 size: 10,
1578 },
1579 {
1580 in: []string{str5},
1581 out: [][]string{{str5}},
1582 size: 10,
1583 },
1584 {
1585 in: []string{str11},
1586 out: nil,
1587 size: 10,
1588 },
1589 {
1590 in: []string{str10, str10},
1591 out: [][]string{{str10}, {str10}},
1592 size: 10,
1593 },
1594 {
1595 in: []string{str9, str10},
1596 out: [][]string{{str9}, {str10}},
1597 size: 10,
1598 },
1599 {
1600 in: []string{str10, str9},
1601 out: [][]string{{str10}, {str9}},
1602 size: 10,
1603 },
1604 {
1605 in: []string{str5, str4},
1606 out: [][]string{{str5, str4}},
1607 size: 10,
1608 },
1609 {
1610 in: []string{str5, str4, str5},
1611 out: [][]string{{str5, str4}, {str5}},
1612 size: 10,
1613 },
1614 {
1615 in: []string{str5, str4, str5, str4},
1616 out: [][]string{{str5, str4}, {str5, str4}},
1617 size: 10,
1618 },
1619 {
1620 in: []string{str5, str4, str5, str5},
1621 out: [][]string{{str5, str4}, {str5}, {str5}},
1622 size: 10,
1623 },
1624 {
1625 in: []string{str5, str5, str5, str4},
1626 out: [][]string{{str5}, {str5}, {str5, str4}},
1627 size: 10,
1628 },
1629 {
1630 in: []string{str9, str11},
1631 out: nil,
1632 size: 10,
1633 },
1634 {
1635 in: []string{str11, str9},
1636 out: nil,
1637 size: 10,
1638 },
1639}
1640
1641func TestSplitListForSize(t *testing.T) {
1642 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08001643 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07001644
1645 var outStrings [][]string
1646
1647 if len(out) > 0 {
1648 outStrings = make([][]string, len(out))
1649 for i, o := range out {
1650 outStrings[i] = o.Strings()
1651 }
1652 }
1653
1654 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07001655 t.Errorf("incorrect output:")
1656 t.Errorf(" input: %#v", testCase.in)
1657 t.Errorf(" size: %d", testCase.size)
1658 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07001659 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07001660 }
1661 }
1662}
Jeff Gaston294356f2017-09-27 17:05:30 -07001663
1664var staticLinkDepOrderTestCases = []struct {
1665 // This is a string representation of a map[moduleName][]moduleDependency .
1666 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001667 inStatic string
1668
1669 // This is a string representation of a map[moduleName][]moduleDependency .
1670 // It models the dependencies declared in an Android.bp file.
1671 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07001672
1673 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
1674 // The keys of allOrdered specify which modules we would like to check.
1675 // The values of allOrdered specify the expected result (of the transitive closure of all
1676 // dependencies) for each module to test
1677 allOrdered string
1678
1679 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
1680 // The keys of outOrdered specify which modules we would like to check.
1681 // The values of outOrdered specify the expected result (of the ordered linker command line)
1682 // for each module to test.
1683 outOrdered string
1684}{
1685 // Simple tests
1686 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001687 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07001688 outOrdered: "",
1689 },
1690 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001691 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001692 outOrdered: "a:",
1693 },
1694 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001695 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001696 outOrdered: "a:b; b:",
1697 },
1698 // Tests of reordering
1699 {
1700 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001701 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001702 outOrdered: "a:b,c,d; b:d; c:d; d:",
1703 },
1704 {
1705 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001706 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001707 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
1708 },
1709 {
1710 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001711 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07001712 outOrdered: "a:d,b,e,c; d:b; e:c",
1713 },
1714 {
1715 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001716 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07001717 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
1718 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
1719 },
1720 {
1721 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001722 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 -07001723 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
1724 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
1725 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001726 // shared dependencies
1727 {
1728 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
1729 // So, we don't actually have to check that a shared dependency of c will change the order
1730 // of a library that depends statically on b and on c. We only need to check that if c has
1731 // a shared dependency on b, that that shows up in allOrdered.
1732 inShared: "c:b",
1733 allOrdered: "c:b",
1734 outOrdered: "c:",
1735 },
1736 {
1737 // This test doesn't actually include any shared dependencies but it's a reminder of what
1738 // the second phase of the above test would look like
1739 inStatic: "a:b,c; c:b",
1740 allOrdered: "a:c,b; c:b",
1741 outOrdered: "a:c,b; c:b",
1742 },
Jeff Gaston294356f2017-09-27 17:05:30 -07001743 // tiebreakers for when two modules specifying different orderings and there is no dependency
1744 // to dictate an order
1745 {
1746 // 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 -08001747 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07001748 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
1749 },
1750 {
1751 // 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 -08001752 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 -07001753 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
1754 },
1755 // Tests involving duplicate dependencies
1756 {
1757 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001758 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001759 outOrdered: "a:c,b",
1760 },
1761 {
1762 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001763 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001764 outOrdered: "a:d,c,b",
1765 },
1766 // Tests to confirm the nonexistence of infinite loops.
1767 // These cases should never happen, so as long as the test terminates and the
1768 // result is deterministic then that should be fine.
1769 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001770 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07001771 outOrdered: "a:a",
1772 },
1773 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001774 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07001775 allOrdered: "a:b,c; b:c,a; c:a,b",
1776 outOrdered: "a:b; b:c; c:a",
1777 },
1778 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001779 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001780 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
1781 outOrdered: "a:c,b; b:a,c; c:b,a",
1782 },
1783}
1784
1785// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
1786func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
1787 // convert from "a:b,c; d:e" to "a:b,c;d:e"
1788 strippedText := strings.Replace(text, " ", "", -1)
1789 if len(strippedText) < 1 {
1790 return []android.Path{}, make(map[android.Path][]android.Path, 0)
1791 }
1792 allDeps = make(map[android.Path][]android.Path, 0)
1793
1794 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
1795 moduleTexts := strings.Split(strippedText, ";")
1796
1797 outputForModuleName := func(moduleName string) android.Path {
1798 return android.PathForTesting(moduleName)
1799 }
1800
1801 for _, moduleText := range moduleTexts {
1802 // convert from "a:b,c" to ["a", "b,c"]
1803 components := strings.Split(moduleText, ":")
1804 if len(components) != 2 {
1805 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
1806 }
1807 moduleName := components[0]
1808 moduleOutput := outputForModuleName(moduleName)
1809 modulesInOrder = append(modulesInOrder, moduleOutput)
1810
1811 depString := components[1]
1812 // convert from "b,c" to ["b", "c"]
1813 depNames := strings.Split(depString, ",")
1814 if len(depString) < 1 {
1815 depNames = []string{}
1816 }
1817 var deps []android.Path
1818 for _, depName := range depNames {
1819 deps = append(deps, outputForModuleName(depName))
1820 }
1821 allDeps[moduleOutput] = deps
1822 }
1823 return modulesInOrder, allDeps
1824}
1825
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001826func TestLinkReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001827 for _, testCase := range staticLinkDepOrderTestCases {
1828 errs := []string{}
1829
1830 // parse testcase
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001831 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)
Jeff Gaston294356f2017-09-27 17:05:30 -07001832 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)
1833 if testCase.allOrdered == "" {
1834 // allow the test case to skip specifying allOrdered
1835 testCase.allOrdered = testCase.outOrdered
1836 }
1837 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001838 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)
Jeff Gaston294356f2017-09-27 17:05:30 -07001839
1840 // For each module whose post-reordered dependencies were specified, validate that
1841 // reordering the inputs produces the expected outputs.
1842 for _, moduleName := range expectedModuleNames {
1843 moduleDeps := givenTransitiveDeps[moduleName]
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001844 givenSharedDeps := givenAllSharedDeps[moduleName]
1845 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -07001846
1847 correctAllOrdered := expectedAllDeps[moduleName]
1848 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {
1849 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001850 "\nin static:%q"+
1851 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07001852 "\nmodule: %v"+
1853 "\nexpected: %s"+
1854 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001855 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07001856 }
1857
1858 correctOutputDeps := expectedTransitiveDeps[moduleName]
1859 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {
1860 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001861 "\nin static:%q"+
1862 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07001863 "\nmodule: %v"+
1864 "\nexpected: %s"+
1865 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001866 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07001867 }
1868 }
1869
1870 if len(errs) > 0 {
1871 sort.Strings(errs)
1872 for _, err := range errs {
1873 t.Error(err)
1874 }
1875 }
1876 }
1877}
Logan Chienf3511742017-10-31 18:04:35 +08001878
Jeff Gaston294356f2017-09-27 17:05:30 -07001879func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
1880 for _, moduleName := range moduleNames {
1881 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
1882 output := module.outputFile.Path()
1883 paths = append(paths, output)
1884 }
1885 return paths
1886}
1887
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001888func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001889 ctx := testCc(t, `
1890 cc_library {
1891 name: "a",
1892 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09001893 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001894 }
1895 cc_library {
1896 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09001897 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001898 }
1899 cc_library {
1900 name: "c",
1901 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09001902 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001903 }
1904 cc_library {
1905 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09001906 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07001907 }
1908
1909 `)
1910
Colin Cross7113d202019-11-20 16:39:12 -08001911 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07001912 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001913 actual := moduleA.depsInLinkOrder
Jeff Gaston294356f2017-09-27 17:05:30 -07001914 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})
1915
1916 if !reflect.DeepEqual(actual, expected) {
1917 t.Errorf("staticDeps orderings were not propagated correctly"+
1918 "\nactual: %v"+
1919 "\nexpected: %v",
1920 actual,
1921 expected,
1922 )
1923 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09001924}
Jeff Gaston294356f2017-09-27 17:05:30 -07001925
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001926func TestStaticLibDepReorderingWithShared(t *testing.T) {
1927 ctx := testCc(t, `
1928 cc_library {
1929 name: "a",
1930 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09001931 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001932 }
1933 cc_library {
1934 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09001935 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001936 }
1937 cc_library {
1938 name: "c",
1939 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09001940 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001941 }
1942
1943 `)
1944
Colin Cross7113d202019-11-20 16:39:12 -08001945 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001946 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
1947 actual := moduleA.depsInLinkOrder
1948 expected := getOutputPaths(ctx, variant, []string{"c", "b"})
1949
1950 if !reflect.DeepEqual(actual, expected) {
1951 t.Errorf("staticDeps orderings did not account for shared libs"+
1952 "\nactual: %v"+
1953 "\nexpected: %v",
1954 actual,
1955 expected,
1956 )
1957 }
1958}
1959
Jiyong Parka46a4d52017-12-14 19:54:34 +09001960func TestLlndkHeaders(t *testing.T) {
1961 ctx := testCc(t, `
1962 llndk_headers {
1963 name: "libllndk_headers",
1964 export_include_dirs: ["my_include"],
1965 }
1966 llndk_library {
1967 name: "libllndk",
1968 export_llndk_headers: ["libllndk_headers"],
1969 }
1970 cc_library {
1971 name: "libvendor",
1972 shared_libs: ["libllndk"],
1973 vendor: true,
1974 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07001975 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08001976 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09001977 }
1978 `)
1979
1980 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08001981 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09001982 cflags := cc.Args["cFlags"]
1983 if !strings.Contains(cflags, "-Imy_include") {
1984 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
1985 }
1986}
1987
Logan Chien43d34c32017-12-20 01:17:32 +08001988func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
1989 actual := module.Properties.AndroidMkRuntimeLibs
1990 if !reflect.DeepEqual(actual, expected) {
1991 t.Errorf("incorrect runtime_libs for shared libs"+
1992 "\nactual: %v"+
1993 "\nexpected: %v",
1994 actual,
1995 expected,
1996 )
1997 }
1998}
1999
2000const runtimeLibAndroidBp = `
2001 cc_library {
2002 name: "libvendor_available1",
2003 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002004 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002005 nocrt : true,
2006 system_shared_libs : [],
2007 }
2008 cc_library {
2009 name: "libvendor_available2",
2010 vendor_available: true,
2011 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002012 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002013 nocrt : true,
2014 system_shared_libs : [],
2015 }
2016 cc_library {
2017 name: "libvendor_available3",
2018 vendor_available: true,
2019 runtime_libs: ["libvendor_available1"],
2020 target: {
2021 vendor: {
2022 exclude_runtime_libs: ["libvendor_available1"],
2023 }
2024 },
Yi Konge7fe9912019-06-02 00:53:50 -07002025 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002026 nocrt : true,
2027 system_shared_libs : [],
2028 }
2029 cc_library {
2030 name: "libcore",
2031 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002032 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002033 nocrt : true,
2034 system_shared_libs : [],
2035 }
2036 cc_library {
2037 name: "libvendor1",
2038 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002039 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002040 nocrt : true,
2041 system_shared_libs : [],
2042 }
2043 cc_library {
2044 name: "libvendor2",
2045 vendor: true,
2046 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002047 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002048 nocrt : true,
2049 system_shared_libs : [],
2050 }
2051`
2052
2053func TestRuntimeLibs(t *testing.T) {
2054 ctx := testCc(t, runtimeLibAndroidBp)
2055
2056 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002057 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002058
2059 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2060 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2061
2062 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
2063 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2064
2065 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2066 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002067 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002068
2069 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2070 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
2071
2072 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2073 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
2074}
2075
2076func TestExcludeRuntimeLibs(t *testing.T) {
2077 ctx := testCc(t, runtimeLibAndroidBp)
2078
Colin Cross7113d202019-11-20 16:39:12 -08002079 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002080 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2081 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2082
Colin Crossfb0c16e2019-11-20 17:12:35 -08002083 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002084 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2085 checkRuntimeLibs(t, nil, module)
2086}
2087
2088func TestRuntimeLibsNoVndk(t *testing.T) {
2089 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2090
2091 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2092
Colin Cross7113d202019-11-20 16:39:12 -08002093 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002094
2095 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2096 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2097
2098 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2099 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
2100}
2101
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002102func checkStaticLibs(t *testing.T, expected []string, module *Module) {
2103 actual := module.Properties.AndroidMkStaticLibs
2104 if !reflect.DeepEqual(actual, expected) {
2105 t.Errorf("incorrect static_libs"+
2106 "\nactual: %v"+
2107 "\nexpected: %v",
2108 actual,
2109 expected,
2110 )
2111 }
2112}
2113
2114const staticLibAndroidBp = `
2115 cc_library {
2116 name: "lib1",
2117 }
2118 cc_library {
2119 name: "lib2",
2120 static_libs: ["lib1"],
2121 }
2122`
2123
2124func TestStaticLibDepExport(t *testing.T) {
2125 ctx := testCc(t, staticLibAndroidBp)
2126
2127 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002128 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002129 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Dan Albert2da19cb2019-07-24 12:17:40 -07002130 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc_stripped"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002131
2132 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002133 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002134 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2135 // libc++_static is linked additionally.
Dan Albert2da19cb2019-07-24 12:17:40 -07002136 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc_stripped"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002137}
2138
Jiyong Parkd08b6972017-09-26 10:50:54 +09002139var compilerFlagsTestCases = []struct {
2140 in string
2141 out bool
2142}{
2143 {
2144 in: "a",
2145 out: false,
2146 },
2147 {
2148 in: "-a",
2149 out: true,
2150 },
2151 {
2152 in: "-Ipath/to/something",
2153 out: false,
2154 },
2155 {
2156 in: "-isystempath/to/something",
2157 out: false,
2158 },
2159 {
2160 in: "--coverage",
2161 out: false,
2162 },
2163 {
2164 in: "-include a/b",
2165 out: true,
2166 },
2167 {
2168 in: "-include a/b c/d",
2169 out: false,
2170 },
2171 {
2172 in: "-DMACRO",
2173 out: true,
2174 },
2175 {
2176 in: "-DMAC RO",
2177 out: false,
2178 },
2179 {
2180 in: "-a -b",
2181 out: false,
2182 },
2183 {
2184 in: "-DMACRO=definition",
2185 out: true,
2186 },
2187 {
2188 in: "-DMACRO=defi nition",
2189 out: true, // TODO(jiyong): this should be false
2190 },
2191 {
2192 in: "-DMACRO(x)=x + 1",
2193 out: true,
2194 },
2195 {
2196 in: "-DMACRO=\"defi nition\"",
2197 out: true,
2198 },
2199}
2200
2201type mockContext struct {
2202 BaseModuleContext
2203 result bool
2204}
2205
2206func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2207 // CheckBadCompilerFlags calls this function when the flag should be rejected
2208 ctx.result = false
2209}
2210
2211func TestCompilerFlags(t *testing.T) {
2212 for _, testCase := range compilerFlagsTestCases {
2213 ctx := &mockContext{result: true}
2214 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
2215 if ctx.result != testCase.out {
2216 t.Errorf("incorrect output:")
2217 t.Errorf(" input: %#v", testCase.in)
2218 t.Errorf(" expected: %#v", testCase.out)
2219 t.Errorf(" got: %#v", ctx.result)
2220 }
2221 }
Jeff Gaston294356f2017-09-27 17:05:30 -07002222}
Jiyong Park374510b2018-03-19 18:23:01 +09002223
2224func TestVendorPublicLibraries(t *testing.T) {
2225 ctx := testCc(t, `
2226 cc_library_headers {
2227 name: "libvendorpublic_headers",
2228 export_include_dirs: ["my_include"],
2229 }
2230 vendor_public_library {
2231 name: "libvendorpublic",
2232 symbol_file: "",
2233 export_public_headers: ["libvendorpublic_headers"],
2234 }
2235 cc_library {
2236 name: "libvendorpublic",
2237 srcs: ["foo.c"],
2238 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002239 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002240 nocrt: true,
2241 }
2242
2243 cc_library {
2244 name: "libsystem",
2245 shared_libs: ["libvendorpublic"],
2246 vendor: false,
2247 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002248 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002249 nocrt: true,
2250 }
2251 cc_library {
2252 name: "libvendor",
2253 shared_libs: ["libvendorpublic"],
2254 vendor: true,
2255 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002256 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002257 nocrt: true,
2258 }
2259 `)
2260
Colin Cross7113d202019-11-20 16:39:12 -08002261 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08002262 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09002263
2264 // test if header search paths are correctly added
2265 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08002266 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09002267 cflags := cc.Args["cFlags"]
2268 if !strings.Contains(cflags, "-Imy_include") {
2269 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
2270 }
2271
2272 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08002273 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002274 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002275 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09002276 if !strings.Contains(libflags, stubPaths[0].String()) {
2277 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
2278 }
2279
2280 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08002281 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002282 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002283 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09002284 if !strings.Contains(libflags, stubPaths[0].String()) {
2285 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
2286 }
2287
2288}
Jiyong Park37b25202018-07-11 10:49:27 +09002289
2290func TestRecovery(t *testing.T) {
2291 ctx := testCc(t, `
2292 cc_library_shared {
2293 name: "librecovery",
2294 recovery: true,
2295 }
2296 cc_library_shared {
2297 name: "librecovery32",
2298 recovery: true,
2299 compile_multilib:"32",
2300 }
Jiyong Park5baac542018-08-28 09:55:37 +09002301 cc_library_shared {
2302 name: "libHalInRecovery",
2303 recovery_available: true,
2304 vendor: true,
2305 }
Jiyong Park37b25202018-07-11 10:49:27 +09002306 `)
2307
2308 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08002309 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09002310 if len(variants) != 1 || !android.InList(arm64, variants) {
2311 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
2312 }
2313
2314 variants = ctx.ModuleVariantsForTests("librecovery32")
2315 if android.InList(arm64, variants) {
2316 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
2317 }
Jiyong Park5baac542018-08-28 09:55:37 +09002318
2319 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
2320 if !recoveryModule.Platform() {
2321 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
2322 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09002323}
Jiyong Park5baac542018-08-28 09:55:37 +09002324
Jiyong Park7ed9de32018-10-15 22:25:07 +09002325func TestVersionedStubs(t *testing.T) {
2326 ctx := testCc(t, `
2327 cc_library_shared {
2328 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002329 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002330 stubs: {
2331 symbol_file: "foo.map.txt",
2332 versions: ["1", "2", "3"],
2333 },
2334 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002335
Jiyong Park7ed9de32018-10-15 22:25:07 +09002336 cc_library_shared {
2337 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002338 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002339 shared_libs: ["libFoo#1"],
2340 }`)
2341
2342 variants := ctx.ModuleVariantsForTests("libFoo")
2343 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08002344 "android_arm64_armv8-a_shared",
2345 "android_arm64_armv8-a_shared_1",
2346 "android_arm64_armv8-a_shared_2",
2347 "android_arm64_armv8-a_shared_3",
2348 "android_arm_armv7-a-neon_shared",
2349 "android_arm_armv7-a-neon_shared_1",
2350 "android_arm_armv7-a-neon_shared_2",
2351 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09002352 }
2353 variantsMismatch := false
2354 if len(variants) != len(expectedVariants) {
2355 variantsMismatch = true
2356 } else {
2357 for _, v := range expectedVariants {
2358 if !inList(v, variants) {
2359 variantsMismatch = false
2360 }
2361 }
2362 }
2363 if variantsMismatch {
2364 t.Errorf("variants of libFoo expected:\n")
2365 for _, v := range expectedVariants {
2366 t.Errorf("%q\n", v)
2367 }
2368 t.Errorf(", but got:\n")
2369 for _, v := range variants {
2370 t.Errorf("%q\n", v)
2371 }
2372 }
2373
Colin Cross7113d202019-11-20 16:39:12 -08002374 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09002375 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002376 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09002377 if !strings.Contains(libFlags, libFoo1StubPath) {
2378 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
2379 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002380
Colin Cross7113d202019-11-20 16:39:12 -08002381 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09002382 cFlags := libBarCompileRule.Args["cFlags"]
2383 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
2384 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
2385 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
2386 }
Jiyong Park37b25202018-07-11 10:49:27 +09002387}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002388
2389func TestStaticExecutable(t *testing.T) {
2390 ctx := testCc(t, `
2391 cc_binary {
2392 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01002393 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002394 static_executable: true,
2395 }`)
2396
Colin Cross7113d202019-11-20 16:39:12 -08002397 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002398 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
2399 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07002400 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002401 for _, lib := range systemStaticLibs {
2402 if !strings.Contains(libFlags, lib) {
2403 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
2404 }
2405 }
2406 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
2407 for _, lib := range systemSharedLibs {
2408 if strings.Contains(libFlags, lib) {
2409 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
2410 }
2411 }
2412}
Jiyong Parke4bb9862019-02-01 00:31:10 +09002413
2414func TestStaticDepsOrderWithStubs(t *testing.T) {
2415 ctx := testCc(t, `
2416 cc_binary {
2417 name: "mybin",
2418 srcs: ["foo.c"],
2419 static_libs: ["libB"],
2420 static_executable: true,
2421 stl: "none",
2422 }
2423
2424 cc_library {
2425 name: "libB",
2426 srcs: ["foo.c"],
2427 shared_libs: ["libC"],
2428 stl: "none",
2429 }
2430
2431 cc_library {
2432 name: "libC",
2433 srcs: ["foo.c"],
2434 stl: "none",
2435 stubs: {
2436 versions: ["1"],
2437 },
2438 }`)
2439
Colin Cross7113d202019-11-20 16:39:12 -08002440 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Module().(*Module)
Jiyong Parke4bb9862019-02-01 00:31:10 +09002441 actual := mybin.depsInLinkOrder
Colin Cross7113d202019-11-20 16:39:12 -08002442 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libB", "libC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09002443
2444 if !reflect.DeepEqual(actual, expected) {
2445 t.Errorf("staticDeps orderings were not propagated correctly"+
2446 "\nactual: %v"+
2447 "\nexpected: %v",
2448 actual,
2449 expected,
2450 )
2451 }
2452}
Jooyung Han38002912019-05-16 04:01:54 +09002453
Jooyung Hand48f3c32019-08-23 11:18:57 +09002454func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
2455 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
2456 cc_library {
2457 name: "libA",
2458 srcs: ["foo.c"],
2459 shared_libs: ["libB"],
2460 stl: "none",
2461 }
2462
2463 cc_library {
2464 name: "libB",
2465 srcs: ["foo.c"],
2466 enabled: false,
2467 stl: "none",
2468 }
2469 `)
2470}
2471
Mitch Phillipsda9a4632019-07-15 09:34:09 -07002472// Simple smoke test for the cc_fuzz target that ensures the rule compiles
2473// correctly.
2474func TestFuzzTarget(t *testing.T) {
2475 ctx := testCc(t, `
2476 cc_fuzz {
2477 name: "fuzz_smoke_test",
2478 srcs: ["foo.c"],
2479 }`)
2480
Paul Duffin075c4172019-12-19 19:06:13 +00002481 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07002482 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
2483}
2484
Jiyong Park29074592019-07-07 16:27:47 +09002485func TestAidl(t *testing.T) {
2486}
2487
Jooyung Han38002912019-05-16 04:01:54 +09002488func assertString(t *testing.T, got, expected string) {
2489 t.Helper()
2490 if got != expected {
2491 t.Errorf("expected %q got %q", expected, got)
2492 }
2493}
2494
2495func assertArrayString(t *testing.T, got, expected []string) {
2496 t.Helper()
2497 if len(got) != len(expected) {
2498 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
2499 return
2500 }
2501 for i := range got {
2502 if got[i] != expected[i] {
2503 t.Errorf("expected %d-th %q (%q) got %q (%q)",
2504 i, expected[i], expected, got[i], got)
2505 return
2506 }
2507 }
2508}
Colin Crosse1bb5d02019-09-24 14:55:04 -07002509
Jooyung Han0302a842019-10-30 18:43:49 +09002510func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
2511 t.Helper()
2512 assertArrayString(t, android.SortedStringKeys(m), expected)
2513}
2514
Colin Crosse1bb5d02019-09-24 14:55:04 -07002515func TestDefaults(t *testing.T) {
2516 ctx := testCc(t, `
2517 cc_defaults {
2518 name: "defaults",
2519 srcs: ["foo.c"],
2520 static: {
2521 srcs: ["bar.c"],
2522 },
2523 shared: {
2524 srcs: ["baz.c"],
2525 },
2526 }
2527
2528 cc_library_static {
2529 name: "libstatic",
2530 defaults: ["defaults"],
2531 }
2532
2533 cc_library_shared {
2534 name: "libshared",
2535 defaults: ["defaults"],
2536 }
2537
2538 cc_library {
2539 name: "libboth",
2540 defaults: ["defaults"],
2541 }
2542
2543 cc_binary {
2544 name: "binary",
2545 defaults: ["defaults"],
2546 }`)
2547
2548 pathsToBase := func(paths android.Paths) []string {
2549 var ret []string
2550 for _, p := range paths {
2551 ret = append(ret, p.Base())
2552 }
2553 return ret
2554 }
2555
Colin Cross7113d202019-11-20 16:39:12 -08002556 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002557 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
2558 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
2559 }
Colin Cross7113d202019-11-20 16:39:12 -08002560 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002561 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
2562 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
2563 }
Colin Cross7113d202019-11-20 16:39:12 -08002564 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002565 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
2566 t.Errorf("binary ld rule wanted %q, got %q", w, g)
2567 }
2568
Colin Cross7113d202019-11-20 16:39:12 -08002569 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002570 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
2571 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
2572 }
Colin Cross7113d202019-11-20 16:39:12 -08002573 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002574 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
2575 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
2576 }
2577}