blob: e9daf33fc402be95d0f762c996488dcb390c8d29 [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 "os"
Inseob Kim1f086e22019-05-09 13:29:15 +090020 "path/filepath"
Colin Cross74d1ec02015-04-28 13:30:13 -070021 "reflect"
Paul Duffin3cb603e2021-02-19 13:57:10 +000022 "regexp"
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 +090029func TestMain(m *testing.M) {
Paul Duffinc3e6ce02021-03-22 23:21:32 +000030 os.Exit(m.Run())
Jiyong Park6a43f042017-10-12 23:05:00 +090031}
32
Paul Duffin2e6f90e2021-03-22 23:20:25 +000033var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000034 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000035 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
36 variables.DeviceVndkVersion = StringPtr("current")
37 variables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090038 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000039 }),
40)
41
Paul Duffin8567f222021-03-23 00:02:06 +000042// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000043//
44// See testCc for an explanation as to how to stop using this deprecated method.
45//
46// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080047func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070048 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000049 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000050 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090051}
52
Paul Duffin8567f222021-03-23 00:02:06 +000053// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000054//
Paul Duffin8567f222021-03-23 00:02:06 +000055// Do not add any new usages of this, instead use the prepareForCcTest directly as it makes it much
Paul Duffin02a3d652021-02-24 18:51:54 +000056// easier to customize the test behavior.
57//
58// If it is necessary to customize the behavior of an existing test that uses this then please first
Paul Duffin8567f222021-03-23 00:02:06 +000059// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000060// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
61// that it did not change the test behavior unexpectedly.
62//
63// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080064func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080065 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000066 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +000067 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +080068}
69
Paul Duffin8567f222021-03-23 00:02:06 +000070// testCcNoVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000071//
72// See testCc for an explanation as to how to stop using this deprecated method.
73//
74// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080075func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080076 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +000077 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jiyong Parkf58c46e2021-04-01 21:35:20 +090078 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Logan Chienf3511742017-10-31 18:04:35 +080079
Colin Cross98be1bb2019-12-13 20:41:13 -080080 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080081}
82
Paul Duffin8567f222021-03-23 00:02:06 +000083// testCcNoProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000084//
85// See testCc for an explanation as to how to stop using this deprecated method.
86//
87// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +090088func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
89 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +000090 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun8a2600c2020-12-07 12:44:03 +090091 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090092 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun8a2600c2020-12-07 12:44:03 +090093
94 return testCcWithConfig(t, config)
95}
96
Paul Duffin8567f222021-03-23 00:02:06 +000097// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000098//
99// See testCc for an explanation as to how to stop using this deprecated method.
100//
101// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900102func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800103 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800104
Paul Duffin8567f222021-03-23 00:02:06 +0000105 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000106 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
107 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800108}
109
Paul Duffin8567f222021-03-23 00:02:06 +0000110// testCcError runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000111//
112// See testCc for an explanation as to how to stop using this deprecated method.
113//
114// deprecated
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()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000117 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900118 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900119 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900120 testCcErrorWithConfig(t, pattern, config)
121 return
122}
123
Paul Duffin8567f222021-03-23 00:02:06 +0000124// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000125//
126// See testCc for an explanation as to how to stop using this deprecated method.
127//
128// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900129func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900130 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000131 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900132 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
133 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900134 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900135 testCcErrorWithConfig(t, pattern, config)
136 return
137}
138
Logan Chienf3511742017-10-31 18:04:35 +0800139const (
Colin Cross7113d202019-11-20 16:39:12 -0800140 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900141 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
142 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800143 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800144)
145
Paul Duffindb462dd2021-03-21 22:01:55 +0000146// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
147// running it in a fixture that requires all source files to exist.
148func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
149 android.GroupFixturePreparers(
150 PrepareForTestWithCcDefaultModules,
151 android.PrepareForTestDisallowNonExistentPaths,
152 ).RunTest(t)
153}
154
Doug Hornc32c6b02019-01-17 14:44:05 -0800155func TestFuchsiaDeps(t *testing.T) {
156 t.Helper()
157
158 bp := `
159 cc_library {
160 name: "libTest",
161 srcs: ["foo.c"],
162 target: {
163 fuchsia: {
164 srcs: ["bar.c"],
165 },
166 },
167 }`
168
Paul Duffin8567f222021-03-23 00:02:06 +0000169 result := android.GroupFixturePreparers(
170 prepareForCcTest,
171 PrepareForTestOnFuchsia,
172 ).RunTestWithBp(t, bp)
Doug Hornc32c6b02019-01-17 14:44:05 -0800173
174 rt := false
175 fb := false
176
Paul Duffinecdac8a2021-02-24 19:18:42 +0000177 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800178 implicits := ld.Implicits
179 for _, lib := range implicits {
180 if strings.Contains(lib.Rel(), "libcompiler_rt") {
181 rt = true
182 }
183
184 if strings.Contains(lib.Rel(), "libbioniccompat") {
185 fb = true
186 }
187 }
188
189 if !rt || !fb {
190 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
191 }
192}
193
194func TestFuchsiaTargetDecl(t *testing.T) {
195 t.Helper()
196
197 bp := `
198 cc_library {
199 name: "libTest",
200 srcs: ["foo.c"],
201 target: {
202 fuchsia: {
203 srcs: ["bar.c"],
204 },
205 },
206 }`
207
Paul Duffin8567f222021-03-23 00:02:06 +0000208 result := android.GroupFixturePreparers(
209 prepareForCcTest,
210 PrepareForTestOnFuchsia,
211 ).RunTestWithBp(t, bp)
Paul Duffinecdac8a2021-02-24 19:18:42 +0000212 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800213 var objs []string
214 for _, o := range ld.Inputs {
215 objs = append(objs, o.Base())
216 }
Paul Duffine84b1332021-03-12 11:59:43 +0000217 android.AssertArrayString(t, "libTest inputs", []string{"foo.o", "bar.o"}, objs)
Doug Hornc32c6b02019-01-17 14:44:05 -0800218}
219
Jiyong Park6a43f042017-10-12 23:05:00 +0900220func TestVendorSrc(t *testing.T) {
221 ctx := testCc(t, `
222 cc_library {
223 name: "libTest",
224 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700225 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800226 nocrt: true,
227 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900228 vendor_available: true,
229 target: {
230 vendor: {
231 srcs: ["bar.c"],
232 },
233 },
234 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900235 `)
236
Logan Chienf3511742017-10-31 18:04:35 +0800237 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900238 var objs []string
239 for _, o := range ld.Inputs {
240 objs = append(objs, o.Base())
241 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800242 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900243 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
244 }
245}
246
Justin Yun7f99ec72021-04-12 13:19:28 +0900247func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
248 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
249 partitionDefined := false
250 checkPartition := func(specific bool, partition string) {
251 if specific {
252 if expected != partition && !partitionDefined {
253 // The variant is installed to the 'partition'
254 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
255 }
256 partitionDefined = true
257 } else {
258 // The variant is not installed to the 'partition'
259 if expected == partition {
260 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
261 }
262 }
263 }
264 socSpecific := func(m *Module) bool {
265 return m.SocSpecific() || m.socSpecificModuleContext()
266 }
267 deviceSpecific := func(m *Module) bool {
268 return m.DeviceSpecific() || m.deviceSpecificModuleContext()
269 }
270 productSpecific := func(m *Module) bool {
271 return m.ProductSpecific() || m.productSpecificModuleContext()
272 }
273 systemExtSpecific := func(m *Module) bool {
274 return m.SystemExtSpecific()
275 }
276 checkPartition(socSpecific(mod), "vendor")
277 checkPartition(deviceSpecific(mod), "odm")
278 checkPartition(productSpecific(mod), "product")
279 checkPartition(systemExtSpecific(mod), "system_ext")
280 if !partitionDefined && expected != "system" {
281 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
282 " but installed to system partition", variant, name, expected)
283 }
284}
285
286func TestInstallPartition(t *testing.T) {
287 t.Helper()
288 ctx := prepareForCcTest.RunTestWithBp(t, `
289 cc_library {
290 name: "libsystem",
291 }
292 cc_library {
293 name: "libsystem_ext",
294 system_ext_specific: true,
295 }
296 cc_library {
297 name: "libproduct",
298 product_specific: true,
299 }
300 cc_library {
301 name: "libvendor",
302 vendor: true,
303 }
304 cc_library {
305 name: "libodm",
306 device_specific: true,
307 }
308 cc_library {
309 name: "liball_available",
310 vendor_available: true,
311 product_available: true,
312 }
313 cc_library {
314 name: "libsystem_ext_all_available",
315 system_ext_specific: true,
316 vendor_available: true,
317 product_available: true,
318 }
319 cc_library {
320 name: "liball_available_odm",
321 odm_available: true,
322 product_available: true,
323 }
324 cc_library {
325 name: "libproduct_vendoravailable",
326 product_specific: true,
327 vendor_available: true,
328 }
329 cc_library {
330 name: "libproduct_odmavailable",
331 product_specific: true,
332 odm_available: true,
333 }
334 `).TestContext
335
336 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
337 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
338 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
339 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
340 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
341
342 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
343 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
344 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
345
346 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
347 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
348 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
349
350 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
351 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
352 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
353
354 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
355 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
356
357 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
358 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
359}
360
Logan Chienf3511742017-10-31 18:04:35 +0800361func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900362 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800363
Logan Chiend3c59a22018-03-29 14:08:15 +0800364 t.Helper()
365
Justin Yun0ecf0b22020-02-28 15:07:59 +0900366 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800367
368 // Check library properties.
369 lib, ok := mod.compiler.(*libraryDecorator)
370 if !ok {
371 t.Errorf("%q must have libraryDecorator", name)
372 } else if lib.baseInstaller.subDir != subDir {
373 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
374 lib.baseInstaller.subDir)
375 }
376
377 // Check VNDK properties.
378 if mod.vndkdep == nil {
379 t.Fatalf("%q must have `vndkdep`", name)
380 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700381 if !mod.IsVndk() {
382 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800383 }
384 if mod.isVndkSp() != isVndkSp {
385 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
386 }
387
388 // Check VNDK extension properties.
389 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500390 if mod.IsVndkExt() != isVndkExt {
391 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800392 }
393
394 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
395 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
396 }
397}
398
Jose Galmes0a942a02021-02-03 14:23:15 -0800399func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool, fake bool) {
Bill Peckham945441c2020-08-31 16:07:58 -0700400 t.Helper()
Paul Duffine8366da2021-03-24 10:40:38 +0000401 mod := ctx.ModuleForTests(moduleName, variant)
402 outputFiles := mod.OutputFiles(t, "")
403 if len(outputFiles) != 1 {
Jooyung Han39edb6c2019-11-06 16:53:07 +0900404 t.Errorf("%q must have single output\n", moduleName)
405 return
406 }
407 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900408
Bill Peckham945441c2020-08-31 16:07:58 -0700409 if include {
410 out := singleton.Output(snapshotPath)
Jose Galmes0a942a02021-02-03 14:23:15 -0800411 if fake {
412 if out.Rule == nil {
413 t.Errorf("Missing rule for module %q output file %q", moduleName, outputFiles[0])
414 }
415 } else {
416 if out.Input.String() != outputFiles[0].String() {
417 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
418 }
Bill Peckham945441c2020-08-31 16:07:58 -0700419 }
420 } else {
421 out := singleton.MaybeOutput(snapshotPath)
422 if out.Rule != nil {
423 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
424 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900425 }
426}
427
Bill Peckham945441c2020-08-31 16:07:58 -0700428func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000429 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800430 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, false)
Bill Peckham945441c2020-08-31 16:07:58 -0700431}
432
433func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000434 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800435 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false, false)
436}
437
438func checkSnapshotRule(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000439 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800440 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, true)
Bill Peckham945441c2020-08-31 16:07:58 -0700441}
442
Jooyung Han2216fb12019-11-06 16:46:15 +0900443func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
444 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800445 content := android.ContentFromFileRuleForTests(t, params)
446 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900447 assertArrayString(t, actual, expected)
448}
449
Jooyung Han097087b2019-10-22 19:32:18 +0900450func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
451 t.Helper()
452 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900453 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
454}
455
456func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
457 t.Helper()
Colin Cross78212242021-01-06 14:51:30 -0800458 got := ctx.ModuleForTests(module, "").Module().(*vndkLibrariesTxt).fileNames
459 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900460}
461
Logan Chienf3511742017-10-31 18:04:35 +0800462func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800463 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800464 cc_library {
465 name: "libvndk",
466 vendor_available: true,
467 vndk: {
468 enabled: true,
469 },
470 nocrt: true,
471 }
472
473 cc_library {
474 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900475 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800476 vndk: {
477 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900478 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800479 },
480 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900481 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800482 }
483
484 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900485 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800486 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900487 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800488 vndk: {
489 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900490 },
491 nocrt: true,
492 target: {
493 vendor: {
494 cflags: ["-DTEST"],
495 },
496 product: {
497 cflags: ["-DTEST"],
498 },
499 },
500 }
501
502 cc_library {
503 name: "libvndk_sp",
504 vendor_available: true,
505 vndk: {
506 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800507 support_system_process: true,
508 },
509 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900510 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800511 }
512
513 cc_library {
514 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900515 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800516 vndk: {
517 enabled: true,
518 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900519 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800520 },
521 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900522 target: {
523 vendor: {
524 suffix: "-x",
525 },
526 },
Logan Chienf3511742017-10-31 18:04:35 +0800527 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900528
529 cc_library {
530 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900531 vendor_available: true,
532 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900533 vndk: {
534 enabled: true,
535 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900536 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900537 },
538 nocrt: true,
539 target: {
540 vendor: {
541 suffix: "-x",
542 },
543 product: {
544 suffix: "-x",
545 },
546 },
547 }
548
Justin Yun450ae722021-04-16 19:58:18 +0900549 cc_library {
550 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700551 llndk: {
552 symbol_file: "libllndk.map.txt",
553 export_llndk_headers: ["libllndk_headers"],
554 }
Justin Yun450ae722021-04-16 19:58:18 +0900555 }
556
Colin Cross627280f2021-04-26 16:53:58 -0700557 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900558 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700559 llndk: {
560 llndk_headers: true,
561 },
Justin Yun450ae722021-04-16 19:58:18 +0900562 export_include_dirs: ["include"],
563 }
564
Colin Crosse4e44bc2020-12-28 13:50:21 -0800565 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900566 name: "llndk.libraries.txt",
567 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800568 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900569 name: "vndkcore.libraries.txt",
570 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800571 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900572 name: "vndksp.libraries.txt",
573 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800574 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900575 name: "vndkprivate.libraries.txt",
576 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800577 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900578 name: "vndkproduct.libraries.txt",
579 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800580 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900581 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800582 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900583 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800584 `
585
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000586 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800587 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900588 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900589 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800590
591 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800592
Jooyung Han261e1582020-10-20 18:54:21 +0900593 // subdir == "" because VNDK libs are not supposed to be installed separately.
594 // They are installed as part of VNDK APEX instead.
595 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
596 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900597 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900598 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
599 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900600 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900601
Justin Yun6977e8a2020-10-29 18:24:11 +0900602 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
603 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900604
Inseob Kim1f086e22019-05-09 13:29:15 +0900605 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900606 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000607 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900608
609 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
610 "arm64", "armv8-a"))
611 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
612 "arm", "armv7-a-neon"))
613
614 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
615 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900616 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
617
Inseob Kim1f086e22019-05-09 13:29:15 +0900618 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
619 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900620 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900621
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900622 variant := "android_vendor.29_arm64_armv8-a_shared"
623 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900624
Inseob Kim7f283f42020-06-01 21:53:49 +0900625 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
626
627 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
628 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900629 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
630 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900631 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
632 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Justin Yun450ae722021-04-16 19:58:18 +0900633 checkSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
634 checkSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900635
Jooyung Han39edb6c2019-11-06 16:53:07 +0900636 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900637 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
638 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
639 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
640 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900641 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900642
Jooyung Han097087b2019-10-22 19:32:18 +0900643 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
644 "LLNDK: libc.so",
645 "LLNDK: libdl.so",
646 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900647 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900648 "LLNDK: libm.so",
649 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900650 "VNDK-SP: libvndk_sp-x.so",
651 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900652 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900653 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900654 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900655 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900656 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900657 "VNDK-private: libvndk-private.so",
658 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900659 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900660 "VNDK-product: libc++.so",
661 "VNDK-product: libvndk_product.so",
662 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900663 })
Justin Yun450ae722021-04-16 19:58:18 +0900664 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900665 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
666 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
667 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 +0900668 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 +0900669 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
670}
671
Yo Chiangbba545e2020-06-09 16:15:37 +0800672func TestVndkWithHostSupported(t *testing.T) {
673 ctx := testCc(t, `
674 cc_library {
675 name: "libvndk_host_supported",
676 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900677 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800678 vndk: {
679 enabled: true,
680 },
681 host_supported: true,
682 }
683
684 cc_library {
685 name: "libvndk_host_supported_but_disabled_on_device",
686 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900687 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800688 vndk: {
689 enabled: true,
690 },
691 host_supported: true,
692 enabled: false,
693 target: {
694 host: {
695 enabled: true,
696 }
697 }
698 }
699
Colin Crosse4e44bc2020-12-28 13:50:21 -0800700 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800701 name: "vndkcore.libraries.txt",
702 }
703 `)
704
705 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
706}
707
Jooyung Han2216fb12019-11-06 16:46:15 +0900708func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800709 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800710 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900711 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800712 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800713 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000714 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800715 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900716 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800717 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900718
719 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Colin Crossaa255532020-07-03 13:18:24 -0700720 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900721 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900722}
723
724func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800725 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900726 cc_library {
727 name: "libvndk",
728 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900729 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900730 vndk: {
731 enabled: true,
732 },
733 nocrt: true,
734 }
735
736 cc_library {
737 name: "libvndk_sp",
738 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900739 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900740 vndk: {
741 enabled: true,
742 support_system_process: true,
743 },
744 nocrt: true,
745 }
746
747 cc_library {
748 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900749 vendor_available: true,
750 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900751 vndk: {
752 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900753 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900754 },
755 nocrt: true,
756 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900757
Colin Crosse4e44bc2020-12-28 13:50:21 -0800758 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900759 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800760 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900761 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800762 `
763
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000764 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800765 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900766 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800767 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
768
769 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
770
771 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900772
Jooyung Han2216fb12019-11-06 16:46:15 +0900773 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900774}
775
Chris Parsons79d66a52020-06-05 17:26:16 -0400776func TestDataLibs(t *testing.T) {
777 bp := `
778 cc_test_library {
779 name: "test_lib",
780 srcs: ["test_lib.cpp"],
781 gtest: false,
782 }
783
784 cc_test {
785 name: "main_test",
786 data_libs: ["test_lib"],
787 gtest: false,
788 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400789 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400790
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000791 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400792 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900793 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400794 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
795
796 ctx := testCcWithConfig(t, config)
797 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
798 testBinary := module.(*Module).linker.(*testBinary)
799 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
800 if err != nil {
801 t.Errorf("Expected cc_test to produce output files, error: %s", err)
802 return
803 }
804 if len(outputFiles) != 1 {
805 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
806 return
807 }
808 if len(testBinary.dataPaths()) != 1 {
809 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
810 return
811 }
812
813 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400814 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400815
816 if !strings.HasSuffix(outputPath, "/main_test") {
817 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
818 return
819 }
820 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
821 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
822 return
823 }
824}
825
Chris Parsons216e10a2020-07-09 17:12:52 -0400826func TestDataLibsRelativeInstallPath(t *testing.T) {
827 bp := `
828 cc_test_library {
829 name: "test_lib",
830 srcs: ["test_lib.cpp"],
831 relative_install_path: "foo/bar/baz",
832 gtest: false,
833 }
834
835 cc_test {
836 name: "main_test",
837 data_libs: ["test_lib"],
838 gtest: false,
839 }
840 `
841
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000842 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400843 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900844 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400845 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
846
847 ctx := testCcWithConfig(t, config)
848 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
849 testBinary := module.(*Module).linker.(*testBinary)
850 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
851 if err != nil {
852 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
853 }
854 if len(outputFiles) != 1 {
855 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
856 }
857 if len(testBinary.dataPaths()) != 1 {
858 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
859 }
860
861 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400862
863 if !strings.HasSuffix(outputPath, "/main_test") {
864 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
865 }
Colin Crossaa255532020-07-03 13:18:24 -0700866 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400867 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
868 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400869 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400870 }
871}
872
Jooyung Han0302a842019-10-30 18:43:49 +0900873func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900874 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900875 cc_library {
876 name: "libvndk",
877 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900878 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900879 vndk: {
880 enabled: true,
881 },
882 nocrt: true,
883 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900884 cc_library {
885 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900886 vendor_available: true,
887 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900888 vndk: {
889 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900890 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900891 },
892 nocrt: true,
893 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800894
895 cc_library {
896 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700897 llndk: {
898 symbol_file: "libllndk.map.txt",
899 export_llndk_headers: ["libllndk_headers"],
900 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800901 }
902
Colin Cross627280f2021-04-26 16:53:58 -0700903 cc_library_headers {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800904 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700905 llndk: {
906 symbol_file: "libllndk.map.txt",
907 },
Colin Crossb5f6fa62021-01-06 17:05:04 -0800908 export_include_dirs: ["include"],
909 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900910 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900911
912 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
913 "LLNDK: libc.so",
914 "LLNDK: libdl.so",
915 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800916 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900917 "LLNDK: libm.so",
918 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900919 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900920 "VNDK-core: libvndk.so",
921 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900922 "VNDK-private: libvndk-private.so",
923 "VNDK-product: libc++.so",
924 "VNDK-product: libvndk-private.so",
925 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900926 })
Logan Chienf3511742017-10-31 18:04:35 +0800927}
928
Justin Yun63e9ec72020-10-29 16:49:43 +0900929func TestVndkModuleError(t *testing.T) {
930 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900931 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900932 cc_library {
933 name: "libvndk",
934 vndk: {
935 enabled: true,
936 },
937 nocrt: true,
938 }
939 `)
940
Justin Yunc0d8c492021-01-07 17:45:31 +0900941 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900942 cc_library {
943 name: "libvndk",
944 product_available: true,
945 vndk: {
946 enabled: true,
947 },
948 nocrt: true,
949 }
950 `)
951
Justin Yun6977e8a2020-10-29 18:24:11 +0900952 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
953 cc_library {
954 name: "libvndkprop",
955 vendor_available: true,
956 product_available: true,
957 vndk: {
958 enabled: true,
959 },
960 nocrt: true,
961 target: {
962 vendor: {
963 cflags: ["-DTEST",],
964 },
965 },
966 }
967 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900968}
969
Logan Chiend3c59a22018-03-29 14:08:15 +0800970func TestVndkDepError(t *testing.T) {
971 // Check whether an error is emitted when a VNDK lib depends on a system lib.
972 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
973 cc_library {
974 name: "libvndk",
975 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900976 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800977 vndk: {
978 enabled: true,
979 },
980 shared_libs: ["libfwk"], // Cause error
981 nocrt: true,
982 }
983
984 cc_library {
985 name: "libfwk",
986 nocrt: true,
987 }
988 `)
989
990 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
991 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
992 cc_library {
993 name: "libvndk",
994 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900995 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800996 vndk: {
997 enabled: true,
998 },
999 shared_libs: ["libvendor"], // Cause error
1000 nocrt: true,
1001 }
1002
1003 cc_library {
1004 name: "libvendor",
1005 vendor: true,
1006 nocrt: true,
1007 }
1008 `)
1009
1010 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
1011 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1012 cc_library {
1013 name: "libvndk_sp",
1014 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001015 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001016 vndk: {
1017 enabled: true,
1018 support_system_process: true,
1019 },
1020 shared_libs: ["libfwk"], // Cause error
1021 nocrt: true,
1022 }
1023
1024 cc_library {
1025 name: "libfwk",
1026 nocrt: true,
1027 }
1028 `)
1029
1030 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
1031 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1032 cc_library {
1033 name: "libvndk_sp",
1034 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001035 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001036 vndk: {
1037 enabled: true,
1038 support_system_process: true,
1039 },
1040 shared_libs: ["libvendor"], // Cause error
1041 nocrt: true,
1042 }
1043
1044 cc_library {
1045 name: "libvendor",
1046 vendor: true,
1047 nocrt: true,
1048 }
1049 `)
1050
1051 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
1052 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1053 cc_library {
1054 name: "libvndk_sp",
1055 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001056 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001057 vndk: {
1058 enabled: true,
1059 support_system_process: true,
1060 },
1061 shared_libs: ["libvndk"], // Cause error
1062 nocrt: true,
1063 }
1064
1065 cc_library {
1066 name: "libvndk",
1067 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001068 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001069 vndk: {
1070 enabled: true,
1071 },
1072 nocrt: true,
1073 }
1074 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001075
1076 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1077 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1078 cc_library {
1079 name: "libvndk",
1080 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001081 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001082 vndk: {
1083 enabled: true,
1084 },
1085 shared_libs: ["libnonvndk"],
1086 nocrt: true,
1087 }
1088
1089 cc_library {
1090 name: "libnonvndk",
1091 vendor_available: true,
1092 nocrt: true,
1093 }
1094 `)
1095
1096 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1097 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1098 cc_library {
1099 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001100 vendor_available: true,
1101 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001102 vndk: {
1103 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001104 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001105 },
1106 shared_libs: ["libnonvndk"],
1107 nocrt: true,
1108 }
1109
1110 cc_library {
1111 name: "libnonvndk",
1112 vendor_available: true,
1113 nocrt: true,
1114 }
1115 `)
1116
1117 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1118 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1119 cc_library {
1120 name: "libvndksp",
1121 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001122 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001123 vndk: {
1124 enabled: true,
1125 support_system_process: true,
1126 },
1127 shared_libs: ["libnonvndk"],
1128 nocrt: true,
1129 }
1130
1131 cc_library {
1132 name: "libnonvndk",
1133 vendor_available: true,
1134 nocrt: true,
1135 }
1136 `)
1137
1138 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1139 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1140 cc_library {
1141 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001142 vendor_available: true,
1143 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001144 vndk: {
1145 enabled: true,
1146 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001147 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001148 },
1149 shared_libs: ["libnonvndk"],
1150 nocrt: true,
1151 }
1152
1153 cc_library {
1154 name: "libnonvndk",
1155 vendor_available: true,
1156 nocrt: true,
1157 }
1158 `)
1159}
1160
1161func TestDoubleLoadbleDep(t *testing.T) {
1162 // okay to link : LLNDK -> double_loadable VNDK
1163 testCc(t, `
1164 cc_library {
1165 name: "libllndk",
1166 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001167 llndk: {
1168 symbol_file: "libllndk.map.txt",
1169 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001170 }
1171
1172 cc_library {
1173 name: "libdoubleloadable",
1174 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001175 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001176 vndk: {
1177 enabled: true,
1178 },
1179 double_loadable: true,
1180 }
1181 `)
1182 // okay to link : LLNDK -> VNDK-SP
1183 testCc(t, `
1184 cc_library {
1185 name: "libllndk",
1186 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001187 llndk: {
1188 symbol_file: "libllndk.map.txt",
1189 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001190 }
1191
1192 cc_library {
1193 name: "libvndksp",
1194 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001195 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001196 vndk: {
1197 enabled: true,
1198 support_system_process: true,
1199 },
1200 }
1201 `)
1202 // okay to link : double_loadable -> double_loadable
1203 testCc(t, `
1204 cc_library {
1205 name: "libdoubleloadable1",
1206 shared_libs: ["libdoubleloadable2"],
1207 vendor_available: true,
1208 double_loadable: true,
1209 }
1210
1211 cc_library {
1212 name: "libdoubleloadable2",
1213 vendor_available: true,
1214 double_loadable: true,
1215 }
1216 `)
1217 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1218 testCc(t, `
1219 cc_library {
1220 name: "libdoubleloadable",
1221 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001222 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001223 vndk: {
1224 enabled: true,
1225 },
1226 double_loadable: true,
1227 shared_libs: ["libnondoubleloadable"],
1228 }
1229
1230 cc_library {
1231 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001232 vendor_available: true,
1233 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001234 vndk: {
1235 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001236 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001237 },
1238 double_loadable: true,
1239 }
1240 `)
1241 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1242 testCc(t, `
1243 cc_library {
1244 name: "libllndk",
1245 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001246 llndk: {
1247 symbol_file: "libllndk.map.txt",
1248 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001249 }
1250
1251 cc_library {
1252 name: "libcoreonly",
1253 shared_libs: ["libvendoravailable"],
1254 }
1255
1256 // indirect dependency of LLNDK
1257 cc_library {
1258 name: "libvendoravailable",
1259 vendor_available: true,
1260 double_loadable: true,
1261 }
1262 `)
1263}
1264
1265func TestDoubleLoadableDepError(t *testing.T) {
1266 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1267 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1268 cc_library {
1269 name: "libllndk",
1270 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001271 llndk: {
1272 symbol_file: "libllndk.map.txt",
1273 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001274 }
1275
1276 cc_library {
1277 name: "libnondoubleloadable",
1278 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001279 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001280 vndk: {
1281 enabled: true,
1282 },
1283 }
1284 `)
1285
1286 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1287 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1288 cc_library {
1289 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001290 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001291 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001292 llndk: {
1293 symbol_file: "libllndk.map.txt",
1294 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001295 }
1296
1297 cc_library {
1298 name: "libnondoubleloadable",
1299 vendor_available: true,
1300 }
1301 `)
1302
Jooyung Hana70f0672019-01-18 15:20:43 +09001303 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1304 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1305 cc_library {
1306 name: "libllndk",
1307 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001308 llndk: {
1309 symbol_file: "libllndk.map.txt",
1310 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001311 }
1312
1313 cc_library {
1314 name: "libcoreonly",
1315 shared_libs: ["libvendoravailable"],
1316 }
1317
1318 // indirect dependency of LLNDK
1319 cc_library {
1320 name: "libvendoravailable",
1321 vendor_available: true,
1322 }
1323 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001324
1325 // The error is not from 'client' but from 'libllndk'
1326 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1327 cc_library {
1328 name: "client",
1329 vendor_available: true,
1330 double_loadable: true,
1331 shared_libs: ["libllndk"],
1332 }
1333 cc_library {
1334 name: "libllndk",
1335 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001336 llndk: {
1337 symbol_file: "libllndk.map.txt",
1338 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001339 }
1340 cc_library {
1341 name: "libnondoubleloadable",
1342 vendor_available: true,
1343 }
1344 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001345}
1346
Jooyung Han479ca172020-10-19 18:51:07 +09001347func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1348 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1349 cc_library {
1350 name: "libvndksp",
1351 shared_libs: ["libanothervndksp"],
1352 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001353 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001354 vndk: {
1355 enabled: true,
1356 support_system_process: true,
1357 }
1358 }
1359
1360 cc_library {
1361 name: "libllndk",
1362 shared_libs: ["libanothervndksp"],
1363 }
1364
Jooyung Han479ca172020-10-19 18:51:07 +09001365 cc_library {
1366 name: "libanothervndksp",
1367 vendor_available: true,
1368 }
1369 `)
1370}
1371
Logan Chienf3511742017-10-31 18:04:35 +08001372func TestVndkExt(t *testing.T) {
1373 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001374 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001375 cc_library {
1376 name: "libvndk",
1377 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001378 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001379 vndk: {
1380 enabled: true,
1381 },
1382 nocrt: true,
1383 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001384 cc_library {
1385 name: "libvndk2",
1386 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001387 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001388 vndk: {
1389 enabled: true,
1390 },
1391 target: {
1392 vendor: {
1393 suffix: "-suffix",
1394 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001395 product: {
1396 suffix: "-suffix",
1397 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001398 },
1399 nocrt: true,
1400 }
Logan Chienf3511742017-10-31 18:04:35 +08001401
1402 cc_library {
1403 name: "libvndk_ext",
1404 vendor: true,
1405 vndk: {
1406 enabled: true,
1407 extends: "libvndk",
1408 },
1409 nocrt: true,
1410 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001411
1412 cc_library {
1413 name: "libvndk2_ext",
1414 vendor: true,
1415 vndk: {
1416 enabled: true,
1417 extends: "libvndk2",
1418 },
1419 nocrt: true,
1420 }
Logan Chienf3511742017-10-31 18:04:35 +08001421
Justin Yun0ecf0b22020-02-28 15:07:59 +09001422 cc_library {
1423 name: "libvndk_ext_product",
1424 product_specific: true,
1425 vndk: {
1426 enabled: true,
1427 extends: "libvndk",
1428 },
1429 nocrt: true,
1430 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001431
Justin Yun0ecf0b22020-02-28 15:07:59 +09001432 cc_library {
1433 name: "libvndk2_ext_product",
1434 product_specific: true,
1435 vndk: {
1436 enabled: true,
1437 extends: "libvndk2",
1438 },
1439 nocrt: true,
1440 }
1441 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001442 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001443 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1444 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001445 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001446
1447 ctx := testCcWithConfig(t, config)
1448
1449 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1450 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1451
1452 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1453 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1454
1455 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1456 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001457}
1458
Logan Chiend3c59a22018-03-29 14:08:15 +08001459func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001460 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1461 ctx := testCcNoVndk(t, `
1462 cc_library {
1463 name: "libvndk",
1464 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001465 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001466 vndk: {
1467 enabled: true,
1468 },
1469 nocrt: true,
1470 }
1471
1472 cc_library {
1473 name: "libvndk_ext",
1474 vendor: true,
1475 vndk: {
1476 enabled: true,
1477 extends: "libvndk",
1478 },
1479 nocrt: true,
1480 }
1481 `)
1482
1483 // Ensures that the core variant of "libvndk_ext" can be found.
1484 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1485 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1486 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1487 }
1488}
1489
Justin Yun0ecf0b22020-02-28 15:07:59 +09001490func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1491 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001492 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001493 cc_library {
1494 name: "libvndk",
1495 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001496 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001497 vndk: {
1498 enabled: true,
1499 },
1500 nocrt: true,
1501 }
1502
1503 cc_library {
1504 name: "libvndk_ext_product",
1505 product_specific: true,
1506 vndk: {
1507 enabled: true,
1508 extends: "libvndk",
1509 },
1510 nocrt: true,
1511 }
1512 `)
1513
1514 // Ensures that the core variant of "libvndk_ext_product" can be found.
1515 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1516 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1517 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1518 }
1519}
1520
Logan Chienf3511742017-10-31 18:04:35 +08001521func TestVndkExtError(t *testing.T) {
1522 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001523 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001524 cc_library {
1525 name: "libvndk",
1526 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001527 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001528 vndk: {
1529 enabled: true,
1530 },
1531 nocrt: true,
1532 }
1533
1534 cc_library {
1535 name: "libvndk_ext",
1536 vndk: {
1537 enabled: true,
1538 extends: "libvndk",
1539 },
1540 nocrt: true,
1541 }
1542 `)
1543
1544 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1545 cc_library {
1546 name: "libvndk",
1547 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001548 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001549 vndk: {
1550 enabled: true,
1551 },
1552 nocrt: true,
1553 }
1554
1555 cc_library {
1556 name: "libvndk_ext",
1557 vendor: true,
1558 vndk: {
1559 enabled: true,
1560 },
1561 nocrt: true,
1562 }
1563 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001564
1565 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1566 cc_library {
1567 name: "libvndk",
1568 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001569 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001570 vndk: {
1571 enabled: true,
1572 },
1573 nocrt: true,
1574 }
1575
1576 cc_library {
1577 name: "libvndk_ext_product",
1578 product_specific: true,
1579 vndk: {
1580 enabled: true,
1581 },
1582 nocrt: true,
1583 }
1584 `)
1585
1586 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1587 cc_library {
1588 name: "libvndk",
1589 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001590 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001591 vndk: {
1592 enabled: true,
1593 },
1594 nocrt: true,
1595 }
1596
1597 cc_library {
1598 name: "libvndk_ext_product",
1599 product_specific: true,
1600 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001601 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001602 vndk: {
1603 enabled: true,
1604 extends: "libvndk",
1605 },
1606 nocrt: true,
1607 }
1608 `)
Logan Chienf3511742017-10-31 18:04:35 +08001609}
1610
1611func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1612 // This test ensures an error is emitted for inconsistent support_system_process.
1613 testCcError(t, "module \".*\" with mismatched support_system_process", `
1614 cc_library {
1615 name: "libvndk",
1616 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001617 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001618 vndk: {
1619 enabled: true,
1620 },
1621 nocrt: true,
1622 }
1623
1624 cc_library {
1625 name: "libvndk_sp_ext",
1626 vendor: true,
1627 vndk: {
1628 enabled: true,
1629 extends: "libvndk",
1630 support_system_process: true,
1631 },
1632 nocrt: true,
1633 }
1634 `)
1635
1636 testCcError(t, "module \".*\" with mismatched support_system_process", `
1637 cc_library {
1638 name: "libvndk_sp",
1639 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001640 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001641 vndk: {
1642 enabled: true,
1643 support_system_process: true,
1644 },
1645 nocrt: true,
1646 }
1647
1648 cc_library {
1649 name: "libvndk_ext",
1650 vendor: true,
1651 vndk: {
1652 enabled: true,
1653 extends: "libvndk_sp",
1654 },
1655 nocrt: true,
1656 }
1657 `)
1658}
1659
1660func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001661 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001662 // with `private: true`.
1663 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001664 cc_library {
1665 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001666 vendor_available: true,
1667 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001668 vndk: {
1669 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001670 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001671 },
1672 nocrt: true,
1673 }
1674
1675 cc_library {
1676 name: "libvndk_ext",
1677 vendor: true,
1678 vndk: {
1679 enabled: true,
1680 extends: "libvndk",
1681 },
1682 nocrt: true,
1683 }
1684 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001685
Justin Yunfd9e8042020-12-23 18:23:14 +09001686 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001687 cc_library {
1688 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001689 vendor_available: true,
1690 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001691 vndk: {
1692 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001693 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001694 },
1695 nocrt: true,
1696 }
1697
1698 cc_library {
1699 name: "libvndk_ext_product",
1700 product_specific: true,
1701 vndk: {
1702 enabled: true,
1703 extends: "libvndk",
1704 },
1705 nocrt: true,
1706 }
1707 `)
Logan Chienf3511742017-10-31 18:04:35 +08001708}
1709
Logan Chiend3c59a22018-03-29 14:08:15 +08001710func TestVendorModuleUseVndkExt(t *testing.T) {
1711 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001712 testCc(t, `
1713 cc_library {
1714 name: "libvndk",
1715 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001716 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001717 vndk: {
1718 enabled: true,
1719 },
1720 nocrt: true,
1721 }
1722
1723 cc_library {
1724 name: "libvndk_ext",
1725 vendor: true,
1726 vndk: {
1727 enabled: true,
1728 extends: "libvndk",
1729 },
1730 nocrt: true,
1731 }
1732
1733 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001734 name: "libvndk_sp",
1735 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001736 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001737 vndk: {
1738 enabled: true,
1739 support_system_process: true,
1740 },
1741 nocrt: true,
1742 }
1743
1744 cc_library {
1745 name: "libvndk_sp_ext",
1746 vendor: true,
1747 vndk: {
1748 enabled: true,
1749 extends: "libvndk_sp",
1750 support_system_process: true,
1751 },
1752 nocrt: true,
1753 }
1754
1755 cc_library {
1756 name: "libvendor",
1757 vendor: true,
1758 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1759 nocrt: true,
1760 }
1761 `)
1762}
1763
Logan Chiend3c59a22018-03-29 14:08:15 +08001764func TestVndkExtUseVendorLib(t *testing.T) {
1765 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001766 testCc(t, `
1767 cc_library {
1768 name: "libvndk",
1769 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001770 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001771 vndk: {
1772 enabled: true,
1773 },
1774 nocrt: true,
1775 }
1776
1777 cc_library {
1778 name: "libvndk_ext",
1779 vendor: true,
1780 vndk: {
1781 enabled: true,
1782 extends: "libvndk",
1783 },
1784 shared_libs: ["libvendor"],
1785 nocrt: true,
1786 }
1787
1788 cc_library {
1789 name: "libvendor",
1790 vendor: true,
1791 nocrt: true,
1792 }
1793 `)
Logan Chienf3511742017-10-31 18:04:35 +08001794
Logan Chiend3c59a22018-03-29 14:08:15 +08001795 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1796 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001797 cc_library {
1798 name: "libvndk_sp",
1799 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001800 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001801 vndk: {
1802 enabled: true,
1803 support_system_process: true,
1804 },
1805 nocrt: true,
1806 }
1807
1808 cc_library {
1809 name: "libvndk_sp_ext",
1810 vendor: true,
1811 vndk: {
1812 enabled: true,
1813 extends: "libvndk_sp",
1814 support_system_process: true,
1815 },
1816 shared_libs: ["libvendor"], // Cause an error
1817 nocrt: true,
1818 }
1819
1820 cc_library {
1821 name: "libvendor",
1822 vendor: true,
1823 nocrt: true,
1824 }
1825 `)
1826}
1827
Justin Yun0ecf0b22020-02-28 15:07:59 +09001828func TestProductVndkExtDependency(t *testing.T) {
1829 bp := `
1830 cc_library {
1831 name: "libvndk",
1832 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001833 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001834 vndk: {
1835 enabled: true,
1836 },
1837 nocrt: true,
1838 }
1839
1840 cc_library {
1841 name: "libvndk_ext_product",
1842 product_specific: true,
1843 vndk: {
1844 enabled: true,
1845 extends: "libvndk",
1846 },
1847 shared_libs: ["libproduct_for_vndklibs"],
1848 nocrt: true,
1849 }
1850
1851 cc_library {
1852 name: "libvndk_sp",
1853 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001854 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001855 vndk: {
1856 enabled: true,
1857 support_system_process: true,
1858 },
1859 nocrt: true,
1860 }
1861
1862 cc_library {
1863 name: "libvndk_sp_ext_product",
1864 product_specific: true,
1865 vndk: {
1866 enabled: true,
1867 extends: "libvndk_sp",
1868 support_system_process: true,
1869 },
1870 shared_libs: ["libproduct_for_vndklibs"],
1871 nocrt: true,
1872 }
1873
1874 cc_library {
1875 name: "libproduct",
1876 product_specific: true,
1877 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1878 nocrt: true,
1879 }
1880
1881 cc_library {
1882 name: "libproduct_for_vndklibs",
1883 product_specific: true,
1884 nocrt: true,
1885 }
1886 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001887 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001888 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1889 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001890 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001891
1892 testCcWithConfig(t, config)
1893}
1894
Logan Chiend3c59a22018-03-29 14:08:15 +08001895func TestVndkSpExtUseVndkError(t *testing.T) {
1896 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1897 // library.
1898 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1899 cc_library {
1900 name: "libvndk",
1901 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001902 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001903 vndk: {
1904 enabled: true,
1905 },
1906 nocrt: true,
1907 }
1908
1909 cc_library {
1910 name: "libvndk_sp",
1911 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001912 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001913 vndk: {
1914 enabled: true,
1915 support_system_process: true,
1916 },
1917 nocrt: true,
1918 }
1919
1920 cc_library {
1921 name: "libvndk_sp_ext",
1922 vendor: true,
1923 vndk: {
1924 enabled: true,
1925 extends: "libvndk_sp",
1926 support_system_process: true,
1927 },
1928 shared_libs: ["libvndk"], // Cause an error
1929 nocrt: true,
1930 }
1931 `)
1932
1933 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1934 // library.
1935 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1936 cc_library {
1937 name: "libvndk",
1938 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001939 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001940 vndk: {
1941 enabled: true,
1942 },
1943 nocrt: true,
1944 }
1945
1946 cc_library {
1947 name: "libvndk_ext",
1948 vendor: true,
1949 vndk: {
1950 enabled: true,
1951 extends: "libvndk",
1952 },
1953 nocrt: true,
1954 }
1955
1956 cc_library {
1957 name: "libvndk_sp",
1958 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001959 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001960 vndk: {
1961 enabled: true,
1962 support_system_process: true,
1963 },
1964 nocrt: true,
1965 }
1966
1967 cc_library {
1968 name: "libvndk_sp_ext",
1969 vendor: true,
1970 vndk: {
1971 enabled: true,
1972 extends: "libvndk_sp",
1973 support_system_process: true,
1974 },
1975 shared_libs: ["libvndk_ext"], // Cause an error
1976 nocrt: true,
1977 }
1978 `)
1979}
1980
1981func TestVndkUseVndkExtError(t *testing.T) {
1982 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1983 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001984 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1985 cc_library {
1986 name: "libvndk",
1987 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001988 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001989 vndk: {
1990 enabled: true,
1991 },
1992 nocrt: true,
1993 }
1994
1995 cc_library {
1996 name: "libvndk_ext",
1997 vendor: true,
1998 vndk: {
1999 enabled: true,
2000 extends: "libvndk",
2001 },
2002 nocrt: true,
2003 }
2004
2005 cc_library {
2006 name: "libvndk2",
2007 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002008 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002009 vndk: {
2010 enabled: true,
2011 },
2012 shared_libs: ["libvndk_ext"],
2013 nocrt: true,
2014 }
2015 `)
2016
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002017 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002018 cc_library {
2019 name: "libvndk",
2020 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002021 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002022 vndk: {
2023 enabled: true,
2024 },
2025 nocrt: true,
2026 }
2027
2028 cc_library {
2029 name: "libvndk_ext",
2030 vendor: true,
2031 vndk: {
2032 enabled: true,
2033 extends: "libvndk",
2034 },
2035 nocrt: true,
2036 }
2037
2038 cc_library {
2039 name: "libvndk2",
2040 vendor_available: true,
2041 vndk: {
2042 enabled: true,
2043 },
2044 target: {
2045 vendor: {
2046 shared_libs: ["libvndk_ext"],
2047 },
2048 },
2049 nocrt: true,
2050 }
2051 `)
2052
2053 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2054 cc_library {
2055 name: "libvndk_sp",
2056 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002057 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002058 vndk: {
2059 enabled: true,
2060 support_system_process: true,
2061 },
2062 nocrt: true,
2063 }
2064
2065 cc_library {
2066 name: "libvndk_sp_ext",
2067 vendor: true,
2068 vndk: {
2069 enabled: true,
2070 extends: "libvndk_sp",
2071 support_system_process: true,
2072 },
2073 nocrt: true,
2074 }
2075
2076 cc_library {
2077 name: "libvndk_sp_2",
2078 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002079 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002080 vndk: {
2081 enabled: true,
2082 support_system_process: true,
2083 },
2084 shared_libs: ["libvndk_sp_ext"],
2085 nocrt: true,
2086 }
2087 `)
2088
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002089 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002090 cc_library {
2091 name: "libvndk_sp",
2092 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002093 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002094 vndk: {
2095 enabled: true,
2096 },
2097 nocrt: true,
2098 }
2099
2100 cc_library {
2101 name: "libvndk_sp_ext",
2102 vendor: true,
2103 vndk: {
2104 enabled: true,
2105 extends: "libvndk_sp",
2106 },
2107 nocrt: true,
2108 }
2109
2110 cc_library {
2111 name: "libvndk_sp2",
2112 vendor_available: true,
2113 vndk: {
2114 enabled: true,
2115 },
2116 target: {
2117 vendor: {
2118 shared_libs: ["libvndk_sp_ext"],
2119 },
2120 },
2121 nocrt: true,
2122 }
2123 `)
2124}
2125
Justin Yun5f7f7e82019-11-18 19:52:14 +09002126func TestEnforceProductVndkVersion(t *testing.T) {
2127 bp := `
2128 cc_library {
2129 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002130 llndk: {
2131 symbol_file: "libllndk.map.txt",
2132 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002133 }
2134 cc_library {
2135 name: "libvndk",
2136 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002137 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002138 vndk: {
2139 enabled: true,
2140 },
2141 nocrt: true,
2142 }
2143 cc_library {
2144 name: "libvndk_sp",
2145 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002146 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002147 vndk: {
2148 enabled: true,
2149 support_system_process: true,
2150 },
2151 nocrt: true,
2152 }
2153 cc_library {
2154 name: "libva",
2155 vendor_available: true,
2156 nocrt: true,
2157 }
2158 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002159 name: "libpa",
2160 product_available: true,
2161 nocrt: true,
2162 }
2163 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002164 name: "libboth_available",
2165 vendor_available: true,
2166 product_available: true,
2167 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002168 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002169 target: {
2170 vendor: {
2171 suffix: "-vendor",
2172 },
2173 product: {
2174 suffix: "-product",
2175 },
2176 }
2177 }
2178 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002179 name: "libproduct_va",
2180 product_specific: true,
2181 vendor_available: true,
2182 nocrt: true,
2183 }
2184 cc_library {
2185 name: "libprod",
2186 product_specific: true,
2187 shared_libs: [
2188 "libllndk",
2189 "libvndk",
2190 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002191 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002192 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002193 "libproduct_va",
2194 ],
2195 nocrt: true,
2196 }
2197 cc_library {
2198 name: "libvendor",
2199 vendor: true,
2200 shared_libs: [
2201 "libllndk",
2202 "libvndk",
2203 "libvndk_sp",
2204 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002205 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002206 "libproduct_va",
2207 ],
2208 nocrt: true,
2209 }
2210 `
2211
Paul Duffin8567f222021-03-23 00:02:06 +00002212 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002213
Jooyung Han261e1582020-10-20 18:54:21 +09002214 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2215 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002216
2217 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2218 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2219
2220 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2221 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002222
2223 ensureStringContains := func(t *testing.T, str string, substr string) {
2224 t.Helper()
2225 if !strings.Contains(str, substr) {
2226 t.Errorf("%q is not found in %v", substr, str)
2227 }
2228 }
2229 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2230 t.Helper()
2231 if strings.Contains(str, substr) {
2232 t.Errorf("%q is found in %v", substr, str)
2233 }
2234 }
2235
2236 // _static variant is used since _shared reuses *.o from the static variant
2237 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2238 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2239
2240 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2241 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2242 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2243 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2244
2245 product_cflags := product_static.Rule("cc").Args["cFlags"]
2246 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2247 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2248 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002249}
2250
2251func TestEnforceProductVndkVersionErrors(t *testing.T) {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002252 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002253 cc_library {
2254 name: "libprod",
2255 product_specific: true,
2256 shared_libs: [
2257 "libvendor",
2258 ],
2259 nocrt: true,
2260 }
2261 cc_library {
2262 name: "libvendor",
2263 vendor: true,
2264 nocrt: true,
2265 }
2266 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002267 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002268 cc_library {
2269 name: "libprod",
2270 product_specific: true,
2271 shared_libs: [
2272 "libsystem",
2273 ],
2274 nocrt: true,
2275 }
2276 cc_library {
2277 name: "libsystem",
2278 nocrt: true,
2279 }
2280 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002281 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002282 cc_library {
2283 name: "libprod",
2284 product_specific: true,
2285 shared_libs: [
2286 "libva",
2287 ],
2288 nocrt: true,
2289 }
2290 cc_library {
2291 name: "libva",
2292 vendor_available: true,
2293 nocrt: true,
2294 }
2295 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002296 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002297 cc_library {
2298 name: "libprod",
2299 product_specific: true,
2300 shared_libs: [
2301 "libvndk_private",
2302 ],
2303 nocrt: true,
2304 }
2305 cc_library {
2306 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002307 vendor_available: true,
2308 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002309 vndk: {
2310 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002311 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002312 },
2313 nocrt: true,
2314 }
2315 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002316 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002317 cc_library {
2318 name: "libprod",
2319 product_specific: true,
2320 shared_libs: [
2321 "libsystem_ext",
2322 ],
2323 nocrt: true,
2324 }
2325 cc_library {
2326 name: "libsystem_ext",
2327 system_ext_specific: true,
2328 nocrt: true,
2329 }
2330 `)
2331 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2332 cc_library {
2333 name: "libsystem",
2334 shared_libs: [
2335 "libproduct_va",
2336 ],
2337 nocrt: true,
2338 }
2339 cc_library {
2340 name: "libproduct_va",
2341 product_specific: true,
2342 vendor_available: true,
2343 nocrt: true,
2344 }
2345 `)
2346}
2347
Jooyung Han38002912019-05-16 04:01:54 +09002348func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002349 bp := `
2350 cc_library {
2351 name: "libvndk",
2352 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002353 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002354 vndk: {
2355 enabled: true,
2356 },
2357 }
2358 cc_library {
2359 name: "libvndksp",
2360 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002361 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002362 vndk: {
2363 enabled: true,
2364 support_system_process: true,
2365 },
2366 }
2367 cc_library {
2368 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002369 vendor_available: true,
2370 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002371 vndk: {
2372 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002373 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002374 },
2375 }
2376 cc_library {
2377 name: "libvendor",
2378 vendor: true,
2379 }
2380 cc_library {
2381 name: "libvndkext",
2382 vendor: true,
2383 vndk: {
2384 enabled: true,
2385 extends: "libvndk",
2386 },
2387 }
2388 vndk_prebuilt_shared {
2389 name: "prevndk",
2390 version: "27",
2391 target_arch: "arm",
2392 binder32bit: true,
2393 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002394 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002395 vndk: {
2396 enabled: true,
2397 },
2398 arch: {
2399 arm: {
2400 srcs: ["liba.so"],
2401 },
2402 },
2403 }
2404 cc_library {
2405 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002406 llndk: {
2407 symbol_file: "libllndk.map.txt",
2408 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002409 }
2410 cc_library {
2411 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002412 llndk: {
2413 symbol_file: "libllndkprivate.map.txt",
2414 private: true,
2415 }
Colin Cross78212242021-01-06 14:51:30 -08002416 }
2417
2418 llndk_libraries_txt {
2419 name: "llndk.libraries.txt",
2420 }
2421 vndkcore_libraries_txt {
2422 name: "vndkcore.libraries.txt",
2423 }
2424 vndksp_libraries_txt {
2425 name: "vndksp.libraries.txt",
2426 }
2427 vndkprivate_libraries_txt {
2428 name: "vndkprivate.libraries.txt",
2429 }
2430 vndkcorevariant_libraries_txt {
2431 name: "vndkcorevariant.libraries.txt",
2432 insert_vndk_version: false,
2433 }
2434 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002435
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002436 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002437 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002438 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002439 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002440 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002441
Colin Cross78212242021-01-06 14:51:30 -08002442 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2443 []string{"libvndk.so", "libvndkprivate.so"})
2444 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2445 []string{"libc++.so", "libvndksp.so"})
2446 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2447 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2448 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2449 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002450
Colin Crossfb0c16e2019-11-20 17:12:35 -08002451 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002452
Jooyung Han38002912019-05-16 04:01:54 +09002453 tests := []struct {
2454 variant string
2455 name string
2456 expected string
2457 }{
2458 {vendorVariant, "libvndk", "native:vndk"},
2459 {vendorVariant, "libvndksp", "native:vndk"},
2460 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2461 {vendorVariant, "libvendor", "native:vendor"},
2462 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002463 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002464 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002465 {coreVariant, "libvndk", "native:platform"},
2466 {coreVariant, "libvndkprivate", "native:platform"},
2467 {coreVariant, "libllndk", "native:platform"},
2468 }
2469 for _, test := range tests {
2470 t.Run(test.name, func(t *testing.T) {
2471 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2472 assertString(t, module.makeLinkType, test.expected)
2473 })
2474 }
2475}
2476
Jeff Gaston294356f2017-09-27 17:05:30 -07002477var staticLinkDepOrderTestCases = []struct {
2478 // This is a string representation of a map[moduleName][]moduleDependency .
2479 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002480 inStatic string
2481
2482 // This is a string representation of a map[moduleName][]moduleDependency .
2483 // It models the dependencies declared in an Android.bp file.
2484 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002485
2486 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2487 // The keys of allOrdered specify which modules we would like to check.
2488 // The values of allOrdered specify the expected result (of the transitive closure of all
2489 // dependencies) for each module to test
2490 allOrdered string
2491
2492 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2493 // The keys of outOrdered specify which modules we would like to check.
2494 // The values of outOrdered specify the expected result (of the ordered linker command line)
2495 // for each module to test.
2496 outOrdered string
2497}{
2498 // Simple tests
2499 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002500 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002501 outOrdered: "",
2502 },
2503 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002504 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002505 outOrdered: "a:",
2506 },
2507 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002508 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002509 outOrdered: "a:b; b:",
2510 },
2511 // Tests of reordering
2512 {
2513 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002514 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002515 outOrdered: "a:b,c,d; b:d; c:d; d:",
2516 },
2517 {
2518 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002519 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002520 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2521 },
2522 {
2523 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002524 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002525 outOrdered: "a:d,b,e,c; d:b; e:c",
2526 },
2527 {
2528 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002529 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002530 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2531 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2532 },
2533 {
2534 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002535 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 -07002536 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2537 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2538 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002539 // shared dependencies
2540 {
2541 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2542 // So, we don't actually have to check that a shared dependency of c will change the order
2543 // of a library that depends statically on b and on c. We only need to check that if c has
2544 // a shared dependency on b, that that shows up in allOrdered.
2545 inShared: "c:b",
2546 allOrdered: "c:b",
2547 outOrdered: "c:",
2548 },
2549 {
2550 // This test doesn't actually include any shared dependencies but it's a reminder of what
2551 // the second phase of the above test would look like
2552 inStatic: "a:b,c; c:b",
2553 allOrdered: "a:c,b; c:b",
2554 outOrdered: "a:c,b; c:b",
2555 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002556 // tiebreakers for when two modules specifying different orderings and there is no dependency
2557 // to dictate an order
2558 {
2559 // 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 -08002560 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002561 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2562 },
2563 {
2564 // 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 -08002565 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 -07002566 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2567 },
2568 // Tests involving duplicate dependencies
2569 {
2570 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002571 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002572 outOrdered: "a:c,b",
2573 },
2574 {
2575 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002576 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002577 outOrdered: "a:d,c,b",
2578 },
2579 // Tests to confirm the nonexistence of infinite loops.
2580 // These cases should never happen, so as long as the test terminates and the
2581 // result is deterministic then that should be fine.
2582 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002583 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002584 outOrdered: "a:a",
2585 },
2586 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002587 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002588 allOrdered: "a:b,c; b:c,a; c:a,b",
2589 outOrdered: "a:b; b:c; c:a",
2590 },
2591 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002592 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002593 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2594 outOrdered: "a:c,b; b:a,c; c:b,a",
2595 },
2596}
2597
2598// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2599func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2600 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2601 strippedText := strings.Replace(text, " ", "", -1)
2602 if len(strippedText) < 1 {
2603 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2604 }
2605 allDeps = make(map[android.Path][]android.Path, 0)
2606
2607 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2608 moduleTexts := strings.Split(strippedText, ";")
2609
2610 outputForModuleName := func(moduleName string) android.Path {
2611 return android.PathForTesting(moduleName)
2612 }
2613
2614 for _, moduleText := range moduleTexts {
2615 // convert from "a:b,c" to ["a", "b,c"]
2616 components := strings.Split(moduleText, ":")
2617 if len(components) != 2 {
2618 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2619 }
2620 moduleName := components[0]
2621 moduleOutput := outputForModuleName(moduleName)
2622 modulesInOrder = append(modulesInOrder, moduleOutput)
2623
2624 depString := components[1]
2625 // convert from "b,c" to ["b", "c"]
2626 depNames := strings.Split(depString, ",")
2627 if len(depString) < 1 {
2628 depNames = []string{}
2629 }
2630 var deps []android.Path
2631 for _, depName := range depNames {
2632 deps = append(deps, outputForModuleName(depName))
2633 }
2634 allDeps[moduleOutput] = deps
2635 }
2636 return modulesInOrder, allDeps
2637}
2638
Jeff Gaston294356f2017-09-27 17:05:30 -07002639func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2640 for _, moduleName := range moduleNames {
2641 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002642 output := module.outputFile.Path().RelativeToTop()
Jeff Gaston294356f2017-09-27 17:05:30 -07002643 paths = append(paths, output)
2644 }
2645 return paths
2646}
2647
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002648func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002649 ctx := testCc(t, `
2650 cc_library {
2651 name: "a",
2652 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002653 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002654 }
2655 cc_library {
2656 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002657 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002658 }
2659 cc_library {
2660 name: "c",
2661 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002662 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002663 }
2664 cc_library {
2665 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002666 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002667 }
2668
2669 `)
2670
Colin Cross7113d202019-11-20 16:39:12 -08002671 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002672 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002673 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2674 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002675 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002676
2677 if !reflect.DeepEqual(actual, expected) {
2678 t.Errorf("staticDeps orderings were not propagated correctly"+
2679 "\nactual: %v"+
2680 "\nexpected: %v",
2681 actual,
2682 expected,
2683 )
2684 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002685}
Jeff Gaston294356f2017-09-27 17:05:30 -07002686
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002687func TestStaticLibDepReorderingWithShared(t *testing.T) {
2688 ctx := testCc(t, `
2689 cc_library {
2690 name: "a",
2691 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002692 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002693 }
2694 cc_library {
2695 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002696 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002697 }
2698 cc_library {
2699 name: "c",
2700 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002701 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002702 }
2703
2704 `)
2705
Colin Cross7113d202019-11-20 16:39:12 -08002706 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002707 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002708 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2709 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002710 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002711
2712 if !reflect.DeepEqual(actual, expected) {
2713 t.Errorf("staticDeps orderings did not account for shared libs"+
2714 "\nactual: %v"+
2715 "\nexpected: %v",
2716 actual,
2717 expected,
2718 )
2719 }
2720}
2721
Jooyung Hanb04a4992020-03-13 18:57:35 +09002722func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002723 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002724 if !reflect.DeepEqual(actual, expected) {
2725 t.Errorf(message+
2726 "\nactual: %v"+
2727 "\nexpected: %v",
2728 actual,
2729 expected,
2730 )
2731 }
2732}
2733
Jooyung Han61b66e92020-03-21 14:21:46 +00002734func TestLlndkLibrary(t *testing.T) {
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002735 result := prepareForCcTest.RunTestWithBp(t, `
2736 cc_library {
2737 name: "libllndk",
2738 stubs: { versions: ["1", "2"] },
2739 llndk: {
2740 symbol_file: "libllndk.map.txt",
2741 },
2742 export_include_dirs: ["include"],
2743 }
2744
2745 cc_prebuilt_library_shared {
2746 name: "libllndkprebuilt",
2747 stubs: { versions: ["1", "2"] },
2748 llndk: {
2749 symbol_file: "libllndkprebuilt.map.txt",
2750 },
2751 }
2752
2753 cc_library {
2754 name: "libllndk_with_external_headers",
2755 stubs: { versions: ["1", "2"] },
2756 llndk: {
2757 symbol_file: "libllndk.map.txt",
2758 export_llndk_headers: ["libexternal_llndk_headers"],
2759 },
2760 header_libs: ["libexternal_headers"],
2761 export_header_lib_headers: ["libexternal_headers"],
2762 }
2763 cc_library_headers {
2764 name: "libexternal_headers",
2765 export_include_dirs: ["include"],
2766 vendor_available: true,
2767 }
2768 cc_library_headers {
2769 name: "libexternal_llndk_headers",
2770 export_include_dirs: ["include_llndk"],
2771 llndk: {
2772 symbol_file: "libllndk.map.txt",
2773 },
2774 vendor_available: true,
2775 }
2776
2777 cc_library {
2778 name: "libllndk_with_override_headers",
2779 stubs: { versions: ["1", "2"] },
2780 llndk: {
2781 symbol_file: "libllndk.map.txt",
2782 override_export_include_dirs: ["include_llndk"],
2783 },
2784 export_include_dirs: ["include"],
2785 }
2786 `)
2787 actual := result.ModuleVariantsForTests("libllndk")
2788 for i := 0; i < len(actual); i++ {
2789 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2790 actual = append(actual[:i], actual[i+1:]...)
2791 i--
2792 }
2793 }
2794 expected := []string{
2795 "android_vendor.29_arm64_armv8-a_shared_1",
2796 "android_vendor.29_arm64_armv8-a_shared_2",
2797 "android_vendor.29_arm64_armv8-a_shared_current",
2798 "android_vendor.29_arm64_armv8-a_shared",
2799 "android_vendor.29_arm_armv7-a-neon_shared_1",
2800 "android_vendor.29_arm_armv7-a-neon_shared_2",
2801 "android_vendor.29_arm_armv7-a-neon_shared_current",
2802 "android_vendor.29_arm_armv7-a-neon_shared",
2803 }
2804 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2805
2806 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2807 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2808
2809 params = result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared_1").Description("generate stub")
2810 android.AssertSame(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2811
2812 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2813 t.Helper()
2814 m := result.ModuleForTests(module, variant).Module()
2815 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2816 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2817 expectedDirs, f.IncludeDirs)
2818 }
2819
2820 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2821 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2822 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2823 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2824 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2825 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2826}
2827
Jiyong Parka46a4d52017-12-14 19:54:34 +09002828func TestLlndkHeaders(t *testing.T) {
2829 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002830 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002831 name: "libllndk_headers",
2832 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002833 llndk: {
2834 llndk_headers: true,
2835 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002836 }
2837 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002838 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002839 llndk: {
2840 symbol_file: "libllndk.map.txt",
2841 export_llndk_headers: ["libllndk_headers"],
2842 }
Colin Cross0477b422020-10-13 18:43:54 -07002843 }
2844
2845 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002846 name: "libvendor",
2847 shared_libs: ["libllndk"],
2848 vendor: true,
2849 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002850 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002851 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002852 }
2853 `)
2854
2855 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002856 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002857 cflags := cc.Args["cFlags"]
2858 if !strings.Contains(cflags, "-Imy_include") {
2859 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2860 }
2861}
2862
Logan Chien43d34c32017-12-20 01:17:32 +08002863func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2864 actual := module.Properties.AndroidMkRuntimeLibs
2865 if !reflect.DeepEqual(actual, expected) {
2866 t.Errorf("incorrect runtime_libs for shared libs"+
2867 "\nactual: %v"+
2868 "\nexpected: %v",
2869 actual,
2870 expected,
2871 )
2872 }
2873}
2874
2875const runtimeLibAndroidBp = `
2876 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002877 name: "liball_available",
2878 vendor_available: true,
2879 product_available: true,
2880 no_libcrt : true,
2881 nocrt : true,
2882 system_shared_libs : [],
2883 }
2884 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002885 name: "libvendor_available1",
2886 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002887 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002888 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002889 nocrt : true,
2890 system_shared_libs : [],
2891 }
2892 cc_library {
2893 name: "libvendor_available2",
2894 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002895 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002896 target: {
2897 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002898 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002899 }
2900 },
Yi Konge7fe9912019-06-02 00:53:50 -07002901 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002902 nocrt : true,
2903 system_shared_libs : [],
2904 }
2905 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002906 name: "libproduct_vendor",
2907 product_specific: true,
2908 vendor_available: true,
2909 no_libcrt : true,
2910 nocrt : true,
2911 system_shared_libs : [],
2912 }
2913 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002914 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002915 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002916 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002917 nocrt : true,
2918 system_shared_libs : [],
2919 }
2920 cc_library {
2921 name: "libvendor1",
2922 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002923 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002924 nocrt : true,
2925 system_shared_libs : [],
2926 }
2927 cc_library {
2928 name: "libvendor2",
2929 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002930 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002931 no_libcrt : true,
2932 nocrt : true,
2933 system_shared_libs : [],
2934 }
2935 cc_library {
2936 name: "libproduct_available1",
2937 product_available: true,
2938 runtime_libs: ["liball_available"],
2939 no_libcrt : true,
2940 nocrt : true,
2941 system_shared_libs : [],
2942 }
2943 cc_library {
2944 name: "libproduct1",
2945 product_specific: true,
2946 no_libcrt : true,
2947 nocrt : true,
2948 system_shared_libs : [],
2949 }
2950 cc_library {
2951 name: "libproduct2",
2952 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002953 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002954 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002955 nocrt : true,
2956 system_shared_libs : [],
2957 }
2958`
2959
2960func TestRuntimeLibs(t *testing.T) {
2961 ctx := testCc(t, runtimeLibAndroidBp)
2962
2963 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002964 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002965
Justin Yun8a2600c2020-12-07 12:44:03 +09002966 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2967 checkRuntimeLibs(t, []string{"liball_available"}, module)
2968
2969 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2970 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002971
2972 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002973 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002974
2975 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2976 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002977 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002978
Justin Yun8a2600c2020-12-07 12:44:03 +09002979 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2980 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002981
2982 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002983 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002984
2985 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2986 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002987 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002988
2989 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2990 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2991
2992 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002993 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002994}
2995
2996func TestExcludeRuntimeLibs(t *testing.T) {
2997 ctx := testCc(t, runtimeLibAndroidBp)
2998
Colin Cross7113d202019-11-20 16:39:12 -08002999 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003000 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3001 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003002
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003003 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003004 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08003005 checkRuntimeLibs(t, nil, module)
3006}
3007
3008func TestRuntimeLibsNoVndk(t *testing.T) {
3009 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3010
3011 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3012
Colin Cross7113d202019-11-20 16:39:12 -08003013 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003014
Justin Yun8a2600c2020-12-07 12:44:03 +09003015 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3016 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003017
3018 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003019 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003020
3021 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003022 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003023}
3024
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003025func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003026 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003027 actual := module.Properties.AndroidMkStaticLibs
3028 if !reflect.DeepEqual(actual, expected) {
3029 t.Errorf("incorrect static_libs"+
3030 "\nactual: %v"+
3031 "\nexpected: %v",
3032 actual,
3033 expected,
3034 )
3035 }
3036}
3037
3038const staticLibAndroidBp = `
3039 cc_library {
3040 name: "lib1",
3041 }
3042 cc_library {
3043 name: "lib2",
3044 static_libs: ["lib1"],
3045 }
3046`
3047
3048func TestStaticLibDepExport(t *testing.T) {
3049 ctx := testCc(t, staticLibAndroidBp)
3050
3051 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003052 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003053 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Ryan Prichardc2018e22021-04-02 20:23:22 -07003054 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003055
3056 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003057 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003058 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3059 // libc++_static is linked additionally.
Ryan Prichardc2018e22021-04-02 20:23:22 -07003060 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003061}
3062
Jiyong Parkd08b6972017-09-26 10:50:54 +09003063var compilerFlagsTestCases = []struct {
3064 in string
3065 out bool
3066}{
3067 {
3068 in: "a",
3069 out: false,
3070 },
3071 {
3072 in: "-a",
3073 out: true,
3074 },
3075 {
3076 in: "-Ipath/to/something",
3077 out: false,
3078 },
3079 {
3080 in: "-isystempath/to/something",
3081 out: false,
3082 },
3083 {
3084 in: "--coverage",
3085 out: false,
3086 },
3087 {
3088 in: "-include a/b",
3089 out: true,
3090 },
3091 {
3092 in: "-include a/b c/d",
3093 out: false,
3094 },
3095 {
3096 in: "-DMACRO",
3097 out: true,
3098 },
3099 {
3100 in: "-DMAC RO",
3101 out: false,
3102 },
3103 {
3104 in: "-a -b",
3105 out: false,
3106 },
3107 {
3108 in: "-DMACRO=definition",
3109 out: true,
3110 },
3111 {
3112 in: "-DMACRO=defi nition",
3113 out: true, // TODO(jiyong): this should be false
3114 },
3115 {
3116 in: "-DMACRO(x)=x + 1",
3117 out: true,
3118 },
3119 {
3120 in: "-DMACRO=\"defi nition\"",
3121 out: true,
3122 },
3123}
3124
3125type mockContext struct {
3126 BaseModuleContext
3127 result bool
3128}
3129
3130func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3131 // CheckBadCompilerFlags calls this function when the flag should be rejected
3132 ctx.result = false
3133}
3134
3135func TestCompilerFlags(t *testing.T) {
3136 for _, testCase := range compilerFlagsTestCases {
3137 ctx := &mockContext{result: true}
3138 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3139 if ctx.result != testCase.out {
3140 t.Errorf("incorrect output:")
3141 t.Errorf(" input: %#v", testCase.in)
3142 t.Errorf(" expected: %#v", testCase.out)
3143 t.Errorf(" got: %#v", ctx.result)
3144 }
3145 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003146}
Jiyong Park374510b2018-03-19 18:23:01 +09003147
Jiyong Park37b25202018-07-11 10:49:27 +09003148func TestRecovery(t *testing.T) {
3149 ctx := testCc(t, `
3150 cc_library_shared {
3151 name: "librecovery",
3152 recovery: true,
3153 }
3154 cc_library_shared {
3155 name: "librecovery32",
3156 recovery: true,
3157 compile_multilib:"32",
3158 }
Jiyong Park5baac542018-08-28 09:55:37 +09003159 cc_library_shared {
3160 name: "libHalInRecovery",
3161 recovery_available: true,
3162 vendor: true,
3163 }
Jiyong Park37b25202018-07-11 10:49:27 +09003164 `)
3165
3166 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003167 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003168 if len(variants) != 1 || !android.InList(arm64, variants) {
3169 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3170 }
3171
3172 variants = ctx.ModuleVariantsForTests("librecovery32")
3173 if android.InList(arm64, variants) {
3174 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3175 }
Jiyong Park5baac542018-08-28 09:55:37 +09003176
3177 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3178 if !recoveryModule.Platform() {
3179 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3180 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003181}
Jiyong Park5baac542018-08-28 09:55:37 +09003182
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003183func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3184 bp := `
3185 cc_prebuilt_test_library_shared {
3186 name: "test_lib",
3187 relative_install_path: "foo/bar/baz",
3188 srcs: ["srcpath/dontusethispath/baz.so"],
3189 }
3190
3191 cc_test {
3192 name: "main_test",
3193 data_libs: ["test_lib"],
3194 gtest: false,
3195 }
3196 `
3197
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003198 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003199 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003200 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003201 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3202
3203 ctx := testCcWithConfig(t, config)
3204 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3205 testBinary := module.(*Module).linker.(*testBinary)
3206 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3207 if err != nil {
3208 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3209 }
3210 if len(outputFiles) != 1 {
3211 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3212 }
3213 if len(testBinary.dataPaths()) != 1 {
3214 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3215 }
3216
3217 outputPath := outputFiles[0].String()
3218
3219 if !strings.HasSuffix(outputPath, "/main_test") {
3220 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3221 }
Colin Crossaa255532020-07-03 13:18:24 -07003222 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003223 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3224 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3225 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3226 }
3227}
3228
Jiyong Park7ed9de32018-10-15 22:25:07 +09003229func TestVersionedStubs(t *testing.T) {
3230 ctx := testCc(t, `
3231 cc_library_shared {
3232 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003233 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003234 stubs: {
3235 symbol_file: "foo.map.txt",
3236 versions: ["1", "2", "3"],
3237 },
3238 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003239
Jiyong Park7ed9de32018-10-15 22:25:07 +09003240 cc_library_shared {
3241 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003242 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003243 shared_libs: ["libFoo#1"],
3244 }`)
3245
3246 variants := ctx.ModuleVariantsForTests("libFoo")
3247 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003248 "android_arm64_armv8-a_shared",
3249 "android_arm64_armv8-a_shared_1",
3250 "android_arm64_armv8-a_shared_2",
3251 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003252 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003253 "android_arm_armv7-a-neon_shared",
3254 "android_arm_armv7-a-neon_shared_1",
3255 "android_arm_armv7-a-neon_shared_2",
3256 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003257 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003258 }
3259 variantsMismatch := false
3260 if len(variants) != len(expectedVariants) {
3261 variantsMismatch = true
3262 } else {
3263 for _, v := range expectedVariants {
3264 if !inList(v, variants) {
3265 variantsMismatch = false
3266 }
3267 }
3268 }
3269 if variantsMismatch {
3270 t.Errorf("variants of libFoo expected:\n")
3271 for _, v := range expectedVariants {
3272 t.Errorf("%q\n", v)
3273 }
3274 t.Errorf(", but got:\n")
3275 for _, v := range variants {
3276 t.Errorf("%q\n", v)
3277 }
3278 }
3279
Colin Cross7113d202019-11-20 16:39:12 -08003280 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003281 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003282 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003283 if !strings.Contains(libFlags, libFoo1StubPath) {
3284 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3285 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003286
Colin Cross7113d202019-11-20 16:39:12 -08003287 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003288 cFlags := libBarCompileRule.Args["cFlags"]
3289 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3290 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3291 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3292 }
Jiyong Park37b25202018-07-11 10:49:27 +09003293}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003294
Jooyung Hanb04a4992020-03-13 18:57:35 +09003295func TestVersioningMacro(t *testing.T) {
3296 for _, tc := range []struct{ moduleName, expected string }{
3297 {"libc", "__LIBC_API__"},
3298 {"libfoo", "__LIBFOO_API__"},
3299 {"libfoo@1", "__LIBFOO_1_API__"},
3300 {"libfoo-v1", "__LIBFOO_V1_API__"},
3301 {"libfoo.v1", "__LIBFOO_V1_API__"},
3302 } {
3303 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3304 }
3305}
3306
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003307func TestStaticExecutable(t *testing.T) {
3308 ctx := testCc(t, `
3309 cc_binary {
3310 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003311 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003312 static_executable: true,
3313 }`)
3314
Colin Cross7113d202019-11-20 16:39:12 -08003315 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003316 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3317 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003318 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003319 for _, lib := range systemStaticLibs {
3320 if !strings.Contains(libFlags, lib) {
3321 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3322 }
3323 }
3324 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3325 for _, lib := range systemSharedLibs {
3326 if strings.Contains(libFlags, lib) {
3327 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3328 }
3329 }
3330}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003331
3332func TestStaticDepsOrderWithStubs(t *testing.T) {
3333 ctx := testCc(t, `
3334 cc_binary {
3335 name: "mybin",
3336 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003337 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003338 static_executable: true,
3339 stl: "none",
3340 }
3341
3342 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003343 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003344 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003345 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003346 stl: "none",
3347 }
3348
3349 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003350 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003351 srcs: ["foo.c"],
3352 stl: "none",
3353 stubs: {
3354 versions: ["1"],
3355 },
3356 }`)
3357
Colin Cross0de8a1e2020-09-18 14:15:30 -07003358 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3359 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003360 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003361
3362 if !reflect.DeepEqual(actual, expected) {
3363 t.Errorf("staticDeps orderings were not propagated correctly"+
3364 "\nactual: %v"+
3365 "\nexpected: %v",
3366 actual,
3367 expected,
3368 )
3369 }
3370}
Jooyung Han38002912019-05-16 04:01:54 +09003371
Jooyung Hand48f3c32019-08-23 11:18:57 +09003372func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3373 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3374 cc_library {
3375 name: "libA",
3376 srcs: ["foo.c"],
3377 shared_libs: ["libB"],
3378 stl: "none",
3379 }
3380
3381 cc_library {
3382 name: "libB",
3383 srcs: ["foo.c"],
3384 enabled: false,
3385 stl: "none",
3386 }
3387 `)
3388}
3389
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003390// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3391// correctly.
3392func TestFuzzTarget(t *testing.T) {
3393 ctx := testCc(t, `
3394 cc_fuzz {
3395 name: "fuzz_smoke_test",
3396 srcs: ["foo.c"],
3397 }`)
3398
Paul Duffin075c4172019-12-19 19:06:13 +00003399 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003400 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3401}
3402
Jiyong Park29074592019-07-07 16:27:47 +09003403func TestAidl(t *testing.T) {
3404}
3405
Jooyung Han38002912019-05-16 04:01:54 +09003406func assertString(t *testing.T, got, expected string) {
3407 t.Helper()
3408 if got != expected {
3409 t.Errorf("expected %q got %q", expected, got)
3410 }
3411}
3412
3413func assertArrayString(t *testing.T, got, expected []string) {
3414 t.Helper()
3415 if len(got) != len(expected) {
3416 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3417 return
3418 }
3419 for i := range got {
3420 if got[i] != expected[i] {
3421 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3422 i, expected[i], expected, got[i], got)
3423 return
3424 }
3425 }
3426}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003427
Jooyung Han0302a842019-10-30 18:43:49 +09003428func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3429 t.Helper()
3430 assertArrayString(t, android.SortedStringKeys(m), expected)
3431}
3432
Colin Crosse1bb5d02019-09-24 14:55:04 -07003433func TestDefaults(t *testing.T) {
3434 ctx := testCc(t, `
3435 cc_defaults {
3436 name: "defaults",
3437 srcs: ["foo.c"],
3438 static: {
3439 srcs: ["bar.c"],
3440 },
3441 shared: {
3442 srcs: ["baz.c"],
3443 },
Liz Kammer3cf52112021-03-31 15:42:03 -04003444 bazel_module: {
3445 bp2build_available: true,
3446 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07003447 }
3448
3449 cc_library_static {
3450 name: "libstatic",
3451 defaults: ["defaults"],
3452 }
3453
3454 cc_library_shared {
3455 name: "libshared",
3456 defaults: ["defaults"],
3457 }
3458
3459 cc_library {
3460 name: "libboth",
3461 defaults: ["defaults"],
3462 }
3463
3464 cc_binary {
3465 name: "binary",
3466 defaults: ["defaults"],
3467 }`)
3468
3469 pathsToBase := func(paths android.Paths) []string {
3470 var ret []string
3471 for _, p := range paths {
3472 ret = append(ret, p.Base())
3473 }
3474 return ret
3475 }
3476
Colin Cross7113d202019-11-20 16:39:12 -08003477 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003478 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3479 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3480 }
Colin Cross7113d202019-11-20 16:39:12 -08003481 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003482 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3483 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3484 }
Colin Cross7113d202019-11-20 16:39:12 -08003485 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003486 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3487 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3488 }
3489
Colin Cross7113d202019-11-20 16:39:12 -08003490 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003491 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3492 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3493 }
Colin Cross7113d202019-11-20 16:39:12 -08003494 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003495 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3496 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3497 }
3498}
Colin Crosseabaedd2020-02-06 17:01:55 -08003499
3500func TestProductVariableDefaults(t *testing.T) {
3501 bp := `
3502 cc_defaults {
3503 name: "libfoo_defaults",
3504 srcs: ["foo.c"],
3505 cppflags: ["-DFOO"],
3506 product_variables: {
3507 debuggable: {
3508 cppflags: ["-DBAR"],
3509 },
3510 },
3511 }
3512
3513 cc_library {
3514 name: "libfoo",
3515 defaults: ["libfoo_defaults"],
3516 }
3517 `
3518
Paul Duffin8567f222021-03-23 00:02:06 +00003519 result := android.GroupFixturePreparers(
3520 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003521 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003522
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003523 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3524 variables.Debuggable = BoolPtr(true)
3525 }),
3526 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003527
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003528 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003529 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003530}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003531
3532func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3533 t.Parallel()
3534 bp := `
3535 cc_library_static {
3536 name: "libfoo",
3537 srcs: ["foo.c"],
3538 whole_static_libs: ["libbar"],
3539 }
3540
3541 cc_library_static {
3542 name: "libbar",
3543 whole_static_libs: ["libmissing"],
3544 }
3545 `
3546
Paul Duffin8567f222021-03-23 00:02:06 +00003547 result := android.GroupFixturePreparers(
3548 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003549 android.PrepareForTestWithAllowMissingDependencies,
3550 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003551
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003552 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003553 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003554
Paul Duffine84b1332021-03-12 11:59:43 +00003555 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003556
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003557 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003558 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003559}
Colin Crosse9fe2942020-11-10 18:12:15 -08003560
3561func TestInstallSharedLibs(t *testing.T) {
3562 bp := `
3563 cc_binary {
3564 name: "bin",
3565 host_supported: true,
3566 shared_libs: ["libshared"],
3567 runtime_libs: ["libruntime"],
3568 srcs: [":gen"],
3569 }
3570
3571 cc_library_shared {
3572 name: "libshared",
3573 host_supported: true,
3574 shared_libs: ["libtransitive"],
3575 }
3576
3577 cc_library_shared {
3578 name: "libtransitive",
3579 host_supported: true,
3580 }
3581
3582 cc_library_shared {
3583 name: "libruntime",
3584 host_supported: true,
3585 }
3586
3587 cc_binary_host {
3588 name: "tool",
3589 srcs: ["foo.cpp"],
3590 }
3591
3592 genrule {
3593 name: "gen",
3594 tools: ["tool"],
3595 out: ["gen.cpp"],
3596 cmd: "$(location tool) $(out)",
3597 }
3598 `
3599
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003600 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003601 ctx := testCcWithConfig(t, config)
3602
3603 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3604 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3605 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3606 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3607 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3608
3609 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3610 t.Errorf("expected host bin dependency %q, got %q", w, g)
3611 }
3612
3613 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3614 t.Errorf("expected host bin dependency %q, got %q", w, g)
3615 }
3616
3617 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3618 t.Errorf("expected host bin dependency %q, got %q", w, g)
3619 }
3620
3621 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3622 t.Errorf("expected host bin dependency %q, got %q", w, g)
3623 }
3624
3625 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3626 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3627 }
3628
3629 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3630 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3631 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3632 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3633
3634 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3635 t.Errorf("expected device bin dependency %q, got %q", w, g)
3636 }
3637
3638 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3639 t.Errorf("expected device bin dependency %q, got %q", w, g)
3640 }
3641
3642 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3643 t.Errorf("expected device bin dependency %q, got %q", w, g)
3644 }
3645
3646 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3647 t.Errorf("expected device bin dependency %q, got %q", w, g)
3648 }
3649
3650 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3651 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3652 }
3653
3654}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003655
3656func TestStubsLibReexportsHeaders(t *testing.T) {
3657 ctx := testCc(t, `
3658 cc_library_shared {
3659 name: "libclient",
3660 srcs: ["foo.c"],
3661 shared_libs: ["libfoo#1"],
3662 }
3663
3664 cc_library_shared {
3665 name: "libfoo",
3666 srcs: ["foo.c"],
3667 shared_libs: ["libbar"],
3668 export_shared_lib_headers: ["libbar"],
3669 stubs: {
3670 symbol_file: "foo.map.txt",
3671 versions: ["1", "2", "3"],
3672 },
3673 }
3674
3675 cc_library_shared {
3676 name: "libbar",
3677 export_include_dirs: ["include/libbar"],
3678 srcs: ["foo.c"],
3679 }`)
3680
3681 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3682
3683 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3684 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3685 }
3686}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003687
3688func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3689 ctx := testCc(t, `
3690 cc_library {
3691 name: "libfoo",
3692 srcs: ["a/Foo.aidl"],
3693 aidl: { flags: ["-Werror"], },
3694 }
3695 `)
3696
3697 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3698 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3699 aidlCommand := manifest.Commands[0].GetCommand()
3700 expectedAidlFlag := "-Werror"
3701 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3702 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3703 }
3704}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003705
Jiyong Parka008fb02021-03-16 17:15:53 +09003706func TestMinSdkVersionInClangTriple(t *testing.T) {
3707 ctx := testCc(t, `
3708 cc_library_shared {
3709 name: "libfoo",
3710 srcs: ["foo.c"],
3711 min_sdk_version: "29",
3712 }`)
3713
3714 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3715 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3716}
3717
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003718type MemtagNoteType int
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003719
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003720const (
3721 None MemtagNoteType = iota + 1
3722 Sync
3723 Async
3724)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003725
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003726func (t MemtagNoteType) str() string {
3727 switch t {
3728 case None:
3729 return "none"
3730 case Sync:
3731 return "sync"
3732 case Async:
3733 return "async"
3734 default:
3735 panic("invalid note type")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003736 }
3737}
3738
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003739func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
3740 note_async := "note_memtag_heap_async"
3741 note_sync := "note_memtag_heap_sync"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003742
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003743 found := None
3744 implicits := m.Rule("ld").Implicits
3745 for _, lib := range implicits {
3746 if strings.Contains(lib.Rel(), note_async) {
3747 found = Async
3748 break
3749 } else if strings.Contains(lib.Rel(), note_sync) {
3750 found = Sync
3751 break
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003752 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003753 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003754
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003755 if found != expected {
3756 t.Errorf("Wrong Memtag note in target %q: found %q, expected %q", m.Module().(*Module).Name(), found.str(), expected.str())
3757 }
3758}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003759
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003760var prepareForTestWithMemtagHeap = android.GroupFixturePreparers(
3761 android.FixtureModifyMockFS(func(fs android.MockFS) {
3762 templateBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003763 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003764 name: "%[1]s_test",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003765 gtest: false,
3766 }
3767
3768 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003769 name: "%[1]s_test_false",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003770 gtest: false,
3771 sanitize: { memtag_heap: false },
3772 }
3773
3774 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003775 name: "%[1]s_test_true",
3776 gtest: false,
3777 sanitize: { memtag_heap: true },
3778 }
3779
3780 cc_test {
3781 name: "%[1]s_test_true_nodiag",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003782 gtest: false,
3783 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3784 }
3785
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003786 cc_test {
3787 name: "%[1]s_test_true_diag",
3788 gtest: false,
3789 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
3790 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003791
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003792 cc_binary {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003793 name: "%[1]s_binary",
3794 }
3795
3796 cc_binary {
3797 name: "%[1]s_binary_false",
3798 sanitize: { memtag_heap: false },
3799 }
3800
3801 cc_binary {
3802 name: "%[1]s_binary_true",
3803 sanitize: { memtag_heap: true },
3804 }
3805
3806 cc_binary {
3807 name: "%[1]s_binary_true_nodiag",
3808 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3809 }
3810
3811 cc_binary {
3812 name: "%[1]s_binary_true_diag",
3813 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003814 }
3815 `
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003816 subdirDefaultBp := fmt.Sprintf(templateBp, "default")
3817 subdirExcludeBp := fmt.Sprintf(templateBp, "exclude")
3818 subdirSyncBp := fmt.Sprintf(templateBp, "sync")
3819 subdirAsyncBp := fmt.Sprintf(templateBp, "async")
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003820
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003821 fs.Merge(android.MockFS{
3822 "subdir_default/Android.bp": []byte(subdirDefaultBp),
3823 "subdir_exclude/Android.bp": []byte(subdirExcludeBp),
3824 "subdir_sync/Android.bp": []byte(subdirSyncBp),
3825 "subdir_async/Android.bp": []byte(subdirAsyncBp),
3826 })
3827 }),
3828 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3829 variables.MemtagHeapExcludePaths = []string{"subdir_exclude"}
Evgenii Stepanov779b64e2021-04-09 14:33:10 -07003830 // "subdir_exclude" is covered by both include and exclude paths. Exclude wins.
3831 variables.MemtagHeapSyncIncludePaths = []string{"subdir_sync", "subdir_exclude"}
3832 variables.MemtagHeapAsyncIncludePaths = []string{"subdir_async", "subdir_exclude"}
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003833 }),
3834)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003835
3836func TestSanitizeMemtagHeap(t *testing.T) {
3837 variant := "android_arm64_armv8-a"
3838
Paul Duffin8567f222021-03-23 00:02:06 +00003839 result := android.GroupFixturePreparers(
3840 prepareForCcTest,
3841 prepareForTestWithMemtagHeap,
3842 ).RunTest(t)
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003843 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003844
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003845 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3846 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3847 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3848 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3849 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3850
3851 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), None)
3852 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3853 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3854 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3855 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3856
3857 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3858 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3859 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3860 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3861 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3862
3863 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3864 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3865 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3866 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3867 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3868
3869 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3870 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3871 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3872 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3873 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3874
3875 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3876 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3877 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3878 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3879 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3880
3881 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3882 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3883 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3884 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3885 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3886
3887 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3888 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3889 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3890 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3891 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3892}
3893
3894func TestSanitizeMemtagHeapWithSanitizeDevice(t *testing.T) {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003895 variant := "android_arm64_armv8-a"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003896
Paul Duffin8567f222021-03-23 00:02:06 +00003897 result := android.GroupFixturePreparers(
3898 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003899 prepareForTestWithMemtagHeap,
3900 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3901 variables.SanitizeDevice = []string{"memtag_heap"}
3902 }),
3903 ).RunTest(t)
3904 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003905
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003906 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3907 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3908 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3909 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3910 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003911
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003912 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Async)
3913 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3914 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3915 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3916 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3917
3918 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3919 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3920 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3921 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3922 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3923
3924 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3925 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3926 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3927 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3928 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3929
3930 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3931 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3932 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3933 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3934 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3935
3936 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3937 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3938 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3939 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3940 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3941
3942 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3943 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3944 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3945 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3946 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3947
3948 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3949 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3950 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3951 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3952 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3953}
3954
3955func TestSanitizeMemtagHeapWithSanitizeDeviceDiag(t *testing.T) {
3956 variant := "android_arm64_armv8-a"
3957
Paul Duffin8567f222021-03-23 00:02:06 +00003958 result := android.GroupFixturePreparers(
3959 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003960 prepareForTestWithMemtagHeap,
3961 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3962 variables.SanitizeDevice = []string{"memtag_heap"}
3963 variables.SanitizeDeviceDiag = []string{"memtag_heap"}
3964 }),
3965 ).RunTest(t)
3966 ctx := result.TestContext
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003967
3968 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3969 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3970 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Sync)
3971 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3972 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3973
3974 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Sync)
3975 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3976 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Sync)
3977 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3978 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3979
3980 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3981 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3982 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Sync)
3983 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3984 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3985
3986 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3987 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3988 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Sync)
3989 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3990 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3991
3992 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3993 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3994 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Sync)
3995 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3996 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3997
3998 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Sync)
3999 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
4000 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Sync)
4001 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
4002 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
4003
4004 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
4005 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
4006 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
4007 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
4008 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
4009
4010 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
4011 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
4012 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
4013 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
4014 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004015}
Paul Duffin3cb603e2021-02-19 13:57:10 +00004016
4017func TestIncludeDirsExporting(t *testing.T) {
4018
4019 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4020 // embedded newline characters alone.
4021 trimIndentingSpaces := func(s string) string {
4022 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4023 }
4024
4025 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4026 t.Helper()
4027 expected = trimIndentingSpaces(expected)
4028 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4029 if expected != actual {
4030 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4031 }
4032 }
4033
4034 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4035
4036 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4037 t.Helper()
4038 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4039 name := module.Name()
4040
4041 for _, checker := range checkers {
4042 checker(t, name, exported)
4043 }
4044 }
4045
4046 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4047 return func(t *testing.T, name string, exported FlagExporterInfo) {
4048 t.Helper()
4049 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4050 }
4051 }
4052
4053 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4054 return func(t *testing.T, name string, exported FlagExporterInfo) {
4055 t.Helper()
4056 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4057 }
4058 }
4059
4060 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4061 return func(t *testing.T, name string, exported FlagExporterInfo) {
4062 t.Helper()
4063 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4064 }
4065 }
4066
4067 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4068 return func(t *testing.T, name string, exported FlagExporterInfo) {
4069 t.Helper()
4070 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4071 }
4072 }
4073
4074 genRuleModules := `
4075 genrule {
4076 name: "genrule_foo",
4077 cmd: "generate-foo",
4078 out: [
4079 "generated_headers/foo/generated_header.h",
4080 ],
4081 export_include_dirs: [
4082 "generated_headers",
4083 ],
4084 }
4085
4086 genrule {
4087 name: "genrule_bar",
4088 cmd: "generate-bar",
4089 out: [
4090 "generated_headers/bar/generated_header.h",
4091 ],
4092 export_include_dirs: [
4093 "generated_headers",
4094 ],
4095 }
4096 `
4097
4098 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4099 ctx := testCc(t, genRuleModules+`
4100 cc_library {
4101 name: "libfoo",
4102 srcs: ["foo.c"],
4103 export_include_dirs: ["foo/standard"],
4104 export_system_include_dirs: ["foo/system"],
4105 generated_headers: ["genrule_foo"],
4106 export_generated_headers: ["genrule_foo"],
4107 }
4108
4109 cc_library {
4110 name: "libbar",
4111 srcs: ["bar.c"],
4112 shared_libs: ["libfoo"],
4113 export_include_dirs: ["bar/standard"],
4114 export_system_include_dirs: ["bar/system"],
4115 generated_headers: ["genrule_bar"],
4116 export_generated_headers: ["genrule_bar"],
4117 }
4118 `)
4119 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4120 checkIncludeDirs(t, ctx, foo,
4121 expectedIncludeDirs(`
4122 foo/standard
4123 .intermediates/genrule_foo/gen/generated_headers
4124 `),
4125 expectedSystemIncludeDirs(`foo/system`),
4126 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4127 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4128 )
4129
4130 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4131 checkIncludeDirs(t, ctx, bar,
4132 expectedIncludeDirs(`
4133 bar/standard
4134 .intermediates/genrule_bar/gen/generated_headers
4135 `),
4136 expectedSystemIncludeDirs(`bar/system`),
4137 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4138 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4139 )
4140 })
4141
4142 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4143 ctx := testCc(t, genRuleModules+`
4144 cc_library {
4145 name: "libfoo",
4146 srcs: ["foo.c"],
4147 export_include_dirs: ["foo/standard"],
4148 export_system_include_dirs: ["foo/system"],
4149 generated_headers: ["genrule_foo"],
4150 export_generated_headers: ["genrule_foo"],
4151 }
4152
4153 cc_library {
4154 name: "libbar",
4155 srcs: ["bar.c"],
4156 whole_static_libs: ["libfoo"],
4157 export_include_dirs: ["bar/standard"],
4158 export_system_include_dirs: ["bar/system"],
4159 generated_headers: ["genrule_bar"],
4160 export_generated_headers: ["genrule_bar"],
4161 }
4162 `)
4163 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4164 checkIncludeDirs(t, ctx, foo,
4165 expectedIncludeDirs(`
4166 foo/standard
4167 .intermediates/genrule_foo/gen/generated_headers
4168 `),
4169 expectedSystemIncludeDirs(`foo/system`),
4170 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4171 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4172 )
4173
4174 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4175 checkIncludeDirs(t, ctx, bar,
4176 expectedIncludeDirs(`
4177 bar/standard
4178 foo/standard
4179 .intermediates/genrule_foo/gen/generated_headers
4180 .intermediates/genrule_bar/gen/generated_headers
4181 `),
4182 expectedSystemIncludeDirs(`
4183 bar/system
4184 foo/system
4185 `),
4186 expectedGeneratedHeaders(`
4187 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4188 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4189 `),
4190 expectedOrderOnlyDeps(`
4191 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4192 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4193 `),
4194 )
4195 })
4196
Paul Duffin3cb603e2021-02-19 13:57:10 +00004197 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4198 ctx := testCc(t, genRuleModules+`
4199 cc_library_shared {
4200 name: "libfoo",
4201 srcs: [
4202 "foo.c",
4203 "b.aidl",
4204 "a.proto",
4205 ],
4206 aidl: {
4207 export_aidl_headers: true,
4208 }
4209 }
4210 `)
4211 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4212 checkIncludeDirs(t, ctx, foo,
4213 expectedIncludeDirs(`
4214 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4215 `),
4216 expectedSystemIncludeDirs(``),
4217 expectedGeneratedHeaders(`
4218 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4219 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4220 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004221 `),
4222 expectedOrderOnlyDeps(`
4223 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4224 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4225 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004226 `),
4227 )
4228 })
4229
Paul Duffin3cb603e2021-02-19 13:57:10 +00004230 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4231 ctx := testCc(t, genRuleModules+`
4232 cc_library_shared {
4233 name: "libfoo",
4234 srcs: [
4235 "foo.c",
4236 "b.aidl",
4237 "a.proto",
4238 ],
4239 proto: {
4240 export_proto_headers: true,
4241 }
4242 }
4243 `)
4244 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4245 checkIncludeDirs(t, ctx, foo,
4246 expectedIncludeDirs(`
4247 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4248 `),
4249 expectedSystemIncludeDirs(``),
4250 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004251 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4252 `),
4253 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004254 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4255 `),
4256 )
4257 })
4258
Paul Duffin33056e82021-02-19 13:49:08 +00004259 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004260 ctx := testCc(t, genRuleModules+`
4261 cc_library_shared {
4262 name: "libfoo",
4263 srcs: [
4264 "foo.c",
4265 "a.sysprop",
4266 "b.aidl",
4267 "a.proto",
4268 ],
4269 }
4270 `)
4271 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4272 checkIncludeDirs(t, ctx, foo,
4273 expectedIncludeDirs(`
4274 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4275 `),
4276 expectedSystemIncludeDirs(``),
4277 expectedGeneratedHeaders(`
4278 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004279 `),
4280 expectedOrderOnlyDeps(`
4281 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
4282 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004283 `),
4284 )
4285 })
4286}