blob: 422df7310616cf4c6ac5cc67df0fb37f2e4feb80 [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"
Cory Barker9cfcf6d2022-07-22 17:22:02 +000023 "runtime"
Jeff Gaston294356f2017-09-27 17:05:30 -070024 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070025 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070026
Vinh Tran367d89d2023-04-28 11:21:25 -040027 "android/soong/aidl_library"
Colin Crosse1bb5d02019-09-24 14:55:04 -070028 "android/soong/android"
Sam Delmerico4e115cc2023-01-19 15:36:52 -050029 "android/soong/bazel/cquery"
Colin Cross74d1ec02015-04-28 13:30:13 -070030)
31
Yu Liue4312402023-01-18 09:15:31 -080032func init() {
33 registerTestMutators(android.InitRegistrationContext)
34}
35
Jiyong Park6a43f042017-10-12 23:05:00 +090036func TestMain(m *testing.M) {
Paul Duffinc3e6ce02021-03-22 23:21:32 +000037 os.Exit(m.Run())
Jiyong Park6a43f042017-10-12 23:05:00 +090038}
39
Paul Duffin2e6f90e2021-03-22 23:20:25 +000040var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000041 PrepareForTestWithCcIncludeVndk,
Vinh Trane6842942023-04-28 11:21:25 -040042 aidl_library.PrepareForTestWithAidlLibrary,
Paul Duffin02a3d652021-02-24 18:51:54 +000043 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
44 variables.DeviceVndkVersion = StringPtr("current")
45 variables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090046 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000047 }),
48)
49
Yu Liue4312402023-01-18 09:15:31 -080050var ccLibInApex = "cc_lib_in_apex"
51var apexVariationName = "apex28"
52var apexVersion = "28"
53
54func registerTestMutators(ctx android.RegistrationContext) {
55 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
56 ctx.BottomUp("apex", testApexMutator).Parallel()
57 ctx.BottomUp("mixed_builds_prep", mixedBuildsPrepareMutator).Parallel()
58 })
59}
60
61func mixedBuildsPrepareMutator(ctx android.BottomUpMutatorContext) {
62 if m := ctx.Module(); m.Enabled() {
63 if mixedBuildMod, ok := m.(android.MixedBuildBuildable); ok {
MarkDacekf47e1422023-04-19 16:47:36 +000064 if mixedBuildMod.IsMixedBuildSupported(ctx) && android.MixedBuildsEnabled(ctx) == android.MixedBuildEnabled {
Yu Liue4312402023-01-18 09:15:31 -080065 mixedBuildMod.QueueBazelCall(ctx)
66 }
67 }
68 }
69}
70
71func testApexMutator(mctx android.BottomUpMutatorContext) {
72 modules := mctx.CreateVariations(apexVariationName)
73 apexInfo := android.ApexInfo{
74 ApexVariationName: apexVariationName,
75 MinSdkVersion: android.ApiLevelForTest(apexVersion),
76 }
77 mctx.SetVariationProvider(modules[0], android.ApexInfoProvider, apexInfo)
78}
79
Paul Duffin8567f222021-03-23 00:02:06 +000080// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000081//
82// See testCc for an explanation as to how to stop using this deprecated method.
83//
84// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080085func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070086 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000087 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000088 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090089}
90
Paul Duffin8567f222021-03-23 00:02:06 +000091// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000092//
Paul Duffin8567f222021-03-23 00:02:06 +000093// 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 +000094// easier to customize the test behavior.
95//
96// 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 +000097// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000098// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
99// that it did not change the test behavior unexpectedly.
100//
101// deprecated
Logan Chienf3511742017-10-31 18:04:35 +0800102func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800103 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +0000104 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +0000105 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +0800106}
107
Paul Duffin8567f222021-03-23 00:02:06 +0000108// testCcNoVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000109//
110// See testCc for an explanation as to how to stop using this deprecated method.
111//
112// deprecated
Logan Chienf3511742017-10-31 18:04:35 +0800113func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800114 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000115 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900116 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Logan Chienf3511742017-10-31 18:04:35 +0800117
Colin Cross98be1bb2019-12-13 20:41:13 -0800118 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800119}
120
Paul Duffin8567f222021-03-23 00:02:06 +0000121// testCcNoProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000122//
123// See testCc for an explanation as to how to stop using this deprecated method.
124//
125// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +0900126func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
127 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000128 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun8a2600c2020-12-07 12:44:03 +0900129 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900130 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun8a2600c2020-12-07 12:44:03 +0900131
132 return testCcWithConfig(t, config)
133}
134
Paul Duffin8567f222021-03-23 00:02:06 +0000135// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000136//
137// See testCc for an explanation as to how to stop using this deprecated method.
138//
139// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900140func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800141 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800142
Paul Duffin8567f222021-03-23 00:02:06 +0000143 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000144 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
145 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800146}
147
Paul Duffin8567f222021-03-23 00:02:06 +0000148// testCcError runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000149//
150// See testCc for an explanation as to how to stop using this deprecated method.
151//
152// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900153func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900154 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000155 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900156 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900157 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900158 testCcErrorWithConfig(t, pattern, config)
159 return
160}
161
Paul Duffin8567f222021-03-23 00:02:06 +0000162// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000163//
164// See testCc for an explanation as to how to stop using this deprecated method.
165//
166// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900167func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900168 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000169 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900170 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
171 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900172 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900173 testCcErrorWithConfig(t, pattern, config)
174 return
175}
176
Logan Chienf3511742017-10-31 18:04:35 +0800177const (
Colin Cross7113d202019-11-20 16:39:12 -0800178 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900179 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
180 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800181 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800182)
183
Paul Duffindb462dd2021-03-21 22:01:55 +0000184// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
185// running it in a fixture that requires all source files to exist.
186func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
187 android.GroupFixturePreparers(
188 PrepareForTestWithCcDefaultModules,
189 android.PrepareForTestDisallowNonExistentPaths,
190 ).RunTest(t)
191}
192
Jiyong Park6a43f042017-10-12 23:05:00 +0900193func TestVendorSrc(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400194 t.Parallel()
Jiyong Park6a43f042017-10-12 23:05:00 +0900195 ctx := testCc(t, `
196 cc_library {
197 name: "libTest",
198 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700199 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800200 nocrt: true,
201 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900202 vendor_available: true,
203 target: {
204 vendor: {
205 srcs: ["bar.c"],
206 },
207 },
208 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900209 `)
210
Logan Chienf3511742017-10-31 18:04:35 +0800211 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900212 var objs []string
213 for _, o := range ld.Inputs {
214 objs = append(objs, o.Base())
215 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800216 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900217 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
218 }
219}
220
Justin Yun7f99ec72021-04-12 13:19:28 +0900221func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
222 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
223 partitionDefined := false
224 checkPartition := func(specific bool, partition string) {
225 if specific {
226 if expected != partition && !partitionDefined {
227 // The variant is installed to the 'partition'
228 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
229 }
230 partitionDefined = true
231 } else {
232 // The variant is not installed to the 'partition'
233 if expected == partition {
234 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
235 }
236 }
237 }
238 socSpecific := func(m *Module) bool {
239 return m.SocSpecific() || m.socSpecificModuleContext()
240 }
241 deviceSpecific := func(m *Module) bool {
242 return m.DeviceSpecific() || m.deviceSpecificModuleContext()
243 }
244 productSpecific := func(m *Module) bool {
245 return m.ProductSpecific() || m.productSpecificModuleContext()
246 }
247 systemExtSpecific := func(m *Module) bool {
248 return m.SystemExtSpecific()
249 }
250 checkPartition(socSpecific(mod), "vendor")
251 checkPartition(deviceSpecific(mod), "odm")
252 checkPartition(productSpecific(mod), "product")
253 checkPartition(systemExtSpecific(mod), "system_ext")
254 if !partitionDefined && expected != "system" {
255 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
256 " but installed to system partition", variant, name, expected)
257 }
258}
259
260func TestInstallPartition(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400261 t.Parallel()
Justin Yun7f99ec72021-04-12 13:19:28 +0900262 t.Helper()
263 ctx := prepareForCcTest.RunTestWithBp(t, `
264 cc_library {
265 name: "libsystem",
266 }
267 cc_library {
268 name: "libsystem_ext",
269 system_ext_specific: true,
270 }
271 cc_library {
272 name: "libproduct",
273 product_specific: true,
274 }
275 cc_library {
276 name: "libvendor",
277 vendor: true,
278 }
279 cc_library {
280 name: "libodm",
281 device_specific: true,
282 }
283 cc_library {
284 name: "liball_available",
285 vendor_available: true,
286 product_available: true,
287 }
288 cc_library {
289 name: "libsystem_ext_all_available",
290 system_ext_specific: true,
291 vendor_available: true,
292 product_available: true,
293 }
294 cc_library {
295 name: "liball_available_odm",
296 odm_available: true,
297 product_available: true,
298 }
299 cc_library {
300 name: "libproduct_vendoravailable",
301 product_specific: true,
302 vendor_available: true,
303 }
304 cc_library {
305 name: "libproduct_odmavailable",
306 product_specific: true,
307 odm_available: true,
308 }
309 `).TestContext
310
311 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
312 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
313 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
314 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
315 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
316
317 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
318 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
319 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
320
321 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
322 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
323 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
324
325 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
326 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
327 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
328
329 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
330 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
331
332 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
333 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
334}
335
Logan Chienf3511742017-10-31 18:04:35 +0800336func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900337 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800338
Logan Chiend3c59a22018-03-29 14:08:15 +0800339 t.Helper()
340
Justin Yun0ecf0b22020-02-28 15:07:59 +0900341 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800342
343 // Check library properties.
344 lib, ok := mod.compiler.(*libraryDecorator)
345 if !ok {
346 t.Errorf("%q must have libraryDecorator", name)
347 } else if lib.baseInstaller.subDir != subDir {
348 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
349 lib.baseInstaller.subDir)
350 }
351
352 // Check VNDK properties.
353 if mod.vndkdep == nil {
354 t.Fatalf("%q must have `vndkdep`", name)
355 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700356 if !mod.IsVndk() {
357 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800358 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400359 if mod.IsVndkSp() != isVndkSp {
360 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800361 }
362
363 // Check VNDK extension properties.
364 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500365 if mod.IsVndkExt() != isVndkExt {
366 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800367 }
368
369 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
370 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
371 }
372}
373
Jooyung Han2216fb12019-11-06 16:46:15 +0900374func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
375 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800376 content := android.ContentFromFileRuleForTests(t, params)
377 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900378 assertArrayString(t, actual, expected)
379}
380
Jooyung Han097087b2019-10-22 19:32:18 +0900381func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
382 t.Helper()
383 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900384 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
385}
386
387func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
388 t.Helper()
Colin Cross45bce852021-11-11 22:47:54 -0800389 got := ctx.ModuleForTests(module, "android_common").Module().(*vndkLibrariesTxt).fileNames
Colin Cross78212242021-01-06 14:51:30 -0800390 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900391}
392
Logan Chienf3511742017-10-31 18:04:35 +0800393func TestVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400394 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800395 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800396 cc_library {
397 name: "libvndk",
398 vendor_available: true,
399 vndk: {
400 enabled: true,
401 },
402 nocrt: true,
403 }
404
405 cc_library {
406 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900407 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800408 vndk: {
409 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900410 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800411 },
412 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900413 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800414 }
415
416 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900417 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800418 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900419 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800420 vndk: {
421 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900422 },
423 nocrt: true,
424 target: {
425 vendor: {
426 cflags: ["-DTEST"],
427 },
428 product: {
429 cflags: ["-DTEST"],
430 },
431 },
432 }
433
434 cc_library {
435 name: "libvndk_sp",
436 vendor_available: true,
437 vndk: {
438 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800439 support_system_process: true,
440 },
441 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900442 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800443 }
444
445 cc_library {
446 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900447 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800448 vndk: {
449 enabled: true,
450 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900451 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800452 },
453 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900454 target: {
455 vendor: {
456 suffix: "-x",
457 },
458 },
Logan Chienf3511742017-10-31 18:04:35 +0800459 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900460
461 cc_library {
462 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900463 vendor_available: true,
464 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900465 vndk: {
466 enabled: true,
467 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900468 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900469 },
470 nocrt: true,
471 target: {
472 vendor: {
473 suffix: "-x",
474 },
475 product: {
476 suffix: "-x",
477 },
478 },
479 }
480
Justin Yun450ae722021-04-16 19:58:18 +0900481 cc_library {
482 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700483 llndk: {
484 symbol_file: "libllndk.map.txt",
485 export_llndk_headers: ["libllndk_headers"],
486 }
Justin Yun450ae722021-04-16 19:58:18 +0900487 }
488
Justin Yun611e8862021-05-24 18:17:33 +0900489 cc_library {
490 name: "libclang_rt.hwasan-llndk",
491 llndk: {
492 symbol_file: "libclang_rt.hwasan.map.txt",
493 }
494 }
495
Colin Cross627280f2021-04-26 16:53:58 -0700496 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900497 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700498 llndk: {
499 llndk_headers: true,
500 },
Justin Yun450ae722021-04-16 19:58:18 +0900501 export_include_dirs: ["include"],
502 }
503
Colin Crosse4e44bc2020-12-28 13:50:21 -0800504 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900505 name: "llndk.libraries.txt",
506 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800507 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900508 name: "vndkcore.libraries.txt",
509 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800510 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900511 name: "vndksp.libraries.txt",
512 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800513 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900514 name: "vndkprivate.libraries.txt",
515 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800516 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900517 name: "vndkproduct.libraries.txt",
518 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800519 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900520 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800521 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900522 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800523 `
524
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000525 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800526 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900527 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900528 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800529
530 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800531
Jooyung Han261e1582020-10-20 18:54:21 +0900532 // subdir == "" because VNDK libs are not supposed to be installed separately.
533 // They are installed as part of VNDK APEX instead.
534 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
535 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900536 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900537 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
538 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900539 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900540
Justin Yun6977e8a2020-10-29 18:24:11 +0900541 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
542 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900543
Inseob Kim1f086e22019-05-09 13:29:15 +0900544 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900545 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000546 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900547
548 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
549 "arm64", "armv8-a"))
550 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
551 "arm", "armv7-a-neon"))
552
553 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
554 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900555 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
556
Inseob Kim1f086e22019-05-09 13:29:15 +0900557 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
558 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900559 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900560
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900561 variant := "android_vendor.29_arm64_armv8-a_shared"
562 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900563
Inseob Kim7f283f42020-06-01 21:53:49 +0900564 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
565
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400566 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
567 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
568 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
569 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
570 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
571 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
572 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
573 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900574
Jooyung Han39edb6c2019-11-06 16:53:07 +0900575 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Colin Cross45bce852021-11-11 22:47:54 -0800576 CheckSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "android_common")
577 CheckSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "android_common")
578 CheckSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "android_common")
579 CheckSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "android_common")
580 CheckSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "android_common")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900581
Jooyung Han097087b2019-10-22 19:32:18 +0900582 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
583 "LLNDK: libc.so",
584 "LLNDK: libdl.so",
585 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900586 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900587 "LLNDK: libm.so",
588 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900589 "VNDK-SP: libvndk_sp-x.so",
590 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900591 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900592 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900593 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900594 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900595 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900596 "VNDK-private: libvndk-private.so",
597 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900598 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900599 "VNDK-product: libc++.so",
600 "VNDK-product: libvndk_product.so",
601 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900602 })
Justin Yun611e8862021-05-24 18:17:33 +0900603 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libclang_rt.hwasan-llndk.so", "libdl.so", "libft2.so", "libllndk.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900604 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
605 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
606 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 +0900607 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 +0900608 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
609}
610
Yo Chiangbba545e2020-06-09 16:15:37 +0800611func TestVndkWithHostSupported(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400612 t.Parallel()
Yo Chiangbba545e2020-06-09 16:15:37 +0800613 ctx := testCc(t, `
614 cc_library {
615 name: "libvndk_host_supported",
616 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900617 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800618 vndk: {
619 enabled: true,
620 },
621 host_supported: true,
622 }
623
624 cc_library {
625 name: "libvndk_host_supported_but_disabled_on_device",
626 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900627 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800628 vndk: {
629 enabled: true,
630 },
631 host_supported: true,
632 enabled: false,
633 target: {
634 host: {
635 enabled: true,
636 }
637 }
638 }
639
Colin Crosse4e44bc2020-12-28 13:50:21 -0800640 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800641 name: "vndkcore.libraries.txt",
642 }
643 `)
644
645 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
646}
647
Jooyung Han2216fb12019-11-06 16:46:15 +0900648func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400649 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800650 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800651 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900652 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800653 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800654 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000655 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800656 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900657 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800658 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900659
Colin Cross45bce852021-11-11 22:47:54 -0800660 module := ctx.ModuleForTests("llndk.libraries.txt", "android_common")
Colin Crossaa255532020-07-03 13:18:24 -0700661 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900662 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900663}
664
665func TestVndkUsingCoreVariant(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400666 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800667 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900668 cc_library {
669 name: "libvndk",
670 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900671 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900672 vndk: {
673 enabled: true,
674 },
675 nocrt: true,
676 }
677
678 cc_library {
679 name: "libvndk_sp",
680 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900681 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900682 vndk: {
683 enabled: true,
684 support_system_process: true,
685 },
686 nocrt: true,
687 }
688
689 cc_library {
690 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900691 vendor_available: true,
692 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900693 vndk: {
694 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900695 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900696 },
697 nocrt: true,
698 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900699
Colin Crosse4e44bc2020-12-28 13:50:21 -0800700 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900701 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800702 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900703 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800704 `
705
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000706 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800707 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900708 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800709 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
710
711 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
712
713 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900714
Jooyung Han2216fb12019-11-06 16:46:15 +0900715 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900716}
717
Chris Parsons79d66a52020-06-05 17:26:16 -0400718func TestDataLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400719 t.Parallel()
Chris Parsons79d66a52020-06-05 17:26:16 -0400720 bp := `
721 cc_test_library {
722 name: "test_lib",
723 srcs: ["test_lib.cpp"],
724 gtest: false,
725 }
726
727 cc_test {
728 name: "main_test",
729 data_libs: ["test_lib"],
730 gtest: false,
731 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400732 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400733
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000734 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400735 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900736 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400737 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
738
739 ctx := testCcWithConfig(t, config)
740 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
741 testBinary := module.(*Module).linker.(*testBinary)
742 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
743 if err != nil {
744 t.Errorf("Expected cc_test to produce output files, error: %s", err)
745 return
746 }
747 if len(outputFiles) != 1 {
748 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
749 return
750 }
751 if len(testBinary.dataPaths()) != 1 {
752 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
753 return
754 }
755
756 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400757 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400758
759 if !strings.HasSuffix(outputPath, "/main_test") {
760 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
761 return
762 }
763 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
764 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
765 return
766 }
767}
768
Chris Parsons216e10a2020-07-09 17:12:52 -0400769func TestDataLibsRelativeInstallPath(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400770 t.Parallel()
Chris Parsons216e10a2020-07-09 17:12:52 -0400771 bp := `
772 cc_test_library {
773 name: "test_lib",
774 srcs: ["test_lib.cpp"],
775 relative_install_path: "foo/bar/baz",
776 gtest: false,
777 }
778
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400779 cc_binary {
780 name: "test_bin",
781 relative_install_path: "foo/bar/baz",
782 compile_multilib: "both",
783 }
784
Chris Parsons216e10a2020-07-09 17:12:52 -0400785 cc_test {
786 name: "main_test",
787 data_libs: ["test_lib"],
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400788 data_bins: ["test_bin"],
Chris Parsons216e10a2020-07-09 17:12:52 -0400789 gtest: false,
790 }
791 `
792
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000793 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400794 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900795 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400796 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
797
798 ctx := testCcWithConfig(t, config)
799 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
800 testBinary := module.(*Module).linker.(*testBinary)
801 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
802 if err != nil {
803 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
804 }
805 if len(outputFiles) != 1 {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400806 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
Chris Parsons216e10a2020-07-09 17:12:52 -0400807 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400808 if len(testBinary.dataPaths()) != 2 {
809 t.Fatalf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
Chris Parsons216e10a2020-07-09 17:12:52 -0400810 }
811
812 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400813
814 if !strings.HasSuffix(outputPath, "/main_test") {
815 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
816 }
Colin Crossaa255532020-07-03 13:18:24 -0700817 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400818 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
819 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400820 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400821 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400822 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":test_bin:foo/bar/baz") {
823 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_bin:foo/bar/baz`,"+
824 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
825 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400826}
827
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000828func TestTestBinaryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400829 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000830 bp := `
831 cc_test {
832 name: "main_test",
833 srcs: ["main_test.cpp"],
834 test_suites: [
835 "suite_1",
836 "suite_2",
837 ],
838 gtest: false,
839 }
840 `
841
842 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
843 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
844
845 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
846 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
847 if len(compatEntries) != 2 {
848 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
849 }
850 if compatEntries[0] != "suite_1" {
851 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
852 " but was '%s'", compatEntries[0])
853 }
854 if compatEntries[1] != "suite_2" {
855 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
856 " but was '%s'", compatEntries[1])
857 }
858}
859
860func TestTestLibraryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400861 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000862 bp := `
863 cc_test_library {
864 name: "main_test_lib",
865 srcs: ["main_test_lib.cpp"],
866 test_suites: [
867 "suite_1",
868 "suite_2",
869 ],
870 gtest: false,
871 }
872 `
873
874 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
875 module := ctx.ModuleForTests("main_test_lib", "android_arm_armv7-a-neon_shared").Module()
876
877 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
878 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
879 if len(compatEntries) != 2 {
880 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
881 }
882 if compatEntries[0] != "suite_1" {
883 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
884 " but was '%s'", compatEntries[0])
885 }
886 if compatEntries[1] != "suite_2" {
887 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
888 " but was '%s'", compatEntries[1])
889 }
890}
891
Jooyung Han0302a842019-10-30 18:43:49 +0900892func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400893 t.Parallel()
Jooyung Han2216fb12019-11-06 16:46:15 +0900894 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900895 cc_library {
896 name: "libvndk",
897 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900898 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900899 vndk: {
900 enabled: true,
901 },
902 nocrt: true,
903 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900904 cc_library {
905 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900906 vendor_available: true,
907 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900908 vndk: {
909 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900910 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900911 },
912 nocrt: true,
913 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800914
915 cc_library {
916 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700917 llndk: {
918 symbol_file: "libllndk.map.txt",
919 export_llndk_headers: ["libllndk_headers"],
920 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800921 }
922
Colin Cross627280f2021-04-26 16:53:58 -0700923 cc_library_headers {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800924 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700925 llndk: {
926 symbol_file: "libllndk.map.txt",
927 },
Colin Crossb5f6fa62021-01-06 17:05:04 -0800928 export_include_dirs: ["include"],
929 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900930 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900931
932 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
933 "LLNDK: libc.so",
934 "LLNDK: libdl.so",
935 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800936 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900937 "LLNDK: libm.so",
938 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900939 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900940 "VNDK-core: libvndk.so",
941 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900942 "VNDK-private: libvndk-private.so",
943 "VNDK-product: libc++.so",
944 "VNDK-product: libvndk-private.so",
945 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900946 })
Logan Chienf3511742017-10-31 18:04:35 +0800947}
948
Justin Yun63e9ec72020-10-29 16:49:43 +0900949func TestVndkModuleError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400950 t.Parallel()
Justin Yun63e9ec72020-10-29 16:49:43 +0900951 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900952 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900953 cc_library {
954 name: "libvndk",
955 vndk: {
956 enabled: true,
957 },
958 nocrt: true,
959 }
960 `)
961
Justin Yunc0d8c492021-01-07 17:45:31 +0900962 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900963 cc_library {
964 name: "libvndk",
965 product_available: true,
966 vndk: {
967 enabled: true,
968 },
969 nocrt: true,
970 }
971 `)
972
Justin Yun6977e8a2020-10-29 18:24:11 +0900973 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
974 cc_library {
975 name: "libvndkprop",
976 vendor_available: true,
977 product_available: true,
978 vndk: {
979 enabled: true,
980 },
981 nocrt: true,
982 target: {
983 vendor: {
984 cflags: ["-DTEST",],
985 },
986 },
987 }
988 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900989}
990
Logan Chiend3c59a22018-03-29 14:08:15 +0800991func TestVndkDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400992 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +0800993 // Check whether an error is emitted when a VNDK lib depends on a system lib.
994 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
995 cc_library {
996 name: "libvndk",
997 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900998 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800999 vndk: {
1000 enabled: true,
1001 },
1002 shared_libs: ["libfwk"], // Cause error
1003 nocrt: true,
1004 }
1005
1006 cc_library {
1007 name: "libfwk",
1008 nocrt: true,
1009 }
1010 `)
1011
1012 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
1013 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1014 cc_library {
1015 name: "libvndk",
1016 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001017 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001018 vndk: {
1019 enabled: true,
1020 },
1021 shared_libs: ["libvendor"], // Cause error
1022 nocrt: true,
1023 }
1024
1025 cc_library {
1026 name: "libvendor",
1027 vendor: true,
1028 nocrt: true,
1029 }
1030 `)
1031
1032 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
1033 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1034 cc_library {
1035 name: "libvndk_sp",
1036 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001037 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001038 vndk: {
1039 enabled: true,
1040 support_system_process: true,
1041 },
1042 shared_libs: ["libfwk"], // Cause error
1043 nocrt: true,
1044 }
1045
1046 cc_library {
1047 name: "libfwk",
1048 nocrt: true,
1049 }
1050 `)
1051
1052 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
1053 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1054 cc_library {
1055 name: "libvndk_sp",
1056 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001057 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001058 vndk: {
1059 enabled: true,
1060 support_system_process: true,
1061 },
1062 shared_libs: ["libvendor"], // Cause error
1063 nocrt: true,
1064 }
1065
1066 cc_library {
1067 name: "libvendor",
1068 vendor: true,
1069 nocrt: true,
1070 }
1071 `)
1072
1073 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
1074 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1075 cc_library {
1076 name: "libvndk_sp",
1077 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001078 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001079 vndk: {
1080 enabled: true,
1081 support_system_process: true,
1082 },
1083 shared_libs: ["libvndk"], // Cause error
1084 nocrt: true,
1085 }
1086
1087 cc_library {
1088 name: "libvndk",
1089 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001090 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001091 vndk: {
1092 enabled: true,
1093 },
1094 nocrt: true,
1095 }
1096 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001097
1098 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1099 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1100 cc_library {
1101 name: "libvndk",
1102 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001103 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001104 vndk: {
1105 enabled: true,
1106 },
1107 shared_libs: ["libnonvndk"],
1108 nocrt: true,
1109 }
1110
1111 cc_library {
1112 name: "libnonvndk",
1113 vendor_available: true,
1114 nocrt: true,
1115 }
1116 `)
1117
1118 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1119 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1120 cc_library {
1121 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001122 vendor_available: true,
1123 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001124 vndk: {
1125 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001126 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001127 },
1128 shared_libs: ["libnonvndk"],
1129 nocrt: true,
1130 }
1131
1132 cc_library {
1133 name: "libnonvndk",
1134 vendor_available: true,
1135 nocrt: true,
1136 }
1137 `)
1138
1139 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1140 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1141 cc_library {
1142 name: "libvndksp",
1143 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001144 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001145 vndk: {
1146 enabled: true,
1147 support_system_process: true,
1148 },
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 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1161 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1162 cc_library {
1163 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001164 vendor_available: true,
1165 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001166 vndk: {
1167 enabled: true,
1168 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001169 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001170 },
1171 shared_libs: ["libnonvndk"],
1172 nocrt: true,
1173 }
1174
1175 cc_library {
1176 name: "libnonvndk",
1177 vendor_available: true,
1178 nocrt: true,
1179 }
1180 `)
1181}
1182
1183func TestDoubleLoadbleDep(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001184 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001185 // okay to link : LLNDK -> double_loadable VNDK
1186 testCc(t, `
1187 cc_library {
1188 name: "libllndk",
1189 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001190 llndk: {
1191 symbol_file: "libllndk.map.txt",
1192 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001193 }
1194
1195 cc_library {
1196 name: "libdoubleloadable",
1197 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001198 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001199 vndk: {
1200 enabled: true,
1201 },
1202 double_loadable: true,
1203 }
1204 `)
1205 // okay to link : LLNDK -> VNDK-SP
1206 testCc(t, `
1207 cc_library {
1208 name: "libllndk",
1209 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001210 llndk: {
1211 symbol_file: "libllndk.map.txt",
1212 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001213 }
1214
1215 cc_library {
1216 name: "libvndksp",
1217 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001218 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001219 vndk: {
1220 enabled: true,
1221 support_system_process: true,
1222 },
1223 }
1224 `)
1225 // okay to link : double_loadable -> double_loadable
1226 testCc(t, `
1227 cc_library {
1228 name: "libdoubleloadable1",
1229 shared_libs: ["libdoubleloadable2"],
1230 vendor_available: true,
1231 double_loadable: true,
1232 }
1233
1234 cc_library {
1235 name: "libdoubleloadable2",
1236 vendor_available: true,
1237 double_loadable: true,
1238 }
1239 `)
1240 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1241 testCc(t, `
1242 cc_library {
1243 name: "libdoubleloadable",
1244 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001245 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001246 vndk: {
1247 enabled: true,
1248 },
1249 double_loadable: true,
1250 shared_libs: ["libnondoubleloadable"],
1251 }
1252
1253 cc_library {
1254 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001255 vendor_available: true,
1256 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001257 vndk: {
1258 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001259 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001260 },
1261 double_loadable: true,
1262 }
1263 `)
1264 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1265 testCc(t, `
1266 cc_library {
1267 name: "libllndk",
1268 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001269 llndk: {
1270 symbol_file: "libllndk.map.txt",
1271 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001272 }
1273
1274 cc_library {
1275 name: "libcoreonly",
1276 shared_libs: ["libvendoravailable"],
1277 }
1278
1279 // indirect dependency of LLNDK
1280 cc_library {
1281 name: "libvendoravailable",
1282 vendor_available: true,
1283 double_loadable: true,
1284 }
1285 `)
1286}
1287
1288func TestDoubleLoadableDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001289 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001290 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1291 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1292 cc_library {
1293 name: "libllndk",
1294 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001295 llndk: {
1296 symbol_file: "libllndk.map.txt",
1297 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001298 }
1299
1300 cc_library {
1301 name: "libnondoubleloadable",
1302 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001303 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001304 vndk: {
1305 enabled: true,
1306 },
1307 }
1308 `)
1309
1310 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1311 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1312 cc_library {
1313 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001314 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001315 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001316 llndk: {
1317 symbol_file: "libllndk.map.txt",
1318 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001319 }
1320
1321 cc_library {
1322 name: "libnondoubleloadable",
1323 vendor_available: true,
1324 }
1325 `)
1326
Jooyung Hana70f0672019-01-18 15:20:43 +09001327 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1328 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1329 cc_library {
1330 name: "libllndk",
1331 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001332 llndk: {
1333 symbol_file: "libllndk.map.txt",
1334 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001335 }
1336
1337 cc_library {
1338 name: "libcoreonly",
1339 shared_libs: ["libvendoravailable"],
1340 }
1341
1342 // indirect dependency of LLNDK
1343 cc_library {
1344 name: "libvendoravailable",
1345 vendor_available: true,
1346 }
1347 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001348
1349 // The error is not from 'client' but from 'libllndk'
1350 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1351 cc_library {
1352 name: "client",
1353 vendor_available: true,
1354 double_loadable: true,
1355 shared_libs: ["libllndk"],
1356 }
1357 cc_library {
1358 name: "libllndk",
1359 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001360 llndk: {
1361 symbol_file: "libllndk.map.txt",
1362 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001363 }
1364 cc_library {
1365 name: "libnondoubleloadable",
1366 vendor_available: true,
1367 }
1368 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001369}
1370
Jooyung Han479ca172020-10-19 18:51:07 +09001371func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001372 t.Parallel()
Jooyung Han479ca172020-10-19 18:51:07 +09001373 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1374 cc_library {
1375 name: "libvndksp",
1376 shared_libs: ["libanothervndksp"],
1377 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001378 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001379 vndk: {
1380 enabled: true,
1381 support_system_process: true,
1382 }
1383 }
1384
1385 cc_library {
1386 name: "libllndk",
1387 shared_libs: ["libanothervndksp"],
1388 }
1389
Jooyung Han479ca172020-10-19 18:51:07 +09001390 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) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001398 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001399 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001400 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001401 cc_library {
1402 name: "libvndk",
1403 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001404 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001405 vndk: {
1406 enabled: true,
1407 },
1408 nocrt: true,
1409 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001410 cc_library {
1411 name: "libvndk2",
1412 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001413 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001414 vndk: {
1415 enabled: true,
1416 },
1417 target: {
1418 vendor: {
1419 suffix: "-suffix",
1420 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001421 product: {
1422 suffix: "-suffix",
1423 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001424 },
1425 nocrt: true,
1426 }
Logan Chienf3511742017-10-31 18:04:35 +08001427
1428 cc_library {
1429 name: "libvndk_ext",
1430 vendor: true,
1431 vndk: {
1432 enabled: true,
1433 extends: "libvndk",
1434 },
1435 nocrt: true,
1436 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001437
1438 cc_library {
1439 name: "libvndk2_ext",
1440 vendor: true,
1441 vndk: {
1442 enabled: true,
1443 extends: "libvndk2",
1444 },
1445 nocrt: true,
1446 }
Logan Chienf3511742017-10-31 18:04:35 +08001447
Justin Yun0ecf0b22020-02-28 15:07:59 +09001448 cc_library {
1449 name: "libvndk_ext_product",
1450 product_specific: true,
1451 vndk: {
1452 enabled: true,
1453 extends: "libvndk",
1454 },
1455 nocrt: true,
1456 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001457
Justin Yun0ecf0b22020-02-28 15:07:59 +09001458 cc_library {
1459 name: "libvndk2_ext_product",
1460 product_specific: true,
1461 vndk: {
1462 enabled: true,
1463 extends: "libvndk2",
1464 },
1465 nocrt: true,
1466 }
1467 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001468 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001469 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1470 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001471 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001472
1473 ctx := testCcWithConfig(t, config)
1474
1475 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1476 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1477
1478 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1479 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1480
1481 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1482 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001483}
1484
Logan Chiend3c59a22018-03-29 14:08:15 +08001485func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001486 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001487 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1488 ctx := testCcNoVndk(t, `
1489 cc_library {
1490 name: "libvndk",
1491 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001492 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001493 vndk: {
1494 enabled: true,
1495 },
1496 nocrt: true,
1497 }
1498
1499 cc_library {
1500 name: "libvndk_ext",
1501 vendor: true,
1502 vndk: {
1503 enabled: true,
1504 extends: "libvndk",
1505 },
1506 nocrt: true,
1507 }
1508 `)
1509
1510 // Ensures that the core variant of "libvndk_ext" can be found.
1511 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1512 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1513 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1514 }
1515}
1516
Justin Yun0ecf0b22020-02-28 15:07:59 +09001517func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001518 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001519 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001520 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001521 cc_library {
1522 name: "libvndk",
1523 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001524 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001525 vndk: {
1526 enabled: true,
1527 },
1528 nocrt: true,
1529 }
1530
1531 cc_library {
1532 name: "libvndk_ext_product",
1533 product_specific: true,
1534 vndk: {
1535 enabled: true,
1536 extends: "libvndk",
1537 },
1538 nocrt: true,
1539 }
1540 `)
1541
1542 // Ensures that the core variant of "libvndk_ext_product" can be found.
1543 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1544 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1545 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1546 }
1547}
1548
Logan Chienf3511742017-10-31 18:04:35 +08001549func TestVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001550 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001551 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001552 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001553 cc_library {
1554 name: "libvndk",
1555 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001556 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001557 vndk: {
1558 enabled: true,
1559 },
1560 nocrt: true,
1561 }
1562
1563 cc_library {
1564 name: "libvndk_ext",
1565 vndk: {
1566 enabled: true,
1567 extends: "libvndk",
1568 },
1569 nocrt: true,
1570 }
1571 `)
1572
1573 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1574 cc_library {
1575 name: "libvndk",
1576 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001577 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001578 vndk: {
1579 enabled: true,
1580 },
1581 nocrt: true,
1582 }
1583
1584 cc_library {
1585 name: "libvndk_ext",
1586 vendor: true,
1587 vndk: {
1588 enabled: true,
1589 },
1590 nocrt: true,
1591 }
1592 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001593
1594 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1595 cc_library {
1596 name: "libvndk",
1597 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001598 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001599 vndk: {
1600 enabled: true,
1601 },
1602 nocrt: true,
1603 }
1604
1605 cc_library {
1606 name: "libvndk_ext_product",
1607 product_specific: true,
1608 vndk: {
1609 enabled: true,
1610 },
1611 nocrt: true,
1612 }
1613 `)
1614
1615 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1616 cc_library {
1617 name: "libvndk",
1618 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001619 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001620 vndk: {
1621 enabled: true,
1622 },
1623 nocrt: true,
1624 }
1625
1626 cc_library {
1627 name: "libvndk_ext_product",
1628 product_specific: true,
1629 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001630 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001631 vndk: {
1632 enabled: true,
1633 extends: "libvndk",
1634 },
1635 nocrt: true,
1636 }
1637 `)
Logan Chienf3511742017-10-31 18:04:35 +08001638}
1639
1640func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001641 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001642 // This test ensures an error is emitted for inconsistent support_system_process.
1643 testCcError(t, "module \".*\" with mismatched support_system_process", `
1644 cc_library {
1645 name: "libvndk",
1646 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001647 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001648 vndk: {
1649 enabled: true,
1650 },
1651 nocrt: true,
1652 }
1653
1654 cc_library {
1655 name: "libvndk_sp_ext",
1656 vendor: true,
1657 vndk: {
1658 enabled: true,
1659 extends: "libvndk",
1660 support_system_process: true,
1661 },
1662 nocrt: true,
1663 }
1664 `)
1665
1666 testCcError(t, "module \".*\" with mismatched support_system_process", `
1667 cc_library {
1668 name: "libvndk_sp",
1669 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001670 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001671 vndk: {
1672 enabled: true,
1673 support_system_process: true,
1674 },
1675 nocrt: true,
1676 }
1677
1678 cc_library {
1679 name: "libvndk_ext",
1680 vendor: true,
1681 vndk: {
1682 enabled: true,
1683 extends: "libvndk_sp",
1684 },
1685 nocrt: true,
1686 }
1687 `)
1688}
1689
1690func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001691 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001692 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001693 // with `private: true`.
1694 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001695 cc_library {
1696 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001697 vendor_available: true,
1698 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001699 vndk: {
1700 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001701 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001702 },
1703 nocrt: true,
1704 }
1705
1706 cc_library {
1707 name: "libvndk_ext",
1708 vendor: true,
1709 vndk: {
1710 enabled: true,
1711 extends: "libvndk",
1712 },
1713 nocrt: true,
1714 }
1715 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001716
Justin Yunfd9e8042020-12-23 18:23:14 +09001717 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001718 cc_library {
1719 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001720 vendor_available: true,
1721 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001722 vndk: {
1723 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001724 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001725 },
1726 nocrt: true,
1727 }
1728
1729 cc_library {
1730 name: "libvndk_ext_product",
1731 product_specific: true,
1732 vndk: {
1733 enabled: true,
1734 extends: "libvndk",
1735 },
1736 nocrt: true,
1737 }
1738 `)
Logan Chienf3511742017-10-31 18:04:35 +08001739}
1740
Logan Chiend3c59a22018-03-29 14:08:15 +08001741func TestVendorModuleUseVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001742 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001743 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001744 testCc(t, `
1745 cc_library {
1746 name: "libvndk",
1747 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001748 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001749 vndk: {
1750 enabled: true,
1751 },
1752 nocrt: true,
1753 }
1754
1755 cc_library {
1756 name: "libvndk_ext",
1757 vendor: true,
1758 vndk: {
1759 enabled: true,
1760 extends: "libvndk",
1761 },
1762 nocrt: true,
1763 }
1764
1765 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001766 name: "libvndk_sp",
1767 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001768 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001769 vndk: {
1770 enabled: true,
1771 support_system_process: true,
1772 },
1773 nocrt: true,
1774 }
1775
1776 cc_library {
1777 name: "libvndk_sp_ext",
1778 vendor: true,
1779 vndk: {
1780 enabled: true,
1781 extends: "libvndk_sp",
1782 support_system_process: true,
1783 },
1784 nocrt: true,
1785 }
1786
1787 cc_library {
1788 name: "libvendor",
1789 vendor: true,
1790 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1791 nocrt: true,
1792 }
1793 `)
1794}
1795
Logan Chiend3c59a22018-03-29 14:08:15 +08001796func TestVndkExtUseVendorLib(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001797 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001798 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001799 testCc(t, `
1800 cc_library {
1801 name: "libvndk",
1802 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001803 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001804 vndk: {
1805 enabled: true,
1806 },
1807 nocrt: true,
1808 }
1809
1810 cc_library {
1811 name: "libvndk_ext",
1812 vendor: true,
1813 vndk: {
1814 enabled: true,
1815 extends: "libvndk",
1816 },
1817 shared_libs: ["libvendor"],
1818 nocrt: true,
1819 }
1820
1821 cc_library {
1822 name: "libvendor",
1823 vendor: true,
1824 nocrt: true,
1825 }
1826 `)
Logan Chienf3511742017-10-31 18:04:35 +08001827
Logan Chiend3c59a22018-03-29 14:08:15 +08001828 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1829 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001830 cc_library {
1831 name: "libvndk_sp",
1832 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001833 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001834 vndk: {
1835 enabled: true,
1836 support_system_process: true,
1837 },
1838 nocrt: true,
1839 }
1840
1841 cc_library {
1842 name: "libvndk_sp_ext",
1843 vendor: true,
1844 vndk: {
1845 enabled: true,
1846 extends: "libvndk_sp",
1847 support_system_process: true,
1848 },
1849 shared_libs: ["libvendor"], // Cause an error
1850 nocrt: true,
1851 }
1852
1853 cc_library {
1854 name: "libvendor",
1855 vendor: true,
1856 nocrt: true,
1857 }
1858 `)
1859}
1860
Justin Yun0ecf0b22020-02-28 15:07:59 +09001861func TestProductVndkExtDependency(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001862 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001863 bp := `
1864 cc_library {
1865 name: "libvndk",
1866 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001867 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001868 vndk: {
1869 enabled: true,
1870 },
1871 nocrt: true,
1872 }
1873
1874 cc_library {
1875 name: "libvndk_ext_product",
1876 product_specific: true,
1877 vndk: {
1878 enabled: true,
1879 extends: "libvndk",
1880 },
1881 shared_libs: ["libproduct_for_vndklibs"],
1882 nocrt: true,
1883 }
1884
1885 cc_library {
1886 name: "libvndk_sp",
1887 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001888 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001889 vndk: {
1890 enabled: true,
1891 support_system_process: true,
1892 },
1893 nocrt: true,
1894 }
1895
1896 cc_library {
1897 name: "libvndk_sp_ext_product",
1898 product_specific: true,
1899 vndk: {
1900 enabled: true,
1901 extends: "libvndk_sp",
1902 support_system_process: true,
1903 },
1904 shared_libs: ["libproduct_for_vndklibs"],
1905 nocrt: true,
1906 }
1907
1908 cc_library {
1909 name: "libproduct",
1910 product_specific: true,
1911 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1912 nocrt: true,
1913 }
1914
1915 cc_library {
1916 name: "libproduct_for_vndklibs",
1917 product_specific: true,
1918 nocrt: true,
1919 }
1920 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001921 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001922 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1923 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001924 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001925
1926 testCcWithConfig(t, config)
1927}
1928
Logan Chiend3c59a22018-03-29 14:08:15 +08001929func TestVndkSpExtUseVndkError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001930 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001931 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1932 // library.
1933 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1934 cc_library {
1935 name: "libvndk",
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 },
1941 nocrt: true,
1942 }
1943
1944 cc_library {
1945 name: "libvndk_sp",
1946 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001947 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001948 vndk: {
1949 enabled: true,
1950 support_system_process: true,
1951 },
1952 nocrt: true,
1953 }
1954
1955 cc_library {
1956 name: "libvndk_sp_ext",
1957 vendor: true,
1958 vndk: {
1959 enabled: true,
1960 extends: "libvndk_sp",
1961 support_system_process: true,
1962 },
1963 shared_libs: ["libvndk"], // Cause an error
1964 nocrt: true,
1965 }
1966 `)
1967
1968 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1969 // library.
1970 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1971 cc_library {
1972 name: "libvndk",
1973 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001974 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001975 vndk: {
1976 enabled: true,
1977 },
1978 nocrt: true,
1979 }
1980
1981 cc_library {
1982 name: "libvndk_ext",
1983 vendor: true,
1984 vndk: {
1985 enabled: true,
1986 extends: "libvndk",
1987 },
1988 nocrt: true,
1989 }
1990
1991 cc_library {
1992 name: "libvndk_sp",
1993 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001994 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001995 vndk: {
1996 enabled: true,
1997 support_system_process: true,
1998 },
1999 nocrt: true,
2000 }
2001
2002 cc_library {
2003 name: "libvndk_sp_ext",
2004 vendor: true,
2005 vndk: {
2006 enabled: true,
2007 extends: "libvndk_sp",
2008 support_system_process: true,
2009 },
2010 shared_libs: ["libvndk_ext"], // Cause an error
2011 nocrt: true,
2012 }
2013 `)
2014}
2015
2016func TestVndkUseVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002017 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08002018 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2019 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002020 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2021 cc_library {
2022 name: "libvndk",
2023 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002024 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002025 vndk: {
2026 enabled: true,
2027 },
2028 nocrt: true,
2029 }
2030
2031 cc_library {
2032 name: "libvndk_ext",
2033 vendor: true,
2034 vndk: {
2035 enabled: true,
2036 extends: "libvndk",
2037 },
2038 nocrt: true,
2039 }
2040
2041 cc_library {
2042 name: "libvndk2",
2043 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002044 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002045 vndk: {
2046 enabled: true,
2047 },
2048 shared_libs: ["libvndk_ext"],
2049 nocrt: true,
2050 }
2051 `)
2052
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002053 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002054 cc_library {
2055 name: "libvndk",
2056 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002057 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002058 vndk: {
2059 enabled: true,
2060 },
2061 nocrt: true,
2062 }
2063
2064 cc_library {
2065 name: "libvndk_ext",
2066 vendor: true,
2067 vndk: {
2068 enabled: true,
2069 extends: "libvndk",
2070 },
2071 nocrt: true,
2072 }
2073
2074 cc_library {
2075 name: "libvndk2",
2076 vendor_available: true,
2077 vndk: {
2078 enabled: true,
2079 },
2080 target: {
2081 vendor: {
2082 shared_libs: ["libvndk_ext"],
2083 },
2084 },
2085 nocrt: true,
2086 }
2087 `)
2088
2089 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2090 cc_library {
2091 name: "libvndk_sp",
2092 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002093 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002094 vndk: {
2095 enabled: true,
2096 support_system_process: true,
2097 },
2098 nocrt: true,
2099 }
2100
2101 cc_library {
2102 name: "libvndk_sp_ext",
2103 vendor: true,
2104 vndk: {
2105 enabled: true,
2106 extends: "libvndk_sp",
2107 support_system_process: true,
2108 },
2109 nocrt: true,
2110 }
2111
2112 cc_library {
2113 name: "libvndk_sp_2",
2114 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002115 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002116 vndk: {
2117 enabled: true,
2118 support_system_process: true,
2119 },
2120 shared_libs: ["libvndk_sp_ext"],
2121 nocrt: true,
2122 }
2123 `)
2124
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002125 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002126 cc_library {
2127 name: "libvndk_sp",
2128 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002129 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002130 vndk: {
2131 enabled: true,
2132 },
2133 nocrt: true,
2134 }
2135
2136 cc_library {
2137 name: "libvndk_sp_ext",
2138 vendor: true,
2139 vndk: {
2140 enabled: true,
2141 extends: "libvndk_sp",
2142 },
2143 nocrt: true,
2144 }
2145
2146 cc_library {
2147 name: "libvndk_sp2",
2148 vendor_available: true,
2149 vndk: {
2150 enabled: true,
2151 },
2152 target: {
2153 vendor: {
2154 shared_libs: ["libvndk_sp_ext"],
2155 },
2156 },
2157 nocrt: true,
2158 }
2159 `)
2160}
2161
Justin Yun5f7f7e82019-11-18 19:52:14 +09002162func TestEnforceProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002163 t.Parallel()
Justin Yun5f7f7e82019-11-18 19:52:14 +09002164 bp := `
2165 cc_library {
2166 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002167 llndk: {
2168 symbol_file: "libllndk.map.txt",
2169 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002170 }
2171 cc_library {
2172 name: "libvndk",
2173 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002174 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002175 vndk: {
2176 enabled: true,
2177 },
2178 nocrt: true,
2179 }
2180 cc_library {
2181 name: "libvndk_sp",
2182 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002183 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002184 vndk: {
2185 enabled: true,
2186 support_system_process: true,
2187 },
2188 nocrt: true,
2189 }
2190 cc_library {
2191 name: "libva",
2192 vendor_available: true,
2193 nocrt: true,
2194 }
2195 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002196 name: "libpa",
2197 product_available: true,
2198 nocrt: true,
2199 }
2200 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002201 name: "libboth_available",
2202 vendor_available: true,
2203 product_available: true,
2204 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002205 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002206 target: {
2207 vendor: {
2208 suffix: "-vendor",
2209 },
2210 product: {
2211 suffix: "-product",
2212 },
2213 }
2214 }
2215 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002216 name: "libproduct_va",
2217 product_specific: true,
2218 vendor_available: true,
2219 nocrt: true,
2220 }
2221 cc_library {
2222 name: "libprod",
2223 product_specific: true,
2224 shared_libs: [
2225 "libllndk",
2226 "libvndk",
2227 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002228 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002229 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002230 "libproduct_va",
2231 ],
2232 nocrt: true,
2233 }
2234 cc_library {
2235 name: "libvendor",
2236 vendor: true,
2237 shared_libs: [
2238 "libllndk",
2239 "libvndk",
2240 "libvndk_sp",
2241 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002242 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002243 "libproduct_va",
2244 ],
2245 nocrt: true,
2246 }
2247 `
2248
Paul Duffin8567f222021-03-23 00:02:06 +00002249 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002250
Jooyung Han261e1582020-10-20 18:54:21 +09002251 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2252 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002253
2254 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2255 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2256
2257 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2258 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002259
2260 ensureStringContains := func(t *testing.T, str string, substr string) {
2261 t.Helper()
2262 if !strings.Contains(str, substr) {
2263 t.Errorf("%q is not found in %v", substr, str)
2264 }
2265 }
2266 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2267 t.Helper()
2268 if strings.Contains(str, substr) {
2269 t.Errorf("%q is found in %v", substr, str)
2270 }
2271 }
2272
2273 // _static variant is used since _shared reuses *.o from the static variant
2274 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2275 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2276
2277 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2278 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2279 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2280 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2281
2282 product_cflags := product_static.Rule("cc").Args["cFlags"]
2283 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2284 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2285 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002286}
2287
2288func TestEnforceProductVndkVersionErrors(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002289 t.Parallel()
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002290 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002291 cc_library {
2292 name: "libprod",
2293 product_specific: true,
2294 shared_libs: [
2295 "libvendor",
2296 ],
2297 nocrt: true,
2298 }
2299 cc_library {
2300 name: "libvendor",
2301 vendor: true,
2302 nocrt: true,
2303 }
2304 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002305 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002306 cc_library {
2307 name: "libprod",
2308 product_specific: true,
2309 shared_libs: [
2310 "libsystem",
2311 ],
2312 nocrt: true,
2313 }
2314 cc_library {
2315 name: "libsystem",
2316 nocrt: true,
2317 }
2318 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002319 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002320 cc_library {
2321 name: "libprod",
2322 product_specific: true,
2323 shared_libs: [
2324 "libva",
2325 ],
2326 nocrt: true,
2327 }
2328 cc_library {
2329 name: "libva",
2330 vendor_available: true,
2331 nocrt: true,
2332 }
2333 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002334 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002335 cc_library {
2336 name: "libprod",
2337 product_specific: true,
2338 shared_libs: [
2339 "libvndk_private",
2340 ],
2341 nocrt: true,
2342 }
2343 cc_library {
2344 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002345 vendor_available: true,
2346 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002347 vndk: {
2348 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002349 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002350 },
2351 nocrt: true,
2352 }
2353 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002354 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002355 cc_library {
2356 name: "libprod",
2357 product_specific: true,
2358 shared_libs: [
2359 "libsystem_ext",
2360 ],
2361 nocrt: true,
2362 }
2363 cc_library {
2364 name: "libsystem_ext",
2365 system_ext_specific: true,
2366 nocrt: true,
2367 }
2368 `)
2369 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2370 cc_library {
2371 name: "libsystem",
2372 shared_libs: [
2373 "libproduct_va",
2374 ],
2375 nocrt: true,
2376 }
2377 cc_library {
2378 name: "libproduct_va",
2379 product_specific: true,
2380 vendor_available: true,
2381 nocrt: true,
2382 }
2383 `)
2384}
2385
Jooyung Han38002912019-05-16 04:01:54 +09002386func TestMakeLinkType(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002387 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -08002388 bp := `
2389 cc_library {
2390 name: "libvndk",
2391 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002392 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002393 vndk: {
2394 enabled: true,
2395 },
2396 }
2397 cc_library {
2398 name: "libvndksp",
2399 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002400 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002401 vndk: {
2402 enabled: true,
2403 support_system_process: true,
2404 },
2405 }
2406 cc_library {
2407 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002408 vendor_available: true,
2409 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002410 vndk: {
2411 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002412 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002413 },
2414 }
2415 cc_library {
2416 name: "libvendor",
2417 vendor: true,
2418 }
2419 cc_library {
2420 name: "libvndkext",
2421 vendor: true,
2422 vndk: {
2423 enabled: true,
2424 extends: "libvndk",
2425 },
2426 }
2427 vndk_prebuilt_shared {
2428 name: "prevndk",
2429 version: "27",
2430 target_arch: "arm",
2431 binder32bit: true,
2432 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002433 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002434 vndk: {
2435 enabled: true,
2436 },
2437 arch: {
2438 arm: {
2439 srcs: ["liba.so"],
2440 },
2441 },
2442 }
2443 cc_library {
2444 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002445 llndk: {
2446 symbol_file: "libllndk.map.txt",
2447 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002448 }
2449 cc_library {
2450 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002451 llndk: {
2452 symbol_file: "libllndkprivate.map.txt",
2453 private: true,
2454 }
Colin Cross78212242021-01-06 14:51:30 -08002455 }
2456
2457 llndk_libraries_txt {
2458 name: "llndk.libraries.txt",
2459 }
2460 vndkcore_libraries_txt {
2461 name: "vndkcore.libraries.txt",
2462 }
2463 vndksp_libraries_txt {
2464 name: "vndksp.libraries.txt",
2465 }
2466 vndkprivate_libraries_txt {
2467 name: "vndkprivate.libraries.txt",
2468 }
2469 vndkcorevariant_libraries_txt {
2470 name: "vndkcorevariant.libraries.txt",
2471 insert_vndk_version: false,
2472 }
2473 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002474
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002475 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002476 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002477 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002478 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002479 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002480
Colin Cross78212242021-01-06 14:51:30 -08002481 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2482 []string{"libvndk.so", "libvndkprivate.so"})
2483 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2484 []string{"libc++.so", "libvndksp.so"})
2485 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2486 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2487 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2488 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002489
Colin Crossfb0c16e2019-11-20 17:12:35 -08002490 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002491
Jooyung Han38002912019-05-16 04:01:54 +09002492 tests := []struct {
2493 variant string
2494 name string
2495 expected string
2496 }{
2497 {vendorVariant, "libvndk", "native:vndk"},
2498 {vendorVariant, "libvndksp", "native:vndk"},
2499 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2500 {vendorVariant, "libvendor", "native:vendor"},
2501 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002502 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002503 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002504 {coreVariant, "libvndk", "native:platform"},
2505 {coreVariant, "libvndkprivate", "native:platform"},
2506 {coreVariant, "libllndk", "native:platform"},
2507 }
2508 for _, test := range tests {
2509 t.Run(test.name, func(t *testing.T) {
2510 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2511 assertString(t, module.makeLinkType, test.expected)
2512 })
2513 }
2514}
2515
Jeff Gaston294356f2017-09-27 17:05:30 -07002516var staticLinkDepOrderTestCases = []struct {
2517 // This is a string representation of a map[moduleName][]moduleDependency .
2518 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002519 inStatic string
2520
2521 // This is a string representation of a map[moduleName][]moduleDependency .
2522 // It models the dependencies declared in an Android.bp file.
2523 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002524
2525 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2526 // The keys of allOrdered specify which modules we would like to check.
2527 // The values of allOrdered specify the expected result (of the transitive closure of all
2528 // dependencies) for each module to test
2529 allOrdered string
2530
2531 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2532 // The keys of outOrdered specify which modules we would like to check.
2533 // The values of outOrdered specify the expected result (of the ordered linker command line)
2534 // for each module to test.
2535 outOrdered string
2536}{
2537 // Simple tests
2538 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002539 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002540 outOrdered: "",
2541 },
2542 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002543 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002544 outOrdered: "a:",
2545 },
2546 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002547 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002548 outOrdered: "a:b; b:",
2549 },
2550 // Tests of reordering
2551 {
2552 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002553 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002554 outOrdered: "a:b,c,d; b:d; c:d; d:",
2555 },
2556 {
2557 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002558 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002559 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2560 },
2561 {
2562 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002563 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002564 outOrdered: "a:d,b,e,c; d:b; e:c",
2565 },
2566 {
2567 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002568 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002569 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2570 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2571 },
2572 {
2573 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002574 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 -07002575 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2576 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2577 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002578 // shared dependencies
2579 {
2580 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2581 // So, we don't actually have to check that a shared dependency of c will change the order
2582 // of a library that depends statically on b and on c. We only need to check that if c has
2583 // a shared dependency on b, that that shows up in allOrdered.
2584 inShared: "c:b",
2585 allOrdered: "c:b",
2586 outOrdered: "c:",
2587 },
2588 {
2589 // This test doesn't actually include any shared dependencies but it's a reminder of what
2590 // the second phase of the above test would look like
2591 inStatic: "a:b,c; c:b",
2592 allOrdered: "a:c,b; c:b",
2593 outOrdered: "a:c,b; c:b",
2594 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002595 // tiebreakers for when two modules specifying different orderings and there is no dependency
2596 // to dictate an order
2597 {
2598 // 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 -08002599 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002600 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2601 },
2602 {
2603 // 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 -08002604 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 -07002605 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2606 },
2607 // Tests involving duplicate dependencies
2608 {
2609 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002610 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002611 outOrdered: "a:c,b",
2612 },
2613 {
2614 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002615 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002616 outOrdered: "a:d,c,b",
2617 },
2618 // Tests to confirm the nonexistence of infinite loops.
2619 // These cases should never happen, so as long as the test terminates and the
2620 // result is deterministic then that should be fine.
2621 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002622 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002623 outOrdered: "a:a",
2624 },
2625 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002626 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002627 allOrdered: "a:b,c; b:c,a; c:a,b",
2628 outOrdered: "a:b; b:c; c:a",
2629 },
2630 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002631 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002632 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2633 outOrdered: "a:c,b; b:a,c; c:b,a",
2634 },
2635}
2636
2637// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2638func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2639 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2640 strippedText := strings.Replace(text, " ", "", -1)
2641 if len(strippedText) < 1 {
2642 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2643 }
2644 allDeps = make(map[android.Path][]android.Path, 0)
2645
2646 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2647 moduleTexts := strings.Split(strippedText, ";")
2648
2649 outputForModuleName := func(moduleName string) android.Path {
2650 return android.PathForTesting(moduleName)
2651 }
2652
2653 for _, moduleText := range moduleTexts {
2654 // convert from "a:b,c" to ["a", "b,c"]
2655 components := strings.Split(moduleText, ":")
2656 if len(components) != 2 {
2657 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2658 }
2659 moduleName := components[0]
2660 moduleOutput := outputForModuleName(moduleName)
2661 modulesInOrder = append(modulesInOrder, moduleOutput)
2662
2663 depString := components[1]
2664 // convert from "b,c" to ["b", "c"]
2665 depNames := strings.Split(depString, ",")
2666 if len(depString) < 1 {
2667 depNames = []string{}
2668 }
2669 var deps []android.Path
2670 for _, depName := range depNames {
2671 deps = append(deps, outputForModuleName(depName))
2672 }
2673 allDeps[moduleOutput] = deps
2674 }
2675 return modulesInOrder, allDeps
2676}
2677
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002678func TestStaticLibDepReordering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002679 t.Parallel()
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()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002706 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) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002719 t.Parallel()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002720 ctx := testCc(t, `
2721 cc_library {
2722 name: "a",
2723 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002724 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002725 }
2726 cc_library {
2727 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002728 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002729 }
2730 cc_library {
2731 name: "c",
2732 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002733 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002734 }
2735
2736 `)
2737
Colin Cross7113d202019-11-20 16:39:12 -08002738 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002739 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002740 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2741 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002742 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002743
2744 if !reflect.DeepEqual(actual, expected) {
2745 t.Errorf("staticDeps orderings did not account for shared libs"+
2746 "\nactual: %v"+
2747 "\nexpected: %v",
2748 actual,
2749 expected,
2750 )
2751 }
2752}
2753
Jooyung Hanb04a4992020-03-13 18:57:35 +09002754func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002755 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002756 if !reflect.DeepEqual(actual, expected) {
2757 t.Errorf(message+
2758 "\nactual: %v"+
2759 "\nexpected: %v",
2760 actual,
2761 expected,
2762 )
2763 }
2764}
2765
Jooyung Han61b66e92020-03-21 14:21:46 +00002766func TestLlndkLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002767 t.Parallel()
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002768 result := prepareForCcTest.RunTestWithBp(t, `
2769 cc_library {
2770 name: "libllndk",
2771 stubs: { versions: ["1", "2"] },
2772 llndk: {
2773 symbol_file: "libllndk.map.txt",
2774 },
2775 export_include_dirs: ["include"],
2776 }
2777
2778 cc_prebuilt_library_shared {
2779 name: "libllndkprebuilt",
2780 stubs: { versions: ["1", "2"] },
2781 llndk: {
2782 symbol_file: "libllndkprebuilt.map.txt",
2783 },
2784 }
2785
2786 cc_library {
2787 name: "libllndk_with_external_headers",
2788 stubs: { versions: ["1", "2"] },
2789 llndk: {
2790 symbol_file: "libllndk.map.txt",
2791 export_llndk_headers: ["libexternal_llndk_headers"],
2792 },
2793 header_libs: ["libexternal_headers"],
2794 export_header_lib_headers: ["libexternal_headers"],
2795 }
2796 cc_library_headers {
2797 name: "libexternal_headers",
2798 export_include_dirs: ["include"],
2799 vendor_available: true,
2800 }
2801 cc_library_headers {
2802 name: "libexternal_llndk_headers",
2803 export_include_dirs: ["include_llndk"],
2804 llndk: {
2805 symbol_file: "libllndk.map.txt",
2806 },
2807 vendor_available: true,
2808 }
2809
2810 cc_library {
2811 name: "libllndk_with_override_headers",
2812 stubs: { versions: ["1", "2"] },
2813 llndk: {
2814 symbol_file: "libllndk.map.txt",
2815 override_export_include_dirs: ["include_llndk"],
2816 },
2817 export_include_dirs: ["include"],
2818 }
2819 `)
2820 actual := result.ModuleVariantsForTests("libllndk")
2821 for i := 0; i < len(actual); i++ {
2822 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2823 actual = append(actual[:i], actual[i+1:]...)
2824 i--
2825 }
2826 }
2827 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002828 "android_vendor.29_arm64_armv8-a_shared_current",
2829 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002830 "android_vendor.29_arm_armv7-a-neon_shared_current",
2831 "android_vendor.29_arm_armv7-a-neon_shared",
2832 }
2833 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2834
2835 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2836 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2837
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002838 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2839 t.Helper()
2840 m := result.ModuleForTests(module, variant).Module()
2841 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2842 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2843 expectedDirs, f.IncludeDirs)
2844 }
2845
2846 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2847 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2848 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2849 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2850 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2851 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2852}
2853
Jiyong Parka46a4d52017-12-14 19:54:34 +09002854func TestLlndkHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002855 t.Parallel()
Jiyong Parka46a4d52017-12-14 19:54:34 +09002856 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002857 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002858 name: "libllndk_headers",
2859 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002860 llndk: {
2861 llndk_headers: true,
2862 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002863 }
2864 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002865 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002866 llndk: {
2867 symbol_file: "libllndk.map.txt",
2868 export_llndk_headers: ["libllndk_headers"],
2869 }
Colin Cross0477b422020-10-13 18:43:54 -07002870 }
2871
2872 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002873 name: "libvendor",
2874 shared_libs: ["libllndk"],
2875 vendor: true,
2876 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002877 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002878 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002879 }
2880 `)
2881
2882 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002883 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002884 cflags := cc.Args["cFlags"]
2885 if !strings.Contains(cflags, "-Imy_include") {
2886 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2887 }
2888}
2889
Logan Chien43d34c32017-12-20 01:17:32 +08002890func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2891 actual := module.Properties.AndroidMkRuntimeLibs
2892 if !reflect.DeepEqual(actual, expected) {
2893 t.Errorf("incorrect runtime_libs for shared libs"+
2894 "\nactual: %v"+
2895 "\nexpected: %v",
2896 actual,
2897 expected,
2898 )
2899 }
2900}
2901
2902const runtimeLibAndroidBp = `
2903 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002904 name: "liball_available",
2905 vendor_available: true,
2906 product_available: true,
2907 no_libcrt : true,
2908 nocrt : true,
2909 system_shared_libs : [],
2910 }
2911 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002912 name: "libvendor_available1",
2913 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002914 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002915 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002916 nocrt : true,
2917 system_shared_libs : [],
2918 }
2919 cc_library {
2920 name: "libvendor_available2",
2921 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002922 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002923 target: {
2924 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002925 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002926 }
2927 },
Yi Konge7fe9912019-06-02 00:53:50 -07002928 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002929 nocrt : true,
2930 system_shared_libs : [],
2931 }
2932 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002933 name: "libproduct_vendor",
2934 product_specific: true,
2935 vendor_available: true,
2936 no_libcrt : true,
2937 nocrt : true,
2938 system_shared_libs : [],
2939 }
2940 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002941 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002942 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002943 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002944 nocrt : true,
2945 system_shared_libs : [],
2946 }
2947 cc_library {
2948 name: "libvendor1",
2949 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002950 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002951 nocrt : true,
2952 system_shared_libs : [],
2953 }
2954 cc_library {
2955 name: "libvendor2",
2956 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002957 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002958 no_libcrt : true,
2959 nocrt : true,
2960 system_shared_libs : [],
2961 }
2962 cc_library {
2963 name: "libproduct_available1",
2964 product_available: true,
2965 runtime_libs: ["liball_available"],
2966 no_libcrt : true,
2967 nocrt : true,
2968 system_shared_libs : [],
2969 }
2970 cc_library {
2971 name: "libproduct1",
2972 product_specific: true,
2973 no_libcrt : true,
2974 nocrt : true,
2975 system_shared_libs : [],
2976 }
2977 cc_library {
2978 name: "libproduct2",
2979 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002980 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002981 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002982 nocrt : true,
2983 system_shared_libs : [],
2984 }
2985`
2986
2987func TestRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002988 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002989 ctx := testCc(t, runtimeLibAndroidBp)
2990
2991 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002992 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002993
Justin Yun8a2600c2020-12-07 12:44:03 +09002994 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2995 checkRuntimeLibs(t, []string{"liball_available"}, module)
2996
2997 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2998 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002999
3000 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003001 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003002
3003 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3004 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003005 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003006
Justin Yun8a2600c2020-12-07 12:44:03 +09003007 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3008 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003009
3010 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003011 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003012
3013 // runtime_libs for product variants have '.product' suffixes if the modules have both core
3014 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003015 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003016
3017 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3018 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
3019
3020 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09003021 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003022}
3023
3024func TestExcludeRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003025 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08003026 ctx := testCc(t, runtimeLibAndroidBp)
3027
Colin Cross7113d202019-11-20 16:39:12 -08003028 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003029 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3030 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003031
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003032 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003033 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08003034 checkRuntimeLibs(t, nil, module)
3035}
3036
3037func TestRuntimeLibsNoVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003038 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08003039 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3040
3041 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3042
Colin Cross7113d202019-11-20 16:39:12 -08003043 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003044
Justin Yun8a2600c2020-12-07 12:44:03 +09003045 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3046 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003047
3048 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003049 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003050
3051 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003052 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003053}
3054
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003055func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003056 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003057 actual := module.Properties.AndroidMkStaticLibs
3058 if !reflect.DeepEqual(actual, expected) {
3059 t.Errorf("incorrect static_libs"+
3060 "\nactual: %v"+
3061 "\nexpected: %v",
3062 actual,
3063 expected,
3064 )
3065 }
3066}
3067
3068const staticLibAndroidBp = `
3069 cc_library {
3070 name: "lib1",
3071 }
3072 cc_library {
3073 name: "lib2",
3074 static_libs: ["lib1"],
3075 }
3076`
3077
3078func TestStaticLibDepExport(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003079 t.Parallel()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003080 ctx := testCc(t, staticLibAndroidBp)
3081
3082 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003083 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003084 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08003085 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003086
3087 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003088 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003089 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3090 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08003091 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003092}
3093
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003094func TestLibDepAndroidMkExportInMixedBuilds(t *testing.T) {
3095 bp := `
3096 cc_library {
3097 name: "static_dep",
3098 }
3099 cc_library {
3100 name: "whole_static_dep",
3101 }
3102 cc_library {
3103 name: "shared_dep",
3104 }
3105 cc_library {
3106 name: "lib",
3107 bazel_module: { label: "//:lib" },
3108 static_libs: ["static_dep"],
3109 whole_static_libs: ["whole_static_dep"],
3110 shared_libs: ["shared_dep"],
3111 }
3112 cc_test {
3113 name: "test",
3114 bazel_module: { label: "//:test" },
3115 static_libs: ["static_dep"],
3116 whole_static_libs: ["whole_static_dep"],
3117 shared_libs: ["shared_dep"],
3118 gtest: false,
Sam Delmericoef69d472023-04-18 17:32:43 -04003119 sanitize: {
3120 // cc_test modules default to memtag_heap: true,
3121 // but this adds extra dependencies that we don't care about
3122 never: true,
3123 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003124 }
3125 cc_binary {
3126 name: "binary",
3127 bazel_module: { label: "//:binary" },
3128 static_libs: ["static_dep"],
3129 whole_static_libs: ["whole_static_dep"],
3130 shared_libs: ["shared_dep"],
3131 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003132 cc_library_headers {
3133 name: "lib_headers",
3134 bazel_module: { label: "//:lib_headers" },
3135 static_libs: ["static_dep"],
3136 whole_static_libs: ["whole_static_dep"],
3137 shared_libs: ["shared_dep"],
3138 }
3139 cc_prebuilt_library {
3140 name: "lib_prebuilt",
3141 bazel_module: { label: "//:lib_prebuilt" },
3142 static_libs: ["static_dep"],
3143 whole_static_libs: ["whole_static_dep"],
3144 shared_libs: ["shared_dep"],
3145 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003146 `
3147
3148 testCases := []struct {
3149 name string
3150 moduleName string
3151 variant string
3152 androidMkInfo cquery.CcAndroidMkInfo
3153 }{
3154 {
3155 name: "shared lib",
3156 moduleName: "lib",
3157 variant: "android_arm64_armv8-a_shared",
3158 androidMkInfo: cquery.CcAndroidMkInfo{
3159 LocalStaticLibs: []string{"static_dep"},
3160 LocalWholeStaticLibs: []string{"whole_static_dep"},
3161 LocalSharedLibs: []string{"shared_dep"},
3162 },
3163 },
3164 {
3165 name: "static lib",
3166 moduleName: "lib",
3167 variant: "android_arm64_armv8-a_static",
3168 androidMkInfo: cquery.CcAndroidMkInfo{
3169 LocalStaticLibs: []string{"static_dep"},
3170 LocalWholeStaticLibs: []string{"whole_static_dep"},
3171 LocalSharedLibs: []string{"shared_dep"},
3172 },
3173 },
3174 {
3175 name: "cc_test arm64",
3176 moduleName: "test",
3177 variant: "android_arm64_armv8-a",
3178 androidMkInfo: cquery.CcAndroidMkInfo{
3179 LocalStaticLibs: []string{"static_dep"},
3180 LocalWholeStaticLibs: []string{"whole_static_dep"},
3181 LocalSharedLibs: []string{"shared_dep"},
3182 },
3183 },
3184 {
3185 name: "cc_test arm",
3186 moduleName: "test",
3187 variant: "android_arm_armv7-a-neon",
3188 androidMkInfo: cquery.CcAndroidMkInfo{
3189 LocalStaticLibs: []string{"static_dep"},
3190 LocalWholeStaticLibs: []string{"whole_static_dep"},
3191 LocalSharedLibs: []string{"shared_dep"},
3192 },
3193 },
3194 {
3195 name: "cc_binary",
3196 moduleName: "binary",
3197 variant: "android_arm64_armv8-a",
3198 androidMkInfo: cquery.CcAndroidMkInfo{
3199 LocalStaticLibs: []string{"static_dep"},
3200 LocalWholeStaticLibs: []string{"whole_static_dep"},
3201 LocalSharedLibs: []string{"shared_dep"},
3202 },
3203 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003204 {
3205 name: "cc_library_headers",
3206 moduleName: "lib_headers",
3207 variant: "android_arm64_armv8-a",
3208 androidMkInfo: cquery.CcAndroidMkInfo{
3209 LocalStaticLibs: []string{"static_dep"},
3210 LocalWholeStaticLibs: []string{"whole_static_dep"},
3211 LocalSharedLibs: []string{"shared_dep"},
3212 },
3213 },
3214 {
3215 name: "prebuilt lib static",
3216 moduleName: "lib_prebuilt",
3217 variant: "android_arm64_armv8-a_static",
3218 androidMkInfo: cquery.CcAndroidMkInfo{
3219 LocalStaticLibs: []string{"static_dep"},
3220 LocalWholeStaticLibs: []string{"whole_static_dep"},
3221 LocalSharedLibs: []string{"shared_dep"},
3222 },
3223 },
3224 {
3225 name: "prebuilt lib shared",
3226 moduleName: "lib_prebuilt",
3227 variant: "android_arm64_armv8-a_shared",
3228 androidMkInfo: cquery.CcAndroidMkInfo{
3229 LocalStaticLibs: []string{"static_dep"},
3230 LocalWholeStaticLibs: []string{"whole_static_dep"},
3231 LocalSharedLibs: []string{"shared_dep"},
3232 },
3233 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003234 }
3235
3236 outputBaseDir := "out/bazel"
3237 for _, tc := range testCases {
3238 t.Run(tc.name, func(t *testing.T) {
3239 result := android.GroupFixturePreparers(
3240 prepareForCcTest,
3241 android.FixtureModifyConfig(func(config android.Config) {
3242 config.BazelContext = android.MockBazelContext{
3243 OutputBaseDir: outputBaseDir,
3244 LabelToCcInfo: map[string]cquery.CcInfo{
3245 "//:lib": cquery.CcInfo{
3246 CcAndroidMkInfo: tc.androidMkInfo,
3247 RootDynamicLibraries: []string{""},
3248 },
3249 "//:lib_bp2build_cc_library_static": cquery.CcInfo{
3250 CcAndroidMkInfo: tc.androidMkInfo,
3251 RootStaticArchives: []string{""},
3252 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003253 "//:lib_headers": cquery.CcInfo{
3254 CcAndroidMkInfo: tc.androidMkInfo,
3255 OutputFiles: []string{""},
3256 },
3257 "//:lib_prebuilt": cquery.CcInfo{
3258 CcAndroidMkInfo: tc.androidMkInfo,
3259 },
3260 "//:lib_prebuilt_bp2build_cc_library_static": cquery.CcInfo{
3261 CcAndroidMkInfo: tc.androidMkInfo,
3262 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003263 },
3264 LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
3265 "//:test": cquery.CcUnstrippedInfo{
3266 CcAndroidMkInfo: tc.androidMkInfo,
3267 },
3268 "//:binary": cquery.CcUnstrippedInfo{
3269 CcAndroidMkInfo: tc.androidMkInfo,
3270 },
3271 },
3272 }
3273 }),
3274 ).RunTestWithBp(t, bp)
3275 ctx := result.TestContext
3276
3277 module := ctx.ModuleForTests(tc.moduleName, tc.variant).Module().(*Module)
3278 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003279 if !reflect.DeepEqual(module.Properties.AndroidMkStaticLibs, tc.androidMkInfo.LocalStaticLibs) {
3280 t.Errorf("incorrect static_libs"+
3281 "\nactual: %v"+
3282 "\nexpected: %v",
3283 module.Properties.AndroidMkStaticLibs,
3284 tc.androidMkInfo.LocalStaticLibs,
3285 )
3286 }
3287 staticDepsDiffer, missingStaticDeps, additionalStaticDeps := android.ListSetDifference(
3288 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3289 tc.androidMkInfo.LocalStaticLibs,
3290 )
3291 if staticDepsDiffer {
3292 t.Errorf(
3293 "expected LOCAL_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3294 tc.androidMkInfo.LocalStaticLibs,
3295 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3296 missingStaticDeps,
3297 additionalStaticDeps,
3298 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003299 }
3300
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003301 if !reflect.DeepEqual(module.Properties.AndroidMkWholeStaticLibs, tc.androidMkInfo.LocalWholeStaticLibs) {
3302 t.Errorf("expected module.Properties.AndroidMkWholeStaticLibs to be %q, but was %q",
3303 tc.androidMkInfo.LocalWholeStaticLibs,
3304 module.Properties.AndroidMkWholeStaticLibs,
3305 )
3306 }
3307 wholeStaticDepsDiffer, missingWholeStaticDeps, additionalWholeStaticDeps := android.ListSetDifference(
3308 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3309 tc.androidMkInfo.LocalWholeStaticLibs,
3310 )
3311 if wholeStaticDepsDiffer {
3312 t.Errorf(
3313 "expected LOCAL_WHOLE_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3314 tc.androidMkInfo.LocalWholeStaticLibs,
3315 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3316 missingWholeStaticDeps,
3317 additionalWholeStaticDeps,
3318 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003319 }
3320
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003321 if !reflect.DeepEqual(module.Properties.AndroidMkSharedLibs, tc.androidMkInfo.LocalSharedLibs) {
3322 t.Errorf("incorrect shared_libs"+
3323 "\nactual: %v"+
3324 "\nexpected: %v",
3325 module.Properties.AndroidMkSharedLibs,
3326 tc.androidMkInfo.LocalSharedLibs,
3327 )
3328 }
3329 sharedDepsDiffer, missingSharedDeps, additionalSharedDeps := android.ListSetDifference(
3330 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3331 tc.androidMkInfo.LocalSharedLibs,
3332 )
3333 if sharedDepsDiffer {
3334 t.Errorf(
3335 "expected LOCAL_SHARED_LIBRARIES to be %q but was %q; missing %q; extra %q",
3336 tc.androidMkInfo.LocalSharedLibs,
3337 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3338 missingSharedDeps,
3339 additionalSharedDeps,
3340 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003341 }
3342 })
3343 }
3344}
3345
Jiyong Parkd08b6972017-09-26 10:50:54 +09003346var compilerFlagsTestCases = []struct {
3347 in string
3348 out bool
3349}{
3350 {
3351 in: "a",
3352 out: false,
3353 },
3354 {
3355 in: "-a",
3356 out: true,
3357 },
3358 {
3359 in: "-Ipath/to/something",
3360 out: false,
3361 },
3362 {
3363 in: "-isystempath/to/something",
3364 out: false,
3365 },
3366 {
3367 in: "--coverage",
3368 out: false,
3369 },
3370 {
3371 in: "-include a/b",
3372 out: true,
3373 },
3374 {
3375 in: "-include a/b c/d",
3376 out: false,
3377 },
3378 {
3379 in: "-DMACRO",
3380 out: true,
3381 },
3382 {
3383 in: "-DMAC RO",
3384 out: false,
3385 },
3386 {
3387 in: "-a -b",
3388 out: false,
3389 },
3390 {
3391 in: "-DMACRO=definition",
3392 out: true,
3393 },
3394 {
3395 in: "-DMACRO=defi nition",
3396 out: true, // TODO(jiyong): this should be false
3397 },
3398 {
3399 in: "-DMACRO(x)=x + 1",
3400 out: true,
3401 },
3402 {
3403 in: "-DMACRO=\"defi nition\"",
3404 out: true,
3405 },
3406}
3407
3408type mockContext struct {
3409 BaseModuleContext
3410 result bool
3411}
3412
3413func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3414 // CheckBadCompilerFlags calls this function when the flag should be rejected
3415 ctx.result = false
3416}
3417
3418func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003419 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003420 for _, testCase := range compilerFlagsTestCases {
3421 ctx := &mockContext{result: true}
3422 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3423 if ctx.result != testCase.out {
3424 t.Errorf("incorrect output:")
3425 t.Errorf(" input: %#v", testCase.in)
3426 t.Errorf(" expected: %#v", testCase.out)
3427 t.Errorf(" got: %#v", ctx.result)
3428 }
3429 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003430}
Jiyong Park374510b2018-03-19 18:23:01 +09003431
Jiyong Park37b25202018-07-11 10:49:27 +09003432func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003433 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003434 ctx := testCc(t, `
3435 cc_library_shared {
3436 name: "librecovery",
3437 recovery: true,
3438 }
3439 cc_library_shared {
3440 name: "librecovery32",
3441 recovery: true,
3442 compile_multilib:"32",
3443 }
Jiyong Park5baac542018-08-28 09:55:37 +09003444 cc_library_shared {
3445 name: "libHalInRecovery",
3446 recovery_available: true,
3447 vendor: true,
3448 }
Jiyong Park37b25202018-07-11 10:49:27 +09003449 `)
3450
3451 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003452 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003453 if len(variants) != 1 || !android.InList(arm64, variants) {
3454 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3455 }
3456
3457 variants = ctx.ModuleVariantsForTests("librecovery32")
3458 if android.InList(arm64, variants) {
3459 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3460 }
Jiyong Park5baac542018-08-28 09:55:37 +09003461
3462 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3463 if !recoveryModule.Platform() {
3464 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3465 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003466}
Jiyong Park5baac542018-08-28 09:55:37 +09003467
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003468func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003469 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003470 bp := `
3471 cc_prebuilt_test_library_shared {
3472 name: "test_lib",
3473 relative_install_path: "foo/bar/baz",
3474 srcs: ["srcpath/dontusethispath/baz.so"],
3475 }
3476
3477 cc_test {
3478 name: "main_test",
3479 data_libs: ["test_lib"],
3480 gtest: false,
3481 }
3482 `
3483
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003484 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003485 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003486 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003487 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3488
3489 ctx := testCcWithConfig(t, config)
3490 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3491 testBinary := module.(*Module).linker.(*testBinary)
3492 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3493 if err != nil {
3494 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3495 }
3496 if len(outputFiles) != 1 {
3497 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3498 }
3499 if len(testBinary.dataPaths()) != 1 {
3500 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3501 }
3502
3503 outputPath := outputFiles[0].String()
3504
3505 if !strings.HasSuffix(outputPath, "/main_test") {
3506 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3507 }
Colin Crossaa255532020-07-03 13:18:24 -07003508 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003509 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3510 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3511 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3512 }
3513}
3514
Jiyong Park7ed9de32018-10-15 22:25:07 +09003515func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003516 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003517 ctx := testCc(t, `
3518 cc_library_shared {
3519 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003520 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003521 stubs: {
3522 symbol_file: "foo.map.txt",
3523 versions: ["1", "2", "3"],
3524 },
3525 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003526
Jiyong Park7ed9de32018-10-15 22:25:07 +09003527 cc_library_shared {
3528 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003529 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003530 shared_libs: ["libFoo#1"],
3531 }`)
3532
3533 variants := ctx.ModuleVariantsForTests("libFoo")
3534 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003535 "android_arm64_armv8-a_shared",
3536 "android_arm64_armv8-a_shared_1",
3537 "android_arm64_armv8-a_shared_2",
3538 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003539 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003540 "android_arm_armv7-a-neon_shared",
3541 "android_arm_armv7-a-neon_shared_1",
3542 "android_arm_armv7-a-neon_shared_2",
3543 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003544 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003545 }
3546 variantsMismatch := false
3547 if len(variants) != len(expectedVariants) {
3548 variantsMismatch = true
3549 } else {
3550 for _, v := range expectedVariants {
3551 if !inList(v, variants) {
3552 variantsMismatch = false
3553 }
3554 }
3555 }
3556 if variantsMismatch {
3557 t.Errorf("variants of libFoo expected:\n")
3558 for _, v := range expectedVariants {
3559 t.Errorf("%q\n", v)
3560 }
3561 t.Errorf(", but got:\n")
3562 for _, v := range variants {
3563 t.Errorf("%q\n", v)
3564 }
3565 }
3566
Colin Cross7113d202019-11-20 16:39:12 -08003567 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003568 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003569 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003570 if !strings.Contains(libFlags, libFoo1StubPath) {
3571 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3572 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003573
Colin Cross7113d202019-11-20 16:39:12 -08003574 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003575 cFlags := libBarCompileRule.Args["cFlags"]
3576 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3577 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3578 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3579 }
Jiyong Park37b25202018-07-11 10:49:27 +09003580}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003581
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003582func TestStubsForLibraryInMultipleApexes(t *testing.T) {
3583 t.Parallel()
3584 ctx := testCc(t, `
3585 cc_library_shared {
3586 name: "libFoo",
3587 srcs: ["foo.c"],
3588 stubs: {
3589 symbol_file: "foo.map.txt",
3590 versions: ["current"],
3591 },
3592 apex_available: ["bar", "a1"],
3593 }
3594
3595 cc_library_shared {
3596 name: "libBar",
3597 srcs: ["bar.c"],
3598 shared_libs: ["libFoo"],
3599 apex_available: ["a1"],
3600 }
3601
3602 cc_library_shared {
3603 name: "libA1",
3604 srcs: ["a1.c"],
3605 shared_libs: ["libFoo"],
3606 apex_available: ["a1"],
3607 }
3608
3609 cc_library_shared {
3610 name: "libBarA1",
3611 srcs: ["bara1.c"],
3612 shared_libs: ["libFoo"],
3613 apex_available: ["bar", "a1"],
3614 }
3615
3616 cc_library_shared {
3617 name: "libAnyApex",
3618 srcs: ["anyApex.c"],
3619 shared_libs: ["libFoo"],
3620 apex_available: ["//apex_available:anyapex"],
3621 }
3622
3623 cc_library_shared {
3624 name: "libBaz",
3625 srcs: ["baz.c"],
3626 shared_libs: ["libFoo"],
3627 apex_available: ["baz"],
3628 }
3629
3630 cc_library_shared {
3631 name: "libQux",
3632 srcs: ["qux.c"],
3633 shared_libs: ["libFoo"],
3634 apex_available: ["qux", "bar"],
3635 }`)
3636
3637 variants := ctx.ModuleVariantsForTests("libFoo")
3638 expectedVariants := []string{
3639 "android_arm64_armv8-a_shared",
3640 "android_arm64_armv8-a_shared_current",
3641 "android_arm_armv7-a-neon_shared",
3642 "android_arm_armv7-a-neon_shared_current",
3643 }
3644 variantsMismatch := false
3645 if len(variants) != len(expectedVariants) {
3646 variantsMismatch = true
3647 } else {
3648 for _, v := range expectedVariants {
3649 if !inList(v, variants) {
3650 variantsMismatch = false
3651 }
3652 }
3653 }
3654 if variantsMismatch {
3655 t.Errorf("variants of libFoo expected:\n")
3656 for _, v := range expectedVariants {
3657 t.Errorf("%q\n", v)
3658 }
3659 t.Errorf(", but got:\n")
3660 for _, v := range variants {
3661 t.Errorf("%q\n", v)
3662 }
3663 }
3664
3665 linkAgainstFoo := []string{"libBarA1"}
3666 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3667
3668 libFooPath := "libFoo/android_arm64_armv8-a_shared/libFoo.so"
3669 for _, lib := range linkAgainstFoo {
3670 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3671 libFlags := libLinkRule.Args["libFlags"]
3672 if !strings.Contains(libFlags, libFooPath) {
3673 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3674 }
3675 }
3676
3677 libFooStubPath := "libFoo/android_arm64_armv8-a_shared_current/libFoo.so"
3678 for _, lib := range linkAgainstFooStubs {
3679 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3680 libFlags := libLinkRule.Args["libFlags"]
3681 if !strings.Contains(libFlags, libFooStubPath) {
3682 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3683 }
3684 }
3685}
3686
Sam Delmerico75dbca22023-04-20 13:13:25 +00003687func TestMixedBuildUsesStubs(t *testing.T) {
Sam Delmerico75dbca22023-04-20 13:13:25 +00003688 t.Parallel()
3689 bp := `
3690 cc_library_shared {
3691 name: "libFoo",
3692 bazel_module: { label: "//:libFoo" },
3693 srcs: ["foo.c"],
3694 stubs: {
3695 symbol_file: "foo.map.txt",
3696 versions: ["current"],
3697 },
3698 apex_available: ["bar", "a1"],
3699 }
3700
3701 cc_library_shared {
3702 name: "libBar",
3703 srcs: ["bar.c"],
3704 shared_libs: ["libFoo"],
3705 apex_available: ["a1"],
3706 }
3707
3708 cc_library_shared {
3709 name: "libA1",
3710 srcs: ["a1.c"],
3711 shared_libs: ["libFoo"],
3712 apex_available: ["a1"],
3713 }
3714
3715 cc_library_shared {
3716 name: "libBarA1",
3717 srcs: ["bara1.c"],
3718 shared_libs: ["libFoo"],
3719 apex_available: ["bar", "a1"],
3720 }
3721
3722 cc_library_shared {
3723 name: "libAnyApex",
3724 srcs: ["anyApex.c"],
3725 shared_libs: ["libFoo"],
3726 apex_available: ["//apex_available:anyapex"],
3727 }
3728
3729 cc_library_shared {
3730 name: "libBaz",
3731 srcs: ["baz.c"],
3732 shared_libs: ["libFoo"],
3733 apex_available: ["baz"],
3734 }
3735
3736 cc_library_shared {
3737 name: "libQux",
3738 srcs: ["qux.c"],
3739 shared_libs: ["libFoo"],
3740 apex_available: ["qux", "bar"],
3741 }`
3742
3743 result := android.GroupFixturePreparers(
3744 prepareForCcTest,
3745 android.FixtureModifyConfig(func(config android.Config) {
3746 config.BazelContext = android.MockBazelContext{
3747 OutputBaseDir: "out/bazel",
3748 LabelToCcInfo: map[string]cquery.CcInfo{
3749 "//:libFoo": {
3750 RootDynamicLibraries: []string{"libFoo.so"},
3751 },
3752 "//:libFoo_stub_libs-current": {
3753 RootDynamicLibraries: []string{"libFoo_stub_libs-current.so"},
3754 },
3755 },
3756 }
3757 }),
3758 ).RunTestWithBp(t, bp)
3759 ctx := result.TestContext
3760
3761 variants := ctx.ModuleVariantsForTests("libFoo")
3762 expectedVariants := []string{
3763 "android_arm64_armv8-a_shared",
3764 "android_arm64_armv8-a_shared_current",
3765 "android_arm_armv7-a-neon_shared",
3766 "android_arm_armv7-a-neon_shared_current",
3767 }
3768 variantsMismatch := false
3769 if len(variants) != len(expectedVariants) {
3770 variantsMismatch = true
3771 } else {
3772 for _, v := range expectedVariants {
3773 if !inList(v, variants) {
3774 variantsMismatch = false
3775 }
3776 }
3777 }
3778 if variantsMismatch {
3779 t.Errorf("variants of libFoo expected:\n")
3780 for _, v := range expectedVariants {
3781 t.Errorf("%q\n", v)
3782 }
3783 t.Errorf(", but got:\n")
3784 for _, v := range variants {
3785 t.Errorf("%q\n", v)
3786 }
3787 }
3788
3789 linkAgainstFoo := []string{"libBarA1"}
3790 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3791
3792 libFooPath := "out/bazel/execroot/__main__/libFoo.so"
3793 for _, lib := range linkAgainstFoo {
3794 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3795 libFlags := libLinkRule.Args["libFlags"]
3796 if !strings.Contains(libFlags, libFooPath) {
3797 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3798 }
3799 }
3800
3801 libFooStubPath := "out/bazel/execroot/__main__/libFoo_stub_libs-current.so"
3802 for _, lib := range linkAgainstFooStubs {
3803 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3804 libFlags := libLinkRule.Args["libFlags"]
3805 if !strings.Contains(libFlags, libFooStubPath) {
3806 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3807 }
3808 }
3809}
3810
Jooyung Hanb04a4992020-03-13 18:57:35 +09003811func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003812 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003813 for _, tc := range []struct{ moduleName, expected string }{
3814 {"libc", "__LIBC_API__"},
3815 {"libfoo", "__LIBFOO_API__"},
3816 {"libfoo@1", "__LIBFOO_1_API__"},
3817 {"libfoo-v1", "__LIBFOO_V1_API__"},
3818 {"libfoo.v1", "__LIBFOO_V1_API__"},
3819 } {
3820 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3821 }
3822}
3823
Liz Kammer83cf81b2022-09-22 08:24:20 -04003824func pathsToBase(paths android.Paths) []string {
3825 var ret []string
3826 for _, p := range paths {
3827 ret = append(ret, p.Base())
3828 }
3829 return ret
3830}
3831
3832func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003833 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003834 ctx := testCc(t, `
3835 cc_library_static {
3836 name: "foo",
3837 srcs: ["foo.c"],
3838 }
3839
3840 cc_library_static {
3841 name: "bar",
3842 srcs: ["bar.c"],
3843 }
3844
3845 cc_library_shared {
3846 name: "qux",
3847 srcs: ["qux.c"],
3848 }
3849
3850 cc_library_static {
3851 name: "baz",
3852 srcs: ["baz.c"],
3853 static_libs: ["foo"],
3854 shared_libs: ["qux"],
3855 whole_static_libs: ["bar"],
3856 }`)
3857
3858 variant := "android_arm64_armv8-a_static"
3859 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3860
3861 // For static libraries, the object files of a whole static dep are included in the archive
3862 // directly
3863 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3864 t.Errorf("Expected input objects %q, got %q", w, g)
3865 }
3866
3867 // non whole static dependencies are not linked into the archive
3868 if len(arRule.Implicits) > 0 {
3869 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3870 }
3871}
3872
3873func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003874 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003875 ctx := testCc(t, `
3876 cc_library_static {
3877 name: "foo",
3878 srcs: ["foo.c"],
3879 }
3880
3881 cc_library_static {
3882 name: "bar",
3883 srcs: ["bar.c"],
3884 }
3885
3886 cc_library_shared {
3887 name: "qux",
3888 srcs: ["qux.c"],
3889 }
3890
3891 cc_library_shared {
3892 name: "baz",
3893 srcs: ["baz.c"],
3894 static_libs: ["foo"],
3895 shared_libs: ["qux"],
3896 whole_static_libs: ["bar"],
3897 }`)
3898
3899 variant := "android_arm64_armv8-a_shared"
3900 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3901 libFlags := linkRule.Args["libFlags"]
3902 // When dynamically linking, we expect static dependencies to be found on the command line
3903 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3904 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3905 }
3906 // When dynamically linking, we expect whole static dependencies to be found on the command line
3907 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3908 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3909 }
3910
3911 // When dynamically linking, we expect shared dependencies to be found on the command line
3912 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3913 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3914 }
3915
3916 // We should only have the objects from the shared library srcs, not the whole static dependencies
3917 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3918 t.Errorf("Expected input objects %q, got %q", w, g)
3919 }
3920}
3921
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003922func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003923 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003924 ctx := testCc(t, `
3925 cc_binary {
3926 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003927 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003928 static_executable: true,
3929 }`)
3930
Colin Cross7113d202019-11-20 16:39:12 -08003931 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003932 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3933 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003934 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003935 for _, lib := range systemStaticLibs {
3936 if !strings.Contains(libFlags, lib) {
3937 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3938 }
3939 }
3940 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3941 for _, lib := range systemSharedLibs {
3942 if strings.Contains(libFlags, lib) {
3943 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3944 }
3945 }
3946}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003947
3948func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003949 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003950 ctx := testCc(t, `
3951 cc_binary {
3952 name: "mybin",
3953 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003954 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003955 static_executable: true,
3956 stl: "none",
3957 }
3958
3959 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003960 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003961 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003962 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003963 stl: "none",
3964 }
3965
3966 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003967 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003968 srcs: ["foo.c"],
3969 stl: "none",
3970 stubs: {
3971 versions: ["1"],
3972 },
3973 }`)
3974
Colin Cross0de8a1e2020-09-18 14:15:30 -07003975 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3976 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003977 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003978
3979 if !reflect.DeepEqual(actual, expected) {
3980 t.Errorf("staticDeps orderings were not propagated correctly"+
3981 "\nactual: %v"+
3982 "\nexpected: %v",
3983 actual,
3984 expected,
3985 )
3986 }
3987}
Jooyung Han38002912019-05-16 04:01:54 +09003988
Jooyung Hand48f3c32019-08-23 11:18:57 +09003989func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003990 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003991 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3992 cc_library {
3993 name: "libA",
3994 srcs: ["foo.c"],
3995 shared_libs: ["libB"],
3996 stl: "none",
3997 }
3998
3999 cc_library {
4000 name: "libB",
4001 srcs: ["foo.c"],
4002 enabled: false,
4003 stl: "none",
4004 }
4005 `)
4006}
4007
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004008func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
4009 bp := `
4010 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00004011 name: "test_afl_fuzz_target",
4012 srcs: ["foo.c"],
4013 host_supported: true,
4014 static_libs: [
4015 "afl_fuzz_static_lib",
4016 ],
4017 shared_libs: [
4018 "afl_fuzz_shared_lib",
4019 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004020 fuzzing_frameworks: {
4021 afl: true,
4022 libfuzzer: false,
4023 },
Cory Barkera1da26f2022-06-07 20:12:06 +00004024 }
4025 cc_library {
4026 name: "afl_fuzz_static_lib",
4027 host_supported: true,
4028 srcs: ["static_file.c"],
4029 }
4030 cc_library {
4031 name: "libfuzzer_only_static_lib",
4032 host_supported: true,
4033 srcs: ["static_file.c"],
4034 }
4035 cc_library {
4036 name: "afl_fuzz_shared_lib",
4037 host_supported: true,
4038 srcs: ["shared_file.c"],
4039 static_libs: [
4040 "second_static_lib",
4041 ],
4042 }
4043 cc_library_headers {
4044 name: "libafl_headers",
4045 vendor_available: true,
4046 host_supported: true,
4047 export_include_dirs: [
4048 "include",
4049 "instrumentation",
4050 ],
4051 }
4052 cc_object {
4053 name: "afl-compiler-rt",
4054 vendor_available: true,
4055 host_supported: true,
4056 cflags: [
4057 "-fPIC",
4058 ],
4059 srcs: [
4060 "instrumentation/afl-compiler-rt.o.c",
4061 ],
4062 }
4063 cc_library {
4064 name: "second_static_lib",
4065 host_supported: true,
4066 srcs: ["second_file.c"],
4067 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004068 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00004069 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004070 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00004071 srcs: [
4072 "aflpp_driver.c",
4073 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004074 }`
4075
4076 testEnv := map[string]string{
4077 "FUZZ_FRAMEWORK": "AFL",
4078 }
4079
4080 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00004081
4082 checkPcGuardFlag := func(
4083 modName string, variantName string, shouldHave bool) {
4084 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
4085
4086 cFlags, ok := cc.Args["cFlags"]
4087 if !ok {
4088 t.Errorf("Could not find cFlags for module %s and variant %s",
4089 modName, variantName)
4090 }
4091
4092 if strings.Contains(
4093 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
4094 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
4095 "Test failed for module %s and variant %s",
4096 !shouldHave, shouldHave, modName, variantName)
4097 }
4098 }
4099
Cory Barkera1da26f2022-06-07 20:12:06 +00004100 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004101 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00004102
4103 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004104 checkPcGuardFlag(moduleName, variant+"_static", false)
4105 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00004106
4107 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004108 checkPcGuardFlag(moduleName, variant+"_static", false)
4109 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00004110
4111 ctx.ModuleForTests("afl_fuzz_shared_lib",
4112 "android_arm64_armv8-a_shared").Rule("cc")
4113 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004114 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
4115}
4116
4117func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004118 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004119 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
4120}
4121
4122func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004123 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004124 if runtime.GOOS != "linux" {
4125 t.Skip("requires linux")
4126 }
4127
4128 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00004129}
4130
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004131// Simple smoke test for the cc_fuzz target that ensures the rule compiles
4132// correctly.
4133func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004134 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004135 ctx := testCc(t, `
4136 cc_fuzz {
4137 name: "fuzz_smoke_test",
4138 srcs: ["foo.c"],
4139 }`)
4140
Paul Duffin075c4172019-12-19 19:06:13 +00004141 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004142 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
4143}
4144
Jooyung Han38002912019-05-16 04:01:54 +09004145func assertString(t *testing.T, got, expected string) {
4146 t.Helper()
4147 if got != expected {
4148 t.Errorf("expected %q got %q", expected, got)
4149 }
4150}
4151
4152func assertArrayString(t *testing.T, got, expected []string) {
4153 t.Helper()
4154 if len(got) != len(expected) {
4155 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
4156 return
4157 }
4158 for i := range got {
4159 if got[i] != expected[i] {
4160 t.Errorf("expected %d-th %q (%q) got %q (%q)",
4161 i, expected[i], expected, got[i], got)
4162 return
4163 }
4164 }
4165}
Colin Crosse1bb5d02019-09-24 14:55:04 -07004166
Jooyung Han0302a842019-10-30 18:43:49 +09004167func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
4168 t.Helper()
Cole Faust18994c72023-02-28 16:02:16 -08004169 assertArrayString(t, android.SortedKeys(m), expected)
Jooyung Han0302a842019-10-30 18:43:49 +09004170}
4171
Colin Crosse1bb5d02019-09-24 14:55:04 -07004172func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004173 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07004174 ctx := testCc(t, `
4175 cc_defaults {
4176 name: "defaults",
4177 srcs: ["foo.c"],
4178 static: {
4179 srcs: ["bar.c"],
4180 },
4181 shared: {
4182 srcs: ["baz.c"],
4183 },
Liz Kammer3cf52112021-03-31 15:42:03 -04004184 bazel_module: {
4185 bp2build_available: true,
4186 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07004187 }
4188
4189 cc_library_static {
4190 name: "libstatic",
4191 defaults: ["defaults"],
4192 }
4193
4194 cc_library_shared {
4195 name: "libshared",
4196 defaults: ["defaults"],
4197 }
4198
4199 cc_library {
4200 name: "libboth",
4201 defaults: ["defaults"],
4202 }
4203
4204 cc_binary {
4205 name: "binary",
4206 defaults: ["defaults"],
4207 }`)
4208
Colin Cross7113d202019-11-20 16:39:12 -08004209 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004210 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4211 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
4212 }
Colin Cross7113d202019-11-20 16:39:12 -08004213 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004214 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4215 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
4216 }
Colin Cross7113d202019-11-20 16:39:12 -08004217 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004218 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
4219 t.Errorf("binary ld rule wanted %q, got %q", w, g)
4220 }
4221
Colin Cross7113d202019-11-20 16:39:12 -08004222 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004223 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4224 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
4225 }
Colin Cross7113d202019-11-20 16:39:12 -08004226 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004227 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4228 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
4229 }
4230}
Colin Crosseabaedd2020-02-06 17:01:55 -08004231
4232func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004233 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08004234 bp := `
4235 cc_defaults {
4236 name: "libfoo_defaults",
4237 srcs: ["foo.c"],
4238 cppflags: ["-DFOO"],
4239 product_variables: {
4240 debuggable: {
4241 cppflags: ["-DBAR"],
4242 },
4243 },
4244 }
4245
4246 cc_library {
4247 name: "libfoo",
4248 defaults: ["libfoo_defaults"],
4249 }
4250 `
4251
Paul Duffin8567f222021-03-23 00:02:06 +00004252 result := android.GroupFixturePreparers(
4253 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004254 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08004255
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004256 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4257 variables.Debuggable = BoolPtr(true)
4258 }),
4259 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08004260
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004261 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00004262 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08004263}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004264
4265func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4266 t.Parallel()
4267 bp := `
4268 cc_library_static {
4269 name: "libfoo",
4270 srcs: ["foo.c"],
4271 whole_static_libs: ["libbar"],
4272 }
4273
4274 cc_library_static {
4275 name: "libbar",
4276 whole_static_libs: ["libmissing"],
4277 }
4278 `
4279
Paul Duffin8567f222021-03-23 00:02:06 +00004280 result := android.GroupFixturePreparers(
4281 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004282 android.PrepareForTestWithAllowMissingDependencies,
4283 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004284
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004285 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004286 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004287
Paul Duffine84b1332021-03-12 11:59:43 +00004288 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07004289
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004290 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004291 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07004292}
Colin Crosse9fe2942020-11-10 18:12:15 -08004293
4294func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004295 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08004296 bp := `
4297 cc_binary {
4298 name: "bin",
4299 host_supported: true,
4300 shared_libs: ["libshared"],
4301 runtime_libs: ["libruntime"],
4302 srcs: [":gen"],
4303 }
4304
4305 cc_library_shared {
4306 name: "libshared",
4307 host_supported: true,
4308 shared_libs: ["libtransitive"],
4309 }
4310
4311 cc_library_shared {
4312 name: "libtransitive",
4313 host_supported: true,
4314 }
4315
4316 cc_library_shared {
4317 name: "libruntime",
4318 host_supported: true,
4319 }
4320
4321 cc_binary_host {
4322 name: "tool",
4323 srcs: ["foo.cpp"],
4324 }
4325
4326 genrule {
4327 name: "gen",
4328 tools: ["tool"],
4329 out: ["gen.cpp"],
4330 cmd: "$(location tool) $(out)",
4331 }
4332 `
4333
Paul Duffinc3e6ce02021-03-22 23:21:32 +00004334 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08004335 ctx := testCcWithConfig(t, config)
4336
4337 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4338 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4339 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4340 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4341 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4342
4343 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4344 t.Errorf("expected host bin dependency %q, got %q", w, g)
4345 }
4346
4347 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4348 t.Errorf("expected host bin dependency %q, got %q", w, g)
4349 }
4350
4351 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4352 t.Errorf("expected host bin dependency %q, got %q", w, g)
4353 }
4354
4355 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4356 t.Errorf("expected host bin dependency %q, got %q", w, g)
4357 }
4358
4359 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4360 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4361 }
4362
4363 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4364 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4365 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4366 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4367
4368 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4369 t.Errorf("expected device bin dependency %q, got %q", w, g)
4370 }
4371
4372 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4373 t.Errorf("expected device bin dependency %q, got %q", w, g)
4374 }
4375
4376 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4377 t.Errorf("expected device bin dependency %q, got %q", w, g)
4378 }
4379
4380 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4381 t.Errorf("expected device bin dependency %q, got %q", w, g)
4382 }
4383
4384 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4385 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4386 }
4387
4388}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004389
4390func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004391 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09004392 ctx := testCc(t, `
4393 cc_library_shared {
4394 name: "libclient",
4395 srcs: ["foo.c"],
4396 shared_libs: ["libfoo#1"],
4397 }
4398
4399 cc_library_shared {
4400 name: "libfoo",
4401 srcs: ["foo.c"],
4402 shared_libs: ["libbar"],
4403 export_shared_lib_headers: ["libbar"],
4404 stubs: {
4405 symbol_file: "foo.map.txt",
4406 versions: ["1", "2", "3"],
4407 },
4408 }
4409
4410 cc_library_shared {
4411 name: "libbar",
4412 export_include_dirs: ["include/libbar"],
4413 srcs: ["foo.c"],
4414 }`)
4415
4416 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4417
4418 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4419 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4420 }
4421}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004422
Vinh Tran367d89d2023-04-28 11:21:25 -04004423func TestAidlLibraryWithHeader(t *testing.T) {
4424 t.Parallel()
4425 ctx := android.GroupFixturePreparers(
4426 prepareForCcTest,
4427 aidl_library.PrepareForTestWithAidlLibrary,
4428 android.MockFS{
4429 "package_bar/Android.bp": []byte(`
4430 aidl_library {
4431 name: "bar",
4432 srcs: ["x/y/Bar.aidl"],
4433 strip_import_prefix: "x",
4434 }
4435 `)}.AddToFixture(),
4436 android.MockFS{
4437 "package_foo/Android.bp": []byte(`
4438 aidl_library {
4439 name: "foo",
4440 srcs: ["a/b/Foo.aidl"],
4441 strip_import_prefix: "a",
4442 deps: ["bar"],
4443 }
4444 cc_library {
4445 name: "libfoo",
4446 aidl: {
4447 libs: ["foo"],
4448 }
4449 }
4450 `),
4451 }.AddToFixture(),
4452 ).RunTest(t).TestContext
4453
4454 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4455 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4456 aidlCommand := manifest.Commands[0].GetCommand()
4457
4458 expectedAidlFlags := "-Ipackage_foo/a -Ipackage_bar/x"
4459 if !strings.Contains(aidlCommand, expectedAidlFlags) {
4460 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlags)
4461 }
4462
4463 outputs := strings.Join(libfoo.AllOutputs(), " ")
4464
4465 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl/b/BpFoo.h")
4466 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl/b/BnFoo.h")
4467 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl/b/Foo.h")
4468 android.AssertStringDoesContain(t, "aidl-generated cpp", outputs, "b/Foo.cpp")
4469 // Confirm that the aidl header doesn't get compiled to cpp and h files
4470 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl/y/BpBar.h")
4471 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl/y/BnBar.h")
4472 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl/y/Bar.h")
4473 android.AssertStringDoesNotContain(t, "aidl-generated cpp", outputs, "y/Bar.cpp")
4474}
4475
Jooyung Hane197d8b2021-01-05 10:33:16 +09004476func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004477 t.Parallel()
Vinh Tran367d89d2023-04-28 11:21:25 -04004478 ctx := android.GroupFixturePreparers(
4479 prepareForCcTest,
4480 aidl_library.PrepareForTestWithAidlLibrary,
4481 ).RunTestWithBp(t, `
Jooyung Hane197d8b2021-01-05 10:33:16 +09004482 cc_library {
4483 name: "libfoo",
4484 srcs: ["a/Foo.aidl"],
4485 aidl: { flags: ["-Werror"], },
4486 }
4487 `)
4488
4489 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4490 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4491 aidlCommand := manifest.Commands[0].GetCommand()
4492 expectedAidlFlag := "-Werror"
4493 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4494 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4495 }
4496}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004497
Jooyung Han07f70c02021-11-06 07:08:45 +09004498func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004499 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09004500 for _, tc := range []struct {
4501 name string
4502 sdkVersion string
4503 variant string
4504 expected string
4505 }{
4506 {
4507 name: "default is current",
4508 sdkVersion: "",
4509 variant: "android_arm64_armv8-a_static",
4510 expected: "platform_apis",
4511 },
4512 {
4513 name: "use sdk_version",
4514 sdkVersion: `sdk_version: "29"`,
4515 variant: "android_arm64_armv8-a_static",
4516 expected: "platform_apis",
4517 },
4518 {
4519 name: "use sdk_version(sdk variant)",
4520 sdkVersion: `sdk_version: "29"`,
4521 variant: "android_arm64_armv8-a_sdk_static",
4522 expected: "29",
4523 },
4524 {
4525 name: "use min_sdk_version",
4526 sdkVersion: `min_sdk_version: "29"`,
4527 variant: "android_arm64_armv8-a_static",
4528 expected: "29",
4529 },
4530 } {
4531 t.Run(tc.name, func(t *testing.T) {
4532 ctx := testCc(t, `
4533 cc_library {
4534 name: "libfoo",
4535 stl: "none",
4536 srcs: ["a/Foo.aidl"],
4537 `+tc.sdkVersion+`
4538 }
4539 `)
4540 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
4541 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4542 aidlCommand := manifest.Commands[0].GetCommand()
4543 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4544 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4545 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4546 }
4547 })
4548 }
4549}
4550
Jiyong Parka008fb02021-03-16 17:15:53 +09004551func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004552 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004553 ctx := testCc(t, `
4554 cc_library_shared {
4555 name: "libfoo",
4556 srcs: ["foo.c"],
4557 min_sdk_version: "29",
4558 }`)
4559
4560 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4561 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4562}
4563
Vinh Tranf1924742022-06-24 16:40:11 -04004564func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004565 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004566 bp := `
4567 cc_library_shared {
4568 name: "libfoo",
4569 srcs: ["foo.c"],
4570 min_sdk_version: "S",
4571 }
4572 `
4573 result := android.GroupFixturePreparers(
4574 prepareForCcTest,
4575 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4576 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4577 }),
4578 ).RunTestWithBp(t, bp)
4579 ctx := result.TestContext
4580 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4581 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4582}
4583
Paul Duffin3cb603e2021-02-19 13:57:10 +00004584func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004585 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004586
4587 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4588 // embedded newline characters alone.
4589 trimIndentingSpaces := func(s string) string {
4590 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4591 }
4592
4593 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4594 t.Helper()
4595 expected = trimIndentingSpaces(expected)
4596 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4597 if expected != actual {
4598 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4599 }
4600 }
4601
4602 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4603
4604 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4605 t.Helper()
4606 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4607 name := module.Name()
4608
4609 for _, checker := range checkers {
4610 checker(t, name, exported)
4611 }
4612 }
4613
4614 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4615 return func(t *testing.T, name string, exported FlagExporterInfo) {
4616 t.Helper()
4617 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4618 }
4619 }
4620
4621 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4622 return func(t *testing.T, name string, exported FlagExporterInfo) {
4623 t.Helper()
4624 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4625 }
4626 }
4627
4628 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4629 return func(t *testing.T, name string, exported FlagExporterInfo) {
4630 t.Helper()
4631 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4632 }
4633 }
4634
4635 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4636 return func(t *testing.T, name string, exported FlagExporterInfo) {
4637 t.Helper()
4638 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4639 }
4640 }
4641
4642 genRuleModules := `
4643 genrule {
4644 name: "genrule_foo",
4645 cmd: "generate-foo",
4646 out: [
4647 "generated_headers/foo/generated_header.h",
4648 ],
4649 export_include_dirs: [
4650 "generated_headers",
4651 ],
4652 }
4653
4654 genrule {
4655 name: "genrule_bar",
4656 cmd: "generate-bar",
4657 out: [
4658 "generated_headers/bar/generated_header.h",
4659 ],
4660 export_include_dirs: [
4661 "generated_headers",
4662 ],
4663 }
4664 `
4665
4666 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4667 ctx := testCc(t, genRuleModules+`
4668 cc_library {
4669 name: "libfoo",
4670 srcs: ["foo.c"],
4671 export_include_dirs: ["foo/standard"],
4672 export_system_include_dirs: ["foo/system"],
4673 generated_headers: ["genrule_foo"],
4674 export_generated_headers: ["genrule_foo"],
4675 }
4676
4677 cc_library {
4678 name: "libbar",
4679 srcs: ["bar.c"],
4680 shared_libs: ["libfoo"],
4681 export_include_dirs: ["bar/standard"],
4682 export_system_include_dirs: ["bar/system"],
4683 generated_headers: ["genrule_bar"],
4684 export_generated_headers: ["genrule_bar"],
4685 }
4686 `)
4687 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4688 checkIncludeDirs(t, ctx, foo,
4689 expectedIncludeDirs(`
4690 foo/standard
4691 .intermediates/genrule_foo/gen/generated_headers
4692 `),
4693 expectedSystemIncludeDirs(`foo/system`),
4694 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4695 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4696 )
4697
4698 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4699 checkIncludeDirs(t, ctx, bar,
4700 expectedIncludeDirs(`
4701 bar/standard
4702 .intermediates/genrule_bar/gen/generated_headers
4703 `),
4704 expectedSystemIncludeDirs(`bar/system`),
4705 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4706 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4707 )
4708 })
4709
4710 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4711 ctx := testCc(t, genRuleModules+`
4712 cc_library {
4713 name: "libfoo",
4714 srcs: ["foo.c"],
4715 export_include_dirs: ["foo/standard"],
4716 export_system_include_dirs: ["foo/system"],
4717 generated_headers: ["genrule_foo"],
4718 export_generated_headers: ["genrule_foo"],
4719 }
4720
4721 cc_library {
4722 name: "libbar",
4723 srcs: ["bar.c"],
4724 whole_static_libs: ["libfoo"],
4725 export_include_dirs: ["bar/standard"],
4726 export_system_include_dirs: ["bar/system"],
4727 generated_headers: ["genrule_bar"],
4728 export_generated_headers: ["genrule_bar"],
4729 }
4730 `)
4731 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4732 checkIncludeDirs(t, ctx, foo,
4733 expectedIncludeDirs(`
4734 foo/standard
4735 .intermediates/genrule_foo/gen/generated_headers
4736 `),
4737 expectedSystemIncludeDirs(`foo/system`),
4738 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4739 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4740 )
4741
4742 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4743 checkIncludeDirs(t, ctx, bar,
4744 expectedIncludeDirs(`
4745 bar/standard
4746 foo/standard
4747 .intermediates/genrule_foo/gen/generated_headers
4748 .intermediates/genrule_bar/gen/generated_headers
4749 `),
4750 expectedSystemIncludeDirs(`
4751 bar/system
4752 foo/system
4753 `),
4754 expectedGeneratedHeaders(`
4755 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4756 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4757 `),
4758 expectedOrderOnlyDeps(`
4759 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4760 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4761 `),
4762 )
4763 })
4764
Paul Duffin3cb603e2021-02-19 13:57:10 +00004765 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004766 ctx := android.GroupFixturePreparers(
4767 prepareForCcTest,
4768 aidl_library.PrepareForTestWithAidlLibrary,
4769 ).RunTestWithBp(t, `
4770 aidl_library {
4771 name: "libfoo_aidl",
4772 srcs: ["x/y/Bar.aidl"],
4773 strip_import_prefix: "x",
4774 }
Paul Duffin3cb603e2021-02-19 13:57:10 +00004775 cc_library_shared {
4776 name: "libfoo",
4777 srcs: [
4778 "foo.c",
4779 "b.aidl",
4780 "a.proto",
4781 ],
4782 aidl: {
Vinh Tran367d89d2023-04-28 11:21:25 -04004783 libs: ["libfoo_aidl"],
Paul Duffin3cb603e2021-02-19 13:57:10 +00004784 export_aidl_headers: true,
4785 }
4786 }
Vinh Tran367d89d2023-04-28 11:21:25 -04004787 `).TestContext
Paul Duffin3cb603e2021-02-19 13:57:10 +00004788 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4789 checkIncludeDirs(t, ctx, foo,
4790 expectedIncludeDirs(`
4791 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4792 `),
4793 expectedSystemIncludeDirs(``),
4794 expectedGeneratedHeaders(`
4795 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4796 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4797 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran367d89d2023-04-28 11:21:25 -04004798 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/y/Bar.h
4799 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/y/BnBar.h
4800 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004801 `),
4802 expectedOrderOnlyDeps(`
4803 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4804 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4805 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran367d89d2023-04-28 11:21:25 -04004806 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/y/Bar.h
4807 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/y/BnBar.h
4808 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004809 `),
4810 )
4811 })
4812
Paul Duffin3cb603e2021-02-19 13:57:10 +00004813 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4814 ctx := testCc(t, genRuleModules+`
4815 cc_library_shared {
4816 name: "libfoo",
4817 srcs: [
4818 "foo.c",
4819 "b.aidl",
4820 "a.proto",
4821 ],
4822 proto: {
4823 export_proto_headers: true,
4824 }
4825 }
4826 `)
4827 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4828 checkIncludeDirs(t, ctx, foo,
4829 expectedIncludeDirs(`
4830 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4831 `),
4832 expectedSystemIncludeDirs(``),
4833 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004834 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4835 `),
4836 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004837 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4838 `),
4839 )
4840 })
4841
Paul Duffin33056e82021-02-19 13:49:08 +00004842 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004843 ctx := testCc(t, genRuleModules+`
4844 cc_library_shared {
4845 name: "libfoo",
4846 srcs: [
4847 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004848 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004849 "b.aidl",
4850 "a.proto",
4851 ],
4852 }
4853 `)
4854 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4855 checkIncludeDirs(t, ctx, foo,
4856 expectedIncludeDirs(`
4857 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4858 `),
4859 expectedSystemIncludeDirs(``),
4860 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004861 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004862 `),
4863 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004864 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4865 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004866 `),
4867 )
4868 })
4869}
Colin Crossae628182021-06-14 16:52:28 -07004870
4871func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004872 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004873 baseExpectedFlags := []string{
4874 "${config.ArmThumbCflags}",
4875 "${config.ArmCflags}",
4876 "${config.CommonGlobalCflags}",
4877 "${config.DeviceGlobalCflags}",
4878 "${config.ExternalCflags}",
4879 "${config.ArmToolchainCflags}",
4880 "${config.ArmArmv7ANeonCflags}",
4881 "${config.ArmGenericCflags}",
4882 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004883 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004884 }
4885
4886 expectedIncludes := []string{
4887 "external/foo/android_arm_export_include_dirs",
4888 "external/foo/lib32_export_include_dirs",
4889 "external/foo/arm_export_include_dirs",
4890 "external/foo/android_export_include_dirs",
4891 "external/foo/linux_export_include_dirs",
4892 "external/foo/export_include_dirs",
4893 "external/foo/android_arm_local_include_dirs",
4894 "external/foo/lib32_local_include_dirs",
4895 "external/foo/arm_local_include_dirs",
4896 "external/foo/android_local_include_dirs",
4897 "external/foo/linux_local_include_dirs",
4898 "external/foo/local_include_dirs",
4899 "external/foo",
4900 "external/foo/libheader1",
4901 "external/foo/libheader2",
4902 "external/foo/libwhole1",
4903 "external/foo/libwhole2",
4904 "external/foo/libstatic1",
4905 "external/foo/libstatic2",
4906 "external/foo/libshared1",
4907 "external/foo/libshared2",
4908 "external/foo/liblinux",
4909 "external/foo/libandroid",
4910 "external/foo/libarm",
4911 "external/foo/lib32",
4912 "external/foo/libandroid_arm",
4913 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004914 }
4915
4916 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4917 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4918
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004919 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesab5e4c62022-03-28 16:47:17 -07004920 cstd := []string{"-std=gnu11", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004921 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004922
4923 lastIncludes := []string{
4924 "out/soong/ndk/sysroot/usr/include",
4925 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4926 }
4927
4928 combineSlices := func(slices ...[]string) []string {
4929 var ret []string
4930 for _, s := range slices {
4931 ret = append(ret, s...)
4932 }
4933 return ret
4934 }
4935
4936 testCases := []struct {
4937 name string
4938 src string
4939 expected []string
4940 }{
4941 {
4942 name: "c",
4943 src: "foo.c",
Stephen Hinese24303f2021-12-14 15:07:08 -08004944 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004945 },
4946 {
4947 name: "cc",
4948 src: "foo.cc",
Stephen Hinese24303f2021-12-14 15:07:08 -08004949 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004950 },
4951 {
4952 name: "assemble",
4953 src: "foo.s",
Liz Kammere4d1bda2022-06-22 21:02:08 +00004954 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004955 },
4956 }
4957
4958 for _, tc := range testCases {
4959 t.Run(tc.name, func(t *testing.T) {
4960 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004961 cc_library {
4962 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004963 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004964 cflags: ["-std=candcpp"],
4965 conlyflags: ["-std=conly"],
4966 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004967 local_include_dirs: ["local_include_dirs"],
4968 export_include_dirs: ["export_include_dirs"],
4969 export_system_include_dirs: ["export_system_include_dirs"],
4970 static_libs: ["libstatic1", "libstatic2"],
4971 whole_static_libs: ["libwhole1", "libwhole2"],
4972 shared_libs: ["libshared1", "libshared2"],
4973 header_libs: ["libheader1", "libheader2"],
4974 target: {
4975 android: {
4976 shared_libs: ["libandroid"],
4977 local_include_dirs: ["android_local_include_dirs"],
4978 export_include_dirs: ["android_export_include_dirs"],
4979 },
4980 android_arm: {
4981 shared_libs: ["libandroid_arm"],
4982 local_include_dirs: ["android_arm_local_include_dirs"],
4983 export_include_dirs: ["android_arm_export_include_dirs"],
4984 },
4985 linux: {
4986 shared_libs: ["liblinux"],
4987 local_include_dirs: ["linux_local_include_dirs"],
4988 export_include_dirs: ["linux_export_include_dirs"],
4989 },
4990 },
4991 multilib: {
4992 lib32: {
4993 shared_libs: ["lib32"],
4994 local_include_dirs: ["lib32_local_include_dirs"],
4995 export_include_dirs: ["lib32_export_include_dirs"],
4996 },
4997 },
4998 arch: {
4999 arm: {
5000 shared_libs: ["libarm"],
5001 local_include_dirs: ["arm_local_include_dirs"],
5002 export_include_dirs: ["arm_export_include_dirs"],
5003 },
5004 },
5005 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005006 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005007 }
5008
5009 cc_library_headers {
5010 name: "libheader1",
5011 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005012 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005013 stl: "none",
5014 }
5015
5016 cc_library_headers {
5017 name: "libheader2",
5018 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005019 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005020 stl: "none",
5021 }
Liz Kammer08572c62021-09-30 10:11:04 -04005022 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07005023
Liz Kammer08572c62021-09-30 10:11:04 -04005024 libs := []string{
5025 "libstatic1",
5026 "libstatic2",
5027 "libwhole1",
5028 "libwhole2",
5029 "libshared1",
5030 "libshared2",
5031 "libandroid",
5032 "libandroid_arm",
5033 "liblinux",
5034 "lib32",
5035 "libarm",
5036 }
Colin Crossae628182021-06-14 16:52:28 -07005037
Liz Kammer08572c62021-09-30 10:11:04 -04005038 for _, lib := range libs {
5039 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07005040 cc_library {
5041 name: "%s",
5042 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005043 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005044 stl: "none",
5045 }
5046 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04005047 }
5048
5049 ctx := android.GroupFixturePreparers(
5050 PrepareForIntegrationTestWithCc,
5051 android.FixtureAddTextFile("external/foo/Android.bp", bp),
5052 ).RunTest(t)
5053 // Use the arm variant instead of the arm64 variant so that it gets headers from
5054 // ndk_libandroid_support to test LateStaticLibs.
5055 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
5056
5057 var includes []string
5058 flags := strings.Split(cflags, " ")
5059 for _, flag := range flags {
5060 if strings.HasPrefix(flag, "-I") {
5061 includes = append(includes, strings.TrimPrefix(flag, "-I"))
5062 } else if flag == "-isystem" {
5063 // skip isystem, include next
5064 } else if len(flag) > 0 {
5065 includes = append(includes, flag)
5066 }
5067 }
5068
5069 android.AssertArrayString(t, "includes", tc.expected, includes)
5070 })
Colin Crossae628182021-06-14 16:52:28 -07005071 }
5072
Colin Crossae628182021-06-14 16:52:28 -07005073}
Alixb5f6d9e2022-04-20 23:00:58 +00005074
zijunzhao933e3802023-01-12 07:26:20 +00005075func TestAddnoOverride64GlobalCflags(t *testing.T) {
5076 t.Parallel()
5077 ctx := testCc(t, `
5078 cc_library_shared {
5079 name: "libclient",
5080 srcs: ["foo.c"],
5081 shared_libs: ["libfoo#1"],
5082 }
5083
5084 cc_library_shared {
5085 name: "libfoo",
5086 srcs: ["foo.c"],
5087 shared_libs: ["libbar"],
5088 export_shared_lib_headers: ["libbar"],
5089 stubs: {
5090 symbol_file: "foo.map.txt",
5091 versions: ["1", "2", "3"],
5092 },
5093 }
5094
5095 cc_library_shared {
5096 name: "libbar",
5097 export_include_dirs: ["include/libbar"],
5098 srcs: ["foo.c"],
5099 }`)
5100
5101 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
5102
5103 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
5104 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
5105 }
5106}
5107
Alixb5f6d9e2022-04-20 23:00:58 +00005108func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005109 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00005110 tests := []struct {
5111 name string
5112 clang bool
5113 BuildBrokenClangProperty bool
5114 err string
5115 }{
5116 {
5117 name: "error when clang is set to false",
5118 clang: false,
5119 err: "is no longer supported",
5120 },
5121 {
5122 name: "error when clang is set to true",
5123 clang: true,
5124 err: "property is deprecated, see Changes.md",
5125 },
5126 {
5127 name: "no error when BuildBrokenClangProperty is explicitly set to true",
5128 clang: true,
5129 BuildBrokenClangProperty: true,
5130 },
5131 }
5132
5133 for _, test := range tests {
5134 t.Run(test.name, func(t *testing.T) {
5135 bp := fmt.Sprintf(`
5136 cc_library {
5137 name: "foo",
5138 clang: %t,
5139 }`, test.clang)
5140
5141 if test.err == "" {
5142 android.GroupFixturePreparers(
5143 prepareForCcTest,
5144 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5145 if test.BuildBrokenClangProperty {
5146 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
5147 }
5148 }),
5149 ).RunTestWithBp(t, bp)
5150 } else {
5151 prepareForCcTest.
5152 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5153 RunTestWithBp(t, bp)
5154 }
5155 })
5156 }
5157}
Alix Espinoef47e542022-09-14 19:10:51 +00005158
5159func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005160 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005161 tests := []struct {
5162 name string
5163 clangAsFlags []string
5164 BuildBrokenClangAsFlags bool
5165 err string
5166 }{
5167 {
5168 name: "error when clang_asflags is set",
5169 clangAsFlags: []string{"-a", "-b"},
5170 err: "clang_asflags: property is deprecated",
5171 },
5172 {
5173 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
5174 clangAsFlags: []string{"-a", "-b"},
5175 BuildBrokenClangAsFlags: true,
5176 },
5177 }
5178
5179 for _, test := range tests {
5180 t.Run(test.name, func(t *testing.T) {
5181 bp := fmt.Sprintf(`
5182 cc_library {
5183 name: "foo",
5184 clang_asflags: %s,
5185 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
5186
5187 if test.err == "" {
5188 android.GroupFixturePreparers(
5189 prepareForCcTest,
5190 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5191 if test.BuildBrokenClangAsFlags {
5192 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
5193 }
5194 }),
5195 ).RunTestWithBp(t, bp)
5196 } else {
5197 prepareForCcTest.
5198 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5199 RunTestWithBp(t, bp)
5200 }
5201 })
5202 }
5203}
5204
5205func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005206 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005207 tests := []struct {
5208 name string
5209 clangCFlags []string
5210 BuildBrokenClangCFlags bool
5211 err string
5212 }{
5213 {
5214 name: "error when clang_cflags is set",
5215 clangCFlags: []string{"-a", "-b"},
5216 err: "clang_cflags: property is deprecated",
5217 },
5218 {
5219 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
5220 clangCFlags: []string{"-a", "-b"},
5221 BuildBrokenClangCFlags: true,
5222 },
5223 }
5224
5225 for _, test := range tests {
5226 t.Run(test.name, func(t *testing.T) {
5227 bp := fmt.Sprintf(`
5228 cc_library {
5229 name: "foo",
5230 clang_cflags: %s,
5231 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
5232
5233 if test.err == "" {
5234 android.GroupFixturePreparers(
5235 prepareForCcTest,
5236 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5237 if test.BuildBrokenClangCFlags {
5238 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
5239 }
5240 }),
5241 ).RunTestWithBp(t, bp)
5242 } else {
5243 prepareForCcTest.
5244 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5245 RunTestWithBp(t, bp)
5246 }
5247 })
5248 }
5249}
Yu Liue4312402023-01-18 09:15:31 -08005250
5251func TestDclaLibraryInApex(t *testing.T) {
5252 t.Parallel()
5253 bp := `
5254 cc_library_shared {
5255 name: "cc_lib_in_apex",
5256 srcs: ["foo.cc"],
5257 apex_available: ["myapex"],
5258 bazel_module: { label: "//foo/bar:bar" },
5259 }`
5260 label := "//foo/bar:bar"
5261 arch64 := "arm64_armv8-a"
5262 arch32 := "arm_armv7-a-neon"
5263 apexCfgKey := android.ApexConfigKey{
5264 WithinApex: true,
5265 ApexSdkVersion: "28",
5266 }
5267
5268 result := android.GroupFixturePreparers(
5269 prepareForCcTest,
5270 android.FixtureRegisterWithContext(registerTestMutators),
5271 android.FixtureModifyConfig(func(config android.Config) {
5272 config.BazelContext = android.MockBazelContext{
5273 OutputBaseDir: "outputbase",
5274 LabelToCcInfo: map[string]cquery.CcInfo{
5275 android.BuildMockBazelContextResultKey(label, arch32, android.Android, apexCfgKey): cquery.CcInfo{
5276 RootDynamicLibraries: []string{"foo.so"},
5277 },
5278 android.BuildMockBazelContextResultKey(label, arch64, android.Android, apexCfgKey): cquery.CcInfo{
5279 RootDynamicLibraries: []string{"foo.so"},
5280 },
5281 },
5282 BazelRequests: make(map[string]bool),
5283 }
5284 }),
5285 ).RunTestWithBp(t, bp)
5286 ctx := result.TestContext
5287
5288 // Test if the bazel request is queued correctly
5289 key := android.BuildMockBazelContextRequestKey(label, cquery.GetCcInfo, arch32, android.Android, apexCfgKey)
5290 if !ctx.Config().BazelContext.(android.MockBazelContext).BazelRequests[key] {
5291 t.Errorf("Bazel request was not queued: %s", key)
5292 }
5293
5294 sharedFoo := ctx.ModuleForTests(ccLibInApex, "android_arm_armv7-a-neon_shared_"+apexVariationName).Module()
5295 producer := sharedFoo.(android.OutputFileProducer)
5296 outputFiles, err := producer.OutputFiles("")
5297 if err != nil {
5298 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5299 }
5300 expectedOutputFiles := []string{"outputbase/execroot/__main__/foo.so"}
5301 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
5302}
Sam Delmericoef69d472023-04-18 17:32:43 -04005303
5304func TestDisableSanitizerVariantsInMixedBuilds(t *testing.T) {
5305 t.Parallel()
5306 bp := `
5307 cc_library_static {
5308 name: "foo_ubsan_minimal",
5309 srcs: ["foo.cc"],
5310 bazel_module: { label: "//foo_ubsan_minimal" },
5311 sanitize: {
5312 all_undefined: true,
5313 integer_overflow: true,
5314 },
5315 }
5316 cc_library_static {
5317 name: "foo",
5318 srcs: ["foo.cc"],
5319 bazel_module: { label: "//foo" },
5320 sanitize: {
5321 address: true,
5322 hwaddress: true,
5323 fuzzer: true,
5324 integer_overflow: true,
5325 scs: true,
5326 },
5327 }
5328 cc_library_static {
5329 name: "foo_tsan",
5330 srcs: ["foo.cc"],
5331 bazel_module: { label: "//foo_tsan" },
5332 sanitize: {
5333 thread: true,
5334 },
5335 }
5336 cc_library_static {
5337 name: "foo_cfi",
5338 srcs: ["foo.cc"],
5339 bazel_module: { label: "//foo_cfi" },
5340 sanitize: {
5341 cfi: true,
5342 },
5343 }
5344 cc_library_static {
5345 name: "foo_memtag_stack",
5346 srcs: ["foo.cc"],
5347 bazel_module: { label: "//foo_memtag_stack" },
5348 sanitize: {
5349 memtag_stack: true,
5350 },
5351 }
5352 cc_library_static {
5353 name: "foo_memtag_heap",
5354 srcs: ["foo.cc"],
5355 bazel_module: { label: "//foo_memtag_heap" },
5356 sanitize: {
5357 memtag_heap: true,
5358 },
5359 }
5360 cc_library_static {
5361 name: "foo_safestack",
5362 srcs: ["foo.cc"],
5363 bazel_module: { label: "//foo_safestack" },
5364 sanitize: {
5365 safestack: true,
5366 },
5367 }
5368 cc_library_static {
5369 name: "foo_scudo",
5370 srcs: ["foo.cc"],
5371 bazel_module: { label: "//foo_scudo" },
5372 sanitize: {
5373 scudo: true,
5374 },
5375 }
5376 `
5377 testcases := []struct {
5378 name string
5379 variant string
5380 expectedOutputPaths []string
5381 }{
5382 {
5383 name: "foo_ubsan_minimal",
5384 variant: "android_arm64_armv8-a_static_apex28",
5385 expectedOutputPaths: []string{
5386 "outputbase/execroot/__main__/foo_ubsan_minimal.a",
5387 },
5388 },
5389 {
5390 name: "foo",
5391 variant: "android_arm64_armv8-a_static_apex28",
5392 expectedOutputPaths: []string{
5393 "outputbase/execroot/__main__/foo.a",
5394 },
5395 },
5396 {
5397 name: "foo",
5398 variant: "android_arm_armv7-a-neon_static_asan_apex28",
5399 expectedOutputPaths: []string{
5400 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_apex28/foo.a",
5401 },
5402 },
5403 {
5404 name: "foo",
5405 variant: "android_arm64_armv8-a_static_hwasan_apex28",
5406 expectedOutputPaths: []string{
5407 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_apex28/foo.a",
5408 },
5409 },
5410 {
5411 name: "foo",
5412 variant: "android_arm64_armv8-a_static_fuzzer_apex28",
5413 expectedOutputPaths: []string{
5414 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_fuzzer_apex28/foo.a",
5415 },
5416 },
5417 {
5418 name: "foo",
5419 variant: "android_arm_armv7-a-neon_static_asan_fuzzer_apex28",
5420 expectedOutputPaths: []string{
5421 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_fuzzer_apex28/foo.a",
5422 },
5423 },
5424 {
5425 name: "foo",
5426 variant: "android_arm64_armv8-a_static_hwasan_fuzzer_apex28",
5427 expectedOutputPaths: []string{
5428 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_fuzzer_apex28/foo.a",
5429 },
5430 },
5431 {
5432 name: "foo",
5433 variant: "android_arm64_armv8-a_static_scs_apex28",
5434 expectedOutputPaths: []string{
5435 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_scs_apex28/foo.a",
5436 },
5437 },
5438 {
5439 name: "foo",
5440 variant: "android_arm64_armv8-a_static_hwasan_scs_apex28",
5441 expectedOutputPaths: []string{
5442 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_apex28/foo.a",
5443 },
5444 },
5445 {
5446 name: "foo",
5447 variant: "android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28",
5448 expectedOutputPaths: []string{
5449 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28/foo.a",
5450 },
5451 },
5452 {
5453 name: "foo_tsan",
5454 variant: "android_arm64_armv8-a_static_apex28",
5455 expectedOutputPaths: []string{
5456 "outputbase/execroot/__main__/foo_tsan.a",
5457 },
5458 },
5459 {
5460 name: "foo_tsan",
5461 variant: "android_arm64_armv8-a_static_tsan_apex28",
5462 expectedOutputPaths: []string{
5463 "out/soong/.intermediates/foo_tsan/android_arm64_armv8-a_static_tsan_apex28/foo_tsan.a",
5464 },
5465 },
5466 {
5467 name: "foo_cfi",
5468 variant: "android_arm64_armv8-a_static_apex28",
5469 expectedOutputPaths: []string{
5470 "outputbase/execroot/__main__/foo_cfi.a",
5471 },
5472 },
5473 {
5474 name: "foo_cfi",
5475 variant: "android_arm64_armv8-a_static_cfi_apex28",
5476 expectedOutputPaths: []string{
5477 "out/soong/.intermediates/foo_cfi/android_arm64_armv8-a_static_cfi_apex28/foo_cfi.a",
5478 },
5479 },
5480 {
5481 name: "foo_memtag_stack",
5482 variant: "android_arm64_armv8-a_static_apex28",
5483 expectedOutputPaths: []string{
5484 "out/soong/.intermediates/foo_memtag_stack/android_arm64_armv8-a_static_apex28/foo_memtag_stack.a",
5485 },
5486 },
5487 {
5488 name: "foo_memtag_heap",
5489 variant: "android_arm64_armv8-a_static_apex28",
5490 expectedOutputPaths: []string{
5491 "out/soong/.intermediates/foo_memtag_heap/android_arm64_armv8-a_static_apex28/foo_memtag_heap.a",
5492 },
5493 },
5494 {
5495 name: "foo_safestack",
5496 variant: "android_arm64_armv8-a_static_apex28",
5497 expectedOutputPaths: []string{
5498 "out/soong/.intermediates/foo_safestack/android_arm64_armv8-a_static_apex28/foo_safestack.a",
5499 },
5500 },
5501 {
5502 name: "foo_scudo",
5503 variant: "android_arm64_armv8-a_static_apex28",
5504 expectedOutputPaths: []string{
5505 "out/soong/.intermediates/foo_scudo/android_arm64_armv8-a_static_apex28/foo_scudo.a",
5506 },
5507 },
5508 }
5509
5510 ctx := android.GroupFixturePreparers(
5511 prepareForCcTest,
5512 prepareForAsanTest,
5513 android.FixtureRegisterWithContext(registerTestMutators),
5514 android.FixtureModifyConfig(func(config android.Config) {
5515 config.BazelContext = android.MockBazelContext{
5516 OutputBaseDir: "outputbase",
5517 LabelToCcInfo: map[string]cquery.CcInfo{
5518 "//foo_ubsan_minimal": {
5519 RootStaticArchives: []string{"foo_ubsan_minimal.a"},
5520 },
5521 "//foo": {
5522 RootStaticArchives: []string{"foo.a"},
5523 },
5524 "//foo_tsan": {
5525 RootStaticArchives: []string{"foo_tsan.a"},
5526 },
5527 "//foo_cfi": {
5528 RootStaticArchives: []string{"foo_cfi.a"},
5529 },
5530 "//foo_memtag_stack": {
5531 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5532 },
5533 "//foo_memtag_heap": {
5534 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5535 },
5536 "//foo_safestack": {
5537 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5538 },
5539 "//foo_scudo": {
5540 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5541 },
5542 },
5543 }
5544 }),
5545 ).RunTestWithBp(t, bp).TestContext
5546
5547 for _, tc := range testcases {
5548 fooMod := ctx.ModuleForTests(tc.name, tc.variant).Module()
5549 outputFiles, err := fooMod.(android.OutputFileProducer).OutputFiles("")
5550 if err != nil {
5551 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5552 }
5553 android.AssertPathsRelativeToTopEquals(t, "output files", tc.expectedOutputPaths, outputFiles)
5554 }
5555}