blob: 7ce0f3715cfca7b71fafb81fc6794645f7f11d9b [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,
Paul Duffin02a3d652021-02-24 18:51:54 +000042 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
43 variables.DeviceVndkVersion = StringPtr("current")
44 variables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090045 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000046 }),
47)
48
Yu Liue4312402023-01-18 09:15:31 -080049var ccLibInApex = "cc_lib_in_apex"
50var apexVariationName = "apex28"
51var apexVersion = "28"
52
53func registerTestMutators(ctx android.RegistrationContext) {
54 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
55 ctx.BottomUp("apex", testApexMutator).Parallel()
56 ctx.BottomUp("mixed_builds_prep", mixedBuildsPrepareMutator).Parallel()
57 })
58}
59
60func mixedBuildsPrepareMutator(ctx android.BottomUpMutatorContext) {
61 if m := ctx.Module(); m.Enabled() {
62 if mixedBuildMod, ok := m.(android.MixedBuildBuildable); ok {
MarkDacekf47e1422023-04-19 16:47:36 +000063 if mixedBuildMod.IsMixedBuildSupported(ctx) && android.MixedBuildsEnabled(ctx) == android.MixedBuildEnabled {
Yu Liue4312402023-01-18 09:15:31 -080064 mixedBuildMod.QueueBazelCall(ctx)
65 }
66 }
67 }
68}
69
70func testApexMutator(mctx android.BottomUpMutatorContext) {
71 modules := mctx.CreateVariations(apexVariationName)
72 apexInfo := android.ApexInfo{
73 ApexVariationName: apexVariationName,
74 MinSdkVersion: android.ApiLevelForTest(apexVersion),
75 }
76 mctx.SetVariationProvider(modules[0], android.ApexInfoProvider, apexInfo)
77}
78
Paul Duffin8567f222021-03-23 00:02:06 +000079// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000080//
81// See testCc for an explanation as to how to stop using this deprecated method.
82//
83// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080084func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070085 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000086 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000087 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090088}
89
Paul Duffin8567f222021-03-23 00:02:06 +000090// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000091//
Paul Duffin8567f222021-03-23 00:02:06 +000092// 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 +000093// easier to customize the test behavior.
94//
95// 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 +000096// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000097// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
98// that it did not change the test behavior unexpectedly.
99//
100// deprecated
Logan Chienf3511742017-10-31 18:04:35 +0800101func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800102 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +0000103 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +0000104 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +0800105}
106
Paul Duffin8567f222021-03-23 00:02:06 +0000107// testCcNoVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000108//
109// See testCc for an explanation as to how to stop using this deprecated method.
110//
111// deprecated
Logan Chienf3511742017-10-31 18:04:35 +0800112func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800113 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000114 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900115 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Logan Chienf3511742017-10-31 18:04:35 +0800116
Colin Cross98be1bb2019-12-13 20:41:13 -0800117 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800118}
119
Paul Duffin8567f222021-03-23 00:02:06 +0000120// testCcNoProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000121//
122// See testCc for an explanation as to how to stop using this deprecated method.
123//
124// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +0900125func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
126 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000127 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun8a2600c2020-12-07 12:44:03 +0900128 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900129 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun8a2600c2020-12-07 12:44:03 +0900130
131 return testCcWithConfig(t, config)
132}
133
Paul Duffin8567f222021-03-23 00:02:06 +0000134// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000135//
136// See testCc for an explanation as to how to stop using this deprecated method.
137//
138// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900139func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800140 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800141
Paul Duffin8567f222021-03-23 00:02:06 +0000142 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000143 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
144 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800145}
146
Paul Duffin8567f222021-03-23 00:02:06 +0000147// testCcError runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000148//
149// See testCc for an explanation as to how to stop using this deprecated method.
150//
151// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900152func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900153 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000154 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900155 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900156 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900157 testCcErrorWithConfig(t, pattern, config)
158 return
159}
160
Paul Duffin8567f222021-03-23 00:02:06 +0000161// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000162//
163// See testCc for an explanation as to how to stop using this deprecated method.
164//
165// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900166func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900167 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000168 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900169 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
170 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900171 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900172 testCcErrorWithConfig(t, pattern, config)
173 return
174}
175
Logan Chienf3511742017-10-31 18:04:35 +0800176const (
Colin Cross7113d202019-11-20 16:39:12 -0800177 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900178 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
179 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800180 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800181)
182
Paul Duffindb462dd2021-03-21 22:01:55 +0000183// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
184// running it in a fixture that requires all source files to exist.
185func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
186 android.GroupFixturePreparers(
187 PrepareForTestWithCcDefaultModules,
188 android.PrepareForTestDisallowNonExistentPaths,
189 ).RunTest(t)
190}
191
Jiyong Park6a43f042017-10-12 23:05:00 +0900192func TestVendorSrc(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400193 t.Parallel()
Jiyong Park6a43f042017-10-12 23:05:00 +0900194 ctx := testCc(t, `
195 cc_library {
196 name: "libTest",
197 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700198 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800199 nocrt: true,
200 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900201 vendor_available: true,
202 target: {
203 vendor: {
204 srcs: ["bar.c"],
205 },
206 },
207 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900208 `)
209
Logan Chienf3511742017-10-31 18:04:35 +0800210 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900211 var objs []string
212 for _, o := range ld.Inputs {
213 objs = append(objs, o.Base())
214 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800215 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900216 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
217 }
218}
219
Justin Yun7f99ec72021-04-12 13:19:28 +0900220func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
221 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
222 partitionDefined := false
223 checkPartition := func(specific bool, partition string) {
224 if specific {
225 if expected != partition && !partitionDefined {
226 // The variant is installed to the 'partition'
227 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
228 }
229 partitionDefined = true
230 } else {
231 // The variant is not installed to the 'partition'
232 if expected == partition {
233 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
234 }
235 }
236 }
237 socSpecific := func(m *Module) bool {
238 return m.SocSpecific() || m.socSpecificModuleContext()
239 }
240 deviceSpecific := func(m *Module) bool {
241 return m.DeviceSpecific() || m.deviceSpecificModuleContext()
242 }
243 productSpecific := func(m *Module) bool {
244 return m.ProductSpecific() || m.productSpecificModuleContext()
245 }
246 systemExtSpecific := func(m *Module) bool {
247 return m.SystemExtSpecific()
248 }
249 checkPartition(socSpecific(mod), "vendor")
250 checkPartition(deviceSpecific(mod), "odm")
251 checkPartition(productSpecific(mod), "product")
252 checkPartition(systemExtSpecific(mod), "system_ext")
253 if !partitionDefined && expected != "system" {
254 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
255 " but installed to system partition", variant, name, expected)
256 }
257}
258
259func TestInstallPartition(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400260 t.Parallel()
Justin Yun7f99ec72021-04-12 13:19:28 +0900261 t.Helper()
262 ctx := prepareForCcTest.RunTestWithBp(t, `
263 cc_library {
264 name: "libsystem",
265 }
266 cc_library {
267 name: "libsystem_ext",
268 system_ext_specific: true,
269 }
270 cc_library {
271 name: "libproduct",
272 product_specific: true,
273 }
274 cc_library {
275 name: "libvendor",
276 vendor: true,
277 }
278 cc_library {
279 name: "libodm",
280 device_specific: true,
281 }
282 cc_library {
283 name: "liball_available",
284 vendor_available: true,
285 product_available: true,
286 }
287 cc_library {
288 name: "libsystem_ext_all_available",
289 system_ext_specific: true,
290 vendor_available: true,
291 product_available: true,
292 }
293 cc_library {
294 name: "liball_available_odm",
295 odm_available: true,
296 product_available: true,
297 }
298 cc_library {
299 name: "libproduct_vendoravailable",
300 product_specific: true,
301 vendor_available: true,
302 }
303 cc_library {
304 name: "libproduct_odmavailable",
305 product_specific: true,
306 odm_available: true,
307 }
308 `).TestContext
309
310 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
311 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
312 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
313 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
314 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
315
316 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
317 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
318 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
319
320 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
321 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
322 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
323
324 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
325 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
326 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
327
328 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
329 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
330
331 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
332 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
333}
334
Logan Chienf3511742017-10-31 18:04:35 +0800335func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900336 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800337
Logan Chiend3c59a22018-03-29 14:08:15 +0800338 t.Helper()
339
Justin Yun0ecf0b22020-02-28 15:07:59 +0900340 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800341
342 // Check library properties.
343 lib, ok := mod.compiler.(*libraryDecorator)
344 if !ok {
345 t.Errorf("%q must have libraryDecorator", name)
346 } else if lib.baseInstaller.subDir != subDir {
347 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
348 lib.baseInstaller.subDir)
349 }
350
351 // Check VNDK properties.
352 if mod.vndkdep == nil {
353 t.Fatalf("%q must have `vndkdep`", name)
354 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700355 if !mod.IsVndk() {
356 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800357 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400358 if mod.IsVndkSp() != isVndkSp {
359 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800360 }
361
362 // Check VNDK extension properties.
363 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500364 if mod.IsVndkExt() != isVndkExt {
365 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800366 }
367
368 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
369 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
370 }
371}
372
Jooyung Han2216fb12019-11-06 16:46:15 +0900373func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
374 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800375 content := android.ContentFromFileRuleForTests(t, params)
376 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900377 assertArrayString(t, actual, expected)
378}
379
Jooyung Han097087b2019-10-22 19:32:18 +0900380func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
381 t.Helper()
382 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900383 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
384}
385
386func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
387 t.Helper()
Colin Cross45bce852021-11-11 22:47:54 -0800388 got := ctx.ModuleForTests(module, "android_common").Module().(*vndkLibrariesTxt).fileNames
Colin Cross78212242021-01-06 14:51:30 -0800389 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900390}
391
Logan Chienf3511742017-10-31 18:04:35 +0800392func TestVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400393 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800394 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800395 cc_library {
396 name: "libvndk",
397 vendor_available: true,
398 vndk: {
399 enabled: true,
400 },
401 nocrt: true,
402 }
403
404 cc_library {
405 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900406 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800407 vndk: {
408 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900409 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800410 },
411 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900412 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800413 }
414
415 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900416 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800417 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900418 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800419 vndk: {
420 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900421 },
422 nocrt: true,
423 target: {
424 vendor: {
425 cflags: ["-DTEST"],
426 },
427 product: {
428 cflags: ["-DTEST"],
429 },
430 },
431 }
432
433 cc_library {
434 name: "libvndk_sp",
435 vendor_available: true,
436 vndk: {
437 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800438 support_system_process: true,
439 },
440 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900441 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800442 }
443
444 cc_library {
445 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900446 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800447 vndk: {
448 enabled: true,
449 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900450 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800451 },
452 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900453 target: {
454 vendor: {
455 suffix: "-x",
456 },
457 },
Logan Chienf3511742017-10-31 18:04:35 +0800458 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900459
460 cc_library {
461 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900462 vendor_available: true,
463 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900464 vndk: {
465 enabled: true,
466 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900467 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900468 },
469 nocrt: true,
470 target: {
471 vendor: {
472 suffix: "-x",
473 },
474 product: {
475 suffix: "-x",
476 },
477 },
478 }
479
Justin Yun450ae722021-04-16 19:58:18 +0900480 cc_library {
481 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700482 llndk: {
483 symbol_file: "libllndk.map.txt",
484 export_llndk_headers: ["libllndk_headers"],
485 }
Justin Yun450ae722021-04-16 19:58:18 +0900486 }
487
Justin Yun611e8862021-05-24 18:17:33 +0900488 cc_library {
489 name: "libclang_rt.hwasan-llndk",
490 llndk: {
491 symbol_file: "libclang_rt.hwasan.map.txt",
492 }
493 }
494
Colin Cross627280f2021-04-26 16:53:58 -0700495 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900496 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700497 llndk: {
498 llndk_headers: true,
499 },
Justin Yun450ae722021-04-16 19:58:18 +0900500 export_include_dirs: ["include"],
501 }
502
Colin Crosse4e44bc2020-12-28 13:50:21 -0800503 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900504 name: "llndk.libraries.txt",
505 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800506 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900507 name: "vndkcore.libraries.txt",
508 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800509 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900510 name: "vndksp.libraries.txt",
511 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800512 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900513 name: "vndkprivate.libraries.txt",
514 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800515 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900516 name: "vndkproduct.libraries.txt",
517 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800518 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900519 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800520 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900521 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800522 `
523
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000524 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800525 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900526 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900527 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800528
529 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800530
Jooyung Han261e1582020-10-20 18:54:21 +0900531 // subdir == "" because VNDK libs are not supposed to be installed separately.
532 // They are installed as part of VNDK APEX instead.
533 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
534 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900535 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900536 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
537 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900538 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900539
Justin Yun6977e8a2020-10-29 18:24:11 +0900540 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
541 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900542
Inseob Kim1f086e22019-05-09 13:29:15 +0900543 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900544 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000545 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900546
547 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
548 "arm64", "armv8-a"))
549 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
550 "arm", "armv7-a-neon"))
551
552 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
553 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900554 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
555
Inseob Kim1f086e22019-05-09 13:29:15 +0900556 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
557 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900558 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900559
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900560 variant := "android_vendor.29_arm64_armv8-a_shared"
561 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900562
Inseob Kim7f283f42020-06-01 21:53:49 +0900563 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
564
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400565 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
566 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
567 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
568 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
569 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
570 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
571 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
572 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900573
Jooyung Han39edb6c2019-11-06 16:53:07 +0900574 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Colin Cross45bce852021-11-11 22:47:54 -0800575 CheckSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "android_common")
576 CheckSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "android_common")
577 CheckSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "android_common")
578 CheckSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "android_common")
579 CheckSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "android_common")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900580
Jooyung Han097087b2019-10-22 19:32:18 +0900581 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
582 "LLNDK: libc.so",
583 "LLNDK: libdl.so",
584 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900585 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900586 "LLNDK: libm.so",
587 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900588 "VNDK-SP: libvndk_sp-x.so",
589 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900590 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900591 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900592 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900593 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900594 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900595 "VNDK-private: libvndk-private.so",
596 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900597 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900598 "VNDK-product: libc++.so",
599 "VNDK-product: libvndk_product.so",
600 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900601 })
Justin Yun611e8862021-05-24 18:17:33 +0900602 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 +0900603 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
604 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
605 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 +0900606 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 +0900607 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
608}
609
Yo Chiangbba545e2020-06-09 16:15:37 +0800610func TestVndkWithHostSupported(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400611 t.Parallel()
Yo Chiangbba545e2020-06-09 16:15:37 +0800612 ctx := testCc(t, `
613 cc_library {
614 name: "libvndk_host_supported",
615 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900616 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800617 vndk: {
618 enabled: true,
619 },
620 host_supported: true,
621 }
622
623 cc_library {
624 name: "libvndk_host_supported_but_disabled_on_device",
625 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900626 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800627 vndk: {
628 enabled: true,
629 },
630 host_supported: true,
631 enabled: false,
632 target: {
633 host: {
634 enabled: true,
635 }
636 }
637 }
638
Colin Crosse4e44bc2020-12-28 13:50:21 -0800639 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800640 name: "vndkcore.libraries.txt",
641 }
642 `)
643
644 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
645}
646
Jooyung Han2216fb12019-11-06 16:46:15 +0900647func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400648 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800649 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800650 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900651 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800652 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800653 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000654 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800655 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900656 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Kiyoung Kima2d6dee2023-08-11 10:14:43 +0900657 config.TestProductVariables.KeepVndk = BoolPtr(true)
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)
Colin Crossc85750b2022-04-21 12:50:51 -07002704 actual := android.Paths(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)
Colin Crossc85750b2022-04-21 12:50:51 -07002740 actual := android.Paths(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{
Jingwen Chen6ee23ad2023-07-24 14:56:28 +00003265 "//:test__tf_internal": cquery.CcUnstrippedInfo{
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003266 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 Tran09581952023-05-16 16:03:20 -04004423func TestAidlLibraryWithHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004424 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"],
Vinh Tran09581952023-05-16 16:03:20 -04004433 hdrs: ["x/HeaderBar.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04004434 strip_import_prefix: "x",
4435 }
4436 `)}.AddToFixture(),
4437 android.MockFS{
4438 "package_foo/Android.bp": []byte(`
4439 aidl_library {
4440 name: "foo",
4441 srcs: ["a/b/Foo.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04004442 hdrs: ["a/HeaderFoo.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04004443 strip_import_prefix: "a",
4444 deps: ["bar"],
4445 }
4446 cc_library {
4447 name: "libfoo",
4448 aidl: {
4449 libs: ["foo"],
4450 }
4451 }
4452 `),
4453 }.AddToFixture(),
4454 ).RunTest(t).TestContext
4455
4456 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Vinh Tran09581952023-05-16 16:03:20 -04004457
4458 android.AssertPathsRelativeToTopEquals(
4459 t,
4460 "aidl headers",
4461 []string{
4462 "package_bar/x/HeaderBar.aidl",
4463 "package_foo/a/HeaderFoo.aidl",
4464 "package_foo/a/b/Foo.aidl",
4465 "out/soong/.intermediates/package_foo/libfoo/android_arm64_armv8-a_static/gen/aidl_library.sbox.textproto",
4466 },
4467 libfoo.Rule("aidl_library").Implicits,
4468 )
4469
4470 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl_library.sbox.textproto"))
Vinh Tran367d89d2023-04-28 11:21:25 -04004471 aidlCommand := manifest.Commands[0].GetCommand()
4472
4473 expectedAidlFlags := "-Ipackage_foo/a -Ipackage_bar/x"
4474 if !strings.Contains(aidlCommand, expectedAidlFlags) {
4475 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlags)
4476 }
4477
4478 outputs := strings.Join(libfoo.AllOutputs(), " ")
4479
Vinh Tran09581952023-05-16 16:03:20 -04004480 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BpFoo.h")
4481 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BnFoo.h")
4482 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/Foo.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04004483 android.AssertStringDoesContain(t, "aidl-generated cpp", outputs, "b/Foo.cpp")
4484 // Confirm that the aidl header doesn't get compiled to cpp and h files
Vinh Tran09581952023-05-16 16:03:20 -04004485 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BpBar.h")
4486 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BnBar.h")
4487 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/Bar.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04004488 android.AssertStringDoesNotContain(t, "aidl-generated cpp", outputs, "y/Bar.cpp")
4489}
4490
Jooyung Hane197d8b2021-01-05 10:33:16 +09004491func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004492 t.Parallel()
Vinh Tran367d89d2023-04-28 11:21:25 -04004493 ctx := android.GroupFixturePreparers(
4494 prepareForCcTest,
4495 aidl_library.PrepareForTestWithAidlLibrary,
4496 ).RunTestWithBp(t, `
Jooyung Hane197d8b2021-01-05 10:33:16 +09004497 cc_library {
4498 name: "libfoo",
4499 srcs: ["a/Foo.aidl"],
4500 aidl: { flags: ["-Werror"], },
4501 }
4502 `)
4503
4504 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4505 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4506 aidlCommand := manifest.Commands[0].GetCommand()
4507 expectedAidlFlag := "-Werror"
4508 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4509 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4510 }
4511}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004512
Jooyung Han07f70c02021-11-06 07:08:45 +09004513func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004514 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09004515 for _, tc := range []struct {
4516 name string
4517 sdkVersion string
4518 variant string
4519 expected string
4520 }{
4521 {
4522 name: "default is current",
4523 sdkVersion: "",
4524 variant: "android_arm64_armv8-a_static",
4525 expected: "platform_apis",
4526 },
4527 {
4528 name: "use sdk_version",
4529 sdkVersion: `sdk_version: "29"`,
4530 variant: "android_arm64_armv8-a_static",
4531 expected: "platform_apis",
4532 },
4533 {
4534 name: "use sdk_version(sdk variant)",
4535 sdkVersion: `sdk_version: "29"`,
4536 variant: "android_arm64_armv8-a_sdk_static",
4537 expected: "29",
4538 },
4539 {
4540 name: "use min_sdk_version",
4541 sdkVersion: `min_sdk_version: "29"`,
4542 variant: "android_arm64_armv8-a_static",
4543 expected: "29",
4544 },
4545 } {
4546 t.Run(tc.name, func(t *testing.T) {
4547 ctx := testCc(t, `
4548 cc_library {
4549 name: "libfoo",
4550 stl: "none",
4551 srcs: ["a/Foo.aidl"],
4552 `+tc.sdkVersion+`
4553 }
4554 `)
4555 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
4556 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4557 aidlCommand := manifest.Commands[0].GetCommand()
4558 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4559 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4560 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4561 }
4562 })
4563 }
4564}
4565
Vinh Tran09581952023-05-16 16:03:20 -04004566func TestInvalidAidlProp(t *testing.T) {
4567 t.Parallel()
4568
4569 testCases := []struct {
4570 description string
4571 bp string
4572 }{
4573 {
4574 description: "Invalid use of aidl.libs and aidl.include_dirs",
4575 bp: `
4576 cc_library {
4577 name: "foo",
4578 aidl: {
4579 libs: ["foo_aidl"],
4580 include_dirs: ["bar/include"],
4581 }
4582 }
4583 `,
4584 },
4585 {
4586 description: "Invalid use of aidl.libs and aidl.local_include_dirs",
4587 bp: `
4588 cc_library {
4589 name: "foo",
4590 aidl: {
4591 libs: ["foo_aidl"],
4592 local_include_dirs: ["include"],
4593 }
4594 }
4595 `,
4596 },
4597 }
4598
4599 for _, testCase := range testCases {
4600 t.Run(testCase.description, func(t *testing.T) {
4601 bp := `
4602 aidl_library {
4603 name: "foo_aidl",
4604 srcs: ["Foo.aidl"],
4605 } ` + testCase.bp
4606 android.GroupFixturePreparers(
4607 prepareForCcTest,
4608 aidl_library.PrepareForTestWithAidlLibrary.
4609 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("For aidl headers, please only use aidl.libs prop")),
4610 ).RunTestWithBp(t, bp)
4611 })
4612 }
4613}
4614
Jiyong Parka008fb02021-03-16 17:15:53 +09004615func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004616 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004617 ctx := testCc(t, `
4618 cc_library_shared {
4619 name: "libfoo",
4620 srcs: ["foo.c"],
4621 min_sdk_version: "29",
4622 }`)
4623
4624 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4625 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4626}
4627
Vinh Tranf1924742022-06-24 16:40:11 -04004628func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004629 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004630 bp := `
4631 cc_library_shared {
4632 name: "libfoo",
4633 srcs: ["foo.c"],
4634 min_sdk_version: "S",
4635 }
4636 `
4637 result := android.GroupFixturePreparers(
4638 prepareForCcTest,
4639 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4640 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4641 }),
4642 ).RunTestWithBp(t, bp)
4643 ctx := result.TestContext
4644 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4645 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4646}
4647
Paul Duffin3cb603e2021-02-19 13:57:10 +00004648func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004649 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004650
4651 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4652 // embedded newline characters alone.
4653 trimIndentingSpaces := func(s string) string {
4654 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4655 }
4656
4657 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4658 t.Helper()
4659 expected = trimIndentingSpaces(expected)
4660 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4661 if expected != actual {
4662 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4663 }
4664 }
4665
4666 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4667
4668 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4669 t.Helper()
4670 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4671 name := module.Name()
4672
4673 for _, checker := range checkers {
4674 checker(t, name, exported)
4675 }
4676 }
4677
4678 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4679 return func(t *testing.T, name string, exported FlagExporterInfo) {
4680 t.Helper()
4681 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4682 }
4683 }
4684
4685 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4686 return func(t *testing.T, name string, exported FlagExporterInfo) {
4687 t.Helper()
4688 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4689 }
4690 }
4691
4692 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4693 return func(t *testing.T, name string, exported FlagExporterInfo) {
4694 t.Helper()
4695 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4696 }
4697 }
4698
4699 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4700 return func(t *testing.T, name string, exported FlagExporterInfo) {
4701 t.Helper()
4702 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4703 }
4704 }
4705
4706 genRuleModules := `
4707 genrule {
4708 name: "genrule_foo",
4709 cmd: "generate-foo",
4710 out: [
4711 "generated_headers/foo/generated_header.h",
4712 ],
4713 export_include_dirs: [
4714 "generated_headers",
4715 ],
4716 }
4717
4718 genrule {
4719 name: "genrule_bar",
4720 cmd: "generate-bar",
4721 out: [
4722 "generated_headers/bar/generated_header.h",
4723 ],
4724 export_include_dirs: [
4725 "generated_headers",
4726 ],
4727 }
4728 `
4729
4730 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4731 ctx := testCc(t, genRuleModules+`
4732 cc_library {
4733 name: "libfoo",
4734 srcs: ["foo.c"],
4735 export_include_dirs: ["foo/standard"],
4736 export_system_include_dirs: ["foo/system"],
4737 generated_headers: ["genrule_foo"],
4738 export_generated_headers: ["genrule_foo"],
4739 }
4740
4741 cc_library {
4742 name: "libbar",
4743 srcs: ["bar.c"],
4744 shared_libs: ["libfoo"],
4745 export_include_dirs: ["bar/standard"],
4746 export_system_include_dirs: ["bar/system"],
4747 generated_headers: ["genrule_bar"],
4748 export_generated_headers: ["genrule_bar"],
4749 }
4750 `)
4751 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4752 checkIncludeDirs(t, ctx, foo,
4753 expectedIncludeDirs(`
4754 foo/standard
4755 .intermediates/genrule_foo/gen/generated_headers
4756 `),
4757 expectedSystemIncludeDirs(`foo/system`),
4758 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4759 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4760 )
4761
4762 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4763 checkIncludeDirs(t, ctx, bar,
4764 expectedIncludeDirs(`
4765 bar/standard
4766 .intermediates/genrule_bar/gen/generated_headers
4767 `),
4768 expectedSystemIncludeDirs(`bar/system`),
4769 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4770 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4771 )
4772 })
4773
4774 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4775 ctx := testCc(t, genRuleModules+`
4776 cc_library {
4777 name: "libfoo",
4778 srcs: ["foo.c"],
4779 export_include_dirs: ["foo/standard"],
4780 export_system_include_dirs: ["foo/system"],
4781 generated_headers: ["genrule_foo"],
4782 export_generated_headers: ["genrule_foo"],
4783 }
4784
4785 cc_library {
4786 name: "libbar",
4787 srcs: ["bar.c"],
4788 whole_static_libs: ["libfoo"],
4789 export_include_dirs: ["bar/standard"],
4790 export_system_include_dirs: ["bar/system"],
4791 generated_headers: ["genrule_bar"],
4792 export_generated_headers: ["genrule_bar"],
4793 }
4794 `)
4795 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4796 checkIncludeDirs(t, ctx, foo,
4797 expectedIncludeDirs(`
4798 foo/standard
4799 .intermediates/genrule_foo/gen/generated_headers
4800 `),
4801 expectedSystemIncludeDirs(`foo/system`),
4802 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4803 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4804 )
4805
4806 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4807 checkIncludeDirs(t, ctx, bar,
4808 expectedIncludeDirs(`
4809 bar/standard
4810 foo/standard
4811 .intermediates/genrule_foo/gen/generated_headers
4812 .intermediates/genrule_bar/gen/generated_headers
4813 `),
4814 expectedSystemIncludeDirs(`
4815 bar/system
4816 foo/system
4817 `),
4818 expectedGeneratedHeaders(`
4819 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4820 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4821 `),
4822 expectedOrderOnlyDeps(`
4823 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4824 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4825 `),
4826 )
4827 })
4828
Paul Duffin3cb603e2021-02-19 13:57:10 +00004829 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004830 ctx := android.GroupFixturePreparers(
4831 prepareForCcTest,
4832 aidl_library.PrepareForTestWithAidlLibrary,
4833 ).RunTestWithBp(t, `
4834 aidl_library {
4835 name: "libfoo_aidl",
4836 srcs: ["x/y/Bar.aidl"],
4837 strip_import_prefix: "x",
4838 }
Paul Duffin3cb603e2021-02-19 13:57:10 +00004839 cc_library_shared {
4840 name: "libfoo",
4841 srcs: [
4842 "foo.c",
4843 "b.aidl",
4844 "a.proto",
4845 ],
4846 aidl: {
Vinh Tran367d89d2023-04-28 11:21:25 -04004847 libs: ["libfoo_aidl"],
Paul Duffin3cb603e2021-02-19 13:57:10 +00004848 export_aidl_headers: true,
4849 }
4850 }
Vinh Tran367d89d2023-04-28 11:21:25 -04004851 `).TestContext
Paul Duffin3cb603e2021-02-19 13:57:10 +00004852 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4853 checkIncludeDirs(t, ctx, foo,
4854 expectedIncludeDirs(`
4855 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
Vinh Tran09581952023-05-16 16:03:20 -04004856 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library
Paul Duffin3cb603e2021-02-19 13:57:10 +00004857 `),
4858 expectedSystemIncludeDirs(``),
4859 expectedGeneratedHeaders(`
4860 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4861 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4862 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004863 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4864 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4865 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004866 `),
4867 expectedOrderOnlyDeps(`
4868 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4869 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4870 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004871 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4872 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4873 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004874 `),
4875 )
4876 })
4877
Paul Duffin3cb603e2021-02-19 13:57:10 +00004878 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4879 ctx := testCc(t, genRuleModules+`
4880 cc_library_shared {
4881 name: "libfoo",
4882 srcs: [
4883 "foo.c",
4884 "b.aidl",
4885 "a.proto",
4886 ],
4887 proto: {
4888 export_proto_headers: true,
4889 }
4890 }
4891 `)
4892 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4893 checkIncludeDirs(t, ctx, foo,
4894 expectedIncludeDirs(`
4895 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4896 `),
4897 expectedSystemIncludeDirs(``),
4898 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004899 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4900 `),
4901 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004902 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4903 `),
4904 )
4905 })
4906
Paul Duffin33056e82021-02-19 13:49:08 +00004907 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004908 ctx := testCc(t, genRuleModules+`
4909 cc_library_shared {
4910 name: "libfoo",
4911 srcs: [
4912 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004913 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004914 "b.aidl",
4915 "a.proto",
4916 ],
4917 }
4918 `)
4919 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4920 checkIncludeDirs(t, ctx, foo,
4921 expectedIncludeDirs(`
4922 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4923 `),
4924 expectedSystemIncludeDirs(``),
4925 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004926 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004927 `),
4928 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004929 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4930 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004931 `),
4932 )
4933 })
4934}
Colin Crossae628182021-06-14 16:52:28 -07004935
4936func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004937 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004938 baseExpectedFlags := []string{
4939 "${config.ArmThumbCflags}",
4940 "${config.ArmCflags}",
4941 "${config.CommonGlobalCflags}",
4942 "${config.DeviceGlobalCflags}",
4943 "${config.ExternalCflags}",
4944 "${config.ArmToolchainCflags}",
4945 "${config.ArmArmv7ANeonCflags}",
4946 "${config.ArmGenericCflags}",
4947 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004948 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004949 }
4950
4951 expectedIncludes := []string{
4952 "external/foo/android_arm_export_include_dirs",
4953 "external/foo/lib32_export_include_dirs",
4954 "external/foo/arm_export_include_dirs",
4955 "external/foo/android_export_include_dirs",
4956 "external/foo/linux_export_include_dirs",
4957 "external/foo/export_include_dirs",
4958 "external/foo/android_arm_local_include_dirs",
4959 "external/foo/lib32_local_include_dirs",
4960 "external/foo/arm_local_include_dirs",
4961 "external/foo/android_local_include_dirs",
4962 "external/foo/linux_local_include_dirs",
4963 "external/foo/local_include_dirs",
4964 "external/foo",
4965 "external/foo/libheader1",
4966 "external/foo/libheader2",
4967 "external/foo/libwhole1",
4968 "external/foo/libwhole2",
4969 "external/foo/libstatic1",
4970 "external/foo/libstatic2",
4971 "external/foo/libshared1",
4972 "external/foo/libshared2",
4973 "external/foo/liblinux",
4974 "external/foo/libandroid",
4975 "external/foo/libarm",
4976 "external/foo/lib32",
4977 "external/foo/libandroid_arm",
4978 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004979 }
4980
4981 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4982 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4983
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004984 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesfb294e32023-06-14 10:42:45 -07004985 cstd := []string{"-std=gnu17", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004986 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004987
4988 lastIncludes := []string{
4989 "out/soong/ndk/sysroot/usr/include",
4990 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4991 }
4992
4993 combineSlices := func(slices ...[]string) []string {
4994 var ret []string
4995 for _, s := range slices {
4996 ret = append(ret, s...)
4997 }
4998 return ret
4999 }
5000
5001 testCases := []struct {
5002 name string
5003 src string
5004 expected []string
5005 }{
5006 {
5007 name: "c",
5008 src: "foo.c",
Yi Kong13beeed2023-07-15 03:09:00 +09005009 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04005010 },
5011 {
5012 name: "cc",
5013 src: "foo.cc",
Yi Kong13beeed2023-07-15 03:09:00 +09005014 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04005015 },
5016 {
5017 name: "assemble",
5018 src: "foo.s",
Yi Kong13beeed2023-07-15 03:09:00 +09005019 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04005020 },
5021 }
5022
5023 for _, tc := range testCases {
5024 t.Run(tc.name, func(t *testing.T) {
5025 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07005026 cc_library {
5027 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04005028 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05005029 cflags: ["-std=candcpp"],
5030 conlyflags: ["-std=conly"],
5031 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07005032 local_include_dirs: ["local_include_dirs"],
5033 export_include_dirs: ["export_include_dirs"],
5034 export_system_include_dirs: ["export_system_include_dirs"],
5035 static_libs: ["libstatic1", "libstatic2"],
5036 whole_static_libs: ["libwhole1", "libwhole2"],
5037 shared_libs: ["libshared1", "libshared2"],
5038 header_libs: ["libheader1", "libheader2"],
5039 target: {
5040 android: {
5041 shared_libs: ["libandroid"],
5042 local_include_dirs: ["android_local_include_dirs"],
5043 export_include_dirs: ["android_export_include_dirs"],
5044 },
5045 android_arm: {
5046 shared_libs: ["libandroid_arm"],
5047 local_include_dirs: ["android_arm_local_include_dirs"],
5048 export_include_dirs: ["android_arm_export_include_dirs"],
5049 },
5050 linux: {
5051 shared_libs: ["liblinux"],
5052 local_include_dirs: ["linux_local_include_dirs"],
5053 export_include_dirs: ["linux_export_include_dirs"],
5054 },
5055 },
5056 multilib: {
5057 lib32: {
5058 shared_libs: ["lib32"],
5059 local_include_dirs: ["lib32_local_include_dirs"],
5060 export_include_dirs: ["lib32_export_include_dirs"],
5061 },
5062 },
5063 arch: {
5064 arm: {
5065 shared_libs: ["libarm"],
5066 local_include_dirs: ["arm_local_include_dirs"],
5067 export_include_dirs: ["arm_export_include_dirs"],
5068 },
5069 },
5070 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005071 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005072 }
5073
5074 cc_library_headers {
5075 name: "libheader1",
5076 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005077 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005078 stl: "none",
5079 }
5080
5081 cc_library_headers {
5082 name: "libheader2",
5083 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005084 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005085 stl: "none",
5086 }
Liz Kammer08572c62021-09-30 10:11:04 -04005087 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07005088
Liz Kammer08572c62021-09-30 10:11:04 -04005089 libs := []string{
5090 "libstatic1",
5091 "libstatic2",
5092 "libwhole1",
5093 "libwhole2",
5094 "libshared1",
5095 "libshared2",
5096 "libandroid",
5097 "libandroid_arm",
5098 "liblinux",
5099 "lib32",
5100 "libarm",
5101 }
Colin Crossae628182021-06-14 16:52:28 -07005102
Liz Kammer08572c62021-09-30 10:11:04 -04005103 for _, lib := range libs {
5104 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07005105 cc_library {
5106 name: "%s",
5107 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005108 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005109 stl: "none",
5110 }
5111 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04005112 }
5113
5114 ctx := android.GroupFixturePreparers(
5115 PrepareForIntegrationTestWithCc,
5116 android.FixtureAddTextFile("external/foo/Android.bp", bp),
5117 ).RunTest(t)
5118 // Use the arm variant instead of the arm64 variant so that it gets headers from
5119 // ndk_libandroid_support to test LateStaticLibs.
5120 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
5121
5122 var includes []string
5123 flags := strings.Split(cflags, " ")
5124 for _, flag := range flags {
5125 if strings.HasPrefix(flag, "-I") {
5126 includes = append(includes, strings.TrimPrefix(flag, "-I"))
5127 } else if flag == "-isystem" {
5128 // skip isystem, include next
5129 } else if len(flag) > 0 {
5130 includes = append(includes, flag)
5131 }
5132 }
5133
5134 android.AssertArrayString(t, "includes", tc.expected, includes)
5135 })
Colin Crossae628182021-06-14 16:52:28 -07005136 }
5137
Colin Crossae628182021-06-14 16:52:28 -07005138}
Alixb5f6d9e2022-04-20 23:00:58 +00005139
zijunzhao933e3802023-01-12 07:26:20 +00005140func TestAddnoOverride64GlobalCflags(t *testing.T) {
5141 t.Parallel()
5142 ctx := testCc(t, `
5143 cc_library_shared {
5144 name: "libclient",
5145 srcs: ["foo.c"],
5146 shared_libs: ["libfoo#1"],
5147 }
5148
5149 cc_library_shared {
5150 name: "libfoo",
5151 srcs: ["foo.c"],
5152 shared_libs: ["libbar"],
5153 export_shared_lib_headers: ["libbar"],
5154 stubs: {
5155 symbol_file: "foo.map.txt",
5156 versions: ["1", "2", "3"],
5157 },
5158 }
5159
5160 cc_library_shared {
5161 name: "libbar",
5162 export_include_dirs: ["include/libbar"],
5163 srcs: ["foo.c"],
5164 }`)
5165
5166 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
5167
5168 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
5169 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
5170 }
5171}
5172
Alixb5f6d9e2022-04-20 23:00:58 +00005173func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005174 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00005175 tests := []struct {
5176 name string
5177 clang bool
5178 BuildBrokenClangProperty bool
5179 err string
5180 }{
5181 {
5182 name: "error when clang is set to false",
5183 clang: false,
5184 err: "is no longer supported",
5185 },
5186 {
5187 name: "error when clang is set to true",
5188 clang: true,
5189 err: "property is deprecated, see Changes.md",
5190 },
5191 {
5192 name: "no error when BuildBrokenClangProperty is explicitly set to true",
5193 clang: true,
5194 BuildBrokenClangProperty: true,
5195 },
5196 }
5197
5198 for _, test := range tests {
5199 t.Run(test.name, func(t *testing.T) {
5200 bp := fmt.Sprintf(`
5201 cc_library {
5202 name: "foo",
5203 clang: %t,
5204 }`, test.clang)
5205
5206 if test.err == "" {
5207 android.GroupFixturePreparers(
5208 prepareForCcTest,
5209 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5210 if test.BuildBrokenClangProperty {
5211 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
5212 }
5213 }),
5214 ).RunTestWithBp(t, bp)
5215 } else {
5216 prepareForCcTest.
5217 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5218 RunTestWithBp(t, bp)
5219 }
5220 })
5221 }
5222}
Alix Espinoef47e542022-09-14 19:10:51 +00005223
5224func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005225 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005226 tests := []struct {
5227 name string
5228 clangAsFlags []string
5229 BuildBrokenClangAsFlags bool
5230 err string
5231 }{
5232 {
5233 name: "error when clang_asflags is set",
5234 clangAsFlags: []string{"-a", "-b"},
5235 err: "clang_asflags: property is deprecated",
5236 },
5237 {
5238 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
5239 clangAsFlags: []string{"-a", "-b"},
5240 BuildBrokenClangAsFlags: true,
5241 },
5242 }
5243
5244 for _, test := range tests {
5245 t.Run(test.name, func(t *testing.T) {
5246 bp := fmt.Sprintf(`
5247 cc_library {
5248 name: "foo",
5249 clang_asflags: %s,
5250 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
5251
5252 if test.err == "" {
5253 android.GroupFixturePreparers(
5254 prepareForCcTest,
5255 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5256 if test.BuildBrokenClangAsFlags {
5257 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
5258 }
5259 }),
5260 ).RunTestWithBp(t, bp)
5261 } else {
5262 prepareForCcTest.
5263 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5264 RunTestWithBp(t, bp)
5265 }
5266 })
5267 }
5268}
5269
5270func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005271 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005272 tests := []struct {
5273 name string
5274 clangCFlags []string
5275 BuildBrokenClangCFlags bool
5276 err string
5277 }{
5278 {
5279 name: "error when clang_cflags is set",
5280 clangCFlags: []string{"-a", "-b"},
5281 err: "clang_cflags: property is deprecated",
5282 },
5283 {
5284 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
5285 clangCFlags: []string{"-a", "-b"},
5286 BuildBrokenClangCFlags: true,
5287 },
5288 }
5289
5290 for _, test := range tests {
5291 t.Run(test.name, func(t *testing.T) {
5292 bp := fmt.Sprintf(`
5293 cc_library {
5294 name: "foo",
5295 clang_cflags: %s,
5296 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
5297
5298 if test.err == "" {
5299 android.GroupFixturePreparers(
5300 prepareForCcTest,
5301 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5302 if test.BuildBrokenClangCFlags {
5303 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
5304 }
5305 }),
5306 ).RunTestWithBp(t, bp)
5307 } else {
5308 prepareForCcTest.
5309 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5310 RunTestWithBp(t, bp)
5311 }
5312 })
5313 }
5314}
Yu Liue4312402023-01-18 09:15:31 -08005315
5316func TestDclaLibraryInApex(t *testing.T) {
5317 t.Parallel()
5318 bp := `
5319 cc_library_shared {
5320 name: "cc_lib_in_apex",
5321 srcs: ["foo.cc"],
5322 apex_available: ["myapex"],
5323 bazel_module: { label: "//foo/bar:bar" },
5324 }`
5325 label := "//foo/bar:bar"
5326 arch64 := "arm64_armv8-a"
5327 arch32 := "arm_armv7-a-neon"
5328 apexCfgKey := android.ApexConfigKey{
5329 WithinApex: true,
5330 ApexSdkVersion: "28",
5331 }
5332
5333 result := android.GroupFixturePreparers(
5334 prepareForCcTest,
5335 android.FixtureRegisterWithContext(registerTestMutators),
5336 android.FixtureModifyConfig(func(config android.Config) {
5337 config.BazelContext = android.MockBazelContext{
5338 OutputBaseDir: "outputbase",
5339 LabelToCcInfo: map[string]cquery.CcInfo{
5340 android.BuildMockBazelContextResultKey(label, arch32, android.Android, apexCfgKey): cquery.CcInfo{
5341 RootDynamicLibraries: []string{"foo.so"},
5342 },
5343 android.BuildMockBazelContextResultKey(label, arch64, android.Android, apexCfgKey): cquery.CcInfo{
5344 RootDynamicLibraries: []string{"foo.so"},
5345 },
5346 },
5347 BazelRequests: make(map[string]bool),
5348 }
5349 }),
5350 ).RunTestWithBp(t, bp)
5351 ctx := result.TestContext
5352
5353 // Test if the bazel request is queued correctly
5354 key := android.BuildMockBazelContextRequestKey(label, cquery.GetCcInfo, arch32, android.Android, apexCfgKey)
5355 if !ctx.Config().BazelContext.(android.MockBazelContext).BazelRequests[key] {
5356 t.Errorf("Bazel request was not queued: %s", key)
5357 }
5358
5359 sharedFoo := ctx.ModuleForTests(ccLibInApex, "android_arm_armv7-a-neon_shared_"+apexVariationName).Module()
5360 producer := sharedFoo.(android.OutputFileProducer)
5361 outputFiles, err := producer.OutputFiles("")
5362 if err != nil {
5363 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5364 }
5365 expectedOutputFiles := []string{"outputbase/execroot/__main__/foo.so"}
5366 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
5367}
Sam Delmericoef69d472023-04-18 17:32:43 -04005368
5369func TestDisableSanitizerVariantsInMixedBuilds(t *testing.T) {
5370 t.Parallel()
5371 bp := `
5372 cc_library_static {
5373 name: "foo_ubsan_minimal",
5374 srcs: ["foo.cc"],
5375 bazel_module: { label: "//foo_ubsan_minimal" },
5376 sanitize: {
5377 all_undefined: true,
5378 integer_overflow: true,
5379 },
5380 }
5381 cc_library_static {
5382 name: "foo",
5383 srcs: ["foo.cc"],
5384 bazel_module: { label: "//foo" },
5385 sanitize: {
5386 address: true,
5387 hwaddress: true,
5388 fuzzer: true,
5389 integer_overflow: true,
5390 scs: true,
5391 },
5392 }
5393 cc_library_static {
5394 name: "foo_tsan",
5395 srcs: ["foo.cc"],
5396 bazel_module: { label: "//foo_tsan" },
5397 sanitize: {
5398 thread: true,
5399 },
5400 }
5401 cc_library_static {
5402 name: "foo_cfi",
5403 srcs: ["foo.cc"],
5404 bazel_module: { label: "//foo_cfi" },
5405 sanitize: {
5406 cfi: true,
5407 },
5408 }
5409 cc_library_static {
5410 name: "foo_memtag_stack",
5411 srcs: ["foo.cc"],
5412 bazel_module: { label: "//foo_memtag_stack" },
5413 sanitize: {
5414 memtag_stack: true,
5415 },
5416 }
5417 cc_library_static {
5418 name: "foo_memtag_heap",
5419 srcs: ["foo.cc"],
5420 bazel_module: { label: "//foo_memtag_heap" },
5421 sanitize: {
5422 memtag_heap: true,
5423 },
5424 }
5425 cc_library_static {
5426 name: "foo_safestack",
5427 srcs: ["foo.cc"],
5428 bazel_module: { label: "//foo_safestack" },
5429 sanitize: {
5430 safestack: true,
5431 },
5432 }
5433 cc_library_static {
5434 name: "foo_scudo",
5435 srcs: ["foo.cc"],
5436 bazel_module: { label: "//foo_scudo" },
5437 sanitize: {
5438 scudo: true,
5439 },
5440 }
5441 `
5442 testcases := []struct {
5443 name string
5444 variant string
5445 expectedOutputPaths []string
5446 }{
5447 {
5448 name: "foo_ubsan_minimal",
5449 variant: "android_arm64_armv8-a_static_apex28",
5450 expectedOutputPaths: []string{
5451 "outputbase/execroot/__main__/foo_ubsan_minimal.a",
5452 },
5453 },
5454 {
5455 name: "foo",
5456 variant: "android_arm64_armv8-a_static_apex28",
5457 expectedOutputPaths: []string{
5458 "outputbase/execroot/__main__/foo.a",
5459 },
5460 },
5461 {
5462 name: "foo",
5463 variant: "android_arm_armv7-a-neon_static_asan_apex28",
5464 expectedOutputPaths: []string{
5465 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_apex28/foo.a",
5466 },
5467 },
5468 {
5469 name: "foo",
5470 variant: "android_arm64_armv8-a_static_hwasan_apex28",
5471 expectedOutputPaths: []string{
5472 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_apex28/foo.a",
5473 },
5474 },
5475 {
5476 name: "foo",
5477 variant: "android_arm64_armv8-a_static_fuzzer_apex28",
5478 expectedOutputPaths: []string{
5479 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_fuzzer_apex28/foo.a",
5480 },
5481 },
5482 {
5483 name: "foo",
5484 variant: "android_arm_armv7-a-neon_static_asan_fuzzer_apex28",
5485 expectedOutputPaths: []string{
5486 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_fuzzer_apex28/foo.a",
5487 },
5488 },
5489 {
5490 name: "foo",
5491 variant: "android_arm64_armv8-a_static_hwasan_fuzzer_apex28",
5492 expectedOutputPaths: []string{
5493 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_fuzzer_apex28/foo.a",
5494 },
5495 },
5496 {
5497 name: "foo",
5498 variant: "android_arm64_armv8-a_static_scs_apex28",
5499 expectedOutputPaths: []string{
5500 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_scs_apex28/foo.a",
5501 },
5502 },
5503 {
5504 name: "foo",
5505 variant: "android_arm64_armv8-a_static_hwasan_scs_apex28",
5506 expectedOutputPaths: []string{
5507 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_apex28/foo.a",
5508 },
5509 },
5510 {
5511 name: "foo",
5512 variant: "android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28",
5513 expectedOutputPaths: []string{
5514 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28/foo.a",
5515 },
5516 },
5517 {
5518 name: "foo_tsan",
5519 variant: "android_arm64_armv8-a_static_apex28",
5520 expectedOutputPaths: []string{
5521 "outputbase/execroot/__main__/foo_tsan.a",
5522 },
5523 },
5524 {
5525 name: "foo_tsan",
5526 variant: "android_arm64_armv8-a_static_tsan_apex28",
5527 expectedOutputPaths: []string{
5528 "out/soong/.intermediates/foo_tsan/android_arm64_armv8-a_static_tsan_apex28/foo_tsan.a",
5529 },
5530 },
5531 {
5532 name: "foo_cfi",
5533 variant: "android_arm64_armv8-a_static_apex28",
5534 expectedOutputPaths: []string{
5535 "outputbase/execroot/__main__/foo_cfi.a",
5536 },
5537 },
5538 {
5539 name: "foo_cfi",
5540 variant: "android_arm64_armv8-a_static_cfi_apex28",
5541 expectedOutputPaths: []string{
Yu Liu95497dc2023-05-25 11:15:07 -07005542 "outputbase/execroot/__main__/foo_cfi.a",
Sam Delmericoef69d472023-04-18 17:32:43 -04005543 },
5544 },
5545 {
5546 name: "foo_memtag_stack",
5547 variant: "android_arm64_armv8-a_static_apex28",
5548 expectedOutputPaths: []string{
5549 "out/soong/.intermediates/foo_memtag_stack/android_arm64_armv8-a_static_apex28/foo_memtag_stack.a",
5550 },
5551 },
5552 {
5553 name: "foo_memtag_heap",
5554 variant: "android_arm64_armv8-a_static_apex28",
5555 expectedOutputPaths: []string{
5556 "out/soong/.intermediates/foo_memtag_heap/android_arm64_armv8-a_static_apex28/foo_memtag_heap.a",
5557 },
5558 },
5559 {
5560 name: "foo_safestack",
5561 variant: "android_arm64_armv8-a_static_apex28",
5562 expectedOutputPaths: []string{
5563 "out/soong/.intermediates/foo_safestack/android_arm64_armv8-a_static_apex28/foo_safestack.a",
5564 },
5565 },
5566 {
5567 name: "foo_scudo",
5568 variant: "android_arm64_armv8-a_static_apex28",
5569 expectedOutputPaths: []string{
5570 "out/soong/.intermediates/foo_scudo/android_arm64_armv8-a_static_apex28/foo_scudo.a",
5571 },
5572 },
5573 }
5574
5575 ctx := android.GroupFixturePreparers(
5576 prepareForCcTest,
5577 prepareForAsanTest,
5578 android.FixtureRegisterWithContext(registerTestMutators),
5579 android.FixtureModifyConfig(func(config android.Config) {
5580 config.BazelContext = android.MockBazelContext{
5581 OutputBaseDir: "outputbase",
5582 LabelToCcInfo: map[string]cquery.CcInfo{
5583 "//foo_ubsan_minimal": {
5584 RootStaticArchives: []string{"foo_ubsan_minimal.a"},
5585 },
5586 "//foo": {
5587 RootStaticArchives: []string{"foo.a"},
5588 },
5589 "//foo_tsan": {
5590 RootStaticArchives: []string{"foo_tsan.a"},
5591 },
5592 "//foo_cfi": {
5593 RootStaticArchives: []string{"foo_cfi.a"},
5594 },
5595 "//foo_memtag_stack": {
5596 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5597 },
5598 "//foo_memtag_heap": {
5599 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5600 },
5601 "//foo_safestack": {
5602 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5603 },
5604 "//foo_scudo": {
5605 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5606 },
5607 },
5608 }
5609 }),
5610 ).RunTestWithBp(t, bp).TestContext
5611
5612 for _, tc := range testcases {
5613 fooMod := ctx.ModuleForTests(tc.name, tc.variant).Module()
5614 outputFiles, err := fooMod.(android.OutputFileProducer).OutputFiles("")
5615 if err != nil {
5616 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5617 }
5618 android.AssertPathsRelativeToTopEquals(t, "output files", tc.expectedOutputPaths, outputFiles)
5619 }
5620}