blob: b78f1f366609adaa3867db6deef05113cb74eb0e [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
Inseob Kim8471cda2019-11-15 09:59:12 +0900261func checkSnapshot(t *testing.T, ctx *android.TestContext, singletonName, moduleName, snapshotFilename, subDir, variant string) {
262 snapshotSingleton := ctx.SingletonForTests(singletonName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900263
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
Inseob Kim8471cda2019-11-15 09:59:12 +0900276 out := snapshotSingleton.Output(snapshotPath)
Jooyung Han39edb6c2019-11-06 16:53:07 +0900277 if out.Input.String() != outputFiles[0].String() {
Inseob Kim8471cda2019-11-15 09:59:12 +0900278 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, 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, "")
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +0900298
299 var output string
300 if module != "vndkcorevariant.libraries.txt" {
301 output = insertVndkVersion(module, "VER")
302 } else {
303 output = module
304 }
305
Jooyung Han2216fb12019-11-06 16:46:15 +0900306 checkWriteFileOutput(t, vndkLibraries.Output(output), expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900307}
308
Logan Chienf3511742017-10-31 18:04:35 +0800309func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800310 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800311 cc_library {
312 name: "libvndk",
313 vendor_available: true,
314 vndk: {
315 enabled: true,
316 },
317 nocrt: true,
318 }
319
320 cc_library {
321 name: "libvndk_private",
322 vendor_available: false,
323 vndk: {
324 enabled: true,
325 },
326 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900327 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800328 }
329
330 cc_library {
331 name: "libvndk_sp",
332 vendor_available: true,
333 vndk: {
334 enabled: true,
335 support_system_process: true,
336 },
337 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900338 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800339 }
340
341 cc_library {
342 name: "libvndk_sp_private",
343 vendor_available: false,
344 vndk: {
345 enabled: true,
346 support_system_process: true,
347 },
348 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900349 target: {
350 vendor: {
351 suffix: "-x",
352 },
353 },
Logan Chienf3511742017-10-31 18:04:35 +0800354 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900355 vndk_libraries_txt {
356 name: "llndk.libraries.txt",
357 }
358 vndk_libraries_txt {
359 name: "vndkcore.libraries.txt",
360 }
361 vndk_libraries_txt {
362 name: "vndksp.libraries.txt",
363 }
364 vndk_libraries_txt {
365 name: "vndkprivate.libraries.txt",
366 }
367 vndk_libraries_txt {
368 name: "vndkcorevariant.libraries.txt",
369 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800370 `
371
372 config := TestConfig(buildDir, android.Android, nil, bp, nil)
373 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
374 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
375
376 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800377
378 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "")
379 checkVndkModule(t, ctx, "libvndk_private", "vndk-VER", false, "")
380 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "")
381 checkVndkModule(t, ctx, "libvndk_sp_private", "vndk-sp-VER", true, "")
Inseob Kim1f086e22019-05-09 13:29:15 +0900382
383 // Check VNDK snapshot output.
384
385 snapshotDir := "vndk-snapshot"
386 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
387
388 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
389 "arm64", "armv8-a"))
390 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
391 "arm", "armv7-a-neon"))
392
393 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
394 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
395 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
396 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
397
Colin Crossfb0c16e2019-11-20 17:12:35 -0800398 variant := "android_vendor.VER_arm64_armv8-a_shared"
399 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900400
Inseob Kim8471cda2019-11-15 09:59:12 +0900401 checkSnapshot(t, ctx, "vndk-snapshot", "libvndk", "libvndk.so", vndkCoreLibPath, variant)
402 checkSnapshot(t, ctx, "vndk-snapshot", "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
403 checkSnapshot(t, ctx, "vndk-snapshot", "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
404 checkSnapshot(t, ctx, "vndk-snapshot", "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900405
Jooyung Han39edb6c2019-11-06 16:53:07 +0900406 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim8471cda2019-11-15 09:59:12 +0900407 checkSnapshot(t, ctx, "vndk-snapshot", "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
408 checkSnapshot(t, ctx, "vndk-snapshot", "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
409 checkSnapshot(t, ctx, "vndk-snapshot", "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
410 checkSnapshot(t, ctx, "vndk-snapshot", "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900411
Jooyung Han097087b2019-10-22 19:32:18 +0900412 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
413 "LLNDK: libc.so",
414 "LLNDK: libdl.so",
415 "LLNDK: libft2.so",
416 "LLNDK: libm.so",
417 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900418 "VNDK-SP: libvndk_sp-x.so",
419 "VNDK-SP: libvndk_sp_private-x.so",
420 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900421 "VNDK-core: libvndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900422 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900423 "VNDK-private: libvndk-private.so",
424 "VNDK-private: libvndk_sp_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900425 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900426 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
427 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so"})
428 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so"})
429 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so"})
430 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
431}
432
433func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800434 bp := `
Jooyung Han2216fb12019-11-06 16:46:15 +0900435 vndk_libraries_txt {
436 name: "llndk.libraries.txt",
Colin Cross98be1bb2019-12-13 20:41:13 -0800437 }`
438 config := TestConfig(buildDir, android.Android, nil, bp, nil)
439 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
440 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
441 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900442
443 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900444 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900445 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900446}
447
448func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800449 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900450 cc_library {
451 name: "libvndk",
452 vendor_available: true,
453 vndk: {
454 enabled: true,
455 },
456 nocrt: true,
457 }
458
459 cc_library {
460 name: "libvndk_sp",
461 vendor_available: true,
462 vndk: {
463 enabled: true,
464 support_system_process: true,
465 },
466 nocrt: true,
467 }
468
469 cc_library {
470 name: "libvndk2",
471 vendor_available: false,
472 vndk: {
473 enabled: true,
474 },
475 nocrt: true,
476 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900477
478 vndk_libraries_txt {
479 name: "vndkcorevariant.libraries.txt",
480 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800481 `
482
483 config := TestConfig(buildDir, android.Android, nil, bp, nil)
484 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
485 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
486 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
487
488 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
489
490 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900491
Jooyung Han2216fb12019-11-06 16:46:15 +0900492 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900493}
494
495func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900496 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900497 cc_library {
498 name: "libvndk",
499 vendor_available: true,
500 vndk: {
501 enabled: true,
502 },
503 nocrt: true,
504 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900505 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900506
507 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
508 "LLNDK: libc.so",
509 "LLNDK: libdl.so",
510 "LLNDK: libft2.so",
511 "LLNDK: libm.so",
512 "VNDK-SP: libc++.so",
513 "VNDK-core: libvndk.so",
514 "VNDK-private: libft2.so",
515 })
Logan Chienf3511742017-10-31 18:04:35 +0800516}
517
Logan Chiend3c59a22018-03-29 14:08:15 +0800518func TestVndkDepError(t *testing.T) {
519 // Check whether an error is emitted when a VNDK lib depends on a system lib.
520 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
521 cc_library {
522 name: "libvndk",
523 vendor_available: true,
524 vndk: {
525 enabled: true,
526 },
527 shared_libs: ["libfwk"], // Cause error
528 nocrt: true,
529 }
530
531 cc_library {
532 name: "libfwk",
533 nocrt: true,
534 }
535 `)
536
537 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
538 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
539 cc_library {
540 name: "libvndk",
541 vendor_available: true,
542 vndk: {
543 enabled: true,
544 },
545 shared_libs: ["libvendor"], // Cause error
546 nocrt: true,
547 }
548
549 cc_library {
550 name: "libvendor",
551 vendor: true,
552 nocrt: true,
553 }
554 `)
555
556 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
557 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
558 cc_library {
559 name: "libvndk_sp",
560 vendor_available: true,
561 vndk: {
562 enabled: true,
563 support_system_process: true,
564 },
565 shared_libs: ["libfwk"], // Cause error
566 nocrt: true,
567 }
568
569 cc_library {
570 name: "libfwk",
571 nocrt: true,
572 }
573 `)
574
575 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
576 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
577 cc_library {
578 name: "libvndk_sp",
579 vendor_available: true,
580 vndk: {
581 enabled: true,
582 support_system_process: true,
583 },
584 shared_libs: ["libvendor"], // Cause error
585 nocrt: true,
586 }
587
588 cc_library {
589 name: "libvendor",
590 vendor: true,
591 nocrt: true,
592 }
593 `)
594
595 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
596 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
597 cc_library {
598 name: "libvndk_sp",
599 vendor_available: true,
600 vndk: {
601 enabled: true,
602 support_system_process: true,
603 },
604 shared_libs: ["libvndk"], // Cause error
605 nocrt: true,
606 }
607
608 cc_library {
609 name: "libvndk",
610 vendor_available: true,
611 vndk: {
612 enabled: true,
613 },
614 nocrt: true,
615 }
616 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900617
618 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
619 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
620 cc_library {
621 name: "libvndk",
622 vendor_available: true,
623 vndk: {
624 enabled: true,
625 },
626 shared_libs: ["libnonvndk"],
627 nocrt: true,
628 }
629
630 cc_library {
631 name: "libnonvndk",
632 vendor_available: true,
633 nocrt: true,
634 }
635 `)
636
637 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
638 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
639 cc_library {
640 name: "libvndkprivate",
641 vendor_available: false,
642 vndk: {
643 enabled: true,
644 },
645 shared_libs: ["libnonvndk"],
646 nocrt: true,
647 }
648
649 cc_library {
650 name: "libnonvndk",
651 vendor_available: true,
652 nocrt: true,
653 }
654 `)
655
656 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
657 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
658 cc_library {
659 name: "libvndksp",
660 vendor_available: true,
661 vndk: {
662 enabled: true,
663 support_system_process: true,
664 },
665 shared_libs: ["libnonvndk"],
666 nocrt: true,
667 }
668
669 cc_library {
670 name: "libnonvndk",
671 vendor_available: true,
672 nocrt: true,
673 }
674 `)
675
676 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
677 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
678 cc_library {
679 name: "libvndkspprivate",
680 vendor_available: false,
681 vndk: {
682 enabled: true,
683 support_system_process: true,
684 },
685 shared_libs: ["libnonvndk"],
686 nocrt: true,
687 }
688
689 cc_library {
690 name: "libnonvndk",
691 vendor_available: true,
692 nocrt: true,
693 }
694 `)
695}
696
697func TestDoubleLoadbleDep(t *testing.T) {
698 // okay to link : LLNDK -> double_loadable VNDK
699 testCc(t, `
700 cc_library {
701 name: "libllndk",
702 shared_libs: ["libdoubleloadable"],
703 }
704
705 llndk_library {
706 name: "libllndk",
707 symbol_file: "",
708 }
709
710 cc_library {
711 name: "libdoubleloadable",
712 vendor_available: true,
713 vndk: {
714 enabled: true,
715 },
716 double_loadable: true,
717 }
718 `)
719 // okay to link : LLNDK -> VNDK-SP
720 testCc(t, `
721 cc_library {
722 name: "libllndk",
723 shared_libs: ["libvndksp"],
724 }
725
726 llndk_library {
727 name: "libllndk",
728 symbol_file: "",
729 }
730
731 cc_library {
732 name: "libvndksp",
733 vendor_available: true,
734 vndk: {
735 enabled: true,
736 support_system_process: true,
737 },
738 }
739 `)
740 // okay to link : double_loadable -> double_loadable
741 testCc(t, `
742 cc_library {
743 name: "libdoubleloadable1",
744 shared_libs: ["libdoubleloadable2"],
745 vendor_available: true,
746 double_loadable: true,
747 }
748
749 cc_library {
750 name: "libdoubleloadable2",
751 vendor_available: true,
752 double_loadable: true,
753 }
754 `)
755 // okay to link : double_loadable VNDK -> double_loadable VNDK private
756 testCc(t, `
757 cc_library {
758 name: "libdoubleloadable",
759 vendor_available: true,
760 vndk: {
761 enabled: true,
762 },
763 double_loadable: true,
764 shared_libs: ["libnondoubleloadable"],
765 }
766
767 cc_library {
768 name: "libnondoubleloadable",
769 vendor_available: false,
770 vndk: {
771 enabled: true,
772 },
773 double_loadable: true,
774 }
775 `)
776 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
777 testCc(t, `
778 cc_library {
779 name: "libllndk",
780 shared_libs: ["libcoreonly"],
781 }
782
783 llndk_library {
784 name: "libllndk",
785 symbol_file: "",
786 }
787
788 cc_library {
789 name: "libcoreonly",
790 shared_libs: ["libvendoravailable"],
791 }
792
793 // indirect dependency of LLNDK
794 cc_library {
795 name: "libvendoravailable",
796 vendor_available: true,
797 double_loadable: true,
798 }
799 `)
800}
801
Inseob Kim8471cda2019-11-15 09:59:12 +0900802func TestVendorSnapshot(t *testing.T) {
803 bp := `
804 cc_library {
805 name: "libvndk",
806 vendor_available: true,
807 vndk: {
808 enabled: true,
809 },
810 nocrt: true,
811 }
812
813 cc_library {
814 name: "libvendor",
815 vendor: true,
816 nocrt: true,
817 }
818
819 cc_library {
820 name: "libvendor_available",
821 vendor_available: true,
822 nocrt: true,
823 }
824
825 cc_library_headers {
826 name: "libvendor_headers",
827 vendor_available: true,
828 nocrt: true,
829 }
830
831 cc_binary {
832 name: "vendor_bin",
833 vendor: true,
834 nocrt: true,
835 }
836
837 cc_binary {
838 name: "vendor_available_bin",
839 vendor_available: true,
840 nocrt: true,
841 }
842`
843 config := TestConfig(buildDir, android.Android, nil, bp, nil)
844 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
845 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
846 ctx := testCcWithConfig(t, config)
847
848 // Check Vendor snapshot output.
849
850 snapshotDir := "vendor-snapshot"
851 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
852
853 for _, arch := range [][]string{
854 []string{"arm64", "armv8-a"},
855 []string{"arm", "armv7-a-neon"},
856 } {
857 archType := arch[0]
858 archVariant := arch[1]
859 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
860
861 // For shared libraries, only non-VNDK vendor_available modules are captured
862 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
863 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
864 checkSnapshot(t, ctx, "vendor-snapshot", "libvendor", "libvendor.so", sharedDir, sharedVariant)
865 checkSnapshot(t, ctx, "vendor-snapshot", "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
866
867 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
868 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
869 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
870 checkSnapshot(t, ctx, "vendor-snapshot", "libvndk", "libvndk.a", staticDir, staticVariant)
871 checkSnapshot(t, ctx, "vendor-snapshot", "libvendor", "libvendor.a", staticDir, staticVariant)
872 checkSnapshot(t, ctx, "vendor-snapshot", "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
873
874 // For binary libraries, all vendor:true and vendor_available modules are captured.
875 if archType == "arm64" {
876 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
877 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
878 checkSnapshot(t, ctx, "vendor-snapshot", "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
879 checkSnapshot(t, ctx, "vendor-snapshot", "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
880 }
881 }
882}
883
Jooyung Hana70f0672019-01-18 15:20:43 +0900884func TestDoubleLoadableDepError(t *testing.T) {
885 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
886 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
887 cc_library {
888 name: "libllndk",
889 shared_libs: ["libnondoubleloadable"],
890 }
891
892 llndk_library {
893 name: "libllndk",
894 symbol_file: "",
895 }
896
897 cc_library {
898 name: "libnondoubleloadable",
899 vendor_available: true,
900 vndk: {
901 enabled: true,
902 },
903 }
904 `)
905
906 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
907 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
908 cc_library {
909 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -0700910 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900911 shared_libs: ["libnondoubleloadable"],
912 }
913
914 llndk_library {
915 name: "libllndk",
916 symbol_file: "",
917 }
918
919 cc_library {
920 name: "libnondoubleloadable",
921 vendor_available: true,
922 }
923 `)
924
925 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
926 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
927 cc_library {
928 name: "libdoubleloadable",
929 vendor_available: true,
930 double_loadable: true,
931 shared_libs: ["libnondoubleloadable"],
932 }
933
934 cc_library {
935 name: "libnondoubleloadable",
936 vendor_available: true,
937 }
938 `)
939
940 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
941 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
942 cc_library {
943 name: "libdoubleloadable",
944 vendor_available: true,
945 double_loadable: true,
946 shared_libs: ["libnondoubleloadable"],
947 }
948
949 cc_library {
950 name: "libnondoubleloadable",
951 vendor_available: true,
952 vndk: {
953 enabled: true,
954 },
955 }
956 `)
957
958 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
959 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
960 cc_library {
961 name: "libdoubleloadable",
962 vendor_available: true,
963 vndk: {
964 enabled: true,
965 },
966 double_loadable: true,
967 shared_libs: ["libnondoubleloadable"],
968 }
969
970 cc_library {
971 name: "libnondoubleloadable",
972 vendor_available: false,
973 vndk: {
974 enabled: true,
975 },
976 }
977 `)
978
979 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
980 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
981 cc_library {
982 name: "libllndk",
983 shared_libs: ["libcoreonly"],
984 }
985
986 llndk_library {
987 name: "libllndk",
988 symbol_file: "",
989 }
990
991 cc_library {
992 name: "libcoreonly",
993 shared_libs: ["libvendoravailable"],
994 }
995
996 // indirect dependency of LLNDK
997 cc_library {
998 name: "libvendoravailable",
999 vendor_available: true,
1000 }
1001 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001002}
1003
Justin Yun9357f4a2018-11-28 15:14:47 +09001004func TestVndkMustNotBeProductSpecific(t *testing.T) {
1005 // Check whether an error is emitted when a vndk lib has 'product_specific: true'.
1006 testCcError(t, "product_specific must not be true when `vndk: {enabled: true}`", `
1007 cc_library {
1008 name: "libvndk",
1009 product_specific: true, // Cause error
1010 vendor_available: true,
1011 vndk: {
1012 enabled: true,
1013 },
1014 nocrt: true,
1015 }
1016 `)
1017}
1018
Logan Chienf3511742017-10-31 18:04:35 +08001019func TestVndkExt(t *testing.T) {
1020 // This test checks the VNDK-Ext properties.
1021 ctx := testCc(t, `
1022 cc_library {
1023 name: "libvndk",
1024 vendor_available: true,
1025 vndk: {
1026 enabled: true,
1027 },
1028 nocrt: true,
1029 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001030 cc_library {
1031 name: "libvndk2",
1032 vendor_available: true,
1033 vndk: {
1034 enabled: true,
1035 },
1036 target: {
1037 vendor: {
1038 suffix: "-suffix",
1039 },
1040 },
1041 nocrt: true,
1042 }
Logan Chienf3511742017-10-31 18:04:35 +08001043
1044 cc_library {
1045 name: "libvndk_ext",
1046 vendor: true,
1047 vndk: {
1048 enabled: true,
1049 extends: "libvndk",
1050 },
1051 nocrt: true,
1052 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001053
1054 cc_library {
1055 name: "libvndk2_ext",
1056 vendor: true,
1057 vndk: {
1058 enabled: true,
1059 extends: "libvndk2",
1060 },
1061 nocrt: true,
1062 }
Logan Chienf3511742017-10-31 18:04:35 +08001063 `)
1064
1065 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk")
Jooyung Han4c2b9422019-10-22 19:53:47 +09001066
1067 mod := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1068 assertString(t, mod.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001069}
1070
Logan Chiend3c59a22018-03-29 14:08:15 +08001071func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001072 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1073 ctx := testCcNoVndk(t, `
1074 cc_library {
1075 name: "libvndk",
1076 vendor_available: true,
1077 vndk: {
1078 enabled: true,
1079 },
1080 nocrt: true,
1081 }
1082
1083 cc_library {
1084 name: "libvndk_ext",
1085 vendor: true,
1086 vndk: {
1087 enabled: true,
1088 extends: "libvndk",
1089 },
1090 nocrt: true,
1091 }
1092 `)
1093
1094 // Ensures that the core variant of "libvndk_ext" can be found.
1095 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1096 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1097 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1098 }
1099}
1100
1101func TestVndkExtError(t *testing.T) {
1102 // This test ensures an error is emitted in ill-formed vndk-ext definition.
1103 testCcError(t, "must set `vendor: true` to set `extends: \".*\"`", `
1104 cc_library {
1105 name: "libvndk",
1106 vendor_available: true,
1107 vndk: {
1108 enabled: true,
1109 },
1110 nocrt: true,
1111 }
1112
1113 cc_library {
1114 name: "libvndk_ext",
1115 vndk: {
1116 enabled: true,
1117 extends: "libvndk",
1118 },
1119 nocrt: true,
1120 }
1121 `)
1122
1123 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1124 cc_library {
1125 name: "libvndk",
1126 vendor_available: true,
1127 vndk: {
1128 enabled: true,
1129 },
1130 nocrt: true,
1131 }
1132
1133 cc_library {
1134 name: "libvndk_ext",
1135 vendor: true,
1136 vndk: {
1137 enabled: true,
1138 },
1139 nocrt: true,
1140 }
1141 `)
1142}
1143
1144func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1145 // This test ensures an error is emitted for inconsistent support_system_process.
1146 testCcError(t, "module \".*\" with mismatched support_system_process", `
1147 cc_library {
1148 name: "libvndk",
1149 vendor_available: true,
1150 vndk: {
1151 enabled: true,
1152 },
1153 nocrt: true,
1154 }
1155
1156 cc_library {
1157 name: "libvndk_sp_ext",
1158 vendor: true,
1159 vndk: {
1160 enabled: true,
1161 extends: "libvndk",
1162 support_system_process: true,
1163 },
1164 nocrt: true,
1165 }
1166 `)
1167
1168 testCcError(t, "module \".*\" with mismatched support_system_process", `
1169 cc_library {
1170 name: "libvndk_sp",
1171 vendor_available: true,
1172 vndk: {
1173 enabled: true,
1174 support_system_process: true,
1175 },
1176 nocrt: true,
1177 }
1178
1179 cc_library {
1180 name: "libvndk_ext",
1181 vendor: true,
1182 vndk: {
1183 enabled: true,
1184 extends: "libvndk_sp",
1185 },
1186 nocrt: true,
1187 }
1188 `)
1189}
1190
1191func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001192 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Logan Chienf3511742017-10-31 18:04:35 +08001193 // with `vendor_available: false`.
1194 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `
1195 cc_library {
1196 name: "libvndk",
1197 vendor_available: false,
1198 vndk: {
1199 enabled: true,
1200 },
1201 nocrt: true,
1202 }
1203
1204 cc_library {
1205 name: "libvndk_ext",
1206 vendor: true,
1207 vndk: {
1208 enabled: true,
1209 extends: "libvndk",
1210 },
1211 nocrt: true,
1212 }
1213 `)
1214}
1215
Logan Chiend3c59a22018-03-29 14:08:15 +08001216func TestVendorModuleUseVndkExt(t *testing.T) {
1217 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001218 testCc(t, `
1219 cc_library {
1220 name: "libvndk",
1221 vendor_available: true,
1222 vndk: {
1223 enabled: true,
1224 },
1225 nocrt: true,
1226 }
1227
1228 cc_library {
1229 name: "libvndk_ext",
1230 vendor: true,
1231 vndk: {
1232 enabled: true,
1233 extends: "libvndk",
1234 },
1235 nocrt: true,
1236 }
1237
1238 cc_library {
1239
1240 name: "libvndk_sp",
1241 vendor_available: true,
1242 vndk: {
1243 enabled: true,
1244 support_system_process: true,
1245 },
1246 nocrt: true,
1247 }
1248
1249 cc_library {
1250 name: "libvndk_sp_ext",
1251 vendor: true,
1252 vndk: {
1253 enabled: true,
1254 extends: "libvndk_sp",
1255 support_system_process: true,
1256 },
1257 nocrt: true,
1258 }
1259
1260 cc_library {
1261 name: "libvendor",
1262 vendor: true,
1263 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1264 nocrt: true,
1265 }
1266 `)
1267}
1268
Logan Chiend3c59a22018-03-29 14:08:15 +08001269func TestVndkExtUseVendorLib(t *testing.T) {
1270 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001271 testCc(t, `
1272 cc_library {
1273 name: "libvndk",
1274 vendor_available: true,
1275 vndk: {
1276 enabled: true,
1277 },
1278 nocrt: true,
1279 }
1280
1281 cc_library {
1282 name: "libvndk_ext",
1283 vendor: true,
1284 vndk: {
1285 enabled: true,
1286 extends: "libvndk",
1287 },
1288 shared_libs: ["libvendor"],
1289 nocrt: true,
1290 }
1291
1292 cc_library {
1293 name: "libvendor",
1294 vendor: true,
1295 nocrt: true,
1296 }
1297 `)
Logan Chienf3511742017-10-31 18:04:35 +08001298
Logan Chiend3c59a22018-03-29 14:08:15 +08001299 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1300 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001301 cc_library {
1302 name: "libvndk_sp",
1303 vendor_available: true,
1304 vndk: {
1305 enabled: true,
1306 support_system_process: true,
1307 },
1308 nocrt: true,
1309 }
1310
1311 cc_library {
1312 name: "libvndk_sp_ext",
1313 vendor: true,
1314 vndk: {
1315 enabled: true,
1316 extends: "libvndk_sp",
1317 support_system_process: true,
1318 },
1319 shared_libs: ["libvendor"], // Cause an error
1320 nocrt: true,
1321 }
1322
1323 cc_library {
1324 name: "libvendor",
1325 vendor: true,
1326 nocrt: true,
1327 }
1328 `)
1329}
1330
Logan Chiend3c59a22018-03-29 14:08:15 +08001331func TestVndkSpExtUseVndkError(t *testing.T) {
1332 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1333 // library.
1334 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1335 cc_library {
1336 name: "libvndk",
1337 vendor_available: true,
1338 vndk: {
1339 enabled: true,
1340 },
1341 nocrt: true,
1342 }
1343
1344 cc_library {
1345 name: "libvndk_sp",
1346 vendor_available: true,
1347 vndk: {
1348 enabled: true,
1349 support_system_process: true,
1350 },
1351 nocrt: true,
1352 }
1353
1354 cc_library {
1355 name: "libvndk_sp_ext",
1356 vendor: true,
1357 vndk: {
1358 enabled: true,
1359 extends: "libvndk_sp",
1360 support_system_process: true,
1361 },
1362 shared_libs: ["libvndk"], // Cause an error
1363 nocrt: true,
1364 }
1365 `)
1366
1367 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1368 // library.
1369 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1370 cc_library {
1371 name: "libvndk",
1372 vendor_available: true,
1373 vndk: {
1374 enabled: true,
1375 },
1376 nocrt: true,
1377 }
1378
1379 cc_library {
1380 name: "libvndk_ext",
1381 vendor: true,
1382 vndk: {
1383 enabled: true,
1384 extends: "libvndk",
1385 },
1386 nocrt: true,
1387 }
1388
1389 cc_library {
1390 name: "libvndk_sp",
1391 vendor_available: true,
1392 vndk: {
1393 enabled: true,
1394 support_system_process: true,
1395 },
1396 nocrt: true,
1397 }
1398
1399 cc_library {
1400 name: "libvndk_sp_ext",
1401 vendor: true,
1402 vndk: {
1403 enabled: true,
1404 extends: "libvndk_sp",
1405 support_system_process: true,
1406 },
1407 shared_libs: ["libvndk_ext"], // Cause an error
1408 nocrt: true,
1409 }
1410 `)
1411}
1412
1413func TestVndkUseVndkExtError(t *testing.T) {
1414 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1415 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001416 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1417 cc_library {
1418 name: "libvndk",
1419 vendor_available: true,
1420 vndk: {
1421 enabled: true,
1422 },
1423 nocrt: true,
1424 }
1425
1426 cc_library {
1427 name: "libvndk_ext",
1428 vendor: true,
1429 vndk: {
1430 enabled: true,
1431 extends: "libvndk",
1432 },
1433 nocrt: true,
1434 }
1435
1436 cc_library {
1437 name: "libvndk2",
1438 vendor_available: true,
1439 vndk: {
1440 enabled: true,
1441 },
1442 shared_libs: ["libvndk_ext"],
1443 nocrt: true,
1444 }
1445 `)
1446
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001447 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001448 cc_library {
1449 name: "libvndk",
1450 vendor_available: true,
1451 vndk: {
1452 enabled: true,
1453 },
1454 nocrt: true,
1455 }
1456
1457 cc_library {
1458 name: "libvndk_ext",
1459 vendor: true,
1460 vndk: {
1461 enabled: true,
1462 extends: "libvndk",
1463 },
1464 nocrt: true,
1465 }
1466
1467 cc_library {
1468 name: "libvndk2",
1469 vendor_available: true,
1470 vndk: {
1471 enabled: true,
1472 },
1473 target: {
1474 vendor: {
1475 shared_libs: ["libvndk_ext"],
1476 },
1477 },
1478 nocrt: true,
1479 }
1480 `)
1481
1482 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1483 cc_library {
1484 name: "libvndk_sp",
1485 vendor_available: true,
1486 vndk: {
1487 enabled: true,
1488 support_system_process: true,
1489 },
1490 nocrt: true,
1491 }
1492
1493 cc_library {
1494 name: "libvndk_sp_ext",
1495 vendor: true,
1496 vndk: {
1497 enabled: true,
1498 extends: "libvndk_sp",
1499 support_system_process: true,
1500 },
1501 nocrt: true,
1502 }
1503
1504 cc_library {
1505 name: "libvndk_sp_2",
1506 vendor_available: true,
1507 vndk: {
1508 enabled: true,
1509 support_system_process: true,
1510 },
1511 shared_libs: ["libvndk_sp_ext"],
1512 nocrt: true,
1513 }
1514 `)
1515
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001516 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001517 cc_library {
1518 name: "libvndk_sp",
1519 vendor_available: true,
1520 vndk: {
1521 enabled: true,
1522 },
1523 nocrt: true,
1524 }
1525
1526 cc_library {
1527 name: "libvndk_sp_ext",
1528 vendor: true,
1529 vndk: {
1530 enabled: true,
1531 extends: "libvndk_sp",
1532 },
1533 nocrt: true,
1534 }
1535
1536 cc_library {
1537 name: "libvndk_sp2",
1538 vendor_available: true,
1539 vndk: {
1540 enabled: true,
1541 },
1542 target: {
1543 vendor: {
1544 shared_libs: ["libvndk_sp_ext"],
1545 },
1546 },
1547 nocrt: true,
1548 }
1549 `)
1550}
1551
Justin Yun5f7f7e82019-11-18 19:52:14 +09001552func TestEnforceProductVndkVersion(t *testing.T) {
1553 bp := `
1554 cc_library {
1555 name: "libllndk",
1556 }
1557 llndk_library {
1558 name: "libllndk",
1559 symbol_file: "",
1560 }
1561 cc_library {
1562 name: "libvndk",
1563 vendor_available: true,
1564 vndk: {
1565 enabled: true,
1566 },
1567 nocrt: true,
1568 }
1569 cc_library {
1570 name: "libvndk_sp",
1571 vendor_available: true,
1572 vndk: {
1573 enabled: true,
1574 support_system_process: true,
1575 },
1576 nocrt: true,
1577 }
1578 cc_library {
1579 name: "libva",
1580 vendor_available: true,
1581 nocrt: true,
1582 }
1583 cc_library {
1584 name: "libproduct_va",
1585 product_specific: true,
1586 vendor_available: true,
1587 nocrt: true,
1588 }
1589 cc_library {
1590 name: "libprod",
1591 product_specific: true,
1592 shared_libs: [
1593 "libllndk",
1594 "libvndk",
1595 "libvndk_sp",
1596 "libva",
1597 "libproduct_va",
1598 ],
1599 nocrt: true,
1600 }
1601 cc_library {
1602 name: "libvendor",
1603 vendor: true,
1604 shared_libs: [
1605 "libllndk",
1606 "libvndk",
1607 "libvndk_sp",
1608 "libva",
1609 "libproduct_va",
1610 ],
1611 nocrt: true,
1612 }
1613 `
1614
1615 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1616 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1617 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1618 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1619
1620 ctx := testCcWithConfig(t, config)
1621
1622 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "")
1623 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "")
1624}
1625
1626func TestEnforceProductVndkVersionErrors(t *testing.T) {
1627 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1628 cc_library {
1629 name: "libprod",
1630 product_specific: true,
1631 shared_libs: [
1632 "libvendor",
1633 ],
1634 nocrt: true,
1635 }
1636 cc_library {
1637 name: "libvendor",
1638 vendor: true,
1639 nocrt: true,
1640 }
1641 `)
1642 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1643 cc_library {
1644 name: "libprod",
1645 product_specific: true,
1646 shared_libs: [
1647 "libsystem",
1648 ],
1649 nocrt: true,
1650 }
1651 cc_library {
1652 name: "libsystem",
1653 nocrt: true,
1654 }
1655 `)
1656 testCcErrorProductVndk(t, "Vendor module that is not VNDK should not link to \".*\" which is marked as `vendor_available: false`", `
1657 cc_library {
1658 name: "libprod",
1659 product_specific: true,
1660 shared_libs: [
1661 "libvndk_private",
1662 ],
1663 nocrt: true,
1664 }
1665 cc_library {
1666 name: "libvndk_private",
1667 vendor_available: false,
1668 vndk: {
1669 enabled: true,
1670 },
1671 nocrt: true,
1672 }
1673 `)
1674 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
1675 cc_library {
1676 name: "libprod",
1677 product_specific: true,
1678 shared_libs: [
1679 "libsystem_ext",
1680 ],
1681 nocrt: true,
1682 }
1683 cc_library {
1684 name: "libsystem_ext",
1685 system_ext_specific: true,
1686 nocrt: true,
1687 }
1688 `)
1689 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
1690 cc_library {
1691 name: "libsystem",
1692 shared_libs: [
1693 "libproduct_va",
1694 ],
1695 nocrt: true,
1696 }
1697 cc_library {
1698 name: "libproduct_va",
1699 product_specific: true,
1700 vendor_available: true,
1701 nocrt: true,
1702 }
1703 `)
1704}
1705
Jooyung Han38002912019-05-16 04:01:54 +09001706func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001707 bp := `
1708 cc_library {
1709 name: "libvndk",
1710 vendor_available: true,
1711 vndk: {
1712 enabled: true,
1713 },
1714 }
1715 cc_library {
1716 name: "libvndksp",
1717 vendor_available: true,
1718 vndk: {
1719 enabled: true,
1720 support_system_process: true,
1721 },
1722 }
1723 cc_library {
1724 name: "libvndkprivate",
1725 vendor_available: false,
1726 vndk: {
1727 enabled: true,
1728 },
1729 }
1730 cc_library {
1731 name: "libvendor",
1732 vendor: true,
1733 }
1734 cc_library {
1735 name: "libvndkext",
1736 vendor: true,
1737 vndk: {
1738 enabled: true,
1739 extends: "libvndk",
1740 },
1741 }
1742 vndk_prebuilt_shared {
1743 name: "prevndk",
1744 version: "27",
1745 target_arch: "arm",
1746 binder32bit: true,
1747 vendor_available: true,
1748 vndk: {
1749 enabled: true,
1750 },
1751 arch: {
1752 arm: {
1753 srcs: ["liba.so"],
1754 },
1755 },
1756 }
1757 cc_library {
1758 name: "libllndk",
1759 }
1760 llndk_library {
1761 name: "libllndk",
1762 symbol_file: "",
1763 }
1764 cc_library {
1765 name: "libllndkprivate",
1766 }
1767 llndk_library {
1768 name: "libllndkprivate",
1769 vendor_available: false,
1770 symbol_file: "",
1771 }`
1772
1773 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09001774 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1775 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1776 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08001777 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09001778
Jooyung Han0302a842019-10-30 18:43:49 +09001779 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09001780 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09001781 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09001782 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09001783 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09001784 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09001785 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09001786 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09001787
Colin Crossfb0c16e2019-11-20 17:12:35 -08001788 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09001789
Jooyung Han38002912019-05-16 04:01:54 +09001790 tests := []struct {
1791 variant string
1792 name string
1793 expected string
1794 }{
1795 {vendorVariant, "libvndk", "native:vndk"},
1796 {vendorVariant, "libvndksp", "native:vndk"},
1797 {vendorVariant, "libvndkprivate", "native:vndk_private"},
1798 {vendorVariant, "libvendor", "native:vendor"},
1799 {vendorVariant, "libvndkext", "native:vendor"},
Jooyung Han38002912019-05-16 04:01:54 +09001800 {vendorVariant, "libllndk.llndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09001801 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09001802 {coreVariant, "libvndk", "native:platform"},
1803 {coreVariant, "libvndkprivate", "native:platform"},
1804 {coreVariant, "libllndk", "native:platform"},
1805 }
1806 for _, test := range tests {
1807 t.Run(test.name, func(t *testing.T) {
1808 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
1809 assertString(t, module.makeLinkType, test.expected)
1810 })
1811 }
1812}
1813
Colin Cross0af4b842015-04-30 16:36:18 -07001814var (
1815 str11 = "01234567891"
1816 str10 = str11[:10]
1817 str9 = str11[:9]
1818 str5 = str11[:5]
1819 str4 = str11[:4]
1820)
1821
1822var splitListForSizeTestCases = []struct {
1823 in []string
1824 out [][]string
1825 size int
1826}{
1827 {
1828 in: []string{str10},
1829 out: [][]string{{str10}},
1830 size: 10,
1831 },
1832 {
1833 in: []string{str9},
1834 out: [][]string{{str9}},
1835 size: 10,
1836 },
1837 {
1838 in: []string{str5},
1839 out: [][]string{{str5}},
1840 size: 10,
1841 },
1842 {
1843 in: []string{str11},
1844 out: nil,
1845 size: 10,
1846 },
1847 {
1848 in: []string{str10, str10},
1849 out: [][]string{{str10}, {str10}},
1850 size: 10,
1851 },
1852 {
1853 in: []string{str9, str10},
1854 out: [][]string{{str9}, {str10}},
1855 size: 10,
1856 },
1857 {
1858 in: []string{str10, str9},
1859 out: [][]string{{str10}, {str9}},
1860 size: 10,
1861 },
1862 {
1863 in: []string{str5, str4},
1864 out: [][]string{{str5, str4}},
1865 size: 10,
1866 },
1867 {
1868 in: []string{str5, str4, str5},
1869 out: [][]string{{str5, str4}, {str5}},
1870 size: 10,
1871 },
1872 {
1873 in: []string{str5, str4, str5, str4},
1874 out: [][]string{{str5, str4}, {str5, str4}},
1875 size: 10,
1876 },
1877 {
1878 in: []string{str5, str4, str5, str5},
1879 out: [][]string{{str5, str4}, {str5}, {str5}},
1880 size: 10,
1881 },
1882 {
1883 in: []string{str5, str5, str5, str4},
1884 out: [][]string{{str5}, {str5}, {str5, str4}},
1885 size: 10,
1886 },
1887 {
1888 in: []string{str9, str11},
1889 out: nil,
1890 size: 10,
1891 },
1892 {
1893 in: []string{str11, str9},
1894 out: nil,
1895 size: 10,
1896 },
1897}
1898
1899func TestSplitListForSize(t *testing.T) {
1900 for _, testCase := range splitListForSizeTestCases {
Colin Cross40e33732019-02-15 11:08:35 -08001901 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)
Colin Cross5b529592017-05-09 13:34:34 -07001902
1903 var outStrings [][]string
1904
1905 if len(out) > 0 {
1906 outStrings = make([][]string, len(out))
1907 for i, o := range out {
1908 outStrings[i] = o.Strings()
1909 }
1910 }
1911
1912 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -07001913 t.Errorf("incorrect output:")
1914 t.Errorf(" input: %#v", testCase.in)
1915 t.Errorf(" size: %d", testCase.size)
1916 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -07001917 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -07001918 }
1919 }
1920}
Jeff Gaston294356f2017-09-27 17:05:30 -07001921
1922var staticLinkDepOrderTestCases = []struct {
1923 // This is a string representation of a map[moduleName][]moduleDependency .
1924 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001925 inStatic string
1926
1927 // This is a string representation of a map[moduleName][]moduleDependency .
1928 // It models the dependencies declared in an Android.bp file.
1929 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07001930
1931 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
1932 // The keys of allOrdered specify which modules we would like to check.
1933 // The values of allOrdered specify the expected result (of the transitive closure of all
1934 // dependencies) for each module to test
1935 allOrdered string
1936
1937 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
1938 // The keys of outOrdered specify which modules we would like to check.
1939 // The values of outOrdered specify the expected result (of the ordered linker command line)
1940 // for each module to test.
1941 outOrdered string
1942}{
1943 // Simple tests
1944 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001945 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07001946 outOrdered: "",
1947 },
1948 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001949 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001950 outOrdered: "a:",
1951 },
1952 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001953 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001954 outOrdered: "a:b; b:",
1955 },
1956 // Tests of reordering
1957 {
1958 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001959 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07001960 outOrdered: "a:b,c,d; b:d; c:d; d:",
1961 },
1962 {
1963 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001964 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07001965 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
1966 },
1967 {
1968 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001969 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07001970 outOrdered: "a:d,b,e,c; d:b; e:c",
1971 },
1972 {
1973 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001974 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07001975 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
1976 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
1977 },
1978 {
1979 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001980 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 -07001981 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
1982 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
1983 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001984 // shared dependencies
1985 {
1986 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
1987 // So, we don't actually have to check that a shared dependency of c will change the order
1988 // of a library that depends statically on b and on c. We only need to check that if c has
1989 // a shared dependency on b, that that shows up in allOrdered.
1990 inShared: "c:b",
1991 allOrdered: "c:b",
1992 outOrdered: "c:",
1993 },
1994 {
1995 // This test doesn't actually include any shared dependencies but it's a reminder of what
1996 // the second phase of the above test would look like
1997 inStatic: "a:b,c; c:b",
1998 allOrdered: "a:c,b; c:b",
1999 outOrdered: "a:c,b; c:b",
2000 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002001 // tiebreakers for when two modules specifying different orderings and there is no dependency
2002 // to dictate an order
2003 {
2004 // 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 -08002005 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002006 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2007 },
2008 {
2009 // 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 -08002010 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 -07002011 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2012 },
2013 // Tests involving duplicate dependencies
2014 {
2015 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002016 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002017 outOrdered: "a:c,b",
2018 },
2019 {
2020 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002021 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002022 outOrdered: "a:d,c,b",
2023 },
2024 // Tests to confirm the nonexistence of infinite loops.
2025 // These cases should never happen, so as long as the test terminates and the
2026 // result is deterministic then that should be fine.
2027 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002028 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002029 outOrdered: "a:a",
2030 },
2031 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002032 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002033 allOrdered: "a:b,c; b:c,a; c:a,b",
2034 outOrdered: "a:b; b:c; c:a",
2035 },
2036 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002037 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002038 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2039 outOrdered: "a:c,b; b:a,c; c:b,a",
2040 },
2041}
2042
2043// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2044func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2045 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2046 strippedText := strings.Replace(text, " ", "", -1)
2047 if len(strippedText) < 1 {
2048 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2049 }
2050 allDeps = make(map[android.Path][]android.Path, 0)
2051
2052 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2053 moduleTexts := strings.Split(strippedText, ";")
2054
2055 outputForModuleName := func(moduleName string) android.Path {
2056 return android.PathForTesting(moduleName)
2057 }
2058
2059 for _, moduleText := range moduleTexts {
2060 // convert from "a:b,c" to ["a", "b,c"]
2061 components := strings.Split(moduleText, ":")
2062 if len(components) != 2 {
2063 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2064 }
2065 moduleName := components[0]
2066 moduleOutput := outputForModuleName(moduleName)
2067 modulesInOrder = append(modulesInOrder, moduleOutput)
2068
2069 depString := components[1]
2070 // convert from "b,c" to ["b", "c"]
2071 depNames := strings.Split(depString, ",")
2072 if len(depString) < 1 {
2073 depNames = []string{}
2074 }
2075 var deps []android.Path
2076 for _, depName := range depNames {
2077 deps = append(deps, outputForModuleName(depName))
2078 }
2079 allDeps[moduleOutput] = deps
2080 }
2081 return modulesInOrder, allDeps
2082}
2083
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002084func TestLinkReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002085 for _, testCase := range staticLinkDepOrderTestCases {
2086 errs := []string{}
2087
2088 // parse testcase
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002089 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)
Jeff Gaston294356f2017-09-27 17:05:30 -07002090 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)
2091 if testCase.allOrdered == "" {
2092 // allow the test case to skip specifying allOrdered
2093 testCase.allOrdered = testCase.outOrdered
2094 }
2095 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002096 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)
Jeff Gaston294356f2017-09-27 17:05:30 -07002097
2098 // For each module whose post-reordered dependencies were specified, validate that
2099 // reordering the inputs produces the expected outputs.
2100 for _, moduleName := range expectedModuleNames {
2101 moduleDeps := givenTransitiveDeps[moduleName]
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002102 givenSharedDeps := givenAllSharedDeps[moduleName]
2103 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -07002104
2105 correctAllOrdered := expectedAllDeps[moduleName]
2106 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {
2107 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002108 "\nin static:%q"+
2109 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002110 "\nmodule: %v"+
2111 "\nexpected: %s"+
2112 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002113 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002114 }
2115
2116 correctOutputDeps := expectedTransitiveDeps[moduleName]
2117 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {
2118 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002119 "\nin static:%q"+
2120 "\nin shared:%q"+
Jeff Gaston294356f2017-09-27 17:05:30 -07002121 "\nmodule: %v"+
2122 "\nexpected: %s"+
2123 "\nactual: %s",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002124 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))
Jeff Gaston294356f2017-09-27 17:05:30 -07002125 }
2126 }
2127
2128 if len(errs) > 0 {
2129 sort.Strings(errs)
2130 for _, err := range errs {
2131 t.Error(err)
2132 }
2133 }
2134 }
2135}
Logan Chienf3511742017-10-31 18:04:35 +08002136
Jeff Gaston294356f2017-09-27 17:05:30 -07002137func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2138 for _, moduleName := range moduleNames {
2139 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
2140 output := module.outputFile.Path()
2141 paths = append(paths, output)
2142 }
2143 return paths
2144}
2145
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002146func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002147 ctx := testCc(t, `
2148 cc_library {
2149 name: "a",
2150 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002151 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002152 }
2153 cc_library {
2154 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002155 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002156 }
2157 cc_library {
2158 name: "c",
2159 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002160 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002161 }
2162 cc_library {
2163 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002164 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002165 }
2166
2167 `)
2168
Colin Cross7113d202019-11-20 16:39:12 -08002169 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002170 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002171 actual := moduleA.depsInLinkOrder
Jeff Gaston294356f2017-09-27 17:05:30 -07002172 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})
2173
2174 if !reflect.DeepEqual(actual, expected) {
2175 t.Errorf("staticDeps orderings were not propagated correctly"+
2176 "\nactual: %v"+
2177 "\nexpected: %v",
2178 actual,
2179 expected,
2180 )
2181 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002182}
Jeff Gaston294356f2017-09-27 17:05:30 -07002183
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002184func TestStaticLibDepReorderingWithShared(t *testing.T) {
2185 ctx := testCc(t, `
2186 cc_library {
2187 name: "a",
2188 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002189 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002190 }
2191 cc_library {
2192 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002193 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002194 }
2195 cc_library {
2196 name: "c",
2197 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002198 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002199 }
2200
2201 `)
2202
Colin Cross7113d202019-11-20 16:39:12 -08002203 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002204 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
2205 actual := moduleA.depsInLinkOrder
2206 expected := getOutputPaths(ctx, variant, []string{"c", "b"})
2207
2208 if !reflect.DeepEqual(actual, expected) {
2209 t.Errorf("staticDeps orderings did not account for shared libs"+
2210 "\nactual: %v"+
2211 "\nexpected: %v",
2212 actual,
2213 expected,
2214 )
2215 }
2216}
2217
Jiyong Parka46a4d52017-12-14 19:54:34 +09002218func TestLlndkHeaders(t *testing.T) {
2219 ctx := testCc(t, `
2220 llndk_headers {
2221 name: "libllndk_headers",
2222 export_include_dirs: ["my_include"],
2223 }
2224 llndk_library {
2225 name: "libllndk",
2226 export_llndk_headers: ["libllndk_headers"],
2227 }
2228 cc_library {
2229 name: "libvendor",
2230 shared_libs: ["libllndk"],
2231 vendor: true,
2232 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002233 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002234 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002235 }
2236 `)
2237
2238 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002239 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002240 cflags := cc.Args["cFlags"]
2241 if !strings.Contains(cflags, "-Imy_include") {
2242 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2243 }
2244}
2245
Logan Chien43d34c32017-12-20 01:17:32 +08002246func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2247 actual := module.Properties.AndroidMkRuntimeLibs
2248 if !reflect.DeepEqual(actual, expected) {
2249 t.Errorf("incorrect runtime_libs for shared libs"+
2250 "\nactual: %v"+
2251 "\nexpected: %v",
2252 actual,
2253 expected,
2254 )
2255 }
2256}
2257
2258const runtimeLibAndroidBp = `
2259 cc_library {
2260 name: "libvendor_available1",
2261 vendor_available: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002262 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002263 nocrt : true,
2264 system_shared_libs : [],
2265 }
2266 cc_library {
2267 name: "libvendor_available2",
2268 vendor_available: true,
2269 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002270 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002271 nocrt : true,
2272 system_shared_libs : [],
2273 }
2274 cc_library {
2275 name: "libvendor_available3",
2276 vendor_available: true,
2277 runtime_libs: ["libvendor_available1"],
2278 target: {
2279 vendor: {
2280 exclude_runtime_libs: ["libvendor_available1"],
2281 }
2282 },
Yi Konge7fe9912019-06-02 00:53:50 -07002283 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002284 nocrt : true,
2285 system_shared_libs : [],
2286 }
2287 cc_library {
2288 name: "libcore",
2289 runtime_libs: ["libvendor_available1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002290 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002291 nocrt : true,
2292 system_shared_libs : [],
2293 }
2294 cc_library {
2295 name: "libvendor1",
2296 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002297 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002298 nocrt : true,
2299 system_shared_libs : [],
2300 }
2301 cc_library {
2302 name: "libvendor2",
2303 vendor: true,
2304 runtime_libs: ["libvendor_available1", "libvendor1"],
Yi Konge7fe9912019-06-02 00:53:50 -07002305 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002306 nocrt : true,
2307 system_shared_libs : [],
2308 }
2309`
2310
2311func TestRuntimeLibs(t *testing.T) {
2312 ctx := testCc(t, runtimeLibAndroidBp)
2313
2314 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002315 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002316
2317 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2318 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2319
2320 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
2321 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2322
2323 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2324 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002325 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002326
2327 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2328 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
2329
2330 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2331 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
2332}
2333
2334func TestExcludeRuntimeLibs(t *testing.T) {
2335 ctx := testCc(t, runtimeLibAndroidBp)
2336
Colin Cross7113d202019-11-20 16:39:12 -08002337 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002338 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2339 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2340
Colin Crossfb0c16e2019-11-20 17:12:35 -08002341 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002342 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
2343 checkRuntimeLibs(t, nil, module)
2344}
2345
2346func TestRuntimeLibsNoVndk(t *testing.T) {
2347 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2348
2349 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2350
Colin Cross7113d202019-11-20 16:39:12 -08002351 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002352
2353 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2354 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
2355
2356 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
2357 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
2358}
2359
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002360func checkStaticLibs(t *testing.T, expected []string, module *Module) {
2361 actual := module.Properties.AndroidMkStaticLibs
2362 if !reflect.DeepEqual(actual, expected) {
2363 t.Errorf("incorrect static_libs"+
2364 "\nactual: %v"+
2365 "\nexpected: %v",
2366 actual,
2367 expected,
2368 )
2369 }
2370}
2371
2372const staticLibAndroidBp = `
2373 cc_library {
2374 name: "lib1",
2375 }
2376 cc_library {
2377 name: "lib2",
2378 static_libs: ["lib1"],
2379 }
2380`
2381
2382func TestStaticLibDepExport(t *testing.T) {
2383 ctx := testCc(t, staticLibAndroidBp)
2384
2385 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002386 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002387 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002388 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002389
2390 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002391 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002392 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2393 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002394 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002395}
2396
Jiyong Parkd08b6972017-09-26 10:50:54 +09002397var compilerFlagsTestCases = []struct {
2398 in string
2399 out bool
2400}{
2401 {
2402 in: "a",
2403 out: false,
2404 },
2405 {
2406 in: "-a",
2407 out: true,
2408 },
2409 {
2410 in: "-Ipath/to/something",
2411 out: false,
2412 },
2413 {
2414 in: "-isystempath/to/something",
2415 out: false,
2416 },
2417 {
2418 in: "--coverage",
2419 out: false,
2420 },
2421 {
2422 in: "-include a/b",
2423 out: true,
2424 },
2425 {
2426 in: "-include a/b c/d",
2427 out: false,
2428 },
2429 {
2430 in: "-DMACRO",
2431 out: true,
2432 },
2433 {
2434 in: "-DMAC RO",
2435 out: false,
2436 },
2437 {
2438 in: "-a -b",
2439 out: false,
2440 },
2441 {
2442 in: "-DMACRO=definition",
2443 out: true,
2444 },
2445 {
2446 in: "-DMACRO=defi nition",
2447 out: true, // TODO(jiyong): this should be false
2448 },
2449 {
2450 in: "-DMACRO(x)=x + 1",
2451 out: true,
2452 },
2453 {
2454 in: "-DMACRO=\"defi nition\"",
2455 out: true,
2456 },
2457}
2458
2459type mockContext struct {
2460 BaseModuleContext
2461 result bool
2462}
2463
2464func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2465 // CheckBadCompilerFlags calls this function when the flag should be rejected
2466 ctx.result = false
2467}
2468
2469func TestCompilerFlags(t *testing.T) {
2470 for _, testCase := range compilerFlagsTestCases {
2471 ctx := &mockContext{result: true}
2472 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
2473 if ctx.result != testCase.out {
2474 t.Errorf("incorrect output:")
2475 t.Errorf(" input: %#v", testCase.in)
2476 t.Errorf(" expected: %#v", testCase.out)
2477 t.Errorf(" got: %#v", ctx.result)
2478 }
2479 }
Jeff Gaston294356f2017-09-27 17:05:30 -07002480}
Jiyong Park374510b2018-03-19 18:23:01 +09002481
2482func TestVendorPublicLibraries(t *testing.T) {
2483 ctx := testCc(t, `
2484 cc_library_headers {
2485 name: "libvendorpublic_headers",
2486 export_include_dirs: ["my_include"],
2487 }
2488 vendor_public_library {
2489 name: "libvendorpublic",
2490 symbol_file: "",
2491 export_public_headers: ["libvendorpublic_headers"],
2492 }
2493 cc_library {
2494 name: "libvendorpublic",
2495 srcs: ["foo.c"],
2496 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002497 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002498 nocrt: true,
2499 }
2500
2501 cc_library {
2502 name: "libsystem",
2503 shared_libs: ["libvendorpublic"],
2504 vendor: false,
2505 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002506 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002507 nocrt: true,
2508 }
2509 cc_library {
2510 name: "libvendor",
2511 shared_libs: ["libvendorpublic"],
2512 vendor: true,
2513 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002514 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09002515 nocrt: true,
2516 }
2517 `)
2518
Colin Cross7113d202019-11-20 16:39:12 -08002519 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08002520 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09002521
2522 // test if header search paths are correctly added
2523 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08002524 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09002525 cflags := cc.Args["cFlags"]
2526 if !strings.Contains(cflags, "-Imy_include") {
2527 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
2528 }
2529
2530 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08002531 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002532 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002533 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09002534 if !strings.Contains(libflags, stubPaths[0].String()) {
2535 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
2536 }
2537
2538 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08002539 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09002540 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002541 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09002542 if !strings.Contains(libflags, stubPaths[0].String()) {
2543 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
2544 }
2545
2546}
Jiyong Park37b25202018-07-11 10:49:27 +09002547
2548func TestRecovery(t *testing.T) {
2549 ctx := testCc(t, `
2550 cc_library_shared {
2551 name: "librecovery",
2552 recovery: true,
2553 }
2554 cc_library_shared {
2555 name: "librecovery32",
2556 recovery: true,
2557 compile_multilib:"32",
2558 }
Jiyong Park5baac542018-08-28 09:55:37 +09002559 cc_library_shared {
2560 name: "libHalInRecovery",
2561 recovery_available: true,
2562 vendor: true,
2563 }
Jiyong Park37b25202018-07-11 10:49:27 +09002564 `)
2565
2566 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08002567 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09002568 if len(variants) != 1 || !android.InList(arm64, variants) {
2569 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
2570 }
2571
2572 variants = ctx.ModuleVariantsForTests("librecovery32")
2573 if android.InList(arm64, variants) {
2574 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
2575 }
Jiyong Park5baac542018-08-28 09:55:37 +09002576
2577 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
2578 if !recoveryModule.Platform() {
2579 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
2580 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09002581}
Jiyong Park5baac542018-08-28 09:55:37 +09002582
Jiyong Park7ed9de32018-10-15 22:25:07 +09002583func TestVersionedStubs(t *testing.T) {
2584 ctx := testCc(t, `
2585 cc_library_shared {
2586 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002587 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002588 stubs: {
2589 symbol_file: "foo.map.txt",
2590 versions: ["1", "2", "3"],
2591 },
2592 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002593
Jiyong Park7ed9de32018-10-15 22:25:07 +09002594 cc_library_shared {
2595 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09002596 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09002597 shared_libs: ["libFoo#1"],
2598 }`)
2599
2600 variants := ctx.ModuleVariantsForTests("libFoo")
2601 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08002602 "android_arm64_armv8-a_shared",
2603 "android_arm64_armv8-a_shared_1",
2604 "android_arm64_armv8-a_shared_2",
2605 "android_arm64_armv8-a_shared_3",
2606 "android_arm_armv7-a-neon_shared",
2607 "android_arm_armv7-a-neon_shared_1",
2608 "android_arm_armv7-a-neon_shared_2",
2609 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09002610 }
2611 variantsMismatch := false
2612 if len(variants) != len(expectedVariants) {
2613 variantsMismatch = true
2614 } else {
2615 for _, v := range expectedVariants {
2616 if !inList(v, variants) {
2617 variantsMismatch = false
2618 }
2619 }
2620 }
2621 if variantsMismatch {
2622 t.Errorf("variants of libFoo expected:\n")
2623 for _, v := range expectedVariants {
2624 t.Errorf("%q\n", v)
2625 }
2626 t.Errorf(", but got:\n")
2627 for _, v := range variants {
2628 t.Errorf("%q\n", v)
2629 }
2630 }
2631
Colin Cross7113d202019-11-20 16:39:12 -08002632 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09002633 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08002634 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09002635 if !strings.Contains(libFlags, libFoo1StubPath) {
2636 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
2637 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09002638
Colin Cross7113d202019-11-20 16:39:12 -08002639 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09002640 cFlags := libBarCompileRule.Args["cFlags"]
2641 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
2642 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
2643 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
2644 }
Jiyong Park37b25202018-07-11 10:49:27 +09002645}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002646
2647func TestStaticExecutable(t *testing.T) {
2648 ctx := testCc(t, `
2649 cc_binary {
2650 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01002651 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002652 static_executable: true,
2653 }`)
2654
Colin Cross7113d202019-11-20 16:39:12 -08002655 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002656 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
2657 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07002658 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08002659 for _, lib := range systemStaticLibs {
2660 if !strings.Contains(libFlags, lib) {
2661 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
2662 }
2663 }
2664 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
2665 for _, lib := range systemSharedLibs {
2666 if strings.Contains(libFlags, lib) {
2667 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
2668 }
2669 }
2670}
Jiyong Parke4bb9862019-02-01 00:31:10 +09002671
2672func TestStaticDepsOrderWithStubs(t *testing.T) {
2673 ctx := testCc(t, `
2674 cc_binary {
2675 name: "mybin",
2676 srcs: ["foo.c"],
2677 static_libs: ["libB"],
2678 static_executable: true,
2679 stl: "none",
2680 }
2681
2682 cc_library {
2683 name: "libB",
2684 srcs: ["foo.c"],
2685 shared_libs: ["libC"],
2686 stl: "none",
2687 }
2688
2689 cc_library {
2690 name: "libC",
2691 srcs: ["foo.c"],
2692 stl: "none",
2693 stubs: {
2694 versions: ["1"],
2695 },
2696 }`)
2697
Colin Cross7113d202019-11-20 16:39:12 -08002698 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Module().(*Module)
Jiyong Parke4bb9862019-02-01 00:31:10 +09002699 actual := mybin.depsInLinkOrder
Colin Cross7113d202019-11-20 16:39:12 -08002700 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libB", "libC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09002701
2702 if !reflect.DeepEqual(actual, expected) {
2703 t.Errorf("staticDeps orderings were not propagated correctly"+
2704 "\nactual: %v"+
2705 "\nexpected: %v",
2706 actual,
2707 expected,
2708 )
2709 }
2710}
Jooyung Han38002912019-05-16 04:01:54 +09002711
Jooyung Hand48f3c32019-08-23 11:18:57 +09002712func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
2713 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
2714 cc_library {
2715 name: "libA",
2716 srcs: ["foo.c"],
2717 shared_libs: ["libB"],
2718 stl: "none",
2719 }
2720
2721 cc_library {
2722 name: "libB",
2723 srcs: ["foo.c"],
2724 enabled: false,
2725 stl: "none",
2726 }
2727 `)
2728}
2729
Mitch Phillipsda9a4632019-07-15 09:34:09 -07002730// Simple smoke test for the cc_fuzz target that ensures the rule compiles
2731// correctly.
2732func TestFuzzTarget(t *testing.T) {
2733 ctx := testCc(t, `
2734 cc_fuzz {
2735 name: "fuzz_smoke_test",
2736 srcs: ["foo.c"],
2737 }`)
2738
Paul Duffin075c4172019-12-19 19:06:13 +00002739 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07002740 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
2741}
2742
Jiyong Park29074592019-07-07 16:27:47 +09002743func TestAidl(t *testing.T) {
2744}
2745
Jooyung Han38002912019-05-16 04:01:54 +09002746func assertString(t *testing.T, got, expected string) {
2747 t.Helper()
2748 if got != expected {
2749 t.Errorf("expected %q got %q", expected, got)
2750 }
2751}
2752
2753func assertArrayString(t *testing.T, got, expected []string) {
2754 t.Helper()
2755 if len(got) != len(expected) {
2756 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
2757 return
2758 }
2759 for i := range got {
2760 if got[i] != expected[i] {
2761 t.Errorf("expected %d-th %q (%q) got %q (%q)",
2762 i, expected[i], expected, got[i], got)
2763 return
2764 }
2765 }
2766}
Colin Crosse1bb5d02019-09-24 14:55:04 -07002767
Jooyung Han0302a842019-10-30 18:43:49 +09002768func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
2769 t.Helper()
2770 assertArrayString(t, android.SortedStringKeys(m), expected)
2771}
2772
Colin Crosse1bb5d02019-09-24 14:55:04 -07002773func TestDefaults(t *testing.T) {
2774 ctx := testCc(t, `
2775 cc_defaults {
2776 name: "defaults",
2777 srcs: ["foo.c"],
2778 static: {
2779 srcs: ["bar.c"],
2780 },
2781 shared: {
2782 srcs: ["baz.c"],
2783 },
2784 }
2785
2786 cc_library_static {
2787 name: "libstatic",
2788 defaults: ["defaults"],
2789 }
2790
2791 cc_library_shared {
2792 name: "libshared",
2793 defaults: ["defaults"],
2794 }
2795
2796 cc_library {
2797 name: "libboth",
2798 defaults: ["defaults"],
2799 }
2800
2801 cc_binary {
2802 name: "binary",
2803 defaults: ["defaults"],
2804 }`)
2805
2806 pathsToBase := func(paths android.Paths) []string {
2807 var ret []string
2808 for _, p := range paths {
2809 ret = append(ret, p.Base())
2810 }
2811 return ret
2812 }
2813
Colin Cross7113d202019-11-20 16:39:12 -08002814 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002815 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
2816 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
2817 }
Colin Cross7113d202019-11-20 16:39:12 -08002818 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002819 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
2820 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
2821 }
Colin Cross7113d202019-11-20 16:39:12 -08002822 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002823 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
2824 t.Errorf("binary ld rule wanted %q, got %q", w, g)
2825 }
2826
Colin Cross7113d202019-11-20 16:39:12 -08002827 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002828 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
2829 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
2830 }
Colin Cross7113d202019-11-20 16:39:12 -08002831 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07002832 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
2833 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
2834 }
2835}
Colin Crosseabaedd2020-02-06 17:01:55 -08002836
2837func TestProductVariableDefaults(t *testing.T) {
2838 bp := `
2839 cc_defaults {
2840 name: "libfoo_defaults",
2841 srcs: ["foo.c"],
2842 cppflags: ["-DFOO"],
2843 product_variables: {
2844 debuggable: {
2845 cppflags: ["-DBAR"],
2846 },
2847 },
2848 }
2849
2850 cc_library {
2851 name: "libfoo",
2852 defaults: ["libfoo_defaults"],
2853 }
2854 `
2855
2856 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2857 config.TestProductVariables.Debuggable = BoolPtr(true)
2858
2859 ctx := CreateTestContext()
2860 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
2861 ctx.BottomUp("variable", android.VariableMutator).Parallel()
2862 })
2863 ctx.Register(config)
2864
2865 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
2866 android.FailIfErrored(t, errs)
2867 _, errs = ctx.PrepareBuildActions(config)
2868 android.FailIfErrored(t, errs)
2869
2870 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
2871 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
2872 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
2873 }
2874}