blob: 26d1f8e8857d367c9db425baa4fef7baf830f41d [file] [log] [blame]
Colin Crossd00350c2017-11-17 10:55:38 -08001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross74d1ec02015-04-28 13:30:13 -070015package cc
16
17import (
Jeff Gaston294356f2017-09-27 17:05:30 -070018 "fmt"
Jiyong Park6a43f042017-10-12 23:05:00 +090019 "os"
Inseob Kim1f086e22019-05-09 13:29:15 +090020 "path/filepath"
Colin Cross74d1ec02015-04-28 13:30:13 -070021 "reflect"
Paul Duffin3cb603e2021-02-19 13:57:10 +000022 "regexp"
Jeff Gaston294356f2017-09-27 17:05:30 -070023 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070024 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070025
26 "android/soong/android"
Colin Cross74d1ec02015-04-28 13:30:13 -070027)
28
Jiyong Park6a43f042017-10-12 23:05:00 +090029func TestMain(m *testing.M) {
Paul Duffinc3e6ce02021-03-22 23:21:32 +000030 os.Exit(m.Run())
Jiyong Park6a43f042017-10-12 23:05:00 +090031}
32
Paul Duffin2e6f90e2021-03-22 23:20:25 +000033var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000034 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000035 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
36 variables.DeviceVndkVersion = StringPtr("current")
37 variables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090038 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000039 }),
40)
41
Paul Duffin8567f222021-03-23 00:02:06 +000042// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000043//
44// See testCc for an explanation as to how to stop using this deprecated method.
45//
46// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080047func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070048 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000049 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000050 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090051}
52
Paul Duffin8567f222021-03-23 00:02:06 +000053// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000054//
Paul Duffin8567f222021-03-23 00:02:06 +000055// Do not add any new usages of this, instead use the prepareForCcTest directly as it makes it much
Paul Duffin02a3d652021-02-24 18:51:54 +000056// easier to customize the test behavior.
57//
58// If it is necessary to customize the behavior of an existing test that uses this then please first
Paul Duffin8567f222021-03-23 00:02:06 +000059// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000060// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
61// that it did not change the test behavior unexpectedly.
62//
63// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080064func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080065 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000066 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +000067 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +080068}
69
Paul Duffin8567f222021-03-23 00:02:06 +000070// testCcNoVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000071//
72// See testCc for an explanation as to how to stop using this deprecated method.
73//
74// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080075func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080076 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +000077 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jiyong Parkf58c46e2021-04-01 21:35:20 +090078 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Logan Chienf3511742017-10-31 18:04:35 +080079
Colin Cross98be1bb2019-12-13 20:41:13 -080080 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080081}
82
Paul Duffin8567f222021-03-23 00:02:06 +000083// testCcNoProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000084//
85// See testCc for an explanation as to how to stop using this deprecated method.
86//
87// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +090088func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
89 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +000090 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun8a2600c2020-12-07 12:44:03 +090091 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090092 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun8a2600c2020-12-07 12:44:03 +090093
94 return testCcWithConfig(t, config)
95}
96
Paul Duffin8567f222021-03-23 00:02:06 +000097// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000098//
99// See testCc for an explanation as to how to stop using this deprecated method.
100//
101// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900102func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800103 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800104
Paul Duffin8567f222021-03-23 00:02:06 +0000105 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000106 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
107 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800108}
109
Paul Duffin8567f222021-03-23 00:02:06 +0000110// testCcError runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000111//
112// See testCc for an explanation as to how to stop using this deprecated method.
113//
114// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900115func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900116 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000117 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900118 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900119 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900120 testCcErrorWithConfig(t, pattern, config)
121 return
122}
123
Paul Duffin8567f222021-03-23 00:02:06 +0000124// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000125//
126// See testCc for an explanation as to how to stop using this deprecated method.
127//
128// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900129func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900130 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000131 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900132 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
133 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900134 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900135 testCcErrorWithConfig(t, pattern, config)
136 return
137}
138
Logan Chienf3511742017-10-31 18:04:35 +0800139const (
Colin Cross7113d202019-11-20 16:39:12 -0800140 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900141 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
142 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800143 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800144)
145
Paul Duffindb462dd2021-03-21 22:01:55 +0000146// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
147// running it in a fixture that requires all source files to exist.
148func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
149 android.GroupFixturePreparers(
150 PrepareForTestWithCcDefaultModules,
151 android.PrepareForTestDisallowNonExistentPaths,
152 ).RunTest(t)
153}
154
Doug Hornc32c6b02019-01-17 14:44:05 -0800155func TestFuchsiaDeps(t *testing.T) {
156 t.Helper()
157
158 bp := `
159 cc_library {
160 name: "libTest",
161 srcs: ["foo.c"],
162 target: {
163 fuchsia: {
164 srcs: ["bar.c"],
165 },
166 },
167 }`
168
Paul Duffin8567f222021-03-23 00:02:06 +0000169 result := android.GroupFixturePreparers(
170 prepareForCcTest,
171 PrepareForTestOnFuchsia,
172 ).RunTestWithBp(t, bp)
Doug Hornc32c6b02019-01-17 14:44:05 -0800173
174 rt := false
175 fb := false
176
Paul Duffinecdac8a2021-02-24 19:18:42 +0000177 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800178 implicits := ld.Implicits
179 for _, lib := range implicits {
180 if strings.Contains(lib.Rel(), "libcompiler_rt") {
181 rt = true
182 }
183
184 if strings.Contains(lib.Rel(), "libbioniccompat") {
185 fb = true
186 }
187 }
188
189 if !rt || !fb {
190 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
191 }
192}
193
194func TestFuchsiaTargetDecl(t *testing.T) {
195 t.Helper()
196
197 bp := `
198 cc_library {
199 name: "libTest",
200 srcs: ["foo.c"],
201 target: {
202 fuchsia: {
203 srcs: ["bar.c"],
204 },
205 },
206 }`
207
Paul Duffin8567f222021-03-23 00:02:06 +0000208 result := android.GroupFixturePreparers(
209 prepareForCcTest,
210 PrepareForTestOnFuchsia,
211 ).RunTestWithBp(t, bp)
Paul Duffinecdac8a2021-02-24 19:18:42 +0000212 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800213 var objs []string
214 for _, o := range ld.Inputs {
215 objs = append(objs, o.Base())
216 }
Paul Duffine84b1332021-03-12 11:59:43 +0000217 android.AssertArrayString(t, "libTest inputs", []string{"foo.o", "bar.o"}, objs)
Doug Hornc32c6b02019-01-17 14:44:05 -0800218}
219
Jiyong Park6a43f042017-10-12 23:05:00 +0900220func TestVendorSrc(t *testing.T) {
221 ctx := testCc(t, `
222 cc_library {
223 name: "libTest",
224 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700225 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800226 nocrt: true,
227 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900228 vendor_available: true,
229 target: {
230 vendor: {
231 srcs: ["bar.c"],
232 },
233 },
234 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900235 `)
236
Logan Chienf3511742017-10-31 18:04:35 +0800237 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900238 var objs []string
239 for _, o := range ld.Inputs {
240 objs = append(objs, o.Base())
241 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800242 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900243 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
244 }
245}
246
Justin Yun7f99ec72021-04-12 13:19:28 +0900247func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
248 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
249 partitionDefined := false
250 checkPartition := func(specific bool, partition string) {
251 if specific {
252 if expected != partition && !partitionDefined {
253 // The variant is installed to the 'partition'
254 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
255 }
256 partitionDefined = true
257 } else {
258 // The variant is not installed to the 'partition'
259 if expected == partition {
260 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
261 }
262 }
263 }
264 socSpecific := func(m *Module) bool {
265 return m.SocSpecific() || m.socSpecificModuleContext()
266 }
267 deviceSpecific := func(m *Module) bool {
268 return m.DeviceSpecific() || m.deviceSpecificModuleContext()
269 }
270 productSpecific := func(m *Module) bool {
271 return m.ProductSpecific() || m.productSpecificModuleContext()
272 }
273 systemExtSpecific := func(m *Module) bool {
274 return m.SystemExtSpecific()
275 }
276 checkPartition(socSpecific(mod), "vendor")
277 checkPartition(deviceSpecific(mod), "odm")
278 checkPartition(productSpecific(mod), "product")
279 checkPartition(systemExtSpecific(mod), "system_ext")
280 if !partitionDefined && expected != "system" {
281 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
282 " but installed to system partition", variant, name, expected)
283 }
284}
285
286func TestInstallPartition(t *testing.T) {
287 t.Helper()
288 ctx := prepareForCcTest.RunTestWithBp(t, `
289 cc_library {
290 name: "libsystem",
291 }
292 cc_library {
293 name: "libsystem_ext",
294 system_ext_specific: true,
295 }
296 cc_library {
297 name: "libproduct",
298 product_specific: true,
299 }
300 cc_library {
301 name: "libvendor",
302 vendor: true,
303 }
304 cc_library {
305 name: "libodm",
306 device_specific: true,
307 }
308 cc_library {
309 name: "liball_available",
310 vendor_available: true,
311 product_available: true,
312 }
313 cc_library {
314 name: "libsystem_ext_all_available",
315 system_ext_specific: true,
316 vendor_available: true,
317 product_available: true,
318 }
319 cc_library {
320 name: "liball_available_odm",
321 odm_available: true,
322 product_available: true,
323 }
324 cc_library {
325 name: "libproduct_vendoravailable",
326 product_specific: true,
327 vendor_available: true,
328 }
329 cc_library {
330 name: "libproduct_odmavailable",
331 product_specific: true,
332 odm_available: true,
333 }
334 `).TestContext
335
336 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
337 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
338 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
339 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
340 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
341
342 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
343 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
344 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
345
346 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
347 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
348 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
349
350 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
351 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
352 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
353
354 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
355 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
356
357 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
358 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
359}
360
Logan Chienf3511742017-10-31 18:04:35 +0800361func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900362 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800363
Logan Chiend3c59a22018-03-29 14:08:15 +0800364 t.Helper()
365
Justin Yun0ecf0b22020-02-28 15:07:59 +0900366 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800367
368 // Check library properties.
369 lib, ok := mod.compiler.(*libraryDecorator)
370 if !ok {
371 t.Errorf("%q must have libraryDecorator", name)
372 } else if lib.baseInstaller.subDir != subDir {
373 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
374 lib.baseInstaller.subDir)
375 }
376
377 // Check VNDK properties.
378 if mod.vndkdep == nil {
379 t.Fatalf("%q must have `vndkdep`", name)
380 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700381 if !mod.IsVndk() {
382 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800383 }
384 if mod.isVndkSp() != isVndkSp {
385 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
386 }
387
388 // Check VNDK extension properties.
389 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500390 if mod.IsVndkExt() != isVndkExt {
391 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800392 }
393
394 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
395 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
396 }
397}
398
Jose Galmes0a942a02021-02-03 14:23:15 -0800399func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool, fake bool) {
Bill Peckham945441c2020-08-31 16:07:58 -0700400 t.Helper()
Paul Duffine8366da2021-03-24 10:40:38 +0000401 mod := ctx.ModuleForTests(moduleName, variant)
402 outputFiles := mod.OutputFiles(t, "")
403 if len(outputFiles) != 1 {
Jooyung Han39edb6c2019-11-06 16:53:07 +0900404 t.Errorf("%q must have single output\n", moduleName)
405 return
406 }
407 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900408
Bill Peckham945441c2020-08-31 16:07:58 -0700409 if include {
410 out := singleton.Output(snapshotPath)
Jose Galmes0a942a02021-02-03 14:23:15 -0800411 if fake {
412 if out.Rule == nil {
413 t.Errorf("Missing rule for module %q output file %q", moduleName, outputFiles[0])
414 }
415 } else {
416 if out.Input.String() != outputFiles[0].String() {
417 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
418 }
Bill Peckham945441c2020-08-31 16:07:58 -0700419 }
420 } else {
421 out := singleton.MaybeOutput(snapshotPath)
422 if out.Rule != nil {
423 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
424 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900425 }
426}
427
Bill Peckham945441c2020-08-31 16:07:58 -0700428func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000429 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800430 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, false)
Bill Peckham945441c2020-08-31 16:07:58 -0700431}
432
433func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000434 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800435 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false, false)
436}
437
438func checkSnapshotRule(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000439 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800440 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, true)
Bill Peckham945441c2020-08-31 16:07:58 -0700441}
442
Jooyung Han2216fb12019-11-06 16:46:15 +0900443func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
444 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800445 content := android.ContentFromFileRuleForTests(t, params)
446 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900447 assertArrayString(t, actual, expected)
448}
449
Jooyung Han097087b2019-10-22 19:32:18 +0900450func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
451 t.Helper()
452 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900453 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
454}
455
456func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
457 t.Helper()
Colin Cross78212242021-01-06 14:51:30 -0800458 got := ctx.ModuleForTests(module, "").Module().(*vndkLibrariesTxt).fileNames
459 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900460}
461
Logan Chienf3511742017-10-31 18:04:35 +0800462func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800463 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800464 cc_library {
465 name: "libvndk",
466 vendor_available: true,
467 vndk: {
468 enabled: true,
469 },
470 nocrt: true,
471 }
472
473 cc_library {
474 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900475 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800476 vndk: {
477 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900478 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800479 },
480 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900481 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800482 }
483
484 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900485 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800486 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900487 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800488 vndk: {
489 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900490 },
491 nocrt: true,
492 target: {
493 vendor: {
494 cflags: ["-DTEST"],
495 },
496 product: {
497 cflags: ["-DTEST"],
498 },
499 },
500 }
501
502 cc_library {
503 name: "libvndk_sp",
504 vendor_available: true,
505 vndk: {
506 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800507 support_system_process: true,
508 },
509 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900510 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800511 }
512
513 cc_library {
514 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900515 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800516 vndk: {
517 enabled: true,
518 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900519 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800520 },
521 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900522 target: {
523 vendor: {
524 suffix: "-x",
525 },
526 },
Logan Chienf3511742017-10-31 18:04:35 +0800527 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900528
529 cc_library {
530 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900531 vendor_available: true,
532 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900533 vndk: {
534 enabled: true,
535 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900536 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900537 },
538 nocrt: true,
539 target: {
540 vendor: {
541 suffix: "-x",
542 },
543 product: {
544 suffix: "-x",
545 },
546 },
547 }
548
Justin Yun450ae722021-04-16 19:58:18 +0900549 cc_library {
550 name: "libllndk",
551 llndk_stubs: "libllndk.llndk",
552 }
553
554 llndk_library {
555 name: "libllndk.llndk",
556 symbol_file: "",
557 export_llndk_headers: ["libllndk_headers"],
558 }
559
560 llndk_headers {
561 name: "libllndk_headers",
562 export_include_dirs: ["include"],
563 }
564
Colin Crosse4e44bc2020-12-28 13:50:21 -0800565 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900566 name: "llndk.libraries.txt",
567 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800568 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900569 name: "vndkcore.libraries.txt",
570 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800571 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900572 name: "vndksp.libraries.txt",
573 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800574 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900575 name: "vndkprivate.libraries.txt",
576 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800577 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900578 name: "vndkproduct.libraries.txt",
579 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800580 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900581 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800582 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900583 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800584 `
585
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000586 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800587 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900588 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900589 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800590
591 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800592
Jooyung Han261e1582020-10-20 18:54:21 +0900593 // subdir == "" because VNDK libs are not supposed to be installed separately.
594 // They are installed as part of VNDK APEX instead.
595 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
596 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900597 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900598 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
599 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900600 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900601
Justin Yun6977e8a2020-10-29 18:24:11 +0900602 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
603 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900604
Inseob Kim1f086e22019-05-09 13:29:15 +0900605 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900606 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000607 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900608
609 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
610 "arm64", "armv8-a"))
611 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
612 "arm", "armv7-a-neon"))
613
614 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
615 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900616 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
617
Inseob Kim1f086e22019-05-09 13:29:15 +0900618 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
619 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900620 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900621
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900622 variant := "android_vendor.29_arm64_armv8-a_shared"
623 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900624
Inseob Kim7f283f42020-06-01 21:53:49 +0900625 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
626
627 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
628 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900629 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
630 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900631 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
632 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Justin Yun450ae722021-04-16 19:58:18 +0900633 checkSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
634 checkSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900635
Jooyung Han39edb6c2019-11-06 16:53:07 +0900636 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900637 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
638 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
639 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
640 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900641 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900642
Jooyung Han097087b2019-10-22 19:32:18 +0900643 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
644 "LLNDK: libc.so",
645 "LLNDK: libdl.so",
646 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900647 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900648 "LLNDK: libm.so",
649 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900650 "VNDK-SP: libvndk_sp-x.so",
651 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900652 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900653 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900654 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900655 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900656 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900657 "VNDK-private: libvndk-private.so",
658 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900659 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900660 "VNDK-product: libc++.so",
661 "VNDK-product: libvndk_product.so",
662 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900663 })
Justin Yun450ae722021-04-16 19:58:18 +0900664 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900665 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
666 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
667 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
Justin Yun8a2600c2020-12-07 12:44:03 +0900668 checkVndkLibrariesOutput(t, ctx, "vndkproduct.libraries.txt", []string{"libc++.so", "libvndk_product.so", "libvndk_sp_product_private-x.so"})
Jooyung Han2216fb12019-11-06 16:46:15 +0900669 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
670}
671
Yo Chiangbba545e2020-06-09 16:15:37 +0800672func TestVndkWithHostSupported(t *testing.T) {
673 ctx := testCc(t, `
674 cc_library {
675 name: "libvndk_host_supported",
676 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900677 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800678 vndk: {
679 enabled: true,
680 },
681 host_supported: true,
682 }
683
684 cc_library {
685 name: "libvndk_host_supported_but_disabled_on_device",
686 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900687 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800688 vndk: {
689 enabled: true,
690 },
691 host_supported: true,
692 enabled: false,
693 target: {
694 host: {
695 enabled: true,
696 }
697 }
698 }
699
Colin Crosse4e44bc2020-12-28 13:50:21 -0800700 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800701 name: "vndkcore.libraries.txt",
702 }
703 `)
704
705 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
706}
707
Jooyung Han2216fb12019-11-06 16:46:15 +0900708func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800709 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800710 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900711 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800712 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800713 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000714 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800715 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900716 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800717 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900718
719 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Colin Crossaa255532020-07-03 13:18:24 -0700720 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900721 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900722}
723
724func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800725 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900726 cc_library {
727 name: "libvndk",
728 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900729 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900730 vndk: {
731 enabled: true,
732 },
733 nocrt: true,
734 }
735
736 cc_library {
737 name: "libvndk_sp",
738 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900739 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900740 vndk: {
741 enabled: true,
742 support_system_process: true,
743 },
744 nocrt: true,
745 }
746
747 cc_library {
748 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900749 vendor_available: true,
750 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900751 vndk: {
752 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900753 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900754 },
755 nocrt: true,
756 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900757
Colin Crosse4e44bc2020-12-28 13:50:21 -0800758 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900759 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800760 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900761 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800762 `
763
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000764 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800765 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900766 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800767 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
768
769 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
770
771 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900772
Jooyung Han2216fb12019-11-06 16:46:15 +0900773 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900774}
775
Chris Parsons79d66a52020-06-05 17:26:16 -0400776func TestDataLibs(t *testing.T) {
777 bp := `
778 cc_test_library {
779 name: "test_lib",
780 srcs: ["test_lib.cpp"],
781 gtest: false,
782 }
783
784 cc_test {
785 name: "main_test",
786 data_libs: ["test_lib"],
787 gtest: false,
788 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400789 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400790
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000791 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400792 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900793 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400794 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
795
796 ctx := testCcWithConfig(t, config)
797 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
798 testBinary := module.(*Module).linker.(*testBinary)
799 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
800 if err != nil {
801 t.Errorf("Expected cc_test to produce output files, error: %s", err)
802 return
803 }
804 if len(outputFiles) != 1 {
805 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
806 return
807 }
808 if len(testBinary.dataPaths()) != 1 {
809 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
810 return
811 }
812
813 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400814 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400815
816 if !strings.HasSuffix(outputPath, "/main_test") {
817 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
818 return
819 }
820 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
821 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
822 return
823 }
824}
825
Chris Parsons216e10a2020-07-09 17:12:52 -0400826func TestDataLibsRelativeInstallPath(t *testing.T) {
827 bp := `
828 cc_test_library {
829 name: "test_lib",
830 srcs: ["test_lib.cpp"],
831 relative_install_path: "foo/bar/baz",
832 gtest: false,
833 }
834
835 cc_test {
836 name: "main_test",
837 data_libs: ["test_lib"],
838 gtest: false,
839 }
840 `
841
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000842 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400843 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900844 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400845 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
846
847 ctx := testCcWithConfig(t, config)
848 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
849 testBinary := module.(*Module).linker.(*testBinary)
850 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
851 if err != nil {
852 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
853 }
854 if len(outputFiles) != 1 {
855 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
856 }
857 if len(testBinary.dataPaths()) != 1 {
858 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
859 }
860
861 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400862
863 if !strings.HasSuffix(outputPath, "/main_test") {
864 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
865 }
Colin Crossaa255532020-07-03 13:18:24 -0700866 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400867 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
868 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400869 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400870 }
871}
872
Jooyung Han0302a842019-10-30 18:43:49 +0900873func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900874 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900875 cc_library {
876 name: "libvndk",
877 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900878 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900879 vndk: {
880 enabled: true,
881 },
882 nocrt: true,
883 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900884 cc_library {
885 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900886 vendor_available: true,
887 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900888 vndk: {
889 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900890 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900891 },
892 nocrt: true,
893 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800894
895 cc_library {
896 name: "libllndk",
897 llndk_stubs: "libllndk.llndk",
898 }
899
900 llndk_library {
901 name: "libllndk.llndk",
902 symbol_file: "",
903 export_llndk_headers: ["libllndk_headers"],
904 }
905
906 llndk_headers {
907 name: "libllndk_headers",
908 export_include_dirs: ["include"],
909 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900910 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900911
912 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
913 "LLNDK: libc.so",
914 "LLNDK: libdl.so",
915 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800916 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900917 "LLNDK: libm.so",
918 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900919 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900920 "VNDK-core: libvndk.so",
921 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900922 "VNDK-private: libvndk-private.so",
923 "VNDK-product: libc++.so",
924 "VNDK-product: libvndk-private.so",
925 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900926 })
Logan Chienf3511742017-10-31 18:04:35 +0800927}
928
Justin Yun63e9ec72020-10-29 16:49:43 +0900929func TestVndkModuleError(t *testing.T) {
930 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900931 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900932 cc_library {
933 name: "libvndk",
934 vndk: {
935 enabled: true,
936 },
937 nocrt: true,
938 }
939 `)
940
Justin Yunc0d8c492021-01-07 17:45:31 +0900941 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900942 cc_library {
943 name: "libvndk",
944 product_available: true,
945 vndk: {
946 enabled: true,
947 },
948 nocrt: true,
949 }
950 `)
951
Justin Yun6977e8a2020-10-29 18:24:11 +0900952 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
953 cc_library {
954 name: "libvndkprop",
955 vendor_available: true,
956 product_available: true,
957 vndk: {
958 enabled: true,
959 },
960 nocrt: true,
961 target: {
962 vendor: {
963 cflags: ["-DTEST",],
964 },
965 },
966 }
967 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900968}
969
Logan Chiend3c59a22018-03-29 14:08:15 +0800970func TestVndkDepError(t *testing.T) {
971 // Check whether an error is emitted when a VNDK lib depends on a system lib.
972 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
973 cc_library {
974 name: "libvndk",
975 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900976 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800977 vndk: {
978 enabled: true,
979 },
980 shared_libs: ["libfwk"], // Cause error
981 nocrt: true,
982 }
983
984 cc_library {
985 name: "libfwk",
986 nocrt: true,
987 }
988 `)
989
990 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
991 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
992 cc_library {
993 name: "libvndk",
994 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900995 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800996 vndk: {
997 enabled: true,
998 },
999 shared_libs: ["libvendor"], // Cause error
1000 nocrt: true,
1001 }
1002
1003 cc_library {
1004 name: "libvendor",
1005 vendor: true,
1006 nocrt: true,
1007 }
1008 `)
1009
1010 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
1011 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1012 cc_library {
1013 name: "libvndk_sp",
1014 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001015 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001016 vndk: {
1017 enabled: true,
1018 support_system_process: true,
1019 },
1020 shared_libs: ["libfwk"], // Cause error
1021 nocrt: true,
1022 }
1023
1024 cc_library {
1025 name: "libfwk",
1026 nocrt: true,
1027 }
1028 `)
1029
1030 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
1031 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1032 cc_library {
1033 name: "libvndk_sp",
1034 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001035 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001036 vndk: {
1037 enabled: true,
1038 support_system_process: true,
1039 },
1040 shared_libs: ["libvendor"], // Cause error
1041 nocrt: true,
1042 }
1043
1044 cc_library {
1045 name: "libvendor",
1046 vendor: true,
1047 nocrt: true,
1048 }
1049 `)
1050
1051 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
1052 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1053 cc_library {
1054 name: "libvndk_sp",
1055 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001056 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001057 vndk: {
1058 enabled: true,
1059 support_system_process: true,
1060 },
1061 shared_libs: ["libvndk"], // Cause error
1062 nocrt: true,
1063 }
1064
1065 cc_library {
1066 name: "libvndk",
1067 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001068 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001069 vndk: {
1070 enabled: true,
1071 },
1072 nocrt: true,
1073 }
1074 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001075
1076 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1077 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1078 cc_library {
1079 name: "libvndk",
1080 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001081 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001082 vndk: {
1083 enabled: true,
1084 },
1085 shared_libs: ["libnonvndk"],
1086 nocrt: true,
1087 }
1088
1089 cc_library {
1090 name: "libnonvndk",
1091 vendor_available: true,
1092 nocrt: true,
1093 }
1094 `)
1095
1096 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1097 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1098 cc_library {
1099 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001100 vendor_available: true,
1101 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001102 vndk: {
1103 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001104 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001105 },
1106 shared_libs: ["libnonvndk"],
1107 nocrt: true,
1108 }
1109
1110 cc_library {
1111 name: "libnonvndk",
1112 vendor_available: true,
1113 nocrt: true,
1114 }
1115 `)
1116
1117 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1118 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1119 cc_library {
1120 name: "libvndksp",
1121 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001122 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001123 vndk: {
1124 enabled: true,
1125 support_system_process: true,
1126 },
1127 shared_libs: ["libnonvndk"],
1128 nocrt: true,
1129 }
1130
1131 cc_library {
1132 name: "libnonvndk",
1133 vendor_available: true,
1134 nocrt: true,
1135 }
1136 `)
1137
1138 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1139 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1140 cc_library {
1141 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001142 vendor_available: true,
1143 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001144 vndk: {
1145 enabled: true,
1146 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001147 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001148 },
1149 shared_libs: ["libnonvndk"],
1150 nocrt: true,
1151 }
1152
1153 cc_library {
1154 name: "libnonvndk",
1155 vendor_available: true,
1156 nocrt: true,
1157 }
1158 `)
1159}
1160
1161func TestDoubleLoadbleDep(t *testing.T) {
1162 // okay to link : LLNDK -> double_loadable VNDK
1163 testCc(t, `
1164 cc_library {
1165 name: "libllndk",
1166 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001167 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001168 }
1169
1170 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001171 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001172 symbol_file: "",
1173 }
1174
1175 cc_library {
1176 name: "libdoubleloadable",
1177 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001178 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001179 vndk: {
1180 enabled: true,
1181 },
1182 double_loadable: true,
1183 }
1184 `)
1185 // okay to link : LLNDK -> VNDK-SP
1186 testCc(t, `
1187 cc_library {
1188 name: "libllndk",
1189 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -07001190 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001191 }
1192
1193 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001194 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001195 symbol_file: "",
1196 }
1197
1198 cc_library {
1199 name: "libvndksp",
1200 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001201 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001202 vndk: {
1203 enabled: true,
1204 support_system_process: true,
1205 },
1206 }
1207 `)
1208 // okay to link : double_loadable -> double_loadable
1209 testCc(t, `
1210 cc_library {
1211 name: "libdoubleloadable1",
1212 shared_libs: ["libdoubleloadable2"],
1213 vendor_available: true,
1214 double_loadable: true,
1215 }
1216
1217 cc_library {
1218 name: "libdoubleloadable2",
1219 vendor_available: true,
1220 double_loadable: true,
1221 }
1222 `)
1223 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1224 testCc(t, `
1225 cc_library {
1226 name: "libdoubleloadable",
1227 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001228 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001229 vndk: {
1230 enabled: true,
1231 },
1232 double_loadable: true,
1233 shared_libs: ["libnondoubleloadable"],
1234 }
1235
1236 cc_library {
1237 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001238 vendor_available: true,
1239 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001240 vndk: {
1241 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001242 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001243 },
1244 double_loadable: true,
1245 }
1246 `)
1247 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1248 testCc(t, `
1249 cc_library {
1250 name: "libllndk",
1251 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001252 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001253 }
1254
1255 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001256 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001257 symbol_file: "",
1258 }
1259
1260 cc_library {
1261 name: "libcoreonly",
1262 shared_libs: ["libvendoravailable"],
1263 }
1264
1265 // indirect dependency of LLNDK
1266 cc_library {
1267 name: "libvendoravailable",
1268 vendor_available: true,
1269 double_loadable: true,
1270 }
1271 `)
1272}
1273
1274func TestDoubleLoadableDepError(t *testing.T) {
1275 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1276 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1277 cc_library {
1278 name: "libllndk",
1279 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001280 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001281 }
1282
1283 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001284 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001285 symbol_file: "",
1286 }
1287
1288 cc_library {
1289 name: "libnondoubleloadable",
1290 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001291 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001292 vndk: {
1293 enabled: true,
1294 },
1295 }
1296 `)
1297
1298 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1299 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1300 cc_library {
1301 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001302 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001303 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001304 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001305 }
1306
1307 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001308 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001309 symbol_file: "",
1310 }
1311
1312 cc_library {
1313 name: "libnondoubleloadable",
1314 vendor_available: true,
1315 }
1316 `)
1317
Jooyung Hana70f0672019-01-18 15:20:43 +09001318 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1319 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1320 cc_library {
1321 name: "libllndk",
1322 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001323 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001324 }
1325
1326 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001327 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001328 symbol_file: "",
1329 }
1330
1331 cc_library {
1332 name: "libcoreonly",
1333 shared_libs: ["libvendoravailable"],
1334 }
1335
1336 // indirect dependency of LLNDK
1337 cc_library {
1338 name: "libvendoravailable",
1339 vendor_available: true,
1340 }
1341 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001342
1343 // The error is not from 'client' but from 'libllndk'
1344 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1345 cc_library {
1346 name: "client",
1347 vendor_available: true,
1348 double_loadable: true,
1349 shared_libs: ["libllndk"],
1350 }
1351 cc_library {
1352 name: "libllndk",
1353 shared_libs: ["libnondoubleloadable"],
1354 llndk_stubs: "libllndk.llndk",
1355 }
1356 llndk_library {
1357 name: "libllndk.llndk",
1358 symbol_file: "",
1359 }
1360 cc_library {
1361 name: "libnondoubleloadable",
1362 vendor_available: true,
1363 }
1364 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001365}
1366
Jooyung Han479ca172020-10-19 18:51:07 +09001367func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1368 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1369 cc_library {
1370 name: "libvndksp",
1371 shared_libs: ["libanothervndksp"],
1372 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001373 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001374 vndk: {
1375 enabled: true,
1376 support_system_process: true,
1377 }
1378 }
1379
1380 cc_library {
1381 name: "libllndk",
1382 shared_libs: ["libanothervndksp"],
1383 }
1384
1385 llndk_library {
1386 name: "libllndk",
1387 symbol_file: "",
1388 }
1389
1390 cc_library {
1391 name: "libanothervndksp",
1392 vendor_available: true,
1393 }
1394 `)
1395}
1396
Logan Chienf3511742017-10-31 18:04:35 +08001397func TestVndkExt(t *testing.T) {
1398 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001399 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001400 cc_library {
1401 name: "libvndk",
1402 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001403 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001404 vndk: {
1405 enabled: true,
1406 },
1407 nocrt: true,
1408 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001409 cc_library {
1410 name: "libvndk2",
1411 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001412 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001413 vndk: {
1414 enabled: true,
1415 },
1416 target: {
1417 vendor: {
1418 suffix: "-suffix",
1419 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001420 product: {
1421 suffix: "-suffix",
1422 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001423 },
1424 nocrt: true,
1425 }
Logan Chienf3511742017-10-31 18:04:35 +08001426
1427 cc_library {
1428 name: "libvndk_ext",
1429 vendor: true,
1430 vndk: {
1431 enabled: true,
1432 extends: "libvndk",
1433 },
1434 nocrt: true,
1435 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001436
1437 cc_library {
1438 name: "libvndk2_ext",
1439 vendor: true,
1440 vndk: {
1441 enabled: true,
1442 extends: "libvndk2",
1443 },
1444 nocrt: true,
1445 }
Logan Chienf3511742017-10-31 18:04:35 +08001446
Justin Yun0ecf0b22020-02-28 15:07:59 +09001447 cc_library {
1448 name: "libvndk_ext_product",
1449 product_specific: true,
1450 vndk: {
1451 enabled: true,
1452 extends: "libvndk",
1453 },
1454 nocrt: true,
1455 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001456
Justin Yun0ecf0b22020-02-28 15:07:59 +09001457 cc_library {
1458 name: "libvndk2_ext_product",
1459 product_specific: true,
1460 vndk: {
1461 enabled: true,
1462 extends: "libvndk2",
1463 },
1464 nocrt: true,
1465 }
1466 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001467 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001468 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1469 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001470 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001471
1472 ctx := testCcWithConfig(t, config)
1473
1474 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1475 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1476
1477 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1478 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1479
1480 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1481 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001482}
1483
Logan Chiend3c59a22018-03-29 14:08:15 +08001484func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001485 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1486 ctx := testCcNoVndk(t, `
1487 cc_library {
1488 name: "libvndk",
1489 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001490 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001491 vndk: {
1492 enabled: true,
1493 },
1494 nocrt: true,
1495 }
1496
1497 cc_library {
1498 name: "libvndk_ext",
1499 vendor: true,
1500 vndk: {
1501 enabled: true,
1502 extends: "libvndk",
1503 },
1504 nocrt: true,
1505 }
1506 `)
1507
1508 // Ensures that the core variant of "libvndk_ext" can be found.
1509 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1510 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1511 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1512 }
1513}
1514
Justin Yun0ecf0b22020-02-28 15:07:59 +09001515func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1516 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001517 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001518 cc_library {
1519 name: "libvndk",
1520 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001521 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001522 vndk: {
1523 enabled: true,
1524 },
1525 nocrt: true,
1526 }
1527
1528 cc_library {
1529 name: "libvndk_ext_product",
1530 product_specific: true,
1531 vndk: {
1532 enabled: true,
1533 extends: "libvndk",
1534 },
1535 nocrt: true,
1536 }
1537 `)
1538
1539 // Ensures that the core variant of "libvndk_ext_product" can be found.
1540 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1541 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1542 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1543 }
1544}
1545
Logan Chienf3511742017-10-31 18:04:35 +08001546func TestVndkExtError(t *testing.T) {
1547 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001548 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001549 cc_library {
1550 name: "libvndk",
1551 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001552 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001553 vndk: {
1554 enabled: true,
1555 },
1556 nocrt: true,
1557 }
1558
1559 cc_library {
1560 name: "libvndk_ext",
1561 vndk: {
1562 enabled: true,
1563 extends: "libvndk",
1564 },
1565 nocrt: true,
1566 }
1567 `)
1568
1569 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1570 cc_library {
1571 name: "libvndk",
1572 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001573 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001574 vndk: {
1575 enabled: true,
1576 },
1577 nocrt: true,
1578 }
1579
1580 cc_library {
1581 name: "libvndk_ext",
1582 vendor: true,
1583 vndk: {
1584 enabled: true,
1585 },
1586 nocrt: true,
1587 }
1588 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001589
1590 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1591 cc_library {
1592 name: "libvndk",
1593 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001594 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001595 vndk: {
1596 enabled: true,
1597 },
1598 nocrt: true,
1599 }
1600
1601 cc_library {
1602 name: "libvndk_ext_product",
1603 product_specific: true,
1604 vndk: {
1605 enabled: true,
1606 },
1607 nocrt: true,
1608 }
1609 `)
1610
1611 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1612 cc_library {
1613 name: "libvndk",
1614 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001615 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001616 vndk: {
1617 enabled: true,
1618 },
1619 nocrt: true,
1620 }
1621
1622 cc_library {
1623 name: "libvndk_ext_product",
1624 product_specific: true,
1625 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001626 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001627 vndk: {
1628 enabled: true,
1629 extends: "libvndk",
1630 },
1631 nocrt: true,
1632 }
1633 `)
Logan Chienf3511742017-10-31 18:04:35 +08001634}
1635
1636func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1637 // This test ensures an error is emitted for inconsistent support_system_process.
1638 testCcError(t, "module \".*\" with mismatched support_system_process", `
1639 cc_library {
1640 name: "libvndk",
1641 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001642 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001643 vndk: {
1644 enabled: true,
1645 },
1646 nocrt: true,
1647 }
1648
1649 cc_library {
1650 name: "libvndk_sp_ext",
1651 vendor: true,
1652 vndk: {
1653 enabled: true,
1654 extends: "libvndk",
1655 support_system_process: true,
1656 },
1657 nocrt: true,
1658 }
1659 `)
1660
1661 testCcError(t, "module \".*\" with mismatched support_system_process", `
1662 cc_library {
1663 name: "libvndk_sp",
1664 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001665 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001666 vndk: {
1667 enabled: true,
1668 support_system_process: true,
1669 },
1670 nocrt: true,
1671 }
1672
1673 cc_library {
1674 name: "libvndk_ext",
1675 vendor: true,
1676 vndk: {
1677 enabled: true,
1678 extends: "libvndk_sp",
1679 },
1680 nocrt: true,
1681 }
1682 `)
1683}
1684
1685func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001686 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001687 // with `private: true`.
1688 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001689 cc_library {
1690 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001691 vendor_available: true,
1692 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001693 vndk: {
1694 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001695 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001696 },
1697 nocrt: true,
1698 }
1699
1700 cc_library {
1701 name: "libvndk_ext",
1702 vendor: true,
1703 vndk: {
1704 enabled: true,
1705 extends: "libvndk",
1706 },
1707 nocrt: true,
1708 }
1709 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001710
Justin Yunfd9e8042020-12-23 18:23:14 +09001711 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001712 cc_library {
1713 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001714 vendor_available: true,
1715 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001716 vndk: {
1717 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001718 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001719 },
1720 nocrt: true,
1721 }
1722
1723 cc_library {
1724 name: "libvndk_ext_product",
1725 product_specific: true,
1726 vndk: {
1727 enabled: true,
1728 extends: "libvndk",
1729 },
1730 nocrt: true,
1731 }
1732 `)
Logan Chienf3511742017-10-31 18:04:35 +08001733}
1734
Logan Chiend3c59a22018-03-29 14:08:15 +08001735func TestVendorModuleUseVndkExt(t *testing.T) {
1736 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001737 testCc(t, `
1738 cc_library {
1739 name: "libvndk",
1740 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001741 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001742 vndk: {
1743 enabled: true,
1744 },
1745 nocrt: true,
1746 }
1747
1748 cc_library {
1749 name: "libvndk_ext",
1750 vendor: true,
1751 vndk: {
1752 enabled: true,
1753 extends: "libvndk",
1754 },
1755 nocrt: true,
1756 }
1757
1758 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001759 name: "libvndk_sp",
1760 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001761 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001762 vndk: {
1763 enabled: true,
1764 support_system_process: true,
1765 },
1766 nocrt: true,
1767 }
1768
1769 cc_library {
1770 name: "libvndk_sp_ext",
1771 vendor: true,
1772 vndk: {
1773 enabled: true,
1774 extends: "libvndk_sp",
1775 support_system_process: true,
1776 },
1777 nocrt: true,
1778 }
1779
1780 cc_library {
1781 name: "libvendor",
1782 vendor: true,
1783 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1784 nocrt: true,
1785 }
1786 `)
1787}
1788
Logan Chiend3c59a22018-03-29 14:08:15 +08001789func TestVndkExtUseVendorLib(t *testing.T) {
1790 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001791 testCc(t, `
1792 cc_library {
1793 name: "libvndk",
1794 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001795 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001796 vndk: {
1797 enabled: true,
1798 },
1799 nocrt: true,
1800 }
1801
1802 cc_library {
1803 name: "libvndk_ext",
1804 vendor: true,
1805 vndk: {
1806 enabled: true,
1807 extends: "libvndk",
1808 },
1809 shared_libs: ["libvendor"],
1810 nocrt: true,
1811 }
1812
1813 cc_library {
1814 name: "libvendor",
1815 vendor: true,
1816 nocrt: true,
1817 }
1818 `)
Logan Chienf3511742017-10-31 18:04:35 +08001819
Logan Chiend3c59a22018-03-29 14:08:15 +08001820 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1821 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001822 cc_library {
1823 name: "libvndk_sp",
1824 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001825 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001826 vndk: {
1827 enabled: true,
1828 support_system_process: true,
1829 },
1830 nocrt: true,
1831 }
1832
1833 cc_library {
1834 name: "libvndk_sp_ext",
1835 vendor: true,
1836 vndk: {
1837 enabled: true,
1838 extends: "libvndk_sp",
1839 support_system_process: true,
1840 },
1841 shared_libs: ["libvendor"], // Cause an error
1842 nocrt: true,
1843 }
1844
1845 cc_library {
1846 name: "libvendor",
1847 vendor: true,
1848 nocrt: true,
1849 }
1850 `)
1851}
1852
Justin Yun0ecf0b22020-02-28 15:07:59 +09001853func TestProductVndkExtDependency(t *testing.T) {
1854 bp := `
1855 cc_library {
1856 name: "libvndk",
1857 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001858 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001859 vndk: {
1860 enabled: true,
1861 },
1862 nocrt: true,
1863 }
1864
1865 cc_library {
1866 name: "libvndk_ext_product",
1867 product_specific: true,
1868 vndk: {
1869 enabled: true,
1870 extends: "libvndk",
1871 },
1872 shared_libs: ["libproduct_for_vndklibs"],
1873 nocrt: true,
1874 }
1875
1876 cc_library {
1877 name: "libvndk_sp",
1878 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001879 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001880 vndk: {
1881 enabled: true,
1882 support_system_process: true,
1883 },
1884 nocrt: true,
1885 }
1886
1887 cc_library {
1888 name: "libvndk_sp_ext_product",
1889 product_specific: true,
1890 vndk: {
1891 enabled: true,
1892 extends: "libvndk_sp",
1893 support_system_process: true,
1894 },
1895 shared_libs: ["libproduct_for_vndklibs"],
1896 nocrt: true,
1897 }
1898
1899 cc_library {
1900 name: "libproduct",
1901 product_specific: true,
1902 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1903 nocrt: true,
1904 }
1905
1906 cc_library {
1907 name: "libproduct_for_vndklibs",
1908 product_specific: true,
1909 nocrt: true,
1910 }
1911 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001912 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001913 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1914 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001915 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001916
1917 testCcWithConfig(t, config)
1918}
1919
Logan Chiend3c59a22018-03-29 14:08:15 +08001920func TestVndkSpExtUseVndkError(t *testing.T) {
1921 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1922 // library.
1923 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1924 cc_library {
1925 name: "libvndk",
1926 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001927 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001928 vndk: {
1929 enabled: true,
1930 },
1931 nocrt: true,
1932 }
1933
1934 cc_library {
1935 name: "libvndk_sp",
1936 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001937 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001938 vndk: {
1939 enabled: true,
1940 support_system_process: true,
1941 },
1942 nocrt: true,
1943 }
1944
1945 cc_library {
1946 name: "libvndk_sp_ext",
1947 vendor: true,
1948 vndk: {
1949 enabled: true,
1950 extends: "libvndk_sp",
1951 support_system_process: true,
1952 },
1953 shared_libs: ["libvndk"], // Cause an error
1954 nocrt: true,
1955 }
1956 `)
1957
1958 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1959 // library.
1960 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1961 cc_library {
1962 name: "libvndk",
1963 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001964 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001965 vndk: {
1966 enabled: true,
1967 },
1968 nocrt: true,
1969 }
1970
1971 cc_library {
1972 name: "libvndk_ext",
1973 vendor: true,
1974 vndk: {
1975 enabled: true,
1976 extends: "libvndk",
1977 },
1978 nocrt: true,
1979 }
1980
1981 cc_library {
1982 name: "libvndk_sp",
1983 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001984 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001985 vndk: {
1986 enabled: true,
1987 support_system_process: true,
1988 },
1989 nocrt: true,
1990 }
1991
1992 cc_library {
1993 name: "libvndk_sp_ext",
1994 vendor: true,
1995 vndk: {
1996 enabled: true,
1997 extends: "libvndk_sp",
1998 support_system_process: true,
1999 },
2000 shared_libs: ["libvndk_ext"], // Cause an error
2001 nocrt: true,
2002 }
2003 `)
2004}
2005
2006func TestVndkUseVndkExtError(t *testing.T) {
2007 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2008 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002009 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2010 cc_library {
2011 name: "libvndk",
2012 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002013 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002014 vndk: {
2015 enabled: true,
2016 },
2017 nocrt: true,
2018 }
2019
2020 cc_library {
2021 name: "libvndk_ext",
2022 vendor: true,
2023 vndk: {
2024 enabled: true,
2025 extends: "libvndk",
2026 },
2027 nocrt: true,
2028 }
2029
2030 cc_library {
2031 name: "libvndk2",
2032 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002033 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002034 vndk: {
2035 enabled: true,
2036 },
2037 shared_libs: ["libvndk_ext"],
2038 nocrt: true,
2039 }
2040 `)
2041
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002042 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002043 cc_library {
2044 name: "libvndk",
2045 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002046 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002047 vndk: {
2048 enabled: true,
2049 },
2050 nocrt: true,
2051 }
2052
2053 cc_library {
2054 name: "libvndk_ext",
2055 vendor: true,
2056 vndk: {
2057 enabled: true,
2058 extends: "libvndk",
2059 },
2060 nocrt: true,
2061 }
2062
2063 cc_library {
2064 name: "libvndk2",
2065 vendor_available: true,
2066 vndk: {
2067 enabled: true,
2068 },
2069 target: {
2070 vendor: {
2071 shared_libs: ["libvndk_ext"],
2072 },
2073 },
2074 nocrt: true,
2075 }
2076 `)
2077
2078 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2079 cc_library {
2080 name: "libvndk_sp",
2081 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002082 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002083 vndk: {
2084 enabled: true,
2085 support_system_process: true,
2086 },
2087 nocrt: true,
2088 }
2089
2090 cc_library {
2091 name: "libvndk_sp_ext",
2092 vendor: true,
2093 vndk: {
2094 enabled: true,
2095 extends: "libvndk_sp",
2096 support_system_process: true,
2097 },
2098 nocrt: true,
2099 }
2100
2101 cc_library {
2102 name: "libvndk_sp_2",
2103 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002104 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002105 vndk: {
2106 enabled: true,
2107 support_system_process: true,
2108 },
2109 shared_libs: ["libvndk_sp_ext"],
2110 nocrt: true,
2111 }
2112 `)
2113
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002114 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002115 cc_library {
2116 name: "libvndk_sp",
2117 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002118 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002119 vndk: {
2120 enabled: true,
2121 },
2122 nocrt: true,
2123 }
2124
2125 cc_library {
2126 name: "libvndk_sp_ext",
2127 vendor: true,
2128 vndk: {
2129 enabled: true,
2130 extends: "libvndk_sp",
2131 },
2132 nocrt: true,
2133 }
2134
2135 cc_library {
2136 name: "libvndk_sp2",
2137 vendor_available: true,
2138 vndk: {
2139 enabled: true,
2140 },
2141 target: {
2142 vendor: {
2143 shared_libs: ["libvndk_sp_ext"],
2144 },
2145 },
2146 nocrt: true,
2147 }
2148 `)
2149}
2150
Justin Yun5f7f7e82019-11-18 19:52:14 +09002151func TestEnforceProductVndkVersion(t *testing.T) {
2152 bp := `
2153 cc_library {
2154 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002155 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002156 }
2157 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002158 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002159 symbol_file: "",
2160 }
2161 cc_library {
2162 name: "libvndk",
2163 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002164 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002165 vndk: {
2166 enabled: true,
2167 },
2168 nocrt: true,
2169 }
2170 cc_library {
2171 name: "libvndk_sp",
2172 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002173 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002174 vndk: {
2175 enabled: true,
2176 support_system_process: true,
2177 },
2178 nocrt: true,
2179 }
2180 cc_library {
2181 name: "libva",
2182 vendor_available: true,
2183 nocrt: true,
2184 }
2185 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002186 name: "libpa",
2187 product_available: true,
2188 nocrt: true,
2189 }
2190 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002191 name: "libboth_available",
2192 vendor_available: true,
2193 product_available: true,
2194 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002195 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002196 target: {
2197 vendor: {
2198 suffix: "-vendor",
2199 },
2200 product: {
2201 suffix: "-product",
2202 },
2203 }
2204 }
2205 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002206 name: "libproduct_va",
2207 product_specific: true,
2208 vendor_available: true,
2209 nocrt: true,
2210 }
2211 cc_library {
2212 name: "libprod",
2213 product_specific: true,
2214 shared_libs: [
2215 "libllndk",
2216 "libvndk",
2217 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002218 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002219 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002220 "libproduct_va",
2221 ],
2222 nocrt: true,
2223 }
2224 cc_library {
2225 name: "libvendor",
2226 vendor: true,
2227 shared_libs: [
2228 "libllndk",
2229 "libvndk",
2230 "libvndk_sp",
2231 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002232 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002233 "libproduct_va",
2234 ],
2235 nocrt: true,
2236 }
2237 `
2238
Paul Duffin8567f222021-03-23 00:02:06 +00002239 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002240
Jooyung Han261e1582020-10-20 18:54:21 +09002241 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2242 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002243
2244 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2245 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2246
2247 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2248 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002249
2250 ensureStringContains := func(t *testing.T, str string, substr string) {
2251 t.Helper()
2252 if !strings.Contains(str, substr) {
2253 t.Errorf("%q is not found in %v", substr, str)
2254 }
2255 }
2256 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2257 t.Helper()
2258 if strings.Contains(str, substr) {
2259 t.Errorf("%q is found in %v", substr, str)
2260 }
2261 }
2262
2263 // _static variant is used since _shared reuses *.o from the static variant
2264 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2265 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2266
2267 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2268 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2269 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2270 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2271
2272 product_cflags := product_static.Rule("cc").Args["cFlags"]
2273 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2274 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2275 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002276}
2277
2278func TestEnforceProductVndkVersionErrors(t *testing.T) {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002279 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002280 cc_library {
2281 name: "libprod",
2282 product_specific: true,
2283 shared_libs: [
2284 "libvendor",
2285 ],
2286 nocrt: true,
2287 }
2288 cc_library {
2289 name: "libvendor",
2290 vendor: true,
2291 nocrt: true,
2292 }
2293 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002294 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002295 cc_library {
2296 name: "libprod",
2297 product_specific: true,
2298 shared_libs: [
2299 "libsystem",
2300 ],
2301 nocrt: true,
2302 }
2303 cc_library {
2304 name: "libsystem",
2305 nocrt: true,
2306 }
2307 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002308 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002309 cc_library {
2310 name: "libprod",
2311 product_specific: true,
2312 shared_libs: [
2313 "libva",
2314 ],
2315 nocrt: true,
2316 }
2317 cc_library {
2318 name: "libva",
2319 vendor_available: true,
2320 nocrt: true,
2321 }
2322 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002323 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002324 cc_library {
2325 name: "libprod",
2326 product_specific: true,
2327 shared_libs: [
2328 "libvndk_private",
2329 ],
2330 nocrt: true,
2331 }
2332 cc_library {
2333 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002334 vendor_available: true,
2335 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002336 vndk: {
2337 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002338 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002339 },
2340 nocrt: true,
2341 }
2342 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002343 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002344 cc_library {
2345 name: "libprod",
2346 product_specific: true,
2347 shared_libs: [
2348 "libsystem_ext",
2349 ],
2350 nocrt: true,
2351 }
2352 cc_library {
2353 name: "libsystem_ext",
2354 system_ext_specific: true,
2355 nocrt: true,
2356 }
2357 `)
2358 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2359 cc_library {
2360 name: "libsystem",
2361 shared_libs: [
2362 "libproduct_va",
2363 ],
2364 nocrt: true,
2365 }
2366 cc_library {
2367 name: "libproduct_va",
2368 product_specific: true,
2369 vendor_available: true,
2370 nocrt: true,
2371 }
2372 `)
2373}
2374
Jooyung Han38002912019-05-16 04:01:54 +09002375func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002376 bp := `
2377 cc_library {
2378 name: "libvndk",
2379 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002380 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002381 vndk: {
2382 enabled: true,
2383 },
2384 }
2385 cc_library {
2386 name: "libvndksp",
2387 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002388 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002389 vndk: {
2390 enabled: true,
2391 support_system_process: true,
2392 },
2393 }
2394 cc_library {
2395 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002396 vendor_available: true,
2397 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002398 vndk: {
2399 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002400 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002401 },
2402 }
2403 cc_library {
2404 name: "libvendor",
2405 vendor: true,
2406 }
2407 cc_library {
2408 name: "libvndkext",
2409 vendor: true,
2410 vndk: {
2411 enabled: true,
2412 extends: "libvndk",
2413 },
2414 }
2415 vndk_prebuilt_shared {
2416 name: "prevndk",
2417 version: "27",
2418 target_arch: "arm",
2419 binder32bit: true,
2420 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002421 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002422 vndk: {
2423 enabled: true,
2424 },
2425 arch: {
2426 arm: {
2427 srcs: ["liba.so"],
2428 },
2429 },
2430 }
2431 cc_library {
2432 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002433 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002434 }
2435 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002436 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002437 symbol_file: "",
2438 }
2439 cc_library {
2440 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07002441 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002442 }
2443 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002444 name: "libllndkprivate.llndk",
Justin Yunc0d8c492021-01-07 17:45:31 +09002445 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002446 symbol_file: "",
Colin Cross78212242021-01-06 14:51:30 -08002447 }
2448
2449 llndk_libraries_txt {
2450 name: "llndk.libraries.txt",
2451 }
2452 vndkcore_libraries_txt {
2453 name: "vndkcore.libraries.txt",
2454 }
2455 vndksp_libraries_txt {
2456 name: "vndksp.libraries.txt",
2457 }
2458 vndkprivate_libraries_txt {
2459 name: "vndkprivate.libraries.txt",
2460 }
2461 vndkcorevariant_libraries_txt {
2462 name: "vndkcorevariant.libraries.txt",
2463 insert_vndk_version: false,
2464 }
2465 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002466
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002467 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002468 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002469 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002470 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002471 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002472
Colin Cross78212242021-01-06 14:51:30 -08002473 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2474 []string{"libvndk.so", "libvndkprivate.so"})
2475 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2476 []string{"libc++.so", "libvndksp.so"})
2477 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2478 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2479 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2480 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002481
Colin Crossfb0c16e2019-11-20 17:12:35 -08002482 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002483
Jooyung Han38002912019-05-16 04:01:54 +09002484 tests := []struct {
2485 variant string
2486 name string
2487 expected string
2488 }{
2489 {vendorVariant, "libvndk", "native:vndk"},
2490 {vendorVariant, "libvndksp", "native:vndk"},
2491 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2492 {vendorVariant, "libvendor", "native:vendor"},
2493 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002494 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002495 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002496 {coreVariant, "libvndk", "native:platform"},
2497 {coreVariant, "libvndkprivate", "native:platform"},
2498 {coreVariant, "libllndk", "native:platform"},
2499 }
2500 for _, test := range tests {
2501 t.Run(test.name, func(t *testing.T) {
2502 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2503 assertString(t, module.makeLinkType, test.expected)
2504 })
2505 }
2506}
2507
Jeff Gaston294356f2017-09-27 17:05:30 -07002508var staticLinkDepOrderTestCases = []struct {
2509 // This is a string representation of a map[moduleName][]moduleDependency .
2510 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002511 inStatic string
2512
2513 // This is a string representation of a map[moduleName][]moduleDependency .
2514 // It models the dependencies declared in an Android.bp file.
2515 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002516
2517 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2518 // The keys of allOrdered specify which modules we would like to check.
2519 // The values of allOrdered specify the expected result (of the transitive closure of all
2520 // dependencies) for each module to test
2521 allOrdered string
2522
2523 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2524 // The keys of outOrdered specify which modules we would like to check.
2525 // The values of outOrdered specify the expected result (of the ordered linker command line)
2526 // for each module to test.
2527 outOrdered string
2528}{
2529 // Simple tests
2530 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002531 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002532 outOrdered: "",
2533 },
2534 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002535 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002536 outOrdered: "a:",
2537 },
2538 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002539 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002540 outOrdered: "a:b; b:",
2541 },
2542 // Tests of reordering
2543 {
2544 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002545 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002546 outOrdered: "a:b,c,d; b:d; c:d; d:",
2547 },
2548 {
2549 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002550 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002551 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2552 },
2553 {
2554 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002555 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002556 outOrdered: "a:d,b,e,c; d:b; e:c",
2557 },
2558 {
2559 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002560 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002561 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2562 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2563 },
2564 {
2565 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002566 inStatic: "a:b,c,d,e,f,g,h; f:b,c,d; b:c,d; c:d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002567 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2568 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2569 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002570 // shared dependencies
2571 {
2572 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2573 // So, we don't actually have to check that a shared dependency of c will change the order
2574 // of a library that depends statically on b and on c. We only need to check that if c has
2575 // a shared dependency on b, that that shows up in allOrdered.
2576 inShared: "c:b",
2577 allOrdered: "c:b",
2578 outOrdered: "c:",
2579 },
2580 {
2581 // This test doesn't actually include any shared dependencies but it's a reminder of what
2582 // the second phase of the above test would look like
2583 inStatic: "a:b,c; c:b",
2584 allOrdered: "a:c,b; c:b",
2585 outOrdered: "a:c,b; c:b",
2586 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002587 // tiebreakers for when two modules specifying different orderings and there is no dependency
2588 // to dictate an order
2589 {
2590 // if the tie is between two modules at the end of a's deps, then a's order wins
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002591 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002592 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2593 },
2594 {
2595 // if the tie is between two modules at the start of a's deps, then c's order is used
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002596 inStatic: "a1:d,e,b1,c1; b1:d,e; c1:e,d; a2:d,e,b2,c2; b2:d,e; c2:d,e",
Jeff Gaston294356f2017-09-27 17:05:30 -07002597 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2598 },
2599 // Tests involving duplicate dependencies
2600 {
2601 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002602 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002603 outOrdered: "a:c,b",
2604 },
2605 {
2606 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002607 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002608 outOrdered: "a:d,c,b",
2609 },
2610 // Tests to confirm the nonexistence of infinite loops.
2611 // These cases should never happen, so as long as the test terminates and the
2612 // result is deterministic then that should be fine.
2613 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002614 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002615 outOrdered: "a:a",
2616 },
2617 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002618 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002619 allOrdered: "a:b,c; b:c,a; c:a,b",
2620 outOrdered: "a:b; b:c; c:a",
2621 },
2622 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002623 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002624 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2625 outOrdered: "a:c,b; b:a,c; c:b,a",
2626 },
2627}
2628
2629// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2630func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2631 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2632 strippedText := strings.Replace(text, " ", "", -1)
2633 if len(strippedText) < 1 {
2634 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2635 }
2636 allDeps = make(map[android.Path][]android.Path, 0)
2637
2638 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2639 moduleTexts := strings.Split(strippedText, ";")
2640
2641 outputForModuleName := func(moduleName string) android.Path {
2642 return android.PathForTesting(moduleName)
2643 }
2644
2645 for _, moduleText := range moduleTexts {
2646 // convert from "a:b,c" to ["a", "b,c"]
2647 components := strings.Split(moduleText, ":")
2648 if len(components) != 2 {
2649 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2650 }
2651 moduleName := components[0]
2652 moduleOutput := outputForModuleName(moduleName)
2653 modulesInOrder = append(modulesInOrder, moduleOutput)
2654
2655 depString := components[1]
2656 // convert from "b,c" to ["b", "c"]
2657 depNames := strings.Split(depString, ",")
2658 if len(depString) < 1 {
2659 depNames = []string{}
2660 }
2661 var deps []android.Path
2662 for _, depName := range depNames {
2663 deps = append(deps, outputForModuleName(depName))
2664 }
2665 allDeps[moduleOutput] = deps
2666 }
2667 return modulesInOrder, allDeps
2668}
2669
Jeff Gaston294356f2017-09-27 17:05:30 -07002670func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2671 for _, moduleName := range moduleNames {
2672 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002673 output := module.outputFile.Path().RelativeToTop()
Jeff Gaston294356f2017-09-27 17:05:30 -07002674 paths = append(paths, output)
2675 }
2676 return paths
2677}
2678
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002679func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002680 ctx := testCc(t, `
2681 cc_library {
2682 name: "a",
2683 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002684 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002685 }
2686 cc_library {
2687 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002688 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002689 }
2690 cc_library {
2691 name: "c",
2692 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002693 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002694 }
2695 cc_library {
2696 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002697 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002698 }
2699
2700 `)
2701
Colin Cross7113d202019-11-20 16:39:12 -08002702 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002703 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002704 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2705 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002706 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002707
2708 if !reflect.DeepEqual(actual, expected) {
2709 t.Errorf("staticDeps orderings were not propagated correctly"+
2710 "\nactual: %v"+
2711 "\nexpected: %v",
2712 actual,
2713 expected,
2714 )
2715 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002716}
Jeff Gaston294356f2017-09-27 17:05:30 -07002717
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002718func TestStaticLibDepReorderingWithShared(t *testing.T) {
2719 ctx := testCc(t, `
2720 cc_library {
2721 name: "a",
2722 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002723 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002724 }
2725 cc_library {
2726 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002727 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002728 }
2729 cc_library {
2730 name: "c",
2731 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002732 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002733 }
2734
2735 `)
2736
Colin Cross7113d202019-11-20 16:39:12 -08002737 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002738 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002739 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2740 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002741 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002742
2743 if !reflect.DeepEqual(actual, expected) {
2744 t.Errorf("staticDeps orderings did not account for shared libs"+
2745 "\nactual: %v"+
2746 "\nexpected: %v",
2747 actual,
2748 expected,
2749 )
2750 }
2751}
2752
Jooyung Hanb04a4992020-03-13 18:57:35 +09002753func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002754 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002755 if !reflect.DeepEqual(actual, expected) {
2756 t.Errorf(message+
2757 "\nactual: %v"+
2758 "\nexpected: %v",
2759 actual,
2760 expected,
2761 )
2762 }
2763}
2764
Jooyung Han61b66e92020-03-21 14:21:46 +00002765func TestLlndkLibrary(t *testing.T) {
2766 ctx := testCc(t, `
2767 cc_library {
2768 name: "libllndk",
2769 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07002770 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002771 }
2772 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002773 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002774 }
Colin Cross127bb8b2020-12-16 16:46:01 -08002775
2776 cc_prebuilt_library_shared {
2777 name: "libllndkprebuilt",
2778 stubs: { versions: ["1", "2"] },
2779 llndk_stubs: "libllndkprebuilt.llndk",
2780 }
2781 llndk_library {
2782 name: "libllndkprebuilt.llndk",
2783 }
2784
2785 cc_library {
2786 name: "libllndk_with_external_headers",
2787 stubs: { versions: ["1", "2"] },
2788 llndk_stubs: "libllndk_with_external_headers.llndk",
2789 header_libs: ["libexternal_headers"],
2790 export_header_lib_headers: ["libexternal_headers"],
2791 }
2792 llndk_library {
2793 name: "libllndk_with_external_headers.llndk",
2794 }
2795 cc_library_headers {
2796 name: "libexternal_headers",
2797 export_include_dirs: ["include"],
2798 vendor_available: true,
2799 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002800 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08002801 actual := ctx.ModuleVariantsForTests("libllndk")
2802 for i := 0; i < len(actual); i++ {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002803 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
Colin Cross127bb8b2020-12-16 16:46:01 -08002804 actual = append(actual[:i], actual[i+1:]...)
2805 i--
2806 }
2807 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002808 expected := []string{
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002809 "android_vendor.29_arm64_armv8-a_shared_1",
2810 "android_vendor.29_arm64_armv8-a_shared_2",
2811 "android_vendor.29_arm64_armv8-a_shared_current",
2812 "android_vendor.29_arm64_armv8-a_shared",
2813 "android_vendor.29_arm_armv7-a-neon_shared_1",
2814 "android_vendor.29_arm_armv7-a-neon_shared_2",
2815 "android_vendor.29_arm_armv7-a-neon_shared_current",
2816 "android_vendor.29_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002817 }
2818 checkEquals(t, "variants for llndk stubs", expected, actual)
2819
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002820 params := ctx.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002821 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2822
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002823 params = ctx.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002824 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2825}
2826
Jiyong Parka46a4d52017-12-14 19:54:34 +09002827func TestLlndkHeaders(t *testing.T) {
2828 ctx := testCc(t, `
2829 llndk_headers {
2830 name: "libllndk_headers",
2831 export_include_dirs: ["my_include"],
2832 }
2833 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002834 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09002835 export_llndk_headers: ["libllndk_headers"],
2836 }
2837 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002838 name: "libllndk",
2839 llndk_stubs: "libllndk.llndk",
2840 }
2841
2842 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002843 name: "libvendor",
2844 shared_libs: ["libllndk"],
2845 vendor: true,
2846 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002847 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002848 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002849 }
2850 `)
2851
2852 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002853 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002854 cflags := cc.Args["cFlags"]
2855 if !strings.Contains(cflags, "-Imy_include") {
2856 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2857 }
2858}
2859
Logan Chien43d34c32017-12-20 01:17:32 +08002860func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2861 actual := module.Properties.AndroidMkRuntimeLibs
2862 if !reflect.DeepEqual(actual, expected) {
2863 t.Errorf("incorrect runtime_libs for shared libs"+
2864 "\nactual: %v"+
2865 "\nexpected: %v",
2866 actual,
2867 expected,
2868 )
2869 }
2870}
2871
2872const runtimeLibAndroidBp = `
2873 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002874 name: "liball_available",
2875 vendor_available: true,
2876 product_available: true,
2877 no_libcrt : true,
2878 nocrt : true,
2879 system_shared_libs : [],
2880 }
2881 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002882 name: "libvendor_available1",
2883 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002884 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002885 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002886 nocrt : true,
2887 system_shared_libs : [],
2888 }
2889 cc_library {
2890 name: "libvendor_available2",
2891 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002892 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002893 target: {
2894 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002895 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002896 }
2897 },
Yi Konge7fe9912019-06-02 00:53:50 -07002898 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002899 nocrt : true,
2900 system_shared_libs : [],
2901 }
2902 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002903 name: "libproduct_vendor",
2904 product_specific: true,
2905 vendor_available: true,
2906 no_libcrt : true,
2907 nocrt : true,
2908 system_shared_libs : [],
2909 }
2910 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002911 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002912 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002913 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002914 nocrt : true,
2915 system_shared_libs : [],
2916 }
2917 cc_library {
2918 name: "libvendor1",
2919 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002920 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002921 nocrt : true,
2922 system_shared_libs : [],
2923 }
2924 cc_library {
2925 name: "libvendor2",
2926 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002927 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002928 no_libcrt : true,
2929 nocrt : true,
2930 system_shared_libs : [],
2931 }
2932 cc_library {
2933 name: "libproduct_available1",
2934 product_available: true,
2935 runtime_libs: ["liball_available"],
2936 no_libcrt : true,
2937 nocrt : true,
2938 system_shared_libs : [],
2939 }
2940 cc_library {
2941 name: "libproduct1",
2942 product_specific: true,
2943 no_libcrt : true,
2944 nocrt : true,
2945 system_shared_libs : [],
2946 }
2947 cc_library {
2948 name: "libproduct2",
2949 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002950 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002951 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002952 nocrt : true,
2953 system_shared_libs : [],
2954 }
2955`
2956
2957func TestRuntimeLibs(t *testing.T) {
2958 ctx := testCc(t, runtimeLibAndroidBp)
2959
2960 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002961 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002962
Justin Yun8a2600c2020-12-07 12:44:03 +09002963 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2964 checkRuntimeLibs(t, []string{"liball_available"}, module)
2965
2966 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2967 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002968
2969 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002970 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002971
2972 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2973 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002974 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002975
Justin Yun8a2600c2020-12-07 12:44:03 +09002976 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2977 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002978
2979 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002980 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002981
2982 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2983 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002984 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002985
2986 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2987 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2988
2989 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002990 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002991}
2992
2993func TestExcludeRuntimeLibs(t *testing.T) {
2994 ctx := testCc(t, runtimeLibAndroidBp)
2995
Colin Cross7113d202019-11-20 16:39:12 -08002996 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002997 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2998 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002999
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003000 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003001 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08003002 checkRuntimeLibs(t, nil, module)
3003}
3004
3005func TestRuntimeLibsNoVndk(t *testing.T) {
3006 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3007
3008 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3009
Colin Cross7113d202019-11-20 16:39:12 -08003010 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003011
Justin Yun8a2600c2020-12-07 12:44:03 +09003012 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3013 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003014
3015 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003016 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003017
3018 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003019 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003020}
3021
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003022func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003023 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003024 actual := module.Properties.AndroidMkStaticLibs
3025 if !reflect.DeepEqual(actual, expected) {
3026 t.Errorf("incorrect static_libs"+
3027 "\nactual: %v"+
3028 "\nexpected: %v",
3029 actual,
3030 expected,
3031 )
3032 }
3033}
3034
3035const staticLibAndroidBp = `
3036 cc_library {
3037 name: "lib1",
3038 }
3039 cc_library {
3040 name: "lib2",
3041 static_libs: ["lib1"],
3042 }
3043`
3044
3045func TestStaticLibDepExport(t *testing.T) {
3046 ctx := testCc(t, staticLibAndroidBp)
3047
3048 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003049 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003050 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Ryan Prichardc2018e22021-04-02 20:23:22 -07003051 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003052
3053 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003054 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003055 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3056 // libc++_static is linked additionally.
Ryan Prichardc2018e22021-04-02 20:23:22 -07003057 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003058}
3059
Jiyong Parkd08b6972017-09-26 10:50:54 +09003060var compilerFlagsTestCases = []struct {
3061 in string
3062 out bool
3063}{
3064 {
3065 in: "a",
3066 out: false,
3067 },
3068 {
3069 in: "-a",
3070 out: true,
3071 },
3072 {
3073 in: "-Ipath/to/something",
3074 out: false,
3075 },
3076 {
3077 in: "-isystempath/to/something",
3078 out: false,
3079 },
3080 {
3081 in: "--coverage",
3082 out: false,
3083 },
3084 {
3085 in: "-include a/b",
3086 out: true,
3087 },
3088 {
3089 in: "-include a/b c/d",
3090 out: false,
3091 },
3092 {
3093 in: "-DMACRO",
3094 out: true,
3095 },
3096 {
3097 in: "-DMAC RO",
3098 out: false,
3099 },
3100 {
3101 in: "-a -b",
3102 out: false,
3103 },
3104 {
3105 in: "-DMACRO=definition",
3106 out: true,
3107 },
3108 {
3109 in: "-DMACRO=defi nition",
3110 out: true, // TODO(jiyong): this should be false
3111 },
3112 {
3113 in: "-DMACRO(x)=x + 1",
3114 out: true,
3115 },
3116 {
3117 in: "-DMACRO=\"defi nition\"",
3118 out: true,
3119 },
3120}
3121
3122type mockContext struct {
3123 BaseModuleContext
3124 result bool
3125}
3126
3127func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3128 // CheckBadCompilerFlags calls this function when the flag should be rejected
3129 ctx.result = false
3130}
3131
3132func TestCompilerFlags(t *testing.T) {
3133 for _, testCase := range compilerFlagsTestCases {
3134 ctx := &mockContext{result: true}
3135 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3136 if ctx.result != testCase.out {
3137 t.Errorf("incorrect output:")
3138 t.Errorf(" input: %#v", testCase.in)
3139 t.Errorf(" expected: %#v", testCase.out)
3140 t.Errorf(" got: %#v", ctx.result)
3141 }
3142 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003143}
Jiyong Park374510b2018-03-19 18:23:01 +09003144
3145func TestVendorPublicLibraries(t *testing.T) {
3146 ctx := testCc(t, `
3147 cc_library_headers {
3148 name: "libvendorpublic_headers",
3149 export_include_dirs: ["my_include"],
3150 }
3151 vendor_public_library {
3152 name: "libvendorpublic",
3153 symbol_file: "",
3154 export_public_headers: ["libvendorpublic_headers"],
3155 }
3156 cc_library {
3157 name: "libvendorpublic",
3158 srcs: ["foo.c"],
3159 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003160 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003161 nocrt: true,
3162 }
3163
3164 cc_library {
3165 name: "libsystem",
3166 shared_libs: ["libvendorpublic"],
3167 vendor: false,
3168 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003169 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003170 nocrt: true,
3171 }
3172 cc_library {
3173 name: "libvendor",
3174 shared_libs: ["libvendorpublic"],
3175 vendor: true,
3176 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003177 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003178 nocrt: true,
3179 }
3180 `)
3181
Colin Cross7113d202019-11-20 16:39:12 -08003182 coreVariant := "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003183 vendorVariant := "android_vendor.29_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003184
3185 // test if header search paths are correctly added
3186 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003187 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003188 cflags := cc.Args["cFlags"]
3189 if !strings.Contains(cflags, "-Imy_include") {
3190 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3191 }
3192
3193 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003194 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003195 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003196 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003197 if !strings.Contains(libflags, stubPaths[0].String()) {
3198 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3199 }
3200
3201 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003202 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003203 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003204 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003205 if !strings.Contains(libflags, stubPaths[0].String()) {
3206 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3207 }
3208
3209}
Jiyong Park37b25202018-07-11 10:49:27 +09003210
3211func TestRecovery(t *testing.T) {
3212 ctx := testCc(t, `
3213 cc_library_shared {
3214 name: "librecovery",
3215 recovery: true,
3216 }
3217 cc_library_shared {
3218 name: "librecovery32",
3219 recovery: true,
3220 compile_multilib:"32",
3221 }
Jiyong Park5baac542018-08-28 09:55:37 +09003222 cc_library_shared {
3223 name: "libHalInRecovery",
3224 recovery_available: true,
3225 vendor: true,
3226 }
Jiyong Park37b25202018-07-11 10:49:27 +09003227 `)
3228
3229 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003230 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003231 if len(variants) != 1 || !android.InList(arm64, variants) {
3232 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3233 }
3234
3235 variants = ctx.ModuleVariantsForTests("librecovery32")
3236 if android.InList(arm64, variants) {
3237 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3238 }
Jiyong Park5baac542018-08-28 09:55:37 +09003239
3240 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3241 if !recoveryModule.Platform() {
3242 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3243 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003244}
Jiyong Park5baac542018-08-28 09:55:37 +09003245
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003246func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3247 bp := `
3248 cc_prebuilt_test_library_shared {
3249 name: "test_lib",
3250 relative_install_path: "foo/bar/baz",
3251 srcs: ["srcpath/dontusethispath/baz.so"],
3252 }
3253
3254 cc_test {
3255 name: "main_test",
3256 data_libs: ["test_lib"],
3257 gtest: false,
3258 }
3259 `
3260
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003261 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003262 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003263 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003264 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3265
3266 ctx := testCcWithConfig(t, config)
3267 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3268 testBinary := module.(*Module).linker.(*testBinary)
3269 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3270 if err != nil {
3271 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3272 }
3273 if len(outputFiles) != 1 {
3274 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3275 }
3276 if len(testBinary.dataPaths()) != 1 {
3277 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3278 }
3279
3280 outputPath := outputFiles[0].String()
3281
3282 if !strings.HasSuffix(outputPath, "/main_test") {
3283 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3284 }
Colin Crossaa255532020-07-03 13:18:24 -07003285 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003286 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3287 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3288 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3289 }
3290}
3291
Jiyong Park7ed9de32018-10-15 22:25:07 +09003292func TestVersionedStubs(t *testing.T) {
3293 ctx := testCc(t, `
3294 cc_library_shared {
3295 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003296 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003297 stubs: {
3298 symbol_file: "foo.map.txt",
3299 versions: ["1", "2", "3"],
3300 },
3301 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003302
Jiyong Park7ed9de32018-10-15 22:25:07 +09003303 cc_library_shared {
3304 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003305 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003306 shared_libs: ["libFoo#1"],
3307 }`)
3308
3309 variants := ctx.ModuleVariantsForTests("libFoo")
3310 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003311 "android_arm64_armv8-a_shared",
3312 "android_arm64_armv8-a_shared_1",
3313 "android_arm64_armv8-a_shared_2",
3314 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003315 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003316 "android_arm_armv7-a-neon_shared",
3317 "android_arm_armv7-a-neon_shared_1",
3318 "android_arm_armv7-a-neon_shared_2",
3319 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003320 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003321 }
3322 variantsMismatch := false
3323 if len(variants) != len(expectedVariants) {
3324 variantsMismatch = true
3325 } else {
3326 for _, v := range expectedVariants {
3327 if !inList(v, variants) {
3328 variantsMismatch = false
3329 }
3330 }
3331 }
3332 if variantsMismatch {
3333 t.Errorf("variants of libFoo expected:\n")
3334 for _, v := range expectedVariants {
3335 t.Errorf("%q\n", v)
3336 }
3337 t.Errorf(", but got:\n")
3338 for _, v := range variants {
3339 t.Errorf("%q\n", v)
3340 }
3341 }
3342
Colin Cross7113d202019-11-20 16:39:12 -08003343 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003344 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003345 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003346 if !strings.Contains(libFlags, libFoo1StubPath) {
3347 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3348 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003349
Colin Cross7113d202019-11-20 16:39:12 -08003350 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003351 cFlags := libBarCompileRule.Args["cFlags"]
3352 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3353 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3354 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3355 }
Jiyong Park37b25202018-07-11 10:49:27 +09003356}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003357
Jooyung Hanb04a4992020-03-13 18:57:35 +09003358func TestVersioningMacro(t *testing.T) {
3359 for _, tc := range []struct{ moduleName, expected string }{
3360 {"libc", "__LIBC_API__"},
3361 {"libfoo", "__LIBFOO_API__"},
3362 {"libfoo@1", "__LIBFOO_1_API__"},
3363 {"libfoo-v1", "__LIBFOO_V1_API__"},
3364 {"libfoo.v1", "__LIBFOO_V1_API__"},
3365 } {
3366 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3367 }
3368}
3369
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003370func TestStaticExecutable(t *testing.T) {
3371 ctx := testCc(t, `
3372 cc_binary {
3373 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003374 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003375 static_executable: true,
3376 }`)
3377
Colin Cross7113d202019-11-20 16:39:12 -08003378 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003379 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3380 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003381 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003382 for _, lib := range systemStaticLibs {
3383 if !strings.Contains(libFlags, lib) {
3384 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3385 }
3386 }
3387 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3388 for _, lib := range systemSharedLibs {
3389 if strings.Contains(libFlags, lib) {
3390 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3391 }
3392 }
3393}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003394
3395func TestStaticDepsOrderWithStubs(t *testing.T) {
3396 ctx := testCc(t, `
3397 cc_binary {
3398 name: "mybin",
3399 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003400 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003401 static_executable: true,
3402 stl: "none",
3403 }
3404
3405 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003406 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003407 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003408 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003409 stl: "none",
3410 }
3411
3412 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003413 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003414 srcs: ["foo.c"],
3415 stl: "none",
3416 stubs: {
3417 versions: ["1"],
3418 },
3419 }`)
3420
Colin Cross0de8a1e2020-09-18 14:15:30 -07003421 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3422 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003423 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003424
3425 if !reflect.DeepEqual(actual, expected) {
3426 t.Errorf("staticDeps orderings were not propagated correctly"+
3427 "\nactual: %v"+
3428 "\nexpected: %v",
3429 actual,
3430 expected,
3431 )
3432 }
3433}
Jooyung Han38002912019-05-16 04:01:54 +09003434
Jooyung Hand48f3c32019-08-23 11:18:57 +09003435func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3436 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3437 cc_library {
3438 name: "libA",
3439 srcs: ["foo.c"],
3440 shared_libs: ["libB"],
3441 stl: "none",
3442 }
3443
3444 cc_library {
3445 name: "libB",
3446 srcs: ["foo.c"],
3447 enabled: false,
3448 stl: "none",
3449 }
3450 `)
3451}
3452
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003453// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3454// correctly.
3455func TestFuzzTarget(t *testing.T) {
3456 ctx := testCc(t, `
3457 cc_fuzz {
3458 name: "fuzz_smoke_test",
3459 srcs: ["foo.c"],
3460 }`)
3461
Paul Duffin075c4172019-12-19 19:06:13 +00003462 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003463 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3464}
3465
Jiyong Park29074592019-07-07 16:27:47 +09003466func TestAidl(t *testing.T) {
3467}
3468
Jooyung Han38002912019-05-16 04:01:54 +09003469func assertString(t *testing.T, got, expected string) {
3470 t.Helper()
3471 if got != expected {
3472 t.Errorf("expected %q got %q", expected, got)
3473 }
3474}
3475
3476func assertArrayString(t *testing.T, got, expected []string) {
3477 t.Helper()
3478 if len(got) != len(expected) {
3479 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3480 return
3481 }
3482 for i := range got {
3483 if got[i] != expected[i] {
3484 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3485 i, expected[i], expected, got[i], got)
3486 return
3487 }
3488 }
3489}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003490
Jooyung Han0302a842019-10-30 18:43:49 +09003491func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3492 t.Helper()
3493 assertArrayString(t, android.SortedStringKeys(m), expected)
3494}
3495
Colin Crosse1bb5d02019-09-24 14:55:04 -07003496func TestDefaults(t *testing.T) {
3497 ctx := testCc(t, `
3498 cc_defaults {
3499 name: "defaults",
3500 srcs: ["foo.c"],
3501 static: {
3502 srcs: ["bar.c"],
3503 },
3504 shared: {
3505 srcs: ["baz.c"],
3506 },
Liz Kammer3cf52112021-03-31 15:42:03 -04003507 bazel_module: {
3508 bp2build_available: true,
3509 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07003510 }
3511
3512 cc_library_static {
3513 name: "libstatic",
3514 defaults: ["defaults"],
3515 }
3516
3517 cc_library_shared {
3518 name: "libshared",
3519 defaults: ["defaults"],
3520 }
3521
3522 cc_library {
3523 name: "libboth",
3524 defaults: ["defaults"],
3525 }
3526
3527 cc_binary {
3528 name: "binary",
3529 defaults: ["defaults"],
3530 }`)
3531
3532 pathsToBase := func(paths android.Paths) []string {
3533 var ret []string
3534 for _, p := range paths {
3535 ret = append(ret, p.Base())
3536 }
3537 return ret
3538 }
3539
Colin Cross7113d202019-11-20 16:39:12 -08003540 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003541 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3542 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3543 }
Colin Cross7113d202019-11-20 16:39:12 -08003544 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003545 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3546 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3547 }
Colin Cross7113d202019-11-20 16:39:12 -08003548 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003549 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3550 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3551 }
3552
Colin Cross7113d202019-11-20 16:39:12 -08003553 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003554 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3555 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3556 }
Colin Cross7113d202019-11-20 16:39:12 -08003557 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003558 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3559 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3560 }
3561}
Colin Crosseabaedd2020-02-06 17:01:55 -08003562
3563func TestProductVariableDefaults(t *testing.T) {
3564 bp := `
3565 cc_defaults {
3566 name: "libfoo_defaults",
3567 srcs: ["foo.c"],
3568 cppflags: ["-DFOO"],
3569 product_variables: {
3570 debuggable: {
3571 cppflags: ["-DBAR"],
3572 },
3573 },
3574 }
3575
3576 cc_library {
3577 name: "libfoo",
3578 defaults: ["libfoo_defaults"],
3579 }
3580 `
3581
Paul Duffin8567f222021-03-23 00:02:06 +00003582 result := android.GroupFixturePreparers(
3583 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003584 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003585
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003586 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3587 variables.Debuggable = BoolPtr(true)
3588 }),
3589 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003590
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003591 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003592 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003593}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003594
3595func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3596 t.Parallel()
3597 bp := `
3598 cc_library_static {
3599 name: "libfoo",
3600 srcs: ["foo.c"],
3601 whole_static_libs: ["libbar"],
3602 }
3603
3604 cc_library_static {
3605 name: "libbar",
3606 whole_static_libs: ["libmissing"],
3607 }
3608 `
3609
Paul Duffin8567f222021-03-23 00:02:06 +00003610 result := android.GroupFixturePreparers(
3611 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003612 android.PrepareForTestWithAllowMissingDependencies,
3613 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003614
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003615 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003616 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003617
Paul Duffine84b1332021-03-12 11:59:43 +00003618 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003619
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003620 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003621 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003622}
Colin Crosse9fe2942020-11-10 18:12:15 -08003623
3624func TestInstallSharedLibs(t *testing.T) {
3625 bp := `
3626 cc_binary {
3627 name: "bin",
3628 host_supported: true,
3629 shared_libs: ["libshared"],
3630 runtime_libs: ["libruntime"],
3631 srcs: [":gen"],
3632 }
3633
3634 cc_library_shared {
3635 name: "libshared",
3636 host_supported: true,
3637 shared_libs: ["libtransitive"],
3638 }
3639
3640 cc_library_shared {
3641 name: "libtransitive",
3642 host_supported: true,
3643 }
3644
3645 cc_library_shared {
3646 name: "libruntime",
3647 host_supported: true,
3648 }
3649
3650 cc_binary_host {
3651 name: "tool",
3652 srcs: ["foo.cpp"],
3653 }
3654
3655 genrule {
3656 name: "gen",
3657 tools: ["tool"],
3658 out: ["gen.cpp"],
3659 cmd: "$(location tool) $(out)",
3660 }
3661 `
3662
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003663 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003664 ctx := testCcWithConfig(t, config)
3665
3666 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3667 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3668 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3669 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3670 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3671
3672 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3673 t.Errorf("expected host bin dependency %q, got %q", w, g)
3674 }
3675
3676 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3677 t.Errorf("expected host bin dependency %q, got %q", w, g)
3678 }
3679
3680 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3681 t.Errorf("expected host bin dependency %q, got %q", w, g)
3682 }
3683
3684 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3685 t.Errorf("expected host bin dependency %q, got %q", w, g)
3686 }
3687
3688 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3689 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3690 }
3691
3692 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3693 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3694 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3695 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3696
3697 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3698 t.Errorf("expected device bin dependency %q, got %q", w, g)
3699 }
3700
3701 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3702 t.Errorf("expected device bin dependency %q, got %q", w, g)
3703 }
3704
3705 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3706 t.Errorf("expected device bin dependency %q, got %q", w, g)
3707 }
3708
3709 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3710 t.Errorf("expected device bin dependency %q, got %q", w, g)
3711 }
3712
3713 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3714 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3715 }
3716
3717}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003718
3719func TestStubsLibReexportsHeaders(t *testing.T) {
3720 ctx := testCc(t, `
3721 cc_library_shared {
3722 name: "libclient",
3723 srcs: ["foo.c"],
3724 shared_libs: ["libfoo#1"],
3725 }
3726
3727 cc_library_shared {
3728 name: "libfoo",
3729 srcs: ["foo.c"],
3730 shared_libs: ["libbar"],
3731 export_shared_lib_headers: ["libbar"],
3732 stubs: {
3733 symbol_file: "foo.map.txt",
3734 versions: ["1", "2", "3"],
3735 },
3736 }
3737
3738 cc_library_shared {
3739 name: "libbar",
3740 export_include_dirs: ["include/libbar"],
3741 srcs: ["foo.c"],
3742 }`)
3743
3744 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3745
3746 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3747 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3748 }
3749}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003750
3751func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3752 ctx := testCc(t, `
3753 cc_library {
3754 name: "libfoo",
3755 srcs: ["a/Foo.aidl"],
3756 aidl: { flags: ["-Werror"], },
3757 }
3758 `)
3759
3760 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3761 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3762 aidlCommand := manifest.Commands[0].GetCommand()
3763 expectedAidlFlag := "-Werror"
3764 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3765 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3766 }
3767}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003768
Jiyong Parka008fb02021-03-16 17:15:53 +09003769func TestMinSdkVersionInClangTriple(t *testing.T) {
3770 ctx := testCc(t, `
3771 cc_library_shared {
3772 name: "libfoo",
3773 srcs: ["foo.c"],
3774 min_sdk_version: "29",
3775 }`)
3776
3777 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3778 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3779}
3780
Jiyong Parkfdaa5f72021-03-19 22:18:04 +09003781func TestMinSdkVersionsOfCrtObjects(t *testing.T) {
3782 ctx := testCc(t, `
3783 cc_object {
3784 name: "crt_foo",
3785 srcs: ["foo.c"],
3786 crt: true,
3787 stl: "none",
3788 min_sdk_version: "28",
3789
3790 }`)
3791
3792 arch := "android_arm64_armv8-a"
3793 for _, v := range []string{"", "28", "29", "30", "current"} {
3794 var variant string
3795 if v == "" {
3796 variant = arch
3797 } else {
3798 variant = arch + "_sdk_" + v
3799 }
3800 cflags := ctx.ModuleForTests("crt_foo", variant).Rule("cc").Args["cFlags"]
3801 vNum := v
3802 if v == "current" || v == "" {
3803 vNum = "10000"
3804 }
3805 expected := "-target aarch64-linux-android" + vNum + " "
3806 android.AssertStringDoesContain(t, "cflag", cflags, expected)
3807 }
3808}
3809
3810func TestUseCrtObjectOfCorrectVersion(t *testing.T) {
3811 ctx := testCc(t, `
3812 cc_binary {
3813 name: "bin",
3814 srcs: ["foo.c"],
3815 stl: "none",
3816 min_sdk_version: "29",
3817 sdk_version: "current",
3818 }
3819 `)
3820
3821 // Sdk variant uses the crt object of the matching min_sdk_version
3822 variant := "android_arm64_armv8-a_sdk"
3823 crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3824 android.AssertStringDoesContain(t, "crt dep of sdk variant", crt,
3825 variant+"_29/crtbegin_dynamic.o")
3826
3827 // platform variant uses the crt object built for platform
3828 variant = "android_arm64_armv8-a"
3829 crt = ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3830 android.AssertStringDoesContain(t, "crt dep of platform variant", crt,
3831 variant+"/crtbegin_dynamic.o")
3832}
3833
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003834type MemtagNoteType int
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003835
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003836const (
3837 None MemtagNoteType = iota + 1
3838 Sync
3839 Async
3840)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003841
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003842func (t MemtagNoteType) str() string {
3843 switch t {
3844 case None:
3845 return "none"
3846 case Sync:
3847 return "sync"
3848 case Async:
3849 return "async"
3850 default:
3851 panic("invalid note type")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003852 }
3853}
3854
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003855func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
3856 note_async := "note_memtag_heap_async"
3857 note_sync := "note_memtag_heap_sync"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003858
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003859 found := None
3860 implicits := m.Rule("ld").Implicits
3861 for _, lib := range implicits {
3862 if strings.Contains(lib.Rel(), note_async) {
3863 found = Async
3864 break
3865 } else if strings.Contains(lib.Rel(), note_sync) {
3866 found = Sync
3867 break
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003868 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003869 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003870
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003871 if found != expected {
3872 t.Errorf("Wrong Memtag note in target %q: found %q, expected %q", m.Module().(*Module).Name(), found.str(), expected.str())
3873 }
3874}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003875
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003876var prepareForTestWithMemtagHeap = android.GroupFixturePreparers(
3877 android.FixtureModifyMockFS(func(fs android.MockFS) {
3878 templateBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003879 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003880 name: "%[1]s_test",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003881 gtest: false,
3882 }
3883
3884 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003885 name: "%[1]s_test_false",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003886 gtest: false,
3887 sanitize: { memtag_heap: false },
3888 }
3889
3890 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003891 name: "%[1]s_test_true",
3892 gtest: false,
3893 sanitize: { memtag_heap: true },
3894 }
3895
3896 cc_test {
3897 name: "%[1]s_test_true_nodiag",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003898 gtest: false,
3899 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3900 }
3901
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003902 cc_test {
3903 name: "%[1]s_test_true_diag",
3904 gtest: false,
3905 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
3906 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003907
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003908 cc_binary {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003909 name: "%[1]s_binary",
3910 }
3911
3912 cc_binary {
3913 name: "%[1]s_binary_false",
3914 sanitize: { memtag_heap: false },
3915 }
3916
3917 cc_binary {
3918 name: "%[1]s_binary_true",
3919 sanitize: { memtag_heap: true },
3920 }
3921
3922 cc_binary {
3923 name: "%[1]s_binary_true_nodiag",
3924 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3925 }
3926
3927 cc_binary {
3928 name: "%[1]s_binary_true_diag",
3929 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003930 }
3931 `
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003932 subdirDefaultBp := fmt.Sprintf(templateBp, "default")
3933 subdirExcludeBp := fmt.Sprintf(templateBp, "exclude")
3934 subdirSyncBp := fmt.Sprintf(templateBp, "sync")
3935 subdirAsyncBp := fmt.Sprintf(templateBp, "async")
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003936
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003937 fs.Merge(android.MockFS{
3938 "subdir_default/Android.bp": []byte(subdirDefaultBp),
3939 "subdir_exclude/Android.bp": []byte(subdirExcludeBp),
3940 "subdir_sync/Android.bp": []byte(subdirSyncBp),
3941 "subdir_async/Android.bp": []byte(subdirAsyncBp),
3942 })
3943 }),
3944 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3945 variables.MemtagHeapExcludePaths = []string{"subdir_exclude"}
Evgenii Stepanov779b64e2021-04-09 14:33:10 -07003946 // "subdir_exclude" is covered by both include and exclude paths. Exclude wins.
3947 variables.MemtagHeapSyncIncludePaths = []string{"subdir_sync", "subdir_exclude"}
3948 variables.MemtagHeapAsyncIncludePaths = []string{"subdir_async", "subdir_exclude"}
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003949 }),
3950)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003951
3952func TestSanitizeMemtagHeap(t *testing.T) {
3953 variant := "android_arm64_armv8-a"
3954
Paul Duffin8567f222021-03-23 00:02:06 +00003955 result := android.GroupFixturePreparers(
3956 prepareForCcTest,
3957 prepareForTestWithMemtagHeap,
3958 ).RunTest(t)
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003959 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003960
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003961 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3962 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3963 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3964 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3965 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3966
3967 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), None)
3968 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3969 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3970 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3971 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3972
3973 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3974 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3975 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3976 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3977 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3978
3979 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3980 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3981 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3982 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3983 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3984
3985 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3986 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3987 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3988 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3989 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3990
3991 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3992 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3993 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3994 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3995 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3996
3997 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3998 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3999 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
4000 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
4001 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
4002
4003 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
4004 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
4005 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
4006 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
4007 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
4008}
4009
4010func TestSanitizeMemtagHeapWithSanitizeDevice(t *testing.T) {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004011 variant := "android_arm64_armv8-a"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004012
Paul Duffin8567f222021-03-23 00:02:06 +00004013 result := android.GroupFixturePreparers(
4014 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004015 prepareForTestWithMemtagHeap,
4016 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4017 variables.SanitizeDevice = []string{"memtag_heap"}
4018 }),
4019 ).RunTest(t)
4020 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004021
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08004022 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
4023 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
4024 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
4025 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
4026 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08004027
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08004028 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Async)
4029 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
4030 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
4031 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
4032 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
4033
4034 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
4035 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
4036 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
4037 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
4038 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
4039
4040 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
4041 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
4042 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
4043 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
4044 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
4045
4046 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
4047 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
4048 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
4049 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
4050 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
4051
4052 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
4053 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
4054 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
4055 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
4056 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
4057
4058 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
4059 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
4060 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
4061 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
4062 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
4063
4064 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
4065 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
4066 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
4067 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
4068 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
4069}
4070
4071func TestSanitizeMemtagHeapWithSanitizeDeviceDiag(t *testing.T) {
4072 variant := "android_arm64_armv8-a"
4073
Paul Duffin8567f222021-03-23 00:02:06 +00004074 result := android.GroupFixturePreparers(
4075 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004076 prepareForTestWithMemtagHeap,
4077 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4078 variables.SanitizeDevice = []string{"memtag_heap"}
4079 variables.SanitizeDeviceDiag = []string{"memtag_heap"}
4080 }),
4081 ).RunTest(t)
4082 ctx := result.TestContext
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08004083
4084 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
4085 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
4086 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Sync)
4087 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
4088 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
4089
4090 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Sync)
4091 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
4092 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Sync)
4093 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
4094 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
4095
4096 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
4097 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
4098 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Sync)
4099 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
4100 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
4101
4102 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
4103 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
4104 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Sync)
4105 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
4106 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
4107
4108 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
4109 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
4110 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Sync)
4111 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
4112 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
4113
4114 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Sync)
4115 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
4116 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Sync)
4117 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
4118 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
4119
4120 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
4121 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
4122 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
4123 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
4124 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
4125
4126 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
4127 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
4128 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
4129 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
4130 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004131}
Paul Duffin3cb603e2021-02-19 13:57:10 +00004132
4133func TestIncludeDirsExporting(t *testing.T) {
4134
4135 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4136 // embedded newline characters alone.
4137 trimIndentingSpaces := func(s string) string {
4138 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4139 }
4140
4141 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4142 t.Helper()
4143 expected = trimIndentingSpaces(expected)
4144 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4145 if expected != actual {
4146 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4147 }
4148 }
4149
4150 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4151
4152 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4153 t.Helper()
4154 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4155 name := module.Name()
4156
4157 for _, checker := range checkers {
4158 checker(t, name, exported)
4159 }
4160 }
4161
4162 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4163 return func(t *testing.T, name string, exported FlagExporterInfo) {
4164 t.Helper()
4165 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4166 }
4167 }
4168
4169 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4170 return func(t *testing.T, name string, exported FlagExporterInfo) {
4171 t.Helper()
4172 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4173 }
4174 }
4175
4176 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4177 return func(t *testing.T, name string, exported FlagExporterInfo) {
4178 t.Helper()
4179 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4180 }
4181 }
4182
4183 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4184 return func(t *testing.T, name string, exported FlagExporterInfo) {
4185 t.Helper()
4186 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4187 }
4188 }
4189
4190 genRuleModules := `
4191 genrule {
4192 name: "genrule_foo",
4193 cmd: "generate-foo",
4194 out: [
4195 "generated_headers/foo/generated_header.h",
4196 ],
4197 export_include_dirs: [
4198 "generated_headers",
4199 ],
4200 }
4201
4202 genrule {
4203 name: "genrule_bar",
4204 cmd: "generate-bar",
4205 out: [
4206 "generated_headers/bar/generated_header.h",
4207 ],
4208 export_include_dirs: [
4209 "generated_headers",
4210 ],
4211 }
4212 `
4213
4214 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4215 ctx := testCc(t, genRuleModules+`
4216 cc_library {
4217 name: "libfoo",
4218 srcs: ["foo.c"],
4219 export_include_dirs: ["foo/standard"],
4220 export_system_include_dirs: ["foo/system"],
4221 generated_headers: ["genrule_foo"],
4222 export_generated_headers: ["genrule_foo"],
4223 }
4224
4225 cc_library {
4226 name: "libbar",
4227 srcs: ["bar.c"],
4228 shared_libs: ["libfoo"],
4229 export_include_dirs: ["bar/standard"],
4230 export_system_include_dirs: ["bar/system"],
4231 generated_headers: ["genrule_bar"],
4232 export_generated_headers: ["genrule_bar"],
4233 }
4234 `)
4235 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4236 checkIncludeDirs(t, ctx, foo,
4237 expectedIncludeDirs(`
4238 foo/standard
4239 .intermediates/genrule_foo/gen/generated_headers
4240 `),
4241 expectedSystemIncludeDirs(`foo/system`),
4242 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4243 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4244 )
4245
4246 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4247 checkIncludeDirs(t, ctx, bar,
4248 expectedIncludeDirs(`
4249 bar/standard
4250 .intermediates/genrule_bar/gen/generated_headers
4251 `),
4252 expectedSystemIncludeDirs(`bar/system`),
4253 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4254 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4255 )
4256 })
4257
4258 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4259 ctx := testCc(t, genRuleModules+`
4260 cc_library {
4261 name: "libfoo",
4262 srcs: ["foo.c"],
4263 export_include_dirs: ["foo/standard"],
4264 export_system_include_dirs: ["foo/system"],
4265 generated_headers: ["genrule_foo"],
4266 export_generated_headers: ["genrule_foo"],
4267 }
4268
4269 cc_library {
4270 name: "libbar",
4271 srcs: ["bar.c"],
4272 whole_static_libs: ["libfoo"],
4273 export_include_dirs: ["bar/standard"],
4274 export_system_include_dirs: ["bar/system"],
4275 generated_headers: ["genrule_bar"],
4276 export_generated_headers: ["genrule_bar"],
4277 }
4278 `)
4279 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4280 checkIncludeDirs(t, ctx, foo,
4281 expectedIncludeDirs(`
4282 foo/standard
4283 .intermediates/genrule_foo/gen/generated_headers
4284 `),
4285 expectedSystemIncludeDirs(`foo/system`),
4286 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4287 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4288 )
4289
4290 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4291 checkIncludeDirs(t, ctx, bar,
4292 expectedIncludeDirs(`
4293 bar/standard
4294 foo/standard
4295 .intermediates/genrule_foo/gen/generated_headers
4296 .intermediates/genrule_bar/gen/generated_headers
4297 `),
4298 expectedSystemIncludeDirs(`
4299 bar/system
4300 foo/system
4301 `),
4302 expectedGeneratedHeaders(`
4303 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4304 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4305 `),
4306 expectedOrderOnlyDeps(`
4307 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4308 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4309 `),
4310 )
4311 })
4312
Paul Duffin3cb603e2021-02-19 13:57:10 +00004313 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4314 ctx := testCc(t, genRuleModules+`
4315 cc_library_shared {
4316 name: "libfoo",
4317 srcs: [
4318 "foo.c",
4319 "b.aidl",
4320 "a.proto",
4321 ],
4322 aidl: {
4323 export_aidl_headers: true,
4324 }
4325 }
4326 `)
4327 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4328 checkIncludeDirs(t, ctx, foo,
4329 expectedIncludeDirs(`
4330 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4331 `),
4332 expectedSystemIncludeDirs(``),
4333 expectedGeneratedHeaders(`
4334 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4335 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4336 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004337 `),
4338 expectedOrderOnlyDeps(`
4339 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4340 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4341 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004342 `),
4343 )
4344 })
4345
Paul Duffin3cb603e2021-02-19 13:57:10 +00004346 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4347 ctx := testCc(t, genRuleModules+`
4348 cc_library_shared {
4349 name: "libfoo",
4350 srcs: [
4351 "foo.c",
4352 "b.aidl",
4353 "a.proto",
4354 ],
4355 proto: {
4356 export_proto_headers: true,
4357 }
4358 }
4359 `)
4360 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4361 checkIncludeDirs(t, ctx, foo,
4362 expectedIncludeDirs(`
4363 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4364 `),
4365 expectedSystemIncludeDirs(``),
4366 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004367 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4368 `),
4369 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004370 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4371 `),
4372 )
4373 })
4374
Paul Duffin33056e82021-02-19 13:49:08 +00004375 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004376 ctx := testCc(t, genRuleModules+`
4377 cc_library_shared {
4378 name: "libfoo",
4379 srcs: [
4380 "foo.c",
4381 "a.sysprop",
4382 "b.aidl",
4383 "a.proto",
4384 ],
4385 }
4386 `)
4387 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4388 checkIncludeDirs(t, ctx, foo,
4389 expectedIncludeDirs(`
4390 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4391 `),
4392 expectedSystemIncludeDirs(``),
4393 expectedGeneratedHeaders(`
4394 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004395 `),
4396 expectedOrderOnlyDeps(`
4397 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
4398 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004399 `),
4400 )
4401 })
4402}