blob: f4228391d1925d278e683e96d71ef5d1c073c853 [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()
Colin Cross78212242021-01-06 14:51:30 -0800319 got := ctx.ModuleForTests(module, "").Module().(*vndkLibrariesTxt).fileNames
320 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900321}
322
Logan Chienf3511742017-10-31 18:04:35 +0800323func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800324 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800325 cc_library {
326 name: "libvndk",
327 vendor_available: true,
328 vndk: {
329 enabled: true,
330 },
331 nocrt: true,
332 }
333
334 cc_library {
335 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900336 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800337 vndk: {
338 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900339 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800340 },
341 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900342 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800343 }
344
345 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900346 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800347 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900348 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800349 vndk: {
350 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900351 },
352 nocrt: true,
353 target: {
354 vendor: {
355 cflags: ["-DTEST"],
356 },
357 product: {
358 cflags: ["-DTEST"],
359 },
360 },
361 }
362
363 cc_library {
364 name: "libvndk_sp",
365 vendor_available: true,
366 vndk: {
367 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800368 support_system_process: true,
369 },
370 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900371 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800372 }
373
374 cc_library {
375 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900376 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800377 vndk: {
378 enabled: true,
379 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900380 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800381 },
382 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900383 target: {
384 vendor: {
385 suffix: "-x",
386 },
387 },
Logan Chienf3511742017-10-31 18:04:35 +0800388 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900389
390 cc_library {
391 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900392 vendor_available: true,
393 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900394 vndk: {
395 enabled: true,
396 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900397 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900398 },
399 nocrt: true,
400 target: {
401 vendor: {
402 suffix: "-x",
403 },
404 product: {
405 suffix: "-x",
406 },
407 },
408 }
409
Colin Crosse4e44bc2020-12-28 13:50:21 -0800410 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900411 name: "llndk.libraries.txt",
412 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800413 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900414 name: "vndkcore.libraries.txt",
415 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800416 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900417 name: "vndksp.libraries.txt",
418 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800419 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900420 name: "vndkprivate.libraries.txt",
421 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800422 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900423 name: "vndkproduct.libraries.txt",
424 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800425 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900426 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800427 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900428 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800429 `
430
431 config := TestConfig(buildDir, android.Android, nil, bp, nil)
432 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900433 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Colin Cross98be1bb2019-12-13 20:41:13 -0800434 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
435
436 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800437
Jooyung Han261e1582020-10-20 18:54:21 +0900438 // subdir == "" because VNDK libs are not supposed to be installed separately.
439 // They are installed as part of VNDK APEX instead.
440 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
441 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900442 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900443 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
444 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900445 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900446
Justin Yun6977e8a2020-10-29 18:24:11 +0900447 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
448 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900449
Inseob Kim1f086e22019-05-09 13:29:15 +0900450 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900451 snapshotDir := "vndk-snapshot"
452 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
453
454 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
455 "arm64", "armv8-a"))
456 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
457 "arm", "armv7-a-neon"))
458
459 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
460 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
461 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
462 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
463
Colin Crossfb0c16e2019-11-20 17:12:35 -0800464 variant := "android_vendor.VER_arm64_armv8-a_shared"
465 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900466
Inseob Kim7f283f42020-06-01 21:53:49 +0900467 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
468
469 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
470 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900471 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
472 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900473 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
474 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900475
Jooyung Han39edb6c2019-11-06 16:53:07 +0900476 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900477 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
478 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
479 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
480 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900481 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900482
Jooyung Han097087b2019-10-22 19:32:18 +0900483 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
484 "LLNDK: libc.so",
485 "LLNDK: libdl.so",
486 "LLNDK: libft2.so",
487 "LLNDK: libm.so",
488 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900489 "VNDK-SP: libvndk_sp-x.so",
490 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900491 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900492 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900493 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900494 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900495 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900496 "VNDK-private: libvndk-private.so",
497 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900498 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900499 "VNDK-product: libc++.so",
500 "VNDK-product: libvndk_product.so",
501 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900502 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900503 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900504 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
505 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
506 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 +0900507 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 +0900508 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
509}
510
Yo Chiangbba545e2020-06-09 16:15:37 +0800511func TestVndkWithHostSupported(t *testing.T) {
512 ctx := testCc(t, `
513 cc_library {
514 name: "libvndk_host_supported",
515 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900516 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800517 vndk: {
518 enabled: true,
519 },
520 host_supported: true,
521 }
522
523 cc_library {
524 name: "libvndk_host_supported_but_disabled_on_device",
525 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900526 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800527 vndk: {
528 enabled: true,
529 },
530 host_supported: true,
531 enabled: false,
532 target: {
533 host: {
534 enabled: true,
535 }
536 }
537 }
538
Colin Crosse4e44bc2020-12-28 13:50:21 -0800539 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800540 name: "vndkcore.libraries.txt",
541 }
542 `)
543
544 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
545}
546
Jooyung Han2216fb12019-11-06 16:46:15 +0900547func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800548 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800549 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900550 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800551 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800552 }`
553 config := TestConfig(buildDir, android.Android, nil, bp, nil)
554 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
555 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
556 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900557
558 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900559 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900560 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900561}
562
563func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800564 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900565 cc_library {
566 name: "libvndk",
567 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900568 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900569 vndk: {
570 enabled: true,
571 },
572 nocrt: true,
573 }
574
575 cc_library {
576 name: "libvndk_sp",
577 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900578 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900579 vndk: {
580 enabled: true,
581 support_system_process: true,
582 },
583 nocrt: true,
584 }
585
586 cc_library {
587 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900588 vendor_available: true,
589 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900590 vndk: {
591 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900592 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900593 },
594 nocrt: true,
595 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900596
Colin Crosse4e44bc2020-12-28 13:50:21 -0800597 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900598 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800599 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900600 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800601 `
602
603 config := TestConfig(buildDir, android.Android, nil, bp, nil)
604 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
605 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
606 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
607
608 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
609
610 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900611
Jooyung Han2216fb12019-11-06 16:46:15 +0900612 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900613}
614
Chris Parsons79d66a52020-06-05 17:26:16 -0400615func TestDataLibs(t *testing.T) {
616 bp := `
617 cc_test_library {
618 name: "test_lib",
619 srcs: ["test_lib.cpp"],
620 gtest: false,
621 }
622
623 cc_test {
624 name: "main_test",
625 data_libs: ["test_lib"],
626 gtest: false,
627 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400628 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400629
630 config := TestConfig(buildDir, android.Android, nil, bp, nil)
631 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
632 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
633 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
634
635 ctx := testCcWithConfig(t, config)
636 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
637 testBinary := module.(*Module).linker.(*testBinary)
638 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
639 if err != nil {
640 t.Errorf("Expected cc_test to produce output files, error: %s", err)
641 return
642 }
643 if len(outputFiles) != 1 {
644 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
645 return
646 }
647 if len(testBinary.dataPaths()) != 1 {
648 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
649 return
650 }
651
652 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400653 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400654
655 if !strings.HasSuffix(outputPath, "/main_test") {
656 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
657 return
658 }
659 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
660 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
661 return
662 }
663}
664
Chris Parsons216e10a2020-07-09 17:12:52 -0400665func TestDataLibsRelativeInstallPath(t *testing.T) {
666 bp := `
667 cc_test_library {
668 name: "test_lib",
669 srcs: ["test_lib.cpp"],
670 relative_install_path: "foo/bar/baz",
671 gtest: false,
672 }
673
674 cc_test {
675 name: "main_test",
676 data_libs: ["test_lib"],
677 gtest: false,
678 }
679 `
680
681 config := TestConfig(buildDir, android.Android, nil, bp, nil)
682 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
683 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
684 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
685
686 ctx := testCcWithConfig(t, config)
687 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
688 testBinary := module.(*Module).linker.(*testBinary)
689 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
690 if err != nil {
691 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
692 }
693 if len(outputFiles) != 1 {
694 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
695 }
696 if len(testBinary.dataPaths()) != 1 {
697 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
698 }
699
700 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400701
702 if !strings.HasSuffix(outputPath, "/main_test") {
703 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
704 }
705 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
706 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
707 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400708 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400709 }
710}
711
Jooyung Han0302a842019-10-30 18:43:49 +0900712func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900713 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900714 cc_library {
715 name: "libvndk",
716 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900717 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900718 vndk: {
719 enabled: true,
720 },
721 nocrt: true,
722 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900723 cc_library {
724 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900725 vendor_available: true,
726 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900727 vndk: {
728 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900729 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900730 },
731 nocrt: true,
732 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800733
734 cc_library {
735 name: "libllndk",
736 llndk_stubs: "libllndk.llndk",
737 }
738
739 llndk_library {
740 name: "libllndk.llndk",
741 symbol_file: "",
742 export_llndk_headers: ["libllndk_headers"],
743 }
744
745 llndk_headers {
746 name: "libllndk_headers",
747 export_include_dirs: ["include"],
748 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900749 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900750
751 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
752 "LLNDK: libc.so",
753 "LLNDK: libdl.so",
754 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800755 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900756 "LLNDK: libm.so",
757 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900758 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900759 "VNDK-core: libvndk.so",
760 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900761 "VNDK-private: libvndk-private.so",
762 "VNDK-product: libc++.so",
763 "VNDK-product: libvndk-private.so",
764 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900765 })
Logan Chienf3511742017-10-31 18:04:35 +0800766}
767
Justin Yun63e9ec72020-10-29 16:49:43 +0900768func TestVndkModuleError(t *testing.T) {
769 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900770 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900771 cc_library {
772 name: "libvndk",
773 vndk: {
774 enabled: true,
775 },
776 nocrt: true,
777 }
778 `)
779
Justin Yunc0d8c492021-01-07 17:45:31 +0900780 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900781 cc_library {
782 name: "libvndk",
783 product_available: true,
784 vndk: {
785 enabled: true,
786 },
787 nocrt: true,
788 }
789 `)
790
Justin Yun6977e8a2020-10-29 18:24:11 +0900791 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
792 cc_library {
793 name: "libvndkprop",
794 vendor_available: true,
795 product_available: true,
796 vndk: {
797 enabled: true,
798 },
799 nocrt: true,
800 target: {
801 vendor: {
802 cflags: ["-DTEST",],
803 },
804 },
805 }
806 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900807}
808
Logan Chiend3c59a22018-03-29 14:08:15 +0800809func TestVndkDepError(t *testing.T) {
810 // Check whether an error is emitted when a VNDK lib depends on a system lib.
811 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
812 cc_library {
813 name: "libvndk",
814 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900815 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800816 vndk: {
817 enabled: true,
818 },
819 shared_libs: ["libfwk"], // Cause error
820 nocrt: true,
821 }
822
823 cc_library {
824 name: "libfwk",
825 nocrt: true,
826 }
827 `)
828
829 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
830 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
831 cc_library {
832 name: "libvndk",
833 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900834 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800835 vndk: {
836 enabled: true,
837 },
838 shared_libs: ["libvendor"], // Cause error
839 nocrt: true,
840 }
841
842 cc_library {
843 name: "libvendor",
844 vendor: true,
845 nocrt: true,
846 }
847 `)
848
849 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
850 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
851 cc_library {
852 name: "libvndk_sp",
853 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900854 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800855 vndk: {
856 enabled: true,
857 support_system_process: true,
858 },
859 shared_libs: ["libfwk"], // Cause error
860 nocrt: true,
861 }
862
863 cc_library {
864 name: "libfwk",
865 nocrt: true,
866 }
867 `)
868
869 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
870 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
871 cc_library {
872 name: "libvndk_sp",
873 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900874 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800875 vndk: {
876 enabled: true,
877 support_system_process: true,
878 },
879 shared_libs: ["libvendor"], // Cause error
880 nocrt: true,
881 }
882
883 cc_library {
884 name: "libvendor",
885 vendor: true,
886 nocrt: true,
887 }
888 `)
889
890 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
891 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
892 cc_library {
893 name: "libvndk_sp",
894 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900895 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800896 vndk: {
897 enabled: true,
898 support_system_process: true,
899 },
900 shared_libs: ["libvndk"], // Cause error
901 nocrt: true,
902 }
903
904 cc_library {
905 name: "libvndk",
906 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900907 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800908 vndk: {
909 enabled: true,
910 },
911 nocrt: true,
912 }
913 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900914
915 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
916 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
917 cc_library {
918 name: "libvndk",
919 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900920 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900921 vndk: {
922 enabled: true,
923 },
924 shared_libs: ["libnonvndk"],
925 nocrt: true,
926 }
927
928 cc_library {
929 name: "libnonvndk",
930 vendor_available: true,
931 nocrt: true,
932 }
933 `)
934
935 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
936 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
937 cc_library {
938 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900939 vendor_available: true,
940 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900941 vndk: {
942 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900943 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900944 },
945 shared_libs: ["libnonvndk"],
946 nocrt: true,
947 }
948
949 cc_library {
950 name: "libnonvndk",
951 vendor_available: true,
952 nocrt: true,
953 }
954 `)
955
956 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
957 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
958 cc_library {
959 name: "libvndksp",
960 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900961 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900962 vndk: {
963 enabled: true,
964 support_system_process: true,
965 },
966 shared_libs: ["libnonvndk"],
967 nocrt: true,
968 }
969
970 cc_library {
971 name: "libnonvndk",
972 vendor_available: true,
973 nocrt: true,
974 }
975 `)
976
977 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
978 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
979 cc_library {
980 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900981 vendor_available: true,
982 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900983 vndk: {
984 enabled: true,
985 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900986 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900987 },
988 shared_libs: ["libnonvndk"],
989 nocrt: true,
990 }
991
992 cc_library {
993 name: "libnonvndk",
994 vendor_available: true,
995 nocrt: true,
996 }
997 `)
998}
999
1000func TestDoubleLoadbleDep(t *testing.T) {
1001 // okay to link : LLNDK -> double_loadable VNDK
1002 testCc(t, `
1003 cc_library {
1004 name: "libllndk",
1005 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001006 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001007 }
1008
1009 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001010 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001011 symbol_file: "",
1012 }
1013
1014 cc_library {
1015 name: "libdoubleloadable",
1016 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001017 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001018 vndk: {
1019 enabled: true,
1020 },
1021 double_loadable: true,
1022 }
1023 `)
1024 // okay to link : LLNDK -> VNDK-SP
1025 testCc(t, `
1026 cc_library {
1027 name: "libllndk",
1028 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -07001029 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001030 }
1031
1032 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001033 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001034 symbol_file: "",
1035 }
1036
1037 cc_library {
1038 name: "libvndksp",
1039 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001040 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001041 vndk: {
1042 enabled: true,
1043 support_system_process: true,
1044 },
1045 }
1046 `)
1047 // okay to link : double_loadable -> double_loadable
1048 testCc(t, `
1049 cc_library {
1050 name: "libdoubleloadable1",
1051 shared_libs: ["libdoubleloadable2"],
1052 vendor_available: true,
1053 double_loadable: true,
1054 }
1055
1056 cc_library {
1057 name: "libdoubleloadable2",
1058 vendor_available: true,
1059 double_loadable: true,
1060 }
1061 `)
1062 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1063 testCc(t, `
1064 cc_library {
1065 name: "libdoubleloadable",
1066 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001067 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001068 vndk: {
1069 enabled: true,
1070 },
1071 double_loadable: true,
1072 shared_libs: ["libnondoubleloadable"],
1073 }
1074
1075 cc_library {
1076 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001077 vendor_available: true,
1078 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001079 vndk: {
1080 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001081 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001082 },
1083 double_loadable: true,
1084 }
1085 `)
1086 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1087 testCc(t, `
1088 cc_library {
1089 name: "libllndk",
1090 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001091 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001092 }
1093
1094 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001095 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001096 symbol_file: "",
1097 }
1098
1099 cc_library {
1100 name: "libcoreonly",
1101 shared_libs: ["libvendoravailable"],
1102 }
1103
1104 // indirect dependency of LLNDK
1105 cc_library {
1106 name: "libvendoravailable",
1107 vendor_available: true,
1108 double_loadable: true,
1109 }
1110 `)
1111}
1112
Inseob Kim5f58ff72020-09-07 19:53:31 +09001113func TestVendorSnapshotCapture(t *testing.T) {
Inseob Kim8471cda2019-11-15 09:59:12 +09001114 bp := `
1115 cc_library {
1116 name: "libvndk",
1117 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001118 product_available: true,
Inseob Kim8471cda2019-11-15 09:59:12 +09001119 vndk: {
1120 enabled: true,
1121 },
1122 nocrt: true,
1123 }
1124
1125 cc_library {
1126 name: "libvendor",
1127 vendor: true,
1128 nocrt: true,
1129 }
1130
1131 cc_library {
1132 name: "libvendor_available",
1133 vendor_available: true,
1134 nocrt: true,
1135 }
1136
1137 cc_library_headers {
1138 name: "libvendor_headers",
1139 vendor_available: true,
1140 nocrt: true,
1141 }
1142
1143 cc_binary {
1144 name: "vendor_bin",
1145 vendor: true,
1146 nocrt: true,
1147 }
1148
1149 cc_binary {
1150 name: "vendor_available_bin",
1151 vendor_available: true,
1152 nocrt: true,
1153 }
Inseob Kim7f283f42020-06-01 21:53:49 +09001154
1155 toolchain_library {
1156 name: "libb",
1157 vendor_available: true,
1158 src: "libb.a",
1159 }
Inseob Kim1042d292020-06-01 23:23:05 +09001160
1161 cc_object {
1162 name: "obj",
1163 vendor_available: true,
1164 }
Colin Cross127bb8b2020-12-16 16:46:01 -08001165
1166 cc_library {
1167 name: "libllndk",
1168 llndk_stubs: "libllndk.llndk",
1169 }
1170
1171 llndk_library {
1172 name: "libllndk.llndk",
1173 symbol_file: "",
1174 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001175`
1176 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1177 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1178 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1179 ctx := testCcWithConfig(t, config)
1180
1181 // Check Vendor snapshot output.
1182
1183 snapshotDir := "vendor-snapshot"
1184 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +09001185 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1186
1187 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +09001188
1189 for _, arch := range [][]string{
1190 []string{"arm64", "armv8-a"},
1191 []string{"arm", "armv7-a-neon"},
1192 } {
1193 archType := arch[0]
1194 archVariant := arch[1]
1195 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1196
1197 // For shared libraries, only non-VNDK vendor_available modules are captured
1198 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1199 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +09001200 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1201 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
1202 jsonFiles = append(jsonFiles,
1203 filepath.Join(sharedDir, "libvendor.so.json"),
1204 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001205
Colin Cross127bb8b2020-12-16 16:46:01 -08001206 // LLNDK modules are not captured
1207 checkSnapshotExclude(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", sharedDir, sharedVariant)
1208
Inseob Kim8471cda2019-11-15 09:59:12 +09001209 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
Inseob Kimc42f2f22020-07-29 20:32:10 +09001210 // Also cfi variants are captured, except for prebuilts like toolchain_library
Inseob Kim8471cda2019-11-15 09:59:12 +09001211 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001212 staticCfiVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static_cfi", archType, archVariant)
Inseob Kim8471cda2019-11-15 09:59:12 +09001213 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +09001214 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1215 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001216 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001217 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001218 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001219 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001220 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001221 jsonFiles = append(jsonFiles,
1222 filepath.Join(staticDir, "libb.a.json"),
1223 filepath.Join(staticDir, "libvndk.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001224 filepath.Join(staticDir, "libvndk.cfi.a.json"),
Inseob Kim7f283f42020-06-01 21:53:49 +09001225 filepath.Join(staticDir, "libvendor.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001226 filepath.Join(staticDir, "libvendor.cfi.a.json"),
1227 filepath.Join(staticDir, "libvendor_available.a.json"),
1228 filepath.Join(staticDir, "libvendor_available.cfi.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001229
Inseob Kim7f283f42020-06-01 21:53:49 +09001230 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +09001231 if archType == "arm64" {
1232 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1233 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +09001234 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
1235 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
1236 jsonFiles = append(jsonFiles,
1237 filepath.Join(binaryDir, "vendor_bin.json"),
1238 filepath.Join(binaryDir, "vendor_available_bin.json"))
1239 }
1240
1241 // For header libraries, all vendor:true and vendor_available modules are captured.
1242 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1243 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +09001244
1245 // For object modules, all vendor:true and vendor_available modules are captured.
1246 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1247 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1248 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1249 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001250 }
1251
1252 for _, jsonFile := range jsonFiles {
1253 // verify all json files exist
1254 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1255 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001256 }
1257 }
Inseob Kime9aec6a2021-01-05 20:03:22 +09001258
1259 // fake snapshot should have all outputs in the normal snapshot.
1260 fakeSnapshotSingleton := ctx.SingletonForTests("vendor-fake-snapshot")
1261 for _, output := range snapshotSingleton.AllOutputs() {
1262 fakeOutput := strings.Replace(output, "/vendor-snapshot/", "/fake/vendor-snapshot/", 1)
1263 if fakeSnapshotSingleton.MaybeOutput(fakeOutput).Rule == nil {
1264 t.Errorf("%q expected but not found", fakeOutput)
1265 }
1266 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001267}
1268
Inseob Kim5f58ff72020-09-07 19:53:31 +09001269func TestVendorSnapshotUse(t *testing.T) {
1270 frameworkBp := `
1271 cc_library {
1272 name: "libvndk",
1273 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001274 product_available: true,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001275 vndk: {
1276 enabled: true,
1277 },
1278 nocrt: true,
1279 compile_multilib: "64",
1280 }
1281
1282 cc_library {
1283 name: "libvendor",
1284 vendor: true,
1285 nocrt: true,
1286 no_libcrt: true,
1287 stl: "none",
1288 system_shared_libs: [],
1289 compile_multilib: "64",
1290 }
1291
1292 cc_binary {
1293 name: "bin",
1294 vendor: true,
1295 nocrt: true,
1296 no_libcrt: true,
1297 stl: "none",
1298 system_shared_libs: [],
1299 compile_multilib: "64",
1300 }
1301`
1302
1303 vndkBp := `
1304 vndk_prebuilt_shared {
1305 name: "libvndk",
1306 version: "BOARD",
1307 target_arch: "arm64",
1308 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001309 product_available: true,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001310 vndk: {
1311 enabled: true,
1312 },
1313 arch: {
1314 arm64: {
1315 srcs: ["libvndk.so"],
Inseob Kim67be7322020-10-19 10:15:28 +09001316 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001317 },
1318 },
1319 }
1320`
1321
1322 vendorProprietaryBp := `
1323 cc_library {
1324 name: "libvendor_without_snapshot",
1325 vendor: true,
1326 nocrt: true,
1327 no_libcrt: true,
1328 stl: "none",
1329 system_shared_libs: [],
1330 compile_multilib: "64",
1331 }
1332
1333 cc_library_shared {
1334 name: "libclient",
1335 vendor: true,
1336 nocrt: true,
1337 no_libcrt: true,
1338 stl: "none",
1339 system_shared_libs: [],
1340 shared_libs: ["libvndk"],
1341 static_libs: ["libvendor", "libvendor_without_snapshot"],
1342 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001343 srcs: ["client.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001344 }
1345
1346 cc_binary {
1347 name: "bin_without_snapshot",
1348 vendor: true,
1349 nocrt: true,
1350 no_libcrt: true,
1351 stl: "none",
1352 system_shared_libs: [],
1353 static_libs: ["libvndk"],
1354 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001355 srcs: ["bin.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001356 }
1357
1358 vendor_snapshot_static {
1359 name: "libvndk",
1360 version: "BOARD",
1361 target_arch: "arm64",
1362 vendor: true,
1363 arch: {
1364 arm64: {
1365 src: "libvndk.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001366 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001367 },
1368 },
1369 }
1370
1371 vendor_snapshot_shared {
1372 name: "libvendor",
1373 version: "BOARD",
1374 target_arch: "arm64",
1375 vendor: true,
1376 arch: {
1377 arm64: {
1378 src: "libvendor.so",
Inseob Kim67be7322020-10-19 10:15:28 +09001379 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001380 },
1381 },
1382 }
1383
1384 vendor_snapshot_static {
1385 name: "libvendor",
1386 version: "BOARD",
1387 target_arch: "arm64",
1388 vendor: true,
1389 arch: {
1390 arm64: {
1391 src: "libvendor.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001392 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001393 },
1394 },
1395 }
1396
1397 vendor_snapshot_binary {
1398 name: "bin",
1399 version: "BOARD",
1400 target_arch: "arm64",
1401 vendor: true,
1402 arch: {
1403 arm64: {
1404 src: "bin",
1405 },
1406 },
1407 }
1408`
1409 depsBp := GatherRequiredDepsForTest(android.Android)
1410
1411 mockFS := map[string][]byte{
Inseob Kim67be7322020-10-19 10:15:28 +09001412 "deps/Android.bp": []byte(depsBp),
1413 "framework/Android.bp": []byte(frameworkBp),
1414 "vendor/Android.bp": []byte(vendorProprietaryBp),
1415 "vendor/bin": nil,
1416 "vendor/bin.cpp": nil,
1417 "vendor/client.cpp": nil,
1418 "vendor/include/libvndk/a.h": nil,
1419 "vendor/include/libvendor/b.h": nil,
1420 "vendor/libvndk.a": nil,
1421 "vendor/libvendor.a": nil,
1422 "vendor/libvendor.so": nil,
1423 "vndk/Android.bp": []byte(vndkBp),
1424 "vndk/include/libvndk/a.h": nil,
1425 "vndk/libvndk.so": nil,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001426 }
1427
1428 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1429 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1430 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001431 ctx := CreateTestContext(config)
1432 ctx.Register()
Inseob Kim5f58ff72020-09-07 19:53:31 +09001433
1434 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "vendor/Android.bp", "vndk/Android.bp"})
1435 android.FailIfErrored(t, errs)
1436 _, errs = ctx.PrepareBuildActions(config)
1437 android.FailIfErrored(t, errs)
1438
1439 sharedVariant := "android_vendor.BOARD_arm64_armv8-a_shared"
1440 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1441 binaryVariant := "android_vendor.BOARD_arm64_armv8-a"
1442
1443 // libclient uses libvndk.vndk.BOARD.arm64, libvendor.vendor_static.BOARD.arm64, libvendor_without_snapshot
Inseob Kim67be7322020-10-19 10:15:28 +09001444 libclientCcFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("cc").Args["cFlags"]
1445 for _, includeFlags := range []string{
1446 "-Ivndk/include/libvndk", // libvndk
1447 "-Ivendor/include/libvendor", // libvendor
1448 } {
1449 if !strings.Contains(libclientCcFlags, includeFlags) {
1450 t.Errorf("flags for libclient must contain %#v, but was %#v.",
1451 includeFlags, libclientCcFlags)
1452 }
1453 }
Inseob Kim5f58ff72020-09-07 19:53:31 +09001454
Inseob Kim67be7322020-10-19 10:15:28 +09001455 libclientLdFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001456 for _, input := range [][]string{
1457 []string{sharedVariant, "libvndk.vndk.BOARD.arm64"},
1458 []string{staticVariant, "libvendor.vendor_static.BOARD.arm64"},
1459 []string{staticVariant, "libvendor_without_snapshot"},
1460 } {
1461 outputPaths := getOutputPaths(ctx, input[0] /* variant */, []string{input[1]} /* module name */)
Inseob Kim67be7322020-10-19 10:15:28 +09001462 if !strings.Contains(libclientLdFlags, outputPaths[0].String()) {
1463 t.Errorf("libflags for libclient must contain %#v, but was %#v", outputPaths[0], libclientLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001464 }
1465 }
1466
1467 // bin_without_snapshot uses libvndk.vendor_static.BOARD.arm64
Inseob Kim67be7322020-10-19 10:15:28 +09001468 binWithoutSnapshotCcFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("cc").Args["cFlags"]
1469 if !strings.Contains(binWithoutSnapshotCcFlags, "-Ivendor/include/libvndk") {
1470 t.Errorf("flags for bin_without_snapshot must contain %#v, but was %#v.",
1471 "-Ivendor/include/libvndk", binWithoutSnapshotCcFlags)
1472 }
1473
1474 binWithoutSnapshotLdFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001475 libVndkStaticOutputPaths := getOutputPaths(ctx, staticVariant, []string{"libvndk.vendor_static.BOARD.arm64"})
Inseob Kim67be7322020-10-19 10:15:28 +09001476 if !strings.Contains(binWithoutSnapshotLdFlags, libVndkStaticOutputPaths[0].String()) {
Inseob Kim5f58ff72020-09-07 19:53:31 +09001477 t.Errorf("libflags for bin_without_snapshot must contain %#v, but was %#v",
Inseob Kim67be7322020-10-19 10:15:28 +09001478 libVndkStaticOutputPaths[0], binWithoutSnapshotLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001479 }
1480
1481 // libvendor.so is installed by libvendor.vendor_shared.BOARD.arm64
1482 ctx.ModuleForTests("libvendor.vendor_shared.BOARD.arm64", sharedVariant).Output("libvendor.so")
1483
1484 // libvendor_without_snapshot.so is installed by libvendor_without_snapshot
1485 ctx.ModuleForTests("libvendor_without_snapshot", sharedVariant).Output("libvendor_without_snapshot.so")
1486
1487 // bin is installed by bin.vendor_binary.BOARD.arm64
1488 ctx.ModuleForTests("bin.vendor_binary.BOARD.arm64", binaryVariant).Output("bin")
1489
1490 // bin_without_snapshot is installed by bin_without_snapshot
1491 ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Output("bin_without_snapshot")
1492
1493 // libvendor and bin don't have vendor.BOARD variant
1494 libvendorVariants := ctx.ModuleVariantsForTests("libvendor")
1495 if inList(sharedVariant, libvendorVariants) {
1496 t.Errorf("libvendor must not have variant %#v, but it does", sharedVariant)
1497 }
1498
1499 binVariants := ctx.ModuleVariantsForTests("bin")
1500 if inList(binaryVariant, binVariants) {
1501 t.Errorf("bin must not have variant %#v, but it does", sharedVariant)
1502 }
1503}
1504
Inseob Kimc42f2f22020-07-29 20:32:10 +09001505func TestVendorSnapshotSanitizer(t *testing.T) {
1506 bp := `
1507 vendor_snapshot_static {
1508 name: "libsnapshot",
1509 vendor: true,
1510 target_arch: "arm64",
1511 version: "BOARD",
1512 arch: {
1513 arm64: {
1514 src: "libsnapshot.a",
1515 cfi: {
1516 src: "libsnapshot.cfi.a",
1517 }
1518 },
1519 },
1520 }
1521`
1522 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1523 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1524 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1525 ctx := testCcWithConfig(t, config)
1526
1527 // Check non-cfi and cfi variant.
1528 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1529 staticCfiVariant := "android_vendor.BOARD_arm64_armv8-a_static_cfi"
1530
1531 staticModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticVariant).Module().(*Module)
1532 assertString(t, staticModule.outputFile.Path().Base(), "libsnapshot.a")
1533
1534 staticCfiModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticCfiVariant).Module().(*Module)
1535 assertString(t, staticCfiModule.outputFile.Path().Base(), "libsnapshot.cfi.a")
1536}
1537
Bill Peckham945441c2020-08-31 16:07:58 -07001538func assertExcludeFromVendorSnapshotIs(t *testing.T, c *Module, expected bool) {
1539 t.Helper()
1540 if c.ExcludeFromVendorSnapshot() != expected {
1541 t.Errorf("expected %q ExcludeFromVendorSnapshot to be %t", c.String(), expected)
1542 }
1543}
1544
Jose Galmes6f843bc2020-12-11 13:36:29 -08001545func assertExcludeFromRecoverySnapshotIs(t *testing.T, c *Module, expected bool) {
1546 t.Helper()
1547 if c.ExcludeFromRecoverySnapshot() != expected {
1548 t.Errorf("expected %q ExcludeFromRecoverySnapshot to be %t", c.String(), expected)
1549 }
1550}
1551
Bill Peckham945441c2020-08-31 16:07:58 -07001552func TestVendorSnapshotExclude(t *testing.T) {
1553
1554 // This test verifies that the exclude_from_vendor_snapshot property
1555 // makes its way from the Android.bp source file into the module data
1556 // structure. It also verifies that modules are correctly included or
1557 // excluded in the vendor snapshot based on their path (framework or
1558 // vendor) and the exclude_from_vendor_snapshot property.
1559
1560 frameworkBp := `
1561 cc_library_shared {
1562 name: "libinclude",
1563 srcs: ["src/include.cpp"],
1564 vendor_available: true,
1565 }
1566 cc_library_shared {
1567 name: "libexclude",
1568 srcs: ["src/exclude.cpp"],
1569 vendor: true,
1570 exclude_from_vendor_snapshot: true,
1571 }
1572 `
1573
1574 vendorProprietaryBp := `
1575 cc_library_shared {
1576 name: "libvendor",
1577 srcs: ["vendor.cpp"],
1578 vendor: true,
1579 }
1580 `
1581
1582 depsBp := GatherRequiredDepsForTest(android.Android)
1583
1584 mockFS := map[string][]byte{
1585 "deps/Android.bp": []byte(depsBp),
1586 "framework/Android.bp": []byte(frameworkBp),
1587 "framework/include.cpp": nil,
1588 "framework/exclude.cpp": nil,
1589 "device/Android.bp": []byte(vendorProprietaryBp),
1590 "device/vendor.cpp": nil,
1591 }
1592
1593 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1594 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1595 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001596 ctx := CreateTestContext(config)
1597 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001598
1599 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1600 android.FailIfErrored(t, errs)
1601 _, errs = ctx.PrepareBuildActions(config)
1602 android.FailIfErrored(t, errs)
1603
1604 // Test an include and exclude framework module.
1605 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1606 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", vendorVariant).Module().(*Module), false)
1607 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libexclude", vendorVariant).Module().(*Module), true)
1608
1609 // A vendor module is excluded, but by its path, not the
1610 // exclude_from_vendor_snapshot property.
1611 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libvendor", vendorVariant).Module().(*Module), false)
1612
1613 // Verify the content of the vendor snapshot.
1614
1615 snapshotDir := "vendor-snapshot"
1616 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1617 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1618
1619 var includeJsonFiles []string
1620 var excludeJsonFiles []string
1621
1622 for _, arch := range [][]string{
1623 []string{"arm64", "armv8-a"},
1624 []string{"arm", "armv7-a-neon"},
1625 } {
1626 archType := arch[0]
1627 archVariant := arch[1]
1628 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1629
1630 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1631 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1632
1633 // Included modules
1634 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1635 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1636
1637 // Excluded modules
1638 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1639 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1640 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1641 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1642 }
1643
1644 // Verify that each json file for an included module has a rule.
1645 for _, jsonFile := range includeJsonFiles {
1646 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1647 t.Errorf("include json file %q not found", jsonFile)
1648 }
1649 }
1650
1651 // Verify that each json file for an excluded module has no rule.
1652 for _, jsonFile := range excludeJsonFiles {
1653 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1654 t.Errorf("exclude json file %q found", jsonFile)
1655 }
1656 }
1657}
1658
1659func TestVendorSnapshotExcludeInVendorProprietaryPathErrors(t *testing.T) {
1660
1661 // This test verifies that using the exclude_from_vendor_snapshot
1662 // property on a module in a vendor proprietary path generates an
1663 // error. These modules are already excluded, so we prohibit using the
1664 // property in this way, which could add to confusion.
1665
1666 vendorProprietaryBp := `
1667 cc_library_shared {
1668 name: "libvendor",
1669 srcs: ["vendor.cpp"],
1670 vendor: true,
1671 exclude_from_vendor_snapshot: true,
1672 }
1673 `
1674
1675 depsBp := GatherRequiredDepsForTest(android.Android)
1676
1677 mockFS := map[string][]byte{
1678 "deps/Android.bp": []byte(depsBp),
1679 "device/Android.bp": []byte(vendorProprietaryBp),
1680 "device/vendor.cpp": nil,
1681 }
1682
1683 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1684 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1685 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001686 ctx := CreateTestContext(config)
1687 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001688
1689 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "device/Android.bp"})
1690 android.FailIfErrored(t, errs)
1691
1692 _, errs = ctx.PrepareBuildActions(config)
1693 android.CheckErrorsAgainstExpectations(t, errs, []string{
1694 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1695 `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 -08001696 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1697 `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 +09001698 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1699 `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 -07001700 })
1701}
1702
1703func TestVendorSnapshotExcludeWithVendorAvailable(t *testing.T) {
1704
1705 // This test verifies that using the exclude_from_vendor_snapshot
1706 // property on a module that is vendor available generates an error. A
1707 // vendor available module must be captured in the vendor snapshot and
1708 // must not built from source when building the vendor image against
1709 // the vendor snapshot.
1710
1711 frameworkBp := `
1712 cc_library_shared {
1713 name: "libinclude",
1714 srcs: ["src/include.cpp"],
1715 vendor_available: true,
1716 exclude_from_vendor_snapshot: true,
1717 }
1718 `
1719
1720 depsBp := GatherRequiredDepsForTest(android.Android)
1721
1722 mockFS := map[string][]byte{
1723 "deps/Android.bp": []byte(depsBp),
1724 "framework/Android.bp": []byte(frameworkBp),
1725 "framework/include.cpp": nil,
1726 }
1727
1728 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1729 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1730 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001731 ctx := CreateTestContext(config)
1732 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001733
1734 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp"})
1735 android.FailIfErrored(t, errs)
1736
1737 _, errs = ctx.PrepareBuildActions(config)
1738 android.CheckErrorsAgainstExpectations(t, errs, []string{
1739 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1740 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1741 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1742 `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 +09001743 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1744 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1745 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1746 `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 -07001747 })
1748}
1749
Jose Galmesf7294582020-11-13 12:07:36 -08001750func TestRecoverySnapshotCapture(t *testing.T) {
1751 bp := `
1752 cc_library {
1753 name: "libvndk",
1754 vendor_available: true,
1755 recovery_available: true,
1756 product_available: true,
1757 vndk: {
1758 enabled: true,
1759 },
1760 nocrt: true,
1761 }
1762
1763 cc_library {
1764 name: "librecovery",
1765 recovery: true,
1766 nocrt: true,
1767 }
1768
1769 cc_library {
1770 name: "librecovery_available",
1771 recovery_available: true,
1772 nocrt: true,
1773 }
1774
1775 cc_library_headers {
1776 name: "librecovery_headers",
1777 recovery_available: true,
1778 nocrt: true,
1779 }
1780
1781 cc_binary {
1782 name: "recovery_bin",
1783 recovery: true,
1784 nocrt: true,
1785 }
1786
1787 cc_binary {
1788 name: "recovery_available_bin",
1789 recovery_available: true,
1790 nocrt: true,
1791 }
1792
1793 toolchain_library {
1794 name: "libb",
1795 recovery_available: true,
1796 src: "libb.a",
1797 }
1798
1799 cc_object {
1800 name: "obj",
1801 recovery_available: true,
1802 }
1803`
1804 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jose Galmes6f843bc2020-12-11 13:36:29 -08001805 config.TestProductVariables.RecoverySnapshotVersion = StringPtr("current")
Jose Galmesf7294582020-11-13 12:07:36 -08001806 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1807 ctx := testCcWithConfig(t, config)
1808
1809 // Check Recovery snapshot output.
1810
1811 snapshotDir := "recovery-snapshot"
1812 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1813 snapshotSingleton := ctx.SingletonForTests("recovery-snapshot")
1814
1815 var jsonFiles []string
1816
1817 for _, arch := range [][]string{
1818 []string{"arm64", "armv8-a"},
1819 } {
1820 archType := arch[0]
1821 archVariant := arch[1]
1822 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1823
1824 // For shared libraries, only recovery_available modules are captured.
1825 sharedVariant := fmt.Sprintf("android_recovery_%s_%s_shared", archType, archVariant)
1826 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1827 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", sharedDir, sharedVariant)
1828 checkSnapshot(t, ctx, snapshotSingleton, "librecovery", "librecovery.so", sharedDir, sharedVariant)
1829 checkSnapshot(t, ctx, snapshotSingleton, "librecovery_available", "librecovery_available.so", sharedDir, sharedVariant)
1830 jsonFiles = append(jsonFiles,
1831 filepath.Join(sharedDir, "libvndk.so.json"),
1832 filepath.Join(sharedDir, "librecovery.so.json"),
1833 filepath.Join(sharedDir, "librecovery_available.so.json"))
1834
1835 // For static libraries, all recovery:true and recovery_available modules are captured.
1836 staticVariant := fmt.Sprintf("android_recovery_%s_%s_static", archType, archVariant)
1837 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
1838 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1839 checkSnapshot(t, ctx, snapshotSingleton, "librecovery", "librecovery.a", staticDir, staticVariant)
1840 checkSnapshot(t, ctx, snapshotSingleton, "librecovery_available", "librecovery_available.a", staticDir, staticVariant)
1841 jsonFiles = append(jsonFiles,
1842 filepath.Join(staticDir, "libb.a.json"),
1843 filepath.Join(staticDir, "librecovery.a.json"),
1844 filepath.Join(staticDir, "librecovery_available.a.json"))
1845
1846 // For binary executables, all recovery:true and recovery_available modules are captured.
1847 if archType == "arm64" {
1848 binaryVariant := fmt.Sprintf("android_recovery_%s_%s", archType, archVariant)
1849 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
1850 checkSnapshot(t, ctx, snapshotSingleton, "recovery_bin", "recovery_bin", binaryDir, binaryVariant)
1851 checkSnapshot(t, ctx, snapshotSingleton, "recovery_available_bin", "recovery_available_bin", binaryDir, binaryVariant)
1852 jsonFiles = append(jsonFiles,
1853 filepath.Join(binaryDir, "recovery_bin.json"),
1854 filepath.Join(binaryDir, "recovery_available_bin.json"))
1855 }
1856
1857 // For header libraries, all vendor:true and vendor_available modules are captured.
1858 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1859 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "librecovery_headers.json"))
1860
1861 // For object modules, all vendor:true and vendor_available modules are captured.
1862 objectVariant := fmt.Sprintf("android_recovery_%s_%s", archType, archVariant)
1863 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1864 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1865 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
1866 }
1867
1868 for _, jsonFile := range jsonFiles {
1869 // verify all json files exist
1870 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1871 t.Errorf("%q expected but not found", jsonFile)
1872 }
1873 }
1874}
1875
Jose Galmes6f843bc2020-12-11 13:36:29 -08001876func TestRecoverySnapshotExclude(t *testing.T) {
1877 // This test verifies that the exclude_from_recovery_snapshot property
1878 // makes its way from the Android.bp source file into the module data
1879 // structure. It also verifies that modules are correctly included or
1880 // excluded in the recovery snapshot based on their path (framework or
1881 // vendor) and the exclude_from_recovery_snapshot property.
1882
1883 frameworkBp := `
1884 cc_library_shared {
1885 name: "libinclude",
1886 srcs: ["src/include.cpp"],
1887 recovery_available: true,
1888 }
1889 cc_library_shared {
1890 name: "libexclude",
1891 srcs: ["src/exclude.cpp"],
1892 recovery: true,
1893 exclude_from_recovery_snapshot: true,
1894 }
1895 `
1896
1897 vendorProprietaryBp := `
1898 cc_library_shared {
1899 name: "libvendor",
1900 srcs: ["vendor.cpp"],
1901 recovery: true,
1902 }
1903 `
1904
1905 depsBp := GatherRequiredDepsForTest(android.Android)
1906
1907 mockFS := map[string][]byte{
1908 "deps/Android.bp": []byte(depsBp),
1909 "framework/Android.bp": []byte(frameworkBp),
1910 "framework/include.cpp": nil,
1911 "framework/exclude.cpp": nil,
1912 "device/Android.bp": []byte(vendorProprietaryBp),
1913 "device/vendor.cpp": nil,
1914 }
1915
1916 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1917 config.TestProductVariables.RecoverySnapshotVersion = StringPtr("current")
1918 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1919 ctx := CreateTestContext(config)
1920 ctx.Register()
1921
1922 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1923 android.FailIfErrored(t, errs)
1924 _, errs = ctx.PrepareBuildActions(config)
1925 android.FailIfErrored(t, errs)
1926
1927 // Test an include and exclude framework module.
1928 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1929 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libinclude", recoveryVariant).Module().(*Module), false)
1930 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libexclude", recoveryVariant).Module().(*Module), true)
1931
1932 // A vendor module is excluded, but by its path, not the
1933 // exclude_from_recovery_snapshot property.
1934 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libvendor", recoveryVariant).Module().(*Module), false)
1935
1936 // Verify the content of the recovery snapshot.
1937
1938 snapshotDir := "recovery-snapshot"
1939 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1940 snapshotSingleton := ctx.SingletonForTests("recovery-snapshot")
1941
1942 var includeJsonFiles []string
1943 var excludeJsonFiles []string
1944
1945 for _, arch := range [][]string{
1946 []string{"arm64", "armv8-a"},
1947 } {
1948 archType := arch[0]
1949 archVariant := arch[1]
1950 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1951
1952 sharedVariant := fmt.Sprintf("android_recovery_%s_%s_shared", archType, archVariant)
1953 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1954
1955 // Included modules
1956 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1957 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1958
1959 // Excluded modules
1960 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1961 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1962 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1963 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1964 }
1965
1966 // Verify that each json file for an included module has a rule.
1967 for _, jsonFile := range includeJsonFiles {
1968 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1969 t.Errorf("include json file %q not found", jsonFile)
1970 }
1971 }
1972
1973 // Verify that each json file for an excluded module has no rule.
1974 for _, jsonFile := range excludeJsonFiles {
1975 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1976 t.Errorf("exclude json file %q found", jsonFile)
1977 }
1978 }
1979}
1980
Jooyung Hana70f0672019-01-18 15:20:43 +09001981func TestDoubleLoadableDepError(t *testing.T) {
1982 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1983 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1984 cc_library {
1985 name: "libllndk",
1986 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001987 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001988 }
1989
1990 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001991 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001992 symbol_file: "",
1993 }
1994
1995 cc_library {
1996 name: "libnondoubleloadable",
1997 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001998 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001999 vndk: {
2000 enabled: true,
2001 },
2002 }
2003 `)
2004
2005 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
2006 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2007 cc_library {
2008 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07002009 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002010 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07002011 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002012 }
2013
2014 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002015 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002016 symbol_file: "",
2017 }
2018
2019 cc_library {
2020 name: "libnondoubleloadable",
2021 vendor_available: true,
2022 }
2023 `)
2024
Jooyung Hana70f0672019-01-18 15:20:43 +09002025 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
2026 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2027 cc_library {
2028 name: "libllndk",
2029 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07002030 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002031 }
2032
2033 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002034 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002035 symbol_file: "",
2036 }
2037
2038 cc_library {
2039 name: "libcoreonly",
2040 shared_libs: ["libvendoravailable"],
2041 }
2042
2043 // indirect dependency of LLNDK
2044 cc_library {
2045 name: "libvendoravailable",
2046 vendor_available: true,
2047 }
2048 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09002049
2050 // The error is not from 'client' but from 'libllndk'
2051 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
2052 cc_library {
2053 name: "client",
2054 vendor_available: true,
2055 double_loadable: true,
2056 shared_libs: ["libllndk"],
2057 }
2058 cc_library {
2059 name: "libllndk",
2060 shared_libs: ["libnondoubleloadable"],
2061 llndk_stubs: "libllndk.llndk",
2062 }
2063 llndk_library {
2064 name: "libllndk.llndk",
2065 symbol_file: "",
2066 }
2067 cc_library {
2068 name: "libnondoubleloadable",
2069 vendor_available: true,
2070 }
2071 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08002072}
2073
Jooyung Han479ca172020-10-19 18:51:07 +09002074func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
2075 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
2076 cc_library {
2077 name: "libvndksp",
2078 shared_libs: ["libanothervndksp"],
2079 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002080 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09002081 vndk: {
2082 enabled: true,
2083 support_system_process: true,
2084 }
2085 }
2086
2087 cc_library {
2088 name: "libllndk",
2089 shared_libs: ["libanothervndksp"],
2090 }
2091
2092 llndk_library {
2093 name: "libllndk",
2094 symbol_file: "",
2095 }
2096
2097 cc_library {
2098 name: "libanothervndksp",
2099 vendor_available: true,
2100 }
2101 `)
2102}
2103
Logan Chienf3511742017-10-31 18:04:35 +08002104func TestVndkExt(t *testing.T) {
2105 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09002106 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08002107 cc_library {
2108 name: "libvndk",
2109 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002110 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002111 vndk: {
2112 enabled: true,
2113 },
2114 nocrt: true,
2115 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002116 cc_library {
2117 name: "libvndk2",
2118 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002119 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09002120 vndk: {
2121 enabled: true,
2122 },
2123 target: {
2124 vendor: {
2125 suffix: "-suffix",
2126 },
Justin Yun63e9ec72020-10-29 16:49:43 +09002127 product: {
2128 suffix: "-suffix",
2129 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09002130 },
2131 nocrt: true,
2132 }
Logan Chienf3511742017-10-31 18:04:35 +08002133
2134 cc_library {
2135 name: "libvndk_ext",
2136 vendor: true,
2137 vndk: {
2138 enabled: true,
2139 extends: "libvndk",
2140 },
2141 nocrt: true,
2142 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002143
2144 cc_library {
2145 name: "libvndk2_ext",
2146 vendor: true,
2147 vndk: {
2148 enabled: true,
2149 extends: "libvndk2",
2150 },
2151 nocrt: true,
2152 }
Logan Chienf3511742017-10-31 18:04:35 +08002153
Justin Yun0ecf0b22020-02-28 15:07:59 +09002154 cc_library {
2155 name: "libvndk_ext_product",
2156 product_specific: true,
2157 vndk: {
2158 enabled: true,
2159 extends: "libvndk",
2160 },
2161 nocrt: true,
2162 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002163
Justin Yun0ecf0b22020-02-28 15:07:59 +09002164 cc_library {
2165 name: "libvndk2_ext_product",
2166 product_specific: true,
2167 vndk: {
2168 enabled: true,
2169 extends: "libvndk2",
2170 },
2171 nocrt: true,
2172 }
2173 `
2174 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2175 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2176 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2177 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2178
2179 ctx := testCcWithConfig(t, config)
2180
2181 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
2182 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
2183
2184 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
2185 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
2186
2187 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
2188 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08002189}
2190
Logan Chiend3c59a22018-03-29 14:08:15 +08002191func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08002192 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
2193 ctx := testCcNoVndk(t, `
2194 cc_library {
2195 name: "libvndk",
2196 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002197 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002198 vndk: {
2199 enabled: true,
2200 },
2201 nocrt: true,
2202 }
2203
2204 cc_library {
2205 name: "libvndk_ext",
2206 vendor: true,
2207 vndk: {
2208 enabled: true,
2209 extends: "libvndk",
2210 },
2211 nocrt: true,
2212 }
2213 `)
2214
2215 // Ensures that the core variant of "libvndk_ext" can be found.
2216 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
2217 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
2218 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
2219 }
2220}
2221
Justin Yun0ecf0b22020-02-28 15:07:59 +09002222func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
2223 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09002224 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09002225 cc_library {
2226 name: "libvndk",
2227 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002228 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002229 vndk: {
2230 enabled: true,
2231 },
2232 nocrt: true,
2233 }
2234
2235 cc_library {
2236 name: "libvndk_ext_product",
2237 product_specific: true,
2238 vndk: {
2239 enabled: true,
2240 extends: "libvndk",
2241 },
2242 nocrt: true,
2243 }
2244 `)
2245
2246 // Ensures that the core variant of "libvndk_ext_product" can be found.
2247 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
2248 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
2249 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
2250 }
2251}
2252
Logan Chienf3511742017-10-31 18:04:35 +08002253func TestVndkExtError(t *testing.T) {
2254 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09002255 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08002256 cc_library {
2257 name: "libvndk",
2258 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002259 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002260 vndk: {
2261 enabled: true,
2262 },
2263 nocrt: true,
2264 }
2265
2266 cc_library {
2267 name: "libvndk_ext",
2268 vndk: {
2269 enabled: true,
2270 extends: "libvndk",
2271 },
2272 nocrt: true,
2273 }
2274 `)
2275
2276 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
2277 cc_library {
2278 name: "libvndk",
2279 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002280 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002281 vndk: {
2282 enabled: true,
2283 },
2284 nocrt: true,
2285 }
2286
2287 cc_library {
2288 name: "libvndk_ext",
2289 vendor: true,
2290 vndk: {
2291 enabled: true,
2292 },
2293 nocrt: true,
2294 }
2295 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002296
2297 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
2298 cc_library {
2299 name: "libvndk",
2300 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002301 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002302 vndk: {
2303 enabled: true,
2304 },
2305 nocrt: true,
2306 }
2307
2308 cc_library {
2309 name: "libvndk_ext_product",
2310 product_specific: true,
2311 vndk: {
2312 enabled: true,
2313 },
2314 nocrt: true,
2315 }
2316 `)
2317
2318 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
2319 cc_library {
2320 name: "libvndk",
2321 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002322 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002323 vndk: {
2324 enabled: true,
2325 },
2326 nocrt: true,
2327 }
2328
2329 cc_library {
2330 name: "libvndk_ext_product",
2331 product_specific: true,
2332 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002333 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002334 vndk: {
2335 enabled: true,
2336 extends: "libvndk",
2337 },
2338 nocrt: true,
2339 }
2340 `)
Logan Chienf3511742017-10-31 18:04:35 +08002341}
2342
2343func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
2344 // This test ensures an error is emitted for inconsistent support_system_process.
2345 testCcError(t, "module \".*\" with mismatched support_system_process", `
2346 cc_library {
2347 name: "libvndk",
2348 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002349 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002350 vndk: {
2351 enabled: true,
2352 },
2353 nocrt: true,
2354 }
2355
2356 cc_library {
2357 name: "libvndk_sp_ext",
2358 vendor: true,
2359 vndk: {
2360 enabled: true,
2361 extends: "libvndk",
2362 support_system_process: true,
2363 },
2364 nocrt: true,
2365 }
2366 `)
2367
2368 testCcError(t, "module \".*\" with mismatched support_system_process", `
2369 cc_library {
2370 name: "libvndk_sp",
2371 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002372 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002373 vndk: {
2374 enabled: true,
2375 support_system_process: true,
2376 },
2377 nocrt: true,
2378 }
2379
2380 cc_library {
2381 name: "libvndk_ext",
2382 vendor: true,
2383 vndk: {
2384 enabled: true,
2385 extends: "libvndk_sp",
2386 },
2387 nocrt: true,
2388 }
2389 `)
2390}
2391
2392func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08002393 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09002394 // with `private: true`.
2395 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08002396 cc_library {
2397 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09002398 vendor_available: true,
2399 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002400 vndk: {
2401 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002402 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08002403 },
2404 nocrt: true,
2405 }
2406
2407 cc_library {
2408 name: "libvndk_ext",
2409 vendor: true,
2410 vndk: {
2411 enabled: true,
2412 extends: "libvndk",
2413 },
2414 nocrt: true,
2415 }
2416 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002417
Justin Yunfd9e8042020-12-23 18:23:14 +09002418 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09002419 cc_library {
2420 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09002421 vendor_available: true,
2422 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002423 vndk: {
2424 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002425 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002426 },
2427 nocrt: true,
2428 }
2429
2430 cc_library {
2431 name: "libvndk_ext_product",
2432 product_specific: true,
2433 vndk: {
2434 enabled: true,
2435 extends: "libvndk",
2436 },
2437 nocrt: true,
2438 }
2439 `)
Logan Chienf3511742017-10-31 18:04:35 +08002440}
2441
Logan Chiend3c59a22018-03-29 14:08:15 +08002442func TestVendorModuleUseVndkExt(t *testing.T) {
2443 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002444 testCc(t, `
2445 cc_library {
2446 name: "libvndk",
2447 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002448 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002449 vndk: {
2450 enabled: true,
2451 },
2452 nocrt: true,
2453 }
2454
2455 cc_library {
2456 name: "libvndk_ext",
2457 vendor: true,
2458 vndk: {
2459 enabled: true,
2460 extends: "libvndk",
2461 },
2462 nocrt: true,
2463 }
2464
2465 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08002466 name: "libvndk_sp",
2467 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002468 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002469 vndk: {
2470 enabled: true,
2471 support_system_process: true,
2472 },
2473 nocrt: true,
2474 }
2475
2476 cc_library {
2477 name: "libvndk_sp_ext",
2478 vendor: true,
2479 vndk: {
2480 enabled: true,
2481 extends: "libvndk_sp",
2482 support_system_process: true,
2483 },
2484 nocrt: true,
2485 }
2486
2487 cc_library {
2488 name: "libvendor",
2489 vendor: true,
2490 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
2491 nocrt: true,
2492 }
2493 `)
2494}
2495
Logan Chiend3c59a22018-03-29 14:08:15 +08002496func TestVndkExtUseVendorLib(t *testing.T) {
2497 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08002498 testCc(t, `
2499 cc_library {
2500 name: "libvndk",
2501 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002502 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002503 vndk: {
2504 enabled: true,
2505 },
2506 nocrt: true,
2507 }
2508
2509 cc_library {
2510 name: "libvndk_ext",
2511 vendor: true,
2512 vndk: {
2513 enabled: true,
2514 extends: "libvndk",
2515 },
2516 shared_libs: ["libvendor"],
2517 nocrt: true,
2518 }
2519
2520 cc_library {
2521 name: "libvendor",
2522 vendor: true,
2523 nocrt: true,
2524 }
2525 `)
Logan Chienf3511742017-10-31 18:04:35 +08002526
Logan Chiend3c59a22018-03-29 14:08:15 +08002527 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
2528 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08002529 cc_library {
2530 name: "libvndk_sp",
2531 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002532 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002533 vndk: {
2534 enabled: true,
2535 support_system_process: true,
2536 },
2537 nocrt: true,
2538 }
2539
2540 cc_library {
2541 name: "libvndk_sp_ext",
2542 vendor: true,
2543 vndk: {
2544 enabled: true,
2545 extends: "libvndk_sp",
2546 support_system_process: true,
2547 },
2548 shared_libs: ["libvendor"], // Cause an error
2549 nocrt: true,
2550 }
2551
2552 cc_library {
2553 name: "libvendor",
2554 vendor: true,
2555 nocrt: true,
2556 }
2557 `)
2558}
2559
Justin Yun0ecf0b22020-02-28 15:07:59 +09002560func TestProductVndkExtDependency(t *testing.T) {
2561 bp := `
2562 cc_library {
2563 name: "libvndk",
2564 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002565 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002566 vndk: {
2567 enabled: true,
2568 },
2569 nocrt: true,
2570 }
2571
2572 cc_library {
2573 name: "libvndk_ext_product",
2574 product_specific: true,
2575 vndk: {
2576 enabled: true,
2577 extends: "libvndk",
2578 },
2579 shared_libs: ["libproduct_for_vndklibs"],
2580 nocrt: true,
2581 }
2582
2583 cc_library {
2584 name: "libvndk_sp",
2585 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002586 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002587 vndk: {
2588 enabled: true,
2589 support_system_process: true,
2590 },
2591 nocrt: true,
2592 }
2593
2594 cc_library {
2595 name: "libvndk_sp_ext_product",
2596 product_specific: true,
2597 vndk: {
2598 enabled: true,
2599 extends: "libvndk_sp",
2600 support_system_process: true,
2601 },
2602 shared_libs: ["libproduct_for_vndklibs"],
2603 nocrt: true,
2604 }
2605
2606 cc_library {
2607 name: "libproduct",
2608 product_specific: true,
2609 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
2610 nocrt: true,
2611 }
2612
2613 cc_library {
2614 name: "libproduct_for_vndklibs",
2615 product_specific: true,
2616 nocrt: true,
2617 }
2618 `
2619 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2620 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2621 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2622 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2623
2624 testCcWithConfig(t, config)
2625}
2626
Logan Chiend3c59a22018-03-29 14:08:15 +08002627func TestVndkSpExtUseVndkError(t *testing.T) {
2628 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
2629 // library.
2630 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2631 cc_library {
2632 name: "libvndk",
2633 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002634 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002635 vndk: {
2636 enabled: true,
2637 },
2638 nocrt: true,
2639 }
2640
2641 cc_library {
2642 name: "libvndk_sp",
2643 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002644 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002645 vndk: {
2646 enabled: true,
2647 support_system_process: true,
2648 },
2649 nocrt: true,
2650 }
2651
2652 cc_library {
2653 name: "libvndk_sp_ext",
2654 vendor: true,
2655 vndk: {
2656 enabled: true,
2657 extends: "libvndk_sp",
2658 support_system_process: true,
2659 },
2660 shared_libs: ["libvndk"], // Cause an error
2661 nocrt: true,
2662 }
2663 `)
2664
2665 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
2666 // library.
2667 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2668 cc_library {
2669 name: "libvndk",
2670 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002671 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002672 vndk: {
2673 enabled: true,
2674 },
2675 nocrt: true,
2676 }
2677
2678 cc_library {
2679 name: "libvndk_ext",
2680 vendor: true,
2681 vndk: {
2682 enabled: true,
2683 extends: "libvndk",
2684 },
2685 nocrt: true,
2686 }
2687
2688 cc_library {
2689 name: "libvndk_sp",
2690 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002691 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002692 vndk: {
2693 enabled: true,
2694 support_system_process: true,
2695 },
2696 nocrt: true,
2697 }
2698
2699 cc_library {
2700 name: "libvndk_sp_ext",
2701 vendor: true,
2702 vndk: {
2703 enabled: true,
2704 extends: "libvndk_sp",
2705 support_system_process: true,
2706 },
2707 shared_libs: ["libvndk_ext"], // Cause an error
2708 nocrt: true,
2709 }
2710 `)
2711}
2712
2713func TestVndkUseVndkExtError(t *testing.T) {
2714 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2715 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002716 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2717 cc_library {
2718 name: "libvndk",
2719 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002720 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002721 vndk: {
2722 enabled: true,
2723 },
2724 nocrt: true,
2725 }
2726
2727 cc_library {
2728 name: "libvndk_ext",
2729 vendor: true,
2730 vndk: {
2731 enabled: true,
2732 extends: "libvndk",
2733 },
2734 nocrt: true,
2735 }
2736
2737 cc_library {
2738 name: "libvndk2",
2739 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002740 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002741 vndk: {
2742 enabled: true,
2743 },
2744 shared_libs: ["libvndk_ext"],
2745 nocrt: true,
2746 }
2747 `)
2748
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002749 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002750 cc_library {
2751 name: "libvndk",
2752 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002753 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002754 vndk: {
2755 enabled: true,
2756 },
2757 nocrt: true,
2758 }
2759
2760 cc_library {
2761 name: "libvndk_ext",
2762 vendor: true,
2763 vndk: {
2764 enabled: true,
2765 extends: "libvndk",
2766 },
2767 nocrt: true,
2768 }
2769
2770 cc_library {
2771 name: "libvndk2",
2772 vendor_available: true,
2773 vndk: {
2774 enabled: true,
2775 },
2776 target: {
2777 vendor: {
2778 shared_libs: ["libvndk_ext"],
2779 },
2780 },
2781 nocrt: true,
2782 }
2783 `)
2784
2785 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2786 cc_library {
2787 name: "libvndk_sp",
2788 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002789 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002790 vndk: {
2791 enabled: true,
2792 support_system_process: true,
2793 },
2794 nocrt: true,
2795 }
2796
2797 cc_library {
2798 name: "libvndk_sp_ext",
2799 vendor: true,
2800 vndk: {
2801 enabled: true,
2802 extends: "libvndk_sp",
2803 support_system_process: true,
2804 },
2805 nocrt: true,
2806 }
2807
2808 cc_library {
2809 name: "libvndk_sp_2",
2810 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002811 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002812 vndk: {
2813 enabled: true,
2814 support_system_process: true,
2815 },
2816 shared_libs: ["libvndk_sp_ext"],
2817 nocrt: true,
2818 }
2819 `)
2820
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002821 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002822 cc_library {
2823 name: "libvndk_sp",
2824 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002825 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002826 vndk: {
2827 enabled: true,
2828 },
2829 nocrt: true,
2830 }
2831
2832 cc_library {
2833 name: "libvndk_sp_ext",
2834 vendor: true,
2835 vndk: {
2836 enabled: true,
2837 extends: "libvndk_sp",
2838 },
2839 nocrt: true,
2840 }
2841
2842 cc_library {
2843 name: "libvndk_sp2",
2844 vendor_available: true,
2845 vndk: {
2846 enabled: true,
2847 },
2848 target: {
2849 vendor: {
2850 shared_libs: ["libvndk_sp_ext"],
2851 },
2852 },
2853 nocrt: true,
2854 }
2855 `)
2856}
2857
Justin Yun5f7f7e82019-11-18 19:52:14 +09002858func TestEnforceProductVndkVersion(t *testing.T) {
2859 bp := `
2860 cc_library {
2861 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002862 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002863 }
2864 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002865 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002866 symbol_file: "",
2867 }
2868 cc_library {
2869 name: "libvndk",
2870 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002871 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002872 vndk: {
2873 enabled: true,
2874 },
2875 nocrt: true,
2876 }
2877 cc_library {
2878 name: "libvndk_sp",
2879 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002880 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002881 vndk: {
2882 enabled: true,
2883 support_system_process: true,
2884 },
2885 nocrt: true,
2886 }
2887 cc_library {
2888 name: "libva",
2889 vendor_available: true,
2890 nocrt: true,
2891 }
2892 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002893 name: "libpa",
2894 product_available: true,
2895 nocrt: true,
2896 }
2897 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002898 name: "libboth_available",
2899 vendor_available: true,
2900 product_available: true,
2901 nocrt: true,
2902 target: {
2903 vendor: {
2904 suffix: "-vendor",
2905 },
2906 product: {
2907 suffix: "-product",
2908 },
2909 }
2910 }
2911 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002912 name: "libproduct_va",
2913 product_specific: true,
2914 vendor_available: true,
2915 nocrt: true,
2916 }
2917 cc_library {
2918 name: "libprod",
2919 product_specific: true,
2920 shared_libs: [
2921 "libllndk",
2922 "libvndk",
2923 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002924 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002925 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002926 "libproduct_va",
2927 ],
2928 nocrt: true,
2929 }
2930 cc_library {
2931 name: "libvendor",
2932 vendor: true,
2933 shared_libs: [
2934 "libllndk",
2935 "libvndk",
2936 "libvndk_sp",
2937 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002938 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002939 "libproduct_va",
2940 ],
2941 nocrt: true,
2942 }
2943 `
2944
2945 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2946 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2947 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2948 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2949
2950 ctx := testCcWithConfig(t, config)
2951
Jooyung Han261e1582020-10-20 18:54:21 +09002952 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2953 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002954
2955 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2956 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2957
2958 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2959 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002960}
2961
2962func TestEnforceProductVndkVersionErrors(t *testing.T) {
2963 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2964 cc_library {
2965 name: "libprod",
2966 product_specific: true,
2967 shared_libs: [
2968 "libvendor",
2969 ],
2970 nocrt: true,
2971 }
2972 cc_library {
2973 name: "libvendor",
2974 vendor: true,
2975 nocrt: true,
2976 }
2977 `)
2978 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2979 cc_library {
2980 name: "libprod",
2981 product_specific: true,
2982 shared_libs: [
2983 "libsystem",
2984 ],
2985 nocrt: true,
2986 }
2987 cc_library {
2988 name: "libsystem",
2989 nocrt: true,
2990 }
2991 `)
Justin Yun6977e8a2020-10-29 18:24:11 +09002992 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2993 cc_library {
2994 name: "libprod",
2995 product_specific: true,
2996 shared_libs: [
2997 "libva",
2998 ],
2999 nocrt: true,
3000 }
3001 cc_library {
3002 name: "libva",
3003 vendor_available: true,
3004 nocrt: true,
3005 }
3006 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09003007 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09003008 cc_library {
3009 name: "libprod",
3010 product_specific: true,
3011 shared_libs: [
3012 "libvndk_private",
3013 ],
3014 nocrt: true,
3015 }
3016 cc_library {
3017 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09003018 vendor_available: true,
3019 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09003020 vndk: {
3021 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09003022 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09003023 },
3024 nocrt: true,
3025 }
3026 `)
3027 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
3028 cc_library {
3029 name: "libprod",
3030 product_specific: true,
3031 shared_libs: [
3032 "libsystem_ext",
3033 ],
3034 nocrt: true,
3035 }
3036 cc_library {
3037 name: "libsystem_ext",
3038 system_ext_specific: true,
3039 nocrt: true,
3040 }
3041 `)
3042 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
3043 cc_library {
3044 name: "libsystem",
3045 shared_libs: [
3046 "libproduct_va",
3047 ],
3048 nocrt: true,
3049 }
3050 cc_library {
3051 name: "libproduct_va",
3052 product_specific: true,
3053 vendor_available: true,
3054 nocrt: true,
3055 }
3056 `)
3057}
3058
Jooyung Han38002912019-05-16 04:01:54 +09003059func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08003060 bp := `
3061 cc_library {
3062 name: "libvndk",
3063 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003064 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003065 vndk: {
3066 enabled: true,
3067 },
3068 }
3069 cc_library {
3070 name: "libvndksp",
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 support_system_process: true,
3076 },
3077 }
3078 cc_library {
3079 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09003080 vendor_available: true,
3081 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003082 vndk: {
3083 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09003084 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003085 },
3086 }
3087 cc_library {
3088 name: "libvendor",
3089 vendor: true,
3090 }
3091 cc_library {
3092 name: "libvndkext",
3093 vendor: true,
3094 vndk: {
3095 enabled: true,
3096 extends: "libvndk",
3097 },
3098 }
3099 vndk_prebuilt_shared {
3100 name: "prevndk",
3101 version: "27",
3102 target_arch: "arm",
3103 binder32bit: true,
3104 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003105 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003106 vndk: {
3107 enabled: true,
3108 },
3109 arch: {
3110 arm: {
3111 srcs: ["liba.so"],
3112 },
3113 },
3114 }
3115 cc_library {
3116 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07003117 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003118 }
3119 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003120 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003121 symbol_file: "",
3122 }
3123 cc_library {
3124 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07003125 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003126 }
3127 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003128 name: "libllndkprivate.llndk",
Justin Yunc0d8c492021-01-07 17:45:31 +09003129 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003130 symbol_file: "",
Colin Cross78212242021-01-06 14:51:30 -08003131 }
3132
3133 llndk_libraries_txt {
3134 name: "llndk.libraries.txt",
3135 }
3136 vndkcore_libraries_txt {
3137 name: "vndkcore.libraries.txt",
3138 }
3139 vndksp_libraries_txt {
3140 name: "vndksp.libraries.txt",
3141 }
3142 vndkprivate_libraries_txt {
3143 name: "vndkprivate.libraries.txt",
3144 }
3145 vndkcorevariant_libraries_txt {
3146 name: "vndkcorevariant.libraries.txt",
3147 insert_vndk_version: false,
3148 }
3149 `
Colin Cross98be1bb2019-12-13 20:41:13 -08003150
3151 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09003152 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3153 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3154 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08003155 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09003156
Colin Cross78212242021-01-06 14:51:30 -08003157 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
3158 []string{"libvndk.so", "libvndkprivate.so"})
3159 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
3160 []string{"libc++.so", "libvndksp.so"})
3161 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
3162 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
3163 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
3164 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09003165
Colin Crossfb0c16e2019-11-20 17:12:35 -08003166 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09003167
Jooyung Han38002912019-05-16 04:01:54 +09003168 tests := []struct {
3169 variant string
3170 name string
3171 expected string
3172 }{
3173 {vendorVariant, "libvndk", "native:vndk"},
3174 {vendorVariant, "libvndksp", "native:vndk"},
3175 {vendorVariant, "libvndkprivate", "native:vndk_private"},
3176 {vendorVariant, "libvendor", "native:vendor"},
3177 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08003178 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09003179 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09003180 {coreVariant, "libvndk", "native:platform"},
3181 {coreVariant, "libvndkprivate", "native:platform"},
3182 {coreVariant, "libllndk", "native:platform"},
3183 }
3184 for _, test := range tests {
3185 t.Run(test.name, func(t *testing.T) {
3186 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
3187 assertString(t, module.makeLinkType, test.expected)
3188 })
3189 }
3190}
3191
Jeff Gaston294356f2017-09-27 17:05:30 -07003192var staticLinkDepOrderTestCases = []struct {
3193 // This is a string representation of a map[moduleName][]moduleDependency .
3194 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003195 inStatic string
3196
3197 // This is a string representation of a map[moduleName][]moduleDependency .
3198 // It models the dependencies declared in an Android.bp file.
3199 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07003200
3201 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
3202 // The keys of allOrdered specify which modules we would like to check.
3203 // The values of allOrdered specify the expected result (of the transitive closure of all
3204 // dependencies) for each module to test
3205 allOrdered string
3206
3207 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
3208 // The keys of outOrdered specify which modules we would like to check.
3209 // The values of outOrdered specify the expected result (of the ordered linker command line)
3210 // for each module to test.
3211 outOrdered string
3212}{
3213 // Simple tests
3214 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003215 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07003216 outOrdered: "",
3217 },
3218 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003219 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003220 outOrdered: "a:",
3221 },
3222 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003223 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003224 outOrdered: "a:b; b:",
3225 },
3226 // Tests of reordering
3227 {
3228 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003229 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003230 outOrdered: "a:b,c,d; b:d; c:d; d:",
3231 },
3232 {
3233 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003234 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003235 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
3236 },
3237 {
3238 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003239 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07003240 outOrdered: "a:d,b,e,c; d:b; e:c",
3241 },
3242 {
3243 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003244 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07003245 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
3246 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
3247 },
3248 {
3249 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003250 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 -07003251 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
3252 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
3253 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003254 // shared dependencies
3255 {
3256 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
3257 // So, we don't actually have to check that a shared dependency of c will change the order
3258 // of a library that depends statically on b and on c. We only need to check that if c has
3259 // a shared dependency on b, that that shows up in allOrdered.
3260 inShared: "c:b",
3261 allOrdered: "c:b",
3262 outOrdered: "c:",
3263 },
3264 {
3265 // This test doesn't actually include any shared dependencies but it's a reminder of what
3266 // the second phase of the above test would look like
3267 inStatic: "a:b,c; c:b",
3268 allOrdered: "a:c,b; c:b",
3269 outOrdered: "a:c,b; c:b",
3270 },
Jeff Gaston294356f2017-09-27 17:05:30 -07003271 // tiebreakers for when two modules specifying different orderings and there is no dependency
3272 // to dictate an order
3273 {
3274 // 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 -08003275 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07003276 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
3277 },
3278 {
3279 // 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 -08003280 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 -07003281 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
3282 },
3283 // Tests involving duplicate dependencies
3284 {
3285 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003286 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003287 outOrdered: "a:c,b",
3288 },
3289 {
3290 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003291 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003292 outOrdered: "a:d,c,b",
3293 },
3294 // Tests to confirm the nonexistence of infinite loops.
3295 // These cases should never happen, so as long as the test terminates and the
3296 // result is deterministic then that should be fine.
3297 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003298 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003299 outOrdered: "a:a",
3300 },
3301 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003302 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003303 allOrdered: "a:b,c; b:c,a; c:a,b",
3304 outOrdered: "a:b; b:c; c:a",
3305 },
3306 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003307 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003308 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
3309 outOrdered: "a:c,b; b:a,c; c:b,a",
3310 },
3311}
3312
3313// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
3314func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
3315 // convert from "a:b,c; d:e" to "a:b,c;d:e"
3316 strippedText := strings.Replace(text, " ", "", -1)
3317 if len(strippedText) < 1 {
3318 return []android.Path{}, make(map[android.Path][]android.Path, 0)
3319 }
3320 allDeps = make(map[android.Path][]android.Path, 0)
3321
3322 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
3323 moduleTexts := strings.Split(strippedText, ";")
3324
3325 outputForModuleName := func(moduleName string) android.Path {
3326 return android.PathForTesting(moduleName)
3327 }
3328
3329 for _, moduleText := range moduleTexts {
3330 // convert from "a:b,c" to ["a", "b,c"]
3331 components := strings.Split(moduleText, ":")
3332 if len(components) != 2 {
3333 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
3334 }
3335 moduleName := components[0]
3336 moduleOutput := outputForModuleName(moduleName)
3337 modulesInOrder = append(modulesInOrder, moduleOutput)
3338
3339 depString := components[1]
3340 // convert from "b,c" to ["b", "c"]
3341 depNames := strings.Split(depString, ",")
3342 if len(depString) < 1 {
3343 depNames = []string{}
3344 }
3345 var deps []android.Path
3346 for _, depName := range depNames {
3347 deps = append(deps, outputForModuleName(depName))
3348 }
3349 allDeps[moduleOutput] = deps
3350 }
3351 return modulesInOrder, allDeps
3352}
3353
Jeff Gaston294356f2017-09-27 17:05:30 -07003354func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
3355 for _, moduleName := range moduleNames {
3356 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
3357 output := module.outputFile.Path()
3358 paths = append(paths, output)
3359 }
3360 return paths
3361}
3362
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003363func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07003364 ctx := testCc(t, `
3365 cc_library {
3366 name: "a",
3367 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09003368 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003369 }
3370 cc_library {
3371 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003372 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003373 }
3374 cc_library {
3375 name: "c",
3376 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003377 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003378 }
3379 cc_library {
3380 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09003381 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003382 }
3383
3384 `)
3385
Colin Cross7113d202019-11-20 16:39:12 -08003386 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07003387 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003388 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3389 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07003390
3391 if !reflect.DeepEqual(actual, expected) {
3392 t.Errorf("staticDeps orderings were not propagated correctly"+
3393 "\nactual: %v"+
3394 "\nexpected: %v",
3395 actual,
3396 expected,
3397 )
3398 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09003399}
Jeff Gaston294356f2017-09-27 17:05:30 -07003400
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003401func TestStaticLibDepReorderingWithShared(t *testing.T) {
3402 ctx := testCc(t, `
3403 cc_library {
3404 name: "a",
3405 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09003406 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003407 }
3408 cc_library {
3409 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003410 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003411 }
3412 cc_library {
3413 name: "c",
3414 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003415 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003416 }
3417
3418 `)
3419
Colin Cross7113d202019-11-20 16:39:12 -08003420 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003421 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003422 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3423 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003424
3425 if !reflect.DeepEqual(actual, expected) {
3426 t.Errorf("staticDeps orderings did not account for shared libs"+
3427 "\nactual: %v"+
3428 "\nexpected: %v",
3429 actual,
3430 expected,
3431 )
3432 }
3433}
3434
Jooyung Hanb04a4992020-03-13 18:57:35 +09003435func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07003436 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003437 if !reflect.DeepEqual(actual, expected) {
3438 t.Errorf(message+
3439 "\nactual: %v"+
3440 "\nexpected: %v",
3441 actual,
3442 expected,
3443 )
3444 }
3445}
3446
Jooyung Han61b66e92020-03-21 14:21:46 +00003447func TestLlndkLibrary(t *testing.T) {
3448 ctx := testCc(t, `
3449 cc_library {
3450 name: "libllndk",
3451 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07003452 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003453 }
3454 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003455 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003456 }
Colin Cross127bb8b2020-12-16 16:46:01 -08003457
3458 cc_prebuilt_library_shared {
3459 name: "libllndkprebuilt",
3460 stubs: { versions: ["1", "2"] },
3461 llndk_stubs: "libllndkprebuilt.llndk",
3462 }
3463 llndk_library {
3464 name: "libllndkprebuilt.llndk",
3465 }
3466
3467 cc_library {
3468 name: "libllndk_with_external_headers",
3469 stubs: { versions: ["1", "2"] },
3470 llndk_stubs: "libllndk_with_external_headers.llndk",
3471 header_libs: ["libexternal_headers"],
3472 export_header_lib_headers: ["libexternal_headers"],
3473 }
3474 llndk_library {
3475 name: "libllndk_with_external_headers.llndk",
3476 }
3477 cc_library_headers {
3478 name: "libexternal_headers",
3479 export_include_dirs: ["include"],
3480 vendor_available: true,
3481 }
Jooyung Han61b66e92020-03-21 14:21:46 +00003482 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08003483 actual := ctx.ModuleVariantsForTests("libllndk")
3484 for i := 0; i < len(actual); i++ {
3485 if !strings.HasPrefix(actual[i], "android_vendor.VER_") {
3486 actual = append(actual[:i], actual[i+1:]...)
3487 i--
3488 }
3489 }
Jooyung Han61b66e92020-03-21 14:21:46 +00003490 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00003491 "android_vendor.VER_arm64_armv8-a_shared_1",
3492 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003493 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003494 "android_vendor.VER_arm_armv7-a-neon_shared_1",
3495 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003496 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003497 }
3498 checkEquals(t, "variants for llndk stubs", expected, actual)
3499
Colin Cross127bb8b2020-12-16 16:46:01 -08003500 params := ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00003501 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
3502
Colin Cross127bb8b2020-12-16 16:46:01 -08003503 params = ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00003504 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
3505}
3506
Jiyong Parka46a4d52017-12-14 19:54:34 +09003507func TestLlndkHeaders(t *testing.T) {
3508 ctx := testCc(t, `
3509 llndk_headers {
3510 name: "libllndk_headers",
3511 export_include_dirs: ["my_include"],
3512 }
3513 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003514 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09003515 export_llndk_headers: ["libllndk_headers"],
3516 }
3517 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07003518 name: "libllndk",
3519 llndk_stubs: "libllndk.llndk",
3520 }
3521
3522 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09003523 name: "libvendor",
3524 shared_libs: ["libllndk"],
3525 vendor: true,
3526 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003527 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08003528 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09003529 }
3530 `)
3531
3532 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08003533 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09003534 cflags := cc.Args["cFlags"]
3535 if !strings.Contains(cflags, "-Imy_include") {
3536 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
3537 }
3538}
3539
Logan Chien43d34c32017-12-20 01:17:32 +08003540func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
3541 actual := module.Properties.AndroidMkRuntimeLibs
3542 if !reflect.DeepEqual(actual, expected) {
3543 t.Errorf("incorrect runtime_libs for shared libs"+
3544 "\nactual: %v"+
3545 "\nexpected: %v",
3546 actual,
3547 expected,
3548 )
3549 }
3550}
3551
3552const runtimeLibAndroidBp = `
3553 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09003554 name: "liball_available",
3555 vendor_available: true,
3556 product_available: true,
3557 no_libcrt : true,
3558 nocrt : true,
3559 system_shared_libs : [],
3560 }
3561 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08003562 name: "libvendor_available1",
3563 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003564 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07003565 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003566 nocrt : true,
3567 system_shared_libs : [],
3568 }
3569 cc_library {
3570 name: "libvendor_available2",
3571 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003572 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08003573 target: {
3574 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09003575 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08003576 }
3577 },
Yi Konge7fe9912019-06-02 00:53:50 -07003578 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003579 nocrt : true,
3580 system_shared_libs : [],
3581 }
3582 cc_library {
3583 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09003584 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07003585 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003586 nocrt : true,
3587 system_shared_libs : [],
3588 }
3589 cc_library {
3590 name: "libvendor1",
3591 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003592 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003593 nocrt : true,
3594 system_shared_libs : [],
3595 }
3596 cc_library {
3597 name: "libvendor2",
3598 vendor: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003599 runtime_libs: ["liball_available", "libvendor1"],
3600 no_libcrt : true,
3601 nocrt : true,
3602 system_shared_libs : [],
3603 }
3604 cc_library {
3605 name: "libproduct_available1",
3606 product_available: true,
3607 runtime_libs: ["liball_available"],
3608 no_libcrt : true,
3609 nocrt : true,
3610 system_shared_libs : [],
3611 }
3612 cc_library {
3613 name: "libproduct1",
3614 product_specific: true,
3615 no_libcrt : true,
3616 nocrt : true,
3617 system_shared_libs : [],
3618 }
3619 cc_library {
3620 name: "libproduct2",
3621 product_specific: true,
3622 runtime_libs: ["liball_available", "libproduct1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003623 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003624 nocrt : true,
3625 system_shared_libs : [],
3626 }
3627`
3628
3629func TestRuntimeLibs(t *testing.T) {
3630 ctx := testCc(t, runtimeLibAndroidBp)
3631
3632 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08003633 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003634
Justin Yun8a2600c2020-12-07 12:44:03 +09003635 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3636 checkRuntimeLibs(t, []string{"liball_available"}, module)
3637
3638 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3639 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003640
3641 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003642 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003643
3644 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3645 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08003646 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003647
Justin Yun8a2600c2020-12-07 12:44:03 +09003648 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3649 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003650
3651 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003652 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1"}, module)
3653
3654 // runtime_libs for product variants have '.product' suffixes if the modules have both core
3655 // and product variants.
3656 variant = "android_product.VER_arm64_armv8-a_shared"
3657
3658 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3659 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
3660
3661 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
3662 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003663}
3664
3665func TestExcludeRuntimeLibs(t *testing.T) {
3666 ctx := testCc(t, runtimeLibAndroidBp)
3667
Colin Cross7113d202019-11-20 16:39:12 -08003668 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003669 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3670 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003671
Colin Crossfb0c16e2019-11-20 17:12:35 -08003672 variant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003673 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08003674 checkRuntimeLibs(t, nil, module)
3675}
3676
3677func TestRuntimeLibsNoVndk(t *testing.T) {
3678 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3679
3680 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3681
Colin Cross7113d202019-11-20 16:39:12 -08003682 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003683
Justin Yun8a2600c2020-12-07 12:44:03 +09003684 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3685 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003686
3687 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003688 checkRuntimeLibs(t, []string{"liball_available", "libvendor1"}, module)
3689
3690 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
3691 checkRuntimeLibs(t, []string{"liball_available", "libproduct1"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003692}
3693
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003694func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003695 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003696 actual := module.Properties.AndroidMkStaticLibs
3697 if !reflect.DeepEqual(actual, expected) {
3698 t.Errorf("incorrect static_libs"+
3699 "\nactual: %v"+
3700 "\nexpected: %v",
3701 actual,
3702 expected,
3703 )
3704 }
3705}
3706
3707const staticLibAndroidBp = `
3708 cc_library {
3709 name: "lib1",
3710 }
3711 cc_library {
3712 name: "lib2",
3713 static_libs: ["lib1"],
3714 }
3715`
3716
3717func TestStaticLibDepExport(t *testing.T) {
3718 ctx := testCc(t, staticLibAndroidBp)
3719
3720 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003721 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003722 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003723 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003724
3725 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003726 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003727 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3728 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003729 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003730}
3731
Jiyong Parkd08b6972017-09-26 10:50:54 +09003732var compilerFlagsTestCases = []struct {
3733 in string
3734 out bool
3735}{
3736 {
3737 in: "a",
3738 out: false,
3739 },
3740 {
3741 in: "-a",
3742 out: true,
3743 },
3744 {
3745 in: "-Ipath/to/something",
3746 out: false,
3747 },
3748 {
3749 in: "-isystempath/to/something",
3750 out: false,
3751 },
3752 {
3753 in: "--coverage",
3754 out: false,
3755 },
3756 {
3757 in: "-include a/b",
3758 out: true,
3759 },
3760 {
3761 in: "-include a/b c/d",
3762 out: false,
3763 },
3764 {
3765 in: "-DMACRO",
3766 out: true,
3767 },
3768 {
3769 in: "-DMAC RO",
3770 out: false,
3771 },
3772 {
3773 in: "-a -b",
3774 out: false,
3775 },
3776 {
3777 in: "-DMACRO=definition",
3778 out: true,
3779 },
3780 {
3781 in: "-DMACRO=defi nition",
3782 out: true, // TODO(jiyong): this should be false
3783 },
3784 {
3785 in: "-DMACRO(x)=x + 1",
3786 out: true,
3787 },
3788 {
3789 in: "-DMACRO=\"defi nition\"",
3790 out: true,
3791 },
3792}
3793
3794type mockContext struct {
3795 BaseModuleContext
3796 result bool
3797}
3798
3799func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3800 // CheckBadCompilerFlags calls this function when the flag should be rejected
3801 ctx.result = false
3802}
3803
3804func TestCompilerFlags(t *testing.T) {
3805 for _, testCase := range compilerFlagsTestCases {
3806 ctx := &mockContext{result: true}
3807 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3808 if ctx.result != testCase.out {
3809 t.Errorf("incorrect output:")
3810 t.Errorf(" input: %#v", testCase.in)
3811 t.Errorf(" expected: %#v", testCase.out)
3812 t.Errorf(" got: %#v", ctx.result)
3813 }
3814 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003815}
Jiyong Park374510b2018-03-19 18:23:01 +09003816
3817func TestVendorPublicLibraries(t *testing.T) {
3818 ctx := testCc(t, `
3819 cc_library_headers {
3820 name: "libvendorpublic_headers",
3821 export_include_dirs: ["my_include"],
3822 }
3823 vendor_public_library {
3824 name: "libvendorpublic",
3825 symbol_file: "",
3826 export_public_headers: ["libvendorpublic_headers"],
3827 }
3828 cc_library {
3829 name: "libvendorpublic",
3830 srcs: ["foo.c"],
3831 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003832 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003833 nocrt: true,
3834 }
3835
3836 cc_library {
3837 name: "libsystem",
3838 shared_libs: ["libvendorpublic"],
3839 vendor: false,
3840 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003841 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003842 nocrt: true,
3843 }
3844 cc_library {
3845 name: "libvendor",
3846 shared_libs: ["libvendorpublic"],
3847 vendor: true,
3848 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003849 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003850 nocrt: true,
3851 }
3852 `)
3853
Colin Cross7113d202019-11-20 16:39:12 -08003854 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003855 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003856
3857 // test if header search paths are correctly added
3858 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003859 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003860 cflags := cc.Args["cFlags"]
3861 if !strings.Contains(cflags, "-Imy_include") {
3862 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3863 }
3864
3865 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003866 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003867 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003868 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003869 if !strings.Contains(libflags, stubPaths[0].String()) {
3870 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3871 }
3872
3873 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003874 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003875 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003876 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003877 if !strings.Contains(libflags, stubPaths[0].String()) {
3878 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3879 }
3880
3881}
Jiyong Park37b25202018-07-11 10:49:27 +09003882
3883func TestRecovery(t *testing.T) {
3884 ctx := testCc(t, `
3885 cc_library_shared {
3886 name: "librecovery",
3887 recovery: true,
3888 }
3889 cc_library_shared {
3890 name: "librecovery32",
3891 recovery: true,
3892 compile_multilib:"32",
3893 }
Jiyong Park5baac542018-08-28 09:55:37 +09003894 cc_library_shared {
3895 name: "libHalInRecovery",
3896 recovery_available: true,
3897 vendor: true,
3898 }
Jiyong Park37b25202018-07-11 10:49:27 +09003899 `)
3900
3901 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003902 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003903 if len(variants) != 1 || !android.InList(arm64, variants) {
3904 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3905 }
3906
3907 variants = ctx.ModuleVariantsForTests("librecovery32")
3908 if android.InList(arm64, variants) {
3909 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3910 }
Jiyong Park5baac542018-08-28 09:55:37 +09003911
3912 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3913 if !recoveryModule.Platform() {
3914 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3915 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003916}
Jiyong Park5baac542018-08-28 09:55:37 +09003917
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003918func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3919 bp := `
3920 cc_prebuilt_test_library_shared {
3921 name: "test_lib",
3922 relative_install_path: "foo/bar/baz",
3923 srcs: ["srcpath/dontusethispath/baz.so"],
3924 }
3925
3926 cc_test {
3927 name: "main_test",
3928 data_libs: ["test_lib"],
3929 gtest: false,
3930 }
3931 `
3932
3933 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3934 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3935 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3936 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3937
3938 ctx := testCcWithConfig(t, config)
3939 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3940 testBinary := module.(*Module).linker.(*testBinary)
3941 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3942 if err != nil {
3943 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3944 }
3945 if len(outputFiles) != 1 {
3946 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3947 }
3948 if len(testBinary.dataPaths()) != 1 {
3949 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3950 }
3951
3952 outputPath := outputFiles[0].String()
3953
3954 if !strings.HasSuffix(outputPath, "/main_test") {
3955 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3956 }
3957 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
3958 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3959 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3960 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3961 }
3962}
3963
Jiyong Park7ed9de32018-10-15 22:25:07 +09003964func TestVersionedStubs(t *testing.T) {
3965 ctx := testCc(t, `
3966 cc_library_shared {
3967 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003968 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003969 stubs: {
3970 symbol_file: "foo.map.txt",
3971 versions: ["1", "2", "3"],
3972 },
3973 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003974
Jiyong Park7ed9de32018-10-15 22:25:07 +09003975 cc_library_shared {
3976 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003977 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003978 shared_libs: ["libFoo#1"],
3979 }`)
3980
3981 variants := ctx.ModuleVariantsForTests("libFoo")
3982 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003983 "android_arm64_armv8-a_shared",
3984 "android_arm64_armv8-a_shared_1",
3985 "android_arm64_armv8-a_shared_2",
3986 "android_arm64_armv8-a_shared_3",
3987 "android_arm_armv7-a-neon_shared",
3988 "android_arm_armv7-a-neon_shared_1",
3989 "android_arm_armv7-a-neon_shared_2",
3990 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003991 }
3992 variantsMismatch := false
3993 if len(variants) != len(expectedVariants) {
3994 variantsMismatch = true
3995 } else {
3996 for _, v := range expectedVariants {
3997 if !inList(v, variants) {
3998 variantsMismatch = false
3999 }
4000 }
4001 }
4002 if variantsMismatch {
4003 t.Errorf("variants of libFoo expected:\n")
4004 for _, v := range expectedVariants {
4005 t.Errorf("%q\n", v)
4006 }
4007 t.Errorf(", but got:\n")
4008 for _, v := range variants {
4009 t.Errorf("%q\n", v)
4010 }
4011 }
4012
Colin Cross7113d202019-11-20 16:39:12 -08004013 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09004014 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08004015 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09004016 if !strings.Contains(libFlags, libFoo1StubPath) {
4017 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
4018 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09004019
Colin Cross7113d202019-11-20 16:39:12 -08004020 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09004021 cFlags := libBarCompileRule.Args["cFlags"]
4022 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
4023 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
4024 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
4025 }
Jiyong Park37b25202018-07-11 10:49:27 +09004026}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004027
Jooyung Hanb04a4992020-03-13 18:57:35 +09004028func TestVersioningMacro(t *testing.T) {
4029 for _, tc := range []struct{ moduleName, expected string }{
4030 {"libc", "__LIBC_API__"},
4031 {"libfoo", "__LIBFOO_API__"},
4032 {"libfoo@1", "__LIBFOO_1_API__"},
4033 {"libfoo-v1", "__LIBFOO_V1_API__"},
4034 {"libfoo.v1", "__LIBFOO_V1_API__"},
4035 } {
4036 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
4037 }
4038}
4039
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004040func TestStaticExecutable(t *testing.T) {
4041 ctx := testCc(t, `
4042 cc_binary {
4043 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01004044 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004045 static_executable: true,
4046 }`)
4047
Colin Cross7113d202019-11-20 16:39:12 -08004048 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004049 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
4050 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07004051 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004052 for _, lib := range systemStaticLibs {
4053 if !strings.Contains(libFlags, lib) {
4054 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
4055 }
4056 }
4057 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
4058 for _, lib := range systemSharedLibs {
4059 if strings.Contains(libFlags, lib) {
4060 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
4061 }
4062 }
4063}
Jiyong Parke4bb9862019-02-01 00:31:10 +09004064
4065func TestStaticDepsOrderWithStubs(t *testing.T) {
4066 ctx := testCc(t, `
4067 cc_binary {
4068 name: "mybin",
4069 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07004070 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09004071 static_executable: true,
4072 stl: "none",
4073 }
4074
4075 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08004076 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09004077 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08004078 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09004079 stl: "none",
4080 }
4081
4082 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08004083 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09004084 srcs: ["foo.c"],
4085 stl: "none",
4086 stubs: {
4087 versions: ["1"],
4088 },
4089 }`)
4090
Colin Cross0de8a1e2020-09-18 14:15:30 -07004091 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
4092 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08004093 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09004094
4095 if !reflect.DeepEqual(actual, expected) {
4096 t.Errorf("staticDeps orderings were not propagated correctly"+
4097 "\nactual: %v"+
4098 "\nexpected: %v",
4099 actual,
4100 expected,
4101 )
4102 }
4103}
Jooyung Han38002912019-05-16 04:01:54 +09004104
Jooyung Hand48f3c32019-08-23 11:18:57 +09004105func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
4106 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
4107 cc_library {
4108 name: "libA",
4109 srcs: ["foo.c"],
4110 shared_libs: ["libB"],
4111 stl: "none",
4112 }
4113
4114 cc_library {
4115 name: "libB",
4116 srcs: ["foo.c"],
4117 enabled: false,
4118 stl: "none",
4119 }
4120 `)
4121}
4122
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004123// Simple smoke test for the cc_fuzz target that ensures the rule compiles
4124// correctly.
4125func TestFuzzTarget(t *testing.T) {
4126 ctx := testCc(t, `
4127 cc_fuzz {
4128 name: "fuzz_smoke_test",
4129 srcs: ["foo.c"],
4130 }`)
4131
Paul Duffin075c4172019-12-19 19:06:13 +00004132 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004133 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
4134}
4135
Jiyong Park29074592019-07-07 16:27:47 +09004136func TestAidl(t *testing.T) {
4137}
4138
Jooyung Han38002912019-05-16 04:01:54 +09004139func assertString(t *testing.T, got, expected string) {
4140 t.Helper()
4141 if got != expected {
4142 t.Errorf("expected %q got %q", expected, got)
4143 }
4144}
4145
4146func assertArrayString(t *testing.T, got, expected []string) {
4147 t.Helper()
4148 if len(got) != len(expected) {
4149 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
4150 return
4151 }
4152 for i := range got {
4153 if got[i] != expected[i] {
4154 t.Errorf("expected %d-th %q (%q) got %q (%q)",
4155 i, expected[i], expected, got[i], got)
4156 return
4157 }
4158 }
4159}
Colin Crosse1bb5d02019-09-24 14:55:04 -07004160
Jooyung Han0302a842019-10-30 18:43:49 +09004161func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
4162 t.Helper()
4163 assertArrayString(t, android.SortedStringKeys(m), expected)
4164}
4165
Colin Crosse1bb5d02019-09-24 14:55:04 -07004166func TestDefaults(t *testing.T) {
4167 ctx := testCc(t, `
4168 cc_defaults {
4169 name: "defaults",
4170 srcs: ["foo.c"],
4171 static: {
4172 srcs: ["bar.c"],
4173 },
4174 shared: {
4175 srcs: ["baz.c"],
4176 },
4177 }
4178
4179 cc_library_static {
4180 name: "libstatic",
4181 defaults: ["defaults"],
4182 }
4183
4184 cc_library_shared {
4185 name: "libshared",
4186 defaults: ["defaults"],
4187 }
4188
4189 cc_library {
4190 name: "libboth",
4191 defaults: ["defaults"],
4192 }
4193
4194 cc_binary {
4195 name: "binary",
4196 defaults: ["defaults"],
4197 }`)
4198
4199 pathsToBase := func(paths android.Paths) []string {
4200 var ret []string
4201 for _, p := range paths {
4202 ret = append(ret, p.Base())
4203 }
4204 return ret
4205 }
4206
Colin Cross7113d202019-11-20 16:39:12 -08004207 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004208 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4209 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
4210 }
Colin Cross7113d202019-11-20 16:39:12 -08004211 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004212 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4213 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
4214 }
Colin Cross7113d202019-11-20 16:39:12 -08004215 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004216 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
4217 t.Errorf("binary ld rule wanted %q, got %q", w, g)
4218 }
4219
Colin Cross7113d202019-11-20 16:39:12 -08004220 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004221 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4222 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
4223 }
Colin Cross7113d202019-11-20 16:39:12 -08004224 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004225 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4226 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
4227 }
4228}
Colin Crosseabaedd2020-02-06 17:01:55 -08004229
4230func TestProductVariableDefaults(t *testing.T) {
4231 bp := `
4232 cc_defaults {
4233 name: "libfoo_defaults",
4234 srcs: ["foo.c"],
4235 cppflags: ["-DFOO"],
4236 product_variables: {
4237 debuggable: {
4238 cppflags: ["-DBAR"],
4239 },
4240 },
4241 }
4242
4243 cc_library {
4244 name: "libfoo",
4245 defaults: ["libfoo_defaults"],
4246 }
4247 `
4248
4249 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4250 config.TestProductVariables.Debuggable = BoolPtr(true)
4251
Colin Crossae8600b2020-10-29 17:09:13 -07004252 ctx := CreateTestContext(config)
Colin Crosseabaedd2020-02-06 17:01:55 -08004253 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
4254 ctx.BottomUp("variable", android.VariableMutator).Parallel()
4255 })
Colin Crossae8600b2020-10-29 17:09:13 -07004256 ctx.Register()
Colin Crosseabaedd2020-02-06 17:01:55 -08004257
4258 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
4259 android.FailIfErrored(t, errs)
4260 _, errs = ctx.PrepareBuildActions(config)
4261 android.FailIfErrored(t, errs)
4262
4263 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
4264 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
4265 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
4266 }
4267}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004268
4269func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4270 t.Parallel()
4271 bp := `
4272 cc_library_static {
4273 name: "libfoo",
4274 srcs: ["foo.c"],
4275 whole_static_libs: ["libbar"],
4276 }
4277
4278 cc_library_static {
4279 name: "libbar",
4280 whole_static_libs: ["libmissing"],
4281 }
4282 `
4283
4284 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4285 config.TestProductVariables.Allow_missing_dependencies = BoolPtr(true)
4286
Colin Crossae8600b2020-10-29 17:09:13 -07004287 ctx := CreateTestContext(config)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004288 ctx.SetAllowMissingDependencies(true)
Colin Crossae8600b2020-10-29 17:09:13 -07004289 ctx.Register()
Colin Crosse4f6eba2020-09-22 18:11:25 -07004290
4291 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
4292 android.FailIfErrored(t, errs)
4293 _, errs = ctx.PrepareBuildActions(config)
4294 android.FailIfErrored(t, errs)
4295
4296 libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
4297 if g, w := libbar.Rule, android.ErrorRule; g != w {
4298 t.Fatalf("Expected libbar rule to be %q, got %q", w, g)
4299 }
4300
4301 if g, w := libbar.Args["error"], "missing dependencies: libmissing"; !strings.Contains(g, w) {
4302 t.Errorf("Expected libbar error to contain %q, was %q", w, g)
4303 }
4304
4305 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
4306 if g, w := libfoo.Inputs.Strings(), libbar.Output.String(); !android.InList(w, g) {
4307 t.Errorf("Expected libfoo.a to depend on %q, got %q", w, g)
4308 }
4309
4310}
Colin Crosse9fe2942020-11-10 18:12:15 -08004311
4312func TestInstallSharedLibs(t *testing.T) {
4313 bp := `
4314 cc_binary {
4315 name: "bin",
4316 host_supported: true,
4317 shared_libs: ["libshared"],
4318 runtime_libs: ["libruntime"],
4319 srcs: [":gen"],
4320 }
4321
4322 cc_library_shared {
4323 name: "libshared",
4324 host_supported: true,
4325 shared_libs: ["libtransitive"],
4326 }
4327
4328 cc_library_shared {
4329 name: "libtransitive",
4330 host_supported: true,
4331 }
4332
4333 cc_library_shared {
4334 name: "libruntime",
4335 host_supported: true,
4336 }
4337
4338 cc_binary_host {
4339 name: "tool",
4340 srcs: ["foo.cpp"],
4341 }
4342
4343 genrule {
4344 name: "gen",
4345 tools: ["tool"],
4346 out: ["gen.cpp"],
4347 cmd: "$(location tool) $(out)",
4348 }
4349 `
4350
4351 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4352 ctx := testCcWithConfig(t, config)
4353
4354 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4355 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4356 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4357 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4358 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4359
4360 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4361 t.Errorf("expected host bin dependency %q, got %q", w, g)
4362 }
4363
4364 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4365 t.Errorf("expected host bin dependency %q, got %q", w, g)
4366 }
4367
4368 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4369 t.Errorf("expected host bin dependency %q, got %q", w, g)
4370 }
4371
4372 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4373 t.Errorf("expected host bin dependency %q, got %q", w, g)
4374 }
4375
4376 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4377 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4378 }
4379
4380 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4381 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4382 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4383 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4384
4385 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4386 t.Errorf("expected device bin dependency %q, got %q", w, g)
4387 }
4388
4389 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4390 t.Errorf("expected device bin dependency %q, got %q", w, g)
4391 }
4392
4393 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4394 t.Errorf("expected device bin dependency %q, got %q", w, g)
4395 }
4396
4397 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4398 t.Errorf("expected device bin dependency %q, got %q", w, g)
4399 }
4400
4401 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4402 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4403 }
4404
4405}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004406
4407func TestStubsLibReexportsHeaders(t *testing.T) {
4408 ctx := testCc(t, `
4409 cc_library_shared {
4410 name: "libclient",
4411 srcs: ["foo.c"],
4412 shared_libs: ["libfoo#1"],
4413 }
4414
4415 cc_library_shared {
4416 name: "libfoo",
4417 srcs: ["foo.c"],
4418 shared_libs: ["libbar"],
4419 export_shared_lib_headers: ["libbar"],
4420 stubs: {
4421 symbol_file: "foo.map.txt",
4422 versions: ["1", "2", "3"],
4423 },
4424 }
4425
4426 cc_library_shared {
4427 name: "libbar",
4428 export_include_dirs: ["include/libbar"],
4429 srcs: ["foo.c"],
4430 }`)
4431
4432 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4433
4434 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4435 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4436 }
4437}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004438
4439func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
4440 ctx := testCc(t, `
4441 cc_library {
4442 name: "libfoo",
4443 srcs: ["a/Foo.aidl"],
4444 aidl: { flags: ["-Werror"], },
4445 }
4446 `)
4447
4448 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4449 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4450 aidlCommand := manifest.Commands[0].GetCommand()
4451 expectedAidlFlag := "-Werror"
4452 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4453 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4454 }
4455}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004456
4457func checkHasImplicitDep(t *testing.T, m android.TestingModule, name string) {
4458 implicits := m.Rule("ld").Implicits
4459 for _, lib := range implicits {
4460 if strings.Contains(lib.Rel(), name) {
4461 return
4462 }
4463 }
4464
4465 t.Errorf("%q is not found in implicit deps of module %q", name, m.Module().(*Module).Name())
4466}
4467
4468func checkDoesNotHaveImplicitDep(t *testing.T, m android.TestingModule, name string) {
4469 implicits := m.Rule("ld").Implicits
4470 for _, lib := range implicits {
4471 if strings.Contains(lib.Rel(), name) {
4472 t.Errorf("%q is found in implicit deps of module %q", name, m.Module().(*Module).Name())
4473 }
4474 }
4475}
4476
4477func TestSanitizeMemtagHeap(t *testing.T) {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08004478 rootBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004479 cc_library_static {
4480 name: "libstatic",
4481 sanitize: { memtag_heap: true },
4482 }
4483
4484 cc_library_shared {
4485 name: "libshared",
4486 sanitize: { memtag_heap: true },
4487 }
4488
4489 cc_library {
4490 name: "libboth",
4491 sanitize: { memtag_heap: true },
4492 }
4493
4494 cc_binary {
4495 name: "binary",
4496 shared_libs: [ "libshared" ],
4497 static_libs: [ "libstatic" ],
4498 }
4499
4500 cc_binary {
4501 name: "binary_true",
4502 sanitize: { memtag_heap: true },
4503 }
4504
4505 cc_binary {
4506 name: "binary_true_sync",
4507 sanitize: { memtag_heap: true, diag: { memtag_heap: true }, },
4508 }
4509
4510 cc_binary {
4511 name: "binary_false",
4512 sanitize: { memtag_heap: false },
4513 }
4514
4515 cc_test {
4516 name: "test",
4517 gtest: false,
4518 }
4519
4520 cc_test {
4521 name: "test_true",
4522 gtest: false,
4523 sanitize: { memtag_heap: true },
4524 }
4525
4526 cc_test {
4527 name: "test_false",
4528 gtest: false,
4529 sanitize: { memtag_heap: false },
4530 }
4531
4532 cc_test {
4533 name: "test_true_async",
4534 gtest: false,
4535 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
4536 }
4537
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08004538 `
4539
4540 subdirAsyncBp := `
4541 cc_binary {
4542 name: "binary_async",
4543 }
4544 `
4545
4546 subdirSyncBp := `
4547 cc_binary {
4548 name: "binary_sync",
4549 }
4550 `
4551
4552 mockFS := map[string][]byte{
4553 "subdir_async/Android.bp": []byte(subdirAsyncBp),
4554 "subdir_sync/Android.bp": []byte(subdirSyncBp),
4555 }
4556
4557 config := TestConfig(buildDir, android.Android, nil, rootBp, mockFS)
4558 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
4559 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
4560 config.TestProductVariables.MemtagHeapAsyncIncludePaths = []string{"subdir_async"}
4561 config.TestProductVariables.MemtagHeapSyncIncludePaths = []string{"subdir_sync"}
4562 ctx := CreateTestContext(config)
4563 ctx.Register()
4564
4565 _, errs := ctx.ParseFileList(".", []string{"Android.bp", "subdir_sync/Android.bp", "subdir_async/Android.bp"})
4566 android.FailIfErrored(t, errs)
4567 _, errs = ctx.PrepareBuildActions(config)
4568 android.FailIfErrored(t, errs)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004569
4570 variant := "android_arm64_armv8-a"
4571 note_async := "note_memtag_heap_async"
4572 note_sync := "note_memtag_heap_sync"
4573 note_any := "note_memtag_"
4574
4575 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared"), note_any)
4576 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared"), note_any)
4577
4578 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("binary", variant), note_any)
4579 checkHasImplicitDep(t, ctx.ModuleForTests("binary_true", variant), note_async)
4580 checkHasImplicitDep(t, ctx.ModuleForTests("binary_true_sync", variant), note_sync)
4581 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("binary_false", variant), note_any)
4582
4583 checkHasImplicitDep(t, ctx.ModuleForTests("test", variant), note_sync)
4584 checkHasImplicitDep(t, ctx.ModuleForTests("test_true", variant), note_async)
4585 checkDoesNotHaveImplicitDep(t, ctx.ModuleForTests("test_false", variant), note_any)
4586 checkHasImplicitDep(t, ctx.ModuleForTests("test_true_async", variant), note_async)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08004587
4588 checkHasImplicitDep(t, ctx.ModuleForTests("binary_async", variant), note_async)
4589 checkHasImplicitDep(t, ctx.ModuleForTests("binary_sync", variant), note_sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004590}