blob: 49fffc9b27f09035e1dc26f69983886c357f89d1 [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",
551 llndk_stubs: "libllndk.llndk",
552 }
553
554 llndk_library {
555 name: "libllndk.llndk",
556 symbol_file: "",
557 export_llndk_headers: ["libllndk_headers"],
558 }
559
Colin Cross627280f2021-04-26 16:53:58 -0700560 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900561 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700562 llndk: {
563 llndk_headers: true,
564 },
Justin Yun450ae722021-04-16 19:58:18 +0900565 export_include_dirs: ["include"],
566 }
567
Colin Crosse4e44bc2020-12-28 13:50:21 -0800568 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900569 name: "llndk.libraries.txt",
570 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800571 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900572 name: "vndkcore.libraries.txt",
573 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800574 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900575 name: "vndksp.libraries.txt",
576 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800577 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900578 name: "vndkprivate.libraries.txt",
579 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800580 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900581 name: "vndkproduct.libraries.txt",
582 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800583 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900584 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800585 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900586 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800587 `
588
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000589 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800590 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900591 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900592 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800593
594 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800595
Jooyung Han261e1582020-10-20 18:54:21 +0900596 // subdir == "" because VNDK libs are not supposed to be installed separately.
597 // They are installed as part of VNDK APEX instead.
598 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
599 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900600 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900601 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
602 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900603 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900604
Justin Yun6977e8a2020-10-29 18:24:11 +0900605 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
606 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900607
Inseob Kim1f086e22019-05-09 13:29:15 +0900608 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900609 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000610 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900611
612 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
613 "arm64", "armv8-a"))
614 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
615 "arm", "armv7-a-neon"))
616
617 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
618 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900619 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
620
Inseob Kim1f086e22019-05-09 13:29:15 +0900621 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
622 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900623 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900624
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900625 variant := "android_vendor.29_arm64_armv8-a_shared"
626 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900627
Inseob Kim7f283f42020-06-01 21:53:49 +0900628 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
629
630 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
631 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900632 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
633 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900634 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
635 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Justin Yun450ae722021-04-16 19:58:18 +0900636 checkSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
637 checkSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900638
Jooyung Han39edb6c2019-11-06 16:53:07 +0900639 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900640 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
641 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
642 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
643 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900644 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900645
Jooyung Han097087b2019-10-22 19:32:18 +0900646 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
647 "LLNDK: libc.so",
648 "LLNDK: libdl.so",
649 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900650 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900651 "LLNDK: libm.so",
652 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900653 "VNDK-SP: libvndk_sp-x.so",
654 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900655 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900656 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900657 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900658 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900659 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900660 "VNDK-private: libvndk-private.so",
661 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900662 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900663 "VNDK-product: libc++.so",
664 "VNDK-product: libvndk_product.so",
665 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900666 })
Justin Yun450ae722021-04-16 19:58:18 +0900667 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900668 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
669 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
670 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 +0900671 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 +0900672 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
673}
674
Yo Chiangbba545e2020-06-09 16:15:37 +0800675func TestVndkWithHostSupported(t *testing.T) {
676 ctx := testCc(t, `
677 cc_library {
678 name: "libvndk_host_supported",
679 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900680 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800681 vndk: {
682 enabled: true,
683 },
684 host_supported: true,
685 }
686
687 cc_library {
688 name: "libvndk_host_supported_but_disabled_on_device",
689 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900690 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800691 vndk: {
692 enabled: true,
693 },
694 host_supported: true,
695 enabled: false,
696 target: {
697 host: {
698 enabled: true,
699 }
700 }
701 }
702
Colin Crosse4e44bc2020-12-28 13:50:21 -0800703 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800704 name: "vndkcore.libraries.txt",
705 }
706 `)
707
708 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
709}
710
Jooyung Han2216fb12019-11-06 16:46:15 +0900711func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800712 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800713 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900714 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800715 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800716 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000717 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800718 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900719 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800720 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900721
722 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Colin Crossaa255532020-07-03 13:18:24 -0700723 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900724 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900725}
726
727func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800728 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900729 cc_library {
730 name: "libvndk",
731 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900732 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900733 vndk: {
734 enabled: true,
735 },
736 nocrt: true,
737 }
738
739 cc_library {
740 name: "libvndk_sp",
741 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900742 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900743 vndk: {
744 enabled: true,
745 support_system_process: true,
746 },
747 nocrt: true,
748 }
749
750 cc_library {
751 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900752 vendor_available: true,
753 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900754 vndk: {
755 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900756 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900757 },
758 nocrt: true,
759 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900760
Colin Crosse4e44bc2020-12-28 13:50:21 -0800761 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900762 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800763 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900764 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800765 `
766
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000767 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800768 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900769 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800770 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
771
772 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
773
774 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900775
Jooyung Han2216fb12019-11-06 16:46:15 +0900776 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900777}
778
Chris Parsons79d66a52020-06-05 17:26:16 -0400779func TestDataLibs(t *testing.T) {
780 bp := `
781 cc_test_library {
782 name: "test_lib",
783 srcs: ["test_lib.cpp"],
784 gtest: false,
785 }
786
787 cc_test {
788 name: "main_test",
789 data_libs: ["test_lib"],
790 gtest: false,
791 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400792 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400793
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000794 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400795 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900796 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400797 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
798
799 ctx := testCcWithConfig(t, config)
800 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
801 testBinary := module.(*Module).linker.(*testBinary)
802 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
803 if err != nil {
804 t.Errorf("Expected cc_test to produce output files, error: %s", err)
805 return
806 }
807 if len(outputFiles) != 1 {
808 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
809 return
810 }
811 if len(testBinary.dataPaths()) != 1 {
812 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
813 return
814 }
815
816 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400817 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400818
819 if !strings.HasSuffix(outputPath, "/main_test") {
820 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
821 return
822 }
823 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
824 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
825 return
826 }
827}
828
Chris Parsons216e10a2020-07-09 17:12:52 -0400829func TestDataLibsRelativeInstallPath(t *testing.T) {
830 bp := `
831 cc_test_library {
832 name: "test_lib",
833 srcs: ["test_lib.cpp"],
834 relative_install_path: "foo/bar/baz",
835 gtest: false,
836 }
837
838 cc_test {
839 name: "main_test",
840 data_libs: ["test_lib"],
841 gtest: false,
842 }
843 `
844
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000845 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400846 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900847 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400848 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
849
850 ctx := testCcWithConfig(t, config)
851 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
852 testBinary := module.(*Module).linker.(*testBinary)
853 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
854 if err != nil {
855 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
856 }
857 if len(outputFiles) != 1 {
858 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
859 }
860 if len(testBinary.dataPaths()) != 1 {
861 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
862 }
863
864 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400865
866 if !strings.HasSuffix(outputPath, "/main_test") {
867 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
868 }
Colin Crossaa255532020-07-03 13:18:24 -0700869 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400870 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
871 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400872 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400873 }
874}
875
Jooyung Han0302a842019-10-30 18:43:49 +0900876func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900877 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900878 cc_library {
879 name: "libvndk",
880 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900881 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900882 vndk: {
883 enabled: true,
884 },
885 nocrt: true,
886 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900887 cc_library {
888 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900889 vendor_available: true,
890 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900891 vndk: {
892 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900893 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900894 },
895 nocrt: true,
896 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800897
898 cc_library {
899 name: "libllndk",
900 llndk_stubs: "libllndk.llndk",
901 }
902
903 llndk_library {
904 name: "libllndk.llndk",
905 symbol_file: "",
906 export_llndk_headers: ["libllndk_headers"],
907 }
908
Colin Cross627280f2021-04-26 16:53:58 -0700909 cc_library_headers {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800910 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700911 llndk: {
912 symbol_file: "libllndk.map.txt",
913 },
Colin Crossb5f6fa62021-01-06 17:05:04 -0800914 export_include_dirs: ["include"],
915 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900916 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900917
918 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
919 "LLNDK: libc.so",
920 "LLNDK: libdl.so",
921 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800922 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900923 "LLNDK: libm.so",
924 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900925 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900926 "VNDK-core: libvndk.so",
927 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900928 "VNDK-private: libvndk-private.so",
929 "VNDK-product: libc++.so",
930 "VNDK-product: libvndk-private.so",
931 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900932 })
Logan Chienf3511742017-10-31 18:04:35 +0800933}
934
Justin Yun63e9ec72020-10-29 16:49:43 +0900935func TestVndkModuleError(t *testing.T) {
936 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900937 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900938 cc_library {
939 name: "libvndk",
940 vndk: {
941 enabled: true,
942 },
943 nocrt: true,
944 }
945 `)
946
Justin Yunc0d8c492021-01-07 17:45:31 +0900947 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900948 cc_library {
949 name: "libvndk",
950 product_available: true,
951 vndk: {
952 enabled: true,
953 },
954 nocrt: true,
955 }
956 `)
957
Justin Yun6977e8a2020-10-29 18:24:11 +0900958 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
959 cc_library {
960 name: "libvndkprop",
961 vendor_available: true,
962 product_available: true,
963 vndk: {
964 enabled: true,
965 },
966 nocrt: true,
967 target: {
968 vendor: {
969 cflags: ["-DTEST",],
970 },
971 },
972 }
973 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900974}
975
Logan Chiend3c59a22018-03-29 14:08:15 +0800976func TestVndkDepError(t *testing.T) {
977 // Check whether an error is emitted when a VNDK lib depends on a system lib.
978 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
979 cc_library {
980 name: "libvndk",
981 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900982 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800983 vndk: {
984 enabled: true,
985 },
986 shared_libs: ["libfwk"], // Cause error
987 nocrt: true,
988 }
989
990 cc_library {
991 name: "libfwk",
992 nocrt: true,
993 }
994 `)
995
996 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
997 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
998 cc_library {
999 name: "libvndk",
1000 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001001 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001002 vndk: {
1003 enabled: true,
1004 },
1005 shared_libs: ["libvendor"], // Cause error
1006 nocrt: true,
1007 }
1008
1009 cc_library {
1010 name: "libvendor",
1011 vendor: true,
1012 nocrt: true,
1013 }
1014 `)
1015
1016 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
1017 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1018 cc_library {
1019 name: "libvndk_sp",
1020 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001021 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001022 vndk: {
1023 enabled: true,
1024 support_system_process: true,
1025 },
1026 shared_libs: ["libfwk"], // Cause error
1027 nocrt: true,
1028 }
1029
1030 cc_library {
1031 name: "libfwk",
1032 nocrt: true,
1033 }
1034 `)
1035
1036 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
1037 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1038 cc_library {
1039 name: "libvndk_sp",
1040 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001041 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001042 vndk: {
1043 enabled: true,
1044 support_system_process: true,
1045 },
1046 shared_libs: ["libvendor"], // Cause error
1047 nocrt: true,
1048 }
1049
1050 cc_library {
1051 name: "libvendor",
1052 vendor: true,
1053 nocrt: true,
1054 }
1055 `)
1056
1057 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
1058 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1059 cc_library {
1060 name: "libvndk_sp",
1061 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001062 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001063 vndk: {
1064 enabled: true,
1065 support_system_process: true,
1066 },
1067 shared_libs: ["libvndk"], // Cause error
1068 nocrt: true,
1069 }
1070
1071 cc_library {
1072 name: "libvndk",
1073 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001074 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001075 vndk: {
1076 enabled: true,
1077 },
1078 nocrt: true,
1079 }
1080 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001081
1082 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1083 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1084 cc_library {
1085 name: "libvndk",
1086 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001087 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001088 vndk: {
1089 enabled: true,
1090 },
1091 shared_libs: ["libnonvndk"],
1092 nocrt: true,
1093 }
1094
1095 cc_library {
1096 name: "libnonvndk",
1097 vendor_available: true,
1098 nocrt: true,
1099 }
1100 `)
1101
1102 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1103 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1104 cc_library {
1105 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001106 vendor_available: true,
1107 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001108 vndk: {
1109 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001110 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001111 },
1112 shared_libs: ["libnonvndk"],
1113 nocrt: true,
1114 }
1115
1116 cc_library {
1117 name: "libnonvndk",
1118 vendor_available: true,
1119 nocrt: true,
1120 }
1121 `)
1122
1123 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1124 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1125 cc_library {
1126 name: "libvndksp",
1127 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001128 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001129 vndk: {
1130 enabled: true,
1131 support_system_process: true,
1132 },
1133 shared_libs: ["libnonvndk"],
1134 nocrt: true,
1135 }
1136
1137 cc_library {
1138 name: "libnonvndk",
1139 vendor_available: true,
1140 nocrt: true,
1141 }
1142 `)
1143
1144 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1145 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1146 cc_library {
1147 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001148 vendor_available: true,
1149 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001150 vndk: {
1151 enabled: true,
1152 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001153 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001154 },
1155 shared_libs: ["libnonvndk"],
1156 nocrt: true,
1157 }
1158
1159 cc_library {
1160 name: "libnonvndk",
1161 vendor_available: true,
1162 nocrt: true,
1163 }
1164 `)
1165}
1166
1167func TestDoubleLoadbleDep(t *testing.T) {
1168 // okay to link : LLNDK -> double_loadable VNDK
1169 testCc(t, `
1170 cc_library {
1171 name: "libllndk",
1172 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001173 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001174 }
1175
1176 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001177 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001178 symbol_file: "",
1179 }
1180
1181 cc_library {
1182 name: "libdoubleloadable",
1183 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001184 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001185 vndk: {
1186 enabled: true,
1187 },
1188 double_loadable: true,
1189 }
1190 `)
1191 // okay to link : LLNDK -> VNDK-SP
1192 testCc(t, `
1193 cc_library {
1194 name: "libllndk",
1195 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -07001196 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001197 }
1198
1199 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001200 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001201 symbol_file: "",
1202 }
1203
1204 cc_library {
1205 name: "libvndksp",
1206 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001207 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001208 vndk: {
1209 enabled: true,
1210 support_system_process: true,
1211 },
1212 }
1213 `)
1214 // okay to link : double_loadable -> double_loadable
1215 testCc(t, `
1216 cc_library {
1217 name: "libdoubleloadable1",
1218 shared_libs: ["libdoubleloadable2"],
1219 vendor_available: true,
1220 double_loadable: true,
1221 }
1222
1223 cc_library {
1224 name: "libdoubleloadable2",
1225 vendor_available: true,
1226 double_loadable: true,
1227 }
1228 `)
1229 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1230 testCc(t, `
1231 cc_library {
1232 name: "libdoubleloadable",
1233 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001234 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001235 vndk: {
1236 enabled: true,
1237 },
1238 double_loadable: true,
1239 shared_libs: ["libnondoubleloadable"],
1240 }
1241
1242 cc_library {
1243 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001244 vendor_available: true,
1245 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001246 vndk: {
1247 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001248 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001249 },
1250 double_loadable: true,
1251 }
1252 `)
1253 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1254 testCc(t, `
1255 cc_library {
1256 name: "libllndk",
1257 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001258 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001259 }
1260
1261 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001262 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001263 symbol_file: "",
1264 }
1265
1266 cc_library {
1267 name: "libcoreonly",
1268 shared_libs: ["libvendoravailable"],
1269 }
1270
1271 // indirect dependency of LLNDK
1272 cc_library {
1273 name: "libvendoravailable",
1274 vendor_available: true,
1275 double_loadable: true,
1276 }
1277 `)
1278}
1279
1280func TestDoubleLoadableDepError(t *testing.T) {
1281 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1282 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1283 cc_library {
1284 name: "libllndk",
1285 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001286 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001287 }
1288
1289 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001290 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001291 symbol_file: "",
1292 }
1293
1294 cc_library {
1295 name: "libnondoubleloadable",
1296 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001297 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001298 vndk: {
1299 enabled: true,
1300 },
1301 }
1302 `)
1303
1304 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1305 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1306 cc_library {
1307 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001308 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001309 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001310 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001311 }
1312
1313 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001314 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001315 symbol_file: "",
1316 }
1317
1318 cc_library {
1319 name: "libnondoubleloadable",
1320 vendor_available: true,
1321 }
1322 `)
1323
Jooyung Hana70f0672019-01-18 15:20:43 +09001324 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1325 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1326 cc_library {
1327 name: "libllndk",
1328 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001329 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001330 }
1331
1332 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001333 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001334 symbol_file: "",
1335 }
1336
1337 cc_library {
1338 name: "libcoreonly",
1339 shared_libs: ["libvendoravailable"],
1340 }
1341
1342 // indirect dependency of LLNDK
1343 cc_library {
1344 name: "libvendoravailable",
1345 vendor_available: true,
1346 }
1347 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001348
1349 // The error is not from 'client' but from 'libllndk'
1350 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1351 cc_library {
1352 name: "client",
1353 vendor_available: true,
1354 double_loadable: true,
1355 shared_libs: ["libllndk"],
1356 }
1357 cc_library {
1358 name: "libllndk",
1359 shared_libs: ["libnondoubleloadable"],
1360 llndk_stubs: "libllndk.llndk",
1361 }
1362 llndk_library {
1363 name: "libllndk.llndk",
1364 symbol_file: "",
1365 }
1366 cc_library {
1367 name: "libnondoubleloadable",
1368 vendor_available: true,
1369 }
1370 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001371}
1372
Jooyung Han479ca172020-10-19 18:51:07 +09001373func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1374 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1375 cc_library {
1376 name: "libvndksp",
1377 shared_libs: ["libanothervndksp"],
1378 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001379 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001380 vndk: {
1381 enabled: true,
1382 support_system_process: true,
1383 }
1384 }
1385
1386 cc_library {
1387 name: "libllndk",
1388 shared_libs: ["libanothervndksp"],
1389 }
1390
1391 llndk_library {
1392 name: "libllndk",
1393 symbol_file: "",
1394 }
1395
1396 cc_library {
1397 name: "libanothervndksp",
1398 vendor_available: true,
1399 }
1400 `)
1401}
1402
Logan Chienf3511742017-10-31 18:04:35 +08001403func TestVndkExt(t *testing.T) {
1404 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001405 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001406 cc_library {
1407 name: "libvndk",
1408 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001409 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001410 vndk: {
1411 enabled: true,
1412 },
1413 nocrt: true,
1414 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001415 cc_library {
1416 name: "libvndk2",
1417 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001418 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001419 vndk: {
1420 enabled: true,
1421 },
1422 target: {
1423 vendor: {
1424 suffix: "-suffix",
1425 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001426 product: {
1427 suffix: "-suffix",
1428 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001429 },
1430 nocrt: true,
1431 }
Logan Chienf3511742017-10-31 18:04:35 +08001432
1433 cc_library {
1434 name: "libvndk_ext",
1435 vendor: true,
1436 vndk: {
1437 enabled: true,
1438 extends: "libvndk",
1439 },
1440 nocrt: true,
1441 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001442
1443 cc_library {
1444 name: "libvndk2_ext",
1445 vendor: true,
1446 vndk: {
1447 enabled: true,
1448 extends: "libvndk2",
1449 },
1450 nocrt: true,
1451 }
Logan Chienf3511742017-10-31 18:04:35 +08001452
Justin Yun0ecf0b22020-02-28 15:07:59 +09001453 cc_library {
1454 name: "libvndk_ext_product",
1455 product_specific: true,
1456 vndk: {
1457 enabled: true,
1458 extends: "libvndk",
1459 },
1460 nocrt: true,
1461 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001462
Justin Yun0ecf0b22020-02-28 15:07:59 +09001463 cc_library {
1464 name: "libvndk2_ext_product",
1465 product_specific: true,
1466 vndk: {
1467 enabled: true,
1468 extends: "libvndk2",
1469 },
1470 nocrt: true,
1471 }
1472 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001473 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001474 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1475 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001476 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001477
1478 ctx := testCcWithConfig(t, config)
1479
1480 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1481 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1482
1483 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1484 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1485
1486 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1487 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001488}
1489
Logan Chiend3c59a22018-03-29 14:08:15 +08001490func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001491 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1492 ctx := testCcNoVndk(t, `
1493 cc_library {
1494 name: "libvndk",
1495 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001496 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001497 vndk: {
1498 enabled: true,
1499 },
1500 nocrt: true,
1501 }
1502
1503 cc_library {
1504 name: "libvndk_ext",
1505 vendor: 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" can be found.
1515 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1516 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1517 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1518 }
1519}
1520
Justin Yun0ecf0b22020-02-28 15:07:59 +09001521func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1522 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001523 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001524 cc_library {
1525 name: "libvndk",
1526 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001527 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001528 vndk: {
1529 enabled: true,
1530 },
1531 nocrt: true,
1532 }
1533
1534 cc_library {
1535 name: "libvndk_ext_product",
1536 product_specific: true,
1537 vndk: {
1538 enabled: true,
1539 extends: "libvndk",
1540 },
1541 nocrt: true,
1542 }
1543 `)
1544
1545 // Ensures that the core variant of "libvndk_ext_product" can be found.
1546 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1547 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1548 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1549 }
1550}
1551
Logan Chienf3511742017-10-31 18:04:35 +08001552func TestVndkExtError(t *testing.T) {
1553 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001554 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001555 cc_library {
1556 name: "libvndk",
1557 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001558 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001559 vndk: {
1560 enabled: true,
1561 },
1562 nocrt: true,
1563 }
1564
1565 cc_library {
1566 name: "libvndk_ext",
1567 vndk: {
1568 enabled: true,
1569 extends: "libvndk",
1570 },
1571 nocrt: true,
1572 }
1573 `)
1574
1575 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1576 cc_library {
1577 name: "libvndk",
1578 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001579 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001580 vndk: {
1581 enabled: true,
1582 },
1583 nocrt: true,
1584 }
1585
1586 cc_library {
1587 name: "libvndk_ext",
1588 vendor: true,
1589 vndk: {
1590 enabled: true,
1591 },
1592 nocrt: true,
1593 }
1594 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001595
1596 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1597 cc_library {
1598 name: "libvndk",
1599 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001600 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001601 vndk: {
1602 enabled: true,
1603 },
1604 nocrt: true,
1605 }
1606
1607 cc_library {
1608 name: "libvndk_ext_product",
1609 product_specific: true,
1610 vndk: {
1611 enabled: true,
1612 },
1613 nocrt: true,
1614 }
1615 `)
1616
1617 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1618 cc_library {
1619 name: "libvndk",
1620 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001621 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001622 vndk: {
1623 enabled: true,
1624 },
1625 nocrt: true,
1626 }
1627
1628 cc_library {
1629 name: "libvndk_ext_product",
1630 product_specific: true,
1631 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001632 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001633 vndk: {
1634 enabled: true,
1635 extends: "libvndk",
1636 },
1637 nocrt: true,
1638 }
1639 `)
Logan Chienf3511742017-10-31 18:04:35 +08001640}
1641
1642func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1643 // This test ensures an error is emitted for inconsistent support_system_process.
1644 testCcError(t, "module \".*\" with mismatched support_system_process", `
1645 cc_library {
1646 name: "libvndk",
1647 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001648 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001649 vndk: {
1650 enabled: true,
1651 },
1652 nocrt: true,
1653 }
1654
1655 cc_library {
1656 name: "libvndk_sp_ext",
1657 vendor: true,
1658 vndk: {
1659 enabled: true,
1660 extends: "libvndk",
1661 support_system_process: true,
1662 },
1663 nocrt: true,
1664 }
1665 `)
1666
1667 testCcError(t, "module \".*\" with mismatched support_system_process", `
1668 cc_library {
1669 name: "libvndk_sp",
1670 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001671 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001672 vndk: {
1673 enabled: true,
1674 support_system_process: true,
1675 },
1676 nocrt: true,
1677 }
1678
1679 cc_library {
1680 name: "libvndk_ext",
1681 vendor: true,
1682 vndk: {
1683 enabled: true,
1684 extends: "libvndk_sp",
1685 },
1686 nocrt: true,
1687 }
1688 `)
1689}
1690
1691func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001692 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001693 // with `private: true`.
1694 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001695 cc_library {
1696 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001697 vendor_available: true,
1698 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001699 vndk: {
1700 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001701 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001702 },
1703 nocrt: true,
1704 }
1705
1706 cc_library {
1707 name: "libvndk_ext",
1708 vendor: true,
1709 vndk: {
1710 enabled: true,
1711 extends: "libvndk",
1712 },
1713 nocrt: true,
1714 }
1715 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001716
Justin Yunfd9e8042020-12-23 18:23:14 +09001717 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001718 cc_library {
1719 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001720 vendor_available: true,
1721 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001722 vndk: {
1723 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001724 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001725 },
1726 nocrt: true,
1727 }
1728
1729 cc_library {
1730 name: "libvndk_ext_product",
1731 product_specific: true,
1732 vndk: {
1733 enabled: true,
1734 extends: "libvndk",
1735 },
1736 nocrt: true,
1737 }
1738 `)
Logan Chienf3511742017-10-31 18:04:35 +08001739}
1740
Logan Chiend3c59a22018-03-29 14:08:15 +08001741func TestVendorModuleUseVndkExt(t *testing.T) {
1742 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001743 testCc(t, `
1744 cc_library {
1745 name: "libvndk",
1746 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001747 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001748 vndk: {
1749 enabled: true,
1750 },
1751 nocrt: true,
1752 }
1753
1754 cc_library {
1755 name: "libvndk_ext",
1756 vendor: true,
1757 vndk: {
1758 enabled: true,
1759 extends: "libvndk",
1760 },
1761 nocrt: true,
1762 }
1763
1764 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001765 name: "libvndk_sp",
1766 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001767 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001768 vndk: {
1769 enabled: true,
1770 support_system_process: true,
1771 },
1772 nocrt: true,
1773 }
1774
1775 cc_library {
1776 name: "libvndk_sp_ext",
1777 vendor: true,
1778 vndk: {
1779 enabled: true,
1780 extends: "libvndk_sp",
1781 support_system_process: true,
1782 },
1783 nocrt: true,
1784 }
1785
1786 cc_library {
1787 name: "libvendor",
1788 vendor: true,
1789 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1790 nocrt: true,
1791 }
1792 `)
1793}
1794
Logan Chiend3c59a22018-03-29 14:08:15 +08001795func TestVndkExtUseVendorLib(t *testing.T) {
1796 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001797 testCc(t, `
1798 cc_library {
1799 name: "libvndk",
1800 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001801 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001802 vndk: {
1803 enabled: true,
1804 },
1805 nocrt: true,
1806 }
1807
1808 cc_library {
1809 name: "libvndk_ext",
1810 vendor: true,
1811 vndk: {
1812 enabled: true,
1813 extends: "libvndk",
1814 },
1815 shared_libs: ["libvendor"],
1816 nocrt: true,
1817 }
1818
1819 cc_library {
1820 name: "libvendor",
1821 vendor: true,
1822 nocrt: true,
1823 }
1824 `)
Logan Chienf3511742017-10-31 18:04:35 +08001825
Logan Chiend3c59a22018-03-29 14:08:15 +08001826 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1827 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001828 cc_library {
1829 name: "libvndk_sp",
1830 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001831 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001832 vndk: {
1833 enabled: true,
1834 support_system_process: true,
1835 },
1836 nocrt: true,
1837 }
1838
1839 cc_library {
1840 name: "libvndk_sp_ext",
1841 vendor: true,
1842 vndk: {
1843 enabled: true,
1844 extends: "libvndk_sp",
1845 support_system_process: true,
1846 },
1847 shared_libs: ["libvendor"], // Cause an error
1848 nocrt: true,
1849 }
1850
1851 cc_library {
1852 name: "libvendor",
1853 vendor: true,
1854 nocrt: true,
1855 }
1856 `)
1857}
1858
Justin Yun0ecf0b22020-02-28 15:07:59 +09001859func TestProductVndkExtDependency(t *testing.T) {
1860 bp := `
1861 cc_library {
1862 name: "libvndk",
1863 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001864 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001865 vndk: {
1866 enabled: true,
1867 },
1868 nocrt: true,
1869 }
1870
1871 cc_library {
1872 name: "libvndk_ext_product",
1873 product_specific: true,
1874 vndk: {
1875 enabled: true,
1876 extends: "libvndk",
1877 },
1878 shared_libs: ["libproduct_for_vndklibs"],
1879 nocrt: true,
1880 }
1881
1882 cc_library {
1883 name: "libvndk_sp",
1884 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001885 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001886 vndk: {
1887 enabled: true,
1888 support_system_process: true,
1889 },
1890 nocrt: true,
1891 }
1892
1893 cc_library {
1894 name: "libvndk_sp_ext_product",
1895 product_specific: true,
1896 vndk: {
1897 enabled: true,
1898 extends: "libvndk_sp",
1899 support_system_process: true,
1900 },
1901 shared_libs: ["libproduct_for_vndklibs"],
1902 nocrt: true,
1903 }
1904
1905 cc_library {
1906 name: "libproduct",
1907 product_specific: true,
1908 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1909 nocrt: true,
1910 }
1911
1912 cc_library {
1913 name: "libproduct_for_vndklibs",
1914 product_specific: true,
1915 nocrt: true,
1916 }
1917 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001918 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001919 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1920 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001921 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001922
1923 testCcWithConfig(t, config)
1924}
1925
Logan Chiend3c59a22018-03-29 14:08:15 +08001926func TestVndkSpExtUseVndkError(t *testing.T) {
1927 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1928 // library.
1929 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1930 cc_library {
1931 name: "libvndk",
1932 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001933 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001934 vndk: {
1935 enabled: true,
1936 },
1937 nocrt: true,
1938 }
1939
1940 cc_library {
1941 name: "libvndk_sp",
1942 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001943 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001944 vndk: {
1945 enabled: true,
1946 support_system_process: true,
1947 },
1948 nocrt: true,
1949 }
1950
1951 cc_library {
1952 name: "libvndk_sp_ext",
1953 vendor: true,
1954 vndk: {
1955 enabled: true,
1956 extends: "libvndk_sp",
1957 support_system_process: true,
1958 },
1959 shared_libs: ["libvndk"], // Cause an error
1960 nocrt: true,
1961 }
1962 `)
1963
1964 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1965 // library.
1966 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1967 cc_library {
1968 name: "libvndk",
1969 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001970 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001971 vndk: {
1972 enabled: true,
1973 },
1974 nocrt: true,
1975 }
1976
1977 cc_library {
1978 name: "libvndk_ext",
1979 vendor: true,
1980 vndk: {
1981 enabled: true,
1982 extends: "libvndk",
1983 },
1984 nocrt: true,
1985 }
1986
1987 cc_library {
1988 name: "libvndk_sp",
1989 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001990 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001991 vndk: {
1992 enabled: true,
1993 support_system_process: true,
1994 },
1995 nocrt: true,
1996 }
1997
1998 cc_library {
1999 name: "libvndk_sp_ext",
2000 vendor: true,
2001 vndk: {
2002 enabled: true,
2003 extends: "libvndk_sp",
2004 support_system_process: true,
2005 },
2006 shared_libs: ["libvndk_ext"], // Cause an error
2007 nocrt: true,
2008 }
2009 `)
2010}
2011
2012func TestVndkUseVndkExtError(t *testing.T) {
2013 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2014 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002015 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2016 cc_library {
2017 name: "libvndk",
2018 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002019 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002020 vndk: {
2021 enabled: true,
2022 },
2023 nocrt: true,
2024 }
2025
2026 cc_library {
2027 name: "libvndk_ext",
2028 vendor: true,
2029 vndk: {
2030 enabled: true,
2031 extends: "libvndk",
2032 },
2033 nocrt: true,
2034 }
2035
2036 cc_library {
2037 name: "libvndk2",
2038 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002039 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002040 vndk: {
2041 enabled: true,
2042 },
2043 shared_libs: ["libvndk_ext"],
2044 nocrt: true,
2045 }
2046 `)
2047
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002048 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002049 cc_library {
2050 name: "libvndk",
2051 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002052 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002053 vndk: {
2054 enabled: true,
2055 },
2056 nocrt: true,
2057 }
2058
2059 cc_library {
2060 name: "libvndk_ext",
2061 vendor: true,
2062 vndk: {
2063 enabled: true,
2064 extends: "libvndk",
2065 },
2066 nocrt: true,
2067 }
2068
2069 cc_library {
2070 name: "libvndk2",
2071 vendor_available: true,
2072 vndk: {
2073 enabled: true,
2074 },
2075 target: {
2076 vendor: {
2077 shared_libs: ["libvndk_ext"],
2078 },
2079 },
2080 nocrt: true,
2081 }
2082 `)
2083
2084 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2085 cc_library {
2086 name: "libvndk_sp",
2087 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002088 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002089 vndk: {
2090 enabled: true,
2091 support_system_process: true,
2092 },
2093 nocrt: true,
2094 }
2095
2096 cc_library {
2097 name: "libvndk_sp_ext",
2098 vendor: true,
2099 vndk: {
2100 enabled: true,
2101 extends: "libvndk_sp",
2102 support_system_process: true,
2103 },
2104 nocrt: true,
2105 }
2106
2107 cc_library {
2108 name: "libvndk_sp_2",
2109 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002110 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002111 vndk: {
2112 enabled: true,
2113 support_system_process: true,
2114 },
2115 shared_libs: ["libvndk_sp_ext"],
2116 nocrt: true,
2117 }
2118 `)
2119
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002120 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002121 cc_library {
2122 name: "libvndk_sp",
2123 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002124 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002125 vndk: {
2126 enabled: true,
2127 },
2128 nocrt: true,
2129 }
2130
2131 cc_library {
2132 name: "libvndk_sp_ext",
2133 vendor: true,
2134 vndk: {
2135 enabled: true,
2136 extends: "libvndk_sp",
2137 },
2138 nocrt: true,
2139 }
2140
2141 cc_library {
2142 name: "libvndk_sp2",
2143 vendor_available: true,
2144 vndk: {
2145 enabled: true,
2146 },
2147 target: {
2148 vendor: {
2149 shared_libs: ["libvndk_sp_ext"],
2150 },
2151 },
2152 nocrt: true,
2153 }
2154 `)
2155}
2156
Justin Yun5f7f7e82019-11-18 19:52:14 +09002157func TestEnforceProductVndkVersion(t *testing.T) {
2158 bp := `
2159 cc_library {
2160 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002161 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002162 }
2163 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002164 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002165 symbol_file: "",
2166 }
2167 cc_library {
2168 name: "libvndk",
2169 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002170 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002171 vndk: {
2172 enabled: true,
2173 },
2174 nocrt: true,
2175 }
2176 cc_library {
2177 name: "libvndk_sp",
2178 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002179 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002180 vndk: {
2181 enabled: true,
2182 support_system_process: true,
2183 },
2184 nocrt: true,
2185 }
2186 cc_library {
2187 name: "libva",
2188 vendor_available: true,
2189 nocrt: true,
2190 }
2191 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002192 name: "libpa",
2193 product_available: true,
2194 nocrt: true,
2195 }
2196 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002197 name: "libboth_available",
2198 vendor_available: true,
2199 product_available: true,
2200 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002201 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002202 target: {
2203 vendor: {
2204 suffix: "-vendor",
2205 },
2206 product: {
2207 suffix: "-product",
2208 },
2209 }
2210 }
2211 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002212 name: "libproduct_va",
2213 product_specific: true,
2214 vendor_available: true,
2215 nocrt: true,
2216 }
2217 cc_library {
2218 name: "libprod",
2219 product_specific: true,
2220 shared_libs: [
2221 "libllndk",
2222 "libvndk",
2223 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002224 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002225 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002226 "libproduct_va",
2227 ],
2228 nocrt: true,
2229 }
2230 cc_library {
2231 name: "libvendor",
2232 vendor: true,
2233 shared_libs: [
2234 "libllndk",
2235 "libvndk",
2236 "libvndk_sp",
2237 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002238 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002239 "libproduct_va",
2240 ],
2241 nocrt: true,
2242 }
2243 `
2244
Paul Duffin8567f222021-03-23 00:02:06 +00002245 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002246
Jooyung Han261e1582020-10-20 18:54:21 +09002247 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2248 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002249
2250 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2251 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2252
2253 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2254 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002255
2256 ensureStringContains := func(t *testing.T, str string, substr string) {
2257 t.Helper()
2258 if !strings.Contains(str, substr) {
2259 t.Errorf("%q is not found in %v", substr, str)
2260 }
2261 }
2262 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2263 t.Helper()
2264 if strings.Contains(str, substr) {
2265 t.Errorf("%q is found in %v", substr, str)
2266 }
2267 }
2268
2269 // _static variant is used since _shared reuses *.o from the static variant
2270 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2271 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2272
2273 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2274 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2275 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2276 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2277
2278 product_cflags := product_static.Rule("cc").Args["cFlags"]
2279 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2280 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2281 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002282}
2283
2284func TestEnforceProductVndkVersionErrors(t *testing.T) {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002285 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002286 cc_library {
2287 name: "libprod",
2288 product_specific: true,
2289 shared_libs: [
2290 "libvendor",
2291 ],
2292 nocrt: true,
2293 }
2294 cc_library {
2295 name: "libvendor",
2296 vendor: true,
2297 nocrt: true,
2298 }
2299 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002300 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002301 cc_library {
2302 name: "libprod",
2303 product_specific: true,
2304 shared_libs: [
2305 "libsystem",
2306 ],
2307 nocrt: true,
2308 }
2309 cc_library {
2310 name: "libsystem",
2311 nocrt: true,
2312 }
2313 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002314 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002315 cc_library {
2316 name: "libprod",
2317 product_specific: true,
2318 shared_libs: [
2319 "libva",
2320 ],
2321 nocrt: true,
2322 }
2323 cc_library {
2324 name: "libva",
2325 vendor_available: true,
2326 nocrt: true,
2327 }
2328 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002329 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002330 cc_library {
2331 name: "libprod",
2332 product_specific: true,
2333 shared_libs: [
2334 "libvndk_private",
2335 ],
2336 nocrt: true,
2337 }
2338 cc_library {
2339 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002340 vendor_available: true,
2341 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002342 vndk: {
2343 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002344 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002345 },
2346 nocrt: true,
2347 }
2348 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002349 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002350 cc_library {
2351 name: "libprod",
2352 product_specific: true,
2353 shared_libs: [
2354 "libsystem_ext",
2355 ],
2356 nocrt: true,
2357 }
2358 cc_library {
2359 name: "libsystem_ext",
2360 system_ext_specific: true,
2361 nocrt: true,
2362 }
2363 `)
2364 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2365 cc_library {
2366 name: "libsystem",
2367 shared_libs: [
2368 "libproduct_va",
2369 ],
2370 nocrt: true,
2371 }
2372 cc_library {
2373 name: "libproduct_va",
2374 product_specific: true,
2375 vendor_available: true,
2376 nocrt: true,
2377 }
2378 `)
2379}
2380
Jooyung Han38002912019-05-16 04:01:54 +09002381func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002382 bp := `
2383 cc_library {
2384 name: "libvndk",
2385 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002386 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002387 vndk: {
2388 enabled: true,
2389 },
2390 }
2391 cc_library {
2392 name: "libvndksp",
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 support_system_process: true,
2398 },
2399 }
2400 cc_library {
2401 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002402 vendor_available: true,
2403 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002404 vndk: {
2405 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002406 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002407 },
2408 }
2409 cc_library {
2410 name: "libvendor",
2411 vendor: true,
2412 }
2413 cc_library {
2414 name: "libvndkext",
2415 vendor: true,
2416 vndk: {
2417 enabled: true,
2418 extends: "libvndk",
2419 },
2420 }
2421 vndk_prebuilt_shared {
2422 name: "prevndk",
2423 version: "27",
2424 target_arch: "arm",
2425 binder32bit: true,
2426 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002427 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002428 vndk: {
2429 enabled: true,
2430 },
2431 arch: {
2432 arm: {
2433 srcs: ["liba.so"],
2434 },
2435 },
2436 }
2437 cc_library {
2438 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002439 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002440 }
2441 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002442 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002443 symbol_file: "",
2444 }
2445 cc_library {
2446 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07002447 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002448 }
2449 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002450 name: "libllndkprivate.llndk",
Justin Yunc0d8c492021-01-07 17:45:31 +09002451 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002452 symbol_file: "",
Colin Cross78212242021-01-06 14:51:30 -08002453 }
2454
2455 llndk_libraries_txt {
2456 name: "llndk.libraries.txt",
2457 }
2458 vndkcore_libraries_txt {
2459 name: "vndkcore.libraries.txt",
2460 }
2461 vndksp_libraries_txt {
2462 name: "vndksp.libraries.txt",
2463 }
2464 vndkprivate_libraries_txt {
2465 name: "vndkprivate.libraries.txt",
2466 }
2467 vndkcorevariant_libraries_txt {
2468 name: "vndkcorevariant.libraries.txt",
2469 insert_vndk_version: false,
2470 }
2471 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002472
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002473 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002474 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002475 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002476 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002477 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002478
Colin Cross78212242021-01-06 14:51:30 -08002479 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2480 []string{"libvndk.so", "libvndkprivate.so"})
2481 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2482 []string{"libc++.so", "libvndksp.so"})
2483 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2484 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2485 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2486 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002487
Colin Crossfb0c16e2019-11-20 17:12:35 -08002488 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002489
Jooyung Han38002912019-05-16 04:01:54 +09002490 tests := []struct {
2491 variant string
2492 name string
2493 expected string
2494 }{
2495 {vendorVariant, "libvndk", "native:vndk"},
2496 {vendorVariant, "libvndksp", "native:vndk"},
2497 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2498 {vendorVariant, "libvendor", "native:vendor"},
2499 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002500 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002501 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002502 {coreVariant, "libvndk", "native:platform"},
2503 {coreVariant, "libvndkprivate", "native:platform"},
2504 {coreVariant, "libllndk", "native:platform"},
2505 }
2506 for _, test := range tests {
2507 t.Run(test.name, func(t *testing.T) {
2508 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2509 assertString(t, module.makeLinkType, test.expected)
2510 })
2511 }
2512}
2513
Jeff Gaston294356f2017-09-27 17:05:30 -07002514var staticLinkDepOrderTestCases = []struct {
2515 // This is a string representation of a map[moduleName][]moduleDependency .
2516 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002517 inStatic string
2518
2519 // This is a string representation of a map[moduleName][]moduleDependency .
2520 // It models the dependencies declared in an Android.bp file.
2521 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002522
2523 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2524 // The keys of allOrdered specify which modules we would like to check.
2525 // The values of allOrdered specify the expected result (of the transitive closure of all
2526 // dependencies) for each module to test
2527 allOrdered string
2528
2529 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2530 // The keys of outOrdered specify which modules we would like to check.
2531 // The values of outOrdered specify the expected result (of the ordered linker command line)
2532 // for each module to test.
2533 outOrdered string
2534}{
2535 // Simple tests
2536 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002537 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002538 outOrdered: "",
2539 },
2540 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002541 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002542 outOrdered: "a:",
2543 },
2544 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002545 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002546 outOrdered: "a:b; b:",
2547 },
2548 // Tests of reordering
2549 {
2550 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002551 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002552 outOrdered: "a:b,c,d; b:d; c:d; d:",
2553 },
2554 {
2555 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002556 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002557 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2558 },
2559 {
2560 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002561 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002562 outOrdered: "a:d,b,e,c; d:b; e:c",
2563 },
2564 {
2565 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002566 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002567 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2568 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2569 },
2570 {
2571 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002572 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 -07002573 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2574 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2575 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002576 // shared dependencies
2577 {
2578 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2579 // So, we don't actually have to check that a shared dependency of c will change the order
2580 // of a library that depends statically on b and on c. We only need to check that if c has
2581 // a shared dependency on b, that that shows up in allOrdered.
2582 inShared: "c:b",
2583 allOrdered: "c:b",
2584 outOrdered: "c:",
2585 },
2586 {
2587 // This test doesn't actually include any shared dependencies but it's a reminder of what
2588 // the second phase of the above test would look like
2589 inStatic: "a:b,c; c:b",
2590 allOrdered: "a:c,b; c:b",
2591 outOrdered: "a:c,b; c:b",
2592 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002593 // tiebreakers for when two modules specifying different orderings and there is no dependency
2594 // to dictate an order
2595 {
2596 // 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 -08002597 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002598 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2599 },
2600 {
2601 // 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 -08002602 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 -07002603 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2604 },
2605 // Tests involving duplicate dependencies
2606 {
2607 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002608 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002609 outOrdered: "a:c,b",
2610 },
2611 {
2612 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002613 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002614 outOrdered: "a:d,c,b",
2615 },
2616 // Tests to confirm the nonexistence of infinite loops.
2617 // These cases should never happen, so as long as the test terminates and the
2618 // result is deterministic then that should be fine.
2619 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002620 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002621 outOrdered: "a:a",
2622 },
2623 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002624 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002625 allOrdered: "a:b,c; b:c,a; c:a,b",
2626 outOrdered: "a:b; b:c; c:a",
2627 },
2628 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002629 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002630 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2631 outOrdered: "a:c,b; b:a,c; c:b,a",
2632 },
2633}
2634
2635// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2636func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2637 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2638 strippedText := strings.Replace(text, " ", "", -1)
2639 if len(strippedText) < 1 {
2640 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2641 }
2642 allDeps = make(map[android.Path][]android.Path, 0)
2643
2644 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2645 moduleTexts := strings.Split(strippedText, ";")
2646
2647 outputForModuleName := func(moduleName string) android.Path {
2648 return android.PathForTesting(moduleName)
2649 }
2650
2651 for _, moduleText := range moduleTexts {
2652 // convert from "a:b,c" to ["a", "b,c"]
2653 components := strings.Split(moduleText, ":")
2654 if len(components) != 2 {
2655 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2656 }
2657 moduleName := components[0]
2658 moduleOutput := outputForModuleName(moduleName)
2659 modulesInOrder = append(modulesInOrder, moduleOutput)
2660
2661 depString := components[1]
2662 // convert from "b,c" to ["b", "c"]
2663 depNames := strings.Split(depString, ",")
2664 if len(depString) < 1 {
2665 depNames = []string{}
2666 }
2667 var deps []android.Path
2668 for _, depName := range depNames {
2669 deps = append(deps, outputForModuleName(depName))
2670 }
2671 allDeps[moduleOutput] = deps
2672 }
2673 return modulesInOrder, allDeps
2674}
2675
Jeff Gaston294356f2017-09-27 17:05:30 -07002676func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2677 for _, moduleName := range moduleNames {
2678 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002679 output := module.outputFile.Path().RelativeToTop()
Jeff Gaston294356f2017-09-27 17:05:30 -07002680 paths = append(paths, output)
2681 }
2682 return paths
2683}
2684
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002685func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002686 ctx := testCc(t, `
2687 cc_library {
2688 name: "a",
2689 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002690 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002691 }
2692 cc_library {
2693 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002694 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002695 }
2696 cc_library {
2697 name: "c",
2698 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002699 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002700 }
2701 cc_library {
2702 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002703 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002704 }
2705
2706 `)
2707
Colin Cross7113d202019-11-20 16:39:12 -08002708 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002709 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002710 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2711 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002712 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002713
2714 if !reflect.DeepEqual(actual, expected) {
2715 t.Errorf("staticDeps orderings were not propagated correctly"+
2716 "\nactual: %v"+
2717 "\nexpected: %v",
2718 actual,
2719 expected,
2720 )
2721 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002722}
Jeff Gaston294356f2017-09-27 17:05:30 -07002723
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002724func TestStaticLibDepReorderingWithShared(t *testing.T) {
2725 ctx := testCc(t, `
2726 cc_library {
2727 name: "a",
2728 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002729 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002730 }
2731 cc_library {
2732 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002733 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002734 }
2735 cc_library {
2736 name: "c",
2737 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002738 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002739 }
2740
2741 `)
2742
Colin Cross7113d202019-11-20 16:39:12 -08002743 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002744 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002745 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2746 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002747 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002748
2749 if !reflect.DeepEqual(actual, expected) {
2750 t.Errorf("staticDeps orderings did not account for shared libs"+
2751 "\nactual: %v"+
2752 "\nexpected: %v",
2753 actual,
2754 expected,
2755 )
2756 }
2757}
2758
Jooyung Hanb04a4992020-03-13 18:57:35 +09002759func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002760 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002761 if !reflect.DeepEqual(actual, expected) {
2762 t.Errorf(message+
2763 "\nactual: %v"+
2764 "\nexpected: %v",
2765 actual,
2766 expected,
2767 )
2768 }
2769}
2770
Jooyung Han61b66e92020-03-21 14:21:46 +00002771func TestLlndkLibrary(t *testing.T) {
2772 ctx := testCc(t, `
2773 cc_library {
2774 name: "libllndk",
2775 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07002776 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002777 }
2778 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002779 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002780 }
Colin Cross127bb8b2020-12-16 16:46:01 -08002781
2782 cc_prebuilt_library_shared {
2783 name: "libllndkprebuilt",
2784 stubs: { versions: ["1", "2"] },
2785 llndk_stubs: "libllndkprebuilt.llndk",
2786 }
2787 llndk_library {
2788 name: "libllndkprebuilt.llndk",
2789 }
2790
2791 cc_library {
2792 name: "libllndk_with_external_headers",
2793 stubs: { versions: ["1", "2"] },
2794 llndk_stubs: "libllndk_with_external_headers.llndk",
2795 header_libs: ["libexternal_headers"],
2796 export_header_lib_headers: ["libexternal_headers"],
2797 }
2798 llndk_library {
2799 name: "libllndk_with_external_headers.llndk",
2800 }
2801 cc_library_headers {
2802 name: "libexternal_headers",
2803 export_include_dirs: ["include"],
2804 vendor_available: true,
2805 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002806 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08002807 actual := ctx.ModuleVariantsForTests("libllndk")
2808 for i := 0; i < len(actual); i++ {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002809 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
Colin Cross127bb8b2020-12-16 16:46:01 -08002810 actual = append(actual[:i], actual[i+1:]...)
2811 i--
2812 }
2813 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002814 expected := []string{
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002815 "android_vendor.29_arm64_armv8-a_shared_1",
2816 "android_vendor.29_arm64_armv8-a_shared_2",
2817 "android_vendor.29_arm64_armv8-a_shared_current",
2818 "android_vendor.29_arm64_armv8-a_shared",
2819 "android_vendor.29_arm_armv7-a-neon_shared_1",
2820 "android_vendor.29_arm_armv7-a-neon_shared_2",
2821 "android_vendor.29_arm_armv7-a-neon_shared_current",
2822 "android_vendor.29_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002823 }
2824 checkEquals(t, "variants for llndk stubs", expected, actual)
2825
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002826 params := ctx.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002827 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2828
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002829 params = ctx.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002830 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2831}
2832
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002833func TestEmbeddedLlndkLibrary(t *testing.T) {
2834 result := prepareForCcTest.RunTestWithBp(t, `
2835 cc_library {
2836 name: "libllndk",
2837 stubs: { versions: ["1", "2"] },
2838 llndk: {
2839 symbol_file: "libllndk.map.txt",
2840 },
2841 export_include_dirs: ["include"],
2842 }
2843
2844 cc_prebuilt_library_shared {
2845 name: "libllndkprebuilt",
2846 stubs: { versions: ["1", "2"] },
2847 llndk: {
2848 symbol_file: "libllndkprebuilt.map.txt",
2849 },
2850 }
2851
2852 cc_library {
2853 name: "libllndk_with_external_headers",
2854 stubs: { versions: ["1", "2"] },
2855 llndk: {
2856 symbol_file: "libllndk.map.txt",
2857 export_llndk_headers: ["libexternal_llndk_headers"],
2858 },
2859 header_libs: ["libexternal_headers"],
2860 export_header_lib_headers: ["libexternal_headers"],
2861 }
2862 cc_library_headers {
2863 name: "libexternal_headers",
2864 export_include_dirs: ["include"],
2865 vendor_available: true,
2866 }
2867 cc_library_headers {
2868 name: "libexternal_llndk_headers",
2869 export_include_dirs: ["include_llndk"],
2870 llndk: {
2871 symbol_file: "libllndk.map.txt",
2872 },
2873 vendor_available: true,
2874 }
2875
2876 cc_library {
2877 name: "libllndk_with_override_headers",
2878 stubs: { versions: ["1", "2"] },
2879 llndk: {
2880 symbol_file: "libllndk.map.txt",
2881 override_export_include_dirs: ["include_llndk"],
2882 },
2883 export_include_dirs: ["include"],
2884 }
2885 `)
2886 actual := result.ModuleVariantsForTests("libllndk")
2887 for i := 0; i < len(actual); i++ {
2888 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2889 actual = append(actual[:i], actual[i+1:]...)
2890 i--
2891 }
2892 }
2893 expected := []string{
2894 "android_vendor.29_arm64_armv8-a_shared_1",
2895 "android_vendor.29_arm64_armv8-a_shared_2",
2896 "android_vendor.29_arm64_armv8-a_shared_current",
2897 "android_vendor.29_arm64_armv8-a_shared",
2898 "android_vendor.29_arm_armv7-a-neon_shared_1",
2899 "android_vendor.29_arm_armv7-a-neon_shared_2",
2900 "android_vendor.29_arm_armv7-a-neon_shared_current",
2901 "android_vendor.29_arm_armv7-a-neon_shared",
2902 }
2903 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2904
2905 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2906 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2907
2908 params = result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared_1").Description("generate stub")
2909 android.AssertSame(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2910
2911 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2912 t.Helper()
2913 m := result.ModuleForTests(module, variant).Module()
2914 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2915 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2916 expectedDirs, f.IncludeDirs)
2917 }
2918
2919 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2920 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2921 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2922 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2923 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2924 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2925}
2926
Jiyong Parka46a4d52017-12-14 19:54:34 +09002927func TestLlndkHeaders(t *testing.T) {
2928 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002929 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002930 name: "libllndk_headers",
2931 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002932 llndk: {
2933 llndk_headers: true,
2934 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002935 }
2936 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002937 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002938 llndk: {
2939 symbol_file: "libllndk.map.txt",
2940 export_llndk_headers: ["libllndk_headers"],
2941 }
Colin Cross0477b422020-10-13 18:43:54 -07002942 }
2943
2944 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002945 name: "libvendor",
2946 shared_libs: ["libllndk"],
2947 vendor: true,
2948 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002949 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002950 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002951 }
2952 `)
2953
2954 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002955 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002956 cflags := cc.Args["cFlags"]
2957 if !strings.Contains(cflags, "-Imy_include") {
2958 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2959 }
2960}
2961
Logan Chien43d34c32017-12-20 01:17:32 +08002962func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2963 actual := module.Properties.AndroidMkRuntimeLibs
2964 if !reflect.DeepEqual(actual, expected) {
2965 t.Errorf("incorrect runtime_libs for shared libs"+
2966 "\nactual: %v"+
2967 "\nexpected: %v",
2968 actual,
2969 expected,
2970 )
2971 }
2972}
2973
2974const runtimeLibAndroidBp = `
2975 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002976 name: "liball_available",
2977 vendor_available: true,
2978 product_available: true,
2979 no_libcrt : true,
2980 nocrt : true,
2981 system_shared_libs : [],
2982 }
2983 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002984 name: "libvendor_available1",
2985 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002986 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002987 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002988 nocrt : true,
2989 system_shared_libs : [],
2990 }
2991 cc_library {
2992 name: "libvendor_available2",
2993 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002994 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002995 target: {
2996 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002997 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002998 }
2999 },
Yi Konge7fe9912019-06-02 00:53:50 -07003000 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003001 nocrt : true,
3002 system_shared_libs : [],
3003 }
3004 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09003005 name: "libproduct_vendor",
3006 product_specific: true,
3007 vendor_available: true,
3008 no_libcrt : true,
3009 nocrt : true,
3010 system_shared_libs : [],
3011 }
3012 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08003013 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09003014 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07003015 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003016 nocrt : true,
3017 system_shared_libs : [],
3018 }
3019 cc_library {
3020 name: "libvendor1",
3021 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003022 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003023 nocrt : true,
3024 system_shared_libs : [],
3025 }
3026 cc_library {
3027 name: "libvendor2",
3028 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09003029 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09003030 no_libcrt : true,
3031 nocrt : true,
3032 system_shared_libs : [],
3033 }
3034 cc_library {
3035 name: "libproduct_available1",
3036 product_available: true,
3037 runtime_libs: ["liball_available"],
3038 no_libcrt : true,
3039 nocrt : true,
3040 system_shared_libs : [],
3041 }
3042 cc_library {
3043 name: "libproduct1",
3044 product_specific: true,
3045 no_libcrt : true,
3046 nocrt : true,
3047 system_shared_libs : [],
3048 }
3049 cc_library {
3050 name: "libproduct2",
3051 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09003052 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07003053 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003054 nocrt : true,
3055 system_shared_libs : [],
3056 }
3057`
3058
3059func TestRuntimeLibs(t *testing.T) {
3060 ctx := testCc(t, runtimeLibAndroidBp)
3061
3062 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08003063 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003064
Justin Yun8a2600c2020-12-07 12:44:03 +09003065 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3066 checkRuntimeLibs(t, []string{"liball_available"}, module)
3067
3068 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3069 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003070
3071 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003072 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003073
3074 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3075 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003076 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003077
Justin Yun8a2600c2020-12-07 12:44:03 +09003078 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3079 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003080
3081 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003082 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003083
3084 // runtime_libs for product variants have '.product' suffixes if the modules have both core
3085 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003086 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003087
3088 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3089 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
3090
3091 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09003092 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003093}
3094
3095func TestExcludeRuntimeLibs(t *testing.T) {
3096 ctx := testCc(t, runtimeLibAndroidBp)
3097
Colin Cross7113d202019-11-20 16:39:12 -08003098 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003099 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3100 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003101
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003102 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003103 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08003104 checkRuntimeLibs(t, nil, module)
3105}
3106
3107func TestRuntimeLibsNoVndk(t *testing.T) {
3108 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3109
3110 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3111
Colin Cross7113d202019-11-20 16:39:12 -08003112 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003113
Justin Yun8a2600c2020-12-07 12:44:03 +09003114 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3115 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003116
3117 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003118 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003119
3120 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003121 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003122}
3123
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003124func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003125 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003126 actual := module.Properties.AndroidMkStaticLibs
3127 if !reflect.DeepEqual(actual, expected) {
3128 t.Errorf("incorrect static_libs"+
3129 "\nactual: %v"+
3130 "\nexpected: %v",
3131 actual,
3132 expected,
3133 )
3134 }
3135}
3136
3137const staticLibAndroidBp = `
3138 cc_library {
3139 name: "lib1",
3140 }
3141 cc_library {
3142 name: "lib2",
3143 static_libs: ["lib1"],
3144 }
3145`
3146
3147func TestStaticLibDepExport(t *testing.T) {
3148 ctx := testCc(t, staticLibAndroidBp)
3149
3150 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003151 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003152 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Ryan Prichardc2018e22021-04-02 20:23:22 -07003153 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003154
3155 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003156 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003157 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3158 // libc++_static is linked additionally.
Ryan Prichardc2018e22021-04-02 20:23:22 -07003159 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003160}
3161
Jiyong Parkd08b6972017-09-26 10:50:54 +09003162var compilerFlagsTestCases = []struct {
3163 in string
3164 out bool
3165}{
3166 {
3167 in: "a",
3168 out: false,
3169 },
3170 {
3171 in: "-a",
3172 out: true,
3173 },
3174 {
3175 in: "-Ipath/to/something",
3176 out: false,
3177 },
3178 {
3179 in: "-isystempath/to/something",
3180 out: false,
3181 },
3182 {
3183 in: "--coverage",
3184 out: false,
3185 },
3186 {
3187 in: "-include a/b",
3188 out: true,
3189 },
3190 {
3191 in: "-include a/b c/d",
3192 out: false,
3193 },
3194 {
3195 in: "-DMACRO",
3196 out: true,
3197 },
3198 {
3199 in: "-DMAC RO",
3200 out: false,
3201 },
3202 {
3203 in: "-a -b",
3204 out: false,
3205 },
3206 {
3207 in: "-DMACRO=definition",
3208 out: true,
3209 },
3210 {
3211 in: "-DMACRO=defi nition",
3212 out: true, // TODO(jiyong): this should be false
3213 },
3214 {
3215 in: "-DMACRO(x)=x + 1",
3216 out: true,
3217 },
3218 {
3219 in: "-DMACRO=\"defi nition\"",
3220 out: true,
3221 },
3222}
3223
3224type mockContext struct {
3225 BaseModuleContext
3226 result bool
3227}
3228
3229func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3230 // CheckBadCompilerFlags calls this function when the flag should be rejected
3231 ctx.result = false
3232}
3233
3234func TestCompilerFlags(t *testing.T) {
3235 for _, testCase := range compilerFlagsTestCases {
3236 ctx := &mockContext{result: true}
3237 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3238 if ctx.result != testCase.out {
3239 t.Errorf("incorrect output:")
3240 t.Errorf(" input: %#v", testCase.in)
3241 t.Errorf(" expected: %#v", testCase.out)
3242 t.Errorf(" got: %#v", ctx.result)
3243 }
3244 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003245}
Jiyong Park374510b2018-03-19 18:23:01 +09003246
Jiyong Park37b25202018-07-11 10:49:27 +09003247func TestRecovery(t *testing.T) {
3248 ctx := testCc(t, `
3249 cc_library_shared {
3250 name: "librecovery",
3251 recovery: true,
3252 }
3253 cc_library_shared {
3254 name: "librecovery32",
3255 recovery: true,
3256 compile_multilib:"32",
3257 }
Jiyong Park5baac542018-08-28 09:55:37 +09003258 cc_library_shared {
3259 name: "libHalInRecovery",
3260 recovery_available: true,
3261 vendor: true,
3262 }
Jiyong Park37b25202018-07-11 10:49:27 +09003263 `)
3264
3265 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003266 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003267 if len(variants) != 1 || !android.InList(arm64, variants) {
3268 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3269 }
3270
3271 variants = ctx.ModuleVariantsForTests("librecovery32")
3272 if android.InList(arm64, variants) {
3273 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3274 }
Jiyong Park5baac542018-08-28 09:55:37 +09003275
3276 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3277 if !recoveryModule.Platform() {
3278 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3279 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003280}
Jiyong Park5baac542018-08-28 09:55:37 +09003281
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003282func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3283 bp := `
3284 cc_prebuilt_test_library_shared {
3285 name: "test_lib",
3286 relative_install_path: "foo/bar/baz",
3287 srcs: ["srcpath/dontusethispath/baz.so"],
3288 }
3289
3290 cc_test {
3291 name: "main_test",
3292 data_libs: ["test_lib"],
3293 gtest: false,
3294 }
3295 `
3296
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003297 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003298 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003299 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003300 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3301
3302 ctx := testCcWithConfig(t, config)
3303 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3304 testBinary := module.(*Module).linker.(*testBinary)
3305 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3306 if err != nil {
3307 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3308 }
3309 if len(outputFiles) != 1 {
3310 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3311 }
3312 if len(testBinary.dataPaths()) != 1 {
3313 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3314 }
3315
3316 outputPath := outputFiles[0].String()
3317
3318 if !strings.HasSuffix(outputPath, "/main_test") {
3319 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3320 }
Colin Crossaa255532020-07-03 13:18:24 -07003321 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003322 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3323 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3324 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3325 }
3326}
3327
Jiyong Park7ed9de32018-10-15 22:25:07 +09003328func TestVersionedStubs(t *testing.T) {
3329 ctx := testCc(t, `
3330 cc_library_shared {
3331 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003332 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003333 stubs: {
3334 symbol_file: "foo.map.txt",
3335 versions: ["1", "2", "3"],
3336 },
3337 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003338
Jiyong Park7ed9de32018-10-15 22:25:07 +09003339 cc_library_shared {
3340 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003341 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003342 shared_libs: ["libFoo#1"],
3343 }`)
3344
3345 variants := ctx.ModuleVariantsForTests("libFoo")
3346 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003347 "android_arm64_armv8-a_shared",
3348 "android_arm64_armv8-a_shared_1",
3349 "android_arm64_armv8-a_shared_2",
3350 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003351 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003352 "android_arm_armv7-a-neon_shared",
3353 "android_arm_armv7-a-neon_shared_1",
3354 "android_arm_armv7-a-neon_shared_2",
3355 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003356 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003357 }
3358 variantsMismatch := false
3359 if len(variants) != len(expectedVariants) {
3360 variantsMismatch = true
3361 } else {
3362 for _, v := range expectedVariants {
3363 if !inList(v, variants) {
3364 variantsMismatch = false
3365 }
3366 }
3367 }
3368 if variantsMismatch {
3369 t.Errorf("variants of libFoo expected:\n")
3370 for _, v := range expectedVariants {
3371 t.Errorf("%q\n", v)
3372 }
3373 t.Errorf(", but got:\n")
3374 for _, v := range variants {
3375 t.Errorf("%q\n", v)
3376 }
3377 }
3378
Colin Cross7113d202019-11-20 16:39:12 -08003379 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003380 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003381 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003382 if !strings.Contains(libFlags, libFoo1StubPath) {
3383 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3384 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003385
Colin Cross7113d202019-11-20 16:39:12 -08003386 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003387 cFlags := libBarCompileRule.Args["cFlags"]
3388 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3389 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3390 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3391 }
Jiyong Park37b25202018-07-11 10:49:27 +09003392}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003393
Jooyung Hanb04a4992020-03-13 18:57:35 +09003394func TestVersioningMacro(t *testing.T) {
3395 for _, tc := range []struct{ moduleName, expected string }{
3396 {"libc", "__LIBC_API__"},
3397 {"libfoo", "__LIBFOO_API__"},
3398 {"libfoo@1", "__LIBFOO_1_API__"},
3399 {"libfoo-v1", "__LIBFOO_V1_API__"},
3400 {"libfoo.v1", "__LIBFOO_V1_API__"},
3401 } {
3402 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3403 }
3404}
3405
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003406func TestStaticExecutable(t *testing.T) {
3407 ctx := testCc(t, `
3408 cc_binary {
3409 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003410 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003411 static_executable: true,
3412 }`)
3413
Colin Cross7113d202019-11-20 16:39:12 -08003414 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003415 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3416 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003417 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003418 for _, lib := range systemStaticLibs {
3419 if !strings.Contains(libFlags, lib) {
3420 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3421 }
3422 }
3423 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3424 for _, lib := range systemSharedLibs {
3425 if strings.Contains(libFlags, lib) {
3426 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3427 }
3428 }
3429}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003430
3431func TestStaticDepsOrderWithStubs(t *testing.T) {
3432 ctx := testCc(t, `
3433 cc_binary {
3434 name: "mybin",
3435 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003436 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003437 static_executable: true,
3438 stl: "none",
3439 }
3440
3441 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003442 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003443 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003444 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003445 stl: "none",
3446 }
3447
3448 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003449 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003450 srcs: ["foo.c"],
3451 stl: "none",
3452 stubs: {
3453 versions: ["1"],
3454 },
3455 }`)
3456
Colin Cross0de8a1e2020-09-18 14:15:30 -07003457 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3458 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003459 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003460
3461 if !reflect.DeepEqual(actual, expected) {
3462 t.Errorf("staticDeps orderings were not propagated correctly"+
3463 "\nactual: %v"+
3464 "\nexpected: %v",
3465 actual,
3466 expected,
3467 )
3468 }
3469}
Jooyung Han38002912019-05-16 04:01:54 +09003470
Jooyung Hand48f3c32019-08-23 11:18:57 +09003471func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3472 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3473 cc_library {
3474 name: "libA",
3475 srcs: ["foo.c"],
3476 shared_libs: ["libB"],
3477 stl: "none",
3478 }
3479
3480 cc_library {
3481 name: "libB",
3482 srcs: ["foo.c"],
3483 enabled: false,
3484 stl: "none",
3485 }
3486 `)
3487}
3488
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003489// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3490// correctly.
3491func TestFuzzTarget(t *testing.T) {
3492 ctx := testCc(t, `
3493 cc_fuzz {
3494 name: "fuzz_smoke_test",
3495 srcs: ["foo.c"],
3496 }`)
3497
Paul Duffin075c4172019-12-19 19:06:13 +00003498 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003499 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3500}
3501
Jiyong Park29074592019-07-07 16:27:47 +09003502func TestAidl(t *testing.T) {
3503}
3504
Jooyung Han38002912019-05-16 04:01:54 +09003505func assertString(t *testing.T, got, expected string) {
3506 t.Helper()
3507 if got != expected {
3508 t.Errorf("expected %q got %q", expected, got)
3509 }
3510}
3511
3512func assertArrayString(t *testing.T, got, expected []string) {
3513 t.Helper()
3514 if len(got) != len(expected) {
3515 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3516 return
3517 }
3518 for i := range got {
3519 if got[i] != expected[i] {
3520 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3521 i, expected[i], expected, got[i], got)
3522 return
3523 }
3524 }
3525}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003526
Jooyung Han0302a842019-10-30 18:43:49 +09003527func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3528 t.Helper()
3529 assertArrayString(t, android.SortedStringKeys(m), expected)
3530}
3531
Colin Crosse1bb5d02019-09-24 14:55:04 -07003532func TestDefaults(t *testing.T) {
3533 ctx := testCc(t, `
3534 cc_defaults {
3535 name: "defaults",
3536 srcs: ["foo.c"],
3537 static: {
3538 srcs: ["bar.c"],
3539 },
3540 shared: {
3541 srcs: ["baz.c"],
3542 },
Liz Kammer3cf52112021-03-31 15:42:03 -04003543 bazel_module: {
3544 bp2build_available: true,
3545 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07003546 }
3547
3548 cc_library_static {
3549 name: "libstatic",
3550 defaults: ["defaults"],
3551 }
3552
3553 cc_library_shared {
3554 name: "libshared",
3555 defaults: ["defaults"],
3556 }
3557
3558 cc_library {
3559 name: "libboth",
3560 defaults: ["defaults"],
3561 }
3562
3563 cc_binary {
3564 name: "binary",
3565 defaults: ["defaults"],
3566 }`)
3567
3568 pathsToBase := func(paths android.Paths) []string {
3569 var ret []string
3570 for _, p := range paths {
3571 ret = append(ret, p.Base())
3572 }
3573 return ret
3574 }
3575
Colin Cross7113d202019-11-20 16:39:12 -08003576 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003577 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3578 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3579 }
Colin Cross7113d202019-11-20 16:39:12 -08003580 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003581 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3582 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3583 }
Colin Cross7113d202019-11-20 16:39:12 -08003584 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003585 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3586 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3587 }
3588
Colin Cross7113d202019-11-20 16:39:12 -08003589 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003590 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3591 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3592 }
Colin Cross7113d202019-11-20 16:39:12 -08003593 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003594 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3595 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3596 }
3597}
Colin Crosseabaedd2020-02-06 17:01:55 -08003598
3599func TestProductVariableDefaults(t *testing.T) {
3600 bp := `
3601 cc_defaults {
3602 name: "libfoo_defaults",
3603 srcs: ["foo.c"],
3604 cppflags: ["-DFOO"],
3605 product_variables: {
3606 debuggable: {
3607 cppflags: ["-DBAR"],
3608 },
3609 },
3610 }
3611
3612 cc_library {
3613 name: "libfoo",
3614 defaults: ["libfoo_defaults"],
3615 }
3616 `
3617
Paul Duffin8567f222021-03-23 00:02:06 +00003618 result := android.GroupFixturePreparers(
3619 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003620 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003621
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003622 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3623 variables.Debuggable = BoolPtr(true)
3624 }),
3625 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003626
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003627 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003628 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003629}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003630
3631func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3632 t.Parallel()
3633 bp := `
3634 cc_library_static {
3635 name: "libfoo",
3636 srcs: ["foo.c"],
3637 whole_static_libs: ["libbar"],
3638 }
3639
3640 cc_library_static {
3641 name: "libbar",
3642 whole_static_libs: ["libmissing"],
3643 }
3644 `
3645
Paul Duffin8567f222021-03-23 00:02:06 +00003646 result := android.GroupFixturePreparers(
3647 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003648 android.PrepareForTestWithAllowMissingDependencies,
3649 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003650
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003651 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003652 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003653
Paul Duffine84b1332021-03-12 11:59:43 +00003654 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003655
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003656 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003657 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003658}
Colin Crosse9fe2942020-11-10 18:12:15 -08003659
3660func TestInstallSharedLibs(t *testing.T) {
3661 bp := `
3662 cc_binary {
3663 name: "bin",
3664 host_supported: true,
3665 shared_libs: ["libshared"],
3666 runtime_libs: ["libruntime"],
3667 srcs: [":gen"],
3668 }
3669
3670 cc_library_shared {
3671 name: "libshared",
3672 host_supported: true,
3673 shared_libs: ["libtransitive"],
3674 }
3675
3676 cc_library_shared {
3677 name: "libtransitive",
3678 host_supported: true,
3679 }
3680
3681 cc_library_shared {
3682 name: "libruntime",
3683 host_supported: true,
3684 }
3685
3686 cc_binary_host {
3687 name: "tool",
3688 srcs: ["foo.cpp"],
3689 }
3690
3691 genrule {
3692 name: "gen",
3693 tools: ["tool"],
3694 out: ["gen.cpp"],
3695 cmd: "$(location tool) $(out)",
3696 }
3697 `
3698
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003699 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003700 ctx := testCcWithConfig(t, config)
3701
3702 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3703 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3704 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3705 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3706 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3707
3708 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3709 t.Errorf("expected host bin dependency %q, got %q", w, g)
3710 }
3711
3712 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3713 t.Errorf("expected host bin dependency %q, got %q", w, g)
3714 }
3715
3716 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3717 t.Errorf("expected host bin dependency %q, got %q", w, g)
3718 }
3719
3720 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3721 t.Errorf("expected host bin dependency %q, got %q", w, g)
3722 }
3723
3724 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3725 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3726 }
3727
3728 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3729 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3730 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3731 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3732
3733 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3734 t.Errorf("expected device bin dependency %q, got %q", w, g)
3735 }
3736
3737 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3738 t.Errorf("expected device bin dependency %q, got %q", w, g)
3739 }
3740
3741 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3742 t.Errorf("expected device bin dependency %q, got %q", w, g)
3743 }
3744
3745 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3746 t.Errorf("expected device bin dependency %q, got %q", w, g)
3747 }
3748
3749 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3750 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3751 }
3752
3753}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003754
3755func TestStubsLibReexportsHeaders(t *testing.T) {
3756 ctx := testCc(t, `
3757 cc_library_shared {
3758 name: "libclient",
3759 srcs: ["foo.c"],
3760 shared_libs: ["libfoo#1"],
3761 }
3762
3763 cc_library_shared {
3764 name: "libfoo",
3765 srcs: ["foo.c"],
3766 shared_libs: ["libbar"],
3767 export_shared_lib_headers: ["libbar"],
3768 stubs: {
3769 symbol_file: "foo.map.txt",
3770 versions: ["1", "2", "3"],
3771 },
3772 }
3773
3774 cc_library_shared {
3775 name: "libbar",
3776 export_include_dirs: ["include/libbar"],
3777 srcs: ["foo.c"],
3778 }`)
3779
3780 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3781
3782 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3783 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3784 }
3785}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003786
3787func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3788 ctx := testCc(t, `
3789 cc_library {
3790 name: "libfoo",
3791 srcs: ["a/Foo.aidl"],
3792 aidl: { flags: ["-Werror"], },
3793 }
3794 `)
3795
3796 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3797 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3798 aidlCommand := manifest.Commands[0].GetCommand()
3799 expectedAidlFlag := "-Werror"
3800 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3801 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3802 }
3803}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003804
Jiyong Parka008fb02021-03-16 17:15:53 +09003805func TestMinSdkVersionInClangTriple(t *testing.T) {
3806 ctx := testCc(t, `
3807 cc_library_shared {
3808 name: "libfoo",
3809 srcs: ["foo.c"],
3810 min_sdk_version: "29",
3811 }`)
3812
3813 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3814 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3815}
3816
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003817type MemtagNoteType int
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003818
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003819const (
3820 None MemtagNoteType = iota + 1
3821 Sync
3822 Async
3823)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003824
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003825func (t MemtagNoteType) str() string {
3826 switch t {
3827 case None:
3828 return "none"
3829 case Sync:
3830 return "sync"
3831 case Async:
3832 return "async"
3833 default:
3834 panic("invalid note type")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003835 }
3836}
3837
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003838func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
3839 note_async := "note_memtag_heap_async"
3840 note_sync := "note_memtag_heap_sync"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003841
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003842 found := None
3843 implicits := m.Rule("ld").Implicits
3844 for _, lib := range implicits {
3845 if strings.Contains(lib.Rel(), note_async) {
3846 found = Async
3847 break
3848 } else if strings.Contains(lib.Rel(), note_sync) {
3849 found = Sync
3850 break
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003851 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003852 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003853
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003854 if found != expected {
3855 t.Errorf("Wrong Memtag note in target %q: found %q, expected %q", m.Module().(*Module).Name(), found.str(), expected.str())
3856 }
3857}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003858
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003859var prepareForTestWithMemtagHeap = android.GroupFixturePreparers(
3860 android.FixtureModifyMockFS(func(fs android.MockFS) {
3861 templateBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003862 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003863 name: "%[1]s_test",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003864 gtest: false,
3865 }
3866
3867 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003868 name: "%[1]s_test_false",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003869 gtest: false,
3870 sanitize: { memtag_heap: false },
3871 }
3872
3873 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003874 name: "%[1]s_test_true",
3875 gtest: false,
3876 sanitize: { memtag_heap: true },
3877 }
3878
3879 cc_test {
3880 name: "%[1]s_test_true_nodiag",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003881 gtest: false,
3882 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3883 }
3884
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003885 cc_test {
3886 name: "%[1]s_test_true_diag",
3887 gtest: false,
3888 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
3889 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003890
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003891 cc_binary {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003892 name: "%[1]s_binary",
3893 }
3894
3895 cc_binary {
3896 name: "%[1]s_binary_false",
3897 sanitize: { memtag_heap: false },
3898 }
3899
3900 cc_binary {
3901 name: "%[1]s_binary_true",
3902 sanitize: { memtag_heap: true },
3903 }
3904
3905 cc_binary {
3906 name: "%[1]s_binary_true_nodiag",
3907 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3908 }
3909
3910 cc_binary {
3911 name: "%[1]s_binary_true_diag",
3912 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003913 }
3914 `
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003915 subdirDefaultBp := fmt.Sprintf(templateBp, "default")
3916 subdirExcludeBp := fmt.Sprintf(templateBp, "exclude")
3917 subdirSyncBp := fmt.Sprintf(templateBp, "sync")
3918 subdirAsyncBp := fmt.Sprintf(templateBp, "async")
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003919
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003920 fs.Merge(android.MockFS{
3921 "subdir_default/Android.bp": []byte(subdirDefaultBp),
3922 "subdir_exclude/Android.bp": []byte(subdirExcludeBp),
3923 "subdir_sync/Android.bp": []byte(subdirSyncBp),
3924 "subdir_async/Android.bp": []byte(subdirAsyncBp),
3925 })
3926 }),
3927 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3928 variables.MemtagHeapExcludePaths = []string{"subdir_exclude"}
Evgenii Stepanov779b64e2021-04-09 14:33:10 -07003929 // "subdir_exclude" is covered by both include and exclude paths. Exclude wins.
3930 variables.MemtagHeapSyncIncludePaths = []string{"subdir_sync", "subdir_exclude"}
3931 variables.MemtagHeapAsyncIncludePaths = []string{"subdir_async", "subdir_exclude"}
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003932 }),
3933)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003934
3935func TestSanitizeMemtagHeap(t *testing.T) {
3936 variant := "android_arm64_armv8-a"
3937
Paul Duffin8567f222021-03-23 00:02:06 +00003938 result := android.GroupFixturePreparers(
3939 prepareForCcTest,
3940 prepareForTestWithMemtagHeap,
3941 ).RunTest(t)
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003942 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003943
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003944 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3945 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3946 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3947 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3948 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3949
3950 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), None)
3951 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3952 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3953 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3954 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3955
3956 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3957 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3958 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3959 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3960 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3961
3962 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3963 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3964 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3965 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3966 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3967
3968 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3969 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3970 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3971 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3972 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3973
3974 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3975 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3976 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3977 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3978 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3979
3980 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3981 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3982 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3983 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3984 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3985
3986 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3987 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3988 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3989 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3990 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3991}
3992
3993func TestSanitizeMemtagHeapWithSanitizeDevice(t *testing.T) {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003994 variant := "android_arm64_armv8-a"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003995
Paul Duffin8567f222021-03-23 00:02:06 +00003996 result := android.GroupFixturePreparers(
3997 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003998 prepareForTestWithMemtagHeap,
3999 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4000 variables.SanitizeDevice = []string{"memtag_heap"}
4001 }),
4002 ).RunTest(t)
4003 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004004
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08004005 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
4006 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
4007 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
4008 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
4009 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08004010
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08004011 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Async)
4012 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
4013 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
4014 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
4015 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
4016
4017 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
4018 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
4019 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
4020 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
4021 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
4022
4023 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
4024 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
4025 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
4026 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
4027 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
4028
4029 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
4030 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
4031 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
4032 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
4033 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
4034
4035 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
4036 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
4037 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
4038 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
4039 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
4040
4041 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
4042 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
4043 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
4044 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
4045 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
4046
4047 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
4048 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
4049 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
4050 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
4051 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
4052}
4053
4054func TestSanitizeMemtagHeapWithSanitizeDeviceDiag(t *testing.T) {
4055 variant := "android_arm64_armv8-a"
4056
Paul Duffin8567f222021-03-23 00:02:06 +00004057 result := android.GroupFixturePreparers(
4058 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004059 prepareForTestWithMemtagHeap,
4060 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4061 variables.SanitizeDevice = []string{"memtag_heap"}
4062 variables.SanitizeDeviceDiag = []string{"memtag_heap"}
4063 }),
4064 ).RunTest(t)
4065 ctx := result.TestContext
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08004066
4067 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
4068 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
4069 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Sync)
4070 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
4071 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
4072
4073 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Sync)
4074 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
4075 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Sync)
4076 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
4077 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
4078
4079 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
4080 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
4081 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Sync)
4082 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
4083 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
4084
4085 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
4086 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
4087 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Sync)
4088 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
4089 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
4090
4091 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
4092 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
4093 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Sync)
4094 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
4095 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
4096
4097 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Sync)
4098 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
4099 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Sync)
4100 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
4101 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
4102
4103 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
4104 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
4105 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
4106 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
4107 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
4108
4109 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
4110 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
4111 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
4112 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
4113 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004114}
Paul Duffin3cb603e2021-02-19 13:57:10 +00004115
4116func TestIncludeDirsExporting(t *testing.T) {
4117
4118 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4119 // embedded newline characters alone.
4120 trimIndentingSpaces := func(s string) string {
4121 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4122 }
4123
4124 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4125 t.Helper()
4126 expected = trimIndentingSpaces(expected)
4127 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4128 if expected != actual {
4129 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4130 }
4131 }
4132
4133 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4134
4135 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4136 t.Helper()
4137 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4138 name := module.Name()
4139
4140 for _, checker := range checkers {
4141 checker(t, name, exported)
4142 }
4143 }
4144
4145 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4146 return func(t *testing.T, name string, exported FlagExporterInfo) {
4147 t.Helper()
4148 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4149 }
4150 }
4151
4152 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4153 return func(t *testing.T, name string, exported FlagExporterInfo) {
4154 t.Helper()
4155 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4156 }
4157 }
4158
4159 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4160 return func(t *testing.T, name string, exported FlagExporterInfo) {
4161 t.Helper()
4162 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4163 }
4164 }
4165
4166 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4167 return func(t *testing.T, name string, exported FlagExporterInfo) {
4168 t.Helper()
4169 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4170 }
4171 }
4172
4173 genRuleModules := `
4174 genrule {
4175 name: "genrule_foo",
4176 cmd: "generate-foo",
4177 out: [
4178 "generated_headers/foo/generated_header.h",
4179 ],
4180 export_include_dirs: [
4181 "generated_headers",
4182 ],
4183 }
4184
4185 genrule {
4186 name: "genrule_bar",
4187 cmd: "generate-bar",
4188 out: [
4189 "generated_headers/bar/generated_header.h",
4190 ],
4191 export_include_dirs: [
4192 "generated_headers",
4193 ],
4194 }
4195 `
4196
4197 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4198 ctx := testCc(t, genRuleModules+`
4199 cc_library {
4200 name: "libfoo",
4201 srcs: ["foo.c"],
4202 export_include_dirs: ["foo/standard"],
4203 export_system_include_dirs: ["foo/system"],
4204 generated_headers: ["genrule_foo"],
4205 export_generated_headers: ["genrule_foo"],
4206 }
4207
4208 cc_library {
4209 name: "libbar",
4210 srcs: ["bar.c"],
4211 shared_libs: ["libfoo"],
4212 export_include_dirs: ["bar/standard"],
4213 export_system_include_dirs: ["bar/system"],
4214 generated_headers: ["genrule_bar"],
4215 export_generated_headers: ["genrule_bar"],
4216 }
4217 `)
4218 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4219 checkIncludeDirs(t, ctx, foo,
4220 expectedIncludeDirs(`
4221 foo/standard
4222 .intermediates/genrule_foo/gen/generated_headers
4223 `),
4224 expectedSystemIncludeDirs(`foo/system`),
4225 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4226 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4227 )
4228
4229 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4230 checkIncludeDirs(t, ctx, bar,
4231 expectedIncludeDirs(`
4232 bar/standard
4233 .intermediates/genrule_bar/gen/generated_headers
4234 `),
4235 expectedSystemIncludeDirs(`bar/system`),
4236 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4237 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4238 )
4239 })
4240
4241 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4242 ctx := testCc(t, genRuleModules+`
4243 cc_library {
4244 name: "libfoo",
4245 srcs: ["foo.c"],
4246 export_include_dirs: ["foo/standard"],
4247 export_system_include_dirs: ["foo/system"],
4248 generated_headers: ["genrule_foo"],
4249 export_generated_headers: ["genrule_foo"],
4250 }
4251
4252 cc_library {
4253 name: "libbar",
4254 srcs: ["bar.c"],
4255 whole_static_libs: ["libfoo"],
4256 export_include_dirs: ["bar/standard"],
4257 export_system_include_dirs: ["bar/system"],
4258 generated_headers: ["genrule_bar"],
4259 export_generated_headers: ["genrule_bar"],
4260 }
4261 `)
4262 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4263 checkIncludeDirs(t, ctx, foo,
4264 expectedIncludeDirs(`
4265 foo/standard
4266 .intermediates/genrule_foo/gen/generated_headers
4267 `),
4268 expectedSystemIncludeDirs(`foo/system`),
4269 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4270 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4271 )
4272
4273 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4274 checkIncludeDirs(t, ctx, bar,
4275 expectedIncludeDirs(`
4276 bar/standard
4277 foo/standard
4278 .intermediates/genrule_foo/gen/generated_headers
4279 .intermediates/genrule_bar/gen/generated_headers
4280 `),
4281 expectedSystemIncludeDirs(`
4282 bar/system
4283 foo/system
4284 `),
4285 expectedGeneratedHeaders(`
4286 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4287 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4288 `),
4289 expectedOrderOnlyDeps(`
4290 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4291 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4292 `),
4293 )
4294 })
4295
Paul Duffin3cb603e2021-02-19 13:57:10 +00004296 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4297 ctx := testCc(t, genRuleModules+`
4298 cc_library_shared {
4299 name: "libfoo",
4300 srcs: [
4301 "foo.c",
4302 "b.aidl",
4303 "a.proto",
4304 ],
4305 aidl: {
4306 export_aidl_headers: true,
4307 }
4308 }
4309 `)
4310 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4311 checkIncludeDirs(t, ctx, foo,
4312 expectedIncludeDirs(`
4313 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4314 `),
4315 expectedSystemIncludeDirs(``),
4316 expectedGeneratedHeaders(`
4317 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4318 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4319 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004320 `),
4321 expectedOrderOnlyDeps(`
4322 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4323 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4324 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004325 `),
4326 )
4327 })
4328
Paul Duffin3cb603e2021-02-19 13:57:10 +00004329 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4330 ctx := testCc(t, genRuleModules+`
4331 cc_library_shared {
4332 name: "libfoo",
4333 srcs: [
4334 "foo.c",
4335 "b.aidl",
4336 "a.proto",
4337 ],
4338 proto: {
4339 export_proto_headers: true,
4340 }
4341 }
4342 `)
4343 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4344 checkIncludeDirs(t, ctx, foo,
4345 expectedIncludeDirs(`
4346 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4347 `),
4348 expectedSystemIncludeDirs(``),
4349 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004350 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4351 `),
4352 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004353 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4354 `),
4355 )
4356 })
4357
Paul Duffin33056e82021-02-19 13:49:08 +00004358 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004359 ctx := testCc(t, genRuleModules+`
4360 cc_library_shared {
4361 name: "libfoo",
4362 srcs: [
4363 "foo.c",
4364 "a.sysprop",
4365 "b.aidl",
4366 "a.proto",
4367 ],
4368 }
4369 `)
4370 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4371 checkIncludeDirs(t, ctx, foo,
4372 expectedIncludeDirs(`
4373 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4374 `),
4375 expectedSystemIncludeDirs(``),
4376 expectedGeneratedHeaders(`
4377 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004378 `),
4379 expectedOrderOnlyDeps(`
4380 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
4381 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004382 `),
4383 )
4384 })
4385}