blob: 3d2160f2e3c2febf9b2eb8547668647a70d75c19 [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
560 llndk_headers {
561 name: "libllndk_headers",
562 export_include_dirs: ["include"],
563 }
564
Colin Crosse4e44bc2020-12-28 13:50:21 -0800565 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900566 name: "llndk.libraries.txt",
567 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800568 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900569 name: "vndkcore.libraries.txt",
570 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800571 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900572 name: "vndksp.libraries.txt",
573 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800574 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900575 name: "vndkprivate.libraries.txt",
576 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800577 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900578 name: "vndkproduct.libraries.txt",
579 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800580 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900581 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800582 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900583 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800584 `
585
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000586 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800587 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900588 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900589 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800590
591 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800592
Jooyung Han261e1582020-10-20 18:54:21 +0900593 // subdir == "" because VNDK libs are not supposed to be installed separately.
594 // They are installed as part of VNDK APEX instead.
595 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
596 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900597 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900598 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
599 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900600 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900601
Justin Yun6977e8a2020-10-29 18:24:11 +0900602 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
603 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900604
Inseob Kim1f086e22019-05-09 13:29:15 +0900605 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900606 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000607 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900608
609 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
610 "arm64", "armv8-a"))
611 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
612 "arm", "armv7-a-neon"))
613
614 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
615 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900616 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
617
Inseob Kim1f086e22019-05-09 13:29:15 +0900618 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
619 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900620 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900621
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900622 variant := "android_vendor.29_arm64_armv8-a_shared"
623 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900624
Inseob Kim7f283f42020-06-01 21:53:49 +0900625 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
626
627 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
628 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900629 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
630 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900631 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
632 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Justin Yun450ae722021-04-16 19:58:18 +0900633 checkSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
634 checkSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900635
Jooyung Han39edb6c2019-11-06 16:53:07 +0900636 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900637 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
638 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
639 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
640 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900641 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900642
Jooyung Han097087b2019-10-22 19:32:18 +0900643 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
644 "LLNDK: libc.so",
645 "LLNDK: libdl.so",
646 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900647 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900648 "LLNDK: libm.so",
649 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900650 "VNDK-SP: libvndk_sp-x.so",
651 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900652 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900653 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900654 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900655 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900656 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900657 "VNDK-private: libvndk-private.so",
658 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900659 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900660 "VNDK-product: libc++.so",
661 "VNDK-product: libvndk_product.so",
662 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900663 })
Justin Yun450ae722021-04-16 19:58:18 +0900664 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900665 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
666 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
667 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
Justin Yun8a2600c2020-12-07 12:44:03 +0900668 checkVndkLibrariesOutput(t, ctx, "vndkproduct.libraries.txt", []string{"libc++.so", "libvndk_product.so", "libvndk_sp_product_private-x.so"})
Jooyung Han2216fb12019-11-06 16:46:15 +0900669 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
670}
671
Yo Chiangbba545e2020-06-09 16:15:37 +0800672func TestVndkWithHostSupported(t *testing.T) {
673 ctx := testCc(t, `
674 cc_library {
675 name: "libvndk_host_supported",
676 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900677 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800678 vndk: {
679 enabled: true,
680 },
681 host_supported: true,
682 }
683
684 cc_library {
685 name: "libvndk_host_supported_but_disabled_on_device",
686 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900687 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800688 vndk: {
689 enabled: true,
690 },
691 host_supported: true,
692 enabled: false,
693 target: {
694 host: {
695 enabled: true,
696 }
697 }
698 }
699
Colin Crosse4e44bc2020-12-28 13:50:21 -0800700 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800701 name: "vndkcore.libraries.txt",
702 }
703 `)
704
705 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
706}
707
Jooyung Han2216fb12019-11-06 16:46:15 +0900708func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800709 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800710 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900711 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800712 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800713 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000714 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800715 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900716 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800717 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900718
719 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Colin Crossaa255532020-07-03 13:18:24 -0700720 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900721 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900722}
723
724func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800725 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900726 cc_library {
727 name: "libvndk",
728 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900729 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900730 vndk: {
731 enabled: true,
732 },
733 nocrt: true,
734 }
735
736 cc_library {
737 name: "libvndk_sp",
738 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900739 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900740 vndk: {
741 enabled: true,
742 support_system_process: true,
743 },
744 nocrt: true,
745 }
746
747 cc_library {
748 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900749 vendor_available: true,
750 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900751 vndk: {
752 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900753 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900754 },
755 nocrt: true,
756 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900757
Colin Crosse4e44bc2020-12-28 13:50:21 -0800758 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900759 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800760 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900761 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800762 `
763
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000764 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800765 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900766 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800767 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
768
769 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
770
771 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900772
Jooyung Han2216fb12019-11-06 16:46:15 +0900773 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900774}
775
Chris Parsons79d66a52020-06-05 17:26:16 -0400776func TestDataLibs(t *testing.T) {
777 bp := `
778 cc_test_library {
779 name: "test_lib",
780 srcs: ["test_lib.cpp"],
781 gtest: false,
782 }
783
784 cc_test {
785 name: "main_test",
786 data_libs: ["test_lib"],
787 gtest: false,
788 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400789 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400790
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000791 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400792 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900793 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400794 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
795
796 ctx := testCcWithConfig(t, config)
797 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
798 testBinary := module.(*Module).linker.(*testBinary)
799 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
800 if err != nil {
801 t.Errorf("Expected cc_test to produce output files, error: %s", err)
802 return
803 }
804 if len(outputFiles) != 1 {
805 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
806 return
807 }
808 if len(testBinary.dataPaths()) != 1 {
809 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
810 return
811 }
812
813 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400814 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400815
816 if !strings.HasSuffix(outputPath, "/main_test") {
817 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
818 return
819 }
820 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
821 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
822 return
823 }
824}
825
Chris Parsons216e10a2020-07-09 17:12:52 -0400826func TestDataLibsRelativeInstallPath(t *testing.T) {
827 bp := `
828 cc_test_library {
829 name: "test_lib",
830 srcs: ["test_lib.cpp"],
831 relative_install_path: "foo/bar/baz",
832 gtest: false,
833 }
834
835 cc_test {
836 name: "main_test",
837 data_libs: ["test_lib"],
838 gtest: false,
839 }
840 `
841
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000842 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400843 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900844 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400845 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
846
847 ctx := testCcWithConfig(t, config)
848 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
849 testBinary := module.(*Module).linker.(*testBinary)
850 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
851 if err != nil {
852 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
853 }
854 if len(outputFiles) != 1 {
855 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
856 }
857 if len(testBinary.dataPaths()) != 1 {
858 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
859 }
860
861 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400862
863 if !strings.HasSuffix(outputPath, "/main_test") {
864 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
865 }
Colin Crossaa255532020-07-03 13:18:24 -0700866 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400867 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
868 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400869 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400870 }
871}
872
Jooyung Han0302a842019-10-30 18:43:49 +0900873func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900874 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900875 cc_library {
876 name: "libvndk",
877 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900878 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900879 vndk: {
880 enabled: true,
881 },
882 nocrt: true,
883 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900884 cc_library {
885 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900886 vendor_available: true,
887 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900888 vndk: {
889 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900890 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900891 },
892 nocrt: true,
893 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800894
895 cc_library {
896 name: "libllndk",
897 llndk_stubs: "libllndk.llndk",
898 }
899
900 llndk_library {
901 name: "libllndk.llndk",
902 symbol_file: "",
903 export_llndk_headers: ["libllndk_headers"],
904 }
905
906 llndk_headers {
907 name: "libllndk_headers",
908 export_include_dirs: ["include"],
909 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900910 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900911
912 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
913 "LLNDK: libc.so",
914 "LLNDK: libdl.so",
915 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800916 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900917 "LLNDK: libm.so",
918 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900919 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900920 "VNDK-core: libvndk.so",
921 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900922 "VNDK-private: libvndk-private.so",
923 "VNDK-product: libc++.so",
924 "VNDK-product: libvndk-private.so",
925 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900926 })
Logan Chienf3511742017-10-31 18:04:35 +0800927}
928
Justin Yun63e9ec72020-10-29 16:49:43 +0900929func TestVndkModuleError(t *testing.T) {
930 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900931 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900932 cc_library {
933 name: "libvndk",
934 vndk: {
935 enabled: true,
936 },
937 nocrt: true,
938 }
939 `)
940
Justin Yunc0d8c492021-01-07 17:45:31 +0900941 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900942 cc_library {
943 name: "libvndk",
944 product_available: true,
945 vndk: {
946 enabled: true,
947 },
948 nocrt: true,
949 }
950 `)
951
Justin Yun6977e8a2020-10-29 18:24:11 +0900952 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
953 cc_library {
954 name: "libvndkprop",
955 vendor_available: true,
956 product_available: true,
957 vndk: {
958 enabled: true,
959 },
960 nocrt: true,
961 target: {
962 vendor: {
963 cflags: ["-DTEST",],
964 },
965 },
966 }
967 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900968}
969
Logan Chiend3c59a22018-03-29 14:08:15 +0800970func TestVndkDepError(t *testing.T) {
971 // Check whether an error is emitted when a VNDK lib depends on a system lib.
972 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
973 cc_library {
974 name: "libvndk",
975 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900976 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800977 vndk: {
978 enabled: true,
979 },
980 shared_libs: ["libfwk"], // Cause error
981 nocrt: true,
982 }
983
984 cc_library {
985 name: "libfwk",
986 nocrt: true,
987 }
988 `)
989
990 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
991 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
992 cc_library {
993 name: "libvndk",
994 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900995 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800996 vndk: {
997 enabled: true,
998 },
999 shared_libs: ["libvendor"], // Cause error
1000 nocrt: true,
1001 }
1002
1003 cc_library {
1004 name: "libvendor",
1005 vendor: true,
1006 nocrt: true,
1007 }
1008 `)
1009
1010 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
1011 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1012 cc_library {
1013 name: "libvndk_sp",
1014 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001015 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001016 vndk: {
1017 enabled: true,
1018 support_system_process: true,
1019 },
1020 shared_libs: ["libfwk"], // Cause error
1021 nocrt: true,
1022 }
1023
1024 cc_library {
1025 name: "libfwk",
1026 nocrt: true,
1027 }
1028 `)
1029
1030 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
1031 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1032 cc_library {
1033 name: "libvndk_sp",
1034 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001035 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001036 vndk: {
1037 enabled: true,
1038 support_system_process: true,
1039 },
1040 shared_libs: ["libvendor"], // Cause error
1041 nocrt: true,
1042 }
1043
1044 cc_library {
1045 name: "libvendor",
1046 vendor: true,
1047 nocrt: true,
1048 }
1049 `)
1050
1051 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
1052 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1053 cc_library {
1054 name: "libvndk_sp",
1055 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001056 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001057 vndk: {
1058 enabled: true,
1059 support_system_process: true,
1060 },
1061 shared_libs: ["libvndk"], // Cause error
1062 nocrt: true,
1063 }
1064
1065 cc_library {
1066 name: "libvndk",
1067 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001068 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001069 vndk: {
1070 enabled: true,
1071 },
1072 nocrt: true,
1073 }
1074 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001075
1076 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1077 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1078 cc_library {
1079 name: "libvndk",
1080 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001081 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001082 vndk: {
1083 enabled: true,
1084 },
1085 shared_libs: ["libnonvndk"],
1086 nocrt: true,
1087 }
1088
1089 cc_library {
1090 name: "libnonvndk",
1091 vendor_available: true,
1092 nocrt: true,
1093 }
1094 `)
1095
1096 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1097 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1098 cc_library {
1099 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001100 vendor_available: true,
1101 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001102 vndk: {
1103 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001104 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001105 },
1106 shared_libs: ["libnonvndk"],
1107 nocrt: true,
1108 }
1109
1110 cc_library {
1111 name: "libnonvndk",
1112 vendor_available: true,
1113 nocrt: true,
1114 }
1115 `)
1116
1117 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1118 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1119 cc_library {
1120 name: "libvndksp",
1121 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001122 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001123 vndk: {
1124 enabled: true,
1125 support_system_process: true,
1126 },
1127 shared_libs: ["libnonvndk"],
1128 nocrt: true,
1129 }
1130
1131 cc_library {
1132 name: "libnonvndk",
1133 vendor_available: true,
1134 nocrt: true,
1135 }
1136 `)
1137
1138 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1139 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1140 cc_library {
1141 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001142 vendor_available: true,
1143 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001144 vndk: {
1145 enabled: true,
1146 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001147 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001148 },
1149 shared_libs: ["libnonvndk"],
1150 nocrt: true,
1151 }
1152
1153 cc_library {
1154 name: "libnonvndk",
1155 vendor_available: true,
1156 nocrt: true,
1157 }
1158 `)
1159}
1160
1161func TestDoubleLoadbleDep(t *testing.T) {
1162 // okay to link : LLNDK -> double_loadable VNDK
1163 testCc(t, `
1164 cc_library {
1165 name: "libllndk",
1166 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001167 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001168 }
1169
1170 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001171 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001172 symbol_file: "",
1173 }
1174
1175 cc_library {
1176 name: "libdoubleloadable",
1177 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001178 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001179 vndk: {
1180 enabled: true,
1181 },
1182 double_loadable: true,
1183 }
1184 `)
1185 // okay to link : LLNDK -> VNDK-SP
1186 testCc(t, `
1187 cc_library {
1188 name: "libllndk",
1189 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -07001190 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001191 }
1192
1193 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001194 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001195 symbol_file: "",
1196 }
1197
1198 cc_library {
1199 name: "libvndksp",
1200 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001201 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001202 vndk: {
1203 enabled: true,
1204 support_system_process: true,
1205 },
1206 }
1207 `)
1208 // okay to link : double_loadable -> double_loadable
1209 testCc(t, `
1210 cc_library {
1211 name: "libdoubleloadable1",
1212 shared_libs: ["libdoubleloadable2"],
1213 vendor_available: true,
1214 double_loadable: true,
1215 }
1216
1217 cc_library {
1218 name: "libdoubleloadable2",
1219 vendor_available: true,
1220 double_loadable: true,
1221 }
1222 `)
1223 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1224 testCc(t, `
1225 cc_library {
1226 name: "libdoubleloadable",
1227 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001228 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001229 vndk: {
1230 enabled: true,
1231 },
1232 double_loadable: true,
1233 shared_libs: ["libnondoubleloadable"],
1234 }
1235
1236 cc_library {
1237 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001238 vendor_available: true,
1239 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001240 vndk: {
1241 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001242 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001243 },
1244 double_loadable: true,
1245 }
1246 `)
1247 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1248 testCc(t, `
1249 cc_library {
1250 name: "libllndk",
1251 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001252 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001253 }
1254
1255 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001256 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001257 symbol_file: "",
1258 }
1259
1260 cc_library {
1261 name: "libcoreonly",
1262 shared_libs: ["libvendoravailable"],
1263 }
1264
1265 // indirect dependency of LLNDK
1266 cc_library {
1267 name: "libvendoravailable",
1268 vendor_available: true,
1269 double_loadable: true,
1270 }
1271 `)
1272}
1273
1274func TestDoubleLoadableDepError(t *testing.T) {
1275 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1276 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1277 cc_library {
1278 name: "libllndk",
1279 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001280 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001281 }
1282
1283 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001284 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001285 symbol_file: "",
1286 }
1287
1288 cc_library {
1289 name: "libnondoubleloadable",
1290 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001291 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001292 vndk: {
1293 enabled: true,
1294 },
1295 }
1296 `)
1297
1298 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1299 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1300 cc_library {
1301 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001302 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001303 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001304 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001305 }
1306
1307 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001308 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001309 symbol_file: "",
1310 }
1311
1312 cc_library {
1313 name: "libnondoubleloadable",
1314 vendor_available: true,
1315 }
1316 `)
1317
Jooyung Hana70f0672019-01-18 15:20:43 +09001318 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1319 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1320 cc_library {
1321 name: "libllndk",
1322 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001323 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001324 }
1325
1326 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001327 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001328 symbol_file: "",
1329 }
1330
1331 cc_library {
1332 name: "libcoreonly",
1333 shared_libs: ["libvendoravailable"],
1334 }
1335
1336 // indirect dependency of LLNDK
1337 cc_library {
1338 name: "libvendoravailable",
1339 vendor_available: true,
1340 }
1341 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001342
1343 // The error is not from 'client' but from 'libllndk'
1344 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1345 cc_library {
1346 name: "client",
1347 vendor_available: true,
1348 double_loadable: true,
1349 shared_libs: ["libllndk"],
1350 }
1351 cc_library {
1352 name: "libllndk",
1353 shared_libs: ["libnondoubleloadable"],
1354 llndk_stubs: "libllndk.llndk",
1355 }
1356 llndk_library {
1357 name: "libllndk.llndk",
1358 symbol_file: "",
1359 }
1360 cc_library {
1361 name: "libnondoubleloadable",
1362 vendor_available: true,
1363 }
1364 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001365}
1366
Jooyung Han479ca172020-10-19 18:51:07 +09001367func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1368 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1369 cc_library {
1370 name: "libvndksp",
1371 shared_libs: ["libanothervndksp"],
1372 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001373 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001374 vndk: {
1375 enabled: true,
1376 support_system_process: true,
1377 }
1378 }
1379
1380 cc_library {
1381 name: "libllndk",
1382 shared_libs: ["libanothervndksp"],
1383 }
1384
1385 llndk_library {
1386 name: "libllndk",
1387 symbol_file: "",
1388 }
1389
1390 cc_library {
1391 name: "libanothervndksp",
1392 vendor_available: true,
1393 }
1394 `)
1395}
1396
Logan Chienf3511742017-10-31 18:04:35 +08001397func TestVndkExt(t *testing.T) {
1398 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001399 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001400 cc_library {
1401 name: "libvndk",
1402 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001403 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001404 vndk: {
1405 enabled: true,
1406 },
1407 nocrt: true,
1408 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001409 cc_library {
1410 name: "libvndk2",
1411 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001412 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001413 vndk: {
1414 enabled: true,
1415 },
1416 target: {
1417 vendor: {
1418 suffix: "-suffix",
1419 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001420 product: {
1421 suffix: "-suffix",
1422 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001423 },
1424 nocrt: true,
1425 }
Logan Chienf3511742017-10-31 18:04:35 +08001426
1427 cc_library {
1428 name: "libvndk_ext",
1429 vendor: true,
1430 vndk: {
1431 enabled: true,
1432 extends: "libvndk",
1433 },
1434 nocrt: true,
1435 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001436
1437 cc_library {
1438 name: "libvndk2_ext",
1439 vendor: true,
1440 vndk: {
1441 enabled: true,
1442 extends: "libvndk2",
1443 },
1444 nocrt: true,
1445 }
Logan Chienf3511742017-10-31 18:04:35 +08001446
Justin Yun0ecf0b22020-02-28 15:07:59 +09001447 cc_library {
1448 name: "libvndk_ext_product",
1449 product_specific: true,
1450 vndk: {
1451 enabled: true,
1452 extends: "libvndk",
1453 },
1454 nocrt: true,
1455 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001456
Justin Yun0ecf0b22020-02-28 15:07:59 +09001457 cc_library {
1458 name: "libvndk2_ext_product",
1459 product_specific: true,
1460 vndk: {
1461 enabled: true,
1462 extends: "libvndk2",
1463 },
1464 nocrt: true,
1465 }
1466 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001467 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001468 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1469 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001470 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001471
1472 ctx := testCcWithConfig(t, config)
1473
1474 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1475 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1476
1477 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1478 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1479
1480 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1481 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001482}
1483
Logan Chiend3c59a22018-03-29 14:08:15 +08001484func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001485 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1486 ctx := testCcNoVndk(t, `
1487 cc_library {
1488 name: "libvndk",
1489 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001490 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001491 vndk: {
1492 enabled: true,
1493 },
1494 nocrt: true,
1495 }
1496
1497 cc_library {
1498 name: "libvndk_ext",
1499 vendor: true,
1500 vndk: {
1501 enabled: true,
1502 extends: "libvndk",
1503 },
1504 nocrt: true,
1505 }
1506 `)
1507
1508 // Ensures that the core variant of "libvndk_ext" can be found.
1509 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1510 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1511 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1512 }
1513}
1514
Justin Yun0ecf0b22020-02-28 15:07:59 +09001515func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1516 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001517 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001518 cc_library {
1519 name: "libvndk",
1520 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001521 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001522 vndk: {
1523 enabled: true,
1524 },
1525 nocrt: true,
1526 }
1527
1528 cc_library {
1529 name: "libvndk_ext_product",
1530 product_specific: true,
1531 vndk: {
1532 enabled: true,
1533 extends: "libvndk",
1534 },
1535 nocrt: true,
1536 }
1537 `)
1538
1539 // Ensures that the core variant of "libvndk_ext_product" can be found.
1540 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1541 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1542 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1543 }
1544}
1545
Logan Chienf3511742017-10-31 18:04:35 +08001546func TestVndkExtError(t *testing.T) {
1547 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001548 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001549 cc_library {
1550 name: "libvndk",
1551 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001552 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001553 vndk: {
1554 enabled: true,
1555 },
1556 nocrt: true,
1557 }
1558
1559 cc_library {
1560 name: "libvndk_ext",
1561 vndk: {
1562 enabled: true,
1563 extends: "libvndk",
1564 },
1565 nocrt: true,
1566 }
1567 `)
1568
1569 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1570 cc_library {
1571 name: "libvndk",
1572 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001573 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001574 vndk: {
1575 enabled: true,
1576 },
1577 nocrt: true,
1578 }
1579
1580 cc_library {
1581 name: "libvndk_ext",
1582 vendor: true,
1583 vndk: {
1584 enabled: true,
1585 },
1586 nocrt: true,
1587 }
1588 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001589
1590 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1591 cc_library {
1592 name: "libvndk",
1593 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001594 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001595 vndk: {
1596 enabled: true,
1597 },
1598 nocrt: true,
1599 }
1600
1601 cc_library {
1602 name: "libvndk_ext_product",
1603 product_specific: true,
1604 vndk: {
1605 enabled: true,
1606 },
1607 nocrt: true,
1608 }
1609 `)
1610
1611 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1612 cc_library {
1613 name: "libvndk",
1614 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001615 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001616 vndk: {
1617 enabled: true,
1618 },
1619 nocrt: true,
1620 }
1621
1622 cc_library {
1623 name: "libvndk_ext_product",
1624 product_specific: true,
1625 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001626 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001627 vndk: {
1628 enabled: true,
1629 extends: "libvndk",
1630 },
1631 nocrt: true,
1632 }
1633 `)
Logan Chienf3511742017-10-31 18:04:35 +08001634}
1635
1636func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1637 // This test ensures an error is emitted for inconsistent support_system_process.
1638 testCcError(t, "module \".*\" with mismatched support_system_process", `
1639 cc_library {
1640 name: "libvndk",
1641 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001642 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001643 vndk: {
1644 enabled: true,
1645 },
1646 nocrt: true,
1647 }
1648
1649 cc_library {
1650 name: "libvndk_sp_ext",
1651 vendor: true,
1652 vndk: {
1653 enabled: true,
1654 extends: "libvndk",
1655 support_system_process: true,
1656 },
1657 nocrt: true,
1658 }
1659 `)
1660
1661 testCcError(t, "module \".*\" with mismatched support_system_process", `
1662 cc_library {
1663 name: "libvndk_sp",
1664 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001665 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001666 vndk: {
1667 enabled: true,
1668 support_system_process: true,
1669 },
1670 nocrt: true,
1671 }
1672
1673 cc_library {
1674 name: "libvndk_ext",
1675 vendor: true,
1676 vndk: {
1677 enabled: true,
1678 extends: "libvndk_sp",
1679 },
1680 nocrt: true,
1681 }
1682 `)
1683}
1684
1685func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001686 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001687 // with `private: true`.
1688 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001689 cc_library {
1690 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001691 vendor_available: true,
1692 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001693 vndk: {
1694 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001695 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001696 },
1697 nocrt: true,
1698 }
1699
1700 cc_library {
1701 name: "libvndk_ext",
1702 vendor: true,
1703 vndk: {
1704 enabled: true,
1705 extends: "libvndk",
1706 },
1707 nocrt: true,
1708 }
1709 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001710
Justin Yunfd9e8042020-12-23 18:23:14 +09001711 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001712 cc_library {
1713 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001714 vendor_available: true,
1715 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001716 vndk: {
1717 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001718 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001719 },
1720 nocrt: true,
1721 }
1722
1723 cc_library {
1724 name: "libvndk_ext_product",
1725 product_specific: true,
1726 vndk: {
1727 enabled: true,
1728 extends: "libvndk",
1729 },
1730 nocrt: true,
1731 }
1732 `)
Logan Chienf3511742017-10-31 18:04:35 +08001733}
1734
Logan Chiend3c59a22018-03-29 14:08:15 +08001735func TestVendorModuleUseVndkExt(t *testing.T) {
1736 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001737 testCc(t, `
1738 cc_library {
1739 name: "libvndk",
1740 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001741 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001742 vndk: {
1743 enabled: true,
1744 },
1745 nocrt: true,
1746 }
1747
1748 cc_library {
1749 name: "libvndk_ext",
1750 vendor: true,
1751 vndk: {
1752 enabled: true,
1753 extends: "libvndk",
1754 },
1755 nocrt: true,
1756 }
1757
1758 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001759 name: "libvndk_sp",
1760 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001761 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001762 vndk: {
1763 enabled: true,
1764 support_system_process: true,
1765 },
1766 nocrt: true,
1767 }
1768
1769 cc_library {
1770 name: "libvndk_sp_ext",
1771 vendor: true,
1772 vndk: {
1773 enabled: true,
1774 extends: "libvndk_sp",
1775 support_system_process: true,
1776 },
1777 nocrt: true,
1778 }
1779
1780 cc_library {
1781 name: "libvendor",
1782 vendor: true,
1783 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1784 nocrt: true,
1785 }
1786 `)
1787}
1788
Logan Chiend3c59a22018-03-29 14:08:15 +08001789func TestVndkExtUseVendorLib(t *testing.T) {
1790 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001791 testCc(t, `
1792 cc_library {
1793 name: "libvndk",
1794 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001795 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001796 vndk: {
1797 enabled: true,
1798 },
1799 nocrt: true,
1800 }
1801
1802 cc_library {
1803 name: "libvndk_ext",
1804 vendor: true,
1805 vndk: {
1806 enabled: true,
1807 extends: "libvndk",
1808 },
1809 shared_libs: ["libvendor"],
1810 nocrt: true,
1811 }
1812
1813 cc_library {
1814 name: "libvendor",
1815 vendor: true,
1816 nocrt: true,
1817 }
1818 `)
Logan Chienf3511742017-10-31 18:04:35 +08001819
Logan Chiend3c59a22018-03-29 14:08:15 +08001820 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1821 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001822 cc_library {
1823 name: "libvndk_sp",
1824 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001825 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001826 vndk: {
1827 enabled: true,
1828 support_system_process: true,
1829 },
1830 nocrt: true,
1831 }
1832
1833 cc_library {
1834 name: "libvndk_sp_ext",
1835 vendor: true,
1836 vndk: {
1837 enabled: true,
1838 extends: "libvndk_sp",
1839 support_system_process: true,
1840 },
1841 shared_libs: ["libvendor"], // Cause an error
1842 nocrt: true,
1843 }
1844
1845 cc_library {
1846 name: "libvendor",
1847 vendor: true,
1848 nocrt: true,
1849 }
1850 `)
1851}
1852
Justin Yun0ecf0b22020-02-28 15:07:59 +09001853func TestProductVndkExtDependency(t *testing.T) {
1854 bp := `
1855 cc_library {
1856 name: "libvndk",
1857 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001858 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001859 vndk: {
1860 enabled: true,
1861 },
1862 nocrt: true,
1863 }
1864
1865 cc_library {
1866 name: "libvndk_ext_product",
1867 product_specific: true,
1868 vndk: {
1869 enabled: true,
1870 extends: "libvndk",
1871 },
1872 shared_libs: ["libproduct_for_vndklibs"],
1873 nocrt: true,
1874 }
1875
1876 cc_library {
1877 name: "libvndk_sp",
1878 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001879 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001880 vndk: {
1881 enabled: true,
1882 support_system_process: true,
1883 },
1884 nocrt: true,
1885 }
1886
1887 cc_library {
1888 name: "libvndk_sp_ext_product",
1889 product_specific: true,
1890 vndk: {
1891 enabled: true,
1892 extends: "libvndk_sp",
1893 support_system_process: true,
1894 },
1895 shared_libs: ["libproduct_for_vndklibs"],
1896 nocrt: true,
1897 }
1898
1899 cc_library {
1900 name: "libproduct",
1901 product_specific: true,
1902 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1903 nocrt: true,
1904 }
1905
1906 cc_library {
1907 name: "libproduct_for_vndklibs",
1908 product_specific: true,
1909 nocrt: true,
1910 }
1911 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001912 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001913 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1914 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001915 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001916
1917 testCcWithConfig(t, config)
1918}
1919
Logan Chiend3c59a22018-03-29 14:08:15 +08001920func TestVndkSpExtUseVndkError(t *testing.T) {
1921 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1922 // library.
1923 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1924 cc_library {
1925 name: "libvndk",
1926 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001927 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001928 vndk: {
1929 enabled: true,
1930 },
1931 nocrt: true,
1932 }
1933
1934 cc_library {
1935 name: "libvndk_sp",
1936 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001937 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001938 vndk: {
1939 enabled: true,
1940 support_system_process: true,
1941 },
1942 nocrt: true,
1943 }
1944
1945 cc_library {
1946 name: "libvndk_sp_ext",
1947 vendor: true,
1948 vndk: {
1949 enabled: true,
1950 extends: "libvndk_sp",
1951 support_system_process: true,
1952 },
1953 shared_libs: ["libvndk"], // Cause an error
1954 nocrt: true,
1955 }
1956 `)
1957
1958 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1959 // library.
1960 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1961 cc_library {
1962 name: "libvndk",
1963 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001964 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001965 vndk: {
1966 enabled: true,
1967 },
1968 nocrt: true,
1969 }
1970
1971 cc_library {
1972 name: "libvndk_ext",
1973 vendor: true,
1974 vndk: {
1975 enabled: true,
1976 extends: "libvndk",
1977 },
1978 nocrt: true,
1979 }
1980
1981 cc_library {
1982 name: "libvndk_sp",
1983 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001984 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001985 vndk: {
1986 enabled: true,
1987 support_system_process: true,
1988 },
1989 nocrt: true,
1990 }
1991
1992 cc_library {
1993 name: "libvndk_sp_ext",
1994 vendor: true,
1995 vndk: {
1996 enabled: true,
1997 extends: "libvndk_sp",
1998 support_system_process: true,
1999 },
2000 shared_libs: ["libvndk_ext"], // Cause an error
2001 nocrt: true,
2002 }
2003 `)
2004}
2005
2006func TestVndkUseVndkExtError(t *testing.T) {
2007 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2008 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002009 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2010 cc_library {
2011 name: "libvndk",
2012 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002013 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002014 vndk: {
2015 enabled: true,
2016 },
2017 nocrt: true,
2018 }
2019
2020 cc_library {
2021 name: "libvndk_ext",
2022 vendor: true,
2023 vndk: {
2024 enabled: true,
2025 extends: "libvndk",
2026 },
2027 nocrt: true,
2028 }
2029
2030 cc_library {
2031 name: "libvndk2",
2032 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002033 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002034 vndk: {
2035 enabled: true,
2036 },
2037 shared_libs: ["libvndk_ext"],
2038 nocrt: true,
2039 }
2040 `)
2041
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002042 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002043 cc_library {
2044 name: "libvndk",
2045 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002046 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002047 vndk: {
2048 enabled: true,
2049 },
2050 nocrt: true,
2051 }
2052
2053 cc_library {
2054 name: "libvndk_ext",
2055 vendor: true,
2056 vndk: {
2057 enabled: true,
2058 extends: "libvndk",
2059 },
2060 nocrt: true,
2061 }
2062
2063 cc_library {
2064 name: "libvndk2",
2065 vendor_available: true,
2066 vndk: {
2067 enabled: true,
2068 },
2069 target: {
2070 vendor: {
2071 shared_libs: ["libvndk_ext"],
2072 },
2073 },
2074 nocrt: true,
2075 }
2076 `)
2077
2078 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2079 cc_library {
2080 name: "libvndk_sp",
2081 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002082 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002083 vndk: {
2084 enabled: true,
2085 support_system_process: true,
2086 },
2087 nocrt: true,
2088 }
2089
2090 cc_library {
2091 name: "libvndk_sp_ext",
2092 vendor: true,
2093 vndk: {
2094 enabled: true,
2095 extends: "libvndk_sp",
2096 support_system_process: true,
2097 },
2098 nocrt: true,
2099 }
2100
2101 cc_library {
2102 name: "libvndk_sp_2",
2103 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002104 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002105 vndk: {
2106 enabled: true,
2107 support_system_process: true,
2108 },
2109 shared_libs: ["libvndk_sp_ext"],
2110 nocrt: true,
2111 }
2112 `)
2113
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002114 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002115 cc_library {
2116 name: "libvndk_sp",
2117 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002118 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002119 vndk: {
2120 enabled: true,
2121 },
2122 nocrt: true,
2123 }
2124
2125 cc_library {
2126 name: "libvndk_sp_ext",
2127 vendor: true,
2128 vndk: {
2129 enabled: true,
2130 extends: "libvndk_sp",
2131 },
2132 nocrt: true,
2133 }
2134
2135 cc_library {
2136 name: "libvndk_sp2",
2137 vendor_available: true,
2138 vndk: {
2139 enabled: true,
2140 },
2141 target: {
2142 vendor: {
2143 shared_libs: ["libvndk_sp_ext"],
2144 },
2145 },
2146 nocrt: true,
2147 }
2148 `)
2149}
2150
Justin Yun5f7f7e82019-11-18 19:52:14 +09002151func TestEnforceProductVndkVersion(t *testing.T) {
2152 bp := `
2153 cc_library {
2154 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002155 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002156 }
2157 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002158 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002159 symbol_file: "",
2160 }
2161 cc_library {
2162 name: "libvndk",
2163 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002164 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002165 vndk: {
2166 enabled: true,
2167 },
2168 nocrt: true,
2169 }
2170 cc_library {
2171 name: "libvndk_sp",
2172 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002173 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002174 vndk: {
2175 enabled: true,
2176 support_system_process: true,
2177 },
2178 nocrt: true,
2179 }
2180 cc_library {
2181 name: "libva",
2182 vendor_available: true,
2183 nocrt: true,
2184 }
2185 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002186 name: "libpa",
2187 product_available: true,
2188 nocrt: true,
2189 }
2190 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002191 name: "libboth_available",
2192 vendor_available: true,
2193 product_available: true,
2194 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002195 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002196 target: {
2197 vendor: {
2198 suffix: "-vendor",
2199 },
2200 product: {
2201 suffix: "-product",
2202 },
2203 }
2204 }
2205 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002206 name: "libproduct_va",
2207 product_specific: true,
2208 vendor_available: true,
2209 nocrt: true,
2210 }
2211 cc_library {
2212 name: "libprod",
2213 product_specific: true,
2214 shared_libs: [
2215 "libllndk",
2216 "libvndk",
2217 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002218 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002219 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002220 "libproduct_va",
2221 ],
2222 nocrt: true,
2223 }
2224 cc_library {
2225 name: "libvendor",
2226 vendor: true,
2227 shared_libs: [
2228 "libllndk",
2229 "libvndk",
2230 "libvndk_sp",
2231 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002232 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002233 "libproduct_va",
2234 ],
2235 nocrt: true,
2236 }
2237 `
2238
Paul Duffin8567f222021-03-23 00:02:06 +00002239 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002240
Jooyung Han261e1582020-10-20 18:54:21 +09002241 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2242 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002243
2244 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2245 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2246
2247 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2248 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002249
2250 ensureStringContains := func(t *testing.T, str string, substr string) {
2251 t.Helper()
2252 if !strings.Contains(str, substr) {
2253 t.Errorf("%q is not found in %v", substr, str)
2254 }
2255 }
2256 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2257 t.Helper()
2258 if strings.Contains(str, substr) {
2259 t.Errorf("%q is found in %v", substr, str)
2260 }
2261 }
2262
2263 // _static variant is used since _shared reuses *.o from the static variant
2264 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2265 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2266
2267 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2268 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2269 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2270 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2271
2272 product_cflags := product_static.Rule("cc").Args["cFlags"]
2273 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2274 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2275 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002276}
2277
2278func TestEnforceProductVndkVersionErrors(t *testing.T) {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002279 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002280 cc_library {
2281 name: "libprod",
2282 product_specific: true,
2283 shared_libs: [
2284 "libvendor",
2285 ],
2286 nocrt: true,
2287 }
2288 cc_library {
2289 name: "libvendor",
2290 vendor: true,
2291 nocrt: true,
2292 }
2293 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002294 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002295 cc_library {
2296 name: "libprod",
2297 product_specific: true,
2298 shared_libs: [
2299 "libsystem",
2300 ],
2301 nocrt: true,
2302 }
2303 cc_library {
2304 name: "libsystem",
2305 nocrt: true,
2306 }
2307 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002308 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002309 cc_library {
2310 name: "libprod",
2311 product_specific: true,
2312 shared_libs: [
2313 "libva",
2314 ],
2315 nocrt: true,
2316 }
2317 cc_library {
2318 name: "libva",
2319 vendor_available: true,
2320 nocrt: true,
2321 }
2322 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002323 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002324 cc_library {
2325 name: "libprod",
2326 product_specific: true,
2327 shared_libs: [
2328 "libvndk_private",
2329 ],
2330 nocrt: true,
2331 }
2332 cc_library {
2333 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002334 vendor_available: true,
2335 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002336 vndk: {
2337 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002338 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002339 },
2340 nocrt: true,
2341 }
2342 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002343 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002344 cc_library {
2345 name: "libprod",
2346 product_specific: true,
2347 shared_libs: [
2348 "libsystem_ext",
2349 ],
2350 nocrt: true,
2351 }
2352 cc_library {
2353 name: "libsystem_ext",
2354 system_ext_specific: true,
2355 nocrt: true,
2356 }
2357 `)
2358 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2359 cc_library {
2360 name: "libsystem",
2361 shared_libs: [
2362 "libproduct_va",
2363 ],
2364 nocrt: true,
2365 }
2366 cc_library {
2367 name: "libproduct_va",
2368 product_specific: true,
2369 vendor_available: true,
2370 nocrt: true,
2371 }
2372 `)
2373}
2374
Jooyung Han38002912019-05-16 04:01:54 +09002375func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002376 bp := `
2377 cc_library {
2378 name: "libvndk",
2379 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002380 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002381 vndk: {
2382 enabled: true,
2383 },
2384 }
2385 cc_library {
2386 name: "libvndksp",
2387 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002388 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002389 vndk: {
2390 enabled: true,
2391 support_system_process: true,
2392 },
2393 }
2394 cc_library {
2395 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002396 vendor_available: true,
2397 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002398 vndk: {
2399 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002400 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002401 },
2402 }
2403 cc_library {
2404 name: "libvendor",
2405 vendor: true,
2406 }
2407 cc_library {
2408 name: "libvndkext",
2409 vendor: true,
2410 vndk: {
2411 enabled: true,
2412 extends: "libvndk",
2413 },
2414 }
2415 vndk_prebuilt_shared {
2416 name: "prevndk",
2417 version: "27",
2418 target_arch: "arm",
2419 binder32bit: true,
2420 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002421 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002422 vndk: {
2423 enabled: true,
2424 },
2425 arch: {
2426 arm: {
2427 srcs: ["liba.so"],
2428 },
2429 },
2430 }
2431 cc_library {
2432 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002433 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002434 }
2435 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002436 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002437 symbol_file: "",
2438 }
2439 cc_library {
2440 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07002441 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002442 }
2443 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002444 name: "libllndkprivate.llndk",
Justin Yunc0d8c492021-01-07 17:45:31 +09002445 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002446 symbol_file: "",
Colin Cross78212242021-01-06 14:51:30 -08002447 }
2448
2449 llndk_libraries_txt {
2450 name: "llndk.libraries.txt",
2451 }
2452 vndkcore_libraries_txt {
2453 name: "vndkcore.libraries.txt",
2454 }
2455 vndksp_libraries_txt {
2456 name: "vndksp.libraries.txt",
2457 }
2458 vndkprivate_libraries_txt {
2459 name: "vndkprivate.libraries.txt",
2460 }
2461 vndkcorevariant_libraries_txt {
2462 name: "vndkcorevariant.libraries.txt",
2463 insert_vndk_version: false,
2464 }
2465 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002466
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002467 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002468 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002469 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002470 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002471 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002472
Colin Cross78212242021-01-06 14:51:30 -08002473 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2474 []string{"libvndk.so", "libvndkprivate.so"})
2475 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2476 []string{"libc++.so", "libvndksp.so"})
2477 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2478 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2479 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2480 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002481
Colin Crossfb0c16e2019-11-20 17:12:35 -08002482 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002483
Jooyung Han38002912019-05-16 04:01:54 +09002484 tests := []struct {
2485 variant string
2486 name string
2487 expected string
2488 }{
2489 {vendorVariant, "libvndk", "native:vndk"},
2490 {vendorVariant, "libvndksp", "native:vndk"},
2491 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2492 {vendorVariant, "libvendor", "native:vendor"},
2493 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002494 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002495 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002496 {coreVariant, "libvndk", "native:platform"},
2497 {coreVariant, "libvndkprivate", "native:platform"},
2498 {coreVariant, "libllndk", "native:platform"},
2499 }
2500 for _, test := range tests {
2501 t.Run(test.name, func(t *testing.T) {
2502 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2503 assertString(t, module.makeLinkType, test.expected)
2504 })
2505 }
2506}
2507
Jeff Gaston294356f2017-09-27 17:05:30 -07002508var staticLinkDepOrderTestCases = []struct {
2509 // This is a string representation of a map[moduleName][]moduleDependency .
2510 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002511 inStatic string
2512
2513 // This is a string representation of a map[moduleName][]moduleDependency .
2514 // It models the dependencies declared in an Android.bp file.
2515 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002516
2517 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2518 // The keys of allOrdered specify which modules we would like to check.
2519 // The values of allOrdered specify the expected result (of the transitive closure of all
2520 // dependencies) for each module to test
2521 allOrdered string
2522
2523 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2524 // The keys of outOrdered specify which modules we would like to check.
2525 // The values of outOrdered specify the expected result (of the ordered linker command line)
2526 // for each module to test.
2527 outOrdered string
2528}{
2529 // Simple tests
2530 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002531 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002532 outOrdered: "",
2533 },
2534 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002535 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002536 outOrdered: "a:",
2537 },
2538 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002539 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002540 outOrdered: "a:b; b:",
2541 },
2542 // Tests of reordering
2543 {
2544 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002545 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002546 outOrdered: "a:b,c,d; b:d; c:d; d:",
2547 },
2548 {
2549 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002550 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002551 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2552 },
2553 {
2554 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002555 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002556 outOrdered: "a:d,b,e,c; d:b; e:c",
2557 },
2558 {
2559 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002560 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002561 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2562 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2563 },
2564 {
2565 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002566 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 -07002567 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2568 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2569 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002570 // shared dependencies
2571 {
2572 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2573 // So, we don't actually have to check that a shared dependency of c will change the order
2574 // of a library that depends statically on b and on c. We only need to check that if c has
2575 // a shared dependency on b, that that shows up in allOrdered.
2576 inShared: "c:b",
2577 allOrdered: "c:b",
2578 outOrdered: "c:",
2579 },
2580 {
2581 // This test doesn't actually include any shared dependencies but it's a reminder of what
2582 // the second phase of the above test would look like
2583 inStatic: "a:b,c; c:b",
2584 allOrdered: "a:c,b; c:b",
2585 outOrdered: "a:c,b; c:b",
2586 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002587 // tiebreakers for when two modules specifying different orderings and there is no dependency
2588 // to dictate an order
2589 {
2590 // 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 -08002591 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002592 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2593 },
2594 {
2595 // 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 -08002596 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 -07002597 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2598 },
2599 // Tests involving duplicate dependencies
2600 {
2601 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002602 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002603 outOrdered: "a:c,b",
2604 },
2605 {
2606 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002607 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002608 outOrdered: "a:d,c,b",
2609 },
2610 // Tests to confirm the nonexistence of infinite loops.
2611 // These cases should never happen, so as long as the test terminates and the
2612 // result is deterministic then that should be fine.
2613 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002614 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002615 outOrdered: "a:a",
2616 },
2617 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002618 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002619 allOrdered: "a:b,c; b:c,a; c:a,b",
2620 outOrdered: "a:b; b:c; c:a",
2621 },
2622 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002623 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002624 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2625 outOrdered: "a:c,b; b:a,c; c:b,a",
2626 },
2627}
2628
2629// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2630func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2631 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2632 strippedText := strings.Replace(text, " ", "", -1)
2633 if len(strippedText) < 1 {
2634 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2635 }
2636 allDeps = make(map[android.Path][]android.Path, 0)
2637
2638 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2639 moduleTexts := strings.Split(strippedText, ";")
2640
2641 outputForModuleName := func(moduleName string) android.Path {
2642 return android.PathForTesting(moduleName)
2643 }
2644
2645 for _, moduleText := range moduleTexts {
2646 // convert from "a:b,c" to ["a", "b,c"]
2647 components := strings.Split(moduleText, ":")
2648 if len(components) != 2 {
2649 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2650 }
2651 moduleName := components[0]
2652 moduleOutput := outputForModuleName(moduleName)
2653 modulesInOrder = append(modulesInOrder, moduleOutput)
2654
2655 depString := components[1]
2656 // convert from "b,c" to ["b", "c"]
2657 depNames := strings.Split(depString, ",")
2658 if len(depString) < 1 {
2659 depNames = []string{}
2660 }
2661 var deps []android.Path
2662 for _, depName := range depNames {
2663 deps = append(deps, outputForModuleName(depName))
2664 }
2665 allDeps[moduleOutput] = deps
2666 }
2667 return modulesInOrder, allDeps
2668}
2669
Jeff Gaston294356f2017-09-27 17:05:30 -07002670func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2671 for _, moduleName := range moduleNames {
2672 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002673 output := module.outputFile.Path().RelativeToTop()
Jeff Gaston294356f2017-09-27 17:05:30 -07002674 paths = append(paths, output)
2675 }
2676 return paths
2677}
2678
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002679func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002680 ctx := testCc(t, `
2681 cc_library {
2682 name: "a",
2683 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002684 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002685 }
2686 cc_library {
2687 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002688 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002689 }
2690 cc_library {
2691 name: "c",
2692 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002693 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002694 }
2695 cc_library {
2696 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002697 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002698 }
2699
2700 `)
2701
Colin Cross7113d202019-11-20 16:39:12 -08002702 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002703 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002704 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2705 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002706 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002707
2708 if !reflect.DeepEqual(actual, expected) {
2709 t.Errorf("staticDeps orderings were not propagated correctly"+
2710 "\nactual: %v"+
2711 "\nexpected: %v",
2712 actual,
2713 expected,
2714 )
2715 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002716}
Jeff Gaston294356f2017-09-27 17:05:30 -07002717
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002718func TestStaticLibDepReorderingWithShared(t *testing.T) {
2719 ctx := testCc(t, `
2720 cc_library {
2721 name: "a",
2722 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002723 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002724 }
2725 cc_library {
2726 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002727 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002728 }
2729 cc_library {
2730 name: "c",
2731 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002732 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002733 }
2734
2735 `)
2736
Colin Cross7113d202019-11-20 16:39:12 -08002737 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002738 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002739 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2740 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002741 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002742
2743 if !reflect.DeepEqual(actual, expected) {
2744 t.Errorf("staticDeps orderings did not account for shared libs"+
2745 "\nactual: %v"+
2746 "\nexpected: %v",
2747 actual,
2748 expected,
2749 )
2750 }
2751}
2752
Jooyung Hanb04a4992020-03-13 18:57:35 +09002753func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002754 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002755 if !reflect.DeepEqual(actual, expected) {
2756 t.Errorf(message+
2757 "\nactual: %v"+
2758 "\nexpected: %v",
2759 actual,
2760 expected,
2761 )
2762 }
2763}
2764
Jooyung Han61b66e92020-03-21 14:21:46 +00002765func TestLlndkLibrary(t *testing.T) {
2766 ctx := testCc(t, `
2767 cc_library {
2768 name: "libllndk",
2769 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07002770 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002771 }
2772 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002773 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002774 }
Colin Cross127bb8b2020-12-16 16:46:01 -08002775
2776 cc_prebuilt_library_shared {
2777 name: "libllndkprebuilt",
2778 stubs: { versions: ["1", "2"] },
2779 llndk_stubs: "libllndkprebuilt.llndk",
2780 }
2781 llndk_library {
2782 name: "libllndkprebuilt.llndk",
2783 }
2784
2785 cc_library {
2786 name: "libllndk_with_external_headers",
2787 stubs: { versions: ["1", "2"] },
2788 llndk_stubs: "libllndk_with_external_headers.llndk",
2789 header_libs: ["libexternal_headers"],
2790 export_header_lib_headers: ["libexternal_headers"],
2791 }
2792 llndk_library {
2793 name: "libllndk_with_external_headers.llndk",
2794 }
2795 cc_library_headers {
2796 name: "libexternal_headers",
2797 export_include_dirs: ["include"],
2798 vendor_available: true,
2799 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002800 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08002801 actual := ctx.ModuleVariantsForTests("libllndk")
2802 for i := 0; i < len(actual); i++ {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002803 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
Colin Cross127bb8b2020-12-16 16:46:01 -08002804 actual = append(actual[:i], actual[i+1:]...)
2805 i--
2806 }
2807 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002808 expected := []string{
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002809 "android_vendor.29_arm64_armv8-a_shared_1",
2810 "android_vendor.29_arm64_armv8-a_shared_2",
2811 "android_vendor.29_arm64_armv8-a_shared_current",
2812 "android_vendor.29_arm64_armv8-a_shared",
2813 "android_vendor.29_arm_armv7-a-neon_shared_1",
2814 "android_vendor.29_arm_armv7-a-neon_shared_2",
2815 "android_vendor.29_arm_armv7-a-neon_shared_current",
2816 "android_vendor.29_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002817 }
2818 checkEquals(t, "variants for llndk stubs", expected, actual)
2819
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002820 params := ctx.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002821 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2822
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002823 params = ctx.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002824 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2825}
2826
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002827func TestEmbeddedLlndkLibrary(t *testing.T) {
2828 result := prepareForCcTest.RunTestWithBp(t, `
2829 cc_library {
2830 name: "libllndk",
2831 stubs: { versions: ["1", "2"] },
2832 llndk: {
2833 symbol_file: "libllndk.map.txt",
2834 },
2835 export_include_dirs: ["include"],
2836 }
2837
2838 cc_prebuilt_library_shared {
2839 name: "libllndkprebuilt",
2840 stubs: { versions: ["1", "2"] },
2841 llndk: {
2842 symbol_file: "libllndkprebuilt.map.txt",
2843 },
2844 }
2845
2846 cc_library {
2847 name: "libllndk_with_external_headers",
2848 stubs: { versions: ["1", "2"] },
2849 llndk: {
2850 symbol_file: "libllndk.map.txt",
2851 export_llndk_headers: ["libexternal_llndk_headers"],
2852 },
2853 header_libs: ["libexternal_headers"],
2854 export_header_lib_headers: ["libexternal_headers"],
2855 }
2856 cc_library_headers {
2857 name: "libexternal_headers",
2858 export_include_dirs: ["include"],
2859 vendor_available: true,
2860 }
2861 cc_library_headers {
2862 name: "libexternal_llndk_headers",
2863 export_include_dirs: ["include_llndk"],
2864 llndk: {
2865 symbol_file: "libllndk.map.txt",
2866 },
2867 vendor_available: true,
2868 }
2869
2870 cc_library {
2871 name: "libllndk_with_override_headers",
2872 stubs: { versions: ["1", "2"] },
2873 llndk: {
2874 symbol_file: "libllndk.map.txt",
2875 override_export_include_dirs: ["include_llndk"],
2876 },
2877 export_include_dirs: ["include"],
2878 }
2879 `)
2880 actual := result.ModuleVariantsForTests("libllndk")
2881 for i := 0; i < len(actual); i++ {
2882 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2883 actual = append(actual[:i], actual[i+1:]...)
2884 i--
2885 }
2886 }
2887 expected := []string{
2888 "android_vendor.29_arm64_armv8-a_shared_1",
2889 "android_vendor.29_arm64_armv8-a_shared_2",
2890 "android_vendor.29_arm64_armv8-a_shared_current",
2891 "android_vendor.29_arm64_armv8-a_shared",
2892 "android_vendor.29_arm_armv7-a-neon_shared_1",
2893 "android_vendor.29_arm_armv7-a-neon_shared_2",
2894 "android_vendor.29_arm_armv7-a-neon_shared_current",
2895 "android_vendor.29_arm_armv7-a-neon_shared",
2896 }
2897 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2898
2899 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2900 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2901
2902 params = result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared_1").Description("generate stub")
2903 android.AssertSame(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2904
2905 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2906 t.Helper()
2907 m := result.ModuleForTests(module, variant).Module()
2908 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2909 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2910 expectedDirs, f.IncludeDirs)
2911 }
2912
2913 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2914 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2915 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2916 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2917 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2918 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2919}
2920
Jiyong Parka46a4d52017-12-14 19:54:34 +09002921func TestLlndkHeaders(t *testing.T) {
2922 ctx := testCc(t, `
2923 llndk_headers {
2924 name: "libllndk_headers",
2925 export_include_dirs: ["my_include"],
2926 }
2927 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002928 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09002929 export_llndk_headers: ["libllndk_headers"],
2930 }
2931 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002932 name: "libllndk",
2933 llndk_stubs: "libllndk.llndk",
2934 }
2935
2936 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002937 name: "libvendor",
2938 shared_libs: ["libllndk"],
2939 vendor: true,
2940 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002941 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002942 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002943 }
2944 `)
2945
2946 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002947 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002948 cflags := cc.Args["cFlags"]
2949 if !strings.Contains(cflags, "-Imy_include") {
2950 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2951 }
2952}
2953
Logan Chien43d34c32017-12-20 01:17:32 +08002954func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2955 actual := module.Properties.AndroidMkRuntimeLibs
2956 if !reflect.DeepEqual(actual, expected) {
2957 t.Errorf("incorrect runtime_libs for shared libs"+
2958 "\nactual: %v"+
2959 "\nexpected: %v",
2960 actual,
2961 expected,
2962 )
2963 }
2964}
2965
2966const runtimeLibAndroidBp = `
2967 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002968 name: "liball_available",
2969 vendor_available: true,
2970 product_available: true,
2971 no_libcrt : true,
2972 nocrt : true,
2973 system_shared_libs : [],
2974 }
2975 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002976 name: "libvendor_available1",
2977 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002978 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002979 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002980 nocrt : true,
2981 system_shared_libs : [],
2982 }
2983 cc_library {
2984 name: "libvendor_available2",
2985 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002986 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002987 target: {
2988 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002989 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002990 }
2991 },
Yi Konge7fe9912019-06-02 00:53:50 -07002992 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002993 nocrt : true,
2994 system_shared_libs : [],
2995 }
2996 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002997 name: "libproduct_vendor",
2998 product_specific: true,
2999 vendor_available: true,
3000 no_libcrt : true,
3001 nocrt : true,
3002 system_shared_libs : [],
3003 }
3004 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08003005 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09003006 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07003007 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003008 nocrt : true,
3009 system_shared_libs : [],
3010 }
3011 cc_library {
3012 name: "libvendor1",
3013 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003014 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003015 nocrt : true,
3016 system_shared_libs : [],
3017 }
3018 cc_library {
3019 name: "libvendor2",
3020 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09003021 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09003022 no_libcrt : true,
3023 nocrt : true,
3024 system_shared_libs : [],
3025 }
3026 cc_library {
3027 name: "libproduct_available1",
3028 product_available: true,
3029 runtime_libs: ["liball_available"],
3030 no_libcrt : true,
3031 nocrt : true,
3032 system_shared_libs : [],
3033 }
3034 cc_library {
3035 name: "libproduct1",
3036 product_specific: true,
3037 no_libcrt : true,
3038 nocrt : true,
3039 system_shared_libs : [],
3040 }
3041 cc_library {
3042 name: "libproduct2",
3043 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09003044 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07003045 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003046 nocrt : true,
3047 system_shared_libs : [],
3048 }
3049`
3050
3051func TestRuntimeLibs(t *testing.T) {
3052 ctx := testCc(t, runtimeLibAndroidBp)
3053
3054 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08003055 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003056
Justin Yun8a2600c2020-12-07 12:44:03 +09003057 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3058 checkRuntimeLibs(t, []string{"liball_available"}, module)
3059
3060 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3061 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003062
3063 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003064 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003065
3066 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3067 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003068 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003069
Justin Yun8a2600c2020-12-07 12:44:03 +09003070 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3071 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003072
3073 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003074 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003075
3076 // runtime_libs for product variants have '.product' suffixes if the modules have both core
3077 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003078 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003079
3080 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3081 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
3082
3083 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09003084 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003085}
3086
3087func TestExcludeRuntimeLibs(t *testing.T) {
3088 ctx := testCc(t, runtimeLibAndroidBp)
3089
Colin Cross7113d202019-11-20 16:39:12 -08003090 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003091 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3092 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003093
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003094 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003095 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08003096 checkRuntimeLibs(t, nil, module)
3097}
3098
3099func TestRuntimeLibsNoVndk(t *testing.T) {
3100 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3101
3102 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3103
Colin Cross7113d202019-11-20 16:39:12 -08003104 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003105
Justin Yun8a2600c2020-12-07 12:44:03 +09003106 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3107 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003108
3109 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003110 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003111
3112 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003113 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003114}
3115
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003116func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003117 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003118 actual := module.Properties.AndroidMkStaticLibs
3119 if !reflect.DeepEqual(actual, expected) {
3120 t.Errorf("incorrect static_libs"+
3121 "\nactual: %v"+
3122 "\nexpected: %v",
3123 actual,
3124 expected,
3125 )
3126 }
3127}
3128
3129const staticLibAndroidBp = `
3130 cc_library {
3131 name: "lib1",
3132 }
3133 cc_library {
3134 name: "lib2",
3135 static_libs: ["lib1"],
3136 }
3137`
3138
3139func TestStaticLibDepExport(t *testing.T) {
3140 ctx := testCc(t, staticLibAndroidBp)
3141
3142 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003143 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003144 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Ryan Prichardc2018e22021-04-02 20:23:22 -07003145 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003146
3147 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003148 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003149 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3150 // libc++_static is linked additionally.
Ryan Prichardc2018e22021-04-02 20:23:22 -07003151 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003152}
3153
Jiyong Parkd08b6972017-09-26 10:50:54 +09003154var compilerFlagsTestCases = []struct {
3155 in string
3156 out bool
3157}{
3158 {
3159 in: "a",
3160 out: false,
3161 },
3162 {
3163 in: "-a",
3164 out: true,
3165 },
3166 {
3167 in: "-Ipath/to/something",
3168 out: false,
3169 },
3170 {
3171 in: "-isystempath/to/something",
3172 out: false,
3173 },
3174 {
3175 in: "--coverage",
3176 out: false,
3177 },
3178 {
3179 in: "-include a/b",
3180 out: true,
3181 },
3182 {
3183 in: "-include a/b c/d",
3184 out: false,
3185 },
3186 {
3187 in: "-DMACRO",
3188 out: true,
3189 },
3190 {
3191 in: "-DMAC RO",
3192 out: false,
3193 },
3194 {
3195 in: "-a -b",
3196 out: false,
3197 },
3198 {
3199 in: "-DMACRO=definition",
3200 out: true,
3201 },
3202 {
3203 in: "-DMACRO=defi nition",
3204 out: true, // TODO(jiyong): this should be false
3205 },
3206 {
3207 in: "-DMACRO(x)=x + 1",
3208 out: true,
3209 },
3210 {
3211 in: "-DMACRO=\"defi nition\"",
3212 out: true,
3213 },
3214}
3215
3216type mockContext struct {
3217 BaseModuleContext
3218 result bool
3219}
3220
3221func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3222 // CheckBadCompilerFlags calls this function when the flag should be rejected
3223 ctx.result = false
3224}
3225
3226func TestCompilerFlags(t *testing.T) {
3227 for _, testCase := range compilerFlagsTestCases {
3228 ctx := &mockContext{result: true}
3229 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3230 if ctx.result != testCase.out {
3231 t.Errorf("incorrect output:")
3232 t.Errorf(" input: %#v", testCase.in)
3233 t.Errorf(" expected: %#v", testCase.out)
3234 t.Errorf(" got: %#v", ctx.result)
3235 }
3236 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003237}
Jiyong Park374510b2018-03-19 18:23:01 +09003238
Jiyong Park37b25202018-07-11 10:49:27 +09003239func TestRecovery(t *testing.T) {
3240 ctx := testCc(t, `
3241 cc_library_shared {
3242 name: "librecovery",
3243 recovery: true,
3244 }
3245 cc_library_shared {
3246 name: "librecovery32",
3247 recovery: true,
3248 compile_multilib:"32",
3249 }
Jiyong Park5baac542018-08-28 09:55:37 +09003250 cc_library_shared {
3251 name: "libHalInRecovery",
3252 recovery_available: true,
3253 vendor: true,
3254 }
Jiyong Park37b25202018-07-11 10:49:27 +09003255 `)
3256
3257 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003258 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003259 if len(variants) != 1 || !android.InList(arm64, variants) {
3260 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3261 }
3262
3263 variants = ctx.ModuleVariantsForTests("librecovery32")
3264 if android.InList(arm64, variants) {
3265 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3266 }
Jiyong Park5baac542018-08-28 09:55:37 +09003267
3268 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3269 if !recoveryModule.Platform() {
3270 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3271 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003272}
Jiyong Park5baac542018-08-28 09:55:37 +09003273
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003274func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3275 bp := `
3276 cc_prebuilt_test_library_shared {
3277 name: "test_lib",
3278 relative_install_path: "foo/bar/baz",
3279 srcs: ["srcpath/dontusethispath/baz.so"],
3280 }
3281
3282 cc_test {
3283 name: "main_test",
3284 data_libs: ["test_lib"],
3285 gtest: false,
3286 }
3287 `
3288
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003289 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003290 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003291 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003292 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3293
3294 ctx := testCcWithConfig(t, config)
3295 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3296 testBinary := module.(*Module).linker.(*testBinary)
3297 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3298 if err != nil {
3299 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3300 }
3301 if len(outputFiles) != 1 {
3302 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3303 }
3304 if len(testBinary.dataPaths()) != 1 {
3305 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3306 }
3307
3308 outputPath := outputFiles[0].String()
3309
3310 if !strings.HasSuffix(outputPath, "/main_test") {
3311 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3312 }
Colin Crossaa255532020-07-03 13:18:24 -07003313 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003314 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3315 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3316 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3317 }
3318}
3319
Jiyong Park7ed9de32018-10-15 22:25:07 +09003320func TestVersionedStubs(t *testing.T) {
3321 ctx := testCc(t, `
3322 cc_library_shared {
3323 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003324 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003325 stubs: {
3326 symbol_file: "foo.map.txt",
3327 versions: ["1", "2", "3"],
3328 },
3329 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003330
Jiyong Park7ed9de32018-10-15 22:25:07 +09003331 cc_library_shared {
3332 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003333 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003334 shared_libs: ["libFoo#1"],
3335 }`)
3336
3337 variants := ctx.ModuleVariantsForTests("libFoo")
3338 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003339 "android_arm64_armv8-a_shared",
3340 "android_arm64_armv8-a_shared_1",
3341 "android_arm64_armv8-a_shared_2",
3342 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003343 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003344 "android_arm_armv7-a-neon_shared",
3345 "android_arm_armv7-a-neon_shared_1",
3346 "android_arm_armv7-a-neon_shared_2",
3347 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003348 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003349 }
3350 variantsMismatch := false
3351 if len(variants) != len(expectedVariants) {
3352 variantsMismatch = true
3353 } else {
3354 for _, v := range expectedVariants {
3355 if !inList(v, variants) {
3356 variantsMismatch = false
3357 }
3358 }
3359 }
3360 if variantsMismatch {
3361 t.Errorf("variants of libFoo expected:\n")
3362 for _, v := range expectedVariants {
3363 t.Errorf("%q\n", v)
3364 }
3365 t.Errorf(", but got:\n")
3366 for _, v := range variants {
3367 t.Errorf("%q\n", v)
3368 }
3369 }
3370
Colin Cross7113d202019-11-20 16:39:12 -08003371 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003372 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003373 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003374 if !strings.Contains(libFlags, libFoo1StubPath) {
3375 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3376 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003377
Colin Cross7113d202019-11-20 16:39:12 -08003378 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003379 cFlags := libBarCompileRule.Args["cFlags"]
3380 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3381 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3382 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3383 }
Jiyong Park37b25202018-07-11 10:49:27 +09003384}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003385
Jooyung Hanb04a4992020-03-13 18:57:35 +09003386func TestVersioningMacro(t *testing.T) {
3387 for _, tc := range []struct{ moduleName, expected string }{
3388 {"libc", "__LIBC_API__"},
3389 {"libfoo", "__LIBFOO_API__"},
3390 {"libfoo@1", "__LIBFOO_1_API__"},
3391 {"libfoo-v1", "__LIBFOO_V1_API__"},
3392 {"libfoo.v1", "__LIBFOO_V1_API__"},
3393 } {
3394 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3395 }
3396}
3397
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003398func TestStaticExecutable(t *testing.T) {
3399 ctx := testCc(t, `
3400 cc_binary {
3401 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003402 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003403 static_executable: true,
3404 }`)
3405
Colin Cross7113d202019-11-20 16:39:12 -08003406 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003407 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3408 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003409 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003410 for _, lib := range systemStaticLibs {
3411 if !strings.Contains(libFlags, lib) {
3412 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3413 }
3414 }
3415 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3416 for _, lib := range systemSharedLibs {
3417 if strings.Contains(libFlags, lib) {
3418 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3419 }
3420 }
3421}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003422
3423func TestStaticDepsOrderWithStubs(t *testing.T) {
3424 ctx := testCc(t, `
3425 cc_binary {
3426 name: "mybin",
3427 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003428 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003429 static_executable: true,
3430 stl: "none",
3431 }
3432
3433 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003434 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003435 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003436 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003437 stl: "none",
3438 }
3439
3440 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003441 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003442 srcs: ["foo.c"],
3443 stl: "none",
3444 stubs: {
3445 versions: ["1"],
3446 },
3447 }`)
3448
Colin Cross0de8a1e2020-09-18 14:15:30 -07003449 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3450 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003451 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003452
3453 if !reflect.DeepEqual(actual, expected) {
3454 t.Errorf("staticDeps orderings were not propagated correctly"+
3455 "\nactual: %v"+
3456 "\nexpected: %v",
3457 actual,
3458 expected,
3459 )
3460 }
3461}
Jooyung Han38002912019-05-16 04:01:54 +09003462
Jooyung Hand48f3c32019-08-23 11:18:57 +09003463func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3464 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3465 cc_library {
3466 name: "libA",
3467 srcs: ["foo.c"],
3468 shared_libs: ["libB"],
3469 stl: "none",
3470 }
3471
3472 cc_library {
3473 name: "libB",
3474 srcs: ["foo.c"],
3475 enabled: false,
3476 stl: "none",
3477 }
3478 `)
3479}
3480
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003481// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3482// correctly.
3483func TestFuzzTarget(t *testing.T) {
3484 ctx := testCc(t, `
3485 cc_fuzz {
3486 name: "fuzz_smoke_test",
3487 srcs: ["foo.c"],
3488 }`)
3489
Paul Duffin075c4172019-12-19 19:06:13 +00003490 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003491 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3492}
3493
Jiyong Park29074592019-07-07 16:27:47 +09003494func TestAidl(t *testing.T) {
3495}
3496
Jooyung Han38002912019-05-16 04:01:54 +09003497func assertString(t *testing.T, got, expected string) {
3498 t.Helper()
3499 if got != expected {
3500 t.Errorf("expected %q got %q", expected, got)
3501 }
3502}
3503
3504func assertArrayString(t *testing.T, got, expected []string) {
3505 t.Helper()
3506 if len(got) != len(expected) {
3507 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3508 return
3509 }
3510 for i := range got {
3511 if got[i] != expected[i] {
3512 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3513 i, expected[i], expected, got[i], got)
3514 return
3515 }
3516 }
3517}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003518
Jooyung Han0302a842019-10-30 18:43:49 +09003519func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3520 t.Helper()
3521 assertArrayString(t, android.SortedStringKeys(m), expected)
3522}
3523
Colin Crosse1bb5d02019-09-24 14:55:04 -07003524func TestDefaults(t *testing.T) {
3525 ctx := testCc(t, `
3526 cc_defaults {
3527 name: "defaults",
3528 srcs: ["foo.c"],
3529 static: {
3530 srcs: ["bar.c"],
3531 },
3532 shared: {
3533 srcs: ["baz.c"],
3534 },
Liz Kammer3cf52112021-03-31 15:42:03 -04003535 bazel_module: {
3536 bp2build_available: true,
3537 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07003538 }
3539
3540 cc_library_static {
3541 name: "libstatic",
3542 defaults: ["defaults"],
3543 }
3544
3545 cc_library_shared {
3546 name: "libshared",
3547 defaults: ["defaults"],
3548 }
3549
3550 cc_library {
3551 name: "libboth",
3552 defaults: ["defaults"],
3553 }
3554
3555 cc_binary {
3556 name: "binary",
3557 defaults: ["defaults"],
3558 }`)
3559
3560 pathsToBase := func(paths android.Paths) []string {
3561 var ret []string
3562 for _, p := range paths {
3563 ret = append(ret, p.Base())
3564 }
3565 return ret
3566 }
3567
Colin Cross7113d202019-11-20 16:39:12 -08003568 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003569 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3570 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3571 }
Colin Cross7113d202019-11-20 16:39:12 -08003572 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003573 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3574 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3575 }
Colin Cross7113d202019-11-20 16:39:12 -08003576 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003577 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3578 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3579 }
3580
Colin Cross7113d202019-11-20 16:39:12 -08003581 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003582 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3583 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3584 }
Colin Cross7113d202019-11-20 16:39:12 -08003585 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003586 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3587 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3588 }
3589}
Colin Crosseabaedd2020-02-06 17:01:55 -08003590
3591func TestProductVariableDefaults(t *testing.T) {
3592 bp := `
3593 cc_defaults {
3594 name: "libfoo_defaults",
3595 srcs: ["foo.c"],
3596 cppflags: ["-DFOO"],
3597 product_variables: {
3598 debuggable: {
3599 cppflags: ["-DBAR"],
3600 },
3601 },
3602 }
3603
3604 cc_library {
3605 name: "libfoo",
3606 defaults: ["libfoo_defaults"],
3607 }
3608 `
3609
Paul Duffin8567f222021-03-23 00:02:06 +00003610 result := android.GroupFixturePreparers(
3611 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003612 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003613
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003614 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3615 variables.Debuggable = BoolPtr(true)
3616 }),
3617 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003618
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003619 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003620 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003621}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003622
3623func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3624 t.Parallel()
3625 bp := `
3626 cc_library_static {
3627 name: "libfoo",
3628 srcs: ["foo.c"],
3629 whole_static_libs: ["libbar"],
3630 }
3631
3632 cc_library_static {
3633 name: "libbar",
3634 whole_static_libs: ["libmissing"],
3635 }
3636 `
3637
Paul Duffin8567f222021-03-23 00:02:06 +00003638 result := android.GroupFixturePreparers(
3639 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003640 android.PrepareForTestWithAllowMissingDependencies,
3641 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003642
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003643 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003644 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003645
Paul Duffine84b1332021-03-12 11:59:43 +00003646 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003647
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003648 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003649 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003650}
Colin Crosse9fe2942020-11-10 18:12:15 -08003651
3652func TestInstallSharedLibs(t *testing.T) {
3653 bp := `
3654 cc_binary {
3655 name: "bin",
3656 host_supported: true,
3657 shared_libs: ["libshared"],
3658 runtime_libs: ["libruntime"],
3659 srcs: [":gen"],
3660 }
3661
3662 cc_library_shared {
3663 name: "libshared",
3664 host_supported: true,
3665 shared_libs: ["libtransitive"],
3666 }
3667
3668 cc_library_shared {
3669 name: "libtransitive",
3670 host_supported: true,
3671 }
3672
3673 cc_library_shared {
3674 name: "libruntime",
3675 host_supported: true,
3676 }
3677
3678 cc_binary_host {
3679 name: "tool",
3680 srcs: ["foo.cpp"],
3681 }
3682
3683 genrule {
3684 name: "gen",
3685 tools: ["tool"],
3686 out: ["gen.cpp"],
3687 cmd: "$(location tool) $(out)",
3688 }
3689 `
3690
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003691 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003692 ctx := testCcWithConfig(t, config)
3693
3694 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3695 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3696 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3697 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3698 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3699
3700 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3701 t.Errorf("expected host bin dependency %q, got %q", w, g)
3702 }
3703
3704 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3705 t.Errorf("expected host bin dependency %q, got %q", w, g)
3706 }
3707
3708 if g, w := hostShared.Implicits.Strings(), hostTransitive.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(), hostRuntime.Output.String(); !android.InList(w, g) {
3713 t.Errorf("expected host bin dependency %q, got %q", w, g)
3714 }
3715
3716 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3717 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3718 }
3719
3720 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3721 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3722 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3723 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3724
3725 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3726 t.Errorf("expected device bin dependency %q, got %q", w, g)
3727 }
3728
3729 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3730 t.Errorf("expected device bin dependency %q, got %q", w, g)
3731 }
3732
3733 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.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(), deviceRuntime.Output.String(); !android.InList(w, g) {
3738 t.Errorf("expected device bin dependency %q, got %q", w, g)
3739 }
3740
3741 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3742 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3743 }
3744
3745}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003746
3747func TestStubsLibReexportsHeaders(t *testing.T) {
3748 ctx := testCc(t, `
3749 cc_library_shared {
3750 name: "libclient",
3751 srcs: ["foo.c"],
3752 shared_libs: ["libfoo#1"],
3753 }
3754
3755 cc_library_shared {
3756 name: "libfoo",
3757 srcs: ["foo.c"],
3758 shared_libs: ["libbar"],
3759 export_shared_lib_headers: ["libbar"],
3760 stubs: {
3761 symbol_file: "foo.map.txt",
3762 versions: ["1", "2", "3"],
3763 },
3764 }
3765
3766 cc_library_shared {
3767 name: "libbar",
3768 export_include_dirs: ["include/libbar"],
3769 srcs: ["foo.c"],
3770 }`)
3771
3772 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3773
3774 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3775 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3776 }
3777}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003778
3779func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3780 ctx := testCc(t, `
3781 cc_library {
3782 name: "libfoo",
3783 srcs: ["a/Foo.aidl"],
3784 aidl: { flags: ["-Werror"], },
3785 }
3786 `)
3787
3788 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3789 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3790 aidlCommand := manifest.Commands[0].GetCommand()
3791 expectedAidlFlag := "-Werror"
3792 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3793 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3794 }
3795}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003796
Jiyong Parka008fb02021-03-16 17:15:53 +09003797func TestMinSdkVersionInClangTriple(t *testing.T) {
3798 ctx := testCc(t, `
3799 cc_library_shared {
3800 name: "libfoo",
3801 srcs: ["foo.c"],
3802 min_sdk_version: "29",
3803 }`)
3804
3805 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3806 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3807}
3808
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003809type MemtagNoteType int
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003810
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003811const (
3812 None MemtagNoteType = iota + 1
3813 Sync
3814 Async
3815)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003816
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003817func (t MemtagNoteType) str() string {
3818 switch t {
3819 case None:
3820 return "none"
3821 case Sync:
3822 return "sync"
3823 case Async:
3824 return "async"
3825 default:
3826 panic("invalid note type")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003827 }
3828}
3829
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003830func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
3831 note_async := "note_memtag_heap_async"
3832 note_sync := "note_memtag_heap_sync"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003833
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003834 found := None
3835 implicits := m.Rule("ld").Implicits
3836 for _, lib := range implicits {
3837 if strings.Contains(lib.Rel(), note_async) {
3838 found = Async
3839 break
3840 } else if strings.Contains(lib.Rel(), note_sync) {
3841 found = Sync
3842 break
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003843 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003844 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003845
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003846 if found != expected {
3847 t.Errorf("Wrong Memtag note in target %q: found %q, expected %q", m.Module().(*Module).Name(), found.str(), expected.str())
3848 }
3849}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003850
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003851var prepareForTestWithMemtagHeap = android.GroupFixturePreparers(
3852 android.FixtureModifyMockFS(func(fs android.MockFS) {
3853 templateBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003854 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003855 name: "%[1]s_test",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003856 gtest: false,
3857 }
3858
3859 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003860 name: "%[1]s_test_false",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003861 gtest: false,
3862 sanitize: { memtag_heap: false },
3863 }
3864
3865 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003866 name: "%[1]s_test_true",
3867 gtest: false,
3868 sanitize: { memtag_heap: true },
3869 }
3870
3871 cc_test {
3872 name: "%[1]s_test_true_nodiag",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003873 gtest: false,
3874 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3875 }
3876
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003877 cc_test {
3878 name: "%[1]s_test_true_diag",
3879 gtest: false,
3880 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
3881 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003882
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003883 cc_binary {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003884 name: "%[1]s_binary",
3885 }
3886
3887 cc_binary {
3888 name: "%[1]s_binary_false",
3889 sanitize: { memtag_heap: false },
3890 }
3891
3892 cc_binary {
3893 name: "%[1]s_binary_true",
3894 sanitize: { memtag_heap: true },
3895 }
3896
3897 cc_binary {
3898 name: "%[1]s_binary_true_nodiag",
3899 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3900 }
3901
3902 cc_binary {
3903 name: "%[1]s_binary_true_diag",
3904 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003905 }
3906 `
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003907 subdirDefaultBp := fmt.Sprintf(templateBp, "default")
3908 subdirExcludeBp := fmt.Sprintf(templateBp, "exclude")
3909 subdirSyncBp := fmt.Sprintf(templateBp, "sync")
3910 subdirAsyncBp := fmt.Sprintf(templateBp, "async")
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003911
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003912 fs.Merge(android.MockFS{
3913 "subdir_default/Android.bp": []byte(subdirDefaultBp),
3914 "subdir_exclude/Android.bp": []byte(subdirExcludeBp),
3915 "subdir_sync/Android.bp": []byte(subdirSyncBp),
3916 "subdir_async/Android.bp": []byte(subdirAsyncBp),
3917 })
3918 }),
3919 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3920 variables.MemtagHeapExcludePaths = []string{"subdir_exclude"}
Evgenii Stepanov779b64e2021-04-09 14:33:10 -07003921 // "subdir_exclude" is covered by both include and exclude paths. Exclude wins.
3922 variables.MemtagHeapSyncIncludePaths = []string{"subdir_sync", "subdir_exclude"}
3923 variables.MemtagHeapAsyncIncludePaths = []string{"subdir_async", "subdir_exclude"}
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003924 }),
3925)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003926
3927func TestSanitizeMemtagHeap(t *testing.T) {
3928 variant := "android_arm64_armv8-a"
3929
Paul Duffin8567f222021-03-23 00:02:06 +00003930 result := android.GroupFixturePreparers(
3931 prepareForCcTest,
3932 prepareForTestWithMemtagHeap,
3933 ).RunTest(t)
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003934 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003935
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003936 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3937 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3938 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3939 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3940 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3941
3942 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), None)
3943 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3944 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3945 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3946 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3947
3948 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3949 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3950 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3951 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3952 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3953
3954 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3955 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3956 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3957 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3958 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3959
3960 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3961 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3962 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3963 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3964 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3965
3966 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3967 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3968 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3969 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3970 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3971
3972 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3973 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3974 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3975 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3976 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3977
3978 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3979 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3980 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3981 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3982 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3983}
3984
3985func TestSanitizeMemtagHeapWithSanitizeDevice(t *testing.T) {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003986 variant := "android_arm64_armv8-a"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003987
Paul Duffin8567f222021-03-23 00:02:06 +00003988 result := android.GroupFixturePreparers(
3989 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003990 prepareForTestWithMemtagHeap,
3991 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3992 variables.SanitizeDevice = []string{"memtag_heap"}
3993 }),
3994 ).RunTest(t)
3995 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003996
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003997 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3998 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3999 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
4000 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
4001 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08004002
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08004003 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Async)
4004 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
4005 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
4006 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
4007 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
4008
4009 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
4010 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
4011 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
4012 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
4013 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
4014
4015 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
4016 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
4017 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
4018 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
4019 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
4020
4021 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
4022 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
4023 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
4024 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
4025 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
4026
4027 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
4028 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
4029 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
4030 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
4031 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
4032
4033 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
4034 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
4035 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
4036 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
4037 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
4038
4039 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
4040 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
4041 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
4042 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
4043 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
4044}
4045
4046func TestSanitizeMemtagHeapWithSanitizeDeviceDiag(t *testing.T) {
4047 variant := "android_arm64_armv8-a"
4048
Paul Duffin8567f222021-03-23 00:02:06 +00004049 result := android.GroupFixturePreparers(
4050 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004051 prepareForTestWithMemtagHeap,
4052 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4053 variables.SanitizeDevice = []string{"memtag_heap"}
4054 variables.SanitizeDeviceDiag = []string{"memtag_heap"}
4055 }),
4056 ).RunTest(t)
4057 ctx := result.TestContext
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08004058
4059 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
4060 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
4061 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Sync)
4062 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
4063 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
4064
4065 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Sync)
4066 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
4067 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Sync)
4068 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
4069 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
4070
4071 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
4072 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
4073 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Sync)
4074 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
4075 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
4076
4077 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
4078 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
4079 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Sync)
4080 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
4081 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
4082
4083 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
4084 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
4085 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Sync)
4086 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
4087 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
4088
4089 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Sync)
4090 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
4091 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Sync)
4092 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
4093 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
4094
4095 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
4096 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
4097 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
4098 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
4099 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
4100
4101 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
4102 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
4103 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
4104 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
4105 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004106}
Paul Duffin3cb603e2021-02-19 13:57:10 +00004107
4108func TestIncludeDirsExporting(t *testing.T) {
4109
4110 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4111 // embedded newline characters alone.
4112 trimIndentingSpaces := func(s string) string {
4113 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4114 }
4115
4116 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4117 t.Helper()
4118 expected = trimIndentingSpaces(expected)
4119 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4120 if expected != actual {
4121 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4122 }
4123 }
4124
4125 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4126
4127 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4128 t.Helper()
4129 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4130 name := module.Name()
4131
4132 for _, checker := range checkers {
4133 checker(t, name, exported)
4134 }
4135 }
4136
4137 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4138 return func(t *testing.T, name string, exported FlagExporterInfo) {
4139 t.Helper()
4140 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4141 }
4142 }
4143
4144 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4145 return func(t *testing.T, name string, exported FlagExporterInfo) {
4146 t.Helper()
4147 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4148 }
4149 }
4150
4151 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4152 return func(t *testing.T, name string, exported FlagExporterInfo) {
4153 t.Helper()
4154 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4155 }
4156 }
4157
4158 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4159 return func(t *testing.T, name string, exported FlagExporterInfo) {
4160 t.Helper()
4161 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4162 }
4163 }
4164
4165 genRuleModules := `
4166 genrule {
4167 name: "genrule_foo",
4168 cmd: "generate-foo",
4169 out: [
4170 "generated_headers/foo/generated_header.h",
4171 ],
4172 export_include_dirs: [
4173 "generated_headers",
4174 ],
4175 }
4176
4177 genrule {
4178 name: "genrule_bar",
4179 cmd: "generate-bar",
4180 out: [
4181 "generated_headers/bar/generated_header.h",
4182 ],
4183 export_include_dirs: [
4184 "generated_headers",
4185 ],
4186 }
4187 `
4188
4189 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4190 ctx := testCc(t, genRuleModules+`
4191 cc_library {
4192 name: "libfoo",
4193 srcs: ["foo.c"],
4194 export_include_dirs: ["foo/standard"],
4195 export_system_include_dirs: ["foo/system"],
4196 generated_headers: ["genrule_foo"],
4197 export_generated_headers: ["genrule_foo"],
4198 }
4199
4200 cc_library {
4201 name: "libbar",
4202 srcs: ["bar.c"],
4203 shared_libs: ["libfoo"],
4204 export_include_dirs: ["bar/standard"],
4205 export_system_include_dirs: ["bar/system"],
4206 generated_headers: ["genrule_bar"],
4207 export_generated_headers: ["genrule_bar"],
4208 }
4209 `)
4210 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4211 checkIncludeDirs(t, ctx, foo,
4212 expectedIncludeDirs(`
4213 foo/standard
4214 .intermediates/genrule_foo/gen/generated_headers
4215 `),
4216 expectedSystemIncludeDirs(`foo/system`),
4217 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4218 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4219 )
4220
4221 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4222 checkIncludeDirs(t, ctx, bar,
4223 expectedIncludeDirs(`
4224 bar/standard
4225 .intermediates/genrule_bar/gen/generated_headers
4226 `),
4227 expectedSystemIncludeDirs(`bar/system`),
4228 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4229 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4230 )
4231 })
4232
4233 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4234 ctx := testCc(t, genRuleModules+`
4235 cc_library {
4236 name: "libfoo",
4237 srcs: ["foo.c"],
4238 export_include_dirs: ["foo/standard"],
4239 export_system_include_dirs: ["foo/system"],
4240 generated_headers: ["genrule_foo"],
4241 export_generated_headers: ["genrule_foo"],
4242 }
4243
4244 cc_library {
4245 name: "libbar",
4246 srcs: ["bar.c"],
4247 whole_static_libs: ["libfoo"],
4248 export_include_dirs: ["bar/standard"],
4249 export_system_include_dirs: ["bar/system"],
4250 generated_headers: ["genrule_bar"],
4251 export_generated_headers: ["genrule_bar"],
4252 }
4253 `)
4254 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4255 checkIncludeDirs(t, ctx, foo,
4256 expectedIncludeDirs(`
4257 foo/standard
4258 .intermediates/genrule_foo/gen/generated_headers
4259 `),
4260 expectedSystemIncludeDirs(`foo/system`),
4261 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4262 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4263 )
4264
4265 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4266 checkIncludeDirs(t, ctx, bar,
4267 expectedIncludeDirs(`
4268 bar/standard
4269 foo/standard
4270 .intermediates/genrule_foo/gen/generated_headers
4271 .intermediates/genrule_bar/gen/generated_headers
4272 `),
4273 expectedSystemIncludeDirs(`
4274 bar/system
4275 foo/system
4276 `),
4277 expectedGeneratedHeaders(`
4278 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4279 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4280 `),
4281 expectedOrderOnlyDeps(`
4282 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4283 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4284 `),
4285 )
4286 })
4287
Paul Duffin3cb603e2021-02-19 13:57:10 +00004288 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4289 ctx := testCc(t, genRuleModules+`
4290 cc_library_shared {
4291 name: "libfoo",
4292 srcs: [
4293 "foo.c",
4294 "b.aidl",
4295 "a.proto",
4296 ],
4297 aidl: {
4298 export_aidl_headers: true,
4299 }
4300 }
4301 `)
4302 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4303 checkIncludeDirs(t, ctx, foo,
4304 expectedIncludeDirs(`
4305 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4306 `),
4307 expectedSystemIncludeDirs(``),
4308 expectedGeneratedHeaders(`
4309 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4310 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4311 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004312 `),
4313 expectedOrderOnlyDeps(`
4314 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4315 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4316 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004317 `),
4318 )
4319 })
4320
Paul Duffin3cb603e2021-02-19 13:57:10 +00004321 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4322 ctx := testCc(t, genRuleModules+`
4323 cc_library_shared {
4324 name: "libfoo",
4325 srcs: [
4326 "foo.c",
4327 "b.aidl",
4328 "a.proto",
4329 ],
4330 proto: {
4331 export_proto_headers: true,
4332 }
4333 }
4334 `)
4335 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4336 checkIncludeDirs(t, ctx, foo,
4337 expectedIncludeDirs(`
4338 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4339 `),
4340 expectedSystemIncludeDirs(``),
4341 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004342 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4343 `),
4344 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004345 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4346 `),
4347 )
4348 })
4349
Paul Duffin33056e82021-02-19 13:49:08 +00004350 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004351 ctx := testCc(t, genRuleModules+`
4352 cc_library_shared {
4353 name: "libfoo",
4354 srcs: [
4355 "foo.c",
4356 "a.sysprop",
4357 "b.aidl",
4358 "a.proto",
4359 ],
4360 }
4361 `)
4362 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4363 checkIncludeDirs(t, ctx, foo,
4364 expectedIncludeDirs(`
4365 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4366 `),
4367 expectedSystemIncludeDirs(``),
4368 expectedGeneratedHeaders(`
4369 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004370 `),
4371 expectedOrderOnlyDeps(`
4372 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
4373 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004374 `),
4375 )
4376 })
4377}