blob: 7fc044df3a0993a0d7706ad15b44db1b2aec25a6 [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 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400384 if mod.IsVndkSp() != isVndkSp {
385 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800386 }
387
388 // Check VNDK extension properties.
389 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500390 if mod.IsVndkExt() != isVndkExt {
391 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800392 }
393
394 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
395 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
396 }
397}
398
Jose Galmes0a942a02021-02-03 14:23:15 -0800399func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool, fake bool) {
Bill Peckham945441c2020-08-31 16:07:58 -0700400 t.Helper()
Paul Duffine8366da2021-03-24 10:40:38 +0000401 mod := ctx.ModuleForTests(moduleName, variant)
402 outputFiles := mod.OutputFiles(t, "")
403 if len(outputFiles) != 1 {
Jooyung Han39edb6c2019-11-06 16:53:07 +0900404 t.Errorf("%q must have single output\n", moduleName)
405 return
406 }
407 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900408
Bill Peckham945441c2020-08-31 16:07:58 -0700409 if include {
410 out := singleton.Output(snapshotPath)
Jose Galmes0a942a02021-02-03 14:23:15 -0800411 if fake {
412 if out.Rule == nil {
413 t.Errorf("Missing rule for module %q output file %q", moduleName, outputFiles[0])
414 }
415 } else {
416 if out.Input.String() != outputFiles[0].String() {
417 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
418 }
Bill Peckham945441c2020-08-31 16:07:58 -0700419 }
420 } else {
421 out := singleton.MaybeOutput(snapshotPath)
422 if out.Rule != nil {
423 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
424 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900425 }
426}
427
Bill Peckham945441c2020-08-31 16:07:58 -0700428func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000429 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800430 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, false)
Bill Peckham945441c2020-08-31 16:07:58 -0700431}
432
433func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000434 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800435 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false, false)
436}
437
438func checkSnapshotRule(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000439 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800440 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, true)
Bill Peckham945441c2020-08-31 16:07:58 -0700441}
442
Jooyung Han2216fb12019-11-06 16:46:15 +0900443func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
444 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800445 content := android.ContentFromFileRuleForTests(t, params)
446 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900447 assertArrayString(t, actual, expected)
448}
449
Jooyung Han097087b2019-10-22 19:32:18 +0900450func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
451 t.Helper()
452 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900453 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
454}
455
456func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
457 t.Helper()
Colin Cross78212242021-01-06 14:51:30 -0800458 got := ctx.ModuleForTests(module, "").Module().(*vndkLibrariesTxt).fileNames
459 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900460}
461
Logan Chienf3511742017-10-31 18:04:35 +0800462func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800463 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800464 cc_library {
465 name: "libvndk",
466 vendor_available: true,
467 vndk: {
468 enabled: true,
469 },
470 nocrt: true,
471 }
472
473 cc_library {
474 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900475 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800476 vndk: {
477 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900478 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800479 },
480 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900481 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800482 }
483
484 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900485 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800486 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900487 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800488 vndk: {
489 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900490 },
491 nocrt: true,
492 target: {
493 vendor: {
494 cflags: ["-DTEST"],
495 },
496 product: {
497 cflags: ["-DTEST"],
498 },
499 },
500 }
501
502 cc_library {
503 name: "libvndk_sp",
504 vendor_available: true,
505 vndk: {
506 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800507 support_system_process: true,
508 },
509 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900510 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800511 }
512
513 cc_library {
514 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900515 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800516 vndk: {
517 enabled: true,
518 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900519 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800520 },
521 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900522 target: {
523 vendor: {
524 suffix: "-x",
525 },
526 },
Logan Chienf3511742017-10-31 18:04:35 +0800527 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900528
529 cc_library {
530 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900531 vendor_available: true,
532 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900533 vndk: {
534 enabled: true,
535 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900536 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900537 },
538 nocrt: true,
539 target: {
540 vendor: {
541 suffix: "-x",
542 },
543 product: {
544 suffix: "-x",
545 },
546 },
547 }
548
Justin Yun450ae722021-04-16 19:58:18 +0900549 cc_library {
550 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700551 llndk: {
552 symbol_file: "libllndk.map.txt",
553 export_llndk_headers: ["libllndk_headers"],
554 }
Justin Yun450ae722021-04-16 19:58:18 +0900555 }
556
Justin Yun611e8862021-05-24 18:17:33 +0900557 cc_library {
558 name: "libclang_rt.hwasan-llndk",
559 llndk: {
560 symbol_file: "libclang_rt.hwasan.map.txt",
561 }
562 }
563
Colin Cross627280f2021-04-26 16:53:58 -0700564 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900565 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700566 llndk: {
567 llndk_headers: true,
568 },
Justin Yun450ae722021-04-16 19:58:18 +0900569 export_include_dirs: ["include"],
570 }
571
Colin Crosse4e44bc2020-12-28 13:50:21 -0800572 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900573 name: "llndk.libraries.txt",
574 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800575 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900576 name: "vndkcore.libraries.txt",
577 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800578 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900579 name: "vndksp.libraries.txt",
580 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800581 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900582 name: "vndkprivate.libraries.txt",
583 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800584 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900585 name: "vndkproduct.libraries.txt",
586 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800587 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900588 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800589 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900590 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800591 `
592
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000593 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800594 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900595 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900596 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800597
598 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800599
Jooyung Han261e1582020-10-20 18:54:21 +0900600 // subdir == "" because VNDK libs are not supposed to be installed separately.
601 // They are installed as part of VNDK APEX instead.
602 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
603 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900604 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900605 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
606 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900607 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900608
Justin Yun6977e8a2020-10-29 18:24:11 +0900609 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
610 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900611
Inseob Kim1f086e22019-05-09 13:29:15 +0900612 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900613 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000614 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900615
616 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
617 "arm64", "armv8-a"))
618 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
619 "arm", "armv7-a-neon"))
620
621 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
622 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900623 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
624
Inseob Kim1f086e22019-05-09 13:29:15 +0900625 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
626 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900627 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900628
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900629 variant := "android_vendor.29_arm64_armv8-a_shared"
630 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900631
Inseob Kim7f283f42020-06-01 21:53:49 +0900632 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
633
634 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
635 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900636 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
637 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900638 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
639 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Justin Yun450ae722021-04-16 19:58:18 +0900640 checkSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
641 checkSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900642
Jooyung Han39edb6c2019-11-06 16:53:07 +0900643 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900644 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
645 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
646 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
647 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900648 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900649
Jooyung Han097087b2019-10-22 19:32:18 +0900650 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
651 "LLNDK: libc.so",
652 "LLNDK: libdl.so",
653 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900654 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900655 "LLNDK: libm.so",
656 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900657 "VNDK-SP: libvndk_sp-x.so",
658 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900659 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900660 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900661 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900662 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900663 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900664 "VNDK-private: libvndk-private.so",
665 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900666 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900667 "VNDK-product: libc++.so",
668 "VNDK-product: libvndk_product.so",
669 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900670 })
Justin Yun611e8862021-05-24 18:17:33 +0900671 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libclang_rt.hwasan-llndk.so", "libdl.so", "libft2.so", "libllndk.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900672 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
673 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
674 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 +0900675 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 +0900676 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
677}
678
Yo Chiangbba545e2020-06-09 16:15:37 +0800679func TestVndkWithHostSupported(t *testing.T) {
680 ctx := testCc(t, `
681 cc_library {
682 name: "libvndk_host_supported",
683 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900684 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800685 vndk: {
686 enabled: true,
687 },
688 host_supported: true,
689 }
690
691 cc_library {
692 name: "libvndk_host_supported_but_disabled_on_device",
693 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900694 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800695 vndk: {
696 enabled: true,
697 },
698 host_supported: true,
699 enabled: false,
700 target: {
701 host: {
702 enabled: true,
703 }
704 }
705 }
706
Colin Crosse4e44bc2020-12-28 13:50:21 -0800707 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800708 name: "vndkcore.libraries.txt",
709 }
710 `)
711
712 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
713}
714
Jooyung Han2216fb12019-11-06 16:46:15 +0900715func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800716 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800717 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900718 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800719 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800720 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000721 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800722 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900723 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800724 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900725
726 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Colin Crossaa255532020-07-03 13:18:24 -0700727 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900728 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900729}
730
731func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800732 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900733 cc_library {
734 name: "libvndk",
735 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900736 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900737 vndk: {
738 enabled: true,
739 },
740 nocrt: true,
741 }
742
743 cc_library {
744 name: "libvndk_sp",
745 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900746 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900747 vndk: {
748 enabled: true,
749 support_system_process: true,
750 },
751 nocrt: true,
752 }
753
754 cc_library {
755 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900756 vendor_available: true,
757 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900758 vndk: {
759 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900760 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900761 },
762 nocrt: true,
763 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900764
Colin Crosse4e44bc2020-12-28 13:50:21 -0800765 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900766 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800767 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900768 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800769 `
770
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000771 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800772 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900773 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800774 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
775
776 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
777
778 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900779
Jooyung Han2216fb12019-11-06 16:46:15 +0900780 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900781}
782
Chris Parsons79d66a52020-06-05 17:26:16 -0400783func TestDataLibs(t *testing.T) {
784 bp := `
785 cc_test_library {
786 name: "test_lib",
787 srcs: ["test_lib.cpp"],
788 gtest: false,
789 }
790
791 cc_test {
792 name: "main_test",
793 data_libs: ["test_lib"],
794 gtest: false,
795 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400796 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400797
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000798 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400799 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900800 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400801 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
802
803 ctx := testCcWithConfig(t, config)
804 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
805 testBinary := module.(*Module).linker.(*testBinary)
806 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
807 if err != nil {
808 t.Errorf("Expected cc_test to produce output files, error: %s", err)
809 return
810 }
811 if len(outputFiles) != 1 {
812 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
813 return
814 }
815 if len(testBinary.dataPaths()) != 1 {
816 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
817 return
818 }
819
820 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400821 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400822
823 if !strings.HasSuffix(outputPath, "/main_test") {
824 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
825 return
826 }
827 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
828 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
829 return
830 }
831}
832
Chris Parsons216e10a2020-07-09 17:12:52 -0400833func TestDataLibsRelativeInstallPath(t *testing.T) {
834 bp := `
835 cc_test_library {
836 name: "test_lib",
837 srcs: ["test_lib.cpp"],
838 relative_install_path: "foo/bar/baz",
839 gtest: false,
840 }
841
842 cc_test {
843 name: "main_test",
844 data_libs: ["test_lib"],
845 gtest: false,
846 }
847 `
848
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000849 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400850 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900851 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400852 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
853
854 ctx := testCcWithConfig(t, config)
855 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
856 testBinary := module.(*Module).linker.(*testBinary)
857 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
858 if err != nil {
859 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
860 }
861 if len(outputFiles) != 1 {
862 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
863 }
864 if len(testBinary.dataPaths()) != 1 {
865 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
866 }
867
868 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400869
870 if !strings.HasSuffix(outputPath, "/main_test") {
871 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
872 }
Colin Crossaa255532020-07-03 13:18:24 -0700873 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400874 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
875 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400876 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400877 }
878}
879
Jooyung Han0302a842019-10-30 18:43:49 +0900880func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900881 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900882 cc_library {
883 name: "libvndk",
884 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900885 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900886 vndk: {
887 enabled: true,
888 },
889 nocrt: true,
890 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900891 cc_library {
892 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900893 vendor_available: true,
894 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900895 vndk: {
896 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900897 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900898 },
899 nocrt: true,
900 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800901
902 cc_library {
903 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700904 llndk: {
905 symbol_file: "libllndk.map.txt",
906 export_llndk_headers: ["libllndk_headers"],
907 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800908 }
909
Colin Cross627280f2021-04-26 16:53:58 -0700910 cc_library_headers {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800911 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700912 llndk: {
913 symbol_file: "libllndk.map.txt",
914 },
Colin Crossb5f6fa62021-01-06 17:05:04 -0800915 export_include_dirs: ["include"],
916 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900917 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900918
919 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
920 "LLNDK: libc.so",
921 "LLNDK: libdl.so",
922 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800923 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900924 "LLNDK: libm.so",
925 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900926 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900927 "VNDK-core: libvndk.so",
928 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900929 "VNDK-private: libvndk-private.so",
930 "VNDK-product: libc++.so",
931 "VNDK-product: libvndk-private.so",
932 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900933 })
Logan Chienf3511742017-10-31 18:04:35 +0800934}
935
Justin Yun63e9ec72020-10-29 16:49:43 +0900936func TestVndkModuleError(t *testing.T) {
937 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900938 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900939 cc_library {
940 name: "libvndk",
941 vndk: {
942 enabled: true,
943 },
944 nocrt: true,
945 }
946 `)
947
Justin Yunc0d8c492021-01-07 17:45:31 +0900948 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900949 cc_library {
950 name: "libvndk",
951 product_available: true,
952 vndk: {
953 enabled: true,
954 },
955 nocrt: true,
956 }
957 `)
958
Justin Yun6977e8a2020-10-29 18:24:11 +0900959 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
960 cc_library {
961 name: "libvndkprop",
962 vendor_available: true,
963 product_available: true,
964 vndk: {
965 enabled: true,
966 },
967 nocrt: true,
968 target: {
969 vendor: {
970 cflags: ["-DTEST",],
971 },
972 },
973 }
974 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900975}
976
Logan Chiend3c59a22018-03-29 14:08:15 +0800977func TestVndkDepError(t *testing.T) {
978 // Check whether an error is emitted when a VNDK lib depends on a system lib.
979 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
980 cc_library {
981 name: "libvndk",
982 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900983 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800984 vndk: {
985 enabled: true,
986 },
987 shared_libs: ["libfwk"], // Cause error
988 nocrt: true,
989 }
990
991 cc_library {
992 name: "libfwk",
993 nocrt: true,
994 }
995 `)
996
997 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
998 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
999 cc_library {
1000 name: "libvndk",
1001 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001002 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001003 vndk: {
1004 enabled: true,
1005 },
1006 shared_libs: ["libvendor"], // Cause error
1007 nocrt: true,
1008 }
1009
1010 cc_library {
1011 name: "libvendor",
1012 vendor: true,
1013 nocrt: true,
1014 }
1015 `)
1016
1017 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
1018 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1019 cc_library {
1020 name: "libvndk_sp",
1021 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001022 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001023 vndk: {
1024 enabled: true,
1025 support_system_process: true,
1026 },
1027 shared_libs: ["libfwk"], // Cause error
1028 nocrt: true,
1029 }
1030
1031 cc_library {
1032 name: "libfwk",
1033 nocrt: true,
1034 }
1035 `)
1036
1037 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
1038 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1039 cc_library {
1040 name: "libvndk_sp",
1041 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001042 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001043 vndk: {
1044 enabled: true,
1045 support_system_process: true,
1046 },
1047 shared_libs: ["libvendor"], // Cause error
1048 nocrt: true,
1049 }
1050
1051 cc_library {
1052 name: "libvendor",
1053 vendor: true,
1054 nocrt: true,
1055 }
1056 `)
1057
1058 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
1059 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1060 cc_library {
1061 name: "libvndk_sp",
1062 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001063 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001064 vndk: {
1065 enabled: true,
1066 support_system_process: true,
1067 },
1068 shared_libs: ["libvndk"], // Cause error
1069 nocrt: true,
1070 }
1071
1072 cc_library {
1073 name: "libvndk",
1074 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001075 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001076 vndk: {
1077 enabled: true,
1078 },
1079 nocrt: true,
1080 }
1081 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001082
1083 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1084 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1085 cc_library {
1086 name: "libvndk",
1087 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001088 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001089 vndk: {
1090 enabled: true,
1091 },
1092 shared_libs: ["libnonvndk"],
1093 nocrt: true,
1094 }
1095
1096 cc_library {
1097 name: "libnonvndk",
1098 vendor_available: true,
1099 nocrt: true,
1100 }
1101 `)
1102
1103 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1104 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1105 cc_library {
1106 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001107 vendor_available: true,
1108 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001109 vndk: {
1110 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001111 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001112 },
1113 shared_libs: ["libnonvndk"],
1114 nocrt: true,
1115 }
1116
1117 cc_library {
1118 name: "libnonvndk",
1119 vendor_available: true,
1120 nocrt: true,
1121 }
1122 `)
1123
1124 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1125 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1126 cc_library {
1127 name: "libvndksp",
1128 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001129 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001130 vndk: {
1131 enabled: true,
1132 support_system_process: true,
1133 },
1134 shared_libs: ["libnonvndk"],
1135 nocrt: true,
1136 }
1137
1138 cc_library {
1139 name: "libnonvndk",
1140 vendor_available: true,
1141 nocrt: true,
1142 }
1143 `)
1144
1145 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1146 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1147 cc_library {
1148 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001149 vendor_available: true,
1150 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001151 vndk: {
1152 enabled: true,
1153 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001154 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001155 },
1156 shared_libs: ["libnonvndk"],
1157 nocrt: true,
1158 }
1159
1160 cc_library {
1161 name: "libnonvndk",
1162 vendor_available: true,
1163 nocrt: true,
1164 }
1165 `)
1166}
1167
1168func TestDoubleLoadbleDep(t *testing.T) {
1169 // okay to link : LLNDK -> double_loadable VNDK
1170 testCc(t, `
1171 cc_library {
1172 name: "libllndk",
1173 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001174 llndk: {
1175 symbol_file: "libllndk.map.txt",
1176 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001177 }
1178
1179 cc_library {
1180 name: "libdoubleloadable",
1181 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001182 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001183 vndk: {
1184 enabled: true,
1185 },
1186 double_loadable: true,
1187 }
1188 `)
1189 // okay to link : LLNDK -> VNDK-SP
1190 testCc(t, `
1191 cc_library {
1192 name: "libllndk",
1193 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001194 llndk: {
1195 symbol_file: "libllndk.map.txt",
1196 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001197 }
1198
1199 cc_library {
1200 name: "libvndksp",
1201 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001202 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001203 vndk: {
1204 enabled: true,
1205 support_system_process: true,
1206 },
1207 }
1208 `)
1209 // okay to link : double_loadable -> double_loadable
1210 testCc(t, `
1211 cc_library {
1212 name: "libdoubleloadable1",
1213 shared_libs: ["libdoubleloadable2"],
1214 vendor_available: true,
1215 double_loadable: true,
1216 }
1217
1218 cc_library {
1219 name: "libdoubleloadable2",
1220 vendor_available: true,
1221 double_loadable: true,
1222 }
1223 `)
1224 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1225 testCc(t, `
1226 cc_library {
1227 name: "libdoubleloadable",
1228 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001229 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001230 vndk: {
1231 enabled: true,
1232 },
1233 double_loadable: true,
1234 shared_libs: ["libnondoubleloadable"],
1235 }
1236
1237 cc_library {
1238 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001239 vendor_available: true,
1240 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001241 vndk: {
1242 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001243 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001244 },
1245 double_loadable: true,
1246 }
1247 `)
1248 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1249 testCc(t, `
1250 cc_library {
1251 name: "libllndk",
1252 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001253 llndk: {
1254 symbol_file: "libllndk.map.txt",
1255 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001256 }
1257
1258 cc_library {
1259 name: "libcoreonly",
1260 shared_libs: ["libvendoravailable"],
1261 }
1262
1263 // indirect dependency of LLNDK
1264 cc_library {
1265 name: "libvendoravailable",
1266 vendor_available: true,
1267 double_loadable: true,
1268 }
1269 `)
1270}
1271
1272func TestDoubleLoadableDepError(t *testing.T) {
1273 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1274 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1275 cc_library {
1276 name: "libllndk",
1277 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001278 llndk: {
1279 symbol_file: "libllndk.map.txt",
1280 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001281 }
1282
1283 cc_library {
1284 name: "libnondoubleloadable",
1285 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001286 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001287 vndk: {
1288 enabled: true,
1289 },
1290 }
1291 `)
1292
1293 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1294 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1295 cc_library {
1296 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001297 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001298 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001299 llndk: {
1300 symbol_file: "libllndk.map.txt",
1301 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001302 }
1303
1304 cc_library {
1305 name: "libnondoubleloadable",
1306 vendor_available: true,
1307 }
1308 `)
1309
Jooyung Hana70f0672019-01-18 15:20:43 +09001310 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1311 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1312 cc_library {
1313 name: "libllndk",
1314 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001315 llndk: {
1316 symbol_file: "libllndk.map.txt",
1317 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001318 }
1319
1320 cc_library {
1321 name: "libcoreonly",
1322 shared_libs: ["libvendoravailable"],
1323 }
1324
1325 // indirect dependency of LLNDK
1326 cc_library {
1327 name: "libvendoravailable",
1328 vendor_available: true,
1329 }
1330 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001331
1332 // The error is not from 'client' but from 'libllndk'
1333 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1334 cc_library {
1335 name: "client",
1336 vendor_available: true,
1337 double_loadable: true,
1338 shared_libs: ["libllndk"],
1339 }
1340 cc_library {
1341 name: "libllndk",
1342 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001343 llndk: {
1344 symbol_file: "libllndk.map.txt",
1345 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001346 }
1347 cc_library {
1348 name: "libnondoubleloadable",
1349 vendor_available: true,
1350 }
1351 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001352}
1353
Jooyung Han479ca172020-10-19 18:51:07 +09001354func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1355 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1356 cc_library {
1357 name: "libvndksp",
1358 shared_libs: ["libanothervndksp"],
1359 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001360 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001361 vndk: {
1362 enabled: true,
1363 support_system_process: true,
1364 }
1365 }
1366
1367 cc_library {
1368 name: "libllndk",
1369 shared_libs: ["libanothervndksp"],
1370 }
1371
Jooyung Han479ca172020-10-19 18:51:07 +09001372 cc_library {
1373 name: "libanothervndksp",
1374 vendor_available: true,
1375 }
1376 `)
1377}
1378
Logan Chienf3511742017-10-31 18:04:35 +08001379func TestVndkExt(t *testing.T) {
1380 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001381 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001382 cc_library {
1383 name: "libvndk",
1384 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001385 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001386 vndk: {
1387 enabled: true,
1388 },
1389 nocrt: true,
1390 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001391 cc_library {
1392 name: "libvndk2",
1393 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001394 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001395 vndk: {
1396 enabled: true,
1397 },
1398 target: {
1399 vendor: {
1400 suffix: "-suffix",
1401 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001402 product: {
1403 suffix: "-suffix",
1404 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001405 },
1406 nocrt: true,
1407 }
Logan Chienf3511742017-10-31 18:04:35 +08001408
1409 cc_library {
1410 name: "libvndk_ext",
1411 vendor: true,
1412 vndk: {
1413 enabled: true,
1414 extends: "libvndk",
1415 },
1416 nocrt: true,
1417 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001418
1419 cc_library {
1420 name: "libvndk2_ext",
1421 vendor: true,
1422 vndk: {
1423 enabled: true,
1424 extends: "libvndk2",
1425 },
1426 nocrt: true,
1427 }
Logan Chienf3511742017-10-31 18:04:35 +08001428
Justin Yun0ecf0b22020-02-28 15:07:59 +09001429 cc_library {
1430 name: "libvndk_ext_product",
1431 product_specific: true,
1432 vndk: {
1433 enabled: true,
1434 extends: "libvndk",
1435 },
1436 nocrt: true,
1437 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001438
Justin Yun0ecf0b22020-02-28 15:07:59 +09001439 cc_library {
1440 name: "libvndk2_ext_product",
1441 product_specific: true,
1442 vndk: {
1443 enabled: true,
1444 extends: "libvndk2",
1445 },
1446 nocrt: true,
1447 }
1448 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001449 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001450 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1451 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001452 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001453
1454 ctx := testCcWithConfig(t, config)
1455
1456 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1457 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1458
1459 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1460 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1461
1462 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1463 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001464}
1465
Logan Chiend3c59a22018-03-29 14:08:15 +08001466func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001467 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1468 ctx := testCcNoVndk(t, `
1469 cc_library {
1470 name: "libvndk",
1471 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001472 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001473 vndk: {
1474 enabled: true,
1475 },
1476 nocrt: true,
1477 }
1478
1479 cc_library {
1480 name: "libvndk_ext",
1481 vendor: true,
1482 vndk: {
1483 enabled: true,
1484 extends: "libvndk",
1485 },
1486 nocrt: true,
1487 }
1488 `)
1489
1490 // Ensures that the core variant of "libvndk_ext" can be found.
1491 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1492 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1493 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1494 }
1495}
1496
Justin Yun0ecf0b22020-02-28 15:07:59 +09001497func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1498 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001499 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001500 cc_library {
1501 name: "libvndk",
1502 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001503 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001504 vndk: {
1505 enabled: true,
1506 },
1507 nocrt: true,
1508 }
1509
1510 cc_library {
1511 name: "libvndk_ext_product",
1512 product_specific: true,
1513 vndk: {
1514 enabled: true,
1515 extends: "libvndk",
1516 },
1517 nocrt: true,
1518 }
1519 `)
1520
1521 // Ensures that the core variant of "libvndk_ext_product" can be found.
1522 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1523 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1524 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1525 }
1526}
1527
Logan Chienf3511742017-10-31 18:04:35 +08001528func TestVndkExtError(t *testing.T) {
1529 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001530 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001531 cc_library {
1532 name: "libvndk",
1533 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001534 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001535 vndk: {
1536 enabled: true,
1537 },
1538 nocrt: true,
1539 }
1540
1541 cc_library {
1542 name: "libvndk_ext",
1543 vndk: {
1544 enabled: true,
1545 extends: "libvndk",
1546 },
1547 nocrt: true,
1548 }
1549 `)
1550
1551 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1552 cc_library {
1553 name: "libvndk",
1554 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001555 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001556 vndk: {
1557 enabled: true,
1558 },
1559 nocrt: true,
1560 }
1561
1562 cc_library {
1563 name: "libvndk_ext",
1564 vendor: true,
1565 vndk: {
1566 enabled: true,
1567 },
1568 nocrt: true,
1569 }
1570 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001571
1572 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1573 cc_library {
1574 name: "libvndk",
1575 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001576 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001577 vndk: {
1578 enabled: true,
1579 },
1580 nocrt: true,
1581 }
1582
1583 cc_library {
1584 name: "libvndk_ext_product",
1585 product_specific: true,
1586 vndk: {
1587 enabled: true,
1588 },
1589 nocrt: true,
1590 }
1591 `)
1592
1593 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1594 cc_library {
1595 name: "libvndk",
1596 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001597 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001598 vndk: {
1599 enabled: true,
1600 },
1601 nocrt: true,
1602 }
1603
1604 cc_library {
1605 name: "libvndk_ext_product",
1606 product_specific: true,
1607 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001608 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001609 vndk: {
1610 enabled: true,
1611 extends: "libvndk",
1612 },
1613 nocrt: true,
1614 }
1615 `)
Logan Chienf3511742017-10-31 18:04:35 +08001616}
1617
1618func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1619 // This test ensures an error is emitted for inconsistent support_system_process.
1620 testCcError(t, "module \".*\" with mismatched support_system_process", `
1621 cc_library {
1622 name: "libvndk",
1623 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001624 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001625 vndk: {
1626 enabled: true,
1627 },
1628 nocrt: true,
1629 }
1630
1631 cc_library {
1632 name: "libvndk_sp_ext",
1633 vendor: true,
1634 vndk: {
1635 enabled: true,
1636 extends: "libvndk",
1637 support_system_process: true,
1638 },
1639 nocrt: true,
1640 }
1641 `)
1642
1643 testCcError(t, "module \".*\" with mismatched support_system_process", `
1644 cc_library {
1645 name: "libvndk_sp",
1646 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001647 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001648 vndk: {
1649 enabled: true,
1650 support_system_process: true,
1651 },
1652 nocrt: true,
1653 }
1654
1655 cc_library {
1656 name: "libvndk_ext",
1657 vendor: true,
1658 vndk: {
1659 enabled: true,
1660 extends: "libvndk_sp",
1661 },
1662 nocrt: true,
1663 }
1664 `)
1665}
1666
1667func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001668 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001669 // with `private: true`.
1670 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001671 cc_library {
1672 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001673 vendor_available: true,
1674 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001675 vndk: {
1676 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001677 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001678 },
1679 nocrt: true,
1680 }
1681
1682 cc_library {
1683 name: "libvndk_ext",
1684 vendor: true,
1685 vndk: {
1686 enabled: true,
1687 extends: "libvndk",
1688 },
1689 nocrt: true,
1690 }
1691 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001692
Justin Yunfd9e8042020-12-23 18:23:14 +09001693 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001694 cc_library {
1695 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001696 vendor_available: true,
1697 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001698 vndk: {
1699 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001700 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001701 },
1702 nocrt: true,
1703 }
1704
1705 cc_library {
1706 name: "libvndk_ext_product",
1707 product_specific: true,
1708 vndk: {
1709 enabled: true,
1710 extends: "libvndk",
1711 },
1712 nocrt: true,
1713 }
1714 `)
Logan Chienf3511742017-10-31 18:04:35 +08001715}
1716
Logan Chiend3c59a22018-03-29 14:08:15 +08001717func TestVendorModuleUseVndkExt(t *testing.T) {
1718 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001719 testCc(t, `
1720 cc_library {
1721 name: "libvndk",
1722 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001723 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001724 vndk: {
1725 enabled: true,
1726 },
1727 nocrt: true,
1728 }
1729
1730 cc_library {
1731 name: "libvndk_ext",
1732 vendor: true,
1733 vndk: {
1734 enabled: true,
1735 extends: "libvndk",
1736 },
1737 nocrt: true,
1738 }
1739
1740 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001741 name: "libvndk_sp",
1742 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001743 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001744 vndk: {
1745 enabled: true,
1746 support_system_process: true,
1747 },
1748 nocrt: true,
1749 }
1750
1751 cc_library {
1752 name: "libvndk_sp_ext",
1753 vendor: true,
1754 vndk: {
1755 enabled: true,
1756 extends: "libvndk_sp",
1757 support_system_process: true,
1758 },
1759 nocrt: true,
1760 }
1761
1762 cc_library {
1763 name: "libvendor",
1764 vendor: true,
1765 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1766 nocrt: true,
1767 }
1768 `)
1769}
1770
Logan Chiend3c59a22018-03-29 14:08:15 +08001771func TestVndkExtUseVendorLib(t *testing.T) {
1772 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001773 testCc(t, `
1774 cc_library {
1775 name: "libvndk",
1776 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001777 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001778 vndk: {
1779 enabled: true,
1780 },
1781 nocrt: true,
1782 }
1783
1784 cc_library {
1785 name: "libvndk_ext",
1786 vendor: true,
1787 vndk: {
1788 enabled: true,
1789 extends: "libvndk",
1790 },
1791 shared_libs: ["libvendor"],
1792 nocrt: true,
1793 }
1794
1795 cc_library {
1796 name: "libvendor",
1797 vendor: true,
1798 nocrt: true,
1799 }
1800 `)
Logan Chienf3511742017-10-31 18:04:35 +08001801
Logan Chiend3c59a22018-03-29 14:08:15 +08001802 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1803 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001804 cc_library {
1805 name: "libvndk_sp",
1806 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001807 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001808 vndk: {
1809 enabled: true,
1810 support_system_process: true,
1811 },
1812 nocrt: true,
1813 }
1814
1815 cc_library {
1816 name: "libvndk_sp_ext",
1817 vendor: true,
1818 vndk: {
1819 enabled: true,
1820 extends: "libvndk_sp",
1821 support_system_process: true,
1822 },
1823 shared_libs: ["libvendor"], // Cause an error
1824 nocrt: true,
1825 }
1826
1827 cc_library {
1828 name: "libvendor",
1829 vendor: true,
1830 nocrt: true,
1831 }
1832 `)
1833}
1834
Justin Yun0ecf0b22020-02-28 15:07:59 +09001835func TestProductVndkExtDependency(t *testing.T) {
1836 bp := `
1837 cc_library {
1838 name: "libvndk",
1839 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001840 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001841 vndk: {
1842 enabled: true,
1843 },
1844 nocrt: true,
1845 }
1846
1847 cc_library {
1848 name: "libvndk_ext_product",
1849 product_specific: true,
1850 vndk: {
1851 enabled: true,
1852 extends: "libvndk",
1853 },
1854 shared_libs: ["libproduct_for_vndklibs"],
1855 nocrt: true,
1856 }
1857
1858 cc_library {
1859 name: "libvndk_sp",
1860 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001861 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001862 vndk: {
1863 enabled: true,
1864 support_system_process: true,
1865 },
1866 nocrt: true,
1867 }
1868
1869 cc_library {
1870 name: "libvndk_sp_ext_product",
1871 product_specific: true,
1872 vndk: {
1873 enabled: true,
1874 extends: "libvndk_sp",
1875 support_system_process: true,
1876 },
1877 shared_libs: ["libproduct_for_vndklibs"],
1878 nocrt: true,
1879 }
1880
1881 cc_library {
1882 name: "libproduct",
1883 product_specific: true,
1884 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1885 nocrt: true,
1886 }
1887
1888 cc_library {
1889 name: "libproduct_for_vndklibs",
1890 product_specific: true,
1891 nocrt: true,
1892 }
1893 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001894 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001895 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1896 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001897 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001898
1899 testCcWithConfig(t, config)
1900}
1901
Logan Chiend3c59a22018-03-29 14:08:15 +08001902func TestVndkSpExtUseVndkError(t *testing.T) {
1903 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1904 // library.
1905 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1906 cc_library {
1907 name: "libvndk",
1908 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001909 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001910 vndk: {
1911 enabled: true,
1912 },
1913 nocrt: true,
1914 }
1915
1916 cc_library {
1917 name: "libvndk_sp",
1918 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001919 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001920 vndk: {
1921 enabled: true,
1922 support_system_process: true,
1923 },
1924 nocrt: true,
1925 }
1926
1927 cc_library {
1928 name: "libvndk_sp_ext",
1929 vendor: true,
1930 vndk: {
1931 enabled: true,
1932 extends: "libvndk_sp",
1933 support_system_process: true,
1934 },
1935 shared_libs: ["libvndk"], // Cause an error
1936 nocrt: true,
1937 }
1938 `)
1939
1940 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1941 // library.
1942 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1943 cc_library {
1944 name: "libvndk",
1945 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001946 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001947 vndk: {
1948 enabled: true,
1949 },
1950 nocrt: true,
1951 }
1952
1953 cc_library {
1954 name: "libvndk_ext",
1955 vendor: true,
1956 vndk: {
1957 enabled: true,
1958 extends: "libvndk",
1959 },
1960 nocrt: true,
1961 }
1962
1963 cc_library {
1964 name: "libvndk_sp",
1965 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001966 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001967 vndk: {
1968 enabled: true,
1969 support_system_process: true,
1970 },
1971 nocrt: true,
1972 }
1973
1974 cc_library {
1975 name: "libvndk_sp_ext",
1976 vendor: true,
1977 vndk: {
1978 enabled: true,
1979 extends: "libvndk_sp",
1980 support_system_process: true,
1981 },
1982 shared_libs: ["libvndk_ext"], // Cause an error
1983 nocrt: true,
1984 }
1985 `)
1986}
1987
1988func TestVndkUseVndkExtError(t *testing.T) {
1989 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1990 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001991 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1992 cc_library {
1993 name: "libvndk",
1994 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001995 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001996 vndk: {
1997 enabled: true,
1998 },
1999 nocrt: true,
2000 }
2001
2002 cc_library {
2003 name: "libvndk_ext",
2004 vendor: true,
2005 vndk: {
2006 enabled: true,
2007 extends: "libvndk",
2008 },
2009 nocrt: true,
2010 }
2011
2012 cc_library {
2013 name: "libvndk2",
2014 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002015 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002016 vndk: {
2017 enabled: true,
2018 },
2019 shared_libs: ["libvndk_ext"],
2020 nocrt: true,
2021 }
2022 `)
2023
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002024 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002025 cc_library {
2026 name: "libvndk",
2027 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002028 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002029 vndk: {
2030 enabled: true,
2031 },
2032 nocrt: true,
2033 }
2034
2035 cc_library {
2036 name: "libvndk_ext",
2037 vendor: true,
2038 vndk: {
2039 enabled: true,
2040 extends: "libvndk",
2041 },
2042 nocrt: true,
2043 }
2044
2045 cc_library {
2046 name: "libvndk2",
2047 vendor_available: true,
2048 vndk: {
2049 enabled: true,
2050 },
2051 target: {
2052 vendor: {
2053 shared_libs: ["libvndk_ext"],
2054 },
2055 },
2056 nocrt: true,
2057 }
2058 `)
2059
2060 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2061 cc_library {
2062 name: "libvndk_sp",
2063 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002064 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002065 vndk: {
2066 enabled: true,
2067 support_system_process: true,
2068 },
2069 nocrt: true,
2070 }
2071
2072 cc_library {
2073 name: "libvndk_sp_ext",
2074 vendor: true,
2075 vndk: {
2076 enabled: true,
2077 extends: "libvndk_sp",
2078 support_system_process: true,
2079 },
2080 nocrt: true,
2081 }
2082
2083 cc_library {
2084 name: "libvndk_sp_2",
2085 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002086 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002087 vndk: {
2088 enabled: true,
2089 support_system_process: true,
2090 },
2091 shared_libs: ["libvndk_sp_ext"],
2092 nocrt: true,
2093 }
2094 `)
2095
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002096 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002097 cc_library {
2098 name: "libvndk_sp",
2099 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002100 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002101 vndk: {
2102 enabled: true,
2103 },
2104 nocrt: true,
2105 }
2106
2107 cc_library {
2108 name: "libvndk_sp_ext",
2109 vendor: true,
2110 vndk: {
2111 enabled: true,
2112 extends: "libvndk_sp",
2113 },
2114 nocrt: true,
2115 }
2116
2117 cc_library {
2118 name: "libvndk_sp2",
2119 vendor_available: true,
2120 vndk: {
2121 enabled: true,
2122 },
2123 target: {
2124 vendor: {
2125 shared_libs: ["libvndk_sp_ext"],
2126 },
2127 },
2128 nocrt: true,
2129 }
2130 `)
2131}
2132
Justin Yun5f7f7e82019-11-18 19:52:14 +09002133func TestEnforceProductVndkVersion(t *testing.T) {
2134 bp := `
2135 cc_library {
2136 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002137 llndk: {
2138 symbol_file: "libllndk.map.txt",
2139 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002140 }
2141 cc_library {
2142 name: "libvndk",
2143 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002144 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002145 vndk: {
2146 enabled: true,
2147 },
2148 nocrt: true,
2149 }
2150 cc_library {
2151 name: "libvndk_sp",
2152 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002153 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002154 vndk: {
2155 enabled: true,
2156 support_system_process: true,
2157 },
2158 nocrt: true,
2159 }
2160 cc_library {
2161 name: "libva",
2162 vendor_available: true,
2163 nocrt: true,
2164 }
2165 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002166 name: "libpa",
2167 product_available: true,
2168 nocrt: true,
2169 }
2170 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002171 name: "libboth_available",
2172 vendor_available: true,
2173 product_available: true,
2174 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002175 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002176 target: {
2177 vendor: {
2178 suffix: "-vendor",
2179 },
2180 product: {
2181 suffix: "-product",
2182 },
2183 }
2184 }
2185 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002186 name: "libproduct_va",
2187 product_specific: true,
2188 vendor_available: true,
2189 nocrt: true,
2190 }
2191 cc_library {
2192 name: "libprod",
2193 product_specific: true,
2194 shared_libs: [
2195 "libllndk",
2196 "libvndk",
2197 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002198 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002199 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002200 "libproduct_va",
2201 ],
2202 nocrt: true,
2203 }
2204 cc_library {
2205 name: "libvendor",
2206 vendor: true,
2207 shared_libs: [
2208 "libllndk",
2209 "libvndk",
2210 "libvndk_sp",
2211 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002212 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002213 "libproduct_va",
2214 ],
2215 nocrt: true,
2216 }
2217 `
2218
Paul Duffin8567f222021-03-23 00:02:06 +00002219 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002220
Jooyung Han261e1582020-10-20 18:54:21 +09002221 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2222 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002223
2224 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2225 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2226
2227 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2228 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002229
2230 ensureStringContains := func(t *testing.T, str string, substr string) {
2231 t.Helper()
2232 if !strings.Contains(str, substr) {
2233 t.Errorf("%q is not found in %v", substr, str)
2234 }
2235 }
2236 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2237 t.Helper()
2238 if strings.Contains(str, substr) {
2239 t.Errorf("%q is found in %v", substr, str)
2240 }
2241 }
2242
2243 // _static variant is used since _shared reuses *.o from the static variant
2244 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2245 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2246
2247 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2248 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2249 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2250 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2251
2252 product_cflags := product_static.Rule("cc").Args["cFlags"]
2253 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2254 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2255 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002256}
2257
2258func TestEnforceProductVndkVersionErrors(t *testing.T) {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002259 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002260 cc_library {
2261 name: "libprod",
2262 product_specific: true,
2263 shared_libs: [
2264 "libvendor",
2265 ],
2266 nocrt: true,
2267 }
2268 cc_library {
2269 name: "libvendor",
2270 vendor: true,
2271 nocrt: true,
2272 }
2273 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002274 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002275 cc_library {
2276 name: "libprod",
2277 product_specific: true,
2278 shared_libs: [
2279 "libsystem",
2280 ],
2281 nocrt: true,
2282 }
2283 cc_library {
2284 name: "libsystem",
2285 nocrt: true,
2286 }
2287 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002288 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002289 cc_library {
2290 name: "libprod",
2291 product_specific: true,
2292 shared_libs: [
2293 "libva",
2294 ],
2295 nocrt: true,
2296 }
2297 cc_library {
2298 name: "libva",
2299 vendor_available: true,
2300 nocrt: true,
2301 }
2302 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002303 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002304 cc_library {
2305 name: "libprod",
2306 product_specific: true,
2307 shared_libs: [
2308 "libvndk_private",
2309 ],
2310 nocrt: true,
2311 }
2312 cc_library {
2313 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002314 vendor_available: true,
2315 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002316 vndk: {
2317 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002318 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002319 },
2320 nocrt: true,
2321 }
2322 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002323 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002324 cc_library {
2325 name: "libprod",
2326 product_specific: true,
2327 shared_libs: [
2328 "libsystem_ext",
2329 ],
2330 nocrt: true,
2331 }
2332 cc_library {
2333 name: "libsystem_ext",
2334 system_ext_specific: true,
2335 nocrt: true,
2336 }
2337 `)
2338 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2339 cc_library {
2340 name: "libsystem",
2341 shared_libs: [
2342 "libproduct_va",
2343 ],
2344 nocrt: true,
2345 }
2346 cc_library {
2347 name: "libproduct_va",
2348 product_specific: true,
2349 vendor_available: true,
2350 nocrt: true,
2351 }
2352 `)
2353}
2354
Jooyung Han38002912019-05-16 04:01:54 +09002355func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002356 bp := `
2357 cc_library {
2358 name: "libvndk",
2359 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002360 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002361 vndk: {
2362 enabled: true,
2363 },
2364 }
2365 cc_library {
2366 name: "libvndksp",
2367 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002368 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002369 vndk: {
2370 enabled: true,
2371 support_system_process: true,
2372 },
2373 }
2374 cc_library {
2375 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002376 vendor_available: true,
2377 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002378 vndk: {
2379 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002380 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002381 },
2382 }
2383 cc_library {
2384 name: "libvendor",
2385 vendor: true,
2386 }
2387 cc_library {
2388 name: "libvndkext",
2389 vendor: true,
2390 vndk: {
2391 enabled: true,
2392 extends: "libvndk",
2393 },
2394 }
2395 vndk_prebuilt_shared {
2396 name: "prevndk",
2397 version: "27",
2398 target_arch: "arm",
2399 binder32bit: true,
2400 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002401 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002402 vndk: {
2403 enabled: true,
2404 },
2405 arch: {
2406 arm: {
2407 srcs: ["liba.so"],
2408 },
2409 },
2410 }
2411 cc_library {
2412 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002413 llndk: {
2414 symbol_file: "libllndk.map.txt",
2415 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002416 }
2417 cc_library {
2418 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002419 llndk: {
2420 symbol_file: "libllndkprivate.map.txt",
2421 private: true,
2422 }
Colin Cross78212242021-01-06 14:51:30 -08002423 }
2424
2425 llndk_libraries_txt {
2426 name: "llndk.libraries.txt",
2427 }
2428 vndkcore_libraries_txt {
2429 name: "vndkcore.libraries.txt",
2430 }
2431 vndksp_libraries_txt {
2432 name: "vndksp.libraries.txt",
2433 }
2434 vndkprivate_libraries_txt {
2435 name: "vndkprivate.libraries.txt",
2436 }
2437 vndkcorevariant_libraries_txt {
2438 name: "vndkcorevariant.libraries.txt",
2439 insert_vndk_version: false,
2440 }
2441 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002442
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002443 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002444 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002445 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002446 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002447 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002448
Colin Cross78212242021-01-06 14:51:30 -08002449 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2450 []string{"libvndk.so", "libvndkprivate.so"})
2451 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2452 []string{"libc++.so", "libvndksp.so"})
2453 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2454 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2455 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2456 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002457
Colin Crossfb0c16e2019-11-20 17:12:35 -08002458 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002459
Jooyung Han38002912019-05-16 04:01:54 +09002460 tests := []struct {
2461 variant string
2462 name string
2463 expected string
2464 }{
2465 {vendorVariant, "libvndk", "native:vndk"},
2466 {vendorVariant, "libvndksp", "native:vndk"},
2467 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2468 {vendorVariant, "libvendor", "native:vendor"},
2469 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002470 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002471 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002472 {coreVariant, "libvndk", "native:platform"},
2473 {coreVariant, "libvndkprivate", "native:platform"},
2474 {coreVariant, "libllndk", "native:platform"},
2475 }
2476 for _, test := range tests {
2477 t.Run(test.name, func(t *testing.T) {
2478 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2479 assertString(t, module.makeLinkType, test.expected)
2480 })
2481 }
2482}
2483
Jeff Gaston294356f2017-09-27 17:05:30 -07002484var staticLinkDepOrderTestCases = []struct {
2485 // This is a string representation of a map[moduleName][]moduleDependency .
2486 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002487 inStatic string
2488
2489 // This is a string representation of a map[moduleName][]moduleDependency .
2490 // It models the dependencies declared in an Android.bp file.
2491 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002492
2493 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2494 // The keys of allOrdered specify which modules we would like to check.
2495 // The values of allOrdered specify the expected result (of the transitive closure of all
2496 // dependencies) for each module to test
2497 allOrdered string
2498
2499 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2500 // The keys of outOrdered specify which modules we would like to check.
2501 // The values of outOrdered specify the expected result (of the ordered linker command line)
2502 // for each module to test.
2503 outOrdered string
2504}{
2505 // Simple tests
2506 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002507 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002508 outOrdered: "",
2509 },
2510 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002511 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002512 outOrdered: "a:",
2513 },
2514 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002515 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002516 outOrdered: "a:b; b:",
2517 },
2518 // Tests of reordering
2519 {
2520 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002521 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002522 outOrdered: "a:b,c,d; b:d; c:d; d:",
2523 },
2524 {
2525 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002526 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002527 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2528 },
2529 {
2530 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002531 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002532 outOrdered: "a:d,b,e,c; d:b; e:c",
2533 },
2534 {
2535 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002536 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002537 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2538 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2539 },
2540 {
2541 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002542 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 -07002543 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2544 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2545 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002546 // shared dependencies
2547 {
2548 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2549 // So, we don't actually have to check that a shared dependency of c will change the order
2550 // of a library that depends statically on b and on c. We only need to check that if c has
2551 // a shared dependency on b, that that shows up in allOrdered.
2552 inShared: "c:b",
2553 allOrdered: "c:b",
2554 outOrdered: "c:",
2555 },
2556 {
2557 // This test doesn't actually include any shared dependencies but it's a reminder of what
2558 // the second phase of the above test would look like
2559 inStatic: "a:b,c; c:b",
2560 allOrdered: "a:c,b; c:b",
2561 outOrdered: "a:c,b; c:b",
2562 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002563 // tiebreakers for when two modules specifying different orderings and there is no dependency
2564 // to dictate an order
2565 {
2566 // 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 -08002567 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002568 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2569 },
2570 {
2571 // 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 -08002572 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 -07002573 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2574 },
2575 // Tests involving duplicate dependencies
2576 {
2577 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002578 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002579 outOrdered: "a:c,b",
2580 },
2581 {
2582 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002583 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002584 outOrdered: "a:d,c,b",
2585 },
2586 // Tests to confirm the nonexistence of infinite loops.
2587 // These cases should never happen, so as long as the test terminates and the
2588 // result is deterministic then that should be fine.
2589 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002590 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002591 outOrdered: "a:a",
2592 },
2593 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002594 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002595 allOrdered: "a:b,c; b:c,a; c:a,b",
2596 outOrdered: "a:b; b:c; c:a",
2597 },
2598 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002599 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002600 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2601 outOrdered: "a:c,b; b:a,c; c:b,a",
2602 },
2603}
2604
2605// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2606func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2607 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2608 strippedText := strings.Replace(text, " ", "", -1)
2609 if len(strippedText) < 1 {
2610 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2611 }
2612 allDeps = make(map[android.Path][]android.Path, 0)
2613
2614 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2615 moduleTexts := strings.Split(strippedText, ";")
2616
2617 outputForModuleName := func(moduleName string) android.Path {
2618 return android.PathForTesting(moduleName)
2619 }
2620
2621 for _, moduleText := range moduleTexts {
2622 // convert from "a:b,c" to ["a", "b,c"]
2623 components := strings.Split(moduleText, ":")
2624 if len(components) != 2 {
2625 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2626 }
2627 moduleName := components[0]
2628 moduleOutput := outputForModuleName(moduleName)
2629 modulesInOrder = append(modulesInOrder, moduleOutput)
2630
2631 depString := components[1]
2632 // convert from "b,c" to ["b", "c"]
2633 depNames := strings.Split(depString, ",")
2634 if len(depString) < 1 {
2635 depNames = []string{}
2636 }
2637 var deps []android.Path
2638 for _, depName := range depNames {
2639 deps = append(deps, outputForModuleName(depName))
2640 }
2641 allDeps[moduleOutput] = deps
2642 }
2643 return modulesInOrder, allDeps
2644}
2645
Jeff Gaston294356f2017-09-27 17:05:30 -07002646func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2647 for _, moduleName := range moduleNames {
2648 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002649 output := module.outputFile.Path().RelativeToTop()
Jeff Gaston294356f2017-09-27 17:05:30 -07002650 paths = append(paths, output)
2651 }
2652 return paths
2653}
2654
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002655func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002656 ctx := testCc(t, `
2657 cc_library {
2658 name: "a",
2659 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002660 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002661 }
2662 cc_library {
2663 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002664 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002665 }
2666 cc_library {
2667 name: "c",
2668 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002669 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002670 }
2671 cc_library {
2672 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002673 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002674 }
2675
2676 `)
2677
Colin Cross7113d202019-11-20 16:39:12 -08002678 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002679 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002680 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2681 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002682 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002683
2684 if !reflect.DeepEqual(actual, expected) {
2685 t.Errorf("staticDeps orderings were not propagated correctly"+
2686 "\nactual: %v"+
2687 "\nexpected: %v",
2688 actual,
2689 expected,
2690 )
2691 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002692}
Jeff Gaston294356f2017-09-27 17:05:30 -07002693
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002694func TestStaticLibDepReorderingWithShared(t *testing.T) {
2695 ctx := testCc(t, `
2696 cc_library {
2697 name: "a",
2698 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002699 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002700 }
2701 cc_library {
2702 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002703 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002704 }
2705 cc_library {
2706 name: "c",
2707 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002708 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002709 }
2710
2711 `)
2712
Colin Cross7113d202019-11-20 16:39:12 -08002713 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002714 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002715 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2716 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002717 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002718
2719 if !reflect.DeepEqual(actual, expected) {
2720 t.Errorf("staticDeps orderings did not account for shared libs"+
2721 "\nactual: %v"+
2722 "\nexpected: %v",
2723 actual,
2724 expected,
2725 )
2726 }
2727}
2728
Jooyung Hanb04a4992020-03-13 18:57:35 +09002729func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002730 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002731 if !reflect.DeepEqual(actual, expected) {
2732 t.Errorf(message+
2733 "\nactual: %v"+
2734 "\nexpected: %v",
2735 actual,
2736 expected,
2737 )
2738 }
2739}
2740
Jooyung Han61b66e92020-03-21 14:21:46 +00002741func TestLlndkLibrary(t *testing.T) {
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002742 result := prepareForCcTest.RunTestWithBp(t, `
2743 cc_library {
2744 name: "libllndk",
2745 stubs: { versions: ["1", "2"] },
2746 llndk: {
2747 symbol_file: "libllndk.map.txt",
2748 },
2749 export_include_dirs: ["include"],
2750 }
2751
2752 cc_prebuilt_library_shared {
2753 name: "libllndkprebuilt",
2754 stubs: { versions: ["1", "2"] },
2755 llndk: {
2756 symbol_file: "libllndkprebuilt.map.txt",
2757 },
2758 }
2759
2760 cc_library {
2761 name: "libllndk_with_external_headers",
2762 stubs: { versions: ["1", "2"] },
2763 llndk: {
2764 symbol_file: "libllndk.map.txt",
2765 export_llndk_headers: ["libexternal_llndk_headers"],
2766 },
2767 header_libs: ["libexternal_headers"],
2768 export_header_lib_headers: ["libexternal_headers"],
2769 }
2770 cc_library_headers {
2771 name: "libexternal_headers",
2772 export_include_dirs: ["include"],
2773 vendor_available: true,
2774 }
2775 cc_library_headers {
2776 name: "libexternal_llndk_headers",
2777 export_include_dirs: ["include_llndk"],
2778 llndk: {
2779 symbol_file: "libllndk.map.txt",
2780 },
2781 vendor_available: true,
2782 }
2783
2784 cc_library {
2785 name: "libllndk_with_override_headers",
2786 stubs: { versions: ["1", "2"] },
2787 llndk: {
2788 symbol_file: "libllndk.map.txt",
2789 override_export_include_dirs: ["include_llndk"],
2790 },
2791 export_include_dirs: ["include"],
2792 }
2793 `)
2794 actual := result.ModuleVariantsForTests("libllndk")
2795 for i := 0; i < len(actual); i++ {
2796 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2797 actual = append(actual[:i], actual[i+1:]...)
2798 i--
2799 }
2800 }
2801 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002802 "android_vendor.29_arm64_armv8-a_shared_current",
2803 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002804 "android_vendor.29_arm_armv7-a-neon_shared_current",
2805 "android_vendor.29_arm_armv7-a-neon_shared",
2806 }
2807 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2808
2809 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2810 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2811
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002812 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2813 t.Helper()
2814 m := result.ModuleForTests(module, variant).Module()
2815 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2816 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2817 expectedDirs, f.IncludeDirs)
2818 }
2819
2820 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2821 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2822 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2823 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2824 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2825 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2826}
2827
Jiyong Parka46a4d52017-12-14 19:54:34 +09002828func TestLlndkHeaders(t *testing.T) {
2829 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002830 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002831 name: "libllndk_headers",
2832 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002833 llndk: {
2834 llndk_headers: true,
2835 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002836 }
2837 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002838 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002839 llndk: {
2840 symbol_file: "libllndk.map.txt",
2841 export_llndk_headers: ["libllndk_headers"],
2842 }
Colin Cross0477b422020-10-13 18:43:54 -07002843 }
2844
2845 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002846 name: "libvendor",
2847 shared_libs: ["libllndk"],
2848 vendor: true,
2849 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002850 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002851 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002852 }
2853 `)
2854
2855 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002856 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002857 cflags := cc.Args["cFlags"]
2858 if !strings.Contains(cflags, "-Imy_include") {
2859 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2860 }
2861}
2862
Logan Chien43d34c32017-12-20 01:17:32 +08002863func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2864 actual := module.Properties.AndroidMkRuntimeLibs
2865 if !reflect.DeepEqual(actual, expected) {
2866 t.Errorf("incorrect runtime_libs for shared libs"+
2867 "\nactual: %v"+
2868 "\nexpected: %v",
2869 actual,
2870 expected,
2871 )
2872 }
2873}
2874
2875const runtimeLibAndroidBp = `
2876 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002877 name: "liball_available",
2878 vendor_available: true,
2879 product_available: true,
2880 no_libcrt : true,
2881 nocrt : true,
2882 system_shared_libs : [],
2883 }
2884 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002885 name: "libvendor_available1",
2886 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002887 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002888 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002889 nocrt : true,
2890 system_shared_libs : [],
2891 }
2892 cc_library {
2893 name: "libvendor_available2",
2894 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002895 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002896 target: {
2897 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002898 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002899 }
2900 },
Yi Konge7fe9912019-06-02 00:53:50 -07002901 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002902 nocrt : true,
2903 system_shared_libs : [],
2904 }
2905 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002906 name: "libproduct_vendor",
2907 product_specific: true,
2908 vendor_available: true,
2909 no_libcrt : true,
2910 nocrt : true,
2911 system_shared_libs : [],
2912 }
2913 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002914 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002915 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002916 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002917 nocrt : true,
2918 system_shared_libs : [],
2919 }
2920 cc_library {
2921 name: "libvendor1",
2922 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002923 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002924 nocrt : true,
2925 system_shared_libs : [],
2926 }
2927 cc_library {
2928 name: "libvendor2",
2929 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002930 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002931 no_libcrt : true,
2932 nocrt : true,
2933 system_shared_libs : [],
2934 }
2935 cc_library {
2936 name: "libproduct_available1",
2937 product_available: true,
2938 runtime_libs: ["liball_available"],
2939 no_libcrt : true,
2940 nocrt : true,
2941 system_shared_libs : [],
2942 }
2943 cc_library {
2944 name: "libproduct1",
2945 product_specific: true,
2946 no_libcrt : true,
2947 nocrt : true,
2948 system_shared_libs : [],
2949 }
2950 cc_library {
2951 name: "libproduct2",
2952 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002953 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002954 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002955 nocrt : true,
2956 system_shared_libs : [],
2957 }
2958`
2959
2960func TestRuntimeLibs(t *testing.T) {
2961 ctx := testCc(t, runtimeLibAndroidBp)
2962
2963 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002964 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002965
Justin Yun8a2600c2020-12-07 12:44:03 +09002966 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2967 checkRuntimeLibs(t, []string{"liball_available"}, module)
2968
2969 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2970 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002971
2972 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002973 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002974
2975 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2976 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002977 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002978
Justin Yun8a2600c2020-12-07 12:44:03 +09002979 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2980 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002981
2982 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002983 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002984
2985 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2986 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002987 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002988
2989 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2990 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2991
2992 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002993 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002994}
2995
2996func TestExcludeRuntimeLibs(t *testing.T) {
2997 ctx := testCc(t, runtimeLibAndroidBp)
2998
Colin Cross7113d202019-11-20 16:39:12 -08002999 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003000 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3001 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003002
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003003 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003004 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08003005 checkRuntimeLibs(t, nil, module)
3006}
3007
3008func TestRuntimeLibsNoVndk(t *testing.T) {
3009 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3010
3011 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3012
Colin Cross7113d202019-11-20 16:39:12 -08003013 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003014
Justin Yun8a2600c2020-12-07 12:44:03 +09003015 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3016 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003017
3018 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003019 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003020
3021 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003022 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003023}
3024
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003025func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003026 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003027 actual := module.Properties.AndroidMkStaticLibs
3028 if !reflect.DeepEqual(actual, expected) {
3029 t.Errorf("incorrect static_libs"+
3030 "\nactual: %v"+
3031 "\nexpected: %v",
3032 actual,
3033 expected,
3034 )
3035 }
3036}
3037
3038const staticLibAndroidBp = `
3039 cc_library {
3040 name: "lib1",
3041 }
3042 cc_library {
3043 name: "lib2",
3044 static_libs: ["lib1"],
3045 }
3046`
3047
3048func TestStaticLibDepExport(t *testing.T) {
3049 ctx := testCc(t, staticLibAndroidBp)
3050
3051 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003052 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003053 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Ryan Prichardc2018e22021-04-02 20:23:22 -07003054 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003055
3056 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003057 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003058 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3059 // libc++_static is linked additionally.
Ryan Prichardc2018e22021-04-02 20:23:22 -07003060 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003061}
3062
Jiyong Parkd08b6972017-09-26 10:50:54 +09003063var compilerFlagsTestCases = []struct {
3064 in string
3065 out bool
3066}{
3067 {
3068 in: "a",
3069 out: false,
3070 },
3071 {
3072 in: "-a",
3073 out: true,
3074 },
3075 {
3076 in: "-Ipath/to/something",
3077 out: false,
3078 },
3079 {
3080 in: "-isystempath/to/something",
3081 out: false,
3082 },
3083 {
3084 in: "--coverage",
3085 out: false,
3086 },
3087 {
3088 in: "-include a/b",
3089 out: true,
3090 },
3091 {
3092 in: "-include a/b c/d",
3093 out: false,
3094 },
3095 {
3096 in: "-DMACRO",
3097 out: true,
3098 },
3099 {
3100 in: "-DMAC RO",
3101 out: false,
3102 },
3103 {
3104 in: "-a -b",
3105 out: false,
3106 },
3107 {
3108 in: "-DMACRO=definition",
3109 out: true,
3110 },
3111 {
3112 in: "-DMACRO=defi nition",
3113 out: true, // TODO(jiyong): this should be false
3114 },
3115 {
3116 in: "-DMACRO(x)=x + 1",
3117 out: true,
3118 },
3119 {
3120 in: "-DMACRO=\"defi nition\"",
3121 out: true,
3122 },
3123}
3124
3125type mockContext struct {
3126 BaseModuleContext
3127 result bool
3128}
3129
3130func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3131 // CheckBadCompilerFlags calls this function when the flag should be rejected
3132 ctx.result = false
3133}
3134
3135func TestCompilerFlags(t *testing.T) {
3136 for _, testCase := range compilerFlagsTestCases {
3137 ctx := &mockContext{result: true}
3138 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3139 if ctx.result != testCase.out {
3140 t.Errorf("incorrect output:")
3141 t.Errorf(" input: %#v", testCase.in)
3142 t.Errorf(" expected: %#v", testCase.out)
3143 t.Errorf(" got: %#v", ctx.result)
3144 }
3145 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003146}
Jiyong Park374510b2018-03-19 18:23:01 +09003147
Jiyong Park37b25202018-07-11 10:49:27 +09003148func TestRecovery(t *testing.T) {
3149 ctx := testCc(t, `
3150 cc_library_shared {
3151 name: "librecovery",
3152 recovery: true,
3153 }
3154 cc_library_shared {
3155 name: "librecovery32",
3156 recovery: true,
3157 compile_multilib:"32",
3158 }
Jiyong Park5baac542018-08-28 09:55:37 +09003159 cc_library_shared {
3160 name: "libHalInRecovery",
3161 recovery_available: true,
3162 vendor: true,
3163 }
Jiyong Park37b25202018-07-11 10:49:27 +09003164 `)
3165
3166 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003167 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003168 if len(variants) != 1 || !android.InList(arm64, variants) {
3169 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3170 }
3171
3172 variants = ctx.ModuleVariantsForTests("librecovery32")
3173 if android.InList(arm64, variants) {
3174 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3175 }
Jiyong Park5baac542018-08-28 09:55:37 +09003176
3177 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3178 if !recoveryModule.Platform() {
3179 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3180 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003181}
Jiyong Park5baac542018-08-28 09:55:37 +09003182
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003183func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3184 bp := `
3185 cc_prebuilt_test_library_shared {
3186 name: "test_lib",
3187 relative_install_path: "foo/bar/baz",
3188 srcs: ["srcpath/dontusethispath/baz.so"],
3189 }
3190
3191 cc_test {
3192 name: "main_test",
3193 data_libs: ["test_lib"],
3194 gtest: false,
3195 }
3196 `
3197
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003198 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003199 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003200 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003201 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3202
3203 ctx := testCcWithConfig(t, config)
3204 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3205 testBinary := module.(*Module).linker.(*testBinary)
3206 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3207 if err != nil {
3208 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3209 }
3210 if len(outputFiles) != 1 {
3211 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3212 }
3213 if len(testBinary.dataPaths()) != 1 {
3214 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3215 }
3216
3217 outputPath := outputFiles[0].String()
3218
3219 if !strings.HasSuffix(outputPath, "/main_test") {
3220 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3221 }
Colin Crossaa255532020-07-03 13:18:24 -07003222 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003223 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3224 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3225 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3226 }
3227}
3228
Jiyong Park7ed9de32018-10-15 22:25:07 +09003229func TestVersionedStubs(t *testing.T) {
3230 ctx := testCc(t, `
3231 cc_library_shared {
3232 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003233 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003234 stubs: {
3235 symbol_file: "foo.map.txt",
3236 versions: ["1", "2", "3"],
3237 },
3238 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003239
Jiyong Park7ed9de32018-10-15 22:25:07 +09003240 cc_library_shared {
3241 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003242 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003243 shared_libs: ["libFoo#1"],
3244 }`)
3245
3246 variants := ctx.ModuleVariantsForTests("libFoo")
3247 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003248 "android_arm64_armv8-a_shared",
3249 "android_arm64_armv8-a_shared_1",
3250 "android_arm64_armv8-a_shared_2",
3251 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003252 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003253 "android_arm_armv7-a-neon_shared",
3254 "android_arm_armv7-a-neon_shared_1",
3255 "android_arm_armv7-a-neon_shared_2",
3256 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003257 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003258 }
3259 variantsMismatch := false
3260 if len(variants) != len(expectedVariants) {
3261 variantsMismatch = true
3262 } else {
3263 for _, v := range expectedVariants {
3264 if !inList(v, variants) {
3265 variantsMismatch = false
3266 }
3267 }
3268 }
3269 if variantsMismatch {
3270 t.Errorf("variants of libFoo expected:\n")
3271 for _, v := range expectedVariants {
3272 t.Errorf("%q\n", v)
3273 }
3274 t.Errorf(", but got:\n")
3275 for _, v := range variants {
3276 t.Errorf("%q\n", v)
3277 }
3278 }
3279
Colin Cross7113d202019-11-20 16:39:12 -08003280 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003281 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003282 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003283 if !strings.Contains(libFlags, libFoo1StubPath) {
3284 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3285 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003286
Colin Cross7113d202019-11-20 16:39:12 -08003287 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003288 cFlags := libBarCompileRule.Args["cFlags"]
3289 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3290 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3291 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3292 }
Jiyong Park37b25202018-07-11 10:49:27 +09003293}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003294
Jooyung Hanb04a4992020-03-13 18:57:35 +09003295func TestVersioningMacro(t *testing.T) {
3296 for _, tc := range []struct{ moduleName, expected string }{
3297 {"libc", "__LIBC_API__"},
3298 {"libfoo", "__LIBFOO_API__"},
3299 {"libfoo@1", "__LIBFOO_1_API__"},
3300 {"libfoo-v1", "__LIBFOO_V1_API__"},
3301 {"libfoo.v1", "__LIBFOO_V1_API__"},
3302 } {
3303 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3304 }
3305}
3306
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003307func TestStaticExecutable(t *testing.T) {
3308 ctx := testCc(t, `
3309 cc_binary {
3310 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003311 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003312 static_executable: true,
3313 }`)
3314
Colin Cross7113d202019-11-20 16:39:12 -08003315 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003316 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3317 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003318 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003319 for _, lib := range systemStaticLibs {
3320 if !strings.Contains(libFlags, lib) {
3321 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3322 }
3323 }
3324 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3325 for _, lib := range systemSharedLibs {
3326 if strings.Contains(libFlags, lib) {
3327 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3328 }
3329 }
3330}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003331
3332func TestStaticDepsOrderWithStubs(t *testing.T) {
3333 ctx := testCc(t, `
3334 cc_binary {
3335 name: "mybin",
3336 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003337 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003338 static_executable: true,
3339 stl: "none",
3340 }
3341
3342 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003343 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003344 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003345 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003346 stl: "none",
3347 }
3348
3349 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003350 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003351 srcs: ["foo.c"],
3352 stl: "none",
3353 stubs: {
3354 versions: ["1"],
3355 },
3356 }`)
3357
Colin Cross0de8a1e2020-09-18 14:15:30 -07003358 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3359 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003360 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003361
3362 if !reflect.DeepEqual(actual, expected) {
3363 t.Errorf("staticDeps orderings were not propagated correctly"+
3364 "\nactual: %v"+
3365 "\nexpected: %v",
3366 actual,
3367 expected,
3368 )
3369 }
3370}
Jooyung Han38002912019-05-16 04:01:54 +09003371
Jooyung Hand48f3c32019-08-23 11:18:57 +09003372func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3373 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3374 cc_library {
3375 name: "libA",
3376 srcs: ["foo.c"],
3377 shared_libs: ["libB"],
3378 stl: "none",
3379 }
3380
3381 cc_library {
3382 name: "libB",
3383 srcs: ["foo.c"],
3384 enabled: false,
3385 stl: "none",
3386 }
3387 `)
3388}
3389
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003390// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3391// correctly.
3392func TestFuzzTarget(t *testing.T) {
3393 ctx := testCc(t, `
3394 cc_fuzz {
3395 name: "fuzz_smoke_test",
3396 srcs: ["foo.c"],
3397 }`)
3398
Paul Duffin075c4172019-12-19 19:06:13 +00003399 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003400 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3401}
3402
Jiyong Park29074592019-07-07 16:27:47 +09003403func TestAidl(t *testing.T) {
3404}
3405
Jooyung Han38002912019-05-16 04:01:54 +09003406func assertString(t *testing.T, got, expected string) {
3407 t.Helper()
3408 if got != expected {
3409 t.Errorf("expected %q got %q", expected, got)
3410 }
3411}
3412
3413func assertArrayString(t *testing.T, got, expected []string) {
3414 t.Helper()
3415 if len(got) != len(expected) {
3416 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3417 return
3418 }
3419 for i := range got {
3420 if got[i] != expected[i] {
3421 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3422 i, expected[i], expected, got[i], got)
3423 return
3424 }
3425 }
3426}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003427
Jooyung Han0302a842019-10-30 18:43:49 +09003428func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3429 t.Helper()
3430 assertArrayString(t, android.SortedStringKeys(m), expected)
3431}
3432
Colin Crosse1bb5d02019-09-24 14:55:04 -07003433func TestDefaults(t *testing.T) {
3434 ctx := testCc(t, `
3435 cc_defaults {
3436 name: "defaults",
3437 srcs: ["foo.c"],
3438 static: {
3439 srcs: ["bar.c"],
3440 },
3441 shared: {
3442 srcs: ["baz.c"],
3443 },
Liz Kammer3cf52112021-03-31 15:42:03 -04003444 bazel_module: {
3445 bp2build_available: true,
3446 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07003447 }
3448
3449 cc_library_static {
3450 name: "libstatic",
3451 defaults: ["defaults"],
3452 }
3453
3454 cc_library_shared {
3455 name: "libshared",
3456 defaults: ["defaults"],
3457 }
3458
3459 cc_library {
3460 name: "libboth",
3461 defaults: ["defaults"],
3462 }
3463
3464 cc_binary {
3465 name: "binary",
3466 defaults: ["defaults"],
3467 }`)
3468
3469 pathsToBase := func(paths android.Paths) []string {
3470 var ret []string
3471 for _, p := range paths {
3472 ret = append(ret, p.Base())
3473 }
3474 return ret
3475 }
3476
Colin Cross7113d202019-11-20 16:39:12 -08003477 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003478 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3479 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3480 }
Colin Cross7113d202019-11-20 16:39:12 -08003481 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003482 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3483 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3484 }
Colin Cross7113d202019-11-20 16:39:12 -08003485 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003486 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3487 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3488 }
3489
Colin Cross7113d202019-11-20 16:39:12 -08003490 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003491 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3492 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3493 }
Colin Cross7113d202019-11-20 16:39:12 -08003494 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003495 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3496 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3497 }
3498}
Colin Crosseabaedd2020-02-06 17:01:55 -08003499
3500func TestProductVariableDefaults(t *testing.T) {
3501 bp := `
3502 cc_defaults {
3503 name: "libfoo_defaults",
3504 srcs: ["foo.c"],
3505 cppflags: ["-DFOO"],
3506 product_variables: {
3507 debuggable: {
3508 cppflags: ["-DBAR"],
3509 },
3510 },
3511 }
3512
3513 cc_library {
3514 name: "libfoo",
3515 defaults: ["libfoo_defaults"],
3516 }
3517 `
3518
Paul Duffin8567f222021-03-23 00:02:06 +00003519 result := android.GroupFixturePreparers(
3520 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003521 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003522
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003523 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3524 variables.Debuggable = BoolPtr(true)
3525 }),
3526 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003527
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003528 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003529 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003530}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003531
3532func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3533 t.Parallel()
3534 bp := `
3535 cc_library_static {
3536 name: "libfoo",
3537 srcs: ["foo.c"],
3538 whole_static_libs: ["libbar"],
3539 }
3540
3541 cc_library_static {
3542 name: "libbar",
3543 whole_static_libs: ["libmissing"],
3544 }
3545 `
3546
Paul Duffin8567f222021-03-23 00:02:06 +00003547 result := android.GroupFixturePreparers(
3548 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003549 android.PrepareForTestWithAllowMissingDependencies,
3550 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003551
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003552 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003553 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003554
Paul Duffine84b1332021-03-12 11:59:43 +00003555 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003556
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003557 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003558 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003559}
Colin Crosse9fe2942020-11-10 18:12:15 -08003560
3561func TestInstallSharedLibs(t *testing.T) {
3562 bp := `
3563 cc_binary {
3564 name: "bin",
3565 host_supported: true,
3566 shared_libs: ["libshared"],
3567 runtime_libs: ["libruntime"],
3568 srcs: [":gen"],
3569 }
3570
3571 cc_library_shared {
3572 name: "libshared",
3573 host_supported: true,
3574 shared_libs: ["libtransitive"],
3575 }
3576
3577 cc_library_shared {
3578 name: "libtransitive",
3579 host_supported: true,
3580 }
3581
3582 cc_library_shared {
3583 name: "libruntime",
3584 host_supported: true,
3585 }
3586
3587 cc_binary_host {
3588 name: "tool",
3589 srcs: ["foo.cpp"],
3590 }
3591
3592 genrule {
3593 name: "gen",
3594 tools: ["tool"],
3595 out: ["gen.cpp"],
3596 cmd: "$(location tool) $(out)",
3597 }
3598 `
3599
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003600 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003601 ctx := testCcWithConfig(t, config)
3602
3603 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3604 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3605 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3606 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3607 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3608
3609 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3610 t.Errorf("expected host bin dependency %q, got %q", w, g)
3611 }
3612
3613 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3614 t.Errorf("expected host bin dependency %q, got %q", w, g)
3615 }
3616
3617 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3618 t.Errorf("expected host bin dependency %q, got %q", w, g)
3619 }
3620
3621 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3622 t.Errorf("expected host bin dependency %q, got %q", w, g)
3623 }
3624
3625 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3626 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3627 }
3628
3629 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3630 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3631 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3632 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3633
3634 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3635 t.Errorf("expected device bin dependency %q, got %q", w, g)
3636 }
3637
3638 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3639 t.Errorf("expected device bin dependency %q, got %q", w, g)
3640 }
3641
3642 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3643 t.Errorf("expected device bin dependency %q, got %q", w, g)
3644 }
3645
3646 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3647 t.Errorf("expected device bin dependency %q, got %q", w, g)
3648 }
3649
3650 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3651 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3652 }
3653
3654}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003655
3656func TestStubsLibReexportsHeaders(t *testing.T) {
3657 ctx := testCc(t, `
3658 cc_library_shared {
3659 name: "libclient",
3660 srcs: ["foo.c"],
3661 shared_libs: ["libfoo#1"],
3662 }
3663
3664 cc_library_shared {
3665 name: "libfoo",
3666 srcs: ["foo.c"],
3667 shared_libs: ["libbar"],
3668 export_shared_lib_headers: ["libbar"],
3669 stubs: {
3670 symbol_file: "foo.map.txt",
3671 versions: ["1", "2", "3"],
3672 },
3673 }
3674
3675 cc_library_shared {
3676 name: "libbar",
3677 export_include_dirs: ["include/libbar"],
3678 srcs: ["foo.c"],
3679 }`)
3680
3681 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3682
3683 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3684 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3685 }
3686}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003687
3688func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3689 ctx := testCc(t, `
3690 cc_library {
3691 name: "libfoo",
3692 srcs: ["a/Foo.aidl"],
3693 aidl: { flags: ["-Werror"], },
3694 }
3695 `)
3696
3697 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3698 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3699 aidlCommand := manifest.Commands[0].GetCommand()
3700 expectedAidlFlag := "-Werror"
3701 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3702 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3703 }
3704}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003705
Jiyong Parka008fb02021-03-16 17:15:53 +09003706func TestMinSdkVersionInClangTriple(t *testing.T) {
3707 ctx := testCc(t, `
3708 cc_library_shared {
3709 name: "libfoo",
3710 srcs: ["foo.c"],
3711 min_sdk_version: "29",
3712 }`)
3713
3714 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3715 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3716}
3717
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003718type MemtagNoteType int
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003719
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003720const (
3721 None MemtagNoteType = iota + 1
3722 Sync
3723 Async
3724)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003725
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003726func (t MemtagNoteType) str() string {
3727 switch t {
3728 case None:
3729 return "none"
3730 case Sync:
3731 return "sync"
3732 case Async:
3733 return "async"
3734 default:
3735 panic("invalid note type")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003736 }
3737}
3738
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003739func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
3740 note_async := "note_memtag_heap_async"
3741 note_sync := "note_memtag_heap_sync"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003742
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003743 found := None
3744 implicits := m.Rule("ld").Implicits
3745 for _, lib := range implicits {
3746 if strings.Contains(lib.Rel(), note_async) {
3747 found = Async
3748 break
3749 } else if strings.Contains(lib.Rel(), note_sync) {
3750 found = Sync
3751 break
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003752 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003753 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003754
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003755 if found != expected {
3756 t.Errorf("Wrong Memtag note in target %q: found %q, expected %q", m.Module().(*Module).Name(), found.str(), expected.str())
3757 }
3758}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003759
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003760var prepareForTestWithMemtagHeap = android.GroupFixturePreparers(
3761 android.FixtureModifyMockFS(func(fs android.MockFS) {
3762 templateBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003763 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003764 name: "%[1]s_test",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003765 gtest: false,
3766 }
3767
3768 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003769 name: "%[1]s_test_false",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003770 gtest: false,
3771 sanitize: { memtag_heap: false },
3772 }
3773
3774 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003775 name: "%[1]s_test_true",
3776 gtest: false,
3777 sanitize: { memtag_heap: true },
3778 }
3779
3780 cc_test {
3781 name: "%[1]s_test_true_nodiag",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003782 gtest: false,
3783 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3784 }
3785
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003786 cc_test {
3787 name: "%[1]s_test_true_diag",
3788 gtest: false,
3789 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
3790 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003791
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003792 cc_binary {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003793 name: "%[1]s_binary",
3794 }
3795
3796 cc_binary {
3797 name: "%[1]s_binary_false",
3798 sanitize: { memtag_heap: false },
3799 }
3800
3801 cc_binary {
3802 name: "%[1]s_binary_true",
3803 sanitize: { memtag_heap: true },
3804 }
3805
3806 cc_binary {
3807 name: "%[1]s_binary_true_nodiag",
3808 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3809 }
3810
3811 cc_binary {
3812 name: "%[1]s_binary_true_diag",
3813 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003814 }
3815 `
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003816 subdirDefaultBp := fmt.Sprintf(templateBp, "default")
3817 subdirExcludeBp := fmt.Sprintf(templateBp, "exclude")
3818 subdirSyncBp := fmt.Sprintf(templateBp, "sync")
3819 subdirAsyncBp := fmt.Sprintf(templateBp, "async")
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003820
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003821 fs.Merge(android.MockFS{
3822 "subdir_default/Android.bp": []byte(subdirDefaultBp),
3823 "subdir_exclude/Android.bp": []byte(subdirExcludeBp),
3824 "subdir_sync/Android.bp": []byte(subdirSyncBp),
3825 "subdir_async/Android.bp": []byte(subdirAsyncBp),
3826 })
3827 }),
3828 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3829 variables.MemtagHeapExcludePaths = []string{"subdir_exclude"}
Evgenii Stepanov779b64e2021-04-09 14:33:10 -07003830 // "subdir_exclude" is covered by both include and exclude paths. Exclude wins.
3831 variables.MemtagHeapSyncIncludePaths = []string{"subdir_sync", "subdir_exclude"}
3832 variables.MemtagHeapAsyncIncludePaths = []string{"subdir_async", "subdir_exclude"}
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003833 }),
3834)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003835
3836func TestSanitizeMemtagHeap(t *testing.T) {
3837 variant := "android_arm64_armv8-a"
3838
Paul Duffin8567f222021-03-23 00:02:06 +00003839 result := android.GroupFixturePreparers(
3840 prepareForCcTest,
3841 prepareForTestWithMemtagHeap,
3842 ).RunTest(t)
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003843 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003844
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003845 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3846 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3847 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3848 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3849 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3850
3851 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), None)
3852 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3853 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3854 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3855 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3856
3857 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3858 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3859 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3860 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3861 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3862
3863 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3864 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3865 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3866 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3867 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3868
3869 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3870 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3871 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3872 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3873 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3874
3875 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3876 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3877 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3878 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3879 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3880
3881 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3882 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3883 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3884 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3885 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3886
3887 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3888 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3889 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3890 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3891 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3892}
3893
3894func TestSanitizeMemtagHeapWithSanitizeDevice(t *testing.T) {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003895 variant := "android_arm64_armv8-a"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003896
Paul Duffin8567f222021-03-23 00:02:06 +00003897 result := android.GroupFixturePreparers(
3898 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003899 prepareForTestWithMemtagHeap,
3900 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3901 variables.SanitizeDevice = []string{"memtag_heap"}
3902 }),
3903 ).RunTest(t)
3904 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003905
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003906 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3907 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3908 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3909 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3910 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003911
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003912 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Async)
3913 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3914 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3915 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3916 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3917
3918 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3919 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3920 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3921 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3922 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3923
3924 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3925 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3926 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3927 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3928 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3929
3930 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3931 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3932 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3933 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3934 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3935
3936 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3937 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3938 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3939 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3940 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3941
3942 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3943 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3944 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3945 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3946 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3947
3948 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3949 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3950 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3951 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3952 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3953}
3954
3955func TestSanitizeMemtagHeapWithSanitizeDeviceDiag(t *testing.T) {
3956 variant := "android_arm64_armv8-a"
3957
Paul Duffin8567f222021-03-23 00:02:06 +00003958 result := android.GroupFixturePreparers(
3959 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003960 prepareForTestWithMemtagHeap,
3961 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3962 variables.SanitizeDevice = []string{"memtag_heap"}
3963 variables.SanitizeDeviceDiag = []string{"memtag_heap"}
3964 }),
3965 ).RunTest(t)
3966 ctx := result.TestContext
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003967
3968 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3969 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3970 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Sync)
3971 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3972 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3973
3974 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Sync)
3975 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3976 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Sync)
3977 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3978 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3979
3980 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3981 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3982 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Sync)
3983 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3984 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3985
3986 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3987 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3988 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Sync)
3989 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3990 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3991
3992 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3993 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3994 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Sync)
3995 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3996 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3997
3998 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Sync)
3999 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
4000 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Sync)
4001 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
4002 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
4003
4004 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
4005 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
4006 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
4007 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
4008 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
4009
4010 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
4011 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
4012 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
4013 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
4014 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004015}
Paul Duffin3cb603e2021-02-19 13:57:10 +00004016
4017func TestIncludeDirsExporting(t *testing.T) {
4018
4019 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4020 // embedded newline characters alone.
4021 trimIndentingSpaces := func(s string) string {
4022 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4023 }
4024
4025 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4026 t.Helper()
4027 expected = trimIndentingSpaces(expected)
4028 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4029 if expected != actual {
4030 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4031 }
4032 }
4033
4034 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4035
4036 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4037 t.Helper()
4038 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4039 name := module.Name()
4040
4041 for _, checker := range checkers {
4042 checker(t, name, exported)
4043 }
4044 }
4045
4046 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4047 return func(t *testing.T, name string, exported FlagExporterInfo) {
4048 t.Helper()
4049 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4050 }
4051 }
4052
4053 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4054 return func(t *testing.T, name string, exported FlagExporterInfo) {
4055 t.Helper()
4056 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4057 }
4058 }
4059
4060 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4061 return func(t *testing.T, name string, exported FlagExporterInfo) {
4062 t.Helper()
4063 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4064 }
4065 }
4066
4067 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4068 return func(t *testing.T, name string, exported FlagExporterInfo) {
4069 t.Helper()
4070 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4071 }
4072 }
4073
4074 genRuleModules := `
4075 genrule {
4076 name: "genrule_foo",
4077 cmd: "generate-foo",
4078 out: [
4079 "generated_headers/foo/generated_header.h",
4080 ],
4081 export_include_dirs: [
4082 "generated_headers",
4083 ],
4084 }
4085
4086 genrule {
4087 name: "genrule_bar",
4088 cmd: "generate-bar",
4089 out: [
4090 "generated_headers/bar/generated_header.h",
4091 ],
4092 export_include_dirs: [
4093 "generated_headers",
4094 ],
4095 }
4096 `
4097
4098 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4099 ctx := testCc(t, genRuleModules+`
4100 cc_library {
4101 name: "libfoo",
4102 srcs: ["foo.c"],
4103 export_include_dirs: ["foo/standard"],
4104 export_system_include_dirs: ["foo/system"],
4105 generated_headers: ["genrule_foo"],
4106 export_generated_headers: ["genrule_foo"],
4107 }
4108
4109 cc_library {
4110 name: "libbar",
4111 srcs: ["bar.c"],
4112 shared_libs: ["libfoo"],
4113 export_include_dirs: ["bar/standard"],
4114 export_system_include_dirs: ["bar/system"],
4115 generated_headers: ["genrule_bar"],
4116 export_generated_headers: ["genrule_bar"],
4117 }
4118 `)
4119 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4120 checkIncludeDirs(t, ctx, foo,
4121 expectedIncludeDirs(`
4122 foo/standard
4123 .intermediates/genrule_foo/gen/generated_headers
4124 `),
4125 expectedSystemIncludeDirs(`foo/system`),
4126 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4127 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4128 )
4129
4130 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4131 checkIncludeDirs(t, ctx, bar,
4132 expectedIncludeDirs(`
4133 bar/standard
4134 .intermediates/genrule_bar/gen/generated_headers
4135 `),
4136 expectedSystemIncludeDirs(`bar/system`),
4137 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4138 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4139 )
4140 })
4141
4142 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4143 ctx := testCc(t, genRuleModules+`
4144 cc_library {
4145 name: "libfoo",
4146 srcs: ["foo.c"],
4147 export_include_dirs: ["foo/standard"],
4148 export_system_include_dirs: ["foo/system"],
4149 generated_headers: ["genrule_foo"],
4150 export_generated_headers: ["genrule_foo"],
4151 }
4152
4153 cc_library {
4154 name: "libbar",
4155 srcs: ["bar.c"],
4156 whole_static_libs: ["libfoo"],
4157 export_include_dirs: ["bar/standard"],
4158 export_system_include_dirs: ["bar/system"],
4159 generated_headers: ["genrule_bar"],
4160 export_generated_headers: ["genrule_bar"],
4161 }
4162 `)
4163 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4164 checkIncludeDirs(t, ctx, foo,
4165 expectedIncludeDirs(`
4166 foo/standard
4167 .intermediates/genrule_foo/gen/generated_headers
4168 `),
4169 expectedSystemIncludeDirs(`foo/system`),
4170 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4171 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4172 )
4173
4174 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4175 checkIncludeDirs(t, ctx, bar,
4176 expectedIncludeDirs(`
4177 bar/standard
4178 foo/standard
4179 .intermediates/genrule_foo/gen/generated_headers
4180 .intermediates/genrule_bar/gen/generated_headers
4181 `),
4182 expectedSystemIncludeDirs(`
4183 bar/system
4184 foo/system
4185 `),
4186 expectedGeneratedHeaders(`
4187 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4188 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4189 `),
4190 expectedOrderOnlyDeps(`
4191 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4192 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4193 `),
4194 )
4195 })
4196
Paul Duffin3cb603e2021-02-19 13:57:10 +00004197 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4198 ctx := testCc(t, genRuleModules+`
4199 cc_library_shared {
4200 name: "libfoo",
4201 srcs: [
4202 "foo.c",
4203 "b.aidl",
4204 "a.proto",
4205 ],
4206 aidl: {
4207 export_aidl_headers: true,
4208 }
4209 }
4210 `)
4211 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4212 checkIncludeDirs(t, ctx, foo,
4213 expectedIncludeDirs(`
4214 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4215 `),
4216 expectedSystemIncludeDirs(``),
4217 expectedGeneratedHeaders(`
4218 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4219 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4220 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004221 `),
4222 expectedOrderOnlyDeps(`
4223 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4224 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4225 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004226 `),
4227 )
4228 })
4229
Paul Duffin3cb603e2021-02-19 13:57:10 +00004230 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4231 ctx := testCc(t, genRuleModules+`
4232 cc_library_shared {
4233 name: "libfoo",
4234 srcs: [
4235 "foo.c",
4236 "b.aidl",
4237 "a.proto",
4238 ],
4239 proto: {
4240 export_proto_headers: true,
4241 }
4242 }
4243 `)
4244 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4245 checkIncludeDirs(t, ctx, foo,
4246 expectedIncludeDirs(`
4247 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4248 `),
4249 expectedSystemIncludeDirs(``),
4250 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004251 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4252 `),
4253 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004254 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4255 `),
4256 )
4257 })
4258
Paul Duffin33056e82021-02-19 13:49:08 +00004259 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004260 ctx := testCc(t, genRuleModules+`
4261 cc_library_shared {
4262 name: "libfoo",
4263 srcs: [
4264 "foo.c",
4265 "a.sysprop",
4266 "b.aidl",
4267 "a.proto",
4268 ],
4269 }
4270 `)
4271 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4272 checkIncludeDirs(t, ctx, foo,
4273 expectedIncludeDirs(`
4274 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4275 `),
4276 expectedSystemIncludeDirs(``),
4277 expectedGeneratedHeaders(`
4278 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004279 `),
4280 expectedOrderOnlyDeps(`
4281 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
4282 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004283 `),
4284 )
4285 })
4286}