blob: 59e1516c997d1468e2e4bc8c953febe94985eab8 [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 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800741
742 cc_library {
743 name: "libllndk",
744 llndk_stubs: "libllndk.llndk",
745 }
746
747 llndk_library {
748 name: "libllndk.llndk",
749 symbol_file: "",
750 export_llndk_headers: ["libllndk_headers"],
751 }
752
753 llndk_headers {
754 name: "libllndk_headers",
755 export_include_dirs: ["include"],
756 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900757 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900758
759 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
760 "LLNDK: libc.so",
761 "LLNDK: libdl.so",
762 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800763 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900764 "LLNDK: libm.so",
765 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900766 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900767 "VNDK-core: libvndk.so",
768 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900769 "VNDK-private: libvndk-private.so",
770 "VNDK-product: libc++.so",
771 "VNDK-product: libvndk-private.so",
772 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900773 })
Logan Chienf3511742017-10-31 18:04:35 +0800774}
775
Justin Yun63e9ec72020-10-29 16:49:43 +0900776func TestVndkModuleError(t *testing.T) {
777 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900778 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900779 cc_library {
780 name: "libvndk",
781 vndk: {
782 enabled: true,
783 },
784 nocrt: true,
785 }
786 `)
787
Justin Yunc0d8c492021-01-07 17:45:31 +0900788 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900789 cc_library {
790 name: "libvndk",
791 product_available: true,
792 vndk: {
793 enabled: true,
794 },
795 nocrt: true,
796 }
797 `)
798
Justin Yun6977e8a2020-10-29 18:24:11 +0900799 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
800 cc_library {
801 name: "libvndkprop",
802 vendor_available: true,
803 product_available: true,
804 vndk: {
805 enabled: true,
806 },
807 nocrt: true,
808 target: {
809 vendor: {
810 cflags: ["-DTEST",],
811 },
812 },
813 }
814 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900815}
816
Logan Chiend3c59a22018-03-29 14:08:15 +0800817func TestVndkDepError(t *testing.T) {
818 // Check whether an error is emitted when a VNDK lib depends on a system lib.
819 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
820 cc_library {
821 name: "libvndk",
822 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900823 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800824 vndk: {
825 enabled: true,
826 },
827 shared_libs: ["libfwk"], // Cause error
828 nocrt: true,
829 }
830
831 cc_library {
832 name: "libfwk",
833 nocrt: true,
834 }
835 `)
836
837 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
838 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
839 cc_library {
840 name: "libvndk",
841 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900842 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800843 vndk: {
844 enabled: true,
845 },
846 shared_libs: ["libvendor"], // Cause error
847 nocrt: true,
848 }
849
850 cc_library {
851 name: "libvendor",
852 vendor: true,
853 nocrt: true,
854 }
855 `)
856
857 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
858 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
859 cc_library {
860 name: "libvndk_sp",
861 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900862 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800863 vndk: {
864 enabled: true,
865 support_system_process: true,
866 },
867 shared_libs: ["libfwk"], // Cause error
868 nocrt: true,
869 }
870
871 cc_library {
872 name: "libfwk",
873 nocrt: true,
874 }
875 `)
876
877 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
878 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
879 cc_library {
880 name: "libvndk_sp",
881 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900882 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800883 vndk: {
884 enabled: true,
885 support_system_process: true,
886 },
887 shared_libs: ["libvendor"], // Cause error
888 nocrt: true,
889 }
890
891 cc_library {
892 name: "libvendor",
893 vendor: true,
894 nocrt: true,
895 }
896 `)
897
898 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
899 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
900 cc_library {
901 name: "libvndk_sp",
902 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900903 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800904 vndk: {
905 enabled: true,
906 support_system_process: true,
907 },
908 shared_libs: ["libvndk"], // Cause error
909 nocrt: true,
910 }
911
912 cc_library {
913 name: "libvndk",
914 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900915 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800916 vndk: {
917 enabled: true,
918 },
919 nocrt: true,
920 }
921 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900922
923 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
924 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
925 cc_library {
926 name: "libvndk",
927 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900928 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900929 vndk: {
930 enabled: true,
931 },
932 shared_libs: ["libnonvndk"],
933 nocrt: true,
934 }
935
936 cc_library {
937 name: "libnonvndk",
938 vendor_available: true,
939 nocrt: true,
940 }
941 `)
942
943 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
944 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
945 cc_library {
946 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900947 vendor_available: true,
948 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900949 vndk: {
950 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900951 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900952 },
953 shared_libs: ["libnonvndk"],
954 nocrt: true,
955 }
956
957 cc_library {
958 name: "libnonvndk",
959 vendor_available: true,
960 nocrt: true,
961 }
962 `)
963
964 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
965 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
966 cc_library {
967 name: "libvndksp",
968 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900969 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900970 vndk: {
971 enabled: true,
972 support_system_process: true,
973 },
974 shared_libs: ["libnonvndk"],
975 nocrt: true,
976 }
977
978 cc_library {
979 name: "libnonvndk",
980 vendor_available: true,
981 nocrt: true,
982 }
983 `)
984
985 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
986 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
987 cc_library {
988 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900989 vendor_available: true,
990 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900991 vndk: {
992 enabled: true,
993 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900994 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900995 },
996 shared_libs: ["libnonvndk"],
997 nocrt: true,
998 }
999
1000 cc_library {
1001 name: "libnonvndk",
1002 vendor_available: true,
1003 nocrt: true,
1004 }
1005 `)
1006}
1007
1008func TestDoubleLoadbleDep(t *testing.T) {
1009 // okay to link : LLNDK -> double_loadable VNDK
1010 testCc(t, `
1011 cc_library {
1012 name: "libllndk",
1013 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001014 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001015 }
1016
1017 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001018 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001019 symbol_file: "",
1020 }
1021
1022 cc_library {
1023 name: "libdoubleloadable",
1024 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001025 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001026 vndk: {
1027 enabled: true,
1028 },
1029 double_loadable: true,
1030 }
1031 `)
1032 // okay to link : LLNDK -> VNDK-SP
1033 testCc(t, `
1034 cc_library {
1035 name: "libllndk",
1036 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -07001037 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001038 }
1039
1040 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001041 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001042 symbol_file: "",
1043 }
1044
1045 cc_library {
1046 name: "libvndksp",
1047 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001048 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001049 vndk: {
1050 enabled: true,
1051 support_system_process: true,
1052 },
1053 }
1054 `)
1055 // okay to link : double_loadable -> double_loadable
1056 testCc(t, `
1057 cc_library {
1058 name: "libdoubleloadable1",
1059 shared_libs: ["libdoubleloadable2"],
1060 vendor_available: true,
1061 double_loadable: true,
1062 }
1063
1064 cc_library {
1065 name: "libdoubleloadable2",
1066 vendor_available: true,
1067 double_loadable: true,
1068 }
1069 `)
1070 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1071 testCc(t, `
1072 cc_library {
1073 name: "libdoubleloadable",
1074 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001075 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001076 vndk: {
1077 enabled: true,
1078 },
1079 double_loadable: true,
1080 shared_libs: ["libnondoubleloadable"],
1081 }
1082
1083 cc_library {
1084 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001085 vendor_available: true,
1086 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001087 vndk: {
1088 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001089 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001090 },
1091 double_loadable: true,
1092 }
1093 `)
1094 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1095 testCc(t, `
1096 cc_library {
1097 name: "libllndk",
1098 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001099 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001100 }
1101
1102 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001103 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001104 symbol_file: "",
1105 }
1106
1107 cc_library {
1108 name: "libcoreonly",
1109 shared_libs: ["libvendoravailable"],
1110 }
1111
1112 // indirect dependency of LLNDK
1113 cc_library {
1114 name: "libvendoravailable",
1115 vendor_available: true,
1116 double_loadable: true,
1117 }
1118 `)
1119}
1120
Inseob Kim5f58ff72020-09-07 19:53:31 +09001121func TestVendorSnapshotCapture(t *testing.T) {
Inseob Kim8471cda2019-11-15 09:59:12 +09001122 bp := `
1123 cc_library {
1124 name: "libvndk",
1125 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001126 product_available: true,
Inseob Kim8471cda2019-11-15 09:59:12 +09001127 vndk: {
1128 enabled: true,
1129 },
1130 nocrt: true,
1131 }
1132
1133 cc_library {
1134 name: "libvendor",
1135 vendor: true,
1136 nocrt: true,
1137 }
1138
1139 cc_library {
1140 name: "libvendor_available",
1141 vendor_available: true,
1142 nocrt: true,
1143 }
1144
1145 cc_library_headers {
1146 name: "libvendor_headers",
1147 vendor_available: true,
1148 nocrt: true,
1149 }
1150
1151 cc_binary {
1152 name: "vendor_bin",
1153 vendor: true,
1154 nocrt: true,
1155 }
1156
1157 cc_binary {
1158 name: "vendor_available_bin",
1159 vendor_available: true,
1160 nocrt: true,
1161 }
Inseob Kim7f283f42020-06-01 21:53:49 +09001162
1163 toolchain_library {
1164 name: "libb",
1165 vendor_available: true,
1166 src: "libb.a",
1167 }
Inseob Kim1042d292020-06-01 23:23:05 +09001168
1169 cc_object {
1170 name: "obj",
1171 vendor_available: true,
1172 }
Colin Cross127bb8b2020-12-16 16:46:01 -08001173
1174 cc_library {
1175 name: "libllndk",
1176 llndk_stubs: "libllndk.llndk",
1177 }
1178
1179 llndk_library {
1180 name: "libllndk.llndk",
1181 symbol_file: "",
1182 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001183`
1184 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1185 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1186 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1187 ctx := testCcWithConfig(t, config)
1188
1189 // Check Vendor snapshot output.
1190
1191 snapshotDir := "vendor-snapshot"
1192 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +09001193 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1194
1195 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +09001196
1197 for _, arch := range [][]string{
1198 []string{"arm64", "armv8-a"},
1199 []string{"arm", "armv7-a-neon"},
1200 } {
1201 archType := arch[0]
1202 archVariant := arch[1]
1203 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1204
1205 // For shared libraries, only non-VNDK vendor_available modules are captured
1206 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1207 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +09001208 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1209 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
1210 jsonFiles = append(jsonFiles,
1211 filepath.Join(sharedDir, "libvendor.so.json"),
1212 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001213
Colin Cross127bb8b2020-12-16 16:46:01 -08001214 // LLNDK modules are not captured
1215 checkSnapshotExclude(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", sharedDir, sharedVariant)
1216
Inseob Kim8471cda2019-11-15 09:59:12 +09001217 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
Inseob Kimc42f2f22020-07-29 20:32:10 +09001218 // Also cfi variants are captured, except for prebuilts like toolchain_library
Inseob Kim8471cda2019-11-15 09:59:12 +09001219 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001220 staticCfiVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static_cfi", archType, archVariant)
Inseob Kim8471cda2019-11-15 09:59:12 +09001221 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +09001222 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1223 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001224 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001225 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001226 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001227 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001228 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001229 jsonFiles = append(jsonFiles,
1230 filepath.Join(staticDir, "libb.a.json"),
1231 filepath.Join(staticDir, "libvndk.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001232 filepath.Join(staticDir, "libvndk.cfi.a.json"),
Inseob Kim7f283f42020-06-01 21:53:49 +09001233 filepath.Join(staticDir, "libvendor.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001234 filepath.Join(staticDir, "libvendor.cfi.a.json"),
1235 filepath.Join(staticDir, "libvendor_available.a.json"),
1236 filepath.Join(staticDir, "libvendor_available.cfi.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001237
Inseob Kim7f283f42020-06-01 21:53:49 +09001238 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +09001239 if archType == "arm64" {
1240 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1241 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +09001242 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
1243 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
1244 jsonFiles = append(jsonFiles,
1245 filepath.Join(binaryDir, "vendor_bin.json"),
1246 filepath.Join(binaryDir, "vendor_available_bin.json"))
1247 }
1248
1249 // For header libraries, all vendor:true and vendor_available modules are captured.
1250 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1251 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +09001252
1253 // For object modules, all vendor:true and vendor_available modules are captured.
1254 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1255 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1256 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1257 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001258 }
1259
1260 for _, jsonFile := range jsonFiles {
1261 // verify all json files exist
1262 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1263 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001264 }
1265 }
Inseob Kime9aec6a2021-01-05 20:03:22 +09001266
1267 // fake snapshot should have all outputs in the normal snapshot.
1268 fakeSnapshotSingleton := ctx.SingletonForTests("vendor-fake-snapshot")
1269 for _, output := range snapshotSingleton.AllOutputs() {
1270 fakeOutput := strings.Replace(output, "/vendor-snapshot/", "/fake/vendor-snapshot/", 1)
1271 if fakeSnapshotSingleton.MaybeOutput(fakeOutput).Rule == nil {
1272 t.Errorf("%q expected but not found", fakeOutput)
1273 }
1274 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001275}
1276
Inseob Kim5f58ff72020-09-07 19:53:31 +09001277func TestVendorSnapshotUse(t *testing.T) {
1278 frameworkBp := `
1279 cc_library {
1280 name: "libvndk",
1281 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001282 product_available: true,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001283 vndk: {
1284 enabled: true,
1285 },
1286 nocrt: true,
1287 compile_multilib: "64",
1288 }
1289
1290 cc_library {
1291 name: "libvendor",
1292 vendor: true,
1293 nocrt: true,
1294 no_libcrt: true,
1295 stl: "none",
1296 system_shared_libs: [],
1297 compile_multilib: "64",
1298 }
1299
1300 cc_binary {
1301 name: "bin",
1302 vendor: true,
1303 nocrt: true,
1304 no_libcrt: true,
1305 stl: "none",
1306 system_shared_libs: [],
1307 compile_multilib: "64",
1308 }
1309`
1310
1311 vndkBp := `
1312 vndk_prebuilt_shared {
1313 name: "libvndk",
1314 version: "BOARD",
1315 target_arch: "arm64",
1316 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001317 product_available: true,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001318 vndk: {
1319 enabled: true,
1320 },
1321 arch: {
1322 arm64: {
1323 srcs: ["libvndk.so"],
Inseob Kim67be7322020-10-19 10:15:28 +09001324 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001325 },
1326 },
1327 }
1328`
1329
1330 vendorProprietaryBp := `
1331 cc_library {
1332 name: "libvendor_without_snapshot",
1333 vendor: true,
1334 nocrt: true,
1335 no_libcrt: true,
1336 stl: "none",
1337 system_shared_libs: [],
1338 compile_multilib: "64",
1339 }
1340
1341 cc_library_shared {
1342 name: "libclient",
1343 vendor: true,
1344 nocrt: true,
1345 no_libcrt: true,
1346 stl: "none",
1347 system_shared_libs: [],
1348 shared_libs: ["libvndk"],
1349 static_libs: ["libvendor", "libvendor_without_snapshot"],
1350 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001351 srcs: ["client.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001352 }
1353
1354 cc_binary {
1355 name: "bin_without_snapshot",
1356 vendor: true,
1357 nocrt: true,
1358 no_libcrt: true,
1359 stl: "none",
1360 system_shared_libs: [],
1361 static_libs: ["libvndk"],
1362 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001363 srcs: ["bin.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001364 }
1365
1366 vendor_snapshot_static {
1367 name: "libvndk",
1368 version: "BOARD",
1369 target_arch: "arm64",
1370 vendor: true,
1371 arch: {
1372 arm64: {
1373 src: "libvndk.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001374 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001375 },
1376 },
1377 }
1378
1379 vendor_snapshot_shared {
1380 name: "libvendor",
1381 version: "BOARD",
1382 target_arch: "arm64",
1383 vendor: true,
1384 arch: {
1385 arm64: {
1386 src: "libvendor.so",
Inseob Kim67be7322020-10-19 10:15:28 +09001387 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001388 },
1389 },
1390 }
1391
1392 vendor_snapshot_static {
1393 name: "libvendor",
1394 version: "BOARD",
1395 target_arch: "arm64",
1396 vendor: true,
1397 arch: {
1398 arm64: {
1399 src: "libvendor.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001400 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001401 },
1402 },
1403 }
1404
1405 vendor_snapshot_binary {
1406 name: "bin",
1407 version: "BOARD",
1408 target_arch: "arm64",
1409 vendor: true,
1410 arch: {
1411 arm64: {
1412 src: "bin",
1413 },
1414 },
1415 }
1416`
1417 depsBp := GatherRequiredDepsForTest(android.Android)
1418
1419 mockFS := map[string][]byte{
Inseob Kim67be7322020-10-19 10:15:28 +09001420 "deps/Android.bp": []byte(depsBp),
1421 "framework/Android.bp": []byte(frameworkBp),
1422 "vendor/Android.bp": []byte(vendorProprietaryBp),
1423 "vendor/bin": nil,
1424 "vendor/bin.cpp": nil,
1425 "vendor/client.cpp": nil,
1426 "vendor/include/libvndk/a.h": nil,
1427 "vendor/include/libvendor/b.h": nil,
1428 "vendor/libvndk.a": nil,
1429 "vendor/libvendor.a": nil,
1430 "vendor/libvendor.so": nil,
1431 "vndk/Android.bp": []byte(vndkBp),
1432 "vndk/include/libvndk/a.h": nil,
1433 "vndk/libvndk.so": nil,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001434 }
1435
1436 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1437 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1438 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001439 ctx := CreateTestContext(config)
1440 ctx.Register()
Inseob Kim5f58ff72020-09-07 19:53:31 +09001441
1442 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "vendor/Android.bp", "vndk/Android.bp"})
1443 android.FailIfErrored(t, errs)
1444 _, errs = ctx.PrepareBuildActions(config)
1445 android.FailIfErrored(t, errs)
1446
1447 sharedVariant := "android_vendor.BOARD_arm64_armv8-a_shared"
1448 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1449 binaryVariant := "android_vendor.BOARD_arm64_armv8-a"
1450
1451 // libclient uses libvndk.vndk.BOARD.arm64, libvendor.vendor_static.BOARD.arm64, libvendor_without_snapshot
Inseob Kim67be7322020-10-19 10:15:28 +09001452 libclientCcFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("cc").Args["cFlags"]
1453 for _, includeFlags := range []string{
1454 "-Ivndk/include/libvndk", // libvndk
1455 "-Ivendor/include/libvendor", // libvendor
1456 } {
1457 if !strings.Contains(libclientCcFlags, includeFlags) {
1458 t.Errorf("flags for libclient must contain %#v, but was %#v.",
1459 includeFlags, libclientCcFlags)
1460 }
1461 }
Inseob Kim5f58ff72020-09-07 19:53:31 +09001462
Inseob Kim67be7322020-10-19 10:15:28 +09001463 libclientLdFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001464 for _, input := range [][]string{
1465 []string{sharedVariant, "libvndk.vndk.BOARD.arm64"},
1466 []string{staticVariant, "libvendor.vendor_static.BOARD.arm64"},
1467 []string{staticVariant, "libvendor_without_snapshot"},
1468 } {
1469 outputPaths := getOutputPaths(ctx, input[0] /* variant */, []string{input[1]} /* module name */)
Inseob Kim67be7322020-10-19 10:15:28 +09001470 if !strings.Contains(libclientLdFlags, outputPaths[0].String()) {
1471 t.Errorf("libflags for libclient must contain %#v, but was %#v", outputPaths[0], libclientLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001472 }
1473 }
1474
1475 // bin_without_snapshot uses libvndk.vendor_static.BOARD.arm64
Inseob Kim67be7322020-10-19 10:15:28 +09001476 binWithoutSnapshotCcFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("cc").Args["cFlags"]
1477 if !strings.Contains(binWithoutSnapshotCcFlags, "-Ivendor/include/libvndk") {
1478 t.Errorf("flags for bin_without_snapshot must contain %#v, but was %#v.",
1479 "-Ivendor/include/libvndk", binWithoutSnapshotCcFlags)
1480 }
1481
1482 binWithoutSnapshotLdFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001483 libVndkStaticOutputPaths := getOutputPaths(ctx, staticVariant, []string{"libvndk.vendor_static.BOARD.arm64"})
Inseob Kim67be7322020-10-19 10:15:28 +09001484 if !strings.Contains(binWithoutSnapshotLdFlags, libVndkStaticOutputPaths[0].String()) {
Inseob Kim5f58ff72020-09-07 19:53:31 +09001485 t.Errorf("libflags for bin_without_snapshot must contain %#v, but was %#v",
Inseob Kim67be7322020-10-19 10:15:28 +09001486 libVndkStaticOutputPaths[0], binWithoutSnapshotLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001487 }
1488
1489 // libvendor.so is installed by libvendor.vendor_shared.BOARD.arm64
1490 ctx.ModuleForTests("libvendor.vendor_shared.BOARD.arm64", sharedVariant).Output("libvendor.so")
1491
1492 // libvendor_without_snapshot.so is installed by libvendor_without_snapshot
1493 ctx.ModuleForTests("libvendor_without_snapshot", sharedVariant).Output("libvendor_without_snapshot.so")
1494
1495 // bin is installed by bin.vendor_binary.BOARD.arm64
1496 ctx.ModuleForTests("bin.vendor_binary.BOARD.arm64", binaryVariant).Output("bin")
1497
1498 // bin_without_snapshot is installed by bin_without_snapshot
1499 ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Output("bin_without_snapshot")
1500
1501 // libvendor and bin don't have vendor.BOARD variant
1502 libvendorVariants := ctx.ModuleVariantsForTests("libvendor")
1503 if inList(sharedVariant, libvendorVariants) {
1504 t.Errorf("libvendor must not have variant %#v, but it does", sharedVariant)
1505 }
1506
1507 binVariants := ctx.ModuleVariantsForTests("bin")
1508 if inList(binaryVariant, binVariants) {
1509 t.Errorf("bin must not have variant %#v, but it does", sharedVariant)
1510 }
1511}
1512
Inseob Kimc42f2f22020-07-29 20:32:10 +09001513func TestVendorSnapshotSanitizer(t *testing.T) {
1514 bp := `
1515 vendor_snapshot_static {
1516 name: "libsnapshot",
1517 vendor: true,
1518 target_arch: "arm64",
1519 version: "BOARD",
1520 arch: {
1521 arm64: {
1522 src: "libsnapshot.a",
1523 cfi: {
1524 src: "libsnapshot.cfi.a",
1525 }
1526 },
1527 },
1528 }
1529`
1530 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1531 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1532 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1533 ctx := testCcWithConfig(t, config)
1534
1535 // Check non-cfi and cfi variant.
1536 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1537 staticCfiVariant := "android_vendor.BOARD_arm64_armv8-a_static_cfi"
1538
1539 staticModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticVariant).Module().(*Module)
1540 assertString(t, staticModule.outputFile.Path().Base(), "libsnapshot.a")
1541
1542 staticCfiModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticCfiVariant).Module().(*Module)
1543 assertString(t, staticCfiModule.outputFile.Path().Base(), "libsnapshot.cfi.a")
1544}
1545
Bill Peckham945441c2020-08-31 16:07:58 -07001546func assertExcludeFromVendorSnapshotIs(t *testing.T, c *Module, expected bool) {
1547 t.Helper()
1548 if c.ExcludeFromVendorSnapshot() != expected {
1549 t.Errorf("expected %q ExcludeFromVendorSnapshot to be %t", c.String(), expected)
1550 }
1551}
1552
Jose Galmes6f843bc2020-12-11 13:36:29 -08001553func assertExcludeFromRecoverySnapshotIs(t *testing.T, c *Module, expected bool) {
1554 t.Helper()
1555 if c.ExcludeFromRecoverySnapshot() != expected {
1556 t.Errorf("expected %q ExcludeFromRecoverySnapshot to be %t", c.String(), expected)
1557 }
1558}
1559
Bill Peckham945441c2020-08-31 16:07:58 -07001560func TestVendorSnapshotExclude(t *testing.T) {
1561
1562 // This test verifies that the exclude_from_vendor_snapshot property
1563 // makes its way from the Android.bp source file into the module data
1564 // structure. It also verifies that modules are correctly included or
1565 // excluded in the vendor snapshot based on their path (framework or
1566 // vendor) and the exclude_from_vendor_snapshot property.
1567
1568 frameworkBp := `
1569 cc_library_shared {
1570 name: "libinclude",
1571 srcs: ["src/include.cpp"],
1572 vendor_available: true,
1573 }
1574 cc_library_shared {
1575 name: "libexclude",
1576 srcs: ["src/exclude.cpp"],
1577 vendor: true,
1578 exclude_from_vendor_snapshot: true,
1579 }
1580 `
1581
1582 vendorProprietaryBp := `
1583 cc_library_shared {
1584 name: "libvendor",
1585 srcs: ["vendor.cpp"],
1586 vendor: true,
1587 }
1588 `
1589
1590 depsBp := GatherRequiredDepsForTest(android.Android)
1591
1592 mockFS := map[string][]byte{
1593 "deps/Android.bp": []byte(depsBp),
1594 "framework/Android.bp": []byte(frameworkBp),
1595 "framework/include.cpp": nil,
1596 "framework/exclude.cpp": nil,
1597 "device/Android.bp": []byte(vendorProprietaryBp),
1598 "device/vendor.cpp": nil,
1599 }
1600
1601 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1602 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1603 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001604 ctx := CreateTestContext(config)
1605 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001606
1607 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1608 android.FailIfErrored(t, errs)
1609 _, errs = ctx.PrepareBuildActions(config)
1610 android.FailIfErrored(t, errs)
1611
1612 // Test an include and exclude framework module.
1613 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1614 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", vendorVariant).Module().(*Module), false)
1615 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libexclude", vendorVariant).Module().(*Module), true)
1616
1617 // A vendor module is excluded, but by its path, not the
1618 // exclude_from_vendor_snapshot property.
1619 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libvendor", vendorVariant).Module().(*Module), false)
1620
1621 // Verify the content of the vendor snapshot.
1622
1623 snapshotDir := "vendor-snapshot"
1624 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1625 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1626
1627 var includeJsonFiles []string
1628 var excludeJsonFiles []string
1629
1630 for _, arch := range [][]string{
1631 []string{"arm64", "armv8-a"},
1632 []string{"arm", "armv7-a-neon"},
1633 } {
1634 archType := arch[0]
1635 archVariant := arch[1]
1636 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1637
1638 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1639 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1640
1641 // Included modules
1642 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1643 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1644
1645 // Excluded modules
1646 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1647 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1648 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1649 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1650 }
1651
1652 // Verify that each json file for an included module has a rule.
1653 for _, jsonFile := range includeJsonFiles {
1654 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1655 t.Errorf("include json file %q not found", jsonFile)
1656 }
1657 }
1658
1659 // Verify that each json file for an excluded module has no rule.
1660 for _, jsonFile := range excludeJsonFiles {
1661 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1662 t.Errorf("exclude json file %q found", jsonFile)
1663 }
1664 }
1665}
1666
1667func TestVendorSnapshotExcludeInVendorProprietaryPathErrors(t *testing.T) {
1668
1669 // This test verifies that using the exclude_from_vendor_snapshot
1670 // property on a module in a vendor proprietary path generates an
1671 // error. These modules are already excluded, so we prohibit using the
1672 // property in this way, which could add to confusion.
1673
1674 vendorProprietaryBp := `
1675 cc_library_shared {
1676 name: "libvendor",
1677 srcs: ["vendor.cpp"],
1678 vendor: true,
1679 exclude_from_vendor_snapshot: true,
1680 }
1681 `
1682
1683 depsBp := GatherRequiredDepsForTest(android.Android)
1684
1685 mockFS := map[string][]byte{
1686 "deps/Android.bp": []byte(depsBp),
1687 "device/Android.bp": []byte(vendorProprietaryBp),
1688 "device/vendor.cpp": nil,
1689 }
1690
1691 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1692 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1693 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001694 ctx := CreateTestContext(config)
1695 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001696
1697 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "device/Android.bp"})
1698 android.FailIfErrored(t, errs)
1699
1700 _, errs = ctx.PrepareBuildActions(config)
1701 android.CheckErrorsAgainstExpectations(t, errs, []string{
1702 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1703 `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 -08001704 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1705 `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 +09001706 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1707 `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 -07001708 })
1709}
1710
1711func TestVendorSnapshotExcludeWithVendorAvailable(t *testing.T) {
1712
1713 // This test verifies that using the exclude_from_vendor_snapshot
1714 // property on a module that is vendor available generates an error. A
1715 // vendor available module must be captured in the vendor snapshot and
1716 // must not built from source when building the vendor image against
1717 // the vendor snapshot.
1718
1719 frameworkBp := `
1720 cc_library_shared {
1721 name: "libinclude",
1722 srcs: ["src/include.cpp"],
1723 vendor_available: true,
1724 exclude_from_vendor_snapshot: true,
1725 }
1726 `
1727
1728 depsBp := GatherRequiredDepsForTest(android.Android)
1729
1730 mockFS := map[string][]byte{
1731 "deps/Android.bp": []byte(depsBp),
1732 "framework/Android.bp": []byte(frameworkBp),
1733 "framework/include.cpp": nil,
1734 }
1735
1736 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1737 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1738 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001739 ctx := CreateTestContext(config)
1740 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001741
1742 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp"})
1743 android.FailIfErrored(t, errs)
1744
1745 _, errs = ctx.PrepareBuildActions(config)
1746 android.CheckErrorsAgainstExpectations(t, errs, []string{
1747 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1748 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1749 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1750 `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 +09001751 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1752 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1753 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1754 `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 -07001755 })
1756}
1757
Jose Galmesf7294582020-11-13 12:07:36 -08001758func TestRecoverySnapshotCapture(t *testing.T) {
1759 bp := `
1760 cc_library {
1761 name: "libvndk",
1762 vendor_available: true,
1763 recovery_available: true,
1764 product_available: true,
1765 vndk: {
1766 enabled: true,
1767 },
1768 nocrt: true,
1769 }
1770
1771 cc_library {
1772 name: "librecovery",
1773 recovery: true,
1774 nocrt: true,
1775 }
1776
1777 cc_library {
1778 name: "librecovery_available",
1779 recovery_available: true,
1780 nocrt: true,
1781 }
1782
1783 cc_library_headers {
1784 name: "librecovery_headers",
1785 recovery_available: true,
1786 nocrt: true,
1787 }
1788
1789 cc_binary {
1790 name: "recovery_bin",
1791 recovery: true,
1792 nocrt: true,
1793 }
1794
1795 cc_binary {
1796 name: "recovery_available_bin",
1797 recovery_available: true,
1798 nocrt: true,
1799 }
1800
1801 toolchain_library {
1802 name: "libb",
1803 recovery_available: true,
1804 src: "libb.a",
1805 }
1806
1807 cc_object {
1808 name: "obj",
1809 recovery_available: true,
1810 }
1811`
1812 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jose Galmes6f843bc2020-12-11 13:36:29 -08001813 config.TestProductVariables.RecoverySnapshotVersion = StringPtr("current")
Jose Galmesf7294582020-11-13 12:07:36 -08001814 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1815 ctx := testCcWithConfig(t, config)
1816
1817 // Check Recovery snapshot output.
1818
1819 snapshotDir := "recovery-snapshot"
1820 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1821 snapshotSingleton := ctx.SingletonForTests("recovery-snapshot")
1822
1823 var jsonFiles []string
1824
1825 for _, arch := range [][]string{
1826 []string{"arm64", "armv8-a"},
1827 } {
1828 archType := arch[0]
1829 archVariant := arch[1]
1830 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1831
1832 // For shared libraries, only recovery_available modules are captured.
1833 sharedVariant := fmt.Sprintf("android_recovery_%s_%s_shared", archType, archVariant)
1834 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1835 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", sharedDir, sharedVariant)
1836 checkSnapshot(t, ctx, snapshotSingleton, "librecovery", "librecovery.so", sharedDir, sharedVariant)
1837 checkSnapshot(t, ctx, snapshotSingleton, "librecovery_available", "librecovery_available.so", sharedDir, sharedVariant)
1838 jsonFiles = append(jsonFiles,
1839 filepath.Join(sharedDir, "libvndk.so.json"),
1840 filepath.Join(sharedDir, "librecovery.so.json"),
1841 filepath.Join(sharedDir, "librecovery_available.so.json"))
1842
1843 // For static libraries, all recovery:true and recovery_available modules are captured.
1844 staticVariant := fmt.Sprintf("android_recovery_%s_%s_static", archType, archVariant)
1845 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
1846 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1847 checkSnapshot(t, ctx, snapshotSingleton, "librecovery", "librecovery.a", staticDir, staticVariant)
1848 checkSnapshot(t, ctx, snapshotSingleton, "librecovery_available", "librecovery_available.a", staticDir, staticVariant)
1849 jsonFiles = append(jsonFiles,
1850 filepath.Join(staticDir, "libb.a.json"),
1851 filepath.Join(staticDir, "librecovery.a.json"),
1852 filepath.Join(staticDir, "librecovery_available.a.json"))
1853
1854 // For binary executables, all recovery:true and recovery_available modules are captured.
1855 if archType == "arm64" {
1856 binaryVariant := fmt.Sprintf("android_recovery_%s_%s", archType, archVariant)
1857 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
1858 checkSnapshot(t, ctx, snapshotSingleton, "recovery_bin", "recovery_bin", binaryDir, binaryVariant)
1859 checkSnapshot(t, ctx, snapshotSingleton, "recovery_available_bin", "recovery_available_bin", binaryDir, binaryVariant)
1860 jsonFiles = append(jsonFiles,
1861 filepath.Join(binaryDir, "recovery_bin.json"),
1862 filepath.Join(binaryDir, "recovery_available_bin.json"))
1863 }
1864
1865 // For header libraries, all vendor:true and vendor_available modules are captured.
1866 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1867 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "librecovery_headers.json"))
1868
1869 // For object modules, all vendor:true and vendor_available modules are captured.
1870 objectVariant := fmt.Sprintf("android_recovery_%s_%s", archType, archVariant)
1871 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1872 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1873 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
1874 }
1875
1876 for _, jsonFile := range jsonFiles {
1877 // verify all json files exist
1878 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1879 t.Errorf("%q expected but not found", jsonFile)
1880 }
1881 }
1882}
1883
Jose Galmes6f843bc2020-12-11 13:36:29 -08001884func TestRecoverySnapshotExclude(t *testing.T) {
1885 // This test verifies that the exclude_from_recovery_snapshot property
1886 // makes its way from the Android.bp source file into the module data
1887 // structure. It also verifies that modules are correctly included or
1888 // excluded in the recovery snapshot based on their path (framework or
1889 // vendor) and the exclude_from_recovery_snapshot property.
1890
1891 frameworkBp := `
1892 cc_library_shared {
1893 name: "libinclude",
1894 srcs: ["src/include.cpp"],
1895 recovery_available: true,
1896 }
1897 cc_library_shared {
1898 name: "libexclude",
1899 srcs: ["src/exclude.cpp"],
1900 recovery: true,
1901 exclude_from_recovery_snapshot: true,
1902 }
1903 `
1904
1905 vendorProprietaryBp := `
1906 cc_library_shared {
1907 name: "libvendor",
1908 srcs: ["vendor.cpp"],
1909 recovery: true,
1910 }
1911 `
1912
1913 depsBp := GatherRequiredDepsForTest(android.Android)
1914
1915 mockFS := map[string][]byte{
1916 "deps/Android.bp": []byte(depsBp),
1917 "framework/Android.bp": []byte(frameworkBp),
1918 "framework/include.cpp": nil,
1919 "framework/exclude.cpp": nil,
1920 "device/Android.bp": []byte(vendorProprietaryBp),
1921 "device/vendor.cpp": nil,
1922 }
1923
1924 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1925 config.TestProductVariables.RecoverySnapshotVersion = StringPtr("current")
1926 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1927 ctx := CreateTestContext(config)
1928 ctx.Register()
1929
1930 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1931 android.FailIfErrored(t, errs)
1932 _, errs = ctx.PrepareBuildActions(config)
1933 android.FailIfErrored(t, errs)
1934
1935 // Test an include and exclude framework module.
1936 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1937 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libinclude", recoveryVariant).Module().(*Module), false)
1938 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libexclude", recoveryVariant).Module().(*Module), true)
1939
1940 // A vendor module is excluded, but by its path, not the
1941 // exclude_from_recovery_snapshot property.
1942 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libvendor", recoveryVariant).Module().(*Module), false)
1943
1944 // Verify the content of the recovery snapshot.
1945
1946 snapshotDir := "recovery-snapshot"
1947 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1948 snapshotSingleton := ctx.SingletonForTests("recovery-snapshot")
1949
1950 var includeJsonFiles []string
1951 var excludeJsonFiles []string
1952
1953 for _, arch := range [][]string{
1954 []string{"arm64", "armv8-a"},
1955 } {
1956 archType := arch[0]
1957 archVariant := arch[1]
1958 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1959
1960 sharedVariant := fmt.Sprintf("android_recovery_%s_%s_shared", archType, archVariant)
1961 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1962
1963 // Included modules
1964 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1965 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1966
1967 // Excluded modules
1968 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1969 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1970 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1971 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1972 }
1973
1974 // Verify that each json file for an included module has a rule.
1975 for _, jsonFile := range includeJsonFiles {
1976 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1977 t.Errorf("include json file %q not found", jsonFile)
1978 }
1979 }
1980
1981 // Verify that each json file for an excluded module has no rule.
1982 for _, jsonFile := range excludeJsonFiles {
1983 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1984 t.Errorf("exclude json file %q found", jsonFile)
1985 }
1986 }
1987}
1988
Jooyung Hana70f0672019-01-18 15:20:43 +09001989func TestDoubleLoadableDepError(t *testing.T) {
1990 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1991 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1992 cc_library {
1993 name: "libllndk",
1994 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001995 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001996 }
1997
1998 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001999 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002000 symbol_file: "",
2001 }
2002
2003 cc_library {
2004 name: "libnondoubleloadable",
2005 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002006 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002007 vndk: {
2008 enabled: true,
2009 },
2010 }
2011 `)
2012
2013 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
2014 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2015 cc_library {
2016 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07002017 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002018 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07002019 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002020 }
2021
2022 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002023 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002024 symbol_file: "",
2025 }
2026
2027 cc_library {
2028 name: "libnondoubleloadable",
2029 vendor_available: true,
2030 }
2031 `)
2032
Jooyung Hana70f0672019-01-18 15:20:43 +09002033 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
2034 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2035 cc_library {
2036 name: "libllndk",
2037 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07002038 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002039 }
2040
2041 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002042 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002043 symbol_file: "",
2044 }
2045
2046 cc_library {
2047 name: "libcoreonly",
2048 shared_libs: ["libvendoravailable"],
2049 }
2050
2051 // indirect dependency of LLNDK
2052 cc_library {
2053 name: "libvendoravailable",
2054 vendor_available: true,
2055 }
2056 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09002057
2058 // The error is not from 'client' but from 'libllndk'
2059 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
2060 cc_library {
2061 name: "client",
2062 vendor_available: true,
2063 double_loadable: true,
2064 shared_libs: ["libllndk"],
2065 }
2066 cc_library {
2067 name: "libllndk",
2068 shared_libs: ["libnondoubleloadable"],
2069 llndk_stubs: "libllndk.llndk",
2070 }
2071 llndk_library {
2072 name: "libllndk.llndk",
2073 symbol_file: "",
2074 }
2075 cc_library {
2076 name: "libnondoubleloadable",
2077 vendor_available: true,
2078 }
2079 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08002080}
2081
Jooyung Han479ca172020-10-19 18:51:07 +09002082func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
2083 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
2084 cc_library {
2085 name: "libvndksp",
2086 shared_libs: ["libanothervndksp"],
2087 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002088 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09002089 vndk: {
2090 enabled: true,
2091 support_system_process: true,
2092 }
2093 }
2094
2095 cc_library {
2096 name: "libllndk",
2097 shared_libs: ["libanothervndksp"],
2098 }
2099
2100 llndk_library {
2101 name: "libllndk",
2102 symbol_file: "",
2103 }
2104
2105 cc_library {
2106 name: "libanothervndksp",
2107 vendor_available: true,
2108 }
2109 `)
2110}
2111
Logan Chienf3511742017-10-31 18:04:35 +08002112func TestVndkExt(t *testing.T) {
2113 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09002114 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08002115 cc_library {
2116 name: "libvndk",
2117 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002118 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002119 vndk: {
2120 enabled: true,
2121 },
2122 nocrt: true,
2123 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002124 cc_library {
2125 name: "libvndk2",
2126 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002127 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09002128 vndk: {
2129 enabled: true,
2130 },
2131 target: {
2132 vendor: {
2133 suffix: "-suffix",
2134 },
Justin Yun63e9ec72020-10-29 16:49:43 +09002135 product: {
2136 suffix: "-suffix",
2137 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09002138 },
2139 nocrt: true,
2140 }
Logan Chienf3511742017-10-31 18:04:35 +08002141
2142 cc_library {
2143 name: "libvndk_ext",
2144 vendor: true,
2145 vndk: {
2146 enabled: true,
2147 extends: "libvndk",
2148 },
2149 nocrt: true,
2150 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002151
2152 cc_library {
2153 name: "libvndk2_ext",
2154 vendor: true,
2155 vndk: {
2156 enabled: true,
2157 extends: "libvndk2",
2158 },
2159 nocrt: true,
2160 }
Logan Chienf3511742017-10-31 18:04:35 +08002161
Justin Yun0ecf0b22020-02-28 15:07:59 +09002162 cc_library {
2163 name: "libvndk_ext_product",
2164 product_specific: true,
2165 vndk: {
2166 enabled: true,
2167 extends: "libvndk",
2168 },
2169 nocrt: true,
2170 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002171
Justin Yun0ecf0b22020-02-28 15:07:59 +09002172 cc_library {
2173 name: "libvndk2_ext_product",
2174 product_specific: true,
2175 vndk: {
2176 enabled: true,
2177 extends: "libvndk2",
2178 },
2179 nocrt: true,
2180 }
2181 `
2182 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2183 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2184 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2185 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2186
2187 ctx := testCcWithConfig(t, config)
2188
2189 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
2190 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
2191
2192 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
2193 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
2194
2195 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
2196 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08002197}
2198
Logan Chiend3c59a22018-03-29 14:08:15 +08002199func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08002200 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
2201 ctx := testCcNoVndk(t, `
2202 cc_library {
2203 name: "libvndk",
2204 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002205 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002206 vndk: {
2207 enabled: true,
2208 },
2209 nocrt: true,
2210 }
2211
2212 cc_library {
2213 name: "libvndk_ext",
2214 vendor: true,
2215 vndk: {
2216 enabled: true,
2217 extends: "libvndk",
2218 },
2219 nocrt: true,
2220 }
2221 `)
2222
2223 // Ensures that the core variant of "libvndk_ext" can be found.
2224 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
2225 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
2226 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
2227 }
2228}
2229
Justin Yun0ecf0b22020-02-28 15:07:59 +09002230func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
2231 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09002232 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09002233 cc_library {
2234 name: "libvndk",
2235 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002236 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002237 vndk: {
2238 enabled: true,
2239 },
2240 nocrt: true,
2241 }
2242
2243 cc_library {
2244 name: "libvndk_ext_product",
2245 product_specific: true,
2246 vndk: {
2247 enabled: true,
2248 extends: "libvndk",
2249 },
2250 nocrt: true,
2251 }
2252 `)
2253
2254 // Ensures that the core variant of "libvndk_ext_product" can be found.
2255 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
2256 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
2257 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
2258 }
2259}
2260
Logan Chienf3511742017-10-31 18:04:35 +08002261func TestVndkExtError(t *testing.T) {
2262 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09002263 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08002264 cc_library {
2265 name: "libvndk",
2266 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002267 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002268 vndk: {
2269 enabled: true,
2270 },
2271 nocrt: true,
2272 }
2273
2274 cc_library {
2275 name: "libvndk_ext",
2276 vndk: {
2277 enabled: true,
2278 extends: "libvndk",
2279 },
2280 nocrt: true,
2281 }
2282 `)
2283
2284 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
2285 cc_library {
2286 name: "libvndk",
2287 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002288 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002289 vndk: {
2290 enabled: true,
2291 },
2292 nocrt: true,
2293 }
2294
2295 cc_library {
2296 name: "libvndk_ext",
2297 vendor: true,
2298 vndk: {
2299 enabled: true,
2300 },
2301 nocrt: true,
2302 }
2303 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002304
2305 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
2306 cc_library {
2307 name: "libvndk",
2308 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002309 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002310 vndk: {
2311 enabled: true,
2312 },
2313 nocrt: true,
2314 }
2315
2316 cc_library {
2317 name: "libvndk_ext_product",
2318 product_specific: true,
2319 vndk: {
2320 enabled: true,
2321 },
2322 nocrt: true,
2323 }
2324 `)
2325
2326 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
2327 cc_library {
2328 name: "libvndk",
2329 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002330 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002331 vndk: {
2332 enabled: true,
2333 },
2334 nocrt: true,
2335 }
2336
2337 cc_library {
2338 name: "libvndk_ext_product",
2339 product_specific: true,
2340 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002341 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002342 vndk: {
2343 enabled: true,
2344 extends: "libvndk",
2345 },
2346 nocrt: true,
2347 }
2348 `)
Logan Chienf3511742017-10-31 18:04:35 +08002349}
2350
2351func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
2352 // This test ensures an error is emitted for inconsistent support_system_process.
2353 testCcError(t, "module \".*\" with mismatched support_system_process", `
2354 cc_library {
2355 name: "libvndk",
2356 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002357 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002358 vndk: {
2359 enabled: true,
2360 },
2361 nocrt: true,
2362 }
2363
2364 cc_library {
2365 name: "libvndk_sp_ext",
2366 vendor: true,
2367 vndk: {
2368 enabled: true,
2369 extends: "libvndk",
2370 support_system_process: true,
2371 },
2372 nocrt: true,
2373 }
2374 `)
2375
2376 testCcError(t, "module \".*\" with mismatched support_system_process", `
2377 cc_library {
2378 name: "libvndk_sp",
2379 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002380 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002381 vndk: {
2382 enabled: true,
2383 support_system_process: true,
2384 },
2385 nocrt: true,
2386 }
2387
2388 cc_library {
2389 name: "libvndk_ext",
2390 vendor: true,
2391 vndk: {
2392 enabled: true,
2393 extends: "libvndk_sp",
2394 },
2395 nocrt: true,
2396 }
2397 `)
2398}
2399
2400func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08002401 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09002402 // with `private: true`.
2403 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08002404 cc_library {
2405 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09002406 vendor_available: true,
2407 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002408 vndk: {
2409 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002410 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08002411 },
2412 nocrt: true,
2413 }
2414
2415 cc_library {
2416 name: "libvndk_ext",
2417 vendor: true,
2418 vndk: {
2419 enabled: true,
2420 extends: "libvndk",
2421 },
2422 nocrt: true,
2423 }
2424 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002425
Justin Yunfd9e8042020-12-23 18:23:14 +09002426 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09002427 cc_library {
2428 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09002429 vendor_available: true,
2430 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002431 vndk: {
2432 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002433 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002434 },
2435 nocrt: true,
2436 }
2437
2438 cc_library {
2439 name: "libvndk_ext_product",
2440 product_specific: true,
2441 vndk: {
2442 enabled: true,
2443 extends: "libvndk",
2444 },
2445 nocrt: true,
2446 }
2447 `)
Logan Chienf3511742017-10-31 18:04:35 +08002448}
2449
Logan Chiend3c59a22018-03-29 14:08:15 +08002450func TestVendorModuleUseVndkExt(t *testing.T) {
2451 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002452 testCc(t, `
2453 cc_library {
2454 name: "libvndk",
2455 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002456 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002457 vndk: {
2458 enabled: true,
2459 },
2460 nocrt: true,
2461 }
2462
2463 cc_library {
2464 name: "libvndk_ext",
2465 vendor: true,
2466 vndk: {
2467 enabled: true,
2468 extends: "libvndk",
2469 },
2470 nocrt: true,
2471 }
2472
2473 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08002474 name: "libvndk_sp",
2475 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002476 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002477 vndk: {
2478 enabled: true,
2479 support_system_process: true,
2480 },
2481 nocrt: true,
2482 }
2483
2484 cc_library {
2485 name: "libvndk_sp_ext",
2486 vendor: true,
2487 vndk: {
2488 enabled: true,
2489 extends: "libvndk_sp",
2490 support_system_process: true,
2491 },
2492 nocrt: true,
2493 }
2494
2495 cc_library {
2496 name: "libvendor",
2497 vendor: true,
2498 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
2499 nocrt: true,
2500 }
2501 `)
2502}
2503
Logan Chiend3c59a22018-03-29 14:08:15 +08002504func TestVndkExtUseVendorLib(t *testing.T) {
2505 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08002506 testCc(t, `
2507 cc_library {
2508 name: "libvndk",
2509 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002510 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002511 vndk: {
2512 enabled: true,
2513 },
2514 nocrt: true,
2515 }
2516
2517 cc_library {
2518 name: "libvndk_ext",
2519 vendor: true,
2520 vndk: {
2521 enabled: true,
2522 extends: "libvndk",
2523 },
2524 shared_libs: ["libvendor"],
2525 nocrt: true,
2526 }
2527
2528 cc_library {
2529 name: "libvendor",
2530 vendor: true,
2531 nocrt: true,
2532 }
2533 `)
Logan Chienf3511742017-10-31 18:04:35 +08002534
Logan Chiend3c59a22018-03-29 14:08:15 +08002535 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
2536 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08002537 cc_library {
2538 name: "libvndk_sp",
2539 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002540 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002541 vndk: {
2542 enabled: true,
2543 support_system_process: true,
2544 },
2545 nocrt: true,
2546 }
2547
2548 cc_library {
2549 name: "libvndk_sp_ext",
2550 vendor: true,
2551 vndk: {
2552 enabled: true,
2553 extends: "libvndk_sp",
2554 support_system_process: true,
2555 },
2556 shared_libs: ["libvendor"], // Cause an error
2557 nocrt: true,
2558 }
2559
2560 cc_library {
2561 name: "libvendor",
2562 vendor: true,
2563 nocrt: true,
2564 }
2565 `)
2566}
2567
Justin Yun0ecf0b22020-02-28 15:07:59 +09002568func TestProductVndkExtDependency(t *testing.T) {
2569 bp := `
2570 cc_library {
2571 name: "libvndk",
2572 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002573 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002574 vndk: {
2575 enabled: true,
2576 },
2577 nocrt: true,
2578 }
2579
2580 cc_library {
2581 name: "libvndk_ext_product",
2582 product_specific: true,
2583 vndk: {
2584 enabled: true,
2585 extends: "libvndk",
2586 },
2587 shared_libs: ["libproduct_for_vndklibs"],
2588 nocrt: true,
2589 }
2590
2591 cc_library {
2592 name: "libvndk_sp",
2593 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002594 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002595 vndk: {
2596 enabled: true,
2597 support_system_process: true,
2598 },
2599 nocrt: true,
2600 }
2601
2602 cc_library {
2603 name: "libvndk_sp_ext_product",
2604 product_specific: true,
2605 vndk: {
2606 enabled: true,
2607 extends: "libvndk_sp",
2608 support_system_process: true,
2609 },
2610 shared_libs: ["libproduct_for_vndklibs"],
2611 nocrt: true,
2612 }
2613
2614 cc_library {
2615 name: "libproduct",
2616 product_specific: true,
2617 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
2618 nocrt: true,
2619 }
2620
2621 cc_library {
2622 name: "libproduct_for_vndklibs",
2623 product_specific: true,
2624 nocrt: true,
2625 }
2626 `
2627 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2628 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2629 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2630 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2631
2632 testCcWithConfig(t, config)
2633}
2634
Logan Chiend3c59a22018-03-29 14:08:15 +08002635func TestVndkSpExtUseVndkError(t *testing.T) {
2636 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
2637 // library.
2638 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2639 cc_library {
2640 name: "libvndk",
2641 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002642 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002643 vndk: {
2644 enabled: true,
2645 },
2646 nocrt: true,
2647 }
2648
2649 cc_library {
2650 name: "libvndk_sp",
2651 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002652 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002653 vndk: {
2654 enabled: true,
2655 support_system_process: true,
2656 },
2657 nocrt: true,
2658 }
2659
2660 cc_library {
2661 name: "libvndk_sp_ext",
2662 vendor: true,
2663 vndk: {
2664 enabled: true,
2665 extends: "libvndk_sp",
2666 support_system_process: true,
2667 },
2668 shared_libs: ["libvndk"], // Cause an error
2669 nocrt: true,
2670 }
2671 `)
2672
2673 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
2674 // library.
2675 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2676 cc_library {
2677 name: "libvndk",
2678 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002679 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002680 vndk: {
2681 enabled: true,
2682 },
2683 nocrt: true,
2684 }
2685
2686 cc_library {
2687 name: "libvndk_ext",
2688 vendor: true,
2689 vndk: {
2690 enabled: true,
2691 extends: "libvndk",
2692 },
2693 nocrt: true,
2694 }
2695
2696 cc_library {
2697 name: "libvndk_sp",
2698 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002699 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002700 vndk: {
2701 enabled: true,
2702 support_system_process: true,
2703 },
2704 nocrt: true,
2705 }
2706
2707 cc_library {
2708 name: "libvndk_sp_ext",
2709 vendor: true,
2710 vndk: {
2711 enabled: true,
2712 extends: "libvndk_sp",
2713 support_system_process: true,
2714 },
2715 shared_libs: ["libvndk_ext"], // Cause an error
2716 nocrt: true,
2717 }
2718 `)
2719}
2720
2721func TestVndkUseVndkExtError(t *testing.T) {
2722 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2723 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002724 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2725 cc_library {
2726 name: "libvndk",
2727 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002728 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002729 vndk: {
2730 enabled: true,
2731 },
2732 nocrt: true,
2733 }
2734
2735 cc_library {
2736 name: "libvndk_ext",
2737 vendor: true,
2738 vndk: {
2739 enabled: true,
2740 extends: "libvndk",
2741 },
2742 nocrt: true,
2743 }
2744
2745 cc_library {
2746 name: "libvndk2",
2747 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002748 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002749 vndk: {
2750 enabled: true,
2751 },
2752 shared_libs: ["libvndk_ext"],
2753 nocrt: true,
2754 }
2755 `)
2756
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002757 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002758 cc_library {
2759 name: "libvndk",
2760 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002761 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002762 vndk: {
2763 enabled: true,
2764 },
2765 nocrt: true,
2766 }
2767
2768 cc_library {
2769 name: "libvndk_ext",
2770 vendor: true,
2771 vndk: {
2772 enabled: true,
2773 extends: "libvndk",
2774 },
2775 nocrt: true,
2776 }
2777
2778 cc_library {
2779 name: "libvndk2",
2780 vendor_available: true,
2781 vndk: {
2782 enabled: true,
2783 },
2784 target: {
2785 vendor: {
2786 shared_libs: ["libvndk_ext"],
2787 },
2788 },
2789 nocrt: true,
2790 }
2791 `)
2792
2793 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2794 cc_library {
2795 name: "libvndk_sp",
2796 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002797 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002798 vndk: {
2799 enabled: true,
2800 support_system_process: true,
2801 },
2802 nocrt: true,
2803 }
2804
2805 cc_library {
2806 name: "libvndk_sp_ext",
2807 vendor: true,
2808 vndk: {
2809 enabled: true,
2810 extends: "libvndk_sp",
2811 support_system_process: true,
2812 },
2813 nocrt: true,
2814 }
2815
2816 cc_library {
2817 name: "libvndk_sp_2",
2818 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002819 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002820 vndk: {
2821 enabled: true,
2822 support_system_process: true,
2823 },
2824 shared_libs: ["libvndk_sp_ext"],
2825 nocrt: true,
2826 }
2827 `)
2828
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002829 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002830 cc_library {
2831 name: "libvndk_sp",
2832 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002833 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002834 vndk: {
2835 enabled: true,
2836 },
2837 nocrt: true,
2838 }
2839
2840 cc_library {
2841 name: "libvndk_sp_ext",
2842 vendor: true,
2843 vndk: {
2844 enabled: true,
2845 extends: "libvndk_sp",
2846 },
2847 nocrt: true,
2848 }
2849
2850 cc_library {
2851 name: "libvndk_sp2",
2852 vendor_available: true,
2853 vndk: {
2854 enabled: true,
2855 },
2856 target: {
2857 vendor: {
2858 shared_libs: ["libvndk_sp_ext"],
2859 },
2860 },
2861 nocrt: true,
2862 }
2863 `)
2864}
2865
Justin Yun5f7f7e82019-11-18 19:52:14 +09002866func TestEnforceProductVndkVersion(t *testing.T) {
2867 bp := `
2868 cc_library {
2869 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002870 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002871 }
2872 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002873 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002874 symbol_file: "",
2875 }
2876 cc_library {
2877 name: "libvndk",
2878 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002879 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002880 vndk: {
2881 enabled: true,
2882 },
2883 nocrt: true,
2884 }
2885 cc_library {
2886 name: "libvndk_sp",
2887 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002888 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002889 vndk: {
2890 enabled: true,
2891 support_system_process: true,
2892 },
2893 nocrt: true,
2894 }
2895 cc_library {
2896 name: "libva",
2897 vendor_available: true,
2898 nocrt: true,
2899 }
2900 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002901 name: "libpa",
2902 product_available: true,
2903 nocrt: true,
2904 }
2905 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002906 name: "libboth_available",
2907 vendor_available: true,
2908 product_available: true,
2909 nocrt: true,
2910 target: {
2911 vendor: {
2912 suffix: "-vendor",
2913 },
2914 product: {
2915 suffix: "-product",
2916 },
2917 }
2918 }
2919 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002920 name: "libproduct_va",
2921 product_specific: true,
2922 vendor_available: true,
2923 nocrt: true,
2924 }
2925 cc_library {
2926 name: "libprod",
2927 product_specific: true,
2928 shared_libs: [
2929 "libllndk",
2930 "libvndk",
2931 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002932 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002933 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002934 "libproduct_va",
2935 ],
2936 nocrt: true,
2937 }
2938 cc_library {
2939 name: "libvendor",
2940 vendor: true,
2941 shared_libs: [
2942 "libllndk",
2943 "libvndk",
2944 "libvndk_sp",
2945 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002946 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002947 "libproduct_va",
2948 ],
2949 nocrt: true,
2950 }
2951 `
2952
2953 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2954 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2955 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2956 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2957
2958 ctx := testCcWithConfig(t, config)
2959
Jooyung Han261e1582020-10-20 18:54:21 +09002960 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2961 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002962
2963 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2964 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2965
2966 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2967 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002968}
2969
2970func TestEnforceProductVndkVersionErrors(t *testing.T) {
2971 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2972 cc_library {
2973 name: "libprod",
2974 product_specific: true,
2975 shared_libs: [
2976 "libvendor",
2977 ],
2978 nocrt: true,
2979 }
2980 cc_library {
2981 name: "libvendor",
2982 vendor: true,
2983 nocrt: true,
2984 }
2985 `)
2986 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2987 cc_library {
2988 name: "libprod",
2989 product_specific: true,
2990 shared_libs: [
2991 "libsystem",
2992 ],
2993 nocrt: true,
2994 }
2995 cc_library {
2996 name: "libsystem",
2997 nocrt: true,
2998 }
2999 `)
Justin Yun6977e8a2020-10-29 18:24:11 +09003000 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
3001 cc_library {
3002 name: "libprod",
3003 product_specific: true,
3004 shared_libs: [
3005 "libva",
3006 ],
3007 nocrt: true,
3008 }
3009 cc_library {
3010 name: "libva",
3011 vendor_available: true,
3012 nocrt: true,
3013 }
3014 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09003015 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09003016 cc_library {
3017 name: "libprod",
3018 product_specific: true,
3019 shared_libs: [
3020 "libvndk_private",
3021 ],
3022 nocrt: true,
3023 }
3024 cc_library {
3025 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09003026 vendor_available: true,
3027 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09003028 vndk: {
3029 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09003030 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09003031 },
3032 nocrt: true,
3033 }
3034 `)
3035 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
3036 cc_library {
3037 name: "libprod",
3038 product_specific: true,
3039 shared_libs: [
3040 "libsystem_ext",
3041 ],
3042 nocrt: true,
3043 }
3044 cc_library {
3045 name: "libsystem_ext",
3046 system_ext_specific: true,
3047 nocrt: true,
3048 }
3049 `)
3050 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
3051 cc_library {
3052 name: "libsystem",
3053 shared_libs: [
3054 "libproduct_va",
3055 ],
3056 nocrt: true,
3057 }
3058 cc_library {
3059 name: "libproduct_va",
3060 product_specific: true,
3061 vendor_available: true,
3062 nocrt: true,
3063 }
3064 `)
3065}
3066
Jooyung Han38002912019-05-16 04:01:54 +09003067func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08003068 bp := `
3069 cc_library {
3070 name: "libvndk",
3071 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003072 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003073 vndk: {
3074 enabled: true,
3075 },
3076 }
3077 cc_library {
3078 name: "libvndksp",
3079 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003080 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003081 vndk: {
3082 enabled: true,
3083 support_system_process: true,
3084 },
3085 }
3086 cc_library {
3087 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09003088 vendor_available: true,
3089 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003090 vndk: {
3091 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09003092 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003093 },
3094 }
3095 cc_library {
3096 name: "libvendor",
3097 vendor: true,
3098 }
3099 cc_library {
3100 name: "libvndkext",
3101 vendor: true,
3102 vndk: {
3103 enabled: true,
3104 extends: "libvndk",
3105 },
3106 }
3107 vndk_prebuilt_shared {
3108 name: "prevndk",
3109 version: "27",
3110 target_arch: "arm",
3111 binder32bit: true,
3112 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003113 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003114 vndk: {
3115 enabled: true,
3116 },
3117 arch: {
3118 arm: {
3119 srcs: ["liba.so"],
3120 },
3121 },
3122 }
3123 cc_library {
3124 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07003125 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003126 }
3127 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003128 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003129 symbol_file: "",
3130 }
3131 cc_library {
3132 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07003133 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003134 }
3135 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003136 name: "libllndkprivate.llndk",
Justin Yunc0d8c492021-01-07 17:45:31 +09003137 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003138 symbol_file: "",
3139 }`
3140
3141 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09003142 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3143 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3144 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08003145 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09003146
Jooyung Han0302a842019-10-30 18:43:49 +09003147 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09003148 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09003149 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09003150 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09003151 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09003152 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09003153 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09003154 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09003155
Colin Crossfb0c16e2019-11-20 17:12:35 -08003156 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09003157
Jooyung Han38002912019-05-16 04:01:54 +09003158 tests := []struct {
3159 variant string
3160 name string
3161 expected string
3162 }{
3163 {vendorVariant, "libvndk", "native:vndk"},
3164 {vendorVariant, "libvndksp", "native:vndk"},
3165 {vendorVariant, "libvndkprivate", "native:vndk_private"},
3166 {vendorVariant, "libvendor", "native:vendor"},
3167 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08003168 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09003169 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09003170 {coreVariant, "libvndk", "native:platform"},
3171 {coreVariant, "libvndkprivate", "native:platform"},
3172 {coreVariant, "libllndk", "native:platform"},
3173 }
3174 for _, test := range tests {
3175 t.Run(test.name, func(t *testing.T) {
3176 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
3177 assertString(t, module.makeLinkType, test.expected)
3178 })
3179 }
3180}
3181
Jeff Gaston294356f2017-09-27 17:05:30 -07003182var staticLinkDepOrderTestCases = []struct {
3183 // This is a string representation of a map[moduleName][]moduleDependency .
3184 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003185 inStatic string
3186
3187 // This is a string representation of a map[moduleName][]moduleDependency .
3188 // It models the dependencies declared in an Android.bp file.
3189 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07003190
3191 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
3192 // The keys of allOrdered specify which modules we would like to check.
3193 // The values of allOrdered specify the expected result (of the transitive closure of all
3194 // dependencies) for each module to test
3195 allOrdered string
3196
3197 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
3198 // The keys of outOrdered specify which modules we would like to check.
3199 // The values of outOrdered specify the expected result (of the ordered linker command line)
3200 // for each module to test.
3201 outOrdered string
3202}{
3203 // Simple tests
3204 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003205 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07003206 outOrdered: "",
3207 },
3208 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003209 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003210 outOrdered: "a:",
3211 },
3212 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003213 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003214 outOrdered: "a:b; b:",
3215 },
3216 // Tests of reordering
3217 {
3218 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003219 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003220 outOrdered: "a:b,c,d; b:d; c:d; d:",
3221 },
3222 {
3223 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003224 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003225 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
3226 },
3227 {
3228 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003229 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07003230 outOrdered: "a:d,b,e,c; d:b; e:c",
3231 },
3232 {
3233 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003234 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07003235 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
3236 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
3237 },
3238 {
3239 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003240 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 -07003241 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
3242 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
3243 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003244 // shared dependencies
3245 {
3246 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
3247 // So, we don't actually have to check that a shared dependency of c will change the order
3248 // of a library that depends statically on b and on c. We only need to check that if c has
3249 // a shared dependency on b, that that shows up in allOrdered.
3250 inShared: "c:b",
3251 allOrdered: "c:b",
3252 outOrdered: "c:",
3253 },
3254 {
3255 // This test doesn't actually include any shared dependencies but it's a reminder of what
3256 // the second phase of the above test would look like
3257 inStatic: "a:b,c; c:b",
3258 allOrdered: "a:c,b; c:b",
3259 outOrdered: "a:c,b; c:b",
3260 },
Jeff Gaston294356f2017-09-27 17:05:30 -07003261 // tiebreakers for when two modules specifying different orderings and there is no dependency
3262 // to dictate an order
3263 {
3264 // 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 -08003265 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07003266 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
3267 },
3268 {
3269 // 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 -08003270 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 -07003271 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
3272 },
3273 // Tests involving duplicate dependencies
3274 {
3275 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003276 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003277 outOrdered: "a:c,b",
3278 },
3279 {
3280 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003281 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003282 outOrdered: "a:d,c,b",
3283 },
3284 // Tests to confirm the nonexistence of infinite loops.
3285 // These cases should never happen, so as long as the test terminates and the
3286 // result is deterministic then that should be fine.
3287 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003288 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003289 outOrdered: "a:a",
3290 },
3291 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003292 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003293 allOrdered: "a:b,c; b:c,a; c:a,b",
3294 outOrdered: "a:b; b:c; c:a",
3295 },
3296 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003297 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003298 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
3299 outOrdered: "a:c,b; b:a,c; c:b,a",
3300 },
3301}
3302
3303// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
3304func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
3305 // convert from "a:b,c; d:e" to "a:b,c;d:e"
3306 strippedText := strings.Replace(text, " ", "", -1)
3307 if len(strippedText) < 1 {
3308 return []android.Path{}, make(map[android.Path][]android.Path, 0)
3309 }
3310 allDeps = make(map[android.Path][]android.Path, 0)
3311
3312 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
3313 moduleTexts := strings.Split(strippedText, ";")
3314
3315 outputForModuleName := func(moduleName string) android.Path {
3316 return android.PathForTesting(moduleName)
3317 }
3318
3319 for _, moduleText := range moduleTexts {
3320 // convert from "a:b,c" to ["a", "b,c"]
3321 components := strings.Split(moduleText, ":")
3322 if len(components) != 2 {
3323 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
3324 }
3325 moduleName := components[0]
3326 moduleOutput := outputForModuleName(moduleName)
3327 modulesInOrder = append(modulesInOrder, moduleOutput)
3328
3329 depString := components[1]
3330 // convert from "b,c" to ["b", "c"]
3331 depNames := strings.Split(depString, ",")
3332 if len(depString) < 1 {
3333 depNames = []string{}
3334 }
3335 var deps []android.Path
3336 for _, depName := range depNames {
3337 deps = append(deps, outputForModuleName(depName))
3338 }
3339 allDeps[moduleOutput] = deps
3340 }
3341 return modulesInOrder, allDeps
3342}
3343
Jeff Gaston294356f2017-09-27 17:05:30 -07003344func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
3345 for _, moduleName := range moduleNames {
3346 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
3347 output := module.outputFile.Path()
3348 paths = append(paths, output)
3349 }
3350 return paths
3351}
3352
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003353func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07003354 ctx := testCc(t, `
3355 cc_library {
3356 name: "a",
3357 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09003358 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003359 }
3360 cc_library {
3361 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003362 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003363 }
3364 cc_library {
3365 name: "c",
3366 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003367 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003368 }
3369 cc_library {
3370 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09003371 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003372 }
3373
3374 `)
3375
Colin Cross7113d202019-11-20 16:39:12 -08003376 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07003377 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003378 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3379 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07003380
3381 if !reflect.DeepEqual(actual, expected) {
3382 t.Errorf("staticDeps orderings were not propagated correctly"+
3383 "\nactual: %v"+
3384 "\nexpected: %v",
3385 actual,
3386 expected,
3387 )
3388 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09003389}
Jeff Gaston294356f2017-09-27 17:05:30 -07003390
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003391func TestStaticLibDepReorderingWithShared(t *testing.T) {
3392 ctx := testCc(t, `
3393 cc_library {
3394 name: "a",
3395 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09003396 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003397 }
3398 cc_library {
3399 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003400 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003401 }
3402 cc_library {
3403 name: "c",
3404 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003405 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003406 }
3407
3408 `)
3409
Colin Cross7113d202019-11-20 16:39:12 -08003410 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003411 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003412 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3413 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003414
3415 if !reflect.DeepEqual(actual, expected) {
3416 t.Errorf("staticDeps orderings did not account for shared libs"+
3417 "\nactual: %v"+
3418 "\nexpected: %v",
3419 actual,
3420 expected,
3421 )
3422 }
3423}
3424
Jooyung Hanb04a4992020-03-13 18:57:35 +09003425func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07003426 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003427 if !reflect.DeepEqual(actual, expected) {
3428 t.Errorf(message+
3429 "\nactual: %v"+
3430 "\nexpected: %v",
3431 actual,
3432 expected,
3433 )
3434 }
3435}
3436
Jooyung Han61b66e92020-03-21 14:21:46 +00003437func TestLlndkLibrary(t *testing.T) {
3438 ctx := testCc(t, `
3439 cc_library {
3440 name: "libllndk",
3441 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07003442 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003443 }
3444 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003445 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003446 }
Colin Cross127bb8b2020-12-16 16:46:01 -08003447
3448 cc_prebuilt_library_shared {
3449 name: "libllndkprebuilt",
3450 stubs: { versions: ["1", "2"] },
3451 llndk_stubs: "libllndkprebuilt.llndk",
3452 }
3453 llndk_library {
3454 name: "libllndkprebuilt.llndk",
3455 }
3456
3457 cc_library {
3458 name: "libllndk_with_external_headers",
3459 stubs: { versions: ["1", "2"] },
3460 llndk_stubs: "libllndk_with_external_headers.llndk",
3461 header_libs: ["libexternal_headers"],
3462 export_header_lib_headers: ["libexternal_headers"],
3463 }
3464 llndk_library {
3465 name: "libllndk_with_external_headers.llndk",
3466 }
3467 cc_library_headers {
3468 name: "libexternal_headers",
3469 export_include_dirs: ["include"],
3470 vendor_available: true,
3471 }
Jooyung Han61b66e92020-03-21 14:21:46 +00003472 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08003473 actual := ctx.ModuleVariantsForTests("libllndk")
3474 for i := 0; i < len(actual); i++ {
3475 if !strings.HasPrefix(actual[i], "android_vendor.VER_") {
3476 actual = append(actual[:i], actual[i+1:]...)
3477 i--
3478 }
3479 }
Jooyung Han61b66e92020-03-21 14:21:46 +00003480 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00003481 "android_vendor.VER_arm64_armv8-a_shared_1",
3482 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003483 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003484 "android_vendor.VER_arm_armv7-a-neon_shared_1",
3485 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003486 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003487 }
3488 checkEquals(t, "variants for llndk stubs", expected, actual)
3489
Colin Cross127bb8b2020-12-16 16:46:01 -08003490 params := ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00003491 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
3492
Colin Cross127bb8b2020-12-16 16:46:01 -08003493 params = ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00003494 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
3495}
3496
Jiyong Parka46a4d52017-12-14 19:54:34 +09003497func TestLlndkHeaders(t *testing.T) {
3498 ctx := testCc(t, `
3499 llndk_headers {
3500 name: "libllndk_headers",
3501 export_include_dirs: ["my_include"],
3502 }
3503 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003504 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09003505 export_llndk_headers: ["libllndk_headers"],
3506 }
3507 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07003508 name: "libllndk",
3509 llndk_stubs: "libllndk.llndk",
3510 }
3511
3512 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09003513 name: "libvendor",
3514 shared_libs: ["libllndk"],
3515 vendor: true,
3516 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003517 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08003518 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09003519 }
3520 `)
3521
3522 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08003523 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09003524 cflags := cc.Args["cFlags"]
3525 if !strings.Contains(cflags, "-Imy_include") {
3526 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
3527 }
3528}
3529
Logan Chien43d34c32017-12-20 01:17:32 +08003530func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
3531 actual := module.Properties.AndroidMkRuntimeLibs
3532 if !reflect.DeepEqual(actual, expected) {
3533 t.Errorf("incorrect runtime_libs for shared libs"+
3534 "\nactual: %v"+
3535 "\nexpected: %v",
3536 actual,
3537 expected,
3538 )
3539 }
3540}
3541
3542const runtimeLibAndroidBp = `
3543 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09003544 name: "liball_available",
3545 vendor_available: true,
3546 product_available: true,
3547 no_libcrt : true,
3548 nocrt : true,
3549 system_shared_libs : [],
3550 }
3551 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08003552 name: "libvendor_available1",
3553 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003554 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07003555 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003556 nocrt : true,
3557 system_shared_libs : [],
3558 }
3559 cc_library {
3560 name: "libvendor_available2",
3561 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003562 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08003563 target: {
3564 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09003565 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08003566 }
3567 },
Yi Konge7fe9912019-06-02 00:53:50 -07003568 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003569 nocrt : true,
3570 system_shared_libs : [],
3571 }
3572 cc_library {
3573 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09003574 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07003575 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003576 nocrt : true,
3577 system_shared_libs : [],
3578 }
3579 cc_library {
3580 name: "libvendor1",
3581 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003582 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003583 nocrt : true,
3584 system_shared_libs : [],
3585 }
3586 cc_library {
3587 name: "libvendor2",
3588 vendor: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003589 runtime_libs: ["liball_available", "libvendor1"],
3590 no_libcrt : true,
3591 nocrt : true,
3592 system_shared_libs : [],
3593 }
3594 cc_library {
3595 name: "libproduct_available1",
3596 product_available: true,
3597 runtime_libs: ["liball_available"],
3598 no_libcrt : true,
3599 nocrt : true,
3600 system_shared_libs : [],
3601 }
3602 cc_library {
3603 name: "libproduct1",
3604 product_specific: true,
3605 no_libcrt : true,
3606 nocrt : true,
3607 system_shared_libs : [],
3608 }
3609 cc_library {
3610 name: "libproduct2",
3611 product_specific: true,
3612 runtime_libs: ["liball_available", "libproduct1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003613 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003614 nocrt : true,
3615 system_shared_libs : [],
3616 }
3617`
3618
3619func TestRuntimeLibs(t *testing.T) {
3620 ctx := testCc(t, runtimeLibAndroidBp)
3621
3622 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08003623 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003624
Justin Yun8a2600c2020-12-07 12:44:03 +09003625 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3626 checkRuntimeLibs(t, []string{"liball_available"}, module)
3627
3628 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3629 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003630
3631 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003632 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003633
3634 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3635 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08003636 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003637
Justin Yun8a2600c2020-12-07 12:44:03 +09003638 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3639 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003640
3641 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003642 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1"}, module)
3643
3644 // runtime_libs for product variants have '.product' suffixes if the modules have both core
3645 // and product variants.
3646 variant = "android_product.VER_arm64_armv8-a_shared"
3647
3648 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3649 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
3650
3651 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
3652 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003653}
3654
3655func TestExcludeRuntimeLibs(t *testing.T) {
3656 ctx := testCc(t, runtimeLibAndroidBp)
3657
Colin Cross7113d202019-11-20 16:39:12 -08003658 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003659 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3660 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003661
Colin Crossfb0c16e2019-11-20 17:12:35 -08003662 variant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003663 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08003664 checkRuntimeLibs(t, nil, module)
3665}
3666
3667func TestRuntimeLibsNoVndk(t *testing.T) {
3668 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3669
3670 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3671
Colin Cross7113d202019-11-20 16:39:12 -08003672 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003673
Justin Yun8a2600c2020-12-07 12:44:03 +09003674 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3675 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003676
3677 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003678 checkRuntimeLibs(t, []string{"liball_available", "libvendor1"}, module)
3679
3680 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
3681 checkRuntimeLibs(t, []string{"liball_available", "libproduct1"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003682}
3683
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003684func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003685 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003686 actual := module.Properties.AndroidMkStaticLibs
3687 if !reflect.DeepEqual(actual, expected) {
3688 t.Errorf("incorrect static_libs"+
3689 "\nactual: %v"+
3690 "\nexpected: %v",
3691 actual,
3692 expected,
3693 )
3694 }
3695}
3696
3697const staticLibAndroidBp = `
3698 cc_library {
3699 name: "lib1",
3700 }
3701 cc_library {
3702 name: "lib2",
3703 static_libs: ["lib1"],
3704 }
3705`
3706
3707func TestStaticLibDepExport(t *testing.T) {
3708 ctx := testCc(t, staticLibAndroidBp)
3709
3710 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003711 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003712 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003713 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003714
3715 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003716 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003717 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3718 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003719 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003720}
3721
Jiyong Parkd08b6972017-09-26 10:50:54 +09003722var compilerFlagsTestCases = []struct {
3723 in string
3724 out bool
3725}{
3726 {
3727 in: "a",
3728 out: false,
3729 },
3730 {
3731 in: "-a",
3732 out: true,
3733 },
3734 {
3735 in: "-Ipath/to/something",
3736 out: false,
3737 },
3738 {
3739 in: "-isystempath/to/something",
3740 out: false,
3741 },
3742 {
3743 in: "--coverage",
3744 out: false,
3745 },
3746 {
3747 in: "-include a/b",
3748 out: true,
3749 },
3750 {
3751 in: "-include a/b c/d",
3752 out: false,
3753 },
3754 {
3755 in: "-DMACRO",
3756 out: true,
3757 },
3758 {
3759 in: "-DMAC RO",
3760 out: false,
3761 },
3762 {
3763 in: "-a -b",
3764 out: false,
3765 },
3766 {
3767 in: "-DMACRO=definition",
3768 out: true,
3769 },
3770 {
3771 in: "-DMACRO=defi nition",
3772 out: true, // TODO(jiyong): this should be false
3773 },
3774 {
3775 in: "-DMACRO(x)=x + 1",
3776 out: true,
3777 },
3778 {
3779 in: "-DMACRO=\"defi nition\"",
3780 out: true,
3781 },
3782}
3783
3784type mockContext struct {
3785 BaseModuleContext
3786 result bool
3787}
3788
3789func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3790 // CheckBadCompilerFlags calls this function when the flag should be rejected
3791 ctx.result = false
3792}
3793
3794func TestCompilerFlags(t *testing.T) {
3795 for _, testCase := range compilerFlagsTestCases {
3796 ctx := &mockContext{result: true}
3797 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3798 if ctx.result != testCase.out {
3799 t.Errorf("incorrect output:")
3800 t.Errorf(" input: %#v", testCase.in)
3801 t.Errorf(" expected: %#v", testCase.out)
3802 t.Errorf(" got: %#v", ctx.result)
3803 }
3804 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003805}
Jiyong Park374510b2018-03-19 18:23:01 +09003806
3807func TestVendorPublicLibraries(t *testing.T) {
3808 ctx := testCc(t, `
3809 cc_library_headers {
3810 name: "libvendorpublic_headers",
3811 export_include_dirs: ["my_include"],
3812 }
3813 vendor_public_library {
3814 name: "libvendorpublic",
3815 symbol_file: "",
3816 export_public_headers: ["libvendorpublic_headers"],
3817 }
3818 cc_library {
3819 name: "libvendorpublic",
3820 srcs: ["foo.c"],
3821 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003822 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003823 nocrt: true,
3824 }
3825
3826 cc_library {
3827 name: "libsystem",
3828 shared_libs: ["libvendorpublic"],
3829 vendor: false,
3830 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003831 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003832 nocrt: true,
3833 }
3834 cc_library {
3835 name: "libvendor",
3836 shared_libs: ["libvendorpublic"],
3837 vendor: true,
3838 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003839 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003840 nocrt: true,
3841 }
3842 `)
3843
Colin Cross7113d202019-11-20 16:39:12 -08003844 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003845 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003846
3847 // test if header search paths are correctly added
3848 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003849 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003850 cflags := cc.Args["cFlags"]
3851 if !strings.Contains(cflags, "-Imy_include") {
3852 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3853 }
3854
3855 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003856 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003857 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003858 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003859 if !strings.Contains(libflags, stubPaths[0].String()) {
3860 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3861 }
3862
3863 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003864 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003865 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003866 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003867 if !strings.Contains(libflags, stubPaths[0].String()) {
3868 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3869 }
3870
3871}
Jiyong Park37b25202018-07-11 10:49:27 +09003872
3873func TestRecovery(t *testing.T) {
3874 ctx := testCc(t, `
3875 cc_library_shared {
3876 name: "librecovery",
3877 recovery: true,
3878 }
3879 cc_library_shared {
3880 name: "librecovery32",
3881 recovery: true,
3882 compile_multilib:"32",
3883 }
Jiyong Park5baac542018-08-28 09:55:37 +09003884 cc_library_shared {
3885 name: "libHalInRecovery",
3886 recovery_available: true,
3887 vendor: true,
3888 }
Jiyong Park37b25202018-07-11 10:49:27 +09003889 `)
3890
3891 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003892 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003893 if len(variants) != 1 || !android.InList(arm64, variants) {
3894 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3895 }
3896
3897 variants = ctx.ModuleVariantsForTests("librecovery32")
3898 if android.InList(arm64, variants) {
3899 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3900 }
Jiyong Park5baac542018-08-28 09:55:37 +09003901
3902 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3903 if !recoveryModule.Platform() {
3904 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3905 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003906}
Jiyong Park5baac542018-08-28 09:55:37 +09003907
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003908func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3909 bp := `
3910 cc_prebuilt_test_library_shared {
3911 name: "test_lib",
3912 relative_install_path: "foo/bar/baz",
3913 srcs: ["srcpath/dontusethispath/baz.so"],
3914 }
3915
3916 cc_test {
3917 name: "main_test",
3918 data_libs: ["test_lib"],
3919 gtest: false,
3920 }
3921 `
3922
3923 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3924 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3925 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3926 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3927
3928 ctx := testCcWithConfig(t, config)
3929 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3930 testBinary := module.(*Module).linker.(*testBinary)
3931 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3932 if err != nil {
3933 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3934 }
3935 if len(outputFiles) != 1 {
3936 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3937 }
3938 if len(testBinary.dataPaths()) != 1 {
3939 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3940 }
3941
3942 outputPath := outputFiles[0].String()
3943
3944 if !strings.HasSuffix(outputPath, "/main_test") {
3945 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3946 }
3947 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
3948 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3949 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3950 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3951 }
3952}
3953
Jiyong Park7ed9de32018-10-15 22:25:07 +09003954func TestVersionedStubs(t *testing.T) {
3955 ctx := testCc(t, `
3956 cc_library_shared {
3957 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003958 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003959 stubs: {
3960 symbol_file: "foo.map.txt",
3961 versions: ["1", "2", "3"],
3962 },
3963 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003964
Jiyong Park7ed9de32018-10-15 22:25:07 +09003965 cc_library_shared {
3966 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003967 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003968 shared_libs: ["libFoo#1"],
3969 }`)
3970
3971 variants := ctx.ModuleVariantsForTests("libFoo")
3972 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003973 "android_arm64_armv8-a_shared",
3974 "android_arm64_armv8-a_shared_1",
3975 "android_arm64_armv8-a_shared_2",
3976 "android_arm64_armv8-a_shared_3",
3977 "android_arm_armv7-a-neon_shared",
3978 "android_arm_armv7-a-neon_shared_1",
3979 "android_arm_armv7-a-neon_shared_2",
3980 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003981 }
3982 variantsMismatch := false
3983 if len(variants) != len(expectedVariants) {
3984 variantsMismatch = true
3985 } else {
3986 for _, v := range expectedVariants {
3987 if !inList(v, variants) {
3988 variantsMismatch = false
3989 }
3990 }
3991 }
3992 if variantsMismatch {
3993 t.Errorf("variants of libFoo expected:\n")
3994 for _, v := range expectedVariants {
3995 t.Errorf("%q\n", v)
3996 }
3997 t.Errorf(", but got:\n")
3998 for _, v := range variants {
3999 t.Errorf("%q\n", v)
4000 }
4001 }
4002
Colin Cross7113d202019-11-20 16:39:12 -08004003 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09004004 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08004005 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09004006 if !strings.Contains(libFlags, libFoo1StubPath) {
4007 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
4008 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09004009
Colin Cross7113d202019-11-20 16:39:12 -08004010 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09004011 cFlags := libBarCompileRule.Args["cFlags"]
4012 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
4013 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
4014 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
4015 }
Jiyong Park37b25202018-07-11 10:49:27 +09004016}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004017
Jooyung Hanb04a4992020-03-13 18:57:35 +09004018func TestVersioningMacro(t *testing.T) {
4019 for _, tc := range []struct{ moduleName, expected string }{
4020 {"libc", "__LIBC_API__"},
4021 {"libfoo", "__LIBFOO_API__"},
4022 {"libfoo@1", "__LIBFOO_1_API__"},
4023 {"libfoo-v1", "__LIBFOO_V1_API__"},
4024 {"libfoo.v1", "__LIBFOO_V1_API__"},
4025 } {
4026 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
4027 }
4028}
4029
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004030func TestStaticExecutable(t *testing.T) {
4031 ctx := testCc(t, `
4032 cc_binary {
4033 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01004034 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004035 static_executable: true,
4036 }`)
4037
Colin Cross7113d202019-11-20 16:39:12 -08004038 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004039 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
4040 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07004041 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004042 for _, lib := range systemStaticLibs {
4043 if !strings.Contains(libFlags, lib) {
4044 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
4045 }
4046 }
4047 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
4048 for _, lib := range systemSharedLibs {
4049 if strings.Contains(libFlags, lib) {
4050 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
4051 }
4052 }
4053}
Jiyong Parke4bb9862019-02-01 00:31:10 +09004054
4055func TestStaticDepsOrderWithStubs(t *testing.T) {
4056 ctx := testCc(t, `
4057 cc_binary {
4058 name: "mybin",
4059 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07004060 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09004061 static_executable: true,
4062 stl: "none",
4063 }
4064
4065 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08004066 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09004067 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08004068 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09004069 stl: "none",
4070 }
4071
4072 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08004073 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09004074 srcs: ["foo.c"],
4075 stl: "none",
4076 stubs: {
4077 versions: ["1"],
4078 },
4079 }`)
4080
Colin Cross0de8a1e2020-09-18 14:15:30 -07004081 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
4082 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08004083 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09004084
4085 if !reflect.DeepEqual(actual, expected) {
4086 t.Errorf("staticDeps orderings were not propagated correctly"+
4087 "\nactual: %v"+
4088 "\nexpected: %v",
4089 actual,
4090 expected,
4091 )
4092 }
4093}
Jooyung Han38002912019-05-16 04:01:54 +09004094
Jooyung Hand48f3c32019-08-23 11:18:57 +09004095func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
4096 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
4097 cc_library {
4098 name: "libA",
4099 srcs: ["foo.c"],
4100 shared_libs: ["libB"],
4101 stl: "none",
4102 }
4103
4104 cc_library {
4105 name: "libB",
4106 srcs: ["foo.c"],
4107 enabled: false,
4108 stl: "none",
4109 }
4110 `)
4111}
4112
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004113// Simple smoke test for the cc_fuzz target that ensures the rule compiles
4114// correctly.
4115func TestFuzzTarget(t *testing.T) {
4116 ctx := testCc(t, `
4117 cc_fuzz {
4118 name: "fuzz_smoke_test",
4119 srcs: ["foo.c"],
4120 }`)
4121
Paul Duffin075c4172019-12-19 19:06:13 +00004122 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004123 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
4124}
4125
Jiyong Park29074592019-07-07 16:27:47 +09004126func TestAidl(t *testing.T) {
4127}
4128
Jooyung Han38002912019-05-16 04:01:54 +09004129func assertString(t *testing.T, got, expected string) {
4130 t.Helper()
4131 if got != expected {
4132 t.Errorf("expected %q got %q", expected, got)
4133 }
4134}
4135
4136func assertArrayString(t *testing.T, got, expected []string) {
4137 t.Helper()
4138 if len(got) != len(expected) {
4139 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
4140 return
4141 }
4142 for i := range got {
4143 if got[i] != expected[i] {
4144 t.Errorf("expected %d-th %q (%q) got %q (%q)",
4145 i, expected[i], expected, got[i], got)
4146 return
4147 }
4148 }
4149}
Colin Crosse1bb5d02019-09-24 14:55:04 -07004150
Jooyung Han0302a842019-10-30 18:43:49 +09004151func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
4152 t.Helper()
4153 assertArrayString(t, android.SortedStringKeys(m), expected)
4154}
4155
Colin Crosse1bb5d02019-09-24 14:55:04 -07004156func TestDefaults(t *testing.T) {
4157 ctx := testCc(t, `
4158 cc_defaults {
4159 name: "defaults",
4160 srcs: ["foo.c"],
4161 static: {
4162 srcs: ["bar.c"],
4163 },
4164 shared: {
4165 srcs: ["baz.c"],
4166 },
4167 }
4168
4169 cc_library_static {
4170 name: "libstatic",
4171 defaults: ["defaults"],
4172 }
4173
4174 cc_library_shared {
4175 name: "libshared",
4176 defaults: ["defaults"],
4177 }
4178
4179 cc_library {
4180 name: "libboth",
4181 defaults: ["defaults"],
4182 }
4183
4184 cc_binary {
4185 name: "binary",
4186 defaults: ["defaults"],
4187 }`)
4188
4189 pathsToBase := func(paths android.Paths) []string {
4190 var ret []string
4191 for _, p := range paths {
4192 ret = append(ret, p.Base())
4193 }
4194 return ret
4195 }
4196
Colin Cross7113d202019-11-20 16:39:12 -08004197 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004198 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4199 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
4200 }
Colin Cross7113d202019-11-20 16:39:12 -08004201 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004202 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4203 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
4204 }
Colin Cross7113d202019-11-20 16:39:12 -08004205 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004206 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
4207 t.Errorf("binary ld rule wanted %q, got %q", w, g)
4208 }
4209
Colin Cross7113d202019-11-20 16:39:12 -08004210 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004211 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4212 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
4213 }
Colin Cross7113d202019-11-20 16:39:12 -08004214 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004215 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4216 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
4217 }
4218}
Colin Crosseabaedd2020-02-06 17:01:55 -08004219
4220func TestProductVariableDefaults(t *testing.T) {
4221 bp := `
4222 cc_defaults {
4223 name: "libfoo_defaults",
4224 srcs: ["foo.c"],
4225 cppflags: ["-DFOO"],
4226 product_variables: {
4227 debuggable: {
4228 cppflags: ["-DBAR"],
4229 },
4230 },
4231 }
4232
4233 cc_library {
4234 name: "libfoo",
4235 defaults: ["libfoo_defaults"],
4236 }
4237 `
4238
4239 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4240 config.TestProductVariables.Debuggable = BoolPtr(true)
4241
Colin Crossae8600b2020-10-29 17:09:13 -07004242 ctx := CreateTestContext(config)
Colin Crosseabaedd2020-02-06 17:01:55 -08004243 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
4244 ctx.BottomUp("variable", android.VariableMutator).Parallel()
4245 })
Colin Crossae8600b2020-10-29 17:09:13 -07004246 ctx.Register()
Colin Crosseabaedd2020-02-06 17:01:55 -08004247
4248 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
4249 android.FailIfErrored(t, errs)
4250 _, errs = ctx.PrepareBuildActions(config)
4251 android.FailIfErrored(t, errs)
4252
4253 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
4254 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
4255 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
4256 }
4257}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004258
4259func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4260 t.Parallel()
4261 bp := `
4262 cc_library_static {
4263 name: "libfoo",
4264 srcs: ["foo.c"],
4265 whole_static_libs: ["libbar"],
4266 }
4267
4268 cc_library_static {
4269 name: "libbar",
4270 whole_static_libs: ["libmissing"],
4271 }
4272 `
4273
4274 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4275 config.TestProductVariables.Allow_missing_dependencies = BoolPtr(true)
4276
Colin Crossae8600b2020-10-29 17:09:13 -07004277 ctx := CreateTestContext(config)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004278 ctx.SetAllowMissingDependencies(true)
Colin Crossae8600b2020-10-29 17:09:13 -07004279 ctx.Register()
Colin Crosse4f6eba2020-09-22 18:11:25 -07004280
4281 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
4282 android.FailIfErrored(t, errs)
4283 _, errs = ctx.PrepareBuildActions(config)
4284 android.FailIfErrored(t, errs)
4285
4286 libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
4287 if g, w := libbar.Rule, android.ErrorRule; g != w {
4288 t.Fatalf("Expected libbar rule to be %q, got %q", w, g)
4289 }
4290
4291 if g, w := libbar.Args["error"], "missing dependencies: libmissing"; !strings.Contains(g, w) {
4292 t.Errorf("Expected libbar error to contain %q, was %q", w, g)
4293 }
4294
4295 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
4296 if g, w := libfoo.Inputs.Strings(), libbar.Output.String(); !android.InList(w, g) {
4297 t.Errorf("Expected libfoo.a to depend on %q, got %q", w, g)
4298 }
4299
4300}
Colin Crosse9fe2942020-11-10 18:12:15 -08004301
4302func TestInstallSharedLibs(t *testing.T) {
4303 bp := `
4304 cc_binary {
4305 name: "bin",
4306 host_supported: true,
4307 shared_libs: ["libshared"],
4308 runtime_libs: ["libruntime"],
4309 srcs: [":gen"],
4310 }
4311
4312 cc_library_shared {
4313 name: "libshared",
4314 host_supported: true,
4315 shared_libs: ["libtransitive"],
4316 }
4317
4318 cc_library_shared {
4319 name: "libtransitive",
4320 host_supported: true,
4321 }
4322
4323 cc_library_shared {
4324 name: "libruntime",
4325 host_supported: true,
4326 }
4327
4328 cc_binary_host {
4329 name: "tool",
4330 srcs: ["foo.cpp"],
4331 }
4332
4333 genrule {
4334 name: "gen",
4335 tools: ["tool"],
4336 out: ["gen.cpp"],
4337 cmd: "$(location tool) $(out)",
4338 }
4339 `
4340
4341 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4342 ctx := testCcWithConfig(t, config)
4343
4344 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4345 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4346 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4347 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4348 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4349
4350 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4351 t.Errorf("expected host bin dependency %q, got %q", w, g)
4352 }
4353
4354 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4355 t.Errorf("expected host bin dependency %q, got %q", w, g)
4356 }
4357
4358 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4359 t.Errorf("expected host bin dependency %q, got %q", w, g)
4360 }
4361
4362 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4363 t.Errorf("expected host bin dependency %q, got %q", w, g)
4364 }
4365
4366 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4367 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4368 }
4369
4370 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4371 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4372 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4373 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4374
4375 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4376 t.Errorf("expected device bin dependency %q, got %q", w, g)
4377 }
4378
4379 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4380 t.Errorf("expected device bin dependency %q, got %q", w, g)
4381 }
4382
4383 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4384 t.Errorf("expected device bin dependency %q, got %q", w, g)
4385 }
4386
4387 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4388 t.Errorf("expected device bin dependency %q, got %q", w, g)
4389 }
4390
4391 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4392 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4393 }
4394
4395}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004396
4397func TestStubsLibReexportsHeaders(t *testing.T) {
4398 ctx := testCc(t, `
4399 cc_library_shared {
4400 name: "libclient",
4401 srcs: ["foo.c"],
4402 shared_libs: ["libfoo#1"],
4403 }
4404
4405 cc_library_shared {
4406 name: "libfoo",
4407 srcs: ["foo.c"],
4408 shared_libs: ["libbar"],
4409 export_shared_lib_headers: ["libbar"],
4410 stubs: {
4411 symbol_file: "foo.map.txt",
4412 versions: ["1", "2", "3"],
4413 },
4414 }
4415
4416 cc_library_shared {
4417 name: "libbar",
4418 export_include_dirs: ["include/libbar"],
4419 srcs: ["foo.c"],
4420 }`)
4421
4422 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4423
4424 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4425 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4426 }
4427}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004428
4429func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
4430 ctx := testCc(t, `
4431 cc_library {
4432 name: "libfoo",
4433 srcs: ["a/Foo.aidl"],
4434 aidl: { flags: ["-Werror"], },
4435 }
4436 `)
4437
4438 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4439 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4440 aidlCommand := manifest.Commands[0].GetCommand()
4441 expectedAidlFlag := "-Werror"
4442 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4443 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4444 }
4445}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004446
4447func checkHasImplicitDep(t *testing.T, m android.TestingModule, name string) {
4448 implicits := m.Rule("ld").Implicits
4449 for _, lib := range implicits {
4450 if strings.Contains(lib.Rel(), name) {
4451 return
4452 }
4453 }
4454
4455 t.Errorf("%q is not found in implicit deps of module %q", name, m.Module().(*Module).Name())
4456}
4457
4458func checkDoesNotHaveImplicitDep(t *testing.T, m android.TestingModule, name string) {
4459 implicits := m.Rule("ld").Implicits
4460 for _, lib := range implicits {
4461 if strings.Contains(lib.Rel(), name) {
4462 t.Errorf("%q is found in implicit deps of module %q", name, m.Module().(*Module).Name())
4463 }
4464 }
4465}
4466
4467func TestSanitizeMemtagHeap(t *testing.T) {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08004468 rootBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004469 cc_library_static {
4470 name: "libstatic",
4471 sanitize: { memtag_heap: true },
4472 }
4473
4474 cc_library_shared {
4475 name: "libshared",
4476 sanitize: { memtag_heap: true },
4477 }
4478
4479 cc_library {
4480 name: "libboth",
4481 sanitize: { memtag_heap: true },
4482 }
4483
4484 cc_binary {
4485 name: "binary",
4486 shared_libs: [ "libshared" ],
4487 static_libs: [ "libstatic" ],
4488 }
4489
4490 cc_binary {
4491 name: "binary_true",
4492 sanitize: { memtag_heap: true },
4493 }
4494
4495 cc_binary {
4496 name: "binary_true_sync",
4497 sanitize: { memtag_heap: true, diag: { memtag_heap: true }, },
4498 }
4499
4500 cc_binary {
4501 name: "binary_false",
4502 sanitize: { memtag_heap: false },
4503 }
4504
4505 cc_test {
4506 name: "test",
4507 gtest: false,
4508 }
4509
4510 cc_test {
4511 name: "test_true",
4512 gtest: false,
4513 sanitize: { memtag_heap: true },
4514 }
4515
4516 cc_test {
4517 name: "test_false",
4518 gtest: false,
4519 sanitize: { memtag_heap: false },
4520 }
4521
4522 cc_test {
4523 name: "test_true_async",
4524 gtest: false,
4525 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
4526 }
4527
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08004528 `
4529
4530 subdirAsyncBp := `
4531 cc_binary {
4532 name: "binary_async",
4533 }
4534 `
4535
4536 subdirSyncBp := `
4537 cc_binary {
4538 name: "binary_sync",
4539 }
4540 `
4541
4542 mockFS := map[string][]byte{
4543 "subdir_async/Android.bp": []byte(subdirAsyncBp),
4544 "subdir_sync/Android.bp": []byte(subdirSyncBp),
4545 }
4546
4547 config := TestConfig(buildDir, android.Android, nil, rootBp, mockFS)
4548 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
4549 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
4550 config.TestProductVariables.MemtagHeapAsyncIncludePaths = []string{"subdir_async"}
4551 config.TestProductVariables.MemtagHeapSyncIncludePaths = []string{"subdir_sync"}
4552 ctx := CreateTestContext(config)
4553 ctx.Register()
4554
4555 _, errs := ctx.ParseFileList(".", []string{"Android.bp", "subdir_sync/Android.bp", "subdir_async/Android.bp"})
4556 android.FailIfErrored(t, errs)
4557 _, errs = ctx.PrepareBuildActions(config)
4558 android.FailIfErrored(t, errs)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004559
4560 variant := "android_arm64_armv8-a"
4561 note_async := "note_memtag_heap_async"
4562 note_sync := "note_memtag_heap_sync"
4563 note_any := "note_memtag_"
4564
4565 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared"), note_any)
4566 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared"), note_any)
4567
4568 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("binary", variant), note_any)
4569 checkHasImplicitDep(t, ctx.ModuleForTests("binary_true", variant), note_async)
4570 checkHasImplicitDep(t, ctx.ModuleForTests("binary_true_sync", variant), note_sync)
4571 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("binary_false", variant), note_any)
4572
4573 checkHasImplicitDep(t, ctx.ModuleForTests("test", variant), note_sync)
4574 checkHasImplicitDep(t, ctx.ModuleForTests("test_true", variant), note_async)
4575 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("test_false", variant), note_any)
4576 checkHasImplicitDep(t, ctx.ModuleForTests("test_true_async", variant), note_async)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08004577
4578 checkHasImplicitDep(t, ctx.ModuleForTests("binary_async", variant), note_async)
4579 checkHasImplicitDep(t, ctx.ModuleForTests("binary_sync", variant), note_sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004580}