blob: 1ab1b82abc6ba04e7eb7536d58691fda2743807d [file] [log] [blame]
Colin Crossd00350c2017-11-17 10:55:38 -08001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross74d1ec02015-04-28 13:30:13 -070015package cc
16
17import (
Jeff Gaston294356f2017-09-27 17:05:30 -070018 "fmt"
Jiyong Park6a43f042017-10-12 23:05:00 +090019 "io/ioutil"
20 "os"
Inseob Kim1f086e22019-05-09 13:29:15 +090021 "path/filepath"
Colin Cross74d1ec02015-04-28 13:30:13 -070022 "reflect"
Jeff Gaston294356f2017-09-27 17:05:30 -070023 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070024 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070025
26 "android/soong/android"
Colin Cross74d1ec02015-04-28 13:30:13 -070027)
28
Jiyong Park6a43f042017-10-12 23:05:00 +090029var buildDir string
30
31func setUp() {
32 var err error
33 buildDir, err = ioutil.TempDir("", "soong_cc_test")
34 if err != nil {
35 panic(err)
36 }
37}
38
39func tearDown() {
40 os.RemoveAll(buildDir)
41}
42
43func TestMain(m *testing.M) {
44 run := func() int {
45 setUp()
46 defer tearDown()
47
48 return m.Run()
49 }
50
51 os.Exit(run())
52}
53
Colin Cross98be1bb2019-12-13 20:41:13 -080054func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070055 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -070056 ctx := CreateTestContext(config)
57 ctx.Register()
Logan Chienf3511742017-10-31 18:04:35 +080058
Jeff Gastond3e141d2017-08-08 17:46:01 -070059 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +080060 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +090061 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +080062 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +090063
64 return ctx
65}
66
Logan Chienf3511742017-10-31 18:04:35 +080067func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080068 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080069 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070070 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun8a2600c2020-12-07 12:44:03 +090071 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Dan Willemsen674dc7f2018-03-12 18:06:05 -070072 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 Yun8a2600c2020-12-07 12:44:03 +090085func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
86 t.Helper()
87 config := TestConfig(buildDir, android.Android, nil, bp, nil)
88 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
89 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
90
91 return testCcWithConfig(t, config)
92}
93
Justin Yun5f7f7e82019-11-18 19:52:14 +090094func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +080095 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +080096
Colin Crossae8600b2020-10-29 17:09:13 -070097 ctx := CreateTestContext(config)
98 ctx.Register()
Logan Chienf3511742017-10-31 18:04:35 +080099
100 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
101 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +0800102 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800103 return
104 }
105
106 _, errs = ctx.PrepareBuildActions(config)
107 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +0800108 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800109 return
110 }
111
112 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
113}
114
Justin Yun5f7f7e82019-11-18 19:52:14 +0900115func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900116 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900117 config := TestConfig(buildDir, android.Android, nil, bp, nil)
118 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
119 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
120 testCcErrorWithConfig(t, pattern, config)
121 return
122}
123
124func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900125 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900126 config := TestConfig(buildDir, android.Android, nil, bp, nil)
127 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
128 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
129 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
130 testCcErrorWithConfig(t, pattern, config)
131 return
132}
133
Logan Chienf3511742017-10-31 18:04:35 +0800134const (
Colin Cross7113d202019-11-20 16:39:12 -0800135 coreVariant = "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800136 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun5f7f7e82019-11-18 19:52:14 +0900137 productVariant = "android_product.VER_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800138 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800139)
140
Doug Hornc32c6b02019-01-17 14:44:05 -0800141func TestFuchsiaDeps(t *testing.T) {
142 t.Helper()
143
144 bp := `
145 cc_library {
146 name: "libTest",
147 srcs: ["foo.c"],
148 target: {
149 fuchsia: {
150 srcs: ["bar.c"],
151 },
152 },
153 }`
154
Colin Cross98be1bb2019-12-13 20:41:13 -0800155 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
156 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800157
158 rt := false
159 fb := false
160
161 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
162 implicits := ld.Implicits
163 for _, lib := range implicits {
164 if strings.Contains(lib.Rel(), "libcompiler_rt") {
165 rt = true
166 }
167
168 if strings.Contains(lib.Rel(), "libbioniccompat") {
169 fb = true
170 }
171 }
172
173 if !rt || !fb {
174 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
175 }
176}
177
178func TestFuchsiaTargetDecl(t *testing.T) {
179 t.Helper()
180
181 bp := `
182 cc_library {
183 name: "libTest",
184 srcs: ["foo.c"],
185 target: {
186 fuchsia: {
187 srcs: ["bar.c"],
188 },
189 },
190 }`
191
Colin Cross98be1bb2019-12-13 20:41:13 -0800192 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
193 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800194 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
195 var objs []string
196 for _, o := range ld.Inputs {
197 objs = append(objs, o.Base())
198 }
199 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
200 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
201 }
202}
203
Jiyong Park6a43f042017-10-12 23:05:00 +0900204func TestVendorSrc(t *testing.T) {
205 ctx := testCc(t, `
206 cc_library {
207 name: "libTest",
208 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700209 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800210 nocrt: true,
211 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900212 vendor_available: true,
213 target: {
214 vendor: {
215 srcs: ["bar.c"],
216 },
217 },
218 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900219 `)
220
Logan Chienf3511742017-10-31 18:04:35 +0800221 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900222 var objs []string
223 for _, o := range ld.Inputs {
224 objs = append(objs, o.Base())
225 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800226 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900227 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
228 }
229}
230
Logan Chienf3511742017-10-31 18:04:35 +0800231func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900232 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800233
Logan Chiend3c59a22018-03-29 14:08:15 +0800234 t.Helper()
235
Justin Yun0ecf0b22020-02-28 15:07:59 +0900236 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800237
238 // Check library properties.
239 lib, ok := mod.compiler.(*libraryDecorator)
240 if !ok {
241 t.Errorf("%q must have libraryDecorator", name)
242 } else if lib.baseInstaller.subDir != subDir {
243 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
244 lib.baseInstaller.subDir)
245 }
246
247 // Check VNDK properties.
248 if mod.vndkdep == nil {
249 t.Fatalf("%q must have `vndkdep`", name)
250 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700251 if !mod.IsVndk() {
252 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800253 }
254 if mod.isVndkSp() != isVndkSp {
255 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
256 }
257
258 // Check VNDK extension properties.
259 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500260 if mod.IsVndkExt() != isVndkExt {
261 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800262 }
263
264 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
265 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
266 }
267}
268
Bill Peckham945441c2020-08-31 16:07:58 -0700269func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool) {
270 t.Helper()
Jooyung Han39edb6c2019-11-06 16:53:07 +0900271 mod, ok := ctx.ModuleForTests(moduleName, variant).Module().(android.OutputFileProducer)
272 if !ok {
273 t.Errorf("%q must have output\n", moduleName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900274 return
275 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900276 outputFiles, err := mod.OutputFiles("")
277 if err != nil || len(outputFiles) != 1 {
278 t.Errorf("%q must have single output\n", moduleName)
279 return
280 }
281 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900282
Bill Peckham945441c2020-08-31 16:07:58 -0700283 if include {
284 out := singleton.Output(snapshotPath)
285 if out.Input.String() != outputFiles[0].String() {
286 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
287 }
288 } else {
289 out := singleton.MaybeOutput(snapshotPath)
290 if out.Rule != nil {
291 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
292 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900293 }
294}
295
Bill Peckham945441c2020-08-31 16:07:58 -0700296func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
297 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true)
298}
299
300func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
301 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false)
302}
303
Jooyung Han2216fb12019-11-06 16:46:15 +0900304func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
305 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800306 content := android.ContentFromFileRuleForTests(t, params)
307 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900308 assertArrayString(t, actual, expected)
309}
310
Jooyung Han097087b2019-10-22 19:32:18 +0900311func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
312 t.Helper()
313 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900314 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
315}
316
317func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
318 t.Helper()
319 vndkLibraries := ctx.ModuleForTests(module, "")
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +0900320
321 var output string
322 if module != "vndkcorevariant.libraries.txt" {
323 output = insertVndkVersion(module, "VER")
324 } else {
325 output = module
326 }
327
Jooyung Han2216fb12019-11-06 16:46:15 +0900328 checkWriteFileOutput(t, vndkLibraries.Output(output), expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900329}
330
Logan Chienf3511742017-10-31 18:04:35 +0800331func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800332 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800333 cc_library {
334 name: "libvndk",
335 vendor_available: true,
336 vndk: {
337 enabled: true,
338 },
339 nocrt: true,
340 }
341
342 cc_library {
343 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900344 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800345 vndk: {
346 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900347 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800348 },
349 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900350 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800351 }
352
353 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900354 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800355 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900356 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800357 vndk: {
358 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900359 },
360 nocrt: true,
361 target: {
362 vendor: {
363 cflags: ["-DTEST"],
364 },
365 product: {
366 cflags: ["-DTEST"],
367 },
368 },
369 }
370
371 cc_library {
372 name: "libvndk_sp",
373 vendor_available: true,
374 vndk: {
375 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800376 support_system_process: true,
377 },
378 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900379 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800380 }
381
382 cc_library {
383 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900384 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800385 vndk: {
386 enabled: true,
387 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900388 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800389 },
390 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900391 target: {
392 vendor: {
393 suffix: "-x",
394 },
395 },
Logan Chienf3511742017-10-31 18:04:35 +0800396 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900397
398 cc_library {
399 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900400 vendor_available: true,
401 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900402 vndk: {
403 enabled: true,
404 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900405 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900406 },
407 nocrt: true,
408 target: {
409 vendor: {
410 suffix: "-x",
411 },
412 product: {
413 suffix: "-x",
414 },
415 },
416 }
417
Colin Crosse4e44bc2020-12-28 13:50:21 -0800418 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900419 name: "llndk.libraries.txt",
420 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800421 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900422 name: "vndkcore.libraries.txt",
423 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800424 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900425 name: "vndksp.libraries.txt",
426 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800427 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900428 name: "vndkprivate.libraries.txt",
429 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800430 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900431 name: "vndkproduct.libraries.txt",
432 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800433 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900434 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800435 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900436 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800437 `
438
439 config := TestConfig(buildDir, android.Android, nil, bp, nil)
440 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900441 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Colin Cross98be1bb2019-12-13 20:41:13 -0800442 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
443
444 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800445
Jooyung Han261e1582020-10-20 18:54:21 +0900446 // subdir == "" because VNDK libs are not supposed to be installed separately.
447 // They are installed as part of VNDK APEX instead.
448 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
449 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900450 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900451 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
452 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900453 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900454
Justin Yun6977e8a2020-10-29 18:24:11 +0900455 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
456 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900457
Inseob Kim1f086e22019-05-09 13:29:15 +0900458 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900459 snapshotDir := "vndk-snapshot"
460 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
461
462 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
463 "arm64", "armv8-a"))
464 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
465 "arm", "armv7-a-neon"))
466
467 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
468 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
469 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
470 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
471
Colin Crossfb0c16e2019-11-20 17:12:35 -0800472 variant := "android_vendor.VER_arm64_armv8-a_shared"
473 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900474
Inseob Kim7f283f42020-06-01 21:53:49 +0900475 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
476
477 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
478 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900479 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
480 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900481 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
482 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900483
Jooyung Han39edb6c2019-11-06 16:53:07 +0900484 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900485 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
486 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
487 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
488 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900489 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900490
Jooyung Han097087b2019-10-22 19:32:18 +0900491 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
492 "LLNDK: libc.so",
493 "LLNDK: libdl.so",
494 "LLNDK: libft2.so",
495 "LLNDK: libm.so",
496 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900497 "VNDK-SP: libvndk_sp-x.so",
498 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900499 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900500 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900501 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900502 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900503 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900504 "VNDK-private: libvndk-private.so",
505 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900506 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900507 "VNDK-product: libc++.so",
508 "VNDK-product: libvndk_product.so",
509 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900510 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900511 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900512 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
513 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
514 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
Justin Yun8a2600c2020-12-07 12:44:03 +0900515 checkVndkLibrariesOutput(t, ctx, "vndkproduct.libraries.txt", []string{"libc++.so", "libvndk_product.so", "libvndk_sp_product_private-x.so"})
Jooyung Han2216fb12019-11-06 16:46:15 +0900516 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
517}
518
Yo Chiangbba545e2020-06-09 16:15:37 +0800519func TestVndkWithHostSupported(t *testing.T) {
520 ctx := testCc(t, `
521 cc_library {
522 name: "libvndk_host_supported",
523 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900524 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800525 vndk: {
526 enabled: true,
527 },
528 host_supported: true,
529 }
530
531 cc_library {
532 name: "libvndk_host_supported_but_disabled_on_device",
533 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900534 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800535 vndk: {
536 enabled: true,
537 },
538 host_supported: true,
539 enabled: false,
540 target: {
541 host: {
542 enabled: true,
543 }
544 }
545 }
546
Colin Crosse4e44bc2020-12-28 13:50:21 -0800547 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800548 name: "vndkcore.libraries.txt",
549 }
550 `)
551
552 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
553}
554
Jooyung Han2216fb12019-11-06 16:46:15 +0900555func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800556 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800557 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900558 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800559 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800560 }`
561 config := TestConfig(buildDir, android.Android, nil, bp, nil)
562 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
563 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
564 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900565
566 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900567 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900568 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900569}
570
571func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800572 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900573 cc_library {
574 name: "libvndk",
575 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900576 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900577 vndk: {
578 enabled: true,
579 },
580 nocrt: true,
581 }
582
583 cc_library {
584 name: "libvndk_sp",
585 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900586 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900587 vndk: {
588 enabled: true,
589 support_system_process: true,
590 },
591 nocrt: true,
592 }
593
594 cc_library {
595 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900596 vendor_available: true,
597 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900598 vndk: {
599 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900600 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900601 },
602 nocrt: true,
603 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900604
Colin Crosse4e44bc2020-12-28 13:50:21 -0800605 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900606 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800607 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900608 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800609 `
610
611 config := TestConfig(buildDir, android.Android, nil, bp, nil)
612 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
613 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
614 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
615
616 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
617
618 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900619
Jooyung Han2216fb12019-11-06 16:46:15 +0900620 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900621}
622
Chris Parsons79d66a52020-06-05 17:26:16 -0400623func TestDataLibs(t *testing.T) {
624 bp := `
625 cc_test_library {
626 name: "test_lib",
627 srcs: ["test_lib.cpp"],
628 gtest: false,
629 }
630
631 cc_test {
632 name: "main_test",
633 data_libs: ["test_lib"],
634 gtest: false,
635 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400636 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400637
638 config := TestConfig(buildDir, android.Android, nil, bp, nil)
639 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
640 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
641 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
642
643 ctx := testCcWithConfig(t, config)
644 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
645 testBinary := module.(*Module).linker.(*testBinary)
646 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
647 if err != nil {
648 t.Errorf("Expected cc_test to produce output files, error: %s", err)
649 return
650 }
651 if len(outputFiles) != 1 {
652 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
653 return
654 }
655 if len(testBinary.dataPaths()) != 1 {
656 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
657 return
658 }
659
660 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400661 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400662
663 if !strings.HasSuffix(outputPath, "/main_test") {
664 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
665 return
666 }
667 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
668 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
669 return
670 }
671}
672
Chris Parsons216e10a2020-07-09 17:12:52 -0400673func TestDataLibsRelativeInstallPath(t *testing.T) {
674 bp := `
675 cc_test_library {
676 name: "test_lib",
677 srcs: ["test_lib.cpp"],
678 relative_install_path: "foo/bar/baz",
679 gtest: false,
680 }
681
682 cc_test {
683 name: "main_test",
684 data_libs: ["test_lib"],
685 gtest: false,
686 }
687 `
688
689 config := TestConfig(buildDir, android.Android, nil, bp, nil)
690 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
691 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
692 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
693
694 ctx := testCcWithConfig(t, config)
695 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
696 testBinary := module.(*Module).linker.(*testBinary)
697 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
698 if err != nil {
699 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
700 }
701 if len(outputFiles) != 1 {
702 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
703 }
704 if len(testBinary.dataPaths()) != 1 {
705 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
706 }
707
708 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400709
710 if !strings.HasSuffix(outputPath, "/main_test") {
711 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
712 }
713 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
714 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
715 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400716 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400717 }
718}
719
Jooyung Han0302a842019-10-30 18:43:49 +0900720func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900721 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900722 cc_library {
723 name: "libvndk",
724 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900725 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900726 vndk: {
727 enabled: true,
728 },
729 nocrt: true,
730 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900731 cc_library {
732 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900733 vendor_available: true,
734 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900735 vndk: {
736 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900737 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900738 },
739 nocrt: true,
740 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900741 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900742
743 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
744 "LLNDK: libc.so",
745 "LLNDK: libdl.so",
746 "LLNDK: libft2.so",
747 "LLNDK: libm.so",
748 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900749 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900750 "VNDK-core: libvndk.so",
751 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900752 "VNDK-private: libvndk-private.so",
753 "VNDK-product: libc++.so",
754 "VNDK-product: libvndk-private.so",
755 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900756 })
Logan Chienf3511742017-10-31 18:04:35 +0800757}
758
Justin Yun63e9ec72020-10-29 16:49:43 +0900759func TestVndkModuleError(t *testing.T) {
760 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900761 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900762 cc_library {
763 name: "libvndk",
764 vndk: {
765 enabled: true,
766 },
767 nocrt: true,
768 }
769 `)
770
Justin Yunc0d8c492021-01-07 17:45:31 +0900771 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900772 cc_library {
773 name: "libvndk",
774 product_available: true,
775 vndk: {
776 enabled: true,
777 },
778 nocrt: true,
779 }
780 `)
781
Justin Yun6977e8a2020-10-29 18:24:11 +0900782 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
783 cc_library {
784 name: "libvndkprop",
785 vendor_available: true,
786 product_available: true,
787 vndk: {
788 enabled: true,
789 },
790 nocrt: true,
791 target: {
792 vendor: {
793 cflags: ["-DTEST",],
794 },
795 },
796 }
797 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900798}
799
Logan Chiend3c59a22018-03-29 14:08:15 +0800800func TestVndkDepError(t *testing.T) {
801 // Check whether an error is emitted when a VNDK lib depends on a system lib.
802 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
803 cc_library {
804 name: "libvndk",
805 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900806 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800807 vndk: {
808 enabled: true,
809 },
810 shared_libs: ["libfwk"], // Cause error
811 nocrt: true,
812 }
813
814 cc_library {
815 name: "libfwk",
816 nocrt: true,
817 }
818 `)
819
820 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
821 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
822 cc_library {
823 name: "libvndk",
824 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900825 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800826 vndk: {
827 enabled: true,
828 },
829 shared_libs: ["libvendor"], // Cause error
830 nocrt: true,
831 }
832
833 cc_library {
834 name: "libvendor",
835 vendor: true,
836 nocrt: true,
837 }
838 `)
839
840 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
841 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
842 cc_library {
843 name: "libvndk_sp",
844 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900845 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800846 vndk: {
847 enabled: true,
848 support_system_process: true,
849 },
850 shared_libs: ["libfwk"], // Cause error
851 nocrt: true,
852 }
853
854 cc_library {
855 name: "libfwk",
856 nocrt: true,
857 }
858 `)
859
860 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
861 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
862 cc_library {
863 name: "libvndk_sp",
864 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900865 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800866 vndk: {
867 enabled: true,
868 support_system_process: true,
869 },
870 shared_libs: ["libvendor"], // Cause error
871 nocrt: true,
872 }
873
874 cc_library {
875 name: "libvendor",
876 vendor: true,
877 nocrt: true,
878 }
879 `)
880
881 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
882 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
883 cc_library {
884 name: "libvndk_sp",
885 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900886 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800887 vndk: {
888 enabled: true,
889 support_system_process: true,
890 },
891 shared_libs: ["libvndk"], // Cause error
892 nocrt: true,
893 }
894
895 cc_library {
896 name: "libvndk",
897 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900898 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800899 vndk: {
900 enabled: true,
901 },
902 nocrt: true,
903 }
904 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900905
906 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
907 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
908 cc_library {
909 name: "libvndk",
910 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900911 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900912 vndk: {
913 enabled: true,
914 },
915 shared_libs: ["libnonvndk"],
916 nocrt: true,
917 }
918
919 cc_library {
920 name: "libnonvndk",
921 vendor_available: true,
922 nocrt: true,
923 }
924 `)
925
926 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
927 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
928 cc_library {
929 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900930 vendor_available: true,
931 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900932 vndk: {
933 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900934 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900935 },
936 shared_libs: ["libnonvndk"],
937 nocrt: true,
938 }
939
940 cc_library {
941 name: "libnonvndk",
942 vendor_available: true,
943 nocrt: true,
944 }
945 `)
946
947 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
948 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
949 cc_library {
950 name: "libvndksp",
951 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900952 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900953 vndk: {
954 enabled: true,
955 support_system_process: true,
956 },
957 shared_libs: ["libnonvndk"],
958 nocrt: true,
959 }
960
961 cc_library {
962 name: "libnonvndk",
963 vendor_available: true,
964 nocrt: true,
965 }
966 `)
967
968 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
969 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
970 cc_library {
971 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900972 vendor_available: true,
973 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900974 vndk: {
975 enabled: true,
976 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900977 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900978 },
979 shared_libs: ["libnonvndk"],
980 nocrt: true,
981 }
982
983 cc_library {
984 name: "libnonvndk",
985 vendor_available: true,
986 nocrt: true,
987 }
988 `)
989}
990
991func TestDoubleLoadbleDep(t *testing.T) {
992 // okay to link : LLNDK -> double_loadable VNDK
993 testCc(t, `
994 cc_library {
995 name: "libllndk",
996 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -0700997 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900998 }
999
1000 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001001 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001002 symbol_file: "",
1003 }
1004
1005 cc_library {
1006 name: "libdoubleloadable",
1007 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001008 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001009 vndk: {
1010 enabled: true,
1011 },
1012 double_loadable: true,
1013 }
1014 `)
1015 // okay to link : LLNDK -> VNDK-SP
1016 testCc(t, `
1017 cc_library {
1018 name: "libllndk",
1019 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -07001020 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001021 }
1022
1023 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001024 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001025 symbol_file: "",
1026 }
1027
1028 cc_library {
1029 name: "libvndksp",
1030 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001031 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001032 vndk: {
1033 enabled: true,
1034 support_system_process: true,
1035 },
1036 }
1037 `)
1038 // okay to link : double_loadable -> double_loadable
1039 testCc(t, `
1040 cc_library {
1041 name: "libdoubleloadable1",
1042 shared_libs: ["libdoubleloadable2"],
1043 vendor_available: true,
1044 double_loadable: true,
1045 }
1046
1047 cc_library {
1048 name: "libdoubleloadable2",
1049 vendor_available: true,
1050 double_loadable: true,
1051 }
1052 `)
1053 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1054 testCc(t, `
1055 cc_library {
1056 name: "libdoubleloadable",
1057 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001058 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001059 vndk: {
1060 enabled: true,
1061 },
1062 double_loadable: true,
1063 shared_libs: ["libnondoubleloadable"],
1064 }
1065
1066 cc_library {
1067 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001068 vendor_available: true,
1069 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001070 vndk: {
1071 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001072 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001073 },
1074 double_loadable: true,
1075 }
1076 `)
1077 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1078 testCc(t, `
1079 cc_library {
1080 name: "libllndk",
1081 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001082 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001083 }
1084
1085 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001086 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001087 symbol_file: "",
1088 }
1089
1090 cc_library {
1091 name: "libcoreonly",
1092 shared_libs: ["libvendoravailable"],
1093 }
1094
1095 // indirect dependency of LLNDK
1096 cc_library {
1097 name: "libvendoravailable",
1098 vendor_available: true,
1099 double_loadable: true,
1100 }
1101 `)
1102}
1103
Inseob Kim5f58ff72020-09-07 19:53:31 +09001104func TestVendorSnapshotCapture(t *testing.T) {
Inseob Kim8471cda2019-11-15 09:59:12 +09001105 bp := `
1106 cc_library {
1107 name: "libvndk",
1108 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001109 product_available: true,
Inseob Kim8471cda2019-11-15 09:59:12 +09001110 vndk: {
1111 enabled: true,
1112 },
1113 nocrt: true,
1114 }
1115
1116 cc_library {
1117 name: "libvendor",
1118 vendor: true,
1119 nocrt: true,
1120 }
1121
1122 cc_library {
1123 name: "libvendor_available",
1124 vendor_available: true,
1125 nocrt: true,
1126 }
1127
1128 cc_library_headers {
1129 name: "libvendor_headers",
1130 vendor_available: true,
1131 nocrt: true,
1132 }
1133
1134 cc_binary {
1135 name: "vendor_bin",
1136 vendor: true,
1137 nocrt: true,
1138 }
1139
1140 cc_binary {
1141 name: "vendor_available_bin",
1142 vendor_available: true,
1143 nocrt: true,
1144 }
Inseob Kim7f283f42020-06-01 21:53:49 +09001145
1146 toolchain_library {
1147 name: "libb",
1148 vendor_available: true,
1149 src: "libb.a",
1150 }
Inseob Kim1042d292020-06-01 23:23:05 +09001151
1152 cc_object {
1153 name: "obj",
1154 vendor_available: true,
1155 }
Colin Cross127bb8b2020-12-16 16:46:01 -08001156
1157 cc_library {
1158 name: "libllndk",
1159 llndk_stubs: "libllndk.llndk",
1160 }
1161
1162 llndk_library {
1163 name: "libllndk.llndk",
1164 symbol_file: "",
1165 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001166`
1167 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1168 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1169 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1170 ctx := testCcWithConfig(t, config)
1171
1172 // Check Vendor snapshot output.
1173
1174 snapshotDir := "vendor-snapshot"
1175 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +09001176 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1177
1178 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +09001179
1180 for _, arch := range [][]string{
1181 []string{"arm64", "armv8-a"},
1182 []string{"arm", "armv7-a-neon"},
1183 } {
1184 archType := arch[0]
1185 archVariant := arch[1]
1186 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1187
1188 // For shared libraries, only non-VNDK vendor_available modules are captured
1189 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1190 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +09001191 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1192 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
1193 jsonFiles = append(jsonFiles,
1194 filepath.Join(sharedDir, "libvendor.so.json"),
1195 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001196
Colin Cross127bb8b2020-12-16 16:46:01 -08001197 // LLNDK modules are not captured
1198 checkSnapshotExclude(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", sharedDir, sharedVariant)
1199
Inseob Kim8471cda2019-11-15 09:59:12 +09001200 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
Inseob Kimc42f2f22020-07-29 20:32:10 +09001201 // Also cfi variants are captured, except for prebuilts like toolchain_library
Inseob Kim8471cda2019-11-15 09:59:12 +09001202 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001203 staticCfiVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static_cfi", archType, archVariant)
Inseob Kim8471cda2019-11-15 09:59:12 +09001204 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +09001205 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1206 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001207 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001208 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001209 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001210 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001211 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001212 jsonFiles = append(jsonFiles,
1213 filepath.Join(staticDir, "libb.a.json"),
1214 filepath.Join(staticDir, "libvndk.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001215 filepath.Join(staticDir, "libvndk.cfi.a.json"),
Inseob Kim7f283f42020-06-01 21:53:49 +09001216 filepath.Join(staticDir, "libvendor.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001217 filepath.Join(staticDir, "libvendor.cfi.a.json"),
1218 filepath.Join(staticDir, "libvendor_available.a.json"),
1219 filepath.Join(staticDir, "libvendor_available.cfi.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001220
Inseob Kim7f283f42020-06-01 21:53:49 +09001221 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +09001222 if archType == "arm64" {
1223 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1224 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +09001225 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
1226 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
1227 jsonFiles = append(jsonFiles,
1228 filepath.Join(binaryDir, "vendor_bin.json"),
1229 filepath.Join(binaryDir, "vendor_available_bin.json"))
1230 }
1231
1232 // For header libraries, all vendor:true and vendor_available modules are captured.
1233 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1234 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +09001235
1236 // For object modules, all vendor:true and vendor_available modules are captured.
1237 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1238 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1239 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1240 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001241 }
1242
1243 for _, jsonFile := range jsonFiles {
1244 // verify all json files exist
1245 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1246 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001247 }
1248 }
Inseob Kime9aec6a2021-01-05 20:03:22 +09001249
1250 // fake snapshot should have all outputs in the normal snapshot.
1251 fakeSnapshotSingleton := ctx.SingletonForTests("vendor-fake-snapshot")
1252 for _, output := range snapshotSingleton.AllOutputs() {
1253 fakeOutput := strings.Replace(output, "/vendor-snapshot/", "/fake/vendor-snapshot/", 1)
1254 if fakeSnapshotSingleton.MaybeOutput(fakeOutput).Rule == nil {
1255 t.Errorf("%q expected but not found", fakeOutput)
1256 }
1257 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001258}
1259
Inseob Kim5f58ff72020-09-07 19:53:31 +09001260func TestVendorSnapshotUse(t *testing.T) {
1261 frameworkBp := `
1262 cc_library {
1263 name: "libvndk",
1264 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001265 product_available: true,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001266 vndk: {
1267 enabled: true,
1268 },
1269 nocrt: true,
1270 compile_multilib: "64",
1271 }
1272
1273 cc_library {
1274 name: "libvendor",
1275 vendor: true,
1276 nocrt: true,
1277 no_libcrt: true,
1278 stl: "none",
1279 system_shared_libs: [],
1280 compile_multilib: "64",
1281 }
1282
1283 cc_binary {
1284 name: "bin",
1285 vendor: true,
1286 nocrt: true,
1287 no_libcrt: true,
1288 stl: "none",
1289 system_shared_libs: [],
1290 compile_multilib: "64",
1291 }
1292`
1293
1294 vndkBp := `
1295 vndk_prebuilt_shared {
1296 name: "libvndk",
1297 version: "BOARD",
1298 target_arch: "arm64",
1299 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001300 product_available: true,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001301 vndk: {
1302 enabled: true,
1303 },
1304 arch: {
1305 arm64: {
1306 srcs: ["libvndk.so"],
Inseob Kim67be7322020-10-19 10:15:28 +09001307 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001308 },
1309 },
1310 }
1311`
1312
1313 vendorProprietaryBp := `
1314 cc_library {
1315 name: "libvendor_without_snapshot",
1316 vendor: true,
1317 nocrt: true,
1318 no_libcrt: true,
1319 stl: "none",
1320 system_shared_libs: [],
1321 compile_multilib: "64",
1322 }
1323
1324 cc_library_shared {
1325 name: "libclient",
1326 vendor: true,
1327 nocrt: true,
1328 no_libcrt: true,
1329 stl: "none",
1330 system_shared_libs: [],
1331 shared_libs: ["libvndk"],
1332 static_libs: ["libvendor", "libvendor_without_snapshot"],
1333 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001334 srcs: ["client.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001335 }
1336
1337 cc_binary {
1338 name: "bin_without_snapshot",
1339 vendor: true,
1340 nocrt: true,
1341 no_libcrt: true,
1342 stl: "none",
1343 system_shared_libs: [],
1344 static_libs: ["libvndk"],
1345 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001346 srcs: ["bin.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001347 }
1348
1349 vendor_snapshot_static {
1350 name: "libvndk",
1351 version: "BOARD",
1352 target_arch: "arm64",
1353 vendor: true,
1354 arch: {
1355 arm64: {
1356 src: "libvndk.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001357 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001358 },
1359 },
1360 }
1361
1362 vendor_snapshot_shared {
1363 name: "libvendor",
1364 version: "BOARD",
1365 target_arch: "arm64",
1366 vendor: true,
1367 arch: {
1368 arm64: {
1369 src: "libvendor.so",
Inseob Kim67be7322020-10-19 10:15:28 +09001370 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001371 },
1372 },
1373 }
1374
1375 vendor_snapshot_static {
1376 name: "libvendor",
1377 version: "BOARD",
1378 target_arch: "arm64",
1379 vendor: true,
1380 arch: {
1381 arm64: {
1382 src: "libvendor.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001383 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001384 },
1385 },
1386 }
1387
1388 vendor_snapshot_binary {
1389 name: "bin",
1390 version: "BOARD",
1391 target_arch: "arm64",
1392 vendor: true,
1393 arch: {
1394 arm64: {
1395 src: "bin",
1396 },
1397 },
1398 }
1399`
1400 depsBp := GatherRequiredDepsForTest(android.Android)
1401
1402 mockFS := map[string][]byte{
Inseob Kim67be7322020-10-19 10:15:28 +09001403 "deps/Android.bp": []byte(depsBp),
1404 "framework/Android.bp": []byte(frameworkBp),
1405 "vendor/Android.bp": []byte(vendorProprietaryBp),
1406 "vendor/bin": nil,
1407 "vendor/bin.cpp": nil,
1408 "vendor/client.cpp": nil,
1409 "vendor/include/libvndk/a.h": nil,
1410 "vendor/include/libvendor/b.h": nil,
1411 "vendor/libvndk.a": nil,
1412 "vendor/libvendor.a": nil,
1413 "vendor/libvendor.so": nil,
1414 "vndk/Android.bp": []byte(vndkBp),
1415 "vndk/include/libvndk/a.h": nil,
1416 "vndk/libvndk.so": nil,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001417 }
1418
1419 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1420 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1421 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001422 ctx := CreateTestContext(config)
1423 ctx.Register()
Inseob Kim5f58ff72020-09-07 19:53:31 +09001424
1425 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "vendor/Android.bp", "vndk/Android.bp"})
1426 android.FailIfErrored(t, errs)
1427 _, errs = ctx.PrepareBuildActions(config)
1428 android.FailIfErrored(t, errs)
1429
1430 sharedVariant := "android_vendor.BOARD_arm64_armv8-a_shared"
1431 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1432 binaryVariant := "android_vendor.BOARD_arm64_armv8-a"
1433
1434 // libclient uses libvndk.vndk.BOARD.arm64, libvendor.vendor_static.BOARD.arm64, libvendor_without_snapshot
Inseob Kim67be7322020-10-19 10:15:28 +09001435 libclientCcFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("cc").Args["cFlags"]
1436 for _, includeFlags := range []string{
1437 "-Ivndk/include/libvndk", // libvndk
1438 "-Ivendor/include/libvendor", // libvendor
1439 } {
1440 if !strings.Contains(libclientCcFlags, includeFlags) {
1441 t.Errorf("flags for libclient must contain %#v, but was %#v.",
1442 includeFlags, libclientCcFlags)
1443 }
1444 }
Inseob Kim5f58ff72020-09-07 19:53:31 +09001445
Inseob Kim67be7322020-10-19 10:15:28 +09001446 libclientLdFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001447 for _, input := range [][]string{
1448 []string{sharedVariant, "libvndk.vndk.BOARD.arm64"},
1449 []string{staticVariant, "libvendor.vendor_static.BOARD.arm64"},
1450 []string{staticVariant, "libvendor_without_snapshot"},
1451 } {
1452 outputPaths := getOutputPaths(ctx, input[0] /* variant */, []string{input[1]} /* module name */)
Inseob Kim67be7322020-10-19 10:15:28 +09001453 if !strings.Contains(libclientLdFlags, outputPaths[0].String()) {
1454 t.Errorf("libflags for libclient must contain %#v, but was %#v", outputPaths[0], libclientLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001455 }
1456 }
1457
1458 // bin_without_snapshot uses libvndk.vendor_static.BOARD.arm64
Inseob Kim67be7322020-10-19 10:15:28 +09001459 binWithoutSnapshotCcFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("cc").Args["cFlags"]
1460 if !strings.Contains(binWithoutSnapshotCcFlags, "-Ivendor/include/libvndk") {
1461 t.Errorf("flags for bin_without_snapshot must contain %#v, but was %#v.",
1462 "-Ivendor/include/libvndk", binWithoutSnapshotCcFlags)
1463 }
1464
1465 binWithoutSnapshotLdFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001466 libVndkStaticOutputPaths := getOutputPaths(ctx, staticVariant, []string{"libvndk.vendor_static.BOARD.arm64"})
Inseob Kim67be7322020-10-19 10:15:28 +09001467 if !strings.Contains(binWithoutSnapshotLdFlags, libVndkStaticOutputPaths[0].String()) {
Inseob Kim5f58ff72020-09-07 19:53:31 +09001468 t.Errorf("libflags for bin_without_snapshot must contain %#v, but was %#v",
Inseob Kim67be7322020-10-19 10:15:28 +09001469 libVndkStaticOutputPaths[0], binWithoutSnapshotLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001470 }
1471
1472 // libvendor.so is installed by libvendor.vendor_shared.BOARD.arm64
1473 ctx.ModuleForTests("libvendor.vendor_shared.BOARD.arm64", sharedVariant).Output("libvendor.so")
1474
1475 // libvendor_without_snapshot.so is installed by libvendor_without_snapshot
1476 ctx.ModuleForTests("libvendor_without_snapshot", sharedVariant).Output("libvendor_without_snapshot.so")
1477
1478 // bin is installed by bin.vendor_binary.BOARD.arm64
1479 ctx.ModuleForTests("bin.vendor_binary.BOARD.arm64", binaryVariant).Output("bin")
1480
1481 // bin_without_snapshot is installed by bin_without_snapshot
1482 ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Output("bin_without_snapshot")
1483
1484 // libvendor and bin don't have vendor.BOARD variant
1485 libvendorVariants := ctx.ModuleVariantsForTests("libvendor")
1486 if inList(sharedVariant, libvendorVariants) {
1487 t.Errorf("libvendor must not have variant %#v, but it does", sharedVariant)
1488 }
1489
1490 binVariants := ctx.ModuleVariantsForTests("bin")
1491 if inList(binaryVariant, binVariants) {
1492 t.Errorf("bin must not have variant %#v, but it does", sharedVariant)
1493 }
1494}
1495
Inseob Kimc42f2f22020-07-29 20:32:10 +09001496func TestVendorSnapshotSanitizer(t *testing.T) {
1497 bp := `
1498 vendor_snapshot_static {
1499 name: "libsnapshot",
1500 vendor: true,
1501 target_arch: "arm64",
1502 version: "BOARD",
1503 arch: {
1504 arm64: {
1505 src: "libsnapshot.a",
1506 cfi: {
1507 src: "libsnapshot.cfi.a",
1508 }
1509 },
1510 },
1511 }
1512`
1513 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1514 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1515 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1516 ctx := testCcWithConfig(t, config)
1517
1518 // Check non-cfi and cfi variant.
1519 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1520 staticCfiVariant := "android_vendor.BOARD_arm64_armv8-a_static_cfi"
1521
1522 staticModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticVariant).Module().(*Module)
1523 assertString(t, staticModule.outputFile.Path().Base(), "libsnapshot.a")
1524
1525 staticCfiModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticCfiVariant).Module().(*Module)
1526 assertString(t, staticCfiModule.outputFile.Path().Base(), "libsnapshot.cfi.a")
1527}
1528
Bill Peckham945441c2020-08-31 16:07:58 -07001529func assertExcludeFromVendorSnapshotIs(t *testing.T, c *Module, expected bool) {
1530 t.Helper()
1531 if c.ExcludeFromVendorSnapshot() != expected {
1532 t.Errorf("expected %q ExcludeFromVendorSnapshot to be %t", c.String(), expected)
1533 }
1534}
1535
Jose Galmes6f843bc2020-12-11 13:36:29 -08001536func assertExcludeFromRecoverySnapshotIs(t *testing.T, c *Module, expected bool) {
1537 t.Helper()
1538 if c.ExcludeFromRecoverySnapshot() != expected {
1539 t.Errorf("expected %q ExcludeFromRecoverySnapshot to be %t", c.String(), expected)
1540 }
1541}
1542
Bill Peckham945441c2020-08-31 16:07:58 -07001543func TestVendorSnapshotExclude(t *testing.T) {
1544
1545 // This test verifies that the exclude_from_vendor_snapshot property
1546 // makes its way from the Android.bp source file into the module data
1547 // structure. It also verifies that modules are correctly included or
1548 // excluded in the vendor snapshot based on their path (framework or
1549 // vendor) and the exclude_from_vendor_snapshot property.
1550
1551 frameworkBp := `
1552 cc_library_shared {
1553 name: "libinclude",
1554 srcs: ["src/include.cpp"],
1555 vendor_available: true,
1556 }
1557 cc_library_shared {
1558 name: "libexclude",
1559 srcs: ["src/exclude.cpp"],
1560 vendor: true,
1561 exclude_from_vendor_snapshot: true,
1562 }
1563 `
1564
1565 vendorProprietaryBp := `
1566 cc_library_shared {
1567 name: "libvendor",
1568 srcs: ["vendor.cpp"],
1569 vendor: true,
1570 }
1571 `
1572
1573 depsBp := GatherRequiredDepsForTest(android.Android)
1574
1575 mockFS := map[string][]byte{
1576 "deps/Android.bp": []byte(depsBp),
1577 "framework/Android.bp": []byte(frameworkBp),
1578 "framework/include.cpp": nil,
1579 "framework/exclude.cpp": nil,
1580 "device/Android.bp": []byte(vendorProprietaryBp),
1581 "device/vendor.cpp": nil,
1582 }
1583
1584 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1585 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1586 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001587 ctx := CreateTestContext(config)
1588 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001589
1590 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1591 android.FailIfErrored(t, errs)
1592 _, errs = ctx.PrepareBuildActions(config)
1593 android.FailIfErrored(t, errs)
1594
1595 // Test an include and exclude framework module.
1596 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1597 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", vendorVariant).Module().(*Module), false)
1598 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libexclude", vendorVariant).Module().(*Module), true)
1599
1600 // A vendor module is excluded, but by its path, not the
1601 // exclude_from_vendor_snapshot property.
1602 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libvendor", vendorVariant).Module().(*Module), false)
1603
1604 // Verify the content of the vendor snapshot.
1605
1606 snapshotDir := "vendor-snapshot"
1607 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1608 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1609
1610 var includeJsonFiles []string
1611 var excludeJsonFiles []string
1612
1613 for _, arch := range [][]string{
1614 []string{"arm64", "armv8-a"},
1615 []string{"arm", "armv7-a-neon"},
1616 } {
1617 archType := arch[0]
1618 archVariant := arch[1]
1619 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1620
1621 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1622 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1623
1624 // Included modules
1625 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1626 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1627
1628 // Excluded modules
1629 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1630 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1631 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1632 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1633 }
1634
1635 // Verify that each json file for an included module has a rule.
1636 for _, jsonFile := range includeJsonFiles {
1637 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1638 t.Errorf("include json file %q not found", jsonFile)
1639 }
1640 }
1641
1642 // Verify that each json file for an excluded module has no rule.
1643 for _, jsonFile := range excludeJsonFiles {
1644 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1645 t.Errorf("exclude json file %q found", jsonFile)
1646 }
1647 }
1648}
1649
1650func TestVendorSnapshotExcludeInVendorProprietaryPathErrors(t *testing.T) {
1651
1652 // This test verifies that using the exclude_from_vendor_snapshot
1653 // property on a module in a vendor proprietary path generates an
1654 // error. These modules are already excluded, so we prohibit using the
1655 // property in this way, which could add to confusion.
1656
1657 vendorProprietaryBp := `
1658 cc_library_shared {
1659 name: "libvendor",
1660 srcs: ["vendor.cpp"],
1661 vendor: true,
1662 exclude_from_vendor_snapshot: true,
1663 }
1664 `
1665
1666 depsBp := GatherRequiredDepsForTest(android.Android)
1667
1668 mockFS := map[string][]byte{
1669 "deps/Android.bp": []byte(depsBp),
1670 "device/Android.bp": []byte(vendorProprietaryBp),
1671 "device/vendor.cpp": nil,
1672 }
1673
1674 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1675 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1676 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001677 ctx := CreateTestContext(config)
1678 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001679
1680 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "device/Android.bp"})
1681 android.FailIfErrored(t, errs)
1682
1683 _, errs = ctx.PrepareBuildActions(config)
1684 android.CheckErrorsAgainstExpectations(t, errs, []string{
1685 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1686 `module "libvendor\{.+,image:vendor.+,arch:arm_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
Jose Galmesf7294582020-11-13 12:07:36 -08001687 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1688 `module "libvendor\{.+,image:vendor.+,arch:arm_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
Inseob Kime9aec6a2021-01-05 20:03:22 +09001689 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1690 `module "libvendor\{.+,image:vendor.+,arch:arm_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
Bill Peckham945441c2020-08-31 16:07:58 -07001691 })
1692}
1693
1694func TestVendorSnapshotExcludeWithVendorAvailable(t *testing.T) {
1695
1696 // This test verifies that using the exclude_from_vendor_snapshot
1697 // property on a module that is vendor available generates an error. A
1698 // vendor available module must be captured in the vendor snapshot and
1699 // must not built from source when building the vendor image against
1700 // the vendor snapshot.
1701
1702 frameworkBp := `
1703 cc_library_shared {
1704 name: "libinclude",
1705 srcs: ["src/include.cpp"],
1706 vendor_available: true,
1707 exclude_from_vendor_snapshot: true,
1708 }
1709 `
1710
1711 depsBp := GatherRequiredDepsForTest(android.Android)
1712
1713 mockFS := map[string][]byte{
1714 "deps/Android.bp": []byte(depsBp),
1715 "framework/Android.bp": []byte(frameworkBp),
1716 "framework/include.cpp": nil,
1717 }
1718
1719 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1720 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1721 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001722 ctx := CreateTestContext(config)
1723 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001724
1725 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp"})
1726 android.FailIfErrored(t, errs)
1727
1728 _, errs = ctx.PrepareBuildActions(config)
1729 android.CheckErrorsAgainstExpectations(t, errs, []string{
1730 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1731 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1732 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1733 `module "libinclude\{.+,image:vendor.+,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
Inseob Kime9aec6a2021-01-05 20:03:22 +09001734 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1735 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1736 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1737 `module "libinclude\{.+,image:vendor.+,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
Bill Peckham945441c2020-08-31 16:07:58 -07001738 })
1739}
1740
Jose Galmesf7294582020-11-13 12:07:36 -08001741func TestRecoverySnapshotCapture(t *testing.T) {
1742 bp := `
1743 cc_library {
1744 name: "libvndk",
1745 vendor_available: true,
1746 recovery_available: true,
1747 product_available: true,
1748 vndk: {
1749 enabled: true,
1750 },
1751 nocrt: true,
1752 }
1753
1754 cc_library {
1755 name: "librecovery",
1756 recovery: true,
1757 nocrt: true,
1758 }
1759
1760 cc_library {
1761 name: "librecovery_available",
1762 recovery_available: true,
1763 nocrt: true,
1764 }
1765
1766 cc_library_headers {
1767 name: "librecovery_headers",
1768 recovery_available: true,
1769 nocrt: true,
1770 }
1771
1772 cc_binary {
1773 name: "recovery_bin",
1774 recovery: true,
1775 nocrt: true,
1776 }
1777
1778 cc_binary {
1779 name: "recovery_available_bin",
1780 recovery_available: true,
1781 nocrt: true,
1782 }
1783
1784 toolchain_library {
1785 name: "libb",
1786 recovery_available: true,
1787 src: "libb.a",
1788 }
1789
1790 cc_object {
1791 name: "obj",
1792 recovery_available: true,
1793 }
1794`
1795 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jose Galmes6f843bc2020-12-11 13:36:29 -08001796 config.TestProductVariables.RecoverySnapshotVersion = StringPtr("current")
Jose Galmesf7294582020-11-13 12:07:36 -08001797 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1798 ctx := testCcWithConfig(t, config)
1799
1800 // Check Recovery snapshot output.
1801
1802 snapshotDir := "recovery-snapshot"
1803 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1804 snapshotSingleton := ctx.SingletonForTests("recovery-snapshot")
1805
1806 var jsonFiles []string
1807
1808 for _, arch := range [][]string{
1809 []string{"arm64", "armv8-a"},
1810 } {
1811 archType := arch[0]
1812 archVariant := arch[1]
1813 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1814
1815 // For shared libraries, only recovery_available modules are captured.
1816 sharedVariant := fmt.Sprintf("android_recovery_%s_%s_shared", archType, archVariant)
1817 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1818 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", sharedDir, sharedVariant)
1819 checkSnapshot(t, ctx, snapshotSingleton, "librecovery", "librecovery.so", sharedDir, sharedVariant)
1820 checkSnapshot(t, ctx, snapshotSingleton, "librecovery_available", "librecovery_available.so", sharedDir, sharedVariant)
1821 jsonFiles = append(jsonFiles,
1822 filepath.Join(sharedDir, "libvndk.so.json"),
1823 filepath.Join(sharedDir, "librecovery.so.json"),
1824 filepath.Join(sharedDir, "librecovery_available.so.json"))
1825
1826 // For static libraries, all recovery:true and recovery_available modules are captured.
1827 staticVariant := fmt.Sprintf("android_recovery_%s_%s_static", archType, archVariant)
1828 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
1829 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1830 checkSnapshot(t, ctx, snapshotSingleton, "librecovery", "librecovery.a", staticDir, staticVariant)
1831 checkSnapshot(t, ctx, snapshotSingleton, "librecovery_available", "librecovery_available.a", staticDir, staticVariant)
1832 jsonFiles = append(jsonFiles,
1833 filepath.Join(staticDir, "libb.a.json"),
1834 filepath.Join(staticDir, "librecovery.a.json"),
1835 filepath.Join(staticDir, "librecovery_available.a.json"))
1836
1837 // For binary executables, all recovery:true and recovery_available modules are captured.
1838 if archType == "arm64" {
1839 binaryVariant := fmt.Sprintf("android_recovery_%s_%s", archType, archVariant)
1840 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
1841 checkSnapshot(t, ctx, snapshotSingleton, "recovery_bin", "recovery_bin", binaryDir, binaryVariant)
1842 checkSnapshot(t, ctx, snapshotSingleton, "recovery_available_bin", "recovery_available_bin", binaryDir, binaryVariant)
1843 jsonFiles = append(jsonFiles,
1844 filepath.Join(binaryDir, "recovery_bin.json"),
1845 filepath.Join(binaryDir, "recovery_available_bin.json"))
1846 }
1847
1848 // For header libraries, all vendor:true and vendor_available modules are captured.
1849 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1850 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "librecovery_headers.json"))
1851
1852 // For object modules, all vendor:true and vendor_available modules are captured.
1853 objectVariant := fmt.Sprintf("android_recovery_%s_%s", archType, archVariant)
1854 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1855 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1856 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
1857 }
1858
1859 for _, jsonFile := range jsonFiles {
1860 // verify all json files exist
1861 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1862 t.Errorf("%q expected but not found", jsonFile)
1863 }
1864 }
1865}
1866
Jose Galmes6f843bc2020-12-11 13:36:29 -08001867func TestRecoverySnapshotExclude(t *testing.T) {
1868 // This test verifies that the exclude_from_recovery_snapshot property
1869 // makes its way from the Android.bp source file into the module data
1870 // structure. It also verifies that modules are correctly included or
1871 // excluded in the recovery snapshot based on their path (framework or
1872 // vendor) and the exclude_from_recovery_snapshot property.
1873
1874 frameworkBp := `
1875 cc_library_shared {
1876 name: "libinclude",
1877 srcs: ["src/include.cpp"],
1878 recovery_available: true,
1879 }
1880 cc_library_shared {
1881 name: "libexclude",
1882 srcs: ["src/exclude.cpp"],
1883 recovery: true,
1884 exclude_from_recovery_snapshot: true,
1885 }
1886 `
1887
1888 vendorProprietaryBp := `
1889 cc_library_shared {
1890 name: "libvendor",
1891 srcs: ["vendor.cpp"],
1892 recovery: true,
1893 }
1894 `
1895
1896 depsBp := GatherRequiredDepsForTest(android.Android)
1897
1898 mockFS := map[string][]byte{
1899 "deps/Android.bp": []byte(depsBp),
1900 "framework/Android.bp": []byte(frameworkBp),
1901 "framework/include.cpp": nil,
1902 "framework/exclude.cpp": nil,
1903 "device/Android.bp": []byte(vendorProprietaryBp),
1904 "device/vendor.cpp": nil,
1905 }
1906
1907 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1908 config.TestProductVariables.RecoverySnapshotVersion = StringPtr("current")
1909 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1910 ctx := CreateTestContext(config)
1911 ctx.Register()
1912
1913 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1914 android.FailIfErrored(t, errs)
1915 _, errs = ctx.PrepareBuildActions(config)
1916 android.FailIfErrored(t, errs)
1917
1918 // Test an include and exclude framework module.
1919 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1920 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libinclude", recoveryVariant).Module().(*Module), false)
1921 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libexclude", recoveryVariant).Module().(*Module), true)
1922
1923 // A vendor module is excluded, but by its path, not the
1924 // exclude_from_recovery_snapshot property.
1925 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libvendor", recoveryVariant).Module().(*Module), false)
1926
1927 // Verify the content of the recovery snapshot.
1928
1929 snapshotDir := "recovery-snapshot"
1930 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1931 snapshotSingleton := ctx.SingletonForTests("recovery-snapshot")
1932
1933 var includeJsonFiles []string
1934 var excludeJsonFiles []string
1935
1936 for _, arch := range [][]string{
1937 []string{"arm64", "armv8-a"},
1938 } {
1939 archType := arch[0]
1940 archVariant := arch[1]
1941 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1942
1943 sharedVariant := fmt.Sprintf("android_recovery_%s_%s_shared", archType, archVariant)
1944 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1945
1946 // Included modules
1947 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1948 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1949
1950 // Excluded modules
1951 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1952 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1953 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1954 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1955 }
1956
1957 // Verify that each json file for an included module has a rule.
1958 for _, jsonFile := range includeJsonFiles {
1959 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1960 t.Errorf("include json file %q not found", jsonFile)
1961 }
1962 }
1963
1964 // Verify that each json file for an excluded module has no rule.
1965 for _, jsonFile := range excludeJsonFiles {
1966 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1967 t.Errorf("exclude json file %q found", jsonFile)
1968 }
1969 }
1970}
1971
Jooyung Hana70f0672019-01-18 15:20:43 +09001972func TestDoubleLoadableDepError(t *testing.T) {
1973 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1974 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1975 cc_library {
1976 name: "libllndk",
1977 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001978 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001979 }
1980
1981 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001982 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001983 symbol_file: "",
1984 }
1985
1986 cc_library {
1987 name: "libnondoubleloadable",
1988 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001989 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001990 vndk: {
1991 enabled: true,
1992 },
1993 }
1994 `)
1995
1996 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1997 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1998 cc_library {
1999 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07002000 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002001 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07002002 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002003 }
2004
2005 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002006 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002007 symbol_file: "",
2008 }
2009
2010 cc_library {
2011 name: "libnondoubleloadable",
2012 vendor_available: true,
2013 }
2014 `)
2015
2016 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
2017 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2018 cc_library {
2019 name: "libdoubleloadable",
2020 vendor_available: true,
2021 double_loadable: true,
2022 shared_libs: ["libnondoubleloadable"],
2023 }
2024
2025 cc_library {
2026 name: "libnondoubleloadable",
2027 vendor_available: true,
2028 }
2029 `)
2030
2031 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
2032 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2033 cc_library {
2034 name: "libdoubleloadable",
2035 vendor_available: true,
2036 double_loadable: true,
2037 shared_libs: ["libnondoubleloadable"],
2038 }
2039
2040 cc_library {
2041 name: "libnondoubleloadable",
2042 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002043 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002044 vndk: {
2045 enabled: true,
2046 },
2047 }
2048 `)
2049
2050 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
2051 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2052 cc_library {
2053 name: "libdoubleloadable",
2054 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002055 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002056 vndk: {
2057 enabled: true,
2058 },
2059 double_loadable: true,
2060 shared_libs: ["libnondoubleloadable"],
2061 }
2062
2063 cc_library {
2064 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09002065 vendor_available: true,
2066 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002067 vndk: {
2068 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002069 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002070 },
2071 }
2072 `)
2073
2074 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
2075 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2076 cc_library {
2077 name: "libllndk",
2078 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07002079 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002080 }
2081
2082 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002083 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002084 symbol_file: "",
2085 }
2086
2087 cc_library {
2088 name: "libcoreonly",
2089 shared_libs: ["libvendoravailable"],
2090 }
2091
2092 // indirect dependency of LLNDK
2093 cc_library {
2094 name: "libvendoravailable",
2095 vendor_available: true,
2096 }
2097 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08002098}
2099
Jooyung Han479ca172020-10-19 18:51:07 +09002100func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
2101 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
2102 cc_library {
2103 name: "libvndksp",
2104 shared_libs: ["libanothervndksp"],
2105 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002106 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09002107 vndk: {
2108 enabled: true,
2109 support_system_process: true,
2110 }
2111 }
2112
2113 cc_library {
2114 name: "libllndk",
2115 shared_libs: ["libanothervndksp"],
2116 }
2117
2118 llndk_library {
2119 name: "libllndk",
2120 symbol_file: "",
2121 }
2122
2123 cc_library {
2124 name: "libanothervndksp",
2125 vendor_available: true,
2126 }
2127 `)
2128}
2129
Logan Chienf3511742017-10-31 18:04:35 +08002130func TestVndkExt(t *testing.T) {
2131 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09002132 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08002133 cc_library {
2134 name: "libvndk",
2135 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002136 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002137 vndk: {
2138 enabled: true,
2139 },
2140 nocrt: true,
2141 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002142 cc_library {
2143 name: "libvndk2",
2144 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002145 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09002146 vndk: {
2147 enabled: true,
2148 },
2149 target: {
2150 vendor: {
2151 suffix: "-suffix",
2152 },
Justin Yun63e9ec72020-10-29 16:49:43 +09002153 product: {
2154 suffix: "-suffix",
2155 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09002156 },
2157 nocrt: true,
2158 }
Logan Chienf3511742017-10-31 18:04:35 +08002159
2160 cc_library {
2161 name: "libvndk_ext",
2162 vendor: true,
2163 vndk: {
2164 enabled: true,
2165 extends: "libvndk",
2166 },
2167 nocrt: true,
2168 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002169
2170 cc_library {
2171 name: "libvndk2_ext",
2172 vendor: true,
2173 vndk: {
2174 enabled: true,
2175 extends: "libvndk2",
2176 },
2177 nocrt: true,
2178 }
Logan Chienf3511742017-10-31 18:04:35 +08002179
Justin Yun0ecf0b22020-02-28 15:07:59 +09002180 cc_library {
2181 name: "libvndk_ext_product",
2182 product_specific: true,
2183 vndk: {
2184 enabled: true,
2185 extends: "libvndk",
2186 },
2187 nocrt: true,
2188 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002189
Justin Yun0ecf0b22020-02-28 15:07:59 +09002190 cc_library {
2191 name: "libvndk2_ext_product",
2192 product_specific: true,
2193 vndk: {
2194 enabled: true,
2195 extends: "libvndk2",
2196 },
2197 nocrt: true,
2198 }
2199 `
2200 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2201 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2202 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2203 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2204
2205 ctx := testCcWithConfig(t, config)
2206
2207 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
2208 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
2209
2210 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
2211 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
2212
2213 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
2214 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08002215}
2216
Logan Chiend3c59a22018-03-29 14:08:15 +08002217func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08002218 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
2219 ctx := testCcNoVndk(t, `
2220 cc_library {
2221 name: "libvndk",
2222 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002223 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002224 vndk: {
2225 enabled: true,
2226 },
2227 nocrt: true,
2228 }
2229
2230 cc_library {
2231 name: "libvndk_ext",
2232 vendor: true,
2233 vndk: {
2234 enabled: true,
2235 extends: "libvndk",
2236 },
2237 nocrt: true,
2238 }
2239 `)
2240
2241 // Ensures that the core variant of "libvndk_ext" can be found.
2242 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
2243 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
2244 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
2245 }
2246}
2247
Justin Yun0ecf0b22020-02-28 15:07:59 +09002248func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
2249 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09002250 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09002251 cc_library {
2252 name: "libvndk",
2253 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002254 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002255 vndk: {
2256 enabled: true,
2257 },
2258 nocrt: true,
2259 }
2260
2261 cc_library {
2262 name: "libvndk_ext_product",
2263 product_specific: true,
2264 vndk: {
2265 enabled: true,
2266 extends: "libvndk",
2267 },
2268 nocrt: true,
2269 }
2270 `)
2271
2272 // Ensures that the core variant of "libvndk_ext_product" can be found.
2273 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
2274 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
2275 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
2276 }
2277}
2278
Logan Chienf3511742017-10-31 18:04:35 +08002279func TestVndkExtError(t *testing.T) {
2280 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09002281 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08002282 cc_library {
2283 name: "libvndk",
2284 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002285 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002286 vndk: {
2287 enabled: true,
2288 },
2289 nocrt: true,
2290 }
2291
2292 cc_library {
2293 name: "libvndk_ext",
2294 vndk: {
2295 enabled: true,
2296 extends: "libvndk",
2297 },
2298 nocrt: true,
2299 }
2300 `)
2301
2302 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
2303 cc_library {
2304 name: "libvndk",
2305 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002306 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002307 vndk: {
2308 enabled: true,
2309 },
2310 nocrt: true,
2311 }
2312
2313 cc_library {
2314 name: "libvndk_ext",
2315 vendor: true,
2316 vndk: {
2317 enabled: true,
2318 },
2319 nocrt: true,
2320 }
2321 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002322
2323 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
2324 cc_library {
2325 name: "libvndk",
2326 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002327 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002328 vndk: {
2329 enabled: true,
2330 },
2331 nocrt: true,
2332 }
2333
2334 cc_library {
2335 name: "libvndk_ext_product",
2336 product_specific: true,
2337 vndk: {
2338 enabled: true,
2339 },
2340 nocrt: true,
2341 }
2342 `)
2343
2344 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
2345 cc_library {
2346 name: "libvndk",
2347 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002348 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002349 vndk: {
2350 enabled: true,
2351 },
2352 nocrt: true,
2353 }
2354
2355 cc_library {
2356 name: "libvndk_ext_product",
2357 product_specific: true,
2358 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002359 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002360 vndk: {
2361 enabled: true,
2362 extends: "libvndk",
2363 },
2364 nocrt: true,
2365 }
2366 `)
Logan Chienf3511742017-10-31 18:04:35 +08002367}
2368
2369func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
2370 // This test ensures an error is emitted for inconsistent support_system_process.
2371 testCcError(t, "module \".*\" with mismatched support_system_process", `
2372 cc_library {
2373 name: "libvndk",
2374 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002375 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002376 vndk: {
2377 enabled: true,
2378 },
2379 nocrt: true,
2380 }
2381
2382 cc_library {
2383 name: "libvndk_sp_ext",
2384 vendor: true,
2385 vndk: {
2386 enabled: true,
2387 extends: "libvndk",
2388 support_system_process: true,
2389 },
2390 nocrt: true,
2391 }
2392 `)
2393
2394 testCcError(t, "module \".*\" with mismatched support_system_process", `
2395 cc_library {
2396 name: "libvndk_sp",
2397 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002398 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002399 vndk: {
2400 enabled: true,
2401 support_system_process: true,
2402 },
2403 nocrt: true,
2404 }
2405
2406 cc_library {
2407 name: "libvndk_ext",
2408 vendor: true,
2409 vndk: {
2410 enabled: true,
2411 extends: "libvndk_sp",
2412 },
2413 nocrt: true,
2414 }
2415 `)
2416}
2417
2418func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08002419 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09002420 // with `private: true`.
2421 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08002422 cc_library {
2423 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09002424 vendor_available: true,
2425 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002426 vndk: {
2427 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002428 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08002429 },
2430 nocrt: true,
2431 }
2432
2433 cc_library {
2434 name: "libvndk_ext",
2435 vendor: true,
2436 vndk: {
2437 enabled: true,
2438 extends: "libvndk",
2439 },
2440 nocrt: true,
2441 }
2442 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002443
Justin Yunfd9e8042020-12-23 18:23:14 +09002444 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09002445 cc_library {
2446 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09002447 vendor_available: true,
2448 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002449 vndk: {
2450 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002451 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002452 },
2453 nocrt: true,
2454 }
2455
2456 cc_library {
2457 name: "libvndk_ext_product",
2458 product_specific: true,
2459 vndk: {
2460 enabled: true,
2461 extends: "libvndk",
2462 },
2463 nocrt: true,
2464 }
2465 `)
Logan Chienf3511742017-10-31 18:04:35 +08002466}
2467
Logan Chiend3c59a22018-03-29 14:08:15 +08002468func TestVendorModuleUseVndkExt(t *testing.T) {
2469 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002470 testCc(t, `
2471 cc_library {
2472 name: "libvndk",
2473 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002474 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002475 vndk: {
2476 enabled: true,
2477 },
2478 nocrt: true,
2479 }
2480
2481 cc_library {
2482 name: "libvndk_ext",
2483 vendor: true,
2484 vndk: {
2485 enabled: true,
2486 extends: "libvndk",
2487 },
2488 nocrt: true,
2489 }
2490
2491 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08002492 name: "libvndk_sp",
2493 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002494 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002495 vndk: {
2496 enabled: true,
2497 support_system_process: true,
2498 },
2499 nocrt: true,
2500 }
2501
2502 cc_library {
2503 name: "libvndk_sp_ext",
2504 vendor: true,
2505 vndk: {
2506 enabled: true,
2507 extends: "libvndk_sp",
2508 support_system_process: true,
2509 },
2510 nocrt: true,
2511 }
2512
2513 cc_library {
2514 name: "libvendor",
2515 vendor: true,
2516 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
2517 nocrt: true,
2518 }
2519 `)
2520}
2521
Logan Chiend3c59a22018-03-29 14:08:15 +08002522func TestVndkExtUseVendorLib(t *testing.T) {
2523 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08002524 testCc(t, `
2525 cc_library {
2526 name: "libvndk",
2527 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002528 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002529 vndk: {
2530 enabled: true,
2531 },
2532 nocrt: true,
2533 }
2534
2535 cc_library {
2536 name: "libvndk_ext",
2537 vendor: true,
2538 vndk: {
2539 enabled: true,
2540 extends: "libvndk",
2541 },
2542 shared_libs: ["libvendor"],
2543 nocrt: true,
2544 }
2545
2546 cc_library {
2547 name: "libvendor",
2548 vendor: true,
2549 nocrt: true,
2550 }
2551 `)
Logan Chienf3511742017-10-31 18:04:35 +08002552
Logan Chiend3c59a22018-03-29 14:08:15 +08002553 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
2554 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08002555 cc_library {
2556 name: "libvndk_sp",
2557 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002558 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002559 vndk: {
2560 enabled: true,
2561 support_system_process: true,
2562 },
2563 nocrt: true,
2564 }
2565
2566 cc_library {
2567 name: "libvndk_sp_ext",
2568 vendor: true,
2569 vndk: {
2570 enabled: true,
2571 extends: "libvndk_sp",
2572 support_system_process: true,
2573 },
2574 shared_libs: ["libvendor"], // Cause an error
2575 nocrt: true,
2576 }
2577
2578 cc_library {
2579 name: "libvendor",
2580 vendor: true,
2581 nocrt: true,
2582 }
2583 `)
2584}
2585
Justin Yun0ecf0b22020-02-28 15:07:59 +09002586func TestProductVndkExtDependency(t *testing.T) {
2587 bp := `
2588 cc_library {
2589 name: "libvndk",
2590 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002591 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002592 vndk: {
2593 enabled: true,
2594 },
2595 nocrt: true,
2596 }
2597
2598 cc_library {
2599 name: "libvndk_ext_product",
2600 product_specific: true,
2601 vndk: {
2602 enabled: true,
2603 extends: "libvndk",
2604 },
2605 shared_libs: ["libproduct_for_vndklibs"],
2606 nocrt: true,
2607 }
2608
2609 cc_library {
2610 name: "libvndk_sp",
2611 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002612 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002613 vndk: {
2614 enabled: true,
2615 support_system_process: true,
2616 },
2617 nocrt: true,
2618 }
2619
2620 cc_library {
2621 name: "libvndk_sp_ext_product",
2622 product_specific: true,
2623 vndk: {
2624 enabled: true,
2625 extends: "libvndk_sp",
2626 support_system_process: true,
2627 },
2628 shared_libs: ["libproduct_for_vndklibs"],
2629 nocrt: true,
2630 }
2631
2632 cc_library {
2633 name: "libproduct",
2634 product_specific: true,
2635 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
2636 nocrt: true,
2637 }
2638
2639 cc_library {
2640 name: "libproduct_for_vndklibs",
2641 product_specific: true,
2642 nocrt: true,
2643 }
2644 `
2645 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2646 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2647 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2648 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2649
2650 testCcWithConfig(t, config)
2651}
2652
Logan Chiend3c59a22018-03-29 14:08:15 +08002653func TestVndkSpExtUseVndkError(t *testing.T) {
2654 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
2655 // library.
2656 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2657 cc_library {
2658 name: "libvndk",
2659 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002660 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002661 vndk: {
2662 enabled: true,
2663 },
2664 nocrt: true,
2665 }
2666
2667 cc_library {
2668 name: "libvndk_sp",
2669 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002670 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002671 vndk: {
2672 enabled: true,
2673 support_system_process: true,
2674 },
2675 nocrt: true,
2676 }
2677
2678 cc_library {
2679 name: "libvndk_sp_ext",
2680 vendor: true,
2681 vndk: {
2682 enabled: true,
2683 extends: "libvndk_sp",
2684 support_system_process: true,
2685 },
2686 shared_libs: ["libvndk"], // Cause an error
2687 nocrt: true,
2688 }
2689 `)
2690
2691 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
2692 // library.
2693 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2694 cc_library {
2695 name: "libvndk",
2696 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002697 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002698 vndk: {
2699 enabled: true,
2700 },
2701 nocrt: true,
2702 }
2703
2704 cc_library {
2705 name: "libvndk_ext",
2706 vendor: true,
2707 vndk: {
2708 enabled: true,
2709 extends: "libvndk",
2710 },
2711 nocrt: true,
2712 }
2713
2714 cc_library {
2715 name: "libvndk_sp",
2716 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002717 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002718 vndk: {
2719 enabled: true,
2720 support_system_process: true,
2721 },
2722 nocrt: true,
2723 }
2724
2725 cc_library {
2726 name: "libvndk_sp_ext",
2727 vendor: true,
2728 vndk: {
2729 enabled: true,
2730 extends: "libvndk_sp",
2731 support_system_process: true,
2732 },
2733 shared_libs: ["libvndk_ext"], // Cause an error
2734 nocrt: true,
2735 }
2736 `)
2737}
2738
2739func TestVndkUseVndkExtError(t *testing.T) {
2740 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2741 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002742 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2743 cc_library {
2744 name: "libvndk",
2745 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002746 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002747 vndk: {
2748 enabled: true,
2749 },
2750 nocrt: true,
2751 }
2752
2753 cc_library {
2754 name: "libvndk_ext",
2755 vendor: true,
2756 vndk: {
2757 enabled: true,
2758 extends: "libvndk",
2759 },
2760 nocrt: true,
2761 }
2762
2763 cc_library {
2764 name: "libvndk2",
2765 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002766 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002767 vndk: {
2768 enabled: true,
2769 },
2770 shared_libs: ["libvndk_ext"],
2771 nocrt: true,
2772 }
2773 `)
2774
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002775 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002776 cc_library {
2777 name: "libvndk",
2778 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002779 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002780 vndk: {
2781 enabled: true,
2782 },
2783 nocrt: true,
2784 }
2785
2786 cc_library {
2787 name: "libvndk_ext",
2788 vendor: true,
2789 vndk: {
2790 enabled: true,
2791 extends: "libvndk",
2792 },
2793 nocrt: true,
2794 }
2795
2796 cc_library {
2797 name: "libvndk2",
2798 vendor_available: true,
2799 vndk: {
2800 enabled: true,
2801 },
2802 target: {
2803 vendor: {
2804 shared_libs: ["libvndk_ext"],
2805 },
2806 },
2807 nocrt: true,
2808 }
2809 `)
2810
2811 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2812 cc_library {
2813 name: "libvndk_sp",
2814 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002815 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002816 vndk: {
2817 enabled: true,
2818 support_system_process: true,
2819 },
2820 nocrt: true,
2821 }
2822
2823 cc_library {
2824 name: "libvndk_sp_ext",
2825 vendor: true,
2826 vndk: {
2827 enabled: true,
2828 extends: "libvndk_sp",
2829 support_system_process: true,
2830 },
2831 nocrt: true,
2832 }
2833
2834 cc_library {
2835 name: "libvndk_sp_2",
2836 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002837 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002838 vndk: {
2839 enabled: true,
2840 support_system_process: true,
2841 },
2842 shared_libs: ["libvndk_sp_ext"],
2843 nocrt: true,
2844 }
2845 `)
2846
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002847 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002848 cc_library {
2849 name: "libvndk_sp",
2850 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002851 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002852 vndk: {
2853 enabled: true,
2854 },
2855 nocrt: true,
2856 }
2857
2858 cc_library {
2859 name: "libvndk_sp_ext",
2860 vendor: true,
2861 vndk: {
2862 enabled: true,
2863 extends: "libvndk_sp",
2864 },
2865 nocrt: true,
2866 }
2867
2868 cc_library {
2869 name: "libvndk_sp2",
2870 vendor_available: true,
2871 vndk: {
2872 enabled: true,
2873 },
2874 target: {
2875 vendor: {
2876 shared_libs: ["libvndk_sp_ext"],
2877 },
2878 },
2879 nocrt: true,
2880 }
2881 `)
2882}
2883
Justin Yun5f7f7e82019-11-18 19:52:14 +09002884func TestEnforceProductVndkVersion(t *testing.T) {
2885 bp := `
2886 cc_library {
2887 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002888 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002889 }
2890 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002891 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002892 symbol_file: "",
2893 }
2894 cc_library {
2895 name: "libvndk",
2896 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002897 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002898 vndk: {
2899 enabled: true,
2900 },
2901 nocrt: true,
2902 }
2903 cc_library {
2904 name: "libvndk_sp",
2905 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002906 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002907 vndk: {
2908 enabled: true,
2909 support_system_process: true,
2910 },
2911 nocrt: true,
2912 }
2913 cc_library {
2914 name: "libva",
2915 vendor_available: true,
2916 nocrt: true,
2917 }
2918 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002919 name: "libpa",
2920 product_available: true,
2921 nocrt: true,
2922 }
2923 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002924 name: "libboth_available",
2925 vendor_available: true,
2926 product_available: true,
2927 nocrt: true,
2928 target: {
2929 vendor: {
2930 suffix: "-vendor",
2931 },
2932 product: {
2933 suffix: "-product",
2934 },
2935 }
2936 }
2937 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002938 name: "libproduct_va",
2939 product_specific: true,
2940 vendor_available: true,
2941 nocrt: true,
2942 }
2943 cc_library {
2944 name: "libprod",
2945 product_specific: true,
2946 shared_libs: [
2947 "libllndk",
2948 "libvndk",
2949 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002950 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002951 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002952 "libproduct_va",
2953 ],
2954 nocrt: true,
2955 }
2956 cc_library {
2957 name: "libvendor",
2958 vendor: true,
2959 shared_libs: [
2960 "libllndk",
2961 "libvndk",
2962 "libvndk_sp",
2963 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002964 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002965 "libproduct_va",
2966 ],
2967 nocrt: true,
2968 }
2969 `
2970
2971 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2972 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2973 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2974 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2975
2976 ctx := testCcWithConfig(t, config)
2977
Jooyung Han261e1582020-10-20 18:54:21 +09002978 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2979 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002980
2981 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2982 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2983
2984 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2985 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002986}
2987
2988func TestEnforceProductVndkVersionErrors(t *testing.T) {
2989 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2990 cc_library {
2991 name: "libprod",
2992 product_specific: true,
2993 shared_libs: [
2994 "libvendor",
2995 ],
2996 nocrt: true,
2997 }
2998 cc_library {
2999 name: "libvendor",
3000 vendor: true,
3001 nocrt: true,
3002 }
3003 `)
3004 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
3005 cc_library {
3006 name: "libprod",
3007 product_specific: true,
3008 shared_libs: [
3009 "libsystem",
3010 ],
3011 nocrt: true,
3012 }
3013 cc_library {
3014 name: "libsystem",
3015 nocrt: true,
3016 }
3017 `)
Justin Yun6977e8a2020-10-29 18:24:11 +09003018 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
3019 cc_library {
3020 name: "libprod",
3021 product_specific: true,
3022 shared_libs: [
3023 "libva",
3024 ],
3025 nocrt: true,
3026 }
3027 cc_library {
3028 name: "libva",
3029 vendor_available: true,
3030 nocrt: true,
3031 }
3032 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09003033 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09003034 cc_library {
3035 name: "libprod",
3036 product_specific: true,
3037 shared_libs: [
3038 "libvndk_private",
3039 ],
3040 nocrt: true,
3041 }
3042 cc_library {
3043 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09003044 vendor_available: true,
3045 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09003046 vndk: {
3047 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09003048 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09003049 },
3050 nocrt: true,
3051 }
3052 `)
3053 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
3054 cc_library {
3055 name: "libprod",
3056 product_specific: true,
3057 shared_libs: [
3058 "libsystem_ext",
3059 ],
3060 nocrt: true,
3061 }
3062 cc_library {
3063 name: "libsystem_ext",
3064 system_ext_specific: true,
3065 nocrt: true,
3066 }
3067 `)
3068 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
3069 cc_library {
3070 name: "libsystem",
3071 shared_libs: [
3072 "libproduct_va",
3073 ],
3074 nocrt: true,
3075 }
3076 cc_library {
3077 name: "libproduct_va",
3078 product_specific: true,
3079 vendor_available: true,
3080 nocrt: true,
3081 }
3082 `)
3083}
3084
Jooyung Han38002912019-05-16 04:01:54 +09003085func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08003086 bp := `
3087 cc_library {
3088 name: "libvndk",
3089 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003090 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003091 vndk: {
3092 enabled: true,
3093 },
3094 }
3095 cc_library {
3096 name: "libvndksp",
3097 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003098 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003099 vndk: {
3100 enabled: true,
3101 support_system_process: true,
3102 },
3103 }
3104 cc_library {
3105 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09003106 vendor_available: true,
3107 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003108 vndk: {
3109 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09003110 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003111 },
3112 }
3113 cc_library {
3114 name: "libvendor",
3115 vendor: true,
3116 }
3117 cc_library {
3118 name: "libvndkext",
3119 vendor: true,
3120 vndk: {
3121 enabled: true,
3122 extends: "libvndk",
3123 },
3124 }
3125 vndk_prebuilt_shared {
3126 name: "prevndk",
3127 version: "27",
3128 target_arch: "arm",
3129 binder32bit: true,
3130 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003131 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003132 vndk: {
3133 enabled: true,
3134 },
3135 arch: {
3136 arm: {
3137 srcs: ["liba.so"],
3138 },
3139 },
3140 }
3141 cc_library {
3142 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07003143 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003144 }
3145 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003146 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003147 symbol_file: "",
3148 }
3149 cc_library {
3150 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07003151 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003152 }
3153 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003154 name: "libllndkprivate.llndk",
Justin Yunc0d8c492021-01-07 17:45:31 +09003155 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003156 symbol_file: "",
3157 }`
3158
3159 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09003160 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3161 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3162 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08003163 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09003164
Jooyung Han0302a842019-10-30 18:43:49 +09003165 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09003166 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09003167 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09003168 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09003169 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09003170 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09003171 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09003172 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09003173
Colin Crossfb0c16e2019-11-20 17:12:35 -08003174 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09003175
Jooyung Han38002912019-05-16 04:01:54 +09003176 tests := []struct {
3177 variant string
3178 name string
3179 expected string
3180 }{
3181 {vendorVariant, "libvndk", "native:vndk"},
3182 {vendorVariant, "libvndksp", "native:vndk"},
3183 {vendorVariant, "libvndkprivate", "native:vndk_private"},
3184 {vendorVariant, "libvendor", "native:vendor"},
3185 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08003186 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09003187 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09003188 {coreVariant, "libvndk", "native:platform"},
3189 {coreVariant, "libvndkprivate", "native:platform"},
3190 {coreVariant, "libllndk", "native:platform"},
3191 }
3192 for _, test := range tests {
3193 t.Run(test.name, func(t *testing.T) {
3194 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
3195 assertString(t, module.makeLinkType, test.expected)
3196 })
3197 }
3198}
3199
Jeff Gaston294356f2017-09-27 17:05:30 -07003200var staticLinkDepOrderTestCases = []struct {
3201 // This is a string representation of a map[moduleName][]moduleDependency .
3202 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003203 inStatic string
3204
3205 // This is a string representation of a map[moduleName][]moduleDependency .
3206 // It models the dependencies declared in an Android.bp file.
3207 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07003208
3209 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
3210 // The keys of allOrdered specify which modules we would like to check.
3211 // The values of allOrdered specify the expected result (of the transitive closure of all
3212 // dependencies) for each module to test
3213 allOrdered string
3214
3215 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
3216 // The keys of outOrdered specify which modules we would like to check.
3217 // The values of outOrdered specify the expected result (of the ordered linker command line)
3218 // for each module to test.
3219 outOrdered string
3220}{
3221 // Simple tests
3222 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003223 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07003224 outOrdered: "",
3225 },
3226 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003227 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003228 outOrdered: "a:",
3229 },
3230 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003231 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003232 outOrdered: "a:b; b:",
3233 },
3234 // Tests of reordering
3235 {
3236 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003237 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003238 outOrdered: "a:b,c,d; b:d; c:d; d:",
3239 },
3240 {
3241 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003242 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003243 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
3244 },
3245 {
3246 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003247 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07003248 outOrdered: "a:d,b,e,c; d:b; e:c",
3249 },
3250 {
3251 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003252 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07003253 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
3254 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
3255 },
3256 {
3257 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003258 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 -07003259 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
3260 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
3261 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003262 // shared dependencies
3263 {
3264 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
3265 // So, we don't actually have to check that a shared dependency of c will change the order
3266 // of a library that depends statically on b and on c. We only need to check that if c has
3267 // a shared dependency on b, that that shows up in allOrdered.
3268 inShared: "c:b",
3269 allOrdered: "c:b",
3270 outOrdered: "c:",
3271 },
3272 {
3273 // This test doesn't actually include any shared dependencies but it's a reminder of what
3274 // the second phase of the above test would look like
3275 inStatic: "a:b,c; c:b",
3276 allOrdered: "a:c,b; c:b",
3277 outOrdered: "a:c,b; c:b",
3278 },
Jeff Gaston294356f2017-09-27 17:05:30 -07003279 // tiebreakers for when two modules specifying different orderings and there is no dependency
3280 // to dictate an order
3281 {
3282 // 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 -08003283 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07003284 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
3285 },
3286 {
3287 // 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 -08003288 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 -07003289 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
3290 },
3291 // Tests involving duplicate dependencies
3292 {
3293 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003294 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003295 outOrdered: "a:c,b",
3296 },
3297 {
3298 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003299 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003300 outOrdered: "a:d,c,b",
3301 },
3302 // Tests to confirm the nonexistence of infinite loops.
3303 // These cases should never happen, so as long as the test terminates and the
3304 // result is deterministic then that should be fine.
3305 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003306 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003307 outOrdered: "a:a",
3308 },
3309 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003310 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003311 allOrdered: "a:b,c; b:c,a; c:a,b",
3312 outOrdered: "a:b; b:c; c:a",
3313 },
3314 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003315 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003316 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
3317 outOrdered: "a:c,b; b:a,c; c:b,a",
3318 },
3319}
3320
3321// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
3322func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
3323 // convert from "a:b,c; d:e" to "a:b,c;d:e"
3324 strippedText := strings.Replace(text, " ", "", -1)
3325 if len(strippedText) < 1 {
3326 return []android.Path{}, make(map[android.Path][]android.Path, 0)
3327 }
3328 allDeps = make(map[android.Path][]android.Path, 0)
3329
3330 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
3331 moduleTexts := strings.Split(strippedText, ";")
3332
3333 outputForModuleName := func(moduleName string) android.Path {
3334 return android.PathForTesting(moduleName)
3335 }
3336
3337 for _, moduleText := range moduleTexts {
3338 // convert from "a:b,c" to ["a", "b,c"]
3339 components := strings.Split(moduleText, ":")
3340 if len(components) != 2 {
3341 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
3342 }
3343 moduleName := components[0]
3344 moduleOutput := outputForModuleName(moduleName)
3345 modulesInOrder = append(modulesInOrder, moduleOutput)
3346
3347 depString := components[1]
3348 // convert from "b,c" to ["b", "c"]
3349 depNames := strings.Split(depString, ",")
3350 if len(depString) < 1 {
3351 depNames = []string{}
3352 }
3353 var deps []android.Path
3354 for _, depName := range depNames {
3355 deps = append(deps, outputForModuleName(depName))
3356 }
3357 allDeps[moduleOutput] = deps
3358 }
3359 return modulesInOrder, allDeps
3360}
3361
Jeff Gaston294356f2017-09-27 17:05:30 -07003362func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
3363 for _, moduleName := range moduleNames {
3364 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
3365 output := module.outputFile.Path()
3366 paths = append(paths, output)
3367 }
3368 return paths
3369}
3370
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003371func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07003372 ctx := testCc(t, `
3373 cc_library {
3374 name: "a",
3375 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09003376 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003377 }
3378 cc_library {
3379 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003380 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003381 }
3382 cc_library {
3383 name: "c",
3384 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003385 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003386 }
3387 cc_library {
3388 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09003389 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003390 }
3391
3392 `)
3393
Colin Cross7113d202019-11-20 16:39:12 -08003394 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07003395 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003396 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3397 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07003398
3399 if !reflect.DeepEqual(actual, expected) {
3400 t.Errorf("staticDeps orderings were not propagated correctly"+
3401 "\nactual: %v"+
3402 "\nexpected: %v",
3403 actual,
3404 expected,
3405 )
3406 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09003407}
Jeff Gaston294356f2017-09-27 17:05:30 -07003408
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003409func TestStaticLibDepReorderingWithShared(t *testing.T) {
3410 ctx := testCc(t, `
3411 cc_library {
3412 name: "a",
3413 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09003414 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003415 }
3416 cc_library {
3417 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003418 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003419 }
3420 cc_library {
3421 name: "c",
3422 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003423 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003424 }
3425
3426 `)
3427
Colin Cross7113d202019-11-20 16:39:12 -08003428 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003429 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003430 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3431 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003432
3433 if !reflect.DeepEqual(actual, expected) {
3434 t.Errorf("staticDeps orderings did not account for shared libs"+
3435 "\nactual: %v"+
3436 "\nexpected: %v",
3437 actual,
3438 expected,
3439 )
3440 }
3441}
3442
Jooyung Hanb04a4992020-03-13 18:57:35 +09003443func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07003444 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003445 if !reflect.DeepEqual(actual, expected) {
3446 t.Errorf(message+
3447 "\nactual: %v"+
3448 "\nexpected: %v",
3449 actual,
3450 expected,
3451 )
3452 }
3453}
3454
Jooyung Han61b66e92020-03-21 14:21:46 +00003455func TestLlndkLibrary(t *testing.T) {
3456 ctx := testCc(t, `
3457 cc_library {
3458 name: "libllndk",
3459 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07003460 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003461 }
3462 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003463 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003464 }
Colin Cross127bb8b2020-12-16 16:46:01 -08003465
3466 cc_prebuilt_library_shared {
3467 name: "libllndkprebuilt",
3468 stubs: { versions: ["1", "2"] },
3469 llndk_stubs: "libllndkprebuilt.llndk",
3470 }
3471 llndk_library {
3472 name: "libllndkprebuilt.llndk",
3473 }
3474
3475 cc_library {
3476 name: "libllndk_with_external_headers",
3477 stubs: { versions: ["1", "2"] },
3478 llndk_stubs: "libllndk_with_external_headers.llndk",
3479 header_libs: ["libexternal_headers"],
3480 export_header_lib_headers: ["libexternal_headers"],
3481 }
3482 llndk_library {
3483 name: "libllndk_with_external_headers.llndk",
3484 }
3485 cc_library_headers {
3486 name: "libexternal_headers",
3487 export_include_dirs: ["include"],
3488 vendor_available: true,
3489 }
Jooyung Han61b66e92020-03-21 14:21:46 +00003490 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08003491 actual := ctx.ModuleVariantsForTests("libllndk")
3492 for i := 0; i < len(actual); i++ {
3493 if !strings.HasPrefix(actual[i], "android_vendor.VER_") {
3494 actual = append(actual[:i], actual[i+1:]...)
3495 i--
3496 }
3497 }
Jooyung Han61b66e92020-03-21 14:21:46 +00003498 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00003499 "android_vendor.VER_arm64_armv8-a_shared_1",
3500 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003501 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003502 "android_vendor.VER_arm_armv7-a-neon_shared_1",
3503 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003504 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003505 }
3506 checkEquals(t, "variants for llndk stubs", expected, actual)
3507
Colin Cross127bb8b2020-12-16 16:46:01 -08003508 params := ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00003509 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
3510
Colin Cross127bb8b2020-12-16 16:46:01 -08003511 params = ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00003512 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
3513}
3514
Jiyong Parka46a4d52017-12-14 19:54:34 +09003515func TestLlndkHeaders(t *testing.T) {
3516 ctx := testCc(t, `
3517 llndk_headers {
3518 name: "libllndk_headers",
3519 export_include_dirs: ["my_include"],
3520 }
3521 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003522 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09003523 export_llndk_headers: ["libllndk_headers"],
3524 }
3525 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07003526 name: "libllndk",
3527 llndk_stubs: "libllndk.llndk",
3528 }
3529
3530 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09003531 name: "libvendor",
3532 shared_libs: ["libllndk"],
3533 vendor: true,
3534 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003535 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08003536 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09003537 }
3538 `)
3539
3540 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08003541 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09003542 cflags := cc.Args["cFlags"]
3543 if !strings.Contains(cflags, "-Imy_include") {
3544 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
3545 }
3546}
3547
Logan Chien43d34c32017-12-20 01:17:32 +08003548func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
3549 actual := module.Properties.AndroidMkRuntimeLibs
3550 if !reflect.DeepEqual(actual, expected) {
3551 t.Errorf("incorrect runtime_libs for shared libs"+
3552 "\nactual: %v"+
3553 "\nexpected: %v",
3554 actual,
3555 expected,
3556 )
3557 }
3558}
3559
3560const runtimeLibAndroidBp = `
3561 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09003562 name: "liball_available",
3563 vendor_available: true,
3564 product_available: true,
3565 no_libcrt : true,
3566 nocrt : true,
3567 system_shared_libs : [],
3568 }
3569 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08003570 name: "libvendor_available1",
3571 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003572 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07003573 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003574 nocrt : true,
3575 system_shared_libs : [],
3576 }
3577 cc_library {
3578 name: "libvendor_available2",
3579 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003580 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08003581 target: {
3582 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09003583 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08003584 }
3585 },
Yi Konge7fe9912019-06-02 00:53:50 -07003586 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003587 nocrt : true,
3588 system_shared_libs : [],
3589 }
3590 cc_library {
3591 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09003592 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07003593 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003594 nocrt : true,
3595 system_shared_libs : [],
3596 }
3597 cc_library {
3598 name: "libvendor1",
3599 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003600 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003601 nocrt : true,
3602 system_shared_libs : [],
3603 }
3604 cc_library {
3605 name: "libvendor2",
3606 vendor: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003607 runtime_libs: ["liball_available", "libvendor1"],
3608 no_libcrt : true,
3609 nocrt : true,
3610 system_shared_libs : [],
3611 }
3612 cc_library {
3613 name: "libproduct_available1",
3614 product_available: true,
3615 runtime_libs: ["liball_available"],
3616 no_libcrt : true,
3617 nocrt : true,
3618 system_shared_libs : [],
3619 }
3620 cc_library {
3621 name: "libproduct1",
3622 product_specific: true,
3623 no_libcrt : true,
3624 nocrt : true,
3625 system_shared_libs : [],
3626 }
3627 cc_library {
3628 name: "libproduct2",
3629 product_specific: true,
3630 runtime_libs: ["liball_available", "libproduct1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003631 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003632 nocrt : true,
3633 system_shared_libs : [],
3634 }
3635`
3636
3637func TestRuntimeLibs(t *testing.T) {
3638 ctx := testCc(t, runtimeLibAndroidBp)
3639
3640 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08003641 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003642
Justin Yun8a2600c2020-12-07 12:44:03 +09003643 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3644 checkRuntimeLibs(t, []string{"liball_available"}, module)
3645
3646 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3647 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003648
3649 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003650 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003651
3652 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3653 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08003654 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003655
Justin Yun8a2600c2020-12-07 12:44:03 +09003656 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3657 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003658
3659 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003660 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1"}, module)
3661
3662 // runtime_libs for product variants have '.product' suffixes if the modules have both core
3663 // and product variants.
3664 variant = "android_product.VER_arm64_armv8-a_shared"
3665
3666 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3667 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
3668
3669 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
3670 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003671}
3672
3673func TestExcludeRuntimeLibs(t *testing.T) {
3674 ctx := testCc(t, runtimeLibAndroidBp)
3675
Colin Cross7113d202019-11-20 16:39:12 -08003676 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003677 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3678 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003679
Colin Crossfb0c16e2019-11-20 17:12:35 -08003680 variant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003681 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08003682 checkRuntimeLibs(t, nil, module)
3683}
3684
3685func TestRuntimeLibsNoVndk(t *testing.T) {
3686 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3687
3688 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3689
Colin Cross7113d202019-11-20 16:39:12 -08003690 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003691
Justin Yun8a2600c2020-12-07 12:44:03 +09003692 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3693 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003694
3695 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003696 checkRuntimeLibs(t, []string{"liball_available", "libvendor1"}, module)
3697
3698 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
3699 checkRuntimeLibs(t, []string{"liball_available", "libproduct1"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003700}
3701
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003702func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003703 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003704 actual := module.Properties.AndroidMkStaticLibs
3705 if !reflect.DeepEqual(actual, expected) {
3706 t.Errorf("incorrect static_libs"+
3707 "\nactual: %v"+
3708 "\nexpected: %v",
3709 actual,
3710 expected,
3711 )
3712 }
3713}
3714
3715const staticLibAndroidBp = `
3716 cc_library {
3717 name: "lib1",
3718 }
3719 cc_library {
3720 name: "lib2",
3721 static_libs: ["lib1"],
3722 }
3723`
3724
3725func TestStaticLibDepExport(t *testing.T) {
3726 ctx := testCc(t, staticLibAndroidBp)
3727
3728 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003729 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003730 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003731 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003732
3733 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003734 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003735 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3736 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003737 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003738}
3739
Jiyong Parkd08b6972017-09-26 10:50:54 +09003740var compilerFlagsTestCases = []struct {
3741 in string
3742 out bool
3743}{
3744 {
3745 in: "a",
3746 out: false,
3747 },
3748 {
3749 in: "-a",
3750 out: true,
3751 },
3752 {
3753 in: "-Ipath/to/something",
3754 out: false,
3755 },
3756 {
3757 in: "-isystempath/to/something",
3758 out: false,
3759 },
3760 {
3761 in: "--coverage",
3762 out: false,
3763 },
3764 {
3765 in: "-include a/b",
3766 out: true,
3767 },
3768 {
3769 in: "-include a/b c/d",
3770 out: false,
3771 },
3772 {
3773 in: "-DMACRO",
3774 out: true,
3775 },
3776 {
3777 in: "-DMAC RO",
3778 out: false,
3779 },
3780 {
3781 in: "-a -b",
3782 out: false,
3783 },
3784 {
3785 in: "-DMACRO=definition",
3786 out: true,
3787 },
3788 {
3789 in: "-DMACRO=defi nition",
3790 out: true, // TODO(jiyong): this should be false
3791 },
3792 {
3793 in: "-DMACRO(x)=x + 1",
3794 out: true,
3795 },
3796 {
3797 in: "-DMACRO=\"defi nition\"",
3798 out: true,
3799 },
3800}
3801
3802type mockContext struct {
3803 BaseModuleContext
3804 result bool
3805}
3806
3807func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3808 // CheckBadCompilerFlags calls this function when the flag should be rejected
3809 ctx.result = false
3810}
3811
3812func TestCompilerFlags(t *testing.T) {
3813 for _, testCase := range compilerFlagsTestCases {
3814 ctx := &mockContext{result: true}
3815 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3816 if ctx.result != testCase.out {
3817 t.Errorf("incorrect output:")
3818 t.Errorf(" input: %#v", testCase.in)
3819 t.Errorf(" expected: %#v", testCase.out)
3820 t.Errorf(" got: %#v", ctx.result)
3821 }
3822 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003823}
Jiyong Park374510b2018-03-19 18:23:01 +09003824
3825func TestVendorPublicLibraries(t *testing.T) {
3826 ctx := testCc(t, `
3827 cc_library_headers {
3828 name: "libvendorpublic_headers",
3829 export_include_dirs: ["my_include"],
3830 }
3831 vendor_public_library {
3832 name: "libvendorpublic",
3833 symbol_file: "",
3834 export_public_headers: ["libvendorpublic_headers"],
3835 }
3836 cc_library {
3837 name: "libvendorpublic",
3838 srcs: ["foo.c"],
3839 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003840 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003841 nocrt: true,
3842 }
3843
3844 cc_library {
3845 name: "libsystem",
3846 shared_libs: ["libvendorpublic"],
3847 vendor: false,
3848 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003849 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003850 nocrt: true,
3851 }
3852 cc_library {
3853 name: "libvendor",
3854 shared_libs: ["libvendorpublic"],
3855 vendor: true,
3856 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003857 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003858 nocrt: true,
3859 }
3860 `)
3861
Colin Cross7113d202019-11-20 16:39:12 -08003862 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003863 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003864
3865 // test if header search paths are correctly added
3866 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003867 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003868 cflags := cc.Args["cFlags"]
3869 if !strings.Contains(cflags, "-Imy_include") {
3870 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3871 }
3872
3873 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003874 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003875 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003876 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003877 if !strings.Contains(libflags, stubPaths[0].String()) {
3878 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3879 }
3880
3881 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003882 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003883 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003884 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003885 if !strings.Contains(libflags, stubPaths[0].String()) {
3886 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3887 }
3888
3889}
Jiyong Park37b25202018-07-11 10:49:27 +09003890
3891func TestRecovery(t *testing.T) {
3892 ctx := testCc(t, `
3893 cc_library_shared {
3894 name: "librecovery",
3895 recovery: true,
3896 }
3897 cc_library_shared {
3898 name: "librecovery32",
3899 recovery: true,
3900 compile_multilib:"32",
3901 }
Jiyong Park5baac542018-08-28 09:55:37 +09003902 cc_library_shared {
3903 name: "libHalInRecovery",
3904 recovery_available: true,
3905 vendor: true,
3906 }
Jiyong Park37b25202018-07-11 10:49:27 +09003907 `)
3908
3909 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003910 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003911 if len(variants) != 1 || !android.InList(arm64, variants) {
3912 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3913 }
3914
3915 variants = ctx.ModuleVariantsForTests("librecovery32")
3916 if android.InList(arm64, variants) {
3917 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3918 }
Jiyong Park5baac542018-08-28 09:55:37 +09003919
3920 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3921 if !recoveryModule.Platform() {
3922 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3923 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003924}
Jiyong Park5baac542018-08-28 09:55:37 +09003925
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003926func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3927 bp := `
3928 cc_prebuilt_test_library_shared {
3929 name: "test_lib",
3930 relative_install_path: "foo/bar/baz",
3931 srcs: ["srcpath/dontusethispath/baz.so"],
3932 }
3933
3934 cc_test {
3935 name: "main_test",
3936 data_libs: ["test_lib"],
3937 gtest: false,
3938 }
3939 `
3940
3941 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3942 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3943 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3944 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3945
3946 ctx := testCcWithConfig(t, config)
3947 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3948 testBinary := module.(*Module).linker.(*testBinary)
3949 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3950 if err != nil {
3951 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3952 }
3953 if len(outputFiles) != 1 {
3954 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3955 }
3956 if len(testBinary.dataPaths()) != 1 {
3957 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3958 }
3959
3960 outputPath := outputFiles[0].String()
3961
3962 if !strings.HasSuffix(outputPath, "/main_test") {
3963 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3964 }
3965 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
3966 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3967 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3968 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3969 }
3970}
3971
Jiyong Park7ed9de32018-10-15 22:25:07 +09003972func TestVersionedStubs(t *testing.T) {
3973 ctx := testCc(t, `
3974 cc_library_shared {
3975 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003976 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003977 stubs: {
3978 symbol_file: "foo.map.txt",
3979 versions: ["1", "2", "3"],
3980 },
3981 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003982
Jiyong Park7ed9de32018-10-15 22:25:07 +09003983 cc_library_shared {
3984 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003985 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003986 shared_libs: ["libFoo#1"],
3987 }`)
3988
3989 variants := ctx.ModuleVariantsForTests("libFoo")
3990 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003991 "android_arm64_armv8-a_shared",
3992 "android_arm64_armv8-a_shared_1",
3993 "android_arm64_armv8-a_shared_2",
3994 "android_arm64_armv8-a_shared_3",
3995 "android_arm_armv7-a-neon_shared",
3996 "android_arm_armv7-a-neon_shared_1",
3997 "android_arm_armv7-a-neon_shared_2",
3998 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003999 }
4000 variantsMismatch := false
4001 if len(variants) != len(expectedVariants) {
4002 variantsMismatch = true
4003 } else {
4004 for _, v := range expectedVariants {
4005 if !inList(v, variants) {
4006 variantsMismatch = false
4007 }
4008 }
4009 }
4010 if variantsMismatch {
4011 t.Errorf("variants of libFoo expected:\n")
4012 for _, v := range expectedVariants {
4013 t.Errorf("%q\n", v)
4014 }
4015 t.Errorf(", but got:\n")
4016 for _, v := range variants {
4017 t.Errorf("%q\n", v)
4018 }
4019 }
4020
Colin Cross7113d202019-11-20 16:39:12 -08004021 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09004022 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08004023 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09004024 if !strings.Contains(libFlags, libFoo1StubPath) {
4025 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
4026 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09004027
Colin Cross7113d202019-11-20 16:39:12 -08004028 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09004029 cFlags := libBarCompileRule.Args["cFlags"]
4030 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
4031 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
4032 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
4033 }
Jiyong Park37b25202018-07-11 10:49:27 +09004034}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004035
Jooyung Hanb04a4992020-03-13 18:57:35 +09004036func TestVersioningMacro(t *testing.T) {
4037 for _, tc := range []struct{ moduleName, expected string }{
4038 {"libc", "__LIBC_API__"},
4039 {"libfoo", "__LIBFOO_API__"},
4040 {"libfoo@1", "__LIBFOO_1_API__"},
4041 {"libfoo-v1", "__LIBFOO_V1_API__"},
4042 {"libfoo.v1", "__LIBFOO_V1_API__"},
4043 } {
4044 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
4045 }
4046}
4047
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004048func TestStaticExecutable(t *testing.T) {
4049 ctx := testCc(t, `
4050 cc_binary {
4051 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01004052 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004053 static_executable: true,
4054 }`)
4055
Colin Cross7113d202019-11-20 16:39:12 -08004056 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004057 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
4058 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07004059 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004060 for _, lib := range systemStaticLibs {
4061 if !strings.Contains(libFlags, lib) {
4062 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
4063 }
4064 }
4065 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
4066 for _, lib := range systemSharedLibs {
4067 if strings.Contains(libFlags, lib) {
4068 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
4069 }
4070 }
4071}
Jiyong Parke4bb9862019-02-01 00:31:10 +09004072
4073func TestStaticDepsOrderWithStubs(t *testing.T) {
4074 ctx := testCc(t, `
4075 cc_binary {
4076 name: "mybin",
4077 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07004078 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09004079 static_executable: true,
4080 stl: "none",
4081 }
4082
4083 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08004084 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09004085 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08004086 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09004087 stl: "none",
4088 }
4089
4090 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08004091 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09004092 srcs: ["foo.c"],
4093 stl: "none",
4094 stubs: {
4095 versions: ["1"],
4096 },
4097 }`)
4098
Colin Cross0de8a1e2020-09-18 14:15:30 -07004099 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
4100 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08004101 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09004102
4103 if !reflect.DeepEqual(actual, expected) {
4104 t.Errorf("staticDeps orderings were not propagated correctly"+
4105 "\nactual: %v"+
4106 "\nexpected: %v",
4107 actual,
4108 expected,
4109 )
4110 }
4111}
Jooyung Han38002912019-05-16 04:01:54 +09004112
Jooyung Hand48f3c32019-08-23 11:18:57 +09004113func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
4114 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
4115 cc_library {
4116 name: "libA",
4117 srcs: ["foo.c"],
4118 shared_libs: ["libB"],
4119 stl: "none",
4120 }
4121
4122 cc_library {
4123 name: "libB",
4124 srcs: ["foo.c"],
4125 enabled: false,
4126 stl: "none",
4127 }
4128 `)
4129}
4130
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004131// Simple smoke test for the cc_fuzz target that ensures the rule compiles
4132// correctly.
4133func TestFuzzTarget(t *testing.T) {
4134 ctx := testCc(t, `
4135 cc_fuzz {
4136 name: "fuzz_smoke_test",
4137 srcs: ["foo.c"],
4138 }`)
4139
Paul Duffin075c4172019-12-19 19:06:13 +00004140 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004141 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
4142}
4143
Jiyong Park29074592019-07-07 16:27:47 +09004144func TestAidl(t *testing.T) {
4145}
4146
Jooyung Han38002912019-05-16 04:01:54 +09004147func assertString(t *testing.T, got, expected string) {
4148 t.Helper()
4149 if got != expected {
4150 t.Errorf("expected %q got %q", expected, got)
4151 }
4152}
4153
4154func assertArrayString(t *testing.T, got, expected []string) {
4155 t.Helper()
4156 if len(got) != len(expected) {
4157 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
4158 return
4159 }
4160 for i := range got {
4161 if got[i] != expected[i] {
4162 t.Errorf("expected %d-th %q (%q) got %q (%q)",
4163 i, expected[i], expected, got[i], got)
4164 return
4165 }
4166 }
4167}
Colin Crosse1bb5d02019-09-24 14:55:04 -07004168
Jooyung Han0302a842019-10-30 18:43:49 +09004169func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
4170 t.Helper()
4171 assertArrayString(t, android.SortedStringKeys(m), expected)
4172}
4173
Colin Crosse1bb5d02019-09-24 14:55:04 -07004174func TestDefaults(t *testing.T) {
4175 ctx := testCc(t, `
4176 cc_defaults {
4177 name: "defaults",
4178 srcs: ["foo.c"],
4179 static: {
4180 srcs: ["bar.c"],
4181 },
4182 shared: {
4183 srcs: ["baz.c"],
4184 },
4185 }
4186
4187 cc_library_static {
4188 name: "libstatic",
4189 defaults: ["defaults"],
4190 }
4191
4192 cc_library_shared {
4193 name: "libshared",
4194 defaults: ["defaults"],
4195 }
4196
4197 cc_library {
4198 name: "libboth",
4199 defaults: ["defaults"],
4200 }
4201
4202 cc_binary {
4203 name: "binary",
4204 defaults: ["defaults"],
4205 }`)
4206
4207 pathsToBase := func(paths android.Paths) []string {
4208 var ret []string
4209 for _, p := range paths {
4210 ret = append(ret, p.Base())
4211 }
4212 return ret
4213 }
4214
Colin Cross7113d202019-11-20 16:39:12 -08004215 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004216 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4217 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
4218 }
Colin Cross7113d202019-11-20 16:39:12 -08004219 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004220 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4221 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
4222 }
Colin Cross7113d202019-11-20 16:39:12 -08004223 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004224 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
4225 t.Errorf("binary ld rule wanted %q, got %q", w, g)
4226 }
4227
Colin Cross7113d202019-11-20 16:39:12 -08004228 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004229 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4230 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
4231 }
Colin Cross7113d202019-11-20 16:39:12 -08004232 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004233 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4234 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
4235 }
4236}
Colin Crosseabaedd2020-02-06 17:01:55 -08004237
4238func TestProductVariableDefaults(t *testing.T) {
4239 bp := `
4240 cc_defaults {
4241 name: "libfoo_defaults",
4242 srcs: ["foo.c"],
4243 cppflags: ["-DFOO"],
4244 product_variables: {
4245 debuggable: {
4246 cppflags: ["-DBAR"],
4247 },
4248 },
4249 }
4250
4251 cc_library {
4252 name: "libfoo",
4253 defaults: ["libfoo_defaults"],
4254 }
4255 `
4256
4257 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4258 config.TestProductVariables.Debuggable = BoolPtr(true)
4259
Colin Crossae8600b2020-10-29 17:09:13 -07004260 ctx := CreateTestContext(config)
Colin Crosseabaedd2020-02-06 17:01:55 -08004261 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
4262 ctx.BottomUp("variable", android.VariableMutator).Parallel()
4263 })
Colin Crossae8600b2020-10-29 17:09:13 -07004264 ctx.Register()
Colin Crosseabaedd2020-02-06 17:01:55 -08004265
4266 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
4267 android.FailIfErrored(t, errs)
4268 _, errs = ctx.PrepareBuildActions(config)
4269 android.FailIfErrored(t, errs)
4270
4271 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
4272 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
4273 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
4274 }
4275}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004276
4277func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4278 t.Parallel()
4279 bp := `
4280 cc_library_static {
4281 name: "libfoo",
4282 srcs: ["foo.c"],
4283 whole_static_libs: ["libbar"],
4284 }
4285
4286 cc_library_static {
4287 name: "libbar",
4288 whole_static_libs: ["libmissing"],
4289 }
4290 `
4291
4292 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4293 config.TestProductVariables.Allow_missing_dependencies = BoolPtr(true)
4294
Colin Crossae8600b2020-10-29 17:09:13 -07004295 ctx := CreateTestContext(config)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004296 ctx.SetAllowMissingDependencies(true)
Colin Crossae8600b2020-10-29 17:09:13 -07004297 ctx.Register()
Colin Crosse4f6eba2020-09-22 18:11:25 -07004298
4299 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
4300 android.FailIfErrored(t, errs)
4301 _, errs = ctx.PrepareBuildActions(config)
4302 android.FailIfErrored(t, errs)
4303
4304 libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
4305 if g, w := libbar.Rule, android.ErrorRule; g != w {
4306 t.Fatalf("Expected libbar rule to be %q, got %q", w, g)
4307 }
4308
4309 if g, w := libbar.Args["error"], "missing dependencies: libmissing"; !strings.Contains(g, w) {
4310 t.Errorf("Expected libbar error to contain %q, was %q", w, g)
4311 }
4312
4313 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
4314 if g, w := libfoo.Inputs.Strings(), libbar.Output.String(); !android.InList(w, g) {
4315 t.Errorf("Expected libfoo.a to depend on %q, got %q", w, g)
4316 }
4317
4318}
Colin Crosse9fe2942020-11-10 18:12:15 -08004319
4320func TestInstallSharedLibs(t *testing.T) {
4321 bp := `
4322 cc_binary {
4323 name: "bin",
4324 host_supported: true,
4325 shared_libs: ["libshared"],
4326 runtime_libs: ["libruntime"],
4327 srcs: [":gen"],
4328 }
4329
4330 cc_library_shared {
4331 name: "libshared",
4332 host_supported: true,
4333 shared_libs: ["libtransitive"],
4334 }
4335
4336 cc_library_shared {
4337 name: "libtransitive",
4338 host_supported: true,
4339 }
4340
4341 cc_library_shared {
4342 name: "libruntime",
4343 host_supported: true,
4344 }
4345
4346 cc_binary_host {
4347 name: "tool",
4348 srcs: ["foo.cpp"],
4349 }
4350
4351 genrule {
4352 name: "gen",
4353 tools: ["tool"],
4354 out: ["gen.cpp"],
4355 cmd: "$(location tool) $(out)",
4356 }
4357 `
4358
4359 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4360 ctx := testCcWithConfig(t, config)
4361
4362 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4363 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4364 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4365 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4366 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4367
4368 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4369 t.Errorf("expected host bin dependency %q, got %q", w, g)
4370 }
4371
4372 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4373 t.Errorf("expected host bin dependency %q, got %q", w, g)
4374 }
4375
4376 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4377 t.Errorf("expected host bin dependency %q, got %q", w, g)
4378 }
4379
4380 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4381 t.Errorf("expected host bin dependency %q, got %q", w, g)
4382 }
4383
4384 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4385 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4386 }
4387
4388 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4389 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4390 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4391 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4392
4393 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4394 t.Errorf("expected device bin dependency %q, got %q", w, g)
4395 }
4396
4397 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4398 t.Errorf("expected device bin dependency %q, got %q", w, g)
4399 }
4400
4401 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4402 t.Errorf("expected device bin dependency %q, got %q", w, g)
4403 }
4404
4405 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4406 t.Errorf("expected device bin dependency %q, got %q", w, g)
4407 }
4408
4409 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4410 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4411 }
4412
4413}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004414
4415func TestStubsLibReexportsHeaders(t *testing.T) {
4416 ctx := testCc(t, `
4417 cc_library_shared {
4418 name: "libclient",
4419 srcs: ["foo.c"],
4420 shared_libs: ["libfoo#1"],
4421 }
4422
4423 cc_library_shared {
4424 name: "libfoo",
4425 srcs: ["foo.c"],
4426 shared_libs: ["libbar"],
4427 export_shared_lib_headers: ["libbar"],
4428 stubs: {
4429 symbol_file: "foo.map.txt",
4430 versions: ["1", "2", "3"],
4431 },
4432 }
4433
4434 cc_library_shared {
4435 name: "libbar",
4436 export_include_dirs: ["include/libbar"],
4437 srcs: ["foo.c"],
4438 }`)
4439
4440 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4441
4442 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4443 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4444 }
4445}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004446
4447func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
4448 ctx := testCc(t, `
4449 cc_library {
4450 name: "libfoo",
4451 srcs: ["a/Foo.aidl"],
4452 aidl: { flags: ["-Werror"], },
4453 }
4454 `)
4455
4456 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4457 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4458 aidlCommand := manifest.Commands[0].GetCommand()
4459 expectedAidlFlag := "-Werror"
4460 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4461 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4462 }
4463}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004464
4465func checkHasImplicitDep(t *testing.T, m android.TestingModule, name string) {
4466 implicits := m.Rule("ld").Implicits
4467 for _, lib := range implicits {
4468 if strings.Contains(lib.Rel(), name) {
4469 return
4470 }
4471 }
4472
4473 t.Errorf("%q is not found in implicit deps of module %q", name, m.Module().(*Module).Name())
4474}
4475
4476func checkDoesNotHaveImplicitDep(t *testing.T, m android.TestingModule, name string) {
4477 implicits := m.Rule("ld").Implicits
4478 for _, lib := range implicits {
4479 if strings.Contains(lib.Rel(), name) {
4480 t.Errorf("%q is found in implicit deps of module %q", name, m.Module().(*Module).Name())
4481 }
4482 }
4483}
4484
4485func TestSanitizeMemtagHeap(t *testing.T) {
4486 ctx := testCc(t, `
4487 cc_library_static {
4488 name: "libstatic",
4489 sanitize: { memtag_heap: true },
4490 }
4491
4492 cc_library_shared {
4493 name: "libshared",
4494 sanitize: { memtag_heap: true },
4495 }
4496
4497 cc_library {
4498 name: "libboth",
4499 sanitize: { memtag_heap: true },
4500 }
4501
4502 cc_binary {
4503 name: "binary",
4504 shared_libs: [ "libshared" ],
4505 static_libs: [ "libstatic" ],
4506 }
4507
4508 cc_binary {
4509 name: "binary_true",
4510 sanitize: { memtag_heap: true },
4511 }
4512
4513 cc_binary {
4514 name: "binary_true_sync",
4515 sanitize: { memtag_heap: true, diag: { memtag_heap: true }, },
4516 }
4517
4518 cc_binary {
4519 name: "binary_false",
4520 sanitize: { memtag_heap: false },
4521 }
4522
4523 cc_test {
4524 name: "test",
4525 gtest: false,
4526 }
4527
4528 cc_test {
4529 name: "test_true",
4530 gtest: false,
4531 sanitize: { memtag_heap: true },
4532 }
4533
4534 cc_test {
4535 name: "test_false",
4536 gtest: false,
4537 sanitize: { memtag_heap: false },
4538 }
4539
4540 cc_test {
4541 name: "test_true_async",
4542 gtest: false,
4543 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
4544 }
4545
4546 `)
4547
4548 variant := "android_arm64_armv8-a"
4549 note_async := "note_memtag_heap_async"
4550 note_sync := "note_memtag_heap_sync"
4551 note_any := "note_memtag_"
4552
4553 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared"), note_any)
4554 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared"), note_any)
4555
4556 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("binary", variant), note_any)
4557 checkHasImplicitDep(t, ctx.ModuleForTests("binary_true", variant), note_async)
4558 checkHasImplicitDep(t, ctx.ModuleForTests("binary_true_sync", variant), note_sync)
4559 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("binary_false", variant), note_any)
4560
4561 checkHasImplicitDep(t, ctx.ModuleForTests("test", variant), note_sync)
4562 checkHasImplicitDep(t, ctx.ModuleForTests("test_true", variant), note_async)
4563 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("test_false", variant), note_any)
4564 checkHasImplicitDep(t, ctx.ModuleForTests("test_true_async", variant), note_async)
4565}