blob: 6df68cb67f31aac10b4aa5fc0efa8f80bdde33c6 [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
2025 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
2026 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2027 cc_library {
2028 name: "libdoubleloadable",
2029 vendor_available: true,
2030 double_loadable: true,
2031 shared_libs: ["libnondoubleloadable"],
2032 }
2033
2034 cc_library {
2035 name: "libnondoubleloadable",
2036 vendor_available: true,
2037 }
2038 `)
2039
2040 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
2041 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2042 cc_library {
2043 name: "libdoubleloadable",
2044 vendor_available: true,
2045 double_loadable: true,
2046 shared_libs: ["libnondoubleloadable"],
2047 }
2048
2049 cc_library {
2050 name: "libnondoubleloadable",
2051 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002052 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002053 vndk: {
2054 enabled: true,
2055 },
2056 }
2057 `)
2058
2059 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
2060 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2061 cc_library {
2062 name: "libdoubleloadable",
2063 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002064 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002065 vndk: {
2066 enabled: true,
2067 },
2068 double_loadable: true,
2069 shared_libs: ["libnondoubleloadable"],
2070 }
2071
2072 cc_library {
2073 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09002074 vendor_available: true,
2075 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002076 vndk: {
2077 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002078 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002079 },
2080 }
2081 `)
2082
2083 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
2084 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2085 cc_library {
2086 name: "libllndk",
2087 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07002088 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002089 }
2090
2091 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002092 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002093 symbol_file: "",
2094 }
2095
2096 cc_library {
2097 name: "libcoreonly",
2098 shared_libs: ["libvendoravailable"],
2099 }
2100
2101 // indirect dependency of LLNDK
2102 cc_library {
2103 name: "libvendoravailable",
2104 vendor_available: true,
2105 }
2106 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08002107}
2108
Jooyung Han479ca172020-10-19 18:51:07 +09002109func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
2110 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
2111 cc_library {
2112 name: "libvndksp",
2113 shared_libs: ["libanothervndksp"],
2114 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002115 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09002116 vndk: {
2117 enabled: true,
2118 support_system_process: true,
2119 }
2120 }
2121
2122 cc_library {
2123 name: "libllndk",
2124 shared_libs: ["libanothervndksp"],
2125 }
2126
2127 llndk_library {
2128 name: "libllndk",
2129 symbol_file: "",
2130 }
2131
2132 cc_library {
2133 name: "libanothervndksp",
2134 vendor_available: true,
2135 }
2136 `)
2137}
2138
Logan Chienf3511742017-10-31 18:04:35 +08002139func TestVndkExt(t *testing.T) {
2140 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09002141 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08002142 cc_library {
2143 name: "libvndk",
2144 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002145 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002146 vndk: {
2147 enabled: true,
2148 },
2149 nocrt: true,
2150 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002151 cc_library {
2152 name: "libvndk2",
2153 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002154 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09002155 vndk: {
2156 enabled: true,
2157 },
2158 target: {
2159 vendor: {
2160 suffix: "-suffix",
2161 },
Justin Yun63e9ec72020-10-29 16:49:43 +09002162 product: {
2163 suffix: "-suffix",
2164 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09002165 },
2166 nocrt: true,
2167 }
Logan Chienf3511742017-10-31 18:04:35 +08002168
2169 cc_library {
2170 name: "libvndk_ext",
2171 vendor: true,
2172 vndk: {
2173 enabled: true,
2174 extends: "libvndk",
2175 },
2176 nocrt: true,
2177 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002178
2179 cc_library {
2180 name: "libvndk2_ext",
2181 vendor: true,
2182 vndk: {
2183 enabled: true,
2184 extends: "libvndk2",
2185 },
2186 nocrt: true,
2187 }
Logan Chienf3511742017-10-31 18:04:35 +08002188
Justin Yun0ecf0b22020-02-28 15:07:59 +09002189 cc_library {
2190 name: "libvndk_ext_product",
2191 product_specific: true,
2192 vndk: {
2193 enabled: true,
2194 extends: "libvndk",
2195 },
2196 nocrt: true,
2197 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002198
Justin Yun0ecf0b22020-02-28 15:07:59 +09002199 cc_library {
2200 name: "libvndk2_ext_product",
2201 product_specific: true,
2202 vndk: {
2203 enabled: true,
2204 extends: "libvndk2",
2205 },
2206 nocrt: true,
2207 }
2208 `
2209 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2210 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2211 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2212 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2213
2214 ctx := testCcWithConfig(t, config)
2215
2216 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
2217 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
2218
2219 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
2220 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
2221
2222 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
2223 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08002224}
2225
Logan Chiend3c59a22018-03-29 14:08:15 +08002226func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08002227 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
2228 ctx := testCcNoVndk(t, `
2229 cc_library {
2230 name: "libvndk",
2231 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002232 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002233 vndk: {
2234 enabled: true,
2235 },
2236 nocrt: true,
2237 }
2238
2239 cc_library {
2240 name: "libvndk_ext",
2241 vendor: true,
2242 vndk: {
2243 enabled: true,
2244 extends: "libvndk",
2245 },
2246 nocrt: true,
2247 }
2248 `)
2249
2250 // Ensures that the core variant of "libvndk_ext" can be found.
2251 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
2252 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
2253 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
2254 }
2255}
2256
Justin Yun0ecf0b22020-02-28 15:07:59 +09002257func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
2258 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09002259 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09002260 cc_library {
2261 name: "libvndk",
2262 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002263 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002264 vndk: {
2265 enabled: true,
2266 },
2267 nocrt: true,
2268 }
2269
2270 cc_library {
2271 name: "libvndk_ext_product",
2272 product_specific: true,
2273 vndk: {
2274 enabled: true,
2275 extends: "libvndk",
2276 },
2277 nocrt: true,
2278 }
2279 `)
2280
2281 // Ensures that the core variant of "libvndk_ext_product" can be found.
2282 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
2283 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
2284 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
2285 }
2286}
2287
Logan Chienf3511742017-10-31 18:04:35 +08002288func TestVndkExtError(t *testing.T) {
2289 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09002290 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08002291 cc_library {
2292 name: "libvndk",
2293 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002294 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002295 vndk: {
2296 enabled: true,
2297 },
2298 nocrt: true,
2299 }
2300
2301 cc_library {
2302 name: "libvndk_ext",
2303 vndk: {
2304 enabled: true,
2305 extends: "libvndk",
2306 },
2307 nocrt: true,
2308 }
2309 `)
2310
2311 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
2312 cc_library {
2313 name: "libvndk",
2314 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002315 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002316 vndk: {
2317 enabled: true,
2318 },
2319 nocrt: true,
2320 }
2321
2322 cc_library {
2323 name: "libvndk_ext",
2324 vendor: true,
2325 vndk: {
2326 enabled: true,
2327 },
2328 nocrt: true,
2329 }
2330 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002331
2332 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
2333 cc_library {
2334 name: "libvndk",
2335 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002336 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002337 vndk: {
2338 enabled: true,
2339 },
2340 nocrt: true,
2341 }
2342
2343 cc_library {
2344 name: "libvndk_ext_product",
2345 product_specific: true,
2346 vndk: {
2347 enabled: true,
2348 },
2349 nocrt: true,
2350 }
2351 `)
2352
2353 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
2354 cc_library {
2355 name: "libvndk",
2356 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002357 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002358 vndk: {
2359 enabled: true,
2360 },
2361 nocrt: true,
2362 }
2363
2364 cc_library {
2365 name: "libvndk_ext_product",
2366 product_specific: true,
2367 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002368 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002369 vndk: {
2370 enabled: true,
2371 extends: "libvndk",
2372 },
2373 nocrt: true,
2374 }
2375 `)
Logan Chienf3511742017-10-31 18:04:35 +08002376}
2377
2378func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
2379 // This test ensures an error is emitted for inconsistent support_system_process.
2380 testCcError(t, "module \".*\" with mismatched support_system_process", `
2381 cc_library {
2382 name: "libvndk",
2383 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002384 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002385 vndk: {
2386 enabled: true,
2387 },
2388 nocrt: true,
2389 }
2390
2391 cc_library {
2392 name: "libvndk_sp_ext",
2393 vendor: true,
2394 vndk: {
2395 enabled: true,
2396 extends: "libvndk",
2397 support_system_process: true,
2398 },
2399 nocrt: true,
2400 }
2401 `)
2402
2403 testCcError(t, "module \".*\" with mismatched support_system_process", `
2404 cc_library {
2405 name: "libvndk_sp",
2406 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002407 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002408 vndk: {
2409 enabled: true,
2410 support_system_process: true,
2411 },
2412 nocrt: true,
2413 }
2414
2415 cc_library {
2416 name: "libvndk_ext",
2417 vendor: true,
2418 vndk: {
2419 enabled: true,
2420 extends: "libvndk_sp",
2421 },
2422 nocrt: true,
2423 }
2424 `)
2425}
2426
2427func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08002428 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09002429 // with `private: true`.
2430 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08002431 cc_library {
2432 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09002433 vendor_available: true,
2434 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002435 vndk: {
2436 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002437 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08002438 },
2439 nocrt: true,
2440 }
2441
2442 cc_library {
2443 name: "libvndk_ext",
2444 vendor: true,
2445 vndk: {
2446 enabled: true,
2447 extends: "libvndk",
2448 },
2449 nocrt: true,
2450 }
2451 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002452
Justin Yunfd9e8042020-12-23 18:23:14 +09002453 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09002454 cc_library {
2455 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09002456 vendor_available: true,
2457 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002458 vndk: {
2459 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002460 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002461 },
2462 nocrt: true,
2463 }
2464
2465 cc_library {
2466 name: "libvndk_ext_product",
2467 product_specific: true,
2468 vndk: {
2469 enabled: true,
2470 extends: "libvndk",
2471 },
2472 nocrt: true,
2473 }
2474 `)
Logan Chienf3511742017-10-31 18:04:35 +08002475}
2476
Logan Chiend3c59a22018-03-29 14:08:15 +08002477func TestVendorModuleUseVndkExt(t *testing.T) {
2478 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002479 testCc(t, `
2480 cc_library {
2481 name: "libvndk",
2482 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002483 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002484 vndk: {
2485 enabled: true,
2486 },
2487 nocrt: true,
2488 }
2489
2490 cc_library {
2491 name: "libvndk_ext",
2492 vendor: true,
2493 vndk: {
2494 enabled: true,
2495 extends: "libvndk",
2496 },
2497 nocrt: true,
2498 }
2499
2500 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08002501 name: "libvndk_sp",
2502 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002503 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002504 vndk: {
2505 enabled: true,
2506 support_system_process: true,
2507 },
2508 nocrt: true,
2509 }
2510
2511 cc_library {
2512 name: "libvndk_sp_ext",
2513 vendor: true,
2514 vndk: {
2515 enabled: true,
2516 extends: "libvndk_sp",
2517 support_system_process: true,
2518 },
2519 nocrt: true,
2520 }
2521
2522 cc_library {
2523 name: "libvendor",
2524 vendor: true,
2525 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
2526 nocrt: true,
2527 }
2528 `)
2529}
2530
Logan Chiend3c59a22018-03-29 14:08:15 +08002531func TestVndkExtUseVendorLib(t *testing.T) {
2532 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08002533 testCc(t, `
2534 cc_library {
2535 name: "libvndk",
2536 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002537 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002538 vndk: {
2539 enabled: true,
2540 },
2541 nocrt: true,
2542 }
2543
2544 cc_library {
2545 name: "libvndk_ext",
2546 vendor: true,
2547 vndk: {
2548 enabled: true,
2549 extends: "libvndk",
2550 },
2551 shared_libs: ["libvendor"],
2552 nocrt: true,
2553 }
2554
2555 cc_library {
2556 name: "libvendor",
2557 vendor: true,
2558 nocrt: true,
2559 }
2560 `)
Logan Chienf3511742017-10-31 18:04:35 +08002561
Logan Chiend3c59a22018-03-29 14:08:15 +08002562 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
2563 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08002564 cc_library {
2565 name: "libvndk_sp",
2566 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002567 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002568 vndk: {
2569 enabled: true,
2570 support_system_process: true,
2571 },
2572 nocrt: true,
2573 }
2574
2575 cc_library {
2576 name: "libvndk_sp_ext",
2577 vendor: true,
2578 vndk: {
2579 enabled: true,
2580 extends: "libvndk_sp",
2581 support_system_process: true,
2582 },
2583 shared_libs: ["libvendor"], // Cause an error
2584 nocrt: true,
2585 }
2586
2587 cc_library {
2588 name: "libvendor",
2589 vendor: true,
2590 nocrt: true,
2591 }
2592 `)
2593}
2594
Justin Yun0ecf0b22020-02-28 15:07:59 +09002595func TestProductVndkExtDependency(t *testing.T) {
2596 bp := `
2597 cc_library {
2598 name: "libvndk",
2599 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002600 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002601 vndk: {
2602 enabled: true,
2603 },
2604 nocrt: true,
2605 }
2606
2607 cc_library {
2608 name: "libvndk_ext_product",
2609 product_specific: true,
2610 vndk: {
2611 enabled: true,
2612 extends: "libvndk",
2613 },
2614 shared_libs: ["libproduct_for_vndklibs"],
2615 nocrt: true,
2616 }
2617
2618 cc_library {
2619 name: "libvndk_sp",
2620 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002621 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002622 vndk: {
2623 enabled: true,
2624 support_system_process: true,
2625 },
2626 nocrt: true,
2627 }
2628
2629 cc_library {
2630 name: "libvndk_sp_ext_product",
2631 product_specific: true,
2632 vndk: {
2633 enabled: true,
2634 extends: "libvndk_sp",
2635 support_system_process: true,
2636 },
2637 shared_libs: ["libproduct_for_vndklibs"],
2638 nocrt: true,
2639 }
2640
2641 cc_library {
2642 name: "libproduct",
2643 product_specific: true,
2644 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
2645 nocrt: true,
2646 }
2647
2648 cc_library {
2649 name: "libproduct_for_vndklibs",
2650 product_specific: true,
2651 nocrt: true,
2652 }
2653 `
2654 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2655 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2656 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2657 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2658
2659 testCcWithConfig(t, config)
2660}
2661
Logan Chiend3c59a22018-03-29 14:08:15 +08002662func TestVndkSpExtUseVndkError(t *testing.T) {
2663 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
2664 // library.
2665 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2666 cc_library {
2667 name: "libvndk",
2668 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002669 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002670 vndk: {
2671 enabled: true,
2672 },
2673 nocrt: true,
2674 }
2675
2676 cc_library {
2677 name: "libvndk_sp",
2678 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002679 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002680 vndk: {
2681 enabled: true,
2682 support_system_process: true,
2683 },
2684 nocrt: true,
2685 }
2686
2687 cc_library {
2688 name: "libvndk_sp_ext",
2689 vendor: true,
2690 vndk: {
2691 enabled: true,
2692 extends: "libvndk_sp",
2693 support_system_process: true,
2694 },
2695 shared_libs: ["libvndk"], // Cause an error
2696 nocrt: true,
2697 }
2698 `)
2699
2700 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
2701 // library.
2702 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2703 cc_library {
2704 name: "libvndk",
2705 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002706 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002707 vndk: {
2708 enabled: true,
2709 },
2710 nocrt: true,
2711 }
2712
2713 cc_library {
2714 name: "libvndk_ext",
2715 vendor: true,
2716 vndk: {
2717 enabled: true,
2718 extends: "libvndk",
2719 },
2720 nocrt: true,
2721 }
2722
2723 cc_library {
2724 name: "libvndk_sp",
2725 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002726 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002727 vndk: {
2728 enabled: true,
2729 support_system_process: true,
2730 },
2731 nocrt: true,
2732 }
2733
2734 cc_library {
2735 name: "libvndk_sp_ext",
2736 vendor: true,
2737 vndk: {
2738 enabled: true,
2739 extends: "libvndk_sp",
2740 support_system_process: true,
2741 },
2742 shared_libs: ["libvndk_ext"], // Cause an error
2743 nocrt: true,
2744 }
2745 `)
2746}
2747
2748func TestVndkUseVndkExtError(t *testing.T) {
2749 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2750 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002751 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2752 cc_library {
2753 name: "libvndk",
2754 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002755 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002756 vndk: {
2757 enabled: true,
2758 },
2759 nocrt: true,
2760 }
2761
2762 cc_library {
2763 name: "libvndk_ext",
2764 vendor: true,
2765 vndk: {
2766 enabled: true,
2767 extends: "libvndk",
2768 },
2769 nocrt: true,
2770 }
2771
2772 cc_library {
2773 name: "libvndk2",
2774 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002775 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002776 vndk: {
2777 enabled: true,
2778 },
2779 shared_libs: ["libvndk_ext"],
2780 nocrt: true,
2781 }
2782 `)
2783
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002784 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002785 cc_library {
2786 name: "libvndk",
2787 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002788 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002789 vndk: {
2790 enabled: true,
2791 },
2792 nocrt: true,
2793 }
2794
2795 cc_library {
2796 name: "libvndk_ext",
2797 vendor: true,
2798 vndk: {
2799 enabled: true,
2800 extends: "libvndk",
2801 },
2802 nocrt: true,
2803 }
2804
2805 cc_library {
2806 name: "libvndk2",
2807 vendor_available: true,
2808 vndk: {
2809 enabled: true,
2810 },
2811 target: {
2812 vendor: {
2813 shared_libs: ["libvndk_ext"],
2814 },
2815 },
2816 nocrt: true,
2817 }
2818 `)
2819
2820 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2821 cc_library {
2822 name: "libvndk_sp",
2823 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002824 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002825 vndk: {
2826 enabled: true,
2827 support_system_process: 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 support_system_process: true,
2839 },
2840 nocrt: true,
2841 }
2842
2843 cc_library {
2844 name: "libvndk_sp_2",
2845 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002846 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002847 vndk: {
2848 enabled: true,
2849 support_system_process: true,
2850 },
2851 shared_libs: ["libvndk_sp_ext"],
2852 nocrt: true,
2853 }
2854 `)
2855
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002856 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002857 cc_library {
2858 name: "libvndk_sp",
2859 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002860 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002861 vndk: {
2862 enabled: true,
2863 },
2864 nocrt: true,
2865 }
2866
2867 cc_library {
2868 name: "libvndk_sp_ext",
2869 vendor: true,
2870 vndk: {
2871 enabled: true,
2872 extends: "libvndk_sp",
2873 },
2874 nocrt: true,
2875 }
2876
2877 cc_library {
2878 name: "libvndk_sp2",
2879 vendor_available: true,
2880 vndk: {
2881 enabled: true,
2882 },
2883 target: {
2884 vendor: {
2885 shared_libs: ["libvndk_sp_ext"],
2886 },
2887 },
2888 nocrt: true,
2889 }
2890 `)
2891}
2892
Justin Yun5f7f7e82019-11-18 19:52:14 +09002893func TestEnforceProductVndkVersion(t *testing.T) {
2894 bp := `
2895 cc_library {
2896 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002897 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002898 }
2899 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002900 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002901 symbol_file: "",
2902 }
2903 cc_library {
2904 name: "libvndk",
2905 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002906 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002907 vndk: {
2908 enabled: true,
2909 },
2910 nocrt: true,
2911 }
2912 cc_library {
2913 name: "libvndk_sp",
2914 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002915 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002916 vndk: {
2917 enabled: true,
2918 support_system_process: true,
2919 },
2920 nocrt: true,
2921 }
2922 cc_library {
2923 name: "libva",
2924 vendor_available: true,
2925 nocrt: true,
2926 }
2927 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002928 name: "libpa",
2929 product_available: true,
2930 nocrt: true,
2931 }
2932 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002933 name: "libboth_available",
2934 vendor_available: true,
2935 product_available: true,
2936 nocrt: true,
2937 target: {
2938 vendor: {
2939 suffix: "-vendor",
2940 },
2941 product: {
2942 suffix: "-product",
2943 },
2944 }
2945 }
2946 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002947 name: "libproduct_va",
2948 product_specific: true,
2949 vendor_available: true,
2950 nocrt: true,
2951 }
2952 cc_library {
2953 name: "libprod",
2954 product_specific: true,
2955 shared_libs: [
2956 "libllndk",
2957 "libvndk",
2958 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002959 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002960 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002961 "libproduct_va",
2962 ],
2963 nocrt: true,
2964 }
2965 cc_library {
2966 name: "libvendor",
2967 vendor: true,
2968 shared_libs: [
2969 "libllndk",
2970 "libvndk",
2971 "libvndk_sp",
2972 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002973 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002974 "libproduct_va",
2975 ],
2976 nocrt: true,
2977 }
2978 `
2979
2980 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2981 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2982 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2983 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2984
2985 ctx := testCcWithConfig(t, config)
2986
Jooyung Han261e1582020-10-20 18:54:21 +09002987 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2988 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002989
2990 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2991 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2992
2993 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2994 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002995}
2996
2997func TestEnforceProductVndkVersionErrors(t *testing.T) {
2998 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2999 cc_library {
3000 name: "libprod",
3001 product_specific: true,
3002 shared_libs: [
3003 "libvendor",
3004 ],
3005 nocrt: true,
3006 }
3007 cc_library {
3008 name: "libvendor",
3009 vendor: true,
3010 nocrt: true,
3011 }
3012 `)
3013 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
3014 cc_library {
3015 name: "libprod",
3016 product_specific: true,
3017 shared_libs: [
3018 "libsystem",
3019 ],
3020 nocrt: true,
3021 }
3022 cc_library {
3023 name: "libsystem",
3024 nocrt: true,
3025 }
3026 `)
Justin Yun6977e8a2020-10-29 18:24:11 +09003027 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
3028 cc_library {
3029 name: "libprod",
3030 product_specific: true,
3031 shared_libs: [
3032 "libva",
3033 ],
3034 nocrt: true,
3035 }
3036 cc_library {
3037 name: "libva",
3038 vendor_available: true,
3039 nocrt: true,
3040 }
3041 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09003042 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09003043 cc_library {
3044 name: "libprod",
3045 product_specific: true,
3046 shared_libs: [
3047 "libvndk_private",
3048 ],
3049 nocrt: true,
3050 }
3051 cc_library {
3052 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09003053 vendor_available: true,
3054 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09003055 vndk: {
3056 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09003057 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09003058 },
3059 nocrt: true,
3060 }
3061 `)
3062 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
3063 cc_library {
3064 name: "libprod",
3065 product_specific: true,
3066 shared_libs: [
3067 "libsystem_ext",
3068 ],
3069 nocrt: true,
3070 }
3071 cc_library {
3072 name: "libsystem_ext",
3073 system_ext_specific: true,
3074 nocrt: true,
3075 }
3076 `)
3077 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
3078 cc_library {
3079 name: "libsystem",
3080 shared_libs: [
3081 "libproduct_va",
3082 ],
3083 nocrt: true,
3084 }
3085 cc_library {
3086 name: "libproduct_va",
3087 product_specific: true,
3088 vendor_available: true,
3089 nocrt: true,
3090 }
3091 `)
3092}
3093
Jooyung Han38002912019-05-16 04:01:54 +09003094func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08003095 bp := `
3096 cc_library {
3097 name: "libvndk",
3098 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003099 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003100 vndk: {
3101 enabled: true,
3102 },
3103 }
3104 cc_library {
3105 name: "libvndksp",
3106 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003107 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003108 vndk: {
3109 enabled: true,
3110 support_system_process: true,
3111 },
3112 }
3113 cc_library {
3114 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09003115 vendor_available: true,
3116 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003117 vndk: {
3118 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09003119 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003120 },
3121 }
3122 cc_library {
3123 name: "libvendor",
3124 vendor: true,
3125 }
3126 cc_library {
3127 name: "libvndkext",
3128 vendor: true,
3129 vndk: {
3130 enabled: true,
3131 extends: "libvndk",
3132 },
3133 }
3134 vndk_prebuilt_shared {
3135 name: "prevndk",
3136 version: "27",
3137 target_arch: "arm",
3138 binder32bit: true,
3139 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003140 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003141 vndk: {
3142 enabled: true,
3143 },
3144 arch: {
3145 arm: {
3146 srcs: ["liba.so"],
3147 },
3148 },
3149 }
3150 cc_library {
3151 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07003152 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003153 }
3154 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003155 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003156 symbol_file: "",
3157 }
3158 cc_library {
3159 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07003160 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003161 }
3162 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003163 name: "libllndkprivate.llndk",
Justin Yunc0d8c492021-01-07 17:45:31 +09003164 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003165 symbol_file: "",
Colin Cross78212242021-01-06 14:51:30 -08003166 }
3167
3168 llndk_libraries_txt {
3169 name: "llndk.libraries.txt",
3170 }
3171 vndkcore_libraries_txt {
3172 name: "vndkcore.libraries.txt",
3173 }
3174 vndksp_libraries_txt {
3175 name: "vndksp.libraries.txt",
3176 }
3177 vndkprivate_libraries_txt {
3178 name: "vndkprivate.libraries.txt",
3179 }
3180 vndkcorevariant_libraries_txt {
3181 name: "vndkcorevariant.libraries.txt",
3182 insert_vndk_version: false,
3183 }
3184 `
Colin Cross98be1bb2019-12-13 20:41:13 -08003185
3186 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09003187 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3188 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3189 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08003190 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09003191
Colin Cross78212242021-01-06 14:51:30 -08003192 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
3193 []string{"libvndk.so", "libvndkprivate.so"})
3194 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
3195 []string{"libc++.so", "libvndksp.so"})
3196 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
3197 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
3198 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
3199 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09003200
Colin Crossfb0c16e2019-11-20 17:12:35 -08003201 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09003202
Jooyung Han38002912019-05-16 04:01:54 +09003203 tests := []struct {
3204 variant string
3205 name string
3206 expected string
3207 }{
3208 {vendorVariant, "libvndk", "native:vndk"},
3209 {vendorVariant, "libvndksp", "native:vndk"},
3210 {vendorVariant, "libvndkprivate", "native:vndk_private"},
3211 {vendorVariant, "libvendor", "native:vendor"},
3212 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08003213 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09003214 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09003215 {coreVariant, "libvndk", "native:platform"},
3216 {coreVariant, "libvndkprivate", "native:platform"},
3217 {coreVariant, "libllndk", "native:platform"},
3218 }
3219 for _, test := range tests {
3220 t.Run(test.name, func(t *testing.T) {
3221 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
3222 assertString(t, module.makeLinkType, test.expected)
3223 })
3224 }
3225}
3226
Jeff Gaston294356f2017-09-27 17:05:30 -07003227var staticLinkDepOrderTestCases = []struct {
3228 // This is a string representation of a map[moduleName][]moduleDependency .
3229 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003230 inStatic string
3231
3232 // This is a string representation of a map[moduleName][]moduleDependency .
3233 // It models the dependencies declared in an Android.bp file.
3234 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07003235
3236 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
3237 // The keys of allOrdered specify which modules we would like to check.
3238 // The values of allOrdered specify the expected result (of the transitive closure of all
3239 // dependencies) for each module to test
3240 allOrdered string
3241
3242 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
3243 // The keys of outOrdered specify which modules we would like to check.
3244 // The values of outOrdered specify the expected result (of the ordered linker command line)
3245 // for each module to test.
3246 outOrdered string
3247}{
3248 // Simple tests
3249 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003250 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07003251 outOrdered: "",
3252 },
3253 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003254 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003255 outOrdered: "a:",
3256 },
3257 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003258 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003259 outOrdered: "a:b; b:",
3260 },
3261 // Tests of reordering
3262 {
3263 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003264 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003265 outOrdered: "a:b,c,d; b:d; c:d; d:",
3266 },
3267 {
3268 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003269 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003270 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
3271 },
3272 {
3273 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003274 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07003275 outOrdered: "a:d,b,e,c; d:b; e:c",
3276 },
3277 {
3278 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003279 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07003280 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
3281 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
3282 },
3283 {
3284 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003285 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 -07003286 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
3287 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
3288 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003289 // shared dependencies
3290 {
3291 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
3292 // So, we don't actually have to check that a shared dependency of c will change the order
3293 // of a library that depends statically on b and on c. We only need to check that if c has
3294 // a shared dependency on b, that that shows up in allOrdered.
3295 inShared: "c:b",
3296 allOrdered: "c:b",
3297 outOrdered: "c:",
3298 },
3299 {
3300 // This test doesn't actually include any shared dependencies but it's a reminder of what
3301 // the second phase of the above test would look like
3302 inStatic: "a:b,c; c:b",
3303 allOrdered: "a:c,b; c:b",
3304 outOrdered: "a:c,b; c:b",
3305 },
Jeff Gaston294356f2017-09-27 17:05:30 -07003306 // tiebreakers for when two modules specifying different orderings and there is no dependency
3307 // to dictate an order
3308 {
3309 // 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 -08003310 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07003311 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
3312 },
3313 {
3314 // 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 -08003315 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 -07003316 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
3317 },
3318 // Tests involving duplicate dependencies
3319 {
3320 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003321 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003322 outOrdered: "a:c,b",
3323 },
3324 {
3325 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003326 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003327 outOrdered: "a:d,c,b",
3328 },
3329 // Tests to confirm the nonexistence of infinite loops.
3330 // These cases should never happen, so as long as the test terminates and the
3331 // result is deterministic then that should be fine.
3332 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003333 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003334 outOrdered: "a:a",
3335 },
3336 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003337 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003338 allOrdered: "a:b,c; b:c,a; c:a,b",
3339 outOrdered: "a:b; b:c; c:a",
3340 },
3341 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003342 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003343 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
3344 outOrdered: "a:c,b; b:a,c; c:b,a",
3345 },
3346}
3347
3348// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
3349func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
3350 // convert from "a:b,c; d:e" to "a:b,c;d:e"
3351 strippedText := strings.Replace(text, " ", "", -1)
3352 if len(strippedText) < 1 {
3353 return []android.Path{}, make(map[android.Path][]android.Path, 0)
3354 }
3355 allDeps = make(map[android.Path][]android.Path, 0)
3356
3357 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
3358 moduleTexts := strings.Split(strippedText, ";")
3359
3360 outputForModuleName := func(moduleName string) android.Path {
3361 return android.PathForTesting(moduleName)
3362 }
3363
3364 for _, moduleText := range moduleTexts {
3365 // convert from "a:b,c" to ["a", "b,c"]
3366 components := strings.Split(moduleText, ":")
3367 if len(components) != 2 {
3368 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
3369 }
3370 moduleName := components[0]
3371 moduleOutput := outputForModuleName(moduleName)
3372 modulesInOrder = append(modulesInOrder, moduleOutput)
3373
3374 depString := components[1]
3375 // convert from "b,c" to ["b", "c"]
3376 depNames := strings.Split(depString, ",")
3377 if len(depString) < 1 {
3378 depNames = []string{}
3379 }
3380 var deps []android.Path
3381 for _, depName := range depNames {
3382 deps = append(deps, outputForModuleName(depName))
3383 }
3384 allDeps[moduleOutput] = deps
3385 }
3386 return modulesInOrder, allDeps
3387}
3388
Jeff Gaston294356f2017-09-27 17:05:30 -07003389func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
3390 for _, moduleName := range moduleNames {
3391 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
3392 output := module.outputFile.Path()
3393 paths = append(paths, output)
3394 }
3395 return paths
3396}
3397
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003398func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07003399 ctx := testCc(t, `
3400 cc_library {
3401 name: "a",
3402 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09003403 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003404 }
3405 cc_library {
3406 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003407 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003408 }
3409 cc_library {
3410 name: "c",
3411 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003412 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003413 }
3414 cc_library {
3415 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09003416 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003417 }
3418
3419 `)
3420
Colin Cross7113d202019-11-20 16:39:12 -08003421 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07003422 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003423 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3424 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07003425
3426 if !reflect.DeepEqual(actual, expected) {
3427 t.Errorf("staticDeps orderings were not propagated correctly"+
3428 "\nactual: %v"+
3429 "\nexpected: %v",
3430 actual,
3431 expected,
3432 )
3433 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09003434}
Jeff Gaston294356f2017-09-27 17:05:30 -07003435
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003436func TestStaticLibDepReorderingWithShared(t *testing.T) {
3437 ctx := testCc(t, `
3438 cc_library {
3439 name: "a",
3440 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09003441 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003442 }
3443 cc_library {
3444 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003445 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003446 }
3447 cc_library {
3448 name: "c",
3449 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003450 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003451 }
3452
3453 `)
3454
Colin Cross7113d202019-11-20 16:39:12 -08003455 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003456 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003457 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3458 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003459
3460 if !reflect.DeepEqual(actual, expected) {
3461 t.Errorf("staticDeps orderings did not account for shared libs"+
3462 "\nactual: %v"+
3463 "\nexpected: %v",
3464 actual,
3465 expected,
3466 )
3467 }
3468}
3469
Jooyung Hanb04a4992020-03-13 18:57:35 +09003470func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07003471 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003472 if !reflect.DeepEqual(actual, expected) {
3473 t.Errorf(message+
3474 "\nactual: %v"+
3475 "\nexpected: %v",
3476 actual,
3477 expected,
3478 )
3479 }
3480}
3481
Jooyung Han61b66e92020-03-21 14:21:46 +00003482func TestLlndkLibrary(t *testing.T) {
3483 ctx := testCc(t, `
3484 cc_library {
3485 name: "libllndk",
3486 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07003487 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003488 }
3489 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003490 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003491 }
Colin Cross127bb8b2020-12-16 16:46:01 -08003492
3493 cc_prebuilt_library_shared {
3494 name: "libllndkprebuilt",
3495 stubs: { versions: ["1", "2"] },
3496 llndk_stubs: "libllndkprebuilt.llndk",
3497 }
3498 llndk_library {
3499 name: "libllndkprebuilt.llndk",
3500 }
3501
3502 cc_library {
3503 name: "libllndk_with_external_headers",
3504 stubs: { versions: ["1", "2"] },
3505 llndk_stubs: "libllndk_with_external_headers.llndk",
3506 header_libs: ["libexternal_headers"],
3507 export_header_lib_headers: ["libexternal_headers"],
3508 }
3509 llndk_library {
3510 name: "libllndk_with_external_headers.llndk",
3511 }
3512 cc_library_headers {
3513 name: "libexternal_headers",
3514 export_include_dirs: ["include"],
3515 vendor_available: true,
3516 }
Jooyung Han61b66e92020-03-21 14:21:46 +00003517 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08003518 actual := ctx.ModuleVariantsForTests("libllndk")
3519 for i := 0; i < len(actual); i++ {
3520 if !strings.HasPrefix(actual[i], "android_vendor.VER_") {
3521 actual = append(actual[:i], actual[i+1:]...)
3522 i--
3523 }
3524 }
Jooyung Han61b66e92020-03-21 14:21:46 +00003525 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00003526 "android_vendor.VER_arm64_armv8-a_shared_1",
3527 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003528 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003529 "android_vendor.VER_arm_armv7-a-neon_shared_1",
3530 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003531 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003532 }
3533 checkEquals(t, "variants for llndk stubs", expected, actual)
3534
Colin Cross127bb8b2020-12-16 16:46:01 -08003535 params := ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00003536 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
3537
Colin Cross127bb8b2020-12-16 16:46:01 -08003538 params = ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00003539 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
3540}
3541
Jiyong Parka46a4d52017-12-14 19:54:34 +09003542func TestLlndkHeaders(t *testing.T) {
3543 ctx := testCc(t, `
3544 llndk_headers {
3545 name: "libllndk_headers",
3546 export_include_dirs: ["my_include"],
3547 }
3548 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003549 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09003550 export_llndk_headers: ["libllndk_headers"],
3551 }
3552 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07003553 name: "libllndk",
3554 llndk_stubs: "libllndk.llndk",
3555 }
3556
3557 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09003558 name: "libvendor",
3559 shared_libs: ["libllndk"],
3560 vendor: true,
3561 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003562 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08003563 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09003564 }
3565 `)
3566
3567 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08003568 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09003569 cflags := cc.Args["cFlags"]
3570 if !strings.Contains(cflags, "-Imy_include") {
3571 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
3572 }
3573}
3574
Logan Chien43d34c32017-12-20 01:17:32 +08003575func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
3576 actual := module.Properties.AndroidMkRuntimeLibs
3577 if !reflect.DeepEqual(actual, expected) {
3578 t.Errorf("incorrect runtime_libs for shared libs"+
3579 "\nactual: %v"+
3580 "\nexpected: %v",
3581 actual,
3582 expected,
3583 )
3584 }
3585}
3586
3587const runtimeLibAndroidBp = `
3588 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09003589 name: "liball_available",
3590 vendor_available: true,
3591 product_available: true,
3592 no_libcrt : true,
3593 nocrt : true,
3594 system_shared_libs : [],
3595 }
3596 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08003597 name: "libvendor_available1",
3598 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003599 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07003600 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003601 nocrt : true,
3602 system_shared_libs : [],
3603 }
3604 cc_library {
3605 name: "libvendor_available2",
3606 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003607 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08003608 target: {
3609 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09003610 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08003611 }
3612 },
Yi Konge7fe9912019-06-02 00:53:50 -07003613 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003614 nocrt : true,
3615 system_shared_libs : [],
3616 }
3617 cc_library {
3618 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09003619 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07003620 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003621 nocrt : true,
3622 system_shared_libs : [],
3623 }
3624 cc_library {
3625 name: "libvendor1",
3626 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003627 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003628 nocrt : true,
3629 system_shared_libs : [],
3630 }
3631 cc_library {
3632 name: "libvendor2",
3633 vendor: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003634 runtime_libs: ["liball_available", "libvendor1"],
3635 no_libcrt : true,
3636 nocrt : true,
3637 system_shared_libs : [],
3638 }
3639 cc_library {
3640 name: "libproduct_available1",
3641 product_available: true,
3642 runtime_libs: ["liball_available"],
3643 no_libcrt : true,
3644 nocrt : true,
3645 system_shared_libs : [],
3646 }
3647 cc_library {
3648 name: "libproduct1",
3649 product_specific: true,
3650 no_libcrt : true,
3651 nocrt : true,
3652 system_shared_libs : [],
3653 }
3654 cc_library {
3655 name: "libproduct2",
3656 product_specific: true,
3657 runtime_libs: ["liball_available", "libproduct1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003658 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003659 nocrt : true,
3660 system_shared_libs : [],
3661 }
3662`
3663
3664func TestRuntimeLibs(t *testing.T) {
3665 ctx := testCc(t, runtimeLibAndroidBp)
3666
3667 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08003668 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003669
Justin Yun8a2600c2020-12-07 12:44:03 +09003670 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3671 checkRuntimeLibs(t, []string{"liball_available"}, module)
3672
3673 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3674 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003675
3676 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003677 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003678
3679 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3680 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08003681 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003682
Justin Yun8a2600c2020-12-07 12:44:03 +09003683 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3684 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003685
3686 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003687 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1"}, module)
3688
3689 // runtime_libs for product variants have '.product' suffixes if the modules have both core
3690 // and product variants.
3691 variant = "android_product.VER_arm64_armv8-a_shared"
3692
3693 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3694 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
3695
3696 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
3697 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003698}
3699
3700func TestExcludeRuntimeLibs(t *testing.T) {
3701 ctx := testCc(t, runtimeLibAndroidBp)
3702
Colin Cross7113d202019-11-20 16:39:12 -08003703 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003704 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3705 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003706
Colin Crossfb0c16e2019-11-20 17:12:35 -08003707 variant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003708 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08003709 checkRuntimeLibs(t, nil, module)
3710}
3711
3712func TestRuntimeLibsNoVndk(t *testing.T) {
3713 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3714
3715 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3716
Colin Cross7113d202019-11-20 16:39:12 -08003717 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003718
Justin Yun8a2600c2020-12-07 12:44:03 +09003719 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3720 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003721
3722 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003723 checkRuntimeLibs(t, []string{"liball_available", "libvendor1"}, module)
3724
3725 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
3726 checkRuntimeLibs(t, []string{"liball_available", "libproduct1"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003727}
3728
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003729func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003730 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003731 actual := module.Properties.AndroidMkStaticLibs
3732 if !reflect.DeepEqual(actual, expected) {
3733 t.Errorf("incorrect static_libs"+
3734 "\nactual: %v"+
3735 "\nexpected: %v",
3736 actual,
3737 expected,
3738 )
3739 }
3740}
3741
3742const staticLibAndroidBp = `
3743 cc_library {
3744 name: "lib1",
3745 }
3746 cc_library {
3747 name: "lib2",
3748 static_libs: ["lib1"],
3749 }
3750`
3751
3752func TestStaticLibDepExport(t *testing.T) {
3753 ctx := testCc(t, staticLibAndroidBp)
3754
3755 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003756 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003757 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003758 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003759
3760 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003761 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003762 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3763 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003764 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003765}
3766
Jiyong Parkd08b6972017-09-26 10:50:54 +09003767var compilerFlagsTestCases = []struct {
3768 in string
3769 out bool
3770}{
3771 {
3772 in: "a",
3773 out: false,
3774 },
3775 {
3776 in: "-a",
3777 out: true,
3778 },
3779 {
3780 in: "-Ipath/to/something",
3781 out: false,
3782 },
3783 {
3784 in: "-isystempath/to/something",
3785 out: false,
3786 },
3787 {
3788 in: "--coverage",
3789 out: false,
3790 },
3791 {
3792 in: "-include a/b",
3793 out: true,
3794 },
3795 {
3796 in: "-include a/b c/d",
3797 out: false,
3798 },
3799 {
3800 in: "-DMACRO",
3801 out: true,
3802 },
3803 {
3804 in: "-DMAC RO",
3805 out: false,
3806 },
3807 {
3808 in: "-a -b",
3809 out: false,
3810 },
3811 {
3812 in: "-DMACRO=definition",
3813 out: true,
3814 },
3815 {
3816 in: "-DMACRO=defi nition",
3817 out: true, // TODO(jiyong): this should be false
3818 },
3819 {
3820 in: "-DMACRO(x)=x + 1",
3821 out: true,
3822 },
3823 {
3824 in: "-DMACRO=\"defi nition\"",
3825 out: true,
3826 },
3827}
3828
3829type mockContext struct {
3830 BaseModuleContext
3831 result bool
3832}
3833
3834func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3835 // CheckBadCompilerFlags calls this function when the flag should be rejected
3836 ctx.result = false
3837}
3838
3839func TestCompilerFlags(t *testing.T) {
3840 for _, testCase := range compilerFlagsTestCases {
3841 ctx := &mockContext{result: true}
3842 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3843 if ctx.result != testCase.out {
3844 t.Errorf("incorrect output:")
3845 t.Errorf(" input: %#v", testCase.in)
3846 t.Errorf(" expected: %#v", testCase.out)
3847 t.Errorf(" got: %#v", ctx.result)
3848 }
3849 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003850}
Jiyong Park374510b2018-03-19 18:23:01 +09003851
3852func TestVendorPublicLibraries(t *testing.T) {
3853 ctx := testCc(t, `
3854 cc_library_headers {
3855 name: "libvendorpublic_headers",
3856 export_include_dirs: ["my_include"],
3857 }
3858 vendor_public_library {
3859 name: "libvendorpublic",
3860 symbol_file: "",
3861 export_public_headers: ["libvendorpublic_headers"],
3862 }
3863 cc_library {
3864 name: "libvendorpublic",
3865 srcs: ["foo.c"],
3866 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003867 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003868 nocrt: true,
3869 }
3870
3871 cc_library {
3872 name: "libsystem",
3873 shared_libs: ["libvendorpublic"],
3874 vendor: false,
3875 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003876 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003877 nocrt: true,
3878 }
3879 cc_library {
3880 name: "libvendor",
3881 shared_libs: ["libvendorpublic"],
3882 vendor: true,
3883 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003884 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003885 nocrt: true,
3886 }
3887 `)
3888
Colin Cross7113d202019-11-20 16:39:12 -08003889 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003890 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003891
3892 // test if header search paths are correctly added
3893 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003894 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003895 cflags := cc.Args["cFlags"]
3896 if !strings.Contains(cflags, "-Imy_include") {
3897 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3898 }
3899
3900 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003901 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003902 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003903 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003904 if !strings.Contains(libflags, stubPaths[0].String()) {
3905 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3906 }
3907
3908 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003909 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003910 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003911 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003912 if !strings.Contains(libflags, stubPaths[0].String()) {
3913 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3914 }
3915
3916}
Jiyong Park37b25202018-07-11 10:49:27 +09003917
3918func TestRecovery(t *testing.T) {
3919 ctx := testCc(t, `
3920 cc_library_shared {
3921 name: "librecovery",
3922 recovery: true,
3923 }
3924 cc_library_shared {
3925 name: "librecovery32",
3926 recovery: true,
3927 compile_multilib:"32",
3928 }
Jiyong Park5baac542018-08-28 09:55:37 +09003929 cc_library_shared {
3930 name: "libHalInRecovery",
3931 recovery_available: true,
3932 vendor: true,
3933 }
Jiyong Park37b25202018-07-11 10:49:27 +09003934 `)
3935
3936 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003937 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003938 if len(variants) != 1 || !android.InList(arm64, variants) {
3939 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3940 }
3941
3942 variants = ctx.ModuleVariantsForTests("librecovery32")
3943 if android.InList(arm64, variants) {
3944 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3945 }
Jiyong Park5baac542018-08-28 09:55:37 +09003946
3947 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3948 if !recoveryModule.Platform() {
3949 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3950 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003951}
Jiyong Park5baac542018-08-28 09:55:37 +09003952
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003953func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3954 bp := `
3955 cc_prebuilt_test_library_shared {
3956 name: "test_lib",
3957 relative_install_path: "foo/bar/baz",
3958 srcs: ["srcpath/dontusethispath/baz.so"],
3959 }
3960
3961 cc_test {
3962 name: "main_test",
3963 data_libs: ["test_lib"],
3964 gtest: false,
3965 }
3966 `
3967
3968 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3969 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3970 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3971 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3972
3973 ctx := testCcWithConfig(t, config)
3974 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3975 testBinary := module.(*Module).linker.(*testBinary)
3976 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3977 if err != nil {
3978 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3979 }
3980 if len(outputFiles) != 1 {
3981 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3982 }
3983 if len(testBinary.dataPaths()) != 1 {
3984 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3985 }
3986
3987 outputPath := outputFiles[0].String()
3988
3989 if !strings.HasSuffix(outputPath, "/main_test") {
3990 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3991 }
3992 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
3993 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3994 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3995 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3996 }
3997}
3998
Jiyong Park7ed9de32018-10-15 22:25:07 +09003999func TestVersionedStubs(t *testing.T) {
4000 ctx := testCc(t, `
4001 cc_library_shared {
4002 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09004003 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09004004 stubs: {
4005 symbol_file: "foo.map.txt",
4006 versions: ["1", "2", "3"],
4007 },
4008 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09004009
Jiyong Park7ed9de32018-10-15 22:25:07 +09004010 cc_library_shared {
4011 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09004012 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09004013 shared_libs: ["libFoo#1"],
4014 }`)
4015
4016 variants := ctx.ModuleVariantsForTests("libFoo")
4017 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08004018 "android_arm64_armv8-a_shared",
4019 "android_arm64_armv8-a_shared_1",
4020 "android_arm64_armv8-a_shared_2",
4021 "android_arm64_armv8-a_shared_3",
4022 "android_arm_armv7-a-neon_shared",
4023 "android_arm_armv7-a-neon_shared_1",
4024 "android_arm_armv7-a-neon_shared_2",
4025 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09004026 }
4027 variantsMismatch := false
4028 if len(variants) != len(expectedVariants) {
4029 variantsMismatch = true
4030 } else {
4031 for _, v := range expectedVariants {
4032 if !inList(v, variants) {
4033 variantsMismatch = false
4034 }
4035 }
4036 }
4037 if variantsMismatch {
4038 t.Errorf("variants of libFoo expected:\n")
4039 for _, v := range expectedVariants {
4040 t.Errorf("%q\n", v)
4041 }
4042 t.Errorf(", but got:\n")
4043 for _, v := range variants {
4044 t.Errorf("%q\n", v)
4045 }
4046 }
4047
Colin Cross7113d202019-11-20 16:39:12 -08004048 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09004049 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08004050 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09004051 if !strings.Contains(libFlags, libFoo1StubPath) {
4052 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
4053 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09004054
Colin Cross7113d202019-11-20 16:39:12 -08004055 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09004056 cFlags := libBarCompileRule.Args["cFlags"]
4057 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
4058 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
4059 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
4060 }
Jiyong Park37b25202018-07-11 10:49:27 +09004061}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004062
Jooyung Hanb04a4992020-03-13 18:57:35 +09004063func TestVersioningMacro(t *testing.T) {
4064 for _, tc := range []struct{ moduleName, expected string }{
4065 {"libc", "__LIBC_API__"},
4066 {"libfoo", "__LIBFOO_API__"},
4067 {"libfoo@1", "__LIBFOO_1_API__"},
4068 {"libfoo-v1", "__LIBFOO_V1_API__"},
4069 {"libfoo.v1", "__LIBFOO_V1_API__"},
4070 } {
4071 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
4072 }
4073}
4074
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004075func TestStaticExecutable(t *testing.T) {
4076 ctx := testCc(t, `
4077 cc_binary {
4078 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01004079 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004080 static_executable: true,
4081 }`)
4082
Colin Cross7113d202019-11-20 16:39:12 -08004083 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004084 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
4085 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07004086 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004087 for _, lib := range systemStaticLibs {
4088 if !strings.Contains(libFlags, lib) {
4089 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
4090 }
4091 }
4092 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
4093 for _, lib := range systemSharedLibs {
4094 if strings.Contains(libFlags, lib) {
4095 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
4096 }
4097 }
4098}
Jiyong Parke4bb9862019-02-01 00:31:10 +09004099
4100func TestStaticDepsOrderWithStubs(t *testing.T) {
4101 ctx := testCc(t, `
4102 cc_binary {
4103 name: "mybin",
4104 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07004105 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09004106 static_executable: true,
4107 stl: "none",
4108 }
4109
4110 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08004111 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09004112 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08004113 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09004114 stl: "none",
4115 }
4116
4117 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08004118 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09004119 srcs: ["foo.c"],
4120 stl: "none",
4121 stubs: {
4122 versions: ["1"],
4123 },
4124 }`)
4125
Colin Cross0de8a1e2020-09-18 14:15:30 -07004126 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
4127 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08004128 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09004129
4130 if !reflect.DeepEqual(actual, expected) {
4131 t.Errorf("staticDeps orderings were not propagated correctly"+
4132 "\nactual: %v"+
4133 "\nexpected: %v",
4134 actual,
4135 expected,
4136 )
4137 }
4138}
Jooyung Han38002912019-05-16 04:01:54 +09004139
Jooyung Hand48f3c32019-08-23 11:18:57 +09004140func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
4141 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
4142 cc_library {
4143 name: "libA",
4144 srcs: ["foo.c"],
4145 shared_libs: ["libB"],
4146 stl: "none",
4147 }
4148
4149 cc_library {
4150 name: "libB",
4151 srcs: ["foo.c"],
4152 enabled: false,
4153 stl: "none",
4154 }
4155 `)
4156}
4157
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004158// Simple smoke test for the cc_fuzz target that ensures the rule compiles
4159// correctly.
4160func TestFuzzTarget(t *testing.T) {
4161 ctx := testCc(t, `
4162 cc_fuzz {
4163 name: "fuzz_smoke_test",
4164 srcs: ["foo.c"],
4165 }`)
4166
Paul Duffin075c4172019-12-19 19:06:13 +00004167 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004168 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
4169}
4170
Jiyong Park29074592019-07-07 16:27:47 +09004171func TestAidl(t *testing.T) {
4172}
4173
Jooyung Han38002912019-05-16 04:01:54 +09004174func assertString(t *testing.T, got, expected string) {
4175 t.Helper()
4176 if got != expected {
4177 t.Errorf("expected %q got %q", expected, got)
4178 }
4179}
4180
4181func assertArrayString(t *testing.T, got, expected []string) {
4182 t.Helper()
4183 if len(got) != len(expected) {
4184 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
4185 return
4186 }
4187 for i := range got {
4188 if got[i] != expected[i] {
4189 t.Errorf("expected %d-th %q (%q) got %q (%q)",
4190 i, expected[i], expected, got[i], got)
4191 return
4192 }
4193 }
4194}
Colin Crosse1bb5d02019-09-24 14:55:04 -07004195
Jooyung Han0302a842019-10-30 18:43:49 +09004196func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
4197 t.Helper()
4198 assertArrayString(t, android.SortedStringKeys(m), expected)
4199}
4200
Colin Crosse1bb5d02019-09-24 14:55:04 -07004201func TestDefaults(t *testing.T) {
4202 ctx := testCc(t, `
4203 cc_defaults {
4204 name: "defaults",
4205 srcs: ["foo.c"],
4206 static: {
4207 srcs: ["bar.c"],
4208 },
4209 shared: {
4210 srcs: ["baz.c"],
4211 },
4212 }
4213
4214 cc_library_static {
4215 name: "libstatic",
4216 defaults: ["defaults"],
4217 }
4218
4219 cc_library_shared {
4220 name: "libshared",
4221 defaults: ["defaults"],
4222 }
4223
4224 cc_library {
4225 name: "libboth",
4226 defaults: ["defaults"],
4227 }
4228
4229 cc_binary {
4230 name: "binary",
4231 defaults: ["defaults"],
4232 }`)
4233
4234 pathsToBase := func(paths android.Paths) []string {
4235 var ret []string
4236 for _, p := range paths {
4237 ret = append(ret, p.Base())
4238 }
4239 return ret
4240 }
4241
Colin Cross7113d202019-11-20 16:39:12 -08004242 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004243 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4244 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
4245 }
Colin Cross7113d202019-11-20 16:39:12 -08004246 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004247 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4248 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
4249 }
Colin Cross7113d202019-11-20 16:39:12 -08004250 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004251 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
4252 t.Errorf("binary ld rule wanted %q, got %q", w, g)
4253 }
4254
Colin Cross7113d202019-11-20 16:39:12 -08004255 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004256 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4257 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
4258 }
Colin Cross7113d202019-11-20 16:39:12 -08004259 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004260 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4261 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
4262 }
4263}
Colin Crosseabaedd2020-02-06 17:01:55 -08004264
4265func TestProductVariableDefaults(t *testing.T) {
4266 bp := `
4267 cc_defaults {
4268 name: "libfoo_defaults",
4269 srcs: ["foo.c"],
4270 cppflags: ["-DFOO"],
4271 product_variables: {
4272 debuggable: {
4273 cppflags: ["-DBAR"],
4274 },
4275 },
4276 }
4277
4278 cc_library {
4279 name: "libfoo",
4280 defaults: ["libfoo_defaults"],
4281 }
4282 `
4283
4284 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4285 config.TestProductVariables.Debuggable = BoolPtr(true)
4286
Colin Crossae8600b2020-10-29 17:09:13 -07004287 ctx := CreateTestContext(config)
Colin Crosseabaedd2020-02-06 17:01:55 -08004288 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
4289 ctx.BottomUp("variable", android.VariableMutator).Parallel()
4290 })
Colin Crossae8600b2020-10-29 17:09:13 -07004291 ctx.Register()
Colin Crosseabaedd2020-02-06 17:01:55 -08004292
4293 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
4294 android.FailIfErrored(t, errs)
4295 _, errs = ctx.PrepareBuildActions(config)
4296 android.FailIfErrored(t, errs)
4297
4298 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
4299 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
4300 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
4301 }
4302}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004303
4304func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4305 t.Parallel()
4306 bp := `
4307 cc_library_static {
4308 name: "libfoo",
4309 srcs: ["foo.c"],
4310 whole_static_libs: ["libbar"],
4311 }
4312
4313 cc_library_static {
4314 name: "libbar",
4315 whole_static_libs: ["libmissing"],
4316 }
4317 `
4318
4319 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4320 config.TestProductVariables.Allow_missing_dependencies = BoolPtr(true)
4321
Colin Crossae8600b2020-10-29 17:09:13 -07004322 ctx := CreateTestContext(config)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004323 ctx.SetAllowMissingDependencies(true)
Colin Crossae8600b2020-10-29 17:09:13 -07004324 ctx.Register()
Colin Crosse4f6eba2020-09-22 18:11:25 -07004325
4326 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
4327 android.FailIfErrored(t, errs)
4328 _, errs = ctx.PrepareBuildActions(config)
4329 android.FailIfErrored(t, errs)
4330
4331 libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
4332 if g, w := libbar.Rule, android.ErrorRule; g != w {
4333 t.Fatalf("Expected libbar rule to be %q, got %q", w, g)
4334 }
4335
4336 if g, w := libbar.Args["error"], "missing dependencies: libmissing"; !strings.Contains(g, w) {
4337 t.Errorf("Expected libbar error to contain %q, was %q", w, g)
4338 }
4339
4340 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
4341 if g, w := libfoo.Inputs.Strings(), libbar.Output.String(); !android.InList(w, g) {
4342 t.Errorf("Expected libfoo.a to depend on %q, got %q", w, g)
4343 }
4344
4345}
Colin Crosse9fe2942020-11-10 18:12:15 -08004346
4347func TestInstallSharedLibs(t *testing.T) {
4348 bp := `
4349 cc_binary {
4350 name: "bin",
4351 host_supported: true,
4352 shared_libs: ["libshared"],
4353 runtime_libs: ["libruntime"],
4354 srcs: [":gen"],
4355 }
4356
4357 cc_library_shared {
4358 name: "libshared",
4359 host_supported: true,
4360 shared_libs: ["libtransitive"],
4361 }
4362
4363 cc_library_shared {
4364 name: "libtransitive",
4365 host_supported: true,
4366 }
4367
4368 cc_library_shared {
4369 name: "libruntime",
4370 host_supported: true,
4371 }
4372
4373 cc_binary_host {
4374 name: "tool",
4375 srcs: ["foo.cpp"],
4376 }
4377
4378 genrule {
4379 name: "gen",
4380 tools: ["tool"],
4381 out: ["gen.cpp"],
4382 cmd: "$(location tool) $(out)",
4383 }
4384 `
4385
4386 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4387 ctx := testCcWithConfig(t, config)
4388
4389 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4390 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4391 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4392 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4393 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4394
4395 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4396 t.Errorf("expected host bin dependency %q, got %q", w, g)
4397 }
4398
4399 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4400 t.Errorf("expected host bin dependency %q, got %q", w, g)
4401 }
4402
4403 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4404 t.Errorf("expected host bin dependency %q, got %q", w, g)
4405 }
4406
4407 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4408 t.Errorf("expected host bin dependency %q, got %q", w, g)
4409 }
4410
4411 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4412 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4413 }
4414
4415 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4416 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4417 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4418 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4419
4420 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4421 t.Errorf("expected device bin dependency %q, got %q", w, g)
4422 }
4423
4424 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4425 t.Errorf("expected device bin dependency %q, got %q", w, g)
4426 }
4427
4428 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4429 t.Errorf("expected device bin dependency %q, got %q", w, g)
4430 }
4431
4432 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4433 t.Errorf("expected device bin dependency %q, got %q", w, g)
4434 }
4435
4436 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4437 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4438 }
4439
4440}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004441
4442func TestStubsLibReexportsHeaders(t *testing.T) {
4443 ctx := testCc(t, `
4444 cc_library_shared {
4445 name: "libclient",
4446 srcs: ["foo.c"],
4447 shared_libs: ["libfoo#1"],
4448 }
4449
4450 cc_library_shared {
4451 name: "libfoo",
4452 srcs: ["foo.c"],
4453 shared_libs: ["libbar"],
4454 export_shared_lib_headers: ["libbar"],
4455 stubs: {
4456 symbol_file: "foo.map.txt",
4457 versions: ["1", "2", "3"],
4458 },
4459 }
4460
4461 cc_library_shared {
4462 name: "libbar",
4463 export_include_dirs: ["include/libbar"],
4464 srcs: ["foo.c"],
4465 }`)
4466
4467 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4468
4469 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4470 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4471 }
4472}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004473
4474func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
4475 ctx := testCc(t, `
4476 cc_library {
4477 name: "libfoo",
4478 srcs: ["a/Foo.aidl"],
4479 aidl: { flags: ["-Werror"], },
4480 }
4481 `)
4482
4483 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4484 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4485 aidlCommand := manifest.Commands[0].GetCommand()
4486 expectedAidlFlag := "-Werror"
4487 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4488 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4489 }
4490}